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 | 8a9478ca | 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 | |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1111 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
| 1112 | { |
| 1113 | int ret; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1114 | struct mem_cgroup *curr = NULL; |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1115 | struct task_struct *p; |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1116 | |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1117 | p = find_lock_task_mm(task); |
| 1118 | if (!p) |
| 1119 | return 0; |
| 1120 | curr = try_get_mem_cgroup_from_mm(p->mm); |
| 1121 | task_unlock(p); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1122 | if (!curr) |
| 1123 | return 0; |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1124 | /* |
| 1125 | * We should check use_hierarchy of "mem" not "curr". Because checking |
| 1126 | * use_hierarchy of "curr" here make this function true if hierarchy is |
| 1127 | * enabled in "curr" and "curr" is a child of "mem" in *cgroup* |
| 1128 | * hierarchy(even if use_hierarchy is disabled in "mem"). |
| 1129 | */ |
| 1130 | if (mem->use_hierarchy) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1131 | ret = css_is_ancestor(&curr->css, &mem->css); |
| 1132 | else |
| 1133 | ret = (curr == mem); |
| 1134 | css_put(&curr->css); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1135 | return ret; |
| 1136 | } |
| 1137 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1138 | 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] | 1139 | { |
| 1140 | unsigned long active; |
| 1141 | unsigned long inactive; |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1142 | unsigned long gb; |
| 1143 | unsigned long inactive_ratio; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1144 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1145 | inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON)); |
| 1146 | active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON)); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1147 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1148 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
| 1149 | if (gb) |
| 1150 | inactive_ratio = int_sqrt(10 * gb); |
| 1151 | else |
| 1152 | inactive_ratio = 1; |
| 1153 | |
| 1154 | if (present_pages) { |
| 1155 | present_pages[0] = inactive; |
| 1156 | present_pages[1] = active; |
| 1157 | } |
| 1158 | |
| 1159 | return inactive_ratio; |
| 1160 | } |
| 1161 | |
| 1162 | int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg) |
| 1163 | { |
| 1164 | unsigned long active; |
| 1165 | unsigned long inactive; |
| 1166 | unsigned long present_pages[2]; |
| 1167 | unsigned long inactive_ratio; |
| 1168 | |
| 1169 | inactive_ratio = calc_inactive_ratio(memcg, present_pages); |
| 1170 | |
| 1171 | inactive = present_pages[0]; |
| 1172 | active = present_pages[1]; |
| 1173 | |
| 1174 | if (inactive * inactive_ratio < active) |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1175 | return 1; |
| 1176 | |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
Rik van Riel | 56e49d2 | 2009-06-16 15:32:28 -0700 | [diff] [blame] | 1180 | int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg) |
| 1181 | { |
| 1182 | unsigned long active; |
| 1183 | unsigned long inactive; |
| 1184 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1185 | inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE)); |
| 1186 | active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE)); |
Rik van Riel | 56e49d2 | 2009-06-16 15:32:28 -0700 | [diff] [blame] | 1187 | |
| 1188 | return (active > inactive); |
| 1189 | } |
| 1190 | |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1191 | struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg, |
| 1192 | struct zone *zone) |
| 1193 | { |
KOSAKI Motohiro | 13d7e3a | 2010-08-10 18:03:06 -0700 | [diff] [blame] | 1194 | int nid = zone_to_nid(zone); |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1195 | int zid = zone_idx(zone); |
| 1196 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 1197 | |
| 1198 | return &mz->reclaim_stat; |
| 1199 | } |
| 1200 | |
| 1201 | struct zone_reclaim_stat * |
| 1202 | mem_cgroup_get_reclaim_stat_from_page(struct page *page) |
| 1203 | { |
| 1204 | struct page_cgroup *pc; |
| 1205 | struct mem_cgroup_per_zone *mz; |
| 1206 | |
| 1207 | if (mem_cgroup_disabled()) |
| 1208 | return NULL; |
| 1209 | |
| 1210 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 1211 | if (!PageCgroupUsed(pc)) |
| 1212 | return NULL; |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 1213 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 1214 | smp_rmb(); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1215 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1216 | return &mz->reclaim_stat; |
| 1217 | } |
| 1218 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1219 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 1220 | struct list_head *dst, |
| 1221 | unsigned long *scanned, int order, |
| 1222 | int mode, struct zone *z, |
| 1223 | struct mem_cgroup *mem_cont, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1224 | int active, int file) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1225 | { |
| 1226 | unsigned long nr_taken = 0; |
| 1227 | struct page *page; |
| 1228 | unsigned long scan; |
| 1229 | LIST_HEAD(pc_list); |
| 1230 | struct list_head *src; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1231 | struct page_cgroup *pc, *tmp; |
KOSAKI Motohiro | 13d7e3a | 2010-08-10 18:03:06 -0700 | [diff] [blame] | 1232 | int nid = zone_to_nid(z); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1233 | int zid = zone_idx(z); |
| 1234 | struct mem_cgroup_per_zone *mz; |
Johannes Weiner | b7c46d1 | 2009-09-21 17:02:56 -0700 | [diff] [blame] | 1235 | int lru = LRU_FILE * file + active; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1236 | int ret; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1237 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 1238 | BUG_ON(!mem_cont); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1239 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1240 | src = &mz->lists[lru]; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1241 | |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1242 | scan = 0; |
| 1243 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1244 | if (scan >= nr_to_scan) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1245 | break; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1246 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1247 | if (unlikely(!PageCgroupUsed(pc))) |
| 1248 | continue; |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 1249 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 1250 | page = lookup_cgroup_page(pc); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 1251 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1252 | if (unlikely(!PageLRU(page))) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1253 | continue; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1254 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1255 | scan++; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1256 | ret = __isolate_lru_page(page, mode, file); |
| 1257 | switch (ret) { |
| 1258 | case 0: |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1259 | list_move(&page->lru, dst); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1260 | mem_cgroup_del_lru(page); |
Rik van Riel | 2c888cf | 2011-01-13 15:47:13 -0800 | [diff] [blame] | 1261 | nr_taken += hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1262 | break; |
| 1263 | case -EBUSY: |
| 1264 | /* we don't affect global LRU but rotate in our LRU */ |
| 1265 | mem_cgroup_rotate_lru_list(page, page_lru(page)); |
| 1266 | break; |
| 1267 | default: |
| 1268 | break; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1272 | *scanned = scan; |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 1273 | |
| 1274 | trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken, |
| 1275 | 0, 0, 0, mode); |
| 1276 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1277 | return nr_taken; |
| 1278 | } |
| 1279 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1280 | #define mem_cgroup_from_res_counter(counter, member) \ |
| 1281 | container_of(counter, struct mem_cgroup, member) |
| 1282 | |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1283 | /** |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1284 | * mem_cgroup_margin - calculate chargeable space of a memory cgroup |
| 1285 | * @mem: the memory cgroup |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1286 | * |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1287 | * Returns the maximum amount of memory @mem can be charged with, in |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1288 | * pages. |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1289 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1290 | static unsigned long mem_cgroup_margin(struct mem_cgroup *mem) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1291 | { |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1292 | unsigned long long margin; |
| 1293 | |
| 1294 | margin = res_counter_margin(&mem->res); |
| 1295 | if (do_swap_account) |
| 1296 | margin = min(margin, res_counter_margin(&mem->memsw)); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1297 | return margin >> PAGE_SHIFT; |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1298 | } |
| 1299 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 1300 | int mem_cgroup_swappiness(struct mem_cgroup *memcg) |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1301 | { |
| 1302 | struct cgroup *cgrp = memcg->css.cgroup; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1303 | |
| 1304 | /* root ? */ |
| 1305 | if (cgrp->parent == NULL) |
| 1306 | return vm_swappiness; |
| 1307 | |
Johannes Weiner | bf1ff26 | 2011-03-23 16:42:32 -0700 | [diff] [blame] | 1308 | return memcg->swappiness; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1311 | static void mem_cgroup_start_move(struct mem_cgroup *mem) |
| 1312 | { |
| 1313 | int cpu; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1314 | |
| 1315 | get_online_cpus(); |
| 1316 | spin_lock(&mem->pcp_counter_lock); |
| 1317 | for_each_online_cpu(cpu) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1318 | per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1319 | mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1; |
| 1320 | spin_unlock(&mem->pcp_counter_lock); |
| 1321 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1322 | |
| 1323 | synchronize_rcu(); |
| 1324 | } |
| 1325 | |
| 1326 | static void mem_cgroup_end_move(struct mem_cgroup *mem) |
| 1327 | { |
| 1328 | int cpu; |
| 1329 | |
| 1330 | if (!mem) |
| 1331 | return; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1332 | get_online_cpus(); |
| 1333 | spin_lock(&mem->pcp_counter_lock); |
| 1334 | for_each_online_cpu(cpu) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1335 | per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1336 | mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1; |
| 1337 | spin_unlock(&mem->pcp_counter_lock); |
| 1338 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1339 | } |
| 1340 | /* |
| 1341 | * 2 routines for checking "mem" is under move_account() or not. |
| 1342 | * |
| 1343 | * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used |
| 1344 | * for avoiding race in accounting. If true, |
| 1345 | * pc->mem_cgroup may be overwritten. |
| 1346 | * |
| 1347 | * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or |
| 1348 | * under hierarchy of moving cgroups. This is for |
| 1349 | * waiting at hith-memory prressure caused by "move". |
| 1350 | */ |
| 1351 | |
| 1352 | static bool mem_cgroup_stealed(struct mem_cgroup *mem) |
| 1353 | { |
| 1354 | VM_BUG_ON(!rcu_read_lock_held()); |
| 1355 | return this_cpu_read(mem->stat->count[MEM_CGROUP_ON_MOVE]) > 0; |
| 1356 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1357 | |
| 1358 | static bool mem_cgroup_under_move(struct mem_cgroup *mem) |
| 1359 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1360 | struct mem_cgroup *from; |
| 1361 | struct mem_cgroup *to; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1362 | bool ret = false; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1363 | /* |
| 1364 | * Unlike task_move routines, we access mc.to, mc.from not under |
| 1365 | * mutual exclusion by cgroup_mutex. Here, we take spinlock instead. |
| 1366 | */ |
| 1367 | spin_lock(&mc.lock); |
| 1368 | from = mc.from; |
| 1369 | to = mc.to; |
| 1370 | if (!from) |
| 1371 | goto unlock; |
| 1372 | if (from == mem || to == mem |
| 1373 | || (mem->use_hierarchy && css_is_ancestor(&from->css, &mem->css)) |
| 1374 | || (mem->use_hierarchy && css_is_ancestor(&to->css, &mem->css))) |
| 1375 | ret = true; |
| 1376 | unlock: |
| 1377 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1378 | return ret; |
| 1379 | } |
| 1380 | |
| 1381 | static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem) |
| 1382 | { |
| 1383 | if (mc.moving_task && current != mc.moving_task) { |
| 1384 | if (mem_cgroup_under_move(mem)) { |
| 1385 | DEFINE_WAIT(wait); |
| 1386 | prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE); |
| 1387 | /* moving charge context might have finished. */ |
| 1388 | if (mc.moving_task) |
| 1389 | schedule(); |
| 1390 | finish_wait(&mc.waitq, &wait); |
| 1391 | return true; |
| 1392 | } |
| 1393 | } |
| 1394 | return false; |
| 1395 | } |
| 1396 | |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1397 | /** |
Kirill A. Shutemov | 6a6135b | 2010-03-10 15:22:25 -0800 | [diff] [blame] | 1398 | * 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] | 1399 | * @memcg: The memory cgroup that went over limit |
| 1400 | * @p: Task that is going to be killed |
| 1401 | * |
| 1402 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is |
| 1403 | * enabled |
| 1404 | */ |
| 1405 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) |
| 1406 | { |
| 1407 | struct cgroup *task_cgrp; |
| 1408 | struct cgroup *mem_cgrp; |
| 1409 | /* |
| 1410 | * Need a buffer in BSS, can't rely on allocations. The code relies |
| 1411 | * on the assumption that OOM is serialized for memory controller. |
| 1412 | * If this assumption is broken, revisit this code. |
| 1413 | */ |
| 1414 | static char memcg_name[PATH_MAX]; |
| 1415 | int ret; |
| 1416 | |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1417 | if (!memcg || !p) |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1418 | return; |
| 1419 | |
| 1420 | |
| 1421 | rcu_read_lock(); |
| 1422 | |
| 1423 | mem_cgrp = memcg->css.cgroup; |
| 1424 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); |
| 1425 | |
| 1426 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); |
| 1427 | if (ret < 0) { |
| 1428 | /* |
| 1429 | * Unfortunately, we are unable to convert to a useful name |
| 1430 | * But we'll still print out the usage information |
| 1431 | */ |
| 1432 | rcu_read_unlock(); |
| 1433 | goto done; |
| 1434 | } |
| 1435 | rcu_read_unlock(); |
| 1436 | |
| 1437 | printk(KERN_INFO "Task in %s killed", memcg_name); |
| 1438 | |
| 1439 | rcu_read_lock(); |
| 1440 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); |
| 1441 | if (ret < 0) { |
| 1442 | rcu_read_unlock(); |
| 1443 | goto done; |
| 1444 | } |
| 1445 | rcu_read_unlock(); |
| 1446 | |
| 1447 | /* |
| 1448 | * Continues from above, so we don't need an KERN_ level |
| 1449 | */ |
| 1450 | printk(KERN_CONT " as a result of limit of %s\n", memcg_name); |
| 1451 | done: |
| 1452 | |
| 1453 | printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n", |
| 1454 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, |
| 1455 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, |
| 1456 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); |
| 1457 | printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, " |
| 1458 | "failcnt %llu\n", |
| 1459 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, |
| 1460 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, |
| 1461 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); |
| 1462 | } |
| 1463 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1464 | /* |
| 1465 | * This function returns the number of memcg under hierarchy tree. Returns |
| 1466 | * 1(self count) if no children. |
| 1467 | */ |
| 1468 | static int mem_cgroup_count_children(struct mem_cgroup *mem) |
| 1469 | { |
| 1470 | int num = 0; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1471 | struct mem_cgroup *iter; |
| 1472 | |
| 1473 | for_each_mem_cgroup_tree(iter, mem) |
| 1474 | num++; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1475 | return num; |
| 1476 | } |
| 1477 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1478 | /* |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1479 | * Return the memory (and swap, if configured) limit for a memcg. |
| 1480 | */ |
| 1481 | u64 mem_cgroup_get_limit(struct mem_cgroup *memcg) |
| 1482 | { |
| 1483 | u64 limit; |
| 1484 | u64 memsw; |
| 1485 | |
Johannes Weiner | f3e8eb7 | 2011-01-13 15:47:39 -0800 | [diff] [blame] | 1486 | limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 1487 | limit += total_swap_pages << PAGE_SHIFT; |
| 1488 | |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1489 | memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 1490 | /* |
| 1491 | * If memsw is finite and limits the amount of swap space available |
| 1492 | * to this memcg, return that limit. |
| 1493 | */ |
| 1494 | return min(limit, memsw); |
| 1495 | } |
| 1496 | |
| 1497 | /* |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1498 | * Visit the first child (need not be the first child as per the ordering |
| 1499 | * of the cgroup list, since we track last_scanned_child) of @mem and use |
| 1500 | * that to reclaim free pages from. |
| 1501 | */ |
| 1502 | static struct mem_cgroup * |
| 1503 | mem_cgroup_select_victim(struct mem_cgroup *root_mem) |
| 1504 | { |
| 1505 | struct mem_cgroup *ret = NULL; |
| 1506 | struct cgroup_subsys_state *css; |
| 1507 | int nextid, found; |
| 1508 | |
| 1509 | if (!root_mem->use_hierarchy) { |
| 1510 | css_get(&root_mem->css); |
| 1511 | ret = root_mem; |
| 1512 | } |
| 1513 | |
| 1514 | while (!ret) { |
| 1515 | rcu_read_lock(); |
| 1516 | nextid = root_mem->last_scanned_child + 1; |
| 1517 | css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css, |
| 1518 | &found); |
| 1519 | if (css && css_tryget(css)) |
| 1520 | ret = container_of(css, struct mem_cgroup, css); |
| 1521 | |
| 1522 | rcu_read_unlock(); |
| 1523 | /* Updates scanning parameter */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1524 | if (!css) { |
| 1525 | /* this means start scan from ID:1 */ |
| 1526 | root_mem->last_scanned_child = 0; |
| 1527 | } else |
| 1528 | root_mem->last_scanned_child = found; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | return ret; |
| 1532 | } |
| 1533 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1534 | /** |
| 1535 | * test_mem_cgroup_node_reclaimable |
| 1536 | * @mem: the target memcg |
| 1537 | * @nid: the node ID to be checked. |
| 1538 | * @noswap : specify true here if the user wants flle only information. |
| 1539 | * |
| 1540 | * This function returns whether the specified memcg contains any |
| 1541 | * reclaimable pages on a node. Returns true if there are any reclaimable |
| 1542 | * pages in the node. |
| 1543 | */ |
| 1544 | static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *mem, |
| 1545 | int nid, bool noswap) |
| 1546 | { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1547 | if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_FILE)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1548 | return true; |
| 1549 | if (noswap || !total_swap_pages) |
| 1550 | return false; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1551 | if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_ANON)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1552 | return true; |
| 1553 | return false; |
| 1554 | |
| 1555 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1556 | #if MAX_NUMNODES > 1 |
| 1557 | |
| 1558 | /* |
| 1559 | * Always updating the nodemask is not very good - even if we have an empty |
| 1560 | * list or the wrong list here, we can start from some node and traverse all |
| 1561 | * nodes based on the zonelist. So update the list loosely once per 10 secs. |
| 1562 | * |
| 1563 | */ |
| 1564 | static void mem_cgroup_may_update_nodemask(struct mem_cgroup *mem) |
| 1565 | { |
| 1566 | int nid; |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1567 | /* |
| 1568 | * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET |
| 1569 | * pagein/pageout changes since the last update. |
| 1570 | */ |
| 1571 | if (!atomic_read(&mem->numainfo_events)) |
| 1572 | return; |
| 1573 | if (atomic_inc_return(&mem->numainfo_updating) > 1) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1574 | return; |
| 1575 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1576 | /* make a nodemask where this memcg uses memory from */ |
| 1577 | mem->scan_nodes = node_states[N_HIGH_MEMORY]; |
| 1578 | |
| 1579 | for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) { |
| 1580 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1581 | if (!test_mem_cgroup_node_reclaimable(mem, nid, false)) |
| 1582 | node_clear(nid, mem->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1583 | } |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1584 | |
| 1585 | atomic_set(&mem->numainfo_events, 0); |
| 1586 | atomic_set(&mem->numainfo_updating, 0); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | /* |
| 1590 | * Selecting a node where we start reclaim from. Because what we need is just |
| 1591 | * reducing usage counter, start from anywhere is O,K. Considering |
| 1592 | * memory reclaim from current node, there are pros. and cons. |
| 1593 | * |
| 1594 | * Freeing memory from current node means freeing memory from a node which |
| 1595 | * we'll use or we've used. So, it may make LRU bad. And if several threads |
| 1596 | * hit limits, it will see a contention on a node. But freeing from remote |
| 1597 | * node means more costs for memory reclaim because of memory latency. |
| 1598 | * |
| 1599 | * Now, we use round-robin. Better algorithm is welcomed. |
| 1600 | */ |
| 1601 | int mem_cgroup_select_victim_node(struct mem_cgroup *mem) |
| 1602 | { |
| 1603 | int node; |
| 1604 | |
| 1605 | mem_cgroup_may_update_nodemask(mem); |
| 1606 | node = mem->last_scanned_node; |
| 1607 | |
| 1608 | node = next_node(node, mem->scan_nodes); |
| 1609 | if (node == MAX_NUMNODES) |
| 1610 | node = first_node(mem->scan_nodes); |
| 1611 | /* |
| 1612 | * We call this when we hit limit, not when pages are added to LRU. |
| 1613 | * No LRU may hold pages because all pages are UNEVICTABLE or |
| 1614 | * memcg is too small and all pages are not on LRU. In that case, |
| 1615 | * we use curret node. |
| 1616 | */ |
| 1617 | if (unlikely(node == MAX_NUMNODES)) |
| 1618 | node = numa_node_id(); |
| 1619 | |
| 1620 | mem->last_scanned_node = node; |
| 1621 | return node; |
| 1622 | } |
| 1623 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1624 | /* |
| 1625 | * Check all nodes whether it contains reclaimable pages or not. |
| 1626 | * For quick scan, we make use of scan_nodes. This will allow us to skip |
| 1627 | * unused nodes. But scan_nodes is lazily updated and may not cotain |
| 1628 | * enough new information. We need to do double check. |
| 1629 | */ |
| 1630 | bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap) |
| 1631 | { |
| 1632 | int nid; |
| 1633 | |
| 1634 | /* |
| 1635 | * quick check...making use of scan_node. |
| 1636 | * We can skip unused nodes. |
| 1637 | */ |
| 1638 | if (!nodes_empty(mem->scan_nodes)) { |
| 1639 | for (nid = first_node(mem->scan_nodes); |
| 1640 | nid < MAX_NUMNODES; |
| 1641 | nid = next_node(nid, mem->scan_nodes)) { |
| 1642 | |
| 1643 | if (test_mem_cgroup_node_reclaimable(mem, nid, noswap)) |
| 1644 | return true; |
| 1645 | } |
| 1646 | } |
| 1647 | /* |
| 1648 | * Check rest of nodes. |
| 1649 | */ |
| 1650 | for_each_node_state(nid, N_HIGH_MEMORY) { |
| 1651 | if (node_isset(nid, mem->scan_nodes)) |
| 1652 | continue; |
| 1653 | if (test_mem_cgroup_node_reclaimable(mem, nid, noswap)) |
| 1654 | return true; |
| 1655 | } |
| 1656 | return false; |
| 1657 | } |
| 1658 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1659 | #else |
| 1660 | int mem_cgroup_select_victim_node(struct mem_cgroup *mem) |
| 1661 | { |
| 1662 | return 0; |
| 1663 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1664 | |
| 1665 | bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap) |
| 1666 | { |
| 1667 | return test_mem_cgroup_node_reclaimable(mem, 0, noswap); |
| 1668 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1669 | #endif |
| 1670 | |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame^] | 1671 | static void __mem_cgroup_record_scanstat(unsigned long *stats, |
| 1672 | struct memcg_scanrecord *rec) |
| 1673 | { |
| 1674 | |
| 1675 | stats[SCAN] += rec->nr_scanned[0] + rec->nr_scanned[1]; |
| 1676 | stats[SCAN_ANON] += rec->nr_scanned[0]; |
| 1677 | stats[SCAN_FILE] += rec->nr_scanned[1]; |
| 1678 | |
| 1679 | stats[ROTATE] += rec->nr_rotated[0] + rec->nr_rotated[1]; |
| 1680 | stats[ROTATE_ANON] += rec->nr_rotated[0]; |
| 1681 | stats[ROTATE_FILE] += rec->nr_rotated[1]; |
| 1682 | |
| 1683 | stats[FREED] += rec->nr_freed[0] + rec->nr_freed[1]; |
| 1684 | stats[FREED_ANON] += rec->nr_freed[0]; |
| 1685 | stats[FREED_FILE] += rec->nr_freed[1]; |
| 1686 | |
| 1687 | stats[ELAPSED] += rec->elapsed; |
| 1688 | } |
| 1689 | |
| 1690 | static void mem_cgroup_record_scanstat(struct memcg_scanrecord *rec) |
| 1691 | { |
| 1692 | struct mem_cgroup *mem; |
| 1693 | int context = rec->context; |
| 1694 | |
| 1695 | if (context >= NR_SCAN_CONTEXT) |
| 1696 | return; |
| 1697 | |
| 1698 | mem = rec->mem; |
| 1699 | spin_lock(&mem->scanstat.lock); |
| 1700 | __mem_cgroup_record_scanstat(mem->scanstat.stats[context], rec); |
| 1701 | spin_unlock(&mem->scanstat.lock); |
| 1702 | |
| 1703 | mem = rec->root; |
| 1704 | spin_lock(&mem->scanstat.lock); |
| 1705 | __mem_cgroup_record_scanstat(mem->scanstat.rootstats[context], rec); |
| 1706 | spin_unlock(&mem->scanstat.lock); |
| 1707 | } |
| 1708 | |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1709 | /* |
| 1710 | * Scan the hierarchy if needed to reclaim memory. We remember the last child |
| 1711 | * we reclaimed from, so that we don't end up penalizing one child extensively |
| 1712 | * based on its position in the children list. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1713 | * |
| 1714 | * root_mem is the original ancestor that we've been reclaim from. |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1715 | * |
| 1716 | * We give up and return to the caller when we visit root_mem twice. |
| 1717 | * (other groups can be removed while we're walking....) |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1718 | * |
| 1719 | * If shrink==true, for avoiding to free too much, this returns immedieately. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1720 | */ |
| 1721 | static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem, |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1722 | struct zone *zone, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1723 | gfp_t gfp_mask, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1724 | unsigned long reclaim_options, |
| 1725 | unsigned long *total_scanned) |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1726 | { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1727 | struct mem_cgroup *victim; |
| 1728 | int ret, total = 0; |
| 1729 | int loop = 0; |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1730 | bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP; |
| 1731 | bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1732 | bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT; |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame^] | 1733 | struct memcg_scanrecord rec; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1734 | unsigned long excess; |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame^] | 1735 | unsigned long scanned; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1736 | |
| 1737 | excess = res_counter_soft_limit_excess(&root_mem->res) >> PAGE_SHIFT; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1738 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1739 | /* If memsw_is_minimum==1, swap-out is of-no-use. */ |
Daisuke Nishimura | 108b6a7 | 2011-07-26 16:08:25 -0700 | [diff] [blame] | 1740 | if (!check_soft && !shrink && root_mem->memsw_is_minimum) |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1741 | noswap = true; |
| 1742 | |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame^] | 1743 | if (shrink) |
| 1744 | rec.context = SCAN_BY_SHRINK; |
| 1745 | else if (check_soft) |
| 1746 | rec.context = SCAN_BY_SYSTEM; |
| 1747 | else |
| 1748 | rec.context = SCAN_BY_LIMIT; |
| 1749 | |
| 1750 | rec.root = root_mem; |
| 1751 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1752 | while (1) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1753 | victim = mem_cgroup_select_victim(root_mem); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1754 | if (victim == root_mem) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1755 | loop++; |
KAMEZAWA Hiroyuki | fbc29a2 | 2011-06-15 15:08:46 -0700 | [diff] [blame] | 1756 | /* |
| 1757 | * We are not draining per cpu cached charges during |
| 1758 | * soft limit reclaim because global reclaim doesn't |
| 1759 | * care about charges. It tries to free some memory and |
| 1760 | * charges will not give any. |
| 1761 | */ |
| 1762 | if (!check_soft && loop >= 1) |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 1763 | drain_all_stock_async(root_mem); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1764 | if (loop >= 2) { |
| 1765 | /* |
| 1766 | * If we have not been able to reclaim |
| 1767 | * anything, it might because there are |
| 1768 | * no reclaimable pages under this hierarchy |
| 1769 | */ |
| 1770 | if (!check_soft || !total) { |
| 1771 | css_put(&victim->css); |
| 1772 | break; |
| 1773 | } |
| 1774 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1775 | * We want to do more targeted reclaim. |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1776 | * excess >> 2 is not to excessive so as to |
| 1777 | * reclaim too much, nor too less that we keep |
| 1778 | * coming back to reclaim from this cgroup |
| 1779 | */ |
| 1780 | if (total >= (excess >> 2) || |
| 1781 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) { |
| 1782 | css_put(&victim->css); |
| 1783 | break; |
| 1784 | } |
| 1785 | } |
| 1786 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1787 | if (!mem_cgroup_reclaimable(victim, noswap)) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1788 | /* this cgroup's local usage == 0 */ |
| 1789 | css_put(&victim->css); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1790 | continue; |
| 1791 | } |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame^] | 1792 | rec.mem = victim; |
| 1793 | rec.nr_scanned[0] = 0; |
| 1794 | rec.nr_scanned[1] = 0; |
| 1795 | rec.nr_rotated[0] = 0; |
| 1796 | rec.nr_rotated[1] = 0; |
| 1797 | rec.nr_freed[0] = 0; |
| 1798 | rec.nr_freed[1] = 0; |
| 1799 | rec.elapsed = 0; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1800 | /* we use swappiness of local cgroup */ |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1801 | if (check_soft) { |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1802 | ret = mem_cgroup_shrink_node_zone(victim, gfp_mask, |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame^] | 1803 | noswap, zone, &rec, &scanned); |
| 1804 | *total_scanned += scanned; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1805 | } else |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1806 | ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame^] | 1807 | noswap, &rec); |
| 1808 | mem_cgroup_record_scanstat(&rec); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1809 | css_put(&victim->css); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1810 | /* |
| 1811 | * At shrinking usage, we can't check we should stop here or |
| 1812 | * reclaim more. It's depends on callers. last_scanned_child |
| 1813 | * will work enough for keeping fairness under tree. |
| 1814 | */ |
| 1815 | if (shrink) |
| 1816 | return ret; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1817 | total += ret; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1818 | if (check_soft) { |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1819 | if (!res_counter_soft_limit_excess(&root_mem->res)) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1820 | return total; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1821 | } else if (mem_cgroup_margin(root_mem)) |
Johannes Weiner | 4fd14eb | 2011-05-26 16:25:35 -0700 | [diff] [blame] | 1822 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1823 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1824 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1825 | } |
| 1826 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1827 | /* |
| 1828 | * Check OOM-Killer is already running under our hierarchy. |
| 1829 | * If someone is running, return false. |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1830 | * Has to be called with memcg_oom_lock |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1831 | */ |
| 1832 | static bool mem_cgroup_oom_lock(struct mem_cgroup *mem) |
| 1833 | { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1834 | int lock_count = -1; |
| 1835 | struct mem_cgroup *iter, *failed = NULL; |
| 1836 | bool cond = true; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1837 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1838 | for_each_mem_cgroup_tree_cond(iter, mem, cond) { |
| 1839 | bool locked = iter->oom_lock; |
| 1840 | |
| 1841 | iter->oom_lock = true; |
| 1842 | if (lock_count == -1) |
| 1843 | lock_count = iter->oom_lock; |
| 1844 | else if (lock_count != locked) { |
| 1845 | /* |
| 1846 | * this subtree of our hierarchy is already locked |
| 1847 | * so we cannot give a lock. |
| 1848 | */ |
| 1849 | lock_count = 0; |
| 1850 | failed = iter; |
| 1851 | cond = false; |
| 1852 | } |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1853 | } |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1854 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1855 | if (!failed) |
| 1856 | goto done; |
| 1857 | |
| 1858 | /* |
| 1859 | * OK, we failed to lock the whole subtree so we have to clean up |
| 1860 | * what we set up to the failing subtree |
| 1861 | */ |
| 1862 | cond = true; |
| 1863 | for_each_mem_cgroup_tree_cond(iter, mem, cond) { |
| 1864 | if (iter == failed) { |
| 1865 | cond = false; |
| 1866 | continue; |
| 1867 | } |
| 1868 | iter->oom_lock = false; |
| 1869 | } |
| 1870 | done: |
| 1871 | return lock_count; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1872 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1873 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1874 | /* |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1875 | * Has to be called with memcg_oom_lock |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1876 | */ |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1877 | static int mem_cgroup_oom_unlock(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1878 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1879 | struct mem_cgroup *iter; |
| 1880 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1881 | for_each_mem_cgroup_tree(iter, mem) |
| 1882 | iter->oom_lock = false; |
| 1883 | return 0; |
| 1884 | } |
| 1885 | |
| 1886 | static void mem_cgroup_mark_under_oom(struct mem_cgroup *mem) |
| 1887 | { |
| 1888 | struct mem_cgroup *iter; |
| 1889 | |
| 1890 | for_each_mem_cgroup_tree(iter, mem) |
| 1891 | atomic_inc(&iter->under_oom); |
| 1892 | } |
| 1893 | |
| 1894 | static void mem_cgroup_unmark_under_oom(struct mem_cgroup *mem) |
| 1895 | { |
| 1896 | struct mem_cgroup *iter; |
| 1897 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1898 | /* |
| 1899 | * When a new child is created while the hierarchy is under oom, |
| 1900 | * mem_cgroup_oom_lock() may not be called. We have to use |
| 1901 | * atomic_add_unless() here. |
| 1902 | */ |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1903 | for_each_mem_cgroup_tree(iter, mem) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1904 | atomic_add_unless(&iter->under_oom, -1, 0); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1905 | } |
| 1906 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1907 | static DEFINE_SPINLOCK(memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1908 | static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq); |
| 1909 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1910 | struct oom_wait_info { |
| 1911 | struct mem_cgroup *mem; |
| 1912 | wait_queue_t wait; |
| 1913 | }; |
| 1914 | |
| 1915 | static int memcg_oom_wake_function(wait_queue_t *wait, |
| 1916 | unsigned mode, int sync, void *arg) |
| 1917 | { |
| 1918 | struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg; |
| 1919 | struct oom_wait_info *oom_wait_info; |
| 1920 | |
| 1921 | oom_wait_info = container_of(wait, struct oom_wait_info, wait); |
| 1922 | |
| 1923 | if (oom_wait_info->mem == wake_mem) |
| 1924 | goto wakeup; |
| 1925 | /* if no hierarchy, no match */ |
| 1926 | if (!oom_wait_info->mem->use_hierarchy || !wake_mem->use_hierarchy) |
| 1927 | return 0; |
| 1928 | /* |
| 1929 | * Both of oom_wait_info->mem and wake_mem are stable under us. |
| 1930 | * Then we can use css_is_ancestor without taking care of RCU. |
| 1931 | */ |
| 1932 | if (!css_is_ancestor(&oom_wait_info->mem->css, &wake_mem->css) && |
| 1933 | !css_is_ancestor(&wake_mem->css, &oom_wait_info->mem->css)) |
| 1934 | return 0; |
| 1935 | |
| 1936 | wakeup: |
| 1937 | return autoremove_wake_function(wait, mode, sync, arg); |
| 1938 | } |
| 1939 | |
| 1940 | static void memcg_wakeup_oom(struct mem_cgroup *mem) |
| 1941 | { |
| 1942 | /* for filtering, pass "mem" as argument. */ |
| 1943 | __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem); |
| 1944 | } |
| 1945 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1946 | static void memcg_oom_recover(struct mem_cgroup *mem) |
| 1947 | { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1948 | if (mem && atomic_read(&mem->under_oom)) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1949 | memcg_wakeup_oom(mem); |
| 1950 | } |
| 1951 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1952 | /* |
| 1953 | * try to call OOM killer. returns false if we should exit memory-reclaim loop. |
| 1954 | */ |
| 1955 | bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask) |
| 1956 | { |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1957 | struct oom_wait_info owait; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1958 | bool locked, need_to_kill; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1959 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1960 | owait.mem = mem; |
| 1961 | owait.wait.flags = 0; |
| 1962 | owait.wait.func = memcg_oom_wake_function; |
| 1963 | owait.wait.private = current; |
| 1964 | INIT_LIST_HEAD(&owait.wait.task_list); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1965 | need_to_kill = true; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1966 | mem_cgroup_mark_under_oom(mem); |
| 1967 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1968 | /* At first, try to OOM lock hierarchy under mem.*/ |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1969 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1970 | locked = mem_cgroup_oom_lock(mem); |
| 1971 | /* |
| 1972 | * Even if signal_pending(), we can't quit charge() loop without |
| 1973 | * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL |
| 1974 | * under OOM is always welcomed, use TASK_KILLABLE here. |
| 1975 | */ |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1976 | prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE); |
| 1977 | if (!locked || mem->oom_kill_disable) |
| 1978 | need_to_kill = false; |
| 1979 | if (locked) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1980 | mem_cgroup_oom_notify(mem); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1981 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1982 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1983 | if (need_to_kill) { |
| 1984 | finish_wait(&memcg_oom_waitq, &owait.wait); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1985 | mem_cgroup_out_of_memory(mem, mask); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1986 | } else { |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1987 | schedule(); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1988 | finish_wait(&memcg_oom_waitq, &owait.wait); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1989 | } |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1990 | spin_lock(&memcg_oom_lock); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1991 | if (locked) |
| 1992 | mem_cgroup_oom_unlock(mem); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1993 | memcg_wakeup_oom(mem); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1994 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1995 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1996 | mem_cgroup_unmark_under_oom(mem); |
| 1997 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1998 | if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current)) |
| 1999 | return false; |
| 2000 | /* Give chance to dying process */ |
| 2001 | schedule_timeout(1); |
| 2002 | return true; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 2003 | } |
| 2004 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2005 | /* |
| 2006 | * Currently used to update mapped file statistics, but the routine can be |
| 2007 | * generalized to update other statistics as well. |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2008 | * |
| 2009 | * Notes: Race condition |
| 2010 | * |
| 2011 | * We usually use page_cgroup_lock() for accessing page_cgroup member but |
| 2012 | * it tends to be costly. But considering some conditions, we doesn't need |
| 2013 | * to do so _always_. |
| 2014 | * |
| 2015 | * Considering "charge", lock_page_cgroup() is not required because all |
| 2016 | * file-stat operations happen after a page is attached to radix-tree. There |
| 2017 | * are no race with "charge". |
| 2018 | * |
| 2019 | * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup |
| 2020 | * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even |
| 2021 | * if there are race with "uncharge". Statistics itself is properly handled |
| 2022 | * by flags. |
| 2023 | * |
| 2024 | * Considering "move", this is an only case we see a race. To make the race |
| 2025 | * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are |
| 2026 | * possibility of race condition. If there is, we take a lock. |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2027 | */ |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2028 | |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2029 | void mem_cgroup_update_page_stat(struct page *page, |
| 2030 | enum mem_cgroup_page_stat_item idx, int val) |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2031 | { |
| 2032 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2033 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 2034 | bool need_unlock = false; |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 2035 | unsigned long uninitialized_var(flags); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2036 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2037 | if (unlikely(!pc)) |
| 2038 | return; |
| 2039 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2040 | rcu_read_lock(); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2041 | mem = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2042 | if (unlikely(!mem || !PageCgroupUsed(pc))) |
| 2043 | goto out; |
| 2044 | /* pc->mem_cgroup is unstable ? */ |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2045 | if (unlikely(mem_cgroup_stealed(mem)) || PageTransHuge(page)) { |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2046 | /* take a lock against to access pc->mem_cgroup */ |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 2047 | move_lock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2048 | need_unlock = true; |
| 2049 | mem = pc->mem_cgroup; |
| 2050 | if (!mem || !PageCgroupUsed(pc)) |
| 2051 | goto out; |
| 2052 | } |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2053 | |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2054 | switch (idx) { |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2055 | case MEMCG_NR_FILE_MAPPED: |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2056 | if (val > 0) |
| 2057 | SetPageCgroupFileMapped(pc); |
| 2058 | else if (!page_mapped(page)) |
KAMEZAWA Hiroyuki | 0c270f8 | 2010-10-27 15:33:39 -0700 | [diff] [blame] | 2059 | ClearPageCgroupFileMapped(pc); |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2060 | idx = MEM_CGROUP_STAT_FILE_MAPPED; |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2061 | break; |
| 2062 | default: |
| 2063 | BUG(); |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 2064 | } |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2065 | |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2066 | this_cpu_add(mem->stat->count[idx], val); |
| 2067 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2068 | out: |
| 2069 | if (unlikely(need_unlock)) |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 2070 | move_unlock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2071 | rcu_read_unlock(); |
| 2072 | return; |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2073 | } |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2074 | EXPORT_SYMBOL(mem_cgroup_update_page_stat); |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2075 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2076 | /* |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2077 | * size of first charge trial. "32" comes from vmscan.c's magic value. |
| 2078 | * TODO: maybe necessary to use big numbers in big irons. |
| 2079 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2080 | #define CHARGE_BATCH 32U |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2081 | struct memcg_stock_pcp { |
| 2082 | struct mem_cgroup *cached; /* this never be root cgroup */ |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2083 | unsigned int nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2084 | struct work_struct work; |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2085 | unsigned long flags; |
| 2086 | #define FLUSHING_CACHED_CHARGE (0) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2087 | }; |
| 2088 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2089 | static DEFINE_MUTEX(percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2090 | |
| 2091 | /* |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2092 | * 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] | 2093 | * from local stock and true is returned. If the stock is 0 or charges from a |
| 2094 | * cgroup which is not current target, returns false. This stock will be |
| 2095 | * refilled. |
| 2096 | */ |
| 2097 | static bool consume_stock(struct mem_cgroup *mem) |
| 2098 | { |
| 2099 | struct memcg_stock_pcp *stock; |
| 2100 | bool ret = true; |
| 2101 | |
| 2102 | stock = &get_cpu_var(memcg_stock); |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2103 | if (mem == stock->cached && stock->nr_pages) |
| 2104 | stock->nr_pages--; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2105 | else /* need to call res_counter_charge */ |
| 2106 | ret = false; |
| 2107 | put_cpu_var(memcg_stock); |
| 2108 | return ret; |
| 2109 | } |
| 2110 | |
| 2111 | /* |
| 2112 | * Returns stocks cached in percpu to res_counter and reset cached information. |
| 2113 | */ |
| 2114 | static void drain_stock(struct memcg_stock_pcp *stock) |
| 2115 | { |
| 2116 | struct mem_cgroup *old = stock->cached; |
| 2117 | |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2118 | if (stock->nr_pages) { |
| 2119 | unsigned long bytes = stock->nr_pages * PAGE_SIZE; |
| 2120 | |
| 2121 | res_counter_uncharge(&old->res, bytes); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2122 | if (do_swap_account) |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2123 | res_counter_uncharge(&old->memsw, bytes); |
| 2124 | stock->nr_pages = 0; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2125 | } |
| 2126 | stock->cached = NULL; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | /* |
| 2130 | * This must be called under preempt disabled or must be called by |
| 2131 | * a thread which is pinned to local cpu. |
| 2132 | */ |
| 2133 | static void drain_local_stock(struct work_struct *dummy) |
| 2134 | { |
| 2135 | struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock); |
| 2136 | drain_stock(stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2137 | clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | /* |
| 2141 | * 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] | 2142 | * This will be consumed by consume_stock() function, later. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2143 | */ |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2144 | static void refill_stock(struct mem_cgroup *mem, unsigned int nr_pages) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2145 | { |
| 2146 | struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock); |
| 2147 | |
| 2148 | if (stock->cached != mem) { /* reset if necessary */ |
| 2149 | drain_stock(stock); |
| 2150 | stock->cached = mem; |
| 2151 | } |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2152 | stock->nr_pages += nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2153 | put_cpu_var(memcg_stock); |
| 2154 | } |
| 2155 | |
| 2156 | /* |
| 2157 | * Tries to drain stocked charges in other cpus. This function is asynchronous |
| 2158 | * and just put a work per cpu for draining localy on each cpu. Caller can |
| 2159 | * expects some charges will be back to res_counter later but cannot wait for |
| 2160 | * it. |
| 2161 | */ |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2162 | static void drain_all_stock_async(struct mem_cgroup *root_mem) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2163 | { |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2164 | int cpu, curcpu; |
| 2165 | /* |
| 2166 | * If someone calls draining, avoid adding more kworker runs. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2167 | */ |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2168 | if (!mutex_trylock(&percpu_charge_mutex)) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2169 | return; |
| 2170 | /* Notify other cpus that system-wide "drain" is running */ |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2171 | get_online_cpus(); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2172 | /* |
| 2173 | * Get a hint for avoiding draining charges on the current cpu, |
| 2174 | * which must be exhausted by our charging. It is not required that |
| 2175 | * this be a precise check, so we use raw_smp_processor_id() instead of |
| 2176 | * getcpu()/putcpu(). |
| 2177 | */ |
| 2178 | curcpu = raw_smp_processor_id(); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2179 | for_each_online_cpu(cpu) { |
| 2180 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2181 | struct mem_cgroup *mem; |
| 2182 | |
| 2183 | if (cpu == curcpu) |
| 2184 | continue; |
| 2185 | |
| 2186 | mem = stock->cached; |
| 2187 | if (!mem) |
| 2188 | continue; |
| 2189 | if (mem != root_mem) { |
| 2190 | if (!root_mem->use_hierarchy) |
| 2191 | continue; |
| 2192 | /* check whether "mem" is under tree of "root_mem" */ |
| 2193 | if (!css_is_ancestor(&mem->css, &root_mem->css)) |
| 2194 | continue; |
| 2195 | } |
| 2196 | if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) |
| 2197 | schedule_work_on(cpu, &stock->work); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2198 | } |
| 2199 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2200 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2201 | /* We don't wait for flush_work */ |
| 2202 | } |
| 2203 | |
| 2204 | /* This is a synchronous drain interface. */ |
| 2205 | static void drain_all_stock_sync(void) |
| 2206 | { |
| 2207 | /* called when force_empty is called */ |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2208 | mutex_lock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2209 | schedule_on_each_cpu(drain_local_stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2210 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2211 | } |
| 2212 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2213 | /* |
| 2214 | * This function drains percpu counter value from DEAD cpu and |
| 2215 | * move it to local cpu. Note that this function can be preempted. |
| 2216 | */ |
| 2217 | static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *mem, int cpu) |
| 2218 | { |
| 2219 | int i; |
| 2220 | |
| 2221 | spin_lock(&mem->pcp_counter_lock); |
| 2222 | for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 2223 | long x = per_cpu(mem->stat->count[i], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2224 | |
| 2225 | per_cpu(mem->stat->count[i], cpu) = 0; |
| 2226 | mem->nocpu_base.count[i] += x; |
| 2227 | } |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2228 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) { |
| 2229 | unsigned long x = per_cpu(mem->stat->events[i], cpu); |
| 2230 | |
| 2231 | per_cpu(mem->stat->events[i], cpu) = 0; |
| 2232 | mem->nocpu_base.events[i] += x; |
| 2233 | } |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2234 | /* need to clear ON_MOVE value, works as a kind of lock. */ |
| 2235 | per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0; |
| 2236 | spin_unlock(&mem->pcp_counter_lock); |
| 2237 | } |
| 2238 | |
| 2239 | static void synchronize_mem_cgroup_on_move(struct mem_cgroup *mem, int cpu) |
| 2240 | { |
| 2241 | int idx = MEM_CGROUP_ON_MOVE; |
| 2242 | |
| 2243 | spin_lock(&mem->pcp_counter_lock); |
| 2244 | per_cpu(mem->stat->count[idx], cpu) = mem->nocpu_base.count[idx]; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2245 | spin_unlock(&mem->pcp_counter_lock); |
| 2246 | } |
| 2247 | |
| 2248 | static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb, |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2249 | unsigned long action, |
| 2250 | void *hcpu) |
| 2251 | { |
| 2252 | int cpu = (unsigned long)hcpu; |
| 2253 | struct memcg_stock_pcp *stock; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2254 | struct mem_cgroup *iter; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2255 | |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2256 | if ((action == CPU_ONLINE)) { |
| 2257 | for_each_mem_cgroup_all(iter) |
| 2258 | synchronize_mem_cgroup_on_move(iter, cpu); |
| 2259 | return NOTIFY_OK; |
| 2260 | } |
| 2261 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2262 | if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2263 | return NOTIFY_OK; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2264 | |
| 2265 | for_each_mem_cgroup_all(iter) |
| 2266 | mem_cgroup_drain_pcp_counter(iter, cpu); |
| 2267 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2268 | stock = &per_cpu(memcg_stock, cpu); |
| 2269 | drain_stock(stock); |
| 2270 | return NOTIFY_OK; |
| 2271 | } |
| 2272 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2273 | |
| 2274 | /* See __mem_cgroup_try_charge() for details */ |
| 2275 | enum { |
| 2276 | CHARGE_OK, /* success */ |
| 2277 | CHARGE_RETRY, /* need to retry but retry is not bad */ |
| 2278 | CHARGE_NOMEM, /* we can't do more. return -ENOMEM */ |
| 2279 | CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */ |
| 2280 | CHARGE_OOM_DIE, /* the current is killed because of OOM */ |
| 2281 | }; |
| 2282 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2283 | static int mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask, |
| 2284 | unsigned int nr_pages, bool oom_check) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2285 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2286 | unsigned long csize = nr_pages * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2287 | struct mem_cgroup *mem_over_limit; |
| 2288 | struct res_counter *fail_res; |
| 2289 | unsigned long flags = 0; |
| 2290 | int ret; |
| 2291 | |
| 2292 | ret = res_counter_charge(&mem->res, csize, &fail_res); |
| 2293 | |
| 2294 | if (likely(!ret)) { |
| 2295 | if (!do_swap_account) |
| 2296 | return CHARGE_OK; |
| 2297 | ret = res_counter_charge(&mem->memsw, csize, &fail_res); |
| 2298 | if (likely(!ret)) |
| 2299 | return CHARGE_OK; |
| 2300 | |
KAMEZAWA Hiroyuki | 01c88e2 | 2011-01-25 15:07:27 -0800 | [diff] [blame] | 2301 | res_counter_uncharge(&mem->res, csize); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2302 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw); |
| 2303 | flags |= MEM_CGROUP_RECLAIM_NOSWAP; |
| 2304 | } else |
| 2305 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, res); |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2306 | /* |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2307 | * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch |
| 2308 | * of regular pages (CHARGE_BATCH), or a single regular page (1). |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2309 | * |
| 2310 | * Never reclaim on behalf of optional batching, retry with a |
| 2311 | * single page instead. |
| 2312 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2313 | if (nr_pages == CHARGE_BATCH) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2314 | return CHARGE_RETRY; |
| 2315 | |
| 2316 | if (!(gfp_mask & __GFP_WAIT)) |
| 2317 | return CHARGE_WOULDBLOCK; |
| 2318 | |
| 2319 | ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 2320 | gfp_mask, flags, NULL); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2321 | if (mem_cgroup_margin(mem_over_limit) >= nr_pages) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2322 | return CHARGE_RETRY; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2323 | /* |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2324 | * Even though the limit is exceeded at this point, reclaim |
| 2325 | * may have been able to free some pages. Retry the charge |
| 2326 | * before killing the task. |
| 2327 | * |
| 2328 | * Only for regular pages, though: huge pages are rather |
| 2329 | * unlikely to succeed so close to the limit, and we fall back |
| 2330 | * to regular pages anyway in case of failure. |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2331 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2332 | if (nr_pages == 1 && ret) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2333 | return CHARGE_RETRY; |
| 2334 | |
| 2335 | /* |
| 2336 | * At task move, charge accounts can be doubly counted. So, it's |
| 2337 | * better to wait until the end of task_move if something is going on. |
| 2338 | */ |
| 2339 | if (mem_cgroup_wait_acct_move(mem_over_limit)) |
| 2340 | return CHARGE_RETRY; |
| 2341 | |
| 2342 | /* If we don't need to call oom-killer at el, return immediately */ |
| 2343 | if (!oom_check) |
| 2344 | return CHARGE_NOMEM; |
| 2345 | /* check OOM */ |
| 2346 | if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask)) |
| 2347 | return CHARGE_OOM_DIE; |
| 2348 | |
| 2349 | return CHARGE_RETRY; |
| 2350 | } |
| 2351 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2352 | /* |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2353 | * Unlike exported interface, "oom" parameter is added. if oom==true, |
| 2354 | * oom-killer can be invoked. |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2355 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2356 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2357 | gfp_t gfp_mask, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2358 | unsigned int nr_pages, |
| 2359 | struct mem_cgroup **memcg, |
| 2360 | bool oom) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2361 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2362 | unsigned int batch = max(CHARGE_BATCH, nr_pages); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2363 | int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
| 2364 | struct mem_cgroup *mem = NULL; |
| 2365 | int ret; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2366 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2367 | /* |
| 2368 | * Unlike gloval-vm's OOM-kill, we're not in memory shortage |
| 2369 | * in system level. So, allow to go ahead dying process in addition to |
| 2370 | * MEMDIE process. |
| 2371 | */ |
| 2372 | if (unlikely(test_thread_flag(TIF_MEMDIE) |
| 2373 | || fatal_signal_pending(current))) |
| 2374 | goto bypass; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2375 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2376 | /* |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2377 | * We always charge the cgroup the mm_struct belongs to. |
| 2378 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2379 | * thread group leader migrates. It's possible that mm is not |
| 2380 | * set, if so charge the init_mm (happens for pagecache usage). |
| 2381 | */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2382 | if (!*memcg && !mm) |
| 2383 | goto bypass; |
| 2384 | again: |
| 2385 | if (*memcg) { /* css should be a valid one */ |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2386 | mem = *memcg; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2387 | VM_BUG_ON(css_is_removed(&mem->css)); |
| 2388 | if (mem_cgroup_is_root(mem)) |
| 2389 | goto done; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2390 | if (nr_pages == 1 && consume_stock(mem)) |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2391 | goto done; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2392 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2393 | } else { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2394 | struct task_struct *p; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2395 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2396 | rcu_read_lock(); |
| 2397 | p = rcu_dereference(mm->owner); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2398 | /* |
KAMEZAWA Hiroyuki | ebb76ce | 2010-12-29 14:07:11 -0800 | [diff] [blame] | 2399 | * Because we don't have task_lock(), "p" can exit. |
| 2400 | * In that case, "mem" can point to root or p can be NULL with |
| 2401 | * race with swapoff. Then, we have small risk of mis-accouning. |
| 2402 | * But such kind of mis-account by race always happens because |
| 2403 | * we don't have cgroup_mutex(). It's overkill and we allo that |
| 2404 | * small race, here. |
| 2405 | * (*) swapoff at el will charge against mm-struct not against |
| 2406 | * task-struct. So, mm->owner can be NULL. |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2407 | */ |
| 2408 | mem = mem_cgroup_from_task(p); |
KAMEZAWA Hiroyuki | ebb76ce | 2010-12-29 14:07:11 -0800 | [diff] [blame] | 2409 | if (!mem || mem_cgroup_is_root(mem)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2410 | rcu_read_unlock(); |
| 2411 | goto done; |
| 2412 | } |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2413 | if (nr_pages == 1 && consume_stock(mem)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2414 | /* |
| 2415 | * It seems dagerous to access memcg without css_get(). |
| 2416 | * But considering how consume_stok works, it's not |
| 2417 | * necessary. If consume_stock success, some charges |
| 2418 | * from this memcg are cached on this cpu. So, we |
| 2419 | * don't need to call css_get()/css_tryget() before |
| 2420 | * calling consume_stock(). |
| 2421 | */ |
| 2422 | rcu_read_unlock(); |
| 2423 | goto done; |
| 2424 | } |
| 2425 | /* after here, we may be blocked. we need to get refcnt */ |
| 2426 | if (!css_tryget(&mem->css)) { |
| 2427 | rcu_read_unlock(); |
| 2428 | goto again; |
| 2429 | } |
| 2430 | rcu_read_unlock(); |
| 2431 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2432 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2433 | do { |
| 2434 | bool oom_check; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2435 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2436 | /* If killed, bypass charge */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2437 | if (fatal_signal_pending(current)) { |
| 2438 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2439 | goto bypass; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2440 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2441 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2442 | oom_check = false; |
| 2443 | if (oom && !nr_oom_retries) { |
| 2444 | oom_check = true; |
| 2445 | nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
| 2446 | } |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 2447 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2448 | ret = mem_cgroup_do_charge(mem, gfp_mask, batch, oom_check); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2449 | switch (ret) { |
| 2450 | case CHARGE_OK: |
| 2451 | break; |
| 2452 | case CHARGE_RETRY: /* not in OOM situation but retry */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2453 | batch = nr_pages; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2454 | css_put(&mem->css); |
| 2455 | mem = NULL; |
| 2456 | goto again; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2457 | case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2458 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2459 | goto nomem; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2460 | case CHARGE_NOMEM: /* OOM routine works */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2461 | if (!oom) { |
| 2462 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2463 | goto nomem; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2464 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2465 | /* If oom, we never return -ENOMEM */ |
| 2466 | nr_oom_retries--; |
| 2467 | break; |
| 2468 | case CHARGE_OOM_DIE: /* Killed by OOM Killer */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2469 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2470 | goto bypass; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2471 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2472 | } while (ret != CHARGE_OK); |
| 2473 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2474 | if (batch > nr_pages) |
| 2475 | refill_stock(mem, batch - nr_pages); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2476 | css_put(&mem->css); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2477 | done: |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2478 | *memcg = mem; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2479 | return 0; |
| 2480 | nomem: |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2481 | *memcg = NULL; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2482 | return -ENOMEM; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2483 | bypass: |
| 2484 | *memcg = NULL; |
| 2485 | return 0; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2486 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2487 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2488 | /* |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2489 | * Somemtimes we have to undo a charge we got by try_charge(). |
| 2490 | * This function is for that and do uncharge, put css's refcnt. |
| 2491 | * gotten by try_charge(). |
| 2492 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2493 | static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem, |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2494 | unsigned int nr_pages) |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2495 | { |
| 2496 | if (!mem_cgroup_is_root(mem)) { |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2497 | unsigned long bytes = nr_pages * PAGE_SIZE; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2498 | |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2499 | res_counter_uncharge(&mem->res, bytes); |
| 2500 | if (do_swap_account) |
| 2501 | res_counter_uncharge(&mem->memsw, bytes); |
| 2502 | } |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | /* |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2506 | * A helper function to get mem_cgroup from ID. must be called under |
| 2507 | * rcu_read_lock(). The caller must check css_is_removed() or some if |
| 2508 | * it's concern. (dropping refcnt from swap can be called against removed |
| 2509 | * memcg.) |
| 2510 | */ |
| 2511 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) |
| 2512 | { |
| 2513 | struct cgroup_subsys_state *css; |
| 2514 | |
| 2515 | /* ID 0 is unused ID */ |
| 2516 | if (!id) |
| 2517 | return NULL; |
| 2518 | css = css_lookup(&mem_cgroup_subsys, id); |
| 2519 | if (!css) |
| 2520 | return NULL; |
| 2521 | return container_of(css, struct mem_cgroup, css); |
| 2522 | } |
| 2523 | |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2524 | struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2525 | { |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2526 | struct mem_cgroup *mem = NULL; |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2527 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2528 | unsigned short id; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2529 | swp_entry_t ent; |
| 2530 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2531 | VM_BUG_ON(!PageLocked(page)); |
| 2532 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2533 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2534 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2535 | if (PageCgroupUsed(pc)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2536 | mem = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2537 | if (mem && !css_tryget(&mem->css)) |
| 2538 | mem = NULL; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2539 | } else if (PageSwapCache(page)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2540 | ent.val = page_private(page); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2541 | id = lookup_swap_cgroup(ent); |
| 2542 | rcu_read_lock(); |
| 2543 | mem = mem_cgroup_lookup(id); |
| 2544 | if (mem && !css_tryget(&mem->css)) |
| 2545 | mem = NULL; |
| 2546 | rcu_read_unlock(); |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2547 | } |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2548 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2549 | return mem; |
| 2550 | } |
| 2551 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2552 | static void __mem_cgroup_commit_charge(struct mem_cgroup *mem, |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2553 | struct page *page, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2554 | unsigned int nr_pages, |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2555 | struct page_cgroup *pc, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2556 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2557 | { |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2558 | lock_page_cgroup(pc); |
| 2559 | if (unlikely(PageCgroupUsed(pc))) { |
| 2560 | unlock_page_cgroup(pc); |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2561 | __mem_cgroup_cancel_charge(mem, nr_pages); |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2562 | return; |
| 2563 | } |
| 2564 | /* |
| 2565 | * we don't need page_cgroup_lock about tail pages, becase they are not |
| 2566 | * accessed by any other context at this point. |
| 2567 | */ |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2568 | pc->mem_cgroup = mem; |
KAMEZAWA Hiroyuki | 261fb61 | 2009-09-23 15:56:33 -0700 | [diff] [blame] | 2569 | /* |
| 2570 | * We access a page_cgroup asynchronously without lock_page_cgroup(). |
| 2571 | * Especially when a page_cgroup is taken from a page, pc->mem_cgroup |
| 2572 | * is accessed after testing USED bit. To make pc->mem_cgroup visible |
| 2573 | * before USED bit, we need memory barrier here. |
| 2574 | * See mem_cgroup_add_lru_list(), etc. |
| 2575 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2576 | smp_wmb(); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 2577 | switch (ctype) { |
| 2578 | case MEM_CGROUP_CHARGE_TYPE_CACHE: |
| 2579 | case MEM_CGROUP_CHARGE_TYPE_SHMEM: |
| 2580 | SetPageCgroupCache(pc); |
| 2581 | SetPageCgroupUsed(pc); |
| 2582 | break; |
| 2583 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
| 2584 | ClearPageCgroupCache(pc); |
| 2585 | SetPageCgroupUsed(pc); |
| 2586 | break; |
| 2587 | default: |
| 2588 | break; |
| 2589 | } |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2590 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2591 | mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), nr_pages); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2592 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 2593 | /* |
| 2594 | * "charge_statistics" updated event counter. Then, check it. |
| 2595 | * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree. |
| 2596 | * if they exceeds softlimit. |
| 2597 | */ |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2598 | memcg_check_events(mem, page); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2599 | } |
| 2600 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2601 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 2602 | |
| 2603 | #define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\ |
| 2604 | (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION)) |
| 2605 | /* |
| 2606 | * Because tail pages are not marked as "used", set it. We're under |
| 2607 | * zone->lru_lock, 'splitting on pmd' and compund_lock. |
| 2608 | */ |
| 2609 | void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail) |
| 2610 | { |
| 2611 | struct page_cgroup *head_pc = lookup_page_cgroup(head); |
| 2612 | struct page_cgroup *tail_pc = lookup_page_cgroup(tail); |
| 2613 | unsigned long flags; |
| 2614 | |
KAMEZAWA Hiroyuki | 3d37c4a | 2011-01-25 15:07:28 -0800 | [diff] [blame] | 2615 | if (mem_cgroup_disabled()) |
| 2616 | return; |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2617 | /* |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2618 | * We have no races with charge/uncharge but will have races with |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2619 | * page state accounting. |
| 2620 | */ |
| 2621 | move_lock_page_cgroup(head_pc, &flags); |
| 2622 | |
| 2623 | tail_pc->mem_cgroup = head_pc->mem_cgroup; |
| 2624 | smp_wmb(); /* see __commit_charge() */ |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2625 | if (PageCgroupAcctLRU(head_pc)) { |
| 2626 | enum lru_list lru; |
| 2627 | struct mem_cgroup_per_zone *mz; |
| 2628 | |
| 2629 | /* |
| 2630 | * LRU flags cannot be copied because we need to add tail |
| 2631 | *.page to LRU by generic call and our hook will be called. |
| 2632 | * We hold lru_lock, then, reduce counter directly. |
| 2633 | */ |
| 2634 | lru = page_lru(head); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 2635 | mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2636 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
| 2637 | } |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2638 | tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT; |
| 2639 | move_unlock_page_cgroup(head_pc, &flags); |
| 2640 | } |
| 2641 | #endif |
| 2642 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2643 | /** |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2644 | * mem_cgroup_move_account - move account of the page |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2645 | * @page: the page |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2646 | * @nr_pages: number of regular pages (>1 for huge pages) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2647 | * @pc: page_cgroup of the page. |
| 2648 | * @from: mem_cgroup which the page is moved from. |
| 2649 | * @to: mem_cgroup which the page is moved to. @from != @to. |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2650 | * @uncharge: whether we should call uncharge and css_put against @from. |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2651 | * |
| 2652 | * The caller must confirm following. |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2653 | * - page is not on LRU (isolate_page() is useful.) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2654 | * - compound_lock is held when nr_pages > 1 |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2655 | * |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2656 | * 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] | 2657 | * 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] | 2658 | * true, this function does "uncharge" from old cgroup, but it doesn't if |
| 2659 | * @uncharge is false, so a caller should do "uncharge". |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2660 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2661 | static int mem_cgroup_move_account(struct page *page, |
| 2662 | unsigned int nr_pages, |
| 2663 | struct page_cgroup *pc, |
| 2664 | struct mem_cgroup *from, |
| 2665 | struct mem_cgroup *to, |
| 2666 | bool uncharge) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2667 | { |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2668 | unsigned long flags; |
| 2669 | int ret; |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2670 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2671 | VM_BUG_ON(from == to); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2672 | VM_BUG_ON(PageLRU(page)); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2673 | /* |
| 2674 | * The page is isolated from LRU. So, collapse function |
| 2675 | * will not handle this page. But page splitting can happen. |
| 2676 | * Do this check under compound_page_lock(). The caller should |
| 2677 | * hold it. |
| 2678 | */ |
| 2679 | ret = -EBUSY; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2680 | if (nr_pages > 1 && !PageTransHuge(page)) |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2681 | goto out; |
| 2682 | |
| 2683 | lock_page_cgroup(pc); |
| 2684 | |
| 2685 | ret = -EINVAL; |
| 2686 | if (!PageCgroupUsed(pc) || pc->mem_cgroup != from) |
| 2687 | goto unlock; |
| 2688 | |
| 2689 | move_lock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2690 | |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 2691 | if (PageCgroupFileMapped(pc)) { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 2692 | /* Update mapped_file data for mem_cgroup */ |
| 2693 | preempt_disable(); |
| 2694 | __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 2695 | __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 2696 | preempt_enable(); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2697 | } |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2698 | mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2699 | if (uncharge) |
| 2700 | /* This is not "cancel", but cancel_charge does all we need. */ |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2701 | __mem_cgroup_cancel_charge(from, nr_pages); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2702 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2703 | /* caller should have done css_get */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2704 | pc->mem_cgroup = to; |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2705 | mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2706 | /* |
| 2707 | * We charges against "to" which may not have any tasks. Then, "to" |
| 2708 | * can be under rmdir(). But in current implementation, caller of |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 2709 | * 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] | 2710 | * guaranteed that "to" is never removed. So, we don't check rmdir |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 2711 | * status here. |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2712 | */ |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2713 | move_unlock_page_cgroup(pc, &flags); |
| 2714 | ret = 0; |
| 2715 | unlock: |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2716 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 2717 | /* |
| 2718 | * check events |
| 2719 | */ |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2720 | memcg_check_events(to, page); |
| 2721 | memcg_check_events(from, page); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2722 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2723 | return ret; |
| 2724 | } |
| 2725 | |
| 2726 | /* |
| 2727 | * move charges to its parent. |
| 2728 | */ |
| 2729 | |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2730 | static int mem_cgroup_move_parent(struct page *page, |
| 2731 | struct page_cgroup *pc, |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2732 | struct mem_cgroup *child, |
| 2733 | gfp_t gfp_mask) |
| 2734 | { |
| 2735 | struct cgroup *cg = child->css.cgroup; |
| 2736 | struct cgroup *pcg = cg->parent; |
| 2737 | struct mem_cgroup *parent; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2738 | unsigned int nr_pages; |
Andrew Morton | 4be4489 | 2011-03-23 16:42:39 -0700 | [diff] [blame] | 2739 | unsigned long uninitialized_var(flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2740 | int ret; |
| 2741 | |
| 2742 | /* Is ROOT ? */ |
| 2743 | if (!pcg) |
| 2744 | return -EINVAL; |
| 2745 | |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2746 | ret = -EBUSY; |
| 2747 | if (!get_page_unless_zero(page)) |
| 2748 | goto out; |
| 2749 | if (isolate_lru_page(page)) |
| 2750 | goto put; |
KAMEZAWA Hiroyuki | 52dbb90 | 2011-01-25 15:07:29 -0800 | [diff] [blame] | 2751 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2752 | nr_pages = hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2753 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2754 | parent = mem_cgroup_from_cont(pcg); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2755 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2756 | if (ret || !parent) |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2757 | goto put_back; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2758 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2759 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2760 | flags = compound_lock_irqsave(page); |
| 2761 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2762 | ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2763 | if (ret) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2764 | __mem_cgroup_cancel_charge(parent, nr_pages); |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 2765 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2766 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2767 | compound_unlock_irqrestore(page, flags); |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 2768 | put_back: |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2769 | putback_lru_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2770 | put: |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 2771 | put_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2772 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2773 | return ret; |
| 2774 | } |
| 2775 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2776 | /* |
| 2777 | * Charge the memory controller for page usage. |
| 2778 | * Return |
| 2779 | * 0 if the charge was successful |
| 2780 | * < 0 if the cgroup is over its limit |
| 2781 | */ |
| 2782 | 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] | 2783 | gfp_t gfp_mask, enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2784 | { |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2785 | struct mem_cgroup *mem = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2786 | unsigned int nr_pages = 1; |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 2787 | struct page_cgroup *pc; |
| 2788 | bool oom = true; |
| 2789 | int ret; |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2790 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2791 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2792 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2793 | VM_BUG_ON(!PageTransHuge(page)); |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 2794 | /* |
| 2795 | * Never OOM-kill a process for a huge page. The |
| 2796 | * fault handler will fall back to regular pages. |
| 2797 | */ |
| 2798 | oom = false; |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2799 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2800 | |
| 2801 | pc = lookup_page_cgroup(page); |
Johannes Weiner | af4a662 | 2011-03-23 16:42:24 -0700 | [diff] [blame] | 2802 | BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */ |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2803 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2804 | ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &mem, oom); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2805 | if (ret || !mem) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2806 | return ret; |
| 2807 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2808 | __mem_cgroup_commit_charge(mem, page, nr_pages, pc, ctype); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2809 | return 0; |
| 2810 | } |
| 2811 | |
| 2812 | int mem_cgroup_newpage_charge(struct page *page, |
| 2813 | struct mm_struct *mm, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2814 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2815 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 2816 | return 0; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2817 | /* |
| 2818 | * If already mapped, we don't have to account. |
| 2819 | * If page cache, page->mapping has address_space. |
| 2820 | * But page->mapping may have out-of-use anon_vma pointer, |
| 2821 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping |
| 2822 | * is NULL. |
| 2823 | */ |
| 2824 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) |
| 2825 | return 0; |
| 2826 | if (unlikely(!mm)) |
| 2827 | mm = &init_mm; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2828 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2829 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2830 | } |
| 2831 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2832 | static void |
| 2833 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 2834 | enum charge_type ctype); |
| 2835 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2836 | static void |
| 2837 | __mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *mem, |
| 2838 | enum charge_type ctype) |
| 2839 | { |
| 2840 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 2841 | /* |
| 2842 | * In some case, SwapCache, FUSE(splice_buf->radixtree), the page |
| 2843 | * is already on LRU. It means the page may on some other page_cgroup's |
| 2844 | * LRU. Take care of it. |
| 2845 | */ |
| 2846 | mem_cgroup_lru_del_before_commit(page); |
| 2847 | __mem_cgroup_commit_charge(mem, page, 1, pc, ctype); |
| 2848 | mem_cgroup_lru_add_after_commit(page); |
| 2849 | return; |
| 2850 | } |
| 2851 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 2852 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 2853 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2854 | { |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2855 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2856 | int ret; |
| 2857 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2858 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 2859 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2860 | if (PageCompound(page)) |
| 2861 | return 0; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2862 | /* |
| 2863 | * Corner case handling. This is called from add_to_page_cache() |
| 2864 | * in usual. But some FS (shmem) precharges this page before calling it |
| 2865 | * and call add_to_page_cache() with GFP_NOWAIT. |
| 2866 | * |
| 2867 | * For GFP_NOWAIT case, the page may be pre-charged before calling |
| 2868 | * add_to_page_cache(). (See shmem.c) check it here and avoid to call |
| 2869 | * charge twice. (It works but has to pay a bit larger cost.) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2870 | * And when the page is SwapCache, it should take swap information |
| 2871 | * into account. This is under lock_page() now. |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2872 | */ |
| 2873 | if (!(gfp_mask & __GFP_WAIT)) { |
| 2874 | struct page_cgroup *pc; |
| 2875 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2876 | pc = lookup_page_cgroup(page); |
| 2877 | if (!pc) |
| 2878 | return 0; |
| 2879 | lock_page_cgroup(pc); |
| 2880 | if (PageCgroupUsed(pc)) { |
| 2881 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2882 | return 0; |
| 2883 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2884 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2885 | } |
| 2886 | |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2887 | if (unlikely(!mm)) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2888 | mm = &init_mm; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2889 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2890 | if (page_is_file_cache(page)) { |
| 2891 | ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &mem, true); |
| 2892 | if (ret || !mem) |
| 2893 | return ret; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2894 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2895 | /* |
| 2896 | * FUSE reuses pages without going through the final |
| 2897 | * put that would remove them from the LRU list, make |
| 2898 | * sure that they get relinked properly. |
| 2899 | */ |
| 2900 | __mem_cgroup_commit_charge_lrucare(page, mem, |
| 2901 | MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 2902 | return ret; |
| 2903 | } |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2904 | /* shmem */ |
| 2905 | if (PageSwapCache(page)) { |
| 2906 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
| 2907 | if (!ret) |
| 2908 | __mem_cgroup_commit_charge_swapin(page, mem, |
| 2909 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
| 2910 | } else |
| 2911 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2912 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2913 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2914 | return ret; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 2915 | } |
| 2916 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2917 | /* |
| 2918 | * While swap-in, try_charge -> commit or cancel, the page is locked. |
| 2919 | * 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] | 2920 | * struct page_cgroup is acquired. This refcnt will be consumed by |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2921 | * "commit()" or removed by "cancel()" |
| 2922 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2923 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
| 2924 | struct page *page, |
| 2925 | gfp_t mask, struct mem_cgroup **ptr) |
| 2926 | { |
| 2927 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2928 | int ret; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2929 | |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 2930 | *ptr = NULL; |
| 2931 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2932 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2933 | return 0; |
| 2934 | |
| 2935 | if (!do_swap_account) |
| 2936 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2937 | /* |
| 2938 | * A racing thread's fault, or swapoff, may have already updated |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2939 | * the pte, and even removed page from swap cache: in those cases |
| 2940 | * do_swap_page()'s pte_same() test will fail; but there's also a |
| 2941 | * KSM case which does need to charge the page. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2942 | */ |
| 2943 | if (!PageSwapCache(page)) |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2944 | goto charge_cur_mm; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2945 | mem = try_get_mem_cgroup_from_page(page); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2946 | if (!mem) |
| 2947 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2948 | *ptr = mem; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2949 | ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2950 | css_put(&mem->css); |
| 2951 | return ret; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2952 | charge_cur_mm: |
| 2953 | if (unlikely(!mm)) |
| 2954 | mm = &init_mm; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2955 | return __mem_cgroup_try_charge(mm, mask, 1, ptr, true); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2956 | } |
| 2957 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2958 | static void |
| 2959 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 2960 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2961 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2962 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2963 | return; |
| 2964 | if (!ptr) |
| 2965 | return; |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2966 | cgroup_exclude_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2967 | |
| 2968 | __mem_cgroup_commit_charge_lrucare(page, ptr, ctype); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2969 | /* |
| 2970 | * Now swap is on-memory. This means this page may be |
| 2971 | * counted both as mem and swap....double count. |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2972 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
| 2973 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() |
| 2974 | * may call delete_from_swap_cache() before reach here. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2975 | */ |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2976 | if (do_swap_account && PageSwapCache(page)) { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2977 | swp_entry_t ent = {.val = page_private(page)}; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2978 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2979 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2980 | |
| 2981 | id = swap_cgroup_record(ent, 0); |
| 2982 | rcu_read_lock(); |
| 2983 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2984 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2985 | /* |
| 2986 | * This recorded memcg can be obsolete one. So, avoid |
| 2987 | * calling css_tryget |
| 2988 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2989 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 2990 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2991 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2992 | mem_cgroup_put(memcg); |
| 2993 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2994 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2995 | } |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2996 | /* |
| 2997 | * At swapin, we may charge account against cgroup which has no tasks. |
| 2998 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 2999 | * In that case, we need to call pre_destroy() again. check it here. |
| 3000 | */ |
| 3001 | cgroup_release_and_wakeup_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3002 | } |
| 3003 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 3004 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
| 3005 | { |
| 3006 | __mem_cgroup_commit_charge_swapin(page, ptr, |
| 3007 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 3008 | } |
| 3009 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3010 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem) |
| 3011 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3012 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3013 | return; |
| 3014 | if (!mem) |
| 3015 | return; |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 3016 | __mem_cgroup_cancel_charge(mem, 1); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3017 | } |
| 3018 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3019 | static void mem_cgroup_do_uncharge(struct mem_cgroup *mem, |
| 3020 | unsigned int nr_pages, |
| 3021 | const enum charge_type ctype) |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3022 | { |
| 3023 | struct memcg_batch_info *batch = NULL; |
| 3024 | bool uncharge_memsw = true; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3025 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3026 | /* If swapout, usage of swap doesn't decrease */ |
| 3027 | if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) |
| 3028 | uncharge_memsw = false; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3029 | |
| 3030 | batch = ¤t->memcg_batch; |
| 3031 | /* |
| 3032 | * In usual, we do css_get() when we remember memcg pointer. |
| 3033 | * But in this case, we keep res->usage until end of a series of |
| 3034 | * uncharges. Then, it's ok to ignore memcg's refcnt. |
| 3035 | */ |
| 3036 | if (!batch->memcg) |
| 3037 | batch->memcg = mem; |
| 3038 | /* |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3039 | * do_batch > 0 when unmapping pages or inode invalidate/truncate. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3040 | * 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] | 3041 | * the same cgroup and we have chance to coalesce uncharges. |
| 3042 | * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE) |
| 3043 | * because we want to do uncharge as soon as possible. |
| 3044 | */ |
| 3045 | |
| 3046 | if (!batch->do_batch || test_thread_flag(TIF_MEMDIE)) |
| 3047 | goto direct_uncharge; |
| 3048 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3049 | if (nr_pages > 1) |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 3050 | goto direct_uncharge; |
| 3051 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3052 | /* |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3053 | * In typical case, batch->memcg == mem. This means we can |
| 3054 | * merge a series of uncharges to an uncharge of res_counter. |
| 3055 | * If not, we uncharge res_counter ony by one. |
| 3056 | */ |
| 3057 | if (batch->memcg != mem) |
| 3058 | goto direct_uncharge; |
| 3059 | /* remember freed charge and uncharge it later */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3060 | batch->nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3061 | if (uncharge_memsw) |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3062 | batch->memsw_nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3063 | return; |
| 3064 | direct_uncharge: |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3065 | res_counter_uncharge(&mem->res, nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3066 | if (uncharge_memsw) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3067 | res_counter_uncharge(&mem->memsw, nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3068 | if (unlikely(batch->memcg != mem)) |
| 3069 | memcg_oom_recover(mem); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3070 | return; |
| 3071 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3072 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 3073 | /* |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3074 | * uncharge if !page_mapped(page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3075 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3076 | static struct mem_cgroup * |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3077 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3078 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3079 | struct mem_cgroup *mem = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3080 | unsigned int nr_pages = 1; |
| 3081 | struct page_cgroup *pc; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3082 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3083 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3084 | return NULL; |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 3085 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3086 | if (PageSwapCache(page)) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3087 | return NULL; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3088 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 3089 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3090 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 3091 | VM_BUG_ON(!PageTransHuge(page)); |
| 3092 | } |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 3093 | /* |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 3094 | * Check if our page_cgroup is valid |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 3095 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3096 | pc = lookup_page_cgroup(page); |
| 3097 | if (unlikely(!pc || !PageCgroupUsed(pc))) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3098 | return NULL; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3099 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3100 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3101 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3102 | mem = pc->mem_cgroup; |
| 3103 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3104 | if (!PageCgroupUsed(pc)) |
| 3105 | goto unlock_out; |
| 3106 | |
| 3107 | switch (ctype) { |
| 3108 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
KAMEZAWA Hiroyuki | 8a9478ca | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3109 | case MEM_CGROUP_CHARGE_TYPE_DROP: |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3110 | /* See mem_cgroup_prepare_migration() */ |
| 3111 | if (page_mapped(page) || PageCgroupMigration(pc)) |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3112 | goto unlock_out; |
| 3113 | break; |
| 3114 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: |
| 3115 | if (!PageAnon(page)) { /* Shared memory */ |
| 3116 | if (page->mapping && !page_is_file_cache(page)) |
| 3117 | goto unlock_out; |
| 3118 | } else if (page_mapped(page)) /* Anon */ |
| 3119 | goto unlock_out; |
| 3120 | break; |
| 3121 | default: |
| 3122 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3123 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3124 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3125 | mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), -nr_pages); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 3126 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3127 | ClearPageCgroupUsed(pc); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 3128 | /* |
| 3129 | * pc->mem_cgroup is not cleared here. It will be accessed when it's |
| 3130 | * freed from LRU. This is safe because uncharged page is expected not |
| 3131 | * to be reused (freed soon). Exception is SwapCache, it's handled by |
| 3132 | * special functions. |
| 3133 | */ |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 3134 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3135 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3136 | /* |
| 3137 | * even after unlock, we have mem->res.usage here and this memcg |
| 3138 | * will never be freed. |
| 3139 | */ |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 3140 | memcg_check_events(mem, page); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3141 | if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) { |
| 3142 | mem_cgroup_swap_statistics(mem, true); |
| 3143 | mem_cgroup_get(mem); |
| 3144 | } |
| 3145 | if (!mem_cgroup_is_root(mem)) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3146 | mem_cgroup_do_uncharge(mem, nr_pages, ctype); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 3147 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3148 | return mem; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3149 | |
| 3150 | unlock_out: |
| 3151 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3152 | return NULL; |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 3153 | } |
| 3154 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3155 | void mem_cgroup_uncharge_page(struct page *page) |
| 3156 | { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3157 | /* early check. */ |
| 3158 | if (page_mapped(page)) |
| 3159 | return; |
| 3160 | if (page->mapping && !PageAnon(page)) |
| 3161 | return; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3162 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 3163 | } |
| 3164 | |
| 3165 | void mem_cgroup_uncharge_cache_page(struct page *page) |
| 3166 | { |
| 3167 | VM_BUG_ON(page_mapped(page)); |
KAMEZAWA Hiroyuki | b7abea9 | 2008-10-18 20:28:09 -0700 | [diff] [blame] | 3168 | VM_BUG_ON(page->mapping); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3169 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 3170 | } |
| 3171 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3172 | /* |
| 3173 | * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate. |
| 3174 | * In that cases, pages are freed continuously and we can expect pages |
| 3175 | * are in the same memcg. All these calls itself limits the number of |
| 3176 | * pages freed at once, then uncharge_start/end() is called properly. |
| 3177 | * This may be called prural(2) times in a context, |
| 3178 | */ |
| 3179 | |
| 3180 | void mem_cgroup_uncharge_start(void) |
| 3181 | { |
| 3182 | current->memcg_batch.do_batch++; |
| 3183 | /* We can do nest. */ |
| 3184 | if (current->memcg_batch.do_batch == 1) { |
| 3185 | current->memcg_batch.memcg = NULL; |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3186 | current->memcg_batch.nr_pages = 0; |
| 3187 | current->memcg_batch.memsw_nr_pages = 0; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3188 | } |
| 3189 | } |
| 3190 | |
| 3191 | void mem_cgroup_uncharge_end(void) |
| 3192 | { |
| 3193 | struct memcg_batch_info *batch = ¤t->memcg_batch; |
| 3194 | |
| 3195 | if (!batch->do_batch) |
| 3196 | return; |
| 3197 | |
| 3198 | batch->do_batch--; |
| 3199 | if (batch->do_batch) /* If stacked, do nothing. */ |
| 3200 | return; |
| 3201 | |
| 3202 | if (!batch->memcg) |
| 3203 | return; |
| 3204 | /* |
| 3205 | * This "batch->memcg" is valid without any css_get/put etc... |
| 3206 | * bacause we hide charges behind us. |
| 3207 | */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3208 | if (batch->nr_pages) |
| 3209 | res_counter_uncharge(&batch->memcg->res, |
| 3210 | batch->nr_pages * PAGE_SIZE); |
| 3211 | if (batch->memsw_nr_pages) |
| 3212 | res_counter_uncharge(&batch->memcg->memsw, |
| 3213 | batch->memsw_nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3214 | memcg_oom_recover(batch->memcg); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3215 | /* forget this pointer (for sanity check) */ |
| 3216 | batch->memcg = NULL; |
| 3217 | } |
| 3218 | |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3219 | #ifdef CONFIG_SWAP |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3220 | /* |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3221 | * called after __delete_from_swap_cache() and drop "page" account. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3222 | * memcg information is recorded to swap_cgroup of "ent" |
| 3223 | */ |
KAMEZAWA Hiroyuki | 8a9478ca | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3224 | void |
| 3225 | 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] | 3226 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3227 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 8a9478ca | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3228 | int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3229 | |
KAMEZAWA Hiroyuki | 8a9478ca | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3230 | if (!swapout) /* this was a swap cache but the swap is unused ! */ |
| 3231 | ctype = MEM_CGROUP_CHARGE_TYPE_DROP; |
| 3232 | |
| 3233 | memcg = __mem_cgroup_uncharge_common(page, ctype); |
| 3234 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3235 | /* |
| 3236 | * record memcg information, if swapout && memcg != NULL, |
| 3237 | * mem_cgroup_get() was called in uncharge(). |
| 3238 | */ |
| 3239 | if (do_swap_account && swapout && memcg) |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3240 | swap_cgroup_record(ent, css_id(&memcg->css)); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3241 | } |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3242 | #endif |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3243 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3244 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 3245 | /* |
| 3246 | * called from swap_entry_free(). remove record in swap_cgroup and |
| 3247 | * uncharge "memsw" account. |
| 3248 | */ |
| 3249 | void mem_cgroup_uncharge_swap(swp_entry_t ent) |
| 3250 | { |
| 3251 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3252 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3253 | |
| 3254 | if (!do_swap_account) |
| 3255 | return; |
| 3256 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3257 | id = swap_cgroup_record(ent, 0); |
| 3258 | rcu_read_lock(); |
| 3259 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3260 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3261 | /* |
| 3262 | * We uncharge this because swap is freed. |
| 3263 | * This memcg can be obsolete one. We avoid calling css_tryget |
| 3264 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3265 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 3266 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3267 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3268 | mem_cgroup_put(memcg); |
| 3269 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3270 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3271 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3272 | |
| 3273 | /** |
| 3274 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. |
| 3275 | * @entry: swap entry to be moved |
| 3276 | * @from: mem_cgroup which the entry is moved from |
| 3277 | * @to: mem_cgroup which the entry is moved to |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3278 | * @need_fixup: whether we should fixup res_counters and refcounts. |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3279 | * |
| 3280 | * It succeeds only when the swap_cgroup's record for this entry is the same |
| 3281 | * as the mem_cgroup's id of @from. |
| 3282 | * |
| 3283 | * Returns 0 on success, -EINVAL on failure. |
| 3284 | * |
| 3285 | * The caller must have charged to @to, IOW, called res_counter_charge() about |
| 3286 | * both res and memsw, and called css_get(). |
| 3287 | */ |
| 3288 | static int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3289 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3290 | { |
| 3291 | unsigned short old_id, new_id; |
| 3292 | |
| 3293 | old_id = css_id(&from->css); |
| 3294 | new_id = css_id(&to->css); |
| 3295 | |
| 3296 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3297 | mem_cgroup_swap_statistics(from, false); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3298 | mem_cgroup_swap_statistics(to, true); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3299 | /* |
| 3300 | * This function is only called from task migration context now. |
| 3301 | * It postpones res_counter and refcount handling till the end |
| 3302 | * of task migration(mem_cgroup_clear_mc()) for performance |
| 3303 | * improvement. But we cannot postpone mem_cgroup_get(to) |
| 3304 | * because if the process that has been moved to @to does |
| 3305 | * swap-in, the refcount of @to might be decreased to 0. |
| 3306 | */ |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3307 | mem_cgroup_get(to); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3308 | if (need_fixup) { |
| 3309 | if (!mem_cgroup_is_root(from)) |
| 3310 | res_counter_uncharge(&from->memsw, PAGE_SIZE); |
| 3311 | mem_cgroup_put(from); |
| 3312 | /* |
| 3313 | * we charged both to->res and to->memsw, so we should |
| 3314 | * uncharge to->res. |
| 3315 | */ |
| 3316 | if (!mem_cgroup_is_root(to)) |
| 3317 | res_counter_uncharge(&to->res, PAGE_SIZE); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3318 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3319 | return 0; |
| 3320 | } |
| 3321 | return -EINVAL; |
| 3322 | } |
| 3323 | #else |
| 3324 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3325 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3326 | { |
| 3327 | return -EINVAL; |
| 3328 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3329 | #endif |
| 3330 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3331 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3332 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
| 3333 | * page belongs to. |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3334 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3335 | int mem_cgroup_prepare_migration(struct page *page, |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 3336 | struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3337 | { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3338 | struct mem_cgroup *mem = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3339 | struct page_cgroup *pc; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3340 | enum charge_type ctype; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3341 | int ret = 0; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 3342 | |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 3343 | *ptr = NULL; |
| 3344 | |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 3345 | VM_BUG_ON(PageTransHuge(page)); |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3346 | if (mem_cgroup_disabled()) |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 3347 | return 0; |
| 3348 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3349 | pc = lookup_page_cgroup(page); |
| 3350 | lock_page_cgroup(pc); |
| 3351 | if (PageCgroupUsed(pc)) { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3352 | mem = pc->mem_cgroup; |
| 3353 | css_get(&mem->css); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3354 | /* |
| 3355 | * At migrating an anonymous page, its mapcount goes down |
| 3356 | * to 0 and uncharge() will be called. But, even if it's fully |
| 3357 | * unmapped, migration may fail and this page has to be |
| 3358 | * charged again. We set MIGRATION flag here and delay uncharge |
| 3359 | * until end_migration() is called |
| 3360 | * |
| 3361 | * Corner Case Thinking |
| 3362 | * A) |
| 3363 | * When the old page was mapped as Anon and it's unmap-and-freed |
| 3364 | * while migration was ongoing. |
| 3365 | * If unmap finds the old page, uncharge() of it will be delayed |
| 3366 | * until end_migration(). If unmap finds a new page, it's |
| 3367 | * uncharged when it make mapcount to be 1->0. If unmap code |
| 3368 | * finds swap_migration_entry, the new page will not be mapped |
| 3369 | * and end_migration() will find it(mapcount==0). |
| 3370 | * |
| 3371 | * B) |
| 3372 | * When the old page was mapped but migraion fails, the kernel |
| 3373 | * remaps it. A charge for it is kept by MIGRATION flag even |
| 3374 | * if mapcount goes down to 0. We can do remap successfully |
| 3375 | * without charging it again. |
| 3376 | * |
| 3377 | * C) |
| 3378 | * The "old" page is under lock_page() until the end of |
| 3379 | * migration, so, the old page itself will not be swapped-out. |
| 3380 | * If the new page is swapped out before end_migraton, our |
| 3381 | * hook to usual swap-out path will catch the event. |
| 3382 | */ |
| 3383 | if (PageAnon(page)) |
| 3384 | SetPageCgroupMigration(pc); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 3385 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3386 | unlock_page_cgroup(pc); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3387 | /* |
| 3388 | * If the page is not charged at this point, |
| 3389 | * we return here. |
| 3390 | */ |
| 3391 | if (!mem) |
| 3392 | return 0; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3393 | |
Andrea Arcangeli | 93d5c9b | 2010-04-23 13:17:39 -0400 | [diff] [blame] | 3394 | *ptr = mem; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3395 | 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] | 3396 | css_put(&mem->css);/* drop extra refcnt */ |
| 3397 | if (ret || *ptr == NULL) { |
| 3398 | if (PageAnon(page)) { |
| 3399 | lock_page_cgroup(pc); |
| 3400 | ClearPageCgroupMigration(pc); |
| 3401 | unlock_page_cgroup(pc); |
| 3402 | /* |
| 3403 | * The old page may be fully unmapped while we kept it. |
| 3404 | */ |
| 3405 | mem_cgroup_uncharge_page(page); |
| 3406 | } |
| 3407 | return -ENOMEM; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3408 | } |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3409 | /* |
| 3410 | * We charge new page before it's used/mapped. So, even if unlock_page() |
| 3411 | * is called before end_migration, we can catch all events on this new |
| 3412 | * page. In the case new page is migrated but not remapped, new page's |
| 3413 | * mapcount will be finally 0 and we call uncharge in end_migration(). |
| 3414 | */ |
| 3415 | pc = lookup_page_cgroup(newpage); |
| 3416 | if (PageAnon(page)) |
| 3417 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; |
| 3418 | else if (page_is_file_cache(page)) |
| 3419 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; |
| 3420 | else |
| 3421 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3422 | __mem_cgroup_commit_charge(mem, page, 1, pc, ctype); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3423 | return ret; |
| 3424 | } |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 3425 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3426 | /* remove redundant charge if migration failed*/ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3427 | void mem_cgroup_end_migration(struct mem_cgroup *mem, |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 3428 | struct page *oldpage, struct page *newpage, bool migration_ok) |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3429 | { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3430 | struct page *used, *unused; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3431 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3432 | |
| 3433 | if (!mem) |
| 3434 | return; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3435 | /* blocks rmdir() */ |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3436 | cgroup_exclude_rmdir(&mem->css); |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 3437 | if (!migration_ok) { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3438 | used = oldpage; |
| 3439 | unused = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3440 | } else { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3441 | used = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3442 | unused = oldpage; |
| 3443 | } |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3444 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3445 | * We disallowed uncharge of pages under migration because mapcount |
| 3446 | * of the page goes down to zero, temporarly. |
| 3447 | * Clear the flag and check the page should be charged. |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3448 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3449 | pc = lookup_page_cgroup(oldpage); |
| 3450 | lock_page_cgroup(pc); |
| 3451 | ClearPageCgroupMigration(pc); |
| 3452 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3453 | |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3454 | __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE); |
| 3455 | |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3456 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3457 | * If a page is a file cache, radix-tree replacement is very atomic |
| 3458 | * and we can skip this check. When it was an Anon page, its mapcount |
| 3459 | * goes down to 0. But because we added MIGRATION flage, it's not |
| 3460 | * uncharged yet. There are several case but page->mapcount check |
| 3461 | * and USED bit check in mem_cgroup_uncharge_page() will do enough |
| 3462 | * check. (see prepare_charge() also) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3463 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3464 | if (PageAnon(used)) |
| 3465 | mem_cgroup_uncharge_page(used); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3466 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3467 | * At migration, we may charge account against cgroup which has no |
| 3468 | * tasks. |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3469 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 3470 | * In that case, we need to call pre_destroy() again. check it here. |
| 3471 | */ |
| 3472 | cgroup_release_and_wakeup_rmdir(&mem->css); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3473 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 3474 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3475 | /* |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3476 | * A call to try to shrink memory usage on charge failure at shmem's swapin. |
| 3477 | * Calling hierarchical_reclaim is not enough because we should update |
| 3478 | * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM. |
| 3479 | * Moreover considering hierarchy, we should reclaim from the mem_over_limit, |
| 3480 | * not from the memcg which this page would be charged to. |
| 3481 | * try_charge_swapin does all of these works properly. |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3482 | */ |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3483 | int mem_cgroup_shmem_charge_fallback(struct page *page, |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 3484 | struct mm_struct *mm, |
| 3485 | gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3486 | { |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 3487 | struct mem_cgroup *mem; |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3488 | int ret; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3489 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3490 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 3491 | return 0; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3492 | |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3493 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
| 3494 | if (!ret) |
| 3495 | mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */ |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3496 | |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3497 | return ret; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3498 | } |
| 3499 | |
Daisuke Nishimura | f212ad7 | 2011-03-23 16:42:25 -0700 | [diff] [blame] | 3500 | #ifdef CONFIG_DEBUG_VM |
| 3501 | static struct page_cgroup *lookup_page_cgroup_used(struct page *page) |
| 3502 | { |
| 3503 | struct page_cgroup *pc; |
| 3504 | |
| 3505 | pc = lookup_page_cgroup(page); |
| 3506 | if (likely(pc) && PageCgroupUsed(pc)) |
| 3507 | return pc; |
| 3508 | return NULL; |
| 3509 | } |
| 3510 | |
| 3511 | bool mem_cgroup_bad_page_check(struct page *page) |
| 3512 | { |
| 3513 | if (mem_cgroup_disabled()) |
| 3514 | return false; |
| 3515 | |
| 3516 | return lookup_page_cgroup_used(page) != NULL; |
| 3517 | } |
| 3518 | |
| 3519 | void mem_cgroup_print_bad_page(struct page *page) |
| 3520 | { |
| 3521 | struct page_cgroup *pc; |
| 3522 | |
| 3523 | pc = lookup_page_cgroup_used(page); |
| 3524 | if (pc) { |
| 3525 | int ret = -1; |
| 3526 | char *path; |
| 3527 | |
| 3528 | printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p", |
| 3529 | pc, pc->flags, pc->mem_cgroup); |
| 3530 | |
| 3531 | path = kmalloc(PATH_MAX, GFP_KERNEL); |
| 3532 | if (path) { |
| 3533 | rcu_read_lock(); |
| 3534 | ret = cgroup_path(pc->mem_cgroup->css.cgroup, |
| 3535 | path, PATH_MAX); |
| 3536 | rcu_read_unlock(); |
| 3537 | } |
| 3538 | |
| 3539 | printk(KERN_CONT "(%s)\n", |
| 3540 | (ret < 0) ? "cannot get the path" : path); |
| 3541 | kfree(path); |
| 3542 | } |
| 3543 | } |
| 3544 | #endif |
| 3545 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3546 | static DEFINE_MUTEX(set_limit_mutex); |
| 3547 | |
KOSAKI Motohiro | d38d2a7 | 2009-01-06 14:39:44 -0800 | [diff] [blame] | 3548 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3549 | unsigned long long val) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3550 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3551 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3552 | u64 memswlimit, memlimit; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3553 | int ret = 0; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3554 | int children = mem_cgroup_count_children(memcg); |
| 3555 | u64 curusage, oldusage; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3556 | int enlarge; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3557 | |
| 3558 | /* |
| 3559 | * For keeping hierarchical_reclaim simple, how long we should retry |
| 3560 | * is depends on callers. We set our retry-count to be function |
| 3561 | * of # of children which we should visit in this loop. |
| 3562 | */ |
| 3563 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; |
| 3564 | |
| 3565 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3566 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3567 | enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3568 | while (retry_count) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3569 | if (signal_pending(current)) { |
| 3570 | ret = -EINTR; |
| 3571 | break; |
| 3572 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3573 | /* |
| 3574 | * Rather than hide all in some function, I do this in |
| 3575 | * open coded manner. You see what this really does. |
| 3576 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 3577 | */ |
| 3578 | mutex_lock(&set_limit_mutex); |
| 3579 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3580 | if (memswlimit < val) { |
| 3581 | ret = -EINVAL; |
| 3582 | mutex_unlock(&set_limit_mutex); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3583 | break; |
| 3584 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3585 | |
| 3586 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3587 | if (memlimit < val) |
| 3588 | enlarge = 1; |
| 3589 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3590 | ret = res_counter_set_limit(&memcg->res, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 3591 | if (!ret) { |
| 3592 | if (memswlimit == val) |
| 3593 | memcg->memsw_is_minimum = true; |
| 3594 | else |
| 3595 | memcg->memsw_is_minimum = false; |
| 3596 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3597 | mutex_unlock(&set_limit_mutex); |
| 3598 | |
| 3599 | if (!ret) |
| 3600 | break; |
| 3601 | |
Bob Liu | aa20d48 | 2009-12-15 16:47:14 -0800 | [diff] [blame] | 3602 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3603 | MEM_CGROUP_RECLAIM_SHRINK, |
| 3604 | NULL); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3605 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
| 3606 | /* Usage is reduced ? */ |
| 3607 | if (curusage >= oldusage) |
| 3608 | retry_count--; |
| 3609 | else |
| 3610 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3611 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3612 | if (!ret && enlarge) |
| 3613 | memcg_oom_recover(memcg); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 3614 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3615 | return ret; |
| 3616 | } |
| 3617 | |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 3618 | static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
| 3619 | unsigned long long val) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3620 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3621 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3622 | u64 memlimit, memswlimit, oldusage, curusage; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3623 | int children = mem_cgroup_count_children(memcg); |
| 3624 | int ret = -EBUSY; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3625 | int enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3626 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3627 | /* see mem_cgroup_resize_res_limit */ |
| 3628 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; |
| 3629 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3630 | while (retry_count) { |
| 3631 | if (signal_pending(current)) { |
| 3632 | ret = -EINTR; |
| 3633 | break; |
| 3634 | } |
| 3635 | /* |
| 3636 | * Rather than hide all in some function, I do this in |
| 3637 | * open coded manner. You see what this really does. |
| 3638 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 3639 | */ |
| 3640 | mutex_lock(&set_limit_mutex); |
| 3641 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3642 | if (memlimit > val) { |
| 3643 | ret = -EINVAL; |
| 3644 | mutex_unlock(&set_limit_mutex); |
| 3645 | break; |
| 3646 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3647 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3648 | if (memswlimit < val) |
| 3649 | enlarge = 1; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3650 | ret = res_counter_set_limit(&memcg->memsw, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 3651 | if (!ret) { |
| 3652 | if (memlimit == val) |
| 3653 | memcg->memsw_is_minimum = true; |
| 3654 | else |
| 3655 | memcg->memsw_is_minimum = false; |
| 3656 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3657 | mutex_unlock(&set_limit_mutex); |
| 3658 | |
| 3659 | if (!ret) |
| 3660 | break; |
| 3661 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3662 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 3663 | MEM_CGROUP_RECLAIM_NOSWAP | |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3664 | MEM_CGROUP_RECLAIM_SHRINK, |
| 3665 | NULL); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3666 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3667 | /* Usage is reduced ? */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3668 | if (curusage >= oldusage) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3669 | retry_count--; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3670 | else |
| 3671 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3672 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3673 | if (!ret && enlarge) |
| 3674 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3675 | return ret; |
| 3676 | } |
| 3677 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3678 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3679 | gfp_t gfp_mask, |
| 3680 | unsigned long *total_scanned) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3681 | { |
| 3682 | unsigned long nr_reclaimed = 0; |
| 3683 | struct mem_cgroup_per_zone *mz, *next_mz = NULL; |
| 3684 | unsigned long reclaimed; |
| 3685 | int loop = 0; |
| 3686 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3687 | unsigned long long excess; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3688 | unsigned long nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3689 | |
| 3690 | if (order > 0) |
| 3691 | return 0; |
| 3692 | |
KOSAKI Motohiro | 00918b6 | 2010-08-10 18:03:05 -0700 | [diff] [blame] | 3693 | 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] | 3694 | /* |
| 3695 | * This loop can run a while, specially if mem_cgroup's continuously |
| 3696 | * keep exceeding their soft limit and putting the system under |
| 3697 | * pressure |
| 3698 | */ |
| 3699 | do { |
| 3700 | if (next_mz) |
| 3701 | mz = next_mz; |
| 3702 | else |
| 3703 | mz = mem_cgroup_largest_soft_limit_node(mctz); |
| 3704 | if (!mz) |
| 3705 | break; |
| 3706 | |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3707 | nr_scanned = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3708 | reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone, |
| 3709 | gfp_mask, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3710 | MEM_CGROUP_RECLAIM_SOFT, |
| 3711 | &nr_scanned); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3712 | nr_reclaimed += reclaimed; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3713 | *total_scanned += nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3714 | spin_lock(&mctz->lock); |
| 3715 | |
| 3716 | /* |
| 3717 | * If we failed to reclaim anything from this memory cgroup |
| 3718 | * it is time to move on to the next cgroup |
| 3719 | */ |
| 3720 | next_mz = NULL; |
| 3721 | if (!reclaimed) { |
| 3722 | do { |
| 3723 | /* |
| 3724 | * Loop until we find yet another one. |
| 3725 | * |
| 3726 | * By the time we get the soft_limit lock |
| 3727 | * again, someone might have aded the |
| 3728 | * group back on the RB tree. Iterate to |
| 3729 | * make sure we get a different mem. |
| 3730 | * mem_cgroup_largest_soft_limit_node returns |
| 3731 | * NULL if no other cgroup is present on |
| 3732 | * the tree |
| 3733 | */ |
| 3734 | next_mz = |
| 3735 | __mem_cgroup_largest_soft_limit_node(mctz); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 3736 | if (next_mz == mz) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3737 | css_put(&next_mz->mem->css); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 3738 | else /* next_mz == NULL or other memcg */ |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3739 | break; |
| 3740 | } while (1); |
| 3741 | } |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3742 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3743 | excess = res_counter_soft_limit_excess(&mz->mem->res); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3744 | /* |
| 3745 | * One school of thought says that we should not add |
| 3746 | * back the node to the tree if reclaim returns 0. |
| 3747 | * But our reclaim could return 0, simply because due |
| 3748 | * to priority we are exposing a smaller subset of |
| 3749 | * memory to reclaim from. Consider this as a longer |
| 3750 | * term TODO. |
| 3751 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3752 | /* If excess == 0, no tree ops */ |
| 3753 | __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3754 | spin_unlock(&mctz->lock); |
| 3755 | css_put(&mz->mem->css); |
| 3756 | loop++; |
| 3757 | /* |
| 3758 | * Could not reclaim anything and there are no more |
| 3759 | * mem cgroups to try or we seem to be looping without |
| 3760 | * reclaiming anything. |
| 3761 | */ |
| 3762 | if (!nr_reclaimed && |
| 3763 | (next_mz == NULL || |
| 3764 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) |
| 3765 | break; |
| 3766 | } while (!nr_reclaimed); |
| 3767 | if (next_mz) |
| 3768 | css_put(&next_mz->mem->css); |
| 3769 | return nr_reclaimed; |
| 3770 | } |
| 3771 | |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3772 | /* |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3773 | * This routine traverse page_cgroup in given list and drop them all. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3774 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
| 3775 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3776 | static int mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3777 | int node, int zid, enum lru_list lru) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3778 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3779 | struct zone *zone; |
| 3780 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3781 | struct page_cgroup *pc, *busy; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3782 | unsigned long flags, loop; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3783 | struct list_head *list; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3784 | int ret = 0; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3785 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3786 | zone = &NODE_DATA(node)->node_zones[zid]; |
| 3787 | mz = mem_cgroup_zoneinfo(mem, node, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3788 | list = &mz->lists[lru]; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3789 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3790 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
| 3791 | /* give some margin against EBUSY etc...*/ |
| 3792 | loop += 256; |
| 3793 | busy = NULL; |
| 3794 | while (loop--) { |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3795 | struct page *page; |
| 3796 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3797 | ret = 0; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3798 | spin_lock_irqsave(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3799 | if (list_empty(list)) { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3800 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3801 | break; |
| 3802 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3803 | pc = list_entry(list->prev, struct page_cgroup, lru); |
| 3804 | if (busy == pc) { |
| 3805 | list_move(&pc->lru, list); |
Thiago Farina | 648bcc7 | 2010-03-05 13:42:04 -0800 | [diff] [blame] | 3806 | busy = NULL; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3807 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3808 | continue; |
| 3809 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3810 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3811 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 3812 | page = lookup_cgroup_page(pc); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3813 | |
| 3814 | ret = mem_cgroup_move_parent(page, pc, mem, GFP_KERNEL); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3815 | if (ret == -ENOMEM) |
| 3816 | break; |
| 3817 | |
| 3818 | if (ret == -EBUSY || ret == -EINVAL) { |
| 3819 | /* found lock contention or "pc" is obsolete. */ |
| 3820 | busy = pc; |
| 3821 | cond_resched(); |
| 3822 | } else |
| 3823 | busy = NULL; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3824 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3825 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3826 | if (!ret && !list_empty(list)) |
| 3827 | return -EBUSY; |
| 3828 | return ret; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3829 | } |
| 3830 | |
| 3831 | /* |
| 3832 | * make mem_cgroup's charge to be 0 if there is no task. |
| 3833 | * This enables deleting this mem_cgroup. |
| 3834 | */ |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3835 | 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] | 3836 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3837 | int ret; |
| 3838 | int node, zid, shrink; |
| 3839 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3840 | struct cgroup *cgrp = mem->css.cgroup; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 3841 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3842 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3843 | |
| 3844 | shrink = 0; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3845 | /* should free all ? */ |
| 3846 | if (free_all) |
| 3847 | goto try_to_free; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3848 | move_account: |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3849 | do { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3850 | ret = -EBUSY; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3851 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3852 | goto out; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3853 | ret = -EINTR; |
| 3854 | if (signal_pending(current)) |
| 3855 | goto out; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3856 | /* This is for making all *used* pages to be on LRU. */ |
| 3857 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 3858 | drain_all_stock_sync(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3859 | ret = 0; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 3860 | mem_cgroup_start_move(mem); |
KAMEZAWA Hiroyuki | 299b4ea | 2009-01-29 14:25:17 -0800 | [diff] [blame] | 3861 | for_each_node_state(node, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3862 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3863 | enum lru_list l; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3864 | for_each_lru(l) { |
| 3865 | ret = mem_cgroup_force_empty_list(mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3866 | node, zid, l); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3867 | if (ret) |
| 3868 | break; |
| 3869 | } |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 3870 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3871 | if (ret) |
| 3872 | break; |
| 3873 | } |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 3874 | mem_cgroup_end_move(mem); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3875 | memcg_oom_recover(mem); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3876 | /* it seems parent cgroup doesn't have enough mem */ |
| 3877 | if (ret == -ENOMEM) |
| 3878 | goto try_to_free; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3879 | cond_resched(); |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3880 | /* "ret" should also be checked to ensure all lists are empty. */ |
| 3881 | } while (mem->res.usage > 0 || ret); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3882 | out: |
| 3883 | css_put(&mem->css); |
| 3884 | return ret; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3885 | |
| 3886 | try_to_free: |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3887 | /* returns EBUSY if there is a task or if we come here twice. */ |
| 3888 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3889 | ret = -EBUSY; |
| 3890 | goto out; |
| 3891 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3892 | /* we call try-to-free pages for make this cgroup empty */ |
| 3893 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3894 | /* try to free all pages in this cgroup */ |
| 3895 | shrink = 1; |
| 3896 | while (nr_retries && mem->res.usage > 0) { |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame^] | 3897 | struct memcg_scanrecord rec; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3898 | int progress; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3899 | |
| 3900 | if (signal_pending(current)) { |
| 3901 | ret = -EINTR; |
| 3902 | goto out; |
| 3903 | } |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame^] | 3904 | rec.context = SCAN_BY_SHRINK; |
| 3905 | rec.mem = mem; |
| 3906 | rec.root = mem; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 3907 | progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL, |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame^] | 3908 | false, &rec); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3909 | if (!progress) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3910 | nr_retries--; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3911 | /* maybe some writeback is necessary */ |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 3912 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3913 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3914 | |
| 3915 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3916 | lru_add_drain(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3917 | /* try move_account...there may be some *locked* pages. */ |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3918 | goto move_account; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3919 | } |
| 3920 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3921 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
| 3922 | { |
| 3923 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); |
| 3924 | } |
| 3925 | |
| 3926 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3927 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
| 3928 | { |
| 3929 | return mem_cgroup_from_cont(cont)->use_hierarchy; |
| 3930 | } |
| 3931 | |
| 3932 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, |
| 3933 | u64 val) |
| 3934 | { |
| 3935 | int retval = 0; |
| 3936 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 3937 | struct cgroup *parent = cont->parent; |
| 3938 | struct mem_cgroup *parent_mem = NULL; |
| 3939 | |
| 3940 | if (parent) |
| 3941 | parent_mem = mem_cgroup_from_cont(parent); |
| 3942 | |
| 3943 | cgroup_lock(); |
| 3944 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 3945 | * 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] | 3946 | * in the child subtrees. If it is unset, then the change can |
| 3947 | * occur, provided the current cgroup has no children. |
| 3948 | * |
| 3949 | * For the root cgroup, parent_mem is NULL, we allow value to be |
| 3950 | * set if there are no children. |
| 3951 | */ |
| 3952 | if ((!parent_mem || !parent_mem->use_hierarchy) && |
| 3953 | (val == 1 || val == 0)) { |
| 3954 | if (list_empty(&cont->children)) |
| 3955 | mem->use_hierarchy = val; |
| 3956 | else |
| 3957 | retval = -EBUSY; |
| 3958 | } else |
| 3959 | retval = -EINVAL; |
| 3960 | cgroup_unlock(); |
| 3961 | |
| 3962 | return retval; |
| 3963 | } |
| 3964 | |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3965 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3966 | static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *mem, |
| 3967 | enum mem_cgroup_stat_index idx) |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3968 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3969 | struct mem_cgroup *iter; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3970 | long val = 0; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3971 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3972 | /* Per-cpu values can be negative, use a signed accumulator */ |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3973 | for_each_mem_cgroup_tree(iter, mem) |
| 3974 | val += mem_cgroup_read_stat(iter, idx); |
| 3975 | |
| 3976 | if (val < 0) /* race ? */ |
| 3977 | val = 0; |
| 3978 | return val; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3979 | } |
| 3980 | |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3981 | static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap) |
| 3982 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3983 | u64 val; |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3984 | |
| 3985 | if (!mem_cgroup_is_root(mem)) { |
| 3986 | if (!swap) |
| 3987 | return res_counter_read_u64(&mem->res, RES_USAGE); |
| 3988 | else |
| 3989 | return res_counter_read_u64(&mem->memsw, RES_USAGE); |
| 3990 | } |
| 3991 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3992 | val = mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_CACHE); |
| 3993 | val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_RSS); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3994 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3995 | if (swap) |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3996 | val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_SWAPOUT); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3997 | |
| 3998 | return val << PAGE_SHIFT; |
| 3999 | } |
| 4000 | |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4001 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4002 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4003 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4004 | u64 val; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4005 | int type, name; |
| 4006 | |
| 4007 | type = MEMFILE_TYPE(cft->private); |
| 4008 | name = MEMFILE_ATTR(cft->private); |
| 4009 | switch (type) { |
| 4010 | case _MEM: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4011 | if (name == RES_USAGE) |
| 4012 | val = mem_cgroup_usage(mem, false); |
| 4013 | else |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 4014 | val = res_counter_read_u64(&mem->res, name); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4015 | break; |
| 4016 | case _MEMSWAP: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4017 | if (name == RES_USAGE) |
| 4018 | val = mem_cgroup_usage(mem, true); |
| 4019 | else |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 4020 | val = res_counter_read_u64(&mem->memsw, name); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4021 | break; |
| 4022 | default: |
| 4023 | BUG(); |
| 4024 | break; |
| 4025 | } |
| 4026 | return val; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4027 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4028 | /* |
| 4029 | * The user of this function is... |
| 4030 | * RES_LIMIT. |
| 4031 | */ |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 4032 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 4033 | const char *buffer) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4034 | { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4035 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4036 | int type, name; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4037 | unsigned long long val; |
| 4038 | int ret; |
| 4039 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4040 | type = MEMFILE_TYPE(cft->private); |
| 4041 | name = MEMFILE_ATTR(cft->private); |
| 4042 | switch (name) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4043 | case RES_LIMIT: |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 4044 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
| 4045 | ret = -EINVAL; |
| 4046 | break; |
| 4047 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4048 | /* This function does all necessary parse...reuse it */ |
| 4049 | ret = res_counter_memparse_write_strategy(buffer, &val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4050 | if (ret) |
| 4051 | break; |
| 4052 | if (type == _MEM) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4053 | ret = mem_cgroup_resize_limit(memcg, val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4054 | else |
| 4055 | ret = mem_cgroup_resize_memsw_limit(memcg, val); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4056 | break; |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 4057 | case RES_SOFT_LIMIT: |
| 4058 | ret = res_counter_memparse_write_strategy(buffer, &val); |
| 4059 | if (ret) |
| 4060 | break; |
| 4061 | /* |
| 4062 | * For memsw, soft limits are hard to implement in terms |
| 4063 | * of semantics, for now, we support soft limits for |
| 4064 | * control without swap |
| 4065 | */ |
| 4066 | if (type == _MEM) |
| 4067 | ret = res_counter_set_soft_limit(&memcg->res, val); |
| 4068 | else |
| 4069 | ret = -EINVAL; |
| 4070 | break; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4071 | default: |
| 4072 | ret = -EINVAL; /* should be BUG() ? */ |
| 4073 | break; |
| 4074 | } |
| 4075 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4076 | } |
| 4077 | |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 4078 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
| 4079 | unsigned long long *mem_limit, unsigned long long *memsw_limit) |
| 4080 | { |
| 4081 | struct cgroup *cgroup; |
| 4082 | unsigned long long min_limit, min_memsw_limit, tmp; |
| 4083 | |
| 4084 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 4085 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 4086 | cgroup = memcg->css.cgroup; |
| 4087 | if (!memcg->use_hierarchy) |
| 4088 | goto out; |
| 4089 | |
| 4090 | while (cgroup->parent) { |
| 4091 | cgroup = cgroup->parent; |
| 4092 | memcg = mem_cgroup_from_cont(cgroup); |
| 4093 | if (!memcg->use_hierarchy) |
| 4094 | break; |
| 4095 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 4096 | min_limit = min(min_limit, tmp); |
| 4097 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 4098 | min_memsw_limit = min(min_memsw_limit, tmp); |
| 4099 | } |
| 4100 | out: |
| 4101 | *mem_limit = min_limit; |
| 4102 | *memsw_limit = min_memsw_limit; |
| 4103 | return; |
| 4104 | } |
| 4105 | |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4106 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4107 | { |
| 4108 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4109 | int type, name; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4110 | |
| 4111 | mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4112 | type = MEMFILE_TYPE(event); |
| 4113 | name = MEMFILE_ATTR(event); |
| 4114 | switch (name) { |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4115 | case RES_MAX_USAGE: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4116 | if (type == _MEM) |
| 4117 | res_counter_reset_max(&mem->res); |
| 4118 | else |
| 4119 | res_counter_reset_max(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4120 | break; |
| 4121 | case RES_FAILCNT: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4122 | if (type == _MEM) |
| 4123 | res_counter_reset_failcnt(&mem->res); |
| 4124 | else |
| 4125 | res_counter_reset_failcnt(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4126 | break; |
| 4127 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4128 | |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 4129 | return 0; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4130 | } |
| 4131 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4132 | static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp, |
| 4133 | struct cftype *cft) |
| 4134 | { |
| 4135 | return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate; |
| 4136 | } |
| 4137 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4138 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4139 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 4140 | struct cftype *cft, u64 val) |
| 4141 | { |
| 4142 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4143 | |
| 4144 | if (val >= (1 << NR_MOVE_TYPE)) |
| 4145 | return -EINVAL; |
| 4146 | /* |
| 4147 | * We check this value several times in both in can_attach() and |
| 4148 | * attach(), so we need cgroup lock to prevent this value from being |
| 4149 | * inconsistent. |
| 4150 | */ |
| 4151 | cgroup_lock(); |
| 4152 | mem->move_charge_at_immigrate = val; |
| 4153 | cgroup_unlock(); |
| 4154 | |
| 4155 | return 0; |
| 4156 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4157 | #else |
| 4158 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 4159 | struct cftype *cft, u64 val) |
| 4160 | { |
| 4161 | return -ENOSYS; |
| 4162 | } |
| 4163 | #endif |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4164 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4165 | |
| 4166 | /* For read statistics */ |
| 4167 | enum { |
| 4168 | MCS_CACHE, |
| 4169 | MCS_RSS, |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 4170 | MCS_FILE_MAPPED, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4171 | MCS_PGPGIN, |
| 4172 | MCS_PGPGOUT, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4173 | MCS_SWAP, |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4174 | MCS_PGFAULT, |
| 4175 | MCS_PGMAJFAULT, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4176 | MCS_INACTIVE_ANON, |
| 4177 | MCS_ACTIVE_ANON, |
| 4178 | MCS_INACTIVE_FILE, |
| 4179 | MCS_ACTIVE_FILE, |
| 4180 | MCS_UNEVICTABLE, |
| 4181 | NR_MCS_STAT, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4182 | }; |
| 4183 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4184 | struct mcs_total_stat { |
| 4185 | s64 stat[NR_MCS_STAT]; |
| 4186 | }; |
| 4187 | |
| 4188 | struct { |
| 4189 | char *local_name; |
| 4190 | char *total_name; |
| 4191 | } memcg_stat_strings[NR_MCS_STAT] = { |
| 4192 | {"cache", "total_cache"}, |
| 4193 | {"rss", "total_rss"}, |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 4194 | {"mapped_file", "total_mapped_file"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4195 | {"pgpgin", "total_pgpgin"}, |
| 4196 | {"pgpgout", "total_pgpgout"}, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4197 | {"swap", "total_swap"}, |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4198 | {"pgfault", "total_pgfault"}, |
| 4199 | {"pgmajfault", "total_pgmajfault"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4200 | {"inactive_anon", "total_inactive_anon"}, |
| 4201 | {"active_anon", "total_active_anon"}, |
| 4202 | {"inactive_file", "total_inactive_file"}, |
| 4203 | {"active_file", "total_active_file"}, |
| 4204 | {"unevictable", "total_unevictable"} |
| 4205 | }; |
| 4206 | |
| 4207 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4208 | static void |
| 4209 | 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] | 4210 | { |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4211 | s64 val; |
| 4212 | |
| 4213 | /* per cpu stat */ |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4214 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4215 | s->stat[MCS_CACHE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4216 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4217 | s->stat[MCS_RSS] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4218 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED); |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 4219 | s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 4220 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGIN); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4221 | s->stat[MCS_PGPGIN] += val; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 4222 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGOUT); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4223 | s->stat[MCS_PGPGOUT] += val; |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4224 | if (do_swap_account) { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4225 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4226 | s->stat[MCS_SWAP] += val * PAGE_SIZE; |
| 4227 | } |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4228 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGFAULT); |
| 4229 | s->stat[MCS_PGFAULT] += val; |
| 4230 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGMAJFAULT); |
| 4231 | s->stat[MCS_PGMAJFAULT] += val; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4232 | |
| 4233 | /* per zone stat */ |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4234 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_ANON)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4235 | s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4236 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_ANON)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4237 | s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4238 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_FILE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4239 | s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4240 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_FILE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4241 | s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4242 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_UNEVICTABLE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4243 | s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4244 | } |
| 4245 | |
| 4246 | static void |
| 4247 | mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s) |
| 4248 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4249 | struct mem_cgroup *iter; |
| 4250 | |
| 4251 | for_each_mem_cgroup_tree(iter, mem) |
| 4252 | mem_cgroup_get_local_stat(iter, s); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4253 | } |
| 4254 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4255 | #ifdef CONFIG_NUMA |
| 4256 | static int mem_control_numa_stat_show(struct seq_file *m, void *arg) |
| 4257 | { |
| 4258 | int nid; |
| 4259 | unsigned long total_nr, file_nr, anon_nr, unevictable_nr; |
| 4260 | unsigned long node_nr; |
| 4261 | struct cgroup *cont = m->private; |
| 4262 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
| 4263 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4264 | total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4265 | seq_printf(m, "total=%lu", total_nr); |
| 4266 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4267 | 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] | 4268 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4269 | } |
| 4270 | seq_putc(m, '\n'); |
| 4271 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4272 | file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4273 | seq_printf(m, "file=%lu", file_nr); |
| 4274 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4275 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4276 | LRU_ALL_FILE); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4277 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4278 | } |
| 4279 | seq_putc(m, '\n'); |
| 4280 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4281 | anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4282 | seq_printf(m, "anon=%lu", anon_nr); |
| 4283 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4284 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4285 | LRU_ALL_ANON); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4286 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4287 | } |
| 4288 | seq_putc(m, '\n'); |
| 4289 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4290 | unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE)); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4291 | seq_printf(m, "unevictable=%lu", unevictable_nr); |
| 4292 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4293 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4294 | BIT(LRU_UNEVICTABLE)); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4295 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4296 | } |
| 4297 | seq_putc(m, '\n'); |
| 4298 | return 0; |
| 4299 | } |
| 4300 | #endif /* CONFIG_NUMA */ |
| 4301 | |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 4302 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
| 4303 | struct cgroup_map_cb *cb) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4304 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4305 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4306 | struct mcs_total_stat mystat; |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4307 | int i; |
| 4308 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4309 | memset(&mystat, 0, sizeof(mystat)); |
| 4310 | mem_cgroup_get_local_stat(mem_cont, &mystat); |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4311 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4312 | |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4313 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 4314 | if (i == MCS_SWAP && !do_swap_account) |
| 4315 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4316 | cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4317 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4318 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4319 | /* Hierarchical information */ |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 4320 | { |
| 4321 | unsigned long long limit, memsw_limit; |
| 4322 | memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit); |
| 4323 | cb->fill(cb, "hierarchical_memory_limit", limit); |
| 4324 | if (do_swap_account) |
| 4325 | cb->fill(cb, "hierarchical_memsw_limit", memsw_limit); |
| 4326 | } |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4327 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4328 | memset(&mystat, 0, sizeof(mystat)); |
| 4329 | mem_cgroup_get_total_stat(mem_cont, &mystat); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4330 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 4331 | if (i == MCS_SWAP && !do_swap_account) |
| 4332 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4333 | cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4334 | } |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4335 | |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4336 | #ifdef CONFIG_DEBUG_VM |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 4337 | cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL)); |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4338 | |
| 4339 | { |
| 4340 | int nid, zid; |
| 4341 | struct mem_cgroup_per_zone *mz; |
| 4342 | unsigned long recent_rotated[2] = {0, 0}; |
| 4343 | unsigned long recent_scanned[2] = {0, 0}; |
| 4344 | |
| 4345 | for_each_online_node(nid) |
| 4346 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 4347 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
| 4348 | |
| 4349 | recent_rotated[0] += |
| 4350 | mz->reclaim_stat.recent_rotated[0]; |
| 4351 | recent_rotated[1] += |
| 4352 | mz->reclaim_stat.recent_rotated[1]; |
| 4353 | recent_scanned[0] += |
| 4354 | mz->reclaim_stat.recent_scanned[0]; |
| 4355 | recent_scanned[1] += |
| 4356 | mz->reclaim_stat.recent_scanned[1]; |
| 4357 | } |
| 4358 | cb->fill(cb, "recent_rotated_anon", recent_rotated[0]); |
| 4359 | cb->fill(cb, "recent_rotated_file", recent_rotated[1]); |
| 4360 | cb->fill(cb, "recent_scanned_anon", recent_scanned[0]); |
| 4361 | cb->fill(cb, "recent_scanned_file", recent_scanned[1]); |
| 4362 | } |
| 4363 | #endif |
| 4364 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4365 | return 0; |
| 4366 | } |
| 4367 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4368 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
| 4369 | { |
| 4370 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4371 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 4372 | return mem_cgroup_swappiness(memcg); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4373 | } |
| 4374 | |
| 4375 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, |
| 4376 | u64 val) |
| 4377 | { |
| 4378 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4379 | struct mem_cgroup *parent; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4380 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4381 | if (val > 100) |
| 4382 | return -EINVAL; |
| 4383 | |
| 4384 | if (cgrp->parent == NULL) |
| 4385 | return -EINVAL; |
| 4386 | |
| 4387 | parent = mem_cgroup_from_cont(cgrp->parent); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4388 | |
| 4389 | cgroup_lock(); |
| 4390 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4391 | /* If under hierarchy, only empty-root can set this value */ |
| 4392 | if ((parent->use_hierarchy) || |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4393 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
| 4394 | cgroup_unlock(); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4395 | return -EINVAL; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4396 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4397 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4398 | memcg->swappiness = val; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4399 | |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4400 | cgroup_unlock(); |
| 4401 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4402 | return 0; |
| 4403 | } |
| 4404 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4405 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) |
| 4406 | { |
| 4407 | struct mem_cgroup_threshold_ary *t; |
| 4408 | u64 usage; |
| 4409 | int i; |
| 4410 | |
| 4411 | rcu_read_lock(); |
| 4412 | if (!swap) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4413 | t = rcu_dereference(memcg->thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4414 | else |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4415 | t = rcu_dereference(memcg->memsw_thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4416 | |
| 4417 | if (!t) |
| 4418 | goto unlock; |
| 4419 | |
| 4420 | usage = mem_cgroup_usage(memcg, swap); |
| 4421 | |
| 4422 | /* |
| 4423 | * current_threshold points to threshold just below usage. |
| 4424 | * If it's not true, a threshold was crossed after last |
| 4425 | * call of __mem_cgroup_threshold(). |
| 4426 | */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4427 | i = t->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4428 | |
| 4429 | /* |
| 4430 | * Iterate backward over array of thresholds starting from |
| 4431 | * current_threshold and check if a threshold is crossed. |
| 4432 | * If none of thresholds below usage is crossed, we read |
| 4433 | * only one element of the array here. |
| 4434 | */ |
| 4435 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) |
| 4436 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4437 | |
| 4438 | /* i = current_threshold + 1 */ |
| 4439 | i++; |
| 4440 | |
| 4441 | /* |
| 4442 | * Iterate forward over array of thresholds starting from |
| 4443 | * current_threshold+1 and check if a threshold is crossed. |
| 4444 | * If none of thresholds above usage is crossed, we read |
| 4445 | * only one element of the array here. |
| 4446 | */ |
| 4447 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) |
| 4448 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4449 | |
| 4450 | /* Update current_threshold */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4451 | t->current_threshold = i - 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4452 | unlock: |
| 4453 | rcu_read_unlock(); |
| 4454 | } |
| 4455 | |
| 4456 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) |
| 4457 | { |
Kirill A. Shutemov | ad4ca5f | 2010-10-07 12:59:27 -0700 | [diff] [blame] | 4458 | while (memcg) { |
| 4459 | __mem_cgroup_threshold(memcg, false); |
| 4460 | if (do_swap_account) |
| 4461 | __mem_cgroup_threshold(memcg, true); |
| 4462 | |
| 4463 | memcg = parent_mem_cgroup(memcg); |
| 4464 | } |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4465 | } |
| 4466 | |
| 4467 | static int compare_thresholds(const void *a, const void *b) |
| 4468 | { |
| 4469 | const struct mem_cgroup_threshold *_a = a; |
| 4470 | const struct mem_cgroup_threshold *_b = b; |
| 4471 | |
| 4472 | return _a->threshold - _b->threshold; |
| 4473 | } |
| 4474 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4475 | static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4476 | { |
| 4477 | struct mem_cgroup_eventfd_list *ev; |
| 4478 | |
| 4479 | list_for_each_entry(ev, &mem->oom_notify, list) |
| 4480 | eventfd_signal(ev->eventfd, 1); |
| 4481 | return 0; |
| 4482 | } |
| 4483 | |
| 4484 | static void mem_cgroup_oom_notify(struct mem_cgroup *mem) |
| 4485 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4486 | struct mem_cgroup *iter; |
| 4487 | |
| 4488 | for_each_mem_cgroup_tree(iter, mem) |
| 4489 | mem_cgroup_oom_notify_cb(iter); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | static int mem_cgroup_usage_register_event(struct cgroup *cgrp, |
| 4493 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4494 | { |
| 4495 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4496 | struct mem_cgroup_thresholds *thresholds; |
| 4497 | struct mem_cgroup_threshold_ary *new; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4498 | int type = MEMFILE_TYPE(cft->private); |
| 4499 | u64 threshold, usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4500 | int i, size, ret; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4501 | |
| 4502 | ret = res_counter_memparse_write_strategy(args, &threshold); |
| 4503 | if (ret) |
| 4504 | return ret; |
| 4505 | |
| 4506 | mutex_lock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4507 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4508 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4509 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4510 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4511 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4512 | else |
| 4513 | BUG(); |
| 4514 | |
| 4515 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 4516 | |
| 4517 | /* Check if a threshold crossed before adding a new one */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4518 | if (thresholds->primary) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4519 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4520 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4521 | size = thresholds->primary ? thresholds->primary->size + 1 : 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4522 | |
| 4523 | /* Allocate memory for new array of thresholds */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4524 | new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4525 | GFP_KERNEL); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4526 | if (!new) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4527 | ret = -ENOMEM; |
| 4528 | goto unlock; |
| 4529 | } |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4530 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4531 | |
| 4532 | /* Copy thresholds (if any) to new array */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4533 | if (thresholds->primary) { |
| 4534 | memcpy(new->entries, thresholds->primary->entries, (size - 1) * |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4535 | sizeof(struct mem_cgroup_threshold)); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4536 | } |
| 4537 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4538 | /* Add new threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4539 | new->entries[size - 1].eventfd = eventfd; |
| 4540 | new->entries[size - 1].threshold = threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4541 | |
| 4542 | /* Sort thresholds. Registering of new threshold isn't time-critical */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4543 | sort(new->entries, size, sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4544 | compare_thresholds, NULL); |
| 4545 | |
| 4546 | /* Find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4547 | new->current_threshold = -1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4548 | for (i = 0; i < size; i++) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4549 | if (new->entries[i].threshold < usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4550 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4551 | * new->current_threshold will not be used until |
| 4552 | * rcu_assign_pointer(), so it's safe to increment |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4553 | * it here. |
| 4554 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4555 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4556 | } |
| 4557 | } |
| 4558 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4559 | /* Free old spare buffer and save old primary buffer as spare */ |
| 4560 | kfree(thresholds->spare); |
| 4561 | thresholds->spare = thresholds->primary; |
| 4562 | |
| 4563 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4564 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4565 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4566 | synchronize_rcu(); |
| 4567 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4568 | unlock: |
| 4569 | mutex_unlock(&memcg->thresholds_lock); |
| 4570 | |
| 4571 | return ret; |
| 4572 | } |
| 4573 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4574 | static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4575 | struct cftype *cft, struct eventfd_ctx *eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4576 | { |
| 4577 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4578 | struct mem_cgroup_thresholds *thresholds; |
| 4579 | struct mem_cgroup_threshold_ary *new; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4580 | int type = MEMFILE_TYPE(cft->private); |
| 4581 | u64 usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4582 | int i, j, size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4583 | |
| 4584 | mutex_lock(&memcg->thresholds_lock); |
| 4585 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4586 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4587 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4588 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4589 | else |
| 4590 | BUG(); |
| 4591 | |
| 4592 | /* |
| 4593 | * Something went wrong if we trying to unregister a threshold |
| 4594 | * if we don't have thresholds |
| 4595 | */ |
| 4596 | BUG_ON(!thresholds); |
| 4597 | |
| 4598 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 4599 | |
| 4600 | /* Check if a threshold crossed before removing */ |
| 4601 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4602 | |
| 4603 | /* Calculate new number of threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4604 | size = 0; |
| 4605 | for (i = 0; i < thresholds->primary->size; i++) { |
| 4606 | if (thresholds->primary->entries[i].eventfd != eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4607 | size++; |
| 4608 | } |
| 4609 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4610 | new = thresholds->spare; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4611 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4612 | /* Set thresholds array to NULL if we don't have thresholds */ |
| 4613 | if (!size) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4614 | kfree(new); |
| 4615 | new = NULL; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4616 | goto swap_buffers; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4617 | } |
| 4618 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4619 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4620 | |
| 4621 | /* Copy thresholds and find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4622 | new->current_threshold = -1; |
| 4623 | for (i = 0, j = 0; i < thresholds->primary->size; i++) { |
| 4624 | if (thresholds->primary->entries[i].eventfd == eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4625 | continue; |
| 4626 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4627 | new->entries[j] = thresholds->primary->entries[i]; |
| 4628 | if (new->entries[j].threshold < usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4629 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4630 | * new->current_threshold will not be used |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4631 | * until rcu_assign_pointer(), so it's safe to increment |
| 4632 | * it here. |
| 4633 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4634 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4635 | } |
| 4636 | j++; |
| 4637 | } |
| 4638 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4639 | swap_buffers: |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4640 | /* Swap primary and spare array */ |
| 4641 | thresholds->spare = thresholds->primary; |
| 4642 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4643 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4644 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4645 | synchronize_rcu(); |
| 4646 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4647 | mutex_unlock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4648 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4649 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4650 | static int mem_cgroup_oom_register_event(struct cgroup *cgrp, |
| 4651 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
| 4652 | { |
| 4653 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4654 | struct mem_cgroup_eventfd_list *event; |
| 4655 | int type = MEMFILE_TYPE(cft->private); |
| 4656 | |
| 4657 | BUG_ON(type != _OOM_TYPE); |
| 4658 | event = kmalloc(sizeof(*event), GFP_KERNEL); |
| 4659 | if (!event) |
| 4660 | return -ENOMEM; |
| 4661 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4662 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4663 | |
| 4664 | event->eventfd = eventfd; |
| 4665 | list_add(&event->list, &memcg->oom_notify); |
| 4666 | |
| 4667 | /* already in OOM ? */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 4668 | if (atomic_read(&memcg->under_oom)) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4669 | eventfd_signal(eventfd, 1); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4670 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4671 | |
| 4672 | return 0; |
| 4673 | } |
| 4674 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4675 | static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4676 | struct cftype *cft, struct eventfd_ctx *eventfd) |
| 4677 | { |
| 4678 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4679 | struct mem_cgroup_eventfd_list *ev, *tmp; |
| 4680 | int type = MEMFILE_TYPE(cft->private); |
| 4681 | |
| 4682 | BUG_ON(type != _OOM_TYPE); |
| 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 | list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) { |
| 4687 | if (ev->eventfd == eventfd) { |
| 4688 | list_del(&ev->list); |
| 4689 | kfree(ev); |
| 4690 | } |
| 4691 | } |
| 4692 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4693 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4694 | } |
| 4695 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4696 | static int mem_cgroup_oom_control_read(struct cgroup *cgrp, |
| 4697 | struct cftype *cft, struct cgroup_map_cb *cb) |
| 4698 | { |
| 4699 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4700 | |
| 4701 | cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable); |
| 4702 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 4703 | if (atomic_read(&mem->under_oom)) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4704 | cb->fill(cb, "under_oom", 1); |
| 4705 | else |
| 4706 | cb->fill(cb, "under_oom", 0); |
| 4707 | return 0; |
| 4708 | } |
| 4709 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4710 | static int mem_cgroup_oom_control_write(struct cgroup *cgrp, |
| 4711 | struct cftype *cft, u64 val) |
| 4712 | { |
| 4713 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4714 | struct mem_cgroup *parent; |
| 4715 | |
| 4716 | /* cannot set to root cgroup and only 0 and 1 are allowed */ |
| 4717 | if (!cgrp->parent || !((val == 0) || (val == 1))) |
| 4718 | return -EINVAL; |
| 4719 | |
| 4720 | parent = mem_cgroup_from_cont(cgrp->parent); |
| 4721 | |
| 4722 | cgroup_lock(); |
| 4723 | /* oom-kill-disable is a flag for subhierarchy. */ |
| 4724 | if ((parent->use_hierarchy) || |
| 4725 | (mem->use_hierarchy && !list_empty(&cgrp->children))) { |
| 4726 | cgroup_unlock(); |
| 4727 | return -EINVAL; |
| 4728 | } |
| 4729 | mem->oom_kill_disable = val; |
KAMEZAWA Hiroyuki | 4d845eb | 2010-06-29 15:05:18 -0700 | [diff] [blame] | 4730 | if (!val) |
| 4731 | memcg_oom_recover(mem); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4732 | cgroup_unlock(); |
| 4733 | return 0; |
| 4734 | } |
| 4735 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4736 | #ifdef CONFIG_NUMA |
| 4737 | static const struct file_operations mem_control_numa_stat_file_operations = { |
| 4738 | .read = seq_read, |
| 4739 | .llseek = seq_lseek, |
| 4740 | .release = single_release, |
| 4741 | }; |
| 4742 | |
| 4743 | static int mem_control_numa_stat_open(struct inode *unused, struct file *file) |
| 4744 | { |
| 4745 | struct cgroup *cont = file->f_dentry->d_parent->d_fsdata; |
| 4746 | |
| 4747 | file->f_op = &mem_control_numa_stat_file_operations; |
| 4748 | return single_open(file, mem_control_numa_stat_show, cont); |
| 4749 | } |
| 4750 | #endif /* CONFIG_NUMA */ |
| 4751 | |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame^] | 4752 | static int mem_cgroup_vmscan_stat_read(struct cgroup *cgrp, |
| 4753 | struct cftype *cft, |
| 4754 | struct cgroup_map_cb *cb) |
| 4755 | { |
| 4756 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4757 | char string[64]; |
| 4758 | int i; |
| 4759 | |
| 4760 | for (i = 0; i < NR_SCANSTATS; i++) { |
| 4761 | strcpy(string, scanstat_string[i]); |
| 4762 | strcat(string, SCANSTAT_WORD_LIMIT); |
| 4763 | cb->fill(cb, string, mem->scanstat.stats[SCAN_BY_LIMIT][i]); |
| 4764 | } |
| 4765 | |
| 4766 | for (i = 0; i < NR_SCANSTATS; i++) { |
| 4767 | strcpy(string, scanstat_string[i]); |
| 4768 | strcat(string, SCANSTAT_WORD_SYSTEM); |
| 4769 | cb->fill(cb, string, mem->scanstat.stats[SCAN_BY_SYSTEM][i]); |
| 4770 | } |
| 4771 | |
| 4772 | for (i = 0; i < NR_SCANSTATS; i++) { |
| 4773 | strcpy(string, scanstat_string[i]); |
| 4774 | strcat(string, SCANSTAT_WORD_LIMIT); |
| 4775 | strcat(string, SCANSTAT_WORD_HIERARCHY); |
| 4776 | cb->fill(cb, string, mem->scanstat.rootstats[SCAN_BY_LIMIT][i]); |
| 4777 | } |
| 4778 | for (i = 0; i < NR_SCANSTATS; i++) { |
| 4779 | strcpy(string, scanstat_string[i]); |
| 4780 | strcat(string, SCANSTAT_WORD_SYSTEM); |
| 4781 | strcat(string, SCANSTAT_WORD_HIERARCHY); |
| 4782 | cb->fill(cb, string, mem->scanstat.rootstats[SCAN_BY_SYSTEM][i]); |
| 4783 | } |
| 4784 | return 0; |
| 4785 | } |
| 4786 | |
| 4787 | static int mem_cgroup_reset_vmscan_stat(struct cgroup *cgrp, |
| 4788 | unsigned int event) |
| 4789 | { |
| 4790 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4791 | |
| 4792 | spin_lock(&mem->scanstat.lock); |
| 4793 | memset(&mem->scanstat.stats, 0, sizeof(mem->scanstat.stats)); |
| 4794 | memset(&mem->scanstat.rootstats, 0, sizeof(mem->scanstat.rootstats)); |
| 4795 | spin_unlock(&mem->scanstat.lock); |
| 4796 | return 0; |
| 4797 | } |
| 4798 | |
| 4799 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4800 | static struct cftype mem_cgroup_files[] = { |
| 4801 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4802 | .name = "usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4803 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4804 | .read_u64 = mem_cgroup_read, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4805 | .register_event = mem_cgroup_usage_register_event, |
| 4806 | .unregister_event = mem_cgroup_usage_unregister_event, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4807 | }, |
| 4808 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4809 | .name = "max_usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4810 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4811 | .trigger = mem_cgroup_reset, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4812 | .read_u64 = mem_cgroup_read, |
| 4813 | }, |
| 4814 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4815 | .name = "limit_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4816 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 4817 | .write_string = mem_cgroup_write, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4818 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4819 | }, |
| 4820 | { |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 4821 | .name = "soft_limit_in_bytes", |
| 4822 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), |
| 4823 | .write_string = mem_cgroup_write, |
| 4824 | .read_u64 = mem_cgroup_read, |
| 4825 | }, |
| 4826 | { |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4827 | .name = "failcnt", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4828 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4829 | .trigger = mem_cgroup_reset, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4830 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4831 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 4832 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4833 | .name = "stat", |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 4834 | .read_map = mem_control_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4835 | }, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4836 | { |
| 4837 | .name = "force_empty", |
| 4838 | .trigger = mem_cgroup_force_empty_write, |
| 4839 | }, |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4840 | { |
| 4841 | .name = "use_hierarchy", |
| 4842 | .write_u64 = mem_cgroup_hierarchy_write, |
| 4843 | .read_u64 = mem_cgroup_hierarchy_read, |
| 4844 | }, |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4845 | { |
| 4846 | .name = "swappiness", |
| 4847 | .read_u64 = mem_cgroup_swappiness_read, |
| 4848 | .write_u64 = mem_cgroup_swappiness_write, |
| 4849 | }, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4850 | { |
| 4851 | .name = "move_charge_at_immigrate", |
| 4852 | .read_u64 = mem_cgroup_move_charge_read, |
| 4853 | .write_u64 = mem_cgroup_move_charge_write, |
| 4854 | }, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4855 | { |
| 4856 | .name = "oom_control", |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4857 | .read_map = mem_cgroup_oom_control_read, |
| 4858 | .write_u64 = mem_cgroup_oom_control_write, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4859 | .register_event = mem_cgroup_oom_register_event, |
| 4860 | .unregister_event = mem_cgroup_oom_unregister_event, |
| 4861 | .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL), |
| 4862 | }, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4863 | #ifdef CONFIG_NUMA |
| 4864 | { |
| 4865 | .name = "numa_stat", |
| 4866 | .open = mem_control_numa_stat_open, |
KAMEZAWA Hiroyuki | 8957712 | 2011-06-15 15:08:41 -0700 | [diff] [blame] | 4867 | .mode = S_IRUGO, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4868 | }, |
| 4869 | #endif |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame^] | 4870 | { |
| 4871 | .name = "vmscan_stat", |
| 4872 | .read_map = mem_cgroup_vmscan_stat_read, |
| 4873 | .trigger = mem_cgroup_reset_vmscan_stat, |
| 4874 | }, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4875 | }; |
| 4876 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4877 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 4878 | static struct cftype memsw_cgroup_files[] = { |
| 4879 | { |
| 4880 | .name = "memsw.usage_in_bytes", |
| 4881 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), |
| 4882 | .read_u64 = mem_cgroup_read, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4883 | .register_event = mem_cgroup_usage_register_event, |
| 4884 | .unregister_event = mem_cgroup_usage_unregister_event, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4885 | }, |
| 4886 | { |
| 4887 | .name = "memsw.max_usage_in_bytes", |
| 4888 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), |
| 4889 | .trigger = mem_cgroup_reset, |
| 4890 | .read_u64 = mem_cgroup_read, |
| 4891 | }, |
| 4892 | { |
| 4893 | .name = "memsw.limit_in_bytes", |
| 4894 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), |
| 4895 | .write_string = mem_cgroup_write, |
| 4896 | .read_u64 = mem_cgroup_read, |
| 4897 | }, |
| 4898 | { |
| 4899 | .name = "memsw.failcnt", |
| 4900 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), |
| 4901 | .trigger = mem_cgroup_reset, |
| 4902 | .read_u64 = mem_cgroup_read, |
| 4903 | }, |
| 4904 | }; |
| 4905 | |
| 4906 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4907 | { |
| 4908 | if (!do_swap_account) |
| 4909 | return 0; |
| 4910 | return cgroup_add_files(cont, ss, memsw_cgroup_files, |
| 4911 | ARRAY_SIZE(memsw_cgroup_files)); |
| 4912 | }; |
| 4913 | #else |
| 4914 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4915 | { |
| 4916 | return 0; |
| 4917 | } |
| 4918 | #endif |
| 4919 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4920 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 4921 | { |
| 4922 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4923 | struct mem_cgroup_per_zone *mz; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 4924 | enum lru_list l; |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 4925 | int zone, tmp = node; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4926 | /* |
| 4927 | * This routine is called against possible nodes. |
| 4928 | * But it's BUG to call kmalloc() against offline node. |
| 4929 | * |
| 4930 | * TODO: this routine can waste much memory for nodes which will |
| 4931 | * never be onlined. It's better to use memory hotplug callback |
| 4932 | * function. |
| 4933 | */ |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 4934 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 4935 | tmp = -1; |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4936 | pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4937 | if (!pn) |
| 4938 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4939 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4940 | mem->info.nodeinfo[node] = pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4941 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 4942 | mz = &pn->zoneinfo[zone]; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 4943 | for_each_lru(l) |
| 4944 | INIT_LIST_HEAD(&mz->lists[l]); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4945 | mz->usage_in_excess = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4946 | mz->on_tree = false; |
| 4947 | mz->mem = mem; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4948 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4949 | return 0; |
| 4950 | } |
| 4951 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4952 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 4953 | { |
| 4954 | kfree(mem->info.nodeinfo[node]); |
| 4955 | } |
| 4956 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4957 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 4958 | { |
| 4959 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4960 | int size = sizeof(struct mem_cgroup); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4961 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4962 | /* Can be very big if MAX_NUMNODES is very big */ |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4963 | if (size < PAGE_SIZE) |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4964 | mem = kzalloc(size, GFP_KERNEL); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4965 | else |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4966 | mem = vzalloc(size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4967 | |
Dan Carpenter | e7bbcdf | 2010-03-23 13:35:12 -0700 | [diff] [blame] | 4968 | if (!mem) |
| 4969 | return NULL; |
| 4970 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4971 | mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu); |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 4972 | if (!mem->stat) |
| 4973 | goto out_free; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 4974 | spin_lock_init(&mem->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4975 | return mem; |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 4976 | |
| 4977 | out_free: |
| 4978 | if (size < PAGE_SIZE) |
| 4979 | kfree(mem); |
| 4980 | else |
| 4981 | vfree(mem); |
| 4982 | return NULL; |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4983 | } |
| 4984 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4985 | /* |
| 4986 | * At destroying mem_cgroup, references from swap_cgroup can remain. |
| 4987 | * (scanning all at force_empty is too costly...) |
| 4988 | * |
| 4989 | * Instead of clearing all references at force_empty, we remember |
| 4990 | * the number of reference from swap_cgroup and free mem_cgroup when |
| 4991 | * it goes down to 0. |
| 4992 | * |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4993 | * Removal of cgroup itself succeeds regardless of refs from swap. |
| 4994 | */ |
| 4995 | |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 4996 | static void __mem_cgroup_free(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4997 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4998 | int node; |
| 4999 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5000 | mem_cgroup_remove_from_trees(mem); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5001 | free_css_id(&mem_cgroup_subsys, &mem->css); |
| 5002 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 5003 | for_each_node_state(node, N_POSSIBLE) |
| 5004 | free_mem_cgroup_per_zone_info(mem, node); |
| 5005 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 5006 | free_percpu(mem->stat); |
| 5007 | if (sizeof(struct mem_cgroup) < PAGE_SIZE) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5008 | kfree(mem); |
| 5009 | else |
| 5010 | vfree(mem); |
| 5011 | } |
| 5012 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5013 | static void mem_cgroup_get(struct mem_cgroup *mem) |
| 5014 | { |
| 5015 | atomic_inc(&mem->refcnt); |
| 5016 | } |
| 5017 | |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5018 | static void __mem_cgroup_put(struct mem_cgroup *mem, int count) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5019 | { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5020 | if (atomic_sub_and_test(count, &mem->refcnt)) { |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5021 | struct mem_cgroup *parent = parent_mem_cgroup(mem); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 5022 | __mem_cgroup_free(mem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5023 | if (parent) |
| 5024 | mem_cgroup_put(parent); |
| 5025 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5026 | } |
| 5027 | |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5028 | static void mem_cgroup_put(struct mem_cgroup *mem) |
| 5029 | { |
| 5030 | __mem_cgroup_put(mem, 1); |
| 5031 | } |
| 5032 | |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5033 | /* |
| 5034 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. |
| 5035 | */ |
| 5036 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem) |
| 5037 | { |
| 5038 | if (!mem->res.parent) |
| 5039 | return NULL; |
| 5040 | return mem_cgroup_from_res_counter(mem->res.parent, res); |
| 5041 | } |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5042 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5043 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 5044 | static void __init enable_swap_cgroup(void) |
| 5045 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 5046 | if (!mem_cgroup_disabled() && really_do_swap_account) |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5047 | do_swap_account = 1; |
| 5048 | } |
| 5049 | #else |
| 5050 | static void __init enable_swap_cgroup(void) |
| 5051 | { |
| 5052 | } |
| 5053 | #endif |
| 5054 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5055 | static int mem_cgroup_soft_limit_tree_init(void) |
| 5056 | { |
| 5057 | struct mem_cgroup_tree_per_node *rtpn; |
| 5058 | struct mem_cgroup_tree_per_zone *rtpz; |
| 5059 | int tmp, node, zone; |
| 5060 | |
| 5061 | for_each_node_state(node, N_POSSIBLE) { |
| 5062 | tmp = node; |
| 5063 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 5064 | tmp = -1; |
| 5065 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp); |
| 5066 | if (!rtpn) |
| 5067 | return 1; |
| 5068 | |
| 5069 | soft_limit_tree.rb_tree_per_node[node] = rtpn; |
| 5070 | |
| 5071 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 5072 | rtpz = &rtpn->rb_tree_per_zone[zone]; |
| 5073 | rtpz->rb_root = RB_ROOT; |
| 5074 | spin_lock_init(&rtpz->lock); |
| 5075 | } |
| 5076 | } |
| 5077 | return 0; |
| 5078 | } |
| 5079 | |
Li Zefan | 0eb253e | 2009-01-15 13:51:25 -0800 | [diff] [blame] | 5080 | static struct cgroup_subsys_state * __ref |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5081 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 5082 | { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5083 | struct mem_cgroup *mem, *parent; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5084 | long error = -ENOMEM; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5085 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5086 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 5087 | mem = mem_cgroup_alloc(); |
| 5088 | if (!mem) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5089 | return ERR_PTR(error); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 5090 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5091 | for_each_node_state(node, N_POSSIBLE) |
| 5092 | if (alloc_mem_cgroup_per_zone_info(mem, node)) |
| 5093 | goto free_out; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5094 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5095 | /* root ? */ |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5096 | if (cont->parent == NULL) { |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 5097 | int cpu; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5098 | enable_swap_cgroup(); |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5099 | parent = NULL; |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 5100 | root_mem_cgroup = mem; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5101 | if (mem_cgroup_soft_limit_tree_init()) |
| 5102 | goto free_out; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 5103 | for_each_possible_cpu(cpu) { |
| 5104 | struct memcg_stock_pcp *stock = |
| 5105 | &per_cpu(memcg_stock, cpu); |
| 5106 | INIT_WORK(&stock->work, drain_local_stock); |
| 5107 | } |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 5108 | hotcpu_notifier(memcg_cpu_hotplug_callback, 0); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5109 | } else { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5110 | parent = mem_cgroup_from_cont(cont->parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5111 | mem->use_hierarchy = parent->use_hierarchy; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 5112 | mem->oom_kill_disable = parent->oom_kill_disable; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5113 | } |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5114 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5115 | if (parent && parent->use_hierarchy) { |
| 5116 | res_counter_init(&mem->res, &parent->res); |
| 5117 | res_counter_init(&mem->memsw, &parent->memsw); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5118 | /* |
| 5119 | * We increment refcnt of the parent to ensure that we can |
| 5120 | * safely access it on res_counter_charge/uncharge. |
| 5121 | * This refcnt will be decremented when freeing this |
| 5122 | * mem_cgroup(see mem_cgroup_put). |
| 5123 | */ |
| 5124 | mem_cgroup_get(parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5125 | } else { |
| 5126 | res_counter_init(&mem->res, NULL); |
| 5127 | res_counter_init(&mem->memsw, NULL); |
| 5128 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5129 | mem->last_scanned_child = 0; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 5130 | mem->last_scanned_node = MAX_NUMNODES; |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5131 | INIT_LIST_HEAD(&mem->oom_notify); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 5132 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5133 | if (parent) |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 5134 | mem->swappiness = mem_cgroup_swappiness(parent); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 5135 | atomic_set(&mem->refcnt, 1); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5136 | mem->move_charge_at_immigrate = 0; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5137 | mutex_init(&mem->thresholds_lock); |
KAMEZAWA Hiroyuki | 82f9d48 | 2011-07-26 16:08:26 -0700 | [diff] [blame^] | 5138 | spin_lock_init(&mem->scanstat.lock); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5139 | return &mem->css; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5140 | free_out: |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 5141 | __mem_cgroup_free(mem); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 5142 | root_mem_cgroup = NULL; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5143 | return ERR_PTR(error); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5144 | } |
| 5145 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 5146 | static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5147 | struct cgroup *cont) |
| 5148 | { |
| 5149 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 5150 | |
| 5151 | return mem_cgroup_force_empty(mem, false); |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5152 | } |
| 5153 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5154 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 5155 | struct cgroup *cont) |
| 5156 | { |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 5157 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 5158 | |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 5159 | mem_cgroup_put(mem); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5160 | } |
| 5161 | |
| 5162 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 5163 | struct cgroup *cont) |
| 5164 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5165 | int ret; |
| 5166 | |
| 5167 | ret = cgroup_add_files(cont, ss, mem_cgroup_files, |
| 5168 | ARRAY_SIZE(mem_cgroup_files)); |
| 5169 | |
| 5170 | if (!ret) |
| 5171 | ret = register_memsw_files(cont, ss); |
| 5172 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5173 | } |
| 5174 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5175 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5176 | /* Handlers for move charge at task migration. */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5177 | #define PRECHARGE_COUNT_AT_ONCE 256 |
| 5178 | static int mem_cgroup_do_precharge(unsigned long count) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5179 | { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5180 | int ret = 0; |
| 5181 | int batch_count = PRECHARGE_COUNT_AT_ONCE; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5182 | struct mem_cgroup *mem = mc.to; |
| 5183 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5184 | if (mem_cgroup_is_root(mem)) { |
| 5185 | mc.precharge += count; |
| 5186 | /* we don't need css_get for root */ |
| 5187 | return ret; |
| 5188 | } |
| 5189 | /* try to charge at once */ |
| 5190 | if (count > 1) { |
| 5191 | struct res_counter *dummy; |
| 5192 | /* |
| 5193 | * "mem" cannot be under rmdir() because we've already checked |
| 5194 | * by cgroup_lock_live_cgroup() that it is not removed and we |
| 5195 | * are still under the same cgroup_mutex. So we can postpone |
| 5196 | * css_get(). |
| 5197 | */ |
| 5198 | if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy)) |
| 5199 | goto one_by_one; |
| 5200 | if (do_swap_account && res_counter_charge(&mem->memsw, |
| 5201 | PAGE_SIZE * count, &dummy)) { |
| 5202 | res_counter_uncharge(&mem->res, PAGE_SIZE * count); |
| 5203 | goto one_by_one; |
| 5204 | } |
| 5205 | mc.precharge += count; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5206 | return ret; |
| 5207 | } |
| 5208 | one_by_one: |
| 5209 | /* fall back to one by one charge */ |
| 5210 | while (count--) { |
| 5211 | if (signal_pending(current)) { |
| 5212 | ret = -EINTR; |
| 5213 | break; |
| 5214 | } |
| 5215 | if (!batch_count--) { |
| 5216 | batch_count = PRECHARGE_COUNT_AT_ONCE; |
| 5217 | cond_resched(); |
| 5218 | } |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 5219 | ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, 1, &mem, false); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5220 | if (ret || !mem) |
| 5221 | /* mem_cgroup_clear_mc() will do uncharge later */ |
| 5222 | return -ENOMEM; |
| 5223 | mc.precharge++; |
| 5224 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5225 | return ret; |
| 5226 | } |
| 5227 | |
| 5228 | /** |
| 5229 | * is_target_pte_for_mc - check a pte whether it is valid for move charge |
| 5230 | * @vma: the vma the pte to be checked belongs |
| 5231 | * @addr: the address corresponding to the pte to be checked |
| 5232 | * @ptent: the pte to be checked |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5233 | * @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] | 5234 | * |
| 5235 | * Returns |
| 5236 | * 0(MC_TARGET_NONE): if the pte is not a target for move charge. |
| 5237 | * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for |
| 5238 | * move charge. if @target is not NULL, the page is stored in target->page |
| 5239 | * with extra refcnt got(Callers should handle it). |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5240 | * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a |
| 5241 | * target for charge migration. if @target is not NULL, the entry is stored |
| 5242 | * in target->ent. |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5243 | * |
| 5244 | * Called with pte lock held. |
| 5245 | */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5246 | union mc_target { |
| 5247 | struct page *page; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5248 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5249 | }; |
| 5250 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5251 | enum mc_target_type { |
| 5252 | MC_TARGET_NONE, /* not used */ |
| 5253 | MC_TARGET_PAGE, |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5254 | MC_TARGET_SWAP, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5255 | }; |
| 5256 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5257 | static struct page *mc_handle_present_pte(struct vm_area_struct *vma, |
| 5258 | unsigned long addr, pte_t ptent) |
| 5259 | { |
| 5260 | struct page *page = vm_normal_page(vma, addr, ptent); |
| 5261 | |
| 5262 | if (!page || !page_mapped(page)) |
| 5263 | return NULL; |
| 5264 | if (PageAnon(page)) { |
| 5265 | /* we don't move shared anon */ |
| 5266 | if (!move_anon() || page_mapcount(page) > 2) |
| 5267 | return NULL; |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5268 | } else if (!move_file()) |
| 5269 | /* we ignore mapcount for file pages */ |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5270 | return NULL; |
| 5271 | if (!get_page_unless_zero(page)) |
| 5272 | return NULL; |
| 5273 | |
| 5274 | return page; |
| 5275 | } |
| 5276 | |
| 5277 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
| 5278 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5279 | { |
| 5280 | int usage_count; |
| 5281 | struct page *page = NULL; |
| 5282 | swp_entry_t ent = pte_to_swp_entry(ptent); |
| 5283 | |
| 5284 | if (!move_anon() || non_swap_entry(ent)) |
| 5285 | return NULL; |
| 5286 | usage_count = mem_cgroup_count_swap_user(ent, &page); |
| 5287 | if (usage_count > 1) { /* we don't move shared anon */ |
| 5288 | if (page) |
| 5289 | put_page(page); |
| 5290 | return NULL; |
| 5291 | } |
| 5292 | if (do_swap_account) |
| 5293 | entry->val = ent.val; |
| 5294 | |
| 5295 | return page; |
| 5296 | } |
| 5297 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5298 | static struct page *mc_handle_file_pte(struct vm_area_struct *vma, |
| 5299 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5300 | { |
| 5301 | struct page *page = NULL; |
| 5302 | struct inode *inode; |
| 5303 | struct address_space *mapping; |
| 5304 | pgoff_t pgoff; |
| 5305 | |
| 5306 | if (!vma->vm_file) /* anonymous vma */ |
| 5307 | return NULL; |
| 5308 | if (!move_file()) |
| 5309 | return NULL; |
| 5310 | |
| 5311 | inode = vma->vm_file->f_path.dentry->d_inode; |
| 5312 | mapping = vma->vm_file->f_mapping; |
| 5313 | if (pte_none(ptent)) |
| 5314 | pgoff = linear_page_index(vma, addr); |
| 5315 | else /* pte_file(ptent) is true */ |
| 5316 | pgoff = pte_to_pgoff(ptent); |
| 5317 | |
| 5318 | /* page is moved even if it's not RSS of this task(page-faulted). */ |
| 5319 | if (!mapping_cap_swap_backed(mapping)) { /* normal file */ |
| 5320 | page = find_get_page(mapping, pgoff); |
| 5321 | } else { /* shmem/tmpfs file. we should take account of swap too. */ |
| 5322 | swp_entry_t ent; |
| 5323 | mem_cgroup_get_shmem_target(inode, pgoff, &page, &ent); |
| 5324 | if (do_swap_account) |
| 5325 | entry->val = ent.val; |
| 5326 | } |
| 5327 | |
| 5328 | return page; |
| 5329 | } |
| 5330 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5331 | static int is_target_pte_for_mc(struct vm_area_struct *vma, |
| 5332 | unsigned long addr, pte_t ptent, union mc_target *target) |
| 5333 | { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5334 | struct page *page = NULL; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5335 | struct page_cgroup *pc; |
| 5336 | int ret = 0; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5337 | swp_entry_t ent = { .val = 0 }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5338 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5339 | if (pte_present(ptent)) |
| 5340 | page = mc_handle_present_pte(vma, addr, ptent); |
| 5341 | else if (is_swap_pte(ptent)) |
| 5342 | page = mc_handle_swap_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5343 | else if (pte_none(ptent) || pte_file(ptent)) |
| 5344 | page = mc_handle_file_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5345 | |
| 5346 | if (!page && !ent.val) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5347 | return 0; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5348 | if (page) { |
| 5349 | pc = lookup_page_cgroup(page); |
| 5350 | /* |
| 5351 | * Do only loose check w/o page_cgroup lock. |
| 5352 | * mem_cgroup_move_account() checks the pc is valid or not under |
| 5353 | * the lock. |
| 5354 | */ |
| 5355 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { |
| 5356 | ret = MC_TARGET_PAGE; |
| 5357 | if (target) |
| 5358 | target->page = page; |
| 5359 | } |
| 5360 | if (!ret || !target) |
| 5361 | put_page(page); |
| 5362 | } |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5363 | /* There is a swap entry and a page doesn't exist or isn't charged */ |
| 5364 | if (ent.val && !ret && |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5365 | css_id(&mc.from->css) == lookup_swap_cgroup(ent)) { |
| 5366 | ret = MC_TARGET_SWAP; |
| 5367 | if (target) |
| 5368 | target->ent = ent; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5369 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5370 | return ret; |
| 5371 | } |
| 5372 | |
| 5373 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, |
| 5374 | unsigned long addr, unsigned long end, |
| 5375 | struct mm_walk *walk) |
| 5376 | { |
| 5377 | struct vm_area_struct *vma = walk->private; |
| 5378 | pte_t *pte; |
| 5379 | spinlock_t *ptl; |
| 5380 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 5381 | split_huge_page_pmd(walk->mm, pmd); |
| 5382 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5383 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5384 | for (; addr != end; pte++, addr += PAGE_SIZE) |
| 5385 | if (is_target_pte_for_mc(vma, addr, *pte, NULL)) |
| 5386 | mc.precharge++; /* increment precharge temporarily */ |
| 5387 | pte_unmap_unlock(pte - 1, ptl); |
| 5388 | cond_resched(); |
| 5389 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5390 | return 0; |
| 5391 | } |
| 5392 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5393 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
| 5394 | { |
| 5395 | unsigned long precharge; |
| 5396 | struct vm_area_struct *vma; |
| 5397 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5398 | down_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5399 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5400 | struct mm_walk mem_cgroup_count_precharge_walk = { |
| 5401 | .pmd_entry = mem_cgroup_count_precharge_pte_range, |
| 5402 | .mm = mm, |
| 5403 | .private = vma, |
| 5404 | }; |
| 5405 | if (is_vm_hugetlb_page(vma)) |
| 5406 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5407 | walk_page_range(vma->vm_start, vma->vm_end, |
| 5408 | &mem_cgroup_count_precharge_walk); |
| 5409 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5410 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5411 | |
| 5412 | precharge = mc.precharge; |
| 5413 | mc.precharge = 0; |
| 5414 | |
| 5415 | return precharge; |
| 5416 | } |
| 5417 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5418 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
| 5419 | { |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5420 | unsigned long precharge = mem_cgroup_count_precharge(mm); |
| 5421 | |
| 5422 | VM_BUG_ON(mc.moving_task); |
| 5423 | mc.moving_task = current; |
| 5424 | return mem_cgroup_do_precharge(precharge); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5425 | } |
| 5426 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5427 | /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */ |
| 5428 | static void __mem_cgroup_clear_mc(void) |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5429 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5430 | struct mem_cgroup *from = mc.from; |
| 5431 | struct mem_cgroup *to = mc.to; |
| 5432 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5433 | /* we must uncharge all the leftover precharges from mc.to */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5434 | if (mc.precharge) { |
| 5435 | __mem_cgroup_cancel_charge(mc.to, mc.precharge); |
| 5436 | mc.precharge = 0; |
| 5437 | } |
| 5438 | /* |
| 5439 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so |
| 5440 | * we must uncharge here. |
| 5441 | */ |
| 5442 | if (mc.moved_charge) { |
| 5443 | __mem_cgroup_cancel_charge(mc.from, mc.moved_charge); |
| 5444 | mc.moved_charge = 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5445 | } |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5446 | /* we must fixup refcnts and charges */ |
| 5447 | if (mc.moved_swap) { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5448 | /* uncharge swap account from the old cgroup */ |
| 5449 | if (!mem_cgroup_is_root(mc.from)) |
| 5450 | res_counter_uncharge(&mc.from->memsw, |
| 5451 | PAGE_SIZE * mc.moved_swap); |
| 5452 | __mem_cgroup_put(mc.from, mc.moved_swap); |
| 5453 | |
| 5454 | if (!mem_cgroup_is_root(mc.to)) { |
| 5455 | /* |
| 5456 | * we charged both to->res and to->memsw, so we should |
| 5457 | * uncharge to->res. |
| 5458 | */ |
| 5459 | res_counter_uncharge(&mc.to->res, |
| 5460 | PAGE_SIZE * mc.moved_swap); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5461 | } |
| 5462 | /* we've already done mem_cgroup_get(mc.to) */ |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5463 | mc.moved_swap = 0; |
| 5464 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5465 | memcg_oom_recover(from); |
| 5466 | memcg_oom_recover(to); |
| 5467 | wake_up_all(&mc.waitq); |
| 5468 | } |
| 5469 | |
| 5470 | static void mem_cgroup_clear_mc(void) |
| 5471 | { |
| 5472 | struct mem_cgroup *from = mc.from; |
| 5473 | |
| 5474 | /* |
| 5475 | * we must clear moving_task before waking up waiters at the end of |
| 5476 | * task migration. |
| 5477 | */ |
| 5478 | mc.moving_task = NULL; |
| 5479 | __mem_cgroup_clear_mc(); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5480 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5481 | mc.from = NULL; |
| 5482 | mc.to = NULL; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5483 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5484 | mem_cgroup_end_move(from); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5485 | } |
| 5486 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5487 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 5488 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5489 | struct task_struct *p) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5490 | { |
| 5491 | int ret = 0; |
| 5492 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup); |
| 5493 | |
| 5494 | if (mem->move_charge_at_immigrate) { |
| 5495 | struct mm_struct *mm; |
| 5496 | struct mem_cgroup *from = mem_cgroup_from_task(p); |
| 5497 | |
| 5498 | VM_BUG_ON(from == mem); |
| 5499 | |
| 5500 | mm = get_task_mm(p); |
| 5501 | if (!mm) |
| 5502 | return 0; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5503 | /* We move charges only when we move a owner of the mm */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5504 | if (mm->owner == p) { |
| 5505 | VM_BUG_ON(mc.from); |
| 5506 | VM_BUG_ON(mc.to); |
| 5507 | VM_BUG_ON(mc.precharge); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5508 | VM_BUG_ON(mc.moved_charge); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5509 | VM_BUG_ON(mc.moved_swap); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5510 | mem_cgroup_start_move(from); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5511 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5512 | mc.from = from; |
| 5513 | mc.to = mem; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5514 | spin_unlock(&mc.lock); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5515 | /* We set mc.moving_task later */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5516 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5517 | ret = mem_cgroup_precharge_mc(mm); |
| 5518 | if (ret) |
| 5519 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5520 | } |
| 5521 | mmput(mm); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5522 | } |
| 5523 | return ret; |
| 5524 | } |
| 5525 | |
| 5526 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 5527 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5528 | struct task_struct *p) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5529 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5530 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5531 | } |
| 5532 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5533 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
| 5534 | unsigned long addr, unsigned long end, |
| 5535 | struct mm_walk *walk) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5536 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5537 | int ret = 0; |
| 5538 | struct vm_area_struct *vma = walk->private; |
| 5539 | pte_t *pte; |
| 5540 | spinlock_t *ptl; |
| 5541 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 5542 | split_huge_page_pmd(walk->mm, pmd); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5543 | retry: |
| 5544 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5545 | for (; addr != end; addr += PAGE_SIZE) { |
| 5546 | pte_t ptent = *(pte++); |
| 5547 | union mc_target target; |
| 5548 | int type; |
| 5549 | struct page *page; |
| 5550 | struct page_cgroup *pc; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5551 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5552 | |
| 5553 | if (!mc.precharge) |
| 5554 | break; |
| 5555 | |
| 5556 | type = is_target_pte_for_mc(vma, addr, ptent, &target); |
| 5557 | switch (type) { |
| 5558 | case MC_TARGET_PAGE: |
| 5559 | page = target.page; |
| 5560 | if (isolate_lru_page(page)) |
| 5561 | goto put; |
| 5562 | pc = lookup_page_cgroup(page); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 5563 | if (!mem_cgroup_move_account(page, 1, pc, |
| 5564 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5565 | mc.precharge--; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5566 | /* we uncharge from mc.from later. */ |
| 5567 | mc.moved_charge++; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5568 | } |
| 5569 | putback_lru_page(page); |
| 5570 | put: /* is_target_pte_for_mc() gets the page */ |
| 5571 | put_page(page); |
| 5572 | break; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5573 | case MC_TARGET_SWAP: |
| 5574 | ent = target.ent; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5575 | if (!mem_cgroup_move_swap_account(ent, |
| 5576 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5577 | mc.precharge--; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5578 | /* we fixup refcnts and charges later. */ |
| 5579 | mc.moved_swap++; |
| 5580 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5581 | break; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5582 | default: |
| 5583 | break; |
| 5584 | } |
| 5585 | } |
| 5586 | pte_unmap_unlock(pte - 1, ptl); |
| 5587 | cond_resched(); |
| 5588 | |
| 5589 | if (addr != end) { |
| 5590 | /* |
| 5591 | * We have consumed all precharges we got in can_attach(). |
| 5592 | * We try charge one by one, but don't do any additional |
| 5593 | * charges to mc.to if we have failed in charge once in attach() |
| 5594 | * phase. |
| 5595 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5596 | ret = mem_cgroup_do_precharge(1); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5597 | if (!ret) |
| 5598 | goto retry; |
| 5599 | } |
| 5600 | |
| 5601 | return ret; |
| 5602 | } |
| 5603 | |
| 5604 | static void mem_cgroup_move_charge(struct mm_struct *mm) |
| 5605 | { |
| 5606 | struct vm_area_struct *vma; |
| 5607 | |
| 5608 | lru_add_drain_all(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5609 | retry: |
| 5610 | if (unlikely(!down_read_trylock(&mm->mmap_sem))) { |
| 5611 | /* |
| 5612 | * Someone who are holding the mmap_sem might be waiting in |
| 5613 | * waitq. So we cancel all extra charges, wake up all waiters, |
| 5614 | * and retry. Because we cancel precharges, we might not be able |
| 5615 | * to move enough charges, but moving charge is a best-effort |
| 5616 | * feature anyway, so it wouldn't be a big problem. |
| 5617 | */ |
| 5618 | __mem_cgroup_clear_mc(); |
| 5619 | cond_resched(); |
| 5620 | goto retry; |
| 5621 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5622 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5623 | int ret; |
| 5624 | struct mm_walk mem_cgroup_move_charge_walk = { |
| 5625 | .pmd_entry = mem_cgroup_move_charge_pte_range, |
| 5626 | .mm = mm, |
| 5627 | .private = vma, |
| 5628 | }; |
| 5629 | if (is_vm_hugetlb_page(vma)) |
| 5630 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5631 | ret = walk_page_range(vma->vm_start, vma->vm_end, |
| 5632 | &mem_cgroup_move_charge_walk); |
| 5633 | if (ret) |
| 5634 | /* |
| 5635 | * means we have consumed all precharges and failed in |
| 5636 | * doing additional charge. Just abandon here. |
| 5637 | */ |
| 5638 | break; |
| 5639 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5640 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5641 | } |
| 5642 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5643 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 5644 | struct cgroup *cont, |
| 5645 | struct cgroup *old_cont, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5646 | struct task_struct *p) |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5647 | { |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5648 | struct mm_struct *mm = get_task_mm(p); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5649 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5650 | if (mm) { |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5651 | if (mc.to) |
| 5652 | mem_cgroup_move_charge(mm); |
| 5653 | put_swap_token(mm); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5654 | mmput(mm); |
| 5655 | } |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5656 | if (mc.to) |
| 5657 | mem_cgroup_clear_mc(); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5658 | } |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5659 | #else /* !CONFIG_MMU */ |
| 5660 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 5661 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5662 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5663 | { |
| 5664 | return 0; |
| 5665 | } |
| 5666 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 5667 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5668 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5669 | { |
| 5670 | } |
| 5671 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 5672 | struct cgroup *cont, |
| 5673 | struct cgroup *old_cont, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5674 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5675 | { |
| 5676 | } |
| 5677 | #endif |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5678 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5679 | struct cgroup_subsys mem_cgroup_subsys = { |
| 5680 | .name = "memory", |
| 5681 | .subsys_id = mem_cgroup_subsys_id, |
| 5682 | .create = mem_cgroup_create, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5683 | .pre_destroy = mem_cgroup_pre_destroy, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5684 | .destroy = mem_cgroup_destroy, |
| 5685 | .populate = mem_cgroup_populate, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5686 | .can_attach = mem_cgroup_can_attach, |
| 5687 | .cancel_attach = mem_cgroup_cancel_attach, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5688 | .attach = mem_cgroup_move_task, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5689 | .early_init = 0, |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5690 | .use_id = 1, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5691 | }; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5692 | |
| 5693 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5694 | static int __init enable_swap_account(char *s) |
| 5695 | { |
| 5696 | /* consider enabled if no parameter or 1 is given */ |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5697 | if (!strcmp(s, "1")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5698 | really_do_swap_account = 1; |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5699 | else if (!strcmp(s, "0")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5700 | really_do_swap_account = 0; |
| 5701 | return 1; |
| 5702 | } |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5703 | __setup("swapaccount=", enable_swap_account); |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5704 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5705 | #endif |