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