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