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