memcontrol.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535
  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. /*
  857. * A helper function to get mem_cgroup from ID. must be called under
  858. * rcu_read_lock(). The caller must check css_is_removed() or some if
  859. * it's concern. (dropping refcnt from swap can be called against removed
  860. * memcg.)
  861. */
  862. static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
  863. {
  864. struct cgroup_subsys_state *css;
  865. /* ID 0 is unused ID */
  866. if (!id)
  867. return NULL;
  868. css = css_lookup(&mem_cgroup_subsys, id);
  869. if (!css)
  870. return NULL;
  871. return container_of(css, struct mem_cgroup, css);
  872. }
  873. static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
  874. {
  875. struct mem_cgroup *mem;
  876. struct page_cgroup *pc;
  877. unsigned short id;
  878. swp_entry_t ent;
  879. VM_BUG_ON(!PageLocked(page));
  880. if (!PageSwapCache(page))
  881. return NULL;
  882. pc = lookup_page_cgroup(page);
  883. /*
  884. * Used bit of swapcache is solid under page lock.
  885. */
  886. if (PageCgroupUsed(pc)) {
  887. mem = pc->mem_cgroup;
  888. if (mem && !css_tryget(&mem->css))
  889. mem = NULL;
  890. } else {
  891. ent.val = page_private(page);
  892. id = lookup_swap_cgroup(ent);
  893. rcu_read_lock();
  894. mem = mem_cgroup_lookup(id);
  895. if (mem && !css_tryget(&mem->css))
  896. mem = NULL;
  897. rcu_read_unlock();
  898. }
  899. return mem;
  900. }
  901. /*
  902. * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
  903. * USED state. If already USED, uncharge and return.
  904. */
  905. static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
  906. struct page_cgroup *pc,
  907. enum charge_type ctype)
  908. {
  909. /* try_charge() can return NULL to *memcg, taking care of it. */
  910. if (!mem)
  911. return;
  912. lock_page_cgroup(pc);
  913. if (unlikely(PageCgroupUsed(pc))) {
  914. unlock_page_cgroup(pc);
  915. res_counter_uncharge(&mem->res, PAGE_SIZE);
  916. if (do_swap_account)
  917. res_counter_uncharge(&mem->memsw, PAGE_SIZE);
  918. css_put(&mem->css);
  919. return;
  920. }
  921. pc->mem_cgroup = mem;
  922. smp_wmb();
  923. pc->flags = pcg_default_flags[ctype];
  924. mem_cgroup_charge_statistics(mem, pc, true);
  925. unlock_page_cgroup(pc);
  926. }
  927. /**
  928. * mem_cgroup_move_account - move account of the page
  929. * @pc: page_cgroup of the page.
  930. * @from: mem_cgroup which the page is moved from.
  931. * @to: mem_cgroup which the page is moved to. @from != @to.
  932. *
  933. * The caller must confirm following.
  934. * - page is not on LRU (isolate_page() is useful.)
  935. *
  936. * returns 0 at success,
  937. * returns -EBUSY when lock is busy or "pc" is unstable.
  938. *
  939. * This function does "uncharge" from old cgroup but doesn't do "charge" to
  940. * new cgroup. It should be done by a caller.
  941. */
  942. static int mem_cgroup_move_account(struct page_cgroup *pc,
  943. struct mem_cgroup *from, struct mem_cgroup *to)
  944. {
  945. struct mem_cgroup_per_zone *from_mz, *to_mz;
  946. int nid, zid;
  947. int ret = -EBUSY;
  948. VM_BUG_ON(from == to);
  949. VM_BUG_ON(PageLRU(pc->page));
  950. nid = page_cgroup_nid(pc);
  951. zid = page_cgroup_zid(pc);
  952. from_mz = mem_cgroup_zoneinfo(from, nid, zid);
  953. to_mz = mem_cgroup_zoneinfo(to, nid, zid);
  954. if (!trylock_page_cgroup(pc))
  955. return ret;
  956. if (!PageCgroupUsed(pc))
  957. goto out;
  958. if (pc->mem_cgroup != from)
  959. goto out;
  960. res_counter_uncharge(&from->res, PAGE_SIZE);
  961. mem_cgroup_charge_statistics(from, pc, false);
  962. if (do_swap_account)
  963. res_counter_uncharge(&from->memsw, PAGE_SIZE);
  964. css_put(&from->css);
  965. css_get(&to->css);
  966. pc->mem_cgroup = to;
  967. mem_cgroup_charge_statistics(to, pc, true);
  968. ret = 0;
  969. out:
  970. unlock_page_cgroup(pc);
  971. return ret;
  972. }
  973. /*
  974. * move charges to its parent.
  975. */
  976. static int mem_cgroup_move_parent(struct page_cgroup *pc,
  977. struct mem_cgroup *child,
  978. gfp_t gfp_mask)
  979. {
  980. struct page *page = pc->page;
  981. struct cgroup *cg = child->css.cgroup;
  982. struct cgroup *pcg = cg->parent;
  983. struct mem_cgroup *parent;
  984. int ret;
  985. /* Is ROOT ? */
  986. if (!pcg)
  987. return -EINVAL;
  988. parent = mem_cgroup_from_cont(pcg);
  989. ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
  990. if (ret || !parent)
  991. return ret;
  992. if (!get_page_unless_zero(page)) {
  993. ret = -EBUSY;
  994. goto uncharge;
  995. }
  996. ret = isolate_lru_page(page);
  997. if (ret)
  998. goto cancel;
  999. ret = mem_cgroup_move_account(pc, child, parent);
  1000. putback_lru_page(page);
  1001. if (!ret) {
  1002. put_page(page);
  1003. /* drop extra refcnt by try_charge() */
  1004. css_put(&parent->css);
  1005. return 0;
  1006. }
  1007. cancel:
  1008. put_page(page);
  1009. uncharge:
  1010. /* drop extra refcnt by try_charge() */
  1011. css_put(&parent->css);
  1012. /* uncharge if move fails */
  1013. res_counter_uncharge(&parent->res, PAGE_SIZE);
  1014. if (do_swap_account)
  1015. res_counter_uncharge(&parent->memsw, PAGE_SIZE);
  1016. return ret;
  1017. }
  1018. /*
  1019. * Charge the memory controller for page usage.
  1020. * Return
  1021. * 0 if the charge was successful
  1022. * < 0 if the cgroup is over its limit
  1023. */
  1024. static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
  1025. gfp_t gfp_mask, enum charge_type ctype,
  1026. struct mem_cgroup *memcg)
  1027. {
  1028. struct mem_cgroup *mem;
  1029. struct page_cgroup *pc;
  1030. int ret;
  1031. pc = lookup_page_cgroup(page);
  1032. /* can happen at boot */
  1033. if (unlikely(!pc))
  1034. return 0;
  1035. prefetchw(pc);
  1036. mem = memcg;
  1037. ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
  1038. if (ret || !mem)
  1039. return ret;
  1040. __mem_cgroup_commit_charge(mem, pc, ctype);
  1041. return 0;
  1042. }
  1043. int mem_cgroup_newpage_charge(struct page *page,
  1044. struct mm_struct *mm, gfp_t gfp_mask)
  1045. {
  1046. if (mem_cgroup_disabled())
  1047. return 0;
  1048. if (PageCompound(page))
  1049. return 0;
  1050. /*
  1051. * If already mapped, we don't have to account.
  1052. * If page cache, page->mapping has address_space.
  1053. * But page->mapping may have out-of-use anon_vma pointer,
  1054. * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
  1055. * is NULL.
  1056. */
  1057. if (page_mapped(page) || (page->mapping && !PageAnon(page)))
  1058. return 0;
  1059. if (unlikely(!mm))
  1060. mm = &init_mm;
  1061. return mem_cgroup_charge_common(page, mm, gfp_mask,
  1062. MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
  1063. }
  1064. static void
  1065. __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
  1066. enum charge_type ctype);
  1067. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  1068. gfp_t gfp_mask)
  1069. {
  1070. struct mem_cgroup *mem = NULL;
  1071. int ret;
  1072. if (mem_cgroup_disabled())
  1073. return 0;
  1074. if (PageCompound(page))
  1075. return 0;
  1076. /*
  1077. * Corner case handling. This is called from add_to_page_cache()
  1078. * in usual. But some FS (shmem) precharges this page before calling it
  1079. * and call add_to_page_cache() with GFP_NOWAIT.
  1080. *
  1081. * For GFP_NOWAIT case, the page may be pre-charged before calling
  1082. * add_to_page_cache(). (See shmem.c) check it here and avoid to call
  1083. * charge twice. (It works but has to pay a bit larger cost.)
  1084. * And when the page is SwapCache, it should take swap information
  1085. * into account. This is under lock_page() now.
  1086. */
  1087. if (!(gfp_mask & __GFP_WAIT)) {
  1088. struct page_cgroup *pc;
  1089. pc = lookup_page_cgroup(page);
  1090. if (!pc)
  1091. return 0;
  1092. lock_page_cgroup(pc);
  1093. if (PageCgroupUsed(pc)) {
  1094. unlock_page_cgroup(pc);
  1095. return 0;
  1096. }
  1097. unlock_page_cgroup(pc);
  1098. }
  1099. if (unlikely(!mm && !mem))
  1100. mm = &init_mm;
  1101. if (page_is_file_cache(page))
  1102. return mem_cgroup_charge_common(page, mm, gfp_mask,
  1103. MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
  1104. /* shmem */
  1105. if (PageSwapCache(page)) {
  1106. ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
  1107. if (!ret)
  1108. __mem_cgroup_commit_charge_swapin(page, mem,
  1109. MEM_CGROUP_CHARGE_TYPE_SHMEM);
  1110. } else
  1111. ret = mem_cgroup_charge_common(page, mm, gfp_mask,
  1112. MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
  1113. return ret;
  1114. }
  1115. /*
  1116. * While swap-in, try_charge -> commit or cancel, the page is locked.
  1117. * And when try_charge() successfully returns, one refcnt to memcg without
  1118. * struct page_cgroup is aquired. This refcnt will be cumsumed by
  1119. * "commit()" or removed by "cancel()"
  1120. */
  1121. int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
  1122. struct page *page,
  1123. gfp_t mask, struct mem_cgroup **ptr)
  1124. {
  1125. struct mem_cgroup *mem;
  1126. int ret;
  1127. if (mem_cgroup_disabled())
  1128. return 0;
  1129. if (!do_swap_account)
  1130. goto charge_cur_mm;
  1131. /*
  1132. * A racing thread's fault, or swapoff, may have already updated
  1133. * the pte, and even removed page from swap cache: return success
  1134. * to go on to do_swap_page()'s pte_same() test, which should fail.
  1135. */
  1136. if (!PageSwapCache(page))
  1137. return 0;
  1138. mem = try_get_mem_cgroup_from_swapcache(page);
  1139. if (!mem)
  1140. goto charge_cur_mm;
  1141. *ptr = mem;
  1142. ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
  1143. /* drop extra refcnt from tryget */
  1144. css_put(&mem->css);
  1145. return ret;
  1146. charge_cur_mm:
  1147. if (unlikely(!mm))
  1148. mm = &init_mm;
  1149. return __mem_cgroup_try_charge(mm, mask, ptr, true);
  1150. }
  1151. static void
  1152. __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
  1153. enum charge_type ctype)
  1154. {
  1155. struct page_cgroup *pc;
  1156. if (mem_cgroup_disabled())
  1157. return;
  1158. if (!ptr)
  1159. return;
  1160. pc = lookup_page_cgroup(page);
  1161. mem_cgroup_lru_del_before_commit_swapcache(page);
  1162. __mem_cgroup_commit_charge(ptr, pc, ctype);
  1163. mem_cgroup_lru_add_after_commit_swapcache(page);
  1164. /*
  1165. * Now swap is on-memory. This means this page may be
  1166. * counted both as mem and swap....double count.
  1167. * Fix it by uncharging from memsw. Basically, this SwapCache is stable
  1168. * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
  1169. * may call delete_from_swap_cache() before reach here.
  1170. */
  1171. if (do_swap_account && PageSwapCache(page)) {
  1172. swp_entry_t ent = {.val = page_private(page)};
  1173. unsigned short id;
  1174. struct mem_cgroup *memcg;
  1175. id = swap_cgroup_record(ent, 0);
  1176. rcu_read_lock();
  1177. memcg = mem_cgroup_lookup(id);
  1178. if (memcg) {
  1179. /*
  1180. * This recorded memcg can be obsolete one. So, avoid
  1181. * calling css_tryget
  1182. */
  1183. res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
  1184. mem_cgroup_put(memcg);
  1185. }
  1186. rcu_read_unlock();
  1187. }
  1188. /* add this page(page_cgroup) to the LRU we want. */
  1189. }
  1190. void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
  1191. {
  1192. __mem_cgroup_commit_charge_swapin(page, ptr,
  1193. MEM_CGROUP_CHARGE_TYPE_MAPPED);
  1194. }
  1195. void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
  1196. {
  1197. if (mem_cgroup_disabled())
  1198. return;
  1199. if (!mem)
  1200. return;
  1201. res_counter_uncharge(&mem->res, PAGE_SIZE);
  1202. if (do_swap_account)
  1203. res_counter_uncharge(&mem->memsw, PAGE_SIZE);
  1204. css_put(&mem->css);
  1205. }
  1206. /*
  1207. * uncharge if !page_mapped(page)
  1208. */
  1209. static struct mem_cgroup *
  1210. __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
  1211. {
  1212. struct page_cgroup *pc;
  1213. struct mem_cgroup *mem = NULL;
  1214. struct mem_cgroup_per_zone *mz;
  1215. if (mem_cgroup_disabled())
  1216. return NULL;
  1217. if (PageSwapCache(page))
  1218. return NULL;
  1219. /*
  1220. * Check if our page_cgroup is valid
  1221. */
  1222. pc = lookup_page_cgroup(page);
  1223. if (unlikely(!pc || !PageCgroupUsed(pc)))
  1224. return NULL;
  1225. lock_page_cgroup(pc);
  1226. mem = pc->mem_cgroup;
  1227. if (!PageCgroupUsed(pc))
  1228. goto unlock_out;
  1229. switch (ctype) {
  1230. case MEM_CGROUP_CHARGE_TYPE_MAPPED:
  1231. if (page_mapped(page))
  1232. goto unlock_out;
  1233. break;
  1234. case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
  1235. if (!PageAnon(page)) { /* Shared memory */
  1236. if (page->mapping && !page_is_file_cache(page))
  1237. goto unlock_out;
  1238. } else if (page_mapped(page)) /* Anon */
  1239. goto unlock_out;
  1240. break;
  1241. default:
  1242. break;
  1243. }
  1244. res_counter_uncharge(&mem->res, PAGE_SIZE);
  1245. if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
  1246. res_counter_uncharge(&mem->memsw, PAGE_SIZE);
  1247. mem_cgroup_charge_statistics(mem, pc, false);
  1248. ClearPageCgroupUsed(pc);
  1249. /*
  1250. * pc->mem_cgroup is not cleared here. It will be accessed when it's
  1251. * freed from LRU. This is safe because uncharged page is expected not
  1252. * to be reused (freed soon). Exception is SwapCache, it's handled by
  1253. * special functions.
  1254. */
  1255. mz = page_cgroup_zoneinfo(pc);
  1256. unlock_page_cgroup(pc);
  1257. /* at swapout, this memcg will be accessed to record to swap */
  1258. if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
  1259. css_put(&mem->css);
  1260. return mem;
  1261. unlock_out:
  1262. unlock_page_cgroup(pc);
  1263. return NULL;
  1264. }
  1265. void mem_cgroup_uncharge_page(struct page *page)
  1266. {
  1267. /* early check. */
  1268. if (page_mapped(page))
  1269. return;
  1270. if (page->mapping && !PageAnon(page))
  1271. return;
  1272. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
  1273. }
  1274. void mem_cgroup_uncharge_cache_page(struct page *page)
  1275. {
  1276. VM_BUG_ON(page_mapped(page));
  1277. VM_BUG_ON(page->mapping);
  1278. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
  1279. }
  1280. /*
  1281. * called from __delete_from_swap_cache() and drop "page" account.
  1282. * memcg information is recorded to swap_cgroup of "ent"
  1283. */
  1284. void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
  1285. {
  1286. struct mem_cgroup *memcg;
  1287. memcg = __mem_cgroup_uncharge_common(page,
  1288. MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
  1289. /* record memcg information */
  1290. if (do_swap_account && memcg) {
  1291. swap_cgroup_record(ent, css_id(&memcg->css));
  1292. mem_cgroup_get(memcg);
  1293. }
  1294. if (memcg)
  1295. css_put(&memcg->css);
  1296. }
  1297. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  1298. /*
  1299. * called from swap_entry_free(). remove record in swap_cgroup and
  1300. * uncharge "memsw" account.
  1301. */
  1302. void mem_cgroup_uncharge_swap(swp_entry_t ent)
  1303. {
  1304. struct mem_cgroup *memcg;
  1305. unsigned short id;
  1306. if (!do_swap_account)
  1307. return;
  1308. id = swap_cgroup_record(ent, 0);
  1309. rcu_read_lock();
  1310. memcg = mem_cgroup_lookup(id);
  1311. if (memcg) {
  1312. /*
  1313. * We uncharge this because swap is freed.
  1314. * This memcg can be obsolete one. We avoid calling css_tryget
  1315. */
  1316. res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
  1317. mem_cgroup_put(memcg);
  1318. }
  1319. rcu_read_unlock();
  1320. }
  1321. #endif
  1322. /*
  1323. * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
  1324. * page belongs to.
  1325. */
  1326. int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
  1327. {
  1328. struct page_cgroup *pc;
  1329. struct mem_cgroup *mem = NULL;
  1330. int ret = 0;
  1331. if (mem_cgroup_disabled())
  1332. return 0;
  1333. pc = lookup_page_cgroup(page);
  1334. lock_page_cgroup(pc);
  1335. if (PageCgroupUsed(pc)) {
  1336. mem = pc->mem_cgroup;
  1337. css_get(&mem->css);
  1338. }
  1339. unlock_page_cgroup(pc);
  1340. if (mem) {
  1341. ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
  1342. css_put(&mem->css);
  1343. }
  1344. *ptr = mem;
  1345. return ret;
  1346. }
  1347. /* remove redundant charge if migration failed*/
  1348. void mem_cgroup_end_migration(struct mem_cgroup *mem,
  1349. struct page *oldpage, struct page *newpage)
  1350. {
  1351. struct page *target, *unused;
  1352. struct page_cgroup *pc;
  1353. enum charge_type ctype;
  1354. if (!mem)
  1355. return;
  1356. /* at migration success, oldpage->mapping is NULL. */
  1357. if (oldpage->mapping) {
  1358. target = oldpage;
  1359. unused = NULL;
  1360. } else {
  1361. target = newpage;
  1362. unused = oldpage;
  1363. }
  1364. if (PageAnon(target))
  1365. ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
  1366. else if (page_is_file_cache(target))
  1367. ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
  1368. else
  1369. ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
  1370. /* unused page is not on radix-tree now. */
  1371. if (unused)
  1372. __mem_cgroup_uncharge_common(unused, ctype);
  1373. pc = lookup_page_cgroup(target);
  1374. /*
  1375. * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
  1376. * So, double-counting is effectively avoided.
  1377. */
  1378. __mem_cgroup_commit_charge(mem, pc, ctype);
  1379. /*
  1380. * Both of oldpage and newpage are still under lock_page().
  1381. * Then, we don't have to care about race in radix-tree.
  1382. * But we have to be careful that this page is unmapped or not.
  1383. *
  1384. * There is a case for !page_mapped(). At the start of
  1385. * migration, oldpage was mapped. But now, it's zapped.
  1386. * But we know *target* page is not freed/reused under us.
  1387. * mem_cgroup_uncharge_page() does all necessary checks.
  1388. */
  1389. if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
  1390. mem_cgroup_uncharge_page(target);
  1391. }
  1392. /*
  1393. * A call to try to shrink memory usage under specified resource controller.
  1394. * This is typically used for page reclaiming for shmem for reducing side
  1395. * effect of page allocation from shmem, which is used by some mem_cgroup.
  1396. */
  1397. int mem_cgroup_shrink_usage(struct page *page,
  1398. struct mm_struct *mm,
  1399. gfp_t gfp_mask)
  1400. {
  1401. struct mem_cgroup *mem = NULL;
  1402. int progress = 0;
  1403. int retry = MEM_CGROUP_RECLAIM_RETRIES;
  1404. if (mem_cgroup_disabled())
  1405. return 0;
  1406. if (page)
  1407. mem = try_get_mem_cgroup_from_swapcache(page);
  1408. if (!mem && mm)
  1409. mem = try_get_mem_cgroup_from_mm(mm);
  1410. if (unlikely(!mem))
  1411. return 0;
  1412. do {
  1413. progress = mem_cgroup_hierarchical_reclaim(mem,
  1414. gfp_mask, true, false);
  1415. progress += mem_cgroup_check_under_limit(mem);
  1416. } while (!progress && --retry);
  1417. css_put(&mem->css);
  1418. if (!retry)
  1419. return -ENOMEM;
  1420. return 0;
  1421. }
  1422. static DEFINE_MUTEX(set_limit_mutex);
  1423. static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
  1424. unsigned long long val)
  1425. {
  1426. int retry_count;
  1427. int progress;
  1428. u64 memswlimit;
  1429. int ret = 0;
  1430. int children = mem_cgroup_count_children(memcg);
  1431. u64 curusage, oldusage;
  1432. /*
  1433. * For keeping hierarchical_reclaim simple, how long we should retry
  1434. * is depends on callers. We set our retry-count to be function
  1435. * of # of children which we should visit in this loop.
  1436. */
  1437. retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
  1438. oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
  1439. while (retry_count) {
  1440. if (signal_pending(current)) {
  1441. ret = -EINTR;
  1442. break;
  1443. }
  1444. /*
  1445. * Rather than hide all in some function, I do this in
  1446. * open coded manner. You see what this really does.
  1447. * We have to guarantee mem->res.limit < mem->memsw.limit.
  1448. */
  1449. mutex_lock(&set_limit_mutex);
  1450. memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  1451. if (memswlimit < val) {
  1452. ret = -EINVAL;
  1453. mutex_unlock(&set_limit_mutex);
  1454. break;
  1455. }
  1456. ret = res_counter_set_limit(&memcg->res, val);
  1457. mutex_unlock(&set_limit_mutex);
  1458. if (!ret)
  1459. break;
  1460. progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL,
  1461. false, true);
  1462. curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
  1463. /* Usage is reduced ? */
  1464. if (curusage >= oldusage)
  1465. retry_count--;
  1466. else
  1467. oldusage = curusage;
  1468. }
  1469. return ret;
  1470. }
  1471. int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
  1472. unsigned long long val)
  1473. {
  1474. int retry_count;
  1475. u64 memlimit, oldusage, curusage;
  1476. int children = mem_cgroup_count_children(memcg);
  1477. int ret = -EBUSY;
  1478. if (!do_swap_account)
  1479. return -EINVAL;
  1480. /* see mem_cgroup_resize_res_limit */
  1481. retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
  1482. oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
  1483. while (retry_count) {
  1484. if (signal_pending(current)) {
  1485. ret = -EINTR;
  1486. break;
  1487. }
  1488. /*
  1489. * Rather than hide all in some function, I do this in
  1490. * open coded manner. You see what this really does.
  1491. * We have to guarantee mem->res.limit < mem->memsw.limit.
  1492. */
  1493. mutex_lock(&set_limit_mutex);
  1494. memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  1495. if (memlimit > val) {
  1496. ret = -EINVAL;
  1497. mutex_unlock(&set_limit_mutex);
  1498. break;
  1499. }
  1500. ret = res_counter_set_limit(&memcg->memsw, val);
  1501. mutex_unlock(&set_limit_mutex);
  1502. if (!ret)
  1503. break;
  1504. mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true);
  1505. curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
  1506. /* Usage is reduced ? */
  1507. if (curusage >= oldusage)
  1508. retry_count--;
  1509. else
  1510. oldusage = curusage;
  1511. }
  1512. return ret;
  1513. }
  1514. /*
  1515. * This routine traverse page_cgroup in given list and drop them all.
  1516. * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  1517. */
  1518. static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
  1519. int node, int zid, enum lru_list lru)
  1520. {
  1521. struct zone *zone;
  1522. struct mem_cgroup_per_zone *mz;
  1523. struct page_cgroup *pc, *busy;
  1524. unsigned long flags, loop;
  1525. struct list_head *list;
  1526. int ret = 0;
  1527. zone = &NODE_DATA(node)->node_zones[zid];
  1528. mz = mem_cgroup_zoneinfo(mem, node, zid);
  1529. list = &mz->lists[lru];
  1530. loop = MEM_CGROUP_ZSTAT(mz, lru);
  1531. /* give some margin against EBUSY etc...*/
  1532. loop += 256;
  1533. busy = NULL;
  1534. while (loop--) {
  1535. ret = 0;
  1536. spin_lock_irqsave(&zone->lru_lock, flags);
  1537. if (list_empty(list)) {
  1538. spin_unlock_irqrestore(&zone->lru_lock, flags);
  1539. break;
  1540. }
  1541. pc = list_entry(list->prev, struct page_cgroup, lru);
  1542. if (busy == pc) {
  1543. list_move(&pc->lru, list);
  1544. busy = 0;
  1545. spin_unlock_irqrestore(&zone->lru_lock, flags);
  1546. continue;
  1547. }
  1548. spin_unlock_irqrestore(&zone->lru_lock, flags);
  1549. ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
  1550. if (ret == -ENOMEM)
  1551. break;
  1552. if (ret == -EBUSY || ret == -EINVAL) {
  1553. /* found lock contention or "pc" is obsolete. */
  1554. busy = pc;
  1555. cond_resched();
  1556. } else
  1557. busy = NULL;
  1558. }
  1559. if (!ret && !list_empty(list))
  1560. return -EBUSY;
  1561. return ret;
  1562. }
  1563. /*
  1564. * make mem_cgroup's charge to be 0 if there is no task.
  1565. * This enables deleting this mem_cgroup.
  1566. */
  1567. static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
  1568. {
  1569. int ret;
  1570. int node, zid, shrink;
  1571. int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  1572. struct cgroup *cgrp = mem->css.cgroup;
  1573. css_get(&mem->css);
  1574. shrink = 0;
  1575. /* should free all ? */
  1576. if (free_all)
  1577. goto try_to_free;
  1578. move_account:
  1579. while (mem->res.usage > 0) {
  1580. ret = -EBUSY;
  1581. if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
  1582. goto out;
  1583. ret = -EINTR;
  1584. if (signal_pending(current))
  1585. goto out;
  1586. /* This is for making all *used* pages to be on LRU. */
  1587. lru_add_drain_all();
  1588. ret = 0;
  1589. for_each_node_state(node, N_HIGH_MEMORY) {
  1590. for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
  1591. enum lru_list l;
  1592. for_each_lru(l) {
  1593. ret = mem_cgroup_force_empty_list(mem,
  1594. node, zid, l);
  1595. if (ret)
  1596. break;
  1597. }
  1598. }
  1599. if (ret)
  1600. break;
  1601. }
  1602. /* it seems parent cgroup doesn't have enough mem */
  1603. if (ret == -ENOMEM)
  1604. goto try_to_free;
  1605. cond_resched();
  1606. }
  1607. ret = 0;
  1608. out:
  1609. css_put(&mem->css);
  1610. return ret;
  1611. try_to_free:
  1612. /* returns EBUSY if there is a task or if we come here twice. */
  1613. if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
  1614. ret = -EBUSY;
  1615. goto out;
  1616. }
  1617. /* we call try-to-free pages for make this cgroup empty */
  1618. lru_add_drain_all();
  1619. /* try to free all pages in this cgroup */
  1620. shrink = 1;
  1621. while (nr_retries && mem->res.usage > 0) {
  1622. int progress;
  1623. if (signal_pending(current)) {
  1624. ret = -EINTR;
  1625. goto out;
  1626. }
  1627. progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
  1628. false, get_swappiness(mem));
  1629. if (!progress) {
  1630. nr_retries--;
  1631. /* maybe some writeback is necessary */
  1632. congestion_wait(WRITE, HZ/10);
  1633. }
  1634. }
  1635. lru_add_drain();
  1636. /* try move_account...there may be some *locked* pages. */
  1637. if (mem->res.usage)
  1638. goto move_account;
  1639. ret = 0;
  1640. goto out;
  1641. }
  1642. int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
  1643. {
  1644. return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
  1645. }
  1646. static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
  1647. {
  1648. return mem_cgroup_from_cont(cont)->use_hierarchy;
  1649. }
  1650. static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
  1651. u64 val)
  1652. {
  1653. int retval = 0;
  1654. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  1655. struct cgroup *parent = cont->parent;
  1656. struct mem_cgroup *parent_mem = NULL;
  1657. if (parent)
  1658. parent_mem = mem_cgroup_from_cont(parent);
  1659. cgroup_lock();
  1660. /*
  1661. * If parent's use_hiearchy is set, we can't make any modifications
  1662. * in the child subtrees. If it is unset, then the change can
  1663. * occur, provided the current cgroup has no children.
  1664. *
  1665. * For the root cgroup, parent_mem is NULL, we allow value to be
  1666. * set if there are no children.
  1667. */
  1668. if ((!parent_mem || !parent_mem->use_hierarchy) &&
  1669. (val == 1 || val == 0)) {
  1670. if (list_empty(&cont->children))
  1671. mem->use_hierarchy = val;
  1672. else
  1673. retval = -EBUSY;
  1674. } else
  1675. retval = -EINVAL;
  1676. cgroup_unlock();
  1677. return retval;
  1678. }
  1679. static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
  1680. {
  1681. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  1682. u64 val = 0;
  1683. int type, name;
  1684. type = MEMFILE_TYPE(cft->private);
  1685. name = MEMFILE_ATTR(cft->private);
  1686. switch (type) {
  1687. case _MEM:
  1688. val = res_counter_read_u64(&mem->res, name);
  1689. break;
  1690. case _MEMSWAP:
  1691. if (do_swap_account)
  1692. val = res_counter_read_u64(&mem->memsw, name);
  1693. break;
  1694. default:
  1695. BUG();
  1696. break;
  1697. }
  1698. return val;
  1699. }
  1700. /*
  1701. * The user of this function is...
  1702. * RES_LIMIT.
  1703. */
  1704. static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  1705. const char *buffer)
  1706. {
  1707. struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
  1708. int type, name;
  1709. unsigned long long val;
  1710. int ret;
  1711. type = MEMFILE_TYPE(cft->private);
  1712. name = MEMFILE_ATTR(cft->private);
  1713. switch (name) {
  1714. case RES_LIMIT:
  1715. /* This function does all necessary parse...reuse it */
  1716. ret = res_counter_memparse_write_strategy(buffer, &val);
  1717. if (ret)
  1718. break;
  1719. if (type == _MEM)
  1720. ret = mem_cgroup_resize_limit(memcg, val);
  1721. else
  1722. ret = mem_cgroup_resize_memsw_limit(memcg, val);
  1723. break;
  1724. default:
  1725. ret = -EINVAL; /* should be BUG() ? */
  1726. break;
  1727. }
  1728. return ret;
  1729. }
  1730. static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
  1731. unsigned long long *mem_limit, unsigned long long *memsw_limit)
  1732. {
  1733. struct cgroup *cgroup;
  1734. unsigned long long min_limit, min_memsw_limit, tmp;
  1735. min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  1736. min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  1737. cgroup = memcg->css.cgroup;
  1738. if (!memcg->use_hierarchy)
  1739. goto out;
  1740. while (cgroup->parent) {
  1741. cgroup = cgroup->parent;
  1742. memcg = mem_cgroup_from_cont(cgroup);
  1743. if (!memcg->use_hierarchy)
  1744. break;
  1745. tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
  1746. min_limit = min(min_limit, tmp);
  1747. tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  1748. min_memsw_limit = min(min_memsw_limit, tmp);
  1749. }
  1750. out:
  1751. *mem_limit = min_limit;
  1752. *memsw_limit = min_memsw_limit;
  1753. return;
  1754. }
  1755. static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
  1756. {
  1757. struct mem_cgroup *mem;
  1758. int type, name;
  1759. mem = mem_cgroup_from_cont(cont);
  1760. type = MEMFILE_TYPE(event);
  1761. name = MEMFILE_ATTR(event);
  1762. switch (name) {
  1763. case RES_MAX_USAGE:
  1764. if (type == _MEM)
  1765. res_counter_reset_max(&mem->res);
  1766. else
  1767. res_counter_reset_max(&mem->memsw);
  1768. break;
  1769. case RES_FAILCNT:
  1770. if (type == _MEM)
  1771. res_counter_reset_failcnt(&mem->res);
  1772. else
  1773. res_counter_reset_failcnt(&mem->memsw);
  1774. break;
  1775. }
  1776. return 0;
  1777. }
  1778. /* For read statistics */
  1779. enum {
  1780. MCS_CACHE,
  1781. MCS_RSS,
  1782. MCS_PGPGIN,
  1783. MCS_PGPGOUT,
  1784. MCS_INACTIVE_ANON,
  1785. MCS_ACTIVE_ANON,
  1786. MCS_INACTIVE_FILE,
  1787. MCS_ACTIVE_FILE,
  1788. MCS_UNEVICTABLE,
  1789. NR_MCS_STAT,
  1790. };
  1791. struct mcs_total_stat {
  1792. s64 stat[NR_MCS_STAT];
  1793. };
  1794. struct {
  1795. char *local_name;
  1796. char *total_name;
  1797. } memcg_stat_strings[NR_MCS_STAT] = {
  1798. {"cache", "total_cache"},
  1799. {"rss", "total_rss"},
  1800. {"pgpgin", "total_pgpgin"},
  1801. {"pgpgout", "total_pgpgout"},
  1802. {"inactive_anon", "total_inactive_anon"},
  1803. {"active_anon", "total_active_anon"},
  1804. {"inactive_file", "total_inactive_file"},
  1805. {"active_file", "total_active_file"},
  1806. {"unevictable", "total_unevictable"}
  1807. };
  1808. static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
  1809. {
  1810. struct mcs_total_stat *s = data;
  1811. s64 val;
  1812. /* per cpu stat */
  1813. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
  1814. s->stat[MCS_CACHE] += val * PAGE_SIZE;
  1815. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
  1816. s->stat[MCS_RSS] += val * PAGE_SIZE;
  1817. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
  1818. s->stat[MCS_PGPGIN] += val;
  1819. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
  1820. s->stat[MCS_PGPGOUT] += val;
  1821. /* per zone stat */
  1822. val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
  1823. s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
  1824. val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
  1825. s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
  1826. val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
  1827. s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
  1828. val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
  1829. s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
  1830. val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
  1831. s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
  1832. return 0;
  1833. }
  1834. static void
  1835. mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
  1836. {
  1837. mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
  1838. }
  1839. static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
  1840. struct cgroup_map_cb *cb)
  1841. {
  1842. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  1843. struct mcs_total_stat mystat;
  1844. int i;
  1845. memset(&mystat, 0, sizeof(mystat));
  1846. mem_cgroup_get_local_stat(mem_cont, &mystat);
  1847. for (i = 0; i < NR_MCS_STAT; i++)
  1848. cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
  1849. /* Hierarchical information */
  1850. {
  1851. unsigned long long limit, memsw_limit;
  1852. memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
  1853. cb->fill(cb, "hierarchical_memory_limit", limit);
  1854. if (do_swap_account)
  1855. cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
  1856. }
  1857. memset(&mystat, 0, sizeof(mystat));
  1858. mem_cgroup_get_total_stat(mem_cont, &mystat);
  1859. for (i = 0; i < NR_MCS_STAT; i++)
  1860. cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
  1861. #ifdef CONFIG_DEBUG_VM
  1862. cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
  1863. {
  1864. int nid, zid;
  1865. struct mem_cgroup_per_zone *mz;
  1866. unsigned long recent_rotated[2] = {0, 0};
  1867. unsigned long recent_scanned[2] = {0, 0};
  1868. for_each_online_node(nid)
  1869. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  1870. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  1871. recent_rotated[0] +=
  1872. mz->reclaim_stat.recent_rotated[0];
  1873. recent_rotated[1] +=
  1874. mz->reclaim_stat.recent_rotated[1];
  1875. recent_scanned[0] +=
  1876. mz->reclaim_stat.recent_scanned[0];
  1877. recent_scanned[1] +=
  1878. mz->reclaim_stat.recent_scanned[1];
  1879. }
  1880. cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
  1881. cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
  1882. cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
  1883. cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
  1884. }
  1885. #endif
  1886. return 0;
  1887. }
  1888. static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
  1889. {
  1890. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  1891. return get_swappiness(memcg);
  1892. }
  1893. static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
  1894. u64 val)
  1895. {
  1896. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  1897. struct mem_cgroup *parent;
  1898. if (val > 100)
  1899. return -EINVAL;
  1900. if (cgrp->parent == NULL)
  1901. return -EINVAL;
  1902. parent = mem_cgroup_from_cont(cgrp->parent);
  1903. cgroup_lock();
  1904. /* If under hierarchy, only empty-root can set this value */
  1905. if ((parent->use_hierarchy) ||
  1906. (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
  1907. cgroup_unlock();
  1908. return -EINVAL;
  1909. }
  1910. spin_lock(&memcg->reclaim_param_lock);
  1911. memcg->swappiness = val;
  1912. spin_unlock(&memcg->reclaim_param_lock);
  1913. cgroup_unlock();
  1914. return 0;
  1915. }
  1916. static struct cftype mem_cgroup_files[] = {
  1917. {
  1918. .name = "usage_in_bytes",
  1919. .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
  1920. .read_u64 = mem_cgroup_read,
  1921. },
  1922. {
  1923. .name = "max_usage_in_bytes",
  1924. .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
  1925. .trigger = mem_cgroup_reset,
  1926. .read_u64 = mem_cgroup_read,
  1927. },
  1928. {
  1929. .name = "limit_in_bytes",
  1930. .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
  1931. .write_string = mem_cgroup_write,
  1932. .read_u64 = mem_cgroup_read,
  1933. },
  1934. {
  1935. .name = "failcnt",
  1936. .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
  1937. .trigger = mem_cgroup_reset,
  1938. .read_u64 = mem_cgroup_read,
  1939. },
  1940. {
  1941. .name = "stat",
  1942. .read_map = mem_control_stat_show,
  1943. },
  1944. {
  1945. .name = "force_empty",
  1946. .trigger = mem_cgroup_force_empty_write,
  1947. },
  1948. {
  1949. .name = "use_hierarchy",
  1950. .write_u64 = mem_cgroup_hierarchy_write,
  1951. .read_u64 = mem_cgroup_hierarchy_read,
  1952. },
  1953. {
  1954. .name = "swappiness",
  1955. .read_u64 = mem_cgroup_swappiness_read,
  1956. .write_u64 = mem_cgroup_swappiness_write,
  1957. },
  1958. };
  1959. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  1960. static struct cftype memsw_cgroup_files[] = {
  1961. {
  1962. .name = "memsw.usage_in_bytes",
  1963. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
  1964. .read_u64 = mem_cgroup_read,
  1965. },
  1966. {
  1967. .name = "memsw.max_usage_in_bytes",
  1968. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
  1969. .trigger = mem_cgroup_reset,
  1970. .read_u64 = mem_cgroup_read,
  1971. },
  1972. {
  1973. .name = "memsw.limit_in_bytes",
  1974. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
  1975. .write_string = mem_cgroup_write,
  1976. .read_u64 = mem_cgroup_read,
  1977. },
  1978. {
  1979. .name = "memsw.failcnt",
  1980. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
  1981. .trigger = mem_cgroup_reset,
  1982. .read_u64 = mem_cgroup_read,
  1983. },
  1984. };
  1985. static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
  1986. {
  1987. if (!do_swap_account)
  1988. return 0;
  1989. return cgroup_add_files(cont, ss, memsw_cgroup_files,
  1990. ARRAY_SIZE(memsw_cgroup_files));
  1991. };
  1992. #else
  1993. static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
  1994. {
  1995. return 0;
  1996. }
  1997. #endif
  1998. static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  1999. {
  2000. struct mem_cgroup_per_node *pn;
  2001. struct mem_cgroup_per_zone *mz;
  2002. enum lru_list l;
  2003. int zone, tmp = node;
  2004. /*
  2005. * This routine is called against possible nodes.
  2006. * But it's BUG to call kmalloc() against offline node.
  2007. *
  2008. * TODO: this routine can waste much memory for nodes which will
  2009. * never be onlined. It's better to use memory hotplug callback
  2010. * function.
  2011. */
  2012. if (!node_state(node, N_NORMAL_MEMORY))
  2013. tmp = -1;
  2014. pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
  2015. if (!pn)
  2016. return 1;
  2017. mem->info.nodeinfo[node] = pn;
  2018. memset(pn, 0, sizeof(*pn));
  2019. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  2020. mz = &pn->zoneinfo[zone];
  2021. for_each_lru(l)
  2022. INIT_LIST_HEAD(&mz->lists[l]);
  2023. }
  2024. return 0;
  2025. }
  2026. static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  2027. {
  2028. kfree(mem->info.nodeinfo[node]);
  2029. }
  2030. static int mem_cgroup_size(void)
  2031. {
  2032. int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
  2033. return sizeof(struct mem_cgroup) + cpustat_size;
  2034. }
  2035. static struct mem_cgroup *mem_cgroup_alloc(void)
  2036. {
  2037. struct mem_cgroup *mem;
  2038. int size = mem_cgroup_size();
  2039. if (size < PAGE_SIZE)
  2040. mem = kmalloc(size, GFP_KERNEL);
  2041. else
  2042. mem = vmalloc(size);
  2043. if (mem)
  2044. memset(mem, 0, size);
  2045. return mem;
  2046. }
  2047. /*
  2048. * At destroying mem_cgroup, references from swap_cgroup can remain.
  2049. * (scanning all at force_empty is too costly...)
  2050. *
  2051. * Instead of clearing all references at force_empty, we remember
  2052. * the number of reference from swap_cgroup and free mem_cgroup when
  2053. * it goes down to 0.
  2054. *
  2055. * Removal of cgroup itself succeeds regardless of refs from swap.
  2056. */
  2057. static void __mem_cgroup_free(struct mem_cgroup *mem)
  2058. {
  2059. int node;
  2060. free_css_id(&mem_cgroup_subsys, &mem->css);
  2061. for_each_node_state(node, N_POSSIBLE)
  2062. free_mem_cgroup_per_zone_info(mem, node);
  2063. if (mem_cgroup_size() < PAGE_SIZE)
  2064. kfree(mem);
  2065. else
  2066. vfree(mem);
  2067. }
  2068. static void mem_cgroup_get(struct mem_cgroup *mem)
  2069. {
  2070. atomic_inc(&mem->refcnt);
  2071. }
  2072. static void mem_cgroup_put(struct mem_cgroup *mem)
  2073. {
  2074. if (atomic_dec_and_test(&mem->refcnt)) {
  2075. struct mem_cgroup *parent = parent_mem_cgroup(mem);
  2076. __mem_cgroup_free(mem);
  2077. if (parent)
  2078. mem_cgroup_put(parent);
  2079. }
  2080. }
  2081. /*
  2082. * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
  2083. */
  2084. static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
  2085. {
  2086. if (!mem->res.parent)
  2087. return NULL;
  2088. return mem_cgroup_from_res_counter(mem->res.parent, res);
  2089. }
  2090. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  2091. static void __init enable_swap_cgroup(void)
  2092. {
  2093. if (!mem_cgroup_disabled() && really_do_swap_account)
  2094. do_swap_account = 1;
  2095. }
  2096. #else
  2097. static void __init enable_swap_cgroup(void)
  2098. {
  2099. }
  2100. #endif
  2101. static struct cgroup_subsys_state * __ref
  2102. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  2103. {
  2104. struct mem_cgroup *mem, *parent;
  2105. long error = -ENOMEM;
  2106. int node;
  2107. mem = mem_cgroup_alloc();
  2108. if (!mem)
  2109. return ERR_PTR(error);
  2110. for_each_node_state(node, N_POSSIBLE)
  2111. if (alloc_mem_cgroup_per_zone_info(mem, node))
  2112. goto free_out;
  2113. /* root ? */
  2114. if (cont->parent == NULL) {
  2115. enable_swap_cgroup();
  2116. parent = NULL;
  2117. } else {
  2118. parent = mem_cgroup_from_cont(cont->parent);
  2119. mem->use_hierarchy = parent->use_hierarchy;
  2120. }
  2121. if (parent && parent->use_hierarchy) {
  2122. res_counter_init(&mem->res, &parent->res);
  2123. res_counter_init(&mem->memsw, &parent->memsw);
  2124. /*
  2125. * We increment refcnt of the parent to ensure that we can
  2126. * safely access it on res_counter_charge/uncharge.
  2127. * This refcnt will be decremented when freeing this
  2128. * mem_cgroup(see mem_cgroup_put).
  2129. */
  2130. mem_cgroup_get(parent);
  2131. } else {
  2132. res_counter_init(&mem->res, NULL);
  2133. res_counter_init(&mem->memsw, NULL);
  2134. }
  2135. mem->last_scanned_child = 0;
  2136. spin_lock_init(&mem->reclaim_param_lock);
  2137. if (parent)
  2138. mem->swappiness = get_swappiness(parent);
  2139. atomic_set(&mem->refcnt, 1);
  2140. return &mem->css;
  2141. free_out:
  2142. __mem_cgroup_free(mem);
  2143. return ERR_PTR(error);
  2144. }
  2145. static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
  2146. struct cgroup *cont)
  2147. {
  2148. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  2149. return mem_cgroup_force_empty(mem, false);
  2150. }
  2151. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  2152. struct cgroup *cont)
  2153. {
  2154. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  2155. mem_cgroup_put(mem);
  2156. }
  2157. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  2158. struct cgroup *cont)
  2159. {
  2160. int ret;
  2161. ret = cgroup_add_files(cont, ss, mem_cgroup_files,
  2162. ARRAY_SIZE(mem_cgroup_files));
  2163. if (!ret)
  2164. ret = register_memsw_files(cont, ss);
  2165. return ret;
  2166. }
  2167. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  2168. struct cgroup *cont,
  2169. struct cgroup *old_cont,
  2170. struct task_struct *p)
  2171. {
  2172. mutex_lock(&memcg_tasklist);
  2173. /*
  2174. * FIXME: It's better to move charges of this process from old
  2175. * memcg to new memcg. But it's just on TODO-List now.
  2176. */
  2177. mutex_unlock(&memcg_tasklist);
  2178. }
  2179. struct cgroup_subsys mem_cgroup_subsys = {
  2180. .name = "memory",
  2181. .subsys_id = mem_cgroup_subsys_id,
  2182. .create = mem_cgroup_create,
  2183. .pre_destroy = mem_cgroup_pre_destroy,
  2184. .destroy = mem_cgroup_destroy,
  2185. .populate = mem_cgroup_populate,
  2186. .attach = mem_cgroup_move_task,
  2187. .early_init = 0,
  2188. .use_id = 1,
  2189. };
  2190. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  2191. static int __init disable_swap_account(char *s)
  2192. {
  2193. really_do_swap_account = 0;
  2194. return 1;
  2195. }
  2196. __setup("noswapaccount", disable_swap_account);
  2197. #endif