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> |
Hugh Dickins | 072441e | 2011-06-27 16:18:02 -0700 | [diff] [blame] | 38 | #include <linux/shmem_fs.h> |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 39 | #include <linux/slab.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 40 | #include <linux/swap.h> |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 41 | #include <linux/swapops.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 42 | #include <linux/spinlock.h> |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 43 | #include <linux/eventfd.h> |
| 44 | #include <linux/sort.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 45 | #include <linux/fs.h> |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 46 | #include <linux/seq_file.h> |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 47 | #include <linux/vmalloc.h> |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 48 | #include <linux/mm_inline.h> |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 49 | #include <linux/page_cgroup.h> |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 50 | #include <linux/cpu.h> |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 51 | #include <linux/oom.h> |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 52 | #include "internal.h" |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 53 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 54 | #include <asm/uaccess.h> |
| 55 | |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 56 | #include <trace/events/vmscan.h> |
| 57 | |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 58 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 59 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 60 | struct mem_cgroup *root_mem_cgroup __read_mostly; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 61 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 62 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 63 | /* 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] | 64 | int do_swap_account __read_mostly; |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 65 | |
| 66 | /* for remember boot option*/ |
| 67 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP_ENABLED |
| 68 | static int really_do_swap_account __initdata = 1; |
| 69 | #else |
| 70 | static int really_do_swap_account __initdata = 0; |
| 71 | #endif |
| 72 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 73 | #else |
| 74 | #define do_swap_account (0) |
| 75 | #endif |
| 76 | |
| 77 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 78 | /* |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 79 | * Statistics for memory cgroup. |
| 80 | */ |
| 81 | enum mem_cgroup_stat_index { |
| 82 | /* |
| 83 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. |
| 84 | */ |
| 85 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 86 | MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */ |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 87 | MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 88 | MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */ |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 89 | MEM_CGROUP_STAT_DATA, /* end of data requires synchronization */ |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 90 | MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 91 | MEM_CGROUP_STAT_NSTATS, |
| 92 | }; |
| 93 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 94 | enum mem_cgroup_events_index { |
| 95 | MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */ |
| 96 | MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */ |
| 97 | MEM_CGROUP_EVENTS_COUNT, /* # of pages paged in/out */ |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 98 | MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */ |
| 99 | MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */ |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 100 | MEM_CGROUP_EVENTS_NSTATS, |
| 101 | }; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 102 | /* |
| 103 | * Per memcg event counter is incremented at every pagein/pageout. With THP, |
| 104 | * it will be incremated by the number of pages. This counter is used for |
| 105 | * for trigger some periodic events. This is straightforward and better |
| 106 | * than using jiffies etc. to handle periodic memcg event. |
| 107 | */ |
| 108 | enum mem_cgroup_events_target { |
| 109 | MEM_CGROUP_TARGET_THRESH, |
| 110 | MEM_CGROUP_TARGET_SOFTLIMIT, |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 111 | MEM_CGROUP_TARGET_NUMAINFO, |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 112 | MEM_CGROUP_NTARGETS, |
| 113 | }; |
| 114 | #define THRESHOLDS_EVENTS_TARGET (128) |
| 115 | #define SOFTLIMIT_EVENTS_TARGET (1024) |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 116 | #define NUMAINFO_EVENTS_TARGET (1024) |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 117 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 118 | struct mem_cgroup_stat_cpu { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 119 | long count[MEM_CGROUP_STAT_NSTATS]; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 120 | unsigned long events[MEM_CGROUP_EVENTS_NSTATS]; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 121 | unsigned long targets[MEM_CGROUP_NTARGETS]; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 122 | }; |
| 123 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 124 | /* |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 125 | * per-zone information in memory controller. |
| 126 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 127 | struct mem_cgroup_per_zone { |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 128 | /* |
| 129 | * spin_lock to protect the per cgroup LRU |
| 130 | */ |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 131 | struct list_head lists[NR_LRU_LISTS]; |
| 132 | unsigned long count[NR_LRU_LISTS]; |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 133 | |
| 134 | struct zone_reclaim_stat reclaim_stat; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 135 | struct rb_node tree_node; /* RB tree node */ |
| 136 | unsigned long long usage_in_excess;/* Set to the value by which */ |
| 137 | /* the soft limit is exceeded*/ |
| 138 | bool on_tree; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 139 | struct mem_cgroup *mem; /* Back pointer, we cannot */ |
| 140 | /* use container_of */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 141 | }; |
| 142 | /* Macro for accessing counter */ |
| 143 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) |
| 144 | |
| 145 | struct mem_cgroup_per_node { |
| 146 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; |
| 147 | }; |
| 148 | |
| 149 | struct mem_cgroup_lru_info { |
| 150 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; |
| 151 | }; |
| 152 | |
| 153 | /* |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 154 | * Cgroups above their limits are maintained in a RB-Tree, independent of |
| 155 | * their hierarchy representation |
| 156 | */ |
| 157 | |
| 158 | struct mem_cgroup_tree_per_zone { |
| 159 | struct rb_root rb_root; |
| 160 | spinlock_t lock; |
| 161 | }; |
| 162 | |
| 163 | struct mem_cgroup_tree_per_node { |
| 164 | struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES]; |
| 165 | }; |
| 166 | |
| 167 | struct mem_cgroup_tree { |
| 168 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; |
| 169 | }; |
| 170 | |
| 171 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; |
| 172 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 173 | struct mem_cgroup_threshold { |
| 174 | struct eventfd_ctx *eventfd; |
| 175 | u64 threshold; |
| 176 | }; |
| 177 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 178 | /* For threshold */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 179 | struct mem_cgroup_threshold_ary { |
| 180 | /* An array index points to threshold just below usage. */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 181 | int current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 182 | /* Size of entries[] */ |
| 183 | unsigned int size; |
| 184 | /* Array of thresholds */ |
| 185 | struct mem_cgroup_threshold entries[0]; |
| 186 | }; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 187 | |
| 188 | struct mem_cgroup_thresholds { |
| 189 | /* Primary thresholds array */ |
| 190 | struct mem_cgroup_threshold_ary *primary; |
| 191 | /* |
| 192 | * Spare threshold array. |
| 193 | * This is needed to make mem_cgroup_unregister_event() "never fail". |
| 194 | * It must be able to store at least primary->size - 1 entries. |
| 195 | */ |
| 196 | struct mem_cgroup_threshold_ary *spare; |
| 197 | }; |
| 198 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 199 | /* for OOM */ |
| 200 | struct mem_cgroup_eventfd_list { |
| 201 | struct list_head list; |
| 202 | struct eventfd_ctx *eventfd; |
| 203 | }; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 204 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 205 | static void mem_cgroup_threshold(struct mem_cgroup *mem); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 206 | static void mem_cgroup_oom_notify(struct mem_cgroup *mem); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 207 | |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 208 | enum { |
| 209 | SCAN_BY_LIMIT, |
| 210 | SCAN_BY_SYSTEM, |
| 211 | NR_SCAN_CONTEXT, |
| 212 | SCAN_BY_SHRINK, /* not recorded now */ |
| 213 | }; |
| 214 | |
| 215 | enum { |
| 216 | SCAN, |
| 217 | SCAN_ANON, |
| 218 | SCAN_FILE, |
| 219 | ROTATE, |
| 220 | ROTATE_ANON, |
| 221 | ROTATE_FILE, |
| 222 | FREED, |
| 223 | FREED_ANON, |
| 224 | FREED_FILE, |
| 225 | ELAPSED, |
| 226 | NR_SCANSTATS, |
| 227 | }; |
| 228 | |
| 229 | struct scanstat { |
| 230 | spinlock_t lock; |
| 231 | unsigned long stats[NR_SCAN_CONTEXT][NR_SCANSTATS]; |
| 232 | unsigned long rootstats[NR_SCAN_CONTEXT][NR_SCANSTATS]; |
| 233 | }; |
| 234 | |
| 235 | const char *scanstat_string[NR_SCANSTATS] = { |
| 236 | "scanned_pages", |
| 237 | "scanned_anon_pages", |
| 238 | "scanned_file_pages", |
| 239 | "rotated_pages", |
| 240 | "rotated_anon_pages", |
| 241 | "rotated_file_pages", |
| 242 | "freed_pages", |
| 243 | "freed_anon_pages", |
| 244 | "freed_file_pages", |
| 245 | "elapsed_ns", |
| 246 | }; |
| 247 | #define SCANSTAT_WORD_LIMIT "_by_limit" |
| 248 | #define SCANSTAT_WORD_SYSTEM "_by_system" |
| 249 | #define SCANSTAT_WORD_HIERARCHY "_under_hierarchy" |
| 250 | |
| 251 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 252 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 253 | * The memory controller data structure. The memory controller controls both |
| 254 | * page cache and RSS per cgroup. We would eventually like to provide |
| 255 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, |
| 256 | * to help the administrator determine what knobs to tune. |
| 257 | * |
| 258 | * 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] | 259 | * we hit the water mark. May be even add a low water mark, such that |
| 260 | * no reclaim occurs from a cgroup at it's low water mark, this is |
| 261 | * a feature that will be implemented much later in the future. |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 262 | */ |
| 263 | struct mem_cgroup { |
| 264 | struct cgroup_subsys_state css; |
| 265 | /* |
| 266 | * the counter to account for memory usage |
| 267 | */ |
| 268 | struct res_counter res; |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 269 | /* |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 270 | * the counter to account for mem+swap usage. |
| 271 | */ |
| 272 | struct res_counter memsw; |
| 273 | /* |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 274 | * Per cgroup active and inactive list, similar to the |
| 275 | * per zone LRU lists. |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 276 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 277 | struct mem_cgroup_lru_info info; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 278 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 279 | * While reclaiming in a hierarchy, we cache the last child we |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 280 | * reclaimed from. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 281 | */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 282 | int last_scanned_child; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 283 | int last_scanned_node; |
| 284 | #if MAX_NUMNODES > 1 |
| 285 | nodemask_t scan_nodes; |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 286 | atomic_t numainfo_events; |
| 287 | atomic_t numainfo_updating; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 288 | #endif |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 289 | /* |
| 290 | * Should the accounting and control be hierarchical, per subtree? |
| 291 | */ |
| 292 | bool use_hierarchy; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 293 | |
| 294 | bool oom_lock; |
| 295 | atomic_t under_oom; |
| 296 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 297 | atomic_t refcnt; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 298 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 299 | int swappiness; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 300 | /* OOM-Killer disable */ |
| 301 | int oom_kill_disable; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 302 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 303 | /* set when res.limit == memsw.limit */ |
| 304 | bool memsw_is_minimum; |
| 305 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 306 | /* protect arrays of thresholds */ |
| 307 | struct mutex thresholds_lock; |
| 308 | |
| 309 | /* thresholds for memory usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 310 | struct mem_cgroup_thresholds thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 311 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 312 | /* thresholds for mem+swap usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 313 | struct mem_cgroup_thresholds memsw_thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 314 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 315 | /* For oom notifier event fd */ |
| 316 | struct list_head oom_notify; |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 317 | /* For recording LRU-scan statistics */ |
| 318 | struct scanstat scanstat; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 319 | /* |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 320 | * Should we move charges of a task when a task is moved into this |
| 321 | * mem_cgroup ? And what type of charges should we move ? |
| 322 | */ |
| 323 | unsigned long move_charge_at_immigrate; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 324 | /* |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 325 | * percpu counter. |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 326 | */ |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 327 | struct mem_cgroup_stat_cpu *stat; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 328 | /* |
| 329 | * used when a cpu is offlined or other synchronizations |
| 330 | * See mem_cgroup_read_stat(). |
| 331 | */ |
| 332 | struct mem_cgroup_stat_cpu nocpu_base; |
| 333 | spinlock_t pcp_counter_lock; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 334 | }; |
| 335 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 336 | /* Stuffs for move charges at task migration. */ |
| 337 | /* |
| 338 | * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a |
| 339 | * left-shifted bitmap of these types. |
| 340 | */ |
| 341 | enum move_type { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 342 | MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */ |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 343 | MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 344 | NR_MOVE_TYPE, |
| 345 | }; |
| 346 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 347 | /* "mc" and its members are protected by cgroup_mutex */ |
| 348 | static struct move_charge_struct { |
Daisuke Nishimura | b1dd693 | 2010-11-24 12:57:06 -0800 | [diff] [blame] | 349 | spinlock_t lock; /* for from, to */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 350 | struct mem_cgroup *from; |
| 351 | struct mem_cgroup *to; |
| 352 | unsigned long precharge; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 353 | unsigned long moved_charge; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 354 | unsigned long moved_swap; |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 355 | struct task_struct *moving_task; /* a task moving charges */ |
| 356 | wait_queue_head_t waitq; /* a waitq for other context */ |
| 357 | } mc = { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 358 | .lock = __SPIN_LOCK_UNLOCKED(mc.lock), |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 359 | .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq), |
| 360 | }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 361 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 362 | static bool move_anon(void) |
| 363 | { |
| 364 | return test_bit(MOVE_CHARGE_TYPE_ANON, |
| 365 | &mc.to->move_charge_at_immigrate); |
| 366 | } |
| 367 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 368 | static bool move_file(void) |
| 369 | { |
| 370 | return test_bit(MOVE_CHARGE_TYPE_FILE, |
| 371 | &mc.to->move_charge_at_immigrate); |
| 372 | } |
| 373 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 374 | /* |
| 375 | * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft |
| 376 | * limit reclaim to prevent infinite loops, if they ever occur. |
| 377 | */ |
| 378 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100) |
| 379 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2) |
| 380 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 381 | enum charge_type { |
| 382 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
| 383 | MEM_CGROUP_CHARGE_TYPE_MAPPED, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 384 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 385 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 386 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 387 | MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 388 | NR_CHARGE_TYPE, |
| 389 | }; |
| 390 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 391 | /* for encoding cft->private value on file */ |
| 392 | #define _MEM (0) |
| 393 | #define _MEMSWAP (1) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 394 | #define _OOM_TYPE (2) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 395 | #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) |
| 396 | #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff) |
| 397 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 398 | /* Used for OOM nofiier */ |
| 399 | #define OOM_CONTROL (0) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 400 | |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 401 | /* |
| 402 | * Reclaim flags for mem_cgroup_hierarchical_reclaim |
| 403 | */ |
| 404 | #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0 |
| 405 | #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT) |
| 406 | #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1 |
| 407 | #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 408 | #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2 |
| 409 | #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT) |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 410 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 411 | static void mem_cgroup_get(struct mem_cgroup *mem); |
| 412 | static void mem_cgroup_put(struct mem_cgroup *mem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 413 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 414 | static void drain_all_stock_async(struct mem_cgroup *mem); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 415 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 416 | static struct mem_cgroup_per_zone * |
| 417 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) |
| 418 | { |
| 419 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; |
| 420 | } |
| 421 | |
Wu Fengguang | d324236 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 422 | struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem) |
| 423 | { |
| 424 | return &mem->css; |
| 425 | } |
| 426 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 427 | static struct mem_cgroup_per_zone * |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 428 | page_cgroup_zoneinfo(struct mem_cgroup *mem, struct page *page) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 429 | { |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 430 | int nid = page_to_nid(page); |
| 431 | int zid = page_zonenum(page); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 432 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 433 | return mem_cgroup_zoneinfo(mem, nid, zid); |
| 434 | } |
| 435 | |
| 436 | static struct mem_cgroup_tree_per_zone * |
| 437 | soft_limit_tree_node_zone(int nid, int zid) |
| 438 | { |
| 439 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 440 | } |
| 441 | |
| 442 | static struct mem_cgroup_tree_per_zone * |
| 443 | soft_limit_tree_from_page(struct page *page) |
| 444 | { |
| 445 | int nid = page_to_nid(page); |
| 446 | int zid = page_zonenum(page); |
| 447 | |
| 448 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 449 | } |
| 450 | |
| 451 | static void |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 452 | __mem_cgroup_insert_exceeded(struct mem_cgroup *mem, |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 453 | struct mem_cgroup_per_zone *mz, |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 454 | struct mem_cgroup_tree_per_zone *mctz, |
| 455 | unsigned long long new_usage_in_excess) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 456 | { |
| 457 | struct rb_node **p = &mctz->rb_root.rb_node; |
| 458 | struct rb_node *parent = NULL; |
| 459 | struct mem_cgroup_per_zone *mz_node; |
| 460 | |
| 461 | if (mz->on_tree) |
| 462 | return; |
| 463 | |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 464 | mz->usage_in_excess = new_usage_in_excess; |
| 465 | if (!mz->usage_in_excess) |
| 466 | return; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 467 | while (*p) { |
| 468 | parent = *p; |
| 469 | mz_node = rb_entry(parent, struct mem_cgroup_per_zone, |
| 470 | tree_node); |
| 471 | if (mz->usage_in_excess < mz_node->usage_in_excess) |
| 472 | p = &(*p)->rb_left; |
| 473 | /* |
| 474 | * We can't avoid mem cgroups that are over their soft |
| 475 | * limit by the same amount |
| 476 | */ |
| 477 | else if (mz->usage_in_excess >= mz_node->usage_in_excess) |
| 478 | p = &(*p)->rb_right; |
| 479 | } |
| 480 | rb_link_node(&mz->tree_node, parent, p); |
| 481 | rb_insert_color(&mz->tree_node, &mctz->rb_root); |
| 482 | mz->on_tree = true; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static void |
| 486 | __mem_cgroup_remove_exceeded(struct mem_cgroup *mem, |
| 487 | struct mem_cgroup_per_zone *mz, |
| 488 | struct mem_cgroup_tree_per_zone *mctz) |
| 489 | { |
| 490 | if (!mz->on_tree) |
| 491 | return; |
| 492 | rb_erase(&mz->tree_node, &mctz->rb_root); |
| 493 | mz->on_tree = false; |
| 494 | } |
| 495 | |
| 496 | static void |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 497 | mem_cgroup_remove_exceeded(struct mem_cgroup *mem, |
| 498 | struct mem_cgroup_per_zone *mz, |
| 499 | struct mem_cgroup_tree_per_zone *mctz) |
| 500 | { |
| 501 | spin_lock(&mctz->lock); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 502 | __mem_cgroup_remove_exceeded(mem, mz, mctz); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 503 | spin_unlock(&mctz->lock); |
| 504 | } |
| 505 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 506 | |
| 507 | static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page) |
| 508 | { |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 509 | unsigned long long excess; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 510 | struct mem_cgroup_per_zone *mz; |
| 511 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 512 | int nid = page_to_nid(page); |
| 513 | int zid = page_zonenum(page); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 514 | mctz = soft_limit_tree_from_page(page); |
| 515 | |
| 516 | /* |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 517 | * Necessary to update all ancestors when hierarchy is used. |
| 518 | * because their event counter is not touched. |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 519 | */ |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 520 | for (; mem; mem = parent_mem_cgroup(mem)) { |
| 521 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 522 | excess = res_counter_soft_limit_excess(&mem->res); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 523 | /* |
| 524 | * We have to update the tree if mz is on RB-tree or |
| 525 | * mem is over its softlimit. |
| 526 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 527 | if (excess || mz->on_tree) { |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 528 | spin_lock(&mctz->lock); |
| 529 | /* if on-tree, remove it */ |
| 530 | if (mz->on_tree) |
| 531 | __mem_cgroup_remove_exceeded(mem, mz, mctz); |
| 532 | /* |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 533 | * Insert again. mz->usage_in_excess will be updated. |
| 534 | * If excess is 0, no tree ops. |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 535 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 536 | __mem_cgroup_insert_exceeded(mem, mz, mctz, excess); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 537 | spin_unlock(&mctz->lock); |
| 538 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
| 542 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem) |
| 543 | { |
| 544 | int node, zone; |
| 545 | struct mem_cgroup_per_zone *mz; |
| 546 | struct mem_cgroup_tree_per_zone *mctz; |
| 547 | |
| 548 | for_each_node_state(node, N_POSSIBLE) { |
| 549 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 550 | mz = mem_cgroup_zoneinfo(mem, node, zone); |
| 551 | mctz = soft_limit_tree_node_zone(node, zone); |
| 552 | mem_cgroup_remove_exceeded(mem, mz, mctz); |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 557 | static struct mem_cgroup_per_zone * |
| 558 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 559 | { |
| 560 | struct rb_node *rightmost = NULL; |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 561 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 562 | |
| 563 | retry: |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 564 | mz = NULL; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 565 | rightmost = rb_last(&mctz->rb_root); |
| 566 | if (!rightmost) |
| 567 | goto done; /* Nothing to reclaim from */ |
| 568 | |
| 569 | mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node); |
| 570 | /* |
| 571 | * Remove the node now but someone else can add it back, |
| 572 | * we will to add it back at the end of reclaim to its correct |
| 573 | * position in the tree. |
| 574 | */ |
| 575 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
| 576 | if (!res_counter_soft_limit_excess(&mz->mem->res) || |
| 577 | !css_tryget(&mz->mem->css)) |
| 578 | goto retry; |
| 579 | done: |
| 580 | return mz; |
| 581 | } |
| 582 | |
| 583 | static struct mem_cgroup_per_zone * |
| 584 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 585 | { |
| 586 | struct mem_cgroup_per_zone *mz; |
| 587 | |
| 588 | spin_lock(&mctz->lock); |
| 589 | mz = __mem_cgroup_largest_soft_limit_node(mctz); |
| 590 | spin_unlock(&mctz->lock); |
| 591 | return mz; |
| 592 | } |
| 593 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 594 | /* |
| 595 | * Implementation Note: reading percpu statistics for memcg. |
| 596 | * |
| 597 | * Both of vmstat[] and percpu_counter has threshold and do periodic |
| 598 | * synchronization to implement "quick" read. There are trade-off between |
| 599 | * reading cost and precision of value. Then, we may have a chance to implement |
| 600 | * a periodic synchronizion of counter in memcg's counter. |
| 601 | * |
| 602 | * But this _read() function is used for user interface now. The user accounts |
| 603 | * memory usage by memory cgroup and he _always_ requires exact value because |
| 604 | * he accounts memory. Even if we provide quick-and-fuzzy read, we always |
| 605 | * have to visit all online cpus and make sum. So, for now, unnecessary |
| 606 | * synchronization is not implemented. (just implemented for cpu hotplug) |
| 607 | * |
| 608 | * If there are kernel internal actions which can make use of some not-exact |
| 609 | * value, and reading all cpu value can be performance bottleneck in some |
| 610 | * common workload, threashold and synchonization as vmstat[] should be |
| 611 | * implemented. |
| 612 | */ |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 613 | static long mem_cgroup_read_stat(struct mem_cgroup *mem, |
| 614 | enum mem_cgroup_stat_index idx) |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 615 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 616 | long val = 0; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 617 | int cpu; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 618 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 619 | get_online_cpus(); |
| 620 | for_each_online_cpu(cpu) |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 621 | val += per_cpu(mem->stat->count[idx], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 622 | #ifdef CONFIG_HOTPLUG_CPU |
| 623 | spin_lock(&mem->pcp_counter_lock); |
| 624 | val += mem->nocpu_base.count[idx]; |
| 625 | spin_unlock(&mem->pcp_counter_lock); |
| 626 | #endif |
| 627 | put_online_cpus(); |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 628 | return val; |
| 629 | } |
| 630 | |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 631 | static void mem_cgroup_swap_statistics(struct mem_cgroup *mem, |
| 632 | bool charge) |
| 633 | { |
| 634 | int val = (charge) ? 1 : -1; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 635 | this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 638 | void mem_cgroup_pgfault(struct mem_cgroup *mem, int val) |
| 639 | { |
| 640 | this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val); |
| 641 | } |
| 642 | |
| 643 | void mem_cgroup_pgmajfault(struct mem_cgroup *mem, int val) |
| 644 | { |
| 645 | this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val); |
| 646 | } |
| 647 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 648 | static unsigned long mem_cgroup_read_events(struct mem_cgroup *mem, |
| 649 | enum mem_cgroup_events_index idx) |
| 650 | { |
| 651 | unsigned long val = 0; |
| 652 | int cpu; |
| 653 | |
| 654 | for_each_online_cpu(cpu) |
| 655 | val += per_cpu(mem->stat->events[idx], cpu); |
| 656 | #ifdef CONFIG_HOTPLUG_CPU |
| 657 | spin_lock(&mem->pcp_counter_lock); |
| 658 | val += mem->nocpu_base.events[idx]; |
| 659 | spin_unlock(&mem->pcp_counter_lock); |
| 660 | #endif |
| 661 | return val; |
| 662 | } |
| 663 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 664 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 665 | bool file, int nr_pages) |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 666 | { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 667 | preempt_disable(); |
| 668 | |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 669 | if (file) |
| 670 | __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], nr_pages); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 671 | else |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 672 | __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], nr_pages); |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 673 | |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 674 | /* pagein of a big page is an event. So, ignore page size */ |
| 675 | if (nr_pages > 0) |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 676 | __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGIN]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 677 | else { |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 678 | __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 679 | nr_pages = -nr_pages; /* for event */ |
| 680 | } |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 681 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 682 | __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] | 683 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 684 | preempt_enable(); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 685 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 686 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 687 | unsigned long |
| 688 | mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *mem, int nid, int zid, |
| 689 | unsigned int lru_mask) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 690 | { |
| 691 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 692 | enum lru_list l; |
| 693 | unsigned long ret = 0; |
| 694 | |
| 695 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 696 | |
| 697 | for_each_lru(l) { |
| 698 | if (BIT(l) & lru_mask) |
| 699 | ret += MEM_CGROUP_ZSTAT(mz, l); |
| 700 | } |
| 701 | return ret; |
| 702 | } |
| 703 | |
| 704 | static unsigned long |
| 705 | mem_cgroup_node_nr_lru_pages(struct mem_cgroup *mem, |
| 706 | int nid, unsigned int lru_mask) |
| 707 | { |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 708 | u64 total = 0; |
| 709 | int zid; |
| 710 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 711 | for (zid = 0; zid < MAX_NR_ZONES; zid++) |
| 712 | total += mem_cgroup_zone_nr_lru_pages(mem, nid, zid, lru_mask); |
| 713 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 714 | return total; |
| 715 | } |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 716 | |
| 717 | static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *mem, |
| 718 | unsigned int lru_mask) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 719 | { |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 720 | int nid; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 721 | u64 total = 0; |
| 722 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 723 | for_each_node_state(nid, N_HIGH_MEMORY) |
| 724 | total += mem_cgroup_node_nr_lru_pages(mem, nid, lru_mask); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 725 | return total; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 726 | } |
| 727 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 728 | static bool __memcg_event_check(struct mem_cgroup *mem, int target) |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 729 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 730 | unsigned long val, next; |
| 731 | |
| 732 | val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]); |
| 733 | next = this_cpu_read(mem->stat->targets[target]); |
| 734 | /* from time_after() in jiffies.h */ |
| 735 | return ((long)next - (long)val < 0); |
| 736 | } |
| 737 | |
| 738 | static void __mem_cgroup_target_update(struct mem_cgroup *mem, int target) |
| 739 | { |
| 740 | unsigned long val, next; |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 741 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 742 | val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 743 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 744 | switch (target) { |
| 745 | case MEM_CGROUP_TARGET_THRESH: |
| 746 | next = val + THRESHOLDS_EVENTS_TARGET; |
| 747 | break; |
| 748 | case MEM_CGROUP_TARGET_SOFTLIMIT: |
| 749 | next = val + SOFTLIMIT_EVENTS_TARGET; |
| 750 | break; |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 751 | case MEM_CGROUP_TARGET_NUMAINFO: |
| 752 | next = val + NUMAINFO_EVENTS_TARGET; |
| 753 | break; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 754 | default: |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | this_cpu_write(mem->stat->targets[target], next); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /* |
| 762 | * Check events in order. |
| 763 | * |
| 764 | */ |
| 765 | static void memcg_check_events(struct mem_cgroup *mem, struct page *page) |
| 766 | { |
| 767 | /* threshold event is triggered in finer grain than soft limit */ |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 768 | if (unlikely(__memcg_event_check(mem, MEM_CGROUP_TARGET_THRESH))) { |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 769 | mem_cgroup_threshold(mem); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 770 | __mem_cgroup_target_update(mem, MEM_CGROUP_TARGET_THRESH); |
| 771 | if (unlikely(__memcg_event_check(mem, |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 772 | MEM_CGROUP_TARGET_SOFTLIMIT))) { |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 773 | mem_cgroup_update_tree(mem, page); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 774 | __mem_cgroup_target_update(mem, |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 775 | MEM_CGROUP_TARGET_SOFTLIMIT); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 776 | } |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 777 | #if MAX_NUMNODES > 1 |
| 778 | if (unlikely(__memcg_event_check(mem, |
| 779 | MEM_CGROUP_TARGET_NUMAINFO))) { |
| 780 | atomic_inc(&mem->numainfo_events); |
| 781 | __mem_cgroup_target_update(mem, |
| 782 | MEM_CGROUP_TARGET_NUMAINFO); |
| 783 | } |
| 784 | #endif |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 788 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 789 | { |
| 790 | return container_of(cgroup_subsys_state(cont, |
| 791 | mem_cgroup_subsys_id), struct mem_cgroup, |
| 792 | css); |
| 793 | } |
| 794 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 795 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 796 | { |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 797 | /* |
| 798 | * mm_update_next_owner() may clear mm->owner to NULL |
| 799 | * if it races with swapoff, page migration, etc. |
| 800 | * So this can be called with p == NULL. |
| 801 | */ |
| 802 | if (unlikely(!p)) |
| 803 | return NULL; |
| 804 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 805 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
| 806 | struct mem_cgroup, css); |
| 807 | } |
| 808 | |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 809 | 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] | 810 | { |
| 811 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 812 | |
| 813 | if (!mm) |
| 814 | return NULL; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 815 | /* |
| 816 | * Because we have no locks, mm->owner's may be being moved to other |
| 817 | * cgroup. We use css_tryget() here even if this looks |
| 818 | * pessimistic (rather than adding locks here). |
| 819 | */ |
| 820 | rcu_read_lock(); |
| 821 | do { |
| 822 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 823 | if (unlikely(!mem)) |
| 824 | break; |
| 825 | } while (!css_tryget(&mem->css)); |
| 826 | rcu_read_unlock(); |
| 827 | return mem; |
| 828 | } |
| 829 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 830 | /* The caller has to guarantee "mem" exists before calling this */ |
| 831 | static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 832 | { |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 833 | struct cgroup_subsys_state *css; |
| 834 | int found; |
| 835 | |
| 836 | if (!mem) /* ROOT cgroup has the smallest ID */ |
| 837 | return root_mem_cgroup; /*css_put/get against root is ignored*/ |
| 838 | if (!mem->use_hierarchy) { |
| 839 | if (css_tryget(&mem->css)) |
| 840 | return mem; |
| 841 | return NULL; |
| 842 | } |
| 843 | rcu_read_lock(); |
| 844 | /* |
| 845 | * searching a memory cgroup which has the smallest ID under given |
| 846 | * ROOT cgroup. (ID >= 1) |
| 847 | */ |
| 848 | css = css_get_next(&mem_cgroup_subsys, 1, &mem->css, &found); |
| 849 | if (css && css_tryget(css)) |
| 850 | mem = container_of(css, struct mem_cgroup, css); |
| 851 | else |
| 852 | mem = NULL; |
| 853 | rcu_read_unlock(); |
| 854 | return mem; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 855 | } |
| 856 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 857 | static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter, |
| 858 | struct mem_cgroup *root, |
| 859 | bool cond) |
| 860 | { |
| 861 | int nextid = css_id(&iter->css) + 1; |
| 862 | int found; |
| 863 | int hierarchy_used; |
| 864 | struct cgroup_subsys_state *css; |
| 865 | |
| 866 | hierarchy_used = iter->use_hierarchy; |
| 867 | |
| 868 | css_put(&iter->css); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 869 | /* If no ROOT, walk all, ignore hierarchy */ |
| 870 | if (!cond || (root && !hierarchy_used)) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 871 | return NULL; |
| 872 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 873 | if (!root) |
| 874 | root = root_mem_cgroup; |
| 875 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 876 | do { |
| 877 | iter = NULL; |
| 878 | rcu_read_lock(); |
| 879 | |
| 880 | css = css_get_next(&mem_cgroup_subsys, nextid, |
| 881 | &root->css, &found); |
| 882 | if (css && css_tryget(css)) |
| 883 | iter = container_of(css, struct mem_cgroup, css); |
| 884 | rcu_read_unlock(); |
| 885 | /* If css is NULL, no more cgroups will be found */ |
| 886 | nextid = found + 1; |
| 887 | } while (css && !iter); |
| 888 | |
| 889 | return iter; |
| 890 | } |
| 891 | /* |
| 892 | * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please |
| 893 | * be careful that "break" loop is not allowed. We have reference count. |
| 894 | * Instead of that modify "cond" to be false and "continue" to exit the loop. |
| 895 | */ |
| 896 | #define for_each_mem_cgroup_tree_cond(iter, root, cond) \ |
| 897 | for (iter = mem_cgroup_start_loop(root);\ |
| 898 | iter != NULL;\ |
| 899 | iter = mem_cgroup_get_next(iter, root, cond)) |
| 900 | |
| 901 | #define for_each_mem_cgroup_tree(iter, root) \ |
| 902 | for_each_mem_cgroup_tree_cond(iter, root, true) |
| 903 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 904 | #define for_each_mem_cgroup_all(iter) \ |
| 905 | for_each_mem_cgroup_tree_cond(iter, NULL, true) |
| 906 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 907 | |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 908 | static inline bool mem_cgroup_is_root(struct mem_cgroup *mem) |
| 909 | { |
| 910 | return (mem == root_mem_cgroup); |
| 911 | } |
| 912 | |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 913 | void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx) |
| 914 | { |
| 915 | struct mem_cgroup *mem; |
| 916 | |
| 917 | if (!mm) |
| 918 | return; |
| 919 | |
| 920 | rcu_read_lock(); |
| 921 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 922 | if (unlikely(!mem)) |
| 923 | goto out; |
| 924 | |
| 925 | switch (idx) { |
| 926 | case PGMAJFAULT: |
| 927 | mem_cgroup_pgmajfault(mem, 1); |
| 928 | break; |
| 929 | case PGFAULT: |
| 930 | mem_cgroup_pgfault(mem, 1); |
| 931 | break; |
| 932 | default: |
| 933 | BUG(); |
| 934 | } |
| 935 | out: |
| 936 | rcu_read_unlock(); |
| 937 | } |
| 938 | EXPORT_SYMBOL(mem_cgroup_count_vm_event); |
| 939 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 940 | /* |
| 941 | * Following LRU functions are allowed to be used without PCG_LOCK. |
| 942 | * Operations are called by routine of global LRU independently from memcg. |
| 943 | * What we have to take care of here is validness of pc->mem_cgroup. |
| 944 | * |
| 945 | * Changes to pc->mem_cgroup happens when |
| 946 | * 1. charge |
| 947 | * 2. moving account |
| 948 | * In typical case, "charge" is done before add-to-lru. Exception is SwapCache. |
| 949 | * It is added to LRU before charge. |
| 950 | * If PCG_USED bit is not set, page_cgroup is not added to this private LRU. |
| 951 | * When moving account, the page is not on LRU. It's isolated. |
| 952 | */ |
| 953 | |
| 954 | 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] | 955 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 956 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 957 | struct mem_cgroup_per_zone *mz; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 958 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 959 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 960 | return; |
| 961 | pc = lookup_page_cgroup(page); |
| 962 | /* can happen while we handle swapcache. */ |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 963 | if (!TestClearPageCgroupAcctLRU(pc)) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 964 | return; |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 965 | VM_BUG_ON(!pc->mem_cgroup); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 966 | /* |
| 967 | * We don't check PCG_USED bit. It's cleared when the "page" is finally |
| 968 | * removed from global LRU. |
| 969 | */ |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 970 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 971 | /* huge page split is done under lru_lock. so, we have no races. */ |
| 972 | MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 973 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 974 | return; |
| 975 | VM_BUG_ON(list_empty(&pc->lru)); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 976 | list_del_init(&pc->lru); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 977 | } |
| 978 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 979 | void mem_cgroup_del_lru(struct page *page) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 980 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 981 | mem_cgroup_del_lru_list(page, page_lru(page)); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 982 | } |
| 983 | |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 984 | /* |
| 985 | * Writeback is about to end against a page which has been marked for immediate |
| 986 | * reclaim. If it still appears to be reclaimable, move it to the tail of the |
| 987 | * inactive list. |
| 988 | */ |
| 989 | void mem_cgroup_rotate_reclaimable_page(struct page *page) |
| 990 | { |
| 991 | struct mem_cgroup_per_zone *mz; |
| 992 | struct page_cgroup *pc; |
| 993 | enum lru_list lru = page_lru(page); |
| 994 | |
| 995 | if (mem_cgroup_disabled()) |
| 996 | return; |
| 997 | |
| 998 | pc = lookup_page_cgroup(page); |
| 999 | /* unused or root page is not rotated. */ |
| 1000 | if (!PageCgroupUsed(pc)) |
| 1001 | return; |
| 1002 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 1003 | smp_rmb(); |
| 1004 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 1005 | return; |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1006 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 1007 | list_move_tail(&pc->lru, &mz->lists[lru]); |
| 1008 | } |
| 1009 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1010 | 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] | 1011 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1012 | struct mem_cgroup_per_zone *mz; |
| 1013 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1014 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1015 | if (mem_cgroup_disabled()) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 1016 | return; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1017 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1018 | pc = lookup_page_cgroup(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 1019 | /* unused or root page is not rotated. */ |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 1020 | if (!PageCgroupUsed(pc)) |
| 1021 | return; |
| 1022 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 1023 | smp_rmb(); |
| 1024 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1025 | return; |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1026 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1027 | list_move(&pc->lru, &mz->lists[lru]); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1030 | void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru) |
| 1031 | { |
| 1032 | struct page_cgroup *pc; |
| 1033 | struct mem_cgroup_per_zone *mz; |
| 1034 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1035 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1036 | return; |
| 1037 | pc = lookup_page_cgroup(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 1038 | VM_BUG_ON(PageCgroupAcctLRU(pc)); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1039 | if (!PageCgroupUsed(pc)) |
| 1040 | return; |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 1041 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 1042 | smp_rmb(); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1043 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 1044 | /* huge page split is done under lru_lock. so, we have no races. */ |
| 1045 | MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 1046 | SetPageCgroupAcctLRU(pc); |
| 1047 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 1048 | return; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1049 | list_add(&pc->lru, &mz->lists[lru]); |
| 1050 | } |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1051 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1052 | /* |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1053 | * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed |
| 1054 | * while it's linked to lru because the page may be reused after it's fully |
| 1055 | * uncharged. To handle that, unlink page_cgroup from LRU when charge it again. |
| 1056 | * 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] | 1057 | */ |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1058 | static void mem_cgroup_lru_del_before_commit(struct page *page) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1059 | { |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1060 | unsigned long flags; |
| 1061 | struct zone *zone = page_zone(page); |
| 1062 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 1063 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1064 | /* |
| 1065 | * Doing this check without taking ->lru_lock seems wrong but this |
| 1066 | * is safe. Because if page_cgroup's USED bit is unset, the page |
| 1067 | * will not be added to any memcg's LRU. If page_cgroup's USED bit is |
| 1068 | * set, the commit after this will fail, anyway. |
| 1069 | * This all charge/uncharge is done under some mutual execustion. |
| 1070 | * So, we don't need to taking care of changes in USED bit. |
| 1071 | */ |
| 1072 | if (likely(!PageLRU(page))) |
| 1073 | return; |
| 1074 | |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1075 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 1076 | /* |
| 1077 | * Forget old LRU when this page_cgroup is *not* used. This Used bit |
| 1078 | * is guarded by lock_page() because the page is SwapCache. |
| 1079 | */ |
| 1080 | if (!PageCgroupUsed(pc)) |
| 1081 | mem_cgroup_del_lru_list(page, page_lru(page)); |
| 1082 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1085 | static void mem_cgroup_lru_add_after_commit(struct page *page) |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1086 | { |
| 1087 | unsigned long flags; |
| 1088 | struct zone *zone = page_zone(page); |
| 1089 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 1090 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1091 | /* taking care of that the page is added to LRU while we commit it */ |
| 1092 | if (likely(!PageLRU(page))) |
| 1093 | return; |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1094 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 1095 | /* 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] | 1096 | if (PageLRU(page) && !PageCgroupAcctLRU(pc)) |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1097 | mem_cgroup_add_lru_list(page, page_lru(page)); |
| 1098 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
| 1099 | } |
| 1100 | |
| 1101 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1102 | void mem_cgroup_move_lists(struct page *page, |
| 1103 | enum lru_list from, enum lru_list to) |
| 1104 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1105 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1106 | return; |
| 1107 | mem_cgroup_del_lru_list(page, from); |
| 1108 | mem_cgroup_add_lru_list(page, to); |
| 1109 | } |
| 1110 | |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame^] | 1111 | /* |
| 1112 | * Checks whether given mem is same or in the root_mem's |
| 1113 | * hierarchy subtree |
| 1114 | */ |
| 1115 | static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_mem, |
| 1116 | struct mem_cgroup *mem) |
| 1117 | { |
| 1118 | if (root_mem != mem) { |
| 1119 | return (root_mem->use_hierarchy && |
| 1120 | css_is_ancestor(&mem->css, &root_mem->css)); |
| 1121 | } |
| 1122 | |
| 1123 | return true; |
| 1124 | } |
| 1125 | |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1126 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
| 1127 | { |
| 1128 | int ret; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1129 | struct mem_cgroup *curr = NULL; |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1130 | struct task_struct *p; |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1131 | |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1132 | p = find_lock_task_mm(task); |
| 1133 | if (!p) |
| 1134 | return 0; |
| 1135 | curr = try_get_mem_cgroup_from_mm(p->mm); |
| 1136 | task_unlock(p); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1137 | if (!curr) |
| 1138 | return 0; |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1139 | /* |
| 1140 | * We should check use_hierarchy of "mem" not "curr". Because checking |
| 1141 | * use_hierarchy of "curr" here make this function true if hierarchy is |
| 1142 | * enabled in "curr" and "curr" is a child of "mem" in *cgroup* |
| 1143 | * hierarchy(even if use_hierarchy is disabled in "mem"). |
| 1144 | */ |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame^] | 1145 | ret = mem_cgroup_same_or_subtree(mem, curr); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1146 | css_put(&curr->css); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1147 | return ret; |
| 1148 | } |
| 1149 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1150 | 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] | 1151 | { |
| 1152 | unsigned long active; |
| 1153 | unsigned long inactive; |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1154 | unsigned long gb; |
| 1155 | unsigned long inactive_ratio; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1156 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1157 | inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON)); |
| 1158 | active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON)); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1159 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1160 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
| 1161 | if (gb) |
| 1162 | inactive_ratio = int_sqrt(10 * gb); |
| 1163 | else |
| 1164 | inactive_ratio = 1; |
| 1165 | |
| 1166 | if (present_pages) { |
| 1167 | present_pages[0] = inactive; |
| 1168 | present_pages[1] = active; |
| 1169 | } |
| 1170 | |
| 1171 | return inactive_ratio; |
| 1172 | } |
| 1173 | |
| 1174 | int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg) |
| 1175 | { |
| 1176 | unsigned long active; |
| 1177 | unsigned long inactive; |
| 1178 | unsigned long present_pages[2]; |
| 1179 | unsigned long inactive_ratio; |
| 1180 | |
| 1181 | inactive_ratio = calc_inactive_ratio(memcg, present_pages); |
| 1182 | |
| 1183 | inactive = present_pages[0]; |
| 1184 | active = present_pages[1]; |
| 1185 | |
| 1186 | if (inactive * inactive_ratio < active) |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1187 | return 1; |
| 1188 | |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
Rik van Riel | 56e49d2 | 2009-06-16 15:32:28 -0700 | [diff] [blame] | 1192 | int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg) |
| 1193 | { |
| 1194 | unsigned long active; |
| 1195 | unsigned long inactive; |
| 1196 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1197 | inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE)); |
| 1198 | active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE)); |
Rik van Riel | 56e49d2 | 2009-06-16 15:32:28 -0700 | [diff] [blame] | 1199 | |
| 1200 | return (active > inactive); |
| 1201 | } |
| 1202 | |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1203 | struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg, |
| 1204 | struct zone *zone) |
| 1205 | { |
KOSAKI Motohiro | 13d7e3a | 2010-08-10 18:03:06 -0700 | [diff] [blame] | 1206 | int nid = zone_to_nid(zone); |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1207 | int zid = zone_idx(zone); |
| 1208 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 1209 | |
| 1210 | return &mz->reclaim_stat; |
| 1211 | } |
| 1212 | |
| 1213 | struct zone_reclaim_stat * |
| 1214 | mem_cgroup_get_reclaim_stat_from_page(struct page *page) |
| 1215 | { |
| 1216 | struct page_cgroup *pc; |
| 1217 | struct mem_cgroup_per_zone *mz; |
| 1218 | |
| 1219 | if (mem_cgroup_disabled()) |
| 1220 | return NULL; |
| 1221 | |
| 1222 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 1223 | if (!PageCgroupUsed(pc)) |
| 1224 | return NULL; |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 1225 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 1226 | smp_rmb(); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1227 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1228 | return &mz->reclaim_stat; |
| 1229 | } |
| 1230 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1231 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 1232 | struct list_head *dst, |
| 1233 | unsigned long *scanned, int order, |
| 1234 | int mode, struct zone *z, |
| 1235 | struct mem_cgroup *mem_cont, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1236 | int active, int file) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1237 | { |
| 1238 | unsigned long nr_taken = 0; |
| 1239 | struct page *page; |
| 1240 | unsigned long scan; |
| 1241 | LIST_HEAD(pc_list); |
| 1242 | struct list_head *src; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1243 | struct page_cgroup *pc, *tmp; |
KOSAKI Motohiro | 13d7e3a | 2010-08-10 18:03:06 -0700 | [diff] [blame] | 1244 | int nid = zone_to_nid(z); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1245 | int zid = zone_idx(z); |
| 1246 | struct mem_cgroup_per_zone *mz; |
Johannes Weiner | b7c46d1 | 2009-09-21 17:02:56 -0700 | [diff] [blame] | 1247 | int lru = LRU_FILE * file + active; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1248 | int ret; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1249 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 1250 | BUG_ON(!mem_cont); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1251 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1252 | src = &mz->lists[lru]; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1253 | |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1254 | scan = 0; |
| 1255 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1256 | if (scan >= nr_to_scan) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1257 | break; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1258 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1259 | if (unlikely(!PageCgroupUsed(pc))) |
| 1260 | continue; |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 1261 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 1262 | page = lookup_cgroup_page(pc); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 1263 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1264 | if (unlikely(!PageLRU(page))) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1265 | continue; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1266 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1267 | scan++; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1268 | ret = __isolate_lru_page(page, mode, file); |
| 1269 | switch (ret) { |
| 1270 | case 0: |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1271 | list_move(&page->lru, dst); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1272 | mem_cgroup_del_lru(page); |
Rik van Riel | 2c888cf | 2011-01-13 15:47:13 -0800 | [diff] [blame] | 1273 | nr_taken += hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1274 | break; |
| 1275 | case -EBUSY: |
| 1276 | /* we don't affect global LRU but rotate in our LRU */ |
| 1277 | mem_cgroup_rotate_lru_list(page, page_lru(page)); |
| 1278 | break; |
| 1279 | default: |
| 1280 | break; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1284 | *scanned = scan; |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 1285 | |
| 1286 | trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken, |
| 1287 | 0, 0, 0, mode); |
| 1288 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1289 | return nr_taken; |
| 1290 | } |
| 1291 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1292 | #define mem_cgroup_from_res_counter(counter, member) \ |
| 1293 | container_of(counter, struct mem_cgroup, member) |
| 1294 | |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1295 | /** |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1296 | * mem_cgroup_margin - calculate chargeable space of a memory cgroup |
| 1297 | * @mem: the memory cgroup |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1298 | * |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1299 | * Returns the maximum amount of memory @mem can be charged with, in |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1300 | * pages. |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1301 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1302 | static unsigned long mem_cgroup_margin(struct mem_cgroup *mem) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1303 | { |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1304 | unsigned long long margin; |
| 1305 | |
| 1306 | margin = res_counter_margin(&mem->res); |
| 1307 | if (do_swap_account) |
| 1308 | margin = min(margin, res_counter_margin(&mem->memsw)); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1309 | return margin >> PAGE_SHIFT; |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1310 | } |
| 1311 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 1312 | int mem_cgroup_swappiness(struct mem_cgroup *memcg) |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1313 | { |
| 1314 | struct cgroup *cgrp = memcg->css.cgroup; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1315 | |
| 1316 | /* root ? */ |
| 1317 | if (cgrp->parent == NULL) |
| 1318 | return vm_swappiness; |
| 1319 | |
Johannes Weiner | bf1ff26 | 2011-03-23 16:42:32 -0700 | [diff] [blame] | 1320 | return memcg->swappiness; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1321 | } |
| 1322 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1323 | static void mem_cgroup_start_move(struct mem_cgroup *mem) |
| 1324 | { |
| 1325 | int cpu; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1326 | |
| 1327 | get_online_cpus(); |
| 1328 | spin_lock(&mem->pcp_counter_lock); |
| 1329 | for_each_online_cpu(cpu) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1330 | per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1331 | mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1; |
| 1332 | spin_unlock(&mem->pcp_counter_lock); |
| 1333 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1334 | |
| 1335 | synchronize_rcu(); |
| 1336 | } |
| 1337 | |
| 1338 | static void mem_cgroup_end_move(struct mem_cgroup *mem) |
| 1339 | { |
| 1340 | int cpu; |
| 1341 | |
| 1342 | if (!mem) |
| 1343 | return; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1344 | get_online_cpus(); |
| 1345 | spin_lock(&mem->pcp_counter_lock); |
| 1346 | for_each_online_cpu(cpu) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1347 | per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1348 | mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1; |
| 1349 | spin_unlock(&mem->pcp_counter_lock); |
| 1350 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1351 | } |
| 1352 | /* |
| 1353 | * 2 routines for checking "mem" is under move_account() or not. |
| 1354 | * |
| 1355 | * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used |
| 1356 | * for avoiding race in accounting. If true, |
| 1357 | * pc->mem_cgroup may be overwritten. |
| 1358 | * |
| 1359 | * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or |
| 1360 | * under hierarchy of moving cgroups. This is for |
| 1361 | * waiting at hith-memory prressure caused by "move". |
| 1362 | */ |
| 1363 | |
| 1364 | static bool mem_cgroup_stealed(struct mem_cgroup *mem) |
| 1365 | { |
| 1366 | VM_BUG_ON(!rcu_read_lock_held()); |
| 1367 | return this_cpu_read(mem->stat->count[MEM_CGROUP_ON_MOVE]) > 0; |
| 1368 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1369 | |
| 1370 | static bool mem_cgroup_under_move(struct mem_cgroup *mem) |
| 1371 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1372 | struct mem_cgroup *from; |
| 1373 | struct mem_cgroup *to; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1374 | bool ret = false; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1375 | /* |
| 1376 | * Unlike task_move routines, we access mc.to, mc.from not under |
| 1377 | * mutual exclusion by cgroup_mutex. Here, we take spinlock instead. |
| 1378 | */ |
| 1379 | spin_lock(&mc.lock); |
| 1380 | from = mc.from; |
| 1381 | to = mc.to; |
| 1382 | if (!from) |
| 1383 | goto unlock; |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame^] | 1384 | |
| 1385 | ret = mem_cgroup_same_or_subtree(mem, from) |
| 1386 | || mem_cgroup_same_or_subtree(mem, to); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1387 | unlock: |
| 1388 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1389 | return ret; |
| 1390 | } |
| 1391 | |
| 1392 | static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem) |
| 1393 | { |
| 1394 | if (mc.moving_task && current != mc.moving_task) { |
| 1395 | if (mem_cgroup_under_move(mem)) { |
| 1396 | DEFINE_WAIT(wait); |
| 1397 | prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE); |
| 1398 | /* moving charge context might have finished. */ |
| 1399 | if (mc.moving_task) |
| 1400 | schedule(); |
| 1401 | finish_wait(&mc.waitq, &wait); |
| 1402 | return true; |
| 1403 | } |
| 1404 | } |
| 1405 | return false; |
| 1406 | } |
| 1407 | |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1408 | /** |
Kirill A. Shutemov | 6a6135b | 2010-03-10 15:22:25 -0800 | [diff] [blame] | 1409 | * 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] | 1410 | * @memcg: The memory cgroup that went over limit |
| 1411 | * @p: Task that is going to be killed |
| 1412 | * |
| 1413 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is |
| 1414 | * enabled |
| 1415 | */ |
| 1416 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) |
| 1417 | { |
| 1418 | struct cgroup *task_cgrp; |
| 1419 | struct cgroup *mem_cgrp; |
| 1420 | /* |
| 1421 | * Need a buffer in BSS, can't rely on allocations. The code relies |
| 1422 | * on the assumption that OOM is serialized for memory controller. |
| 1423 | * If this assumption is broken, revisit this code. |
| 1424 | */ |
| 1425 | static char memcg_name[PATH_MAX]; |
| 1426 | int ret; |
| 1427 | |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1428 | if (!memcg || !p) |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1429 | return; |
| 1430 | |
| 1431 | |
| 1432 | rcu_read_lock(); |
| 1433 | |
| 1434 | mem_cgrp = memcg->css.cgroup; |
| 1435 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); |
| 1436 | |
| 1437 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); |
| 1438 | if (ret < 0) { |
| 1439 | /* |
| 1440 | * Unfortunately, we are unable to convert to a useful name |
| 1441 | * But we'll still print out the usage information |
| 1442 | */ |
| 1443 | rcu_read_unlock(); |
| 1444 | goto done; |
| 1445 | } |
| 1446 | rcu_read_unlock(); |
| 1447 | |
| 1448 | printk(KERN_INFO "Task in %s killed", memcg_name); |
| 1449 | |
| 1450 | rcu_read_lock(); |
| 1451 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); |
| 1452 | if (ret < 0) { |
| 1453 | rcu_read_unlock(); |
| 1454 | goto done; |
| 1455 | } |
| 1456 | rcu_read_unlock(); |
| 1457 | |
| 1458 | /* |
| 1459 | * Continues from above, so we don't need an KERN_ level |
| 1460 | */ |
| 1461 | printk(KERN_CONT " as a result of limit of %s\n", memcg_name); |
| 1462 | done: |
| 1463 | |
| 1464 | printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n", |
| 1465 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, |
| 1466 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, |
| 1467 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); |
| 1468 | printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, " |
| 1469 | "failcnt %llu\n", |
| 1470 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, |
| 1471 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, |
| 1472 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); |
| 1473 | } |
| 1474 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1475 | /* |
| 1476 | * This function returns the number of memcg under hierarchy tree. Returns |
| 1477 | * 1(self count) if no children. |
| 1478 | */ |
| 1479 | static int mem_cgroup_count_children(struct mem_cgroup *mem) |
| 1480 | { |
| 1481 | int num = 0; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1482 | struct mem_cgroup *iter; |
| 1483 | |
| 1484 | for_each_mem_cgroup_tree(iter, mem) |
| 1485 | num++; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1486 | return num; |
| 1487 | } |
| 1488 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1489 | /* |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1490 | * Return the memory (and swap, if configured) limit for a memcg. |
| 1491 | */ |
| 1492 | u64 mem_cgroup_get_limit(struct mem_cgroup *memcg) |
| 1493 | { |
| 1494 | u64 limit; |
| 1495 | u64 memsw; |
| 1496 | |
Johannes Weiner | f3e8eb7 | 2011-01-13 15:47:39 -0800 | [diff] [blame] | 1497 | limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 1498 | limit += total_swap_pages << PAGE_SHIFT; |
| 1499 | |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1500 | memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 1501 | /* |
| 1502 | * If memsw is finite and limits the amount of swap space available |
| 1503 | * to this memcg, return that limit. |
| 1504 | */ |
| 1505 | return min(limit, memsw); |
| 1506 | } |
| 1507 | |
| 1508 | /* |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1509 | * Visit the first child (need not be the first child as per the ordering |
| 1510 | * of the cgroup list, since we track last_scanned_child) of @mem and use |
| 1511 | * that to reclaim free pages from. |
| 1512 | */ |
| 1513 | static struct mem_cgroup * |
| 1514 | mem_cgroup_select_victim(struct mem_cgroup *root_mem) |
| 1515 | { |
| 1516 | struct mem_cgroup *ret = NULL; |
| 1517 | struct cgroup_subsys_state *css; |
| 1518 | int nextid, found; |
| 1519 | |
| 1520 | if (!root_mem->use_hierarchy) { |
| 1521 | css_get(&root_mem->css); |
| 1522 | ret = root_mem; |
| 1523 | } |
| 1524 | |
| 1525 | while (!ret) { |
| 1526 | rcu_read_lock(); |
| 1527 | nextid = root_mem->last_scanned_child + 1; |
| 1528 | css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css, |
| 1529 | &found); |
| 1530 | if (css && css_tryget(css)) |
| 1531 | ret = container_of(css, struct mem_cgroup, css); |
| 1532 | |
| 1533 | rcu_read_unlock(); |
| 1534 | /* Updates scanning parameter */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1535 | if (!css) { |
| 1536 | /* this means start scan from ID:1 */ |
| 1537 | root_mem->last_scanned_child = 0; |
| 1538 | } else |
| 1539 | root_mem->last_scanned_child = found; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | return ret; |
| 1543 | } |
| 1544 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1545 | /** |
| 1546 | * test_mem_cgroup_node_reclaimable |
| 1547 | * @mem: the target memcg |
| 1548 | * @nid: the node ID to be checked. |
| 1549 | * @noswap : specify true here if the user wants flle only information. |
| 1550 | * |
| 1551 | * This function returns whether the specified memcg contains any |
| 1552 | * reclaimable pages on a node. Returns true if there are any reclaimable |
| 1553 | * pages in the node. |
| 1554 | */ |
| 1555 | static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *mem, |
| 1556 | int nid, bool noswap) |
| 1557 | { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1558 | if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_FILE)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1559 | return true; |
| 1560 | if (noswap || !total_swap_pages) |
| 1561 | return false; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1562 | if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_ANON)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1563 | return true; |
| 1564 | return false; |
| 1565 | |
| 1566 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1567 | #if MAX_NUMNODES > 1 |
| 1568 | |
| 1569 | /* |
| 1570 | * Always updating the nodemask is not very good - even if we have an empty |
| 1571 | * list or the wrong list here, we can start from some node and traverse all |
| 1572 | * nodes based on the zonelist. So update the list loosely once per 10 secs. |
| 1573 | * |
| 1574 | */ |
| 1575 | static void mem_cgroup_may_update_nodemask(struct mem_cgroup *mem) |
| 1576 | { |
| 1577 | int nid; |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1578 | /* |
| 1579 | * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET |
| 1580 | * pagein/pageout changes since the last update. |
| 1581 | */ |
| 1582 | if (!atomic_read(&mem->numainfo_events)) |
| 1583 | return; |
| 1584 | if (atomic_inc_return(&mem->numainfo_updating) > 1) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1585 | return; |
| 1586 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1587 | /* make a nodemask where this memcg uses memory from */ |
| 1588 | mem->scan_nodes = node_states[N_HIGH_MEMORY]; |
| 1589 | |
| 1590 | for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) { |
| 1591 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1592 | if (!test_mem_cgroup_node_reclaimable(mem, nid, false)) |
| 1593 | node_clear(nid, mem->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1594 | } |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1595 | |
| 1596 | atomic_set(&mem->numainfo_events, 0); |
| 1597 | atomic_set(&mem->numainfo_updating, 0); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | /* |
| 1601 | * Selecting a node where we start reclaim from. Because what we need is just |
| 1602 | * reducing usage counter, start from anywhere is O,K. Considering |
| 1603 | * memory reclaim from current node, there are pros. and cons. |
| 1604 | * |
| 1605 | * Freeing memory from current node means freeing memory from a node which |
| 1606 | * we'll use or we've used. So, it may make LRU bad. And if several threads |
| 1607 | * hit limits, it will see a contention on a node. But freeing from remote |
| 1608 | * node means more costs for memory reclaim because of memory latency. |
| 1609 | * |
| 1610 | * Now, we use round-robin. Better algorithm is welcomed. |
| 1611 | */ |
| 1612 | int mem_cgroup_select_victim_node(struct mem_cgroup *mem) |
| 1613 | { |
| 1614 | int node; |
| 1615 | |
| 1616 | mem_cgroup_may_update_nodemask(mem); |
| 1617 | node = mem->last_scanned_node; |
| 1618 | |
| 1619 | node = next_node(node, mem->scan_nodes); |
| 1620 | if (node == MAX_NUMNODES) |
| 1621 | node = first_node(mem->scan_nodes); |
| 1622 | /* |
| 1623 | * We call this when we hit limit, not when pages are added to LRU. |
| 1624 | * No LRU may hold pages because all pages are UNEVICTABLE or |
| 1625 | * memcg is too small and all pages are not on LRU. In that case, |
| 1626 | * we use curret node. |
| 1627 | */ |
| 1628 | if (unlikely(node == MAX_NUMNODES)) |
| 1629 | node = numa_node_id(); |
| 1630 | |
| 1631 | mem->last_scanned_node = node; |
| 1632 | return node; |
| 1633 | } |
| 1634 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1635 | /* |
| 1636 | * Check all nodes whether it contains reclaimable pages or not. |
| 1637 | * For quick scan, we make use of scan_nodes. This will allow us to skip |
| 1638 | * unused nodes. But scan_nodes is lazily updated and may not cotain |
| 1639 | * enough new information. We need to do double check. |
| 1640 | */ |
| 1641 | bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap) |
| 1642 | { |
| 1643 | int nid; |
| 1644 | |
| 1645 | /* |
| 1646 | * quick check...making use of scan_node. |
| 1647 | * We can skip unused nodes. |
| 1648 | */ |
| 1649 | if (!nodes_empty(mem->scan_nodes)) { |
| 1650 | for (nid = first_node(mem->scan_nodes); |
| 1651 | nid < MAX_NUMNODES; |
| 1652 | nid = next_node(nid, mem->scan_nodes)) { |
| 1653 | |
| 1654 | if (test_mem_cgroup_node_reclaimable(mem, nid, noswap)) |
| 1655 | return true; |
| 1656 | } |
| 1657 | } |
| 1658 | /* |
| 1659 | * Check rest of nodes. |
| 1660 | */ |
| 1661 | for_each_node_state(nid, N_HIGH_MEMORY) { |
| 1662 | if (node_isset(nid, mem->scan_nodes)) |
| 1663 | continue; |
| 1664 | if (test_mem_cgroup_node_reclaimable(mem, nid, noswap)) |
| 1665 | return true; |
| 1666 | } |
| 1667 | return false; |
| 1668 | } |
| 1669 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1670 | #else |
| 1671 | int mem_cgroup_select_victim_node(struct mem_cgroup *mem) |
| 1672 | { |
| 1673 | return 0; |
| 1674 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1675 | |
| 1676 | bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap) |
| 1677 | { |
| 1678 | return test_mem_cgroup_node_reclaimable(mem, 0, noswap); |
| 1679 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1680 | #endif |
| 1681 | |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 1682 | static void __mem_cgroup_record_scanstat(unsigned long *stats, |
| 1683 | struct memcg_scanrecord *rec) |
| 1684 | { |
| 1685 | |
| 1686 | stats[SCAN] += rec->nr_scanned[0] + rec->nr_scanned[1]; |
| 1687 | stats[SCAN_ANON] += rec->nr_scanned[0]; |
| 1688 | stats[SCAN_FILE] += rec->nr_scanned[1]; |
| 1689 | |
| 1690 | stats[ROTATE] += rec->nr_rotated[0] + rec->nr_rotated[1]; |
| 1691 | stats[ROTATE_ANON] += rec->nr_rotated[0]; |
| 1692 | stats[ROTATE_FILE] += rec->nr_rotated[1]; |
| 1693 | |
| 1694 | stats[FREED] += rec->nr_freed[0] + rec->nr_freed[1]; |
| 1695 | stats[FREED_ANON] += rec->nr_freed[0]; |
| 1696 | stats[FREED_FILE] += rec->nr_freed[1]; |
| 1697 | |
| 1698 | stats[ELAPSED] += rec->elapsed; |
| 1699 | } |
| 1700 | |
| 1701 | static void mem_cgroup_record_scanstat(struct memcg_scanrecord *rec) |
| 1702 | { |
| 1703 | struct mem_cgroup *mem; |
| 1704 | int context = rec->context; |
| 1705 | |
| 1706 | if (context >= NR_SCAN_CONTEXT) |
| 1707 | return; |
| 1708 | |
| 1709 | mem = rec->mem; |
| 1710 | spin_lock(&mem->scanstat.lock); |
| 1711 | __mem_cgroup_record_scanstat(mem->scanstat.stats[context], rec); |
| 1712 | spin_unlock(&mem->scanstat.lock); |
| 1713 | |
| 1714 | mem = rec->root; |
| 1715 | spin_lock(&mem->scanstat.lock); |
| 1716 | __mem_cgroup_record_scanstat(mem->scanstat.rootstats[context], rec); |
| 1717 | spin_unlock(&mem->scanstat.lock); |
| 1718 | } |
| 1719 | |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1720 | /* |
| 1721 | * Scan the hierarchy if needed to reclaim memory. We remember the last child |
| 1722 | * we reclaimed from, so that we don't end up penalizing one child extensively |
| 1723 | * based on its position in the children list. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1724 | * |
| 1725 | * root_mem is the original ancestor that we've been reclaim from. |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1726 | * |
| 1727 | * We give up and return to the caller when we visit root_mem twice. |
| 1728 | * (other groups can be removed while we're walking....) |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1729 | * |
| 1730 | * If shrink==true, for avoiding to free too much, this returns immedieately. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1731 | */ |
| 1732 | static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem, |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1733 | struct zone *zone, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1734 | gfp_t gfp_mask, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1735 | unsigned long reclaim_options, |
| 1736 | unsigned long *total_scanned) |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1737 | { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1738 | struct mem_cgroup *victim; |
| 1739 | int ret, total = 0; |
| 1740 | int loop = 0; |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1741 | bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP; |
| 1742 | bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1743 | bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT; |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 1744 | struct memcg_scanrecord rec; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1745 | unsigned long excess; |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 1746 | unsigned long scanned; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1747 | |
| 1748 | excess = res_counter_soft_limit_excess(&root_mem->res) >> PAGE_SHIFT; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1749 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1750 | /* If memsw_is_minimum==1, swap-out is of-no-use. */ |
Daisuke Nishimura | 108b6a7 | 2011-07-26 16:08:25 -0700 | [diff] [blame] | 1751 | if (!check_soft && !shrink && root_mem->memsw_is_minimum) |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1752 | noswap = true; |
| 1753 | |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 1754 | if (shrink) |
| 1755 | rec.context = SCAN_BY_SHRINK; |
| 1756 | else if (check_soft) |
| 1757 | rec.context = SCAN_BY_SYSTEM; |
| 1758 | else |
| 1759 | rec.context = SCAN_BY_LIMIT; |
| 1760 | |
| 1761 | rec.root = root_mem; |
| 1762 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1763 | while (1) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1764 | victim = mem_cgroup_select_victim(root_mem); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1765 | if (victim == root_mem) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1766 | loop++; |
KAMEZAWA Hiroyuki | fbc29a2 | 2011-06-15 15:08:46 -0700 | [diff] [blame] | 1767 | /* |
| 1768 | * We are not draining per cpu cached charges during |
| 1769 | * soft limit reclaim because global reclaim doesn't |
| 1770 | * care about charges. It tries to free some memory and |
| 1771 | * charges will not give any. |
| 1772 | */ |
| 1773 | if (!check_soft && loop >= 1) |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 1774 | drain_all_stock_async(root_mem); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1775 | if (loop >= 2) { |
| 1776 | /* |
| 1777 | * If we have not been able to reclaim |
| 1778 | * anything, it might because there are |
| 1779 | * no reclaimable pages under this hierarchy |
| 1780 | */ |
| 1781 | if (!check_soft || !total) { |
| 1782 | css_put(&victim->css); |
| 1783 | break; |
| 1784 | } |
| 1785 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1786 | * We want to do more targeted reclaim. |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1787 | * excess >> 2 is not to excessive so as to |
| 1788 | * reclaim too much, nor too less that we keep |
| 1789 | * coming back to reclaim from this cgroup |
| 1790 | */ |
| 1791 | if (total >= (excess >> 2) || |
| 1792 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) { |
| 1793 | css_put(&victim->css); |
| 1794 | break; |
| 1795 | } |
| 1796 | } |
| 1797 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1798 | if (!mem_cgroup_reclaimable(victim, noswap)) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1799 | /* this cgroup's local usage == 0 */ |
| 1800 | css_put(&victim->css); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1801 | continue; |
| 1802 | } |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 1803 | rec.mem = victim; |
| 1804 | rec.nr_scanned[0] = 0; |
| 1805 | rec.nr_scanned[1] = 0; |
| 1806 | rec.nr_rotated[0] = 0; |
| 1807 | rec.nr_rotated[1] = 0; |
| 1808 | rec.nr_freed[0] = 0; |
| 1809 | rec.nr_freed[1] = 0; |
| 1810 | rec.elapsed = 0; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1811 | /* we use swappiness of local cgroup */ |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1812 | if (check_soft) { |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1813 | ret = mem_cgroup_shrink_node_zone(victim, gfp_mask, |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 1814 | noswap, zone, &rec, &scanned); |
| 1815 | *total_scanned += scanned; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1816 | } else |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1817 | ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 1818 | noswap, &rec); |
| 1819 | mem_cgroup_record_scanstat(&rec); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1820 | css_put(&victim->css); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1821 | /* |
| 1822 | * At shrinking usage, we can't check we should stop here or |
| 1823 | * reclaim more. It's depends on callers. last_scanned_child |
| 1824 | * will work enough for keeping fairness under tree. |
| 1825 | */ |
| 1826 | if (shrink) |
| 1827 | return ret; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1828 | total += ret; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1829 | if (check_soft) { |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1830 | if (!res_counter_soft_limit_excess(&root_mem->res)) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1831 | return total; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1832 | } else if (mem_cgroup_margin(root_mem)) |
Johannes Weiner | 4fd14eb | 2011-05-26 16:25:35 -0700 | [diff] [blame] | 1833 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1834 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1835 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1836 | } |
| 1837 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1838 | /* |
| 1839 | * Check OOM-Killer is already running under our hierarchy. |
| 1840 | * If someone is running, return false. |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1841 | * Has to be called with memcg_oom_lock |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1842 | */ |
| 1843 | static bool mem_cgroup_oom_lock(struct mem_cgroup *mem) |
| 1844 | { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1845 | int lock_count = -1; |
| 1846 | struct mem_cgroup *iter, *failed = NULL; |
| 1847 | bool cond = true; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1848 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1849 | for_each_mem_cgroup_tree_cond(iter, mem, cond) { |
| 1850 | bool locked = iter->oom_lock; |
| 1851 | |
| 1852 | iter->oom_lock = true; |
| 1853 | if (lock_count == -1) |
| 1854 | lock_count = iter->oom_lock; |
| 1855 | else if (lock_count != locked) { |
| 1856 | /* |
| 1857 | * this subtree of our hierarchy is already locked |
| 1858 | * so we cannot give a lock. |
| 1859 | */ |
| 1860 | lock_count = 0; |
| 1861 | failed = iter; |
| 1862 | cond = false; |
| 1863 | } |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1864 | } |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1865 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1866 | if (!failed) |
| 1867 | goto done; |
| 1868 | |
| 1869 | /* |
| 1870 | * OK, we failed to lock the whole subtree so we have to clean up |
| 1871 | * what we set up to the failing subtree |
| 1872 | */ |
| 1873 | cond = true; |
| 1874 | for_each_mem_cgroup_tree_cond(iter, mem, cond) { |
| 1875 | if (iter == failed) { |
| 1876 | cond = false; |
| 1877 | continue; |
| 1878 | } |
| 1879 | iter->oom_lock = false; |
| 1880 | } |
| 1881 | done: |
| 1882 | return lock_count; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1883 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1884 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1885 | /* |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1886 | * Has to be called with memcg_oom_lock |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1887 | */ |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1888 | static int mem_cgroup_oom_unlock(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1889 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1890 | struct mem_cgroup *iter; |
| 1891 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1892 | for_each_mem_cgroup_tree(iter, mem) |
| 1893 | iter->oom_lock = false; |
| 1894 | return 0; |
| 1895 | } |
| 1896 | |
| 1897 | static void mem_cgroup_mark_under_oom(struct mem_cgroup *mem) |
| 1898 | { |
| 1899 | struct mem_cgroup *iter; |
| 1900 | |
| 1901 | for_each_mem_cgroup_tree(iter, mem) |
| 1902 | atomic_inc(&iter->under_oom); |
| 1903 | } |
| 1904 | |
| 1905 | static void mem_cgroup_unmark_under_oom(struct mem_cgroup *mem) |
| 1906 | { |
| 1907 | struct mem_cgroup *iter; |
| 1908 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1909 | /* |
| 1910 | * When a new child is created while the hierarchy is under oom, |
| 1911 | * mem_cgroup_oom_lock() may not be called. We have to use |
| 1912 | * atomic_add_unless() here. |
| 1913 | */ |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1914 | for_each_mem_cgroup_tree(iter, mem) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1915 | atomic_add_unless(&iter->under_oom, -1, 0); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1918 | static DEFINE_SPINLOCK(memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1919 | static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq); |
| 1920 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1921 | struct oom_wait_info { |
| 1922 | struct mem_cgroup *mem; |
| 1923 | wait_queue_t wait; |
| 1924 | }; |
| 1925 | |
| 1926 | static int memcg_oom_wake_function(wait_queue_t *wait, |
| 1927 | unsigned mode, int sync, void *arg) |
| 1928 | { |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame^] | 1929 | struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg, |
| 1930 | *oom_wait_mem; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1931 | struct oom_wait_info *oom_wait_info; |
| 1932 | |
| 1933 | oom_wait_info = container_of(wait, struct oom_wait_info, wait); |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame^] | 1934 | oom_wait_mem = oom_wait_info->mem; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1935 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1936 | /* |
| 1937 | * Both of oom_wait_info->mem and wake_mem are stable under us. |
| 1938 | * Then we can use css_is_ancestor without taking care of RCU. |
| 1939 | */ |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame^] | 1940 | if (!mem_cgroup_same_or_subtree(oom_wait_mem, wake_mem) |
| 1941 | && !mem_cgroup_same_or_subtree(wake_mem, oom_wait_mem)) |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1942 | return 0; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1943 | return autoremove_wake_function(wait, mode, sync, arg); |
| 1944 | } |
| 1945 | |
| 1946 | static void memcg_wakeup_oom(struct mem_cgroup *mem) |
| 1947 | { |
| 1948 | /* for filtering, pass "mem" as argument. */ |
| 1949 | __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem); |
| 1950 | } |
| 1951 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1952 | static void memcg_oom_recover(struct mem_cgroup *mem) |
| 1953 | { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1954 | if (mem && atomic_read(&mem->under_oom)) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1955 | memcg_wakeup_oom(mem); |
| 1956 | } |
| 1957 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1958 | /* |
| 1959 | * try to call OOM killer. returns false if we should exit memory-reclaim loop. |
| 1960 | */ |
| 1961 | bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask) |
| 1962 | { |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1963 | struct oom_wait_info owait; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1964 | bool locked, need_to_kill; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1965 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1966 | owait.mem = mem; |
| 1967 | owait.wait.flags = 0; |
| 1968 | owait.wait.func = memcg_oom_wake_function; |
| 1969 | owait.wait.private = current; |
| 1970 | INIT_LIST_HEAD(&owait.wait.task_list); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1971 | need_to_kill = true; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1972 | mem_cgroup_mark_under_oom(mem); |
| 1973 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1974 | /* At first, try to OOM lock hierarchy under mem.*/ |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1975 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1976 | locked = mem_cgroup_oom_lock(mem); |
| 1977 | /* |
| 1978 | * Even if signal_pending(), we can't quit charge() loop without |
| 1979 | * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL |
| 1980 | * under OOM is always welcomed, use TASK_KILLABLE here. |
| 1981 | */ |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1982 | prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE); |
| 1983 | if (!locked || mem->oom_kill_disable) |
| 1984 | need_to_kill = false; |
| 1985 | if (locked) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1986 | mem_cgroup_oom_notify(mem); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1987 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1988 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1989 | if (need_to_kill) { |
| 1990 | finish_wait(&memcg_oom_waitq, &owait.wait); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1991 | mem_cgroup_out_of_memory(mem, mask); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1992 | } else { |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1993 | schedule(); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1994 | finish_wait(&memcg_oom_waitq, &owait.wait); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1995 | } |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1996 | spin_lock(&memcg_oom_lock); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1997 | if (locked) |
| 1998 | mem_cgroup_oom_unlock(mem); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1999 | memcg_wakeup_oom(mem); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 2000 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2001 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 2002 | mem_cgroup_unmark_under_oom(mem); |
| 2003 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2004 | if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current)) |
| 2005 | return false; |
| 2006 | /* Give chance to dying process */ |
| 2007 | schedule_timeout(1); |
| 2008 | return true; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 2009 | } |
| 2010 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2011 | /* |
| 2012 | * Currently used to update mapped file statistics, but the routine can be |
| 2013 | * generalized to update other statistics as well. |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2014 | * |
| 2015 | * Notes: Race condition |
| 2016 | * |
| 2017 | * We usually use page_cgroup_lock() for accessing page_cgroup member but |
| 2018 | * it tends to be costly. But considering some conditions, we doesn't need |
| 2019 | * to do so _always_. |
| 2020 | * |
| 2021 | * Considering "charge", lock_page_cgroup() is not required because all |
| 2022 | * file-stat operations happen after a page is attached to radix-tree. There |
| 2023 | * are no race with "charge". |
| 2024 | * |
| 2025 | * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup |
| 2026 | * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even |
| 2027 | * if there are race with "uncharge". Statistics itself is properly handled |
| 2028 | * by flags. |
| 2029 | * |
| 2030 | * Considering "move", this is an only case we see a race. To make the race |
| 2031 | * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are |
| 2032 | * possibility of race condition. If there is, we take a lock. |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2033 | */ |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2034 | |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2035 | void mem_cgroup_update_page_stat(struct page *page, |
| 2036 | enum mem_cgroup_page_stat_item idx, int val) |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2037 | { |
| 2038 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2039 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 2040 | bool need_unlock = false; |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 2041 | unsigned long uninitialized_var(flags); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2042 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2043 | if (unlikely(!pc)) |
| 2044 | return; |
| 2045 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2046 | rcu_read_lock(); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2047 | mem = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2048 | if (unlikely(!mem || !PageCgroupUsed(pc))) |
| 2049 | goto out; |
| 2050 | /* pc->mem_cgroup is unstable ? */ |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2051 | if (unlikely(mem_cgroup_stealed(mem)) || PageTransHuge(page)) { |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2052 | /* take a lock against to access pc->mem_cgroup */ |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 2053 | move_lock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2054 | need_unlock = true; |
| 2055 | mem = pc->mem_cgroup; |
| 2056 | if (!mem || !PageCgroupUsed(pc)) |
| 2057 | goto out; |
| 2058 | } |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2059 | |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2060 | switch (idx) { |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2061 | case MEMCG_NR_FILE_MAPPED: |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2062 | if (val > 0) |
| 2063 | SetPageCgroupFileMapped(pc); |
| 2064 | else if (!page_mapped(page)) |
KAMEZAWA Hiroyuki | 0c270f8 | 2010-10-27 15:33:39 -0700 | [diff] [blame] | 2065 | ClearPageCgroupFileMapped(pc); |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2066 | idx = MEM_CGROUP_STAT_FILE_MAPPED; |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2067 | break; |
| 2068 | default: |
| 2069 | BUG(); |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 2070 | } |
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 | this_cpu_add(mem->stat->count[idx], val); |
| 2073 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2074 | out: |
| 2075 | if (unlikely(need_unlock)) |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 2076 | move_unlock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2077 | rcu_read_unlock(); |
| 2078 | return; |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2079 | } |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2080 | EXPORT_SYMBOL(mem_cgroup_update_page_stat); |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2081 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2082 | /* |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2083 | * size of first charge trial. "32" comes from vmscan.c's magic value. |
| 2084 | * TODO: maybe necessary to use big numbers in big irons. |
| 2085 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2086 | #define CHARGE_BATCH 32U |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2087 | struct memcg_stock_pcp { |
| 2088 | struct mem_cgroup *cached; /* this never be root cgroup */ |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2089 | unsigned int nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2090 | struct work_struct work; |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2091 | unsigned long flags; |
| 2092 | #define FLUSHING_CACHED_CHARGE (0) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2093 | }; |
| 2094 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2095 | static DEFINE_MUTEX(percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2096 | |
| 2097 | /* |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2098 | * 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] | 2099 | * from local stock and true is returned. If the stock is 0 or charges from a |
| 2100 | * cgroup which is not current target, returns false. This stock will be |
| 2101 | * refilled. |
| 2102 | */ |
| 2103 | static bool consume_stock(struct mem_cgroup *mem) |
| 2104 | { |
| 2105 | struct memcg_stock_pcp *stock; |
| 2106 | bool ret = true; |
| 2107 | |
| 2108 | stock = &get_cpu_var(memcg_stock); |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2109 | if (mem == stock->cached && stock->nr_pages) |
| 2110 | stock->nr_pages--; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2111 | else /* need to call res_counter_charge */ |
| 2112 | ret = false; |
| 2113 | put_cpu_var(memcg_stock); |
| 2114 | return ret; |
| 2115 | } |
| 2116 | |
| 2117 | /* |
| 2118 | * Returns stocks cached in percpu to res_counter and reset cached information. |
| 2119 | */ |
| 2120 | static void drain_stock(struct memcg_stock_pcp *stock) |
| 2121 | { |
| 2122 | struct mem_cgroup *old = stock->cached; |
| 2123 | |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2124 | if (stock->nr_pages) { |
| 2125 | unsigned long bytes = stock->nr_pages * PAGE_SIZE; |
| 2126 | |
| 2127 | res_counter_uncharge(&old->res, bytes); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2128 | if (do_swap_account) |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2129 | res_counter_uncharge(&old->memsw, bytes); |
| 2130 | stock->nr_pages = 0; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2131 | } |
| 2132 | stock->cached = NULL; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | /* |
| 2136 | * This must be called under preempt disabled or must be called by |
| 2137 | * a thread which is pinned to local cpu. |
| 2138 | */ |
| 2139 | static void drain_local_stock(struct work_struct *dummy) |
| 2140 | { |
| 2141 | struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock); |
| 2142 | drain_stock(stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2143 | clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | /* |
| 2147 | * 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] | 2148 | * This will be consumed by consume_stock() function, later. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2149 | */ |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2150 | static void refill_stock(struct mem_cgroup *mem, unsigned int nr_pages) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2151 | { |
| 2152 | struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock); |
| 2153 | |
| 2154 | if (stock->cached != mem) { /* reset if necessary */ |
| 2155 | drain_stock(stock); |
| 2156 | stock->cached = mem; |
| 2157 | } |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2158 | stock->nr_pages += nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2159 | put_cpu_var(memcg_stock); |
| 2160 | } |
| 2161 | |
| 2162 | /* |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2163 | * Drains all per-CPU charge caches for given root_mem resp. subtree |
| 2164 | * of the hierarchy under it. sync flag says whether we should block |
| 2165 | * until the work is done. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2166 | */ |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2167 | static void drain_all_stock(struct mem_cgroup *root_mem, bool sync) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2168 | { |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2169 | int cpu, curcpu; |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2170 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2171 | /* Notify other cpus that system-wide "drain" is running */ |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2172 | get_online_cpus(); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2173 | /* |
| 2174 | * Get a hint for avoiding draining charges on the current cpu, |
| 2175 | * which must be exhausted by our charging. It is not required that |
| 2176 | * this be a precise check, so we use raw_smp_processor_id() instead of |
| 2177 | * getcpu()/putcpu(). |
| 2178 | */ |
| 2179 | curcpu = raw_smp_processor_id(); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2180 | for_each_online_cpu(cpu) { |
| 2181 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2182 | struct mem_cgroup *mem; |
| 2183 | |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2184 | mem = stock->cached; |
Michal Hocko | d1a05b6 | 2011-07-26 16:08:27 -0700 | [diff] [blame] | 2185 | if (!mem || !stock->nr_pages) |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2186 | continue; |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame^] | 2187 | if (!mem_cgroup_same_or_subtree(root_mem, mem)) |
| 2188 | continue; |
Michal Hocko | d1a05b6 | 2011-07-26 16:08:27 -0700 | [diff] [blame] | 2189 | if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) { |
| 2190 | if (cpu == curcpu) |
| 2191 | drain_local_stock(&stock->work); |
| 2192 | else |
| 2193 | schedule_work_on(cpu, &stock->work); |
| 2194 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2195 | } |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2196 | |
| 2197 | if (!sync) |
| 2198 | goto out; |
| 2199 | |
| 2200 | for_each_online_cpu(cpu) { |
| 2201 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
| 2202 | if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) |
| 2203 | flush_work(&stock->work); |
| 2204 | } |
| 2205 | out: |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2206 | put_online_cpus(); |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | /* |
| 2210 | * Tries to drain stocked charges in other cpus. This function is asynchronous |
| 2211 | * and just put a work per cpu for draining localy on each cpu. Caller can |
| 2212 | * expects some charges will be back to res_counter later but cannot wait for |
| 2213 | * it. |
| 2214 | */ |
| 2215 | static void drain_all_stock_async(struct mem_cgroup *root_mem) |
| 2216 | { |
| 2217 | /* |
| 2218 | * If someone calls draining, avoid adding more kworker runs. |
| 2219 | */ |
| 2220 | if (!mutex_trylock(&percpu_charge_mutex)) |
| 2221 | return; |
| 2222 | drain_all_stock(root_mem, false); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2223 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | /* This is a synchronous drain interface. */ |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2227 | static void drain_all_stock_sync(struct mem_cgroup *root_mem) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2228 | { |
| 2229 | /* called when force_empty is called */ |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2230 | mutex_lock(&percpu_charge_mutex); |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2231 | drain_all_stock(root_mem, true); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2232 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2233 | } |
| 2234 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2235 | /* |
| 2236 | * This function drains percpu counter value from DEAD cpu and |
| 2237 | * move it to local cpu. Note that this function can be preempted. |
| 2238 | */ |
| 2239 | static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *mem, int cpu) |
| 2240 | { |
| 2241 | int i; |
| 2242 | |
| 2243 | spin_lock(&mem->pcp_counter_lock); |
| 2244 | for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 2245 | long x = per_cpu(mem->stat->count[i], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2246 | |
| 2247 | per_cpu(mem->stat->count[i], cpu) = 0; |
| 2248 | mem->nocpu_base.count[i] += x; |
| 2249 | } |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2250 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) { |
| 2251 | unsigned long x = per_cpu(mem->stat->events[i], cpu); |
| 2252 | |
| 2253 | per_cpu(mem->stat->events[i], cpu) = 0; |
| 2254 | mem->nocpu_base.events[i] += x; |
| 2255 | } |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2256 | /* need to clear ON_MOVE value, works as a kind of lock. */ |
| 2257 | per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0; |
| 2258 | spin_unlock(&mem->pcp_counter_lock); |
| 2259 | } |
| 2260 | |
| 2261 | static void synchronize_mem_cgroup_on_move(struct mem_cgroup *mem, int cpu) |
| 2262 | { |
| 2263 | int idx = MEM_CGROUP_ON_MOVE; |
| 2264 | |
| 2265 | spin_lock(&mem->pcp_counter_lock); |
| 2266 | per_cpu(mem->stat->count[idx], cpu) = mem->nocpu_base.count[idx]; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2267 | spin_unlock(&mem->pcp_counter_lock); |
| 2268 | } |
| 2269 | |
| 2270 | static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb, |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2271 | unsigned long action, |
| 2272 | void *hcpu) |
| 2273 | { |
| 2274 | int cpu = (unsigned long)hcpu; |
| 2275 | struct memcg_stock_pcp *stock; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2276 | struct mem_cgroup *iter; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2277 | |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2278 | if ((action == CPU_ONLINE)) { |
| 2279 | for_each_mem_cgroup_all(iter) |
| 2280 | synchronize_mem_cgroup_on_move(iter, cpu); |
| 2281 | return NOTIFY_OK; |
| 2282 | } |
| 2283 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2284 | if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2285 | return NOTIFY_OK; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2286 | |
| 2287 | for_each_mem_cgroup_all(iter) |
| 2288 | mem_cgroup_drain_pcp_counter(iter, cpu); |
| 2289 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2290 | stock = &per_cpu(memcg_stock, cpu); |
| 2291 | drain_stock(stock); |
| 2292 | return NOTIFY_OK; |
| 2293 | } |
| 2294 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2295 | |
| 2296 | /* See __mem_cgroup_try_charge() for details */ |
| 2297 | enum { |
| 2298 | CHARGE_OK, /* success */ |
| 2299 | CHARGE_RETRY, /* need to retry but retry is not bad */ |
| 2300 | CHARGE_NOMEM, /* we can't do more. return -ENOMEM */ |
| 2301 | CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */ |
| 2302 | CHARGE_OOM_DIE, /* the current is killed because of OOM */ |
| 2303 | }; |
| 2304 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2305 | static int mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask, |
| 2306 | unsigned int nr_pages, bool oom_check) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2307 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2308 | unsigned long csize = nr_pages * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2309 | struct mem_cgroup *mem_over_limit; |
| 2310 | struct res_counter *fail_res; |
| 2311 | unsigned long flags = 0; |
| 2312 | int ret; |
| 2313 | |
| 2314 | ret = res_counter_charge(&mem->res, csize, &fail_res); |
| 2315 | |
| 2316 | if (likely(!ret)) { |
| 2317 | if (!do_swap_account) |
| 2318 | return CHARGE_OK; |
| 2319 | ret = res_counter_charge(&mem->memsw, csize, &fail_res); |
| 2320 | if (likely(!ret)) |
| 2321 | return CHARGE_OK; |
| 2322 | |
KAMEZAWA Hiroyuki | 01c88e2 | 2011-01-25 15:07:27 -0800 | [diff] [blame] | 2323 | res_counter_uncharge(&mem->res, csize); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2324 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw); |
| 2325 | flags |= MEM_CGROUP_RECLAIM_NOSWAP; |
| 2326 | } else |
| 2327 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, res); |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2328 | /* |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2329 | * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch |
| 2330 | * of regular pages (CHARGE_BATCH), or a single regular page (1). |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2331 | * |
| 2332 | * Never reclaim on behalf of optional batching, retry with a |
| 2333 | * single page instead. |
| 2334 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2335 | if (nr_pages == CHARGE_BATCH) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2336 | return CHARGE_RETRY; |
| 2337 | |
| 2338 | if (!(gfp_mask & __GFP_WAIT)) |
| 2339 | return CHARGE_WOULDBLOCK; |
| 2340 | |
| 2341 | ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 2342 | gfp_mask, flags, NULL); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2343 | if (mem_cgroup_margin(mem_over_limit) >= nr_pages) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2344 | return CHARGE_RETRY; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2345 | /* |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2346 | * Even though the limit is exceeded at this point, reclaim |
| 2347 | * may have been able to free some pages. Retry the charge |
| 2348 | * before killing the task. |
| 2349 | * |
| 2350 | * Only for regular pages, though: huge pages are rather |
| 2351 | * unlikely to succeed so close to the limit, and we fall back |
| 2352 | * to regular pages anyway in case of failure. |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2353 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2354 | if (nr_pages == 1 && ret) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2355 | return CHARGE_RETRY; |
| 2356 | |
| 2357 | /* |
| 2358 | * At task move, charge accounts can be doubly counted. So, it's |
| 2359 | * better to wait until the end of task_move if something is going on. |
| 2360 | */ |
| 2361 | if (mem_cgroup_wait_acct_move(mem_over_limit)) |
| 2362 | return CHARGE_RETRY; |
| 2363 | |
| 2364 | /* If we don't need to call oom-killer at el, return immediately */ |
| 2365 | if (!oom_check) |
| 2366 | return CHARGE_NOMEM; |
| 2367 | /* check OOM */ |
| 2368 | if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask)) |
| 2369 | return CHARGE_OOM_DIE; |
| 2370 | |
| 2371 | return CHARGE_RETRY; |
| 2372 | } |
| 2373 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2374 | /* |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2375 | * Unlike exported interface, "oom" parameter is added. if oom==true, |
| 2376 | * oom-killer can be invoked. |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2377 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2378 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2379 | gfp_t gfp_mask, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2380 | unsigned int nr_pages, |
| 2381 | struct mem_cgroup **memcg, |
| 2382 | bool oom) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2383 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2384 | unsigned int batch = max(CHARGE_BATCH, nr_pages); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2385 | int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
| 2386 | struct mem_cgroup *mem = NULL; |
| 2387 | int ret; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2388 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2389 | /* |
| 2390 | * Unlike gloval-vm's OOM-kill, we're not in memory shortage |
| 2391 | * in system level. So, allow to go ahead dying process in addition to |
| 2392 | * MEMDIE process. |
| 2393 | */ |
| 2394 | if (unlikely(test_thread_flag(TIF_MEMDIE) |
| 2395 | || fatal_signal_pending(current))) |
| 2396 | goto bypass; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2397 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2398 | /* |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2399 | * We always charge the cgroup the mm_struct belongs to. |
| 2400 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2401 | * thread group leader migrates. It's possible that mm is not |
| 2402 | * set, if so charge the init_mm (happens for pagecache usage). |
| 2403 | */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2404 | if (!*memcg && !mm) |
| 2405 | goto bypass; |
| 2406 | again: |
| 2407 | if (*memcg) { /* css should be a valid one */ |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2408 | mem = *memcg; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2409 | VM_BUG_ON(css_is_removed(&mem->css)); |
| 2410 | if (mem_cgroup_is_root(mem)) |
| 2411 | goto done; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2412 | if (nr_pages == 1 && consume_stock(mem)) |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2413 | goto done; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2414 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2415 | } else { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2416 | struct task_struct *p; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2417 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2418 | rcu_read_lock(); |
| 2419 | p = rcu_dereference(mm->owner); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2420 | /* |
KAMEZAWA Hiroyuki | ebb76ce | 2010-12-29 14:07:11 -0800 | [diff] [blame] | 2421 | * Because we don't have task_lock(), "p" can exit. |
| 2422 | * In that case, "mem" can point to root or p can be NULL with |
| 2423 | * race with swapoff. Then, we have small risk of mis-accouning. |
| 2424 | * But such kind of mis-account by race always happens because |
| 2425 | * we don't have cgroup_mutex(). It's overkill and we allo that |
| 2426 | * small race, here. |
| 2427 | * (*) swapoff at el will charge against mm-struct not against |
| 2428 | * task-struct. So, mm->owner can be NULL. |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2429 | */ |
| 2430 | mem = mem_cgroup_from_task(p); |
KAMEZAWA Hiroyuki | ebb76ce | 2010-12-29 14:07:11 -0800 | [diff] [blame] | 2431 | if (!mem || mem_cgroup_is_root(mem)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2432 | rcu_read_unlock(); |
| 2433 | goto done; |
| 2434 | } |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2435 | if (nr_pages == 1 && consume_stock(mem)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2436 | /* |
| 2437 | * It seems dagerous to access memcg without css_get(). |
| 2438 | * But considering how consume_stok works, it's not |
| 2439 | * necessary. If consume_stock success, some charges |
| 2440 | * from this memcg are cached on this cpu. So, we |
| 2441 | * don't need to call css_get()/css_tryget() before |
| 2442 | * calling consume_stock(). |
| 2443 | */ |
| 2444 | rcu_read_unlock(); |
| 2445 | goto done; |
| 2446 | } |
| 2447 | /* after here, we may be blocked. we need to get refcnt */ |
| 2448 | if (!css_tryget(&mem->css)) { |
| 2449 | rcu_read_unlock(); |
| 2450 | goto again; |
| 2451 | } |
| 2452 | rcu_read_unlock(); |
| 2453 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2454 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2455 | do { |
| 2456 | bool oom_check; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2457 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2458 | /* If killed, bypass charge */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2459 | if (fatal_signal_pending(current)) { |
| 2460 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2461 | goto bypass; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2462 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2463 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2464 | oom_check = false; |
| 2465 | if (oom && !nr_oom_retries) { |
| 2466 | oom_check = true; |
| 2467 | nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
| 2468 | } |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 2469 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2470 | ret = mem_cgroup_do_charge(mem, gfp_mask, batch, oom_check); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2471 | switch (ret) { |
| 2472 | case CHARGE_OK: |
| 2473 | break; |
| 2474 | case CHARGE_RETRY: /* not in OOM situation but retry */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2475 | batch = nr_pages; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2476 | css_put(&mem->css); |
| 2477 | mem = NULL; |
| 2478 | goto again; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2479 | case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2480 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2481 | goto nomem; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2482 | case CHARGE_NOMEM: /* OOM routine works */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2483 | if (!oom) { |
| 2484 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2485 | goto nomem; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2486 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2487 | /* If oom, we never return -ENOMEM */ |
| 2488 | nr_oom_retries--; |
| 2489 | break; |
| 2490 | case CHARGE_OOM_DIE: /* Killed by OOM Killer */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2491 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2492 | goto bypass; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2493 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2494 | } while (ret != CHARGE_OK); |
| 2495 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2496 | if (batch > nr_pages) |
| 2497 | refill_stock(mem, batch - nr_pages); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2498 | css_put(&mem->css); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2499 | done: |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2500 | *memcg = mem; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2501 | return 0; |
| 2502 | nomem: |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2503 | *memcg = NULL; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2504 | return -ENOMEM; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2505 | bypass: |
| 2506 | *memcg = NULL; |
| 2507 | return 0; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2508 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2509 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2510 | /* |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2511 | * Somemtimes we have to undo a charge we got by try_charge(). |
| 2512 | * This function is for that and do uncharge, put css's refcnt. |
| 2513 | * gotten by try_charge(). |
| 2514 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2515 | static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem, |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2516 | unsigned int nr_pages) |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2517 | { |
| 2518 | if (!mem_cgroup_is_root(mem)) { |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2519 | unsigned long bytes = nr_pages * PAGE_SIZE; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2520 | |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2521 | res_counter_uncharge(&mem->res, bytes); |
| 2522 | if (do_swap_account) |
| 2523 | res_counter_uncharge(&mem->memsw, bytes); |
| 2524 | } |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | /* |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2528 | * A helper function to get mem_cgroup from ID. must be called under |
| 2529 | * rcu_read_lock(). The caller must check css_is_removed() or some if |
| 2530 | * it's concern. (dropping refcnt from swap can be called against removed |
| 2531 | * memcg.) |
| 2532 | */ |
| 2533 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) |
| 2534 | { |
| 2535 | struct cgroup_subsys_state *css; |
| 2536 | |
| 2537 | /* ID 0 is unused ID */ |
| 2538 | if (!id) |
| 2539 | return NULL; |
| 2540 | css = css_lookup(&mem_cgroup_subsys, id); |
| 2541 | if (!css) |
| 2542 | return NULL; |
| 2543 | return container_of(css, struct mem_cgroup, css); |
| 2544 | } |
| 2545 | |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2546 | struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2547 | { |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2548 | struct mem_cgroup *mem = NULL; |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2549 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2550 | unsigned short id; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2551 | swp_entry_t ent; |
| 2552 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2553 | VM_BUG_ON(!PageLocked(page)); |
| 2554 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2555 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | c0bd3f63 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2556 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2557 | if (PageCgroupUsed(pc)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2558 | mem = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2559 | if (mem && !css_tryget(&mem->css)) |
| 2560 | mem = NULL; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2561 | } else if (PageSwapCache(page)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2562 | ent.val = page_private(page); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2563 | id = lookup_swap_cgroup(ent); |
| 2564 | rcu_read_lock(); |
| 2565 | mem = mem_cgroup_lookup(id); |
| 2566 | if (mem && !css_tryget(&mem->css)) |
| 2567 | mem = NULL; |
| 2568 | rcu_read_unlock(); |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2569 | } |
Daisuke Nishimura | c0bd3f63 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2570 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2571 | return mem; |
| 2572 | } |
| 2573 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2574 | static void __mem_cgroup_commit_charge(struct mem_cgroup *mem, |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2575 | struct page *page, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2576 | unsigned int nr_pages, |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2577 | struct page_cgroup *pc, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2578 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2579 | { |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2580 | lock_page_cgroup(pc); |
| 2581 | if (unlikely(PageCgroupUsed(pc))) { |
| 2582 | unlock_page_cgroup(pc); |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2583 | __mem_cgroup_cancel_charge(mem, nr_pages); |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2584 | return; |
| 2585 | } |
| 2586 | /* |
| 2587 | * we don't need page_cgroup_lock about tail pages, becase they are not |
| 2588 | * accessed by any other context at this point. |
| 2589 | */ |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2590 | pc->mem_cgroup = mem; |
KAMEZAWA Hiroyuki | 261fb61 | 2009-09-23 15:56:33 -0700 | [diff] [blame] | 2591 | /* |
| 2592 | * We access a page_cgroup asynchronously without lock_page_cgroup(). |
| 2593 | * Especially when a page_cgroup is taken from a page, pc->mem_cgroup |
| 2594 | * is accessed after testing USED bit. To make pc->mem_cgroup visible |
| 2595 | * before USED bit, we need memory barrier here. |
| 2596 | * See mem_cgroup_add_lru_list(), etc. |
| 2597 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2598 | smp_wmb(); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 2599 | switch (ctype) { |
| 2600 | case MEM_CGROUP_CHARGE_TYPE_CACHE: |
| 2601 | case MEM_CGROUP_CHARGE_TYPE_SHMEM: |
| 2602 | SetPageCgroupCache(pc); |
| 2603 | SetPageCgroupUsed(pc); |
| 2604 | break; |
| 2605 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
| 2606 | ClearPageCgroupCache(pc); |
| 2607 | SetPageCgroupUsed(pc); |
| 2608 | break; |
| 2609 | default: |
| 2610 | break; |
| 2611 | } |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2612 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2613 | mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), nr_pages); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2614 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 2615 | /* |
| 2616 | * "charge_statistics" updated event counter. Then, check it. |
| 2617 | * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree. |
| 2618 | * if they exceeds softlimit. |
| 2619 | */ |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2620 | memcg_check_events(mem, page); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2621 | } |
| 2622 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2623 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 2624 | |
| 2625 | #define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\ |
| 2626 | (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION)) |
| 2627 | /* |
| 2628 | * Because tail pages are not marked as "used", set it. We're under |
| 2629 | * zone->lru_lock, 'splitting on pmd' and compund_lock. |
| 2630 | */ |
| 2631 | void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail) |
| 2632 | { |
| 2633 | struct page_cgroup *head_pc = lookup_page_cgroup(head); |
| 2634 | struct page_cgroup *tail_pc = lookup_page_cgroup(tail); |
| 2635 | unsigned long flags; |
| 2636 | |
KAMEZAWA Hiroyuki | 3d37c4a | 2011-01-25 15:07:28 -0800 | [diff] [blame] | 2637 | if (mem_cgroup_disabled()) |
| 2638 | return; |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2639 | /* |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2640 | * We have no races with charge/uncharge but will have races with |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2641 | * page state accounting. |
| 2642 | */ |
| 2643 | move_lock_page_cgroup(head_pc, &flags); |
| 2644 | |
| 2645 | tail_pc->mem_cgroup = head_pc->mem_cgroup; |
| 2646 | smp_wmb(); /* see __commit_charge() */ |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2647 | if (PageCgroupAcctLRU(head_pc)) { |
| 2648 | enum lru_list lru; |
| 2649 | struct mem_cgroup_per_zone *mz; |
| 2650 | |
| 2651 | /* |
| 2652 | * LRU flags cannot be copied because we need to add tail |
| 2653 | *.page to LRU by generic call and our hook will be called. |
| 2654 | * We hold lru_lock, then, reduce counter directly. |
| 2655 | */ |
| 2656 | lru = page_lru(head); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 2657 | mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2658 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
| 2659 | } |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2660 | tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT; |
| 2661 | move_unlock_page_cgroup(head_pc, &flags); |
| 2662 | } |
| 2663 | #endif |
| 2664 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2665 | /** |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2666 | * mem_cgroup_move_account - move account of the page |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2667 | * @page: the page |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2668 | * @nr_pages: number of regular pages (>1 for huge pages) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2669 | * @pc: page_cgroup of the page. |
| 2670 | * @from: mem_cgroup which the page is moved from. |
| 2671 | * @to: mem_cgroup which the page is moved to. @from != @to. |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2672 | * @uncharge: whether we should call uncharge and css_put against @from. |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2673 | * |
| 2674 | * The caller must confirm following. |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2675 | * - page is not on LRU (isolate_page() is useful.) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2676 | * - compound_lock is held when nr_pages > 1 |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2677 | * |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2678 | * 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] | 2679 | * 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] | 2680 | * true, this function does "uncharge" from old cgroup, but it doesn't if |
| 2681 | * @uncharge is false, so a caller should do "uncharge". |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2682 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2683 | static int mem_cgroup_move_account(struct page *page, |
| 2684 | unsigned int nr_pages, |
| 2685 | struct page_cgroup *pc, |
| 2686 | struct mem_cgroup *from, |
| 2687 | struct mem_cgroup *to, |
| 2688 | bool uncharge) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2689 | { |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2690 | unsigned long flags; |
| 2691 | int ret; |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2692 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2693 | VM_BUG_ON(from == to); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2694 | VM_BUG_ON(PageLRU(page)); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2695 | /* |
| 2696 | * The page is isolated from LRU. So, collapse function |
| 2697 | * will not handle this page. But page splitting can happen. |
| 2698 | * Do this check under compound_page_lock(). The caller should |
| 2699 | * hold it. |
| 2700 | */ |
| 2701 | ret = -EBUSY; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2702 | if (nr_pages > 1 && !PageTransHuge(page)) |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2703 | goto out; |
| 2704 | |
| 2705 | lock_page_cgroup(pc); |
| 2706 | |
| 2707 | ret = -EINVAL; |
| 2708 | if (!PageCgroupUsed(pc) || pc->mem_cgroup != from) |
| 2709 | goto unlock; |
| 2710 | |
| 2711 | move_lock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2712 | |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 2713 | if (PageCgroupFileMapped(pc)) { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 2714 | /* Update mapped_file data for mem_cgroup */ |
| 2715 | preempt_disable(); |
| 2716 | __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 2717 | __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 2718 | preempt_enable(); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2719 | } |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2720 | mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2721 | if (uncharge) |
| 2722 | /* This is not "cancel", but cancel_charge does all we need. */ |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2723 | __mem_cgroup_cancel_charge(from, nr_pages); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2724 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2725 | /* caller should have done css_get */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2726 | pc->mem_cgroup = to; |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2727 | mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2728 | /* |
| 2729 | * We charges against "to" which may not have any tasks. Then, "to" |
| 2730 | * can be under rmdir(). But in current implementation, caller of |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 2731 | * 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] | 2732 | * guaranteed that "to" is never removed. So, we don't check rmdir |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 2733 | * status here. |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2734 | */ |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2735 | move_unlock_page_cgroup(pc, &flags); |
| 2736 | ret = 0; |
| 2737 | unlock: |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2738 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 2739 | /* |
| 2740 | * check events |
| 2741 | */ |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2742 | memcg_check_events(to, page); |
| 2743 | memcg_check_events(from, page); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2744 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2745 | return ret; |
| 2746 | } |
| 2747 | |
| 2748 | /* |
| 2749 | * move charges to its parent. |
| 2750 | */ |
| 2751 | |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2752 | static int mem_cgroup_move_parent(struct page *page, |
| 2753 | struct page_cgroup *pc, |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2754 | struct mem_cgroup *child, |
| 2755 | gfp_t gfp_mask) |
| 2756 | { |
| 2757 | struct cgroup *cg = child->css.cgroup; |
| 2758 | struct cgroup *pcg = cg->parent; |
| 2759 | struct mem_cgroup *parent; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2760 | unsigned int nr_pages; |
Andrew Morton | 4be4489 | 2011-03-23 16:42:39 -0700 | [diff] [blame] | 2761 | unsigned long uninitialized_var(flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2762 | int ret; |
| 2763 | |
| 2764 | /* Is ROOT ? */ |
| 2765 | if (!pcg) |
| 2766 | return -EINVAL; |
| 2767 | |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2768 | ret = -EBUSY; |
| 2769 | if (!get_page_unless_zero(page)) |
| 2770 | goto out; |
| 2771 | if (isolate_lru_page(page)) |
| 2772 | goto put; |
KAMEZAWA Hiroyuki | 52dbb90 | 2011-01-25 15:07:29 -0800 | [diff] [blame] | 2773 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2774 | nr_pages = hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2775 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2776 | parent = mem_cgroup_from_cont(pcg); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2777 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2778 | if (ret || !parent) |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2779 | goto put_back; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2780 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2781 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2782 | flags = compound_lock_irqsave(page); |
| 2783 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2784 | ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2785 | if (ret) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2786 | __mem_cgroup_cancel_charge(parent, nr_pages); |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 2787 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2788 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2789 | compound_unlock_irqrestore(page, flags); |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 2790 | put_back: |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2791 | putback_lru_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2792 | put: |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 2793 | put_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2794 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2795 | return ret; |
| 2796 | } |
| 2797 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2798 | /* |
| 2799 | * Charge the memory controller for page usage. |
| 2800 | * Return |
| 2801 | * 0 if the charge was successful |
| 2802 | * < 0 if the cgroup is over its limit |
| 2803 | */ |
| 2804 | 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] | 2805 | gfp_t gfp_mask, enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2806 | { |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2807 | struct mem_cgroup *mem = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2808 | unsigned int nr_pages = 1; |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 2809 | struct page_cgroup *pc; |
| 2810 | bool oom = true; |
| 2811 | int ret; |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2812 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2813 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2814 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2815 | VM_BUG_ON(!PageTransHuge(page)); |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 2816 | /* |
| 2817 | * Never OOM-kill a process for a huge page. The |
| 2818 | * fault handler will fall back to regular pages. |
| 2819 | */ |
| 2820 | oom = false; |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2821 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2822 | |
| 2823 | pc = lookup_page_cgroup(page); |
Johannes Weiner | af4a662 | 2011-03-23 16:42:24 -0700 | [diff] [blame] | 2824 | BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */ |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2825 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2826 | ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &mem, oom); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2827 | if (ret || !mem) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2828 | return ret; |
| 2829 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2830 | __mem_cgroup_commit_charge(mem, page, nr_pages, pc, ctype); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2831 | return 0; |
| 2832 | } |
| 2833 | |
| 2834 | int mem_cgroup_newpage_charge(struct page *page, |
| 2835 | struct mm_struct *mm, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2836 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2837 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 2838 | return 0; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2839 | /* |
| 2840 | * If already mapped, we don't have to account. |
| 2841 | * If page cache, page->mapping has address_space. |
| 2842 | * But page->mapping may have out-of-use anon_vma pointer, |
| 2843 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping |
| 2844 | * is NULL. |
| 2845 | */ |
| 2846 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) |
| 2847 | return 0; |
| 2848 | if (unlikely(!mm)) |
| 2849 | mm = &init_mm; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2850 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2851 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2852 | } |
| 2853 | |
Daisuke Nishimura | 83aae4c7 | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2854 | static void |
| 2855 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 2856 | enum charge_type ctype); |
| 2857 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2858 | static void |
| 2859 | __mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *mem, |
| 2860 | enum charge_type ctype) |
| 2861 | { |
| 2862 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 2863 | /* |
| 2864 | * In some case, SwapCache, FUSE(splice_buf->radixtree), the page |
| 2865 | * is already on LRU. It means the page may on some other page_cgroup's |
| 2866 | * LRU. Take care of it. |
| 2867 | */ |
| 2868 | mem_cgroup_lru_del_before_commit(page); |
| 2869 | __mem_cgroup_commit_charge(mem, page, 1, pc, ctype); |
| 2870 | mem_cgroup_lru_add_after_commit(page); |
| 2871 | return; |
| 2872 | } |
| 2873 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 2874 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 2875 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2876 | { |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2877 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2878 | int ret; |
| 2879 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2880 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 2881 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2882 | if (PageCompound(page)) |
| 2883 | return 0; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2884 | /* |
| 2885 | * Corner case handling. This is called from add_to_page_cache() |
| 2886 | * in usual. But some FS (shmem) precharges this page before calling it |
| 2887 | * and call add_to_page_cache() with GFP_NOWAIT. |
| 2888 | * |
| 2889 | * For GFP_NOWAIT case, the page may be pre-charged before calling |
| 2890 | * add_to_page_cache(). (See shmem.c) check it here and avoid to call |
| 2891 | * charge twice. (It works but has to pay a bit larger cost.) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2892 | * And when the page is SwapCache, it should take swap information |
| 2893 | * into account. This is under lock_page() now. |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2894 | */ |
| 2895 | if (!(gfp_mask & __GFP_WAIT)) { |
| 2896 | struct page_cgroup *pc; |
| 2897 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2898 | pc = lookup_page_cgroup(page); |
| 2899 | if (!pc) |
| 2900 | return 0; |
| 2901 | lock_page_cgroup(pc); |
| 2902 | if (PageCgroupUsed(pc)) { |
| 2903 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2904 | return 0; |
| 2905 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2906 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2907 | } |
| 2908 | |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2909 | if (unlikely(!mm)) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2910 | mm = &init_mm; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2911 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2912 | if (page_is_file_cache(page)) { |
| 2913 | ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &mem, true); |
| 2914 | if (ret || !mem) |
| 2915 | return ret; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2916 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2917 | /* |
| 2918 | * FUSE reuses pages without going through the final |
| 2919 | * put that would remove them from the LRU list, make |
| 2920 | * sure that they get relinked properly. |
| 2921 | */ |
| 2922 | __mem_cgroup_commit_charge_lrucare(page, mem, |
| 2923 | MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 2924 | return ret; |
| 2925 | } |
Daisuke Nishimura | 83aae4c7 | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2926 | /* shmem */ |
| 2927 | if (PageSwapCache(page)) { |
| 2928 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
| 2929 | if (!ret) |
| 2930 | __mem_cgroup_commit_charge_swapin(page, mem, |
| 2931 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
| 2932 | } else |
| 2933 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2934 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2935 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2936 | return ret; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 2937 | } |
| 2938 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2939 | /* |
| 2940 | * While swap-in, try_charge -> commit or cancel, the page is locked. |
| 2941 | * 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] | 2942 | * struct page_cgroup is acquired. This refcnt will be consumed by |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2943 | * "commit()" or removed by "cancel()" |
| 2944 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2945 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
| 2946 | struct page *page, |
| 2947 | gfp_t mask, struct mem_cgroup **ptr) |
| 2948 | { |
| 2949 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2950 | int ret; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2951 | |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 2952 | *ptr = NULL; |
| 2953 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2954 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2955 | return 0; |
| 2956 | |
| 2957 | if (!do_swap_account) |
| 2958 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2959 | /* |
| 2960 | * A racing thread's fault, or swapoff, may have already updated |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2961 | * the pte, and even removed page from swap cache: in those cases |
| 2962 | * do_swap_page()'s pte_same() test will fail; but there's also a |
| 2963 | * KSM case which does need to charge the page. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2964 | */ |
| 2965 | if (!PageSwapCache(page)) |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2966 | goto charge_cur_mm; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2967 | mem = try_get_mem_cgroup_from_page(page); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2968 | if (!mem) |
| 2969 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2970 | *ptr = mem; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2971 | ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2972 | css_put(&mem->css); |
| 2973 | return ret; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2974 | charge_cur_mm: |
| 2975 | if (unlikely(!mm)) |
| 2976 | mm = &init_mm; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2977 | return __mem_cgroup_try_charge(mm, mask, 1, ptr, true); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2978 | } |
| 2979 | |
Daisuke Nishimura | 83aae4c7 | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2980 | static void |
| 2981 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 2982 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2983 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2984 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2985 | return; |
| 2986 | if (!ptr) |
| 2987 | return; |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2988 | cgroup_exclude_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2989 | |
| 2990 | __mem_cgroup_commit_charge_lrucare(page, ptr, ctype); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2991 | /* |
| 2992 | * Now swap is on-memory. This means this page may be |
| 2993 | * counted both as mem and swap....double count. |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2994 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
| 2995 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() |
| 2996 | * may call delete_from_swap_cache() before reach here. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2997 | */ |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2998 | if (do_swap_account && PageSwapCache(page)) { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2999 | swp_entry_t ent = {.val = page_private(page)}; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3000 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3001 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3002 | |
| 3003 | id = swap_cgroup_record(ent, 0); |
| 3004 | rcu_read_lock(); |
| 3005 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3006 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3007 | /* |
| 3008 | * This recorded memcg can be obsolete one. So, avoid |
| 3009 | * calling css_tryget |
| 3010 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3011 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 3012 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3013 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3014 | mem_cgroup_put(memcg); |
| 3015 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3016 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3017 | } |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3018 | /* |
| 3019 | * At swapin, we may charge account against cgroup which has no tasks. |
| 3020 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 3021 | * In that case, we need to call pre_destroy() again. check it here. |
| 3022 | */ |
| 3023 | cgroup_release_and_wakeup_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3024 | } |
| 3025 | |
Daisuke Nishimura | 83aae4c7 | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 3026 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
| 3027 | { |
| 3028 | __mem_cgroup_commit_charge_swapin(page, ptr, |
| 3029 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 3030 | } |
| 3031 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3032 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem) |
| 3033 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3034 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3035 | return; |
| 3036 | if (!mem) |
| 3037 | return; |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 3038 | __mem_cgroup_cancel_charge(mem, 1); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3039 | } |
| 3040 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3041 | static void mem_cgroup_do_uncharge(struct mem_cgroup *mem, |
| 3042 | unsigned int nr_pages, |
| 3043 | const enum charge_type ctype) |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3044 | { |
| 3045 | struct memcg_batch_info *batch = NULL; |
| 3046 | bool uncharge_memsw = true; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3047 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3048 | /* If swapout, usage of swap doesn't decrease */ |
| 3049 | if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) |
| 3050 | uncharge_memsw = false; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3051 | |
| 3052 | batch = ¤t->memcg_batch; |
| 3053 | /* |
| 3054 | * In usual, we do css_get() when we remember memcg pointer. |
| 3055 | * But in this case, we keep res->usage until end of a series of |
| 3056 | * uncharges. Then, it's ok to ignore memcg's refcnt. |
| 3057 | */ |
| 3058 | if (!batch->memcg) |
| 3059 | batch->memcg = mem; |
| 3060 | /* |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3061 | * do_batch > 0 when unmapping pages or inode invalidate/truncate. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3062 | * 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] | 3063 | * the same cgroup and we have chance to coalesce uncharges. |
| 3064 | * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE) |
| 3065 | * because we want to do uncharge as soon as possible. |
| 3066 | */ |
| 3067 | |
| 3068 | if (!batch->do_batch || test_thread_flag(TIF_MEMDIE)) |
| 3069 | goto direct_uncharge; |
| 3070 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3071 | if (nr_pages > 1) |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 3072 | goto direct_uncharge; |
| 3073 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3074 | /* |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3075 | * In typical case, batch->memcg == mem. This means we can |
| 3076 | * merge a series of uncharges to an uncharge of res_counter. |
| 3077 | * If not, we uncharge res_counter ony by one. |
| 3078 | */ |
| 3079 | if (batch->memcg != mem) |
| 3080 | goto direct_uncharge; |
| 3081 | /* remember freed charge and uncharge it later */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3082 | batch->nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3083 | if (uncharge_memsw) |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3084 | batch->memsw_nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3085 | return; |
| 3086 | direct_uncharge: |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3087 | res_counter_uncharge(&mem->res, nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3088 | if (uncharge_memsw) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3089 | res_counter_uncharge(&mem->memsw, nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3090 | if (unlikely(batch->memcg != mem)) |
| 3091 | memcg_oom_recover(mem); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3092 | return; |
| 3093 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3094 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 3095 | /* |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3096 | * uncharge if !page_mapped(page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3097 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3098 | static struct mem_cgroup * |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3099 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3100 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3101 | struct mem_cgroup *mem = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3102 | unsigned int nr_pages = 1; |
| 3103 | struct page_cgroup *pc; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3104 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3105 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3106 | return NULL; |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 3107 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3108 | if (PageSwapCache(page)) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3109 | return NULL; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3110 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 3111 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3112 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 3113 | VM_BUG_ON(!PageTransHuge(page)); |
| 3114 | } |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 3115 | /* |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 3116 | * Check if our page_cgroup is valid |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 3117 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3118 | pc = lookup_page_cgroup(page); |
| 3119 | if (unlikely(!pc || !PageCgroupUsed(pc))) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3120 | return NULL; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3121 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3122 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3123 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3124 | mem = pc->mem_cgroup; |
| 3125 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3126 | if (!PageCgroupUsed(pc)) |
| 3127 | goto unlock_out; |
| 3128 | |
| 3129 | switch (ctype) { |
| 3130 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3131 | case MEM_CGROUP_CHARGE_TYPE_DROP: |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3132 | /* See mem_cgroup_prepare_migration() */ |
| 3133 | if (page_mapped(page) || PageCgroupMigration(pc)) |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3134 | goto unlock_out; |
| 3135 | break; |
| 3136 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: |
| 3137 | if (!PageAnon(page)) { /* Shared memory */ |
| 3138 | if (page->mapping && !page_is_file_cache(page)) |
| 3139 | goto unlock_out; |
| 3140 | } else if (page_mapped(page)) /* Anon */ |
| 3141 | goto unlock_out; |
| 3142 | break; |
| 3143 | default: |
| 3144 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3145 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3146 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3147 | mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), -nr_pages); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 3148 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3149 | ClearPageCgroupUsed(pc); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 3150 | /* |
| 3151 | * pc->mem_cgroup is not cleared here. It will be accessed when it's |
| 3152 | * freed from LRU. This is safe because uncharged page is expected not |
| 3153 | * to be reused (freed soon). Exception is SwapCache, it's handled by |
| 3154 | * special functions. |
| 3155 | */ |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 3156 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3157 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3158 | /* |
| 3159 | * even after unlock, we have mem->res.usage here and this memcg |
| 3160 | * will never be freed. |
| 3161 | */ |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 3162 | memcg_check_events(mem, page); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3163 | if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) { |
| 3164 | mem_cgroup_swap_statistics(mem, true); |
| 3165 | mem_cgroup_get(mem); |
| 3166 | } |
| 3167 | if (!mem_cgroup_is_root(mem)) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3168 | mem_cgroup_do_uncharge(mem, nr_pages, ctype); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 3169 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3170 | return mem; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3171 | |
| 3172 | unlock_out: |
| 3173 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3174 | return NULL; |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 3175 | } |
| 3176 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3177 | void mem_cgroup_uncharge_page(struct page *page) |
| 3178 | { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3179 | /* early check. */ |
| 3180 | if (page_mapped(page)) |
| 3181 | return; |
| 3182 | if (page->mapping && !PageAnon(page)) |
| 3183 | return; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3184 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 3185 | } |
| 3186 | |
| 3187 | void mem_cgroup_uncharge_cache_page(struct page *page) |
| 3188 | { |
| 3189 | VM_BUG_ON(page_mapped(page)); |
KAMEZAWA Hiroyuki | b7abea9 | 2008-10-18 20:28:09 -0700 | [diff] [blame] | 3190 | VM_BUG_ON(page->mapping); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3191 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 3192 | } |
| 3193 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3194 | /* |
| 3195 | * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate. |
| 3196 | * In that cases, pages are freed continuously and we can expect pages |
| 3197 | * are in the same memcg. All these calls itself limits the number of |
| 3198 | * pages freed at once, then uncharge_start/end() is called properly. |
| 3199 | * This may be called prural(2) times in a context, |
| 3200 | */ |
| 3201 | |
| 3202 | void mem_cgroup_uncharge_start(void) |
| 3203 | { |
| 3204 | current->memcg_batch.do_batch++; |
| 3205 | /* We can do nest. */ |
| 3206 | if (current->memcg_batch.do_batch == 1) { |
| 3207 | current->memcg_batch.memcg = NULL; |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3208 | current->memcg_batch.nr_pages = 0; |
| 3209 | current->memcg_batch.memsw_nr_pages = 0; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3210 | } |
| 3211 | } |
| 3212 | |
| 3213 | void mem_cgroup_uncharge_end(void) |
| 3214 | { |
| 3215 | struct memcg_batch_info *batch = ¤t->memcg_batch; |
| 3216 | |
| 3217 | if (!batch->do_batch) |
| 3218 | return; |
| 3219 | |
| 3220 | batch->do_batch--; |
| 3221 | if (batch->do_batch) /* If stacked, do nothing. */ |
| 3222 | return; |
| 3223 | |
| 3224 | if (!batch->memcg) |
| 3225 | return; |
| 3226 | /* |
| 3227 | * This "batch->memcg" is valid without any css_get/put etc... |
| 3228 | * bacause we hide charges behind us. |
| 3229 | */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3230 | if (batch->nr_pages) |
| 3231 | res_counter_uncharge(&batch->memcg->res, |
| 3232 | batch->nr_pages * PAGE_SIZE); |
| 3233 | if (batch->memsw_nr_pages) |
| 3234 | res_counter_uncharge(&batch->memcg->memsw, |
| 3235 | batch->memsw_nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3236 | memcg_oom_recover(batch->memcg); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3237 | /* forget this pointer (for sanity check) */ |
| 3238 | batch->memcg = NULL; |
| 3239 | } |
| 3240 | |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3241 | #ifdef CONFIG_SWAP |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3242 | /* |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3243 | * called after __delete_from_swap_cache() and drop "page" account. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3244 | * memcg information is recorded to swap_cgroup of "ent" |
| 3245 | */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3246 | void |
| 3247 | 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] | 3248 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3249 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3250 | int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3251 | |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3252 | if (!swapout) /* this was a swap cache but the swap is unused ! */ |
| 3253 | ctype = MEM_CGROUP_CHARGE_TYPE_DROP; |
| 3254 | |
| 3255 | memcg = __mem_cgroup_uncharge_common(page, ctype); |
| 3256 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3257 | /* |
| 3258 | * record memcg information, if swapout && memcg != NULL, |
| 3259 | * mem_cgroup_get() was called in uncharge(). |
| 3260 | */ |
| 3261 | if (do_swap_account && swapout && memcg) |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3262 | swap_cgroup_record(ent, css_id(&memcg->css)); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3263 | } |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3264 | #endif |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3265 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3266 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 3267 | /* |
| 3268 | * called from swap_entry_free(). remove record in swap_cgroup and |
| 3269 | * uncharge "memsw" account. |
| 3270 | */ |
| 3271 | void mem_cgroup_uncharge_swap(swp_entry_t ent) |
| 3272 | { |
| 3273 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3274 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3275 | |
| 3276 | if (!do_swap_account) |
| 3277 | return; |
| 3278 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3279 | id = swap_cgroup_record(ent, 0); |
| 3280 | rcu_read_lock(); |
| 3281 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3282 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3283 | /* |
| 3284 | * We uncharge this because swap is freed. |
| 3285 | * This memcg can be obsolete one. We avoid calling css_tryget |
| 3286 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3287 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 3288 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3289 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3290 | mem_cgroup_put(memcg); |
| 3291 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3292 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3293 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3294 | |
| 3295 | /** |
| 3296 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. |
| 3297 | * @entry: swap entry to be moved |
| 3298 | * @from: mem_cgroup which the entry is moved from |
| 3299 | * @to: mem_cgroup which the entry is moved to |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3300 | * @need_fixup: whether we should fixup res_counters and refcounts. |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3301 | * |
| 3302 | * It succeeds only when the swap_cgroup's record for this entry is the same |
| 3303 | * as the mem_cgroup's id of @from. |
| 3304 | * |
| 3305 | * Returns 0 on success, -EINVAL on failure. |
| 3306 | * |
| 3307 | * The caller must have charged to @to, IOW, called res_counter_charge() about |
| 3308 | * both res and memsw, and called css_get(). |
| 3309 | */ |
| 3310 | static int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3311 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3312 | { |
| 3313 | unsigned short old_id, new_id; |
| 3314 | |
| 3315 | old_id = css_id(&from->css); |
| 3316 | new_id = css_id(&to->css); |
| 3317 | |
| 3318 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3319 | mem_cgroup_swap_statistics(from, false); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3320 | mem_cgroup_swap_statistics(to, true); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3321 | /* |
| 3322 | * This function is only called from task migration context now. |
| 3323 | * It postpones res_counter and refcount handling till the end |
| 3324 | * of task migration(mem_cgroup_clear_mc()) for performance |
| 3325 | * improvement. But we cannot postpone mem_cgroup_get(to) |
| 3326 | * because if the process that has been moved to @to does |
| 3327 | * swap-in, the refcount of @to might be decreased to 0. |
| 3328 | */ |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3329 | mem_cgroup_get(to); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3330 | if (need_fixup) { |
| 3331 | if (!mem_cgroup_is_root(from)) |
| 3332 | res_counter_uncharge(&from->memsw, PAGE_SIZE); |
| 3333 | mem_cgroup_put(from); |
| 3334 | /* |
| 3335 | * we charged both to->res and to->memsw, so we should |
| 3336 | * uncharge to->res. |
| 3337 | */ |
| 3338 | if (!mem_cgroup_is_root(to)) |
| 3339 | res_counter_uncharge(&to->res, PAGE_SIZE); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3340 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3341 | return 0; |
| 3342 | } |
| 3343 | return -EINVAL; |
| 3344 | } |
| 3345 | #else |
| 3346 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3347 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3348 | { |
| 3349 | return -EINVAL; |
| 3350 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3351 | #endif |
| 3352 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3353 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3354 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
| 3355 | * page belongs to. |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3356 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3357 | int mem_cgroup_prepare_migration(struct page *page, |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 3358 | struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3359 | { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3360 | struct mem_cgroup *mem = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3361 | struct page_cgroup *pc; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3362 | enum charge_type ctype; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3363 | int ret = 0; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 3364 | |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 3365 | *ptr = NULL; |
| 3366 | |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 3367 | VM_BUG_ON(PageTransHuge(page)); |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3368 | if (mem_cgroup_disabled()) |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 3369 | return 0; |
| 3370 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3371 | pc = lookup_page_cgroup(page); |
| 3372 | lock_page_cgroup(pc); |
| 3373 | if (PageCgroupUsed(pc)) { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3374 | mem = pc->mem_cgroup; |
| 3375 | css_get(&mem->css); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3376 | /* |
| 3377 | * At migrating an anonymous page, its mapcount goes down |
| 3378 | * to 0 and uncharge() will be called. But, even if it's fully |
| 3379 | * unmapped, migration may fail and this page has to be |
| 3380 | * charged again. We set MIGRATION flag here and delay uncharge |
| 3381 | * until end_migration() is called |
| 3382 | * |
| 3383 | * Corner Case Thinking |
| 3384 | * A) |
| 3385 | * When the old page was mapped as Anon and it's unmap-and-freed |
| 3386 | * while migration was ongoing. |
| 3387 | * If unmap finds the old page, uncharge() of it will be delayed |
| 3388 | * until end_migration(). If unmap finds a new page, it's |
| 3389 | * uncharged when it make mapcount to be 1->0. If unmap code |
| 3390 | * finds swap_migration_entry, the new page will not be mapped |
| 3391 | * and end_migration() will find it(mapcount==0). |
| 3392 | * |
| 3393 | * B) |
| 3394 | * When the old page was mapped but migraion fails, the kernel |
| 3395 | * remaps it. A charge for it is kept by MIGRATION flag even |
| 3396 | * if mapcount goes down to 0. We can do remap successfully |
| 3397 | * without charging it again. |
| 3398 | * |
| 3399 | * C) |
| 3400 | * The "old" page is under lock_page() until the end of |
| 3401 | * migration, so, the old page itself will not be swapped-out. |
| 3402 | * If the new page is swapped out before end_migraton, our |
| 3403 | * hook to usual swap-out path will catch the event. |
| 3404 | */ |
| 3405 | if (PageAnon(page)) |
| 3406 | SetPageCgroupMigration(pc); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 3407 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3408 | unlock_page_cgroup(pc); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3409 | /* |
| 3410 | * If the page is not charged at this point, |
| 3411 | * we return here. |
| 3412 | */ |
| 3413 | if (!mem) |
| 3414 | return 0; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3415 | |
Andrea Arcangeli | 93d5c9b | 2010-04-23 13:17:39 -0400 | [diff] [blame] | 3416 | *ptr = mem; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3417 | 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] | 3418 | css_put(&mem->css);/* drop extra refcnt */ |
| 3419 | if (ret || *ptr == NULL) { |
| 3420 | if (PageAnon(page)) { |
| 3421 | lock_page_cgroup(pc); |
| 3422 | ClearPageCgroupMigration(pc); |
| 3423 | unlock_page_cgroup(pc); |
| 3424 | /* |
| 3425 | * The old page may be fully unmapped while we kept it. |
| 3426 | */ |
| 3427 | mem_cgroup_uncharge_page(page); |
| 3428 | } |
| 3429 | return -ENOMEM; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3430 | } |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3431 | /* |
| 3432 | * We charge new page before it's used/mapped. So, even if unlock_page() |
| 3433 | * is called before end_migration, we can catch all events on this new |
| 3434 | * page. In the case new page is migrated but not remapped, new page's |
| 3435 | * mapcount will be finally 0 and we call uncharge in end_migration(). |
| 3436 | */ |
| 3437 | pc = lookup_page_cgroup(newpage); |
| 3438 | if (PageAnon(page)) |
| 3439 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; |
| 3440 | else if (page_is_file_cache(page)) |
| 3441 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; |
| 3442 | else |
| 3443 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3444 | __mem_cgroup_commit_charge(mem, page, 1, pc, ctype); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3445 | return ret; |
| 3446 | } |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 3447 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3448 | /* remove redundant charge if migration failed*/ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3449 | void mem_cgroup_end_migration(struct mem_cgroup *mem, |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 3450 | struct page *oldpage, struct page *newpage, bool migration_ok) |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3451 | { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3452 | struct page *used, *unused; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3453 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3454 | |
| 3455 | if (!mem) |
| 3456 | return; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3457 | /* blocks rmdir() */ |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3458 | cgroup_exclude_rmdir(&mem->css); |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 3459 | if (!migration_ok) { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3460 | used = oldpage; |
| 3461 | unused = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3462 | } else { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3463 | used = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3464 | unused = oldpage; |
| 3465 | } |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3466 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3467 | * We disallowed uncharge of pages under migration because mapcount |
| 3468 | * of the page goes down to zero, temporarly. |
| 3469 | * Clear the flag and check the page should be charged. |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3470 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3471 | pc = lookup_page_cgroup(oldpage); |
| 3472 | lock_page_cgroup(pc); |
| 3473 | ClearPageCgroupMigration(pc); |
| 3474 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3475 | |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3476 | __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE); |
| 3477 | |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3478 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3479 | * If a page is a file cache, radix-tree replacement is very atomic |
| 3480 | * and we can skip this check. When it was an Anon page, its mapcount |
| 3481 | * goes down to 0. But because we added MIGRATION flage, it's not |
| 3482 | * uncharged yet. There are several case but page->mapcount check |
| 3483 | * and USED bit check in mem_cgroup_uncharge_page() will do enough |
| 3484 | * check. (see prepare_charge() also) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3485 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3486 | if (PageAnon(used)) |
| 3487 | mem_cgroup_uncharge_page(used); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3488 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3489 | * At migration, we may charge account against cgroup which has no |
| 3490 | * tasks. |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3491 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 3492 | * In that case, we need to call pre_destroy() again. check it here. |
| 3493 | */ |
| 3494 | cgroup_release_and_wakeup_rmdir(&mem->css); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3495 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 3496 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3497 | /* |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3498 | * A call to try to shrink memory usage on charge failure at shmem's swapin. |
| 3499 | * Calling hierarchical_reclaim is not enough because we should update |
| 3500 | * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM. |
| 3501 | * Moreover considering hierarchy, we should reclaim from the mem_over_limit, |
| 3502 | * not from the memcg which this page would be charged to. |
| 3503 | * try_charge_swapin does all of these works properly. |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3504 | */ |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3505 | int mem_cgroup_shmem_charge_fallback(struct page *page, |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 3506 | struct mm_struct *mm, |
| 3507 | gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3508 | { |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 3509 | struct mem_cgroup *mem; |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3510 | int ret; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3511 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3512 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 3513 | return 0; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3514 | |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3515 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
| 3516 | if (!ret) |
| 3517 | mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */ |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3518 | |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3519 | return ret; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3520 | } |
| 3521 | |
Daisuke Nishimura | f212ad7 | 2011-03-23 16:42:25 -0700 | [diff] [blame] | 3522 | #ifdef CONFIG_DEBUG_VM |
| 3523 | static struct page_cgroup *lookup_page_cgroup_used(struct page *page) |
| 3524 | { |
| 3525 | struct page_cgroup *pc; |
| 3526 | |
| 3527 | pc = lookup_page_cgroup(page); |
| 3528 | if (likely(pc) && PageCgroupUsed(pc)) |
| 3529 | return pc; |
| 3530 | return NULL; |
| 3531 | } |
| 3532 | |
| 3533 | bool mem_cgroup_bad_page_check(struct page *page) |
| 3534 | { |
| 3535 | if (mem_cgroup_disabled()) |
| 3536 | return false; |
| 3537 | |
| 3538 | return lookup_page_cgroup_used(page) != NULL; |
| 3539 | } |
| 3540 | |
| 3541 | void mem_cgroup_print_bad_page(struct page *page) |
| 3542 | { |
| 3543 | struct page_cgroup *pc; |
| 3544 | |
| 3545 | pc = lookup_page_cgroup_used(page); |
| 3546 | if (pc) { |
| 3547 | int ret = -1; |
| 3548 | char *path; |
| 3549 | |
| 3550 | printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p", |
| 3551 | pc, pc->flags, pc->mem_cgroup); |
| 3552 | |
| 3553 | path = kmalloc(PATH_MAX, GFP_KERNEL); |
| 3554 | if (path) { |
| 3555 | rcu_read_lock(); |
| 3556 | ret = cgroup_path(pc->mem_cgroup->css.cgroup, |
| 3557 | path, PATH_MAX); |
| 3558 | rcu_read_unlock(); |
| 3559 | } |
| 3560 | |
| 3561 | printk(KERN_CONT "(%s)\n", |
| 3562 | (ret < 0) ? "cannot get the path" : path); |
| 3563 | kfree(path); |
| 3564 | } |
| 3565 | } |
| 3566 | #endif |
| 3567 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3568 | static DEFINE_MUTEX(set_limit_mutex); |
| 3569 | |
KOSAKI Motohiro | d38d2a7 | 2009-01-06 14:39:44 -0800 | [diff] [blame] | 3570 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3571 | unsigned long long val) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3572 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3573 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3574 | u64 memswlimit, memlimit; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3575 | int ret = 0; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3576 | int children = mem_cgroup_count_children(memcg); |
| 3577 | u64 curusage, oldusage; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3578 | int enlarge; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3579 | |
| 3580 | /* |
| 3581 | * For keeping hierarchical_reclaim simple, how long we should retry |
| 3582 | * is depends on callers. We set our retry-count to be function |
| 3583 | * of # of children which we should visit in this loop. |
| 3584 | */ |
| 3585 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; |
| 3586 | |
| 3587 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3588 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3589 | enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3590 | while (retry_count) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3591 | if (signal_pending(current)) { |
| 3592 | ret = -EINTR; |
| 3593 | break; |
| 3594 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 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 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3602 | if (memswlimit < val) { |
| 3603 | ret = -EINVAL; |
| 3604 | mutex_unlock(&set_limit_mutex); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3605 | break; |
| 3606 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3607 | |
| 3608 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3609 | if (memlimit < val) |
| 3610 | enlarge = 1; |
| 3611 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3612 | ret = res_counter_set_limit(&memcg->res, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 3613 | if (!ret) { |
| 3614 | if (memswlimit == val) |
| 3615 | memcg->memsw_is_minimum = true; |
| 3616 | else |
| 3617 | memcg->memsw_is_minimum = false; |
| 3618 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3619 | mutex_unlock(&set_limit_mutex); |
| 3620 | |
| 3621 | if (!ret) |
| 3622 | break; |
| 3623 | |
Bob Liu | aa20d48 | 2009-12-15 16:47:14 -0800 | [diff] [blame] | 3624 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3625 | MEM_CGROUP_RECLAIM_SHRINK, |
| 3626 | NULL); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3627 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
| 3628 | /* Usage is reduced ? */ |
| 3629 | if (curusage >= oldusage) |
| 3630 | retry_count--; |
| 3631 | else |
| 3632 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3633 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3634 | if (!ret && enlarge) |
| 3635 | memcg_oom_recover(memcg); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 3636 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3637 | return ret; |
| 3638 | } |
| 3639 | |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 3640 | static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
| 3641 | unsigned long long val) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3642 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3643 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3644 | u64 memlimit, memswlimit, oldusage, curusage; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3645 | int children = mem_cgroup_count_children(memcg); |
| 3646 | int ret = -EBUSY; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3647 | int enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3648 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3649 | /* see mem_cgroup_resize_res_limit */ |
| 3650 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; |
| 3651 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3652 | while (retry_count) { |
| 3653 | if (signal_pending(current)) { |
| 3654 | ret = -EINTR; |
| 3655 | break; |
| 3656 | } |
| 3657 | /* |
| 3658 | * Rather than hide all in some function, I do this in |
| 3659 | * open coded manner. You see what this really does. |
| 3660 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 3661 | */ |
| 3662 | mutex_lock(&set_limit_mutex); |
| 3663 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3664 | if (memlimit > val) { |
| 3665 | ret = -EINVAL; |
| 3666 | mutex_unlock(&set_limit_mutex); |
| 3667 | break; |
| 3668 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3669 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3670 | if (memswlimit < val) |
| 3671 | enlarge = 1; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3672 | ret = res_counter_set_limit(&memcg->memsw, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 3673 | if (!ret) { |
| 3674 | if (memlimit == val) |
| 3675 | memcg->memsw_is_minimum = true; |
| 3676 | else |
| 3677 | memcg->memsw_is_minimum = false; |
| 3678 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3679 | mutex_unlock(&set_limit_mutex); |
| 3680 | |
| 3681 | if (!ret) |
| 3682 | break; |
| 3683 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3684 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 3685 | MEM_CGROUP_RECLAIM_NOSWAP | |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3686 | MEM_CGROUP_RECLAIM_SHRINK, |
| 3687 | NULL); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3688 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3689 | /* Usage is reduced ? */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3690 | if (curusage >= oldusage) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3691 | retry_count--; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3692 | else |
| 3693 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3694 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3695 | if (!ret && enlarge) |
| 3696 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3697 | return ret; |
| 3698 | } |
| 3699 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3700 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3701 | gfp_t gfp_mask, |
| 3702 | unsigned long *total_scanned) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3703 | { |
| 3704 | unsigned long nr_reclaimed = 0; |
| 3705 | struct mem_cgroup_per_zone *mz, *next_mz = NULL; |
| 3706 | unsigned long reclaimed; |
| 3707 | int loop = 0; |
| 3708 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3709 | unsigned long long excess; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3710 | unsigned long nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3711 | |
| 3712 | if (order > 0) |
| 3713 | return 0; |
| 3714 | |
KOSAKI Motohiro | 00918b6 | 2010-08-10 18:03:05 -0700 | [diff] [blame] | 3715 | 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] | 3716 | /* |
| 3717 | * This loop can run a while, specially if mem_cgroup's continuously |
| 3718 | * keep exceeding their soft limit and putting the system under |
| 3719 | * pressure |
| 3720 | */ |
| 3721 | do { |
| 3722 | if (next_mz) |
| 3723 | mz = next_mz; |
| 3724 | else |
| 3725 | mz = mem_cgroup_largest_soft_limit_node(mctz); |
| 3726 | if (!mz) |
| 3727 | break; |
| 3728 | |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3729 | nr_scanned = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3730 | reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone, |
| 3731 | gfp_mask, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3732 | MEM_CGROUP_RECLAIM_SOFT, |
| 3733 | &nr_scanned); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3734 | nr_reclaimed += reclaimed; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3735 | *total_scanned += nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3736 | spin_lock(&mctz->lock); |
| 3737 | |
| 3738 | /* |
| 3739 | * If we failed to reclaim anything from this memory cgroup |
| 3740 | * it is time to move on to the next cgroup |
| 3741 | */ |
| 3742 | next_mz = NULL; |
| 3743 | if (!reclaimed) { |
| 3744 | do { |
| 3745 | /* |
| 3746 | * Loop until we find yet another one. |
| 3747 | * |
| 3748 | * By the time we get the soft_limit lock |
| 3749 | * again, someone might have aded the |
| 3750 | * group back on the RB tree. Iterate to |
| 3751 | * make sure we get a different mem. |
| 3752 | * mem_cgroup_largest_soft_limit_node returns |
| 3753 | * NULL if no other cgroup is present on |
| 3754 | * the tree |
| 3755 | */ |
| 3756 | next_mz = |
| 3757 | __mem_cgroup_largest_soft_limit_node(mctz); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 3758 | if (next_mz == mz) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3759 | css_put(&next_mz->mem->css); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 3760 | else /* next_mz == NULL or other memcg */ |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3761 | break; |
| 3762 | } while (1); |
| 3763 | } |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3764 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3765 | excess = res_counter_soft_limit_excess(&mz->mem->res); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3766 | /* |
| 3767 | * One school of thought says that we should not add |
| 3768 | * back the node to the tree if reclaim returns 0. |
| 3769 | * But our reclaim could return 0, simply because due |
| 3770 | * to priority we are exposing a smaller subset of |
| 3771 | * memory to reclaim from. Consider this as a longer |
| 3772 | * term TODO. |
| 3773 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3774 | /* If excess == 0, no tree ops */ |
| 3775 | __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3776 | spin_unlock(&mctz->lock); |
| 3777 | css_put(&mz->mem->css); |
| 3778 | loop++; |
| 3779 | /* |
| 3780 | * Could not reclaim anything and there are no more |
| 3781 | * mem cgroups to try or we seem to be looping without |
| 3782 | * reclaiming anything. |
| 3783 | */ |
| 3784 | if (!nr_reclaimed && |
| 3785 | (next_mz == NULL || |
| 3786 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) |
| 3787 | break; |
| 3788 | } while (!nr_reclaimed); |
| 3789 | if (next_mz) |
| 3790 | css_put(&next_mz->mem->css); |
| 3791 | return nr_reclaimed; |
| 3792 | } |
| 3793 | |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3794 | /* |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3795 | * This routine traverse page_cgroup in given list and drop them all. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3796 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
| 3797 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3798 | static int mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3799 | int node, int zid, enum lru_list lru) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3800 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3801 | struct zone *zone; |
| 3802 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3803 | struct page_cgroup *pc, *busy; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3804 | unsigned long flags, loop; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3805 | struct list_head *list; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3806 | int ret = 0; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3807 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3808 | zone = &NODE_DATA(node)->node_zones[zid]; |
| 3809 | mz = mem_cgroup_zoneinfo(mem, node, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3810 | list = &mz->lists[lru]; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3811 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3812 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
| 3813 | /* give some margin against EBUSY etc...*/ |
| 3814 | loop += 256; |
| 3815 | busy = NULL; |
| 3816 | while (loop--) { |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3817 | struct page *page; |
| 3818 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3819 | ret = 0; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3820 | spin_lock_irqsave(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3821 | if (list_empty(list)) { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3822 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3823 | break; |
| 3824 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3825 | pc = list_entry(list->prev, struct page_cgroup, lru); |
| 3826 | if (busy == pc) { |
| 3827 | list_move(&pc->lru, list); |
Thiago Farina | 648bcc7 | 2010-03-05 13:42:04 -0800 | [diff] [blame] | 3828 | busy = NULL; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3829 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3830 | continue; |
| 3831 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3832 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3833 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 3834 | page = lookup_cgroup_page(pc); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3835 | |
| 3836 | ret = mem_cgroup_move_parent(page, pc, mem, GFP_KERNEL); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3837 | if (ret == -ENOMEM) |
| 3838 | break; |
| 3839 | |
| 3840 | if (ret == -EBUSY || ret == -EINVAL) { |
| 3841 | /* found lock contention or "pc" is obsolete. */ |
| 3842 | busy = pc; |
| 3843 | cond_resched(); |
| 3844 | } else |
| 3845 | busy = NULL; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3846 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3847 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3848 | if (!ret && !list_empty(list)) |
| 3849 | return -EBUSY; |
| 3850 | return ret; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3851 | } |
| 3852 | |
| 3853 | /* |
| 3854 | * make mem_cgroup's charge to be 0 if there is no task. |
| 3855 | * This enables deleting this mem_cgroup. |
| 3856 | */ |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3857 | 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] | 3858 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3859 | int ret; |
| 3860 | int node, zid, shrink; |
| 3861 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3862 | struct cgroup *cgrp = mem->css.cgroup; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 3863 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3864 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3865 | |
| 3866 | shrink = 0; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3867 | /* should free all ? */ |
| 3868 | if (free_all) |
| 3869 | goto try_to_free; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3870 | move_account: |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3871 | do { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3872 | ret = -EBUSY; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3873 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3874 | goto out; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3875 | ret = -EINTR; |
| 3876 | if (signal_pending(current)) |
| 3877 | goto out; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3878 | /* This is for making all *used* pages to be on LRU. */ |
| 3879 | lru_add_drain_all(); |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 3880 | drain_all_stock_sync(mem); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3881 | ret = 0; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 3882 | mem_cgroup_start_move(mem); |
KAMEZAWA Hiroyuki | 299b4ea | 2009-01-29 14:25:17 -0800 | [diff] [blame] | 3883 | for_each_node_state(node, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3884 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3885 | enum lru_list l; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3886 | for_each_lru(l) { |
| 3887 | ret = mem_cgroup_force_empty_list(mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3888 | node, zid, l); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3889 | if (ret) |
| 3890 | break; |
| 3891 | } |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 3892 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3893 | if (ret) |
| 3894 | break; |
| 3895 | } |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 3896 | mem_cgroup_end_move(mem); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3897 | memcg_oom_recover(mem); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3898 | /* it seems parent cgroup doesn't have enough mem */ |
| 3899 | if (ret == -ENOMEM) |
| 3900 | goto try_to_free; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3901 | cond_resched(); |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3902 | /* "ret" should also be checked to ensure all lists are empty. */ |
| 3903 | } while (mem->res.usage > 0 || ret); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3904 | out: |
| 3905 | css_put(&mem->css); |
| 3906 | return ret; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3907 | |
| 3908 | try_to_free: |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3909 | /* returns EBUSY if there is a task or if we come here twice. */ |
| 3910 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3911 | ret = -EBUSY; |
| 3912 | goto out; |
| 3913 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3914 | /* we call try-to-free pages for make this cgroup empty */ |
| 3915 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3916 | /* try to free all pages in this cgroup */ |
| 3917 | shrink = 1; |
| 3918 | while (nr_retries && mem->res.usage > 0) { |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 3919 | struct memcg_scanrecord rec; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3920 | int progress; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3921 | |
| 3922 | if (signal_pending(current)) { |
| 3923 | ret = -EINTR; |
| 3924 | goto out; |
| 3925 | } |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 3926 | rec.context = SCAN_BY_SHRINK; |
| 3927 | rec.mem = mem; |
| 3928 | rec.root = mem; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 3929 | progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL, |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 3930 | false, &rec); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3931 | if (!progress) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3932 | nr_retries--; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3933 | /* maybe some writeback is necessary */ |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 3934 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3935 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3936 | |
| 3937 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3938 | lru_add_drain(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3939 | /* try move_account...there may be some *locked* pages. */ |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3940 | goto move_account; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3941 | } |
| 3942 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3943 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
| 3944 | { |
| 3945 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); |
| 3946 | } |
| 3947 | |
| 3948 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3949 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
| 3950 | { |
| 3951 | return mem_cgroup_from_cont(cont)->use_hierarchy; |
| 3952 | } |
| 3953 | |
| 3954 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, |
| 3955 | u64 val) |
| 3956 | { |
| 3957 | int retval = 0; |
| 3958 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 3959 | struct cgroup *parent = cont->parent; |
| 3960 | struct mem_cgroup *parent_mem = NULL; |
| 3961 | |
| 3962 | if (parent) |
| 3963 | parent_mem = mem_cgroup_from_cont(parent); |
| 3964 | |
| 3965 | cgroup_lock(); |
| 3966 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 3967 | * 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] | 3968 | * in the child subtrees. If it is unset, then the change can |
| 3969 | * occur, provided the current cgroup has no children. |
| 3970 | * |
| 3971 | * For the root cgroup, parent_mem is NULL, we allow value to be |
| 3972 | * set if there are no children. |
| 3973 | */ |
| 3974 | if ((!parent_mem || !parent_mem->use_hierarchy) && |
| 3975 | (val == 1 || val == 0)) { |
| 3976 | if (list_empty(&cont->children)) |
| 3977 | mem->use_hierarchy = val; |
| 3978 | else |
| 3979 | retval = -EBUSY; |
| 3980 | } else |
| 3981 | retval = -EINVAL; |
| 3982 | cgroup_unlock(); |
| 3983 | |
| 3984 | return retval; |
| 3985 | } |
| 3986 | |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3987 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3988 | static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *mem, |
| 3989 | enum mem_cgroup_stat_index idx) |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3990 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3991 | struct mem_cgroup *iter; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3992 | long val = 0; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3993 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3994 | /* Per-cpu values can be negative, use a signed accumulator */ |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3995 | for_each_mem_cgroup_tree(iter, mem) |
| 3996 | val += mem_cgroup_read_stat(iter, idx); |
| 3997 | |
| 3998 | if (val < 0) /* race ? */ |
| 3999 | val = 0; |
| 4000 | return val; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 4001 | } |
| 4002 | |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4003 | static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap) |
| 4004 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4005 | u64 val; |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4006 | |
| 4007 | if (!mem_cgroup_is_root(mem)) { |
| 4008 | if (!swap) |
| 4009 | return res_counter_read_u64(&mem->res, RES_USAGE); |
| 4010 | else |
| 4011 | return res_counter_read_u64(&mem->memsw, RES_USAGE); |
| 4012 | } |
| 4013 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 4014 | val = mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_CACHE); |
| 4015 | val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_RSS); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4016 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4017 | if (swap) |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 4018 | val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_SWAPOUT); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4019 | |
| 4020 | return val << PAGE_SHIFT; |
| 4021 | } |
| 4022 | |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4023 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4024 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4025 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4026 | u64 val; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4027 | int type, name; |
| 4028 | |
| 4029 | type = MEMFILE_TYPE(cft->private); |
| 4030 | name = MEMFILE_ATTR(cft->private); |
| 4031 | switch (type) { |
| 4032 | case _MEM: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4033 | if (name == RES_USAGE) |
| 4034 | val = mem_cgroup_usage(mem, false); |
| 4035 | else |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 4036 | val = res_counter_read_u64(&mem->res, name); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4037 | break; |
| 4038 | case _MEMSWAP: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4039 | if (name == RES_USAGE) |
| 4040 | val = mem_cgroup_usage(mem, true); |
| 4041 | else |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 4042 | val = res_counter_read_u64(&mem->memsw, name); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4043 | break; |
| 4044 | default: |
| 4045 | BUG(); |
| 4046 | break; |
| 4047 | } |
| 4048 | return val; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4049 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4050 | /* |
| 4051 | * The user of this function is... |
| 4052 | * RES_LIMIT. |
| 4053 | */ |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 4054 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 4055 | const char *buffer) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4056 | { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4057 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4058 | int type, name; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4059 | unsigned long long val; |
| 4060 | int ret; |
| 4061 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4062 | type = MEMFILE_TYPE(cft->private); |
| 4063 | name = MEMFILE_ATTR(cft->private); |
| 4064 | switch (name) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4065 | case RES_LIMIT: |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 4066 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
| 4067 | ret = -EINVAL; |
| 4068 | break; |
| 4069 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4070 | /* This function does all necessary parse...reuse it */ |
| 4071 | ret = res_counter_memparse_write_strategy(buffer, &val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4072 | if (ret) |
| 4073 | break; |
| 4074 | if (type == _MEM) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4075 | ret = mem_cgroup_resize_limit(memcg, val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4076 | else |
| 4077 | ret = mem_cgroup_resize_memsw_limit(memcg, val); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4078 | break; |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 4079 | case RES_SOFT_LIMIT: |
| 4080 | ret = res_counter_memparse_write_strategy(buffer, &val); |
| 4081 | if (ret) |
| 4082 | break; |
| 4083 | /* |
| 4084 | * For memsw, soft limits are hard to implement in terms |
| 4085 | * of semantics, for now, we support soft limits for |
| 4086 | * control without swap |
| 4087 | */ |
| 4088 | if (type == _MEM) |
| 4089 | ret = res_counter_set_soft_limit(&memcg->res, val); |
| 4090 | else |
| 4091 | ret = -EINVAL; |
| 4092 | break; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4093 | default: |
| 4094 | ret = -EINVAL; /* should be BUG() ? */ |
| 4095 | break; |
| 4096 | } |
| 4097 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4098 | } |
| 4099 | |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 4100 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
| 4101 | unsigned long long *mem_limit, unsigned long long *memsw_limit) |
| 4102 | { |
| 4103 | struct cgroup *cgroup; |
| 4104 | unsigned long long min_limit, min_memsw_limit, tmp; |
| 4105 | |
| 4106 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 4107 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 4108 | cgroup = memcg->css.cgroup; |
| 4109 | if (!memcg->use_hierarchy) |
| 4110 | goto out; |
| 4111 | |
| 4112 | while (cgroup->parent) { |
| 4113 | cgroup = cgroup->parent; |
| 4114 | memcg = mem_cgroup_from_cont(cgroup); |
| 4115 | if (!memcg->use_hierarchy) |
| 4116 | break; |
| 4117 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 4118 | min_limit = min(min_limit, tmp); |
| 4119 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 4120 | min_memsw_limit = min(min_memsw_limit, tmp); |
| 4121 | } |
| 4122 | out: |
| 4123 | *mem_limit = min_limit; |
| 4124 | *memsw_limit = min_memsw_limit; |
| 4125 | return; |
| 4126 | } |
| 4127 | |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4128 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4129 | { |
| 4130 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4131 | int type, name; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4132 | |
| 4133 | mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4134 | type = MEMFILE_TYPE(event); |
| 4135 | name = MEMFILE_ATTR(event); |
| 4136 | switch (name) { |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4137 | case RES_MAX_USAGE: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4138 | if (type == _MEM) |
| 4139 | res_counter_reset_max(&mem->res); |
| 4140 | else |
| 4141 | res_counter_reset_max(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4142 | break; |
| 4143 | case RES_FAILCNT: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4144 | if (type == _MEM) |
| 4145 | res_counter_reset_failcnt(&mem->res); |
| 4146 | else |
| 4147 | res_counter_reset_failcnt(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4148 | break; |
| 4149 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4150 | |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 4151 | return 0; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4152 | } |
| 4153 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4154 | static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp, |
| 4155 | struct cftype *cft) |
| 4156 | { |
| 4157 | return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate; |
| 4158 | } |
| 4159 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4160 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4161 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 4162 | struct cftype *cft, u64 val) |
| 4163 | { |
| 4164 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4165 | |
| 4166 | if (val >= (1 << NR_MOVE_TYPE)) |
| 4167 | return -EINVAL; |
| 4168 | /* |
| 4169 | * We check this value several times in both in can_attach() and |
| 4170 | * attach(), so we need cgroup lock to prevent this value from being |
| 4171 | * inconsistent. |
| 4172 | */ |
| 4173 | cgroup_lock(); |
| 4174 | mem->move_charge_at_immigrate = val; |
| 4175 | cgroup_unlock(); |
| 4176 | |
| 4177 | return 0; |
| 4178 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4179 | #else |
| 4180 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 4181 | struct cftype *cft, u64 val) |
| 4182 | { |
| 4183 | return -ENOSYS; |
| 4184 | } |
| 4185 | #endif |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4186 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4187 | |
| 4188 | /* For read statistics */ |
| 4189 | enum { |
| 4190 | MCS_CACHE, |
| 4191 | MCS_RSS, |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 4192 | MCS_FILE_MAPPED, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4193 | MCS_PGPGIN, |
| 4194 | MCS_PGPGOUT, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4195 | MCS_SWAP, |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4196 | MCS_PGFAULT, |
| 4197 | MCS_PGMAJFAULT, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4198 | MCS_INACTIVE_ANON, |
| 4199 | MCS_ACTIVE_ANON, |
| 4200 | MCS_INACTIVE_FILE, |
| 4201 | MCS_ACTIVE_FILE, |
| 4202 | MCS_UNEVICTABLE, |
| 4203 | NR_MCS_STAT, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4204 | }; |
| 4205 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4206 | struct mcs_total_stat { |
| 4207 | s64 stat[NR_MCS_STAT]; |
| 4208 | }; |
| 4209 | |
| 4210 | struct { |
| 4211 | char *local_name; |
| 4212 | char *total_name; |
| 4213 | } memcg_stat_strings[NR_MCS_STAT] = { |
| 4214 | {"cache", "total_cache"}, |
| 4215 | {"rss", "total_rss"}, |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 4216 | {"mapped_file", "total_mapped_file"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4217 | {"pgpgin", "total_pgpgin"}, |
| 4218 | {"pgpgout", "total_pgpgout"}, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4219 | {"swap", "total_swap"}, |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4220 | {"pgfault", "total_pgfault"}, |
| 4221 | {"pgmajfault", "total_pgmajfault"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4222 | {"inactive_anon", "total_inactive_anon"}, |
| 4223 | {"active_anon", "total_active_anon"}, |
| 4224 | {"inactive_file", "total_inactive_file"}, |
| 4225 | {"active_file", "total_active_file"}, |
| 4226 | {"unevictable", "total_unevictable"} |
| 4227 | }; |
| 4228 | |
| 4229 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4230 | static void |
| 4231 | 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] | 4232 | { |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4233 | s64 val; |
| 4234 | |
| 4235 | /* per cpu stat */ |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4236 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4237 | s->stat[MCS_CACHE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4238 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4239 | s->stat[MCS_RSS] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4240 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED); |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 4241 | s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 4242 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGIN); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4243 | s->stat[MCS_PGPGIN] += val; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 4244 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGOUT); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4245 | s->stat[MCS_PGPGOUT] += val; |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4246 | if (do_swap_account) { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4247 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4248 | s->stat[MCS_SWAP] += val * PAGE_SIZE; |
| 4249 | } |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4250 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGFAULT); |
| 4251 | s->stat[MCS_PGFAULT] += val; |
| 4252 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGMAJFAULT); |
| 4253 | s->stat[MCS_PGMAJFAULT] += val; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4254 | |
| 4255 | /* per zone stat */ |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4256 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_ANON)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4257 | s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4258 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_ANON)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4259 | s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4260 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_FILE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4261 | s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4262 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_FILE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4263 | s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4264 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_UNEVICTABLE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4265 | s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4266 | } |
| 4267 | |
| 4268 | static void |
| 4269 | mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s) |
| 4270 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4271 | struct mem_cgroup *iter; |
| 4272 | |
| 4273 | for_each_mem_cgroup_tree(iter, mem) |
| 4274 | mem_cgroup_get_local_stat(iter, s); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4275 | } |
| 4276 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4277 | #ifdef CONFIG_NUMA |
| 4278 | static int mem_control_numa_stat_show(struct seq_file *m, void *arg) |
| 4279 | { |
| 4280 | int nid; |
| 4281 | unsigned long total_nr, file_nr, anon_nr, unevictable_nr; |
| 4282 | unsigned long node_nr; |
| 4283 | struct cgroup *cont = m->private; |
| 4284 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
| 4285 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4286 | total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4287 | seq_printf(m, "total=%lu", total_nr); |
| 4288 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4289 | 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] | 4290 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4291 | } |
| 4292 | seq_putc(m, '\n'); |
| 4293 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4294 | file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4295 | seq_printf(m, "file=%lu", file_nr); |
| 4296 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4297 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4298 | LRU_ALL_FILE); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4299 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4300 | } |
| 4301 | seq_putc(m, '\n'); |
| 4302 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4303 | anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4304 | seq_printf(m, "anon=%lu", anon_nr); |
| 4305 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4306 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4307 | LRU_ALL_ANON); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4308 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4309 | } |
| 4310 | seq_putc(m, '\n'); |
| 4311 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4312 | unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE)); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4313 | seq_printf(m, "unevictable=%lu", unevictable_nr); |
| 4314 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4315 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4316 | BIT(LRU_UNEVICTABLE)); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4317 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4318 | } |
| 4319 | seq_putc(m, '\n'); |
| 4320 | return 0; |
| 4321 | } |
| 4322 | #endif /* CONFIG_NUMA */ |
| 4323 | |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 4324 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
| 4325 | struct cgroup_map_cb *cb) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4326 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4327 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4328 | struct mcs_total_stat mystat; |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4329 | int i; |
| 4330 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4331 | memset(&mystat, 0, sizeof(mystat)); |
| 4332 | mem_cgroup_get_local_stat(mem_cont, &mystat); |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4333 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4334 | |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4335 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 4336 | if (i == MCS_SWAP && !do_swap_account) |
| 4337 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4338 | cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4339 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4340 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4341 | /* Hierarchical information */ |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 4342 | { |
| 4343 | unsigned long long limit, memsw_limit; |
| 4344 | memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit); |
| 4345 | cb->fill(cb, "hierarchical_memory_limit", limit); |
| 4346 | if (do_swap_account) |
| 4347 | cb->fill(cb, "hierarchical_memsw_limit", memsw_limit); |
| 4348 | } |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4349 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4350 | memset(&mystat, 0, sizeof(mystat)); |
| 4351 | mem_cgroup_get_total_stat(mem_cont, &mystat); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4352 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 4353 | if (i == MCS_SWAP && !do_swap_account) |
| 4354 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4355 | cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4356 | } |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4357 | |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4358 | #ifdef CONFIG_DEBUG_VM |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 4359 | cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL)); |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4360 | |
| 4361 | { |
| 4362 | int nid, zid; |
| 4363 | struct mem_cgroup_per_zone *mz; |
| 4364 | unsigned long recent_rotated[2] = {0, 0}; |
| 4365 | unsigned long recent_scanned[2] = {0, 0}; |
| 4366 | |
| 4367 | for_each_online_node(nid) |
| 4368 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 4369 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
| 4370 | |
| 4371 | recent_rotated[0] += |
| 4372 | mz->reclaim_stat.recent_rotated[0]; |
| 4373 | recent_rotated[1] += |
| 4374 | mz->reclaim_stat.recent_rotated[1]; |
| 4375 | recent_scanned[0] += |
| 4376 | mz->reclaim_stat.recent_scanned[0]; |
| 4377 | recent_scanned[1] += |
| 4378 | mz->reclaim_stat.recent_scanned[1]; |
| 4379 | } |
| 4380 | cb->fill(cb, "recent_rotated_anon", recent_rotated[0]); |
| 4381 | cb->fill(cb, "recent_rotated_file", recent_rotated[1]); |
| 4382 | cb->fill(cb, "recent_scanned_anon", recent_scanned[0]); |
| 4383 | cb->fill(cb, "recent_scanned_file", recent_scanned[1]); |
| 4384 | } |
| 4385 | #endif |
| 4386 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4387 | return 0; |
| 4388 | } |
| 4389 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4390 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
| 4391 | { |
| 4392 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4393 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 4394 | return mem_cgroup_swappiness(memcg); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4395 | } |
| 4396 | |
| 4397 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, |
| 4398 | u64 val) |
| 4399 | { |
| 4400 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4401 | struct mem_cgroup *parent; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4402 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4403 | if (val > 100) |
| 4404 | return -EINVAL; |
| 4405 | |
| 4406 | if (cgrp->parent == NULL) |
| 4407 | return -EINVAL; |
| 4408 | |
| 4409 | parent = mem_cgroup_from_cont(cgrp->parent); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4410 | |
| 4411 | cgroup_lock(); |
| 4412 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4413 | /* If under hierarchy, only empty-root can set this value */ |
| 4414 | if ((parent->use_hierarchy) || |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4415 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
| 4416 | cgroup_unlock(); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4417 | return -EINVAL; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4418 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4419 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4420 | memcg->swappiness = val; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4421 | |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4422 | cgroup_unlock(); |
| 4423 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4424 | return 0; |
| 4425 | } |
| 4426 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4427 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) |
| 4428 | { |
| 4429 | struct mem_cgroup_threshold_ary *t; |
| 4430 | u64 usage; |
| 4431 | int i; |
| 4432 | |
| 4433 | rcu_read_lock(); |
| 4434 | if (!swap) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4435 | t = rcu_dereference(memcg->thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4436 | else |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4437 | t = rcu_dereference(memcg->memsw_thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4438 | |
| 4439 | if (!t) |
| 4440 | goto unlock; |
| 4441 | |
| 4442 | usage = mem_cgroup_usage(memcg, swap); |
| 4443 | |
| 4444 | /* |
| 4445 | * current_threshold points to threshold just below usage. |
| 4446 | * If it's not true, a threshold was crossed after last |
| 4447 | * call of __mem_cgroup_threshold(). |
| 4448 | */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4449 | i = t->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4450 | |
| 4451 | /* |
| 4452 | * Iterate backward over array of thresholds starting from |
| 4453 | * current_threshold and check if a threshold is crossed. |
| 4454 | * If none of thresholds below usage is crossed, we read |
| 4455 | * only one element of the array here. |
| 4456 | */ |
| 4457 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) |
| 4458 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4459 | |
| 4460 | /* i = current_threshold + 1 */ |
| 4461 | i++; |
| 4462 | |
| 4463 | /* |
| 4464 | * Iterate forward over array of thresholds starting from |
| 4465 | * current_threshold+1 and check if a threshold is crossed. |
| 4466 | * If none of thresholds above usage is crossed, we read |
| 4467 | * only one element of the array here. |
| 4468 | */ |
| 4469 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) |
| 4470 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4471 | |
| 4472 | /* Update current_threshold */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4473 | t->current_threshold = i - 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4474 | unlock: |
| 4475 | rcu_read_unlock(); |
| 4476 | } |
| 4477 | |
| 4478 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) |
| 4479 | { |
Kirill A. Shutemov | ad4ca5f | 2010-10-07 12:59:27 -0700 | [diff] [blame] | 4480 | while (memcg) { |
| 4481 | __mem_cgroup_threshold(memcg, false); |
| 4482 | if (do_swap_account) |
| 4483 | __mem_cgroup_threshold(memcg, true); |
| 4484 | |
| 4485 | memcg = parent_mem_cgroup(memcg); |
| 4486 | } |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | static int compare_thresholds(const void *a, const void *b) |
| 4490 | { |
| 4491 | const struct mem_cgroup_threshold *_a = a; |
| 4492 | const struct mem_cgroup_threshold *_b = b; |
| 4493 | |
| 4494 | return _a->threshold - _b->threshold; |
| 4495 | } |
| 4496 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4497 | static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4498 | { |
| 4499 | struct mem_cgroup_eventfd_list *ev; |
| 4500 | |
| 4501 | list_for_each_entry(ev, &mem->oom_notify, list) |
| 4502 | eventfd_signal(ev->eventfd, 1); |
| 4503 | return 0; |
| 4504 | } |
| 4505 | |
| 4506 | static void mem_cgroup_oom_notify(struct mem_cgroup *mem) |
| 4507 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4508 | struct mem_cgroup *iter; |
| 4509 | |
| 4510 | for_each_mem_cgroup_tree(iter, mem) |
| 4511 | mem_cgroup_oom_notify_cb(iter); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | static int mem_cgroup_usage_register_event(struct cgroup *cgrp, |
| 4515 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4516 | { |
| 4517 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4518 | struct mem_cgroup_thresholds *thresholds; |
| 4519 | struct mem_cgroup_threshold_ary *new; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4520 | int type = MEMFILE_TYPE(cft->private); |
| 4521 | u64 threshold, usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4522 | int i, size, ret; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4523 | |
| 4524 | ret = res_counter_memparse_write_strategy(args, &threshold); |
| 4525 | if (ret) |
| 4526 | return ret; |
| 4527 | |
| 4528 | mutex_lock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4529 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4530 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4531 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4532 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4533 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4534 | else |
| 4535 | BUG(); |
| 4536 | |
| 4537 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 4538 | |
| 4539 | /* Check if a threshold crossed before adding a new one */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4540 | if (thresholds->primary) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4541 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4542 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4543 | size = thresholds->primary ? thresholds->primary->size + 1 : 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4544 | |
| 4545 | /* Allocate memory for new array of thresholds */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4546 | new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4547 | GFP_KERNEL); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4548 | if (!new) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4549 | ret = -ENOMEM; |
| 4550 | goto unlock; |
| 4551 | } |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4552 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4553 | |
| 4554 | /* Copy thresholds (if any) to new array */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4555 | if (thresholds->primary) { |
| 4556 | memcpy(new->entries, thresholds->primary->entries, (size - 1) * |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4557 | sizeof(struct mem_cgroup_threshold)); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4558 | } |
| 4559 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4560 | /* Add new threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4561 | new->entries[size - 1].eventfd = eventfd; |
| 4562 | new->entries[size - 1].threshold = threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4563 | |
| 4564 | /* Sort thresholds. Registering of new threshold isn't time-critical */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4565 | sort(new->entries, size, sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4566 | compare_thresholds, NULL); |
| 4567 | |
| 4568 | /* Find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4569 | new->current_threshold = -1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4570 | for (i = 0; i < size; i++) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4571 | if (new->entries[i].threshold < usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4572 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4573 | * new->current_threshold will not be used until |
| 4574 | * rcu_assign_pointer(), so it's safe to increment |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4575 | * it here. |
| 4576 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4577 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4578 | } |
| 4579 | } |
| 4580 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4581 | /* Free old spare buffer and save old primary buffer as spare */ |
| 4582 | kfree(thresholds->spare); |
| 4583 | thresholds->spare = thresholds->primary; |
| 4584 | |
| 4585 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4586 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4587 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4588 | synchronize_rcu(); |
| 4589 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4590 | unlock: |
| 4591 | mutex_unlock(&memcg->thresholds_lock); |
| 4592 | |
| 4593 | return ret; |
| 4594 | } |
| 4595 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4596 | static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4597 | struct cftype *cft, struct eventfd_ctx *eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4598 | { |
| 4599 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4600 | struct mem_cgroup_thresholds *thresholds; |
| 4601 | struct mem_cgroup_threshold_ary *new; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4602 | int type = MEMFILE_TYPE(cft->private); |
| 4603 | u64 usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4604 | int i, j, size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4605 | |
| 4606 | mutex_lock(&memcg->thresholds_lock); |
| 4607 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4608 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4609 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4610 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4611 | else |
| 4612 | BUG(); |
| 4613 | |
| 4614 | /* |
| 4615 | * Something went wrong if we trying to unregister a threshold |
| 4616 | * if we don't have thresholds |
| 4617 | */ |
| 4618 | BUG_ON(!thresholds); |
| 4619 | |
| 4620 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 4621 | |
| 4622 | /* Check if a threshold crossed before removing */ |
| 4623 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4624 | |
| 4625 | /* Calculate new number of threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4626 | size = 0; |
| 4627 | for (i = 0; i < thresholds->primary->size; i++) { |
| 4628 | if (thresholds->primary->entries[i].eventfd != eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4629 | size++; |
| 4630 | } |
| 4631 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4632 | new = thresholds->spare; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4633 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4634 | /* Set thresholds array to NULL if we don't have thresholds */ |
| 4635 | if (!size) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4636 | kfree(new); |
| 4637 | new = NULL; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4638 | goto swap_buffers; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4639 | } |
| 4640 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4641 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4642 | |
| 4643 | /* Copy thresholds and find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4644 | new->current_threshold = -1; |
| 4645 | for (i = 0, j = 0; i < thresholds->primary->size; i++) { |
| 4646 | if (thresholds->primary->entries[i].eventfd == eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4647 | continue; |
| 4648 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4649 | new->entries[j] = thresholds->primary->entries[i]; |
| 4650 | if (new->entries[j].threshold < usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4651 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4652 | * new->current_threshold will not be used |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4653 | * until rcu_assign_pointer(), so it's safe to increment |
| 4654 | * it here. |
| 4655 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4656 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4657 | } |
| 4658 | j++; |
| 4659 | } |
| 4660 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4661 | swap_buffers: |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4662 | /* Swap primary and spare array */ |
| 4663 | thresholds->spare = thresholds->primary; |
| 4664 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4665 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4666 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4667 | synchronize_rcu(); |
| 4668 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4669 | mutex_unlock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4670 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4671 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4672 | static int mem_cgroup_oom_register_event(struct cgroup *cgrp, |
| 4673 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
| 4674 | { |
| 4675 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4676 | struct mem_cgroup_eventfd_list *event; |
| 4677 | int type = MEMFILE_TYPE(cft->private); |
| 4678 | |
| 4679 | BUG_ON(type != _OOM_TYPE); |
| 4680 | event = kmalloc(sizeof(*event), GFP_KERNEL); |
| 4681 | if (!event) |
| 4682 | return -ENOMEM; |
| 4683 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4684 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4685 | |
| 4686 | event->eventfd = eventfd; |
| 4687 | list_add(&event->list, &memcg->oom_notify); |
| 4688 | |
| 4689 | /* already in OOM ? */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 4690 | if (atomic_read(&memcg->under_oom)) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4691 | eventfd_signal(eventfd, 1); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4692 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4693 | |
| 4694 | return 0; |
| 4695 | } |
| 4696 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4697 | static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4698 | struct cftype *cft, struct eventfd_ctx *eventfd) |
| 4699 | { |
| 4700 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4701 | struct mem_cgroup_eventfd_list *ev, *tmp; |
| 4702 | int type = MEMFILE_TYPE(cft->private); |
| 4703 | |
| 4704 | BUG_ON(type != _OOM_TYPE); |
| 4705 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4706 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4707 | |
| 4708 | list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) { |
| 4709 | if (ev->eventfd == eventfd) { |
| 4710 | list_del(&ev->list); |
| 4711 | kfree(ev); |
| 4712 | } |
| 4713 | } |
| 4714 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4715 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4716 | } |
| 4717 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4718 | static int mem_cgroup_oom_control_read(struct cgroup *cgrp, |
| 4719 | struct cftype *cft, struct cgroup_map_cb *cb) |
| 4720 | { |
| 4721 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4722 | |
| 4723 | cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable); |
| 4724 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 4725 | if (atomic_read(&mem->under_oom)) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4726 | cb->fill(cb, "under_oom", 1); |
| 4727 | else |
| 4728 | cb->fill(cb, "under_oom", 0); |
| 4729 | return 0; |
| 4730 | } |
| 4731 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4732 | static int mem_cgroup_oom_control_write(struct cgroup *cgrp, |
| 4733 | struct cftype *cft, u64 val) |
| 4734 | { |
| 4735 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4736 | struct mem_cgroup *parent; |
| 4737 | |
| 4738 | /* cannot set to root cgroup and only 0 and 1 are allowed */ |
| 4739 | if (!cgrp->parent || !((val == 0) || (val == 1))) |
| 4740 | return -EINVAL; |
| 4741 | |
| 4742 | parent = mem_cgroup_from_cont(cgrp->parent); |
| 4743 | |
| 4744 | cgroup_lock(); |
| 4745 | /* oom-kill-disable is a flag for subhierarchy. */ |
| 4746 | if ((parent->use_hierarchy) || |
| 4747 | (mem->use_hierarchy && !list_empty(&cgrp->children))) { |
| 4748 | cgroup_unlock(); |
| 4749 | return -EINVAL; |
| 4750 | } |
| 4751 | mem->oom_kill_disable = val; |
KAMEZAWA Hiroyuki | 4d845eb | 2010-06-29 15:05:18 -0700 | [diff] [blame] | 4752 | if (!val) |
| 4753 | memcg_oom_recover(mem); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4754 | cgroup_unlock(); |
| 4755 | return 0; |
| 4756 | } |
| 4757 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4758 | #ifdef CONFIG_NUMA |
| 4759 | static const struct file_operations mem_control_numa_stat_file_operations = { |
| 4760 | .read = seq_read, |
| 4761 | .llseek = seq_lseek, |
| 4762 | .release = single_release, |
| 4763 | }; |
| 4764 | |
| 4765 | static int mem_control_numa_stat_open(struct inode *unused, struct file *file) |
| 4766 | { |
| 4767 | struct cgroup *cont = file->f_dentry->d_parent->d_fsdata; |
| 4768 | |
| 4769 | file->f_op = &mem_control_numa_stat_file_operations; |
| 4770 | return single_open(file, mem_control_numa_stat_show, cont); |
| 4771 | } |
| 4772 | #endif /* CONFIG_NUMA */ |
| 4773 | |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 4774 | static int mem_cgroup_vmscan_stat_read(struct cgroup *cgrp, |
| 4775 | struct cftype *cft, |
| 4776 | struct cgroup_map_cb *cb) |
| 4777 | { |
| 4778 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4779 | char string[64]; |
| 4780 | int i; |
| 4781 | |
| 4782 | for (i = 0; i < NR_SCANSTATS; i++) { |
| 4783 | strcpy(string, scanstat_string[i]); |
| 4784 | strcat(string, SCANSTAT_WORD_LIMIT); |
| 4785 | cb->fill(cb, string, mem->scanstat.stats[SCAN_BY_LIMIT][i]); |
| 4786 | } |
| 4787 | |
| 4788 | for (i = 0; i < NR_SCANSTATS; i++) { |
| 4789 | strcpy(string, scanstat_string[i]); |
| 4790 | strcat(string, SCANSTAT_WORD_SYSTEM); |
| 4791 | cb->fill(cb, string, mem->scanstat.stats[SCAN_BY_SYSTEM][i]); |
| 4792 | } |
| 4793 | |
| 4794 | for (i = 0; i < NR_SCANSTATS; i++) { |
| 4795 | strcpy(string, scanstat_string[i]); |
| 4796 | strcat(string, SCANSTAT_WORD_LIMIT); |
| 4797 | strcat(string, SCANSTAT_WORD_HIERARCHY); |
| 4798 | cb->fill(cb, string, mem->scanstat.rootstats[SCAN_BY_LIMIT][i]); |
| 4799 | } |
| 4800 | for (i = 0; i < NR_SCANSTATS; i++) { |
| 4801 | strcpy(string, scanstat_string[i]); |
| 4802 | strcat(string, SCANSTAT_WORD_SYSTEM); |
| 4803 | strcat(string, SCANSTAT_WORD_HIERARCHY); |
| 4804 | cb->fill(cb, string, mem->scanstat.rootstats[SCAN_BY_SYSTEM][i]); |
| 4805 | } |
| 4806 | return 0; |
| 4807 | } |
| 4808 | |
| 4809 | static int mem_cgroup_reset_vmscan_stat(struct cgroup *cgrp, |
| 4810 | unsigned int event) |
| 4811 | { |
| 4812 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4813 | |
| 4814 | spin_lock(&mem->scanstat.lock); |
| 4815 | memset(&mem->scanstat.stats, 0, sizeof(mem->scanstat.stats)); |
| 4816 | memset(&mem->scanstat.rootstats, 0, sizeof(mem->scanstat.rootstats)); |
| 4817 | spin_unlock(&mem->scanstat.lock); |
| 4818 | return 0; |
| 4819 | } |
| 4820 | |
| 4821 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4822 | static struct cftype mem_cgroup_files[] = { |
| 4823 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4824 | .name = "usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4825 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4826 | .read_u64 = mem_cgroup_read, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4827 | .register_event = mem_cgroup_usage_register_event, |
| 4828 | .unregister_event = mem_cgroup_usage_unregister_event, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4829 | }, |
| 4830 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4831 | .name = "max_usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4832 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4833 | .trigger = mem_cgroup_reset, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4834 | .read_u64 = mem_cgroup_read, |
| 4835 | }, |
| 4836 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4837 | .name = "limit_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4838 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 4839 | .write_string = mem_cgroup_write, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4840 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4841 | }, |
| 4842 | { |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 4843 | .name = "soft_limit_in_bytes", |
| 4844 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), |
| 4845 | .write_string = mem_cgroup_write, |
| 4846 | .read_u64 = mem_cgroup_read, |
| 4847 | }, |
| 4848 | { |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4849 | .name = "failcnt", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4850 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4851 | .trigger = mem_cgroup_reset, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4852 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4853 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 4854 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4855 | .name = "stat", |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 4856 | .read_map = mem_control_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4857 | }, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4858 | { |
| 4859 | .name = "force_empty", |
| 4860 | .trigger = mem_cgroup_force_empty_write, |
| 4861 | }, |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4862 | { |
| 4863 | .name = "use_hierarchy", |
| 4864 | .write_u64 = mem_cgroup_hierarchy_write, |
| 4865 | .read_u64 = mem_cgroup_hierarchy_read, |
| 4866 | }, |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4867 | { |
| 4868 | .name = "swappiness", |
| 4869 | .read_u64 = mem_cgroup_swappiness_read, |
| 4870 | .write_u64 = mem_cgroup_swappiness_write, |
| 4871 | }, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4872 | { |
| 4873 | .name = "move_charge_at_immigrate", |
| 4874 | .read_u64 = mem_cgroup_move_charge_read, |
| 4875 | .write_u64 = mem_cgroup_move_charge_write, |
| 4876 | }, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4877 | { |
| 4878 | .name = "oom_control", |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4879 | .read_map = mem_cgroup_oom_control_read, |
| 4880 | .write_u64 = mem_cgroup_oom_control_write, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4881 | .register_event = mem_cgroup_oom_register_event, |
| 4882 | .unregister_event = mem_cgroup_oom_unregister_event, |
| 4883 | .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL), |
| 4884 | }, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4885 | #ifdef CONFIG_NUMA |
| 4886 | { |
| 4887 | .name = "numa_stat", |
| 4888 | .open = mem_control_numa_stat_open, |
KAMEZAWA Hiroyuki | 8957712 | 2011-06-15 15:08:41 -0700 | [diff] [blame] | 4889 | .mode = S_IRUGO, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4890 | }, |
| 4891 | #endif |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 4892 | { |
| 4893 | .name = "vmscan_stat", |
| 4894 | .read_map = mem_cgroup_vmscan_stat_read, |
| 4895 | .trigger = mem_cgroup_reset_vmscan_stat, |
| 4896 | }, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4897 | }; |
| 4898 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4899 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 4900 | static struct cftype memsw_cgroup_files[] = { |
| 4901 | { |
| 4902 | .name = "memsw.usage_in_bytes", |
| 4903 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), |
| 4904 | .read_u64 = mem_cgroup_read, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4905 | .register_event = mem_cgroup_usage_register_event, |
| 4906 | .unregister_event = mem_cgroup_usage_unregister_event, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4907 | }, |
| 4908 | { |
| 4909 | .name = "memsw.max_usage_in_bytes", |
| 4910 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), |
| 4911 | .trigger = mem_cgroup_reset, |
| 4912 | .read_u64 = mem_cgroup_read, |
| 4913 | }, |
| 4914 | { |
| 4915 | .name = "memsw.limit_in_bytes", |
| 4916 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), |
| 4917 | .write_string = mem_cgroup_write, |
| 4918 | .read_u64 = mem_cgroup_read, |
| 4919 | }, |
| 4920 | { |
| 4921 | .name = "memsw.failcnt", |
| 4922 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), |
| 4923 | .trigger = mem_cgroup_reset, |
| 4924 | .read_u64 = mem_cgroup_read, |
| 4925 | }, |
| 4926 | }; |
| 4927 | |
| 4928 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4929 | { |
| 4930 | if (!do_swap_account) |
| 4931 | return 0; |
| 4932 | return cgroup_add_files(cont, ss, memsw_cgroup_files, |
| 4933 | ARRAY_SIZE(memsw_cgroup_files)); |
| 4934 | }; |
| 4935 | #else |
| 4936 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4937 | { |
| 4938 | return 0; |
| 4939 | } |
| 4940 | #endif |
| 4941 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4942 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 4943 | { |
| 4944 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4945 | struct mem_cgroup_per_zone *mz; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 4946 | enum lru_list l; |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 4947 | int zone, tmp = node; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4948 | /* |
| 4949 | * This routine is called against possible nodes. |
| 4950 | * But it's BUG to call kmalloc() against offline node. |
| 4951 | * |
| 4952 | * TODO: this routine can waste much memory for nodes which will |
| 4953 | * never be onlined. It's better to use memory hotplug callback |
| 4954 | * function. |
| 4955 | */ |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 4956 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 4957 | tmp = -1; |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4958 | pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4959 | if (!pn) |
| 4960 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4961 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4962 | mem->info.nodeinfo[node] = pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4963 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 4964 | mz = &pn->zoneinfo[zone]; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 4965 | for_each_lru(l) |
| 4966 | INIT_LIST_HEAD(&mz->lists[l]); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4967 | mz->usage_in_excess = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4968 | mz->on_tree = false; |
| 4969 | mz->mem = mem; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4970 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4971 | return 0; |
| 4972 | } |
| 4973 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4974 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 4975 | { |
| 4976 | kfree(mem->info.nodeinfo[node]); |
| 4977 | } |
| 4978 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4979 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 4980 | { |
| 4981 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4982 | int size = sizeof(struct mem_cgroup); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4983 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4984 | /* Can be very big if MAX_NUMNODES is very big */ |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4985 | if (size < PAGE_SIZE) |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4986 | mem = kzalloc(size, GFP_KERNEL); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4987 | else |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4988 | mem = vzalloc(size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4989 | |
Dan Carpenter | e7bbcdf | 2010-03-23 13:35:12 -0700 | [diff] [blame] | 4990 | if (!mem) |
| 4991 | return NULL; |
| 4992 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4993 | mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu); |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 4994 | if (!mem->stat) |
| 4995 | goto out_free; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 4996 | spin_lock_init(&mem->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4997 | return mem; |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 4998 | |
| 4999 | out_free: |
| 5000 | if (size < PAGE_SIZE) |
| 5001 | kfree(mem); |
| 5002 | else |
| 5003 | vfree(mem); |
| 5004 | return NULL; |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5005 | } |
| 5006 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5007 | /* |
| 5008 | * At destroying mem_cgroup, references from swap_cgroup can remain. |
| 5009 | * (scanning all at force_empty is too costly...) |
| 5010 | * |
| 5011 | * Instead of clearing all references at force_empty, we remember |
| 5012 | * the number of reference from swap_cgroup and free mem_cgroup when |
| 5013 | * it goes down to 0. |
| 5014 | * |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5015 | * Removal of cgroup itself succeeds regardless of refs from swap. |
| 5016 | */ |
| 5017 | |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 5018 | static void __mem_cgroup_free(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5019 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 5020 | int node; |
| 5021 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5022 | mem_cgroup_remove_from_trees(mem); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5023 | free_css_id(&mem_cgroup_subsys, &mem->css); |
| 5024 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 5025 | for_each_node_state(node, N_POSSIBLE) |
| 5026 | free_mem_cgroup_per_zone_info(mem, node); |
| 5027 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 5028 | free_percpu(mem->stat); |
| 5029 | if (sizeof(struct mem_cgroup) < PAGE_SIZE) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5030 | kfree(mem); |
| 5031 | else |
| 5032 | vfree(mem); |
| 5033 | } |
| 5034 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5035 | static void mem_cgroup_get(struct mem_cgroup *mem) |
| 5036 | { |
| 5037 | atomic_inc(&mem->refcnt); |
| 5038 | } |
| 5039 | |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5040 | static void __mem_cgroup_put(struct mem_cgroup *mem, int count) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5041 | { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5042 | if (atomic_sub_and_test(count, &mem->refcnt)) { |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5043 | struct mem_cgroup *parent = parent_mem_cgroup(mem); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 5044 | __mem_cgroup_free(mem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5045 | if (parent) |
| 5046 | mem_cgroup_put(parent); |
| 5047 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5048 | } |
| 5049 | |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5050 | static void mem_cgroup_put(struct mem_cgroup *mem) |
| 5051 | { |
| 5052 | __mem_cgroup_put(mem, 1); |
| 5053 | } |
| 5054 | |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5055 | /* |
| 5056 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. |
| 5057 | */ |
| 5058 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem) |
| 5059 | { |
| 5060 | if (!mem->res.parent) |
| 5061 | return NULL; |
| 5062 | return mem_cgroup_from_res_counter(mem->res.parent, res); |
| 5063 | } |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5064 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5065 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 5066 | static void __init enable_swap_cgroup(void) |
| 5067 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 5068 | if (!mem_cgroup_disabled() && really_do_swap_account) |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5069 | do_swap_account = 1; |
| 5070 | } |
| 5071 | #else |
| 5072 | static void __init enable_swap_cgroup(void) |
| 5073 | { |
| 5074 | } |
| 5075 | #endif |
| 5076 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5077 | static int mem_cgroup_soft_limit_tree_init(void) |
| 5078 | { |
| 5079 | struct mem_cgroup_tree_per_node *rtpn; |
| 5080 | struct mem_cgroup_tree_per_zone *rtpz; |
| 5081 | int tmp, node, zone; |
| 5082 | |
| 5083 | for_each_node_state(node, N_POSSIBLE) { |
| 5084 | tmp = node; |
| 5085 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 5086 | tmp = -1; |
| 5087 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp); |
| 5088 | if (!rtpn) |
| 5089 | return 1; |
| 5090 | |
| 5091 | soft_limit_tree.rb_tree_per_node[node] = rtpn; |
| 5092 | |
| 5093 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 5094 | rtpz = &rtpn->rb_tree_per_zone[zone]; |
| 5095 | rtpz->rb_root = RB_ROOT; |
| 5096 | spin_lock_init(&rtpz->lock); |
| 5097 | } |
| 5098 | } |
| 5099 | return 0; |
| 5100 | } |
| 5101 | |
Li Zefan | 0eb253e | 2009-01-15 13:51:25 -0800 | [diff] [blame] | 5102 | static struct cgroup_subsys_state * __ref |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5103 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 5104 | { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5105 | struct mem_cgroup *mem, *parent; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5106 | long error = -ENOMEM; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5107 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5108 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 5109 | mem = mem_cgroup_alloc(); |
| 5110 | if (!mem) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5111 | return ERR_PTR(error); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 5112 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5113 | for_each_node_state(node, N_POSSIBLE) |
| 5114 | if (alloc_mem_cgroup_per_zone_info(mem, node)) |
| 5115 | goto free_out; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5116 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5117 | /* root ? */ |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5118 | if (cont->parent == NULL) { |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 5119 | int cpu; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5120 | enable_swap_cgroup(); |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5121 | parent = NULL; |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 5122 | root_mem_cgroup = mem; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5123 | if (mem_cgroup_soft_limit_tree_init()) |
| 5124 | goto free_out; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 5125 | for_each_possible_cpu(cpu) { |
| 5126 | struct memcg_stock_pcp *stock = |
| 5127 | &per_cpu(memcg_stock, cpu); |
| 5128 | INIT_WORK(&stock->work, drain_local_stock); |
| 5129 | } |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 5130 | hotcpu_notifier(memcg_cpu_hotplug_callback, 0); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5131 | } else { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5132 | parent = mem_cgroup_from_cont(cont->parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5133 | mem->use_hierarchy = parent->use_hierarchy; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 5134 | mem->oom_kill_disable = parent->oom_kill_disable; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5135 | } |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5136 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5137 | if (parent && parent->use_hierarchy) { |
| 5138 | res_counter_init(&mem->res, &parent->res); |
| 5139 | res_counter_init(&mem->memsw, &parent->memsw); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5140 | /* |
| 5141 | * We increment refcnt of the parent to ensure that we can |
| 5142 | * safely access it on res_counter_charge/uncharge. |
| 5143 | * This refcnt will be decremented when freeing this |
| 5144 | * mem_cgroup(see mem_cgroup_put). |
| 5145 | */ |
| 5146 | mem_cgroup_get(parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5147 | } else { |
| 5148 | res_counter_init(&mem->res, NULL); |
| 5149 | res_counter_init(&mem->memsw, NULL); |
| 5150 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5151 | mem->last_scanned_child = 0; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 5152 | mem->last_scanned_node = MAX_NUMNODES; |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5153 | INIT_LIST_HEAD(&mem->oom_notify); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 5154 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5155 | if (parent) |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 5156 | mem->swappiness = mem_cgroup_swappiness(parent); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 5157 | atomic_set(&mem->refcnt, 1); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5158 | mem->move_charge_at_immigrate = 0; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5159 | mutex_init(&mem->thresholds_lock); |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame] | 5160 | spin_lock_init(&mem->scanstat.lock); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5161 | return &mem->css; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5162 | free_out: |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 5163 | __mem_cgroup_free(mem); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 5164 | root_mem_cgroup = NULL; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5165 | return ERR_PTR(error); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5166 | } |
| 5167 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 5168 | static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5169 | struct cgroup *cont) |
| 5170 | { |
| 5171 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 5172 | |
| 5173 | return mem_cgroup_force_empty(mem, false); |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5174 | } |
| 5175 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5176 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 5177 | struct cgroup *cont) |
| 5178 | { |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 5179 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 5180 | |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 5181 | mem_cgroup_put(mem); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 5185 | struct cgroup *cont) |
| 5186 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5187 | int ret; |
| 5188 | |
| 5189 | ret = cgroup_add_files(cont, ss, mem_cgroup_files, |
| 5190 | ARRAY_SIZE(mem_cgroup_files)); |
| 5191 | |
| 5192 | if (!ret) |
| 5193 | ret = register_memsw_files(cont, ss); |
| 5194 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5195 | } |
| 5196 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5197 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5198 | /* Handlers for move charge at task migration. */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5199 | #define PRECHARGE_COUNT_AT_ONCE 256 |
| 5200 | static int mem_cgroup_do_precharge(unsigned long count) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5201 | { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5202 | int ret = 0; |
| 5203 | int batch_count = PRECHARGE_COUNT_AT_ONCE; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5204 | struct mem_cgroup *mem = mc.to; |
| 5205 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5206 | if (mem_cgroup_is_root(mem)) { |
| 5207 | mc.precharge += count; |
| 5208 | /* we don't need css_get for root */ |
| 5209 | return ret; |
| 5210 | } |
| 5211 | /* try to charge at once */ |
| 5212 | if (count > 1) { |
| 5213 | struct res_counter *dummy; |
| 5214 | /* |
| 5215 | * "mem" cannot be under rmdir() because we've already checked |
| 5216 | * by cgroup_lock_live_cgroup() that it is not removed and we |
| 5217 | * are still under the same cgroup_mutex. So we can postpone |
| 5218 | * css_get(). |
| 5219 | */ |
| 5220 | if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy)) |
| 5221 | goto one_by_one; |
| 5222 | if (do_swap_account && res_counter_charge(&mem->memsw, |
| 5223 | PAGE_SIZE * count, &dummy)) { |
| 5224 | res_counter_uncharge(&mem->res, PAGE_SIZE * count); |
| 5225 | goto one_by_one; |
| 5226 | } |
| 5227 | mc.precharge += count; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5228 | return ret; |
| 5229 | } |
| 5230 | one_by_one: |
| 5231 | /* fall back to one by one charge */ |
| 5232 | while (count--) { |
| 5233 | if (signal_pending(current)) { |
| 5234 | ret = -EINTR; |
| 5235 | break; |
| 5236 | } |
| 5237 | if (!batch_count--) { |
| 5238 | batch_count = PRECHARGE_COUNT_AT_ONCE; |
| 5239 | cond_resched(); |
| 5240 | } |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 5241 | ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, 1, &mem, false); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5242 | if (ret || !mem) |
| 5243 | /* mem_cgroup_clear_mc() will do uncharge later */ |
| 5244 | return -ENOMEM; |
| 5245 | mc.precharge++; |
| 5246 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5247 | return ret; |
| 5248 | } |
| 5249 | |
| 5250 | /** |
| 5251 | * is_target_pte_for_mc - check a pte whether it is valid for move charge |
| 5252 | * @vma: the vma the pte to be checked belongs |
| 5253 | * @addr: the address corresponding to the pte to be checked |
| 5254 | * @ptent: the pte to be checked |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5255 | * @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] | 5256 | * |
| 5257 | * Returns |
| 5258 | * 0(MC_TARGET_NONE): if the pte is not a target for move charge. |
| 5259 | * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for |
| 5260 | * move charge. if @target is not NULL, the page is stored in target->page |
| 5261 | * with extra refcnt got(Callers should handle it). |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5262 | * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a |
| 5263 | * target for charge migration. if @target is not NULL, the entry is stored |
| 5264 | * in target->ent. |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5265 | * |
| 5266 | * Called with pte lock held. |
| 5267 | */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5268 | union mc_target { |
| 5269 | struct page *page; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5270 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5271 | }; |
| 5272 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5273 | enum mc_target_type { |
| 5274 | MC_TARGET_NONE, /* not used */ |
| 5275 | MC_TARGET_PAGE, |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5276 | MC_TARGET_SWAP, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5277 | }; |
| 5278 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5279 | static struct page *mc_handle_present_pte(struct vm_area_struct *vma, |
| 5280 | unsigned long addr, pte_t ptent) |
| 5281 | { |
| 5282 | struct page *page = vm_normal_page(vma, addr, ptent); |
| 5283 | |
| 5284 | if (!page || !page_mapped(page)) |
| 5285 | return NULL; |
| 5286 | if (PageAnon(page)) { |
| 5287 | /* we don't move shared anon */ |
| 5288 | if (!move_anon() || page_mapcount(page) > 2) |
| 5289 | return NULL; |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5290 | } else if (!move_file()) |
| 5291 | /* we ignore mapcount for file pages */ |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5292 | return NULL; |
| 5293 | if (!get_page_unless_zero(page)) |
| 5294 | return NULL; |
| 5295 | |
| 5296 | return page; |
| 5297 | } |
| 5298 | |
| 5299 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
| 5300 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5301 | { |
| 5302 | int usage_count; |
| 5303 | struct page *page = NULL; |
| 5304 | swp_entry_t ent = pte_to_swp_entry(ptent); |
| 5305 | |
| 5306 | if (!move_anon() || non_swap_entry(ent)) |
| 5307 | return NULL; |
| 5308 | usage_count = mem_cgroup_count_swap_user(ent, &page); |
| 5309 | if (usage_count > 1) { /* we don't move shared anon */ |
| 5310 | if (page) |
| 5311 | put_page(page); |
| 5312 | return NULL; |
| 5313 | } |
| 5314 | if (do_swap_account) |
| 5315 | entry->val = ent.val; |
| 5316 | |
| 5317 | return page; |
| 5318 | } |
| 5319 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5320 | static struct page *mc_handle_file_pte(struct vm_area_struct *vma, |
| 5321 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5322 | { |
| 5323 | struct page *page = NULL; |
| 5324 | struct inode *inode; |
| 5325 | struct address_space *mapping; |
| 5326 | pgoff_t pgoff; |
| 5327 | |
| 5328 | if (!vma->vm_file) /* anonymous vma */ |
| 5329 | return NULL; |
| 5330 | if (!move_file()) |
| 5331 | return NULL; |
| 5332 | |
| 5333 | inode = vma->vm_file->f_path.dentry->d_inode; |
| 5334 | mapping = vma->vm_file->f_mapping; |
| 5335 | if (pte_none(ptent)) |
| 5336 | pgoff = linear_page_index(vma, addr); |
| 5337 | else /* pte_file(ptent) is true */ |
| 5338 | pgoff = pte_to_pgoff(ptent); |
| 5339 | |
| 5340 | /* page is moved even if it's not RSS of this task(page-faulted). */ |
| 5341 | if (!mapping_cap_swap_backed(mapping)) { /* normal file */ |
| 5342 | page = find_get_page(mapping, pgoff); |
| 5343 | } else { /* shmem/tmpfs file. we should take account of swap too. */ |
| 5344 | swp_entry_t ent; |
| 5345 | mem_cgroup_get_shmem_target(inode, pgoff, &page, &ent); |
| 5346 | if (do_swap_account) |
| 5347 | entry->val = ent.val; |
| 5348 | } |
| 5349 | |
| 5350 | return page; |
| 5351 | } |
| 5352 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5353 | static int is_target_pte_for_mc(struct vm_area_struct *vma, |
| 5354 | unsigned long addr, pte_t ptent, union mc_target *target) |
| 5355 | { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5356 | struct page *page = NULL; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5357 | struct page_cgroup *pc; |
| 5358 | int ret = 0; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5359 | swp_entry_t ent = { .val = 0 }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5360 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5361 | if (pte_present(ptent)) |
| 5362 | page = mc_handle_present_pte(vma, addr, ptent); |
| 5363 | else if (is_swap_pte(ptent)) |
| 5364 | page = mc_handle_swap_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5365 | else if (pte_none(ptent) || pte_file(ptent)) |
| 5366 | page = mc_handle_file_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5367 | |
| 5368 | if (!page && !ent.val) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5369 | return 0; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5370 | if (page) { |
| 5371 | pc = lookup_page_cgroup(page); |
| 5372 | /* |
| 5373 | * Do only loose check w/o page_cgroup lock. |
| 5374 | * mem_cgroup_move_account() checks the pc is valid or not under |
| 5375 | * the lock. |
| 5376 | */ |
| 5377 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { |
| 5378 | ret = MC_TARGET_PAGE; |
| 5379 | if (target) |
| 5380 | target->page = page; |
| 5381 | } |
| 5382 | if (!ret || !target) |
| 5383 | put_page(page); |
| 5384 | } |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5385 | /* There is a swap entry and a page doesn't exist or isn't charged */ |
| 5386 | if (ent.val && !ret && |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5387 | css_id(&mc.from->css) == lookup_swap_cgroup(ent)) { |
| 5388 | ret = MC_TARGET_SWAP; |
| 5389 | if (target) |
| 5390 | target->ent = ent; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5391 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5392 | return ret; |
| 5393 | } |
| 5394 | |
| 5395 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, |
| 5396 | unsigned long addr, unsigned long end, |
| 5397 | struct mm_walk *walk) |
| 5398 | { |
| 5399 | struct vm_area_struct *vma = walk->private; |
| 5400 | pte_t *pte; |
| 5401 | spinlock_t *ptl; |
| 5402 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 5403 | split_huge_page_pmd(walk->mm, pmd); |
| 5404 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5405 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5406 | for (; addr != end; pte++, addr += PAGE_SIZE) |
| 5407 | if (is_target_pte_for_mc(vma, addr, *pte, NULL)) |
| 5408 | mc.precharge++; /* increment precharge temporarily */ |
| 5409 | pte_unmap_unlock(pte - 1, ptl); |
| 5410 | cond_resched(); |
| 5411 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5412 | return 0; |
| 5413 | } |
| 5414 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5415 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
| 5416 | { |
| 5417 | unsigned long precharge; |
| 5418 | struct vm_area_struct *vma; |
| 5419 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5420 | down_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5421 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5422 | struct mm_walk mem_cgroup_count_precharge_walk = { |
| 5423 | .pmd_entry = mem_cgroup_count_precharge_pte_range, |
| 5424 | .mm = mm, |
| 5425 | .private = vma, |
| 5426 | }; |
| 5427 | if (is_vm_hugetlb_page(vma)) |
| 5428 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5429 | walk_page_range(vma->vm_start, vma->vm_end, |
| 5430 | &mem_cgroup_count_precharge_walk); |
| 5431 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5432 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5433 | |
| 5434 | precharge = mc.precharge; |
| 5435 | mc.precharge = 0; |
| 5436 | |
| 5437 | return precharge; |
| 5438 | } |
| 5439 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5440 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
| 5441 | { |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5442 | unsigned long precharge = mem_cgroup_count_precharge(mm); |
| 5443 | |
| 5444 | VM_BUG_ON(mc.moving_task); |
| 5445 | mc.moving_task = current; |
| 5446 | return mem_cgroup_do_precharge(precharge); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5447 | } |
| 5448 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5449 | /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */ |
| 5450 | static void __mem_cgroup_clear_mc(void) |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5451 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5452 | struct mem_cgroup *from = mc.from; |
| 5453 | struct mem_cgroup *to = mc.to; |
| 5454 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5455 | /* we must uncharge all the leftover precharges from mc.to */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5456 | if (mc.precharge) { |
| 5457 | __mem_cgroup_cancel_charge(mc.to, mc.precharge); |
| 5458 | mc.precharge = 0; |
| 5459 | } |
| 5460 | /* |
| 5461 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so |
| 5462 | * we must uncharge here. |
| 5463 | */ |
| 5464 | if (mc.moved_charge) { |
| 5465 | __mem_cgroup_cancel_charge(mc.from, mc.moved_charge); |
| 5466 | mc.moved_charge = 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5467 | } |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5468 | /* we must fixup refcnts and charges */ |
| 5469 | if (mc.moved_swap) { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5470 | /* uncharge swap account from the old cgroup */ |
| 5471 | if (!mem_cgroup_is_root(mc.from)) |
| 5472 | res_counter_uncharge(&mc.from->memsw, |
| 5473 | PAGE_SIZE * mc.moved_swap); |
| 5474 | __mem_cgroup_put(mc.from, mc.moved_swap); |
| 5475 | |
| 5476 | if (!mem_cgroup_is_root(mc.to)) { |
| 5477 | /* |
| 5478 | * we charged both to->res and to->memsw, so we should |
| 5479 | * uncharge to->res. |
| 5480 | */ |
| 5481 | res_counter_uncharge(&mc.to->res, |
| 5482 | PAGE_SIZE * mc.moved_swap); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5483 | } |
| 5484 | /* we've already done mem_cgroup_get(mc.to) */ |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5485 | mc.moved_swap = 0; |
| 5486 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5487 | memcg_oom_recover(from); |
| 5488 | memcg_oom_recover(to); |
| 5489 | wake_up_all(&mc.waitq); |
| 5490 | } |
| 5491 | |
| 5492 | static void mem_cgroup_clear_mc(void) |
| 5493 | { |
| 5494 | struct mem_cgroup *from = mc.from; |
| 5495 | |
| 5496 | /* |
| 5497 | * we must clear moving_task before waking up waiters at the end of |
| 5498 | * task migration. |
| 5499 | */ |
| 5500 | mc.moving_task = NULL; |
| 5501 | __mem_cgroup_clear_mc(); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5502 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5503 | mc.from = NULL; |
| 5504 | mc.to = NULL; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5505 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5506 | mem_cgroup_end_move(from); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5507 | } |
| 5508 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5509 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 5510 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5511 | struct task_struct *p) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5512 | { |
| 5513 | int ret = 0; |
| 5514 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup); |
| 5515 | |
| 5516 | if (mem->move_charge_at_immigrate) { |
| 5517 | struct mm_struct *mm; |
| 5518 | struct mem_cgroup *from = mem_cgroup_from_task(p); |
| 5519 | |
| 5520 | VM_BUG_ON(from == mem); |
| 5521 | |
| 5522 | mm = get_task_mm(p); |
| 5523 | if (!mm) |
| 5524 | return 0; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5525 | /* We move charges only when we move a owner of the mm */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5526 | if (mm->owner == p) { |
| 5527 | VM_BUG_ON(mc.from); |
| 5528 | VM_BUG_ON(mc.to); |
| 5529 | VM_BUG_ON(mc.precharge); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5530 | VM_BUG_ON(mc.moved_charge); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5531 | VM_BUG_ON(mc.moved_swap); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5532 | mem_cgroup_start_move(from); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5533 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5534 | mc.from = from; |
| 5535 | mc.to = mem; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5536 | spin_unlock(&mc.lock); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5537 | /* We set mc.moving_task later */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5538 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5539 | ret = mem_cgroup_precharge_mc(mm); |
| 5540 | if (ret) |
| 5541 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5542 | } |
| 5543 | mmput(mm); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5544 | } |
| 5545 | return ret; |
| 5546 | } |
| 5547 | |
| 5548 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 5549 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5550 | struct task_struct *p) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5551 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5552 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5553 | } |
| 5554 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5555 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
| 5556 | unsigned long addr, unsigned long end, |
| 5557 | struct mm_walk *walk) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5558 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5559 | int ret = 0; |
| 5560 | struct vm_area_struct *vma = walk->private; |
| 5561 | pte_t *pte; |
| 5562 | spinlock_t *ptl; |
| 5563 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 5564 | split_huge_page_pmd(walk->mm, pmd); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5565 | retry: |
| 5566 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5567 | for (; addr != end; addr += PAGE_SIZE) { |
| 5568 | pte_t ptent = *(pte++); |
| 5569 | union mc_target target; |
| 5570 | int type; |
| 5571 | struct page *page; |
| 5572 | struct page_cgroup *pc; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5573 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5574 | |
| 5575 | if (!mc.precharge) |
| 5576 | break; |
| 5577 | |
| 5578 | type = is_target_pte_for_mc(vma, addr, ptent, &target); |
| 5579 | switch (type) { |
| 5580 | case MC_TARGET_PAGE: |
| 5581 | page = target.page; |
| 5582 | if (isolate_lru_page(page)) |
| 5583 | goto put; |
| 5584 | pc = lookup_page_cgroup(page); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 5585 | if (!mem_cgroup_move_account(page, 1, pc, |
| 5586 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5587 | mc.precharge--; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5588 | /* we uncharge from mc.from later. */ |
| 5589 | mc.moved_charge++; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5590 | } |
| 5591 | putback_lru_page(page); |
| 5592 | put: /* is_target_pte_for_mc() gets the page */ |
| 5593 | put_page(page); |
| 5594 | break; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5595 | case MC_TARGET_SWAP: |
| 5596 | ent = target.ent; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5597 | if (!mem_cgroup_move_swap_account(ent, |
| 5598 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5599 | mc.precharge--; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5600 | /* we fixup refcnts and charges later. */ |
| 5601 | mc.moved_swap++; |
| 5602 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5603 | break; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5604 | default: |
| 5605 | break; |
| 5606 | } |
| 5607 | } |
| 5608 | pte_unmap_unlock(pte - 1, ptl); |
| 5609 | cond_resched(); |
| 5610 | |
| 5611 | if (addr != end) { |
| 5612 | /* |
| 5613 | * We have consumed all precharges we got in can_attach(). |
| 5614 | * We try charge one by one, but don't do any additional |
| 5615 | * charges to mc.to if we have failed in charge once in attach() |
| 5616 | * phase. |
| 5617 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5618 | ret = mem_cgroup_do_precharge(1); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5619 | if (!ret) |
| 5620 | goto retry; |
| 5621 | } |
| 5622 | |
| 5623 | return ret; |
| 5624 | } |
| 5625 | |
| 5626 | static void mem_cgroup_move_charge(struct mm_struct *mm) |
| 5627 | { |
| 5628 | struct vm_area_struct *vma; |
| 5629 | |
| 5630 | lru_add_drain_all(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5631 | retry: |
| 5632 | if (unlikely(!down_read_trylock(&mm->mmap_sem))) { |
| 5633 | /* |
| 5634 | * Someone who are holding the mmap_sem might be waiting in |
| 5635 | * waitq. So we cancel all extra charges, wake up all waiters, |
| 5636 | * and retry. Because we cancel precharges, we might not be able |
| 5637 | * to move enough charges, but moving charge is a best-effort |
| 5638 | * feature anyway, so it wouldn't be a big problem. |
| 5639 | */ |
| 5640 | __mem_cgroup_clear_mc(); |
| 5641 | cond_resched(); |
| 5642 | goto retry; |
| 5643 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5644 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5645 | int ret; |
| 5646 | struct mm_walk mem_cgroup_move_charge_walk = { |
| 5647 | .pmd_entry = mem_cgroup_move_charge_pte_range, |
| 5648 | .mm = mm, |
| 5649 | .private = vma, |
| 5650 | }; |
| 5651 | if (is_vm_hugetlb_page(vma)) |
| 5652 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5653 | ret = walk_page_range(vma->vm_start, vma->vm_end, |
| 5654 | &mem_cgroup_move_charge_walk); |
| 5655 | if (ret) |
| 5656 | /* |
| 5657 | * means we have consumed all precharges and failed in |
| 5658 | * doing additional charge. Just abandon here. |
| 5659 | */ |
| 5660 | break; |
| 5661 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5662 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5663 | } |
| 5664 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5665 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 5666 | struct cgroup *cont, |
| 5667 | struct cgroup *old_cont, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5668 | struct task_struct *p) |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5669 | { |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5670 | struct mm_struct *mm = get_task_mm(p); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5671 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5672 | if (mm) { |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5673 | if (mc.to) |
| 5674 | mem_cgroup_move_charge(mm); |
| 5675 | put_swap_token(mm); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5676 | mmput(mm); |
| 5677 | } |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5678 | if (mc.to) |
| 5679 | mem_cgroup_clear_mc(); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5680 | } |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5681 | #else /* !CONFIG_MMU */ |
| 5682 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 5683 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5684 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5685 | { |
| 5686 | return 0; |
| 5687 | } |
| 5688 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 5689 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5690 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5691 | { |
| 5692 | } |
| 5693 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 5694 | struct cgroup *cont, |
| 5695 | struct cgroup *old_cont, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5696 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5697 | { |
| 5698 | } |
| 5699 | #endif |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5700 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5701 | struct cgroup_subsys mem_cgroup_subsys = { |
| 5702 | .name = "memory", |
| 5703 | .subsys_id = mem_cgroup_subsys_id, |
| 5704 | .create = mem_cgroup_create, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5705 | .pre_destroy = mem_cgroup_pre_destroy, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5706 | .destroy = mem_cgroup_destroy, |
| 5707 | .populate = mem_cgroup_populate, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5708 | .can_attach = mem_cgroup_can_attach, |
| 5709 | .cancel_attach = mem_cgroup_cancel_attach, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5710 | .attach = mem_cgroup_move_task, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5711 | .early_init = 0, |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5712 | .use_id = 1, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5713 | }; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5714 | |
| 5715 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5716 | static int __init enable_swap_account(char *s) |
| 5717 | { |
| 5718 | /* consider enabled if no parameter or 1 is given */ |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5719 | if (!strcmp(s, "1")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5720 | really_do_swap_account = 1; |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5721 | else if (!strcmp(s, "0")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5722 | really_do_swap_account = 0; |
| 5723 | return 1; |
| 5724 | } |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5725 | __setup("swapaccount=", enable_swap_account); |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5726 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5727 | #endif |