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 | * |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 13 | * Kernel Memory Controller |
| 14 | * Copyright (C) 2012 Parallels Inc. and Google Inc. |
| 15 | * Authors: Glauber Costa and Suleiman Souhlal |
| 16 | * |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 17 | * This program is free software; you can redistribute it and/or modify |
| 18 | * it under the terms of the GNU General Public License as published by |
| 19 | * the Free Software Foundation; either version 2 of the License, or |
| 20 | * (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | * GNU General Public License for more details. |
| 26 | */ |
| 27 | |
| 28 | #include <linux/res_counter.h> |
| 29 | #include <linux/memcontrol.h> |
| 30 | #include <linux/cgroup.h> |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 31 | #include <linux/mm.h> |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 32 | #include <linux/hugetlb.h> |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 33 | #include <linux/pagemap.h> |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 34 | #include <linux/smp.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 35 | #include <linux/page-flags.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 36 | #include <linux/backing-dev.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 37 | #include <linux/bit_spinlock.h> |
| 38 | #include <linux/rcupdate.h> |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 39 | #include <linux/limits.h> |
Paul Gortmaker | b9e15ba | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 40 | #include <linux/export.h> |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 41 | #include <linux/mutex.h> |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 42 | #include <linux/rbtree.h> |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 43 | #include <linux/slab.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 44 | #include <linux/swap.h> |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 45 | #include <linux/swapops.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 46 | #include <linux/spinlock.h> |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 47 | #include <linux/eventfd.h> |
| 48 | #include <linux/sort.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 49 | #include <linux/fs.h> |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 50 | #include <linux/seq_file.h> |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 51 | #include <linux/vmalloc.h> |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 52 | #include <linux/mm_inline.h> |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 53 | #include <linux/page_cgroup.h> |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 54 | #include <linux/cpu.h> |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 55 | #include <linux/oom.h> |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 56 | #include "internal.h" |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 57 | #include <net/sock.h> |
Michal Hocko | 4bd2c1e | 2012-10-08 16:33:10 -0700 | [diff] [blame] | 58 | #include <net/ip.h> |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 59 | #include <net/tcp_memcontrol.h> |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 60 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 61 | #include <asm/uaccess.h> |
| 62 | |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 63 | #include <trace/events/vmscan.h> |
| 64 | |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 65 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
David Rientjes | 68ae564 | 2012-12-12 13:51:57 -0800 | [diff] [blame] | 66 | EXPORT_SYMBOL(mem_cgroup_subsys); |
| 67 | |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 68 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
Kirill A. Shutemov | 6bbda35 | 2012-05-29 15:06:55 -0700 | [diff] [blame] | 69 | static struct mem_cgroup *root_mem_cgroup __read_mostly; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 70 | |
Andrew Morton | c255a45 | 2012-07-31 16:43:02 -0700 | [diff] [blame] | 71 | #ifdef CONFIG_MEMCG_SWAP |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 72 | /* 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] | 73 | int do_swap_account __read_mostly; |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 74 | |
| 75 | /* for remember boot option*/ |
Andrew Morton | c255a45 | 2012-07-31 16:43:02 -0700 | [diff] [blame] | 76 | #ifdef CONFIG_MEMCG_SWAP_ENABLED |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 77 | static int really_do_swap_account __initdata = 1; |
| 78 | #else |
| 79 | static int really_do_swap_account __initdata = 0; |
| 80 | #endif |
| 81 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 82 | #else |
Kirill A. Shutemov | a0db00f | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 83 | #define do_swap_account 0 |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 84 | #endif |
| 85 | |
| 86 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 87 | /* |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 88 | * Statistics for memory cgroup. |
| 89 | */ |
| 90 | enum mem_cgroup_stat_index { |
| 91 | /* |
| 92 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. |
| 93 | */ |
| 94 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 95 | MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */ |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 96 | MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */ |
Kamezawa Hiroyuki | bff6bb8 | 2012-07-31 16:41:38 -0700 | [diff] [blame] | 97 | MEM_CGROUP_STAT_SWAP, /* # of pages, swapped out */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 98 | MEM_CGROUP_STAT_NSTATS, |
| 99 | }; |
| 100 | |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 101 | static const char * const mem_cgroup_stat_names[] = { |
| 102 | "cache", |
| 103 | "rss", |
| 104 | "mapped_file", |
| 105 | "swap", |
| 106 | }; |
| 107 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 108 | enum mem_cgroup_events_index { |
| 109 | MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */ |
| 110 | MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */ |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 111 | MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */ |
| 112 | MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */ |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 113 | MEM_CGROUP_EVENTS_NSTATS, |
| 114 | }; |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 115 | |
| 116 | static const char * const mem_cgroup_events_names[] = { |
| 117 | "pgpgin", |
| 118 | "pgpgout", |
| 119 | "pgfault", |
| 120 | "pgmajfault", |
| 121 | }; |
| 122 | |
Sha Zhengju | 58cf188 | 2013-02-22 16:32:05 -0800 | [diff] [blame] | 123 | static const char * const mem_cgroup_lru_names[] = { |
| 124 | "inactive_anon", |
| 125 | "active_anon", |
| 126 | "inactive_file", |
| 127 | "active_file", |
| 128 | "unevictable", |
| 129 | }; |
| 130 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 131 | /* |
| 132 | * Per memcg event counter is incremented at every pagein/pageout. With THP, |
| 133 | * it will be incremated by the number of pages. This counter is used for |
| 134 | * for trigger some periodic events. This is straightforward and better |
| 135 | * than using jiffies etc. to handle periodic memcg event. |
| 136 | */ |
| 137 | enum mem_cgroup_events_target { |
| 138 | MEM_CGROUP_TARGET_THRESH, |
| 139 | MEM_CGROUP_TARGET_SOFTLIMIT, |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 140 | MEM_CGROUP_TARGET_NUMAINFO, |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 141 | MEM_CGROUP_NTARGETS, |
| 142 | }; |
Kirill A. Shutemov | a0db00f | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 143 | #define THRESHOLDS_EVENTS_TARGET 128 |
| 144 | #define SOFTLIMIT_EVENTS_TARGET 1024 |
| 145 | #define NUMAINFO_EVENTS_TARGET 1024 |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 146 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 147 | struct mem_cgroup_stat_cpu { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 148 | long count[MEM_CGROUP_STAT_NSTATS]; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 149 | unsigned long events[MEM_CGROUP_EVENTS_NSTATS]; |
Johannes Weiner | 1311471 | 2012-05-29 15:07:07 -0700 | [diff] [blame] | 150 | unsigned long nr_page_events; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 151 | unsigned long targets[MEM_CGROUP_NTARGETS]; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 152 | }; |
| 153 | |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 154 | struct mem_cgroup_reclaim_iter { |
| 155 | /* css_id of the last scanned hierarchy member */ |
| 156 | int position; |
| 157 | /* scan generation, increased every round-trip */ |
| 158 | unsigned int generation; |
| 159 | }; |
| 160 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 161 | /* |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 162 | * per-zone information in memory controller. |
| 163 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 164 | struct mem_cgroup_per_zone { |
Johannes Weiner | 6290df5 | 2012-01-12 17:18:10 -0800 | [diff] [blame] | 165 | struct lruvec lruvec; |
Hugh Dickins | 1eb4927 | 2012-03-21 16:34:19 -0700 | [diff] [blame] | 166 | unsigned long lru_size[NR_LRU_LISTS]; |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 167 | |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 168 | struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1]; |
| 169 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 170 | struct rb_node tree_node; /* RB tree node */ |
| 171 | unsigned long long usage_in_excess;/* Set to the value by which */ |
| 172 | /* the soft limit is exceeded*/ |
| 173 | bool on_tree; |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 174 | struct mem_cgroup *memcg; /* Back pointer, we cannot */ |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 175 | /* use container_of */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 176 | }; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 177 | |
| 178 | struct mem_cgroup_per_node { |
| 179 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; |
| 180 | }; |
| 181 | |
| 182 | struct mem_cgroup_lru_info { |
Glauber Costa | 45cf7eb | 2013-02-22 16:34:49 -0800 | [diff] [blame] | 183 | struct mem_cgroup_per_node *nodeinfo[0]; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | /* |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 187 | * Cgroups above their limits are maintained in a RB-Tree, independent of |
| 188 | * their hierarchy representation |
| 189 | */ |
| 190 | |
| 191 | struct mem_cgroup_tree_per_zone { |
| 192 | struct rb_root rb_root; |
| 193 | spinlock_t lock; |
| 194 | }; |
| 195 | |
| 196 | struct mem_cgroup_tree_per_node { |
| 197 | struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES]; |
| 198 | }; |
| 199 | |
| 200 | struct mem_cgroup_tree { |
| 201 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; |
| 202 | }; |
| 203 | |
| 204 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; |
| 205 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 206 | struct mem_cgroup_threshold { |
| 207 | struct eventfd_ctx *eventfd; |
| 208 | u64 threshold; |
| 209 | }; |
| 210 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 211 | /* For threshold */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 212 | struct mem_cgroup_threshold_ary { |
Sha Zhengju | 748dad3 | 2012-05-29 15:06:57 -0700 | [diff] [blame] | 213 | /* An array index points to threshold just below or equal to usage. */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 214 | int current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 215 | /* Size of entries[] */ |
| 216 | unsigned int size; |
| 217 | /* Array of thresholds */ |
| 218 | struct mem_cgroup_threshold entries[0]; |
| 219 | }; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 220 | |
| 221 | struct mem_cgroup_thresholds { |
| 222 | /* Primary thresholds array */ |
| 223 | struct mem_cgroup_threshold_ary *primary; |
| 224 | /* |
| 225 | * Spare threshold array. |
| 226 | * This is needed to make mem_cgroup_unregister_event() "never fail". |
| 227 | * It must be able to store at least primary->size - 1 entries. |
| 228 | */ |
| 229 | struct mem_cgroup_threshold_ary *spare; |
| 230 | }; |
| 231 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 232 | /* for OOM */ |
| 233 | struct mem_cgroup_eventfd_list { |
| 234 | struct list_head list; |
| 235 | struct eventfd_ctx *eventfd; |
| 236 | }; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 237 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 238 | static void mem_cgroup_threshold(struct mem_cgroup *memcg); |
| 239 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 240 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 241 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 242 | * The memory controller data structure. The memory controller controls both |
| 243 | * page cache and RSS per cgroup. We would eventually like to provide |
| 244 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, |
| 245 | * to help the administrator determine what knobs to tune. |
| 246 | * |
| 247 | * 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] | 248 | * we hit the water mark. May be even add a low water mark, such that |
| 249 | * no reclaim occurs from a cgroup at it's low water mark, this is |
| 250 | * a feature that will be implemented much later in the future. |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 251 | */ |
| 252 | struct mem_cgroup { |
| 253 | struct cgroup_subsys_state css; |
| 254 | /* |
| 255 | * the counter to account for memory usage |
| 256 | */ |
| 257 | struct res_counter res; |
Hugh Dickins | 59927fb | 2012-03-15 15:17:07 -0700 | [diff] [blame] | 258 | |
| 259 | union { |
| 260 | /* |
| 261 | * the counter to account for mem+swap usage. |
| 262 | */ |
| 263 | struct res_counter memsw; |
| 264 | |
| 265 | /* |
| 266 | * rcu_freeing is used only when freeing struct mem_cgroup, |
| 267 | * so put it into a union to avoid wasting more memory. |
| 268 | * It must be disjoint from the css field. It could be |
| 269 | * in a union with the res field, but res plays a much |
| 270 | * larger part in mem_cgroup life than memsw, and might |
| 271 | * be of interest, even at time of free, when debugging. |
| 272 | * So share rcu_head with the less interesting memsw. |
| 273 | */ |
| 274 | struct rcu_head rcu_freeing; |
| 275 | /* |
Glauber Costa | 3afe36b | 2012-05-29 15:07:10 -0700 | [diff] [blame] | 276 | * We also need some space for a worker in deferred freeing. |
| 277 | * By the time we call it, rcu_freeing is no longer in use. |
Hugh Dickins | 59927fb | 2012-03-15 15:17:07 -0700 | [diff] [blame] | 278 | */ |
| 279 | struct work_struct work_freeing; |
| 280 | }; |
| 281 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 282 | /* |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 283 | * the counter to account for kernel memory usage. |
| 284 | */ |
| 285 | struct res_counter kmem; |
| 286 | /* |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 287 | * Should the accounting and control be hierarchical, per subtree? |
| 288 | */ |
| 289 | bool use_hierarchy; |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 290 | unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 291 | |
| 292 | bool oom_lock; |
| 293 | atomic_t under_oom; |
| 294 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 295 | atomic_t refcnt; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 296 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 297 | int swappiness; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 298 | /* OOM-Killer disable */ |
| 299 | int oom_kill_disable; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 300 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 301 | /* set when res.limit == memsw.limit */ |
| 302 | bool memsw_is_minimum; |
| 303 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 304 | /* protect arrays of thresholds */ |
| 305 | struct mutex thresholds_lock; |
| 306 | |
| 307 | /* thresholds for memory usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 308 | struct mem_cgroup_thresholds thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 309 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 310 | /* thresholds for mem+swap usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 311 | struct mem_cgroup_thresholds memsw_thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 312 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 313 | /* For oom notifier event fd */ |
| 314 | struct list_head oom_notify; |
Johannes Weiner | 185efc0 | 2011-09-14 16:21:58 -0700 | [diff] [blame] | 315 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 316 | /* |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 317 | * Should we move charges of a task when a task is moved into this |
| 318 | * mem_cgroup ? And what type of charges should we move ? |
| 319 | */ |
| 320 | unsigned long move_charge_at_immigrate; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 321 | /* |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 322 | * set > 0 if pages under this cgroup are moving to other cgroup. |
| 323 | */ |
| 324 | atomic_t moving_account; |
KAMEZAWA Hiroyuki | 312734c0 | 2012-03-21 16:34:24 -0700 | [diff] [blame] | 325 | /* taken only while moving_account > 0 */ |
| 326 | spinlock_t move_lock; |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 327 | /* |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 328 | * percpu counter. |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 329 | */ |
Kirill A. Shutemov | 3a7951b | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 330 | struct mem_cgroup_stat_cpu __percpu *stat; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 331 | /* |
| 332 | * used when a cpu is offlined or other synchronizations |
| 333 | * See mem_cgroup_read_stat(). |
| 334 | */ |
| 335 | struct mem_cgroup_stat_cpu nocpu_base; |
| 336 | spinlock_t pcp_counter_lock; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 337 | |
Michal Hocko | 4bd2c1e | 2012-10-08 16:33:10 -0700 | [diff] [blame] | 338 | #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET) |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 339 | struct tcp_memcontrol tcp_mem; |
| 340 | #endif |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 341 | #if defined(CONFIG_MEMCG_KMEM) |
| 342 | /* analogous to slab_common's slab_caches list. per-memcg */ |
| 343 | struct list_head memcg_slab_caches; |
| 344 | /* Not a spinlock, we can take a lot of time walking the list */ |
| 345 | struct mutex slab_caches_mutex; |
| 346 | /* Index in the kmem_cache->memcg_params->memcg_caches array */ |
| 347 | int kmemcg_id; |
| 348 | #endif |
Glauber Costa | 45cf7eb | 2013-02-22 16:34:49 -0800 | [diff] [blame] | 349 | |
| 350 | int last_scanned_node; |
| 351 | #if MAX_NUMNODES > 1 |
| 352 | nodemask_t scan_nodes; |
| 353 | atomic_t numainfo_events; |
| 354 | atomic_t numainfo_updating; |
| 355 | #endif |
| 356 | /* |
| 357 | * Per cgroup active and inactive list, similar to the |
| 358 | * per zone LRU lists. |
| 359 | * |
| 360 | * WARNING: This has to be the last element of the struct. Don't |
| 361 | * add new fields after this point. |
| 362 | */ |
| 363 | struct mem_cgroup_lru_info info; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 364 | }; |
| 365 | |
Glauber Costa | 45cf7eb | 2013-02-22 16:34:49 -0800 | [diff] [blame] | 366 | static size_t memcg_size(void) |
| 367 | { |
| 368 | return sizeof(struct mem_cgroup) + |
| 369 | nr_node_ids * sizeof(struct mem_cgroup_per_node); |
| 370 | } |
| 371 | |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 372 | /* internal only representation about the status of kmem accounting. */ |
| 373 | enum { |
| 374 | KMEM_ACCOUNTED_ACTIVE = 0, /* accounted by this cgroup itself */ |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 375 | KMEM_ACCOUNTED_ACTIVATED, /* static key enabled. */ |
Glauber Costa | 7de3768 | 2012-12-18 14:22:07 -0800 | [diff] [blame] | 376 | KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */ |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 377 | }; |
| 378 | |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 379 | /* We account when limit is on, but only after call sites are patched */ |
| 380 | #define KMEM_ACCOUNTED_MASK \ |
| 381 | ((1 << KMEM_ACCOUNTED_ACTIVE) | (1 << KMEM_ACCOUNTED_ACTIVATED)) |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 382 | |
| 383 | #ifdef CONFIG_MEMCG_KMEM |
| 384 | static inline void memcg_kmem_set_active(struct mem_cgroup *memcg) |
| 385 | { |
| 386 | set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags); |
| 387 | } |
Glauber Costa | 7de3768 | 2012-12-18 14:22:07 -0800 | [diff] [blame] | 388 | |
| 389 | static bool memcg_kmem_is_active(struct mem_cgroup *memcg) |
| 390 | { |
| 391 | return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags); |
| 392 | } |
| 393 | |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 394 | static void memcg_kmem_set_activated(struct mem_cgroup *memcg) |
| 395 | { |
| 396 | set_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags); |
| 397 | } |
| 398 | |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 399 | static void memcg_kmem_clear_activated(struct mem_cgroup *memcg) |
| 400 | { |
| 401 | clear_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags); |
| 402 | } |
| 403 | |
Glauber Costa | 7de3768 | 2012-12-18 14:22:07 -0800 | [diff] [blame] | 404 | static void memcg_kmem_mark_dead(struct mem_cgroup *memcg) |
| 405 | { |
| 406 | if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags)) |
| 407 | set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags); |
| 408 | } |
| 409 | |
| 410 | static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg) |
| 411 | { |
| 412 | return test_and_clear_bit(KMEM_ACCOUNTED_DEAD, |
| 413 | &memcg->kmem_account_flags); |
| 414 | } |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 415 | #endif |
| 416 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 417 | /* Stuffs for move charges at task migration. */ |
| 418 | /* |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 419 | * Types of charges to be moved. "move_charge_at_immitgrate" and |
| 420 | * "immigrate_flags" are treated as a left-shifted bitmap of these types. |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 421 | */ |
| 422 | enum move_type { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 423 | MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */ |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 424 | MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 425 | NR_MOVE_TYPE, |
| 426 | }; |
| 427 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 428 | /* "mc" and its members are protected by cgroup_mutex */ |
| 429 | static struct move_charge_struct { |
Daisuke Nishimura | b1dd693 | 2010-11-24 12:57:06 -0800 | [diff] [blame] | 430 | spinlock_t lock; /* for from, to */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 431 | struct mem_cgroup *from; |
| 432 | struct mem_cgroup *to; |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 433 | unsigned long immigrate_flags; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 434 | unsigned long precharge; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 435 | unsigned long moved_charge; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 436 | unsigned long moved_swap; |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 437 | struct task_struct *moving_task; /* a task moving charges */ |
| 438 | wait_queue_head_t waitq; /* a waitq for other context */ |
| 439 | } mc = { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 440 | .lock = __SPIN_LOCK_UNLOCKED(mc.lock), |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 441 | .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq), |
| 442 | }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 443 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 444 | static bool move_anon(void) |
| 445 | { |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 446 | return test_bit(MOVE_CHARGE_TYPE_ANON, &mc.immigrate_flags); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 449 | static bool move_file(void) |
| 450 | { |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 451 | return test_bit(MOVE_CHARGE_TYPE_FILE, &mc.immigrate_flags); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 454 | /* |
| 455 | * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft |
| 456 | * limit reclaim to prevent infinite loops, if they ever occur. |
| 457 | */ |
Kirill A. Shutemov | a0db00f | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 458 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS 100 |
| 459 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2 |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 460 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 461 | enum charge_type { |
| 462 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
Kamezawa Hiroyuki | 41326c1 | 2012-07-31 16:41:40 -0700 | [diff] [blame] | 463 | MEM_CGROUP_CHARGE_TYPE_ANON, |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 464 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 465 | MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 466 | NR_CHARGE_TYPE, |
| 467 | }; |
| 468 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 469 | /* for encoding cft->private value on file */ |
Glauber Costa | 86ae53e | 2012-12-18 14:21:45 -0800 | [diff] [blame] | 470 | enum res_type { |
| 471 | _MEM, |
| 472 | _MEMSWAP, |
| 473 | _OOM_TYPE, |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 474 | _KMEM, |
Glauber Costa | 86ae53e | 2012-12-18 14:21:45 -0800 | [diff] [blame] | 475 | }; |
| 476 | |
Kirill A. Shutemov | a0db00f | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 477 | #define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val)) |
| 478 | #define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 479 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 480 | /* Used for OOM nofiier */ |
| 481 | #define OOM_CONTROL (0) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 482 | |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 483 | /* |
| 484 | * Reclaim flags for mem_cgroup_hierarchical_reclaim |
| 485 | */ |
| 486 | #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0 |
| 487 | #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT) |
| 488 | #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1 |
| 489 | #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT) |
| 490 | |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 491 | /* |
| 492 | * The memcg_create_mutex will be held whenever a new cgroup is created. |
| 493 | * As a consequence, any change that needs to protect against new child cgroups |
| 494 | * appearing has to hold it as well. |
| 495 | */ |
| 496 | static DEFINE_MUTEX(memcg_create_mutex); |
| 497 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 498 | static void mem_cgroup_get(struct mem_cgroup *memcg); |
| 499 | static void mem_cgroup_put(struct mem_cgroup *memcg); |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 500 | |
Wanpeng Li | b214514 | 2012-07-31 16:46:01 -0700 | [diff] [blame] | 501 | static inline |
| 502 | struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s) |
| 503 | { |
| 504 | return container_of(s, struct mem_cgroup, css); |
| 505 | } |
| 506 | |
Michal Hocko | 7ffc0ed | 2012-10-08 16:33:13 -0700 | [diff] [blame] | 507 | static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg) |
| 508 | { |
| 509 | return (memcg == root_mem_cgroup); |
| 510 | } |
| 511 | |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 512 | /* Writing them here to avoid exposing memcg's inner layout */ |
Michal Hocko | 4bd2c1e | 2012-10-08 16:33:10 -0700 | [diff] [blame] | 513 | #if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM) |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 514 | |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 515 | void sock_update_memcg(struct sock *sk) |
| 516 | { |
Glauber Costa | 376be5f | 2012-01-20 04:57:14 +0000 | [diff] [blame] | 517 | if (mem_cgroup_sockets_enabled) { |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 518 | struct mem_cgroup *memcg; |
Glauber Costa | 3f13461 | 2012-05-29 15:07:11 -0700 | [diff] [blame] | 519 | struct cg_proto *cg_proto; |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 520 | |
| 521 | BUG_ON(!sk->sk_prot->proto_cgroup); |
| 522 | |
Glauber Costa | f3f511e | 2012-01-05 20:16:39 +0000 | [diff] [blame] | 523 | /* Socket cloning can throw us here with sk_cgrp already |
| 524 | * filled. It won't however, necessarily happen from |
| 525 | * process context. So the test for root memcg given |
| 526 | * the current task's memcg won't help us in this case. |
| 527 | * |
| 528 | * Respecting the original socket's memcg is a better |
| 529 | * decision in this case. |
| 530 | */ |
| 531 | if (sk->sk_cgrp) { |
| 532 | BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg)); |
| 533 | mem_cgroup_get(sk->sk_cgrp->memcg); |
| 534 | return; |
| 535 | } |
| 536 | |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 537 | rcu_read_lock(); |
| 538 | memcg = mem_cgroup_from_task(current); |
Glauber Costa | 3f13461 | 2012-05-29 15:07:11 -0700 | [diff] [blame] | 539 | cg_proto = sk->sk_prot->proto_cgroup(memcg); |
| 540 | if (!mem_cgroup_is_root(memcg) && memcg_proto_active(cg_proto)) { |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 541 | mem_cgroup_get(memcg); |
Glauber Costa | 3f13461 | 2012-05-29 15:07:11 -0700 | [diff] [blame] | 542 | sk->sk_cgrp = cg_proto; |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 543 | } |
| 544 | rcu_read_unlock(); |
| 545 | } |
| 546 | } |
| 547 | EXPORT_SYMBOL(sock_update_memcg); |
| 548 | |
| 549 | void sock_release_memcg(struct sock *sk) |
| 550 | { |
Glauber Costa | 376be5f | 2012-01-20 04:57:14 +0000 | [diff] [blame] | 551 | if (mem_cgroup_sockets_enabled && sk->sk_cgrp) { |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 552 | struct mem_cgroup *memcg; |
| 553 | WARN_ON(!sk->sk_cgrp->memcg); |
| 554 | memcg = sk->sk_cgrp->memcg; |
| 555 | mem_cgroup_put(memcg); |
| 556 | } |
| 557 | } |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 558 | |
| 559 | struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg) |
| 560 | { |
| 561 | if (!memcg || mem_cgroup_is_root(memcg)) |
| 562 | return NULL; |
| 563 | |
| 564 | return &memcg->tcp_mem.cg_proto; |
| 565 | } |
| 566 | EXPORT_SYMBOL(tcp_proto_cgroup); |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 567 | |
Glauber Costa | 3f13461 | 2012-05-29 15:07:11 -0700 | [diff] [blame] | 568 | static void disarm_sock_keys(struct mem_cgroup *memcg) |
| 569 | { |
| 570 | if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto)) |
| 571 | return; |
| 572 | static_key_slow_dec(&memcg_socket_limit_enabled); |
| 573 | } |
| 574 | #else |
| 575 | static void disarm_sock_keys(struct mem_cgroup *memcg) |
| 576 | { |
| 577 | } |
| 578 | #endif |
| 579 | |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 580 | #ifdef CONFIG_MEMCG_KMEM |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 581 | /* |
| 582 | * This will be the memcg's index in each cache's ->memcg_params->memcg_caches. |
| 583 | * There are two main reasons for not using the css_id for this: |
| 584 | * 1) this works better in sparse environments, where we have a lot of memcgs, |
| 585 | * but only a few kmem-limited. Or also, if we have, for instance, 200 |
| 586 | * memcgs, and none but the 200th is kmem-limited, we'd have to have a |
| 587 | * 200 entry array for that. |
| 588 | * |
| 589 | * 2) In order not to violate the cgroup API, we would like to do all memory |
| 590 | * allocation in ->create(). At that point, we haven't yet allocated the |
| 591 | * css_id. Having a separate index prevents us from messing with the cgroup |
| 592 | * core for this |
| 593 | * |
| 594 | * The current size of the caches array is stored in |
| 595 | * memcg_limited_groups_array_size. It will double each time we have to |
| 596 | * increase it. |
| 597 | */ |
| 598 | static DEFINE_IDA(kmem_limited_groups); |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 599 | int memcg_limited_groups_array_size; |
| 600 | |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 601 | /* |
| 602 | * MIN_SIZE is different than 1, because we would like to avoid going through |
| 603 | * the alloc/free process all the time. In a small machine, 4 kmem-limited |
| 604 | * cgroups is a reasonable guess. In the future, it could be a parameter or |
| 605 | * tunable, but that is strictly not necessary. |
| 606 | * |
| 607 | * MAX_SIZE should be as large as the number of css_ids. Ideally, we could get |
| 608 | * this constant directly from cgroup, but it is understandable that this is |
| 609 | * better kept as an internal representation in cgroup.c. In any case, the |
| 610 | * css_id space is not getting any smaller, and we don't have to necessarily |
| 611 | * increase ours as well if it increases. |
| 612 | */ |
| 613 | #define MEMCG_CACHES_MIN_SIZE 4 |
| 614 | #define MEMCG_CACHES_MAX_SIZE 65535 |
| 615 | |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 616 | /* |
| 617 | * A lot of the calls to the cache allocation functions are expected to be |
| 618 | * inlined by the compiler. Since the calls to memcg_kmem_get_cache are |
| 619 | * conditional to this static branch, we'll have to allow modules that does |
| 620 | * kmem_cache_alloc and the such to see this symbol as well |
| 621 | */ |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 622 | struct static_key memcg_kmem_enabled_key; |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 623 | EXPORT_SYMBOL(memcg_kmem_enabled_key); |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 624 | |
| 625 | static void disarm_kmem_keys(struct mem_cgroup *memcg) |
| 626 | { |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 627 | if (memcg_kmem_is_active(memcg)) { |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 628 | static_key_slow_dec(&memcg_kmem_enabled_key); |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 629 | ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id); |
| 630 | } |
Glauber Costa | bea207c | 2012-12-18 14:22:11 -0800 | [diff] [blame] | 631 | /* |
| 632 | * This check can't live in kmem destruction function, |
| 633 | * since the charges will outlive the cgroup |
| 634 | */ |
| 635 | WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0); |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 636 | } |
| 637 | #else |
| 638 | static void disarm_kmem_keys(struct mem_cgroup *memcg) |
| 639 | { |
| 640 | } |
| 641 | #endif /* CONFIG_MEMCG_KMEM */ |
| 642 | |
| 643 | static void disarm_static_keys(struct mem_cgroup *memcg) |
| 644 | { |
| 645 | disarm_sock_keys(memcg); |
| 646 | disarm_kmem_keys(memcg); |
| 647 | } |
| 648 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 649 | static void drain_all_stock_async(struct mem_cgroup *memcg); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 650 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 651 | static struct mem_cgroup_per_zone * |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 652 | mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 653 | { |
Glauber Costa | 45cf7eb | 2013-02-22 16:34:49 -0800 | [diff] [blame] | 654 | VM_BUG_ON((unsigned)nid >= nr_node_ids); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 655 | return &memcg->info.nodeinfo[nid]->zoneinfo[zid]; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 658 | struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg) |
Wu Fengguang | d324236 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 659 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 660 | return &memcg->css; |
Wu Fengguang | d324236 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 661 | } |
| 662 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 663 | static struct mem_cgroup_per_zone * |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 664 | page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 665 | { |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 666 | int nid = page_to_nid(page); |
| 667 | int zid = page_zonenum(page); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 668 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 669 | return mem_cgroup_zoneinfo(memcg, nid, zid); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | static struct mem_cgroup_tree_per_zone * |
| 673 | soft_limit_tree_node_zone(int nid, int zid) |
| 674 | { |
| 675 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 676 | } |
| 677 | |
| 678 | static struct mem_cgroup_tree_per_zone * |
| 679 | soft_limit_tree_from_page(struct page *page) |
| 680 | { |
| 681 | int nid = page_to_nid(page); |
| 682 | int zid = page_zonenum(page); |
| 683 | |
| 684 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 685 | } |
| 686 | |
| 687 | static void |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 688 | __mem_cgroup_insert_exceeded(struct mem_cgroup *memcg, |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 689 | struct mem_cgroup_per_zone *mz, |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 690 | struct mem_cgroup_tree_per_zone *mctz, |
| 691 | unsigned long long new_usage_in_excess) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 692 | { |
| 693 | struct rb_node **p = &mctz->rb_root.rb_node; |
| 694 | struct rb_node *parent = NULL; |
| 695 | struct mem_cgroup_per_zone *mz_node; |
| 696 | |
| 697 | if (mz->on_tree) |
| 698 | return; |
| 699 | |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 700 | mz->usage_in_excess = new_usage_in_excess; |
| 701 | if (!mz->usage_in_excess) |
| 702 | return; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 703 | while (*p) { |
| 704 | parent = *p; |
| 705 | mz_node = rb_entry(parent, struct mem_cgroup_per_zone, |
| 706 | tree_node); |
| 707 | if (mz->usage_in_excess < mz_node->usage_in_excess) |
| 708 | p = &(*p)->rb_left; |
| 709 | /* |
| 710 | * We can't avoid mem cgroups that are over their soft |
| 711 | * limit by the same amount |
| 712 | */ |
| 713 | else if (mz->usage_in_excess >= mz_node->usage_in_excess) |
| 714 | p = &(*p)->rb_right; |
| 715 | } |
| 716 | rb_link_node(&mz->tree_node, parent, p); |
| 717 | rb_insert_color(&mz->tree_node, &mctz->rb_root); |
| 718 | mz->on_tree = true; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | static void |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 722 | __mem_cgroup_remove_exceeded(struct mem_cgroup *memcg, |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 723 | struct mem_cgroup_per_zone *mz, |
| 724 | struct mem_cgroup_tree_per_zone *mctz) |
| 725 | { |
| 726 | if (!mz->on_tree) |
| 727 | return; |
| 728 | rb_erase(&mz->tree_node, &mctz->rb_root); |
| 729 | mz->on_tree = false; |
| 730 | } |
| 731 | |
| 732 | static void |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 733 | mem_cgroup_remove_exceeded(struct mem_cgroup *memcg, |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 734 | struct mem_cgroup_per_zone *mz, |
| 735 | struct mem_cgroup_tree_per_zone *mctz) |
| 736 | { |
| 737 | spin_lock(&mctz->lock); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 738 | __mem_cgroup_remove_exceeded(memcg, mz, mctz); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 739 | spin_unlock(&mctz->lock); |
| 740 | } |
| 741 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 742 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 743 | static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 744 | { |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 745 | unsigned long long excess; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 746 | struct mem_cgroup_per_zone *mz; |
| 747 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 748 | int nid = page_to_nid(page); |
| 749 | int zid = page_zonenum(page); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 750 | mctz = soft_limit_tree_from_page(page); |
| 751 | |
| 752 | /* |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 753 | * Necessary to update all ancestors when hierarchy is used. |
| 754 | * because their event counter is not touched. |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 755 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 756 | for (; memcg; memcg = parent_mem_cgroup(memcg)) { |
| 757 | mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 758 | excess = res_counter_soft_limit_excess(&memcg->res); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 759 | /* |
| 760 | * We have to update the tree if mz is on RB-tree or |
| 761 | * mem is over its softlimit. |
| 762 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 763 | if (excess || mz->on_tree) { |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 764 | spin_lock(&mctz->lock); |
| 765 | /* if on-tree, remove it */ |
| 766 | if (mz->on_tree) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 767 | __mem_cgroup_remove_exceeded(memcg, mz, mctz); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 768 | /* |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 769 | * Insert again. mz->usage_in_excess will be updated. |
| 770 | * If excess is 0, no tree ops. |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 771 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 772 | __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 773 | spin_unlock(&mctz->lock); |
| 774 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 778 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 779 | { |
| 780 | int node, zone; |
| 781 | struct mem_cgroup_per_zone *mz; |
| 782 | struct mem_cgroup_tree_per_zone *mctz; |
| 783 | |
Bob Liu | 3ed28fa | 2012-01-12 17:19:04 -0800 | [diff] [blame] | 784 | for_each_node(node) { |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 785 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 786 | mz = mem_cgroup_zoneinfo(memcg, node, zone); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 787 | mctz = soft_limit_tree_node_zone(node, zone); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 788 | mem_cgroup_remove_exceeded(memcg, mz, mctz); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | } |
| 792 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 793 | static struct mem_cgroup_per_zone * |
| 794 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 795 | { |
| 796 | struct rb_node *rightmost = NULL; |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 797 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 798 | |
| 799 | retry: |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 800 | mz = NULL; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 801 | rightmost = rb_last(&mctz->rb_root); |
| 802 | if (!rightmost) |
| 803 | goto done; /* Nothing to reclaim from */ |
| 804 | |
| 805 | mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node); |
| 806 | /* |
| 807 | * Remove the node now but someone else can add it back, |
| 808 | * we will to add it back at the end of reclaim to its correct |
| 809 | * position in the tree. |
| 810 | */ |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 811 | __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz); |
| 812 | if (!res_counter_soft_limit_excess(&mz->memcg->res) || |
| 813 | !css_tryget(&mz->memcg->css)) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 814 | goto retry; |
| 815 | done: |
| 816 | return mz; |
| 817 | } |
| 818 | |
| 819 | static struct mem_cgroup_per_zone * |
| 820 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 821 | { |
| 822 | struct mem_cgroup_per_zone *mz; |
| 823 | |
| 824 | spin_lock(&mctz->lock); |
| 825 | mz = __mem_cgroup_largest_soft_limit_node(mctz); |
| 826 | spin_unlock(&mctz->lock); |
| 827 | return mz; |
| 828 | } |
| 829 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 830 | /* |
| 831 | * Implementation Note: reading percpu statistics for memcg. |
| 832 | * |
| 833 | * Both of vmstat[] and percpu_counter has threshold and do periodic |
| 834 | * synchronization to implement "quick" read. There are trade-off between |
| 835 | * reading cost and precision of value. Then, we may have a chance to implement |
| 836 | * a periodic synchronizion of counter in memcg's counter. |
| 837 | * |
| 838 | * But this _read() function is used for user interface now. The user accounts |
| 839 | * memory usage by memory cgroup and he _always_ requires exact value because |
| 840 | * he accounts memory. Even if we provide quick-and-fuzzy read, we always |
| 841 | * have to visit all online cpus and make sum. So, for now, unnecessary |
| 842 | * synchronization is not implemented. (just implemented for cpu hotplug) |
| 843 | * |
| 844 | * If there are kernel internal actions which can make use of some not-exact |
| 845 | * value, and reading all cpu value can be performance bottleneck in some |
| 846 | * common workload, threashold and synchonization as vmstat[] should be |
| 847 | * implemented. |
| 848 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 849 | static long mem_cgroup_read_stat(struct mem_cgroup *memcg, |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 850 | enum mem_cgroup_stat_index idx) |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 851 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 852 | long val = 0; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 853 | int cpu; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 854 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 855 | get_online_cpus(); |
| 856 | for_each_online_cpu(cpu) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 857 | val += per_cpu(memcg->stat->count[idx], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 858 | #ifdef CONFIG_HOTPLUG_CPU |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 859 | spin_lock(&memcg->pcp_counter_lock); |
| 860 | val += memcg->nocpu_base.count[idx]; |
| 861 | spin_unlock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 862 | #endif |
| 863 | put_online_cpus(); |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 864 | return val; |
| 865 | } |
| 866 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 867 | static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg, |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 868 | bool charge) |
| 869 | { |
| 870 | int val = (charge) ? 1 : -1; |
Kamezawa Hiroyuki | bff6bb8 | 2012-07-31 16:41:38 -0700 | [diff] [blame] | 871 | this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 874 | static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg, |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 875 | enum mem_cgroup_events_index idx) |
| 876 | { |
| 877 | unsigned long val = 0; |
| 878 | int cpu; |
| 879 | |
| 880 | for_each_online_cpu(cpu) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 881 | val += per_cpu(memcg->stat->events[idx], cpu); |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 882 | #ifdef CONFIG_HOTPLUG_CPU |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 883 | spin_lock(&memcg->pcp_counter_lock); |
| 884 | val += memcg->nocpu_base.events[idx]; |
| 885 | spin_unlock(&memcg->pcp_counter_lock); |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 886 | #endif |
| 887 | return val; |
| 888 | } |
| 889 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 890 | static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 891 | bool anon, int nr_pages) |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 892 | { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 893 | preempt_disable(); |
| 894 | |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 895 | /* |
| 896 | * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is |
| 897 | * counted as CACHE even if it's on ANON LRU. |
| 898 | */ |
| 899 | if (anon) |
| 900 | __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS], |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 901 | nr_pages); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 902 | else |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 903 | __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE], |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 904 | nr_pages); |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 905 | |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 906 | /* pagein of a big page is an event. So, ignore page size */ |
| 907 | if (nr_pages > 0) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 908 | __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 909 | else { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 910 | __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 911 | nr_pages = -nr_pages; /* for event */ |
| 912 | } |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 913 | |
Johannes Weiner | 1311471 | 2012-05-29 15:07:07 -0700 | [diff] [blame] | 914 | __this_cpu_add(memcg->stat->nr_page_events, nr_pages); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 915 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 916 | preempt_enable(); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 917 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 918 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 919 | unsigned long |
Hugh Dickins | 4d7dcca | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 920 | mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru) |
Konstantin Khlebnikov | 074291f | 2012-05-29 15:07:00 -0700 | [diff] [blame] | 921 | { |
| 922 | struct mem_cgroup_per_zone *mz; |
| 923 | |
| 924 | mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec); |
| 925 | return mz->lru_size[lru]; |
| 926 | } |
| 927 | |
| 928 | static unsigned long |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 929 | mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid, |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 930 | unsigned int lru_mask) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 931 | { |
| 932 | struct mem_cgroup_per_zone *mz; |
Hugh Dickins | f156ab9 | 2012-03-21 16:34:19 -0700 | [diff] [blame] | 933 | enum lru_list lru; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 934 | unsigned long ret = 0; |
| 935 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 936 | mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 937 | |
Hugh Dickins | f156ab9 | 2012-03-21 16:34:19 -0700 | [diff] [blame] | 938 | for_each_lru(lru) { |
| 939 | if (BIT(lru) & lru_mask) |
| 940 | ret += mz->lru_size[lru]; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 941 | } |
| 942 | return ret; |
| 943 | } |
| 944 | |
| 945 | static unsigned long |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 946 | mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 947 | int nid, unsigned int lru_mask) |
| 948 | { |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 949 | u64 total = 0; |
| 950 | int zid; |
| 951 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 952 | for (zid = 0; zid < MAX_NR_ZONES; zid++) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 953 | total += mem_cgroup_zone_nr_lru_pages(memcg, |
| 954 | nid, zid, lru_mask); |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 955 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 956 | return total; |
| 957 | } |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 958 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 959 | static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 960 | unsigned int lru_mask) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 961 | { |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 962 | int nid; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 963 | u64 total = 0; |
| 964 | |
Lai Jiangshan | 31aaea4 | 2012-12-12 13:51:27 -0800 | [diff] [blame] | 965 | for_each_node_state(nid, N_MEMORY) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 966 | total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 967 | return total; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 968 | } |
| 969 | |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 970 | static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg, |
| 971 | enum mem_cgroup_events_target target) |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 972 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 973 | unsigned long val, next; |
| 974 | |
Johannes Weiner | 1311471 | 2012-05-29 15:07:07 -0700 | [diff] [blame] | 975 | val = __this_cpu_read(memcg->stat->nr_page_events); |
Steven Rostedt | 4799401 | 2011-11-02 13:38:33 -0700 | [diff] [blame] | 976 | next = __this_cpu_read(memcg->stat->targets[target]); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 977 | /* from time_after() in jiffies.h */ |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 978 | if ((long)next - (long)val < 0) { |
| 979 | switch (target) { |
| 980 | case MEM_CGROUP_TARGET_THRESH: |
| 981 | next = val + THRESHOLDS_EVENTS_TARGET; |
| 982 | break; |
| 983 | case MEM_CGROUP_TARGET_SOFTLIMIT: |
| 984 | next = val + SOFTLIMIT_EVENTS_TARGET; |
| 985 | break; |
| 986 | case MEM_CGROUP_TARGET_NUMAINFO: |
| 987 | next = val + NUMAINFO_EVENTS_TARGET; |
| 988 | break; |
| 989 | default: |
| 990 | break; |
| 991 | } |
| 992 | __this_cpu_write(memcg->stat->targets[target], next); |
| 993 | return true; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 994 | } |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 995 | return false; |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | /* |
| 999 | * Check events in order. |
| 1000 | * |
| 1001 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1002 | static void memcg_check_events(struct mem_cgroup *memcg, struct page *page) |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 1003 | { |
Steven Rostedt | 4799401 | 2011-11-02 13:38:33 -0700 | [diff] [blame] | 1004 | preempt_disable(); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 1005 | /* threshold event is triggered in finer grain than soft limit */ |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 1006 | if (unlikely(mem_cgroup_event_ratelimit(memcg, |
| 1007 | MEM_CGROUP_TARGET_THRESH))) { |
Andrew Morton | 82b3f2a | 2012-02-03 15:37:14 -0800 | [diff] [blame] | 1008 | bool do_softlimit; |
| 1009 | bool do_numainfo __maybe_unused; |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 1010 | |
| 1011 | do_softlimit = mem_cgroup_event_ratelimit(memcg, |
| 1012 | MEM_CGROUP_TARGET_SOFTLIMIT); |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1013 | #if MAX_NUMNODES > 1 |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 1014 | do_numainfo = mem_cgroup_event_ratelimit(memcg, |
| 1015 | MEM_CGROUP_TARGET_NUMAINFO); |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1016 | #endif |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 1017 | preempt_enable(); |
| 1018 | |
| 1019 | mem_cgroup_threshold(memcg); |
| 1020 | if (unlikely(do_softlimit)) |
| 1021 | mem_cgroup_update_tree(memcg, page); |
| 1022 | #if MAX_NUMNODES > 1 |
| 1023 | if (unlikely(do_numainfo)) |
| 1024 | atomic_inc(&memcg->numainfo_events); |
| 1025 | #endif |
| 1026 | } else |
| 1027 | preempt_enable(); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 1030 | struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1031 | { |
Wanpeng Li | b214514 | 2012-07-31 16:46:01 -0700 | [diff] [blame] | 1032 | return mem_cgroup_from_css( |
| 1033 | cgroup_subsys_state(cont, mem_cgroup_subsys_id)); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 1036 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1037 | { |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 1038 | /* |
| 1039 | * mm_update_next_owner() may clear mm->owner to NULL |
| 1040 | * if it races with swapoff, page migration, etc. |
| 1041 | * So this can be called with p == NULL. |
| 1042 | */ |
| 1043 | if (unlikely(!p)) |
| 1044 | return NULL; |
| 1045 | |
Wanpeng Li | b214514 | 2012-07-31 16:46:01 -0700 | [diff] [blame] | 1046 | return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id)); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 1049 | 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] | 1050 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1051 | struct mem_cgroup *memcg = NULL; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1052 | |
| 1053 | if (!mm) |
| 1054 | return NULL; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1055 | /* |
| 1056 | * Because we have no locks, mm->owner's may be being moved to other |
| 1057 | * cgroup. We use css_tryget() here even if this looks |
| 1058 | * pessimistic (rather than adding locks here). |
| 1059 | */ |
| 1060 | rcu_read_lock(); |
| 1061 | do { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1062 | memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 1063 | if (unlikely(!memcg)) |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1064 | break; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1065 | } while (!css_tryget(&memcg->css)); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1066 | rcu_read_unlock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1067 | return memcg; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1070 | /** |
| 1071 | * mem_cgroup_iter - iterate over memory cgroup hierarchy |
| 1072 | * @root: hierarchy root |
| 1073 | * @prev: previously returned memcg, NULL on first invocation |
| 1074 | * @reclaim: cookie for shared reclaim walks, NULL for full walks |
| 1075 | * |
| 1076 | * Returns references to children of the hierarchy below @root, or |
| 1077 | * @root itself, or %NULL after a full round-trip. |
| 1078 | * |
| 1079 | * Caller must pass the return value in @prev on subsequent |
| 1080 | * invocations for reference counting, or use mem_cgroup_iter_break() |
| 1081 | * to cancel a hierarchy walk before the round-trip is complete. |
| 1082 | * |
| 1083 | * Reclaimers can specify a zone and a priority level in @reclaim to |
| 1084 | * divide up the memcgs in the hierarchy among all concurrent |
| 1085 | * reclaimers operating on the same zone and priority. |
| 1086 | */ |
| 1087 | struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, |
| 1088 | struct mem_cgroup *prev, |
| 1089 | struct mem_cgroup_reclaim_cookie *reclaim) |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 1090 | { |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1091 | struct mem_cgroup *memcg = NULL; |
| 1092 | int id = 0; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1093 | |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1094 | if (mem_cgroup_disabled()) |
| 1095 | return NULL; |
| 1096 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1097 | if (!root) |
| 1098 | root = root_mem_cgroup; |
| 1099 | |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1100 | if (prev && !reclaim) |
| 1101 | id = css_id(&prev->css); |
| 1102 | |
| 1103 | if (prev && prev != root) |
| 1104 | css_put(&prev->css); |
| 1105 | |
| 1106 | if (!root->use_hierarchy && root != root_mem_cgroup) { |
| 1107 | if (prev) |
| 1108 | return NULL; |
| 1109 | return root; |
| 1110 | } |
| 1111 | |
| 1112 | while (!memcg) { |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 1113 | struct mem_cgroup_reclaim_iter *uninitialized_var(iter); |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1114 | struct cgroup_subsys_state *css; |
| 1115 | |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 1116 | if (reclaim) { |
| 1117 | int nid = zone_to_nid(reclaim->zone); |
| 1118 | int zid = zone_idx(reclaim->zone); |
| 1119 | struct mem_cgroup_per_zone *mz; |
| 1120 | |
| 1121 | mz = mem_cgroup_zoneinfo(root, nid, zid); |
| 1122 | iter = &mz->reclaim_iter[reclaim->priority]; |
| 1123 | if (prev && reclaim->generation != iter->generation) |
| 1124 | return NULL; |
| 1125 | id = iter->position; |
| 1126 | } |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1127 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1128 | rcu_read_lock(); |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1129 | css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id); |
| 1130 | if (css) { |
| 1131 | if (css == &root->css || css_tryget(css)) |
Wanpeng Li | b214514 | 2012-07-31 16:46:01 -0700 | [diff] [blame] | 1132 | memcg = mem_cgroup_from_css(css); |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1133 | } else |
| 1134 | id = 0; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1135 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1136 | |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 1137 | if (reclaim) { |
| 1138 | iter->position = id; |
| 1139 | if (!css) |
| 1140 | iter->generation++; |
| 1141 | else if (!prev && memcg) |
| 1142 | reclaim->generation = iter->generation; |
| 1143 | } |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1144 | |
| 1145 | if (prev && !css) |
| 1146 | return NULL; |
| 1147 | } |
| 1148 | return memcg; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1149 | } |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1150 | |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1151 | /** |
| 1152 | * mem_cgroup_iter_break - abort a hierarchy walk prematurely |
| 1153 | * @root: hierarchy root |
| 1154 | * @prev: last visited hierarchy member as returned by mem_cgroup_iter() |
| 1155 | */ |
| 1156 | void mem_cgroup_iter_break(struct mem_cgroup *root, |
| 1157 | struct mem_cgroup *prev) |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1158 | { |
| 1159 | if (!root) |
| 1160 | root = root_mem_cgroup; |
| 1161 | if (prev && prev != root) |
| 1162 | css_put(&prev->css); |
| 1163 | } |
| 1164 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1165 | /* |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1166 | * Iteration constructs for visiting all cgroups (under a tree). If |
| 1167 | * loops are exited prematurely (break), mem_cgroup_iter_break() must |
| 1168 | * be used for reference counting. |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1169 | */ |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1170 | #define for_each_mem_cgroup_tree(iter, root) \ |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 1171 | for (iter = mem_cgroup_iter(root, NULL, NULL); \ |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1172 | iter != NULL; \ |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 1173 | iter = mem_cgroup_iter(root, iter, NULL)) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1174 | |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1175 | #define for_each_mem_cgroup(iter) \ |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 1176 | for (iter = mem_cgroup_iter(NULL, NULL, NULL); \ |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1177 | iter != NULL; \ |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 1178 | iter = mem_cgroup_iter(NULL, iter, NULL)) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1179 | |
David Rientjes | 68ae564 | 2012-12-12 13:51:57 -0800 | [diff] [blame] | 1180 | void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx) |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 1181 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1182 | struct mem_cgroup *memcg; |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 1183 | |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 1184 | rcu_read_lock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1185 | memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 1186 | if (unlikely(!memcg)) |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 1187 | goto out; |
| 1188 | |
| 1189 | switch (idx) { |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 1190 | case PGFAULT: |
Johannes Weiner | 0e574a9 | 2012-01-12 17:18:35 -0800 | [diff] [blame] | 1191 | this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]); |
| 1192 | break; |
| 1193 | case PGMAJFAULT: |
| 1194 | this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 1195 | break; |
| 1196 | default: |
| 1197 | BUG(); |
| 1198 | } |
| 1199 | out: |
| 1200 | rcu_read_unlock(); |
| 1201 | } |
David Rientjes | 68ae564 | 2012-12-12 13:51:57 -0800 | [diff] [blame] | 1202 | EXPORT_SYMBOL(__mem_cgroup_count_vm_event); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 1203 | |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1204 | /** |
| 1205 | * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg |
| 1206 | * @zone: zone of the wanted lruvec |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1207 | * @memcg: memcg of the wanted lruvec |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1208 | * |
| 1209 | * Returns the lru list vector holding pages for the given @zone and |
| 1210 | * @mem. This can be the global zone lruvec, if the memory controller |
| 1211 | * is disabled. |
| 1212 | */ |
| 1213 | struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone, |
| 1214 | struct mem_cgroup *memcg) |
| 1215 | { |
| 1216 | struct mem_cgroup_per_zone *mz; |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 1217 | struct lruvec *lruvec; |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1218 | |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 1219 | if (mem_cgroup_disabled()) { |
| 1220 | lruvec = &zone->lruvec; |
| 1221 | goto out; |
| 1222 | } |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1223 | |
| 1224 | mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone)); |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 1225 | lruvec = &mz->lruvec; |
| 1226 | out: |
| 1227 | /* |
| 1228 | * Since a node can be onlined after the mem_cgroup was created, |
| 1229 | * we have to be prepared to initialize lruvec->zone here; |
| 1230 | * and if offlined then reonlined, we need to reinitialize it. |
| 1231 | */ |
| 1232 | if (unlikely(lruvec->zone != zone)) |
| 1233 | lruvec->zone = zone; |
| 1234 | return lruvec; |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1237 | /* |
| 1238 | * Following LRU functions are allowed to be used without PCG_LOCK. |
| 1239 | * Operations are called by routine of global LRU independently from memcg. |
| 1240 | * What we have to take care of here is validness of pc->mem_cgroup. |
| 1241 | * |
| 1242 | * Changes to pc->mem_cgroup happens when |
| 1243 | * 1. charge |
| 1244 | * 2. moving account |
| 1245 | * In typical case, "charge" is done before add-to-lru. Exception is SwapCache. |
| 1246 | * It is added to LRU before charge. |
| 1247 | * If PCG_USED bit is not set, page_cgroup is not added to this private LRU. |
| 1248 | * When moving account, the page is not on LRU. It's isolated. |
| 1249 | */ |
| 1250 | |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1251 | /** |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1252 | * mem_cgroup_page_lruvec - return lruvec for adding an lru page |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1253 | * @page: the page |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1254 | * @zone: zone of the page |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 1255 | */ |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1256 | struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone) |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 1257 | { |
| 1258 | struct mem_cgroup_per_zone *mz; |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1259 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1260 | struct page_cgroup *pc; |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 1261 | struct lruvec *lruvec; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1262 | |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 1263 | if (mem_cgroup_disabled()) { |
| 1264 | lruvec = &zone->lruvec; |
| 1265 | goto out; |
| 1266 | } |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1267 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1268 | pc = lookup_page_cgroup(page); |
KAMEZAWA Hiroyuki | 38c5d72 | 2012-01-12 17:19:01 -0800 | [diff] [blame] | 1269 | memcg = pc->mem_cgroup; |
Hugh Dickins | 7512102 | 2012-03-05 14:59:18 -0800 | [diff] [blame] | 1270 | |
| 1271 | /* |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1272 | * Surreptitiously switch any uncharged offlist page to root: |
Hugh Dickins | 7512102 | 2012-03-05 14:59:18 -0800 | [diff] [blame] | 1273 | * an uncharged page off lru does nothing to secure |
| 1274 | * its former mem_cgroup from sudden removal. |
| 1275 | * |
| 1276 | * Our caller holds lru_lock, and PageCgroupUsed is updated |
| 1277 | * under page_cgroup lock: between them, they make all uses |
| 1278 | * of pc->mem_cgroup safe. |
| 1279 | */ |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1280 | if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup) |
Hugh Dickins | 7512102 | 2012-03-05 14:59:18 -0800 | [diff] [blame] | 1281 | pc->mem_cgroup = memcg = root_mem_cgroup; |
| 1282 | |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1283 | mz = page_cgroup_zoneinfo(memcg, page); |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 1284 | lruvec = &mz->lruvec; |
| 1285 | out: |
| 1286 | /* |
| 1287 | * Since a node can be onlined after the mem_cgroup was created, |
| 1288 | * we have to be prepared to initialize lruvec->zone here; |
| 1289 | * and if offlined then reonlined, we need to reinitialize it. |
| 1290 | */ |
| 1291 | if (unlikely(lruvec->zone != zone)) |
| 1292 | lruvec->zone = zone; |
| 1293 | return lruvec; |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | /** |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1297 | * mem_cgroup_update_lru_size - account for adding or removing an lru page |
| 1298 | * @lruvec: mem_cgroup per zone lru vector |
| 1299 | * @lru: index of lru list the page is sitting on |
| 1300 | * @nr_pages: positive when adding or negative when removing |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1301 | * |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1302 | * This function must be called when a page is added to or removed from an |
| 1303 | * lru list. |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1304 | */ |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1305 | void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru, |
| 1306 | int nr_pages) |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1307 | { |
| 1308 | struct mem_cgroup_per_zone *mz; |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1309 | unsigned long *lru_size; |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1310 | |
| 1311 | if (mem_cgroup_disabled()) |
| 1312 | return; |
| 1313 | |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1314 | mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec); |
| 1315 | lru_size = mz->lru_size + lru; |
| 1316 | *lru_size += nr_pages; |
| 1317 | VM_BUG_ON((long)(*lru_size) < 0); |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1318 | } |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1319 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1320 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1321 | * Checks whether given mem is same or in the root_mem_cgroup's |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1322 | * hierarchy subtree |
| 1323 | */ |
Johannes Weiner | c3ac9a8 | 2012-05-29 15:06:25 -0700 | [diff] [blame] | 1324 | bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg, |
| 1325 | struct mem_cgroup *memcg) |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1326 | { |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 1327 | if (root_memcg == memcg) |
| 1328 | return true; |
Hugh Dickins | 3a981f4 | 2012-06-20 12:52:58 -0700 | [diff] [blame] | 1329 | if (!root_memcg->use_hierarchy || !memcg) |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 1330 | return false; |
Johannes Weiner | c3ac9a8 | 2012-05-29 15:06:25 -0700 | [diff] [blame] | 1331 | return css_is_ancestor(&memcg->css, &root_memcg->css); |
| 1332 | } |
| 1333 | |
| 1334 | static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg, |
| 1335 | struct mem_cgroup *memcg) |
| 1336 | { |
| 1337 | bool ret; |
| 1338 | |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 1339 | rcu_read_lock(); |
Johannes Weiner | c3ac9a8 | 2012-05-29 15:06:25 -0700 | [diff] [blame] | 1340 | ret = __mem_cgroup_same_or_subtree(root_memcg, memcg); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 1341 | rcu_read_unlock(); |
| 1342 | return ret; |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1345 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg) |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1346 | { |
| 1347 | int ret; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1348 | struct mem_cgroup *curr = NULL; |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1349 | struct task_struct *p; |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1350 | |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1351 | p = find_lock_task_mm(task); |
David Rientjes | de077d2 | 2012-01-12 17:18:52 -0800 | [diff] [blame] | 1352 | if (p) { |
| 1353 | curr = try_get_mem_cgroup_from_mm(p->mm); |
| 1354 | task_unlock(p); |
| 1355 | } else { |
| 1356 | /* |
| 1357 | * All threads may have already detached their mm's, but the oom |
| 1358 | * killer still needs to detect if they have already been oom |
| 1359 | * killed to prevent needlessly killing additional tasks. |
| 1360 | */ |
| 1361 | task_lock(task); |
| 1362 | curr = mem_cgroup_from_task(task); |
| 1363 | if (curr) |
| 1364 | css_get(&curr->css); |
| 1365 | task_unlock(task); |
| 1366 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1367 | if (!curr) |
| 1368 | return 0; |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1369 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1370 | * We should check use_hierarchy of "memcg" not "curr". Because checking |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1371 | * use_hierarchy of "curr" here make this function true if hierarchy is |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1372 | * enabled in "curr" and "curr" is a child of "memcg" in *cgroup* |
| 1373 | * hierarchy(even if use_hierarchy is disabled in "memcg"). |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1374 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1375 | ret = mem_cgroup_same_or_subtree(memcg, curr); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1376 | css_put(&curr->css); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1377 | return ret; |
| 1378 | } |
| 1379 | |
Konstantin Khlebnikov | c56d5c7 | 2012-05-29 15:07:00 -0700 | [diff] [blame] | 1380 | int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec) |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1381 | { |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1382 | unsigned long inactive_ratio; |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1383 | unsigned long inactive; |
| 1384 | unsigned long active; |
| 1385 | unsigned long gb; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1386 | |
Hugh Dickins | 4d7dcca | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 1387 | inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON); |
| 1388 | active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1389 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1390 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
| 1391 | if (gb) |
| 1392 | inactive_ratio = int_sqrt(10 * gb); |
| 1393 | else |
| 1394 | inactive_ratio = 1; |
| 1395 | |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1396 | return inactive * inactive_ratio < active; |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1399 | #define mem_cgroup_from_res_counter(counter, member) \ |
| 1400 | container_of(counter, struct mem_cgroup, member) |
| 1401 | |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1402 | /** |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1403 | * mem_cgroup_margin - calculate chargeable space of a memory cgroup |
Wanpeng Li | dad7557 | 2012-06-20 12:53:01 -0700 | [diff] [blame] | 1404 | * @memcg: the memory cgroup |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1405 | * |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1406 | * Returns the maximum amount of memory @mem can be charged with, in |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1407 | * pages. |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1408 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1409 | static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1410 | { |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1411 | unsigned long long margin; |
| 1412 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1413 | margin = res_counter_margin(&memcg->res); |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1414 | if (do_swap_account) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1415 | margin = min(margin, res_counter_margin(&memcg->memsw)); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1416 | return margin >> PAGE_SHIFT; |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1417 | } |
| 1418 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 1419 | int mem_cgroup_swappiness(struct mem_cgroup *memcg) |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1420 | { |
| 1421 | struct cgroup *cgrp = memcg->css.cgroup; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1422 | |
| 1423 | /* root ? */ |
| 1424 | if (cgrp->parent == NULL) |
| 1425 | return vm_swappiness; |
| 1426 | |
Johannes Weiner | bf1ff26 | 2011-03-23 16:42:32 -0700 | [diff] [blame] | 1427 | return memcg->swappiness; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1428 | } |
| 1429 | |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 1430 | /* |
| 1431 | * memcg->moving_account is used for checking possibility that some thread is |
| 1432 | * calling move_account(). When a thread on CPU-A starts moving pages under |
| 1433 | * a memcg, other threads should check memcg->moving_account under |
| 1434 | * rcu_read_lock(), like this: |
| 1435 | * |
| 1436 | * CPU-A CPU-B |
| 1437 | * rcu_read_lock() |
| 1438 | * memcg->moving_account+1 if (memcg->mocing_account) |
| 1439 | * take heavy locks. |
| 1440 | * synchronize_rcu() update something. |
| 1441 | * rcu_read_unlock() |
| 1442 | * start move here. |
| 1443 | */ |
KAMEZAWA Hiroyuki | 4331f7d | 2012-03-21 16:34:26 -0700 | [diff] [blame] | 1444 | |
| 1445 | /* for quick checking without looking up memcg */ |
| 1446 | atomic_t memcg_moving __read_mostly; |
| 1447 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1448 | static void mem_cgroup_start_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1449 | { |
KAMEZAWA Hiroyuki | 4331f7d | 2012-03-21 16:34:26 -0700 | [diff] [blame] | 1450 | atomic_inc(&memcg_moving); |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 1451 | atomic_inc(&memcg->moving_account); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1452 | synchronize_rcu(); |
| 1453 | } |
| 1454 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1455 | static void mem_cgroup_end_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1456 | { |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 1457 | /* |
| 1458 | * Now, mem_cgroup_clear_mc() may call this function with NULL. |
| 1459 | * We check NULL in callee rather than caller. |
| 1460 | */ |
KAMEZAWA Hiroyuki | 4331f7d | 2012-03-21 16:34:26 -0700 | [diff] [blame] | 1461 | if (memcg) { |
| 1462 | atomic_dec(&memcg_moving); |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 1463 | atomic_dec(&memcg->moving_account); |
KAMEZAWA Hiroyuki | 4331f7d | 2012-03-21 16:34:26 -0700 | [diff] [blame] | 1464 | } |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1465 | } |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 1466 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1467 | /* |
| 1468 | * 2 routines for checking "mem" is under move_account() or not. |
| 1469 | * |
Andrew Morton | 13fd1dd9 | 2012-03-21 16:34:26 -0700 | [diff] [blame] | 1470 | * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This |
| 1471 | * is used for avoiding races in accounting. If true, |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1472 | * pc->mem_cgroup may be overwritten. |
| 1473 | * |
| 1474 | * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or |
| 1475 | * under hierarchy of moving cgroups. This is for |
| 1476 | * waiting at hith-memory prressure caused by "move". |
| 1477 | */ |
| 1478 | |
Andrew Morton | 13fd1dd9 | 2012-03-21 16:34:26 -0700 | [diff] [blame] | 1479 | static bool mem_cgroup_stolen(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1480 | { |
| 1481 | VM_BUG_ON(!rcu_read_lock_held()); |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 1482 | return atomic_read(&memcg->moving_account) > 0; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1483 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1484 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1485 | static bool mem_cgroup_under_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1486 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1487 | struct mem_cgroup *from; |
| 1488 | struct mem_cgroup *to; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1489 | bool ret = false; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1490 | /* |
| 1491 | * Unlike task_move routines, we access mc.to, mc.from not under |
| 1492 | * mutual exclusion by cgroup_mutex. Here, we take spinlock instead. |
| 1493 | */ |
| 1494 | spin_lock(&mc.lock); |
| 1495 | from = mc.from; |
| 1496 | to = mc.to; |
| 1497 | if (!from) |
| 1498 | goto unlock; |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1499 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1500 | ret = mem_cgroup_same_or_subtree(memcg, from) |
| 1501 | || mem_cgroup_same_or_subtree(memcg, to); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1502 | unlock: |
| 1503 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1504 | return ret; |
| 1505 | } |
| 1506 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1507 | static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1508 | { |
| 1509 | if (mc.moving_task && current != mc.moving_task) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1510 | if (mem_cgroup_under_move(memcg)) { |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1511 | DEFINE_WAIT(wait); |
| 1512 | prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE); |
| 1513 | /* moving charge context might have finished. */ |
| 1514 | if (mc.moving_task) |
| 1515 | schedule(); |
| 1516 | finish_wait(&mc.waitq, &wait); |
| 1517 | return true; |
| 1518 | } |
| 1519 | } |
| 1520 | return false; |
| 1521 | } |
| 1522 | |
KAMEZAWA Hiroyuki | 312734c0 | 2012-03-21 16:34:24 -0700 | [diff] [blame] | 1523 | /* |
| 1524 | * Take this lock when |
| 1525 | * - a code tries to modify page's memcg while it's USED. |
| 1526 | * - a code tries to modify page state accounting in a memcg. |
Andrew Morton | 13fd1dd9 | 2012-03-21 16:34:26 -0700 | [diff] [blame] | 1527 | * see mem_cgroup_stolen(), too. |
KAMEZAWA Hiroyuki | 312734c0 | 2012-03-21 16:34:24 -0700 | [diff] [blame] | 1528 | */ |
| 1529 | static void move_lock_mem_cgroup(struct mem_cgroup *memcg, |
| 1530 | unsigned long *flags) |
| 1531 | { |
| 1532 | spin_lock_irqsave(&memcg->move_lock, *flags); |
| 1533 | } |
| 1534 | |
| 1535 | static void move_unlock_mem_cgroup(struct mem_cgroup *memcg, |
| 1536 | unsigned long *flags) |
| 1537 | { |
| 1538 | spin_unlock_irqrestore(&memcg->move_lock, *flags); |
| 1539 | } |
| 1540 | |
Sha Zhengju | 58cf188 | 2013-02-22 16:32:05 -0800 | [diff] [blame] | 1541 | #define K(x) ((x) << (PAGE_SHIFT-10)) |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1542 | /** |
Sha Zhengju | 58cf188 | 2013-02-22 16:32:05 -0800 | [diff] [blame] | 1543 | * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller. |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1544 | * @memcg: The memory cgroup that went over limit |
| 1545 | * @p: Task that is going to be killed |
| 1546 | * |
| 1547 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is |
| 1548 | * enabled |
| 1549 | */ |
| 1550 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) |
| 1551 | { |
| 1552 | struct cgroup *task_cgrp; |
| 1553 | struct cgroup *mem_cgrp; |
| 1554 | /* |
| 1555 | * Need a buffer in BSS, can't rely on allocations. The code relies |
| 1556 | * on the assumption that OOM is serialized for memory controller. |
| 1557 | * If this assumption is broken, revisit this code. |
| 1558 | */ |
| 1559 | static char memcg_name[PATH_MAX]; |
| 1560 | int ret; |
Sha Zhengju | 58cf188 | 2013-02-22 16:32:05 -0800 | [diff] [blame] | 1561 | struct mem_cgroup *iter; |
| 1562 | unsigned int i; |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1563 | |
Sha Zhengju | 58cf188 | 2013-02-22 16:32:05 -0800 | [diff] [blame] | 1564 | if (!p) |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1565 | return; |
| 1566 | |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1567 | rcu_read_lock(); |
| 1568 | |
| 1569 | mem_cgrp = memcg->css.cgroup; |
| 1570 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); |
| 1571 | |
| 1572 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); |
| 1573 | if (ret < 0) { |
| 1574 | /* |
| 1575 | * Unfortunately, we are unable to convert to a useful name |
| 1576 | * But we'll still print out the usage information |
| 1577 | */ |
| 1578 | rcu_read_unlock(); |
| 1579 | goto done; |
| 1580 | } |
| 1581 | rcu_read_unlock(); |
| 1582 | |
Andrew Morton | d045197 | 2013-02-22 16:32:06 -0800 | [diff] [blame] | 1583 | pr_info("Task in %s killed", memcg_name); |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1584 | |
| 1585 | rcu_read_lock(); |
| 1586 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); |
| 1587 | if (ret < 0) { |
| 1588 | rcu_read_unlock(); |
| 1589 | goto done; |
| 1590 | } |
| 1591 | rcu_read_unlock(); |
| 1592 | |
| 1593 | /* |
| 1594 | * Continues from above, so we don't need an KERN_ level |
| 1595 | */ |
Andrew Morton | d045197 | 2013-02-22 16:32:06 -0800 | [diff] [blame] | 1596 | pr_cont(" as a result of limit of %s\n", memcg_name); |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1597 | done: |
| 1598 | |
Andrew Morton | d045197 | 2013-02-22 16:32:06 -0800 | [diff] [blame] | 1599 | pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n", |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1600 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, |
| 1601 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, |
| 1602 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); |
Andrew Morton | d045197 | 2013-02-22 16:32:06 -0800 | [diff] [blame] | 1603 | pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n", |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1604 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, |
| 1605 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, |
| 1606 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); |
Andrew Morton | d045197 | 2013-02-22 16:32:06 -0800 | [diff] [blame] | 1607 | pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n", |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 1608 | res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10, |
| 1609 | res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10, |
| 1610 | res_counter_read_u64(&memcg->kmem, RES_FAILCNT)); |
Sha Zhengju | 58cf188 | 2013-02-22 16:32:05 -0800 | [diff] [blame] | 1611 | |
| 1612 | for_each_mem_cgroup_tree(iter, memcg) { |
| 1613 | pr_info("Memory cgroup stats"); |
| 1614 | |
| 1615 | rcu_read_lock(); |
| 1616 | ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX); |
| 1617 | if (!ret) |
| 1618 | pr_cont(" for %s", memcg_name); |
| 1619 | rcu_read_unlock(); |
| 1620 | pr_cont(":"); |
| 1621 | |
| 1622 | for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) { |
| 1623 | if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account) |
| 1624 | continue; |
| 1625 | pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i], |
| 1626 | K(mem_cgroup_read_stat(iter, i))); |
| 1627 | } |
| 1628 | |
| 1629 | for (i = 0; i < NR_LRU_LISTS; i++) |
| 1630 | pr_cont(" %s:%luKB", mem_cgroup_lru_names[i], |
| 1631 | K(mem_cgroup_nr_lru_pages(iter, BIT(i)))); |
| 1632 | |
| 1633 | pr_cont("\n"); |
| 1634 | } |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1637 | /* |
| 1638 | * This function returns the number of memcg under hierarchy tree. Returns |
| 1639 | * 1(self count) if no children. |
| 1640 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1641 | static int mem_cgroup_count_children(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1642 | { |
| 1643 | int num = 0; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1644 | struct mem_cgroup *iter; |
| 1645 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1646 | for_each_mem_cgroup_tree(iter, memcg) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1647 | num++; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1648 | return num; |
| 1649 | } |
| 1650 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1651 | /* |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1652 | * Return the memory (and swap, if configured) limit for a memcg. |
| 1653 | */ |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1654 | static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg) |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1655 | { |
| 1656 | u64 limit; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1657 | |
Johannes Weiner | f3e8eb7 | 2011-01-13 15:47:39 -0800 | [diff] [blame] | 1658 | limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
Johannes Weiner | f3e8eb7 | 2011-01-13 15:47:39 -0800 | [diff] [blame] | 1659 | |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1660 | /* |
Michal Hocko | 9a5a8f1 | 2012-11-16 14:14:49 -0800 | [diff] [blame] | 1661 | * Do not consider swap space if we cannot swap due to swappiness |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1662 | */ |
Michal Hocko | 9a5a8f1 | 2012-11-16 14:14:49 -0800 | [diff] [blame] | 1663 | if (mem_cgroup_swappiness(memcg)) { |
| 1664 | u64 memsw; |
| 1665 | |
| 1666 | limit += total_swap_pages << PAGE_SHIFT; |
| 1667 | memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 1668 | |
| 1669 | /* |
| 1670 | * If memsw is finite and limits the amount of swap space |
| 1671 | * available to this memcg, return that limit. |
| 1672 | */ |
| 1673 | limit = min(limit, memsw); |
| 1674 | } |
| 1675 | |
| 1676 | return limit; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1677 | } |
| 1678 | |
David Rientjes | 1996546 | 2012-12-11 16:00:26 -0800 | [diff] [blame] | 1679 | static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, |
| 1680 | int order) |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1681 | { |
| 1682 | struct mem_cgroup *iter; |
| 1683 | unsigned long chosen_points = 0; |
| 1684 | unsigned long totalpages; |
| 1685 | unsigned int points = 0; |
| 1686 | struct task_struct *chosen = NULL; |
| 1687 | |
David Rientjes | 876aafb | 2012-07-31 16:43:48 -0700 | [diff] [blame] | 1688 | /* |
| 1689 | * If current has a pending SIGKILL, then automatically select it. The |
| 1690 | * goal is to allow it to allocate so that it may quickly exit and free |
| 1691 | * its memory. |
| 1692 | */ |
| 1693 | if (fatal_signal_pending(current)) { |
| 1694 | set_thread_flag(TIF_MEMDIE); |
| 1695 | return; |
| 1696 | } |
| 1697 | |
| 1698 | check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL); |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1699 | totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1; |
| 1700 | for_each_mem_cgroup_tree(iter, memcg) { |
| 1701 | struct cgroup *cgroup = iter->css.cgroup; |
| 1702 | struct cgroup_iter it; |
| 1703 | struct task_struct *task; |
| 1704 | |
| 1705 | cgroup_iter_start(cgroup, &it); |
| 1706 | while ((task = cgroup_iter_next(cgroup, &it))) { |
| 1707 | switch (oom_scan_process_thread(task, totalpages, NULL, |
| 1708 | false)) { |
| 1709 | case OOM_SCAN_SELECT: |
| 1710 | if (chosen) |
| 1711 | put_task_struct(chosen); |
| 1712 | chosen = task; |
| 1713 | chosen_points = ULONG_MAX; |
| 1714 | get_task_struct(chosen); |
| 1715 | /* fall through */ |
| 1716 | case OOM_SCAN_CONTINUE: |
| 1717 | continue; |
| 1718 | case OOM_SCAN_ABORT: |
| 1719 | cgroup_iter_end(cgroup, &it); |
| 1720 | mem_cgroup_iter_break(memcg, iter); |
| 1721 | if (chosen) |
| 1722 | put_task_struct(chosen); |
| 1723 | return; |
| 1724 | case OOM_SCAN_OK: |
| 1725 | break; |
| 1726 | }; |
| 1727 | points = oom_badness(task, memcg, NULL, totalpages); |
| 1728 | if (points > chosen_points) { |
| 1729 | if (chosen) |
| 1730 | put_task_struct(chosen); |
| 1731 | chosen = task; |
| 1732 | chosen_points = points; |
| 1733 | get_task_struct(chosen); |
| 1734 | } |
| 1735 | } |
| 1736 | cgroup_iter_end(cgroup, &it); |
| 1737 | } |
| 1738 | |
| 1739 | if (!chosen) |
| 1740 | return; |
| 1741 | points = chosen_points * 1000 / totalpages; |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1742 | oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg, |
| 1743 | NULL, "Memory cgroup out of memory"); |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1746 | static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg, |
| 1747 | gfp_t gfp_mask, |
| 1748 | unsigned long flags) |
| 1749 | { |
| 1750 | unsigned long total = 0; |
| 1751 | bool noswap = false; |
| 1752 | int loop; |
| 1753 | |
| 1754 | if (flags & MEM_CGROUP_RECLAIM_NOSWAP) |
| 1755 | noswap = true; |
| 1756 | if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum) |
| 1757 | noswap = true; |
| 1758 | |
| 1759 | for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) { |
| 1760 | if (loop) |
| 1761 | drain_all_stock_async(memcg); |
| 1762 | total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap); |
| 1763 | /* |
| 1764 | * Allow limit shrinkers, which are triggered directly |
| 1765 | * by userspace, to catch signals and stop reclaim |
| 1766 | * after minimal progress, regardless of the margin. |
| 1767 | */ |
| 1768 | if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK)) |
| 1769 | break; |
| 1770 | if (mem_cgroup_margin(memcg)) |
| 1771 | break; |
| 1772 | /* |
| 1773 | * If nothing was reclaimed after two attempts, there |
| 1774 | * may be no reclaimable pages in this hierarchy. |
| 1775 | */ |
| 1776 | if (loop && !total) |
| 1777 | break; |
| 1778 | } |
| 1779 | return total; |
| 1780 | } |
| 1781 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1782 | /** |
| 1783 | * test_mem_cgroup_node_reclaimable |
Wanpeng Li | dad7557 | 2012-06-20 12:53:01 -0700 | [diff] [blame] | 1784 | * @memcg: the target memcg |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1785 | * @nid: the node ID to be checked. |
| 1786 | * @noswap : specify true here if the user wants flle only information. |
| 1787 | * |
| 1788 | * This function returns whether the specified memcg contains any |
| 1789 | * reclaimable pages on a node. Returns true if there are any reclaimable |
| 1790 | * pages in the node. |
| 1791 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1792 | static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1793 | int nid, bool noswap) |
| 1794 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1795 | if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1796 | return true; |
| 1797 | if (noswap || !total_swap_pages) |
| 1798 | return false; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1799 | if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1800 | return true; |
| 1801 | return false; |
| 1802 | |
| 1803 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1804 | #if MAX_NUMNODES > 1 |
| 1805 | |
| 1806 | /* |
| 1807 | * Always updating the nodemask is not very good - even if we have an empty |
| 1808 | * list or the wrong list here, we can start from some node and traverse all |
| 1809 | * nodes based on the zonelist. So update the list loosely once per 10 secs. |
| 1810 | * |
| 1811 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1812 | static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1813 | { |
| 1814 | int nid; |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1815 | /* |
| 1816 | * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET |
| 1817 | * pagein/pageout changes since the last update. |
| 1818 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1819 | if (!atomic_read(&memcg->numainfo_events)) |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1820 | return; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1821 | if (atomic_inc_return(&memcg->numainfo_updating) > 1) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1822 | return; |
| 1823 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1824 | /* make a nodemask where this memcg uses memory from */ |
Lai Jiangshan | 31aaea4 | 2012-12-12 13:51:27 -0800 | [diff] [blame] | 1825 | memcg->scan_nodes = node_states[N_MEMORY]; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1826 | |
Lai Jiangshan | 31aaea4 | 2012-12-12 13:51:27 -0800 | [diff] [blame] | 1827 | for_each_node_mask(nid, node_states[N_MEMORY]) { |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1828 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1829 | if (!test_mem_cgroup_node_reclaimable(memcg, nid, false)) |
| 1830 | node_clear(nid, memcg->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1831 | } |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1832 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1833 | atomic_set(&memcg->numainfo_events, 0); |
| 1834 | atomic_set(&memcg->numainfo_updating, 0); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | /* |
| 1838 | * Selecting a node where we start reclaim from. Because what we need is just |
| 1839 | * reducing usage counter, start from anywhere is O,K. Considering |
| 1840 | * memory reclaim from current node, there are pros. and cons. |
| 1841 | * |
| 1842 | * Freeing memory from current node means freeing memory from a node which |
| 1843 | * we'll use or we've used. So, it may make LRU bad. And if several threads |
| 1844 | * hit limits, it will see a contention on a node. But freeing from remote |
| 1845 | * node means more costs for memory reclaim because of memory latency. |
| 1846 | * |
| 1847 | * Now, we use round-robin. Better algorithm is welcomed. |
| 1848 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1849 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1850 | { |
| 1851 | int node; |
| 1852 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1853 | mem_cgroup_may_update_nodemask(memcg); |
| 1854 | node = memcg->last_scanned_node; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1855 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1856 | node = next_node(node, memcg->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1857 | if (node == MAX_NUMNODES) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1858 | node = first_node(memcg->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1859 | /* |
| 1860 | * We call this when we hit limit, not when pages are added to LRU. |
| 1861 | * No LRU may hold pages because all pages are UNEVICTABLE or |
| 1862 | * memcg is too small and all pages are not on LRU. In that case, |
| 1863 | * we use curret node. |
| 1864 | */ |
| 1865 | if (unlikely(node == MAX_NUMNODES)) |
| 1866 | node = numa_node_id(); |
| 1867 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1868 | memcg->last_scanned_node = node; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1869 | return node; |
| 1870 | } |
| 1871 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1872 | /* |
| 1873 | * Check all nodes whether it contains reclaimable pages or not. |
| 1874 | * For quick scan, we make use of scan_nodes. This will allow us to skip |
| 1875 | * unused nodes. But scan_nodes is lazily updated and may not cotain |
| 1876 | * enough new information. We need to do double check. |
| 1877 | */ |
Kirill A. Shutemov | 6bbda35 | 2012-05-29 15:06:55 -0700 | [diff] [blame] | 1878 | static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1879 | { |
| 1880 | int nid; |
| 1881 | |
| 1882 | /* |
| 1883 | * quick check...making use of scan_node. |
| 1884 | * We can skip unused nodes. |
| 1885 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1886 | if (!nodes_empty(memcg->scan_nodes)) { |
| 1887 | for (nid = first_node(memcg->scan_nodes); |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1888 | nid < MAX_NUMNODES; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1889 | nid = next_node(nid, memcg->scan_nodes)) { |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1890 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1891 | if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1892 | return true; |
| 1893 | } |
| 1894 | } |
| 1895 | /* |
| 1896 | * Check rest of nodes. |
| 1897 | */ |
Lai Jiangshan | 31aaea4 | 2012-12-12 13:51:27 -0800 | [diff] [blame] | 1898 | for_each_node_state(nid, N_MEMORY) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1899 | if (node_isset(nid, memcg->scan_nodes)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1900 | continue; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1901 | if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1902 | return true; |
| 1903 | } |
| 1904 | return false; |
| 1905 | } |
| 1906 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1907 | #else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1908 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1909 | { |
| 1910 | return 0; |
| 1911 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1912 | |
Kirill A. Shutemov | 6bbda35 | 2012-05-29 15:06:55 -0700 | [diff] [blame] | 1913 | static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1914 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1915 | return test_mem_cgroup_node_reclaimable(memcg, 0, noswap); |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1916 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1917 | #endif |
| 1918 | |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1919 | static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg, |
| 1920 | struct zone *zone, |
| 1921 | gfp_t gfp_mask, |
| 1922 | unsigned long *total_scanned) |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1923 | { |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1924 | struct mem_cgroup *victim = NULL; |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1925 | int total = 0; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1926 | int loop = 0; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1927 | unsigned long excess; |
Johannes Weiner | 185efc0 | 2011-09-14 16:21:58 -0700 | [diff] [blame] | 1928 | unsigned long nr_scanned; |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 1929 | struct mem_cgroup_reclaim_cookie reclaim = { |
| 1930 | .zone = zone, |
| 1931 | .priority = 0, |
| 1932 | }; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1933 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1934 | excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1935 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1936 | while (1) { |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 1937 | victim = mem_cgroup_iter(root_memcg, victim, &reclaim); |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1938 | if (!victim) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1939 | loop++; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1940 | if (loop >= 2) { |
| 1941 | /* |
| 1942 | * If we have not been able to reclaim |
| 1943 | * anything, it might because there are |
| 1944 | * no reclaimable pages under this hierarchy |
| 1945 | */ |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1946 | if (!total) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1947 | break; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1948 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1949 | * We want to do more targeted reclaim. |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1950 | * excess >> 2 is not to excessive so as to |
| 1951 | * reclaim too much, nor too less that we keep |
| 1952 | * coming back to reclaim from this cgroup |
| 1953 | */ |
| 1954 | if (total >= (excess >> 2) || |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1955 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1956 | break; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1957 | } |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1958 | continue; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1959 | } |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1960 | if (!mem_cgroup_reclaimable(victim, false)) |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1961 | continue; |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1962 | total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false, |
| 1963 | zone, &nr_scanned); |
| 1964 | *total_scanned += nr_scanned; |
| 1965 | if (!res_counter_soft_limit_excess(&root_memcg->res)) |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1966 | break; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1967 | } |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1968 | mem_cgroup_iter_break(root_memcg, victim); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1969 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1970 | } |
| 1971 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1972 | /* |
| 1973 | * Check OOM-Killer is already running under our hierarchy. |
| 1974 | * If someone is running, return false. |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1975 | * Has to be called with memcg_oom_lock |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1976 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1977 | static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1978 | { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1979 | struct mem_cgroup *iter, *failed = NULL; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1980 | |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1981 | for_each_mem_cgroup_tree(iter, memcg) { |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 1982 | if (iter->oom_lock) { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1983 | /* |
| 1984 | * this subtree of our hierarchy is already locked |
| 1985 | * so we cannot give a lock. |
| 1986 | */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1987 | failed = iter; |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1988 | mem_cgroup_iter_break(memcg, iter); |
| 1989 | break; |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 1990 | } else |
| 1991 | iter->oom_lock = true; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1992 | } |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1993 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1994 | if (!failed) |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 1995 | return true; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1996 | |
| 1997 | /* |
| 1998 | * OK, we failed to lock the whole subtree so we have to clean up |
| 1999 | * what we set up to the failing subtree |
| 2000 | */ |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 2001 | for_each_mem_cgroup_tree(iter, memcg) { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 2002 | if (iter == failed) { |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 2003 | mem_cgroup_iter_break(memcg, iter); |
| 2004 | break; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 2005 | } |
| 2006 | iter->oom_lock = false; |
| 2007 | } |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 2008 | return false; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2009 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 2010 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 2011 | /* |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 2012 | * Has to be called with memcg_oom_lock |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 2013 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2014 | static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 2015 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 2016 | struct mem_cgroup *iter; |
| 2017 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2018 | for_each_mem_cgroup_tree(iter, memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 2019 | iter->oom_lock = false; |
| 2020 | return 0; |
| 2021 | } |
| 2022 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2023 | static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 2024 | { |
| 2025 | struct mem_cgroup *iter; |
| 2026 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2027 | for_each_mem_cgroup_tree(iter, memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 2028 | atomic_inc(&iter->under_oom); |
| 2029 | } |
| 2030 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2031 | static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 2032 | { |
| 2033 | struct mem_cgroup *iter; |
| 2034 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2035 | /* |
| 2036 | * When a new child is created while the hierarchy is under oom, |
| 2037 | * mem_cgroup_oom_lock() may not be called. We have to use |
| 2038 | * atomic_add_unless() here. |
| 2039 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2040 | for_each_mem_cgroup_tree(iter, memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 2041 | atomic_add_unless(&iter->under_oom, -1, 0); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 2042 | } |
| 2043 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 2044 | static DEFINE_SPINLOCK(memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2045 | static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq); |
| 2046 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2047 | struct oom_wait_info { |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 2048 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2049 | wait_queue_t wait; |
| 2050 | }; |
| 2051 | |
| 2052 | static int memcg_oom_wake_function(wait_queue_t *wait, |
| 2053 | unsigned mode, int sync, void *arg) |
| 2054 | { |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 2055 | struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg; |
| 2056 | struct mem_cgroup *oom_wait_memcg; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2057 | struct oom_wait_info *oom_wait_info; |
| 2058 | |
| 2059 | oom_wait_info = container_of(wait, struct oom_wait_info, wait); |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 2060 | oom_wait_memcg = oom_wait_info->memcg; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2061 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2062 | /* |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 2063 | * Both of oom_wait_info->memcg and wake_memcg are stable under us. |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2064 | * Then we can use css_is_ancestor without taking care of RCU. |
| 2065 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2066 | if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg) |
| 2067 | && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg)) |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2068 | return 0; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2069 | return autoremove_wake_function(wait, mode, sync, arg); |
| 2070 | } |
| 2071 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2072 | static void memcg_wakeup_oom(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2073 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2074 | /* for filtering, pass "memcg" as argument. */ |
| 2075 | __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2078 | static void memcg_oom_recover(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2079 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2080 | if (memcg && atomic_read(&memcg->under_oom)) |
| 2081 | memcg_wakeup_oom(memcg); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2084 | /* |
| 2085 | * try to call OOM killer. returns false if we should exit memory-reclaim loop. |
| 2086 | */ |
Kirill A. Shutemov | 6bbda35 | 2012-05-29 15:06:55 -0700 | [diff] [blame] | 2087 | static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask, |
| 2088 | int order) |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2089 | { |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2090 | struct oom_wait_info owait; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2091 | bool locked, need_to_kill; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2092 | |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 2093 | owait.memcg = memcg; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2094 | owait.wait.flags = 0; |
| 2095 | owait.wait.func = memcg_oom_wake_function; |
| 2096 | owait.wait.private = current; |
| 2097 | INIT_LIST_HEAD(&owait.wait.task_list); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2098 | need_to_kill = true; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2099 | mem_cgroup_mark_under_oom(memcg); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 2100 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2101 | /* At first, try to OOM lock hierarchy under memcg.*/ |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 2102 | spin_lock(&memcg_oom_lock); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2103 | locked = mem_cgroup_oom_lock(memcg); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2104 | /* |
| 2105 | * Even if signal_pending(), we can't quit charge() loop without |
| 2106 | * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL |
| 2107 | * under OOM is always welcomed, use TASK_KILLABLE here. |
| 2108 | */ |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2109 | prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2110 | if (!locked || memcg->oom_kill_disable) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2111 | need_to_kill = false; |
| 2112 | if (locked) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2113 | mem_cgroup_oom_notify(memcg); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 2114 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2115 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2116 | if (need_to_kill) { |
| 2117 | finish_wait(&memcg_oom_waitq, &owait.wait); |
David Rientjes | e845e19 | 2012-03-21 16:34:10 -0700 | [diff] [blame] | 2118 | mem_cgroup_out_of_memory(memcg, mask, order); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2119 | } else { |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2120 | schedule(); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2121 | finish_wait(&memcg_oom_waitq, &owait.wait); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2122 | } |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 2123 | spin_lock(&memcg_oom_lock); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 2124 | if (locked) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2125 | mem_cgroup_oom_unlock(memcg); |
| 2126 | memcg_wakeup_oom(memcg); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 2127 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2128 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2129 | mem_cgroup_unmark_under_oom(memcg); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 2130 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2131 | if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current)) |
| 2132 | return false; |
| 2133 | /* Give chance to dying process */ |
KAMEZAWA Hiroyuki | 715a5ee | 2011-11-02 13:38:18 -0700 | [diff] [blame] | 2134 | schedule_timeout_uninterruptible(1); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2135 | return true; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 2136 | } |
| 2137 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2138 | /* |
| 2139 | * Currently used to update mapped file statistics, but the routine can be |
| 2140 | * generalized to update other statistics as well. |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2141 | * |
| 2142 | * Notes: Race condition |
| 2143 | * |
| 2144 | * We usually use page_cgroup_lock() for accessing page_cgroup member but |
| 2145 | * it tends to be costly. But considering some conditions, we doesn't need |
| 2146 | * to do so _always_. |
| 2147 | * |
| 2148 | * Considering "charge", lock_page_cgroup() is not required because all |
| 2149 | * file-stat operations happen after a page is attached to radix-tree. There |
| 2150 | * are no race with "charge". |
| 2151 | * |
| 2152 | * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup |
| 2153 | * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even |
| 2154 | * if there are race with "uncharge". Statistics itself is properly handled |
| 2155 | * by flags. |
| 2156 | * |
| 2157 | * Considering "move", this is an only case we see a race. To make the race |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 2158 | * small, we check mm->moving_account and detect there are possibility of race |
| 2159 | * If there is, we take a lock. |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2160 | */ |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2161 | |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2162 | void __mem_cgroup_begin_update_page_stat(struct page *page, |
| 2163 | bool *locked, unsigned long *flags) |
| 2164 | { |
| 2165 | struct mem_cgroup *memcg; |
| 2166 | struct page_cgroup *pc; |
| 2167 | |
| 2168 | pc = lookup_page_cgroup(page); |
| 2169 | again: |
| 2170 | memcg = pc->mem_cgroup; |
| 2171 | if (unlikely(!memcg || !PageCgroupUsed(pc))) |
| 2172 | return; |
| 2173 | /* |
| 2174 | * If this memory cgroup is not under account moving, we don't |
Wanpeng Li | da92c47 | 2012-07-31 16:43:26 -0700 | [diff] [blame] | 2175 | * need to take move_lock_mem_cgroup(). Because we already hold |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2176 | * rcu_read_lock(), any calls to move_account will be delayed until |
Andrew Morton | 13fd1dd9 | 2012-03-21 16:34:26 -0700 | [diff] [blame] | 2177 | * rcu_read_unlock() if mem_cgroup_stolen() == true. |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2178 | */ |
Andrew Morton | 13fd1dd9 | 2012-03-21 16:34:26 -0700 | [diff] [blame] | 2179 | if (!mem_cgroup_stolen(memcg)) |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2180 | return; |
| 2181 | |
| 2182 | move_lock_mem_cgroup(memcg, flags); |
| 2183 | if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) { |
| 2184 | move_unlock_mem_cgroup(memcg, flags); |
| 2185 | goto again; |
| 2186 | } |
| 2187 | *locked = true; |
| 2188 | } |
| 2189 | |
| 2190 | void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags) |
| 2191 | { |
| 2192 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 2193 | |
| 2194 | /* |
| 2195 | * It's guaranteed that pc->mem_cgroup never changes while |
| 2196 | * lock is held because a routine modifies pc->mem_cgroup |
Wanpeng Li | da92c47 | 2012-07-31 16:43:26 -0700 | [diff] [blame] | 2197 | * should take move_lock_mem_cgroup(). |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2198 | */ |
| 2199 | move_unlock_mem_cgroup(pc->mem_cgroup, flags); |
| 2200 | } |
| 2201 | |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2202 | void mem_cgroup_update_page_stat(struct page *page, |
| 2203 | enum mem_cgroup_page_stat_item idx, int val) |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2204 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2205 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2206 | struct page_cgroup *pc = lookup_page_cgroup(page); |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 2207 | unsigned long uninitialized_var(flags); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2208 | |
Johannes Weiner | cfa4494 | 2012-01-12 17:18:38 -0800 | [diff] [blame] | 2209 | if (mem_cgroup_disabled()) |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2210 | return; |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2211 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2212 | memcg = pc->mem_cgroup; |
| 2213 | if (unlikely(!memcg || !PageCgroupUsed(pc))) |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2214 | return; |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2215 | |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2216 | switch (idx) { |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2217 | case MEMCG_NR_FILE_MAPPED: |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 2218 | idx = MEM_CGROUP_STAT_FILE_MAPPED; |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2219 | break; |
| 2220 | default: |
| 2221 | BUG(); |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 2222 | } |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2223 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2224 | this_cpu_add(memcg->stat->count[idx], val); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2225 | } |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2226 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2227 | /* |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2228 | * size of first charge trial. "32" comes from vmscan.c's magic value. |
| 2229 | * TODO: maybe necessary to use big numbers in big irons. |
| 2230 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2231 | #define CHARGE_BATCH 32U |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2232 | struct memcg_stock_pcp { |
| 2233 | struct mem_cgroup *cached; /* this never be root cgroup */ |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2234 | unsigned int nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2235 | struct work_struct work; |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2236 | unsigned long flags; |
Kirill A. Shutemov | a0db00f | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 2237 | #define FLUSHING_CACHED_CHARGE 0 |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2238 | }; |
| 2239 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2240 | static DEFINE_MUTEX(percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2241 | |
Suleiman Souhlal | a0956d5 | 2012-12-18 14:21:36 -0800 | [diff] [blame] | 2242 | /** |
| 2243 | * consume_stock: Try to consume stocked charge on this cpu. |
| 2244 | * @memcg: memcg to consume from. |
| 2245 | * @nr_pages: how many pages to charge. |
| 2246 | * |
| 2247 | * The charges will only happen if @memcg matches the current cpu's memcg |
| 2248 | * stock, and at least @nr_pages are available in that stock. Failure to |
| 2249 | * service an allocation will refill the stock. |
| 2250 | * |
| 2251 | * returns true if successful, false otherwise. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2252 | */ |
Suleiman Souhlal | a0956d5 | 2012-12-18 14:21:36 -0800 | [diff] [blame] | 2253 | static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2254 | { |
| 2255 | struct memcg_stock_pcp *stock; |
| 2256 | bool ret = true; |
| 2257 | |
Suleiman Souhlal | a0956d5 | 2012-12-18 14:21:36 -0800 | [diff] [blame] | 2258 | if (nr_pages > CHARGE_BATCH) |
| 2259 | return false; |
| 2260 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2261 | stock = &get_cpu_var(memcg_stock); |
Suleiman Souhlal | a0956d5 | 2012-12-18 14:21:36 -0800 | [diff] [blame] | 2262 | if (memcg == stock->cached && stock->nr_pages >= nr_pages) |
| 2263 | stock->nr_pages -= nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2264 | else /* need to call res_counter_charge */ |
| 2265 | ret = false; |
| 2266 | put_cpu_var(memcg_stock); |
| 2267 | return ret; |
| 2268 | } |
| 2269 | |
| 2270 | /* |
| 2271 | * Returns stocks cached in percpu to res_counter and reset cached information. |
| 2272 | */ |
| 2273 | static void drain_stock(struct memcg_stock_pcp *stock) |
| 2274 | { |
| 2275 | struct mem_cgroup *old = stock->cached; |
| 2276 | |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2277 | if (stock->nr_pages) { |
| 2278 | unsigned long bytes = stock->nr_pages * PAGE_SIZE; |
| 2279 | |
| 2280 | res_counter_uncharge(&old->res, bytes); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2281 | if (do_swap_account) |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2282 | res_counter_uncharge(&old->memsw, bytes); |
| 2283 | stock->nr_pages = 0; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2284 | } |
| 2285 | stock->cached = NULL; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | /* |
| 2289 | * This must be called under preempt disabled or must be called by |
| 2290 | * a thread which is pinned to local cpu. |
| 2291 | */ |
| 2292 | static void drain_local_stock(struct work_struct *dummy) |
| 2293 | { |
| 2294 | struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock); |
| 2295 | drain_stock(stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2296 | clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2297 | } |
| 2298 | |
Michal Hocko | e477749 | 2013-02-22 16:35:40 -0800 | [diff] [blame] | 2299 | static void __init memcg_stock_init(void) |
| 2300 | { |
| 2301 | int cpu; |
| 2302 | |
| 2303 | for_each_possible_cpu(cpu) { |
| 2304 | struct memcg_stock_pcp *stock = |
| 2305 | &per_cpu(memcg_stock, cpu); |
| 2306 | INIT_WORK(&stock->work, drain_local_stock); |
| 2307 | } |
| 2308 | } |
| 2309 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2310 | /* |
| 2311 | * 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] | 2312 | * This will be consumed by consume_stock() function, later. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2313 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2314 | static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2315 | { |
| 2316 | struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock); |
| 2317 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2318 | if (stock->cached != memcg) { /* reset if necessary */ |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2319 | drain_stock(stock); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2320 | stock->cached = memcg; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2321 | } |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2322 | stock->nr_pages += nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2323 | put_cpu_var(memcg_stock); |
| 2324 | } |
| 2325 | |
| 2326 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2327 | * Drains all per-CPU charge caches for given root_memcg resp. subtree |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2328 | * of the hierarchy under it. sync flag says whether we should block |
| 2329 | * until the work is done. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2330 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2331 | static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2332 | { |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2333 | int cpu, curcpu; |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2334 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2335 | /* Notify other cpus that system-wide "drain" is running */ |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2336 | get_online_cpus(); |
Johannes Weiner | 5af12d0 | 2011-08-25 15:59:07 -0700 | [diff] [blame] | 2337 | curcpu = get_cpu(); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2338 | for_each_online_cpu(cpu) { |
| 2339 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2340 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2341 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2342 | memcg = stock->cached; |
| 2343 | if (!memcg || !stock->nr_pages) |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2344 | continue; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2345 | if (!mem_cgroup_same_or_subtree(root_memcg, memcg)) |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 2346 | continue; |
Michal Hocko | d1a05b6 | 2011-07-26 16:08:27 -0700 | [diff] [blame] | 2347 | if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) { |
| 2348 | if (cpu == curcpu) |
| 2349 | drain_local_stock(&stock->work); |
| 2350 | else |
| 2351 | schedule_work_on(cpu, &stock->work); |
| 2352 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2353 | } |
Johannes Weiner | 5af12d0 | 2011-08-25 15:59:07 -0700 | [diff] [blame] | 2354 | put_cpu(); |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2355 | |
| 2356 | if (!sync) |
| 2357 | goto out; |
| 2358 | |
| 2359 | for_each_online_cpu(cpu) { |
| 2360 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2361 | if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2362 | flush_work(&stock->work); |
| 2363 | } |
| 2364 | out: |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2365 | put_online_cpus(); |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2366 | } |
| 2367 | |
| 2368 | /* |
| 2369 | * Tries to drain stocked charges in other cpus. This function is asynchronous |
| 2370 | * and just put a work per cpu for draining localy on each cpu. Caller can |
| 2371 | * expects some charges will be back to res_counter later but cannot wait for |
| 2372 | * it. |
| 2373 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2374 | static void drain_all_stock_async(struct mem_cgroup *root_memcg) |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2375 | { |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2376 | /* |
| 2377 | * If someone calls draining, avoid adding more kworker runs. |
| 2378 | */ |
| 2379 | if (!mutex_trylock(&percpu_charge_mutex)) |
| 2380 | return; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2381 | drain_all_stock(root_memcg, false); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2382 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | /* This is a synchronous drain interface. */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2386 | static void drain_all_stock_sync(struct mem_cgroup *root_memcg) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2387 | { |
| 2388 | /* called when force_empty is called */ |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2389 | mutex_lock(&percpu_charge_mutex); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2390 | drain_all_stock(root_memcg, true); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2391 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2392 | } |
| 2393 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2394 | /* |
| 2395 | * This function drains percpu counter value from DEAD cpu and |
| 2396 | * move it to local cpu. Note that this function can be preempted. |
| 2397 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2398 | static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu) |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2399 | { |
| 2400 | int i; |
| 2401 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2402 | spin_lock(&memcg->pcp_counter_lock); |
Johannes Weiner | 6104621 | 2012-05-29 15:07:05 -0700 | [diff] [blame] | 2403 | for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2404 | long x = per_cpu(memcg->stat->count[i], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2405 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2406 | per_cpu(memcg->stat->count[i], cpu) = 0; |
| 2407 | memcg->nocpu_base.count[i] += x; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2408 | } |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2409 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2410 | unsigned long x = per_cpu(memcg->stat->events[i], cpu); |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2411 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2412 | per_cpu(memcg->stat->events[i], cpu) = 0; |
| 2413 | memcg->nocpu_base.events[i] += x; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2414 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2415 | spin_unlock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb, |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2419 | unsigned long action, |
| 2420 | void *hcpu) |
| 2421 | { |
| 2422 | int cpu = (unsigned long)hcpu; |
| 2423 | struct memcg_stock_pcp *stock; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2424 | struct mem_cgroup *iter; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2425 | |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 2426 | if (action == CPU_ONLINE) |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2427 | return NOTIFY_OK; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2428 | |
Kirill A. Shutemov | d833049 | 2012-04-12 12:49:11 -0700 | [diff] [blame] | 2429 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2430 | return NOTIFY_OK; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2431 | |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 2432 | for_each_mem_cgroup(iter) |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2433 | mem_cgroup_drain_pcp_counter(iter, cpu); |
| 2434 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2435 | stock = &per_cpu(memcg_stock, cpu); |
| 2436 | drain_stock(stock); |
| 2437 | return NOTIFY_OK; |
| 2438 | } |
| 2439 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2440 | |
| 2441 | /* See __mem_cgroup_try_charge() for details */ |
| 2442 | enum { |
| 2443 | CHARGE_OK, /* success */ |
| 2444 | CHARGE_RETRY, /* need to retry but retry is not bad */ |
| 2445 | CHARGE_NOMEM, /* we can't do more. return -ENOMEM */ |
| 2446 | CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */ |
| 2447 | CHARGE_OOM_DIE, /* the current is killed because of OOM */ |
| 2448 | }; |
| 2449 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2450 | static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, |
Suleiman Souhlal | 4c9c535 | 2012-12-18 14:21:41 -0800 | [diff] [blame] | 2451 | unsigned int nr_pages, unsigned int min_pages, |
| 2452 | bool oom_check) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2453 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2454 | unsigned long csize = nr_pages * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2455 | struct mem_cgroup *mem_over_limit; |
| 2456 | struct res_counter *fail_res; |
| 2457 | unsigned long flags = 0; |
| 2458 | int ret; |
| 2459 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2460 | ret = res_counter_charge(&memcg->res, csize, &fail_res); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2461 | |
| 2462 | if (likely(!ret)) { |
| 2463 | if (!do_swap_account) |
| 2464 | return CHARGE_OK; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2465 | ret = res_counter_charge(&memcg->memsw, csize, &fail_res); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2466 | if (likely(!ret)) |
| 2467 | return CHARGE_OK; |
| 2468 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2469 | res_counter_uncharge(&memcg->res, csize); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2470 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw); |
| 2471 | flags |= MEM_CGROUP_RECLAIM_NOSWAP; |
| 2472 | } else |
| 2473 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, res); |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2474 | /* |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2475 | * Never reclaim on behalf of optional batching, retry with a |
| 2476 | * single page instead. |
| 2477 | */ |
Suleiman Souhlal | 4c9c535 | 2012-12-18 14:21:41 -0800 | [diff] [blame] | 2478 | if (nr_pages > min_pages) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2479 | return CHARGE_RETRY; |
| 2480 | |
| 2481 | if (!(gfp_mask & __GFP_WAIT)) |
| 2482 | return CHARGE_WOULDBLOCK; |
| 2483 | |
Suleiman Souhlal | 4c9c535 | 2012-12-18 14:21:41 -0800 | [diff] [blame] | 2484 | if (gfp_mask & __GFP_NORETRY) |
| 2485 | return CHARGE_NOMEM; |
| 2486 | |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 2487 | ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2488 | if (mem_cgroup_margin(mem_over_limit) >= nr_pages) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2489 | return CHARGE_RETRY; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2490 | /* |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2491 | * Even though the limit is exceeded at this point, reclaim |
| 2492 | * may have been able to free some pages. Retry the charge |
| 2493 | * before killing the task. |
| 2494 | * |
| 2495 | * Only for regular pages, though: huge pages are rather |
| 2496 | * unlikely to succeed so close to the limit, and we fall back |
| 2497 | * to regular pages anyway in case of failure. |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2498 | */ |
Suleiman Souhlal | 4c9c535 | 2012-12-18 14:21:41 -0800 | [diff] [blame] | 2499 | if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2500 | return CHARGE_RETRY; |
| 2501 | |
| 2502 | /* |
| 2503 | * At task move, charge accounts can be doubly counted. So, it's |
| 2504 | * better to wait until the end of task_move if something is going on. |
| 2505 | */ |
| 2506 | if (mem_cgroup_wait_acct_move(mem_over_limit)) |
| 2507 | return CHARGE_RETRY; |
| 2508 | |
| 2509 | /* If we don't need to call oom-killer at el, return immediately */ |
| 2510 | if (!oom_check) |
| 2511 | return CHARGE_NOMEM; |
| 2512 | /* check OOM */ |
David Rientjes | e845e19 | 2012-03-21 16:34:10 -0700 | [diff] [blame] | 2513 | if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize))) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2514 | return CHARGE_OOM_DIE; |
| 2515 | |
| 2516 | return CHARGE_RETRY; |
| 2517 | } |
| 2518 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2519 | /* |
KAMEZAWA Hiroyuki | 38c5d72 | 2012-01-12 17:19:01 -0800 | [diff] [blame] | 2520 | * __mem_cgroup_try_charge() does |
| 2521 | * 1. detect memcg to be charged against from passed *mm and *ptr, |
| 2522 | * 2. update res_counter |
| 2523 | * 3. call memory reclaim if necessary. |
| 2524 | * |
| 2525 | * In some special case, if the task is fatal, fatal_signal_pending() or |
| 2526 | * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup |
| 2527 | * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon |
| 2528 | * as possible without any hazards. 2: all pages should have a valid |
| 2529 | * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg |
| 2530 | * pointer, that is treated as a charge to root_mem_cgroup. |
| 2531 | * |
| 2532 | * So __mem_cgroup_try_charge() will return |
| 2533 | * 0 ... on success, filling *ptr with a valid memcg pointer. |
| 2534 | * -ENOMEM ... charge failure because of resource limits. |
| 2535 | * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup. |
| 2536 | * |
| 2537 | * Unlike the exported interface, an "oom" parameter is added. if oom==true, |
| 2538 | * the oom-killer can be invoked. |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2539 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2540 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2541 | gfp_t gfp_mask, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2542 | unsigned int nr_pages, |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2543 | struct mem_cgroup **ptr, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2544 | bool oom) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2545 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2546 | unsigned int batch = max(CHARGE_BATCH, nr_pages); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2547 | int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2548 | struct mem_cgroup *memcg = NULL; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2549 | int ret; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2550 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2551 | /* |
| 2552 | * Unlike gloval-vm's OOM-kill, we're not in memory shortage |
| 2553 | * in system level. So, allow to go ahead dying process in addition to |
| 2554 | * MEMDIE process. |
| 2555 | */ |
| 2556 | if (unlikely(test_thread_flag(TIF_MEMDIE) |
| 2557 | || fatal_signal_pending(current))) |
| 2558 | goto bypass; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2559 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2560 | /* |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2561 | * We always charge the cgroup the mm_struct belongs to. |
| 2562 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2563 | * thread group leader migrates. It's possible that mm is not |
Johannes Weiner | 24467ca | 2012-07-31 16:45:40 -0700 | [diff] [blame] | 2564 | * set, if so charge the root memcg (happens for pagecache usage). |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2565 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2566 | if (!*ptr && !mm) |
KAMEZAWA Hiroyuki | 38c5d72 | 2012-01-12 17:19:01 -0800 | [diff] [blame] | 2567 | *ptr = root_mem_cgroup; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2568 | again: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2569 | if (*ptr) { /* css should be a valid one */ |
| 2570 | memcg = *ptr; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2571 | if (mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2572 | goto done; |
Suleiman Souhlal | a0956d5 | 2012-12-18 14:21:36 -0800 | [diff] [blame] | 2573 | if (consume_stock(memcg, nr_pages)) |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2574 | goto done; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2575 | css_get(&memcg->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2576 | } else { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2577 | struct task_struct *p; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2578 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2579 | rcu_read_lock(); |
| 2580 | p = rcu_dereference(mm->owner); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2581 | /* |
KAMEZAWA Hiroyuki | ebb76ce | 2010-12-29 14:07:11 -0800 | [diff] [blame] | 2582 | * Because we don't have task_lock(), "p" can exit. |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2583 | * In that case, "memcg" can point to root or p can be NULL with |
KAMEZAWA Hiroyuki | ebb76ce | 2010-12-29 14:07:11 -0800 | [diff] [blame] | 2584 | * race with swapoff. Then, we have small risk of mis-accouning. |
| 2585 | * But such kind of mis-account by race always happens because |
| 2586 | * we don't have cgroup_mutex(). It's overkill and we allo that |
| 2587 | * small race, here. |
| 2588 | * (*) swapoff at el will charge against mm-struct not against |
| 2589 | * task-struct. So, mm->owner can be NULL. |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2590 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2591 | memcg = mem_cgroup_from_task(p); |
KAMEZAWA Hiroyuki | 38c5d72 | 2012-01-12 17:19:01 -0800 | [diff] [blame] | 2592 | if (!memcg) |
| 2593 | memcg = root_mem_cgroup; |
| 2594 | if (mem_cgroup_is_root(memcg)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2595 | rcu_read_unlock(); |
| 2596 | goto done; |
| 2597 | } |
Suleiman Souhlal | a0956d5 | 2012-12-18 14:21:36 -0800 | [diff] [blame] | 2598 | if (consume_stock(memcg, nr_pages)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2599 | /* |
| 2600 | * It seems dagerous to access memcg without css_get(). |
| 2601 | * But considering how consume_stok works, it's not |
| 2602 | * necessary. If consume_stock success, some charges |
| 2603 | * from this memcg are cached on this cpu. So, we |
| 2604 | * don't need to call css_get()/css_tryget() before |
| 2605 | * calling consume_stock(). |
| 2606 | */ |
| 2607 | rcu_read_unlock(); |
| 2608 | goto done; |
| 2609 | } |
| 2610 | /* after here, we may be blocked. we need to get refcnt */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2611 | if (!css_tryget(&memcg->css)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2612 | rcu_read_unlock(); |
| 2613 | goto again; |
| 2614 | } |
| 2615 | rcu_read_unlock(); |
| 2616 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2617 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2618 | do { |
| 2619 | bool oom_check; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2620 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2621 | /* If killed, bypass charge */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2622 | if (fatal_signal_pending(current)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2623 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2624 | goto bypass; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2625 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2626 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2627 | oom_check = false; |
| 2628 | if (oom && !nr_oom_retries) { |
| 2629 | oom_check = true; |
| 2630 | nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
| 2631 | } |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 2632 | |
Suleiman Souhlal | 4c9c535 | 2012-12-18 14:21:41 -0800 | [diff] [blame] | 2633 | ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages, |
| 2634 | oom_check); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2635 | switch (ret) { |
| 2636 | case CHARGE_OK: |
| 2637 | break; |
| 2638 | case CHARGE_RETRY: /* not in OOM situation but retry */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2639 | batch = nr_pages; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2640 | css_put(&memcg->css); |
| 2641 | memcg = NULL; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2642 | goto again; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2643 | case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2644 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2645 | goto nomem; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2646 | case CHARGE_NOMEM: /* OOM routine works */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2647 | if (!oom) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2648 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2649 | goto nomem; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2650 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2651 | /* If oom, we never return -ENOMEM */ |
| 2652 | nr_oom_retries--; |
| 2653 | break; |
| 2654 | case CHARGE_OOM_DIE: /* Killed by OOM Killer */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2655 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2656 | goto bypass; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2657 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2658 | } while (ret != CHARGE_OK); |
| 2659 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2660 | if (batch > nr_pages) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2661 | refill_stock(memcg, batch - nr_pages); |
| 2662 | css_put(&memcg->css); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2663 | done: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2664 | *ptr = memcg; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2665 | return 0; |
| 2666 | nomem: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2667 | *ptr = NULL; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2668 | return -ENOMEM; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2669 | bypass: |
KAMEZAWA Hiroyuki | 38c5d72 | 2012-01-12 17:19:01 -0800 | [diff] [blame] | 2670 | *ptr = root_mem_cgroup; |
| 2671 | return -EINTR; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2672 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2673 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2674 | /* |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2675 | * Somemtimes we have to undo a charge we got by try_charge(). |
| 2676 | * This function is for that and do uncharge, put css's refcnt. |
| 2677 | * gotten by try_charge(). |
| 2678 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2679 | static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg, |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2680 | unsigned int nr_pages) |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2681 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2682 | if (!mem_cgroup_is_root(memcg)) { |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2683 | unsigned long bytes = nr_pages * PAGE_SIZE; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2684 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2685 | res_counter_uncharge(&memcg->res, bytes); |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2686 | if (do_swap_account) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2687 | res_counter_uncharge(&memcg->memsw, bytes); |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2688 | } |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2689 | } |
| 2690 | |
| 2691 | /* |
KAMEZAWA Hiroyuki | d01dd17 | 2012-05-29 15:07:03 -0700 | [diff] [blame] | 2692 | * Cancel chrages in this cgroup....doesn't propagate to parent cgroup. |
| 2693 | * This is useful when moving usage to parent cgroup. |
| 2694 | */ |
| 2695 | static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg, |
| 2696 | unsigned int nr_pages) |
| 2697 | { |
| 2698 | unsigned long bytes = nr_pages * PAGE_SIZE; |
| 2699 | |
| 2700 | if (mem_cgroup_is_root(memcg)) |
| 2701 | return; |
| 2702 | |
| 2703 | res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes); |
| 2704 | if (do_swap_account) |
| 2705 | res_counter_uncharge_until(&memcg->memsw, |
| 2706 | memcg->memsw.parent, bytes); |
| 2707 | } |
| 2708 | |
| 2709 | /* |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2710 | * A helper function to get mem_cgroup from ID. must be called under |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 2711 | * rcu_read_lock(). The caller is responsible for calling css_tryget if |
| 2712 | * the mem_cgroup is used for charging. (dropping refcnt from swap can be |
| 2713 | * called against removed memcg.) |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2714 | */ |
| 2715 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) |
| 2716 | { |
| 2717 | struct cgroup_subsys_state *css; |
| 2718 | |
| 2719 | /* ID 0 is unused ID */ |
| 2720 | if (!id) |
| 2721 | return NULL; |
| 2722 | css = css_lookup(&mem_cgroup_subsys, id); |
| 2723 | if (!css) |
| 2724 | return NULL; |
Wanpeng Li | b214514 | 2012-07-31 16:46:01 -0700 | [diff] [blame] | 2725 | return mem_cgroup_from_css(css); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2726 | } |
| 2727 | |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2728 | struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2729 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2730 | struct mem_cgroup *memcg = NULL; |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2731 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2732 | unsigned short id; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2733 | swp_entry_t ent; |
| 2734 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2735 | VM_BUG_ON(!PageLocked(page)); |
| 2736 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2737 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2738 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2739 | if (PageCgroupUsed(pc)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2740 | memcg = pc->mem_cgroup; |
| 2741 | if (memcg && !css_tryget(&memcg->css)) |
| 2742 | memcg = NULL; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2743 | } else if (PageSwapCache(page)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2744 | ent.val = page_private(page); |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 2745 | id = lookup_swap_cgroup_id(ent); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2746 | rcu_read_lock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2747 | memcg = mem_cgroup_lookup(id); |
| 2748 | if (memcg && !css_tryget(&memcg->css)) |
| 2749 | memcg = NULL; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2750 | rcu_read_unlock(); |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2751 | } |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2752 | unlock_page_cgroup(pc); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2753 | return memcg; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2754 | } |
| 2755 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2756 | static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg, |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2757 | struct page *page, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2758 | unsigned int nr_pages, |
Hugh Dickins | 9ce70c0 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 2759 | enum charge_type ctype, |
| 2760 | bool lrucare) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2761 | { |
Johannes Weiner | ce587e6 | 2012-04-24 20:22:33 +0200 | [diff] [blame] | 2762 | struct page_cgroup *pc = lookup_page_cgroup(page); |
Hugh Dickins | 9ce70c0 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 2763 | struct zone *uninitialized_var(zone); |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 2764 | struct lruvec *lruvec; |
Hugh Dickins | 9ce70c0 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 2765 | bool was_on_lru = false; |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 2766 | bool anon; |
Hugh Dickins | 9ce70c0 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 2767 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2768 | lock_page_cgroup(pc); |
Johannes Weiner | 90deb78 | 2012-07-31 16:45:47 -0700 | [diff] [blame] | 2769 | VM_BUG_ON(PageCgroupUsed(pc)); |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2770 | /* |
| 2771 | * we don't need page_cgroup_lock about tail pages, becase they are not |
| 2772 | * accessed by any other context at this point. |
| 2773 | */ |
Hugh Dickins | 9ce70c0 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 2774 | |
| 2775 | /* |
| 2776 | * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page |
| 2777 | * may already be on some other mem_cgroup's LRU. Take care of it. |
| 2778 | */ |
| 2779 | if (lrucare) { |
| 2780 | zone = page_zone(page); |
| 2781 | spin_lock_irq(&zone->lru_lock); |
| 2782 | if (PageLRU(page)) { |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 2783 | lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup); |
Hugh Dickins | 9ce70c0 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 2784 | ClearPageLRU(page); |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 2785 | del_page_from_lru_list(page, lruvec, page_lru(page)); |
Hugh Dickins | 9ce70c0 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 2786 | was_on_lru = true; |
| 2787 | } |
| 2788 | } |
| 2789 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2790 | pc->mem_cgroup = memcg; |
KAMEZAWA Hiroyuki | 261fb61 | 2009-09-23 15:56:33 -0700 | [diff] [blame] | 2791 | /* |
| 2792 | * We access a page_cgroup asynchronously without lock_page_cgroup(). |
| 2793 | * Especially when a page_cgroup is taken from a page, pc->mem_cgroup |
| 2794 | * is accessed after testing USED bit. To make pc->mem_cgroup visible |
| 2795 | * before USED bit, we need memory barrier here. |
| 2796 | * See mem_cgroup_add_lru_list(), etc. |
| 2797 | */ |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2798 | smp_wmb(); |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 2799 | SetPageCgroupUsed(pc); |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2800 | |
Hugh Dickins | 9ce70c0 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 2801 | if (lrucare) { |
| 2802 | if (was_on_lru) { |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 2803 | lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup); |
Hugh Dickins | 9ce70c0 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 2804 | VM_BUG_ON(PageLRU(page)); |
| 2805 | SetPageLRU(page); |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 2806 | add_page_to_lru_list(page, lruvec, page_lru(page)); |
Hugh Dickins | 9ce70c0 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 2807 | } |
| 2808 | spin_unlock_irq(&zone->lru_lock); |
| 2809 | } |
| 2810 | |
Kamezawa Hiroyuki | 41326c1 | 2012-07-31 16:41:40 -0700 | [diff] [blame] | 2811 | if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON) |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 2812 | anon = true; |
| 2813 | else |
| 2814 | anon = false; |
| 2815 | |
| 2816 | mem_cgroup_charge_statistics(memcg, anon, nr_pages); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2817 | unlock_page_cgroup(pc); |
Hugh Dickins | 9ce70c0 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 2818 | |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 2819 | /* |
| 2820 | * "charge_statistics" updated event counter. Then, check it. |
| 2821 | * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree. |
| 2822 | * if they exceeds softlimit. |
| 2823 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2824 | memcg_check_events(memcg, page); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2825 | } |
| 2826 | |
Glauber Costa | 7cf2798 | 2012-12-18 14:22:55 -0800 | [diff] [blame] | 2827 | static DEFINE_MUTEX(set_limit_mutex); |
| 2828 | |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2829 | #ifdef CONFIG_MEMCG_KMEM |
| 2830 | static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg) |
| 2831 | { |
| 2832 | return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) && |
| 2833 | (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK); |
| 2834 | } |
| 2835 | |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 2836 | /* |
| 2837 | * This is a bit cumbersome, but it is rarely used and avoids a backpointer |
| 2838 | * in the memcg_cache_params struct. |
| 2839 | */ |
| 2840 | static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p) |
| 2841 | { |
| 2842 | struct kmem_cache *cachep; |
| 2843 | |
| 2844 | VM_BUG_ON(p->is_root_cache); |
| 2845 | cachep = p->root_cache; |
| 2846 | return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)]; |
| 2847 | } |
| 2848 | |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 2849 | #ifdef CONFIG_SLABINFO |
| 2850 | static int mem_cgroup_slabinfo_read(struct cgroup *cont, struct cftype *cft, |
| 2851 | struct seq_file *m) |
| 2852 | { |
| 2853 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
| 2854 | struct memcg_cache_params *params; |
| 2855 | |
| 2856 | if (!memcg_can_account_kmem(memcg)) |
| 2857 | return -EIO; |
| 2858 | |
| 2859 | print_slabinfo_header(m); |
| 2860 | |
| 2861 | mutex_lock(&memcg->slab_caches_mutex); |
| 2862 | list_for_each_entry(params, &memcg->memcg_slab_caches, list) |
| 2863 | cache_show(memcg_params_to_cache(params), m); |
| 2864 | mutex_unlock(&memcg->slab_caches_mutex); |
| 2865 | |
| 2866 | return 0; |
| 2867 | } |
| 2868 | #endif |
| 2869 | |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2870 | static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size) |
| 2871 | { |
| 2872 | struct res_counter *fail_res; |
| 2873 | struct mem_cgroup *_memcg; |
| 2874 | int ret = 0; |
| 2875 | bool may_oom; |
| 2876 | |
| 2877 | ret = res_counter_charge(&memcg->kmem, size, &fail_res); |
| 2878 | if (ret) |
| 2879 | return ret; |
| 2880 | |
| 2881 | /* |
| 2882 | * Conditions under which we can wait for the oom_killer. Those are |
| 2883 | * the same conditions tested by the core page allocator |
| 2884 | */ |
| 2885 | may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY); |
| 2886 | |
| 2887 | _memcg = memcg; |
| 2888 | ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT, |
| 2889 | &_memcg, may_oom); |
| 2890 | |
| 2891 | if (ret == -EINTR) { |
| 2892 | /* |
| 2893 | * __mem_cgroup_try_charge() chosed to bypass to root due to |
| 2894 | * OOM kill or fatal signal. Since our only options are to |
| 2895 | * either fail the allocation or charge it to this cgroup, do |
| 2896 | * it as a temporary condition. But we can't fail. From a |
| 2897 | * kmem/slab perspective, the cache has already been selected, |
| 2898 | * by mem_cgroup_kmem_get_cache(), so it is too late to change |
| 2899 | * our minds. |
| 2900 | * |
| 2901 | * This condition will only trigger if the task entered |
| 2902 | * memcg_charge_kmem in a sane state, but was OOM-killed during |
| 2903 | * __mem_cgroup_try_charge() above. Tasks that were already |
| 2904 | * dying when the allocation triggers should have been already |
| 2905 | * directed to the root cgroup in memcontrol.h |
| 2906 | */ |
| 2907 | res_counter_charge_nofail(&memcg->res, size, &fail_res); |
| 2908 | if (do_swap_account) |
| 2909 | res_counter_charge_nofail(&memcg->memsw, size, |
| 2910 | &fail_res); |
| 2911 | ret = 0; |
| 2912 | } else if (ret) |
| 2913 | res_counter_uncharge(&memcg->kmem, size); |
| 2914 | |
| 2915 | return ret; |
| 2916 | } |
| 2917 | |
| 2918 | static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size) |
| 2919 | { |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2920 | res_counter_uncharge(&memcg->res, size); |
| 2921 | if (do_swap_account) |
| 2922 | res_counter_uncharge(&memcg->memsw, size); |
Glauber Costa | 7de3768 | 2012-12-18 14:22:07 -0800 | [diff] [blame] | 2923 | |
| 2924 | /* Not down to 0 */ |
| 2925 | if (res_counter_uncharge(&memcg->kmem, size)) |
| 2926 | return; |
| 2927 | |
| 2928 | if (memcg_kmem_test_and_clear_dead(memcg)) |
| 2929 | mem_cgroup_put(memcg); |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2930 | } |
| 2931 | |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 2932 | void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep) |
| 2933 | { |
| 2934 | if (!memcg) |
| 2935 | return; |
| 2936 | |
| 2937 | mutex_lock(&memcg->slab_caches_mutex); |
| 2938 | list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches); |
| 2939 | mutex_unlock(&memcg->slab_caches_mutex); |
| 2940 | } |
| 2941 | |
| 2942 | /* |
| 2943 | * helper for acessing a memcg's index. It will be used as an index in the |
| 2944 | * child cache array in kmem_cache, and also to derive its name. This function |
| 2945 | * will return -1 when this is not a kmem-limited memcg. |
| 2946 | */ |
| 2947 | int memcg_cache_id(struct mem_cgroup *memcg) |
| 2948 | { |
| 2949 | return memcg ? memcg->kmemcg_id : -1; |
| 2950 | } |
| 2951 | |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 2952 | /* |
| 2953 | * This ends up being protected by the set_limit mutex, during normal |
| 2954 | * operation, because that is its main call site. |
| 2955 | * |
| 2956 | * But when we create a new cache, we can call this as well if its parent |
| 2957 | * is kmem-limited. That will have to hold set_limit_mutex as well. |
| 2958 | */ |
| 2959 | int memcg_update_cache_sizes(struct mem_cgroup *memcg) |
| 2960 | { |
| 2961 | int num, ret; |
| 2962 | |
| 2963 | num = ida_simple_get(&kmem_limited_groups, |
| 2964 | 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL); |
| 2965 | if (num < 0) |
| 2966 | return num; |
| 2967 | /* |
| 2968 | * After this point, kmem_accounted (that we test atomically in |
| 2969 | * the beginning of this conditional), is no longer 0. This |
| 2970 | * guarantees only one process will set the following boolean |
| 2971 | * to true. We don't need test_and_set because we're protected |
| 2972 | * by the set_limit_mutex anyway. |
| 2973 | */ |
| 2974 | memcg_kmem_set_activated(memcg); |
| 2975 | |
| 2976 | ret = memcg_update_all_caches(num+1); |
| 2977 | if (ret) { |
| 2978 | ida_simple_remove(&kmem_limited_groups, num); |
| 2979 | memcg_kmem_clear_activated(memcg); |
| 2980 | return ret; |
| 2981 | } |
| 2982 | |
| 2983 | memcg->kmemcg_id = num; |
| 2984 | INIT_LIST_HEAD(&memcg->memcg_slab_caches); |
| 2985 | mutex_init(&memcg->slab_caches_mutex); |
| 2986 | return 0; |
| 2987 | } |
| 2988 | |
| 2989 | static size_t memcg_caches_array_size(int num_groups) |
| 2990 | { |
| 2991 | ssize_t size; |
| 2992 | if (num_groups <= 0) |
| 2993 | return 0; |
| 2994 | |
| 2995 | size = 2 * num_groups; |
| 2996 | if (size < MEMCG_CACHES_MIN_SIZE) |
| 2997 | size = MEMCG_CACHES_MIN_SIZE; |
| 2998 | else if (size > MEMCG_CACHES_MAX_SIZE) |
| 2999 | size = MEMCG_CACHES_MAX_SIZE; |
| 3000 | |
| 3001 | return size; |
| 3002 | } |
| 3003 | |
| 3004 | /* |
| 3005 | * We should update the current array size iff all caches updates succeed. This |
| 3006 | * can only be done from the slab side. The slab mutex needs to be held when |
| 3007 | * calling this. |
| 3008 | */ |
| 3009 | void memcg_update_array_size(int num) |
| 3010 | { |
| 3011 | if (num > memcg_limited_groups_array_size) |
| 3012 | memcg_limited_groups_array_size = memcg_caches_array_size(num); |
| 3013 | } |
| 3014 | |
Konstantin Khlebnikov | 15cf17d | 2013-03-08 12:43:36 -0800 | [diff] [blame^] | 3015 | static void kmem_cache_destroy_work_func(struct work_struct *w); |
| 3016 | |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 3017 | int memcg_update_cache_size(struct kmem_cache *s, int num_groups) |
| 3018 | { |
| 3019 | struct memcg_cache_params *cur_params = s->memcg_params; |
| 3020 | |
| 3021 | VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache); |
| 3022 | |
| 3023 | if (num_groups > memcg_limited_groups_array_size) { |
| 3024 | int i; |
| 3025 | ssize_t size = memcg_caches_array_size(num_groups); |
| 3026 | |
| 3027 | size *= sizeof(void *); |
| 3028 | size += sizeof(struct memcg_cache_params); |
| 3029 | |
| 3030 | s->memcg_params = kzalloc(size, GFP_KERNEL); |
| 3031 | if (!s->memcg_params) { |
| 3032 | s->memcg_params = cur_params; |
| 3033 | return -ENOMEM; |
| 3034 | } |
| 3035 | |
Konstantin Khlebnikov | 15cf17d | 2013-03-08 12:43:36 -0800 | [diff] [blame^] | 3036 | INIT_WORK(&s->memcg_params->destroy, |
| 3037 | kmem_cache_destroy_work_func); |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 3038 | s->memcg_params->is_root_cache = true; |
| 3039 | |
| 3040 | /* |
| 3041 | * There is the chance it will be bigger than |
| 3042 | * memcg_limited_groups_array_size, if we failed an allocation |
| 3043 | * in a cache, in which case all caches updated before it, will |
| 3044 | * have a bigger array. |
| 3045 | * |
| 3046 | * But if that is the case, the data after |
| 3047 | * memcg_limited_groups_array_size is certainly unused |
| 3048 | */ |
| 3049 | for (i = 0; i < memcg_limited_groups_array_size; i++) { |
| 3050 | if (!cur_params->memcg_caches[i]) |
| 3051 | continue; |
| 3052 | s->memcg_params->memcg_caches[i] = |
| 3053 | cur_params->memcg_caches[i]; |
| 3054 | } |
| 3055 | |
| 3056 | /* |
| 3057 | * Ideally, we would wait until all caches succeed, and only |
| 3058 | * then free the old one. But this is not worth the extra |
| 3059 | * pointer per-cache we'd have to have for this. |
| 3060 | * |
| 3061 | * It is not a big deal if some caches are left with a size |
| 3062 | * bigger than the others. And all updates will reset this |
| 3063 | * anyway. |
| 3064 | */ |
| 3065 | kfree(cur_params); |
| 3066 | } |
| 3067 | return 0; |
| 3068 | } |
| 3069 | |
Glauber Costa | 943a451 | 2012-12-18 14:23:03 -0800 | [diff] [blame] | 3070 | int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s, |
| 3071 | struct kmem_cache *root_cache) |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 3072 | { |
| 3073 | size_t size = sizeof(struct memcg_cache_params); |
| 3074 | |
| 3075 | if (!memcg_kmem_enabled()) |
| 3076 | return 0; |
| 3077 | |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 3078 | if (!memcg) |
| 3079 | size += memcg_limited_groups_array_size * sizeof(void *); |
| 3080 | |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 3081 | s->memcg_params = kzalloc(size, GFP_KERNEL); |
| 3082 | if (!s->memcg_params) |
| 3083 | return -ENOMEM; |
| 3084 | |
Konstantin Khlebnikov | 15cf17d | 2013-03-08 12:43:36 -0800 | [diff] [blame^] | 3085 | INIT_WORK(&s->memcg_params->destroy, |
| 3086 | kmem_cache_destroy_work_func); |
Glauber Costa | 943a451 | 2012-12-18 14:23:03 -0800 | [diff] [blame] | 3087 | if (memcg) { |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 3088 | s->memcg_params->memcg = memcg; |
Glauber Costa | 943a451 | 2012-12-18 14:23:03 -0800 | [diff] [blame] | 3089 | s->memcg_params->root_cache = root_cache; |
Glauber Costa | 4ba902b | 2013-02-12 13:46:22 -0800 | [diff] [blame] | 3090 | } else |
| 3091 | s->memcg_params->is_root_cache = true; |
| 3092 | |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 3093 | return 0; |
| 3094 | } |
| 3095 | |
| 3096 | void memcg_release_cache(struct kmem_cache *s) |
| 3097 | { |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3098 | struct kmem_cache *root; |
| 3099 | struct mem_cgroup *memcg; |
| 3100 | int id; |
| 3101 | |
| 3102 | /* |
| 3103 | * This happens, for instance, when a root cache goes away before we |
| 3104 | * add any memcg. |
| 3105 | */ |
| 3106 | if (!s->memcg_params) |
| 3107 | return; |
| 3108 | |
| 3109 | if (s->memcg_params->is_root_cache) |
| 3110 | goto out; |
| 3111 | |
| 3112 | memcg = s->memcg_params->memcg; |
| 3113 | id = memcg_cache_id(memcg); |
| 3114 | |
| 3115 | root = s->memcg_params->root_cache; |
| 3116 | root->memcg_params->memcg_caches[id] = NULL; |
| 3117 | mem_cgroup_put(memcg); |
| 3118 | |
| 3119 | mutex_lock(&memcg->slab_caches_mutex); |
| 3120 | list_del(&s->memcg_params->list); |
| 3121 | mutex_unlock(&memcg->slab_caches_mutex); |
| 3122 | |
| 3123 | out: |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 3124 | kfree(s->memcg_params); |
| 3125 | } |
| 3126 | |
Glauber Costa | 0e9d92f | 2012-12-18 14:22:42 -0800 | [diff] [blame] | 3127 | /* |
| 3128 | * During the creation a new cache, we need to disable our accounting mechanism |
| 3129 | * altogether. This is true even if we are not creating, but rather just |
| 3130 | * enqueing new caches to be created. |
| 3131 | * |
| 3132 | * This is because that process will trigger allocations; some visible, like |
| 3133 | * explicit kmallocs to auxiliary data structures, name strings and internal |
| 3134 | * cache structures; some well concealed, like INIT_WORK() that can allocate |
| 3135 | * objects during debug. |
| 3136 | * |
| 3137 | * If any allocation happens during memcg_kmem_get_cache, we will recurse back |
| 3138 | * to it. This may not be a bounded recursion: since the first cache creation |
| 3139 | * failed to complete (waiting on the allocation), we'll just try to create the |
| 3140 | * cache again, failing at the same point. |
| 3141 | * |
| 3142 | * memcg_kmem_get_cache is prepared to abort after seeing a positive count of |
| 3143 | * memcg_kmem_skip_account. So we enclose anything that might allocate memory |
| 3144 | * inside the following two functions. |
| 3145 | */ |
| 3146 | static inline void memcg_stop_kmem_account(void) |
| 3147 | { |
| 3148 | VM_BUG_ON(!current->mm); |
| 3149 | current->memcg_kmem_skip_account++; |
| 3150 | } |
| 3151 | |
| 3152 | static inline void memcg_resume_kmem_account(void) |
| 3153 | { |
| 3154 | VM_BUG_ON(!current->mm); |
| 3155 | current->memcg_kmem_skip_account--; |
| 3156 | } |
| 3157 | |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 3158 | static void kmem_cache_destroy_work_func(struct work_struct *w) |
| 3159 | { |
| 3160 | struct kmem_cache *cachep; |
| 3161 | struct memcg_cache_params *p; |
| 3162 | |
| 3163 | p = container_of(w, struct memcg_cache_params, destroy); |
| 3164 | |
| 3165 | cachep = memcg_params_to_cache(p); |
| 3166 | |
Glauber Costa | 2293315 | 2012-12-18 14:22:59 -0800 | [diff] [blame] | 3167 | /* |
| 3168 | * If we get down to 0 after shrink, we could delete right away. |
| 3169 | * However, memcg_release_pages() already puts us back in the workqueue |
| 3170 | * in that case. If we proceed deleting, we'll get a dangling |
| 3171 | * reference, and removing the object from the workqueue in that case |
| 3172 | * is unnecessary complication. We are not a fast path. |
| 3173 | * |
| 3174 | * Note that this case is fundamentally different from racing with |
| 3175 | * shrink_slab(): if memcg_cgroup_destroy_cache() is called in |
| 3176 | * kmem_cache_shrink, not only we would be reinserting a dead cache |
| 3177 | * into the queue, but doing so from inside the worker racing to |
| 3178 | * destroy it. |
| 3179 | * |
| 3180 | * So if we aren't down to zero, we'll just schedule a worker and try |
| 3181 | * again |
| 3182 | */ |
| 3183 | if (atomic_read(&cachep->memcg_params->nr_pages) != 0) { |
| 3184 | kmem_cache_shrink(cachep); |
| 3185 | if (atomic_read(&cachep->memcg_params->nr_pages) == 0) |
| 3186 | return; |
| 3187 | } else |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 3188 | kmem_cache_destroy(cachep); |
| 3189 | } |
| 3190 | |
| 3191 | void mem_cgroup_destroy_cache(struct kmem_cache *cachep) |
| 3192 | { |
| 3193 | if (!cachep->memcg_params->dead) |
| 3194 | return; |
| 3195 | |
| 3196 | /* |
Glauber Costa | 2293315 | 2012-12-18 14:22:59 -0800 | [diff] [blame] | 3197 | * There are many ways in which we can get here. |
| 3198 | * |
| 3199 | * We can get to a memory-pressure situation while the delayed work is |
| 3200 | * still pending to run. The vmscan shrinkers can then release all |
| 3201 | * cache memory and get us to destruction. If this is the case, we'll |
| 3202 | * be executed twice, which is a bug (the second time will execute over |
| 3203 | * bogus data). In this case, cancelling the work should be fine. |
| 3204 | * |
| 3205 | * But we can also get here from the worker itself, if |
| 3206 | * kmem_cache_shrink is enough to shake all the remaining objects and |
| 3207 | * get the page count to 0. In this case, we'll deadlock if we try to |
| 3208 | * cancel the work (the worker runs with an internal lock held, which |
| 3209 | * is the same lock we would hold for cancel_work_sync().) |
| 3210 | * |
| 3211 | * Since we can't possibly know who got us here, just refrain from |
| 3212 | * running if there is already work pending |
| 3213 | */ |
| 3214 | if (work_pending(&cachep->memcg_params->destroy)) |
| 3215 | return; |
| 3216 | /* |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 3217 | * We have to defer the actual destroying to a workqueue, because |
| 3218 | * we might currently be in a context that cannot sleep. |
| 3219 | */ |
| 3220 | schedule_work(&cachep->memcg_params->destroy); |
| 3221 | } |
| 3222 | |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3223 | static char *memcg_cache_name(struct mem_cgroup *memcg, struct kmem_cache *s) |
| 3224 | { |
| 3225 | char *name; |
| 3226 | struct dentry *dentry; |
| 3227 | |
| 3228 | rcu_read_lock(); |
| 3229 | dentry = rcu_dereference(memcg->css.cgroup->dentry); |
| 3230 | rcu_read_unlock(); |
| 3231 | |
| 3232 | BUG_ON(dentry == NULL); |
| 3233 | |
| 3234 | name = kasprintf(GFP_KERNEL, "%s(%d:%s)", s->name, |
| 3235 | memcg_cache_id(memcg), dentry->d_name.name); |
| 3236 | |
| 3237 | return name; |
| 3238 | } |
| 3239 | |
| 3240 | static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg, |
| 3241 | struct kmem_cache *s) |
| 3242 | { |
| 3243 | char *name; |
| 3244 | struct kmem_cache *new; |
| 3245 | |
| 3246 | name = memcg_cache_name(memcg, s); |
| 3247 | if (!name) |
| 3248 | return NULL; |
| 3249 | |
| 3250 | new = kmem_cache_create_memcg(memcg, name, s->object_size, s->align, |
Glauber Costa | 943a451 | 2012-12-18 14:23:03 -0800 | [diff] [blame] | 3251 | (s->flags & ~SLAB_PANIC), s->ctor, s); |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3252 | |
Glauber Costa | d79923f | 2012-12-18 14:22:48 -0800 | [diff] [blame] | 3253 | if (new) |
| 3254 | new->allocflags |= __GFP_KMEMCG; |
| 3255 | |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3256 | kfree(name); |
| 3257 | return new; |
| 3258 | } |
| 3259 | |
| 3260 | /* |
| 3261 | * This lock protects updaters, not readers. We want readers to be as fast as |
| 3262 | * they can, and they will either see NULL or a valid cache value. Our model |
| 3263 | * allow them to see NULL, in which case the root memcg will be selected. |
| 3264 | * |
| 3265 | * We need this lock because multiple allocations to the same cache from a non |
| 3266 | * will span more than one worker. Only one of them can create the cache. |
| 3267 | */ |
| 3268 | static DEFINE_MUTEX(memcg_cache_mutex); |
| 3269 | static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg, |
| 3270 | struct kmem_cache *cachep) |
| 3271 | { |
| 3272 | struct kmem_cache *new_cachep; |
| 3273 | int idx; |
| 3274 | |
| 3275 | BUG_ON(!memcg_can_account_kmem(memcg)); |
| 3276 | |
| 3277 | idx = memcg_cache_id(memcg); |
| 3278 | |
| 3279 | mutex_lock(&memcg_cache_mutex); |
| 3280 | new_cachep = cachep->memcg_params->memcg_caches[idx]; |
| 3281 | if (new_cachep) |
| 3282 | goto out; |
| 3283 | |
| 3284 | new_cachep = kmem_cache_dup(memcg, cachep); |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3285 | if (new_cachep == NULL) { |
| 3286 | new_cachep = cachep; |
| 3287 | goto out; |
| 3288 | } |
| 3289 | |
| 3290 | mem_cgroup_get(memcg); |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 3291 | atomic_set(&new_cachep->memcg_params->nr_pages , 0); |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3292 | |
| 3293 | cachep->memcg_params->memcg_caches[idx] = new_cachep; |
| 3294 | /* |
| 3295 | * the readers won't lock, make sure everybody sees the updated value, |
| 3296 | * so they won't put stuff in the queue again for no reason |
| 3297 | */ |
| 3298 | wmb(); |
| 3299 | out: |
| 3300 | mutex_unlock(&memcg_cache_mutex); |
| 3301 | return new_cachep; |
| 3302 | } |
| 3303 | |
Glauber Costa | 7cf2798 | 2012-12-18 14:22:55 -0800 | [diff] [blame] | 3304 | void kmem_cache_destroy_memcg_children(struct kmem_cache *s) |
| 3305 | { |
| 3306 | struct kmem_cache *c; |
| 3307 | int i; |
| 3308 | |
| 3309 | if (!s->memcg_params) |
| 3310 | return; |
| 3311 | if (!s->memcg_params->is_root_cache) |
| 3312 | return; |
| 3313 | |
| 3314 | /* |
| 3315 | * If the cache is being destroyed, we trust that there is no one else |
| 3316 | * requesting objects from it. Even if there are, the sanity checks in |
| 3317 | * kmem_cache_destroy should caught this ill-case. |
| 3318 | * |
| 3319 | * Still, we don't want anyone else freeing memcg_caches under our |
| 3320 | * noses, which can happen if a new memcg comes to life. As usual, |
| 3321 | * we'll take the set_limit_mutex to protect ourselves against this. |
| 3322 | */ |
| 3323 | mutex_lock(&set_limit_mutex); |
| 3324 | for (i = 0; i < memcg_limited_groups_array_size; i++) { |
| 3325 | c = s->memcg_params->memcg_caches[i]; |
| 3326 | if (!c) |
| 3327 | continue; |
| 3328 | |
| 3329 | /* |
| 3330 | * We will now manually delete the caches, so to avoid races |
| 3331 | * we need to cancel all pending destruction workers and |
| 3332 | * proceed with destruction ourselves. |
| 3333 | * |
| 3334 | * kmem_cache_destroy() will call kmem_cache_shrink internally, |
| 3335 | * and that could spawn the workers again: it is likely that |
| 3336 | * the cache still have active pages until this very moment. |
| 3337 | * This would lead us back to mem_cgroup_destroy_cache. |
| 3338 | * |
| 3339 | * But that will not execute at all if the "dead" flag is not |
| 3340 | * set, so flip it down to guarantee we are in control. |
| 3341 | */ |
| 3342 | c->memcg_params->dead = false; |
Glauber Costa | 2293315 | 2012-12-18 14:22:59 -0800 | [diff] [blame] | 3343 | cancel_work_sync(&c->memcg_params->destroy); |
Glauber Costa | 7cf2798 | 2012-12-18 14:22:55 -0800 | [diff] [blame] | 3344 | kmem_cache_destroy(c); |
| 3345 | } |
| 3346 | mutex_unlock(&set_limit_mutex); |
| 3347 | } |
| 3348 | |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3349 | struct create_work { |
| 3350 | struct mem_cgroup *memcg; |
| 3351 | struct kmem_cache *cachep; |
| 3352 | struct work_struct work; |
| 3353 | }; |
| 3354 | |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 3355 | static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg) |
| 3356 | { |
| 3357 | struct kmem_cache *cachep; |
| 3358 | struct memcg_cache_params *params; |
| 3359 | |
| 3360 | if (!memcg_kmem_is_active(memcg)) |
| 3361 | return; |
| 3362 | |
| 3363 | mutex_lock(&memcg->slab_caches_mutex); |
| 3364 | list_for_each_entry(params, &memcg->memcg_slab_caches, list) { |
| 3365 | cachep = memcg_params_to_cache(params); |
| 3366 | cachep->memcg_params->dead = true; |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 3367 | schedule_work(&cachep->memcg_params->destroy); |
| 3368 | } |
| 3369 | mutex_unlock(&memcg->slab_caches_mutex); |
| 3370 | } |
| 3371 | |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3372 | static void memcg_create_cache_work_func(struct work_struct *w) |
| 3373 | { |
| 3374 | struct create_work *cw; |
| 3375 | |
| 3376 | cw = container_of(w, struct create_work, work); |
| 3377 | memcg_create_kmem_cache(cw->memcg, cw->cachep); |
| 3378 | /* Drop the reference gotten when we enqueued. */ |
| 3379 | css_put(&cw->memcg->css); |
| 3380 | kfree(cw); |
| 3381 | } |
| 3382 | |
| 3383 | /* |
| 3384 | * Enqueue the creation of a per-memcg kmem_cache. |
| 3385 | * Called with rcu_read_lock. |
| 3386 | */ |
Glauber Costa | 0e9d92f | 2012-12-18 14:22:42 -0800 | [diff] [blame] | 3387 | static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg, |
| 3388 | struct kmem_cache *cachep) |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3389 | { |
| 3390 | struct create_work *cw; |
| 3391 | |
| 3392 | cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT); |
| 3393 | if (cw == NULL) |
| 3394 | return; |
| 3395 | |
| 3396 | /* The corresponding put will be done in the workqueue. */ |
| 3397 | if (!css_tryget(&memcg->css)) { |
| 3398 | kfree(cw); |
| 3399 | return; |
| 3400 | } |
| 3401 | |
| 3402 | cw->memcg = memcg; |
| 3403 | cw->cachep = cachep; |
| 3404 | |
| 3405 | INIT_WORK(&cw->work, memcg_create_cache_work_func); |
| 3406 | schedule_work(&cw->work); |
| 3407 | } |
| 3408 | |
Glauber Costa | 0e9d92f | 2012-12-18 14:22:42 -0800 | [diff] [blame] | 3409 | static void memcg_create_cache_enqueue(struct mem_cgroup *memcg, |
| 3410 | struct kmem_cache *cachep) |
| 3411 | { |
| 3412 | /* |
| 3413 | * We need to stop accounting when we kmalloc, because if the |
| 3414 | * corresponding kmalloc cache is not yet created, the first allocation |
| 3415 | * in __memcg_create_cache_enqueue will recurse. |
| 3416 | * |
| 3417 | * However, it is better to enclose the whole function. Depending on |
| 3418 | * the debugging options enabled, INIT_WORK(), for instance, can |
| 3419 | * trigger an allocation. This too, will make us recurse. Because at |
| 3420 | * this point we can't allow ourselves back into memcg_kmem_get_cache, |
| 3421 | * the safest choice is to do it like this, wrapping the whole function. |
| 3422 | */ |
| 3423 | memcg_stop_kmem_account(); |
| 3424 | __memcg_create_cache_enqueue(memcg, cachep); |
| 3425 | memcg_resume_kmem_account(); |
| 3426 | } |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3427 | /* |
| 3428 | * Return the kmem_cache we're supposed to use for a slab allocation. |
| 3429 | * We try to use the current memcg's version of the cache. |
| 3430 | * |
| 3431 | * If the cache does not exist yet, if we are the first user of it, |
| 3432 | * we either create it immediately, if possible, or create it asynchronously |
| 3433 | * in a workqueue. |
| 3434 | * In the latter case, we will let the current allocation go through with |
| 3435 | * the original cache. |
| 3436 | * |
| 3437 | * Can't be called in interrupt context or from kernel threads. |
| 3438 | * This function needs to be called with rcu_read_lock() held. |
| 3439 | */ |
| 3440 | struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep, |
| 3441 | gfp_t gfp) |
| 3442 | { |
| 3443 | struct mem_cgroup *memcg; |
| 3444 | int idx; |
| 3445 | |
| 3446 | VM_BUG_ON(!cachep->memcg_params); |
| 3447 | VM_BUG_ON(!cachep->memcg_params->is_root_cache); |
| 3448 | |
Glauber Costa | 0e9d92f | 2012-12-18 14:22:42 -0800 | [diff] [blame] | 3449 | if (!current->mm || current->memcg_kmem_skip_account) |
| 3450 | return cachep; |
| 3451 | |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3452 | rcu_read_lock(); |
| 3453 | memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner)); |
| 3454 | rcu_read_unlock(); |
| 3455 | |
| 3456 | if (!memcg_can_account_kmem(memcg)) |
| 3457 | return cachep; |
| 3458 | |
| 3459 | idx = memcg_cache_id(memcg); |
| 3460 | |
| 3461 | /* |
| 3462 | * barrier to mare sure we're always seeing the up to date value. The |
| 3463 | * code updating memcg_caches will issue a write barrier to match this. |
| 3464 | */ |
| 3465 | read_barrier_depends(); |
| 3466 | if (unlikely(cachep->memcg_params->memcg_caches[idx] == NULL)) { |
| 3467 | /* |
| 3468 | * If we are in a safe context (can wait, and not in interrupt |
| 3469 | * context), we could be be predictable and return right away. |
| 3470 | * This would guarantee that the allocation being performed |
| 3471 | * already belongs in the new cache. |
| 3472 | * |
| 3473 | * However, there are some clashes that can arrive from locking. |
| 3474 | * For instance, because we acquire the slab_mutex while doing |
| 3475 | * kmem_cache_dup, this means no further allocation could happen |
| 3476 | * with the slab_mutex held. |
| 3477 | * |
| 3478 | * Also, because cache creation issue get_online_cpus(), this |
| 3479 | * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex, |
| 3480 | * that ends up reversed during cpu hotplug. (cpuset allocates |
| 3481 | * a bunch of GFP_KERNEL memory during cpuup). Due to all that, |
| 3482 | * better to defer everything. |
| 3483 | */ |
| 3484 | memcg_create_cache_enqueue(memcg, cachep); |
| 3485 | return cachep; |
| 3486 | } |
| 3487 | |
| 3488 | return cachep->memcg_params->memcg_caches[idx]; |
| 3489 | } |
| 3490 | EXPORT_SYMBOL(__memcg_kmem_get_cache); |
| 3491 | |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3492 | /* |
| 3493 | * We need to verify if the allocation against current->mm->owner's memcg is |
| 3494 | * possible for the given order. But the page is not allocated yet, so we'll |
| 3495 | * need a further commit step to do the final arrangements. |
| 3496 | * |
| 3497 | * It is possible for the task to switch cgroups in this mean time, so at |
| 3498 | * commit time, we can't rely on task conversion any longer. We'll then use |
| 3499 | * the handle argument to return to the caller which cgroup we should commit |
| 3500 | * against. We could also return the memcg directly and avoid the pointer |
| 3501 | * passing, but a boolean return value gives better semantics considering |
| 3502 | * the compiled-out case as well. |
| 3503 | * |
| 3504 | * Returning true means the allocation is possible. |
| 3505 | */ |
| 3506 | bool |
| 3507 | __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order) |
| 3508 | { |
| 3509 | struct mem_cgroup *memcg; |
| 3510 | int ret; |
| 3511 | |
| 3512 | *_memcg = NULL; |
| 3513 | memcg = try_get_mem_cgroup_from_mm(current->mm); |
| 3514 | |
| 3515 | /* |
| 3516 | * very rare case described in mem_cgroup_from_task. Unfortunately there |
| 3517 | * isn't much we can do without complicating this too much, and it would |
| 3518 | * be gfp-dependent anyway. Just let it go |
| 3519 | */ |
| 3520 | if (unlikely(!memcg)) |
| 3521 | return true; |
| 3522 | |
| 3523 | if (!memcg_can_account_kmem(memcg)) { |
| 3524 | css_put(&memcg->css); |
| 3525 | return true; |
| 3526 | } |
| 3527 | |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3528 | ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order); |
| 3529 | if (!ret) |
| 3530 | *_memcg = memcg; |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3531 | |
| 3532 | css_put(&memcg->css); |
| 3533 | return (ret == 0); |
| 3534 | } |
| 3535 | |
| 3536 | void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, |
| 3537 | int order) |
| 3538 | { |
| 3539 | struct page_cgroup *pc; |
| 3540 | |
| 3541 | VM_BUG_ON(mem_cgroup_is_root(memcg)); |
| 3542 | |
| 3543 | /* The page allocation failed. Revert */ |
| 3544 | if (!page) { |
| 3545 | memcg_uncharge_kmem(memcg, PAGE_SIZE << order); |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3546 | return; |
| 3547 | } |
| 3548 | |
| 3549 | pc = lookup_page_cgroup(page); |
| 3550 | lock_page_cgroup(pc); |
| 3551 | pc->mem_cgroup = memcg; |
| 3552 | SetPageCgroupUsed(pc); |
| 3553 | unlock_page_cgroup(pc); |
| 3554 | } |
| 3555 | |
| 3556 | void __memcg_kmem_uncharge_pages(struct page *page, int order) |
| 3557 | { |
| 3558 | struct mem_cgroup *memcg = NULL; |
| 3559 | struct page_cgroup *pc; |
| 3560 | |
| 3561 | |
| 3562 | pc = lookup_page_cgroup(page); |
| 3563 | /* |
| 3564 | * Fast unlocked return. Theoretically might have changed, have to |
| 3565 | * check again after locking. |
| 3566 | */ |
| 3567 | if (!PageCgroupUsed(pc)) |
| 3568 | return; |
| 3569 | |
| 3570 | lock_page_cgroup(pc); |
| 3571 | if (PageCgroupUsed(pc)) { |
| 3572 | memcg = pc->mem_cgroup; |
| 3573 | ClearPageCgroupUsed(pc); |
| 3574 | } |
| 3575 | unlock_page_cgroup(pc); |
| 3576 | |
| 3577 | /* |
| 3578 | * We trust that only if there is a memcg associated with the page, it |
| 3579 | * is a valid allocation |
| 3580 | */ |
| 3581 | if (!memcg) |
| 3582 | return; |
| 3583 | |
| 3584 | VM_BUG_ON(mem_cgroup_is_root(memcg)); |
| 3585 | memcg_uncharge_kmem(memcg, PAGE_SIZE << order); |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3586 | } |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 3587 | #else |
| 3588 | static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg) |
| 3589 | { |
| 3590 | } |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3591 | #endif /* CONFIG_MEMCG_KMEM */ |
| 3592 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3593 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 3594 | |
Kirill A. Shutemov | a0db00f | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 3595 | #define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION) |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3596 | /* |
| 3597 | * Because tail pages are not marked as "used", set it. We're under |
KAMEZAWA Hiroyuki | e94c8a9 | 2012-01-12 17:18:20 -0800 | [diff] [blame] | 3598 | * zone->lru_lock, 'splitting on pmd' and compound_lock. |
| 3599 | * charge/uncharge will be never happen and move_account() is done under |
| 3600 | * compound_lock(), so we don't have to take care of races. |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3601 | */ |
KAMEZAWA Hiroyuki | e94c8a9 | 2012-01-12 17:18:20 -0800 | [diff] [blame] | 3602 | void mem_cgroup_split_huge_fixup(struct page *head) |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3603 | { |
| 3604 | struct page_cgroup *head_pc = lookup_page_cgroup(head); |
KAMEZAWA Hiroyuki | e94c8a9 | 2012-01-12 17:18:20 -0800 | [diff] [blame] | 3605 | struct page_cgroup *pc; |
| 3606 | int i; |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3607 | |
KAMEZAWA Hiroyuki | 3d37c4a | 2011-01-25 15:07:28 -0800 | [diff] [blame] | 3608 | if (mem_cgroup_disabled()) |
| 3609 | return; |
KAMEZAWA Hiroyuki | e94c8a9 | 2012-01-12 17:18:20 -0800 | [diff] [blame] | 3610 | for (i = 1; i < HPAGE_PMD_NR; i++) { |
| 3611 | pc = head_pc + i; |
| 3612 | pc->mem_cgroup = head_pc->mem_cgroup; |
| 3613 | smp_wmb();/* see __commit_charge() */ |
KAMEZAWA Hiroyuki | e94c8a9 | 2012-01-12 17:18:20 -0800 | [diff] [blame] | 3614 | pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT; |
| 3615 | } |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3616 | } |
Hugh Dickins | 12d2710 | 2012-01-12 17:19:52 -0800 | [diff] [blame] | 3617 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3618 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3619 | /** |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 3620 | * mem_cgroup_move_account - move account of the page |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3621 | * @page: the page |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3622 | * @nr_pages: number of regular pages (>1 for huge pages) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3623 | * @pc: page_cgroup of the page. |
| 3624 | * @from: mem_cgroup which the page is moved from. |
| 3625 | * @to: mem_cgroup which the page is moved to. @from != @to. |
| 3626 | * |
| 3627 | * The caller must confirm following. |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3628 | * - page is not on LRU (isolate_page() is useful.) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3629 | * - compound_lock is held when nr_pages > 1 |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3630 | * |
KAMEZAWA Hiroyuki | 2f3479b | 2012-05-29 15:07:04 -0700 | [diff] [blame] | 3631 | * This function doesn't do "charge" to new cgroup and doesn't do "uncharge" |
| 3632 | * from old cgroup. |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3633 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3634 | static int mem_cgroup_move_account(struct page *page, |
| 3635 | unsigned int nr_pages, |
| 3636 | struct page_cgroup *pc, |
| 3637 | struct mem_cgroup *from, |
KAMEZAWA Hiroyuki | 2f3479b | 2012-05-29 15:07:04 -0700 | [diff] [blame] | 3638 | struct mem_cgroup *to) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3639 | { |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 3640 | unsigned long flags; |
| 3641 | int ret; |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 3642 | bool anon = PageAnon(page); |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 3643 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3644 | VM_BUG_ON(from == to); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3645 | VM_BUG_ON(PageLRU(page)); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 3646 | /* |
| 3647 | * The page is isolated from LRU. So, collapse function |
| 3648 | * will not handle this page. But page splitting can happen. |
| 3649 | * Do this check under compound_page_lock(). The caller should |
| 3650 | * hold it. |
| 3651 | */ |
| 3652 | ret = -EBUSY; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3653 | if (nr_pages > 1 && !PageTransHuge(page)) |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 3654 | goto out; |
| 3655 | |
| 3656 | lock_page_cgroup(pc); |
| 3657 | |
| 3658 | ret = -EINVAL; |
| 3659 | if (!PageCgroupUsed(pc) || pc->mem_cgroup != from) |
| 3660 | goto unlock; |
| 3661 | |
KAMEZAWA Hiroyuki | 312734c0 | 2012-03-21 16:34:24 -0700 | [diff] [blame] | 3662 | move_lock_mem_cgroup(from, &flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3663 | |
KAMEZAWA Hiroyuki | 2ff76f1 | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 3664 | if (!anon && page_mapped(page)) { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 3665 | /* Update mapped_file data for mem_cgroup */ |
| 3666 | preempt_disable(); |
| 3667 | __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 3668 | __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 3669 | preempt_enable(); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 3670 | } |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 3671 | mem_cgroup_charge_statistics(from, anon, -nr_pages); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 3672 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 3673 | /* caller should have done css_get */ |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3674 | pc->mem_cgroup = to; |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 3675 | mem_cgroup_charge_statistics(to, anon, nr_pages); |
KAMEZAWA Hiroyuki | 312734c0 | 2012-03-21 16:34:24 -0700 | [diff] [blame] | 3676 | move_unlock_mem_cgroup(from, &flags); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 3677 | ret = 0; |
| 3678 | unlock: |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 3679 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 3680 | /* |
| 3681 | * check events |
| 3682 | */ |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3683 | memcg_check_events(to, page); |
| 3684 | memcg_check_events(from, page); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 3685 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3686 | return ret; |
| 3687 | } |
| 3688 | |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3689 | /** |
| 3690 | * mem_cgroup_move_parent - moves page to the parent group |
| 3691 | * @page: the page to move |
| 3692 | * @pc: page_cgroup of the page |
| 3693 | * @child: page's cgroup |
| 3694 | * |
| 3695 | * move charges to its parent or the root cgroup if the group has no |
| 3696 | * parent (aka use_hierarchy==0). |
| 3697 | * Although this might fail (get_page_unless_zero, isolate_lru_page or |
| 3698 | * mem_cgroup_move_account fails) the failure is always temporary and |
| 3699 | * it signals a race with a page removal/uncharge or migration. In the |
| 3700 | * first case the page is on the way out and it will vanish from the LRU |
| 3701 | * on the next attempt and the call should be retried later. |
| 3702 | * Isolation from the LRU fails only if page has been isolated from |
| 3703 | * the LRU since we looked at it and that usually means either global |
| 3704 | * reclaim or migration going on. The page will either get back to the |
| 3705 | * LRU or vanish. |
| 3706 | * Finaly mem_cgroup_move_account fails only if the page got uncharged |
| 3707 | * (!PageCgroupUsed) or moved to a different group. The page will |
| 3708 | * disappear in the next attempt. |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3709 | */ |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3710 | static int mem_cgroup_move_parent(struct page *page, |
| 3711 | struct page_cgroup *pc, |
KAMEZAWA Hiroyuki | 6068bf0 | 2012-07-31 16:42:45 -0700 | [diff] [blame] | 3712 | struct mem_cgroup *child) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3713 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3714 | struct mem_cgroup *parent; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3715 | unsigned int nr_pages; |
Andrew Morton | 4be4489 | 2011-03-23 16:42:39 -0700 | [diff] [blame] | 3716 | unsigned long uninitialized_var(flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3717 | int ret; |
| 3718 | |
Michal Hocko | d842301 | 2012-10-26 13:37:29 +0200 | [diff] [blame] | 3719 | VM_BUG_ON(mem_cgroup_is_root(child)); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3720 | |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 3721 | ret = -EBUSY; |
| 3722 | if (!get_page_unless_zero(page)) |
| 3723 | goto out; |
| 3724 | if (isolate_lru_page(page)) |
| 3725 | goto put; |
KAMEZAWA Hiroyuki | 52dbb90 | 2011-01-25 15:07:29 -0800 | [diff] [blame] | 3726 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3727 | nr_pages = hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3728 | |
KAMEZAWA Hiroyuki | cc926f7 | 2012-05-29 15:07:04 -0700 | [diff] [blame] | 3729 | parent = parent_mem_cgroup(child); |
| 3730 | /* |
| 3731 | * If no parent, move charges to root cgroup. |
| 3732 | */ |
| 3733 | if (!parent) |
| 3734 | parent = root_mem_cgroup; |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3735 | |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3736 | if (nr_pages > 1) { |
| 3737 | VM_BUG_ON(!PageTransHuge(page)); |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 3738 | flags = compound_lock_irqsave(page); |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3739 | } |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 3740 | |
KAMEZAWA Hiroyuki | cc926f7 | 2012-05-29 15:07:04 -0700 | [diff] [blame] | 3741 | ret = mem_cgroup_move_account(page, nr_pages, |
KAMEZAWA Hiroyuki | 2f3479b | 2012-05-29 15:07:04 -0700 | [diff] [blame] | 3742 | pc, child, parent); |
KAMEZAWA Hiroyuki | cc926f7 | 2012-05-29 15:07:04 -0700 | [diff] [blame] | 3743 | if (!ret) |
| 3744 | __mem_cgroup_cancel_local_charge(child, nr_pages); |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 3745 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3746 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 3747 | compound_unlock_irqrestore(page, flags); |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3748 | putback_lru_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 3749 | put: |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 3750 | put_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 3751 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3752 | return ret; |
| 3753 | } |
| 3754 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3755 | /* |
| 3756 | * Charge the memory controller for page usage. |
| 3757 | * Return |
| 3758 | * 0 if the charge was successful |
| 3759 | * < 0 if the cgroup is over its limit |
| 3760 | */ |
| 3761 | 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] | 3762 | gfp_t gfp_mask, enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3763 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3764 | struct mem_cgroup *memcg = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3765 | unsigned int nr_pages = 1; |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 3766 | bool oom = true; |
| 3767 | int ret; |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 3768 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 3769 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3770 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 3771 | VM_BUG_ON(!PageTransHuge(page)); |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 3772 | /* |
| 3773 | * Never OOM-kill a process for a huge page. The |
| 3774 | * fault handler will fall back to regular pages. |
| 3775 | */ |
| 3776 | oom = false; |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 3777 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3778 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3779 | ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom); |
KAMEZAWA Hiroyuki | 38c5d72 | 2012-01-12 17:19:01 -0800 | [diff] [blame] | 3780 | if (ret == -ENOMEM) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3781 | return ret; |
Johannes Weiner | ce587e6 | 2012-04-24 20:22:33 +0200 | [diff] [blame] | 3782 | __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3783 | return 0; |
| 3784 | } |
| 3785 | |
| 3786 | int mem_cgroup_newpage_charge(struct page *page, |
| 3787 | struct mm_struct *mm, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 3788 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3789 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 3790 | return 0; |
Johannes Weiner | 7a0524c | 2012-01-12 17:18:43 -0800 | [diff] [blame] | 3791 | VM_BUG_ON(page_mapped(page)); |
| 3792 | VM_BUG_ON(page->mapping && !PageAnon(page)); |
| 3793 | VM_BUG_ON(!mm); |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 3794 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
Kamezawa Hiroyuki | 41326c1 | 2012-07-31 16:41:40 -0700 | [diff] [blame] | 3795 | MEM_CGROUP_CHARGE_TYPE_ANON); |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 3796 | } |
| 3797 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 3798 | /* |
| 3799 | * While swap-in, try_charge -> commit or cancel, the page is locked. |
| 3800 | * 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] | 3801 | * struct page_cgroup is acquired. This refcnt will be consumed by |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 3802 | * "commit()" or removed by "cancel()" |
| 3803 | */ |
Johannes Weiner | 0435a2f | 2012-07-31 16:45:43 -0700 | [diff] [blame] | 3804 | static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
| 3805 | struct page *page, |
| 3806 | gfp_t mask, |
| 3807 | struct mem_cgroup **memcgp) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3808 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3809 | struct mem_cgroup *memcg; |
Johannes Weiner | 90deb78 | 2012-07-31 16:45:47 -0700 | [diff] [blame] | 3810 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 3811 | int ret; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3812 | |
Johannes Weiner | 90deb78 | 2012-07-31 16:45:47 -0700 | [diff] [blame] | 3813 | pc = lookup_page_cgroup(page); |
| 3814 | /* |
| 3815 | * Every swap fault against a single page tries to charge the |
| 3816 | * page, bail as early as possible. shmem_unuse() encounters |
| 3817 | * already charged pages, too. The USED bit is protected by |
| 3818 | * the page lock, which serializes swap cache removal, which |
| 3819 | * in turn serializes uncharging. |
| 3820 | */ |
| 3821 | if (PageCgroupUsed(pc)) |
| 3822 | return 0; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3823 | if (!do_swap_account) |
| 3824 | goto charge_cur_mm; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3825 | memcg = try_get_mem_cgroup_from_page(page); |
| 3826 | if (!memcg) |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 3827 | goto charge_cur_mm; |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 3828 | *memcgp = memcg; |
| 3829 | ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3830 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 38c5d72 | 2012-01-12 17:19:01 -0800 | [diff] [blame] | 3831 | if (ret == -EINTR) |
| 3832 | ret = 0; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 3833 | return ret; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3834 | charge_cur_mm: |
KAMEZAWA Hiroyuki | 38c5d72 | 2012-01-12 17:19:01 -0800 | [diff] [blame] | 3835 | ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true); |
| 3836 | if (ret == -EINTR) |
| 3837 | ret = 0; |
| 3838 | return ret; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3839 | } |
| 3840 | |
Johannes Weiner | 0435a2f | 2012-07-31 16:45:43 -0700 | [diff] [blame] | 3841 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page, |
| 3842 | gfp_t gfp_mask, struct mem_cgroup **memcgp) |
| 3843 | { |
| 3844 | *memcgp = NULL; |
| 3845 | if (mem_cgroup_disabled()) |
| 3846 | return 0; |
Johannes Weiner | bdf4f4d | 2012-07-31 16:45:50 -0700 | [diff] [blame] | 3847 | /* |
| 3848 | * A racing thread's fault, or swapoff, may have already |
| 3849 | * updated the pte, and even removed page from swap cache: in |
| 3850 | * those cases unuse_pte()'s pte_same() test will fail; but |
| 3851 | * there's also a KSM case which does need to charge the page. |
| 3852 | */ |
| 3853 | if (!PageSwapCache(page)) { |
| 3854 | int ret; |
| 3855 | |
| 3856 | ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true); |
| 3857 | if (ret == -EINTR) |
| 3858 | ret = 0; |
| 3859 | return ret; |
| 3860 | } |
Johannes Weiner | 0435a2f | 2012-07-31 16:45:43 -0700 | [diff] [blame] | 3861 | return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp); |
| 3862 | } |
| 3863 | |
Johannes Weiner | 827a03d | 2012-07-31 16:45:36 -0700 | [diff] [blame] | 3864 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg) |
| 3865 | { |
| 3866 | if (mem_cgroup_disabled()) |
| 3867 | return; |
| 3868 | if (!memcg) |
| 3869 | return; |
| 3870 | __mem_cgroup_cancel_charge(memcg, 1); |
| 3871 | } |
| 3872 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 3873 | static void |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 3874 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg, |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 3875 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3876 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3877 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3878 | return; |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 3879 | if (!memcg) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3880 | return; |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 3881 | |
Johannes Weiner | ce587e6 | 2012-04-24 20:22:33 +0200 | [diff] [blame] | 3882 | __mem_cgroup_commit_charge(memcg, page, 1, ctype, true); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3883 | /* |
| 3884 | * Now swap is on-memory. This means this page may be |
| 3885 | * counted both as mem and swap....double count. |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 3886 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
| 3887 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() |
| 3888 | * may call delete_from_swap_cache() before reach here. |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3889 | */ |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 3890 | if (do_swap_account && PageSwapCache(page)) { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3891 | swp_entry_t ent = {.val = page_private(page)}; |
Hugh Dickins | 8649300 | 2012-05-29 15:06:52 -0700 | [diff] [blame] | 3892 | mem_cgroup_uncharge_swap(ent); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3893 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3894 | } |
| 3895 | |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 3896 | void mem_cgroup_commit_charge_swapin(struct page *page, |
| 3897 | struct mem_cgroup *memcg) |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 3898 | { |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 3899 | __mem_cgroup_commit_charge_swapin(page, memcg, |
Kamezawa Hiroyuki | 41326c1 | 2012-07-31 16:41:40 -0700 | [diff] [blame] | 3900 | MEM_CGROUP_CHARGE_TYPE_ANON); |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 3901 | } |
| 3902 | |
Johannes Weiner | 827a03d | 2012-07-31 16:45:36 -0700 | [diff] [blame] | 3903 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 3904 | gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3905 | { |
Johannes Weiner | 827a03d | 2012-07-31 16:45:36 -0700 | [diff] [blame] | 3906 | struct mem_cgroup *memcg = NULL; |
| 3907 | enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE; |
| 3908 | int ret; |
| 3909 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3910 | if (mem_cgroup_disabled()) |
Johannes Weiner | 827a03d | 2012-07-31 16:45:36 -0700 | [diff] [blame] | 3911 | return 0; |
| 3912 | if (PageCompound(page)) |
| 3913 | return 0; |
| 3914 | |
Johannes Weiner | 827a03d | 2012-07-31 16:45:36 -0700 | [diff] [blame] | 3915 | if (!PageSwapCache(page)) |
| 3916 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, type); |
| 3917 | else { /* page is swapcache/shmem */ |
Johannes Weiner | 0435a2f | 2012-07-31 16:45:43 -0700 | [diff] [blame] | 3918 | ret = __mem_cgroup_try_charge_swapin(mm, page, |
| 3919 | gfp_mask, &memcg); |
Johannes Weiner | 827a03d | 2012-07-31 16:45:36 -0700 | [diff] [blame] | 3920 | if (!ret) |
| 3921 | __mem_cgroup_commit_charge_swapin(page, memcg, type); |
| 3922 | } |
| 3923 | return ret; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3924 | } |
| 3925 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3926 | static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3927 | unsigned int nr_pages, |
| 3928 | const enum charge_type ctype) |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3929 | { |
| 3930 | struct memcg_batch_info *batch = NULL; |
| 3931 | bool uncharge_memsw = true; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3932 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3933 | /* If swapout, usage of swap doesn't decrease */ |
| 3934 | if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) |
| 3935 | uncharge_memsw = false; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3936 | |
| 3937 | batch = ¤t->memcg_batch; |
| 3938 | /* |
| 3939 | * In usual, we do css_get() when we remember memcg pointer. |
| 3940 | * But in this case, we keep res->usage until end of a series of |
| 3941 | * uncharges. Then, it's ok to ignore memcg's refcnt. |
| 3942 | */ |
| 3943 | if (!batch->memcg) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3944 | batch->memcg = memcg; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3945 | /* |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3946 | * do_batch > 0 when unmapping pages or inode invalidate/truncate. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3947 | * 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] | 3948 | * the same cgroup and we have chance to coalesce uncharges. |
| 3949 | * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE) |
| 3950 | * because we want to do uncharge as soon as possible. |
| 3951 | */ |
| 3952 | |
| 3953 | if (!batch->do_batch || test_thread_flag(TIF_MEMDIE)) |
| 3954 | goto direct_uncharge; |
| 3955 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3956 | if (nr_pages > 1) |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 3957 | goto direct_uncharge; |
| 3958 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3959 | /* |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3960 | * In typical case, batch->memcg == mem. This means we can |
| 3961 | * merge a series of uncharges to an uncharge of res_counter. |
| 3962 | * If not, we uncharge res_counter ony by one. |
| 3963 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3964 | if (batch->memcg != memcg) |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3965 | goto direct_uncharge; |
| 3966 | /* remember freed charge and uncharge it later */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3967 | batch->nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3968 | if (uncharge_memsw) |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3969 | batch->memsw_nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3970 | return; |
| 3971 | direct_uncharge: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3972 | res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3973 | if (uncharge_memsw) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3974 | res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE); |
| 3975 | if (unlikely(batch->memcg != memcg)) |
| 3976 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3977 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 3978 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 3979 | /* |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3980 | * uncharge if !page_mapped(page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3981 | */ |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3982 | static struct mem_cgroup * |
Johannes Weiner | 0030f53 | 2012-07-31 16:45:25 -0700 | [diff] [blame] | 3983 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype, |
| 3984 | bool end_migration) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3985 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3986 | struct mem_cgroup *memcg = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3987 | unsigned int nr_pages = 1; |
| 3988 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 3989 | bool anon; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3990 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3991 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3992 | return NULL; |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 3993 | |
Johannes Weiner | 0c59b89 | 2012-07-31 16:45:31 -0700 | [diff] [blame] | 3994 | VM_BUG_ON(PageSwapCache(page)); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3995 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 3996 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3997 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 3998 | VM_BUG_ON(!PageTransHuge(page)); |
| 3999 | } |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 4000 | /* |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 4001 | * Check if our page_cgroup is valid |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 4002 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 4003 | pc = lookup_page_cgroup(page); |
Johannes Weiner | cfa4494 | 2012-01-12 17:18:38 -0800 | [diff] [blame] | 4004 | if (unlikely(!PageCgroupUsed(pc))) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4005 | return NULL; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 4006 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 4007 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 4008 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4009 | memcg = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4010 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 4011 | if (!PageCgroupUsed(pc)) |
| 4012 | goto unlock_out; |
| 4013 | |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 4014 | anon = PageAnon(page); |
| 4015 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 4016 | switch (ctype) { |
Kamezawa Hiroyuki | 41326c1 | 2012-07-31 16:41:40 -0700 | [diff] [blame] | 4017 | case MEM_CGROUP_CHARGE_TYPE_ANON: |
KAMEZAWA Hiroyuki | 2ff76f1 | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 4018 | /* |
| 4019 | * Generally PageAnon tells if it's the anon statistics to be |
| 4020 | * updated; but sometimes e.g. mem_cgroup_uncharge_page() is |
| 4021 | * used before page reached the stage of being marked PageAnon. |
| 4022 | */ |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 4023 | anon = true; |
| 4024 | /* fallthrough */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 4025 | case MEM_CGROUP_CHARGE_TYPE_DROP: |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4026 | /* See mem_cgroup_prepare_migration() */ |
Johannes Weiner | 0030f53 | 2012-07-31 16:45:25 -0700 | [diff] [blame] | 4027 | if (page_mapped(page)) |
| 4028 | goto unlock_out; |
| 4029 | /* |
| 4030 | * Pages under migration may not be uncharged. But |
| 4031 | * end_migration() /must/ be the one uncharging the |
| 4032 | * unused post-migration page and so it has to call |
| 4033 | * here with the migration bit still set. See the |
| 4034 | * res_counter handling below. |
| 4035 | */ |
| 4036 | if (!end_migration && PageCgroupMigration(pc)) |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 4037 | goto unlock_out; |
| 4038 | break; |
| 4039 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: |
| 4040 | if (!PageAnon(page)) { /* Shared memory */ |
| 4041 | if (page->mapping && !page_is_file_cache(page)) |
| 4042 | goto unlock_out; |
| 4043 | } else if (page_mapped(page)) /* Anon */ |
| 4044 | goto unlock_out; |
| 4045 | break; |
| 4046 | default: |
| 4047 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 4048 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 4049 | |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 4050 | mem_cgroup_charge_statistics(memcg, anon, -nr_pages); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4051 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 4052 | ClearPageCgroupUsed(pc); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 4053 | /* |
| 4054 | * pc->mem_cgroup is not cleared here. It will be accessed when it's |
| 4055 | * freed from LRU. This is safe because uncharged page is expected not |
| 4056 | * to be reused (freed soon). Exception is SwapCache, it's handled by |
| 4057 | * special functions. |
| 4058 | */ |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 4059 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 4060 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 4061 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4062 | * even after unlock, we have memcg->res.usage here and this memcg |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 4063 | * will never be freed. |
| 4064 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4065 | memcg_check_events(memcg, page); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 4066 | if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4067 | mem_cgroup_swap_statistics(memcg, true); |
| 4068 | mem_cgroup_get(memcg); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 4069 | } |
Johannes Weiner | 0030f53 | 2012-07-31 16:45:25 -0700 | [diff] [blame] | 4070 | /* |
| 4071 | * Migration does not charge the res_counter for the |
| 4072 | * replacement page, so leave it alone when phasing out the |
| 4073 | * page that is unused after the migration. |
| 4074 | */ |
| 4075 | if (!end_migration && !mem_cgroup_is_root(memcg)) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4076 | mem_cgroup_do_uncharge(memcg, nr_pages, ctype); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4077 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4078 | return memcg; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 4079 | |
| 4080 | unlock_out: |
| 4081 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4082 | return NULL; |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 4083 | } |
| 4084 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 4085 | void mem_cgroup_uncharge_page(struct page *page) |
| 4086 | { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 4087 | /* early check. */ |
| 4088 | if (page_mapped(page)) |
| 4089 | return; |
Johannes Weiner | 40f23a2 | 2012-01-12 17:18:45 -0800 | [diff] [blame] | 4090 | VM_BUG_ON(page->mapping && !PageAnon(page)); |
Johannes Weiner | 0c59b89 | 2012-07-31 16:45:31 -0700 | [diff] [blame] | 4091 | if (PageSwapCache(page)) |
| 4092 | return; |
Johannes Weiner | 0030f53 | 2012-07-31 16:45:25 -0700 | [diff] [blame] | 4093 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | void mem_cgroup_uncharge_cache_page(struct page *page) |
| 4097 | { |
| 4098 | VM_BUG_ON(page_mapped(page)); |
KAMEZAWA Hiroyuki | b7abea9 | 2008-10-18 20:28:09 -0700 | [diff] [blame] | 4099 | VM_BUG_ON(page->mapping); |
Johannes Weiner | 0030f53 | 2012-07-31 16:45:25 -0700 | [diff] [blame] | 4100 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 4101 | } |
| 4102 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 4103 | /* |
| 4104 | * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate. |
| 4105 | * In that cases, pages are freed continuously and we can expect pages |
| 4106 | * are in the same memcg. All these calls itself limits the number of |
| 4107 | * pages freed at once, then uncharge_start/end() is called properly. |
| 4108 | * This may be called prural(2) times in a context, |
| 4109 | */ |
| 4110 | |
| 4111 | void mem_cgroup_uncharge_start(void) |
| 4112 | { |
| 4113 | current->memcg_batch.do_batch++; |
| 4114 | /* We can do nest. */ |
| 4115 | if (current->memcg_batch.do_batch == 1) { |
| 4116 | current->memcg_batch.memcg = NULL; |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 4117 | current->memcg_batch.nr_pages = 0; |
| 4118 | current->memcg_batch.memsw_nr_pages = 0; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 4119 | } |
| 4120 | } |
| 4121 | |
| 4122 | void mem_cgroup_uncharge_end(void) |
| 4123 | { |
| 4124 | struct memcg_batch_info *batch = ¤t->memcg_batch; |
| 4125 | |
| 4126 | if (!batch->do_batch) |
| 4127 | return; |
| 4128 | |
| 4129 | batch->do_batch--; |
| 4130 | if (batch->do_batch) /* If stacked, do nothing. */ |
| 4131 | return; |
| 4132 | |
| 4133 | if (!batch->memcg) |
| 4134 | return; |
| 4135 | /* |
| 4136 | * This "batch->memcg" is valid without any css_get/put etc... |
| 4137 | * bacause we hide charges behind us. |
| 4138 | */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 4139 | if (batch->nr_pages) |
| 4140 | res_counter_uncharge(&batch->memcg->res, |
| 4141 | batch->nr_pages * PAGE_SIZE); |
| 4142 | if (batch->memsw_nr_pages) |
| 4143 | res_counter_uncharge(&batch->memcg->memsw, |
| 4144 | batch->memsw_nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4145 | memcg_oom_recover(batch->memcg); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 4146 | /* forget this pointer (for sanity check) */ |
| 4147 | batch->memcg = NULL; |
| 4148 | } |
| 4149 | |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 4150 | #ifdef CONFIG_SWAP |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4151 | /* |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 4152 | * called after __delete_from_swap_cache() and drop "page" account. |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4153 | * memcg information is recorded to swap_cgroup of "ent" |
| 4154 | */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 4155 | void |
| 4156 | 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] | 4157 | { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4158 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 4159 | int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4160 | |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 4161 | if (!swapout) /* this was a swap cache but the swap is unused ! */ |
| 4162 | ctype = MEM_CGROUP_CHARGE_TYPE_DROP; |
| 4163 | |
Johannes Weiner | 0030f53 | 2012-07-31 16:45:25 -0700 | [diff] [blame] | 4164 | memcg = __mem_cgroup_uncharge_common(page, ctype, false); |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 4165 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 4166 | /* |
| 4167 | * record memcg information, if swapout && memcg != NULL, |
| 4168 | * mem_cgroup_get() was called in uncharge(). |
| 4169 | */ |
| 4170 | if (do_swap_account && swapout && memcg) |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 4171 | swap_cgroup_record(ent, css_id(&memcg->css)); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 4172 | } |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 4173 | #endif |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 4174 | |
Andrew Morton | c255a45 | 2012-07-31 16:43:02 -0700 | [diff] [blame] | 4175 | #ifdef CONFIG_MEMCG_SWAP |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4176 | /* |
| 4177 | * called from swap_entry_free(). remove record in swap_cgroup and |
| 4178 | * uncharge "memsw" account. |
| 4179 | */ |
| 4180 | void mem_cgroup_uncharge_swap(swp_entry_t ent) |
| 4181 | { |
| 4182 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 4183 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4184 | |
| 4185 | if (!do_swap_account) |
| 4186 | return; |
| 4187 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 4188 | id = swap_cgroup_record(ent, 0); |
| 4189 | rcu_read_lock(); |
| 4190 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4191 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 4192 | /* |
| 4193 | * We uncharge this because swap is freed. |
| 4194 | * This memcg can be obsolete one. We avoid calling css_tryget |
| 4195 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 4196 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 4197 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 4198 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4199 | mem_cgroup_put(memcg); |
| 4200 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 4201 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4202 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4203 | |
| 4204 | /** |
| 4205 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. |
| 4206 | * @entry: swap entry to be moved |
| 4207 | * @from: mem_cgroup which the entry is moved from |
| 4208 | * @to: mem_cgroup which the entry is moved to |
| 4209 | * |
| 4210 | * It succeeds only when the swap_cgroup's record for this entry is the same |
| 4211 | * as the mem_cgroup's id of @from. |
| 4212 | * |
| 4213 | * Returns 0 on success, -EINVAL on failure. |
| 4214 | * |
| 4215 | * The caller must have charged to @to, IOW, called res_counter_charge() about |
| 4216 | * both res and memsw, and called css_get(). |
| 4217 | */ |
| 4218 | static int mem_cgroup_move_swap_account(swp_entry_t entry, |
Hugh Dickins | e91cbb4 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 4219 | struct mem_cgroup *from, struct mem_cgroup *to) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4220 | { |
| 4221 | unsigned short old_id, new_id; |
| 4222 | |
| 4223 | old_id = css_id(&from->css); |
| 4224 | new_id = css_id(&to->css); |
| 4225 | |
| 4226 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4227 | mem_cgroup_swap_statistics(from, false); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4228 | mem_cgroup_swap_statistics(to, true); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4229 | /* |
| 4230 | * This function is only called from task migration context now. |
| 4231 | * It postpones res_counter and refcount handling till the end |
| 4232 | * of task migration(mem_cgroup_clear_mc()) for performance |
| 4233 | * improvement. But we cannot postpone mem_cgroup_get(to) |
| 4234 | * because if the process that has been moved to @to does |
| 4235 | * swap-in, the refcount of @to might be decreased to 0. |
| 4236 | */ |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4237 | mem_cgroup_get(to); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4238 | return 0; |
| 4239 | } |
| 4240 | return -EINVAL; |
| 4241 | } |
| 4242 | #else |
| 4243 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, |
Hugh Dickins | e91cbb4 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 4244 | struct mem_cgroup *from, struct mem_cgroup *to) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4245 | { |
| 4246 | return -EINVAL; |
| 4247 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4248 | #endif |
| 4249 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 4250 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 4251 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
| 4252 | * page belongs to. |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 4253 | */ |
Johannes Weiner | 0030f53 | 2012-07-31 16:45:25 -0700 | [diff] [blame] | 4254 | void mem_cgroup_prepare_migration(struct page *page, struct page *newpage, |
| 4255 | struct mem_cgroup **memcgp) |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 4256 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4257 | struct mem_cgroup *memcg = NULL; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 4258 | unsigned int nr_pages = 1; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 4259 | struct page_cgroup *pc; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4260 | enum charge_type ctype; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 4261 | |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 4262 | *memcgp = NULL; |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 4263 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 4264 | if (mem_cgroup_disabled()) |
Johannes Weiner | 0030f53 | 2012-07-31 16:45:25 -0700 | [diff] [blame] | 4265 | return; |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 4266 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 4267 | if (PageTransHuge(page)) |
| 4268 | nr_pages <<= compound_order(page); |
| 4269 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 4270 | pc = lookup_page_cgroup(page); |
| 4271 | lock_page_cgroup(pc); |
| 4272 | if (PageCgroupUsed(pc)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4273 | memcg = pc->mem_cgroup; |
| 4274 | css_get(&memcg->css); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4275 | /* |
| 4276 | * At migrating an anonymous page, its mapcount goes down |
| 4277 | * to 0 and uncharge() will be called. But, even if it's fully |
| 4278 | * unmapped, migration may fail and this page has to be |
| 4279 | * charged again. We set MIGRATION flag here and delay uncharge |
| 4280 | * until end_migration() is called |
| 4281 | * |
| 4282 | * Corner Case Thinking |
| 4283 | * A) |
| 4284 | * When the old page was mapped as Anon and it's unmap-and-freed |
| 4285 | * while migration was ongoing. |
| 4286 | * If unmap finds the old page, uncharge() of it will be delayed |
| 4287 | * until end_migration(). If unmap finds a new page, it's |
| 4288 | * uncharged when it make mapcount to be 1->0. If unmap code |
| 4289 | * finds swap_migration_entry, the new page will not be mapped |
| 4290 | * and end_migration() will find it(mapcount==0). |
| 4291 | * |
| 4292 | * B) |
| 4293 | * When the old page was mapped but migraion fails, the kernel |
| 4294 | * remaps it. A charge for it is kept by MIGRATION flag even |
| 4295 | * if mapcount goes down to 0. We can do remap successfully |
| 4296 | * without charging it again. |
| 4297 | * |
| 4298 | * C) |
| 4299 | * The "old" page is under lock_page() until the end of |
| 4300 | * migration, so, the old page itself will not be swapped-out. |
| 4301 | * If the new page is swapped out before end_migraton, our |
| 4302 | * hook to usual swap-out path will catch the event. |
| 4303 | */ |
| 4304 | if (PageAnon(page)) |
| 4305 | SetPageCgroupMigration(pc); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 4306 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 4307 | unlock_page_cgroup(pc); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4308 | /* |
| 4309 | * If the page is not charged at this point, |
| 4310 | * we return here. |
| 4311 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4312 | if (!memcg) |
Johannes Weiner | 0030f53 | 2012-07-31 16:45:25 -0700 | [diff] [blame] | 4313 | return; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 4314 | |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 4315 | *memcgp = memcg; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4316 | /* |
| 4317 | * We charge new page before it's used/mapped. So, even if unlock_page() |
| 4318 | * is called before end_migration, we can catch all events on this new |
| 4319 | * page. In the case new page is migrated but not remapped, new page's |
| 4320 | * mapcount will be finally 0 and we call uncharge in end_migration(). |
| 4321 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4322 | if (PageAnon(page)) |
Kamezawa Hiroyuki | 41326c1 | 2012-07-31 16:41:40 -0700 | [diff] [blame] | 4323 | ctype = MEM_CGROUP_CHARGE_TYPE_ANON; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4324 | else |
Johannes Weiner | 62ba744 | 2012-07-31 16:45:39 -0700 | [diff] [blame] | 4325 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; |
Johannes Weiner | 0030f53 | 2012-07-31 16:45:25 -0700 | [diff] [blame] | 4326 | /* |
| 4327 | * The page is committed to the memcg, but it's not actually |
| 4328 | * charged to the res_counter since we plan on replacing the |
| 4329 | * old one and only one page is going to be left afterwards. |
| 4330 | */ |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 4331 | __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 4332 | } |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 4333 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 4334 | /* remove redundant charge if migration failed*/ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4335 | void mem_cgroup_end_migration(struct mem_cgroup *memcg, |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 4336 | struct page *oldpage, struct page *newpage, bool migration_ok) |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 4337 | { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4338 | struct page *used, *unused; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 4339 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 4340 | bool anon; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 4341 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4342 | if (!memcg) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 4343 | return; |
Tejun Heo | b25ed60 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4344 | |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 4345 | if (!migration_ok) { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4346 | used = oldpage; |
| 4347 | unused = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 4348 | } else { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4349 | used = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 4350 | unused = oldpage; |
| 4351 | } |
Johannes Weiner | 0030f53 | 2012-07-31 16:45:25 -0700 | [diff] [blame] | 4352 | anon = PageAnon(used); |
Johannes Weiner | 7d18895 | 2012-07-31 16:45:34 -0700 | [diff] [blame] | 4353 | __mem_cgroup_uncharge_common(unused, |
| 4354 | anon ? MEM_CGROUP_CHARGE_TYPE_ANON |
| 4355 | : MEM_CGROUP_CHARGE_TYPE_CACHE, |
| 4356 | true); |
Johannes Weiner | 0030f53 | 2012-07-31 16:45:25 -0700 | [diff] [blame] | 4357 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 4358 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4359 | * We disallowed uncharge of pages under migration because mapcount |
| 4360 | * of the page goes down to zero, temporarly. |
| 4361 | * Clear the flag and check the page should be charged. |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 4362 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4363 | pc = lookup_page_cgroup(oldpage); |
| 4364 | lock_page_cgroup(pc); |
| 4365 | ClearPageCgroupMigration(pc); |
| 4366 | unlock_page_cgroup(pc); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4367 | |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 4368 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4369 | * If a page is a file cache, radix-tree replacement is very atomic |
| 4370 | * and we can skip this check. When it was an Anon page, its mapcount |
| 4371 | * goes down to 0. But because we added MIGRATION flage, it's not |
| 4372 | * uncharged yet. There are several case but page->mapcount check |
| 4373 | * and USED bit check in mem_cgroup_uncharge_page() will do enough |
| 4374 | * check. (see prepare_charge() also) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 4375 | */ |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 4376 | if (anon) |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4377 | mem_cgroup_uncharge_page(used); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 4378 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 4379 | |
KAMEZAWA Hiroyuki | ab936cb | 2012-01-12 17:17:44 -0800 | [diff] [blame] | 4380 | /* |
| 4381 | * At replace page cache, newpage is not under any memcg but it's on |
| 4382 | * LRU. So, this function doesn't touch res_counter but handles LRU |
| 4383 | * in correct way. Both pages are locked so we cannot race with uncharge. |
| 4384 | */ |
| 4385 | void mem_cgroup_replace_page_cache(struct page *oldpage, |
| 4386 | struct page *newpage) |
| 4387 | { |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 4388 | struct mem_cgroup *memcg = NULL; |
KAMEZAWA Hiroyuki | ab936cb | 2012-01-12 17:17:44 -0800 | [diff] [blame] | 4389 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | ab936cb | 2012-01-12 17:17:44 -0800 | [diff] [blame] | 4390 | enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE; |
KAMEZAWA Hiroyuki | ab936cb | 2012-01-12 17:17:44 -0800 | [diff] [blame] | 4391 | |
| 4392 | if (mem_cgroup_disabled()) |
| 4393 | return; |
| 4394 | |
| 4395 | pc = lookup_page_cgroup(oldpage); |
| 4396 | /* fix accounting on old pages */ |
| 4397 | lock_page_cgroup(pc); |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 4398 | if (PageCgroupUsed(pc)) { |
| 4399 | memcg = pc->mem_cgroup; |
| 4400 | mem_cgroup_charge_statistics(memcg, false, -1); |
| 4401 | ClearPageCgroupUsed(pc); |
| 4402 | } |
KAMEZAWA Hiroyuki | ab936cb | 2012-01-12 17:17:44 -0800 | [diff] [blame] | 4403 | unlock_page_cgroup(pc); |
| 4404 | |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 4405 | /* |
| 4406 | * When called from shmem_replace_page(), in some cases the |
| 4407 | * oldpage has already been charged, and in some cases not. |
| 4408 | */ |
| 4409 | if (!memcg) |
| 4410 | return; |
KAMEZAWA Hiroyuki | ab936cb | 2012-01-12 17:17:44 -0800 | [diff] [blame] | 4411 | /* |
| 4412 | * Even if newpage->mapping was NULL before starting replacement, |
| 4413 | * the newpage may be on LRU(or pagevec for LRU) already. We lock |
| 4414 | * LRU while we overwrite pc->mem_cgroup. |
| 4415 | */ |
Johannes Weiner | ce587e6 | 2012-04-24 20:22:33 +0200 | [diff] [blame] | 4416 | __mem_cgroup_commit_charge(memcg, newpage, 1, type, true); |
KAMEZAWA Hiroyuki | ab936cb | 2012-01-12 17:17:44 -0800 | [diff] [blame] | 4417 | } |
| 4418 | |
Daisuke Nishimura | f212ad7 | 2011-03-23 16:42:25 -0700 | [diff] [blame] | 4419 | #ifdef CONFIG_DEBUG_VM |
| 4420 | static struct page_cgroup *lookup_page_cgroup_used(struct page *page) |
| 4421 | { |
| 4422 | struct page_cgroup *pc; |
| 4423 | |
| 4424 | pc = lookup_page_cgroup(page); |
Johannes Weiner | cfa4494 | 2012-01-12 17:18:38 -0800 | [diff] [blame] | 4425 | /* |
| 4426 | * Can be NULL while feeding pages into the page allocator for |
| 4427 | * the first time, i.e. during boot or memory hotplug; |
| 4428 | * or when mem_cgroup_disabled(). |
| 4429 | */ |
Daisuke Nishimura | f212ad7 | 2011-03-23 16:42:25 -0700 | [diff] [blame] | 4430 | if (likely(pc) && PageCgroupUsed(pc)) |
| 4431 | return pc; |
| 4432 | return NULL; |
| 4433 | } |
| 4434 | |
| 4435 | bool mem_cgroup_bad_page_check(struct page *page) |
| 4436 | { |
| 4437 | if (mem_cgroup_disabled()) |
| 4438 | return false; |
| 4439 | |
| 4440 | return lookup_page_cgroup_used(page) != NULL; |
| 4441 | } |
| 4442 | |
| 4443 | void mem_cgroup_print_bad_page(struct page *page) |
| 4444 | { |
| 4445 | struct page_cgroup *pc; |
| 4446 | |
| 4447 | pc = lookup_page_cgroup_used(page); |
| 4448 | if (pc) { |
Andrew Morton | d045197 | 2013-02-22 16:32:06 -0800 | [diff] [blame] | 4449 | pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n", |
| 4450 | pc, pc->flags, pc->mem_cgroup); |
Daisuke Nishimura | f212ad7 | 2011-03-23 16:42:25 -0700 | [diff] [blame] | 4451 | } |
| 4452 | } |
| 4453 | #endif |
| 4454 | |
KOSAKI Motohiro | d38d2a7 | 2009-01-06 14:39:44 -0800 | [diff] [blame] | 4455 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4456 | unsigned long long val) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4457 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 4458 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4459 | u64 memswlimit, memlimit; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4460 | int ret = 0; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 4461 | int children = mem_cgroup_count_children(memcg); |
| 4462 | u64 curusage, oldusage; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4463 | int enlarge; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 4464 | |
| 4465 | /* |
| 4466 | * For keeping hierarchical_reclaim simple, how long we should retry |
| 4467 | * is depends on callers. We set our retry-count to be function |
| 4468 | * of # of children which we should visit in this loop. |
| 4469 | */ |
| 4470 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; |
| 4471 | |
| 4472 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4473 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4474 | enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4475 | while (retry_count) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4476 | if (signal_pending(current)) { |
| 4477 | ret = -EINTR; |
| 4478 | break; |
| 4479 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4480 | /* |
| 4481 | * Rather than hide all in some function, I do this in |
| 4482 | * open coded manner. You see what this really does. |
Wanpeng Li | aaad153 | 2012-07-31 16:43:23 -0700 | [diff] [blame] | 4483 | * We have to guarantee memcg->res.limit <= memcg->memsw.limit. |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4484 | */ |
| 4485 | mutex_lock(&set_limit_mutex); |
| 4486 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 4487 | if (memswlimit < val) { |
| 4488 | ret = -EINVAL; |
| 4489 | mutex_unlock(&set_limit_mutex); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4490 | break; |
| 4491 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4492 | |
| 4493 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 4494 | if (memlimit < val) |
| 4495 | enlarge = 1; |
| 4496 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4497 | ret = res_counter_set_limit(&memcg->res, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 4498 | if (!ret) { |
| 4499 | if (memswlimit == val) |
| 4500 | memcg->memsw_is_minimum = true; |
| 4501 | else |
| 4502 | memcg->memsw_is_minimum = false; |
| 4503 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4504 | mutex_unlock(&set_limit_mutex); |
| 4505 | |
| 4506 | if (!ret) |
| 4507 | break; |
| 4508 | |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 4509 | mem_cgroup_reclaim(memcg, GFP_KERNEL, |
| 4510 | MEM_CGROUP_RECLAIM_SHRINK); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 4511 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
| 4512 | /* Usage is reduced ? */ |
| 4513 | if (curusage >= oldusage) |
| 4514 | retry_count--; |
| 4515 | else |
| 4516 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4517 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4518 | if (!ret && enlarge) |
| 4519 | memcg_oom_recover(memcg); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 4520 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4521 | return ret; |
| 4522 | } |
| 4523 | |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 4524 | static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
| 4525 | unsigned long long val) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4526 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 4527 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4528 | u64 memlimit, memswlimit, oldusage, curusage; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 4529 | int children = mem_cgroup_count_children(memcg); |
| 4530 | int ret = -EBUSY; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4531 | int enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4532 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 4533 | /* see mem_cgroup_resize_res_limit */ |
| 4534 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; |
| 4535 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4536 | while (retry_count) { |
| 4537 | if (signal_pending(current)) { |
| 4538 | ret = -EINTR; |
| 4539 | break; |
| 4540 | } |
| 4541 | /* |
| 4542 | * Rather than hide all in some function, I do this in |
| 4543 | * open coded manner. You see what this really does. |
Wanpeng Li | aaad153 | 2012-07-31 16:43:23 -0700 | [diff] [blame] | 4544 | * We have to guarantee memcg->res.limit <= memcg->memsw.limit. |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4545 | */ |
| 4546 | mutex_lock(&set_limit_mutex); |
| 4547 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 4548 | if (memlimit > val) { |
| 4549 | ret = -EINVAL; |
| 4550 | mutex_unlock(&set_limit_mutex); |
| 4551 | break; |
| 4552 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4553 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 4554 | if (memswlimit < val) |
| 4555 | enlarge = 1; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4556 | ret = res_counter_set_limit(&memcg->memsw, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 4557 | if (!ret) { |
| 4558 | if (memlimit == val) |
| 4559 | memcg->memsw_is_minimum = true; |
| 4560 | else |
| 4561 | memcg->memsw_is_minimum = false; |
| 4562 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4563 | mutex_unlock(&set_limit_mutex); |
| 4564 | |
| 4565 | if (!ret) |
| 4566 | break; |
| 4567 | |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 4568 | mem_cgroup_reclaim(memcg, GFP_KERNEL, |
| 4569 | MEM_CGROUP_RECLAIM_NOSWAP | |
| 4570 | MEM_CGROUP_RECLAIM_SHRINK); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4571 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 4572 | /* Usage is reduced ? */ |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4573 | if (curusage >= oldusage) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4574 | retry_count--; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 4575 | else |
| 4576 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4577 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4578 | if (!ret && enlarge) |
| 4579 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4580 | return ret; |
| 4581 | } |
| 4582 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4583 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 4584 | gfp_t gfp_mask, |
| 4585 | unsigned long *total_scanned) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4586 | { |
| 4587 | unsigned long nr_reclaimed = 0; |
| 4588 | struct mem_cgroup_per_zone *mz, *next_mz = NULL; |
| 4589 | unsigned long reclaimed; |
| 4590 | int loop = 0; |
| 4591 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 4592 | unsigned long long excess; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 4593 | unsigned long nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4594 | |
| 4595 | if (order > 0) |
| 4596 | return 0; |
| 4597 | |
KOSAKI Motohiro | 00918b6 | 2010-08-10 18:03:05 -0700 | [diff] [blame] | 4598 | 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] | 4599 | /* |
| 4600 | * This loop can run a while, specially if mem_cgroup's continuously |
| 4601 | * keep exceeding their soft limit and putting the system under |
| 4602 | * pressure |
| 4603 | */ |
| 4604 | do { |
| 4605 | if (next_mz) |
| 4606 | mz = next_mz; |
| 4607 | else |
| 4608 | mz = mem_cgroup_largest_soft_limit_node(mctz); |
| 4609 | if (!mz) |
| 4610 | break; |
| 4611 | |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 4612 | nr_scanned = 0; |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 4613 | reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone, |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 4614 | gfp_mask, &nr_scanned); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4615 | nr_reclaimed += reclaimed; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 4616 | *total_scanned += nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4617 | spin_lock(&mctz->lock); |
| 4618 | |
| 4619 | /* |
| 4620 | * If we failed to reclaim anything from this memory cgroup |
| 4621 | * it is time to move on to the next cgroup |
| 4622 | */ |
| 4623 | next_mz = NULL; |
| 4624 | if (!reclaimed) { |
| 4625 | do { |
| 4626 | /* |
| 4627 | * Loop until we find yet another one. |
| 4628 | * |
| 4629 | * By the time we get the soft_limit lock |
| 4630 | * again, someone might have aded the |
| 4631 | * group back on the RB tree. Iterate to |
| 4632 | * make sure we get a different mem. |
| 4633 | * mem_cgroup_largest_soft_limit_node returns |
| 4634 | * NULL if no other cgroup is present on |
| 4635 | * the tree |
| 4636 | */ |
| 4637 | next_mz = |
| 4638 | __mem_cgroup_largest_soft_limit_node(mctz); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 4639 | if (next_mz == mz) |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 4640 | css_put(&next_mz->memcg->css); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 4641 | else /* next_mz == NULL or other memcg */ |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4642 | break; |
| 4643 | } while (1); |
| 4644 | } |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 4645 | __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz); |
| 4646 | excess = res_counter_soft_limit_excess(&mz->memcg->res); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4647 | /* |
| 4648 | * One school of thought says that we should not add |
| 4649 | * back the node to the tree if reclaim returns 0. |
| 4650 | * But our reclaim could return 0, simply because due |
| 4651 | * to priority we are exposing a smaller subset of |
| 4652 | * memory to reclaim from. Consider this as a longer |
| 4653 | * term TODO. |
| 4654 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 4655 | /* If excess == 0, no tree ops */ |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 4656 | __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4657 | spin_unlock(&mctz->lock); |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 4658 | css_put(&mz->memcg->css); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4659 | loop++; |
| 4660 | /* |
| 4661 | * Could not reclaim anything and there are no more |
| 4662 | * mem cgroups to try or we seem to be looping without |
| 4663 | * reclaiming anything. |
| 4664 | */ |
| 4665 | if (!nr_reclaimed && |
| 4666 | (next_mz == NULL || |
| 4667 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) |
| 4668 | break; |
| 4669 | } while (!nr_reclaimed); |
| 4670 | if (next_mz) |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 4671 | css_put(&next_mz->memcg->css); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4672 | return nr_reclaimed; |
| 4673 | } |
| 4674 | |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 4675 | /** |
| 4676 | * mem_cgroup_force_empty_list - clears LRU of a group |
| 4677 | * @memcg: group to clear |
| 4678 | * @node: NUMA node |
| 4679 | * @zid: zone id |
| 4680 | * @lru: lru to to clear |
| 4681 | * |
KAMEZAWA Hiroyuki | 3c935d1 | 2012-07-31 16:42:46 -0700 | [diff] [blame] | 4682 | * Traverse a specified page_cgroup list and try to drop them all. This doesn't |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 4683 | * reclaim the pages page themselves - pages are moved to the parent (or root) |
| 4684 | * group. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 4685 | */ |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 4686 | static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4687 | int node, int zid, enum lru_list lru) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 4688 | { |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 4689 | struct lruvec *lruvec; |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 4690 | unsigned long flags; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 4691 | struct list_head *list; |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 4692 | struct page *busy; |
| 4693 | struct zone *zone; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 4694 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4695 | zone = &NODE_DATA(node)->node_zones[zid]; |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 4696 | lruvec = mem_cgroup_zone_lruvec(zone, memcg); |
| 4697 | list = &lruvec->lists[lru]; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 4698 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4699 | busy = NULL; |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 4700 | do { |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 4701 | struct page_cgroup *pc; |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 4702 | struct page *page; |
| 4703 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4704 | spin_lock_irqsave(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4705 | if (list_empty(list)) { |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4706 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 4707 | break; |
| 4708 | } |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 4709 | page = list_entry(list->prev, struct page, lru); |
| 4710 | if (busy == page) { |
| 4711 | list_move(&page->lru, list); |
Thiago Farina | 648bcc7 | 2010-03-05 13:42:04 -0800 | [diff] [blame] | 4712 | busy = NULL; |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4713 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4714 | continue; |
| 4715 | } |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4716 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4717 | |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 4718 | pc = lookup_page_cgroup(page); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 4719 | |
KAMEZAWA Hiroyuki | 3c935d1 | 2012-07-31 16:42:46 -0700 | [diff] [blame] | 4720 | if (mem_cgroup_move_parent(page, pc, memcg)) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4721 | /* found lock contention or "pc" is obsolete. */ |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 4722 | busy = page; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4723 | cond_resched(); |
| 4724 | } else |
| 4725 | busy = NULL; |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 4726 | } while (!list_empty(list)); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 4727 | } |
| 4728 | |
| 4729 | /* |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 4730 | * make mem_cgroup's charge to be 0 if there is no task by moving |
| 4731 | * all the charges and pages to the parent. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 4732 | * This enables deleting this mem_cgroup. |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 4733 | * |
| 4734 | * Caller is responsible for holding css reference on the memcg. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 4735 | */ |
Michal Hocko | ab5196c | 2012-10-26 13:37:32 +0200 | [diff] [blame] | 4736 | static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 4737 | { |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 4738 | int node, zid; |
Glauber Costa | bea207c | 2012-12-18 14:22:11 -0800 | [diff] [blame] | 4739 | u64 usage; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 4740 | |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 4741 | do { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 4742 | /* This is for making all *used* pages to be on LRU. */ |
| 4743 | lru_add_drain_all(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4744 | drain_all_stock_sync(memcg); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4745 | mem_cgroup_start_move(memcg); |
Lai Jiangshan | 31aaea4 | 2012-12-12 13:51:27 -0800 | [diff] [blame] | 4746 | for_each_node_state(node, N_MEMORY) { |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 4747 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
Hugh Dickins | f156ab9 | 2012-03-21 16:34:19 -0700 | [diff] [blame] | 4748 | enum lru_list lru; |
| 4749 | for_each_lru(lru) { |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 4750 | mem_cgroup_force_empty_list(memcg, |
Hugh Dickins | f156ab9 | 2012-03-21 16:34:19 -0700 | [diff] [blame] | 4751 | node, zid, lru); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4752 | } |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4753 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4754 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4755 | mem_cgroup_end_move(memcg); |
| 4756 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 4757 | cond_resched(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4758 | |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 4759 | /* |
Glauber Costa | bea207c | 2012-12-18 14:22:11 -0800 | [diff] [blame] | 4760 | * Kernel memory may not necessarily be trackable to a specific |
| 4761 | * process. So they are not migrated, and therefore we can't |
| 4762 | * expect their value to drop to 0 here. |
| 4763 | * Having res filled up with kmem only is enough. |
| 4764 | * |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 4765 | * This is a safety check because mem_cgroup_force_empty_list |
| 4766 | * could have raced with mem_cgroup_replace_page_cache callers |
| 4767 | * so the lru seemed empty but the page could have been added |
| 4768 | * right after the check. RES_USAGE should be safe as we always |
| 4769 | * charge before adding to the LRU. |
| 4770 | */ |
Glauber Costa | bea207c | 2012-12-18 14:22:11 -0800 | [diff] [blame] | 4771 | usage = res_counter_read_u64(&memcg->res, RES_USAGE) - |
| 4772 | res_counter_read_u64(&memcg->kmem, RES_USAGE); |
| 4773 | } while (usage > 0); |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | /* |
Glauber Costa | b5f99b5 | 2013-02-22 16:34:53 -0800 | [diff] [blame] | 4777 | * This mainly exists for tests during the setting of set of use_hierarchy. |
| 4778 | * Since this is the very setting we are changing, the current hierarchy value |
| 4779 | * is meaningless |
| 4780 | */ |
| 4781 | static inline bool __memcg_has_children(struct mem_cgroup *memcg) |
| 4782 | { |
| 4783 | struct cgroup *pos; |
| 4784 | |
| 4785 | /* bounce at first found */ |
| 4786 | cgroup_for_each_child(pos, memcg->css.cgroup) |
| 4787 | return true; |
| 4788 | return false; |
| 4789 | } |
| 4790 | |
| 4791 | /* |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 4792 | * Must be called with memcg_create_mutex held, unless the cgroup is guaranteed |
| 4793 | * to be already dead (as in mem_cgroup_force_empty, for instance). This is |
Glauber Costa | b5f99b5 | 2013-02-22 16:34:53 -0800 | [diff] [blame] | 4794 | * from mem_cgroup_count_children(), in the sense that we don't really care how |
| 4795 | * many children we have; we only need to know if we have any. It also counts |
| 4796 | * any memcg without hierarchy as infertile. |
| 4797 | */ |
| 4798 | static inline bool memcg_has_children(struct mem_cgroup *memcg) |
| 4799 | { |
| 4800 | return memcg->use_hierarchy && __memcg_has_children(memcg); |
| 4801 | } |
| 4802 | |
| 4803 | /* |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 4804 | * Reclaims as many pages from the given memcg as possible and moves |
| 4805 | * the rest to the parent. |
| 4806 | * |
| 4807 | * Caller is responsible for holding css reference for memcg. |
| 4808 | */ |
| 4809 | static int mem_cgroup_force_empty(struct mem_cgroup *memcg) |
| 4810 | { |
| 4811 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
| 4812 | struct cgroup *cgrp = memcg->css.cgroup; |
| 4813 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4814 | /* returns EBUSY if there is a task or if we come here twice. */ |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 4815 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
| 4816 | return -EBUSY; |
| 4817 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4818 | /* we call try-to-free pages for make this cgroup empty */ |
| 4819 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4820 | /* try to free all pages in this cgroup */ |
Glauber Costa | 569530f | 2012-04-12 12:49:13 -0700 | [diff] [blame] | 4821 | while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4822 | int progress; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4823 | |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 4824 | if (signal_pending(current)) |
| 4825 | return -EINTR; |
| 4826 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4827 | progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL, |
Johannes Weiner | 185efc0 | 2011-09-14 16:21:58 -0700 | [diff] [blame] | 4828 | false); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4829 | if (!progress) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4830 | nr_retries--; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4831 | /* maybe some writeback is necessary */ |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 4832 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4833 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4834 | |
| 4835 | } |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4836 | lru_add_drain(); |
Michal Hocko | ab5196c | 2012-10-26 13:37:32 +0200 | [diff] [blame] | 4837 | mem_cgroup_reparent_charges(memcg); |
| 4838 | |
| 4839 | return 0; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 4840 | } |
| 4841 | |
Kirill A. Shutemov | 6bbda35 | 2012-05-29 15:06:55 -0700 | [diff] [blame] | 4842 | static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4843 | { |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 4844 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
| 4845 | int ret; |
| 4846 | |
Michal Hocko | d842301 | 2012-10-26 13:37:29 +0200 | [diff] [blame] | 4847 | if (mem_cgroup_is_root(memcg)) |
| 4848 | return -EINVAL; |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 4849 | css_get(&memcg->css); |
| 4850 | ret = mem_cgroup_force_empty(memcg); |
| 4851 | css_put(&memcg->css); |
| 4852 | |
| 4853 | return ret; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4857 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
| 4858 | { |
| 4859 | return mem_cgroup_from_cont(cont)->use_hierarchy; |
| 4860 | } |
| 4861 | |
| 4862 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, |
| 4863 | u64 val) |
| 4864 | { |
| 4865 | int retval = 0; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4866 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4867 | struct cgroup *parent = cont->parent; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4868 | struct mem_cgroup *parent_memcg = NULL; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4869 | |
| 4870 | if (parent) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4871 | parent_memcg = mem_cgroup_from_cont(parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4872 | |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 4873 | mutex_lock(&memcg_create_mutex); |
Glauber Costa | 567fb43 | 2012-07-31 16:43:07 -0700 | [diff] [blame] | 4874 | |
| 4875 | if (memcg->use_hierarchy == val) |
| 4876 | goto out; |
| 4877 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4878 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 4879 | * 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] | 4880 | * in the child subtrees. If it is unset, then the change can |
| 4881 | * occur, provided the current cgroup has no children. |
| 4882 | * |
| 4883 | * For the root cgroup, parent_mem is NULL, we allow value to be |
| 4884 | * set if there are no children. |
| 4885 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4886 | if ((!parent_memcg || !parent_memcg->use_hierarchy) && |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4887 | (val == 1 || val == 0)) { |
Glauber Costa | b5f99b5 | 2013-02-22 16:34:53 -0800 | [diff] [blame] | 4888 | if (!__memcg_has_children(memcg)) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4889 | memcg->use_hierarchy = val; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4890 | else |
| 4891 | retval = -EBUSY; |
| 4892 | } else |
| 4893 | retval = -EINVAL; |
Glauber Costa | 567fb43 | 2012-07-31 16:43:07 -0700 | [diff] [blame] | 4894 | |
| 4895 | out: |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 4896 | mutex_unlock(&memcg_create_mutex); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4897 | |
| 4898 | return retval; |
| 4899 | } |
| 4900 | |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 4901 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4902 | static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg, |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 4903 | enum mem_cgroup_stat_index idx) |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 4904 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4905 | struct mem_cgroup *iter; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 4906 | long val = 0; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 4907 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 4908 | /* Per-cpu values can be negative, use a signed accumulator */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4909 | for_each_mem_cgroup_tree(iter, memcg) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4910 | val += mem_cgroup_read_stat(iter, idx); |
| 4911 | |
| 4912 | if (val < 0) /* race ? */ |
| 4913 | val = 0; |
| 4914 | return val; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 4915 | } |
| 4916 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4917 | static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap) |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4918 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4919 | u64 val; |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4920 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4921 | if (!mem_cgroup_is_root(memcg)) { |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4922 | if (!swap) |
Glauber Costa | 65c64ce | 2011-12-22 01:02:27 +0000 | [diff] [blame] | 4923 | return res_counter_read_u64(&memcg->res, RES_USAGE); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4924 | else |
Glauber Costa | 65c64ce | 2011-12-22 01:02:27 +0000 | [diff] [blame] | 4925 | return res_counter_read_u64(&memcg->memsw, RES_USAGE); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4926 | } |
| 4927 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4928 | val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE); |
| 4929 | val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4930 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4931 | if (swap) |
Kamezawa Hiroyuki | bff6bb8 | 2012-07-31 16:41:38 -0700 | [diff] [blame] | 4932 | val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4933 | |
| 4934 | return val << PAGE_SHIFT; |
| 4935 | } |
| 4936 | |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4937 | static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft, |
| 4938 | struct file *file, char __user *buf, |
| 4939 | size_t nbytes, loff_t *ppos) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4940 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4941 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4942 | char str[64]; |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4943 | u64 val; |
Glauber Costa | 86ae53e | 2012-12-18 14:21:45 -0800 | [diff] [blame] | 4944 | int name, len; |
| 4945 | enum res_type type; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4946 | |
| 4947 | type = MEMFILE_TYPE(cft->private); |
| 4948 | name = MEMFILE_ATTR(cft->private); |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4949 | |
| 4950 | if (!do_swap_account && type == _MEMSWAP) |
| 4951 | return -EOPNOTSUPP; |
| 4952 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4953 | switch (type) { |
| 4954 | case _MEM: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4955 | if (name == RES_USAGE) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4956 | val = mem_cgroup_usage(memcg, false); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4957 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4958 | val = res_counter_read_u64(&memcg->res, name); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4959 | break; |
| 4960 | case _MEMSWAP: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4961 | if (name == RES_USAGE) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4962 | val = mem_cgroup_usage(memcg, true); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 4963 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4964 | val = res_counter_read_u64(&memcg->memsw, name); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4965 | break; |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 4966 | case _KMEM: |
| 4967 | val = res_counter_read_u64(&memcg->kmem, name); |
| 4968 | break; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4969 | default: |
| 4970 | BUG(); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4971 | } |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4972 | |
| 4973 | len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val); |
| 4974 | return simple_read_from_buffer(buf, nbytes, ppos, str, len); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4975 | } |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 4976 | |
| 4977 | static int memcg_update_kmem_limit(struct cgroup *cont, u64 val) |
| 4978 | { |
| 4979 | int ret = -EINVAL; |
| 4980 | #ifdef CONFIG_MEMCG_KMEM |
| 4981 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
| 4982 | /* |
| 4983 | * For simplicity, we won't allow this to be disabled. It also can't |
| 4984 | * be changed if the cgroup has children already, or if tasks had |
| 4985 | * already joined. |
| 4986 | * |
| 4987 | * If tasks join before we set the limit, a person looking at |
| 4988 | * kmem.usage_in_bytes will have no way to determine when it took |
| 4989 | * place, which makes the value quite meaningless. |
| 4990 | * |
| 4991 | * After it first became limited, changes in the value of the limit are |
| 4992 | * of course permitted. |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 4993 | */ |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 4994 | mutex_lock(&memcg_create_mutex); |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 4995 | mutex_lock(&set_limit_mutex); |
| 4996 | if (!memcg->kmem_account_flags && val != RESOURCE_MAX) { |
Glauber Costa | b5f99b5 | 2013-02-22 16:34:53 -0800 | [diff] [blame] | 4997 | if (cgroup_task_count(cont) || memcg_has_children(memcg)) { |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 4998 | ret = -EBUSY; |
| 4999 | goto out; |
| 5000 | } |
| 5001 | ret = res_counter_set_limit(&memcg->kmem, val); |
| 5002 | VM_BUG_ON(ret); |
| 5003 | |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 5004 | ret = memcg_update_cache_sizes(memcg); |
| 5005 | if (ret) { |
| 5006 | res_counter_set_limit(&memcg->kmem, RESOURCE_MAX); |
| 5007 | goto out; |
| 5008 | } |
Glauber Costa | 692e89a | 2013-02-22 16:34:56 -0800 | [diff] [blame] | 5009 | static_key_slow_inc(&memcg_kmem_enabled_key); |
| 5010 | /* |
| 5011 | * setting the active bit after the inc will guarantee no one |
| 5012 | * starts accounting before all call sites are patched |
| 5013 | */ |
| 5014 | memcg_kmem_set_active(memcg); |
| 5015 | |
Glauber Costa | 7de3768 | 2012-12-18 14:22:07 -0800 | [diff] [blame] | 5016 | /* |
| 5017 | * kmem charges can outlive the cgroup. In the case of slab |
| 5018 | * pages, for instance, a page contain objects from various |
| 5019 | * processes, so it is unfeasible to migrate them away. We |
| 5020 | * need to reference count the memcg because of that. |
| 5021 | */ |
| 5022 | mem_cgroup_get(memcg); |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5023 | } else |
| 5024 | ret = res_counter_set_limit(&memcg->kmem, val); |
| 5025 | out: |
| 5026 | mutex_unlock(&set_limit_mutex); |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 5027 | mutex_unlock(&memcg_create_mutex); |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5028 | #endif |
| 5029 | return ret; |
| 5030 | } |
| 5031 | |
Hugh Dickins | 6d043990 | 2013-02-22 16:35:50 -0800 | [diff] [blame] | 5032 | #ifdef CONFIG_MEMCG_KMEM |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 5033 | static int memcg_propagate_kmem(struct mem_cgroup *memcg) |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5034 | { |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 5035 | int ret = 0; |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5036 | struct mem_cgroup *parent = parent_mem_cgroup(memcg); |
| 5037 | if (!parent) |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 5038 | goto out; |
| 5039 | |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5040 | memcg->kmem_account_flags = parent->kmem_account_flags; |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 5041 | /* |
| 5042 | * When that happen, we need to disable the static branch only on those |
| 5043 | * memcgs that enabled it. To achieve this, we would be forced to |
| 5044 | * complicate the code by keeping track of which memcgs were the ones |
| 5045 | * that actually enabled limits, and which ones got it from its |
| 5046 | * parents. |
| 5047 | * |
| 5048 | * It is a lot simpler just to do static_key_slow_inc() on every child |
| 5049 | * that is accounted. |
| 5050 | */ |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 5051 | if (!memcg_kmem_is_active(memcg)) |
| 5052 | goto out; |
| 5053 | |
| 5054 | /* |
| 5055 | * destroy(), called if we fail, will issue static_key_slow_inc() and |
| 5056 | * mem_cgroup_put() if kmem is enabled. We have to either call them |
| 5057 | * unconditionally, or clear the KMEM_ACTIVE flag. I personally find |
| 5058 | * this more consistent, since it always leads to the same destroy path |
| 5059 | */ |
| 5060 | mem_cgroup_get(memcg); |
| 5061 | static_key_slow_inc(&memcg_kmem_enabled_key); |
| 5062 | |
| 5063 | mutex_lock(&set_limit_mutex); |
| 5064 | ret = memcg_update_cache_sizes(memcg); |
| 5065 | mutex_unlock(&set_limit_mutex); |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 5066 | out: |
| 5067 | return ret; |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5068 | } |
Hugh Dickins | 6d043990 | 2013-02-22 16:35:50 -0800 | [diff] [blame] | 5069 | #endif /* CONFIG_MEMCG_KMEM */ |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5070 | |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 5071 | /* |
| 5072 | * The user of this function is... |
| 5073 | * RES_LIMIT. |
| 5074 | */ |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 5075 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 5076 | const char *buffer) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5077 | { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 5078 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
Glauber Costa | 86ae53e | 2012-12-18 14:21:45 -0800 | [diff] [blame] | 5079 | enum res_type type; |
| 5080 | int name; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 5081 | unsigned long long val; |
| 5082 | int ret; |
| 5083 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5084 | type = MEMFILE_TYPE(cft->private); |
| 5085 | name = MEMFILE_ATTR(cft->private); |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5086 | |
| 5087 | if (!do_swap_account && type == _MEMSWAP) |
| 5088 | return -EOPNOTSUPP; |
| 5089 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5090 | switch (name) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 5091 | case RES_LIMIT: |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 5092 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
| 5093 | ret = -EINVAL; |
| 5094 | break; |
| 5095 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 5096 | /* This function does all necessary parse...reuse it */ |
| 5097 | ret = res_counter_memparse_write_strategy(buffer, &val); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5098 | if (ret) |
| 5099 | break; |
| 5100 | if (type == _MEM) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 5101 | ret = mem_cgroup_resize_limit(memcg, val); |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5102 | else if (type == _MEMSWAP) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5103 | ret = mem_cgroup_resize_memsw_limit(memcg, val); |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5104 | else if (type == _KMEM) |
| 5105 | ret = memcg_update_kmem_limit(cont, val); |
| 5106 | else |
| 5107 | return -EINVAL; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 5108 | break; |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 5109 | case RES_SOFT_LIMIT: |
| 5110 | ret = res_counter_memparse_write_strategy(buffer, &val); |
| 5111 | if (ret) |
| 5112 | break; |
| 5113 | /* |
| 5114 | * For memsw, soft limits are hard to implement in terms |
| 5115 | * of semantics, for now, we support soft limits for |
| 5116 | * control without swap |
| 5117 | */ |
| 5118 | if (type == _MEM) |
| 5119 | ret = res_counter_set_soft_limit(&memcg->res, val); |
| 5120 | else |
| 5121 | ret = -EINVAL; |
| 5122 | break; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 5123 | default: |
| 5124 | ret = -EINVAL; /* should be BUG() ? */ |
| 5125 | break; |
| 5126 | } |
| 5127 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5128 | } |
| 5129 | |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 5130 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
| 5131 | unsigned long long *mem_limit, unsigned long long *memsw_limit) |
| 5132 | { |
| 5133 | struct cgroup *cgroup; |
| 5134 | unsigned long long min_limit, min_memsw_limit, tmp; |
| 5135 | |
| 5136 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 5137 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 5138 | cgroup = memcg->css.cgroup; |
| 5139 | if (!memcg->use_hierarchy) |
| 5140 | goto out; |
| 5141 | |
| 5142 | while (cgroup->parent) { |
| 5143 | cgroup = cgroup->parent; |
| 5144 | memcg = mem_cgroup_from_cont(cgroup); |
| 5145 | if (!memcg->use_hierarchy) |
| 5146 | break; |
| 5147 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 5148 | min_limit = min(min_limit, tmp); |
| 5149 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 5150 | min_memsw_limit = min(min_memsw_limit, tmp); |
| 5151 | } |
| 5152 | out: |
| 5153 | *mem_limit = min_limit; |
| 5154 | *memsw_limit = min_memsw_limit; |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 5155 | } |
| 5156 | |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 5157 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 5158 | { |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5159 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
Glauber Costa | 86ae53e | 2012-12-18 14:21:45 -0800 | [diff] [blame] | 5160 | int name; |
| 5161 | enum res_type type; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 5162 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5163 | type = MEMFILE_TYPE(event); |
| 5164 | name = MEMFILE_ATTR(event); |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5165 | |
| 5166 | if (!do_swap_account && type == _MEMSWAP) |
| 5167 | return -EOPNOTSUPP; |
| 5168 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5169 | switch (name) { |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 5170 | case RES_MAX_USAGE: |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5171 | if (type == _MEM) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5172 | res_counter_reset_max(&memcg->res); |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5173 | else if (type == _MEMSWAP) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5174 | res_counter_reset_max(&memcg->memsw); |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5175 | else if (type == _KMEM) |
| 5176 | res_counter_reset_max(&memcg->kmem); |
| 5177 | else |
| 5178 | return -EINVAL; |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 5179 | break; |
| 5180 | case RES_FAILCNT: |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5181 | if (type == _MEM) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5182 | res_counter_reset_failcnt(&memcg->res); |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5183 | else if (type == _MEMSWAP) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5184 | res_counter_reset_failcnt(&memcg->memsw); |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5185 | else if (type == _KMEM) |
| 5186 | res_counter_reset_failcnt(&memcg->kmem); |
| 5187 | else |
| 5188 | return -EINVAL; |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 5189 | break; |
| 5190 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5191 | |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 5192 | return 0; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 5193 | } |
| 5194 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5195 | static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp, |
| 5196 | struct cftype *cft) |
| 5197 | { |
| 5198 | return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate; |
| 5199 | } |
| 5200 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5201 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5202 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 5203 | struct cftype *cft, u64 val) |
| 5204 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5205 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5206 | |
| 5207 | if (val >= (1 << NR_MOVE_TYPE)) |
| 5208 | return -EINVAL; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5209 | |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 5210 | /* |
| 5211 | * No kind of locking is needed in here, because ->can_attach() will |
| 5212 | * check this value once in the beginning of the process, and then carry |
| 5213 | * on with stale data. This means that changes to this value will only |
| 5214 | * affect task migrations starting after the change. |
| 5215 | */ |
| 5216 | memcg->move_charge_at_immigrate = val; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5217 | return 0; |
| 5218 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5219 | #else |
| 5220 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 5221 | struct cftype *cft, u64 val) |
| 5222 | { |
| 5223 | return -ENOSYS; |
| 5224 | } |
| 5225 | #endif |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5226 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5227 | #ifdef CONFIG_NUMA |
Wanpeng Li | ab21588 | 2012-07-31 16:43:09 -0700 | [diff] [blame] | 5228 | static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft, |
Johannes Weiner | fada52c | 2012-05-29 15:07:06 -0700 | [diff] [blame] | 5229 | struct seq_file *m) |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5230 | { |
| 5231 | int nid; |
| 5232 | unsigned long total_nr, file_nr, anon_nr, unevictable_nr; |
| 5233 | unsigned long node_nr; |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5234 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5235 | |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5236 | total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5237 | seq_printf(m, "total=%lu", total_nr); |
Lai Jiangshan | 31aaea4 | 2012-12-12 13:51:27 -0800 | [diff] [blame] | 5238 | for_each_node_state(nid, N_MEMORY) { |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5239 | node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5240 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 5241 | } |
| 5242 | seq_putc(m, '\n'); |
| 5243 | |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5244 | file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5245 | seq_printf(m, "file=%lu", file_nr); |
Lai Jiangshan | 31aaea4 | 2012-12-12 13:51:27 -0800 | [diff] [blame] | 5246 | for_each_node_state(nid, N_MEMORY) { |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5247 | node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 5248 | LRU_ALL_FILE); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5249 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 5250 | } |
| 5251 | seq_putc(m, '\n'); |
| 5252 | |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5253 | anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5254 | seq_printf(m, "anon=%lu", anon_nr); |
Lai Jiangshan | 31aaea4 | 2012-12-12 13:51:27 -0800 | [diff] [blame] | 5255 | for_each_node_state(nid, N_MEMORY) { |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5256 | node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 5257 | LRU_ALL_ANON); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5258 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 5259 | } |
| 5260 | seq_putc(m, '\n'); |
| 5261 | |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5262 | unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE)); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5263 | seq_printf(m, "unevictable=%lu", unevictable_nr); |
Lai Jiangshan | 31aaea4 | 2012-12-12 13:51:27 -0800 | [diff] [blame] | 5264 | for_each_node_state(nid, N_MEMORY) { |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5265 | node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 5266 | BIT(LRU_UNEVICTABLE)); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5267 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 5268 | } |
| 5269 | seq_putc(m, '\n'); |
| 5270 | return 0; |
| 5271 | } |
| 5272 | #endif /* CONFIG_NUMA */ |
| 5273 | |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 5274 | static inline void mem_cgroup_lru_names_not_uptodate(void) |
| 5275 | { |
| 5276 | BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS); |
| 5277 | } |
| 5278 | |
Wanpeng Li | ab21588 | 2012-07-31 16:43:09 -0700 | [diff] [blame] | 5279 | static int memcg_stat_show(struct cgroup *cont, struct cftype *cft, |
Johannes Weiner | 78ccf5b | 2012-05-29 15:07:06 -0700 | [diff] [blame] | 5280 | struct seq_file *m) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 5281 | { |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5282 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 5283 | struct mem_cgroup *mi; |
| 5284 | unsigned int i; |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 5285 | |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 5286 | for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) { |
Kamezawa Hiroyuki | bff6bb8 | 2012-07-31 16:41:38 -0700 | [diff] [blame] | 5287 | if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account) |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 5288 | continue; |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 5289 | seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i], |
| 5290 | mem_cgroup_read_stat(memcg, i) * PAGE_SIZE); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 5291 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5292 | |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 5293 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) |
| 5294 | seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i], |
| 5295 | mem_cgroup_read_events(memcg, i)); |
| 5296 | |
| 5297 | for (i = 0; i < NR_LRU_LISTS; i++) |
| 5298 | seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i], |
| 5299 | mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE); |
| 5300 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 5301 | /* Hierarchical information */ |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 5302 | { |
| 5303 | unsigned long long limit, memsw_limit; |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5304 | memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit); |
Johannes Weiner | 78ccf5b | 2012-05-29 15:07:06 -0700 | [diff] [blame] | 5305 | seq_printf(m, "hierarchical_memory_limit %llu\n", limit); |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 5306 | if (do_swap_account) |
Johannes Weiner | 78ccf5b | 2012-05-29 15:07:06 -0700 | [diff] [blame] | 5307 | seq_printf(m, "hierarchical_memsw_limit %llu\n", |
| 5308 | memsw_limit); |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 5309 | } |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 5310 | |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 5311 | for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) { |
| 5312 | long long val = 0; |
| 5313 | |
Kamezawa Hiroyuki | bff6bb8 | 2012-07-31 16:41:38 -0700 | [diff] [blame] | 5314 | if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account) |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 5315 | continue; |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 5316 | for_each_mem_cgroup_tree(mi, memcg) |
| 5317 | val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE; |
| 5318 | seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val); |
| 5319 | } |
| 5320 | |
| 5321 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) { |
| 5322 | unsigned long long val = 0; |
| 5323 | |
| 5324 | for_each_mem_cgroup_tree(mi, memcg) |
| 5325 | val += mem_cgroup_read_events(mi, i); |
| 5326 | seq_printf(m, "total_%s %llu\n", |
| 5327 | mem_cgroup_events_names[i], val); |
| 5328 | } |
| 5329 | |
| 5330 | for (i = 0; i < NR_LRU_LISTS; i++) { |
| 5331 | unsigned long long val = 0; |
| 5332 | |
| 5333 | for_each_mem_cgroup_tree(mi, memcg) |
| 5334 | val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE; |
| 5335 | seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 5336 | } |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 5337 | |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 5338 | #ifdef CONFIG_DEBUG_VM |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 5339 | { |
| 5340 | int nid, zid; |
| 5341 | struct mem_cgroup_per_zone *mz; |
Hugh Dickins | 89abfab | 2012-05-29 15:06:53 -0700 | [diff] [blame] | 5342 | struct zone_reclaim_stat *rstat; |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 5343 | unsigned long recent_rotated[2] = {0, 0}; |
| 5344 | unsigned long recent_scanned[2] = {0, 0}; |
| 5345 | |
| 5346 | for_each_online_node(nid) |
| 5347 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5348 | mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
Hugh Dickins | 89abfab | 2012-05-29 15:06:53 -0700 | [diff] [blame] | 5349 | rstat = &mz->lruvec.reclaim_stat; |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 5350 | |
Hugh Dickins | 89abfab | 2012-05-29 15:06:53 -0700 | [diff] [blame] | 5351 | recent_rotated[0] += rstat->recent_rotated[0]; |
| 5352 | recent_rotated[1] += rstat->recent_rotated[1]; |
| 5353 | recent_scanned[0] += rstat->recent_scanned[0]; |
| 5354 | recent_scanned[1] += rstat->recent_scanned[1]; |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 5355 | } |
Johannes Weiner | 78ccf5b | 2012-05-29 15:07:06 -0700 | [diff] [blame] | 5356 | seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]); |
| 5357 | seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]); |
| 5358 | seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]); |
| 5359 | seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]); |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 5360 | } |
| 5361 | #endif |
| 5362 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 5363 | return 0; |
| 5364 | } |
| 5365 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5366 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
| 5367 | { |
| 5368 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 5369 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 5370 | return mem_cgroup_swappiness(memcg); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5371 | } |
| 5372 | |
| 5373 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, |
| 5374 | u64 val) |
| 5375 | { |
| 5376 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 5377 | struct mem_cgroup *parent; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 5378 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5379 | if (val > 100) |
| 5380 | return -EINVAL; |
| 5381 | |
| 5382 | if (cgrp->parent == NULL) |
| 5383 | return -EINVAL; |
| 5384 | |
| 5385 | parent = mem_cgroup_from_cont(cgrp->parent); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 5386 | |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 5387 | mutex_lock(&memcg_create_mutex); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 5388 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5389 | /* If under hierarchy, only empty-root can set this value */ |
Glauber Costa | b5f99b5 | 2013-02-22 16:34:53 -0800 | [diff] [blame] | 5390 | if ((parent->use_hierarchy) || memcg_has_children(memcg)) { |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 5391 | mutex_unlock(&memcg_create_mutex); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5392 | return -EINVAL; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 5393 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5394 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5395 | memcg->swappiness = val; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5396 | |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 5397 | mutex_unlock(&memcg_create_mutex); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 5398 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5399 | return 0; |
| 5400 | } |
| 5401 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5402 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) |
| 5403 | { |
| 5404 | struct mem_cgroup_threshold_ary *t; |
| 5405 | u64 usage; |
| 5406 | int i; |
| 5407 | |
| 5408 | rcu_read_lock(); |
| 5409 | if (!swap) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5410 | t = rcu_dereference(memcg->thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5411 | else |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5412 | t = rcu_dereference(memcg->memsw_thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5413 | |
| 5414 | if (!t) |
| 5415 | goto unlock; |
| 5416 | |
| 5417 | usage = mem_cgroup_usage(memcg, swap); |
| 5418 | |
| 5419 | /* |
Sha Zhengju | 748dad3 | 2012-05-29 15:06:57 -0700 | [diff] [blame] | 5420 | * current_threshold points to threshold just below or equal to usage. |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5421 | * If it's not true, a threshold was crossed after last |
| 5422 | * call of __mem_cgroup_threshold(). |
| 5423 | */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 5424 | i = t->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5425 | |
| 5426 | /* |
| 5427 | * Iterate backward over array of thresholds starting from |
| 5428 | * current_threshold and check if a threshold is crossed. |
| 5429 | * If none of thresholds below usage is crossed, we read |
| 5430 | * only one element of the array here. |
| 5431 | */ |
| 5432 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) |
| 5433 | eventfd_signal(t->entries[i].eventfd, 1); |
| 5434 | |
| 5435 | /* i = current_threshold + 1 */ |
| 5436 | i++; |
| 5437 | |
| 5438 | /* |
| 5439 | * Iterate forward over array of thresholds starting from |
| 5440 | * current_threshold+1 and check if a threshold is crossed. |
| 5441 | * If none of thresholds above usage is crossed, we read |
| 5442 | * only one element of the array here. |
| 5443 | */ |
| 5444 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) |
| 5445 | eventfd_signal(t->entries[i].eventfd, 1); |
| 5446 | |
| 5447 | /* Update current_threshold */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 5448 | t->current_threshold = i - 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5449 | unlock: |
| 5450 | rcu_read_unlock(); |
| 5451 | } |
| 5452 | |
| 5453 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) |
| 5454 | { |
Kirill A. Shutemov | ad4ca5f | 2010-10-07 12:59:27 -0700 | [diff] [blame] | 5455 | while (memcg) { |
| 5456 | __mem_cgroup_threshold(memcg, false); |
| 5457 | if (do_swap_account) |
| 5458 | __mem_cgroup_threshold(memcg, true); |
| 5459 | |
| 5460 | memcg = parent_mem_cgroup(memcg); |
| 5461 | } |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5462 | } |
| 5463 | |
| 5464 | static int compare_thresholds(const void *a, const void *b) |
| 5465 | { |
| 5466 | const struct mem_cgroup_threshold *_a = a; |
| 5467 | const struct mem_cgroup_threshold *_b = b; |
| 5468 | |
| 5469 | return _a->threshold - _b->threshold; |
| 5470 | } |
| 5471 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5472 | static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5473 | { |
| 5474 | struct mem_cgroup_eventfd_list *ev; |
| 5475 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5476 | list_for_each_entry(ev, &memcg->oom_notify, list) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5477 | eventfd_signal(ev->eventfd, 1); |
| 5478 | return 0; |
| 5479 | } |
| 5480 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5481 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5482 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 5483 | struct mem_cgroup *iter; |
| 5484 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5485 | for_each_mem_cgroup_tree(iter, memcg) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 5486 | mem_cgroup_oom_notify_cb(iter); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5487 | } |
| 5488 | |
| 5489 | static int mem_cgroup_usage_register_event(struct cgroup *cgrp, |
| 5490 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5491 | { |
| 5492 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5493 | struct mem_cgroup_thresholds *thresholds; |
| 5494 | struct mem_cgroup_threshold_ary *new; |
Glauber Costa | 86ae53e | 2012-12-18 14:21:45 -0800 | [diff] [blame] | 5495 | enum res_type type = MEMFILE_TYPE(cft->private); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5496 | u64 threshold, usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5497 | int i, size, ret; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5498 | |
| 5499 | ret = res_counter_memparse_write_strategy(args, &threshold); |
| 5500 | if (ret) |
| 5501 | return ret; |
| 5502 | |
| 5503 | mutex_lock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5504 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5505 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5506 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5507 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5508 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5509 | else |
| 5510 | BUG(); |
| 5511 | |
| 5512 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 5513 | |
| 5514 | /* Check if a threshold crossed before adding a new one */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5515 | if (thresholds->primary) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5516 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 5517 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5518 | size = thresholds->primary ? thresholds->primary->size + 1 : 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5519 | |
| 5520 | /* Allocate memory for new array of thresholds */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5521 | new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5522 | GFP_KERNEL); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5523 | if (!new) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5524 | ret = -ENOMEM; |
| 5525 | goto unlock; |
| 5526 | } |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5527 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5528 | |
| 5529 | /* Copy thresholds (if any) to new array */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5530 | if (thresholds->primary) { |
| 5531 | memcpy(new->entries, thresholds->primary->entries, (size - 1) * |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5532 | sizeof(struct mem_cgroup_threshold)); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5533 | } |
| 5534 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5535 | /* Add new threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5536 | new->entries[size - 1].eventfd = eventfd; |
| 5537 | new->entries[size - 1].threshold = threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5538 | |
| 5539 | /* Sort thresholds. Registering of new threshold isn't time-critical */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5540 | sort(new->entries, size, sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5541 | compare_thresholds, NULL); |
| 5542 | |
| 5543 | /* Find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5544 | new->current_threshold = -1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5545 | for (i = 0; i < size; i++) { |
Sha Zhengju | 748dad3 | 2012-05-29 15:06:57 -0700 | [diff] [blame] | 5546 | if (new->entries[i].threshold <= usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5547 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5548 | * new->current_threshold will not be used until |
| 5549 | * rcu_assign_pointer(), so it's safe to increment |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5550 | * it here. |
| 5551 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5552 | ++new->current_threshold; |
Sha Zhengju | 748dad3 | 2012-05-29 15:06:57 -0700 | [diff] [blame] | 5553 | } else |
| 5554 | break; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5555 | } |
| 5556 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5557 | /* Free old spare buffer and save old primary buffer as spare */ |
| 5558 | kfree(thresholds->spare); |
| 5559 | thresholds->spare = thresholds->primary; |
| 5560 | |
| 5561 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5562 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 5563 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5564 | synchronize_rcu(); |
| 5565 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5566 | unlock: |
| 5567 | mutex_unlock(&memcg->thresholds_lock); |
| 5568 | |
| 5569 | return ret; |
| 5570 | } |
| 5571 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 5572 | static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5573 | struct cftype *cft, struct eventfd_ctx *eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5574 | { |
| 5575 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5576 | struct mem_cgroup_thresholds *thresholds; |
| 5577 | struct mem_cgroup_threshold_ary *new; |
Glauber Costa | 86ae53e | 2012-12-18 14:21:45 -0800 | [diff] [blame] | 5578 | enum res_type type = MEMFILE_TYPE(cft->private); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5579 | u64 usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5580 | int i, j, size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5581 | |
| 5582 | mutex_lock(&memcg->thresholds_lock); |
| 5583 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5584 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5585 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5586 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5587 | else |
| 5588 | BUG(); |
| 5589 | |
Anton Vorontsov | 371528c | 2012-02-24 05:14:46 +0400 | [diff] [blame] | 5590 | if (!thresholds->primary) |
| 5591 | goto unlock; |
| 5592 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5593 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 5594 | |
| 5595 | /* Check if a threshold crossed before removing */ |
| 5596 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 5597 | |
| 5598 | /* Calculate new number of threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5599 | size = 0; |
| 5600 | for (i = 0; i < thresholds->primary->size; i++) { |
| 5601 | if (thresholds->primary->entries[i].eventfd != eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5602 | size++; |
| 5603 | } |
| 5604 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5605 | new = thresholds->spare; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 5606 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5607 | /* Set thresholds array to NULL if we don't have thresholds */ |
| 5608 | if (!size) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5609 | kfree(new); |
| 5610 | new = NULL; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 5611 | goto swap_buffers; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5612 | } |
| 5613 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5614 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5615 | |
| 5616 | /* Copy thresholds and find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5617 | new->current_threshold = -1; |
| 5618 | for (i = 0, j = 0; i < thresholds->primary->size; i++) { |
| 5619 | if (thresholds->primary->entries[i].eventfd == eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5620 | continue; |
| 5621 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5622 | new->entries[j] = thresholds->primary->entries[i]; |
Sha Zhengju | 748dad3 | 2012-05-29 15:06:57 -0700 | [diff] [blame] | 5623 | if (new->entries[j].threshold <= usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5624 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5625 | * new->current_threshold will not be used |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5626 | * until rcu_assign_pointer(), so it's safe to increment |
| 5627 | * it here. |
| 5628 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5629 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5630 | } |
| 5631 | j++; |
| 5632 | } |
| 5633 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 5634 | swap_buffers: |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5635 | /* Swap primary and spare array */ |
| 5636 | thresholds->spare = thresholds->primary; |
Sha Zhengju | 8c75776 | 2012-05-10 13:01:45 -0700 | [diff] [blame] | 5637 | /* If all events are unregistered, free the spare array */ |
| 5638 | if (!new) { |
| 5639 | kfree(thresholds->spare); |
| 5640 | thresholds->spare = NULL; |
| 5641 | } |
| 5642 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 5643 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5644 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 5645 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5646 | synchronize_rcu(); |
Anton Vorontsov | 371528c | 2012-02-24 05:14:46 +0400 | [diff] [blame] | 5647 | unlock: |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5648 | mutex_unlock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 5649 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 5650 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5651 | static int mem_cgroup_oom_register_event(struct cgroup *cgrp, |
| 5652 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
| 5653 | { |
| 5654 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 5655 | struct mem_cgroup_eventfd_list *event; |
Glauber Costa | 86ae53e | 2012-12-18 14:21:45 -0800 | [diff] [blame] | 5656 | enum res_type type = MEMFILE_TYPE(cft->private); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5657 | |
| 5658 | BUG_ON(type != _OOM_TYPE); |
| 5659 | event = kmalloc(sizeof(*event), GFP_KERNEL); |
| 5660 | if (!event) |
| 5661 | return -ENOMEM; |
| 5662 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 5663 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5664 | |
| 5665 | event->eventfd = eventfd; |
| 5666 | list_add(&event->list, &memcg->oom_notify); |
| 5667 | |
| 5668 | /* already in OOM ? */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 5669 | if (atomic_read(&memcg->under_oom)) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5670 | eventfd_signal(eventfd, 1); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 5671 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5672 | |
| 5673 | return 0; |
| 5674 | } |
| 5675 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 5676 | static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5677 | struct cftype *cft, struct eventfd_ctx *eventfd) |
| 5678 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5679 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5680 | struct mem_cgroup_eventfd_list *ev, *tmp; |
Glauber Costa | 86ae53e | 2012-12-18 14:21:45 -0800 | [diff] [blame] | 5681 | enum res_type type = MEMFILE_TYPE(cft->private); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5682 | |
| 5683 | BUG_ON(type != _OOM_TYPE); |
| 5684 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 5685 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5686 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5687 | list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) { |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5688 | if (ev->eventfd == eventfd) { |
| 5689 | list_del(&ev->list); |
| 5690 | kfree(ev); |
| 5691 | } |
| 5692 | } |
| 5693 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 5694 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5695 | } |
| 5696 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 5697 | static int mem_cgroup_oom_control_read(struct cgroup *cgrp, |
| 5698 | struct cftype *cft, struct cgroup_map_cb *cb) |
| 5699 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5700 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 5701 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5702 | cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 5703 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5704 | if (atomic_read(&memcg->under_oom)) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 5705 | cb->fill(cb, "under_oom", 1); |
| 5706 | else |
| 5707 | cb->fill(cb, "under_oom", 0); |
| 5708 | return 0; |
| 5709 | } |
| 5710 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 5711 | static int mem_cgroup_oom_control_write(struct cgroup *cgrp, |
| 5712 | struct cftype *cft, u64 val) |
| 5713 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5714 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 5715 | struct mem_cgroup *parent; |
| 5716 | |
| 5717 | /* cannot set to root cgroup and only 0 and 1 are allowed */ |
| 5718 | if (!cgrp->parent || !((val == 0) || (val == 1))) |
| 5719 | return -EINVAL; |
| 5720 | |
| 5721 | parent = mem_cgroup_from_cont(cgrp->parent); |
| 5722 | |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 5723 | mutex_lock(&memcg_create_mutex); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 5724 | /* oom-kill-disable is a flag for subhierarchy. */ |
Glauber Costa | b5f99b5 | 2013-02-22 16:34:53 -0800 | [diff] [blame] | 5725 | if ((parent->use_hierarchy) || memcg_has_children(memcg)) { |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 5726 | mutex_unlock(&memcg_create_mutex); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 5727 | return -EINVAL; |
| 5728 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5729 | memcg->oom_kill_disable = val; |
KAMEZAWA Hiroyuki | 4d845eb | 2010-06-29 15:05:18 -0700 | [diff] [blame] | 5730 | if (!val) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5731 | memcg_oom_recover(memcg); |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 5732 | mutex_unlock(&memcg_create_mutex); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 5733 | return 0; |
| 5734 | } |
| 5735 | |
Andrew Morton | c255a45 | 2012-07-31 16:43:02 -0700 | [diff] [blame] | 5736 | #ifdef CONFIG_MEMCG_KMEM |
Glauber Costa | cbe128e3 | 2012-04-09 19:36:34 -0300 | [diff] [blame] | 5737 | static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss) |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 5738 | { |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 5739 | int ret; |
| 5740 | |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 5741 | memcg->kmemcg_id = -1; |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 5742 | ret = memcg_propagate_kmem(memcg); |
| 5743 | if (ret) |
| 5744 | return ret; |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 5745 | |
Glauber Costa | 1d62e43 | 2012-04-09 19:36:33 -0300 | [diff] [blame] | 5746 | return mem_cgroup_sockets_init(memcg, ss); |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 5747 | }; |
| 5748 | |
Glauber Costa | 1d62e43 | 2012-04-09 19:36:33 -0300 | [diff] [blame] | 5749 | static void kmem_cgroup_destroy(struct mem_cgroup *memcg) |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 5750 | { |
Glauber Costa | 1d62e43 | 2012-04-09 19:36:33 -0300 | [diff] [blame] | 5751 | mem_cgroup_sockets_destroy(memcg); |
Glauber Costa | 7de3768 | 2012-12-18 14:22:07 -0800 | [diff] [blame] | 5752 | |
| 5753 | memcg_kmem_mark_dead(memcg); |
| 5754 | |
| 5755 | if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0) |
| 5756 | return; |
| 5757 | |
| 5758 | /* |
| 5759 | * Charges already down to 0, undo mem_cgroup_get() done in the charge |
| 5760 | * path here, being careful not to race with memcg_uncharge_kmem: it is |
| 5761 | * possible that the charges went down to 0 between mark_dead and the |
| 5762 | * res_counter read, so in that case, we don't need the put |
| 5763 | */ |
| 5764 | if (memcg_kmem_test_and_clear_dead(memcg)) |
| 5765 | mem_cgroup_put(memcg); |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 5766 | } |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 5767 | #else |
Glauber Costa | cbe128e3 | 2012-04-09 19:36:34 -0300 | [diff] [blame] | 5768 | static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss) |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 5769 | { |
| 5770 | return 0; |
| 5771 | } |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 5772 | |
Glauber Costa | 1d62e43 | 2012-04-09 19:36:33 -0300 | [diff] [blame] | 5773 | static void kmem_cgroup_destroy(struct mem_cgroup *memcg) |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 5774 | { |
| 5775 | } |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 5776 | #endif |
| 5777 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5778 | static struct cftype mem_cgroup_files[] = { |
| 5779 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 5780 | .name = "usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5781 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5782 | .read = mem_cgroup_read, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5783 | .register_event = mem_cgroup_usage_register_event, |
| 5784 | .unregister_event = mem_cgroup_usage_unregister_event, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5785 | }, |
| 5786 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 5787 | .name = "max_usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5788 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 5789 | .trigger = mem_cgroup_reset, |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5790 | .read = mem_cgroup_read, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 5791 | }, |
| 5792 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 5793 | .name = "limit_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5794 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 5795 | .write_string = mem_cgroup_write, |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5796 | .read = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5797 | }, |
| 5798 | { |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 5799 | .name = "soft_limit_in_bytes", |
| 5800 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), |
| 5801 | .write_string = mem_cgroup_write, |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5802 | .read = mem_cgroup_read, |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 5803 | }, |
| 5804 | { |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5805 | .name = "failcnt", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5806 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 5807 | .trigger = mem_cgroup_reset, |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5808 | .read = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5809 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 5810 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 5811 | .name = "stat", |
Wanpeng Li | ab21588 | 2012-07-31 16:43:09 -0700 | [diff] [blame] | 5812 | .read_seq_string = memcg_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 5813 | }, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 5814 | { |
| 5815 | .name = "force_empty", |
| 5816 | .trigger = mem_cgroup_force_empty_write, |
| 5817 | }, |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5818 | { |
| 5819 | .name = "use_hierarchy", |
| 5820 | .write_u64 = mem_cgroup_hierarchy_write, |
| 5821 | .read_u64 = mem_cgroup_hierarchy_read, |
| 5822 | }, |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5823 | { |
| 5824 | .name = "swappiness", |
| 5825 | .read_u64 = mem_cgroup_swappiness_read, |
| 5826 | .write_u64 = mem_cgroup_swappiness_write, |
| 5827 | }, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5828 | { |
| 5829 | .name = "move_charge_at_immigrate", |
| 5830 | .read_u64 = mem_cgroup_move_charge_read, |
| 5831 | .write_u64 = mem_cgroup_move_charge_write, |
| 5832 | }, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5833 | { |
| 5834 | .name = "oom_control", |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 5835 | .read_map = mem_cgroup_oom_control_read, |
| 5836 | .write_u64 = mem_cgroup_oom_control_write, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5837 | .register_event = mem_cgroup_oom_register_event, |
| 5838 | .unregister_event = mem_cgroup_oom_unregister_event, |
| 5839 | .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL), |
| 5840 | }, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5841 | #ifdef CONFIG_NUMA |
| 5842 | { |
| 5843 | .name = "numa_stat", |
Wanpeng Li | ab21588 | 2012-07-31 16:43:09 -0700 | [diff] [blame] | 5844 | .read_seq_string = memcg_numa_stat_show, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5845 | }, |
| 5846 | #endif |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5847 | #ifdef CONFIG_MEMCG_KMEM |
| 5848 | { |
| 5849 | .name = "kmem.limit_in_bytes", |
| 5850 | .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT), |
| 5851 | .write_string = mem_cgroup_write, |
| 5852 | .read = mem_cgroup_read, |
| 5853 | }, |
| 5854 | { |
| 5855 | .name = "kmem.usage_in_bytes", |
| 5856 | .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE), |
| 5857 | .read = mem_cgroup_read, |
| 5858 | }, |
| 5859 | { |
| 5860 | .name = "kmem.failcnt", |
| 5861 | .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT), |
| 5862 | .trigger = mem_cgroup_reset, |
| 5863 | .read = mem_cgroup_read, |
| 5864 | }, |
| 5865 | { |
| 5866 | .name = "kmem.max_usage_in_bytes", |
| 5867 | .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE), |
| 5868 | .trigger = mem_cgroup_reset, |
| 5869 | .read = mem_cgroup_read, |
| 5870 | }, |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 5871 | #ifdef CONFIG_SLABINFO |
| 5872 | { |
| 5873 | .name = "kmem.slabinfo", |
| 5874 | .read_seq_string = mem_cgroup_slabinfo_read, |
| 5875 | }, |
| 5876 | #endif |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5877 | #endif |
Tejun Heo | 6bc1034 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5878 | { }, /* terminate */ |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5879 | }; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5880 | |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 5881 | #ifdef CONFIG_MEMCG_SWAP |
| 5882 | static struct cftype memsw_cgroup_files[] = { |
| 5883 | { |
| 5884 | .name = "memsw.usage_in_bytes", |
| 5885 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), |
| 5886 | .read = mem_cgroup_read, |
| 5887 | .register_event = mem_cgroup_usage_register_event, |
| 5888 | .unregister_event = mem_cgroup_usage_unregister_event, |
| 5889 | }, |
| 5890 | { |
| 5891 | .name = "memsw.max_usage_in_bytes", |
| 5892 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), |
| 5893 | .trigger = mem_cgroup_reset, |
| 5894 | .read = mem_cgroup_read, |
| 5895 | }, |
| 5896 | { |
| 5897 | .name = "memsw.limit_in_bytes", |
| 5898 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), |
| 5899 | .write_string = mem_cgroup_write, |
| 5900 | .read = mem_cgroup_read, |
| 5901 | }, |
| 5902 | { |
| 5903 | .name = "memsw.failcnt", |
| 5904 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), |
| 5905 | .trigger = mem_cgroup_reset, |
| 5906 | .read = mem_cgroup_read, |
| 5907 | }, |
| 5908 | { }, /* terminate */ |
| 5909 | }; |
| 5910 | #endif |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5911 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5912 | { |
| 5913 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5914 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 5915 | int zone, tmp = node; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5916 | /* |
| 5917 | * This routine is called against possible nodes. |
| 5918 | * But it's BUG to call kmalloc() against offline node. |
| 5919 | * |
| 5920 | * TODO: this routine can waste much memory for nodes which will |
| 5921 | * never be onlined. It's better to use memory hotplug callback |
| 5922 | * function. |
| 5923 | */ |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 5924 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 5925 | tmp = -1; |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 5926 | pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5927 | if (!pn) |
| 5928 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5929 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5930 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 5931 | mz = &pn->zoneinfo[zone]; |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 5932 | lruvec_init(&mz->lruvec); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5933 | mz->usage_in_excess = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 5934 | mz->on_tree = false; |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5935 | mz->memcg = memcg; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5936 | } |
Igor Mammedov | 0a619e5 | 2011-11-02 13:38:21 -0700 | [diff] [blame] | 5937 | memcg->info.nodeinfo[node] = pn; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5938 | return 0; |
| 5939 | } |
| 5940 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5941 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node) |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5942 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5943 | kfree(memcg->info.nodeinfo[node]); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5944 | } |
| 5945 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5946 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 5947 | { |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5948 | struct mem_cgroup *memcg; |
Glauber Costa | 45cf7eb | 2013-02-22 16:34:49 -0800 | [diff] [blame] | 5949 | size_t size = memcg_size(); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5950 | |
Glauber Costa | 45cf7eb | 2013-02-22 16:34:49 -0800 | [diff] [blame] | 5951 | /* Can be very big if nr_node_ids is very big */ |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 5952 | if (size < PAGE_SIZE) |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5953 | memcg = kzalloc(size, GFP_KERNEL); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5954 | else |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5955 | memcg = vzalloc(size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5956 | |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5957 | if (!memcg) |
Dan Carpenter | e7bbcdf | 2010-03-23 13:35:12 -0700 | [diff] [blame] | 5958 | return NULL; |
| 5959 | |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5960 | memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu); |
| 5961 | if (!memcg->stat) |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 5962 | goto out_free; |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5963 | spin_lock_init(&memcg->pcp_counter_lock); |
| 5964 | return memcg; |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 5965 | |
| 5966 | out_free: |
| 5967 | if (size < PAGE_SIZE) |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5968 | kfree(memcg); |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 5969 | else |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5970 | vfree(memcg); |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 5971 | return NULL; |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5972 | } |
| 5973 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5974 | /* |
Glauber Costa | c8b2a36 | 2012-12-18 14:22:13 -0800 | [diff] [blame] | 5975 | * At destroying mem_cgroup, references from swap_cgroup can remain. |
| 5976 | * (scanning all at force_empty is too costly...) |
| 5977 | * |
| 5978 | * Instead of clearing all references at force_empty, we remember |
| 5979 | * the number of reference from swap_cgroup and free mem_cgroup when |
| 5980 | * it goes down to 0. |
| 5981 | * |
| 5982 | * Removal of cgroup itself succeeds regardless of refs from swap. |
Hugh Dickins | 59927fb | 2012-03-15 15:17:07 -0700 | [diff] [blame] | 5983 | */ |
Glauber Costa | c8b2a36 | 2012-12-18 14:22:13 -0800 | [diff] [blame] | 5984 | |
| 5985 | static void __mem_cgroup_free(struct mem_cgroup *memcg) |
Hugh Dickins | 59927fb | 2012-03-15 15:17:07 -0700 | [diff] [blame] | 5986 | { |
Glauber Costa | c8b2a36 | 2012-12-18 14:22:13 -0800 | [diff] [blame] | 5987 | int node; |
Glauber Costa | 45cf7eb | 2013-02-22 16:34:49 -0800 | [diff] [blame] | 5988 | size_t size = memcg_size(); |
Hugh Dickins | 59927fb | 2012-03-15 15:17:07 -0700 | [diff] [blame] | 5989 | |
Glauber Costa | c8b2a36 | 2012-12-18 14:22:13 -0800 | [diff] [blame] | 5990 | mem_cgroup_remove_from_trees(memcg); |
| 5991 | free_css_id(&mem_cgroup_subsys, &memcg->css); |
| 5992 | |
| 5993 | for_each_node(node) |
| 5994 | free_mem_cgroup_per_zone_info(memcg, node); |
| 5995 | |
| 5996 | free_percpu(memcg->stat); |
| 5997 | |
Glauber Costa | 3f13461 | 2012-05-29 15:07:11 -0700 | [diff] [blame] | 5998 | /* |
| 5999 | * We need to make sure that (at least for now), the jump label |
| 6000 | * destruction code runs outside of the cgroup lock. This is because |
| 6001 | * get_online_cpus(), which is called from the static_branch update, |
| 6002 | * can't be called inside the cgroup_lock. cpusets are the ones |
| 6003 | * enforcing this dependency, so if they ever change, we might as well. |
| 6004 | * |
| 6005 | * schedule_work() will guarantee this happens. Be careful if you need |
| 6006 | * to move this code around, and make sure it is outside |
| 6007 | * the cgroup_lock. |
| 6008 | */ |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 6009 | disarm_static_keys(memcg); |
Glauber Costa | 3afe36b | 2012-05-29 15:07:10 -0700 | [diff] [blame] | 6010 | if (size < PAGE_SIZE) |
| 6011 | kfree(memcg); |
| 6012 | else |
| 6013 | vfree(memcg); |
Hugh Dickins | 59927fb | 2012-03-15 15:17:07 -0700 | [diff] [blame] | 6014 | } |
Glauber Costa | 3afe36b | 2012-05-29 15:07:10 -0700 | [diff] [blame] | 6015 | |
Glauber Costa | c8b2a36 | 2012-12-18 14:22:13 -0800 | [diff] [blame] | 6016 | |
| 6017 | /* |
| 6018 | * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU, |
| 6019 | * but in process context. The work_freeing structure is overlaid |
| 6020 | * on the rcu_freeing structure, which itself is overlaid on memsw. |
| 6021 | */ |
| 6022 | static void free_work(struct work_struct *work) |
| 6023 | { |
| 6024 | struct mem_cgroup *memcg; |
| 6025 | |
| 6026 | memcg = container_of(work, struct mem_cgroup, work_freeing); |
| 6027 | __mem_cgroup_free(memcg); |
| 6028 | } |
| 6029 | |
Glauber Costa | 3afe36b | 2012-05-29 15:07:10 -0700 | [diff] [blame] | 6030 | static void free_rcu(struct rcu_head *rcu_head) |
Hugh Dickins | 59927fb | 2012-03-15 15:17:07 -0700 | [diff] [blame] | 6031 | { |
| 6032 | struct mem_cgroup *memcg; |
| 6033 | |
| 6034 | memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing); |
Glauber Costa | 3afe36b | 2012-05-29 15:07:10 -0700 | [diff] [blame] | 6035 | INIT_WORK(&memcg->work_freeing, free_work); |
Hugh Dickins | 59927fb | 2012-03-15 15:17:07 -0700 | [diff] [blame] | 6036 | schedule_work(&memcg->work_freeing); |
| 6037 | } |
| 6038 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6039 | static void mem_cgroup_get(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 6040 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6041 | atomic_inc(&memcg->refcnt); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 6042 | } |
| 6043 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6044 | static void __mem_cgroup_put(struct mem_cgroup *memcg, int count) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 6045 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6046 | if (atomic_sub_and_test(count, &memcg->refcnt)) { |
| 6047 | struct mem_cgroup *parent = parent_mem_cgroup(memcg); |
Glauber Costa | c8b2a36 | 2012-12-18 14:22:13 -0800 | [diff] [blame] | 6048 | call_rcu(&memcg->rcu_freeing, free_rcu); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 6049 | if (parent) |
| 6050 | mem_cgroup_put(parent); |
| 6051 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 6052 | } |
| 6053 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6054 | static void mem_cgroup_put(struct mem_cgroup *memcg) |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 6055 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6056 | __mem_cgroup_put(memcg, 1); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 6057 | } |
| 6058 | |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 6059 | /* |
| 6060 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. |
| 6061 | */ |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 6062 | struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg) |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 6063 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6064 | if (!memcg->res.parent) |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 6065 | return NULL; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6066 | return mem_cgroup_from_res_counter(memcg->res.parent, res); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 6067 | } |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 6068 | EXPORT_SYMBOL(parent_mem_cgroup); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 6069 | |
Michal Hocko | 8787a1d | 2013-02-22 16:35:39 -0800 | [diff] [blame] | 6070 | static void __init mem_cgroup_soft_limit_tree_init(void) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 6071 | { |
| 6072 | struct mem_cgroup_tree_per_node *rtpn; |
| 6073 | struct mem_cgroup_tree_per_zone *rtpz; |
| 6074 | int tmp, node, zone; |
| 6075 | |
Bob Liu | 3ed28fa | 2012-01-12 17:19:04 -0800 | [diff] [blame] | 6076 | for_each_node(node) { |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 6077 | tmp = node; |
| 6078 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 6079 | tmp = -1; |
| 6080 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp); |
Michal Hocko | 8787a1d | 2013-02-22 16:35:39 -0800 | [diff] [blame] | 6081 | BUG_ON(!rtpn); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 6082 | |
| 6083 | soft_limit_tree.rb_tree_per_node[node] = rtpn; |
| 6084 | |
| 6085 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 6086 | rtpz = &rtpn->rb_tree_per_zone[zone]; |
| 6087 | rtpz->rb_root = RB_ROOT; |
| 6088 | spin_lock_init(&rtpz->lock); |
| 6089 | } |
| 6090 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 6091 | } |
| 6092 | |
Li Zefan | 0eb253e | 2009-01-15 13:51:25 -0800 | [diff] [blame] | 6093 | static struct cgroup_subsys_state * __ref |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 6094 | mem_cgroup_css_alloc(struct cgroup *cont) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 6095 | { |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 6096 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 6097 | long error = -ENOMEM; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 6098 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 6099 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6100 | memcg = mem_cgroup_alloc(); |
| 6101 | if (!memcg) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 6102 | return ERR_PTR(error); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 6103 | |
Bob Liu | 3ed28fa | 2012-01-12 17:19:04 -0800 | [diff] [blame] | 6104 | for_each_node(node) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6105 | if (alloc_mem_cgroup_per_zone_info(memcg, node)) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 6106 | goto free_out; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 6107 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 6108 | /* root ? */ |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 6109 | if (cont->parent == NULL) { |
Hillf Danton | a41c58a | 2011-12-19 17:11:57 -0800 | [diff] [blame] | 6110 | root_mem_cgroup = memcg; |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 6111 | res_counter_init(&memcg->res, NULL); |
| 6112 | res_counter_init(&memcg->memsw, NULL); |
| 6113 | res_counter_init(&memcg->kmem, NULL); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 6114 | } |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 6115 | |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 6116 | memcg->last_scanned_node = MAX_NUMNODES; |
| 6117 | INIT_LIST_HEAD(&memcg->oom_notify); |
| 6118 | atomic_set(&memcg->refcnt, 1); |
| 6119 | memcg->move_charge_at_immigrate = 0; |
| 6120 | mutex_init(&memcg->thresholds_lock); |
| 6121 | spin_lock_init(&memcg->move_lock); |
| 6122 | |
| 6123 | return &memcg->css; |
| 6124 | |
| 6125 | free_out: |
| 6126 | __mem_cgroup_free(memcg); |
| 6127 | return ERR_PTR(error); |
| 6128 | } |
| 6129 | |
| 6130 | static int |
| 6131 | mem_cgroup_css_online(struct cgroup *cont) |
| 6132 | { |
| 6133 | struct mem_cgroup *memcg, *parent; |
| 6134 | int error = 0; |
| 6135 | |
| 6136 | if (!cont->parent) |
| 6137 | return 0; |
| 6138 | |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 6139 | mutex_lock(&memcg_create_mutex); |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 6140 | memcg = mem_cgroup_from_cont(cont); |
| 6141 | parent = mem_cgroup_from_cont(cont->parent); |
| 6142 | |
| 6143 | memcg->use_hierarchy = parent->use_hierarchy; |
| 6144 | memcg->oom_kill_disable = parent->oom_kill_disable; |
| 6145 | memcg->swappiness = mem_cgroup_swappiness(parent); |
| 6146 | |
| 6147 | if (parent->use_hierarchy) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6148 | res_counter_init(&memcg->res, &parent->res); |
| 6149 | res_counter_init(&memcg->memsw, &parent->memsw); |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 6150 | res_counter_init(&memcg->kmem, &parent->kmem); |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 6151 | |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 6152 | /* |
| 6153 | * We increment refcnt of the parent to ensure that we can |
| 6154 | * safely access it on res_counter_charge/uncharge. |
| 6155 | * This refcnt will be decremented when freeing this |
| 6156 | * mem_cgroup(see mem_cgroup_put). |
| 6157 | */ |
| 6158 | mem_cgroup_get(parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 6159 | } else { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6160 | res_counter_init(&memcg->res, NULL); |
| 6161 | res_counter_init(&memcg->memsw, NULL); |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 6162 | res_counter_init(&memcg->kmem, NULL); |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 6163 | /* |
| 6164 | * Deeper hierachy with use_hierarchy == false doesn't make |
| 6165 | * much sense so let cgroup subsystem know about this |
| 6166 | * unfortunate state in our controller. |
| 6167 | */ |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 6168 | if (parent != root_mem_cgroup) |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 6169 | mem_cgroup_subsys.broken_hierarchy = true; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 6170 | } |
Glauber Costa | cbe128e3 | 2012-04-09 19:36:34 -0300 | [diff] [blame] | 6171 | |
| 6172 | error = memcg_init_kmem(memcg, &mem_cgroup_subsys); |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 6173 | mutex_unlock(&memcg_create_mutex); |
Glauber Costa | cbe128e3 | 2012-04-09 19:36:34 -0300 | [diff] [blame] | 6174 | if (error) { |
| 6175 | /* |
| 6176 | * We call put now because our (and parent's) refcnts |
| 6177 | * are already in place. mem_cgroup_put() will internally |
| 6178 | * call __mem_cgroup_free, so return directly |
| 6179 | */ |
| 6180 | mem_cgroup_put(memcg); |
Glauber Costa | e4715f0 | 2013-02-22 16:34:57 -0800 | [diff] [blame] | 6181 | if (parent->use_hierarchy) |
| 6182 | mem_cgroup_put(parent); |
Glauber Costa | cbe128e3 | 2012-04-09 19:36:34 -0300 | [diff] [blame] | 6183 | } |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 6184 | return error; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 6185 | } |
| 6186 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 6187 | static void mem_cgroup_css_offline(struct cgroup *cont) |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 6188 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6189 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 6190 | |
Michal Hocko | ab5196c | 2012-10-26 13:37:32 +0200 | [diff] [blame] | 6191 | mem_cgroup_reparent_charges(memcg); |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 6192 | mem_cgroup_destroy_all_caches(memcg); |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 6193 | } |
| 6194 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 6195 | static void mem_cgroup_css_free(struct cgroup *cont) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 6196 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6197 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 6198 | |
Glauber Costa | 1d62e43 | 2012-04-09 19:36:33 -0300 | [diff] [blame] | 6199 | kmem_cgroup_destroy(memcg); |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 6200 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6201 | mem_cgroup_put(memcg); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 6202 | } |
| 6203 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6204 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6205 | /* Handlers for move charge at task migration. */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6206 | #define PRECHARGE_COUNT_AT_ONCE 256 |
| 6207 | static int mem_cgroup_do_precharge(unsigned long count) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6208 | { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6209 | int ret = 0; |
| 6210 | int batch_count = PRECHARGE_COUNT_AT_ONCE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6211 | struct mem_cgroup *memcg = mc.to; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6212 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6213 | if (mem_cgroup_is_root(memcg)) { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6214 | mc.precharge += count; |
| 6215 | /* we don't need css_get for root */ |
| 6216 | return ret; |
| 6217 | } |
| 6218 | /* try to charge at once */ |
| 6219 | if (count > 1) { |
| 6220 | struct res_counter *dummy; |
| 6221 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6222 | * "memcg" cannot be under rmdir() because we've already checked |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6223 | * by cgroup_lock_live_cgroup() that it is not removed and we |
| 6224 | * are still under the same cgroup_mutex. So we can postpone |
| 6225 | * css_get(). |
| 6226 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6227 | if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy)) |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6228 | goto one_by_one; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6229 | if (do_swap_account && res_counter_charge(&memcg->memsw, |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6230 | PAGE_SIZE * count, &dummy)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6231 | res_counter_uncharge(&memcg->res, PAGE_SIZE * count); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6232 | goto one_by_one; |
| 6233 | } |
| 6234 | mc.precharge += count; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6235 | return ret; |
| 6236 | } |
| 6237 | one_by_one: |
| 6238 | /* fall back to one by one charge */ |
| 6239 | while (count--) { |
| 6240 | if (signal_pending(current)) { |
| 6241 | ret = -EINTR; |
| 6242 | break; |
| 6243 | } |
| 6244 | if (!batch_count--) { |
| 6245 | batch_count = PRECHARGE_COUNT_AT_ONCE; |
| 6246 | cond_resched(); |
| 6247 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6248 | ret = __mem_cgroup_try_charge(NULL, |
| 6249 | GFP_KERNEL, 1, &memcg, false); |
KAMEZAWA Hiroyuki | 38c5d72 | 2012-01-12 17:19:01 -0800 | [diff] [blame] | 6250 | if (ret) |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6251 | /* mem_cgroup_clear_mc() will do uncharge later */ |
KAMEZAWA Hiroyuki | 38c5d72 | 2012-01-12 17:19:01 -0800 | [diff] [blame] | 6252 | return ret; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6253 | mc.precharge++; |
| 6254 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6255 | return ret; |
| 6256 | } |
| 6257 | |
| 6258 | /** |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 6259 | * get_mctgt_type - get target type of moving charge |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6260 | * @vma: the vma the pte to be checked belongs |
| 6261 | * @addr: the address corresponding to the pte to be checked |
| 6262 | * @ptent: the pte to be checked |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6263 | * @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] | 6264 | * |
| 6265 | * Returns |
| 6266 | * 0(MC_TARGET_NONE): if the pte is not a target for move charge. |
| 6267 | * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for |
| 6268 | * move charge. if @target is not NULL, the page is stored in target->page |
| 6269 | * with extra refcnt got(Callers should handle it). |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6270 | * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a |
| 6271 | * target for charge migration. if @target is not NULL, the entry is stored |
| 6272 | * in target->ent. |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6273 | * |
| 6274 | * Called with pte lock held. |
| 6275 | */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6276 | union mc_target { |
| 6277 | struct page *page; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6278 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6279 | }; |
| 6280 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6281 | enum mc_target_type { |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 6282 | MC_TARGET_NONE = 0, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6283 | MC_TARGET_PAGE, |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6284 | MC_TARGET_SWAP, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6285 | }; |
| 6286 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 6287 | static struct page *mc_handle_present_pte(struct vm_area_struct *vma, |
| 6288 | unsigned long addr, pte_t ptent) |
| 6289 | { |
| 6290 | struct page *page = vm_normal_page(vma, addr, ptent); |
| 6291 | |
| 6292 | if (!page || !page_mapped(page)) |
| 6293 | return NULL; |
| 6294 | if (PageAnon(page)) { |
| 6295 | /* we don't move shared anon */ |
KAMEZAWA Hiroyuki | 4b91355 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 6296 | if (!move_anon()) |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 6297 | return NULL; |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 6298 | } else if (!move_file()) |
| 6299 | /* we ignore mapcount for file pages */ |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 6300 | return NULL; |
| 6301 | if (!get_page_unless_zero(page)) |
| 6302 | return NULL; |
| 6303 | |
| 6304 | return page; |
| 6305 | } |
| 6306 | |
KAMEZAWA Hiroyuki | 4b91355 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 6307 | #ifdef CONFIG_SWAP |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 6308 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
| 6309 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 6310 | { |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 6311 | struct page *page = NULL; |
| 6312 | swp_entry_t ent = pte_to_swp_entry(ptent); |
| 6313 | |
| 6314 | if (!move_anon() || non_swap_entry(ent)) |
| 6315 | return NULL; |
KAMEZAWA Hiroyuki | 4b91355 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 6316 | /* |
| 6317 | * Because lookup_swap_cache() updates some statistics counter, |
| 6318 | * we call find_get_page() with swapper_space directly. |
| 6319 | */ |
Shaohua Li | 33806f0 | 2013-02-22 16:34:37 -0800 | [diff] [blame] | 6320 | page = find_get_page(swap_address_space(ent), ent.val); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 6321 | if (do_swap_account) |
| 6322 | entry->val = ent.val; |
| 6323 | |
| 6324 | return page; |
| 6325 | } |
KAMEZAWA Hiroyuki | 4b91355 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 6326 | #else |
| 6327 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
| 6328 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 6329 | { |
| 6330 | return NULL; |
| 6331 | } |
| 6332 | #endif |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 6333 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 6334 | static struct page *mc_handle_file_pte(struct vm_area_struct *vma, |
| 6335 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 6336 | { |
| 6337 | struct page *page = NULL; |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 6338 | struct address_space *mapping; |
| 6339 | pgoff_t pgoff; |
| 6340 | |
| 6341 | if (!vma->vm_file) /* anonymous vma */ |
| 6342 | return NULL; |
| 6343 | if (!move_file()) |
| 6344 | return NULL; |
| 6345 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 6346 | mapping = vma->vm_file->f_mapping; |
| 6347 | if (pte_none(ptent)) |
| 6348 | pgoff = linear_page_index(vma, addr); |
| 6349 | else /* pte_file(ptent) is true */ |
| 6350 | pgoff = pte_to_pgoff(ptent); |
| 6351 | |
| 6352 | /* page is moved even if it's not RSS of this task(page-faulted). */ |
Hugh Dickins | aa3b189 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 6353 | page = find_get_page(mapping, pgoff); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 6354 | |
Hugh Dickins | aa3b189 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 6355 | #ifdef CONFIG_SWAP |
| 6356 | /* shmem/tmpfs may report page out on swap: account for that too. */ |
| 6357 | if (radix_tree_exceptional_entry(page)) { |
| 6358 | swp_entry_t swap = radix_to_swp_entry(page); |
| 6359 | if (do_swap_account) |
| 6360 | *entry = swap; |
Shaohua Li | 33806f0 | 2013-02-22 16:34:37 -0800 | [diff] [blame] | 6361 | page = find_get_page(swap_address_space(swap), swap.val); |
Hugh Dickins | aa3b189 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 6362 | } |
| 6363 | #endif |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 6364 | return page; |
| 6365 | } |
| 6366 | |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 6367 | static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6368 | unsigned long addr, pte_t ptent, union mc_target *target) |
| 6369 | { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6370 | struct page *page = NULL; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6371 | struct page_cgroup *pc; |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 6372 | enum mc_target_type ret = MC_TARGET_NONE; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6373 | swp_entry_t ent = { .val = 0 }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6374 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 6375 | if (pte_present(ptent)) |
| 6376 | page = mc_handle_present_pte(vma, addr, ptent); |
| 6377 | else if (is_swap_pte(ptent)) |
| 6378 | page = mc_handle_swap_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 6379 | else if (pte_none(ptent) || pte_file(ptent)) |
| 6380 | page = mc_handle_file_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 6381 | |
| 6382 | if (!page && !ent.val) |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 6383 | return ret; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6384 | if (page) { |
| 6385 | pc = lookup_page_cgroup(page); |
| 6386 | /* |
| 6387 | * Do only loose check w/o page_cgroup lock. |
| 6388 | * mem_cgroup_move_account() checks the pc is valid or not under |
| 6389 | * the lock. |
| 6390 | */ |
| 6391 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { |
| 6392 | ret = MC_TARGET_PAGE; |
| 6393 | if (target) |
| 6394 | target->page = page; |
| 6395 | } |
| 6396 | if (!ret || !target) |
| 6397 | put_page(page); |
| 6398 | } |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 6399 | /* There is a swap entry and a page doesn't exist or isn't charged */ |
| 6400 | if (ent.val && !ret && |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 6401 | css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 6402 | ret = MC_TARGET_SWAP; |
| 6403 | if (target) |
| 6404 | target->ent = ent; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6405 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6406 | return ret; |
| 6407 | } |
| 6408 | |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6409 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 6410 | /* |
| 6411 | * We don't consider swapping or file mapped pages because THP does not |
| 6412 | * support them for now. |
| 6413 | * Caller should make sure that pmd_trans_huge(pmd) is true. |
| 6414 | */ |
| 6415 | static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma, |
| 6416 | unsigned long addr, pmd_t pmd, union mc_target *target) |
| 6417 | { |
| 6418 | struct page *page = NULL; |
| 6419 | struct page_cgroup *pc; |
| 6420 | enum mc_target_type ret = MC_TARGET_NONE; |
| 6421 | |
| 6422 | page = pmd_page(pmd); |
| 6423 | VM_BUG_ON(!page || !PageHead(page)); |
| 6424 | if (!move_anon()) |
| 6425 | return ret; |
| 6426 | pc = lookup_page_cgroup(page); |
| 6427 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { |
| 6428 | ret = MC_TARGET_PAGE; |
| 6429 | if (target) { |
| 6430 | get_page(page); |
| 6431 | target->page = page; |
| 6432 | } |
| 6433 | } |
| 6434 | return ret; |
| 6435 | } |
| 6436 | #else |
| 6437 | static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma, |
| 6438 | unsigned long addr, pmd_t pmd, union mc_target *target) |
| 6439 | { |
| 6440 | return MC_TARGET_NONE; |
| 6441 | } |
| 6442 | #endif |
| 6443 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6444 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, |
| 6445 | unsigned long addr, unsigned long end, |
| 6446 | struct mm_walk *walk) |
| 6447 | { |
| 6448 | struct vm_area_struct *vma = walk->private; |
| 6449 | pte_t *pte; |
| 6450 | spinlock_t *ptl; |
| 6451 | |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6452 | if (pmd_trans_huge_lock(pmd, vma) == 1) { |
| 6453 | if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE) |
| 6454 | mc.precharge += HPAGE_PMD_NR; |
| 6455 | spin_unlock(&vma->vm_mm->page_table_lock); |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 6456 | return 0; |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6457 | } |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 6458 | |
Andrea Arcangeli | 45f83ce | 2012-03-28 14:42:40 -0700 | [diff] [blame] | 6459 | if (pmd_trans_unstable(pmd)) |
| 6460 | return 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6461 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 6462 | for (; addr != end; pte++, addr += PAGE_SIZE) |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 6463 | if (get_mctgt_type(vma, addr, *pte, NULL)) |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6464 | mc.precharge++; /* increment precharge temporarily */ |
| 6465 | pte_unmap_unlock(pte - 1, ptl); |
| 6466 | cond_resched(); |
| 6467 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6468 | return 0; |
| 6469 | } |
| 6470 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6471 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
| 6472 | { |
| 6473 | unsigned long precharge; |
| 6474 | struct vm_area_struct *vma; |
| 6475 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6476 | down_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6477 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 6478 | struct mm_walk mem_cgroup_count_precharge_walk = { |
| 6479 | .pmd_entry = mem_cgroup_count_precharge_pte_range, |
| 6480 | .mm = mm, |
| 6481 | .private = vma, |
| 6482 | }; |
| 6483 | if (is_vm_hugetlb_page(vma)) |
| 6484 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6485 | walk_page_range(vma->vm_start, vma->vm_end, |
| 6486 | &mem_cgroup_count_precharge_walk); |
| 6487 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6488 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6489 | |
| 6490 | precharge = mc.precharge; |
| 6491 | mc.precharge = 0; |
| 6492 | |
| 6493 | return precharge; |
| 6494 | } |
| 6495 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6496 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
| 6497 | { |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6498 | unsigned long precharge = mem_cgroup_count_precharge(mm); |
| 6499 | |
| 6500 | VM_BUG_ON(mc.moving_task); |
| 6501 | mc.moving_task = current; |
| 6502 | return mem_cgroup_do_precharge(precharge); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6503 | } |
| 6504 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6505 | /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */ |
| 6506 | static void __mem_cgroup_clear_mc(void) |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6507 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 6508 | struct mem_cgroup *from = mc.from; |
| 6509 | struct mem_cgroup *to = mc.to; |
| 6510 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6511 | /* we must uncharge all the leftover precharges from mc.to */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6512 | if (mc.precharge) { |
| 6513 | __mem_cgroup_cancel_charge(mc.to, mc.precharge); |
| 6514 | mc.precharge = 0; |
| 6515 | } |
| 6516 | /* |
| 6517 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so |
| 6518 | * we must uncharge here. |
| 6519 | */ |
| 6520 | if (mc.moved_charge) { |
| 6521 | __mem_cgroup_cancel_charge(mc.from, mc.moved_charge); |
| 6522 | mc.moved_charge = 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6523 | } |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 6524 | /* we must fixup refcnts and charges */ |
| 6525 | if (mc.moved_swap) { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 6526 | /* uncharge swap account from the old cgroup */ |
| 6527 | if (!mem_cgroup_is_root(mc.from)) |
| 6528 | res_counter_uncharge(&mc.from->memsw, |
| 6529 | PAGE_SIZE * mc.moved_swap); |
| 6530 | __mem_cgroup_put(mc.from, mc.moved_swap); |
| 6531 | |
| 6532 | if (!mem_cgroup_is_root(mc.to)) { |
| 6533 | /* |
| 6534 | * we charged both to->res and to->memsw, so we should |
| 6535 | * uncharge to->res. |
| 6536 | */ |
| 6537 | res_counter_uncharge(&mc.to->res, |
| 6538 | PAGE_SIZE * mc.moved_swap); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 6539 | } |
| 6540 | /* we've already done mem_cgroup_get(mc.to) */ |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 6541 | mc.moved_swap = 0; |
| 6542 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6543 | memcg_oom_recover(from); |
| 6544 | memcg_oom_recover(to); |
| 6545 | wake_up_all(&mc.waitq); |
| 6546 | } |
| 6547 | |
| 6548 | static void mem_cgroup_clear_mc(void) |
| 6549 | { |
| 6550 | struct mem_cgroup *from = mc.from; |
| 6551 | |
| 6552 | /* |
| 6553 | * we must clear moving_task before waking up waiters at the end of |
| 6554 | * task migration. |
| 6555 | */ |
| 6556 | mc.moving_task = NULL; |
| 6557 | __mem_cgroup_clear_mc(); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 6558 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6559 | mc.from = NULL; |
| 6560 | mc.to = NULL; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 6561 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 6562 | mem_cgroup_end_move(from); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6563 | } |
| 6564 | |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 6565 | static int mem_cgroup_can_attach(struct cgroup *cgroup, |
| 6566 | struct cgroup_taskset *tset) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6567 | { |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 6568 | struct task_struct *p = cgroup_taskset_first(tset); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6569 | int ret = 0; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6570 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup); |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 6571 | unsigned long move_charge_at_immigrate; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6572 | |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 6573 | /* |
| 6574 | * We are now commited to this value whatever it is. Changes in this |
| 6575 | * tunable will only affect upcoming migrations, not the current one. |
| 6576 | * So we need to save it, and keep it going. |
| 6577 | */ |
| 6578 | move_charge_at_immigrate = memcg->move_charge_at_immigrate; |
| 6579 | if (move_charge_at_immigrate) { |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6580 | struct mm_struct *mm; |
| 6581 | struct mem_cgroup *from = mem_cgroup_from_task(p); |
| 6582 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6583 | VM_BUG_ON(from == memcg); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6584 | |
| 6585 | mm = get_task_mm(p); |
| 6586 | if (!mm) |
| 6587 | return 0; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6588 | /* We move charges only when we move a owner of the mm */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6589 | if (mm->owner == p) { |
| 6590 | VM_BUG_ON(mc.from); |
| 6591 | VM_BUG_ON(mc.to); |
| 6592 | VM_BUG_ON(mc.precharge); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6593 | VM_BUG_ON(mc.moved_charge); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 6594 | VM_BUG_ON(mc.moved_swap); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 6595 | mem_cgroup_start_move(from); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 6596 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6597 | mc.from = from; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 6598 | mc.to = memcg; |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 6599 | mc.immigrate_flags = move_charge_at_immigrate; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 6600 | spin_unlock(&mc.lock); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6601 | /* We set mc.moving_task later */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6602 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6603 | ret = mem_cgroup_precharge_mc(mm); |
| 6604 | if (ret) |
| 6605 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6606 | } |
| 6607 | mmput(mm); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6608 | } |
| 6609 | return ret; |
| 6610 | } |
| 6611 | |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 6612 | static void mem_cgroup_cancel_attach(struct cgroup *cgroup, |
| 6613 | struct cgroup_taskset *tset) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6614 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6615 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6616 | } |
| 6617 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6618 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
| 6619 | unsigned long addr, unsigned long end, |
| 6620 | struct mm_walk *walk) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6621 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6622 | int ret = 0; |
| 6623 | struct vm_area_struct *vma = walk->private; |
| 6624 | pte_t *pte; |
| 6625 | spinlock_t *ptl; |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6626 | enum mc_target_type target_type; |
| 6627 | union mc_target target; |
| 6628 | struct page *page; |
| 6629 | struct page_cgroup *pc; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6630 | |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6631 | /* |
| 6632 | * We don't take compound_lock() here but no race with splitting thp |
| 6633 | * happens because: |
| 6634 | * - if pmd_trans_huge_lock() returns 1, the relevant thp is not |
| 6635 | * under splitting, which means there's no concurrent thp split, |
| 6636 | * - if another thread runs into split_huge_page() just after we |
| 6637 | * entered this if-block, the thread must wait for page table lock |
| 6638 | * to be unlocked in __split_huge_page_splitting(), where the main |
| 6639 | * part of thp split is not executed yet. |
| 6640 | */ |
| 6641 | if (pmd_trans_huge_lock(pmd, vma) == 1) { |
Hugh Dickins | 62ade86 | 2012-05-18 11:28:34 -0700 | [diff] [blame] | 6642 | if (mc.precharge < HPAGE_PMD_NR) { |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6643 | spin_unlock(&vma->vm_mm->page_table_lock); |
| 6644 | return 0; |
| 6645 | } |
| 6646 | target_type = get_mctgt_type_thp(vma, addr, *pmd, &target); |
| 6647 | if (target_type == MC_TARGET_PAGE) { |
| 6648 | page = target.page; |
| 6649 | if (!isolate_lru_page(page)) { |
| 6650 | pc = lookup_page_cgroup(page); |
| 6651 | if (!mem_cgroup_move_account(page, HPAGE_PMD_NR, |
KAMEZAWA Hiroyuki | 2f3479b | 2012-05-29 15:07:04 -0700 | [diff] [blame] | 6652 | pc, mc.from, mc.to)) { |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6653 | mc.precharge -= HPAGE_PMD_NR; |
| 6654 | mc.moved_charge += HPAGE_PMD_NR; |
| 6655 | } |
| 6656 | putback_lru_page(page); |
| 6657 | } |
| 6658 | put_page(page); |
| 6659 | } |
| 6660 | spin_unlock(&vma->vm_mm->page_table_lock); |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 6661 | return 0; |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6662 | } |
| 6663 | |
Andrea Arcangeli | 45f83ce | 2012-03-28 14:42:40 -0700 | [diff] [blame] | 6664 | if (pmd_trans_unstable(pmd)) |
| 6665 | return 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6666 | retry: |
| 6667 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 6668 | for (; addr != end; addr += PAGE_SIZE) { |
| 6669 | pte_t ptent = *(pte++); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6670 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6671 | |
| 6672 | if (!mc.precharge) |
| 6673 | break; |
| 6674 | |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 6675 | switch (get_mctgt_type(vma, addr, ptent, &target)) { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6676 | case MC_TARGET_PAGE: |
| 6677 | page = target.page; |
| 6678 | if (isolate_lru_page(page)) |
| 6679 | goto put; |
| 6680 | pc = lookup_page_cgroup(page); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 6681 | if (!mem_cgroup_move_account(page, 1, pc, |
KAMEZAWA Hiroyuki | 2f3479b | 2012-05-29 15:07:04 -0700 | [diff] [blame] | 6682 | mc.from, mc.to)) { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6683 | mc.precharge--; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6684 | /* we uncharge from mc.from later. */ |
| 6685 | mc.moved_charge++; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6686 | } |
| 6687 | putback_lru_page(page); |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 6688 | put: /* get_mctgt_type() gets the page */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6689 | put_page(page); |
| 6690 | break; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6691 | case MC_TARGET_SWAP: |
| 6692 | ent = target.ent; |
Hugh Dickins | e91cbb4 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 6693 | if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6694 | mc.precharge--; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 6695 | /* we fixup refcnts and charges later. */ |
| 6696 | mc.moved_swap++; |
| 6697 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6698 | break; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6699 | default: |
| 6700 | break; |
| 6701 | } |
| 6702 | } |
| 6703 | pte_unmap_unlock(pte - 1, ptl); |
| 6704 | cond_resched(); |
| 6705 | |
| 6706 | if (addr != end) { |
| 6707 | /* |
| 6708 | * We have consumed all precharges we got in can_attach(). |
| 6709 | * We try charge one by one, but don't do any additional |
| 6710 | * charges to mc.to if we have failed in charge once in attach() |
| 6711 | * phase. |
| 6712 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6713 | ret = mem_cgroup_do_precharge(1); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6714 | if (!ret) |
| 6715 | goto retry; |
| 6716 | } |
| 6717 | |
| 6718 | return ret; |
| 6719 | } |
| 6720 | |
| 6721 | static void mem_cgroup_move_charge(struct mm_struct *mm) |
| 6722 | { |
| 6723 | struct vm_area_struct *vma; |
| 6724 | |
| 6725 | lru_add_drain_all(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6726 | retry: |
| 6727 | if (unlikely(!down_read_trylock(&mm->mmap_sem))) { |
| 6728 | /* |
| 6729 | * Someone who are holding the mmap_sem might be waiting in |
| 6730 | * waitq. So we cancel all extra charges, wake up all waiters, |
| 6731 | * and retry. Because we cancel precharges, we might not be able |
| 6732 | * to move enough charges, but moving charge is a best-effort |
| 6733 | * feature anyway, so it wouldn't be a big problem. |
| 6734 | */ |
| 6735 | __mem_cgroup_clear_mc(); |
| 6736 | cond_resched(); |
| 6737 | goto retry; |
| 6738 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6739 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 6740 | int ret; |
| 6741 | struct mm_walk mem_cgroup_move_charge_walk = { |
| 6742 | .pmd_entry = mem_cgroup_move_charge_pte_range, |
| 6743 | .mm = mm, |
| 6744 | .private = vma, |
| 6745 | }; |
| 6746 | if (is_vm_hugetlb_page(vma)) |
| 6747 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6748 | ret = walk_page_range(vma->vm_start, vma->vm_end, |
| 6749 | &mem_cgroup_move_charge_walk); |
| 6750 | if (ret) |
| 6751 | /* |
| 6752 | * means we have consumed all precharges and failed in |
| 6753 | * doing additional charge. Just abandon here. |
| 6754 | */ |
| 6755 | break; |
| 6756 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6757 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6758 | } |
| 6759 | |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 6760 | static void mem_cgroup_move_task(struct cgroup *cont, |
| 6761 | struct cgroup_taskset *tset) |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 6762 | { |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 6763 | struct task_struct *p = cgroup_taskset_first(tset); |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 6764 | struct mm_struct *mm = get_task_mm(p); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6765 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6766 | if (mm) { |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 6767 | if (mc.to) |
| 6768 | mem_cgroup_move_charge(mm); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6769 | mmput(mm); |
| 6770 | } |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 6771 | if (mc.to) |
| 6772 | mem_cgroup_clear_mc(); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 6773 | } |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 6774 | #else /* !CONFIG_MMU */ |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 6775 | static int mem_cgroup_can_attach(struct cgroup *cgroup, |
| 6776 | struct cgroup_taskset *tset) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 6777 | { |
| 6778 | return 0; |
| 6779 | } |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 6780 | static void mem_cgroup_cancel_attach(struct cgroup *cgroup, |
| 6781 | struct cgroup_taskset *tset) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 6782 | { |
| 6783 | } |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 6784 | static void mem_cgroup_move_task(struct cgroup *cont, |
| 6785 | struct cgroup_taskset *tset) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 6786 | { |
| 6787 | } |
| 6788 | #endif |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 6789 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 6790 | struct cgroup_subsys mem_cgroup_subsys = { |
| 6791 | .name = "memory", |
| 6792 | .subsys_id = mem_cgroup_subsys_id, |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 6793 | .css_alloc = mem_cgroup_css_alloc, |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 6794 | .css_online = mem_cgroup_css_online, |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 6795 | .css_offline = mem_cgroup_css_offline, |
| 6796 | .css_free = mem_cgroup_css_free, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6797 | .can_attach = mem_cgroup_can_attach, |
| 6798 | .cancel_attach = mem_cgroup_cancel_attach, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 6799 | .attach = mem_cgroup_move_task, |
Tejun Heo | 6bc1034 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 6800 | .base_cftypes = mem_cgroup_files, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 6801 | .early_init = 0, |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 6802 | .use_id = 1, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 6803 | }; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 6804 | |
Andrew Morton | c255a45 | 2012-07-31 16:43:02 -0700 | [diff] [blame] | 6805 | #ifdef CONFIG_MEMCG_SWAP |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 6806 | static int __init enable_swap_account(char *s) |
| 6807 | { |
| 6808 | /* consider enabled if no parameter or 1 is given */ |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 6809 | if (!strcmp(s, "1")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 6810 | really_do_swap_account = 1; |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 6811 | else if (!strcmp(s, "0")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 6812 | really_do_swap_account = 0; |
| 6813 | return 1; |
| 6814 | } |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 6815 | __setup("swapaccount=", enable_swap_account); |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 6816 | |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 6817 | static void __init memsw_file_init(void) |
| 6818 | { |
Michal Hocko | 6acc8b0 | 2013-02-22 16:34:45 -0800 | [diff] [blame] | 6819 | WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files)); |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 6820 | } |
Michal Hocko | 6acc8b0 | 2013-02-22 16:34:45 -0800 | [diff] [blame] | 6821 | |
| 6822 | static void __init enable_swap_cgroup(void) |
| 6823 | { |
| 6824 | if (!mem_cgroup_disabled() && really_do_swap_account) { |
| 6825 | do_swap_account = 1; |
| 6826 | memsw_file_init(); |
| 6827 | } |
| 6828 | } |
| 6829 | |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 6830 | #else |
Michal Hocko | 6acc8b0 | 2013-02-22 16:34:45 -0800 | [diff] [blame] | 6831 | static void __init enable_swap_cgroup(void) |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 6832 | { |
| 6833 | } |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 6834 | #endif |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 6835 | |
| 6836 | /* |
Michal Hocko | 1081312 | 2013-02-22 16:35:41 -0800 | [diff] [blame] | 6837 | * subsys_initcall() for memory controller. |
| 6838 | * |
| 6839 | * Some parts like hotcpu_notifier() have to be initialized from this context |
| 6840 | * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically |
| 6841 | * everything that doesn't depend on a specific mem_cgroup structure should |
| 6842 | * be initialized from here. |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 6843 | */ |
| 6844 | static int __init mem_cgroup_init(void) |
| 6845 | { |
| 6846 | hotcpu_notifier(memcg_cpu_hotplug_callback, 0); |
Michal Hocko | 6acc8b0 | 2013-02-22 16:34:45 -0800 | [diff] [blame] | 6847 | enable_swap_cgroup(); |
Michal Hocko | 8787a1d | 2013-02-22 16:35:39 -0800 | [diff] [blame] | 6848 | mem_cgroup_soft_limit_tree_init(); |
Michal Hocko | e477749 | 2013-02-22 16:35:40 -0800 | [diff] [blame] | 6849 | memcg_stock_init(); |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 6850 | return 0; |
| 6851 | } |
| 6852 | subsys_initcall(mem_cgroup_init); |