memcontrol.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525
  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. /*
  1280. * called from __delete_from_swap_cache() and drop "page" account.
  1281. * memcg information is recorded to swap_cgroup of "ent"
  1282. */
  1283. void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
  1284. {
  1285. struct mem_cgroup *memcg;
  1286. memcg = __mem_cgroup_uncharge_common(page,
  1287. MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
  1288. /* record memcg information */
  1289. if (do_swap_account && memcg) {
  1290. swap_cgroup_record(ent, css_id(&memcg->css));
  1291. mem_cgroup_get(memcg);
  1292. }
  1293. if (memcg)
  1294. css_put(&memcg->css);
  1295. }
  1296. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  1297. /*
  1298. * called from swap_entry_free(). remove record in swap_cgroup and
  1299. * uncharge "memsw" account.
  1300. */
  1301. void mem_cgroup_uncharge_swap(swp_entry_t ent)
  1302. {
  1303. struct mem_cgroup *memcg;
  1304. unsigned short id;
  1305. if (!do_swap_account)
  1306. return;
  1307. id = swap_cgroup_record(ent, 0);
  1308. rcu_read_lock();
  1309. memcg = mem_cgroup_lookup(id);
  1310. if (memcg) {
  1311. /*
  1312. * We uncharge this because swap is freed.
  1313. * This memcg can be obsolete one. We avoid calling css_tryget
  1314. */
  1315. res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
  1316. mem_cgroup_put(memcg);
  1317. }
  1318. rcu_read_unlock();
  1319. }
  1320. #endif
  1321. /*
  1322. * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
  1323. * page belongs to.
  1324. */
  1325. int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
  1326. {
  1327. struct page_cgroup *pc;
  1328. struct mem_cgroup *mem = NULL;
  1329. int ret = 0;
  1330. if (mem_cgroup_disabled())
  1331. return 0;
  1332. pc = lookup_page_cgroup(page);
  1333. lock_page_cgroup(pc);
  1334. if (PageCgroupUsed(pc)) {
  1335. mem = pc->mem_cgroup;
  1336. css_get(&mem->css);
  1337. }
  1338. unlock_page_cgroup(pc);
  1339. if (mem) {
  1340. ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
  1341. css_put(&mem->css);
  1342. }
  1343. *ptr = mem;
  1344. return ret;
  1345. }
  1346. /* remove redundant charge if migration failed*/
  1347. void mem_cgroup_end_migration(struct mem_cgroup *mem,
  1348. struct page *oldpage, struct page *newpage)
  1349. {
  1350. struct page *target, *unused;
  1351. struct page_cgroup *pc;
  1352. enum charge_type ctype;
  1353. if (!mem)
  1354. return;
  1355. /* at migration success, oldpage->mapping is NULL. */
  1356. if (oldpage->mapping) {
  1357. target = oldpage;
  1358. unused = NULL;
  1359. } else {
  1360. target = newpage;
  1361. unused = oldpage;
  1362. }
  1363. if (PageAnon(target))
  1364. ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
  1365. else if (page_is_file_cache(target))
  1366. ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
  1367. else
  1368. ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
  1369. /* unused page is not on radix-tree now. */
  1370. if (unused)
  1371. __mem_cgroup_uncharge_common(unused, ctype);
  1372. pc = lookup_page_cgroup(target);
  1373. /*
  1374. * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
  1375. * So, double-counting is effectively avoided.
  1376. */
  1377. __mem_cgroup_commit_charge(mem, pc, ctype);
  1378. /*
  1379. * Both of oldpage and newpage are still under lock_page().
  1380. * Then, we don't have to care about race in radix-tree.
  1381. * But we have to be careful that this page is unmapped or not.
  1382. *
  1383. * There is a case for !page_mapped(). At the start of
  1384. * migration, oldpage was mapped. But now, it's zapped.
  1385. * But we know *target* page is not freed/reused under us.
  1386. * mem_cgroup_uncharge_page() does all necessary checks.
  1387. */
  1388. if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
  1389. mem_cgroup_uncharge_page(target);
  1390. }
  1391. /*
  1392. * A call to try to shrink memory usage on charge failure at shmem's swapin.
  1393. * Calling hierarchical_reclaim is not enough because we should update
  1394. * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
  1395. * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
  1396. * not from the memcg which this page would be charged to.
  1397. * try_charge_swapin does all of these works properly.
  1398. */
  1399. int mem_cgroup_shmem_charge_fallback(struct page *page,
  1400. struct mm_struct *mm,
  1401. gfp_t gfp_mask)
  1402. {
  1403. struct mem_cgroup *mem = NULL;
  1404. int ret;
  1405. if (mem_cgroup_disabled())
  1406. return 0;
  1407. ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
  1408. if (!ret)
  1409. mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
  1410. return ret;
  1411. }
  1412. static DEFINE_MUTEX(set_limit_mutex);
  1413. static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
  1414. unsigned long long val)
  1415. {
  1416. int retry_count;
  1417. int progress;
  1418. u64 memswlimit;
  1419. int ret = 0;
  1420. int children = mem_cgroup_count_children(memcg);
  1421. u64 curusage, oldusage;
  1422. /*
  1423. * For keeping hierarchical_reclaim simple, how long we should retry
  1424. * is depends on callers. We set our retry-count to be function
  1425. * of # of children which we should visit in this loop.
  1426. */
  1427. retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
  1428. oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
  1429. while (retry_count) {
  1430. if (signal_pending(current)) {
  1431. ret = -EINTR;
  1432. break;
  1433. }
  1434. /*
  1435. * Rather than hide all in some function, I do this in
  1436. * open coded manner. You see what this really does.
  1437. * We have to guarantee mem->res.limit < mem->memsw.limit.
  1438. */
  1439. mutex_lock(&set_limit_mutex);
  1440. memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  1441. if (memswlimit < val) {
  1442. ret = -EINVAL;
  1443. mutex_unlock(&set_limit_mutex);
  1444. break;
  1445. }
  1446. ret = res_counter_set_limit(&memcg->res, val);
  1447. mutex_unlock(&set_limit_mutex);
  1448. if (!ret)
  1449. break;
  1450. progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL,
  1451. false, true);
  1452. curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
  1453. /* Usage is reduced ? */
  1454. if (curusage >= oldusage)
  1455. retry_count--;
  1456. else
  1457. oldusage = curusage;
  1458. }
  1459. return ret;
  1460. }
  1461. int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
  1462. unsigned long long val)
  1463. {
  1464. int retry_count;
  1465. u64 memlimit, oldusage, curusage;
  1466. int children = mem_cgroup_count_children(memcg);
  1467. int ret = -EBUSY;
  1468. if (!do_swap_account)
  1469. return -EINVAL;
  1470. /* see mem_cgroup_resize_res_limit */
  1471. retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
  1472. oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
  1473. while (retry_count) {
  1474. if (signal_pending(current)) {
  1475. ret = -EINTR;
  1476. break;
  1477. }
  1478. /*
  1479. * Rather than hide all in some function, I do this in
  1480. * open coded manner. You see what this really does.
  1481. * We have to guarantee mem->res.limit < mem->memsw.limit.
  1482. */
  1483. mutex_lock(&set_limit_mutex);
  1484. memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  1485. if (memlimit > val) {
  1486. ret = -EINVAL;
  1487. mutex_unlock(&set_limit_mutex);
  1488. break;
  1489. }
  1490. ret = res_counter_set_limit(&memcg->memsw, val);
  1491. mutex_unlock(&set_limit_mutex);
  1492. if (!ret)
  1493. break;
  1494. mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true);
  1495. curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
  1496. /* Usage is reduced ? */
  1497. if (curusage >= oldusage)
  1498. retry_count--;
  1499. else
  1500. oldusage = curusage;
  1501. }
  1502. return ret;
  1503. }
  1504. /*
  1505. * This routine traverse page_cgroup in given list and drop them all.
  1506. * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  1507. */
  1508. static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
  1509. int node, int zid, enum lru_list lru)
  1510. {
  1511. struct zone *zone;
  1512. struct mem_cgroup_per_zone *mz;
  1513. struct page_cgroup *pc, *busy;
  1514. unsigned long flags, loop;
  1515. struct list_head *list;
  1516. int ret = 0;
  1517. zone = &NODE_DATA(node)->node_zones[zid];
  1518. mz = mem_cgroup_zoneinfo(mem, node, zid);
  1519. list = &mz->lists[lru];
  1520. loop = MEM_CGROUP_ZSTAT(mz, lru);
  1521. /* give some margin against EBUSY etc...*/
  1522. loop += 256;
  1523. busy = NULL;
  1524. while (loop--) {
  1525. ret = 0;
  1526. spin_lock_irqsave(&zone->lru_lock, flags);
  1527. if (list_empty(list)) {
  1528. spin_unlock_irqrestore(&zone->lru_lock, flags);
  1529. break;
  1530. }
  1531. pc = list_entry(list->prev, struct page_cgroup, lru);
  1532. if (busy == pc) {
  1533. list_move(&pc->lru, list);
  1534. busy = 0;
  1535. spin_unlock_irqrestore(&zone->lru_lock, flags);
  1536. continue;
  1537. }
  1538. spin_unlock_irqrestore(&zone->lru_lock, flags);
  1539. ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
  1540. if (ret == -ENOMEM)
  1541. break;
  1542. if (ret == -EBUSY || ret == -EINVAL) {
  1543. /* found lock contention or "pc" is obsolete. */
  1544. busy = pc;
  1545. cond_resched();
  1546. } else
  1547. busy = NULL;
  1548. }
  1549. if (!ret && !list_empty(list))
  1550. return -EBUSY;
  1551. return ret;
  1552. }
  1553. /*
  1554. * make mem_cgroup's charge to be 0 if there is no task.
  1555. * This enables deleting this mem_cgroup.
  1556. */
  1557. static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
  1558. {
  1559. int ret;
  1560. int node, zid, shrink;
  1561. int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  1562. struct cgroup *cgrp = mem->css.cgroup;
  1563. css_get(&mem->css);
  1564. shrink = 0;
  1565. /* should free all ? */
  1566. if (free_all)
  1567. goto try_to_free;
  1568. move_account:
  1569. while (mem->res.usage > 0) {
  1570. ret = -EBUSY;
  1571. if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
  1572. goto out;
  1573. ret = -EINTR;
  1574. if (signal_pending(current))
  1575. goto out;
  1576. /* This is for making all *used* pages to be on LRU. */
  1577. lru_add_drain_all();
  1578. ret = 0;
  1579. for_each_node_state(node, N_HIGH_MEMORY) {
  1580. for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
  1581. enum lru_list l;
  1582. for_each_lru(l) {
  1583. ret = mem_cgroup_force_empty_list(mem,
  1584. node, zid, l);
  1585. if (ret)
  1586. break;
  1587. }
  1588. }
  1589. if (ret)
  1590. break;
  1591. }
  1592. /* it seems parent cgroup doesn't have enough mem */
  1593. if (ret == -ENOMEM)
  1594. goto try_to_free;
  1595. cond_resched();
  1596. }
  1597. ret = 0;
  1598. out:
  1599. css_put(&mem->css);
  1600. return ret;
  1601. try_to_free:
  1602. /* returns EBUSY if there is a task or if we come here twice. */
  1603. if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
  1604. ret = -EBUSY;
  1605. goto out;
  1606. }
  1607. /* we call try-to-free pages for make this cgroup empty */
  1608. lru_add_drain_all();
  1609. /* try to free all pages in this cgroup */
  1610. shrink = 1;
  1611. while (nr_retries && mem->res.usage > 0) {
  1612. int progress;
  1613. if (signal_pending(current)) {
  1614. ret = -EINTR;
  1615. goto out;
  1616. }
  1617. progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
  1618. false, get_swappiness(mem));
  1619. if (!progress) {
  1620. nr_retries--;
  1621. /* maybe some writeback is necessary */
  1622. congestion_wait(WRITE, HZ/10);
  1623. }
  1624. }
  1625. lru_add_drain();
  1626. /* try move_account...there may be some *locked* pages. */
  1627. if (mem->res.usage)
  1628. goto move_account;
  1629. ret = 0;
  1630. goto out;
  1631. }
  1632. int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
  1633. {
  1634. return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
  1635. }
  1636. static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
  1637. {
  1638. return mem_cgroup_from_cont(cont)->use_hierarchy;
  1639. }
  1640. static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
  1641. u64 val)
  1642. {
  1643. int retval = 0;
  1644. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  1645. struct cgroup *parent = cont->parent;
  1646. struct mem_cgroup *parent_mem = NULL;
  1647. if (parent)
  1648. parent_mem = mem_cgroup_from_cont(parent);
  1649. cgroup_lock();
  1650. /*
  1651. * If parent's use_hiearchy is set, we can't make any modifications
  1652. * in the child subtrees. If it is unset, then the change can
  1653. * occur, provided the current cgroup has no children.
  1654. *
  1655. * For the root cgroup, parent_mem is NULL, we allow value to be
  1656. * set if there are no children.
  1657. */
  1658. if ((!parent_mem || !parent_mem->use_hierarchy) &&
  1659. (val == 1 || val == 0)) {
  1660. if (list_empty(&cont->children))
  1661. mem->use_hierarchy = val;
  1662. else
  1663. retval = -EBUSY;
  1664. } else
  1665. retval = -EINVAL;
  1666. cgroup_unlock();
  1667. return retval;
  1668. }
  1669. static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
  1670. {
  1671. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  1672. u64 val = 0;
  1673. int type, name;
  1674. type = MEMFILE_TYPE(cft->private);
  1675. name = MEMFILE_ATTR(cft->private);
  1676. switch (type) {
  1677. case _MEM:
  1678. val = res_counter_read_u64(&mem->res, name);
  1679. break;
  1680. case _MEMSWAP:
  1681. if (do_swap_account)
  1682. val = res_counter_read_u64(&mem->memsw, name);
  1683. break;
  1684. default:
  1685. BUG();
  1686. break;
  1687. }
  1688. return val;
  1689. }
  1690. /*
  1691. * The user of this function is...
  1692. * RES_LIMIT.
  1693. */
  1694. static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  1695. const char *buffer)
  1696. {
  1697. struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
  1698. int type, name;
  1699. unsigned long long val;
  1700. int ret;
  1701. type = MEMFILE_TYPE(cft->private);
  1702. name = MEMFILE_ATTR(cft->private);
  1703. switch (name) {
  1704. case RES_LIMIT:
  1705. /* This function does all necessary parse...reuse it */
  1706. ret = res_counter_memparse_write_strategy(buffer, &val);
  1707. if (ret)
  1708. break;
  1709. if (type == _MEM)
  1710. ret = mem_cgroup_resize_limit(memcg, val);
  1711. else
  1712. ret = mem_cgroup_resize_memsw_limit(memcg, val);
  1713. break;
  1714. default:
  1715. ret = -EINVAL; /* should be BUG() ? */
  1716. break;
  1717. }
  1718. return ret;
  1719. }
  1720. static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
  1721. unsigned long long *mem_limit, unsigned long long *memsw_limit)
  1722. {
  1723. struct cgroup *cgroup;
  1724. unsigned long long min_limit, min_memsw_limit, tmp;
  1725. min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  1726. min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  1727. cgroup = memcg->css.cgroup;
  1728. if (!memcg->use_hierarchy)
  1729. goto out;
  1730. while (cgroup->parent) {
  1731. cgroup = cgroup->parent;
  1732. memcg = mem_cgroup_from_cont(cgroup);
  1733. if (!memcg->use_hierarchy)
  1734. break;
  1735. tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
  1736. min_limit = min(min_limit, tmp);
  1737. tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  1738. min_memsw_limit = min(min_memsw_limit, tmp);
  1739. }
  1740. out:
  1741. *mem_limit = min_limit;
  1742. *memsw_limit = min_memsw_limit;
  1743. return;
  1744. }
  1745. static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
  1746. {
  1747. struct mem_cgroup *mem;
  1748. int type, name;
  1749. mem = mem_cgroup_from_cont(cont);
  1750. type = MEMFILE_TYPE(event);
  1751. name = MEMFILE_ATTR(event);
  1752. switch (name) {
  1753. case RES_MAX_USAGE:
  1754. if (type == _MEM)
  1755. res_counter_reset_max(&mem->res);
  1756. else
  1757. res_counter_reset_max(&mem->memsw);
  1758. break;
  1759. case RES_FAILCNT:
  1760. if (type == _MEM)
  1761. res_counter_reset_failcnt(&mem->res);
  1762. else
  1763. res_counter_reset_failcnt(&mem->memsw);
  1764. break;
  1765. }
  1766. return 0;
  1767. }
  1768. /* For read statistics */
  1769. enum {
  1770. MCS_CACHE,
  1771. MCS_RSS,
  1772. MCS_PGPGIN,
  1773. MCS_PGPGOUT,
  1774. MCS_INACTIVE_ANON,
  1775. MCS_ACTIVE_ANON,
  1776. MCS_INACTIVE_FILE,
  1777. MCS_ACTIVE_FILE,
  1778. MCS_UNEVICTABLE,
  1779. NR_MCS_STAT,
  1780. };
  1781. struct mcs_total_stat {
  1782. s64 stat[NR_MCS_STAT];
  1783. };
  1784. struct {
  1785. char *local_name;
  1786. char *total_name;
  1787. } memcg_stat_strings[NR_MCS_STAT] = {
  1788. {"cache", "total_cache"},
  1789. {"rss", "total_rss"},
  1790. {"pgpgin", "total_pgpgin"},
  1791. {"pgpgout", "total_pgpgout"},
  1792. {"inactive_anon", "total_inactive_anon"},
  1793. {"active_anon", "total_active_anon"},
  1794. {"inactive_file", "total_inactive_file"},
  1795. {"active_file", "total_active_file"},
  1796. {"unevictable", "total_unevictable"}
  1797. };
  1798. static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
  1799. {
  1800. struct mcs_total_stat *s = data;
  1801. s64 val;
  1802. /* per cpu stat */
  1803. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
  1804. s->stat[MCS_CACHE] += val * PAGE_SIZE;
  1805. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
  1806. s->stat[MCS_RSS] += val * PAGE_SIZE;
  1807. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
  1808. s->stat[MCS_PGPGIN] += val;
  1809. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
  1810. s->stat[MCS_PGPGOUT] += val;
  1811. /* per zone stat */
  1812. val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
  1813. s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
  1814. val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
  1815. s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
  1816. val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
  1817. s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
  1818. val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
  1819. s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
  1820. val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
  1821. s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
  1822. return 0;
  1823. }
  1824. static void
  1825. mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
  1826. {
  1827. mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
  1828. }
  1829. static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
  1830. struct cgroup_map_cb *cb)
  1831. {
  1832. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  1833. struct mcs_total_stat mystat;
  1834. int i;
  1835. memset(&mystat, 0, sizeof(mystat));
  1836. mem_cgroup_get_local_stat(mem_cont, &mystat);
  1837. for (i = 0; i < NR_MCS_STAT; i++)
  1838. cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
  1839. /* Hierarchical information */
  1840. {
  1841. unsigned long long limit, memsw_limit;
  1842. memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
  1843. cb->fill(cb, "hierarchical_memory_limit", limit);
  1844. if (do_swap_account)
  1845. cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
  1846. }
  1847. memset(&mystat, 0, sizeof(mystat));
  1848. mem_cgroup_get_total_stat(mem_cont, &mystat);
  1849. for (i = 0; i < NR_MCS_STAT; i++)
  1850. cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
  1851. #ifdef CONFIG_DEBUG_VM
  1852. cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
  1853. {
  1854. int nid, zid;
  1855. struct mem_cgroup_per_zone *mz;
  1856. unsigned long recent_rotated[2] = {0, 0};
  1857. unsigned long recent_scanned[2] = {0, 0};
  1858. for_each_online_node(nid)
  1859. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  1860. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  1861. recent_rotated[0] +=
  1862. mz->reclaim_stat.recent_rotated[0];
  1863. recent_rotated[1] +=
  1864. mz->reclaim_stat.recent_rotated[1];
  1865. recent_scanned[0] +=
  1866. mz->reclaim_stat.recent_scanned[0];
  1867. recent_scanned[1] +=
  1868. mz->reclaim_stat.recent_scanned[1];
  1869. }
  1870. cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
  1871. cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
  1872. cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
  1873. cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
  1874. }
  1875. #endif
  1876. return 0;
  1877. }
  1878. static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
  1879. {
  1880. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  1881. return get_swappiness(memcg);
  1882. }
  1883. static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
  1884. u64 val)
  1885. {
  1886. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  1887. struct mem_cgroup *parent;
  1888. if (val > 100)
  1889. return -EINVAL;
  1890. if (cgrp->parent == NULL)
  1891. return -EINVAL;
  1892. parent = mem_cgroup_from_cont(cgrp->parent);
  1893. cgroup_lock();
  1894. /* If under hierarchy, only empty-root can set this value */
  1895. if ((parent->use_hierarchy) ||
  1896. (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
  1897. cgroup_unlock();
  1898. return -EINVAL;
  1899. }
  1900. spin_lock(&memcg->reclaim_param_lock);
  1901. memcg->swappiness = val;
  1902. spin_unlock(&memcg->reclaim_param_lock);
  1903. cgroup_unlock();
  1904. return 0;
  1905. }
  1906. static struct cftype mem_cgroup_files[] = {
  1907. {
  1908. .name = "usage_in_bytes",
  1909. .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
  1910. .read_u64 = mem_cgroup_read,
  1911. },
  1912. {
  1913. .name = "max_usage_in_bytes",
  1914. .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
  1915. .trigger = mem_cgroup_reset,
  1916. .read_u64 = mem_cgroup_read,
  1917. },
  1918. {
  1919. .name = "limit_in_bytes",
  1920. .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
  1921. .write_string = mem_cgroup_write,
  1922. .read_u64 = mem_cgroup_read,
  1923. },
  1924. {
  1925. .name = "failcnt",
  1926. .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
  1927. .trigger = mem_cgroup_reset,
  1928. .read_u64 = mem_cgroup_read,
  1929. },
  1930. {
  1931. .name = "stat",
  1932. .read_map = mem_control_stat_show,
  1933. },
  1934. {
  1935. .name = "force_empty",
  1936. .trigger = mem_cgroup_force_empty_write,
  1937. },
  1938. {
  1939. .name = "use_hierarchy",
  1940. .write_u64 = mem_cgroup_hierarchy_write,
  1941. .read_u64 = mem_cgroup_hierarchy_read,
  1942. },
  1943. {
  1944. .name = "swappiness",
  1945. .read_u64 = mem_cgroup_swappiness_read,
  1946. .write_u64 = mem_cgroup_swappiness_write,
  1947. },
  1948. };
  1949. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  1950. static struct cftype memsw_cgroup_files[] = {
  1951. {
  1952. .name = "memsw.usage_in_bytes",
  1953. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
  1954. .read_u64 = mem_cgroup_read,
  1955. },
  1956. {
  1957. .name = "memsw.max_usage_in_bytes",
  1958. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
  1959. .trigger = mem_cgroup_reset,
  1960. .read_u64 = mem_cgroup_read,
  1961. },
  1962. {
  1963. .name = "memsw.limit_in_bytes",
  1964. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
  1965. .write_string = mem_cgroup_write,
  1966. .read_u64 = mem_cgroup_read,
  1967. },
  1968. {
  1969. .name = "memsw.failcnt",
  1970. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
  1971. .trigger = mem_cgroup_reset,
  1972. .read_u64 = mem_cgroup_read,
  1973. },
  1974. };
  1975. static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
  1976. {
  1977. if (!do_swap_account)
  1978. return 0;
  1979. return cgroup_add_files(cont, ss, memsw_cgroup_files,
  1980. ARRAY_SIZE(memsw_cgroup_files));
  1981. };
  1982. #else
  1983. static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
  1984. {
  1985. return 0;
  1986. }
  1987. #endif
  1988. static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  1989. {
  1990. struct mem_cgroup_per_node *pn;
  1991. struct mem_cgroup_per_zone *mz;
  1992. enum lru_list l;
  1993. int zone, tmp = node;
  1994. /*
  1995. * This routine is called against possible nodes.
  1996. * But it's BUG to call kmalloc() against offline node.
  1997. *
  1998. * TODO: this routine can waste much memory for nodes which will
  1999. * never be onlined. It's better to use memory hotplug callback
  2000. * function.
  2001. */
  2002. if (!node_state(node, N_NORMAL_MEMORY))
  2003. tmp = -1;
  2004. pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
  2005. if (!pn)
  2006. return 1;
  2007. mem->info.nodeinfo[node] = pn;
  2008. memset(pn, 0, sizeof(*pn));
  2009. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  2010. mz = &pn->zoneinfo[zone];
  2011. for_each_lru(l)
  2012. INIT_LIST_HEAD(&mz->lists[l]);
  2013. }
  2014. return 0;
  2015. }
  2016. static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  2017. {
  2018. kfree(mem->info.nodeinfo[node]);
  2019. }
  2020. static int mem_cgroup_size(void)
  2021. {
  2022. int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
  2023. return sizeof(struct mem_cgroup) + cpustat_size;
  2024. }
  2025. static struct mem_cgroup *mem_cgroup_alloc(void)
  2026. {
  2027. struct mem_cgroup *mem;
  2028. int size = mem_cgroup_size();
  2029. if (size < PAGE_SIZE)
  2030. mem = kmalloc(size, GFP_KERNEL);
  2031. else
  2032. mem = vmalloc(size);
  2033. if (mem)
  2034. memset(mem, 0, size);
  2035. return mem;
  2036. }
  2037. /*
  2038. * At destroying mem_cgroup, references from swap_cgroup can remain.
  2039. * (scanning all at force_empty is too costly...)
  2040. *
  2041. * Instead of clearing all references at force_empty, we remember
  2042. * the number of reference from swap_cgroup and free mem_cgroup when
  2043. * it goes down to 0.
  2044. *
  2045. * Removal of cgroup itself succeeds regardless of refs from swap.
  2046. */
  2047. static void __mem_cgroup_free(struct mem_cgroup *mem)
  2048. {
  2049. int node;
  2050. free_css_id(&mem_cgroup_subsys, &mem->css);
  2051. for_each_node_state(node, N_POSSIBLE)
  2052. free_mem_cgroup_per_zone_info(mem, node);
  2053. if (mem_cgroup_size() < PAGE_SIZE)
  2054. kfree(mem);
  2055. else
  2056. vfree(mem);
  2057. }
  2058. static void mem_cgroup_get(struct mem_cgroup *mem)
  2059. {
  2060. atomic_inc(&mem->refcnt);
  2061. }
  2062. static void mem_cgroup_put(struct mem_cgroup *mem)
  2063. {
  2064. if (atomic_dec_and_test(&mem->refcnt)) {
  2065. struct mem_cgroup *parent = parent_mem_cgroup(mem);
  2066. __mem_cgroup_free(mem);
  2067. if (parent)
  2068. mem_cgroup_put(parent);
  2069. }
  2070. }
  2071. /*
  2072. * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
  2073. */
  2074. static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
  2075. {
  2076. if (!mem->res.parent)
  2077. return NULL;
  2078. return mem_cgroup_from_res_counter(mem->res.parent, res);
  2079. }
  2080. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  2081. static void __init enable_swap_cgroup(void)
  2082. {
  2083. if (!mem_cgroup_disabled() && really_do_swap_account)
  2084. do_swap_account = 1;
  2085. }
  2086. #else
  2087. static void __init enable_swap_cgroup(void)
  2088. {
  2089. }
  2090. #endif
  2091. static struct cgroup_subsys_state * __ref
  2092. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  2093. {
  2094. struct mem_cgroup *mem, *parent;
  2095. long error = -ENOMEM;
  2096. int node;
  2097. mem = mem_cgroup_alloc();
  2098. if (!mem)
  2099. return ERR_PTR(error);
  2100. for_each_node_state(node, N_POSSIBLE)
  2101. if (alloc_mem_cgroup_per_zone_info(mem, node))
  2102. goto free_out;
  2103. /* root ? */
  2104. if (cont->parent == NULL) {
  2105. enable_swap_cgroup();
  2106. parent = NULL;
  2107. } else {
  2108. parent = mem_cgroup_from_cont(cont->parent);
  2109. mem->use_hierarchy = parent->use_hierarchy;
  2110. }
  2111. if (parent && parent->use_hierarchy) {
  2112. res_counter_init(&mem->res, &parent->res);
  2113. res_counter_init(&mem->memsw, &parent->memsw);
  2114. /*
  2115. * We increment refcnt of the parent to ensure that we can
  2116. * safely access it on res_counter_charge/uncharge.
  2117. * This refcnt will be decremented when freeing this
  2118. * mem_cgroup(see mem_cgroup_put).
  2119. */
  2120. mem_cgroup_get(parent);
  2121. } else {
  2122. res_counter_init(&mem->res, NULL);
  2123. res_counter_init(&mem->memsw, NULL);
  2124. }
  2125. mem->last_scanned_child = 0;
  2126. spin_lock_init(&mem->reclaim_param_lock);
  2127. if (parent)
  2128. mem->swappiness = get_swappiness(parent);
  2129. atomic_set(&mem->refcnt, 1);
  2130. return &mem->css;
  2131. free_out:
  2132. __mem_cgroup_free(mem);
  2133. return ERR_PTR(error);
  2134. }
  2135. static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
  2136. struct cgroup *cont)
  2137. {
  2138. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  2139. return mem_cgroup_force_empty(mem, false);
  2140. }
  2141. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  2142. struct cgroup *cont)
  2143. {
  2144. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  2145. mem_cgroup_put(mem);
  2146. }
  2147. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  2148. struct cgroup *cont)
  2149. {
  2150. int ret;
  2151. ret = cgroup_add_files(cont, ss, mem_cgroup_files,
  2152. ARRAY_SIZE(mem_cgroup_files));
  2153. if (!ret)
  2154. ret = register_memsw_files(cont, ss);
  2155. return ret;
  2156. }
  2157. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  2158. struct cgroup *cont,
  2159. struct cgroup *old_cont,
  2160. struct task_struct *p)
  2161. {
  2162. mutex_lock(&memcg_tasklist);
  2163. /*
  2164. * FIXME: It's better to move charges of this process from old
  2165. * memcg to new memcg. But it's just on TODO-List now.
  2166. */
  2167. mutex_unlock(&memcg_tasklist);
  2168. }
  2169. struct cgroup_subsys mem_cgroup_subsys = {
  2170. .name = "memory",
  2171. .subsys_id = mem_cgroup_subsys_id,
  2172. .create = mem_cgroup_create,
  2173. .pre_destroy = mem_cgroup_pre_destroy,
  2174. .destroy = mem_cgroup_destroy,
  2175. .populate = mem_cgroup_populate,
  2176. .attach = mem_cgroup_move_task,
  2177. .early_init = 0,
  2178. .use_id = 1,
  2179. };
  2180. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  2181. static int __init disable_swap_account(char *s)
  2182. {
  2183. really_do_swap_account = 0;
  2184. return 1;
  2185. }
  2186. __setup("noswapaccount", disable_swap_account);
  2187. #endif