memcontrol.c 64 KB

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