memcontrol.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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 <asm/uaccess.h>
  35. struct cgroup_subsys mem_cgroup_subsys __read_mostly;
  36. static struct kmem_cache *page_cgroup_cache __read_mostly;
  37. #define MEM_CGROUP_RECLAIM_RETRIES 5
  38. /*
  39. * Statistics for memory cgroup.
  40. */
  41. enum mem_cgroup_stat_index {
  42. /*
  43. * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
  44. */
  45. MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
  46. MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
  47. MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
  48. MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
  49. MEM_CGROUP_STAT_NSTATS,
  50. };
  51. struct mem_cgroup_stat_cpu {
  52. s64 count[MEM_CGROUP_STAT_NSTATS];
  53. } ____cacheline_aligned_in_smp;
  54. struct mem_cgroup_stat {
  55. struct mem_cgroup_stat_cpu cpustat[NR_CPUS];
  56. };
  57. /*
  58. * For accounting under irq disable, no need for increment preempt count.
  59. */
  60. static void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat *stat,
  61. enum mem_cgroup_stat_index idx, int val)
  62. {
  63. int cpu = smp_processor_id();
  64. stat->cpustat[cpu].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. enum mem_cgroup_zstat_index {
  79. MEM_CGROUP_ZSTAT_ACTIVE,
  80. MEM_CGROUP_ZSTAT_INACTIVE,
  81. NR_MEM_CGROUP_ZSTAT,
  82. };
  83. struct mem_cgroup_per_zone {
  84. /*
  85. * spin_lock to protect the per cgroup LRU
  86. */
  87. spinlock_t lru_lock;
  88. struct list_head active_list;
  89. struct list_head inactive_list;
  90. unsigned long count[NR_MEM_CGROUP_ZSTAT];
  91. };
  92. /* Macro for accessing counter */
  93. #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
  94. struct mem_cgroup_per_node {
  95. struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
  96. };
  97. struct mem_cgroup_lru_info {
  98. struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
  99. };
  100. /*
  101. * The memory controller data structure. The memory controller controls both
  102. * page cache and RSS per cgroup. We would eventually like to provide
  103. * statistics based on the statistics developed by Rik Van Riel for clock-pro,
  104. * to help the administrator determine what knobs to tune.
  105. *
  106. * TODO: Add a water mark for the memory controller. Reclaim will begin when
  107. * we hit the water mark. May be even add a low water mark, such that
  108. * no reclaim occurs from a cgroup at it's low water mark, this is
  109. * a feature that will be implemented much later in the future.
  110. */
  111. struct mem_cgroup {
  112. struct cgroup_subsys_state css;
  113. /*
  114. * the counter to account for memory usage
  115. */
  116. struct res_counter res;
  117. /*
  118. * Per cgroup active and inactive list, similar to the
  119. * per zone LRU lists.
  120. */
  121. struct mem_cgroup_lru_info info;
  122. int prev_priority; /* for recording reclaim priority */
  123. /*
  124. * statistics.
  125. */
  126. struct mem_cgroup_stat stat;
  127. };
  128. static struct mem_cgroup init_mem_cgroup;
  129. /*
  130. * We use the lower bit of the page->page_cgroup pointer as a bit spin
  131. * lock. We need to ensure that page->page_cgroup is at least two
  132. * byte aligned (based on comments from Nick Piggin). But since
  133. * bit_spin_lock doesn't actually set that lock bit in a non-debug
  134. * uniprocessor kernel, we should avoid setting it here too.
  135. */
  136. #define PAGE_CGROUP_LOCK_BIT 0x0
  137. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  138. #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
  139. #else
  140. #define PAGE_CGROUP_LOCK 0x0
  141. #endif
  142. /*
  143. * A page_cgroup page is associated with every page descriptor. The
  144. * page_cgroup helps us identify information about the cgroup
  145. */
  146. struct page_cgroup {
  147. struct list_head lru; /* per cgroup LRU list */
  148. struct page *page;
  149. struct mem_cgroup *mem_cgroup;
  150. int flags;
  151. };
  152. #define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
  153. #define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */
  154. static int page_cgroup_nid(struct page_cgroup *pc)
  155. {
  156. return page_to_nid(pc->page);
  157. }
  158. static enum zone_type page_cgroup_zid(struct page_cgroup *pc)
  159. {
  160. return page_zonenum(pc->page);
  161. }
  162. enum charge_type {
  163. MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
  164. MEM_CGROUP_CHARGE_TYPE_MAPPED,
  165. MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
  166. };
  167. /*
  168. * Always modified under lru lock. Then, not necessary to preempt_disable()
  169. */
  170. static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags,
  171. bool charge)
  172. {
  173. int val = (charge)? 1 : -1;
  174. struct mem_cgroup_stat *stat = &mem->stat;
  175. VM_BUG_ON(!irqs_disabled());
  176. if (flags & PAGE_CGROUP_FLAG_CACHE)
  177. __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_CACHE, val);
  178. else
  179. __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
  180. if (charge)
  181. __mem_cgroup_stat_add_safe(stat,
  182. MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
  183. else
  184. __mem_cgroup_stat_add_safe(stat,
  185. MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
  186. }
  187. static struct mem_cgroup_per_zone *
  188. mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
  189. {
  190. return &mem->info.nodeinfo[nid]->zoneinfo[zid];
  191. }
  192. static struct mem_cgroup_per_zone *
  193. page_cgroup_zoneinfo(struct page_cgroup *pc)
  194. {
  195. struct mem_cgroup *mem = pc->mem_cgroup;
  196. int nid = page_cgroup_nid(pc);
  197. int zid = page_cgroup_zid(pc);
  198. return mem_cgroup_zoneinfo(mem, nid, zid);
  199. }
  200. static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
  201. enum mem_cgroup_zstat_index idx)
  202. {
  203. int nid, zid;
  204. struct mem_cgroup_per_zone *mz;
  205. u64 total = 0;
  206. for_each_online_node(nid)
  207. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  208. mz = mem_cgroup_zoneinfo(mem, nid, zid);
  209. total += MEM_CGROUP_ZSTAT(mz, idx);
  210. }
  211. return total;
  212. }
  213. static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
  214. {
  215. return container_of(cgroup_subsys_state(cont,
  216. mem_cgroup_subsys_id), struct mem_cgroup,
  217. css);
  218. }
  219. struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
  220. {
  221. return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
  222. struct mem_cgroup, css);
  223. }
  224. static inline int page_cgroup_locked(struct page *page)
  225. {
  226. return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  227. }
  228. static void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
  229. {
  230. VM_BUG_ON(!page_cgroup_locked(page));
  231. page->page_cgroup = ((unsigned long)pc | PAGE_CGROUP_LOCK);
  232. }
  233. struct page_cgroup *page_get_page_cgroup(struct page *page)
  234. {
  235. return (struct page_cgroup *) (page->page_cgroup & ~PAGE_CGROUP_LOCK);
  236. }
  237. static void lock_page_cgroup(struct page *page)
  238. {
  239. bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  240. }
  241. static int try_lock_page_cgroup(struct page *page)
  242. {
  243. return bit_spin_trylock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  244. }
  245. static void unlock_page_cgroup(struct page *page)
  246. {
  247. bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  248. }
  249. static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
  250. struct page_cgroup *pc)
  251. {
  252. int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
  253. if (from)
  254. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1;
  255. else
  256. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1;
  257. mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false);
  258. list_del(&pc->lru);
  259. }
  260. static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
  261. struct page_cgroup *pc)
  262. {
  263. int to = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
  264. if (!to) {
  265. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
  266. list_add(&pc->lru, &mz->inactive_list);
  267. } else {
  268. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
  269. list_add(&pc->lru, &mz->active_list);
  270. }
  271. mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true);
  272. }
  273. static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
  274. {
  275. int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
  276. struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
  277. if (from)
  278. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1;
  279. else
  280. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1;
  281. if (active) {
  282. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
  283. pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
  284. list_move(&pc->lru, &mz->active_list);
  285. } else {
  286. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
  287. pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
  288. list_move(&pc->lru, &mz->inactive_list);
  289. }
  290. }
  291. int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
  292. {
  293. int ret;
  294. task_lock(task);
  295. ret = task->mm && mm_match_cgroup(task->mm, mem);
  296. task_unlock(task);
  297. return ret;
  298. }
  299. /*
  300. * This routine assumes that the appropriate zone's lru lock is already held
  301. */
  302. void mem_cgroup_move_lists(struct page *page, bool active)
  303. {
  304. struct page_cgroup *pc;
  305. struct mem_cgroup_per_zone *mz;
  306. unsigned long flags;
  307. /*
  308. * We cannot lock_page_cgroup while holding zone's lru_lock,
  309. * because other holders of lock_page_cgroup can be interrupted
  310. * with an attempt to rotate_reclaimable_page. But we cannot
  311. * safely get to page_cgroup without it, so just try_lock it:
  312. * mem_cgroup_isolate_pages allows for page left on wrong list.
  313. */
  314. if (!try_lock_page_cgroup(page))
  315. return;
  316. pc = page_get_page_cgroup(page);
  317. if (pc) {
  318. mz = page_cgroup_zoneinfo(pc);
  319. spin_lock_irqsave(&mz->lru_lock, flags);
  320. __mem_cgroup_move_lists(pc, active);
  321. spin_unlock_irqrestore(&mz->lru_lock, flags);
  322. }
  323. unlock_page_cgroup(page);
  324. }
  325. /*
  326. * Calculate mapped_ratio under memory controller. This will be used in
  327. * vmscan.c for deteremining we have to reclaim mapped pages.
  328. */
  329. int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
  330. {
  331. long total, rss;
  332. /*
  333. * usage is recorded in bytes. But, here, we assume the number of
  334. * physical pages can be represented by "long" on any arch.
  335. */
  336. total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
  337. rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
  338. return (int)((rss * 100L) / total);
  339. }
  340. /*
  341. * This function is called from vmscan.c. In page reclaiming loop. balance
  342. * between active and inactive list is calculated. For memory controller
  343. * page reclaiming, we should use using mem_cgroup's imbalance rather than
  344. * zone's global lru imbalance.
  345. */
  346. long mem_cgroup_reclaim_imbalance(struct mem_cgroup *mem)
  347. {
  348. unsigned long active, inactive;
  349. /* active and inactive are the number of pages. 'long' is ok.*/
  350. active = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_ACTIVE);
  351. inactive = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_INACTIVE);
  352. return (long) (active / (inactive + 1));
  353. }
  354. /*
  355. * prev_priority control...this will be used in memory reclaim path.
  356. */
  357. int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
  358. {
  359. return mem->prev_priority;
  360. }
  361. void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
  362. {
  363. if (priority < mem->prev_priority)
  364. mem->prev_priority = priority;
  365. }
  366. void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
  367. {
  368. mem->prev_priority = priority;
  369. }
  370. /*
  371. * Calculate # of pages to be scanned in this priority/zone.
  372. * See also vmscan.c
  373. *
  374. * priority starts from "DEF_PRIORITY" and decremented in each loop.
  375. * (see include/linux/mmzone.h)
  376. */
  377. long mem_cgroup_calc_reclaim_active(struct mem_cgroup *mem,
  378. struct zone *zone, int priority)
  379. {
  380. long nr_active;
  381. int nid = zone->zone_pgdat->node_id;
  382. int zid = zone_idx(zone);
  383. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
  384. nr_active = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE);
  385. return (nr_active >> priority);
  386. }
  387. long mem_cgroup_calc_reclaim_inactive(struct mem_cgroup *mem,
  388. struct zone *zone, int priority)
  389. {
  390. long nr_inactive;
  391. int nid = zone->zone_pgdat->node_id;
  392. int zid = zone_idx(zone);
  393. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
  394. nr_inactive = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE);
  395. return (nr_inactive >> priority);
  396. }
  397. unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
  398. struct list_head *dst,
  399. unsigned long *scanned, int order,
  400. int mode, struct zone *z,
  401. struct mem_cgroup *mem_cont,
  402. int active)
  403. {
  404. unsigned long nr_taken = 0;
  405. struct page *page;
  406. unsigned long scan;
  407. LIST_HEAD(pc_list);
  408. struct list_head *src;
  409. struct page_cgroup *pc, *tmp;
  410. int nid = z->zone_pgdat->node_id;
  411. int zid = zone_idx(z);
  412. struct mem_cgroup_per_zone *mz;
  413. BUG_ON(!mem_cont);
  414. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  415. if (active)
  416. src = &mz->active_list;
  417. else
  418. src = &mz->inactive_list;
  419. spin_lock(&mz->lru_lock);
  420. scan = 0;
  421. list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
  422. if (scan >= nr_to_scan)
  423. break;
  424. page = pc->page;
  425. if (unlikely(!PageLRU(page)))
  426. continue;
  427. if (PageActive(page) && !active) {
  428. __mem_cgroup_move_lists(pc, true);
  429. continue;
  430. }
  431. if (!PageActive(page) && active) {
  432. __mem_cgroup_move_lists(pc, false);
  433. continue;
  434. }
  435. scan++;
  436. list_move(&pc->lru, &pc_list);
  437. if (__isolate_lru_page(page, mode) == 0) {
  438. list_move(&page->lru, dst);
  439. nr_taken++;
  440. }
  441. }
  442. list_splice(&pc_list, src);
  443. spin_unlock(&mz->lru_lock);
  444. *scanned = scan;
  445. return nr_taken;
  446. }
  447. /*
  448. * Charge the memory controller for page usage.
  449. * Return
  450. * 0 if the charge was successful
  451. * < 0 if the cgroup is over its limit
  452. */
  453. static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
  454. gfp_t gfp_mask, enum charge_type ctype,
  455. struct mem_cgroup *memcg)
  456. {
  457. struct mem_cgroup *mem;
  458. struct page_cgroup *pc;
  459. unsigned long flags;
  460. unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  461. struct mem_cgroup_per_zone *mz;
  462. if (mem_cgroup_subsys.disabled)
  463. return 0;
  464. /*
  465. * Should page_cgroup's go to their own slab?
  466. * One could optimize the performance of the charging routine
  467. * by saving a bit in the page_flags and using it as a lock
  468. * to see if the cgroup page already has a page_cgroup associated
  469. * with it
  470. */
  471. retry:
  472. lock_page_cgroup(page);
  473. pc = page_get_page_cgroup(page);
  474. /*
  475. * The page_cgroup exists and
  476. * the page has already been accounted.
  477. */
  478. if (unlikely(pc)) {
  479. VM_BUG_ON(pc->page != page);
  480. VM_BUG_ON(!pc->mem_cgroup);
  481. unlock_page_cgroup(page);
  482. goto done;
  483. }
  484. unlock_page_cgroup(page);
  485. pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask);
  486. if (unlikely(pc == NULL))
  487. goto err;
  488. /*
  489. * We always charge the cgroup the mm_struct belongs to.
  490. * The mm_struct's mem_cgroup changes on task migration if the
  491. * thread group leader migrates. It's possible that mm is not
  492. * set, if so charge the init_mm (happens for pagecache usage).
  493. */
  494. if (likely(!memcg)) {
  495. rcu_read_lock();
  496. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  497. /*
  498. * For every charge from the cgroup, increment reference count
  499. */
  500. css_get(&mem->css);
  501. rcu_read_unlock();
  502. } else {
  503. mem = memcg;
  504. css_get(&memcg->css);
  505. }
  506. while (res_counter_charge(&mem->res, PAGE_SIZE)) {
  507. if (!(gfp_mask & __GFP_WAIT))
  508. goto out;
  509. if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
  510. continue;
  511. /*
  512. * try_to_free_mem_cgroup_pages() might not give us a full
  513. * picture of reclaim. Some pages are reclaimed and might be
  514. * moved to swap cache or just unmapped from the cgroup.
  515. * Check the limit again to see if the reclaim reduced the
  516. * current usage of the cgroup before giving up
  517. */
  518. if (res_counter_check_under_limit(&mem->res))
  519. continue;
  520. if (!nr_retries--) {
  521. mem_cgroup_out_of_memory(mem, gfp_mask);
  522. goto out;
  523. }
  524. }
  525. pc->mem_cgroup = mem;
  526. pc->page = page;
  527. /*
  528. * If a page is accounted as a page cache, insert to inactive list.
  529. * If anon, insert to active list.
  530. */
  531. if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE)
  532. pc->flags = PAGE_CGROUP_FLAG_CACHE;
  533. else
  534. pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
  535. lock_page_cgroup(page);
  536. if (unlikely(page_get_page_cgroup(page))) {
  537. unlock_page_cgroup(page);
  538. /*
  539. * Another charge has been added to this page already.
  540. * We take lock_page_cgroup(page) again and read
  541. * page->cgroup, increment refcnt.... just retry is OK.
  542. */
  543. res_counter_uncharge(&mem->res, PAGE_SIZE);
  544. css_put(&mem->css);
  545. kmem_cache_free(page_cgroup_cache, pc);
  546. goto retry;
  547. }
  548. page_assign_page_cgroup(page, pc);
  549. mz = page_cgroup_zoneinfo(pc);
  550. spin_lock_irqsave(&mz->lru_lock, flags);
  551. __mem_cgroup_add_list(mz, pc);
  552. spin_unlock_irqrestore(&mz->lru_lock, flags);
  553. unlock_page_cgroup(page);
  554. done:
  555. return 0;
  556. out:
  557. css_put(&mem->css);
  558. kmem_cache_free(page_cgroup_cache, pc);
  559. err:
  560. return -ENOMEM;
  561. }
  562. int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
  563. {
  564. /*
  565. * If already mapped, we don't have to account.
  566. * If page cache, page->mapping has address_space.
  567. * But page->mapping may have out-of-use anon_vma pointer,
  568. * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
  569. * is NULL.
  570. */
  571. if (page_mapped(page) || (page->mapping && !PageAnon(page)))
  572. return 0;
  573. if (unlikely(!mm))
  574. mm = &init_mm;
  575. return mem_cgroup_charge_common(page, mm, gfp_mask,
  576. MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
  577. }
  578. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  579. gfp_t gfp_mask)
  580. {
  581. if (unlikely(!mm))
  582. mm = &init_mm;
  583. return mem_cgroup_charge_common(page, mm, gfp_mask,
  584. MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
  585. }
  586. /*
  587. * uncharge if !page_mapped(page)
  588. */
  589. static void
  590. __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
  591. {
  592. struct page_cgroup *pc;
  593. struct mem_cgroup *mem;
  594. struct mem_cgroup_per_zone *mz;
  595. unsigned long flags;
  596. if (mem_cgroup_subsys.disabled)
  597. return;
  598. /*
  599. * Check if our page_cgroup is valid
  600. */
  601. lock_page_cgroup(page);
  602. pc = page_get_page_cgroup(page);
  603. if (unlikely(!pc))
  604. goto unlock;
  605. VM_BUG_ON(pc->page != page);
  606. if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
  607. && ((pc->flags & PAGE_CGROUP_FLAG_CACHE)
  608. || page_mapped(page)))
  609. goto unlock;
  610. mz = page_cgroup_zoneinfo(pc);
  611. spin_lock_irqsave(&mz->lru_lock, flags);
  612. __mem_cgroup_remove_list(mz, pc);
  613. spin_unlock_irqrestore(&mz->lru_lock, flags);
  614. page_assign_page_cgroup(page, NULL);
  615. unlock_page_cgroup(page);
  616. mem = pc->mem_cgroup;
  617. res_counter_uncharge(&mem->res, PAGE_SIZE);
  618. css_put(&mem->css);
  619. kmem_cache_free(page_cgroup_cache, pc);
  620. return;
  621. unlock:
  622. unlock_page_cgroup(page);
  623. }
  624. void mem_cgroup_uncharge_page(struct page *page)
  625. {
  626. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
  627. }
  628. void mem_cgroup_uncharge_cache_page(struct page *page)
  629. {
  630. VM_BUG_ON(page_mapped(page));
  631. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
  632. }
  633. /*
  634. * Before starting migration, account against new page.
  635. */
  636. int mem_cgroup_prepare_migration(struct page *page, struct page *newpage)
  637. {
  638. struct page_cgroup *pc;
  639. struct mem_cgroup *mem = NULL;
  640. enum charge_type ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
  641. int ret = 0;
  642. if (mem_cgroup_subsys.disabled)
  643. return 0;
  644. lock_page_cgroup(page);
  645. pc = page_get_page_cgroup(page);
  646. if (pc) {
  647. mem = pc->mem_cgroup;
  648. css_get(&mem->css);
  649. if (pc->flags & PAGE_CGROUP_FLAG_CACHE)
  650. ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
  651. }
  652. unlock_page_cgroup(page);
  653. if (mem) {
  654. ret = mem_cgroup_charge_common(newpage, NULL, GFP_KERNEL,
  655. ctype, mem);
  656. css_put(&mem->css);
  657. }
  658. return ret;
  659. }
  660. /* remove redundant charge if migration failed*/
  661. void mem_cgroup_end_migration(struct page *newpage)
  662. {
  663. /*
  664. * At success, page->mapping is not NULL.
  665. * special rollback care is necessary when
  666. * 1. at migration failure. (newpage->mapping is cleared in this case)
  667. * 2. the newpage was moved but not remapped again because the task
  668. * exits and the newpage is obsolete. In this case, the new page
  669. * may be a swapcache. So, we just call mem_cgroup_uncharge_page()
  670. * always for avoiding mess. The page_cgroup will be removed if
  671. * unnecessary. File cache pages is still on radix-tree. Don't
  672. * care it.
  673. */
  674. if (!newpage->mapping)
  675. __mem_cgroup_uncharge_common(newpage,
  676. MEM_CGROUP_CHARGE_TYPE_FORCE);
  677. else if (PageAnon(newpage))
  678. mem_cgroup_uncharge_page(newpage);
  679. }
  680. /*
  681. * A call to try to shrink memory usage under specified resource controller.
  682. * This is typically used for page reclaiming for shmem for reducing side
  683. * effect of page allocation from shmem, which is used by some mem_cgroup.
  684. */
  685. int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
  686. {
  687. struct mem_cgroup *mem;
  688. int progress = 0;
  689. int retry = MEM_CGROUP_RECLAIM_RETRIES;
  690. rcu_read_lock();
  691. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  692. css_get(&mem->css);
  693. rcu_read_unlock();
  694. do {
  695. progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
  696. } while (!progress && --retry);
  697. css_put(&mem->css);
  698. if (!retry)
  699. return -ENOMEM;
  700. return 0;
  701. }
  702. /*
  703. * This routine traverse page_cgroup in given list and drop them all.
  704. * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  705. */
  706. #define FORCE_UNCHARGE_BATCH (128)
  707. static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
  708. struct mem_cgroup_per_zone *mz,
  709. int active)
  710. {
  711. struct page_cgroup *pc;
  712. struct page *page;
  713. int count = FORCE_UNCHARGE_BATCH;
  714. unsigned long flags;
  715. struct list_head *list;
  716. if (active)
  717. list = &mz->active_list;
  718. else
  719. list = &mz->inactive_list;
  720. spin_lock_irqsave(&mz->lru_lock, flags);
  721. while (!list_empty(list)) {
  722. pc = list_entry(list->prev, struct page_cgroup, lru);
  723. page = pc->page;
  724. get_page(page);
  725. spin_unlock_irqrestore(&mz->lru_lock, flags);
  726. /*
  727. * Check if this page is on LRU. !LRU page can be found
  728. * if it's under page migration.
  729. */
  730. if (PageLRU(page)) {
  731. __mem_cgroup_uncharge_common(page,
  732. MEM_CGROUP_CHARGE_TYPE_FORCE);
  733. put_page(page);
  734. if (--count <= 0) {
  735. count = FORCE_UNCHARGE_BATCH;
  736. cond_resched();
  737. }
  738. } else
  739. cond_resched();
  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. if (mem_cgroup_subsys.disabled)
  753. return 0;
  754. css_get(&mem->css);
  755. /*
  756. * page reclaim code (kswapd etc..) will move pages between
  757. * active_list <-> inactive_list while we don't take a lock.
  758. * So, we have to do loop here until all lists are empty.
  759. */
  760. while (mem->res.usage > 0) {
  761. if (atomic_read(&mem->css.cgroup->count) > 0)
  762. goto out;
  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. mz = mem_cgroup_zoneinfo(mem, node, zid);
  767. /* drop all page_cgroup in active_list */
  768. mem_cgroup_force_empty_list(mem, mz, 1);
  769. /* drop all page_cgroup in inactive_list */
  770. mem_cgroup_force_empty_list(mem, mz, 0);
  771. }
  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. static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  784. const char *buffer)
  785. {
  786. return res_counter_write(&mem_cgroup_from_cont(cont)->res,
  787. cft->private, buffer,
  788. res_counter_memparse_write_strategy);
  789. }
  790. static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
  791. {
  792. struct mem_cgroup *mem;
  793. mem = mem_cgroup_from_cont(cont);
  794. switch (event) {
  795. case RES_MAX_USAGE:
  796. res_counter_reset_max(&mem->res);
  797. break;
  798. case RES_FAILCNT:
  799. res_counter_reset_failcnt(&mem->res);
  800. break;
  801. }
  802. return 0;
  803. }
  804. static int mem_force_empty_write(struct cgroup *cont, unsigned int event)
  805. {
  806. return mem_cgroup_force_empty(mem_cgroup_from_cont(cont));
  807. }
  808. static const struct mem_cgroup_stat_desc {
  809. const char *msg;
  810. u64 unit;
  811. } mem_cgroup_stat_desc[] = {
  812. [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
  813. [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
  814. [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
  815. [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
  816. };
  817. static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
  818. struct cgroup_map_cb *cb)
  819. {
  820. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  821. struct mem_cgroup_stat *stat = &mem_cont->stat;
  822. int i;
  823. for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
  824. s64 val;
  825. val = mem_cgroup_read_stat(stat, i);
  826. val *= mem_cgroup_stat_desc[i].unit;
  827. cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
  828. }
  829. /* showing # of active pages */
  830. {
  831. unsigned long active, inactive;
  832. inactive = mem_cgroup_get_all_zonestat(mem_cont,
  833. MEM_CGROUP_ZSTAT_INACTIVE);
  834. active = mem_cgroup_get_all_zonestat(mem_cont,
  835. MEM_CGROUP_ZSTAT_ACTIVE);
  836. cb->fill(cb, "active", (active) * PAGE_SIZE);
  837. cb->fill(cb, "inactive", (inactive) * PAGE_SIZE);
  838. }
  839. return 0;
  840. }
  841. static struct cftype mem_cgroup_files[] = {
  842. {
  843. .name = "usage_in_bytes",
  844. .private = RES_USAGE,
  845. .read_u64 = mem_cgroup_read,
  846. },
  847. {
  848. .name = "max_usage_in_bytes",
  849. .private = RES_MAX_USAGE,
  850. .trigger = mem_cgroup_reset,
  851. .read_u64 = mem_cgroup_read,
  852. },
  853. {
  854. .name = "limit_in_bytes",
  855. .private = RES_LIMIT,
  856. .write_string = mem_cgroup_write,
  857. .read_u64 = mem_cgroup_read,
  858. },
  859. {
  860. .name = "failcnt",
  861. .private = RES_FAILCNT,
  862. .trigger = mem_cgroup_reset,
  863. .read_u64 = mem_cgroup_read,
  864. },
  865. {
  866. .name = "force_empty",
  867. .trigger = mem_force_empty_write,
  868. },
  869. {
  870. .name = "stat",
  871. .read_map = mem_control_stat_show,
  872. },
  873. };
  874. static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  875. {
  876. struct mem_cgroup_per_node *pn;
  877. struct mem_cgroup_per_zone *mz;
  878. int zone, tmp = node;
  879. /*
  880. * This routine is called against possible nodes.
  881. * But it's BUG to call kmalloc() against offline node.
  882. *
  883. * TODO: this routine can waste much memory for nodes which will
  884. * never be onlined. It's better to use memory hotplug callback
  885. * function.
  886. */
  887. if (!node_state(node, N_NORMAL_MEMORY))
  888. tmp = -1;
  889. pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
  890. if (!pn)
  891. return 1;
  892. mem->info.nodeinfo[node] = pn;
  893. memset(pn, 0, sizeof(*pn));
  894. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  895. mz = &pn->zoneinfo[zone];
  896. INIT_LIST_HEAD(&mz->active_list);
  897. INIT_LIST_HEAD(&mz->inactive_list);
  898. spin_lock_init(&mz->lru_lock);
  899. }
  900. return 0;
  901. }
  902. static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  903. {
  904. kfree(mem->info.nodeinfo[node]);
  905. }
  906. static struct mem_cgroup *mem_cgroup_alloc(void)
  907. {
  908. struct mem_cgroup *mem;
  909. if (sizeof(*mem) < PAGE_SIZE)
  910. mem = kmalloc(sizeof(*mem), GFP_KERNEL);
  911. else
  912. mem = vmalloc(sizeof(*mem));
  913. if (mem)
  914. memset(mem, 0, sizeof(*mem));
  915. return mem;
  916. }
  917. static void mem_cgroup_free(struct mem_cgroup *mem)
  918. {
  919. if (sizeof(*mem) < PAGE_SIZE)
  920. kfree(mem);
  921. else
  922. vfree(mem);
  923. }
  924. static struct cgroup_subsys_state *
  925. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  926. {
  927. struct mem_cgroup *mem;
  928. int node;
  929. if (unlikely((cont->parent) == NULL)) {
  930. mem = &init_mem_cgroup;
  931. page_cgroup_cache = KMEM_CACHE(page_cgroup, SLAB_PANIC);
  932. } else {
  933. mem = mem_cgroup_alloc();
  934. if (!mem)
  935. return ERR_PTR(-ENOMEM);
  936. }
  937. res_counter_init(&mem->res);
  938. for_each_node_state(node, N_POSSIBLE)
  939. if (alloc_mem_cgroup_per_zone_info(mem, node))
  940. goto free_out;
  941. return &mem->css;
  942. free_out:
  943. for_each_node_state(node, N_POSSIBLE)
  944. free_mem_cgroup_per_zone_info(mem, node);
  945. if (cont->parent != NULL)
  946. mem_cgroup_free(mem);
  947. return ERR_PTR(-ENOMEM);
  948. }
  949. static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
  950. struct cgroup *cont)
  951. {
  952. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  953. mem_cgroup_force_empty(mem);
  954. }
  955. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  956. struct cgroup *cont)
  957. {
  958. int node;
  959. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  960. for_each_node_state(node, N_POSSIBLE)
  961. free_mem_cgroup_per_zone_info(mem, node);
  962. mem_cgroup_free(mem_cgroup_from_cont(cont));
  963. }
  964. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  965. struct cgroup *cont)
  966. {
  967. if (mem_cgroup_subsys.disabled)
  968. return 0;
  969. return cgroup_add_files(cont, ss, mem_cgroup_files,
  970. ARRAY_SIZE(mem_cgroup_files));
  971. }
  972. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  973. struct cgroup *cont,
  974. struct cgroup *old_cont,
  975. struct task_struct *p)
  976. {
  977. struct mm_struct *mm;
  978. struct mem_cgroup *mem, *old_mem;
  979. if (mem_cgroup_subsys.disabled)
  980. return;
  981. mm = get_task_mm(p);
  982. if (mm == NULL)
  983. return;
  984. mem = mem_cgroup_from_cont(cont);
  985. old_mem = mem_cgroup_from_cont(old_cont);
  986. if (mem == old_mem)
  987. goto out;
  988. /*
  989. * Only thread group leaders are allowed to migrate, the mm_struct is
  990. * in effect owned by the leader
  991. */
  992. if (!thread_group_leader(p))
  993. goto out;
  994. out:
  995. mmput(mm);
  996. }
  997. struct cgroup_subsys mem_cgroup_subsys = {
  998. .name = "memory",
  999. .subsys_id = mem_cgroup_subsys_id,
  1000. .create = mem_cgroup_create,
  1001. .pre_destroy = mem_cgroup_pre_destroy,
  1002. .destroy = mem_cgroup_destroy,
  1003. .populate = mem_cgroup_populate,
  1004. .attach = mem_cgroup_move_task,
  1005. .early_init = 0,
  1006. };