memcontrol.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. 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 ref_cnt; /* cached, mapped, migrating */
  146. int flags;
  147. };
  148. #define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
  149. #define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */
  150. static int page_cgroup_nid(struct page_cgroup *pc)
  151. {
  152. return page_to_nid(pc->page);
  153. }
  154. static enum zone_type page_cgroup_zid(struct page_cgroup *pc)
  155. {
  156. return page_zonenum(pc->page);
  157. }
  158. enum charge_type {
  159. MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
  160. MEM_CGROUP_CHARGE_TYPE_MAPPED,
  161. };
  162. /*
  163. * Always modified under lru lock. Then, not necessary to preempt_disable()
  164. */
  165. static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags,
  166. bool charge)
  167. {
  168. int val = (charge)? 1 : -1;
  169. struct mem_cgroup_stat *stat = &mem->stat;
  170. VM_BUG_ON(!irqs_disabled());
  171. if (flags & PAGE_CGROUP_FLAG_CACHE)
  172. __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_CACHE, val);
  173. else
  174. __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
  175. }
  176. static struct mem_cgroup_per_zone *
  177. mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
  178. {
  179. return &mem->info.nodeinfo[nid]->zoneinfo[zid];
  180. }
  181. static struct mem_cgroup_per_zone *
  182. page_cgroup_zoneinfo(struct page_cgroup *pc)
  183. {
  184. struct mem_cgroup *mem = pc->mem_cgroup;
  185. int nid = page_cgroup_nid(pc);
  186. int zid = page_cgroup_zid(pc);
  187. return mem_cgroup_zoneinfo(mem, nid, zid);
  188. }
  189. static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
  190. enum mem_cgroup_zstat_index idx)
  191. {
  192. int nid, zid;
  193. struct mem_cgroup_per_zone *mz;
  194. u64 total = 0;
  195. for_each_online_node(nid)
  196. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  197. mz = mem_cgroup_zoneinfo(mem, nid, zid);
  198. total += MEM_CGROUP_ZSTAT(mz, idx);
  199. }
  200. return total;
  201. }
  202. static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
  203. {
  204. return container_of(cgroup_subsys_state(cont,
  205. mem_cgroup_subsys_id), struct mem_cgroup,
  206. css);
  207. }
  208. static struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
  209. {
  210. return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
  211. struct mem_cgroup, css);
  212. }
  213. void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p)
  214. {
  215. struct mem_cgroup *mem;
  216. mem = mem_cgroup_from_task(p);
  217. css_get(&mem->css);
  218. mm->mem_cgroup = mem;
  219. }
  220. void mm_free_cgroup(struct mm_struct *mm)
  221. {
  222. css_put(&mm->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 page_cgroup *pc)
  250. {
  251. int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
  252. struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
  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_init(&pc->lru);
  259. }
  260. static void __mem_cgroup_add_list(struct page_cgroup *pc)
  261. {
  262. int to = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
  263. struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
  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 *mem;
  306. struct mem_cgroup_per_zone *mz;
  307. unsigned long flags;
  308. /*
  309. * We cannot lock_page_cgroup while holding zone's lru_lock,
  310. * because other holders of lock_page_cgroup can be interrupted
  311. * with an attempt to rotate_reclaimable_page. But we cannot
  312. * safely get to page_cgroup without it, so just try_lock it:
  313. * mem_cgroup_isolate_pages allows for page left on wrong list.
  314. */
  315. if (!try_lock_page_cgroup(page))
  316. return;
  317. /*
  318. * Now page_cgroup is stable, but we cannot acquire mz->lru_lock
  319. * while holding it, because mem_cgroup_force_empty_list does the
  320. * reverse. Get a hold on the mem_cgroup before unlocking, so that
  321. * the zoneinfo remains stable, then take mz->lru_lock; then check
  322. * that page still points to pc and pc (even if freed and reassigned
  323. * to that same page meanwhile) still points to the same mem_cgroup.
  324. * Then we know mz still points to the right spinlock, so it's safe
  325. * to move_lists (page->page_cgroup might be reset while we do so, but
  326. * that doesn't matter: pc->page is stable till we drop mz->lru_lock).
  327. * We're being a little naughty not to try_lock_page_cgroup again
  328. * inside there, but we are safe, aren't we? Aren't we? Whistle...
  329. */
  330. pc = page_get_page_cgroup(page);
  331. if (pc) {
  332. mem = pc->mem_cgroup;
  333. mz = page_cgroup_zoneinfo(pc);
  334. css_get(&mem->css);
  335. unlock_page_cgroup(page);
  336. spin_lock_irqsave(&mz->lru_lock, flags);
  337. if (page_get_page_cgroup(page) == pc && pc->mem_cgroup == mem)
  338. __mem_cgroup_move_lists(pc, active);
  339. spin_unlock_irqrestore(&mz->lru_lock, flags);
  340. css_put(&mem->css);
  341. } else
  342. unlock_page_cgroup(page);
  343. }
  344. /*
  345. * Calculate mapped_ratio under memory controller. This will be used in
  346. * vmscan.c for deteremining we have to reclaim mapped pages.
  347. */
  348. int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
  349. {
  350. long total, rss;
  351. /*
  352. * usage is recorded in bytes. But, here, we assume the number of
  353. * physical pages can be represented by "long" on any arch.
  354. */
  355. total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
  356. rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
  357. return (int)((rss * 100L) / total);
  358. }
  359. /*
  360. * This function is called from vmscan.c. In page reclaiming loop. balance
  361. * between active and inactive list is calculated. For memory controller
  362. * page reclaiming, we should use using mem_cgroup's imbalance rather than
  363. * zone's global lru imbalance.
  364. */
  365. long mem_cgroup_reclaim_imbalance(struct mem_cgroup *mem)
  366. {
  367. unsigned long active, inactive;
  368. /* active and inactive are the number of pages. 'long' is ok.*/
  369. active = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_ACTIVE);
  370. inactive = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_INACTIVE);
  371. return (long) (active / (inactive + 1));
  372. }
  373. /*
  374. * prev_priority control...this will be used in memory reclaim path.
  375. */
  376. int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
  377. {
  378. return mem->prev_priority;
  379. }
  380. void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
  381. {
  382. if (priority < mem->prev_priority)
  383. mem->prev_priority = priority;
  384. }
  385. void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
  386. {
  387. mem->prev_priority = priority;
  388. }
  389. /*
  390. * Calculate # of pages to be scanned in this priority/zone.
  391. * See also vmscan.c
  392. *
  393. * priority starts from "DEF_PRIORITY" and decremented in each loop.
  394. * (see include/linux/mmzone.h)
  395. */
  396. long mem_cgroup_calc_reclaim_active(struct mem_cgroup *mem,
  397. struct zone *zone, int priority)
  398. {
  399. long nr_active;
  400. int nid = zone->zone_pgdat->node_id;
  401. int zid = zone_idx(zone);
  402. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
  403. nr_active = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE);
  404. return (nr_active >> priority);
  405. }
  406. long mem_cgroup_calc_reclaim_inactive(struct mem_cgroup *mem,
  407. struct zone *zone, int priority)
  408. {
  409. long nr_inactive;
  410. int nid = zone->zone_pgdat->node_id;
  411. int zid = zone_idx(zone);
  412. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
  413. nr_inactive = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE);
  414. return (nr_inactive >> priority);
  415. }
  416. unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
  417. struct list_head *dst,
  418. unsigned long *scanned, int order,
  419. int mode, struct zone *z,
  420. struct mem_cgroup *mem_cont,
  421. int active)
  422. {
  423. unsigned long nr_taken = 0;
  424. struct page *page;
  425. unsigned long scan;
  426. LIST_HEAD(pc_list);
  427. struct list_head *src;
  428. struct page_cgroup *pc, *tmp;
  429. int nid = z->zone_pgdat->node_id;
  430. int zid = zone_idx(z);
  431. struct mem_cgroup_per_zone *mz;
  432. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  433. if (active)
  434. src = &mz->active_list;
  435. else
  436. src = &mz->inactive_list;
  437. spin_lock(&mz->lru_lock);
  438. scan = 0;
  439. list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
  440. if (scan >= nr_to_scan)
  441. break;
  442. page = pc->page;
  443. if (unlikely(!PageLRU(page)))
  444. continue;
  445. if (PageActive(page) && !active) {
  446. __mem_cgroup_move_lists(pc, true);
  447. continue;
  448. }
  449. if (!PageActive(page) && active) {
  450. __mem_cgroup_move_lists(pc, false);
  451. continue;
  452. }
  453. scan++;
  454. list_move(&pc->lru, &pc_list);
  455. if (__isolate_lru_page(page, mode) == 0) {
  456. list_move(&page->lru, dst);
  457. nr_taken++;
  458. }
  459. }
  460. list_splice(&pc_list, src);
  461. spin_unlock(&mz->lru_lock);
  462. *scanned = scan;
  463. return nr_taken;
  464. }
  465. /*
  466. * Charge the memory controller for page usage.
  467. * Return
  468. * 0 if the charge was successful
  469. * < 0 if the cgroup is over its limit
  470. */
  471. static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
  472. gfp_t gfp_mask, enum charge_type ctype)
  473. {
  474. struct mem_cgroup *mem;
  475. struct page_cgroup *pc;
  476. unsigned long flags;
  477. unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  478. struct mem_cgroup_per_zone *mz;
  479. /*
  480. * Should page_cgroup's go to their own slab?
  481. * One could optimize the performance of the charging routine
  482. * by saving a bit in the page_flags and using it as a lock
  483. * to see if the cgroup page already has a page_cgroup associated
  484. * with it
  485. */
  486. retry:
  487. lock_page_cgroup(page);
  488. pc = page_get_page_cgroup(page);
  489. /*
  490. * The page_cgroup exists and
  491. * the page has already been accounted.
  492. */
  493. if (pc) {
  494. VM_BUG_ON(pc->page != page);
  495. VM_BUG_ON(pc->ref_cnt <= 0);
  496. pc->ref_cnt++;
  497. unlock_page_cgroup(page);
  498. goto done;
  499. }
  500. unlock_page_cgroup(page);
  501. pc = kzalloc(sizeof(struct page_cgroup), gfp_mask);
  502. if (pc == NULL)
  503. goto err;
  504. /*
  505. * We always charge the cgroup the mm_struct belongs to.
  506. * The mm_struct's mem_cgroup changes on task migration if the
  507. * thread group leader migrates. It's possible that mm is not
  508. * set, if so charge the init_mm (happens for pagecache usage).
  509. */
  510. if (!mm)
  511. mm = &init_mm;
  512. rcu_read_lock();
  513. mem = rcu_dereference(mm->mem_cgroup);
  514. /*
  515. * For every charge from the cgroup, increment reference count
  516. */
  517. css_get(&mem->css);
  518. rcu_read_unlock();
  519. while (res_counter_charge(&mem->res, PAGE_SIZE)) {
  520. if (!(gfp_mask & __GFP_WAIT))
  521. goto out;
  522. if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
  523. continue;
  524. /*
  525. * try_to_free_mem_cgroup_pages() might not give us a full
  526. * picture of reclaim. Some pages are reclaimed and might be
  527. * moved to swap cache or just unmapped from the cgroup.
  528. * Check the limit again to see if the reclaim reduced the
  529. * current usage of the cgroup before giving up
  530. */
  531. if (res_counter_check_under_limit(&mem->res))
  532. continue;
  533. if (!nr_retries--) {
  534. mem_cgroup_out_of_memory(mem, gfp_mask);
  535. goto out;
  536. }
  537. congestion_wait(WRITE, HZ/10);
  538. }
  539. pc->ref_cnt = 1;
  540. pc->mem_cgroup = mem;
  541. pc->page = page;
  542. pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
  543. if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE)
  544. pc->flags |= PAGE_CGROUP_FLAG_CACHE;
  545. lock_page_cgroup(page);
  546. if (page_get_page_cgroup(page)) {
  547. unlock_page_cgroup(page);
  548. /*
  549. * Another charge has been added to this page already.
  550. * We take lock_page_cgroup(page) again and read
  551. * page->cgroup, increment refcnt.... just retry is OK.
  552. */
  553. res_counter_uncharge(&mem->res, PAGE_SIZE);
  554. css_put(&mem->css);
  555. kfree(pc);
  556. goto retry;
  557. }
  558. page_assign_page_cgroup(page, pc);
  559. unlock_page_cgroup(page);
  560. mz = page_cgroup_zoneinfo(pc);
  561. spin_lock_irqsave(&mz->lru_lock, flags);
  562. __mem_cgroup_add_list(pc);
  563. spin_unlock_irqrestore(&mz->lru_lock, flags);
  564. done:
  565. return 0;
  566. out:
  567. css_put(&mem->css);
  568. kfree(pc);
  569. err:
  570. return -ENOMEM;
  571. }
  572. int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
  573. {
  574. return mem_cgroup_charge_common(page, mm, gfp_mask,
  575. MEM_CGROUP_CHARGE_TYPE_MAPPED);
  576. }
  577. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  578. gfp_t gfp_mask)
  579. {
  580. if (!mm)
  581. mm = &init_mm;
  582. return mem_cgroup_charge_common(page, mm, gfp_mask,
  583. MEM_CGROUP_CHARGE_TYPE_CACHE);
  584. }
  585. /*
  586. * Uncharging is always a welcome operation, we never complain, simply
  587. * uncharge.
  588. */
  589. void mem_cgroup_uncharge_page(struct page *page)
  590. {
  591. struct page_cgroup *pc;
  592. struct mem_cgroup *mem;
  593. struct mem_cgroup_per_zone *mz;
  594. unsigned long flags;
  595. /*
  596. * Check if our page_cgroup is valid
  597. */
  598. lock_page_cgroup(page);
  599. pc = page_get_page_cgroup(page);
  600. if (!pc)
  601. goto unlock;
  602. VM_BUG_ON(pc->page != page);
  603. VM_BUG_ON(pc->ref_cnt <= 0);
  604. if (--(pc->ref_cnt) == 0) {
  605. page_assign_page_cgroup(page, NULL);
  606. unlock_page_cgroup(page);
  607. mz = page_cgroup_zoneinfo(pc);
  608. spin_lock_irqsave(&mz->lru_lock, flags);
  609. __mem_cgroup_remove_list(pc);
  610. spin_unlock_irqrestore(&mz->lru_lock, flags);
  611. mem = pc->mem_cgroup;
  612. res_counter_uncharge(&mem->res, PAGE_SIZE);
  613. css_put(&mem->css);
  614. kfree(pc);
  615. return;
  616. }
  617. unlock:
  618. unlock_page_cgroup(page);
  619. }
  620. /*
  621. * Returns non-zero if a page (under migration) has valid page_cgroup member.
  622. * Refcnt of page_cgroup is incremented.
  623. */
  624. int mem_cgroup_prepare_migration(struct page *page)
  625. {
  626. struct page_cgroup *pc;
  627. lock_page_cgroup(page);
  628. pc = page_get_page_cgroup(page);
  629. if (pc)
  630. pc->ref_cnt++;
  631. unlock_page_cgroup(page);
  632. return pc != NULL;
  633. }
  634. void mem_cgroup_end_migration(struct page *page)
  635. {
  636. mem_cgroup_uncharge_page(page);
  637. }
  638. /*
  639. * We know both *page* and *newpage* are now not-on-LRU and PG_locked.
  640. * And no race with uncharge() routines because page_cgroup for *page*
  641. * has extra one reference by mem_cgroup_prepare_migration.
  642. */
  643. void mem_cgroup_page_migration(struct page *page, struct page *newpage)
  644. {
  645. struct page_cgroup *pc;
  646. struct mem_cgroup_per_zone *mz;
  647. unsigned long flags;
  648. lock_page_cgroup(page);
  649. pc = page_get_page_cgroup(page);
  650. if (!pc) {
  651. unlock_page_cgroup(page);
  652. return;
  653. }
  654. page_assign_page_cgroup(page, NULL);
  655. unlock_page_cgroup(page);
  656. mz = page_cgroup_zoneinfo(pc);
  657. spin_lock_irqsave(&mz->lru_lock, flags);
  658. __mem_cgroup_remove_list(pc);
  659. spin_unlock_irqrestore(&mz->lru_lock, flags);
  660. pc->page = newpage;
  661. lock_page_cgroup(newpage);
  662. page_assign_page_cgroup(newpage, pc);
  663. unlock_page_cgroup(newpage);
  664. mz = page_cgroup_zoneinfo(pc);
  665. spin_lock_irqsave(&mz->lru_lock, flags);
  666. __mem_cgroup_add_list(pc);
  667. spin_unlock_irqrestore(&mz->lru_lock, flags);
  668. }
  669. /*
  670. * This routine traverse page_cgroup in given list and drop them all.
  671. * This routine ignores page_cgroup->ref_cnt.
  672. * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  673. */
  674. #define FORCE_UNCHARGE_BATCH (128)
  675. static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
  676. struct mem_cgroup_per_zone *mz,
  677. int active)
  678. {
  679. struct page_cgroup *pc;
  680. struct page *page;
  681. int count;
  682. unsigned long flags;
  683. struct list_head *list;
  684. if (active)
  685. list = &mz->active_list;
  686. else
  687. list = &mz->inactive_list;
  688. if (list_empty(list))
  689. return;
  690. retry:
  691. count = FORCE_UNCHARGE_BATCH;
  692. spin_lock_irqsave(&mz->lru_lock, flags);
  693. while (--count && !list_empty(list)) {
  694. pc = list_entry(list->prev, struct page_cgroup, lru);
  695. page = pc->page;
  696. lock_page_cgroup(page);
  697. if (page_get_page_cgroup(page) == pc) {
  698. page_assign_page_cgroup(page, NULL);
  699. unlock_page_cgroup(page);
  700. __mem_cgroup_remove_list(pc);
  701. res_counter_uncharge(&mem->res, PAGE_SIZE);
  702. css_put(&mem->css);
  703. kfree(pc);
  704. } else {
  705. /* racing uncharge: let page go then retry */
  706. unlock_page_cgroup(page);
  707. break;
  708. }
  709. }
  710. spin_unlock_irqrestore(&mz->lru_lock, flags);
  711. if (!list_empty(list)) {
  712. cond_resched();
  713. goto retry;
  714. }
  715. }
  716. /*
  717. * make mem_cgroup's charge to be 0 if there is no task.
  718. * This enables deleting this mem_cgroup.
  719. */
  720. static int mem_cgroup_force_empty(struct mem_cgroup *mem)
  721. {
  722. int ret = -EBUSY;
  723. int node, zid;
  724. css_get(&mem->css);
  725. /*
  726. * page reclaim code (kswapd etc..) will move pages between
  727. * active_list <-> inactive_list while we don't take a lock.
  728. * So, we have to do loop here until all lists are empty.
  729. */
  730. while (mem->res.usage > 0) {
  731. if (atomic_read(&mem->css.cgroup->count) > 0)
  732. goto out;
  733. for_each_node_state(node, N_POSSIBLE)
  734. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  735. struct mem_cgroup_per_zone *mz;
  736. mz = mem_cgroup_zoneinfo(mem, node, zid);
  737. /* drop all page_cgroup in active_list */
  738. mem_cgroup_force_empty_list(mem, mz, 1);
  739. /* drop all page_cgroup in inactive_list */
  740. mem_cgroup_force_empty_list(mem, mz, 0);
  741. }
  742. }
  743. ret = 0;
  744. out:
  745. css_put(&mem->css);
  746. return ret;
  747. }
  748. static int mem_cgroup_write_strategy(char *buf, unsigned long long *tmp)
  749. {
  750. *tmp = memparse(buf, &buf);
  751. if (*buf != '\0')
  752. return -EINVAL;
  753. /*
  754. * Round up the value to the closest page size
  755. */
  756. *tmp = ((*tmp + PAGE_SIZE - 1) >> PAGE_SHIFT) << PAGE_SHIFT;
  757. return 0;
  758. }
  759. static ssize_t mem_cgroup_read(struct cgroup *cont,
  760. struct cftype *cft, struct file *file,
  761. char __user *userbuf, size_t nbytes, loff_t *ppos)
  762. {
  763. return res_counter_read(&mem_cgroup_from_cont(cont)->res,
  764. cft->private, userbuf, nbytes, ppos,
  765. NULL);
  766. }
  767. static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  768. struct file *file, const char __user *userbuf,
  769. size_t nbytes, loff_t *ppos)
  770. {
  771. return res_counter_write(&mem_cgroup_from_cont(cont)->res,
  772. cft->private, userbuf, nbytes, ppos,
  773. mem_cgroup_write_strategy);
  774. }
  775. static ssize_t mem_force_empty_write(struct cgroup *cont,
  776. struct cftype *cft, struct file *file,
  777. const char __user *userbuf,
  778. size_t nbytes, loff_t *ppos)
  779. {
  780. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  781. int ret = mem_cgroup_force_empty(mem);
  782. if (!ret)
  783. ret = nbytes;
  784. return ret;
  785. }
  786. /*
  787. * Note: This should be removed if cgroup supports write-only file.
  788. */
  789. static ssize_t mem_force_empty_read(struct cgroup *cont,
  790. struct cftype *cft,
  791. struct file *file, char __user *userbuf,
  792. size_t nbytes, loff_t *ppos)
  793. {
  794. return -EINVAL;
  795. }
  796. static const struct mem_cgroup_stat_desc {
  797. const char *msg;
  798. u64 unit;
  799. } mem_cgroup_stat_desc[] = {
  800. [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
  801. [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
  802. };
  803. static int mem_control_stat_show(struct seq_file *m, void *arg)
  804. {
  805. struct cgroup *cont = m->private;
  806. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  807. struct mem_cgroup_stat *stat = &mem_cont->stat;
  808. int i;
  809. for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
  810. s64 val;
  811. val = mem_cgroup_read_stat(stat, i);
  812. val *= mem_cgroup_stat_desc[i].unit;
  813. seq_printf(m, "%s %lld\n", mem_cgroup_stat_desc[i].msg,
  814. (long long)val);
  815. }
  816. /* showing # of active pages */
  817. {
  818. unsigned long active, inactive;
  819. inactive = mem_cgroup_get_all_zonestat(mem_cont,
  820. MEM_CGROUP_ZSTAT_INACTIVE);
  821. active = mem_cgroup_get_all_zonestat(mem_cont,
  822. MEM_CGROUP_ZSTAT_ACTIVE);
  823. seq_printf(m, "active %ld\n", (active) * PAGE_SIZE);
  824. seq_printf(m, "inactive %ld\n", (inactive) * PAGE_SIZE);
  825. }
  826. return 0;
  827. }
  828. static const struct file_operations mem_control_stat_file_operations = {
  829. .read = seq_read,
  830. .llseek = seq_lseek,
  831. .release = single_release,
  832. };
  833. static int mem_control_stat_open(struct inode *unused, struct file *file)
  834. {
  835. /* XXX __d_cont */
  836. struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
  837. file->f_op = &mem_control_stat_file_operations;
  838. return single_open(file, mem_control_stat_show, cont);
  839. }
  840. static struct cftype mem_cgroup_files[] = {
  841. {
  842. .name = "usage_in_bytes",
  843. .private = RES_USAGE,
  844. .read = mem_cgroup_read,
  845. },
  846. {
  847. .name = "limit_in_bytes",
  848. .private = RES_LIMIT,
  849. .write = mem_cgroup_write,
  850. .read = mem_cgroup_read,
  851. },
  852. {
  853. .name = "failcnt",
  854. .private = RES_FAILCNT,
  855. .read = mem_cgroup_read,
  856. },
  857. {
  858. .name = "force_empty",
  859. .write = mem_force_empty_write,
  860. .read = mem_force_empty_read,
  861. },
  862. {
  863. .name = "stat",
  864. .open = mem_control_stat_open,
  865. },
  866. };
  867. static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  868. {
  869. struct mem_cgroup_per_node *pn;
  870. struct mem_cgroup_per_zone *mz;
  871. int zone;
  872. /*
  873. * This routine is called against possible nodes.
  874. * But it's BUG to call kmalloc() against offline node.
  875. *
  876. * TODO: this routine can waste much memory for nodes which will
  877. * never be onlined. It's better to use memory hotplug callback
  878. * function.
  879. */
  880. if (node_state(node, N_HIGH_MEMORY))
  881. pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, node);
  882. else
  883. pn = kmalloc(sizeof(*pn), GFP_KERNEL);
  884. if (!pn)
  885. return 1;
  886. mem->info.nodeinfo[node] = pn;
  887. memset(pn, 0, sizeof(*pn));
  888. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  889. mz = &pn->zoneinfo[zone];
  890. INIT_LIST_HEAD(&mz->active_list);
  891. INIT_LIST_HEAD(&mz->inactive_list);
  892. spin_lock_init(&mz->lru_lock);
  893. }
  894. return 0;
  895. }
  896. static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  897. {
  898. kfree(mem->info.nodeinfo[node]);
  899. }
  900. static struct cgroup_subsys_state *
  901. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  902. {
  903. struct mem_cgroup *mem;
  904. int node;
  905. if (unlikely((cont->parent) == NULL)) {
  906. mem = &init_mem_cgroup;
  907. init_mm.mem_cgroup = mem;
  908. } else
  909. mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL);
  910. if (mem == NULL)
  911. return ERR_PTR(-ENOMEM);
  912. res_counter_init(&mem->res);
  913. memset(&mem->info, 0, sizeof(mem->info));
  914. for_each_node_state(node, N_POSSIBLE)
  915. if (alloc_mem_cgroup_per_zone_info(mem, node))
  916. goto free_out;
  917. return &mem->css;
  918. free_out:
  919. for_each_node_state(node, N_POSSIBLE)
  920. free_mem_cgroup_per_zone_info(mem, node);
  921. if (cont->parent != NULL)
  922. kfree(mem);
  923. return ERR_PTR(-ENOMEM);
  924. }
  925. static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
  926. struct cgroup *cont)
  927. {
  928. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  929. mem_cgroup_force_empty(mem);
  930. }
  931. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  932. struct cgroup *cont)
  933. {
  934. int node;
  935. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  936. for_each_node_state(node, N_POSSIBLE)
  937. free_mem_cgroup_per_zone_info(mem, node);
  938. kfree(mem_cgroup_from_cont(cont));
  939. }
  940. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  941. struct cgroup *cont)
  942. {
  943. return cgroup_add_files(cont, ss, mem_cgroup_files,
  944. ARRAY_SIZE(mem_cgroup_files));
  945. }
  946. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  947. struct cgroup *cont,
  948. struct cgroup *old_cont,
  949. struct task_struct *p)
  950. {
  951. struct mm_struct *mm;
  952. struct mem_cgroup *mem, *old_mem;
  953. mm = get_task_mm(p);
  954. if (mm == NULL)
  955. return;
  956. mem = mem_cgroup_from_cont(cont);
  957. old_mem = mem_cgroup_from_cont(old_cont);
  958. if (mem == old_mem)
  959. goto out;
  960. /*
  961. * Only thread group leaders are allowed to migrate, the mm_struct is
  962. * in effect owned by the leader
  963. */
  964. if (p->tgid != p->pid)
  965. goto out;
  966. css_get(&mem->css);
  967. rcu_assign_pointer(mm->mem_cgroup, mem);
  968. css_put(&old_mem->css);
  969. out:
  970. mmput(mm);
  971. }
  972. struct cgroup_subsys mem_cgroup_subsys = {
  973. .name = "memory",
  974. .subsys_id = mem_cgroup_subsys_id,
  975. .create = mem_cgroup_create,
  976. .pre_destroy = mem_cgroup_pre_destroy,
  977. .destroy = mem_cgroup_destroy,
  978. .populate = mem_cgroup_populate,
  979. .attach = mem_cgroup_move_task,
  980. .early_init = 0,
  981. };