memcontrol.c 61 KB

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