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