memcontrol.c 66 KB

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