memcontrol.c 49 KB

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