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