Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1 | /* memcontrol.c - Memory Controller |
| 2 | * |
| 3 | * Copyright IBM Corporation, 2007 |
| 4 | * Author Balbir Singh <balbir@linux.vnet.ibm.com> |
| 5 | * |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 6 | * Copyright 2007 OpenVZ SWsoft Inc |
| 7 | * Author: Pavel Emelianov <xemul@openvz.org> |
| 8 | * |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 9 | * Memory thresholds |
| 10 | * Copyright (C) 2009 Nokia Corporation |
| 11 | * Author: Kirill A. Shutemov |
| 12 | * |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/res_counter.h> |
| 25 | #include <linux/memcontrol.h> |
| 26 | #include <linux/cgroup.h> |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 27 | #include <linux/mm.h> |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 28 | #include <linux/hugetlb.h> |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 29 | #include <linux/pagemap.h> |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 30 | #include <linux/smp.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 31 | #include <linux/page-flags.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 32 | #include <linux/backing-dev.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 33 | #include <linux/bit_spinlock.h> |
| 34 | #include <linux/rcupdate.h> |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 35 | #include <linux/limits.h> |
Paul Gortmaker | b9e15ba | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 36 | #include <linux/export.h> |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 37 | #include <linux/mutex.h> |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 38 | #include <linux/rbtree.h> |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 39 | #include <linux/slab.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 40 | #include <linux/swap.h> |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 41 | #include <linux/swapops.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 42 | #include <linux/spinlock.h> |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 43 | #include <linux/eventfd.h> |
| 44 | #include <linux/sort.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 45 | #include <linux/fs.h> |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 46 | #include <linux/seq_file.h> |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 47 | #include <linux/vmalloc.h> |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 48 | #include <linux/mm_inline.h> |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 49 | #include <linux/page_cgroup.h> |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 50 | #include <linux/cpu.h> |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 51 | #include <linux/oom.h> |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 52 | #include "internal.h" |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 53 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 54 | #include <asm/uaccess.h> |
| 55 | |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 56 | #include <trace/events/vmscan.h> |
| 57 | |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 58 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 59 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 60 | struct mem_cgroup *root_mem_cgroup __read_mostly; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 61 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 62 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 63 | /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */ |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 64 | int do_swap_account __read_mostly; |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 65 | |
| 66 | /* for remember boot option*/ |
| 67 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP_ENABLED |
| 68 | static int really_do_swap_account __initdata = 1; |
| 69 | #else |
| 70 | static int really_do_swap_account __initdata = 0; |
| 71 | #endif |
| 72 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 73 | #else |
| 74 | #define do_swap_account (0) |
| 75 | #endif |
| 76 | |
| 77 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 78 | /* |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 79 | * Statistics for memory cgroup. |
| 80 | */ |
| 81 | enum mem_cgroup_stat_index { |
| 82 | /* |
| 83 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. |
| 84 | */ |
| 85 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 86 | MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */ |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 87 | MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 88 | MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */ |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 89 | MEM_CGROUP_STAT_DATA, /* end of data requires synchronization */ |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 90 | MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 91 | MEM_CGROUP_STAT_NSTATS, |
| 92 | }; |
| 93 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 94 | enum mem_cgroup_events_index { |
| 95 | MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */ |
| 96 | MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */ |
| 97 | MEM_CGROUP_EVENTS_COUNT, /* # of pages paged in/out */ |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 98 | MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */ |
| 99 | MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */ |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 100 | MEM_CGROUP_EVENTS_NSTATS, |
| 101 | }; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 102 | /* |
| 103 | * Per memcg event counter is incremented at every pagein/pageout. With THP, |
| 104 | * it will be incremated by the number of pages. This counter is used for |
| 105 | * for trigger some periodic events. This is straightforward and better |
| 106 | * than using jiffies etc. to handle periodic memcg event. |
| 107 | */ |
| 108 | enum mem_cgroup_events_target { |
| 109 | MEM_CGROUP_TARGET_THRESH, |
| 110 | MEM_CGROUP_TARGET_SOFTLIMIT, |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 111 | MEM_CGROUP_TARGET_NUMAINFO, |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 112 | MEM_CGROUP_NTARGETS, |
| 113 | }; |
| 114 | #define THRESHOLDS_EVENTS_TARGET (128) |
| 115 | #define SOFTLIMIT_EVENTS_TARGET (1024) |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 116 | #define NUMAINFO_EVENTS_TARGET (1024) |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 117 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 118 | struct mem_cgroup_stat_cpu { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 119 | long count[MEM_CGROUP_STAT_NSTATS]; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 120 | unsigned long events[MEM_CGROUP_EVENTS_NSTATS]; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 121 | unsigned long targets[MEM_CGROUP_NTARGETS]; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 122 | }; |
| 123 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 124 | /* |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 125 | * per-zone information in memory controller. |
| 126 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 127 | struct mem_cgroup_per_zone { |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 128 | /* |
| 129 | * spin_lock to protect the per cgroup LRU |
| 130 | */ |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 131 | struct list_head lists[NR_LRU_LISTS]; |
| 132 | unsigned long count[NR_LRU_LISTS]; |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 133 | |
| 134 | struct zone_reclaim_stat reclaim_stat; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 135 | struct rb_node tree_node; /* RB tree node */ |
| 136 | unsigned long long usage_in_excess;/* Set to the value by which */ |
| 137 | /* the soft limit is exceeded*/ |
| 138 | bool on_tree; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 139 | struct mem_cgroup *mem; /* Back pointer, we cannot */ |
| 140 | /* use container_of */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 141 | }; |
| 142 | /* Macro for accessing counter */ |
| 143 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) |
| 144 | |
| 145 | struct mem_cgroup_per_node { |
| 146 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; |
| 147 | }; |
| 148 | |
| 149 | struct mem_cgroup_lru_info { |
| 150 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; |
| 151 | }; |
| 152 | |
| 153 | /* |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 154 | * Cgroups above their limits are maintained in a RB-Tree, independent of |
| 155 | * their hierarchy representation |
| 156 | */ |
| 157 | |
| 158 | struct mem_cgroup_tree_per_zone { |
| 159 | struct rb_root rb_root; |
| 160 | spinlock_t lock; |
| 161 | }; |
| 162 | |
| 163 | struct mem_cgroup_tree_per_node { |
| 164 | struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES]; |
| 165 | }; |
| 166 | |
| 167 | struct mem_cgroup_tree { |
| 168 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; |
| 169 | }; |
| 170 | |
| 171 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; |
| 172 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 173 | struct mem_cgroup_threshold { |
| 174 | struct eventfd_ctx *eventfd; |
| 175 | u64 threshold; |
| 176 | }; |
| 177 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 178 | /* For threshold */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 179 | struct mem_cgroup_threshold_ary { |
| 180 | /* An array index points to threshold just below usage. */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 181 | int current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 182 | /* Size of entries[] */ |
| 183 | unsigned int size; |
| 184 | /* Array of thresholds */ |
| 185 | struct mem_cgroup_threshold entries[0]; |
| 186 | }; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 187 | |
| 188 | struct mem_cgroup_thresholds { |
| 189 | /* Primary thresholds array */ |
| 190 | struct mem_cgroup_threshold_ary *primary; |
| 191 | /* |
| 192 | * Spare threshold array. |
| 193 | * This is needed to make mem_cgroup_unregister_event() "never fail". |
| 194 | * It must be able to store at least primary->size - 1 entries. |
| 195 | */ |
| 196 | struct mem_cgroup_threshold_ary *spare; |
| 197 | }; |
| 198 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 199 | /* for OOM */ |
| 200 | struct mem_cgroup_eventfd_list { |
| 201 | struct list_head list; |
| 202 | struct eventfd_ctx *eventfd; |
| 203 | }; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 204 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 205 | static void mem_cgroup_threshold(struct mem_cgroup *memcg); |
| 206 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 207 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 208 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 209 | * The memory controller data structure. The memory controller controls both |
| 210 | * page cache and RSS per cgroup. We would eventually like to provide |
| 211 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, |
| 212 | * to help the administrator determine what knobs to tune. |
| 213 | * |
| 214 | * 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] | 215 | * we hit the water mark. May be even add a low water mark, such that |
| 216 | * no reclaim occurs from a cgroup at it's low water mark, this is |
| 217 | * a feature that will be implemented much later in the future. |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 218 | */ |
| 219 | struct mem_cgroup { |
| 220 | struct cgroup_subsys_state css; |
| 221 | /* |
| 222 | * the counter to account for memory usage |
| 223 | */ |
| 224 | struct res_counter res; |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 225 | /* |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 226 | * the counter to account for mem+swap usage. |
| 227 | */ |
| 228 | struct res_counter memsw; |
| 229 | /* |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 230 | * the counter to account for kmem usage. |
| 231 | */ |
| 232 | struct res_counter kmem; |
| 233 | /* |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 234 | * Per cgroup active and inactive list, similar to the |
| 235 | * per zone LRU lists. |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 236 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 237 | struct mem_cgroup_lru_info info; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 238 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 239 | * While reclaiming in a hierarchy, we cache the last child we |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 240 | * reclaimed from. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 241 | */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 242 | int last_scanned_child; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 243 | int last_scanned_node; |
| 244 | #if MAX_NUMNODES > 1 |
| 245 | nodemask_t scan_nodes; |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 246 | atomic_t numainfo_events; |
| 247 | atomic_t numainfo_updating; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 248 | #endif |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 249 | /* |
| 250 | * Should the accounting and control be hierarchical, per subtree? |
| 251 | */ |
| 252 | bool use_hierarchy; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 253 | |
| 254 | bool oom_lock; |
| 255 | atomic_t under_oom; |
| 256 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 257 | atomic_t refcnt; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 258 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 259 | int swappiness; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 260 | /* OOM-Killer disable */ |
| 261 | int oom_kill_disable; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 262 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 263 | /* set when res.limit == memsw.limit */ |
| 264 | bool memsw_is_minimum; |
| 265 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 266 | /* protect arrays of thresholds */ |
| 267 | struct mutex thresholds_lock; |
| 268 | |
| 269 | /* thresholds for memory usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 270 | struct mem_cgroup_thresholds thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 271 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 272 | /* thresholds for mem+swap usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 273 | struct mem_cgroup_thresholds memsw_thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 274 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 275 | /* For oom notifier event fd */ |
| 276 | struct list_head oom_notify; |
Johannes Weiner | 185efc0 | 2011-09-14 16:21:58 -0700 | [diff] [blame] | 277 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 278 | /* |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 279 | * Should we move charges of a task when a task is moved into this |
| 280 | * mem_cgroup ? And what type of charges should we move ? |
| 281 | */ |
| 282 | unsigned long move_charge_at_immigrate; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 283 | /* |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 284 | * Should kernel memory limits be stabilished independently |
| 285 | * from user memory ? |
| 286 | */ |
| 287 | int kmem_independent_accounting; |
| 288 | /* |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 289 | * percpu counter. |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 290 | */ |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 291 | struct mem_cgroup_stat_cpu *stat; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 292 | /* |
| 293 | * used when a cpu is offlined or other synchronizations |
| 294 | * See mem_cgroup_read_stat(). |
| 295 | */ |
| 296 | struct mem_cgroup_stat_cpu nocpu_base; |
| 297 | spinlock_t pcp_counter_lock; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 298 | }; |
| 299 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 300 | /* Stuffs for move charges at task migration. */ |
| 301 | /* |
| 302 | * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a |
| 303 | * left-shifted bitmap of these types. |
| 304 | */ |
| 305 | enum move_type { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 306 | MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */ |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 307 | MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 308 | NR_MOVE_TYPE, |
| 309 | }; |
| 310 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 311 | /* "mc" and its members are protected by cgroup_mutex */ |
| 312 | static struct move_charge_struct { |
Daisuke Nishimura | b1dd693 | 2010-11-24 12:57:06 -0800 | [diff] [blame] | 313 | spinlock_t lock; /* for from, to */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 314 | struct mem_cgroup *from; |
| 315 | struct mem_cgroup *to; |
| 316 | unsigned long precharge; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 317 | unsigned long moved_charge; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 318 | unsigned long moved_swap; |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 319 | struct task_struct *moving_task; /* a task moving charges */ |
| 320 | wait_queue_head_t waitq; /* a waitq for other context */ |
| 321 | } mc = { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 322 | .lock = __SPIN_LOCK_UNLOCKED(mc.lock), |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 323 | .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq), |
| 324 | }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 325 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 326 | static bool move_anon(void) |
| 327 | { |
| 328 | return test_bit(MOVE_CHARGE_TYPE_ANON, |
| 329 | &mc.to->move_charge_at_immigrate); |
| 330 | } |
| 331 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 332 | static bool move_file(void) |
| 333 | { |
| 334 | return test_bit(MOVE_CHARGE_TYPE_FILE, |
| 335 | &mc.to->move_charge_at_immigrate); |
| 336 | } |
| 337 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 338 | /* |
| 339 | * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft |
| 340 | * limit reclaim to prevent infinite loops, if they ever occur. |
| 341 | */ |
| 342 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100) |
| 343 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2) |
| 344 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 345 | enum charge_type { |
| 346 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
| 347 | MEM_CGROUP_CHARGE_TYPE_MAPPED, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 348 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 349 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 350 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 351 | MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 352 | NR_CHARGE_TYPE, |
| 353 | }; |
| 354 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 355 | /* for encoding cft->private value on file */ |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 356 | |
| 357 | enum mem_type { |
| 358 | _MEM = 0, |
| 359 | _MEMSWAP, |
| 360 | _OOM_TYPE, |
| 361 | _KMEM, |
| 362 | }; |
| 363 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 364 | #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) |
| 365 | #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff) |
| 366 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 367 | /* Used for OOM nofiier */ |
| 368 | #define OOM_CONTROL (0) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 369 | |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 370 | /* |
| 371 | * Reclaim flags for mem_cgroup_hierarchical_reclaim |
| 372 | */ |
| 373 | #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0 |
| 374 | #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT) |
| 375 | #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1 |
| 376 | #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 377 | #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2 |
| 378 | #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT) |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 379 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 380 | static void mem_cgroup_get(struct mem_cgroup *memcg); |
| 381 | static void mem_cgroup_put(struct mem_cgroup *memcg); |
| 382 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg); |
| 383 | static void drain_all_stock_async(struct mem_cgroup *memcg); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 384 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 385 | static struct mem_cgroup_per_zone * |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 386 | mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 387 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 388 | return &memcg->info.nodeinfo[nid]->zoneinfo[zid]; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 391 | struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg) |
Wu Fengguang | d324236 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 392 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 393 | return &memcg->css; |
Wu Fengguang | d324236 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 394 | } |
| 395 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 396 | static struct mem_cgroup_per_zone * |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 397 | page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 398 | { |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 399 | int nid = page_to_nid(page); |
| 400 | int zid = page_zonenum(page); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 401 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 402 | return mem_cgroup_zoneinfo(memcg, nid, zid); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static struct mem_cgroup_tree_per_zone * |
| 406 | soft_limit_tree_node_zone(int nid, int zid) |
| 407 | { |
| 408 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 409 | } |
| 410 | |
| 411 | static struct mem_cgroup_tree_per_zone * |
| 412 | soft_limit_tree_from_page(struct page *page) |
| 413 | { |
| 414 | int nid = page_to_nid(page); |
| 415 | int zid = page_zonenum(page); |
| 416 | |
| 417 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 418 | } |
| 419 | |
| 420 | static void |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 421 | __mem_cgroup_insert_exceeded(struct mem_cgroup *memcg, |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 422 | struct mem_cgroup_per_zone *mz, |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 423 | struct mem_cgroup_tree_per_zone *mctz, |
| 424 | unsigned long long new_usage_in_excess) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 425 | { |
| 426 | struct rb_node **p = &mctz->rb_root.rb_node; |
| 427 | struct rb_node *parent = NULL; |
| 428 | struct mem_cgroup_per_zone *mz_node; |
| 429 | |
| 430 | if (mz->on_tree) |
| 431 | return; |
| 432 | |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 433 | mz->usage_in_excess = new_usage_in_excess; |
| 434 | if (!mz->usage_in_excess) |
| 435 | return; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 436 | while (*p) { |
| 437 | parent = *p; |
| 438 | mz_node = rb_entry(parent, struct mem_cgroup_per_zone, |
| 439 | tree_node); |
| 440 | if (mz->usage_in_excess < mz_node->usage_in_excess) |
| 441 | p = &(*p)->rb_left; |
| 442 | /* |
| 443 | * We can't avoid mem cgroups that are over their soft |
| 444 | * limit by the same amount |
| 445 | */ |
| 446 | else if (mz->usage_in_excess >= mz_node->usage_in_excess) |
| 447 | p = &(*p)->rb_right; |
| 448 | } |
| 449 | rb_link_node(&mz->tree_node, parent, p); |
| 450 | rb_insert_color(&mz->tree_node, &mctz->rb_root); |
| 451 | mz->on_tree = true; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static void |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 455 | __mem_cgroup_remove_exceeded(struct mem_cgroup *memcg, |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 456 | struct mem_cgroup_per_zone *mz, |
| 457 | struct mem_cgroup_tree_per_zone *mctz) |
| 458 | { |
| 459 | if (!mz->on_tree) |
| 460 | return; |
| 461 | rb_erase(&mz->tree_node, &mctz->rb_root); |
| 462 | mz->on_tree = false; |
| 463 | } |
| 464 | |
| 465 | static void |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 466 | mem_cgroup_remove_exceeded(struct mem_cgroup *memcg, |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 467 | struct mem_cgroup_per_zone *mz, |
| 468 | struct mem_cgroup_tree_per_zone *mctz) |
| 469 | { |
| 470 | spin_lock(&mctz->lock); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 471 | __mem_cgroup_remove_exceeded(memcg, mz, mctz); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 472 | spin_unlock(&mctz->lock); |
| 473 | } |
| 474 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 475 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 476 | 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] | 477 | { |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 478 | unsigned long long excess; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 479 | struct mem_cgroup_per_zone *mz; |
| 480 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 481 | int nid = page_to_nid(page); |
| 482 | int zid = page_zonenum(page); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 483 | mctz = soft_limit_tree_from_page(page); |
| 484 | |
| 485 | /* |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 486 | * Necessary to update all ancestors when hierarchy is used. |
| 487 | * because their event counter is not touched. |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 488 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 489 | for (; memcg; memcg = parent_mem_cgroup(memcg)) { |
| 490 | mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 491 | excess = res_counter_soft_limit_excess(&memcg->res); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 492 | /* |
| 493 | * We have to update the tree if mz is on RB-tree or |
| 494 | * mem is over its softlimit. |
| 495 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 496 | if (excess || mz->on_tree) { |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 497 | spin_lock(&mctz->lock); |
| 498 | /* if on-tree, remove it */ |
| 499 | if (mz->on_tree) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 500 | __mem_cgroup_remove_exceeded(memcg, mz, mctz); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 501 | /* |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 502 | * Insert again. mz->usage_in_excess will be updated. |
| 503 | * If excess is 0, no tree ops. |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 504 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 505 | __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 506 | spin_unlock(&mctz->lock); |
| 507 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 511 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 512 | { |
| 513 | int node, zone; |
| 514 | struct mem_cgroup_per_zone *mz; |
| 515 | struct mem_cgroup_tree_per_zone *mctz; |
| 516 | |
| 517 | for_each_node_state(node, N_POSSIBLE) { |
| 518 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 519 | mz = mem_cgroup_zoneinfo(memcg, node, zone); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 520 | mctz = soft_limit_tree_node_zone(node, zone); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 521 | mem_cgroup_remove_exceeded(memcg, mz, mctz); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 526 | static struct mem_cgroup_per_zone * |
| 527 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 528 | { |
| 529 | struct rb_node *rightmost = NULL; |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 530 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 531 | |
| 532 | retry: |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 533 | mz = NULL; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 534 | rightmost = rb_last(&mctz->rb_root); |
| 535 | if (!rightmost) |
| 536 | goto done; /* Nothing to reclaim from */ |
| 537 | |
| 538 | mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node); |
| 539 | /* |
| 540 | * Remove the node now but someone else can add it back, |
| 541 | * we will to add it back at the end of reclaim to its correct |
| 542 | * position in the tree. |
| 543 | */ |
| 544 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
| 545 | if (!res_counter_soft_limit_excess(&mz->mem->res) || |
| 546 | !css_tryget(&mz->mem->css)) |
| 547 | goto retry; |
| 548 | done: |
| 549 | return mz; |
| 550 | } |
| 551 | |
| 552 | static struct mem_cgroup_per_zone * |
| 553 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 554 | { |
| 555 | struct mem_cgroup_per_zone *mz; |
| 556 | |
| 557 | spin_lock(&mctz->lock); |
| 558 | mz = __mem_cgroup_largest_soft_limit_node(mctz); |
| 559 | spin_unlock(&mctz->lock); |
| 560 | return mz; |
| 561 | } |
| 562 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 563 | /* |
| 564 | * Implementation Note: reading percpu statistics for memcg. |
| 565 | * |
| 566 | * Both of vmstat[] and percpu_counter has threshold and do periodic |
| 567 | * synchronization to implement "quick" read. There are trade-off between |
| 568 | * reading cost and precision of value. Then, we may have a chance to implement |
| 569 | * a periodic synchronizion of counter in memcg's counter. |
| 570 | * |
| 571 | * But this _read() function is used for user interface now. The user accounts |
| 572 | * memory usage by memory cgroup and he _always_ requires exact value because |
| 573 | * he accounts memory. Even if we provide quick-and-fuzzy read, we always |
| 574 | * have to visit all online cpus and make sum. So, for now, unnecessary |
| 575 | * synchronization is not implemented. (just implemented for cpu hotplug) |
| 576 | * |
| 577 | * If there are kernel internal actions which can make use of some not-exact |
| 578 | * value, and reading all cpu value can be performance bottleneck in some |
| 579 | * common workload, threashold and synchonization as vmstat[] should be |
| 580 | * implemented. |
| 581 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 582 | static long mem_cgroup_read_stat(struct mem_cgroup *memcg, |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 583 | enum mem_cgroup_stat_index idx) |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 584 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 585 | long val = 0; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 586 | int cpu; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 587 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 588 | get_online_cpus(); |
| 589 | for_each_online_cpu(cpu) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 590 | val += per_cpu(memcg->stat->count[idx], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 591 | #ifdef CONFIG_HOTPLUG_CPU |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 592 | spin_lock(&memcg->pcp_counter_lock); |
| 593 | val += memcg->nocpu_base.count[idx]; |
| 594 | spin_unlock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 595 | #endif |
| 596 | put_online_cpus(); |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 597 | return val; |
| 598 | } |
| 599 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 600 | static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg, |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 601 | bool charge) |
| 602 | { |
| 603 | int val = (charge) ? 1 : -1; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 604 | this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAPOUT], val); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 607 | void mem_cgroup_pgfault(struct mem_cgroup *memcg, int val) |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 608 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 609 | this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 612 | void mem_cgroup_pgmajfault(struct mem_cgroup *memcg, int val) |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 613 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 614 | this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 617 | static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg, |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 618 | enum mem_cgroup_events_index idx) |
| 619 | { |
| 620 | unsigned long val = 0; |
| 621 | int cpu; |
| 622 | |
| 623 | for_each_online_cpu(cpu) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 624 | val += per_cpu(memcg->stat->events[idx], cpu); |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 625 | #ifdef CONFIG_HOTPLUG_CPU |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 626 | spin_lock(&memcg->pcp_counter_lock); |
| 627 | val += memcg->nocpu_base.events[idx]; |
| 628 | spin_unlock(&memcg->pcp_counter_lock); |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 629 | #endif |
| 630 | return val; |
| 631 | } |
| 632 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 633 | static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 634 | bool file, int nr_pages) |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 635 | { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 636 | preempt_disable(); |
| 637 | |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 638 | if (file) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 639 | __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE], |
| 640 | nr_pages); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 641 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 642 | __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS], |
| 643 | nr_pages); |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 644 | |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 645 | /* pagein of a big page is an event. So, ignore page size */ |
| 646 | if (nr_pages > 0) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 647 | __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 648 | else { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 649 | __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 650 | nr_pages = -nr_pages; /* for event */ |
| 651 | } |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 652 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 653 | __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 654 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 655 | preempt_enable(); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 656 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 657 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 658 | unsigned long |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 659 | 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] | 660 | unsigned int lru_mask) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 661 | { |
| 662 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 663 | enum lru_list l; |
| 664 | unsigned long ret = 0; |
| 665 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 666 | mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 667 | |
| 668 | for_each_lru(l) { |
| 669 | if (BIT(l) & lru_mask) |
| 670 | ret += MEM_CGROUP_ZSTAT(mz, l); |
| 671 | } |
| 672 | return ret; |
| 673 | } |
| 674 | |
| 675 | static unsigned long |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 676 | mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 677 | int nid, unsigned int lru_mask) |
| 678 | { |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 679 | u64 total = 0; |
| 680 | int zid; |
| 681 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 682 | for (zid = 0; zid < MAX_NR_ZONES; zid++) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 683 | total += mem_cgroup_zone_nr_lru_pages(memcg, |
| 684 | nid, zid, lru_mask); |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 685 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 686 | return total; |
| 687 | } |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 688 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 689 | static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 690 | unsigned int lru_mask) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 691 | { |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 692 | int nid; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 693 | u64 total = 0; |
| 694 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 695 | for_each_node_state(nid, N_HIGH_MEMORY) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 696 | total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 697 | return total; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 698 | } |
| 699 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 700 | static bool __memcg_event_check(struct mem_cgroup *memcg, int target) |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 701 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 702 | unsigned long val, next; |
| 703 | |
Steven Rostedt | 4799401 | 2011-11-02 13:38:33 -0700 | [diff] [blame] | 704 | val = __this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]); |
| 705 | next = __this_cpu_read(memcg->stat->targets[target]); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 706 | /* from time_after() in jiffies.h */ |
| 707 | return ((long)next - (long)val < 0); |
| 708 | } |
| 709 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 710 | static void __mem_cgroup_target_update(struct mem_cgroup *memcg, int target) |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 711 | { |
| 712 | unsigned long val, next; |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 713 | |
Steven Rostedt | 4799401 | 2011-11-02 13:38:33 -0700 | [diff] [blame] | 714 | val = __this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 715 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 716 | switch (target) { |
| 717 | case MEM_CGROUP_TARGET_THRESH: |
| 718 | next = val + THRESHOLDS_EVENTS_TARGET; |
| 719 | break; |
| 720 | case MEM_CGROUP_TARGET_SOFTLIMIT: |
| 721 | next = val + SOFTLIMIT_EVENTS_TARGET; |
| 722 | break; |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 723 | case MEM_CGROUP_TARGET_NUMAINFO: |
| 724 | next = val + NUMAINFO_EVENTS_TARGET; |
| 725 | break; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 726 | default: |
| 727 | return; |
| 728 | } |
| 729 | |
Steven Rostedt | 4799401 | 2011-11-02 13:38:33 -0700 | [diff] [blame] | 730 | __this_cpu_write(memcg->stat->targets[target], next); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /* |
| 734 | * Check events in order. |
| 735 | * |
| 736 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 737 | static void memcg_check_events(struct mem_cgroup *memcg, struct page *page) |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 738 | { |
Steven Rostedt | 4799401 | 2011-11-02 13:38:33 -0700 | [diff] [blame] | 739 | preempt_disable(); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 740 | /* threshold event is triggered in finer grain than soft limit */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 741 | if (unlikely(__memcg_event_check(memcg, MEM_CGROUP_TARGET_THRESH))) { |
| 742 | mem_cgroup_threshold(memcg); |
| 743 | __mem_cgroup_target_update(memcg, MEM_CGROUP_TARGET_THRESH); |
| 744 | if (unlikely(__memcg_event_check(memcg, |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 745 | MEM_CGROUP_TARGET_SOFTLIMIT))) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 746 | mem_cgroup_update_tree(memcg, page); |
| 747 | __mem_cgroup_target_update(memcg, |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 748 | MEM_CGROUP_TARGET_SOFTLIMIT); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 749 | } |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 750 | #if MAX_NUMNODES > 1 |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 751 | if (unlikely(__memcg_event_check(memcg, |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 752 | MEM_CGROUP_TARGET_NUMAINFO))) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 753 | atomic_inc(&memcg->numainfo_events); |
| 754 | __mem_cgroup_target_update(memcg, |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 755 | MEM_CGROUP_TARGET_NUMAINFO); |
| 756 | } |
| 757 | #endif |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 758 | } |
Steven Rostedt | 4799401 | 2011-11-02 13:38:33 -0700 | [diff] [blame] | 759 | preempt_enable(); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 760 | } |
| 761 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 762 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 763 | { |
| 764 | return container_of(cgroup_subsys_state(cont, |
| 765 | mem_cgroup_subsys_id), struct mem_cgroup, |
| 766 | css); |
| 767 | } |
| 768 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 769 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 770 | { |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 771 | /* |
| 772 | * mm_update_next_owner() may clear mm->owner to NULL |
| 773 | * if it races with swapoff, page migration, etc. |
| 774 | * So this can be called with p == NULL. |
| 775 | */ |
| 776 | if (unlikely(!p)) |
| 777 | return NULL; |
| 778 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 779 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
| 780 | struct mem_cgroup, css); |
| 781 | } |
| 782 | |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 783 | 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] | 784 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 785 | struct mem_cgroup *memcg = NULL; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 786 | |
| 787 | if (!mm) |
| 788 | return NULL; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 789 | /* |
| 790 | * Because we have no locks, mm->owner's may be being moved to other |
| 791 | * cgroup. We use css_tryget() here even if this looks |
| 792 | * pessimistic (rather than adding locks here). |
| 793 | */ |
| 794 | rcu_read_lock(); |
| 795 | do { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 796 | memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 797 | if (unlikely(!memcg)) |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 798 | break; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 799 | } while (!css_tryget(&memcg->css)); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 800 | rcu_read_unlock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 801 | return memcg; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 802 | } |
| 803 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 804 | /* The caller has to guarantee "mem" exists before calling this */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 805 | static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 806 | { |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 807 | struct cgroup_subsys_state *css; |
| 808 | int found; |
| 809 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 810 | if (!memcg) /* ROOT cgroup has the smallest ID */ |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 811 | return root_mem_cgroup; /*css_put/get against root is ignored*/ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 812 | if (!memcg->use_hierarchy) { |
| 813 | if (css_tryget(&memcg->css)) |
| 814 | return memcg; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 815 | return NULL; |
| 816 | } |
| 817 | rcu_read_lock(); |
| 818 | /* |
| 819 | * searching a memory cgroup which has the smallest ID under given |
| 820 | * ROOT cgroup. (ID >= 1) |
| 821 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 822 | css = css_get_next(&mem_cgroup_subsys, 1, &memcg->css, &found); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 823 | if (css && css_tryget(css)) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 824 | memcg = container_of(css, struct mem_cgroup, css); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 825 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 826 | memcg = NULL; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 827 | rcu_read_unlock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 828 | return memcg; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 829 | } |
| 830 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 831 | static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter, |
| 832 | struct mem_cgroup *root, |
| 833 | bool cond) |
| 834 | { |
| 835 | int nextid = css_id(&iter->css) + 1; |
| 836 | int found; |
| 837 | int hierarchy_used; |
| 838 | struct cgroup_subsys_state *css; |
| 839 | |
| 840 | hierarchy_used = iter->use_hierarchy; |
| 841 | |
| 842 | css_put(&iter->css); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 843 | /* If no ROOT, walk all, ignore hierarchy */ |
| 844 | if (!cond || (root && !hierarchy_used)) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 845 | return NULL; |
| 846 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 847 | if (!root) |
| 848 | root = root_mem_cgroup; |
| 849 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 850 | do { |
| 851 | iter = NULL; |
| 852 | rcu_read_lock(); |
| 853 | |
| 854 | css = css_get_next(&mem_cgroup_subsys, nextid, |
| 855 | &root->css, &found); |
| 856 | if (css && css_tryget(css)) |
| 857 | iter = container_of(css, struct mem_cgroup, css); |
| 858 | rcu_read_unlock(); |
| 859 | /* If css is NULL, no more cgroups will be found */ |
| 860 | nextid = found + 1; |
| 861 | } while (css && !iter); |
| 862 | |
| 863 | return iter; |
| 864 | } |
| 865 | /* |
| 866 | * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please |
| 867 | * be careful that "break" loop is not allowed. We have reference count. |
| 868 | * Instead of that modify "cond" to be false and "continue" to exit the loop. |
| 869 | */ |
| 870 | #define for_each_mem_cgroup_tree_cond(iter, root, cond) \ |
| 871 | for (iter = mem_cgroup_start_loop(root);\ |
| 872 | iter != NULL;\ |
| 873 | iter = mem_cgroup_get_next(iter, root, cond)) |
| 874 | |
| 875 | #define for_each_mem_cgroup_tree(iter, root) \ |
| 876 | for_each_mem_cgroup_tree_cond(iter, root, true) |
| 877 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 878 | #define for_each_mem_cgroup_all(iter) \ |
| 879 | for_each_mem_cgroup_tree_cond(iter, NULL, true) |
| 880 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 881 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 882 | static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg) |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 883 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 884 | return (memcg == root_mem_cgroup); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 887 | void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx) |
| 888 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 889 | struct mem_cgroup *memcg; |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 890 | |
| 891 | if (!mm) |
| 892 | return; |
| 893 | |
| 894 | rcu_read_lock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 895 | memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 896 | if (unlikely(!memcg)) |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 897 | goto out; |
| 898 | |
| 899 | switch (idx) { |
| 900 | case PGMAJFAULT: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 901 | mem_cgroup_pgmajfault(memcg, 1); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 902 | break; |
| 903 | case PGFAULT: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 904 | mem_cgroup_pgfault(memcg, 1); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 905 | break; |
| 906 | default: |
| 907 | BUG(); |
| 908 | } |
| 909 | out: |
| 910 | rcu_read_unlock(); |
| 911 | } |
| 912 | EXPORT_SYMBOL(mem_cgroup_count_vm_event); |
| 913 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 914 | /* |
| 915 | * Following LRU functions are allowed to be used without PCG_LOCK. |
| 916 | * Operations are called by routine of global LRU independently from memcg. |
| 917 | * What we have to take care of here is validness of pc->mem_cgroup. |
| 918 | * |
| 919 | * Changes to pc->mem_cgroup happens when |
| 920 | * 1. charge |
| 921 | * 2. moving account |
| 922 | * In typical case, "charge" is done before add-to-lru. Exception is SwapCache. |
| 923 | * It is added to LRU before charge. |
| 924 | * If PCG_USED bit is not set, page_cgroup is not added to this private LRU. |
| 925 | * When moving account, the page is not on LRU. It's isolated. |
| 926 | */ |
| 927 | |
| 928 | void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 929 | { |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 930 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 931 | struct mem_cgroup_per_zone *mz; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 932 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 933 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 934 | return; |
| 935 | pc = lookup_page_cgroup(page); |
| 936 | /* can happen while we handle swapcache. */ |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 937 | if (!TestClearPageCgroupAcctLRU(pc)) |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 938 | return; |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 939 | VM_BUG_ON(!pc->mem_cgroup); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 940 | /* |
| 941 | * We don't check PCG_USED bit. It's cleared when the "page" is finally |
| 942 | * removed from global LRU. |
| 943 | */ |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 944 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 945 | /* huge page split is done under lru_lock. so, we have no races. */ |
| 946 | MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 947 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 948 | return; |
| 949 | VM_BUG_ON(list_empty(&pc->lru)); |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 950 | list_del_init(&pc->lru); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 951 | } |
| 952 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 953 | void mem_cgroup_del_lru(struct page *page) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 954 | { |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 955 | mem_cgroup_del_lru_list(page, page_lru(page)); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 956 | } |
| 957 | |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 958 | /* |
| 959 | * Writeback is about to end against a page which has been marked for immediate |
| 960 | * reclaim. If it still appears to be reclaimable, move it to the tail of the |
| 961 | * inactive list. |
| 962 | */ |
| 963 | void mem_cgroup_rotate_reclaimable_page(struct page *page) |
| 964 | { |
| 965 | struct mem_cgroup_per_zone *mz; |
| 966 | struct page_cgroup *pc; |
| 967 | enum lru_list lru = page_lru(page); |
| 968 | |
| 969 | if (mem_cgroup_disabled()) |
| 970 | return; |
| 971 | |
| 972 | pc = lookup_page_cgroup(page); |
| 973 | /* unused or root page is not rotated. */ |
| 974 | if (!PageCgroupUsed(pc)) |
| 975 | return; |
| 976 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 977 | smp_rmb(); |
| 978 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 979 | return; |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 980 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 981 | list_move_tail(&pc->lru, &mz->lists[lru]); |
| 982 | } |
| 983 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 984 | void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 985 | { |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 986 | struct mem_cgroup_per_zone *mz; |
| 987 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 988 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 989 | if (mem_cgroup_disabled()) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 990 | return; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 991 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 992 | pc = lookup_page_cgroup(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 993 | /* unused or root page is not rotated. */ |
Johannes Weiner | 713735b4 | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 994 | if (!PageCgroupUsed(pc)) |
| 995 | return; |
| 996 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 997 | smp_rmb(); |
| 998 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 999 | return; |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1000 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1001 | list_move(&pc->lru, &mz->lists[lru]); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1002 | } |
| 1003 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1004 | void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru) |
| 1005 | { |
| 1006 | struct page_cgroup *pc; |
| 1007 | struct mem_cgroup_per_zone *mz; |
| 1008 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1009 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1010 | return; |
| 1011 | pc = lookup_page_cgroup(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 1012 | VM_BUG_ON(PageCgroupAcctLRU(pc)); |
Johannes Weiner | a61ed3c | 2011-11-02 13:38:29 -0700 | [diff] [blame] | 1013 | /* |
| 1014 | * putback: charge: |
| 1015 | * SetPageLRU SetPageCgroupUsed |
| 1016 | * smp_mb smp_mb |
| 1017 | * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU |
| 1018 | * |
| 1019 | * Ensure that one of the two sides adds the page to the memcg |
| 1020 | * LRU during a race. |
| 1021 | */ |
| 1022 | smp_mb(); |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1023 | if (!PageCgroupUsed(pc)) |
| 1024 | return; |
Johannes Weiner | 713735b4 | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 1025 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 1026 | smp_rmb(); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1027 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 1028 | /* huge page split is done under lru_lock. so, we have no races. */ |
| 1029 | MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 1030 | SetPageCgroupAcctLRU(pc); |
| 1031 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 1032 | return; |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1033 | list_add(&pc->lru, &mz->lists[lru]); |
| 1034 | } |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1035 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1036 | /* |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1037 | * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed |
| 1038 | * while it's linked to lru because the page may be reused after it's fully |
| 1039 | * uncharged. To handle that, unlink page_cgroup from LRU when charge it again. |
| 1040 | * It's done under lock_page and expected that zone->lru_lock isnever held. |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1041 | */ |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1042 | static void mem_cgroup_lru_del_before_commit(struct page *page) |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1043 | { |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1044 | unsigned long flags; |
| 1045 | struct zone *zone = page_zone(page); |
| 1046 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 1047 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1048 | /* |
| 1049 | * Doing this check without taking ->lru_lock seems wrong but this |
| 1050 | * is safe. Because if page_cgroup's USED bit is unset, the page |
| 1051 | * will not be added to any memcg's LRU. If page_cgroup's USED bit is |
| 1052 | * set, the commit after this will fail, anyway. |
| 1053 | * This all charge/uncharge is done under some mutual execustion. |
| 1054 | * So, we don't need to taking care of changes in USED bit. |
| 1055 | */ |
| 1056 | if (likely(!PageLRU(page))) |
| 1057 | return; |
| 1058 | |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1059 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 1060 | /* |
| 1061 | * Forget old LRU when this page_cgroup is *not* used. This Used bit |
| 1062 | * is guarded by lock_page() because the page is SwapCache. |
| 1063 | */ |
| 1064 | if (!PageCgroupUsed(pc)) |
| 1065 | mem_cgroup_del_lru_list(page, page_lru(page)); |
| 1066 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1069 | static void mem_cgroup_lru_add_after_commit(struct page *page) |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1070 | { |
| 1071 | unsigned long flags; |
| 1072 | struct zone *zone = page_zone(page); |
| 1073 | struct page_cgroup *pc = lookup_page_cgroup(page); |
Johannes Weiner | a61ed3c | 2011-11-02 13:38:29 -0700 | [diff] [blame] | 1074 | /* |
| 1075 | * putback: charge: |
| 1076 | * SetPageLRU SetPageCgroupUsed |
| 1077 | * smp_mb smp_mb |
| 1078 | * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU |
| 1079 | * |
| 1080 | * Ensure that one of the two sides adds the page to the memcg |
| 1081 | * LRU during a race. |
| 1082 | */ |
| 1083 | smp_mb(); |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1084 | /* taking care of that the page is added to LRU while we commit it */ |
| 1085 | if (likely(!PageLRU(page))) |
| 1086 | return; |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1087 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 1088 | /* link when the page is linked to LRU but page_cgroup isn't */ |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 1089 | if (PageLRU(page) && !PageCgroupAcctLRU(pc)) |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1090 | mem_cgroup_add_lru_list(page, page_lru(page)); |
| 1091 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
| 1092 | } |
| 1093 | |
| 1094 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1095 | void mem_cgroup_move_lists(struct page *page, |
| 1096 | enum lru_list from, enum lru_list to) |
| 1097 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1098 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1099 | return; |
| 1100 | mem_cgroup_del_lru_list(page, from); |
| 1101 | mem_cgroup_add_lru_list(page, to); |
| 1102 | } |
| 1103 | |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1104 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1105 | * 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] | 1106 | * hierarchy subtree |
| 1107 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1108 | static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg, |
| 1109 | struct mem_cgroup *memcg) |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1110 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1111 | if (root_memcg != memcg) { |
| 1112 | return (root_memcg->use_hierarchy && |
| 1113 | css_is_ancestor(&memcg->css, &root_memcg->css)); |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | return true; |
| 1117 | } |
| 1118 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1119 | 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] | 1120 | { |
| 1121 | int ret; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1122 | struct mem_cgroup *curr = NULL; |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1123 | struct task_struct *p; |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1124 | |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1125 | p = find_lock_task_mm(task); |
| 1126 | if (!p) |
| 1127 | return 0; |
| 1128 | curr = try_get_mem_cgroup_from_mm(p->mm); |
| 1129 | task_unlock(p); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1130 | if (!curr) |
| 1131 | return 0; |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1132 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1133 | * We should check use_hierarchy of "memcg" not "curr". Because checking |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1134 | * 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] | 1135 | * enabled in "curr" and "curr" is a child of "memcg" in *cgroup* |
| 1136 | * hierarchy(even if use_hierarchy is disabled in "memcg"). |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1137 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1138 | ret = mem_cgroup_same_or_subtree(memcg, curr); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1139 | css_put(&curr->css); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1140 | return ret; |
| 1141 | } |
| 1142 | |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1143 | int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg, struct zone *zone) |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1144 | { |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1145 | unsigned long inactive_ratio; |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1146 | int nid = zone_to_nid(zone); |
| 1147 | int zid = zone_idx(zone); |
| 1148 | unsigned long inactive; |
| 1149 | unsigned long active; |
| 1150 | unsigned long gb; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1151 | |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1152 | inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid, |
| 1153 | BIT(LRU_INACTIVE_ANON)); |
| 1154 | active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid, |
| 1155 | BIT(LRU_ACTIVE_ANON)); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1156 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1157 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
| 1158 | if (gb) |
| 1159 | inactive_ratio = int_sqrt(10 * gb); |
| 1160 | else |
| 1161 | inactive_ratio = 1; |
| 1162 | |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1163 | return inactive * inactive_ratio < active; |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1164 | } |
| 1165 | |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1166 | int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg, struct zone *zone) |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1167 | { |
| 1168 | unsigned long active; |
| 1169 | unsigned long inactive; |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1170 | int zid = zone_idx(zone); |
| 1171 | int nid = zone_to_nid(zone); |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1172 | |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1173 | inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid, |
| 1174 | BIT(LRU_INACTIVE_FILE)); |
| 1175 | active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid, |
| 1176 | BIT(LRU_ACTIVE_FILE)); |
Rik van Riel | 56e49d2 | 2009-06-16 15:32:28 -0700 | [diff] [blame] | 1177 | |
| 1178 | return (active > inactive); |
| 1179 | } |
| 1180 | |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1181 | struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg, |
| 1182 | struct zone *zone) |
| 1183 | { |
KOSAKI Motohiro | 13d7e3a | 2010-08-10 18:03:06 -0700 | [diff] [blame] | 1184 | int nid = zone_to_nid(zone); |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1185 | int zid = zone_idx(zone); |
| 1186 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 1187 | |
| 1188 | return &mz->reclaim_stat; |
| 1189 | } |
| 1190 | |
| 1191 | struct zone_reclaim_stat * |
| 1192 | mem_cgroup_get_reclaim_stat_from_page(struct page *page) |
| 1193 | { |
| 1194 | struct page_cgroup *pc; |
| 1195 | struct mem_cgroup_per_zone *mz; |
| 1196 | |
| 1197 | if (mem_cgroup_disabled()) |
| 1198 | return NULL; |
| 1199 | |
| 1200 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 1201 | if (!PageCgroupUsed(pc)) |
| 1202 | return NULL; |
Johannes Weiner | 713735b4 | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 1203 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 1204 | smp_rmb(); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1205 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1206 | return &mz->reclaim_stat; |
| 1207 | } |
| 1208 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1209 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 1210 | struct list_head *dst, |
| 1211 | unsigned long *scanned, int order, |
Minchan Kim | 4356f21 | 2011-10-31 17:06:47 -0700 | [diff] [blame] | 1212 | isolate_mode_t mode, |
| 1213 | struct zone *z, |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1214 | struct mem_cgroup *mem_cont, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1215 | int active, int file) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1216 | { |
| 1217 | unsigned long nr_taken = 0; |
| 1218 | struct page *page; |
| 1219 | unsigned long scan; |
| 1220 | LIST_HEAD(pc_list); |
| 1221 | struct list_head *src; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1222 | struct page_cgroup *pc, *tmp; |
KOSAKI Motohiro | 13d7e3a | 2010-08-10 18:03:06 -0700 | [diff] [blame] | 1223 | int nid = zone_to_nid(z); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1224 | int zid = zone_idx(z); |
| 1225 | struct mem_cgroup_per_zone *mz; |
Johannes Weiner | b7c46d1 | 2009-09-21 17:02:56 -0700 | [diff] [blame] | 1226 | int lru = LRU_FILE * file + active; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1227 | int ret; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1228 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 1229 | BUG_ON(!mem_cont); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1230 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1231 | src = &mz->lists[lru]; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1232 | |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1233 | scan = 0; |
| 1234 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1235 | if (scan >= nr_to_scan) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1236 | break; |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1237 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1238 | if (unlikely(!PageCgroupUsed(pc))) |
| 1239 | continue; |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 1240 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 1241 | page = lookup_cgroup_page(pc); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 1242 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1243 | if (unlikely(!PageLRU(page))) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1244 | continue; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1245 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1246 | scan++; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1247 | ret = __isolate_lru_page(page, mode, file); |
| 1248 | switch (ret) { |
| 1249 | case 0: |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1250 | list_move(&page->lru, dst); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1251 | mem_cgroup_del_lru(page); |
Rik van Riel | 2c888cf | 2011-01-13 15:47:13 -0800 | [diff] [blame] | 1252 | nr_taken += hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1253 | break; |
| 1254 | case -EBUSY: |
| 1255 | /* we don't affect global LRU but rotate in our LRU */ |
| 1256 | mem_cgroup_rotate_lru_list(page, page_lru(page)); |
| 1257 | break; |
| 1258 | default: |
| 1259 | break; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1263 | *scanned = scan; |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 1264 | |
| 1265 | trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken, |
| 1266 | 0, 0, 0, mode); |
| 1267 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1268 | return nr_taken; |
| 1269 | } |
| 1270 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1271 | #define mem_cgroup_from_res_counter(counter, member) \ |
| 1272 | container_of(counter, struct mem_cgroup, member) |
| 1273 | |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1274 | /** |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1275 | * mem_cgroup_margin - calculate chargeable space of a memory cgroup |
| 1276 | * @mem: the memory cgroup |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1277 | * |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1278 | * Returns the maximum amount of memory @mem can be charged with, in |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1279 | * pages. |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1280 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1281 | static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1282 | { |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1283 | unsigned long long margin; |
| 1284 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1285 | margin = res_counter_margin(&memcg->res); |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1286 | if (do_swap_account) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1287 | margin = min(margin, res_counter_margin(&memcg->memsw)); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1288 | return margin >> PAGE_SHIFT; |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1289 | } |
| 1290 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 1291 | int mem_cgroup_swappiness(struct mem_cgroup *memcg) |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1292 | { |
| 1293 | struct cgroup *cgrp = memcg->css.cgroup; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1294 | |
| 1295 | /* root ? */ |
| 1296 | if (cgrp->parent == NULL) |
| 1297 | return vm_swappiness; |
| 1298 | |
Johannes Weiner | bf1ff26 | 2011-03-23 16:42:32 -0700 | [diff] [blame] | 1299 | return memcg->swappiness; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1302 | static void mem_cgroup_start_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1303 | { |
| 1304 | int cpu; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1305 | |
| 1306 | get_online_cpus(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1307 | spin_lock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1308 | for_each_online_cpu(cpu) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1309 | per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1; |
| 1310 | memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1; |
| 1311 | spin_unlock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1312 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1313 | |
| 1314 | synchronize_rcu(); |
| 1315 | } |
| 1316 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1317 | static void mem_cgroup_end_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1318 | { |
| 1319 | int cpu; |
| 1320 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1321 | if (!memcg) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1322 | return; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1323 | get_online_cpus(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1324 | spin_lock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1325 | for_each_online_cpu(cpu) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1326 | per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1; |
| 1327 | memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1; |
| 1328 | spin_unlock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1329 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1330 | } |
| 1331 | /* |
| 1332 | * 2 routines for checking "mem" is under move_account() or not. |
| 1333 | * |
| 1334 | * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used |
| 1335 | * for avoiding race in accounting. If true, |
| 1336 | * pc->mem_cgroup may be overwritten. |
| 1337 | * |
| 1338 | * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or |
| 1339 | * under hierarchy of moving cgroups. This is for |
| 1340 | * waiting at hith-memory prressure caused by "move". |
| 1341 | */ |
| 1342 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1343 | static bool mem_cgroup_stealed(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1344 | { |
| 1345 | VM_BUG_ON(!rcu_read_lock_held()); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1346 | return this_cpu_read(memcg->stat->count[MEM_CGROUP_ON_MOVE]) > 0; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1347 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1348 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1349 | static bool mem_cgroup_under_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1350 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1351 | struct mem_cgroup *from; |
| 1352 | struct mem_cgroup *to; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1353 | bool ret = false; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1354 | /* |
| 1355 | * Unlike task_move routines, we access mc.to, mc.from not under |
| 1356 | * mutual exclusion by cgroup_mutex. Here, we take spinlock instead. |
| 1357 | */ |
| 1358 | spin_lock(&mc.lock); |
| 1359 | from = mc.from; |
| 1360 | to = mc.to; |
| 1361 | if (!from) |
| 1362 | goto unlock; |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1363 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1364 | ret = mem_cgroup_same_or_subtree(memcg, from) |
| 1365 | || mem_cgroup_same_or_subtree(memcg, to); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1366 | unlock: |
| 1367 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1368 | return ret; |
| 1369 | } |
| 1370 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1371 | static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1372 | { |
| 1373 | if (mc.moving_task && current != mc.moving_task) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1374 | if (mem_cgroup_under_move(memcg)) { |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1375 | DEFINE_WAIT(wait); |
| 1376 | prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE); |
| 1377 | /* moving charge context might have finished. */ |
| 1378 | if (mc.moving_task) |
| 1379 | schedule(); |
| 1380 | finish_wait(&mc.waitq, &wait); |
| 1381 | return true; |
| 1382 | } |
| 1383 | } |
| 1384 | return false; |
| 1385 | } |
| 1386 | |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1387 | /** |
Kirill A. Shutemov | 6a6135b | 2010-03-10 15:22:25 -0800 | [diff] [blame] | 1388 | * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode. |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1389 | * @memcg: The memory cgroup that went over limit |
| 1390 | * @p: Task that is going to be killed |
| 1391 | * |
| 1392 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is |
| 1393 | * enabled |
| 1394 | */ |
| 1395 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) |
| 1396 | { |
| 1397 | struct cgroup *task_cgrp; |
| 1398 | struct cgroup *mem_cgrp; |
| 1399 | /* |
| 1400 | * Need a buffer in BSS, can't rely on allocations. The code relies |
| 1401 | * on the assumption that OOM is serialized for memory controller. |
| 1402 | * If this assumption is broken, revisit this code. |
| 1403 | */ |
| 1404 | static char memcg_name[PATH_MAX]; |
| 1405 | int ret; |
| 1406 | |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1407 | if (!memcg || !p) |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1408 | return; |
| 1409 | |
| 1410 | |
| 1411 | rcu_read_lock(); |
| 1412 | |
| 1413 | mem_cgrp = memcg->css.cgroup; |
| 1414 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); |
| 1415 | |
| 1416 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); |
| 1417 | if (ret < 0) { |
| 1418 | /* |
| 1419 | * Unfortunately, we are unable to convert to a useful name |
| 1420 | * But we'll still print out the usage information |
| 1421 | */ |
| 1422 | rcu_read_unlock(); |
| 1423 | goto done; |
| 1424 | } |
| 1425 | rcu_read_unlock(); |
| 1426 | |
| 1427 | printk(KERN_INFO "Task in %s killed", memcg_name); |
| 1428 | |
| 1429 | rcu_read_lock(); |
| 1430 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); |
| 1431 | if (ret < 0) { |
| 1432 | rcu_read_unlock(); |
| 1433 | goto done; |
| 1434 | } |
| 1435 | rcu_read_unlock(); |
| 1436 | |
| 1437 | /* |
| 1438 | * Continues from above, so we don't need an KERN_ level |
| 1439 | */ |
| 1440 | printk(KERN_CONT " as a result of limit of %s\n", memcg_name); |
| 1441 | done: |
| 1442 | |
| 1443 | printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n", |
| 1444 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, |
| 1445 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, |
| 1446 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); |
| 1447 | printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, " |
| 1448 | "failcnt %llu\n", |
| 1449 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, |
| 1450 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, |
| 1451 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); |
| 1452 | } |
| 1453 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1454 | /* |
| 1455 | * This function returns the number of memcg under hierarchy tree. Returns |
| 1456 | * 1(self count) if no children. |
| 1457 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1458 | static int mem_cgroup_count_children(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1459 | { |
| 1460 | int num = 0; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1461 | struct mem_cgroup *iter; |
| 1462 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1463 | for_each_mem_cgroup_tree(iter, memcg) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1464 | num++; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1465 | return num; |
| 1466 | } |
| 1467 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1468 | /* |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1469 | * Return the memory (and swap, if configured) limit for a memcg. |
| 1470 | */ |
| 1471 | u64 mem_cgroup_get_limit(struct mem_cgroup *memcg) |
| 1472 | { |
| 1473 | u64 limit; |
| 1474 | u64 memsw; |
| 1475 | |
Johannes Weiner | f3e8eb7 | 2011-01-13 15:47:39 -0800 | [diff] [blame] | 1476 | limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 1477 | limit += total_swap_pages << PAGE_SHIFT; |
| 1478 | |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1479 | memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 1480 | /* |
| 1481 | * If memsw is finite and limits the amount of swap space available |
| 1482 | * to this memcg, return that limit. |
| 1483 | */ |
| 1484 | return min(limit, memsw); |
| 1485 | } |
| 1486 | |
| 1487 | /* |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1488 | * Visit the first child (need not be the first child as per the ordering |
| 1489 | * of the cgroup list, since we track last_scanned_child) of @mem and use |
| 1490 | * that to reclaim free pages from. |
| 1491 | */ |
| 1492 | static struct mem_cgroup * |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1493 | mem_cgroup_select_victim(struct mem_cgroup *root_memcg) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1494 | { |
| 1495 | struct mem_cgroup *ret = NULL; |
| 1496 | struct cgroup_subsys_state *css; |
| 1497 | int nextid, found; |
| 1498 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1499 | if (!root_memcg->use_hierarchy) { |
| 1500 | css_get(&root_memcg->css); |
| 1501 | ret = root_memcg; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | while (!ret) { |
| 1505 | rcu_read_lock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1506 | nextid = root_memcg->last_scanned_child + 1; |
| 1507 | css = css_get_next(&mem_cgroup_subsys, nextid, &root_memcg->css, |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1508 | &found); |
| 1509 | if (css && css_tryget(css)) |
| 1510 | ret = container_of(css, struct mem_cgroup, css); |
| 1511 | |
| 1512 | rcu_read_unlock(); |
| 1513 | /* Updates scanning parameter */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1514 | if (!css) { |
| 1515 | /* this means start scan from ID:1 */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1516 | root_memcg->last_scanned_child = 0; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1517 | } else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1518 | root_memcg->last_scanned_child = found; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | return ret; |
| 1522 | } |
| 1523 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1524 | /** |
| 1525 | * test_mem_cgroup_node_reclaimable |
| 1526 | * @mem: the target memcg |
| 1527 | * @nid: the node ID to be checked. |
| 1528 | * @noswap : specify true here if the user wants flle only information. |
| 1529 | * |
| 1530 | * This function returns whether the specified memcg contains any |
| 1531 | * reclaimable pages on a node. Returns true if there are any reclaimable |
| 1532 | * pages in the node. |
| 1533 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1534 | static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1535 | int nid, bool noswap) |
| 1536 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1537 | if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1538 | return true; |
| 1539 | if (noswap || !total_swap_pages) |
| 1540 | return false; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1541 | if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1542 | return true; |
| 1543 | return false; |
| 1544 | |
| 1545 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1546 | #if MAX_NUMNODES > 1 |
| 1547 | |
| 1548 | /* |
| 1549 | * Always updating the nodemask is not very good - even if we have an empty |
| 1550 | * list or the wrong list here, we can start from some node and traverse all |
| 1551 | * nodes based on the zonelist. So update the list loosely once per 10 secs. |
| 1552 | * |
| 1553 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1554 | static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1555 | { |
| 1556 | int nid; |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1557 | /* |
| 1558 | * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET |
| 1559 | * pagein/pageout changes since the last update. |
| 1560 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1561 | if (!atomic_read(&memcg->numainfo_events)) |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1562 | return; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1563 | if (atomic_inc_return(&memcg->numainfo_updating) > 1) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1564 | return; |
| 1565 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1566 | /* make a nodemask where this memcg uses memory from */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1567 | memcg->scan_nodes = node_states[N_HIGH_MEMORY]; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1568 | |
| 1569 | for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) { |
| 1570 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1571 | if (!test_mem_cgroup_node_reclaimable(memcg, nid, false)) |
| 1572 | node_clear(nid, memcg->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1573 | } |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1574 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1575 | atomic_set(&memcg->numainfo_events, 0); |
| 1576 | atomic_set(&memcg->numainfo_updating, 0); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /* |
| 1580 | * Selecting a node where we start reclaim from. Because what we need is just |
| 1581 | * reducing usage counter, start from anywhere is O,K. Considering |
| 1582 | * memory reclaim from current node, there are pros. and cons. |
| 1583 | * |
| 1584 | * Freeing memory from current node means freeing memory from a node which |
| 1585 | * we'll use or we've used. So, it may make LRU bad. And if several threads |
| 1586 | * hit limits, it will see a contention on a node. But freeing from remote |
| 1587 | * node means more costs for memory reclaim because of memory latency. |
| 1588 | * |
| 1589 | * Now, we use round-robin. Better algorithm is welcomed. |
| 1590 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1591 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1592 | { |
| 1593 | int node; |
| 1594 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1595 | mem_cgroup_may_update_nodemask(memcg); |
| 1596 | node = memcg->last_scanned_node; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1597 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1598 | node = next_node(node, memcg->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1599 | if (node == MAX_NUMNODES) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1600 | node = first_node(memcg->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1601 | /* |
| 1602 | * We call this when we hit limit, not when pages are added to LRU. |
| 1603 | * No LRU may hold pages because all pages are UNEVICTABLE or |
| 1604 | * memcg is too small and all pages are not on LRU. In that case, |
| 1605 | * we use curret node. |
| 1606 | */ |
| 1607 | if (unlikely(node == MAX_NUMNODES)) |
| 1608 | node = numa_node_id(); |
| 1609 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1610 | memcg->last_scanned_node = node; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1611 | return node; |
| 1612 | } |
| 1613 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1614 | /* |
| 1615 | * Check all nodes whether it contains reclaimable pages or not. |
| 1616 | * For quick scan, we make use of scan_nodes. This will allow us to skip |
| 1617 | * unused nodes. But scan_nodes is lazily updated and may not cotain |
| 1618 | * enough new information. We need to do double check. |
| 1619 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1620 | bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1621 | { |
| 1622 | int nid; |
| 1623 | |
| 1624 | /* |
| 1625 | * quick check...making use of scan_node. |
| 1626 | * We can skip unused nodes. |
| 1627 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1628 | if (!nodes_empty(memcg->scan_nodes)) { |
| 1629 | for (nid = first_node(memcg->scan_nodes); |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1630 | nid < MAX_NUMNODES; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1631 | nid = next_node(nid, memcg->scan_nodes)) { |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1632 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1633 | if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1634 | return true; |
| 1635 | } |
| 1636 | } |
| 1637 | /* |
| 1638 | * Check rest of nodes. |
| 1639 | */ |
| 1640 | for_each_node_state(nid, N_HIGH_MEMORY) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1641 | if (node_isset(nid, memcg->scan_nodes)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1642 | continue; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1643 | if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1644 | return true; |
| 1645 | } |
| 1646 | return false; |
| 1647 | } |
| 1648 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1649 | #else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1650 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1651 | { |
| 1652 | return 0; |
| 1653 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1654 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1655 | bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1656 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1657 | return test_mem_cgroup_node_reclaimable(memcg, 0, noswap); |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1658 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1659 | #endif |
| 1660 | |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1661 | /* |
| 1662 | * Scan the hierarchy if needed to reclaim memory. We remember the last child |
| 1663 | * we reclaimed from, so that we don't end up penalizing one child extensively |
| 1664 | * based on its position in the children list. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1665 | * |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1666 | * root_memcg is the original ancestor that we've been reclaim from. |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1667 | * |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1668 | * We give up and return to the caller when we visit root_memcg twice. |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1669 | * (other groups can be removed while we're walking....) |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1670 | * |
| 1671 | * If shrink==true, for avoiding to free too much, this returns immedieately. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1672 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1673 | static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_memcg, |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1674 | struct zone *zone, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1675 | gfp_t gfp_mask, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1676 | unsigned long reclaim_options, |
| 1677 | unsigned long *total_scanned) |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1678 | { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1679 | struct mem_cgroup *victim; |
| 1680 | int ret, total = 0; |
| 1681 | int loop = 0; |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1682 | bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP; |
| 1683 | bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1684 | bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1685 | unsigned long excess; |
Johannes Weiner | 185efc0 | 2011-09-14 16:21:58 -0700 | [diff] [blame] | 1686 | unsigned long nr_scanned; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1687 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1688 | excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1689 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1690 | /* If memsw_is_minimum==1, swap-out is of-no-use. */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1691 | if (!check_soft && !shrink && root_memcg->memsw_is_minimum) |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1692 | noswap = true; |
| 1693 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1694 | while (1) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1695 | victim = mem_cgroup_select_victim(root_memcg); |
| 1696 | if (victim == root_memcg) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1697 | loop++; |
KAMEZAWA Hiroyuki | fbc29a2 | 2011-06-15 15:08:46 -0700 | [diff] [blame] | 1698 | /* |
| 1699 | * We are not draining per cpu cached charges during |
| 1700 | * soft limit reclaim because global reclaim doesn't |
| 1701 | * care about charges. It tries to free some memory and |
| 1702 | * charges will not give any. |
| 1703 | */ |
| 1704 | if (!check_soft && loop >= 1) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1705 | drain_all_stock_async(root_memcg); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1706 | if (loop >= 2) { |
| 1707 | /* |
| 1708 | * If we have not been able to reclaim |
| 1709 | * anything, it might because there are |
| 1710 | * no reclaimable pages under this hierarchy |
| 1711 | */ |
| 1712 | if (!check_soft || !total) { |
| 1713 | css_put(&victim->css); |
| 1714 | break; |
| 1715 | } |
| 1716 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1717 | * We want to do more targeted reclaim. |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1718 | * excess >> 2 is not to excessive so as to |
| 1719 | * reclaim too much, nor too less that we keep |
| 1720 | * coming back to reclaim from this cgroup |
| 1721 | */ |
| 1722 | if (total >= (excess >> 2) || |
| 1723 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) { |
| 1724 | css_put(&victim->css); |
| 1725 | break; |
| 1726 | } |
| 1727 | } |
| 1728 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1729 | if (!mem_cgroup_reclaimable(victim, noswap)) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1730 | /* this cgroup's local usage == 0 */ |
| 1731 | css_put(&victim->css); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1732 | continue; |
| 1733 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1734 | /* we use swappiness of local cgroup */ |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1735 | if (check_soft) { |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1736 | ret = mem_cgroup_shrink_node_zone(victim, gfp_mask, |
Johannes Weiner | 185efc0 | 2011-09-14 16:21:58 -0700 | [diff] [blame] | 1737 | noswap, zone, &nr_scanned); |
| 1738 | *total_scanned += nr_scanned; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1739 | } else |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1740 | ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, |
Johannes Weiner | 185efc0 | 2011-09-14 16:21:58 -0700 | [diff] [blame] | 1741 | noswap); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1742 | css_put(&victim->css); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1743 | /* |
| 1744 | * At shrinking usage, we can't check we should stop here or |
| 1745 | * reclaim more. It's depends on callers. last_scanned_child |
| 1746 | * will work enough for keeping fairness under tree. |
| 1747 | */ |
| 1748 | if (shrink) |
| 1749 | return ret; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1750 | total += ret; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1751 | if (check_soft) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1752 | if (!res_counter_soft_limit_excess(&root_memcg->res)) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1753 | return total; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1754 | } else if (mem_cgroup_margin(root_memcg)) |
Johannes Weiner | 4fd14eb | 2011-05-26 16:25:35 -0700 | [diff] [blame] | 1755 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1756 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1757 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1758 | } |
| 1759 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1760 | /* |
| 1761 | * Check OOM-Killer is already running under our hierarchy. |
| 1762 | * If someone is running, return false. |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1763 | * Has to be called with memcg_oom_lock |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1764 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1765 | static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1766 | { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1767 | struct mem_cgroup *iter, *failed = NULL; |
| 1768 | bool cond = true; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1769 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1770 | for_each_mem_cgroup_tree_cond(iter, memcg, cond) { |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 1771 | if (iter->oom_lock) { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1772 | /* |
| 1773 | * this subtree of our hierarchy is already locked |
| 1774 | * so we cannot give a lock. |
| 1775 | */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1776 | failed = iter; |
| 1777 | cond = false; |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 1778 | } else |
| 1779 | iter->oom_lock = true; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1780 | } |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1781 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1782 | if (!failed) |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 1783 | return true; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1784 | |
| 1785 | /* |
| 1786 | * OK, we failed to lock the whole subtree so we have to clean up |
| 1787 | * what we set up to the failing subtree |
| 1788 | */ |
| 1789 | cond = true; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1790 | for_each_mem_cgroup_tree_cond(iter, memcg, cond) { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1791 | if (iter == failed) { |
| 1792 | cond = false; |
| 1793 | continue; |
| 1794 | } |
| 1795 | iter->oom_lock = false; |
| 1796 | } |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 1797 | return false; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1798 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1799 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1800 | /* |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1801 | * Has to be called with memcg_oom_lock |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1802 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1803 | static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1804 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1805 | struct mem_cgroup *iter; |
| 1806 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1807 | for_each_mem_cgroup_tree(iter, memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1808 | iter->oom_lock = false; |
| 1809 | return 0; |
| 1810 | } |
| 1811 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1812 | static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1813 | { |
| 1814 | struct mem_cgroup *iter; |
| 1815 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1816 | for_each_mem_cgroup_tree(iter, memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1817 | atomic_inc(&iter->under_oom); |
| 1818 | } |
| 1819 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1820 | static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1821 | { |
| 1822 | struct mem_cgroup *iter; |
| 1823 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1824 | /* |
| 1825 | * When a new child is created while the hierarchy is under oom, |
| 1826 | * mem_cgroup_oom_lock() may not be called. We have to use |
| 1827 | * atomic_add_unless() here. |
| 1828 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1829 | for_each_mem_cgroup_tree(iter, memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1830 | atomic_add_unless(&iter->under_oom, -1, 0); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1833 | static DEFINE_SPINLOCK(memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1834 | static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq); |
| 1835 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1836 | struct oom_wait_info { |
| 1837 | struct mem_cgroup *mem; |
| 1838 | wait_queue_t wait; |
| 1839 | }; |
| 1840 | |
| 1841 | static int memcg_oom_wake_function(wait_queue_t *wait, |
| 1842 | unsigned mode, int sync, void *arg) |
| 1843 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1844 | struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg, |
| 1845 | *oom_wait_memcg; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1846 | struct oom_wait_info *oom_wait_info; |
| 1847 | |
| 1848 | oom_wait_info = container_of(wait, struct oom_wait_info, wait); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1849 | oom_wait_memcg = oom_wait_info->mem; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1850 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1851 | /* |
| 1852 | * Both of oom_wait_info->mem and wake_mem are stable under us. |
| 1853 | * Then we can use css_is_ancestor without taking care of RCU. |
| 1854 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1855 | if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg) |
| 1856 | && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg)) |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1857 | return 0; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1858 | return autoremove_wake_function(wait, mode, sync, arg); |
| 1859 | } |
| 1860 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1861 | static void memcg_wakeup_oom(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1862 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1863 | /* for filtering, pass "memcg" as argument. */ |
| 1864 | __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1865 | } |
| 1866 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1867 | static void memcg_oom_recover(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1868 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1869 | if (memcg && atomic_read(&memcg->under_oom)) |
| 1870 | memcg_wakeup_oom(memcg); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1871 | } |
| 1872 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1873 | /* |
| 1874 | * try to call OOM killer. returns false if we should exit memory-reclaim loop. |
| 1875 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1876 | bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask) |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1877 | { |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1878 | struct oom_wait_info owait; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1879 | bool locked, need_to_kill; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1880 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1881 | owait.mem = memcg; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1882 | owait.wait.flags = 0; |
| 1883 | owait.wait.func = memcg_oom_wake_function; |
| 1884 | owait.wait.private = current; |
| 1885 | INIT_LIST_HEAD(&owait.wait.task_list); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1886 | need_to_kill = true; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1887 | mem_cgroup_mark_under_oom(memcg); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1888 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1889 | /* At first, try to OOM lock hierarchy under memcg.*/ |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1890 | spin_lock(&memcg_oom_lock); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1891 | locked = mem_cgroup_oom_lock(memcg); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1892 | /* |
| 1893 | * Even if signal_pending(), we can't quit charge() loop without |
| 1894 | * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL |
| 1895 | * under OOM is always welcomed, use TASK_KILLABLE here. |
| 1896 | */ |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1897 | prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1898 | if (!locked || memcg->oom_kill_disable) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1899 | need_to_kill = false; |
| 1900 | if (locked) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1901 | mem_cgroup_oom_notify(memcg); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1902 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1903 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1904 | if (need_to_kill) { |
| 1905 | finish_wait(&memcg_oom_waitq, &owait.wait); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1906 | mem_cgroup_out_of_memory(memcg, mask); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1907 | } else { |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1908 | schedule(); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1909 | finish_wait(&memcg_oom_waitq, &owait.wait); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1910 | } |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1911 | spin_lock(&memcg_oom_lock); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1912 | if (locked) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1913 | mem_cgroup_oom_unlock(memcg); |
| 1914 | memcg_wakeup_oom(memcg); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1915 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1916 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1917 | mem_cgroup_unmark_under_oom(memcg); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1918 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1919 | if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current)) |
| 1920 | return false; |
| 1921 | /* Give chance to dying process */ |
KAMEZAWA Hiroyuki | 715a5ee | 2011-11-02 13:38:18 -0700 | [diff] [blame] | 1922 | schedule_timeout_uninterruptible(1); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1923 | return true; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1924 | } |
| 1925 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1926 | /* |
| 1927 | * Currently used to update mapped file statistics, but the routine can be |
| 1928 | * generalized to update other statistics as well. |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1929 | * |
| 1930 | * Notes: Race condition |
| 1931 | * |
| 1932 | * We usually use page_cgroup_lock() for accessing page_cgroup member but |
| 1933 | * it tends to be costly. But considering some conditions, we doesn't need |
| 1934 | * to do so _always_. |
| 1935 | * |
| 1936 | * Considering "charge", lock_page_cgroup() is not required because all |
| 1937 | * file-stat operations happen after a page is attached to radix-tree. There |
| 1938 | * are no race with "charge". |
| 1939 | * |
| 1940 | * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup |
| 1941 | * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even |
| 1942 | * if there are race with "uncharge". Statistics itself is properly handled |
| 1943 | * by flags. |
| 1944 | * |
| 1945 | * Considering "move", this is an only case we see a race. To make the race |
| 1946 | * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are |
| 1947 | * possibility of race condition. If there is, we take a lock. |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1948 | */ |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1949 | |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1950 | void mem_cgroup_update_page_stat(struct page *page, |
| 1951 | enum mem_cgroup_page_stat_item idx, int val) |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1952 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1953 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1954 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 1955 | bool need_unlock = false; |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 1956 | unsigned long uninitialized_var(flags); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1957 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1958 | if (unlikely(!pc)) |
| 1959 | return; |
| 1960 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1961 | rcu_read_lock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1962 | memcg = pc->mem_cgroup; |
| 1963 | if (unlikely(!memcg || !PageCgroupUsed(pc))) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1964 | goto out; |
| 1965 | /* pc->mem_cgroup is unstable ? */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1966 | if (unlikely(mem_cgroup_stealed(memcg)) || PageTransHuge(page)) { |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1967 | /* take a lock against to access pc->mem_cgroup */ |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 1968 | move_lock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1969 | need_unlock = true; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1970 | memcg = pc->mem_cgroup; |
| 1971 | if (!memcg || !PageCgroupUsed(pc)) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1972 | goto out; |
| 1973 | } |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1974 | |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1975 | switch (idx) { |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1976 | case MEMCG_NR_FILE_MAPPED: |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1977 | if (val > 0) |
| 1978 | SetPageCgroupFileMapped(pc); |
| 1979 | else if (!page_mapped(page)) |
KAMEZAWA Hiroyuki | 0c270f8 | 2010-10-27 15:33:39 -0700 | [diff] [blame] | 1980 | ClearPageCgroupFileMapped(pc); |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1981 | idx = MEM_CGROUP_STAT_FILE_MAPPED; |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1982 | break; |
| 1983 | default: |
| 1984 | BUG(); |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 1985 | } |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1986 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1987 | this_cpu_add(memcg->stat->count[idx], val); |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1988 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1989 | out: |
| 1990 | if (unlikely(need_unlock)) |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 1991 | move_unlock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1992 | rcu_read_unlock(); |
| 1993 | return; |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1994 | } |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1995 | EXPORT_SYMBOL(mem_cgroup_update_page_stat); |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1996 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1997 | /* |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1998 | * size of first charge trial. "32" comes from vmscan.c's magic value. |
| 1999 | * TODO: maybe necessary to use big numbers in big irons. |
| 2000 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2001 | #define CHARGE_BATCH 32U |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2002 | struct memcg_stock_pcp { |
| 2003 | struct mem_cgroup *cached; /* this never be root cgroup */ |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2004 | unsigned int nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2005 | struct work_struct work; |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2006 | unsigned long flags; |
| 2007 | #define FLUSHING_CACHED_CHARGE (0) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2008 | }; |
| 2009 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2010 | static DEFINE_MUTEX(percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2011 | |
| 2012 | /* |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2013 | * Try to consume stocked charge on this cpu. If success, one page is consumed |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2014 | * from local stock and true is returned. If the stock is 0 or charges from a |
| 2015 | * cgroup which is not current target, returns false. This stock will be |
| 2016 | * refilled. |
| 2017 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2018 | static bool consume_stock(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2019 | { |
| 2020 | struct memcg_stock_pcp *stock; |
| 2021 | bool ret = true; |
| 2022 | |
| 2023 | stock = &get_cpu_var(memcg_stock); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2024 | if (memcg == stock->cached && stock->nr_pages) |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2025 | stock->nr_pages--; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2026 | else /* need to call res_counter_charge */ |
| 2027 | ret = false; |
| 2028 | put_cpu_var(memcg_stock); |
| 2029 | return ret; |
| 2030 | } |
| 2031 | |
| 2032 | /* |
| 2033 | * Returns stocks cached in percpu to res_counter and reset cached information. |
| 2034 | */ |
| 2035 | static void drain_stock(struct memcg_stock_pcp *stock) |
| 2036 | { |
| 2037 | struct mem_cgroup *old = stock->cached; |
| 2038 | |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2039 | if (stock->nr_pages) { |
| 2040 | unsigned long bytes = stock->nr_pages * PAGE_SIZE; |
| 2041 | |
| 2042 | res_counter_uncharge(&old->res, bytes); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2043 | if (do_swap_account) |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2044 | res_counter_uncharge(&old->memsw, bytes); |
| 2045 | stock->nr_pages = 0; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2046 | } |
| 2047 | stock->cached = NULL; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | /* |
| 2051 | * This must be called under preempt disabled or must be called by |
| 2052 | * a thread which is pinned to local cpu. |
| 2053 | */ |
| 2054 | static void drain_local_stock(struct work_struct *dummy) |
| 2055 | { |
| 2056 | struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock); |
| 2057 | drain_stock(stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2058 | clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | /* |
| 2062 | * 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] | 2063 | * This will be consumed by consume_stock() function, later. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2064 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2065 | static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2066 | { |
| 2067 | struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock); |
| 2068 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2069 | if (stock->cached != memcg) { /* reset if necessary */ |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2070 | drain_stock(stock); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2071 | stock->cached = memcg; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2072 | } |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2073 | stock->nr_pages += nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2074 | put_cpu_var(memcg_stock); |
| 2075 | } |
| 2076 | |
| 2077 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2078 | * Drains all per-CPU charge caches for given root_memcg resp. subtree |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2079 | * of the hierarchy under it. sync flag says whether we should block |
| 2080 | * until the work is done. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2081 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2082 | static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2083 | { |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2084 | int cpu, curcpu; |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2085 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2086 | /* Notify other cpus that system-wide "drain" is running */ |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2087 | get_online_cpus(); |
Johannes Weiner | 5af12d0 | 2011-08-25 15:59:07 -0700 | [diff] [blame] | 2088 | curcpu = get_cpu(); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2089 | for_each_online_cpu(cpu) { |
| 2090 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2091 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2092 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2093 | memcg = stock->cached; |
| 2094 | if (!memcg || !stock->nr_pages) |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2095 | continue; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2096 | if (!mem_cgroup_same_or_subtree(root_memcg, memcg)) |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 2097 | continue; |
Michal Hocko | d1a05b6 | 2011-07-26 16:08:27 -0700 | [diff] [blame] | 2098 | if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) { |
| 2099 | if (cpu == curcpu) |
| 2100 | drain_local_stock(&stock->work); |
| 2101 | else |
| 2102 | schedule_work_on(cpu, &stock->work); |
| 2103 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2104 | } |
Johannes Weiner | 5af12d0 | 2011-08-25 15:59:07 -0700 | [diff] [blame] | 2105 | put_cpu(); |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2106 | |
| 2107 | if (!sync) |
| 2108 | goto out; |
| 2109 | |
| 2110 | for_each_online_cpu(cpu) { |
| 2111 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2112 | if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2113 | flush_work(&stock->work); |
| 2114 | } |
| 2115 | out: |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2116 | put_online_cpus(); |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | /* |
| 2120 | * Tries to drain stocked charges in other cpus. This function is asynchronous |
| 2121 | * and just put a work per cpu for draining localy on each cpu. Caller can |
| 2122 | * expects some charges will be back to res_counter later but cannot wait for |
| 2123 | * it. |
| 2124 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2125 | static void drain_all_stock_async(struct mem_cgroup *root_memcg) |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2126 | { |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2127 | /* |
| 2128 | * If someone calls draining, avoid adding more kworker runs. |
| 2129 | */ |
| 2130 | if (!mutex_trylock(&percpu_charge_mutex)) |
| 2131 | return; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2132 | drain_all_stock(root_memcg, false); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2133 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | /* This is a synchronous drain interface. */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2137 | static void drain_all_stock_sync(struct mem_cgroup *root_memcg) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2138 | { |
| 2139 | /* called when force_empty is called */ |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2140 | mutex_lock(&percpu_charge_mutex); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2141 | drain_all_stock(root_memcg, true); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2142 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2143 | } |
| 2144 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2145 | /* |
| 2146 | * This function drains percpu counter value from DEAD cpu and |
| 2147 | * move it to local cpu. Note that this function can be preempted. |
| 2148 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2149 | 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] | 2150 | { |
| 2151 | int i; |
| 2152 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2153 | spin_lock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2154 | for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2155 | long x = per_cpu(memcg->stat->count[i], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2156 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2157 | per_cpu(memcg->stat->count[i], cpu) = 0; |
| 2158 | memcg->nocpu_base.count[i] += x; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2159 | } |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2160 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2161 | unsigned long x = per_cpu(memcg->stat->events[i], cpu); |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2162 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2163 | per_cpu(memcg->stat->events[i], cpu) = 0; |
| 2164 | memcg->nocpu_base.events[i] += x; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2165 | } |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2166 | /* need to clear ON_MOVE value, works as a kind of lock. */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2167 | per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0; |
| 2168 | spin_unlock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2169 | } |
| 2170 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2171 | static void synchronize_mem_cgroup_on_move(struct mem_cgroup *memcg, int cpu) |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2172 | { |
| 2173 | int idx = MEM_CGROUP_ON_MOVE; |
| 2174 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2175 | spin_lock(&memcg->pcp_counter_lock); |
| 2176 | per_cpu(memcg->stat->count[idx], cpu) = memcg->nocpu_base.count[idx]; |
| 2177 | spin_unlock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb, |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2181 | unsigned long action, |
| 2182 | void *hcpu) |
| 2183 | { |
| 2184 | int cpu = (unsigned long)hcpu; |
| 2185 | struct memcg_stock_pcp *stock; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2186 | struct mem_cgroup *iter; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2187 | |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2188 | if ((action == CPU_ONLINE)) { |
| 2189 | for_each_mem_cgroup_all(iter) |
| 2190 | synchronize_mem_cgroup_on_move(iter, cpu); |
| 2191 | return NOTIFY_OK; |
| 2192 | } |
| 2193 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2194 | if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2195 | return NOTIFY_OK; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2196 | |
| 2197 | for_each_mem_cgroup_all(iter) |
| 2198 | mem_cgroup_drain_pcp_counter(iter, cpu); |
| 2199 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2200 | stock = &per_cpu(memcg_stock, cpu); |
| 2201 | drain_stock(stock); |
| 2202 | return NOTIFY_OK; |
| 2203 | } |
| 2204 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2205 | |
| 2206 | /* See __mem_cgroup_try_charge() for details */ |
| 2207 | enum { |
| 2208 | CHARGE_OK, /* success */ |
| 2209 | CHARGE_RETRY, /* need to retry but retry is not bad */ |
| 2210 | CHARGE_NOMEM, /* we can't do more. return -ENOMEM */ |
| 2211 | CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */ |
| 2212 | CHARGE_OOM_DIE, /* the current is killed because of OOM */ |
| 2213 | }; |
| 2214 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2215 | static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2216 | unsigned int nr_pages, bool oom_check) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2217 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2218 | unsigned long csize = nr_pages * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2219 | struct mem_cgroup *mem_over_limit; |
| 2220 | struct res_counter *fail_res; |
| 2221 | unsigned long flags = 0; |
| 2222 | int ret; |
| 2223 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2224 | ret = res_counter_charge(&memcg->res, csize, &fail_res); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2225 | |
| 2226 | if (likely(!ret)) { |
| 2227 | if (!do_swap_account) |
| 2228 | return CHARGE_OK; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2229 | ret = res_counter_charge(&memcg->memsw, csize, &fail_res); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2230 | if (likely(!ret)) |
| 2231 | return CHARGE_OK; |
| 2232 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2233 | res_counter_uncharge(&memcg->res, csize); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2234 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw); |
| 2235 | flags |= MEM_CGROUP_RECLAIM_NOSWAP; |
| 2236 | } else |
| 2237 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, res); |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2238 | /* |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2239 | * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch |
| 2240 | * of regular pages (CHARGE_BATCH), or a single regular page (1). |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2241 | * |
| 2242 | * Never reclaim on behalf of optional batching, retry with a |
| 2243 | * single page instead. |
| 2244 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2245 | if (nr_pages == CHARGE_BATCH) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2246 | return CHARGE_RETRY; |
| 2247 | |
| 2248 | if (!(gfp_mask & __GFP_WAIT)) |
| 2249 | return CHARGE_WOULDBLOCK; |
| 2250 | |
| 2251 | ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 2252 | gfp_mask, flags, NULL); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2253 | if (mem_cgroup_margin(mem_over_limit) >= nr_pages) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2254 | return CHARGE_RETRY; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2255 | /* |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2256 | * Even though the limit is exceeded at this point, reclaim |
| 2257 | * may have been able to free some pages. Retry the charge |
| 2258 | * before killing the task. |
| 2259 | * |
| 2260 | * Only for regular pages, though: huge pages are rather |
| 2261 | * unlikely to succeed so close to the limit, and we fall back |
| 2262 | * to regular pages anyway in case of failure. |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2263 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2264 | if (nr_pages == 1 && ret) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2265 | return CHARGE_RETRY; |
| 2266 | |
| 2267 | /* |
| 2268 | * At task move, charge accounts can be doubly counted. So, it's |
| 2269 | * better to wait until the end of task_move if something is going on. |
| 2270 | */ |
| 2271 | if (mem_cgroup_wait_acct_move(mem_over_limit)) |
| 2272 | return CHARGE_RETRY; |
| 2273 | |
| 2274 | /* If we don't need to call oom-killer at el, return immediately */ |
| 2275 | if (!oom_check) |
| 2276 | return CHARGE_NOMEM; |
| 2277 | /* check OOM */ |
| 2278 | if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask)) |
| 2279 | return CHARGE_OOM_DIE; |
| 2280 | |
| 2281 | return CHARGE_RETRY; |
| 2282 | } |
| 2283 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2284 | /* |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2285 | * Unlike exported interface, "oom" parameter is added. if oom==true, |
| 2286 | * oom-killer can be invoked. |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2287 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2288 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2289 | gfp_t gfp_mask, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2290 | unsigned int nr_pages, |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2291 | struct mem_cgroup **ptr, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2292 | bool oom) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2293 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2294 | unsigned int batch = max(CHARGE_BATCH, nr_pages); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2295 | int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2296 | struct mem_cgroup *memcg = NULL; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2297 | int ret; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2298 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2299 | /* |
| 2300 | * Unlike gloval-vm's OOM-kill, we're not in memory shortage |
| 2301 | * in system level. So, allow to go ahead dying process in addition to |
| 2302 | * MEMDIE process. |
| 2303 | */ |
| 2304 | if (unlikely(test_thread_flag(TIF_MEMDIE) |
| 2305 | || fatal_signal_pending(current))) |
| 2306 | goto bypass; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2307 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2308 | /* |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2309 | * We always charge the cgroup the mm_struct belongs to. |
| 2310 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2311 | * thread group leader migrates. It's possible that mm is not |
| 2312 | * set, if so charge the init_mm (happens for pagecache usage). |
| 2313 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2314 | if (!*ptr && !mm) |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2315 | goto bypass; |
| 2316 | again: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2317 | if (*ptr) { /* css should be a valid one */ |
| 2318 | memcg = *ptr; |
| 2319 | VM_BUG_ON(css_is_removed(&memcg->css)); |
| 2320 | if (mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2321 | goto done; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2322 | if (nr_pages == 1 && consume_stock(memcg)) |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2323 | goto done; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2324 | css_get(&memcg->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2325 | } else { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2326 | struct task_struct *p; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2327 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2328 | rcu_read_lock(); |
| 2329 | p = rcu_dereference(mm->owner); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2330 | /* |
KAMEZAWA Hiroyuki | ebb76ce | 2010-12-29 14:07:11 -0800 | [diff] [blame] | 2331 | * Because we don't have task_lock(), "p" can exit. |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2332 | * 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] | 2333 | * race with swapoff. Then, we have small risk of mis-accouning. |
| 2334 | * But such kind of mis-account by race always happens because |
| 2335 | * we don't have cgroup_mutex(). It's overkill and we allo that |
| 2336 | * small race, here. |
| 2337 | * (*) swapoff at el will charge against mm-struct not against |
| 2338 | * task-struct. So, mm->owner can be NULL. |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2339 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2340 | memcg = mem_cgroup_from_task(p); |
| 2341 | if (!memcg || mem_cgroup_is_root(memcg)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2342 | rcu_read_unlock(); |
| 2343 | goto done; |
| 2344 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2345 | if (nr_pages == 1 && consume_stock(memcg)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2346 | /* |
| 2347 | * It seems dagerous to access memcg without css_get(). |
| 2348 | * But considering how consume_stok works, it's not |
| 2349 | * necessary. If consume_stock success, some charges |
| 2350 | * from this memcg are cached on this cpu. So, we |
| 2351 | * don't need to call css_get()/css_tryget() before |
| 2352 | * calling consume_stock(). |
| 2353 | */ |
| 2354 | rcu_read_unlock(); |
| 2355 | goto done; |
| 2356 | } |
| 2357 | /* after here, we may be blocked. we need to get refcnt */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2358 | if (!css_tryget(&memcg->css)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2359 | rcu_read_unlock(); |
| 2360 | goto again; |
| 2361 | } |
| 2362 | rcu_read_unlock(); |
| 2363 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2364 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2365 | do { |
| 2366 | bool oom_check; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2367 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2368 | /* If killed, bypass charge */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2369 | if (fatal_signal_pending(current)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2370 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2371 | goto bypass; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2372 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2373 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2374 | oom_check = false; |
| 2375 | if (oom && !nr_oom_retries) { |
| 2376 | oom_check = true; |
| 2377 | nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
| 2378 | } |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 2379 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2380 | ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, oom_check); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2381 | switch (ret) { |
| 2382 | case CHARGE_OK: |
| 2383 | break; |
| 2384 | case CHARGE_RETRY: /* not in OOM situation but retry */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2385 | batch = nr_pages; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2386 | css_put(&memcg->css); |
| 2387 | memcg = NULL; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2388 | goto again; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2389 | case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2390 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2391 | goto nomem; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2392 | case CHARGE_NOMEM: /* OOM routine works */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2393 | if (!oom) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2394 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2395 | goto nomem; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2396 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2397 | /* If oom, we never return -ENOMEM */ |
| 2398 | nr_oom_retries--; |
| 2399 | break; |
| 2400 | case CHARGE_OOM_DIE: /* Killed by OOM Killer */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2401 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2402 | goto bypass; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2403 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2404 | } while (ret != CHARGE_OK); |
| 2405 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2406 | if (batch > nr_pages) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2407 | refill_stock(memcg, batch - nr_pages); |
| 2408 | css_put(&memcg->css); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2409 | done: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2410 | *ptr = memcg; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2411 | return 0; |
| 2412 | nomem: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2413 | *ptr = NULL; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2414 | return -ENOMEM; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2415 | bypass: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2416 | *ptr = NULL; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2417 | return 0; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2418 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2419 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2420 | /* |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2421 | * Somemtimes we have to undo a charge we got by try_charge(). |
| 2422 | * This function is for that and do uncharge, put css's refcnt. |
| 2423 | * gotten by try_charge(). |
| 2424 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2425 | static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg, |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2426 | unsigned int nr_pages) |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2427 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2428 | if (!mem_cgroup_is_root(memcg)) { |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2429 | unsigned long bytes = nr_pages * PAGE_SIZE; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2430 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2431 | res_counter_uncharge(&memcg->res, bytes); |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2432 | if (do_swap_account) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2433 | res_counter_uncharge(&memcg->memsw, bytes); |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2434 | } |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | /* |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2438 | * A helper function to get mem_cgroup from ID. must be called under |
| 2439 | * rcu_read_lock(). The caller must check css_is_removed() or some if |
| 2440 | * it's concern. (dropping refcnt from swap can be called against removed |
| 2441 | * memcg.) |
| 2442 | */ |
| 2443 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) |
| 2444 | { |
| 2445 | struct cgroup_subsys_state *css; |
| 2446 | |
| 2447 | /* ID 0 is unused ID */ |
| 2448 | if (!id) |
| 2449 | return NULL; |
| 2450 | css = css_lookup(&mem_cgroup_subsys, id); |
| 2451 | if (!css) |
| 2452 | return NULL; |
| 2453 | return container_of(css, struct mem_cgroup, css); |
| 2454 | } |
| 2455 | |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2456 | struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2457 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2458 | struct mem_cgroup *memcg = NULL; |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2459 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2460 | unsigned short id; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2461 | swp_entry_t ent; |
| 2462 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2463 | VM_BUG_ON(!PageLocked(page)); |
| 2464 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2465 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2466 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2467 | if (PageCgroupUsed(pc)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2468 | memcg = pc->mem_cgroup; |
| 2469 | if (memcg && !css_tryget(&memcg->css)) |
| 2470 | memcg = NULL; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2471 | } else if (PageSwapCache(page)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2472 | ent.val = page_private(page); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2473 | id = lookup_swap_cgroup(ent); |
| 2474 | rcu_read_lock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2475 | memcg = mem_cgroup_lookup(id); |
| 2476 | if (memcg && !css_tryget(&memcg->css)) |
| 2477 | memcg = NULL; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2478 | rcu_read_unlock(); |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2479 | } |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2480 | unlock_page_cgroup(pc); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2481 | return memcg; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2482 | } |
| 2483 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2484 | static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg, |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2485 | struct page *page, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2486 | unsigned int nr_pages, |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2487 | struct page_cgroup *pc, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2488 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2489 | { |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2490 | lock_page_cgroup(pc); |
| 2491 | if (unlikely(PageCgroupUsed(pc))) { |
| 2492 | unlock_page_cgroup(pc); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2493 | __mem_cgroup_cancel_charge(memcg, nr_pages); |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2494 | return; |
| 2495 | } |
| 2496 | /* |
| 2497 | * we don't need page_cgroup_lock about tail pages, becase they are not |
| 2498 | * accessed by any other context at this point. |
| 2499 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2500 | pc->mem_cgroup = memcg; |
KAMEZAWA Hiroyuki | 261fb61 | 2009-09-23 15:56:33 -0700 | [diff] [blame] | 2501 | /* |
| 2502 | * We access a page_cgroup asynchronously without lock_page_cgroup(). |
| 2503 | * Especially when a page_cgroup is taken from a page, pc->mem_cgroup |
| 2504 | * is accessed after testing USED bit. To make pc->mem_cgroup visible |
| 2505 | * before USED bit, we need memory barrier here. |
| 2506 | * See mem_cgroup_add_lru_list(), etc. |
| 2507 | */ |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2508 | smp_wmb(); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 2509 | switch (ctype) { |
| 2510 | case MEM_CGROUP_CHARGE_TYPE_CACHE: |
| 2511 | case MEM_CGROUP_CHARGE_TYPE_SHMEM: |
| 2512 | SetPageCgroupCache(pc); |
| 2513 | SetPageCgroupUsed(pc); |
| 2514 | break; |
| 2515 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
| 2516 | ClearPageCgroupCache(pc); |
| 2517 | SetPageCgroupUsed(pc); |
| 2518 | break; |
| 2519 | default: |
| 2520 | break; |
| 2521 | } |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2522 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2523 | mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), nr_pages); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2524 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 2525 | /* |
| 2526 | * "charge_statistics" updated event counter. Then, check it. |
| 2527 | * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree. |
| 2528 | * if they exceeds softlimit. |
| 2529 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2530 | memcg_check_events(memcg, page); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2531 | } |
| 2532 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2533 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 2534 | |
| 2535 | #define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\ |
| 2536 | (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION)) |
| 2537 | /* |
| 2538 | * Because tail pages are not marked as "used", set it. We're under |
| 2539 | * zone->lru_lock, 'splitting on pmd' and compund_lock. |
| 2540 | */ |
| 2541 | void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail) |
| 2542 | { |
| 2543 | struct page_cgroup *head_pc = lookup_page_cgroup(head); |
| 2544 | struct page_cgroup *tail_pc = lookup_page_cgroup(tail); |
| 2545 | unsigned long flags; |
| 2546 | |
KAMEZAWA Hiroyuki | 3d37c4a | 2011-01-25 15:07:28 -0800 | [diff] [blame] | 2547 | if (mem_cgroup_disabled()) |
| 2548 | return; |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2549 | /* |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2550 | * We have no races with charge/uncharge but will have races with |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2551 | * page state accounting. |
| 2552 | */ |
| 2553 | move_lock_page_cgroup(head_pc, &flags); |
| 2554 | |
| 2555 | tail_pc->mem_cgroup = head_pc->mem_cgroup; |
| 2556 | smp_wmb(); /* see __commit_charge() */ |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2557 | if (PageCgroupAcctLRU(head_pc)) { |
| 2558 | enum lru_list lru; |
| 2559 | struct mem_cgroup_per_zone *mz; |
| 2560 | |
| 2561 | /* |
| 2562 | * LRU flags cannot be copied because we need to add tail |
| 2563 | *.page to LRU by generic call and our hook will be called. |
| 2564 | * We hold lru_lock, then, reduce counter directly. |
| 2565 | */ |
| 2566 | lru = page_lru(head); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 2567 | mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2568 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
| 2569 | } |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2570 | tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT; |
| 2571 | move_unlock_page_cgroup(head_pc, &flags); |
| 2572 | } |
| 2573 | #endif |
| 2574 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2575 | /** |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2576 | * mem_cgroup_move_account - move account of the page |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2577 | * @page: the page |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2578 | * @nr_pages: number of regular pages (>1 for huge pages) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2579 | * @pc: page_cgroup of the page. |
| 2580 | * @from: mem_cgroup which the page is moved from. |
| 2581 | * @to: mem_cgroup which the page is moved to. @from != @to. |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2582 | * @uncharge: whether we should call uncharge and css_put against @from. |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2583 | * |
| 2584 | * The caller must confirm following. |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2585 | * - page is not on LRU (isolate_page() is useful.) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2586 | * - compound_lock is held when nr_pages > 1 |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2587 | * |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2588 | * This function doesn't do "charge" nor css_get to new cgroup. It should be |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2589 | * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2590 | * true, this function does "uncharge" from old cgroup, but it doesn't if |
| 2591 | * @uncharge is false, so a caller should do "uncharge". |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2592 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2593 | static int mem_cgroup_move_account(struct page *page, |
| 2594 | unsigned int nr_pages, |
| 2595 | struct page_cgroup *pc, |
| 2596 | struct mem_cgroup *from, |
| 2597 | struct mem_cgroup *to, |
| 2598 | bool uncharge) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2599 | { |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2600 | unsigned long flags; |
| 2601 | int ret; |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2602 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2603 | VM_BUG_ON(from == to); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2604 | VM_BUG_ON(PageLRU(page)); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2605 | /* |
| 2606 | * The page is isolated from LRU. So, collapse function |
| 2607 | * will not handle this page. But page splitting can happen. |
| 2608 | * Do this check under compound_page_lock(). The caller should |
| 2609 | * hold it. |
| 2610 | */ |
| 2611 | ret = -EBUSY; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2612 | if (nr_pages > 1 && !PageTransHuge(page)) |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2613 | goto out; |
| 2614 | |
| 2615 | lock_page_cgroup(pc); |
| 2616 | |
| 2617 | ret = -EINVAL; |
| 2618 | if (!PageCgroupUsed(pc) || pc->mem_cgroup != from) |
| 2619 | goto unlock; |
| 2620 | |
| 2621 | move_lock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2622 | |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 2623 | if (PageCgroupFileMapped(pc)) { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 2624 | /* Update mapped_file data for mem_cgroup */ |
| 2625 | preempt_disable(); |
| 2626 | __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 2627 | __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 2628 | preempt_enable(); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2629 | } |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2630 | mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2631 | if (uncharge) |
| 2632 | /* This is not "cancel", but cancel_charge does all we need. */ |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2633 | __mem_cgroup_cancel_charge(from, nr_pages); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2634 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2635 | /* caller should have done css_get */ |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2636 | pc->mem_cgroup = to; |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2637 | mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2638 | /* |
| 2639 | * We charges against "to" which may not have any tasks. Then, "to" |
| 2640 | * can be under rmdir(). But in current implementation, caller of |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 2641 | * this function is just force_empty() and move charge, so it's |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2642 | * guaranteed that "to" is never removed. So, we don't check rmdir |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 2643 | * status here. |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2644 | */ |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2645 | move_unlock_page_cgroup(pc, &flags); |
| 2646 | ret = 0; |
| 2647 | unlock: |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2648 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 2649 | /* |
| 2650 | * check events |
| 2651 | */ |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2652 | memcg_check_events(to, page); |
| 2653 | memcg_check_events(from, page); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2654 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2655 | return ret; |
| 2656 | } |
| 2657 | |
| 2658 | /* |
| 2659 | * move charges to its parent. |
| 2660 | */ |
| 2661 | |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2662 | static int mem_cgroup_move_parent(struct page *page, |
| 2663 | struct page_cgroup *pc, |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2664 | struct mem_cgroup *child, |
| 2665 | gfp_t gfp_mask) |
| 2666 | { |
| 2667 | struct cgroup *cg = child->css.cgroup; |
| 2668 | struct cgroup *pcg = cg->parent; |
| 2669 | struct mem_cgroup *parent; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2670 | unsigned int nr_pages; |
Andrew Morton | 4be4489 | 2011-03-23 16:42:39 -0700 | [diff] [blame] | 2671 | unsigned long uninitialized_var(flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2672 | int ret; |
| 2673 | |
| 2674 | /* Is ROOT ? */ |
| 2675 | if (!pcg) |
| 2676 | return -EINVAL; |
| 2677 | |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2678 | ret = -EBUSY; |
| 2679 | if (!get_page_unless_zero(page)) |
| 2680 | goto out; |
| 2681 | if (isolate_lru_page(page)) |
| 2682 | goto put; |
KAMEZAWA Hiroyuki | 52dbb90 | 2011-01-25 15:07:29 -0800 | [diff] [blame] | 2683 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2684 | nr_pages = hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2685 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2686 | parent = mem_cgroup_from_cont(pcg); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2687 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2688 | if (ret || !parent) |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2689 | goto put_back; |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2690 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2691 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2692 | flags = compound_lock_irqsave(page); |
| 2693 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2694 | ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2695 | if (ret) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2696 | __mem_cgroup_cancel_charge(parent, nr_pages); |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 2697 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2698 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2699 | compound_unlock_irqrestore(page, flags); |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 2700 | put_back: |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2701 | putback_lru_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2702 | put: |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 2703 | put_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2704 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2705 | return ret; |
| 2706 | } |
| 2707 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2708 | /* |
| 2709 | * Charge the memory controller for page usage. |
| 2710 | * Return |
| 2711 | * 0 if the charge was successful |
| 2712 | * < 0 if the cgroup is over its limit |
| 2713 | */ |
| 2714 | 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] | 2715 | gfp_t gfp_mask, enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2716 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2717 | struct mem_cgroup *memcg = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2718 | unsigned int nr_pages = 1; |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 2719 | struct page_cgroup *pc; |
| 2720 | bool oom = true; |
| 2721 | int ret; |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2722 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2723 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2724 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2725 | VM_BUG_ON(!PageTransHuge(page)); |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 2726 | /* |
| 2727 | * Never OOM-kill a process for a huge page. The |
| 2728 | * fault handler will fall back to regular pages. |
| 2729 | */ |
| 2730 | oom = false; |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2731 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2732 | |
| 2733 | pc = lookup_page_cgroup(page); |
Johannes Weiner | af4a662 | 2011-03-23 16:42:24 -0700 | [diff] [blame] | 2734 | BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */ |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2735 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2736 | ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom); |
| 2737 | if (ret || !memcg) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2738 | return ret; |
| 2739 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2740 | __mem_cgroup_commit_charge(memcg, page, nr_pages, pc, ctype); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2741 | return 0; |
| 2742 | } |
| 2743 | |
| 2744 | int mem_cgroup_newpage_charge(struct page *page, |
| 2745 | struct mm_struct *mm, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2746 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2747 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 2748 | return 0; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2749 | /* |
| 2750 | * If already mapped, we don't have to account. |
| 2751 | * If page cache, page->mapping has address_space. |
| 2752 | * But page->mapping may have out-of-use anon_vma pointer, |
| 2753 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping |
| 2754 | * is NULL. |
| 2755 | */ |
| 2756 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) |
| 2757 | return 0; |
| 2758 | if (unlikely(!mm)) |
| 2759 | mm = &init_mm; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2760 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2761 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2762 | } |
| 2763 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2764 | static void |
| 2765 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 2766 | enum charge_type ctype); |
| 2767 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2768 | static void |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2769 | __mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2770 | enum charge_type ctype) |
| 2771 | { |
| 2772 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 2773 | /* |
| 2774 | * In some case, SwapCache, FUSE(splice_buf->radixtree), the page |
| 2775 | * is already on LRU. It means the page may on some other page_cgroup's |
| 2776 | * LRU. Take care of it. |
| 2777 | */ |
| 2778 | mem_cgroup_lru_del_before_commit(page); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2779 | __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype); |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2780 | mem_cgroup_lru_add_after_commit(page); |
| 2781 | return; |
| 2782 | } |
| 2783 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 2784 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 2785 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2786 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2787 | struct mem_cgroup *memcg = NULL; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2788 | int ret; |
| 2789 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2790 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 2791 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2792 | if (PageCompound(page)) |
| 2793 | return 0; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2794 | |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2795 | if (unlikely(!mm)) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2796 | mm = &init_mm; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2797 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2798 | if (page_is_file_cache(page)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2799 | ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &memcg, true); |
| 2800 | if (ret || !memcg) |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2801 | return ret; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2802 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2803 | /* |
| 2804 | * FUSE reuses pages without going through the final |
| 2805 | * put that would remove them from the LRU list, make |
| 2806 | * sure that they get relinked properly. |
| 2807 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2808 | __mem_cgroup_commit_charge_lrucare(page, memcg, |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2809 | MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 2810 | return ret; |
| 2811 | } |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2812 | /* shmem */ |
| 2813 | if (PageSwapCache(page)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2814 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &memcg); |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2815 | if (!ret) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2816 | __mem_cgroup_commit_charge_swapin(page, memcg, |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2817 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
| 2818 | } else |
| 2819 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2820 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2821 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2822 | return ret; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 2823 | } |
| 2824 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2825 | /* |
| 2826 | * While swap-in, try_charge -> commit or cancel, the page is locked. |
| 2827 | * 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] | 2828 | * struct page_cgroup is acquired. This refcnt will be consumed by |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2829 | * "commit()" or removed by "cancel()" |
| 2830 | */ |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2831 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
| 2832 | struct page *page, |
| 2833 | gfp_t mask, struct mem_cgroup **ptr) |
| 2834 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2835 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2836 | int ret; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2837 | |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 2838 | *ptr = NULL; |
| 2839 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2840 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2841 | return 0; |
| 2842 | |
| 2843 | if (!do_swap_account) |
| 2844 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2845 | /* |
| 2846 | * A racing thread's fault, or swapoff, may have already updated |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2847 | * the pte, and even removed page from swap cache: in those cases |
| 2848 | * do_swap_page()'s pte_same() test will fail; but there's also a |
| 2849 | * KSM case which does need to charge the page. |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2850 | */ |
| 2851 | if (!PageSwapCache(page)) |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2852 | goto charge_cur_mm; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2853 | memcg = try_get_mem_cgroup_from_page(page); |
| 2854 | if (!memcg) |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2855 | goto charge_cur_mm; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2856 | *ptr = memcg; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2857 | ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2858 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2859 | return ret; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2860 | charge_cur_mm: |
| 2861 | if (unlikely(!mm)) |
| 2862 | mm = &init_mm; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2863 | return __mem_cgroup_try_charge(mm, mask, 1, ptr, true); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2864 | } |
| 2865 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2866 | static void |
| 2867 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 2868 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2869 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2870 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2871 | return; |
| 2872 | if (!ptr) |
| 2873 | return; |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2874 | cgroup_exclude_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2875 | |
| 2876 | __mem_cgroup_commit_charge_lrucare(page, ptr, ctype); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2877 | /* |
| 2878 | * Now swap is on-memory. This means this page may be |
| 2879 | * counted both as mem and swap....double count. |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2880 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
| 2881 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() |
| 2882 | * may call delete_from_swap_cache() before reach here. |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2883 | */ |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2884 | if (do_swap_account && PageSwapCache(page)) { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2885 | swp_entry_t ent = {.val = page_private(page)}; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2886 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2887 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2888 | |
| 2889 | id = swap_cgroup_record(ent, 0); |
| 2890 | rcu_read_lock(); |
| 2891 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2892 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2893 | /* |
| 2894 | * This recorded memcg can be obsolete one. So, avoid |
| 2895 | * calling css_tryget |
| 2896 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2897 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 2898 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2899 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2900 | mem_cgroup_put(memcg); |
| 2901 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2902 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2903 | } |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2904 | /* |
| 2905 | * At swapin, we may charge account against cgroup which has no tasks. |
| 2906 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 2907 | * In that case, we need to call pre_destroy() again. check it here. |
| 2908 | */ |
| 2909 | cgroup_release_and_wakeup_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2910 | } |
| 2911 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2912 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
| 2913 | { |
| 2914 | __mem_cgroup_commit_charge_swapin(page, ptr, |
| 2915 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 2916 | } |
| 2917 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2918 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2919 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2920 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2921 | return; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2922 | if (!memcg) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2923 | return; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2924 | __mem_cgroup_cancel_charge(memcg, 1); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2925 | } |
| 2926 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2927 | static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2928 | unsigned int nr_pages, |
| 2929 | const enum charge_type ctype) |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2930 | { |
| 2931 | struct memcg_batch_info *batch = NULL; |
| 2932 | bool uncharge_memsw = true; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2933 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2934 | /* If swapout, usage of swap doesn't decrease */ |
| 2935 | if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) |
| 2936 | uncharge_memsw = false; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2937 | |
| 2938 | batch = ¤t->memcg_batch; |
| 2939 | /* |
| 2940 | * In usual, we do css_get() when we remember memcg pointer. |
| 2941 | * But in this case, we keep res->usage until end of a series of |
| 2942 | * uncharges. Then, it's ok to ignore memcg's refcnt. |
| 2943 | */ |
| 2944 | if (!batch->memcg) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2945 | batch->memcg = memcg; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2946 | /* |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2947 | * do_batch > 0 when unmapping pages or inode invalidate/truncate. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2948 | * 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] | 2949 | * the same cgroup and we have chance to coalesce uncharges. |
| 2950 | * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE) |
| 2951 | * because we want to do uncharge as soon as possible. |
| 2952 | */ |
| 2953 | |
| 2954 | if (!batch->do_batch || test_thread_flag(TIF_MEMDIE)) |
| 2955 | goto direct_uncharge; |
| 2956 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2957 | if (nr_pages > 1) |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2958 | goto direct_uncharge; |
| 2959 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2960 | /* |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2961 | * In typical case, batch->memcg == mem. This means we can |
| 2962 | * merge a series of uncharges to an uncharge of res_counter. |
| 2963 | * If not, we uncharge res_counter ony by one. |
| 2964 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2965 | if (batch->memcg != memcg) |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2966 | goto direct_uncharge; |
| 2967 | /* remember freed charge and uncharge it later */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 2968 | batch->nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2969 | if (uncharge_memsw) |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 2970 | batch->memsw_nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2971 | return; |
| 2972 | direct_uncharge: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2973 | res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2974 | if (uncharge_memsw) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2975 | res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE); |
| 2976 | if (unlikely(batch->memcg != memcg)) |
| 2977 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2978 | return; |
| 2979 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2980 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2981 | /* |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2982 | * uncharge if !page_mapped(page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2983 | */ |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2984 | static struct mem_cgroup * |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2985 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2986 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2987 | struct mem_cgroup *memcg = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2988 | unsigned int nr_pages = 1; |
| 2989 | struct page_cgroup *pc; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2990 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2991 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2992 | return NULL; |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 2993 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2994 | if (PageSwapCache(page)) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2995 | return NULL; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2996 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2997 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2998 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2999 | VM_BUG_ON(!PageTransHuge(page)); |
| 3000 | } |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 3001 | /* |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 3002 | * Check if our page_cgroup is valid |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 3003 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3004 | pc = lookup_page_cgroup(page); |
| 3005 | if (unlikely(!pc || !PageCgroupUsed(pc))) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3006 | return NULL; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3007 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3008 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3009 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3010 | memcg = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3011 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3012 | if (!PageCgroupUsed(pc)) |
| 3013 | goto unlock_out; |
| 3014 | |
| 3015 | switch (ctype) { |
| 3016 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3017 | case MEM_CGROUP_CHARGE_TYPE_DROP: |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3018 | /* See mem_cgroup_prepare_migration() */ |
| 3019 | if (page_mapped(page) || PageCgroupMigration(pc)) |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3020 | goto unlock_out; |
| 3021 | break; |
| 3022 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: |
| 3023 | if (!PageAnon(page)) { /* Shared memory */ |
| 3024 | if (page->mapping && !page_is_file_cache(page)) |
| 3025 | goto unlock_out; |
| 3026 | } else if (page_mapped(page)) /* Anon */ |
| 3027 | goto unlock_out; |
| 3028 | break; |
| 3029 | default: |
| 3030 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3031 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3032 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3033 | mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), -nr_pages); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 3034 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3035 | ClearPageCgroupUsed(pc); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 3036 | /* |
| 3037 | * pc->mem_cgroup is not cleared here. It will be accessed when it's |
| 3038 | * freed from LRU. This is safe because uncharged page is expected not |
| 3039 | * to be reused (freed soon). Exception is SwapCache, it's handled by |
| 3040 | * special functions. |
| 3041 | */ |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 3042 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3043 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3044 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3045 | * even after unlock, we have memcg->res.usage here and this memcg |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3046 | * will never be freed. |
| 3047 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3048 | memcg_check_events(memcg, page); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3049 | if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3050 | mem_cgroup_swap_statistics(memcg, true); |
| 3051 | mem_cgroup_get(memcg); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3052 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3053 | if (!mem_cgroup_is_root(memcg)) |
| 3054 | mem_cgroup_do_uncharge(memcg, nr_pages, ctype); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 3055 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3056 | return memcg; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3057 | |
| 3058 | unlock_out: |
| 3059 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3060 | return NULL; |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 3061 | } |
| 3062 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3063 | void mem_cgroup_uncharge_page(struct page *page) |
| 3064 | { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3065 | /* early check. */ |
| 3066 | if (page_mapped(page)) |
| 3067 | return; |
| 3068 | if (page->mapping && !PageAnon(page)) |
| 3069 | return; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3070 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 3071 | } |
| 3072 | |
| 3073 | void mem_cgroup_uncharge_cache_page(struct page *page) |
| 3074 | { |
| 3075 | VM_BUG_ON(page_mapped(page)); |
KAMEZAWA Hiroyuki | b7abea9 | 2008-10-18 20:28:09 -0700 | [diff] [blame] | 3076 | VM_BUG_ON(page->mapping); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3077 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 3078 | } |
| 3079 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3080 | /* |
| 3081 | * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate. |
| 3082 | * In that cases, pages are freed continuously and we can expect pages |
| 3083 | * are in the same memcg. All these calls itself limits the number of |
| 3084 | * pages freed at once, then uncharge_start/end() is called properly. |
| 3085 | * This may be called prural(2) times in a context, |
| 3086 | */ |
| 3087 | |
| 3088 | void mem_cgroup_uncharge_start(void) |
| 3089 | { |
| 3090 | current->memcg_batch.do_batch++; |
| 3091 | /* We can do nest. */ |
| 3092 | if (current->memcg_batch.do_batch == 1) { |
| 3093 | current->memcg_batch.memcg = NULL; |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3094 | current->memcg_batch.nr_pages = 0; |
| 3095 | current->memcg_batch.memsw_nr_pages = 0; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3096 | } |
| 3097 | } |
| 3098 | |
| 3099 | void mem_cgroup_uncharge_end(void) |
| 3100 | { |
| 3101 | struct memcg_batch_info *batch = ¤t->memcg_batch; |
| 3102 | |
| 3103 | if (!batch->do_batch) |
| 3104 | return; |
| 3105 | |
| 3106 | batch->do_batch--; |
| 3107 | if (batch->do_batch) /* If stacked, do nothing. */ |
| 3108 | return; |
| 3109 | |
| 3110 | if (!batch->memcg) |
| 3111 | return; |
| 3112 | /* |
| 3113 | * This "batch->memcg" is valid without any css_get/put etc... |
| 3114 | * bacause we hide charges behind us. |
| 3115 | */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3116 | if (batch->nr_pages) |
| 3117 | res_counter_uncharge(&batch->memcg->res, |
| 3118 | batch->nr_pages * PAGE_SIZE); |
| 3119 | if (batch->memsw_nr_pages) |
| 3120 | res_counter_uncharge(&batch->memcg->memsw, |
| 3121 | batch->memsw_nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3122 | memcg_oom_recover(batch->memcg); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3123 | /* forget this pointer (for sanity check) */ |
| 3124 | batch->memcg = NULL; |
| 3125 | } |
| 3126 | |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3127 | #ifdef CONFIG_SWAP |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3128 | /* |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3129 | * called after __delete_from_swap_cache() and drop "page" account. |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3130 | * memcg information is recorded to swap_cgroup of "ent" |
| 3131 | */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3132 | void |
| 3133 | 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] | 3134 | { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3135 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3136 | int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3137 | |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3138 | if (!swapout) /* this was a swap cache but the swap is unused ! */ |
| 3139 | ctype = MEM_CGROUP_CHARGE_TYPE_DROP; |
| 3140 | |
| 3141 | memcg = __mem_cgroup_uncharge_common(page, ctype); |
| 3142 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3143 | /* |
| 3144 | * record memcg information, if swapout && memcg != NULL, |
| 3145 | * mem_cgroup_get() was called in uncharge(). |
| 3146 | */ |
| 3147 | if (do_swap_account && swapout && memcg) |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3148 | swap_cgroup_record(ent, css_id(&memcg->css)); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3149 | } |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3150 | #endif |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3151 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3152 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 3153 | /* |
| 3154 | * called from swap_entry_free(). remove record in swap_cgroup and |
| 3155 | * uncharge "memsw" account. |
| 3156 | */ |
| 3157 | void mem_cgroup_uncharge_swap(swp_entry_t ent) |
| 3158 | { |
| 3159 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3160 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3161 | |
| 3162 | if (!do_swap_account) |
| 3163 | return; |
| 3164 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3165 | id = swap_cgroup_record(ent, 0); |
| 3166 | rcu_read_lock(); |
| 3167 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3168 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3169 | /* |
| 3170 | * We uncharge this because swap is freed. |
| 3171 | * This memcg can be obsolete one. We avoid calling css_tryget |
| 3172 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3173 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 3174 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3175 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3176 | mem_cgroup_put(memcg); |
| 3177 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3178 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3179 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3180 | |
| 3181 | /** |
| 3182 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. |
| 3183 | * @entry: swap entry to be moved |
| 3184 | * @from: mem_cgroup which the entry is moved from |
| 3185 | * @to: mem_cgroup which the entry is moved to |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3186 | * @need_fixup: whether we should fixup res_counters and refcounts. |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3187 | * |
| 3188 | * It succeeds only when the swap_cgroup's record for this entry is the same |
| 3189 | * as the mem_cgroup's id of @from. |
| 3190 | * |
| 3191 | * Returns 0 on success, -EINVAL on failure. |
| 3192 | * |
| 3193 | * The caller must have charged to @to, IOW, called res_counter_charge() about |
| 3194 | * both res and memsw, and called css_get(). |
| 3195 | */ |
| 3196 | static int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3197 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3198 | { |
| 3199 | unsigned short old_id, new_id; |
| 3200 | |
| 3201 | old_id = css_id(&from->css); |
| 3202 | new_id = css_id(&to->css); |
| 3203 | |
| 3204 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3205 | mem_cgroup_swap_statistics(from, false); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3206 | mem_cgroup_swap_statistics(to, true); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3207 | /* |
| 3208 | * This function is only called from task migration context now. |
| 3209 | * It postpones res_counter and refcount handling till the end |
| 3210 | * of task migration(mem_cgroup_clear_mc()) for performance |
| 3211 | * improvement. But we cannot postpone mem_cgroup_get(to) |
| 3212 | * because if the process that has been moved to @to does |
| 3213 | * swap-in, the refcount of @to might be decreased to 0. |
| 3214 | */ |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3215 | mem_cgroup_get(to); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3216 | if (need_fixup) { |
| 3217 | if (!mem_cgroup_is_root(from)) |
| 3218 | res_counter_uncharge(&from->memsw, PAGE_SIZE); |
| 3219 | mem_cgroup_put(from); |
| 3220 | /* |
| 3221 | * we charged both to->res and to->memsw, so we should |
| 3222 | * uncharge to->res. |
| 3223 | */ |
| 3224 | if (!mem_cgroup_is_root(to)) |
| 3225 | res_counter_uncharge(&to->res, PAGE_SIZE); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3226 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3227 | return 0; |
| 3228 | } |
| 3229 | return -EINVAL; |
| 3230 | } |
| 3231 | #else |
| 3232 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3233 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3234 | { |
| 3235 | return -EINVAL; |
| 3236 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3237 | #endif |
| 3238 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3239 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3240 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
| 3241 | * page belongs to. |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3242 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3243 | int mem_cgroup_prepare_migration(struct page *page, |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 3244 | struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3245 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3246 | struct mem_cgroup *memcg = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3247 | struct page_cgroup *pc; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3248 | enum charge_type ctype; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3249 | int ret = 0; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 3250 | |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 3251 | *ptr = NULL; |
| 3252 | |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 3253 | VM_BUG_ON(PageTransHuge(page)); |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3254 | if (mem_cgroup_disabled()) |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 3255 | return 0; |
| 3256 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3257 | pc = lookup_page_cgroup(page); |
| 3258 | lock_page_cgroup(pc); |
| 3259 | if (PageCgroupUsed(pc)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3260 | memcg = pc->mem_cgroup; |
| 3261 | css_get(&memcg->css); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3262 | /* |
| 3263 | * At migrating an anonymous page, its mapcount goes down |
| 3264 | * to 0 and uncharge() will be called. But, even if it's fully |
| 3265 | * unmapped, migration may fail and this page has to be |
| 3266 | * charged again. We set MIGRATION flag here and delay uncharge |
| 3267 | * until end_migration() is called |
| 3268 | * |
| 3269 | * Corner Case Thinking |
| 3270 | * A) |
| 3271 | * When the old page was mapped as Anon and it's unmap-and-freed |
| 3272 | * while migration was ongoing. |
| 3273 | * If unmap finds the old page, uncharge() of it will be delayed |
| 3274 | * until end_migration(). If unmap finds a new page, it's |
| 3275 | * uncharged when it make mapcount to be 1->0. If unmap code |
| 3276 | * finds swap_migration_entry, the new page will not be mapped |
| 3277 | * and end_migration() will find it(mapcount==0). |
| 3278 | * |
| 3279 | * B) |
| 3280 | * When the old page was mapped but migraion fails, the kernel |
| 3281 | * remaps it. A charge for it is kept by MIGRATION flag even |
| 3282 | * if mapcount goes down to 0. We can do remap successfully |
| 3283 | * without charging it again. |
| 3284 | * |
| 3285 | * C) |
| 3286 | * The "old" page is under lock_page() until the end of |
| 3287 | * migration, so, the old page itself will not be swapped-out. |
| 3288 | * If the new page is swapped out before end_migraton, our |
| 3289 | * hook to usual swap-out path will catch the event. |
| 3290 | */ |
| 3291 | if (PageAnon(page)) |
| 3292 | SetPageCgroupMigration(pc); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 3293 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3294 | unlock_page_cgroup(pc); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3295 | /* |
| 3296 | * If the page is not charged at this point, |
| 3297 | * we return here. |
| 3298 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3299 | if (!memcg) |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3300 | return 0; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3301 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3302 | *ptr = memcg; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3303 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3304 | css_put(&memcg->css);/* drop extra refcnt */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3305 | if (ret || *ptr == NULL) { |
| 3306 | if (PageAnon(page)) { |
| 3307 | lock_page_cgroup(pc); |
| 3308 | ClearPageCgroupMigration(pc); |
| 3309 | unlock_page_cgroup(pc); |
| 3310 | /* |
| 3311 | * The old page may be fully unmapped while we kept it. |
| 3312 | */ |
| 3313 | mem_cgroup_uncharge_page(page); |
| 3314 | } |
| 3315 | return -ENOMEM; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3316 | } |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3317 | /* |
| 3318 | * We charge new page before it's used/mapped. So, even if unlock_page() |
| 3319 | * is called before end_migration, we can catch all events on this new |
| 3320 | * page. In the case new page is migrated but not remapped, new page's |
| 3321 | * mapcount will be finally 0 and we call uncharge in end_migration(). |
| 3322 | */ |
| 3323 | pc = lookup_page_cgroup(newpage); |
| 3324 | if (PageAnon(page)) |
| 3325 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; |
| 3326 | else if (page_is_file_cache(page)) |
| 3327 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; |
| 3328 | else |
| 3329 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3330 | __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3331 | return ret; |
| 3332 | } |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 3333 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3334 | /* remove redundant charge if migration failed*/ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3335 | void mem_cgroup_end_migration(struct mem_cgroup *memcg, |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 3336 | struct page *oldpage, struct page *newpage, bool migration_ok) |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3337 | { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3338 | struct page *used, *unused; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3339 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3340 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3341 | if (!memcg) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3342 | return; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3343 | /* blocks rmdir() */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3344 | cgroup_exclude_rmdir(&memcg->css); |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 3345 | if (!migration_ok) { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3346 | used = oldpage; |
| 3347 | unused = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3348 | } else { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3349 | used = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3350 | unused = oldpage; |
| 3351 | } |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3352 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3353 | * We disallowed uncharge of pages under migration because mapcount |
| 3354 | * of the page goes down to zero, temporarly. |
| 3355 | * Clear the flag and check the page should be charged. |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3356 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3357 | pc = lookup_page_cgroup(oldpage); |
| 3358 | lock_page_cgroup(pc); |
| 3359 | ClearPageCgroupMigration(pc); |
| 3360 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3361 | |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3362 | __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE); |
| 3363 | |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3364 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3365 | * If a page is a file cache, radix-tree replacement is very atomic |
| 3366 | * and we can skip this check. When it was an Anon page, its mapcount |
| 3367 | * goes down to 0. But because we added MIGRATION flage, it's not |
| 3368 | * uncharged yet. There are several case but page->mapcount check |
| 3369 | * and USED bit check in mem_cgroup_uncharge_page() will do enough |
| 3370 | * check. (see prepare_charge() also) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3371 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3372 | if (PageAnon(used)) |
| 3373 | mem_cgroup_uncharge_page(used); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3374 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3375 | * At migration, we may charge account against cgroup which has no |
| 3376 | * tasks. |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3377 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 3378 | * In that case, we need to call pre_destroy() again. check it here. |
| 3379 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3380 | cgroup_release_and_wakeup_rmdir(&memcg->css); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3381 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 3382 | |
Daisuke Nishimura | f212ad7 | 2011-03-23 16:42:25 -0700 | [diff] [blame] | 3383 | #ifdef CONFIG_DEBUG_VM |
| 3384 | static struct page_cgroup *lookup_page_cgroup_used(struct page *page) |
| 3385 | { |
| 3386 | struct page_cgroup *pc; |
| 3387 | |
| 3388 | pc = lookup_page_cgroup(page); |
| 3389 | if (likely(pc) && PageCgroupUsed(pc)) |
| 3390 | return pc; |
| 3391 | return NULL; |
| 3392 | } |
| 3393 | |
| 3394 | bool mem_cgroup_bad_page_check(struct page *page) |
| 3395 | { |
| 3396 | if (mem_cgroup_disabled()) |
| 3397 | return false; |
| 3398 | |
| 3399 | return lookup_page_cgroup_used(page) != NULL; |
| 3400 | } |
| 3401 | |
| 3402 | void mem_cgroup_print_bad_page(struct page *page) |
| 3403 | { |
| 3404 | struct page_cgroup *pc; |
| 3405 | |
| 3406 | pc = lookup_page_cgroup_used(page); |
| 3407 | if (pc) { |
| 3408 | int ret = -1; |
| 3409 | char *path; |
| 3410 | |
| 3411 | printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p", |
| 3412 | pc, pc->flags, pc->mem_cgroup); |
| 3413 | |
| 3414 | path = kmalloc(PATH_MAX, GFP_KERNEL); |
| 3415 | if (path) { |
| 3416 | rcu_read_lock(); |
| 3417 | ret = cgroup_path(pc->mem_cgroup->css.cgroup, |
| 3418 | path, PATH_MAX); |
| 3419 | rcu_read_unlock(); |
| 3420 | } |
| 3421 | |
| 3422 | printk(KERN_CONT "(%s)\n", |
| 3423 | (ret < 0) ? "cannot get the path" : path); |
| 3424 | kfree(path); |
| 3425 | } |
| 3426 | } |
| 3427 | #endif |
| 3428 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3429 | static DEFINE_MUTEX(set_limit_mutex); |
| 3430 | |
KOSAKI Motohiro | d38d2a7 | 2009-01-06 14:39:44 -0800 | [diff] [blame] | 3431 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3432 | unsigned long long val) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3433 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3434 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3435 | u64 memswlimit, memlimit; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3436 | int ret = 0; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3437 | int children = mem_cgroup_count_children(memcg); |
| 3438 | u64 curusage, oldusage; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3439 | int enlarge; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3440 | |
| 3441 | /* |
| 3442 | * For keeping hierarchical_reclaim simple, how long we should retry |
| 3443 | * is depends on callers. We set our retry-count to be function |
| 3444 | * of # of children which we should visit in this loop. |
| 3445 | */ |
| 3446 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; |
| 3447 | |
| 3448 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3449 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3450 | enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3451 | while (retry_count) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3452 | if (signal_pending(current)) { |
| 3453 | ret = -EINTR; |
| 3454 | break; |
| 3455 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3456 | /* |
| 3457 | * Rather than hide all in some function, I do this in |
| 3458 | * open coded manner. You see what this really does. |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3459 | * We have to guarantee memcg->res.limit < memcg->memsw.limit. |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3460 | */ |
| 3461 | mutex_lock(&set_limit_mutex); |
| 3462 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3463 | if (memswlimit < val) { |
| 3464 | ret = -EINVAL; |
| 3465 | mutex_unlock(&set_limit_mutex); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3466 | break; |
| 3467 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3468 | |
| 3469 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3470 | if (memlimit < val) |
| 3471 | enlarge = 1; |
| 3472 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3473 | ret = res_counter_set_limit(&memcg->res, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 3474 | if (!ret) { |
| 3475 | if (memswlimit == val) |
| 3476 | memcg->memsw_is_minimum = true; |
| 3477 | else |
| 3478 | memcg->memsw_is_minimum = false; |
| 3479 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3480 | mutex_unlock(&set_limit_mutex); |
| 3481 | |
| 3482 | if (!ret) |
| 3483 | break; |
| 3484 | |
Bob Liu | aa20d48 | 2009-12-15 16:47:14 -0800 | [diff] [blame] | 3485 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3486 | MEM_CGROUP_RECLAIM_SHRINK, |
| 3487 | NULL); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3488 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
| 3489 | /* Usage is reduced ? */ |
| 3490 | if (curusage >= oldusage) |
| 3491 | retry_count--; |
| 3492 | else |
| 3493 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3494 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3495 | if (!ret && enlarge) |
| 3496 | memcg_oom_recover(memcg); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 3497 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3498 | return ret; |
| 3499 | } |
| 3500 | |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 3501 | static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
| 3502 | unsigned long long val) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3503 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3504 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3505 | u64 memlimit, memswlimit, oldusage, curusage; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3506 | int children = mem_cgroup_count_children(memcg); |
| 3507 | int ret = -EBUSY; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3508 | int enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3509 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3510 | /* see mem_cgroup_resize_res_limit */ |
| 3511 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; |
| 3512 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3513 | while (retry_count) { |
| 3514 | if (signal_pending(current)) { |
| 3515 | ret = -EINTR; |
| 3516 | break; |
| 3517 | } |
| 3518 | /* |
| 3519 | * Rather than hide all in some function, I do this in |
| 3520 | * open coded manner. You see what this really does. |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3521 | * We have to guarantee memcg->res.limit < memcg->memsw.limit. |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3522 | */ |
| 3523 | mutex_lock(&set_limit_mutex); |
| 3524 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3525 | if (memlimit > val) { |
| 3526 | ret = -EINVAL; |
| 3527 | mutex_unlock(&set_limit_mutex); |
| 3528 | break; |
| 3529 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3530 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3531 | if (memswlimit < val) |
| 3532 | enlarge = 1; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3533 | ret = res_counter_set_limit(&memcg->memsw, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 3534 | if (!ret) { |
| 3535 | if (memlimit == val) |
| 3536 | memcg->memsw_is_minimum = true; |
| 3537 | else |
| 3538 | memcg->memsw_is_minimum = false; |
| 3539 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3540 | mutex_unlock(&set_limit_mutex); |
| 3541 | |
| 3542 | if (!ret) |
| 3543 | break; |
| 3544 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3545 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 3546 | MEM_CGROUP_RECLAIM_NOSWAP | |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3547 | MEM_CGROUP_RECLAIM_SHRINK, |
| 3548 | NULL); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3549 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3550 | /* Usage is reduced ? */ |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3551 | if (curusage >= oldusage) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3552 | retry_count--; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3553 | else |
| 3554 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3555 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3556 | if (!ret && enlarge) |
| 3557 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3558 | return ret; |
| 3559 | } |
| 3560 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3561 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3562 | gfp_t gfp_mask, |
| 3563 | unsigned long *total_scanned) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3564 | { |
| 3565 | unsigned long nr_reclaimed = 0; |
| 3566 | struct mem_cgroup_per_zone *mz, *next_mz = NULL; |
| 3567 | unsigned long reclaimed; |
| 3568 | int loop = 0; |
| 3569 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3570 | unsigned long long excess; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3571 | unsigned long nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3572 | |
| 3573 | if (order > 0) |
| 3574 | return 0; |
| 3575 | |
KOSAKI Motohiro | 00918b6 | 2010-08-10 18:03:05 -0700 | [diff] [blame] | 3576 | 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] | 3577 | /* |
| 3578 | * This loop can run a while, specially if mem_cgroup's continuously |
| 3579 | * keep exceeding their soft limit and putting the system under |
| 3580 | * pressure |
| 3581 | */ |
| 3582 | do { |
| 3583 | if (next_mz) |
| 3584 | mz = next_mz; |
| 3585 | else |
| 3586 | mz = mem_cgroup_largest_soft_limit_node(mctz); |
| 3587 | if (!mz) |
| 3588 | break; |
| 3589 | |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3590 | nr_scanned = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3591 | reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone, |
| 3592 | gfp_mask, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3593 | MEM_CGROUP_RECLAIM_SOFT, |
| 3594 | &nr_scanned); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3595 | nr_reclaimed += reclaimed; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3596 | *total_scanned += nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3597 | spin_lock(&mctz->lock); |
| 3598 | |
| 3599 | /* |
| 3600 | * If we failed to reclaim anything from this memory cgroup |
| 3601 | * it is time to move on to the next cgroup |
| 3602 | */ |
| 3603 | next_mz = NULL; |
| 3604 | if (!reclaimed) { |
| 3605 | do { |
| 3606 | /* |
| 3607 | * Loop until we find yet another one. |
| 3608 | * |
| 3609 | * By the time we get the soft_limit lock |
| 3610 | * again, someone might have aded the |
| 3611 | * group back on the RB tree. Iterate to |
| 3612 | * make sure we get a different mem. |
| 3613 | * mem_cgroup_largest_soft_limit_node returns |
| 3614 | * NULL if no other cgroup is present on |
| 3615 | * the tree |
| 3616 | */ |
| 3617 | next_mz = |
| 3618 | __mem_cgroup_largest_soft_limit_node(mctz); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 3619 | if (next_mz == mz) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3620 | css_put(&next_mz->mem->css); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 3621 | else /* next_mz == NULL or other memcg */ |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3622 | break; |
| 3623 | } while (1); |
| 3624 | } |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3625 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3626 | excess = res_counter_soft_limit_excess(&mz->mem->res); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3627 | /* |
| 3628 | * One school of thought says that we should not add |
| 3629 | * back the node to the tree if reclaim returns 0. |
| 3630 | * But our reclaim could return 0, simply because due |
| 3631 | * to priority we are exposing a smaller subset of |
| 3632 | * memory to reclaim from. Consider this as a longer |
| 3633 | * term TODO. |
| 3634 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3635 | /* If excess == 0, no tree ops */ |
| 3636 | __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3637 | spin_unlock(&mctz->lock); |
| 3638 | css_put(&mz->mem->css); |
| 3639 | loop++; |
| 3640 | /* |
| 3641 | * Could not reclaim anything and there are no more |
| 3642 | * mem cgroups to try or we seem to be looping without |
| 3643 | * reclaiming anything. |
| 3644 | */ |
| 3645 | if (!nr_reclaimed && |
| 3646 | (next_mz == NULL || |
| 3647 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) |
| 3648 | break; |
| 3649 | } while (!nr_reclaimed); |
| 3650 | if (next_mz) |
| 3651 | css_put(&next_mz->mem->css); |
| 3652 | return nr_reclaimed; |
| 3653 | } |
| 3654 | |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3655 | /* |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3656 | * This routine traverse page_cgroup in given list and drop them all. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3657 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
| 3658 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3659 | static int mem_cgroup_force_empty_list(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3660 | int node, int zid, enum lru_list lru) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3661 | { |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3662 | struct zone *zone; |
| 3663 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3664 | struct page_cgroup *pc, *busy; |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3665 | unsigned long flags, loop; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3666 | struct list_head *list; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3667 | int ret = 0; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3668 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3669 | zone = &NODE_DATA(node)->node_zones[zid]; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3670 | mz = mem_cgroup_zoneinfo(memcg, node, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3671 | list = &mz->lists[lru]; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3672 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3673 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
| 3674 | /* give some margin against EBUSY etc...*/ |
| 3675 | loop += 256; |
| 3676 | busy = NULL; |
| 3677 | while (loop--) { |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3678 | struct page *page; |
| 3679 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3680 | ret = 0; |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3681 | spin_lock_irqsave(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3682 | if (list_empty(list)) { |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3683 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3684 | break; |
| 3685 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3686 | pc = list_entry(list->prev, struct page_cgroup, lru); |
| 3687 | if (busy == pc) { |
| 3688 | list_move(&pc->lru, list); |
Thiago Farina | 648bcc7 | 2010-03-05 13:42:04 -0800 | [diff] [blame] | 3689 | busy = NULL; |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3690 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3691 | continue; |
| 3692 | } |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3693 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3694 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 3695 | page = lookup_cgroup_page(pc); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3696 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3697 | ret = mem_cgroup_move_parent(page, pc, memcg, GFP_KERNEL); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3698 | if (ret == -ENOMEM) |
| 3699 | break; |
| 3700 | |
| 3701 | if (ret == -EBUSY || ret == -EINVAL) { |
| 3702 | /* found lock contention or "pc" is obsolete. */ |
| 3703 | busy = pc; |
| 3704 | cond_resched(); |
| 3705 | } else |
| 3706 | busy = NULL; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3707 | } |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3708 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3709 | if (!ret && !list_empty(list)) |
| 3710 | return -EBUSY; |
| 3711 | return ret; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3712 | } |
| 3713 | |
| 3714 | /* |
| 3715 | * make mem_cgroup's charge to be 0 if there is no task. |
| 3716 | * This enables deleting this mem_cgroup. |
| 3717 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3718 | static int mem_cgroup_force_empty(struct mem_cgroup *memcg, bool free_all) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3719 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3720 | int ret; |
| 3721 | int node, zid, shrink; |
| 3722 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3723 | struct cgroup *cgrp = memcg->css.cgroup; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 3724 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3725 | css_get(&memcg->css); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3726 | |
| 3727 | shrink = 0; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3728 | /* should free all ? */ |
| 3729 | if (free_all) |
| 3730 | goto try_to_free; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3731 | move_account: |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3732 | do { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3733 | ret = -EBUSY; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3734 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3735 | goto out; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3736 | ret = -EINTR; |
| 3737 | if (signal_pending(current)) |
| 3738 | goto out; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3739 | /* This is for making all *used* pages to be on LRU. */ |
| 3740 | lru_add_drain_all(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3741 | drain_all_stock_sync(memcg); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3742 | ret = 0; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3743 | mem_cgroup_start_move(memcg); |
KAMEZAWA Hiroyuki | 299b4ea | 2009-01-29 14:25:17 -0800 | [diff] [blame] | 3744 | for_each_node_state(node, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3745 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3746 | enum lru_list l; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3747 | for_each_lru(l) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3748 | ret = mem_cgroup_force_empty_list(memcg, |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3749 | node, zid, l); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3750 | if (ret) |
| 3751 | break; |
| 3752 | } |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 3753 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3754 | if (ret) |
| 3755 | break; |
| 3756 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3757 | mem_cgroup_end_move(memcg); |
| 3758 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3759 | /* it seems parent cgroup doesn't have enough mem */ |
| 3760 | if (ret == -ENOMEM) |
| 3761 | goto try_to_free; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3762 | cond_resched(); |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3763 | /* "ret" should also be checked to ensure all lists are empty. */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3764 | } while (memcg->res.usage > 0 || ret); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3765 | out: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3766 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3767 | return ret; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3768 | |
| 3769 | try_to_free: |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3770 | /* returns EBUSY if there is a task or if we come here twice. */ |
| 3771 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3772 | ret = -EBUSY; |
| 3773 | goto out; |
| 3774 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3775 | /* we call try-to-free pages for make this cgroup empty */ |
| 3776 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3777 | /* try to free all pages in this cgroup */ |
| 3778 | shrink = 1; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3779 | while (nr_retries && memcg->res.usage > 0) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3780 | int progress; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3781 | |
| 3782 | if (signal_pending(current)) { |
| 3783 | ret = -EINTR; |
| 3784 | goto out; |
| 3785 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3786 | progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL, |
Johannes Weiner | 185efc0 | 2011-09-14 16:21:58 -0700 | [diff] [blame] | 3787 | false); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3788 | if (!progress) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3789 | nr_retries--; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3790 | /* maybe some writeback is necessary */ |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 3791 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3792 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3793 | |
| 3794 | } |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3795 | lru_add_drain(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3796 | /* try move_account...there may be some *locked* pages. */ |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3797 | goto move_account; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3798 | } |
| 3799 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3800 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
| 3801 | { |
| 3802 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); |
| 3803 | } |
| 3804 | |
| 3805 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3806 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
| 3807 | { |
| 3808 | return mem_cgroup_from_cont(cont)->use_hierarchy; |
| 3809 | } |
| 3810 | |
| 3811 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, |
| 3812 | u64 val) |
| 3813 | { |
| 3814 | int retval = 0; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3815 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3816 | struct cgroup *parent = cont->parent; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3817 | struct mem_cgroup *parent_memcg = NULL; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3818 | |
| 3819 | if (parent) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3820 | parent_memcg = mem_cgroup_from_cont(parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3821 | |
| 3822 | cgroup_lock(); |
| 3823 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 3824 | * 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] | 3825 | * in the child subtrees. If it is unset, then the change can |
| 3826 | * occur, provided the current cgroup has no children. |
| 3827 | * |
| 3828 | * For the root cgroup, parent_mem is NULL, we allow value to be |
| 3829 | * set if there are no children. |
| 3830 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3831 | if ((!parent_memcg || !parent_memcg->use_hierarchy) && |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3832 | (val == 1 || val == 0)) { |
| 3833 | if (list_empty(&cont->children)) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3834 | memcg->use_hierarchy = val; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3835 | else |
| 3836 | retval = -EBUSY; |
| 3837 | } else |
| 3838 | retval = -EINVAL; |
| 3839 | cgroup_unlock(); |
| 3840 | |
| 3841 | return retval; |
| 3842 | } |
| 3843 | |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3844 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3845 | static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg, |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3846 | enum mem_cgroup_stat_index idx) |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3847 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3848 | struct mem_cgroup *iter; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3849 | long val = 0; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3850 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3851 | /* Per-cpu values can be negative, use a signed accumulator */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3852 | for_each_mem_cgroup_tree(iter, memcg) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3853 | val += mem_cgroup_read_stat(iter, idx); |
| 3854 | |
| 3855 | if (val < 0) /* race ? */ |
| 3856 | val = 0; |
| 3857 | return val; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3858 | } |
| 3859 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3860 | 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] | 3861 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3862 | u64 val; |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3863 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3864 | if (!mem_cgroup_is_root(memcg)) { |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 3865 | val = 0; |
| 3866 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM |
| 3867 | if (!memcg->kmem_independent_accounting) |
| 3868 | val = res_counter_read_u64(&memcg->kmem, RES_USAGE); |
| 3869 | #endif |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3870 | if (!swap) |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 3871 | val += res_counter_read_u64(&memcg->res, RES_USAGE); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3872 | else |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 3873 | val += res_counter_read_u64(&memcg->memsw, RES_USAGE); |
| 3874 | |
| 3875 | return val; |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3876 | } |
| 3877 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3878 | val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE); |
| 3879 | val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3880 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3881 | if (swap) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3882 | val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAPOUT); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3883 | |
| 3884 | return val << PAGE_SHIFT; |
| 3885 | } |
| 3886 | |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 3887 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3888 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3889 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3890 | u64 val; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3891 | int type, name; |
| 3892 | |
| 3893 | type = MEMFILE_TYPE(cft->private); |
| 3894 | name = MEMFILE_ATTR(cft->private); |
| 3895 | switch (type) { |
| 3896 | case _MEM: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3897 | if (name == RES_USAGE) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3898 | val = mem_cgroup_usage(memcg, false); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3899 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3900 | val = res_counter_read_u64(&memcg->res, name); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3901 | break; |
| 3902 | case _MEMSWAP: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3903 | if (name == RES_USAGE) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3904 | val = mem_cgroup_usage(memcg, true); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3905 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3906 | val = res_counter_read_u64(&memcg->memsw, name); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3907 | break; |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 3908 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM |
| 3909 | case _KMEM: |
| 3910 | val = res_counter_read_u64(&memcg->kmem, name); |
| 3911 | break; |
| 3912 | #endif |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3913 | default: |
| 3914 | BUG(); |
| 3915 | break; |
| 3916 | } |
| 3917 | return val; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3918 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3919 | /* |
| 3920 | * The user of this function is... |
| 3921 | * RES_LIMIT. |
| 3922 | */ |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 3923 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 3924 | const char *buffer) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3925 | { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3926 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3927 | int type, name; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3928 | unsigned long long val; |
| 3929 | int ret; |
| 3930 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3931 | type = MEMFILE_TYPE(cft->private); |
| 3932 | name = MEMFILE_ATTR(cft->private); |
| 3933 | switch (name) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3934 | case RES_LIMIT: |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 3935 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
| 3936 | ret = -EINVAL; |
| 3937 | break; |
| 3938 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3939 | /* This function does all necessary parse...reuse it */ |
| 3940 | ret = res_counter_memparse_write_strategy(buffer, &val); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3941 | if (ret) |
| 3942 | break; |
| 3943 | if (type == _MEM) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3944 | ret = mem_cgroup_resize_limit(memcg, val); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3945 | else |
| 3946 | ret = mem_cgroup_resize_memsw_limit(memcg, val); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3947 | break; |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 3948 | case RES_SOFT_LIMIT: |
| 3949 | ret = res_counter_memparse_write_strategy(buffer, &val); |
| 3950 | if (ret) |
| 3951 | break; |
| 3952 | /* |
| 3953 | * For memsw, soft limits are hard to implement in terms |
| 3954 | * of semantics, for now, we support soft limits for |
| 3955 | * control without swap |
| 3956 | */ |
| 3957 | if (type == _MEM) |
| 3958 | ret = res_counter_set_soft_limit(&memcg->res, val); |
| 3959 | else |
| 3960 | ret = -EINVAL; |
| 3961 | break; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3962 | default: |
| 3963 | ret = -EINVAL; /* should be BUG() ? */ |
| 3964 | break; |
| 3965 | } |
| 3966 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3967 | } |
| 3968 | |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 3969 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
| 3970 | unsigned long long *mem_limit, unsigned long long *memsw_limit) |
| 3971 | { |
| 3972 | struct cgroup *cgroup; |
| 3973 | unsigned long long min_limit, min_memsw_limit, tmp; |
| 3974 | |
| 3975 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3976 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3977 | cgroup = memcg->css.cgroup; |
| 3978 | if (!memcg->use_hierarchy) |
| 3979 | goto out; |
| 3980 | |
| 3981 | while (cgroup->parent) { |
| 3982 | cgroup = cgroup->parent; |
| 3983 | memcg = mem_cgroup_from_cont(cgroup); |
| 3984 | if (!memcg->use_hierarchy) |
| 3985 | break; |
| 3986 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3987 | min_limit = min(min_limit, tmp); |
| 3988 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3989 | min_memsw_limit = min(min_memsw_limit, tmp); |
| 3990 | } |
| 3991 | out: |
| 3992 | *mem_limit = min_limit; |
| 3993 | *memsw_limit = min_memsw_limit; |
| 3994 | return; |
| 3995 | } |
| 3996 | |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 3997 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 3998 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3999 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4000 | int type, name; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4001 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4002 | memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4003 | type = MEMFILE_TYPE(event); |
| 4004 | name = MEMFILE_ATTR(event); |
| 4005 | switch (name) { |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4006 | case RES_MAX_USAGE: |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4007 | if (type == _MEM) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4008 | res_counter_reset_max(&memcg->res); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4009 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4010 | res_counter_reset_max(&memcg->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4011 | break; |
| 4012 | case RES_FAILCNT: |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4013 | if (type == _MEM) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4014 | res_counter_reset_failcnt(&memcg->res); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4015 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4016 | res_counter_reset_failcnt(&memcg->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4017 | break; |
| 4018 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4019 | |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 4020 | return 0; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4021 | } |
| 4022 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4023 | static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp, |
| 4024 | struct cftype *cft) |
| 4025 | { |
| 4026 | return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate; |
| 4027 | } |
| 4028 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4029 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4030 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 4031 | struct cftype *cft, u64 val) |
| 4032 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4033 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4034 | |
| 4035 | if (val >= (1 << NR_MOVE_TYPE)) |
| 4036 | return -EINVAL; |
| 4037 | /* |
| 4038 | * We check this value several times in both in can_attach() and |
| 4039 | * attach(), so we need cgroup lock to prevent this value from being |
| 4040 | * inconsistent. |
| 4041 | */ |
| 4042 | cgroup_lock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4043 | memcg->move_charge_at_immigrate = val; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4044 | cgroup_unlock(); |
| 4045 | |
| 4046 | return 0; |
| 4047 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4048 | #else |
| 4049 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 4050 | struct cftype *cft, u64 val) |
| 4051 | { |
| 4052 | return -ENOSYS; |
| 4053 | } |
| 4054 | #endif |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4055 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4056 | |
| 4057 | /* For read statistics */ |
| 4058 | enum { |
| 4059 | MCS_CACHE, |
| 4060 | MCS_RSS, |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 4061 | MCS_FILE_MAPPED, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4062 | MCS_PGPGIN, |
| 4063 | MCS_PGPGOUT, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4064 | MCS_SWAP, |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4065 | MCS_PGFAULT, |
| 4066 | MCS_PGMAJFAULT, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4067 | MCS_INACTIVE_ANON, |
| 4068 | MCS_ACTIVE_ANON, |
| 4069 | MCS_INACTIVE_FILE, |
| 4070 | MCS_ACTIVE_FILE, |
| 4071 | MCS_UNEVICTABLE, |
| 4072 | NR_MCS_STAT, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4073 | }; |
| 4074 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4075 | struct mcs_total_stat { |
| 4076 | s64 stat[NR_MCS_STAT]; |
| 4077 | }; |
| 4078 | |
| 4079 | struct { |
| 4080 | char *local_name; |
| 4081 | char *total_name; |
| 4082 | } memcg_stat_strings[NR_MCS_STAT] = { |
| 4083 | {"cache", "total_cache"}, |
| 4084 | {"rss", "total_rss"}, |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 4085 | {"mapped_file", "total_mapped_file"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4086 | {"pgpgin", "total_pgpgin"}, |
| 4087 | {"pgpgout", "total_pgpgout"}, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4088 | {"swap", "total_swap"}, |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4089 | {"pgfault", "total_pgfault"}, |
| 4090 | {"pgmajfault", "total_pgmajfault"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4091 | {"inactive_anon", "total_inactive_anon"}, |
| 4092 | {"active_anon", "total_active_anon"}, |
| 4093 | {"inactive_file", "total_inactive_file"}, |
| 4094 | {"active_file", "total_active_file"}, |
| 4095 | {"unevictable", "total_unevictable"} |
| 4096 | }; |
| 4097 | |
| 4098 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4099 | static void |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4100 | mem_cgroup_get_local_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s) |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4101 | { |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4102 | s64 val; |
| 4103 | |
| 4104 | /* per cpu stat */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4105 | val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_CACHE); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4106 | s->stat[MCS_CACHE] += val * PAGE_SIZE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4107 | val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_RSS); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4108 | s->stat[MCS_RSS] += val * PAGE_SIZE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4109 | val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_FILE_MAPPED); |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 4110 | s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4111 | val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGIN); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4112 | s->stat[MCS_PGPGIN] += val; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4113 | val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGOUT); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4114 | s->stat[MCS_PGPGOUT] += val; |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4115 | if (do_swap_account) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4116 | val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_SWAPOUT); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4117 | s->stat[MCS_SWAP] += val * PAGE_SIZE; |
| 4118 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4119 | val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGFAULT); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4120 | s->stat[MCS_PGFAULT] += val; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4121 | val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGMAJFAULT); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4122 | s->stat[MCS_PGMAJFAULT] += val; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4123 | |
| 4124 | /* per zone stat */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4125 | val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4126 | s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4127 | val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4128 | s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4129 | val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4130 | s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4131 | val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4132 | s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4133 | val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4134 | s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | static void |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4138 | mem_cgroup_get_total_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s) |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4139 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4140 | struct mem_cgroup *iter; |
| 4141 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4142 | for_each_mem_cgroup_tree(iter, memcg) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4143 | mem_cgroup_get_local_stat(iter, s); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4144 | } |
| 4145 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4146 | #ifdef CONFIG_NUMA |
| 4147 | static int mem_control_numa_stat_show(struct seq_file *m, void *arg) |
| 4148 | { |
| 4149 | int nid; |
| 4150 | unsigned long total_nr, file_nr, anon_nr, unevictable_nr; |
| 4151 | unsigned long node_nr; |
| 4152 | struct cgroup *cont = m->private; |
| 4153 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
| 4154 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4155 | total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4156 | seq_printf(m, "total=%lu", total_nr); |
| 4157 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4158 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, LRU_ALL); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4159 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4160 | } |
| 4161 | seq_putc(m, '\n'); |
| 4162 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4163 | file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4164 | seq_printf(m, "file=%lu", file_nr); |
| 4165 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4166 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4167 | LRU_ALL_FILE); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4168 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4169 | } |
| 4170 | seq_putc(m, '\n'); |
| 4171 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4172 | anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4173 | seq_printf(m, "anon=%lu", anon_nr); |
| 4174 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4175 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4176 | LRU_ALL_ANON); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4177 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4178 | } |
| 4179 | seq_putc(m, '\n'); |
| 4180 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4181 | unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE)); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4182 | seq_printf(m, "unevictable=%lu", unevictable_nr); |
| 4183 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4184 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4185 | BIT(LRU_UNEVICTABLE)); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4186 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4187 | } |
| 4188 | seq_putc(m, '\n'); |
| 4189 | return 0; |
| 4190 | } |
| 4191 | #endif /* CONFIG_NUMA */ |
| 4192 | |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 4193 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
| 4194 | struct cgroup_map_cb *cb) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4195 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4196 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4197 | struct mcs_total_stat mystat; |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4198 | int i; |
| 4199 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4200 | memset(&mystat, 0, sizeof(mystat)); |
| 4201 | mem_cgroup_get_local_stat(mem_cont, &mystat); |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4202 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4203 | |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4204 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 4205 | if (i == MCS_SWAP && !do_swap_account) |
| 4206 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4207 | cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4208 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4209 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4210 | /* Hierarchical information */ |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 4211 | { |
| 4212 | unsigned long long limit, memsw_limit; |
| 4213 | memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit); |
| 4214 | cb->fill(cb, "hierarchical_memory_limit", limit); |
| 4215 | if (do_swap_account) |
| 4216 | cb->fill(cb, "hierarchical_memsw_limit", memsw_limit); |
| 4217 | } |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4218 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4219 | memset(&mystat, 0, sizeof(mystat)); |
| 4220 | mem_cgroup_get_total_stat(mem_cont, &mystat); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4221 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 4222 | if (i == MCS_SWAP && !do_swap_account) |
| 4223 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4224 | cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4225 | } |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4226 | |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4227 | #ifdef CONFIG_DEBUG_VM |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4228 | { |
| 4229 | int nid, zid; |
| 4230 | struct mem_cgroup_per_zone *mz; |
| 4231 | unsigned long recent_rotated[2] = {0, 0}; |
| 4232 | unsigned long recent_scanned[2] = {0, 0}; |
| 4233 | |
| 4234 | for_each_online_node(nid) |
| 4235 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 4236 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
| 4237 | |
| 4238 | recent_rotated[0] += |
| 4239 | mz->reclaim_stat.recent_rotated[0]; |
| 4240 | recent_rotated[1] += |
| 4241 | mz->reclaim_stat.recent_rotated[1]; |
| 4242 | recent_scanned[0] += |
| 4243 | mz->reclaim_stat.recent_scanned[0]; |
| 4244 | recent_scanned[1] += |
| 4245 | mz->reclaim_stat.recent_scanned[1]; |
| 4246 | } |
| 4247 | cb->fill(cb, "recent_rotated_anon", recent_rotated[0]); |
| 4248 | cb->fill(cb, "recent_rotated_file", recent_rotated[1]); |
| 4249 | cb->fill(cb, "recent_scanned_anon", recent_scanned[0]); |
| 4250 | cb->fill(cb, "recent_scanned_file", recent_scanned[1]); |
| 4251 | } |
| 4252 | #endif |
| 4253 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4254 | return 0; |
| 4255 | } |
| 4256 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4257 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
| 4258 | { |
| 4259 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4260 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 4261 | return mem_cgroup_swappiness(memcg); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4262 | } |
| 4263 | |
| 4264 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, |
| 4265 | u64 val) |
| 4266 | { |
| 4267 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4268 | struct mem_cgroup *parent; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4269 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4270 | if (val > 100) |
| 4271 | return -EINVAL; |
| 4272 | |
| 4273 | if (cgrp->parent == NULL) |
| 4274 | return -EINVAL; |
| 4275 | |
| 4276 | parent = mem_cgroup_from_cont(cgrp->parent); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4277 | |
| 4278 | cgroup_lock(); |
| 4279 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4280 | /* If under hierarchy, only empty-root can set this value */ |
| 4281 | if ((parent->use_hierarchy) || |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4282 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
| 4283 | cgroup_unlock(); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4284 | return -EINVAL; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4285 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4286 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4287 | memcg->swappiness = val; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4288 | |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4289 | cgroup_unlock(); |
| 4290 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4291 | return 0; |
| 4292 | } |
| 4293 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4294 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) |
| 4295 | { |
| 4296 | struct mem_cgroup_threshold_ary *t; |
| 4297 | u64 usage; |
| 4298 | int i; |
| 4299 | |
| 4300 | rcu_read_lock(); |
| 4301 | if (!swap) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4302 | t = rcu_dereference(memcg->thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4303 | else |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4304 | t = rcu_dereference(memcg->memsw_thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4305 | |
| 4306 | if (!t) |
| 4307 | goto unlock; |
| 4308 | |
| 4309 | usage = mem_cgroup_usage(memcg, swap); |
| 4310 | |
| 4311 | /* |
| 4312 | * current_threshold points to threshold just below usage. |
| 4313 | * If it's not true, a threshold was crossed after last |
| 4314 | * call of __mem_cgroup_threshold(). |
| 4315 | */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4316 | i = t->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4317 | |
| 4318 | /* |
| 4319 | * Iterate backward over array of thresholds starting from |
| 4320 | * current_threshold and check if a threshold is crossed. |
| 4321 | * If none of thresholds below usage is crossed, we read |
| 4322 | * only one element of the array here. |
| 4323 | */ |
| 4324 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) |
| 4325 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4326 | |
| 4327 | /* i = current_threshold + 1 */ |
| 4328 | i++; |
| 4329 | |
| 4330 | /* |
| 4331 | * Iterate forward over array of thresholds starting from |
| 4332 | * current_threshold+1 and check if a threshold is crossed. |
| 4333 | * If none of thresholds above usage is crossed, we read |
| 4334 | * only one element of the array here. |
| 4335 | */ |
| 4336 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) |
| 4337 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4338 | |
| 4339 | /* Update current_threshold */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4340 | t->current_threshold = i - 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4341 | unlock: |
| 4342 | rcu_read_unlock(); |
| 4343 | } |
| 4344 | |
| 4345 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) |
| 4346 | { |
Kirill A. Shutemov | ad4ca5f | 2010-10-07 12:59:27 -0700 | [diff] [blame] | 4347 | while (memcg) { |
| 4348 | __mem_cgroup_threshold(memcg, false); |
| 4349 | if (do_swap_account) |
| 4350 | __mem_cgroup_threshold(memcg, true); |
| 4351 | |
| 4352 | memcg = parent_mem_cgroup(memcg); |
| 4353 | } |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | static int compare_thresholds(const void *a, const void *b) |
| 4357 | { |
| 4358 | const struct mem_cgroup_threshold *_a = a; |
| 4359 | const struct mem_cgroup_threshold *_b = b; |
| 4360 | |
| 4361 | return _a->threshold - _b->threshold; |
| 4362 | } |
| 4363 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4364 | static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4365 | { |
| 4366 | struct mem_cgroup_eventfd_list *ev; |
| 4367 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4368 | list_for_each_entry(ev, &memcg->oom_notify, list) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4369 | eventfd_signal(ev->eventfd, 1); |
| 4370 | return 0; |
| 4371 | } |
| 4372 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4373 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4374 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4375 | struct mem_cgroup *iter; |
| 4376 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4377 | for_each_mem_cgroup_tree(iter, memcg) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4378 | mem_cgroup_oom_notify_cb(iter); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4379 | } |
| 4380 | |
| 4381 | static int mem_cgroup_usage_register_event(struct cgroup *cgrp, |
| 4382 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4383 | { |
| 4384 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4385 | struct mem_cgroup_thresholds *thresholds; |
| 4386 | struct mem_cgroup_threshold_ary *new; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4387 | int type = MEMFILE_TYPE(cft->private); |
| 4388 | u64 threshold, usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4389 | int i, size, ret; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4390 | |
| 4391 | ret = res_counter_memparse_write_strategy(args, &threshold); |
| 4392 | if (ret) |
| 4393 | return ret; |
| 4394 | |
| 4395 | mutex_lock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4396 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4397 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4398 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4399 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4400 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4401 | else |
| 4402 | BUG(); |
| 4403 | |
| 4404 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 4405 | |
| 4406 | /* Check if a threshold crossed before adding a new one */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4407 | if (thresholds->primary) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4408 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4409 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4410 | size = thresholds->primary ? thresholds->primary->size + 1 : 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4411 | |
| 4412 | /* Allocate memory for new array of thresholds */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4413 | new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4414 | GFP_KERNEL); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4415 | if (!new) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4416 | ret = -ENOMEM; |
| 4417 | goto unlock; |
| 4418 | } |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4419 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4420 | |
| 4421 | /* Copy thresholds (if any) to new array */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4422 | if (thresholds->primary) { |
| 4423 | memcpy(new->entries, thresholds->primary->entries, (size - 1) * |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4424 | sizeof(struct mem_cgroup_threshold)); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4425 | } |
| 4426 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4427 | /* Add new threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4428 | new->entries[size - 1].eventfd = eventfd; |
| 4429 | new->entries[size - 1].threshold = threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4430 | |
| 4431 | /* Sort thresholds. Registering of new threshold isn't time-critical */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4432 | sort(new->entries, size, sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4433 | compare_thresholds, NULL); |
| 4434 | |
| 4435 | /* Find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4436 | new->current_threshold = -1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4437 | for (i = 0; i < size; i++) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4438 | if (new->entries[i].threshold < usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4439 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4440 | * new->current_threshold will not be used until |
| 4441 | * rcu_assign_pointer(), so it's safe to increment |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4442 | * it here. |
| 4443 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4444 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4445 | } |
| 4446 | } |
| 4447 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4448 | /* Free old spare buffer and save old primary buffer as spare */ |
| 4449 | kfree(thresholds->spare); |
| 4450 | thresholds->spare = thresholds->primary; |
| 4451 | |
| 4452 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4453 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4454 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4455 | synchronize_rcu(); |
| 4456 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4457 | unlock: |
| 4458 | mutex_unlock(&memcg->thresholds_lock); |
| 4459 | |
| 4460 | return ret; |
| 4461 | } |
| 4462 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4463 | static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4464 | struct cftype *cft, struct eventfd_ctx *eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4465 | { |
| 4466 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4467 | struct mem_cgroup_thresholds *thresholds; |
| 4468 | struct mem_cgroup_threshold_ary *new; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4469 | int type = MEMFILE_TYPE(cft->private); |
| 4470 | u64 usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4471 | int i, j, size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4472 | |
| 4473 | mutex_lock(&memcg->thresholds_lock); |
| 4474 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4475 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4476 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4477 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4478 | else |
| 4479 | BUG(); |
| 4480 | |
| 4481 | /* |
| 4482 | * Something went wrong if we trying to unregister a threshold |
| 4483 | * if we don't have thresholds |
| 4484 | */ |
| 4485 | BUG_ON(!thresholds); |
| 4486 | |
| 4487 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 4488 | |
| 4489 | /* Check if a threshold crossed before removing */ |
| 4490 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4491 | |
| 4492 | /* Calculate new number of threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4493 | size = 0; |
| 4494 | for (i = 0; i < thresholds->primary->size; i++) { |
| 4495 | if (thresholds->primary->entries[i].eventfd != eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4496 | size++; |
| 4497 | } |
| 4498 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4499 | new = thresholds->spare; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4500 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4501 | /* Set thresholds array to NULL if we don't have thresholds */ |
| 4502 | if (!size) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4503 | kfree(new); |
| 4504 | new = NULL; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4505 | goto swap_buffers; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4506 | } |
| 4507 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4508 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4509 | |
| 4510 | /* Copy thresholds and find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4511 | new->current_threshold = -1; |
| 4512 | for (i = 0, j = 0; i < thresholds->primary->size; i++) { |
| 4513 | if (thresholds->primary->entries[i].eventfd == eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4514 | continue; |
| 4515 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4516 | new->entries[j] = thresholds->primary->entries[i]; |
| 4517 | if (new->entries[j].threshold < usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4518 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4519 | * new->current_threshold will not be used |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4520 | * until rcu_assign_pointer(), so it's safe to increment |
| 4521 | * it here. |
| 4522 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4523 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4524 | } |
| 4525 | j++; |
| 4526 | } |
| 4527 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4528 | swap_buffers: |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4529 | /* Swap primary and spare array */ |
| 4530 | thresholds->spare = thresholds->primary; |
| 4531 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4532 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4533 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4534 | synchronize_rcu(); |
| 4535 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4536 | mutex_unlock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4537 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4538 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4539 | static int mem_cgroup_oom_register_event(struct cgroup *cgrp, |
| 4540 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
| 4541 | { |
| 4542 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4543 | struct mem_cgroup_eventfd_list *event; |
| 4544 | int type = MEMFILE_TYPE(cft->private); |
| 4545 | |
| 4546 | BUG_ON(type != _OOM_TYPE); |
| 4547 | event = kmalloc(sizeof(*event), GFP_KERNEL); |
| 4548 | if (!event) |
| 4549 | return -ENOMEM; |
| 4550 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4551 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4552 | |
| 4553 | event->eventfd = eventfd; |
| 4554 | list_add(&event->list, &memcg->oom_notify); |
| 4555 | |
| 4556 | /* already in OOM ? */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 4557 | if (atomic_read(&memcg->under_oom)) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4558 | eventfd_signal(eventfd, 1); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4559 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4560 | |
| 4561 | return 0; |
| 4562 | } |
| 4563 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4564 | static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4565 | struct cftype *cft, struct eventfd_ctx *eventfd) |
| 4566 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4567 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4568 | struct mem_cgroup_eventfd_list *ev, *tmp; |
| 4569 | int type = MEMFILE_TYPE(cft->private); |
| 4570 | |
| 4571 | BUG_ON(type != _OOM_TYPE); |
| 4572 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4573 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4574 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4575 | list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) { |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4576 | if (ev->eventfd == eventfd) { |
| 4577 | list_del(&ev->list); |
| 4578 | kfree(ev); |
| 4579 | } |
| 4580 | } |
| 4581 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4582 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4583 | } |
| 4584 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4585 | static int mem_cgroup_oom_control_read(struct cgroup *cgrp, |
| 4586 | struct cftype *cft, struct cgroup_map_cb *cb) |
| 4587 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4588 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4589 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4590 | cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4591 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4592 | if (atomic_read(&memcg->under_oom)) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4593 | cb->fill(cb, "under_oom", 1); |
| 4594 | else |
| 4595 | cb->fill(cb, "under_oom", 0); |
| 4596 | return 0; |
| 4597 | } |
| 4598 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4599 | static int mem_cgroup_oom_control_write(struct cgroup *cgrp, |
| 4600 | struct cftype *cft, u64 val) |
| 4601 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4602 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4603 | struct mem_cgroup *parent; |
| 4604 | |
| 4605 | /* cannot set to root cgroup and only 0 and 1 are allowed */ |
| 4606 | if (!cgrp->parent || !((val == 0) || (val == 1))) |
| 4607 | return -EINVAL; |
| 4608 | |
| 4609 | parent = mem_cgroup_from_cont(cgrp->parent); |
| 4610 | |
| 4611 | cgroup_lock(); |
| 4612 | /* oom-kill-disable is a flag for subhierarchy. */ |
| 4613 | if ((parent->use_hierarchy) || |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4614 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4615 | cgroup_unlock(); |
| 4616 | return -EINVAL; |
| 4617 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4618 | memcg->oom_kill_disable = val; |
KAMEZAWA Hiroyuki | 4d845eb | 2010-06-29 15:05:18 -0700 | [diff] [blame] | 4619 | if (!val) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4620 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4621 | cgroup_unlock(); |
| 4622 | return 0; |
| 4623 | } |
| 4624 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4625 | #ifdef CONFIG_NUMA |
| 4626 | static const struct file_operations mem_control_numa_stat_file_operations = { |
| 4627 | .read = seq_read, |
| 4628 | .llseek = seq_lseek, |
| 4629 | .release = single_release, |
| 4630 | }; |
| 4631 | |
| 4632 | static int mem_control_numa_stat_open(struct inode *unused, struct file *file) |
| 4633 | { |
| 4634 | struct cgroup *cont = file->f_dentry->d_parent->d_fsdata; |
| 4635 | |
| 4636 | file->f_op = &mem_control_numa_stat_file_operations; |
| 4637 | return single_open(file, mem_control_numa_stat_show, cont); |
| 4638 | } |
| 4639 | #endif /* CONFIG_NUMA */ |
| 4640 | |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 4641 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM |
| 4642 | static u64 kmem_limit_independent_read(struct cgroup *cgroup, struct cftype *cft) |
| 4643 | { |
| 4644 | return mem_cgroup_from_cont(cgroup)->kmem_independent_accounting; |
| 4645 | } |
| 4646 | |
| 4647 | static int kmem_limit_independent_write(struct cgroup *cgroup, struct cftype *cft, |
| 4648 | u64 val) |
| 4649 | { |
| 4650 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup); |
| 4651 | struct mem_cgroup *parent = parent_mem_cgroup(memcg); |
| 4652 | |
| 4653 | val = !!val; |
| 4654 | |
| 4655 | /* |
| 4656 | * This follows the same hierarchy restrictions than |
| 4657 | * mem_cgroup_hierarchy_write() |
| 4658 | */ |
| 4659 | if (!parent || !parent->use_hierarchy) { |
| 4660 | if (list_empty(&cgroup->children)) |
| 4661 | memcg->kmem_independent_accounting = val; |
| 4662 | else |
| 4663 | return -EBUSY; |
| 4664 | } |
| 4665 | else |
| 4666 | return -EINVAL; |
| 4667 | |
| 4668 | return 0; |
| 4669 | } |
| 4670 | static struct cftype kmem_cgroup_files[] = { |
| 4671 | { |
| 4672 | .name = "independent_kmem_limit", |
| 4673 | .read_u64 = kmem_limit_independent_read, |
| 4674 | .write_u64 = kmem_limit_independent_write, |
| 4675 | }, |
| 4676 | { |
| 4677 | .name = "kmem.usage_in_bytes", |
| 4678 | .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE), |
| 4679 | .read_u64 = mem_cgroup_read, |
| 4680 | }, |
| 4681 | { |
| 4682 | .name = "kmem.limit_in_bytes", |
| 4683 | .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT), |
| 4684 | .read_u64 = mem_cgroup_read, |
| 4685 | }, |
| 4686 | }; |
| 4687 | |
| 4688 | static int register_kmem_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4689 | { |
| 4690 | int ret = 0; |
| 4691 | |
| 4692 | ret = cgroup_add_files(cont, ss, kmem_cgroup_files, |
| 4693 | ARRAY_SIZE(kmem_cgroup_files)); |
| 4694 | return ret; |
| 4695 | }; |
| 4696 | |
| 4697 | #else |
| 4698 | static int register_kmem_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4699 | { |
| 4700 | return 0; |
| 4701 | } |
| 4702 | #endif |
| 4703 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4704 | static struct cftype mem_cgroup_files[] = { |
| 4705 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4706 | .name = "usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4707 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4708 | .read_u64 = mem_cgroup_read, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4709 | .register_event = mem_cgroup_usage_register_event, |
| 4710 | .unregister_event = mem_cgroup_usage_unregister_event, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4711 | }, |
| 4712 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4713 | .name = "max_usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4714 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4715 | .trigger = mem_cgroup_reset, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4716 | .read_u64 = mem_cgroup_read, |
| 4717 | }, |
| 4718 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4719 | .name = "limit_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4720 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 4721 | .write_string = mem_cgroup_write, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4722 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4723 | }, |
| 4724 | { |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 4725 | .name = "soft_limit_in_bytes", |
| 4726 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), |
| 4727 | .write_string = mem_cgroup_write, |
| 4728 | .read_u64 = mem_cgroup_read, |
| 4729 | }, |
| 4730 | { |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4731 | .name = "failcnt", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4732 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4733 | .trigger = mem_cgroup_reset, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4734 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4735 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 4736 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4737 | .name = "stat", |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 4738 | .read_map = mem_control_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4739 | }, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4740 | { |
| 4741 | .name = "force_empty", |
| 4742 | .trigger = mem_cgroup_force_empty_write, |
| 4743 | }, |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4744 | { |
| 4745 | .name = "use_hierarchy", |
| 4746 | .write_u64 = mem_cgroup_hierarchy_write, |
| 4747 | .read_u64 = mem_cgroup_hierarchy_read, |
| 4748 | }, |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4749 | { |
| 4750 | .name = "swappiness", |
| 4751 | .read_u64 = mem_cgroup_swappiness_read, |
| 4752 | .write_u64 = mem_cgroup_swappiness_write, |
| 4753 | }, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4754 | { |
| 4755 | .name = "move_charge_at_immigrate", |
| 4756 | .read_u64 = mem_cgroup_move_charge_read, |
| 4757 | .write_u64 = mem_cgroup_move_charge_write, |
| 4758 | }, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4759 | { |
| 4760 | .name = "oom_control", |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4761 | .read_map = mem_cgroup_oom_control_read, |
| 4762 | .write_u64 = mem_cgroup_oom_control_write, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4763 | .register_event = mem_cgroup_oom_register_event, |
| 4764 | .unregister_event = mem_cgroup_oom_unregister_event, |
| 4765 | .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL), |
| 4766 | }, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4767 | #ifdef CONFIG_NUMA |
| 4768 | { |
| 4769 | .name = "numa_stat", |
| 4770 | .open = mem_control_numa_stat_open, |
KAMEZAWA Hiroyuki | 8957712 | 2011-06-15 15:08:41 -0700 | [diff] [blame] | 4771 | .mode = S_IRUGO, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4772 | }, |
| 4773 | #endif |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4774 | }; |
| 4775 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4776 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 4777 | static struct cftype memsw_cgroup_files[] = { |
| 4778 | { |
| 4779 | .name = "memsw.usage_in_bytes", |
| 4780 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), |
| 4781 | .read_u64 = mem_cgroup_read, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4782 | .register_event = mem_cgroup_usage_register_event, |
| 4783 | .unregister_event = mem_cgroup_usage_unregister_event, |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4784 | }, |
| 4785 | { |
| 4786 | .name = "memsw.max_usage_in_bytes", |
| 4787 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), |
| 4788 | .trigger = mem_cgroup_reset, |
| 4789 | .read_u64 = mem_cgroup_read, |
| 4790 | }, |
| 4791 | { |
| 4792 | .name = "memsw.limit_in_bytes", |
| 4793 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), |
| 4794 | .write_string = mem_cgroup_write, |
| 4795 | .read_u64 = mem_cgroup_read, |
| 4796 | }, |
| 4797 | { |
| 4798 | .name = "memsw.failcnt", |
| 4799 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), |
| 4800 | .trigger = mem_cgroup_reset, |
| 4801 | .read_u64 = mem_cgroup_read, |
| 4802 | }, |
| 4803 | }; |
| 4804 | |
| 4805 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4806 | { |
| 4807 | if (!do_swap_account) |
| 4808 | return 0; |
| 4809 | return cgroup_add_files(cont, ss, memsw_cgroup_files, |
| 4810 | ARRAY_SIZE(memsw_cgroup_files)); |
| 4811 | }; |
| 4812 | #else |
| 4813 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4814 | { |
| 4815 | return 0; |
| 4816 | } |
| 4817 | #endif |
| 4818 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4819 | 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] | 4820 | { |
| 4821 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4822 | struct mem_cgroup_per_zone *mz; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 4823 | enum lru_list l; |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 4824 | int zone, tmp = node; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4825 | /* |
| 4826 | * This routine is called against possible nodes. |
| 4827 | * But it's BUG to call kmalloc() against offline node. |
| 4828 | * |
| 4829 | * TODO: this routine can waste much memory for nodes which will |
| 4830 | * never be onlined. It's better to use memory hotplug callback |
| 4831 | * function. |
| 4832 | */ |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 4833 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 4834 | tmp = -1; |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4835 | pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4836 | if (!pn) |
| 4837 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4838 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4839 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 4840 | mz = &pn->zoneinfo[zone]; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 4841 | for_each_lru(l) |
| 4842 | INIT_LIST_HEAD(&mz->lists[l]); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4843 | mz->usage_in_excess = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4844 | mz->on_tree = false; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4845 | mz->mem = memcg; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4846 | } |
Igor Mammedov | 0a619e5 | 2011-11-02 13:38:21 -0700 | [diff] [blame] | 4847 | memcg->info.nodeinfo[node] = pn; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4848 | return 0; |
| 4849 | } |
| 4850 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4851 | 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] | 4852 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4853 | kfree(memcg->info.nodeinfo[node]); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4854 | } |
| 4855 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4856 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 4857 | { |
| 4858 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4859 | int size = sizeof(struct mem_cgroup); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4860 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4861 | /* Can be very big if MAX_NUMNODES is very big */ |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4862 | if (size < PAGE_SIZE) |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4863 | mem = kzalloc(size, GFP_KERNEL); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4864 | else |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4865 | mem = vzalloc(size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4866 | |
Dan Carpenter | e7bbcdf | 2010-03-23 13:35:12 -0700 | [diff] [blame] | 4867 | if (!mem) |
| 4868 | return NULL; |
| 4869 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4870 | mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu); |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 4871 | if (!mem->stat) |
| 4872 | goto out_free; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 4873 | spin_lock_init(&mem->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4874 | return mem; |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 4875 | |
| 4876 | out_free: |
| 4877 | if (size < PAGE_SIZE) |
| 4878 | kfree(mem); |
| 4879 | else |
| 4880 | vfree(mem); |
| 4881 | return NULL; |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4882 | } |
| 4883 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4884 | /* |
| 4885 | * At destroying mem_cgroup, references from swap_cgroup can remain. |
| 4886 | * (scanning all at force_empty is too costly...) |
| 4887 | * |
| 4888 | * Instead of clearing all references at force_empty, we remember |
| 4889 | * the number of reference from swap_cgroup and free mem_cgroup when |
| 4890 | * it goes down to 0. |
| 4891 | * |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4892 | * Removal of cgroup itself succeeds regardless of refs from swap. |
| 4893 | */ |
| 4894 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4895 | static void __mem_cgroup_free(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4896 | { |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4897 | int node; |
| 4898 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4899 | mem_cgroup_remove_from_trees(memcg); |
| 4900 | free_css_id(&mem_cgroup_subsys, &memcg->css); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4901 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4902 | for_each_node_state(node, N_POSSIBLE) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4903 | free_mem_cgroup_per_zone_info(memcg, node); |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4904 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4905 | free_percpu(memcg->stat); |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4906 | if (sizeof(struct mem_cgroup) < PAGE_SIZE) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4907 | kfree(memcg); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4908 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4909 | vfree(memcg); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4910 | } |
| 4911 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4912 | static void mem_cgroup_get(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4913 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4914 | atomic_inc(&memcg->refcnt); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4915 | } |
| 4916 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4917 | static void __mem_cgroup_put(struct mem_cgroup *memcg, int count) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4918 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4919 | if (atomic_sub_and_test(count, &memcg->refcnt)) { |
| 4920 | struct mem_cgroup *parent = parent_mem_cgroup(memcg); |
| 4921 | __mem_cgroup_free(memcg); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4922 | if (parent) |
| 4923 | mem_cgroup_put(parent); |
| 4924 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4925 | } |
| 4926 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4927 | static void mem_cgroup_put(struct mem_cgroup *memcg) |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4928 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4929 | __mem_cgroup_put(memcg, 1); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4930 | } |
| 4931 | |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4932 | /* |
| 4933 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. |
| 4934 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4935 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg) |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4936 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4937 | if (!memcg->res.parent) |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4938 | return NULL; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4939 | return mem_cgroup_from_res_counter(memcg->res.parent, res); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4940 | } |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4941 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4942 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 4943 | static void __init enable_swap_cgroup(void) |
| 4944 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 4945 | if (!mem_cgroup_disabled() && really_do_swap_account) |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4946 | do_swap_account = 1; |
| 4947 | } |
| 4948 | #else |
| 4949 | static void __init enable_swap_cgroup(void) |
| 4950 | { |
| 4951 | } |
| 4952 | #endif |
| 4953 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4954 | static int mem_cgroup_soft_limit_tree_init(void) |
| 4955 | { |
| 4956 | struct mem_cgroup_tree_per_node *rtpn; |
| 4957 | struct mem_cgroup_tree_per_zone *rtpz; |
| 4958 | int tmp, node, zone; |
| 4959 | |
| 4960 | for_each_node_state(node, N_POSSIBLE) { |
| 4961 | tmp = node; |
| 4962 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 4963 | tmp = -1; |
| 4964 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp); |
| 4965 | if (!rtpn) |
| 4966 | return 1; |
| 4967 | |
| 4968 | soft_limit_tree.rb_tree_per_node[node] = rtpn; |
| 4969 | |
| 4970 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 4971 | rtpz = &rtpn->rb_tree_per_zone[zone]; |
| 4972 | rtpz->rb_root = RB_ROOT; |
| 4973 | spin_lock_init(&rtpz->lock); |
| 4974 | } |
| 4975 | } |
| 4976 | return 0; |
| 4977 | } |
| 4978 | |
Li Zefan | 0eb253e | 2009-01-15 13:51:25 -0800 | [diff] [blame] | 4979 | static struct cgroup_subsys_state * __ref |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4980 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 4981 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4982 | struct mem_cgroup *memcg, *parent; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4983 | long error = -ENOMEM; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4984 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4985 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4986 | memcg = mem_cgroup_alloc(); |
| 4987 | if (!memcg) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4988 | return ERR_PTR(error); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 4989 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4990 | for_each_node_state(node, N_POSSIBLE) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4991 | if (alloc_mem_cgroup_per_zone_info(memcg, node)) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4992 | goto free_out; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4993 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4994 | /* root ? */ |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4995 | if (cont->parent == NULL) { |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 4996 | int cpu; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4997 | enable_swap_cgroup(); |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4998 | parent = NULL; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4999 | root_mem_cgroup = memcg; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5000 | if (mem_cgroup_soft_limit_tree_init()) |
| 5001 | goto free_out; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 5002 | for_each_possible_cpu(cpu) { |
| 5003 | struct memcg_stock_pcp *stock = |
| 5004 | &per_cpu(memcg_stock, cpu); |
| 5005 | INIT_WORK(&stock->work, drain_local_stock); |
| 5006 | } |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 5007 | hotcpu_notifier(memcg_cpu_hotplug_callback, 0); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5008 | } else { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5009 | parent = mem_cgroup_from_cont(cont->parent); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5010 | memcg->use_hierarchy = parent->use_hierarchy; |
| 5011 | memcg->oom_kill_disable = parent->oom_kill_disable; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5012 | } |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5013 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5014 | if (parent && parent->use_hierarchy) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5015 | res_counter_init(&memcg->res, &parent->res); |
| 5016 | res_counter_init(&memcg->memsw, &parent->memsw); |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 5017 | res_counter_init(&memcg->kmem, &parent->kmem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5018 | /* |
| 5019 | * We increment refcnt of the parent to ensure that we can |
| 5020 | * safely access it on res_counter_charge/uncharge. |
| 5021 | * This refcnt will be decremented when freeing this |
| 5022 | * mem_cgroup(see mem_cgroup_put). |
| 5023 | */ |
| 5024 | mem_cgroup_get(parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5025 | } else { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5026 | res_counter_init(&memcg->res, NULL); |
| 5027 | res_counter_init(&memcg->memsw, NULL); |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 5028 | res_counter_init(&memcg->kmem, NULL); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5029 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5030 | memcg->last_scanned_child = 0; |
| 5031 | memcg->last_scanned_node = MAX_NUMNODES; |
| 5032 | INIT_LIST_HEAD(&memcg->oom_notify); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 5033 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5034 | if (parent) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5035 | memcg->swappiness = mem_cgroup_swappiness(parent); |
| 5036 | atomic_set(&memcg->refcnt, 1); |
| 5037 | memcg->move_charge_at_immigrate = 0; |
| 5038 | mutex_init(&memcg->thresholds_lock); |
| 5039 | return &memcg->css; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5040 | free_out: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5041 | __mem_cgroup_free(memcg); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 5042 | root_mem_cgroup = NULL; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5043 | return ERR_PTR(error); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5044 | } |
| 5045 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 5046 | static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5047 | struct cgroup *cont) |
| 5048 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5049 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 5050 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5051 | return mem_cgroup_force_empty(memcg, false); |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5052 | } |
| 5053 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5054 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 5055 | struct cgroup *cont) |
| 5056 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5057 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 5058 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5059 | mem_cgroup_put(memcg); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5060 | } |
| 5061 | |
| 5062 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 5063 | struct cgroup *cont) |
| 5064 | { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5065 | int ret; |
| 5066 | |
| 5067 | ret = cgroup_add_files(cont, ss, mem_cgroup_files, |
| 5068 | ARRAY_SIZE(mem_cgroup_files)); |
| 5069 | |
| 5070 | if (!ret) |
| 5071 | ret = register_memsw_files(cont, ss); |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 5072 | |
| 5073 | if (!ret) |
| 5074 | ret = register_kmem_files(cont, ss); |
| 5075 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5076 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5077 | } |
| 5078 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5079 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5080 | /* Handlers for move charge at task migration. */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5081 | #define PRECHARGE_COUNT_AT_ONCE 256 |
| 5082 | static int mem_cgroup_do_precharge(unsigned long count) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5083 | { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5084 | int ret = 0; |
| 5085 | int batch_count = PRECHARGE_COUNT_AT_ONCE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5086 | struct mem_cgroup *memcg = mc.to; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5087 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5088 | if (mem_cgroup_is_root(memcg)) { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5089 | mc.precharge += count; |
| 5090 | /* we don't need css_get for root */ |
| 5091 | return ret; |
| 5092 | } |
| 5093 | /* try to charge at once */ |
| 5094 | if (count > 1) { |
| 5095 | struct res_counter *dummy; |
| 5096 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5097 | * "memcg" cannot be under rmdir() because we've already checked |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5098 | * by cgroup_lock_live_cgroup() that it is not removed and we |
| 5099 | * are still under the same cgroup_mutex. So we can postpone |
| 5100 | * css_get(). |
| 5101 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5102 | if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy)) |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5103 | goto one_by_one; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5104 | if (do_swap_account && res_counter_charge(&memcg->memsw, |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5105 | PAGE_SIZE * count, &dummy)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5106 | res_counter_uncharge(&memcg->res, PAGE_SIZE * count); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5107 | goto one_by_one; |
| 5108 | } |
| 5109 | mc.precharge += count; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5110 | return ret; |
| 5111 | } |
| 5112 | one_by_one: |
| 5113 | /* fall back to one by one charge */ |
| 5114 | while (count--) { |
| 5115 | if (signal_pending(current)) { |
| 5116 | ret = -EINTR; |
| 5117 | break; |
| 5118 | } |
| 5119 | if (!batch_count--) { |
| 5120 | batch_count = PRECHARGE_COUNT_AT_ONCE; |
| 5121 | cond_resched(); |
| 5122 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5123 | ret = __mem_cgroup_try_charge(NULL, |
| 5124 | GFP_KERNEL, 1, &memcg, false); |
| 5125 | if (ret || !memcg) |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5126 | /* mem_cgroup_clear_mc() will do uncharge later */ |
| 5127 | return -ENOMEM; |
| 5128 | mc.precharge++; |
| 5129 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5130 | return ret; |
| 5131 | } |
| 5132 | |
| 5133 | /** |
| 5134 | * is_target_pte_for_mc - check a pte whether it is valid for move charge |
| 5135 | * @vma: the vma the pte to be checked belongs |
| 5136 | * @addr: the address corresponding to the pte to be checked |
| 5137 | * @ptent: the pte to be checked |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5138 | * @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] | 5139 | * |
| 5140 | * Returns |
| 5141 | * 0(MC_TARGET_NONE): if the pte is not a target for move charge. |
| 5142 | * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for |
| 5143 | * move charge. if @target is not NULL, the page is stored in target->page |
| 5144 | * with extra refcnt got(Callers should handle it). |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5145 | * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a |
| 5146 | * target for charge migration. if @target is not NULL, the entry is stored |
| 5147 | * in target->ent. |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5148 | * |
| 5149 | * Called with pte lock held. |
| 5150 | */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5151 | union mc_target { |
| 5152 | struct page *page; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5153 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5154 | }; |
| 5155 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5156 | enum mc_target_type { |
| 5157 | MC_TARGET_NONE, /* not used */ |
| 5158 | MC_TARGET_PAGE, |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5159 | MC_TARGET_SWAP, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5160 | }; |
| 5161 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5162 | static struct page *mc_handle_present_pte(struct vm_area_struct *vma, |
| 5163 | unsigned long addr, pte_t ptent) |
| 5164 | { |
| 5165 | struct page *page = vm_normal_page(vma, addr, ptent); |
| 5166 | |
| 5167 | if (!page || !page_mapped(page)) |
| 5168 | return NULL; |
| 5169 | if (PageAnon(page)) { |
| 5170 | /* we don't move shared anon */ |
| 5171 | if (!move_anon() || page_mapcount(page) > 2) |
| 5172 | return NULL; |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5173 | } else if (!move_file()) |
| 5174 | /* we ignore mapcount for file pages */ |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5175 | return NULL; |
| 5176 | if (!get_page_unless_zero(page)) |
| 5177 | return NULL; |
| 5178 | |
| 5179 | return page; |
| 5180 | } |
| 5181 | |
| 5182 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
| 5183 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5184 | { |
| 5185 | int usage_count; |
| 5186 | struct page *page = NULL; |
| 5187 | swp_entry_t ent = pte_to_swp_entry(ptent); |
| 5188 | |
| 5189 | if (!move_anon() || non_swap_entry(ent)) |
| 5190 | return NULL; |
| 5191 | usage_count = mem_cgroup_count_swap_user(ent, &page); |
| 5192 | if (usage_count > 1) { /* we don't move shared anon */ |
| 5193 | if (page) |
| 5194 | put_page(page); |
| 5195 | return NULL; |
| 5196 | } |
| 5197 | if (do_swap_account) |
| 5198 | entry->val = ent.val; |
| 5199 | |
| 5200 | return page; |
| 5201 | } |
| 5202 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5203 | static struct page *mc_handle_file_pte(struct vm_area_struct *vma, |
| 5204 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5205 | { |
| 5206 | struct page *page = NULL; |
| 5207 | struct inode *inode; |
| 5208 | struct address_space *mapping; |
| 5209 | pgoff_t pgoff; |
| 5210 | |
| 5211 | if (!vma->vm_file) /* anonymous vma */ |
| 5212 | return NULL; |
| 5213 | if (!move_file()) |
| 5214 | return NULL; |
| 5215 | |
| 5216 | inode = vma->vm_file->f_path.dentry->d_inode; |
| 5217 | mapping = vma->vm_file->f_mapping; |
| 5218 | if (pte_none(ptent)) |
| 5219 | pgoff = linear_page_index(vma, addr); |
| 5220 | else /* pte_file(ptent) is true */ |
| 5221 | pgoff = pte_to_pgoff(ptent); |
| 5222 | |
| 5223 | /* 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] | 5224 | page = find_get_page(mapping, pgoff); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5225 | |
Hugh Dickins | aa3b189 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 5226 | #ifdef CONFIG_SWAP |
| 5227 | /* shmem/tmpfs may report page out on swap: account for that too. */ |
| 5228 | if (radix_tree_exceptional_entry(page)) { |
| 5229 | swp_entry_t swap = radix_to_swp_entry(page); |
| 5230 | if (do_swap_account) |
| 5231 | *entry = swap; |
| 5232 | page = find_get_page(&swapper_space, swap.val); |
| 5233 | } |
| 5234 | #endif |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5235 | return page; |
| 5236 | } |
| 5237 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5238 | static int is_target_pte_for_mc(struct vm_area_struct *vma, |
| 5239 | unsigned long addr, pte_t ptent, union mc_target *target) |
| 5240 | { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5241 | struct page *page = NULL; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5242 | struct page_cgroup *pc; |
| 5243 | int ret = 0; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5244 | swp_entry_t ent = { .val = 0 }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5245 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5246 | if (pte_present(ptent)) |
| 5247 | page = mc_handle_present_pte(vma, addr, ptent); |
| 5248 | else if (is_swap_pte(ptent)) |
| 5249 | page = mc_handle_swap_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5250 | else if (pte_none(ptent) || pte_file(ptent)) |
| 5251 | page = mc_handle_file_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5252 | |
| 5253 | if (!page && !ent.val) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5254 | return 0; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5255 | if (page) { |
| 5256 | pc = lookup_page_cgroup(page); |
| 5257 | /* |
| 5258 | * Do only loose check w/o page_cgroup lock. |
| 5259 | * mem_cgroup_move_account() checks the pc is valid or not under |
| 5260 | * the lock. |
| 5261 | */ |
| 5262 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { |
| 5263 | ret = MC_TARGET_PAGE; |
| 5264 | if (target) |
| 5265 | target->page = page; |
| 5266 | } |
| 5267 | if (!ret || !target) |
| 5268 | put_page(page); |
| 5269 | } |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5270 | /* There is a swap entry and a page doesn't exist or isn't charged */ |
| 5271 | if (ent.val && !ret && |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5272 | css_id(&mc.from->css) == lookup_swap_cgroup(ent)) { |
| 5273 | ret = MC_TARGET_SWAP; |
| 5274 | if (target) |
| 5275 | target->ent = ent; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5276 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5277 | return ret; |
| 5278 | } |
| 5279 | |
| 5280 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, |
| 5281 | unsigned long addr, unsigned long end, |
| 5282 | struct mm_walk *walk) |
| 5283 | { |
| 5284 | struct vm_area_struct *vma = walk->private; |
| 5285 | pte_t *pte; |
| 5286 | spinlock_t *ptl; |
| 5287 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 5288 | split_huge_page_pmd(walk->mm, pmd); |
| 5289 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5290 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5291 | for (; addr != end; pte++, addr += PAGE_SIZE) |
| 5292 | if (is_target_pte_for_mc(vma, addr, *pte, NULL)) |
| 5293 | mc.precharge++; /* increment precharge temporarily */ |
| 5294 | pte_unmap_unlock(pte - 1, ptl); |
| 5295 | cond_resched(); |
| 5296 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5297 | return 0; |
| 5298 | } |
| 5299 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5300 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
| 5301 | { |
| 5302 | unsigned long precharge; |
| 5303 | struct vm_area_struct *vma; |
| 5304 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5305 | down_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5306 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5307 | struct mm_walk mem_cgroup_count_precharge_walk = { |
| 5308 | .pmd_entry = mem_cgroup_count_precharge_pte_range, |
| 5309 | .mm = mm, |
| 5310 | .private = vma, |
| 5311 | }; |
| 5312 | if (is_vm_hugetlb_page(vma)) |
| 5313 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5314 | walk_page_range(vma->vm_start, vma->vm_end, |
| 5315 | &mem_cgroup_count_precharge_walk); |
| 5316 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5317 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5318 | |
| 5319 | precharge = mc.precharge; |
| 5320 | mc.precharge = 0; |
| 5321 | |
| 5322 | return precharge; |
| 5323 | } |
| 5324 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5325 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
| 5326 | { |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5327 | unsigned long precharge = mem_cgroup_count_precharge(mm); |
| 5328 | |
| 5329 | VM_BUG_ON(mc.moving_task); |
| 5330 | mc.moving_task = current; |
| 5331 | return mem_cgroup_do_precharge(precharge); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5332 | } |
| 5333 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5334 | /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */ |
| 5335 | static void __mem_cgroup_clear_mc(void) |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5336 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5337 | struct mem_cgroup *from = mc.from; |
| 5338 | struct mem_cgroup *to = mc.to; |
| 5339 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5340 | /* we must uncharge all the leftover precharges from mc.to */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5341 | if (mc.precharge) { |
| 5342 | __mem_cgroup_cancel_charge(mc.to, mc.precharge); |
| 5343 | mc.precharge = 0; |
| 5344 | } |
| 5345 | /* |
| 5346 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so |
| 5347 | * we must uncharge here. |
| 5348 | */ |
| 5349 | if (mc.moved_charge) { |
| 5350 | __mem_cgroup_cancel_charge(mc.from, mc.moved_charge); |
| 5351 | mc.moved_charge = 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5352 | } |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5353 | /* we must fixup refcnts and charges */ |
| 5354 | if (mc.moved_swap) { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5355 | /* uncharge swap account from the old cgroup */ |
| 5356 | if (!mem_cgroup_is_root(mc.from)) |
| 5357 | res_counter_uncharge(&mc.from->memsw, |
| 5358 | PAGE_SIZE * mc.moved_swap); |
| 5359 | __mem_cgroup_put(mc.from, mc.moved_swap); |
| 5360 | |
| 5361 | if (!mem_cgroup_is_root(mc.to)) { |
| 5362 | /* |
| 5363 | * we charged both to->res and to->memsw, so we should |
| 5364 | * uncharge to->res. |
| 5365 | */ |
| 5366 | res_counter_uncharge(&mc.to->res, |
| 5367 | PAGE_SIZE * mc.moved_swap); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5368 | } |
| 5369 | /* we've already done mem_cgroup_get(mc.to) */ |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5370 | mc.moved_swap = 0; |
| 5371 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5372 | memcg_oom_recover(from); |
| 5373 | memcg_oom_recover(to); |
| 5374 | wake_up_all(&mc.waitq); |
| 5375 | } |
| 5376 | |
| 5377 | static void mem_cgroup_clear_mc(void) |
| 5378 | { |
| 5379 | struct mem_cgroup *from = mc.from; |
| 5380 | |
| 5381 | /* |
| 5382 | * we must clear moving_task before waking up waiters at the end of |
| 5383 | * task migration. |
| 5384 | */ |
| 5385 | mc.moving_task = NULL; |
| 5386 | __mem_cgroup_clear_mc(); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5387 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5388 | mc.from = NULL; |
| 5389 | mc.to = NULL; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5390 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5391 | mem_cgroup_end_move(from); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5392 | } |
| 5393 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5394 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 5395 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5396 | struct task_struct *p) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5397 | { |
| 5398 | int ret = 0; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5399 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5400 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5401 | if (memcg->move_charge_at_immigrate) { |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5402 | struct mm_struct *mm; |
| 5403 | struct mem_cgroup *from = mem_cgroup_from_task(p); |
| 5404 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5405 | VM_BUG_ON(from == memcg); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5406 | |
| 5407 | mm = get_task_mm(p); |
| 5408 | if (!mm) |
| 5409 | return 0; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5410 | /* We move charges only when we move a owner of the mm */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5411 | if (mm->owner == p) { |
| 5412 | VM_BUG_ON(mc.from); |
| 5413 | VM_BUG_ON(mc.to); |
| 5414 | VM_BUG_ON(mc.precharge); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5415 | VM_BUG_ON(mc.moved_charge); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5416 | VM_BUG_ON(mc.moved_swap); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5417 | mem_cgroup_start_move(from); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5418 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5419 | mc.from = from; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5420 | mc.to = memcg; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5421 | spin_unlock(&mc.lock); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5422 | /* We set mc.moving_task later */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5423 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5424 | ret = mem_cgroup_precharge_mc(mm); |
| 5425 | if (ret) |
| 5426 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5427 | } |
| 5428 | mmput(mm); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5429 | } |
| 5430 | return ret; |
| 5431 | } |
| 5432 | |
| 5433 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 5434 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5435 | struct task_struct *p) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5436 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5437 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5438 | } |
| 5439 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5440 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
| 5441 | unsigned long addr, unsigned long end, |
| 5442 | struct mm_walk *walk) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5443 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5444 | int ret = 0; |
| 5445 | struct vm_area_struct *vma = walk->private; |
| 5446 | pte_t *pte; |
| 5447 | spinlock_t *ptl; |
| 5448 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 5449 | split_huge_page_pmd(walk->mm, pmd); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5450 | retry: |
| 5451 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5452 | for (; addr != end; addr += PAGE_SIZE) { |
| 5453 | pte_t ptent = *(pte++); |
| 5454 | union mc_target target; |
| 5455 | int type; |
| 5456 | struct page *page; |
| 5457 | struct page_cgroup *pc; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5458 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5459 | |
| 5460 | if (!mc.precharge) |
| 5461 | break; |
| 5462 | |
| 5463 | type = is_target_pte_for_mc(vma, addr, ptent, &target); |
| 5464 | switch (type) { |
| 5465 | case MC_TARGET_PAGE: |
| 5466 | page = target.page; |
| 5467 | if (isolate_lru_page(page)) |
| 5468 | goto put; |
| 5469 | pc = lookup_page_cgroup(page); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 5470 | if (!mem_cgroup_move_account(page, 1, pc, |
| 5471 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5472 | mc.precharge--; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5473 | /* we uncharge from mc.from later. */ |
| 5474 | mc.moved_charge++; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5475 | } |
| 5476 | putback_lru_page(page); |
| 5477 | put: /* is_target_pte_for_mc() gets the page */ |
| 5478 | put_page(page); |
| 5479 | break; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5480 | case MC_TARGET_SWAP: |
| 5481 | ent = target.ent; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5482 | if (!mem_cgroup_move_swap_account(ent, |
| 5483 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5484 | mc.precharge--; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5485 | /* we fixup refcnts and charges later. */ |
| 5486 | mc.moved_swap++; |
| 5487 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5488 | break; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5489 | default: |
| 5490 | break; |
| 5491 | } |
| 5492 | } |
| 5493 | pte_unmap_unlock(pte - 1, ptl); |
| 5494 | cond_resched(); |
| 5495 | |
| 5496 | if (addr != end) { |
| 5497 | /* |
| 5498 | * We have consumed all precharges we got in can_attach(). |
| 5499 | * We try charge one by one, but don't do any additional |
| 5500 | * charges to mc.to if we have failed in charge once in attach() |
| 5501 | * phase. |
| 5502 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5503 | ret = mem_cgroup_do_precharge(1); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5504 | if (!ret) |
| 5505 | goto retry; |
| 5506 | } |
| 5507 | |
| 5508 | return ret; |
| 5509 | } |
| 5510 | |
| 5511 | static void mem_cgroup_move_charge(struct mm_struct *mm) |
| 5512 | { |
| 5513 | struct vm_area_struct *vma; |
| 5514 | |
| 5515 | lru_add_drain_all(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5516 | retry: |
| 5517 | if (unlikely(!down_read_trylock(&mm->mmap_sem))) { |
| 5518 | /* |
| 5519 | * Someone who are holding the mmap_sem might be waiting in |
| 5520 | * waitq. So we cancel all extra charges, wake up all waiters, |
| 5521 | * and retry. Because we cancel precharges, we might not be able |
| 5522 | * to move enough charges, but moving charge is a best-effort |
| 5523 | * feature anyway, so it wouldn't be a big problem. |
| 5524 | */ |
| 5525 | __mem_cgroup_clear_mc(); |
| 5526 | cond_resched(); |
| 5527 | goto retry; |
| 5528 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5529 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5530 | int ret; |
| 5531 | struct mm_walk mem_cgroup_move_charge_walk = { |
| 5532 | .pmd_entry = mem_cgroup_move_charge_pte_range, |
| 5533 | .mm = mm, |
| 5534 | .private = vma, |
| 5535 | }; |
| 5536 | if (is_vm_hugetlb_page(vma)) |
| 5537 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5538 | ret = walk_page_range(vma->vm_start, vma->vm_end, |
| 5539 | &mem_cgroup_move_charge_walk); |
| 5540 | if (ret) |
| 5541 | /* |
| 5542 | * means we have consumed all precharges and failed in |
| 5543 | * doing additional charge. Just abandon here. |
| 5544 | */ |
| 5545 | break; |
| 5546 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5547 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5548 | } |
| 5549 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5550 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 5551 | struct cgroup *cont, |
| 5552 | struct cgroup *old_cont, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5553 | struct task_struct *p) |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5554 | { |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5555 | struct mm_struct *mm = get_task_mm(p); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5556 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5557 | if (mm) { |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5558 | if (mc.to) |
| 5559 | mem_cgroup_move_charge(mm); |
| 5560 | put_swap_token(mm); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5561 | mmput(mm); |
| 5562 | } |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5563 | if (mc.to) |
| 5564 | mem_cgroup_clear_mc(); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5565 | } |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5566 | #else /* !CONFIG_MMU */ |
| 5567 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 5568 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5569 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5570 | { |
| 5571 | return 0; |
| 5572 | } |
| 5573 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 5574 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5575 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5576 | { |
| 5577 | } |
| 5578 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 5579 | struct cgroup *cont, |
| 5580 | struct cgroup *old_cont, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5581 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5582 | { |
| 5583 | } |
| 5584 | #endif |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5585 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5586 | struct cgroup_subsys mem_cgroup_subsys = { |
| 5587 | .name = "memory", |
| 5588 | .subsys_id = mem_cgroup_subsys_id, |
| 5589 | .create = mem_cgroup_create, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5590 | .pre_destroy = mem_cgroup_pre_destroy, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5591 | .destroy = mem_cgroup_destroy, |
| 5592 | .populate = mem_cgroup_populate, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5593 | .can_attach = mem_cgroup_can_attach, |
| 5594 | .cancel_attach = mem_cgroup_cancel_attach, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5595 | .attach = mem_cgroup_move_task, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5596 | .early_init = 0, |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5597 | .use_id = 1, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5598 | }; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5599 | |
| 5600 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5601 | static int __init enable_swap_account(char *s) |
| 5602 | { |
| 5603 | /* consider enabled if no parameter or 1 is given */ |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5604 | if (!strcmp(s, "1")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5605 | really_do_swap_account = 1; |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5606 | else if (!strcmp(s, "0")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5607 | really_do_swap_account = 0; |
| 5608 | return 1; |
| 5609 | } |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5610 | __setup("swapaccount=", enable_swap_account); |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5611 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5612 | #endif |