memcontrol.c 53 KB

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