memcontrol.c 27 KB

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