memcontrol.c 30 KB

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