memcontrol.c 60 KB

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