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