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 | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 29 | #include <linux/slab.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 30 | #include <linux/swap.h> |
| 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/fs.h> |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 33 | #include <linux/seq_file.h> |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 34 | #include <linux/vmalloc.h> |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 35 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 36 | #include <asm/uaccess.h> |
| 37 | |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 38 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
| 39 | static struct kmem_cache *page_cgroup_cache __read_mostly; |
| 40 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 41 | |
| 42 | /* |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 43 | * Statistics for memory cgroup. |
| 44 | */ |
| 45 | enum mem_cgroup_stat_index { |
| 46 | /* |
| 47 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. |
| 48 | */ |
| 49 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ |
| 50 | MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */ |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 51 | MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */ |
| 52 | MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 53 | |
| 54 | MEM_CGROUP_STAT_NSTATS, |
| 55 | }; |
| 56 | |
| 57 | struct mem_cgroup_stat_cpu { |
| 58 | s64 count[MEM_CGROUP_STAT_NSTATS]; |
| 59 | } ____cacheline_aligned_in_smp; |
| 60 | |
| 61 | struct mem_cgroup_stat { |
| 62 | struct mem_cgroup_stat_cpu cpustat[NR_CPUS]; |
| 63 | }; |
| 64 | |
| 65 | /* |
| 66 | * For accounting under irq disable, no need for increment preempt count. |
| 67 | */ |
| 68 | static void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat *stat, |
| 69 | enum mem_cgroup_stat_index idx, int val) |
| 70 | { |
| 71 | int cpu = smp_processor_id(); |
| 72 | stat->cpustat[cpu].count[idx] += val; |
| 73 | } |
| 74 | |
| 75 | static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat, |
| 76 | enum mem_cgroup_stat_index idx) |
| 77 | { |
| 78 | int cpu; |
| 79 | s64 ret = 0; |
| 80 | for_each_possible_cpu(cpu) |
| 81 | ret += stat->cpustat[cpu].count[idx]; |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | /* |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 86 | * per-zone information in memory controller. |
| 87 | */ |
| 88 | |
| 89 | enum mem_cgroup_zstat_index { |
| 90 | MEM_CGROUP_ZSTAT_ACTIVE, |
| 91 | MEM_CGROUP_ZSTAT_INACTIVE, |
| 92 | |
| 93 | NR_MEM_CGROUP_ZSTAT, |
| 94 | }; |
| 95 | |
| 96 | struct mem_cgroup_per_zone { |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 97 | /* |
| 98 | * spin_lock to protect the per cgroup LRU |
| 99 | */ |
| 100 | spinlock_t lru_lock; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 101 | struct list_head active_list; |
| 102 | struct list_head inactive_list; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 103 | unsigned long count[NR_MEM_CGROUP_ZSTAT]; |
| 104 | }; |
| 105 | /* Macro for accessing counter */ |
| 106 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) |
| 107 | |
| 108 | struct mem_cgroup_per_node { |
| 109 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; |
| 110 | }; |
| 111 | |
| 112 | struct mem_cgroup_lru_info { |
| 113 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; |
| 114 | }; |
| 115 | |
| 116 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 117 | * The memory controller data structure. The memory controller controls both |
| 118 | * page cache and RSS per cgroup. We would eventually like to provide |
| 119 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, |
| 120 | * to help the administrator determine what knobs to tune. |
| 121 | * |
| 122 | * 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] | 123 | * we hit the water mark. May be even add a low water mark, such that |
| 124 | * no reclaim occurs from a cgroup at it's low water mark, this is |
| 125 | * a feature that will be implemented much later in the future. |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 126 | */ |
| 127 | struct mem_cgroup { |
| 128 | struct cgroup_subsys_state css; |
| 129 | /* |
| 130 | * the counter to account for memory usage |
| 131 | */ |
| 132 | struct res_counter res; |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 133 | /* |
| 134 | * Per cgroup active and inactive list, similar to the |
| 135 | * per zone LRU lists. |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 136 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 137 | struct mem_cgroup_lru_info info; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 138 | |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 139 | int prev_priority; /* for recording reclaim priority */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 140 | /* |
| 141 | * statistics. |
| 142 | */ |
| 143 | struct mem_cgroup_stat stat; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 144 | }; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 145 | static struct mem_cgroup init_mem_cgroup; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 146 | |
| 147 | /* |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 148 | * 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] | 149 | * lock. We need to ensure that page->page_cgroup is at least two |
| 150 | * byte aligned (based on comments from Nick Piggin). But since |
| 151 | * bit_spin_lock doesn't actually set that lock bit in a non-debug |
| 152 | * uniprocessor kernel, we should avoid setting it here too. |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 153 | */ |
| 154 | #define PAGE_CGROUP_LOCK_BIT 0x0 |
Hugh Dickins | 9442ec9 | 2008-03-04 14:29:07 -0800 | [diff] [blame] | 155 | #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) |
| 156 | #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT) |
| 157 | #else |
| 158 | #define PAGE_CGROUP_LOCK 0x0 |
| 159 | #endif |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 160 | |
| 161 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 162 | * A page_cgroup page is associated with every page descriptor. The |
| 163 | * page_cgroup helps us identify information about the cgroup |
| 164 | */ |
| 165 | struct page_cgroup { |
| 166 | struct list_head lru; /* per cgroup LRU list */ |
| 167 | struct page *page; |
| 168 | struct mem_cgroup *mem_cgroup; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 169 | int flags; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 170 | }; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 171 | #define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */ |
KAMEZAWA Hiroyuki | 3564c7c | 2008-02-07 00:14:23 -0800 | [diff] [blame] | 172 | #define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */ |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 173 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 174 | static int page_cgroup_nid(struct page_cgroup *pc) |
KAMEZAWA Hiroyuki | c0149530 | 2008-02-07 00:14:30 -0800 | [diff] [blame] | 175 | { |
| 176 | return page_to_nid(pc->page); |
| 177 | } |
| 178 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 179 | static enum zone_type page_cgroup_zid(struct page_cgroup *pc) |
KAMEZAWA Hiroyuki | c0149530 | 2008-02-07 00:14:30 -0800 | [diff] [blame] | 180 | { |
| 181 | return page_zonenum(pc->page); |
| 182 | } |
| 183 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 184 | enum charge_type { |
| 185 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
| 186 | MEM_CGROUP_CHARGE_TYPE_MAPPED, |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 187 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 188 | }; |
| 189 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 190 | /* |
| 191 | * Always modified under lru lock. Then, not necessary to preempt_disable() |
| 192 | */ |
| 193 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags, |
| 194 | bool charge) |
| 195 | { |
| 196 | int val = (charge)? 1 : -1; |
| 197 | struct mem_cgroup_stat *stat = &mem->stat; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 198 | |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 199 | VM_BUG_ON(!irqs_disabled()); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 200 | if (flags & PAGE_CGROUP_FLAG_CACHE) |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 201 | __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_CACHE, val); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 202 | else |
| 203 | __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val); |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 204 | |
| 205 | if (charge) |
| 206 | __mem_cgroup_stat_add_safe(stat, |
| 207 | MEM_CGROUP_STAT_PGPGIN_COUNT, 1); |
| 208 | else |
| 209 | __mem_cgroup_stat_add_safe(stat, |
| 210 | MEM_CGROUP_STAT_PGPGOUT_COUNT, 1); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 211 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 212 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 213 | static struct mem_cgroup_per_zone * |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 214 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) |
| 215 | { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 216 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; |
| 217 | } |
| 218 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 219 | static struct mem_cgroup_per_zone * |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 220 | page_cgroup_zoneinfo(struct page_cgroup *pc) |
| 221 | { |
| 222 | struct mem_cgroup *mem = pc->mem_cgroup; |
| 223 | int nid = page_cgroup_nid(pc); |
| 224 | int zid = page_cgroup_zid(pc); |
| 225 | |
| 226 | return mem_cgroup_zoneinfo(mem, nid, zid); |
| 227 | } |
| 228 | |
| 229 | static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem, |
| 230 | enum mem_cgroup_zstat_index idx) |
| 231 | { |
| 232 | int nid, zid; |
| 233 | struct mem_cgroup_per_zone *mz; |
| 234 | u64 total = 0; |
| 235 | |
| 236 | for_each_online_node(nid) |
| 237 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 238 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 239 | total += MEM_CGROUP_ZSTAT(mz, idx); |
| 240 | } |
| 241 | return total; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 244 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 245 | { |
| 246 | return container_of(cgroup_subsys_state(cont, |
| 247 | mem_cgroup_subsys_id), struct mem_cgroup, |
| 248 | css); |
| 249 | } |
| 250 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 251 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 252 | { |
| 253 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
| 254 | struct mem_cgroup, css); |
| 255 | } |
| 256 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 257 | static inline int page_cgroup_locked(struct page *page) |
| 258 | { |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 259 | return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Hugh Dickins | 9442ec9 | 2008-03-04 14:29:07 -0800 | [diff] [blame] | 262 | 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] | 263 | { |
Hugh Dickins | 9442ec9 | 2008-03-04 14:29:07 -0800 | [diff] [blame] | 264 | VM_BUG_ON(!page_cgroup_locked(page)); |
| 265 | page->page_cgroup = ((unsigned long)pc | PAGE_CGROUP_LOCK); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | struct page_cgroup *page_get_page_cgroup(struct page *page) |
| 269 | { |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 270 | return (struct page_cgroup *) (page->page_cgroup & ~PAGE_CGROUP_LOCK); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 273 | static void lock_page_cgroup(struct page *page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 274 | { |
| 275 | bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Hugh Dickins | 2680eed | 2008-03-04 14:29:13 -0800 | [diff] [blame] | 278 | static int try_lock_page_cgroup(struct page *page) |
| 279 | { |
| 280 | return bit_spin_trylock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); |
| 281 | } |
| 282 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 283 | static void unlock_page_cgroup(struct page *page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 284 | { |
| 285 | bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); |
| 286 | } |
| 287 | |
KAMEZAWA Hiroyuki | 3eae90c | 2008-04-29 01:00:22 -0700 | [diff] [blame] | 288 | static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz, |
| 289 | struct page_cgroup *pc) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 290 | { |
| 291 | int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 292 | |
| 293 | if (from) |
| 294 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1; |
| 295 | else |
| 296 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1; |
| 297 | |
| 298 | mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false); |
KAMEZAWA Hiroyuki | 508b7be | 2008-07-25 01:47:09 -0700 | [diff] [blame] | 299 | list_del(&pc->lru); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 300 | } |
| 301 | |
KAMEZAWA Hiroyuki | 3eae90c | 2008-04-29 01:00:22 -0700 | [diff] [blame] | 302 | static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz, |
| 303 | struct page_cgroup *pc) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 304 | { |
| 305 | int to = pc->flags & PAGE_CGROUP_FLAG_ACTIVE; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 306 | |
| 307 | if (!to) { |
| 308 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 309 | list_add(&pc->lru, &mz->inactive_list); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 310 | } else { |
| 311 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 312 | list_add(&pc->lru, &mz->active_list); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 313 | } |
| 314 | mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true); |
| 315 | } |
| 316 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 317 | static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 318 | { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 319 | int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE; |
| 320 | struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc); |
| 321 | |
| 322 | if (from) |
| 323 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1; |
| 324 | else |
| 325 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1; |
| 326 | |
KAMEZAWA Hiroyuki | 3564c7c | 2008-02-07 00:14:23 -0800 | [diff] [blame] | 327 | if (active) { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 328 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1; |
KAMEZAWA Hiroyuki | 3564c7c | 2008-02-07 00:14:23 -0800 | [diff] [blame] | 329 | pc->flags |= PAGE_CGROUP_FLAG_ACTIVE; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 330 | list_move(&pc->lru, &mz->active_list); |
KAMEZAWA Hiroyuki | 3564c7c | 2008-02-07 00:14:23 -0800 | [diff] [blame] | 331 | } else { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 332 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1; |
KAMEZAWA Hiroyuki | 3564c7c | 2008-02-07 00:14:23 -0800 | [diff] [blame] | 333 | pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 334 | list_move(&pc->lru, &mz->inactive_list); |
KAMEZAWA Hiroyuki | 3564c7c | 2008-02-07 00:14:23 -0800 | [diff] [blame] | 335 | } |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 336 | } |
| 337 | |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 338 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
| 339 | { |
| 340 | int ret; |
| 341 | |
| 342 | task_lock(task); |
Hugh Dickins | bd845e3 | 2008-03-04 14:29:01 -0800 | [diff] [blame] | 343 | ret = task->mm && mm_match_cgroup(task->mm, mem); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 344 | task_unlock(task); |
| 345 | return ret; |
| 346 | } |
| 347 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 348 | /* |
| 349 | * This routine assumes that the appropriate zone's lru lock is already held |
| 350 | */ |
Hugh Dickins | 427d541 | 2008-03-04 14:29:03 -0800 | [diff] [blame] | 351 | void mem_cgroup_move_lists(struct page *page, bool active) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 352 | { |
Hugh Dickins | 427d541 | 2008-03-04 14:29:03 -0800 | [diff] [blame] | 353 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 354 | struct mem_cgroup_per_zone *mz; |
| 355 | unsigned long flags; |
| 356 | |
Hugh Dickins | 2680eed | 2008-03-04 14:29:13 -0800 | [diff] [blame] | 357 | /* |
| 358 | * We cannot lock_page_cgroup while holding zone's lru_lock, |
| 359 | * because other holders of lock_page_cgroup can be interrupted |
| 360 | * with an attempt to rotate_reclaimable_page. But we cannot |
| 361 | * safely get to page_cgroup without it, so just try_lock it: |
| 362 | * mem_cgroup_isolate_pages allows for page left on wrong list. |
| 363 | */ |
| 364 | if (!try_lock_page_cgroup(page)) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 365 | return; |
| 366 | |
Hugh Dickins | 2680eed | 2008-03-04 14:29:13 -0800 | [diff] [blame] | 367 | pc = page_get_page_cgroup(page); |
| 368 | if (pc) { |
Hugh Dickins | 2680eed | 2008-03-04 14:29:13 -0800 | [diff] [blame] | 369 | mz = page_cgroup_zoneinfo(pc); |
Hugh Dickins | 2680eed | 2008-03-04 14:29:13 -0800 | [diff] [blame] | 370 | spin_lock_irqsave(&mz->lru_lock, flags); |
Hirokazu Takahashi | 9b3c0a0 | 2008-03-04 14:29:15 -0800 | [diff] [blame] | 371 | __mem_cgroup_move_lists(pc, active); |
Hugh Dickins | 2680eed | 2008-03-04 14:29:13 -0800 | [diff] [blame] | 372 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
Hirokazu Takahashi | 9b3c0a0 | 2008-03-04 14:29:15 -0800 | [diff] [blame] | 373 | } |
| 374 | unlock_page_cgroup(page); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 375 | } |
| 376 | |
KAMEZAWA Hiroyuki | 58ae83d | 2008-02-07 00:14:32 -0800 | [diff] [blame] | 377 | /* |
| 378 | * Calculate mapped_ratio under memory controller. This will be used in |
| 379 | * vmscan.c for deteremining we have to reclaim mapped pages. |
| 380 | */ |
| 381 | int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem) |
| 382 | { |
| 383 | long total, rss; |
| 384 | |
| 385 | /* |
| 386 | * usage is recorded in bytes. But, here, we assume the number of |
| 387 | * physical pages can be represented by "long" on any arch. |
| 388 | */ |
| 389 | total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L; |
| 390 | rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS); |
| 391 | return (int)((rss * 100L) / total); |
| 392 | } |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 393 | |
KAMEZAWA Hiroyuki | 5932f36 | 2008-02-07 00:14:33 -0800 | [diff] [blame] | 394 | /* |
| 395 | * This function is called from vmscan.c. In page reclaiming loop. balance |
| 396 | * between active and inactive list is calculated. For memory controller |
| 397 | * page reclaiming, we should use using mem_cgroup's imbalance rather than |
| 398 | * zone's global lru imbalance. |
| 399 | */ |
| 400 | long mem_cgroup_reclaim_imbalance(struct mem_cgroup *mem) |
| 401 | { |
| 402 | unsigned long active, inactive; |
| 403 | /* active and inactive are the number of pages. 'long' is ok.*/ |
| 404 | active = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_ACTIVE); |
| 405 | inactive = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_INACTIVE); |
| 406 | return (long) (active / (inactive + 1)); |
| 407 | } |
KAMEZAWA Hiroyuki | 58ae83d | 2008-02-07 00:14:32 -0800 | [diff] [blame] | 408 | |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 409 | /* |
| 410 | * prev_priority control...this will be used in memory reclaim path. |
| 411 | */ |
| 412 | int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem) |
| 413 | { |
| 414 | return mem->prev_priority; |
| 415 | } |
| 416 | |
| 417 | void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority) |
| 418 | { |
| 419 | if (priority < mem->prev_priority) |
| 420 | mem->prev_priority = priority; |
| 421 | } |
| 422 | |
| 423 | void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority) |
| 424 | { |
| 425 | mem->prev_priority = priority; |
| 426 | } |
| 427 | |
KAMEZAWA Hiroyuki | cc38108 | 2008-02-07 00:14:35 -0800 | [diff] [blame] | 428 | /* |
| 429 | * Calculate # of pages to be scanned in this priority/zone. |
| 430 | * See also vmscan.c |
| 431 | * |
| 432 | * priority starts from "DEF_PRIORITY" and decremented in each loop. |
| 433 | * (see include/linux/mmzone.h) |
| 434 | */ |
| 435 | |
| 436 | long mem_cgroup_calc_reclaim_active(struct mem_cgroup *mem, |
| 437 | struct zone *zone, int priority) |
| 438 | { |
| 439 | long nr_active; |
| 440 | int nid = zone->zone_pgdat->node_id; |
| 441 | int zid = zone_idx(zone); |
| 442 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 443 | |
| 444 | nr_active = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE); |
| 445 | return (nr_active >> priority); |
| 446 | } |
| 447 | |
| 448 | long mem_cgroup_calc_reclaim_inactive(struct mem_cgroup *mem, |
| 449 | struct zone *zone, int priority) |
| 450 | { |
| 451 | long nr_inactive; |
| 452 | int nid = zone->zone_pgdat->node_id; |
| 453 | int zid = zone_idx(zone); |
| 454 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 455 | |
| 456 | nr_inactive = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE); |
KAMEZAWA Hiroyuki | cc38108 | 2008-02-07 00:14:35 -0800 | [diff] [blame] | 457 | return (nr_inactive >> priority); |
| 458 | } |
| 459 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 460 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 461 | struct list_head *dst, |
| 462 | unsigned long *scanned, int order, |
| 463 | int mode, struct zone *z, |
| 464 | struct mem_cgroup *mem_cont, |
| 465 | int active) |
| 466 | { |
| 467 | unsigned long nr_taken = 0; |
| 468 | struct page *page; |
| 469 | unsigned long scan; |
| 470 | LIST_HEAD(pc_list); |
| 471 | struct list_head *src; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 472 | struct page_cgroup *pc, *tmp; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 473 | int nid = z->zone_pgdat->node_id; |
| 474 | int zid = zone_idx(z); |
| 475 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 476 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 477 | BUG_ON(!mem_cont); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 478 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 479 | if (active) |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 480 | src = &mz->active_list; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 481 | else |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 482 | src = &mz->inactive_list; |
| 483 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 484 | |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 485 | spin_lock(&mz->lru_lock); |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 486 | scan = 0; |
| 487 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 488 | if (scan >= nr_to_scan) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 489 | break; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 490 | page = pc->page; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 491 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 492 | if (unlikely(!PageLRU(page))) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 493 | continue; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 494 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 495 | if (PageActive(page) && !active) { |
| 496 | __mem_cgroup_move_lists(pc, true); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 497 | continue; |
| 498 | } |
| 499 | if (!PageActive(page) && active) { |
| 500 | __mem_cgroup_move_lists(pc, false); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 501 | continue; |
| 502 | } |
| 503 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 504 | scan++; |
| 505 | list_move(&pc->lru, &pc_list); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 506 | |
| 507 | if (__isolate_lru_page(page, mode) == 0) { |
| 508 | list_move(&page->lru, dst); |
| 509 | nr_taken++; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | list_splice(&pc_list, src); |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 514 | spin_unlock(&mz->lru_lock); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 515 | |
| 516 | *scanned = scan; |
| 517 | return nr_taken; |
| 518 | } |
| 519 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 520 | /* |
| 521 | * Charge the memory controller for page usage. |
| 522 | * Return |
| 523 | * 0 if the charge was successful |
| 524 | * < 0 if the cgroup is over its limit |
| 525 | */ |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 526 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 527 | gfp_t gfp_mask, enum charge_type ctype, |
| 528 | struct mem_cgroup *memcg) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 529 | { |
| 530 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 531 | struct page_cgroup *pc; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 532 | unsigned long flags; |
| 533 | unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 534 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 535 | |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 536 | if (mem_cgroup_subsys.disabled) |
| 537 | return 0; |
| 538 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 539 | /* |
| 540 | * Should page_cgroup's go to their own slab? |
| 541 | * One could optimize the performance of the charging routine |
| 542 | * by saving a bit in the page_flags and using it as a lock |
| 543 | * to see if the cgroup page already has a page_cgroup associated |
| 544 | * with it |
| 545 | */ |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 546 | retry: |
Hugh Dickins | 7e924aa | 2008-03-04 14:29:08 -0800 | [diff] [blame] | 547 | lock_page_cgroup(page); |
| 548 | pc = page_get_page_cgroup(page); |
| 549 | /* |
| 550 | * The page_cgroup exists and |
| 551 | * the page has already been accounted. |
| 552 | */ |
KAMEZAWA Hiroyuki | b76734e | 2008-07-25 01:47:16 -0700 | [diff] [blame^] | 553 | if (unlikely(pc)) { |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 554 | VM_BUG_ON(pc->page != page); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 555 | VM_BUG_ON(!pc->mem_cgroup); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 556 | unlock_page_cgroup(page); |
| 557 | goto done; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 558 | } |
Hugh Dickins | 7e924aa | 2008-03-04 14:29:08 -0800 | [diff] [blame] | 559 | unlock_page_cgroup(page); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 560 | |
KAMEZAWA Hiroyuki | 508b7be | 2008-07-25 01:47:09 -0700 | [diff] [blame] | 561 | pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask); |
KAMEZAWA Hiroyuki | b76734e | 2008-07-25 01:47:16 -0700 | [diff] [blame^] | 562 | if (unlikely(pc == NULL)) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 563 | goto err; |
| 564 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 565 | /* |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 566 | * We always charge the cgroup the mm_struct belongs to. |
| 567 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 568 | * thread group leader migrates. It's possible that mm is not |
| 569 | * set, if so charge the init_mm (happens for pagecache usage). |
| 570 | */ |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 571 | if (likely(!memcg)) { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 572 | rcu_read_lock(); |
| 573 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 574 | /* |
| 575 | * For every charge from the cgroup, increment reference count |
| 576 | */ |
| 577 | css_get(&mem->css); |
| 578 | rcu_read_unlock(); |
| 579 | } else { |
| 580 | mem = memcg; |
| 581 | css_get(&memcg->css); |
| 582 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 583 | |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 584 | while (res_counter_charge(&mem->res, PAGE_SIZE)) { |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 585 | if (!(gfp_mask & __GFP_WAIT)) |
| 586 | goto out; |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 587 | |
| 588 | if (try_to_free_mem_cgroup_pages(mem, gfp_mask)) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 589 | continue; |
| 590 | |
| 591 | /* |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 592 | * try_to_free_mem_cgroup_pages() might not give us a full |
| 593 | * picture of reclaim. Some pages are reclaimed and might be |
| 594 | * moved to swap cache or just unmapped from the cgroup. |
| 595 | * Check the limit again to see if the reclaim reduced the |
| 596 | * current usage of the cgroup before giving up |
| 597 | */ |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 598 | if (res_counter_check_under_limit(&mem->res)) |
| 599 | continue; |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 600 | |
| 601 | if (!nr_retries--) { |
| 602 | mem_cgroup_out_of_memory(mem, gfp_mask); |
| 603 | goto out; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 604 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 605 | } |
| 606 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 607 | pc->mem_cgroup = mem; |
| 608 | pc->page = page; |
KAMEZAWA Hiroyuki | 508b7be | 2008-07-25 01:47:09 -0700 | [diff] [blame] | 609 | /* |
| 610 | * If a page is accounted as a page cache, insert to inactive list. |
| 611 | * If anon, insert to active list. |
| 612 | */ |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 613 | if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE) |
Balbir Singh | 4a56d02 | 2008-04-29 01:00:23 -0700 | [diff] [blame] | 614 | pc->flags = PAGE_CGROUP_FLAG_CACHE; |
KAMEZAWA Hiroyuki | 508b7be | 2008-07-25 01:47:09 -0700 | [diff] [blame] | 615 | else |
| 616 | pc->flags = PAGE_CGROUP_FLAG_ACTIVE; |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 617 | |
Hugh Dickins | 7e924aa | 2008-03-04 14:29:08 -0800 | [diff] [blame] | 618 | lock_page_cgroup(page); |
KAMEZAWA Hiroyuki | b76734e | 2008-07-25 01:47:16 -0700 | [diff] [blame^] | 619 | if (unlikely(page_get_page_cgroup(page))) { |
Hugh Dickins | 7e924aa | 2008-03-04 14:29:08 -0800 | [diff] [blame] | 620 | unlock_page_cgroup(page); |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 621 | /* |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 622 | * Another charge has been added to this page already. |
| 623 | * We take lock_page_cgroup(page) again and read |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 624 | * page->cgroup, increment refcnt.... just retry is OK. |
| 625 | */ |
| 626 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 627 | css_put(&mem->css); |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 628 | kmem_cache_free(page_cgroup_cache, pc); |
KAMEZAWA Hiroyuki | 9175e03 | 2008-02-07 00:14:08 -0800 | [diff] [blame] | 629 | goto retry; |
| 630 | } |
Hugh Dickins | 7e924aa | 2008-03-04 14:29:08 -0800 | [diff] [blame] | 631 | page_assign_page_cgroup(page, pc); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 632 | |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 633 | mz = page_cgroup_zoneinfo(pc); |
| 634 | spin_lock_irqsave(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | 3eae90c | 2008-04-29 01:00:22 -0700 | [diff] [blame] | 635 | __mem_cgroup_add_list(mz, pc); |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 636 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 637 | |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 638 | unlock_page_cgroup(page); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 639 | done: |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 640 | return 0; |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 641 | out: |
| 642 | css_put(&mem->css); |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 643 | kmem_cache_free(page_cgroup_cache, pc); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 644 | err: |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 645 | return -ENOMEM; |
| 646 | } |
| 647 | |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 648 | 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] | 649 | { |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 650 | /* |
| 651 | * If already mapped, we don't have to account. |
| 652 | * If page cache, page->mapping has address_space. |
| 653 | * But page->mapping may have out-of-use anon_vma pointer, |
| 654 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping |
| 655 | * is NULL. |
| 656 | */ |
| 657 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) |
| 658 | return 0; |
| 659 | if (unlikely(!mm)) |
| 660 | mm = &init_mm; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 661 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 662 | MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL); |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 663 | } |
| 664 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 665 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 666 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 667 | { |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 668 | if (unlikely(!mm)) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 669 | mm = &init_mm; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 670 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 671 | MEM_CGROUP_CHARGE_TYPE_CACHE, NULL); |
| 672 | } |
| 673 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 674 | /* |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 675 | * uncharge if !page_mapped(page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 676 | */ |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 677 | static void |
| 678 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 679 | { |
Hugh Dickins | 8289546 | 2008-03-04 14:29:08 -0800 | [diff] [blame] | 680 | struct page_cgroup *pc; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 681 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 682 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 683 | unsigned long flags; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 684 | |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 685 | if (mem_cgroup_subsys.disabled) |
| 686 | return; |
| 687 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 688 | /* |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 689 | * Check if our page_cgroup is valid |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 690 | */ |
Hugh Dickins | 8289546 | 2008-03-04 14:29:08 -0800 | [diff] [blame] | 691 | lock_page_cgroup(page); |
| 692 | pc = page_get_page_cgroup(page); |
KAMEZAWA Hiroyuki | b76734e | 2008-07-25 01:47:16 -0700 | [diff] [blame^] | 693 | if (unlikely(!pc)) |
Hugh Dickins | 8289546 | 2008-03-04 14:29:08 -0800 | [diff] [blame] | 694 | goto unlock; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 695 | |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 696 | VM_BUG_ON(pc->page != page); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 697 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 698 | if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED) |
| 699 | && ((pc->flags & PAGE_CGROUP_FLAG_CACHE) |
| 700 | || page_mapped(page))) |
| 701 | goto unlock; |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 702 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 703 | mz = page_cgroup_zoneinfo(pc); |
| 704 | spin_lock_irqsave(&mz->lru_lock, flags); |
| 705 | __mem_cgroup_remove_list(mz, pc); |
| 706 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 707 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 708 | page_assign_page_cgroup(page, NULL); |
| 709 | unlock_page_cgroup(page); |
Hugh Dickins | 6d48ff8 | 2008-03-04 14:29:12 -0800 | [diff] [blame] | 710 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 711 | mem = pc->mem_cgroup; |
| 712 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 713 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 714 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 715 | kmem_cache_free(page_cgroup_cache, pc); |
| 716 | return; |
Hugh Dickins | 8289546 | 2008-03-04 14:29:08 -0800 | [diff] [blame] | 717 | unlock: |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 718 | unlock_page_cgroup(page); |
| 719 | } |
| 720 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 721 | void mem_cgroup_uncharge_page(struct page *page) |
| 722 | { |
| 723 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 724 | } |
| 725 | |
| 726 | void mem_cgroup_uncharge_cache_page(struct page *page) |
| 727 | { |
| 728 | VM_BUG_ON(page_mapped(page)); |
| 729 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 730 | } |
| 731 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 732 | /* |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 733 | * Before starting migration, account against new page. |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 734 | */ |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 735 | int mem_cgroup_prepare_migration(struct page *page, struct page *newpage) |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 736 | { |
| 737 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 738 | struct mem_cgroup *mem = NULL; |
| 739 | enum charge_type ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; |
| 740 | int ret = 0; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 741 | |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 742 | if (mem_cgroup_subsys.disabled) |
| 743 | return 0; |
| 744 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 745 | lock_page_cgroup(page); |
| 746 | pc = page_get_page_cgroup(page); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 747 | if (pc) { |
| 748 | mem = pc->mem_cgroup; |
| 749 | css_get(&mem->css); |
| 750 | if (pc->flags & PAGE_CGROUP_FLAG_CACHE) |
| 751 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 752 | } |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 753 | unlock_page_cgroup(page); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 754 | if (mem) { |
| 755 | ret = mem_cgroup_charge_common(newpage, NULL, GFP_KERNEL, |
| 756 | ctype, mem); |
| 757 | css_put(&mem->css); |
| 758 | } |
| 759 | return ret; |
| 760 | } |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 761 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 762 | /* remove redundant charge if migration failed*/ |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 763 | void mem_cgroup_end_migration(struct page *newpage) |
| 764 | { |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 765 | /* |
| 766 | * At success, page->mapping is not NULL. |
| 767 | * special rollback care is necessary when |
| 768 | * 1. at migration failure. (newpage->mapping is cleared in this case) |
| 769 | * 2. the newpage was moved but not remapped again because the task |
| 770 | * exits and the newpage is obsolete. In this case, the new page |
| 771 | * may be a swapcache. So, we just call mem_cgroup_uncharge_page() |
| 772 | * always for avoiding mess. The page_cgroup will be removed if |
| 773 | * unnecessary. File cache pages is still on radix-tree. Don't |
| 774 | * care it. |
| 775 | */ |
| 776 | if (!newpage->mapping) |
| 777 | __mem_cgroup_uncharge_common(newpage, |
| 778 | MEM_CGROUP_CHARGE_TYPE_FORCE); |
| 779 | else if (PageAnon(newpage)) |
| 780 | mem_cgroup_uncharge_page(newpage); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 781 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 782 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 783 | /* |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 784 | * A call to try to shrink memory usage under specified resource controller. |
| 785 | * This is typically used for page reclaiming for shmem for reducing side |
| 786 | * effect of page allocation from shmem, which is used by some mem_cgroup. |
| 787 | */ |
| 788 | int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask) |
| 789 | { |
| 790 | struct mem_cgroup *mem; |
| 791 | int progress = 0; |
| 792 | int retry = MEM_CGROUP_RECLAIM_RETRIES; |
| 793 | |
| 794 | rcu_read_lock(); |
| 795 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 796 | css_get(&mem->css); |
| 797 | rcu_read_unlock(); |
| 798 | |
| 799 | do { |
| 800 | progress = try_to_free_mem_cgroup_pages(mem, gfp_mask); |
| 801 | } while (!progress && --retry); |
| 802 | |
| 803 | css_put(&mem->css); |
| 804 | if (!retry) |
| 805 | return -ENOMEM; |
| 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | /* |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 810 | * This routine traverse page_cgroup in given list and drop them all. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 811 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
| 812 | */ |
| 813 | #define FORCE_UNCHARGE_BATCH (128) |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 814 | static void mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 815 | struct mem_cgroup_per_zone *mz, |
| 816 | int active) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 817 | { |
| 818 | struct page_cgroup *pc; |
| 819 | struct page *page; |
Hirokazu Takahashi | 9b3c0a0 | 2008-03-04 14:29:15 -0800 | [diff] [blame] | 820 | int count = FORCE_UNCHARGE_BATCH; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 821 | unsigned long flags; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 822 | struct list_head *list; |
| 823 | |
| 824 | if (active) |
| 825 | list = &mz->active_list; |
| 826 | else |
| 827 | list = &mz->inactive_list; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 828 | |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 829 | spin_lock_irqsave(&mz->lru_lock, flags); |
Hirokazu Takahashi | 9b3c0a0 | 2008-03-04 14:29:15 -0800 | [diff] [blame] | 830 | while (!list_empty(list)) { |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 831 | pc = list_entry(list->prev, struct page_cgroup, lru); |
| 832 | page = pc->page; |
Hirokazu Takahashi | 9b3c0a0 | 2008-03-04 14:29:15 -0800 | [diff] [blame] | 833 | get_page(page); |
| 834 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 835 | /* |
| 836 | * Check if this page is on LRU. !LRU page can be found |
| 837 | * if it's under page migration. |
| 838 | */ |
| 839 | if (PageLRU(page)) { |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 840 | __mem_cgroup_uncharge_common(page, |
| 841 | MEM_CGROUP_CHARGE_TYPE_FORCE); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 842 | put_page(page); |
| 843 | if (--count <= 0) { |
| 844 | count = FORCE_UNCHARGE_BATCH; |
| 845 | cond_resched(); |
| 846 | } |
| 847 | } else |
Hirokazu Takahashi | 9b3c0a0 | 2008-03-04 14:29:15 -0800 | [diff] [blame] | 848 | cond_resched(); |
Hirokazu Takahashi | 9b3c0a0 | 2008-03-04 14:29:15 -0800 | [diff] [blame] | 849 | spin_lock_irqsave(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 850 | } |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 851 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /* |
| 855 | * make mem_cgroup's charge to be 0 if there is no task. |
| 856 | * This enables deleting this mem_cgroup. |
| 857 | */ |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 858 | static int mem_cgroup_force_empty(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 859 | { |
| 860 | int ret = -EBUSY; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 861 | int node, zid; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 862 | |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 863 | if (mem_cgroup_subsys.disabled) |
| 864 | return 0; |
| 865 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 866 | css_get(&mem->css); |
| 867 | /* |
| 868 | * page reclaim code (kswapd etc..) will move pages between |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 869 | * active_list <-> inactive_list while we don't take a lock. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 870 | * So, we have to do loop here until all lists are empty. |
| 871 | */ |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 872 | while (mem->res.usage > 0) { |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 873 | if (atomic_read(&mem->css.cgroup->count) > 0) |
| 874 | goto out; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 875 | for_each_node_state(node, N_POSSIBLE) |
| 876 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 877 | struct mem_cgroup_per_zone *mz; |
| 878 | mz = mem_cgroup_zoneinfo(mem, node, zid); |
| 879 | /* drop all page_cgroup in active_list */ |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 880 | mem_cgroup_force_empty_list(mem, mz, 1); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 881 | /* drop all page_cgroup in inactive_list */ |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 882 | mem_cgroup_force_empty_list(mem, mz, 0); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 883 | } |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 884 | } |
| 885 | ret = 0; |
| 886 | out: |
| 887 | css_put(&mem->css); |
| 888 | return ret; |
| 889 | } |
| 890 | |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 891 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 892 | { |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 893 | return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res, |
| 894 | cft->private); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 895 | } |
| 896 | |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 897 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 898 | const char *buffer) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 899 | { |
| 900 | return res_counter_write(&mem_cgroup_from_cont(cont)->res, |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 901 | cft->private, buffer, |
| 902 | res_counter_memparse_write_strategy); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 903 | } |
| 904 | |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 905 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 906 | { |
| 907 | struct mem_cgroup *mem; |
| 908 | |
| 909 | mem = mem_cgroup_from_cont(cont); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 910 | switch (event) { |
| 911 | case RES_MAX_USAGE: |
| 912 | res_counter_reset_max(&mem->res); |
| 913 | break; |
| 914 | case RES_FAILCNT: |
| 915 | res_counter_reset_failcnt(&mem->res); |
| 916 | break; |
| 917 | } |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 918 | return 0; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 921 | static int mem_force_empty_write(struct cgroup *cont, unsigned int event) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 922 | { |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 923 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont)); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 924 | } |
| 925 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 926 | static const struct mem_cgroup_stat_desc { |
| 927 | const char *msg; |
| 928 | u64 unit; |
| 929 | } mem_cgroup_stat_desc[] = { |
| 930 | [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, }, |
| 931 | [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, }, |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 932 | [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, }, |
| 933 | [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, }, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 934 | }; |
| 935 | |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 936 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
| 937 | struct cgroup_map_cb *cb) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 938 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 939 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
| 940 | struct mem_cgroup_stat *stat = &mem_cont->stat; |
| 941 | int i; |
| 942 | |
| 943 | for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) { |
| 944 | s64 val; |
| 945 | |
| 946 | val = mem_cgroup_read_stat(stat, i); |
| 947 | val *= mem_cgroup_stat_desc[i].unit; |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 948 | cb->fill(cb, mem_cgroup_stat_desc[i].msg, val); |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 949 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 950 | /* showing # of active pages */ |
| 951 | { |
| 952 | unsigned long active, inactive; |
| 953 | |
| 954 | inactive = mem_cgroup_get_all_zonestat(mem_cont, |
| 955 | MEM_CGROUP_ZSTAT_INACTIVE); |
| 956 | active = mem_cgroup_get_all_zonestat(mem_cont, |
| 957 | MEM_CGROUP_ZSTAT_ACTIVE); |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 958 | cb->fill(cb, "active", (active) * PAGE_SIZE); |
| 959 | cb->fill(cb, "inactive", (inactive) * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 960 | } |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 961 | return 0; |
| 962 | } |
| 963 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 964 | static struct cftype mem_cgroup_files[] = { |
| 965 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 966 | .name = "usage_in_bytes", |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 967 | .private = RES_USAGE, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 968 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 969 | }, |
| 970 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 971 | .name = "max_usage_in_bytes", |
| 972 | .private = RES_MAX_USAGE, |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 973 | .trigger = mem_cgroup_reset, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 974 | .read_u64 = mem_cgroup_read, |
| 975 | }, |
| 976 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 977 | .name = "limit_in_bytes", |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 978 | .private = RES_LIMIT, |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 979 | .write_string = mem_cgroup_write, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 980 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 981 | }, |
| 982 | { |
| 983 | .name = "failcnt", |
| 984 | .private = RES_FAILCNT, |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 985 | .trigger = mem_cgroup_reset, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 986 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 987 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 988 | { |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 989 | .name = "force_empty", |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 990 | .trigger = mem_force_empty_write, |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 991 | }, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 992 | { |
| 993 | .name = "stat", |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 994 | .read_map = mem_control_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 995 | }, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 996 | }; |
| 997 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 998 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 999 | { |
| 1000 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1001 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 1002 | int zone, tmp = node; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1003 | /* |
| 1004 | * This routine is called against possible nodes. |
| 1005 | * But it's BUG to call kmalloc() against offline node. |
| 1006 | * |
| 1007 | * TODO: this routine can waste much memory for nodes which will |
| 1008 | * never be onlined. It's better to use memory hotplug callback |
| 1009 | * function. |
| 1010 | */ |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 1011 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 1012 | tmp = -1; |
| 1013 | pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1014 | if (!pn) |
| 1015 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1016 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1017 | mem->info.nodeinfo[node] = pn; |
| 1018 | memset(pn, 0, sizeof(*pn)); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1019 | |
| 1020 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 1021 | mz = &pn->zoneinfo[zone]; |
| 1022 | INIT_LIST_HEAD(&mz->active_list); |
| 1023 | INIT_LIST_HEAD(&mz->inactive_list); |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 1024 | spin_lock_init(&mz->lru_lock); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1025 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1026 | return 0; |
| 1027 | } |
| 1028 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1029 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 1030 | { |
| 1031 | kfree(mem->info.nodeinfo[node]); |
| 1032 | } |
| 1033 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1034 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 1035 | { |
| 1036 | struct mem_cgroup *mem; |
| 1037 | |
| 1038 | if (sizeof(*mem) < PAGE_SIZE) |
| 1039 | mem = kmalloc(sizeof(*mem), GFP_KERNEL); |
| 1040 | else |
| 1041 | mem = vmalloc(sizeof(*mem)); |
| 1042 | |
| 1043 | if (mem) |
| 1044 | memset(mem, 0, sizeof(*mem)); |
| 1045 | return mem; |
| 1046 | } |
| 1047 | |
| 1048 | static void mem_cgroup_free(struct mem_cgroup *mem) |
| 1049 | { |
| 1050 | if (sizeof(*mem) < PAGE_SIZE) |
| 1051 | kfree(mem); |
| 1052 | else |
| 1053 | vfree(mem); |
| 1054 | } |
| 1055 | |
| 1056 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1057 | static struct cgroup_subsys_state * |
| 1058 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 1059 | { |
| 1060 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1061 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1062 | |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 1063 | if (unlikely((cont->parent) == NULL)) { |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1064 | mem = &init_mem_cgroup; |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 1065 | page_cgroup_cache = KMEM_CACHE(page_cgroup, SLAB_PANIC); |
| 1066 | } else { |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1067 | mem = mem_cgroup_alloc(); |
| 1068 | if (!mem) |
| 1069 | return ERR_PTR(-ENOMEM); |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 1070 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1071 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1072 | res_counter_init(&mem->res); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1073 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1074 | for_each_node_state(node, N_POSSIBLE) |
| 1075 | if (alloc_mem_cgroup_per_zone_info(mem, node)) |
| 1076 | goto free_out; |
| 1077 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1078 | return &mem->css; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1079 | free_out: |
| 1080 | for_each_node_state(node, N_POSSIBLE) |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1081 | free_mem_cgroup_per_zone_info(mem, node); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1082 | if (cont->parent != NULL) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1083 | mem_cgroup_free(mem); |
Li Zefan | 2dda81c | 2008-02-23 15:24:14 -0800 | [diff] [blame] | 1084 | return ERR_PTR(-ENOMEM); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 1087 | static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
| 1088 | struct cgroup *cont) |
| 1089 | { |
| 1090 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 1091 | mem_cgroup_force_empty(mem); |
| 1092 | } |
| 1093 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1094 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 1095 | struct cgroup *cont) |
| 1096 | { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1097 | int node; |
| 1098 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 1099 | |
| 1100 | for_each_node_state(node, N_POSSIBLE) |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1101 | free_mem_cgroup_per_zone_info(mem, node); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1102 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1103 | mem_cgroup_free(mem_cgroup_from_cont(cont)); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 1107 | struct cgroup *cont) |
| 1108 | { |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 1109 | if (mem_cgroup_subsys.disabled) |
| 1110 | return 0; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1111 | return cgroup_add_files(cont, ss, mem_cgroup_files, |
| 1112 | ARRAY_SIZE(mem_cgroup_files)); |
| 1113 | } |
| 1114 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1115 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 1116 | struct cgroup *cont, |
| 1117 | struct cgroup *old_cont, |
| 1118 | struct task_struct *p) |
| 1119 | { |
| 1120 | struct mm_struct *mm; |
| 1121 | struct mem_cgroup *mem, *old_mem; |
| 1122 | |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 1123 | if (mem_cgroup_subsys.disabled) |
| 1124 | return; |
| 1125 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1126 | mm = get_task_mm(p); |
| 1127 | if (mm == NULL) |
| 1128 | return; |
| 1129 | |
| 1130 | mem = mem_cgroup_from_cont(cont); |
| 1131 | old_mem = mem_cgroup_from_cont(old_cont); |
| 1132 | |
| 1133 | if (mem == old_mem) |
| 1134 | goto out; |
| 1135 | |
| 1136 | /* |
| 1137 | * Only thread group leaders are allowed to migrate, the mm_struct is |
| 1138 | * in effect owned by the leader |
| 1139 | */ |
Pavel Emelyanov | 52ea27e | 2008-03-19 17:00:45 -0700 | [diff] [blame] | 1140 | if (!thread_group_leader(p)) |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1141 | goto out; |
| 1142 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1143 | out: |
| 1144 | mmput(mm); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1145 | } |
| 1146 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1147 | struct cgroup_subsys mem_cgroup_subsys = { |
| 1148 | .name = "memory", |
| 1149 | .subsys_id = mem_cgroup_subsys_id, |
| 1150 | .create = mem_cgroup_create, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 1151 | .pre_destroy = mem_cgroup_pre_destroy, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1152 | .destroy = mem_cgroup_destroy, |
| 1153 | .populate = mem_cgroup_populate, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1154 | .attach = mem_cgroup_move_task, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1155 | .early_init = 0, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1156 | }; |