memcontrol.c 49 KB

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