memcontrol.c 54 KB

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