memcontrol.c 61 KB

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