memcontrol.c 56 KB

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