memcontrol.c 53 KB

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