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