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