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> |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 30 | #include <linux/limits.h> |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 31 | #include <linux/mutex.h> |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 32 | #include <linux/slab.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 33 | #include <linux/swap.h> |
| 34 | #include <linux/spinlock.h> |
| 35 | #include <linux/fs.h> |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 36 | #include <linux/seq_file.h> |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 37 | #include <linux/vmalloc.h> |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 38 | #include <linux/mm_inline.h> |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 39 | #include <linux/page_cgroup.h> |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 40 | #include "internal.h" |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 41 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 42 | #include <asm/uaccess.h> |
| 43 | |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 44 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 45 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 46 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 47 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 48 | /* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */ |
| 49 | int do_swap_account __read_mostly; |
| 50 | static int really_do_swap_account __initdata = 1; /* for remember boot option*/ |
| 51 | #else |
| 52 | #define do_swap_account (0) |
| 53 | #endif |
| 54 | |
Daisuke Nishimura | 7f4d454 | 2009-01-07 18:08:29 -0800 | [diff] [blame] | 55 | static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */ |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 56 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 57 | /* |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 58 | * Statistics for memory cgroup. |
| 59 | */ |
| 60 | enum mem_cgroup_stat_index { |
| 61 | /* |
| 62 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. |
| 63 | */ |
| 64 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ |
| 65 | MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */ |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 66 | MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */ |
| 67 | MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 68 | |
| 69 | MEM_CGROUP_STAT_NSTATS, |
| 70 | }; |
| 71 | |
| 72 | struct mem_cgroup_stat_cpu { |
| 73 | s64 count[MEM_CGROUP_STAT_NSTATS]; |
| 74 | } ____cacheline_aligned_in_smp; |
| 75 | |
| 76 | struct mem_cgroup_stat { |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 77 | struct mem_cgroup_stat_cpu cpustat[0]; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | /* |
| 81 | * For accounting under irq disable, no need for increment preempt count. |
| 82 | */ |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 83 | 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] | 84 | enum mem_cgroup_stat_index idx, int val) |
| 85 | { |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 86 | stat->count[idx] += val; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat, |
| 90 | enum mem_cgroup_stat_index idx) |
| 91 | { |
| 92 | int cpu; |
| 93 | s64 ret = 0; |
| 94 | for_each_possible_cpu(cpu) |
| 95 | ret += stat->cpustat[cpu].count[idx]; |
| 96 | return ret; |
| 97 | } |
| 98 | |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 99 | static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat) |
| 100 | { |
| 101 | s64 ret; |
| 102 | |
| 103 | ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE); |
| 104 | ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS); |
| 105 | return ret; |
| 106 | } |
| 107 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 108 | /* |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 109 | * per-zone information in memory controller. |
| 110 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 111 | struct mem_cgroup_per_zone { |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 112 | /* |
| 113 | * spin_lock to protect the per cgroup LRU |
| 114 | */ |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 115 | struct list_head lists[NR_LRU_LISTS]; |
| 116 | unsigned long count[NR_LRU_LISTS]; |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 117 | |
| 118 | struct zone_reclaim_stat reclaim_stat; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 119 | }; |
| 120 | /* Macro for accessing counter */ |
| 121 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) |
| 122 | |
| 123 | struct mem_cgroup_per_node { |
| 124 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; |
| 125 | }; |
| 126 | |
| 127 | struct mem_cgroup_lru_info { |
| 128 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; |
| 129 | }; |
| 130 | |
| 131 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 132 | * The memory controller data structure. The memory controller controls both |
| 133 | * page cache and RSS per cgroup. We would eventually like to provide |
| 134 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, |
| 135 | * to help the administrator determine what knobs to tune. |
| 136 | * |
| 137 | * 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] | 138 | * we hit the water mark. May be even add a low water mark, such that |
| 139 | * no reclaim occurs from a cgroup at it's low water mark, this is |
| 140 | * a feature that will be implemented much later in the future. |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 141 | */ |
| 142 | struct mem_cgroup { |
| 143 | struct cgroup_subsys_state css; |
| 144 | /* |
| 145 | * the counter to account for memory usage |
| 146 | */ |
| 147 | struct res_counter res; |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 148 | /* |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 149 | * the counter to account for mem+swap usage. |
| 150 | */ |
| 151 | struct res_counter memsw; |
| 152 | /* |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 153 | * Per cgroup active and inactive list, similar to the |
| 154 | * per zone LRU lists. |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 155 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 156 | struct mem_cgroup_lru_info info; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 157 | |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 158 | /* |
| 159 | protect against reclaim related member. |
| 160 | */ |
| 161 | spinlock_t reclaim_param_lock; |
| 162 | |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 163 | int prev_priority; /* for recording reclaim priority */ |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | * While reclaiming in a hiearchy, we cache the last child we |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 167 | * reclaimed from. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 168 | */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 169 | int last_scanned_child; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 170 | /* |
| 171 | * Should the accounting and control be hierarchical, per subtree? |
| 172 | */ |
| 173 | bool use_hierarchy; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 174 | unsigned long last_oom_jiffies; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 175 | atomic_t refcnt; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 176 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 177 | unsigned int swappiness; |
| 178 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 179 | /* |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 180 | * statistics. This must be placed at the end of memcg. |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 181 | */ |
| 182 | struct mem_cgroup_stat stat; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 183 | }; |
| 184 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 185 | enum charge_type { |
| 186 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
| 187 | MEM_CGROUP_CHARGE_TYPE_MAPPED, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 188 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 189 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 190 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 191 | NR_CHARGE_TYPE, |
| 192 | }; |
| 193 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 194 | /* only for here (for easy reading.) */ |
| 195 | #define PCGF_CACHE (1UL << PCG_CACHE) |
| 196 | #define PCGF_USED (1UL << PCG_USED) |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 197 | #define PCGF_LOCK (1UL << PCG_LOCK) |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 198 | static const unsigned long |
| 199 | pcg_default_flags[NR_CHARGE_TYPE] = { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 200 | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */ |
| 201 | PCGF_USED | PCGF_LOCK, /* Anon */ |
| 202 | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 203 | 0, /* FORCE */ |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 204 | }; |
| 205 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 206 | /* for encoding cft->private value on file */ |
| 207 | #define _MEM (0) |
| 208 | #define _MEMSWAP (1) |
| 209 | #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) |
| 210 | #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff) |
| 211 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
| 212 | |
| 213 | static void mem_cgroup_get(struct mem_cgroup *mem); |
| 214 | static void mem_cgroup_put(struct mem_cgroup *mem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 215 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 216 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 217 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, |
| 218 | struct page_cgroup *pc, |
| 219 | bool charge) |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 220 | { |
| 221 | int val = (charge)? 1 : -1; |
| 222 | struct mem_cgroup_stat *stat = &mem->stat; |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 223 | struct mem_cgroup_stat_cpu *cpustat; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 224 | int cpu = get_cpu(); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 225 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 226 | cpustat = &stat->cpustat[cpu]; |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 227 | if (PageCgroupCache(pc)) |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 228 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 229 | else |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 230 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val); |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 231 | |
| 232 | if (charge) |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 233 | __mem_cgroup_stat_add_safe(cpustat, |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 234 | MEM_CGROUP_STAT_PGPGIN_COUNT, 1); |
| 235 | else |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 236 | __mem_cgroup_stat_add_safe(cpustat, |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 237 | MEM_CGROUP_STAT_PGPGOUT_COUNT, 1); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 238 | put_cpu(); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 239 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 240 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 241 | static struct mem_cgroup_per_zone * |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 242 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) |
| 243 | { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 244 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; |
| 245 | } |
| 246 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 247 | static struct mem_cgroup_per_zone * |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 248 | page_cgroup_zoneinfo(struct page_cgroup *pc) |
| 249 | { |
| 250 | struct mem_cgroup *mem = pc->mem_cgroup; |
| 251 | int nid = page_cgroup_nid(pc); |
| 252 | int zid = page_cgroup_zid(pc); |
| 253 | |
KOSAKI Motohiro | 5499276 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 254 | if (!mem) |
| 255 | return NULL; |
| 256 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 257 | return mem_cgroup_zoneinfo(mem, nid, zid); |
| 258 | } |
| 259 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 260 | static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem, |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 261 | enum lru_list idx) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 262 | { |
| 263 | int nid, zid; |
| 264 | struct mem_cgroup_per_zone *mz; |
| 265 | u64 total = 0; |
| 266 | |
| 267 | for_each_online_node(nid) |
| 268 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 269 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 270 | total += MEM_CGROUP_ZSTAT(mz, idx); |
| 271 | } |
| 272 | return total; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 275 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 276 | { |
| 277 | return container_of(cgroup_subsys_state(cont, |
| 278 | mem_cgroup_subsys_id), struct mem_cgroup, |
| 279 | css); |
| 280 | } |
| 281 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 282 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 283 | { |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 284 | /* |
| 285 | * mm_update_next_owner() may clear mm->owner to NULL |
| 286 | * if it races with swapoff, page migration, etc. |
| 287 | * So this can be called with p == NULL. |
| 288 | */ |
| 289 | if (unlikely(!p)) |
| 290 | return NULL; |
| 291 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 292 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
| 293 | struct mem_cgroup, css); |
| 294 | } |
| 295 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 296 | static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) |
| 297 | { |
| 298 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 299 | |
| 300 | if (!mm) |
| 301 | return NULL; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 302 | /* |
| 303 | * Because we have no locks, mm->owner's may be being moved to other |
| 304 | * cgroup. We use css_tryget() here even if this looks |
| 305 | * pessimistic (rather than adding locks here). |
| 306 | */ |
| 307 | rcu_read_lock(); |
| 308 | do { |
| 309 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 310 | if (unlikely(!mem)) |
| 311 | break; |
| 312 | } while (!css_tryget(&mem->css)); |
| 313 | rcu_read_unlock(); |
| 314 | return mem; |
| 315 | } |
| 316 | |
| 317 | static bool mem_cgroup_is_obsolete(struct mem_cgroup *mem) |
| 318 | { |
| 319 | if (!mem) |
| 320 | return true; |
| 321 | return css_is_removed(&mem->css); |
| 322 | } |
| 323 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 324 | |
| 325 | /* |
| 326 | * Call callback function against all cgroup under hierarchy tree. |
| 327 | */ |
| 328 | static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data, |
| 329 | int (*func)(struct mem_cgroup *, void *)) |
| 330 | { |
| 331 | int found, ret, nextid; |
| 332 | struct cgroup_subsys_state *css; |
| 333 | struct mem_cgroup *mem; |
| 334 | |
| 335 | if (!root->use_hierarchy) |
| 336 | return (*func)(root, data); |
| 337 | |
| 338 | nextid = 1; |
| 339 | do { |
| 340 | ret = 0; |
| 341 | mem = NULL; |
| 342 | |
| 343 | rcu_read_lock(); |
| 344 | css = css_get_next(&mem_cgroup_subsys, nextid, &root->css, |
| 345 | &found); |
| 346 | if (css && css_tryget(css)) |
| 347 | mem = container_of(css, struct mem_cgroup, css); |
| 348 | rcu_read_unlock(); |
| 349 | |
| 350 | if (mem) { |
| 351 | ret = (*func)(mem, data); |
| 352 | css_put(&mem->css); |
| 353 | } |
| 354 | nextid = found + 1; |
| 355 | } while (!ret && css); |
| 356 | |
| 357 | return ret; |
| 358 | } |
| 359 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 360 | /* |
| 361 | * Following LRU functions are allowed to be used without PCG_LOCK. |
| 362 | * Operations are called by routine of global LRU independently from memcg. |
| 363 | * What we have to take care of here is validness of pc->mem_cgroup. |
| 364 | * |
| 365 | * Changes to pc->mem_cgroup happens when |
| 366 | * 1. charge |
| 367 | * 2. moving account |
| 368 | * In typical case, "charge" is done before add-to-lru. Exception is SwapCache. |
| 369 | * It is added to LRU before charge. |
| 370 | * If PCG_USED bit is not set, page_cgroup is not added to this private LRU. |
| 371 | * When moving account, the page is not on LRU. It's isolated. |
| 372 | */ |
| 373 | |
| 374 | 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] | 375 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 376 | struct page_cgroup *pc; |
| 377 | struct mem_cgroup *mem; |
| 378 | struct mem_cgroup_per_zone *mz; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 379 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 380 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 381 | return; |
| 382 | pc = lookup_page_cgroup(page); |
| 383 | /* can happen while we handle swapcache. */ |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 384 | if (list_empty(&pc->lru) || !pc->mem_cgroup) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 385 | return; |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 386 | /* |
| 387 | * We don't check PCG_USED bit. It's cleared when the "page" is finally |
| 388 | * removed from global LRU. |
| 389 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 390 | mz = page_cgroup_zoneinfo(pc); |
| 391 | mem = pc->mem_cgroup; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 392 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 393 | list_del_init(&pc->lru); |
| 394 | return; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 395 | } |
| 396 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 397 | void mem_cgroup_del_lru(struct page *page) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 398 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 399 | mem_cgroup_del_lru_list(page, page_lru(page)); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 400 | } |
| 401 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 402 | 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] | 403 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 404 | struct mem_cgroup_per_zone *mz; |
| 405 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 406 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 407 | if (mem_cgroup_disabled()) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 408 | return; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 409 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 410 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 411 | /* |
| 412 | * Used bit is set without atomic ops but after smp_wmb(). |
| 413 | * For making pc->mem_cgroup visible, insert smp_rmb() here. |
| 414 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 415 | smp_rmb(); |
| 416 | /* unused page is not rotated. */ |
| 417 | if (!PageCgroupUsed(pc)) |
| 418 | return; |
| 419 | mz = page_cgroup_zoneinfo(pc); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 420 | list_move(&pc->lru, &mz->lists[lru]); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 421 | } |
| 422 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 423 | void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru) |
| 424 | { |
| 425 | struct page_cgroup *pc; |
| 426 | struct mem_cgroup_per_zone *mz; |
| 427 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 428 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 429 | return; |
| 430 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 431 | /* |
| 432 | * Used bit is set without atomic ops but after smp_wmb(). |
| 433 | * For making pc->mem_cgroup visible, insert smp_rmb() here. |
| 434 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 435 | smp_rmb(); |
| 436 | if (!PageCgroupUsed(pc)) |
| 437 | return; |
| 438 | |
| 439 | mz = page_cgroup_zoneinfo(pc); |
| 440 | MEM_CGROUP_ZSTAT(mz, lru) += 1; |
| 441 | list_add(&pc->lru, &mz->lists[lru]); |
| 442 | } |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 443 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 444 | /* |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 445 | * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to |
| 446 | * lru because the page may.be reused after it's fully uncharged (because of |
| 447 | * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge |
| 448 | * it again. This function is only used to charge SwapCache. It's done under |
| 449 | * lock_page and expected that zone->lru_lock is never held. |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 450 | */ |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 451 | static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 452 | { |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 453 | unsigned long flags; |
| 454 | struct zone *zone = page_zone(page); |
| 455 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 456 | |
| 457 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 458 | /* |
| 459 | * Forget old LRU when this page_cgroup is *not* used. This Used bit |
| 460 | * is guarded by lock_page() because the page is SwapCache. |
| 461 | */ |
| 462 | if (!PageCgroupUsed(pc)) |
| 463 | mem_cgroup_del_lru_list(page, page_lru(page)); |
| 464 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 465 | } |
| 466 | |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 467 | static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page) |
| 468 | { |
| 469 | unsigned long flags; |
| 470 | struct zone *zone = page_zone(page); |
| 471 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 472 | |
| 473 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 474 | /* link when the page is linked to LRU but page_cgroup isn't */ |
| 475 | if (PageLRU(page) && list_empty(&pc->lru)) |
| 476 | mem_cgroup_add_lru_list(page, page_lru(page)); |
| 477 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
| 478 | } |
| 479 | |
| 480 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 481 | void mem_cgroup_move_lists(struct page *page, |
| 482 | enum lru_list from, enum lru_list to) |
| 483 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 484 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 485 | return; |
| 486 | mem_cgroup_del_lru_list(page, from); |
| 487 | mem_cgroup_add_lru_list(page, to); |
| 488 | } |
| 489 | |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 490 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
| 491 | { |
| 492 | int ret; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 493 | struct mem_cgroup *curr = NULL; |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 494 | |
| 495 | task_lock(task); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 496 | rcu_read_lock(); |
| 497 | curr = try_get_mem_cgroup_from_mm(task->mm); |
| 498 | rcu_read_unlock(); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 499 | task_unlock(task); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 500 | if (!curr) |
| 501 | return 0; |
| 502 | if (curr->use_hierarchy) |
| 503 | ret = css_is_ancestor(&curr->css, &mem->css); |
| 504 | else |
| 505 | ret = (curr == mem); |
| 506 | css_put(&curr->css); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 507 | return ret; |
| 508 | } |
| 509 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 510 | /* |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 511 | * prev_priority control...this will be used in memory reclaim path. |
| 512 | */ |
| 513 | int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem) |
| 514 | { |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 515 | int prev_priority; |
| 516 | |
| 517 | spin_lock(&mem->reclaim_param_lock); |
| 518 | prev_priority = mem->prev_priority; |
| 519 | spin_unlock(&mem->reclaim_param_lock); |
| 520 | |
| 521 | return prev_priority; |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority) |
| 525 | { |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 526 | spin_lock(&mem->reclaim_param_lock); |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 527 | if (priority < mem->prev_priority) |
| 528 | mem->prev_priority = priority; |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 529 | spin_unlock(&mem->reclaim_param_lock); |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority) |
| 533 | { |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 534 | spin_lock(&mem->reclaim_param_lock); |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 535 | mem->prev_priority = priority; |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 536 | spin_unlock(&mem->reclaim_param_lock); |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 537 | } |
| 538 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 539 | static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages) |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 540 | { |
| 541 | unsigned long active; |
| 542 | unsigned long inactive; |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 543 | unsigned long gb; |
| 544 | unsigned long inactive_ratio; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 545 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 546 | inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON); |
| 547 | active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 548 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 549 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
| 550 | if (gb) |
| 551 | inactive_ratio = int_sqrt(10 * gb); |
| 552 | else |
| 553 | inactive_ratio = 1; |
| 554 | |
| 555 | if (present_pages) { |
| 556 | present_pages[0] = inactive; |
| 557 | present_pages[1] = active; |
| 558 | } |
| 559 | |
| 560 | return inactive_ratio; |
| 561 | } |
| 562 | |
| 563 | int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg) |
| 564 | { |
| 565 | unsigned long active; |
| 566 | unsigned long inactive; |
| 567 | unsigned long present_pages[2]; |
| 568 | unsigned long inactive_ratio; |
| 569 | |
| 570 | inactive_ratio = calc_inactive_ratio(memcg, present_pages); |
| 571 | |
| 572 | inactive = present_pages[0]; |
| 573 | active = present_pages[1]; |
| 574 | |
| 575 | if (inactive * inactive_ratio < active) |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 576 | return 1; |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
KOSAKI Motohiro | a3d8e05 | 2009-01-07 18:08:19 -0800 | [diff] [blame] | 581 | unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg, |
| 582 | struct zone *zone, |
| 583 | enum lru_list lru) |
| 584 | { |
| 585 | int nid = zone->zone_pgdat->node_id; |
| 586 | int zid = zone_idx(zone); |
| 587 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 588 | |
| 589 | return MEM_CGROUP_ZSTAT(mz, lru); |
| 590 | } |
| 591 | |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 592 | struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg, |
| 593 | struct zone *zone) |
| 594 | { |
| 595 | int nid = zone->zone_pgdat->node_id; |
| 596 | int zid = zone_idx(zone); |
| 597 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 598 | |
| 599 | return &mz->reclaim_stat; |
| 600 | } |
| 601 | |
| 602 | struct zone_reclaim_stat * |
| 603 | mem_cgroup_get_reclaim_stat_from_page(struct page *page) |
| 604 | { |
| 605 | struct page_cgroup *pc; |
| 606 | struct mem_cgroup_per_zone *mz; |
| 607 | |
| 608 | if (mem_cgroup_disabled()) |
| 609 | return NULL; |
| 610 | |
| 611 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 612 | /* |
| 613 | * Used bit is set without atomic ops but after smp_wmb(). |
| 614 | * For making pc->mem_cgroup visible, insert smp_rmb() here. |
| 615 | */ |
| 616 | smp_rmb(); |
| 617 | if (!PageCgroupUsed(pc)) |
| 618 | return NULL; |
| 619 | |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 620 | mz = page_cgroup_zoneinfo(pc); |
| 621 | if (!mz) |
| 622 | return NULL; |
| 623 | |
| 624 | return &mz->reclaim_stat; |
| 625 | } |
| 626 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 627 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 628 | struct list_head *dst, |
| 629 | unsigned long *scanned, int order, |
| 630 | int mode, struct zone *z, |
| 631 | struct mem_cgroup *mem_cont, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 632 | int active, int file) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 633 | { |
| 634 | unsigned long nr_taken = 0; |
| 635 | struct page *page; |
| 636 | unsigned long scan; |
| 637 | LIST_HEAD(pc_list); |
| 638 | struct list_head *src; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 639 | struct page_cgroup *pc, *tmp; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 640 | int nid = z->zone_pgdat->node_id; |
| 641 | int zid = zone_idx(z); |
| 642 | struct mem_cgroup_per_zone *mz; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 643 | int lru = LRU_FILE * !!file + !!active; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 644 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 645 | BUG_ON(!mem_cont); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 646 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 647 | src = &mz->lists[lru]; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 648 | |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 649 | scan = 0; |
| 650 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 651 | if (scan >= nr_to_scan) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 652 | break; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 653 | |
| 654 | page = pc->page; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 655 | if (unlikely(!PageCgroupUsed(pc))) |
| 656 | continue; |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 657 | if (unlikely(!PageLRU(page))) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 658 | continue; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 659 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 660 | scan++; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 661 | if (__isolate_lru_page(page, mode, file) == 0) { |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 662 | list_move(&page->lru, dst); |
| 663 | nr_taken++; |
| 664 | } |
| 665 | } |
| 666 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 667 | *scanned = scan; |
| 668 | return nr_taken; |
| 669 | } |
| 670 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 671 | #define mem_cgroup_from_res_counter(counter, member) \ |
| 672 | container_of(counter, struct mem_cgroup, member) |
| 673 | |
Daisuke Nishimura | b85a96c | 2009-01-07 18:08:12 -0800 | [diff] [blame] | 674 | static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem) |
| 675 | { |
| 676 | if (do_swap_account) { |
| 677 | if (res_counter_check_under_limit(&mem->res) && |
| 678 | res_counter_check_under_limit(&mem->memsw)) |
| 679 | return true; |
| 680 | } else |
| 681 | if (res_counter_check_under_limit(&mem->res)) |
| 682 | return true; |
| 683 | return false; |
| 684 | } |
| 685 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 686 | static unsigned int get_swappiness(struct mem_cgroup *memcg) |
| 687 | { |
| 688 | struct cgroup *cgrp = memcg->css.cgroup; |
| 689 | unsigned int swappiness; |
| 690 | |
| 691 | /* root ? */ |
| 692 | if (cgrp->parent == NULL) |
| 693 | return vm_swappiness; |
| 694 | |
| 695 | spin_lock(&memcg->reclaim_param_lock); |
| 696 | swappiness = memcg->swappiness; |
| 697 | spin_unlock(&memcg->reclaim_param_lock); |
| 698 | |
| 699 | return swappiness; |
| 700 | } |
| 701 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 702 | static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data) |
| 703 | { |
| 704 | int *val = data; |
| 705 | (*val)++; |
| 706 | return 0; |
| 707 | } |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 708 | |
| 709 | /** |
| 710 | * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode. |
| 711 | * @memcg: The memory cgroup that went over limit |
| 712 | * @p: Task that is going to be killed |
| 713 | * |
| 714 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is |
| 715 | * enabled |
| 716 | */ |
| 717 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) |
| 718 | { |
| 719 | struct cgroup *task_cgrp; |
| 720 | struct cgroup *mem_cgrp; |
| 721 | /* |
| 722 | * Need a buffer in BSS, can't rely on allocations. The code relies |
| 723 | * on the assumption that OOM is serialized for memory controller. |
| 724 | * If this assumption is broken, revisit this code. |
| 725 | */ |
| 726 | static char memcg_name[PATH_MAX]; |
| 727 | int ret; |
| 728 | |
| 729 | if (!memcg) |
| 730 | return; |
| 731 | |
| 732 | |
| 733 | rcu_read_lock(); |
| 734 | |
| 735 | mem_cgrp = memcg->css.cgroup; |
| 736 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); |
| 737 | |
| 738 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); |
| 739 | if (ret < 0) { |
| 740 | /* |
| 741 | * Unfortunately, we are unable to convert to a useful name |
| 742 | * But we'll still print out the usage information |
| 743 | */ |
| 744 | rcu_read_unlock(); |
| 745 | goto done; |
| 746 | } |
| 747 | rcu_read_unlock(); |
| 748 | |
| 749 | printk(KERN_INFO "Task in %s killed", memcg_name); |
| 750 | |
| 751 | rcu_read_lock(); |
| 752 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); |
| 753 | if (ret < 0) { |
| 754 | rcu_read_unlock(); |
| 755 | goto done; |
| 756 | } |
| 757 | rcu_read_unlock(); |
| 758 | |
| 759 | /* |
| 760 | * Continues from above, so we don't need an KERN_ level |
| 761 | */ |
| 762 | printk(KERN_CONT " as a result of limit of %s\n", memcg_name); |
| 763 | done: |
| 764 | |
| 765 | printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n", |
| 766 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, |
| 767 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, |
| 768 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); |
| 769 | printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, " |
| 770 | "failcnt %llu\n", |
| 771 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, |
| 772 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, |
| 773 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); |
| 774 | } |
| 775 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 776 | /* |
| 777 | * This function returns the number of memcg under hierarchy tree. Returns |
| 778 | * 1(self count) if no children. |
| 779 | */ |
| 780 | static int mem_cgroup_count_children(struct mem_cgroup *mem) |
| 781 | { |
| 782 | int num = 0; |
| 783 | mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb); |
| 784 | return num; |
| 785 | } |
| 786 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 787 | /* |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 788 | * Visit the first child (need not be the first child as per the ordering |
| 789 | * of the cgroup list, since we track last_scanned_child) of @mem and use |
| 790 | * that to reclaim free pages from. |
| 791 | */ |
| 792 | static struct mem_cgroup * |
| 793 | mem_cgroup_select_victim(struct mem_cgroup *root_mem) |
| 794 | { |
| 795 | struct mem_cgroup *ret = NULL; |
| 796 | struct cgroup_subsys_state *css; |
| 797 | int nextid, found; |
| 798 | |
| 799 | if (!root_mem->use_hierarchy) { |
| 800 | css_get(&root_mem->css); |
| 801 | ret = root_mem; |
| 802 | } |
| 803 | |
| 804 | while (!ret) { |
| 805 | rcu_read_lock(); |
| 806 | nextid = root_mem->last_scanned_child + 1; |
| 807 | css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css, |
| 808 | &found); |
| 809 | if (css && css_tryget(css)) |
| 810 | ret = container_of(css, struct mem_cgroup, css); |
| 811 | |
| 812 | rcu_read_unlock(); |
| 813 | /* Updates scanning parameter */ |
| 814 | spin_lock(&root_mem->reclaim_param_lock); |
| 815 | if (!css) { |
| 816 | /* this means start scan from ID:1 */ |
| 817 | root_mem->last_scanned_child = 0; |
| 818 | } else |
| 819 | root_mem->last_scanned_child = found; |
| 820 | spin_unlock(&root_mem->reclaim_param_lock); |
| 821 | } |
| 822 | |
| 823 | return ret; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Scan the hierarchy if needed to reclaim memory. We remember the last child |
| 828 | * we reclaimed from, so that we don't end up penalizing one child extensively |
| 829 | * based on its position in the children list. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 830 | * |
| 831 | * root_mem is the original ancestor that we've been reclaim from. |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 832 | * |
| 833 | * We give up and return to the caller when we visit root_mem twice. |
| 834 | * (other groups can be removed while we're walking....) |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 835 | * |
| 836 | * If shrink==true, for avoiding to free too much, this returns immedieately. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 837 | */ |
| 838 | static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem, |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 839 | gfp_t gfp_mask, bool noswap, bool shrink) |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 840 | { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 841 | struct mem_cgroup *victim; |
| 842 | int ret, total = 0; |
| 843 | int loop = 0; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 844 | |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 845 | while (loop < 2) { |
| 846 | victim = mem_cgroup_select_victim(root_mem); |
| 847 | if (victim == root_mem) |
| 848 | loop++; |
| 849 | if (!mem_cgroup_local_usage(&victim->stat)) { |
| 850 | /* this cgroup's local usage == 0 */ |
| 851 | css_put(&victim->css); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 852 | continue; |
| 853 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 854 | /* we use swappiness of local cgroup */ |
| 855 | ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, noswap, |
| 856 | get_swappiness(victim)); |
| 857 | css_put(&victim->css); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 858 | /* |
| 859 | * At shrinking usage, we can't check we should stop here or |
| 860 | * reclaim more. It's depends on callers. last_scanned_child |
| 861 | * will work enough for keeping fairness under tree. |
| 862 | */ |
| 863 | if (shrink) |
| 864 | return ret; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 865 | total += ret; |
Daisuke Nishimura | b85a96c | 2009-01-07 18:08:12 -0800 | [diff] [blame] | 866 | if (mem_cgroup_check_under_limit(root_mem)) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 867 | return 1 + total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 868 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 869 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 870 | } |
| 871 | |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 872 | bool mem_cgroup_oom_called(struct task_struct *task) |
| 873 | { |
| 874 | bool ret = false; |
| 875 | struct mem_cgroup *mem; |
| 876 | struct mm_struct *mm; |
| 877 | |
| 878 | rcu_read_lock(); |
| 879 | mm = task->mm; |
| 880 | if (!mm) |
| 881 | mm = &init_mm; |
| 882 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 883 | if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10)) |
| 884 | ret = true; |
| 885 | rcu_read_unlock(); |
| 886 | return ret; |
| 887 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 888 | |
| 889 | static int record_last_oom_cb(struct mem_cgroup *mem, void *data) |
| 890 | { |
| 891 | mem->last_oom_jiffies = jiffies; |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | static void record_last_oom(struct mem_cgroup *mem) |
| 896 | { |
| 897 | mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb); |
| 898 | } |
| 899 | |
| 900 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 901 | /* |
| 902 | * Unlike exported interface, "oom" parameter is added. if oom==true, |
| 903 | * oom-killer can be invoked. |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 904 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 905 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 906 | gfp_t gfp_mask, struct mem_cgroup **memcg, |
| 907 | bool oom) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 908 | { |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 909 | struct mem_cgroup *mem, *mem_over_limit; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 910 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 911 | struct res_counter *fail_res; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 912 | |
| 913 | if (unlikely(test_thread_flag(TIF_MEMDIE))) { |
| 914 | /* Don't account this! */ |
| 915 | *memcg = NULL; |
| 916 | return 0; |
| 917 | } |
| 918 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 919 | /* |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 920 | * We always charge the cgroup the mm_struct belongs to. |
| 921 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 922 | * thread group leader migrates. It's possible that mm is not |
| 923 | * set, if so charge the init_mm (happens for pagecache usage). |
| 924 | */ |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 925 | mem = *memcg; |
| 926 | if (likely(!mem)) { |
| 927 | mem = try_get_mem_cgroup_from_mm(mm); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 928 | *memcg = mem; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 929 | } else { |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 930 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 931 | } |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 932 | if (unlikely(!mem)) |
| 933 | return 0; |
| 934 | |
KAMEZAWA Hiroyuki | a8031cb | 2009-04-13 14:40:08 -0700 | [diff] [blame] | 935 | VM_BUG_ON(!mem || mem_cgroup_is_obsolete(mem)); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 936 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 937 | while (1) { |
| 938 | int ret; |
| 939 | bool noswap = false; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 940 | |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 941 | ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 942 | if (likely(!ret)) { |
| 943 | if (!do_swap_account) |
| 944 | break; |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 945 | ret = res_counter_charge(&mem->memsw, PAGE_SIZE, |
| 946 | &fail_res); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 947 | if (likely(!ret)) |
| 948 | break; |
| 949 | /* mem+swap counter fails */ |
| 950 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 951 | noswap = true; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 952 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, |
| 953 | memsw); |
| 954 | } else |
| 955 | /* mem counter fails */ |
| 956 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, |
| 957 | res); |
| 958 | |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 959 | if (!(gfp_mask & __GFP_WAIT)) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 960 | goto nomem; |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 961 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 962 | ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask, |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 963 | noswap, false); |
Daisuke Nishimura | 4d1c627 | 2009-01-15 13:51:14 -0800 | [diff] [blame] | 964 | if (ret) |
| 965 | continue; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 966 | |
| 967 | /* |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 968 | * try_to_free_mem_cgroup_pages() might not give us a full |
| 969 | * picture of reclaim. Some pages are reclaimed and might be |
| 970 | * moved to swap cache or just unmapped from the cgroup. |
| 971 | * Check the limit again to see if the reclaim reduced the |
| 972 | * current usage of the cgroup before giving up |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 973 | * |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 974 | */ |
Daisuke Nishimura | b85a96c | 2009-01-07 18:08:12 -0800 | [diff] [blame] | 975 | if (mem_cgroup_check_under_limit(mem_over_limit)) |
| 976 | continue; |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 977 | |
| 978 | if (!nr_retries--) { |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 979 | if (oom) { |
Daisuke Nishimura | 7f4d454 | 2009-01-07 18:08:29 -0800 | [diff] [blame] | 980 | mutex_lock(&memcg_tasklist); |
KAMEZAWA Hiroyuki | 8870075 | 2009-01-07 18:08:09 -0800 | [diff] [blame] | 981 | mem_cgroup_out_of_memory(mem_over_limit, gfp_mask); |
Daisuke Nishimura | 7f4d454 | 2009-01-07 18:08:29 -0800 | [diff] [blame] | 982 | mutex_unlock(&memcg_tasklist); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 983 | record_last_oom(mem_over_limit); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 984 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 985 | goto nomem; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 986 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 987 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 988 | return 0; |
| 989 | nomem: |
| 990 | css_put(&mem->css); |
| 991 | return -ENOMEM; |
| 992 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 993 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 994 | |
| 995 | /* |
| 996 | * A helper function to get mem_cgroup from ID. must be called under |
| 997 | * rcu_read_lock(). The caller must check css_is_removed() or some if |
| 998 | * it's concern. (dropping refcnt from swap can be called against removed |
| 999 | * memcg.) |
| 1000 | */ |
| 1001 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) |
| 1002 | { |
| 1003 | struct cgroup_subsys_state *css; |
| 1004 | |
| 1005 | /* ID 0 is unused ID */ |
| 1006 | if (!id) |
| 1007 | return NULL; |
| 1008 | css = css_lookup(&mem_cgroup_subsys, id); |
| 1009 | if (!css) |
| 1010 | return NULL; |
| 1011 | return container_of(css, struct mem_cgroup, css); |
| 1012 | } |
| 1013 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1014 | static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page) |
| 1015 | { |
| 1016 | struct mem_cgroup *mem; |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1017 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1018 | unsigned short id; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1019 | swp_entry_t ent; |
| 1020 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1021 | VM_BUG_ON(!PageLocked(page)); |
| 1022 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1023 | if (!PageSwapCache(page)) |
| 1024 | return NULL; |
| 1025 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1026 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 1027 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1028 | if (PageCgroupUsed(pc)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1029 | mem = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1030 | if (mem && !css_tryget(&mem->css)) |
| 1031 | mem = NULL; |
| 1032 | } else { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1033 | ent.val = page_private(page); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1034 | id = lookup_swap_cgroup(ent); |
| 1035 | rcu_read_lock(); |
| 1036 | mem = mem_cgroup_lookup(id); |
| 1037 | if (mem && !css_tryget(&mem->css)) |
| 1038 | mem = NULL; |
| 1039 | rcu_read_unlock(); |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1040 | } |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 1041 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1042 | return mem; |
| 1043 | } |
| 1044 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1045 | /* |
Daisuke Nishimura | a5e924f | 2009-01-07 18:08:28 -0800 | [diff] [blame] | 1046 | * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1047 | * USED state. If already USED, uncharge and return. |
| 1048 | */ |
| 1049 | |
| 1050 | static void __mem_cgroup_commit_charge(struct mem_cgroup *mem, |
| 1051 | struct page_cgroup *pc, |
| 1052 | enum charge_type ctype) |
| 1053 | { |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1054 | /* try_charge() can return NULL to *memcg, taking care of it. */ |
| 1055 | if (!mem) |
| 1056 | return; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1057 | |
| 1058 | lock_page_cgroup(pc); |
| 1059 | if (unlikely(PageCgroupUsed(pc))) { |
| 1060 | unlock_page_cgroup(pc); |
| 1061 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1062 | if (do_swap_account) |
| 1063 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1064 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1065 | return; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1066 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1067 | pc->mem_cgroup = mem; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1068 | smp_wmb(); |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 1069 | pc->flags = pcg_default_flags[ctype]; |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 1070 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1071 | mem_cgroup_charge_statistics(mem, pc, true); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1072 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1073 | unlock_page_cgroup(pc); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1076 | /** |
| 1077 | * mem_cgroup_move_account - move account of the page |
| 1078 | * @pc: page_cgroup of the page. |
| 1079 | * @from: mem_cgroup which the page is moved from. |
| 1080 | * @to: mem_cgroup which the page is moved to. @from != @to. |
| 1081 | * |
| 1082 | * The caller must confirm following. |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1083 | * - page is not on LRU (isolate_page() is useful.) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1084 | * |
| 1085 | * returns 0 at success, |
| 1086 | * returns -EBUSY when lock is busy or "pc" is unstable. |
| 1087 | * |
| 1088 | * This function does "uncharge" from old cgroup but doesn't do "charge" to |
| 1089 | * new cgroup. It should be done by a caller. |
| 1090 | */ |
| 1091 | |
| 1092 | static int mem_cgroup_move_account(struct page_cgroup *pc, |
| 1093 | struct mem_cgroup *from, struct mem_cgroup *to) |
| 1094 | { |
| 1095 | struct mem_cgroup_per_zone *from_mz, *to_mz; |
| 1096 | int nid, zid; |
| 1097 | int ret = -EBUSY; |
| 1098 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1099 | VM_BUG_ON(from == to); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1100 | VM_BUG_ON(PageLRU(pc->page)); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1101 | |
| 1102 | nid = page_cgroup_nid(pc); |
| 1103 | zid = page_cgroup_zid(pc); |
| 1104 | from_mz = mem_cgroup_zoneinfo(from, nid, zid); |
| 1105 | to_mz = mem_cgroup_zoneinfo(to, nid, zid); |
| 1106 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1107 | if (!trylock_page_cgroup(pc)) |
| 1108 | return ret; |
| 1109 | |
| 1110 | if (!PageCgroupUsed(pc)) |
| 1111 | goto out; |
| 1112 | |
| 1113 | if (pc->mem_cgroup != from) |
| 1114 | goto out; |
| 1115 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1116 | res_counter_uncharge(&from->res, PAGE_SIZE); |
| 1117 | mem_cgroup_charge_statistics(from, pc, false); |
| 1118 | if (do_swap_account) |
| 1119 | res_counter_uncharge(&from->memsw, PAGE_SIZE); |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 1120 | css_put(&from->css); |
| 1121 | |
| 1122 | css_get(&to->css); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1123 | pc->mem_cgroup = to; |
| 1124 | mem_cgroup_charge_statistics(to, pc, true); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1125 | ret = 0; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1126 | out: |
| 1127 | unlock_page_cgroup(pc); |
| 1128 | return ret; |
| 1129 | } |
| 1130 | |
| 1131 | /* |
| 1132 | * move charges to its parent. |
| 1133 | */ |
| 1134 | |
| 1135 | static int mem_cgroup_move_parent(struct page_cgroup *pc, |
| 1136 | struct mem_cgroup *child, |
| 1137 | gfp_t gfp_mask) |
| 1138 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1139 | struct page *page = pc->page; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1140 | struct cgroup *cg = child->css.cgroup; |
| 1141 | struct cgroup *pcg = cg->parent; |
| 1142 | struct mem_cgroup *parent; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1143 | int ret; |
| 1144 | |
| 1145 | /* Is ROOT ? */ |
| 1146 | if (!pcg) |
| 1147 | return -EINVAL; |
| 1148 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1149 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1150 | parent = mem_cgroup_from_cont(pcg); |
| 1151 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1152 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1153 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1154 | if (ret || !parent) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1155 | return ret; |
| 1156 | |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 1157 | if (!get_page_unless_zero(page)) { |
| 1158 | ret = -EBUSY; |
| 1159 | goto uncharge; |
| 1160 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1161 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1162 | ret = isolate_lru_page(page); |
| 1163 | |
| 1164 | if (ret) |
| 1165 | goto cancel; |
| 1166 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1167 | ret = mem_cgroup_move_account(pc, child, parent); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1168 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1169 | putback_lru_page(page); |
| 1170 | if (!ret) { |
| 1171 | put_page(page); |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 1172 | /* drop extra refcnt by try_charge() */ |
| 1173 | css_put(&parent->css); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1174 | return 0; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1175 | } |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 1176 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1177 | cancel: |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 1178 | put_page(page); |
| 1179 | uncharge: |
| 1180 | /* drop extra refcnt by try_charge() */ |
| 1181 | css_put(&parent->css); |
| 1182 | /* uncharge if move fails */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1183 | res_counter_uncharge(&parent->res, PAGE_SIZE); |
| 1184 | if (do_swap_account) |
| 1185 | res_counter_uncharge(&parent->memsw, PAGE_SIZE); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1186 | return ret; |
| 1187 | } |
| 1188 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1189 | /* |
| 1190 | * Charge the memory controller for page usage. |
| 1191 | * Return |
| 1192 | * 0 if the charge was successful |
| 1193 | * < 0 if the cgroup is over its limit |
| 1194 | */ |
| 1195 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, |
| 1196 | gfp_t gfp_mask, enum charge_type ctype, |
| 1197 | struct mem_cgroup *memcg) |
| 1198 | { |
| 1199 | struct mem_cgroup *mem; |
| 1200 | struct page_cgroup *pc; |
| 1201 | int ret; |
| 1202 | |
| 1203 | pc = lookup_page_cgroup(page); |
| 1204 | /* can happen at boot */ |
| 1205 | if (unlikely(!pc)) |
| 1206 | return 0; |
| 1207 | prefetchw(pc); |
| 1208 | |
| 1209 | mem = memcg; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1210 | ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1211 | if (ret || !mem) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1212 | return ret; |
| 1213 | |
| 1214 | __mem_cgroup_commit_charge(mem, pc, ctype); |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
| 1218 | int mem_cgroup_newpage_charge(struct page *page, |
| 1219 | struct mm_struct *mm, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 1220 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1221 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 1222 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1223 | if (PageCompound(page)) |
| 1224 | return 0; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1225 | /* |
| 1226 | * If already mapped, we don't have to account. |
| 1227 | * If page cache, page->mapping has address_space. |
| 1228 | * But page->mapping may have out-of-use anon_vma pointer, |
| 1229 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping |
| 1230 | * is NULL. |
| 1231 | */ |
| 1232 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) |
| 1233 | return 0; |
| 1234 | if (unlikely(!mm)) |
| 1235 | mm = &init_mm; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 1236 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1237 | MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL); |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 1238 | } |
| 1239 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 1240 | static void |
| 1241 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 1242 | enum charge_type ctype); |
| 1243 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 1244 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 1245 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1246 | { |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1247 | struct mem_cgroup *mem = NULL; |
| 1248 | int ret; |
| 1249 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1250 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 1251 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1252 | if (PageCompound(page)) |
| 1253 | return 0; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 1254 | /* |
| 1255 | * Corner case handling. This is called from add_to_page_cache() |
| 1256 | * in usual. But some FS (shmem) precharges this page before calling it |
| 1257 | * and call add_to_page_cache() with GFP_NOWAIT. |
| 1258 | * |
| 1259 | * For GFP_NOWAIT case, the page may be pre-charged before calling |
| 1260 | * add_to_page_cache(). (See shmem.c) check it here and avoid to call |
| 1261 | * charge twice. (It works but has to pay a bit larger cost.) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1262 | * And when the page is SwapCache, it should take swap information |
| 1263 | * into account. This is under lock_page() now. |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 1264 | */ |
| 1265 | if (!(gfp_mask & __GFP_WAIT)) { |
| 1266 | struct page_cgroup *pc; |
| 1267 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1268 | |
| 1269 | pc = lookup_page_cgroup(page); |
| 1270 | if (!pc) |
| 1271 | return 0; |
| 1272 | lock_page_cgroup(pc); |
| 1273 | if (PageCgroupUsed(pc)) { |
| 1274 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 1275 | return 0; |
| 1276 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1277 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1280 | if (unlikely(!mm && !mem)) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1281 | mm = &init_mm; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 1282 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 1283 | if (page_is_file_cache(page)) |
| 1284 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1285 | MEM_CGROUP_CHARGE_TYPE_CACHE, NULL); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1286 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 1287 | /* shmem */ |
| 1288 | if (PageSwapCache(page)) { |
| 1289 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
| 1290 | if (!ret) |
| 1291 | __mem_cgroup_commit_charge_swapin(page, mem, |
| 1292 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
| 1293 | } else |
| 1294 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, |
| 1295 | MEM_CGROUP_CHARGE_TYPE_SHMEM, mem); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1296 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1297 | return ret; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1300 | /* |
| 1301 | * While swap-in, try_charge -> commit or cancel, the page is locked. |
| 1302 | * And when try_charge() successfully returns, one refcnt to memcg without |
| 1303 | * struct page_cgroup is aquired. This refcnt will be cumsumed by |
| 1304 | * "commit()" or removed by "cancel()" |
| 1305 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1306 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
| 1307 | struct page *page, |
| 1308 | gfp_t mask, struct mem_cgroup **ptr) |
| 1309 | { |
| 1310 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1311 | int ret; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1312 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1313 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1314 | return 0; |
| 1315 | |
| 1316 | if (!do_swap_account) |
| 1317 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1318 | /* |
| 1319 | * A racing thread's fault, or swapoff, may have already updated |
| 1320 | * the pte, and even removed page from swap cache: return success |
| 1321 | * to go on to do_swap_page()'s pte_same() test, which should fail. |
| 1322 | */ |
| 1323 | if (!PageSwapCache(page)) |
| 1324 | return 0; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1325 | mem = try_get_mem_cgroup_from_swapcache(page); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1326 | if (!mem) |
| 1327 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1328 | *ptr = mem; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1329 | ret = __mem_cgroup_try_charge(NULL, mask, ptr, true); |
| 1330 | /* drop extra refcnt from tryget */ |
| 1331 | css_put(&mem->css); |
| 1332 | return ret; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1333 | charge_cur_mm: |
| 1334 | if (unlikely(!mm)) |
| 1335 | mm = &init_mm; |
| 1336 | return __mem_cgroup_try_charge(mm, mask, ptr, true); |
| 1337 | } |
| 1338 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 1339 | static void |
| 1340 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 1341 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1342 | { |
| 1343 | struct page_cgroup *pc; |
| 1344 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1345 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1346 | return; |
| 1347 | if (!ptr) |
| 1348 | return; |
| 1349 | pc = lookup_page_cgroup(page); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1350 | mem_cgroup_lru_del_before_commit_swapcache(page); |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 1351 | __mem_cgroup_commit_charge(ptr, pc, ctype); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1352 | mem_cgroup_lru_add_after_commit_swapcache(page); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1353 | /* |
| 1354 | * Now swap is on-memory. This means this page may be |
| 1355 | * counted both as mem and swap....double count. |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 1356 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
| 1357 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() |
| 1358 | * may call delete_from_swap_cache() before reach here. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1359 | */ |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 1360 | if (do_swap_account && PageSwapCache(page)) { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1361 | swp_entry_t ent = {.val = page_private(page)}; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1362 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1363 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1364 | |
| 1365 | id = swap_cgroup_record(ent, 0); |
| 1366 | rcu_read_lock(); |
| 1367 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1368 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1369 | /* |
| 1370 | * This recorded memcg can be obsolete one. So, avoid |
| 1371 | * calling css_tryget |
| 1372 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1373 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
| 1374 | mem_cgroup_put(memcg); |
| 1375 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1376 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1377 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1378 | /* add this page(page_cgroup) to the LRU we want. */ |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1379 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1380 | } |
| 1381 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 1382 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
| 1383 | { |
| 1384 | __mem_cgroup_commit_charge_swapin(page, ptr, |
| 1385 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 1386 | } |
| 1387 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1388 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem) |
| 1389 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1390 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1391 | return; |
| 1392 | if (!mem) |
| 1393 | return; |
| 1394 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1395 | if (do_swap_account) |
| 1396 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1397 | css_put(&mem->css); |
| 1398 | } |
| 1399 | |
| 1400 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1401 | /* |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1402 | * uncharge if !page_mapped(page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1403 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1404 | static struct mem_cgroup * |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1405 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1406 | { |
Hugh Dickins | 8289546 | 2008-03-04 14:29:08 -0800 | [diff] [blame] | 1407 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1408 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 1409 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1410 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1411 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1412 | return NULL; |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 1413 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1414 | if (PageSwapCache(page)) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1415 | return NULL; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1416 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1417 | /* |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 1418 | * Check if our page_cgroup is valid |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1419 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1420 | pc = lookup_page_cgroup(page); |
| 1421 | if (unlikely(!pc || !PageCgroupUsed(pc))) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1422 | return NULL; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1423 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1424 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1425 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1426 | mem = pc->mem_cgroup; |
| 1427 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1428 | if (!PageCgroupUsed(pc)) |
| 1429 | goto unlock_out; |
| 1430 | |
| 1431 | switch (ctype) { |
| 1432 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
| 1433 | if (page_mapped(page)) |
| 1434 | goto unlock_out; |
| 1435 | break; |
| 1436 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: |
| 1437 | if (!PageAnon(page)) { /* Shared memory */ |
| 1438 | if (page->mapping && !page_is_file_cache(page)) |
| 1439 | goto unlock_out; |
| 1440 | } else if (page_mapped(page)) /* Anon */ |
| 1441 | goto unlock_out; |
| 1442 | break; |
| 1443 | default: |
| 1444 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1445 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1446 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1447 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 1448 | if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)) |
| 1449 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1450 | mem_cgroup_charge_statistics(mem, pc, false); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1451 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1452 | ClearPageCgroupUsed(pc); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1453 | /* |
| 1454 | * pc->mem_cgroup is not cleared here. It will be accessed when it's |
| 1455 | * freed from LRU. This is safe because uncharged page is expected not |
| 1456 | * to be reused (freed soon). Exception is SwapCache, it's handled by |
| 1457 | * special functions. |
| 1458 | */ |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 1459 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1460 | mz = page_cgroup_zoneinfo(pc); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1461 | unlock_page_cgroup(pc); |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 1462 | |
KAMEZAWA Hiroyuki | a7fe942 | 2009-01-07 18:08:13 -0800 | [diff] [blame] | 1463 | /* at swapout, this memcg will be accessed to record to swap */ |
| 1464 | if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT) |
| 1465 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1466 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1467 | return mem; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1468 | |
| 1469 | unlock_out: |
| 1470 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1471 | return NULL; |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 1472 | } |
| 1473 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1474 | void mem_cgroup_uncharge_page(struct page *page) |
| 1475 | { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1476 | /* early check. */ |
| 1477 | if (page_mapped(page)) |
| 1478 | return; |
| 1479 | if (page->mapping && !PageAnon(page)) |
| 1480 | return; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1481 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 1482 | } |
| 1483 | |
| 1484 | void mem_cgroup_uncharge_cache_page(struct page *page) |
| 1485 | { |
| 1486 | VM_BUG_ON(page_mapped(page)); |
KAMEZAWA Hiroyuki | b7abea9 | 2008-10-18 20:28:09 -0700 | [diff] [blame] | 1487 | VM_BUG_ON(page->mapping); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1488 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 1489 | } |
| 1490 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1491 | /* |
| 1492 | * called from __delete_from_swap_cache() and drop "page" account. |
| 1493 | * memcg information is recorded to swap_cgroup of "ent" |
| 1494 | */ |
| 1495 | void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent) |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1496 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1497 | struct mem_cgroup *memcg; |
| 1498 | |
| 1499 | memcg = __mem_cgroup_uncharge_common(page, |
| 1500 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT); |
| 1501 | /* record memcg information */ |
| 1502 | if (do_swap_account && memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1503 | swap_cgroup_record(ent, css_id(&memcg->css)); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1504 | mem_cgroup_get(memcg); |
| 1505 | } |
KAMEZAWA Hiroyuki | a7fe942 | 2009-01-07 18:08:13 -0800 | [diff] [blame] | 1506 | if (memcg) |
| 1507 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1508 | } |
| 1509 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1510 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 1511 | /* |
| 1512 | * called from swap_entry_free(). remove record in swap_cgroup and |
| 1513 | * uncharge "memsw" account. |
| 1514 | */ |
| 1515 | void mem_cgroup_uncharge_swap(swp_entry_t ent) |
| 1516 | { |
| 1517 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1518 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1519 | |
| 1520 | if (!do_swap_account) |
| 1521 | return; |
| 1522 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1523 | id = swap_cgroup_record(ent, 0); |
| 1524 | rcu_read_lock(); |
| 1525 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1526 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1527 | /* |
| 1528 | * We uncharge this because swap is freed. |
| 1529 | * This memcg can be obsolete one. We avoid calling css_tryget |
| 1530 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1531 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
| 1532 | mem_cgroup_put(memcg); |
| 1533 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1534 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1535 | } |
| 1536 | #endif |
| 1537 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 1538 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1539 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
| 1540 | * page belongs to. |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 1541 | */ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1542 | int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr) |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 1543 | { |
| 1544 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1545 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1546 | int ret = 0; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 1547 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1548 | if (mem_cgroup_disabled()) |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 1549 | return 0; |
| 1550 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1551 | pc = lookup_page_cgroup(page); |
| 1552 | lock_page_cgroup(pc); |
| 1553 | if (PageCgroupUsed(pc)) { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1554 | mem = pc->mem_cgroup; |
| 1555 | css_get(&mem->css); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 1556 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1557 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1558 | |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1559 | if (mem) { |
Daisuke Nishimura | 3bb4edf | 2009-01-07 18:08:28 -0800 | [diff] [blame] | 1560 | ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1561 | css_put(&mem->css); |
| 1562 | } |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1563 | *ptr = mem; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1564 | return ret; |
| 1565 | } |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 1566 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1567 | /* remove redundant charge if migration failed*/ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1568 | void mem_cgroup_end_migration(struct mem_cgroup *mem, |
| 1569 | struct page *oldpage, struct page *newpage) |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1570 | { |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1571 | struct page *target, *unused; |
| 1572 | struct page_cgroup *pc; |
| 1573 | enum charge_type ctype; |
| 1574 | |
| 1575 | if (!mem) |
| 1576 | return; |
| 1577 | |
| 1578 | /* at migration success, oldpage->mapping is NULL. */ |
| 1579 | if (oldpage->mapping) { |
| 1580 | target = oldpage; |
| 1581 | unused = NULL; |
| 1582 | } else { |
| 1583 | target = newpage; |
| 1584 | unused = oldpage; |
| 1585 | } |
| 1586 | |
| 1587 | if (PageAnon(target)) |
| 1588 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; |
| 1589 | else if (page_is_file_cache(target)) |
| 1590 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; |
| 1591 | else |
| 1592 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; |
| 1593 | |
| 1594 | /* unused page is not on radix-tree now. */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1595 | if (unused) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1596 | __mem_cgroup_uncharge_common(unused, ctype); |
| 1597 | |
| 1598 | pc = lookup_page_cgroup(target); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1599 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1600 | * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup. |
| 1601 | * So, double-counting is effectively avoided. |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1602 | */ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1603 | __mem_cgroup_commit_charge(mem, pc, ctype); |
| 1604 | |
| 1605 | /* |
| 1606 | * Both of oldpage and newpage are still under lock_page(). |
| 1607 | * Then, we don't have to care about race in radix-tree. |
| 1608 | * But we have to be careful that this page is unmapped or not. |
| 1609 | * |
| 1610 | * There is a case for !page_mapped(). At the start of |
| 1611 | * migration, oldpage was mapped. But now, it's zapped. |
| 1612 | * But we know *target* page is not freed/reused under us. |
| 1613 | * mem_cgroup_uncharge_page() does all necessary checks. |
| 1614 | */ |
| 1615 | if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED) |
| 1616 | mem_cgroup_uncharge_page(target); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 1617 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1618 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1619 | /* |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 1620 | * A call to try to shrink memory usage on charge failure at shmem's swapin. |
| 1621 | * Calling hierarchical_reclaim is not enough because we should update |
| 1622 | * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM. |
| 1623 | * Moreover considering hierarchy, we should reclaim from the mem_over_limit, |
| 1624 | * not from the memcg which this page would be charged to. |
| 1625 | * try_charge_swapin does all of these works properly. |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1626 | */ |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 1627 | int mem_cgroup_shmem_charge_fallback(struct page *page, |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1628 | struct mm_struct *mm, |
| 1629 | gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1630 | { |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1631 | struct mem_cgroup *mem = NULL; |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 1632 | int ret; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1633 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1634 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 1635 | return 0; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1636 | |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 1637 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
| 1638 | if (!ret) |
| 1639 | mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */ |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1640 | |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 1641 | return ret; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1644 | static DEFINE_MUTEX(set_limit_mutex); |
| 1645 | |
KOSAKI Motohiro | d38d2a7 | 2009-01-06 14:39:44 -0800 | [diff] [blame] | 1646 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1647 | unsigned long long val) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1648 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1649 | int retry_count; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1650 | int progress; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1651 | u64 memswlimit; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1652 | int ret = 0; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1653 | int children = mem_cgroup_count_children(memcg); |
| 1654 | u64 curusage, oldusage; |
| 1655 | |
| 1656 | /* |
| 1657 | * For keeping hierarchical_reclaim simple, how long we should retry |
| 1658 | * is depends on callers. We set our retry-count to be function |
| 1659 | * of # of children which we should visit in this loop. |
| 1660 | */ |
| 1661 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; |
| 1662 | |
| 1663 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1664 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1665 | while (retry_count) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1666 | if (signal_pending(current)) { |
| 1667 | ret = -EINTR; |
| 1668 | break; |
| 1669 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1670 | /* |
| 1671 | * Rather than hide all in some function, I do this in |
| 1672 | * open coded manner. You see what this really does. |
| 1673 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 1674 | */ |
| 1675 | mutex_lock(&set_limit_mutex); |
| 1676 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 1677 | if (memswlimit < val) { |
| 1678 | ret = -EINVAL; |
| 1679 | mutex_unlock(&set_limit_mutex); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1680 | break; |
| 1681 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1682 | ret = res_counter_set_limit(&memcg->res, val); |
| 1683 | mutex_unlock(&set_limit_mutex); |
| 1684 | |
| 1685 | if (!ret) |
| 1686 | break; |
| 1687 | |
Daisuke Nishimura | 42e9abb | 2009-01-07 18:08:30 -0800 | [diff] [blame] | 1688 | progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1689 | false, true); |
| 1690 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
| 1691 | /* Usage is reduced ? */ |
| 1692 | if (curusage >= oldusage) |
| 1693 | retry_count--; |
| 1694 | else |
| 1695 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1696 | } |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1697 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1698 | return ret; |
| 1699 | } |
| 1700 | |
| 1701 | int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
| 1702 | unsigned long long val) |
| 1703 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1704 | int retry_count; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1705 | u64 memlimit, oldusage, curusage; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1706 | int children = mem_cgroup_count_children(memcg); |
| 1707 | int ret = -EBUSY; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1708 | |
| 1709 | if (!do_swap_account) |
| 1710 | return -EINVAL; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1711 | /* see mem_cgroup_resize_res_limit */ |
| 1712 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; |
| 1713 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1714 | while (retry_count) { |
| 1715 | if (signal_pending(current)) { |
| 1716 | ret = -EINTR; |
| 1717 | break; |
| 1718 | } |
| 1719 | /* |
| 1720 | * Rather than hide all in some function, I do this in |
| 1721 | * open coded manner. You see what this really does. |
| 1722 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 1723 | */ |
| 1724 | mutex_lock(&set_limit_mutex); |
| 1725 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 1726 | if (memlimit > val) { |
| 1727 | ret = -EINVAL; |
| 1728 | mutex_unlock(&set_limit_mutex); |
| 1729 | break; |
| 1730 | } |
| 1731 | ret = res_counter_set_limit(&memcg->memsw, val); |
| 1732 | mutex_unlock(&set_limit_mutex); |
| 1733 | |
| 1734 | if (!ret) |
| 1735 | break; |
| 1736 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1737 | mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1738 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1739 | /* Usage is reduced ? */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1740 | if (curusage >= oldusage) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1741 | retry_count--; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1742 | else |
| 1743 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1744 | } |
| 1745 | return ret; |
| 1746 | } |
| 1747 | |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1748 | /* |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1749 | * This routine traverse page_cgroup in given list and drop them all. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1750 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
| 1751 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1752 | static int mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1753 | int node, int zid, enum lru_list lru) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1754 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1755 | struct zone *zone; |
| 1756 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1757 | struct page_cgroup *pc, *busy; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1758 | unsigned long flags, loop; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 1759 | struct list_head *list; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1760 | int ret = 0; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 1761 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1762 | zone = &NODE_DATA(node)->node_zones[zid]; |
| 1763 | mz = mem_cgroup_zoneinfo(mem, node, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1764 | list = &mz->lists[lru]; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1765 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1766 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
| 1767 | /* give some margin against EBUSY etc...*/ |
| 1768 | loop += 256; |
| 1769 | busy = NULL; |
| 1770 | while (loop--) { |
| 1771 | ret = 0; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1772 | spin_lock_irqsave(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1773 | if (list_empty(list)) { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1774 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1775 | break; |
| 1776 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1777 | pc = list_entry(list->prev, struct page_cgroup, lru); |
| 1778 | if (busy == pc) { |
| 1779 | list_move(&pc->lru, list); |
| 1780 | busy = 0; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1781 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1782 | continue; |
| 1783 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1784 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1785 | |
KAMEZAWA Hiroyuki | 2c26fdd | 2009-01-07 18:08:10 -0800 | [diff] [blame] | 1786 | ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1787 | if (ret == -ENOMEM) |
| 1788 | break; |
| 1789 | |
| 1790 | if (ret == -EBUSY || ret == -EINVAL) { |
| 1791 | /* found lock contention or "pc" is obsolete. */ |
| 1792 | busy = pc; |
| 1793 | cond_resched(); |
| 1794 | } else |
| 1795 | busy = NULL; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1796 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1797 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1798 | if (!ret && !list_empty(list)) |
| 1799 | return -EBUSY; |
| 1800 | return ret; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | /* |
| 1804 | * make mem_cgroup's charge to be 0 if there is no task. |
| 1805 | * This enables deleting this mem_cgroup. |
| 1806 | */ |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1807 | 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] | 1808 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1809 | int ret; |
| 1810 | int node, zid, shrink; |
| 1811 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1812 | struct cgroup *cgrp = mem->css.cgroup; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 1813 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1814 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1815 | |
| 1816 | shrink = 0; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1817 | /* should free all ? */ |
| 1818 | if (free_all) |
| 1819 | goto try_to_free; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1820 | move_account: |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1821 | while (mem->res.usage > 0) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1822 | ret = -EBUSY; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1823 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1824 | goto out; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1825 | ret = -EINTR; |
| 1826 | if (signal_pending(current)) |
| 1827 | goto out; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1828 | /* This is for making all *used* pages to be on LRU. */ |
| 1829 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1830 | ret = 0; |
KAMEZAWA Hiroyuki | 299b4ea | 2009-01-29 14:25:17 -0800 | [diff] [blame] | 1831 | for_each_node_state(node, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1832 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1833 | enum lru_list l; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1834 | for_each_lru(l) { |
| 1835 | ret = mem_cgroup_force_empty_list(mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1836 | node, zid, l); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1837 | if (ret) |
| 1838 | break; |
| 1839 | } |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1840 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1841 | if (ret) |
| 1842 | break; |
| 1843 | } |
| 1844 | /* it seems parent cgroup doesn't have enough mem */ |
| 1845 | if (ret == -ENOMEM) |
| 1846 | goto try_to_free; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1847 | cond_resched(); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1848 | } |
| 1849 | ret = 0; |
| 1850 | out: |
| 1851 | css_put(&mem->css); |
| 1852 | return ret; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1853 | |
| 1854 | try_to_free: |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1855 | /* returns EBUSY if there is a task or if we come here twice. */ |
| 1856 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1857 | ret = -EBUSY; |
| 1858 | goto out; |
| 1859 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1860 | /* we call try-to-free pages for make this cgroup empty */ |
| 1861 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1862 | /* try to free all pages in this cgroup */ |
| 1863 | shrink = 1; |
| 1864 | while (nr_retries && mem->res.usage > 0) { |
| 1865 | int progress; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1866 | |
| 1867 | if (signal_pending(current)) { |
| 1868 | ret = -EINTR; |
| 1869 | goto out; |
| 1870 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1871 | progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL, |
| 1872 | false, get_swappiness(mem)); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1873 | if (!progress) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1874 | nr_retries--; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1875 | /* maybe some writeback is necessary */ |
| 1876 | congestion_wait(WRITE, HZ/10); |
| 1877 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1878 | |
| 1879 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1880 | lru_add_drain(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1881 | /* try move_account...there may be some *locked* pages. */ |
| 1882 | if (mem->res.usage) |
| 1883 | goto move_account; |
| 1884 | ret = 0; |
| 1885 | goto out; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1886 | } |
| 1887 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1888 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
| 1889 | { |
| 1890 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); |
| 1891 | } |
| 1892 | |
| 1893 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 1894 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
| 1895 | { |
| 1896 | return mem_cgroup_from_cont(cont)->use_hierarchy; |
| 1897 | } |
| 1898 | |
| 1899 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, |
| 1900 | u64 val) |
| 1901 | { |
| 1902 | int retval = 0; |
| 1903 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 1904 | struct cgroup *parent = cont->parent; |
| 1905 | struct mem_cgroup *parent_mem = NULL; |
| 1906 | |
| 1907 | if (parent) |
| 1908 | parent_mem = mem_cgroup_from_cont(parent); |
| 1909 | |
| 1910 | cgroup_lock(); |
| 1911 | /* |
| 1912 | * If parent's use_hiearchy is set, we can't make any modifications |
| 1913 | * in the child subtrees. If it is unset, then the change can |
| 1914 | * occur, provided the current cgroup has no children. |
| 1915 | * |
| 1916 | * For the root cgroup, parent_mem is NULL, we allow value to be |
| 1917 | * set if there are no children. |
| 1918 | */ |
| 1919 | if ((!parent_mem || !parent_mem->use_hierarchy) && |
| 1920 | (val == 1 || val == 0)) { |
| 1921 | if (list_empty(&cont->children)) |
| 1922 | mem->use_hierarchy = val; |
| 1923 | else |
| 1924 | retval = -EBUSY; |
| 1925 | } else |
| 1926 | retval = -EINVAL; |
| 1927 | cgroup_unlock(); |
| 1928 | |
| 1929 | return retval; |
| 1930 | } |
| 1931 | |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 1932 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1933 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1934 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 1935 | u64 val = 0; |
| 1936 | int type, name; |
| 1937 | |
| 1938 | type = MEMFILE_TYPE(cft->private); |
| 1939 | name = MEMFILE_ATTR(cft->private); |
| 1940 | switch (type) { |
| 1941 | case _MEM: |
| 1942 | val = res_counter_read_u64(&mem->res, name); |
| 1943 | break; |
| 1944 | case _MEMSWAP: |
| 1945 | if (do_swap_account) |
| 1946 | val = res_counter_read_u64(&mem->memsw, name); |
| 1947 | break; |
| 1948 | default: |
| 1949 | BUG(); |
| 1950 | break; |
| 1951 | } |
| 1952 | return val; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1953 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1954 | /* |
| 1955 | * The user of this function is... |
| 1956 | * RES_LIMIT. |
| 1957 | */ |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 1958 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 1959 | const char *buffer) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1960 | { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1961 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1962 | int type, name; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1963 | unsigned long long val; |
| 1964 | int ret; |
| 1965 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1966 | type = MEMFILE_TYPE(cft->private); |
| 1967 | name = MEMFILE_ATTR(cft->private); |
| 1968 | switch (name) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1969 | case RES_LIMIT: |
| 1970 | /* This function does all necessary parse...reuse it */ |
| 1971 | ret = res_counter_memparse_write_strategy(buffer, &val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1972 | if (ret) |
| 1973 | break; |
| 1974 | if (type == _MEM) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1975 | ret = mem_cgroup_resize_limit(memcg, val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1976 | else |
| 1977 | ret = mem_cgroup_resize_memsw_limit(memcg, val); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1978 | break; |
| 1979 | default: |
| 1980 | ret = -EINVAL; /* should be BUG() ? */ |
| 1981 | break; |
| 1982 | } |
| 1983 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1984 | } |
| 1985 | |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 1986 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
| 1987 | unsigned long long *mem_limit, unsigned long long *memsw_limit) |
| 1988 | { |
| 1989 | struct cgroup *cgroup; |
| 1990 | unsigned long long min_limit, min_memsw_limit, tmp; |
| 1991 | |
| 1992 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 1993 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 1994 | cgroup = memcg->css.cgroup; |
| 1995 | if (!memcg->use_hierarchy) |
| 1996 | goto out; |
| 1997 | |
| 1998 | while (cgroup->parent) { |
| 1999 | cgroup = cgroup->parent; |
| 2000 | memcg = mem_cgroup_from_cont(cgroup); |
| 2001 | if (!memcg->use_hierarchy) |
| 2002 | break; |
| 2003 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 2004 | min_limit = min(min_limit, tmp); |
| 2005 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 2006 | min_memsw_limit = min(min_memsw_limit, tmp); |
| 2007 | } |
| 2008 | out: |
| 2009 | *mem_limit = min_limit; |
| 2010 | *memsw_limit = min_memsw_limit; |
| 2011 | return; |
| 2012 | } |
| 2013 | |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 2014 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 2015 | { |
| 2016 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2017 | int type, name; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 2018 | |
| 2019 | mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2020 | type = MEMFILE_TYPE(event); |
| 2021 | name = MEMFILE_ATTR(event); |
| 2022 | switch (name) { |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 2023 | case RES_MAX_USAGE: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2024 | if (type == _MEM) |
| 2025 | res_counter_reset_max(&mem->res); |
| 2026 | else |
| 2027 | res_counter_reset_max(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 2028 | break; |
| 2029 | case RES_FAILCNT: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2030 | if (type == _MEM) |
| 2031 | res_counter_reset_failcnt(&mem->res); |
| 2032 | else |
| 2033 | res_counter_reset_failcnt(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 2034 | break; |
| 2035 | } |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 2036 | return 0; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 2037 | } |
| 2038 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2039 | |
| 2040 | /* For read statistics */ |
| 2041 | enum { |
| 2042 | MCS_CACHE, |
| 2043 | MCS_RSS, |
| 2044 | MCS_PGPGIN, |
| 2045 | MCS_PGPGOUT, |
| 2046 | MCS_INACTIVE_ANON, |
| 2047 | MCS_ACTIVE_ANON, |
| 2048 | MCS_INACTIVE_FILE, |
| 2049 | MCS_ACTIVE_FILE, |
| 2050 | MCS_UNEVICTABLE, |
| 2051 | NR_MCS_STAT, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2052 | }; |
| 2053 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2054 | struct mcs_total_stat { |
| 2055 | s64 stat[NR_MCS_STAT]; |
| 2056 | }; |
| 2057 | |
| 2058 | struct { |
| 2059 | char *local_name; |
| 2060 | char *total_name; |
| 2061 | } memcg_stat_strings[NR_MCS_STAT] = { |
| 2062 | {"cache", "total_cache"}, |
| 2063 | {"rss", "total_rss"}, |
| 2064 | {"pgpgin", "total_pgpgin"}, |
| 2065 | {"pgpgout", "total_pgpgout"}, |
| 2066 | {"inactive_anon", "total_inactive_anon"}, |
| 2067 | {"active_anon", "total_active_anon"}, |
| 2068 | {"inactive_file", "total_inactive_file"}, |
| 2069 | {"active_file", "total_active_file"}, |
| 2070 | {"unevictable", "total_unevictable"} |
| 2071 | }; |
| 2072 | |
| 2073 | |
| 2074 | static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data) |
| 2075 | { |
| 2076 | struct mcs_total_stat *s = data; |
| 2077 | s64 val; |
| 2078 | |
| 2079 | /* per cpu stat */ |
| 2080 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE); |
| 2081 | s->stat[MCS_CACHE] += val * PAGE_SIZE; |
| 2082 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS); |
| 2083 | s->stat[MCS_RSS] += val * PAGE_SIZE; |
| 2084 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT); |
| 2085 | s->stat[MCS_PGPGIN] += val; |
| 2086 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT); |
| 2087 | s->stat[MCS_PGPGOUT] += val; |
| 2088 | |
| 2089 | /* per zone stat */ |
| 2090 | val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON); |
| 2091 | s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE; |
| 2092 | val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON); |
| 2093 | s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE; |
| 2094 | val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE); |
| 2095 | s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE; |
| 2096 | val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE); |
| 2097 | s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE; |
| 2098 | val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE); |
| 2099 | s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE; |
| 2100 | return 0; |
| 2101 | } |
| 2102 | |
| 2103 | static void |
| 2104 | mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s) |
| 2105 | { |
| 2106 | mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat); |
| 2107 | } |
| 2108 | |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 2109 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
| 2110 | struct cgroup_map_cb *cb) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2111 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2112 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2113 | struct mcs_total_stat mystat; |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2114 | int i; |
| 2115 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2116 | memset(&mystat, 0, sizeof(mystat)); |
| 2117 | mem_cgroup_get_local_stat(mem_cont, &mystat); |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2118 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2119 | for (i = 0; i < NR_MCS_STAT; i++) |
| 2120 | cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2121 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2122 | /* Hierarchical information */ |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 2123 | { |
| 2124 | unsigned long long limit, memsw_limit; |
| 2125 | memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit); |
| 2126 | cb->fill(cb, "hierarchical_memory_limit", limit); |
| 2127 | if (do_swap_account) |
| 2128 | cb->fill(cb, "hierarchical_memsw_limit", memsw_limit); |
| 2129 | } |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 2130 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2131 | memset(&mystat, 0, sizeof(mystat)); |
| 2132 | mem_cgroup_get_total_stat(mem_cont, &mystat); |
| 2133 | for (i = 0; i < NR_MCS_STAT; i++) |
| 2134 | cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]); |
| 2135 | |
| 2136 | |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 2137 | #ifdef CONFIG_DEBUG_VM |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 2138 | cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL)); |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 2139 | |
| 2140 | { |
| 2141 | int nid, zid; |
| 2142 | struct mem_cgroup_per_zone *mz; |
| 2143 | unsigned long recent_rotated[2] = {0, 0}; |
| 2144 | unsigned long recent_scanned[2] = {0, 0}; |
| 2145 | |
| 2146 | for_each_online_node(nid) |
| 2147 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 2148 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
| 2149 | |
| 2150 | recent_rotated[0] += |
| 2151 | mz->reclaim_stat.recent_rotated[0]; |
| 2152 | recent_rotated[1] += |
| 2153 | mz->reclaim_stat.recent_rotated[1]; |
| 2154 | recent_scanned[0] += |
| 2155 | mz->reclaim_stat.recent_scanned[0]; |
| 2156 | recent_scanned[1] += |
| 2157 | mz->reclaim_stat.recent_scanned[1]; |
| 2158 | } |
| 2159 | cb->fill(cb, "recent_rotated_anon", recent_rotated[0]); |
| 2160 | cb->fill(cb, "recent_rotated_file", recent_rotated[1]); |
| 2161 | cb->fill(cb, "recent_scanned_anon", recent_scanned[0]); |
| 2162 | cb->fill(cb, "recent_scanned_file", recent_scanned[1]); |
| 2163 | } |
| 2164 | #endif |
| 2165 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2166 | return 0; |
| 2167 | } |
| 2168 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2169 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
| 2170 | { |
| 2171 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 2172 | |
| 2173 | return get_swappiness(memcg); |
| 2174 | } |
| 2175 | |
| 2176 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, |
| 2177 | u64 val) |
| 2178 | { |
| 2179 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 2180 | struct mem_cgroup *parent; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 2181 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2182 | if (val > 100) |
| 2183 | return -EINVAL; |
| 2184 | |
| 2185 | if (cgrp->parent == NULL) |
| 2186 | return -EINVAL; |
| 2187 | |
| 2188 | parent = mem_cgroup_from_cont(cgrp->parent); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 2189 | |
| 2190 | cgroup_lock(); |
| 2191 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2192 | /* If under hierarchy, only empty-root can set this value */ |
| 2193 | if ((parent->use_hierarchy) || |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 2194 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
| 2195 | cgroup_unlock(); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2196 | return -EINVAL; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 2197 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2198 | |
| 2199 | spin_lock(&memcg->reclaim_param_lock); |
| 2200 | memcg->swappiness = val; |
| 2201 | spin_unlock(&memcg->reclaim_param_lock); |
| 2202 | |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 2203 | cgroup_unlock(); |
| 2204 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2205 | return 0; |
| 2206 | } |
| 2207 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2208 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2209 | static struct cftype mem_cgroup_files[] = { |
| 2210 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 2211 | .name = "usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2212 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 2213 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2214 | }, |
| 2215 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 2216 | .name = "max_usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2217 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 2218 | .trigger = mem_cgroup_reset, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 2219 | .read_u64 = mem_cgroup_read, |
| 2220 | }, |
| 2221 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 2222 | .name = "limit_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2223 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 2224 | .write_string = mem_cgroup_write, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 2225 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2226 | }, |
| 2227 | { |
| 2228 | .name = "failcnt", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2229 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 2230 | .trigger = mem_cgroup_reset, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 2231 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2232 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2233 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2234 | .name = "stat", |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 2235 | .read_map = mem_control_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2236 | }, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2237 | { |
| 2238 | .name = "force_empty", |
| 2239 | .trigger = mem_cgroup_force_empty_write, |
| 2240 | }, |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 2241 | { |
| 2242 | .name = "use_hierarchy", |
| 2243 | .write_u64 = mem_cgroup_hierarchy_write, |
| 2244 | .read_u64 = mem_cgroup_hierarchy_read, |
| 2245 | }, |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2246 | { |
| 2247 | .name = "swappiness", |
| 2248 | .read_u64 = mem_cgroup_swappiness_read, |
| 2249 | .write_u64 = mem_cgroup_swappiness_write, |
| 2250 | }, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2251 | }; |
| 2252 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2253 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 2254 | static struct cftype memsw_cgroup_files[] = { |
| 2255 | { |
| 2256 | .name = "memsw.usage_in_bytes", |
| 2257 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), |
| 2258 | .read_u64 = mem_cgroup_read, |
| 2259 | }, |
| 2260 | { |
| 2261 | .name = "memsw.max_usage_in_bytes", |
| 2262 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), |
| 2263 | .trigger = mem_cgroup_reset, |
| 2264 | .read_u64 = mem_cgroup_read, |
| 2265 | }, |
| 2266 | { |
| 2267 | .name = "memsw.limit_in_bytes", |
| 2268 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), |
| 2269 | .write_string = mem_cgroup_write, |
| 2270 | .read_u64 = mem_cgroup_read, |
| 2271 | }, |
| 2272 | { |
| 2273 | .name = "memsw.failcnt", |
| 2274 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), |
| 2275 | .trigger = mem_cgroup_reset, |
| 2276 | .read_u64 = mem_cgroup_read, |
| 2277 | }, |
| 2278 | }; |
| 2279 | |
| 2280 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 2281 | { |
| 2282 | if (!do_swap_account) |
| 2283 | return 0; |
| 2284 | return cgroup_add_files(cont, ss, memsw_cgroup_files, |
| 2285 | ARRAY_SIZE(memsw_cgroup_files)); |
| 2286 | }; |
| 2287 | #else |
| 2288 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 2289 | { |
| 2290 | return 0; |
| 2291 | } |
| 2292 | #endif |
| 2293 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2294 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 2295 | { |
| 2296 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 2297 | struct mem_cgroup_per_zone *mz; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 2298 | enum lru_list l; |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 2299 | int zone, tmp = node; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 2300 | /* |
| 2301 | * This routine is called against possible nodes. |
| 2302 | * But it's BUG to call kmalloc() against offline node. |
| 2303 | * |
| 2304 | * TODO: this routine can waste much memory for nodes which will |
| 2305 | * never be onlined. It's better to use memory hotplug callback |
| 2306 | * function. |
| 2307 | */ |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 2308 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 2309 | tmp = -1; |
| 2310 | pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2311 | if (!pn) |
| 2312 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 2313 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2314 | mem->info.nodeinfo[node] = pn; |
| 2315 | memset(pn, 0, sizeof(*pn)); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 2316 | |
| 2317 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 2318 | mz = &pn->zoneinfo[zone]; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 2319 | for_each_lru(l) |
| 2320 | INIT_LIST_HEAD(&mz->lists[l]); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 2321 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2322 | return 0; |
| 2323 | } |
| 2324 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 2325 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 2326 | { |
| 2327 | kfree(mem->info.nodeinfo[node]); |
| 2328 | } |
| 2329 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2330 | static int mem_cgroup_size(void) |
| 2331 | { |
| 2332 | int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu); |
| 2333 | return sizeof(struct mem_cgroup) + cpustat_size; |
| 2334 | } |
| 2335 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2336 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 2337 | { |
| 2338 | struct mem_cgroup *mem; |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2339 | int size = mem_cgroup_size(); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2340 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2341 | if (size < PAGE_SIZE) |
| 2342 | mem = kmalloc(size, GFP_KERNEL); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2343 | else |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2344 | mem = vmalloc(size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2345 | |
| 2346 | if (mem) |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2347 | memset(mem, 0, size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2348 | return mem; |
| 2349 | } |
| 2350 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2351 | /* |
| 2352 | * At destroying mem_cgroup, references from swap_cgroup can remain. |
| 2353 | * (scanning all at force_empty is too costly...) |
| 2354 | * |
| 2355 | * Instead of clearing all references at force_empty, we remember |
| 2356 | * the number of reference from swap_cgroup and free mem_cgroup when |
| 2357 | * it goes down to 0. |
| 2358 | * |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2359 | * Removal of cgroup itself succeeds regardless of refs from swap. |
| 2360 | */ |
| 2361 | |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 2362 | static void __mem_cgroup_free(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2363 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2364 | int node; |
| 2365 | |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 2366 | free_css_id(&mem_cgroup_subsys, &mem->css); |
| 2367 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2368 | for_each_node_state(node, N_POSSIBLE) |
| 2369 | free_mem_cgroup_per_zone_info(mem, node); |
| 2370 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2371 | if (mem_cgroup_size() < PAGE_SIZE) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2372 | kfree(mem); |
| 2373 | else |
| 2374 | vfree(mem); |
| 2375 | } |
| 2376 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2377 | static void mem_cgroup_get(struct mem_cgroup *mem) |
| 2378 | { |
| 2379 | atomic_inc(&mem->refcnt); |
| 2380 | } |
| 2381 | |
| 2382 | static void mem_cgroup_put(struct mem_cgroup *mem) |
| 2383 | { |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 2384 | if (atomic_dec_and_test(&mem->refcnt)) { |
| 2385 | struct mem_cgroup *parent = parent_mem_cgroup(mem); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 2386 | __mem_cgroup_free(mem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 2387 | if (parent) |
| 2388 | mem_cgroup_put(parent); |
| 2389 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2390 | } |
| 2391 | |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 2392 | /* |
| 2393 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. |
| 2394 | */ |
| 2395 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem) |
| 2396 | { |
| 2397 | if (!mem->res.parent) |
| 2398 | return NULL; |
| 2399 | return mem_cgroup_from_res_counter(mem->res.parent, res); |
| 2400 | } |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2401 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 2402 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 2403 | static void __init enable_swap_cgroup(void) |
| 2404 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2405 | if (!mem_cgroup_disabled() && really_do_swap_account) |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 2406 | do_swap_account = 1; |
| 2407 | } |
| 2408 | #else |
| 2409 | static void __init enable_swap_cgroup(void) |
| 2410 | { |
| 2411 | } |
| 2412 | #endif |
| 2413 | |
Li Zefan | 0eb253e | 2009-01-15 13:51:25 -0800 | [diff] [blame] | 2414 | static struct cgroup_subsys_state * __ref |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2415 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 2416 | { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 2417 | struct mem_cgroup *mem, *parent; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 2418 | long error = -ENOMEM; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2419 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2420 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2421 | mem = mem_cgroup_alloc(); |
| 2422 | if (!mem) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 2423 | return ERR_PTR(error); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 2424 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2425 | for_each_node_state(node, N_POSSIBLE) |
| 2426 | if (alloc_mem_cgroup_per_zone_info(mem, node)) |
| 2427 | goto free_out; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 2428 | /* root ? */ |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 2429 | if (cont->parent == NULL) { |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 2430 | enable_swap_cgroup(); |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 2431 | parent = NULL; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 2432 | } else { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 2433 | parent = mem_cgroup_from_cont(cont->parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 2434 | mem->use_hierarchy = parent->use_hierarchy; |
| 2435 | } |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 2436 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 2437 | if (parent && parent->use_hierarchy) { |
| 2438 | res_counter_init(&mem->res, &parent->res); |
| 2439 | res_counter_init(&mem->memsw, &parent->memsw); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 2440 | /* |
| 2441 | * We increment refcnt of the parent to ensure that we can |
| 2442 | * safely access it on res_counter_charge/uncharge. |
| 2443 | * This refcnt will be decremented when freeing this |
| 2444 | * mem_cgroup(see mem_cgroup_put). |
| 2445 | */ |
| 2446 | mem_cgroup_get(parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 2447 | } else { |
| 2448 | res_counter_init(&mem->res, NULL); |
| 2449 | res_counter_init(&mem->memsw, NULL); |
| 2450 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 2451 | mem->last_scanned_child = 0; |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 2452 | spin_lock_init(&mem->reclaim_param_lock); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 2453 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2454 | if (parent) |
| 2455 | mem->swappiness = get_swappiness(parent); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 2456 | atomic_set(&mem->refcnt, 1); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2457 | return &mem->css; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2458 | free_out: |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 2459 | __mem_cgroup_free(mem); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 2460 | return ERR_PTR(error); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2461 | } |
| 2462 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 2463 | static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 2464 | struct cgroup *cont) |
| 2465 | { |
| 2466 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 2467 | |
| 2468 | return mem_cgroup_force_empty(mem, false); |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 2469 | } |
| 2470 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2471 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 2472 | struct cgroup *cont) |
| 2473 | { |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 2474 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 2475 | |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 2476 | mem_cgroup_put(mem); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 2480 | struct cgroup *cont) |
| 2481 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2482 | int ret; |
| 2483 | |
| 2484 | ret = cgroup_add_files(cont, ss, mem_cgroup_files, |
| 2485 | ARRAY_SIZE(mem_cgroup_files)); |
| 2486 | |
| 2487 | if (!ret) |
| 2488 | ret = register_memsw_files(cont, ss); |
| 2489 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2490 | } |
| 2491 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 2492 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 2493 | struct cgroup *cont, |
| 2494 | struct cgroup *old_cont, |
| 2495 | struct task_struct *p) |
| 2496 | { |
Daisuke Nishimura | 7f4d454 | 2009-01-07 18:08:29 -0800 | [diff] [blame] | 2497 | mutex_lock(&memcg_tasklist); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 2498 | /* |
Nikanth Karthikesan | f9717d2 | 2009-01-07 18:08:11 -0800 | [diff] [blame] | 2499 | * FIXME: It's better to move charges of this process from old |
| 2500 | * memcg to new memcg. But it's just on TODO-List now. |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 2501 | */ |
Daisuke Nishimura | 7f4d454 | 2009-01-07 18:08:29 -0800 | [diff] [blame] | 2502 | mutex_unlock(&memcg_tasklist); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 2503 | } |
| 2504 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2505 | struct cgroup_subsys mem_cgroup_subsys = { |
| 2506 | .name = "memory", |
| 2507 | .subsys_id = mem_cgroup_subsys_id, |
| 2508 | .create = mem_cgroup_create, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 2509 | .pre_destroy = mem_cgroup_pre_destroy, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2510 | .destroy = mem_cgroup_destroy, |
| 2511 | .populate = mem_cgroup_populate, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 2512 | .attach = mem_cgroup_move_task, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2513 | .early_init = 0, |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 2514 | .use_id = 1, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2515 | }; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 2516 | |
| 2517 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 2518 | |
| 2519 | static int __init disable_swap_account(char *s) |
| 2520 | { |
| 2521 | really_do_swap_account = 0; |
| 2522 | return 1; |
| 2523 | } |
| 2524 | __setup("noswapaccount", disable_swap_account); |
| 2525 | #endif |