memcontrol.c 65 KB

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