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 | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 24 | #include <linux/pagemap.h> |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 25 | #include <linux/smp.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 26 | #include <linux/page-flags.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 27 | #include <linux/backing-dev.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 28 | #include <linux/bit_spinlock.h> |
| 29 | #include <linux/rcupdate.h> |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 30 | #include <linux/mutex.h> |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 31 | #include <linux/slab.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 32 | #include <linux/swap.h> |
| 33 | #include <linux/spinlock.h> |
| 34 | #include <linux/fs.h> |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 35 | #include <linux/seq_file.h> |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 36 | #include <linux/vmalloc.h> |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 37 | #include <linux/mm_inline.h> |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 38 | #include <linux/page_cgroup.h> |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 39 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 40 | #include <asm/uaccess.h> |
| 41 | |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 42 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 43 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 44 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 45 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 46 | /* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */ |
| 47 | int do_swap_account __read_mostly; |
| 48 | static int really_do_swap_account __initdata = 1; /* for remember boot option*/ |
| 49 | #else |
| 50 | #define do_swap_account (0) |
| 51 | #endif |
| 52 | |
| 53 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 54 | /* |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 55 | * Statistics for memory cgroup. |
| 56 | */ |
| 57 | enum mem_cgroup_stat_index { |
| 58 | /* |
| 59 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. |
| 60 | */ |
| 61 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ |
| 62 | MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */ |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 63 | MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */ |
| 64 | MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 65 | |
| 66 | MEM_CGROUP_STAT_NSTATS, |
| 67 | }; |
| 68 | |
| 69 | struct mem_cgroup_stat_cpu { |
| 70 | s64 count[MEM_CGROUP_STAT_NSTATS]; |
| 71 | } ____cacheline_aligned_in_smp; |
| 72 | |
| 73 | struct mem_cgroup_stat { |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 74 | struct mem_cgroup_stat_cpu cpustat[0]; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /* |
| 78 | * For accounting under irq disable, no need for increment preempt count. |
| 79 | */ |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 80 | 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] | 81 | enum mem_cgroup_stat_index idx, int val) |
| 82 | { |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 83 | stat->count[idx] += val; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat, |
| 87 | enum mem_cgroup_stat_index idx) |
| 88 | { |
| 89 | int cpu; |
| 90 | s64 ret = 0; |
| 91 | for_each_possible_cpu(cpu) |
| 92 | ret += stat->cpustat[cpu].count[idx]; |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | /* |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 97 | * per-zone information in memory controller. |
| 98 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 99 | struct mem_cgroup_per_zone { |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 100 | /* |
| 101 | * spin_lock to protect the per cgroup LRU |
| 102 | */ |
| 103 | spinlock_t lru_lock; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 104 | struct list_head lists[NR_LRU_LISTS]; |
| 105 | unsigned long count[NR_LRU_LISTS]; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 106 | }; |
| 107 | /* Macro for accessing counter */ |
| 108 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) |
| 109 | |
| 110 | struct mem_cgroup_per_node { |
| 111 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; |
| 112 | }; |
| 113 | |
| 114 | struct mem_cgroup_lru_info { |
| 115 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; |
| 116 | }; |
| 117 | |
| 118 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 119 | * The memory controller data structure. The memory controller controls both |
| 120 | * page cache and RSS per cgroup. We would eventually like to provide |
| 121 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, |
| 122 | * to help the administrator determine what knobs to tune. |
| 123 | * |
| 124 | * 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] | 125 | * we hit the water mark. May be even add a low water mark, such that |
| 126 | * no reclaim occurs from a cgroup at it's low water mark, this is |
| 127 | * a feature that will be implemented much later in the future. |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 128 | */ |
| 129 | struct mem_cgroup { |
| 130 | struct cgroup_subsys_state css; |
| 131 | /* |
| 132 | * the counter to account for memory usage |
| 133 | */ |
| 134 | struct res_counter res; |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 135 | /* |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 136 | * the counter to account for mem+swap usage. |
| 137 | */ |
| 138 | struct res_counter memsw; |
| 139 | /* |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 140 | * Per cgroup active and inactive list, similar to the |
| 141 | * per zone LRU lists. |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 142 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 143 | struct mem_cgroup_lru_info info; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 144 | |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 145 | int prev_priority; /* for recording reclaim priority */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 146 | int obsolete; |
| 147 | atomic_t refcnt; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 148 | /* |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 149 | * statistics. This must be placed at the end of memcg. |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 150 | */ |
| 151 | struct mem_cgroup_stat stat; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 152 | }; |
| 153 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 154 | enum charge_type { |
| 155 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
| 156 | MEM_CGROUP_CHARGE_TYPE_MAPPED, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 157 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 158 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 159 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 160 | NR_CHARGE_TYPE, |
| 161 | }; |
| 162 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 163 | /* only for here (for easy reading.) */ |
| 164 | #define PCGF_CACHE (1UL << PCG_CACHE) |
| 165 | #define PCGF_USED (1UL << PCG_USED) |
| 166 | #define PCGF_ACTIVE (1UL << PCG_ACTIVE) |
| 167 | #define PCGF_LOCK (1UL << PCG_LOCK) |
| 168 | #define PCGF_FILE (1UL << PCG_FILE) |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 169 | static const unsigned long |
| 170 | pcg_default_flags[NR_CHARGE_TYPE] = { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 171 | PCGF_CACHE | PCGF_FILE | PCGF_USED | PCGF_LOCK, /* File Cache */ |
| 172 | PCGF_ACTIVE | PCGF_USED | PCGF_LOCK, /* Anon */ |
| 173 | PCGF_ACTIVE | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */ |
| 174 | 0, /* FORCE */ |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 177 | |
| 178 | /* for encoding cft->private value on file */ |
| 179 | #define _MEM (0) |
| 180 | #define _MEMSWAP (1) |
| 181 | #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) |
| 182 | #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff) |
| 183 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
| 184 | |
| 185 | static void mem_cgroup_get(struct mem_cgroup *mem); |
| 186 | static void mem_cgroup_put(struct mem_cgroup *mem); |
| 187 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 188 | /* |
| 189 | * Always modified under lru lock. Then, not necessary to preempt_disable() |
| 190 | */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 191 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, |
| 192 | struct page_cgroup *pc, |
| 193 | bool charge) |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 194 | { |
| 195 | int val = (charge)? 1 : -1; |
| 196 | struct mem_cgroup_stat *stat = &mem->stat; |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 197 | struct mem_cgroup_stat_cpu *cpustat; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 198 | |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 199 | VM_BUG_ON(!irqs_disabled()); |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 200 | |
| 201 | cpustat = &stat->cpustat[smp_processor_id()]; |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 202 | if (PageCgroupCache(pc)) |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 203 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 204 | else |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 205 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val); |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 206 | |
| 207 | if (charge) |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 208 | __mem_cgroup_stat_add_safe(cpustat, |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 209 | MEM_CGROUP_STAT_PGPGIN_COUNT, 1); |
| 210 | else |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 211 | __mem_cgroup_stat_add_safe(cpustat, |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 212 | MEM_CGROUP_STAT_PGPGOUT_COUNT, 1); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 213 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 214 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 215 | static struct mem_cgroup_per_zone * |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 216 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) |
| 217 | { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 218 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; |
| 219 | } |
| 220 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 221 | static struct mem_cgroup_per_zone * |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 222 | page_cgroup_zoneinfo(struct page_cgroup *pc) |
| 223 | { |
| 224 | struct mem_cgroup *mem = pc->mem_cgroup; |
| 225 | int nid = page_cgroup_nid(pc); |
| 226 | int zid = page_cgroup_zid(pc); |
| 227 | |
| 228 | return mem_cgroup_zoneinfo(mem, nid, zid); |
| 229 | } |
| 230 | |
| 231 | static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem, |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 232 | enum lru_list idx) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 233 | { |
| 234 | int nid, zid; |
| 235 | struct mem_cgroup_per_zone *mz; |
| 236 | u64 total = 0; |
| 237 | |
| 238 | for_each_online_node(nid) |
| 239 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 240 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 241 | total += MEM_CGROUP_ZSTAT(mz, idx); |
| 242 | } |
| 243 | return total; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 246 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 247 | { |
| 248 | return container_of(cgroup_subsys_state(cont, |
| 249 | mem_cgroup_subsys_id), struct mem_cgroup, |
| 250 | css); |
| 251 | } |
| 252 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 253 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 254 | { |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 255 | /* |
| 256 | * mm_update_next_owner() may clear mm->owner to NULL |
| 257 | * if it races with swapoff, page migration, etc. |
| 258 | * So this can be called with p == NULL. |
| 259 | */ |
| 260 | if (unlikely(!p)) |
| 261 | return NULL; |
| 262 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 263 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
| 264 | struct mem_cgroup, css); |
| 265 | } |
| 266 | |
KAMEZAWA Hiroyuki | 3eae90c | 2008-04-29 01:00:22 -0700 | [diff] [blame] | 267 | static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz, |
| 268 | struct page_cgroup *pc) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 269 | { |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 270 | int lru = LRU_BASE; |
| 271 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 272 | if (PageCgroupUnevictable(pc)) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 273 | lru = LRU_UNEVICTABLE; |
| 274 | else { |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 275 | if (PageCgroupActive(pc)) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 276 | lru += LRU_ACTIVE; |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 277 | if (PageCgroupFile(pc)) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 278 | lru += LRU_FILE; |
| 279 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 280 | |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 281 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 282 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 283 | mem_cgroup_charge_statistics(pc->mem_cgroup, pc, false); |
KAMEZAWA Hiroyuki | 508b7be | 2008-07-25 01:47:09 -0700 | [diff] [blame] | 284 | list_del(&pc->lru); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 285 | } |
| 286 | |
KAMEZAWA Hiroyuki | 3eae90c | 2008-04-29 01:00:22 -0700 | [diff] [blame] | 287 | static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz, |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 288 | struct page_cgroup *pc, bool hot) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 289 | { |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 290 | int lru = LRU_BASE; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 291 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 292 | if (PageCgroupUnevictable(pc)) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 293 | lru = LRU_UNEVICTABLE; |
| 294 | else { |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 295 | if (PageCgroupActive(pc)) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 296 | lru += LRU_ACTIVE; |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 297 | if (PageCgroupFile(pc)) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 298 | lru += LRU_FILE; |
| 299 | } |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 300 | |
| 301 | MEM_CGROUP_ZSTAT(mz, lru) += 1; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 302 | if (hot) |
| 303 | list_add(&pc->lru, &mz->lists[lru]); |
| 304 | else |
| 305 | list_add_tail(&pc->lru, &mz->lists[lru]); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 306 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 307 | mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 310 | 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] | 311 | { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 312 | struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc); |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 313 | int active = PageCgroupActive(pc); |
| 314 | int file = PageCgroupFile(pc); |
| 315 | int unevictable = PageCgroupUnevictable(pc); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 316 | enum lru_list from = unevictable ? LRU_UNEVICTABLE : |
| 317 | (LRU_FILE * !!file + !!active); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 318 | |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 319 | if (lru == from) |
| 320 | return; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 321 | |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 322 | MEM_CGROUP_ZSTAT(mz, from) -= 1; |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 323 | /* |
| 324 | * However this is done under mz->lru_lock, another flags, which |
| 325 | * are not related to LRU, will be modified from out-of-lock. |
| 326 | * We have to use atomic set/clear flags. |
| 327 | */ |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 328 | if (is_unevictable_lru(lru)) { |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 329 | ClearPageCgroupActive(pc); |
| 330 | SetPageCgroupUnevictable(pc); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 331 | } else { |
| 332 | if (is_active_lru(lru)) |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 333 | SetPageCgroupActive(pc); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 334 | else |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 335 | ClearPageCgroupActive(pc); |
| 336 | ClearPageCgroupUnevictable(pc); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 337 | } |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 338 | |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 339 | MEM_CGROUP_ZSTAT(mz, lru) += 1; |
| 340 | list_move(&pc->lru, &mz->lists[lru]); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 341 | } |
| 342 | |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 343 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
| 344 | { |
| 345 | int ret; |
| 346 | |
| 347 | task_lock(task); |
Hugh Dickins | bd845e3 | 2008-03-04 14:29:01 -0800 | [diff] [blame] | 348 | ret = task->mm && mm_match_cgroup(task->mm, mem); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 349 | task_unlock(task); |
| 350 | return ret; |
| 351 | } |
| 352 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 353 | /* |
| 354 | * This routine assumes that the appropriate zone's lru lock is already held |
| 355 | */ |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 356 | void mem_cgroup_move_lists(struct page *page, enum lru_list lru) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 357 | { |
Hugh Dickins | 427d541 | 2008-03-04 14:29:03 -0800 | [diff] [blame] | 358 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 359 | struct mem_cgroup_per_zone *mz; |
| 360 | unsigned long flags; |
| 361 | |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 362 | if (mem_cgroup_subsys.disabled) |
| 363 | return; |
| 364 | |
Hugh Dickins | 2680eed | 2008-03-04 14:29:13 -0800 | [diff] [blame] | 365 | /* |
| 366 | * We cannot lock_page_cgroup while holding zone's lru_lock, |
| 367 | * because other holders of lock_page_cgroup can be interrupted |
| 368 | * with an attempt to rotate_reclaimable_page. But we cannot |
| 369 | * safely get to page_cgroup without it, so just try_lock it: |
| 370 | * mem_cgroup_isolate_pages allows for page left on wrong list. |
| 371 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 372 | pc = lookup_page_cgroup(page); |
| 373 | if (!trylock_page_cgroup(pc)) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 374 | return; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 375 | if (pc && PageCgroupUsed(pc)) { |
Hugh Dickins | 2680eed | 2008-03-04 14:29:13 -0800 | [diff] [blame] | 376 | mz = page_cgroup_zoneinfo(pc); |
Hugh Dickins | 2680eed | 2008-03-04 14:29:13 -0800 | [diff] [blame] | 377 | spin_lock_irqsave(&mz->lru_lock, flags); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 378 | __mem_cgroup_move_lists(pc, lru); |
Hugh Dickins | 2680eed | 2008-03-04 14:29:13 -0800 | [diff] [blame] | 379 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
Hirokazu Takahashi | 9b3c0a0 | 2008-03-04 14:29:15 -0800 | [diff] [blame] | 380 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 381 | unlock_page_cgroup(pc); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 382 | } |
| 383 | |
KAMEZAWA Hiroyuki | 58ae83d | 2008-02-07 00:14:32 -0800 | [diff] [blame] | 384 | /* |
| 385 | * Calculate mapped_ratio under memory controller. This will be used in |
| 386 | * vmscan.c for deteremining we have to reclaim mapped pages. |
| 387 | */ |
| 388 | int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem) |
| 389 | { |
| 390 | long total, rss; |
| 391 | |
| 392 | /* |
| 393 | * usage is recorded in bytes. But, here, we assume the number of |
| 394 | * physical pages can be represented by "long" on any arch. |
| 395 | */ |
| 396 | total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L; |
| 397 | rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS); |
| 398 | return (int)((rss * 100L) / total); |
| 399 | } |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 400 | |
KAMEZAWA Hiroyuki | 5932f36 | 2008-02-07 00:14:33 -0800 | [diff] [blame] | 401 | /* |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 402 | * prev_priority control...this will be used in memory reclaim path. |
| 403 | */ |
| 404 | int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem) |
| 405 | { |
| 406 | return mem->prev_priority; |
| 407 | } |
| 408 | |
| 409 | void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority) |
| 410 | { |
| 411 | if (priority < mem->prev_priority) |
| 412 | mem->prev_priority = priority; |
| 413 | } |
| 414 | |
| 415 | void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority) |
| 416 | { |
| 417 | mem->prev_priority = priority; |
| 418 | } |
| 419 | |
KAMEZAWA Hiroyuki | cc38108 | 2008-02-07 00:14:35 -0800 | [diff] [blame] | 420 | /* |
| 421 | * Calculate # of pages to be scanned in this priority/zone. |
| 422 | * See also vmscan.c |
| 423 | * |
| 424 | * priority starts from "DEF_PRIORITY" and decremented in each loop. |
| 425 | * (see include/linux/mmzone.h) |
| 426 | */ |
| 427 | |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 428 | long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone, |
| 429 | int priority, enum lru_list lru) |
KAMEZAWA Hiroyuki | cc38108 | 2008-02-07 00:14:35 -0800 | [diff] [blame] | 430 | { |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 431 | long nr_pages; |
KAMEZAWA Hiroyuki | cc38108 | 2008-02-07 00:14:35 -0800 | [diff] [blame] | 432 | int nid = zone->zone_pgdat->node_id; |
| 433 | int zid = zone_idx(zone); |
| 434 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 435 | |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 436 | nr_pages = MEM_CGROUP_ZSTAT(mz, lru); |
KAMEZAWA Hiroyuki | cc38108 | 2008-02-07 00:14:35 -0800 | [diff] [blame] | 437 | |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 438 | return (nr_pages >> priority); |
KAMEZAWA Hiroyuki | cc38108 | 2008-02-07 00:14:35 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 441 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 442 | struct list_head *dst, |
| 443 | unsigned long *scanned, int order, |
| 444 | int mode, struct zone *z, |
| 445 | struct mem_cgroup *mem_cont, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 446 | int active, int file) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 447 | { |
| 448 | unsigned long nr_taken = 0; |
| 449 | struct page *page; |
| 450 | unsigned long scan; |
| 451 | LIST_HEAD(pc_list); |
| 452 | struct list_head *src; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 453 | struct page_cgroup *pc, *tmp; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 454 | int nid = z->zone_pgdat->node_id; |
| 455 | int zid = zone_idx(z); |
| 456 | struct mem_cgroup_per_zone *mz; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 457 | int lru = LRU_FILE * !!file + !!active; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 458 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 459 | BUG_ON(!mem_cont); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 460 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 461 | src = &mz->lists[lru]; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 462 | |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 463 | spin_lock(&mz->lru_lock); |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 464 | scan = 0; |
| 465 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 466 | if (scan >= nr_to_scan) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 467 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 468 | if (unlikely(!PageCgroupUsed(pc))) |
| 469 | continue; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 470 | page = pc->page; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 471 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 472 | if (unlikely(!PageLRU(page))) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 473 | continue; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 474 | |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 475 | /* |
| 476 | * TODO: play better with lumpy reclaim, grabbing anything. |
| 477 | */ |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 478 | if (PageUnevictable(page) || |
| 479 | (PageActive(page) && !active) || |
| 480 | (!PageActive(page) && active)) { |
| 481 | __mem_cgroup_move_lists(pc, page_lru(page)); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 482 | continue; |
| 483 | } |
| 484 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 485 | scan++; |
| 486 | list_move(&pc->lru, &pc_list); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 487 | |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 488 | if (__isolate_lru_page(page, mode, file) == 0) { |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 489 | list_move(&page->lru, dst); |
| 490 | nr_taken++; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | list_splice(&pc_list, src); |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 495 | spin_unlock(&mz->lru_lock); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 496 | |
| 497 | *scanned = scan; |
| 498 | return nr_taken; |
| 499 | } |
| 500 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 501 | /* |
| 502 | * Unlike exported interface, "oom" parameter is added. if oom==true, |
| 503 | * oom-killer can be invoked. |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 504 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 505 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 506 | gfp_t gfp_mask, struct mem_cgroup **memcg, |
| 507 | bool oom) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 508 | { |
| 509 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 510 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 511 | /* |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 512 | * We always charge the cgroup the mm_struct belongs to. |
| 513 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 514 | * thread group leader migrates. It's possible that mm is not |
| 515 | * set, if so charge the init_mm (happens for pagecache usage). |
| 516 | */ |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 517 | if (likely(!*memcg)) { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 518 | rcu_read_lock(); |
| 519 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 520 | if (unlikely(!mem)) { |
| 521 | rcu_read_unlock(); |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 522 | return 0; |
| 523 | } |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 524 | /* |
| 525 | * For every charge from the cgroup, increment reference count |
| 526 | */ |
| 527 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 528 | *memcg = mem; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 529 | rcu_read_unlock(); |
| 530 | } else { |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 531 | mem = *memcg; |
| 532 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 533 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 534 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 535 | while (1) { |
| 536 | int ret; |
| 537 | bool noswap = false; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 538 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 539 | ret = res_counter_charge(&mem->res, PAGE_SIZE); |
| 540 | if (likely(!ret)) { |
| 541 | if (!do_swap_account) |
| 542 | break; |
| 543 | ret = res_counter_charge(&mem->memsw, PAGE_SIZE); |
| 544 | if (likely(!ret)) |
| 545 | break; |
| 546 | /* mem+swap counter fails */ |
| 547 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 548 | noswap = true; |
| 549 | } |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 550 | if (!(gfp_mask & __GFP_WAIT)) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 551 | goto nomem; |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 552 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 553 | if (try_to_free_mem_cgroup_pages(mem, gfp_mask, noswap)) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 554 | continue; |
| 555 | |
| 556 | /* |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 557 | * try_to_free_mem_cgroup_pages() might not give us a full |
| 558 | * picture of reclaim. Some pages are reclaimed and might be |
| 559 | * moved to swap cache or just unmapped from the cgroup. |
| 560 | * Check the limit again to see if the reclaim reduced the |
| 561 | * current usage of the cgroup before giving up |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 562 | * |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 563 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 564 | if (!do_swap_account && |
| 565 | res_counter_check_under_limit(&mem->res)) |
| 566 | continue; |
| 567 | if (do_swap_account && |
| 568 | res_counter_check_under_limit(&mem->memsw)) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 569 | continue; |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 570 | |
| 571 | if (!nr_retries--) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 572 | if (oom) |
| 573 | mem_cgroup_out_of_memory(mem, gfp_mask); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 574 | goto nomem; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 575 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 576 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 577 | return 0; |
| 578 | nomem: |
| 579 | css_put(&mem->css); |
| 580 | return -ENOMEM; |
| 581 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 582 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 583 | /** |
| 584 | * mem_cgroup_try_charge - get charge of PAGE_SIZE. |
| 585 | * @mm: an mm_struct which is charged against. (when *memcg is NULL) |
| 586 | * @gfp_mask: gfp_mask for reclaim. |
| 587 | * @memcg: a pointer to memory cgroup which is charged against. |
| 588 | * |
| 589 | * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated |
| 590 | * memory cgroup from @mm is got and stored in *memcg. |
| 591 | * |
| 592 | * Returns 0 if success. -ENOMEM at failure. |
| 593 | * This call can invoke OOM-Killer. |
| 594 | */ |
| 595 | |
| 596 | int mem_cgroup_try_charge(struct mm_struct *mm, |
| 597 | gfp_t mask, struct mem_cgroup **memcg) |
| 598 | { |
| 599 | return __mem_cgroup_try_charge(mm, mask, memcg, true); |
| 600 | } |
| 601 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 602 | /* |
| 603 | * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be |
| 604 | * USED state. If already USED, uncharge and return. |
| 605 | */ |
| 606 | |
| 607 | static void __mem_cgroup_commit_charge(struct mem_cgroup *mem, |
| 608 | struct page_cgroup *pc, |
| 609 | enum charge_type ctype) |
| 610 | { |
| 611 | struct mem_cgroup_per_zone *mz; |
| 612 | unsigned long flags; |
| 613 | |
| 614 | /* try_charge() can return NULL to *memcg, taking care of it. */ |
| 615 | if (!mem) |
| 616 | return; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 617 | |
| 618 | lock_page_cgroup(pc); |
| 619 | if (unlikely(PageCgroupUsed(pc))) { |
| 620 | unlock_page_cgroup(pc); |
| 621 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 622 | if (do_swap_account) |
| 623 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 624 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 625 | return; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 626 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 627 | pc->mem_cgroup = mem; |
KAMEZAWA Hiroyuki | 508b7be | 2008-07-25 01:47:09 -0700 | [diff] [blame] | 628 | /* |
| 629 | * If a page is accounted as a page cache, insert to inactive list. |
| 630 | * If anon, insert to active list. |
| 631 | */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 632 | pc->flags = pcg_default_flags[ctype]; |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 633 | |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 634 | mz = page_cgroup_zoneinfo(pc); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 635 | |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 636 | spin_lock_irqsave(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 637 | __mem_cgroup_add_list(mz, pc, true); |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 638 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 639 | unlock_page_cgroup(pc); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 640 | } |
| 641 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 642 | /** |
| 643 | * mem_cgroup_move_account - move account of the page |
| 644 | * @pc: page_cgroup of the page. |
| 645 | * @from: mem_cgroup which the page is moved from. |
| 646 | * @to: mem_cgroup which the page is moved to. @from != @to. |
| 647 | * |
| 648 | * The caller must confirm following. |
| 649 | * 1. disable irq. |
| 650 | * 2. lru_lock of old mem_cgroup(@from) should be held. |
| 651 | * |
| 652 | * returns 0 at success, |
| 653 | * returns -EBUSY when lock is busy or "pc" is unstable. |
| 654 | * |
| 655 | * This function does "uncharge" from old cgroup but doesn't do "charge" to |
| 656 | * new cgroup. It should be done by a caller. |
| 657 | */ |
| 658 | |
| 659 | static int mem_cgroup_move_account(struct page_cgroup *pc, |
| 660 | struct mem_cgroup *from, struct mem_cgroup *to) |
| 661 | { |
| 662 | struct mem_cgroup_per_zone *from_mz, *to_mz; |
| 663 | int nid, zid; |
| 664 | int ret = -EBUSY; |
| 665 | |
| 666 | VM_BUG_ON(!irqs_disabled()); |
| 667 | VM_BUG_ON(from == to); |
| 668 | |
| 669 | nid = page_cgroup_nid(pc); |
| 670 | zid = page_cgroup_zid(pc); |
| 671 | from_mz = mem_cgroup_zoneinfo(from, nid, zid); |
| 672 | to_mz = mem_cgroup_zoneinfo(to, nid, zid); |
| 673 | |
| 674 | |
| 675 | if (!trylock_page_cgroup(pc)) |
| 676 | return ret; |
| 677 | |
| 678 | if (!PageCgroupUsed(pc)) |
| 679 | goto out; |
| 680 | |
| 681 | if (pc->mem_cgroup != from) |
| 682 | goto out; |
| 683 | |
| 684 | if (spin_trylock(&to_mz->lru_lock)) { |
| 685 | __mem_cgroup_remove_list(from_mz, pc); |
| 686 | css_put(&from->css); |
| 687 | res_counter_uncharge(&from->res, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 688 | if (do_swap_account) |
| 689 | res_counter_uncharge(&from->memsw, PAGE_SIZE); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 690 | pc->mem_cgroup = to; |
| 691 | css_get(&to->css); |
| 692 | __mem_cgroup_add_list(to_mz, pc, false); |
| 693 | ret = 0; |
| 694 | spin_unlock(&to_mz->lru_lock); |
| 695 | } |
| 696 | out: |
| 697 | unlock_page_cgroup(pc); |
| 698 | return ret; |
| 699 | } |
| 700 | |
| 701 | /* |
| 702 | * move charges to its parent. |
| 703 | */ |
| 704 | |
| 705 | static int mem_cgroup_move_parent(struct page_cgroup *pc, |
| 706 | struct mem_cgroup *child, |
| 707 | gfp_t gfp_mask) |
| 708 | { |
| 709 | struct cgroup *cg = child->css.cgroup; |
| 710 | struct cgroup *pcg = cg->parent; |
| 711 | struct mem_cgroup *parent; |
| 712 | struct mem_cgroup_per_zone *mz; |
| 713 | unsigned long flags; |
| 714 | int ret; |
| 715 | |
| 716 | /* Is ROOT ? */ |
| 717 | if (!pcg) |
| 718 | return -EINVAL; |
| 719 | |
| 720 | parent = mem_cgroup_from_cont(pcg); |
| 721 | |
| 722 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false); |
| 723 | if (ret) |
| 724 | return ret; |
| 725 | |
| 726 | mz = mem_cgroup_zoneinfo(child, |
| 727 | page_cgroup_nid(pc), page_cgroup_zid(pc)); |
| 728 | |
| 729 | spin_lock_irqsave(&mz->lru_lock, flags); |
| 730 | ret = mem_cgroup_move_account(pc, child, parent); |
| 731 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
| 732 | |
| 733 | /* drop extra refcnt */ |
| 734 | css_put(&parent->css); |
| 735 | /* uncharge if move fails */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 736 | if (ret) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 737 | res_counter_uncharge(&parent->res, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 738 | if (do_swap_account) |
| 739 | res_counter_uncharge(&parent->memsw, PAGE_SIZE); |
| 740 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 741 | |
| 742 | return ret; |
| 743 | } |
| 744 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 745 | /* |
| 746 | * Charge the memory controller for page usage. |
| 747 | * Return |
| 748 | * 0 if the charge was successful |
| 749 | * < 0 if the cgroup is over its limit |
| 750 | */ |
| 751 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, |
| 752 | gfp_t gfp_mask, enum charge_type ctype, |
| 753 | struct mem_cgroup *memcg) |
| 754 | { |
| 755 | struct mem_cgroup *mem; |
| 756 | struct page_cgroup *pc; |
| 757 | int ret; |
| 758 | |
| 759 | pc = lookup_page_cgroup(page); |
| 760 | /* can happen at boot */ |
| 761 | if (unlikely(!pc)) |
| 762 | return 0; |
| 763 | prefetchw(pc); |
| 764 | |
| 765 | mem = memcg; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 766 | ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 767 | if (ret) |
| 768 | return ret; |
| 769 | |
| 770 | __mem_cgroup_commit_charge(mem, pc, ctype); |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | int mem_cgroup_newpage_charge(struct page *page, |
| 775 | struct mm_struct *mm, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 776 | { |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 777 | if (mem_cgroup_subsys.disabled) |
| 778 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 779 | if (PageCompound(page)) |
| 780 | return 0; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 781 | /* |
| 782 | * If already mapped, we don't have to account. |
| 783 | * If page cache, page->mapping has address_space. |
| 784 | * But page->mapping may have out-of-use anon_vma pointer, |
| 785 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping |
| 786 | * is NULL. |
| 787 | */ |
| 788 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) |
| 789 | return 0; |
| 790 | if (unlikely(!mm)) |
| 791 | mm = &init_mm; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 792 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 793 | MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL); |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 794 | } |
| 795 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 796 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 797 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 798 | { |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 799 | if (mem_cgroup_subsys.disabled) |
| 800 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 801 | if (PageCompound(page)) |
| 802 | return 0; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 803 | /* |
| 804 | * Corner case handling. This is called from add_to_page_cache() |
| 805 | * in usual. But some FS (shmem) precharges this page before calling it |
| 806 | * and call add_to_page_cache() with GFP_NOWAIT. |
| 807 | * |
| 808 | * For GFP_NOWAIT case, the page may be pre-charged before calling |
| 809 | * add_to_page_cache(). (See shmem.c) check it here and avoid to call |
| 810 | * charge twice. (It works but has to pay a bit larger cost.) |
| 811 | */ |
| 812 | if (!(gfp_mask & __GFP_WAIT)) { |
| 813 | struct page_cgroup *pc; |
| 814 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 815 | |
| 816 | pc = lookup_page_cgroup(page); |
| 817 | if (!pc) |
| 818 | return 0; |
| 819 | lock_page_cgroup(pc); |
| 820 | if (PageCgroupUsed(pc)) { |
| 821 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 822 | return 0; |
| 823 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 824 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 825 | } |
| 826 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 827 | if (unlikely(!mm)) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 828 | mm = &init_mm; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 829 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 830 | if (page_is_file_cache(page)) |
| 831 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 832 | MEM_CGROUP_CHARGE_TYPE_CACHE, NULL); |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 833 | else |
| 834 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
| 835 | MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 836 | } |
| 837 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 838 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
| 839 | struct page *page, |
| 840 | gfp_t mask, struct mem_cgroup **ptr) |
| 841 | { |
| 842 | struct mem_cgroup *mem; |
| 843 | swp_entry_t ent; |
| 844 | |
| 845 | if (mem_cgroup_subsys.disabled) |
| 846 | return 0; |
| 847 | |
| 848 | if (!do_swap_account) |
| 849 | goto charge_cur_mm; |
| 850 | |
| 851 | /* |
| 852 | * A racing thread's fault, or swapoff, may have already updated |
| 853 | * the pte, and even removed page from swap cache: return success |
| 854 | * to go on to do_swap_page()'s pte_same() test, which should fail. |
| 855 | */ |
| 856 | if (!PageSwapCache(page)) |
| 857 | return 0; |
| 858 | |
| 859 | ent.val = page_private(page); |
| 860 | |
| 861 | mem = lookup_swap_cgroup(ent); |
| 862 | if (!mem || mem->obsolete) |
| 863 | goto charge_cur_mm; |
| 864 | *ptr = mem; |
| 865 | return __mem_cgroup_try_charge(NULL, mask, ptr, true); |
| 866 | charge_cur_mm: |
| 867 | if (unlikely(!mm)) |
| 868 | mm = &init_mm; |
| 869 | return __mem_cgroup_try_charge(mm, mask, ptr, true); |
| 870 | } |
| 871 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 872 | #ifdef CONFIG_SWAP |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 873 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 874 | int mem_cgroup_cache_charge_swapin(struct page *page, |
| 875 | struct mm_struct *mm, gfp_t mask, bool locked) |
| 876 | { |
| 877 | int ret = 0; |
| 878 | |
| 879 | if (mem_cgroup_subsys.disabled) |
| 880 | return 0; |
| 881 | if (unlikely(!mm)) |
| 882 | mm = &init_mm; |
| 883 | if (!locked) |
| 884 | lock_page(page); |
| 885 | /* |
| 886 | * If not locked, the page can be dropped from SwapCache until |
| 887 | * we reach here. |
| 888 | */ |
| 889 | if (PageSwapCache(page)) { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 890 | struct mem_cgroup *mem = NULL; |
| 891 | swp_entry_t ent; |
| 892 | |
| 893 | ent.val = page_private(page); |
| 894 | if (do_swap_account) { |
| 895 | mem = lookup_swap_cgroup(ent); |
| 896 | if (mem && mem->obsolete) |
| 897 | mem = NULL; |
| 898 | if (mem) |
| 899 | mm = NULL; |
| 900 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 901 | ret = mem_cgroup_charge_common(page, mm, mask, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 902 | MEM_CGROUP_CHARGE_TYPE_SHMEM, mem); |
| 903 | |
| 904 | if (!ret && do_swap_account) { |
| 905 | /* avoid double counting */ |
| 906 | mem = swap_cgroup_record(ent, NULL); |
| 907 | if (mem) { |
| 908 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); |
| 909 | mem_cgroup_put(mem); |
| 910 | } |
| 911 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 912 | } |
| 913 | if (!locked) |
| 914 | unlock_page(page); |
| 915 | |
| 916 | return ret; |
| 917 | } |
| 918 | #endif |
| 919 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 920 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
| 921 | { |
| 922 | struct page_cgroup *pc; |
| 923 | |
| 924 | if (mem_cgroup_subsys.disabled) |
| 925 | return; |
| 926 | if (!ptr) |
| 927 | return; |
| 928 | pc = lookup_page_cgroup(page); |
| 929 | __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 930 | /* |
| 931 | * Now swap is on-memory. This means this page may be |
| 932 | * counted both as mem and swap....double count. |
| 933 | * Fix it by uncharging from memsw. This SwapCache is stable |
| 934 | * because we're still under lock_page(). |
| 935 | */ |
| 936 | if (do_swap_account) { |
| 937 | swp_entry_t ent = {.val = page_private(page)}; |
| 938 | struct mem_cgroup *memcg; |
| 939 | memcg = swap_cgroup_record(ent, NULL); |
| 940 | if (memcg) { |
| 941 | /* If memcg is obsolete, memcg can be != ptr */ |
| 942 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
| 943 | mem_cgroup_put(memcg); |
| 944 | } |
| 945 | |
| 946 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem) |
| 950 | { |
| 951 | if (mem_cgroup_subsys.disabled) |
| 952 | return; |
| 953 | if (!mem) |
| 954 | return; |
| 955 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 956 | if (do_swap_account) |
| 957 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 958 | css_put(&mem->css); |
| 959 | } |
| 960 | |
| 961 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 962 | /* |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 963 | * uncharge if !page_mapped(page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 964 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 965 | static struct mem_cgroup * |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 966 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 967 | { |
Hugh Dickins | 8289546 | 2008-03-04 14:29:08 -0800 | [diff] [blame] | 968 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 969 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 970 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 971 | unsigned long flags; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 972 | |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 973 | if (mem_cgroup_subsys.disabled) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 974 | return NULL; |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 975 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 976 | if (PageSwapCache(page)) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 977 | return NULL; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 978 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 979 | /* |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 980 | * Check if our page_cgroup is valid |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 981 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 982 | pc = lookup_page_cgroup(page); |
| 983 | if (unlikely(!pc || !PageCgroupUsed(pc))) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 984 | return NULL; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 985 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 986 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 987 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 988 | mem = pc->mem_cgroup; |
| 989 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 990 | if (!PageCgroupUsed(pc)) |
| 991 | goto unlock_out; |
| 992 | |
| 993 | switch (ctype) { |
| 994 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
| 995 | if (page_mapped(page)) |
| 996 | goto unlock_out; |
| 997 | break; |
| 998 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: |
| 999 | if (!PageAnon(page)) { /* Shared memory */ |
| 1000 | if (page->mapping && !page_is_file_cache(page)) |
| 1001 | goto unlock_out; |
| 1002 | } else if (page_mapped(page)) /* Anon */ |
| 1003 | goto unlock_out; |
| 1004 | break; |
| 1005 | default: |
| 1006 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1007 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1008 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1009 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 1010 | if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)) |
| 1011 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); |
| 1012 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1013 | ClearPageCgroupUsed(pc); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 1014 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1015 | mz = page_cgroup_zoneinfo(pc); |
| 1016 | spin_lock_irqsave(&mz->lru_lock, flags); |
| 1017 | __mem_cgroup_remove_list(mz, pc); |
| 1018 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1019 | unlock_page_cgroup(pc); |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 1020 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1021 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1022 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1023 | return mem; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1024 | |
| 1025 | unlock_out: |
| 1026 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1027 | return NULL; |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1030 | void mem_cgroup_uncharge_page(struct page *page) |
| 1031 | { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1032 | /* early check. */ |
| 1033 | if (page_mapped(page)) |
| 1034 | return; |
| 1035 | if (page->mapping && !PageAnon(page)) |
| 1036 | return; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1037 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 1038 | } |
| 1039 | |
| 1040 | void mem_cgroup_uncharge_cache_page(struct page *page) |
| 1041 | { |
| 1042 | VM_BUG_ON(page_mapped(page)); |
KAMEZAWA Hiroyuki | b7abea9 | 2008-10-18 20:28:09 -0700 | [diff] [blame] | 1043 | VM_BUG_ON(page->mapping); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1044 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 1045 | } |
| 1046 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1047 | /* |
| 1048 | * called from __delete_from_swap_cache() and drop "page" account. |
| 1049 | * memcg information is recorded to swap_cgroup of "ent" |
| 1050 | */ |
| 1051 | void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent) |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1052 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1053 | struct mem_cgroup *memcg; |
| 1054 | |
| 1055 | memcg = __mem_cgroup_uncharge_common(page, |
| 1056 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT); |
| 1057 | /* record memcg information */ |
| 1058 | if (do_swap_account && memcg) { |
| 1059 | swap_cgroup_record(ent, memcg); |
| 1060 | mem_cgroup_get(memcg); |
| 1061 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1064 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 1065 | /* |
| 1066 | * called from swap_entry_free(). remove record in swap_cgroup and |
| 1067 | * uncharge "memsw" account. |
| 1068 | */ |
| 1069 | void mem_cgroup_uncharge_swap(swp_entry_t ent) |
| 1070 | { |
| 1071 | struct mem_cgroup *memcg; |
| 1072 | |
| 1073 | if (!do_swap_account) |
| 1074 | return; |
| 1075 | |
| 1076 | memcg = swap_cgroup_record(ent, NULL); |
| 1077 | if (memcg) { |
| 1078 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
| 1079 | mem_cgroup_put(memcg); |
| 1080 | } |
| 1081 | } |
| 1082 | #endif |
| 1083 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 1084 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1085 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
| 1086 | * page belongs to. |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 1087 | */ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1088 | int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr) |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 1089 | { |
| 1090 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1091 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1092 | int ret = 0; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 1093 | |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 1094 | if (mem_cgroup_subsys.disabled) |
| 1095 | return 0; |
| 1096 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1097 | pc = lookup_page_cgroup(page); |
| 1098 | lock_page_cgroup(pc); |
| 1099 | if (PageCgroupUsed(pc)) { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1100 | mem = pc->mem_cgroup; |
| 1101 | css_get(&mem->css); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 1102 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1103 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1104 | |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1105 | if (mem) { |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1106 | ret = mem_cgroup_try_charge(NULL, GFP_HIGHUSER_MOVABLE, &mem); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1107 | css_put(&mem->css); |
| 1108 | } |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1109 | *ptr = mem; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1110 | return ret; |
| 1111 | } |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 1112 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1113 | /* remove redundant charge if migration failed*/ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1114 | void mem_cgroup_end_migration(struct mem_cgroup *mem, |
| 1115 | struct page *oldpage, struct page *newpage) |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1116 | { |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1117 | struct page *target, *unused; |
| 1118 | struct page_cgroup *pc; |
| 1119 | enum charge_type ctype; |
| 1120 | |
| 1121 | if (!mem) |
| 1122 | return; |
| 1123 | |
| 1124 | /* at migration success, oldpage->mapping is NULL. */ |
| 1125 | if (oldpage->mapping) { |
| 1126 | target = oldpage; |
| 1127 | unused = NULL; |
| 1128 | } else { |
| 1129 | target = newpage; |
| 1130 | unused = oldpage; |
| 1131 | } |
| 1132 | |
| 1133 | if (PageAnon(target)) |
| 1134 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; |
| 1135 | else if (page_is_file_cache(target)) |
| 1136 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; |
| 1137 | else |
| 1138 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; |
| 1139 | |
| 1140 | /* unused page is not on radix-tree now. */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1141 | if (unused) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1142 | __mem_cgroup_uncharge_common(unused, ctype); |
| 1143 | |
| 1144 | pc = lookup_page_cgroup(target); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1145 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1146 | * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup. |
| 1147 | * So, double-counting is effectively avoided. |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1148 | */ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1149 | __mem_cgroup_commit_charge(mem, pc, ctype); |
| 1150 | |
| 1151 | /* |
| 1152 | * Both of oldpage and newpage are still under lock_page(). |
| 1153 | * Then, we don't have to care about race in radix-tree. |
| 1154 | * But we have to be careful that this page is unmapped or not. |
| 1155 | * |
| 1156 | * There is a case for !page_mapped(). At the start of |
| 1157 | * migration, oldpage was mapped. But now, it's zapped. |
| 1158 | * But we know *target* page is not freed/reused under us. |
| 1159 | * mem_cgroup_uncharge_page() does all necessary checks. |
| 1160 | */ |
| 1161 | if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED) |
| 1162 | mem_cgroup_uncharge_page(target); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 1163 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1164 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1165 | /* |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1166 | * A call to try to shrink memory usage under specified resource controller. |
| 1167 | * This is typically used for page reclaiming for shmem for reducing side |
| 1168 | * effect of page allocation from shmem, which is used by some mem_cgroup. |
| 1169 | */ |
| 1170 | int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask) |
| 1171 | { |
| 1172 | struct mem_cgroup *mem; |
| 1173 | int progress = 0; |
| 1174 | int retry = MEM_CGROUP_RECLAIM_RETRIES; |
| 1175 | |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 1176 | if (mem_cgroup_subsys.disabled) |
| 1177 | return 0; |
Hugh Dickins | 9623e07 | 2008-08-12 15:08:41 -0700 | [diff] [blame] | 1178 | if (!mm) |
| 1179 | return 0; |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 1180 | |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1181 | rcu_read_lock(); |
| 1182 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 1183 | if (unlikely(!mem)) { |
| 1184 | rcu_read_unlock(); |
| 1185 | return 0; |
| 1186 | } |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1187 | css_get(&mem->css); |
| 1188 | rcu_read_unlock(); |
| 1189 | |
| 1190 | do { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1191 | progress = try_to_free_mem_cgroup_pages(mem, gfp_mask, true); |
Daisuke Nishimura | a10cebf | 2008-09-22 13:57:52 -0700 | [diff] [blame] | 1192 | progress += res_counter_check_under_limit(&mem->res); |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1193 | } while (!progress && --retry); |
| 1194 | |
| 1195 | css_put(&mem->css); |
| 1196 | if (!retry) |
| 1197 | return -ENOMEM; |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1201 | static DEFINE_MUTEX(set_limit_mutex); |
| 1202 | |
KOSAKI Motohiro | d38d2a7 | 2009-01-06 14:39:44 -0800 | [diff] [blame] | 1203 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1204 | unsigned long long val) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1205 | { |
| 1206 | |
| 1207 | int retry_count = MEM_CGROUP_RECLAIM_RETRIES; |
| 1208 | int progress; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1209 | u64 memswlimit; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1210 | int ret = 0; |
| 1211 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1212 | while (retry_count) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1213 | if (signal_pending(current)) { |
| 1214 | ret = -EINTR; |
| 1215 | break; |
| 1216 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1217 | /* |
| 1218 | * Rather than hide all in some function, I do this in |
| 1219 | * open coded manner. You see what this really does. |
| 1220 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 1221 | */ |
| 1222 | mutex_lock(&set_limit_mutex); |
| 1223 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 1224 | if (memswlimit < val) { |
| 1225 | ret = -EINVAL; |
| 1226 | mutex_unlock(&set_limit_mutex); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1227 | break; |
| 1228 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1229 | ret = res_counter_set_limit(&memcg->res, val); |
| 1230 | mutex_unlock(&set_limit_mutex); |
| 1231 | |
| 1232 | if (!ret) |
| 1233 | break; |
| 1234 | |
KAMEZAWA Hiroyuki | bced052 | 2009-01-07 18:07:49 -0800 | [diff] [blame] | 1235 | progress = try_to_free_mem_cgroup_pages(memcg, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1236 | GFP_HIGHUSER_MOVABLE, false); |
| 1237 | if (!progress) retry_count--; |
| 1238 | } |
| 1239 | return ret; |
| 1240 | } |
| 1241 | |
| 1242 | int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
| 1243 | unsigned long long val) |
| 1244 | { |
| 1245 | int retry_count = MEM_CGROUP_RECLAIM_RETRIES; |
| 1246 | u64 memlimit, oldusage, curusage; |
| 1247 | int ret; |
| 1248 | |
| 1249 | if (!do_swap_account) |
| 1250 | return -EINVAL; |
| 1251 | |
| 1252 | while (retry_count) { |
| 1253 | if (signal_pending(current)) { |
| 1254 | ret = -EINTR; |
| 1255 | break; |
| 1256 | } |
| 1257 | /* |
| 1258 | * Rather than hide all in some function, I do this in |
| 1259 | * open coded manner. You see what this really does. |
| 1260 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 1261 | */ |
| 1262 | mutex_lock(&set_limit_mutex); |
| 1263 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 1264 | if (memlimit > val) { |
| 1265 | ret = -EINVAL; |
| 1266 | mutex_unlock(&set_limit_mutex); |
| 1267 | break; |
| 1268 | } |
| 1269 | ret = res_counter_set_limit(&memcg->memsw, val); |
| 1270 | mutex_unlock(&set_limit_mutex); |
| 1271 | |
| 1272 | if (!ret) |
| 1273 | break; |
| 1274 | |
| 1275 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
| 1276 | try_to_free_mem_cgroup_pages(memcg, GFP_HIGHUSER_MOVABLE, true); |
| 1277 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
| 1278 | if (curusage >= oldusage) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1279 | retry_count--; |
| 1280 | } |
| 1281 | return ret; |
| 1282 | } |
| 1283 | |
| 1284 | |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1285 | /* |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1286 | * This routine traverse page_cgroup in given list and drop them all. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1287 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
| 1288 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1289 | static int mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 1290 | struct mem_cgroup_per_zone *mz, |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1291 | enum lru_list lru) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1292 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1293 | struct page_cgroup *pc, *busy; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1294 | unsigned long flags; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1295 | unsigned long loop; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 1296 | struct list_head *list; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1297 | int ret = 0; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 1298 | |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1299 | list = &mz->lists[lru]; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1300 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1301 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
| 1302 | /* give some margin against EBUSY etc...*/ |
| 1303 | loop += 256; |
| 1304 | busy = NULL; |
| 1305 | while (loop--) { |
| 1306 | ret = 0; |
| 1307 | spin_lock_irqsave(&mz->lru_lock, flags); |
| 1308 | if (list_empty(list)) { |
| 1309 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1310 | break; |
| 1311 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1312 | pc = list_entry(list->prev, struct page_cgroup, lru); |
| 1313 | if (busy == pc) { |
| 1314 | list_move(&pc->lru, list); |
| 1315 | busy = 0; |
| 1316 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
| 1317 | continue; |
| 1318 | } |
| 1319 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
| 1320 | |
| 1321 | ret = mem_cgroup_move_parent(pc, mem, GFP_HIGHUSER_MOVABLE); |
| 1322 | if (ret == -ENOMEM) |
| 1323 | break; |
| 1324 | |
| 1325 | if (ret == -EBUSY || ret == -EINVAL) { |
| 1326 | /* found lock contention or "pc" is obsolete. */ |
| 1327 | busy = pc; |
| 1328 | cond_resched(); |
| 1329 | } else |
| 1330 | busy = NULL; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1331 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1332 | if (!ret && !list_empty(list)) |
| 1333 | return -EBUSY; |
| 1334 | return ret; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | /* |
| 1338 | * make mem_cgroup's charge to be 0 if there is no task. |
| 1339 | * This enables deleting this mem_cgroup. |
| 1340 | */ |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1341 | static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1342 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1343 | int ret; |
| 1344 | int node, zid, shrink; |
| 1345 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1346 | struct cgroup *cgrp = mem->css.cgroup; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 1347 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1348 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1349 | |
| 1350 | shrink = 0; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1351 | /* should free all ? */ |
| 1352 | if (free_all) |
| 1353 | goto try_to_free; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1354 | move_account: |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1355 | while (mem->res.usage > 0) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1356 | ret = -EBUSY; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1357 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1358 | goto out; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1359 | ret = -EINTR; |
| 1360 | if (signal_pending(current)) |
| 1361 | goto out; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1362 | /* This is for making all *used* pages to be on LRU. */ |
| 1363 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1364 | ret = 0; |
| 1365 | for_each_node_state(node, N_POSSIBLE) { |
| 1366 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1367 | struct mem_cgroup_per_zone *mz; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1368 | enum lru_list l; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1369 | mz = mem_cgroup_zoneinfo(mem, node, zid); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1370 | for_each_lru(l) { |
| 1371 | ret = mem_cgroup_force_empty_list(mem, |
| 1372 | mz, l); |
| 1373 | if (ret) |
| 1374 | break; |
| 1375 | } |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1376 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1377 | if (ret) |
| 1378 | break; |
| 1379 | } |
| 1380 | /* it seems parent cgroup doesn't have enough mem */ |
| 1381 | if (ret == -ENOMEM) |
| 1382 | goto try_to_free; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1383 | cond_resched(); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1384 | } |
| 1385 | ret = 0; |
| 1386 | out: |
| 1387 | css_put(&mem->css); |
| 1388 | return ret; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1389 | |
| 1390 | try_to_free: |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1391 | /* returns EBUSY if there is a task or if we come here twice. */ |
| 1392 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1393 | ret = -EBUSY; |
| 1394 | goto out; |
| 1395 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1396 | /* we call try-to-free pages for make this cgroup empty */ |
| 1397 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1398 | /* try to free all pages in this cgroup */ |
| 1399 | shrink = 1; |
| 1400 | while (nr_retries && mem->res.usage > 0) { |
| 1401 | int progress; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1402 | |
| 1403 | if (signal_pending(current)) { |
| 1404 | ret = -EINTR; |
| 1405 | goto out; |
| 1406 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1407 | progress = try_to_free_mem_cgroup_pages(mem, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1408 | GFP_HIGHUSER_MOVABLE, false); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1409 | if (!progress) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1410 | nr_retries--; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1411 | /* maybe some writeback is necessary */ |
| 1412 | congestion_wait(WRITE, HZ/10); |
| 1413 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1414 | |
| 1415 | } |
| 1416 | /* try move_account...there may be some *locked* pages. */ |
| 1417 | if (mem->res.usage) |
| 1418 | goto move_account; |
| 1419 | ret = 0; |
| 1420 | goto out; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1421 | } |
| 1422 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1423 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
| 1424 | { |
| 1425 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); |
| 1426 | } |
| 1427 | |
| 1428 | |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 1429 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1430 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1431 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 1432 | u64 val = 0; |
| 1433 | int type, name; |
| 1434 | |
| 1435 | type = MEMFILE_TYPE(cft->private); |
| 1436 | name = MEMFILE_ATTR(cft->private); |
| 1437 | switch (type) { |
| 1438 | case _MEM: |
| 1439 | val = res_counter_read_u64(&mem->res, name); |
| 1440 | break; |
| 1441 | case _MEMSWAP: |
| 1442 | if (do_swap_account) |
| 1443 | val = res_counter_read_u64(&mem->memsw, name); |
| 1444 | break; |
| 1445 | default: |
| 1446 | BUG(); |
| 1447 | break; |
| 1448 | } |
| 1449 | return val; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1450 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1451 | /* |
| 1452 | * The user of this function is... |
| 1453 | * RES_LIMIT. |
| 1454 | */ |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 1455 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 1456 | const char *buffer) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1457 | { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1458 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1459 | int type, name; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1460 | unsigned long long val; |
| 1461 | int ret; |
| 1462 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1463 | type = MEMFILE_TYPE(cft->private); |
| 1464 | name = MEMFILE_ATTR(cft->private); |
| 1465 | switch (name) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1466 | case RES_LIMIT: |
| 1467 | /* This function does all necessary parse...reuse it */ |
| 1468 | ret = res_counter_memparse_write_strategy(buffer, &val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1469 | if (ret) |
| 1470 | break; |
| 1471 | if (type == _MEM) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1472 | ret = mem_cgroup_resize_limit(memcg, val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1473 | else |
| 1474 | ret = mem_cgroup_resize_memsw_limit(memcg, val); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1475 | break; |
| 1476 | default: |
| 1477 | ret = -EINVAL; /* should be BUG() ? */ |
| 1478 | break; |
| 1479 | } |
| 1480 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1481 | } |
| 1482 | |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 1483 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 1484 | { |
| 1485 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1486 | int type, name; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 1487 | |
| 1488 | mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1489 | type = MEMFILE_TYPE(event); |
| 1490 | name = MEMFILE_ATTR(event); |
| 1491 | switch (name) { |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 1492 | case RES_MAX_USAGE: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1493 | if (type == _MEM) |
| 1494 | res_counter_reset_max(&mem->res); |
| 1495 | else |
| 1496 | res_counter_reset_max(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 1497 | break; |
| 1498 | case RES_FAILCNT: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1499 | if (type == _MEM) |
| 1500 | res_counter_reset_failcnt(&mem->res); |
| 1501 | else |
| 1502 | res_counter_reset_failcnt(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 1503 | break; |
| 1504 | } |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 1505 | return 0; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1508 | static const struct mem_cgroup_stat_desc { |
| 1509 | const char *msg; |
| 1510 | u64 unit; |
| 1511 | } mem_cgroup_stat_desc[] = { |
| 1512 | [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, }, |
| 1513 | [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, }, |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 1514 | [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, }, |
| 1515 | [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, }, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1516 | }; |
| 1517 | |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 1518 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
| 1519 | struct cgroup_map_cb *cb) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1520 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1521 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
| 1522 | struct mem_cgroup_stat *stat = &mem_cont->stat; |
| 1523 | int i; |
| 1524 | |
| 1525 | for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) { |
| 1526 | s64 val; |
| 1527 | |
| 1528 | val = mem_cgroup_read_stat(stat, i); |
| 1529 | val *= mem_cgroup_stat_desc[i].unit; |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 1530 | cb->fill(cb, mem_cgroup_stat_desc[i].msg, val); |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1531 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1532 | /* showing # of active pages */ |
| 1533 | { |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1534 | unsigned long active_anon, inactive_anon; |
| 1535 | unsigned long active_file, inactive_file; |
Lee Schermerhorn | 7b85412 | 2008-10-18 20:26:40 -0700 | [diff] [blame] | 1536 | unsigned long unevictable; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1537 | |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1538 | inactive_anon = mem_cgroup_get_all_zonestat(mem_cont, |
| 1539 | LRU_INACTIVE_ANON); |
| 1540 | active_anon = mem_cgroup_get_all_zonestat(mem_cont, |
| 1541 | LRU_ACTIVE_ANON); |
| 1542 | inactive_file = mem_cgroup_get_all_zonestat(mem_cont, |
| 1543 | LRU_INACTIVE_FILE); |
| 1544 | active_file = mem_cgroup_get_all_zonestat(mem_cont, |
| 1545 | LRU_ACTIVE_FILE); |
Lee Schermerhorn | 7b85412 | 2008-10-18 20:26:40 -0700 | [diff] [blame] | 1546 | unevictable = mem_cgroup_get_all_zonestat(mem_cont, |
| 1547 | LRU_UNEVICTABLE); |
| 1548 | |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1549 | cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE); |
| 1550 | cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE); |
| 1551 | cb->fill(cb, "active_file", (active_file) * PAGE_SIZE); |
| 1552 | cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE); |
Lee Schermerhorn | 7b85412 | 2008-10-18 20:26:40 -0700 | [diff] [blame] | 1553 | cb->fill(cb, "unevictable", unevictable * PAGE_SIZE); |
| 1554 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1555 | } |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1556 | return 0; |
| 1557 | } |
| 1558 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1559 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1560 | static struct cftype mem_cgroup_files[] = { |
| 1561 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 1562 | .name = "usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1563 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 1564 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1565 | }, |
| 1566 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 1567 | .name = "max_usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1568 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 1569 | .trigger = mem_cgroup_reset, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 1570 | .read_u64 = mem_cgroup_read, |
| 1571 | }, |
| 1572 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 1573 | .name = "limit_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1574 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 1575 | .write_string = mem_cgroup_write, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 1576 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1577 | }, |
| 1578 | { |
| 1579 | .name = "failcnt", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1580 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 1581 | .trigger = mem_cgroup_reset, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 1582 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1583 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1584 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1585 | .name = "stat", |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 1586 | .read_map = mem_control_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1587 | }, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1588 | { |
| 1589 | .name = "force_empty", |
| 1590 | .trigger = mem_cgroup_force_empty_write, |
| 1591 | }, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1592 | }; |
| 1593 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1594 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 1595 | static struct cftype memsw_cgroup_files[] = { |
| 1596 | { |
| 1597 | .name = "memsw.usage_in_bytes", |
| 1598 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), |
| 1599 | .read_u64 = mem_cgroup_read, |
| 1600 | }, |
| 1601 | { |
| 1602 | .name = "memsw.max_usage_in_bytes", |
| 1603 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), |
| 1604 | .trigger = mem_cgroup_reset, |
| 1605 | .read_u64 = mem_cgroup_read, |
| 1606 | }, |
| 1607 | { |
| 1608 | .name = "memsw.limit_in_bytes", |
| 1609 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), |
| 1610 | .write_string = mem_cgroup_write, |
| 1611 | .read_u64 = mem_cgroup_read, |
| 1612 | }, |
| 1613 | { |
| 1614 | .name = "memsw.failcnt", |
| 1615 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), |
| 1616 | .trigger = mem_cgroup_reset, |
| 1617 | .read_u64 = mem_cgroup_read, |
| 1618 | }, |
| 1619 | }; |
| 1620 | |
| 1621 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 1622 | { |
| 1623 | if (!do_swap_account) |
| 1624 | return 0; |
| 1625 | return cgroup_add_files(cont, ss, memsw_cgroup_files, |
| 1626 | ARRAY_SIZE(memsw_cgroup_files)); |
| 1627 | }; |
| 1628 | #else |
| 1629 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 1630 | { |
| 1631 | return 0; |
| 1632 | } |
| 1633 | #endif |
| 1634 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1635 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 1636 | { |
| 1637 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1638 | struct mem_cgroup_per_zone *mz; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1639 | enum lru_list l; |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 1640 | int zone, tmp = node; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1641 | /* |
| 1642 | * This routine is called against possible nodes. |
| 1643 | * But it's BUG to call kmalloc() against offline node. |
| 1644 | * |
| 1645 | * TODO: this routine can waste much memory for nodes which will |
| 1646 | * never be onlined. It's better to use memory hotplug callback |
| 1647 | * function. |
| 1648 | */ |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 1649 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 1650 | tmp = -1; |
| 1651 | pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1652 | if (!pn) |
| 1653 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1654 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1655 | mem->info.nodeinfo[node] = pn; |
| 1656 | memset(pn, 0, sizeof(*pn)); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1657 | |
| 1658 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 1659 | mz = &pn->zoneinfo[zone]; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 1660 | spin_lock_init(&mz->lru_lock); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1661 | for_each_lru(l) |
| 1662 | INIT_LIST_HEAD(&mz->lists[l]); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1663 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1664 | return 0; |
| 1665 | } |
| 1666 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1667 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 1668 | { |
| 1669 | kfree(mem->info.nodeinfo[node]); |
| 1670 | } |
| 1671 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1672 | static int mem_cgroup_size(void) |
| 1673 | { |
| 1674 | int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu); |
| 1675 | return sizeof(struct mem_cgroup) + cpustat_size; |
| 1676 | } |
| 1677 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1678 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 1679 | { |
| 1680 | struct mem_cgroup *mem; |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1681 | int size = mem_cgroup_size(); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1682 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1683 | if (size < PAGE_SIZE) |
| 1684 | mem = kmalloc(size, GFP_KERNEL); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1685 | else |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1686 | mem = vmalloc(size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1687 | |
| 1688 | if (mem) |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1689 | memset(mem, 0, size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1690 | return mem; |
| 1691 | } |
| 1692 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1693 | /* |
| 1694 | * At destroying mem_cgroup, references from swap_cgroup can remain. |
| 1695 | * (scanning all at force_empty is too costly...) |
| 1696 | * |
| 1697 | * Instead of clearing all references at force_empty, we remember |
| 1698 | * the number of reference from swap_cgroup and free mem_cgroup when |
| 1699 | * it goes down to 0. |
| 1700 | * |
| 1701 | * When mem_cgroup is destroyed, mem->obsolete will be set to 0 and |
| 1702 | * entry which points to this memcg will be ignore at swapin. |
| 1703 | * |
| 1704 | * Removal of cgroup itself succeeds regardless of refs from swap. |
| 1705 | */ |
| 1706 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1707 | static void mem_cgroup_free(struct mem_cgroup *mem) |
| 1708 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1709 | if (atomic_read(&mem->refcnt) > 0) |
| 1710 | return; |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1711 | if (mem_cgroup_size() < PAGE_SIZE) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1712 | kfree(mem); |
| 1713 | else |
| 1714 | vfree(mem); |
| 1715 | } |
| 1716 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1717 | static void mem_cgroup_get(struct mem_cgroup *mem) |
| 1718 | { |
| 1719 | atomic_inc(&mem->refcnt); |
| 1720 | } |
| 1721 | |
| 1722 | static void mem_cgroup_put(struct mem_cgroup *mem) |
| 1723 | { |
| 1724 | if (atomic_dec_and_test(&mem->refcnt)) { |
| 1725 | if (!mem->obsolete) |
| 1726 | return; |
| 1727 | mem_cgroup_free(mem); |
| 1728 | } |
| 1729 | } |
| 1730 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1731 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 1732 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 1733 | static void __init enable_swap_cgroup(void) |
| 1734 | { |
| 1735 | if (!mem_cgroup_subsys.disabled && really_do_swap_account) |
| 1736 | do_swap_account = 1; |
| 1737 | } |
| 1738 | #else |
| 1739 | static void __init enable_swap_cgroup(void) |
| 1740 | { |
| 1741 | } |
| 1742 | #endif |
| 1743 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1744 | static struct cgroup_subsys_state * |
| 1745 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 1746 | { |
| 1747 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1748 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1749 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1750 | mem = mem_cgroup_alloc(); |
| 1751 | if (!mem) |
| 1752 | return ERR_PTR(-ENOMEM); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1753 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1754 | res_counter_init(&mem->res); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1755 | res_counter_init(&mem->memsw); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1756 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1757 | for_each_node_state(node, N_POSSIBLE) |
| 1758 | if (alloc_mem_cgroup_per_zone_info(mem, node)) |
| 1759 | goto free_out; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 1760 | /* root ? */ |
| 1761 | if (cont->parent == NULL) |
| 1762 | enable_swap_cgroup(); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1763 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1764 | return &mem->css; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1765 | free_out: |
| 1766 | for_each_node_state(node, N_POSSIBLE) |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1767 | free_mem_cgroup_per_zone_info(mem, node); |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1768 | mem_cgroup_free(mem); |
Li Zefan | 2dda81c | 2008-02-23 15:24:14 -0800 | [diff] [blame] | 1769 | return ERR_PTR(-ENOMEM); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1770 | } |
| 1771 | |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 1772 | static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
| 1773 | struct cgroup *cont) |
| 1774 | { |
| 1775 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1776 | mem->obsolete = 1; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1777 | mem_cgroup_force_empty(mem, false); |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 1778 | } |
| 1779 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1780 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 1781 | struct cgroup *cont) |
| 1782 | { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1783 | int node; |
| 1784 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 1785 | |
| 1786 | for_each_node_state(node, N_POSSIBLE) |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1787 | free_mem_cgroup_per_zone_info(mem, node); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1788 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1789 | mem_cgroup_free(mem_cgroup_from_cont(cont)); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 1793 | struct cgroup *cont) |
| 1794 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame^] | 1795 | int ret; |
| 1796 | |
| 1797 | ret = cgroup_add_files(cont, ss, mem_cgroup_files, |
| 1798 | ARRAY_SIZE(mem_cgroup_files)); |
| 1799 | |
| 1800 | if (!ret) |
| 1801 | ret = register_memsw_files(cont, ss); |
| 1802 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1803 | } |
| 1804 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1805 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 1806 | struct cgroup *cont, |
| 1807 | struct cgroup *old_cont, |
| 1808 | struct task_struct *p) |
| 1809 | { |
| 1810 | struct mm_struct *mm; |
| 1811 | struct mem_cgroup *mem, *old_mem; |
| 1812 | |
| 1813 | mm = get_task_mm(p); |
| 1814 | if (mm == NULL) |
| 1815 | return; |
| 1816 | |
| 1817 | mem = mem_cgroup_from_cont(cont); |
| 1818 | old_mem = mem_cgroup_from_cont(old_cont); |
| 1819 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1820 | /* |
| 1821 | * Only thread group leaders are allowed to migrate, the mm_struct is |
| 1822 | * in effect owned by the leader |
| 1823 | */ |
Pavel Emelyanov | 52ea27e | 2008-03-19 17:00:45 -0700 | [diff] [blame] | 1824 | if (!thread_group_leader(p)) |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1825 | goto out; |
| 1826 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1827 | out: |
| 1828 | mmput(mm); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1829 | } |
| 1830 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1831 | struct cgroup_subsys mem_cgroup_subsys = { |
| 1832 | .name = "memory", |
| 1833 | .subsys_id = mem_cgroup_subsys_id, |
| 1834 | .create = mem_cgroup_create, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 1835 | .pre_destroy = mem_cgroup_pre_destroy, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1836 | .destroy = mem_cgroup_destroy, |
| 1837 | .populate = mem_cgroup_populate, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1838 | .attach = mem_cgroup_move_task, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1839 | .early_init = 0, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1840 | }; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 1841 | |
| 1842 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 1843 | |
| 1844 | static int __init disable_swap_account(char *s) |
| 1845 | { |
| 1846 | really_do_swap_account = 0; |
| 1847 | return 1; |
| 1848 | } |
| 1849 | __setup("noswapaccount", disable_swap_account); |
| 1850 | #endif |