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