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 | 8c7c6e34 | 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 | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 52 | #include "internal.h" |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 53 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 54 | #include <asm/uaccess.h> |
| 55 | |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 56 | #include <trace/events/vmscan.h> |
| 57 | |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 58 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 59 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 60 | struct mem_cgroup *root_mem_cgroup __read_mostly; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 61 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 62 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 63 | /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */ |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 64 | int do_swap_account __read_mostly; |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 65 | |
| 66 | /* for remember boot option*/ |
| 67 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP_ENABLED |
| 68 | static int really_do_swap_account __initdata = 1; |
| 69 | #else |
| 70 | static int really_do_swap_account __initdata = 0; |
| 71 | #endif |
| 72 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 73 | #else |
| 74 | #define do_swap_account (0) |
| 75 | #endif |
| 76 | |
| 77 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 78 | /* |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 79 | * Statistics for memory cgroup. |
| 80 | */ |
| 81 | enum mem_cgroup_stat_index { |
| 82 | /* |
| 83 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. |
| 84 | */ |
| 85 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 86 | MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */ |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 87 | MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 88 | MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */ |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 89 | MEM_CGROUP_STAT_DATA, /* end of data requires synchronization */ |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 90 | MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 91 | MEM_CGROUP_STAT_NSTATS, |
| 92 | }; |
| 93 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 94 | enum mem_cgroup_events_index { |
| 95 | MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */ |
| 96 | MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */ |
| 97 | MEM_CGROUP_EVENTS_COUNT, /* # of pages paged in/out */ |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 98 | MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */ |
| 99 | MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */ |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 100 | MEM_CGROUP_EVENTS_NSTATS, |
| 101 | }; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 102 | /* |
| 103 | * Per memcg event counter is incremented at every pagein/pageout. With THP, |
| 104 | * it will be incremated by the number of pages. This counter is used for |
| 105 | * for trigger some periodic events. This is straightforward and better |
| 106 | * than using jiffies etc. to handle periodic memcg event. |
| 107 | */ |
| 108 | enum mem_cgroup_events_target { |
| 109 | MEM_CGROUP_TARGET_THRESH, |
| 110 | MEM_CGROUP_TARGET_SOFTLIMIT, |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 111 | MEM_CGROUP_TARGET_NUMAINFO, |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 112 | MEM_CGROUP_NTARGETS, |
| 113 | }; |
| 114 | #define THRESHOLDS_EVENTS_TARGET (128) |
| 115 | #define SOFTLIMIT_EVENTS_TARGET (1024) |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 116 | #define NUMAINFO_EVENTS_TARGET (1024) |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 117 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 118 | struct mem_cgroup_stat_cpu { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 119 | long count[MEM_CGROUP_STAT_NSTATS]; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 120 | unsigned long events[MEM_CGROUP_EVENTS_NSTATS]; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 121 | unsigned long targets[MEM_CGROUP_NTARGETS]; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 122 | }; |
| 123 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 124 | /* |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 125 | * per-zone information in memory controller. |
| 126 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 127 | struct mem_cgroup_per_zone { |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 128 | /* |
| 129 | * spin_lock to protect the per cgroup LRU |
| 130 | */ |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 131 | struct list_head lists[NR_LRU_LISTS]; |
| 132 | unsigned long count[NR_LRU_LISTS]; |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 133 | |
| 134 | struct zone_reclaim_stat reclaim_stat; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 135 | struct rb_node tree_node; /* RB tree node */ |
| 136 | unsigned long long usage_in_excess;/* Set to the value by which */ |
| 137 | /* the soft limit is exceeded*/ |
| 138 | bool on_tree; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 139 | struct mem_cgroup *mem; /* Back pointer, we cannot */ |
| 140 | /* use container_of */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 141 | }; |
| 142 | /* Macro for accessing counter */ |
| 143 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) |
| 144 | |
| 145 | struct mem_cgroup_per_node { |
| 146 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; |
| 147 | }; |
| 148 | |
| 149 | struct mem_cgroup_lru_info { |
| 150 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; |
| 151 | }; |
| 152 | |
| 153 | /* |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 154 | * Cgroups above their limits are maintained in a RB-Tree, independent of |
| 155 | * their hierarchy representation |
| 156 | */ |
| 157 | |
| 158 | struct mem_cgroup_tree_per_zone { |
| 159 | struct rb_root rb_root; |
| 160 | spinlock_t lock; |
| 161 | }; |
| 162 | |
| 163 | struct mem_cgroup_tree_per_node { |
| 164 | struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES]; |
| 165 | }; |
| 166 | |
| 167 | struct mem_cgroup_tree { |
| 168 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; |
| 169 | }; |
| 170 | |
| 171 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; |
| 172 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 173 | struct mem_cgroup_threshold { |
| 174 | struct eventfd_ctx *eventfd; |
| 175 | u64 threshold; |
| 176 | }; |
| 177 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 178 | /* For threshold */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 179 | struct mem_cgroup_threshold_ary { |
| 180 | /* An array index points to threshold just below usage. */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 181 | int current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 182 | /* Size of entries[] */ |
| 183 | unsigned int size; |
| 184 | /* Array of thresholds */ |
| 185 | struct mem_cgroup_threshold entries[0]; |
| 186 | }; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 187 | |
| 188 | struct mem_cgroup_thresholds { |
| 189 | /* Primary thresholds array */ |
| 190 | struct mem_cgroup_threshold_ary *primary; |
| 191 | /* |
| 192 | * Spare threshold array. |
| 193 | * This is needed to make mem_cgroup_unregister_event() "never fail". |
| 194 | * It must be able to store at least primary->size - 1 entries. |
| 195 | */ |
| 196 | struct mem_cgroup_threshold_ary *spare; |
| 197 | }; |
| 198 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 199 | /* for OOM */ |
| 200 | struct mem_cgroup_eventfd_list { |
| 201 | struct list_head list; |
| 202 | struct eventfd_ctx *eventfd; |
| 203 | }; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 204 | |
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 | 8c7c6e34 | 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 | /* |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 230 | * Per cgroup active and inactive list, similar to the |
| 231 | * per zone LRU lists. |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 232 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 233 | struct mem_cgroup_lru_info info; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 234 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 235 | * While reclaiming in a hierarchy, we cache the last child we |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 236 | * reclaimed from. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 237 | */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 238 | int last_scanned_child; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 239 | int last_scanned_node; |
| 240 | #if MAX_NUMNODES > 1 |
| 241 | nodemask_t scan_nodes; |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 242 | atomic_t numainfo_events; |
| 243 | atomic_t numainfo_updating; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 244 | #endif |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 245 | /* |
| 246 | * Should the accounting and control be hierarchical, per subtree? |
| 247 | */ |
| 248 | bool use_hierarchy; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 249 | |
| 250 | bool oom_lock; |
| 251 | atomic_t under_oom; |
| 252 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 253 | atomic_t refcnt; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 254 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 255 | int swappiness; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 256 | /* OOM-Killer disable */ |
| 257 | int oom_kill_disable; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 258 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 259 | /* set when res.limit == memsw.limit */ |
| 260 | bool memsw_is_minimum; |
| 261 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 262 | /* protect arrays of thresholds */ |
| 263 | struct mutex thresholds_lock; |
| 264 | |
| 265 | /* thresholds for memory usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 266 | struct mem_cgroup_thresholds thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 267 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 268 | /* thresholds for mem+swap usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 269 | struct mem_cgroup_thresholds memsw_thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 270 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 271 | /* For oom notifier event fd */ |
| 272 | struct list_head oom_notify; |
Johannes Weiner | 185efc0 | 2011-09-14 16:21:58 -0700 | [diff] [blame] | 273 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 274 | /* |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 275 | * Should we move charges of a task when a task is moved into this |
| 276 | * mem_cgroup ? And what type of charges should we move ? |
| 277 | */ |
| 278 | unsigned long move_charge_at_immigrate; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 279 | /* |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 280 | * percpu counter. |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 281 | */ |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 282 | struct mem_cgroup_stat_cpu *stat; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 283 | /* |
| 284 | * used when a cpu is offlined or other synchronizations |
| 285 | * See mem_cgroup_read_stat(). |
| 286 | */ |
| 287 | struct mem_cgroup_stat_cpu nocpu_base; |
| 288 | spinlock_t pcp_counter_lock; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 289 | }; |
| 290 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 291 | /* Stuffs for move charges at task migration. */ |
| 292 | /* |
| 293 | * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a |
| 294 | * left-shifted bitmap of these types. |
| 295 | */ |
| 296 | enum move_type { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 297 | MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */ |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 298 | MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 299 | NR_MOVE_TYPE, |
| 300 | }; |
| 301 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 302 | /* "mc" and its members are protected by cgroup_mutex */ |
| 303 | static struct move_charge_struct { |
Daisuke Nishimura | b1dd693 | 2010-11-24 12:57:06 -0800 | [diff] [blame] | 304 | spinlock_t lock; /* for from, to */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 305 | struct mem_cgroup *from; |
| 306 | struct mem_cgroup *to; |
| 307 | unsigned long precharge; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 308 | unsigned long moved_charge; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 309 | unsigned long moved_swap; |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 310 | struct task_struct *moving_task; /* a task moving charges */ |
| 311 | wait_queue_head_t waitq; /* a waitq for other context */ |
| 312 | } mc = { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 313 | .lock = __SPIN_LOCK_UNLOCKED(mc.lock), |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 314 | .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq), |
| 315 | }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 316 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 317 | static bool move_anon(void) |
| 318 | { |
| 319 | return test_bit(MOVE_CHARGE_TYPE_ANON, |
| 320 | &mc.to->move_charge_at_immigrate); |
| 321 | } |
| 322 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 323 | static bool move_file(void) |
| 324 | { |
| 325 | return test_bit(MOVE_CHARGE_TYPE_FILE, |
| 326 | &mc.to->move_charge_at_immigrate); |
| 327 | } |
| 328 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 329 | /* |
| 330 | * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft |
| 331 | * limit reclaim to prevent infinite loops, if they ever occur. |
| 332 | */ |
| 333 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100) |
| 334 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2) |
| 335 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 336 | enum charge_type { |
| 337 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
| 338 | MEM_CGROUP_CHARGE_TYPE_MAPPED, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 339 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 340 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 341 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 342 | MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 343 | NR_CHARGE_TYPE, |
| 344 | }; |
| 345 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 346 | /* for encoding cft->private value on file */ |
| 347 | #define _MEM (0) |
| 348 | #define _MEMSWAP (1) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 349 | #define _OOM_TYPE (2) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 350 | #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) |
| 351 | #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff) |
| 352 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 353 | /* Used for OOM nofiier */ |
| 354 | #define OOM_CONTROL (0) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 355 | |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 356 | /* |
| 357 | * Reclaim flags for mem_cgroup_hierarchical_reclaim |
| 358 | */ |
| 359 | #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0 |
| 360 | #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT) |
| 361 | #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1 |
| 362 | #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 363 | #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2 |
| 364 | #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT) |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 365 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 366 | static void mem_cgroup_get(struct mem_cgroup *memcg); |
| 367 | static void mem_cgroup_put(struct mem_cgroup *memcg); |
| 368 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg); |
| 369 | static void drain_all_stock_async(struct mem_cgroup *memcg); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 370 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 371 | static struct mem_cgroup_per_zone * |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 372 | mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 373 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 374 | return &memcg->info.nodeinfo[nid]->zoneinfo[zid]; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 377 | struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg) |
Wu Fengguang | d324236 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 378 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 379 | return &memcg->css; |
Wu Fengguang | d324236 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 382 | static struct mem_cgroup_per_zone * |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 383 | page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 384 | { |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 385 | int nid = page_to_nid(page); |
| 386 | int zid = page_zonenum(page); |
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 mem_cgroup_zoneinfo(memcg, nid, zid); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static struct mem_cgroup_tree_per_zone * |
| 392 | soft_limit_tree_node_zone(int nid, int zid) |
| 393 | { |
| 394 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 395 | } |
| 396 | |
| 397 | static struct mem_cgroup_tree_per_zone * |
| 398 | soft_limit_tree_from_page(struct page *page) |
| 399 | { |
| 400 | int nid = page_to_nid(page); |
| 401 | int zid = page_zonenum(page); |
| 402 | |
| 403 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 404 | } |
| 405 | |
| 406 | static void |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 407 | __mem_cgroup_insert_exceeded(struct mem_cgroup *memcg, |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 408 | struct mem_cgroup_per_zone *mz, |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 409 | struct mem_cgroup_tree_per_zone *mctz, |
| 410 | unsigned long long new_usage_in_excess) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 411 | { |
| 412 | struct rb_node **p = &mctz->rb_root.rb_node; |
| 413 | struct rb_node *parent = NULL; |
| 414 | struct mem_cgroup_per_zone *mz_node; |
| 415 | |
| 416 | if (mz->on_tree) |
| 417 | return; |
| 418 | |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 419 | mz->usage_in_excess = new_usage_in_excess; |
| 420 | if (!mz->usage_in_excess) |
| 421 | return; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 422 | while (*p) { |
| 423 | parent = *p; |
| 424 | mz_node = rb_entry(parent, struct mem_cgroup_per_zone, |
| 425 | tree_node); |
| 426 | if (mz->usage_in_excess < mz_node->usage_in_excess) |
| 427 | p = &(*p)->rb_left; |
| 428 | /* |
| 429 | * We can't avoid mem cgroups that are over their soft |
| 430 | * limit by the same amount |
| 431 | */ |
| 432 | else if (mz->usage_in_excess >= mz_node->usage_in_excess) |
| 433 | p = &(*p)->rb_right; |
| 434 | } |
| 435 | rb_link_node(&mz->tree_node, parent, p); |
| 436 | rb_insert_color(&mz->tree_node, &mctz->rb_root); |
| 437 | mz->on_tree = true; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static void |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 441 | __mem_cgroup_remove_exceeded(struct mem_cgroup *memcg, |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 442 | struct mem_cgroup_per_zone *mz, |
| 443 | struct mem_cgroup_tree_per_zone *mctz) |
| 444 | { |
| 445 | if (!mz->on_tree) |
| 446 | return; |
| 447 | rb_erase(&mz->tree_node, &mctz->rb_root); |
| 448 | mz->on_tree = false; |
| 449 | } |
| 450 | |
| 451 | static void |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 452 | mem_cgroup_remove_exceeded(struct mem_cgroup *memcg, |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 453 | struct mem_cgroup_per_zone *mz, |
| 454 | struct mem_cgroup_tree_per_zone *mctz) |
| 455 | { |
| 456 | spin_lock(&mctz->lock); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 457 | __mem_cgroup_remove_exceeded(memcg, mz, mctz); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 458 | spin_unlock(&mctz->lock); |
| 459 | } |
| 460 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 461 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 462 | 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] | 463 | { |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 464 | unsigned long long excess; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 465 | struct mem_cgroup_per_zone *mz; |
| 466 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 467 | int nid = page_to_nid(page); |
| 468 | int zid = page_zonenum(page); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 469 | mctz = soft_limit_tree_from_page(page); |
| 470 | |
| 471 | /* |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 472 | * Necessary to update all ancestors when hierarchy is used. |
| 473 | * because their event counter is not touched. |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 474 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 475 | for (; memcg; memcg = parent_mem_cgroup(memcg)) { |
| 476 | mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 477 | excess = res_counter_soft_limit_excess(&memcg->res); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 478 | /* |
| 479 | * We have to update the tree if mz is on RB-tree or |
| 480 | * mem is over its softlimit. |
| 481 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 482 | if (excess || mz->on_tree) { |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 483 | spin_lock(&mctz->lock); |
| 484 | /* if on-tree, remove it */ |
| 485 | if (mz->on_tree) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 486 | __mem_cgroup_remove_exceeded(memcg, mz, mctz); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 487 | /* |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 488 | * Insert again. mz->usage_in_excess will be updated. |
| 489 | * If excess is 0, no tree ops. |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 490 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 491 | __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 492 | spin_unlock(&mctz->lock); |
| 493 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 497 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 498 | { |
| 499 | int node, zone; |
| 500 | struct mem_cgroup_per_zone *mz; |
| 501 | struct mem_cgroup_tree_per_zone *mctz; |
| 502 | |
| 503 | for_each_node_state(node, N_POSSIBLE) { |
| 504 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 505 | mz = mem_cgroup_zoneinfo(memcg, node, zone); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 506 | mctz = soft_limit_tree_node_zone(node, zone); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 507 | mem_cgroup_remove_exceeded(memcg, mz, mctz); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 512 | static struct mem_cgroup_per_zone * |
| 513 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 514 | { |
| 515 | struct rb_node *rightmost = NULL; |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 516 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 517 | |
| 518 | retry: |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 519 | mz = NULL; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 520 | rightmost = rb_last(&mctz->rb_root); |
| 521 | if (!rightmost) |
| 522 | goto done; /* Nothing to reclaim from */ |
| 523 | |
| 524 | mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node); |
| 525 | /* |
| 526 | * Remove the node now but someone else can add it back, |
| 527 | * we will to add it back at the end of reclaim to its correct |
| 528 | * position in the tree. |
| 529 | */ |
| 530 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
| 531 | if (!res_counter_soft_limit_excess(&mz->mem->res) || |
| 532 | !css_tryget(&mz->mem->css)) |
| 533 | goto retry; |
| 534 | done: |
| 535 | return mz; |
| 536 | } |
| 537 | |
| 538 | static struct mem_cgroup_per_zone * |
| 539 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 540 | { |
| 541 | struct mem_cgroup_per_zone *mz; |
| 542 | |
| 543 | spin_lock(&mctz->lock); |
| 544 | mz = __mem_cgroup_largest_soft_limit_node(mctz); |
| 545 | spin_unlock(&mctz->lock); |
| 546 | return mz; |
| 547 | } |
| 548 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 549 | /* |
| 550 | * Implementation Note: reading percpu statistics for memcg. |
| 551 | * |
| 552 | * Both of vmstat[] and percpu_counter has threshold and do periodic |
| 553 | * synchronization to implement "quick" read. There are trade-off between |
| 554 | * reading cost and precision of value. Then, we may have a chance to implement |
| 555 | * a periodic synchronizion of counter in memcg's counter. |
| 556 | * |
| 557 | * But this _read() function is used for user interface now. The user accounts |
| 558 | * memory usage by memory cgroup and he _always_ requires exact value because |
| 559 | * he accounts memory. Even if we provide quick-and-fuzzy read, we always |
| 560 | * have to visit all online cpus and make sum. So, for now, unnecessary |
| 561 | * synchronization is not implemented. (just implemented for cpu hotplug) |
| 562 | * |
| 563 | * If there are kernel internal actions which can make use of some not-exact |
| 564 | * value, and reading all cpu value can be performance bottleneck in some |
| 565 | * common workload, threashold and synchonization as vmstat[] should be |
| 566 | * implemented. |
| 567 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 568 | static long mem_cgroup_read_stat(struct mem_cgroup *memcg, |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 569 | enum mem_cgroup_stat_index idx) |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 570 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 571 | long val = 0; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 572 | int cpu; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 573 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 574 | get_online_cpus(); |
| 575 | for_each_online_cpu(cpu) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 576 | val += per_cpu(memcg->stat->count[idx], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 577 | #ifdef CONFIG_HOTPLUG_CPU |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 578 | spin_lock(&memcg->pcp_counter_lock); |
| 579 | val += memcg->nocpu_base.count[idx]; |
| 580 | spin_unlock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 581 | #endif |
| 582 | put_online_cpus(); |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 583 | return val; |
| 584 | } |
| 585 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 586 | static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg, |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 587 | bool charge) |
| 588 | { |
| 589 | int val = (charge) ? 1 : -1; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 590 | this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAPOUT], val); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 593 | void mem_cgroup_pgfault(struct mem_cgroup *memcg, int val) |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 594 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 595 | this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 598 | void mem_cgroup_pgmajfault(struct mem_cgroup *memcg, int val) |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 599 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 600 | this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 603 | static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg, |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 604 | enum mem_cgroup_events_index idx) |
| 605 | { |
| 606 | unsigned long val = 0; |
| 607 | int cpu; |
| 608 | |
| 609 | for_each_online_cpu(cpu) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 610 | val += per_cpu(memcg->stat->events[idx], cpu); |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 611 | #ifdef CONFIG_HOTPLUG_CPU |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 612 | spin_lock(&memcg->pcp_counter_lock); |
| 613 | val += memcg->nocpu_base.events[idx]; |
| 614 | spin_unlock(&memcg->pcp_counter_lock); |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 615 | #endif |
| 616 | return val; |
| 617 | } |
| 618 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 619 | static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 620 | bool file, int nr_pages) |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 621 | { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 622 | preempt_disable(); |
| 623 | |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 624 | if (file) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 625 | __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE], |
| 626 | nr_pages); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 627 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 628 | __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS], |
| 629 | nr_pages); |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 630 | |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 631 | /* pagein of a big page is an event. So, ignore page size */ |
| 632 | if (nr_pages > 0) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 633 | __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 634 | else { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 635 | __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 636 | nr_pages = -nr_pages; /* for event */ |
| 637 | } |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 638 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 639 | __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] | 640 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 641 | preempt_enable(); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 642 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 643 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 644 | unsigned long |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 645 | 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] | 646 | unsigned int lru_mask) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 647 | { |
| 648 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 649 | enum lru_list l; |
| 650 | unsigned long ret = 0; |
| 651 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 652 | mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 653 | |
| 654 | for_each_lru(l) { |
| 655 | if (BIT(l) & lru_mask) |
| 656 | ret += MEM_CGROUP_ZSTAT(mz, l); |
| 657 | } |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | static unsigned long |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 662 | mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 663 | int nid, unsigned int lru_mask) |
| 664 | { |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 665 | u64 total = 0; |
| 666 | int zid; |
| 667 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 668 | for (zid = 0; zid < MAX_NR_ZONES; zid++) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 669 | total += mem_cgroup_zone_nr_lru_pages(memcg, |
| 670 | nid, zid, lru_mask); |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 671 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 672 | return total; |
| 673 | } |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 674 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 675 | static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 676 | unsigned int lru_mask) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 677 | { |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 678 | int nid; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 679 | u64 total = 0; |
| 680 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 681 | for_each_node_state(nid, N_HIGH_MEMORY) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 682 | total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 683 | return total; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 686 | static bool __memcg_event_check(struct mem_cgroup *memcg, int target) |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 687 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 688 | unsigned long val, next; |
| 689 | |
Steven Rostedt | 4799401 | 2011-11-02 13:38:33 -0700 | [diff] [blame] | 690 | val = __this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]); |
| 691 | next = __this_cpu_read(memcg->stat->targets[target]); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 692 | /* from time_after() in jiffies.h */ |
| 693 | return ((long)next - (long)val < 0); |
| 694 | } |
| 695 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 696 | static void __mem_cgroup_target_update(struct mem_cgroup *memcg, int target) |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 697 | { |
| 698 | unsigned long val, next; |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 699 | |
Steven Rostedt | 4799401 | 2011-11-02 13:38:33 -0700 | [diff] [blame] | 700 | val = __this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]); |
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 | switch (target) { |
| 703 | case MEM_CGROUP_TARGET_THRESH: |
| 704 | next = val + THRESHOLDS_EVENTS_TARGET; |
| 705 | break; |
| 706 | case MEM_CGROUP_TARGET_SOFTLIMIT: |
| 707 | next = val + SOFTLIMIT_EVENTS_TARGET; |
| 708 | break; |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 709 | case MEM_CGROUP_TARGET_NUMAINFO: |
| 710 | next = val + NUMAINFO_EVENTS_TARGET; |
| 711 | break; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 712 | default: |
| 713 | return; |
| 714 | } |
| 715 | |
Steven Rostedt | 4799401 | 2011-11-02 13:38:33 -0700 | [diff] [blame] | 716 | __this_cpu_write(memcg->stat->targets[target], next); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Check events in order. |
| 721 | * |
| 722 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 723 | static void memcg_check_events(struct mem_cgroup *memcg, struct page *page) |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 724 | { |
Steven Rostedt | 4799401 | 2011-11-02 13:38:33 -0700 | [diff] [blame] | 725 | preempt_disable(); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 726 | /* threshold event is triggered in finer grain than soft limit */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 727 | if (unlikely(__memcg_event_check(memcg, MEM_CGROUP_TARGET_THRESH))) { |
| 728 | mem_cgroup_threshold(memcg); |
| 729 | __mem_cgroup_target_update(memcg, MEM_CGROUP_TARGET_THRESH); |
| 730 | if (unlikely(__memcg_event_check(memcg, |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 731 | MEM_CGROUP_TARGET_SOFTLIMIT))) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 732 | mem_cgroup_update_tree(memcg, page); |
| 733 | __mem_cgroup_target_update(memcg, |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 734 | MEM_CGROUP_TARGET_SOFTLIMIT); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 735 | } |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 736 | #if MAX_NUMNODES > 1 |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 737 | if (unlikely(__memcg_event_check(memcg, |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 738 | MEM_CGROUP_TARGET_NUMAINFO))) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 739 | atomic_inc(&memcg->numainfo_events); |
| 740 | __mem_cgroup_target_update(memcg, |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 741 | MEM_CGROUP_TARGET_NUMAINFO); |
| 742 | } |
| 743 | #endif |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 744 | } |
Steven Rostedt | 4799401 | 2011-11-02 13:38:33 -0700 | [diff] [blame] | 745 | preempt_enable(); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 746 | } |
| 747 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 748 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 749 | { |
| 750 | return container_of(cgroup_subsys_state(cont, |
| 751 | mem_cgroup_subsys_id), struct mem_cgroup, |
| 752 | css); |
| 753 | } |
| 754 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 755 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 756 | { |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 757 | /* |
| 758 | * mm_update_next_owner() may clear mm->owner to NULL |
| 759 | * if it races with swapoff, page migration, etc. |
| 760 | * So this can be called with p == NULL. |
| 761 | */ |
| 762 | if (unlikely(!p)) |
| 763 | return NULL; |
| 764 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 765 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
| 766 | struct mem_cgroup, css); |
| 767 | } |
| 768 | |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 769 | 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] | 770 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 771 | struct mem_cgroup *memcg = NULL; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 772 | |
| 773 | if (!mm) |
| 774 | return NULL; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 775 | /* |
| 776 | * Because we have no locks, mm->owner's may be being moved to other |
| 777 | * cgroup. We use css_tryget() here even if this looks |
| 778 | * pessimistic (rather than adding locks here). |
| 779 | */ |
| 780 | rcu_read_lock(); |
| 781 | do { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 782 | memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 783 | if (unlikely(!memcg)) |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 784 | break; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 785 | } while (!css_tryget(&memcg->css)); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 786 | rcu_read_unlock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 787 | return memcg; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 788 | } |
| 789 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 790 | /* The caller has to guarantee "mem" exists before calling this */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 791 | static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 792 | { |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 793 | struct cgroup_subsys_state *css; |
| 794 | int found; |
| 795 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 796 | if (!memcg) /* ROOT cgroup has the smallest ID */ |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 797 | return root_mem_cgroup; /*css_put/get against root is ignored*/ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 798 | if (!memcg->use_hierarchy) { |
| 799 | if (css_tryget(&memcg->css)) |
| 800 | return memcg; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 801 | return NULL; |
| 802 | } |
| 803 | rcu_read_lock(); |
| 804 | /* |
| 805 | * searching a memory cgroup which has the smallest ID under given |
| 806 | * ROOT cgroup. (ID >= 1) |
| 807 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 808 | css = css_get_next(&mem_cgroup_subsys, 1, &memcg->css, &found); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 809 | if (css && css_tryget(css)) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 810 | memcg = container_of(css, struct mem_cgroup, css); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 811 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 812 | memcg = NULL; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 813 | rcu_read_unlock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 814 | return memcg; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 815 | } |
| 816 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 817 | static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter, |
| 818 | struct mem_cgroup *root, |
| 819 | bool cond) |
| 820 | { |
| 821 | int nextid = css_id(&iter->css) + 1; |
| 822 | int found; |
| 823 | int hierarchy_used; |
| 824 | struct cgroup_subsys_state *css; |
| 825 | |
| 826 | hierarchy_used = iter->use_hierarchy; |
| 827 | |
| 828 | css_put(&iter->css); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 829 | /* If no ROOT, walk all, ignore hierarchy */ |
| 830 | if (!cond || (root && !hierarchy_used)) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 831 | return NULL; |
| 832 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 833 | if (!root) |
| 834 | root = root_mem_cgroup; |
| 835 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 836 | do { |
| 837 | iter = NULL; |
| 838 | rcu_read_lock(); |
| 839 | |
| 840 | css = css_get_next(&mem_cgroup_subsys, nextid, |
| 841 | &root->css, &found); |
| 842 | if (css && css_tryget(css)) |
| 843 | iter = container_of(css, struct mem_cgroup, css); |
| 844 | rcu_read_unlock(); |
| 845 | /* If css is NULL, no more cgroups will be found */ |
| 846 | nextid = found + 1; |
| 847 | } while (css && !iter); |
| 848 | |
| 849 | return iter; |
| 850 | } |
| 851 | /* |
| 852 | * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please |
| 853 | * be careful that "break" loop is not allowed. We have reference count. |
| 854 | * Instead of that modify "cond" to be false and "continue" to exit the loop. |
| 855 | */ |
| 856 | #define for_each_mem_cgroup_tree_cond(iter, root, cond) \ |
| 857 | for (iter = mem_cgroup_start_loop(root);\ |
| 858 | iter != NULL;\ |
| 859 | iter = mem_cgroup_get_next(iter, root, cond)) |
| 860 | |
| 861 | #define for_each_mem_cgroup_tree(iter, root) \ |
| 862 | for_each_mem_cgroup_tree_cond(iter, root, true) |
| 863 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 864 | #define for_each_mem_cgroup_all(iter) \ |
| 865 | for_each_mem_cgroup_tree_cond(iter, NULL, true) |
| 866 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 867 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 868 | static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg) |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 869 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 870 | return (memcg == root_mem_cgroup); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 873 | void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx) |
| 874 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 875 | struct mem_cgroup *memcg; |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 876 | |
| 877 | if (!mm) |
| 878 | return; |
| 879 | |
| 880 | rcu_read_lock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 881 | memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 882 | if (unlikely(!memcg)) |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 883 | goto out; |
| 884 | |
| 885 | switch (idx) { |
| 886 | case PGMAJFAULT: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 887 | mem_cgroup_pgmajfault(memcg, 1); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 888 | break; |
| 889 | case PGFAULT: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 890 | mem_cgroup_pgfault(memcg, 1); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 891 | break; |
| 892 | default: |
| 893 | BUG(); |
| 894 | } |
| 895 | out: |
| 896 | rcu_read_unlock(); |
| 897 | } |
| 898 | EXPORT_SYMBOL(mem_cgroup_count_vm_event); |
| 899 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 900 | /* |
| 901 | * Following LRU functions are allowed to be used without PCG_LOCK. |
| 902 | * Operations are called by routine of global LRU independently from memcg. |
| 903 | * What we have to take care of here is validness of pc->mem_cgroup. |
| 904 | * |
| 905 | * Changes to pc->mem_cgroup happens when |
| 906 | * 1. charge |
| 907 | * 2. moving account |
| 908 | * In typical case, "charge" is done before add-to-lru. Exception is SwapCache. |
| 909 | * It is added to LRU before charge. |
| 910 | * If PCG_USED bit is not set, page_cgroup is not added to this private LRU. |
| 911 | * When moving account, the page is not on LRU. It's isolated. |
| 912 | */ |
| 913 | |
| 914 | 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] | 915 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 916 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 917 | struct mem_cgroup_per_zone *mz; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 918 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 919 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 920 | return; |
| 921 | pc = lookup_page_cgroup(page); |
| 922 | /* can happen while we handle swapcache. */ |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 923 | if (!TestClearPageCgroupAcctLRU(pc)) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 924 | return; |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 925 | VM_BUG_ON(!pc->mem_cgroup); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 926 | /* |
| 927 | * We don't check PCG_USED bit. It's cleared when the "page" is finally |
| 928 | * removed from global LRU. |
| 929 | */ |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 930 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 931 | /* huge page split is done under lru_lock. so, we have no races. */ |
| 932 | MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 933 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 934 | return; |
| 935 | VM_BUG_ON(list_empty(&pc->lru)); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 936 | list_del_init(&pc->lru); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 937 | } |
| 938 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 939 | void mem_cgroup_del_lru(struct page *page) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 940 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 941 | mem_cgroup_del_lru_list(page, page_lru(page)); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 942 | } |
| 943 | |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 944 | /* |
| 945 | * Writeback is about to end against a page which has been marked for immediate |
| 946 | * reclaim. If it still appears to be reclaimable, move it to the tail of the |
| 947 | * inactive list. |
| 948 | */ |
| 949 | void mem_cgroup_rotate_reclaimable_page(struct page *page) |
| 950 | { |
| 951 | struct mem_cgroup_per_zone *mz; |
| 952 | struct page_cgroup *pc; |
| 953 | enum lru_list lru = page_lru(page); |
| 954 | |
| 955 | if (mem_cgroup_disabled()) |
| 956 | return; |
| 957 | |
| 958 | pc = lookup_page_cgroup(page); |
| 959 | /* unused or root page is not rotated. */ |
| 960 | if (!PageCgroupUsed(pc)) |
| 961 | return; |
| 962 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 963 | smp_rmb(); |
| 964 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 965 | return; |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 966 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 967 | list_move_tail(&pc->lru, &mz->lists[lru]); |
| 968 | } |
| 969 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 970 | 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] | 971 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 972 | struct mem_cgroup_per_zone *mz; |
| 973 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 974 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 975 | if (mem_cgroup_disabled()) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 976 | return; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 977 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 978 | pc = lookup_page_cgroup(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 979 | /* unused or root page is not rotated. */ |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 980 | if (!PageCgroupUsed(pc)) |
| 981 | return; |
| 982 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 983 | smp_rmb(); |
| 984 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 985 | return; |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 986 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 987 | list_move(&pc->lru, &mz->lists[lru]); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 988 | } |
| 989 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 990 | void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru) |
| 991 | { |
| 992 | struct page_cgroup *pc; |
| 993 | struct mem_cgroup_per_zone *mz; |
| 994 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 995 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 996 | return; |
| 997 | pc = lookup_page_cgroup(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 998 | VM_BUG_ON(PageCgroupAcctLRU(pc)); |
Johannes Weiner | a61ed3c | 2011-11-02 13:38:29 -0700 | [diff] [blame] | 999 | /* |
| 1000 | * putback: charge: |
| 1001 | * SetPageLRU SetPageCgroupUsed |
| 1002 | * smp_mb smp_mb |
| 1003 | * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU |
| 1004 | * |
| 1005 | * Ensure that one of the two sides adds the page to the memcg |
| 1006 | * LRU during a race. |
| 1007 | */ |
| 1008 | smp_mb(); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1009 | if (!PageCgroupUsed(pc)) |
| 1010 | return; |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 1011 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 1012 | smp_rmb(); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1013 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 1014 | /* huge page split is done under lru_lock. so, we have no races. */ |
| 1015 | MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 1016 | SetPageCgroupAcctLRU(pc); |
| 1017 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 1018 | return; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1019 | list_add(&pc->lru, &mz->lists[lru]); |
| 1020 | } |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1021 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1022 | /* |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1023 | * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed |
| 1024 | * while it's linked to lru because the page may be reused after it's fully |
| 1025 | * uncharged. To handle that, unlink page_cgroup from LRU when charge it again. |
| 1026 | * It's done under lock_page and expected that zone->lru_lock isnever held. |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1027 | */ |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1028 | static void mem_cgroup_lru_del_before_commit(struct page *page) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1029 | { |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1030 | unsigned long flags; |
| 1031 | struct zone *zone = page_zone(page); |
| 1032 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 1033 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1034 | /* |
| 1035 | * Doing this check without taking ->lru_lock seems wrong but this |
| 1036 | * is safe. Because if page_cgroup's USED bit is unset, the page |
| 1037 | * will not be added to any memcg's LRU. If page_cgroup's USED bit is |
| 1038 | * set, the commit after this will fail, anyway. |
| 1039 | * This all charge/uncharge is done under some mutual execustion. |
| 1040 | * So, we don't need to taking care of changes in USED bit. |
| 1041 | */ |
| 1042 | if (likely(!PageLRU(page))) |
| 1043 | return; |
| 1044 | |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1045 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 1046 | /* |
| 1047 | * Forget old LRU when this page_cgroup is *not* used. This Used bit |
| 1048 | * is guarded by lock_page() because the page is SwapCache. |
| 1049 | */ |
| 1050 | if (!PageCgroupUsed(pc)) |
| 1051 | mem_cgroup_del_lru_list(page, page_lru(page)); |
| 1052 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1055 | static void mem_cgroup_lru_add_after_commit(struct page *page) |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1056 | { |
| 1057 | unsigned long flags; |
| 1058 | struct zone *zone = page_zone(page); |
| 1059 | struct page_cgroup *pc = lookup_page_cgroup(page); |
Johannes Weiner | a61ed3c | 2011-11-02 13:38:29 -0700 | [diff] [blame] | 1060 | /* |
| 1061 | * putback: charge: |
| 1062 | * SetPageLRU SetPageCgroupUsed |
| 1063 | * smp_mb smp_mb |
| 1064 | * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU |
| 1065 | * |
| 1066 | * Ensure that one of the two sides adds the page to the memcg |
| 1067 | * LRU during a race. |
| 1068 | */ |
| 1069 | smp_mb(); |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1070 | /* taking care of that the page is added to LRU while we commit it */ |
| 1071 | if (likely(!PageLRU(page))) |
| 1072 | return; |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1073 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 1074 | /* 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] | 1075 | if (PageLRU(page) && !PageCgroupAcctLRU(pc)) |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1076 | mem_cgroup_add_lru_list(page, page_lru(page)); |
| 1077 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
| 1078 | } |
| 1079 | |
| 1080 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1081 | void mem_cgroup_move_lists(struct page *page, |
| 1082 | enum lru_list from, enum lru_list to) |
| 1083 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1084 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1085 | return; |
| 1086 | mem_cgroup_del_lru_list(page, from); |
| 1087 | mem_cgroup_add_lru_list(page, to); |
| 1088 | } |
| 1089 | |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1090 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1091 | * 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] | 1092 | * hierarchy subtree |
| 1093 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1094 | static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg, |
| 1095 | struct mem_cgroup *memcg) |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1096 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1097 | if (root_memcg != memcg) { |
| 1098 | return (root_memcg->use_hierarchy && |
| 1099 | css_is_ancestor(&memcg->css, &root_memcg->css)); |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | return true; |
| 1103 | } |
| 1104 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1105 | 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] | 1106 | { |
| 1107 | int ret; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1108 | struct mem_cgroup *curr = NULL; |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1109 | struct task_struct *p; |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1110 | |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1111 | p = find_lock_task_mm(task); |
| 1112 | if (!p) |
| 1113 | return 0; |
| 1114 | curr = try_get_mem_cgroup_from_mm(p->mm); |
| 1115 | task_unlock(p); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1116 | if (!curr) |
| 1117 | return 0; |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1118 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1119 | * We should check use_hierarchy of "memcg" not "curr". Because checking |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1120 | * 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] | 1121 | * enabled in "curr" and "curr" is a child of "memcg" in *cgroup* |
| 1122 | * hierarchy(even if use_hierarchy is disabled in "memcg"). |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1123 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1124 | ret = mem_cgroup_same_or_subtree(memcg, curr); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1125 | css_put(&curr->css); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1126 | return ret; |
| 1127 | } |
| 1128 | |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1129 | 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] | 1130 | { |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1131 | unsigned long inactive_ratio; |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1132 | int nid = zone_to_nid(zone); |
| 1133 | int zid = zone_idx(zone); |
| 1134 | unsigned long inactive; |
| 1135 | unsigned long active; |
| 1136 | unsigned long gb; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1137 | |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1138 | inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid, |
| 1139 | BIT(LRU_INACTIVE_ANON)); |
| 1140 | active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid, |
| 1141 | BIT(LRU_ACTIVE_ANON)); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1142 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1143 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
| 1144 | if (gb) |
| 1145 | inactive_ratio = int_sqrt(10 * gb); |
| 1146 | else |
| 1147 | inactive_ratio = 1; |
| 1148 | |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1149 | return inactive * inactive_ratio < active; |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1152 | 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] | 1153 | { |
| 1154 | unsigned long active; |
| 1155 | unsigned long inactive; |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1156 | int zid = zone_idx(zone); |
| 1157 | int nid = zone_to_nid(zone); |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1158 | |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1159 | inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid, |
| 1160 | BIT(LRU_INACTIVE_FILE)); |
| 1161 | active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid, |
| 1162 | BIT(LRU_ACTIVE_FILE)); |
Rik van Riel | 56e49d2 | 2009-06-16 15:32:28 -0700 | [diff] [blame] | 1163 | |
| 1164 | return (active > inactive); |
| 1165 | } |
| 1166 | |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1167 | struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg, |
| 1168 | struct zone *zone) |
| 1169 | { |
KOSAKI Motohiro | 13d7e3a | 2010-08-10 18:03:06 -0700 | [diff] [blame] | 1170 | int nid = zone_to_nid(zone); |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1171 | int zid = zone_idx(zone); |
| 1172 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 1173 | |
| 1174 | return &mz->reclaim_stat; |
| 1175 | } |
| 1176 | |
| 1177 | struct zone_reclaim_stat * |
| 1178 | mem_cgroup_get_reclaim_stat_from_page(struct page *page) |
| 1179 | { |
| 1180 | struct page_cgroup *pc; |
| 1181 | struct mem_cgroup_per_zone *mz; |
| 1182 | |
| 1183 | if (mem_cgroup_disabled()) |
| 1184 | return NULL; |
| 1185 | |
| 1186 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 1187 | if (!PageCgroupUsed(pc)) |
| 1188 | return NULL; |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 1189 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 1190 | smp_rmb(); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1191 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1192 | return &mz->reclaim_stat; |
| 1193 | } |
| 1194 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1195 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 1196 | struct list_head *dst, |
| 1197 | unsigned long *scanned, int order, |
Minchan Kim | 4356f21 | 2011-10-31 17:06:47 -0700 | [diff] [blame] | 1198 | isolate_mode_t mode, |
| 1199 | struct zone *z, |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1200 | struct mem_cgroup *mem_cont, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1201 | int active, int file) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1202 | { |
| 1203 | unsigned long nr_taken = 0; |
| 1204 | struct page *page; |
| 1205 | unsigned long scan; |
| 1206 | LIST_HEAD(pc_list); |
| 1207 | struct list_head *src; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1208 | struct page_cgroup *pc, *tmp; |
KOSAKI Motohiro | 13d7e3a | 2010-08-10 18:03:06 -0700 | [diff] [blame] | 1209 | int nid = zone_to_nid(z); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1210 | int zid = zone_idx(z); |
| 1211 | struct mem_cgroup_per_zone *mz; |
Johannes Weiner | b7c46d1 | 2009-09-21 17:02:56 -0700 | [diff] [blame] | 1212 | int lru = LRU_FILE * file + active; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1213 | int ret; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1214 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 1215 | BUG_ON(!mem_cont); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1216 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1217 | src = &mz->lists[lru]; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1218 | |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1219 | scan = 0; |
| 1220 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1221 | if (scan >= nr_to_scan) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1222 | break; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1223 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1224 | if (unlikely(!PageCgroupUsed(pc))) |
| 1225 | continue; |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 1226 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 1227 | page = lookup_cgroup_page(pc); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 1228 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1229 | if (unlikely(!PageLRU(page))) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1230 | continue; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1231 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1232 | scan++; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1233 | ret = __isolate_lru_page(page, mode, file); |
| 1234 | switch (ret) { |
| 1235 | case 0: |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1236 | list_move(&page->lru, dst); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1237 | mem_cgroup_del_lru(page); |
Rik van Riel | 2c888cf | 2011-01-13 15:47:13 -0800 | [diff] [blame] | 1238 | nr_taken += hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1239 | break; |
| 1240 | case -EBUSY: |
| 1241 | /* we don't affect global LRU but rotate in our LRU */ |
| 1242 | mem_cgroup_rotate_lru_list(page, page_lru(page)); |
| 1243 | break; |
| 1244 | default: |
| 1245 | break; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1249 | *scanned = scan; |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 1250 | |
| 1251 | trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken, |
| 1252 | 0, 0, 0, mode); |
| 1253 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1254 | return nr_taken; |
| 1255 | } |
| 1256 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1257 | #define mem_cgroup_from_res_counter(counter, member) \ |
| 1258 | container_of(counter, struct mem_cgroup, member) |
| 1259 | |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1260 | /** |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1261 | * mem_cgroup_margin - calculate chargeable space of a memory cgroup |
| 1262 | * @mem: the memory cgroup |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1263 | * |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1264 | * Returns the maximum amount of memory @mem can be charged with, in |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1265 | * pages. |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1266 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1267 | static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1268 | { |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1269 | unsigned long long margin; |
| 1270 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1271 | margin = res_counter_margin(&memcg->res); |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1272 | if (do_swap_account) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1273 | margin = min(margin, res_counter_margin(&memcg->memsw)); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1274 | return margin >> PAGE_SHIFT; |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1275 | } |
| 1276 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 1277 | int mem_cgroup_swappiness(struct mem_cgroup *memcg) |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1278 | { |
| 1279 | struct cgroup *cgrp = memcg->css.cgroup; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1280 | |
| 1281 | /* root ? */ |
| 1282 | if (cgrp->parent == NULL) |
| 1283 | return vm_swappiness; |
| 1284 | |
Johannes Weiner | bf1ff26 | 2011-03-23 16:42:32 -0700 | [diff] [blame] | 1285 | return memcg->swappiness; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1286 | } |
| 1287 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1288 | static void mem_cgroup_start_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1289 | { |
| 1290 | int cpu; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1291 | |
| 1292 | get_online_cpus(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1293 | spin_lock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1294 | for_each_online_cpu(cpu) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1295 | per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1; |
| 1296 | memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1; |
| 1297 | spin_unlock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1298 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1299 | |
| 1300 | synchronize_rcu(); |
| 1301 | } |
| 1302 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1303 | static void mem_cgroup_end_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1304 | { |
| 1305 | int cpu; |
| 1306 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1307 | if (!memcg) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1308 | return; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1309 | get_online_cpus(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1310 | spin_lock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1311 | for_each_online_cpu(cpu) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1312 | per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1; |
| 1313 | memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1; |
| 1314 | spin_unlock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1315 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1316 | } |
| 1317 | /* |
| 1318 | * 2 routines for checking "mem" is under move_account() or not. |
| 1319 | * |
| 1320 | * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used |
| 1321 | * for avoiding race in accounting. If true, |
| 1322 | * pc->mem_cgroup may be overwritten. |
| 1323 | * |
| 1324 | * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or |
| 1325 | * under hierarchy of moving cgroups. This is for |
| 1326 | * waiting at hith-memory prressure caused by "move". |
| 1327 | */ |
| 1328 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1329 | static bool mem_cgroup_stealed(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1330 | { |
| 1331 | VM_BUG_ON(!rcu_read_lock_held()); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1332 | return this_cpu_read(memcg->stat->count[MEM_CGROUP_ON_MOVE]) > 0; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1333 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1334 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1335 | static bool mem_cgroup_under_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1336 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1337 | struct mem_cgroup *from; |
| 1338 | struct mem_cgroup *to; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1339 | bool ret = false; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1340 | /* |
| 1341 | * Unlike task_move routines, we access mc.to, mc.from not under |
| 1342 | * mutual exclusion by cgroup_mutex. Here, we take spinlock instead. |
| 1343 | */ |
| 1344 | spin_lock(&mc.lock); |
| 1345 | from = mc.from; |
| 1346 | to = mc.to; |
| 1347 | if (!from) |
| 1348 | goto unlock; |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1349 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1350 | ret = mem_cgroup_same_or_subtree(memcg, from) |
| 1351 | || mem_cgroup_same_or_subtree(memcg, to); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1352 | unlock: |
| 1353 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1354 | return ret; |
| 1355 | } |
| 1356 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1357 | static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1358 | { |
| 1359 | if (mc.moving_task && current != mc.moving_task) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1360 | if (mem_cgroup_under_move(memcg)) { |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1361 | DEFINE_WAIT(wait); |
| 1362 | prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE); |
| 1363 | /* moving charge context might have finished. */ |
| 1364 | if (mc.moving_task) |
| 1365 | schedule(); |
| 1366 | finish_wait(&mc.waitq, &wait); |
| 1367 | return true; |
| 1368 | } |
| 1369 | } |
| 1370 | return false; |
| 1371 | } |
| 1372 | |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1373 | /** |
Kirill A. Shutemov | 6a6135b | 2010-03-10 15:22:25 -0800 | [diff] [blame] | 1374 | * 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] | 1375 | * @memcg: The memory cgroup that went over limit |
| 1376 | * @p: Task that is going to be killed |
| 1377 | * |
| 1378 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is |
| 1379 | * enabled |
| 1380 | */ |
| 1381 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) |
| 1382 | { |
| 1383 | struct cgroup *task_cgrp; |
| 1384 | struct cgroup *mem_cgrp; |
| 1385 | /* |
| 1386 | * Need a buffer in BSS, can't rely on allocations. The code relies |
| 1387 | * on the assumption that OOM is serialized for memory controller. |
| 1388 | * If this assumption is broken, revisit this code. |
| 1389 | */ |
| 1390 | static char memcg_name[PATH_MAX]; |
| 1391 | int ret; |
| 1392 | |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1393 | if (!memcg || !p) |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1394 | return; |
| 1395 | |
| 1396 | |
| 1397 | rcu_read_lock(); |
| 1398 | |
| 1399 | mem_cgrp = memcg->css.cgroup; |
| 1400 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); |
| 1401 | |
| 1402 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); |
| 1403 | if (ret < 0) { |
| 1404 | /* |
| 1405 | * Unfortunately, we are unable to convert to a useful name |
| 1406 | * But we'll still print out the usage information |
| 1407 | */ |
| 1408 | rcu_read_unlock(); |
| 1409 | goto done; |
| 1410 | } |
| 1411 | rcu_read_unlock(); |
| 1412 | |
| 1413 | printk(KERN_INFO "Task in %s killed", memcg_name); |
| 1414 | |
| 1415 | rcu_read_lock(); |
| 1416 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); |
| 1417 | if (ret < 0) { |
| 1418 | rcu_read_unlock(); |
| 1419 | goto done; |
| 1420 | } |
| 1421 | rcu_read_unlock(); |
| 1422 | |
| 1423 | /* |
| 1424 | * Continues from above, so we don't need an KERN_ level |
| 1425 | */ |
| 1426 | printk(KERN_CONT " as a result of limit of %s\n", memcg_name); |
| 1427 | done: |
| 1428 | |
| 1429 | printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n", |
| 1430 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, |
| 1431 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, |
| 1432 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); |
| 1433 | printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, " |
| 1434 | "failcnt %llu\n", |
| 1435 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, |
| 1436 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, |
| 1437 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); |
| 1438 | } |
| 1439 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1440 | /* |
| 1441 | * This function returns the number of memcg under hierarchy tree. Returns |
| 1442 | * 1(self count) if no children. |
| 1443 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1444 | static int mem_cgroup_count_children(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1445 | { |
| 1446 | int num = 0; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1447 | struct mem_cgroup *iter; |
| 1448 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1449 | for_each_mem_cgroup_tree(iter, memcg) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1450 | num++; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1451 | return num; |
| 1452 | } |
| 1453 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1454 | /* |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1455 | * Return the memory (and swap, if configured) limit for a memcg. |
| 1456 | */ |
| 1457 | u64 mem_cgroup_get_limit(struct mem_cgroup *memcg) |
| 1458 | { |
| 1459 | u64 limit; |
| 1460 | u64 memsw; |
| 1461 | |
Johannes Weiner | f3e8eb7 | 2011-01-13 15:47:39 -0800 | [diff] [blame] | 1462 | limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 1463 | limit += total_swap_pages << PAGE_SHIFT; |
| 1464 | |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1465 | memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 1466 | /* |
| 1467 | * If memsw is finite and limits the amount of swap space available |
| 1468 | * to this memcg, return that limit. |
| 1469 | */ |
| 1470 | return min(limit, memsw); |
| 1471 | } |
| 1472 | |
| 1473 | /* |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1474 | * Visit the first child (need not be the first child as per the ordering |
| 1475 | * of the cgroup list, since we track last_scanned_child) of @mem and use |
| 1476 | * that to reclaim free pages from. |
| 1477 | */ |
| 1478 | static struct mem_cgroup * |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1479 | mem_cgroup_select_victim(struct mem_cgroup *root_memcg) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1480 | { |
| 1481 | struct mem_cgroup *ret = NULL; |
| 1482 | struct cgroup_subsys_state *css; |
| 1483 | int nextid, found; |
| 1484 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1485 | if (!root_memcg->use_hierarchy) { |
| 1486 | css_get(&root_memcg->css); |
| 1487 | ret = root_memcg; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | while (!ret) { |
| 1491 | rcu_read_lock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1492 | nextid = root_memcg->last_scanned_child + 1; |
| 1493 | css = css_get_next(&mem_cgroup_subsys, nextid, &root_memcg->css, |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1494 | &found); |
| 1495 | if (css && css_tryget(css)) |
| 1496 | ret = container_of(css, struct mem_cgroup, css); |
| 1497 | |
| 1498 | rcu_read_unlock(); |
| 1499 | /* Updates scanning parameter */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1500 | if (!css) { |
| 1501 | /* this means start scan from ID:1 */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1502 | root_memcg->last_scanned_child = 0; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1503 | } else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1504 | root_memcg->last_scanned_child = found; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | return ret; |
| 1508 | } |
| 1509 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1510 | /** |
| 1511 | * test_mem_cgroup_node_reclaimable |
| 1512 | * @mem: the target memcg |
| 1513 | * @nid: the node ID to be checked. |
| 1514 | * @noswap : specify true here if the user wants flle only information. |
| 1515 | * |
| 1516 | * This function returns whether the specified memcg contains any |
| 1517 | * reclaimable pages on a node. Returns true if there are any reclaimable |
| 1518 | * pages in the node. |
| 1519 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1520 | static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1521 | int nid, bool noswap) |
| 1522 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1523 | if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1524 | return true; |
| 1525 | if (noswap || !total_swap_pages) |
| 1526 | return false; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1527 | if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1528 | return true; |
| 1529 | return false; |
| 1530 | |
| 1531 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1532 | #if MAX_NUMNODES > 1 |
| 1533 | |
| 1534 | /* |
| 1535 | * Always updating the nodemask is not very good - even if we have an empty |
| 1536 | * list or the wrong list here, we can start from some node and traverse all |
| 1537 | * nodes based on the zonelist. So update the list loosely once per 10 secs. |
| 1538 | * |
| 1539 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1540 | static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1541 | { |
| 1542 | int nid; |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1543 | /* |
| 1544 | * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET |
| 1545 | * pagein/pageout changes since the last update. |
| 1546 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1547 | if (!atomic_read(&memcg->numainfo_events)) |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1548 | return; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1549 | if (atomic_inc_return(&memcg->numainfo_updating) > 1) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1550 | return; |
| 1551 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1552 | /* make a nodemask where this memcg uses memory from */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1553 | memcg->scan_nodes = node_states[N_HIGH_MEMORY]; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1554 | |
| 1555 | for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) { |
| 1556 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1557 | if (!test_mem_cgroup_node_reclaimable(memcg, nid, false)) |
| 1558 | node_clear(nid, memcg->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1559 | } |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1560 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1561 | atomic_set(&memcg->numainfo_events, 0); |
| 1562 | atomic_set(&memcg->numainfo_updating, 0); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | /* |
| 1566 | * Selecting a node where we start reclaim from. Because what we need is just |
| 1567 | * reducing usage counter, start from anywhere is O,K. Considering |
| 1568 | * memory reclaim from current node, there are pros. and cons. |
| 1569 | * |
| 1570 | * Freeing memory from current node means freeing memory from a node which |
| 1571 | * we'll use or we've used. So, it may make LRU bad. And if several threads |
| 1572 | * hit limits, it will see a contention on a node. But freeing from remote |
| 1573 | * node means more costs for memory reclaim because of memory latency. |
| 1574 | * |
| 1575 | * Now, we use round-robin. Better algorithm is welcomed. |
| 1576 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1577 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1578 | { |
| 1579 | int node; |
| 1580 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1581 | mem_cgroup_may_update_nodemask(memcg); |
| 1582 | node = memcg->last_scanned_node; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1583 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1584 | node = next_node(node, memcg->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1585 | if (node == MAX_NUMNODES) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1586 | node = first_node(memcg->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1587 | /* |
| 1588 | * We call this when we hit limit, not when pages are added to LRU. |
| 1589 | * No LRU may hold pages because all pages are UNEVICTABLE or |
| 1590 | * memcg is too small and all pages are not on LRU. In that case, |
| 1591 | * we use curret node. |
| 1592 | */ |
| 1593 | if (unlikely(node == MAX_NUMNODES)) |
| 1594 | node = numa_node_id(); |
| 1595 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1596 | memcg->last_scanned_node = node; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1597 | return node; |
| 1598 | } |
| 1599 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1600 | /* |
| 1601 | * Check all nodes whether it contains reclaimable pages or not. |
| 1602 | * For quick scan, we make use of scan_nodes. This will allow us to skip |
| 1603 | * unused nodes. But scan_nodes is lazily updated and may not cotain |
| 1604 | * enough new information. We need to do double check. |
| 1605 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1606 | bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1607 | { |
| 1608 | int nid; |
| 1609 | |
| 1610 | /* |
| 1611 | * quick check...making use of scan_node. |
| 1612 | * We can skip unused nodes. |
| 1613 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1614 | if (!nodes_empty(memcg->scan_nodes)) { |
| 1615 | for (nid = first_node(memcg->scan_nodes); |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1616 | nid < MAX_NUMNODES; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1617 | nid = next_node(nid, memcg->scan_nodes)) { |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1618 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1619 | if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1620 | return true; |
| 1621 | } |
| 1622 | } |
| 1623 | /* |
| 1624 | * Check rest of nodes. |
| 1625 | */ |
| 1626 | for_each_node_state(nid, N_HIGH_MEMORY) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1627 | if (node_isset(nid, memcg->scan_nodes)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1628 | continue; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1629 | if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1630 | return true; |
| 1631 | } |
| 1632 | return false; |
| 1633 | } |
| 1634 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1635 | #else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1636 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1637 | { |
| 1638 | return 0; |
| 1639 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1640 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1641 | bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1642 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1643 | return test_mem_cgroup_node_reclaimable(memcg, 0, noswap); |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1644 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1645 | #endif |
| 1646 | |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1647 | /* |
| 1648 | * Scan the hierarchy if needed to reclaim memory. We remember the last child |
| 1649 | * we reclaimed from, so that we don't end up penalizing one child extensively |
| 1650 | * based on its position in the children list. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1651 | * |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1652 | * root_memcg is the original ancestor that we've been reclaim from. |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1653 | * |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1654 | * 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] | 1655 | * (other groups can be removed while we're walking....) |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1656 | * |
| 1657 | * If shrink==true, for avoiding to free too much, this returns immedieately. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1658 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1659 | static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_memcg, |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1660 | struct zone *zone, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1661 | gfp_t gfp_mask, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1662 | unsigned long reclaim_options, |
| 1663 | unsigned long *total_scanned) |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1664 | { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1665 | struct mem_cgroup *victim; |
| 1666 | int ret, total = 0; |
| 1667 | int loop = 0; |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1668 | bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP; |
| 1669 | bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1670 | bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1671 | unsigned long excess; |
Johannes Weiner | 185efc0 | 2011-09-14 16:21:58 -0700 | [diff] [blame] | 1672 | unsigned long nr_scanned; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1673 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1674 | excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1675 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1676 | /* If memsw_is_minimum==1, swap-out is of-no-use. */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1677 | if (!check_soft && !shrink && root_memcg->memsw_is_minimum) |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1678 | noswap = true; |
| 1679 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1680 | while (1) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1681 | victim = mem_cgroup_select_victim(root_memcg); |
| 1682 | if (victim == root_memcg) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1683 | loop++; |
KAMEZAWA Hiroyuki | fbc29a2 | 2011-06-15 15:08:46 -0700 | [diff] [blame] | 1684 | /* |
| 1685 | * We are not draining per cpu cached charges during |
| 1686 | * soft limit reclaim because global reclaim doesn't |
| 1687 | * care about charges. It tries to free some memory and |
| 1688 | * charges will not give any. |
| 1689 | */ |
| 1690 | if (!check_soft && loop >= 1) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1691 | drain_all_stock_async(root_memcg); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1692 | if (loop >= 2) { |
| 1693 | /* |
| 1694 | * If we have not been able to reclaim |
| 1695 | * anything, it might because there are |
| 1696 | * no reclaimable pages under this hierarchy |
| 1697 | */ |
| 1698 | if (!check_soft || !total) { |
| 1699 | css_put(&victim->css); |
| 1700 | break; |
| 1701 | } |
| 1702 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1703 | * We want to do more targeted reclaim. |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1704 | * excess >> 2 is not to excessive so as to |
| 1705 | * reclaim too much, nor too less that we keep |
| 1706 | * coming back to reclaim from this cgroup |
| 1707 | */ |
| 1708 | if (total >= (excess >> 2) || |
| 1709 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) { |
| 1710 | css_put(&victim->css); |
| 1711 | break; |
| 1712 | } |
| 1713 | } |
| 1714 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1715 | if (!mem_cgroup_reclaimable(victim, noswap)) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1716 | /* this cgroup's local usage == 0 */ |
| 1717 | css_put(&victim->css); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1718 | continue; |
| 1719 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1720 | /* we use swappiness of local cgroup */ |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1721 | if (check_soft) { |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1722 | ret = mem_cgroup_shrink_node_zone(victim, gfp_mask, |
Johannes Weiner | 185efc0 | 2011-09-14 16:21:58 -0700 | [diff] [blame] | 1723 | noswap, zone, &nr_scanned); |
| 1724 | *total_scanned += nr_scanned; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1725 | } else |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1726 | ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, |
Johannes Weiner | 185efc0 | 2011-09-14 16:21:58 -0700 | [diff] [blame] | 1727 | noswap); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1728 | css_put(&victim->css); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1729 | /* |
| 1730 | * At shrinking usage, we can't check we should stop here or |
| 1731 | * reclaim more. It's depends on callers. last_scanned_child |
| 1732 | * will work enough for keeping fairness under tree. |
| 1733 | */ |
| 1734 | if (shrink) |
| 1735 | return ret; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1736 | total += ret; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1737 | if (check_soft) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1738 | if (!res_counter_soft_limit_excess(&root_memcg->res)) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1739 | return total; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1740 | } else if (mem_cgroup_margin(root_memcg)) |
Johannes Weiner | 4fd14eb | 2011-05-26 16:25:35 -0700 | [diff] [blame] | 1741 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1742 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1743 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1744 | } |
| 1745 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1746 | /* |
| 1747 | * Check OOM-Killer is already running under our hierarchy. |
| 1748 | * If someone is running, return false. |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1749 | * Has to be called with memcg_oom_lock |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1750 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1751 | static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1752 | { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1753 | struct mem_cgroup *iter, *failed = NULL; |
| 1754 | bool cond = true; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1755 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1756 | for_each_mem_cgroup_tree_cond(iter, memcg, cond) { |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 1757 | if (iter->oom_lock) { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1758 | /* |
| 1759 | * this subtree of our hierarchy is already locked |
| 1760 | * so we cannot give a lock. |
| 1761 | */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1762 | failed = iter; |
| 1763 | cond = false; |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 1764 | } else |
| 1765 | iter->oom_lock = true; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1766 | } |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1767 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1768 | if (!failed) |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 1769 | return true; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1770 | |
| 1771 | /* |
| 1772 | * OK, we failed to lock the whole subtree so we have to clean up |
| 1773 | * what we set up to the failing subtree |
| 1774 | */ |
| 1775 | cond = true; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1776 | for_each_mem_cgroup_tree_cond(iter, memcg, cond) { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1777 | if (iter == failed) { |
| 1778 | cond = false; |
| 1779 | continue; |
| 1780 | } |
| 1781 | iter->oom_lock = false; |
| 1782 | } |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 1783 | return false; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1784 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1785 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1786 | /* |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1787 | * Has to be called with memcg_oom_lock |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1788 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1789 | static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1790 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1791 | struct mem_cgroup *iter; |
| 1792 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1793 | for_each_mem_cgroup_tree(iter, memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1794 | iter->oom_lock = false; |
| 1795 | return 0; |
| 1796 | } |
| 1797 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1798 | static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1799 | { |
| 1800 | struct mem_cgroup *iter; |
| 1801 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1802 | for_each_mem_cgroup_tree(iter, memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1803 | atomic_inc(&iter->under_oom); |
| 1804 | } |
| 1805 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1806 | static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1807 | { |
| 1808 | struct mem_cgroup *iter; |
| 1809 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1810 | /* |
| 1811 | * When a new child is created while the hierarchy is under oom, |
| 1812 | * mem_cgroup_oom_lock() may not be called. We have to use |
| 1813 | * atomic_add_unless() here. |
| 1814 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1815 | for_each_mem_cgroup_tree(iter, memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1816 | atomic_add_unless(&iter->under_oom, -1, 0); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1819 | static DEFINE_SPINLOCK(memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1820 | static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq); |
| 1821 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1822 | struct oom_wait_info { |
| 1823 | struct mem_cgroup *mem; |
| 1824 | wait_queue_t wait; |
| 1825 | }; |
| 1826 | |
| 1827 | static int memcg_oom_wake_function(wait_queue_t *wait, |
| 1828 | unsigned mode, int sync, void *arg) |
| 1829 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1830 | struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg, |
| 1831 | *oom_wait_memcg; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1832 | struct oom_wait_info *oom_wait_info; |
| 1833 | |
| 1834 | oom_wait_info = container_of(wait, struct oom_wait_info, wait); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1835 | oom_wait_memcg = oom_wait_info->mem; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1836 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1837 | /* |
| 1838 | * Both of oom_wait_info->mem and wake_mem are stable under us. |
| 1839 | * Then we can use css_is_ancestor without taking care of RCU. |
| 1840 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1841 | if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg) |
| 1842 | && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg)) |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1843 | return 0; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1844 | return autoremove_wake_function(wait, mode, sync, arg); |
| 1845 | } |
| 1846 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1847 | static void memcg_wakeup_oom(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1848 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1849 | /* for filtering, pass "memcg" as argument. */ |
| 1850 | __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1851 | } |
| 1852 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1853 | static void memcg_oom_recover(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1854 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1855 | if (memcg && atomic_read(&memcg->under_oom)) |
| 1856 | memcg_wakeup_oom(memcg); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1859 | /* |
| 1860 | * try to call OOM killer. returns false if we should exit memory-reclaim loop. |
| 1861 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1862 | bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask) |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1863 | { |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1864 | struct oom_wait_info owait; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1865 | bool locked, need_to_kill; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1866 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1867 | owait.mem = memcg; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1868 | owait.wait.flags = 0; |
| 1869 | owait.wait.func = memcg_oom_wake_function; |
| 1870 | owait.wait.private = current; |
| 1871 | INIT_LIST_HEAD(&owait.wait.task_list); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1872 | need_to_kill = true; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1873 | mem_cgroup_mark_under_oom(memcg); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1874 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1875 | /* At first, try to OOM lock hierarchy under memcg.*/ |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1876 | spin_lock(&memcg_oom_lock); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1877 | locked = mem_cgroup_oom_lock(memcg); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1878 | /* |
| 1879 | * Even if signal_pending(), we can't quit charge() loop without |
| 1880 | * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL |
| 1881 | * under OOM is always welcomed, use TASK_KILLABLE here. |
| 1882 | */ |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1883 | prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1884 | if (!locked || memcg->oom_kill_disable) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1885 | need_to_kill = false; |
| 1886 | if (locked) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1887 | mem_cgroup_oom_notify(memcg); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1888 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1889 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1890 | if (need_to_kill) { |
| 1891 | finish_wait(&memcg_oom_waitq, &owait.wait); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1892 | mem_cgroup_out_of_memory(memcg, mask); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1893 | } else { |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1894 | schedule(); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1895 | finish_wait(&memcg_oom_waitq, &owait.wait); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1896 | } |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1897 | spin_lock(&memcg_oom_lock); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1898 | if (locked) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1899 | mem_cgroup_oom_unlock(memcg); |
| 1900 | memcg_wakeup_oom(memcg); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 1901 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1902 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1903 | mem_cgroup_unmark_under_oom(memcg); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1904 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1905 | if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current)) |
| 1906 | return false; |
| 1907 | /* Give chance to dying process */ |
KAMEZAWA Hiroyuki | 715a5ee | 2011-11-02 13:38:18 -0700 | [diff] [blame] | 1908 | schedule_timeout_uninterruptible(1); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1909 | return true; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1910 | } |
| 1911 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1912 | /* |
| 1913 | * Currently used to update mapped file statistics, but the routine can be |
| 1914 | * generalized to update other statistics as well. |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1915 | * |
| 1916 | * Notes: Race condition |
| 1917 | * |
| 1918 | * We usually use page_cgroup_lock() for accessing page_cgroup member but |
| 1919 | * it tends to be costly. But considering some conditions, we doesn't need |
| 1920 | * to do so _always_. |
| 1921 | * |
| 1922 | * Considering "charge", lock_page_cgroup() is not required because all |
| 1923 | * file-stat operations happen after a page is attached to radix-tree. There |
| 1924 | * are no race with "charge". |
| 1925 | * |
| 1926 | * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup |
| 1927 | * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even |
| 1928 | * if there are race with "uncharge". Statistics itself is properly handled |
| 1929 | * by flags. |
| 1930 | * |
| 1931 | * Considering "move", this is an only case we see a race. To make the race |
| 1932 | * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are |
| 1933 | * possibility of race condition. If there is, we take a lock. |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1934 | */ |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1935 | |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1936 | void mem_cgroup_update_page_stat(struct page *page, |
| 1937 | enum mem_cgroup_page_stat_item idx, int val) |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1938 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1939 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1940 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 1941 | bool need_unlock = false; |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 1942 | unsigned long uninitialized_var(flags); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1943 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1944 | if (unlikely(!pc)) |
| 1945 | return; |
| 1946 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1947 | rcu_read_lock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1948 | memcg = pc->mem_cgroup; |
| 1949 | if (unlikely(!memcg || !PageCgroupUsed(pc))) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1950 | goto out; |
| 1951 | /* pc->mem_cgroup is unstable ? */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1952 | if (unlikely(mem_cgroup_stealed(memcg)) || PageTransHuge(page)) { |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1953 | /* take a lock against to access pc->mem_cgroup */ |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 1954 | move_lock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1955 | need_unlock = true; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1956 | memcg = pc->mem_cgroup; |
| 1957 | if (!memcg || !PageCgroupUsed(pc)) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1958 | goto out; |
| 1959 | } |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1960 | |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1961 | switch (idx) { |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1962 | case MEMCG_NR_FILE_MAPPED: |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1963 | if (val > 0) |
| 1964 | SetPageCgroupFileMapped(pc); |
| 1965 | else if (!page_mapped(page)) |
KAMEZAWA Hiroyuki | 0c270f8 | 2010-10-27 15:33:39 -0700 | [diff] [blame] | 1966 | ClearPageCgroupFileMapped(pc); |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1967 | idx = MEM_CGROUP_STAT_FILE_MAPPED; |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1968 | break; |
| 1969 | default: |
| 1970 | BUG(); |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 1971 | } |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1972 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1973 | this_cpu_add(memcg->stat->count[idx], val); |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1974 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1975 | out: |
| 1976 | if (unlikely(need_unlock)) |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 1977 | move_unlock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1978 | rcu_read_unlock(); |
| 1979 | return; |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1980 | } |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1981 | EXPORT_SYMBOL(mem_cgroup_update_page_stat); |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1982 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1983 | /* |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1984 | * size of first charge trial. "32" comes from vmscan.c's magic value. |
| 1985 | * TODO: maybe necessary to use big numbers in big irons. |
| 1986 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1987 | #define CHARGE_BATCH 32U |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1988 | struct memcg_stock_pcp { |
| 1989 | struct mem_cgroup *cached; /* this never be root cgroup */ |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 1990 | unsigned int nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1991 | struct work_struct work; |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 1992 | unsigned long flags; |
| 1993 | #define FLUSHING_CACHED_CHARGE (0) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1994 | }; |
| 1995 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 1996 | static DEFINE_MUTEX(percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1997 | |
| 1998 | /* |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 1999 | * 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] | 2000 | * from local stock and true is returned. If the stock is 0 or charges from a |
| 2001 | * cgroup which is not current target, returns false. This stock will be |
| 2002 | * refilled. |
| 2003 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2004 | static bool consume_stock(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2005 | { |
| 2006 | struct memcg_stock_pcp *stock; |
| 2007 | bool ret = true; |
| 2008 | |
| 2009 | stock = &get_cpu_var(memcg_stock); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2010 | if (memcg == stock->cached && stock->nr_pages) |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2011 | stock->nr_pages--; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2012 | else /* need to call res_counter_charge */ |
| 2013 | ret = false; |
| 2014 | put_cpu_var(memcg_stock); |
| 2015 | return ret; |
| 2016 | } |
| 2017 | |
| 2018 | /* |
| 2019 | * Returns stocks cached in percpu to res_counter and reset cached information. |
| 2020 | */ |
| 2021 | static void drain_stock(struct memcg_stock_pcp *stock) |
| 2022 | { |
| 2023 | struct mem_cgroup *old = stock->cached; |
| 2024 | |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2025 | if (stock->nr_pages) { |
| 2026 | unsigned long bytes = stock->nr_pages * PAGE_SIZE; |
| 2027 | |
| 2028 | res_counter_uncharge(&old->res, bytes); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2029 | if (do_swap_account) |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2030 | res_counter_uncharge(&old->memsw, bytes); |
| 2031 | stock->nr_pages = 0; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2032 | } |
| 2033 | stock->cached = NULL; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | /* |
| 2037 | * This must be called under preempt disabled or must be called by |
| 2038 | * a thread which is pinned to local cpu. |
| 2039 | */ |
| 2040 | static void drain_local_stock(struct work_struct *dummy) |
| 2041 | { |
| 2042 | struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock); |
| 2043 | drain_stock(stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2044 | clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | /* |
| 2048 | * 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] | 2049 | * This will be consumed by consume_stock() function, later. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2050 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2051 | static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2052 | { |
| 2053 | struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock); |
| 2054 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2055 | if (stock->cached != memcg) { /* reset if necessary */ |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2056 | drain_stock(stock); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2057 | stock->cached = memcg; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2058 | } |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2059 | stock->nr_pages += nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2060 | put_cpu_var(memcg_stock); |
| 2061 | } |
| 2062 | |
| 2063 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2064 | * Drains all per-CPU charge caches for given root_memcg resp. subtree |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2065 | * of the hierarchy under it. sync flag says whether we should block |
| 2066 | * until the work is done. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2067 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2068 | static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2069 | { |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2070 | int cpu, curcpu; |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2071 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2072 | /* Notify other cpus that system-wide "drain" is running */ |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2073 | get_online_cpus(); |
Johannes Weiner | 5af12d0 | 2011-08-25 15:59:07 -0700 | [diff] [blame] | 2074 | curcpu = get_cpu(); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2075 | for_each_online_cpu(cpu) { |
| 2076 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2077 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2078 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2079 | memcg = stock->cached; |
| 2080 | if (!memcg || !stock->nr_pages) |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2081 | continue; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2082 | if (!mem_cgroup_same_or_subtree(root_memcg, memcg)) |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 2083 | continue; |
Michal Hocko | d1a05b6 | 2011-07-26 16:08:27 -0700 | [diff] [blame] | 2084 | if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) { |
| 2085 | if (cpu == curcpu) |
| 2086 | drain_local_stock(&stock->work); |
| 2087 | else |
| 2088 | schedule_work_on(cpu, &stock->work); |
| 2089 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2090 | } |
Johannes Weiner | 5af12d0 | 2011-08-25 15:59:07 -0700 | [diff] [blame] | 2091 | put_cpu(); |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2092 | |
| 2093 | if (!sync) |
| 2094 | goto out; |
| 2095 | |
| 2096 | for_each_online_cpu(cpu) { |
| 2097 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2098 | if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2099 | flush_work(&stock->work); |
| 2100 | } |
| 2101 | out: |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2102 | put_online_cpus(); |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | /* |
| 2106 | * Tries to drain stocked charges in other cpus. This function is asynchronous |
| 2107 | * and just put a work per cpu for draining localy on each cpu. Caller can |
| 2108 | * expects some charges will be back to res_counter later but cannot wait for |
| 2109 | * it. |
| 2110 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2111 | static void drain_all_stock_async(struct mem_cgroup *root_memcg) |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2112 | { |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2113 | /* |
| 2114 | * If someone calls draining, avoid adding more kworker runs. |
| 2115 | */ |
| 2116 | if (!mutex_trylock(&percpu_charge_mutex)) |
| 2117 | return; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2118 | drain_all_stock(root_memcg, false); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2119 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | /* This is a synchronous drain interface. */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2123 | static void drain_all_stock_sync(struct mem_cgroup *root_memcg) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2124 | { |
| 2125 | /* called when force_empty is called */ |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2126 | mutex_lock(&percpu_charge_mutex); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2127 | drain_all_stock(root_memcg, true); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2128 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2129 | } |
| 2130 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2131 | /* |
| 2132 | * This function drains percpu counter value from DEAD cpu and |
| 2133 | * move it to local cpu. Note that this function can be preempted. |
| 2134 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2135 | 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] | 2136 | { |
| 2137 | int i; |
| 2138 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2139 | spin_lock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2140 | for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2141 | long x = per_cpu(memcg->stat->count[i], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2142 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2143 | per_cpu(memcg->stat->count[i], cpu) = 0; |
| 2144 | memcg->nocpu_base.count[i] += x; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2145 | } |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2146 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2147 | unsigned long x = per_cpu(memcg->stat->events[i], cpu); |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2148 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2149 | per_cpu(memcg->stat->events[i], cpu) = 0; |
| 2150 | memcg->nocpu_base.events[i] += x; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2151 | } |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2152 | /* 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] | 2153 | per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0; |
| 2154 | spin_unlock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2155 | } |
| 2156 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2157 | 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] | 2158 | { |
| 2159 | int idx = MEM_CGROUP_ON_MOVE; |
| 2160 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2161 | spin_lock(&memcg->pcp_counter_lock); |
| 2162 | per_cpu(memcg->stat->count[idx], cpu) = memcg->nocpu_base.count[idx]; |
| 2163 | spin_unlock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb, |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2167 | unsigned long action, |
| 2168 | void *hcpu) |
| 2169 | { |
| 2170 | int cpu = (unsigned long)hcpu; |
| 2171 | struct memcg_stock_pcp *stock; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2172 | struct mem_cgroup *iter; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2173 | |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2174 | if ((action == CPU_ONLINE)) { |
| 2175 | for_each_mem_cgroup_all(iter) |
| 2176 | synchronize_mem_cgroup_on_move(iter, cpu); |
| 2177 | return NOTIFY_OK; |
| 2178 | } |
| 2179 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2180 | if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2181 | return NOTIFY_OK; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2182 | |
| 2183 | for_each_mem_cgroup_all(iter) |
| 2184 | mem_cgroup_drain_pcp_counter(iter, cpu); |
| 2185 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2186 | stock = &per_cpu(memcg_stock, cpu); |
| 2187 | drain_stock(stock); |
| 2188 | return NOTIFY_OK; |
| 2189 | } |
| 2190 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2191 | |
| 2192 | /* See __mem_cgroup_try_charge() for details */ |
| 2193 | enum { |
| 2194 | CHARGE_OK, /* success */ |
| 2195 | CHARGE_RETRY, /* need to retry but retry is not bad */ |
| 2196 | CHARGE_NOMEM, /* we can't do more. return -ENOMEM */ |
| 2197 | CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */ |
| 2198 | CHARGE_OOM_DIE, /* the current is killed because of OOM */ |
| 2199 | }; |
| 2200 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2201 | 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] | 2202 | unsigned int nr_pages, bool oom_check) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2203 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2204 | unsigned long csize = nr_pages * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2205 | struct mem_cgroup *mem_over_limit; |
| 2206 | struct res_counter *fail_res; |
| 2207 | unsigned long flags = 0; |
| 2208 | int ret; |
| 2209 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2210 | ret = res_counter_charge(&memcg->res, csize, &fail_res); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2211 | |
| 2212 | if (likely(!ret)) { |
| 2213 | if (!do_swap_account) |
| 2214 | return CHARGE_OK; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2215 | ret = res_counter_charge(&memcg->memsw, csize, &fail_res); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2216 | if (likely(!ret)) |
| 2217 | return CHARGE_OK; |
| 2218 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2219 | res_counter_uncharge(&memcg->res, csize); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2220 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw); |
| 2221 | flags |= MEM_CGROUP_RECLAIM_NOSWAP; |
| 2222 | } else |
| 2223 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, res); |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2224 | /* |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2225 | * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch |
| 2226 | * of regular pages (CHARGE_BATCH), or a single regular page (1). |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2227 | * |
| 2228 | * Never reclaim on behalf of optional batching, retry with a |
| 2229 | * single page instead. |
| 2230 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2231 | if (nr_pages == CHARGE_BATCH) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2232 | return CHARGE_RETRY; |
| 2233 | |
| 2234 | if (!(gfp_mask & __GFP_WAIT)) |
| 2235 | return CHARGE_WOULDBLOCK; |
| 2236 | |
| 2237 | ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 2238 | gfp_mask, flags, NULL); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2239 | if (mem_cgroup_margin(mem_over_limit) >= nr_pages) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2240 | return CHARGE_RETRY; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2241 | /* |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2242 | * Even though the limit is exceeded at this point, reclaim |
| 2243 | * may have been able to free some pages. Retry the charge |
| 2244 | * before killing the task. |
| 2245 | * |
| 2246 | * Only for regular pages, though: huge pages are rather |
| 2247 | * unlikely to succeed so close to the limit, and we fall back |
| 2248 | * to regular pages anyway in case of failure. |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2249 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2250 | if (nr_pages == 1 && ret) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2251 | return CHARGE_RETRY; |
| 2252 | |
| 2253 | /* |
| 2254 | * At task move, charge accounts can be doubly counted. So, it's |
| 2255 | * better to wait until the end of task_move if something is going on. |
| 2256 | */ |
| 2257 | if (mem_cgroup_wait_acct_move(mem_over_limit)) |
| 2258 | return CHARGE_RETRY; |
| 2259 | |
| 2260 | /* If we don't need to call oom-killer at el, return immediately */ |
| 2261 | if (!oom_check) |
| 2262 | return CHARGE_NOMEM; |
| 2263 | /* check OOM */ |
| 2264 | if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask)) |
| 2265 | return CHARGE_OOM_DIE; |
| 2266 | |
| 2267 | return CHARGE_RETRY; |
| 2268 | } |
| 2269 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2270 | /* |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2271 | * Unlike exported interface, "oom" parameter is added. if oom==true, |
| 2272 | * oom-killer can be invoked. |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2273 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2274 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2275 | gfp_t gfp_mask, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2276 | unsigned int nr_pages, |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2277 | struct mem_cgroup **ptr, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2278 | bool oom) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2279 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2280 | unsigned int batch = max(CHARGE_BATCH, nr_pages); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2281 | int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2282 | struct mem_cgroup *memcg = NULL; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2283 | int ret; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2284 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2285 | /* |
| 2286 | * Unlike gloval-vm's OOM-kill, we're not in memory shortage |
| 2287 | * in system level. So, allow to go ahead dying process in addition to |
| 2288 | * MEMDIE process. |
| 2289 | */ |
| 2290 | if (unlikely(test_thread_flag(TIF_MEMDIE) |
| 2291 | || fatal_signal_pending(current))) |
| 2292 | goto bypass; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2293 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2294 | /* |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2295 | * We always charge the cgroup the mm_struct belongs to. |
| 2296 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2297 | * thread group leader migrates. It's possible that mm is not |
| 2298 | * set, if so charge the init_mm (happens for pagecache usage). |
| 2299 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2300 | if (!*ptr && !mm) |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2301 | goto bypass; |
| 2302 | again: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2303 | if (*ptr) { /* css should be a valid one */ |
| 2304 | memcg = *ptr; |
| 2305 | VM_BUG_ON(css_is_removed(&memcg->css)); |
| 2306 | if (mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2307 | goto done; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2308 | if (nr_pages == 1 && consume_stock(memcg)) |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2309 | goto done; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2310 | css_get(&memcg->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2311 | } else { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2312 | struct task_struct *p; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2313 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2314 | rcu_read_lock(); |
| 2315 | p = rcu_dereference(mm->owner); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2316 | /* |
KAMEZAWA Hiroyuki | ebb76ce | 2010-12-29 14:07:11 -0800 | [diff] [blame] | 2317 | * Because we don't have task_lock(), "p" can exit. |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2318 | * 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] | 2319 | * race with swapoff. Then, we have small risk of mis-accouning. |
| 2320 | * But such kind of mis-account by race always happens because |
| 2321 | * we don't have cgroup_mutex(). It's overkill and we allo that |
| 2322 | * small race, here. |
| 2323 | * (*) swapoff at el will charge against mm-struct not against |
| 2324 | * task-struct. So, mm->owner can be NULL. |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2325 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2326 | memcg = mem_cgroup_from_task(p); |
| 2327 | if (!memcg || mem_cgroup_is_root(memcg)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2328 | rcu_read_unlock(); |
| 2329 | goto done; |
| 2330 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2331 | if (nr_pages == 1 && consume_stock(memcg)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2332 | /* |
| 2333 | * It seems dagerous to access memcg without css_get(). |
| 2334 | * But considering how consume_stok works, it's not |
| 2335 | * necessary. If consume_stock success, some charges |
| 2336 | * from this memcg are cached on this cpu. So, we |
| 2337 | * don't need to call css_get()/css_tryget() before |
| 2338 | * calling consume_stock(). |
| 2339 | */ |
| 2340 | rcu_read_unlock(); |
| 2341 | goto done; |
| 2342 | } |
| 2343 | /* after here, we may be blocked. we need to get refcnt */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2344 | if (!css_tryget(&memcg->css)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2345 | rcu_read_unlock(); |
| 2346 | goto again; |
| 2347 | } |
| 2348 | rcu_read_unlock(); |
| 2349 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2350 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2351 | do { |
| 2352 | bool oom_check; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2353 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2354 | /* If killed, bypass charge */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2355 | if (fatal_signal_pending(current)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2356 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2357 | goto bypass; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2358 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2359 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2360 | oom_check = false; |
| 2361 | if (oom && !nr_oom_retries) { |
| 2362 | oom_check = true; |
| 2363 | nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
| 2364 | } |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 2365 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2366 | ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, oom_check); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2367 | switch (ret) { |
| 2368 | case CHARGE_OK: |
| 2369 | break; |
| 2370 | case CHARGE_RETRY: /* not in OOM situation but retry */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2371 | batch = nr_pages; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2372 | css_put(&memcg->css); |
| 2373 | memcg = NULL; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2374 | goto again; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2375 | case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2376 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2377 | goto nomem; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2378 | case CHARGE_NOMEM: /* OOM routine works */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2379 | if (!oom) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2380 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2381 | goto nomem; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2382 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2383 | /* If oom, we never return -ENOMEM */ |
| 2384 | nr_oom_retries--; |
| 2385 | break; |
| 2386 | case CHARGE_OOM_DIE: /* Killed by OOM Killer */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2387 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2388 | goto bypass; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2389 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2390 | } while (ret != CHARGE_OK); |
| 2391 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2392 | if (batch > nr_pages) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2393 | refill_stock(memcg, batch - nr_pages); |
| 2394 | css_put(&memcg->css); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2395 | done: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2396 | *ptr = memcg; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2397 | return 0; |
| 2398 | nomem: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2399 | *ptr = NULL; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2400 | return -ENOMEM; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2401 | bypass: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2402 | *ptr = NULL; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2403 | return 0; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2404 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2405 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2406 | /* |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2407 | * Somemtimes we have to undo a charge we got by try_charge(). |
| 2408 | * This function is for that and do uncharge, put css's refcnt. |
| 2409 | * gotten by try_charge(). |
| 2410 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2411 | static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg, |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2412 | unsigned int nr_pages) |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2413 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2414 | if (!mem_cgroup_is_root(memcg)) { |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2415 | unsigned long bytes = nr_pages * PAGE_SIZE; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2416 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2417 | res_counter_uncharge(&memcg->res, bytes); |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2418 | if (do_swap_account) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2419 | res_counter_uncharge(&memcg->memsw, bytes); |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2420 | } |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2421 | } |
| 2422 | |
| 2423 | /* |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2424 | * A helper function to get mem_cgroup from ID. must be called under |
| 2425 | * rcu_read_lock(). The caller must check css_is_removed() or some if |
| 2426 | * it's concern. (dropping refcnt from swap can be called against removed |
| 2427 | * memcg.) |
| 2428 | */ |
| 2429 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) |
| 2430 | { |
| 2431 | struct cgroup_subsys_state *css; |
| 2432 | |
| 2433 | /* ID 0 is unused ID */ |
| 2434 | if (!id) |
| 2435 | return NULL; |
| 2436 | css = css_lookup(&mem_cgroup_subsys, id); |
| 2437 | if (!css) |
| 2438 | return NULL; |
| 2439 | return container_of(css, struct mem_cgroup, css); |
| 2440 | } |
| 2441 | |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2442 | struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2443 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2444 | struct mem_cgroup *memcg = NULL; |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2445 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2446 | unsigned short id; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2447 | swp_entry_t ent; |
| 2448 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2449 | VM_BUG_ON(!PageLocked(page)); |
| 2450 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2451 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | c0bd3f63 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2452 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2453 | if (PageCgroupUsed(pc)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2454 | memcg = pc->mem_cgroup; |
| 2455 | if (memcg && !css_tryget(&memcg->css)) |
| 2456 | memcg = NULL; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2457 | } else if (PageSwapCache(page)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2458 | ent.val = page_private(page); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2459 | id = lookup_swap_cgroup(ent); |
| 2460 | rcu_read_lock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2461 | memcg = mem_cgroup_lookup(id); |
| 2462 | if (memcg && !css_tryget(&memcg->css)) |
| 2463 | memcg = NULL; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2464 | rcu_read_unlock(); |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2465 | } |
Daisuke Nishimura | c0bd3f63 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2466 | unlock_page_cgroup(pc); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2467 | return memcg; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2468 | } |
| 2469 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2470 | static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg, |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2471 | struct page *page, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2472 | unsigned int nr_pages, |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2473 | struct page_cgroup *pc, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2474 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2475 | { |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2476 | lock_page_cgroup(pc); |
| 2477 | if (unlikely(PageCgroupUsed(pc))) { |
| 2478 | unlock_page_cgroup(pc); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2479 | __mem_cgroup_cancel_charge(memcg, nr_pages); |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2480 | return; |
| 2481 | } |
| 2482 | /* |
| 2483 | * we don't need page_cgroup_lock about tail pages, becase they are not |
| 2484 | * accessed by any other context at this point. |
| 2485 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2486 | pc->mem_cgroup = memcg; |
KAMEZAWA Hiroyuki | 261fb61 | 2009-09-23 15:56:33 -0700 | [diff] [blame] | 2487 | /* |
| 2488 | * We access a page_cgroup asynchronously without lock_page_cgroup(). |
| 2489 | * Especially when a page_cgroup is taken from a page, pc->mem_cgroup |
| 2490 | * is accessed after testing USED bit. To make pc->mem_cgroup visible |
| 2491 | * before USED bit, we need memory barrier here. |
| 2492 | * See mem_cgroup_add_lru_list(), etc. |
| 2493 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2494 | smp_wmb(); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 2495 | switch (ctype) { |
| 2496 | case MEM_CGROUP_CHARGE_TYPE_CACHE: |
| 2497 | case MEM_CGROUP_CHARGE_TYPE_SHMEM: |
| 2498 | SetPageCgroupCache(pc); |
| 2499 | SetPageCgroupUsed(pc); |
| 2500 | break; |
| 2501 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
| 2502 | ClearPageCgroupCache(pc); |
| 2503 | SetPageCgroupUsed(pc); |
| 2504 | break; |
| 2505 | default: |
| 2506 | break; |
| 2507 | } |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2508 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2509 | mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), nr_pages); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2510 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 2511 | /* |
| 2512 | * "charge_statistics" updated event counter. Then, check it. |
| 2513 | * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree. |
| 2514 | * if they exceeds softlimit. |
| 2515 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2516 | memcg_check_events(memcg, page); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2517 | } |
| 2518 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2519 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 2520 | |
| 2521 | #define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\ |
| 2522 | (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION)) |
| 2523 | /* |
| 2524 | * Because tail pages are not marked as "used", set it. We're under |
| 2525 | * zone->lru_lock, 'splitting on pmd' and compund_lock. |
| 2526 | */ |
| 2527 | void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail) |
| 2528 | { |
| 2529 | struct page_cgroup *head_pc = lookup_page_cgroup(head); |
| 2530 | struct page_cgroup *tail_pc = lookup_page_cgroup(tail); |
| 2531 | unsigned long flags; |
| 2532 | |
KAMEZAWA Hiroyuki | 3d37c4a | 2011-01-25 15:07:28 -0800 | [diff] [blame] | 2533 | if (mem_cgroup_disabled()) |
| 2534 | return; |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2535 | /* |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2536 | * We have no races with charge/uncharge but will have races with |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2537 | * page state accounting. |
| 2538 | */ |
| 2539 | move_lock_page_cgroup(head_pc, &flags); |
| 2540 | |
| 2541 | tail_pc->mem_cgroup = head_pc->mem_cgroup; |
| 2542 | smp_wmb(); /* see __commit_charge() */ |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2543 | if (PageCgroupAcctLRU(head_pc)) { |
| 2544 | enum lru_list lru; |
| 2545 | struct mem_cgroup_per_zone *mz; |
| 2546 | |
| 2547 | /* |
| 2548 | * LRU flags cannot be copied because we need to add tail |
| 2549 | *.page to LRU by generic call and our hook will be called. |
| 2550 | * We hold lru_lock, then, reduce counter directly. |
| 2551 | */ |
| 2552 | lru = page_lru(head); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 2553 | mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2554 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
| 2555 | } |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2556 | tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT; |
| 2557 | move_unlock_page_cgroup(head_pc, &flags); |
| 2558 | } |
| 2559 | #endif |
| 2560 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2561 | /** |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2562 | * mem_cgroup_move_account - move account of the page |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2563 | * @page: the page |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2564 | * @nr_pages: number of regular pages (>1 for huge pages) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2565 | * @pc: page_cgroup of the page. |
| 2566 | * @from: mem_cgroup which the page is moved from. |
| 2567 | * @to: mem_cgroup which the page is moved to. @from != @to. |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2568 | * @uncharge: whether we should call uncharge and css_put against @from. |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2569 | * |
| 2570 | * The caller must confirm following. |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2571 | * - page is not on LRU (isolate_page() is useful.) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2572 | * - compound_lock is held when nr_pages > 1 |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2573 | * |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2574 | * 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] | 2575 | * 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] | 2576 | * true, this function does "uncharge" from old cgroup, but it doesn't if |
| 2577 | * @uncharge is false, so a caller should do "uncharge". |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2578 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2579 | static int mem_cgroup_move_account(struct page *page, |
| 2580 | unsigned int nr_pages, |
| 2581 | struct page_cgroup *pc, |
| 2582 | struct mem_cgroup *from, |
| 2583 | struct mem_cgroup *to, |
| 2584 | bool uncharge) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2585 | { |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2586 | unsigned long flags; |
| 2587 | int ret; |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2588 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2589 | VM_BUG_ON(from == to); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2590 | VM_BUG_ON(PageLRU(page)); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2591 | /* |
| 2592 | * The page is isolated from LRU. So, collapse function |
| 2593 | * will not handle this page. But page splitting can happen. |
| 2594 | * Do this check under compound_page_lock(). The caller should |
| 2595 | * hold it. |
| 2596 | */ |
| 2597 | ret = -EBUSY; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2598 | if (nr_pages > 1 && !PageTransHuge(page)) |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2599 | goto out; |
| 2600 | |
| 2601 | lock_page_cgroup(pc); |
| 2602 | |
| 2603 | ret = -EINVAL; |
| 2604 | if (!PageCgroupUsed(pc) || pc->mem_cgroup != from) |
| 2605 | goto unlock; |
| 2606 | |
| 2607 | move_lock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2608 | |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 2609 | if (PageCgroupFileMapped(pc)) { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 2610 | /* Update mapped_file data for mem_cgroup */ |
| 2611 | preempt_disable(); |
| 2612 | __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 2613 | __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 2614 | preempt_enable(); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2615 | } |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2616 | mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2617 | if (uncharge) |
| 2618 | /* This is not "cancel", but cancel_charge does all we need. */ |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2619 | __mem_cgroup_cancel_charge(from, nr_pages); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2620 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2621 | /* caller should have done css_get */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2622 | pc->mem_cgroup = to; |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2623 | mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2624 | /* |
| 2625 | * We charges against "to" which may not have any tasks. Then, "to" |
| 2626 | * can be under rmdir(). But in current implementation, caller of |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 2627 | * 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] | 2628 | * guaranteed that "to" is never removed. So, we don't check rmdir |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 2629 | * status here. |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2630 | */ |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2631 | move_unlock_page_cgroup(pc, &flags); |
| 2632 | ret = 0; |
| 2633 | unlock: |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2634 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 2635 | /* |
| 2636 | * check events |
| 2637 | */ |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2638 | memcg_check_events(to, page); |
| 2639 | memcg_check_events(from, page); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2640 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2641 | return ret; |
| 2642 | } |
| 2643 | |
| 2644 | /* |
| 2645 | * move charges to its parent. |
| 2646 | */ |
| 2647 | |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2648 | static int mem_cgroup_move_parent(struct page *page, |
| 2649 | struct page_cgroup *pc, |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2650 | struct mem_cgroup *child, |
| 2651 | gfp_t gfp_mask) |
| 2652 | { |
| 2653 | struct cgroup *cg = child->css.cgroup; |
| 2654 | struct cgroup *pcg = cg->parent; |
| 2655 | struct mem_cgroup *parent; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2656 | unsigned int nr_pages; |
Andrew Morton | 4be4489 | 2011-03-23 16:42:39 -0700 | [diff] [blame] | 2657 | unsigned long uninitialized_var(flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2658 | int ret; |
| 2659 | |
| 2660 | /* Is ROOT ? */ |
| 2661 | if (!pcg) |
| 2662 | return -EINVAL; |
| 2663 | |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2664 | ret = -EBUSY; |
| 2665 | if (!get_page_unless_zero(page)) |
| 2666 | goto out; |
| 2667 | if (isolate_lru_page(page)) |
| 2668 | goto put; |
KAMEZAWA Hiroyuki | 52dbb90 | 2011-01-25 15:07:29 -0800 | [diff] [blame] | 2669 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2670 | nr_pages = hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2671 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2672 | parent = mem_cgroup_from_cont(pcg); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2673 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2674 | if (ret || !parent) |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2675 | goto put_back; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2676 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2677 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2678 | flags = compound_lock_irqsave(page); |
| 2679 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2680 | ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2681 | if (ret) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2682 | __mem_cgroup_cancel_charge(parent, nr_pages); |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 2683 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2684 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2685 | compound_unlock_irqrestore(page, flags); |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 2686 | put_back: |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2687 | putback_lru_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2688 | put: |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 2689 | put_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2690 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2691 | return ret; |
| 2692 | } |
| 2693 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2694 | /* |
| 2695 | * Charge the memory controller for page usage. |
| 2696 | * Return |
| 2697 | * 0 if the charge was successful |
| 2698 | * < 0 if the cgroup is over its limit |
| 2699 | */ |
| 2700 | 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] | 2701 | gfp_t gfp_mask, enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2702 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2703 | struct mem_cgroup *memcg = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2704 | unsigned int nr_pages = 1; |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 2705 | struct page_cgroup *pc; |
| 2706 | bool oom = true; |
| 2707 | int ret; |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2708 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2709 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2710 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2711 | VM_BUG_ON(!PageTransHuge(page)); |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 2712 | /* |
| 2713 | * Never OOM-kill a process for a huge page. The |
| 2714 | * fault handler will fall back to regular pages. |
| 2715 | */ |
| 2716 | oom = false; |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2717 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2718 | |
| 2719 | pc = lookup_page_cgroup(page); |
Johannes Weiner | af4a662 | 2011-03-23 16:42:24 -0700 | [diff] [blame] | 2720 | BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */ |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2721 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2722 | ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom); |
| 2723 | if (ret || !memcg) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2724 | return ret; |
| 2725 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2726 | __mem_cgroup_commit_charge(memcg, page, nr_pages, pc, ctype); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2727 | return 0; |
| 2728 | } |
| 2729 | |
| 2730 | int mem_cgroup_newpage_charge(struct page *page, |
| 2731 | struct mm_struct *mm, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2732 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2733 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 2734 | return 0; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2735 | /* |
| 2736 | * If already mapped, we don't have to account. |
| 2737 | * If page cache, page->mapping has address_space. |
| 2738 | * But page->mapping may have out-of-use anon_vma pointer, |
| 2739 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping |
| 2740 | * is NULL. |
| 2741 | */ |
| 2742 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) |
| 2743 | return 0; |
| 2744 | if (unlikely(!mm)) |
| 2745 | mm = &init_mm; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2746 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2747 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2748 | } |
| 2749 | |
Daisuke Nishimura | 83aae4c7 | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2750 | static void |
| 2751 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 2752 | enum charge_type ctype); |
| 2753 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2754 | static void |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2755 | __mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2756 | enum charge_type ctype) |
| 2757 | { |
| 2758 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 2759 | /* |
| 2760 | * In some case, SwapCache, FUSE(splice_buf->radixtree), the page |
| 2761 | * is already on LRU. It means the page may on some other page_cgroup's |
| 2762 | * LRU. Take care of it. |
| 2763 | */ |
| 2764 | mem_cgroup_lru_del_before_commit(page); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2765 | __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype); |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2766 | mem_cgroup_lru_add_after_commit(page); |
| 2767 | return; |
| 2768 | } |
| 2769 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 2770 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 2771 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2772 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2773 | struct mem_cgroup *memcg = NULL; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2774 | int ret; |
| 2775 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2776 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 2777 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2778 | if (PageCompound(page)) |
| 2779 | return 0; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2780 | |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2781 | if (unlikely(!mm)) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2782 | mm = &init_mm; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2783 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2784 | if (page_is_file_cache(page)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2785 | ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &memcg, true); |
| 2786 | if (ret || !memcg) |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2787 | return ret; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2788 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2789 | /* |
| 2790 | * FUSE reuses pages without going through the final |
| 2791 | * put that would remove them from the LRU list, make |
| 2792 | * sure that they get relinked properly. |
| 2793 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2794 | __mem_cgroup_commit_charge_lrucare(page, memcg, |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2795 | MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 2796 | return ret; |
| 2797 | } |
Daisuke Nishimura | 83aae4c7 | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2798 | /* shmem */ |
| 2799 | if (PageSwapCache(page)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2800 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &memcg); |
Daisuke Nishimura | 83aae4c7 | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2801 | if (!ret) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2802 | __mem_cgroup_commit_charge_swapin(page, memcg, |
Daisuke Nishimura | 83aae4c7 | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2803 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
| 2804 | } else |
| 2805 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2806 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2807 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2808 | return ret; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 2809 | } |
| 2810 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2811 | /* |
| 2812 | * While swap-in, try_charge -> commit or cancel, the page is locked. |
| 2813 | * 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] | 2814 | * struct page_cgroup is acquired. This refcnt will be consumed by |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2815 | * "commit()" or removed by "cancel()" |
| 2816 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2817 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
| 2818 | struct page *page, |
| 2819 | gfp_t mask, struct mem_cgroup **ptr) |
| 2820 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2821 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2822 | int ret; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2823 | |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 2824 | *ptr = NULL; |
| 2825 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2826 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2827 | return 0; |
| 2828 | |
| 2829 | if (!do_swap_account) |
| 2830 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2831 | /* |
| 2832 | * A racing thread's fault, or swapoff, may have already updated |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2833 | * the pte, and even removed page from swap cache: in those cases |
| 2834 | * do_swap_page()'s pte_same() test will fail; but there's also a |
| 2835 | * KSM case which does need to charge the page. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2836 | */ |
| 2837 | if (!PageSwapCache(page)) |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2838 | goto charge_cur_mm; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2839 | memcg = try_get_mem_cgroup_from_page(page); |
| 2840 | if (!memcg) |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2841 | goto charge_cur_mm; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2842 | *ptr = memcg; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2843 | ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2844 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2845 | return ret; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2846 | charge_cur_mm: |
| 2847 | if (unlikely(!mm)) |
| 2848 | mm = &init_mm; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2849 | return __mem_cgroup_try_charge(mm, mask, 1, ptr, true); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2850 | } |
| 2851 | |
Daisuke Nishimura | 83aae4c7 | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2852 | static void |
| 2853 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 2854 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2855 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2856 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2857 | return; |
| 2858 | if (!ptr) |
| 2859 | return; |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2860 | cgroup_exclude_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2861 | |
| 2862 | __mem_cgroup_commit_charge_lrucare(page, ptr, ctype); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2863 | /* |
| 2864 | * Now swap is on-memory. This means this page may be |
| 2865 | * counted both as mem and swap....double count. |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2866 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
| 2867 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() |
| 2868 | * may call delete_from_swap_cache() before reach here. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2869 | */ |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2870 | if (do_swap_account && PageSwapCache(page)) { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2871 | swp_entry_t ent = {.val = page_private(page)}; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2872 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2873 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2874 | |
| 2875 | id = swap_cgroup_record(ent, 0); |
| 2876 | rcu_read_lock(); |
| 2877 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2878 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2879 | /* |
| 2880 | * This recorded memcg can be obsolete one. So, avoid |
| 2881 | * calling css_tryget |
| 2882 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2883 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 2884 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2885 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2886 | mem_cgroup_put(memcg); |
| 2887 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2888 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2889 | } |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2890 | /* |
| 2891 | * At swapin, we may charge account against cgroup which has no tasks. |
| 2892 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 2893 | * In that case, we need to call pre_destroy() again. check it here. |
| 2894 | */ |
| 2895 | cgroup_release_and_wakeup_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2896 | } |
| 2897 | |
Daisuke Nishimura | 83aae4c7 | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2898 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
| 2899 | { |
| 2900 | __mem_cgroup_commit_charge_swapin(page, ptr, |
| 2901 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 2902 | } |
| 2903 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2904 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2905 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2906 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2907 | return; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2908 | if (!memcg) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2909 | return; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2910 | __mem_cgroup_cancel_charge(memcg, 1); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2911 | } |
| 2912 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2913 | static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2914 | unsigned int nr_pages, |
| 2915 | const enum charge_type ctype) |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2916 | { |
| 2917 | struct memcg_batch_info *batch = NULL; |
| 2918 | bool uncharge_memsw = true; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2919 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2920 | /* If swapout, usage of swap doesn't decrease */ |
| 2921 | if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) |
| 2922 | uncharge_memsw = false; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2923 | |
| 2924 | batch = ¤t->memcg_batch; |
| 2925 | /* |
| 2926 | * In usual, we do css_get() when we remember memcg pointer. |
| 2927 | * But in this case, we keep res->usage until end of a series of |
| 2928 | * uncharges. Then, it's ok to ignore memcg's refcnt. |
| 2929 | */ |
| 2930 | if (!batch->memcg) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2931 | batch->memcg = memcg; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2932 | /* |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2933 | * do_batch > 0 when unmapping pages or inode invalidate/truncate. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2934 | * 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] | 2935 | * the same cgroup and we have chance to coalesce uncharges. |
| 2936 | * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE) |
| 2937 | * because we want to do uncharge as soon as possible. |
| 2938 | */ |
| 2939 | |
| 2940 | if (!batch->do_batch || test_thread_flag(TIF_MEMDIE)) |
| 2941 | goto direct_uncharge; |
| 2942 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2943 | if (nr_pages > 1) |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2944 | goto direct_uncharge; |
| 2945 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2946 | /* |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2947 | * In typical case, batch->memcg == mem. This means we can |
| 2948 | * merge a series of uncharges to an uncharge of res_counter. |
| 2949 | * If not, we uncharge res_counter ony by one. |
| 2950 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2951 | if (batch->memcg != memcg) |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2952 | goto direct_uncharge; |
| 2953 | /* remember freed charge and uncharge it later */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 2954 | batch->nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2955 | if (uncharge_memsw) |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 2956 | batch->memsw_nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2957 | return; |
| 2958 | direct_uncharge: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2959 | res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2960 | if (uncharge_memsw) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2961 | res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE); |
| 2962 | if (unlikely(batch->memcg != memcg)) |
| 2963 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2964 | return; |
| 2965 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2966 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2967 | /* |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2968 | * uncharge if !page_mapped(page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2969 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2970 | static struct mem_cgroup * |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2971 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2972 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2973 | struct mem_cgroup *memcg = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2974 | unsigned int nr_pages = 1; |
| 2975 | struct page_cgroup *pc; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2976 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2977 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2978 | return NULL; |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 2979 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2980 | if (PageSwapCache(page)) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2981 | return NULL; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2982 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2983 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2984 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2985 | VM_BUG_ON(!PageTransHuge(page)); |
| 2986 | } |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2987 | /* |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 2988 | * Check if our page_cgroup is valid |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2989 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2990 | pc = lookup_page_cgroup(page); |
| 2991 | if (unlikely(!pc || !PageCgroupUsed(pc))) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2992 | return NULL; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2993 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2994 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2995 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2996 | memcg = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2997 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2998 | if (!PageCgroupUsed(pc)) |
| 2999 | goto unlock_out; |
| 3000 | |
| 3001 | switch (ctype) { |
| 3002 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3003 | case MEM_CGROUP_CHARGE_TYPE_DROP: |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3004 | /* See mem_cgroup_prepare_migration() */ |
| 3005 | if (page_mapped(page) || PageCgroupMigration(pc)) |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3006 | goto unlock_out; |
| 3007 | break; |
| 3008 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: |
| 3009 | if (!PageAnon(page)) { /* Shared memory */ |
| 3010 | if (page->mapping && !page_is_file_cache(page)) |
| 3011 | goto unlock_out; |
| 3012 | } else if (page_mapped(page)) /* Anon */ |
| 3013 | goto unlock_out; |
| 3014 | break; |
| 3015 | default: |
| 3016 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3017 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3018 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3019 | mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), -nr_pages); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 3020 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3021 | ClearPageCgroupUsed(pc); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 3022 | /* |
| 3023 | * pc->mem_cgroup is not cleared here. It will be accessed when it's |
| 3024 | * freed from LRU. This is safe because uncharged page is expected not |
| 3025 | * to be reused (freed soon). Exception is SwapCache, it's handled by |
| 3026 | * special functions. |
| 3027 | */ |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 3028 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3029 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3030 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3031 | * even after unlock, we have memcg->res.usage here and this memcg |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3032 | * will never be freed. |
| 3033 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3034 | memcg_check_events(memcg, page); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3035 | if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3036 | mem_cgroup_swap_statistics(memcg, true); |
| 3037 | mem_cgroup_get(memcg); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3038 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3039 | if (!mem_cgroup_is_root(memcg)) |
| 3040 | mem_cgroup_do_uncharge(memcg, nr_pages, ctype); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 3041 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3042 | return memcg; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3043 | |
| 3044 | unlock_out: |
| 3045 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3046 | return NULL; |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 3047 | } |
| 3048 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3049 | void mem_cgroup_uncharge_page(struct page *page) |
| 3050 | { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3051 | /* early check. */ |
| 3052 | if (page_mapped(page)) |
| 3053 | return; |
| 3054 | if (page->mapping && !PageAnon(page)) |
| 3055 | return; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3056 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 3057 | } |
| 3058 | |
| 3059 | void mem_cgroup_uncharge_cache_page(struct page *page) |
| 3060 | { |
| 3061 | VM_BUG_ON(page_mapped(page)); |
KAMEZAWA Hiroyuki | b7abea9 | 2008-10-18 20:28:09 -0700 | [diff] [blame] | 3062 | VM_BUG_ON(page->mapping); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3063 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 3064 | } |
| 3065 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3066 | /* |
| 3067 | * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate. |
| 3068 | * In that cases, pages are freed continuously and we can expect pages |
| 3069 | * are in the same memcg. All these calls itself limits the number of |
| 3070 | * pages freed at once, then uncharge_start/end() is called properly. |
| 3071 | * This may be called prural(2) times in a context, |
| 3072 | */ |
| 3073 | |
| 3074 | void mem_cgroup_uncharge_start(void) |
| 3075 | { |
| 3076 | current->memcg_batch.do_batch++; |
| 3077 | /* We can do nest. */ |
| 3078 | if (current->memcg_batch.do_batch == 1) { |
| 3079 | current->memcg_batch.memcg = NULL; |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3080 | current->memcg_batch.nr_pages = 0; |
| 3081 | current->memcg_batch.memsw_nr_pages = 0; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3082 | } |
| 3083 | } |
| 3084 | |
| 3085 | void mem_cgroup_uncharge_end(void) |
| 3086 | { |
| 3087 | struct memcg_batch_info *batch = ¤t->memcg_batch; |
| 3088 | |
| 3089 | if (!batch->do_batch) |
| 3090 | return; |
| 3091 | |
| 3092 | batch->do_batch--; |
| 3093 | if (batch->do_batch) /* If stacked, do nothing. */ |
| 3094 | return; |
| 3095 | |
| 3096 | if (!batch->memcg) |
| 3097 | return; |
| 3098 | /* |
| 3099 | * This "batch->memcg" is valid without any css_get/put etc... |
| 3100 | * bacause we hide charges behind us. |
| 3101 | */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3102 | if (batch->nr_pages) |
| 3103 | res_counter_uncharge(&batch->memcg->res, |
| 3104 | batch->nr_pages * PAGE_SIZE); |
| 3105 | if (batch->memsw_nr_pages) |
| 3106 | res_counter_uncharge(&batch->memcg->memsw, |
| 3107 | batch->memsw_nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3108 | memcg_oom_recover(batch->memcg); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3109 | /* forget this pointer (for sanity check) */ |
| 3110 | batch->memcg = NULL; |
| 3111 | } |
| 3112 | |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3113 | #ifdef CONFIG_SWAP |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3114 | /* |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3115 | * called after __delete_from_swap_cache() and drop "page" account. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3116 | * memcg information is recorded to swap_cgroup of "ent" |
| 3117 | */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3118 | void |
| 3119 | 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] | 3120 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3121 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3122 | int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3123 | |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3124 | if (!swapout) /* this was a swap cache but the swap is unused ! */ |
| 3125 | ctype = MEM_CGROUP_CHARGE_TYPE_DROP; |
| 3126 | |
| 3127 | memcg = __mem_cgroup_uncharge_common(page, ctype); |
| 3128 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3129 | /* |
| 3130 | * record memcg information, if swapout && memcg != NULL, |
| 3131 | * mem_cgroup_get() was called in uncharge(). |
| 3132 | */ |
| 3133 | if (do_swap_account && swapout && memcg) |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3134 | swap_cgroup_record(ent, css_id(&memcg->css)); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3135 | } |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3136 | #endif |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3137 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3138 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 3139 | /* |
| 3140 | * called from swap_entry_free(). remove record in swap_cgroup and |
| 3141 | * uncharge "memsw" account. |
| 3142 | */ |
| 3143 | void mem_cgroup_uncharge_swap(swp_entry_t ent) |
| 3144 | { |
| 3145 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3146 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3147 | |
| 3148 | if (!do_swap_account) |
| 3149 | return; |
| 3150 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3151 | id = swap_cgroup_record(ent, 0); |
| 3152 | rcu_read_lock(); |
| 3153 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3154 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3155 | /* |
| 3156 | * We uncharge this because swap is freed. |
| 3157 | * This memcg can be obsolete one. We avoid calling css_tryget |
| 3158 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3159 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 3160 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3161 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3162 | mem_cgroup_put(memcg); |
| 3163 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3164 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3165 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3166 | |
| 3167 | /** |
| 3168 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. |
| 3169 | * @entry: swap entry to be moved |
| 3170 | * @from: mem_cgroup which the entry is moved from |
| 3171 | * @to: mem_cgroup which the entry is moved to |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3172 | * @need_fixup: whether we should fixup res_counters and refcounts. |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3173 | * |
| 3174 | * It succeeds only when the swap_cgroup's record for this entry is the same |
| 3175 | * as the mem_cgroup's id of @from. |
| 3176 | * |
| 3177 | * Returns 0 on success, -EINVAL on failure. |
| 3178 | * |
| 3179 | * The caller must have charged to @to, IOW, called res_counter_charge() about |
| 3180 | * both res and memsw, and called css_get(). |
| 3181 | */ |
| 3182 | static int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3183 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3184 | { |
| 3185 | unsigned short old_id, new_id; |
| 3186 | |
| 3187 | old_id = css_id(&from->css); |
| 3188 | new_id = css_id(&to->css); |
| 3189 | |
| 3190 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3191 | mem_cgroup_swap_statistics(from, false); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3192 | mem_cgroup_swap_statistics(to, true); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3193 | /* |
| 3194 | * This function is only called from task migration context now. |
| 3195 | * It postpones res_counter and refcount handling till the end |
| 3196 | * of task migration(mem_cgroup_clear_mc()) for performance |
| 3197 | * improvement. But we cannot postpone mem_cgroup_get(to) |
| 3198 | * because if the process that has been moved to @to does |
| 3199 | * swap-in, the refcount of @to might be decreased to 0. |
| 3200 | */ |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3201 | mem_cgroup_get(to); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3202 | if (need_fixup) { |
| 3203 | if (!mem_cgroup_is_root(from)) |
| 3204 | res_counter_uncharge(&from->memsw, PAGE_SIZE); |
| 3205 | mem_cgroup_put(from); |
| 3206 | /* |
| 3207 | * we charged both to->res and to->memsw, so we should |
| 3208 | * uncharge to->res. |
| 3209 | */ |
| 3210 | if (!mem_cgroup_is_root(to)) |
| 3211 | res_counter_uncharge(&to->res, PAGE_SIZE); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3212 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3213 | return 0; |
| 3214 | } |
| 3215 | return -EINVAL; |
| 3216 | } |
| 3217 | #else |
| 3218 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3219 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3220 | { |
| 3221 | return -EINVAL; |
| 3222 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3223 | #endif |
| 3224 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3225 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3226 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
| 3227 | * page belongs to. |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3228 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3229 | int mem_cgroup_prepare_migration(struct page *page, |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 3230 | struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3231 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3232 | struct mem_cgroup *memcg = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3233 | struct page_cgroup *pc; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3234 | enum charge_type ctype; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3235 | int ret = 0; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 3236 | |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 3237 | *ptr = NULL; |
| 3238 | |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 3239 | VM_BUG_ON(PageTransHuge(page)); |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3240 | if (mem_cgroup_disabled()) |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 3241 | return 0; |
| 3242 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3243 | pc = lookup_page_cgroup(page); |
| 3244 | lock_page_cgroup(pc); |
| 3245 | if (PageCgroupUsed(pc)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3246 | memcg = pc->mem_cgroup; |
| 3247 | css_get(&memcg->css); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3248 | /* |
| 3249 | * At migrating an anonymous page, its mapcount goes down |
| 3250 | * to 0 and uncharge() will be called. But, even if it's fully |
| 3251 | * unmapped, migration may fail and this page has to be |
| 3252 | * charged again. We set MIGRATION flag here and delay uncharge |
| 3253 | * until end_migration() is called |
| 3254 | * |
| 3255 | * Corner Case Thinking |
| 3256 | * A) |
| 3257 | * When the old page was mapped as Anon and it's unmap-and-freed |
| 3258 | * while migration was ongoing. |
| 3259 | * If unmap finds the old page, uncharge() of it will be delayed |
| 3260 | * until end_migration(). If unmap finds a new page, it's |
| 3261 | * uncharged when it make mapcount to be 1->0. If unmap code |
| 3262 | * finds swap_migration_entry, the new page will not be mapped |
| 3263 | * and end_migration() will find it(mapcount==0). |
| 3264 | * |
| 3265 | * B) |
| 3266 | * When the old page was mapped but migraion fails, the kernel |
| 3267 | * remaps it. A charge for it is kept by MIGRATION flag even |
| 3268 | * if mapcount goes down to 0. We can do remap successfully |
| 3269 | * without charging it again. |
| 3270 | * |
| 3271 | * C) |
| 3272 | * The "old" page is under lock_page() until the end of |
| 3273 | * migration, so, the old page itself will not be swapped-out. |
| 3274 | * If the new page is swapped out before end_migraton, our |
| 3275 | * hook to usual swap-out path will catch the event. |
| 3276 | */ |
| 3277 | if (PageAnon(page)) |
| 3278 | SetPageCgroupMigration(pc); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 3279 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3280 | unlock_page_cgroup(pc); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3281 | /* |
| 3282 | * If the page is not charged at this point, |
| 3283 | * we return here. |
| 3284 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3285 | if (!memcg) |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3286 | return 0; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3287 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3288 | *ptr = memcg; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3289 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3290 | css_put(&memcg->css);/* drop extra refcnt */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3291 | if (ret || *ptr == NULL) { |
| 3292 | if (PageAnon(page)) { |
| 3293 | lock_page_cgroup(pc); |
| 3294 | ClearPageCgroupMigration(pc); |
| 3295 | unlock_page_cgroup(pc); |
| 3296 | /* |
| 3297 | * The old page may be fully unmapped while we kept it. |
| 3298 | */ |
| 3299 | mem_cgroup_uncharge_page(page); |
| 3300 | } |
| 3301 | return -ENOMEM; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3302 | } |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3303 | /* |
| 3304 | * We charge new page before it's used/mapped. So, even if unlock_page() |
| 3305 | * is called before end_migration, we can catch all events on this new |
| 3306 | * page. In the case new page is migrated but not remapped, new page's |
| 3307 | * mapcount will be finally 0 and we call uncharge in end_migration(). |
| 3308 | */ |
| 3309 | pc = lookup_page_cgroup(newpage); |
| 3310 | if (PageAnon(page)) |
| 3311 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; |
| 3312 | else if (page_is_file_cache(page)) |
| 3313 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; |
| 3314 | else |
| 3315 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3316 | __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3317 | return ret; |
| 3318 | } |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 3319 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3320 | /* remove redundant charge if migration failed*/ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3321 | void mem_cgroup_end_migration(struct mem_cgroup *memcg, |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 3322 | struct page *oldpage, struct page *newpage, bool migration_ok) |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3323 | { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3324 | struct page *used, *unused; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3325 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3326 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3327 | if (!memcg) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3328 | return; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3329 | /* blocks rmdir() */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3330 | cgroup_exclude_rmdir(&memcg->css); |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 3331 | if (!migration_ok) { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3332 | used = oldpage; |
| 3333 | unused = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3334 | } else { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3335 | used = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3336 | unused = oldpage; |
| 3337 | } |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3338 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3339 | * We disallowed uncharge of pages under migration because mapcount |
| 3340 | * of the page goes down to zero, temporarly. |
| 3341 | * Clear the flag and check the page should be charged. |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3342 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3343 | pc = lookup_page_cgroup(oldpage); |
| 3344 | lock_page_cgroup(pc); |
| 3345 | ClearPageCgroupMigration(pc); |
| 3346 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3347 | |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3348 | __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE); |
| 3349 | |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3350 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3351 | * If a page is a file cache, radix-tree replacement is very atomic |
| 3352 | * and we can skip this check. When it was an Anon page, its mapcount |
| 3353 | * goes down to 0. But because we added MIGRATION flage, it's not |
| 3354 | * uncharged yet. There are several case but page->mapcount check |
| 3355 | * and USED bit check in mem_cgroup_uncharge_page() will do enough |
| 3356 | * check. (see prepare_charge() also) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3357 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3358 | if (PageAnon(used)) |
| 3359 | mem_cgroup_uncharge_page(used); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3360 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3361 | * At migration, we may charge account against cgroup which has no |
| 3362 | * tasks. |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3363 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 3364 | * In that case, we need to call pre_destroy() again. check it here. |
| 3365 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3366 | cgroup_release_and_wakeup_rmdir(&memcg->css); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3367 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 3368 | |
Daisuke Nishimura | f212ad7 | 2011-03-23 16:42:25 -0700 | [diff] [blame] | 3369 | #ifdef CONFIG_DEBUG_VM |
| 3370 | static struct page_cgroup *lookup_page_cgroup_used(struct page *page) |
| 3371 | { |
| 3372 | struct page_cgroup *pc; |
| 3373 | |
| 3374 | pc = lookup_page_cgroup(page); |
| 3375 | if (likely(pc) && PageCgroupUsed(pc)) |
| 3376 | return pc; |
| 3377 | return NULL; |
| 3378 | } |
| 3379 | |
| 3380 | bool mem_cgroup_bad_page_check(struct page *page) |
| 3381 | { |
| 3382 | if (mem_cgroup_disabled()) |
| 3383 | return false; |
| 3384 | |
| 3385 | return lookup_page_cgroup_used(page) != NULL; |
| 3386 | } |
| 3387 | |
| 3388 | void mem_cgroup_print_bad_page(struct page *page) |
| 3389 | { |
| 3390 | struct page_cgroup *pc; |
| 3391 | |
| 3392 | pc = lookup_page_cgroup_used(page); |
| 3393 | if (pc) { |
| 3394 | int ret = -1; |
| 3395 | char *path; |
| 3396 | |
| 3397 | printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p", |
| 3398 | pc, pc->flags, pc->mem_cgroup); |
| 3399 | |
| 3400 | path = kmalloc(PATH_MAX, GFP_KERNEL); |
| 3401 | if (path) { |
| 3402 | rcu_read_lock(); |
| 3403 | ret = cgroup_path(pc->mem_cgroup->css.cgroup, |
| 3404 | path, PATH_MAX); |
| 3405 | rcu_read_unlock(); |
| 3406 | } |
| 3407 | |
| 3408 | printk(KERN_CONT "(%s)\n", |
| 3409 | (ret < 0) ? "cannot get the path" : path); |
| 3410 | kfree(path); |
| 3411 | } |
| 3412 | } |
| 3413 | #endif |
| 3414 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3415 | static DEFINE_MUTEX(set_limit_mutex); |
| 3416 | |
KOSAKI Motohiro | d38d2a7 | 2009-01-06 14:39:44 -0800 | [diff] [blame] | 3417 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3418 | unsigned long long val) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3419 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3420 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3421 | u64 memswlimit, memlimit; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3422 | int ret = 0; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3423 | int children = mem_cgroup_count_children(memcg); |
| 3424 | u64 curusage, oldusage; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3425 | int enlarge; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3426 | |
| 3427 | /* |
| 3428 | * For keeping hierarchical_reclaim simple, how long we should retry |
| 3429 | * is depends on callers. We set our retry-count to be function |
| 3430 | * of # of children which we should visit in this loop. |
| 3431 | */ |
| 3432 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; |
| 3433 | |
| 3434 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3435 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3436 | enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3437 | while (retry_count) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3438 | if (signal_pending(current)) { |
| 3439 | ret = -EINTR; |
| 3440 | break; |
| 3441 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3442 | /* |
| 3443 | * Rather than hide all in some function, I do this in |
| 3444 | * open coded manner. You see what this really does. |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3445 | * We have to guarantee memcg->res.limit < memcg->memsw.limit. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3446 | */ |
| 3447 | mutex_lock(&set_limit_mutex); |
| 3448 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3449 | if (memswlimit < val) { |
| 3450 | ret = -EINVAL; |
| 3451 | mutex_unlock(&set_limit_mutex); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3452 | break; |
| 3453 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3454 | |
| 3455 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3456 | if (memlimit < val) |
| 3457 | enlarge = 1; |
| 3458 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3459 | ret = res_counter_set_limit(&memcg->res, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 3460 | if (!ret) { |
| 3461 | if (memswlimit == val) |
| 3462 | memcg->memsw_is_minimum = true; |
| 3463 | else |
| 3464 | memcg->memsw_is_minimum = false; |
| 3465 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3466 | mutex_unlock(&set_limit_mutex); |
| 3467 | |
| 3468 | if (!ret) |
| 3469 | break; |
| 3470 | |
Bob Liu | aa20d48 | 2009-12-15 16:47:14 -0800 | [diff] [blame] | 3471 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3472 | MEM_CGROUP_RECLAIM_SHRINK, |
| 3473 | NULL); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3474 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
| 3475 | /* Usage is reduced ? */ |
| 3476 | if (curusage >= oldusage) |
| 3477 | retry_count--; |
| 3478 | else |
| 3479 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3480 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3481 | if (!ret && enlarge) |
| 3482 | memcg_oom_recover(memcg); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 3483 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3484 | return ret; |
| 3485 | } |
| 3486 | |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 3487 | static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
| 3488 | unsigned long long val) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3489 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3490 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3491 | u64 memlimit, memswlimit, oldusage, curusage; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3492 | int children = mem_cgroup_count_children(memcg); |
| 3493 | int ret = -EBUSY; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3494 | int enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3495 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3496 | /* see mem_cgroup_resize_res_limit */ |
| 3497 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; |
| 3498 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3499 | while (retry_count) { |
| 3500 | if (signal_pending(current)) { |
| 3501 | ret = -EINTR; |
| 3502 | break; |
| 3503 | } |
| 3504 | /* |
| 3505 | * Rather than hide all in some function, I do this in |
| 3506 | * open coded manner. You see what this really does. |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3507 | * We have to guarantee memcg->res.limit < memcg->memsw.limit. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3508 | */ |
| 3509 | mutex_lock(&set_limit_mutex); |
| 3510 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3511 | if (memlimit > val) { |
| 3512 | ret = -EINVAL; |
| 3513 | mutex_unlock(&set_limit_mutex); |
| 3514 | break; |
| 3515 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3516 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3517 | if (memswlimit < val) |
| 3518 | enlarge = 1; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3519 | ret = res_counter_set_limit(&memcg->memsw, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 3520 | if (!ret) { |
| 3521 | if (memlimit == val) |
| 3522 | memcg->memsw_is_minimum = true; |
| 3523 | else |
| 3524 | memcg->memsw_is_minimum = false; |
| 3525 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3526 | mutex_unlock(&set_limit_mutex); |
| 3527 | |
| 3528 | if (!ret) |
| 3529 | break; |
| 3530 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3531 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 3532 | MEM_CGROUP_RECLAIM_NOSWAP | |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3533 | MEM_CGROUP_RECLAIM_SHRINK, |
| 3534 | NULL); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3535 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3536 | /* Usage is reduced ? */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3537 | if (curusage >= oldusage) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3538 | retry_count--; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3539 | else |
| 3540 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3541 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3542 | if (!ret && enlarge) |
| 3543 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3544 | return ret; |
| 3545 | } |
| 3546 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3547 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3548 | gfp_t gfp_mask, |
| 3549 | unsigned long *total_scanned) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3550 | { |
| 3551 | unsigned long nr_reclaimed = 0; |
| 3552 | struct mem_cgroup_per_zone *mz, *next_mz = NULL; |
| 3553 | unsigned long reclaimed; |
| 3554 | int loop = 0; |
| 3555 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3556 | unsigned long long excess; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3557 | unsigned long nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3558 | |
| 3559 | if (order > 0) |
| 3560 | return 0; |
| 3561 | |
KOSAKI Motohiro | 00918b6 | 2010-08-10 18:03:05 -0700 | [diff] [blame] | 3562 | 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] | 3563 | /* |
| 3564 | * This loop can run a while, specially if mem_cgroup's continuously |
| 3565 | * keep exceeding their soft limit and putting the system under |
| 3566 | * pressure |
| 3567 | */ |
| 3568 | do { |
| 3569 | if (next_mz) |
| 3570 | mz = next_mz; |
| 3571 | else |
| 3572 | mz = mem_cgroup_largest_soft_limit_node(mctz); |
| 3573 | if (!mz) |
| 3574 | break; |
| 3575 | |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3576 | nr_scanned = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3577 | reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone, |
| 3578 | gfp_mask, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3579 | MEM_CGROUP_RECLAIM_SOFT, |
| 3580 | &nr_scanned); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3581 | nr_reclaimed += reclaimed; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3582 | *total_scanned += nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3583 | spin_lock(&mctz->lock); |
| 3584 | |
| 3585 | /* |
| 3586 | * If we failed to reclaim anything from this memory cgroup |
| 3587 | * it is time to move on to the next cgroup |
| 3588 | */ |
| 3589 | next_mz = NULL; |
| 3590 | if (!reclaimed) { |
| 3591 | do { |
| 3592 | /* |
| 3593 | * Loop until we find yet another one. |
| 3594 | * |
| 3595 | * By the time we get the soft_limit lock |
| 3596 | * again, someone might have aded the |
| 3597 | * group back on the RB tree. Iterate to |
| 3598 | * make sure we get a different mem. |
| 3599 | * mem_cgroup_largest_soft_limit_node returns |
| 3600 | * NULL if no other cgroup is present on |
| 3601 | * the tree |
| 3602 | */ |
| 3603 | next_mz = |
| 3604 | __mem_cgroup_largest_soft_limit_node(mctz); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 3605 | if (next_mz == mz) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3606 | css_put(&next_mz->mem->css); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 3607 | else /* next_mz == NULL or other memcg */ |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3608 | break; |
| 3609 | } while (1); |
| 3610 | } |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3611 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3612 | excess = res_counter_soft_limit_excess(&mz->mem->res); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3613 | /* |
| 3614 | * One school of thought says that we should not add |
| 3615 | * back the node to the tree if reclaim returns 0. |
| 3616 | * But our reclaim could return 0, simply because due |
| 3617 | * to priority we are exposing a smaller subset of |
| 3618 | * memory to reclaim from. Consider this as a longer |
| 3619 | * term TODO. |
| 3620 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3621 | /* If excess == 0, no tree ops */ |
| 3622 | __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3623 | spin_unlock(&mctz->lock); |
| 3624 | css_put(&mz->mem->css); |
| 3625 | loop++; |
| 3626 | /* |
| 3627 | * Could not reclaim anything and there are no more |
| 3628 | * mem cgroups to try or we seem to be looping without |
| 3629 | * reclaiming anything. |
| 3630 | */ |
| 3631 | if (!nr_reclaimed && |
| 3632 | (next_mz == NULL || |
| 3633 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) |
| 3634 | break; |
| 3635 | } while (!nr_reclaimed); |
| 3636 | if (next_mz) |
| 3637 | css_put(&next_mz->mem->css); |
| 3638 | return nr_reclaimed; |
| 3639 | } |
| 3640 | |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3641 | /* |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3642 | * This routine traverse page_cgroup in given list and drop them all. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3643 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
| 3644 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3645 | static int mem_cgroup_force_empty_list(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3646 | int node, int zid, enum lru_list lru) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3647 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3648 | struct zone *zone; |
| 3649 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3650 | struct page_cgroup *pc, *busy; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3651 | unsigned long flags, loop; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3652 | struct list_head *list; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3653 | int ret = 0; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3654 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3655 | zone = &NODE_DATA(node)->node_zones[zid]; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3656 | mz = mem_cgroup_zoneinfo(memcg, node, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3657 | list = &mz->lists[lru]; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3658 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3659 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
| 3660 | /* give some margin against EBUSY etc...*/ |
| 3661 | loop += 256; |
| 3662 | busy = NULL; |
| 3663 | while (loop--) { |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3664 | struct page *page; |
| 3665 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3666 | ret = 0; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3667 | spin_lock_irqsave(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3668 | if (list_empty(list)) { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3669 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3670 | break; |
| 3671 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3672 | pc = list_entry(list->prev, struct page_cgroup, lru); |
| 3673 | if (busy == pc) { |
| 3674 | list_move(&pc->lru, list); |
Thiago Farina | 648bcc7 | 2010-03-05 13:42:04 -0800 | [diff] [blame] | 3675 | busy = NULL; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3676 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3677 | continue; |
| 3678 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3679 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3680 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 3681 | page = lookup_cgroup_page(pc); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3682 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3683 | ret = mem_cgroup_move_parent(page, pc, memcg, GFP_KERNEL); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3684 | if (ret == -ENOMEM) |
| 3685 | break; |
| 3686 | |
| 3687 | if (ret == -EBUSY || ret == -EINVAL) { |
| 3688 | /* found lock contention or "pc" is obsolete. */ |
| 3689 | busy = pc; |
| 3690 | cond_resched(); |
| 3691 | } else |
| 3692 | busy = NULL; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3693 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3694 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3695 | if (!ret && !list_empty(list)) |
| 3696 | return -EBUSY; |
| 3697 | return ret; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | /* |
| 3701 | * make mem_cgroup's charge to be 0 if there is no task. |
| 3702 | * This enables deleting this mem_cgroup. |
| 3703 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3704 | 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] | 3705 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3706 | int ret; |
| 3707 | int node, zid, shrink; |
| 3708 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3709 | struct cgroup *cgrp = memcg->css.cgroup; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 3710 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3711 | css_get(&memcg->css); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3712 | |
| 3713 | shrink = 0; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3714 | /* should free all ? */ |
| 3715 | if (free_all) |
| 3716 | goto try_to_free; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3717 | move_account: |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3718 | do { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3719 | ret = -EBUSY; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3720 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3721 | goto out; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3722 | ret = -EINTR; |
| 3723 | if (signal_pending(current)) |
| 3724 | goto out; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3725 | /* This is for making all *used* pages to be on LRU. */ |
| 3726 | lru_add_drain_all(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3727 | drain_all_stock_sync(memcg); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3728 | ret = 0; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3729 | mem_cgroup_start_move(memcg); |
KAMEZAWA Hiroyuki | 299b4ea | 2009-01-29 14:25:17 -0800 | [diff] [blame] | 3730 | for_each_node_state(node, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3731 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3732 | enum lru_list l; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3733 | for_each_lru(l) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3734 | ret = mem_cgroup_force_empty_list(memcg, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3735 | node, zid, l); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3736 | if (ret) |
| 3737 | break; |
| 3738 | } |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 3739 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3740 | if (ret) |
| 3741 | break; |
| 3742 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3743 | mem_cgroup_end_move(memcg); |
| 3744 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3745 | /* it seems parent cgroup doesn't have enough mem */ |
| 3746 | if (ret == -ENOMEM) |
| 3747 | goto try_to_free; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3748 | cond_resched(); |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3749 | /* "ret" should also be checked to ensure all lists are empty. */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3750 | } while (memcg->res.usage > 0 || ret); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3751 | out: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3752 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3753 | return ret; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3754 | |
| 3755 | try_to_free: |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3756 | /* returns EBUSY if there is a task or if we come here twice. */ |
| 3757 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3758 | ret = -EBUSY; |
| 3759 | goto out; |
| 3760 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3761 | /* we call try-to-free pages for make this cgroup empty */ |
| 3762 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3763 | /* try to free all pages in this cgroup */ |
| 3764 | shrink = 1; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3765 | while (nr_retries && memcg->res.usage > 0) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3766 | int progress; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3767 | |
| 3768 | if (signal_pending(current)) { |
| 3769 | ret = -EINTR; |
| 3770 | goto out; |
| 3771 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3772 | progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL, |
Johannes Weiner | 185efc0 | 2011-09-14 16:21:58 -0700 | [diff] [blame] | 3773 | false); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3774 | if (!progress) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3775 | nr_retries--; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3776 | /* maybe some writeback is necessary */ |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 3777 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3778 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3779 | |
| 3780 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3781 | lru_add_drain(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3782 | /* try move_account...there may be some *locked* pages. */ |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3783 | goto move_account; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3784 | } |
| 3785 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3786 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
| 3787 | { |
| 3788 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); |
| 3789 | } |
| 3790 | |
| 3791 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3792 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
| 3793 | { |
| 3794 | return mem_cgroup_from_cont(cont)->use_hierarchy; |
| 3795 | } |
| 3796 | |
| 3797 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, |
| 3798 | u64 val) |
| 3799 | { |
| 3800 | int retval = 0; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3801 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3802 | struct cgroup *parent = cont->parent; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3803 | struct mem_cgroup *parent_memcg = NULL; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3804 | |
| 3805 | if (parent) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3806 | parent_memcg = mem_cgroup_from_cont(parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3807 | |
| 3808 | cgroup_lock(); |
| 3809 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 3810 | * 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] | 3811 | * in the child subtrees. If it is unset, then the change can |
| 3812 | * occur, provided the current cgroup has no children. |
| 3813 | * |
| 3814 | * For the root cgroup, parent_mem is NULL, we allow value to be |
| 3815 | * set if there are no children. |
| 3816 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3817 | if ((!parent_memcg || !parent_memcg->use_hierarchy) && |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3818 | (val == 1 || val == 0)) { |
| 3819 | if (list_empty(&cont->children)) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3820 | memcg->use_hierarchy = val; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3821 | else |
| 3822 | retval = -EBUSY; |
| 3823 | } else |
| 3824 | retval = -EINVAL; |
| 3825 | cgroup_unlock(); |
| 3826 | |
| 3827 | return retval; |
| 3828 | } |
| 3829 | |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3830 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3831 | static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg, |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3832 | enum mem_cgroup_stat_index idx) |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3833 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3834 | struct mem_cgroup *iter; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3835 | long val = 0; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3836 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3837 | /* Per-cpu values can be negative, use a signed accumulator */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3838 | for_each_mem_cgroup_tree(iter, memcg) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3839 | val += mem_cgroup_read_stat(iter, idx); |
| 3840 | |
| 3841 | if (val < 0) /* race ? */ |
| 3842 | val = 0; |
| 3843 | return val; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3844 | } |
| 3845 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3846 | 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] | 3847 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3848 | u64 val; |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3849 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3850 | if (!mem_cgroup_is_root(memcg)) { |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3851 | if (!swap) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3852 | return res_counter_read_u64(&memcg->res, RES_USAGE); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3853 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3854 | return res_counter_read_u64(&memcg->memsw, RES_USAGE); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3855 | } |
| 3856 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3857 | val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE); |
| 3858 | val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3859 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3860 | if (swap) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3861 | val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAPOUT); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3862 | |
| 3863 | return val << PAGE_SHIFT; |
| 3864 | } |
| 3865 | |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 3866 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3867 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3868 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3869 | u64 val; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3870 | int type, name; |
| 3871 | |
| 3872 | type = MEMFILE_TYPE(cft->private); |
| 3873 | name = MEMFILE_ATTR(cft->private); |
| 3874 | switch (type) { |
| 3875 | case _MEM: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3876 | if (name == RES_USAGE) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3877 | val = mem_cgroup_usage(memcg, false); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3878 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3879 | val = res_counter_read_u64(&memcg->res, name); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3880 | break; |
| 3881 | case _MEMSWAP: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3882 | if (name == RES_USAGE) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3883 | val = mem_cgroup_usage(memcg, true); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3884 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3885 | val = res_counter_read_u64(&memcg->memsw, name); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3886 | break; |
| 3887 | default: |
| 3888 | BUG(); |
| 3889 | break; |
| 3890 | } |
| 3891 | return val; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3892 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3893 | /* |
| 3894 | * The user of this function is... |
| 3895 | * RES_LIMIT. |
| 3896 | */ |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 3897 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 3898 | const char *buffer) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3899 | { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3900 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3901 | int type, name; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3902 | unsigned long long val; |
| 3903 | int ret; |
| 3904 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3905 | type = MEMFILE_TYPE(cft->private); |
| 3906 | name = MEMFILE_ATTR(cft->private); |
| 3907 | switch (name) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3908 | case RES_LIMIT: |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 3909 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
| 3910 | ret = -EINVAL; |
| 3911 | break; |
| 3912 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3913 | /* This function does all necessary parse...reuse it */ |
| 3914 | ret = res_counter_memparse_write_strategy(buffer, &val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3915 | if (ret) |
| 3916 | break; |
| 3917 | if (type == _MEM) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3918 | ret = mem_cgroup_resize_limit(memcg, val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3919 | else |
| 3920 | ret = mem_cgroup_resize_memsw_limit(memcg, val); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3921 | break; |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 3922 | case RES_SOFT_LIMIT: |
| 3923 | ret = res_counter_memparse_write_strategy(buffer, &val); |
| 3924 | if (ret) |
| 3925 | break; |
| 3926 | /* |
| 3927 | * For memsw, soft limits are hard to implement in terms |
| 3928 | * of semantics, for now, we support soft limits for |
| 3929 | * control without swap |
| 3930 | */ |
| 3931 | if (type == _MEM) |
| 3932 | ret = res_counter_set_soft_limit(&memcg->res, val); |
| 3933 | else |
| 3934 | ret = -EINVAL; |
| 3935 | break; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3936 | default: |
| 3937 | ret = -EINVAL; /* should be BUG() ? */ |
| 3938 | break; |
| 3939 | } |
| 3940 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3941 | } |
| 3942 | |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 3943 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
| 3944 | unsigned long long *mem_limit, unsigned long long *memsw_limit) |
| 3945 | { |
| 3946 | struct cgroup *cgroup; |
| 3947 | unsigned long long min_limit, min_memsw_limit, tmp; |
| 3948 | |
| 3949 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3950 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3951 | cgroup = memcg->css.cgroup; |
| 3952 | if (!memcg->use_hierarchy) |
| 3953 | goto out; |
| 3954 | |
| 3955 | while (cgroup->parent) { |
| 3956 | cgroup = cgroup->parent; |
| 3957 | memcg = mem_cgroup_from_cont(cgroup); |
| 3958 | if (!memcg->use_hierarchy) |
| 3959 | break; |
| 3960 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3961 | min_limit = min(min_limit, tmp); |
| 3962 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3963 | min_memsw_limit = min(min_memsw_limit, tmp); |
| 3964 | } |
| 3965 | out: |
| 3966 | *mem_limit = min_limit; |
| 3967 | *memsw_limit = min_memsw_limit; |
| 3968 | return; |
| 3969 | } |
| 3970 | |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 3971 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 3972 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3973 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3974 | int type, name; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 3975 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3976 | memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3977 | type = MEMFILE_TYPE(event); |
| 3978 | name = MEMFILE_ATTR(event); |
| 3979 | switch (name) { |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 3980 | case RES_MAX_USAGE: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3981 | if (type == _MEM) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3982 | res_counter_reset_max(&memcg->res); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3983 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3984 | res_counter_reset_max(&memcg->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 3985 | break; |
| 3986 | case RES_FAILCNT: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3987 | if (type == _MEM) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3988 | res_counter_reset_failcnt(&memcg->res); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3989 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3990 | res_counter_reset_failcnt(&memcg->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 3991 | break; |
| 3992 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 3993 | |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 3994 | return 0; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 3995 | } |
| 3996 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 3997 | static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp, |
| 3998 | struct cftype *cft) |
| 3999 | { |
| 4000 | return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate; |
| 4001 | } |
| 4002 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4003 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4004 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 4005 | struct cftype *cft, u64 val) |
| 4006 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4007 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4008 | |
| 4009 | if (val >= (1 << NR_MOVE_TYPE)) |
| 4010 | return -EINVAL; |
| 4011 | /* |
| 4012 | * We check this value several times in both in can_attach() and |
| 4013 | * attach(), so we need cgroup lock to prevent this value from being |
| 4014 | * inconsistent. |
| 4015 | */ |
| 4016 | cgroup_lock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4017 | memcg->move_charge_at_immigrate = val; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4018 | cgroup_unlock(); |
| 4019 | |
| 4020 | return 0; |
| 4021 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4022 | #else |
| 4023 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 4024 | struct cftype *cft, u64 val) |
| 4025 | { |
| 4026 | return -ENOSYS; |
| 4027 | } |
| 4028 | #endif |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4029 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4030 | |
| 4031 | /* For read statistics */ |
| 4032 | enum { |
| 4033 | MCS_CACHE, |
| 4034 | MCS_RSS, |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 4035 | MCS_FILE_MAPPED, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4036 | MCS_PGPGIN, |
| 4037 | MCS_PGPGOUT, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4038 | MCS_SWAP, |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4039 | MCS_PGFAULT, |
| 4040 | MCS_PGMAJFAULT, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4041 | MCS_INACTIVE_ANON, |
| 4042 | MCS_ACTIVE_ANON, |
| 4043 | MCS_INACTIVE_FILE, |
| 4044 | MCS_ACTIVE_FILE, |
| 4045 | MCS_UNEVICTABLE, |
| 4046 | NR_MCS_STAT, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4047 | }; |
| 4048 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4049 | struct mcs_total_stat { |
| 4050 | s64 stat[NR_MCS_STAT]; |
| 4051 | }; |
| 4052 | |
| 4053 | struct { |
| 4054 | char *local_name; |
| 4055 | char *total_name; |
| 4056 | } memcg_stat_strings[NR_MCS_STAT] = { |
| 4057 | {"cache", "total_cache"}, |
| 4058 | {"rss", "total_rss"}, |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 4059 | {"mapped_file", "total_mapped_file"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4060 | {"pgpgin", "total_pgpgin"}, |
| 4061 | {"pgpgout", "total_pgpgout"}, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4062 | {"swap", "total_swap"}, |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4063 | {"pgfault", "total_pgfault"}, |
| 4064 | {"pgmajfault", "total_pgmajfault"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4065 | {"inactive_anon", "total_inactive_anon"}, |
| 4066 | {"active_anon", "total_active_anon"}, |
| 4067 | {"inactive_file", "total_inactive_file"}, |
| 4068 | {"active_file", "total_active_file"}, |
| 4069 | {"unevictable", "total_unevictable"} |
| 4070 | }; |
| 4071 | |
| 4072 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4073 | static void |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4074 | 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] | 4075 | { |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4076 | s64 val; |
| 4077 | |
| 4078 | /* per cpu stat */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4079 | val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_CACHE); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4080 | s->stat[MCS_CACHE] += val * PAGE_SIZE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4081 | val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_RSS); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4082 | s->stat[MCS_RSS] += val * PAGE_SIZE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4083 | val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_FILE_MAPPED); |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 4084 | s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4085 | val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGIN); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4086 | s->stat[MCS_PGPGIN] += val; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4087 | val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGOUT); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4088 | s->stat[MCS_PGPGOUT] += val; |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4089 | if (do_swap_account) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4090 | val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_SWAPOUT); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4091 | s->stat[MCS_SWAP] += val * PAGE_SIZE; |
| 4092 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4093 | val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGFAULT); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4094 | s->stat[MCS_PGFAULT] += val; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4095 | val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGMAJFAULT); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4096 | s->stat[MCS_PGMAJFAULT] += val; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4097 | |
| 4098 | /* per zone stat */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4099 | val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4100 | s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4101 | val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4102 | s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4103 | val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4104 | s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4105 | val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4106 | s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4107 | val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4108 | s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4109 | } |
| 4110 | |
| 4111 | static void |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4112 | 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] | 4113 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4114 | struct mem_cgroup *iter; |
| 4115 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4116 | for_each_mem_cgroup_tree(iter, memcg) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4117 | mem_cgroup_get_local_stat(iter, s); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4118 | } |
| 4119 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4120 | #ifdef CONFIG_NUMA |
| 4121 | static int mem_control_numa_stat_show(struct seq_file *m, void *arg) |
| 4122 | { |
| 4123 | int nid; |
| 4124 | unsigned long total_nr, file_nr, anon_nr, unevictable_nr; |
| 4125 | unsigned long node_nr; |
| 4126 | struct cgroup *cont = m->private; |
| 4127 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
| 4128 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4129 | total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4130 | seq_printf(m, "total=%lu", total_nr); |
| 4131 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4132 | 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] | 4133 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4134 | } |
| 4135 | seq_putc(m, '\n'); |
| 4136 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4137 | file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4138 | seq_printf(m, "file=%lu", file_nr); |
| 4139 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4140 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4141 | LRU_ALL_FILE); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4142 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4143 | } |
| 4144 | seq_putc(m, '\n'); |
| 4145 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4146 | anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4147 | seq_printf(m, "anon=%lu", anon_nr); |
| 4148 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4149 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4150 | LRU_ALL_ANON); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4151 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4152 | } |
| 4153 | seq_putc(m, '\n'); |
| 4154 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4155 | unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE)); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4156 | seq_printf(m, "unevictable=%lu", unevictable_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, |
| 4159 | BIT(LRU_UNEVICTABLE)); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4160 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4161 | } |
| 4162 | seq_putc(m, '\n'); |
| 4163 | return 0; |
| 4164 | } |
| 4165 | #endif /* CONFIG_NUMA */ |
| 4166 | |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 4167 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
| 4168 | struct cgroup_map_cb *cb) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4169 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4170 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4171 | struct mcs_total_stat mystat; |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4172 | int i; |
| 4173 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4174 | memset(&mystat, 0, sizeof(mystat)); |
| 4175 | mem_cgroup_get_local_stat(mem_cont, &mystat); |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4176 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4177 | |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4178 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 4179 | if (i == MCS_SWAP && !do_swap_account) |
| 4180 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4181 | cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4182 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4183 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4184 | /* Hierarchical information */ |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 4185 | { |
| 4186 | unsigned long long limit, memsw_limit; |
| 4187 | memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit); |
| 4188 | cb->fill(cb, "hierarchical_memory_limit", limit); |
| 4189 | if (do_swap_account) |
| 4190 | cb->fill(cb, "hierarchical_memsw_limit", memsw_limit); |
| 4191 | } |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4192 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4193 | memset(&mystat, 0, sizeof(mystat)); |
| 4194 | mem_cgroup_get_total_stat(mem_cont, &mystat); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4195 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 4196 | if (i == MCS_SWAP && !do_swap_account) |
| 4197 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4198 | cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4199 | } |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4200 | |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4201 | #ifdef CONFIG_DEBUG_VM |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4202 | { |
| 4203 | int nid, zid; |
| 4204 | struct mem_cgroup_per_zone *mz; |
| 4205 | unsigned long recent_rotated[2] = {0, 0}; |
| 4206 | unsigned long recent_scanned[2] = {0, 0}; |
| 4207 | |
| 4208 | for_each_online_node(nid) |
| 4209 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 4210 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
| 4211 | |
| 4212 | recent_rotated[0] += |
| 4213 | mz->reclaim_stat.recent_rotated[0]; |
| 4214 | recent_rotated[1] += |
| 4215 | mz->reclaim_stat.recent_rotated[1]; |
| 4216 | recent_scanned[0] += |
| 4217 | mz->reclaim_stat.recent_scanned[0]; |
| 4218 | recent_scanned[1] += |
| 4219 | mz->reclaim_stat.recent_scanned[1]; |
| 4220 | } |
| 4221 | cb->fill(cb, "recent_rotated_anon", recent_rotated[0]); |
| 4222 | cb->fill(cb, "recent_rotated_file", recent_rotated[1]); |
| 4223 | cb->fill(cb, "recent_scanned_anon", recent_scanned[0]); |
| 4224 | cb->fill(cb, "recent_scanned_file", recent_scanned[1]); |
| 4225 | } |
| 4226 | #endif |
| 4227 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4228 | return 0; |
| 4229 | } |
| 4230 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4231 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
| 4232 | { |
| 4233 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4234 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 4235 | return mem_cgroup_swappiness(memcg); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, |
| 4239 | u64 val) |
| 4240 | { |
| 4241 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4242 | struct mem_cgroup *parent; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4243 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4244 | if (val > 100) |
| 4245 | return -EINVAL; |
| 4246 | |
| 4247 | if (cgrp->parent == NULL) |
| 4248 | return -EINVAL; |
| 4249 | |
| 4250 | parent = mem_cgroup_from_cont(cgrp->parent); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4251 | |
| 4252 | cgroup_lock(); |
| 4253 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4254 | /* If under hierarchy, only empty-root can set this value */ |
| 4255 | if ((parent->use_hierarchy) || |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4256 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
| 4257 | cgroup_unlock(); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4258 | return -EINVAL; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4259 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4260 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4261 | memcg->swappiness = val; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4262 | |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4263 | cgroup_unlock(); |
| 4264 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4265 | return 0; |
| 4266 | } |
| 4267 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4268 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) |
| 4269 | { |
| 4270 | struct mem_cgroup_threshold_ary *t; |
| 4271 | u64 usage; |
| 4272 | int i; |
| 4273 | |
| 4274 | rcu_read_lock(); |
| 4275 | if (!swap) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4276 | t = rcu_dereference(memcg->thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4277 | else |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4278 | t = rcu_dereference(memcg->memsw_thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4279 | |
| 4280 | if (!t) |
| 4281 | goto unlock; |
| 4282 | |
| 4283 | usage = mem_cgroup_usage(memcg, swap); |
| 4284 | |
| 4285 | /* |
| 4286 | * current_threshold points to threshold just below usage. |
| 4287 | * If it's not true, a threshold was crossed after last |
| 4288 | * call of __mem_cgroup_threshold(). |
| 4289 | */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4290 | i = t->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4291 | |
| 4292 | /* |
| 4293 | * Iterate backward over array of thresholds starting from |
| 4294 | * current_threshold and check if a threshold is crossed. |
| 4295 | * If none of thresholds below usage is crossed, we read |
| 4296 | * only one element of the array here. |
| 4297 | */ |
| 4298 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) |
| 4299 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4300 | |
| 4301 | /* i = current_threshold + 1 */ |
| 4302 | i++; |
| 4303 | |
| 4304 | /* |
| 4305 | * Iterate forward over array of thresholds starting from |
| 4306 | * current_threshold+1 and check if a threshold is crossed. |
| 4307 | * If none of thresholds above usage is crossed, we read |
| 4308 | * only one element of the array here. |
| 4309 | */ |
| 4310 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) |
| 4311 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4312 | |
| 4313 | /* Update current_threshold */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4314 | t->current_threshold = i - 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4315 | unlock: |
| 4316 | rcu_read_unlock(); |
| 4317 | } |
| 4318 | |
| 4319 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) |
| 4320 | { |
Kirill A. Shutemov | ad4ca5f | 2010-10-07 12:59:27 -0700 | [diff] [blame] | 4321 | while (memcg) { |
| 4322 | __mem_cgroup_threshold(memcg, false); |
| 4323 | if (do_swap_account) |
| 4324 | __mem_cgroup_threshold(memcg, true); |
| 4325 | |
| 4326 | memcg = parent_mem_cgroup(memcg); |
| 4327 | } |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4328 | } |
| 4329 | |
| 4330 | static int compare_thresholds(const void *a, const void *b) |
| 4331 | { |
| 4332 | const struct mem_cgroup_threshold *_a = a; |
| 4333 | const struct mem_cgroup_threshold *_b = b; |
| 4334 | |
| 4335 | return _a->threshold - _b->threshold; |
| 4336 | } |
| 4337 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4338 | static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4339 | { |
| 4340 | struct mem_cgroup_eventfd_list *ev; |
| 4341 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4342 | list_for_each_entry(ev, &memcg->oom_notify, list) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4343 | eventfd_signal(ev->eventfd, 1); |
| 4344 | return 0; |
| 4345 | } |
| 4346 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4347 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4348 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4349 | struct mem_cgroup *iter; |
| 4350 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4351 | for_each_mem_cgroup_tree(iter, memcg) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4352 | mem_cgroup_oom_notify_cb(iter); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4353 | } |
| 4354 | |
| 4355 | static int mem_cgroup_usage_register_event(struct cgroup *cgrp, |
| 4356 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4357 | { |
| 4358 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4359 | struct mem_cgroup_thresholds *thresholds; |
| 4360 | struct mem_cgroup_threshold_ary *new; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4361 | int type = MEMFILE_TYPE(cft->private); |
| 4362 | u64 threshold, usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4363 | int i, size, ret; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4364 | |
| 4365 | ret = res_counter_memparse_write_strategy(args, &threshold); |
| 4366 | if (ret) |
| 4367 | return ret; |
| 4368 | |
| 4369 | mutex_lock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4370 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4371 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4372 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4373 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4374 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4375 | else |
| 4376 | BUG(); |
| 4377 | |
| 4378 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 4379 | |
| 4380 | /* Check if a threshold crossed before adding a new one */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4381 | if (thresholds->primary) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4382 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4383 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4384 | size = thresholds->primary ? thresholds->primary->size + 1 : 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4385 | |
| 4386 | /* Allocate memory for new array of thresholds */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4387 | new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4388 | GFP_KERNEL); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4389 | if (!new) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4390 | ret = -ENOMEM; |
| 4391 | goto unlock; |
| 4392 | } |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4393 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4394 | |
| 4395 | /* Copy thresholds (if any) to new array */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4396 | if (thresholds->primary) { |
| 4397 | memcpy(new->entries, thresholds->primary->entries, (size - 1) * |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4398 | sizeof(struct mem_cgroup_threshold)); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4399 | } |
| 4400 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4401 | /* Add new threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4402 | new->entries[size - 1].eventfd = eventfd; |
| 4403 | new->entries[size - 1].threshold = threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4404 | |
| 4405 | /* Sort thresholds. Registering of new threshold isn't time-critical */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4406 | sort(new->entries, size, sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4407 | compare_thresholds, NULL); |
| 4408 | |
| 4409 | /* Find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4410 | new->current_threshold = -1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4411 | for (i = 0; i < size; i++) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4412 | if (new->entries[i].threshold < usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4413 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4414 | * new->current_threshold will not be used until |
| 4415 | * rcu_assign_pointer(), so it's safe to increment |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4416 | * it here. |
| 4417 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4418 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4419 | } |
| 4420 | } |
| 4421 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4422 | /* Free old spare buffer and save old primary buffer as spare */ |
| 4423 | kfree(thresholds->spare); |
| 4424 | thresholds->spare = thresholds->primary; |
| 4425 | |
| 4426 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4427 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4428 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4429 | synchronize_rcu(); |
| 4430 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4431 | unlock: |
| 4432 | mutex_unlock(&memcg->thresholds_lock); |
| 4433 | |
| 4434 | return ret; |
| 4435 | } |
| 4436 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4437 | static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4438 | struct cftype *cft, struct eventfd_ctx *eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4439 | { |
| 4440 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4441 | struct mem_cgroup_thresholds *thresholds; |
| 4442 | struct mem_cgroup_threshold_ary *new; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4443 | int type = MEMFILE_TYPE(cft->private); |
| 4444 | u64 usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4445 | int i, j, size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4446 | |
| 4447 | mutex_lock(&memcg->thresholds_lock); |
| 4448 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4449 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4450 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4451 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4452 | else |
| 4453 | BUG(); |
| 4454 | |
| 4455 | /* |
| 4456 | * Something went wrong if we trying to unregister a threshold |
| 4457 | * if we don't have thresholds |
| 4458 | */ |
| 4459 | BUG_ON(!thresholds); |
| 4460 | |
| 4461 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 4462 | |
| 4463 | /* Check if a threshold crossed before removing */ |
| 4464 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4465 | |
| 4466 | /* Calculate new number of threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4467 | size = 0; |
| 4468 | for (i = 0; i < thresholds->primary->size; i++) { |
| 4469 | if (thresholds->primary->entries[i].eventfd != eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4470 | size++; |
| 4471 | } |
| 4472 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4473 | new = thresholds->spare; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4474 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4475 | /* Set thresholds array to NULL if we don't have thresholds */ |
| 4476 | if (!size) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4477 | kfree(new); |
| 4478 | new = NULL; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4479 | goto swap_buffers; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4480 | } |
| 4481 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4482 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4483 | |
| 4484 | /* Copy thresholds and find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4485 | new->current_threshold = -1; |
| 4486 | for (i = 0, j = 0; i < thresholds->primary->size; i++) { |
| 4487 | if (thresholds->primary->entries[i].eventfd == eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4488 | continue; |
| 4489 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4490 | new->entries[j] = thresholds->primary->entries[i]; |
| 4491 | if (new->entries[j].threshold < usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4492 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4493 | * new->current_threshold will not be used |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4494 | * until rcu_assign_pointer(), so it's safe to increment |
| 4495 | * it here. |
| 4496 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4497 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4498 | } |
| 4499 | j++; |
| 4500 | } |
| 4501 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4502 | swap_buffers: |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4503 | /* Swap primary and spare array */ |
| 4504 | thresholds->spare = thresholds->primary; |
| 4505 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4506 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4507 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4508 | synchronize_rcu(); |
| 4509 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4510 | mutex_unlock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4511 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4512 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4513 | static int mem_cgroup_oom_register_event(struct cgroup *cgrp, |
| 4514 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
| 4515 | { |
| 4516 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4517 | struct mem_cgroup_eventfd_list *event; |
| 4518 | int type = MEMFILE_TYPE(cft->private); |
| 4519 | |
| 4520 | BUG_ON(type != _OOM_TYPE); |
| 4521 | event = kmalloc(sizeof(*event), GFP_KERNEL); |
| 4522 | if (!event) |
| 4523 | return -ENOMEM; |
| 4524 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4525 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4526 | |
| 4527 | event->eventfd = eventfd; |
| 4528 | list_add(&event->list, &memcg->oom_notify); |
| 4529 | |
| 4530 | /* already in OOM ? */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 4531 | if (atomic_read(&memcg->under_oom)) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4532 | eventfd_signal(eventfd, 1); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4533 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4534 | |
| 4535 | return 0; |
| 4536 | } |
| 4537 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4538 | static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4539 | struct cftype *cft, struct eventfd_ctx *eventfd) |
| 4540 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4541 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4542 | struct mem_cgroup_eventfd_list *ev, *tmp; |
| 4543 | int type = MEMFILE_TYPE(cft->private); |
| 4544 | |
| 4545 | BUG_ON(type != _OOM_TYPE); |
| 4546 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4547 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4548 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4549 | list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) { |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4550 | if (ev->eventfd == eventfd) { |
| 4551 | list_del(&ev->list); |
| 4552 | kfree(ev); |
| 4553 | } |
| 4554 | } |
| 4555 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4556 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4557 | } |
| 4558 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4559 | static int mem_cgroup_oom_control_read(struct cgroup *cgrp, |
| 4560 | struct cftype *cft, struct cgroup_map_cb *cb) |
| 4561 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4562 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4563 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4564 | cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4565 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4566 | if (atomic_read(&memcg->under_oom)) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4567 | cb->fill(cb, "under_oom", 1); |
| 4568 | else |
| 4569 | cb->fill(cb, "under_oom", 0); |
| 4570 | return 0; |
| 4571 | } |
| 4572 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4573 | static int mem_cgroup_oom_control_write(struct cgroup *cgrp, |
| 4574 | struct cftype *cft, u64 val) |
| 4575 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4576 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4577 | struct mem_cgroup *parent; |
| 4578 | |
| 4579 | /* cannot set to root cgroup and only 0 and 1 are allowed */ |
| 4580 | if (!cgrp->parent || !((val == 0) || (val == 1))) |
| 4581 | return -EINVAL; |
| 4582 | |
| 4583 | parent = mem_cgroup_from_cont(cgrp->parent); |
| 4584 | |
| 4585 | cgroup_lock(); |
| 4586 | /* oom-kill-disable is a flag for subhierarchy. */ |
| 4587 | if ((parent->use_hierarchy) || |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4588 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4589 | cgroup_unlock(); |
| 4590 | return -EINVAL; |
| 4591 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4592 | memcg->oom_kill_disable = val; |
KAMEZAWA Hiroyuki | 4d845eb | 2010-06-29 15:05:18 -0700 | [diff] [blame] | 4593 | if (!val) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4594 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4595 | cgroup_unlock(); |
| 4596 | return 0; |
| 4597 | } |
| 4598 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4599 | #ifdef CONFIG_NUMA |
| 4600 | static const struct file_operations mem_control_numa_stat_file_operations = { |
| 4601 | .read = seq_read, |
| 4602 | .llseek = seq_lseek, |
| 4603 | .release = single_release, |
| 4604 | }; |
| 4605 | |
| 4606 | static int mem_control_numa_stat_open(struct inode *unused, struct file *file) |
| 4607 | { |
| 4608 | struct cgroup *cont = file->f_dentry->d_parent->d_fsdata; |
| 4609 | |
| 4610 | file->f_op = &mem_control_numa_stat_file_operations; |
| 4611 | return single_open(file, mem_control_numa_stat_show, cont); |
| 4612 | } |
| 4613 | #endif /* CONFIG_NUMA */ |
| 4614 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4615 | static struct cftype mem_cgroup_files[] = { |
| 4616 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4617 | .name = "usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4618 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4619 | .read_u64 = mem_cgroup_read, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4620 | .register_event = mem_cgroup_usage_register_event, |
| 4621 | .unregister_event = mem_cgroup_usage_unregister_event, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4622 | }, |
| 4623 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4624 | .name = "max_usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4625 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4626 | .trigger = mem_cgroup_reset, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4627 | .read_u64 = mem_cgroup_read, |
| 4628 | }, |
| 4629 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4630 | .name = "limit_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4631 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 4632 | .write_string = mem_cgroup_write, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4633 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4634 | }, |
| 4635 | { |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 4636 | .name = "soft_limit_in_bytes", |
| 4637 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), |
| 4638 | .write_string = mem_cgroup_write, |
| 4639 | .read_u64 = mem_cgroup_read, |
| 4640 | }, |
| 4641 | { |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4642 | .name = "failcnt", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4643 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4644 | .trigger = mem_cgroup_reset, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4645 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4646 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 4647 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4648 | .name = "stat", |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 4649 | .read_map = mem_control_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4650 | }, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4651 | { |
| 4652 | .name = "force_empty", |
| 4653 | .trigger = mem_cgroup_force_empty_write, |
| 4654 | }, |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4655 | { |
| 4656 | .name = "use_hierarchy", |
| 4657 | .write_u64 = mem_cgroup_hierarchy_write, |
| 4658 | .read_u64 = mem_cgroup_hierarchy_read, |
| 4659 | }, |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4660 | { |
| 4661 | .name = "swappiness", |
| 4662 | .read_u64 = mem_cgroup_swappiness_read, |
| 4663 | .write_u64 = mem_cgroup_swappiness_write, |
| 4664 | }, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4665 | { |
| 4666 | .name = "move_charge_at_immigrate", |
| 4667 | .read_u64 = mem_cgroup_move_charge_read, |
| 4668 | .write_u64 = mem_cgroup_move_charge_write, |
| 4669 | }, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4670 | { |
| 4671 | .name = "oom_control", |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4672 | .read_map = mem_cgroup_oom_control_read, |
| 4673 | .write_u64 = mem_cgroup_oom_control_write, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4674 | .register_event = mem_cgroup_oom_register_event, |
| 4675 | .unregister_event = mem_cgroup_oom_unregister_event, |
| 4676 | .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL), |
| 4677 | }, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4678 | #ifdef CONFIG_NUMA |
| 4679 | { |
| 4680 | .name = "numa_stat", |
| 4681 | .open = mem_control_numa_stat_open, |
KAMEZAWA Hiroyuki | 8957712 | 2011-06-15 15:08:41 -0700 | [diff] [blame] | 4682 | .mode = S_IRUGO, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4683 | }, |
| 4684 | #endif |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4685 | }; |
| 4686 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4687 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 4688 | static struct cftype memsw_cgroup_files[] = { |
| 4689 | { |
| 4690 | .name = "memsw.usage_in_bytes", |
| 4691 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), |
| 4692 | .read_u64 = mem_cgroup_read, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4693 | .register_event = mem_cgroup_usage_register_event, |
| 4694 | .unregister_event = mem_cgroup_usage_unregister_event, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4695 | }, |
| 4696 | { |
| 4697 | .name = "memsw.max_usage_in_bytes", |
| 4698 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), |
| 4699 | .trigger = mem_cgroup_reset, |
| 4700 | .read_u64 = mem_cgroup_read, |
| 4701 | }, |
| 4702 | { |
| 4703 | .name = "memsw.limit_in_bytes", |
| 4704 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), |
| 4705 | .write_string = mem_cgroup_write, |
| 4706 | .read_u64 = mem_cgroup_read, |
| 4707 | }, |
| 4708 | { |
| 4709 | .name = "memsw.failcnt", |
| 4710 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), |
| 4711 | .trigger = mem_cgroup_reset, |
| 4712 | .read_u64 = mem_cgroup_read, |
| 4713 | }, |
| 4714 | }; |
| 4715 | |
| 4716 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4717 | { |
| 4718 | if (!do_swap_account) |
| 4719 | return 0; |
| 4720 | return cgroup_add_files(cont, ss, memsw_cgroup_files, |
| 4721 | ARRAY_SIZE(memsw_cgroup_files)); |
| 4722 | }; |
| 4723 | #else |
| 4724 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4725 | { |
| 4726 | return 0; |
| 4727 | } |
| 4728 | #endif |
| 4729 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4730 | 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] | 4731 | { |
| 4732 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4733 | struct mem_cgroup_per_zone *mz; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 4734 | enum lru_list l; |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 4735 | int zone, tmp = node; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4736 | /* |
| 4737 | * This routine is called against possible nodes. |
| 4738 | * But it's BUG to call kmalloc() against offline node. |
| 4739 | * |
| 4740 | * TODO: this routine can waste much memory for nodes which will |
| 4741 | * never be onlined. It's better to use memory hotplug callback |
| 4742 | * function. |
| 4743 | */ |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 4744 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 4745 | tmp = -1; |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4746 | pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4747 | if (!pn) |
| 4748 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4749 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4750 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 4751 | mz = &pn->zoneinfo[zone]; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 4752 | for_each_lru(l) |
| 4753 | INIT_LIST_HEAD(&mz->lists[l]); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4754 | mz->usage_in_excess = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4755 | mz->on_tree = false; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4756 | mz->mem = memcg; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4757 | } |
Igor Mammedov | 0a619e5 | 2011-11-02 13:38:21 -0700 | [diff] [blame] | 4758 | memcg->info.nodeinfo[node] = pn; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4759 | return 0; |
| 4760 | } |
| 4761 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4762 | 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] | 4763 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4764 | kfree(memcg->info.nodeinfo[node]); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4765 | } |
| 4766 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4767 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 4768 | { |
| 4769 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4770 | int size = sizeof(struct mem_cgroup); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4771 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4772 | /* Can be very big if MAX_NUMNODES is very big */ |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4773 | if (size < PAGE_SIZE) |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4774 | mem = kzalloc(size, GFP_KERNEL); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4775 | else |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4776 | mem = vzalloc(size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4777 | |
Dan Carpenter | e7bbcdf | 2010-03-23 13:35:12 -0700 | [diff] [blame] | 4778 | if (!mem) |
| 4779 | return NULL; |
| 4780 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4781 | mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu); |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 4782 | if (!mem->stat) |
| 4783 | goto out_free; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 4784 | spin_lock_init(&mem->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4785 | return mem; |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 4786 | |
| 4787 | out_free: |
| 4788 | if (size < PAGE_SIZE) |
| 4789 | kfree(mem); |
| 4790 | else |
| 4791 | vfree(mem); |
| 4792 | return NULL; |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4793 | } |
| 4794 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4795 | /* |
| 4796 | * At destroying mem_cgroup, references from swap_cgroup can remain. |
| 4797 | * (scanning all at force_empty is too costly...) |
| 4798 | * |
| 4799 | * Instead of clearing all references at force_empty, we remember |
| 4800 | * the number of reference from swap_cgroup and free mem_cgroup when |
| 4801 | * it goes down to 0. |
| 4802 | * |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4803 | * Removal of cgroup itself succeeds regardless of refs from swap. |
| 4804 | */ |
| 4805 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4806 | static void __mem_cgroup_free(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4807 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4808 | int node; |
| 4809 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4810 | mem_cgroup_remove_from_trees(memcg); |
| 4811 | free_css_id(&mem_cgroup_subsys, &memcg->css); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4812 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4813 | for_each_node_state(node, N_POSSIBLE) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4814 | free_mem_cgroup_per_zone_info(memcg, node); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4815 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4816 | free_percpu(memcg->stat); |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4817 | if (sizeof(struct mem_cgroup) < PAGE_SIZE) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4818 | kfree(memcg); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4819 | else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4820 | vfree(memcg); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4821 | } |
| 4822 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4823 | static void mem_cgroup_get(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4824 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4825 | atomic_inc(&memcg->refcnt); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4826 | } |
| 4827 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4828 | static void __mem_cgroup_put(struct mem_cgroup *memcg, int count) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4829 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4830 | if (atomic_sub_and_test(count, &memcg->refcnt)) { |
| 4831 | struct mem_cgroup *parent = parent_mem_cgroup(memcg); |
| 4832 | __mem_cgroup_free(memcg); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4833 | if (parent) |
| 4834 | mem_cgroup_put(parent); |
| 4835 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4836 | } |
| 4837 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4838 | static void mem_cgroup_put(struct mem_cgroup *memcg) |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4839 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4840 | __mem_cgroup_put(memcg, 1); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4841 | } |
| 4842 | |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4843 | /* |
| 4844 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. |
| 4845 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4846 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg) |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4847 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4848 | if (!memcg->res.parent) |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4849 | return NULL; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4850 | return mem_cgroup_from_res_counter(memcg->res.parent, res); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4851 | } |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4852 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4853 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 4854 | static void __init enable_swap_cgroup(void) |
| 4855 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 4856 | if (!mem_cgroup_disabled() && really_do_swap_account) |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4857 | do_swap_account = 1; |
| 4858 | } |
| 4859 | #else |
| 4860 | static void __init enable_swap_cgroup(void) |
| 4861 | { |
| 4862 | } |
| 4863 | #endif |
| 4864 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4865 | static int mem_cgroup_soft_limit_tree_init(void) |
| 4866 | { |
| 4867 | struct mem_cgroup_tree_per_node *rtpn; |
| 4868 | struct mem_cgroup_tree_per_zone *rtpz; |
| 4869 | int tmp, node, zone; |
| 4870 | |
| 4871 | for_each_node_state(node, N_POSSIBLE) { |
| 4872 | tmp = node; |
| 4873 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 4874 | tmp = -1; |
| 4875 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp); |
| 4876 | if (!rtpn) |
| 4877 | return 1; |
| 4878 | |
| 4879 | soft_limit_tree.rb_tree_per_node[node] = rtpn; |
| 4880 | |
| 4881 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 4882 | rtpz = &rtpn->rb_tree_per_zone[zone]; |
| 4883 | rtpz->rb_root = RB_ROOT; |
| 4884 | spin_lock_init(&rtpz->lock); |
| 4885 | } |
| 4886 | } |
| 4887 | return 0; |
| 4888 | } |
| 4889 | |
Li Zefan | 0eb253e | 2009-01-15 13:51:25 -0800 | [diff] [blame] | 4890 | static struct cgroup_subsys_state * __ref |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4891 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 4892 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4893 | struct mem_cgroup *memcg, *parent; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4894 | long error = -ENOMEM; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4895 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4896 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4897 | memcg = mem_cgroup_alloc(); |
| 4898 | if (!memcg) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4899 | return ERR_PTR(error); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 4900 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4901 | for_each_node_state(node, N_POSSIBLE) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4902 | if (alloc_mem_cgroup_per_zone_info(memcg, node)) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4903 | goto free_out; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4904 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4905 | /* root ? */ |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4906 | if (cont->parent == NULL) { |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 4907 | int cpu; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4908 | enable_swap_cgroup(); |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4909 | parent = NULL; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4910 | root_mem_cgroup = memcg; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4911 | if (mem_cgroup_soft_limit_tree_init()) |
| 4912 | goto free_out; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 4913 | for_each_possible_cpu(cpu) { |
| 4914 | struct memcg_stock_pcp *stock = |
| 4915 | &per_cpu(memcg_stock, cpu); |
| 4916 | INIT_WORK(&stock->work, drain_local_stock); |
| 4917 | } |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 4918 | hotcpu_notifier(memcg_cpu_hotplug_callback, 0); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4919 | } else { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4920 | parent = mem_cgroup_from_cont(cont->parent); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4921 | memcg->use_hierarchy = parent->use_hierarchy; |
| 4922 | memcg->oom_kill_disable = parent->oom_kill_disable; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4923 | } |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4924 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4925 | if (parent && parent->use_hierarchy) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4926 | res_counter_init(&memcg->res, &parent->res); |
| 4927 | res_counter_init(&memcg->memsw, &parent->memsw); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4928 | /* |
| 4929 | * We increment refcnt of the parent to ensure that we can |
| 4930 | * safely access it on res_counter_charge/uncharge. |
| 4931 | * This refcnt will be decremented when freeing this |
| 4932 | * mem_cgroup(see mem_cgroup_put). |
| 4933 | */ |
| 4934 | mem_cgroup_get(parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4935 | } else { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4936 | res_counter_init(&memcg->res, NULL); |
| 4937 | res_counter_init(&memcg->memsw, NULL); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4938 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4939 | memcg->last_scanned_child = 0; |
| 4940 | memcg->last_scanned_node = MAX_NUMNODES; |
| 4941 | INIT_LIST_HEAD(&memcg->oom_notify); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 4942 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4943 | if (parent) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4944 | memcg->swappiness = mem_cgroup_swappiness(parent); |
| 4945 | atomic_set(&memcg->refcnt, 1); |
| 4946 | memcg->move_charge_at_immigrate = 0; |
| 4947 | mutex_init(&memcg->thresholds_lock); |
| 4948 | return &memcg->css; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4949 | free_out: |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4950 | __mem_cgroup_free(memcg); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 4951 | root_mem_cgroup = NULL; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4952 | return ERR_PTR(error); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4953 | } |
| 4954 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4955 | static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 4956 | struct cgroup *cont) |
| 4957 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4958 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4959 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4960 | return mem_cgroup_force_empty(memcg, false); |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 4961 | } |
| 4962 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4963 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 4964 | struct cgroup *cont) |
| 4965 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4966 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 4967 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4968 | mem_cgroup_put(memcg); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4969 | } |
| 4970 | |
| 4971 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 4972 | struct cgroup *cont) |
| 4973 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4974 | int ret; |
| 4975 | |
| 4976 | ret = cgroup_add_files(cont, ss, mem_cgroup_files, |
| 4977 | ARRAY_SIZE(mem_cgroup_files)); |
| 4978 | |
| 4979 | if (!ret) |
| 4980 | ret = register_memsw_files(cont, ss); |
| 4981 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4982 | } |
| 4983 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4984 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4985 | /* Handlers for move charge at task migration. */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 4986 | #define PRECHARGE_COUNT_AT_ONCE 256 |
| 4987 | static int mem_cgroup_do_precharge(unsigned long count) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4988 | { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 4989 | int ret = 0; |
| 4990 | int batch_count = PRECHARGE_COUNT_AT_ONCE; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4991 | struct mem_cgroup *memcg = mc.to; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4992 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4993 | if (mem_cgroup_is_root(memcg)) { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 4994 | mc.precharge += count; |
| 4995 | /* we don't need css_get for root */ |
| 4996 | return ret; |
| 4997 | } |
| 4998 | /* try to charge at once */ |
| 4999 | if (count > 1) { |
| 5000 | struct res_counter *dummy; |
| 5001 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5002 | * "memcg" cannot be under rmdir() because we've already checked |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5003 | * by cgroup_lock_live_cgroup() that it is not removed and we |
| 5004 | * are still under the same cgroup_mutex. So we can postpone |
| 5005 | * css_get(). |
| 5006 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5007 | if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy)) |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5008 | goto one_by_one; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5009 | if (do_swap_account && res_counter_charge(&memcg->memsw, |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5010 | PAGE_SIZE * count, &dummy)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5011 | res_counter_uncharge(&memcg->res, PAGE_SIZE * count); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5012 | goto one_by_one; |
| 5013 | } |
| 5014 | mc.precharge += count; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5015 | return ret; |
| 5016 | } |
| 5017 | one_by_one: |
| 5018 | /* fall back to one by one charge */ |
| 5019 | while (count--) { |
| 5020 | if (signal_pending(current)) { |
| 5021 | ret = -EINTR; |
| 5022 | break; |
| 5023 | } |
| 5024 | if (!batch_count--) { |
| 5025 | batch_count = PRECHARGE_COUNT_AT_ONCE; |
| 5026 | cond_resched(); |
| 5027 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5028 | ret = __mem_cgroup_try_charge(NULL, |
| 5029 | GFP_KERNEL, 1, &memcg, false); |
| 5030 | if (ret || !memcg) |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5031 | /* mem_cgroup_clear_mc() will do uncharge later */ |
| 5032 | return -ENOMEM; |
| 5033 | mc.precharge++; |
| 5034 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5035 | return ret; |
| 5036 | } |
| 5037 | |
| 5038 | /** |
| 5039 | * is_target_pte_for_mc - check a pte whether it is valid for move charge |
| 5040 | * @vma: the vma the pte to be checked belongs |
| 5041 | * @addr: the address corresponding to the pte to be checked |
| 5042 | * @ptent: the pte to be checked |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5043 | * @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] | 5044 | * |
| 5045 | * Returns |
| 5046 | * 0(MC_TARGET_NONE): if the pte is not a target for move charge. |
| 5047 | * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for |
| 5048 | * move charge. if @target is not NULL, the page is stored in target->page |
| 5049 | * with extra refcnt got(Callers should handle it). |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5050 | * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a |
| 5051 | * target for charge migration. if @target is not NULL, the entry is stored |
| 5052 | * in target->ent. |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5053 | * |
| 5054 | * Called with pte lock held. |
| 5055 | */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5056 | union mc_target { |
| 5057 | struct page *page; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5058 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5059 | }; |
| 5060 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5061 | enum mc_target_type { |
| 5062 | MC_TARGET_NONE, /* not used */ |
| 5063 | MC_TARGET_PAGE, |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5064 | MC_TARGET_SWAP, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5065 | }; |
| 5066 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5067 | static struct page *mc_handle_present_pte(struct vm_area_struct *vma, |
| 5068 | unsigned long addr, pte_t ptent) |
| 5069 | { |
| 5070 | struct page *page = vm_normal_page(vma, addr, ptent); |
| 5071 | |
| 5072 | if (!page || !page_mapped(page)) |
| 5073 | return NULL; |
| 5074 | if (PageAnon(page)) { |
| 5075 | /* we don't move shared anon */ |
| 5076 | if (!move_anon() || page_mapcount(page) > 2) |
| 5077 | return NULL; |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5078 | } else if (!move_file()) |
| 5079 | /* we ignore mapcount for file pages */ |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5080 | return NULL; |
| 5081 | if (!get_page_unless_zero(page)) |
| 5082 | return NULL; |
| 5083 | |
| 5084 | return page; |
| 5085 | } |
| 5086 | |
| 5087 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
| 5088 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5089 | { |
| 5090 | int usage_count; |
| 5091 | struct page *page = NULL; |
| 5092 | swp_entry_t ent = pte_to_swp_entry(ptent); |
| 5093 | |
| 5094 | if (!move_anon() || non_swap_entry(ent)) |
| 5095 | return NULL; |
| 5096 | usage_count = mem_cgroup_count_swap_user(ent, &page); |
| 5097 | if (usage_count > 1) { /* we don't move shared anon */ |
| 5098 | if (page) |
| 5099 | put_page(page); |
| 5100 | return NULL; |
| 5101 | } |
| 5102 | if (do_swap_account) |
| 5103 | entry->val = ent.val; |
| 5104 | |
| 5105 | return page; |
| 5106 | } |
| 5107 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5108 | static struct page *mc_handle_file_pte(struct vm_area_struct *vma, |
| 5109 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5110 | { |
| 5111 | struct page *page = NULL; |
| 5112 | struct inode *inode; |
| 5113 | struct address_space *mapping; |
| 5114 | pgoff_t pgoff; |
| 5115 | |
| 5116 | if (!vma->vm_file) /* anonymous vma */ |
| 5117 | return NULL; |
| 5118 | if (!move_file()) |
| 5119 | return NULL; |
| 5120 | |
| 5121 | inode = vma->vm_file->f_path.dentry->d_inode; |
| 5122 | mapping = vma->vm_file->f_mapping; |
| 5123 | if (pte_none(ptent)) |
| 5124 | pgoff = linear_page_index(vma, addr); |
| 5125 | else /* pte_file(ptent) is true */ |
| 5126 | pgoff = pte_to_pgoff(ptent); |
| 5127 | |
| 5128 | /* 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] | 5129 | page = find_get_page(mapping, pgoff); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5130 | |
Hugh Dickins | aa3b189 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 5131 | #ifdef CONFIG_SWAP |
| 5132 | /* shmem/tmpfs may report page out on swap: account for that too. */ |
| 5133 | if (radix_tree_exceptional_entry(page)) { |
| 5134 | swp_entry_t swap = radix_to_swp_entry(page); |
| 5135 | if (do_swap_account) |
| 5136 | *entry = swap; |
| 5137 | page = find_get_page(&swapper_space, swap.val); |
| 5138 | } |
| 5139 | #endif |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5140 | return page; |
| 5141 | } |
| 5142 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5143 | static int is_target_pte_for_mc(struct vm_area_struct *vma, |
| 5144 | unsigned long addr, pte_t ptent, union mc_target *target) |
| 5145 | { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5146 | struct page *page = NULL; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5147 | struct page_cgroup *pc; |
| 5148 | int ret = 0; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5149 | swp_entry_t ent = { .val = 0 }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5150 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5151 | if (pte_present(ptent)) |
| 5152 | page = mc_handle_present_pte(vma, addr, ptent); |
| 5153 | else if (is_swap_pte(ptent)) |
| 5154 | page = mc_handle_swap_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5155 | else if (pte_none(ptent) || pte_file(ptent)) |
| 5156 | page = mc_handle_file_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5157 | |
| 5158 | if (!page && !ent.val) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5159 | return 0; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5160 | if (page) { |
| 5161 | pc = lookup_page_cgroup(page); |
| 5162 | /* |
| 5163 | * Do only loose check w/o page_cgroup lock. |
| 5164 | * mem_cgroup_move_account() checks the pc is valid or not under |
| 5165 | * the lock. |
| 5166 | */ |
| 5167 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { |
| 5168 | ret = MC_TARGET_PAGE; |
| 5169 | if (target) |
| 5170 | target->page = page; |
| 5171 | } |
| 5172 | if (!ret || !target) |
| 5173 | put_page(page); |
| 5174 | } |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5175 | /* There is a swap entry and a page doesn't exist or isn't charged */ |
| 5176 | if (ent.val && !ret && |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5177 | css_id(&mc.from->css) == lookup_swap_cgroup(ent)) { |
| 5178 | ret = MC_TARGET_SWAP; |
| 5179 | if (target) |
| 5180 | target->ent = ent; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5181 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5182 | return ret; |
| 5183 | } |
| 5184 | |
| 5185 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, |
| 5186 | unsigned long addr, unsigned long end, |
| 5187 | struct mm_walk *walk) |
| 5188 | { |
| 5189 | struct vm_area_struct *vma = walk->private; |
| 5190 | pte_t *pte; |
| 5191 | spinlock_t *ptl; |
| 5192 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 5193 | split_huge_page_pmd(walk->mm, pmd); |
| 5194 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5195 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5196 | for (; addr != end; pte++, addr += PAGE_SIZE) |
| 5197 | if (is_target_pte_for_mc(vma, addr, *pte, NULL)) |
| 5198 | mc.precharge++; /* increment precharge temporarily */ |
| 5199 | pte_unmap_unlock(pte - 1, ptl); |
| 5200 | cond_resched(); |
| 5201 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5202 | return 0; |
| 5203 | } |
| 5204 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5205 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
| 5206 | { |
| 5207 | unsigned long precharge; |
| 5208 | struct vm_area_struct *vma; |
| 5209 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5210 | down_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5211 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5212 | struct mm_walk mem_cgroup_count_precharge_walk = { |
| 5213 | .pmd_entry = mem_cgroup_count_precharge_pte_range, |
| 5214 | .mm = mm, |
| 5215 | .private = vma, |
| 5216 | }; |
| 5217 | if (is_vm_hugetlb_page(vma)) |
| 5218 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5219 | walk_page_range(vma->vm_start, vma->vm_end, |
| 5220 | &mem_cgroup_count_precharge_walk); |
| 5221 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5222 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5223 | |
| 5224 | precharge = mc.precharge; |
| 5225 | mc.precharge = 0; |
| 5226 | |
| 5227 | return precharge; |
| 5228 | } |
| 5229 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5230 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
| 5231 | { |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5232 | unsigned long precharge = mem_cgroup_count_precharge(mm); |
| 5233 | |
| 5234 | VM_BUG_ON(mc.moving_task); |
| 5235 | mc.moving_task = current; |
| 5236 | return mem_cgroup_do_precharge(precharge); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5237 | } |
| 5238 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5239 | /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */ |
| 5240 | static void __mem_cgroup_clear_mc(void) |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5241 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5242 | struct mem_cgroup *from = mc.from; |
| 5243 | struct mem_cgroup *to = mc.to; |
| 5244 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5245 | /* we must uncharge all the leftover precharges from mc.to */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5246 | if (mc.precharge) { |
| 5247 | __mem_cgroup_cancel_charge(mc.to, mc.precharge); |
| 5248 | mc.precharge = 0; |
| 5249 | } |
| 5250 | /* |
| 5251 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so |
| 5252 | * we must uncharge here. |
| 5253 | */ |
| 5254 | if (mc.moved_charge) { |
| 5255 | __mem_cgroup_cancel_charge(mc.from, mc.moved_charge); |
| 5256 | mc.moved_charge = 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5257 | } |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5258 | /* we must fixup refcnts and charges */ |
| 5259 | if (mc.moved_swap) { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5260 | /* uncharge swap account from the old cgroup */ |
| 5261 | if (!mem_cgroup_is_root(mc.from)) |
| 5262 | res_counter_uncharge(&mc.from->memsw, |
| 5263 | PAGE_SIZE * mc.moved_swap); |
| 5264 | __mem_cgroup_put(mc.from, mc.moved_swap); |
| 5265 | |
| 5266 | if (!mem_cgroup_is_root(mc.to)) { |
| 5267 | /* |
| 5268 | * we charged both to->res and to->memsw, so we should |
| 5269 | * uncharge to->res. |
| 5270 | */ |
| 5271 | res_counter_uncharge(&mc.to->res, |
| 5272 | PAGE_SIZE * mc.moved_swap); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5273 | } |
| 5274 | /* we've already done mem_cgroup_get(mc.to) */ |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5275 | mc.moved_swap = 0; |
| 5276 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5277 | memcg_oom_recover(from); |
| 5278 | memcg_oom_recover(to); |
| 5279 | wake_up_all(&mc.waitq); |
| 5280 | } |
| 5281 | |
| 5282 | static void mem_cgroup_clear_mc(void) |
| 5283 | { |
| 5284 | struct mem_cgroup *from = mc.from; |
| 5285 | |
| 5286 | /* |
| 5287 | * we must clear moving_task before waking up waiters at the end of |
| 5288 | * task migration. |
| 5289 | */ |
| 5290 | mc.moving_task = NULL; |
| 5291 | __mem_cgroup_clear_mc(); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5292 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5293 | mc.from = NULL; |
| 5294 | mc.to = NULL; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5295 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5296 | mem_cgroup_end_move(from); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5297 | } |
| 5298 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5299 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 5300 | struct cgroup *cgroup, |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame^] | 5301 | struct cgroup_taskset *tset) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5302 | { |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame^] | 5303 | struct task_struct *p = cgroup_taskset_first(tset); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5304 | int ret = 0; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5305 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5306 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5307 | if (memcg->move_charge_at_immigrate) { |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5308 | struct mm_struct *mm; |
| 5309 | struct mem_cgroup *from = mem_cgroup_from_task(p); |
| 5310 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5311 | VM_BUG_ON(from == memcg); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5312 | |
| 5313 | mm = get_task_mm(p); |
| 5314 | if (!mm) |
| 5315 | return 0; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5316 | /* We move charges only when we move a owner of the mm */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5317 | if (mm->owner == p) { |
| 5318 | VM_BUG_ON(mc.from); |
| 5319 | VM_BUG_ON(mc.to); |
| 5320 | VM_BUG_ON(mc.precharge); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5321 | VM_BUG_ON(mc.moved_charge); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5322 | VM_BUG_ON(mc.moved_swap); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5323 | mem_cgroup_start_move(from); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5324 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5325 | mc.from = from; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5326 | mc.to = memcg; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5327 | spin_unlock(&mc.lock); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5328 | /* We set mc.moving_task later */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5329 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5330 | ret = mem_cgroup_precharge_mc(mm); |
| 5331 | if (ret) |
| 5332 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5333 | } |
| 5334 | mmput(mm); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5335 | } |
| 5336 | return ret; |
| 5337 | } |
| 5338 | |
| 5339 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 5340 | struct cgroup *cgroup, |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame^] | 5341 | struct cgroup_taskset *tset) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5342 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5343 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5344 | } |
| 5345 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5346 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
| 5347 | unsigned long addr, unsigned long end, |
| 5348 | struct mm_walk *walk) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5349 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5350 | int ret = 0; |
| 5351 | struct vm_area_struct *vma = walk->private; |
| 5352 | pte_t *pte; |
| 5353 | spinlock_t *ptl; |
| 5354 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 5355 | split_huge_page_pmd(walk->mm, pmd); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5356 | retry: |
| 5357 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5358 | for (; addr != end; addr += PAGE_SIZE) { |
| 5359 | pte_t ptent = *(pte++); |
| 5360 | union mc_target target; |
| 5361 | int type; |
| 5362 | struct page *page; |
| 5363 | struct page_cgroup *pc; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5364 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5365 | |
| 5366 | if (!mc.precharge) |
| 5367 | break; |
| 5368 | |
| 5369 | type = is_target_pte_for_mc(vma, addr, ptent, &target); |
| 5370 | switch (type) { |
| 5371 | case MC_TARGET_PAGE: |
| 5372 | page = target.page; |
| 5373 | if (isolate_lru_page(page)) |
| 5374 | goto put; |
| 5375 | pc = lookup_page_cgroup(page); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 5376 | if (!mem_cgroup_move_account(page, 1, pc, |
| 5377 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5378 | mc.precharge--; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5379 | /* we uncharge from mc.from later. */ |
| 5380 | mc.moved_charge++; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5381 | } |
| 5382 | putback_lru_page(page); |
| 5383 | put: /* is_target_pte_for_mc() gets the page */ |
| 5384 | put_page(page); |
| 5385 | break; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5386 | case MC_TARGET_SWAP: |
| 5387 | ent = target.ent; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5388 | if (!mem_cgroup_move_swap_account(ent, |
| 5389 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5390 | mc.precharge--; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5391 | /* we fixup refcnts and charges later. */ |
| 5392 | mc.moved_swap++; |
| 5393 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5394 | break; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5395 | default: |
| 5396 | break; |
| 5397 | } |
| 5398 | } |
| 5399 | pte_unmap_unlock(pte - 1, ptl); |
| 5400 | cond_resched(); |
| 5401 | |
| 5402 | if (addr != end) { |
| 5403 | /* |
| 5404 | * We have consumed all precharges we got in can_attach(). |
| 5405 | * We try charge one by one, but don't do any additional |
| 5406 | * charges to mc.to if we have failed in charge once in attach() |
| 5407 | * phase. |
| 5408 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5409 | ret = mem_cgroup_do_precharge(1); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5410 | if (!ret) |
| 5411 | goto retry; |
| 5412 | } |
| 5413 | |
| 5414 | return ret; |
| 5415 | } |
| 5416 | |
| 5417 | static void mem_cgroup_move_charge(struct mm_struct *mm) |
| 5418 | { |
| 5419 | struct vm_area_struct *vma; |
| 5420 | |
| 5421 | lru_add_drain_all(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5422 | retry: |
| 5423 | if (unlikely(!down_read_trylock(&mm->mmap_sem))) { |
| 5424 | /* |
| 5425 | * Someone who are holding the mmap_sem might be waiting in |
| 5426 | * waitq. So we cancel all extra charges, wake up all waiters, |
| 5427 | * and retry. Because we cancel precharges, we might not be able |
| 5428 | * to move enough charges, but moving charge is a best-effort |
| 5429 | * feature anyway, so it wouldn't be a big problem. |
| 5430 | */ |
| 5431 | __mem_cgroup_clear_mc(); |
| 5432 | cond_resched(); |
| 5433 | goto retry; |
| 5434 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5435 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5436 | int ret; |
| 5437 | struct mm_walk mem_cgroup_move_charge_walk = { |
| 5438 | .pmd_entry = mem_cgroup_move_charge_pte_range, |
| 5439 | .mm = mm, |
| 5440 | .private = vma, |
| 5441 | }; |
| 5442 | if (is_vm_hugetlb_page(vma)) |
| 5443 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5444 | ret = walk_page_range(vma->vm_start, vma->vm_end, |
| 5445 | &mem_cgroup_move_charge_walk); |
| 5446 | if (ret) |
| 5447 | /* |
| 5448 | * means we have consumed all precharges and failed in |
| 5449 | * doing additional charge. Just abandon here. |
| 5450 | */ |
| 5451 | break; |
| 5452 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5453 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5454 | } |
| 5455 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5456 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 5457 | struct cgroup *cont, |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame^] | 5458 | struct cgroup_taskset *tset) |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5459 | { |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame^] | 5460 | struct task_struct *p = cgroup_taskset_first(tset); |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5461 | struct mm_struct *mm = get_task_mm(p); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5462 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5463 | if (mm) { |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5464 | if (mc.to) |
| 5465 | mem_cgroup_move_charge(mm); |
| 5466 | put_swap_token(mm); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5467 | mmput(mm); |
| 5468 | } |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5469 | if (mc.to) |
| 5470 | mem_cgroup_clear_mc(); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5471 | } |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5472 | #else /* !CONFIG_MMU */ |
| 5473 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 5474 | struct cgroup *cgroup, |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame^] | 5475 | struct cgroup_taskset *tset) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5476 | { |
| 5477 | return 0; |
| 5478 | } |
| 5479 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 5480 | struct cgroup *cgroup, |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame^] | 5481 | struct cgroup_taskset *tset) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5482 | { |
| 5483 | } |
| 5484 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 5485 | struct cgroup *cont, |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame^] | 5486 | struct cgroup_taskset *tset) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5487 | { |
| 5488 | } |
| 5489 | #endif |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5490 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5491 | struct cgroup_subsys mem_cgroup_subsys = { |
| 5492 | .name = "memory", |
| 5493 | .subsys_id = mem_cgroup_subsys_id, |
| 5494 | .create = mem_cgroup_create, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5495 | .pre_destroy = mem_cgroup_pre_destroy, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5496 | .destroy = mem_cgroup_destroy, |
| 5497 | .populate = mem_cgroup_populate, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5498 | .can_attach = mem_cgroup_can_attach, |
| 5499 | .cancel_attach = mem_cgroup_cancel_attach, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5500 | .attach = mem_cgroup_move_task, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5501 | .early_init = 0, |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5502 | .use_id = 1, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5503 | }; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5504 | |
| 5505 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5506 | static int __init enable_swap_account(char *s) |
| 5507 | { |
| 5508 | /* consider enabled if no parameter or 1 is given */ |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5509 | if (!strcmp(s, "1")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5510 | really_do_swap_account = 1; |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5511 | else if (!strcmp(s, "0")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5512 | really_do_swap_account = 0; |
| 5513 | return 1; |
| 5514 | } |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5515 | __setup("swapaccount=", enable_swap_account); |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5516 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5517 | #endif |