memcontrol.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. /* memcontrol.c - Memory Controller
  2. *
  3. * Copyright IBM Corporation, 2007
  4. * Author Balbir Singh <balbir@linux.vnet.ibm.com>
  5. *
  6. * Copyright 2007 OpenVZ SWsoft Inc
  7. * Author: Pavel Emelianov <xemul@openvz.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/res_counter.h>
  20. #include <linux/memcontrol.h>
  21. #include <linux/cgroup.h>
  22. #include <linux/mm.h>
  23. #include <linux/smp.h>
  24. #include <linux/page-flags.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/bit_spinlock.h>
  27. #include <linux/rcupdate.h>
  28. #include <linux/slab.h>
  29. #include <linux/swap.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/fs.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/vmalloc.h>
  34. #include <linux/mm_inline.h>
  35. #include <asm/uaccess.h>
  36. struct cgroup_subsys mem_cgroup_subsys __read_mostly;
  37. static struct kmem_cache *page_cgroup_cache __read_mostly;
  38. #define MEM_CGROUP_RECLAIM_RETRIES 5
  39. /*
  40. * Statistics for memory cgroup.
  41. */
  42. enum mem_cgroup_stat_index {
  43. /*
  44. * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
  45. */
  46. MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
  47. MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
  48. MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
  49. MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
  50. MEM_CGROUP_STAT_NSTATS,
  51. };
  52. struct mem_cgroup_stat_cpu {
  53. s64 count[MEM_CGROUP_STAT_NSTATS];
  54. } ____cacheline_aligned_in_smp;
  55. struct mem_cgroup_stat {
  56. struct mem_cgroup_stat_cpu cpustat[NR_CPUS];
  57. };
  58. /*
  59. * For accounting under irq disable, no need for increment preempt count.
  60. */
  61. static void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat *stat,
  62. enum mem_cgroup_stat_index idx, int val)
  63. {
  64. int cpu = smp_processor_id();
  65. stat->cpustat[cpu].count[idx] += val;
  66. }
  67. static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
  68. enum mem_cgroup_stat_index idx)
  69. {
  70. int cpu;
  71. s64 ret = 0;
  72. for_each_possible_cpu(cpu)
  73. ret += stat->cpustat[cpu].count[idx];
  74. return ret;
  75. }
  76. /*
  77. * per-zone information in memory controller.
  78. */
  79. struct mem_cgroup_per_zone {
  80. /*
  81. * spin_lock to protect the per cgroup LRU
  82. */
  83. spinlock_t lru_lock;
  84. struct list_head lists[NR_LRU_LISTS];
  85. unsigned long count[NR_LRU_LISTS];
  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. static struct mem_cgroup init_mem_cgroup;
  124. /*
  125. * We use the lower bit of the page->page_cgroup pointer as a bit spin
  126. * lock. We need to ensure that page->page_cgroup is at least two
  127. * byte aligned (based on comments from Nick Piggin). But since
  128. * bit_spin_lock doesn't actually set that lock bit in a non-debug
  129. * uniprocessor kernel, we should avoid setting it here too.
  130. */
  131. #define PAGE_CGROUP_LOCK_BIT 0x0
  132. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  133. #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
  134. #else
  135. #define PAGE_CGROUP_LOCK 0x0
  136. #endif
  137. /*
  138. * A page_cgroup page is associated with every page descriptor. The
  139. * page_cgroup helps us identify information about the cgroup
  140. */
  141. struct page_cgroup {
  142. struct list_head lru; /* per cgroup LRU list */
  143. struct page *page;
  144. struct mem_cgroup *mem_cgroup;
  145. int flags;
  146. };
  147. #define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
  148. #define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */
  149. #define PAGE_CGROUP_FLAG_FILE (0x4) /* page is file system backed */
  150. #define PAGE_CGROUP_FLAG_UNEVICTABLE (0x8) /* page is unevictableable */
  151. static int page_cgroup_nid(struct page_cgroup *pc)
  152. {
  153. return page_to_nid(pc->page);
  154. }
  155. static enum zone_type page_cgroup_zid(struct page_cgroup *pc)
  156. {
  157. return page_zonenum(pc->page);
  158. }
  159. enum charge_type {
  160. MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
  161. MEM_CGROUP_CHARGE_TYPE_MAPPED,
  162. MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
  163. MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
  164. };
  165. /*
  166. * Always modified under lru lock. Then, not necessary to preempt_disable()
  167. */
  168. static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags,
  169. bool charge)
  170. {
  171. int val = (charge)? 1 : -1;
  172. struct mem_cgroup_stat *stat = &mem->stat;
  173. VM_BUG_ON(!irqs_disabled());
  174. if (flags & PAGE_CGROUP_FLAG_CACHE)
  175. __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_CACHE, val);
  176. else
  177. __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
  178. if (charge)
  179. __mem_cgroup_stat_add_safe(stat,
  180. MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
  181. else
  182. __mem_cgroup_stat_add_safe(stat,
  183. MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
  184. }
  185. static struct mem_cgroup_per_zone *
  186. mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
  187. {
  188. return &mem->info.nodeinfo[nid]->zoneinfo[zid];
  189. }
  190. static struct mem_cgroup_per_zone *
  191. page_cgroup_zoneinfo(struct page_cgroup *pc)
  192. {
  193. struct mem_cgroup *mem = pc->mem_cgroup;
  194. int nid = page_cgroup_nid(pc);
  195. int zid = page_cgroup_zid(pc);
  196. return mem_cgroup_zoneinfo(mem, nid, zid);
  197. }
  198. static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
  199. enum lru_list idx)
  200. {
  201. int nid, zid;
  202. struct mem_cgroup_per_zone *mz;
  203. u64 total = 0;
  204. for_each_online_node(nid)
  205. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  206. mz = mem_cgroup_zoneinfo(mem, nid, zid);
  207. total += MEM_CGROUP_ZSTAT(mz, idx);
  208. }
  209. return total;
  210. }
  211. static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
  212. {
  213. return container_of(cgroup_subsys_state(cont,
  214. mem_cgroup_subsys_id), struct mem_cgroup,
  215. css);
  216. }
  217. struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
  218. {
  219. /*
  220. * mm_update_next_owner() may clear mm->owner to NULL
  221. * if it races with swapoff, page migration, etc.
  222. * So this can be called with p == NULL.
  223. */
  224. if (unlikely(!p))
  225. return NULL;
  226. return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
  227. struct mem_cgroup, css);
  228. }
  229. static inline int page_cgroup_locked(struct page *page)
  230. {
  231. return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  232. }
  233. static void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
  234. {
  235. VM_BUG_ON(!page_cgroup_locked(page));
  236. page->page_cgroup = ((unsigned long)pc | PAGE_CGROUP_LOCK);
  237. }
  238. struct page_cgroup *page_get_page_cgroup(struct page *page)
  239. {
  240. return (struct page_cgroup *) (page->page_cgroup & ~PAGE_CGROUP_LOCK);
  241. }
  242. static void lock_page_cgroup(struct page *page)
  243. {
  244. bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  245. }
  246. static int try_lock_page_cgroup(struct page *page)
  247. {
  248. return bit_spin_trylock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  249. }
  250. static void unlock_page_cgroup(struct page *page)
  251. {
  252. bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  253. }
  254. static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
  255. struct page_cgroup *pc)
  256. {
  257. int lru = LRU_BASE;
  258. if (pc->flags & PAGE_CGROUP_FLAG_UNEVICTABLE)
  259. lru = LRU_UNEVICTABLE;
  260. else {
  261. if (pc->flags & PAGE_CGROUP_FLAG_ACTIVE)
  262. lru += LRU_ACTIVE;
  263. if (pc->flags & PAGE_CGROUP_FLAG_FILE)
  264. lru += LRU_FILE;
  265. }
  266. MEM_CGROUP_ZSTAT(mz, lru) -= 1;
  267. mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false);
  268. list_del(&pc->lru);
  269. }
  270. static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
  271. struct page_cgroup *pc)
  272. {
  273. int lru = LRU_BASE;
  274. if (pc->flags & PAGE_CGROUP_FLAG_UNEVICTABLE)
  275. lru = LRU_UNEVICTABLE;
  276. else {
  277. if (pc->flags & PAGE_CGROUP_FLAG_ACTIVE)
  278. lru += LRU_ACTIVE;
  279. if (pc->flags & PAGE_CGROUP_FLAG_FILE)
  280. lru += LRU_FILE;
  281. }
  282. MEM_CGROUP_ZSTAT(mz, lru) += 1;
  283. list_add(&pc->lru, &mz->lists[lru]);
  284. mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true);
  285. }
  286. static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
  287. {
  288. struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
  289. int active = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
  290. int file = pc->flags & PAGE_CGROUP_FLAG_FILE;
  291. int unevictable = pc->flags & PAGE_CGROUP_FLAG_UNEVICTABLE;
  292. enum lru_list from = unevictable ? LRU_UNEVICTABLE :
  293. (LRU_FILE * !!file + !!active);
  294. if (lru == from)
  295. return;
  296. MEM_CGROUP_ZSTAT(mz, from) -= 1;
  297. if (is_unevictable_lru(lru)) {
  298. pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
  299. pc->flags |= PAGE_CGROUP_FLAG_UNEVICTABLE;
  300. } else {
  301. if (is_active_lru(lru))
  302. pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
  303. else
  304. pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
  305. pc->flags &= ~PAGE_CGROUP_FLAG_UNEVICTABLE;
  306. }
  307. MEM_CGROUP_ZSTAT(mz, lru) += 1;
  308. list_move(&pc->lru, &mz->lists[lru]);
  309. }
  310. int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
  311. {
  312. int ret;
  313. task_lock(task);
  314. ret = task->mm && mm_match_cgroup(task->mm, mem);
  315. task_unlock(task);
  316. return ret;
  317. }
  318. /*
  319. * This routine assumes that the appropriate zone's lru lock is already held
  320. */
  321. void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
  322. {
  323. struct page_cgroup *pc;
  324. struct mem_cgroup_per_zone *mz;
  325. unsigned long flags;
  326. if (mem_cgroup_subsys.disabled)
  327. return;
  328. /*
  329. * We cannot lock_page_cgroup while holding zone's lru_lock,
  330. * because other holders of lock_page_cgroup can be interrupted
  331. * with an attempt to rotate_reclaimable_page. But we cannot
  332. * safely get to page_cgroup without it, so just try_lock it:
  333. * mem_cgroup_isolate_pages allows for page left on wrong list.
  334. */
  335. if (!try_lock_page_cgroup(page))
  336. return;
  337. pc = page_get_page_cgroup(page);
  338. if (pc) {
  339. mz = page_cgroup_zoneinfo(pc);
  340. spin_lock_irqsave(&mz->lru_lock, flags);
  341. __mem_cgroup_move_lists(pc, lru);
  342. spin_unlock_irqrestore(&mz->lru_lock, flags);
  343. }
  344. unlock_page_cgroup(page);
  345. }
  346. /*
  347. * Calculate mapped_ratio under memory controller. This will be used in
  348. * vmscan.c for deteremining we have to reclaim mapped pages.
  349. */
  350. int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
  351. {
  352. long total, rss;
  353. /*
  354. * usage is recorded in bytes. But, here, we assume the number of
  355. * physical pages can be represented by "long" on any arch.
  356. */
  357. total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
  358. rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
  359. return (int)((rss * 100L) / total);
  360. }
  361. /*
  362. * prev_priority control...this will be used in memory reclaim path.
  363. */
  364. int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
  365. {
  366. return mem->prev_priority;
  367. }
  368. void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
  369. {
  370. if (priority < mem->prev_priority)
  371. mem->prev_priority = priority;
  372. }
  373. void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
  374. {
  375. mem->prev_priority = priority;
  376. }
  377. /*
  378. * Calculate # of pages to be scanned in this priority/zone.
  379. * See also vmscan.c
  380. *
  381. * priority starts from "DEF_PRIORITY" and decremented in each loop.
  382. * (see include/linux/mmzone.h)
  383. */
  384. long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
  385. int priority, enum lru_list lru)
  386. {
  387. long nr_pages;
  388. int nid = zone->zone_pgdat->node_id;
  389. int zid = zone_idx(zone);
  390. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
  391. nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
  392. return (nr_pages >> priority);
  393. }
  394. unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
  395. struct list_head *dst,
  396. unsigned long *scanned, int order,
  397. int mode, struct zone *z,
  398. struct mem_cgroup *mem_cont,
  399. int active, int file)
  400. {
  401. unsigned long nr_taken = 0;
  402. struct page *page;
  403. unsigned long scan;
  404. LIST_HEAD(pc_list);
  405. struct list_head *src;
  406. struct page_cgroup *pc, *tmp;
  407. int nid = z->zone_pgdat->node_id;
  408. int zid = zone_idx(z);
  409. struct mem_cgroup_per_zone *mz;
  410. int lru = LRU_FILE * !!file + !!active;
  411. BUG_ON(!mem_cont);
  412. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  413. src = &mz->lists[lru];
  414. spin_lock(&mz->lru_lock);
  415. scan = 0;
  416. list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
  417. if (scan >= nr_to_scan)
  418. break;
  419. page = pc->page;
  420. if (unlikely(!PageLRU(page)))
  421. continue;
  422. /*
  423. * TODO: play better with lumpy reclaim, grabbing anything.
  424. */
  425. if (PageUnevictable(page) ||
  426. (PageActive(page) && !active) ||
  427. (!PageActive(page) && active)) {
  428. __mem_cgroup_move_lists(pc, page_lru(page));
  429. continue;
  430. }
  431. scan++;
  432. list_move(&pc->lru, &pc_list);
  433. if (__isolate_lru_page(page, mode, file) == 0) {
  434. list_move(&page->lru, dst);
  435. nr_taken++;
  436. }
  437. }
  438. list_splice(&pc_list, src);
  439. spin_unlock(&mz->lru_lock);
  440. *scanned = scan;
  441. return nr_taken;
  442. }
  443. /*
  444. * Charge the memory controller for page usage.
  445. * Return
  446. * 0 if the charge was successful
  447. * < 0 if the cgroup is over its limit
  448. */
  449. static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
  450. gfp_t gfp_mask, enum charge_type ctype,
  451. struct mem_cgroup *memcg)
  452. {
  453. struct mem_cgroup *mem;
  454. struct page_cgroup *pc;
  455. unsigned long flags;
  456. unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  457. struct mem_cgroup_per_zone *mz;
  458. pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask);
  459. if (unlikely(pc == NULL))
  460. goto err;
  461. /*
  462. * We always charge the cgroup the mm_struct belongs to.
  463. * The mm_struct's mem_cgroup changes on task migration if the
  464. * thread group leader migrates. It's possible that mm is not
  465. * set, if so charge the init_mm (happens for pagecache usage).
  466. */
  467. if (likely(!memcg)) {
  468. rcu_read_lock();
  469. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  470. if (unlikely(!mem)) {
  471. rcu_read_unlock();
  472. kmem_cache_free(page_cgroup_cache, pc);
  473. return 0;
  474. }
  475. /*
  476. * For every charge from the cgroup, increment reference count
  477. */
  478. css_get(&mem->css);
  479. rcu_read_unlock();
  480. } else {
  481. mem = memcg;
  482. css_get(&memcg->css);
  483. }
  484. while (res_counter_charge(&mem->res, PAGE_SIZE)) {
  485. if (!(gfp_mask & __GFP_WAIT))
  486. goto out;
  487. if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
  488. continue;
  489. /*
  490. * try_to_free_mem_cgroup_pages() might not give us a full
  491. * picture of reclaim. Some pages are reclaimed and might be
  492. * moved to swap cache or just unmapped from the cgroup.
  493. * Check the limit again to see if the reclaim reduced the
  494. * current usage of the cgroup before giving up
  495. */
  496. if (res_counter_check_under_limit(&mem->res))
  497. continue;
  498. if (!nr_retries--) {
  499. mem_cgroup_out_of_memory(mem, gfp_mask);
  500. goto out;
  501. }
  502. }
  503. pc->mem_cgroup = mem;
  504. pc->page = page;
  505. /*
  506. * If a page is accounted as a page cache, insert to inactive list.
  507. * If anon, insert to active list.
  508. */
  509. if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE) {
  510. pc->flags = PAGE_CGROUP_FLAG_CACHE;
  511. if (page_is_file_cache(page))
  512. pc->flags |= PAGE_CGROUP_FLAG_FILE;
  513. else
  514. pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
  515. } else if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
  516. pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
  517. else /* MEM_CGROUP_CHARGE_TYPE_SHMEM */
  518. pc->flags = PAGE_CGROUP_FLAG_CACHE | PAGE_CGROUP_FLAG_ACTIVE;
  519. lock_page_cgroup(page);
  520. if (unlikely(page_get_page_cgroup(page))) {
  521. unlock_page_cgroup(page);
  522. res_counter_uncharge(&mem->res, PAGE_SIZE);
  523. css_put(&mem->css);
  524. kmem_cache_free(page_cgroup_cache, pc);
  525. goto done;
  526. }
  527. page_assign_page_cgroup(page, pc);
  528. mz = page_cgroup_zoneinfo(pc);
  529. spin_lock_irqsave(&mz->lru_lock, flags);
  530. __mem_cgroup_add_list(mz, pc);
  531. spin_unlock_irqrestore(&mz->lru_lock, flags);
  532. unlock_page_cgroup(page);
  533. done:
  534. return 0;
  535. out:
  536. css_put(&mem->css);
  537. kmem_cache_free(page_cgroup_cache, pc);
  538. err:
  539. return -ENOMEM;
  540. }
  541. int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
  542. {
  543. if (mem_cgroup_subsys.disabled)
  544. return 0;
  545. /*
  546. * If already mapped, we don't have to account.
  547. * If page cache, page->mapping has address_space.
  548. * But page->mapping may have out-of-use anon_vma pointer,
  549. * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
  550. * is NULL.
  551. */
  552. if (page_mapped(page) || (page->mapping && !PageAnon(page)))
  553. return 0;
  554. if (unlikely(!mm))
  555. mm = &init_mm;
  556. return mem_cgroup_charge_common(page, mm, gfp_mask,
  557. MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
  558. }
  559. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  560. gfp_t gfp_mask)
  561. {
  562. if (mem_cgroup_subsys.disabled)
  563. return 0;
  564. /*
  565. * Corner case handling. This is called from add_to_page_cache()
  566. * in usual. But some FS (shmem) precharges this page before calling it
  567. * and call add_to_page_cache() with GFP_NOWAIT.
  568. *
  569. * For GFP_NOWAIT case, the page may be pre-charged before calling
  570. * add_to_page_cache(). (See shmem.c) check it here and avoid to call
  571. * charge twice. (It works but has to pay a bit larger cost.)
  572. */
  573. if (!(gfp_mask & __GFP_WAIT)) {
  574. struct page_cgroup *pc;
  575. lock_page_cgroup(page);
  576. pc = page_get_page_cgroup(page);
  577. if (pc) {
  578. VM_BUG_ON(pc->page != page);
  579. VM_BUG_ON(!pc->mem_cgroup);
  580. unlock_page_cgroup(page);
  581. return 0;
  582. }
  583. unlock_page_cgroup(page);
  584. }
  585. if (unlikely(!mm))
  586. mm = &init_mm;
  587. return mem_cgroup_charge_common(page, mm, gfp_mask,
  588. MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
  589. }
  590. /*
  591. * uncharge if !page_mapped(page)
  592. */
  593. static void
  594. __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
  595. {
  596. struct page_cgroup *pc;
  597. struct mem_cgroup *mem;
  598. struct mem_cgroup_per_zone *mz;
  599. unsigned long flags;
  600. if (mem_cgroup_subsys.disabled)
  601. return;
  602. /*
  603. * Check if our page_cgroup is valid
  604. */
  605. lock_page_cgroup(page);
  606. pc = page_get_page_cgroup(page);
  607. if (unlikely(!pc))
  608. goto unlock;
  609. VM_BUG_ON(pc->page != page);
  610. if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
  611. && ((pc->flags & PAGE_CGROUP_FLAG_CACHE)
  612. || page_mapped(page)))
  613. goto unlock;
  614. mz = page_cgroup_zoneinfo(pc);
  615. spin_lock_irqsave(&mz->lru_lock, flags);
  616. __mem_cgroup_remove_list(mz, pc);
  617. spin_unlock_irqrestore(&mz->lru_lock, flags);
  618. page_assign_page_cgroup(page, NULL);
  619. unlock_page_cgroup(page);
  620. mem = pc->mem_cgroup;
  621. res_counter_uncharge(&mem->res, PAGE_SIZE);
  622. css_put(&mem->css);
  623. kmem_cache_free(page_cgroup_cache, pc);
  624. return;
  625. unlock:
  626. unlock_page_cgroup(page);
  627. }
  628. void mem_cgroup_uncharge_page(struct page *page)
  629. {
  630. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
  631. }
  632. void mem_cgroup_uncharge_cache_page(struct page *page)
  633. {
  634. VM_BUG_ON(page_mapped(page));
  635. VM_BUG_ON(page->mapping);
  636. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
  637. }
  638. /*
  639. * Before starting migration, account against new page.
  640. */
  641. int mem_cgroup_prepare_migration(struct page *page, struct page *newpage)
  642. {
  643. struct page_cgroup *pc;
  644. struct mem_cgroup *mem = NULL;
  645. enum charge_type ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
  646. int ret = 0;
  647. if (mem_cgroup_subsys.disabled)
  648. return 0;
  649. lock_page_cgroup(page);
  650. pc = page_get_page_cgroup(page);
  651. if (pc) {
  652. mem = pc->mem_cgroup;
  653. css_get(&mem->css);
  654. if (pc->flags & PAGE_CGROUP_FLAG_CACHE) {
  655. if (page_is_file_cache(page))
  656. ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
  657. else
  658. ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
  659. }
  660. }
  661. unlock_page_cgroup(page);
  662. if (mem) {
  663. ret = mem_cgroup_charge_common(newpage, NULL, GFP_KERNEL,
  664. ctype, mem);
  665. css_put(&mem->css);
  666. }
  667. return ret;
  668. }
  669. /* remove redundant charge if migration failed*/
  670. void mem_cgroup_end_migration(struct page *newpage)
  671. {
  672. /*
  673. * At success, page->mapping is not NULL.
  674. * special rollback care is necessary when
  675. * 1. at migration failure. (newpage->mapping is cleared in this case)
  676. * 2. the newpage was moved but not remapped again because the task
  677. * exits and the newpage is obsolete. In this case, the new page
  678. * may be a swapcache. So, we just call mem_cgroup_uncharge_page()
  679. * always for avoiding mess. The page_cgroup will be removed if
  680. * unnecessary. File cache pages is still on radix-tree. Don't
  681. * care it.
  682. */
  683. if (!newpage->mapping)
  684. __mem_cgroup_uncharge_common(newpage,
  685. MEM_CGROUP_CHARGE_TYPE_FORCE);
  686. else if (PageAnon(newpage))
  687. mem_cgroup_uncharge_page(newpage);
  688. }
  689. /*
  690. * A call to try to shrink memory usage under specified resource controller.
  691. * This is typically used for page reclaiming for shmem for reducing side
  692. * effect of page allocation from shmem, which is used by some mem_cgroup.
  693. */
  694. int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
  695. {
  696. struct mem_cgroup *mem;
  697. int progress = 0;
  698. int retry = MEM_CGROUP_RECLAIM_RETRIES;
  699. if (mem_cgroup_subsys.disabled)
  700. return 0;
  701. if (!mm)
  702. return 0;
  703. rcu_read_lock();
  704. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  705. if (unlikely(!mem)) {
  706. rcu_read_unlock();
  707. return 0;
  708. }
  709. css_get(&mem->css);
  710. rcu_read_unlock();
  711. do {
  712. progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
  713. progress += res_counter_check_under_limit(&mem->res);
  714. } while (!progress && --retry);
  715. css_put(&mem->css);
  716. if (!retry)
  717. return -ENOMEM;
  718. return 0;
  719. }
  720. int mem_cgroup_resize_limit(struct mem_cgroup *memcg, unsigned long long val)
  721. {
  722. int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
  723. int progress;
  724. int ret = 0;
  725. while (res_counter_set_limit(&memcg->res, val)) {
  726. if (signal_pending(current)) {
  727. ret = -EINTR;
  728. break;
  729. }
  730. if (!retry_count) {
  731. ret = -EBUSY;
  732. break;
  733. }
  734. progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL);
  735. if (!progress)
  736. retry_count--;
  737. }
  738. return ret;
  739. }
  740. /*
  741. * This routine traverse page_cgroup in given list and drop them all.
  742. * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  743. */
  744. #define FORCE_UNCHARGE_BATCH (128)
  745. static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
  746. struct mem_cgroup_per_zone *mz,
  747. enum lru_list lru)
  748. {
  749. struct page_cgroup *pc;
  750. struct page *page;
  751. int count = FORCE_UNCHARGE_BATCH;
  752. unsigned long flags;
  753. struct list_head *list;
  754. list = &mz->lists[lru];
  755. spin_lock_irqsave(&mz->lru_lock, flags);
  756. while (!list_empty(list)) {
  757. pc = list_entry(list->prev, struct page_cgroup, lru);
  758. page = pc->page;
  759. get_page(page);
  760. spin_unlock_irqrestore(&mz->lru_lock, flags);
  761. /*
  762. * Check if this page is on LRU. !LRU page can be found
  763. * if it's under page migration.
  764. */
  765. if (PageLRU(page)) {
  766. __mem_cgroup_uncharge_common(page,
  767. MEM_CGROUP_CHARGE_TYPE_FORCE);
  768. put_page(page);
  769. if (--count <= 0) {
  770. count = FORCE_UNCHARGE_BATCH;
  771. cond_resched();
  772. }
  773. } else
  774. cond_resched();
  775. spin_lock_irqsave(&mz->lru_lock, flags);
  776. }
  777. spin_unlock_irqrestore(&mz->lru_lock, flags);
  778. }
  779. /*
  780. * make mem_cgroup's charge to be 0 if there is no task.
  781. * This enables deleting this mem_cgroup.
  782. */
  783. static int mem_cgroup_force_empty(struct mem_cgroup *mem)
  784. {
  785. int ret = -EBUSY;
  786. int node, zid;
  787. css_get(&mem->css);
  788. /*
  789. * page reclaim code (kswapd etc..) will move pages between
  790. * active_list <-> inactive_list while we don't take a lock.
  791. * So, we have to do loop here until all lists are empty.
  792. */
  793. while (mem->res.usage > 0) {
  794. if (atomic_read(&mem->css.cgroup->count) > 0)
  795. goto out;
  796. for_each_node_state(node, N_POSSIBLE)
  797. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  798. struct mem_cgroup_per_zone *mz;
  799. enum lru_list l;
  800. mz = mem_cgroup_zoneinfo(mem, node, zid);
  801. for_each_lru(l)
  802. mem_cgroup_force_empty_list(mem, mz, l);
  803. }
  804. }
  805. ret = 0;
  806. out:
  807. css_put(&mem->css);
  808. return ret;
  809. }
  810. static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
  811. {
  812. return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
  813. cft->private);
  814. }
  815. /*
  816. * The user of this function is...
  817. * RES_LIMIT.
  818. */
  819. static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  820. const char *buffer)
  821. {
  822. struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
  823. unsigned long long val;
  824. int ret;
  825. switch (cft->private) {
  826. case RES_LIMIT:
  827. /* This function does all necessary parse...reuse it */
  828. ret = res_counter_memparse_write_strategy(buffer, &val);
  829. if (!ret)
  830. ret = mem_cgroup_resize_limit(memcg, val);
  831. break;
  832. default:
  833. ret = -EINVAL; /* should be BUG() ? */
  834. break;
  835. }
  836. return ret;
  837. }
  838. static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
  839. {
  840. struct mem_cgroup *mem;
  841. mem = mem_cgroup_from_cont(cont);
  842. switch (event) {
  843. case RES_MAX_USAGE:
  844. res_counter_reset_max(&mem->res);
  845. break;
  846. case RES_FAILCNT:
  847. res_counter_reset_failcnt(&mem->res);
  848. break;
  849. }
  850. return 0;
  851. }
  852. static int mem_force_empty_write(struct cgroup *cont, unsigned int event)
  853. {
  854. return mem_cgroup_force_empty(mem_cgroup_from_cont(cont));
  855. }
  856. static const struct mem_cgroup_stat_desc {
  857. const char *msg;
  858. u64 unit;
  859. } mem_cgroup_stat_desc[] = {
  860. [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
  861. [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
  862. [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
  863. [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
  864. };
  865. static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
  866. struct cgroup_map_cb *cb)
  867. {
  868. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  869. struct mem_cgroup_stat *stat = &mem_cont->stat;
  870. int i;
  871. for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
  872. s64 val;
  873. val = mem_cgroup_read_stat(stat, i);
  874. val *= mem_cgroup_stat_desc[i].unit;
  875. cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
  876. }
  877. /* showing # of active pages */
  878. {
  879. unsigned long active_anon, inactive_anon;
  880. unsigned long active_file, inactive_file;
  881. unsigned long unevictable;
  882. inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
  883. LRU_INACTIVE_ANON);
  884. active_anon = mem_cgroup_get_all_zonestat(mem_cont,
  885. LRU_ACTIVE_ANON);
  886. inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
  887. LRU_INACTIVE_FILE);
  888. active_file = mem_cgroup_get_all_zonestat(mem_cont,
  889. LRU_ACTIVE_FILE);
  890. unevictable = mem_cgroup_get_all_zonestat(mem_cont,
  891. LRU_UNEVICTABLE);
  892. cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
  893. cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
  894. cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
  895. cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
  896. cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
  897. }
  898. return 0;
  899. }
  900. static struct cftype mem_cgroup_files[] = {
  901. {
  902. .name = "usage_in_bytes",
  903. .private = RES_USAGE,
  904. .read_u64 = mem_cgroup_read,
  905. },
  906. {
  907. .name = "max_usage_in_bytes",
  908. .private = RES_MAX_USAGE,
  909. .trigger = mem_cgroup_reset,
  910. .read_u64 = mem_cgroup_read,
  911. },
  912. {
  913. .name = "limit_in_bytes",
  914. .private = RES_LIMIT,
  915. .write_string = mem_cgroup_write,
  916. .read_u64 = mem_cgroup_read,
  917. },
  918. {
  919. .name = "failcnt",
  920. .private = RES_FAILCNT,
  921. .trigger = mem_cgroup_reset,
  922. .read_u64 = mem_cgroup_read,
  923. },
  924. {
  925. .name = "force_empty",
  926. .trigger = mem_force_empty_write,
  927. },
  928. {
  929. .name = "stat",
  930. .read_map = mem_control_stat_show,
  931. },
  932. };
  933. static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  934. {
  935. struct mem_cgroup_per_node *pn;
  936. struct mem_cgroup_per_zone *mz;
  937. enum lru_list l;
  938. int zone, tmp = node;
  939. /*
  940. * This routine is called against possible nodes.
  941. * But it's BUG to call kmalloc() against offline node.
  942. *
  943. * TODO: this routine can waste much memory for nodes which will
  944. * never be onlined. It's better to use memory hotplug callback
  945. * function.
  946. */
  947. if (!node_state(node, N_NORMAL_MEMORY))
  948. tmp = -1;
  949. pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
  950. if (!pn)
  951. return 1;
  952. mem->info.nodeinfo[node] = pn;
  953. memset(pn, 0, sizeof(*pn));
  954. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  955. mz = &pn->zoneinfo[zone];
  956. spin_lock_init(&mz->lru_lock);
  957. for_each_lru(l)
  958. INIT_LIST_HEAD(&mz->lists[l]);
  959. }
  960. return 0;
  961. }
  962. static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  963. {
  964. kfree(mem->info.nodeinfo[node]);
  965. }
  966. static struct mem_cgroup *mem_cgroup_alloc(void)
  967. {
  968. struct mem_cgroup *mem;
  969. if (sizeof(*mem) < PAGE_SIZE)
  970. mem = kmalloc(sizeof(*mem), GFP_KERNEL);
  971. else
  972. mem = vmalloc(sizeof(*mem));
  973. if (mem)
  974. memset(mem, 0, sizeof(*mem));
  975. return mem;
  976. }
  977. static void mem_cgroup_free(struct mem_cgroup *mem)
  978. {
  979. if (sizeof(*mem) < PAGE_SIZE)
  980. kfree(mem);
  981. else
  982. vfree(mem);
  983. }
  984. static struct cgroup_subsys_state *
  985. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  986. {
  987. struct mem_cgroup *mem;
  988. int node;
  989. if (unlikely((cont->parent) == NULL)) {
  990. mem = &init_mem_cgroup;
  991. page_cgroup_cache = KMEM_CACHE(page_cgroup, SLAB_PANIC);
  992. } else {
  993. mem = mem_cgroup_alloc();
  994. if (!mem)
  995. return ERR_PTR(-ENOMEM);
  996. }
  997. res_counter_init(&mem->res);
  998. for_each_node_state(node, N_POSSIBLE)
  999. if (alloc_mem_cgroup_per_zone_info(mem, node))
  1000. goto free_out;
  1001. return &mem->css;
  1002. free_out:
  1003. for_each_node_state(node, N_POSSIBLE)
  1004. free_mem_cgroup_per_zone_info(mem, node);
  1005. if (cont->parent != NULL)
  1006. mem_cgroup_free(mem);
  1007. return ERR_PTR(-ENOMEM);
  1008. }
  1009. static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
  1010. struct cgroup *cont)
  1011. {
  1012. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  1013. mem_cgroup_force_empty(mem);
  1014. }
  1015. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  1016. struct cgroup *cont)
  1017. {
  1018. int node;
  1019. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  1020. for_each_node_state(node, N_POSSIBLE)
  1021. free_mem_cgroup_per_zone_info(mem, node);
  1022. mem_cgroup_free(mem_cgroup_from_cont(cont));
  1023. }
  1024. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  1025. struct cgroup *cont)
  1026. {
  1027. return cgroup_add_files(cont, ss, mem_cgroup_files,
  1028. ARRAY_SIZE(mem_cgroup_files));
  1029. }
  1030. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  1031. struct cgroup *cont,
  1032. struct cgroup *old_cont,
  1033. struct task_struct *p)
  1034. {
  1035. struct mm_struct *mm;
  1036. struct mem_cgroup *mem, *old_mem;
  1037. mm = get_task_mm(p);
  1038. if (mm == NULL)
  1039. return;
  1040. mem = mem_cgroup_from_cont(cont);
  1041. old_mem = mem_cgroup_from_cont(old_cont);
  1042. /*
  1043. * Only thread group leaders are allowed to migrate, the mm_struct is
  1044. * in effect owned by the leader
  1045. */
  1046. if (!thread_group_leader(p))
  1047. goto out;
  1048. out:
  1049. mmput(mm);
  1050. }
  1051. struct cgroup_subsys mem_cgroup_subsys = {
  1052. .name = "memory",
  1053. .subsys_id = mem_cgroup_subsys_id,
  1054. .create = mem_cgroup_create,
  1055. .pre_destroy = mem_cgroup_pre_destroy,
  1056. .destroy = mem_cgroup_destroy,
  1057. .populate = mem_cgroup_populate,
  1058. .attach = mem_cgroup_move_task,
  1059. .early_init = 0,
  1060. };