memcontrol.c 64 KB

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