memcontrol.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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)
  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. list_add(&pc->lru, &mz->lists[lru]);
  240. mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true);
  241. }
  242. static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
  243. {
  244. struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
  245. int active = PageCgroupActive(pc);
  246. int file = PageCgroupFile(pc);
  247. int unevictable = PageCgroupUnevictable(pc);
  248. enum lru_list from = unevictable ? LRU_UNEVICTABLE :
  249. (LRU_FILE * !!file + !!active);
  250. if (lru == from)
  251. return;
  252. MEM_CGROUP_ZSTAT(mz, from) -= 1;
  253. /*
  254. * However this is done under mz->lru_lock, another flags, which
  255. * are not related to LRU, will be modified from out-of-lock.
  256. * We have to use atomic set/clear flags.
  257. */
  258. if (is_unevictable_lru(lru)) {
  259. ClearPageCgroupActive(pc);
  260. SetPageCgroupUnevictable(pc);
  261. } else {
  262. if (is_active_lru(lru))
  263. SetPageCgroupActive(pc);
  264. else
  265. ClearPageCgroupActive(pc);
  266. ClearPageCgroupUnevictable(pc);
  267. }
  268. MEM_CGROUP_ZSTAT(mz, lru) += 1;
  269. list_move(&pc->lru, &mz->lists[lru]);
  270. }
  271. int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
  272. {
  273. int ret;
  274. task_lock(task);
  275. ret = task->mm && mm_match_cgroup(task->mm, mem);
  276. task_unlock(task);
  277. return ret;
  278. }
  279. /*
  280. * This routine assumes that the appropriate zone's lru lock is already held
  281. */
  282. void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
  283. {
  284. struct page_cgroup *pc;
  285. struct mem_cgroup_per_zone *mz;
  286. unsigned long flags;
  287. if (mem_cgroup_subsys.disabled)
  288. return;
  289. /*
  290. * We cannot lock_page_cgroup while holding zone's lru_lock,
  291. * because other holders of lock_page_cgroup can be interrupted
  292. * with an attempt to rotate_reclaimable_page. But we cannot
  293. * safely get to page_cgroup without it, so just try_lock it:
  294. * mem_cgroup_isolate_pages allows for page left on wrong list.
  295. */
  296. pc = lookup_page_cgroup(page);
  297. if (!trylock_page_cgroup(pc))
  298. return;
  299. if (pc && PageCgroupUsed(pc)) {
  300. mz = page_cgroup_zoneinfo(pc);
  301. spin_lock_irqsave(&mz->lru_lock, flags);
  302. __mem_cgroup_move_lists(pc, lru);
  303. spin_unlock_irqrestore(&mz->lru_lock, flags);
  304. }
  305. unlock_page_cgroup(pc);
  306. }
  307. /*
  308. * Calculate mapped_ratio under memory controller. This will be used in
  309. * vmscan.c for deteremining we have to reclaim mapped pages.
  310. */
  311. int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
  312. {
  313. long total, rss;
  314. /*
  315. * usage is recorded in bytes. But, here, we assume the number of
  316. * physical pages can be represented by "long" on any arch.
  317. */
  318. total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
  319. rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
  320. return (int)((rss * 100L) / total);
  321. }
  322. /*
  323. * prev_priority control...this will be used in memory reclaim path.
  324. */
  325. int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
  326. {
  327. return mem->prev_priority;
  328. }
  329. void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
  330. {
  331. if (priority < mem->prev_priority)
  332. mem->prev_priority = priority;
  333. }
  334. void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
  335. {
  336. mem->prev_priority = priority;
  337. }
  338. /*
  339. * Calculate # of pages to be scanned in this priority/zone.
  340. * See also vmscan.c
  341. *
  342. * priority starts from "DEF_PRIORITY" and decremented in each loop.
  343. * (see include/linux/mmzone.h)
  344. */
  345. long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
  346. int priority, enum lru_list lru)
  347. {
  348. long nr_pages;
  349. int nid = zone->zone_pgdat->node_id;
  350. int zid = zone_idx(zone);
  351. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
  352. nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
  353. return (nr_pages >> priority);
  354. }
  355. unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
  356. struct list_head *dst,
  357. unsigned long *scanned, int order,
  358. int mode, struct zone *z,
  359. struct mem_cgroup *mem_cont,
  360. int active, int file)
  361. {
  362. unsigned long nr_taken = 0;
  363. struct page *page;
  364. unsigned long scan;
  365. LIST_HEAD(pc_list);
  366. struct list_head *src;
  367. struct page_cgroup *pc, *tmp;
  368. int nid = z->zone_pgdat->node_id;
  369. int zid = zone_idx(z);
  370. struct mem_cgroup_per_zone *mz;
  371. int lru = LRU_FILE * !!file + !!active;
  372. BUG_ON(!mem_cont);
  373. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  374. src = &mz->lists[lru];
  375. spin_lock(&mz->lru_lock);
  376. scan = 0;
  377. list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
  378. if (scan >= nr_to_scan)
  379. break;
  380. if (unlikely(!PageCgroupUsed(pc)))
  381. continue;
  382. page = pc->page;
  383. if (unlikely(!PageLRU(page)))
  384. continue;
  385. /*
  386. * TODO: play better with lumpy reclaim, grabbing anything.
  387. */
  388. if (PageUnevictable(page) ||
  389. (PageActive(page) && !active) ||
  390. (!PageActive(page) && active)) {
  391. __mem_cgroup_move_lists(pc, page_lru(page));
  392. continue;
  393. }
  394. scan++;
  395. list_move(&pc->lru, &pc_list);
  396. if (__isolate_lru_page(page, mode, file) == 0) {
  397. list_move(&page->lru, dst);
  398. nr_taken++;
  399. }
  400. }
  401. list_splice(&pc_list, src);
  402. spin_unlock(&mz->lru_lock);
  403. *scanned = scan;
  404. return nr_taken;
  405. }
  406. /*
  407. * Charge the memory controller for page usage.
  408. * Return
  409. * 0 if the charge was successful
  410. * < 0 if the cgroup is over its limit
  411. */
  412. static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
  413. gfp_t gfp_mask, enum charge_type ctype,
  414. struct mem_cgroup *memcg)
  415. {
  416. struct mem_cgroup *mem;
  417. struct page_cgroup *pc;
  418. unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  419. struct mem_cgroup_per_zone *mz;
  420. unsigned long flags;
  421. pc = lookup_page_cgroup(page);
  422. /* can happen at boot */
  423. if (unlikely(!pc))
  424. return 0;
  425. prefetchw(pc);
  426. /*
  427. * We always charge the cgroup the mm_struct belongs to.
  428. * The mm_struct's mem_cgroup changes on task migration if the
  429. * thread group leader migrates. It's possible that mm is not
  430. * set, if so charge the init_mm (happens for pagecache usage).
  431. */
  432. if (likely(!memcg)) {
  433. rcu_read_lock();
  434. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  435. if (unlikely(!mem)) {
  436. rcu_read_unlock();
  437. return 0;
  438. }
  439. /*
  440. * For every charge from the cgroup, increment reference count
  441. */
  442. css_get(&mem->css);
  443. rcu_read_unlock();
  444. } else {
  445. mem = memcg;
  446. css_get(&memcg->css);
  447. }
  448. while (unlikely(res_counter_charge(&mem->res, PAGE_SIZE))) {
  449. if (!(gfp_mask & __GFP_WAIT))
  450. goto out;
  451. if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
  452. continue;
  453. /*
  454. * try_to_free_mem_cgroup_pages() might not give us a full
  455. * picture of reclaim. Some pages are reclaimed and might be
  456. * moved to swap cache or just unmapped from the cgroup.
  457. * Check the limit again to see if the reclaim reduced the
  458. * current usage of the cgroup before giving up
  459. */
  460. if (res_counter_check_under_limit(&mem->res))
  461. continue;
  462. if (!nr_retries--) {
  463. mem_cgroup_out_of_memory(mem, gfp_mask);
  464. goto out;
  465. }
  466. }
  467. lock_page_cgroup(pc);
  468. if (unlikely(PageCgroupUsed(pc))) {
  469. unlock_page_cgroup(pc);
  470. res_counter_uncharge(&mem->res, PAGE_SIZE);
  471. css_put(&mem->css);
  472. goto done;
  473. }
  474. pc->mem_cgroup = mem;
  475. /*
  476. * If a page is accounted as a page cache, insert to inactive list.
  477. * If anon, insert to active list.
  478. */
  479. pc->flags = pcg_default_flags[ctype];
  480. mz = page_cgroup_zoneinfo(pc);
  481. spin_lock_irqsave(&mz->lru_lock, flags);
  482. __mem_cgroup_add_list(mz, pc);
  483. spin_unlock_irqrestore(&mz->lru_lock, flags);
  484. unlock_page_cgroup(pc);
  485. done:
  486. return 0;
  487. out:
  488. css_put(&mem->css);
  489. return -ENOMEM;
  490. }
  491. int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
  492. {
  493. if (mem_cgroup_subsys.disabled)
  494. return 0;
  495. if (PageCompound(page))
  496. return 0;
  497. /*
  498. * If already mapped, we don't have to account.
  499. * If page cache, page->mapping has address_space.
  500. * But page->mapping may have out-of-use anon_vma pointer,
  501. * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
  502. * is NULL.
  503. */
  504. if (page_mapped(page) || (page->mapping && !PageAnon(page)))
  505. return 0;
  506. if (unlikely(!mm))
  507. mm = &init_mm;
  508. return mem_cgroup_charge_common(page, mm, gfp_mask,
  509. MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
  510. }
  511. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  512. gfp_t gfp_mask)
  513. {
  514. if (mem_cgroup_subsys.disabled)
  515. return 0;
  516. if (PageCompound(page))
  517. return 0;
  518. /*
  519. * Corner case handling. This is called from add_to_page_cache()
  520. * in usual. But some FS (shmem) precharges this page before calling it
  521. * and call add_to_page_cache() with GFP_NOWAIT.
  522. *
  523. * For GFP_NOWAIT case, the page may be pre-charged before calling
  524. * add_to_page_cache(). (See shmem.c) check it here and avoid to call
  525. * charge twice. (It works but has to pay a bit larger cost.)
  526. */
  527. if (!(gfp_mask & __GFP_WAIT)) {
  528. struct page_cgroup *pc;
  529. pc = lookup_page_cgroup(page);
  530. if (!pc)
  531. return 0;
  532. lock_page_cgroup(pc);
  533. if (PageCgroupUsed(pc)) {
  534. unlock_page_cgroup(pc);
  535. return 0;
  536. }
  537. unlock_page_cgroup(pc);
  538. }
  539. if (unlikely(!mm))
  540. mm = &init_mm;
  541. if (page_is_file_cache(page))
  542. return mem_cgroup_charge_common(page, mm, gfp_mask,
  543. MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
  544. else
  545. return mem_cgroup_charge_common(page, mm, gfp_mask,
  546. MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
  547. }
  548. /*
  549. * uncharge if !page_mapped(page)
  550. */
  551. static void
  552. __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
  553. {
  554. struct page_cgroup *pc;
  555. struct mem_cgroup *mem;
  556. struct mem_cgroup_per_zone *mz;
  557. unsigned long flags;
  558. if (mem_cgroup_subsys.disabled)
  559. return;
  560. /*
  561. * Check if our page_cgroup is valid
  562. */
  563. pc = lookup_page_cgroup(page);
  564. if (unlikely(!pc || !PageCgroupUsed(pc)))
  565. return;
  566. lock_page_cgroup(pc);
  567. if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED && page_mapped(page))
  568. || !PageCgroupUsed(pc)) {
  569. /* This happens at race in zap_pte_range() and do_swap_page()*/
  570. unlock_page_cgroup(pc);
  571. return;
  572. }
  573. ClearPageCgroupUsed(pc);
  574. mem = pc->mem_cgroup;
  575. mz = page_cgroup_zoneinfo(pc);
  576. spin_lock_irqsave(&mz->lru_lock, flags);
  577. __mem_cgroup_remove_list(mz, pc);
  578. spin_unlock_irqrestore(&mz->lru_lock, flags);
  579. unlock_page_cgroup(pc);
  580. res_counter_uncharge(&mem->res, PAGE_SIZE);
  581. css_put(&mem->css);
  582. return;
  583. }
  584. void mem_cgroup_uncharge_page(struct page *page)
  585. {
  586. /* early check. */
  587. if (page_mapped(page))
  588. return;
  589. if (page->mapping && !PageAnon(page))
  590. return;
  591. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
  592. }
  593. void mem_cgroup_uncharge_cache_page(struct page *page)
  594. {
  595. VM_BUG_ON(page_mapped(page));
  596. VM_BUG_ON(page->mapping);
  597. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
  598. }
  599. /*
  600. * Before starting migration, account against new page.
  601. */
  602. int mem_cgroup_prepare_migration(struct page *page, struct page *newpage)
  603. {
  604. struct page_cgroup *pc;
  605. struct mem_cgroup *mem = NULL;
  606. enum charge_type ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
  607. int ret = 0;
  608. if (mem_cgroup_subsys.disabled)
  609. return 0;
  610. pc = lookup_page_cgroup(page);
  611. lock_page_cgroup(pc);
  612. if (PageCgroupUsed(pc)) {
  613. mem = pc->mem_cgroup;
  614. css_get(&mem->css);
  615. if (PageCgroupCache(pc)) {
  616. if (page_is_file_cache(page))
  617. ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
  618. else
  619. ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
  620. }
  621. }
  622. unlock_page_cgroup(pc);
  623. if (mem) {
  624. ret = mem_cgroup_charge_common(newpage, NULL, GFP_KERNEL,
  625. ctype, mem);
  626. css_put(&mem->css);
  627. }
  628. return ret;
  629. }
  630. /* remove redundant charge if migration failed*/
  631. void mem_cgroup_end_migration(struct page *newpage)
  632. {
  633. /*
  634. * At success, page->mapping is not NULL.
  635. * special rollback care is necessary when
  636. * 1. at migration failure. (newpage->mapping is cleared in this case)
  637. * 2. the newpage was moved but not remapped again because the task
  638. * exits and the newpage is obsolete. In this case, the new page
  639. * may be a swapcache. So, we just call mem_cgroup_uncharge_page()
  640. * always for avoiding mess. The page_cgroup will be removed if
  641. * unnecessary. File cache pages is still on radix-tree. Don't
  642. * care it.
  643. */
  644. if (!newpage->mapping)
  645. __mem_cgroup_uncharge_common(newpage,
  646. MEM_CGROUP_CHARGE_TYPE_FORCE);
  647. else if (PageAnon(newpage))
  648. mem_cgroup_uncharge_page(newpage);
  649. }
  650. /*
  651. * A call to try to shrink memory usage under specified resource controller.
  652. * This is typically used for page reclaiming for shmem for reducing side
  653. * effect of page allocation from shmem, which is used by some mem_cgroup.
  654. */
  655. int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
  656. {
  657. struct mem_cgroup *mem;
  658. int progress = 0;
  659. int retry = MEM_CGROUP_RECLAIM_RETRIES;
  660. if (mem_cgroup_subsys.disabled)
  661. return 0;
  662. if (!mm)
  663. return 0;
  664. rcu_read_lock();
  665. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  666. if (unlikely(!mem)) {
  667. rcu_read_unlock();
  668. return 0;
  669. }
  670. css_get(&mem->css);
  671. rcu_read_unlock();
  672. do {
  673. progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
  674. progress += res_counter_check_under_limit(&mem->res);
  675. } while (!progress && --retry);
  676. css_put(&mem->css);
  677. if (!retry)
  678. return -ENOMEM;
  679. return 0;
  680. }
  681. int mem_cgroup_resize_limit(struct mem_cgroup *memcg, unsigned long long val)
  682. {
  683. int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
  684. int progress;
  685. int ret = 0;
  686. while (res_counter_set_limit(&memcg->res, val)) {
  687. if (signal_pending(current)) {
  688. ret = -EINTR;
  689. break;
  690. }
  691. if (!retry_count) {
  692. ret = -EBUSY;
  693. break;
  694. }
  695. progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL);
  696. if (!progress)
  697. retry_count--;
  698. }
  699. return ret;
  700. }
  701. /*
  702. * This routine traverse page_cgroup in given list and drop them all.
  703. * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  704. */
  705. #define FORCE_UNCHARGE_BATCH (128)
  706. static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
  707. struct mem_cgroup_per_zone *mz,
  708. enum lru_list lru)
  709. {
  710. struct page_cgroup *pc;
  711. struct page *page;
  712. int count = FORCE_UNCHARGE_BATCH;
  713. unsigned long flags;
  714. struct list_head *list;
  715. list = &mz->lists[lru];
  716. spin_lock_irqsave(&mz->lru_lock, flags);
  717. while (!list_empty(list)) {
  718. pc = list_entry(list->prev, struct page_cgroup, lru);
  719. page = pc->page;
  720. if (!PageCgroupUsed(pc))
  721. break;
  722. get_page(page);
  723. spin_unlock_irqrestore(&mz->lru_lock, flags);
  724. /*
  725. * Check if this page is on LRU. !LRU page can be found
  726. * if it's under page migration.
  727. */
  728. if (PageLRU(page)) {
  729. __mem_cgroup_uncharge_common(page,
  730. MEM_CGROUP_CHARGE_TYPE_FORCE);
  731. put_page(page);
  732. if (--count <= 0) {
  733. count = FORCE_UNCHARGE_BATCH;
  734. cond_resched();
  735. }
  736. } else {
  737. spin_lock_irqsave(&mz->lru_lock, flags);
  738. break;
  739. }
  740. spin_lock_irqsave(&mz->lru_lock, flags);
  741. }
  742. spin_unlock_irqrestore(&mz->lru_lock, flags);
  743. }
  744. /*
  745. * make mem_cgroup's charge to be 0 if there is no task.
  746. * This enables deleting this mem_cgroup.
  747. */
  748. static int mem_cgroup_force_empty(struct mem_cgroup *mem)
  749. {
  750. int ret = -EBUSY;
  751. int node, zid;
  752. css_get(&mem->css);
  753. /*
  754. * page reclaim code (kswapd etc..) will move pages between
  755. * active_list <-> inactive_list while we don't take a lock.
  756. * So, we have to do loop here until all lists are empty.
  757. */
  758. while (mem->res.usage > 0) {
  759. if (atomic_read(&mem->css.cgroup->count) > 0)
  760. goto out;
  761. /* This is for making all *used* pages to be on LRU. */
  762. lru_add_drain_all();
  763. for_each_node_state(node, N_POSSIBLE)
  764. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  765. struct mem_cgroup_per_zone *mz;
  766. enum lru_list l;
  767. mz = mem_cgroup_zoneinfo(mem, node, zid);
  768. for_each_lru(l)
  769. mem_cgroup_force_empty_list(mem, mz, l);
  770. }
  771. cond_resched();
  772. }
  773. ret = 0;
  774. out:
  775. css_put(&mem->css);
  776. return ret;
  777. }
  778. static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
  779. {
  780. return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
  781. cft->private);
  782. }
  783. /*
  784. * The user of this function is...
  785. * RES_LIMIT.
  786. */
  787. static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  788. const char *buffer)
  789. {
  790. struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
  791. unsigned long long val;
  792. int ret;
  793. switch (cft->private) {
  794. case RES_LIMIT:
  795. /* This function does all necessary parse...reuse it */
  796. ret = res_counter_memparse_write_strategy(buffer, &val);
  797. if (!ret)
  798. ret = mem_cgroup_resize_limit(memcg, val);
  799. break;
  800. default:
  801. ret = -EINVAL; /* should be BUG() ? */
  802. break;
  803. }
  804. return ret;
  805. }
  806. static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
  807. {
  808. struct mem_cgroup *mem;
  809. mem = mem_cgroup_from_cont(cont);
  810. switch (event) {
  811. case RES_MAX_USAGE:
  812. res_counter_reset_max(&mem->res);
  813. break;
  814. case RES_FAILCNT:
  815. res_counter_reset_failcnt(&mem->res);
  816. break;
  817. }
  818. return 0;
  819. }
  820. static int mem_force_empty_write(struct cgroup *cont, unsigned int event)
  821. {
  822. return mem_cgroup_force_empty(mem_cgroup_from_cont(cont));
  823. }
  824. static const struct mem_cgroup_stat_desc {
  825. const char *msg;
  826. u64 unit;
  827. } mem_cgroup_stat_desc[] = {
  828. [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
  829. [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
  830. [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
  831. [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
  832. };
  833. static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
  834. struct cgroup_map_cb *cb)
  835. {
  836. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  837. struct mem_cgroup_stat *stat = &mem_cont->stat;
  838. int i;
  839. for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
  840. s64 val;
  841. val = mem_cgroup_read_stat(stat, i);
  842. val *= mem_cgroup_stat_desc[i].unit;
  843. cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
  844. }
  845. /* showing # of active pages */
  846. {
  847. unsigned long active_anon, inactive_anon;
  848. unsigned long active_file, inactive_file;
  849. unsigned long unevictable;
  850. inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
  851. LRU_INACTIVE_ANON);
  852. active_anon = mem_cgroup_get_all_zonestat(mem_cont,
  853. LRU_ACTIVE_ANON);
  854. inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
  855. LRU_INACTIVE_FILE);
  856. active_file = mem_cgroup_get_all_zonestat(mem_cont,
  857. LRU_ACTIVE_FILE);
  858. unevictable = mem_cgroup_get_all_zonestat(mem_cont,
  859. LRU_UNEVICTABLE);
  860. cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
  861. cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
  862. cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
  863. cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
  864. cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
  865. }
  866. return 0;
  867. }
  868. static struct cftype mem_cgroup_files[] = {
  869. {
  870. .name = "usage_in_bytes",
  871. .private = RES_USAGE,
  872. .read_u64 = mem_cgroup_read,
  873. },
  874. {
  875. .name = "max_usage_in_bytes",
  876. .private = RES_MAX_USAGE,
  877. .trigger = mem_cgroup_reset,
  878. .read_u64 = mem_cgroup_read,
  879. },
  880. {
  881. .name = "limit_in_bytes",
  882. .private = RES_LIMIT,
  883. .write_string = mem_cgroup_write,
  884. .read_u64 = mem_cgroup_read,
  885. },
  886. {
  887. .name = "failcnt",
  888. .private = RES_FAILCNT,
  889. .trigger = mem_cgroup_reset,
  890. .read_u64 = mem_cgroup_read,
  891. },
  892. {
  893. .name = "force_empty",
  894. .trigger = mem_force_empty_write,
  895. },
  896. {
  897. .name = "stat",
  898. .read_map = mem_control_stat_show,
  899. },
  900. };
  901. static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  902. {
  903. struct mem_cgroup_per_node *pn;
  904. struct mem_cgroup_per_zone *mz;
  905. enum lru_list l;
  906. int zone, tmp = node;
  907. /*
  908. * This routine is called against possible nodes.
  909. * But it's BUG to call kmalloc() against offline node.
  910. *
  911. * TODO: this routine can waste much memory for nodes which will
  912. * never be onlined. It's better to use memory hotplug callback
  913. * function.
  914. */
  915. if (!node_state(node, N_NORMAL_MEMORY))
  916. tmp = -1;
  917. pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
  918. if (!pn)
  919. return 1;
  920. mem->info.nodeinfo[node] = pn;
  921. memset(pn, 0, sizeof(*pn));
  922. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  923. mz = &pn->zoneinfo[zone];
  924. spin_lock_init(&mz->lru_lock);
  925. for_each_lru(l)
  926. INIT_LIST_HEAD(&mz->lists[l]);
  927. }
  928. return 0;
  929. }
  930. static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  931. {
  932. kfree(mem->info.nodeinfo[node]);
  933. }
  934. static struct mem_cgroup *mem_cgroup_alloc(void)
  935. {
  936. struct mem_cgroup *mem;
  937. if (sizeof(*mem) < PAGE_SIZE)
  938. mem = kmalloc(sizeof(*mem), GFP_KERNEL);
  939. else
  940. mem = vmalloc(sizeof(*mem));
  941. if (mem)
  942. memset(mem, 0, sizeof(*mem));
  943. return mem;
  944. }
  945. static void mem_cgroup_free(struct mem_cgroup *mem)
  946. {
  947. if (sizeof(*mem) < PAGE_SIZE)
  948. kfree(mem);
  949. else
  950. vfree(mem);
  951. }
  952. static struct cgroup_subsys_state *
  953. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  954. {
  955. struct mem_cgroup *mem;
  956. int node;
  957. if (unlikely((cont->parent) == NULL)) {
  958. mem = &init_mem_cgroup;
  959. } else {
  960. mem = mem_cgroup_alloc();
  961. if (!mem)
  962. return ERR_PTR(-ENOMEM);
  963. }
  964. res_counter_init(&mem->res);
  965. for_each_node_state(node, N_POSSIBLE)
  966. if (alloc_mem_cgroup_per_zone_info(mem, node))
  967. goto free_out;
  968. return &mem->css;
  969. free_out:
  970. for_each_node_state(node, N_POSSIBLE)
  971. free_mem_cgroup_per_zone_info(mem, node);
  972. if (cont->parent != NULL)
  973. mem_cgroup_free(mem);
  974. return ERR_PTR(-ENOMEM);
  975. }
  976. static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
  977. struct cgroup *cont)
  978. {
  979. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  980. mem_cgroup_force_empty(mem);
  981. }
  982. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  983. struct cgroup *cont)
  984. {
  985. int node;
  986. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  987. for_each_node_state(node, N_POSSIBLE)
  988. free_mem_cgroup_per_zone_info(mem, node);
  989. mem_cgroup_free(mem_cgroup_from_cont(cont));
  990. }
  991. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  992. struct cgroup *cont)
  993. {
  994. return cgroup_add_files(cont, ss, mem_cgroup_files,
  995. ARRAY_SIZE(mem_cgroup_files));
  996. }
  997. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  998. struct cgroup *cont,
  999. struct cgroup *old_cont,
  1000. struct task_struct *p)
  1001. {
  1002. struct mm_struct *mm;
  1003. struct mem_cgroup *mem, *old_mem;
  1004. mm = get_task_mm(p);
  1005. if (mm == NULL)
  1006. return;
  1007. mem = mem_cgroup_from_cont(cont);
  1008. old_mem = mem_cgroup_from_cont(old_cont);
  1009. /*
  1010. * Only thread group leaders are allowed to migrate, the mm_struct is
  1011. * in effect owned by the leader
  1012. */
  1013. if (!thread_group_leader(p))
  1014. goto out;
  1015. out:
  1016. mmput(mm);
  1017. }
  1018. struct cgroup_subsys mem_cgroup_subsys = {
  1019. .name = "memory",
  1020. .subsys_id = mem_cgroup_subsys_id,
  1021. .create = mem_cgroup_create,
  1022. .pre_destroy = mem_cgroup_pre_destroy,
  1023. .destroy = mem_cgroup_destroy,
  1024. .populate = mem_cgroup_populate,
  1025. .attach = mem_cgroup_move_task,
  1026. .early_init = 0,
  1027. };