memcontrol.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283
  1. /* memcontrol.c - Memory Controller
  2. *
  3. * Copyright IBM Corporation, 2007
  4. * Author Balbir Singh <balbir@linux.vnet.ibm.com>
  5. *
  6. * Copyright 2007 OpenVZ SWsoft Inc
  7. * Author: Pavel Emelianov <xemul@openvz.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/res_counter.h>
  20. #include <linux/memcontrol.h>
  21. #include <linux/cgroup.h>
  22. #include <linux/mm.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/smp.h>
  25. #include <linux/page-flags.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/bit_spinlock.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/mutex.h>
  30. #include <linux/slab.h>
  31. #include <linux/swap.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/fs.h>
  34. #include <linux/seq_file.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/mm_inline.h>
  37. #include <linux/page_cgroup.h>
  38. #include "internal.h"
  39. #include <asm/uaccess.h>
  40. struct cgroup_subsys mem_cgroup_subsys __read_mostly;
  41. #define MEM_CGROUP_RECLAIM_RETRIES 5
  42. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  43. /* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */
  44. int do_swap_account __read_mostly;
  45. static int really_do_swap_account __initdata = 1; /* for remember boot option*/
  46. #else
  47. #define do_swap_account (0)
  48. #endif
  49. static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
  50. /*
  51. * Statistics for memory cgroup.
  52. */
  53. enum mem_cgroup_stat_index {
  54. /*
  55. * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
  56. */
  57. MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
  58. MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
  59. MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
  60. MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
  61. MEM_CGROUP_STAT_NSTATS,
  62. };
  63. struct mem_cgroup_stat_cpu {
  64. s64 count[MEM_CGROUP_STAT_NSTATS];
  65. } ____cacheline_aligned_in_smp;
  66. struct mem_cgroup_stat {
  67. struct mem_cgroup_stat_cpu cpustat[0];
  68. };
  69. /*
  70. * For accounting under irq disable, no need for increment preempt count.
  71. */
  72. static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
  73. enum mem_cgroup_stat_index idx, int val)
  74. {
  75. stat->count[idx] += val;
  76. }
  77. static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
  78. enum mem_cgroup_stat_index idx)
  79. {
  80. int cpu;
  81. s64 ret = 0;
  82. for_each_possible_cpu(cpu)
  83. ret += stat->cpustat[cpu].count[idx];
  84. return ret;
  85. }
  86. /*
  87. * per-zone information in memory controller.
  88. */
  89. struct mem_cgroup_per_zone {
  90. /*
  91. * spin_lock to protect the per cgroup LRU
  92. */
  93. struct list_head lists[NR_LRU_LISTS];
  94. unsigned long count[NR_LRU_LISTS];
  95. struct zone_reclaim_stat reclaim_stat;
  96. };
  97. /* Macro for accessing counter */
  98. #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
  99. struct mem_cgroup_per_node {
  100. struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
  101. };
  102. struct mem_cgroup_lru_info {
  103. struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
  104. };
  105. /*
  106. * The memory controller data structure. The memory controller controls both
  107. * page cache and RSS per cgroup. We would eventually like to provide
  108. * statistics based on the statistics developed by Rik Van Riel for clock-pro,
  109. * to help the administrator determine what knobs to tune.
  110. *
  111. * TODO: Add a water mark for the memory controller. Reclaim will begin when
  112. * we hit the water mark. May be even add a low water mark, such that
  113. * no reclaim occurs from a cgroup at it's low water mark, this is
  114. * a feature that will be implemented much later in the future.
  115. */
  116. struct mem_cgroup {
  117. struct cgroup_subsys_state css;
  118. /*
  119. * the counter to account for memory usage
  120. */
  121. struct res_counter res;
  122. /*
  123. * the counter to account for mem+swap usage.
  124. */
  125. struct res_counter memsw;
  126. /*
  127. * Per cgroup active and inactive list, similar to the
  128. * per zone LRU lists.
  129. */
  130. struct mem_cgroup_lru_info info;
  131. /*
  132. protect against reclaim related member.
  133. */
  134. spinlock_t reclaim_param_lock;
  135. int prev_priority; /* for recording reclaim priority */
  136. /*
  137. * While reclaiming in a hiearchy, we cache the last child we
  138. * reclaimed from. Protected by hierarchy_mutex
  139. */
  140. struct mem_cgroup *last_scanned_child;
  141. /*
  142. * Should the accounting and control be hierarchical, per subtree?
  143. */
  144. bool use_hierarchy;
  145. unsigned long last_oom_jiffies;
  146. atomic_t refcnt;
  147. unsigned int swappiness;
  148. /*
  149. * statistics. This must be placed at the end of memcg.
  150. */
  151. struct mem_cgroup_stat stat;
  152. };
  153. enum charge_type {
  154. MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
  155. MEM_CGROUP_CHARGE_TYPE_MAPPED,
  156. MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
  157. MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
  158. MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
  159. NR_CHARGE_TYPE,
  160. };
  161. /* only for here (for easy reading.) */
  162. #define PCGF_CACHE (1UL << PCG_CACHE)
  163. #define PCGF_USED (1UL << PCG_USED)
  164. #define PCGF_LOCK (1UL << PCG_LOCK)
  165. static const unsigned long
  166. pcg_default_flags[NR_CHARGE_TYPE] = {
  167. PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */
  168. PCGF_USED | PCGF_LOCK, /* Anon */
  169. PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
  170. 0, /* FORCE */
  171. };
  172. /* for encoding cft->private value on file */
  173. #define _MEM (0)
  174. #define _MEMSWAP (1)
  175. #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
  176. #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
  177. #define MEMFILE_ATTR(val) ((val) & 0xffff)
  178. static void mem_cgroup_get(struct mem_cgroup *mem);
  179. static void mem_cgroup_put(struct mem_cgroup *mem);
  180. static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
  181. struct page_cgroup *pc,
  182. bool charge)
  183. {
  184. int val = (charge)? 1 : -1;
  185. struct mem_cgroup_stat *stat = &mem->stat;
  186. struct mem_cgroup_stat_cpu *cpustat;
  187. int cpu = get_cpu();
  188. cpustat = &stat->cpustat[cpu];
  189. if (PageCgroupCache(pc))
  190. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
  191. else
  192. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
  193. if (charge)
  194. __mem_cgroup_stat_add_safe(cpustat,
  195. MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
  196. else
  197. __mem_cgroup_stat_add_safe(cpustat,
  198. MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
  199. put_cpu();
  200. }
  201. static struct mem_cgroup_per_zone *
  202. mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
  203. {
  204. return &mem->info.nodeinfo[nid]->zoneinfo[zid];
  205. }
  206. static struct mem_cgroup_per_zone *
  207. page_cgroup_zoneinfo(struct page_cgroup *pc)
  208. {
  209. struct mem_cgroup *mem = pc->mem_cgroup;
  210. int nid = page_cgroup_nid(pc);
  211. int zid = page_cgroup_zid(pc);
  212. if (!mem)
  213. return NULL;
  214. return mem_cgroup_zoneinfo(mem, nid, zid);
  215. }
  216. static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
  217. enum lru_list idx)
  218. {
  219. int nid, zid;
  220. struct mem_cgroup_per_zone *mz;
  221. u64 total = 0;
  222. for_each_online_node(nid)
  223. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  224. mz = mem_cgroup_zoneinfo(mem, nid, zid);
  225. total += MEM_CGROUP_ZSTAT(mz, idx);
  226. }
  227. return total;
  228. }
  229. static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
  230. {
  231. return container_of(cgroup_subsys_state(cont,
  232. mem_cgroup_subsys_id), struct mem_cgroup,
  233. css);
  234. }
  235. struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
  236. {
  237. /*
  238. * mm_update_next_owner() may clear mm->owner to NULL
  239. * if it races with swapoff, page migration, etc.
  240. * So this can be called with p == NULL.
  241. */
  242. if (unlikely(!p))
  243. return NULL;
  244. return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
  245. struct mem_cgroup, css);
  246. }
  247. static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
  248. {
  249. struct mem_cgroup *mem = NULL;
  250. /*
  251. * Because we have no locks, mm->owner's may be being moved to other
  252. * cgroup. We use css_tryget() here even if this looks
  253. * pessimistic (rather than adding locks here).
  254. */
  255. rcu_read_lock();
  256. do {
  257. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  258. if (unlikely(!mem))
  259. break;
  260. } while (!css_tryget(&mem->css));
  261. rcu_read_unlock();
  262. return mem;
  263. }
  264. static bool mem_cgroup_is_obsolete(struct mem_cgroup *mem)
  265. {
  266. if (!mem)
  267. return true;
  268. return css_is_removed(&mem->css);
  269. }
  270. /*
  271. * Following LRU functions are allowed to be used without PCG_LOCK.
  272. * Operations are called by routine of global LRU independently from memcg.
  273. * What we have to take care of here is validness of pc->mem_cgroup.
  274. *
  275. * Changes to pc->mem_cgroup happens when
  276. * 1. charge
  277. * 2. moving account
  278. * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
  279. * It is added to LRU before charge.
  280. * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
  281. * When moving account, the page is not on LRU. It's isolated.
  282. */
  283. void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
  284. {
  285. struct page_cgroup *pc;
  286. struct mem_cgroup *mem;
  287. struct mem_cgroup_per_zone *mz;
  288. if (mem_cgroup_disabled())
  289. return;
  290. pc = lookup_page_cgroup(page);
  291. /* can happen while we handle swapcache. */
  292. if (list_empty(&pc->lru) || !pc->mem_cgroup)
  293. return;
  294. /*
  295. * We don't check PCG_USED bit. It's cleared when the "page" is finally
  296. * removed from global LRU.
  297. */
  298. mz = page_cgroup_zoneinfo(pc);
  299. mem = pc->mem_cgroup;
  300. MEM_CGROUP_ZSTAT(mz, lru) -= 1;
  301. list_del_init(&pc->lru);
  302. return;
  303. }
  304. void mem_cgroup_del_lru(struct page *page)
  305. {
  306. mem_cgroup_del_lru_list(page, page_lru(page));
  307. }
  308. void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
  309. {
  310. struct mem_cgroup_per_zone *mz;
  311. struct page_cgroup *pc;
  312. if (mem_cgroup_disabled())
  313. return;
  314. pc = lookup_page_cgroup(page);
  315. smp_rmb();
  316. /* unused page is not rotated. */
  317. if (!PageCgroupUsed(pc))
  318. return;
  319. mz = page_cgroup_zoneinfo(pc);
  320. list_move(&pc->lru, &mz->lists[lru]);
  321. }
  322. void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
  323. {
  324. struct page_cgroup *pc;
  325. struct mem_cgroup_per_zone *mz;
  326. if (mem_cgroup_disabled())
  327. return;
  328. pc = lookup_page_cgroup(page);
  329. /* barrier to sync with "charge" */
  330. smp_rmb();
  331. if (!PageCgroupUsed(pc))
  332. return;
  333. mz = page_cgroup_zoneinfo(pc);
  334. MEM_CGROUP_ZSTAT(mz, lru) += 1;
  335. list_add(&pc->lru, &mz->lists[lru]);
  336. }
  337. /*
  338. * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
  339. * lru because the page may.be reused after it's fully uncharged (because of
  340. * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
  341. * it again. This function is only used to charge SwapCache. It's done under
  342. * lock_page and expected that zone->lru_lock is never held.
  343. */
  344. static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
  345. {
  346. unsigned long flags;
  347. struct zone *zone = page_zone(page);
  348. struct page_cgroup *pc = lookup_page_cgroup(page);
  349. spin_lock_irqsave(&zone->lru_lock, flags);
  350. /*
  351. * Forget old LRU when this page_cgroup is *not* used. This Used bit
  352. * is guarded by lock_page() because the page is SwapCache.
  353. */
  354. if (!PageCgroupUsed(pc))
  355. mem_cgroup_del_lru_list(page, page_lru(page));
  356. spin_unlock_irqrestore(&zone->lru_lock, flags);
  357. }
  358. static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
  359. {
  360. unsigned long flags;
  361. struct zone *zone = page_zone(page);
  362. struct page_cgroup *pc = lookup_page_cgroup(page);
  363. spin_lock_irqsave(&zone->lru_lock, flags);
  364. /* link when the page is linked to LRU but page_cgroup isn't */
  365. if (PageLRU(page) && list_empty(&pc->lru))
  366. mem_cgroup_add_lru_list(page, page_lru(page));
  367. spin_unlock_irqrestore(&zone->lru_lock, flags);
  368. }
  369. void mem_cgroup_move_lists(struct page *page,
  370. enum lru_list from, enum lru_list to)
  371. {
  372. if (mem_cgroup_disabled())
  373. return;
  374. mem_cgroup_del_lru_list(page, from);
  375. mem_cgroup_add_lru_list(page, to);
  376. }
  377. int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
  378. {
  379. int ret;
  380. task_lock(task);
  381. ret = task->mm && mm_match_cgroup(task->mm, mem);
  382. task_unlock(task);
  383. return ret;
  384. }
  385. /*
  386. * Calculate mapped_ratio under memory controller. This will be used in
  387. * vmscan.c for deteremining we have to reclaim mapped pages.
  388. */
  389. int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
  390. {
  391. long total, rss;
  392. /*
  393. * usage is recorded in bytes. But, here, we assume the number of
  394. * physical pages can be represented by "long" on any arch.
  395. */
  396. total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
  397. rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
  398. return (int)((rss * 100L) / total);
  399. }
  400. /*
  401. * prev_priority control...this will be used in memory reclaim path.
  402. */
  403. int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
  404. {
  405. int prev_priority;
  406. spin_lock(&mem->reclaim_param_lock);
  407. prev_priority = mem->prev_priority;
  408. spin_unlock(&mem->reclaim_param_lock);
  409. return prev_priority;
  410. }
  411. void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
  412. {
  413. spin_lock(&mem->reclaim_param_lock);
  414. if (priority < mem->prev_priority)
  415. mem->prev_priority = priority;
  416. spin_unlock(&mem->reclaim_param_lock);
  417. }
  418. void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
  419. {
  420. spin_lock(&mem->reclaim_param_lock);
  421. mem->prev_priority = priority;
  422. spin_unlock(&mem->reclaim_param_lock);
  423. }
  424. static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
  425. {
  426. unsigned long active;
  427. unsigned long inactive;
  428. unsigned long gb;
  429. unsigned long inactive_ratio;
  430. inactive = mem_cgroup_get_all_zonestat(memcg, LRU_INACTIVE_ANON);
  431. active = mem_cgroup_get_all_zonestat(memcg, LRU_ACTIVE_ANON);
  432. gb = (inactive + active) >> (30 - PAGE_SHIFT);
  433. if (gb)
  434. inactive_ratio = int_sqrt(10 * gb);
  435. else
  436. inactive_ratio = 1;
  437. if (present_pages) {
  438. present_pages[0] = inactive;
  439. present_pages[1] = active;
  440. }
  441. return inactive_ratio;
  442. }
  443. int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
  444. {
  445. unsigned long active;
  446. unsigned long inactive;
  447. unsigned long present_pages[2];
  448. unsigned long inactive_ratio;
  449. inactive_ratio = calc_inactive_ratio(memcg, present_pages);
  450. inactive = present_pages[0];
  451. active = present_pages[1];
  452. if (inactive * inactive_ratio < active)
  453. return 1;
  454. return 0;
  455. }
  456. unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
  457. struct zone *zone,
  458. enum lru_list lru)
  459. {
  460. int nid = zone->zone_pgdat->node_id;
  461. int zid = zone_idx(zone);
  462. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
  463. return MEM_CGROUP_ZSTAT(mz, lru);
  464. }
  465. struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
  466. struct zone *zone)
  467. {
  468. int nid = zone->zone_pgdat->node_id;
  469. int zid = zone_idx(zone);
  470. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
  471. return &mz->reclaim_stat;
  472. }
  473. struct zone_reclaim_stat *
  474. mem_cgroup_get_reclaim_stat_from_page(struct page *page)
  475. {
  476. struct page_cgroup *pc;
  477. struct mem_cgroup_per_zone *mz;
  478. if (mem_cgroup_disabled())
  479. return NULL;
  480. pc = lookup_page_cgroup(page);
  481. mz = page_cgroup_zoneinfo(pc);
  482. if (!mz)
  483. return NULL;
  484. return &mz->reclaim_stat;
  485. }
  486. unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
  487. struct list_head *dst,
  488. unsigned long *scanned, int order,
  489. int mode, struct zone *z,
  490. struct mem_cgroup *mem_cont,
  491. int active, int file)
  492. {
  493. unsigned long nr_taken = 0;
  494. struct page *page;
  495. unsigned long scan;
  496. LIST_HEAD(pc_list);
  497. struct list_head *src;
  498. struct page_cgroup *pc, *tmp;
  499. int nid = z->zone_pgdat->node_id;
  500. int zid = zone_idx(z);
  501. struct mem_cgroup_per_zone *mz;
  502. int lru = LRU_FILE * !!file + !!active;
  503. BUG_ON(!mem_cont);
  504. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  505. src = &mz->lists[lru];
  506. scan = 0;
  507. list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
  508. if (scan >= nr_to_scan)
  509. break;
  510. page = pc->page;
  511. if (unlikely(!PageCgroupUsed(pc)))
  512. continue;
  513. if (unlikely(!PageLRU(page)))
  514. continue;
  515. scan++;
  516. if (__isolate_lru_page(page, mode, file) == 0) {
  517. list_move(&page->lru, dst);
  518. nr_taken++;
  519. }
  520. }
  521. *scanned = scan;
  522. return nr_taken;
  523. }
  524. #define mem_cgroup_from_res_counter(counter, member) \
  525. container_of(counter, struct mem_cgroup, member)
  526. /*
  527. * This routine finds the DFS walk successor. This routine should be
  528. * called with hierarchy_mutex held
  529. */
  530. static struct mem_cgroup *
  531. mem_cgroup_get_next_node(struct mem_cgroup *curr, struct mem_cgroup *root_mem)
  532. {
  533. struct cgroup *cgroup, *curr_cgroup, *root_cgroup;
  534. curr_cgroup = curr->css.cgroup;
  535. root_cgroup = root_mem->css.cgroup;
  536. if (!list_empty(&curr_cgroup->children)) {
  537. /*
  538. * Walk down to children
  539. */
  540. mem_cgroup_put(curr);
  541. cgroup = list_entry(curr_cgroup->children.next,
  542. struct cgroup, sibling);
  543. curr = mem_cgroup_from_cont(cgroup);
  544. mem_cgroup_get(curr);
  545. goto done;
  546. }
  547. visit_parent:
  548. if (curr_cgroup == root_cgroup) {
  549. mem_cgroup_put(curr);
  550. curr = root_mem;
  551. mem_cgroup_get(curr);
  552. goto done;
  553. }
  554. /*
  555. * Goto next sibling
  556. */
  557. if (curr_cgroup->sibling.next != &curr_cgroup->parent->children) {
  558. mem_cgroup_put(curr);
  559. cgroup = list_entry(curr_cgroup->sibling.next, struct cgroup,
  560. sibling);
  561. curr = mem_cgroup_from_cont(cgroup);
  562. mem_cgroup_get(curr);
  563. goto done;
  564. }
  565. /*
  566. * Go up to next parent and next parent's sibling if need be
  567. */
  568. curr_cgroup = curr_cgroup->parent;
  569. goto visit_parent;
  570. done:
  571. root_mem->last_scanned_child = curr;
  572. return curr;
  573. }
  574. /*
  575. * Visit the first child (need not be the first child as per the ordering
  576. * of the cgroup list, since we track last_scanned_child) of @mem and use
  577. * that to reclaim free pages from.
  578. */
  579. static struct mem_cgroup *
  580. mem_cgroup_get_first_node(struct mem_cgroup *root_mem)
  581. {
  582. struct cgroup *cgroup;
  583. struct mem_cgroup *ret;
  584. bool obsolete;
  585. obsolete = mem_cgroup_is_obsolete(root_mem->last_scanned_child);
  586. /*
  587. * Scan all children under the mem_cgroup mem
  588. */
  589. mutex_lock(&mem_cgroup_subsys.hierarchy_mutex);
  590. if (list_empty(&root_mem->css.cgroup->children)) {
  591. ret = root_mem;
  592. goto done;
  593. }
  594. if (!root_mem->last_scanned_child || obsolete) {
  595. if (obsolete && root_mem->last_scanned_child)
  596. mem_cgroup_put(root_mem->last_scanned_child);
  597. cgroup = list_first_entry(&root_mem->css.cgroup->children,
  598. struct cgroup, sibling);
  599. ret = mem_cgroup_from_cont(cgroup);
  600. mem_cgroup_get(ret);
  601. } else
  602. ret = mem_cgroup_get_next_node(root_mem->last_scanned_child,
  603. root_mem);
  604. done:
  605. root_mem->last_scanned_child = ret;
  606. mutex_unlock(&mem_cgroup_subsys.hierarchy_mutex);
  607. return ret;
  608. }
  609. static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
  610. {
  611. if (do_swap_account) {
  612. if (res_counter_check_under_limit(&mem->res) &&
  613. res_counter_check_under_limit(&mem->memsw))
  614. return true;
  615. } else
  616. if (res_counter_check_under_limit(&mem->res))
  617. return true;
  618. return false;
  619. }
  620. static unsigned int get_swappiness(struct mem_cgroup *memcg)
  621. {
  622. struct cgroup *cgrp = memcg->css.cgroup;
  623. unsigned int swappiness;
  624. /* root ? */
  625. if (cgrp->parent == NULL)
  626. return vm_swappiness;
  627. spin_lock(&memcg->reclaim_param_lock);
  628. swappiness = memcg->swappiness;
  629. spin_unlock(&memcg->reclaim_param_lock);
  630. return swappiness;
  631. }
  632. /*
  633. * Dance down the hierarchy if needed to reclaim memory. We remember the
  634. * last child we reclaimed from, so that we don't end up penalizing
  635. * one child extensively based on its position in the children list.
  636. *
  637. * root_mem is the original ancestor that we've been reclaim from.
  638. */
  639. static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
  640. gfp_t gfp_mask, bool noswap)
  641. {
  642. struct mem_cgroup *next_mem;
  643. int ret = 0;
  644. /*
  645. * Reclaim unconditionally and don't check for return value.
  646. * We need to reclaim in the current group and down the tree.
  647. * One might think about checking for children before reclaiming,
  648. * but there might be left over accounting, even after children
  649. * have left.
  650. */
  651. ret = try_to_free_mem_cgroup_pages(root_mem, gfp_mask, noswap,
  652. get_swappiness(root_mem));
  653. if (mem_cgroup_check_under_limit(root_mem))
  654. return 0;
  655. if (!root_mem->use_hierarchy)
  656. return ret;
  657. next_mem = mem_cgroup_get_first_node(root_mem);
  658. while (next_mem != root_mem) {
  659. if (mem_cgroup_is_obsolete(next_mem)) {
  660. mem_cgroup_put(next_mem);
  661. next_mem = mem_cgroup_get_first_node(root_mem);
  662. continue;
  663. }
  664. ret = try_to_free_mem_cgroup_pages(next_mem, gfp_mask, noswap,
  665. get_swappiness(next_mem));
  666. if (mem_cgroup_check_under_limit(root_mem))
  667. return 0;
  668. mutex_lock(&mem_cgroup_subsys.hierarchy_mutex);
  669. next_mem = mem_cgroup_get_next_node(next_mem, root_mem);
  670. mutex_unlock(&mem_cgroup_subsys.hierarchy_mutex);
  671. }
  672. return ret;
  673. }
  674. bool mem_cgroup_oom_called(struct task_struct *task)
  675. {
  676. bool ret = false;
  677. struct mem_cgroup *mem;
  678. struct mm_struct *mm;
  679. rcu_read_lock();
  680. mm = task->mm;
  681. if (!mm)
  682. mm = &init_mm;
  683. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  684. if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
  685. ret = true;
  686. rcu_read_unlock();
  687. return ret;
  688. }
  689. /*
  690. * Unlike exported interface, "oom" parameter is added. if oom==true,
  691. * oom-killer can be invoked.
  692. */
  693. static int __mem_cgroup_try_charge(struct mm_struct *mm,
  694. gfp_t gfp_mask, struct mem_cgroup **memcg,
  695. bool oom)
  696. {
  697. struct mem_cgroup *mem, *mem_over_limit;
  698. int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  699. struct res_counter *fail_res;
  700. if (unlikely(test_thread_flag(TIF_MEMDIE))) {
  701. /* Don't account this! */
  702. *memcg = NULL;
  703. return 0;
  704. }
  705. /*
  706. * We always charge the cgroup the mm_struct belongs to.
  707. * The mm_struct's mem_cgroup changes on task migration if the
  708. * thread group leader migrates. It's possible that mm is not
  709. * set, if so charge the init_mm (happens for pagecache usage).
  710. */
  711. mem = *memcg;
  712. if (likely(!mem)) {
  713. mem = try_get_mem_cgroup_from_mm(mm);
  714. *memcg = mem;
  715. } else {
  716. css_get(&mem->css);
  717. }
  718. if (unlikely(!mem))
  719. return 0;
  720. VM_BUG_ON(mem_cgroup_is_obsolete(mem));
  721. while (1) {
  722. int ret;
  723. bool noswap = false;
  724. ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
  725. if (likely(!ret)) {
  726. if (!do_swap_account)
  727. break;
  728. ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
  729. &fail_res);
  730. if (likely(!ret))
  731. break;
  732. /* mem+swap counter fails */
  733. res_counter_uncharge(&mem->res, PAGE_SIZE);
  734. noswap = true;
  735. mem_over_limit = mem_cgroup_from_res_counter(fail_res,
  736. memsw);
  737. } else
  738. /* mem counter fails */
  739. mem_over_limit = mem_cgroup_from_res_counter(fail_res,
  740. res);
  741. if (!(gfp_mask & __GFP_WAIT))
  742. goto nomem;
  743. ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
  744. noswap);
  745. /*
  746. * try_to_free_mem_cgroup_pages() might not give us a full
  747. * picture of reclaim. Some pages are reclaimed and might be
  748. * moved to swap cache or just unmapped from the cgroup.
  749. * Check the limit again to see if the reclaim reduced the
  750. * current usage of the cgroup before giving up
  751. *
  752. */
  753. if (mem_cgroup_check_under_limit(mem_over_limit))
  754. continue;
  755. if (!nr_retries--) {
  756. if (oom) {
  757. mutex_lock(&memcg_tasklist);
  758. mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
  759. mutex_unlock(&memcg_tasklist);
  760. mem_over_limit->last_oom_jiffies = jiffies;
  761. }
  762. goto nomem;
  763. }
  764. }
  765. return 0;
  766. nomem:
  767. css_put(&mem->css);
  768. return -ENOMEM;
  769. }
  770. static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
  771. {
  772. struct mem_cgroup *mem;
  773. swp_entry_t ent;
  774. if (!PageSwapCache(page))
  775. return NULL;
  776. ent.val = page_private(page);
  777. mem = lookup_swap_cgroup(ent);
  778. if (!mem)
  779. return NULL;
  780. if (!css_tryget(&mem->css))
  781. return NULL;
  782. return mem;
  783. }
  784. /*
  785. * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
  786. * USED state. If already USED, uncharge and return.
  787. */
  788. static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
  789. struct page_cgroup *pc,
  790. enum charge_type ctype)
  791. {
  792. /* try_charge() can return NULL to *memcg, taking care of it. */
  793. if (!mem)
  794. return;
  795. lock_page_cgroup(pc);
  796. if (unlikely(PageCgroupUsed(pc))) {
  797. unlock_page_cgroup(pc);
  798. res_counter_uncharge(&mem->res, PAGE_SIZE);
  799. if (do_swap_account)
  800. res_counter_uncharge(&mem->memsw, PAGE_SIZE);
  801. css_put(&mem->css);
  802. return;
  803. }
  804. pc->mem_cgroup = mem;
  805. smp_wmb();
  806. pc->flags = pcg_default_flags[ctype];
  807. mem_cgroup_charge_statistics(mem, pc, true);
  808. unlock_page_cgroup(pc);
  809. }
  810. /**
  811. * mem_cgroup_move_account - move account of the page
  812. * @pc: page_cgroup of the page.
  813. * @from: mem_cgroup which the page is moved from.
  814. * @to: mem_cgroup which the page is moved to. @from != @to.
  815. *
  816. * The caller must confirm following.
  817. * - page is not on LRU (isolate_page() is useful.)
  818. *
  819. * returns 0 at success,
  820. * returns -EBUSY when lock is busy or "pc" is unstable.
  821. *
  822. * This function does "uncharge" from old cgroup but doesn't do "charge" to
  823. * new cgroup. It should be done by a caller.
  824. */
  825. static int mem_cgroup_move_account(struct page_cgroup *pc,
  826. struct mem_cgroup *from, struct mem_cgroup *to)
  827. {
  828. struct mem_cgroup_per_zone *from_mz, *to_mz;
  829. int nid, zid;
  830. int ret = -EBUSY;
  831. VM_BUG_ON(from == to);
  832. VM_BUG_ON(PageLRU(pc->page));
  833. nid = page_cgroup_nid(pc);
  834. zid = page_cgroup_zid(pc);
  835. from_mz = mem_cgroup_zoneinfo(from, nid, zid);
  836. to_mz = mem_cgroup_zoneinfo(to, nid, zid);
  837. if (!trylock_page_cgroup(pc))
  838. return ret;
  839. if (!PageCgroupUsed(pc))
  840. goto out;
  841. if (pc->mem_cgroup != from)
  842. goto out;
  843. css_put(&from->css);
  844. res_counter_uncharge(&from->res, PAGE_SIZE);
  845. mem_cgroup_charge_statistics(from, pc, false);
  846. if (do_swap_account)
  847. res_counter_uncharge(&from->memsw, PAGE_SIZE);
  848. pc->mem_cgroup = to;
  849. mem_cgroup_charge_statistics(to, pc, true);
  850. css_get(&to->css);
  851. ret = 0;
  852. out:
  853. unlock_page_cgroup(pc);
  854. return ret;
  855. }
  856. /*
  857. * move charges to its parent.
  858. */
  859. static int mem_cgroup_move_parent(struct page_cgroup *pc,
  860. struct mem_cgroup *child,
  861. gfp_t gfp_mask)
  862. {
  863. struct page *page = pc->page;
  864. struct cgroup *cg = child->css.cgroup;
  865. struct cgroup *pcg = cg->parent;
  866. struct mem_cgroup *parent;
  867. int ret;
  868. /* Is ROOT ? */
  869. if (!pcg)
  870. return -EINVAL;
  871. parent = mem_cgroup_from_cont(pcg);
  872. ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
  873. if (ret || !parent)
  874. return ret;
  875. if (!get_page_unless_zero(page))
  876. return -EBUSY;
  877. ret = isolate_lru_page(page);
  878. if (ret)
  879. goto cancel;
  880. ret = mem_cgroup_move_account(pc, child, parent);
  881. /* drop extra refcnt by try_charge() (move_account increment one) */
  882. css_put(&parent->css);
  883. putback_lru_page(page);
  884. if (!ret) {
  885. put_page(page);
  886. return 0;
  887. }
  888. /* uncharge if move fails */
  889. cancel:
  890. res_counter_uncharge(&parent->res, PAGE_SIZE);
  891. if (do_swap_account)
  892. res_counter_uncharge(&parent->memsw, PAGE_SIZE);
  893. put_page(page);
  894. return ret;
  895. }
  896. /*
  897. * Charge the memory controller for page usage.
  898. * Return
  899. * 0 if the charge was successful
  900. * < 0 if the cgroup is over its limit
  901. */
  902. static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
  903. gfp_t gfp_mask, enum charge_type ctype,
  904. struct mem_cgroup *memcg)
  905. {
  906. struct mem_cgroup *mem;
  907. struct page_cgroup *pc;
  908. int ret;
  909. pc = lookup_page_cgroup(page);
  910. /* can happen at boot */
  911. if (unlikely(!pc))
  912. return 0;
  913. prefetchw(pc);
  914. mem = memcg;
  915. ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
  916. if (ret || !mem)
  917. return ret;
  918. __mem_cgroup_commit_charge(mem, pc, ctype);
  919. return 0;
  920. }
  921. int mem_cgroup_newpage_charge(struct page *page,
  922. struct mm_struct *mm, gfp_t gfp_mask)
  923. {
  924. if (mem_cgroup_disabled())
  925. return 0;
  926. if (PageCompound(page))
  927. return 0;
  928. /*
  929. * If already mapped, we don't have to account.
  930. * If page cache, page->mapping has address_space.
  931. * But page->mapping may have out-of-use anon_vma pointer,
  932. * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
  933. * is NULL.
  934. */
  935. if (page_mapped(page) || (page->mapping && !PageAnon(page)))
  936. return 0;
  937. if (unlikely(!mm))
  938. mm = &init_mm;
  939. return mem_cgroup_charge_common(page, mm, gfp_mask,
  940. MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
  941. }
  942. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  943. gfp_t gfp_mask)
  944. {
  945. struct mem_cgroup *mem = NULL;
  946. int ret;
  947. if (mem_cgroup_disabled())
  948. return 0;
  949. if (PageCompound(page))
  950. return 0;
  951. /*
  952. * Corner case handling. This is called from add_to_page_cache()
  953. * in usual. But some FS (shmem) precharges this page before calling it
  954. * and call add_to_page_cache() with GFP_NOWAIT.
  955. *
  956. * For GFP_NOWAIT case, the page may be pre-charged before calling
  957. * add_to_page_cache(). (See shmem.c) check it here and avoid to call
  958. * charge twice. (It works but has to pay a bit larger cost.)
  959. * And when the page is SwapCache, it should take swap information
  960. * into account. This is under lock_page() now.
  961. */
  962. if (!(gfp_mask & __GFP_WAIT)) {
  963. struct page_cgroup *pc;
  964. pc = lookup_page_cgroup(page);
  965. if (!pc)
  966. return 0;
  967. lock_page_cgroup(pc);
  968. if (PageCgroupUsed(pc)) {
  969. unlock_page_cgroup(pc);
  970. return 0;
  971. }
  972. unlock_page_cgroup(pc);
  973. }
  974. if (do_swap_account && PageSwapCache(page)) {
  975. mem = try_get_mem_cgroup_from_swapcache(page);
  976. if (mem)
  977. mm = NULL;
  978. else
  979. mem = NULL;
  980. /* SwapCache may be still linked to LRU now. */
  981. mem_cgroup_lru_del_before_commit_swapcache(page);
  982. }
  983. if (unlikely(!mm && !mem))
  984. mm = &init_mm;
  985. if (page_is_file_cache(page))
  986. return mem_cgroup_charge_common(page, mm, gfp_mask,
  987. MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
  988. ret = mem_cgroup_charge_common(page, mm, gfp_mask,
  989. MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
  990. if (mem)
  991. css_put(&mem->css);
  992. if (PageSwapCache(page))
  993. mem_cgroup_lru_add_after_commit_swapcache(page);
  994. if (do_swap_account && !ret && PageSwapCache(page)) {
  995. swp_entry_t ent = {.val = page_private(page)};
  996. /* avoid double counting */
  997. mem = swap_cgroup_record(ent, NULL);
  998. if (mem) {
  999. res_counter_uncharge(&mem->memsw, PAGE_SIZE);
  1000. mem_cgroup_put(mem);
  1001. }
  1002. }
  1003. return ret;
  1004. }
  1005. /*
  1006. * While swap-in, try_charge -> commit or cancel, the page is locked.
  1007. * And when try_charge() successfully returns, one refcnt to memcg without
  1008. * struct page_cgroup is aquired. This refcnt will be cumsumed by
  1009. * "commit()" or removed by "cancel()"
  1010. */
  1011. int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
  1012. struct page *page,
  1013. gfp_t mask, struct mem_cgroup **ptr)
  1014. {
  1015. struct mem_cgroup *mem;
  1016. int ret;
  1017. if (mem_cgroup_disabled())
  1018. return 0;
  1019. if (!do_swap_account)
  1020. goto charge_cur_mm;
  1021. /*
  1022. * A racing thread's fault, or swapoff, may have already updated
  1023. * the pte, and even removed page from swap cache: return success
  1024. * to go on to do_swap_page()'s pte_same() test, which should fail.
  1025. */
  1026. if (!PageSwapCache(page))
  1027. return 0;
  1028. mem = try_get_mem_cgroup_from_swapcache(page);
  1029. if (!mem)
  1030. goto charge_cur_mm;
  1031. *ptr = mem;
  1032. ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
  1033. /* drop extra refcnt from tryget */
  1034. css_put(&mem->css);
  1035. return ret;
  1036. charge_cur_mm:
  1037. if (unlikely(!mm))
  1038. mm = &init_mm;
  1039. return __mem_cgroup_try_charge(mm, mask, ptr, true);
  1040. }
  1041. void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
  1042. {
  1043. struct page_cgroup *pc;
  1044. if (mem_cgroup_disabled())
  1045. return;
  1046. if (!ptr)
  1047. return;
  1048. pc = lookup_page_cgroup(page);
  1049. mem_cgroup_lru_del_before_commit_swapcache(page);
  1050. __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
  1051. mem_cgroup_lru_add_after_commit_swapcache(page);
  1052. /*
  1053. * Now swap is on-memory. This means this page may be
  1054. * counted both as mem and swap....double count.
  1055. * Fix it by uncharging from memsw. Basically, this SwapCache is stable
  1056. * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
  1057. * may call delete_from_swap_cache() before reach here.
  1058. */
  1059. if (do_swap_account && PageSwapCache(page)) {
  1060. swp_entry_t ent = {.val = page_private(page)};
  1061. struct mem_cgroup *memcg;
  1062. memcg = swap_cgroup_record(ent, NULL);
  1063. if (memcg) {
  1064. res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
  1065. mem_cgroup_put(memcg);
  1066. }
  1067. }
  1068. /* add this page(page_cgroup) to the LRU we want. */
  1069. }
  1070. void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
  1071. {
  1072. if (mem_cgroup_disabled())
  1073. return;
  1074. if (!mem)
  1075. return;
  1076. res_counter_uncharge(&mem->res, PAGE_SIZE);
  1077. if (do_swap_account)
  1078. res_counter_uncharge(&mem->memsw, PAGE_SIZE);
  1079. css_put(&mem->css);
  1080. }
  1081. /*
  1082. * uncharge if !page_mapped(page)
  1083. */
  1084. static struct mem_cgroup *
  1085. __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
  1086. {
  1087. struct page_cgroup *pc;
  1088. struct mem_cgroup *mem = NULL;
  1089. struct mem_cgroup_per_zone *mz;
  1090. if (mem_cgroup_disabled())
  1091. return NULL;
  1092. if (PageSwapCache(page))
  1093. return NULL;
  1094. /*
  1095. * Check if our page_cgroup is valid
  1096. */
  1097. pc = lookup_page_cgroup(page);
  1098. if (unlikely(!pc || !PageCgroupUsed(pc)))
  1099. return NULL;
  1100. lock_page_cgroup(pc);
  1101. mem = pc->mem_cgroup;
  1102. if (!PageCgroupUsed(pc))
  1103. goto unlock_out;
  1104. switch (ctype) {
  1105. case MEM_CGROUP_CHARGE_TYPE_MAPPED:
  1106. if (page_mapped(page))
  1107. goto unlock_out;
  1108. break;
  1109. case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
  1110. if (!PageAnon(page)) { /* Shared memory */
  1111. if (page->mapping && !page_is_file_cache(page))
  1112. goto unlock_out;
  1113. } else if (page_mapped(page)) /* Anon */
  1114. goto unlock_out;
  1115. break;
  1116. default:
  1117. break;
  1118. }
  1119. res_counter_uncharge(&mem->res, PAGE_SIZE);
  1120. if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
  1121. res_counter_uncharge(&mem->memsw, PAGE_SIZE);
  1122. mem_cgroup_charge_statistics(mem, pc, false);
  1123. ClearPageCgroupUsed(pc);
  1124. /*
  1125. * pc->mem_cgroup is not cleared here. It will be accessed when it's
  1126. * freed from LRU. This is safe because uncharged page is expected not
  1127. * to be reused (freed soon). Exception is SwapCache, it's handled by
  1128. * special functions.
  1129. */
  1130. mz = page_cgroup_zoneinfo(pc);
  1131. unlock_page_cgroup(pc);
  1132. /* at swapout, this memcg will be accessed to record to swap */
  1133. if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
  1134. css_put(&mem->css);
  1135. return mem;
  1136. unlock_out:
  1137. unlock_page_cgroup(pc);
  1138. return NULL;
  1139. }
  1140. void mem_cgroup_uncharge_page(struct page *page)
  1141. {
  1142. /* early check. */
  1143. if (page_mapped(page))
  1144. return;
  1145. if (page->mapping && !PageAnon(page))
  1146. return;
  1147. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
  1148. }
  1149. void mem_cgroup_uncharge_cache_page(struct page *page)
  1150. {
  1151. VM_BUG_ON(page_mapped(page));
  1152. VM_BUG_ON(page->mapping);
  1153. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
  1154. }
  1155. /*
  1156. * called from __delete_from_swap_cache() and drop "page" account.
  1157. * memcg information is recorded to swap_cgroup of "ent"
  1158. */
  1159. void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
  1160. {
  1161. struct mem_cgroup *memcg;
  1162. memcg = __mem_cgroup_uncharge_common(page,
  1163. MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
  1164. /* record memcg information */
  1165. if (do_swap_account && memcg) {
  1166. swap_cgroup_record(ent, memcg);
  1167. mem_cgroup_get(memcg);
  1168. }
  1169. if (memcg)
  1170. css_put(&memcg->css);
  1171. }
  1172. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  1173. /*
  1174. * called from swap_entry_free(). remove record in swap_cgroup and
  1175. * uncharge "memsw" account.
  1176. */
  1177. void mem_cgroup_uncharge_swap(swp_entry_t ent)
  1178. {
  1179. struct mem_cgroup *memcg;
  1180. if (!do_swap_account)
  1181. return;
  1182. memcg = swap_cgroup_record(ent, NULL);
  1183. if (memcg) {
  1184. res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
  1185. mem_cgroup_put(memcg);
  1186. }
  1187. }
  1188. #endif
  1189. /*
  1190. * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
  1191. * page belongs to.
  1192. */
  1193. int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
  1194. {
  1195. struct page_cgroup *pc;
  1196. struct mem_cgroup *mem = NULL;
  1197. int ret = 0;
  1198. if (mem_cgroup_disabled())
  1199. return 0;
  1200. pc = lookup_page_cgroup(page);
  1201. lock_page_cgroup(pc);
  1202. if (PageCgroupUsed(pc)) {
  1203. mem = pc->mem_cgroup;
  1204. css_get(&mem->css);
  1205. }
  1206. unlock_page_cgroup(pc);
  1207. if (mem) {
  1208. ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
  1209. css_put(&mem->css);
  1210. }
  1211. *ptr = mem;
  1212. return ret;
  1213. }
  1214. /* remove redundant charge if migration failed*/
  1215. void mem_cgroup_end_migration(struct mem_cgroup *mem,
  1216. struct page *oldpage, struct page *newpage)
  1217. {
  1218. struct page *target, *unused;
  1219. struct page_cgroup *pc;
  1220. enum charge_type ctype;
  1221. if (!mem)
  1222. return;
  1223. /* at migration success, oldpage->mapping is NULL. */
  1224. if (oldpage->mapping) {
  1225. target = oldpage;
  1226. unused = NULL;
  1227. } else {
  1228. target = newpage;
  1229. unused = oldpage;
  1230. }
  1231. if (PageAnon(target))
  1232. ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
  1233. else if (page_is_file_cache(target))
  1234. ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
  1235. else
  1236. ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
  1237. /* unused page is not on radix-tree now. */
  1238. if (unused)
  1239. __mem_cgroup_uncharge_common(unused, ctype);
  1240. pc = lookup_page_cgroup(target);
  1241. /*
  1242. * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
  1243. * So, double-counting is effectively avoided.
  1244. */
  1245. __mem_cgroup_commit_charge(mem, pc, ctype);
  1246. /*
  1247. * Both of oldpage and newpage are still under lock_page().
  1248. * Then, we don't have to care about race in radix-tree.
  1249. * But we have to be careful that this page is unmapped or not.
  1250. *
  1251. * There is a case for !page_mapped(). At the start of
  1252. * migration, oldpage was mapped. But now, it's zapped.
  1253. * But we know *target* page is not freed/reused under us.
  1254. * mem_cgroup_uncharge_page() does all necessary checks.
  1255. */
  1256. if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
  1257. mem_cgroup_uncharge_page(target);
  1258. }
  1259. /*
  1260. * A call to try to shrink memory usage under specified resource controller.
  1261. * This is typically used for page reclaiming for shmem for reducing side
  1262. * effect of page allocation from shmem, which is used by some mem_cgroup.
  1263. */
  1264. int mem_cgroup_shrink_usage(struct page *page,
  1265. struct mm_struct *mm,
  1266. gfp_t gfp_mask)
  1267. {
  1268. struct mem_cgroup *mem = NULL;
  1269. int progress = 0;
  1270. int retry = MEM_CGROUP_RECLAIM_RETRIES;
  1271. if (mem_cgroup_disabled())
  1272. return 0;
  1273. if (page)
  1274. mem = try_get_mem_cgroup_from_swapcache(page);
  1275. if (!mem && mm)
  1276. mem = try_get_mem_cgroup_from_mm(mm);
  1277. if (unlikely(!mem))
  1278. return 0;
  1279. do {
  1280. progress = mem_cgroup_hierarchical_reclaim(mem, gfp_mask, true);
  1281. progress += mem_cgroup_check_under_limit(mem);
  1282. } while (!progress && --retry);
  1283. css_put(&mem->css);
  1284. if (!retry)
  1285. return -ENOMEM;
  1286. return 0;
  1287. }
  1288. static DEFINE_MUTEX(set_limit_mutex);
  1289. static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
  1290. unsigned long long val)
  1291. {
  1292. int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
  1293. int progress;
  1294. u64 memswlimit;
  1295. int ret = 0;
  1296. while (retry_count) {
  1297. if (signal_pending(current)) {
  1298. ret = -EINTR;
  1299. break;
  1300. }
  1301. /*
  1302. * Rather than hide all in some function, I do this in
  1303. * open coded manner. You see what this really does.
  1304. * We have to guarantee mem->res.limit < mem->memsw.limit.
  1305. */
  1306. mutex_lock(&set_limit_mutex);
  1307. memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  1308. if (memswlimit < val) {
  1309. ret = -EINVAL;
  1310. mutex_unlock(&set_limit_mutex);
  1311. break;
  1312. }
  1313. ret = res_counter_set_limit(&memcg->res, val);
  1314. mutex_unlock(&set_limit_mutex);
  1315. if (!ret)
  1316. break;
  1317. progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL,
  1318. false);
  1319. if (!progress) retry_count--;
  1320. }
  1321. return ret;
  1322. }
  1323. int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
  1324. unsigned long long val)
  1325. {
  1326. int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
  1327. u64 memlimit, oldusage, curusage;
  1328. int ret;
  1329. if (!do_swap_account)
  1330. return -EINVAL;
  1331. while (retry_count) {
  1332. if (signal_pending(current)) {
  1333. ret = -EINTR;
  1334. break;
  1335. }
  1336. /*
  1337. * Rather than hide all in some function, I do this in
  1338. * open coded manner. You see what this really does.
  1339. * We have to guarantee mem->res.limit < mem->memsw.limit.
  1340. */
  1341. mutex_lock(&set_limit_mutex);
  1342. memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  1343. if (memlimit > val) {
  1344. ret = -EINVAL;
  1345. mutex_unlock(&set_limit_mutex);
  1346. break;
  1347. }
  1348. ret = res_counter_set_limit(&memcg->memsw, val);
  1349. mutex_unlock(&set_limit_mutex);
  1350. if (!ret)
  1351. break;
  1352. oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
  1353. mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true);
  1354. curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
  1355. if (curusage >= oldusage)
  1356. retry_count--;
  1357. }
  1358. return ret;
  1359. }
  1360. /*
  1361. * This routine traverse page_cgroup in given list and drop them all.
  1362. * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  1363. */
  1364. static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
  1365. int node, int zid, enum lru_list lru)
  1366. {
  1367. struct zone *zone;
  1368. struct mem_cgroup_per_zone *mz;
  1369. struct page_cgroup *pc, *busy;
  1370. unsigned long flags, loop;
  1371. struct list_head *list;
  1372. int ret = 0;
  1373. zone = &NODE_DATA(node)->node_zones[zid];
  1374. mz = mem_cgroup_zoneinfo(mem, node, zid);
  1375. list = &mz->lists[lru];
  1376. loop = MEM_CGROUP_ZSTAT(mz, lru);
  1377. /* give some margin against EBUSY etc...*/
  1378. loop += 256;
  1379. busy = NULL;
  1380. while (loop--) {
  1381. ret = 0;
  1382. spin_lock_irqsave(&zone->lru_lock, flags);
  1383. if (list_empty(list)) {
  1384. spin_unlock_irqrestore(&zone->lru_lock, flags);
  1385. break;
  1386. }
  1387. pc = list_entry(list->prev, struct page_cgroup, lru);
  1388. if (busy == pc) {
  1389. list_move(&pc->lru, list);
  1390. busy = 0;
  1391. spin_unlock_irqrestore(&zone->lru_lock, flags);
  1392. continue;
  1393. }
  1394. spin_unlock_irqrestore(&zone->lru_lock, flags);
  1395. ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
  1396. if (ret == -ENOMEM)
  1397. break;
  1398. if (ret == -EBUSY || ret == -EINVAL) {
  1399. /* found lock contention or "pc" is obsolete. */
  1400. busy = pc;
  1401. cond_resched();
  1402. } else
  1403. busy = NULL;
  1404. }
  1405. if (!ret && !list_empty(list))
  1406. return -EBUSY;
  1407. return ret;
  1408. }
  1409. /*
  1410. * make mem_cgroup's charge to be 0 if there is no task.
  1411. * This enables deleting this mem_cgroup.
  1412. */
  1413. static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
  1414. {
  1415. int ret;
  1416. int node, zid, shrink;
  1417. int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  1418. struct cgroup *cgrp = mem->css.cgroup;
  1419. css_get(&mem->css);
  1420. shrink = 0;
  1421. /* should free all ? */
  1422. if (free_all)
  1423. goto try_to_free;
  1424. move_account:
  1425. while (mem->res.usage > 0) {
  1426. ret = -EBUSY;
  1427. if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
  1428. goto out;
  1429. ret = -EINTR;
  1430. if (signal_pending(current))
  1431. goto out;
  1432. /* This is for making all *used* pages to be on LRU. */
  1433. lru_add_drain_all();
  1434. ret = 0;
  1435. for_each_node_state(node, N_POSSIBLE) {
  1436. for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
  1437. enum lru_list l;
  1438. for_each_lru(l) {
  1439. ret = mem_cgroup_force_empty_list(mem,
  1440. node, zid, l);
  1441. if (ret)
  1442. break;
  1443. }
  1444. }
  1445. if (ret)
  1446. break;
  1447. }
  1448. /* it seems parent cgroup doesn't have enough mem */
  1449. if (ret == -ENOMEM)
  1450. goto try_to_free;
  1451. cond_resched();
  1452. }
  1453. ret = 0;
  1454. out:
  1455. css_put(&mem->css);
  1456. return ret;
  1457. try_to_free:
  1458. /* returns EBUSY if there is a task or if we come here twice. */
  1459. if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
  1460. ret = -EBUSY;
  1461. goto out;
  1462. }
  1463. /* we call try-to-free pages for make this cgroup empty */
  1464. lru_add_drain_all();
  1465. /* try to free all pages in this cgroup */
  1466. shrink = 1;
  1467. while (nr_retries && mem->res.usage > 0) {
  1468. int progress;
  1469. if (signal_pending(current)) {
  1470. ret = -EINTR;
  1471. goto out;
  1472. }
  1473. progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
  1474. false, get_swappiness(mem));
  1475. if (!progress) {
  1476. nr_retries--;
  1477. /* maybe some writeback is necessary */
  1478. congestion_wait(WRITE, HZ/10);
  1479. }
  1480. }
  1481. lru_add_drain();
  1482. /* try move_account...there may be some *locked* pages. */
  1483. if (mem->res.usage)
  1484. goto move_account;
  1485. ret = 0;
  1486. goto out;
  1487. }
  1488. int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
  1489. {
  1490. return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
  1491. }
  1492. static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
  1493. {
  1494. return mem_cgroup_from_cont(cont)->use_hierarchy;
  1495. }
  1496. static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
  1497. u64 val)
  1498. {
  1499. int retval = 0;
  1500. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  1501. struct cgroup *parent = cont->parent;
  1502. struct mem_cgroup *parent_mem = NULL;
  1503. if (parent)
  1504. parent_mem = mem_cgroup_from_cont(parent);
  1505. cgroup_lock();
  1506. /*
  1507. * If parent's use_hiearchy is set, we can't make any modifications
  1508. * in the child subtrees. If it is unset, then the change can
  1509. * occur, provided the current cgroup has no children.
  1510. *
  1511. * For the root cgroup, parent_mem is NULL, we allow value to be
  1512. * set if there are no children.
  1513. */
  1514. if ((!parent_mem || !parent_mem->use_hierarchy) &&
  1515. (val == 1 || val == 0)) {
  1516. if (list_empty(&cont->children))
  1517. mem->use_hierarchy = val;
  1518. else
  1519. retval = -EBUSY;
  1520. } else
  1521. retval = -EINVAL;
  1522. cgroup_unlock();
  1523. return retval;
  1524. }
  1525. static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
  1526. {
  1527. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  1528. u64 val = 0;
  1529. int type, name;
  1530. type = MEMFILE_TYPE(cft->private);
  1531. name = MEMFILE_ATTR(cft->private);
  1532. switch (type) {
  1533. case _MEM:
  1534. val = res_counter_read_u64(&mem->res, name);
  1535. break;
  1536. case _MEMSWAP:
  1537. if (do_swap_account)
  1538. val = res_counter_read_u64(&mem->memsw, name);
  1539. break;
  1540. default:
  1541. BUG();
  1542. break;
  1543. }
  1544. return val;
  1545. }
  1546. /*
  1547. * The user of this function is...
  1548. * RES_LIMIT.
  1549. */
  1550. static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  1551. const char *buffer)
  1552. {
  1553. struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
  1554. int type, name;
  1555. unsigned long long val;
  1556. int ret;
  1557. type = MEMFILE_TYPE(cft->private);
  1558. name = MEMFILE_ATTR(cft->private);
  1559. switch (name) {
  1560. case RES_LIMIT:
  1561. /* This function does all necessary parse...reuse it */
  1562. ret = res_counter_memparse_write_strategy(buffer, &val);
  1563. if (ret)
  1564. break;
  1565. if (type == _MEM)
  1566. ret = mem_cgroup_resize_limit(memcg, val);
  1567. else
  1568. ret = mem_cgroup_resize_memsw_limit(memcg, val);
  1569. break;
  1570. default:
  1571. ret = -EINVAL; /* should be BUG() ? */
  1572. break;
  1573. }
  1574. return ret;
  1575. }
  1576. static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
  1577. unsigned long long *mem_limit, unsigned long long *memsw_limit)
  1578. {
  1579. struct cgroup *cgroup;
  1580. unsigned long long min_limit, min_memsw_limit, tmp;
  1581. min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  1582. min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  1583. cgroup = memcg->css.cgroup;
  1584. if (!memcg->use_hierarchy)
  1585. goto out;
  1586. while (cgroup->parent) {
  1587. cgroup = cgroup->parent;
  1588. memcg = mem_cgroup_from_cont(cgroup);
  1589. if (!memcg->use_hierarchy)
  1590. break;
  1591. tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
  1592. min_limit = min(min_limit, tmp);
  1593. tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  1594. min_memsw_limit = min(min_memsw_limit, tmp);
  1595. }
  1596. out:
  1597. *mem_limit = min_limit;
  1598. *memsw_limit = min_memsw_limit;
  1599. return;
  1600. }
  1601. static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
  1602. {
  1603. struct mem_cgroup *mem;
  1604. int type, name;
  1605. mem = mem_cgroup_from_cont(cont);
  1606. type = MEMFILE_TYPE(event);
  1607. name = MEMFILE_ATTR(event);
  1608. switch (name) {
  1609. case RES_MAX_USAGE:
  1610. if (type == _MEM)
  1611. res_counter_reset_max(&mem->res);
  1612. else
  1613. res_counter_reset_max(&mem->memsw);
  1614. break;
  1615. case RES_FAILCNT:
  1616. if (type == _MEM)
  1617. res_counter_reset_failcnt(&mem->res);
  1618. else
  1619. res_counter_reset_failcnt(&mem->memsw);
  1620. break;
  1621. }
  1622. return 0;
  1623. }
  1624. static const struct mem_cgroup_stat_desc {
  1625. const char *msg;
  1626. u64 unit;
  1627. } mem_cgroup_stat_desc[] = {
  1628. [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
  1629. [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
  1630. [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
  1631. [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
  1632. };
  1633. static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
  1634. struct cgroup_map_cb *cb)
  1635. {
  1636. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  1637. struct mem_cgroup_stat *stat = &mem_cont->stat;
  1638. int i;
  1639. for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
  1640. s64 val;
  1641. val = mem_cgroup_read_stat(stat, i);
  1642. val *= mem_cgroup_stat_desc[i].unit;
  1643. cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
  1644. }
  1645. /* showing # of active pages */
  1646. {
  1647. unsigned long active_anon, inactive_anon;
  1648. unsigned long active_file, inactive_file;
  1649. unsigned long unevictable;
  1650. inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
  1651. LRU_INACTIVE_ANON);
  1652. active_anon = mem_cgroup_get_all_zonestat(mem_cont,
  1653. LRU_ACTIVE_ANON);
  1654. inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
  1655. LRU_INACTIVE_FILE);
  1656. active_file = mem_cgroup_get_all_zonestat(mem_cont,
  1657. LRU_ACTIVE_FILE);
  1658. unevictable = mem_cgroup_get_all_zonestat(mem_cont,
  1659. LRU_UNEVICTABLE);
  1660. cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
  1661. cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
  1662. cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
  1663. cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
  1664. cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
  1665. }
  1666. {
  1667. unsigned long long limit, memsw_limit;
  1668. memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
  1669. cb->fill(cb, "hierarchical_memory_limit", limit);
  1670. if (do_swap_account)
  1671. cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
  1672. }
  1673. #ifdef CONFIG_DEBUG_VM
  1674. cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
  1675. {
  1676. int nid, zid;
  1677. struct mem_cgroup_per_zone *mz;
  1678. unsigned long recent_rotated[2] = {0, 0};
  1679. unsigned long recent_scanned[2] = {0, 0};
  1680. for_each_online_node(nid)
  1681. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  1682. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  1683. recent_rotated[0] +=
  1684. mz->reclaim_stat.recent_rotated[0];
  1685. recent_rotated[1] +=
  1686. mz->reclaim_stat.recent_rotated[1];
  1687. recent_scanned[0] +=
  1688. mz->reclaim_stat.recent_scanned[0];
  1689. recent_scanned[1] +=
  1690. mz->reclaim_stat.recent_scanned[1];
  1691. }
  1692. cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
  1693. cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
  1694. cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
  1695. cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
  1696. }
  1697. #endif
  1698. return 0;
  1699. }
  1700. static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
  1701. {
  1702. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  1703. return get_swappiness(memcg);
  1704. }
  1705. static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
  1706. u64 val)
  1707. {
  1708. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  1709. struct mem_cgroup *parent;
  1710. if (val > 100)
  1711. return -EINVAL;
  1712. if (cgrp->parent == NULL)
  1713. return -EINVAL;
  1714. parent = mem_cgroup_from_cont(cgrp->parent);
  1715. /* If under hierarchy, only empty-root can set this value */
  1716. if ((parent->use_hierarchy) ||
  1717. (memcg->use_hierarchy && !list_empty(&cgrp->children)))
  1718. return -EINVAL;
  1719. spin_lock(&memcg->reclaim_param_lock);
  1720. memcg->swappiness = val;
  1721. spin_unlock(&memcg->reclaim_param_lock);
  1722. return 0;
  1723. }
  1724. static struct cftype mem_cgroup_files[] = {
  1725. {
  1726. .name = "usage_in_bytes",
  1727. .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
  1728. .read_u64 = mem_cgroup_read,
  1729. },
  1730. {
  1731. .name = "max_usage_in_bytes",
  1732. .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
  1733. .trigger = mem_cgroup_reset,
  1734. .read_u64 = mem_cgroup_read,
  1735. },
  1736. {
  1737. .name = "limit_in_bytes",
  1738. .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
  1739. .write_string = mem_cgroup_write,
  1740. .read_u64 = mem_cgroup_read,
  1741. },
  1742. {
  1743. .name = "failcnt",
  1744. .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
  1745. .trigger = mem_cgroup_reset,
  1746. .read_u64 = mem_cgroup_read,
  1747. },
  1748. {
  1749. .name = "stat",
  1750. .read_map = mem_control_stat_show,
  1751. },
  1752. {
  1753. .name = "force_empty",
  1754. .trigger = mem_cgroup_force_empty_write,
  1755. },
  1756. {
  1757. .name = "use_hierarchy",
  1758. .write_u64 = mem_cgroup_hierarchy_write,
  1759. .read_u64 = mem_cgroup_hierarchy_read,
  1760. },
  1761. {
  1762. .name = "swappiness",
  1763. .read_u64 = mem_cgroup_swappiness_read,
  1764. .write_u64 = mem_cgroup_swappiness_write,
  1765. },
  1766. };
  1767. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  1768. static struct cftype memsw_cgroup_files[] = {
  1769. {
  1770. .name = "memsw.usage_in_bytes",
  1771. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
  1772. .read_u64 = mem_cgroup_read,
  1773. },
  1774. {
  1775. .name = "memsw.max_usage_in_bytes",
  1776. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
  1777. .trigger = mem_cgroup_reset,
  1778. .read_u64 = mem_cgroup_read,
  1779. },
  1780. {
  1781. .name = "memsw.limit_in_bytes",
  1782. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
  1783. .write_string = mem_cgroup_write,
  1784. .read_u64 = mem_cgroup_read,
  1785. },
  1786. {
  1787. .name = "memsw.failcnt",
  1788. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
  1789. .trigger = mem_cgroup_reset,
  1790. .read_u64 = mem_cgroup_read,
  1791. },
  1792. };
  1793. static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
  1794. {
  1795. if (!do_swap_account)
  1796. return 0;
  1797. return cgroup_add_files(cont, ss, memsw_cgroup_files,
  1798. ARRAY_SIZE(memsw_cgroup_files));
  1799. };
  1800. #else
  1801. static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
  1802. {
  1803. return 0;
  1804. }
  1805. #endif
  1806. static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  1807. {
  1808. struct mem_cgroup_per_node *pn;
  1809. struct mem_cgroup_per_zone *mz;
  1810. enum lru_list l;
  1811. int zone, tmp = node;
  1812. /*
  1813. * This routine is called against possible nodes.
  1814. * But it's BUG to call kmalloc() against offline node.
  1815. *
  1816. * TODO: this routine can waste much memory for nodes which will
  1817. * never be onlined. It's better to use memory hotplug callback
  1818. * function.
  1819. */
  1820. if (!node_state(node, N_NORMAL_MEMORY))
  1821. tmp = -1;
  1822. pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
  1823. if (!pn)
  1824. return 1;
  1825. mem->info.nodeinfo[node] = pn;
  1826. memset(pn, 0, sizeof(*pn));
  1827. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  1828. mz = &pn->zoneinfo[zone];
  1829. for_each_lru(l)
  1830. INIT_LIST_HEAD(&mz->lists[l]);
  1831. }
  1832. return 0;
  1833. }
  1834. static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  1835. {
  1836. kfree(mem->info.nodeinfo[node]);
  1837. }
  1838. static int mem_cgroup_size(void)
  1839. {
  1840. int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
  1841. return sizeof(struct mem_cgroup) + cpustat_size;
  1842. }
  1843. static struct mem_cgroup *mem_cgroup_alloc(void)
  1844. {
  1845. struct mem_cgroup *mem;
  1846. int size = mem_cgroup_size();
  1847. if (size < PAGE_SIZE)
  1848. mem = kmalloc(size, GFP_KERNEL);
  1849. else
  1850. mem = vmalloc(size);
  1851. if (mem)
  1852. memset(mem, 0, size);
  1853. return mem;
  1854. }
  1855. /*
  1856. * At destroying mem_cgroup, references from swap_cgroup can remain.
  1857. * (scanning all at force_empty is too costly...)
  1858. *
  1859. * Instead of clearing all references at force_empty, we remember
  1860. * the number of reference from swap_cgroup and free mem_cgroup when
  1861. * it goes down to 0.
  1862. *
  1863. * Removal of cgroup itself succeeds regardless of refs from swap.
  1864. */
  1865. static void __mem_cgroup_free(struct mem_cgroup *mem)
  1866. {
  1867. int node;
  1868. for_each_node_state(node, N_POSSIBLE)
  1869. free_mem_cgroup_per_zone_info(mem, node);
  1870. if (mem_cgroup_size() < PAGE_SIZE)
  1871. kfree(mem);
  1872. else
  1873. vfree(mem);
  1874. }
  1875. static void mem_cgroup_get(struct mem_cgroup *mem)
  1876. {
  1877. atomic_inc(&mem->refcnt);
  1878. }
  1879. static void mem_cgroup_put(struct mem_cgroup *mem)
  1880. {
  1881. if (atomic_dec_and_test(&mem->refcnt))
  1882. __mem_cgroup_free(mem);
  1883. }
  1884. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  1885. static void __init enable_swap_cgroup(void)
  1886. {
  1887. if (!mem_cgroup_disabled() && really_do_swap_account)
  1888. do_swap_account = 1;
  1889. }
  1890. #else
  1891. static void __init enable_swap_cgroup(void)
  1892. {
  1893. }
  1894. #endif
  1895. static struct cgroup_subsys_state *
  1896. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  1897. {
  1898. struct mem_cgroup *mem, *parent;
  1899. int node;
  1900. mem = mem_cgroup_alloc();
  1901. if (!mem)
  1902. return ERR_PTR(-ENOMEM);
  1903. for_each_node_state(node, N_POSSIBLE)
  1904. if (alloc_mem_cgroup_per_zone_info(mem, node))
  1905. goto free_out;
  1906. /* root ? */
  1907. if (cont->parent == NULL) {
  1908. enable_swap_cgroup();
  1909. parent = NULL;
  1910. } else {
  1911. parent = mem_cgroup_from_cont(cont->parent);
  1912. mem->use_hierarchy = parent->use_hierarchy;
  1913. }
  1914. if (parent && parent->use_hierarchy) {
  1915. res_counter_init(&mem->res, &parent->res);
  1916. res_counter_init(&mem->memsw, &parent->memsw);
  1917. } else {
  1918. res_counter_init(&mem->res, NULL);
  1919. res_counter_init(&mem->memsw, NULL);
  1920. }
  1921. mem->last_scanned_child = NULL;
  1922. spin_lock_init(&mem->reclaim_param_lock);
  1923. if (parent)
  1924. mem->swappiness = get_swappiness(parent);
  1925. atomic_set(&mem->refcnt, 1);
  1926. return &mem->css;
  1927. free_out:
  1928. __mem_cgroup_free(mem);
  1929. return ERR_PTR(-ENOMEM);
  1930. }
  1931. static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
  1932. struct cgroup *cont)
  1933. {
  1934. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  1935. mem_cgroup_force_empty(mem, false);
  1936. }
  1937. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  1938. struct cgroup *cont)
  1939. {
  1940. mem_cgroup_put(mem_cgroup_from_cont(cont));
  1941. }
  1942. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  1943. struct cgroup *cont)
  1944. {
  1945. int ret;
  1946. ret = cgroup_add_files(cont, ss, mem_cgroup_files,
  1947. ARRAY_SIZE(mem_cgroup_files));
  1948. if (!ret)
  1949. ret = register_memsw_files(cont, ss);
  1950. return ret;
  1951. }
  1952. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  1953. struct cgroup *cont,
  1954. struct cgroup *old_cont,
  1955. struct task_struct *p)
  1956. {
  1957. mutex_lock(&memcg_tasklist);
  1958. /*
  1959. * FIXME: It's better to move charges of this process from old
  1960. * memcg to new memcg. But it's just on TODO-List now.
  1961. */
  1962. mutex_unlock(&memcg_tasklist);
  1963. }
  1964. struct cgroup_subsys mem_cgroup_subsys = {
  1965. .name = "memory",
  1966. .subsys_id = mem_cgroup_subsys_id,
  1967. .create = mem_cgroup_create,
  1968. .pre_destroy = mem_cgroup_pre_destroy,
  1969. .destroy = mem_cgroup_destroy,
  1970. .populate = mem_cgroup_populate,
  1971. .attach = mem_cgroup_move_task,
  1972. .early_init = 0,
  1973. };
  1974. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  1975. static int __init disable_swap_account(char *s)
  1976. {
  1977. really_do_swap_account = 0;
  1978. return 1;
  1979. }
  1980. __setup("noswapaccount", disable_swap_account);
  1981. #endif