memcontrol.c 61 KB

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