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