mvp_balloon.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Linux 2.6.32 and later Kernel module for VMware MVP Hypervisor Support
  3. *
  4. * Copyright (C) 2010-2013 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; see the file COPYING. If not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #line 5
  20. /**
  21. * @file
  22. *
  23. * @brief Common guest/host balloon state machine.
  24. */
  25. #ifndef _MVP_BALLOON_H
  26. #define _MVP_BALLOON_H
  27. #define INCLUDE_ALLOW_VMX
  28. #define INCLUDE_ALLOW_PV
  29. #define INCLUDE_ALLOW_GPL
  30. #define INCLUDE_ALLOW_GUESTUSER
  31. #define INCLUDE_ALLOW_MODULE
  32. #include "include_check.h"
  33. /**
  34. * @brief Balloon watchdog timeout (in seconds).
  35. *
  36. * If we don't hear back from the guest balloon driver in this amount of time,
  37. * we terminate the guest.
  38. *
  39. * This can sound arbitrary long but we need to deal with checkpointing. The
  40. * watchdog goal is only to not let not-responding VM running for ages.
  41. */
  42. #define BALLOON_WATCHDOG_TIMEOUT_SECS 90
  43. /**
  44. * @brief Threshold to distinguish an oom_score_adj from an oom_adj value.
  45. */
  46. #define USE_OOM_SCORE_ADJ_THRESHOLD 50
  47. /**
  48. * @brief MVP_BALLOON_GET_DELTA return.
  49. */
  50. typedef union {
  51. struct {
  52. /**< Number/direction balloon adjustment in pages. */
  53. int32 delta:21;
  54. };
  55. uint32 u;
  56. } Balloon_GetDeltaRet;
  57. /**
  58. * @name Guest settings for lowmemorykiller oom_adj and minfree thresholds, as reflected in
  59. * the guest's /sys/module/lowmemorykiller/parameters/{minfree,adj}.
  60. *
  61. * @{
  62. */
  63. /**
  64. * @brief Android low memory killer thresholds (in pages).
  65. */
  66. typedef enum {
  67. BALLOON_ANDROID_GUEST_MIN_FREE_FOREGROUND_APP_PAGES = 1536,
  68. BALLOON_ANDROID_GUEST_MIN_FREE_VISIBLE_APP_PAGES = 2048,
  69. BALLOON_ANDROID_GUEST_MIN_FREE_SECONDARY_SERVER_PAGES = 4096,
  70. BALLOON_ANDROID_GUEST_MIN_FREE_BACKUP_APP_PAGES = 4096,
  71. BALLOON_ANDROID_GUEST_MIN_FREE_HOME_APP_PAGES = 4096,
  72. BALLOON_ANDROID_GUEST_MIN_FREE_HIDDEN_APP_PAGES = 5120,
  73. BALLOON_ANDROID_GUEST_MIN_FREE_CONTENT_PROVIDER_PAGES = 5632,
  74. BALLOON_ANDROID_GUEST_MIN_FREE_EMPTY_APP_MEM_PAGES = 6144
  75. } Balloon_AndroidGuestMinFreePages;
  76. /* @} */
  77. /**
  78. * @brief Host Android levels for the various thresholds.
  79. *
  80. * From framework/base/services/java/com/android/server/am/ProcessList.java
  81. * mOomAdj[]
  82. */
  83. typedef enum {
  84. HOST_FOREGROUND_APP = 0,
  85. HOST_VISIBLE_APP = 1,
  86. HOST_PERCEPTIBLE_APP = 2,
  87. HOST_BACKUP_APP = 3,
  88. HOST_HIDDEN_APP_MIN = 4,
  89. HOST_HIDDEN_APP_MAX = 5,
  90. } Balloon_AndroidHostApp;
  91. /**
  92. * @brief Calculate distance to the point at which Android will terminate
  93. * processes.
  94. *
  95. * In the balloon policy we strive to maintain the low memory killer minfree
  96. * value (e.g. max(freePages, filePages)) above the threshold for terminating
  97. * empty apps (as per the Android low memory killer's logic). Here we measure
  98. * the number of pages we have buffering us from this point.
  99. *
  100. * We chose the empty app threshold instead instead of the home app threshold,
  101. * the threshold we ultimately want to avoid crossing for two reasons:
  102. * - We want to avoid any error introduced by the use of max(free, file) when
  103. * between the two thresholds from interfering with the errorBackground term
  104. * in the balloon policy. If we instead measure the distance to the home app
  105. * threshold, we can get into the situation that even when both sides have
  106. * balanced background pages and the same low memory distance, different
  107. * free/file ratios in the two worlds introduces a further bias.
  108. * - It's helpful in avoiding extreme situations where the balloon won't be able
  109. * to adapt quickly to leave a buffer. With empty app minfree as the target,
  110. * when background pages drops to zero, and both worlds are below the empty
  111. * app minfree target, the balloon will stop adjusting, leaving each world to
  112. * fend for itself. At this point, the worlds have a maximum of 8192 pages
  113. * (using the above logic) until they start killing services and foreground
  114. * apps, which seems like a reasonable buffer to have in place. Another way of
  115. * putting it is that at this point, we are unsure that rebalancing the
  116. * balloon won't harm the side it balances against by eating into its buffer.
  117. *
  118. * We assume that normally filePages only decreases as a result of freePages
  119. * being close to zero, when vmscan reclaiming kicks in. Based on this,
  120. * there are two cases when computing the distance.
  121. *
  122. * - filePages >= emptyAppPages:
  123. * freePages + filePages - emptyAppPages
  124. * - filePages < emptyAppPages:
  125. * freePages - emptyAppPages
  126. *
  127. * @param freePages number of free pages.
  128. * @param filePages number of pages in the page cache.
  129. * @param emptyAppPages number of free/file pages at which the
  130. * lowmemorykiller will start killing empty apps.
  131. *
  132. * @return Low memory distance measure (in pages), negative if below threshold.
  133. */
  134. static inline int32
  135. Balloon_LowMemDistance(uint32 freePages,
  136. uint32 filePages,
  137. uint32 emptyAppPages)
  138. {
  139. return filePages >= emptyAppPages ?
  140. (int32) (freePages + filePages) - (int32) emptyAppPages :
  141. (int32) freePages - (int32) emptyAppPages;
  142. }
  143. #ifdef __KERNEL__
  144. /**
  145. * @brief Obtain approximation of # anonymous pages belonging to Android
  146. * background processes.
  147. *
  148. * Used to inform balloon policy. Note that this is a coarse approximation only,
  149. * since we use RSS. More precise accounting is possible but potentially costly
  150. * as it's not available directly in the task struct.
  151. *
  152. * @param minHiddenAppOOMScoreAdj minimum oom_score_adj for hidden apps.
  153. *
  154. * @return sum of empty, content provider and hidden app anon resident pages.
  155. *
  156. * @note On Linux < 3.3.0, Android LMK was using oom_adj values, which are much
  157. * lower than oom_score_adj. If we detect a value higher than the
  158. * threshold below, we use the latter instead of the former.
  159. */
  160. static uint32
  161. Balloon_AndroidBackgroundPages(uint32 minHiddenAppOOMScoreAdj)
  162. {
  163. uint32 backgroundPages = 0, nonBackgroundPages = 0;
  164. struct task_struct *t;
  165. /*
  166. * Traverse the tasklist to replicate the behavior of the Android
  167. * low memory killer.
  168. */
  169. rcu_read_lock();
  170. for_each_process(t) {
  171. int oom_score_adj = 0;
  172. task_lock(t);
  173. if (t->signal == NULL) {
  174. task_unlock(t);
  175. continue;
  176. } else {
  177. /*
  178. * Use 50 as a threshold. 15 is the highest value we've
  179. * observed on Android phones, and new phones will all
  180. * use the oom_score_adj and thus have much bigger
  181. * numbers (529 for example).
  182. */
  183. oom_score_adj =
  184. minHiddenAppOOMScoreAdj > USE_OOM_SCORE_ADJ_THRESHOLD
  185. ? t->signal->oom_score_adj
  186. : t->signal->oom_adj;
  187. }
  188. if (t->mm != NULL) {
  189. #ifdef BALLOON_DEBUG_PRINT_ANDROID_PAGES
  190. pr_info("Balloon_AndroidBackgroundPages: %d %d %s\n",
  191. oom_score_adj,
  192. (int)get_mm_counter(t->mm, MM_ANONPAGES),
  193. t->comm);
  194. #endif
  195. if (oom_score_adj >= (int)minHiddenAppOOMScoreAdj)
  196. /*
  197. * Unlike the Android low memory killer, we
  198. * only consider anonymous memory here, since
  199. * we already account for file pages in the
  200. * balloon policy using
  201. * global_page_state(NR_FILE_PAGES).
  202. */
  203. backgroundPages +=
  204. get_mm_counter(t->mm, MM_ANONPAGES);
  205. else
  206. nonBackgroundPages +=
  207. get_mm_counter(t->mm, MM_ANONPAGES);
  208. }
  209. task_unlock(t);
  210. }
  211. rcu_read_unlock();
  212. #ifdef BALLOON_DEBUG_PRINT_ANDROID_PAGES
  213. pr_info("Balloon_AndroidBackgroundPages: non-background pages: %d " \
  214. "background pages: %d\n",
  215. nonBackgroundPages,
  216. backgroundPages);
  217. #endif
  218. return backgroundPages;
  219. }
  220. #endif
  221. #endif