memcontrol.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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/bit_spinlock.h>
  25. #include <linux/rcupdate.h>
  26. struct cgroup_subsys mem_cgroup_subsys;
  27. /*
  28. * The memory controller data structure. The memory controller controls both
  29. * page cache and RSS per cgroup. We would eventually like to provide
  30. * statistics based on the statistics developed by Rik Van Riel for clock-pro,
  31. * to help the administrator determine what knobs to tune.
  32. *
  33. * TODO: Add a water mark for the memory controller. Reclaim will begin when
  34. * we hit the water mark. May be even add a low water mark, such that
  35. * no reclaim occurs from a cgroup at it's low water mark, this is
  36. * a feature that will be implemented much later in the future.
  37. */
  38. struct mem_cgroup {
  39. struct cgroup_subsys_state css;
  40. /*
  41. * the counter to account for memory usage
  42. */
  43. struct res_counter res;
  44. /*
  45. * Per cgroup active and inactive list, similar to the
  46. * per zone LRU lists.
  47. * TODO: Consider making these lists per zone
  48. */
  49. struct list_head active_list;
  50. struct list_head inactive_list;
  51. };
  52. /*
  53. * We use the lower bit of the page->page_cgroup pointer as a bit spin
  54. * lock. We need to ensure that page->page_cgroup is atleast two
  55. * byte aligned (based on comments from Nick Piggin)
  56. */
  57. #define PAGE_CGROUP_LOCK_BIT 0x0
  58. #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
  59. /*
  60. * A page_cgroup page is associated with every page descriptor. The
  61. * page_cgroup helps us identify information about the cgroup
  62. */
  63. struct page_cgroup {
  64. struct list_head lru; /* per cgroup LRU list */
  65. struct page *page;
  66. struct mem_cgroup *mem_cgroup;
  67. atomic_t ref_cnt; /* Helpful when pages move b/w */
  68. /* mapped and cached states */
  69. };
  70. static inline
  71. struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
  72. {
  73. return container_of(cgroup_subsys_state(cont,
  74. mem_cgroup_subsys_id), struct mem_cgroup,
  75. css);
  76. }
  77. static inline
  78. struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
  79. {
  80. return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
  81. struct mem_cgroup, css);
  82. }
  83. void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p)
  84. {
  85. struct mem_cgroup *mem;
  86. mem = mem_cgroup_from_task(p);
  87. css_get(&mem->css);
  88. mm->mem_cgroup = mem;
  89. }
  90. void mm_free_cgroup(struct mm_struct *mm)
  91. {
  92. css_put(&mm->mem_cgroup->css);
  93. }
  94. static inline int page_cgroup_locked(struct page *page)
  95. {
  96. return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT,
  97. &page->page_cgroup);
  98. }
  99. void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
  100. {
  101. int locked;
  102. /*
  103. * While resetting the page_cgroup we might not hold the
  104. * page_cgroup lock. free_hot_cold_page() is an example
  105. * of such a scenario
  106. */
  107. if (pc)
  108. VM_BUG_ON(!page_cgroup_locked(page));
  109. locked = (page->page_cgroup & PAGE_CGROUP_LOCK);
  110. page->page_cgroup = ((unsigned long)pc | locked);
  111. }
  112. struct page_cgroup *page_get_page_cgroup(struct page *page)
  113. {
  114. return (struct page_cgroup *)
  115. (page->page_cgroup & ~PAGE_CGROUP_LOCK);
  116. }
  117. void __always_inline lock_page_cgroup(struct page *page)
  118. {
  119. bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  120. VM_BUG_ON(!page_cgroup_locked(page));
  121. }
  122. void __always_inline unlock_page_cgroup(struct page *page)
  123. {
  124. bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  125. }
  126. /*
  127. * Charge the memory controller for page usage.
  128. * Return
  129. * 0 if the charge was successful
  130. * < 0 if the cgroup is over its limit
  131. */
  132. int mem_cgroup_charge(struct page *page, struct mm_struct *mm)
  133. {
  134. struct mem_cgroup *mem;
  135. struct page_cgroup *pc, *race_pc;
  136. /*
  137. * Should page_cgroup's go to their own slab?
  138. * One could optimize the performance of the charging routine
  139. * by saving a bit in the page_flags and using it as a lock
  140. * to see if the cgroup page already has a page_cgroup associated
  141. * with it
  142. */
  143. lock_page_cgroup(page);
  144. pc = page_get_page_cgroup(page);
  145. /*
  146. * The page_cgroup exists and the page has already been accounted
  147. */
  148. if (pc) {
  149. atomic_inc(&pc->ref_cnt);
  150. goto done;
  151. }
  152. unlock_page_cgroup(page);
  153. pc = kzalloc(sizeof(struct page_cgroup), GFP_KERNEL);
  154. if (pc == NULL)
  155. goto err;
  156. rcu_read_lock();
  157. /*
  158. * We always charge the cgroup the mm_struct belongs to
  159. * the mm_struct's mem_cgroup changes on task migration if the
  160. * thread group leader migrates. It's possible that mm is not
  161. * set, if so charge the init_mm (happens for pagecache usage).
  162. */
  163. if (!mm)
  164. mm = &init_mm;
  165. mem = rcu_dereference(mm->mem_cgroup);
  166. /*
  167. * For every charge from the cgroup, increment reference
  168. * count
  169. */
  170. css_get(&mem->css);
  171. rcu_read_unlock();
  172. /*
  173. * If we created the page_cgroup, we should free it on exceeding
  174. * the cgroup limit.
  175. */
  176. if (res_counter_charge(&mem->res, 1)) {
  177. css_put(&mem->css);
  178. goto free_pc;
  179. }
  180. lock_page_cgroup(page);
  181. /*
  182. * Check if somebody else beat us to allocating the page_cgroup
  183. */
  184. race_pc = page_get_page_cgroup(page);
  185. if (race_pc) {
  186. kfree(pc);
  187. pc = race_pc;
  188. atomic_inc(&pc->ref_cnt);
  189. res_counter_uncharge(&mem->res, 1);
  190. css_put(&mem->css);
  191. goto done;
  192. }
  193. atomic_set(&pc->ref_cnt, 1);
  194. pc->mem_cgroup = mem;
  195. pc->page = page;
  196. page_assign_page_cgroup(page, pc);
  197. done:
  198. unlock_page_cgroup(page);
  199. return 0;
  200. free_pc:
  201. kfree(pc);
  202. return -ENOMEM;
  203. err:
  204. unlock_page_cgroup(page);
  205. return -ENOMEM;
  206. }
  207. /*
  208. * Uncharging is always a welcome operation, we never complain, simply
  209. * uncharge.
  210. */
  211. void mem_cgroup_uncharge(struct page_cgroup *pc)
  212. {
  213. struct mem_cgroup *mem;
  214. struct page *page;
  215. if (!pc)
  216. return;
  217. if (atomic_dec_and_test(&pc->ref_cnt)) {
  218. page = pc->page;
  219. lock_page_cgroup(page);
  220. mem = pc->mem_cgroup;
  221. css_put(&mem->css);
  222. page_assign_page_cgroup(page, NULL);
  223. unlock_page_cgroup(page);
  224. res_counter_uncharge(&mem->res, 1);
  225. kfree(pc);
  226. }
  227. }
  228. static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
  229. struct file *file, char __user *userbuf, size_t nbytes,
  230. loff_t *ppos)
  231. {
  232. return res_counter_read(&mem_cgroup_from_cont(cont)->res,
  233. cft->private, userbuf, nbytes, ppos);
  234. }
  235. static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  236. struct file *file, const char __user *userbuf,
  237. size_t nbytes, loff_t *ppos)
  238. {
  239. return res_counter_write(&mem_cgroup_from_cont(cont)->res,
  240. cft->private, userbuf, nbytes, ppos);
  241. }
  242. static struct cftype mem_cgroup_files[] = {
  243. {
  244. .name = "usage",
  245. .private = RES_USAGE,
  246. .read = mem_cgroup_read,
  247. },
  248. {
  249. .name = "limit",
  250. .private = RES_LIMIT,
  251. .write = mem_cgroup_write,
  252. .read = mem_cgroup_read,
  253. },
  254. {
  255. .name = "failcnt",
  256. .private = RES_FAILCNT,
  257. .read = mem_cgroup_read,
  258. },
  259. };
  260. static struct mem_cgroup init_mem_cgroup;
  261. static struct cgroup_subsys_state *
  262. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  263. {
  264. struct mem_cgroup *mem;
  265. if (unlikely((cont->parent) == NULL)) {
  266. mem = &init_mem_cgroup;
  267. init_mm.mem_cgroup = mem;
  268. } else
  269. mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL);
  270. if (mem == NULL)
  271. return NULL;
  272. res_counter_init(&mem->res);
  273. INIT_LIST_HEAD(&mem->active_list);
  274. INIT_LIST_HEAD(&mem->inactive_list);
  275. return &mem->css;
  276. }
  277. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  278. struct cgroup *cont)
  279. {
  280. kfree(mem_cgroup_from_cont(cont));
  281. }
  282. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  283. struct cgroup *cont)
  284. {
  285. return cgroup_add_files(cont, ss, mem_cgroup_files,
  286. ARRAY_SIZE(mem_cgroup_files));
  287. }
  288. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  289. struct cgroup *cont,
  290. struct cgroup *old_cont,
  291. struct task_struct *p)
  292. {
  293. struct mm_struct *mm;
  294. struct mem_cgroup *mem, *old_mem;
  295. mm = get_task_mm(p);
  296. if (mm == NULL)
  297. return;
  298. mem = mem_cgroup_from_cont(cont);
  299. old_mem = mem_cgroup_from_cont(old_cont);
  300. if (mem == old_mem)
  301. goto out;
  302. /*
  303. * Only thread group leaders are allowed to migrate, the mm_struct is
  304. * in effect owned by the leader
  305. */
  306. if (p->tgid != p->pid)
  307. goto out;
  308. css_get(&mem->css);
  309. rcu_assign_pointer(mm->mem_cgroup, mem);
  310. css_put(&old_mem->css);
  311. out:
  312. mmput(mm);
  313. return;
  314. }
  315. struct cgroup_subsys mem_cgroup_subsys = {
  316. .name = "memory",
  317. .subsys_id = mem_cgroup_subsys_id,
  318. .create = mem_cgroup_create,
  319. .destroy = mem_cgroup_destroy,
  320. .populate = mem_cgroup_populate,
  321. .attach = mem_cgroup_move_task,
  322. .early_init = 1,
  323. };