Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1 | /* memcontrol.c - Memory Controller |
| 2 | * |
| 3 | * Copyright IBM Corporation, 2007 |
| 4 | * Author Balbir Singh <balbir@linux.vnet.ibm.com> |
| 5 | * |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 6 | * Copyright 2007 OpenVZ SWsoft Inc |
| 7 | * Author: Pavel Emelianov <xemul@openvz.org> |
| 8 | * |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 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 | |
| 20 | #include <linux/res_counter.h> |
| 21 | #include <linux/memcontrol.h> |
| 22 | #include <linux/cgroup.h> |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 23 | #include <linux/mm.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 24 | #include <linux/page-flags.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 25 | #include <linux/backing-dev.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 26 | #include <linux/bit_spinlock.h> |
| 27 | #include <linux/rcupdate.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 28 | #include <linux/swap.h> |
| 29 | #include <linux/spinlock.h> |
| 30 | #include <linux/fs.h> |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 31 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 32 | #include <asm/uaccess.h> |
| 33 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 34 | struct cgroup_subsys mem_cgroup_subsys; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 35 | static const int MEM_CGROUP_RECLAIM_RETRIES = 5; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * The memory controller data structure. The memory controller controls both |
| 39 | * page cache and RSS per cgroup. We would eventually like to provide |
| 40 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, |
| 41 | * to help the administrator determine what knobs to tune. |
| 42 | * |
| 43 | * TODO: Add a water mark for the memory controller. Reclaim will begin when |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 44 | * we hit the water mark. May be even add a low water mark, such that |
| 45 | * no reclaim occurs from a cgroup at it's low water mark, this is |
| 46 | * a feature that will be implemented much later in the future. |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 47 | */ |
| 48 | struct mem_cgroup { |
| 49 | struct cgroup_subsys_state css; |
| 50 | /* |
| 51 | * the counter to account for memory usage |
| 52 | */ |
| 53 | struct res_counter res; |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 54 | /* |
| 55 | * Per cgroup active and inactive list, similar to the |
| 56 | * per zone LRU lists. |
| 57 | * TODO: Consider making these lists per zone |
| 58 | */ |
| 59 | struct list_head active_list; |
| 60 | struct list_head inactive_list; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 61 | /* |
| 62 | * spin_lock to protect the per cgroup LRU |
| 63 | */ |
| 64 | spinlock_t lru_lock; |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 65 | unsigned long control_type; /* control RSS or RSS+Pagecache */ |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | /* |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 69 | * We use the lower bit of the page->page_cgroup pointer as a bit spin |
| 70 | * lock. We need to ensure that page->page_cgroup is atleast two |
| 71 | * byte aligned (based on comments from Nick Piggin) |
| 72 | */ |
| 73 | #define PAGE_CGROUP_LOCK_BIT 0x0 |
| 74 | #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT) |
| 75 | |
| 76 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 77 | * A page_cgroup page is associated with every page descriptor. The |
| 78 | * page_cgroup helps us identify information about the cgroup |
| 79 | */ |
| 80 | struct page_cgroup { |
| 81 | struct list_head lru; /* per cgroup LRU list */ |
| 82 | struct page *page; |
| 83 | struct mem_cgroup *mem_cgroup; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 84 | atomic_t ref_cnt; /* Helpful when pages move b/w */ |
| 85 | /* mapped and cached states */ |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 86 | int flags; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 87 | }; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 88 | #define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */ |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 89 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 90 | enum { |
| 91 | MEM_CGROUP_TYPE_UNSPEC = 0, |
| 92 | MEM_CGROUP_TYPE_MAPPED, |
| 93 | MEM_CGROUP_TYPE_CACHED, |
| 94 | MEM_CGROUP_TYPE_ALL, |
| 95 | MEM_CGROUP_TYPE_MAX, |
| 96 | }; |
| 97 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 98 | enum charge_type { |
| 99 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
| 100 | MEM_CGROUP_CHARGE_TYPE_MAPPED, |
| 101 | }; |
| 102 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 103 | static struct mem_cgroup init_mem_cgroup; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 104 | |
| 105 | static inline |
| 106 | struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
| 107 | { |
| 108 | return container_of(cgroup_subsys_state(cont, |
| 109 | mem_cgroup_subsys_id), struct mem_cgroup, |
| 110 | css); |
| 111 | } |
| 112 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 113 | static inline |
| 114 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
| 115 | { |
| 116 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
| 117 | struct mem_cgroup, css); |
| 118 | } |
| 119 | |
| 120 | void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p) |
| 121 | { |
| 122 | struct mem_cgroup *mem; |
| 123 | |
| 124 | mem = mem_cgroup_from_task(p); |
| 125 | css_get(&mem->css); |
| 126 | mm->mem_cgroup = mem; |
| 127 | } |
| 128 | |
| 129 | void mm_free_cgroup(struct mm_struct *mm) |
| 130 | { |
| 131 | css_put(&mm->mem_cgroup->css); |
| 132 | } |
| 133 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 134 | static inline int page_cgroup_locked(struct page *page) |
| 135 | { |
| 136 | return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, |
| 137 | &page->page_cgroup); |
| 138 | } |
| 139 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 140 | void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc) |
| 141 | { |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 142 | int locked; |
| 143 | |
| 144 | /* |
| 145 | * While resetting the page_cgroup we might not hold the |
| 146 | * page_cgroup lock. free_hot_cold_page() is an example |
| 147 | * of such a scenario |
| 148 | */ |
| 149 | if (pc) |
| 150 | VM_BUG_ON(!page_cgroup_locked(page)); |
| 151 | locked = (page->page_cgroup & PAGE_CGROUP_LOCK); |
| 152 | page->page_cgroup = ((unsigned long)pc | locked); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | struct page_cgroup *page_get_page_cgroup(struct page *page) |
| 156 | { |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 157 | return (struct page_cgroup *) |
| 158 | (page->page_cgroup & ~PAGE_CGROUP_LOCK); |
| 159 | } |
| 160 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 161 | static void __always_inline lock_page_cgroup(struct page *page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 162 | { |
| 163 | bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); |
| 164 | VM_BUG_ON(!page_cgroup_locked(page)); |
| 165 | } |
| 166 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 167 | static void __always_inline unlock_page_cgroup(struct page *page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 168 | { |
| 169 | bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); |
| 170 | } |
| 171 | |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 172 | /* |
| 173 | * Tie new page_cgroup to struct page under lock_page_cgroup() |
| 174 | * This can fail if the page has been tied to a page_cgroup. |
| 175 | * If success, returns 0. |
| 176 | */ |
| 177 | static inline int |
| 178 | page_cgroup_assign_new_page_cgroup(struct page *page, struct page_cgroup *pc) |
| 179 | { |
| 180 | int ret = 0; |
| 181 | |
| 182 | lock_page_cgroup(page); |
| 183 | if (!page_get_page_cgroup(page)) |
| 184 | page_assign_page_cgroup(page, pc); |
| 185 | else /* A page is tied to other pc. */ |
| 186 | ret = 1; |
| 187 | unlock_page_cgroup(page); |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Clear page->page_cgroup member under lock_page_cgroup(). |
| 193 | * If given "pc" value is different from one page->page_cgroup, |
| 194 | * page->cgroup is not cleared. |
| 195 | * Returns a value of page->page_cgroup at lock taken. |
| 196 | * A can can detect failure of clearing by following |
| 197 | * clear_page_cgroup(page, pc) == pc |
| 198 | */ |
| 199 | |
| 200 | static inline struct page_cgroup * |
| 201 | clear_page_cgroup(struct page *page, struct page_cgroup *pc) |
| 202 | { |
| 203 | struct page_cgroup *ret; |
| 204 | /* lock and clear */ |
| 205 | lock_page_cgroup(page); |
| 206 | ret = page_get_page_cgroup(page); |
| 207 | if (likely(ret == pc)) |
| 208 | page_assign_page_cgroup(page, NULL); |
| 209 | unlock_page_cgroup(page); |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 214 | static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 215 | { |
| 216 | if (active) |
| 217 | list_move(&pc->lru, &pc->mem_cgroup->active_list); |
| 218 | else |
| 219 | list_move(&pc->lru, &pc->mem_cgroup->inactive_list); |
| 220 | } |
| 221 | |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 222 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
| 223 | { |
| 224 | int ret; |
| 225 | |
| 226 | task_lock(task); |
| 227 | ret = task->mm && mm_cgroup(task->mm) == mem; |
| 228 | task_unlock(task); |
| 229 | return ret; |
| 230 | } |
| 231 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 232 | /* |
| 233 | * This routine assumes that the appropriate zone's lru lock is already held |
| 234 | */ |
| 235 | void mem_cgroup_move_lists(struct page_cgroup *pc, bool active) |
| 236 | { |
| 237 | struct mem_cgroup *mem; |
| 238 | if (!pc) |
| 239 | return; |
| 240 | |
| 241 | mem = pc->mem_cgroup; |
| 242 | |
| 243 | spin_lock(&mem->lru_lock); |
| 244 | __mem_cgroup_move_lists(pc, active); |
| 245 | spin_unlock(&mem->lru_lock); |
| 246 | } |
| 247 | |
| 248 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 249 | struct list_head *dst, |
| 250 | unsigned long *scanned, int order, |
| 251 | int mode, struct zone *z, |
| 252 | struct mem_cgroup *mem_cont, |
| 253 | int active) |
| 254 | { |
| 255 | unsigned long nr_taken = 0; |
| 256 | struct page *page; |
| 257 | unsigned long scan; |
| 258 | LIST_HEAD(pc_list); |
| 259 | struct list_head *src; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 260 | struct page_cgroup *pc, *tmp; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 261 | |
| 262 | if (active) |
| 263 | src = &mem_cont->active_list; |
| 264 | else |
| 265 | src = &mem_cont->inactive_list; |
| 266 | |
| 267 | spin_lock(&mem_cont->lru_lock); |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 268 | scan = 0; |
| 269 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 270 | if (scan >= nr_to_scan) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 271 | break; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 272 | page = pc->page; |
| 273 | VM_BUG_ON(!pc); |
| 274 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 275 | if (unlikely(!PageLRU(page))) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 276 | continue; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 277 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 278 | if (PageActive(page) && !active) { |
| 279 | __mem_cgroup_move_lists(pc, true); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 280 | continue; |
| 281 | } |
| 282 | if (!PageActive(page) && active) { |
| 283 | __mem_cgroup_move_lists(pc, false); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 284 | continue; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Reclaim, per zone |
| 289 | * TODO: make the active/inactive lists per zone |
| 290 | */ |
| 291 | if (page_zone(page) != z) |
| 292 | continue; |
| 293 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 294 | scan++; |
| 295 | list_move(&pc->lru, &pc_list); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 296 | |
| 297 | if (__isolate_lru_page(page, mode) == 0) { |
| 298 | list_move(&page->lru, dst); |
| 299 | nr_taken++; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | list_splice(&pc_list, src); |
| 304 | spin_unlock(&mem_cont->lru_lock); |
| 305 | |
| 306 | *scanned = scan; |
| 307 | return nr_taken; |
| 308 | } |
| 309 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 310 | /* |
| 311 | * Charge the memory controller for page usage. |
| 312 | * Return |
| 313 | * 0 if the charge was successful |
| 314 | * < 0 if the cgroup is over its limit |
| 315 | */ |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 316 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, |
| 317 | gfp_t gfp_mask, enum charge_type ctype) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 318 | { |
| 319 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 320 | struct page_cgroup *pc; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 321 | unsigned long flags; |
| 322 | unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 323 | |
| 324 | /* |
| 325 | * Should page_cgroup's go to their own slab? |
| 326 | * One could optimize the performance of the charging routine |
| 327 | * by saving a bit in the page_flags and using it as a lock |
| 328 | * to see if the cgroup page already has a page_cgroup associated |
| 329 | * with it |
| 330 | */ |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 331 | retry: |
Hugh Dickins | 8236955 | 2008-02-07 00:14:22 -0800 | [diff] [blame^] | 332 | if (page) { |
| 333 | lock_page_cgroup(page); |
| 334 | pc = page_get_page_cgroup(page); |
| 335 | /* |
| 336 | * The page_cgroup exists and |
| 337 | * the page has already been accounted. |
| 338 | */ |
| 339 | if (pc) { |
| 340 | if (unlikely(!atomic_inc_not_zero(&pc->ref_cnt))) { |
| 341 | /* this page is under being uncharged ? */ |
| 342 | unlock_page_cgroup(page); |
| 343 | cpu_relax(); |
| 344 | goto retry; |
| 345 | } else { |
| 346 | unlock_page_cgroup(page); |
| 347 | goto done; |
| 348 | } |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 349 | } |
Hugh Dickins | 8236955 | 2008-02-07 00:14:22 -0800 | [diff] [blame^] | 350 | unlock_page_cgroup(page); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 351 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 352 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 353 | pc = kzalloc(sizeof(struct page_cgroup), gfp_mask); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 354 | if (pc == NULL) |
| 355 | goto err; |
| 356 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 357 | /* |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 358 | * We always charge the cgroup the mm_struct belongs to. |
| 359 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 360 | * thread group leader migrates. It's possible that mm is not |
| 361 | * set, if so charge the init_mm (happens for pagecache usage). |
| 362 | */ |
| 363 | if (!mm) |
| 364 | mm = &init_mm; |
| 365 | |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 366 | rcu_read_lock(); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 367 | mem = rcu_dereference(mm->mem_cgroup); |
| 368 | /* |
| 369 | * For every charge from the cgroup, increment reference |
| 370 | * count |
| 371 | */ |
| 372 | css_get(&mem->css); |
| 373 | rcu_read_unlock(); |
| 374 | |
| 375 | /* |
| 376 | * If we created the page_cgroup, we should free it on exceeding |
| 377 | * the cgroup limit. |
| 378 | */ |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 379 | while (res_counter_charge(&mem->res, PAGE_SIZE)) { |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 380 | if (!(gfp_mask & __GFP_WAIT)) |
| 381 | goto out; |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 382 | |
| 383 | if (try_to_free_mem_cgroup_pages(mem, gfp_mask)) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 384 | continue; |
| 385 | |
| 386 | /* |
| 387 | * try_to_free_mem_cgroup_pages() might not give us a full |
| 388 | * picture of reclaim. Some pages are reclaimed and might be |
| 389 | * moved to swap cache or just unmapped from the cgroup. |
| 390 | * Check the limit again to see if the reclaim reduced the |
| 391 | * current usage of the cgroup before giving up |
| 392 | */ |
| 393 | if (res_counter_check_under_limit(&mem->res)) |
| 394 | continue; |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 395 | |
| 396 | if (!nr_retries--) { |
| 397 | mem_cgroup_out_of_memory(mem, gfp_mask); |
| 398 | goto out; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 399 | } |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 400 | congestion_wait(WRITE, HZ/10); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 403 | atomic_set(&pc->ref_cnt, 1); |
| 404 | pc->mem_cgroup = mem; |
| 405 | pc->page = page; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 406 | pc->flags = 0; |
| 407 | if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE) |
| 408 | pc->flags |= PAGE_CGROUP_FLAG_CACHE; |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 409 | |
Hugh Dickins | 8236955 | 2008-02-07 00:14:22 -0800 | [diff] [blame^] | 410 | if (!page || page_cgroup_assign_new_page_cgroup(page, pc)) { |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 411 | /* |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 412 | * Another charge has been added to this page already. |
| 413 | * We take lock_page_cgroup(page) again and read |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 414 | * page->cgroup, increment refcnt.... just retry is OK. |
| 415 | */ |
| 416 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 417 | css_put(&mem->css); |
| 418 | kfree(pc); |
Hugh Dickins | 8236955 | 2008-02-07 00:14:22 -0800 | [diff] [blame^] | 419 | if (!page) |
| 420 | goto done; |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 421 | goto retry; |
| 422 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 423 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 424 | spin_lock_irqsave(&mem->lru_lock, flags); |
| 425 | list_add(&pc->lru, &mem->active_list); |
| 426 | spin_unlock_irqrestore(&mem->lru_lock, flags); |
| 427 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 428 | done: |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 429 | return 0; |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 430 | out: |
| 431 | css_put(&mem->css); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 432 | kfree(pc); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 433 | err: |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 434 | return -ENOMEM; |
| 435 | } |
| 436 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 437 | int mem_cgroup_charge(struct page *page, struct mm_struct *mm, |
| 438 | gfp_t gfp_mask) |
| 439 | { |
| 440 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
| 441 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 442 | } |
| 443 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 444 | /* |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 445 | * See if the cached pages should be charged at all? |
| 446 | */ |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 447 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 448 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 449 | { |
Balbir Singh | ac44d35 | 2008-02-07 00:14:18 -0800 | [diff] [blame] | 450 | int ret = 0; |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 451 | struct mem_cgroup *mem; |
| 452 | if (!mm) |
| 453 | mm = &init_mm; |
| 454 | |
Balbir Singh | ac44d35 | 2008-02-07 00:14:18 -0800 | [diff] [blame] | 455 | rcu_read_lock(); |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 456 | mem = rcu_dereference(mm->mem_cgroup); |
Balbir Singh | ac44d35 | 2008-02-07 00:14:18 -0800 | [diff] [blame] | 457 | css_get(&mem->css); |
| 458 | rcu_read_unlock(); |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 459 | if (mem->control_type == MEM_CGROUP_TYPE_ALL) |
Balbir Singh | ac44d35 | 2008-02-07 00:14:18 -0800 | [diff] [blame] | 460 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 461 | MEM_CGROUP_CHARGE_TYPE_CACHE); |
Balbir Singh | ac44d35 | 2008-02-07 00:14:18 -0800 | [diff] [blame] | 462 | css_put(&mem->css); |
| 463 | return ret; |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 467 | * Uncharging is always a welcome operation, we never complain, simply |
| 468 | * uncharge. |
| 469 | */ |
| 470 | void mem_cgroup_uncharge(struct page_cgroup *pc) |
| 471 | { |
| 472 | struct mem_cgroup *mem; |
| 473 | struct page *page; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 474 | unsigned long flags; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 475 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 476 | /* |
| 477 | * This can handle cases when a page is not charged at all and we |
| 478 | * are switching between handling the control_type. |
| 479 | */ |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 480 | if (!pc) |
| 481 | return; |
| 482 | |
| 483 | if (atomic_dec_and_test(&pc->ref_cnt)) { |
| 484 | page = pc->page; |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 485 | /* |
| 486 | * get page->cgroup and clear it under lock. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 487 | * force_empty can drop page->cgroup without checking refcnt. |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 488 | */ |
| 489 | if (clear_page_cgroup(page, pc) == pc) { |
| 490 | mem = pc->mem_cgroup; |
| 491 | css_put(&mem->css); |
| 492 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 493 | spin_lock_irqsave(&mem->lru_lock, flags); |
| 494 | list_del_init(&pc->lru); |
| 495 | spin_unlock_irqrestore(&mem->lru_lock, flags); |
| 496 | kfree(pc); |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 497 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 498 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 499 | } |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 500 | /* |
| 501 | * Returns non-zero if a page (under migration) has valid page_cgroup member. |
| 502 | * Refcnt of page_cgroup is incremented. |
| 503 | */ |
| 504 | |
| 505 | int mem_cgroup_prepare_migration(struct page *page) |
| 506 | { |
| 507 | struct page_cgroup *pc; |
| 508 | int ret = 0; |
| 509 | lock_page_cgroup(page); |
| 510 | pc = page_get_page_cgroup(page); |
| 511 | if (pc && atomic_inc_not_zero(&pc->ref_cnt)) |
| 512 | ret = 1; |
| 513 | unlock_page_cgroup(page); |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | void mem_cgroup_end_migration(struct page *page) |
| 518 | { |
| 519 | struct page_cgroup *pc = page_get_page_cgroup(page); |
| 520 | mem_cgroup_uncharge(pc); |
| 521 | } |
| 522 | /* |
| 523 | * We know both *page* and *newpage* are now not-on-LRU and Pg_locked. |
| 524 | * And no race with uncharge() routines because page_cgroup for *page* |
| 525 | * has extra one reference by mem_cgroup_prepare_migration. |
| 526 | */ |
| 527 | |
| 528 | void mem_cgroup_page_migration(struct page *page, struct page *newpage) |
| 529 | { |
| 530 | struct page_cgroup *pc; |
| 531 | retry: |
| 532 | pc = page_get_page_cgroup(page); |
| 533 | if (!pc) |
| 534 | return; |
| 535 | if (clear_page_cgroup(page, pc) != pc) |
| 536 | goto retry; |
| 537 | pc->page = newpage; |
| 538 | lock_page_cgroup(newpage); |
| 539 | page_assign_page_cgroup(newpage, pc); |
| 540 | unlock_page_cgroup(newpage); |
| 541 | return; |
| 542 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 543 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 544 | /* |
| 545 | * This routine traverse page_cgroup in given list and drop them all. |
| 546 | * This routine ignores page_cgroup->ref_cnt. |
| 547 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
| 548 | */ |
| 549 | #define FORCE_UNCHARGE_BATCH (128) |
| 550 | static void |
| 551 | mem_cgroup_force_empty_list(struct mem_cgroup *mem, struct list_head *list) |
| 552 | { |
| 553 | struct page_cgroup *pc; |
| 554 | struct page *page; |
| 555 | int count; |
| 556 | unsigned long flags; |
| 557 | |
| 558 | retry: |
| 559 | count = FORCE_UNCHARGE_BATCH; |
| 560 | spin_lock_irqsave(&mem->lru_lock, flags); |
| 561 | |
| 562 | while (--count && !list_empty(list)) { |
| 563 | pc = list_entry(list->prev, struct page_cgroup, lru); |
| 564 | page = pc->page; |
| 565 | /* Avoid race with charge */ |
| 566 | atomic_set(&pc->ref_cnt, 0); |
| 567 | if (clear_page_cgroup(page, pc) == pc) { |
| 568 | css_put(&mem->css); |
| 569 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 570 | list_del_init(&pc->lru); |
| 571 | kfree(pc); |
| 572 | } else /* being uncharged ? ...do relax */ |
| 573 | break; |
| 574 | } |
| 575 | spin_unlock_irqrestore(&mem->lru_lock, flags); |
| 576 | if (!list_empty(list)) { |
| 577 | cond_resched(); |
| 578 | goto retry; |
| 579 | } |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * make mem_cgroup's charge to be 0 if there is no task. |
| 585 | * This enables deleting this mem_cgroup. |
| 586 | */ |
| 587 | |
| 588 | int mem_cgroup_force_empty(struct mem_cgroup *mem) |
| 589 | { |
| 590 | int ret = -EBUSY; |
| 591 | css_get(&mem->css); |
| 592 | /* |
| 593 | * page reclaim code (kswapd etc..) will move pages between |
| 594 | ` * active_list <-> inactive_list while we don't take a lock. |
| 595 | * So, we have to do loop here until all lists are empty. |
| 596 | */ |
| 597 | while (!(list_empty(&mem->active_list) && |
| 598 | list_empty(&mem->inactive_list))) { |
| 599 | if (atomic_read(&mem->css.cgroup->count) > 0) |
| 600 | goto out; |
| 601 | /* drop all page_cgroup in active_list */ |
| 602 | mem_cgroup_force_empty_list(mem, &mem->active_list); |
| 603 | /* drop all page_cgroup in inactive_list */ |
| 604 | mem_cgroup_force_empty_list(mem, &mem->inactive_list); |
| 605 | } |
| 606 | ret = 0; |
| 607 | out: |
| 608 | css_put(&mem->css); |
| 609 | return ret; |
| 610 | } |
| 611 | |
| 612 | |
| 613 | |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 614 | int mem_cgroup_write_strategy(char *buf, unsigned long long *tmp) |
| 615 | { |
| 616 | *tmp = memparse(buf, &buf); |
| 617 | if (*buf != '\0') |
| 618 | return -EINVAL; |
| 619 | |
| 620 | /* |
| 621 | * Round up the value to the closest page size |
| 622 | */ |
| 623 | *tmp = ((*tmp + PAGE_SIZE - 1) >> PAGE_SHIFT) << PAGE_SHIFT; |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static ssize_t mem_cgroup_read(struct cgroup *cont, |
| 628 | struct cftype *cft, struct file *file, |
| 629 | char __user *userbuf, size_t nbytes, loff_t *ppos) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 630 | { |
| 631 | return res_counter_read(&mem_cgroup_from_cont(cont)->res, |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 632 | cft->private, userbuf, nbytes, ppos, |
| 633 | NULL); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 637 | struct file *file, const char __user *userbuf, |
| 638 | size_t nbytes, loff_t *ppos) |
| 639 | { |
| 640 | return res_counter_write(&mem_cgroup_from_cont(cont)->res, |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 641 | cft->private, userbuf, nbytes, ppos, |
| 642 | mem_cgroup_write_strategy); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 643 | } |
| 644 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 645 | static ssize_t mem_control_type_write(struct cgroup *cont, |
| 646 | struct cftype *cft, struct file *file, |
| 647 | const char __user *userbuf, |
| 648 | size_t nbytes, loff_t *pos) |
| 649 | { |
| 650 | int ret; |
| 651 | char *buf, *end; |
| 652 | unsigned long tmp; |
| 653 | struct mem_cgroup *mem; |
| 654 | |
| 655 | mem = mem_cgroup_from_cont(cont); |
| 656 | buf = kmalloc(nbytes + 1, GFP_KERNEL); |
| 657 | ret = -ENOMEM; |
| 658 | if (buf == NULL) |
| 659 | goto out; |
| 660 | |
| 661 | buf[nbytes] = 0; |
| 662 | ret = -EFAULT; |
| 663 | if (copy_from_user(buf, userbuf, nbytes)) |
| 664 | goto out_free; |
| 665 | |
| 666 | ret = -EINVAL; |
| 667 | tmp = simple_strtoul(buf, &end, 10); |
| 668 | if (*end != '\0') |
| 669 | goto out_free; |
| 670 | |
| 671 | if (tmp <= MEM_CGROUP_TYPE_UNSPEC || tmp >= MEM_CGROUP_TYPE_MAX) |
| 672 | goto out_free; |
| 673 | |
| 674 | mem->control_type = tmp; |
| 675 | ret = nbytes; |
| 676 | out_free: |
| 677 | kfree(buf); |
| 678 | out: |
| 679 | return ret; |
| 680 | } |
| 681 | |
| 682 | static ssize_t mem_control_type_read(struct cgroup *cont, |
| 683 | struct cftype *cft, |
| 684 | struct file *file, char __user *userbuf, |
| 685 | size_t nbytes, loff_t *ppos) |
| 686 | { |
| 687 | unsigned long val; |
| 688 | char buf[64], *s; |
| 689 | struct mem_cgroup *mem; |
| 690 | |
| 691 | mem = mem_cgroup_from_cont(cont); |
| 692 | s = buf; |
| 693 | val = mem->control_type; |
| 694 | s += sprintf(s, "%lu\n", val); |
| 695 | return simple_read_from_buffer((void __user *)userbuf, nbytes, |
| 696 | ppos, buf, s - buf); |
| 697 | } |
| 698 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 699 | |
| 700 | static ssize_t mem_force_empty_write(struct cgroup *cont, |
| 701 | struct cftype *cft, struct file *file, |
| 702 | const char __user *userbuf, |
| 703 | size_t nbytes, loff_t *ppos) |
| 704 | { |
| 705 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 706 | int ret; |
| 707 | ret = mem_cgroup_force_empty(mem); |
| 708 | if (!ret) |
| 709 | ret = nbytes; |
| 710 | return ret; |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * Note: This should be removed if cgroup supports write-only file. |
| 715 | */ |
| 716 | |
| 717 | static ssize_t mem_force_empty_read(struct cgroup *cont, |
| 718 | struct cftype *cft, |
| 719 | struct file *file, char __user *userbuf, |
| 720 | size_t nbytes, loff_t *ppos) |
| 721 | { |
| 722 | return -EINVAL; |
| 723 | } |
| 724 | |
| 725 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 726 | static struct cftype mem_cgroup_files[] = { |
| 727 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 728 | .name = "usage_in_bytes", |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 729 | .private = RES_USAGE, |
| 730 | .read = mem_cgroup_read, |
| 731 | }, |
| 732 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 733 | .name = "limit_in_bytes", |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 734 | .private = RES_LIMIT, |
| 735 | .write = mem_cgroup_write, |
| 736 | .read = mem_cgroup_read, |
| 737 | }, |
| 738 | { |
| 739 | .name = "failcnt", |
| 740 | .private = RES_FAILCNT, |
| 741 | .read = mem_cgroup_read, |
| 742 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 743 | { |
| 744 | .name = "control_type", |
| 745 | .write = mem_control_type_write, |
| 746 | .read = mem_control_type_read, |
| 747 | }, |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 748 | { |
| 749 | .name = "force_empty", |
| 750 | .write = mem_force_empty_write, |
| 751 | .read = mem_force_empty_read, |
| 752 | }, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 753 | }; |
| 754 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 755 | static struct mem_cgroup init_mem_cgroup; |
| 756 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 757 | static struct cgroup_subsys_state * |
| 758 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 759 | { |
| 760 | struct mem_cgroup *mem; |
| 761 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 762 | if (unlikely((cont->parent) == NULL)) { |
| 763 | mem = &init_mem_cgroup; |
| 764 | init_mm.mem_cgroup = mem; |
| 765 | } else |
| 766 | mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL); |
| 767 | |
| 768 | if (mem == NULL) |
| 769 | return NULL; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 770 | |
| 771 | res_counter_init(&mem->res); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 772 | INIT_LIST_HEAD(&mem->active_list); |
| 773 | INIT_LIST_HEAD(&mem->inactive_list); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 774 | spin_lock_init(&mem->lru_lock); |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 775 | mem->control_type = MEM_CGROUP_TYPE_ALL; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 776 | return &mem->css; |
| 777 | } |
| 778 | |
| 779 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 780 | struct cgroup *cont) |
| 781 | { |
| 782 | kfree(mem_cgroup_from_cont(cont)); |
| 783 | } |
| 784 | |
| 785 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 786 | struct cgroup *cont) |
| 787 | { |
| 788 | return cgroup_add_files(cont, ss, mem_cgroup_files, |
| 789 | ARRAY_SIZE(mem_cgroup_files)); |
| 790 | } |
| 791 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 792 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 793 | struct cgroup *cont, |
| 794 | struct cgroup *old_cont, |
| 795 | struct task_struct *p) |
| 796 | { |
| 797 | struct mm_struct *mm; |
| 798 | struct mem_cgroup *mem, *old_mem; |
| 799 | |
| 800 | mm = get_task_mm(p); |
| 801 | if (mm == NULL) |
| 802 | return; |
| 803 | |
| 804 | mem = mem_cgroup_from_cont(cont); |
| 805 | old_mem = mem_cgroup_from_cont(old_cont); |
| 806 | |
| 807 | if (mem == old_mem) |
| 808 | goto out; |
| 809 | |
| 810 | /* |
| 811 | * Only thread group leaders are allowed to migrate, the mm_struct is |
| 812 | * in effect owned by the leader |
| 813 | */ |
| 814 | if (p->tgid != p->pid) |
| 815 | goto out; |
| 816 | |
| 817 | css_get(&mem->css); |
| 818 | rcu_assign_pointer(mm->mem_cgroup, mem); |
| 819 | css_put(&old_mem->css); |
| 820 | |
| 821 | out: |
| 822 | mmput(mm); |
| 823 | return; |
| 824 | } |
| 825 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 826 | struct cgroup_subsys mem_cgroup_subsys = { |
| 827 | .name = "memory", |
| 828 | .subsys_id = mem_cgroup_subsys_id, |
| 829 | .create = mem_cgroup_create, |
| 830 | .destroy = mem_cgroup_destroy, |
| 831 | .populate = mem_cgroup_populate, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 832 | .attach = mem_cgroup_move_task, |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 833 | .early_init = 1, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 834 | }; |