memcontrol.c 29 KB

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