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 | /* |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 81 | * per-zone information in memory controller. |
| 82 | */ |
| 83 | |
| 84 | enum mem_cgroup_zstat_index { |
| 85 | MEM_CGROUP_ZSTAT_ACTIVE, |
| 86 | MEM_CGROUP_ZSTAT_INACTIVE, |
| 87 | |
| 88 | NR_MEM_CGROUP_ZSTAT, |
| 89 | }; |
| 90 | |
| 91 | struct mem_cgroup_per_zone { |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 92 | /* |
| 93 | * spin_lock to protect the per cgroup LRU |
| 94 | */ |
| 95 | spinlock_t lru_lock; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 96 | struct list_head active_list; |
| 97 | struct list_head inactive_list; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 98 | unsigned long count[NR_MEM_CGROUP_ZSTAT]; |
| 99 | }; |
| 100 | /* Macro for accessing counter */ |
| 101 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) |
| 102 | |
| 103 | struct mem_cgroup_per_node { |
| 104 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; |
| 105 | }; |
| 106 | |
| 107 | struct mem_cgroup_lru_info { |
| 108 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; |
| 109 | }; |
| 110 | |
| 111 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 112 | * The memory controller data structure. The memory controller controls both |
| 113 | * page cache and RSS per cgroup. We would eventually like to provide |
| 114 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, |
| 115 | * to help the administrator determine what knobs to tune. |
| 116 | * |
| 117 | * 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] | 118 | * we hit the water mark. May be even add a low water mark, such that |
| 119 | * no reclaim occurs from a cgroup at it's low water mark, this is |
| 120 | * a feature that will be implemented much later in the future. |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 121 | */ |
| 122 | struct mem_cgroup { |
| 123 | struct cgroup_subsys_state css; |
| 124 | /* |
| 125 | * the counter to account for memory usage |
| 126 | */ |
| 127 | struct res_counter res; |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 128 | /* |
| 129 | * Per cgroup active and inactive list, similar to the |
| 130 | * per zone LRU lists. |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 131 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 132 | struct mem_cgroup_lru_info info; |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 133 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 134 | unsigned long control_type; /* control RSS or RSS+Pagecache */ |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 135 | int prev_priority; /* for recording reclaim priority */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 136 | /* |
| 137 | * statistics. |
| 138 | */ |
| 139 | struct mem_cgroup_stat stat; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | /* |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 143 | * We use the lower bit of the page->page_cgroup pointer as a bit spin |
| 144 | * lock. We need to ensure that page->page_cgroup is atleast two |
| 145 | * byte aligned (based on comments from Nick Piggin) |
| 146 | */ |
| 147 | #define PAGE_CGROUP_LOCK_BIT 0x0 |
| 148 | #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT) |
| 149 | |
| 150 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 151 | * A page_cgroup page is associated with every page descriptor. The |
| 152 | * page_cgroup helps us identify information about the cgroup |
| 153 | */ |
| 154 | struct page_cgroup { |
| 155 | struct list_head lru; /* per cgroup LRU list */ |
| 156 | struct page *page; |
| 157 | struct mem_cgroup *mem_cgroup; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 158 | atomic_t ref_cnt; /* Helpful when pages move b/w */ |
| 159 | /* mapped and cached states */ |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 160 | int flags; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 161 | }; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 162 | #define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */ |
KAMEZAWA Hiroyuki | 3564c7c | 2008-02-07 00:14:23 -0800 | [diff] [blame] | 163 | #define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */ |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 164 | |
KAMEZAWA Hiroyuki | c014953 | 2008-02-07 00:14:30 -0800 | [diff] [blame] | 165 | static inline int page_cgroup_nid(struct page_cgroup *pc) |
| 166 | { |
| 167 | return page_to_nid(pc->page); |
| 168 | } |
| 169 | |
| 170 | static inline enum zone_type page_cgroup_zid(struct page_cgroup *pc) |
| 171 | { |
| 172 | return page_zonenum(pc->page); |
| 173 | } |
| 174 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 175 | enum { |
| 176 | MEM_CGROUP_TYPE_UNSPEC = 0, |
| 177 | MEM_CGROUP_TYPE_MAPPED, |
| 178 | MEM_CGROUP_TYPE_CACHED, |
| 179 | MEM_CGROUP_TYPE_ALL, |
| 180 | MEM_CGROUP_TYPE_MAX, |
| 181 | }; |
| 182 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 183 | enum charge_type { |
| 184 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
| 185 | MEM_CGROUP_CHARGE_TYPE_MAPPED, |
| 186 | }; |
| 187 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 188 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 189 | /* |
| 190 | * Always modified under lru lock. Then, not necessary to preempt_disable() |
| 191 | */ |
| 192 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags, |
| 193 | bool charge) |
| 194 | { |
| 195 | int val = (charge)? 1 : -1; |
| 196 | struct mem_cgroup_stat *stat = &mem->stat; |
| 197 | VM_BUG_ON(!irqs_disabled()); |
| 198 | |
| 199 | if (flags & PAGE_CGROUP_FLAG_CACHE) |
| 200 | __mem_cgroup_stat_add_safe(stat, |
| 201 | MEM_CGROUP_STAT_CACHE, val); |
| 202 | else |
| 203 | __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 204 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 205 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 206 | static inline struct mem_cgroup_per_zone * |
| 207 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) |
| 208 | { |
| 209 | BUG_ON(!mem->info.nodeinfo[nid]); |
| 210 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; |
| 211 | } |
| 212 | |
| 213 | static inline struct mem_cgroup_per_zone * |
| 214 | page_cgroup_zoneinfo(struct page_cgroup *pc) |
| 215 | { |
| 216 | struct mem_cgroup *mem = pc->mem_cgroup; |
| 217 | int nid = page_cgroup_nid(pc); |
| 218 | int zid = page_cgroup_zid(pc); |
| 219 | |
| 220 | return mem_cgroup_zoneinfo(mem, nid, zid); |
| 221 | } |
| 222 | |
| 223 | static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem, |
| 224 | enum mem_cgroup_zstat_index idx) |
| 225 | { |
| 226 | int nid, zid; |
| 227 | struct mem_cgroup_per_zone *mz; |
| 228 | u64 total = 0; |
| 229 | |
| 230 | for_each_online_node(nid) |
| 231 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 232 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 233 | total += MEM_CGROUP_ZSTAT(mz, idx); |
| 234 | } |
| 235 | return total; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 238 | static struct mem_cgroup init_mem_cgroup; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 239 | |
| 240 | static inline |
| 241 | struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
| 242 | { |
| 243 | return container_of(cgroup_subsys_state(cont, |
| 244 | mem_cgroup_subsys_id), struct mem_cgroup, |
| 245 | css); |
| 246 | } |
| 247 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 248 | static inline |
| 249 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
| 250 | { |
| 251 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
| 252 | struct mem_cgroup, css); |
| 253 | } |
| 254 | |
| 255 | void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p) |
| 256 | { |
| 257 | struct mem_cgroup *mem; |
| 258 | |
| 259 | mem = mem_cgroup_from_task(p); |
| 260 | css_get(&mem->css); |
| 261 | mm->mem_cgroup = mem; |
| 262 | } |
| 263 | |
| 264 | void mm_free_cgroup(struct mm_struct *mm) |
| 265 | { |
| 266 | css_put(&mm->mem_cgroup->css); |
| 267 | } |
| 268 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 269 | static inline int page_cgroup_locked(struct page *page) |
| 270 | { |
| 271 | return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, |
| 272 | &page->page_cgroup); |
| 273 | } |
| 274 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 275 | void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc) |
| 276 | { |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 277 | int locked; |
| 278 | |
| 279 | /* |
| 280 | * While resetting the page_cgroup we might not hold the |
| 281 | * page_cgroup lock. free_hot_cold_page() is an example |
| 282 | * of such a scenario |
| 283 | */ |
| 284 | if (pc) |
| 285 | VM_BUG_ON(!page_cgroup_locked(page)); |
| 286 | locked = (page->page_cgroup & PAGE_CGROUP_LOCK); |
| 287 | page->page_cgroup = ((unsigned long)pc | locked); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | struct page_cgroup *page_get_page_cgroup(struct page *page) |
| 291 | { |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 292 | return (struct page_cgroup *) |
| 293 | (page->page_cgroup & ~PAGE_CGROUP_LOCK); |
| 294 | } |
| 295 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 296 | static void __always_inline lock_page_cgroup(struct page *page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 297 | { |
| 298 | bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); |
| 299 | VM_BUG_ON(!page_cgroup_locked(page)); |
| 300 | } |
| 301 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 302 | static void __always_inline unlock_page_cgroup(struct page *page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 303 | { |
| 304 | bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); |
| 305 | } |
| 306 | |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 307 | /* |
| 308 | * Tie new page_cgroup to struct page under lock_page_cgroup() |
| 309 | * This can fail if the page has been tied to a page_cgroup. |
| 310 | * If success, returns 0. |
| 311 | */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 312 | static int page_cgroup_assign_new_page_cgroup(struct page *page, |
| 313 | struct page_cgroup *pc) |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 314 | { |
| 315 | int ret = 0; |
| 316 | |
| 317 | lock_page_cgroup(page); |
| 318 | if (!page_get_page_cgroup(page)) |
| 319 | page_assign_page_cgroup(page, pc); |
| 320 | else /* A page is tied to other pc. */ |
| 321 | ret = 1; |
| 322 | unlock_page_cgroup(page); |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Clear page->page_cgroup member under lock_page_cgroup(). |
| 328 | * If given "pc" value is different from one page->page_cgroup, |
| 329 | * page->cgroup is not cleared. |
| 330 | * Returns a value of page->page_cgroup at lock taken. |
| 331 | * A can can detect failure of clearing by following |
| 332 | * clear_page_cgroup(page, pc) == pc |
| 333 | */ |
| 334 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 335 | static struct page_cgroup *clear_page_cgroup(struct page *page, |
| 336 | struct page_cgroup *pc) |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 337 | { |
| 338 | struct page_cgroup *ret; |
| 339 | /* lock and clear */ |
| 340 | lock_page_cgroup(page); |
| 341 | ret = page_get_page_cgroup(page); |
| 342 | if (likely(ret == pc)) |
| 343 | page_assign_page_cgroup(page, NULL); |
| 344 | unlock_page_cgroup(page); |
| 345 | return ret; |
| 346 | } |
| 347 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 348 | static void __mem_cgroup_remove_list(struct page_cgroup *pc) |
| 349 | { |
| 350 | int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE; |
| 351 | struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc); |
| 352 | |
| 353 | if (from) |
| 354 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1; |
| 355 | else |
| 356 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1; |
| 357 | |
| 358 | mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false); |
| 359 | list_del_init(&pc->lru); |
| 360 | } |
| 361 | |
| 362 | static void __mem_cgroup_add_list(struct page_cgroup *pc) |
| 363 | { |
| 364 | int to = pc->flags & PAGE_CGROUP_FLAG_ACTIVE; |
| 365 | struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc); |
| 366 | |
| 367 | if (!to) { |
| 368 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 369 | list_add(&pc->lru, &mz->inactive_list); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 370 | } else { |
| 371 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 372 | list_add(&pc->lru, &mz->active_list); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 373 | } |
| 374 | mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true); |
| 375 | } |
| 376 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 377 | static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 378 | { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 379 | int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE; |
| 380 | struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc); |
| 381 | |
| 382 | if (from) |
| 383 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1; |
| 384 | else |
| 385 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1; |
| 386 | |
KAMEZAWA Hiroyuki | 3564c7c | 2008-02-07 00:14:23 -0800 | [diff] [blame] | 387 | if (active) { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 388 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1; |
KAMEZAWA Hiroyuki | 3564c7c | 2008-02-07 00:14:23 -0800 | [diff] [blame] | 389 | pc->flags |= PAGE_CGROUP_FLAG_ACTIVE; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 390 | list_move(&pc->lru, &mz->active_list); |
KAMEZAWA Hiroyuki | 3564c7c | 2008-02-07 00:14:23 -0800 | [diff] [blame] | 391 | } else { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 392 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1; |
KAMEZAWA Hiroyuki | 3564c7c | 2008-02-07 00:14:23 -0800 | [diff] [blame] | 393 | pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 394 | list_move(&pc->lru, &mz->inactive_list); |
KAMEZAWA Hiroyuki | 3564c7c | 2008-02-07 00:14:23 -0800 | [diff] [blame] | 395 | } |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 396 | } |
| 397 | |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 398 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
| 399 | { |
| 400 | int ret; |
| 401 | |
| 402 | task_lock(task); |
| 403 | ret = task->mm && mm_cgroup(task->mm) == mem; |
| 404 | task_unlock(task); |
| 405 | return ret; |
| 406 | } |
| 407 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 408 | /* |
| 409 | * This routine assumes that the appropriate zone's lru lock is already held |
| 410 | */ |
| 411 | void mem_cgroup_move_lists(struct page_cgroup *pc, bool active) |
| 412 | { |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 413 | struct mem_cgroup_per_zone *mz; |
| 414 | unsigned long flags; |
| 415 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 416 | if (!pc) |
| 417 | return; |
| 418 | |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 419 | mz = page_cgroup_zoneinfo(pc); |
| 420 | spin_lock_irqsave(&mz->lru_lock, flags); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 421 | __mem_cgroup_move_lists(pc, active); |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 422 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 423 | } |
| 424 | |
KAMEZAWA Hiroyuki | 58ae83d | 2008-02-07 00:14:32 -0800 | [diff] [blame] | 425 | /* |
| 426 | * Calculate mapped_ratio under memory controller. This will be used in |
| 427 | * vmscan.c for deteremining we have to reclaim mapped pages. |
| 428 | */ |
| 429 | int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem) |
| 430 | { |
| 431 | long total, rss; |
| 432 | |
| 433 | /* |
| 434 | * usage is recorded in bytes. But, here, we assume the number of |
| 435 | * physical pages can be represented by "long" on any arch. |
| 436 | */ |
| 437 | total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L; |
| 438 | rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS); |
| 439 | return (int)((rss * 100L) / total); |
| 440 | } |
KAMEZAWA Hiroyuki | 5932f36 | 2008-02-07 00:14:33 -0800 | [diff] [blame] | 441 | /* |
| 442 | * This function is called from vmscan.c. In page reclaiming loop. balance |
| 443 | * between active and inactive list is calculated. For memory controller |
| 444 | * page reclaiming, we should use using mem_cgroup's imbalance rather than |
| 445 | * zone's global lru imbalance. |
| 446 | */ |
| 447 | long mem_cgroup_reclaim_imbalance(struct mem_cgroup *mem) |
| 448 | { |
| 449 | unsigned long active, inactive; |
| 450 | /* active and inactive are the number of pages. 'long' is ok.*/ |
| 451 | active = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_ACTIVE); |
| 452 | inactive = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_INACTIVE); |
| 453 | return (long) (active / (inactive + 1)); |
| 454 | } |
KAMEZAWA Hiroyuki | 58ae83d | 2008-02-07 00:14:32 -0800 | [diff] [blame] | 455 | |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 456 | /* |
| 457 | * prev_priority control...this will be used in memory reclaim path. |
| 458 | */ |
| 459 | int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem) |
| 460 | { |
| 461 | return mem->prev_priority; |
| 462 | } |
| 463 | |
| 464 | void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority) |
| 465 | { |
| 466 | if (priority < mem->prev_priority) |
| 467 | mem->prev_priority = priority; |
| 468 | } |
| 469 | |
| 470 | void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority) |
| 471 | { |
| 472 | mem->prev_priority = priority; |
| 473 | } |
| 474 | |
KAMEZAWA Hiroyuki | cc38108 | 2008-02-07 00:14:35 -0800 | [diff] [blame] | 475 | /* |
| 476 | * Calculate # of pages to be scanned in this priority/zone. |
| 477 | * See also vmscan.c |
| 478 | * |
| 479 | * priority starts from "DEF_PRIORITY" and decremented in each loop. |
| 480 | * (see include/linux/mmzone.h) |
| 481 | */ |
| 482 | |
| 483 | long mem_cgroup_calc_reclaim_active(struct mem_cgroup *mem, |
| 484 | struct zone *zone, int priority) |
| 485 | { |
| 486 | long nr_active; |
| 487 | int nid = zone->zone_pgdat->node_id; |
| 488 | int zid = zone_idx(zone); |
| 489 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 490 | |
| 491 | nr_active = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE); |
| 492 | return (nr_active >> priority); |
| 493 | } |
| 494 | |
| 495 | long mem_cgroup_calc_reclaim_inactive(struct mem_cgroup *mem, |
| 496 | struct zone *zone, int priority) |
| 497 | { |
| 498 | long nr_inactive; |
| 499 | int nid = zone->zone_pgdat->node_id; |
| 500 | int zid = zone_idx(zone); |
| 501 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 502 | |
| 503 | nr_inactive = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE); |
| 504 | |
| 505 | return (nr_inactive >> priority); |
| 506 | } |
| 507 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 508 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 509 | struct list_head *dst, |
| 510 | unsigned long *scanned, int order, |
| 511 | int mode, struct zone *z, |
| 512 | struct mem_cgroup *mem_cont, |
| 513 | int active) |
| 514 | { |
| 515 | unsigned long nr_taken = 0; |
| 516 | struct page *page; |
| 517 | unsigned long scan; |
| 518 | LIST_HEAD(pc_list); |
| 519 | struct list_head *src; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 520 | struct page_cgroup *pc, *tmp; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 521 | int nid = z->zone_pgdat->node_id; |
| 522 | int zid = zone_idx(z); |
| 523 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 524 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 525 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 526 | if (active) |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 527 | src = &mz->active_list; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 528 | else |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 529 | src = &mz->inactive_list; |
| 530 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 531 | |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 532 | spin_lock(&mz->lru_lock); |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 533 | scan = 0; |
| 534 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 535 | if (scan >= nr_to_scan) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 536 | break; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 537 | page = pc->page; |
| 538 | VM_BUG_ON(!pc); |
| 539 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 540 | if (unlikely(!PageLRU(page))) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 541 | continue; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 542 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 543 | if (PageActive(page) && !active) { |
| 544 | __mem_cgroup_move_lists(pc, true); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 545 | continue; |
| 546 | } |
| 547 | if (!PageActive(page) && active) { |
| 548 | __mem_cgroup_move_lists(pc, false); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 549 | continue; |
| 550 | } |
| 551 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 552 | scan++; |
| 553 | list_move(&pc->lru, &pc_list); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 554 | |
| 555 | if (__isolate_lru_page(page, mode) == 0) { |
| 556 | list_move(&page->lru, dst); |
| 557 | nr_taken++; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | list_splice(&pc_list, src); |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 562 | spin_unlock(&mz->lru_lock); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 563 | |
| 564 | *scanned = scan; |
| 565 | return nr_taken; |
| 566 | } |
| 567 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 568 | /* |
| 569 | * Charge the memory controller for page usage. |
| 570 | * Return |
| 571 | * 0 if the charge was successful |
| 572 | * < 0 if the cgroup is over its limit |
| 573 | */ |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 574 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, |
| 575 | gfp_t gfp_mask, enum charge_type ctype) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 576 | { |
| 577 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 578 | struct page_cgroup *pc; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 579 | unsigned long flags; |
| 580 | unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 581 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 582 | |
| 583 | /* |
| 584 | * Should page_cgroup's go to their own slab? |
| 585 | * One could optimize the performance of the charging routine |
| 586 | * by saving a bit in the page_flags and using it as a lock |
| 587 | * to see if the cgroup page already has a page_cgroup associated |
| 588 | * with it |
| 589 | */ |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 590 | retry: |
Hugh Dickins | 8236955 | 2008-02-07 00:14:22 -0800 | [diff] [blame] | 591 | if (page) { |
| 592 | lock_page_cgroup(page); |
| 593 | pc = page_get_page_cgroup(page); |
| 594 | /* |
| 595 | * The page_cgroup exists and |
| 596 | * the page has already been accounted. |
| 597 | */ |
| 598 | if (pc) { |
| 599 | if (unlikely(!atomic_inc_not_zero(&pc->ref_cnt))) { |
| 600 | /* this page is under being uncharged ? */ |
| 601 | unlock_page_cgroup(page); |
| 602 | cpu_relax(); |
| 603 | goto retry; |
| 604 | } else { |
| 605 | unlock_page_cgroup(page); |
| 606 | goto done; |
| 607 | } |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 608 | } |
Hugh Dickins | 8236955 | 2008-02-07 00:14:22 -0800 | [diff] [blame] | 609 | unlock_page_cgroup(page); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 610 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 611 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 612 | pc = kzalloc(sizeof(struct page_cgroup), gfp_mask); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 613 | if (pc == NULL) |
| 614 | goto err; |
| 615 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 616 | /* |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 617 | * We always charge the cgroup the mm_struct belongs to. |
| 618 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 619 | * thread group leader migrates. It's possible that mm is not |
| 620 | * set, if so charge the init_mm (happens for pagecache usage). |
| 621 | */ |
| 622 | if (!mm) |
| 623 | mm = &init_mm; |
| 624 | |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 625 | rcu_read_lock(); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 626 | mem = rcu_dereference(mm->mem_cgroup); |
| 627 | /* |
| 628 | * For every charge from the cgroup, increment reference |
| 629 | * count |
| 630 | */ |
| 631 | css_get(&mem->css); |
| 632 | rcu_read_unlock(); |
| 633 | |
| 634 | /* |
| 635 | * If we created the page_cgroup, we should free it on exceeding |
| 636 | * the cgroup limit. |
| 637 | */ |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 638 | while (res_counter_charge(&mem->res, PAGE_SIZE)) { |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 639 | if (!(gfp_mask & __GFP_WAIT)) |
| 640 | goto out; |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 641 | |
| 642 | if (try_to_free_mem_cgroup_pages(mem, gfp_mask)) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 643 | continue; |
| 644 | |
| 645 | /* |
| 646 | * try_to_free_mem_cgroup_pages() might not give us a full |
| 647 | * picture of reclaim. Some pages are reclaimed and might be |
| 648 | * moved to swap cache or just unmapped from the cgroup. |
| 649 | * Check the limit again to see if the reclaim reduced the |
| 650 | * current usage of the cgroup before giving up |
| 651 | */ |
| 652 | if (res_counter_check_under_limit(&mem->res)) |
| 653 | continue; |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 654 | |
| 655 | if (!nr_retries--) { |
| 656 | mem_cgroup_out_of_memory(mem, gfp_mask); |
| 657 | goto out; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 658 | } |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 659 | congestion_wait(WRITE, HZ/10); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 660 | } |
| 661 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 662 | atomic_set(&pc->ref_cnt, 1); |
| 663 | pc->mem_cgroup = mem; |
| 664 | pc->page = page; |
KAMEZAWA Hiroyuki | 3564c7c | 2008-02-07 00:14:23 -0800 | [diff] [blame] | 665 | pc->flags = PAGE_CGROUP_FLAG_ACTIVE; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 666 | if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE) |
| 667 | pc->flags |= PAGE_CGROUP_FLAG_CACHE; |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 668 | |
Hugh Dickins | 8236955 | 2008-02-07 00:14:22 -0800 | [diff] [blame] | 669 | if (!page || page_cgroup_assign_new_page_cgroup(page, pc)) { |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 670 | /* |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 671 | * Another charge has been added to this page already. |
| 672 | * We take lock_page_cgroup(page) again and read |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 673 | * page->cgroup, increment refcnt.... just retry is OK. |
| 674 | */ |
| 675 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 676 | css_put(&mem->css); |
| 677 | kfree(pc); |
Hugh Dickins | 8236955 | 2008-02-07 00:14:22 -0800 | [diff] [blame] | 678 | if (!page) |
| 679 | goto done; |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 680 | goto retry; |
| 681 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 682 | |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 683 | mz = page_cgroup_zoneinfo(pc); |
| 684 | spin_lock_irqsave(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 685 | /* Update statistics vector */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 686 | __mem_cgroup_add_list(pc); |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 687 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 688 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 689 | done: |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 690 | return 0; |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 691 | out: |
| 692 | css_put(&mem->css); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 693 | kfree(pc); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 694 | err: |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 695 | return -ENOMEM; |
| 696 | } |
| 697 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 698 | int mem_cgroup_charge(struct page *page, struct mm_struct *mm, |
| 699 | gfp_t gfp_mask) |
| 700 | { |
| 701 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
| 702 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 703 | } |
| 704 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 705 | /* |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 706 | * See if the cached pages should be charged at all? |
| 707 | */ |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 708 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 709 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 710 | { |
Balbir Singh | ac44d35 | 2008-02-07 00:14:18 -0800 | [diff] [blame] | 711 | int ret = 0; |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 712 | struct mem_cgroup *mem; |
| 713 | if (!mm) |
| 714 | mm = &init_mm; |
| 715 | |
Balbir Singh | ac44d35 | 2008-02-07 00:14:18 -0800 | [diff] [blame] | 716 | rcu_read_lock(); |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 717 | mem = rcu_dereference(mm->mem_cgroup); |
Balbir Singh | ac44d35 | 2008-02-07 00:14:18 -0800 | [diff] [blame] | 718 | css_get(&mem->css); |
| 719 | rcu_read_unlock(); |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 720 | if (mem->control_type == MEM_CGROUP_TYPE_ALL) |
Balbir Singh | ac44d35 | 2008-02-07 00:14:18 -0800 | [diff] [blame] | 721 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 722 | MEM_CGROUP_CHARGE_TYPE_CACHE); |
Balbir Singh | ac44d35 | 2008-02-07 00:14:18 -0800 | [diff] [blame] | 723 | css_put(&mem->css); |
| 724 | return ret; |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /* |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 728 | * Uncharging is always a welcome operation, we never complain, simply |
| 729 | * uncharge. |
| 730 | */ |
| 731 | void mem_cgroup_uncharge(struct page_cgroup *pc) |
| 732 | { |
| 733 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 734 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 735 | struct page *page; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 736 | unsigned long flags; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 737 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 738 | /* |
| 739 | * This can handle cases when a page is not charged at all and we |
| 740 | * are switching between handling the control_type. |
| 741 | */ |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 742 | if (!pc) |
| 743 | return; |
| 744 | |
| 745 | if (atomic_dec_and_test(&pc->ref_cnt)) { |
| 746 | page = pc->page; |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 747 | mz = page_cgroup_zoneinfo(pc); |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 748 | /* |
| 749 | * get page->cgroup and clear it under lock. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 750 | * force_empty can drop page->cgroup without checking refcnt. |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 751 | */ |
| 752 | if (clear_page_cgroup(page, pc) == pc) { |
| 753 | mem = pc->mem_cgroup; |
| 754 | css_put(&mem->css); |
| 755 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 756 | spin_lock_irqsave(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 757 | __mem_cgroup_remove_list(pc); |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 758 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 759 | kfree(pc); |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 760 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 761 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 762 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 763 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 764 | /* |
| 765 | * Returns non-zero if a page (under migration) has valid page_cgroup member. |
| 766 | * Refcnt of page_cgroup is incremented. |
| 767 | */ |
| 768 | |
| 769 | int mem_cgroup_prepare_migration(struct page *page) |
| 770 | { |
| 771 | struct page_cgroup *pc; |
| 772 | int ret = 0; |
| 773 | lock_page_cgroup(page); |
| 774 | pc = page_get_page_cgroup(page); |
| 775 | if (pc && atomic_inc_not_zero(&pc->ref_cnt)) |
| 776 | ret = 1; |
| 777 | unlock_page_cgroup(page); |
| 778 | return ret; |
| 779 | } |
| 780 | |
| 781 | void mem_cgroup_end_migration(struct page *page) |
| 782 | { |
| 783 | struct page_cgroup *pc = page_get_page_cgroup(page); |
| 784 | mem_cgroup_uncharge(pc); |
| 785 | } |
| 786 | /* |
| 787 | * We know both *page* and *newpage* are now not-on-LRU and Pg_locked. |
| 788 | * And no race with uncharge() routines because page_cgroup for *page* |
| 789 | * has extra one reference by mem_cgroup_prepare_migration. |
| 790 | */ |
| 791 | |
| 792 | void mem_cgroup_page_migration(struct page *page, struct page *newpage) |
| 793 | { |
| 794 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 795 | struct mem_cgroup *mem; |
| 796 | unsigned long flags; |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 797 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 798 | retry: |
| 799 | pc = page_get_page_cgroup(page); |
| 800 | if (!pc) |
| 801 | return; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 802 | mem = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 803 | mz = page_cgroup_zoneinfo(pc); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 804 | if (clear_page_cgroup(page, pc) != pc) |
| 805 | goto retry; |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 806 | spin_lock_irqsave(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 807 | |
| 808 | __mem_cgroup_remove_list(pc); |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 809 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
| 810 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 811 | pc->page = newpage; |
| 812 | lock_page_cgroup(newpage); |
| 813 | page_assign_page_cgroup(newpage, pc); |
| 814 | unlock_page_cgroup(newpage); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 815 | |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 816 | mz = page_cgroup_zoneinfo(pc); |
| 817 | spin_lock_irqsave(&mz->lru_lock, flags); |
| 818 | __mem_cgroup_add_list(pc); |
| 819 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 820 | return; |
| 821 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 822 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 823 | /* |
| 824 | * This routine traverse page_cgroup in given list and drop them all. |
| 825 | * This routine ignores page_cgroup->ref_cnt. |
| 826 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
| 827 | */ |
| 828 | #define FORCE_UNCHARGE_BATCH (128) |
| 829 | static void |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 830 | mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
| 831 | struct mem_cgroup_per_zone *mz, |
| 832 | int active) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 833 | { |
| 834 | struct page_cgroup *pc; |
| 835 | struct page *page; |
| 836 | int count; |
| 837 | unsigned long flags; |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 838 | struct list_head *list; |
| 839 | |
| 840 | if (active) |
| 841 | list = &mz->active_list; |
| 842 | else |
| 843 | list = &mz->inactive_list; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 844 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 845 | if (list_empty(list)) |
| 846 | return; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 847 | retry: |
| 848 | count = FORCE_UNCHARGE_BATCH; |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 849 | spin_lock_irqsave(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 850 | |
| 851 | while (--count && !list_empty(list)) { |
| 852 | pc = list_entry(list->prev, struct page_cgroup, lru); |
| 853 | page = pc->page; |
| 854 | /* Avoid race with charge */ |
| 855 | atomic_set(&pc->ref_cnt, 0); |
| 856 | if (clear_page_cgroup(page, pc) == pc) { |
| 857 | css_put(&mem->css); |
| 858 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 859 | __mem_cgroup_remove_list(pc); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 860 | kfree(pc); |
| 861 | } else /* being uncharged ? ...do relax */ |
| 862 | break; |
| 863 | } |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 864 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 865 | if (!list_empty(list)) { |
| 866 | cond_resched(); |
| 867 | goto retry; |
| 868 | } |
| 869 | return; |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | * make mem_cgroup's charge to be 0 if there is no task. |
| 874 | * This enables deleting this mem_cgroup. |
| 875 | */ |
| 876 | |
| 877 | int mem_cgroup_force_empty(struct mem_cgroup *mem) |
| 878 | { |
| 879 | int ret = -EBUSY; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 880 | int node, zid; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 881 | css_get(&mem->css); |
| 882 | /* |
| 883 | * page reclaim code (kswapd etc..) will move pages between |
| 884 | ` * active_list <-> inactive_list while we don't take a lock. |
| 885 | * So, we have to do loop here until all lists are empty. |
| 886 | */ |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 887 | while (mem->res.usage > 0) { |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 888 | if (atomic_read(&mem->css.cgroup->count) > 0) |
| 889 | goto out; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 890 | for_each_node_state(node, N_POSSIBLE) |
| 891 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 892 | struct mem_cgroup_per_zone *mz; |
| 893 | mz = mem_cgroup_zoneinfo(mem, node, zid); |
| 894 | /* drop all page_cgroup in active_list */ |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 895 | mem_cgroup_force_empty_list(mem, mz, 1); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 896 | /* drop all page_cgroup in inactive_list */ |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 897 | mem_cgroup_force_empty_list(mem, mz, 0); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 898 | } |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 899 | } |
| 900 | ret = 0; |
| 901 | out: |
| 902 | css_put(&mem->css); |
| 903 | return ret; |
| 904 | } |
| 905 | |
| 906 | |
| 907 | |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 908 | int mem_cgroup_write_strategy(char *buf, unsigned long long *tmp) |
| 909 | { |
| 910 | *tmp = memparse(buf, &buf); |
| 911 | if (*buf != '\0') |
| 912 | return -EINVAL; |
| 913 | |
| 914 | /* |
| 915 | * Round up the value to the closest page size |
| 916 | */ |
| 917 | *tmp = ((*tmp + PAGE_SIZE - 1) >> PAGE_SHIFT) << PAGE_SHIFT; |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | static ssize_t mem_cgroup_read(struct cgroup *cont, |
| 922 | struct cftype *cft, struct file *file, |
| 923 | char __user *userbuf, size_t nbytes, loff_t *ppos) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 924 | { |
| 925 | return res_counter_read(&mem_cgroup_from_cont(cont)->res, |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 926 | cft->private, userbuf, nbytes, ppos, |
| 927 | NULL); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 931 | struct file *file, const char __user *userbuf, |
| 932 | size_t nbytes, loff_t *ppos) |
| 933 | { |
| 934 | return res_counter_write(&mem_cgroup_from_cont(cont)->res, |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 935 | cft->private, userbuf, nbytes, ppos, |
| 936 | mem_cgroup_write_strategy); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 937 | } |
| 938 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 939 | static ssize_t mem_control_type_write(struct cgroup *cont, |
| 940 | struct cftype *cft, struct file *file, |
| 941 | const char __user *userbuf, |
| 942 | size_t nbytes, loff_t *pos) |
| 943 | { |
| 944 | int ret; |
| 945 | char *buf, *end; |
| 946 | unsigned long tmp; |
| 947 | struct mem_cgroup *mem; |
| 948 | |
| 949 | mem = mem_cgroup_from_cont(cont); |
| 950 | buf = kmalloc(nbytes + 1, GFP_KERNEL); |
| 951 | ret = -ENOMEM; |
| 952 | if (buf == NULL) |
| 953 | goto out; |
| 954 | |
| 955 | buf[nbytes] = 0; |
| 956 | ret = -EFAULT; |
| 957 | if (copy_from_user(buf, userbuf, nbytes)) |
| 958 | goto out_free; |
| 959 | |
| 960 | ret = -EINVAL; |
| 961 | tmp = simple_strtoul(buf, &end, 10); |
| 962 | if (*end != '\0') |
| 963 | goto out_free; |
| 964 | |
| 965 | if (tmp <= MEM_CGROUP_TYPE_UNSPEC || tmp >= MEM_CGROUP_TYPE_MAX) |
| 966 | goto out_free; |
| 967 | |
| 968 | mem->control_type = tmp; |
| 969 | ret = nbytes; |
| 970 | out_free: |
| 971 | kfree(buf); |
| 972 | out: |
| 973 | return ret; |
| 974 | } |
| 975 | |
| 976 | static ssize_t mem_control_type_read(struct cgroup *cont, |
| 977 | struct cftype *cft, |
| 978 | struct file *file, char __user *userbuf, |
| 979 | size_t nbytes, loff_t *ppos) |
| 980 | { |
| 981 | unsigned long val; |
| 982 | char buf[64], *s; |
| 983 | struct mem_cgroup *mem; |
| 984 | |
| 985 | mem = mem_cgroup_from_cont(cont); |
| 986 | s = buf; |
| 987 | val = mem->control_type; |
| 988 | s += sprintf(s, "%lu\n", val); |
| 989 | return simple_read_from_buffer((void __user *)userbuf, nbytes, |
| 990 | ppos, buf, s - buf); |
| 991 | } |
| 992 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 993 | |
| 994 | static ssize_t mem_force_empty_write(struct cgroup *cont, |
| 995 | struct cftype *cft, struct file *file, |
| 996 | const char __user *userbuf, |
| 997 | size_t nbytes, loff_t *ppos) |
| 998 | { |
| 999 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 1000 | int ret; |
| 1001 | ret = mem_cgroup_force_empty(mem); |
| 1002 | if (!ret) |
| 1003 | ret = nbytes; |
| 1004 | return ret; |
| 1005 | } |
| 1006 | |
| 1007 | /* |
| 1008 | * Note: This should be removed if cgroup supports write-only file. |
| 1009 | */ |
| 1010 | |
| 1011 | static ssize_t mem_force_empty_read(struct cgroup *cont, |
| 1012 | struct cftype *cft, |
| 1013 | struct file *file, char __user *userbuf, |
| 1014 | size_t nbytes, loff_t *ppos) |
| 1015 | { |
| 1016 | return -EINVAL; |
| 1017 | } |
| 1018 | |
| 1019 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1020 | static const struct mem_cgroup_stat_desc { |
| 1021 | const char *msg; |
| 1022 | u64 unit; |
| 1023 | } mem_cgroup_stat_desc[] = { |
| 1024 | [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, }, |
| 1025 | [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, }, |
| 1026 | }; |
| 1027 | |
| 1028 | static int mem_control_stat_show(struct seq_file *m, void *arg) |
| 1029 | { |
| 1030 | struct cgroup *cont = m->private; |
| 1031 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
| 1032 | struct mem_cgroup_stat *stat = &mem_cont->stat; |
| 1033 | int i; |
| 1034 | |
| 1035 | for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) { |
| 1036 | s64 val; |
| 1037 | |
| 1038 | val = mem_cgroup_read_stat(stat, i); |
| 1039 | val *= mem_cgroup_stat_desc[i].unit; |
| 1040 | seq_printf(m, "%s %lld\n", mem_cgroup_stat_desc[i].msg, |
| 1041 | (long long)val); |
| 1042 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1043 | /* showing # of active pages */ |
| 1044 | { |
| 1045 | unsigned long active, inactive; |
| 1046 | |
| 1047 | inactive = mem_cgroup_get_all_zonestat(mem_cont, |
| 1048 | MEM_CGROUP_ZSTAT_INACTIVE); |
| 1049 | active = mem_cgroup_get_all_zonestat(mem_cont, |
| 1050 | MEM_CGROUP_ZSTAT_ACTIVE); |
| 1051 | seq_printf(m, "active %ld\n", (active) * PAGE_SIZE); |
| 1052 | seq_printf(m, "inactive %ld\n", (inactive) * PAGE_SIZE); |
| 1053 | } |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | static const struct file_operations mem_control_stat_file_operations = { |
| 1058 | .read = seq_read, |
| 1059 | .llseek = seq_lseek, |
| 1060 | .release = single_release, |
| 1061 | }; |
| 1062 | |
| 1063 | static int mem_control_stat_open(struct inode *unused, struct file *file) |
| 1064 | { |
| 1065 | /* XXX __d_cont */ |
| 1066 | struct cgroup *cont = file->f_dentry->d_parent->d_fsdata; |
| 1067 | |
| 1068 | file->f_op = &mem_control_stat_file_operations; |
| 1069 | return single_open(file, mem_control_stat_show, cont); |
| 1070 | } |
| 1071 | |
| 1072 | |
| 1073 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1074 | static struct cftype mem_cgroup_files[] = { |
| 1075 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 1076 | .name = "usage_in_bytes", |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1077 | .private = RES_USAGE, |
| 1078 | .read = mem_cgroup_read, |
| 1079 | }, |
| 1080 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 1081 | .name = "limit_in_bytes", |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1082 | .private = RES_LIMIT, |
| 1083 | .write = mem_cgroup_write, |
| 1084 | .read = mem_cgroup_read, |
| 1085 | }, |
| 1086 | { |
| 1087 | .name = "failcnt", |
| 1088 | .private = RES_FAILCNT, |
| 1089 | .read = mem_cgroup_read, |
| 1090 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1091 | { |
| 1092 | .name = "control_type", |
| 1093 | .write = mem_control_type_write, |
| 1094 | .read = mem_control_type_read, |
| 1095 | }, |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1096 | { |
| 1097 | .name = "force_empty", |
| 1098 | .write = mem_force_empty_write, |
| 1099 | .read = mem_force_empty_read, |
| 1100 | }, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1101 | { |
| 1102 | .name = "stat", |
| 1103 | .open = mem_control_stat_open, |
| 1104 | }, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1105 | }; |
| 1106 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1107 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 1108 | { |
| 1109 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1110 | struct mem_cgroup_per_zone *mz; |
| 1111 | int zone; |
| 1112 | /* |
| 1113 | * This routine is called against possible nodes. |
| 1114 | * But it's BUG to call kmalloc() against offline node. |
| 1115 | * |
| 1116 | * TODO: this routine can waste much memory for nodes which will |
| 1117 | * never be onlined. It's better to use memory hotplug callback |
| 1118 | * function. |
| 1119 | */ |
| 1120 | if (node_state(node, N_HIGH_MEMORY)) |
| 1121 | pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, node); |
| 1122 | else |
| 1123 | pn = kmalloc(sizeof(*pn), GFP_KERNEL); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1124 | if (!pn) |
| 1125 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1126 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1127 | mem->info.nodeinfo[node] = pn; |
| 1128 | memset(pn, 0, sizeof(*pn)); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1129 | |
| 1130 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 1131 | mz = &pn->zoneinfo[zone]; |
| 1132 | INIT_LIST_HEAD(&mz->active_list); |
| 1133 | INIT_LIST_HEAD(&mz->inactive_list); |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame^] | 1134 | spin_lock_init(&mz->lru_lock); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1135 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1136 | return 0; |
| 1137 | } |
| 1138 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1139 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 1140 | { |
| 1141 | kfree(mem->info.nodeinfo[node]); |
| 1142 | } |
| 1143 | |
| 1144 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1145 | static struct mem_cgroup init_mem_cgroup; |
| 1146 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1147 | static struct cgroup_subsys_state * |
| 1148 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 1149 | { |
| 1150 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1151 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1152 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1153 | if (unlikely((cont->parent) == NULL)) { |
| 1154 | mem = &init_mem_cgroup; |
| 1155 | init_mm.mem_cgroup = mem; |
| 1156 | } else |
| 1157 | mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL); |
| 1158 | |
| 1159 | if (mem == NULL) |
| 1160 | return NULL; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1161 | |
| 1162 | res_counter_init(&mem->res); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1163 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1164 | mem->control_type = MEM_CGROUP_TYPE_ALL; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1165 | memset(&mem->info, 0, sizeof(mem->info)); |
| 1166 | |
| 1167 | for_each_node_state(node, N_POSSIBLE) |
| 1168 | if (alloc_mem_cgroup_per_zone_info(mem, node)) |
| 1169 | goto free_out; |
| 1170 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1171 | return &mem->css; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1172 | free_out: |
| 1173 | for_each_node_state(node, N_POSSIBLE) |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1174 | free_mem_cgroup_per_zone_info(mem, node); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1175 | if (cont->parent != NULL) |
| 1176 | kfree(mem); |
| 1177 | return NULL; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 1180 | static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
| 1181 | struct cgroup *cont) |
| 1182 | { |
| 1183 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 1184 | mem_cgroup_force_empty(mem); |
| 1185 | } |
| 1186 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1187 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 1188 | struct cgroup *cont) |
| 1189 | { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1190 | int node; |
| 1191 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 1192 | |
| 1193 | for_each_node_state(node, N_POSSIBLE) |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1194 | free_mem_cgroup_per_zone_info(mem, node); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1195 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1196 | kfree(mem_cgroup_from_cont(cont)); |
| 1197 | } |
| 1198 | |
| 1199 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 1200 | struct cgroup *cont) |
| 1201 | { |
| 1202 | return cgroup_add_files(cont, ss, mem_cgroup_files, |
| 1203 | ARRAY_SIZE(mem_cgroup_files)); |
| 1204 | } |
| 1205 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1206 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 1207 | struct cgroup *cont, |
| 1208 | struct cgroup *old_cont, |
| 1209 | struct task_struct *p) |
| 1210 | { |
| 1211 | struct mm_struct *mm; |
| 1212 | struct mem_cgroup *mem, *old_mem; |
| 1213 | |
| 1214 | mm = get_task_mm(p); |
| 1215 | if (mm == NULL) |
| 1216 | return; |
| 1217 | |
| 1218 | mem = mem_cgroup_from_cont(cont); |
| 1219 | old_mem = mem_cgroup_from_cont(old_cont); |
| 1220 | |
| 1221 | if (mem == old_mem) |
| 1222 | goto out; |
| 1223 | |
| 1224 | /* |
| 1225 | * Only thread group leaders are allowed to migrate, the mm_struct is |
| 1226 | * in effect owned by the leader |
| 1227 | */ |
| 1228 | if (p->tgid != p->pid) |
| 1229 | goto out; |
| 1230 | |
| 1231 | css_get(&mem->css); |
| 1232 | rcu_assign_pointer(mm->mem_cgroup, mem); |
| 1233 | css_put(&old_mem->css); |
| 1234 | |
| 1235 | out: |
| 1236 | mmput(mm); |
| 1237 | return; |
| 1238 | } |
| 1239 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1240 | struct cgroup_subsys mem_cgroup_subsys = { |
| 1241 | .name = "memory", |
| 1242 | .subsys_id = mem_cgroup_subsys_id, |
| 1243 | .create = mem_cgroup_create, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 1244 | .pre_destroy = mem_cgroup_pre_destroy, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1245 | .destroy = mem_cgroup_destroy, |
| 1246 | .populate = mem_cgroup_populate, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1247 | .attach = mem_cgroup_move_task, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1248 | .early_init = 0, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1249 | }; |