memcontrol.c 61 KB

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