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