memcontrol.c 53 KB

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