memcontrol.c 56 KB

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