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