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 | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 50 | #include <linux/oom.h> |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 51 | #include "internal.h" |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 52 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 53 | #include <asm/uaccess.h> |
| 54 | |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 55 | #include <trace/events/vmscan.h> |
| 56 | |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 57 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 58 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 59 | struct mem_cgroup *root_mem_cgroup __read_mostly; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 60 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 61 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 62 | /* 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] | 63 | int do_swap_account __read_mostly; |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 64 | |
| 65 | /* for remember boot option*/ |
| 66 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP_ENABLED |
| 67 | static int really_do_swap_account __initdata = 1; |
| 68 | #else |
| 69 | static int really_do_swap_account __initdata = 0; |
| 70 | #endif |
| 71 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 72 | #else |
| 73 | #define do_swap_account (0) |
| 74 | #endif |
| 75 | |
| 76 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 77 | /* |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 78 | * Statistics for memory cgroup. |
| 79 | */ |
| 80 | enum mem_cgroup_stat_index { |
| 81 | /* |
| 82 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. |
| 83 | */ |
| 84 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 85 | MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */ |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 86 | MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 87 | MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */ |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 88 | MEM_CGROUP_STAT_DATA, /* end of data requires synchronization */ |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 89 | MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 90 | MEM_CGROUP_STAT_NSTATS, |
| 91 | }; |
| 92 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 93 | enum mem_cgroup_events_index { |
| 94 | MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */ |
| 95 | MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */ |
| 96 | MEM_CGROUP_EVENTS_COUNT, /* # of pages paged in/out */ |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 97 | MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */ |
| 98 | MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */ |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 99 | MEM_CGROUP_EVENTS_NSTATS, |
| 100 | }; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 101 | /* |
| 102 | * Per memcg event counter is incremented at every pagein/pageout. With THP, |
| 103 | * it will be incremated by the number of pages. This counter is used for |
| 104 | * for trigger some periodic events. This is straightforward and better |
| 105 | * than using jiffies etc. to handle periodic memcg event. |
| 106 | */ |
| 107 | enum mem_cgroup_events_target { |
| 108 | MEM_CGROUP_TARGET_THRESH, |
| 109 | MEM_CGROUP_TARGET_SOFTLIMIT, |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 110 | MEM_CGROUP_TARGET_NUMAINFO, |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 111 | MEM_CGROUP_NTARGETS, |
| 112 | }; |
| 113 | #define THRESHOLDS_EVENTS_TARGET (128) |
| 114 | #define SOFTLIMIT_EVENTS_TARGET (1024) |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 115 | #define NUMAINFO_EVENTS_TARGET (1024) |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 116 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 117 | struct mem_cgroup_stat_cpu { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 118 | long count[MEM_CGROUP_STAT_NSTATS]; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 119 | unsigned long events[MEM_CGROUP_EVENTS_NSTATS]; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 120 | unsigned long targets[MEM_CGROUP_NTARGETS]; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 123 | /* |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 124 | * per-zone information in memory controller. |
| 125 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 126 | struct mem_cgroup_per_zone { |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 127 | /* |
| 128 | * spin_lock to protect the per cgroup LRU |
| 129 | */ |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 130 | struct list_head lists[NR_LRU_LISTS]; |
| 131 | unsigned long count[NR_LRU_LISTS]; |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 132 | |
| 133 | struct zone_reclaim_stat reclaim_stat; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 134 | struct rb_node tree_node; /* RB tree node */ |
| 135 | unsigned long long usage_in_excess;/* Set to the value by which */ |
| 136 | /* the soft limit is exceeded*/ |
| 137 | bool on_tree; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 138 | struct mem_cgroup *mem; /* Back pointer, we cannot */ |
| 139 | /* use container_of */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 140 | }; |
| 141 | /* Macro for accessing counter */ |
| 142 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) |
| 143 | |
| 144 | struct mem_cgroup_per_node { |
| 145 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; |
| 146 | }; |
| 147 | |
| 148 | struct mem_cgroup_lru_info { |
| 149 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; |
| 150 | }; |
| 151 | |
| 152 | /* |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 153 | * Cgroups above their limits are maintained in a RB-Tree, independent of |
| 154 | * their hierarchy representation |
| 155 | */ |
| 156 | |
| 157 | struct mem_cgroup_tree_per_zone { |
| 158 | struct rb_root rb_root; |
| 159 | spinlock_t lock; |
| 160 | }; |
| 161 | |
| 162 | struct mem_cgroup_tree_per_node { |
| 163 | struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES]; |
| 164 | }; |
| 165 | |
| 166 | struct mem_cgroup_tree { |
| 167 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; |
| 168 | }; |
| 169 | |
| 170 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; |
| 171 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 172 | struct mem_cgroup_threshold { |
| 173 | struct eventfd_ctx *eventfd; |
| 174 | u64 threshold; |
| 175 | }; |
| 176 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 177 | /* For threshold */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 178 | struct mem_cgroup_threshold_ary { |
| 179 | /* An array index points to threshold just below usage. */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 180 | int current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 181 | /* Size of entries[] */ |
| 182 | unsigned int size; |
| 183 | /* Array of thresholds */ |
| 184 | struct mem_cgroup_threshold entries[0]; |
| 185 | }; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 186 | |
| 187 | struct mem_cgroup_thresholds { |
| 188 | /* Primary thresholds array */ |
| 189 | struct mem_cgroup_threshold_ary *primary; |
| 190 | /* |
| 191 | * Spare threshold array. |
| 192 | * This is needed to make mem_cgroup_unregister_event() "never fail". |
| 193 | * It must be able to store at least primary->size - 1 entries. |
| 194 | */ |
| 195 | struct mem_cgroup_threshold_ary *spare; |
| 196 | }; |
| 197 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 198 | /* for OOM */ |
| 199 | struct mem_cgroup_eventfd_list { |
| 200 | struct list_head list; |
| 201 | struct eventfd_ctx *eventfd; |
| 202 | }; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 203 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 204 | static void mem_cgroup_threshold(struct mem_cgroup *mem); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 205 | static void mem_cgroup_oom_notify(struct mem_cgroup *mem); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 206 | |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 207 | enum { |
| 208 | SCAN_BY_LIMIT, |
| 209 | SCAN_BY_SYSTEM, |
| 210 | NR_SCAN_CONTEXT, |
| 211 | SCAN_BY_SHRINK, /* not recorded now */ |
| 212 | }; |
| 213 | |
| 214 | enum { |
| 215 | SCAN, |
| 216 | SCAN_ANON, |
| 217 | SCAN_FILE, |
| 218 | ROTATE, |
| 219 | ROTATE_ANON, |
| 220 | ROTATE_FILE, |
| 221 | FREED, |
| 222 | FREED_ANON, |
| 223 | FREED_FILE, |
| 224 | ELAPSED, |
| 225 | NR_SCANSTATS, |
| 226 | }; |
| 227 | |
| 228 | struct scanstat { |
| 229 | spinlock_t lock; |
| 230 | unsigned long stats[NR_SCAN_CONTEXT][NR_SCANSTATS]; |
| 231 | unsigned long rootstats[NR_SCAN_CONTEXT][NR_SCANSTATS]; |
| 232 | }; |
| 233 | |
| 234 | const char *scanstat_string[NR_SCANSTATS] = { |
| 235 | "scanned_pages", |
| 236 | "scanned_anon_pages", |
| 237 | "scanned_file_pages", |
| 238 | "rotated_pages", |
| 239 | "rotated_anon_pages", |
| 240 | "rotated_file_pages", |
| 241 | "freed_pages", |
| 242 | "freed_anon_pages", |
| 243 | "freed_file_pages", |
| 244 | "elapsed_ns", |
| 245 | }; |
| 246 | #define SCANSTAT_WORD_LIMIT "_by_limit" |
| 247 | #define SCANSTAT_WORD_SYSTEM "_by_system" |
| 248 | #define SCANSTAT_WORD_HIERARCHY "_under_hierarchy" |
| 249 | |
| 250 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 251 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 252 | * The memory controller data structure. The memory controller controls both |
| 253 | * page cache and RSS per cgroup. We would eventually like to provide |
| 254 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, |
| 255 | * to help the administrator determine what knobs to tune. |
| 256 | * |
| 257 | * 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] | 258 | * we hit the water mark. May be even add a low water mark, such that |
| 259 | * no reclaim occurs from a cgroup at it's low water mark, this is |
| 260 | * a feature that will be implemented much later in the future. |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 261 | */ |
| 262 | struct mem_cgroup { |
| 263 | struct cgroup_subsys_state css; |
| 264 | /* |
| 265 | * the counter to account for memory usage |
| 266 | */ |
| 267 | struct res_counter res; |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 268 | /* |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 269 | * the counter to account for mem+swap usage. |
| 270 | */ |
| 271 | struct res_counter memsw; |
| 272 | /* |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 273 | * Per cgroup active and inactive list, similar to the |
| 274 | * per zone LRU lists. |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 275 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 276 | struct mem_cgroup_lru_info info; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 277 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 278 | * While reclaiming in a hierarchy, we cache the last child we |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 279 | * reclaimed from. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 280 | */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 281 | int last_scanned_child; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 282 | int last_scanned_node; |
| 283 | #if MAX_NUMNODES > 1 |
| 284 | nodemask_t scan_nodes; |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 285 | atomic_t numainfo_events; |
| 286 | atomic_t numainfo_updating; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 287 | #endif |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 288 | /* |
| 289 | * Should the accounting and control be hierarchical, per subtree? |
| 290 | */ |
| 291 | bool use_hierarchy; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 292 | |
| 293 | bool oom_lock; |
| 294 | atomic_t under_oom; |
| 295 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 296 | atomic_t refcnt; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 297 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 298 | int swappiness; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 299 | /* OOM-Killer disable */ |
| 300 | int oom_kill_disable; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 301 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 302 | /* set when res.limit == memsw.limit */ |
| 303 | bool memsw_is_minimum; |
| 304 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 305 | /* protect arrays of thresholds */ |
| 306 | struct mutex thresholds_lock; |
| 307 | |
| 308 | /* thresholds for memory usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 309 | struct mem_cgroup_thresholds thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 310 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 311 | /* thresholds for mem+swap usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 312 | struct mem_cgroup_thresholds memsw_thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 313 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 314 | /* For oom notifier event fd */ |
| 315 | struct list_head oom_notify; |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 316 | /* For recording LRU-scan statistics */ |
| 317 | struct scanstat scanstat; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 318 | /* |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 319 | * Should we move charges of a task when a task is moved into this |
| 320 | * mem_cgroup ? And what type of charges should we move ? |
| 321 | */ |
| 322 | unsigned long move_charge_at_immigrate; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 323 | /* |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 324 | * percpu counter. |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 325 | */ |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 326 | struct mem_cgroup_stat_cpu *stat; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 327 | /* |
| 328 | * used when a cpu is offlined or other synchronizations |
| 329 | * See mem_cgroup_read_stat(). |
| 330 | */ |
| 331 | struct mem_cgroup_stat_cpu nocpu_base; |
| 332 | spinlock_t pcp_counter_lock; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 333 | }; |
| 334 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 335 | /* Stuffs for move charges at task migration. */ |
| 336 | /* |
| 337 | * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a |
| 338 | * left-shifted bitmap of these types. |
| 339 | */ |
| 340 | enum move_type { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 341 | MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */ |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 342 | MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 343 | NR_MOVE_TYPE, |
| 344 | }; |
| 345 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 346 | /* "mc" and its members are protected by cgroup_mutex */ |
| 347 | static struct move_charge_struct { |
Daisuke Nishimura | b1dd693 | 2010-11-24 12:57:06 -0800 | [diff] [blame] | 348 | spinlock_t lock; /* for from, to */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 349 | struct mem_cgroup *from; |
| 350 | struct mem_cgroup *to; |
| 351 | unsigned long precharge; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 352 | unsigned long moved_charge; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 353 | unsigned long moved_swap; |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 354 | struct task_struct *moving_task; /* a task moving charges */ |
| 355 | wait_queue_head_t waitq; /* a waitq for other context */ |
| 356 | } mc = { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 357 | .lock = __SPIN_LOCK_UNLOCKED(mc.lock), |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 358 | .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq), |
| 359 | }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 360 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 361 | static bool move_anon(void) |
| 362 | { |
| 363 | return test_bit(MOVE_CHARGE_TYPE_ANON, |
| 364 | &mc.to->move_charge_at_immigrate); |
| 365 | } |
| 366 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 367 | static bool move_file(void) |
| 368 | { |
| 369 | return test_bit(MOVE_CHARGE_TYPE_FILE, |
| 370 | &mc.to->move_charge_at_immigrate); |
| 371 | } |
| 372 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 373 | /* |
| 374 | * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft |
| 375 | * limit reclaim to prevent infinite loops, if they ever occur. |
| 376 | */ |
| 377 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100) |
| 378 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2) |
| 379 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 380 | enum charge_type { |
| 381 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
| 382 | MEM_CGROUP_CHARGE_TYPE_MAPPED, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 383 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 384 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 385 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 386 | MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 387 | NR_CHARGE_TYPE, |
| 388 | }; |
| 389 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 390 | /* for encoding cft->private value on file */ |
| 391 | #define _MEM (0) |
| 392 | #define _MEMSWAP (1) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 393 | #define _OOM_TYPE (2) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 394 | #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) |
| 395 | #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff) |
| 396 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 397 | /* Used for OOM nofiier */ |
| 398 | #define OOM_CONTROL (0) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 399 | |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 400 | /* |
| 401 | * Reclaim flags for mem_cgroup_hierarchical_reclaim |
| 402 | */ |
| 403 | #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0 |
| 404 | #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT) |
| 405 | #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1 |
| 406 | #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 407 | #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2 |
| 408 | #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT) |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 409 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 410 | static void mem_cgroup_get(struct mem_cgroup *mem); |
| 411 | static void mem_cgroup_put(struct mem_cgroup *mem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 412 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 413 | static void drain_all_stock_async(struct mem_cgroup *mem); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 414 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 415 | static struct mem_cgroup_per_zone * |
| 416 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) |
| 417 | { |
| 418 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; |
| 419 | } |
| 420 | |
Wu Fengguang | d324236 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 421 | struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem) |
| 422 | { |
| 423 | return &mem->css; |
| 424 | } |
| 425 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 426 | static struct mem_cgroup_per_zone * |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 427 | page_cgroup_zoneinfo(struct mem_cgroup *mem, struct page *page) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 428 | { |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 429 | int nid = page_to_nid(page); |
| 430 | int zid = page_zonenum(page); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 431 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 432 | return mem_cgroup_zoneinfo(mem, nid, zid); |
| 433 | } |
| 434 | |
| 435 | static struct mem_cgroup_tree_per_zone * |
| 436 | soft_limit_tree_node_zone(int nid, int zid) |
| 437 | { |
| 438 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 439 | } |
| 440 | |
| 441 | static struct mem_cgroup_tree_per_zone * |
| 442 | soft_limit_tree_from_page(struct page *page) |
| 443 | { |
| 444 | int nid = page_to_nid(page); |
| 445 | int zid = page_zonenum(page); |
| 446 | |
| 447 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 448 | } |
| 449 | |
| 450 | static void |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 451 | __mem_cgroup_insert_exceeded(struct mem_cgroup *mem, |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 452 | struct mem_cgroup_per_zone *mz, |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 453 | struct mem_cgroup_tree_per_zone *mctz, |
| 454 | unsigned long long new_usage_in_excess) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 455 | { |
| 456 | struct rb_node **p = &mctz->rb_root.rb_node; |
| 457 | struct rb_node *parent = NULL; |
| 458 | struct mem_cgroup_per_zone *mz_node; |
| 459 | |
| 460 | if (mz->on_tree) |
| 461 | return; |
| 462 | |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 463 | mz->usage_in_excess = new_usage_in_excess; |
| 464 | if (!mz->usage_in_excess) |
| 465 | return; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 466 | while (*p) { |
| 467 | parent = *p; |
| 468 | mz_node = rb_entry(parent, struct mem_cgroup_per_zone, |
| 469 | tree_node); |
| 470 | if (mz->usage_in_excess < mz_node->usage_in_excess) |
| 471 | p = &(*p)->rb_left; |
| 472 | /* |
| 473 | * We can't avoid mem cgroups that are over their soft |
| 474 | * limit by the same amount |
| 475 | */ |
| 476 | else if (mz->usage_in_excess >= mz_node->usage_in_excess) |
| 477 | p = &(*p)->rb_right; |
| 478 | } |
| 479 | rb_link_node(&mz->tree_node, parent, p); |
| 480 | rb_insert_color(&mz->tree_node, &mctz->rb_root); |
| 481 | mz->on_tree = true; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | static void |
| 485 | __mem_cgroup_remove_exceeded(struct mem_cgroup *mem, |
| 486 | struct mem_cgroup_per_zone *mz, |
| 487 | struct mem_cgroup_tree_per_zone *mctz) |
| 488 | { |
| 489 | if (!mz->on_tree) |
| 490 | return; |
| 491 | rb_erase(&mz->tree_node, &mctz->rb_root); |
| 492 | mz->on_tree = false; |
| 493 | } |
| 494 | |
| 495 | static void |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 496 | mem_cgroup_remove_exceeded(struct mem_cgroup *mem, |
| 497 | struct mem_cgroup_per_zone *mz, |
| 498 | struct mem_cgroup_tree_per_zone *mctz) |
| 499 | { |
| 500 | spin_lock(&mctz->lock); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 501 | __mem_cgroup_remove_exceeded(mem, mz, mctz); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 502 | spin_unlock(&mctz->lock); |
| 503 | } |
| 504 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 505 | |
| 506 | static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page) |
| 507 | { |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 508 | unsigned long long excess; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 509 | struct mem_cgroup_per_zone *mz; |
| 510 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 511 | int nid = page_to_nid(page); |
| 512 | int zid = page_zonenum(page); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 513 | mctz = soft_limit_tree_from_page(page); |
| 514 | |
| 515 | /* |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 516 | * Necessary to update all ancestors when hierarchy is used. |
| 517 | * because their event counter is not touched. |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 518 | */ |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 519 | for (; mem; mem = parent_mem_cgroup(mem)) { |
| 520 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 521 | excess = res_counter_soft_limit_excess(&mem->res); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 522 | /* |
| 523 | * We have to update the tree if mz is on RB-tree or |
| 524 | * mem is over its softlimit. |
| 525 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 526 | if (excess || mz->on_tree) { |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 527 | spin_lock(&mctz->lock); |
| 528 | /* if on-tree, remove it */ |
| 529 | if (mz->on_tree) |
| 530 | __mem_cgroup_remove_exceeded(mem, mz, mctz); |
| 531 | /* |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 532 | * Insert again. mz->usage_in_excess will be updated. |
| 533 | * If excess is 0, no tree ops. |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 534 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 535 | __mem_cgroup_insert_exceeded(mem, mz, mctz, excess); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 536 | spin_unlock(&mctz->lock); |
| 537 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
| 541 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem) |
| 542 | { |
| 543 | int node, zone; |
| 544 | struct mem_cgroup_per_zone *mz; |
| 545 | struct mem_cgroup_tree_per_zone *mctz; |
| 546 | |
| 547 | for_each_node_state(node, N_POSSIBLE) { |
| 548 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 549 | mz = mem_cgroup_zoneinfo(mem, node, zone); |
| 550 | mctz = soft_limit_tree_node_zone(node, zone); |
| 551 | mem_cgroup_remove_exceeded(mem, mz, mctz); |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 556 | static struct mem_cgroup_per_zone * |
| 557 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 558 | { |
| 559 | struct rb_node *rightmost = NULL; |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 560 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 561 | |
| 562 | retry: |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 563 | mz = NULL; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 564 | rightmost = rb_last(&mctz->rb_root); |
| 565 | if (!rightmost) |
| 566 | goto done; /* Nothing to reclaim from */ |
| 567 | |
| 568 | mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node); |
| 569 | /* |
| 570 | * Remove the node now but someone else can add it back, |
| 571 | * we will to add it back at the end of reclaim to its correct |
| 572 | * position in the tree. |
| 573 | */ |
| 574 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
| 575 | if (!res_counter_soft_limit_excess(&mz->mem->res) || |
| 576 | !css_tryget(&mz->mem->css)) |
| 577 | goto retry; |
| 578 | done: |
| 579 | return mz; |
| 580 | } |
| 581 | |
| 582 | static struct mem_cgroup_per_zone * |
| 583 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 584 | { |
| 585 | struct mem_cgroup_per_zone *mz; |
| 586 | |
| 587 | spin_lock(&mctz->lock); |
| 588 | mz = __mem_cgroup_largest_soft_limit_node(mctz); |
| 589 | spin_unlock(&mctz->lock); |
| 590 | return mz; |
| 591 | } |
| 592 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 593 | /* |
| 594 | * Implementation Note: reading percpu statistics for memcg. |
| 595 | * |
| 596 | * Both of vmstat[] and percpu_counter has threshold and do periodic |
| 597 | * synchronization to implement "quick" read. There are trade-off between |
| 598 | * reading cost and precision of value. Then, we may have a chance to implement |
| 599 | * a periodic synchronizion of counter in memcg's counter. |
| 600 | * |
| 601 | * But this _read() function is used for user interface now. The user accounts |
| 602 | * memory usage by memory cgroup and he _always_ requires exact value because |
| 603 | * he accounts memory. Even if we provide quick-and-fuzzy read, we always |
| 604 | * have to visit all online cpus and make sum. So, for now, unnecessary |
| 605 | * synchronization is not implemented. (just implemented for cpu hotplug) |
| 606 | * |
| 607 | * If there are kernel internal actions which can make use of some not-exact |
| 608 | * value, and reading all cpu value can be performance bottleneck in some |
| 609 | * common workload, threashold and synchonization as vmstat[] should be |
| 610 | * implemented. |
| 611 | */ |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 612 | static long mem_cgroup_read_stat(struct mem_cgroup *mem, |
| 613 | enum mem_cgroup_stat_index idx) |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 614 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 615 | long val = 0; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 616 | int cpu; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 617 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 618 | get_online_cpus(); |
| 619 | for_each_online_cpu(cpu) |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 620 | val += per_cpu(mem->stat->count[idx], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 621 | #ifdef CONFIG_HOTPLUG_CPU |
| 622 | spin_lock(&mem->pcp_counter_lock); |
| 623 | val += mem->nocpu_base.count[idx]; |
| 624 | spin_unlock(&mem->pcp_counter_lock); |
| 625 | #endif |
| 626 | put_online_cpus(); |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 627 | return val; |
| 628 | } |
| 629 | |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 630 | static void mem_cgroup_swap_statistics(struct mem_cgroup *mem, |
| 631 | bool charge) |
| 632 | { |
| 633 | int val = (charge) ? 1 : -1; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 634 | this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 637 | void mem_cgroup_pgfault(struct mem_cgroup *mem, int val) |
| 638 | { |
| 639 | this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val); |
| 640 | } |
| 641 | |
| 642 | void mem_cgroup_pgmajfault(struct mem_cgroup *mem, int val) |
| 643 | { |
| 644 | this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val); |
| 645 | } |
| 646 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 647 | static unsigned long mem_cgroup_read_events(struct mem_cgroup *mem, |
| 648 | enum mem_cgroup_events_index idx) |
| 649 | { |
| 650 | unsigned long val = 0; |
| 651 | int cpu; |
| 652 | |
| 653 | for_each_online_cpu(cpu) |
| 654 | val += per_cpu(mem->stat->events[idx], cpu); |
| 655 | #ifdef CONFIG_HOTPLUG_CPU |
| 656 | spin_lock(&mem->pcp_counter_lock); |
| 657 | val += mem->nocpu_base.events[idx]; |
| 658 | spin_unlock(&mem->pcp_counter_lock); |
| 659 | #endif |
| 660 | return val; |
| 661 | } |
| 662 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 663 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 664 | bool file, int nr_pages) |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 665 | { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 666 | preempt_disable(); |
| 667 | |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 668 | if (file) |
| 669 | __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], nr_pages); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 670 | else |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 671 | __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], nr_pages); |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 672 | |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 673 | /* pagein of a big page is an event. So, ignore page size */ |
| 674 | if (nr_pages > 0) |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 675 | __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGIN]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 676 | else { |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 677 | __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 678 | nr_pages = -nr_pages; /* for event */ |
| 679 | } |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 680 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 681 | __this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 682 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 683 | preempt_enable(); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 684 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 685 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 686 | unsigned long |
| 687 | mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *mem, int nid, int zid, |
| 688 | unsigned int lru_mask) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 689 | { |
| 690 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 691 | enum lru_list l; |
| 692 | unsigned long ret = 0; |
| 693 | |
| 694 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 695 | |
| 696 | for_each_lru(l) { |
| 697 | if (BIT(l) & lru_mask) |
| 698 | ret += MEM_CGROUP_ZSTAT(mz, l); |
| 699 | } |
| 700 | return ret; |
| 701 | } |
| 702 | |
| 703 | static unsigned long |
| 704 | mem_cgroup_node_nr_lru_pages(struct mem_cgroup *mem, |
| 705 | int nid, unsigned int lru_mask) |
| 706 | { |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 707 | u64 total = 0; |
| 708 | int zid; |
| 709 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 710 | for (zid = 0; zid < MAX_NR_ZONES; zid++) |
| 711 | total += mem_cgroup_zone_nr_lru_pages(mem, nid, zid, lru_mask); |
| 712 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 713 | return total; |
| 714 | } |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 715 | |
| 716 | static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *mem, |
| 717 | unsigned int lru_mask) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 718 | { |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 719 | int nid; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 720 | u64 total = 0; |
| 721 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 722 | for_each_node_state(nid, N_HIGH_MEMORY) |
| 723 | total += mem_cgroup_node_nr_lru_pages(mem, nid, lru_mask); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 724 | return total; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 725 | } |
| 726 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 727 | static bool __memcg_event_check(struct mem_cgroup *mem, int target) |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 728 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 729 | unsigned long val, next; |
| 730 | |
| 731 | val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]); |
| 732 | next = this_cpu_read(mem->stat->targets[target]); |
| 733 | /* from time_after() in jiffies.h */ |
| 734 | return ((long)next - (long)val < 0); |
| 735 | } |
| 736 | |
| 737 | static void __mem_cgroup_target_update(struct mem_cgroup *mem, int target) |
| 738 | { |
| 739 | unsigned long val, next; |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 740 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 741 | val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 742 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 743 | switch (target) { |
| 744 | case MEM_CGROUP_TARGET_THRESH: |
| 745 | next = val + THRESHOLDS_EVENTS_TARGET; |
| 746 | break; |
| 747 | case MEM_CGROUP_TARGET_SOFTLIMIT: |
| 748 | next = val + SOFTLIMIT_EVENTS_TARGET; |
| 749 | break; |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 750 | case MEM_CGROUP_TARGET_NUMAINFO: |
| 751 | next = val + NUMAINFO_EVENTS_TARGET; |
| 752 | break; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 753 | default: |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | this_cpu_write(mem->stat->targets[target], next); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /* |
| 761 | * Check events in order. |
| 762 | * |
| 763 | */ |
| 764 | static void memcg_check_events(struct mem_cgroup *mem, struct page *page) |
| 765 | { |
| 766 | /* threshold event is triggered in finer grain than soft limit */ |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 767 | if (unlikely(__memcg_event_check(mem, MEM_CGROUP_TARGET_THRESH))) { |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 768 | mem_cgroup_threshold(mem); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 769 | __mem_cgroup_target_update(mem, MEM_CGROUP_TARGET_THRESH); |
| 770 | if (unlikely(__memcg_event_check(mem, |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 771 | MEM_CGROUP_TARGET_SOFTLIMIT))) { |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 772 | mem_cgroup_update_tree(mem, page); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 773 | __mem_cgroup_target_update(mem, |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 774 | MEM_CGROUP_TARGET_SOFTLIMIT); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 775 | } |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 776 | #if MAX_NUMNODES > 1 |
| 777 | if (unlikely(__memcg_event_check(mem, |
| 778 | MEM_CGROUP_TARGET_NUMAINFO))) { |
| 779 | atomic_inc(&mem->numainfo_events); |
| 780 | __mem_cgroup_target_update(mem, |
| 781 | MEM_CGROUP_TARGET_NUMAINFO); |
| 782 | } |
| 783 | #endif |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 787 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 788 | { |
| 789 | return container_of(cgroup_subsys_state(cont, |
| 790 | mem_cgroup_subsys_id), struct mem_cgroup, |
| 791 | css); |
| 792 | } |
| 793 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 794 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 795 | { |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 796 | /* |
| 797 | * mm_update_next_owner() may clear mm->owner to NULL |
| 798 | * if it races with swapoff, page migration, etc. |
| 799 | * So this can be called with p == NULL. |
| 800 | */ |
| 801 | if (unlikely(!p)) |
| 802 | return NULL; |
| 803 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 804 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
| 805 | struct mem_cgroup, css); |
| 806 | } |
| 807 | |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 808 | struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 809 | { |
| 810 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 811 | |
| 812 | if (!mm) |
| 813 | return NULL; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 814 | /* |
| 815 | * Because we have no locks, mm->owner's may be being moved to other |
| 816 | * cgroup. We use css_tryget() here even if this looks |
| 817 | * pessimistic (rather than adding locks here). |
| 818 | */ |
| 819 | rcu_read_lock(); |
| 820 | do { |
| 821 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 822 | if (unlikely(!mem)) |
| 823 | break; |
| 824 | } while (!css_tryget(&mem->css)); |
| 825 | rcu_read_unlock(); |
| 826 | return mem; |
| 827 | } |
| 828 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 829 | /* The caller has to guarantee "mem" exists before calling this */ |
| 830 | static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 831 | { |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 832 | struct cgroup_subsys_state *css; |
| 833 | int found; |
| 834 | |
| 835 | if (!mem) /* ROOT cgroup has the smallest ID */ |
| 836 | return root_mem_cgroup; /*css_put/get against root is ignored*/ |
| 837 | if (!mem->use_hierarchy) { |
| 838 | if (css_tryget(&mem->css)) |
| 839 | return mem; |
| 840 | return NULL; |
| 841 | } |
| 842 | rcu_read_lock(); |
| 843 | /* |
| 844 | * searching a memory cgroup which has the smallest ID under given |
| 845 | * ROOT cgroup. (ID >= 1) |
| 846 | */ |
| 847 | css = css_get_next(&mem_cgroup_subsys, 1, &mem->css, &found); |
| 848 | if (css && css_tryget(css)) |
| 849 | mem = container_of(css, struct mem_cgroup, css); |
| 850 | else |
| 851 | mem = NULL; |
| 852 | rcu_read_unlock(); |
| 853 | return mem; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 854 | } |
| 855 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 856 | static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter, |
| 857 | struct mem_cgroup *root, |
| 858 | bool cond) |
| 859 | { |
| 860 | int nextid = css_id(&iter->css) + 1; |
| 861 | int found; |
| 862 | int hierarchy_used; |
| 863 | struct cgroup_subsys_state *css; |
| 864 | |
| 865 | hierarchy_used = iter->use_hierarchy; |
| 866 | |
| 867 | css_put(&iter->css); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 868 | /* If no ROOT, walk all, ignore hierarchy */ |
| 869 | if (!cond || (root && !hierarchy_used)) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 870 | return NULL; |
| 871 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 872 | if (!root) |
| 873 | root = root_mem_cgroup; |
| 874 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 875 | do { |
| 876 | iter = NULL; |
| 877 | rcu_read_lock(); |
| 878 | |
| 879 | css = css_get_next(&mem_cgroup_subsys, nextid, |
| 880 | &root->css, &found); |
| 881 | if (css && css_tryget(css)) |
| 882 | iter = container_of(css, struct mem_cgroup, css); |
| 883 | rcu_read_unlock(); |
| 884 | /* If css is NULL, no more cgroups will be found */ |
| 885 | nextid = found + 1; |
| 886 | } while (css && !iter); |
| 887 | |
| 888 | return iter; |
| 889 | } |
| 890 | /* |
| 891 | * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please |
| 892 | * be careful that "break" loop is not allowed. We have reference count. |
| 893 | * Instead of that modify "cond" to be false and "continue" to exit the loop. |
| 894 | */ |
| 895 | #define for_each_mem_cgroup_tree_cond(iter, root, cond) \ |
| 896 | for (iter = mem_cgroup_start_loop(root);\ |
| 897 | iter != NULL;\ |
| 898 | iter = mem_cgroup_get_next(iter, root, cond)) |
| 899 | |
| 900 | #define for_each_mem_cgroup_tree(iter, root) \ |
| 901 | for_each_mem_cgroup_tree_cond(iter, root, true) |
| 902 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 903 | #define for_each_mem_cgroup_all(iter) \ |
| 904 | for_each_mem_cgroup_tree_cond(iter, NULL, true) |
| 905 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 906 | |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 907 | static inline bool mem_cgroup_is_root(struct mem_cgroup *mem) |
| 908 | { |
| 909 | return (mem == root_mem_cgroup); |
| 910 | } |
| 911 | |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 912 | void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx) |
| 913 | { |
| 914 | struct mem_cgroup *mem; |
| 915 | |
| 916 | if (!mm) |
| 917 | return; |
| 918 | |
| 919 | rcu_read_lock(); |
| 920 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 921 | if (unlikely(!mem)) |
| 922 | goto out; |
| 923 | |
| 924 | switch (idx) { |
| 925 | case PGMAJFAULT: |
| 926 | mem_cgroup_pgmajfault(mem, 1); |
| 927 | break; |
| 928 | case PGFAULT: |
| 929 | mem_cgroup_pgfault(mem, 1); |
| 930 | break; |
| 931 | default: |
| 932 | BUG(); |
| 933 | } |
| 934 | out: |
| 935 | rcu_read_unlock(); |
| 936 | } |
| 937 | EXPORT_SYMBOL(mem_cgroup_count_vm_event); |
| 938 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 939 | /* |
| 940 | * Following LRU functions are allowed to be used without PCG_LOCK. |
| 941 | * Operations are called by routine of global LRU independently from memcg. |
| 942 | * What we have to take care of here is validness of pc->mem_cgroup. |
| 943 | * |
| 944 | * Changes to pc->mem_cgroup happens when |
| 945 | * 1. charge |
| 946 | * 2. moving account |
| 947 | * In typical case, "charge" is done before add-to-lru. Exception is SwapCache. |
| 948 | * It is added to LRU before charge. |
| 949 | * If PCG_USED bit is not set, page_cgroup is not added to this private LRU. |
| 950 | * When moving account, the page is not on LRU. It's isolated. |
| 951 | */ |
| 952 | |
| 953 | 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] | 954 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 955 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 956 | struct mem_cgroup_per_zone *mz; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 957 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 958 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 959 | return; |
| 960 | pc = lookup_page_cgroup(page); |
| 961 | /* can happen while we handle swapcache. */ |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 962 | if (!TestClearPageCgroupAcctLRU(pc)) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 963 | return; |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 964 | VM_BUG_ON(!pc->mem_cgroup); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 965 | /* |
| 966 | * We don't check PCG_USED bit. It's cleared when the "page" is finally |
| 967 | * removed from global LRU. |
| 968 | */ |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 969 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 970 | /* huge page split is done under lru_lock. so, we have no races. */ |
| 971 | MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 972 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 973 | return; |
| 974 | VM_BUG_ON(list_empty(&pc->lru)); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 975 | list_del_init(&pc->lru); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 976 | } |
| 977 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 978 | void mem_cgroup_del_lru(struct page *page) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 979 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 980 | mem_cgroup_del_lru_list(page, page_lru(page)); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 981 | } |
| 982 | |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 983 | /* |
| 984 | * Writeback is about to end against a page which has been marked for immediate |
| 985 | * reclaim. If it still appears to be reclaimable, move it to the tail of the |
| 986 | * inactive list. |
| 987 | */ |
| 988 | void mem_cgroup_rotate_reclaimable_page(struct page *page) |
| 989 | { |
| 990 | struct mem_cgroup_per_zone *mz; |
| 991 | struct page_cgroup *pc; |
| 992 | enum lru_list lru = page_lru(page); |
| 993 | |
| 994 | if (mem_cgroup_disabled()) |
| 995 | return; |
| 996 | |
| 997 | pc = lookup_page_cgroup(page); |
| 998 | /* unused or root page is not rotated. */ |
| 999 | if (!PageCgroupUsed(pc)) |
| 1000 | return; |
| 1001 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 1002 | smp_rmb(); |
| 1003 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 1004 | return; |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1005 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 1006 | list_move_tail(&pc->lru, &mz->lists[lru]); |
| 1007 | } |
| 1008 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1009 | 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] | 1010 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1011 | struct mem_cgroup_per_zone *mz; |
| 1012 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1013 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1014 | if (mem_cgroup_disabled()) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 1015 | return; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1016 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1017 | pc = lookup_page_cgroup(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 1018 | /* unused or root page is not rotated. */ |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 1019 | if (!PageCgroupUsed(pc)) |
| 1020 | return; |
| 1021 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 1022 | smp_rmb(); |
| 1023 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1024 | return; |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1025 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1026 | list_move(&pc->lru, &mz->lists[lru]); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1027 | } |
| 1028 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1029 | void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru) |
| 1030 | { |
| 1031 | struct page_cgroup *pc; |
| 1032 | struct mem_cgroup_per_zone *mz; |
| 1033 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1034 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1035 | return; |
| 1036 | pc = lookup_page_cgroup(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 1037 | VM_BUG_ON(PageCgroupAcctLRU(pc)); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1038 | if (!PageCgroupUsed(pc)) |
| 1039 | return; |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 1040 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 1041 | smp_rmb(); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1042 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 1043 | /* huge page split is done under lru_lock. so, we have no races. */ |
| 1044 | MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 1045 | SetPageCgroupAcctLRU(pc); |
| 1046 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 1047 | return; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1048 | list_add(&pc->lru, &mz->lists[lru]); |
| 1049 | } |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1050 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1051 | /* |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1052 | * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed |
| 1053 | * while it's linked to lru because the page may be reused after it's fully |
| 1054 | * uncharged. To handle that, unlink page_cgroup from LRU when charge it again. |
| 1055 | * It's done under lock_page and expected that zone->lru_lock isnever held. |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1056 | */ |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1057 | static void mem_cgroup_lru_del_before_commit(struct page *page) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1058 | { |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1059 | unsigned long flags; |
| 1060 | struct zone *zone = page_zone(page); |
| 1061 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 1062 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1063 | /* |
| 1064 | * Doing this check without taking ->lru_lock seems wrong but this |
| 1065 | * is safe. Because if page_cgroup's USED bit is unset, the page |
| 1066 | * will not be added to any memcg's LRU. If page_cgroup's USED bit is |
| 1067 | * set, the commit after this will fail, anyway. |
| 1068 | * This all charge/uncharge is done under some mutual execustion. |
| 1069 | * So, we don't need to taking care of changes in USED bit. |
| 1070 | */ |
| 1071 | if (likely(!PageLRU(page))) |
| 1072 | return; |
| 1073 | |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1074 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 1075 | /* |
| 1076 | * Forget old LRU when this page_cgroup is *not* used. This Used bit |
| 1077 | * is guarded by lock_page() because the page is SwapCache. |
| 1078 | */ |
| 1079 | if (!PageCgroupUsed(pc)) |
| 1080 | mem_cgroup_del_lru_list(page, page_lru(page)); |
| 1081 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1084 | static void mem_cgroup_lru_add_after_commit(struct page *page) |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1085 | { |
| 1086 | unsigned long flags; |
| 1087 | struct zone *zone = page_zone(page); |
| 1088 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 1089 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1090 | /* taking care of that the page is added to LRU while we commit it */ |
| 1091 | if (likely(!PageLRU(page))) |
| 1092 | return; |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1093 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 1094 | /* 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] | 1095 | if (PageLRU(page) && !PageCgroupAcctLRU(pc)) |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1096 | mem_cgroup_add_lru_list(page, page_lru(page)); |
| 1097 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
| 1098 | } |
| 1099 | |
| 1100 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1101 | void mem_cgroup_move_lists(struct page *page, |
| 1102 | enum lru_list from, enum lru_list to) |
| 1103 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1104 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1105 | return; |
| 1106 | mem_cgroup_del_lru_list(page, from); |
| 1107 | mem_cgroup_add_lru_list(page, to); |
| 1108 | } |
| 1109 | |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1110 | /* |
| 1111 | * Checks whether given mem is same or in the root_mem's |
| 1112 | * hierarchy subtree |
| 1113 | */ |
| 1114 | static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_mem, |
| 1115 | struct mem_cgroup *mem) |
| 1116 | { |
| 1117 | if (root_mem != mem) { |
| 1118 | return (root_mem->use_hierarchy && |
| 1119 | css_is_ancestor(&mem->css, &root_mem->css)); |
| 1120 | } |
| 1121 | |
| 1122 | return true; |
| 1123 | } |
| 1124 | |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1125 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
| 1126 | { |
| 1127 | int ret; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1128 | struct mem_cgroup *curr = NULL; |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1129 | struct task_struct *p; |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1130 | |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1131 | p = find_lock_task_mm(task); |
| 1132 | if (!p) |
| 1133 | return 0; |
| 1134 | curr = try_get_mem_cgroup_from_mm(p->mm); |
| 1135 | task_unlock(p); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1136 | if (!curr) |
| 1137 | return 0; |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1138 | /* |
| 1139 | * We should check use_hierarchy of "mem" not "curr". Because checking |
| 1140 | * use_hierarchy of "curr" here make this function true if hierarchy is |
| 1141 | * enabled in "curr" and "curr" is a child of "mem" in *cgroup* |
| 1142 | * hierarchy(even if use_hierarchy is disabled in "mem"). |
| 1143 | */ |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1144 | ret = mem_cgroup_same_or_subtree(mem, curr); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1145 | css_put(&curr->css); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1146 | return ret; |
| 1147 | } |
| 1148 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1149 | 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] | 1150 | { |
| 1151 | unsigned long active; |
| 1152 | unsigned long inactive; |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1153 | unsigned long gb; |
| 1154 | unsigned long inactive_ratio; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1155 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1156 | inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON)); |
| 1157 | active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON)); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1158 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1159 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
| 1160 | if (gb) |
| 1161 | inactive_ratio = int_sqrt(10 * gb); |
| 1162 | else |
| 1163 | inactive_ratio = 1; |
| 1164 | |
| 1165 | if (present_pages) { |
| 1166 | present_pages[0] = inactive; |
| 1167 | present_pages[1] = active; |
| 1168 | } |
| 1169 | |
| 1170 | return inactive_ratio; |
| 1171 | } |
| 1172 | |
| 1173 | int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg) |
| 1174 | { |
| 1175 | unsigned long active; |
| 1176 | unsigned long inactive; |
| 1177 | unsigned long present_pages[2]; |
| 1178 | unsigned long inactive_ratio; |
| 1179 | |
| 1180 | inactive_ratio = calc_inactive_ratio(memcg, present_pages); |
| 1181 | |
| 1182 | inactive = present_pages[0]; |
| 1183 | active = present_pages[1]; |
| 1184 | |
| 1185 | if (inactive * inactive_ratio < active) |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1186 | return 1; |
| 1187 | |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
Rik van Riel | 56e49d2 | 2009-06-16 15:32:28 -0700 | [diff] [blame] | 1191 | int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg) |
| 1192 | { |
| 1193 | unsigned long active; |
| 1194 | unsigned long inactive; |
| 1195 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1196 | inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE)); |
| 1197 | active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE)); |
Rik van Riel | 56e49d2 | 2009-06-16 15:32:28 -0700 | [diff] [blame] | 1198 | |
| 1199 | return (active > inactive); |
| 1200 | } |
| 1201 | |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1202 | struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg, |
| 1203 | struct zone *zone) |
| 1204 | { |
KOSAKI Motohiro | 13d7e3a | 2010-08-10 18:03:06 -0700 | [diff] [blame] | 1205 | int nid = zone_to_nid(zone); |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1206 | int zid = zone_idx(zone); |
| 1207 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 1208 | |
| 1209 | return &mz->reclaim_stat; |
| 1210 | } |
| 1211 | |
| 1212 | struct zone_reclaim_stat * |
| 1213 | mem_cgroup_get_reclaim_stat_from_page(struct page *page) |
| 1214 | { |
| 1215 | struct page_cgroup *pc; |
| 1216 | struct mem_cgroup_per_zone *mz; |
| 1217 | |
| 1218 | if (mem_cgroup_disabled()) |
| 1219 | return NULL; |
| 1220 | |
| 1221 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 1222 | if (!PageCgroupUsed(pc)) |
| 1223 | return NULL; |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 1224 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 1225 | smp_rmb(); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1226 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1227 | return &mz->reclaim_stat; |
| 1228 | } |
| 1229 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1230 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 1231 | struct list_head *dst, |
| 1232 | unsigned long *scanned, int order, |
| 1233 | int mode, struct zone *z, |
| 1234 | struct mem_cgroup *mem_cont, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1235 | int active, int file) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1236 | { |
| 1237 | unsigned long nr_taken = 0; |
| 1238 | struct page *page; |
| 1239 | unsigned long scan; |
| 1240 | LIST_HEAD(pc_list); |
| 1241 | struct list_head *src; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1242 | struct page_cgroup *pc, *tmp; |
KOSAKI Motohiro | 13d7e3a | 2010-08-10 18:03:06 -0700 | [diff] [blame] | 1243 | int nid = zone_to_nid(z); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1244 | int zid = zone_idx(z); |
| 1245 | struct mem_cgroup_per_zone *mz; |
Johannes Weiner | b7c46d1 | 2009-09-21 17:02:56 -0700 | [diff] [blame] | 1246 | int lru = LRU_FILE * file + active; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1247 | int ret; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1248 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 1249 | BUG_ON(!mem_cont); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1250 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1251 | src = &mz->lists[lru]; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1252 | |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1253 | scan = 0; |
| 1254 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1255 | if (scan >= nr_to_scan) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1256 | break; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1257 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1258 | if (unlikely(!PageCgroupUsed(pc))) |
| 1259 | continue; |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 1260 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 1261 | page = lookup_cgroup_page(pc); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 1262 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1263 | if (unlikely(!PageLRU(page))) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1264 | continue; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1265 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1266 | scan++; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1267 | ret = __isolate_lru_page(page, mode, file); |
| 1268 | switch (ret) { |
| 1269 | case 0: |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1270 | list_move(&page->lru, dst); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1271 | mem_cgroup_del_lru(page); |
Rik van Riel | 2c888cf | 2011-01-13 15:47:13 -0800 | [diff] [blame] | 1272 | nr_taken += hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1273 | break; |
| 1274 | case -EBUSY: |
| 1275 | /* we don't affect global LRU but rotate in our LRU */ |
| 1276 | mem_cgroup_rotate_lru_list(page, page_lru(page)); |
| 1277 | break; |
| 1278 | default: |
| 1279 | break; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1280 | } |
| 1281 | } |
| 1282 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1283 | *scanned = scan; |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 1284 | |
| 1285 | trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken, |
| 1286 | 0, 0, 0, mode); |
| 1287 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1288 | return nr_taken; |
| 1289 | } |
| 1290 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1291 | #define mem_cgroup_from_res_counter(counter, member) \ |
| 1292 | container_of(counter, struct mem_cgroup, member) |
| 1293 | |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1294 | /** |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1295 | * mem_cgroup_margin - calculate chargeable space of a memory cgroup |
| 1296 | * @mem: the memory cgroup |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1297 | * |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1298 | * Returns the maximum amount of memory @mem can be charged with, in |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1299 | * pages. |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1300 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1301 | static unsigned long mem_cgroup_margin(struct mem_cgroup *mem) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1302 | { |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1303 | unsigned long long margin; |
| 1304 | |
| 1305 | margin = res_counter_margin(&mem->res); |
| 1306 | if (do_swap_account) |
| 1307 | margin = min(margin, res_counter_margin(&mem->memsw)); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1308 | return margin >> PAGE_SHIFT; |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 1311 | int mem_cgroup_swappiness(struct mem_cgroup *memcg) |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1312 | { |
| 1313 | struct cgroup *cgrp = memcg->css.cgroup; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1314 | |
| 1315 | /* root ? */ |
| 1316 | if (cgrp->parent == NULL) |
| 1317 | return vm_swappiness; |
| 1318 | |
Johannes Weiner | bf1ff26 | 2011-03-23 16:42:32 -0700 | [diff] [blame] | 1319 | return memcg->swappiness; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1320 | } |
| 1321 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1322 | static void mem_cgroup_start_move(struct mem_cgroup *mem) |
| 1323 | { |
| 1324 | int cpu; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1325 | |
| 1326 | get_online_cpus(); |
| 1327 | spin_lock(&mem->pcp_counter_lock); |
| 1328 | for_each_online_cpu(cpu) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1329 | per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1330 | mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1; |
| 1331 | spin_unlock(&mem->pcp_counter_lock); |
| 1332 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1333 | |
| 1334 | synchronize_rcu(); |
| 1335 | } |
| 1336 | |
| 1337 | static void mem_cgroup_end_move(struct mem_cgroup *mem) |
| 1338 | { |
| 1339 | int cpu; |
| 1340 | |
| 1341 | if (!mem) |
| 1342 | return; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1343 | get_online_cpus(); |
| 1344 | spin_lock(&mem->pcp_counter_lock); |
| 1345 | for_each_online_cpu(cpu) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1346 | per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1347 | mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1; |
| 1348 | spin_unlock(&mem->pcp_counter_lock); |
| 1349 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1350 | } |
| 1351 | /* |
| 1352 | * 2 routines for checking "mem" is under move_account() or not. |
| 1353 | * |
| 1354 | * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used |
| 1355 | * for avoiding race in accounting. If true, |
| 1356 | * pc->mem_cgroup may be overwritten. |
| 1357 | * |
| 1358 | * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or |
| 1359 | * under hierarchy of moving cgroups. This is for |
| 1360 | * waiting at hith-memory prressure caused by "move". |
| 1361 | */ |
| 1362 | |
| 1363 | static bool mem_cgroup_stealed(struct mem_cgroup *mem) |
| 1364 | { |
| 1365 | VM_BUG_ON(!rcu_read_lock_held()); |
| 1366 | return this_cpu_read(mem->stat->count[MEM_CGROUP_ON_MOVE]) > 0; |
| 1367 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1368 | |
| 1369 | static bool mem_cgroup_under_move(struct mem_cgroup *mem) |
| 1370 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1371 | struct mem_cgroup *from; |
| 1372 | struct mem_cgroup *to; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1373 | bool ret = false; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1374 | /* |
| 1375 | * Unlike task_move routines, we access mc.to, mc.from not under |
| 1376 | * mutual exclusion by cgroup_mutex. Here, we take spinlock instead. |
| 1377 | */ |
| 1378 | spin_lock(&mc.lock); |
| 1379 | from = mc.from; |
| 1380 | to = mc.to; |
| 1381 | if (!from) |
| 1382 | goto unlock; |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1383 | |
| 1384 | ret = mem_cgroup_same_or_subtree(mem, from) |
| 1385 | || mem_cgroup_same_or_subtree(mem, to); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1386 | unlock: |
| 1387 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1388 | return ret; |
| 1389 | } |
| 1390 | |
| 1391 | static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem) |
| 1392 | { |
| 1393 | if (mc.moving_task && current != mc.moving_task) { |
| 1394 | if (mem_cgroup_under_move(mem)) { |
| 1395 | DEFINE_WAIT(wait); |
| 1396 | prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE); |
| 1397 | /* moving charge context might have finished. */ |
| 1398 | if (mc.moving_task) |
| 1399 | schedule(); |
| 1400 | finish_wait(&mc.waitq, &wait); |
| 1401 | return true; |
| 1402 | } |
| 1403 | } |
| 1404 | return false; |
| 1405 | } |
| 1406 | |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1407 | /** |
Kirill A. Shutemov | 6a6135b | 2010-03-10 15:22:25 -0800 | [diff] [blame] | 1408 | * 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] | 1409 | * @memcg: The memory cgroup that went over limit |
| 1410 | * @p: Task that is going to be killed |
| 1411 | * |
| 1412 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is |
| 1413 | * enabled |
| 1414 | */ |
| 1415 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) |
| 1416 | { |
| 1417 | struct cgroup *task_cgrp; |
| 1418 | struct cgroup *mem_cgrp; |
| 1419 | /* |
| 1420 | * Need a buffer in BSS, can't rely on allocations. The code relies |
| 1421 | * on the assumption that OOM is serialized for memory controller. |
| 1422 | * If this assumption is broken, revisit this code. |
| 1423 | */ |
| 1424 | static char memcg_name[PATH_MAX]; |
| 1425 | int ret; |
| 1426 | |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1427 | if (!memcg || !p) |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1428 | return; |
| 1429 | |
| 1430 | |
| 1431 | rcu_read_lock(); |
| 1432 | |
| 1433 | mem_cgrp = memcg->css.cgroup; |
| 1434 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); |
| 1435 | |
| 1436 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); |
| 1437 | if (ret < 0) { |
| 1438 | /* |
| 1439 | * Unfortunately, we are unable to convert to a useful name |
| 1440 | * But we'll still print out the usage information |
| 1441 | */ |
| 1442 | rcu_read_unlock(); |
| 1443 | goto done; |
| 1444 | } |
| 1445 | rcu_read_unlock(); |
| 1446 | |
| 1447 | printk(KERN_INFO "Task in %s killed", memcg_name); |
| 1448 | |
| 1449 | rcu_read_lock(); |
| 1450 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); |
| 1451 | if (ret < 0) { |
| 1452 | rcu_read_unlock(); |
| 1453 | goto done; |
| 1454 | } |
| 1455 | rcu_read_unlock(); |
| 1456 | |
| 1457 | /* |
| 1458 | * Continues from above, so we don't need an KERN_ level |
| 1459 | */ |
| 1460 | printk(KERN_CONT " as a result of limit of %s\n", memcg_name); |
| 1461 | done: |
| 1462 | |
| 1463 | printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n", |
| 1464 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, |
| 1465 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, |
| 1466 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); |
| 1467 | printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, " |
| 1468 | "failcnt %llu\n", |
| 1469 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, |
| 1470 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, |
| 1471 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); |
| 1472 | } |
| 1473 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1474 | /* |
| 1475 | * This function returns the number of memcg under hierarchy tree. Returns |
| 1476 | * 1(self count) if no children. |
| 1477 | */ |
| 1478 | static int mem_cgroup_count_children(struct mem_cgroup *mem) |
| 1479 | { |
| 1480 | int num = 0; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1481 | struct mem_cgroup *iter; |
| 1482 | |
| 1483 | for_each_mem_cgroup_tree(iter, mem) |
| 1484 | num++; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1485 | return num; |
| 1486 | } |
| 1487 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1488 | /* |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1489 | * Return the memory (and swap, if configured) limit for a memcg. |
| 1490 | */ |
| 1491 | u64 mem_cgroup_get_limit(struct mem_cgroup *memcg) |
| 1492 | { |
| 1493 | u64 limit; |
| 1494 | u64 memsw; |
| 1495 | |
Johannes Weiner | f3e8eb7 | 2011-01-13 15:47:39 -0800 | [diff] [blame] | 1496 | limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 1497 | limit += total_swap_pages << PAGE_SHIFT; |
| 1498 | |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1499 | memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 1500 | /* |
| 1501 | * If memsw is finite and limits the amount of swap space available |
| 1502 | * to this memcg, return that limit. |
| 1503 | */ |
| 1504 | return min(limit, memsw); |
| 1505 | } |
| 1506 | |
| 1507 | /* |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1508 | * Visit the first child (need not be the first child as per the ordering |
| 1509 | * of the cgroup list, since we track last_scanned_child) of @mem and use |
| 1510 | * that to reclaim free pages from. |
| 1511 | */ |
| 1512 | static struct mem_cgroup * |
| 1513 | mem_cgroup_select_victim(struct mem_cgroup *root_mem) |
| 1514 | { |
| 1515 | struct mem_cgroup *ret = NULL; |
| 1516 | struct cgroup_subsys_state *css; |
| 1517 | int nextid, found; |
| 1518 | |
| 1519 | if (!root_mem->use_hierarchy) { |
| 1520 | css_get(&root_mem->css); |
| 1521 | ret = root_mem; |
| 1522 | } |
| 1523 | |
| 1524 | while (!ret) { |
| 1525 | rcu_read_lock(); |
| 1526 | nextid = root_mem->last_scanned_child + 1; |
| 1527 | css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css, |
| 1528 | &found); |
| 1529 | if (css && css_tryget(css)) |
| 1530 | ret = container_of(css, struct mem_cgroup, css); |
| 1531 | |
| 1532 | rcu_read_unlock(); |
| 1533 | /* Updates scanning parameter */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1534 | if (!css) { |
| 1535 | /* this means start scan from ID:1 */ |
| 1536 | root_mem->last_scanned_child = 0; |
| 1537 | } else |
| 1538 | root_mem->last_scanned_child = found; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | return ret; |
| 1542 | } |
| 1543 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1544 | /** |
| 1545 | * test_mem_cgroup_node_reclaimable |
| 1546 | * @mem: the target memcg |
| 1547 | * @nid: the node ID to be checked. |
| 1548 | * @noswap : specify true here if the user wants flle only information. |
| 1549 | * |
| 1550 | * This function returns whether the specified memcg contains any |
| 1551 | * reclaimable pages on a node. Returns true if there are any reclaimable |
| 1552 | * pages in the node. |
| 1553 | */ |
| 1554 | static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *mem, |
| 1555 | int nid, bool noswap) |
| 1556 | { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1557 | if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_FILE)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1558 | return true; |
| 1559 | if (noswap || !total_swap_pages) |
| 1560 | return false; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1561 | if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_ANON)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1562 | return true; |
| 1563 | return false; |
| 1564 | |
| 1565 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1566 | #if MAX_NUMNODES > 1 |
| 1567 | |
| 1568 | /* |
| 1569 | * Always updating the nodemask is not very good - even if we have an empty |
| 1570 | * list or the wrong list here, we can start from some node and traverse all |
| 1571 | * nodes based on the zonelist. So update the list loosely once per 10 secs. |
| 1572 | * |
| 1573 | */ |
| 1574 | static void mem_cgroup_may_update_nodemask(struct mem_cgroup *mem) |
| 1575 | { |
| 1576 | int nid; |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1577 | /* |
| 1578 | * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET |
| 1579 | * pagein/pageout changes since the last update. |
| 1580 | */ |
| 1581 | if (!atomic_read(&mem->numainfo_events)) |
| 1582 | return; |
| 1583 | if (atomic_inc_return(&mem->numainfo_updating) > 1) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1584 | return; |
| 1585 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1586 | /* make a nodemask where this memcg uses memory from */ |
| 1587 | mem->scan_nodes = node_states[N_HIGH_MEMORY]; |
| 1588 | |
| 1589 | for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) { |
| 1590 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1591 | if (!test_mem_cgroup_node_reclaimable(mem, nid, false)) |
| 1592 | node_clear(nid, mem->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1593 | } |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1594 | |
| 1595 | atomic_set(&mem->numainfo_events, 0); |
| 1596 | atomic_set(&mem->numainfo_updating, 0); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | /* |
| 1600 | * Selecting a node where we start reclaim from. Because what we need is just |
| 1601 | * reducing usage counter, start from anywhere is O,K. Considering |
| 1602 | * memory reclaim from current node, there are pros. and cons. |
| 1603 | * |
| 1604 | * Freeing memory from current node means freeing memory from a node which |
| 1605 | * we'll use or we've used. So, it may make LRU bad. And if several threads |
| 1606 | * hit limits, it will see a contention on a node. But freeing from remote |
| 1607 | * node means more costs for memory reclaim because of memory latency. |
| 1608 | * |
| 1609 | * Now, we use round-robin. Better algorithm is welcomed. |
| 1610 | */ |
| 1611 | int mem_cgroup_select_victim_node(struct mem_cgroup *mem) |
| 1612 | { |
| 1613 | int node; |
| 1614 | |
| 1615 | mem_cgroup_may_update_nodemask(mem); |
| 1616 | node = mem->last_scanned_node; |
| 1617 | |
| 1618 | node = next_node(node, mem->scan_nodes); |
| 1619 | if (node == MAX_NUMNODES) |
| 1620 | node = first_node(mem->scan_nodes); |
| 1621 | /* |
| 1622 | * We call this when we hit limit, not when pages are added to LRU. |
| 1623 | * No LRU may hold pages because all pages are UNEVICTABLE or |
| 1624 | * memcg is too small and all pages are not on LRU. In that case, |
| 1625 | * we use curret node. |
| 1626 | */ |
| 1627 | if (unlikely(node == MAX_NUMNODES)) |
| 1628 | node = numa_node_id(); |
| 1629 | |
| 1630 | mem->last_scanned_node = node; |
| 1631 | return node; |
| 1632 | } |
| 1633 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1634 | /* |
| 1635 | * Check all nodes whether it contains reclaimable pages or not. |
| 1636 | * For quick scan, we make use of scan_nodes. This will allow us to skip |
| 1637 | * unused nodes. But scan_nodes is lazily updated and may not cotain |
| 1638 | * enough new information. We need to do double check. |
| 1639 | */ |
| 1640 | bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap) |
| 1641 | { |
| 1642 | int nid; |
| 1643 | |
| 1644 | /* |
| 1645 | * quick check...making use of scan_node. |
| 1646 | * We can skip unused nodes. |
| 1647 | */ |
| 1648 | if (!nodes_empty(mem->scan_nodes)) { |
| 1649 | for (nid = first_node(mem->scan_nodes); |
| 1650 | nid < MAX_NUMNODES; |
| 1651 | nid = next_node(nid, mem->scan_nodes)) { |
| 1652 | |
| 1653 | if (test_mem_cgroup_node_reclaimable(mem, nid, noswap)) |
| 1654 | return true; |
| 1655 | } |
| 1656 | } |
| 1657 | /* |
| 1658 | * Check rest of nodes. |
| 1659 | */ |
| 1660 | for_each_node_state(nid, N_HIGH_MEMORY) { |
| 1661 | if (node_isset(nid, mem->scan_nodes)) |
| 1662 | continue; |
| 1663 | if (test_mem_cgroup_node_reclaimable(mem, nid, noswap)) |
| 1664 | return true; |
| 1665 | } |
| 1666 | return false; |
| 1667 | } |
| 1668 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1669 | #else |
| 1670 | int mem_cgroup_select_victim_node(struct mem_cgroup *mem) |
| 1671 | { |
| 1672 | return 0; |
| 1673 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1674 | |
| 1675 | bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap) |
| 1676 | { |
| 1677 | return test_mem_cgroup_node_reclaimable(mem, 0, noswap); |
| 1678 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1679 | #endif |
| 1680 | |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 1681 | static void __mem_cgroup_record_scanstat(unsigned long *stats, |
| 1682 | struct memcg_scanrecord *rec) |
| 1683 | { |
| 1684 | |
| 1685 | stats[SCAN] += rec->nr_scanned[0] + rec->nr_scanned[1]; |
| 1686 | stats[SCAN_ANON] += rec->nr_scanned[0]; |
| 1687 | stats[SCAN_FILE] += rec->nr_scanned[1]; |
| 1688 | |
| 1689 | stats[ROTATE] += rec->nr_rotated[0] + rec->nr_rotated[1]; |
| 1690 | stats[ROTATE_ANON] += rec->nr_rotated[0]; |
| 1691 | stats[ROTATE_FILE] += rec->nr_rotated[1]; |
| 1692 | |
| 1693 | stats[FREED] += rec->nr_freed[0] + rec->nr_freed[1]; |
| 1694 | stats[FREED_ANON] += rec->nr_freed[0]; |
| 1695 | stats[FREED_FILE] += rec->nr_freed[1]; |
| 1696 | |
| 1697 | stats[ELAPSED] += rec->elapsed; |
| 1698 | } |
| 1699 | |
| 1700 | static void mem_cgroup_record_scanstat(struct memcg_scanrecord *rec) |
| 1701 | { |
| 1702 | struct mem_cgroup *mem; |
| 1703 | int context = rec->context; |
| 1704 | |
| 1705 | if (context >= NR_SCAN_CONTEXT) |
| 1706 | return; |
| 1707 | |
| 1708 | mem = rec->mem; |
| 1709 | spin_lock(&mem->scanstat.lock); |
| 1710 | __mem_cgroup_record_scanstat(mem->scanstat.stats[context], rec); |
| 1711 | spin_unlock(&mem->scanstat.lock); |
| 1712 | |
| 1713 | mem = rec->root; |
| 1714 | spin_lock(&mem->scanstat.lock); |
| 1715 | __mem_cgroup_record_scanstat(mem->scanstat.rootstats[context], rec); |
| 1716 | spin_unlock(&mem->scanstat.lock); |
| 1717 | } |
| 1718 | |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1719 | /* |
| 1720 | * Scan the hierarchy if needed to reclaim memory. We remember the last child |
| 1721 | * we reclaimed from, so that we don't end up penalizing one child extensively |
| 1722 | * based on its position in the children list. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1723 | * |
| 1724 | * root_mem is the original ancestor that we've been reclaim from. |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1725 | * |
| 1726 | * We give up and return to the caller when we visit root_mem twice. |
| 1727 | * (other groups can be removed while we're walking....) |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1728 | * |
| 1729 | * If shrink==true, for avoiding to free too much, this returns immedieately. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1730 | */ |
| 1731 | static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem, |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1732 | struct zone *zone, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1733 | gfp_t gfp_mask, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1734 | unsigned long reclaim_options, |
| 1735 | unsigned long *total_scanned) |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1736 | { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1737 | struct mem_cgroup *victim; |
| 1738 | int ret, total = 0; |
| 1739 | int loop = 0; |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1740 | bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP; |
| 1741 | bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1742 | bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT; |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 1743 | struct memcg_scanrecord rec; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1744 | unsigned long excess; |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 1745 | unsigned long scanned; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1746 | |
| 1747 | excess = res_counter_soft_limit_excess(&root_mem->res) >> PAGE_SHIFT; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1748 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1749 | /* If memsw_is_minimum==1, swap-out is of-no-use. */ |
Daisuke Nishimura | 108b6a7 | 2011-07-26 16:08:25 -0700 | [diff] [blame] | 1750 | if (!check_soft && !shrink && root_mem->memsw_is_minimum) |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1751 | noswap = true; |
| 1752 | |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 1753 | if (shrink) |
| 1754 | rec.context = SCAN_BY_SHRINK; |
| 1755 | else if (check_soft) |
| 1756 | rec.context = SCAN_BY_SYSTEM; |
| 1757 | else |
| 1758 | rec.context = SCAN_BY_LIMIT; |
| 1759 | |
| 1760 | rec.root = root_mem; |
| 1761 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1762 | while (1) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1763 | victim = mem_cgroup_select_victim(root_mem); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1764 | if (victim == root_mem) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1765 | loop++; |
KAMEZAWA Hiroyuki | fbc29a2 | 2011-06-15 15:08:46 -0700 | [diff] [blame] | 1766 | /* |
| 1767 | * We are not draining per cpu cached charges during |
| 1768 | * soft limit reclaim because global reclaim doesn't |
| 1769 | * care about charges. It tries to free some memory and |
| 1770 | * charges will not give any. |
| 1771 | */ |
| 1772 | if (!check_soft && loop >= 1) |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 1773 | drain_all_stock_async(root_mem); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1774 | if (loop >= 2) { |
| 1775 | /* |
| 1776 | * If we have not been able to reclaim |
| 1777 | * anything, it might because there are |
| 1778 | * no reclaimable pages under this hierarchy |
| 1779 | */ |
| 1780 | if (!check_soft || !total) { |
| 1781 | css_put(&victim->css); |
| 1782 | break; |
| 1783 | } |
| 1784 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1785 | * We want to do more targeted reclaim. |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1786 | * excess >> 2 is not to excessive so as to |
| 1787 | * reclaim too much, nor too less that we keep |
| 1788 | * coming back to reclaim from this cgroup |
| 1789 | */ |
| 1790 | if (total >= (excess >> 2) || |
| 1791 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) { |
| 1792 | css_put(&victim->css); |
| 1793 | break; |
| 1794 | } |
| 1795 | } |
| 1796 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1797 | if (!mem_cgroup_reclaimable(victim, noswap)) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1798 | /* this cgroup's local usage == 0 */ |
| 1799 | css_put(&victim->css); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1800 | continue; |
| 1801 | } |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 1802 | rec.mem = victim; |
| 1803 | rec.nr_scanned[0] = 0; |
| 1804 | rec.nr_scanned[1] = 0; |
| 1805 | rec.nr_rotated[0] = 0; |
| 1806 | rec.nr_rotated[1] = 0; |
| 1807 | rec.nr_freed[0] = 0; |
| 1808 | rec.nr_freed[1] = 0; |
| 1809 | rec.elapsed = 0; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1810 | /* we use swappiness of local cgroup */ |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1811 | if (check_soft) { |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1812 | ret = mem_cgroup_shrink_node_zone(victim, gfp_mask, |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 1813 | noswap, zone, &rec, &scanned); |
| 1814 | *total_scanned += scanned; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1815 | } else |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1816 | ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 1817 | noswap, &rec); |
| 1818 | mem_cgroup_record_scanstat(&rec); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1819 | css_put(&victim->css); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1820 | /* |
| 1821 | * At shrinking usage, we can't check we should stop here or |
| 1822 | * reclaim more. It's depends on callers. last_scanned_child |
| 1823 | * will work enough for keeping fairness under tree. |
| 1824 | */ |
| 1825 | if (shrink) |
| 1826 | return ret; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1827 | total += ret; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1828 | if (check_soft) { |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1829 | if (!res_counter_soft_limit_excess(&root_mem->res)) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1830 | return total; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1831 | } else if (mem_cgroup_margin(root_mem)) |
Johannes Weiner | 4fd14eb | 2011-05-26 16:25:35 -0700 | [diff] [blame] | 1832 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1833 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1834 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1835 | } |
| 1836 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1837 | /* |
| 1838 | * Check OOM-Killer is already running under our hierarchy. |
| 1839 | * If someone is running, return false. |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1840 | * Has to be called with memcg_oom_lock |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1841 | */ |
| 1842 | static bool mem_cgroup_oom_lock(struct mem_cgroup *mem) |
| 1843 | { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1844 | struct mem_cgroup *iter, *failed = NULL; |
| 1845 | bool cond = true; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1846 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1847 | for_each_mem_cgroup_tree_cond(iter, mem, cond) { |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame^] | 1848 | if (iter->oom_lock) { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1849 | /* |
| 1850 | * this subtree of our hierarchy is already locked |
| 1851 | * so we cannot give a lock. |
| 1852 | */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1853 | failed = iter; |
| 1854 | cond = false; |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame^] | 1855 | } else |
| 1856 | iter->oom_lock = true; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1857 | } |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1858 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1859 | if (!failed) |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame^] | 1860 | return true; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1861 | |
| 1862 | /* |
| 1863 | * OK, we failed to lock the whole subtree so we have to clean up |
| 1864 | * what we set up to the failing subtree |
| 1865 | */ |
| 1866 | cond = true; |
| 1867 | for_each_mem_cgroup_tree_cond(iter, mem, cond) { |
| 1868 | if (iter == failed) { |
| 1869 | cond = false; |
| 1870 | continue; |
| 1871 | } |
| 1872 | iter->oom_lock = false; |
| 1873 | } |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame^] | 1874 | return false; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1875 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1876 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1877 | /* |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1878 | * Has to be called with memcg_oom_lock |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1879 | */ |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1880 | static int mem_cgroup_oom_unlock(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1881 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1882 | struct mem_cgroup *iter; |
| 1883 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1884 | for_each_mem_cgroup_tree(iter, mem) |
| 1885 | iter->oom_lock = false; |
| 1886 | return 0; |
| 1887 | } |
| 1888 | |
| 1889 | static void mem_cgroup_mark_under_oom(struct mem_cgroup *mem) |
| 1890 | { |
| 1891 | struct mem_cgroup *iter; |
| 1892 | |
| 1893 | for_each_mem_cgroup_tree(iter, mem) |
| 1894 | atomic_inc(&iter->under_oom); |
| 1895 | } |
| 1896 | |
| 1897 | static void mem_cgroup_unmark_under_oom(struct mem_cgroup *mem) |
| 1898 | { |
| 1899 | struct mem_cgroup *iter; |
| 1900 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1901 | /* |
| 1902 | * When a new child is created while the hierarchy is under oom, |
| 1903 | * mem_cgroup_oom_lock() may not be called. We have to use |
| 1904 | * atomic_add_unless() here. |
| 1905 | */ |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1906 | for_each_mem_cgroup_tree(iter, mem) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1907 | atomic_add_unless(&iter->under_oom, -1, 0); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1908 | } |
| 1909 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1910 | static DEFINE_SPINLOCK(memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1911 | static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq); |
| 1912 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1913 | struct oom_wait_info { |
| 1914 | struct mem_cgroup *mem; |
| 1915 | wait_queue_t wait; |
| 1916 | }; |
| 1917 | |
| 1918 | static int memcg_oom_wake_function(wait_queue_t *wait, |
| 1919 | unsigned mode, int sync, void *arg) |
| 1920 | { |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1921 | struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg, |
| 1922 | *oom_wait_mem; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1923 | struct oom_wait_info *oom_wait_info; |
| 1924 | |
| 1925 | oom_wait_info = container_of(wait, struct oom_wait_info, wait); |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1926 | oom_wait_mem = oom_wait_info->mem; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1927 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1928 | /* |
| 1929 | * Both of oom_wait_info->mem and wake_mem are stable under us. |
| 1930 | * Then we can use css_is_ancestor without taking care of RCU. |
| 1931 | */ |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1932 | if (!mem_cgroup_same_or_subtree(oom_wait_mem, wake_mem) |
| 1933 | && !mem_cgroup_same_or_subtree(wake_mem, oom_wait_mem)) |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1934 | return 0; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1935 | return autoremove_wake_function(wait, mode, sync, arg); |
| 1936 | } |
| 1937 | |
| 1938 | static void memcg_wakeup_oom(struct mem_cgroup *mem) |
| 1939 | { |
| 1940 | /* for filtering, pass "mem" as argument. */ |
| 1941 | __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem); |
| 1942 | } |
| 1943 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1944 | static void memcg_oom_recover(struct mem_cgroup *mem) |
| 1945 | { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1946 | if (mem && atomic_read(&mem->under_oom)) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1947 | memcg_wakeup_oom(mem); |
| 1948 | } |
| 1949 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1950 | /* |
| 1951 | * try to call OOM killer. returns false if we should exit memory-reclaim loop. |
| 1952 | */ |
| 1953 | bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask) |
| 1954 | { |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1955 | struct oom_wait_info owait; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1956 | bool locked, need_to_kill; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1957 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1958 | owait.mem = mem; |
| 1959 | owait.wait.flags = 0; |
| 1960 | owait.wait.func = memcg_oom_wake_function; |
| 1961 | owait.wait.private = current; |
| 1962 | INIT_LIST_HEAD(&owait.wait.task_list); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1963 | need_to_kill = true; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1964 | mem_cgroup_mark_under_oom(mem); |
| 1965 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1966 | /* At first, try to OOM lock hierarchy under mem.*/ |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1967 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1968 | locked = mem_cgroup_oom_lock(mem); |
| 1969 | /* |
| 1970 | * Even if signal_pending(), we can't quit charge() loop without |
| 1971 | * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL |
| 1972 | * under OOM is always welcomed, use TASK_KILLABLE here. |
| 1973 | */ |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1974 | prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE); |
| 1975 | if (!locked || mem->oom_kill_disable) |
| 1976 | need_to_kill = false; |
| 1977 | if (locked) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1978 | mem_cgroup_oom_notify(mem); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1979 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1980 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1981 | if (need_to_kill) { |
| 1982 | finish_wait(&memcg_oom_waitq, &owait.wait); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1983 | mem_cgroup_out_of_memory(mem, mask); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1984 | } else { |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1985 | schedule(); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1986 | finish_wait(&memcg_oom_waitq, &owait.wait); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1987 | } |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1988 | spin_lock(&memcg_oom_lock); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1989 | if (locked) |
| 1990 | mem_cgroup_oom_unlock(mem); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1991 | memcg_wakeup_oom(mem); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1992 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1993 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1994 | mem_cgroup_unmark_under_oom(mem); |
| 1995 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1996 | if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current)) |
| 1997 | return false; |
| 1998 | /* Give chance to dying process */ |
| 1999 | schedule_timeout(1); |
| 2000 | return true; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 2001 | } |
| 2002 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2003 | /* |
| 2004 | * Currently used to update mapped file statistics, but the routine can be |
| 2005 | * generalized to update other statistics as well. |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2006 | * |
| 2007 | * Notes: Race condition |
| 2008 | * |
| 2009 | * We usually use page_cgroup_lock() for accessing page_cgroup member but |
| 2010 | * it tends to be costly. But considering some conditions, we doesn't need |
| 2011 | * to do so _always_. |
| 2012 | * |
| 2013 | * Considering "charge", lock_page_cgroup() is not required because all |
| 2014 | * file-stat operations happen after a page is attached to radix-tree. There |
| 2015 | * are no race with "charge". |
| 2016 | * |
| 2017 | * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup |
| 2018 | * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even |
| 2019 | * if there are race with "uncharge". Statistics itself is properly handled |
| 2020 | * by flags. |
| 2021 | * |
| 2022 | * Considering "move", this is an only case we see a race. To make the race |
| 2023 | * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are |
| 2024 | * possibility of race condition. If there is, we take a lock. |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2025 | */ |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2026 | |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2027 | void mem_cgroup_update_page_stat(struct page *page, |
| 2028 | enum mem_cgroup_page_stat_item idx, int val) |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2029 | { |
| 2030 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2031 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 2032 | bool need_unlock = false; |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 2033 | unsigned long uninitialized_var(flags); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2034 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2035 | if (unlikely(!pc)) |
| 2036 | return; |
| 2037 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2038 | rcu_read_lock(); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2039 | mem = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2040 | if (unlikely(!mem || !PageCgroupUsed(pc))) |
| 2041 | goto out; |
| 2042 | /* pc->mem_cgroup is unstable ? */ |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2043 | if (unlikely(mem_cgroup_stealed(mem)) || PageTransHuge(page)) { |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2044 | /* take a lock against to access pc->mem_cgroup */ |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 2045 | move_lock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2046 | need_unlock = true; |
| 2047 | mem = pc->mem_cgroup; |
| 2048 | if (!mem || !PageCgroupUsed(pc)) |
| 2049 | goto out; |
| 2050 | } |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2051 | |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2052 | switch (idx) { |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2053 | case MEMCG_NR_FILE_MAPPED: |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2054 | if (val > 0) |
| 2055 | SetPageCgroupFileMapped(pc); |
| 2056 | else if (!page_mapped(page)) |
KAMEZAWA Hiroyuki | 0c270f8 | 2010-10-27 15:33:39 -0700 | [diff] [blame] | 2057 | ClearPageCgroupFileMapped(pc); |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2058 | idx = MEM_CGROUP_STAT_FILE_MAPPED; |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2059 | break; |
| 2060 | default: |
| 2061 | BUG(); |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 2062 | } |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2063 | |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2064 | this_cpu_add(mem->stat->count[idx], val); |
| 2065 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2066 | out: |
| 2067 | if (unlikely(need_unlock)) |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 2068 | move_unlock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2069 | rcu_read_unlock(); |
| 2070 | return; |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2071 | } |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2072 | EXPORT_SYMBOL(mem_cgroup_update_page_stat); |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2073 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2074 | /* |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2075 | * size of first charge trial. "32" comes from vmscan.c's magic value. |
| 2076 | * TODO: maybe necessary to use big numbers in big irons. |
| 2077 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2078 | #define CHARGE_BATCH 32U |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2079 | struct memcg_stock_pcp { |
| 2080 | struct mem_cgroup *cached; /* this never be root cgroup */ |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2081 | unsigned int nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2082 | struct work_struct work; |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2083 | unsigned long flags; |
| 2084 | #define FLUSHING_CACHED_CHARGE (0) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2085 | }; |
| 2086 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2087 | static DEFINE_MUTEX(percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2088 | |
| 2089 | /* |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2090 | * Try to consume stocked charge on this cpu. If success, one page is consumed |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2091 | * from local stock and true is returned. If the stock is 0 or charges from a |
| 2092 | * cgroup which is not current target, returns false. This stock will be |
| 2093 | * refilled. |
| 2094 | */ |
| 2095 | static bool consume_stock(struct mem_cgroup *mem) |
| 2096 | { |
| 2097 | struct memcg_stock_pcp *stock; |
| 2098 | bool ret = true; |
| 2099 | |
| 2100 | stock = &get_cpu_var(memcg_stock); |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2101 | if (mem == stock->cached && stock->nr_pages) |
| 2102 | stock->nr_pages--; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2103 | else /* need to call res_counter_charge */ |
| 2104 | ret = false; |
| 2105 | put_cpu_var(memcg_stock); |
| 2106 | return ret; |
| 2107 | } |
| 2108 | |
| 2109 | /* |
| 2110 | * Returns stocks cached in percpu to res_counter and reset cached information. |
| 2111 | */ |
| 2112 | static void drain_stock(struct memcg_stock_pcp *stock) |
| 2113 | { |
| 2114 | struct mem_cgroup *old = stock->cached; |
| 2115 | |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2116 | if (stock->nr_pages) { |
| 2117 | unsigned long bytes = stock->nr_pages * PAGE_SIZE; |
| 2118 | |
| 2119 | res_counter_uncharge(&old->res, bytes); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2120 | if (do_swap_account) |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2121 | res_counter_uncharge(&old->memsw, bytes); |
| 2122 | stock->nr_pages = 0; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2123 | } |
| 2124 | stock->cached = NULL; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | /* |
| 2128 | * This must be called under preempt disabled or must be called by |
| 2129 | * a thread which is pinned to local cpu. |
| 2130 | */ |
| 2131 | static void drain_local_stock(struct work_struct *dummy) |
| 2132 | { |
| 2133 | struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock); |
| 2134 | drain_stock(stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2135 | clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | /* |
| 2139 | * Cache charges(val) which is from res_counter, to local per_cpu area. |
Greg Thelen | 320cc51 | 2010-03-15 15:27:28 +0100 | [diff] [blame] | 2140 | * This will be consumed by consume_stock() function, later. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2141 | */ |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2142 | static void refill_stock(struct mem_cgroup *mem, unsigned int nr_pages) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2143 | { |
| 2144 | struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock); |
| 2145 | |
| 2146 | if (stock->cached != mem) { /* reset if necessary */ |
| 2147 | drain_stock(stock); |
| 2148 | stock->cached = mem; |
| 2149 | } |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2150 | stock->nr_pages += nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2151 | put_cpu_var(memcg_stock); |
| 2152 | } |
| 2153 | |
| 2154 | /* |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2155 | * Drains all per-CPU charge caches for given root_mem resp. subtree |
| 2156 | * of the hierarchy under it. sync flag says whether we should block |
| 2157 | * until the work is done. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2158 | */ |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2159 | static void drain_all_stock(struct mem_cgroup *root_mem, bool sync) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2160 | { |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2161 | int cpu, curcpu; |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2162 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2163 | /* Notify other cpus that system-wide "drain" is running */ |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2164 | get_online_cpus(); |
Johannes Weiner | 5af12d0 | 2011-08-25 15:59:07 -0700 | [diff] [blame] | 2165 | curcpu = get_cpu(); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2166 | for_each_online_cpu(cpu) { |
| 2167 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2168 | struct mem_cgroup *mem; |
| 2169 | |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2170 | mem = stock->cached; |
Michal Hocko | d1a05b6 | 2011-07-26 16:08:27 -0700 | [diff] [blame] | 2171 | if (!mem || !stock->nr_pages) |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2172 | continue; |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 2173 | if (!mem_cgroup_same_or_subtree(root_mem, mem)) |
| 2174 | continue; |
Michal Hocko | d1a05b6 | 2011-07-26 16:08:27 -0700 | [diff] [blame] | 2175 | if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) { |
| 2176 | if (cpu == curcpu) |
| 2177 | drain_local_stock(&stock->work); |
| 2178 | else |
| 2179 | schedule_work_on(cpu, &stock->work); |
| 2180 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2181 | } |
Johannes Weiner | 5af12d0 | 2011-08-25 15:59:07 -0700 | [diff] [blame] | 2182 | put_cpu(); |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2183 | |
| 2184 | if (!sync) |
| 2185 | goto out; |
| 2186 | |
| 2187 | for_each_online_cpu(cpu) { |
| 2188 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2189 | if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2190 | flush_work(&stock->work); |
| 2191 | } |
| 2192 | out: |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2193 | put_online_cpus(); |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | /* |
| 2197 | * Tries to drain stocked charges in other cpus. This function is asynchronous |
| 2198 | * and just put a work per cpu for draining localy on each cpu. Caller can |
| 2199 | * expects some charges will be back to res_counter later but cannot wait for |
| 2200 | * it. |
| 2201 | */ |
| 2202 | static void drain_all_stock_async(struct mem_cgroup *root_mem) |
| 2203 | { |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2204 | /* |
| 2205 | * If someone calls draining, avoid adding more kworker runs. |
| 2206 | */ |
| 2207 | if (!mutex_trylock(&percpu_charge_mutex)) |
| 2208 | return; |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2209 | drain_all_stock(root_mem, false); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2210 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | /* This is a synchronous drain interface. */ |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2214 | static void drain_all_stock_sync(struct mem_cgroup *root_mem) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2215 | { |
| 2216 | /* called when force_empty is called */ |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2217 | mutex_lock(&percpu_charge_mutex); |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2218 | drain_all_stock(root_mem, true); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2219 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2220 | } |
| 2221 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2222 | /* |
| 2223 | * This function drains percpu counter value from DEAD cpu and |
| 2224 | * move it to local cpu. Note that this function can be preempted. |
| 2225 | */ |
| 2226 | static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *mem, int cpu) |
| 2227 | { |
| 2228 | int i; |
| 2229 | |
| 2230 | spin_lock(&mem->pcp_counter_lock); |
| 2231 | for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 2232 | long x = per_cpu(mem->stat->count[i], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2233 | |
| 2234 | per_cpu(mem->stat->count[i], cpu) = 0; |
| 2235 | mem->nocpu_base.count[i] += x; |
| 2236 | } |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2237 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) { |
| 2238 | unsigned long x = per_cpu(mem->stat->events[i], cpu); |
| 2239 | |
| 2240 | per_cpu(mem->stat->events[i], cpu) = 0; |
| 2241 | mem->nocpu_base.events[i] += x; |
| 2242 | } |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2243 | /* need to clear ON_MOVE value, works as a kind of lock. */ |
| 2244 | per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0; |
| 2245 | spin_unlock(&mem->pcp_counter_lock); |
| 2246 | } |
| 2247 | |
| 2248 | static void synchronize_mem_cgroup_on_move(struct mem_cgroup *mem, int cpu) |
| 2249 | { |
| 2250 | int idx = MEM_CGROUP_ON_MOVE; |
| 2251 | |
| 2252 | spin_lock(&mem->pcp_counter_lock); |
| 2253 | per_cpu(mem->stat->count[idx], cpu) = mem->nocpu_base.count[idx]; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2254 | spin_unlock(&mem->pcp_counter_lock); |
| 2255 | } |
| 2256 | |
| 2257 | static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb, |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2258 | unsigned long action, |
| 2259 | void *hcpu) |
| 2260 | { |
| 2261 | int cpu = (unsigned long)hcpu; |
| 2262 | struct memcg_stock_pcp *stock; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2263 | struct mem_cgroup *iter; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2264 | |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2265 | if ((action == CPU_ONLINE)) { |
| 2266 | for_each_mem_cgroup_all(iter) |
| 2267 | synchronize_mem_cgroup_on_move(iter, cpu); |
| 2268 | return NOTIFY_OK; |
| 2269 | } |
| 2270 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2271 | if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2272 | return NOTIFY_OK; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2273 | |
| 2274 | for_each_mem_cgroup_all(iter) |
| 2275 | mem_cgroup_drain_pcp_counter(iter, cpu); |
| 2276 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2277 | stock = &per_cpu(memcg_stock, cpu); |
| 2278 | drain_stock(stock); |
| 2279 | return NOTIFY_OK; |
| 2280 | } |
| 2281 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2282 | |
| 2283 | /* See __mem_cgroup_try_charge() for details */ |
| 2284 | enum { |
| 2285 | CHARGE_OK, /* success */ |
| 2286 | CHARGE_RETRY, /* need to retry but retry is not bad */ |
| 2287 | CHARGE_NOMEM, /* we can't do more. return -ENOMEM */ |
| 2288 | CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */ |
| 2289 | CHARGE_OOM_DIE, /* the current is killed because of OOM */ |
| 2290 | }; |
| 2291 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2292 | static int mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask, |
| 2293 | unsigned int nr_pages, bool oom_check) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2294 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2295 | unsigned long csize = nr_pages * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2296 | struct mem_cgroup *mem_over_limit; |
| 2297 | struct res_counter *fail_res; |
| 2298 | unsigned long flags = 0; |
| 2299 | int ret; |
| 2300 | |
| 2301 | ret = res_counter_charge(&mem->res, csize, &fail_res); |
| 2302 | |
| 2303 | if (likely(!ret)) { |
| 2304 | if (!do_swap_account) |
| 2305 | return CHARGE_OK; |
| 2306 | ret = res_counter_charge(&mem->memsw, csize, &fail_res); |
| 2307 | if (likely(!ret)) |
| 2308 | return CHARGE_OK; |
| 2309 | |
KAMEZAWA Hiroyuki | 01c88e2 | 2011-01-25 15:07:27 -0800 | [diff] [blame] | 2310 | res_counter_uncharge(&mem->res, csize); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2311 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw); |
| 2312 | flags |= MEM_CGROUP_RECLAIM_NOSWAP; |
| 2313 | } else |
| 2314 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, res); |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2315 | /* |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2316 | * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch |
| 2317 | * of regular pages (CHARGE_BATCH), or a single regular page (1). |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2318 | * |
| 2319 | * Never reclaim on behalf of optional batching, retry with a |
| 2320 | * single page instead. |
| 2321 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2322 | if (nr_pages == CHARGE_BATCH) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2323 | return CHARGE_RETRY; |
| 2324 | |
| 2325 | if (!(gfp_mask & __GFP_WAIT)) |
| 2326 | return CHARGE_WOULDBLOCK; |
| 2327 | |
| 2328 | ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 2329 | gfp_mask, flags, NULL); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2330 | if (mem_cgroup_margin(mem_over_limit) >= nr_pages) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2331 | return CHARGE_RETRY; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2332 | /* |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2333 | * Even though the limit is exceeded at this point, reclaim |
| 2334 | * may have been able to free some pages. Retry the charge |
| 2335 | * before killing the task. |
| 2336 | * |
| 2337 | * Only for regular pages, though: huge pages are rather |
| 2338 | * unlikely to succeed so close to the limit, and we fall back |
| 2339 | * to regular pages anyway in case of failure. |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2340 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2341 | if (nr_pages == 1 && ret) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2342 | return CHARGE_RETRY; |
| 2343 | |
| 2344 | /* |
| 2345 | * At task move, charge accounts can be doubly counted. So, it's |
| 2346 | * better to wait until the end of task_move if something is going on. |
| 2347 | */ |
| 2348 | if (mem_cgroup_wait_acct_move(mem_over_limit)) |
| 2349 | return CHARGE_RETRY; |
| 2350 | |
| 2351 | /* If we don't need to call oom-killer at el, return immediately */ |
| 2352 | if (!oom_check) |
| 2353 | return CHARGE_NOMEM; |
| 2354 | /* check OOM */ |
| 2355 | if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask)) |
| 2356 | return CHARGE_OOM_DIE; |
| 2357 | |
| 2358 | return CHARGE_RETRY; |
| 2359 | } |
| 2360 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2361 | /* |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2362 | * Unlike exported interface, "oom" parameter is added. if oom==true, |
| 2363 | * oom-killer can be invoked. |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2364 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2365 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2366 | gfp_t gfp_mask, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2367 | unsigned int nr_pages, |
| 2368 | struct mem_cgroup **memcg, |
| 2369 | bool oom) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2370 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2371 | unsigned int batch = max(CHARGE_BATCH, nr_pages); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2372 | int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
| 2373 | struct mem_cgroup *mem = NULL; |
| 2374 | int ret; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2375 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2376 | /* |
| 2377 | * Unlike gloval-vm's OOM-kill, we're not in memory shortage |
| 2378 | * in system level. So, allow to go ahead dying process in addition to |
| 2379 | * MEMDIE process. |
| 2380 | */ |
| 2381 | if (unlikely(test_thread_flag(TIF_MEMDIE) |
| 2382 | || fatal_signal_pending(current))) |
| 2383 | goto bypass; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2384 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2385 | /* |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2386 | * We always charge the cgroup the mm_struct belongs to. |
| 2387 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2388 | * thread group leader migrates. It's possible that mm is not |
| 2389 | * set, if so charge the init_mm (happens for pagecache usage). |
| 2390 | */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2391 | if (!*memcg && !mm) |
| 2392 | goto bypass; |
| 2393 | again: |
| 2394 | if (*memcg) { /* css should be a valid one */ |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2395 | mem = *memcg; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2396 | VM_BUG_ON(css_is_removed(&mem->css)); |
| 2397 | if (mem_cgroup_is_root(mem)) |
| 2398 | goto done; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2399 | if (nr_pages == 1 && consume_stock(mem)) |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2400 | goto done; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2401 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2402 | } else { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2403 | struct task_struct *p; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2404 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2405 | rcu_read_lock(); |
| 2406 | p = rcu_dereference(mm->owner); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2407 | /* |
KAMEZAWA Hiroyuki | ebb76ce | 2010-12-29 14:07:11 -0800 | [diff] [blame] | 2408 | * Because we don't have task_lock(), "p" can exit. |
| 2409 | * In that case, "mem" can point to root or p can be NULL with |
| 2410 | * race with swapoff. Then, we have small risk of mis-accouning. |
| 2411 | * But such kind of mis-account by race always happens because |
| 2412 | * we don't have cgroup_mutex(). It's overkill and we allo that |
| 2413 | * small race, here. |
| 2414 | * (*) swapoff at el will charge against mm-struct not against |
| 2415 | * task-struct. So, mm->owner can be NULL. |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2416 | */ |
| 2417 | mem = mem_cgroup_from_task(p); |
KAMEZAWA Hiroyuki | ebb76ce | 2010-12-29 14:07:11 -0800 | [diff] [blame] | 2418 | if (!mem || mem_cgroup_is_root(mem)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2419 | rcu_read_unlock(); |
| 2420 | goto done; |
| 2421 | } |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2422 | if (nr_pages == 1 && consume_stock(mem)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2423 | /* |
| 2424 | * It seems dagerous to access memcg without css_get(). |
| 2425 | * But considering how consume_stok works, it's not |
| 2426 | * necessary. If consume_stock success, some charges |
| 2427 | * from this memcg are cached on this cpu. So, we |
| 2428 | * don't need to call css_get()/css_tryget() before |
| 2429 | * calling consume_stock(). |
| 2430 | */ |
| 2431 | rcu_read_unlock(); |
| 2432 | goto done; |
| 2433 | } |
| 2434 | /* after here, we may be blocked. we need to get refcnt */ |
| 2435 | if (!css_tryget(&mem->css)) { |
| 2436 | rcu_read_unlock(); |
| 2437 | goto again; |
| 2438 | } |
| 2439 | rcu_read_unlock(); |
| 2440 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2441 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2442 | do { |
| 2443 | bool oom_check; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2444 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2445 | /* If killed, bypass charge */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2446 | if (fatal_signal_pending(current)) { |
| 2447 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2448 | goto bypass; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2449 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2450 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2451 | oom_check = false; |
| 2452 | if (oom && !nr_oom_retries) { |
| 2453 | oom_check = true; |
| 2454 | nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
| 2455 | } |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 2456 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2457 | ret = mem_cgroup_do_charge(mem, gfp_mask, batch, oom_check); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2458 | switch (ret) { |
| 2459 | case CHARGE_OK: |
| 2460 | break; |
| 2461 | case CHARGE_RETRY: /* not in OOM situation but retry */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2462 | batch = nr_pages; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2463 | css_put(&mem->css); |
| 2464 | mem = NULL; |
| 2465 | goto again; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2466 | case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2467 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2468 | goto nomem; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2469 | case CHARGE_NOMEM: /* OOM routine works */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2470 | if (!oom) { |
| 2471 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2472 | goto nomem; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2473 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2474 | /* If oom, we never return -ENOMEM */ |
| 2475 | nr_oom_retries--; |
| 2476 | break; |
| 2477 | case CHARGE_OOM_DIE: /* Killed by OOM Killer */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2478 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2479 | goto bypass; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2480 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2481 | } while (ret != CHARGE_OK); |
| 2482 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2483 | if (batch > nr_pages) |
| 2484 | refill_stock(mem, batch - nr_pages); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2485 | css_put(&mem->css); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2486 | done: |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2487 | *memcg = mem; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2488 | return 0; |
| 2489 | nomem: |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2490 | *memcg = NULL; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2491 | return -ENOMEM; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2492 | bypass: |
| 2493 | *memcg = NULL; |
| 2494 | return 0; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2495 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2496 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2497 | /* |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2498 | * Somemtimes we have to undo a charge we got by try_charge(). |
| 2499 | * This function is for that and do uncharge, put css's refcnt. |
| 2500 | * gotten by try_charge(). |
| 2501 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2502 | static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem, |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2503 | unsigned int nr_pages) |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2504 | { |
| 2505 | if (!mem_cgroup_is_root(mem)) { |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2506 | unsigned long bytes = nr_pages * PAGE_SIZE; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2507 | |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2508 | res_counter_uncharge(&mem->res, bytes); |
| 2509 | if (do_swap_account) |
| 2510 | res_counter_uncharge(&mem->memsw, bytes); |
| 2511 | } |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | /* |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2515 | * A helper function to get mem_cgroup from ID. must be called under |
| 2516 | * rcu_read_lock(). The caller must check css_is_removed() or some if |
| 2517 | * it's concern. (dropping refcnt from swap can be called against removed |
| 2518 | * memcg.) |
| 2519 | */ |
| 2520 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) |
| 2521 | { |
| 2522 | struct cgroup_subsys_state *css; |
| 2523 | |
| 2524 | /* ID 0 is unused ID */ |
| 2525 | if (!id) |
| 2526 | return NULL; |
| 2527 | css = css_lookup(&mem_cgroup_subsys, id); |
| 2528 | if (!css) |
| 2529 | return NULL; |
| 2530 | return container_of(css, struct mem_cgroup, css); |
| 2531 | } |
| 2532 | |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2533 | struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2534 | { |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2535 | struct mem_cgroup *mem = NULL; |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2536 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2537 | unsigned short id; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2538 | swp_entry_t ent; |
| 2539 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2540 | VM_BUG_ON(!PageLocked(page)); |
| 2541 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2542 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2543 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2544 | if (PageCgroupUsed(pc)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2545 | mem = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2546 | if (mem && !css_tryget(&mem->css)) |
| 2547 | mem = NULL; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2548 | } else if (PageSwapCache(page)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2549 | ent.val = page_private(page); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2550 | id = lookup_swap_cgroup(ent); |
| 2551 | rcu_read_lock(); |
| 2552 | mem = mem_cgroup_lookup(id); |
| 2553 | if (mem && !css_tryget(&mem->css)) |
| 2554 | mem = NULL; |
| 2555 | rcu_read_unlock(); |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2556 | } |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2557 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2558 | return mem; |
| 2559 | } |
| 2560 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2561 | static void __mem_cgroup_commit_charge(struct mem_cgroup *mem, |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2562 | struct page *page, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2563 | unsigned int nr_pages, |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2564 | struct page_cgroup *pc, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2565 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2566 | { |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2567 | lock_page_cgroup(pc); |
| 2568 | if (unlikely(PageCgroupUsed(pc))) { |
| 2569 | unlock_page_cgroup(pc); |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2570 | __mem_cgroup_cancel_charge(mem, nr_pages); |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2571 | return; |
| 2572 | } |
| 2573 | /* |
| 2574 | * we don't need page_cgroup_lock about tail pages, becase they are not |
| 2575 | * accessed by any other context at this point. |
| 2576 | */ |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2577 | pc->mem_cgroup = mem; |
KAMEZAWA Hiroyuki | 261fb61 | 2009-09-23 15:56:33 -0700 | [diff] [blame] | 2578 | /* |
| 2579 | * We access a page_cgroup asynchronously without lock_page_cgroup(). |
| 2580 | * Especially when a page_cgroup is taken from a page, pc->mem_cgroup |
| 2581 | * is accessed after testing USED bit. To make pc->mem_cgroup visible |
| 2582 | * before USED bit, we need memory barrier here. |
| 2583 | * See mem_cgroup_add_lru_list(), etc. |
| 2584 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2585 | smp_wmb(); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 2586 | switch (ctype) { |
| 2587 | case MEM_CGROUP_CHARGE_TYPE_CACHE: |
| 2588 | case MEM_CGROUP_CHARGE_TYPE_SHMEM: |
| 2589 | SetPageCgroupCache(pc); |
| 2590 | SetPageCgroupUsed(pc); |
| 2591 | break; |
| 2592 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
| 2593 | ClearPageCgroupCache(pc); |
| 2594 | SetPageCgroupUsed(pc); |
| 2595 | break; |
| 2596 | default: |
| 2597 | break; |
| 2598 | } |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2599 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2600 | mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), nr_pages); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2601 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 2602 | /* |
| 2603 | * "charge_statistics" updated event counter. Then, check it. |
| 2604 | * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree. |
| 2605 | * if they exceeds softlimit. |
| 2606 | */ |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2607 | memcg_check_events(mem, page); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2608 | } |
| 2609 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2610 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 2611 | |
| 2612 | #define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\ |
| 2613 | (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION)) |
| 2614 | /* |
| 2615 | * Because tail pages are not marked as "used", set it. We're under |
| 2616 | * zone->lru_lock, 'splitting on pmd' and compund_lock. |
| 2617 | */ |
| 2618 | void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail) |
| 2619 | { |
| 2620 | struct page_cgroup *head_pc = lookup_page_cgroup(head); |
| 2621 | struct page_cgroup *tail_pc = lookup_page_cgroup(tail); |
| 2622 | unsigned long flags; |
| 2623 | |
KAMEZAWA Hiroyuki | 3d37c4a | 2011-01-25 15:07:28 -0800 | [diff] [blame] | 2624 | if (mem_cgroup_disabled()) |
| 2625 | return; |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2626 | /* |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2627 | * We have no races with charge/uncharge but will have races with |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2628 | * page state accounting. |
| 2629 | */ |
| 2630 | move_lock_page_cgroup(head_pc, &flags); |
| 2631 | |
| 2632 | tail_pc->mem_cgroup = head_pc->mem_cgroup; |
| 2633 | smp_wmb(); /* see __commit_charge() */ |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2634 | if (PageCgroupAcctLRU(head_pc)) { |
| 2635 | enum lru_list lru; |
| 2636 | struct mem_cgroup_per_zone *mz; |
| 2637 | |
| 2638 | /* |
| 2639 | * LRU flags cannot be copied because we need to add tail |
| 2640 | *.page to LRU by generic call and our hook will be called. |
| 2641 | * We hold lru_lock, then, reduce counter directly. |
| 2642 | */ |
| 2643 | lru = page_lru(head); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 2644 | mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2645 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
| 2646 | } |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2647 | tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT; |
| 2648 | move_unlock_page_cgroup(head_pc, &flags); |
| 2649 | } |
| 2650 | #endif |
| 2651 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2652 | /** |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2653 | * mem_cgroup_move_account - move account of the page |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2654 | * @page: the page |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2655 | * @nr_pages: number of regular pages (>1 for huge pages) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2656 | * @pc: page_cgroup of the page. |
| 2657 | * @from: mem_cgroup which the page is moved from. |
| 2658 | * @to: mem_cgroup which the page is moved to. @from != @to. |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2659 | * @uncharge: whether we should call uncharge and css_put against @from. |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2660 | * |
| 2661 | * The caller must confirm following. |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2662 | * - page is not on LRU (isolate_page() is useful.) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2663 | * - compound_lock is held when nr_pages > 1 |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2664 | * |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2665 | * This function doesn't do "charge" nor css_get to new cgroup. It should be |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2666 | * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2667 | * true, this function does "uncharge" from old cgroup, but it doesn't if |
| 2668 | * @uncharge is false, so a caller should do "uncharge". |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2669 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2670 | static int mem_cgroup_move_account(struct page *page, |
| 2671 | unsigned int nr_pages, |
| 2672 | struct page_cgroup *pc, |
| 2673 | struct mem_cgroup *from, |
| 2674 | struct mem_cgroup *to, |
| 2675 | bool uncharge) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2676 | { |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2677 | unsigned long flags; |
| 2678 | int ret; |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2679 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2680 | VM_BUG_ON(from == to); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2681 | VM_BUG_ON(PageLRU(page)); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2682 | /* |
| 2683 | * The page is isolated from LRU. So, collapse function |
| 2684 | * will not handle this page. But page splitting can happen. |
| 2685 | * Do this check under compound_page_lock(). The caller should |
| 2686 | * hold it. |
| 2687 | */ |
| 2688 | ret = -EBUSY; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2689 | if (nr_pages > 1 && !PageTransHuge(page)) |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2690 | goto out; |
| 2691 | |
| 2692 | lock_page_cgroup(pc); |
| 2693 | |
| 2694 | ret = -EINVAL; |
| 2695 | if (!PageCgroupUsed(pc) || pc->mem_cgroup != from) |
| 2696 | goto unlock; |
| 2697 | |
| 2698 | move_lock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2699 | |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 2700 | if (PageCgroupFileMapped(pc)) { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 2701 | /* Update mapped_file data for mem_cgroup */ |
| 2702 | preempt_disable(); |
| 2703 | __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 2704 | __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 2705 | preempt_enable(); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2706 | } |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2707 | mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2708 | if (uncharge) |
| 2709 | /* This is not "cancel", but cancel_charge does all we need. */ |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2710 | __mem_cgroup_cancel_charge(from, nr_pages); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2711 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2712 | /* caller should have done css_get */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2713 | pc->mem_cgroup = to; |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2714 | mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2715 | /* |
| 2716 | * We charges against "to" which may not have any tasks. Then, "to" |
| 2717 | * can be under rmdir(). But in current implementation, caller of |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 2718 | * this function is just force_empty() and move charge, so it's |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2719 | * guaranteed that "to" is never removed. So, we don't check rmdir |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 2720 | * status here. |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2721 | */ |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2722 | move_unlock_page_cgroup(pc, &flags); |
| 2723 | ret = 0; |
| 2724 | unlock: |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2725 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 2726 | /* |
| 2727 | * check events |
| 2728 | */ |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2729 | memcg_check_events(to, page); |
| 2730 | memcg_check_events(from, page); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2731 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2732 | return ret; |
| 2733 | } |
| 2734 | |
| 2735 | /* |
| 2736 | * move charges to its parent. |
| 2737 | */ |
| 2738 | |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2739 | static int mem_cgroup_move_parent(struct page *page, |
| 2740 | struct page_cgroup *pc, |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2741 | struct mem_cgroup *child, |
| 2742 | gfp_t gfp_mask) |
| 2743 | { |
| 2744 | struct cgroup *cg = child->css.cgroup; |
| 2745 | struct cgroup *pcg = cg->parent; |
| 2746 | struct mem_cgroup *parent; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2747 | unsigned int nr_pages; |
Andrew Morton | 4be4489 | 2011-03-23 16:42:39 -0700 | [diff] [blame] | 2748 | unsigned long uninitialized_var(flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2749 | int ret; |
| 2750 | |
| 2751 | /* Is ROOT ? */ |
| 2752 | if (!pcg) |
| 2753 | return -EINVAL; |
| 2754 | |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2755 | ret = -EBUSY; |
| 2756 | if (!get_page_unless_zero(page)) |
| 2757 | goto out; |
| 2758 | if (isolate_lru_page(page)) |
| 2759 | goto put; |
KAMEZAWA Hiroyuki | 52dbb90 | 2011-01-25 15:07:29 -0800 | [diff] [blame] | 2760 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2761 | nr_pages = hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2762 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2763 | parent = mem_cgroup_from_cont(pcg); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2764 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2765 | if (ret || !parent) |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2766 | goto put_back; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2767 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2768 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2769 | flags = compound_lock_irqsave(page); |
| 2770 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2771 | ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2772 | if (ret) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2773 | __mem_cgroup_cancel_charge(parent, nr_pages); |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 2774 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2775 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2776 | compound_unlock_irqrestore(page, flags); |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 2777 | put_back: |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2778 | putback_lru_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2779 | put: |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 2780 | put_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2781 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2782 | return ret; |
| 2783 | } |
| 2784 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2785 | /* |
| 2786 | * Charge the memory controller for page usage. |
| 2787 | * Return |
| 2788 | * 0 if the charge was successful |
| 2789 | * < 0 if the cgroup is over its limit |
| 2790 | */ |
| 2791 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2792 | gfp_t gfp_mask, enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2793 | { |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2794 | struct mem_cgroup *mem = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2795 | unsigned int nr_pages = 1; |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 2796 | struct page_cgroup *pc; |
| 2797 | bool oom = true; |
| 2798 | int ret; |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2799 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2800 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2801 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2802 | VM_BUG_ON(!PageTransHuge(page)); |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 2803 | /* |
| 2804 | * Never OOM-kill a process for a huge page. The |
| 2805 | * fault handler will fall back to regular pages. |
| 2806 | */ |
| 2807 | oom = false; |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2808 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2809 | |
| 2810 | pc = lookup_page_cgroup(page); |
Johannes Weiner | af4a662 | 2011-03-23 16:42:24 -0700 | [diff] [blame] | 2811 | BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */ |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2812 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2813 | ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &mem, oom); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2814 | if (ret || !mem) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2815 | return ret; |
| 2816 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2817 | __mem_cgroup_commit_charge(mem, page, nr_pages, pc, ctype); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2818 | return 0; |
| 2819 | } |
| 2820 | |
| 2821 | int mem_cgroup_newpage_charge(struct page *page, |
| 2822 | struct mm_struct *mm, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2823 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2824 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 2825 | return 0; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2826 | /* |
| 2827 | * If already mapped, we don't have to account. |
| 2828 | * If page cache, page->mapping has address_space. |
| 2829 | * But page->mapping may have out-of-use anon_vma pointer, |
| 2830 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping |
| 2831 | * is NULL. |
| 2832 | */ |
| 2833 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) |
| 2834 | return 0; |
| 2835 | if (unlikely(!mm)) |
| 2836 | mm = &init_mm; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2837 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2838 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2839 | } |
| 2840 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2841 | static void |
| 2842 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 2843 | enum charge_type ctype); |
| 2844 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2845 | static void |
| 2846 | __mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *mem, |
| 2847 | enum charge_type ctype) |
| 2848 | { |
| 2849 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 2850 | /* |
| 2851 | * In some case, SwapCache, FUSE(splice_buf->radixtree), the page |
| 2852 | * is already on LRU. It means the page may on some other page_cgroup's |
| 2853 | * LRU. Take care of it. |
| 2854 | */ |
| 2855 | mem_cgroup_lru_del_before_commit(page); |
| 2856 | __mem_cgroup_commit_charge(mem, page, 1, pc, ctype); |
| 2857 | mem_cgroup_lru_add_after_commit(page); |
| 2858 | return; |
| 2859 | } |
| 2860 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 2861 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 2862 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2863 | { |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2864 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2865 | int ret; |
| 2866 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2867 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 2868 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2869 | if (PageCompound(page)) |
| 2870 | return 0; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2871 | |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2872 | if (unlikely(!mm)) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2873 | mm = &init_mm; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2874 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2875 | if (page_is_file_cache(page)) { |
| 2876 | ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &mem, true); |
| 2877 | if (ret || !mem) |
| 2878 | return ret; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2879 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2880 | /* |
| 2881 | * FUSE reuses pages without going through the final |
| 2882 | * put that would remove them from the LRU list, make |
| 2883 | * sure that they get relinked properly. |
| 2884 | */ |
| 2885 | __mem_cgroup_commit_charge_lrucare(page, mem, |
| 2886 | MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 2887 | return ret; |
| 2888 | } |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2889 | /* shmem */ |
| 2890 | if (PageSwapCache(page)) { |
| 2891 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
| 2892 | if (!ret) |
| 2893 | __mem_cgroup_commit_charge_swapin(page, mem, |
| 2894 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
| 2895 | } else |
| 2896 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2897 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2898 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2899 | return ret; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 2900 | } |
| 2901 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2902 | /* |
| 2903 | * While swap-in, try_charge -> commit or cancel, the page is locked. |
| 2904 | * 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] | 2905 | * struct page_cgroup is acquired. This refcnt will be consumed by |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2906 | * "commit()" or removed by "cancel()" |
| 2907 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2908 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
| 2909 | struct page *page, |
| 2910 | gfp_t mask, struct mem_cgroup **ptr) |
| 2911 | { |
| 2912 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2913 | int ret; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2914 | |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 2915 | *ptr = NULL; |
| 2916 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2917 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2918 | return 0; |
| 2919 | |
| 2920 | if (!do_swap_account) |
| 2921 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2922 | /* |
| 2923 | * A racing thread's fault, or swapoff, may have already updated |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2924 | * the pte, and even removed page from swap cache: in those cases |
| 2925 | * do_swap_page()'s pte_same() test will fail; but there's also a |
| 2926 | * KSM case which does need to charge the page. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2927 | */ |
| 2928 | if (!PageSwapCache(page)) |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2929 | goto charge_cur_mm; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2930 | mem = try_get_mem_cgroup_from_page(page); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2931 | if (!mem) |
| 2932 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2933 | *ptr = mem; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2934 | ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2935 | css_put(&mem->css); |
| 2936 | return ret; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2937 | charge_cur_mm: |
| 2938 | if (unlikely(!mm)) |
| 2939 | mm = &init_mm; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2940 | return __mem_cgroup_try_charge(mm, mask, 1, ptr, true); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2941 | } |
| 2942 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2943 | static void |
| 2944 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 2945 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2946 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2947 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2948 | return; |
| 2949 | if (!ptr) |
| 2950 | return; |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2951 | cgroup_exclude_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2952 | |
| 2953 | __mem_cgroup_commit_charge_lrucare(page, ptr, ctype); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2954 | /* |
| 2955 | * Now swap is on-memory. This means this page may be |
| 2956 | * counted both as mem and swap....double count. |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2957 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
| 2958 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() |
| 2959 | * may call delete_from_swap_cache() before reach here. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2960 | */ |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2961 | if (do_swap_account && PageSwapCache(page)) { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2962 | swp_entry_t ent = {.val = page_private(page)}; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2963 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2964 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2965 | |
| 2966 | id = swap_cgroup_record(ent, 0); |
| 2967 | rcu_read_lock(); |
| 2968 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2969 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2970 | /* |
| 2971 | * This recorded memcg can be obsolete one. So, avoid |
| 2972 | * calling css_tryget |
| 2973 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2974 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 2975 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2976 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2977 | mem_cgroup_put(memcg); |
| 2978 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2979 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2980 | } |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2981 | /* |
| 2982 | * At swapin, we may charge account against cgroup which has no tasks. |
| 2983 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 2984 | * In that case, we need to call pre_destroy() again. check it here. |
| 2985 | */ |
| 2986 | cgroup_release_and_wakeup_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2987 | } |
| 2988 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2989 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
| 2990 | { |
| 2991 | __mem_cgroup_commit_charge_swapin(page, ptr, |
| 2992 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 2993 | } |
| 2994 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2995 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem) |
| 2996 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2997 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2998 | return; |
| 2999 | if (!mem) |
| 3000 | return; |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 3001 | __mem_cgroup_cancel_charge(mem, 1); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3002 | } |
| 3003 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3004 | static void mem_cgroup_do_uncharge(struct mem_cgroup *mem, |
| 3005 | unsigned int nr_pages, |
| 3006 | const enum charge_type ctype) |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3007 | { |
| 3008 | struct memcg_batch_info *batch = NULL; |
| 3009 | bool uncharge_memsw = true; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3010 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3011 | /* If swapout, usage of swap doesn't decrease */ |
| 3012 | if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) |
| 3013 | uncharge_memsw = false; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3014 | |
| 3015 | batch = ¤t->memcg_batch; |
| 3016 | /* |
| 3017 | * In usual, we do css_get() when we remember memcg pointer. |
| 3018 | * But in this case, we keep res->usage until end of a series of |
| 3019 | * uncharges. Then, it's ok to ignore memcg's refcnt. |
| 3020 | */ |
| 3021 | if (!batch->memcg) |
| 3022 | batch->memcg = mem; |
| 3023 | /* |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3024 | * do_batch > 0 when unmapping pages or inode invalidate/truncate. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3025 | * In those cases, all pages freed continuously can be expected to be in |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3026 | * the same cgroup and we have chance to coalesce uncharges. |
| 3027 | * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE) |
| 3028 | * because we want to do uncharge as soon as possible. |
| 3029 | */ |
| 3030 | |
| 3031 | if (!batch->do_batch || test_thread_flag(TIF_MEMDIE)) |
| 3032 | goto direct_uncharge; |
| 3033 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3034 | if (nr_pages > 1) |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 3035 | goto direct_uncharge; |
| 3036 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3037 | /* |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3038 | * In typical case, batch->memcg == mem. This means we can |
| 3039 | * merge a series of uncharges to an uncharge of res_counter. |
| 3040 | * If not, we uncharge res_counter ony by one. |
| 3041 | */ |
| 3042 | if (batch->memcg != mem) |
| 3043 | goto direct_uncharge; |
| 3044 | /* remember freed charge and uncharge it later */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3045 | batch->nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3046 | if (uncharge_memsw) |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3047 | batch->memsw_nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3048 | return; |
| 3049 | direct_uncharge: |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3050 | res_counter_uncharge(&mem->res, nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3051 | if (uncharge_memsw) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3052 | res_counter_uncharge(&mem->memsw, nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3053 | if (unlikely(batch->memcg != mem)) |
| 3054 | memcg_oom_recover(mem); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3055 | return; |
| 3056 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3057 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 3058 | /* |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3059 | * uncharge if !page_mapped(page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3060 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3061 | static struct mem_cgroup * |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3062 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3063 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3064 | struct mem_cgroup *mem = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3065 | unsigned int nr_pages = 1; |
| 3066 | struct page_cgroup *pc; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3067 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3068 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3069 | return NULL; |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 3070 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3071 | if (PageSwapCache(page)) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3072 | return NULL; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3073 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 3074 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3075 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 3076 | VM_BUG_ON(!PageTransHuge(page)); |
| 3077 | } |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 3078 | /* |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 3079 | * Check if our page_cgroup is valid |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 3080 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3081 | pc = lookup_page_cgroup(page); |
| 3082 | if (unlikely(!pc || !PageCgroupUsed(pc))) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3083 | return NULL; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3084 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3085 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3086 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3087 | mem = pc->mem_cgroup; |
| 3088 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3089 | if (!PageCgroupUsed(pc)) |
| 3090 | goto unlock_out; |
| 3091 | |
| 3092 | switch (ctype) { |
| 3093 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3094 | case MEM_CGROUP_CHARGE_TYPE_DROP: |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3095 | /* See mem_cgroup_prepare_migration() */ |
| 3096 | if (page_mapped(page) || PageCgroupMigration(pc)) |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3097 | goto unlock_out; |
| 3098 | break; |
| 3099 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: |
| 3100 | if (!PageAnon(page)) { /* Shared memory */ |
| 3101 | if (page->mapping && !page_is_file_cache(page)) |
| 3102 | goto unlock_out; |
| 3103 | } else if (page_mapped(page)) /* Anon */ |
| 3104 | goto unlock_out; |
| 3105 | break; |
| 3106 | default: |
| 3107 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3108 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3109 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3110 | mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), -nr_pages); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 3111 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3112 | ClearPageCgroupUsed(pc); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 3113 | /* |
| 3114 | * pc->mem_cgroup is not cleared here. It will be accessed when it's |
| 3115 | * freed from LRU. This is safe because uncharged page is expected not |
| 3116 | * to be reused (freed soon). Exception is SwapCache, it's handled by |
| 3117 | * special functions. |
| 3118 | */ |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 3119 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3120 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3121 | /* |
| 3122 | * even after unlock, we have mem->res.usage here and this memcg |
| 3123 | * will never be freed. |
| 3124 | */ |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 3125 | memcg_check_events(mem, page); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3126 | if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) { |
| 3127 | mem_cgroup_swap_statistics(mem, true); |
| 3128 | mem_cgroup_get(mem); |
| 3129 | } |
| 3130 | if (!mem_cgroup_is_root(mem)) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3131 | mem_cgroup_do_uncharge(mem, nr_pages, ctype); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 3132 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3133 | return mem; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3134 | |
| 3135 | unlock_out: |
| 3136 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3137 | return NULL; |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 3138 | } |
| 3139 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3140 | void mem_cgroup_uncharge_page(struct page *page) |
| 3141 | { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3142 | /* early check. */ |
| 3143 | if (page_mapped(page)) |
| 3144 | return; |
| 3145 | if (page->mapping && !PageAnon(page)) |
| 3146 | return; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3147 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 3148 | } |
| 3149 | |
| 3150 | void mem_cgroup_uncharge_cache_page(struct page *page) |
| 3151 | { |
| 3152 | VM_BUG_ON(page_mapped(page)); |
KAMEZAWA Hiroyuki | b7abea9 | 2008-10-18 20:28:09 -0700 | [diff] [blame] | 3153 | VM_BUG_ON(page->mapping); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3154 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 3155 | } |
| 3156 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3157 | /* |
| 3158 | * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate. |
| 3159 | * In that cases, pages are freed continuously and we can expect pages |
| 3160 | * are in the same memcg. All these calls itself limits the number of |
| 3161 | * pages freed at once, then uncharge_start/end() is called properly. |
| 3162 | * This may be called prural(2) times in a context, |
| 3163 | */ |
| 3164 | |
| 3165 | void mem_cgroup_uncharge_start(void) |
| 3166 | { |
| 3167 | current->memcg_batch.do_batch++; |
| 3168 | /* We can do nest. */ |
| 3169 | if (current->memcg_batch.do_batch == 1) { |
| 3170 | current->memcg_batch.memcg = NULL; |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3171 | current->memcg_batch.nr_pages = 0; |
| 3172 | current->memcg_batch.memsw_nr_pages = 0; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | void mem_cgroup_uncharge_end(void) |
| 3177 | { |
| 3178 | struct memcg_batch_info *batch = ¤t->memcg_batch; |
| 3179 | |
| 3180 | if (!batch->do_batch) |
| 3181 | return; |
| 3182 | |
| 3183 | batch->do_batch--; |
| 3184 | if (batch->do_batch) /* If stacked, do nothing. */ |
| 3185 | return; |
| 3186 | |
| 3187 | if (!batch->memcg) |
| 3188 | return; |
| 3189 | /* |
| 3190 | * This "batch->memcg" is valid without any css_get/put etc... |
| 3191 | * bacause we hide charges behind us. |
| 3192 | */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3193 | if (batch->nr_pages) |
| 3194 | res_counter_uncharge(&batch->memcg->res, |
| 3195 | batch->nr_pages * PAGE_SIZE); |
| 3196 | if (batch->memsw_nr_pages) |
| 3197 | res_counter_uncharge(&batch->memcg->memsw, |
| 3198 | batch->memsw_nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3199 | memcg_oom_recover(batch->memcg); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3200 | /* forget this pointer (for sanity check) */ |
| 3201 | batch->memcg = NULL; |
| 3202 | } |
| 3203 | |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3204 | #ifdef CONFIG_SWAP |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3205 | /* |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3206 | * called after __delete_from_swap_cache() and drop "page" account. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3207 | * memcg information is recorded to swap_cgroup of "ent" |
| 3208 | */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3209 | void |
| 3210 | 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] | 3211 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3212 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3213 | int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3214 | |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3215 | if (!swapout) /* this was a swap cache but the swap is unused ! */ |
| 3216 | ctype = MEM_CGROUP_CHARGE_TYPE_DROP; |
| 3217 | |
| 3218 | memcg = __mem_cgroup_uncharge_common(page, ctype); |
| 3219 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3220 | /* |
| 3221 | * record memcg information, if swapout && memcg != NULL, |
| 3222 | * mem_cgroup_get() was called in uncharge(). |
| 3223 | */ |
| 3224 | if (do_swap_account && swapout && memcg) |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3225 | swap_cgroup_record(ent, css_id(&memcg->css)); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3226 | } |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3227 | #endif |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3228 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3229 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 3230 | /* |
| 3231 | * called from swap_entry_free(). remove record in swap_cgroup and |
| 3232 | * uncharge "memsw" account. |
| 3233 | */ |
| 3234 | void mem_cgroup_uncharge_swap(swp_entry_t ent) |
| 3235 | { |
| 3236 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3237 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3238 | |
| 3239 | if (!do_swap_account) |
| 3240 | return; |
| 3241 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3242 | id = swap_cgroup_record(ent, 0); |
| 3243 | rcu_read_lock(); |
| 3244 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3245 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3246 | /* |
| 3247 | * We uncharge this because swap is freed. |
| 3248 | * This memcg can be obsolete one. We avoid calling css_tryget |
| 3249 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3250 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 3251 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3252 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3253 | mem_cgroup_put(memcg); |
| 3254 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3255 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3256 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3257 | |
| 3258 | /** |
| 3259 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. |
| 3260 | * @entry: swap entry to be moved |
| 3261 | * @from: mem_cgroup which the entry is moved from |
| 3262 | * @to: mem_cgroup which the entry is moved to |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3263 | * @need_fixup: whether we should fixup res_counters and refcounts. |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3264 | * |
| 3265 | * It succeeds only when the swap_cgroup's record for this entry is the same |
| 3266 | * as the mem_cgroup's id of @from. |
| 3267 | * |
| 3268 | * Returns 0 on success, -EINVAL on failure. |
| 3269 | * |
| 3270 | * The caller must have charged to @to, IOW, called res_counter_charge() about |
| 3271 | * both res and memsw, and called css_get(). |
| 3272 | */ |
| 3273 | static int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3274 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3275 | { |
| 3276 | unsigned short old_id, new_id; |
| 3277 | |
| 3278 | old_id = css_id(&from->css); |
| 3279 | new_id = css_id(&to->css); |
| 3280 | |
| 3281 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3282 | mem_cgroup_swap_statistics(from, false); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3283 | mem_cgroup_swap_statistics(to, true); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3284 | /* |
| 3285 | * This function is only called from task migration context now. |
| 3286 | * It postpones res_counter and refcount handling till the end |
| 3287 | * of task migration(mem_cgroup_clear_mc()) for performance |
| 3288 | * improvement. But we cannot postpone mem_cgroup_get(to) |
| 3289 | * because if the process that has been moved to @to does |
| 3290 | * swap-in, the refcount of @to might be decreased to 0. |
| 3291 | */ |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3292 | mem_cgroup_get(to); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3293 | if (need_fixup) { |
| 3294 | if (!mem_cgroup_is_root(from)) |
| 3295 | res_counter_uncharge(&from->memsw, PAGE_SIZE); |
| 3296 | mem_cgroup_put(from); |
| 3297 | /* |
| 3298 | * we charged both to->res and to->memsw, so we should |
| 3299 | * uncharge to->res. |
| 3300 | */ |
| 3301 | if (!mem_cgroup_is_root(to)) |
| 3302 | res_counter_uncharge(&to->res, PAGE_SIZE); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3303 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3304 | return 0; |
| 3305 | } |
| 3306 | return -EINVAL; |
| 3307 | } |
| 3308 | #else |
| 3309 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3310 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3311 | { |
| 3312 | return -EINVAL; |
| 3313 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3314 | #endif |
| 3315 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3316 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3317 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
| 3318 | * page belongs to. |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3319 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3320 | int mem_cgroup_prepare_migration(struct page *page, |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 3321 | struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3322 | { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3323 | struct mem_cgroup *mem = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3324 | struct page_cgroup *pc; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3325 | enum charge_type ctype; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3326 | int ret = 0; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 3327 | |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 3328 | *ptr = NULL; |
| 3329 | |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 3330 | VM_BUG_ON(PageTransHuge(page)); |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3331 | if (mem_cgroup_disabled()) |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 3332 | return 0; |
| 3333 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3334 | pc = lookup_page_cgroup(page); |
| 3335 | lock_page_cgroup(pc); |
| 3336 | if (PageCgroupUsed(pc)) { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3337 | mem = pc->mem_cgroup; |
| 3338 | css_get(&mem->css); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3339 | /* |
| 3340 | * At migrating an anonymous page, its mapcount goes down |
| 3341 | * to 0 and uncharge() will be called. But, even if it's fully |
| 3342 | * unmapped, migration may fail and this page has to be |
| 3343 | * charged again. We set MIGRATION flag here and delay uncharge |
| 3344 | * until end_migration() is called |
| 3345 | * |
| 3346 | * Corner Case Thinking |
| 3347 | * A) |
| 3348 | * When the old page was mapped as Anon and it's unmap-and-freed |
| 3349 | * while migration was ongoing. |
| 3350 | * If unmap finds the old page, uncharge() of it will be delayed |
| 3351 | * until end_migration(). If unmap finds a new page, it's |
| 3352 | * uncharged when it make mapcount to be 1->0. If unmap code |
| 3353 | * finds swap_migration_entry, the new page will not be mapped |
| 3354 | * and end_migration() will find it(mapcount==0). |
| 3355 | * |
| 3356 | * B) |
| 3357 | * When the old page was mapped but migraion fails, the kernel |
| 3358 | * remaps it. A charge for it is kept by MIGRATION flag even |
| 3359 | * if mapcount goes down to 0. We can do remap successfully |
| 3360 | * without charging it again. |
| 3361 | * |
| 3362 | * C) |
| 3363 | * The "old" page is under lock_page() until the end of |
| 3364 | * migration, so, the old page itself will not be swapped-out. |
| 3365 | * If the new page is swapped out before end_migraton, our |
| 3366 | * hook to usual swap-out path will catch the event. |
| 3367 | */ |
| 3368 | if (PageAnon(page)) |
| 3369 | SetPageCgroupMigration(pc); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 3370 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3371 | unlock_page_cgroup(pc); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3372 | /* |
| 3373 | * If the page is not charged at this point, |
| 3374 | * we return here. |
| 3375 | */ |
| 3376 | if (!mem) |
| 3377 | return 0; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3378 | |
Andrea Arcangeli | 93d5c9b | 2010-04-23 13:17:39 -0400 | [diff] [blame] | 3379 | *ptr = mem; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3380 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3381 | css_put(&mem->css);/* drop extra refcnt */ |
| 3382 | if (ret || *ptr == NULL) { |
| 3383 | if (PageAnon(page)) { |
| 3384 | lock_page_cgroup(pc); |
| 3385 | ClearPageCgroupMigration(pc); |
| 3386 | unlock_page_cgroup(pc); |
| 3387 | /* |
| 3388 | * The old page may be fully unmapped while we kept it. |
| 3389 | */ |
| 3390 | mem_cgroup_uncharge_page(page); |
| 3391 | } |
| 3392 | return -ENOMEM; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3393 | } |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3394 | /* |
| 3395 | * We charge new page before it's used/mapped. So, even if unlock_page() |
| 3396 | * is called before end_migration, we can catch all events on this new |
| 3397 | * page. In the case new page is migrated but not remapped, new page's |
| 3398 | * mapcount will be finally 0 and we call uncharge in end_migration(). |
| 3399 | */ |
| 3400 | pc = lookup_page_cgroup(newpage); |
| 3401 | if (PageAnon(page)) |
| 3402 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; |
| 3403 | else if (page_is_file_cache(page)) |
| 3404 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; |
| 3405 | else |
| 3406 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3407 | __mem_cgroup_commit_charge(mem, page, 1, pc, ctype); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3408 | return ret; |
| 3409 | } |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 3410 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3411 | /* remove redundant charge if migration failed*/ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3412 | void mem_cgroup_end_migration(struct mem_cgroup *mem, |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 3413 | struct page *oldpage, struct page *newpage, bool migration_ok) |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3414 | { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3415 | struct page *used, *unused; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3416 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3417 | |
| 3418 | if (!mem) |
| 3419 | return; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3420 | /* blocks rmdir() */ |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3421 | cgroup_exclude_rmdir(&mem->css); |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 3422 | if (!migration_ok) { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3423 | used = oldpage; |
| 3424 | unused = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3425 | } else { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3426 | used = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3427 | unused = oldpage; |
| 3428 | } |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3429 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3430 | * We disallowed uncharge of pages under migration because mapcount |
| 3431 | * of the page goes down to zero, temporarly. |
| 3432 | * Clear the flag and check the page should be charged. |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3433 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3434 | pc = lookup_page_cgroup(oldpage); |
| 3435 | lock_page_cgroup(pc); |
| 3436 | ClearPageCgroupMigration(pc); |
| 3437 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3438 | |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3439 | __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE); |
| 3440 | |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3441 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3442 | * If a page is a file cache, radix-tree replacement is very atomic |
| 3443 | * and we can skip this check. When it was an Anon page, its mapcount |
| 3444 | * goes down to 0. But because we added MIGRATION flage, it's not |
| 3445 | * uncharged yet. There are several case but page->mapcount check |
| 3446 | * and USED bit check in mem_cgroup_uncharge_page() will do enough |
| 3447 | * check. (see prepare_charge() also) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3448 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3449 | if (PageAnon(used)) |
| 3450 | mem_cgroup_uncharge_page(used); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3451 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3452 | * At migration, we may charge account against cgroup which has no |
| 3453 | * tasks. |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3454 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 3455 | * In that case, we need to call pre_destroy() again. check it here. |
| 3456 | */ |
| 3457 | cgroup_release_and_wakeup_rmdir(&mem->css); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3458 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 3459 | |
Daisuke Nishimura | f212ad7 | 2011-03-23 16:42:25 -0700 | [diff] [blame] | 3460 | #ifdef CONFIG_DEBUG_VM |
| 3461 | static struct page_cgroup *lookup_page_cgroup_used(struct page *page) |
| 3462 | { |
| 3463 | struct page_cgroup *pc; |
| 3464 | |
| 3465 | pc = lookup_page_cgroup(page); |
| 3466 | if (likely(pc) && PageCgroupUsed(pc)) |
| 3467 | return pc; |
| 3468 | return NULL; |
| 3469 | } |
| 3470 | |
| 3471 | bool mem_cgroup_bad_page_check(struct page *page) |
| 3472 | { |
| 3473 | if (mem_cgroup_disabled()) |
| 3474 | return false; |
| 3475 | |
| 3476 | return lookup_page_cgroup_used(page) != NULL; |
| 3477 | } |
| 3478 | |
| 3479 | void mem_cgroup_print_bad_page(struct page *page) |
| 3480 | { |
| 3481 | struct page_cgroup *pc; |
| 3482 | |
| 3483 | pc = lookup_page_cgroup_used(page); |
| 3484 | if (pc) { |
| 3485 | int ret = -1; |
| 3486 | char *path; |
| 3487 | |
| 3488 | printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p", |
| 3489 | pc, pc->flags, pc->mem_cgroup); |
| 3490 | |
| 3491 | path = kmalloc(PATH_MAX, GFP_KERNEL); |
| 3492 | if (path) { |
| 3493 | rcu_read_lock(); |
| 3494 | ret = cgroup_path(pc->mem_cgroup->css.cgroup, |
| 3495 | path, PATH_MAX); |
| 3496 | rcu_read_unlock(); |
| 3497 | } |
| 3498 | |
| 3499 | printk(KERN_CONT "(%s)\n", |
| 3500 | (ret < 0) ? "cannot get the path" : path); |
| 3501 | kfree(path); |
| 3502 | } |
| 3503 | } |
| 3504 | #endif |
| 3505 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3506 | static DEFINE_MUTEX(set_limit_mutex); |
| 3507 | |
KOSAKI Motohiro | d38d2a7 | 2009-01-06 14:39:44 -0800 | [diff] [blame] | 3508 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3509 | unsigned long long val) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3510 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3511 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3512 | u64 memswlimit, memlimit; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3513 | int ret = 0; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3514 | int children = mem_cgroup_count_children(memcg); |
| 3515 | u64 curusage, oldusage; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3516 | int enlarge; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3517 | |
| 3518 | /* |
| 3519 | * For keeping hierarchical_reclaim simple, how long we should retry |
| 3520 | * is depends on callers. We set our retry-count to be function |
| 3521 | * of # of children which we should visit in this loop. |
| 3522 | */ |
| 3523 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; |
| 3524 | |
| 3525 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3526 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3527 | enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3528 | while (retry_count) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3529 | if (signal_pending(current)) { |
| 3530 | ret = -EINTR; |
| 3531 | break; |
| 3532 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3533 | /* |
| 3534 | * Rather than hide all in some function, I do this in |
| 3535 | * open coded manner. You see what this really does. |
| 3536 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 3537 | */ |
| 3538 | mutex_lock(&set_limit_mutex); |
| 3539 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3540 | if (memswlimit < val) { |
| 3541 | ret = -EINVAL; |
| 3542 | mutex_unlock(&set_limit_mutex); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3543 | break; |
| 3544 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3545 | |
| 3546 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3547 | if (memlimit < val) |
| 3548 | enlarge = 1; |
| 3549 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3550 | ret = res_counter_set_limit(&memcg->res, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 3551 | if (!ret) { |
| 3552 | if (memswlimit == val) |
| 3553 | memcg->memsw_is_minimum = true; |
| 3554 | else |
| 3555 | memcg->memsw_is_minimum = false; |
| 3556 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3557 | mutex_unlock(&set_limit_mutex); |
| 3558 | |
| 3559 | if (!ret) |
| 3560 | break; |
| 3561 | |
Bob Liu | aa20d48 | 2009-12-15 16:47:14 -0800 | [diff] [blame] | 3562 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3563 | MEM_CGROUP_RECLAIM_SHRINK, |
| 3564 | NULL); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3565 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
| 3566 | /* Usage is reduced ? */ |
| 3567 | if (curusage >= oldusage) |
| 3568 | retry_count--; |
| 3569 | else |
| 3570 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3571 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3572 | if (!ret && enlarge) |
| 3573 | memcg_oom_recover(memcg); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 3574 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3575 | return ret; |
| 3576 | } |
| 3577 | |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 3578 | static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
| 3579 | unsigned long long val) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3580 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3581 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3582 | u64 memlimit, memswlimit, oldusage, curusage; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3583 | int children = mem_cgroup_count_children(memcg); |
| 3584 | int ret = -EBUSY; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3585 | int enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3586 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3587 | /* see mem_cgroup_resize_res_limit */ |
| 3588 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; |
| 3589 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3590 | while (retry_count) { |
| 3591 | if (signal_pending(current)) { |
| 3592 | ret = -EINTR; |
| 3593 | break; |
| 3594 | } |
| 3595 | /* |
| 3596 | * Rather than hide all in some function, I do this in |
| 3597 | * open coded manner. You see what this really does. |
| 3598 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 3599 | */ |
| 3600 | mutex_lock(&set_limit_mutex); |
| 3601 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3602 | if (memlimit > val) { |
| 3603 | ret = -EINVAL; |
| 3604 | mutex_unlock(&set_limit_mutex); |
| 3605 | break; |
| 3606 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3607 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3608 | if (memswlimit < val) |
| 3609 | enlarge = 1; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3610 | ret = res_counter_set_limit(&memcg->memsw, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 3611 | if (!ret) { |
| 3612 | if (memlimit == val) |
| 3613 | memcg->memsw_is_minimum = true; |
| 3614 | else |
| 3615 | memcg->memsw_is_minimum = false; |
| 3616 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3617 | mutex_unlock(&set_limit_mutex); |
| 3618 | |
| 3619 | if (!ret) |
| 3620 | break; |
| 3621 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3622 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 3623 | MEM_CGROUP_RECLAIM_NOSWAP | |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3624 | MEM_CGROUP_RECLAIM_SHRINK, |
| 3625 | NULL); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3626 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3627 | /* Usage is reduced ? */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3628 | if (curusage >= oldusage) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3629 | retry_count--; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3630 | else |
| 3631 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3632 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3633 | if (!ret && enlarge) |
| 3634 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3635 | return ret; |
| 3636 | } |
| 3637 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3638 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3639 | gfp_t gfp_mask, |
| 3640 | unsigned long *total_scanned) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3641 | { |
| 3642 | unsigned long nr_reclaimed = 0; |
| 3643 | struct mem_cgroup_per_zone *mz, *next_mz = NULL; |
| 3644 | unsigned long reclaimed; |
| 3645 | int loop = 0; |
| 3646 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3647 | unsigned long long excess; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3648 | unsigned long nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3649 | |
| 3650 | if (order > 0) |
| 3651 | return 0; |
| 3652 | |
KOSAKI Motohiro | 00918b6 | 2010-08-10 18:03:05 -0700 | [diff] [blame] | 3653 | mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone)); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3654 | /* |
| 3655 | * This loop can run a while, specially if mem_cgroup's continuously |
| 3656 | * keep exceeding their soft limit and putting the system under |
| 3657 | * pressure |
| 3658 | */ |
| 3659 | do { |
| 3660 | if (next_mz) |
| 3661 | mz = next_mz; |
| 3662 | else |
| 3663 | mz = mem_cgroup_largest_soft_limit_node(mctz); |
| 3664 | if (!mz) |
| 3665 | break; |
| 3666 | |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3667 | nr_scanned = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3668 | reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone, |
| 3669 | gfp_mask, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3670 | MEM_CGROUP_RECLAIM_SOFT, |
| 3671 | &nr_scanned); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3672 | nr_reclaimed += reclaimed; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3673 | *total_scanned += nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3674 | spin_lock(&mctz->lock); |
| 3675 | |
| 3676 | /* |
| 3677 | * If we failed to reclaim anything from this memory cgroup |
| 3678 | * it is time to move on to the next cgroup |
| 3679 | */ |
| 3680 | next_mz = NULL; |
| 3681 | if (!reclaimed) { |
| 3682 | do { |
| 3683 | /* |
| 3684 | * Loop until we find yet another one. |
| 3685 | * |
| 3686 | * By the time we get the soft_limit lock |
| 3687 | * again, someone might have aded the |
| 3688 | * group back on the RB tree. Iterate to |
| 3689 | * make sure we get a different mem. |
| 3690 | * mem_cgroup_largest_soft_limit_node returns |
| 3691 | * NULL if no other cgroup is present on |
| 3692 | * the tree |
| 3693 | */ |
| 3694 | next_mz = |
| 3695 | __mem_cgroup_largest_soft_limit_node(mctz); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 3696 | if (next_mz == mz) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3697 | css_put(&next_mz->mem->css); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 3698 | else /* next_mz == NULL or other memcg */ |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3699 | break; |
| 3700 | } while (1); |
| 3701 | } |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3702 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3703 | excess = res_counter_soft_limit_excess(&mz->mem->res); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3704 | /* |
| 3705 | * One school of thought says that we should not add |
| 3706 | * back the node to the tree if reclaim returns 0. |
| 3707 | * But our reclaim could return 0, simply because due |
| 3708 | * to priority we are exposing a smaller subset of |
| 3709 | * memory to reclaim from. Consider this as a longer |
| 3710 | * term TODO. |
| 3711 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3712 | /* If excess == 0, no tree ops */ |
| 3713 | __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3714 | spin_unlock(&mctz->lock); |
| 3715 | css_put(&mz->mem->css); |
| 3716 | loop++; |
| 3717 | /* |
| 3718 | * Could not reclaim anything and there are no more |
| 3719 | * mem cgroups to try or we seem to be looping without |
| 3720 | * reclaiming anything. |
| 3721 | */ |
| 3722 | if (!nr_reclaimed && |
| 3723 | (next_mz == NULL || |
| 3724 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) |
| 3725 | break; |
| 3726 | } while (!nr_reclaimed); |
| 3727 | if (next_mz) |
| 3728 | css_put(&next_mz->mem->css); |
| 3729 | return nr_reclaimed; |
| 3730 | } |
| 3731 | |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3732 | /* |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3733 | * This routine traverse page_cgroup in given list and drop them all. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3734 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
| 3735 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3736 | static int mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3737 | int node, int zid, enum lru_list lru) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3738 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3739 | struct zone *zone; |
| 3740 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3741 | struct page_cgroup *pc, *busy; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3742 | unsigned long flags, loop; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3743 | struct list_head *list; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3744 | int ret = 0; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3745 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3746 | zone = &NODE_DATA(node)->node_zones[zid]; |
| 3747 | mz = mem_cgroup_zoneinfo(mem, node, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3748 | list = &mz->lists[lru]; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3749 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3750 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
| 3751 | /* give some margin against EBUSY etc...*/ |
| 3752 | loop += 256; |
| 3753 | busy = NULL; |
| 3754 | while (loop--) { |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3755 | struct page *page; |
| 3756 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3757 | ret = 0; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3758 | spin_lock_irqsave(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3759 | if (list_empty(list)) { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3760 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3761 | break; |
| 3762 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3763 | pc = list_entry(list->prev, struct page_cgroup, lru); |
| 3764 | if (busy == pc) { |
| 3765 | list_move(&pc->lru, list); |
Thiago Farina | 648bcc7 | 2010-03-05 13:42:04 -0800 | [diff] [blame] | 3766 | busy = NULL; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3767 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3768 | continue; |
| 3769 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3770 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3771 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 3772 | page = lookup_cgroup_page(pc); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3773 | |
| 3774 | ret = mem_cgroup_move_parent(page, pc, mem, GFP_KERNEL); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3775 | if (ret == -ENOMEM) |
| 3776 | break; |
| 3777 | |
| 3778 | if (ret == -EBUSY || ret == -EINVAL) { |
| 3779 | /* found lock contention or "pc" is obsolete. */ |
| 3780 | busy = pc; |
| 3781 | cond_resched(); |
| 3782 | } else |
| 3783 | busy = NULL; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3784 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3785 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3786 | if (!ret && !list_empty(list)) |
| 3787 | return -EBUSY; |
| 3788 | return ret; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | /* |
| 3792 | * make mem_cgroup's charge to be 0 if there is no task. |
| 3793 | * This enables deleting this mem_cgroup. |
| 3794 | */ |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3795 | 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] | 3796 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3797 | int ret; |
| 3798 | int node, zid, shrink; |
| 3799 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3800 | struct cgroup *cgrp = mem->css.cgroup; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 3801 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3802 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3803 | |
| 3804 | shrink = 0; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3805 | /* should free all ? */ |
| 3806 | if (free_all) |
| 3807 | goto try_to_free; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3808 | move_account: |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3809 | do { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3810 | ret = -EBUSY; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3811 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3812 | goto out; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3813 | ret = -EINTR; |
| 3814 | if (signal_pending(current)) |
| 3815 | goto out; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3816 | /* This is for making all *used* pages to be on LRU. */ |
| 3817 | lru_add_drain_all(); |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 3818 | drain_all_stock_sync(mem); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3819 | ret = 0; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 3820 | mem_cgroup_start_move(mem); |
KAMEZAWA Hiroyuki | 299b4ea | 2009-01-29 14:25:17 -0800 | [diff] [blame] | 3821 | for_each_node_state(node, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3822 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3823 | enum lru_list l; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3824 | for_each_lru(l) { |
| 3825 | ret = mem_cgroup_force_empty_list(mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3826 | node, zid, l); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3827 | if (ret) |
| 3828 | break; |
| 3829 | } |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 3830 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3831 | if (ret) |
| 3832 | break; |
| 3833 | } |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 3834 | mem_cgroup_end_move(mem); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3835 | memcg_oom_recover(mem); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3836 | /* it seems parent cgroup doesn't have enough mem */ |
| 3837 | if (ret == -ENOMEM) |
| 3838 | goto try_to_free; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3839 | cond_resched(); |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3840 | /* "ret" should also be checked to ensure all lists are empty. */ |
| 3841 | } while (mem->res.usage > 0 || ret); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3842 | out: |
| 3843 | css_put(&mem->css); |
| 3844 | return ret; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3845 | |
| 3846 | try_to_free: |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3847 | /* returns EBUSY if there is a task or if we come here twice. */ |
| 3848 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3849 | ret = -EBUSY; |
| 3850 | goto out; |
| 3851 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3852 | /* we call try-to-free pages for make this cgroup empty */ |
| 3853 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3854 | /* try to free all pages in this cgroup */ |
| 3855 | shrink = 1; |
| 3856 | while (nr_retries && mem->res.usage > 0) { |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 3857 | struct memcg_scanrecord rec; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3858 | int progress; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3859 | |
| 3860 | if (signal_pending(current)) { |
| 3861 | ret = -EINTR; |
| 3862 | goto out; |
| 3863 | } |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 3864 | rec.context = SCAN_BY_SHRINK; |
| 3865 | rec.mem = mem; |
| 3866 | rec.root = mem; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 3867 | progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL, |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 3868 | false, &rec); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3869 | if (!progress) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3870 | nr_retries--; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3871 | /* maybe some writeback is necessary */ |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 3872 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3873 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3874 | |
| 3875 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3876 | lru_add_drain(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3877 | /* try move_account...there may be some *locked* pages. */ |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3878 | goto move_account; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3879 | } |
| 3880 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3881 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
| 3882 | { |
| 3883 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); |
| 3884 | } |
| 3885 | |
| 3886 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3887 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
| 3888 | { |
| 3889 | return mem_cgroup_from_cont(cont)->use_hierarchy; |
| 3890 | } |
| 3891 | |
| 3892 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, |
| 3893 | u64 val) |
| 3894 | { |
| 3895 | int retval = 0; |
| 3896 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 3897 | struct cgroup *parent = cont->parent; |
| 3898 | struct mem_cgroup *parent_mem = NULL; |
| 3899 | |
| 3900 | if (parent) |
| 3901 | parent_mem = mem_cgroup_from_cont(parent); |
| 3902 | |
| 3903 | cgroup_lock(); |
| 3904 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 3905 | * 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] | 3906 | * in the child subtrees. If it is unset, then the change can |
| 3907 | * occur, provided the current cgroup has no children. |
| 3908 | * |
| 3909 | * For the root cgroup, parent_mem is NULL, we allow value to be |
| 3910 | * set if there are no children. |
| 3911 | */ |
| 3912 | if ((!parent_mem || !parent_mem->use_hierarchy) && |
| 3913 | (val == 1 || val == 0)) { |
| 3914 | if (list_empty(&cont->children)) |
| 3915 | mem->use_hierarchy = val; |
| 3916 | else |
| 3917 | retval = -EBUSY; |
| 3918 | } else |
| 3919 | retval = -EINVAL; |
| 3920 | cgroup_unlock(); |
| 3921 | |
| 3922 | return retval; |
| 3923 | } |
| 3924 | |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3925 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3926 | static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *mem, |
| 3927 | enum mem_cgroup_stat_index idx) |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3928 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3929 | struct mem_cgroup *iter; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3930 | long val = 0; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3931 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3932 | /* Per-cpu values can be negative, use a signed accumulator */ |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3933 | for_each_mem_cgroup_tree(iter, mem) |
| 3934 | val += mem_cgroup_read_stat(iter, idx); |
| 3935 | |
| 3936 | if (val < 0) /* race ? */ |
| 3937 | val = 0; |
| 3938 | return val; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3939 | } |
| 3940 | |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3941 | static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap) |
| 3942 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3943 | u64 val; |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3944 | |
| 3945 | if (!mem_cgroup_is_root(mem)) { |
| 3946 | if (!swap) |
| 3947 | return res_counter_read_u64(&mem->res, RES_USAGE); |
| 3948 | else |
| 3949 | return res_counter_read_u64(&mem->memsw, RES_USAGE); |
| 3950 | } |
| 3951 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3952 | val = mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_CACHE); |
| 3953 | val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_RSS); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3954 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3955 | if (swap) |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3956 | val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_SWAPOUT); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3957 | |
| 3958 | return val << PAGE_SHIFT; |
| 3959 | } |
| 3960 | |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 3961 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3962 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3963 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3964 | u64 val; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3965 | int type, name; |
| 3966 | |
| 3967 | type = MEMFILE_TYPE(cft->private); |
| 3968 | name = MEMFILE_ATTR(cft->private); |
| 3969 | switch (type) { |
| 3970 | case _MEM: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3971 | if (name == RES_USAGE) |
| 3972 | val = mem_cgroup_usage(mem, false); |
| 3973 | else |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3974 | val = res_counter_read_u64(&mem->res, name); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3975 | break; |
| 3976 | case _MEMSWAP: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3977 | if (name == RES_USAGE) |
| 3978 | val = mem_cgroup_usage(mem, true); |
| 3979 | else |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3980 | val = res_counter_read_u64(&mem->memsw, name); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3981 | break; |
| 3982 | default: |
| 3983 | BUG(); |
| 3984 | break; |
| 3985 | } |
| 3986 | return val; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3987 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3988 | /* |
| 3989 | * The user of this function is... |
| 3990 | * RES_LIMIT. |
| 3991 | */ |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 3992 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 3993 | const char *buffer) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3994 | { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3995 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3996 | int type, name; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3997 | unsigned long long val; |
| 3998 | int ret; |
| 3999 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4000 | type = MEMFILE_TYPE(cft->private); |
| 4001 | name = MEMFILE_ATTR(cft->private); |
| 4002 | switch (name) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4003 | case RES_LIMIT: |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 4004 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
| 4005 | ret = -EINVAL; |
| 4006 | break; |
| 4007 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4008 | /* This function does all necessary parse...reuse it */ |
| 4009 | ret = res_counter_memparse_write_strategy(buffer, &val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4010 | if (ret) |
| 4011 | break; |
| 4012 | if (type == _MEM) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4013 | ret = mem_cgroup_resize_limit(memcg, val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4014 | else |
| 4015 | ret = mem_cgroup_resize_memsw_limit(memcg, val); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4016 | break; |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 4017 | case RES_SOFT_LIMIT: |
| 4018 | ret = res_counter_memparse_write_strategy(buffer, &val); |
| 4019 | if (ret) |
| 4020 | break; |
| 4021 | /* |
| 4022 | * For memsw, soft limits are hard to implement in terms |
| 4023 | * of semantics, for now, we support soft limits for |
| 4024 | * control without swap |
| 4025 | */ |
| 4026 | if (type == _MEM) |
| 4027 | ret = res_counter_set_soft_limit(&memcg->res, val); |
| 4028 | else |
| 4029 | ret = -EINVAL; |
| 4030 | break; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4031 | default: |
| 4032 | ret = -EINVAL; /* should be BUG() ? */ |
| 4033 | break; |
| 4034 | } |
| 4035 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4036 | } |
| 4037 | |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 4038 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
| 4039 | unsigned long long *mem_limit, unsigned long long *memsw_limit) |
| 4040 | { |
| 4041 | struct cgroup *cgroup; |
| 4042 | unsigned long long min_limit, min_memsw_limit, tmp; |
| 4043 | |
| 4044 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 4045 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 4046 | cgroup = memcg->css.cgroup; |
| 4047 | if (!memcg->use_hierarchy) |
| 4048 | goto out; |
| 4049 | |
| 4050 | while (cgroup->parent) { |
| 4051 | cgroup = cgroup->parent; |
| 4052 | memcg = mem_cgroup_from_cont(cgroup); |
| 4053 | if (!memcg->use_hierarchy) |
| 4054 | break; |
| 4055 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 4056 | min_limit = min(min_limit, tmp); |
| 4057 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 4058 | min_memsw_limit = min(min_memsw_limit, tmp); |
| 4059 | } |
| 4060 | out: |
| 4061 | *mem_limit = min_limit; |
| 4062 | *memsw_limit = min_memsw_limit; |
| 4063 | return; |
| 4064 | } |
| 4065 | |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4066 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4067 | { |
| 4068 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4069 | int type, name; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4070 | |
| 4071 | mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4072 | type = MEMFILE_TYPE(event); |
| 4073 | name = MEMFILE_ATTR(event); |
| 4074 | switch (name) { |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4075 | case RES_MAX_USAGE: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4076 | if (type == _MEM) |
| 4077 | res_counter_reset_max(&mem->res); |
| 4078 | else |
| 4079 | res_counter_reset_max(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4080 | break; |
| 4081 | case RES_FAILCNT: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4082 | if (type == _MEM) |
| 4083 | res_counter_reset_failcnt(&mem->res); |
| 4084 | else |
| 4085 | res_counter_reset_failcnt(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4086 | break; |
| 4087 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4088 | |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 4089 | return 0; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4090 | } |
| 4091 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4092 | static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp, |
| 4093 | struct cftype *cft) |
| 4094 | { |
| 4095 | return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate; |
| 4096 | } |
| 4097 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4098 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4099 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 4100 | struct cftype *cft, u64 val) |
| 4101 | { |
| 4102 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4103 | |
| 4104 | if (val >= (1 << NR_MOVE_TYPE)) |
| 4105 | return -EINVAL; |
| 4106 | /* |
| 4107 | * We check this value several times in both in can_attach() and |
| 4108 | * attach(), so we need cgroup lock to prevent this value from being |
| 4109 | * inconsistent. |
| 4110 | */ |
| 4111 | cgroup_lock(); |
| 4112 | mem->move_charge_at_immigrate = val; |
| 4113 | cgroup_unlock(); |
| 4114 | |
| 4115 | return 0; |
| 4116 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4117 | #else |
| 4118 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 4119 | struct cftype *cft, u64 val) |
| 4120 | { |
| 4121 | return -ENOSYS; |
| 4122 | } |
| 4123 | #endif |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4124 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4125 | |
| 4126 | /* For read statistics */ |
| 4127 | enum { |
| 4128 | MCS_CACHE, |
| 4129 | MCS_RSS, |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 4130 | MCS_FILE_MAPPED, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4131 | MCS_PGPGIN, |
| 4132 | MCS_PGPGOUT, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4133 | MCS_SWAP, |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4134 | MCS_PGFAULT, |
| 4135 | MCS_PGMAJFAULT, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4136 | MCS_INACTIVE_ANON, |
| 4137 | MCS_ACTIVE_ANON, |
| 4138 | MCS_INACTIVE_FILE, |
| 4139 | MCS_ACTIVE_FILE, |
| 4140 | MCS_UNEVICTABLE, |
| 4141 | NR_MCS_STAT, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4142 | }; |
| 4143 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4144 | struct mcs_total_stat { |
| 4145 | s64 stat[NR_MCS_STAT]; |
| 4146 | }; |
| 4147 | |
| 4148 | struct { |
| 4149 | char *local_name; |
| 4150 | char *total_name; |
| 4151 | } memcg_stat_strings[NR_MCS_STAT] = { |
| 4152 | {"cache", "total_cache"}, |
| 4153 | {"rss", "total_rss"}, |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 4154 | {"mapped_file", "total_mapped_file"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4155 | {"pgpgin", "total_pgpgin"}, |
| 4156 | {"pgpgout", "total_pgpgout"}, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4157 | {"swap", "total_swap"}, |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4158 | {"pgfault", "total_pgfault"}, |
| 4159 | {"pgmajfault", "total_pgmajfault"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4160 | {"inactive_anon", "total_inactive_anon"}, |
| 4161 | {"active_anon", "total_active_anon"}, |
| 4162 | {"inactive_file", "total_inactive_file"}, |
| 4163 | {"active_file", "total_active_file"}, |
| 4164 | {"unevictable", "total_unevictable"} |
| 4165 | }; |
| 4166 | |
| 4167 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4168 | static void |
| 4169 | mem_cgroup_get_local_stat(struct mem_cgroup *mem, struct mcs_total_stat *s) |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4170 | { |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4171 | s64 val; |
| 4172 | |
| 4173 | /* per cpu stat */ |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4174 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4175 | s->stat[MCS_CACHE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4176 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4177 | s->stat[MCS_RSS] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4178 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED); |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 4179 | s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 4180 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGIN); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4181 | s->stat[MCS_PGPGIN] += val; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 4182 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGOUT); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4183 | s->stat[MCS_PGPGOUT] += val; |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4184 | if (do_swap_account) { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4185 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4186 | s->stat[MCS_SWAP] += val * PAGE_SIZE; |
| 4187 | } |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4188 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGFAULT); |
| 4189 | s->stat[MCS_PGFAULT] += val; |
| 4190 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGMAJFAULT); |
| 4191 | s->stat[MCS_PGMAJFAULT] += val; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4192 | |
| 4193 | /* per zone stat */ |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4194 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_ANON)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4195 | s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4196 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_ANON)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4197 | s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4198 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_FILE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4199 | s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4200 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_FILE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4201 | s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4202 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_UNEVICTABLE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4203 | s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4204 | } |
| 4205 | |
| 4206 | static void |
| 4207 | mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s) |
| 4208 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4209 | struct mem_cgroup *iter; |
| 4210 | |
| 4211 | for_each_mem_cgroup_tree(iter, mem) |
| 4212 | mem_cgroup_get_local_stat(iter, s); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4213 | } |
| 4214 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4215 | #ifdef CONFIG_NUMA |
| 4216 | static int mem_control_numa_stat_show(struct seq_file *m, void *arg) |
| 4217 | { |
| 4218 | int nid; |
| 4219 | unsigned long total_nr, file_nr, anon_nr, unevictable_nr; |
| 4220 | unsigned long node_nr; |
| 4221 | struct cgroup *cont = m->private; |
| 4222 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
| 4223 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4224 | total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4225 | seq_printf(m, "total=%lu", total_nr); |
| 4226 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4227 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, LRU_ALL); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4228 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4229 | } |
| 4230 | seq_putc(m, '\n'); |
| 4231 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4232 | file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4233 | seq_printf(m, "file=%lu", file_nr); |
| 4234 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4235 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4236 | LRU_ALL_FILE); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4237 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4238 | } |
| 4239 | seq_putc(m, '\n'); |
| 4240 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4241 | anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4242 | seq_printf(m, "anon=%lu", anon_nr); |
| 4243 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4244 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4245 | LRU_ALL_ANON); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4246 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4247 | } |
| 4248 | seq_putc(m, '\n'); |
| 4249 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4250 | unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE)); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4251 | seq_printf(m, "unevictable=%lu", unevictable_nr); |
| 4252 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4253 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4254 | BIT(LRU_UNEVICTABLE)); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4255 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4256 | } |
| 4257 | seq_putc(m, '\n'); |
| 4258 | return 0; |
| 4259 | } |
| 4260 | #endif /* CONFIG_NUMA */ |
| 4261 | |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 4262 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
| 4263 | struct cgroup_map_cb *cb) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4264 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4265 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4266 | struct mcs_total_stat mystat; |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4267 | int i; |
| 4268 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4269 | memset(&mystat, 0, sizeof(mystat)); |
| 4270 | mem_cgroup_get_local_stat(mem_cont, &mystat); |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4271 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4272 | |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4273 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 4274 | if (i == MCS_SWAP && !do_swap_account) |
| 4275 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4276 | cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4277 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4278 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4279 | /* Hierarchical information */ |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 4280 | { |
| 4281 | unsigned long long limit, memsw_limit; |
| 4282 | memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit); |
| 4283 | cb->fill(cb, "hierarchical_memory_limit", limit); |
| 4284 | if (do_swap_account) |
| 4285 | cb->fill(cb, "hierarchical_memsw_limit", memsw_limit); |
| 4286 | } |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4287 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4288 | memset(&mystat, 0, sizeof(mystat)); |
| 4289 | mem_cgroup_get_total_stat(mem_cont, &mystat); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4290 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 4291 | if (i == MCS_SWAP && !do_swap_account) |
| 4292 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4293 | cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4294 | } |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4295 | |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4296 | #ifdef CONFIG_DEBUG_VM |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 4297 | cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL)); |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4298 | |
| 4299 | { |
| 4300 | int nid, zid; |
| 4301 | struct mem_cgroup_per_zone *mz; |
| 4302 | unsigned long recent_rotated[2] = {0, 0}; |
| 4303 | unsigned long recent_scanned[2] = {0, 0}; |
| 4304 | |
| 4305 | for_each_online_node(nid) |
| 4306 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 4307 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
| 4308 | |
| 4309 | recent_rotated[0] += |
| 4310 | mz->reclaim_stat.recent_rotated[0]; |
| 4311 | recent_rotated[1] += |
| 4312 | mz->reclaim_stat.recent_rotated[1]; |
| 4313 | recent_scanned[0] += |
| 4314 | mz->reclaim_stat.recent_scanned[0]; |
| 4315 | recent_scanned[1] += |
| 4316 | mz->reclaim_stat.recent_scanned[1]; |
| 4317 | } |
| 4318 | cb->fill(cb, "recent_rotated_anon", recent_rotated[0]); |
| 4319 | cb->fill(cb, "recent_rotated_file", recent_rotated[1]); |
| 4320 | cb->fill(cb, "recent_scanned_anon", recent_scanned[0]); |
| 4321 | cb->fill(cb, "recent_scanned_file", recent_scanned[1]); |
| 4322 | } |
| 4323 | #endif |
| 4324 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4325 | return 0; |
| 4326 | } |
| 4327 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4328 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
| 4329 | { |
| 4330 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4331 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 4332 | return mem_cgroup_swappiness(memcg); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4333 | } |
| 4334 | |
| 4335 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, |
| 4336 | u64 val) |
| 4337 | { |
| 4338 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4339 | struct mem_cgroup *parent; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4340 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4341 | if (val > 100) |
| 4342 | return -EINVAL; |
| 4343 | |
| 4344 | if (cgrp->parent == NULL) |
| 4345 | return -EINVAL; |
| 4346 | |
| 4347 | parent = mem_cgroup_from_cont(cgrp->parent); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4348 | |
| 4349 | cgroup_lock(); |
| 4350 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4351 | /* If under hierarchy, only empty-root can set this value */ |
| 4352 | if ((parent->use_hierarchy) || |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4353 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
| 4354 | cgroup_unlock(); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4355 | return -EINVAL; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4356 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4357 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4358 | memcg->swappiness = val; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4359 | |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4360 | cgroup_unlock(); |
| 4361 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4362 | return 0; |
| 4363 | } |
| 4364 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4365 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) |
| 4366 | { |
| 4367 | struct mem_cgroup_threshold_ary *t; |
| 4368 | u64 usage; |
| 4369 | int i; |
| 4370 | |
| 4371 | rcu_read_lock(); |
| 4372 | if (!swap) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4373 | t = rcu_dereference(memcg->thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4374 | else |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4375 | t = rcu_dereference(memcg->memsw_thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4376 | |
| 4377 | if (!t) |
| 4378 | goto unlock; |
| 4379 | |
| 4380 | usage = mem_cgroup_usage(memcg, swap); |
| 4381 | |
| 4382 | /* |
| 4383 | * current_threshold points to threshold just below usage. |
| 4384 | * If it's not true, a threshold was crossed after last |
| 4385 | * call of __mem_cgroup_threshold(). |
| 4386 | */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4387 | i = t->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4388 | |
| 4389 | /* |
| 4390 | * Iterate backward over array of thresholds starting from |
| 4391 | * current_threshold and check if a threshold is crossed. |
| 4392 | * If none of thresholds below usage is crossed, we read |
| 4393 | * only one element of the array here. |
| 4394 | */ |
| 4395 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) |
| 4396 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4397 | |
| 4398 | /* i = current_threshold + 1 */ |
| 4399 | i++; |
| 4400 | |
| 4401 | /* |
| 4402 | * Iterate forward over array of thresholds starting from |
| 4403 | * current_threshold+1 and check if a threshold is crossed. |
| 4404 | * If none of thresholds above usage is crossed, we read |
| 4405 | * only one element of the array here. |
| 4406 | */ |
| 4407 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) |
| 4408 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4409 | |
| 4410 | /* Update current_threshold */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4411 | t->current_threshold = i - 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4412 | unlock: |
| 4413 | rcu_read_unlock(); |
| 4414 | } |
| 4415 | |
| 4416 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) |
| 4417 | { |
Kirill A. Shutemov | ad4ca5f | 2010-10-07 12:59:27 -0700 | [diff] [blame] | 4418 | while (memcg) { |
| 4419 | __mem_cgroup_threshold(memcg, false); |
| 4420 | if (do_swap_account) |
| 4421 | __mem_cgroup_threshold(memcg, true); |
| 4422 | |
| 4423 | memcg = parent_mem_cgroup(memcg); |
| 4424 | } |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | static int compare_thresholds(const void *a, const void *b) |
| 4428 | { |
| 4429 | const struct mem_cgroup_threshold *_a = a; |
| 4430 | const struct mem_cgroup_threshold *_b = b; |
| 4431 | |
| 4432 | return _a->threshold - _b->threshold; |
| 4433 | } |
| 4434 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4435 | static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4436 | { |
| 4437 | struct mem_cgroup_eventfd_list *ev; |
| 4438 | |
| 4439 | list_for_each_entry(ev, &mem->oom_notify, list) |
| 4440 | eventfd_signal(ev->eventfd, 1); |
| 4441 | return 0; |
| 4442 | } |
| 4443 | |
| 4444 | static void mem_cgroup_oom_notify(struct mem_cgroup *mem) |
| 4445 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4446 | struct mem_cgroup *iter; |
| 4447 | |
| 4448 | for_each_mem_cgroup_tree(iter, mem) |
| 4449 | mem_cgroup_oom_notify_cb(iter); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4450 | } |
| 4451 | |
| 4452 | static int mem_cgroup_usage_register_event(struct cgroup *cgrp, |
| 4453 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4454 | { |
| 4455 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4456 | struct mem_cgroup_thresholds *thresholds; |
| 4457 | struct mem_cgroup_threshold_ary *new; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4458 | int type = MEMFILE_TYPE(cft->private); |
| 4459 | u64 threshold, usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4460 | int i, size, ret; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4461 | |
| 4462 | ret = res_counter_memparse_write_strategy(args, &threshold); |
| 4463 | if (ret) |
| 4464 | return ret; |
| 4465 | |
| 4466 | mutex_lock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4467 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4468 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4469 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4470 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4471 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4472 | else |
| 4473 | BUG(); |
| 4474 | |
| 4475 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 4476 | |
| 4477 | /* Check if a threshold crossed before adding a new one */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4478 | if (thresholds->primary) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4479 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4480 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4481 | size = thresholds->primary ? thresholds->primary->size + 1 : 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4482 | |
| 4483 | /* Allocate memory for new array of thresholds */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4484 | new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4485 | GFP_KERNEL); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4486 | if (!new) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4487 | ret = -ENOMEM; |
| 4488 | goto unlock; |
| 4489 | } |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4490 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4491 | |
| 4492 | /* Copy thresholds (if any) to new array */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4493 | if (thresholds->primary) { |
| 4494 | memcpy(new->entries, thresholds->primary->entries, (size - 1) * |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4495 | sizeof(struct mem_cgroup_threshold)); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4496 | } |
| 4497 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4498 | /* Add new threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4499 | new->entries[size - 1].eventfd = eventfd; |
| 4500 | new->entries[size - 1].threshold = threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4501 | |
| 4502 | /* Sort thresholds. Registering of new threshold isn't time-critical */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4503 | sort(new->entries, size, sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4504 | compare_thresholds, NULL); |
| 4505 | |
| 4506 | /* Find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4507 | new->current_threshold = -1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4508 | for (i = 0; i < size; i++) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4509 | if (new->entries[i].threshold < usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4510 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4511 | * new->current_threshold will not be used until |
| 4512 | * rcu_assign_pointer(), so it's safe to increment |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4513 | * it here. |
| 4514 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4515 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4516 | } |
| 4517 | } |
| 4518 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4519 | /* Free old spare buffer and save old primary buffer as spare */ |
| 4520 | kfree(thresholds->spare); |
| 4521 | thresholds->spare = thresholds->primary; |
| 4522 | |
| 4523 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4524 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4525 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4526 | synchronize_rcu(); |
| 4527 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4528 | unlock: |
| 4529 | mutex_unlock(&memcg->thresholds_lock); |
| 4530 | |
| 4531 | return ret; |
| 4532 | } |
| 4533 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4534 | static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4535 | struct cftype *cft, struct eventfd_ctx *eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4536 | { |
| 4537 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4538 | struct mem_cgroup_thresholds *thresholds; |
| 4539 | struct mem_cgroup_threshold_ary *new; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4540 | int type = MEMFILE_TYPE(cft->private); |
| 4541 | u64 usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4542 | int i, j, size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4543 | |
| 4544 | mutex_lock(&memcg->thresholds_lock); |
| 4545 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4546 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4547 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4548 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4549 | else |
| 4550 | BUG(); |
| 4551 | |
| 4552 | /* |
| 4553 | * Something went wrong if we trying to unregister a threshold |
| 4554 | * if we don't have thresholds |
| 4555 | */ |
| 4556 | BUG_ON(!thresholds); |
| 4557 | |
| 4558 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 4559 | |
| 4560 | /* Check if a threshold crossed before removing */ |
| 4561 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4562 | |
| 4563 | /* Calculate new number of threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4564 | size = 0; |
| 4565 | for (i = 0; i < thresholds->primary->size; i++) { |
| 4566 | if (thresholds->primary->entries[i].eventfd != eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4567 | size++; |
| 4568 | } |
| 4569 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4570 | new = thresholds->spare; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4571 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4572 | /* Set thresholds array to NULL if we don't have thresholds */ |
| 4573 | if (!size) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4574 | kfree(new); |
| 4575 | new = NULL; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4576 | goto swap_buffers; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4577 | } |
| 4578 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4579 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4580 | |
| 4581 | /* Copy thresholds and find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4582 | new->current_threshold = -1; |
| 4583 | for (i = 0, j = 0; i < thresholds->primary->size; i++) { |
| 4584 | if (thresholds->primary->entries[i].eventfd == eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4585 | continue; |
| 4586 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4587 | new->entries[j] = thresholds->primary->entries[i]; |
| 4588 | if (new->entries[j].threshold < usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4589 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4590 | * new->current_threshold will not be used |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4591 | * until rcu_assign_pointer(), so it's safe to increment |
| 4592 | * it here. |
| 4593 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4594 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4595 | } |
| 4596 | j++; |
| 4597 | } |
| 4598 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4599 | swap_buffers: |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4600 | /* Swap primary and spare array */ |
| 4601 | thresholds->spare = thresholds->primary; |
| 4602 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4603 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4604 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4605 | synchronize_rcu(); |
| 4606 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4607 | mutex_unlock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4608 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4609 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4610 | static int mem_cgroup_oom_register_event(struct cgroup *cgrp, |
| 4611 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
| 4612 | { |
| 4613 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4614 | struct mem_cgroup_eventfd_list *event; |
| 4615 | int type = MEMFILE_TYPE(cft->private); |
| 4616 | |
| 4617 | BUG_ON(type != _OOM_TYPE); |
| 4618 | event = kmalloc(sizeof(*event), GFP_KERNEL); |
| 4619 | if (!event) |
| 4620 | return -ENOMEM; |
| 4621 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4622 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4623 | |
| 4624 | event->eventfd = eventfd; |
| 4625 | list_add(&event->list, &memcg->oom_notify); |
| 4626 | |
| 4627 | /* already in OOM ? */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 4628 | if (atomic_read(&memcg->under_oom)) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4629 | eventfd_signal(eventfd, 1); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4630 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4631 | |
| 4632 | return 0; |
| 4633 | } |
| 4634 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4635 | static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4636 | struct cftype *cft, struct eventfd_ctx *eventfd) |
| 4637 | { |
| 4638 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4639 | struct mem_cgroup_eventfd_list *ev, *tmp; |
| 4640 | int type = MEMFILE_TYPE(cft->private); |
| 4641 | |
| 4642 | BUG_ON(type != _OOM_TYPE); |
| 4643 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4644 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4645 | |
| 4646 | list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) { |
| 4647 | if (ev->eventfd == eventfd) { |
| 4648 | list_del(&ev->list); |
| 4649 | kfree(ev); |
| 4650 | } |
| 4651 | } |
| 4652 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4653 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4654 | } |
| 4655 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4656 | static int mem_cgroup_oom_control_read(struct cgroup *cgrp, |
| 4657 | struct cftype *cft, struct cgroup_map_cb *cb) |
| 4658 | { |
| 4659 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4660 | |
| 4661 | cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable); |
| 4662 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 4663 | if (atomic_read(&mem->under_oom)) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4664 | cb->fill(cb, "under_oom", 1); |
| 4665 | else |
| 4666 | cb->fill(cb, "under_oom", 0); |
| 4667 | return 0; |
| 4668 | } |
| 4669 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4670 | static int mem_cgroup_oom_control_write(struct cgroup *cgrp, |
| 4671 | struct cftype *cft, u64 val) |
| 4672 | { |
| 4673 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4674 | struct mem_cgroup *parent; |
| 4675 | |
| 4676 | /* cannot set to root cgroup and only 0 and 1 are allowed */ |
| 4677 | if (!cgrp->parent || !((val == 0) || (val == 1))) |
| 4678 | return -EINVAL; |
| 4679 | |
| 4680 | parent = mem_cgroup_from_cont(cgrp->parent); |
| 4681 | |
| 4682 | cgroup_lock(); |
| 4683 | /* oom-kill-disable is a flag for subhierarchy. */ |
| 4684 | if ((parent->use_hierarchy) || |
| 4685 | (mem->use_hierarchy && !list_empty(&cgrp->children))) { |
| 4686 | cgroup_unlock(); |
| 4687 | return -EINVAL; |
| 4688 | } |
| 4689 | mem->oom_kill_disable = val; |
KAMEZAWA Hiroyuki | 4d845eb | 2010-06-29 15:05:18 -0700 | [diff] [blame] | 4690 | if (!val) |
| 4691 | memcg_oom_recover(mem); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4692 | cgroup_unlock(); |
| 4693 | return 0; |
| 4694 | } |
| 4695 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4696 | #ifdef CONFIG_NUMA |
| 4697 | static const struct file_operations mem_control_numa_stat_file_operations = { |
| 4698 | .read = seq_read, |
| 4699 | .llseek = seq_lseek, |
| 4700 | .release = single_release, |
| 4701 | }; |
| 4702 | |
| 4703 | static int mem_control_numa_stat_open(struct inode *unused, struct file *file) |
| 4704 | { |
| 4705 | struct cgroup *cont = file->f_dentry->d_parent->d_fsdata; |
| 4706 | |
| 4707 | file->f_op = &mem_control_numa_stat_file_operations; |
| 4708 | return single_open(file, mem_control_numa_stat_show, cont); |
| 4709 | } |
| 4710 | #endif /* CONFIG_NUMA */ |
| 4711 | |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 4712 | static int mem_cgroup_vmscan_stat_read(struct cgroup *cgrp, |
| 4713 | struct cftype *cft, |
| 4714 | struct cgroup_map_cb *cb) |
| 4715 | { |
| 4716 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4717 | char string[64]; |
| 4718 | int i; |
| 4719 | |
| 4720 | for (i = 0; i < NR_SCANSTATS; i++) { |
| 4721 | strcpy(string, scanstat_string[i]); |
| 4722 | strcat(string, SCANSTAT_WORD_LIMIT); |
| 4723 | cb->fill(cb, string, mem->scanstat.stats[SCAN_BY_LIMIT][i]); |
| 4724 | } |
| 4725 | |
| 4726 | for (i = 0; i < NR_SCANSTATS; i++) { |
| 4727 | strcpy(string, scanstat_string[i]); |
| 4728 | strcat(string, SCANSTAT_WORD_SYSTEM); |
| 4729 | cb->fill(cb, string, mem->scanstat.stats[SCAN_BY_SYSTEM][i]); |
| 4730 | } |
| 4731 | |
| 4732 | for (i = 0; i < NR_SCANSTATS; i++) { |
| 4733 | strcpy(string, scanstat_string[i]); |
| 4734 | strcat(string, SCANSTAT_WORD_LIMIT); |
| 4735 | strcat(string, SCANSTAT_WORD_HIERARCHY); |
| 4736 | cb->fill(cb, string, mem->scanstat.rootstats[SCAN_BY_LIMIT][i]); |
| 4737 | } |
| 4738 | for (i = 0; i < NR_SCANSTATS; i++) { |
| 4739 | strcpy(string, scanstat_string[i]); |
| 4740 | strcat(string, SCANSTAT_WORD_SYSTEM); |
| 4741 | strcat(string, SCANSTAT_WORD_HIERARCHY); |
| 4742 | cb->fill(cb, string, mem->scanstat.rootstats[SCAN_BY_SYSTEM][i]); |
| 4743 | } |
| 4744 | return 0; |
| 4745 | } |
| 4746 | |
| 4747 | static int mem_cgroup_reset_vmscan_stat(struct cgroup *cgrp, |
| 4748 | unsigned int event) |
| 4749 | { |
| 4750 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4751 | |
| 4752 | spin_lock(&mem->scanstat.lock); |
| 4753 | memset(&mem->scanstat.stats, 0, sizeof(mem->scanstat.stats)); |
| 4754 | memset(&mem->scanstat.rootstats, 0, sizeof(mem->scanstat.rootstats)); |
| 4755 | spin_unlock(&mem->scanstat.lock); |
| 4756 | return 0; |
| 4757 | } |
| 4758 | |
| 4759 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4760 | static struct cftype mem_cgroup_files[] = { |
| 4761 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4762 | .name = "usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4763 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4764 | .read_u64 = mem_cgroup_read, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4765 | .register_event = mem_cgroup_usage_register_event, |
| 4766 | .unregister_event = mem_cgroup_usage_unregister_event, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4767 | }, |
| 4768 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4769 | .name = "max_usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4770 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4771 | .trigger = mem_cgroup_reset, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4772 | .read_u64 = mem_cgroup_read, |
| 4773 | }, |
| 4774 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4775 | .name = "limit_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4776 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 4777 | .write_string = mem_cgroup_write, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4778 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4779 | }, |
| 4780 | { |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 4781 | .name = "soft_limit_in_bytes", |
| 4782 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), |
| 4783 | .write_string = mem_cgroup_write, |
| 4784 | .read_u64 = mem_cgroup_read, |
| 4785 | }, |
| 4786 | { |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4787 | .name = "failcnt", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4788 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4789 | .trigger = mem_cgroup_reset, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4790 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4791 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 4792 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4793 | .name = "stat", |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 4794 | .read_map = mem_control_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4795 | }, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4796 | { |
| 4797 | .name = "force_empty", |
| 4798 | .trigger = mem_cgroup_force_empty_write, |
| 4799 | }, |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4800 | { |
| 4801 | .name = "use_hierarchy", |
| 4802 | .write_u64 = mem_cgroup_hierarchy_write, |
| 4803 | .read_u64 = mem_cgroup_hierarchy_read, |
| 4804 | }, |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4805 | { |
| 4806 | .name = "swappiness", |
| 4807 | .read_u64 = mem_cgroup_swappiness_read, |
| 4808 | .write_u64 = mem_cgroup_swappiness_write, |
| 4809 | }, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4810 | { |
| 4811 | .name = "move_charge_at_immigrate", |
| 4812 | .read_u64 = mem_cgroup_move_charge_read, |
| 4813 | .write_u64 = mem_cgroup_move_charge_write, |
| 4814 | }, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4815 | { |
| 4816 | .name = "oom_control", |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4817 | .read_map = mem_cgroup_oom_control_read, |
| 4818 | .write_u64 = mem_cgroup_oom_control_write, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4819 | .register_event = mem_cgroup_oom_register_event, |
| 4820 | .unregister_event = mem_cgroup_oom_unregister_event, |
| 4821 | .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL), |
| 4822 | }, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4823 | #ifdef CONFIG_NUMA |
| 4824 | { |
| 4825 | .name = "numa_stat", |
| 4826 | .open = mem_control_numa_stat_open, |
KAMEZAWA Hiroyuki | 8957712 | 2011-06-15 15:08:41 -0700 | [diff] [blame] | 4827 | .mode = S_IRUGO, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4828 | }, |
| 4829 | #endif |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 4830 | { |
| 4831 | .name = "vmscan_stat", |
| 4832 | .read_map = mem_cgroup_vmscan_stat_read, |
| 4833 | .trigger = mem_cgroup_reset_vmscan_stat, |
| 4834 | }, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4835 | }; |
| 4836 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4837 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 4838 | static struct cftype memsw_cgroup_files[] = { |
| 4839 | { |
| 4840 | .name = "memsw.usage_in_bytes", |
| 4841 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), |
| 4842 | .read_u64 = mem_cgroup_read, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4843 | .register_event = mem_cgroup_usage_register_event, |
| 4844 | .unregister_event = mem_cgroup_usage_unregister_event, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4845 | }, |
| 4846 | { |
| 4847 | .name = "memsw.max_usage_in_bytes", |
| 4848 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), |
| 4849 | .trigger = mem_cgroup_reset, |
| 4850 | .read_u64 = mem_cgroup_read, |
| 4851 | }, |
| 4852 | { |
| 4853 | .name = "memsw.limit_in_bytes", |
| 4854 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), |
| 4855 | .write_string = mem_cgroup_write, |
| 4856 | .read_u64 = mem_cgroup_read, |
| 4857 | }, |
| 4858 | { |
| 4859 | .name = "memsw.failcnt", |
| 4860 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), |
| 4861 | .trigger = mem_cgroup_reset, |
| 4862 | .read_u64 = mem_cgroup_read, |
| 4863 | }, |
| 4864 | }; |
| 4865 | |
| 4866 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4867 | { |
| 4868 | if (!do_swap_account) |
| 4869 | return 0; |
| 4870 | return cgroup_add_files(cont, ss, memsw_cgroup_files, |
| 4871 | ARRAY_SIZE(memsw_cgroup_files)); |
| 4872 | }; |
| 4873 | #else |
| 4874 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4875 | { |
| 4876 | return 0; |
| 4877 | } |
| 4878 | #endif |
| 4879 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4880 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 4881 | { |
| 4882 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4883 | struct mem_cgroup_per_zone *mz; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 4884 | enum lru_list l; |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 4885 | int zone, tmp = node; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4886 | /* |
| 4887 | * This routine is called against possible nodes. |
| 4888 | * But it's BUG to call kmalloc() against offline node. |
| 4889 | * |
| 4890 | * TODO: this routine can waste much memory for nodes which will |
| 4891 | * never be onlined. It's better to use memory hotplug callback |
| 4892 | * function. |
| 4893 | */ |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 4894 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 4895 | tmp = -1; |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4896 | pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4897 | if (!pn) |
| 4898 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4899 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4900 | mem->info.nodeinfo[node] = pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4901 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 4902 | mz = &pn->zoneinfo[zone]; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 4903 | for_each_lru(l) |
| 4904 | INIT_LIST_HEAD(&mz->lists[l]); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4905 | mz->usage_in_excess = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4906 | mz->on_tree = false; |
| 4907 | mz->mem = mem; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4908 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4909 | return 0; |
| 4910 | } |
| 4911 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4912 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 4913 | { |
| 4914 | kfree(mem->info.nodeinfo[node]); |
| 4915 | } |
| 4916 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4917 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 4918 | { |
| 4919 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4920 | int size = sizeof(struct mem_cgroup); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4921 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4922 | /* Can be very big if MAX_NUMNODES is very big */ |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4923 | if (size < PAGE_SIZE) |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4924 | mem = kzalloc(size, GFP_KERNEL); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4925 | else |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4926 | mem = vzalloc(size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4927 | |
Dan Carpenter | e7bbcdf | 2010-03-23 13:35:12 -0700 | [diff] [blame] | 4928 | if (!mem) |
| 4929 | return NULL; |
| 4930 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4931 | mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu); |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 4932 | if (!mem->stat) |
| 4933 | goto out_free; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 4934 | spin_lock_init(&mem->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4935 | return mem; |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 4936 | |
| 4937 | out_free: |
| 4938 | if (size < PAGE_SIZE) |
| 4939 | kfree(mem); |
| 4940 | else |
| 4941 | vfree(mem); |
| 4942 | return NULL; |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4943 | } |
| 4944 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4945 | /* |
| 4946 | * At destroying mem_cgroup, references from swap_cgroup can remain. |
| 4947 | * (scanning all at force_empty is too costly...) |
| 4948 | * |
| 4949 | * Instead of clearing all references at force_empty, we remember |
| 4950 | * the number of reference from swap_cgroup and free mem_cgroup when |
| 4951 | * it goes down to 0. |
| 4952 | * |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4953 | * Removal of cgroup itself succeeds regardless of refs from swap. |
| 4954 | */ |
| 4955 | |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 4956 | static void __mem_cgroup_free(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4957 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4958 | int node; |
| 4959 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4960 | mem_cgroup_remove_from_trees(mem); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4961 | free_css_id(&mem_cgroup_subsys, &mem->css); |
| 4962 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4963 | for_each_node_state(node, N_POSSIBLE) |
| 4964 | free_mem_cgroup_per_zone_info(mem, node); |
| 4965 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4966 | free_percpu(mem->stat); |
| 4967 | if (sizeof(struct mem_cgroup) < PAGE_SIZE) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4968 | kfree(mem); |
| 4969 | else |
| 4970 | vfree(mem); |
| 4971 | } |
| 4972 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4973 | static void mem_cgroup_get(struct mem_cgroup *mem) |
| 4974 | { |
| 4975 | atomic_inc(&mem->refcnt); |
| 4976 | } |
| 4977 | |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4978 | static void __mem_cgroup_put(struct mem_cgroup *mem, int count) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4979 | { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4980 | if (atomic_sub_and_test(count, &mem->refcnt)) { |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4981 | struct mem_cgroup *parent = parent_mem_cgroup(mem); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 4982 | __mem_cgroup_free(mem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4983 | if (parent) |
| 4984 | mem_cgroup_put(parent); |
| 4985 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4986 | } |
| 4987 | |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4988 | static void mem_cgroup_put(struct mem_cgroup *mem) |
| 4989 | { |
| 4990 | __mem_cgroup_put(mem, 1); |
| 4991 | } |
| 4992 | |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4993 | /* |
| 4994 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. |
| 4995 | */ |
| 4996 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem) |
| 4997 | { |
| 4998 | if (!mem->res.parent) |
| 4999 | return NULL; |
| 5000 | return mem_cgroup_from_res_counter(mem->res.parent, res); |
| 5001 | } |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5002 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5003 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 5004 | static void __init enable_swap_cgroup(void) |
| 5005 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 5006 | if (!mem_cgroup_disabled() && really_do_swap_account) |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5007 | do_swap_account = 1; |
| 5008 | } |
| 5009 | #else |
| 5010 | static void __init enable_swap_cgroup(void) |
| 5011 | { |
| 5012 | } |
| 5013 | #endif |
| 5014 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5015 | static int mem_cgroup_soft_limit_tree_init(void) |
| 5016 | { |
| 5017 | struct mem_cgroup_tree_per_node *rtpn; |
| 5018 | struct mem_cgroup_tree_per_zone *rtpz; |
| 5019 | int tmp, node, zone; |
| 5020 | |
| 5021 | for_each_node_state(node, N_POSSIBLE) { |
| 5022 | tmp = node; |
| 5023 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 5024 | tmp = -1; |
| 5025 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp); |
| 5026 | if (!rtpn) |
| 5027 | return 1; |
| 5028 | |
| 5029 | soft_limit_tree.rb_tree_per_node[node] = rtpn; |
| 5030 | |
| 5031 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 5032 | rtpz = &rtpn->rb_tree_per_zone[zone]; |
| 5033 | rtpz->rb_root = RB_ROOT; |
| 5034 | spin_lock_init(&rtpz->lock); |
| 5035 | } |
| 5036 | } |
| 5037 | return 0; |
| 5038 | } |
| 5039 | |
Li Zefan | 0eb253e | 2009-01-15 13:51:25 -0800 | [diff] [blame] | 5040 | static struct cgroup_subsys_state * __ref |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5041 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 5042 | { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5043 | struct mem_cgroup *mem, *parent; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5044 | long error = -ENOMEM; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5045 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5046 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 5047 | mem = mem_cgroup_alloc(); |
| 5048 | if (!mem) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5049 | return ERR_PTR(error); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 5050 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5051 | for_each_node_state(node, N_POSSIBLE) |
| 5052 | if (alloc_mem_cgroup_per_zone_info(mem, node)) |
| 5053 | goto free_out; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5054 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5055 | /* root ? */ |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5056 | if (cont->parent == NULL) { |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 5057 | int cpu; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5058 | enable_swap_cgroup(); |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5059 | parent = NULL; |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 5060 | root_mem_cgroup = mem; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5061 | if (mem_cgroup_soft_limit_tree_init()) |
| 5062 | goto free_out; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 5063 | for_each_possible_cpu(cpu) { |
| 5064 | struct memcg_stock_pcp *stock = |
| 5065 | &per_cpu(memcg_stock, cpu); |
| 5066 | INIT_WORK(&stock->work, drain_local_stock); |
| 5067 | } |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 5068 | hotcpu_notifier(memcg_cpu_hotplug_callback, 0); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5069 | } else { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5070 | parent = mem_cgroup_from_cont(cont->parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5071 | mem->use_hierarchy = parent->use_hierarchy; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 5072 | mem->oom_kill_disable = parent->oom_kill_disable; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5073 | } |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5074 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5075 | if (parent && parent->use_hierarchy) { |
| 5076 | res_counter_init(&mem->res, &parent->res); |
| 5077 | res_counter_init(&mem->memsw, &parent->memsw); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5078 | /* |
| 5079 | * We increment refcnt of the parent to ensure that we can |
| 5080 | * safely access it on res_counter_charge/uncharge. |
| 5081 | * This refcnt will be decremented when freeing this |
| 5082 | * mem_cgroup(see mem_cgroup_put). |
| 5083 | */ |
| 5084 | mem_cgroup_get(parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5085 | } else { |
| 5086 | res_counter_init(&mem->res, NULL); |
| 5087 | res_counter_init(&mem->memsw, NULL); |
| 5088 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5089 | mem->last_scanned_child = 0; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 5090 | mem->last_scanned_node = MAX_NUMNODES; |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5091 | INIT_LIST_HEAD(&mem->oom_notify); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 5092 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5093 | if (parent) |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 5094 | mem->swappiness = mem_cgroup_swappiness(parent); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 5095 | atomic_set(&mem->refcnt, 1); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5096 | mem->move_charge_at_immigrate = 0; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5097 | mutex_init(&mem->thresholds_lock); |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 5098 | spin_lock_init(&mem->scanstat.lock); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5099 | return &mem->css; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5100 | free_out: |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 5101 | __mem_cgroup_free(mem); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 5102 | root_mem_cgroup = NULL; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5103 | return ERR_PTR(error); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5104 | } |
| 5105 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 5106 | static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5107 | struct cgroup *cont) |
| 5108 | { |
| 5109 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 5110 | |
| 5111 | return mem_cgroup_force_empty(mem, false); |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5112 | } |
| 5113 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5114 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 5115 | struct cgroup *cont) |
| 5116 | { |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 5117 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 5118 | |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 5119 | mem_cgroup_put(mem); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5120 | } |
| 5121 | |
| 5122 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 5123 | struct cgroup *cont) |
| 5124 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5125 | int ret; |
| 5126 | |
| 5127 | ret = cgroup_add_files(cont, ss, mem_cgroup_files, |
| 5128 | ARRAY_SIZE(mem_cgroup_files)); |
| 5129 | |
| 5130 | if (!ret) |
| 5131 | ret = register_memsw_files(cont, ss); |
| 5132 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5133 | } |
| 5134 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5135 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5136 | /* Handlers for move charge at task migration. */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5137 | #define PRECHARGE_COUNT_AT_ONCE 256 |
| 5138 | static int mem_cgroup_do_precharge(unsigned long count) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5139 | { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5140 | int ret = 0; |
| 5141 | int batch_count = PRECHARGE_COUNT_AT_ONCE; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5142 | struct mem_cgroup *mem = mc.to; |
| 5143 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5144 | if (mem_cgroup_is_root(mem)) { |
| 5145 | mc.precharge += count; |
| 5146 | /* we don't need css_get for root */ |
| 5147 | return ret; |
| 5148 | } |
| 5149 | /* try to charge at once */ |
| 5150 | if (count > 1) { |
| 5151 | struct res_counter *dummy; |
| 5152 | /* |
| 5153 | * "mem" cannot be under rmdir() because we've already checked |
| 5154 | * by cgroup_lock_live_cgroup() that it is not removed and we |
| 5155 | * are still under the same cgroup_mutex. So we can postpone |
| 5156 | * css_get(). |
| 5157 | */ |
| 5158 | if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy)) |
| 5159 | goto one_by_one; |
| 5160 | if (do_swap_account && res_counter_charge(&mem->memsw, |
| 5161 | PAGE_SIZE * count, &dummy)) { |
| 5162 | res_counter_uncharge(&mem->res, PAGE_SIZE * count); |
| 5163 | goto one_by_one; |
| 5164 | } |
| 5165 | mc.precharge += count; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5166 | return ret; |
| 5167 | } |
| 5168 | one_by_one: |
| 5169 | /* fall back to one by one charge */ |
| 5170 | while (count--) { |
| 5171 | if (signal_pending(current)) { |
| 5172 | ret = -EINTR; |
| 5173 | break; |
| 5174 | } |
| 5175 | if (!batch_count--) { |
| 5176 | batch_count = PRECHARGE_COUNT_AT_ONCE; |
| 5177 | cond_resched(); |
| 5178 | } |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 5179 | ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, 1, &mem, false); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5180 | if (ret || !mem) |
| 5181 | /* mem_cgroup_clear_mc() will do uncharge later */ |
| 5182 | return -ENOMEM; |
| 5183 | mc.precharge++; |
| 5184 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5185 | return ret; |
| 5186 | } |
| 5187 | |
| 5188 | /** |
| 5189 | * is_target_pte_for_mc - check a pte whether it is valid for move charge |
| 5190 | * @vma: the vma the pte to be checked belongs |
| 5191 | * @addr: the address corresponding to the pte to be checked |
| 5192 | * @ptent: the pte to be checked |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5193 | * @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] | 5194 | * |
| 5195 | * Returns |
| 5196 | * 0(MC_TARGET_NONE): if the pte is not a target for move charge. |
| 5197 | * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for |
| 5198 | * move charge. if @target is not NULL, the page is stored in target->page |
| 5199 | * with extra refcnt got(Callers should handle it). |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5200 | * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a |
| 5201 | * target for charge migration. if @target is not NULL, the entry is stored |
| 5202 | * in target->ent. |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5203 | * |
| 5204 | * Called with pte lock held. |
| 5205 | */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5206 | union mc_target { |
| 5207 | struct page *page; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5208 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5209 | }; |
| 5210 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5211 | enum mc_target_type { |
| 5212 | MC_TARGET_NONE, /* not used */ |
| 5213 | MC_TARGET_PAGE, |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5214 | MC_TARGET_SWAP, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5215 | }; |
| 5216 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5217 | static struct page *mc_handle_present_pte(struct vm_area_struct *vma, |
| 5218 | unsigned long addr, pte_t ptent) |
| 5219 | { |
| 5220 | struct page *page = vm_normal_page(vma, addr, ptent); |
| 5221 | |
| 5222 | if (!page || !page_mapped(page)) |
| 5223 | return NULL; |
| 5224 | if (PageAnon(page)) { |
| 5225 | /* we don't move shared anon */ |
| 5226 | if (!move_anon() || page_mapcount(page) > 2) |
| 5227 | return NULL; |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5228 | } else if (!move_file()) |
| 5229 | /* we ignore mapcount for file pages */ |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5230 | return NULL; |
| 5231 | if (!get_page_unless_zero(page)) |
| 5232 | return NULL; |
| 5233 | |
| 5234 | return page; |
| 5235 | } |
| 5236 | |
| 5237 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
| 5238 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5239 | { |
| 5240 | int usage_count; |
| 5241 | struct page *page = NULL; |
| 5242 | swp_entry_t ent = pte_to_swp_entry(ptent); |
| 5243 | |
| 5244 | if (!move_anon() || non_swap_entry(ent)) |
| 5245 | return NULL; |
| 5246 | usage_count = mem_cgroup_count_swap_user(ent, &page); |
| 5247 | if (usage_count > 1) { /* we don't move shared anon */ |
| 5248 | if (page) |
| 5249 | put_page(page); |
| 5250 | return NULL; |
| 5251 | } |
| 5252 | if (do_swap_account) |
| 5253 | entry->val = ent.val; |
| 5254 | |
| 5255 | return page; |
| 5256 | } |
| 5257 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5258 | static struct page *mc_handle_file_pte(struct vm_area_struct *vma, |
| 5259 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5260 | { |
| 5261 | struct page *page = NULL; |
| 5262 | struct inode *inode; |
| 5263 | struct address_space *mapping; |
| 5264 | pgoff_t pgoff; |
| 5265 | |
| 5266 | if (!vma->vm_file) /* anonymous vma */ |
| 5267 | return NULL; |
| 5268 | if (!move_file()) |
| 5269 | return NULL; |
| 5270 | |
| 5271 | inode = vma->vm_file->f_path.dentry->d_inode; |
| 5272 | mapping = vma->vm_file->f_mapping; |
| 5273 | if (pte_none(ptent)) |
| 5274 | pgoff = linear_page_index(vma, addr); |
| 5275 | else /* pte_file(ptent) is true */ |
| 5276 | pgoff = pte_to_pgoff(ptent); |
| 5277 | |
| 5278 | /* page is moved even if it's not RSS of this task(page-faulted). */ |
Hugh Dickins | aa3b189 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 5279 | page = find_get_page(mapping, pgoff); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5280 | |
Hugh Dickins | aa3b189 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 5281 | #ifdef CONFIG_SWAP |
| 5282 | /* shmem/tmpfs may report page out on swap: account for that too. */ |
| 5283 | if (radix_tree_exceptional_entry(page)) { |
| 5284 | swp_entry_t swap = radix_to_swp_entry(page); |
| 5285 | if (do_swap_account) |
| 5286 | *entry = swap; |
| 5287 | page = find_get_page(&swapper_space, swap.val); |
| 5288 | } |
| 5289 | #endif |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5290 | return page; |
| 5291 | } |
| 5292 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5293 | static int is_target_pte_for_mc(struct vm_area_struct *vma, |
| 5294 | unsigned long addr, pte_t ptent, union mc_target *target) |
| 5295 | { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5296 | struct page *page = NULL; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5297 | struct page_cgroup *pc; |
| 5298 | int ret = 0; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5299 | swp_entry_t ent = { .val = 0 }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5300 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5301 | if (pte_present(ptent)) |
| 5302 | page = mc_handle_present_pte(vma, addr, ptent); |
| 5303 | else if (is_swap_pte(ptent)) |
| 5304 | page = mc_handle_swap_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5305 | else if (pte_none(ptent) || pte_file(ptent)) |
| 5306 | page = mc_handle_file_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5307 | |
| 5308 | if (!page && !ent.val) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5309 | return 0; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5310 | if (page) { |
| 5311 | pc = lookup_page_cgroup(page); |
| 5312 | /* |
| 5313 | * Do only loose check w/o page_cgroup lock. |
| 5314 | * mem_cgroup_move_account() checks the pc is valid or not under |
| 5315 | * the lock. |
| 5316 | */ |
| 5317 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { |
| 5318 | ret = MC_TARGET_PAGE; |
| 5319 | if (target) |
| 5320 | target->page = page; |
| 5321 | } |
| 5322 | if (!ret || !target) |
| 5323 | put_page(page); |
| 5324 | } |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5325 | /* There is a swap entry and a page doesn't exist or isn't charged */ |
| 5326 | if (ent.val && !ret && |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5327 | css_id(&mc.from->css) == lookup_swap_cgroup(ent)) { |
| 5328 | ret = MC_TARGET_SWAP; |
| 5329 | if (target) |
| 5330 | target->ent = ent; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5331 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5332 | return ret; |
| 5333 | } |
| 5334 | |
| 5335 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, |
| 5336 | unsigned long addr, unsigned long end, |
| 5337 | struct mm_walk *walk) |
| 5338 | { |
| 5339 | struct vm_area_struct *vma = walk->private; |
| 5340 | pte_t *pte; |
| 5341 | spinlock_t *ptl; |
| 5342 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 5343 | split_huge_page_pmd(walk->mm, pmd); |
| 5344 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5345 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5346 | for (; addr != end; pte++, addr += PAGE_SIZE) |
| 5347 | if (is_target_pte_for_mc(vma, addr, *pte, NULL)) |
| 5348 | mc.precharge++; /* increment precharge temporarily */ |
| 5349 | pte_unmap_unlock(pte - 1, ptl); |
| 5350 | cond_resched(); |
| 5351 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5352 | return 0; |
| 5353 | } |
| 5354 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5355 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
| 5356 | { |
| 5357 | unsigned long precharge; |
| 5358 | struct vm_area_struct *vma; |
| 5359 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5360 | down_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5361 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5362 | struct mm_walk mem_cgroup_count_precharge_walk = { |
| 5363 | .pmd_entry = mem_cgroup_count_precharge_pte_range, |
| 5364 | .mm = mm, |
| 5365 | .private = vma, |
| 5366 | }; |
| 5367 | if (is_vm_hugetlb_page(vma)) |
| 5368 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5369 | walk_page_range(vma->vm_start, vma->vm_end, |
| 5370 | &mem_cgroup_count_precharge_walk); |
| 5371 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5372 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5373 | |
| 5374 | precharge = mc.precharge; |
| 5375 | mc.precharge = 0; |
| 5376 | |
| 5377 | return precharge; |
| 5378 | } |
| 5379 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5380 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
| 5381 | { |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5382 | unsigned long precharge = mem_cgroup_count_precharge(mm); |
| 5383 | |
| 5384 | VM_BUG_ON(mc.moving_task); |
| 5385 | mc.moving_task = current; |
| 5386 | return mem_cgroup_do_precharge(precharge); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5387 | } |
| 5388 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5389 | /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */ |
| 5390 | static void __mem_cgroup_clear_mc(void) |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5391 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5392 | struct mem_cgroup *from = mc.from; |
| 5393 | struct mem_cgroup *to = mc.to; |
| 5394 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5395 | /* we must uncharge all the leftover precharges from mc.to */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5396 | if (mc.precharge) { |
| 5397 | __mem_cgroup_cancel_charge(mc.to, mc.precharge); |
| 5398 | mc.precharge = 0; |
| 5399 | } |
| 5400 | /* |
| 5401 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so |
| 5402 | * we must uncharge here. |
| 5403 | */ |
| 5404 | if (mc.moved_charge) { |
| 5405 | __mem_cgroup_cancel_charge(mc.from, mc.moved_charge); |
| 5406 | mc.moved_charge = 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5407 | } |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5408 | /* we must fixup refcnts and charges */ |
| 5409 | if (mc.moved_swap) { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5410 | /* uncharge swap account from the old cgroup */ |
| 5411 | if (!mem_cgroup_is_root(mc.from)) |
| 5412 | res_counter_uncharge(&mc.from->memsw, |
| 5413 | PAGE_SIZE * mc.moved_swap); |
| 5414 | __mem_cgroup_put(mc.from, mc.moved_swap); |
| 5415 | |
| 5416 | if (!mem_cgroup_is_root(mc.to)) { |
| 5417 | /* |
| 5418 | * we charged both to->res and to->memsw, so we should |
| 5419 | * uncharge to->res. |
| 5420 | */ |
| 5421 | res_counter_uncharge(&mc.to->res, |
| 5422 | PAGE_SIZE * mc.moved_swap); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5423 | } |
| 5424 | /* we've already done mem_cgroup_get(mc.to) */ |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5425 | mc.moved_swap = 0; |
| 5426 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5427 | memcg_oom_recover(from); |
| 5428 | memcg_oom_recover(to); |
| 5429 | wake_up_all(&mc.waitq); |
| 5430 | } |
| 5431 | |
| 5432 | static void mem_cgroup_clear_mc(void) |
| 5433 | { |
| 5434 | struct mem_cgroup *from = mc.from; |
| 5435 | |
| 5436 | /* |
| 5437 | * we must clear moving_task before waking up waiters at the end of |
| 5438 | * task migration. |
| 5439 | */ |
| 5440 | mc.moving_task = NULL; |
| 5441 | __mem_cgroup_clear_mc(); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5442 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5443 | mc.from = NULL; |
| 5444 | mc.to = NULL; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5445 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5446 | mem_cgroup_end_move(from); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5447 | } |
| 5448 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5449 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 5450 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5451 | struct task_struct *p) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5452 | { |
| 5453 | int ret = 0; |
| 5454 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup); |
| 5455 | |
| 5456 | if (mem->move_charge_at_immigrate) { |
| 5457 | struct mm_struct *mm; |
| 5458 | struct mem_cgroup *from = mem_cgroup_from_task(p); |
| 5459 | |
| 5460 | VM_BUG_ON(from == mem); |
| 5461 | |
| 5462 | mm = get_task_mm(p); |
| 5463 | if (!mm) |
| 5464 | return 0; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5465 | /* We move charges only when we move a owner of the mm */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5466 | if (mm->owner == p) { |
| 5467 | VM_BUG_ON(mc.from); |
| 5468 | VM_BUG_ON(mc.to); |
| 5469 | VM_BUG_ON(mc.precharge); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5470 | VM_BUG_ON(mc.moved_charge); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5471 | VM_BUG_ON(mc.moved_swap); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5472 | mem_cgroup_start_move(from); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5473 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5474 | mc.from = from; |
| 5475 | mc.to = mem; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5476 | spin_unlock(&mc.lock); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5477 | /* We set mc.moving_task later */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5478 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5479 | ret = mem_cgroup_precharge_mc(mm); |
| 5480 | if (ret) |
| 5481 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5482 | } |
| 5483 | mmput(mm); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5484 | } |
| 5485 | return ret; |
| 5486 | } |
| 5487 | |
| 5488 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 5489 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5490 | struct task_struct *p) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5491 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5492 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5493 | } |
| 5494 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5495 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
| 5496 | unsigned long addr, unsigned long end, |
| 5497 | struct mm_walk *walk) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5498 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5499 | int ret = 0; |
| 5500 | struct vm_area_struct *vma = walk->private; |
| 5501 | pte_t *pte; |
| 5502 | spinlock_t *ptl; |
| 5503 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 5504 | split_huge_page_pmd(walk->mm, pmd); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5505 | retry: |
| 5506 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5507 | for (; addr != end; addr += PAGE_SIZE) { |
| 5508 | pte_t ptent = *(pte++); |
| 5509 | union mc_target target; |
| 5510 | int type; |
| 5511 | struct page *page; |
| 5512 | struct page_cgroup *pc; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5513 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5514 | |
| 5515 | if (!mc.precharge) |
| 5516 | break; |
| 5517 | |
| 5518 | type = is_target_pte_for_mc(vma, addr, ptent, &target); |
| 5519 | switch (type) { |
| 5520 | case MC_TARGET_PAGE: |
| 5521 | page = target.page; |
| 5522 | if (isolate_lru_page(page)) |
| 5523 | goto put; |
| 5524 | pc = lookup_page_cgroup(page); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 5525 | if (!mem_cgroup_move_account(page, 1, pc, |
| 5526 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5527 | mc.precharge--; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5528 | /* we uncharge from mc.from later. */ |
| 5529 | mc.moved_charge++; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5530 | } |
| 5531 | putback_lru_page(page); |
| 5532 | put: /* is_target_pte_for_mc() gets the page */ |
| 5533 | put_page(page); |
| 5534 | break; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5535 | case MC_TARGET_SWAP: |
| 5536 | ent = target.ent; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5537 | if (!mem_cgroup_move_swap_account(ent, |
| 5538 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5539 | mc.precharge--; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5540 | /* we fixup refcnts and charges later. */ |
| 5541 | mc.moved_swap++; |
| 5542 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5543 | break; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5544 | default: |
| 5545 | break; |
| 5546 | } |
| 5547 | } |
| 5548 | pte_unmap_unlock(pte - 1, ptl); |
| 5549 | cond_resched(); |
| 5550 | |
| 5551 | if (addr != end) { |
| 5552 | /* |
| 5553 | * We have consumed all precharges we got in can_attach(). |
| 5554 | * We try charge one by one, but don't do any additional |
| 5555 | * charges to mc.to if we have failed in charge once in attach() |
| 5556 | * phase. |
| 5557 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5558 | ret = mem_cgroup_do_precharge(1); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5559 | if (!ret) |
| 5560 | goto retry; |
| 5561 | } |
| 5562 | |
| 5563 | return ret; |
| 5564 | } |
| 5565 | |
| 5566 | static void mem_cgroup_move_charge(struct mm_struct *mm) |
| 5567 | { |
| 5568 | struct vm_area_struct *vma; |
| 5569 | |
| 5570 | lru_add_drain_all(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5571 | retry: |
| 5572 | if (unlikely(!down_read_trylock(&mm->mmap_sem))) { |
| 5573 | /* |
| 5574 | * Someone who are holding the mmap_sem might be waiting in |
| 5575 | * waitq. So we cancel all extra charges, wake up all waiters, |
| 5576 | * and retry. Because we cancel precharges, we might not be able |
| 5577 | * to move enough charges, but moving charge is a best-effort |
| 5578 | * feature anyway, so it wouldn't be a big problem. |
| 5579 | */ |
| 5580 | __mem_cgroup_clear_mc(); |
| 5581 | cond_resched(); |
| 5582 | goto retry; |
| 5583 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5584 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5585 | int ret; |
| 5586 | struct mm_walk mem_cgroup_move_charge_walk = { |
| 5587 | .pmd_entry = mem_cgroup_move_charge_pte_range, |
| 5588 | .mm = mm, |
| 5589 | .private = vma, |
| 5590 | }; |
| 5591 | if (is_vm_hugetlb_page(vma)) |
| 5592 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5593 | ret = walk_page_range(vma->vm_start, vma->vm_end, |
| 5594 | &mem_cgroup_move_charge_walk); |
| 5595 | if (ret) |
| 5596 | /* |
| 5597 | * means we have consumed all precharges and failed in |
| 5598 | * doing additional charge. Just abandon here. |
| 5599 | */ |
| 5600 | break; |
| 5601 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5602 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5603 | } |
| 5604 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5605 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 5606 | struct cgroup *cont, |
| 5607 | struct cgroup *old_cont, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5608 | struct task_struct *p) |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5609 | { |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5610 | struct mm_struct *mm = get_task_mm(p); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5611 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5612 | if (mm) { |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5613 | if (mc.to) |
| 5614 | mem_cgroup_move_charge(mm); |
| 5615 | put_swap_token(mm); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5616 | mmput(mm); |
| 5617 | } |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5618 | if (mc.to) |
| 5619 | mem_cgroup_clear_mc(); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5620 | } |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5621 | #else /* !CONFIG_MMU */ |
| 5622 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 5623 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5624 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5625 | { |
| 5626 | return 0; |
| 5627 | } |
| 5628 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 5629 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5630 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5631 | { |
| 5632 | } |
| 5633 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 5634 | struct cgroup *cont, |
| 5635 | struct cgroup *old_cont, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5636 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5637 | { |
| 5638 | } |
| 5639 | #endif |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5640 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5641 | struct cgroup_subsys mem_cgroup_subsys = { |
| 5642 | .name = "memory", |
| 5643 | .subsys_id = mem_cgroup_subsys_id, |
| 5644 | .create = mem_cgroup_create, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5645 | .pre_destroy = mem_cgroup_pre_destroy, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5646 | .destroy = mem_cgroup_destroy, |
| 5647 | .populate = mem_cgroup_populate, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5648 | .can_attach = mem_cgroup_can_attach, |
| 5649 | .cancel_attach = mem_cgroup_cancel_attach, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5650 | .attach = mem_cgroup_move_task, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5651 | .early_init = 0, |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5652 | .use_id = 1, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5653 | }; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5654 | |
| 5655 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5656 | static int __init enable_swap_account(char *s) |
| 5657 | { |
| 5658 | /* consider enabled if no parameter or 1 is given */ |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5659 | if (!strcmp(s, "1")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5660 | really_do_swap_account = 1; |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5661 | else if (!strcmp(s, "0")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5662 | really_do_swap_account = 0; |
| 5663 | return 1; |
| 5664 | } |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5665 | __setup("swapaccount=", enable_swap_account); |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5666 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5667 | #endif |