memcontrol.c 63 KB

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