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 */ |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 86 | }; |
| 87 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 88 | enum { |
| 89 | MEM_CGROUP_TYPE_UNSPEC = 0, |
| 90 | MEM_CGROUP_TYPE_MAPPED, |
| 91 | MEM_CGROUP_TYPE_CACHED, |
| 92 | MEM_CGROUP_TYPE_ALL, |
| 93 | MEM_CGROUP_TYPE_MAX, |
| 94 | }; |
| 95 | |
| 96 | static struct mem_cgroup init_mem_cgroup; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 97 | |
| 98 | static inline |
| 99 | struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
| 100 | { |
| 101 | return container_of(cgroup_subsys_state(cont, |
| 102 | mem_cgroup_subsys_id), struct mem_cgroup, |
| 103 | css); |
| 104 | } |
| 105 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 106 | static inline |
| 107 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
| 108 | { |
| 109 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
| 110 | struct mem_cgroup, css); |
| 111 | } |
| 112 | |
| 113 | void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p) |
| 114 | { |
| 115 | struct mem_cgroup *mem; |
| 116 | |
| 117 | mem = mem_cgroup_from_task(p); |
| 118 | css_get(&mem->css); |
| 119 | mm->mem_cgroup = mem; |
| 120 | } |
| 121 | |
| 122 | void mm_free_cgroup(struct mm_struct *mm) |
| 123 | { |
| 124 | css_put(&mm->mem_cgroup->css); |
| 125 | } |
| 126 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 127 | static inline int page_cgroup_locked(struct page *page) |
| 128 | { |
| 129 | return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, |
| 130 | &page->page_cgroup); |
| 131 | } |
| 132 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 133 | void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc) |
| 134 | { |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 135 | int locked; |
| 136 | |
| 137 | /* |
| 138 | * While resetting the page_cgroup we might not hold the |
| 139 | * page_cgroup lock. free_hot_cold_page() is an example |
| 140 | * of such a scenario |
| 141 | */ |
| 142 | if (pc) |
| 143 | VM_BUG_ON(!page_cgroup_locked(page)); |
| 144 | locked = (page->page_cgroup & PAGE_CGROUP_LOCK); |
| 145 | page->page_cgroup = ((unsigned long)pc | locked); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | struct page_cgroup *page_get_page_cgroup(struct page *page) |
| 149 | { |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 150 | return (struct page_cgroup *) |
| 151 | (page->page_cgroup & ~PAGE_CGROUP_LOCK); |
| 152 | } |
| 153 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 154 | static void __always_inline lock_page_cgroup(struct page *page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 155 | { |
| 156 | bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); |
| 157 | VM_BUG_ON(!page_cgroup_locked(page)); |
| 158 | } |
| 159 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 160 | static void __always_inline unlock_page_cgroup(struct page *page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 161 | { |
| 162 | bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); |
| 163 | } |
| 164 | |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 165 | /* |
| 166 | * Tie new page_cgroup to struct page under lock_page_cgroup() |
| 167 | * This can fail if the page has been tied to a page_cgroup. |
| 168 | * If success, returns 0. |
| 169 | */ |
| 170 | static inline int |
| 171 | page_cgroup_assign_new_page_cgroup(struct page *page, struct page_cgroup *pc) |
| 172 | { |
| 173 | int ret = 0; |
| 174 | |
| 175 | lock_page_cgroup(page); |
| 176 | if (!page_get_page_cgroup(page)) |
| 177 | page_assign_page_cgroup(page, pc); |
| 178 | else /* A page is tied to other pc. */ |
| 179 | ret = 1; |
| 180 | unlock_page_cgroup(page); |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Clear page->page_cgroup member under lock_page_cgroup(). |
| 186 | * If given "pc" value is different from one page->page_cgroup, |
| 187 | * page->cgroup is not cleared. |
| 188 | * Returns a value of page->page_cgroup at lock taken. |
| 189 | * A can can detect failure of clearing by following |
| 190 | * clear_page_cgroup(page, pc) == pc |
| 191 | */ |
| 192 | |
| 193 | static inline struct page_cgroup * |
| 194 | clear_page_cgroup(struct page *page, struct page_cgroup *pc) |
| 195 | { |
| 196 | struct page_cgroup *ret; |
| 197 | /* lock and clear */ |
| 198 | lock_page_cgroup(page); |
| 199 | ret = page_get_page_cgroup(page); |
| 200 | if (likely(ret == pc)) |
| 201 | page_assign_page_cgroup(page, NULL); |
| 202 | unlock_page_cgroup(page); |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 207 | static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 208 | { |
| 209 | if (active) |
| 210 | list_move(&pc->lru, &pc->mem_cgroup->active_list); |
| 211 | else |
| 212 | list_move(&pc->lru, &pc->mem_cgroup->inactive_list); |
| 213 | } |
| 214 | |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 215 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
| 216 | { |
| 217 | int ret; |
| 218 | |
| 219 | task_lock(task); |
| 220 | ret = task->mm && mm_cgroup(task->mm) == mem; |
| 221 | task_unlock(task); |
| 222 | return ret; |
| 223 | } |
| 224 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 225 | /* |
| 226 | * This routine assumes that the appropriate zone's lru lock is already held |
| 227 | */ |
| 228 | void mem_cgroup_move_lists(struct page_cgroup *pc, bool active) |
| 229 | { |
| 230 | struct mem_cgroup *mem; |
| 231 | if (!pc) |
| 232 | return; |
| 233 | |
| 234 | mem = pc->mem_cgroup; |
| 235 | |
| 236 | spin_lock(&mem->lru_lock); |
| 237 | __mem_cgroup_move_lists(pc, active); |
| 238 | spin_unlock(&mem->lru_lock); |
| 239 | } |
| 240 | |
| 241 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 242 | struct list_head *dst, |
| 243 | unsigned long *scanned, int order, |
| 244 | int mode, struct zone *z, |
| 245 | struct mem_cgroup *mem_cont, |
| 246 | int active) |
| 247 | { |
| 248 | unsigned long nr_taken = 0; |
| 249 | struct page *page; |
| 250 | unsigned long scan; |
| 251 | LIST_HEAD(pc_list); |
| 252 | struct list_head *src; |
| 253 | struct page_cgroup *pc; |
| 254 | |
| 255 | if (active) |
| 256 | src = &mem_cont->active_list; |
| 257 | else |
| 258 | src = &mem_cont->inactive_list; |
| 259 | |
| 260 | spin_lock(&mem_cont->lru_lock); |
| 261 | for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { |
| 262 | pc = list_entry(src->prev, struct page_cgroup, lru); |
| 263 | page = pc->page; |
| 264 | VM_BUG_ON(!pc); |
| 265 | |
| 266 | if (PageActive(page) && !active) { |
| 267 | __mem_cgroup_move_lists(pc, true); |
| 268 | scan--; |
| 269 | continue; |
| 270 | } |
| 271 | if (!PageActive(page) && active) { |
| 272 | __mem_cgroup_move_lists(pc, false); |
| 273 | scan--; |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Reclaim, per zone |
| 279 | * TODO: make the active/inactive lists per zone |
| 280 | */ |
| 281 | if (page_zone(page) != z) |
| 282 | continue; |
| 283 | |
| 284 | /* |
| 285 | * Check if the meta page went away from under us |
| 286 | */ |
| 287 | if (!list_empty(&pc->lru)) |
| 288 | list_move(&pc->lru, &pc_list); |
| 289 | else |
| 290 | continue; |
| 291 | |
| 292 | if (__isolate_lru_page(page, mode) == 0) { |
| 293 | list_move(&page->lru, dst); |
| 294 | nr_taken++; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | list_splice(&pc_list, src); |
| 299 | spin_unlock(&mem_cont->lru_lock); |
| 300 | |
| 301 | *scanned = scan; |
| 302 | return nr_taken; |
| 303 | } |
| 304 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 305 | /* |
| 306 | * Charge the memory controller for page usage. |
| 307 | * Return |
| 308 | * 0 if the charge was successful |
| 309 | * < 0 if the cgroup is over its limit |
| 310 | */ |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 311 | int mem_cgroup_charge(struct page *page, struct mm_struct *mm, |
| 312 | gfp_t gfp_mask) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 313 | { |
| 314 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 315 | struct page_cgroup *pc; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 316 | unsigned long flags; |
| 317 | unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 318 | |
| 319 | /* |
| 320 | * Should page_cgroup's go to their own slab? |
| 321 | * One could optimize the performance of the charging routine |
| 322 | * by saving a bit in the page_flags and using it as a lock |
| 323 | * to see if the cgroup page already has a page_cgroup associated |
| 324 | * with it |
| 325 | */ |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 326 | retry: |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 327 | lock_page_cgroup(page); |
| 328 | pc = page_get_page_cgroup(page); |
| 329 | /* |
| 330 | * The page_cgroup exists and the page has already been accounted |
| 331 | */ |
| 332 | if (pc) { |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 333 | if (unlikely(!atomic_inc_not_zero(&pc->ref_cnt))) { |
| 334 | /* this page is under being uncharged ? */ |
| 335 | unlock_page_cgroup(page); |
| 336 | cpu_relax(); |
| 337 | goto retry; |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 338 | } else { |
| 339 | unlock_page_cgroup(page); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 340 | goto done; |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 341 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | unlock_page_cgroup(page); |
| 345 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 346 | pc = kzalloc(sizeof(struct page_cgroup), gfp_mask); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 347 | if (pc == NULL) |
| 348 | goto err; |
| 349 | |
| 350 | rcu_read_lock(); |
| 351 | /* |
| 352 | * We always charge the cgroup the mm_struct belongs to |
| 353 | * the mm_struct's mem_cgroup changes on task migration if the |
| 354 | * thread group leader migrates. It's possible that mm is not |
| 355 | * set, if so charge the init_mm (happens for pagecache usage). |
| 356 | */ |
| 357 | if (!mm) |
| 358 | mm = &init_mm; |
| 359 | |
| 360 | mem = rcu_dereference(mm->mem_cgroup); |
| 361 | /* |
| 362 | * For every charge from the cgroup, increment reference |
| 363 | * count |
| 364 | */ |
| 365 | css_get(&mem->css); |
| 366 | rcu_read_unlock(); |
| 367 | |
| 368 | /* |
| 369 | * If we created the page_cgroup, we should free it on exceeding |
| 370 | * the cgroup limit. |
| 371 | */ |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 372 | while (res_counter_charge(&mem->res, PAGE_SIZE)) { |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 373 | bool is_atomic = gfp_mask & GFP_ATOMIC; |
| 374 | /* |
| 375 | * We cannot reclaim under GFP_ATOMIC, fail the charge |
| 376 | */ |
| 377 | if (is_atomic) |
| 378 | goto noreclaim; |
| 379 | |
| 380 | if (try_to_free_mem_cgroup_pages(mem, gfp_mask)) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 381 | continue; |
| 382 | |
| 383 | /* |
| 384 | * try_to_free_mem_cgroup_pages() might not give us a full |
| 385 | * picture of reclaim. Some pages are reclaimed and might be |
| 386 | * moved to swap cache or just unmapped from the cgroup. |
| 387 | * Check the limit again to see if the reclaim reduced the |
| 388 | * current usage of the cgroup before giving up |
| 389 | */ |
| 390 | if (res_counter_check_under_limit(&mem->res)) |
| 391 | continue; |
| 392 | /* |
| 393 | * Since we control both RSS and cache, we end up with a |
| 394 | * very interesting scenario where we end up reclaiming |
| 395 | * memory (essentially RSS), since the memory is pushed |
| 396 | * to swap cache, we eventually end up adding those |
| 397 | * pages back to our list. Hence we give ourselves a |
| 398 | * few chances before we fail |
| 399 | */ |
| 400 | else if (nr_retries--) { |
| 401 | congestion_wait(WRITE, HZ/10); |
| 402 | continue; |
| 403 | } |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 404 | noreclaim: |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 405 | css_put(&mem->css); |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 406 | if (!is_atomic) |
| 407 | mem_cgroup_out_of_memory(mem, GFP_KERNEL); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 408 | goto free_pc; |
| 409 | } |
| 410 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 411 | atomic_set(&pc->ref_cnt, 1); |
| 412 | pc->mem_cgroup = mem; |
| 413 | pc->page = page; |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 414 | if (page_cgroup_assign_new_page_cgroup(page, pc)) { |
| 415 | /* |
| 416 | * an another charge is added to this page already. |
| 417 | * we do take lock_page_cgroup(page) again and read |
| 418 | * page->cgroup, increment refcnt.... just retry is OK. |
| 419 | */ |
| 420 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 421 | css_put(&mem->css); |
| 422 | kfree(pc); |
| 423 | goto retry; |
| 424 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 425 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 426 | spin_lock_irqsave(&mem->lru_lock, flags); |
| 427 | list_add(&pc->lru, &mem->active_list); |
| 428 | spin_unlock_irqrestore(&mem->lru_lock, flags); |
| 429 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 430 | done: |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 431 | return 0; |
| 432 | free_pc: |
| 433 | kfree(pc); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 434 | err: |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 435 | return -ENOMEM; |
| 436 | } |
| 437 | |
| 438 | /* |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 439 | * See if the cached pages should be charged at all? |
| 440 | */ |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 441 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 442 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 443 | { |
| 444 | struct mem_cgroup *mem; |
| 445 | if (!mm) |
| 446 | mm = &init_mm; |
| 447 | |
| 448 | mem = rcu_dereference(mm->mem_cgroup); |
| 449 | if (mem->control_type == MEM_CGROUP_TYPE_ALL) |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 450 | return mem_cgroup_charge(page, mm, gfp_mask); |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 451 | else |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | /* |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 456 | * Uncharging is always a welcome operation, we never complain, simply |
| 457 | * uncharge. |
| 458 | */ |
| 459 | void mem_cgroup_uncharge(struct page_cgroup *pc) |
| 460 | { |
| 461 | struct mem_cgroup *mem; |
| 462 | struct page *page; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 463 | unsigned long flags; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 464 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 465 | /* |
| 466 | * This can handle cases when a page is not charged at all and we |
| 467 | * are switching between handling the control_type. |
| 468 | */ |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 469 | if (!pc) |
| 470 | return; |
| 471 | |
| 472 | if (atomic_dec_and_test(&pc->ref_cnt)) { |
| 473 | page = pc->page; |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 474 | /* |
| 475 | * get page->cgroup and clear it under lock. |
| 476 | */ |
| 477 | if (clear_page_cgroup(page, pc) == pc) { |
| 478 | mem = pc->mem_cgroup; |
| 479 | css_put(&mem->css); |
| 480 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 481 | spin_lock_irqsave(&mem->lru_lock, flags); |
| 482 | list_del_init(&pc->lru); |
| 483 | spin_unlock_irqrestore(&mem->lru_lock, flags); |
| 484 | kfree(pc); |
| 485 | } else { |
| 486 | /* |
| 487 | * Note:This will be removed when force-empty patch is |
| 488 | * applied. just show warning here. |
| 489 | */ |
| 490 | printk(KERN_ERR "Race in mem_cgroup_uncharge() ?"); |
| 491 | dump_stack(); |
| 492 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 493 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 494 | } |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame^] | 495 | /* |
| 496 | * Returns non-zero if a page (under migration) has valid page_cgroup member. |
| 497 | * Refcnt of page_cgroup is incremented. |
| 498 | */ |
| 499 | |
| 500 | int mem_cgroup_prepare_migration(struct page *page) |
| 501 | { |
| 502 | struct page_cgroup *pc; |
| 503 | int ret = 0; |
| 504 | lock_page_cgroup(page); |
| 505 | pc = page_get_page_cgroup(page); |
| 506 | if (pc && atomic_inc_not_zero(&pc->ref_cnt)) |
| 507 | ret = 1; |
| 508 | unlock_page_cgroup(page); |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | void mem_cgroup_end_migration(struct page *page) |
| 513 | { |
| 514 | struct page_cgroup *pc = page_get_page_cgroup(page); |
| 515 | mem_cgroup_uncharge(pc); |
| 516 | } |
| 517 | /* |
| 518 | * We know both *page* and *newpage* are now not-on-LRU and Pg_locked. |
| 519 | * And no race with uncharge() routines because page_cgroup for *page* |
| 520 | * has extra one reference by mem_cgroup_prepare_migration. |
| 521 | */ |
| 522 | |
| 523 | void mem_cgroup_page_migration(struct page *page, struct page *newpage) |
| 524 | { |
| 525 | struct page_cgroup *pc; |
| 526 | retry: |
| 527 | pc = page_get_page_cgroup(page); |
| 528 | if (!pc) |
| 529 | return; |
| 530 | if (clear_page_cgroup(page, pc) != pc) |
| 531 | goto retry; |
| 532 | pc->page = newpage; |
| 533 | lock_page_cgroup(newpage); |
| 534 | page_assign_page_cgroup(newpage, pc); |
| 535 | unlock_page_cgroup(newpage); |
| 536 | return; |
| 537 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 538 | |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 539 | int mem_cgroup_write_strategy(char *buf, unsigned long long *tmp) |
| 540 | { |
| 541 | *tmp = memparse(buf, &buf); |
| 542 | if (*buf != '\0') |
| 543 | return -EINVAL; |
| 544 | |
| 545 | /* |
| 546 | * Round up the value to the closest page size |
| 547 | */ |
| 548 | *tmp = ((*tmp + PAGE_SIZE - 1) >> PAGE_SHIFT) << PAGE_SHIFT; |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | static ssize_t mem_cgroup_read(struct cgroup *cont, |
| 553 | struct cftype *cft, struct file *file, |
| 554 | char __user *userbuf, size_t nbytes, loff_t *ppos) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 555 | { |
| 556 | return res_counter_read(&mem_cgroup_from_cont(cont)->res, |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 557 | cft->private, userbuf, nbytes, ppos, |
| 558 | NULL); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 562 | struct file *file, const char __user *userbuf, |
| 563 | size_t nbytes, loff_t *ppos) |
| 564 | { |
| 565 | return res_counter_write(&mem_cgroup_from_cont(cont)->res, |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 566 | cft->private, userbuf, nbytes, ppos, |
| 567 | mem_cgroup_write_strategy); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 570 | static ssize_t mem_control_type_write(struct cgroup *cont, |
| 571 | struct cftype *cft, struct file *file, |
| 572 | const char __user *userbuf, |
| 573 | size_t nbytes, loff_t *pos) |
| 574 | { |
| 575 | int ret; |
| 576 | char *buf, *end; |
| 577 | unsigned long tmp; |
| 578 | struct mem_cgroup *mem; |
| 579 | |
| 580 | mem = mem_cgroup_from_cont(cont); |
| 581 | buf = kmalloc(nbytes + 1, GFP_KERNEL); |
| 582 | ret = -ENOMEM; |
| 583 | if (buf == NULL) |
| 584 | goto out; |
| 585 | |
| 586 | buf[nbytes] = 0; |
| 587 | ret = -EFAULT; |
| 588 | if (copy_from_user(buf, userbuf, nbytes)) |
| 589 | goto out_free; |
| 590 | |
| 591 | ret = -EINVAL; |
| 592 | tmp = simple_strtoul(buf, &end, 10); |
| 593 | if (*end != '\0') |
| 594 | goto out_free; |
| 595 | |
| 596 | if (tmp <= MEM_CGROUP_TYPE_UNSPEC || tmp >= MEM_CGROUP_TYPE_MAX) |
| 597 | goto out_free; |
| 598 | |
| 599 | mem->control_type = tmp; |
| 600 | ret = nbytes; |
| 601 | out_free: |
| 602 | kfree(buf); |
| 603 | out: |
| 604 | return ret; |
| 605 | } |
| 606 | |
| 607 | static ssize_t mem_control_type_read(struct cgroup *cont, |
| 608 | struct cftype *cft, |
| 609 | struct file *file, char __user *userbuf, |
| 610 | size_t nbytes, loff_t *ppos) |
| 611 | { |
| 612 | unsigned long val; |
| 613 | char buf[64], *s; |
| 614 | struct mem_cgroup *mem; |
| 615 | |
| 616 | mem = mem_cgroup_from_cont(cont); |
| 617 | s = buf; |
| 618 | val = mem->control_type; |
| 619 | s += sprintf(s, "%lu\n", val); |
| 620 | return simple_read_from_buffer((void __user *)userbuf, nbytes, |
| 621 | ppos, buf, s - buf); |
| 622 | } |
| 623 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 624 | static struct cftype mem_cgroup_files[] = { |
| 625 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 626 | .name = "usage_in_bytes", |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 627 | .private = RES_USAGE, |
| 628 | .read = mem_cgroup_read, |
| 629 | }, |
| 630 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 631 | .name = "limit_in_bytes", |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 632 | .private = RES_LIMIT, |
| 633 | .write = mem_cgroup_write, |
| 634 | .read = mem_cgroup_read, |
| 635 | }, |
| 636 | { |
| 637 | .name = "failcnt", |
| 638 | .private = RES_FAILCNT, |
| 639 | .read = mem_cgroup_read, |
| 640 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 641 | { |
| 642 | .name = "control_type", |
| 643 | .write = mem_control_type_write, |
| 644 | .read = mem_control_type_read, |
| 645 | }, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 646 | }; |
| 647 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 648 | static struct mem_cgroup init_mem_cgroup; |
| 649 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 650 | static struct cgroup_subsys_state * |
| 651 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 652 | { |
| 653 | struct mem_cgroup *mem; |
| 654 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 655 | if (unlikely((cont->parent) == NULL)) { |
| 656 | mem = &init_mem_cgroup; |
| 657 | init_mm.mem_cgroup = mem; |
| 658 | } else |
| 659 | mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL); |
| 660 | |
| 661 | if (mem == NULL) |
| 662 | return NULL; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 663 | |
| 664 | res_counter_init(&mem->res); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 665 | INIT_LIST_HEAD(&mem->active_list); |
| 666 | INIT_LIST_HEAD(&mem->inactive_list); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 667 | spin_lock_init(&mem->lru_lock); |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 668 | mem->control_type = MEM_CGROUP_TYPE_ALL; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 669 | return &mem->css; |
| 670 | } |
| 671 | |
| 672 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 673 | struct cgroup *cont) |
| 674 | { |
| 675 | kfree(mem_cgroup_from_cont(cont)); |
| 676 | } |
| 677 | |
| 678 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 679 | struct cgroup *cont) |
| 680 | { |
| 681 | return cgroup_add_files(cont, ss, mem_cgroup_files, |
| 682 | ARRAY_SIZE(mem_cgroup_files)); |
| 683 | } |
| 684 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 685 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 686 | struct cgroup *cont, |
| 687 | struct cgroup *old_cont, |
| 688 | struct task_struct *p) |
| 689 | { |
| 690 | struct mm_struct *mm; |
| 691 | struct mem_cgroup *mem, *old_mem; |
| 692 | |
| 693 | mm = get_task_mm(p); |
| 694 | if (mm == NULL) |
| 695 | return; |
| 696 | |
| 697 | mem = mem_cgroup_from_cont(cont); |
| 698 | old_mem = mem_cgroup_from_cont(old_cont); |
| 699 | |
| 700 | if (mem == old_mem) |
| 701 | goto out; |
| 702 | |
| 703 | /* |
| 704 | * Only thread group leaders are allowed to migrate, the mm_struct is |
| 705 | * in effect owned by the leader |
| 706 | */ |
| 707 | if (p->tgid != p->pid) |
| 708 | goto out; |
| 709 | |
| 710 | css_get(&mem->css); |
| 711 | rcu_assign_pointer(mm->mem_cgroup, mem); |
| 712 | css_put(&old_mem->css); |
| 713 | |
| 714 | out: |
| 715 | mmput(mm); |
| 716 | return; |
| 717 | } |
| 718 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 719 | struct cgroup_subsys mem_cgroup_subsys = { |
| 720 | .name = "memory", |
| 721 | .subsys_id = mem_cgroup_subsys_id, |
| 722 | .create = mem_cgroup_create, |
| 723 | .destroy = mem_cgroup_destroy, |
| 724 | .populate = mem_cgroup_populate, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 725 | .attach = mem_cgroup_move_task, |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 726 | .early_init = 1, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 727 | }; |