memcontrol.c 57 KB

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