memcontrol.c 53 KB

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