memcontrol.c 27 KB

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