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