memcontrol.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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/smp.h>
  24. #include <linux/page-flags.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/bit_spinlock.h>
  27. #include <linux/rcupdate.h>
  28. #include <linux/slab.h>
  29. #include <linux/swap.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/fs.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/vmalloc.h>
  34. #include <linux/mm_inline.h>
  35. #include <linux/page_cgroup.h>
  36. #include <asm/uaccess.h>
  37. struct cgroup_subsys mem_cgroup_subsys __read_mostly;
  38. #define MEM_CGROUP_RECLAIM_RETRIES 5
  39. /*
  40. * Statistics for memory cgroup.
  41. */
  42. enum mem_cgroup_stat_index {
  43. /*
  44. * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
  45. */
  46. MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
  47. MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
  48. MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
  49. MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
  50. MEM_CGROUP_STAT_NSTATS,
  51. };
  52. struct mem_cgroup_stat_cpu {
  53. s64 count[MEM_CGROUP_STAT_NSTATS];
  54. } ____cacheline_aligned_in_smp;
  55. struct mem_cgroup_stat {
  56. struct mem_cgroup_stat_cpu cpustat[NR_CPUS];
  57. };
  58. /*
  59. * For accounting under irq disable, no need for increment preempt count.
  60. */
  61. static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
  62. enum mem_cgroup_stat_index idx, int val)
  63. {
  64. stat->count[idx] += val;
  65. }
  66. static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
  67. enum mem_cgroup_stat_index idx)
  68. {
  69. int cpu;
  70. s64 ret = 0;
  71. for_each_possible_cpu(cpu)
  72. ret += stat->cpustat[cpu].count[idx];
  73. return ret;
  74. }
  75. /*
  76. * per-zone information in memory controller.
  77. */
  78. struct mem_cgroup_per_zone {
  79. /*
  80. * spin_lock to protect the per cgroup LRU
  81. */
  82. spinlock_t lru_lock;
  83. struct list_head lists[NR_LRU_LISTS];
  84. unsigned long count[NR_LRU_LISTS];
  85. };
  86. /* Macro for accessing counter */
  87. #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
  88. struct mem_cgroup_per_node {
  89. struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
  90. };
  91. struct mem_cgroup_lru_info {
  92. struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
  93. };
  94. /*
  95. * The memory controller data structure. The memory controller controls both
  96. * page cache and RSS per cgroup. We would eventually like to provide
  97. * statistics based on the statistics developed by Rik Van Riel for clock-pro,
  98. * to help the administrator determine what knobs to tune.
  99. *
  100. * TODO: Add a water mark for the memory controller. Reclaim will begin when
  101. * we hit the water mark. May be even add a low water mark, such that
  102. * no reclaim occurs from a cgroup at it's low water mark, this is
  103. * a feature that will be implemented much later in the future.
  104. */
  105. struct mem_cgroup {
  106. struct cgroup_subsys_state css;
  107. /*
  108. * the counter to account for memory usage
  109. */
  110. struct res_counter res;
  111. /*
  112. * Per cgroup active and inactive list, similar to the
  113. * per zone LRU lists.
  114. */
  115. struct mem_cgroup_lru_info info;
  116. int prev_priority; /* for recording reclaim priority */
  117. /*
  118. * statistics.
  119. */
  120. struct mem_cgroup_stat stat;
  121. };
  122. static struct mem_cgroup init_mem_cgroup;
  123. enum charge_type {
  124. MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
  125. MEM_CGROUP_CHARGE_TYPE_MAPPED,
  126. MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
  127. MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
  128. NR_CHARGE_TYPE,
  129. };
  130. /* only for here (for easy reading.) */
  131. #define PCGF_CACHE (1UL << PCG_CACHE)
  132. #define PCGF_USED (1UL << PCG_USED)
  133. #define PCGF_ACTIVE (1UL << PCG_ACTIVE)
  134. #define PCGF_LOCK (1UL << PCG_LOCK)
  135. #define PCGF_FILE (1UL << PCG_FILE)
  136. static const unsigned long
  137. pcg_default_flags[NR_CHARGE_TYPE] = {
  138. PCGF_CACHE | PCGF_FILE | PCGF_USED | PCGF_LOCK, /* File Cache */
  139. PCGF_ACTIVE | PCGF_USED | PCGF_LOCK, /* Anon */
  140. PCGF_ACTIVE | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
  141. 0, /* FORCE */
  142. };
  143. /*
  144. * Always modified under lru lock. Then, not necessary to preempt_disable()
  145. */
  146. static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
  147. struct page_cgroup *pc,
  148. bool charge)
  149. {
  150. int val = (charge)? 1 : -1;
  151. struct mem_cgroup_stat *stat = &mem->stat;
  152. struct mem_cgroup_stat_cpu *cpustat;
  153. VM_BUG_ON(!irqs_disabled());
  154. cpustat = &stat->cpustat[smp_processor_id()];
  155. if (PageCgroupCache(pc))
  156. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
  157. else
  158. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
  159. if (charge)
  160. __mem_cgroup_stat_add_safe(cpustat,
  161. MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
  162. else
  163. __mem_cgroup_stat_add_safe(cpustat,
  164. MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
  165. }
  166. static struct mem_cgroup_per_zone *
  167. mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
  168. {
  169. return &mem->info.nodeinfo[nid]->zoneinfo[zid];
  170. }
  171. static struct mem_cgroup_per_zone *
  172. page_cgroup_zoneinfo(struct page_cgroup *pc)
  173. {
  174. struct mem_cgroup *mem = pc->mem_cgroup;
  175. int nid = page_cgroup_nid(pc);
  176. int zid = page_cgroup_zid(pc);
  177. return mem_cgroup_zoneinfo(mem, nid, zid);
  178. }
  179. static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
  180. enum lru_list idx)
  181. {
  182. int nid, zid;
  183. struct mem_cgroup_per_zone *mz;
  184. u64 total = 0;
  185. for_each_online_node(nid)
  186. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  187. mz = mem_cgroup_zoneinfo(mem, nid, zid);
  188. total += MEM_CGROUP_ZSTAT(mz, idx);
  189. }
  190. return total;
  191. }
  192. static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
  193. {
  194. return container_of(cgroup_subsys_state(cont,
  195. mem_cgroup_subsys_id), struct mem_cgroup,
  196. css);
  197. }
  198. struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
  199. {
  200. /*
  201. * mm_update_next_owner() may clear mm->owner to NULL
  202. * if it races with swapoff, page migration, etc.
  203. * So this can be called with p == NULL.
  204. */
  205. if (unlikely(!p))
  206. return NULL;
  207. return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
  208. struct mem_cgroup, css);
  209. }
  210. static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
  211. struct page_cgroup *pc)
  212. {
  213. int lru = LRU_BASE;
  214. if (PageCgroupUnevictable(pc))
  215. lru = LRU_UNEVICTABLE;
  216. else {
  217. if (PageCgroupActive(pc))
  218. lru += LRU_ACTIVE;
  219. if (PageCgroupFile(pc))
  220. lru += LRU_FILE;
  221. }
  222. MEM_CGROUP_ZSTAT(mz, lru) -= 1;
  223. mem_cgroup_charge_statistics(pc->mem_cgroup, pc, false);
  224. list_del(&pc->lru);
  225. }
  226. static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
  227. struct page_cgroup *pc, bool hot)
  228. {
  229. int lru = LRU_BASE;
  230. if (PageCgroupUnevictable(pc))
  231. lru = LRU_UNEVICTABLE;
  232. else {
  233. if (PageCgroupActive(pc))
  234. lru += LRU_ACTIVE;
  235. if (PageCgroupFile(pc))
  236. lru += LRU_FILE;
  237. }
  238. MEM_CGROUP_ZSTAT(mz, lru) += 1;
  239. if (hot)
  240. list_add(&pc->lru, &mz->lists[lru]);
  241. else
  242. list_add_tail(&pc->lru, &mz->lists[lru]);
  243. mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true);
  244. }
  245. static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
  246. {
  247. struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
  248. int active = PageCgroupActive(pc);
  249. int file = PageCgroupFile(pc);
  250. int unevictable = PageCgroupUnevictable(pc);
  251. enum lru_list from = unevictable ? LRU_UNEVICTABLE :
  252. (LRU_FILE * !!file + !!active);
  253. if (lru == from)
  254. return;
  255. MEM_CGROUP_ZSTAT(mz, from) -= 1;
  256. /*
  257. * However this is done under mz->lru_lock, another flags, which
  258. * are not related to LRU, will be modified from out-of-lock.
  259. * We have to use atomic set/clear flags.
  260. */
  261. if (is_unevictable_lru(lru)) {
  262. ClearPageCgroupActive(pc);
  263. SetPageCgroupUnevictable(pc);
  264. } else {
  265. if (is_active_lru(lru))
  266. SetPageCgroupActive(pc);
  267. else
  268. ClearPageCgroupActive(pc);
  269. ClearPageCgroupUnevictable(pc);
  270. }
  271. MEM_CGROUP_ZSTAT(mz, lru) += 1;
  272. list_move(&pc->lru, &mz->lists[lru]);
  273. }
  274. int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
  275. {
  276. int ret;
  277. task_lock(task);
  278. ret = task->mm && mm_match_cgroup(task->mm, mem);
  279. task_unlock(task);
  280. return ret;
  281. }
  282. /*
  283. * This routine assumes that the appropriate zone's lru lock is already held
  284. */
  285. void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
  286. {
  287. struct page_cgroup *pc;
  288. struct mem_cgroup_per_zone *mz;
  289. unsigned long flags;
  290. if (mem_cgroup_subsys.disabled)
  291. return;
  292. /*
  293. * We cannot lock_page_cgroup while holding zone's lru_lock,
  294. * because other holders of lock_page_cgroup can be interrupted
  295. * with an attempt to rotate_reclaimable_page. But we cannot
  296. * safely get to page_cgroup without it, so just try_lock it:
  297. * mem_cgroup_isolate_pages allows for page left on wrong list.
  298. */
  299. pc = lookup_page_cgroup(page);
  300. if (!trylock_page_cgroup(pc))
  301. return;
  302. if (pc && PageCgroupUsed(pc)) {
  303. mz = page_cgroup_zoneinfo(pc);
  304. spin_lock_irqsave(&mz->lru_lock, flags);
  305. __mem_cgroup_move_lists(pc, lru);
  306. spin_unlock_irqrestore(&mz->lru_lock, flags);
  307. }
  308. unlock_page_cgroup(pc);
  309. }
  310. /*
  311. * Calculate mapped_ratio under memory controller. This will be used in
  312. * vmscan.c for deteremining we have to reclaim mapped pages.
  313. */
  314. int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
  315. {
  316. long total, rss;
  317. /*
  318. * usage is recorded in bytes. But, here, we assume the number of
  319. * physical pages can be represented by "long" on any arch.
  320. */
  321. total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
  322. rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
  323. return (int)((rss * 100L) / total);
  324. }
  325. /*
  326. * prev_priority control...this will be used in memory reclaim path.
  327. */
  328. int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
  329. {
  330. return mem->prev_priority;
  331. }
  332. void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
  333. {
  334. if (priority < mem->prev_priority)
  335. mem->prev_priority = priority;
  336. }
  337. void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
  338. {
  339. mem->prev_priority = priority;
  340. }
  341. /*
  342. * Calculate # of pages to be scanned in this priority/zone.
  343. * See also vmscan.c
  344. *
  345. * priority starts from "DEF_PRIORITY" and decremented in each loop.
  346. * (see include/linux/mmzone.h)
  347. */
  348. long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
  349. int priority, enum lru_list lru)
  350. {
  351. long nr_pages;
  352. int nid = zone->zone_pgdat->node_id;
  353. int zid = zone_idx(zone);
  354. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
  355. nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
  356. return (nr_pages >> priority);
  357. }
  358. unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
  359. struct list_head *dst,
  360. unsigned long *scanned, int order,
  361. int mode, struct zone *z,
  362. struct mem_cgroup *mem_cont,
  363. int active, int file)
  364. {
  365. unsigned long nr_taken = 0;
  366. struct page *page;
  367. unsigned long scan;
  368. LIST_HEAD(pc_list);
  369. struct list_head *src;
  370. struct page_cgroup *pc, *tmp;
  371. int nid = z->zone_pgdat->node_id;
  372. int zid = zone_idx(z);
  373. struct mem_cgroup_per_zone *mz;
  374. int lru = LRU_FILE * !!file + !!active;
  375. BUG_ON(!mem_cont);
  376. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  377. src = &mz->lists[lru];
  378. spin_lock(&mz->lru_lock);
  379. scan = 0;
  380. list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
  381. if (scan >= nr_to_scan)
  382. break;
  383. if (unlikely(!PageCgroupUsed(pc)))
  384. continue;
  385. page = pc->page;
  386. if (unlikely(!PageLRU(page)))
  387. continue;
  388. /*
  389. * TODO: play better with lumpy reclaim, grabbing anything.
  390. */
  391. if (PageUnevictable(page) ||
  392. (PageActive(page) && !active) ||
  393. (!PageActive(page) && active)) {
  394. __mem_cgroup_move_lists(pc, page_lru(page));
  395. continue;
  396. }
  397. scan++;
  398. list_move(&pc->lru, &pc_list);
  399. if (__isolate_lru_page(page, mode, file) == 0) {
  400. list_move(&page->lru, dst);
  401. nr_taken++;
  402. }
  403. }
  404. list_splice(&pc_list, src);
  405. spin_unlock(&mz->lru_lock);
  406. *scanned = scan;
  407. return nr_taken;
  408. }
  409. /*
  410. * Unlike exported interface, "oom" parameter is added. if oom==true,
  411. * oom-killer can be invoked.
  412. */
  413. static int __mem_cgroup_try_charge(struct mm_struct *mm,
  414. gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
  415. {
  416. struct mem_cgroup *mem;
  417. int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  418. /*
  419. * We always charge the cgroup the mm_struct belongs to.
  420. * The mm_struct's mem_cgroup changes on task migration if the
  421. * thread group leader migrates. It's possible that mm is not
  422. * set, if so charge the init_mm (happens for pagecache usage).
  423. */
  424. if (likely(!*memcg)) {
  425. rcu_read_lock();
  426. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  427. if (unlikely(!mem)) {
  428. rcu_read_unlock();
  429. return 0;
  430. }
  431. /*
  432. * For every charge from the cgroup, increment reference count
  433. */
  434. css_get(&mem->css);
  435. *memcg = mem;
  436. rcu_read_unlock();
  437. } else {
  438. mem = *memcg;
  439. css_get(&mem->css);
  440. }
  441. while (unlikely(res_counter_charge(&mem->res, PAGE_SIZE))) {
  442. if (!(gfp_mask & __GFP_WAIT))
  443. goto nomem;
  444. if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
  445. continue;
  446. /*
  447. * try_to_free_mem_cgroup_pages() might not give us a full
  448. * picture of reclaim. Some pages are reclaimed and might be
  449. * moved to swap cache or just unmapped from the cgroup.
  450. * Check the limit again to see if the reclaim reduced the
  451. * current usage of the cgroup before giving up
  452. */
  453. if (res_counter_check_under_limit(&mem->res))
  454. continue;
  455. if (!nr_retries--) {
  456. if (oom)
  457. mem_cgroup_out_of_memory(mem, gfp_mask);
  458. goto nomem;
  459. }
  460. }
  461. return 0;
  462. nomem:
  463. css_put(&mem->css);
  464. return -ENOMEM;
  465. }
  466. /**
  467. * mem_cgroup_try_charge - get charge of PAGE_SIZE.
  468. * @mm: an mm_struct which is charged against. (when *memcg is NULL)
  469. * @gfp_mask: gfp_mask for reclaim.
  470. * @memcg: a pointer to memory cgroup which is charged against.
  471. *
  472. * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
  473. * memory cgroup from @mm is got and stored in *memcg.
  474. *
  475. * Returns 0 if success. -ENOMEM at failure.
  476. * This call can invoke OOM-Killer.
  477. */
  478. int mem_cgroup_try_charge(struct mm_struct *mm,
  479. gfp_t mask, struct mem_cgroup **memcg)
  480. {
  481. return __mem_cgroup_try_charge(mm, mask, memcg, true);
  482. }
  483. /*
  484. * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
  485. * USED state. If already USED, uncharge and return.
  486. */
  487. static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
  488. struct page_cgroup *pc,
  489. enum charge_type ctype)
  490. {
  491. struct mem_cgroup_per_zone *mz;
  492. unsigned long flags;
  493. /* try_charge() can return NULL to *memcg, taking care of it. */
  494. if (!mem)
  495. return;
  496. lock_page_cgroup(pc);
  497. if (unlikely(PageCgroupUsed(pc))) {
  498. unlock_page_cgroup(pc);
  499. res_counter_uncharge(&mem->res, PAGE_SIZE);
  500. css_put(&mem->css);
  501. return;
  502. }
  503. pc->mem_cgroup = mem;
  504. /*
  505. * If a page is accounted as a page cache, insert to inactive list.
  506. * If anon, insert to active list.
  507. */
  508. pc->flags = pcg_default_flags[ctype];
  509. mz = page_cgroup_zoneinfo(pc);
  510. spin_lock_irqsave(&mz->lru_lock, flags);
  511. __mem_cgroup_add_list(mz, pc, true);
  512. spin_unlock_irqrestore(&mz->lru_lock, flags);
  513. unlock_page_cgroup(pc);
  514. }
  515. /**
  516. * mem_cgroup_move_account - move account of the page
  517. * @pc: page_cgroup of the page.
  518. * @from: mem_cgroup which the page is moved from.
  519. * @to: mem_cgroup which the page is moved to. @from != @to.
  520. *
  521. * The caller must confirm following.
  522. * 1. disable irq.
  523. * 2. lru_lock of old mem_cgroup(@from) should be held.
  524. *
  525. * returns 0 at success,
  526. * returns -EBUSY when lock is busy or "pc" is unstable.
  527. *
  528. * This function does "uncharge" from old cgroup but doesn't do "charge" to
  529. * new cgroup. It should be done by a caller.
  530. */
  531. static int mem_cgroup_move_account(struct page_cgroup *pc,
  532. struct mem_cgroup *from, struct mem_cgroup *to)
  533. {
  534. struct mem_cgroup_per_zone *from_mz, *to_mz;
  535. int nid, zid;
  536. int ret = -EBUSY;
  537. VM_BUG_ON(!irqs_disabled());
  538. VM_BUG_ON(from == to);
  539. nid = page_cgroup_nid(pc);
  540. zid = page_cgroup_zid(pc);
  541. from_mz = mem_cgroup_zoneinfo(from, nid, zid);
  542. to_mz = mem_cgroup_zoneinfo(to, nid, zid);
  543. if (!trylock_page_cgroup(pc))
  544. return ret;
  545. if (!PageCgroupUsed(pc))
  546. goto out;
  547. if (pc->mem_cgroup != from)
  548. goto out;
  549. if (spin_trylock(&to_mz->lru_lock)) {
  550. __mem_cgroup_remove_list(from_mz, pc);
  551. css_put(&from->css);
  552. res_counter_uncharge(&from->res, PAGE_SIZE);
  553. pc->mem_cgroup = to;
  554. css_get(&to->css);
  555. __mem_cgroup_add_list(to_mz, pc, false);
  556. ret = 0;
  557. spin_unlock(&to_mz->lru_lock);
  558. }
  559. out:
  560. unlock_page_cgroup(pc);
  561. return ret;
  562. }
  563. /*
  564. * move charges to its parent.
  565. */
  566. static int mem_cgroup_move_parent(struct page_cgroup *pc,
  567. struct mem_cgroup *child,
  568. gfp_t gfp_mask)
  569. {
  570. struct cgroup *cg = child->css.cgroup;
  571. struct cgroup *pcg = cg->parent;
  572. struct mem_cgroup *parent;
  573. struct mem_cgroup_per_zone *mz;
  574. unsigned long flags;
  575. int ret;
  576. /* Is ROOT ? */
  577. if (!pcg)
  578. return -EINVAL;
  579. parent = mem_cgroup_from_cont(pcg);
  580. ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
  581. if (ret)
  582. return ret;
  583. mz = mem_cgroup_zoneinfo(child,
  584. page_cgroup_nid(pc), page_cgroup_zid(pc));
  585. spin_lock_irqsave(&mz->lru_lock, flags);
  586. ret = mem_cgroup_move_account(pc, child, parent);
  587. spin_unlock_irqrestore(&mz->lru_lock, flags);
  588. /* drop extra refcnt */
  589. css_put(&parent->css);
  590. /* uncharge if move fails */
  591. if (ret)
  592. res_counter_uncharge(&parent->res, PAGE_SIZE);
  593. return ret;
  594. }
  595. /*
  596. * Charge the memory controller for page usage.
  597. * Return
  598. * 0 if the charge was successful
  599. * < 0 if the cgroup is over its limit
  600. */
  601. static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
  602. gfp_t gfp_mask, enum charge_type ctype,
  603. struct mem_cgroup *memcg)
  604. {
  605. struct mem_cgroup *mem;
  606. struct page_cgroup *pc;
  607. int ret;
  608. pc = lookup_page_cgroup(page);
  609. /* can happen at boot */
  610. if (unlikely(!pc))
  611. return 0;
  612. prefetchw(pc);
  613. mem = memcg;
  614. ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
  615. if (ret)
  616. return ret;
  617. __mem_cgroup_commit_charge(mem, pc, ctype);
  618. return 0;
  619. }
  620. int mem_cgroup_newpage_charge(struct page *page,
  621. struct mm_struct *mm, gfp_t gfp_mask)
  622. {
  623. if (mem_cgroup_subsys.disabled)
  624. return 0;
  625. if (PageCompound(page))
  626. return 0;
  627. /*
  628. * If already mapped, we don't have to account.
  629. * If page cache, page->mapping has address_space.
  630. * But page->mapping may have out-of-use anon_vma pointer,
  631. * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
  632. * is NULL.
  633. */
  634. if (page_mapped(page) || (page->mapping && !PageAnon(page)))
  635. return 0;
  636. if (unlikely(!mm))
  637. mm = &init_mm;
  638. return mem_cgroup_charge_common(page, mm, gfp_mask,
  639. MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
  640. }
  641. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  642. gfp_t gfp_mask)
  643. {
  644. if (mem_cgroup_subsys.disabled)
  645. return 0;
  646. if (PageCompound(page))
  647. return 0;
  648. /*
  649. * Corner case handling. This is called from add_to_page_cache()
  650. * in usual. But some FS (shmem) precharges this page before calling it
  651. * and call add_to_page_cache() with GFP_NOWAIT.
  652. *
  653. * For GFP_NOWAIT case, the page may be pre-charged before calling
  654. * add_to_page_cache(). (See shmem.c) check it here and avoid to call
  655. * charge twice. (It works but has to pay a bit larger cost.)
  656. */
  657. if (!(gfp_mask & __GFP_WAIT)) {
  658. struct page_cgroup *pc;
  659. pc = lookup_page_cgroup(page);
  660. if (!pc)
  661. return 0;
  662. lock_page_cgroup(pc);
  663. if (PageCgroupUsed(pc)) {
  664. unlock_page_cgroup(pc);
  665. return 0;
  666. }
  667. unlock_page_cgroup(pc);
  668. }
  669. if (unlikely(!mm))
  670. mm = &init_mm;
  671. if (page_is_file_cache(page))
  672. return mem_cgroup_charge_common(page, mm, gfp_mask,
  673. MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
  674. else
  675. return mem_cgroup_charge_common(page, mm, gfp_mask,
  676. MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
  677. }
  678. void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
  679. {
  680. struct page_cgroup *pc;
  681. if (mem_cgroup_subsys.disabled)
  682. return;
  683. if (!ptr)
  684. return;
  685. pc = lookup_page_cgroup(page);
  686. __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
  687. }
  688. void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
  689. {
  690. if (mem_cgroup_subsys.disabled)
  691. return;
  692. if (!mem)
  693. return;
  694. res_counter_uncharge(&mem->res, PAGE_SIZE);
  695. css_put(&mem->css);
  696. }
  697. /*
  698. * uncharge if !page_mapped(page)
  699. */
  700. static void
  701. __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
  702. {
  703. struct page_cgroup *pc;
  704. struct mem_cgroup *mem;
  705. struct mem_cgroup_per_zone *mz;
  706. unsigned long flags;
  707. if (mem_cgroup_subsys.disabled)
  708. return;
  709. /*
  710. * Check if our page_cgroup is valid
  711. */
  712. pc = lookup_page_cgroup(page);
  713. if (unlikely(!pc || !PageCgroupUsed(pc)))
  714. return;
  715. lock_page_cgroup(pc);
  716. if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED && page_mapped(page))
  717. || !PageCgroupUsed(pc)) {
  718. /* This happens at race in zap_pte_range() and do_swap_page()*/
  719. unlock_page_cgroup(pc);
  720. return;
  721. }
  722. ClearPageCgroupUsed(pc);
  723. mem = pc->mem_cgroup;
  724. mz = page_cgroup_zoneinfo(pc);
  725. spin_lock_irqsave(&mz->lru_lock, flags);
  726. __mem_cgroup_remove_list(mz, pc);
  727. spin_unlock_irqrestore(&mz->lru_lock, flags);
  728. unlock_page_cgroup(pc);
  729. res_counter_uncharge(&mem->res, PAGE_SIZE);
  730. css_put(&mem->css);
  731. return;
  732. }
  733. void mem_cgroup_uncharge_page(struct page *page)
  734. {
  735. /* early check. */
  736. if (page_mapped(page))
  737. return;
  738. if (page->mapping && !PageAnon(page))
  739. return;
  740. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
  741. }
  742. void mem_cgroup_uncharge_cache_page(struct page *page)
  743. {
  744. VM_BUG_ON(page_mapped(page));
  745. VM_BUG_ON(page->mapping);
  746. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
  747. }
  748. /*
  749. * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
  750. * page belongs to.
  751. */
  752. int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
  753. {
  754. struct page_cgroup *pc;
  755. struct mem_cgroup *mem = NULL;
  756. int ret = 0;
  757. if (mem_cgroup_subsys.disabled)
  758. return 0;
  759. pc = lookup_page_cgroup(page);
  760. lock_page_cgroup(pc);
  761. if (PageCgroupUsed(pc)) {
  762. mem = pc->mem_cgroup;
  763. css_get(&mem->css);
  764. }
  765. unlock_page_cgroup(pc);
  766. if (mem) {
  767. ret = mem_cgroup_try_charge(NULL, GFP_HIGHUSER_MOVABLE, &mem);
  768. css_put(&mem->css);
  769. }
  770. *ptr = mem;
  771. return ret;
  772. }
  773. /* remove redundant charge if migration failed*/
  774. void mem_cgroup_end_migration(struct mem_cgroup *mem,
  775. struct page *oldpage, struct page *newpage)
  776. {
  777. struct page *target, *unused;
  778. struct page_cgroup *pc;
  779. enum charge_type ctype;
  780. if (!mem)
  781. return;
  782. /* at migration success, oldpage->mapping is NULL. */
  783. if (oldpage->mapping) {
  784. target = oldpage;
  785. unused = NULL;
  786. } else {
  787. target = newpage;
  788. unused = oldpage;
  789. }
  790. if (PageAnon(target))
  791. ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
  792. else if (page_is_file_cache(target))
  793. ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
  794. else
  795. ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
  796. /* unused page is not on radix-tree now. */
  797. if (unused && ctype != MEM_CGROUP_CHARGE_TYPE_MAPPED)
  798. __mem_cgroup_uncharge_common(unused, ctype);
  799. pc = lookup_page_cgroup(target);
  800. /*
  801. * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
  802. * So, double-counting is effectively avoided.
  803. */
  804. __mem_cgroup_commit_charge(mem, pc, ctype);
  805. /*
  806. * Both of oldpage and newpage are still under lock_page().
  807. * Then, we don't have to care about race in radix-tree.
  808. * But we have to be careful that this page is unmapped or not.
  809. *
  810. * There is a case for !page_mapped(). At the start of
  811. * migration, oldpage was mapped. But now, it's zapped.
  812. * But we know *target* page is not freed/reused under us.
  813. * mem_cgroup_uncharge_page() does all necessary checks.
  814. */
  815. if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
  816. mem_cgroup_uncharge_page(target);
  817. }
  818. /*
  819. * A call to try to shrink memory usage under specified resource controller.
  820. * This is typically used for page reclaiming for shmem for reducing side
  821. * effect of page allocation from shmem, which is used by some mem_cgroup.
  822. */
  823. int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
  824. {
  825. struct mem_cgroup *mem;
  826. int progress = 0;
  827. int retry = MEM_CGROUP_RECLAIM_RETRIES;
  828. if (mem_cgroup_subsys.disabled)
  829. return 0;
  830. if (!mm)
  831. return 0;
  832. rcu_read_lock();
  833. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  834. if (unlikely(!mem)) {
  835. rcu_read_unlock();
  836. return 0;
  837. }
  838. css_get(&mem->css);
  839. rcu_read_unlock();
  840. do {
  841. progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
  842. progress += res_counter_check_under_limit(&mem->res);
  843. } while (!progress && --retry);
  844. css_put(&mem->css);
  845. if (!retry)
  846. return -ENOMEM;
  847. return 0;
  848. }
  849. static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
  850. unsigned long long val)
  851. {
  852. int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
  853. int progress;
  854. int ret = 0;
  855. while (res_counter_set_limit(&memcg->res, val)) {
  856. if (signal_pending(current)) {
  857. ret = -EINTR;
  858. break;
  859. }
  860. if (!retry_count) {
  861. ret = -EBUSY;
  862. break;
  863. }
  864. progress = try_to_free_mem_cgroup_pages(memcg,
  865. GFP_HIGHUSER_MOVABLE);
  866. if (!progress)
  867. retry_count--;
  868. }
  869. return ret;
  870. }
  871. /*
  872. * This routine traverse page_cgroup in given list and drop them all.
  873. * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  874. */
  875. static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
  876. struct mem_cgroup_per_zone *mz,
  877. enum lru_list lru)
  878. {
  879. struct page_cgroup *pc, *busy;
  880. unsigned long flags;
  881. unsigned long loop;
  882. struct list_head *list;
  883. int ret = 0;
  884. list = &mz->lists[lru];
  885. loop = MEM_CGROUP_ZSTAT(mz, lru);
  886. /* give some margin against EBUSY etc...*/
  887. loop += 256;
  888. busy = NULL;
  889. while (loop--) {
  890. ret = 0;
  891. spin_lock_irqsave(&mz->lru_lock, flags);
  892. if (list_empty(list)) {
  893. spin_unlock_irqrestore(&mz->lru_lock, flags);
  894. break;
  895. }
  896. pc = list_entry(list->prev, struct page_cgroup, lru);
  897. if (busy == pc) {
  898. list_move(&pc->lru, list);
  899. busy = 0;
  900. spin_unlock_irqrestore(&mz->lru_lock, flags);
  901. continue;
  902. }
  903. spin_unlock_irqrestore(&mz->lru_lock, flags);
  904. ret = mem_cgroup_move_parent(pc, mem, GFP_HIGHUSER_MOVABLE);
  905. if (ret == -ENOMEM)
  906. break;
  907. if (ret == -EBUSY || ret == -EINVAL) {
  908. /* found lock contention or "pc" is obsolete. */
  909. busy = pc;
  910. cond_resched();
  911. } else
  912. busy = NULL;
  913. }
  914. if (!ret && !list_empty(list))
  915. return -EBUSY;
  916. return ret;
  917. }
  918. /*
  919. * make mem_cgroup's charge to be 0 if there is no task.
  920. * This enables deleting this mem_cgroup.
  921. */
  922. static int mem_cgroup_force_empty(struct mem_cgroup *mem)
  923. {
  924. int ret;
  925. int node, zid, shrink;
  926. int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  927. css_get(&mem->css);
  928. shrink = 0;
  929. move_account:
  930. while (mem->res.usage > 0) {
  931. ret = -EBUSY;
  932. if (atomic_read(&mem->css.cgroup->count) > 0)
  933. goto out;
  934. /* This is for making all *used* pages to be on LRU. */
  935. lru_add_drain_all();
  936. ret = 0;
  937. for_each_node_state(node, N_POSSIBLE) {
  938. for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
  939. struct mem_cgroup_per_zone *mz;
  940. enum lru_list l;
  941. mz = mem_cgroup_zoneinfo(mem, node, zid);
  942. for_each_lru(l) {
  943. ret = mem_cgroup_force_empty_list(mem,
  944. mz, l);
  945. if (ret)
  946. break;
  947. }
  948. }
  949. if (ret)
  950. break;
  951. }
  952. /* it seems parent cgroup doesn't have enough mem */
  953. if (ret == -ENOMEM)
  954. goto try_to_free;
  955. cond_resched();
  956. }
  957. ret = 0;
  958. out:
  959. css_put(&mem->css);
  960. return ret;
  961. try_to_free:
  962. /* returns EBUSY if we come here twice. */
  963. if (shrink) {
  964. ret = -EBUSY;
  965. goto out;
  966. }
  967. /* try to free all pages in this cgroup */
  968. shrink = 1;
  969. while (nr_retries && mem->res.usage > 0) {
  970. int progress;
  971. progress = try_to_free_mem_cgroup_pages(mem,
  972. GFP_HIGHUSER_MOVABLE);
  973. if (!progress)
  974. nr_retries--;
  975. }
  976. /* try move_account...there may be some *locked* pages. */
  977. if (mem->res.usage)
  978. goto move_account;
  979. ret = 0;
  980. goto out;
  981. }
  982. static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
  983. {
  984. return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
  985. cft->private);
  986. }
  987. /*
  988. * The user of this function is...
  989. * RES_LIMIT.
  990. */
  991. static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  992. const char *buffer)
  993. {
  994. struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
  995. unsigned long long val;
  996. int ret;
  997. switch (cft->private) {
  998. case RES_LIMIT:
  999. /* This function does all necessary parse...reuse it */
  1000. ret = res_counter_memparse_write_strategy(buffer, &val);
  1001. if (!ret)
  1002. ret = mem_cgroup_resize_limit(memcg, val);
  1003. break;
  1004. default:
  1005. ret = -EINVAL; /* should be BUG() ? */
  1006. break;
  1007. }
  1008. return ret;
  1009. }
  1010. static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
  1011. {
  1012. struct mem_cgroup *mem;
  1013. mem = mem_cgroup_from_cont(cont);
  1014. switch (event) {
  1015. case RES_MAX_USAGE:
  1016. res_counter_reset_max(&mem->res);
  1017. break;
  1018. case RES_FAILCNT:
  1019. res_counter_reset_failcnt(&mem->res);
  1020. break;
  1021. }
  1022. return 0;
  1023. }
  1024. static const struct mem_cgroup_stat_desc {
  1025. const char *msg;
  1026. u64 unit;
  1027. } mem_cgroup_stat_desc[] = {
  1028. [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
  1029. [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
  1030. [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
  1031. [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
  1032. };
  1033. static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
  1034. struct cgroup_map_cb *cb)
  1035. {
  1036. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  1037. struct mem_cgroup_stat *stat = &mem_cont->stat;
  1038. int i;
  1039. for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
  1040. s64 val;
  1041. val = mem_cgroup_read_stat(stat, i);
  1042. val *= mem_cgroup_stat_desc[i].unit;
  1043. cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
  1044. }
  1045. /* showing # of active pages */
  1046. {
  1047. unsigned long active_anon, inactive_anon;
  1048. unsigned long active_file, inactive_file;
  1049. unsigned long unevictable;
  1050. inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
  1051. LRU_INACTIVE_ANON);
  1052. active_anon = mem_cgroup_get_all_zonestat(mem_cont,
  1053. LRU_ACTIVE_ANON);
  1054. inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
  1055. LRU_INACTIVE_FILE);
  1056. active_file = mem_cgroup_get_all_zonestat(mem_cont,
  1057. LRU_ACTIVE_FILE);
  1058. unevictable = mem_cgroup_get_all_zonestat(mem_cont,
  1059. LRU_UNEVICTABLE);
  1060. cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
  1061. cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
  1062. cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
  1063. cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
  1064. cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
  1065. }
  1066. return 0;
  1067. }
  1068. static struct cftype mem_cgroup_files[] = {
  1069. {
  1070. .name = "usage_in_bytes",
  1071. .private = RES_USAGE,
  1072. .read_u64 = mem_cgroup_read,
  1073. },
  1074. {
  1075. .name = "max_usage_in_bytes",
  1076. .private = RES_MAX_USAGE,
  1077. .trigger = mem_cgroup_reset,
  1078. .read_u64 = mem_cgroup_read,
  1079. },
  1080. {
  1081. .name = "limit_in_bytes",
  1082. .private = RES_LIMIT,
  1083. .write_string = mem_cgroup_write,
  1084. .read_u64 = mem_cgroup_read,
  1085. },
  1086. {
  1087. .name = "failcnt",
  1088. .private = RES_FAILCNT,
  1089. .trigger = mem_cgroup_reset,
  1090. .read_u64 = mem_cgroup_read,
  1091. },
  1092. {
  1093. .name = "stat",
  1094. .read_map = mem_control_stat_show,
  1095. },
  1096. };
  1097. static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  1098. {
  1099. struct mem_cgroup_per_node *pn;
  1100. struct mem_cgroup_per_zone *mz;
  1101. enum lru_list l;
  1102. int zone, tmp = node;
  1103. /*
  1104. * This routine is called against possible nodes.
  1105. * But it's BUG to call kmalloc() against offline node.
  1106. *
  1107. * TODO: this routine can waste much memory for nodes which will
  1108. * never be onlined. It's better to use memory hotplug callback
  1109. * function.
  1110. */
  1111. if (!node_state(node, N_NORMAL_MEMORY))
  1112. tmp = -1;
  1113. pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
  1114. if (!pn)
  1115. return 1;
  1116. mem->info.nodeinfo[node] = pn;
  1117. memset(pn, 0, sizeof(*pn));
  1118. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  1119. mz = &pn->zoneinfo[zone];
  1120. spin_lock_init(&mz->lru_lock);
  1121. for_each_lru(l)
  1122. INIT_LIST_HEAD(&mz->lists[l]);
  1123. }
  1124. return 0;
  1125. }
  1126. static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  1127. {
  1128. kfree(mem->info.nodeinfo[node]);
  1129. }
  1130. static struct mem_cgroup *mem_cgroup_alloc(void)
  1131. {
  1132. struct mem_cgroup *mem;
  1133. if (sizeof(*mem) < PAGE_SIZE)
  1134. mem = kmalloc(sizeof(*mem), GFP_KERNEL);
  1135. else
  1136. mem = vmalloc(sizeof(*mem));
  1137. if (mem)
  1138. memset(mem, 0, sizeof(*mem));
  1139. return mem;
  1140. }
  1141. static void mem_cgroup_free(struct mem_cgroup *mem)
  1142. {
  1143. if (sizeof(*mem) < PAGE_SIZE)
  1144. kfree(mem);
  1145. else
  1146. vfree(mem);
  1147. }
  1148. static struct cgroup_subsys_state *
  1149. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  1150. {
  1151. struct mem_cgroup *mem;
  1152. int node;
  1153. if (unlikely((cont->parent) == NULL)) {
  1154. mem = &init_mem_cgroup;
  1155. } else {
  1156. mem = mem_cgroup_alloc();
  1157. if (!mem)
  1158. return ERR_PTR(-ENOMEM);
  1159. }
  1160. res_counter_init(&mem->res);
  1161. for_each_node_state(node, N_POSSIBLE)
  1162. if (alloc_mem_cgroup_per_zone_info(mem, node))
  1163. goto free_out;
  1164. return &mem->css;
  1165. free_out:
  1166. for_each_node_state(node, N_POSSIBLE)
  1167. free_mem_cgroup_per_zone_info(mem, node);
  1168. if (cont->parent != NULL)
  1169. mem_cgroup_free(mem);
  1170. return ERR_PTR(-ENOMEM);
  1171. }
  1172. static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
  1173. struct cgroup *cont)
  1174. {
  1175. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  1176. mem_cgroup_force_empty(mem);
  1177. }
  1178. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  1179. struct cgroup *cont)
  1180. {
  1181. int node;
  1182. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  1183. for_each_node_state(node, N_POSSIBLE)
  1184. free_mem_cgroup_per_zone_info(mem, node);
  1185. mem_cgroup_free(mem_cgroup_from_cont(cont));
  1186. }
  1187. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  1188. struct cgroup *cont)
  1189. {
  1190. return cgroup_add_files(cont, ss, mem_cgroup_files,
  1191. ARRAY_SIZE(mem_cgroup_files));
  1192. }
  1193. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  1194. struct cgroup *cont,
  1195. struct cgroup *old_cont,
  1196. struct task_struct *p)
  1197. {
  1198. struct mm_struct *mm;
  1199. struct mem_cgroup *mem, *old_mem;
  1200. mm = get_task_mm(p);
  1201. if (mm == NULL)
  1202. return;
  1203. mem = mem_cgroup_from_cont(cont);
  1204. old_mem = mem_cgroup_from_cont(old_cont);
  1205. /*
  1206. * Only thread group leaders are allowed to migrate, the mm_struct is
  1207. * in effect owned by the leader
  1208. */
  1209. if (!thread_group_leader(p))
  1210. goto out;
  1211. out:
  1212. mmput(mm);
  1213. }
  1214. struct cgroup_subsys mem_cgroup_subsys = {
  1215. .name = "memory",
  1216. .subsys_id = mem_cgroup_subsys_id,
  1217. .create = mem_cgroup_create,
  1218. .pre_destroy = mem_cgroup_pre_destroy,
  1219. .destroy = mem_cgroup_destroy,
  1220. .populate = mem_cgroup_populate,
  1221. .attach = mem_cgroup_move_task,
  1222. .early_init = 0,
  1223. };