Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1 | /* memcontrol.c - Memory Controller |
| 2 | * |
| 3 | * Copyright IBM Corporation, 2007 |
| 4 | * Author Balbir Singh <balbir@linux.vnet.ibm.com> |
| 5 | * |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 6 | * Copyright 2007 OpenVZ SWsoft Inc |
| 7 | * Author: Pavel Emelianov <xemul@openvz.org> |
| 8 | * |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 9 | * Memory thresholds |
| 10 | * Copyright (C) 2009 Nokia Corporation |
| 11 | * Author: Kirill A. Shutemov |
| 12 | * |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/res_counter.h> |
| 25 | #include <linux/memcontrol.h> |
| 26 | #include <linux/cgroup.h> |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 27 | #include <linux/mm.h> |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 28 | #include <linux/hugetlb.h> |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 29 | #include <linux/pagemap.h> |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 30 | #include <linux/smp.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 31 | #include <linux/page-flags.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 32 | #include <linux/backing-dev.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 33 | #include <linux/bit_spinlock.h> |
| 34 | #include <linux/rcupdate.h> |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 35 | #include <linux/limits.h> |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 36 | #include <linux/mutex.h> |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 37 | #include <linux/rbtree.h> |
Hugh Dickins | 072441e | 2011-06-27 16:18:02 -0700 | [diff] [blame] | 38 | #include <linux/shmem_fs.h> |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 39 | #include <linux/slab.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 40 | #include <linux/swap.h> |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 41 | #include <linux/swapops.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 42 | #include <linux/spinlock.h> |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 43 | #include <linux/eventfd.h> |
| 44 | #include <linux/sort.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 45 | #include <linux/fs.h> |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 46 | #include <linux/seq_file.h> |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 47 | #include <linux/vmalloc.h> |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 48 | #include <linux/mm_inline.h> |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 49 | #include <linux/page_cgroup.h> |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 50 | #include <linux/cpu.h> |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 51 | #include <linux/oom.h> |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 52 | #include "internal.h" |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 53 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 54 | #include <asm/uaccess.h> |
| 55 | |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 56 | #include <trace/events/vmscan.h> |
| 57 | |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 58 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 59 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 60 | struct mem_cgroup *root_mem_cgroup __read_mostly; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 61 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 62 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 63 | /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */ |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 64 | int do_swap_account __read_mostly; |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 65 | |
| 66 | /* for remember boot option*/ |
| 67 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP_ENABLED |
| 68 | static int really_do_swap_account __initdata = 1; |
| 69 | #else |
| 70 | static int really_do_swap_account __initdata = 0; |
| 71 | #endif |
| 72 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 73 | #else |
| 74 | #define do_swap_account (0) |
| 75 | #endif |
| 76 | |
| 77 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 78 | /* |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 79 | * Statistics for memory cgroup. |
| 80 | */ |
| 81 | enum mem_cgroup_stat_index { |
| 82 | /* |
| 83 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. |
| 84 | */ |
| 85 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 86 | MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */ |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 87 | MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 88 | MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */ |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 89 | MEM_CGROUP_STAT_DATA, /* end of data requires synchronization */ |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 90 | MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 91 | MEM_CGROUP_STAT_NSTATS, |
| 92 | }; |
| 93 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 94 | enum mem_cgroup_events_index { |
| 95 | MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */ |
| 96 | MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */ |
| 97 | MEM_CGROUP_EVENTS_COUNT, /* # of pages paged in/out */ |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 98 | MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */ |
| 99 | MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */ |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 100 | MEM_CGROUP_EVENTS_NSTATS, |
| 101 | }; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 102 | /* |
| 103 | * Per memcg event counter is incremented at every pagein/pageout. With THP, |
| 104 | * it will be incremated by the number of pages. This counter is used for |
| 105 | * for trigger some periodic events. This is straightforward and better |
| 106 | * than using jiffies etc. to handle periodic memcg event. |
| 107 | */ |
| 108 | enum mem_cgroup_events_target { |
| 109 | MEM_CGROUP_TARGET_THRESH, |
| 110 | MEM_CGROUP_TARGET_SOFTLIMIT, |
| 111 | MEM_CGROUP_NTARGETS, |
| 112 | }; |
| 113 | #define THRESHOLDS_EVENTS_TARGET (128) |
| 114 | #define SOFTLIMIT_EVENTS_TARGET (1024) |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 115 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 116 | struct mem_cgroup_stat_cpu { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 117 | long count[MEM_CGROUP_STAT_NSTATS]; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 118 | unsigned long events[MEM_CGROUP_EVENTS_NSTATS]; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 119 | unsigned long targets[MEM_CGROUP_NTARGETS]; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 120 | }; |
| 121 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 122 | /* |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 123 | * per-zone information in memory controller. |
| 124 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 125 | struct mem_cgroup_per_zone { |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 126 | /* |
| 127 | * spin_lock to protect the per cgroup LRU |
| 128 | */ |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 129 | struct list_head lists[NR_LRU_LISTS]; |
| 130 | unsigned long count[NR_LRU_LISTS]; |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 131 | |
| 132 | struct zone_reclaim_stat reclaim_stat; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 133 | struct rb_node tree_node; /* RB tree node */ |
| 134 | unsigned long long usage_in_excess;/* Set to the value by which */ |
| 135 | /* the soft limit is exceeded*/ |
| 136 | bool on_tree; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 137 | struct mem_cgroup *mem; /* Back pointer, we cannot */ |
| 138 | /* use container_of */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 139 | }; |
| 140 | /* Macro for accessing counter */ |
| 141 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) |
| 142 | |
| 143 | struct mem_cgroup_per_node { |
| 144 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; |
| 145 | }; |
| 146 | |
| 147 | struct mem_cgroup_lru_info { |
| 148 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; |
| 149 | }; |
| 150 | |
| 151 | /* |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 152 | * Cgroups above their limits are maintained in a RB-Tree, independent of |
| 153 | * their hierarchy representation |
| 154 | */ |
| 155 | |
| 156 | struct mem_cgroup_tree_per_zone { |
| 157 | struct rb_root rb_root; |
| 158 | spinlock_t lock; |
| 159 | }; |
| 160 | |
| 161 | struct mem_cgroup_tree_per_node { |
| 162 | struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES]; |
| 163 | }; |
| 164 | |
| 165 | struct mem_cgroup_tree { |
| 166 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; |
| 167 | }; |
| 168 | |
| 169 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; |
| 170 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 171 | struct mem_cgroup_threshold { |
| 172 | struct eventfd_ctx *eventfd; |
| 173 | u64 threshold; |
| 174 | }; |
| 175 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 176 | /* For threshold */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 177 | struct mem_cgroup_threshold_ary { |
| 178 | /* An array index points to threshold just below usage. */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 179 | int current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 180 | /* Size of entries[] */ |
| 181 | unsigned int size; |
| 182 | /* Array of thresholds */ |
| 183 | struct mem_cgroup_threshold entries[0]; |
| 184 | }; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 185 | |
| 186 | struct mem_cgroup_thresholds { |
| 187 | /* Primary thresholds array */ |
| 188 | struct mem_cgroup_threshold_ary *primary; |
| 189 | /* |
| 190 | * Spare threshold array. |
| 191 | * This is needed to make mem_cgroup_unregister_event() "never fail". |
| 192 | * It must be able to store at least primary->size - 1 entries. |
| 193 | */ |
| 194 | struct mem_cgroup_threshold_ary *spare; |
| 195 | }; |
| 196 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 197 | /* for OOM */ |
| 198 | struct mem_cgroup_eventfd_list { |
| 199 | struct list_head list; |
| 200 | struct eventfd_ctx *eventfd; |
| 201 | }; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 202 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 203 | static void mem_cgroup_threshold(struct mem_cgroup *mem); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 204 | static void mem_cgroup_oom_notify(struct mem_cgroup *mem); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 205 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 206 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 207 | * The memory controller data structure. The memory controller controls both |
| 208 | * page cache and RSS per cgroup. We would eventually like to provide |
| 209 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, |
| 210 | * to help the administrator determine what knobs to tune. |
| 211 | * |
| 212 | * 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] | 213 | * we hit the water mark. May be even add a low water mark, such that |
| 214 | * no reclaim occurs from a cgroup at it's low water mark, this is |
| 215 | * a feature that will be implemented much later in the future. |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 216 | */ |
| 217 | struct mem_cgroup { |
| 218 | struct cgroup_subsys_state css; |
| 219 | /* |
| 220 | * the counter to account for memory usage |
| 221 | */ |
| 222 | struct res_counter res; |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 223 | /* |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 224 | * the counter to account for mem+swap usage. |
| 225 | */ |
| 226 | struct res_counter memsw; |
| 227 | /* |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 228 | * Per cgroup active and inactive list, similar to the |
| 229 | * per zone LRU lists. |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 230 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 231 | struct mem_cgroup_lru_info info; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 232 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 233 | * While reclaiming in a hierarchy, we cache the last child we |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 234 | * reclaimed from. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 235 | */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 236 | int last_scanned_child; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 237 | int last_scanned_node; |
| 238 | #if MAX_NUMNODES > 1 |
| 239 | nodemask_t scan_nodes; |
| 240 | unsigned long next_scan_node_update; |
| 241 | #endif |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 242 | /* |
| 243 | * Should the accounting and control be hierarchical, per subtree? |
| 244 | */ |
| 245 | bool use_hierarchy; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 246 | atomic_t oom_lock; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 247 | atomic_t refcnt; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 248 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 249 | unsigned int swappiness; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 250 | /* OOM-Killer disable */ |
| 251 | int oom_kill_disable; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 252 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 253 | /* set when res.limit == memsw.limit */ |
| 254 | bool memsw_is_minimum; |
| 255 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 256 | /* protect arrays of thresholds */ |
| 257 | struct mutex thresholds_lock; |
| 258 | |
| 259 | /* thresholds for memory usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 260 | struct mem_cgroup_thresholds thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 261 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 262 | /* thresholds for mem+swap usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 263 | struct mem_cgroup_thresholds memsw_thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 264 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 265 | /* For oom notifier event fd */ |
| 266 | struct list_head oom_notify; |
| 267 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 268 | /* |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 269 | * Should we move charges of a task when a task is moved into this |
| 270 | * mem_cgroup ? And what type of charges should we move ? |
| 271 | */ |
| 272 | unsigned long move_charge_at_immigrate; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 273 | /* |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 274 | * percpu counter. |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 275 | */ |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 276 | struct mem_cgroup_stat_cpu *stat; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 277 | /* |
| 278 | * used when a cpu is offlined or other synchronizations |
| 279 | * See mem_cgroup_read_stat(). |
| 280 | */ |
| 281 | struct mem_cgroup_stat_cpu nocpu_base; |
| 282 | spinlock_t pcp_counter_lock; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 283 | }; |
| 284 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 285 | /* Stuffs for move charges at task migration. */ |
| 286 | /* |
| 287 | * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a |
| 288 | * left-shifted bitmap of these types. |
| 289 | */ |
| 290 | enum move_type { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 291 | MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */ |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 292 | MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 293 | NR_MOVE_TYPE, |
| 294 | }; |
| 295 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 296 | /* "mc" and its members are protected by cgroup_mutex */ |
| 297 | static struct move_charge_struct { |
Daisuke Nishimura | b1dd693 | 2010-11-24 12:57:06 -0800 | [diff] [blame] | 298 | spinlock_t lock; /* for from, to */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 299 | struct mem_cgroup *from; |
| 300 | struct mem_cgroup *to; |
| 301 | unsigned long precharge; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 302 | unsigned long moved_charge; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 303 | unsigned long moved_swap; |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 304 | struct task_struct *moving_task; /* a task moving charges */ |
| 305 | wait_queue_head_t waitq; /* a waitq for other context */ |
| 306 | } mc = { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 307 | .lock = __SPIN_LOCK_UNLOCKED(mc.lock), |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 308 | .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq), |
| 309 | }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 310 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 311 | static bool move_anon(void) |
| 312 | { |
| 313 | return test_bit(MOVE_CHARGE_TYPE_ANON, |
| 314 | &mc.to->move_charge_at_immigrate); |
| 315 | } |
| 316 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 317 | static bool move_file(void) |
| 318 | { |
| 319 | return test_bit(MOVE_CHARGE_TYPE_FILE, |
| 320 | &mc.to->move_charge_at_immigrate); |
| 321 | } |
| 322 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 323 | /* |
| 324 | * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft |
| 325 | * limit reclaim to prevent infinite loops, if they ever occur. |
| 326 | */ |
| 327 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100) |
| 328 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2) |
| 329 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 330 | enum charge_type { |
| 331 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
| 332 | MEM_CGROUP_CHARGE_TYPE_MAPPED, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 333 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 334 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 335 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 336 | MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 337 | NR_CHARGE_TYPE, |
| 338 | }; |
| 339 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 340 | /* for encoding cft->private value on file */ |
| 341 | #define _MEM (0) |
| 342 | #define _MEMSWAP (1) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 343 | #define _OOM_TYPE (2) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 344 | #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) |
| 345 | #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff) |
| 346 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 347 | /* Used for OOM nofiier */ |
| 348 | #define OOM_CONTROL (0) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 349 | |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 350 | /* |
| 351 | * Reclaim flags for mem_cgroup_hierarchical_reclaim |
| 352 | */ |
| 353 | #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0 |
| 354 | #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT) |
| 355 | #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1 |
| 356 | #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 357 | #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2 |
| 358 | #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT) |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 359 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 360 | static void mem_cgroup_get(struct mem_cgroup *mem); |
| 361 | static void mem_cgroup_put(struct mem_cgroup *mem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 362 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 363 | static void drain_all_stock_async(struct mem_cgroup *mem); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 364 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 365 | static struct mem_cgroup_per_zone * |
| 366 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) |
| 367 | { |
| 368 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; |
| 369 | } |
| 370 | |
Wu Fengguang | d324236 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 371 | struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem) |
| 372 | { |
| 373 | return &mem->css; |
| 374 | } |
| 375 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 376 | static struct mem_cgroup_per_zone * |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 377 | page_cgroup_zoneinfo(struct mem_cgroup *mem, struct page *page) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 378 | { |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 379 | int nid = page_to_nid(page); |
| 380 | int zid = page_zonenum(page); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 381 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 382 | return mem_cgroup_zoneinfo(mem, nid, zid); |
| 383 | } |
| 384 | |
| 385 | static struct mem_cgroup_tree_per_zone * |
| 386 | soft_limit_tree_node_zone(int nid, int zid) |
| 387 | { |
| 388 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 389 | } |
| 390 | |
| 391 | static struct mem_cgroup_tree_per_zone * |
| 392 | soft_limit_tree_from_page(struct page *page) |
| 393 | { |
| 394 | int nid = page_to_nid(page); |
| 395 | int zid = page_zonenum(page); |
| 396 | |
| 397 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 398 | } |
| 399 | |
| 400 | static void |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 401 | __mem_cgroup_insert_exceeded(struct mem_cgroup *mem, |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 402 | struct mem_cgroup_per_zone *mz, |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 403 | struct mem_cgroup_tree_per_zone *mctz, |
| 404 | unsigned long long new_usage_in_excess) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 405 | { |
| 406 | struct rb_node **p = &mctz->rb_root.rb_node; |
| 407 | struct rb_node *parent = NULL; |
| 408 | struct mem_cgroup_per_zone *mz_node; |
| 409 | |
| 410 | if (mz->on_tree) |
| 411 | return; |
| 412 | |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 413 | mz->usage_in_excess = new_usage_in_excess; |
| 414 | if (!mz->usage_in_excess) |
| 415 | return; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 416 | while (*p) { |
| 417 | parent = *p; |
| 418 | mz_node = rb_entry(parent, struct mem_cgroup_per_zone, |
| 419 | tree_node); |
| 420 | if (mz->usage_in_excess < mz_node->usage_in_excess) |
| 421 | p = &(*p)->rb_left; |
| 422 | /* |
| 423 | * We can't avoid mem cgroups that are over their soft |
| 424 | * limit by the same amount |
| 425 | */ |
| 426 | else if (mz->usage_in_excess >= mz_node->usage_in_excess) |
| 427 | p = &(*p)->rb_right; |
| 428 | } |
| 429 | rb_link_node(&mz->tree_node, parent, p); |
| 430 | rb_insert_color(&mz->tree_node, &mctz->rb_root); |
| 431 | mz->on_tree = true; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static void |
| 435 | __mem_cgroup_remove_exceeded(struct mem_cgroup *mem, |
| 436 | struct mem_cgroup_per_zone *mz, |
| 437 | struct mem_cgroup_tree_per_zone *mctz) |
| 438 | { |
| 439 | if (!mz->on_tree) |
| 440 | return; |
| 441 | rb_erase(&mz->tree_node, &mctz->rb_root); |
| 442 | mz->on_tree = false; |
| 443 | } |
| 444 | |
| 445 | static void |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 446 | mem_cgroup_remove_exceeded(struct mem_cgroup *mem, |
| 447 | struct mem_cgroup_per_zone *mz, |
| 448 | struct mem_cgroup_tree_per_zone *mctz) |
| 449 | { |
| 450 | spin_lock(&mctz->lock); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 451 | __mem_cgroup_remove_exceeded(mem, mz, mctz); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 452 | spin_unlock(&mctz->lock); |
| 453 | } |
| 454 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 455 | |
| 456 | static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page) |
| 457 | { |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 458 | unsigned long long excess; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 459 | struct mem_cgroup_per_zone *mz; |
| 460 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 461 | int nid = page_to_nid(page); |
| 462 | int zid = page_zonenum(page); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 463 | mctz = soft_limit_tree_from_page(page); |
| 464 | |
| 465 | /* |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 466 | * Necessary to update all ancestors when hierarchy is used. |
| 467 | * because their event counter is not touched. |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 468 | */ |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 469 | for (; mem; mem = parent_mem_cgroup(mem)) { |
| 470 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 471 | excess = res_counter_soft_limit_excess(&mem->res); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 472 | /* |
| 473 | * We have to update the tree if mz is on RB-tree or |
| 474 | * mem is over its softlimit. |
| 475 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 476 | if (excess || mz->on_tree) { |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 477 | spin_lock(&mctz->lock); |
| 478 | /* if on-tree, remove it */ |
| 479 | if (mz->on_tree) |
| 480 | __mem_cgroup_remove_exceeded(mem, mz, mctz); |
| 481 | /* |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 482 | * Insert again. mz->usage_in_excess will be updated. |
| 483 | * If excess is 0, no tree ops. |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 484 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 485 | __mem_cgroup_insert_exceeded(mem, mz, mctz, excess); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 486 | spin_unlock(&mctz->lock); |
| 487 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
| 491 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem) |
| 492 | { |
| 493 | int node, zone; |
| 494 | struct mem_cgroup_per_zone *mz; |
| 495 | struct mem_cgroup_tree_per_zone *mctz; |
| 496 | |
| 497 | for_each_node_state(node, N_POSSIBLE) { |
| 498 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 499 | mz = mem_cgroup_zoneinfo(mem, node, zone); |
| 500 | mctz = soft_limit_tree_node_zone(node, zone); |
| 501 | mem_cgroup_remove_exceeded(mem, mz, mctz); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 506 | static struct mem_cgroup_per_zone * |
| 507 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 508 | { |
| 509 | struct rb_node *rightmost = NULL; |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 510 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 511 | |
| 512 | retry: |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 513 | mz = NULL; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 514 | rightmost = rb_last(&mctz->rb_root); |
| 515 | if (!rightmost) |
| 516 | goto done; /* Nothing to reclaim from */ |
| 517 | |
| 518 | mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node); |
| 519 | /* |
| 520 | * Remove the node now but someone else can add it back, |
| 521 | * we will to add it back at the end of reclaim to its correct |
| 522 | * position in the tree. |
| 523 | */ |
| 524 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
| 525 | if (!res_counter_soft_limit_excess(&mz->mem->res) || |
| 526 | !css_tryget(&mz->mem->css)) |
| 527 | goto retry; |
| 528 | done: |
| 529 | return mz; |
| 530 | } |
| 531 | |
| 532 | static struct mem_cgroup_per_zone * |
| 533 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 534 | { |
| 535 | struct mem_cgroup_per_zone *mz; |
| 536 | |
| 537 | spin_lock(&mctz->lock); |
| 538 | mz = __mem_cgroup_largest_soft_limit_node(mctz); |
| 539 | spin_unlock(&mctz->lock); |
| 540 | return mz; |
| 541 | } |
| 542 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 543 | /* |
| 544 | * Implementation Note: reading percpu statistics for memcg. |
| 545 | * |
| 546 | * Both of vmstat[] and percpu_counter has threshold and do periodic |
| 547 | * synchronization to implement "quick" read. There are trade-off between |
| 548 | * reading cost and precision of value. Then, we may have a chance to implement |
| 549 | * a periodic synchronizion of counter in memcg's counter. |
| 550 | * |
| 551 | * But this _read() function is used for user interface now. The user accounts |
| 552 | * memory usage by memory cgroup and he _always_ requires exact value because |
| 553 | * he accounts memory. Even if we provide quick-and-fuzzy read, we always |
| 554 | * have to visit all online cpus and make sum. So, for now, unnecessary |
| 555 | * synchronization is not implemented. (just implemented for cpu hotplug) |
| 556 | * |
| 557 | * If there are kernel internal actions which can make use of some not-exact |
| 558 | * value, and reading all cpu value can be performance bottleneck in some |
| 559 | * common workload, threashold and synchonization as vmstat[] should be |
| 560 | * implemented. |
| 561 | */ |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 562 | static long mem_cgroup_read_stat(struct mem_cgroup *mem, |
| 563 | enum mem_cgroup_stat_index idx) |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 564 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 565 | long val = 0; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 566 | int cpu; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 567 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 568 | get_online_cpus(); |
| 569 | for_each_online_cpu(cpu) |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 570 | val += per_cpu(mem->stat->count[idx], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 571 | #ifdef CONFIG_HOTPLUG_CPU |
| 572 | spin_lock(&mem->pcp_counter_lock); |
| 573 | val += mem->nocpu_base.count[idx]; |
| 574 | spin_unlock(&mem->pcp_counter_lock); |
| 575 | #endif |
| 576 | put_online_cpus(); |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 577 | return val; |
| 578 | } |
| 579 | |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 580 | static void mem_cgroup_swap_statistics(struct mem_cgroup *mem, |
| 581 | bool charge) |
| 582 | { |
| 583 | int val = (charge) ? 1 : -1; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 584 | this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 587 | void mem_cgroup_pgfault(struct mem_cgroup *mem, int val) |
| 588 | { |
| 589 | this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val); |
| 590 | } |
| 591 | |
| 592 | void mem_cgroup_pgmajfault(struct mem_cgroup *mem, int val) |
| 593 | { |
| 594 | this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val); |
| 595 | } |
| 596 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 597 | static unsigned long mem_cgroup_read_events(struct mem_cgroup *mem, |
| 598 | enum mem_cgroup_events_index idx) |
| 599 | { |
| 600 | unsigned long val = 0; |
| 601 | int cpu; |
| 602 | |
| 603 | for_each_online_cpu(cpu) |
| 604 | val += per_cpu(mem->stat->events[idx], cpu); |
| 605 | #ifdef CONFIG_HOTPLUG_CPU |
| 606 | spin_lock(&mem->pcp_counter_lock); |
| 607 | val += mem->nocpu_base.events[idx]; |
| 608 | spin_unlock(&mem->pcp_counter_lock); |
| 609 | #endif |
| 610 | return val; |
| 611 | } |
| 612 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 613 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 614 | bool file, int nr_pages) |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 615 | { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 616 | preempt_disable(); |
| 617 | |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 618 | if (file) |
| 619 | __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], nr_pages); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 620 | else |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 621 | __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], nr_pages); |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 622 | |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 623 | /* pagein of a big page is an event. So, ignore page size */ |
| 624 | if (nr_pages > 0) |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 625 | __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGIN]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 626 | else { |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 627 | __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 628 | nr_pages = -nr_pages; /* for event */ |
| 629 | } |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 630 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 631 | __this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 632 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 633 | preempt_enable(); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 634 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 635 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 636 | static unsigned long |
| 637 | mem_cgroup_get_zonestat_node(struct mem_cgroup *mem, int nid, enum lru_list idx) |
| 638 | { |
| 639 | struct mem_cgroup_per_zone *mz; |
| 640 | u64 total = 0; |
| 641 | int zid; |
| 642 | |
| 643 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 644 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 645 | total += MEM_CGROUP_ZSTAT(mz, idx); |
| 646 | } |
| 647 | return total; |
| 648 | } |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 649 | static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem, |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 650 | enum lru_list idx) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 651 | { |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 652 | int nid; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 653 | u64 total = 0; |
| 654 | |
| 655 | for_each_online_node(nid) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 656 | total += mem_cgroup_get_zonestat_node(mem, nid, idx); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 657 | return total; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 658 | } |
| 659 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 660 | static bool __memcg_event_check(struct mem_cgroup *mem, int target) |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 661 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 662 | unsigned long val, next; |
| 663 | |
| 664 | val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]); |
| 665 | next = this_cpu_read(mem->stat->targets[target]); |
| 666 | /* from time_after() in jiffies.h */ |
| 667 | return ((long)next - (long)val < 0); |
| 668 | } |
| 669 | |
| 670 | static void __mem_cgroup_target_update(struct mem_cgroup *mem, int target) |
| 671 | { |
| 672 | unsigned long val, next; |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 673 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 674 | val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 675 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 676 | switch (target) { |
| 677 | case MEM_CGROUP_TARGET_THRESH: |
| 678 | next = val + THRESHOLDS_EVENTS_TARGET; |
| 679 | break; |
| 680 | case MEM_CGROUP_TARGET_SOFTLIMIT: |
| 681 | next = val + SOFTLIMIT_EVENTS_TARGET; |
| 682 | break; |
| 683 | default: |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | this_cpu_write(mem->stat->targets[target], next); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /* |
| 691 | * Check events in order. |
| 692 | * |
| 693 | */ |
| 694 | static void memcg_check_events(struct mem_cgroup *mem, struct page *page) |
| 695 | { |
| 696 | /* threshold event is triggered in finer grain than soft limit */ |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 697 | if (unlikely(__memcg_event_check(mem, MEM_CGROUP_TARGET_THRESH))) { |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 698 | mem_cgroup_threshold(mem); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 699 | __mem_cgroup_target_update(mem, MEM_CGROUP_TARGET_THRESH); |
| 700 | if (unlikely(__memcg_event_check(mem, |
| 701 | MEM_CGROUP_TARGET_SOFTLIMIT))){ |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 702 | mem_cgroup_update_tree(mem, page); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 703 | __mem_cgroup_target_update(mem, |
| 704 | MEM_CGROUP_TARGET_SOFTLIMIT); |
| 705 | } |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 709 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 710 | { |
| 711 | return container_of(cgroup_subsys_state(cont, |
| 712 | mem_cgroup_subsys_id), struct mem_cgroup, |
| 713 | css); |
| 714 | } |
| 715 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 716 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 717 | { |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 718 | /* |
| 719 | * mm_update_next_owner() may clear mm->owner to NULL |
| 720 | * if it races with swapoff, page migration, etc. |
| 721 | * So this can be called with p == NULL. |
| 722 | */ |
| 723 | if (unlikely(!p)) |
| 724 | return NULL; |
| 725 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 726 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
| 727 | struct mem_cgroup, css); |
| 728 | } |
| 729 | |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 730 | 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] | 731 | { |
| 732 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 733 | |
| 734 | if (!mm) |
| 735 | return NULL; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 736 | /* |
| 737 | * Because we have no locks, mm->owner's may be being moved to other |
| 738 | * cgroup. We use css_tryget() here even if this looks |
| 739 | * pessimistic (rather than adding locks here). |
| 740 | */ |
| 741 | rcu_read_lock(); |
| 742 | do { |
| 743 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 744 | if (unlikely(!mem)) |
| 745 | break; |
| 746 | } while (!css_tryget(&mem->css)); |
| 747 | rcu_read_unlock(); |
| 748 | return mem; |
| 749 | } |
| 750 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 751 | /* The caller has to guarantee "mem" exists before calling this */ |
| 752 | static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 753 | { |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 754 | struct cgroup_subsys_state *css; |
| 755 | int found; |
| 756 | |
| 757 | if (!mem) /* ROOT cgroup has the smallest ID */ |
| 758 | return root_mem_cgroup; /*css_put/get against root is ignored*/ |
| 759 | if (!mem->use_hierarchy) { |
| 760 | if (css_tryget(&mem->css)) |
| 761 | return mem; |
| 762 | return NULL; |
| 763 | } |
| 764 | rcu_read_lock(); |
| 765 | /* |
| 766 | * searching a memory cgroup which has the smallest ID under given |
| 767 | * ROOT cgroup. (ID >= 1) |
| 768 | */ |
| 769 | css = css_get_next(&mem_cgroup_subsys, 1, &mem->css, &found); |
| 770 | if (css && css_tryget(css)) |
| 771 | mem = container_of(css, struct mem_cgroup, css); |
| 772 | else |
| 773 | mem = NULL; |
| 774 | rcu_read_unlock(); |
| 775 | return mem; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 776 | } |
| 777 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 778 | static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter, |
| 779 | struct mem_cgroup *root, |
| 780 | bool cond) |
| 781 | { |
| 782 | int nextid = css_id(&iter->css) + 1; |
| 783 | int found; |
| 784 | int hierarchy_used; |
| 785 | struct cgroup_subsys_state *css; |
| 786 | |
| 787 | hierarchy_used = iter->use_hierarchy; |
| 788 | |
| 789 | css_put(&iter->css); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 790 | /* If no ROOT, walk all, ignore hierarchy */ |
| 791 | if (!cond || (root && !hierarchy_used)) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 792 | return NULL; |
| 793 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 794 | if (!root) |
| 795 | root = root_mem_cgroup; |
| 796 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 797 | do { |
| 798 | iter = NULL; |
| 799 | rcu_read_lock(); |
| 800 | |
| 801 | css = css_get_next(&mem_cgroup_subsys, nextid, |
| 802 | &root->css, &found); |
| 803 | if (css && css_tryget(css)) |
| 804 | iter = container_of(css, struct mem_cgroup, css); |
| 805 | rcu_read_unlock(); |
| 806 | /* If css is NULL, no more cgroups will be found */ |
| 807 | nextid = found + 1; |
| 808 | } while (css && !iter); |
| 809 | |
| 810 | return iter; |
| 811 | } |
| 812 | /* |
| 813 | * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please |
| 814 | * be careful that "break" loop is not allowed. We have reference count. |
| 815 | * Instead of that modify "cond" to be false and "continue" to exit the loop. |
| 816 | */ |
| 817 | #define for_each_mem_cgroup_tree_cond(iter, root, cond) \ |
| 818 | for (iter = mem_cgroup_start_loop(root);\ |
| 819 | iter != NULL;\ |
| 820 | iter = mem_cgroup_get_next(iter, root, cond)) |
| 821 | |
| 822 | #define for_each_mem_cgroup_tree(iter, root) \ |
| 823 | for_each_mem_cgroup_tree_cond(iter, root, true) |
| 824 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 825 | #define for_each_mem_cgroup_all(iter) \ |
| 826 | for_each_mem_cgroup_tree_cond(iter, NULL, true) |
| 827 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 828 | |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 829 | static inline bool mem_cgroup_is_root(struct mem_cgroup *mem) |
| 830 | { |
| 831 | return (mem == root_mem_cgroup); |
| 832 | } |
| 833 | |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 834 | void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx) |
| 835 | { |
| 836 | struct mem_cgroup *mem; |
| 837 | |
| 838 | if (!mm) |
| 839 | return; |
| 840 | |
| 841 | rcu_read_lock(); |
| 842 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 843 | if (unlikely(!mem)) |
| 844 | goto out; |
| 845 | |
| 846 | switch (idx) { |
| 847 | case PGMAJFAULT: |
| 848 | mem_cgroup_pgmajfault(mem, 1); |
| 849 | break; |
| 850 | case PGFAULT: |
| 851 | mem_cgroup_pgfault(mem, 1); |
| 852 | break; |
| 853 | default: |
| 854 | BUG(); |
| 855 | } |
| 856 | out: |
| 857 | rcu_read_unlock(); |
| 858 | } |
| 859 | EXPORT_SYMBOL(mem_cgroup_count_vm_event); |
| 860 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 861 | /* |
| 862 | * Following LRU functions are allowed to be used without PCG_LOCK. |
| 863 | * Operations are called by routine of global LRU independently from memcg. |
| 864 | * What we have to take care of here is validness of pc->mem_cgroup. |
| 865 | * |
| 866 | * Changes to pc->mem_cgroup happens when |
| 867 | * 1. charge |
| 868 | * 2. moving account |
| 869 | * In typical case, "charge" is done before add-to-lru. Exception is SwapCache. |
| 870 | * It is added to LRU before charge. |
| 871 | * If PCG_USED bit is not set, page_cgroup is not added to this private LRU. |
| 872 | * When moving account, the page is not on LRU. It's isolated. |
| 873 | */ |
| 874 | |
| 875 | 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] | 876 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 877 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 878 | struct mem_cgroup_per_zone *mz; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 879 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 880 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 881 | return; |
| 882 | pc = lookup_page_cgroup(page); |
| 883 | /* can happen while we handle swapcache. */ |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 884 | if (!TestClearPageCgroupAcctLRU(pc)) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 885 | return; |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 886 | VM_BUG_ON(!pc->mem_cgroup); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 887 | /* |
| 888 | * We don't check PCG_USED bit. It's cleared when the "page" is finally |
| 889 | * removed from global LRU. |
| 890 | */ |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 891 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 892 | /* huge page split is done under lru_lock. so, we have no races. */ |
| 893 | MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 894 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 895 | return; |
| 896 | VM_BUG_ON(list_empty(&pc->lru)); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 897 | list_del_init(&pc->lru); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 898 | } |
| 899 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 900 | void mem_cgroup_del_lru(struct page *page) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 901 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 902 | mem_cgroup_del_lru_list(page, page_lru(page)); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 903 | } |
| 904 | |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 905 | /* |
| 906 | * Writeback is about to end against a page which has been marked for immediate |
| 907 | * reclaim. If it still appears to be reclaimable, move it to the tail of the |
| 908 | * inactive list. |
| 909 | */ |
| 910 | void mem_cgroup_rotate_reclaimable_page(struct page *page) |
| 911 | { |
| 912 | struct mem_cgroup_per_zone *mz; |
| 913 | struct page_cgroup *pc; |
| 914 | enum lru_list lru = page_lru(page); |
| 915 | |
| 916 | if (mem_cgroup_disabled()) |
| 917 | return; |
| 918 | |
| 919 | pc = lookup_page_cgroup(page); |
| 920 | /* unused or root page is not rotated. */ |
| 921 | if (!PageCgroupUsed(pc)) |
| 922 | return; |
| 923 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 924 | smp_rmb(); |
| 925 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 926 | return; |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 927 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 928 | list_move_tail(&pc->lru, &mz->lists[lru]); |
| 929 | } |
| 930 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 931 | 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] | 932 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 933 | struct mem_cgroup_per_zone *mz; |
| 934 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 935 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 936 | if (mem_cgroup_disabled()) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 937 | return; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 938 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 939 | pc = lookup_page_cgroup(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 940 | /* unused or root page is not rotated. */ |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 941 | if (!PageCgroupUsed(pc)) |
| 942 | return; |
| 943 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 944 | smp_rmb(); |
| 945 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 946 | return; |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 947 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 948 | list_move(&pc->lru, &mz->lists[lru]); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 949 | } |
| 950 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 951 | void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru) |
| 952 | { |
| 953 | struct page_cgroup *pc; |
| 954 | struct mem_cgroup_per_zone *mz; |
| 955 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 956 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 957 | return; |
| 958 | pc = lookup_page_cgroup(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 959 | VM_BUG_ON(PageCgroupAcctLRU(pc)); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 960 | if (!PageCgroupUsed(pc)) |
| 961 | return; |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 962 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 963 | smp_rmb(); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 964 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 965 | /* huge page split is done under lru_lock. so, we have no races. */ |
| 966 | MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 967 | SetPageCgroupAcctLRU(pc); |
| 968 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 969 | return; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 970 | list_add(&pc->lru, &mz->lists[lru]); |
| 971 | } |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 972 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 973 | /* |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 974 | * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed |
| 975 | * while it's linked to lru because the page may be reused after it's fully |
| 976 | * uncharged. To handle that, unlink page_cgroup from LRU when charge it again. |
| 977 | * 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] | 978 | */ |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 979 | static void mem_cgroup_lru_del_before_commit(struct page *page) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 980 | { |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 981 | unsigned long flags; |
| 982 | struct zone *zone = page_zone(page); |
| 983 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 984 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 985 | /* |
| 986 | * Doing this check without taking ->lru_lock seems wrong but this |
| 987 | * is safe. Because if page_cgroup's USED bit is unset, the page |
| 988 | * will not be added to any memcg's LRU. If page_cgroup's USED bit is |
| 989 | * set, the commit after this will fail, anyway. |
| 990 | * This all charge/uncharge is done under some mutual execustion. |
| 991 | * So, we don't need to taking care of changes in USED bit. |
| 992 | */ |
| 993 | if (likely(!PageLRU(page))) |
| 994 | return; |
| 995 | |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 996 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 997 | /* |
| 998 | * Forget old LRU when this page_cgroup is *not* used. This Used bit |
| 999 | * is guarded by lock_page() because the page is SwapCache. |
| 1000 | */ |
| 1001 | if (!PageCgroupUsed(pc)) |
| 1002 | mem_cgroup_del_lru_list(page, page_lru(page)); |
| 1003 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1006 | static void mem_cgroup_lru_add_after_commit(struct page *page) |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1007 | { |
| 1008 | unsigned long flags; |
| 1009 | struct zone *zone = page_zone(page); |
| 1010 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 1011 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1012 | /* taking care of that the page is added to LRU while we commit it */ |
| 1013 | if (likely(!PageLRU(page))) |
| 1014 | return; |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1015 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 1016 | /* 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] | 1017 | if (PageLRU(page) && !PageCgroupAcctLRU(pc)) |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1018 | mem_cgroup_add_lru_list(page, page_lru(page)); |
| 1019 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
| 1020 | } |
| 1021 | |
| 1022 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1023 | void mem_cgroup_move_lists(struct page *page, |
| 1024 | enum lru_list from, enum lru_list to) |
| 1025 | { |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1026 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1027 | return; |
| 1028 | mem_cgroup_del_lru_list(page, from); |
| 1029 | mem_cgroup_add_lru_list(page, to); |
| 1030 | } |
| 1031 | |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1032 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
| 1033 | { |
| 1034 | int ret; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1035 | struct mem_cgroup *curr = NULL; |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1036 | struct task_struct *p; |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1037 | |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1038 | p = find_lock_task_mm(task); |
| 1039 | if (!p) |
| 1040 | return 0; |
| 1041 | curr = try_get_mem_cgroup_from_mm(p->mm); |
| 1042 | task_unlock(p); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1043 | if (!curr) |
| 1044 | return 0; |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1045 | /* |
| 1046 | * We should check use_hierarchy of "mem" not "curr". Because checking |
| 1047 | * use_hierarchy of "curr" here make this function true if hierarchy is |
| 1048 | * enabled in "curr" and "curr" is a child of "mem" in *cgroup* |
| 1049 | * hierarchy(even if use_hierarchy is disabled in "mem"). |
| 1050 | */ |
| 1051 | if (mem->use_hierarchy) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1052 | ret = css_is_ancestor(&curr->css, &mem->css); |
| 1053 | else |
| 1054 | ret = (curr == mem); |
| 1055 | css_put(&curr->css); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1056 | return ret; |
| 1057 | } |
| 1058 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1059 | static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages) |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1060 | { |
| 1061 | unsigned long active; |
| 1062 | unsigned long inactive; |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1063 | unsigned long gb; |
| 1064 | unsigned long inactive_ratio; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1065 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 1066 | inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON); |
| 1067 | active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1068 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1069 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
| 1070 | if (gb) |
| 1071 | inactive_ratio = int_sqrt(10 * gb); |
| 1072 | else |
| 1073 | inactive_ratio = 1; |
| 1074 | |
| 1075 | if (present_pages) { |
| 1076 | present_pages[0] = inactive; |
| 1077 | present_pages[1] = active; |
| 1078 | } |
| 1079 | |
| 1080 | return inactive_ratio; |
| 1081 | } |
| 1082 | |
| 1083 | int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg) |
| 1084 | { |
| 1085 | unsigned long active; |
| 1086 | unsigned long inactive; |
| 1087 | unsigned long present_pages[2]; |
| 1088 | unsigned long inactive_ratio; |
| 1089 | |
| 1090 | inactive_ratio = calc_inactive_ratio(memcg, present_pages); |
| 1091 | |
| 1092 | inactive = present_pages[0]; |
| 1093 | active = present_pages[1]; |
| 1094 | |
| 1095 | if (inactive * inactive_ratio < active) |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1096 | return 1; |
| 1097 | |
| 1098 | return 0; |
| 1099 | } |
| 1100 | |
Rik van Riel | 56e49d2 | 2009-06-16 15:32:28 -0700 | [diff] [blame] | 1101 | int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg) |
| 1102 | { |
| 1103 | unsigned long active; |
| 1104 | unsigned long inactive; |
| 1105 | |
| 1106 | inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE); |
| 1107 | active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE); |
| 1108 | |
| 1109 | return (active > inactive); |
| 1110 | } |
| 1111 | |
Ying Han | 1bac180 | 2011-05-26 16:25:36 -0700 | [diff] [blame] | 1112 | unsigned long mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, |
| 1113 | struct zone *zone, |
| 1114 | enum lru_list lru) |
KOSAKI Motohiro | a3d8e05 | 2009-01-07 18:08:19 -0800 | [diff] [blame] | 1115 | { |
KOSAKI Motohiro | 13d7e3a | 2010-08-10 18:03:06 -0700 | [diff] [blame] | 1116 | int nid = zone_to_nid(zone); |
KOSAKI Motohiro | a3d8e05 | 2009-01-07 18:08:19 -0800 | [diff] [blame] | 1117 | int zid = zone_idx(zone); |
| 1118 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 1119 | |
| 1120 | return MEM_CGROUP_ZSTAT(mz, lru); |
| 1121 | } |
| 1122 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 1123 | static unsigned long mem_cgroup_node_nr_file_lru_pages(struct mem_cgroup *memcg, |
| 1124 | int nid) |
| 1125 | { |
| 1126 | unsigned long ret; |
| 1127 | |
| 1128 | ret = mem_cgroup_get_zonestat_node(memcg, nid, LRU_INACTIVE_FILE) + |
| 1129 | mem_cgroup_get_zonestat_node(memcg, nid, LRU_ACTIVE_FILE); |
| 1130 | |
| 1131 | return ret; |
| 1132 | } |
| 1133 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame^] | 1134 | static unsigned long mem_cgroup_node_nr_anon_lru_pages(struct mem_cgroup *memcg, |
| 1135 | int nid) |
| 1136 | { |
| 1137 | unsigned long ret; |
| 1138 | |
| 1139 | ret = mem_cgroup_get_zonestat_node(memcg, nid, LRU_INACTIVE_ANON) + |
| 1140 | mem_cgroup_get_zonestat_node(memcg, nid, LRU_ACTIVE_ANON); |
| 1141 | return ret; |
| 1142 | } |
| 1143 | |
| 1144 | #if MAX_NUMNODES > 1 |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 1145 | static unsigned long mem_cgroup_nr_file_lru_pages(struct mem_cgroup *memcg) |
| 1146 | { |
| 1147 | u64 total = 0; |
| 1148 | int nid; |
| 1149 | |
| 1150 | for_each_node_state(nid, N_HIGH_MEMORY) |
| 1151 | total += mem_cgroup_node_nr_file_lru_pages(memcg, nid); |
| 1152 | |
| 1153 | return total; |
| 1154 | } |
| 1155 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 1156 | static unsigned long mem_cgroup_nr_anon_lru_pages(struct mem_cgroup *memcg) |
| 1157 | { |
| 1158 | u64 total = 0; |
| 1159 | int nid; |
| 1160 | |
| 1161 | for_each_node_state(nid, N_HIGH_MEMORY) |
| 1162 | total += mem_cgroup_node_nr_anon_lru_pages(memcg, nid); |
| 1163 | |
| 1164 | return total; |
| 1165 | } |
| 1166 | |
| 1167 | static unsigned long |
| 1168 | mem_cgroup_node_nr_unevictable_lru_pages(struct mem_cgroup *memcg, int nid) |
| 1169 | { |
| 1170 | return mem_cgroup_get_zonestat_node(memcg, nid, LRU_UNEVICTABLE); |
| 1171 | } |
| 1172 | |
| 1173 | static unsigned long |
| 1174 | mem_cgroup_nr_unevictable_lru_pages(struct mem_cgroup *memcg) |
| 1175 | { |
| 1176 | u64 total = 0; |
| 1177 | int nid; |
| 1178 | |
| 1179 | for_each_node_state(nid, N_HIGH_MEMORY) |
| 1180 | total += mem_cgroup_node_nr_unevictable_lru_pages(memcg, nid); |
| 1181 | |
| 1182 | return total; |
| 1183 | } |
| 1184 | |
| 1185 | static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg, |
| 1186 | int nid) |
| 1187 | { |
| 1188 | enum lru_list l; |
| 1189 | u64 total = 0; |
| 1190 | |
| 1191 | for_each_lru(l) |
| 1192 | total += mem_cgroup_get_zonestat_node(memcg, nid, l); |
| 1193 | |
| 1194 | return total; |
| 1195 | } |
| 1196 | |
| 1197 | static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg) |
| 1198 | { |
| 1199 | u64 total = 0; |
| 1200 | int nid; |
| 1201 | |
| 1202 | for_each_node_state(nid, N_HIGH_MEMORY) |
| 1203 | total += mem_cgroup_node_nr_lru_pages(memcg, nid); |
| 1204 | |
| 1205 | return total; |
| 1206 | } |
| 1207 | #endif /* CONFIG_NUMA */ |
| 1208 | |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1209 | struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg, |
| 1210 | struct zone *zone) |
| 1211 | { |
KOSAKI Motohiro | 13d7e3a | 2010-08-10 18:03:06 -0700 | [diff] [blame] | 1212 | int nid = zone_to_nid(zone); |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1213 | int zid = zone_idx(zone); |
| 1214 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 1215 | |
| 1216 | return &mz->reclaim_stat; |
| 1217 | } |
| 1218 | |
| 1219 | struct zone_reclaim_stat * |
| 1220 | mem_cgroup_get_reclaim_stat_from_page(struct page *page) |
| 1221 | { |
| 1222 | struct page_cgroup *pc; |
| 1223 | struct mem_cgroup_per_zone *mz; |
| 1224 | |
| 1225 | if (mem_cgroup_disabled()) |
| 1226 | return NULL; |
| 1227 | |
| 1228 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 1229 | if (!PageCgroupUsed(pc)) |
| 1230 | return NULL; |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 1231 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 1232 | smp_rmb(); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1233 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1234 | return &mz->reclaim_stat; |
| 1235 | } |
| 1236 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1237 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 1238 | struct list_head *dst, |
| 1239 | unsigned long *scanned, int order, |
| 1240 | int mode, struct zone *z, |
| 1241 | struct mem_cgroup *mem_cont, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1242 | int active, int file) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1243 | { |
| 1244 | unsigned long nr_taken = 0; |
| 1245 | struct page *page; |
| 1246 | unsigned long scan; |
| 1247 | LIST_HEAD(pc_list); |
| 1248 | struct list_head *src; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1249 | struct page_cgroup *pc, *tmp; |
KOSAKI Motohiro | 13d7e3a | 2010-08-10 18:03:06 -0700 | [diff] [blame] | 1250 | int nid = zone_to_nid(z); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1251 | int zid = zone_idx(z); |
| 1252 | struct mem_cgroup_per_zone *mz; |
Johannes Weiner | b7c46d1 | 2009-09-21 17:02:56 -0700 | [diff] [blame] | 1253 | int lru = LRU_FILE * file + active; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1254 | int ret; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1255 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 1256 | BUG_ON(!mem_cont); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1257 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1258 | src = &mz->lists[lru]; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1259 | |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1260 | scan = 0; |
| 1261 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1262 | if (scan >= nr_to_scan) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1263 | break; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1264 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1265 | if (unlikely(!PageCgroupUsed(pc))) |
| 1266 | continue; |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 1267 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 1268 | page = lookup_cgroup_page(pc); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 1269 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1270 | if (unlikely(!PageLRU(page))) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1271 | continue; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1272 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1273 | scan++; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1274 | ret = __isolate_lru_page(page, mode, file); |
| 1275 | switch (ret) { |
| 1276 | case 0: |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1277 | list_move(&page->lru, dst); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1278 | mem_cgroup_del_lru(page); |
Rik van Riel | 2c888cf | 2011-01-13 15:47:13 -0800 | [diff] [blame] | 1279 | nr_taken += hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1280 | break; |
| 1281 | case -EBUSY: |
| 1282 | /* we don't affect global LRU but rotate in our LRU */ |
| 1283 | mem_cgroup_rotate_lru_list(page, page_lru(page)); |
| 1284 | break; |
| 1285 | default: |
| 1286 | break; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1290 | *scanned = scan; |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 1291 | |
| 1292 | trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken, |
| 1293 | 0, 0, 0, mode); |
| 1294 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1295 | return nr_taken; |
| 1296 | } |
| 1297 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1298 | #define mem_cgroup_from_res_counter(counter, member) \ |
| 1299 | container_of(counter, struct mem_cgroup, member) |
| 1300 | |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1301 | /** |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1302 | * mem_cgroup_margin - calculate chargeable space of a memory cgroup |
| 1303 | * @mem: the memory cgroup |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1304 | * |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1305 | * Returns the maximum amount of memory @mem can be charged with, in |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1306 | * pages. |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1307 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1308 | static unsigned long mem_cgroup_margin(struct mem_cgroup *mem) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1309 | { |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1310 | unsigned long long margin; |
| 1311 | |
| 1312 | margin = res_counter_margin(&mem->res); |
| 1313 | if (do_swap_account) |
| 1314 | margin = min(margin, res_counter_margin(&mem->memsw)); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1315 | return margin >> PAGE_SHIFT; |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1316 | } |
| 1317 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1318 | static unsigned int get_swappiness(struct mem_cgroup *memcg) |
| 1319 | { |
| 1320 | struct cgroup *cgrp = memcg->css.cgroup; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1321 | |
| 1322 | /* root ? */ |
| 1323 | if (cgrp->parent == NULL) |
| 1324 | return vm_swappiness; |
| 1325 | |
Johannes Weiner | bf1ff26 | 2011-03-23 16:42:32 -0700 | [diff] [blame] | 1326 | return memcg->swappiness; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1327 | } |
| 1328 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1329 | static void mem_cgroup_start_move(struct mem_cgroup *mem) |
| 1330 | { |
| 1331 | int cpu; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1332 | |
| 1333 | get_online_cpus(); |
| 1334 | spin_lock(&mem->pcp_counter_lock); |
| 1335 | for_each_online_cpu(cpu) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1336 | per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1337 | mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1; |
| 1338 | spin_unlock(&mem->pcp_counter_lock); |
| 1339 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1340 | |
| 1341 | synchronize_rcu(); |
| 1342 | } |
| 1343 | |
| 1344 | static void mem_cgroup_end_move(struct mem_cgroup *mem) |
| 1345 | { |
| 1346 | int cpu; |
| 1347 | |
| 1348 | if (!mem) |
| 1349 | return; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1350 | get_online_cpus(); |
| 1351 | spin_lock(&mem->pcp_counter_lock); |
| 1352 | for_each_online_cpu(cpu) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1353 | per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1354 | mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1; |
| 1355 | spin_unlock(&mem->pcp_counter_lock); |
| 1356 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1357 | } |
| 1358 | /* |
| 1359 | * 2 routines for checking "mem" is under move_account() or not. |
| 1360 | * |
| 1361 | * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used |
| 1362 | * for avoiding race in accounting. If true, |
| 1363 | * pc->mem_cgroup may be overwritten. |
| 1364 | * |
| 1365 | * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or |
| 1366 | * under hierarchy of moving cgroups. This is for |
| 1367 | * waiting at hith-memory prressure caused by "move". |
| 1368 | */ |
| 1369 | |
| 1370 | static bool mem_cgroup_stealed(struct mem_cgroup *mem) |
| 1371 | { |
| 1372 | VM_BUG_ON(!rcu_read_lock_held()); |
| 1373 | return this_cpu_read(mem->stat->count[MEM_CGROUP_ON_MOVE]) > 0; |
| 1374 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1375 | |
| 1376 | static bool mem_cgroup_under_move(struct mem_cgroup *mem) |
| 1377 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1378 | struct mem_cgroup *from; |
| 1379 | struct mem_cgroup *to; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1380 | bool ret = false; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1381 | /* |
| 1382 | * Unlike task_move routines, we access mc.to, mc.from not under |
| 1383 | * mutual exclusion by cgroup_mutex. Here, we take spinlock instead. |
| 1384 | */ |
| 1385 | spin_lock(&mc.lock); |
| 1386 | from = mc.from; |
| 1387 | to = mc.to; |
| 1388 | if (!from) |
| 1389 | goto unlock; |
| 1390 | if (from == mem || to == mem |
| 1391 | || (mem->use_hierarchy && css_is_ancestor(&from->css, &mem->css)) |
| 1392 | || (mem->use_hierarchy && css_is_ancestor(&to->css, &mem->css))) |
| 1393 | ret = true; |
| 1394 | unlock: |
| 1395 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1396 | return ret; |
| 1397 | } |
| 1398 | |
| 1399 | static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem) |
| 1400 | { |
| 1401 | if (mc.moving_task && current != mc.moving_task) { |
| 1402 | if (mem_cgroup_under_move(mem)) { |
| 1403 | DEFINE_WAIT(wait); |
| 1404 | prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE); |
| 1405 | /* moving charge context might have finished. */ |
| 1406 | if (mc.moving_task) |
| 1407 | schedule(); |
| 1408 | finish_wait(&mc.waitq, &wait); |
| 1409 | return true; |
| 1410 | } |
| 1411 | } |
| 1412 | return false; |
| 1413 | } |
| 1414 | |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1415 | /** |
Kirill A. Shutemov | 6a6135b | 2010-03-10 15:22:25 -0800 | [diff] [blame] | 1416 | * 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] | 1417 | * @memcg: The memory cgroup that went over limit |
| 1418 | * @p: Task that is going to be killed |
| 1419 | * |
| 1420 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is |
| 1421 | * enabled |
| 1422 | */ |
| 1423 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) |
| 1424 | { |
| 1425 | struct cgroup *task_cgrp; |
| 1426 | struct cgroup *mem_cgrp; |
| 1427 | /* |
| 1428 | * Need a buffer in BSS, can't rely on allocations. The code relies |
| 1429 | * on the assumption that OOM is serialized for memory controller. |
| 1430 | * If this assumption is broken, revisit this code. |
| 1431 | */ |
| 1432 | static char memcg_name[PATH_MAX]; |
| 1433 | int ret; |
| 1434 | |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1435 | if (!memcg || !p) |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1436 | return; |
| 1437 | |
| 1438 | |
| 1439 | rcu_read_lock(); |
| 1440 | |
| 1441 | mem_cgrp = memcg->css.cgroup; |
| 1442 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); |
| 1443 | |
| 1444 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); |
| 1445 | if (ret < 0) { |
| 1446 | /* |
| 1447 | * Unfortunately, we are unable to convert to a useful name |
| 1448 | * But we'll still print out the usage information |
| 1449 | */ |
| 1450 | rcu_read_unlock(); |
| 1451 | goto done; |
| 1452 | } |
| 1453 | rcu_read_unlock(); |
| 1454 | |
| 1455 | printk(KERN_INFO "Task in %s killed", memcg_name); |
| 1456 | |
| 1457 | rcu_read_lock(); |
| 1458 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); |
| 1459 | if (ret < 0) { |
| 1460 | rcu_read_unlock(); |
| 1461 | goto done; |
| 1462 | } |
| 1463 | rcu_read_unlock(); |
| 1464 | |
| 1465 | /* |
| 1466 | * Continues from above, so we don't need an KERN_ level |
| 1467 | */ |
| 1468 | printk(KERN_CONT " as a result of limit of %s\n", memcg_name); |
| 1469 | done: |
| 1470 | |
| 1471 | printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n", |
| 1472 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, |
| 1473 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, |
| 1474 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); |
| 1475 | printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, " |
| 1476 | "failcnt %llu\n", |
| 1477 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, |
| 1478 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, |
| 1479 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); |
| 1480 | } |
| 1481 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1482 | /* |
| 1483 | * This function returns the number of memcg under hierarchy tree. Returns |
| 1484 | * 1(self count) if no children. |
| 1485 | */ |
| 1486 | static int mem_cgroup_count_children(struct mem_cgroup *mem) |
| 1487 | { |
| 1488 | int num = 0; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1489 | struct mem_cgroup *iter; |
| 1490 | |
| 1491 | for_each_mem_cgroup_tree(iter, mem) |
| 1492 | num++; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1493 | return num; |
| 1494 | } |
| 1495 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1496 | /* |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1497 | * Return the memory (and swap, if configured) limit for a memcg. |
| 1498 | */ |
| 1499 | u64 mem_cgroup_get_limit(struct mem_cgroup *memcg) |
| 1500 | { |
| 1501 | u64 limit; |
| 1502 | u64 memsw; |
| 1503 | |
Johannes Weiner | f3e8eb7 | 2011-01-13 15:47:39 -0800 | [diff] [blame] | 1504 | limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 1505 | limit += total_swap_pages << PAGE_SHIFT; |
| 1506 | |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1507 | memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 1508 | /* |
| 1509 | * If memsw is finite and limits the amount of swap space available |
| 1510 | * to this memcg, return that limit. |
| 1511 | */ |
| 1512 | return min(limit, memsw); |
| 1513 | } |
| 1514 | |
| 1515 | /* |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1516 | * Visit the first child (need not be the first child as per the ordering |
| 1517 | * of the cgroup list, since we track last_scanned_child) of @mem and use |
| 1518 | * that to reclaim free pages from. |
| 1519 | */ |
| 1520 | static struct mem_cgroup * |
| 1521 | mem_cgroup_select_victim(struct mem_cgroup *root_mem) |
| 1522 | { |
| 1523 | struct mem_cgroup *ret = NULL; |
| 1524 | struct cgroup_subsys_state *css; |
| 1525 | int nextid, found; |
| 1526 | |
| 1527 | if (!root_mem->use_hierarchy) { |
| 1528 | css_get(&root_mem->css); |
| 1529 | ret = root_mem; |
| 1530 | } |
| 1531 | |
| 1532 | while (!ret) { |
| 1533 | rcu_read_lock(); |
| 1534 | nextid = root_mem->last_scanned_child + 1; |
| 1535 | css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css, |
| 1536 | &found); |
| 1537 | if (css && css_tryget(css)) |
| 1538 | ret = container_of(css, struct mem_cgroup, css); |
| 1539 | |
| 1540 | rcu_read_unlock(); |
| 1541 | /* Updates scanning parameter */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1542 | if (!css) { |
| 1543 | /* this means start scan from ID:1 */ |
| 1544 | root_mem->last_scanned_child = 0; |
| 1545 | } else |
| 1546 | root_mem->last_scanned_child = found; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | return ret; |
| 1550 | } |
| 1551 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame^] | 1552 | /** |
| 1553 | * test_mem_cgroup_node_reclaimable |
| 1554 | * @mem: the target memcg |
| 1555 | * @nid: the node ID to be checked. |
| 1556 | * @noswap : specify true here if the user wants flle only information. |
| 1557 | * |
| 1558 | * This function returns whether the specified memcg contains any |
| 1559 | * reclaimable pages on a node. Returns true if there are any reclaimable |
| 1560 | * pages in the node. |
| 1561 | */ |
| 1562 | static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *mem, |
| 1563 | int nid, bool noswap) |
| 1564 | { |
| 1565 | if (mem_cgroup_node_nr_file_lru_pages(mem, nid)) |
| 1566 | return true; |
| 1567 | if (noswap || !total_swap_pages) |
| 1568 | return false; |
| 1569 | if (mem_cgroup_node_nr_anon_lru_pages(mem, nid)) |
| 1570 | return true; |
| 1571 | return false; |
| 1572 | |
| 1573 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1574 | #if MAX_NUMNODES > 1 |
| 1575 | |
| 1576 | /* |
| 1577 | * Always updating the nodemask is not very good - even if we have an empty |
| 1578 | * list or the wrong list here, we can start from some node and traverse all |
| 1579 | * nodes based on the zonelist. So update the list loosely once per 10 secs. |
| 1580 | * |
| 1581 | */ |
| 1582 | static void mem_cgroup_may_update_nodemask(struct mem_cgroup *mem) |
| 1583 | { |
| 1584 | int nid; |
| 1585 | |
| 1586 | if (time_after(mem->next_scan_node_update, jiffies)) |
| 1587 | return; |
| 1588 | |
| 1589 | mem->next_scan_node_update = jiffies + 10*HZ; |
| 1590 | /* make a nodemask where this memcg uses memory from */ |
| 1591 | mem->scan_nodes = node_states[N_HIGH_MEMORY]; |
| 1592 | |
| 1593 | for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) { |
| 1594 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame^] | 1595 | if (!test_mem_cgroup_node_reclaimable(mem, nid, false)) |
| 1596 | node_clear(nid, mem->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | /* |
| 1601 | * Selecting a node where we start reclaim from. Because what we need is just |
| 1602 | * reducing usage counter, start from anywhere is O,K. Considering |
| 1603 | * memory reclaim from current node, there are pros. and cons. |
| 1604 | * |
| 1605 | * Freeing memory from current node means freeing memory from a node which |
| 1606 | * we'll use or we've used. So, it may make LRU bad. And if several threads |
| 1607 | * hit limits, it will see a contention on a node. But freeing from remote |
| 1608 | * node means more costs for memory reclaim because of memory latency. |
| 1609 | * |
| 1610 | * Now, we use round-robin. Better algorithm is welcomed. |
| 1611 | */ |
| 1612 | int mem_cgroup_select_victim_node(struct mem_cgroup *mem) |
| 1613 | { |
| 1614 | int node; |
| 1615 | |
| 1616 | mem_cgroup_may_update_nodemask(mem); |
| 1617 | node = mem->last_scanned_node; |
| 1618 | |
| 1619 | node = next_node(node, mem->scan_nodes); |
| 1620 | if (node == MAX_NUMNODES) |
| 1621 | node = first_node(mem->scan_nodes); |
| 1622 | /* |
| 1623 | * We call this when we hit limit, not when pages are added to LRU. |
| 1624 | * No LRU may hold pages because all pages are UNEVICTABLE or |
| 1625 | * memcg is too small and all pages are not on LRU. In that case, |
| 1626 | * we use curret node. |
| 1627 | */ |
| 1628 | if (unlikely(node == MAX_NUMNODES)) |
| 1629 | node = numa_node_id(); |
| 1630 | |
| 1631 | mem->last_scanned_node = node; |
| 1632 | return node; |
| 1633 | } |
| 1634 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame^] | 1635 | /* |
| 1636 | * Check all nodes whether it contains reclaimable pages or not. |
| 1637 | * For quick scan, we make use of scan_nodes. This will allow us to skip |
| 1638 | * unused nodes. But scan_nodes is lazily updated and may not cotain |
| 1639 | * enough new information. We need to do double check. |
| 1640 | */ |
| 1641 | bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap) |
| 1642 | { |
| 1643 | int nid; |
| 1644 | |
| 1645 | /* |
| 1646 | * quick check...making use of scan_node. |
| 1647 | * We can skip unused nodes. |
| 1648 | */ |
| 1649 | if (!nodes_empty(mem->scan_nodes)) { |
| 1650 | for (nid = first_node(mem->scan_nodes); |
| 1651 | nid < MAX_NUMNODES; |
| 1652 | nid = next_node(nid, mem->scan_nodes)) { |
| 1653 | |
| 1654 | if (test_mem_cgroup_node_reclaimable(mem, nid, noswap)) |
| 1655 | return true; |
| 1656 | } |
| 1657 | } |
| 1658 | /* |
| 1659 | * Check rest of nodes. |
| 1660 | */ |
| 1661 | for_each_node_state(nid, N_HIGH_MEMORY) { |
| 1662 | if (node_isset(nid, mem->scan_nodes)) |
| 1663 | continue; |
| 1664 | if (test_mem_cgroup_node_reclaimable(mem, nid, noswap)) |
| 1665 | return true; |
| 1666 | } |
| 1667 | return false; |
| 1668 | } |
| 1669 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1670 | #else |
| 1671 | int mem_cgroup_select_victim_node(struct mem_cgroup *mem) |
| 1672 | { |
| 1673 | return 0; |
| 1674 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame^] | 1675 | |
| 1676 | bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap) |
| 1677 | { |
| 1678 | return test_mem_cgroup_node_reclaimable(mem, 0, noswap); |
| 1679 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1680 | #endif |
| 1681 | |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1682 | /* |
| 1683 | * Scan the hierarchy if needed to reclaim memory. We remember the last child |
| 1684 | * we reclaimed from, so that we don't end up penalizing one child extensively |
| 1685 | * based on its position in the children list. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1686 | * |
| 1687 | * root_mem is the original ancestor that we've been reclaim from. |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1688 | * |
| 1689 | * We give up and return to the caller when we visit root_mem twice. |
| 1690 | * (other groups can be removed while we're walking....) |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1691 | * |
| 1692 | * If shrink==true, for avoiding to free too much, this returns immedieately. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1693 | */ |
| 1694 | static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem, |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1695 | struct zone *zone, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1696 | gfp_t gfp_mask, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1697 | unsigned long reclaim_options, |
| 1698 | unsigned long *total_scanned) |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1699 | { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1700 | struct mem_cgroup *victim; |
| 1701 | int ret, total = 0; |
| 1702 | int loop = 0; |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1703 | bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP; |
| 1704 | bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1705 | bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1706 | unsigned long excess; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1707 | unsigned long nr_scanned; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1708 | |
| 1709 | excess = res_counter_soft_limit_excess(&root_mem->res) >> PAGE_SHIFT; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1710 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1711 | /* If memsw_is_minimum==1, swap-out is of-no-use. */ |
KAMEZAWA Hiroyuki | 7ae534d | 2011-06-15 15:08:44 -0700 | [diff] [blame] | 1712 | if (!check_soft && root_mem->memsw_is_minimum) |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1713 | noswap = true; |
| 1714 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1715 | while (1) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1716 | victim = mem_cgroup_select_victim(root_mem); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1717 | if (victim == root_mem) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1718 | loop++; |
KAMEZAWA Hiroyuki | fbc29a2 | 2011-06-15 15:08:46 -0700 | [diff] [blame] | 1719 | /* |
| 1720 | * We are not draining per cpu cached charges during |
| 1721 | * soft limit reclaim because global reclaim doesn't |
| 1722 | * care about charges. It tries to free some memory and |
| 1723 | * charges will not give any. |
| 1724 | */ |
| 1725 | if (!check_soft && loop >= 1) |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 1726 | drain_all_stock_async(root_mem); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1727 | if (loop >= 2) { |
| 1728 | /* |
| 1729 | * If we have not been able to reclaim |
| 1730 | * anything, it might because there are |
| 1731 | * no reclaimable pages under this hierarchy |
| 1732 | */ |
| 1733 | if (!check_soft || !total) { |
| 1734 | css_put(&victim->css); |
| 1735 | break; |
| 1736 | } |
| 1737 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1738 | * We want to do more targeted reclaim. |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1739 | * excess >> 2 is not to excessive so as to |
| 1740 | * reclaim too much, nor too less that we keep |
| 1741 | * coming back to reclaim from this cgroup |
| 1742 | */ |
| 1743 | if (total >= (excess >> 2) || |
| 1744 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) { |
| 1745 | css_put(&victim->css); |
| 1746 | break; |
| 1747 | } |
| 1748 | } |
| 1749 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame^] | 1750 | if (!mem_cgroup_reclaimable(victim, noswap)) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1751 | /* this cgroup's local usage == 0 */ |
| 1752 | css_put(&victim->css); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1753 | continue; |
| 1754 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1755 | /* we use swappiness of local cgroup */ |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1756 | if (check_soft) { |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1757 | ret = mem_cgroup_shrink_node_zone(victim, gfp_mask, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1758 | noswap, get_swappiness(victim), zone, |
| 1759 | &nr_scanned); |
| 1760 | *total_scanned += nr_scanned; |
| 1761 | } else |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1762 | ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, |
| 1763 | noswap, get_swappiness(victim)); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1764 | css_put(&victim->css); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1765 | /* |
| 1766 | * At shrinking usage, we can't check we should stop here or |
| 1767 | * reclaim more. It's depends on callers. last_scanned_child |
| 1768 | * will work enough for keeping fairness under tree. |
| 1769 | */ |
| 1770 | if (shrink) |
| 1771 | return ret; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1772 | total += ret; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1773 | if (check_soft) { |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1774 | if (!res_counter_soft_limit_excess(&root_mem->res)) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1775 | return total; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1776 | } else if (mem_cgroup_margin(root_mem)) |
Johannes Weiner | 4fd14eb | 2011-05-26 16:25:35 -0700 | [diff] [blame] | 1777 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1778 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1779 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1780 | } |
| 1781 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1782 | /* |
| 1783 | * Check OOM-Killer is already running under our hierarchy. |
| 1784 | * If someone is running, return false. |
| 1785 | */ |
| 1786 | static bool mem_cgroup_oom_lock(struct mem_cgroup *mem) |
| 1787 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1788 | int x, lock_count = 0; |
| 1789 | struct mem_cgroup *iter; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1790 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1791 | for_each_mem_cgroup_tree(iter, mem) { |
| 1792 | x = atomic_inc_return(&iter->oom_lock); |
| 1793 | lock_count = max(x, lock_count); |
| 1794 | } |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1795 | |
| 1796 | if (lock_count == 1) |
| 1797 | return true; |
| 1798 | return false; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1799 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1800 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1801 | static int mem_cgroup_oom_unlock(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1802 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1803 | struct mem_cgroup *iter; |
| 1804 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1805 | /* |
| 1806 | * When a new child is created while the hierarchy is under oom, |
| 1807 | * mem_cgroup_oom_lock() may not be called. We have to use |
| 1808 | * atomic_add_unless() here. |
| 1809 | */ |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1810 | for_each_mem_cgroup_tree(iter, mem) |
| 1811 | atomic_add_unless(&iter->oom_lock, -1, 0); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1812 | return 0; |
| 1813 | } |
| 1814 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1815 | |
| 1816 | static DEFINE_MUTEX(memcg_oom_mutex); |
| 1817 | static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq); |
| 1818 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1819 | struct oom_wait_info { |
| 1820 | struct mem_cgroup *mem; |
| 1821 | wait_queue_t wait; |
| 1822 | }; |
| 1823 | |
| 1824 | static int memcg_oom_wake_function(wait_queue_t *wait, |
| 1825 | unsigned mode, int sync, void *arg) |
| 1826 | { |
| 1827 | struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg; |
| 1828 | struct oom_wait_info *oom_wait_info; |
| 1829 | |
| 1830 | oom_wait_info = container_of(wait, struct oom_wait_info, wait); |
| 1831 | |
| 1832 | if (oom_wait_info->mem == wake_mem) |
| 1833 | goto wakeup; |
| 1834 | /* if no hierarchy, no match */ |
| 1835 | if (!oom_wait_info->mem->use_hierarchy || !wake_mem->use_hierarchy) |
| 1836 | return 0; |
| 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 | */ |
| 1841 | if (!css_is_ancestor(&oom_wait_info->mem->css, &wake_mem->css) && |
| 1842 | !css_is_ancestor(&wake_mem->css, &oom_wait_info->mem->css)) |
| 1843 | return 0; |
| 1844 | |
| 1845 | wakeup: |
| 1846 | return autoremove_wake_function(wait, mode, sync, arg); |
| 1847 | } |
| 1848 | |
| 1849 | static void memcg_wakeup_oom(struct mem_cgroup *mem) |
| 1850 | { |
| 1851 | /* for filtering, pass "mem" as argument. */ |
| 1852 | __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem); |
| 1853 | } |
| 1854 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1855 | static void memcg_oom_recover(struct mem_cgroup *mem) |
| 1856 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1857 | if (mem && atomic_read(&mem->oom_lock)) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1858 | memcg_wakeup_oom(mem); |
| 1859 | } |
| 1860 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1861 | /* |
| 1862 | * try to call OOM killer. returns false if we should exit memory-reclaim loop. |
| 1863 | */ |
| 1864 | bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask) |
| 1865 | { |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1866 | struct oom_wait_info owait; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1867 | bool locked, need_to_kill; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1868 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1869 | owait.mem = mem; |
| 1870 | owait.wait.flags = 0; |
| 1871 | owait.wait.func = memcg_oom_wake_function; |
| 1872 | owait.wait.private = current; |
| 1873 | INIT_LIST_HEAD(&owait.wait.task_list); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1874 | need_to_kill = true; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1875 | /* At first, try to OOM lock hierarchy under mem.*/ |
| 1876 | mutex_lock(&memcg_oom_mutex); |
| 1877 | locked = mem_cgroup_oom_lock(mem); |
| 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); |
| 1884 | if (!locked || mem->oom_kill_disable) |
| 1885 | need_to_kill = false; |
| 1886 | if (locked) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1887 | mem_cgroup_oom_notify(mem); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1888 | mutex_unlock(&memcg_oom_mutex); |
| 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); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1892 | mem_cgroup_out_of_memory(mem, 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 | } |
| 1897 | mutex_lock(&memcg_oom_mutex); |
| 1898 | mem_cgroup_oom_unlock(mem); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1899 | memcg_wakeup_oom(mem); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1900 | mutex_unlock(&memcg_oom_mutex); |
| 1901 | |
| 1902 | if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current)) |
| 1903 | return false; |
| 1904 | /* Give chance to dying process */ |
| 1905 | schedule_timeout(1); |
| 1906 | return true; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1907 | } |
| 1908 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1909 | /* |
| 1910 | * Currently used to update mapped file statistics, but the routine can be |
| 1911 | * generalized to update other statistics as well. |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1912 | * |
| 1913 | * Notes: Race condition |
| 1914 | * |
| 1915 | * We usually use page_cgroup_lock() for accessing page_cgroup member but |
| 1916 | * it tends to be costly. But considering some conditions, we doesn't need |
| 1917 | * to do so _always_. |
| 1918 | * |
| 1919 | * Considering "charge", lock_page_cgroup() is not required because all |
| 1920 | * file-stat operations happen after a page is attached to radix-tree. There |
| 1921 | * are no race with "charge". |
| 1922 | * |
| 1923 | * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup |
| 1924 | * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even |
| 1925 | * if there are race with "uncharge". Statistics itself is properly handled |
| 1926 | * by flags. |
| 1927 | * |
| 1928 | * Considering "move", this is an only case we see a race. To make the race |
| 1929 | * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are |
| 1930 | * possibility of race condition. If there is, we take a lock. |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1931 | */ |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1932 | |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1933 | void mem_cgroup_update_page_stat(struct page *page, |
| 1934 | enum mem_cgroup_page_stat_item idx, int val) |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1935 | { |
| 1936 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1937 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 1938 | bool need_unlock = false; |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 1939 | unsigned long uninitialized_var(flags); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1940 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1941 | if (unlikely(!pc)) |
| 1942 | return; |
| 1943 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1944 | rcu_read_lock(); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1945 | mem = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1946 | if (unlikely(!mem || !PageCgroupUsed(pc))) |
| 1947 | goto out; |
| 1948 | /* pc->mem_cgroup is unstable ? */ |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 1949 | if (unlikely(mem_cgroup_stealed(mem)) || PageTransHuge(page)) { |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1950 | /* take a lock against to access pc->mem_cgroup */ |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 1951 | move_lock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1952 | need_unlock = true; |
| 1953 | mem = pc->mem_cgroup; |
| 1954 | if (!mem || !PageCgroupUsed(pc)) |
| 1955 | goto out; |
| 1956 | } |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1957 | |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1958 | switch (idx) { |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1959 | case MEMCG_NR_FILE_MAPPED: |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1960 | if (val > 0) |
| 1961 | SetPageCgroupFileMapped(pc); |
| 1962 | else if (!page_mapped(page)) |
KAMEZAWA Hiroyuki | 0c270f8 | 2010-10-27 15:33:39 -0700 | [diff] [blame] | 1963 | ClearPageCgroupFileMapped(pc); |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1964 | idx = MEM_CGROUP_STAT_FILE_MAPPED; |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1965 | break; |
| 1966 | default: |
| 1967 | BUG(); |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 1968 | } |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1969 | |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1970 | this_cpu_add(mem->stat->count[idx], val); |
| 1971 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1972 | out: |
| 1973 | if (unlikely(need_unlock)) |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 1974 | move_unlock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1975 | rcu_read_unlock(); |
| 1976 | return; |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1977 | } |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1978 | EXPORT_SYMBOL(mem_cgroup_update_page_stat); |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1979 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1980 | /* |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1981 | * size of first charge trial. "32" comes from vmscan.c's magic value. |
| 1982 | * TODO: maybe necessary to use big numbers in big irons. |
| 1983 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1984 | #define CHARGE_BATCH 32U |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1985 | struct memcg_stock_pcp { |
| 1986 | struct mem_cgroup *cached; /* this never be root cgroup */ |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 1987 | unsigned int nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1988 | struct work_struct work; |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 1989 | unsigned long flags; |
| 1990 | #define FLUSHING_CACHED_CHARGE (0) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1991 | }; |
| 1992 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 1993 | static DEFINE_MUTEX(percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1994 | |
| 1995 | /* |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 1996 | * 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] | 1997 | * from local stock and true is returned. If the stock is 0 or charges from a |
| 1998 | * cgroup which is not current target, returns false. This stock will be |
| 1999 | * refilled. |
| 2000 | */ |
| 2001 | static bool consume_stock(struct mem_cgroup *mem) |
| 2002 | { |
| 2003 | struct memcg_stock_pcp *stock; |
| 2004 | bool ret = true; |
| 2005 | |
| 2006 | stock = &get_cpu_var(memcg_stock); |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2007 | if (mem == stock->cached && stock->nr_pages) |
| 2008 | stock->nr_pages--; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2009 | else /* need to call res_counter_charge */ |
| 2010 | ret = false; |
| 2011 | put_cpu_var(memcg_stock); |
| 2012 | return ret; |
| 2013 | } |
| 2014 | |
| 2015 | /* |
| 2016 | * Returns stocks cached in percpu to res_counter and reset cached information. |
| 2017 | */ |
| 2018 | static void drain_stock(struct memcg_stock_pcp *stock) |
| 2019 | { |
| 2020 | struct mem_cgroup *old = stock->cached; |
| 2021 | |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2022 | if (stock->nr_pages) { |
| 2023 | unsigned long bytes = stock->nr_pages * PAGE_SIZE; |
| 2024 | |
| 2025 | res_counter_uncharge(&old->res, bytes); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2026 | if (do_swap_account) |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2027 | res_counter_uncharge(&old->memsw, bytes); |
| 2028 | stock->nr_pages = 0; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2029 | } |
| 2030 | stock->cached = NULL; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | /* |
| 2034 | * This must be called under preempt disabled or must be called by |
| 2035 | * a thread which is pinned to local cpu. |
| 2036 | */ |
| 2037 | static void drain_local_stock(struct work_struct *dummy) |
| 2038 | { |
| 2039 | struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock); |
| 2040 | drain_stock(stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2041 | clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | /* |
| 2045 | * 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] | 2046 | * This will be consumed by consume_stock() function, later. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2047 | */ |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2048 | static void refill_stock(struct mem_cgroup *mem, unsigned int nr_pages) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2049 | { |
| 2050 | struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock); |
| 2051 | |
| 2052 | if (stock->cached != mem) { /* reset if necessary */ |
| 2053 | drain_stock(stock); |
| 2054 | stock->cached = mem; |
| 2055 | } |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2056 | stock->nr_pages += nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2057 | put_cpu_var(memcg_stock); |
| 2058 | } |
| 2059 | |
| 2060 | /* |
| 2061 | * Tries to drain stocked charges in other cpus. This function is asynchronous |
| 2062 | * and just put a work per cpu for draining localy on each cpu. Caller can |
| 2063 | * expects some charges will be back to res_counter later but cannot wait for |
| 2064 | * it. |
| 2065 | */ |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2066 | static void drain_all_stock_async(struct mem_cgroup *root_mem) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2067 | { |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2068 | int cpu, curcpu; |
| 2069 | /* |
| 2070 | * If someone calls draining, avoid adding more kworker runs. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2071 | */ |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2072 | if (!mutex_trylock(&percpu_charge_mutex)) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2073 | return; |
| 2074 | /* Notify other cpus that system-wide "drain" is running */ |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2075 | get_online_cpus(); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2076 | /* |
| 2077 | * Get a hint for avoiding draining charges on the current cpu, |
| 2078 | * which must be exhausted by our charging. It is not required that |
| 2079 | * this be a precise check, so we use raw_smp_processor_id() instead of |
| 2080 | * getcpu()/putcpu(). |
| 2081 | */ |
| 2082 | curcpu = raw_smp_processor_id(); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2083 | for_each_online_cpu(cpu) { |
| 2084 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2085 | struct mem_cgroup *mem; |
| 2086 | |
| 2087 | if (cpu == curcpu) |
| 2088 | continue; |
| 2089 | |
| 2090 | mem = stock->cached; |
| 2091 | if (!mem) |
| 2092 | continue; |
| 2093 | if (mem != root_mem) { |
| 2094 | if (!root_mem->use_hierarchy) |
| 2095 | continue; |
| 2096 | /* check whether "mem" is under tree of "root_mem" */ |
| 2097 | if (!css_is_ancestor(&mem->css, &root_mem->css)) |
| 2098 | continue; |
| 2099 | } |
| 2100 | if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) |
| 2101 | schedule_work_on(cpu, &stock->work); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2102 | } |
| 2103 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2104 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2105 | /* We don't wait for flush_work */ |
| 2106 | } |
| 2107 | |
| 2108 | /* This is a synchronous drain interface. */ |
| 2109 | static void drain_all_stock_sync(void) |
| 2110 | { |
| 2111 | /* called when force_empty is called */ |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2112 | mutex_lock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2113 | schedule_on_each_cpu(drain_local_stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2114 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2115 | } |
| 2116 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2117 | /* |
| 2118 | * This function drains percpu counter value from DEAD cpu and |
| 2119 | * move it to local cpu. Note that this function can be preempted. |
| 2120 | */ |
| 2121 | static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *mem, int cpu) |
| 2122 | { |
| 2123 | int i; |
| 2124 | |
| 2125 | spin_lock(&mem->pcp_counter_lock); |
| 2126 | for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 2127 | long x = per_cpu(mem->stat->count[i], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2128 | |
| 2129 | per_cpu(mem->stat->count[i], cpu) = 0; |
| 2130 | mem->nocpu_base.count[i] += x; |
| 2131 | } |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2132 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) { |
| 2133 | unsigned long x = per_cpu(mem->stat->events[i], cpu); |
| 2134 | |
| 2135 | per_cpu(mem->stat->events[i], cpu) = 0; |
| 2136 | mem->nocpu_base.events[i] += x; |
| 2137 | } |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2138 | /* need to clear ON_MOVE value, works as a kind of lock. */ |
| 2139 | per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0; |
| 2140 | spin_unlock(&mem->pcp_counter_lock); |
| 2141 | } |
| 2142 | |
| 2143 | static void synchronize_mem_cgroup_on_move(struct mem_cgroup *mem, int cpu) |
| 2144 | { |
| 2145 | int idx = MEM_CGROUP_ON_MOVE; |
| 2146 | |
| 2147 | spin_lock(&mem->pcp_counter_lock); |
| 2148 | per_cpu(mem->stat->count[idx], cpu) = mem->nocpu_base.count[idx]; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2149 | spin_unlock(&mem->pcp_counter_lock); |
| 2150 | } |
| 2151 | |
| 2152 | static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb, |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2153 | unsigned long action, |
| 2154 | void *hcpu) |
| 2155 | { |
| 2156 | int cpu = (unsigned long)hcpu; |
| 2157 | struct memcg_stock_pcp *stock; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2158 | struct mem_cgroup *iter; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2159 | |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2160 | if ((action == CPU_ONLINE)) { |
| 2161 | for_each_mem_cgroup_all(iter) |
| 2162 | synchronize_mem_cgroup_on_move(iter, cpu); |
| 2163 | return NOTIFY_OK; |
| 2164 | } |
| 2165 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2166 | if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2167 | return NOTIFY_OK; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2168 | |
| 2169 | for_each_mem_cgroup_all(iter) |
| 2170 | mem_cgroup_drain_pcp_counter(iter, cpu); |
| 2171 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2172 | stock = &per_cpu(memcg_stock, cpu); |
| 2173 | drain_stock(stock); |
| 2174 | return NOTIFY_OK; |
| 2175 | } |
| 2176 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2177 | |
| 2178 | /* See __mem_cgroup_try_charge() for details */ |
| 2179 | enum { |
| 2180 | CHARGE_OK, /* success */ |
| 2181 | CHARGE_RETRY, /* need to retry but retry is not bad */ |
| 2182 | CHARGE_NOMEM, /* we can't do more. return -ENOMEM */ |
| 2183 | CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */ |
| 2184 | CHARGE_OOM_DIE, /* the current is killed because of OOM */ |
| 2185 | }; |
| 2186 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2187 | static int mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask, |
| 2188 | unsigned int nr_pages, bool oom_check) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2189 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2190 | unsigned long csize = nr_pages * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2191 | struct mem_cgroup *mem_over_limit; |
| 2192 | struct res_counter *fail_res; |
| 2193 | unsigned long flags = 0; |
| 2194 | int ret; |
| 2195 | |
| 2196 | ret = res_counter_charge(&mem->res, csize, &fail_res); |
| 2197 | |
| 2198 | if (likely(!ret)) { |
| 2199 | if (!do_swap_account) |
| 2200 | return CHARGE_OK; |
| 2201 | ret = res_counter_charge(&mem->memsw, csize, &fail_res); |
| 2202 | if (likely(!ret)) |
| 2203 | return CHARGE_OK; |
| 2204 | |
KAMEZAWA Hiroyuki | 01c88e2 | 2011-01-25 15:07:27 -0800 | [diff] [blame] | 2205 | res_counter_uncharge(&mem->res, csize); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2206 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw); |
| 2207 | flags |= MEM_CGROUP_RECLAIM_NOSWAP; |
| 2208 | } else |
| 2209 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, res); |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2210 | /* |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2211 | * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch |
| 2212 | * of regular pages (CHARGE_BATCH), or a single regular page (1). |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2213 | * |
| 2214 | * Never reclaim on behalf of optional batching, retry with a |
| 2215 | * single page instead. |
| 2216 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2217 | if (nr_pages == CHARGE_BATCH) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2218 | return CHARGE_RETRY; |
| 2219 | |
| 2220 | if (!(gfp_mask & __GFP_WAIT)) |
| 2221 | return CHARGE_WOULDBLOCK; |
| 2222 | |
| 2223 | ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 2224 | gfp_mask, flags, NULL); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2225 | if (mem_cgroup_margin(mem_over_limit) >= nr_pages) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2226 | return CHARGE_RETRY; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2227 | /* |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2228 | * Even though the limit is exceeded at this point, reclaim |
| 2229 | * may have been able to free some pages. Retry the charge |
| 2230 | * before killing the task. |
| 2231 | * |
| 2232 | * Only for regular pages, though: huge pages are rather |
| 2233 | * unlikely to succeed so close to the limit, and we fall back |
| 2234 | * to regular pages anyway in case of failure. |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2235 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2236 | if (nr_pages == 1 && ret) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2237 | return CHARGE_RETRY; |
| 2238 | |
| 2239 | /* |
| 2240 | * At task move, charge accounts can be doubly counted. So, it's |
| 2241 | * better to wait until the end of task_move if something is going on. |
| 2242 | */ |
| 2243 | if (mem_cgroup_wait_acct_move(mem_over_limit)) |
| 2244 | return CHARGE_RETRY; |
| 2245 | |
| 2246 | /* If we don't need to call oom-killer at el, return immediately */ |
| 2247 | if (!oom_check) |
| 2248 | return CHARGE_NOMEM; |
| 2249 | /* check OOM */ |
| 2250 | if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask)) |
| 2251 | return CHARGE_OOM_DIE; |
| 2252 | |
| 2253 | return CHARGE_RETRY; |
| 2254 | } |
| 2255 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2256 | /* |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2257 | * Unlike exported interface, "oom" parameter is added. if oom==true, |
| 2258 | * oom-killer can be invoked. |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2259 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2260 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2261 | gfp_t gfp_mask, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2262 | unsigned int nr_pages, |
| 2263 | struct mem_cgroup **memcg, |
| 2264 | bool oom) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2265 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2266 | unsigned int batch = max(CHARGE_BATCH, nr_pages); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2267 | int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
| 2268 | struct mem_cgroup *mem = NULL; |
| 2269 | int ret; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2270 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2271 | /* |
| 2272 | * Unlike gloval-vm's OOM-kill, we're not in memory shortage |
| 2273 | * in system level. So, allow to go ahead dying process in addition to |
| 2274 | * MEMDIE process. |
| 2275 | */ |
| 2276 | if (unlikely(test_thread_flag(TIF_MEMDIE) |
| 2277 | || fatal_signal_pending(current))) |
| 2278 | goto bypass; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2279 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2280 | /* |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2281 | * We always charge the cgroup the mm_struct belongs to. |
| 2282 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2283 | * thread group leader migrates. It's possible that mm is not |
| 2284 | * set, if so charge the init_mm (happens for pagecache usage). |
| 2285 | */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2286 | if (!*memcg && !mm) |
| 2287 | goto bypass; |
| 2288 | again: |
| 2289 | if (*memcg) { /* css should be a valid one */ |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2290 | mem = *memcg; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2291 | VM_BUG_ON(css_is_removed(&mem->css)); |
| 2292 | if (mem_cgroup_is_root(mem)) |
| 2293 | goto done; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2294 | if (nr_pages == 1 && consume_stock(mem)) |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2295 | goto done; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2296 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2297 | } else { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2298 | struct task_struct *p; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2299 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2300 | rcu_read_lock(); |
| 2301 | p = rcu_dereference(mm->owner); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2302 | /* |
KAMEZAWA Hiroyuki | ebb76ce | 2010-12-29 14:07:11 -0800 | [diff] [blame] | 2303 | * Because we don't have task_lock(), "p" can exit. |
| 2304 | * In that case, "mem" can point to root or p can be NULL with |
| 2305 | * race with swapoff. Then, we have small risk of mis-accouning. |
| 2306 | * But such kind of mis-account by race always happens because |
| 2307 | * we don't have cgroup_mutex(). It's overkill and we allo that |
| 2308 | * small race, here. |
| 2309 | * (*) swapoff at el will charge against mm-struct not against |
| 2310 | * task-struct. So, mm->owner can be NULL. |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2311 | */ |
| 2312 | mem = mem_cgroup_from_task(p); |
KAMEZAWA Hiroyuki | ebb76ce | 2010-12-29 14:07:11 -0800 | [diff] [blame] | 2313 | if (!mem || mem_cgroup_is_root(mem)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2314 | rcu_read_unlock(); |
| 2315 | goto done; |
| 2316 | } |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2317 | if (nr_pages == 1 && consume_stock(mem)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2318 | /* |
| 2319 | * It seems dagerous to access memcg without css_get(). |
| 2320 | * But considering how consume_stok works, it's not |
| 2321 | * necessary. If consume_stock success, some charges |
| 2322 | * from this memcg are cached on this cpu. So, we |
| 2323 | * don't need to call css_get()/css_tryget() before |
| 2324 | * calling consume_stock(). |
| 2325 | */ |
| 2326 | rcu_read_unlock(); |
| 2327 | goto done; |
| 2328 | } |
| 2329 | /* after here, we may be blocked. we need to get refcnt */ |
| 2330 | if (!css_tryget(&mem->css)) { |
| 2331 | rcu_read_unlock(); |
| 2332 | goto again; |
| 2333 | } |
| 2334 | rcu_read_unlock(); |
| 2335 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2336 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2337 | do { |
| 2338 | bool oom_check; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2339 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2340 | /* If killed, bypass charge */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2341 | if (fatal_signal_pending(current)) { |
| 2342 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2343 | goto bypass; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2344 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2345 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2346 | oom_check = false; |
| 2347 | if (oom && !nr_oom_retries) { |
| 2348 | oom_check = true; |
| 2349 | nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
| 2350 | } |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 2351 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2352 | ret = mem_cgroup_do_charge(mem, gfp_mask, batch, oom_check); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2353 | switch (ret) { |
| 2354 | case CHARGE_OK: |
| 2355 | break; |
| 2356 | case CHARGE_RETRY: /* not in OOM situation but retry */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2357 | batch = nr_pages; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2358 | css_put(&mem->css); |
| 2359 | mem = NULL; |
| 2360 | goto again; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2361 | case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2362 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2363 | goto nomem; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2364 | case CHARGE_NOMEM: /* OOM routine works */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2365 | if (!oom) { |
| 2366 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2367 | goto nomem; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2368 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2369 | /* If oom, we never return -ENOMEM */ |
| 2370 | nr_oom_retries--; |
| 2371 | break; |
| 2372 | case CHARGE_OOM_DIE: /* Killed by OOM Killer */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2373 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2374 | goto bypass; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2375 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2376 | } while (ret != CHARGE_OK); |
| 2377 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2378 | if (batch > nr_pages) |
| 2379 | refill_stock(mem, batch - nr_pages); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2380 | css_put(&mem->css); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2381 | done: |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2382 | *memcg = mem; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2383 | return 0; |
| 2384 | nomem: |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2385 | *memcg = NULL; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2386 | return -ENOMEM; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2387 | bypass: |
| 2388 | *memcg = NULL; |
| 2389 | return 0; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2390 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2391 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2392 | /* |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2393 | * Somemtimes we have to undo a charge we got by try_charge(). |
| 2394 | * This function is for that and do uncharge, put css's refcnt. |
| 2395 | * gotten by try_charge(). |
| 2396 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2397 | static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem, |
Johannes Weiner | e7018b8 | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2398 | unsigned int nr_pages) |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2399 | { |
| 2400 | if (!mem_cgroup_is_root(mem)) { |
Johannes Weiner | e7018b8 | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2401 | unsigned long bytes = nr_pages * PAGE_SIZE; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2402 | |
Johannes Weiner | e7018b8 | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2403 | res_counter_uncharge(&mem->res, bytes); |
| 2404 | if (do_swap_account) |
| 2405 | res_counter_uncharge(&mem->memsw, bytes); |
| 2406 | } |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | /* |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2410 | * A helper function to get mem_cgroup from ID. must be called under |
| 2411 | * rcu_read_lock(). The caller must check css_is_removed() or some if |
| 2412 | * it's concern. (dropping refcnt from swap can be called against removed |
| 2413 | * memcg.) |
| 2414 | */ |
| 2415 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) |
| 2416 | { |
| 2417 | struct cgroup_subsys_state *css; |
| 2418 | |
| 2419 | /* ID 0 is unused ID */ |
| 2420 | if (!id) |
| 2421 | return NULL; |
| 2422 | css = css_lookup(&mem_cgroup_subsys, id); |
| 2423 | if (!css) |
| 2424 | return NULL; |
| 2425 | return container_of(css, struct mem_cgroup, css); |
| 2426 | } |
| 2427 | |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2428 | struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2429 | { |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2430 | struct mem_cgroup *mem = NULL; |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2431 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2432 | unsigned short id; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2433 | swp_entry_t ent; |
| 2434 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2435 | VM_BUG_ON(!PageLocked(page)); |
| 2436 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2437 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2438 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2439 | if (PageCgroupUsed(pc)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2440 | mem = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2441 | if (mem && !css_tryget(&mem->css)) |
| 2442 | mem = NULL; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2443 | } else if (PageSwapCache(page)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2444 | ent.val = page_private(page); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2445 | id = lookup_swap_cgroup(ent); |
| 2446 | rcu_read_lock(); |
| 2447 | mem = mem_cgroup_lookup(id); |
| 2448 | if (mem && !css_tryget(&mem->css)) |
| 2449 | mem = NULL; |
| 2450 | rcu_read_unlock(); |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2451 | } |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2452 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2453 | return mem; |
| 2454 | } |
| 2455 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2456 | static void __mem_cgroup_commit_charge(struct mem_cgroup *mem, |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2457 | struct page *page, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2458 | unsigned int nr_pages, |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2459 | struct page_cgroup *pc, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2460 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2461 | { |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2462 | lock_page_cgroup(pc); |
| 2463 | if (unlikely(PageCgroupUsed(pc))) { |
| 2464 | unlock_page_cgroup(pc); |
Johannes Weiner | e7018b8 | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2465 | __mem_cgroup_cancel_charge(mem, nr_pages); |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2466 | return; |
| 2467 | } |
| 2468 | /* |
| 2469 | * we don't need page_cgroup_lock about tail pages, becase they are not |
| 2470 | * accessed by any other context at this point. |
| 2471 | */ |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2472 | pc->mem_cgroup = mem; |
KAMEZAWA Hiroyuki | 261fb61 | 2009-09-23 15:56:33 -0700 | [diff] [blame] | 2473 | /* |
| 2474 | * We access a page_cgroup asynchronously without lock_page_cgroup(). |
| 2475 | * Especially when a page_cgroup is taken from a page, pc->mem_cgroup |
| 2476 | * is accessed after testing USED bit. To make pc->mem_cgroup visible |
| 2477 | * before USED bit, we need memory barrier here. |
| 2478 | * See mem_cgroup_add_lru_list(), etc. |
| 2479 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2480 | smp_wmb(); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 2481 | switch (ctype) { |
| 2482 | case MEM_CGROUP_CHARGE_TYPE_CACHE: |
| 2483 | case MEM_CGROUP_CHARGE_TYPE_SHMEM: |
| 2484 | SetPageCgroupCache(pc); |
| 2485 | SetPageCgroupUsed(pc); |
| 2486 | break; |
| 2487 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
| 2488 | ClearPageCgroupCache(pc); |
| 2489 | SetPageCgroupUsed(pc); |
| 2490 | break; |
| 2491 | default: |
| 2492 | break; |
| 2493 | } |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2494 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2495 | mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), nr_pages); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2496 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 2497 | /* |
| 2498 | * "charge_statistics" updated event counter. Then, check it. |
| 2499 | * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree. |
| 2500 | * if they exceeds softlimit. |
| 2501 | */ |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2502 | memcg_check_events(mem, page); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2503 | } |
| 2504 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2505 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 2506 | |
| 2507 | #define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\ |
| 2508 | (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION)) |
| 2509 | /* |
| 2510 | * Because tail pages are not marked as "used", set it. We're under |
| 2511 | * zone->lru_lock, 'splitting on pmd' and compund_lock. |
| 2512 | */ |
| 2513 | void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail) |
| 2514 | { |
| 2515 | struct page_cgroup *head_pc = lookup_page_cgroup(head); |
| 2516 | struct page_cgroup *tail_pc = lookup_page_cgroup(tail); |
| 2517 | unsigned long flags; |
| 2518 | |
KAMEZAWA Hiroyuki | 3d37c4a | 2011-01-25 15:07:28 -0800 | [diff] [blame] | 2519 | if (mem_cgroup_disabled()) |
| 2520 | return; |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2521 | /* |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2522 | * We have no races with charge/uncharge but will have races with |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2523 | * page state accounting. |
| 2524 | */ |
| 2525 | move_lock_page_cgroup(head_pc, &flags); |
| 2526 | |
| 2527 | tail_pc->mem_cgroup = head_pc->mem_cgroup; |
| 2528 | smp_wmb(); /* see __commit_charge() */ |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2529 | if (PageCgroupAcctLRU(head_pc)) { |
| 2530 | enum lru_list lru; |
| 2531 | struct mem_cgroup_per_zone *mz; |
| 2532 | |
| 2533 | /* |
| 2534 | * LRU flags cannot be copied because we need to add tail |
| 2535 | *.page to LRU by generic call and our hook will be called. |
| 2536 | * We hold lru_lock, then, reduce counter directly. |
| 2537 | */ |
| 2538 | lru = page_lru(head); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 2539 | mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2540 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
| 2541 | } |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2542 | tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT; |
| 2543 | move_unlock_page_cgroup(head_pc, &flags); |
| 2544 | } |
| 2545 | #endif |
| 2546 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2547 | /** |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2548 | * mem_cgroup_move_account - move account of the page |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2549 | * @page: the page |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2550 | * @nr_pages: number of regular pages (>1 for huge pages) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2551 | * @pc: page_cgroup of the page. |
| 2552 | * @from: mem_cgroup which the page is moved from. |
| 2553 | * @to: mem_cgroup which the page is moved to. @from != @to. |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2554 | * @uncharge: whether we should call uncharge and css_put against @from. |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2555 | * |
| 2556 | * The caller must confirm following. |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2557 | * - page is not on LRU (isolate_page() is useful.) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2558 | * - compound_lock is held when nr_pages > 1 |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2559 | * |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2560 | * 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] | 2561 | * 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] | 2562 | * true, this function does "uncharge" from old cgroup, but it doesn't if |
| 2563 | * @uncharge is false, so a caller should do "uncharge". |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2564 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2565 | static int mem_cgroup_move_account(struct page *page, |
| 2566 | unsigned int nr_pages, |
| 2567 | struct page_cgroup *pc, |
| 2568 | struct mem_cgroup *from, |
| 2569 | struct mem_cgroup *to, |
| 2570 | bool uncharge) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2571 | { |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2572 | unsigned long flags; |
| 2573 | int ret; |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2574 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2575 | VM_BUG_ON(from == to); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2576 | VM_BUG_ON(PageLRU(page)); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2577 | /* |
| 2578 | * The page is isolated from LRU. So, collapse function |
| 2579 | * will not handle this page. But page splitting can happen. |
| 2580 | * Do this check under compound_page_lock(). The caller should |
| 2581 | * hold it. |
| 2582 | */ |
| 2583 | ret = -EBUSY; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2584 | if (nr_pages > 1 && !PageTransHuge(page)) |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2585 | goto out; |
| 2586 | |
| 2587 | lock_page_cgroup(pc); |
| 2588 | |
| 2589 | ret = -EINVAL; |
| 2590 | if (!PageCgroupUsed(pc) || pc->mem_cgroup != from) |
| 2591 | goto unlock; |
| 2592 | |
| 2593 | move_lock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2594 | |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 2595 | if (PageCgroupFileMapped(pc)) { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 2596 | /* Update mapped_file data for mem_cgroup */ |
| 2597 | preempt_disable(); |
| 2598 | __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 2599 | __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 2600 | preempt_enable(); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2601 | } |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2602 | mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2603 | if (uncharge) |
| 2604 | /* This is not "cancel", but cancel_charge does all we need. */ |
Johannes Weiner | e7018b8 | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2605 | __mem_cgroup_cancel_charge(from, nr_pages); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2606 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2607 | /* caller should have done css_get */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2608 | pc->mem_cgroup = to; |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2609 | mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2610 | /* |
| 2611 | * We charges against "to" which may not have any tasks. Then, "to" |
| 2612 | * can be under rmdir(). But in current implementation, caller of |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 2613 | * 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] | 2614 | * guaranteed that "to" is never removed. So, we don't check rmdir |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 2615 | * status here. |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2616 | */ |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2617 | move_unlock_page_cgroup(pc, &flags); |
| 2618 | ret = 0; |
| 2619 | unlock: |
Daisuke Nishimura | 57f9fd7 | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2620 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 2621 | /* |
| 2622 | * check events |
| 2623 | */ |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2624 | memcg_check_events(to, page); |
| 2625 | memcg_check_events(from, page); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2626 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2627 | return ret; |
| 2628 | } |
| 2629 | |
| 2630 | /* |
| 2631 | * move charges to its parent. |
| 2632 | */ |
| 2633 | |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2634 | static int mem_cgroup_move_parent(struct page *page, |
| 2635 | struct page_cgroup *pc, |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2636 | struct mem_cgroup *child, |
| 2637 | gfp_t gfp_mask) |
| 2638 | { |
| 2639 | struct cgroup *cg = child->css.cgroup; |
| 2640 | struct cgroup *pcg = cg->parent; |
| 2641 | struct mem_cgroup *parent; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2642 | unsigned int nr_pages; |
Andrew Morton | 4be4489 | 2011-03-23 16:42:39 -0700 | [diff] [blame] | 2643 | unsigned long uninitialized_var(flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2644 | int ret; |
| 2645 | |
| 2646 | /* Is ROOT ? */ |
| 2647 | if (!pcg) |
| 2648 | return -EINVAL; |
| 2649 | |
Daisuke Nishimura | 57f9fd7 | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2650 | ret = -EBUSY; |
| 2651 | if (!get_page_unless_zero(page)) |
| 2652 | goto out; |
| 2653 | if (isolate_lru_page(page)) |
| 2654 | goto put; |
KAMEZAWA Hiroyuki | 52dbb90 | 2011-01-25 15:07:29 -0800 | [diff] [blame] | 2655 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2656 | nr_pages = hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2657 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2658 | parent = mem_cgroup_from_cont(pcg); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2659 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2660 | if (ret || !parent) |
Daisuke Nishimura | 57f9fd7 | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2661 | goto put_back; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2662 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2663 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2664 | flags = compound_lock_irqsave(page); |
| 2665 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2666 | ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2667 | if (ret) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2668 | __mem_cgroup_cancel_charge(parent, nr_pages); |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 2669 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2670 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2671 | compound_unlock_irqrestore(page, flags); |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 2672 | put_back: |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2673 | putback_lru_page(page); |
Daisuke Nishimura | 57f9fd7 | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2674 | put: |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 2675 | put_page(page); |
Daisuke Nishimura | 57f9fd7 | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2676 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2677 | return ret; |
| 2678 | } |
| 2679 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2680 | /* |
| 2681 | * Charge the memory controller for page usage. |
| 2682 | * Return |
| 2683 | * 0 if the charge was successful |
| 2684 | * < 0 if the cgroup is over its limit |
| 2685 | */ |
| 2686 | 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] | 2687 | gfp_t gfp_mask, enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2688 | { |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2689 | struct mem_cgroup *mem = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2690 | unsigned int nr_pages = 1; |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 2691 | struct page_cgroup *pc; |
| 2692 | bool oom = true; |
| 2693 | int ret; |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2694 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2695 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2696 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2697 | VM_BUG_ON(!PageTransHuge(page)); |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 2698 | /* |
| 2699 | * Never OOM-kill a process for a huge page. The |
| 2700 | * fault handler will fall back to regular pages. |
| 2701 | */ |
| 2702 | oom = false; |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2703 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2704 | |
| 2705 | pc = lookup_page_cgroup(page); |
Johannes Weiner | af4a662 | 2011-03-23 16:42:24 -0700 | [diff] [blame] | 2706 | BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */ |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2707 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2708 | ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &mem, oom); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2709 | if (ret || !mem) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2710 | return ret; |
| 2711 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2712 | __mem_cgroup_commit_charge(mem, page, nr_pages, pc, ctype); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2713 | return 0; |
| 2714 | } |
| 2715 | |
| 2716 | int mem_cgroup_newpage_charge(struct page *page, |
| 2717 | struct mm_struct *mm, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2718 | { |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2719 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 2720 | return 0; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2721 | /* |
| 2722 | * If already mapped, we don't have to account. |
| 2723 | * If page cache, page->mapping has address_space. |
| 2724 | * But page->mapping may have out-of-use anon_vma pointer, |
| 2725 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping |
| 2726 | * is NULL. |
| 2727 | */ |
| 2728 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) |
| 2729 | return 0; |
| 2730 | if (unlikely(!mm)) |
| 2731 | mm = &init_mm; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2732 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2733 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2734 | } |
| 2735 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2736 | static void |
| 2737 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 2738 | enum charge_type ctype); |
| 2739 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2740 | static void |
| 2741 | __mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *mem, |
| 2742 | enum charge_type ctype) |
| 2743 | { |
| 2744 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 2745 | /* |
| 2746 | * In some case, SwapCache, FUSE(splice_buf->radixtree), the page |
| 2747 | * is already on LRU. It means the page may on some other page_cgroup's |
| 2748 | * LRU. Take care of it. |
| 2749 | */ |
| 2750 | mem_cgroup_lru_del_before_commit(page); |
| 2751 | __mem_cgroup_commit_charge(mem, page, 1, pc, ctype); |
| 2752 | mem_cgroup_lru_add_after_commit(page); |
| 2753 | return; |
| 2754 | } |
| 2755 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 2756 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 2757 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2758 | { |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2759 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2760 | int ret; |
| 2761 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2762 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 2763 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2764 | if (PageCompound(page)) |
| 2765 | return 0; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2766 | /* |
| 2767 | * Corner case handling. This is called from add_to_page_cache() |
| 2768 | * in usual. But some FS (shmem) precharges this page before calling it |
| 2769 | * and call add_to_page_cache() with GFP_NOWAIT. |
| 2770 | * |
| 2771 | * For GFP_NOWAIT case, the page may be pre-charged before calling |
| 2772 | * add_to_page_cache(). (See shmem.c) check it here and avoid to call |
| 2773 | * charge twice. (It works but has to pay a bit larger cost.) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2774 | * And when the page is SwapCache, it should take swap information |
| 2775 | * into account. This is under lock_page() now. |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2776 | */ |
| 2777 | if (!(gfp_mask & __GFP_WAIT)) { |
| 2778 | struct page_cgroup *pc; |
| 2779 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2780 | pc = lookup_page_cgroup(page); |
| 2781 | if (!pc) |
| 2782 | return 0; |
| 2783 | lock_page_cgroup(pc); |
| 2784 | if (PageCgroupUsed(pc)) { |
| 2785 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2786 | return 0; |
| 2787 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2788 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2789 | } |
| 2790 | |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2791 | if (unlikely(!mm)) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2792 | mm = &init_mm; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2793 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2794 | if (page_is_file_cache(page)) { |
| 2795 | ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &mem, true); |
| 2796 | if (ret || !mem) |
| 2797 | return ret; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2798 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2799 | /* |
| 2800 | * FUSE reuses pages without going through the final |
| 2801 | * put that would remove them from the LRU list, make |
| 2802 | * sure that they get relinked properly. |
| 2803 | */ |
| 2804 | __mem_cgroup_commit_charge_lrucare(page, mem, |
| 2805 | MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 2806 | return ret; |
| 2807 | } |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2808 | /* shmem */ |
| 2809 | if (PageSwapCache(page)) { |
| 2810 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
| 2811 | if (!ret) |
| 2812 | __mem_cgroup_commit_charge_swapin(page, mem, |
| 2813 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
| 2814 | } else |
| 2815 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2816 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2817 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2818 | return ret; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 2819 | } |
| 2820 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2821 | /* |
| 2822 | * While swap-in, try_charge -> commit or cancel, the page is locked. |
| 2823 | * 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] | 2824 | * struct page_cgroup is acquired. This refcnt will be consumed by |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2825 | * "commit()" or removed by "cancel()" |
| 2826 | */ |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2827 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
| 2828 | struct page *page, |
| 2829 | gfp_t mask, struct mem_cgroup **ptr) |
| 2830 | { |
| 2831 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2832 | int ret; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2833 | |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 2834 | *ptr = NULL; |
| 2835 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2836 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2837 | return 0; |
| 2838 | |
| 2839 | if (!do_swap_account) |
| 2840 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2841 | /* |
| 2842 | * A racing thread's fault, or swapoff, may have already updated |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2843 | * the pte, and even removed page from swap cache: in those cases |
| 2844 | * do_swap_page()'s pte_same() test will fail; but there's also a |
| 2845 | * KSM case which does need to charge the page. |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2846 | */ |
| 2847 | if (!PageSwapCache(page)) |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2848 | goto charge_cur_mm; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2849 | mem = try_get_mem_cgroup_from_page(page); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2850 | if (!mem) |
| 2851 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2852 | *ptr = mem; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2853 | ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2854 | css_put(&mem->css); |
| 2855 | return ret; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2856 | charge_cur_mm: |
| 2857 | if (unlikely(!mm)) |
| 2858 | mm = &init_mm; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2859 | return __mem_cgroup_try_charge(mm, mask, 1, ptr, true); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2860 | } |
| 2861 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2862 | static void |
| 2863 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 2864 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2865 | { |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2866 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2867 | return; |
| 2868 | if (!ptr) |
| 2869 | return; |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2870 | cgroup_exclude_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2871 | |
| 2872 | __mem_cgroup_commit_charge_lrucare(page, ptr, ctype); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2873 | /* |
| 2874 | * Now swap is on-memory. This means this page may be |
| 2875 | * counted both as mem and swap....double count. |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2876 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
| 2877 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() |
| 2878 | * may call delete_from_swap_cache() before reach here. |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2879 | */ |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2880 | if (do_swap_account && PageSwapCache(page)) { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2881 | swp_entry_t ent = {.val = page_private(page)}; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2882 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2883 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2884 | |
| 2885 | id = swap_cgroup_record(ent, 0); |
| 2886 | rcu_read_lock(); |
| 2887 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2888 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2889 | /* |
| 2890 | * This recorded memcg can be obsolete one. So, avoid |
| 2891 | * calling css_tryget |
| 2892 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2893 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 2894 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2895 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2896 | mem_cgroup_put(memcg); |
| 2897 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2898 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2899 | } |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2900 | /* |
| 2901 | * At swapin, we may charge account against cgroup which has no tasks. |
| 2902 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 2903 | * In that case, we need to call pre_destroy() again. check it here. |
| 2904 | */ |
| 2905 | cgroup_release_and_wakeup_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2906 | } |
| 2907 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2908 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
| 2909 | { |
| 2910 | __mem_cgroup_commit_charge_swapin(page, ptr, |
| 2911 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 2912 | } |
| 2913 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2914 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem) |
| 2915 | { |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2916 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2917 | return; |
| 2918 | if (!mem) |
| 2919 | return; |
Johannes Weiner | e7018b8 | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2920 | __mem_cgroup_cancel_charge(mem, 1); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2921 | } |
| 2922 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2923 | static void mem_cgroup_do_uncharge(struct mem_cgroup *mem, |
| 2924 | unsigned int nr_pages, |
| 2925 | const enum charge_type ctype) |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2926 | { |
| 2927 | struct memcg_batch_info *batch = NULL; |
| 2928 | bool uncharge_memsw = true; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2929 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2930 | /* If swapout, usage of swap doesn't decrease */ |
| 2931 | if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) |
| 2932 | uncharge_memsw = false; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2933 | |
| 2934 | batch = ¤t->memcg_batch; |
| 2935 | /* |
| 2936 | * In usual, we do css_get() when we remember memcg pointer. |
| 2937 | * But in this case, we keep res->usage until end of a series of |
| 2938 | * uncharges. Then, it's ok to ignore memcg's refcnt. |
| 2939 | */ |
| 2940 | if (!batch->memcg) |
| 2941 | batch->memcg = mem; |
| 2942 | /* |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2943 | * do_batch > 0 when unmapping pages or inode invalidate/truncate. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2944 | * 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] | 2945 | * the same cgroup and we have chance to coalesce uncharges. |
| 2946 | * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE) |
| 2947 | * because we want to do uncharge as soon as possible. |
| 2948 | */ |
| 2949 | |
| 2950 | if (!batch->do_batch || test_thread_flag(TIF_MEMDIE)) |
| 2951 | goto direct_uncharge; |
| 2952 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2953 | if (nr_pages > 1) |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2954 | goto direct_uncharge; |
| 2955 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2956 | /* |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2957 | * In typical case, batch->memcg == mem. This means we can |
| 2958 | * merge a series of uncharges to an uncharge of res_counter. |
| 2959 | * If not, we uncharge res_counter ony by one. |
| 2960 | */ |
| 2961 | if (batch->memcg != mem) |
| 2962 | goto direct_uncharge; |
| 2963 | /* remember freed charge and uncharge it later */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 2964 | batch->nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2965 | if (uncharge_memsw) |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 2966 | batch->memsw_nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2967 | return; |
| 2968 | direct_uncharge: |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2969 | res_counter_uncharge(&mem->res, nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2970 | if (uncharge_memsw) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2971 | res_counter_uncharge(&mem->memsw, nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2972 | if (unlikely(batch->memcg != mem)) |
| 2973 | memcg_oom_recover(mem); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2974 | return; |
| 2975 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2976 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2977 | /* |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2978 | * uncharge if !page_mapped(page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2979 | */ |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2980 | static struct mem_cgroup * |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2981 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2982 | { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2983 | struct mem_cgroup *mem = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2984 | unsigned int nr_pages = 1; |
| 2985 | struct page_cgroup *pc; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2986 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2987 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2988 | return NULL; |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 2989 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2990 | if (PageSwapCache(page)) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2991 | return NULL; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2992 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2993 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2994 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2995 | VM_BUG_ON(!PageTransHuge(page)); |
| 2996 | } |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2997 | /* |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 2998 | * Check if our page_cgroup is valid |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2999 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3000 | pc = lookup_page_cgroup(page); |
| 3001 | if (unlikely(!pc || !PageCgroupUsed(pc))) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3002 | return NULL; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3003 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3004 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3005 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3006 | mem = pc->mem_cgroup; |
| 3007 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3008 | if (!PageCgroupUsed(pc)) |
| 3009 | goto unlock_out; |
| 3010 | |
| 3011 | switch (ctype) { |
| 3012 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3013 | case MEM_CGROUP_CHARGE_TYPE_DROP: |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3014 | /* See mem_cgroup_prepare_migration() */ |
| 3015 | if (page_mapped(page) || PageCgroupMigration(pc)) |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3016 | goto unlock_out; |
| 3017 | break; |
| 3018 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: |
| 3019 | if (!PageAnon(page)) { /* Shared memory */ |
| 3020 | if (page->mapping && !page_is_file_cache(page)) |
| 3021 | goto unlock_out; |
| 3022 | } else if (page_mapped(page)) /* Anon */ |
| 3023 | goto unlock_out; |
| 3024 | break; |
| 3025 | default: |
| 3026 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3027 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3028 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3029 | mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), -nr_pages); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 3030 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3031 | ClearPageCgroupUsed(pc); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 3032 | /* |
| 3033 | * pc->mem_cgroup is not cleared here. It will be accessed when it's |
| 3034 | * freed from LRU. This is safe because uncharged page is expected not |
| 3035 | * to be reused (freed soon). Exception is SwapCache, it's handled by |
| 3036 | * special functions. |
| 3037 | */ |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 3038 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3039 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3040 | /* |
| 3041 | * even after unlock, we have mem->res.usage here and this memcg |
| 3042 | * will never be freed. |
| 3043 | */ |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 3044 | memcg_check_events(mem, page); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3045 | if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) { |
| 3046 | mem_cgroup_swap_statistics(mem, true); |
| 3047 | mem_cgroup_get(mem); |
| 3048 | } |
| 3049 | if (!mem_cgroup_is_root(mem)) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3050 | mem_cgroup_do_uncharge(mem, nr_pages, ctype); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 3051 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3052 | return mem; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3053 | |
| 3054 | unlock_out: |
| 3055 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3056 | return NULL; |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 3057 | } |
| 3058 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3059 | void mem_cgroup_uncharge_page(struct page *page) |
| 3060 | { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3061 | /* early check. */ |
| 3062 | if (page_mapped(page)) |
| 3063 | return; |
| 3064 | if (page->mapping && !PageAnon(page)) |
| 3065 | return; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3066 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 3067 | } |
| 3068 | |
| 3069 | void mem_cgroup_uncharge_cache_page(struct page *page) |
| 3070 | { |
| 3071 | VM_BUG_ON(page_mapped(page)); |
KAMEZAWA Hiroyuki | b7abea9 | 2008-10-18 20:28:09 -0700 | [diff] [blame] | 3072 | VM_BUG_ON(page->mapping); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3073 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 3074 | } |
| 3075 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3076 | /* |
| 3077 | * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate. |
| 3078 | * In that cases, pages are freed continuously and we can expect pages |
| 3079 | * are in the same memcg. All these calls itself limits the number of |
| 3080 | * pages freed at once, then uncharge_start/end() is called properly. |
| 3081 | * This may be called prural(2) times in a context, |
| 3082 | */ |
| 3083 | |
| 3084 | void mem_cgroup_uncharge_start(void) |
| 3085 | { |
| 3086 | current->memcg_batch.do_batch++; |
| 3087 | /* We can do nest. */ |
| 3088 | if (current->memcg_batch.do_batch == 1) { |
| 3089 | current->memcg_batch.memcg = NULL; |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3090 | current->memcg_batch.nr_pages = 0; |
| 3091 | current->memcg_batch.memsw_nr_pages = 0; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3092 | } |
| 3093 | } |
| 3094 | |
| 3095 | void mem_cgroup_uncharge_end(void) |
| 3096 | { |
| 3097 | struct memcg_batch_info *batch = ¤t->memcg_batch; |
| 3098 | |
| 3099 | if (!batch->do_batch) |
| 3100 | return; |
| 3101 | |
| 3102 | batch->do_batch--; |
| 3103 | if (batch->do_batch) /* If stacked, do nothing. */ |
| 3104 | return; |
| 3105 | |
| 3106 | if (!batch->memcg) |
| 3107 | return; |
| 3108 | /* |
| 3109 | * This "batch->memcg" is valid without any css_get/put etc... |
| 3110 | * bacause we hide charges behind us. |
| 3111 | */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3112 | if (batch->nr_pages) |
| 3113 | res_counter_uncharge(&batch->memcg->res, |
| 3114 | batch->nr_pages * PAGE_SIZE); |
| 3115 | if (batch->memsw_nr_pages) |
| 3116 | res_counter_uncharge(&batch->memcg->memsw, |
| 3117 | batch->memsw_nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3118 | memcg_oom_recover(batch->memcg); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3119 | /* forget this pointer (for sanity check) */ |
| 3120 | batch->memcg = NULL; |
| 3121 | } |
| 3122 | |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3123 | #ifdef CONFIG_SWAP |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3124 | /* |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3125 | * called after __delete_from_swap_cache() and drop "page" account. |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3126 | * memcg information is recorded to swap_cgroup of "ent" |
| 3127 | */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3128 | void |
| 3129 | 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] | 3130 | { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3131 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3132 | int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3133 | |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3134 | if (!swapout) /* this was a swap cache but the swap is unused ! */ |
| 3135 | ctype = MEM_CGROUP_CHARGE_TYPE_DROP; |
| 3136 | |
| 3137 | memcg = __mem_cgroup_uncharge_common(page, ctype); |
| 3138 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3139 | /* |
| 3140 | * record memcg information, if swapout && memcg != NULL, |
| 3141 | * mem_cgroup_get() was called in uncharge(). |
| 3142 | */ |
| 3143 | if (do_swap_account && swapout && memcg) |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3144 | swap_cgroup_record(ent, css_id(&memcg->css)); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3145 | } |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3146 | #endif |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3147 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3148 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 3149 | /* |
| 3150 | * called from swap_entry_free(). remove record in swap_cgroup and |
| 3151 | * uncharge "memsw" account. |
| 3152 | */ |
| 3153 | void mem_cgroup_uncharge_swap(swp_entry_t ent) |
| 3154 | { |
| 3155 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3156 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3157 | |
| 3158 | if (!do_swap_account) |
| 3159 | return; |
| 3160 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3161 | id = swap_cgroup_record(ent, 0); |
| 3162 | rcu_read_lock(); |
| 3163 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3164 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3165 | /* |
| 3166 | * We uncharge this because swap is freed. |
| 3167 | * This memcg can be obsolete one. We avoid calling css_tryget |
| 3168 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3169 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 3170 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3171 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3172 | mem_cgroup_put(memcg); |
| 3173 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3174 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3175 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3176 | |
| 3177 | /** |
| 3178 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. |
| 3179 | * @entry: swap entry to be moved |
| 3180 | * @from: mem_cgroup which the entry is moved from |
| 3181 | * @to: mem_cgroup which the entry is moved to |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3182 | * @need_fixup: whether we should fixup res_counters and refcounts. |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3183 | * |
| 3184 | * It succeeds only when the swap_cgroup's record for this entry is the same |
| 3185 | * as the mem_cgroup's id of @from. |
| 3186 | * |
| 3187 | * Returns 0 on success, -EINVAL on failure. |
| 3188 | * |
| 3189 | * The caller must have charged to @to, IOW, called res_counter_charge() about |
| 3190 | * both res and memsw, and called css_get(). |
| 3191 | */ |
| 3192 | static int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3193 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3194 | { |
| 3195 | unsigned short old_id, new_id; |
| 3196 | |
| 3197 | old_id = css_id(&from->css); |
| 3198 | new_id = css_id(&to->css); |
| 3199 | |
| 3200 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3201 | mem_cgroup_swap_statistics(from, false); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3202 | mem_cgroup_swap_statistics(to, true); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3203 | /* |
| 3204 | * This function is only called from task migration context now. |
| 3205 | * It postpones res_counter and refcount handling till the end |
| 3206 | * of task migration(mem_cgroup_clear_mc()) for performance |
| 3207 | * improvement. But we cannot postpone mem_cgroup_get(to) |
| 3208 | * because if the process that has been moved to @to does |
| 3209 | * swap-in, the refcount of @to might be decreased to 0. |
| 3210 | */ |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3211 | mem_cgroup_get(to); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3212 | if (need_fixup) { |
| 3213 | if (!mem_cgroup_is_root(from)) |
| 3214 | res_counter_uncharge(&from->memsw, PAGE_SIZE); |
| 3215 | mem_cgroup_put(from); |
| 3216 | /* |
| 3217 | * we charged both to->res and to->memsw, so we should |
| 3218 | * uncharge to->res. |
| 3219 | */ |
| 3220 | if (!mem_cgroup_is_root(to)) |
| 3221 | res_counter_uncharge(&to->res, PAGE_SIZE); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3222 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3223 | return 0; |
| 3224 | } |
| 3225 | return -EINVAL; |
| 3226 | } |
| 3227 | #else |
| 3228 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3229 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3230 | { |
| 3231 | return -EINVAL; |
| 3232 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3233 | #endif |
| 3234 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3235 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3236 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
| 3237 | * page belongs to. |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3238 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3239 | int mem_cgroup_prepare_migration(struct page *page, |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 3240 | struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3241 | { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3242 | struct mem_cgroup *mem = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3243 | struct page_cgroup *pc; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3244 | enum charge_type ctype; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3245 | int ret = 0; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 3246 | |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 3247 | *ptr = NULL; |
| 3248 | |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 3249 | VM_BUG_ON(PageTransHuge(page)); |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3250 | if (mem_cgroup_disabled()) |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 3251 | return 0; |
| 3252 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3253 | pc = lookup_page_cgroup(page); |
| 3254 | lock_page_cgroup(pc); |
| 3255 | if (PageCgroupUsed(pc)) { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3256 | mem = pc->mem_cgroup; |
| 3257 | css_get(&mem->css); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3258 | /* |
| 3259 | * At migrating an anonymous page, its mapcount goes down |
| 3260 | * to 0 and uncharge() will be called. But, even if it's fully |
| 3261 | * unmapped, migration may fail and this page has to be |
| 3262 | * charged again. We set MIGRATION flag here and delay uncharge |
| 3263 | * until end_migration() is called |
| 3264 | * |
| 3265 | * Corner Case Thinking |
| 3266 | * A) |
| 3267 | * When the old page was mapped as Anon and it's unmap-and-freed |
| 3268 | * while migration was ongoing. |
| 3269 | * If unmap finds the old page, uncharge() of it will be delayed |
| 3270 | * until end_migration(). If unmap finds a new page, it's |
| 3271 | * uncharged when it make mapcount to be 1->0. If unmap code |
| 3272 | * finds swap_migration_entry, the new page will not be mapped |
| 3273 | * and end_migration() will find it(mapcount==0). |
| 3274 | * |
| 3275 | * B) |
| 3276 | * When the old page was mapped but migraion fails, the kernel |
| 3277 | * remaps it. A charge for it is kept by MIGRATION flag even |
| 3278 | * if mapcount goes down to 0. We can do remap successfully |
| 3279 | * without charging it again. |
| 3280 | * |
| 3281 | * C) |
| 3282 | * The "old" page is under lock_page() until the end of |
| 3283 | * migration, so, the old page itself will not be swapped-out. |
| 3284 | * If the new page is swapped out before end_migraton, our |
| 3285 | * hook to usual swap-out path will catch the event. |
| 3286 | */ |
| 3287 | if (PageAnon(page)) |
| 3288 | SetPageCgroupMigration(pc); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 3289 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3290 | unlock_page_cgroup(pc); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3291 | /* |
| 3292 | * If the page is not charged at this point, |
| 3293 | * we return here. |
| 3294 | */ |
| 3295 | if (!mem) |
| 3296 | return 0; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3297 | |
Andrea Arcangeli | 93d5c9b | 2010-04-23 13:17:39 -0400 | [diff] [blame] | 3298 | *ptr = mem; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3299 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3300 | css_put(&mem->css);/* drop extra refcnt */ |
| 3301 | if (ret || *ptr == NULL) { |
| 3302 | if (PageAnon(page)) { |
| 3303 | lock_page_cgroup(pc); |
| 3304 | ClearPageCgroupMigration(pc); |
| 3305 | unlock_page_cgroup(pc); |
| 3306 | /* |
| 3307 | * The old page may be fully unmapped while we kept it. |
| 3308 | */ |
| 3309 | mem_cgroup_uncharge_page(page); |
| 3310 | } |
| 3311 | return -ENOMEM; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3312 | } |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3313 | /* |
| 3314 | * We charge new page before it's used/mapped. So, even if unlock_page() |
| 3315 | * is called before end_migration, we can catch all events on this new |
| 3316 | * page. In the case new page is migrated but not remapped, new page's |
| 3317 | * mapcount will be finally 0 and we call uncharge in end_migration(). |
| 3318 | */ |
| 3319 | pc = lookup_page_cgroup(newpage); |
| 3320 | if (PageAnon(page)) |
| 3321 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; |
| 3322 | else if (page_is_file_cache(page)) |
| 3323 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; |
| 3324 | else |
| 3325 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3326 | __mem_cgroup_commit_charge(mem, page, 1, pc, ctype); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3327 | return ret; |
| 3328 | } |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 3329 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3330 | /* remove redundant charge if migration failed*/ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3331 | void mem_cgroup_end_migration(struct mem_cgroup *mem, |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 3332 | struct page *oldpage, struct page *newpage, bool migration_ok) |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3333 | { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3334 | struct page *used, *unused; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3335 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3336 | |
| 3337 | if (!mem) |
| 3338 | return; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3339 | /* blocks rmdir() */ |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3340 | cgroup_exclude_rmdir(&mem->css); |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 3341 | if (!migration_ok) { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3342 | used = oldpage; |
| 3343 | unused = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3344 | } else { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3345 | used = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3346 | unused = oldpage; |
| 3347 | } |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3348 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3349 | * We disallowed uncharge of pages under migration because mapcount |
| 3350 | * of the page goes down to zero, temporarly. |
| 3351 | * Clear the flag and check the page should be charged. |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3352 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3353 | pc = lookup_page_cgroup(oldpage); |
| 3354 | lock_page_cgroup(pc); |
| 3355 | ClearPageCgroupMigration(pc); |
| 3356 | unlock_page_cgroup(pc); |
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 | __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE); |
| 3359 | |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3360 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3361 | * If a page is a file cache, radix-tree replacement is very atomic |
| 3362 | * and we can skip this check. When it was an Anon page, its mapcount |
| 3363 | * goes down to 0. But because we added MIGRATION flage, it's not |
| 3364 | * uncharged yet. There are several case but page->mapcount check |
| 3365 | * and USED bit check in mem_cgroup_uncharge_page() will do enough |
| 3366 | * check. (see prepare_charge() also) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3367 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3368 | if (PageAnon(used)) |
| 3369 | mem_cgroup_uncharge_page(used); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3370 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3371 | * At migration, we may charge account against cgroup which has no |
| 3372 | * tasks. |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3373 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 3374 | * In that case, we need to call pre_destroy() again. check it here. |
| 3375 | */ |
| 3376 | cgroup_release_and_wakeup_rmdir(&mem->css); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3377 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 3378 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3379 | /* |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3380 | * A call to try to shrink memory usage on charge failure at shmem's swapin. |
| 3381 | * Calling hierarchical_reclaim is not enough because we should update |
| 3382 | * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM. |
| 3383 | * Moreover considering hierarchy, we should reclaim from the mem_over_limit, |
| 3384 | * not from the memcg which this page would be charged to. |
| 3385 | * try_charge_swapin does all of these works properly. |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3386 | */ |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3387 | int mem_cgroup_shmem_charge_fallback(struct page *page, |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 3388 | struct mm_struct *mm, |
| 3389 | gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3390 | { |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 3391 | struct mem_cgroup *mem; |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3392 | int ret; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3393 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3394 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 3395 | return 0; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3396 | |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3397 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
| 3398 | if (!ret) |
| 3399 | mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */ |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3400 | |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3401 | return ret; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3402 | } |
| 3403 | |
Daisuke Nishimura | f212ad7 | 2011-03-23 16:42:25 -0700 | [diff] [blame] | 3404 | #ifdef CONFIG_DEBUG_VM |
| 3405 | static struct page_cgroup *lookup_page_cgroup_used(struct page *page) |
| 3406 | { |
| 3407 | struct page_cgroup *pc; |
| 3408 | |
| 3409 | pc = lookup_page_cgroup(page); |
| 3410 | if (likely(pc) && PageCgroupUsed(pc)) |
| 3411 | return pc; |
| 3412 | return NULL; |
| 3413 | } |
| 3414 | |
| 3415 | bool mem_cgroup_bad_page_check(struct page *page) |
| 3416 | { |
| 3417 | if (mem_cgroup_disabled()) |
| 3418 | return false; |
| 3419 | |
| 3420 | return lookup_page_cgroup_used(page) != NULL; |
| 3421 | } |
| 3422 | |
| 3423 | void mem_cgroup_print_bad_page(struct page *page) |
| 3424 | { |
| 3425 | struct page_cgroup *pc; |
| 3426 | |
| 3427 | pc = lookup_page_cgroup_used(page); |
| 3428 | if (pc) { |
| 3429 | int ret = -1; |
| 3430 | char *path; |
| 3431 | |
| 3432 | printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p", |
| 3433 | pc, pc->flags, pc->mem_cgroup); |
| 3434 | |
| 3435 | path = kmalloc(PATH_MAX, GFP_KERNEL); |
| 3436 | if (path) { |
| 3437 | rcu_read_lock(); |
| 3438 | ret = cgroup_path(pc->mem_cgroup->css.cgroup, |
| 3439 | path, PATH_MAX); |
| 3440 | rcu_read_unlock(); |
| 3441 | } |
| 3442 | |
| 3443 | printk(KERN_CONT "(%s)\n", |
| 3444 | (ret < 0) ? "cannot get the path" : path); |
| 3445 | kfree(path); |
| 3446 | } |
| 3447 | } |
| 3448 | #endif |
| 3449 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3450 | static DEFINE_MUTEX(set_limit_mutex); |
| 3451 | |
KOSAKI Motohiro | d38d2a7 | 2009-01-06 14:39:44 -0800 | [diff] [blame] | 3452 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3453 | unsigned long long val) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3454 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3455 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3456 | u64 memswlimit, memlimit; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3457 | int ret = 0; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3458 | int children = mem_cgroup_count_children(memcg); |
| 3459 | u64 curusage, oldusage; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3460 | int enlarge; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3461 | |
| 3462 | /* |
| 3463 | * For keeping hierarchical_reclaim simple, how long we should retry |
| 3464 | * is depends on callers. We set our retry-count to be function |
| 3465 | * of # of children which we should visit in this loop. |
| 3466 | */ |
| 3467 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; |
| 3468 | |
| 3469 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3470 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3471 | enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3472 | while (retry_count) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3473 | if (signal_pending(current)) { |
| 3474 | ret = -EINTR; |
| 3475 | break; |
| 3476 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3477 | /* |
| 3478 | * Rather than hide all in some function, I do this in |
| 3479 | * open coded manner. You see what this really does. |
| 3480 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 3481 | */ |
| 3482 | mutex_lock(&set_limit_mutex); |
| 3483 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3484 | if (memswlimit < val) { |
| 3485 | ret = -EINVAL; |
| 3486 | mutex_unlock(&set_limit_mutex); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3487 | break; |
| 3488 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3489 | |
| 3490 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3491 | if (memlimit < val) |
| 3492 | enlarge = 1; |
| 3493 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3494 | ret = res_counter_set_limit(&memcg->res, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 3495 | if (!ret) { |
| 3496 | if (memswlimit == val) |
| 3497 | memcg->memsw_is_minimum = true; |
| 3498 | else |
| 3499 | memcg->memsw_is_minimum = false; |
| 3500 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3501 | mutex_unlock(&set_limit_mutex); |
| 3502 | |
| 3503 | if (!ret) |
| 3504 | break; |
| 3505 | |
Bob Liu | aa20d48 | 2009-12-15 16:47:14 -0800 | [diff] [blame] | 3506 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3507 | MEM_CGROUP_RECLAIM_SHRINK, |
| 3508 | NULL); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3509 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
| 3510 | /* Usage is reduced ? */ |
| 3511 | if (curusage >= oldusage) |
| 3512 | retry_count--; |
| 3513 | else |
| 3514 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3515 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3516 | if (!ret && enlarge) |
| 3517 | memcg_oom_recover(memcg); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 3518 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3519 | return ret; |
| 3520 | } |
| 3521 | |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 3522 | static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
| 3523 | unsigned long long val) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3524 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3525 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3526 | u64 memlimit, memswlimit, oldusage, curusage; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3527 | int children = mem_cgroup_count_children(memcg); |
| 3528 | int ret = -EBUSY; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3529 | int enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3530 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3531 | /* see mem_cgroup_resize_res_limit */ |
| 3532 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; |
| 3533 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3534 | while (retry_count) { |
| 3535 | if (signal_pending(current)) { |
| 3536 | ret = -EINTR; |
| 3537 | break; |
| 3538 | } |
| 3539 | /* |
| 3540 | * Rather than hide all in some function, I do this in |
| 3541 | * open coded manner. You see what this really does. |
| 3542 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 3543 | */ |
| 3544 | mutex_lock(&set_limit_mutex); |
| 3545 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3546 | if (memlimit > val) { |
| 3547 | ret = -EINVAL; |
| 3548 | mutex_unlock(&set_limit_mutex); |
| 3549 | break; |
| 3550 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3551 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3552 | if (memswlimit < val) |
| 3553 | enlarge = 1; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3554 | ret = res_counter_set_limit(&memcg->memsw, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 3555 | if (!ret) { |
| 3556 | if (memlimit == val) |
| 3557 | memcg->memsw_is_minimum = true; |
| 3558 | else |
| 3559 | memcg->memsw_is_minimum = false; |
| 3560 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3561 | mutex_unlock(&set_limit_mutex); |
| 3562 | |
| 3563 | if (!ret) |
| 3564 | break; |
| 3565 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3566 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 3567 | MEM_CGROUP_RECLAIM_NOSWAP | |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3568 | MEM_CGROUP_RECLAIM_SHRINK, |
| 3569 | NULL); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3570 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3571 | /* Usage is reduced ? */ |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3572 | if (curusage >= oldusage) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3573 | retry_count--; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3574 | else |
| 3575 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3576 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3577 | if (!ret && enlarge) |
| 3578 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3579 | return ret; |
| 3580 | } |
| 3581 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3582 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3583 | gfp_t gfp_mask, |
| 3584 | unsigned long *total_scanned) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3585 | { |
| 3586 | unsigned long nr_reclaimed = 0; |
| 3587 | struct mem_cgroup_per_zone *mz, *next_mz = NULL; |
| 3588 | unsigned long reclaimed; |
| 3589 | int loop = 0; |
| 3590 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3591 | unsigned long long excess; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3592 | unsigned long nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3593 | |
| 3594 | if (order > 0) |
| 3595 | return 0; |
| 3596 | |
KOSAKI Motohiro | 00918b6 | 2010-08-10 18:03:05 -0700 | [diff] [blame] | 3597 | 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] | 3598 | /* |
| 3599 | * This loop can run a while, specially if mem_cgroup's continuously |
| 3600 | * keep exceeding their soft limit and putting the system under |
| 3601 | * pressure |
| 3602 | */ |
| 3603 | do { |
| 3604 | if (next_mz) |
| 3605 | mz = next_mz; |
| 3606 | else |
| 3607 | mz = mem_cgroup_largest_soft_limit_node(mctz); |
| 3608 | if (!mz) |
| 3609 | break; |
| 3610 | |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3611 | nr_scanned = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3612 | reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone, |
| 3613 | gfp_mask, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3614 | MEM_CGROUP_RECLAIM_SOFT, |
| 3615 | &nr_scanned); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3616 | nr_reclaimed += reclaimed; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3617 | *total_scanned += nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3618 | spin_lock(&mctz->lock); |
| 3619 | |
| 3620 | /* |
| 3621 | * If we failed to reclaim anything from this memory cgroup |
| 3622 | * it is time to move on to the next cgroup |
| 3623 | */ |
| 3624 | next_mz = NULL; |
| 3625 | if (!reclaimed) { |
| 3626 | do { |
| 3627 | /* |
| 3628 | * Loop until we find yet another one. |
| 3629 | * |
| 3630 | * By the time we get the soft_limit lock |
| 3631 | * again, someone might have aded the |
| 3632 | * group back on the RB tree. Iterate to |
| 3633 | * make sure we get a different mem. |
| 3634 | * mem_cgroup_largest_soft_limit_node returns |
| 3635 | * NULL if no other cgroup is present on |
| 3636 | * the tree |
| 3637 | */ |
| 3638 | next_mz = |
| 3639 | __mem_cgroup_largest_soft_limit_node(mctz); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 3640 | if (next_mz == mz) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3641 | css_put(&next_mz->mem->css); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 3642 | else /* next_mz == NULL or other memcg */ |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3643 | break; |
| 3644 | } while (1); |
| 3645 | } |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3646 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3647 | excess = res_counter_soft_limit_excess(&mz->mem->res); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3648 | /* |
| 3649 | * One school of thought says that we should not add |
| 3650 | * back the node to the tree if reclaim returns 0. |
| 3651 | * But our reclaim could return 0, simply because due |
| 3652 | * to priority we are exposing a smaller subset of |
| 3653 | * memory to reclaim from. Consider this as a longer |
| 3654 | * term TODO. |
| 3655 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3656 | /* If excess == 0, no tree ops */ |
| 3657 | __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3658 | spin_unlock(&mctz->lock); |
| 3659 | css_put(&mz->mem->css); |
| 3660 | loop++; |
| 3661 | /* |
| 3662 | * Could not reclaim anything and there are no more |
| 3663 | * mem cgroups to try or we seem to be looping without |
| 3664 | * reclaiming anything. |
| 3665 | */ |
| 3666 | if (!nr_reclaimed && |
| 3667 | (next_mz == NULL || |
| 3668 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) |
| 3669 | break; |
| 3670 | } while (!nr_reclaimed); |
| 3671 | if (next_mz) |
| 3672 | css_put(&next_mz->mem->css); |
| 3673 | return nr_reclaimed; |
| 3674 | } |
| 3675 | |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3676 | /* |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3677 | * This routine traverse page_cgroup in given list and drop them all. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3678 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
| 3679 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3680 | static int mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3681 | int node, int zid, enum lru_list lru) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3682 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3683 | struct zone *zone; |
| 3684 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3685 | struct page_cgroup *pc, *busy; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3686 | unsigned long flags, loop; |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3687 | struct list_head *list; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3688 | int ret = 0; |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3689 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3690 | zone = &NODE_DATA(node)->node_zones[zid]; |
| 3691 | mz = mem_cgroup_zoneinfo(mem, node, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3692 | list = &mz->lists[lru]; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3693 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3694 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
| 3695 | /* give some margin against EBUSY etc...*/ |
| 3696 | loop += 256; |
| 3697 | busy = NULL; |
| 3698 | while (loop--) { |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3699 | struct page *page; |
| 3700 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3701 | ret = 0; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3702 | spin_lock_irqsave(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3703 | if (list_empty(list)) { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3704 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3705 | break; |
| 3706 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3707 | pc = list_entry(list->prev, struct page_cgroup, lru); |
| 3708 | if (busy == pc) { |
| 3709 | list_move(&pc->lru, list); |
Thiago Farina | 648bcc7 | 2010-03-05 13:42:04 -0800 | [diff] [blame] | 3710 | busy = NULL; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3711 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3712 | continue; |
| 3713 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3714 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3715 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 3716 | page = lookup_cgroup_page(pc); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3717 | |
| 3718 | ret = mem_cgroup_move_parent(page, pc, mem, GFP_KERNEL); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3719 | if (ret == -ENOMEM) |
| 3720 | break; |
| 3721 | |
| 3722 | if (ret == -EBUSY || ret == -EINVAL) { |
| 3723 | /* found lock contention or "pc" is obsolete. */ |
| 3724 | busy = pc; |
| 3725 | cond_resched(); |
| 3726 | } else |
| 3727 | busy = NULL; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3728 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3729 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3730 | if (!ret && !list_empty(list)) |
| 3731 | return -EBUSY; |
| 3732 | return ret; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3733 | } |
| 3734 | |
| 3735 | /* |
| 3736 | * make mem_cgroup's charge to be 0 if there is no task. |
| 3737 | * This enables deleting this mem_cgroup. |
| 3738 | */ |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3739 | static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3740 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3741 | int ret; |
| 3742 | int node, zid, shrink; |
| 3743 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3744 | struct cgroup *cgrp = mem->css.cgroup; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 3745 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3746 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3747 | |
| 3748 | shrink = 0; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3749 | /* should free all ? */ |
| 3750 | if (free_all) |
| 3751 | goto try_to_free; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3752 | move_account: |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3753 | do { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3754 | ret = -EBUSY; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3755 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3756 | goto out; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3757 | ret = -EINTR; |
| 3758 | if (signal_pending(current)) |
| 3759 | goto out; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3760 | /* This is for making all *used* pages to be on LRU. */ |
| 3761 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 3762 | drain_all_stock_sync(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3763 | ret = 0; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 3764 | mem_cgroup_start_move(mem); |
KAMEZAWA Hiroyuki | 299b4ea | 2009-01-29 14:25:17 -0800 | [diff] [blame] | 3765 | for_each_node_state(node, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3766 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3767 | enum lru_list l; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3768 | for_each_lru(l) { |
| 3769 | ret = mem_cgroup_force_empty_list(mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3770 | node, zid, l); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3771 | if (ret) |
| 3772 | break; |
| 3773 | } |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 3774 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3775 | if (ret) |
| 3776 | break; |
| 3777 | } |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 3778 | mem_cgroup_end_move(mem); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3779 | memcg_oom_recover(mem); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3780 | /* it seems parent cgroup doesn't have enough mem */ |
| 3781 | if (ret == -ENOMEM) |
| 3782 | goto try_to_free; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3783 | cond_resched(); |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3784 | /* "ret" should also be checked to ensure all lists are empty. */ |
| 3785 | } while (mem->res.usage > 0 || ret); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3786 | out: |
| 3787 | css_put(&mem->css); |
| 3788 | return ret; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3789 | |
| 3790 | try_to_free: |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3791 | /* returns EBUSY if there is a task or if we come here twice. */ |
| 3792 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3793 | ret = -EBUSY; |
| 3794 | goto out; |
| 3795 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3796 | /* we call try-to-free pages for make this cgroup empty */ |
| 3797 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3798 | /* try to free all pages in this cgroup */ |
| 3799 | shrink = 1; |
| 3800 | while (nr_retries && mem->res.usage > 0) { |
| 3801 | int progress; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3802 | |
| 3803 | if (signal_pending(current)) { |
| 3804 | ret = -EINTR; |
| 3805 | goto out; |
| 3806 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 3807 | progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL, |
| 3808 | false, get_swappiness(mem)); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3809 | if (!progress) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3810 | nr_retries--; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3811 | /* maybe some writeback is necessary */ |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 3812 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3813 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3814 | |
| 3815 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3816 | lru_add_drain(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3817 | /* try move_account...there may be some *locked* pages. */ |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3818 | goto move_account; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3819 | } |
| 3820 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3821 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
| 3822 | { |
| 3823 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); |
| 3824 | } |
| 3825 | |
| 3826 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3827 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
| 3828 | { |
| 3829 | return mem_cgroup_from_cont(cont)->use_hierarchy; |
| 3830 | } |
| 3831 | |
| 3832 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, |
| 3833 | u64 val) |
| 3834 | { |
| 3835 | int retval = 0; |
| 3836 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 3837 | struct cgroup *parent = cont->parent; |
| 3838 | struct mem_cgroup *parent_mem = NULL; |
| 3839 | |
| 3840 | if (parent) |
| 3841 | parent_mem = mem_cgroup_from_cont(parent); |
| 3842 | |
| 3843 | cgroup_lock(); |
| 3844 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 3845 | * 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] | 3846 | * in the child subtrees. If it is unset, then the change can |
| 3847 | * occur, provided the current cgroup has no children. |
| 3848 | * |
| 3849 | * For the root cgroup, parent_mem is NULL, we allow value to be |
| 3850 | * set if there are no children. |
| 3851 | */ |
| 3852 | if ((!parent_mem || !parent_mem->use_hierarchy) && |
| 3853 | (val == 1 || val == 0)) { |
| 3854 | if (list_empty(&cont->children)) |
| 3855 | mem->use_hierarchy = val; |
| 3856 | else |
| 3857 | retval = -EBUSY; |
| 3858 | } else |
| 3859 | retval = -EINVAL; |
| 3860 | cgroup_unlock(); |
| 3861 | |
| 3862 | return retval; |
| 3863 | } |
| 3864 | |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3865 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3866 | static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *mem, |
| 3867 | enum mem_cgroup_stat_index idx) |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3868 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3869 | struct mem_cgroup *iter; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3870 | long val = 0; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3871 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3872 | /* Per-cpu values can be negative, use a signed accumulator */ |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3873 | for_each_mem_cgroup_tree(iter, mem) |
| 3874 | val += mem_cgroup_read_stat(iter, idx); |
| 3875 | |
| 3876 | if (val < 0) /* race ? */ |
| 3877 | val = 0; |
| 3878 | return val; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3879 | } |
| 3880 | |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3881 | static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap) |
| 3882 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3883 | u64 val; |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3884 | |
| 3885 | if (!mem_cgroup_is_root(mem)) { |
| 3886 | if (!swap) |
| 3887 | return res_counter_read_u64(&mem->res, RES_USAGE); |
| 3888 | else |
| 3889 | return res_counter_read_u64(&mem->memsw, RES_USAGE); |
| 3890 | } |
| 3891 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3892 | val = mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_CACHE); |
| 3893 | val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_RSS); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3894 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3895 | if (swap) |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3896 | val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_SWAPOUT); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3897 | |
| 3898 | return val << PAGE_SHIFT; |
| 3899 | } |
| 3900 | |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 3901 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3902 | { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3903 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3904 | u64 val; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3905 | int type, name; |
| 3906 | |
| 3907 | type = MEMFILE_TYPE(cft->private); |
| 3908 | name = MEMFILE_ATTR(cft->private); |
| 3909 | switch (type) { |
| 3910 | case _MEM: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3911 | if (name == RES_USAGE) |
| 3912 | val = mem_cgroup_usage(mem, false); |
| 3913 | else |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3914 | val = res_counter_read_u64(&mem->res, name); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3915 | break; |
| 3916 | case _MEMSWAP: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3917 | if (name == RES_USAGE) |
| 3918 | val = mem_cgroup_usage(mem, true); |
| 3919 | else |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3920 | val = res_counter_read_u64(&mem->memsw, name); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3921 | break; |
| 3922 | default: |
| 3923 | BUG(); |
| 3924 | break; |
| 3925 | } |
| 3926 | return val; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3927 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3928 | /* |
| 3929 | * The user of this function is... |
| 3930 | * RES_LIMIT. |
| 3931 | */ |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 3932 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 3933 | const char *buffer) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3934 | { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3935 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3936 | int type, name; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3937 | unsigned long long val; |
| 3938 | int ret; |
| 3939 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3940 | type = MEMFILE_TYPE(cft->private); |
| 3941 | name = MEMFILE_ATTR(cft->private); |
| 3942 | switch (name) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3943 | case RES_LIMIT: |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 3944 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
| 3945 | ret = -EINVAL; |
| 3946 | break; |
| 3947 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3948 | /* This function does all necessary parse...reuse it */ |
| 3949 | ret = res_counter_memparse_write_strategy(buffer, &val); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3950 | if (ret) |
| 3951 | break; |
| 3952 | if (type == _MEM) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3953 | ret = mem_cgroup_resize_limit(memcg, val); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3954 | else |
| 3955 | ret = mem_cgroup_resize_memsw_limit(memcg, val); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3956 | break; |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 3957 | case RES_SOFT_LIMIT: |
| 3958 | ret = res_counter_memparse_write_strategy(buffer, &val); |
| 3959 | if (ret) |
| 3960 | break; |
| 3961 | /* |
| 3962 | * For memsw, soft limits are hard to implement in terms |
| 3963 | * of semantics, for now, we support soft limits for |
| 3964 | * control without swap |
| 3965 | */ |
| 3966 | if (type == _MEM) |
| 3967 | ret = res_counter_set_soft_limit(&memcg->res, val); |
| 3968 | else |
| 3969 | ret = -EINVAL; |
| 3970 | break; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3971 | default: |
| 3972 | ret = -EINVAL; /* should be BUG() ? */ |
| 3973 | break; |
| 3974 | } |
| 3975 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3976 | } |
| 3977 | |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 3978 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
| 3979 | unsigned long long *mem_limit, unsigned long long *memsw_limit) |
| 3980 | { |
| 3981 | struct cgroup *cgroup; |
| 3982 | unsigned long long min_limit, min_memsw_limit, tmp; |
| 3983 | |
| 3984 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3985 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3986 | cgroup = memcg->css.cgroup; |
| 3987 | if (!memcg->use_hierarchy) |
| 3988 | goto out; |
| 3989 | |
| 3990 | while (cgroup->parent) { |
| 3991 | cgroup = cgroup->parent; |
| 3992 | memcg = mem_cgroup_from_cont(cgroup); |
| 3993 | if (!memcg->use_hierarchy) |
| 3994 | break; |
| 3995 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3996 | min_limit = min(min_limit, tmp); |
| 3997 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3998 | min_memsw_limit = min(min_memsw_limit, tmp); |
| 3999 | } |
| 4000 | out: |
| 4001 | *mem_limit = min_limit; |
| 4002 | *memsw_limit = min_memsw_limit; |
| 4003 | return; |
| 4004 | } |
| 4005 | |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4006 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4007 | { |
| 4008 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4009 | int type, name; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4010 | |
| 4011 | mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4012 | type = MEMFILE_TYPE(event); |
| 4013 | name = MEMFILE_ATTR(event); |
| 4014 | switch (name) { |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4015 | case RES_MAX_USAGE: |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4016 | if (type == _MEM) |
| 4017 | res_counter_reset_max(&mem->res); |
| 4018 | else |
| 4019 | res_counter_reset_max(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4020 | break; |
| 4021 | case RES_FAILCNT: |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4022 | if (type == _MEM) |
| 4023 | res_counter_reset_failcnt(&mem->res); |
| 4024 | else |
| 4025 | res_counter_reset_failcnt(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4026 | break; |
| 4027 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4028 | |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 4029 | return 0; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4030 | } |
| 4031 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4032 | static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp, |
| 4033 | struct cftype *cft) |
| 4034 | { |
| 4035 | return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate; |
| 4036 | } |
| 4037 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4038 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4039 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 4040 | struct cftype *cft, u64 val) |
| 4041 | { |
| 4042 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4043 | |
| 4044 | if (val >= (1 << NR_MOVE_TYPE)) |
| 4045 | return -EINVAL; |
| 4046 | /* |
| 4047 | * We check this value several times in both in can_attach() and |
| 4048 | * attach(), so we need cgroup lock to prevent this value from being |
| 4049 | * inconsistent. |
| 4050 | */ |
| 4051 | cgroup_lock(); |
| 4052 | mem->move_charge_at_immigrate = val; |
| 4053 | cgroup_unlock(); |
| 4054 | |
| 4055 | return 0; |
| 4056 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4057 | #else |
| 4058 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 4059 | struct cftype *cft, u64 val) |
| 4060 | { |
| 4061 | return -ENOSYS; |
| 4062 | } |
| 4063 | #endif |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4064 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4065 | |
| 4066 | /* For read statistics */ |
| 4067 | enum { |
| 4068 | MCS_CACHE, |
| 4069 | MCS_RSS, |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 4070 | MCS_FILE_MAPPED, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4071 | MCS_PGPGIN, |
| 4072 | MCS_PGPGOUT, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4073 | MCS_SWAP, |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4074 | MCS_PGFAULT, |
| 4075 | MCS_PGMAJFAULT, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4076 | MCS_INACTIVE_ANON, |
| 4077 | MCS_ACTIVE_ANON, |
| 4078 | MCS_INACTIVE_FILE, |
| 4079 | MCS_ACTIVE_FILE, |
| 4080 | MCS_UNEVICTABLE, |
| 4081 | NR_MCS_STAT, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4082 | }; |
| 4083 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4084 | struct mcs_total_stat { |
| 4085 | s64 stat[NR_MCS_STAT]; |
| 4086 | }; |
| 4087 | |
| 4088 | struct { |
| 4089 | char *local_name; |
| 4090 | char *total_name; |
| 4091 | } memcg_stat_strings[NR_MCS_STAT] = { |
| 4092 | {"cache", "total_cache"}, |
| 4093 | {"rss", "total_rss"}, |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 4094 | {"mapped_file", "total_mapped_file"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4095 | {"pgpgin", "total_pgpgin"}, |
| 4096 | {"pgpgout", "total_pgpgout"}, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4097 | {"swap", "total_swap"}, |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4098 | {"pgfault", "total_pgfault"}, |
| 4099 | {"pgmajfault", "total_pgmajfault"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4100 | {"inactive_anon", "total_inactive_anon"}, |
| 4101 | {"active_anon", "total_active_anon"}, |
| 4102 | {"inactive_file", "total_inactive_file"}, |
| 4103 | {"active_file", "total_active_file"}, |
| 4104 | {"unevictable", "total_unevictable"} |
| 4105 | }; |
| 4106 | |
| 4107 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4108 | static void |
| 4109 | mem_cgroup_get_local_stat(struct mem_cgroup *mem, struct mcs_total_stat *s) |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4110 | { |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4111 | s64 val; |
| 4112 | |
| 4113 | /* per cpu stat */ |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4114 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4115 | s->stat[MCS_CACHE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4116 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4117 | s->stat[MCS_RSS] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4118 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED); |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 4119 | s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 4120 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGIN); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4121 | s->stat[MCS_PGPGIN] += val; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 4122 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGOUT); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4123 | s->stat[MCS_PGPGOUT] += val; |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4124 | if (do_swap_account) { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4125 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4126 | s->stat[MCS_SWAP] += val * PAGE_SIZE; |
| 4127 | } |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4128 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGFAULT); |
| 4129 | s->stat[MCS_PGFAULT] += val; |
| 4130 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGMAJFAULT); |
| 4131 | s->stat[MCS_PGMAJFAULT] += val; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4132 | |
| 4133 | /* per zone stat */ |
| 4134 | val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON); |
| 4135 | s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE; |
| 4136 | val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON); |
| 4137 | s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE; |
| 4138 | val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE); |
| 4139 | s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE; |
| 4140 | val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE); |
| 4141 | s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE; |
| 4142 | val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE); |
| 4143 | s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4144 | } |
| 4145 | |
| 4146 | static void |
| 4147 | mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s) |
| 4148 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4149 | struct mem_cgroup *iter; |
| 4150 | |
| 4151 | for_each_mem_cgroup_tree(iter, mem) |
| 4152 | mem_cgroup_get_local_stat(iter, s); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4153 | } |
| 4154 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4155 | #ifdef CONFIG_NUMA |
| 4156 | static int mem_control_numa_stat_show(struct seq_file *m, void *arg) |
| 4157 | { |
| 4158 | int nid; |
| 4159 | unsigned long total_nr, file_nr, anon_nr, unevictable_nr; |
| 4160 | unsigned long node_nr; |
| 4161 | struct cgroup *cont = m->private; |
| 4162 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
| 4163 | |
| 4164 | total_nr = mem_cgroup_nr_lru_pages(mem_cont); |
| 4165 | seq_printf(m, "total=%lu", total_nr); |
| 4166 | for_each_node_state(nid, N_HIGH_MEMORY) { |
| 4167 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid); |
| 4168 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4169 | } |
| 4170 | seq_putc(m, '\n'); |
| 4171 | |
| 4172 | file_nr = mem_cgroup_nr_file_lru_pages(mem_cont); |
| 4173 | seq_printf(m, "file=%lu", file_nr); |
| 4174 | for_each_node_state(nid, N_HIGH_MEMORY) { |
| 4175 | node_nr = mem_cgroup_node_nr_file_lru_pages(mem_cont, nid); |
| 4176 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4177 | } |
| 4178 | seq_putc(m, '\n'); |
| 4179 | |
| 4180 | anon_nr = mem_cgroup_nr_anon_lru_pages(mem_cont); |
| 4181 | seq_printf(m, "anon=%lu", anon_nr); |
| 4182 | for_each_node_state(nid, N_HIGH_MEMORY) { |
| 4183 | node_nr = mem_cgroup_node_nr_anon_lru_pages(mem_cont, nid); |
| 4184 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4185 | } |
| 4186 | seq_putc(m, '\n'); |
| 4187 | |
| 4188 | unevictable_nr = mem_cgroup_nr_unevictable_lru_pages(mem_cont); |
| 4189 | seq_printf(m, "unevictable=%lu", unevictable_nr); |
| 4190 | for_each_node_state(nid, N_HIGH_MEMORY) { |
| 4191 | node_nr = mem_cgroup_node_nr_unevictable_lru_pages(mem_cont, |
| 4192 | nid); |
| 4193 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4194 | } |
| 4195 | seq_putc(m, '\n'); |
| 4196 | return 0; |
| 4197 | } |
| 4198 | #endif /* CONFIG_NUMA */ |
| 4199 | |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 4200 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
| 4201 | struct cgroup_map_cb *cb) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4202 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4203 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4204 | struct mcs_total_stat mystat; |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4205 | int i; |
| 4206 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4207 | memset(&mystat, 0, sizeof(mystat)); |
| 4208 | mem_cgroup_get_local_stat(mem_cont, &mystat); |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4209 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4210 | |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4211 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 4212 | if (i == MCS_SWAP && !do_swap_account) |
| 4213 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4214 | cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4215 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4216 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4217 | /* Hierarchical information */ |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 4218 | { |
| 4219 | unsigned long long limit, memsw_limit; |
| 4220 | memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit); |
| 4221 | cb->fill(cb, "hierarchical_memory_limit", limit); |
| 4222 | if (do_swap_account) |
| 4223 | cb->fill(cb, "hierarchical_memsw_limit", memsw_limit); |
| 4224 | } |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4225 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4226 | memset(&mystat, 0, sizeof(mystat)); |
| 4227 | mem_cgroup_get_total_stat(mem_cont, &mystat); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4228 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 4229 | if (i == MCS_SWAP && !do_swap_account) |
| 4230 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4231 | cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4232 | } |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4233 | |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4234 | #ifdef CONFIG_DEBUG_VM |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 4235 | cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL)); |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4236 | |
| 4237 | { |
| 4238 | int nid, zid; |
| 4239 | struct mem_cgroup_per_zone *mz; |
| 4240 | unsigned long recent_rotated[2] = {0, 0}; |
| 4241 | unsigned long recent_scanned[2] = {0, 0}; |
| 4242 | |
| 4243 | for_each_online_node(nid) |
| 4244 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 4245 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
| 4246 | |
| 4247 | recent_rotated[0] += |
| 4248 | mz->reclaim_stat.recent_rotated[0]; |
| 4249 | recent_rotated[1] += |
| 4250 | mz->reclaim_stat.recent_rotated[1]; |
| 4251 | recent_scanned[0] += |
| 4252 | mz->reclaim_stat.recent_scanned[0]; |
| 4253 | recent_scanned[1] += |
| 4254 | mz->reclaim_stat.recent_scanned[1]; |
| 4255 | } |
| 4256 | cb->fill(cb, "recent_rotated_anon", recent_rotated[0]); |
| 4257 | cb->fill(cb, "recent_rotated_file", recent_rotated[1]); |
| 4258 | cb->fill(cb, "recent_scanned_anon", recent_scanned[0]); |
| 4259 | cb->fill(cb, "recent_scanned_file", recent_scanned[1]); |
| 4260 | } |
| 4261 | #endif |
| 4262 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4263 | return 0; |
| 4264 | } |
| 4265 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4266 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
| 4267 | { |
| 4268 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4269 | |
| 4270 | return get_swappiness(memcg); |
| 4271 | } |
| 4272 | |
| 4273 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, |
| 4274 | u64 val) |
| 4275 | { |
| 4276 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4277 | struct mem_cgroup *parent; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4278 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4279 | if (val > 100) |
| 4280 | return -EINVAL; |
| 4281 | |
| 4282 | if (cgrp->parent == NULL) |
| 4283 | return -EINVAL; |
| 4284 | |
| 4285 | parent = mem_cgroup_from_cont(cgrp->parent); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4286 | |
| 4287 | cgroup_lock(); |
| 4288 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4289 | /* If under hierarchy, only empty-root can set this value */ |
| 4290 | if ((parent->use_hierarchy) || |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4291 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
| 4292 | cgroup_unlock(); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4293 | return -EINVAL; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4294 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4295 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4296 | memcg->swappiness = val; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4297 | |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4298 | cgroup_unlock(); |
| 4299 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4300 | return 0; |
| 4301 | } |
| 4302 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4303 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) |
| 4304 | { |
| 4305 | struct mem_cgroup_threshold_ary *t; |
| 4306 | u64 usage; |
| 4307 | int i; |
| 4308 | |
| 4309 | rcu_read_lock(); |
| 4310 | if (!swap) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4311 | t = rcu_dereference(memcg->thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4312 | else |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4313 | t = rcu_dereference(memcg->memsw_thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4314 | |
| 4315 | if (!t) |
| 4316 | goto unlock; |
| 4317 | |
| 4318 | usage = mem_cgroup_usage(memcg, swap); |
| 4319 | |
| 4320 | /* |
| 4321 | * current_threshold points to threshold just below usage. |
| 4322 | * If it's not true, a threshold was crossed after last |
| 4323 | * call of __mem_cgroup_threshold(). |
| 4324 | */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4325 | i = t->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4326 | |
| 4327 | /* |
| 4328 | * Iterate backward over array of thresholds starting from |
| 4329 | * current_threshold and check if a threshold is crossed. |
| 4330 | * If none of thresholds below usage is crossed, we read |
| 4331 | * only one element of the array here. |
| 4332 | */ |
| 4333 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) |
| 4334 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4335 | |
| 4336 | /* i = current_threshold + 1 */ |
| 4337 | i++; |
| 4338 | |
| 4339 | /* |
| 4340 | * Iterate forward over array of thresholds starting from |
| 4341 | * current_threshold+1 and check if a threshold is crossed. |
| 4342 | * If none of thresholds above usage is crossed, we read |
| 4343 | * only one element of the array here. |
| 4344 | */ |
| 4345 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) |
| 4346 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4347 | |
| 4348 | /* Update current_threshold */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4349 | t->current_threshold = i - 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4350 | unlock: |
| 4351 | rcu_read_unlock(); |
| 4352 | } |
| 4353 | |
| 4354 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) |
| 4355 | { |
Kirill A. Shutemov | ad4ca5f | 2010-10-07 12:59:27 -0700 | [diff] [blame] | 4356 | while (memcg) { |
| 4357 | __mem_cgroup_threshold(memcg, false); |
| 4358 | if (do_swap_account) |
| 4359 | __mem_cgroup_threshold(memcg, true); |
| 4360 | |
| 4361 | memcg = parent_mem_cgroup(memcg); |
| 4362 | } |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4363 | } |
| 4364 | |
| 4365 | static int compare_thresholds(const void *a, const void *b) |
| 4366 | { |
| 4367 | const struct mem_cgroup_threshold *_a = a; |
| 4368 | const struct mem_cgroup_threshold *_b = b; |
| 4369 | |
| 4370 | return _a->threshold - _b->threshold; |
| 4371 | } |
| 4372 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4373 | static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4374 | { |
| 4375 | struct mem_cgroup_eventfd_list *ev; |
| 4376 | |
| 4377 | list_for_each_entry(ev, &mem->oom_notify, list) |
| 4378 | eventfd_signal(ev->eventfd, 1); |
| 4379 | return 0; |
| 4380 | } |
| 4381 | |
| 4382 | static void mem_cgroup_oom_notify(struct mem_cgroup *mem) |
| 4383 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4384 | struct mem_cgroup *iter; |
| 4385 | |
| 4386 | for_each_mem_cgroup_tree(iter, mem) |
| 4387 | mem_cgroup_oom_notify_cb(iter); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | static int mem_cgroup_usage_register_event(struct cgroup *cgrp, |
| 4391 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4392 | { |
| 4393 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4394 | struct mem_cgroup_thresholds *thresholds; |
| 4395 | struct mem_cgroup_threshold_ary *new; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4396 | int type = MEMFILE_TYPE(cft->private); |
| 4397 | u64 threshold, usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4398 | int i, size, ret; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4399 | |
| 4400 | ret = res_counter_memparse_write_strategy(args, &threshold); |
| 4401 | if (ret) |
| 4402 | return ret; |
| 4403 | |
| 4404 | mutex_lock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4405 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4406 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4407 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4408 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4409 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4410 | else |
| 4411 | BUG(); |
| 4412 | |
| 4413 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 4414 | |
| 4415 | /* Check if a threshold crossed before adding a new one */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4416 | if (thresholds->primary) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4417 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4418 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4419 | size = thresholds->primary ? thresholds->primary->size + 1 : 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4420 | |
| 4421 | /* Allocate memory for new array of thresholds */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4422 | new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4423 | GFP_KERNEL); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4424 | if (!new) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4425 | ret = -ENOMEM; |
| 4426 | goto unlock; |
| 4427 | } |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4428 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4429 | |
| 4430 | /* Copy thresholds (if any) to new array */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4431 | if (thresholds->primary) { |
| 4432 | memcpy(new->entries, thresholds->primary->entries, (size - 1) * |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4433 | sizeof(struct mem_cgroup_threshold)); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4434 | } |
| 4435 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4436 | /* Add new threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4437 | new->entries[size - 1].eventfd = eventfd; |
| 4438 | new->entries[size - 1].threshold = threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4439 | |
| 4440 | /* Sort thresholds. Registering of new threshold isn't time-critical */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4441 | sort(new->entries, size, sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4442 | compare_thresholds, NULL); |
| 4443 | |
| 4444 | /* Find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4445 | new->current_threshold = -1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4446 | for (i = 0; i < size; i++) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4447 | if (new->entries[i].threshold < usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4448 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4449 | * new->current_threshold will not be used until |
| 4450 | * rcu_assign_pointer(), so it's safe to increment |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4451 | * it here. |
| 4452 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4453 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4454 | } |
| 4455 | } |
| 4456 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4457 | /* Free old spare buffer and save old primary buffer as spare */ |
| 4458 | kfree(thresholds->spare); |
| 4459 | thresholds->spare = thresholds->primary; |
| 4460 | |
| 4461 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4462 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4463 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4464 | synchronize_rcu(); |
| 4465 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4466 | unlock: |
| 4467 | mutex_unlock(&memcg->thresholds_lock); |
| 4468 | |
| 4469 | return ret; |
| 4470 | } |
| 4471 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4472 | static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4473 | struct cftype *cft, struct eventfd_ctx *eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4474 | { |
| 4475 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4476 | struct mem_cgroup_thresholds *thresholds; |
| 4477 | struct mem_cgroup_threshold_ary *new; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4478 | int type = MEMFILE_TYPE(cft->private); |
| 4479 | u64 usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4480 | int i, j, size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4481 | |
| 4482 | mutex_lock(&memcg->thresholds_lock); |
| 4483 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4484 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4485 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4486 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4487 | else |
| 4488 | BUG(); |
| 4489 | |
| 4490 | /* |
| 4491 | * Something went wrong if we trying to unregister a threshold |
| 4492 | * if we don't have thresholds |
| 4493 | */ |
| 4494 | BUG_ON(!thresholds); |
| 4495 | |
| 4496 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 4497 | |
| 4498 | /* Check if a threshold crossed before removing */ |
| 4499 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4500 | |
| 4501 | /* Calculate new number of threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4502 | size = 0; |
| 4503 | for (i = 0; i < thresholds->primary->size; i++) { |
| 4504 | if (thresholds->primary->entries[i].eventfd != eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4505 | size++; |
| 4506 | } |
| 4507 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4508 | new = thresholds->spare; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4509 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4510 | /* Set thresholds array to NULL if we don't have thresholds */ |
| 4511 | if (!size) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4512 | kfree(new); |
| 4513 | new = NULL; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4514 | goto swap_buffers; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4515 | } |
| 4516 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4517 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4518 | |
| 4519 | /* Copy thresholds and find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4520 | new->current_threshold = -1; |
| 4521 | for (i = 0, j = 0; i < thresholds->primary->size; i++) { |
| 4522 | if (thresholds->primary->entries[i].eventfd == eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4523 | continue; |
| 4524 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4525 | new->entries[j] = thresholds->primary->entries[i]; |
| 4526 | if (new->entries[j].threshold < usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4527 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4528 | * new->current_threshold will not be used |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4529 | * until rcu_assign_pointer(), so it's safe to increment |
| 4530 | * it here. |
| 4531 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4532 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4533 | } |
| 4534 | j++; |
| 4535 | } |
| 4536 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4537 | swap_buffers: |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4538 | /* Swap primary and spare array */ |
| 4539 | thresholds->spare = thresholds->primary; |
| 4540 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4541 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4542 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4543 | synchronize_rcu(); |
| 4544 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4545 | mutex_unlock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4546 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4547 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4548 | static int mem_cgroup_oom_register_event(struct cgroup *cgrp, |
| 4549 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
| 4550 | { |
| 4551 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4552 | struct mem_cgroup_eventfd_list *event; |
| 4553 | int type = MEMFILE_TYPE(cft->private); |
| 4554 | |
| 4555 | BUG_ON(type != _OOM_TYPE); |
| 4556 | event = kmalloc(sizeof(*event), GFP_KERNEL); |
| 4557 | if (!event) |
| 4558 | return -ENOMEM; |
| 4559 | |
| 4560 | mutex_lock(&memcg_oom_mutex); |
| 4561 | |
| 4562 | event->eventfd = eventfd; |
| 4563 | list_add(&event->list, &memcg->oom_notify); |
| 4564 | |
| 4565 | /* already in OOM ? */ |
| 4566 | if (atomic_read(&memcg->oom_lock)) |
| 4567 | eventfd_signal(eventfd, 1); |
| 4568 | mutex_unlock(&memcg_oom_mutex); |
| 4569 | |
| 4570 | return 0; |
| 4571 | } |
| 4572 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4573 | static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4574 | struct cftype *cft, struct eventfd_ctx *eventfd) |
| 4575 | { |
| 4576 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4577 | struct mem_cgroup_eventfd_list *ev, *tmp; |
| 4578 | int type = MEMFILE_TYPE(cft->private); |
| 4579 | |
| 4580 | BUG_ON(type != _OOM_TYPE); |
| 4581 | |
| 4582 | mutex_lock(&memcg_oom_mutex); |
| 4583 | |
| 4584 | list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) { |
| 4585 | if (ev->eventfd == eventfd) { |
| 4586 | list_del(&ev->list); |
| 4587 | kfree(ev); |
| 4588 | } |
| 4589 | } |
| 4590 | |
| 4591 | mutex_unlock(&memcg_oom_mutex); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4592 | } |
| 4593 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4594 | static int mem_cgroup_oom_control_read(struct cgroup *cgrp, |
| 4595 | struct cftype *cft, struct cgroup_map_cb *cb) |
| 4596 | { |
| 4597 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4598 | |
| 4599 | cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable); |
| 4600 | |
| 4601 | if (atomic_read(&mem->oom_lock)) |
| 4602 | cb->fill(cb, "under_oom", 1); |
| 4603 | else |
| 4604 | cb->fill(cb, "under_oom", 0); |
| 4605 | return 0; |
| 4606 | } |
| 4607 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4608 | static int mem_cgroup_oom_control_write(struct cgroup *cgrp, |
| 4609 | struct cftype *cft, u64 val) |
| 4610 | { |
| 4611 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4612 | struct mem_cgroup *parent; |
| 4613 | |
| 4614 | /* cannot set to root cgroup and only 0 and 1 are allowed */ |
| 4615 | if (!cgrp->parent || !((val == 0) || (val == 1))) |
| 4616 | return -EINVAL; |
| 4617 | |
| 4618 | parent = mem_cgroup_from_cont(cgrp->parent); |
| 4619 | |
| 4620 | cgroup_lock(); |
| 4621 | /* oom-kill-disable is a flag for subhierarchy. */ |
| 4622 | if ((parent->use_hierarchy) || |
| 4623 | (mem->use_hierarchy && !list_empty(&cgrp->children))) { |
| 4624 | cgroup_unlock(); |
| 4625 | return -EINVAL; |
| 4626 | } |
| 4627 | mem->oom_kill_disable = val; |
KAMEZAWA Hiroyuki | 4d845eb | 2010-06-29 15:05:18 -0700 | [diff] [blame] | 4628 | if (!val) |
| 4629 | memcg_oom_recover(mem); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4630 | cgroup_unlock(); |
| 4631 | return 0; |
| 4632 | } |
| 4633 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4634 | #ifdef CONFIG_NUMA |
| 4635 | static const struct file_operations mem_control_numa_stat_file_operations = { |
| 4636 | .read = seq_read, |
| 4637 | .llseek = seq_lseek, |
| 4638 | .release = single_release, |
| 4639 | }; |
| 4640 | |
| 4641 | static int mem_control_numa_stat_open(struct inode *unused, struct file *file) |
| 4642 | { |
| 4643 | struct cgroup *cont = file->f_dentry->d_parent->d_fsdata; |
| 4644 | |
| 4645 | file->f_op = &mem_control_numa_stat_file_operations; |
| 4646 | return single_open(file, mem_control_numa_stat_show, cont); |
| 4647 | } |
| 4648 | #endif /* CONFIG_NUMA */ |
| 4649 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4650 | static struct cftype mem_cgroup_files[] = { |
| 4651 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4652 | .name = "usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4653 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4654 | .read_u64 = mem_cgroup_read, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4655 | .register_event = mem_cgroup_usage_register_event, |
| 4656 | .unregister_event = mem_cgroup_usage_unregister_event, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4657 | }, |
| 4658 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4659 | .name = "max_usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4660 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4661 | .trigger = mem_cgroup_reset, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4662 | .read_u64 = mem_cgroup_read, |
| 4663 | }, |
| 4664 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4665 | .name = "limit_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4666 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 4667 | .write_string = mem_cgroup_write, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4668 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4669 | }, |
| 4670 | { |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 4671 | .name = "soft_limit_in_bytes", |
| 4672 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), |
| 4673 | .write_string = mem_cgroup_write, |
| 4674 | .read_u64 = mem_cgroup_read, |
| 4675 | }, |
| 4676 | { |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4677 | .name = "failcnt", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4678 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4679 | .trigger = mem_cgroup_reset, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4680 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4681 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 4682 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4683 | .name = "stat", |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 4684 | .read_map = mem_control_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4685 | }, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4686 | { |
| 4687 | .name = "force_empty", |
| 4688 | .trigger = mem_cgroup_force_empty_write, |
| 4689 | }, |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4690 | { |
| 4691 | .name = "use_hierarchy", |
| 4692 | .write_u64 = mem_cgroup_hierarchy_write, |
| 4693 | .read_u64 = mem_cgroup_hierarchy_read, |
| 4694 | }, |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4695 | { |
| 4696 | .name = "swappiness", |
| 4697 | .read_u64 = mem_cgroup_swappiness_read, |
| 4698 | .write_u64 = mem_cgroup_swappiness_write, |
| 4699 | }, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4700 | { |
| 4701 | .name = "move_charge_at_immigrate", |
| 4702 | .read_u64 = mem_cgroup_move_charge_read, |
| 4703 | .write_u64 = mem_cgroup_move_charge_write, |
| 4704 | }, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4705 | { |
| 4706 | .name = "oom_control", |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4707 | .read_map = mem_cgroup_oom_control_read, |
| 4708 | .write_u64 = mem_cgroup_oom_control_write, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4709 | .register_event = mem_cgroup_oom_register_event, |
| 4710 | .unregister_event = mem_cgroup_oom_unregister_event, |
| 4711 | .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL), |
| 4712 | }, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4713 | #ifdef CONFIG_NUMA |
| 4714 | { |
| 4715 | .name = "numa_stat", |
| 4716 | .open = mem_control_numa_stat_open, |
KAMEZAWA Hiroyuki | 8957712 | 2011-06-15 15:08:41 -0700 | [diff] [blame] | 4717 | .mode = S_IRUGO, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4718 | }, |
| 4719 | #endif |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4720 | }; |
| 4721 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4722 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 4723 | static struct cftype memsw_cgroup_files[] = { |
| 4724 | { |
| 4725 | .name = "memsw.usage_in_bytes", |
| 4726 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), |
| 4727 | .read_u64 = mem_cgroup_read, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4728 | .register_event = mem_cgroup_usage_register_event, |
| 4729 | .unregister_event = mem_cgroup_usage_unregister_event, |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4730 | }, |
| 4731 | { |
| 4732 | .name = "memsw.max_usage_in_bytes", |
| 4733 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), |
| 4734 | .trigger = mem_cgroup_reset, |
| 4735 | .read_u64 = mem_cgroup_read, |
| 4736 | }, |
| 4737 | { |
| 4738 | .name = "memsw.limit_in_bytes", |
| 4739 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), |
| 4740 | .write_string = mem_cgroup_write, |
| 4741 | .read_u64 = mem_cgroup_read, |
| 4742 | }, |
| 4743 | { |
| 4744 | .name = "memsw.failcnt", |
| 4745 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), |
| 4746 | .trigger = mem_cgroup_reset, |
| 4747 | .read_u64 = mem_cgroup_read, |
| 4748 | }, |
| 4749 | }; |
| 4750 | |
| 4751 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4752 | { |
| 4753 | if (!do_swap_account) |
| 4754 | return 0; |
| 4755 | return cgroup_add_files(cont, ss, memsw_cgroup_files, |
| 4756 | ARRAY_SIZE(memsw_cgroup_files)); |
| 4757 | }; |
| 4758 | #else |
| 4759 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4760 | { |
| 4761 | return 0; |
| 4762 | } |
| 4763 | #endif |
| 4764 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4765 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 4766 | { |
| 4767 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4768 | struct mem_cgroup_per_zone *mz; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 4769 | enum lru_list l; |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 4770 | int zone, tmp = node; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4771 | /* |
| 4772 | * This routine is called against possible nodes. |
| 4773 | * But it's BUG to call kmalloc() against offline node. |
| 4774 | * |
| 4775 | * TODO: this routine can waste much memory for nodes which will |
| 4776 | * never be onlined. It's better to use memory hotplug callback |
| 4777 | * function. |
| 4778 | */ |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 4779 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 4780 | tmp = -1; |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4781 | pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4782 | if (!pn) |
| 4783 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4784 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4785 | mem->info.nodeinfo[node] = pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4786 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 4787 | mz = &pn->zoneinfo[zone]; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 4788 | for_each_lru(l) |
| 4789 | INIT_LIST_HEAD(&mz->lists[l]); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4790 | mz->usage_in_excess = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4791 | mz->on_tree = false; |
| 4792 | mz->mem = mem; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4793 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4794 | return 0; |
| 4795 | } |
| 4796 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4797 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 4798 | { |
| 4799 | kfree(mem->info.nodeinfo[node]); |
| 4800 | } |
| 4801 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4802 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 4803 | { |
| 4804 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4805 | int size = sizeof(struct mem_cgroup); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4806 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4807 | /* Can be very big if MAX_NUMNODES is very big */ |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4808 | if (size < PAGE_SIZE) |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4809 | mem = kzalloc(size, GFP_KERNEL); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4810 | else |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4811 | mem = vzalloc(size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4812 | |
Dan Carpenter | e7bbcdf | 2010-03-23 13:35:12 -0700 | [diff] [blame] | 4813 | if (!mem) |
| 4814 | return NULL; |
| 4815 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4816 | mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu); |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 4817 | if (!mem->stat) |
| 4818 | goto out_free; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 4819 | spin_lock_init(&mem->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4820 | return mem; |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 4821 | |
| 4822 | out_free: |
| 4823 | if (size < PAGE_SIZE) |
| 4824 | kfree(mem); |
| 4825 | else |
| 4826 | vfree(mem); |
| 4827 | return NULL; |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4828 | } |
| 4829 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4830 | /* |
| 4831 | * At destroying mem_cgroup, references from swap_cgroup can remain. |
| 4832 | * (scanning all at force_empty is too costly...) |
| 4833 | * |
| 4834 | * Instead of clearing all references at force_empty, we remember |
| 4835 | * the number of reference from swap_cgroup and free mem_cgroup when |
| 4836 | * it goes down to 0. |
| 4837 | * |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4838 | * Removal of cgroup itself succeeds regardless of refs from swap. |
| 4839 | */ |
| 4840 | |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 4841 | static void __mem_cgroup_free(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4842 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4843 | int node; |
| 4844 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4845 | mem_cgroup_remove_from_trees(mem); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4846 | free_css_id(&mem_cgroup_subsys, &mem->css); |
| 4847 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4848 | for_each_node_state(node, N_POSSIBLE) |
| 4849 | free_mem_cgroup_per_zone_info(mem, node); |
| 4850 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4851 | free_percpu(mem->stat); |
| 4852 | if (sizeof(struct mem_cgroup) < PAGE_SIZE) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4853 | kfree(mem); |
| 4854 | else |
| 4855 | vfree(mem); |
| 4856 | } |
| 4857 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4858 | static void mem_cgroup_get(struct mem_cgroup *mem) |
| 4859 | { |
| 4860 | atomic_inc(&mem->refcnt); |
| 4861 | } |
| 4862 | |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4863 | static void __mem_cgroup_put(struct mem_cgroup *mem, int count) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4864 | { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4865 | if (atomic_sub_and_test(count, &mem->refcnt)) { |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4866 | struct mem_cgroup *parent = parent_mem_cgroup(mem); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 4867 | __mem_cgroup_free(mem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4868 | if (parent) |
| 4869 | mem_cgroup_put(parent); |
| 4870 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4871 | } |
| 4872 | |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4873 | static void mem_cgroup_put(struct mem_cgroup *mem) |
| 4874 | { |
| 4875 | __mem_cgroup_put(mem, 1); |
| 4876 | } |
| 4877 | |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4878 | /* |
| 4879 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. |
| 4880 | */ |
| 4881 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem) |
| 4882 | { |
| 4883 | if (!mem->res.parent) |
| 4884 | return NULL; |
| 4885 | return mem_cgroup_from_res_counter(mem->res.parent, res); |
| 4886 | } |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4887 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4888 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 4889 | static void __init enable_swap_cgroup(void) |
| 4890 | { |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 4891 | if (!mem_cgroup_disabled() && really_do_swap_account) |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4892 | do_swap_account = 1; |
| 4893 | } |
| 4894 | #else |
| 4895 | static void __init enable_swap_cgroup(void) |
| 4896 | { |
| 4897 | } |
| 4898 | #endif |
| 4899 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4900 | static int mem_cgroup_soft_limit_tree_init(void) |
| 4901 | { |
| 4902 | struct mem_cgroup_tree_per_node *rtpn; |
| 4903 | struct mem_cgroup_tree_per_zone *rtpz; |
| 4904 | int tmp, node, zone; |
| 4905 | |
| 4906 | for_each_node_state(node, N_POSSIBLE) { |
| 4907 | tmp = node; |
| 4908 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 4909 | tmp = -1; |
| 4910 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp); |
| 4911 | if (!rtpn) |
| 4912 | return 1; |
| 4913 | |
| 4914 | soft_limit_tree.rb_tree_per_node[node] = rtpn; |
| 4915 | |
| 4916 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 4917 | rtpz = &rtpn->rb_tree_per_zone[zone]; |
| 4918 | rtpz->rb_root = RB_ROOT; |
| 4919 | spin_lock_init(&rtpz->lock); |
| 4920 | } |
| 4921 | } |
| 4922 | return 0; |
| 4923 | } |
| 4924 | |
Li Zefan | 0eb253e | 2009-01-15 13:51:25 -0800 | [diff] [blame] | 4925 | static struct cgroup_subsys_state * __ref |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4926 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 4927 | { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4928 | struct mem_cgroup *mem, *parent; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4929 | long error = -ENOMEM; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4930 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4931 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4932 | mem = mem_cgroup_alloc(); |
| 4933 | if (!mem) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4934 | return ERR_PTR(error); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 4935 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4936 | for_each_node_state(node, N_POSSIBLE) |
| 4937 | if (alloc_mem_cgroup_per_zone_info(mem, node)) |
| 4938 | goto free_out; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4939 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4940 | /* root ? */ |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4941 | if (cont->parent == NULL) { |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 4942 | int cpu; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4943 | enable_swap_cgroup(); |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4944 | parent = NULL; |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 4945 | root_mem_cgroup = mem; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4946 | if (mem_cgroup_soft_limit_tree_init()) |
| 4947 | goto free_out; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 4948 | for_each_possible_cpu(cpu) { |
| 4949 | struct memcg_stock_pcp *stock = |
| 4950 | &per_cpu(memcg_stock, cpu); |
| 4951 | INIT_WORK(&stock->work, drain_local_stock); |
| 4952 | } |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 4953 | hotcpu_notifier(memcg_cpu_hotplug_callback, 0); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4954 | } else { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4955 | parent = mem_cgroup_from_cont(cont->parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4956 | mem->use_hierarchy = parent->use_hierarchy; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4957 | mem->oom_kill_disable = parent->oom_kill_disable; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4958 | } |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4959 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4960 | if (parent && parent->use_hierarchy) { |
| 4961 | res_counter_init(&mem->res, &parent->res); |
| 4962 | res_counter_init(&mem->memsw, &parent->memsw); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4963 | /* |
| 4964 | * We increment refcnt of the parent to ensure that we can |
| 4965 | * safely access it on res_counter_charge/uncharge. |
| 4966 | * This refcnt will be decremented when freeing this |
| 4967 | * mem_cgroup(see mem_cgroup_put). |
| 4968 | */ |
| 4969 | mem_cgroup_get(parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4970 | } else { |
| 4971 | res_counter_init(&mem->res, NULL); |
| 4972 | res_counter_init(&mem->memsw, NULL); |
| 4973 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4974 | mem->last_scanned_child = 0; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 4975 | mem->last_scanned_node = MAX_NUMNODES; |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4976 | INIT_LIST_HEAD(&mem->oom_notify); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 4977 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4978 | if (parent) |
| 4979 | mem->swappiness = get_swappiness(parent); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 4980 | atomic_set(&mem->refcnt, 1); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4981 | mem->move_charge_at_immigrate = 0; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4982 | mutex_init(&mem->thresholds_lock); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4983 | return &mem->css; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4984 | free_out: |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 4985 | __mem_cgroup_free(mem); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 4986 | root_mem_cgroup = NULL; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4987 | return ERR_PTR(error); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4988 | } |
| 4989 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4990 | static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 4991 | struct cgroup *cont) |
| 4992 | { |
| 4993 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4994 | |
| 4995 | return mem_cgroup_force_empty(mem, false); |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 4996 | } |
| 4997 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4998 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 4999 | struct cgroup *cont) |
| 5000 | { |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 5001 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 5002 | |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 5003 | mem_cgroup_put(mem); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5004 | } |
| 5005 | |
| 5006 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 5007 | struct cgroup *cont) |
| 5008 | { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5009 | int ret; |
| 5010 | |
| 5011 | ret = cgroup_add_files(cont, ss, mem_cgroup_files, |
| 5012 | ARRAY_SIZE(mem_cgroup_files)); |
| 5013 | |
| 5014 | if (!ret) |
| 5015 | ret = register_memsw_files(cont, ss); |
| 5016 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5017 | } |
| 5018 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5019 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5020 | /* Handlers for move charge at task migration. */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5021 | #define PRECHARGE_COUNT_AT_ONCE 256 |
| 5022 | static int mem_cgroup_do_precharge(unsigned long count) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5023 | { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5024 | int ret = 0; |
| 5025 | int batch_count = PRECHARGE_COUNT_AT_ONCE; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5026 | struct mem_cgroup *mem = mc.to; |
| 5027 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5028 | if (mem_cgroup_is_root(mem)) { |
| 5029 | mc.precharge += count; |
| 5030 | /* we don't need css_get for root */ |
| 5031 | return ret; |
| 5032 | } |
| 5033 | /* try to charge at once */ |
| 5034 | if (count > 1) { |
| 5035 | struct res_counter *dummy; |
| 5036 | /* |
| 5037 | * "mem" cannot be under rmdir() because we've already checked |
| 5038 | * by cgroup_lock_live_cgroup() that it is not removed and we |
| 5039 | * are still under the same cgroup_mutex. So we can postpone |
| 5040 | * css_get(). |
| 5041 | */ |
| 5042 | if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy)) |
| 5043 | goto one_by_one; |
| 5044 | if (do_swap_account && res_counter_charge(&mem->memsw, |
| 5045 | PAGE_SIZE * count, &dummy)) { |
| 5046 | res_counter_uncharge(&mem->res, PAGE_SIZE * count); |
| 5047 | goto one_by_one; |
| 5048 | } |
| 5049 | mc.precharge += count; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5050 | return ret; |
| 5051 | } |
| 5052 | one_by_one: |
| 5053 | /* fall back to one by one charge */ |
| 5054 | while (count--) { |
| 5055 | if (signal_pending(current)) { |
| 5056 | ret = -EINTR; |
| 5057 | break; |
| 5058 | } |
| 5059 | if (!batch_count--) { |
| 5060 | batch_count = PRECHARGE_COUNT_AT_ONCE; |
| 5061 | cond_resched(); |
| 5062 | } |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 5063 | ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, 1, &mem, false); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5064 | if (ret || !mem) |
| 5065 | /* mem_cgroup_clear_mc() will do uncharge later */ |
| 5066 | return -ENOMEM; |
| 5067 | mc.precharge++; |
| 5068 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5069 | return ret; |
| 5070 | } |
| 5071 | |
| 5072 | /** |
| 5073 | * is_target_pte_for_mc - check a pte whether it is valid for move charge |
| 5074 | * @vma: the vma the pte to be checked belongs |
| 5075 | * @addr: the address corresponding to the pte to be checked |
| 5076 | * @ptent: the pte to be checked |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5077 | * @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] | 5078 | * |
| 5079 | * Returns |
| 5080 | * 0(MC_TARGET_NONE): if the pte is not a target for move charge. |
| 5081 | * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for |
| 5082 | * move charge. if @target is not NULL, the page is stored in target->page |
| 5083 | * with extra refcnt got(Callers should handle it). |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5084 | * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a |
| 5085 | * target for charge migration. if @target is not NULL, the entry is stored |
| 5086 | * in target->ent. |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5087 | * |
| 5088 | * Called with pte lock held. |
| 5089 | */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5090 | union mc_target { |
| 5091 | struct page *page; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5092 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5093 | }; |
| 5094 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5095 | enum mc_target_type { |
| 5096 | MC_TARGET_NONE, /* not used */ |
| 5097 | MC_TARGET_PAGE, |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5098 | MC_TARGET_SWAP, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5099 | }; |
| 5100 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5101 | static struct page *mc_handle_present_pte(struct vm_area_struct *vma, |
| 5102 | unsigned long addr, pte_t ptent) |
| 5103 | { |
| 5104 | struct page *page = vm_normal_page(vma, addr, ptent); |
| 5105 | |
| 5106 | if (!page || !page_mapped(page)) |
| 5107 | return NULL; |
| 5108 | if (PageAnon(page)) { |
| 5109 | /* we don't move shared anon */ |
| 5110 | if (!move_anon() || page_mapcount(page) > 2) |
| 5111 | return NULL; |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5112 | } else if (!move_file()) |
| 5113 | /* we ignore mapcount for file pages */ |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5114 | return NULL; |
| 5115 | if (!get_page_unless_zero(page)) |
| 5116 | return NULL; |
| 5117 | |
| 5118 | return page; |
| 5119 | } |
| 5120 | |
| 5121 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
| 5122 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5123 | { |
| 5124 | int usage_count; |
| 5125 | struct page *page = NULL; |
| 5126 | swp_entry_t ent = pte_to_swp_entry(ptent); |
| 5127 | |
| 5128 | if (!move_anon() || non_swap_entry(ent)) |
| 5129 | return NULL; |
| 5130 | usage_count = mem_cgroup_count_swap_user(ent, &page); |
| 5131 | if (usage_count > 1) { /* we don't move shared anon */ |
| 5132 | if (page) |
| 5133 | put_page(page); |
| 5134 | return NULL; |
| 5135 | } |
| 5136 | if (do_swap_account) |
| 5137 | entry->val = ent.val; |
| 5138 | |
| 5139 | return page; |
| 5140 | } |
| 5141 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5142 | static struct page *mc_handle_file_pte(struct vm_area_struct *vma, |
| 5143 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5144 | { |
| 5145 | struct page *page = NULL; |
| 5146 | struct inode *inode; |
| 5147 | struct address_space *mapping; |
| 5148 | pgoff_t pgoff; |
| 5149 | |
| 5150 | if (!vma->vm_file) /* anonymous vma */ |
| 5151 | return NULL; |
| 5152 | if (!move_file()) |
| 5153 | return NULL; |
| 5154 | |
| 5155 | inode = vma->vm_file->f_path.dentry->d_inode; |
| 5156 | mapping = vma->vm_file->f_mapping; |
| 5157 | if (pte_none(ptent)) |
| 5158 | pgoff = linear_page_index(vma, addr); |
| 5159 | else /* pte_file(ptent) is true */ |
| 5160 | pgoff = pte_to_pgoff(ptent); |
| 5161 | |
| 5162 | /* page is moved even if it's not RSS of this task(page-faulted). */ |
| 5163 | if (!mapping_cap_swap_backed(mapping)) { /* normal file */ |
| 5164 | page = find_get_page(mapping, pgoff); |
| 5165 | } else { /* shmem/tmpfs file. we should take account of swap too. */ |
| 5166 | swp_entry_t ent; |
| 5167 | mem_cgroup_get_shmem_target(inode, pgoff, &page, &ent); |
| 5168 | if (do_swap_account) |
| 5169 | entry->val = ent.val; |
| 5170 | } |
| 5171 | |
| 5172 | return page; |
| 5173 | } |
| 5174 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5175 | static int is_target_pte_for_mc(struct vm_area_struct *vma, |
| 5176 | unsigned long addr, pte_t ptent, union mc_target *target) |
| 5177 | { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5178 | struct page *page = NULL; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5179 | struct page_cgroup *pc; |
| 5180 | int ret = 0; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5181 | swp_entry_t ent = { .val = 0 }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5182 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5183 | if (pte_present(ptent)) |
| 5184 | page = mc_handle_present_pte(vma, addr, ptent); |
| 5185 | else if (is_swap_pte(ptent)) |
| 5186 | page = mc_handle_swap_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5187 | else if (pte_none(ptent) || pte_file(ptent)) |
| 5188 | page = mc_handle_file_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5189 | |
| 5190 | if (!page && !ent.val) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5191 | return 0; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5192 | if (page) { |
| 5193 | pc = lookup_page_cgroup(page); |
| 5194 | /* |
| 5195 | * Do only loose check w/o page_cgroup lock. |
| 5196 | * mem_cgroup_move_account() checks the pc is valid or not under |
| 5197 | * the lock. |
| 5198 | */ |
| 5199 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { |
| 5200 | ret = MC_TARGET_PAGE; |
| 5201 | if (target) |
| 5202 | target->page = page; |
| 5203 | } |
| 5204 | if (!ret || !target) |
| 5205 | put_page(page); |
| 5206 | } |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5207 | /* There is a swap entry and a page doesn't exist or isn't charged */ |
| 5208 | if (ent.val && !ret && |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5209 | css_id(&mc.from->css) == lookup_swap_cgroup(ent)) { |
| 5210 | ret = MC_TARGET_SWAP; |
| 5211 | if (target) |
| 5212 | target->ent = ent; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5213 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5214 | return ret; |
| 5215 | } |
| 5216 | |
| 5217 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, |
| 5218 | unsigned long addr, unsigned long end, |
| 5219 | struct mm_walk *walk) |
| 5220 | { |
| 5221 | struct vm_area_struct *vma = walk->private; |
| 5222 | pte_t *pte; |
| 5223 | spinlock_t *ptl; |
| 5224 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 5225 | split_huge_page_pmd(walk->mm, pmd); |
| 5226 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5227 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5228 | for (; addr != end; pte++, addr += PAGE_SIZE) |
| 5229 | if (is_target_pte_for_mc(vma, addr, *pte, NULL)) |
| 5230 | mc.precharge++; /* increment precharge temporarily */ |
| 5231 | pte_unmap_unlock(pte - 1, ptl); |
| 5232 | cond_resched(); |
| 5233 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5234 | return 0; |
| 5235 | } |
| 5236 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5237 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
| 5238 | { |
| 5239 | unsigned long precharge; |
| 5240 | struct vm_area_struct *vma; |
| 5241 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5242 | down_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5243 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5244 | struct mm_walk mem_cgroup_count_precharge_walk = { |
| 5245 | .pmd_entry = mem_cgroup_count_precharge_pte_range, |
| 5246 | .mm = mm, |
| 5247 | .private = vma, |
| 5248 | }; |
| 5249 | if (is_vm_hugetlb_page(vma)) |
| 5250 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5251 | walk_page_range(vma->vm_start, vma->vm_end, |
| 5252 | &mem_cgroup_count_precharge_walk); |
| 5253 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5254 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5255 | |
| 5256 | precharge = mc.precharge; |
| 5257 | mc.precharge = 0; |
| 5258 | |
| 5259 | return precharge; |
| 5260 | } |
| 5261 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5262 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
| 5263 | { |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5264 | unsigned long precharge = mem_cgroup_count_precharge(mm); |
| 5265 | |
| 5266 | VM_BUG_ON(mc.moving_task); |
| 5267 | mc.moving_task = current; |
| 5268 | return mem_cgroup_do_precharge(precharge); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5269 | } |
| 5270 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5271 | /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */ |
| 5272 | static void __mem_cgroup_clear_mc(void) |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5273 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5274 | struct mem_cgroup *from = mc.from; |
| 5275 | struct mem_cgroup *to = mc.to; |
| 5276 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5277 | /* we must uncharge all the leftover precharges from mc.to */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5278 | if (mc.precharge) { |
| 5279 | __mem_cgroup_cancel_charge(mc.to, mc.precharge); |
| 5280 | mc.precharge = 0; |
| 5281 | } |
| 5282 | /* |
| 5283 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so |
| 5284 | * we must uncharge here. |
| 5285 | */ |
| 5286 | if (mc.moved_charge) { |
| 5287 | __mem_cgroup_cancel_charge(mc.from, mc.moved_charge); |
| 5288 | mc.moved_charge = 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5289 | } |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5290 | /* we must fixup refcnts and charges */ |
| 5291 | if (mc.moved_swap) { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5292 | /* uncharge swap account from the old cgroup */ |
| 5293 | if (!mem_cgroup_is_root(mc.from)) |
| 5294 | res_counter_uncharge(&mc.from->memsw, |
| 5295 | PAGE_SIZE * mc.moved_swap); |
| 5296 | __mem_cgroup_put(mc.from, mc.moved_swap); |
| 5297 | |
| 5298 | if (!mem_cgroup_is_root(mc.to)) { |
| 5299 | /* |
| 5300 | * we charged both to->res and to->memsw, so we should |
| 5301 | * uncharge to->res. |
| 5302 | */ |
| 5303 | res_counter_uncharge(&mc.to->res, |
| 5304 | PAGE_SIZE * mc.moved_swap); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5305 | } |
| 5306 | /* we've already done mem_cgroup_get(mc.to) */ |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5307 | mc.moved_swap = 0; |
| 5308 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5309 | memcg_oom_recover(from); |
| 5310 | memcg_oom_recover(to); |
| 5311 | wake_up_all(&mc.waitq); |
| 5312 | } |
| 5313 | |
| 5314 | static void mem_cgroup_clear_mc(void) |
| 5315 | { |
| 5316 | struct mem_cgroup *from = mc.from; |
| 5317 | |
| 5318 | /* |
| 5319 | * we must clear moving_task before waking up waiters at the end of |
| 5320 | * task migration. |
| 5321 | */ |
| 5322 | mc.moving_task = NULL; |
| 5323 | __mem_cgroup_clear_mc(); |
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 = NULL; |
| 5326 | mc.to = NULL; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5327 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5328 | mem_cgroup_end_move(from); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5329 | } |
| 5330 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5331 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 5332 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5333 | struct task_struct *p) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5334 | { |
| 5335 | int ret = 0; |
| 5336 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup); |
| 5337 | |
| 5338 | if (mem->move_charge_at_immigrate) { |
| 5339 | struct mm_struct *mm; |
| 5340 | struct mem_cgroup *from = mem_cgroup_from_task(p); |
| 5341 | |
| 5342 | VM_BUG_ON(from == mem); |
| 5343 | |
| 5344 | mm = get_task_mm(p); |
| 5345 | if (!mm) |
| 5346 | return 0; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5347 | /* We move charges only when we move a owner of the mm */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5348 | if (mm->owner == p) { |
| 5349 | VM_BUG_ON(mc.from); |
| 5350 | VM_BUG_ON(mc.to); |
| 5351 | VM_BUG_ON(mc.precharge); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5352 | VM_BUG_ON(mc.moved_charge); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5353 | VM_BUG_ON(mc.moved_swap); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5354 | mem_cgroup_start_move(from); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5355 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5356 | mc.from = from; |
| 5357 | mc.to = mem; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5358 | spin_unlock(&mc.lock); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5359 | /* We set mc.moving_task later */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5360 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5361 | ret = mem_cgroup_precharge_mc(mm); |
| 5362 | if (ret) |
| 5363 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5364 | } |
| 5365 | mmput(mm); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5366 | } |
| 5367 | return ret; |
| 5368 | } |
| 5369 | |
| 5370 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 5371 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5372 | struct task_struct *p) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5373 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5374 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5375 | } |
| 5376 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5377 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
| 5378 | unsigned long addr, unsigned long end, |
| 5379 | struct mm_walk *walk) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5380 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5381 | int ret = 0; |
| 5382 | struct vm_area_struct *vma = walk->private; |
| 5383 | pte_t *pte; |
| 5384 | spinlock_t *ptl; |
| 5385 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 5386 | split_huge_page_pmd(walk->mm, pmd); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5387 | retry: |
| 5388 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5389 | for (; addr != end; addr += PAGE_SIZE) { |
| 5390 | pte_t ptent = *(pte++); |
| 5391 | union mc_target target; |
| 5392 | int type; |
| 5393 | struct page *page; |
| 5394 | struct page_cgroup *pc; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5395 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5396 | |
| 5397 | if (!mc.precharge) |
| 5398 | break; |
| 5399 | |
| 5400 | type = is_target_pte_for_mc(vma, addr, ptent, &target); |
| 5401 | switch (type) { |
| 5402 | case MC_TARGET_PAGE: |
| 5403 | page = target.page; |
| 5404 | if (isolate_lru_page(page)) |
| 5405 | goto put; |
| 5406 | pc = lookup_page_cgroup(page); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 5407 | if (!mem_cgroup_move_account(page, 1, pc, |
| 5408 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5409 | mc.precharge--; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5410 | /* we uncharge from mc.from later. */ |
| 5411 | mc.moved_charge++; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5412 | } |
| 5413 | putback_lru_page(page); |
| 5414 | put: /* is_target_pte_for_mc() gets the page */ |
| 5415 | put_page(page); |
| 5416 | break; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5417 | case MC_TARGET_SWAP: |
| 5418 | ent = target.ent; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5419 | if (!mem_cgroup_move_swap_account(ent, |
| 5420 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5421 | mc.precharge--; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5422 | /* we fixup refcnts and charges later. */ |
| 5423 | mc.moved_swap++; |
| 5424 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5425 | break; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5426 | default: |
| 5427 | break; |
| 5428 | } |
| 5429 | } |
| 5430 | pte_unmap_unlock(pte - 1, ptl); |
| 5431 | cond_resched(); |
| 5432 | |
| 5433 | if (addr != end) { |
| 5434 | /* |
| 5435 | * We have consumed all precharges we got in can_attach(). |
| 5436 | * We try charge one by one, but don't do any additional |
| 5437 | * charges to mc.to if we have failed in charge once in attach() |
| 5438 | * phase. |
| 5439 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5440 | ret = mem_cgroup_do_precharge(1); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5441 | if (!ret) |
| 5442 | goto retry; |
| 5443 | } |
| 5444 | |
| 5445 | return ret; |
| 5446 | } |
| 5447 | |
| 5448 | static void mem_cgroup_move_charge(struct mm_struct *mm) |
| 5449 | { |
| 5450 | struct vm_area_struct *vma; |
| 5451 | |
| 5452 | lru_add_drain_all(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5453 | retry: |
| 5454 | if (unlikely(!down_read_trylock(&mm->mmap_sem))) { |
| 5455 | /* |
| 5456 | * Someone who are holding the mmap_sem might be waiting in |
| 5457 | * waitq. So we cancel all extra charges, wake up all waiters, |
| 5458 | * and retry. Because we cancel precharges, we might not be able |
| 5459 | * to move enough charges, but moving charge is a best-effort |
| 5460 | * feature anyway, so it wouldn't be a big problem. |
| 5461 | */ |
| 5462 | __mem_cgroup_clear_mc(); |
| 5463 | cond_resched(); |
| 5464 | goto retry; |
| 5465 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5466 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5467 | int ret; |
| 5468 | struct mm_walk mem_cgroup_move_charge_walk = { |
| 5469 | .pmd_entry = mem_cgroup_move_charge_pte_range, |
| 5470 | .mm = mm, |
| 5471 | .private = vma, |
| 5472 | }; |
| 5473 | if (is_vm_hugetlb_page(vma)) |
| 5474 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5475 | ret = walk_page_range(vma->vm_start, vma->vm_end, |
| 5476 | &mem_cgroup_move_charge_walk); |
| 5477 | if (ret) |
| 5478 | /* |
| 5479 | * means we have consumed all precharges and failed in |
| 5480 | * doing additional charge. Just abandon here. |
| 5481 | */ |
| 5482 | break; |
| 5483 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5484 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5485 | } |
| 5486 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5487 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 5488 | struct cgroup *cont, |
| 5489 | struct cgroup *old_cont, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5490 | struct task_struct *p) |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5491 | { |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5492 | struct mm_struct *mm = get_task_mm(p); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5493 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5494 | if (mm) { |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5495 | if (mc.to) |
| 5496 | mem_cgroup_move_charge(mm); |
| 5497 | put_swap_token(mm); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5498 | mmput(mm); |
| 5499 | } |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5500 | if (mc.to) |
| 5501 | mem_cgroup_clear_mc(); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5502 | } |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5503 | #else /* !CONFIG_MMU */ |
| 5504 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 5505 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5506 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5507 | { |
| 5508 | return 0; |
| 5509 | } |
| 5510 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 5511 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5512 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5513 | { |
| 5514 | } |
| 5515 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 5516 | struct cgroup *cont, |
| 5517 | struct cgroup *old_cont, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5518 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5519 | { |
| 5520 | } |
| 5521 | #endif |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5522 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5523 | struct cgroup_subsys mem_cgroup_subsys = { |
| 5524 | .name = "memory", |
| 5525 | .subsys_id = mem_cgroup_subsys_id, |
| 5526 | .create = mem_cgroup_create, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5527 | .pre_destroy = mem_cgroup_pre_destroy, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5528 | .destroy = mem_cgroup_destroy, |
| 5529 | .populate = mem_cgroup_populate, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5530 | .can_attach = mem_cgroup_can_attach, |
| 5531 | .cancel_attach = mem_cgroup_cancel_attach, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5532 | .attach = mem_cgroup_move_task, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5533 | .early_init = 0, |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5534 | .use_id = 1, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5535 | }; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5536 | |
| 5537 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5538 | static int __init enable_swap_account(char *s) |
| 5539 | { |
| 5540 | /* consider enabled if no parameter or 1 is given */ |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5541 | if (!strcmp(s, "1")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5542 | really_do_swap_account = 1; |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5543 | else if (!strcmp(s, "0")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5544 | really_do_swap_account = 0; |
| 5545 | return 1; |
| 5546 | } |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5547 | __setup("swapaccount=", enable_swap_account); |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5548 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5549 | #endif |