memcontrol.c 49 KB

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