memcontrol.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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/page-flags.h>
  24. #include <linux/backing-dev.h>
  25. #include <linux/bit_spinlock.h>
  26. #include <linux/rcupdate.h>
  27. #include <linux/swap.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/fs.h>
  30. struct cgroup_subsys mem_cgroup_subsys;
  31. static const int MEM_CGROUP_RECLAIM_RETRIES = 5;
  32. /*
  33. * The memory controller data structure. The memory controller controls both
  34. * page cache and RSS per cgroup. We would eventually like to provide
  35. * statistics based on the statistics developed by Rik Van Riel for clock-pro,
  36. * to help the administrator determine what knobs to tune.
  37. *
  38. * TODO: Add a water mark for the memory controller. Reclaim will begin when
  39. * we hit the water mark. May be even add a low water mark, such that
  40. * no reclaim occurs from a cgroup at it's low water mark, this is
  41. * a feature that will be implemented much later in the future.
  42. */
  43. struct mem_cgroup {
  44. struct cgroup_subsys_state css;
  45. /*
  46. * the counter to account for memory usage
  47. */
  48. struct res_counter res;
  49. /*
  50. * Per cgroup active and inactive list, similar to the
  51. * per zone LRU lists.
  52. * TODO: Consider making these lists per zone
  53. */
  54. struct list_head active_list;
  55. struct list_head inactive_list;
  56. /*
  57. * spin_lock to protect the per cgroup LRU
  58. */
  59. spinlock_t lru_lock;
  60. };
  61. /*
  62. * We use the lower bit of the page->page_cgroup pointer as a bit spin
  63. * lock. We need to ensure that page->page_cgroup is atleast two
  64. * byte aligned (based on comments from Nick Piggin)
  65. */
  66. #define PAGE_CGROUP_LOCK_BIT 0x0
  67. #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
  68. /*
  69. * A page_cgroup page is associated with every page descriptor. The
  70. * page_cgroup helps us identify information about the cgroup
  71. */
  72. struct page_cgroup {
  73. struct list_head lru; /* per cgroup LRU list */
  74. struct page *page;
  75. struct mem_cgroup *mem_cgroup;
  76. atomic_t ref_cnt; /* Helpful when pages move b/w */
  77. /* mapped and cached states */
  78. };
  79. static inline
  80. struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
  81. {
  82. return container_of(cgroup_subsys_state(cont,
  83. mem_cgroup_subsys_id), struct mem_cgroup,
  84. css);
  85. }
  86. static inline
  87. struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
  88. {
  89. return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
  90. struct mem_cgroup, css);
  91. }
  92. void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p)
  93. {
  94. struct mem_cgroup *mem;
  95. mem = mem_cgroup_from_task(p);
  96. css_get(&mem->css);
  97. mm->mem_cgroup = mem;
  98. }
  99. void mm_free_cgroup(struct mm_struct *mm)
  100. {
  101. css_put(&mm->mem_cgroup->css);
  102. }
  103. static inline int page_cgroup_locked(struct page *page)
  104. {
  105. return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT,
  106. &page->page_cgroup);
  107. }
  108. void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
  109. {
  110. int locked;
  111. /*
  112. * While resetting the page_cgroup we might not hold the
  113. * page_cgroup lock. free_hot_cold_page() is an example
  114. * of such a scenario
  115. */
  116. if (pc)
  117. VM_BUG_ON(!page_cgroup_locked(page));
  118. locked = (page->page_cgroup & PAGE_CGROUP_LOCK);
  119. page->page_cgroup = ((unsigned long)pc | locked);
  120. }
  121. struct page_cgroup *page_get_page_cgroup(struct page *page)
  122. {
  123. return (struct page_cgroup *)
  124. (page->page_cgroup & ~PAGE_CGROUP_LOCK);
  125. }
  126. void __always_inline lock_page_cgroup(struct page *page)
  127. {
  128. bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  129. VM_BUG_ON(!page_cgroup_locked(page));
  130. }
  131. void __always_inline unlock_page_cgroup(struct page *page)
  132. {
  133. bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  134. }
  135. void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
  136. {
  137. if (active)
  138. list_move(&pc->lru, &pc->mem_cgroup->active_list);
  139. else
  140. list_move(&pc->lru, &pc->mem_cgroup->inactive_list);
  141. }
  142. /*
  143. * This routine assumes that the appropriate zone's lru lock is already held
  144. */
  145. void mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
  146. {
  147. struct mem_cgroup *mem;
  148. if (!pc)
  149. return;
  150. mem = pc->mem_cgroup;
  151. spin_lock(&mem->lru_lock);
  152. __mem_cgroup_move_lists(pc, active);
  153. spin_unlock(&mem->lru_lock);
  154. }
  155. unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
  156. struct list_head *dst,
  157. unsigned long *scanned, int order,
  158. int mode, struct zone *z,
  159. struct mem_cgroup *mem_cont,
  160. int active)
  161. {
  162. unsigned long nr_taken = 0;
  163. struct page *page;
  164. unsigned long scan;
  165. LIST_HEAD(pc_list);
  166. struct list_head *src;
  167. struct page_cgroup *pc;
  168. if (active)
  169. src = &mem_cont->active_list;
  170. else
  171. src = &mem_cont->inactive_list;
  172. spin_lock(&mem_cont->lru_lock);
  173. for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
  174. pc = list_entry(src->prev, struct page_cgroup, lru);
  175. page = pc->page;
  176. VM_BUG_ON(!pc);
  177. if (PageActive(page) && !active) {
  178. __mem_cgroup_move_lists(pc, true);
  179. scan--;
  180. continue;
  181. }
  182. if (!PageActive(page) && active) {
  183. __mem_cgroup_move_lists(pc, false);
  184. scan--;
  185. continue;
  186. }
  187. /*
  188. * Reclaim, per zone
  189. * TODO: make the active/inactive lists per zone
  190. */
  191. if (page_zone(page) != z)
  192. continue;
  193. /*
  194. * Check if the meta page went away from under us
  195. */
  196. if (!list_empty(&pc->lru))
  197. list_move(&pc->lru, &pc_list);
  198. else
  199. continue;
  200. if (__isolate_lru_page(page, mode) == 0) {
  201. list_move(&page->lru, dst);
  202. nr_taken++;
  203. }
  204. }
  205. list_splice(&pc_list, src);
  206. spin_unlock(&mem_cont->lru_lock);
  207. *scanned = scan;
  208. return nr_taken;
  209. }
  210. /*
  211. * Charge the memory controller for page usage.
  212. * Return
  213. * 0 if the charge was successful
  214. * < 0 if the cgroup is over its limit
  215. */
  216. int mem_cgroup_charge(struct page *page, struct mm_struct *mm)
  217. {
  218. struct mem_cgroup *mem;
  219. struct page_cgroup *pc, *race_pc;
  220. unsigned long flags;
  221. unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  222. /*
  223. * Should page_cgroup's go to their own slab?
  224. * One could optimize the performance of the charging routine
  225. * by saving a bit in the page_flags and using it as a lock
  226. * to see if the cgroup page already has a page_cgroup associated
  227. * with it
  228. */
  229. retry:
  230. lock_page_cgroup(page);
  231. pc = page_get_page_cgroup(page);
  232. /*
  233. * The page_cgroup exists and the page has already been accounted
  234. */
  235. if (pc) {
  236. if (unlikely(!atomic_inc_not_zero(&pc->ref_cnt))) {
  237. /* this page is under being uncharged ? */
  238. unlock_page_cgroup(page);
  239. cpu_relax();
  240. goto retry;
  241. } else
  242. goto done;
  243. }
  244. unlock_page_cgroup(page);
  245. pc = kzalloc(sizeof(struct page_cgroup), GFP_KERNEL);
  246. if (pc == NULL)
  247. goto err;
  248. rcu_read_lock();
  249. /*
  250. * We always charge the cgroup the mm_struct belongs to
  251. * the mm_struct's mem_cgroup changes on task migration if the
  252. * thread group leader migrates. It's possible that mm is not
  253. * set, if so charge the init_mm (happens for pagecache usage).
  254. */
  255. if (!mm)
  256. mm = &init_mm;
  257. mem = rcu_dereference(mm->mem_cgroup);
  258. /*
  259. * For every charge from the cgroup, increment reference
  260. * count
  261. */
  262. css_get(&mem->css);
  263. rcu_read_unlock();
  264. /*
  265. * If we created the page_cgroup, we should free it on exceeding
  266. * the cgroup limit.
  267. */
  268. while (res_counter_charge(&mem->res, 1)) {
  269. if (try_to_free_mem_cgroup_pages(mem))
  270. continue;
  271. /*
  272. * try_to_free_mem_cgroup_pages() might not give us a full
  273. * picture of reclaim. Some pages are reclaimed and might be
  274. * moved to swap cache or just unmapped from the cgroup.
  275. * Check the limit again to see if the reclaim reduced the
  276. * current usage of the cgroup before giving up
  277. */
  278. if (res_counter_check_under_limit(&mem->res))
  279. continue;
  280. /*
  281. * Since we control both RSS and cache, we end up with a
  282. * very interesting scenario where we end up reclaiming
  283. * memory (essentially RSS), since the memory is pushed
  284. * to swap cache, we eventually end up adding those
  285. * pages back to our list. Hence we give ourselves a
  286. * few chances before we fail
  287. */
  288. else if (nr_retries--) {
  289. congestion_wait(WRITE, HZ/10);
  290. continue;
  291. }
  292. css_put(&mem->css);
  293. goto free_pc;
  294. }
  295. lock_page_cgroup(page);
  296. /*
  297. * Check if somebody else beat us to allocating the page_cgroup
  298. */
  299. race_pc = page_get_page_cgroup(page);
  300. if (race_pc) {
  301. kfree(pc);
  302. pc = race_pc;
  303. atomic_inc(&pc->ref_cnt);
  304. res_counter_uncharge(&mem->res, 1);
  305. css_put(&mem->css);
  306. goto done;
  307. }
  308. atomic_set(&pc->ref_cnt, 1);
  309. pc->mem_cgroup = mem;
  310. pc->page = page;
  311. page_assign_page_cgroup(page, pc);
  312. spin_lock_irqsave(&mem->lru_lock, flags);
  313. list_add(&pc->lru, &mem->active_list);
  314. spin_unlock_irqrestore(&mem->lru_lock, flags);
  315. done:
  316. unlock_page_cgroup(page);
  317. return 0;
  318. free_pc:
  319. kfree(pc);
  320. err:
  321. return -ENOMEM;
  322. }
  323. /*
  324. * Uncharging is always a welcome operation, we never complain, simply
  325. * uncharge.
  326. */
  327. void mem_cgroup_uncharge(struct page_cgroup *pc)
  328. {
  329. struct mem_cgroup *mem;
  330. struct page *page;
  331. unsigned long flags;
  332. if (!pc)
  333. return;
  334. if (atomic_dec_and_test(&pc->ref_cnt)) {
  335. page = pc->page;
  336. lock_page_cgroup(page);
  337. mem = pc->mem_cgroup;
  338. css_put(&mem->css);
  339. page_assign_page_cgroup(page, NULL);
  340. unlock_page_cgroup(page);
  341. res_counter_uncharge(&mem->res, 1);
  342. spin_lock_irqsave(&mem->lru_lock, flags);
  343. list_del_init(&pc->lru);
  344. spin_unlock_irqrestore(&mem->lru_lock, flags);
  345. kfree(pc);
  346. }
  347. }
  348. static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
  349. struct file *file, char __user *userbuf, size_t nbytes,
  350. loff_t *ppos)
  351. {
  352. return res_counter_read(&mem_cgroup_from_cont(cont)->res,
  353. cft->private, userbuf, nbytes, ppos);
  354. }
  355. static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  356. struct file *file, const char __user *userbuf,
  357. size_t nbytes, loff_t *ppos)
  358. {
  359. return res_counter_write(&mem_cgroup_from_cont(cont)->res,
  360. cft->private, userbuf, nbytes, ppos);
  361. }
  362. static struct cftype mem_cgroup_files[] = {
  363. {
  364. .name = "usage",
  365. .private = RES_USAGE,
  366. .read = mem_cgroup_read,
  367. },
  368. {
  369. .name = "limit",
  370. .private = RES_LIMIT,
  371. .write = mem_cgroup_write,
  372. .read = mem_cgroup_read,
  373. },
  374. {
  375. .name = "failcnt",
  376. .private = RES_FAILCNT,
  377. .read = mem_cgroup_read,
  378. },
  379. };
  380. static struct mem_cgroup init_mem_cgroup;
  381. static struct cgroup_subsys_state *
  382. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  383. {
  384. struct mem_cgroup *mem;
  385. if (unlikely((cont->parent) == NULL)) {
  386. mem = &init_mem_cgroup;
  387. init_mm.mem_cgroup = mem;
  388. } else
  389. mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL);
  390. if (mem == NULL)
  391. return NULL;
  392. res_counter_init(&mem->res);
  393. INIT_LIST_HEAD(&mem->active_list);
  394. INIT_LIST_HEAD(&mem->inactive_list);
  395. spin_lock_init(&mem->lru_lock);
  396. return &mem->css;
  397. }
  398. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  399. struct cgroup *cont)
  400. {
  401. kfree(mem_cgroup_from_cont(cont));
  402. }
  403. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  404. struct cgroup *cont)
  405. {
  406. return cgroup_add_files(cont, ss, mem_cgroup_files,
  407. ARRAY_SIZE(mem_cgroup_files));
  408. }
  409. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  410. struct cgroup *cont,
  411. struct cgroup *old_cont,
  412. struct task_struct *p)
  413. {
  414. struct mm_struct *mm;
  415. struct mem_cgroup *mem, *old_mem;
  416. mm = get_task_mm(p);
  417. if (mm == NULL)
  418. return;
  419. mem = mem_cgroup_from_cont(cont);
  420. old_mem = mem_cgroup_from_cont(old_cont);
  421. if (mem == old_mem)
  422. goto out;
  423. /*
  424. * Only thread group leaders are allowed to migrate, the mm_struct is
  425. * in effect owned by the leader
  426. */
  427. if (p->tgid != p->pid)
  428. goto out;
  429. css_get(&mem->css);
  430. rcu_assign_pointer(mm->mem_cgroup, mem);
  431. css_put(&old_mem->css);
  432. out:
  433. mmput(mm);
  434. return;
  435. }
  436. struct cgroup_subsys mem_cgroup_subsys = {
  437. .name = "memory",
  438. .subsys_id = mem_cgroup_subsys_id,
  439. .create = mem_cgroup_create,
  440. .destroy = mem_cgroup_destroy,
  441. .populate = mem_cgroup_populate,
  442. .attach = mem_cgroup_move_task,
  443. .early_init = 1,
  444. };