memcontrol.c 63 KB

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