Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1 | /* memcontrol.c - Memory Controller |
| 2 | * |
| 3 | * Copyright IBM Corporation, 2007 |
| 4 | * Author Balbir Singh <balbir@linux.vnet.ibm.com> |
| 5 | * |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 6 | * Copyright 2007 OpenVZ SWsoft Inc |
| 7 | * Author: Pavel Emelianov <xemul@openvz.org> |
| 8 | * |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 9 | * Memory thresholds |
| 10 | * Copyright (C) 2009 Nokia Corporation |
| 11 | * Author: Kirill A. Shutemov |
| 12 | * |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/res_counter.h> |
| 25 | #include <linux/memcontrol.h> |
| 26 | #include <linux/cgroup.h> |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 27 | #include <linux/mm.h> |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 28 | #include <linux/hugetlb.h> |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 29 | #include <linux/pagemap.h> |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 30 | #include <linux/smp.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 31 | #include <linux/page-flags.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 32 | #include <linux/backing-dev.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 33 | #include <linux/bit_spinlock.h> |
| 34 | #include <linux/rcupdate.h> |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 35 | #include <linux/limits.h> |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 36 | #include <linux/mutex.h> |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 37 | #include <linux/rbtree.h> |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 38 | #include <linux/slab.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 39 | #include <linux/swap.h> |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 40 | #include <linux/swapops.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 41 | #include <linux/spinlock.h> |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 42 | #include <linux/eventfd.h> |
| 43 | #include <linux/sort.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 44 | #include <linux/fs.h> |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 45 | #include <linux/seq_file.h> |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 46 | #include <linux/vmalloc.h> |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 47 | #include <linux/mm_inline.h> |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 48 | #include <linux/page_cgroup.h> |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 49 | #include <linux/cpu.h> |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 50 | #include "internal.h" |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 51 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 52 | #include <asm/uaccess.h> |
| 53 | |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 54 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 55 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 56 | struct mem_cgroup *root_mem_cgroup __read_mostly; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 57 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 58 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 59 | /* 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] | 60 | int do_swap_account __read_mostly; |
| 61 | static int really_do_swap_account __initdata = 1; /* for remember boot option*/ |
| 62 | #else |
| 63 | #define do_swap_account (0) |
| 64 | #endif |
| 65 | |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 66 | /* |
| 67 | * Per memcg event counter is incremented at every pagein/pageout. This counter |
| 68 | * is used for trigger some periodic events. This is straightforward and better |
| 69 | * than using jiffies etc. to handle periodic memcg event. |
| 70 | * |
| 71 | * These values will be used as !((event) & ((1 <<(thresh)) - 1)) |
| 72 | */ |
| 73 | #define THRESHOLDS_EVENTS_THRESH (7) /* once in 128 */ |
| 74 | #define SOFTLIMIT_EVENTS_THRESH (10) /* once in 1024 */ |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 75 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 76 | /* |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 77 | * Statistics for memory cgroup. |
| 78 | */ |
| 79 | enum mem_cgroup_stat_index { |
| 80 | /* |
| 81 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. |
| 82 | */ |
| 83 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 84 | MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */ |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 85 | MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */ |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 86 | MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */ |
| 87 | MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 88 | MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */ |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 89 | MEM_CGROUP_EVENTS, /* incremented at every pagein/pageout */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 90 | |
| 91 | MEM_CGROUP_STAT_NSTATS, |
| 92 | }; |
| 93 | |
| 94 | struct mem_cgroup_stat_cpu { |
| 95 | s64 count[MEM_CGROUP_STAT_NSTATS]; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 96 | }; |
| 97 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 98 | /* |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 99 | * per-zone information in memory controller. |
| 100 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 101 | struct mem_cgroup_per_zone { |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 102 | /* |
| 103 | * spin_lock to protect the per cgroup LRU |
| 104 | */ |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 105 | struct list_head lists[NR_LRU_LISTS]; |
| 106 | unsigned long count[NR_LRU_LISTS]; |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 107 | |
| 108 | struct zone_reclaim_stat reclaim_stat; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 109 | struct rb_node tree_node; /* RB tree node */ |
| 110 | unsigned long long usage_in_excess;/* Set to the value by which */ |
| 111 | /* the soft limit is exceeded*/ |
| 112 | bool on_tree; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 113 | struct mem_cgroup *mem; /* Back pointer, we cannot */ |
| 114 | /* use container_of */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 115 | }; |
| 116 | /* Macro for accessing counter */ |
| 117 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) |
| 118 | |
| 119 | struct mem_cgroup_per_node { |
| 120 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; |
| 121 | }; |
| 122 | |
| 123 | struct mem_cgroup_lru_info { |
| 124 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; |
| 125 | }; |
| 126 | |
| 127 | /* |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 128 | * Cgroups above their limits are maintained in a RB-Tree, independent of |
| 129 | * their hierarchy representation |
| 130 | */ |
| 131 | |
| 132 | struct mem_cgroup_tree_per_zone { |
| 133 | struct rb_root rb_root; |
| 134 | spinlock_t lock; |
| 135 | }; |
| 136 | |
| 137 | struct mem_cgroup_tree_per_node { |
| 138 | struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES]; |
| 139 | }; |
| 140 | |
| 141 | struct mem_cgroup_tree { |
| 142 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; |
| 143 | }; |
| 144 | |
| 145 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; |
| 146 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 147 | struct mem_cgroup_threshold { |
| 148 | struct eventfd_ctx *eventfd; |
| 149 | u64 threshold; |
| 150 | }; |
| 151 | |
| 152 | struct mem_cgroup_threshold_ary { |
| 153 | /* An array index points to threshold just below usage. */ |
| 154 | atomic_t current_threshold; |
| 155 | /* Size of entries[] */ |
| 156 | unsigned int size; |
| 157 | /* Array of thresholds */ |
| 158 | struct mem_cgroup_threshold entries[0]; |
| 159 | }; |
| 160 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 161 | static void mem_cgroup_threshold(struct mem_cgroup *mem); |
| 162 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 163 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 164 | * The memory controller data structure. The memory controller controls both |
| 165 | * page cache and RSS per cgroup. We would eventually like to provide |
| 166 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, |
| 167 | * to help the administrator determine what knobs to tune. |
| 168 | * |
| 169 | * 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] | 170 | * we hit the water mark. May be even add a low water mark, such that |
| 171 | * no reclaim occurs from a cgroup at it's low water mark, this is |
| 172 | * a feature that will be implemented much later in the future. |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 173 | */ |
| 174 | struct mem_cgroup { |
| 175 | struct cgroup_subsys_state css; |
| 176 | /* |
| 177 | * the counter to account for memory usage |
| 178 | */ |
| 179 | struct res_counter res; |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 180 | /* |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 181 | * the counter to account for mem+swap usage. |
| 182 | */ |
| 183 | struct res_counter memsw; |
| 184 | /* |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 185 | * Per cgroup active and inactive list, similar to the |
| 186 | * per zone LRU lists. |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 187 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 188 | struct mem_cgroup_lru_info info; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 189 | |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 190 | /* |
| 191 | protect against reclaim related member. |
| 192 | */ |
| 193 | spinlock_t reclaim_param_lock; |
| 194 | |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 195 | int prev_priority; /* for recording reclaim priority */ |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 196 | |
| 197 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 198 | * While reclaiming in a hierarchy, we cache the last child we |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 199 | * reclaimed from. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 200 | */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 201 | int last_scanned_child; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 202 | /* |
| 203 | * Should the accounting and control be hierarchical, per subtree? |
| 204 | */ |
| 205 | bool use_hierarchy; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 206 | atomic_t oom_lock; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 207 | atomic_t refcnt; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 208 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 209 | unsigned int swappiness; |
| 210 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 211 | /* set when res.limit == memsw.limit */ |
| 212 | bool memsw_is_minimum; |
| 213 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 214 | /* protect arrays of thresholds */ |
| 215 | struct mutex thresholds_lock; |
| 216 | |
| 217 | /* thresholds for memory usage. RCU-protected */ |
| 218 | struct mem_cgroup_threshold_ary *thresholds; |
| 219 | |
| 220 | /* thresholds for mem+swap usage. RCU-protected */ |
| 221 | struct mem_cgroup_threshold_ary *memsw_thresholds; |
| 222 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 223 | /* |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 224 | * Should we move charges of a task when a task is moved into this |
| 225 | * mem_cgroup ? And what type of charges should we move ? |
| 226 | */ |
| 227 | unsigned long move_charge_at_immigrate; |
| 228 | |
| 229 | /* |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 230 | * percpu counter. |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 231 | */ |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 232 | struct mem_cgroup_stat_cpu *stat; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 233 | }; |
| 234 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 235 | /* Stuffs for move charges at task migration. */ |
| 236 | /* |
| 237 | * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a |
| 238 | * left-shifted bitmap of these types. |
| 239 | */ |
| 240 | enum move_type { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 241 | MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 242 | NR_MOVE_TYPE, |
| 243 | }; |
| 244 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 245 | /* "mc" and its members are protected by cgroup_mutex */ |
| 246 | static struct move_charge_struct { |
| 247 | struct mem_cgroup *from; |
| 248 | struct mem_cgroup *to; |
| 249 | unsigned long precharge; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 250 | unsigned long moved_charge; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 251 | unsigned long moved_swap; |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 252 | struct task_struct *moving_task; /* a task moving charges */ |
| 253 | wait_queue_head_t waitq; /* a waitq for other context */ |
| 254 | } mc = { |
| 255 | .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq), |
| 256 | }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 257 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 258 | /* |
| 259 | * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft |
| 260 | * limit reclaim to prevent infinite loops, if they ever occur. |
| 261 | */ |
| 262 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100) |
| 263 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2) |
| 264 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 265 | enum charge_type { |
| 266 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
| 267 | MEM_CGROUP_CHARGE_TYPE_MAPPED, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 268 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 269 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 270 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 271 | MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 272 | NR_CHARGE_TYPE, |
| 273 | }; |
| 274 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 275 | /* only for here (for easy reading.) */ |
| 276 | #define PCGF_CACHE (1UL << PCG_CACHE) |
| 277 | #define PCGF_USED (1UL << PCG_USED) |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 278 | #define PCGF_LOCK (1UL << PCG_LOCK) |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 279 | /* Not used, but added here for completeness */ |
| 280 | #define PCGF_ACCT (1UL << PCG_ACCT) |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 281 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 282 | /* for encoding cft->private value on file */ |
| 283 | #define _MEM (0) |
| 284 | #define _MEMSWAP (1) |
| 285 | #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) |
| 286 | #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff) |
| 287 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
| 288 | |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 289 | /* |
| 290 | * Reclaim flags for mem_cgroup_hierarchical_reclaim |
| 291 | */ |
| 292 | #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0 |
| 293 | #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT) |
| 294 | #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1 |
| 295 | #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 296 | #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2 |
| 297 | #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT) |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 298 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 299 | static void mem_cgroup_get(struct mem_cgroup *mem); |
| 300 | static void mem_cgroup_put(struct mem_cgroup *mem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 301 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 302 | static void drain_all_stock_async(void); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 303 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 304 | static struct mem_cgroup_per_zone * |
| 305 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) |
| 306 | { |
| 307 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; |
| 308 | } |
| 309 | |
Wu Fengguang | d324236 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 310 | struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem) |
| 311 | { |
| 312 | return &mem->css; |
| 313 | } |
| 314 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 315 | static struct mem_cgroup_per_zone * |
| 316 | page_cgroup_zoneinfo(struct page_cgroup *pc) |
| 317 | { |
| 318 | struct mem_cgroup *mem = pc->mem_cgroup; |
| 319 | int nid = page_cgroup_nid(pc); |
| 320 | int zid = page_cgroup_zid(pc); |
| 321 | |
| 322 | if (!mem) |
| 323 | return NULL; |
| 324 | |
| 325 | return mem_cgroup_zoneinfo(mem, nid, zid); |
| 326 | } |
| 327 | |
| 328 | static struct mem_cgroup_tree_per_zone * |
| 329 | soft_limit_tree_node_zone(int nid, int zid) |
| 330 | { |
| 331 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 332 | } |
| 333 | |
| 334 | static struct mem_cgroup_tree_per_zone * |
| 335 | soft_limit_tree_from_page(struct page *page) |
| 336 | { |
| 337 | int nid = page_to_nid(page); |
| 338 | int zid = page_zonenum(page); |
| 339 | |
| 340 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 341 | } |
| 342 | |
| 343 | static void |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 344 | __mem_cgroup_insert_exceeded(struct mem_cgroup *mem, |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 345 | struct mem_cgroup_per_zone *mz, |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 346 | struct mem_cgroup_tree_per_zone *mctz, |
| 347 | unsigned long long new_usage_in_excess) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 348 | { |
| 349 | struct rb_node **p = &mctz->rb_root.rb_node; |
| 350 | struct rb_node *parent = NULL; |
| 351 | struct mem_cgroup_per_zone *mz_node; |
| 352 | |
| 353 | if (mz->on_tree) |
| 354 | return; |
| 355 | |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 356 | mz->usage_in_excess = new_usage_in_excess; |
| 357 | if (!mz->usage_in_excess) |
| 358 | return; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 359 | while (*p) { |
| 360 | parent = *p; |
| 361 | mz_node = rb_entry(parent, struct mem_cgroup_per_zone, |
| 362 | tree_node); |
| 363 | if (mz->usage_in_excess < mz_node->usage_in_excess) |
| 364 | p = &(*p)->rb_left; |
| 365 | /* |
| 366 | * We can't avoid mem cgroups that are over their soft |
| 367 | * limit by the same amount |
| 368 | */ |
| 369 | else if (mz->usage_in_excess >= mz_node->usage_in_excess) |
| 370 | p = &(*p)->rb_right; |
| 371 | } |
| 372 | rb_link_node(&mz->tree_node, parent, p); |
| 373 | rb_insert_color(&mz->tree_node, &mctz->rb_root); |
| 374 | mz->on_tree = true; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static void |
| 378 | __mem_cgroup_remove_exceeded(struct mem_cgroup *mem, |
| 379 | struct mem_cgroup_per_zone *mz, |
| 380 | struct mem_cgroup_tree_per_zone *mctz) |
| 381 | { |
| 382 | if (!mz->on_tree) |
| 383 | return; |
| 384 | rb_erase(&mz->tree_node, &mctz->rb_root); |
| 385 | mz->on_tree = false; |
| 386 | } |
| 387 | |
| 388 | static void |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 389 | mem_cgroup_remove_exceeded(struct mem_cgroup *mem, |
| 390 | struct mem_cgroup_per_zone *mz, |
| 391 | struct mem_cgroup_tree_per_zone *mctz) |
| 392 | { |
| 393 | spin_lock(&mctz->lock); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 394 | __mem_cgroup_remove_exceeded(mem, mz, mctz); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 395 | spin_unlock(&mctz->lock); |
| 396 | } |
| 397 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 398 | |
| 399 | static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page) |
| 400 | { |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 401 | unsigned long long excess; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 402 | struct mem_cgroup_per_zone *mz; |
| 403 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 404 | int nid = page_to_nid(page); |
| 405 | int zid = page_zonenum(page); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 406 | mctz = soft_limit_tree_from_page(page); |
| 407 | |
| 408 | /* |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 409 | * Necessary to update all ancestors when hierarchy is used. |
| 410 | * because their event counter is not touched. |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 411 | */ |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 412 | for (; mem; mem = parent_mem_cgroup(mem)) { |
| 413 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 414 | excess = res_counter_soft_limit_excess(&mem->res); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 415 | /* |
| 416 | * We have to update the tree if mz is on RB-tree or |
| 417 | * mem is over its softlimit. |
| 418 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 419 | if (excess || mz->on_tree) { |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 420 | spin_lock(&mctz->lock); |
| 421 | /* if on-tree, remove it */ |
| 422 | if (mz->on_tree) |
| 423 | __mem_cgroup_remove_exceeded(mem, mz, mctz); |
| 424 | /* |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 425 | * Insert again. mz->usage_in_excess will be updated. |
| 426 | * If excess is 0, no tree ops. |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 427 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 428 | __mem_cgroup_insert_exceeded(mem, mz, mctz, excess); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 429 | spin_unlock(&mctz->lock); |
| 430 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem) |
| 435 | { |
| 436 | int node, zone; |
| 437 | struct mem_cgroup_per_zone *mz; |
| 438 | struct mem_cgroup_tree_per_zone *mctz; |
| 439 | |
| 440 | for_each_node_state(node, N_POSSIBLE) { |
| 441 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 442 | mz = mem_cgroup_zoneinfo(mem, node, zone); |
| 443 | mctz = soft_limit_tree_node_zone(node, zone); |
| 444 | mem_cgroup_remove_exceeded(mem, mz, mctz); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 449 | static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem) |
| 450 | { |
| 451 | return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT; |
| 452 | } |
| 453 | |
| 454 | static struct mem_cgroup_per_zone * |
| 455 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 456 | { |
| 457 | struct rb_node *rightmost = NULL; |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 458 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 459 | |
| 460 | retry: |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 461 | mz = NULL; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 462 | rightmost = rb_last(&mctz->rb_root); |
| 463 | if (!rightmost) |
| 464 | goto done; /* Nothing to reclaim from */ |
| 465 | |
| 466 | mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node); |
| 467 | /* |
| 468 | * Remove the node now but someone else can add it back, |
| 469 | * we will to add it back at the end of reclaim to its correct |
| 470 | * position in the tree. |
| 471 | */ |
| 472 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
| 473 | if (!res_counter_soft_limit_excess(&mz->mem->res) || |
| 474 | !css_tryget(&mz->mem->css)) |
| 475 | goto retry; |
| 476 | done: |
| 477 | return mz; |
| 478 | } |
| 479 | |
| 480 | static struct mem_cgroup_per_zone * |
| 481 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 482 | { |
| 483 | struct mem_cgroup_per_zone *mz; |
| 484 | |
| 485 | spin_lock(&mctz->lock); |
| 486 | mz = __mem_cgroup_largest_soft_limit_node(mctz); |
| 487 | spin_unlock(&mctz->lock); |
| 488 | return mz; |
| 489 | } |
| 490 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 491 | static s64 mem_cgroup_read_stat(struct mem_cgroup *mem, |
| 492 | enum mem_cgroup_stat_index idx) |
| 493 | { |
| 494 | int cpu; |
| 495 | s64 val = 0; |
| 496 | |
| 497 | for_each_possible_cpu(cpu) |
| 498 | val += per_cpu(mem->stat->count[idx], cpu); |
| 499 | return val; |
| 500 | } |
| 501 | |
| 502 | static s64 mem_cgroup_local_usage(struct mem_cgroup *mem) |
| 503 | { |
| 504 | s64 ret; |
| 505 | |
| 506 | ret = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS); |
| 507 | ret += mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE); |
| 508 | return ret; |
| 509 | } |
| 510 | |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 511 | static void mem_cgroup_swap_statistics(struct mem_cgroup *mem, |
| 512 | bool charge) |
| 513 | { |
| 514 | int val = (charge) ? 1 : -1; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 515 | this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 516 | } |
| 517 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 518 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, |
| 519 | struct page_cgroup *pc, |
| 520 | bool charge) |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 521 | { |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 522 | int val = (charge) ? 1 : -1; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 523 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 524 | preempt_disable(); |
| 525 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 526 | if (PageCgroupCache(pc)) |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 527 | __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], val); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 528 | else |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 529 | __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], val); |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 530 | |
| 531 | if (charge) |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 532 | __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGIN_COUNT]); |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 533 | else |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 534 | __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGOUT_COUNT]); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 535 | __this_cpu_inc(mem->stat->count[MEM_CGROUP_EVENTS]); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 536 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 537 | preempt_enable(); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 538 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 539 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 540 | static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem, |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 541 | enum lru_list idx) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 542 | { |
| 543 | int nid, zid; |
| 544 | struct mem_cgroup_per_zone *mz; |
| 545 | u64 total = 0; |
| 546 | |
| 547 | for_each_online_node(nid) |
| 548 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 549 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 550 | total += MEM_CGROUP_ZSTAT(mz, idx); |
| 551 | } |
| 552 | return total; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 553 | } |
| 554 | |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 555 | static bool __memcg_event_check(struct mem_cgroup *mem, int event_mask_shift) |
| 556 | { |
| 557 | s64 val; |
| 558 | |
| 559 | val = this_cpu_read(mem->stat->count[MEM_CGROUP_EVENTS]); |
| 560 | |
| 561 | return !(val & ((1 << event_mask_shift) - 1)); |
| 562 | } |
| 563 | |
| 564 | /* |
| 565 | * Check events in order. |
| 566 | * |
| 567 | */ |
| 568 | static void memcg_check_events(struct mem_cgroup *mem, struct page *page) |
| 569 | { |
| 570 | /* threshold event is triggered in finer grain than soft limit */ |
| 571 | if (unlikely(__memcg_event_check(mem, THRESHOLDS_EVENTS_THRESH))) { |
| 572 | mem_cgroup_threshold(mem); |
| 573 | if (unlikely(__memcg_event_check(mem, SOFTLIMIT_EVENTS_THRESH))) |
| 574 | mem_cgroup_update_tree(mem, page); |
| 575 | } |
| 576 | } |
| 577 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 578 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 579 | { |
| 580 | return container_of(cgroup_subsys_state(cont, |
| 581 | mem_cgroup_subsys_id), struct mem_cgroup, |
| 582 | css); |
| 583 | } |
| 584 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 585 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 586 | { |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 587 | /* |
| 588 | * mm_update_next_owner() may clear mm->owner to NULL |
| 589 | * if it races with swapoff, page migration, etc. |
| 590 | * So this can be called with p == NULL. |
| 591 | */ |
| 592 | if (unlikely(!p)) |
| 593 | return NULL; |
| 594 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 595 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
| 596 | struct mem_cgroup, css); |
| 597 | } |
| 598 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 599 | static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) |
| 600 | { |
| 601 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 602 | |
| 603 | if (!mm) |
| 604 | return NULL; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 605 | /* |
| 606 | * Because we have no locks, mm->owner's may be being moved to other |
| 607 | * cgroup. We use css_tryget() here even if this looks |
| 608 | * pessimistic (rather than adding locks here). |
| 609 | */ |
| 610 | rcu_read_lock(); |
| 611 | do { |
| 612 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 613 | if (unlikely(!mem)) |
| 614 | break; |
| 615 | } while (!css_tryget(&mem->css)); |
| 616 | rcu_read_unlock(); |
| 617 | return mem; |
| 618 | } |
| 619 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 620 | /* |
| 621 | * Call callback function against all cgroup under hierarchy tree. |
| 622 | */ |
| 623 | static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data, |
| 624 | int (*func)(struct mem_cgroup *, void *)) |
| 625 | { |
| 626 | int found, ret, nextid; |
| 627 | struct cgroup_subsys_state *css; |
| 628 | struct mem_cgroup *mem; |
| 629 | |
| 630 | if (!root->use_hierarchy) |
| 631 | return (*func)(root, data); |
| 632 | |
| 633 | nextid = 1; |
| 634 | do { |
| 635 | ret = 0; |
| 636 | mem = NULL; |
| 637 | |
| 638 | rcu_read_lock(); |
| 639 | css = css_get_next(&mem_cgroup_subsys, nextid, &root->css, |
| 640 | &found); |
| 641 | if (css && css_tryget(css)) |
| 642 | mem = container_of(css, struct mem_cgroup, css); |
| 643 | rcu_read_unlock(); |
| 644 | |
| 645 | if (mem) { |
| 646 | ret = (*func)(mem, data); |
| 647 | css_put(&mem->css); |
| 648 | } |
| 649 | nextid = found + 1; |
| 650 | } while (!ret && css); |
| 651 | |
| 652 | return ret; |
| 653 | } |
| 654 | |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 655 | static inline bool mem_cgroup_is_root(struct mem_cgroup *mem) |
| 656 | { |
| 657 | return (mem == root_mem_cgroup); |
| 658 | } |
| 659 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 660 | /* |
| 661 | * Following LRU functions are allowed to be used without PCG_LOCK. |
| 662 | * Operations are called by routine of global LRU independently from memcg. |
| 663 | * What we have to take care of here is validness of pc->mem_cgroup. |
| 664 | * |
| 665 | * Changes to pc->mem_cgroup happens when |
| 666 | * 1. charge |
| 667 | * 2. moving account |
| 668 | * In typical case, "charge" is done before add-to-lru. Exception is SwapCache. |
| 669 | * It is added to LRU before charge. |
| 670 | * If PCG_USED bit is not set, page_cgroup is not added to this private LRU. |
| 671 | * When moving account, the page is not on LRU. It's isolated. |
| 672 | */ |
| 673 | |
| 674 | 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] | 675 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 676 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 677 | struct mem_cgroup_per_zone *mz; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 678 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 679 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 680 | return; |
| 681 | pc = lookup_page_cgroup(page); |
| 682 | /* can happen while we handle swapcache. */ |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 683 | if (!TestClearPageCgroupAcctLRU(pc)) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 684 | return; |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 685 | VM_BUG_ON(!pc->mem_cgroup); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 686 | /* |
| 687 | * We don't check PCG_USED bit. It's cleared when the "page" is finally |
| 688 | * removed from global LRU. |
| 689 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 690 | mz = page_cgroup_zoneinfo(pc); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 691 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 692 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 693 | return; |
| 694 | VM_BUG_ON(list_empty(&pc->lru)); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 695 | list_del_init(&pc->lru); |
| 696 | return; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 697 | } |
| 698 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 699 | void mem_cgroup_del_lru(struct page *page) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 700 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 701 | mem_cgroup_del_lru_list(page, page_lru(page)); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 702 | } |
| 703 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 704 | 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] | 705 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 706 | struct mem_cgroup_per_zone *mz; |
| 707 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 708 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 709 | if (mem_cgroup_disabled()) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 710 | return; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 711 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 712 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 713 | /* |
| 714 | * Used bit is set without atomic ops but after smp_wmb(). |
| 715 | * For making pc->mem_cgroup visible, insert smp_rmb() here. |
| 716 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 717 | smp_rmb(); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 718 | /* unused or root page is not rotated. */ |
| 719 | if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup)) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 720 | return; |
| 721 | mz = page_cgroup_zoneinfo(pc); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 722 | list_move(&pc->lru, &mz->lists[lru]); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 723 | } |
| 724 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 725 | void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru) |
| 726 | { |
| 727 | struct page_cgroup *pc; |
| 728 | struct mem_cgroup_per_zone *mz; |
| 729 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 730 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 731 | return; |
| 732 | pc = lookup_page_cgroup(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 733 | VM_BUG_ON(PageCgroupAcctLRU(pc)); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 734 | /* |
| 735 | * Used bit is set without atomic ops but after smp_wmb(). |
| 736 | * For making pc->mem_cgroup visible, insert smp_rmb() here. |
| 737 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 738 | smp_rmb(); |
| 739 | if (!PageCgroupUsed(pc)) |
| 740 | return; |
| 741 | |
| 742 | mz = page_cgroup_zoneinfo(pc); |
| 743 | MEM_CGROUP_ZSTAT(mz, lru) += 1; |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 744 | SetPageCgroupAcctLRU(pc); |
| 745 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 746 | return; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 747 | list_add(&pc->lru, &mz->lists[lru]); |
| 748 | } |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 749 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 750 | /* |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 751 | * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to |
| 752 | * lru because the page may.be reused after it's fully uncharged (because of |
| 753 | * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge |
| 754 | * it again. This function is only used to charge SwapCache. It's done under |
| 755 | * lock_page and expected that zone->lru_lock is never held. |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 756 | */ |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 757 | static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 758 | { |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 759 | unsigned long flags; |
| 760 | struct zone *zone = page_zone(page); |
| 761 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 762 | |
| 763 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 764 | /* |
| 765 | * Forget old LRU when this page_cgroup is *not* used. This Used bit |
| 766 | * is guarded by lock_page() because the page is SwapCache. |
| 767 | */ |
| 768 | if (!PageCgroupUsed(pc)) |
| 769 | mem_cgroup_del_lru_list(page, page_lru(page)); |
| 770 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 771 | } |
| 772 | |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 773 | static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page) |
| 774 | { |
| 775 | unsigned long flags; |
| 776 | struct zone *zone = page_zone(page); |
| 777 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 778 | |
| 779 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 780 | /* 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] | 781 | if (PageLRU(page) && !PageCgroupAcctLRU(pc)) |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 782 | mem_cgroup_add_lru_list(page, page_lru(page)); |
| 783 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
| 784 | } |
| 785 | |
| 786 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 787 | void mem_cgroup_move_lists(struct page *page, |
| 788 | enum lru_list from, enum lru_list to) |
| 789 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 790 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 791 | return; |
| 792 | mem_cgroup_del_lru_list(page, from); |
| 793 | mem_cgroup_add_lru_list(page, to); |
| 794 | } |
| 795 | |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 796 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
| 797 | { |
| 798 | int ret; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 799 | struct mem_cgroup *curr = NULL; |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 800 | |
| 801 | task_lock(task); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 802 | rcu_read_lock(); |
| 803 | curr = try_get_mem_cgroup_from_mm(task->mm); |
| 804 | rcu_read_unlock(); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 805 | task_unlock(task); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 806 | if (!curr) |
| 807 | return 0; |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 808 | /* |
| 809 | * We should check use_hierarchy of "mem" not "curr". Because checking |
| 810 | * use_hierarchy of "curr" here make this function true if hierarchy is |
| 811 | * enabled in "curr" and "curr" is a child of "mem" in *cgroup* |
| 812 | * hierarchy(even if use_hierarchy is disabled in "mem"). |
| 813 | */ |
| 814 | if (mem->use_hierarchy) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 815 | ret = css_is_ancestor(&curr->css, &mem->css); |
| 816 | else |
| 817 | ret = (curr == mem); |
| 818 | css_put(&curr->css); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 819 | return ret; |
| 820 | } |
| 821 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 822 | /* |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 823 | * prev_priority control...this will be used in memory reclaim path. |
| 824 | */ |
| 825 | int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem) |
| 826 | { |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 827 | int prev_priority; |
| 828 | |
| 829 | spin_lock(&mem->reclaim_param_lock); |
| 830 | prev_priority = mem->prev_priority; |
| 831 | spin_unlock(&mem->reclaim_param_lock); |
| 832 | |
| 833 | return prev_priority; |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority) |
| 837 | { |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 838 | spin_lock(&mem->reclaim_param_lock); |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 839 | if (priority < mem->prev_priority) |
| 840 | mem->prev_priority = priority; |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 841 | spin_unlock(&mem->reclaim_param_lock); |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority) |
| 845 | { |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 846 | spin_lock(&mem->reclaim_param_lock); |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 847 | mem->prev_priority = priority; |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 848 | spin_unlock(&mem->reclaim_param_lock); |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 849 | } |
| 850 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 851 | 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] | 852 | { |
| 853 | unsigned long active; |
| 854 | unsigned long inactive; |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 855 | unsigned long gb; |
| 856 | unsigned long inactive_ratio; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 857 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 858 | inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON); |
| 859 | active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 860 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 861 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
| 862 | if (gb) |
| 863 | inactive_ratio = int_sqrt(10 * gb); |
| 864 | else |
| 865 | inactive_ratio = 1; |
| 866 | |
| 867 | if (present_pages) { |
| 868 | present_pages[0] = inactive; |
| 869 | present_pages[1] = active; |
| 870 | } |
| 871 | |
| 872 | return inactive_ratio; |
| 873 | } |
| 874 | |
| 875 | int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg) |
| 876 | { |
| 877 | unsigned long active; |
| 878 | unsigned long inactive; |
| 879 | unsigned long present_pages[2]; |
| 880 | unsigned long inactive_ratio; |
| 881 | |
| 882 | inactive_ratio = calc_inactive_ratio(memcg, present_pages); |
| 883 | |
| 884 | inactive = present_pages[0]; |
| 885 | active = present_pages[1]; |
| 886 | |
| 887 | if (inactive * inactive_ratio < active) |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 888 | return 1; |
| 889 | |
| 890 | return 0; |
| 891 | } |
| 892 | |
Rik van Riel | 56e49d2 | 2009-06-16 15:32:28 -0700 | [diff] [blame] | 893 | int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg) |
| 894 | { |
| 895 | unsigned long active; |
| 896 | unsigned long inactive; |
| 897 | |
| 898 | inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE); |
| 899 | active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE); |
| 900 | |
| 901 | return (active > inactive); |
| 902 | } |
| 903 | |
KOSAKI Motohiro | a3d8e05 | 2009-01-07 18:08:19 -0800 | [diff] [blame] | 904 | unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg, |
| 905 | struct zone *zone, |
| 906 | enum lru_list lru) |
| 907 | { |
| 908 | int nid = zone->zone_pgdat->node_id; |
| 909 | int zid = zone_idx(zone); |
| 910 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 911 | |
| 912 | return MEM_CGROUP_ZSTAT(mz, lru); |
| 913 | } |
| 914 | |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 915 | struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg, |
| 916 | struct zone *zone) |
| 917 | { |
| 918 | int nid = zone->zone_pgdat->node_id; |
| 919 | int zid = zone_idx(zone); |
| 920 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 921 | |
| 922 | return &mz->reclaim_stat; |
| 923 | } |
| 924 | |
| 925 | struct zone_reclaim_stat * |
| 926 | mem_cgroup_get_reclaim_stat_from_page(struct page *page) |
| 927 | { |
| 928 | struct page_cgroup *pc; |
| 929 | struct mem_cgroup_per_zone *mz; |
| 930 | |
| 931 | if (mem_cgroup_disabled()) |
| 932 | return NULL; |
| 933 | |
| 934 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 935 | /* |
| 936 | * Used bit is set without atomic ops but after smp_wmb(). |
| 937 | * For making pc->mem_cgroup visible, insert smp_rmb() here. |
| 938 | */ |
| 939 | smp_rmb(); |
| 940 | if (!PageCgroupUsed(pc)) |
| 941 | return NULL; |
| 942 | |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 943 | mz = page_cgroup_zoneinfo(pc); |
| 944 | if (!mz) |
| 945 | return NULL; |
| 946 | |
| 947 | return &mz->reclaim_stat; |
| 948 | } |
| 949 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 950 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 951 | struct list_head *dst, |
| 952 | unsigned long *scanned, int order, |
| 953 | int mode, struct zone *z, |
| 954 | struct mem_cgroup *mem_cont, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 955 | int active, int file) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 956 | { |
| 957 | unsigned long nr_taken = 0; |
| 958 | struct page *page; |
| 959 | unsigned long scan; |
| 960 | LIST_HEAD(pc_list); |
| 961 | struct list_head *src; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 962 | struct page_cgroup *pc, *tmp; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 963 | int nid = z->zone_pgdat->node_id; |
| 964 | int zid = zone_idx(z); |
| 965 | struct mem_cgroup_per_zone *mz; |
Johannes Weiner | b7c46d1 | 2009-09-21 17:02:56 -0700 | [diff] [blame] | 966 | int lru = LRU_FILE * file + active; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 967 | int ret; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 968 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 969 | BUG_ON(!mem_cont); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 970 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 971 | src = &mz->lists[lru]; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 972 | |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 973 | scan = 0; |
| 974 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 975 | if (scan >= nr_to_scan) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 976 | break; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 977 | |
| 978 | page = pc->page; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 979 | if (unlikely(!PageCgroupUsed(pc))) |
| 980 | continue; |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 981 | if (unlikely(!PageLRU(page))) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 982 | continue; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 983 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 984 | scan++; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 985 | ret = __isolate_lru_page(page, mode, file); |
| 986 | switch (ret) { |
| 987 | case 0: |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 988 | list_move(&page->lru, dst); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 989 | mem_cgroup_del_lru(page); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 990 | nr_taken++; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 991 | break; |
| 992 | case -EBUSY: |
| 993 | /* we don't affect global LRU but rotate in our LRU */ |
| 994 | mem_cgroup_rotate_lru_list(page, page_lru(page)); |
| 995 | break; |
| 996 | default: |
| 997 | break; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1001 | *scanned = scan; |
| 1002 | return nr_taken; |
| 1003 | } |
| 1004 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1005 | #define mem_cgroup_from_res_counter(counter, member) \ |
| 1006 | container_of(counter, struct mem_cgroup, member) |
| 1007 | |
Daisuke Nishimura | b85a96c | 2009-01-07 18:08:12 -0800 | [diff] [blame] | 1008 | static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem) |
| 1009 | { |
| 1010 | if (do_swap_account) { |
| 1011 | if (res_counter_check_under_limit(&mem->res) && |
| 1012 | res_counter_check_under_limit(&mem->memsw)) |
| 1013 | return true; |
| 1014 | } else |
| 1015 | if (res_counter_check_under_limit(&mem->res)) |
| 1016 | return true; |
| 1017 | return false; |
| 1018 | } |
| 1019 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1020 | static unsigned int get_swappiness(struct mem_cgroup *memcg) |
| 1021 | { |
| 1022 | struct cgroup *cgrp = memcg->css.cgroup; |
| 1023 | unsigned int swappiness; |
| 1024 | |
| 1025 | /* root ? */ |
| 1026 | if (cgrp->parent == NULL) |
| 1027 | return vm_swappiness; |
| 1028 | |
| 1029 | spin_lock(&memcg->reclaim_param_lock); |
| 1030 | swappiness = memcg->swappiness; |
| 1031 | spin_unlock(&memcg->reclaim_param_lock); |
| 1032 | |
| 1033 | return swappiness; |
| 1034 | } |
| 1035 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1036 | static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data) |
| 1037 | { |
| 1038 | int *val = data; |
| 1039 | (*val)++; |
| 1040 | return 0; |
| 1041 | } |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1042 | |
| 1043 | /** |
Kirill A. Shutemov | 6a6135b | 2010-03-10 15:22:25 -0800 | [diff] [blame] | 1044 | * 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] | 1045 | * @memcg: The memory cgroup that went over limit |
| 1046 | * @p: Task that is going to be killed |
| 1047 | * |
| 1048 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is |
| 1049 | * enabled |
| 1050 | */ |
| 1051 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) |
| 1052 | { |
| 1053 | struct cgroup *task_cgrp; |
| 1054 | struct cgroup *mem_cgrp; |
| 1055 | /* |
| 1056 | * Need a buffer in BSS, can't rely on allocations. The code relies |
| 1057 | * on the assumption that OOM is serialized for memory controller. |
| 1058 | * If this assumption is broken, revisit this code. |
| 1059 | */ |
| 1060 | static char memcg_name[PATH_MAX]; |
| 1061 | int ret; |
| 1062 | |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1063 | if (!memcg || !p) |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1064 | return; |
| 1065 | |
| 1066 | |
| 1067 | rcu_read_lock(); |
| 1068 | |
| 1069 | mem_cgrp = memcg->css.cgroup; |
| 1070 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); |
| 1071 | |
| 1072 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); |
| 1073 | if (ret < 0) { |
| 1074 | /* |
| 1075 | * Unfortunately, we are unable to convert to a useful name |
| 1076 | * But we'll still print out the usage information |
| 1077 | */ |
| 1078 | rcu_read_unlock(); |
| 1079 | goto done; |
| 1080 | } |
| 1081 | rcu_read_unlock(); |
| 1082 | |
| 1083 | printk(KERN_INFO "Task in %s killed", memcg_name); |
| 1084 | |
| 1085 | rcu_read_lock(); |
| 1086 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); |
| 1087 | if (ret < 0) { |
| 1088 | rcu_read_unlock(); |
| 1089 | goto done; |
| 1090 | } |
| 1091 | rcu_read_unlock(); |
| 1092 | |
| 1093 | /* |
| 1094 | * Continues from above, so we don't need an KERN_ level |
| 1095 | */ |
| 1096 | printk(KERN_CONT " as a result of limit of %s\n", memcg_name); |
| 1097 | done: |
| 1098 | |
| 1099 | printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n", |
| 1100 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, |
| 1101 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, |
| 1102 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); |
| 1103 | printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, " |
| 1104 | "failcnt %llu\n", |
| 1105 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, |
| 1106 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, |
| 1107 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); |
| 1108 | } |
| 1109 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1110 | /* |
| 1111 | * This function returns the number of memcg under hierarchy tree. Returns |
| 1112 | * 1(self count) if no children. |
| 1113 | */ |
| 1114 | static int mem_cgroup_count_children(struct mem_cgroup *mem) |
| 1115 | { |
| 1116 | int num = 0; |
| 1117 | mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb); |
| 1118 | return num; |
| 1119 | } |
| 1120 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1121 | /* |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1122 | * Visit the first child (need not be the first child as per the ordering |
| 1123 | * of the cgroup list, since we track last_scanned_child) of @mem and use |
| 1124 | * that to reclaim free pages from. |
| 1125 | */ |
| 1126 | static struct mem_cgroup * |
| 1127 | mem_cgroup_select_victim(struct mem_cgroup *root_mem) |
| 1128 | { |
| 1129 | struct mem_cgroup *ret = NULL; |
| 1130 | struct cgroup_subsys_state *css; |
| 1131 | int nextid, found; |
| 1132 | |
| 1133 | if (!root_mem->use_hierarchy) { |
| 1134 | css_get(&root_mem->css); |
| 1135 | ret = root_mem; |
| 1136 | } |
| 1137 | |
| 1138 | while (!ret) { |
| 1139 | rcu_read_lock(); |
| 1140 | nextid = root_mem->last_scanned_child + 1; |
| 1141 | css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css, |
| 1142 | &found); |
| 1143 | if (css && css_tryget(css)) |
| 1144 | ret = container_of(css, struct mem_cgroup, css); |
| 1145 | |
| 1146 | rcu_read_unlock(); |
| 1147 | /* Updates scanning parameter */ |
| 1148 | spin_lock(&root_mem->reclaim_param_lock); |
| 1149 | if (!css) { |
| 1150 | /* this means start scan from ID:1 */ |
| 1151 | root_mem->last_scanned_child = 0; |
| 1152 | } else |
| 1153 | root_mem->last_scanned_child = found; |
| 1154 | spin_unlock(&root_mem->reclaim_param_lock); |
| 1155 | } |
| 1156 | |
| 1157 | return ret; |
| 1158 | } |
| 1159 | |
| 1160 | /* |
| 1161 | * Scan the hierarchy if needed to reclaim memory. We remember the last child |
| 1162 | * we reclaimed from, so that we don't end up penalizing one child extensively |
| 1163 | * based on its position in the children list. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1164 | * |
| 1165 | * root_mem is the original ancestor that we've been reclaim from. |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1166 | * |
| 1167 | * We give up and return to the caller when we visit root_mem twice. |
| 1168 | * (other groups can be removed while we're walking....) |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1169 | * |
| 1170 | * If shrink==true, for avoiding to free too much, this returns immedieately. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1171 | */ |
| 1172 | static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem, |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1173 | struct zone *zone, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1174 | gfp_t gfp_mask, |
| 1175 | unsigned long reclaim_options) |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1176 | { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1177 | struct mem_cgroup *victim; |
| 1178 | int ret, total = 0; |
| 1179 | int loop = 0; |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1180 | bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP; |
| 1181 | bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1182 | bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT; |
| 1183 | unsigned long excess = mem_cgroup_get_excess(root_mem); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1184 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1185 | /* If memsw_is_minimum==1, swap-out is of-no-use. */ |
| 1186 | if (root_mem->memsw_is_minimum) |
| 1187 | noswap = true; |
| 1188 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1189 | while (1) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1190 | victim = mem_cgroup_select_victim(root_mem); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1191 | if (victim == root_mem) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1192 | loop++; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1193 | if (loop >= 1) |
| 1194 | drain_all_stock_async(); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1195 | if (loop >= 2) { |
| 1196 | /* |
| 1197 | * If we have not been able to reclaim |
| 1198 | * anything, it might because there are |
| 1199 | * no reclaimable pages under this hierarchy |
| 1200 | */ |
| 1201 | if (!check_soft || !total) { |
| 1202 | css_put(&victim->css); |
| 1203 | break; |
| 1204 | } |
| 1205 | /* |
| 1206 | * We want to do more targetted reclaim. |
| 1207 | * excess >> 2 is not to excessive so as to |
| 1208 | * reclaim too much, nor too less that we keep |
| 1209 | * coming back to reclaim from this cgroup |
| 1210 | */ |
| 1211 | if (total >= (excess >> 2) || |
| 1212 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) { |
| 1213 | css_put(&victim->css); |
| 1214 | break; |
| 1215 | } |
| 1216 | } |
| 1217 | } |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 1218 | if (!mem_cgroup_local_usage(victim)) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1219 | /* this cgroup's local usage == 0 */ |
| 1220 | css_put(&victim->css); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1221 | continue; |
| 1222 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1223 | /* we use swappiness of local cgroup */ |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1224 | if (check_soft) |
| 1225 | ret = mem_cgroup_shrink_node_zone(victim, gfp_mask, |
| 1226 | noswap, get_swappiness(victim), zone, |
| 1227 | zone->zone_pgdat->node_id); |
| 1228 | else |
| 1229 | ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, |
| 1230 | noswap, get_swappiness(victim)); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1231 | css_put(&victim->css); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1232 | /* |
| 1233 | * At shrinking usage, we can't check we should stop here or |
| 1234 | * reclaim more. It's depends on callers. last_scanned_child |
| 1235 | * will work enough for keeping fairness under tree. |
| 1236 | */ |
| 1237 | if (shrink) |
| 1238 | return ret; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1239 | total += ret; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1240 | if (check_soft) { |
| 1241 | if (res_counter_check_under_soft_limit(&root_mem->res)) |
| 1242 | return total; |
| 1243 | } else if (mem_cgroup_check_under_limit(root_mem)) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1244 | return 1 + total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1245 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1246 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1247 | } |
| 1248 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1249 | static int mem_cgroup_oom_lock_cb(struct mem_cgroup *mem, void *data) |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1250 | { |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1251 | int *val = (int *)data; |
| 1252 | int x; |
| 1253 | /* |
| 1254 | * Logically, we can stop scanning immediately when we find |
| 1255 | * a memcg is already locked. But condidering unlock ops and |
| 1256 | * creation/removal of memcg, scan-all is simple operation. |
| 1257 | */ |
| 1258 | x = atomic_inc_return(&mem->oom_lock); |
| 1259 | *val = max(x, *val); |
| 1260 | return 0; |
| 1261 | } |
| 1262 | /* |
| 1263 | * Check OOM-Killer is already running under our hierarchy. |
| 1264 | * If someone is running, return false. |
| 1265 | */ |
| 1266 | static bool mem_cgroup_oom_lock(struct mem_cgroup *mem) |
| 1267 | { |
| 1268 | int lock_count = 0; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1269 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1270 | mem_cgroup_walk_tree(mem, &lock_count, mem_cgroup_oom_lock_cb); |
| 1271 | |
| 1272 | if (lock_count == 1) |
| 1273 | return true; |
| 1274 | return false; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1275 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1276 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1277 | static int mem_cgroup_oom_unlock_cb(struct mem_cgroup *mem, void *data) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1278 | { |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1279 | /* |
| 1280 | * When a new child is created while the hierarchy is under oom, |
| 1281 | * mem_cgroup_oom_lock() may not be called. We have to use |
| 1282 | * atomic_add_unless() here. |
| 1283 | */ |
| 1284 | atomic_add_unless(&mem->oom_lock, -1, 0); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1285 | return 0; |
| 1286 | } |
| 1287 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1288 | static void mem_cgroup_oom_unlock(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1289 | { |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1290 | mem_cgroup_walk_tree(mem, NULL, mem_cgroup_oom_unlock_cb); |
| 1291 | } |
| 1292 | |
| 1293 | static DEFINE_MUTEX(memcg_oom_mutex); |
| 1294 | static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq); |
| 1295 | |
| 1296 | /* |
| 1297 | * try to call OOM killer. returns false if we should exit memory-reclaim loop. |
| 1298 | */ |
| 1299 | bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask) |
| 1300 | { |
| 1301 | DEFINE_WAIT(wait); |
| 1302 | bool locked; |
| 1303 | |
| 1304 | /* At first, try to OOM lock hierarchy under mem.*/ |
| 1305 | mutex_lock(&memcg_oom_mutex); |
| 1306 | locked = mem_cgroup_oom_lock(mem); |
| 1307 | /* |
| 1308 | * Even if signal_pending(), we can't quit charge() loop without |
| 1309 | * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL |
| 1310 | * under OOM is always welcomed, use TASK_KILLABLE here. |
| 1311 | */ |
| 1312 | if (!locked) |
| 1313 | prepare_to_wait(&memcg_oom_waitq, &wait, TASK_KILLABLE); |
| 1314 | mutex_unlock(&memcg_oom_mutex); |
| 1315 | |
| 1316 | if (locked) |
| 1317 | mem_cgroup_out_of_memory(mem, mask); |
| 1318 | else { |
| 1319 | schedule(); |
| 1320 | finish_wait(&memcg_oom_waitq, &wait); |
| 1321 | } |
| 1322 | mutex_lock(&memcg_oom_mutex); |
| 1323 | mem_cgroup_oom_unlock(mem); |
| 1324 | /* |
| 1325 | * Here, we use global waitq .....more fine grained waitq ? |
| 1326 | * Assume following hierarchy. |
| 1327 | * A/ |
| 1328 | * 01 |
| 1329 | * 02 |
| 1330 | * assume OOM happens both in A and 01 at the same time. Tthey are |
| 1331 | * mutually exclusive by lock. (kill in 01 helps A.) |
| 1332 | * When we use per memcg waitq, we have to wake up waiters on A and 02 |
| 1333 | * in addtion to waiters on 01. We use global waitq for avoiding mess. |
| 1334 | * It will not be a big problem. |
| 1335 | * (And a task may be moved to other groups while it's waiting for OOM.) |
| 1336 | */ |
| 1337 | wake_up_all(&memcg_oom_waitq); |
| 1338 | mutex_unlock(&memcg_oom_mutex); |
| 1339 | |
| 1340 | if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current)) |
| 1341 | return false; |
| 1342 | /* Give chance to dying process */ |
| 1343 | schedule_timeout(1); |
| 1344 | return true; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1347 | /* |
| 1348 | * Currently used to update mapped file statistics, but the routine can be |
| 1349 | * generalized to update other statistics as well. |
| 1350 | */ |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 1351 | void mem_cgroup_update_file_mapped(struct page *page, int val) |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1352 | { |
| 1353 | struct mem_cgroup *mem; |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1354 | struct page_cgroup *pc; |
| 1355 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1356 | pc = lookup_page_cgroup(page); |
| 1357 | if (unlikely(!pc)) |
| 1358 | return; |
| 1359 | |
| 1360 | lock_page_cgroup(pc); |
| 1361 | mem = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 1362 | if (!mem || !PageCgroupUsed(pc)) |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1363 | goto done; |
| 1364 | |
| 1365 | /* |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 1366 | * Preemption is already disabled. We can use __this_cpu_xxx |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1367 | */ |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 1368 | if (val > 0) { |
| 1369 | __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 1370 | SetPageCgroupFileMapped(pc); |
| 1371 | } else { |
| 1372 | __this_cpu_dec(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 1373 | ClearPageCgroupFileMapped(pc); |
| 1374 | } |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1375 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1376 | done: |
| 1377 | unlock_page_cgroup(pc); |
| 1378 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1379 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1380 | /* |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1381 | * size of first charge trial. "32" comes from vmscan.c's magic value. |
| 1382 | * TODO: maybe necessary to use big numbers in big irons. |
| 1383 | */ |
| 1384 | #define CHARGE_SIZE (32 * PAGE_SIZE) |
| 1385 | struct memcg_stock_pcp { |
| 1386 | struct mem_cgroup *cached; /* this never be root cgroup */ |
| 1387 | int charge; |
| 1388 | struct work_struct work; |
| 1389 | }; |
| 1390 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock); |
| 1391 | static atomic_t memcg_drain_count; |
| 1392 | |
| 1393 | /* |
| 1394 | * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed |
| 1395 | * from local stock and true is returned. If the stock is 0 or charges from a |
| 1396 | * cgroup which is not current target, returns false. This stock will be |
| 1397 | * refilled. |
| 1398 | */ |
| 1399 | static bool consume_stock(struct mem_cgroup *mem) |
| 1400 | { |
| 1401 | struct memcg_stock_pcp *stock; |
| 1402 | bool ret = true; |
| 1403 | |
| 1404 | stock = &get_cpu_var(memcg_stock); |
| 1405 | if (mem == stock->cached && stock->charge) |
| 1406 | stock->charge -= PAGE_SIZE; |
| 1407 | else /* need to call res_counter_charge */ |
| 1408 | ret = false; |
| 1409 | put_cpu_var(memcg_stock); |
| 1410 | return ret; |
| 1411 | } |
| 1412 | |
| 1413 | /* |
| 1414 | * Returns stocks cached in percpu to res_counter and reset cached information. |
| 1415 | */ |
| 1416 | static void drain_stock(struct memcg_stock_pcp *stock) |
| 1417 | { |
| 1418 | struct mem_cgroup *old = stock->cached; |
| 1419 | |
| 1420 | if (stock->charge) { |
| 1421 | res_counter_uncharge(&old->res, stock->charge); |
| 1422 | if (do_swap_account) |
| 1423 | res_counter_uncharge(&old->memsw, stock->charge); |
| 1424 | } |
| 1425 | stock->cached = NULL; |
| 1426 | stock->charge = 0; |
| 1427 | } |
| 1428 | |
| 1429 | /* |
| 1430 | * This must be called under preempt disabled or must be called by |
| 1431 | * a thread which is pinned to local cpu. |
| 1432 | */ |
| 1433 | static void drain_local_stock(struct work_struct *dummy) |
| 1434 | { |
| 1435 | struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock); |
| 1436 | drain_stock(stock); |
| 1437 | } |
| 1438 | |
| 1439 | /* |
| 1440 | * Cache charges(val) which is from res_counter, to local per_cpu area. |
| 1441 | * This will be consumed by consumt_stock() function, later. |
| 1442 | */ |
| 1443 | static void refill_stock(struct mem_cgroup *mem, int val) |
| 1444 | { |
| 1445 | struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock); |
| 1446 | |
| 1447 | if (stock->cached != mem) { /* reset if necessary */ |
| 1448 | drain_stock(stock); |
| 1449 | stock->cached = mem; |
| 1450 | } |
| 1451 | stock->charge += val; |
| 1452 | put_cpu_var(memcg_stock); |
| 1453 | } |
| 1454 | |
| 1455 | /* |
| 1456 | * Tries to drain stocked charges in other cpus. This function is asynchronous |
| 1457 | * and just put a work per cpu for draining localy on each cpu. Caller can |
| 1458 | * expects some charges will be back to res_counter later but cannot wait for |
| 1459 | * it. |
| 1460 | */ |
| 1461 | static void drain_all_stock_async(void) |
| 1462 | { |
| 1463 | int cpu; |
| 1464 | /* This function is for scheduling "drain" in asynchronous way. |
| 1465 | * The result of "drain" is not directly handled by callers. Then, |
| 1466 | * if someone is calling drain, we don't have to call drain more. |
| 1467 | * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if |
| 1468 | * there is a race. We just do loose check here. |
| 1469 | */ |
| 1470 | if (atomic_read(&memcg_drain_count)) |
| 1471 | return; |
| 1472 | /* Notify other cpus that system-wide "drain" is running */ |
| 1473 | atomic_inc(&memcg_drain_count); |
| 1474 | get_online_cpus(); |
| 1475 | for_each_online_cpu(cpu) { |
| 1476 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
| 1477 | schedule_work_on(cpu, &stock->work); |
| 1478 | } |
| 1479 | put_online_cpus(); |
| 1480 | atomic_dec(&memcg_drain_count); |
| 1481 | /* We don't wait for flush_work */ |
| 1482 | } |
| 1483 | |
| 1484 | /* This is a synchronous drain interface. */ |
| 1485 | static void drain_all_stock_sync(void) |
| 1486 | { |
| 1487 | /* called when force_empty is called */ |
| 1488 | atomic_inc(&memcg_drain_count); |
| 1489 | schedule_on_each_cpu(drain_local_stock); |
| 1490 | atomic_dec(&memcg_drain_count); |
| 1491 | } |
| 1492 | |
| 1493 | static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb, |
| 1494 | unsigned long action, |
| 1495 | void *hcpu) |
| 1496 | { |
| 1497 | int cpu = (unsigned long)hcpu; |
| 1498 | struct memcg_stock_pcp *stock; |
| 1499 | |
| 1500 | if (action != CPU_DEAD) |
| 1501 | return NOTIFY_OK; |
| 1502 | stock = &per_cpu(memcg_stock, cpu); |
| 1503 | drain_stock(stock); |
| 1504 | return NOTIFY_OK; |
| 1505 | } |
| 1506 | |
| 1507 | /* |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1508 | * Unlike exported interface, "oom" parameter is added. if oom==true, |
| 1509 | * oom-killer can be invoked. |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1510 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1511 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 1512 | gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1513 | { |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 1514 | struct mem_cgroup *mem, *mem_over_limit; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1515 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 1516 | struct res_counter *fail_res; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1517 | int csize = CHARGE_SIZE; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1518 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1519 | /* |
| 1520 | * Unlike gloval-vm's OOM-kill, we're not in memory shortage |
| 1521 | * in system level. So, allow to go ahead dying process in addition to |
| 1522 | * MEMDIE process. |
| 1523 | */ |
| 1524 | if (unlikely(test_thread_flag(TIF_MEMDIE) |
| 1525 | || fatal_signal_pending(current))) |
| 1526 | goto bypass; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1527 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1528 | /* |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 1529 | * We always charge the cgroup the mm_struct belongs to. |
| 1530 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1531 | * thread group leader migrates. It's possible that mm is not |
| 1532 | * set, if so charge the init_mm (happens for pagecache usage). |
| 1533 | */ |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1534 | mem = *memcg; |
| 1535 | if (likely(!mem)) { |
| 1536 | mem = try_get_mem_cgroup_from_mm(mm); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1537 | *memcg = mem; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1538 | } else { |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1539 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1540 | } |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1541 | if (unlikely(!mem)) |
| 1542 | return 0; |
| 1543 | |
Nikanth Karthikesan | 46f7e60 | 2009-05-28 14:34:41 -0700 | [diff] [blame] | 1544 | VM_BUG_ON(css_is_removed(&mem->css)); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1545 | if (mem_cgroup_is_root(mem)) |
| 1546 | goto done; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1547 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1548 | while (1) { |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 1549 | int ret = 0; |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1550 | unsigned long flags = 0; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1551 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1552 | if (consume_stock(mem)) |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 1553 | goto done; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1554 | |
| 1555 | ret = res_counter_charge(&mem->res, csize, &fail_res); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1556 | if (likely(!ret)) { |
| 1557 | if (!do_swap_account) |
| 1558 | break; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1559 | ret = res_counter_charge(&mem->memsw, csize, &fail_res); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1560 | if (likely(!ret)) |
| 1561 | break; |
| 1562 | /* mem+swap counter fails */ |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1563 | res_counter_uncharge(&mem->res, csize); |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1564 | flags |= MEM_CGROUP_RECLAIM_NOSWAP; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1565 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, |
| 1566 | memsw); |
| 1567 | } else |
| 1568 | /* mem counter fails */ |
| 1569 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, |
| 1570 | res); |
| 1571 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1572 | /* reduce request size and retry */ |
| 1573 | if (csize > PAGE_SIZE) { |
| 1574 | csize = PAGE_SIZE; |
| 1575 | continue; |
| 1576 | } |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 1577 | if (!(gfp_mask & __GFP_WAIT)) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1578 | goto nomem; |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 1579 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1580 | ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL, |
| 1581 | gfp_mask, flags); |
Daisuke Nishimura | 4d1c627 | 2009-01-15 13:51:14 -0800 | [diff] [blame] | 1582 | if (ret) |
| 1583 | continue; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1584 | |
| 1585 | /* |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 1586 | * try_to_free_mem_cgroup_pages() might not give us a full |
| 1587 | * picture of reclaim. Some pages are reclaimed and might be |
| 1588 | * moved to swap cache or just unmapped from the cgroup. |
| 1589 | * Check the limit again to see if the reclaim reduced the |
| 1590 | * current usage of the cgroup before giving up |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1591 | * |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 1592 | */ |
Daisuke Nishimura | b85a96c | 2009-01-07 18:08:12 -0800 | [diff] [blame] | 1593 | if (mem_cgroup_check_under_limit(mem_over_limit)) |
| 1594 | continue; |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 1595 | |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 1596 | /* try to avoid oom while someone is moving charge */ |
| 1597 | if (mc.moving_task && current != mc.moving_task) { |
| 1598 | struct mem_cgroup *from, *to; |
| 1599 | bool do_continue = false; |
| 1600 | /* |
| 1601 | * There is a small race that "from" or "to" can be |
| 1602 | * freed by rmdir, so we use css_tryget(). |
| 1603 | */ |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 1604 | from = mc.from; |
| 1605 | to = mc.to; |
| 1606 | if (from && css_tryget(&from->css)) { |
| 1607 | if (mem_over_limit->use_hierarchy) |
| 1608 | do_continue = css_is_ancestor( |
| 1609 | &from->css, |
| 1610 | &mem_over_limit->css); |
| 1611 | else |
| 1612 | do_continue = (from == mem_over_limit); |
| 1613 | css_put(&from->css); |
| 1614 | } |
| 1615 | if (!do_continue && to && css_tryget(&to->css)) { |
| 1616 | if (mem_over_limit->use_hierarchy) |
| 1617 | do_continue = css_is_ancestor( |
| 1618 | &to->css, |
| 1619 | &mem_over_limit->css); |
| 1620 | else |
| 1621 | do_continue = (to == mem_over_limit); |
| 1622 | css_put(&to->css); |
| 1623 | } |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 1624 | if (do_continue) { |
| 1625 | DEFINE_WAIT(wait); |
| 1626 | prepare_to_wait(&mc.waitq, &wait, |
| 1627 | TASK_INTERRUPTIBLE); |
| 1628 | /* moving charge context might have finished. */ |
| 1629 | if (mc.moving_task) |
| 1630 | schedule(); |
| 1631 | finish_wait(&mc.waitq, &wait); |
| 1632 | continue; |
| 1633 | } |
| 1634 | } |
| 1635 | |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 1636 | if (!nr_retries--) { |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1637 | if (!oom) |
| 1638 | goto nomem; |
| 1639 | if (mem_cgroup_handle_oom(mem_over_limit, gfp_mask)) { |
| 1640 | nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
| 1641 | continue; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1642 | } |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1643 | /* When we reach here, current task is dying .*/ |
| 1644 | css_put(&mem->css); |
| 1645 | goto bypass; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1646 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1647 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1648 | if (csize > PAGE_SIZE) |
| 1649 | refill_stock(mem, csize - PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 1650 | done: |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1651 | return 0; |
| 1652 | nomem: |
| 1653 | css_put(&mem->css); |
| 1654 | return -ENOMEM; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1655 | bypass: |
| 1656 | *memcg = NULL; |
| 1657 | return 0; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1658 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1659 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1660 | /* |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 1661 | * Somemtimes we have to undo a charge we got by try_charge(). |
| 1662 | * This function is for that and do uncharge, put css's refcnt. |
| 1663 | * gotten by try_charge(). |
| 1664 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 1665 | static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem, |
| 1666 | unsigned long count) |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 1667 | { |
| 1668 | if (!mem_cgroup_is_root(mem)) { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 1669 | res_counter_uncharge(&mem->res, PAGE_SIZE * count); |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 1670 | if (do_swap_account) |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 1671 | res_counter_uncharge(&mem->memsw, PAGE_SIZE * count); |
| 1672 | VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags)); |
| 1673 | WARN_ON_ONCE(count > INT_MAX); |
| 1674 | __css_put(&mem->css, (int)count); |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 1675 | } |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 1676 | /* we don't need css_put for root */ |
| 1677 | } |
| 1678 | |
| 1679 | static void mem_cgroup_cancel_charge(struct mem_cgroup *mem) |
| 1680 | { |
| 1681 | __mem_cgroup_cancel_charge(mem, 1); |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | /* |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1685 | * A helper function to get mem_cgroup from ID. must be called under |
| 1686 | * rcu_read_lock(). The caller must check css_is_removed() or some if |
| 1687 | * it's concern. (dropping refcnt from swap can be called against removed |
| 1688 | * memcg.) |
| 1689 | */ |
| 1690 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) |
| 1691 | { |
| 1692 | struct cgroup_subsys_state *css; |
| 1693 | |
| 1694 | /* ID 0 is unused ID */ |
| 1695 | if (!id) |
| 1696 | return NULL; |
| 1697 | css = css_lookup(&mem_cgroup_subsys, id); |
| 1698 | if (!css) |
| 1699 | return NULL; |
| 1700 | return container_of(css, struct mem_cgroup, css); |
| 1701 | } |
| 1702 | |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 1703 | struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1704 | { |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 1705 | struct mem_cgroup *mem = NULL; |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1706 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1707 | unsigned short id; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1708 | swp_entry_t ent; |
| 1709 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1710 | VM_BUG_ON(!PageLocked(page)); |
| 1711 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1712 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 1713 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1714 | if (PageCgroupUsed(pc)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1715 | mem = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1716 | if (mem && !css_tryget(&mem->css)) |
| 1717 | mem = NULL; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 1718 | } else if (PageSwapCache(page)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1719 | ent.val = page_private(page); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1720 | id = lookup_swap_cgroup(ent); |
| 1721 | rcu_read_lock(); |
| 1722 | mem = mem_cgroup_lookup(id); |
| 1723 | if (mem && !css_tryget(&mem->css)) |
| 1724 | mem = NULL; |
| 1725 | rcu_read_unlock(); |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1726 | } |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 1727 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1728 | return mem; |
| 1729 | } |
| 1730 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1731 | /* |
Daisuke Nishimura | a5e924f | 2009-01-07 18:08:28 -0800 | [diff] [blame] | 1732 | * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1733 | * USED state. If already USED, uncharge and return. |
| 1734 | */ |
| 1735 | |
| 1736 | static void __mem_cgroup_commit_charge(struct mem_cgroup *mem, |
| 1737 | struct page_cgroup *pc, |
| 1738 | enum charge_type ctype) |
| 1739 | { |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1740 | /* try_charge() can return NULL to *memcg, taking care of it. */ |
| 1741 | if (!mem) |
| 1742 | return; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1743 | |
| 1744 | lock_page_cgroup(pc); |
| 1745 | if (unlikely(PageCgroupUsed(pc))) { |
| 1746 | unlock_page_cgroup(pc); |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 1747 | mem_cgroup_cancel_charge(mem); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1748 | return; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1749 | } |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 1750 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1751 | pc->mem_cgroup = mem; |
KAMEZAWA Hiroyuki | 261fb61 | 2009-09-23 15:56:33 -0700 | [diff] [blame] | 1752 | /* |
| 1753 | * We access a page_cgroup asynchronously without lock_page_cgroup(). |
| 1754 | * Especially when a page_cgroup is taken from a page, pc->mem_cgroup |
| 1755 | * is accessed after testing USED bit. To make pc->mem_cgroup visible |
| 1756 | * before USED bit, we need memory barrier here. |
| 1757 | * See mem_cgroup_add_lru_list(), etc. |
| 1758 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1759 | smp_wmb(); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 1760 | switch (ctype) { |
| 1761 | case MEM_CGROUP_CHARGE_TYPE_CACHE: |
| 1762 | case MEM_CGROUP_CHARGE_TYPE_SHMEM: |
| 1763 | SetPageCgroupCache(pc); |
| 1764 | SetPageCgroupUsed(pc); |
| 1765 | break; |
| 1766 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
| 1767 | ClearPageCgroupCache(pc); |
| 1768 | SetPageCgroupUsed(pc); |
| 1769 | break; |
| 1770 | default: |
| 1771 | break; |
| 1772 | } |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 1773 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1774 | mem_cgroup_charge_statistics(mem, pc, true); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1775 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1776 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 1777 | /* |
| 1778 | * "charge_statistics" updated event counter. Then, check it. |
| 1779 | * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree. |
| 1780 | * if they exceeds softlimit. |
| 1781 | */ |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 1782 | memcg_check_events(mem, pc->page); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1783 | } |
| 1784 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1785 | /** |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 1786 | * __mem_cgroup_move_account - move account of the page |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1787 | * @pc: page_cgroup of the page. |
| 1788 | * @from: mem_cgroup which the page is moved from. |
| 1789 | * @to: mem_cgroup which the page is moved to. @from != @to. |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 1790 | * @uncharge: whether we should call uncharge and css_put against @from. |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1791 | * |
| 1792 | * The caller must confirm following. |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1793 | * - page is not on LRU (isolate_page() is useful.) |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 1794 | * - the pc is locked, used, and ->mem_cgroup points to @from. |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1795 | * |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 1796 | * This function doesn't do "charge" nor css_get to new cgroup. It should be |
| 1797 | * done by a caller(__mem_cgroup_try_charge would be usefull). If @uncharge is |
| 1798 | * true, this function does "uncharge" from old cgroup, but it doesn't if |
| 1799 | * @uncharge is false, so a caller should do "uncharge". |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1800 | */ |
| 1801 | |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 1802 | static void __mem_cgroup_move_account(struct page_cgroup *pc, |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 1803 | struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1804 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1805 | VM_BUG_ON(from == to); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1806 | VM_BUG_ON(PageLRU(pc->page)); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 1807 | VM_BUG_ON(!PageCgroupLocked(pc)); |
| 1808 | VM_BUG_ON(!PageCgroupUsed(pc)); |
| 1809 | VM_BUG_ON(pc->mem_cgroup != from); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1810 | |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 1811 | if (PageCgroupFileMapped(pc)) { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 1812 | /* Update mapped_file data for mem_cgroup */ |
| 1813 | preempt_disable(); |
| 1814 | __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 1815 | __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 1816 | preempt_enable(); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1817 | } |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 1818 | mem_cgroup_charge_statistics(from, pc, false); |
| 1819 | if (uncharge) |
| 1820 | /* This is not "cancel", but cancel_charge does all we need. */ |
| 1821 | mem_cgroup_cancel_charge(from); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1822 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 1823 | /* caller should have done css_get */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1824 | pc->mem_cgroup = to; |
| 1825 | mem_cgroup_charge_statistics(to, pc, true); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 1826 | /* |
| 1827 | * We charges against "to" which may not have any tasks. Then, "to" |
| 1828 | * can be under rmdir(). But in current implementation, caller of |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 1829 | * this function is just force_empty() and move charge, so it's |
| 1830 | * garanteed that "to" is never removed. So, we don't check rmdir |
| 1831 | * status here. |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 1832 | */ |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | /* |
| 1836 | * check whether the @pc is valid for moving account and call |
| 1837 | * __mem_cgroup_move_account() |
| 1838 | */ |
| 1839 | static int mem_cgroup_move_account(struct page_cgroup *pc, |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 1840 | struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge) |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 1841 | { |
| 1842 | int ret = -EINVAL; |
| 1843 | lock_page_cgroup(pc); |
| 1844 | if (PageCgroupUsed(pc) && pc->mem_cgroup == from) { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 1845 | __mem_cgroup_move_account(pc, from, to, uncharge); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 1846 | ret = 0; |
| 1847 | } |
| 1848 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 1849 | /* |
| 1850 | * check events |
| 1851 | */ |
| 1852 | memcg_check_events(to, pc->page); |
| 1853 | memcg_check_events(from, pc->page); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1854 | return ret; |
| 1855 | } |
| 1856 | |
| 1857 | /* |
| 1858 | * move charges to its parent. |
| 1859 | */ |
| 1860 | |
| 1861 | static int mem_cgroup_move_parent(struct page_cgroup *pc, |
| 1862 | struct mem_cgroup *child, |
| 1863 | gfp_t gfp_mask) |
| 1864 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1865 | struct page *page = pc->page; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1866 | struct cgroup *cg = child->css.cgroup; |
| 1867 | struct cgroup *pcg = cg->parent; |
| 1868 | struct mem_cgroup *parent; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1869 | int ret; |
| 1870 | |
| 1871 | /* Is ROOT ? */ |
| 1872 | if (!pcg) |
| 1873 | return -EINVAL; |
| 1874 | |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 1875 | ret = -EBUSY; |
| 1876 | if (!get_page_unless_zero(page)) |
| 1877 | goto out; |
| 1878 | if (isolate_lru_page(page)) |
| 1879 | goto put; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1880 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1881 | parent = mem_cgroup_from_cont(pcg); |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 1882 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1883 | if (ret || !parent) |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 1884 | goto put_back; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1885 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 1886 | ret = mem_cgroup_move_account(pc, child, parent, true); |
| 1887 | if (ret) |
| 1888 | mem_cgroup_cancel_charge(parent); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 1889 | put_back: |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1890 | putback_lru_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 1891 | put: |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 1892 | put_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 1893 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1894 | return ret; |
| 1895 | } |
| 1896 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1897 | /* |
| 1898 | * Charge the memory controller for page usage. |
| 1899 | * Return |
| 1900 | * 0 if the charge was successful |
| 1901 | * < 0 if the cgroup is over its limit |
| 1902 | */ |
| 1903 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, |
| 1904 | gfp_t gfp_mask, enum charge_type ctype, |
| 1905 | struct mem_cgroup *memcg) |
| 1906 | { |
| 1907 | struct mem_cgroup *mem; |
| 1908 | struct page_cgroup *pc; |
| 1909 | int ret; |
| 1910 | |
| 1911 | pc = lookup_page_cgroup(page); |
| 1912 | /* can happen at boot */ |
| 1913 | if (unlikely(!pc)) |
| 1914 | return 0; |
| 1915 | prefetchw(pc); |
| 1916 | |
| 1917 | mem = memcg; |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 1918 | ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1919 | if (ret || !mem) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1920 | return ret; |
| 1921 | |
| 1922 | __mem_cgroup_commit_charge(mem, pc, ctype); |
| 1923 | return 0; |
| 1924 | } |
| 1925 | |
| 1926 | int mem_cgroup_newpage_charge(struct page *page, |
| 1927 | struct mm_struct *mm, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 1928 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1929 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 1930 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1931 | if (PageCompound(page)) |
| 1932 | return 0; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1933 | /* |
| 1934 | * If already mapped, we don't have to account. |
| 1935 | * If page cache, page->mapping has address_space. |
| 1936 | * But page->mapping may have out-of-use anon_vma pointer, |
| 1937 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping |
| 1938 | * is NULL. |
| 1939 | */ |
| 1940 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) |
| 1941 | return 0; |
| 1942 | if (unlikely(!mm)) |
| 1943 | mm = &init_mm; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 1944 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1945 | MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL); |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 1946 | } |
| 1947 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 1948 | static void |
| 1949 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 1950 | enum charge_type ctype); |
| 1951 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 1952 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 1953 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1954 | { |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1955 | struct mem_cgroup *mem = NULL; |
| 1956 | int ret; |
| 1957 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1958 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 1959 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1960 | if (PageCompound(page)) |
| 1961 | return 0; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 1962 | /* |
| 1963 | * Corner case handling. This is called from add_to_page_cache() |
| 1964 | * in usual. But some FS (shmem) precharges this page before calling it |
| 1965 | * and call add_to_page_cache() with GFP_NOWAIT. |
| 1966 | * |
| 1967 | * For GFP_NOWAIT case, the page may be pre-charged before calling |
| 1968 | * add_to_page_cache(). (See shmem.c) check it here and avoid to call |
| 1969 | * charge twice. (It works but has to pay a bit larger cost.) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1970 | * And when the page is SwapCache, it should take swap information |
| 1971 | * into account. This is under lock_page() now. |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 1972 | */ |
| 1973 | if (!(gfp_mask & __GFP_WAIT)) { |
| 1974 | struct page_cgroup *pc; |
| 1975 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1976 | |
| 1977 | pc = lookup_page_cgroup(page); |
| 1978 | if (!pc) |
| 1979 | return 0; |
| 1980 | lock_page_cgroup(pc); |
| 1981 | if (PageCgroupUsed(pc)) { |
| 1982 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 1983 | return 0; |
| 1984 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1985 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 1986 | } |
| 1987 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1988 | if (unlikely(!mm && !mem)) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1989 | mm = &init_mm; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 1990 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 1991 | if (page_is_file_cache(page)) |
| 1992 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1993 | MEM_CGROUP_CHARGE_TYPE_CACHE, NULL); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1994 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 1995 | /* shmem */ |
| 1996 | if (PageSwapCache(page)) { |
| 1997 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
| 1998 | if (!ret) |
| 1999 | __mem_cgroup_commit_charge_swapin(page, mem, |
| 2000 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
| 2001 | } else |
| 2002 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, |
| 2003 | MEM_CGROUP_CHARGE_TYPE_SHMEM, mem); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2004 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2005 | return ret; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 2006 | } |
| 2007 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2008 | /* |
| 2009 | * While swap-in, try_charge -> commit or cancel, the page is locked. |
| 2010 | * 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] | 2011 | * struct page_cgroup is acquired. This refcnt will be consumed by |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2012 | * "commit()" or removed by "cancel()" |
| 2013 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2014 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
| 2015 | struct page *page, |
| 2016 | gfp_t mask, struct mem_cgroup **ptr) |
| 2017 | { |
| 2018 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2019 | int ret; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2020 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2021 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2022 | return 0; |
| 2023 | |
| 2024 | if (!do_swap_account) |
| 2025 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2026 | /* |
| 2027 | * A racing thread's fault, or swapoff, may have already updated |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2028 | * the pte, and even removed page from swap cache: in those cases |
| 2029 | * do_swap_page()'s pte_same() test will fail; but there's also a |
| 2030 | * KSM case which does need to charge the page. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2031 | */ |
| 2032 | if (!PageSwapCache(page)) |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2033 | goto charge_cur_mm; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2034 | mem = try_get_mem_cgroup_from_page(page); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2035 | if (!mem) |
| 2036 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2037 | *ptr = mem; |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 2038 | ret = __mem_cgroup_try_charge(NULL, mask, ptr, true); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2039 | /* drop extra refcnt from tryget */ |
| 2040 | css_put(&mem->css); |
| 2041 | return ret; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2042 | charge_cur_mm: |
| 2043 | if (unlikely(!mm)) |
| 2044 | mm = &init_mm; |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 2045 | return __mem_cgroup_try_charge(mm, mask, ptr, true); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2046 | } |
| 2047 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2048 | static void |
| 2049 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 2050 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2051 | { |
| 2052 | struct page_cgroup *pc; |
| 2053 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2054 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2055 | return; |
| 2056 | if (!ptr) |
| 2057 | return; |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2058 | cgroup_exclude_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2059 | pc = lookup_page_cgroup(page); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 2060 | mem_cgroup_lru_del_before_commit_swapcache(page); |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2061 | __mem_cgroup_commit_charge(ptr, pc, ctype); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 2062 | mem_cgroup_lru_add_after_commit_swapcache(page); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2063 | /* |
| 2064 | * Now swap is on-memory. This means this page may be |
| 2065 | * counted both as mem and swap....double count. |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2066 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
| 2067 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() |
| 2068 | * may call delete_from_swap_cache() before reach here. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2069 | */ |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2070 | if (do_swap_account && PageSwapCache(page)) { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2071 | swp_entry_t ent = {.val = page_private(page)}; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2072 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2073 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2074 | |
| 2075 | id = swap_cgroup_record(ent, 0); |
| 2076 | rcu_read_lock(); |
| 2077 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2078 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2079 | /* |
| 2080 | * This recorded memcg can be obsolete one. So, avoid |
| 2081 | * calling css_tryget |
| 2082 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2083 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 2084 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2085 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2086 | mem_cgroup_put(memcg); |
| 2087 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2088 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2089 | } |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2090 | /* |
| 2091 | * At swapin, we may charge account against cgroup which has no tasks. |
| 2092 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 2093 | * In that case, we need to call pre_destroy() again. check it here. |
| 2094 | */ |
| 2095 | cgroup_release_and_wakeup_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2096 | } |
| 2097 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2098 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
| 2099 | { |
| 2100 | __mem_cgroup_commit_charge_swapin(page, ptr, |
| 2101 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 2102 | } |
| 2103 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2104 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem) |
| 2105 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2106 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2107 | return; |
| 2108 | if (!mem) |
| 2109 | return; |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2110 | mem_cgroup_cancel_charge(mem); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2111 | } |
| 2112 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2113 | static void |
| 2114 | __do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype) |
| 2115 | { |
| 2116 | struct memcg_batch_info *batch = NULL; |
| 2117 | bool uncharge_memsw = true; |
| 2118 | /* If swapout, usage of swap doesn't decrease */ |
| 2119 | if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) |
| 2120 | uncharge_memsw = false; |
| 2121 | /* |
| 2122 | * do_batch > 0 when unmapping pages or inode invalidate/truncate. |
| 2123 | * In those cases, all pages freed continously can be expected to be in |
| 2124 | * the same cgroup and we have chance to coalesce uncharges. |
| 2125 | * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE) |
| 2126 | * because we want to do uncharge as soon as possible. |
| 2127 | */ |
| 2128 | if (!current->memcg_batch.do_batch || test_thread_flag(TIF_MEMDIE)) |
| 2129 | goto direct_uncharge; |
| 2130 | |
| 2131 | batch = ¤t->memcg_batch; |
| 2132 | /* |
| 2133 | * In usual, we do css_get() when we remember memcg pointer. |
| 2134 | * But in this case, we keep res->usage until end of a series of |
| 2135 | * uncharges. Then, it's ok to ignore memcg's refcnt. |
| 2136 | */ |
| 2137 | if (!batch->memcg) |
| 2138 | batch->memcg = mem; |
| 2139 | /* |
| 2140 | * In typical case, batch->memcg == mem. This means we can |
| 2141 | * merge a series of uncharges to an uncharge of res_counter. |
| 2142 | * If not, we uncharge res_counter ony by one. |
| 2143 | */ |
| 2144 | if (batch->memcg != mem) |
| 2145 | goto direct_uncharge; |
| 2146 | /* remember freed charge and uncharge it later */ |
| 2147 | batch->bytes += PAGE_SIZE; |
| 2148 | if (uncharge_memsw) |
| 2149 | batch->memsw_bytes += PAGE_SIZE; |
| 2150 | return; |
| 2151 | direct_uncharge: |
| 2152 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 2153 | if (uncharge_memsw) |
| 2154 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); |
| 2155 | return; |
| 2156 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2157 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2158 | /* |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2159 | * uncharge if !page_mapped(page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2160 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2161 | static struct mem_cgroup * |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2162 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2163 | { |
Hugh Dickins | 8289546 | 2008-03-04 14:29:08 -0800 | [diff] [blame] | 2164 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2165 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 2166 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2167 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2168 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2169 | return NULL; |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 2170 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2171 | if (PageSwapCache(page)) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2172 | return NULL; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2173 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2174 | /* |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 2175 | * Check if our page_cgroup is valid |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2176 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2177 | pc = lookup_page_cgroup(page); |
| 2178 | if (unlikely(!pc || !PageCgroupUsed(pc))) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2179 | return NULL; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2180 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2181 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2182 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2183 | mem = pc->mem_cgroup; |
| 2184 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2185 | if (!PageCgroupUsed(pc)) |
| 2186 | goto unlock_out; |
| 2187 | |
| 2188 | switch (ctype) { |
| 2189 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 2190 | case MEM_CGROUP_CHARGE_TYPE_DROP: |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2191 | if (page_mapped(page)) |
| 2192 | goto unlock_out; |
| 2193 | break; |
| 2194 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: |
| 2195 | if (!PageAnon(page)) { /* Shared memory */ |
| 2196 | if (page->mapping && !page_is_file_cache(page)) |
| 2197 | goto unlock_out; |
| 2198 | } else if (page_mapped(page)) /* Anon */ |
| 2199 | goto unlock_out; |
| 2200 | break; |
| 2201 | default: |
| 2202 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2203 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2204 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2205 | if (!mem_cgroup_is_root(mem)) |
| 2206 | __do_uncharge(mem, ctype); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2207 | if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) |
| 2208 | mem_cgroup_swap_statistics(mem, true); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2209 | mem_cgroup_charge_statistics(mem, pc, false); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 2210 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2211 | ClearPageCgroupUsed(pc); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 2212 | /* |
| 2213 | * pc->mem_cgroup is not cleared here. It will be accessed when it's |
| 2214 | * freed from LRU. This is safe because uncharged page is expected not |
| 2215 | * to be reused (freed soon). Exception is SwapCache, it's handled by |
| 2216 | * special functions. |
| 2217 | */ |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 2218 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2219 | mz = page_cgroup_zoneinfo(pc); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2220 | unlock_page_cgroup(pc); |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 2221 | |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 2222 | memcg_check_events(mem, page); |
KAMEZAWA Hiroyuki | a7fe942 | 2009-01-07 18:08:13 -0800 | [diff] [blame] | 2223 | /* at swapout, this memcg will be accessed to record to swap */ |
| 2224 | if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT) |
| 2225 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2226 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2227 | return mem; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2228 | |
| 2229 | unlock_out: |
| 2230 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2231 | return NULL; |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 2232 | } |
| 2233 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2234 | void mem_cgroup_uncharge_page(struct page *page) |
| 2235 | { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2236 | /* early check. */ |
| 2237 | if (page_mapped(page)) |
| 2238 | return; |
| 2239 | if (page->mapping && !PageAnon(page)) |
| 2240 | return; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2241 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 2242 | } |
| 2243 | |
| 2244 | void mem_cgroup_uncharge_cache_page(struct page *page) |
| 2245 | { |
| 2246 | VM_BUG_ON(page_mapped(page)); |
KAMEZAWA Hiroyuki | b7abea9 | 2008-10-18 20:28:09 -0700 | [diff] [blame] | 2247 | VM_BUG_ON(page->mapping); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2248 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 2249 | } |
| 2250 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2251 | /* |
| 2252 | * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate. |
| 2253 | * In that cases, pages are freed continuously and we can expect pages |
| 2254 | * are in the same memcg. All these calls itself limits the number of |
| 2255 | * pages freed at once, then uncharge_start/end() is called properly. |
| 2256 | * This may be called prural(2) times in a context, |
| 2257 | */ |
| 2258 | |
| 2259 | void mem_cgroup_uncharge_start(void) |
| 2260 | { |
| 2261 | current->memcg_batch.do_batch++; |
| 2262 | /* We can do nest. */ |
| 2263 | if (current->memcg_batch.do_batch == 1) { |
| 2264 | current->memcg_batch.memcg = NULL; |
| 2265 | current->memcg_batch.bytes = 0; |
| 2266 | current->memcg_batch.memsw_bytes = 0; |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | void mem_cgroup_uncharge_end(void) |
| 2271 | { |
| 2272 | struct memcg_batch_info *batch = ¤t->memcg_batch; |
| 2273 | |
| 2274 | if (!batch->do_batch) |
| 2275 | return; |
| 2276 | |
| 2277 | batch->do_batch--; |
| 2278 | if (batch->do_batch) /* If stacked, do nothing. */ |
| 2279 | return; |
| 2280 | |
| 2281 | if (!batch->memcg) |
| 2282 | return; |
| 2283 | /* |
| 2284 | * This "batch->memcg" is valid without any css_get/put etc... |
| 2285 | * bacause we hide charges behind us. |
| 2286 | */ |
| 2287 | if (batch->bytes) |
| 2288 | res_counter_uncharge(&batch->memcg->res, batch->bytes); |
| 2289 | if (batch->memsw_bytes) |
| 2290 | res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes); |
| 2291 | /* forget this pointer (for sanity check) */ |
| 2292 | batch->memcg = NULL; |
| 2293 | } |
| 2294 | |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 2295 | #ifdef CONFIG_SWAP |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2296 | /* |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 2297 | * called after __delete_from_swap_cache() and drop "page" account. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2298 | * memcg information is recorded to swap_cgroup of "ent" |
| 2299 | */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 2300 | void |
| 2301 | 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] | 2302 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2303 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 2304 | int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2305 | |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 2306 | if (!swapout) /* this was a swap cache but the swap is unused ! */ |
| 2307 | ctype = MEM_CGROUP_CHARGE_TYPE_DROP; |
| 2308 | |
| 2309 | memcg = __mem_cgroup_uncharge_common(page, ctype); |
| 2310 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2311 | /* record memcg information */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 2312 | if (do_swap_account && swapout && memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2313 | swap_cgroup_record(ent, css_id(&memcg->css)); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2314 | mem_cgroup_get(memcg); |
| 2315 | } |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 2316 | if (swapout && memcg) |
KAMEZAWA Hiroyuki | a7fe942 | 2009-01-07 18:08:13 -0800 | [diff] [blame] | 2317 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2318 | } |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 2319 | #endif |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2320 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2321 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 2322 | /* |
| 2323 | * called from swap_entry_free(). remove record in swap_cgroup and |
| 2324 | * uncharge "memsw" account. |
| 2325 | */ |
| 2326 | void mem_cgroup_uncharge_swap(swp_entry_t ent) |
| 2327 | { |
| 2328 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2329 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2330 | |
| 2331 | if (!do_swap_account) |
| 2332 | return; |
| 2333 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2334 | id = swap_cgroup_record(ent, 0); |
| 2335 | rcu_read_lock(); |
| 2336 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2337 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2338 | /* |
| 2339 | * We uncharge this because swap is freed. |
| 2340 | * This memcg can be obsolete one. We avoid calling css_tryget |
| 2341 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2342 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 2343 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2344 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2345 | mem_cgroup_put(memcg); |
| 2346 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2347 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2348 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 2349 | |
| 2350 | /** |
| 2351 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. |
| 2352 | * @entry: swap entry to be moved |
| 2353 | * @from: mem_cgroup which the entry is moved from |
| 2354 | * @to: mem_cgroup which the entry is moved to |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 2355 | * @need_fixup: whether we should fixup res_counters and refcounts. |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 2356 | * |
| 2357 | * It succeeds only when the swap_cgroup's record for this entry is the same |
| 2358 | * as the mem_cgroup's id of @from. |
| 2359 | * |
| 2360 | * Returns 0 on success, -EINVAL on failure. |
| 2361 | * |
| 2362 | * The caller must have charged to @to, IOW, called res_counter_charge() about |
| 2363 | * both res and memsw, and called css_get(). |
| 2364 | */ |
| 2365 | static int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 2366 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 2367 | { |
| 2368 | unsigned short old_id, new_id; |
| 2369 | |
| 2370 | old_id = css_id(&from->css); |
| 2371 | new_id = css_id(&to->css); |
| 2372 | |
| 2373 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 2374 | mem_cgroup_swap_statistics(from, false); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 2375 | mem_cgroup_swap_statistics(to, true); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 2376 | /* |
| 2377 | * This function is only called from task migration context now. |
| 2378 | * It postpones res_counter and refcount handling till the end |
| 2379 | * of task migration(mem_cgroup_clear_mc()) for performance |
| 2380 | * improvement. But we cannot postpone mem_cgroup_get(to) |
| 2381 | * because if the process that has been moved to @to does |
| 2382 | * swap-in, the refcount of @to might be decreased to 0. |
| 2383 | */ |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 2384 | mem_cgroup_get(to); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 2385 | if (need_fixup) { |
| 2386 | if (!mem_cgroup_is_root(from)) |
| 2387 | res_counter_uncharge(&from->memsw, PAGE_SIZE); |
| 2388 | mem_cgroup_put(from); |
| 2389 | /* |
| 2390 | * we charged both to->res and to->memsw, so we should |
| 2391 | * uncharge to->res. |
| 2392 | */ |
| 2393 | if (!mem_cgroup_is_root(to)) |
| 2394 | res_counter_uncharge(&to->res, PAGE_SIZE); |
| 2395 | css_put(&to->css); |
| 2396 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 2397 | return 0; |
| 2398 | } |
| 2399 | return -EINVAL; |
| 2400 | } |
| 2401 | #else |
| 2402 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 2403 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 2404 | { |
| 2405 | return -EINVAL; |
| 2406 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2407 | #endif |
| 2408 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 2409 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 2410 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
| 2411 | * page belongs to. |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 2412 | */ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 2413 | int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr) |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 2414 | { |
| 2415 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 2416 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 2417 | int ret = 0; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 2418 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2419 | if (mem_cgroup_disabled()) |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 2420 | return 0; |
| 2421 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2422 | pc = lookup_page_cgroup(page); |
| 2423 | lock_page_cgroup(pc); |
| 2424 | if (PageCgroupUsed(pc)) { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 2425 | mem = pc->mem_cgroup; |
| 2426 | css_get(&mem->css); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 2427 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2428 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 2429 | |
Andrea Arcangeli | 93d5c9b | 2010-04-23 13:17:39 -0400 | [diff] [blame] | 2430 | *ptr = mem; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 2431 | if (mem) { |
Andrea Arcangeli | 93d5c9b | 2010-04-23 13:17:39 -0400 | [diff] [blame] | 2432 | ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, ptr, false); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 2433 | css_put(&mem->css); |
| 2434 | } |
| 2435 | return ret; |
| 2436 | } |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 2437 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2438 | /* remove redundant charge if migration failed*/ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 2439 | void mem_cgroup_end_migration(struct mem_cgroup *mem, |
| 2440 | struct page *oldpage, struct page *newpage) |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 2441 | { |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 2442 | struct page *target, *unused; |
| 2443 | struct page_cgroup *pc; |
| 2444 | enum charge_type ctype; |
| 2445 | |
| 2446 | if (!mem) |
| 2447 | return; |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2448 | cgroup_exclude_rmdir(&mem->css); |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 2449 | /* at migration success, oldpage->mapping is NULL. */ |
| 2450 | if (oldpage->mapping) { |
| 2451 | target = oldpage; |
| 2452 | unused = NULL; |
| 2453 | } else { |
| 2454 | target = newpage; |
| 2455 | unused = oldpage; |
| 2456 | } |
| 2457 | |
| 2458 | if (PageAnon(target)) |
| 2459 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; |
| 2460 | else if (page_is_file_cache(target)) |
| 2461 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; |
| 2462 | else |
| 2463 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; |
| 2464 | |
| 2465 | /* unused page is not on radix-tree now. */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2466 | if (unused) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 2467 | __mem_cgroup_uncharge_common(unused, ctype); |
| 2468 | |
| 2469 | pc = lookup_page_cgroup(target); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2470 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 2471 | * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup. |
| 2472 | * So, double-counting is effectively avoided. |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2473 | */ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 2474 | __mem_cgroup_commit_charge(mem, pc, ctype); |
| 2475 | |
| 2476 | /* |
| 2477 | * Both of oldpage and newpage are still under lock_page(). |
| 2478 | * Then, we don't have to care about race in radix-tree. |
| 2479 | * But we have to be careful that this page is unmapped or not. |
| 2480 | * |
| 2481 | * There is a case for !page_mapped(). At the start of |
| 2482 | * migration, oldpage was mapped. But now, it's zapped. |
| 2483 | * But we know *target* page is not freed/reused under us. |
| 2484 | * mem_cgroup_uncharge_page() does all necessary checks. |
| 2485 | */ |
| 2486 | if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED) |
| 2487 | mem_cgroup_uncharge_page(target); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2488 | /* |
| 2489 | * At migration, we may charge account against cgroup which has no tasks |
| 2490 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 2491 | * In that case, we need to call pre_destroy() again. check it here. |
| 2492 | */ |
| 2493 | cgroup_release_and_wakeup_rmdir(&mem->css); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 2494 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 2495 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 2496 | /* |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 2497 | * A call to try to shrink memory usage on charge failure at shmem's swapin. |
| 2498 | * Calling hierarchical_reclaim is not enough because we should update |
| 2499 | * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM. |
| 2500 | * Moreover considering hierarchy, we should reclaim from the mem_over_limit, |
| 2501 | * not from the memcg which this page would be charged to. |
| 2502 | * try_charge_swapin does all of these works properly. |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 2503 | */ |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 2504 | int mem_cgroup_shmem_charge_fallback(struct page *page, |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2505 | struct mm_struct *mm, |
| 2506 | gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 2507 | { |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2508 | struct mem_cgroup *mem = NULL; |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 2509 | int ret; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 2510 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2511 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 2512 | return 0; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 2513 | |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 2514 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
| 2515 | if (!ret) |
| 2516 | mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */ |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 2517 | |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 2518 | return ret; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 2519 | } |
| 2520 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2521 | static DEFINE_MUTEX(set_limit_mutex); |
| 2522 | |
KOSAKI Motohiro | d38d2a7 | 2009-01-06 14:39:44 -0800 | [diff] [blame] | 2523 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2524 | unsigned long long val) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2525 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 2526 | int retry_count; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2527 | u64 memswlimit; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2528 | int ret = 0; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 2529 | int children = mem_cgroup_count_children(memcg); |
| 2530 | u64 curusage, oldusage; |
| 2531 | |
| 2532 | /* |
| 2533 | * For keeping hierarchical_reclaim simple, how long we should retry |
| 2534 | * is depends on callers. We set our retry-count to be function |
| 2535 | * of # of children which we should visit in this loop. |
| 2536 | */ |
| 2537 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; |
| 2538 | |
| 2539 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2540 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2541 | while (retry_count) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2542 | if (signal_pending(current)) { |
| 2543 | ret = -EINTR; |
| 2544 | break; |
| 2545 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2546 | /* |
| 2547 | * Rather than hide all in some function, I do this in |
| 2548 | * open coded manner. You see what this really does. |
| 2549 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 2550 | */ |
| 2551 | mutex_lock(&set_limit_mutex); |
| 2552 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 2553 | if (memswlimit < val) { |
| 2554 | ret = -EINVAL; |
| 2555 | mutex_unlock(&set_limit_mutex); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2556 | break; |
| 2557 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2558 | ret = res_counter_set_limit(&memcg->res, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 2559 | if (!ret) { |
| 2560 | if (memswlimit == val) |
| 2561 | memcg->memsw_is_minimum = true; |
| 2562 | else |
| 2563 | memcg->memsw_is_minimum = false; |
| 2564 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2565 | mutex_unlock(&set_limit_mutex); |
| 2566 | |
| 2567 | if (!ret) |
| 2568 | break; |
| 2569 | |
Bob Liu | aa20d48 | 2009-12-15 16:47:14 -0800 | [diff] [blame] | 2570 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2571 | MEM_CGROUP_RECLAIM_SHRINK); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 2572 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
| 2573 | /* Usage is reduced ? */ |
| 2574 | if (curusage >= oldusage) |
| 2575 | retry_count--; |
| 2576 | else |
| 2577 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2578 | } |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 2579 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2580 | return ret; |
| 2581 | } |
| 2582 | |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 2583 | static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
| 2584 | unsigned long long val) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2585 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 2586 | int retry_count; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2587 | u64 memlimit, oldusage, curusage; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 2588 | int children = mem_cgroup_count_children(memcg); |
| 2589 | int ret = -EBUSY; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2590 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 2591 | /* see mem_cgroup_resize_res_limit */ |
| 2592 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; |
| 2593 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2594 | while (retry_count) { |
| 2595 | if (signal_pending(current)) { |
| 2596 | ret = -EINTR; |
| 2597 | break; |
| 2598 | } |
| 2599 | /* |
| 2600 | * Rather than hide all in some function, I do this in |
| 2601 | * open coded manner. You see what this really does. |
| 2602 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 2603 | */ |
| 2604 | mutex_lock(&set_limit_mutex); |
| 2605 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 2606 | if (memlimit > val) { |
| 2607 | ret = -EINVAL; |
| 2608 | mutex_unlock(&set_limit_mutex); |
| 2609 | break; |
| 2610 | } |
| 2611 | ret = res_counter_set_limit(&memcg->memsw, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 2612 | if (!ret) { |
| 2613 | if (memlimit == val) |
| 2614 | memcg->memsw_is_minimum = true; |
| 2615 | else |
| 2616 | memcg->memsw_is_minimum = false; |
| 2617 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2618 | mutex_unlock(&set_limit_mutex); |
| 2619 | |
| 2620 | if (!ret) |
| 2621 | break; |
| 2622 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2623 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 2624 | MEM_CGROUP_RECLAIM_NOSWAP | |
| 2625 | MEM_CGROUP_RECLAIM_SHRINK); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2626 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 2627 | /* Usage is reduced ? */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2628 | if (curusage >= oldusage) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2629 | retry_count--; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 2630 | else |
| 2631 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2632 | } |
| 2633 | return ret; |
| 2634 | } |
| 2635 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2636 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
| 2637 | gfp_t gfp_mask, int nid, |
| 2638 | int zid) |
| 2639 | { |
| 2640 | unsigned long nr_reclaimed = 0; |
| 2641 | struct mem_cgroup_per_zone *mz, *next_mz = NULL; |
| 2642 | unsigned long reclaimed; |
| 2643 | int loop = 0; |
| 2644 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 2645 | unsigned long long excess; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2646 | |
| 2647 | if (order > 0) |
| 2648 | return 0; |
| 2649 | |
| 2650 | mctz = soft_limit_tree_node_zone(nid, zid); |
| 2651 | /* |
| 2652 | * This loop can run a while, specially if mem_cgroup's continuously |
| 2653 | * keep exceeding their soft limit and putting the system under |
| 2654 | * pressure |
| 2655 | */ |
| 2656 | do { |
| 2657 | if (next_mz) |
| 2658 | mz = next_mz; |
| 2659 | else |
| 2660 | mz = mem_cgroup_largest_soft_limit_node(mctz); |
| 2661 | if (!mz) |
| 2662 | break; |
| 2663 | |
| 2664 | reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone, |
| 2665 | gfp_mask, |
| 2666 | MEM_CGROUP_RECLAIM_SOFT); |
| 2667 | nr_reclaimed += reclaimed; |
| 2668 | spin_lock(&mctz->lock); |
| 2669 | |
| 2670 | /* |
| 2671 | * If we failed to reclaim anything from this memory cgroup |
| 2672 | * it is time to move on to the next cgroup |
| 2673 | */ |
| 2674 | next_mz = NULL; |
| 2675 | if (!reclaimed) { |
| 2676 | do { |
| 2677 | /* |
| 2678 | * Loop until we find yet another one. |
| 2679 | * |
| 2680 | * By the time we get the soft_limit lock |
| 2681 | * again, someone might have aded the |
| 2682 | * group back on the RB tree. Iterate to |
| 2683 | * make sure we get a different mem. |
| 2684 | * mem_cgroup_largest_soft_limit_node returns |
| 2685 | * NULL if no other cgroup is present on |
| 2686 | * the tree |
| 2687 | */ |
| 2688 | next_mz = |
| 2689 | __mem_cgroup_largest_soft_limit_node(mctz); |
| 2690 | if (next_mz == mz) { |
| 2691 | css_put(&next_mz->mem->css); |
| 2692 | next_mz = NULL; |
| 2693 | } else /* next_mz == NULL or other memcg */ |
| 2694 | break; |
| 2695 | } while (1); |
| 2696 | } |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2697 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 2698 | excess = res_counter_soft_limit_excess(&mz->mem->res); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2699 | /* |
| 2700 | * One school of thought says that we should not add |
| 2701 | * back the node to the tree if reclaim returns 0. |
| 2702 | * But our reclaim could return 0, simply because due |
| 2703 | * to priority we are exposing a smaller subset of |
| 2704 | * memory to reclaim from. Consider this as a longer |
| 2705 | * term TODO. |
| 2706 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 2707 | /* If excess == 0, no tree ops */ |
| 2708 | __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2709 | spin_unlock(&mctz->lock); |
| 2710 | css_put(&mz->mem->css); |
| 2711 | loop++; |
| 2712 | /* |
| 2713 | * Could not reclaim anything and there are no more |
| 2714 | * mem cgroups to try or we seem to be looping without |
| 2715 | * reclaiming anything. |
| 2716 | */ |
| 2717 | if (!nr_reclaimed && |
| 2718 | (next_mz == NULL || |
| 2719 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) |
| 2720 | break; |
| 2721 | } while (!nr_reclaimed); |
| 2722 | if (next_mz) |
| 2723 | css_put(&next_mz->mem->css); |
| 2724 | return nr_reclaimed; |
| 2725 | } |
| 2726 | |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 2727 | /* |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 2728 | * This routine traverse page_cgroup in given list and drop them all. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 2729 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
| 2730 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2731 | static int mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2732 | int node, int zid, enum lru_list lru) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 2733 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2734 | struct zone *zone; |
| 2735 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2736 | struct page_cgroup *pc, *busy; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2737 | unsigned long flags, loop; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 2738 | struct list_head *list; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2739 | int ret = 0; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 2740 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2741 | zone = &NODE_DATA(node)->node_zones[zid]; |
| 2742 | mz = mem_cgroup_zoneinfo(mem, node, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 2743 | list = &mz->lists[lru]; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 2744 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2745 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
| 2746 | /* give some margin against EBUSY etc...*/ |
| 2747 | loop += 256; |
| 2748 | busy = NULL; |
| 2749 | while (loop--) { |
| 2750 | ret = 0; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2751 | spin_lock_irqsave(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2752 | if (list_empty(list)) { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2753 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2754 | break; |
| 2755 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2756 | pc = list_entry(list->prev, struct page_cgroup, lru); |
| 2757 | if (busy == pc) { |
| 2758 | list_move(&pc->lru, list); |
Thiago Farina | 648bcc7 | 2010-03-05 13:42:04 -0800 | [diff] [blame] | 2759 | busy = NULL; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2760 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2761 | continue; |
| 2762 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2763 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2764 | |
KAMEZAWA Hiroyuki | 2c26fdd | 2009-01-07 18:08:10 -0800 | [diff] [blame] | 2765 | ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2766 | if (ret == -ENOMEM) |
| 2767 | break; |
| 2768 | |
| 2769 | if (ret == -EBUSY || ret == -EINVAL) { |
| 2770 | /* found lock contention or "pc" is obsolete. */ |
| 2771 | busy = pc; |
| 2772 | cond_resched(); |
| 2773 | } else |
| 2774 | busy = NULL; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 2775 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2776 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2777 | if (!ret && !list_empty(list)) |
| 2778 | return -EBUSY; |
| 2779 | return ret; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | /* |
| 2783 | * make mem_cgroup's charge to be 0 if there is no task. |
| 2784 | * This enables deleting this mem_cgroup. |
| 2785 | */ |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2786 | 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] | 2787 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2788 | int ret; |
| 2789 | int node, zid, shrink; |
| 2790 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2791 | struct cgroup *cgrp = mem->css.cgroup; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 2792 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 2793 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2794 | |
| 2795 | shrink = 0; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2796 | /* should free all ? */ |
| 2797 | if (free_all) |
| 2798 | goto try_to_free; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2799 | move_account: |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 2800 | do { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2801 | ret = -EBUSY; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2802 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 2803 | goto out; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2804 | ret = -EINTR; |
| 2805 | if (signal_pending(current)) |
| 2806 | goto out; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2807 | /* This is for making all *used* pages to be on LRU. */ |
| 2808 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2809 | drain_all_stock_sync(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2810 | ret = 0; |
KAMEZAWA Hiroyuki | 299b4ea | 2009-01-29 14:25:17 -0800 | [diff] [blame] | 2811 | for_each_node_state(node, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2812 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 2813 | enum lru_list l; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2814 | for_each_lru(l) { |
| 2815 | ret = mem_cgroup_force_empty_list(mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2816 | node, zid, l); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2817 | if (ret) |
| 2818 | break; |
| 2819 | } |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 2820 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2821 | if (ret) |
| 2822 | break; |
| 2823 | } |
| 2824 | /* it seems parent cgroup doesn't have enough mem */ |
| 2825 | if (ret == -ENOMEM) |
| 2826 | goto try_to_free; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2827 | cond_resched(); |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 2828 | /* "ret" should also be checked to ensure all lists are empty. */ |
| 2829 | } while (mem->res.usage > 0 || ret); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 2830 | out: |
| 2831 | css_put(&mem->css); |
| 2832 | return ret; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2833 | |
| 2834 | try_to_free: |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2835 | /* returns EBUSY if there is a task or if we come here twice. */ |
| 2836 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2837 | ret = -EBUSY; |
| 2838 | goto out; |
| 2839 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2840 | /* we call try-to-free pages for make this cgroup empty */ |
| 2841 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2842 | /* try to free all pages in this cgroup */ |
| 2843 | shrink = 1; |
| 2844 | while (nr_retries && mem->res.usage > 0) { |
| 2845 | int progress; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2846 | |
| 2847 | if (signal_pending(current)) { |
| 2848 | ret = -EINTR; |
| 2849 | goto out; |
| 2850 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2851 | progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL, |
| 2852 | false, get_swappiness(mem)); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2853 | if (!progress) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2854 | nr_retries--; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2855 | /* maybe some writeback is necessary */ |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 2856 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2857 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2858 | |
| 2859 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2860 | lru_add_drain(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2861 | /* try move_account...there may be some *locked* pages. */ |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 2862 | goto move_account; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 2863 | } |
| 2864 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2865 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
| 2866 | { |
| 2867 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); |
| 2868 | } |
| 2869 | |
| 2870 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 2871 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
| 2872 | { |
| 2873 | return mem_cgroup_from_cont(cont)->use_hierarchy; |
| 2874 | } |
| 2875 | |
| 2876 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, |
| 2877 | u64 val) |
| 2878 | { |
| 2879 | int retval = 0; |
| 2880 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 2881 | struct cgroup *parent = cont->parent; |
| 2882 | struct mem_cgroup *parent_mem = NULL; |
| 2883 | |
| 2884 | if (parent) |
| 2885 | parent_mem = mem_cgroup_from_cont(parent); |
| 2886 | |
| 2887 | cgroup_lock(); |
| 2888 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2889 | * 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] | 2890 | * in the child subtrees. If it is unset, then the change can |
| 2891 | * occur, provided the current cgroup has no children. |
| 2892 | * |
| 2893 | * For the root cgroup, parent_mem is NULL, we allow value to be |
| 2894 | * set if there are no children. |
| 2895 | */ |
| 2896 | if ((!parent_mem || !parent_mem->use_hierarchy) && |
| 2897 | (val == 1 || val == 0)) { |
| 2898 | if (list_empty(&cont->children)) |
| 2899 | mem->use_hierarchy = val; |
| 2900 | else |
| 2901 | retval = -EBUSY; |
| 2902 | } else |
| 2903 | retval = -EINVAL; |
| 2904 | cgroup_unlock(); |
| 2905 | |
| 2906 | return retval; |
| 2907 | } |
| 2908 | |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2909 | struct mem_cgroup_idx_data { |
| 2910 | s64 val; |
| 2911 | enum mem_cgroup_stat_index idx; |
| 2912 | }; |
| 2913 | |
| 2914 | static int |
| 2915 | mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data) |
| 2916 | { |
| 2917 | struct mem_cgroup_idx_data *d = data; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 2918 | d->val += mem_cgroup_read_stat(mem, d->idx); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2919 | return 0; |
| 2920 | } |
| 2921 | |
| 2922 | static void |
| 2923 | mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem, |
| 2924 | enum mem_cgroup_stat_index idx, s64 *val) |
| 2925 | { |
| 2926 | struct mem_cgroup_idx_data d; |
| 2927 | d.idx = idx; |
| 2928 | d.val = 0; |
| 2929 | mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat); |
| 2930 | *val = d.val; |
| 2931 | } |
| 2932 | |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 2933 | static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap) |
| 2934 | { |
| 2935 | u64 idx_val, val; |
| 2936 | |
| 2937 | if (!mem_cgroup_is_root(mem)) { |
| 2938 | if (!swap) |
| 2939 | return res_counter_read_u64(&mem->res, RES_USAGE); |
| 2940 | else |
| 2941 | return res_counter_read_u64(&mem->memsw, RES_USAGE); |
| 2942 | } |
| 2943 | |
| 2944 | mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE, &idx_val); |
| 2945 | val = idx_val; |
| 2946 | mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS, &idx_val); |
| 2947 | val += idx_val; |
| 2948 | |
| 2949 | if (swap) { |
| 2950 | mem_cgroup_get_recursive_idx_stat(mem, |
| 2951 | MEM_CGROUP_STAT_SWAPOUT, &idx_val); |
| 2952 | val += idx_val; |
| 2953 | } |
| 2954 | |
| 2955 | return val << PAGE_SHIFT; |
| 2956 | } |
| 2957 | |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 2958 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2959 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2960 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 2961 | u64 val; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2962 | int type, name; |
| 2963 | |
| 2964 | type = MEMFILE_TYPE(cft->private); |
| 2965 | name = MEMFILE_ATTR(cft->private); |
| 2966 | switch (type) { |
| 2967 | case _MEM: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 2968 | if (name == RES_USAGE) |
| 2969 | val = mem_cgroup_usage(mem, false); |
| 2970 | else |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2971 | val = res_counter_read_u64(&mem->res, name); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2972 | break; |
| 2973 | case _MEMSWAP: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 2974 | if (name == RES_USAGE) |
| 2975 | val = mem_cgroup_usage(mem, true); |
| 2976 | else |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2977 | val = res_counter_read_u64(&mem->memsw, name); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2978 | break; |
| 2979 | default: |
| 2980 | BUG(); |
| 2981 | break; |
| 2982 | } |
| 2983 | return val; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2984 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2985 | /* |
| 2986 | * The user of this function is... |
| 2987 | * RES_LIMIT. |
| 2988 | */ |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 2989 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 2990 | const char *buffer) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2991 | { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2992 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2993 | int type, name; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2994 | unsigned long long val; |
| 2995 | int ret; |
| 2996 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2997 | type = MEMFILE_TYPE(cft->private); |
| 2998 | name = MEMFILE_ATTR(cft->private); |
| 2999 | switch (name) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3000 | case RES_LIMIT: |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 3001 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
| 3002 | ret = -EINVAL; |
| 3003 | break; |
| 3004 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3005 | /* This function does all necessary parse...reuse it */ |
| 3006 | ret = res_counter_memparse_write_strategy(buffer, &val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3007 | if (ret) |
| 3008 | break; |
| 3009 | if (type == _MEM) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3010 | ret = mem_cgroup_resize_limit(memcg, val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3011 | else |
| 3012 | ret = mem_cgroup_resize_memsw_limit(memcg, val); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3013 | break; |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 3014 | case RES_SOFT_LIMIT: |
| 3015 | ret = res_counter_memparse_write_strategy(buffer, &val); |
| 3016 | if (ret) |
| 3017 | break; |
| 3018 | /* |
| 3019 | * For memsw, soft limits are hard to implement in terms |
| 3020 | * of semantics, for now, we support soft limits for |
| 3021 | * control without swap |
| 3022 | */ |
| 3023 | if (type == _MEM) |
| 3024 | ret = res_counter_set_soft_limit(&memcg->res, val); |
| 3025 | else |
| 3026 | ret = -EINVAL; |
| 3027 | break; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3028 | default: |
| 3029 | ret = -EINVAL; /* should be BUG() ? */ |
| 3030 | break; |
| 3031 | } |
| 3032 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3033 | } |
| 3034 | |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 3035 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
| 3036 | unsigned long long *mem_limit, unsigned long long *memsw_limit) |
| 3037 | { |
| 3038 | struct cgroup *cgroup; |
| 3039 | unsigned long long min_limit, min_memsw_limit, tmp; |
| 3040 | |
| 3041 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3042 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3043 | cgroup = memcg->css.cgroup; |
| 3044 | if (!memcg->use_hierarchy) |
| 3045 | goto out; |
| 3046 | |
| 3047 | while (cgroup->parent) { |
| 3048 | cgroup = cgroup->parent; |
| 3049 | memcg = mem_cgroup_from_cont(cgroup); |
| 3050 | if (!memcg->use_hierarchy) |
| 3051 | break; |
| 3052 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3053 | min_limit = min(min_limit, tmp); |
| 3054 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3055 | min_memsw_limit = min(min_memsw_limit, tmp); |
| 3056 | } |
| 3057 | out: |
| 3058 | *mem_limit = min_limit; |
| 3059 | *memsw_limit = min_memsw_limit; |
| 3060 | return; |
| 3061 | } |
| 3062 | |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 3063 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 3064 | { |
| 3065 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3066 | int type, name; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 3067 | |
| 3068 | mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3069 | type = MEMFILE_TYPE(event); |
| 3070 | name = MEMFILE_ATTR(event); |
| 3071 | switch (name) { |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 3072 | case RES_MAX_USAGE: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3073 | if (type == _MEM) |
| 3074 | res_counter_reset_max(&mem->res); |
| 3075 | else |
| 3076 | res_counter_reset_max(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 3077 | break; |
| 3078 | case RES_FAILCNT: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3079 | if (type == _MEM) |
| 3080 | res_counter_reset_failcnt(&mem->res); |
| 3081 | else |
| 3082 | res_counter_reset_failcnt(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 3083 | break; |
| 3084 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 3085 | |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 3086 | return 0; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 3087 | } |
| 3088 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 3089 | static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp, |
| 3090 | struct cftype *cft) |
| 3091 | { |
| 3092 | return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate; |
| 3093 | } |
| 3094 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3095 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 3096 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 3097 | struct cftype *cft, u64 val) |
| 3098 | { |
| 3099 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 3100 | |
| 3101 | if (val >= (1 << NR_MOVE_TYPE)) |
| 3102 | return -EINVAL; |
| 3103 | /* |
| 3104 | * We check this value several times in both in can_attach() and |
| 3105 | * attach(), so we need cgroup lock to prevent this value from being |
| 3106 | * inconsistent. |
| 3107 | */ |
| 3108 | cgroup_lock(); |
| 3109 | mem->move_charge_at_immigrate = val; |
| 3110 | cgroup_unlock(); |
| 3111 | |
| 3112 | return 0; |
| 3113 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3114 | #else |
| 3115 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 3116 | struct cftype *cft, u64 val) |
| 3117 | { |
| 3118 | return -ENOSYS; |
| 3119 | } |
| 3120 | #endif |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 3121 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3122 | |
| 3123 | /* For read statistics */ |
| 3124 | enum { |
| 3125 | MCS_CACHE, |
| 3126 | MCS_RSS, |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 3127 | MCS_FILE_MAPPED, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3128 | MCS_PGPGIN, |
| 3129 | MCS_PGPGOUT, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 3130 | MCS_SWAP, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3131 | MCS_INACTIVE_ANON, |
| 3132 | MCS_ACTIVE_ANON, |
| 3133 | MCS_INACTIVE_FILE, |
| 3134 | MCS_ACTIVE_FILE, |
| 3135 | MCS_UNEVICTABLE, |
| 3136 | NR_MCS_STAT, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 3137 | }; |
| 3138 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3139 | struct mcs_total_stat { |
| 3140 | s64 stat[NR_MCS_STAT]; |
| 3141 | }; |
| 3142 | |
| 3143 | struct { |
| 3144 | char *local_name; |
| 3145 | char *total_name; |
| 3146 | } memcg_stat_strings[NR_MCS_STAT] = { |
| 3147 | {"cache", "total_cache"}, |
| 3148 | {"rss", "total_rss"}, |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 3149 | {"mapped_file", "total_mapped_file"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3150 | {"pgpgin", "total_pgpgin"}, |
| 3151 | {"pgpgout", "total_pgpgout"}, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 3152 | {"swap", "total_swap"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3153 | {"inactive_anon", "total_inactive_anon"}, |
| 3154 | {"active_anon", "total_active_anon"}, |
| 3155 | {"inactive_file", "total_inactive_file"}, |
| 3156 | {"active_file", "total_active_file"}, |
| 3157 | {"unevictable", "total_unevictable"} |
| 3158 | }; |
| 3159 | |
| 3160 | |
| 3161 | static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data) |
| 3162 | { |
| 3163 | struct mcs_total_stat *s = data; |
| 3164 | s64 val; |
| 3165 | |
| 3166 | /* per cpu stat */ |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 3167 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3168 | s->stat[MCS_CACHE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 3169 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3170 | s->stat[MCS_RSS] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 3171 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED); |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 3172 | s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 3173 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGIN_COUNT); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3174 | s->stat[MCS_PGPGIN] += val; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 3175 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGOUT_COUNT); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3176 | s->stat[MCS_PGPGOUT] += val; |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 3177 | if (do_swap_account) { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 3178 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 3179 | s->stat[MCS_SWAP] += val * PAGE_SIZE; |
| 3180 | } |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3181 | |
| 3182 | /* per zone stat */ |
| 3183 | val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON); |
| 3184 | s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE; |
| 3185 | val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON); |
| 3186 | s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE; |
| 3187 | val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE); |
| 3188 | s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE; |
| 3189 | val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE); |
| 3190 | s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE; |
| 3191 | val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE); |
| 3192 | s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE; |
| 3193 | return 0; |
| 3194 | } |
| 3195 | |
| 3196 | static void |
| 3197 | mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s) |
| 3198 | { |
| 3199 | mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat); |
| 3200 | } |
| 3201 | |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 3202 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
| 3203 | struct cgroup_map_cb *cb) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 3204 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 3205 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3206 | struct mcs_total_stat mystat; |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 3207 | int i; |
| 3208 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3209 | memset(&mystat, 0, sizeof(mystat)); |
| 3210 | mem_cgroup_get_local_stat(mem_cont, &mystat); |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 3211 | |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 3212 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 3213 | if (i == MCS_SWAP && !do_swap_account) |
| 3214 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3215 | cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 3216 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 3217 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3218 | /* Hierarchical information */ |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 3219 | { |
| 3220 | unsigned long long limit, memsw_limit; |
| 3221 | memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit); |
| 3222 | cb->fill(cb, "hierarchical_memory_limit", limit); |
| 3223 | if (do_swap_account) |
| 3224 | cb->fill(cb, "hierarchical_memsw_limit", memsw_limit); |
| 3225 | } |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 3226 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3227 | memset(&mystat, 0, sizeof(mystat)); |
| 3228 | mem_cgroup_get_total_stat(mem_cont, &mystat); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 3229 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 3230 | if (i == MCS_SWAP && !do_swap_account) |
| 3231 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3232 | cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 3233 | } |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 3234 | |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 3235 | #ifdef CONFIG_DEBUG_VM |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 3236 | cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL)); |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 3237 | |
| 3238 | { |
| 3239 | int nid, zid; |
| 3240 | struct mem_cgroup_per_zone *mz; |
| 3241 | unsigned long recent_rotated[2] = {0, 0}; |
| 3242 | unsigned long recent_scanned[2] = {0, 0}; |
| 3243 | |
| 3244 | for_each_online_node(nid) |
| 3245 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 3246 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
| 3247 | |
| 3248 | recent_rotated[0] += |
| 3249 | mz->reclaim_stat.recent_rotated[0]; |
| 3250 | recent_rotated[1] += |
| 3251 | mz->reclaim_stat.recent_rotated[1]; |
| 3252 | recent_scanned[0] += |
| 3253 | mz->reclaim_stat.recent_scanned[0]; |
| 3254 | recent_scanned[1] += |
| 3255 | mz->reclaim_stat.recent_scanned[1]; |
| 3256 | } |
| 3257 | cb->fill(cb, "recent_rotated_anon", recent_rotated[0]); |
| 3258 | cb->fill(cb, "recent_rotated_file", recent_rotated[1]); |
| 3259 | cb->fill(cb, "recent_scanned_anon", recent_scanned[0]); |
| 3260 | cb->fill(cb, "recent_scanned_file", recent_scanned[1]); |
| 3261 | } |
| 3262 | #endif |
| 3263 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 3264 | return 0; |
| 3265 | } |
| 3266 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 3267 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
| 3268 | { |
| 3269 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 3270 | |
| 3271 | return get_swappiness(memcg); |
| 3272 | } |
| 3273 | |
| 3274 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, |
| 3275 | u64 val) |
| 3276 | { |
| 3277 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 3278 | struct mem_cgroup *parent; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 3279 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 3280 | if (val > 100) |
| 3281 | return -EINVAL; |
| 3282 | |
| 3283 | if (cgrp->parent == NULL) |
| 3284 | return -EINVAL; |
| 3285 | |
| 3286 | parent = mem_cgroup_from_cont(cgrp->parent); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 3287 | |
| 3288 | cgroup_lock(); |
| 3289 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 3290 | /* If under hierarchy, only empty-root can set this value */ |
| 3291 | if ((parent->use_hierarchy) || |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 3292 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
| 3293 | cgroup_unlock(); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 3294 | return -EINVAL; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 3295 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 3296 | |
| 3297 | spin_lock(&memcg->reclaim_param_lock); |
| 3298 | memcg->swappiness = val; |
| 3299 | spin_unlock(&memcg->reclaim_param_lock); |
| 3300 | |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 3301 | cgroup_unlock(); |
| 3302 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 3303 | return 0; |
| 3304 | } |
| 3305 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 3306 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) |
| 3307 | { |
| 3308 | struct mem_cgroup_threshold_ary *t; |
| 3309 | u64 usage; |
| 3310 | int i; |
| 3311 | |
| 3312 | rcu_read_lock(); |
| 3313 | if (!swap) |
| 3314 | t = rcu_dereference(memcg->thresholds); |
| 3315 | else |
| 3316 | t = rcu_dereference(memcg->memsw_thresholds); |
| 3317 | |
| 3318 | if (!t) |
| 3319 | goto unlock; |
| 3320 | |
| 3321 | usage = mem_cgroup_usage(memcg, swap); |
| 3322 | |
| 3323 | /* |
| 3324 | * current_threshold points to threshold just below usage. |
| 3325 | * If it's not true, a threshold was crossed after last |
| 3326 | * call of __mem_cgroup_threshold(). |
| 3327 | */ |
| 3328 | i = atomic_read(&t->current_threshold); |
| 3329 | |
| 3330 | /* |
| 3331 | * Iterate backward over array of thresholds starting from |
| 3332 | * current_threshold and check if a threshold is crossed. |
| 3333 | * If none of thresholds below usage is crossed, we read |
| 3334 | * only one element of the array here. |
| 3335 | */ |
| 3336 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) |
| 3337 | eventfd_signal(t->entries[i].eventfd, 1); |
| 3338 | |
| 3339 | /* i = current_threshold + 1 */ |
| 3340 | i++; |
| 3341 | |
| 3342 | /* |
| 3343 | * Iterate forward over array of thresholds starting from |
| 3344 | * current_threshold+1 and check if a threshold is crossed. |
| 3345 | * If none of thresholds above usage is crossed, we read |
| 3346 | * only one element of the array here. |
| 3347 | */ |
| 3348 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) |
| 3349 | eventfd_signal(t->entries[i].eventfd, 1); |
| 3350 | |
| 3351 | /* Update current_threshold */ |
| 3352 | atomic_set(&t->current_threshold, i - 1); |
| 3353 | unlock: |
| 3354 | rcu_read_unlock(); |
| 3355 | } |
| 3356 | |
| 3357 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) |
| 3358 | { |
| 3359 | __mem_cgroup_threshold(memcg, false); |
| 3360 | if (do_swap_account) |
| 3361 | __mem_cgroup_threshold(memcg, true); |
| 3362 | } |
| 3363 | |
| 3364 | static int compare_thresholds(const void *a, const void *b) |
| 3365 | { |
| 3366 | const struct mem_cgroup_threshold *_a = a; |
| 3367 | const struct mem_cgroup_threshold *_b = b; |
| 3368 | |
| 3369 | return _a->threshold - _b->threshold; |
| 3370 | } |
| 3371 | |
| 3372 | static int mem_cgroup_register_event(struct cgroup *cgrp, struct cftype *cft, |
| 3373 | struct eventfd_ctx *eventfd, const char *args) |
| 3374 | { |
| 3375 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 3376 | struct mem_cgroup_threshold_ary *thresholds, *thresholds_new; |
| 3377 | int type = MEMFILE_TYPE(cft->private); |
| 3378 | u64 threshold, usage; |
| 3379 | int size; |
| 3380 | int i, ret; |
| 3381 | |
| 3382 | ret = res_counter_memparse_write_strategy(args, &threshold); |
| 3383 | if (ret) |
| 3384 | return ret; |
| 3385 | |
| 3386 | mutex_lock(&memcg->thresholds_lock); |
| 3387 | if (type == _MEM) |
| 3388 | thresholds = memcg->thresholds; |
| 3389 | else if (type == _MEMSWAP) |
| 3390 | thresholds = memcg->memsw_thresholds; |
| 3391 | else |
| 3392 | BUG(); |
| 3393 | |
| 3394 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 3395 | |
| 3396 | /* Check if a threshold crossed before adding a new one */ |
| 3397 | if (thresholds) |
| 3398 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 3399 | |
| 3400 | if (thresholds) |
| 3401 | size = thresholds->size + 1; |
| 3402 | else |
| 3403 | size = 1; |
| 3404 | |
| 3405 | /* Allocate memory for new array of thresholds */ |
| 3406 | thresholds_new = kmalloc(sizeof(*thresholds_new) + |
| 3407 | size * sizeof(struct mem_cgroup_threshold), |
| 3408 | GFP_KERNEL); |
| 3409 | if (!thresholds_new) { |
| 3410 | ret = -ENOMEM; |
| 3411 | goto unlock; |
| 3412 | } |
| 3413 | thresholds_new->size = size; |
| 3414 | |
| 3415 | /* Copy thresholds (if any) to new array */ |
| 3416 | if (thresholds) |
| 3417 | memcpy(thresholds_new->entries, thresholds->entries, |
| 3418 | thresholds->size * |
| 3419 | sizeof(struct mem_cgroup_threshold)); |
| 3420 | /* Add new threshold */ |
| 3421 | thresholds_new->entries[size - 1].eventfd = eventfd; |
| 3422 | thresholds_new->entries[size - 1].threshold = threshold; |
| 3423 | |
| 3424 | /* Sort thresholds. Registering of new threshold isn't time-critical */ |
| 3425 | sort(thresholds_new->entries, size, |
| 3426 | sizeof(struct mem_cgroup_threshold), |
| 3427 | compare_thresholds, NULL); |
| 3428 | |
| 3429 | /* Find current threshold */ |
| 3430 | atomic_set(&thresholds_new->current_threshold, -1); |
| 3431 | for (i = 0; i < size; i++) { |
| 3432 | if (thresholds_new->entries[i].threshold < usage) { |
| 3433 | /* |
| 3434 | * thresholds_new->current_threshold will not be used |
| 3435 | * until rcu_assign_pointer(), so it's safe to increment |
| 3436 | * it here. |
| 3437 | */ |
| 3438 | atomic_inc(&thresholds_new->current_threshold); |
| 3439 | } |
| 3440 | } |
| 3441 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 3442 | if (type == _MEM) |
| 3443 | rcu_assign_pointer(memcg->thresholds, thresholds_new); |
| 3444 | else |
| 3445 | rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new); |
| 3446 | |
| 3447 | /* To be sure that nobody uses thresholds before freeing it */ |
| 3448 | synchronize_rcu(); |
| 3449 | |
| 3450 | kfree(thresholds); |
| 3451 | unlock: |
| 3452 | mutex_unlock(&memcg->thresholds_lock); |
| 3453 | |
| 3454 | return ret; |
| 3455 | } |
| 3456 | |
| 3457 | static int mem_cgroup_unregister_event(struct cgroup *cgrp, struct cftype *cft, |
| 3458 | struct eventfd_ctx *eventfd) |
| 3459 | { |
| 3460 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 3461 | struct mem_cgroup_threshold_ary *thresholds, *thresholds_new; |
| 3462 | int type = MEMFILE_TYPE(cft->private); |
| 3463 | u64 usage; |
| 3464 | int size = 0; |
| 3465 | int i, j, ret; |
| 3466 | |
| 3467 | mutex_lock(&memcg->thresholds_lock); |
| 3468 | if (type == _MEM) |
| 3469 | thresholds = memcg->thresholds; |
| 3470 | else if (type == _MEMSWAP) |
| 3471 | thresholds = memcg->memsw_thresholds; |
| 3472 | else |
| 3473 | BUG(); |
| 3474 | |
| 3475 | /* |
| 3476 | * Something went wrong if we trying to unregister a threshold |
| 3477 | * if we don't have thresholds |
| 3478 | */ |
| 3479 | BUG_ON(!thresholds); |
| 3480 | |
| 3481 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 3482 | |
| 3483 | /* Check if a threshold crossed before removing */ |
| 3484 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 3485 | |
| 3486 | /* Calculate new number of threshold */ |
| 3487 | for (i = 0; i < thresholds->size; i++) { |
| 3488 | if (thresholds->entries[i].eventfd != eventfd) |
| 3489 | size++; |
| 3490 | } |
| 3491 | |
| 3492 | /* Set thresholds array to NULL if we don't have thresholds */ |
| 3493 | if (!size) { |
| 3494 | thresholds_new = NULL; |
| 3495 | goto assign; |
| 3496 | } |
| 3497 | |
| 3498 | /* Allocate memory for new array of thresholds */ |
| 3499 | thresholds_new = kmalloc(sizeof(*thresholds_new) + |
| 3500 | size * sizeof(struct mem_cgroup_threshold), |
| 3501 | GFP_KERNEL); |
| 3502 | if (!thresholds_new) { |
| 3503 | ret = -ENOMEM; |
| 3504 | goto unlock; |
| 3505 | } |
| 3506 | thresholds_new->size = size; |
| 3507 | |
| 3508 | /* Copy thresholds and find current threshold */ |
| 3509 | atomic_set(&thresholds_new->current_threshold, -1); |
| 3510 | for (i = 0, j = 0; i < thresholds->size; i++) { |
| 3511 | if (thresholds->entries[i].eventfd == eventfd) |
| 3512 | continue; |
| 3513 | |
| 3514 | thresholds_new->entries[j] = thresholds->entries[i]; |
| 3515 | if (thresholds_new->entries[j].threshold < usage) { |
| 3516 | /* |
| 3517 | * thresholds_new->current_threshold will not be used |
| 3518 | * until rcu_assign_pointer(), so it's safe to increment |
| 3519 | * it here. |
| 3520 | */ |
| 3521 | atomic_inc(&thresholds_new->current_threshold); |
| 3522 | } |
| 3523 | j++; |
| 3524 | } |
| 3525 | |
| 3526 | assign: |
| 3527 | if (type == _MEM) |
| 3528 | rcu_assign_pointer(memcg->thresholds, thresholds_new); |
| 3529 | else |
| 3530 | rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new); |
| 3531 | |
| 3532 | /* To be sure that nobody uses thresholds before freeing it */ |
| 3533 | synchronize_rcu(); |
| 3534 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 3535 | kfree(thresholds); |
| 3536 | unlock: |
| 3537 | mutex_unlock(&memcg->thresholds_lock); |
| 3538 | |
| 3539 | return ret; |
| 3540 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3541 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3542 | static struct cftype mem_cgroup_files[] = { |
| 3543 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 3544 | .name = "usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3545 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 3546 | .read_u64 = mem_cgroup_read, |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 3547 | .register_event = mem_cgroup_register_event, |
| 3548 | .unregister_event = mem_cgroup_unregister_event, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3549 | }, |
| 3550 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 3551 | .name = "max_usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3552 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 3553 | .trigger = mem_cgroup_reset, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 3554 | .read_u64 = mem_cgroup_read, |
| 3555 | }, |
| 3556 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 3557 | .name = "limit_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3558 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 3559 | .write_string = mem_cgroup_write, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 3560 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3561 | }, |
| 3562 | { |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 3563 | .name = "soft_limit_in_bytes", |
| 3564 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), |
| 3565 | .write_string = mem_cgroup_write, |
| 3566 | .read_u64 = mem_cgroup_read, |
| 3567 | }, |
| 3568 | { |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3569 | .name = "failcnt", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3570 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 3571 | .trigger = mem_cgroup_reset, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 3572 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3573 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 3574 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 3575 | .name = "stat", |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 3576 | .read_map = mem_control_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 3577 | }, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3578 | { |
| 3579 | .name = "force_empty", |
| 3580 | .trigger = mem_cgroup_force_empty_write, |
| 3581 | }, |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3582 | { |
| 3583 | .name = "use_hierarchy", |
| 3584 | .write_u64 = mem_cgroup_hierarchy_write, |
| 3585 | .read_u64 = mem_cgroup_hierarchy_read, |
| 3586 | }, |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 3587 | { |
| 3588 | .name = "swappiness", |
| 3589 | .read_u64 = mem_cgroup_swappiness_read, |
| 3590 | .write_u64 = mem_cgroup_swappiness_write, |
| 3591 | }, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 3592 | { |
| 3593 | .name = "move_charge_at_immigrate", |
| 3594 | .read_u64 = mem_cgroup_move_charge_read, |
| 3595 | .write_u64 = mem_cgroup_move_charge_write, |
| 3596 | }, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3597 | }; |
| 3598 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3599 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 3600 | static struct cftype memsw_cgroup_files[] = { |
| 3601 | { |
| 3602 | .name = "memsw.usage_in_bytes", |
| 3603 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), |
| 3604 | .read_u64 = mem_cgroup_read, |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 3605 | .register_event = mem_cgroup_register_event, |
| 3606 | .unregister_event = mem_cgroup_unregister_event, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3607 | }, |
| 3608 | { |
| 3609 | .name = "memsw.max_usage_in_bytes", |
| 3610 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), |
| 3611 | .trigger = mem_cgroup_reset, |
| 3612 | .read_u64 = mem_cgroup_read, |
| 3613 | }, |
| 3614 | { |
| 3615 | .name = "memsw.limit_in_bytes", |
| 3616 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), |
| 3617 | .write_string = mem_cgroup_write, |
| 3618 | .read_u64 = mem_cgroup_read, |
| 3619 | }, |
| 3620 | { |
| 3621 | .name = "memsw.failcnt", |
| 3622 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), |
| 3623 | .trigger = mem_cgroup_reset, |
| 3624 | .read_u64 = mem_cgroup_read, |
| 3625 | }, |
| 3626 | }; |
| 3627 | |
| 3628 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 3629 | { |
| 3630 | if (!do_swap_account) |
| 3631 | return 0; |
| 3632 | return cgroup_add_files(cont, ss, memsw_cgroup_files, |
| 3633 | ARRAY_SIZE(memsw_cgroup_files)); |
| 3634 | }; |
| 3635 | #else |
| 3636 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 3637 | { |
| 3638 | return 0; |
| 3639 | } |
| 3640 | #endif |
| 3641 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 3642 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 3643 | { |
| 3644 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 3645 | struct mem_cgroup_per_zone *mz; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3646 | enum lru_list l; |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 3647 | int zone, tmp = node; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 3648 | /* |
| 3649 | * This routine is called against possible nodes. |
| 3650 | * But it's BUG to call kmalloc() against offline node. |
| 3651 | * |
| 3652 | * TODO: this routine can waste much memory for nodes which will |
| 3653 | * never be onlined. It's better to use memory hotplug callback |
| 3654 | * function. |
| 3655 | */ |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 3656 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 3657 | tmp = -1; |
| 3658 | pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 3659 | if (!pn) |
| 3660 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 3661 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 3662 | mem->info.nodeinfo[node] = pn; |
| 3663 | memset(pn, 0, sizeof(*pn)); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 3664 | |
| 3665 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 3666 | mz = &pn->zoneinfo[zone]; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3667 | for_each_lru(l) |
| 3668 | INIT_LIST_HEAD(&mz->lists[l]); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 3669 | mz->usage_in_excess = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3670 | mz->on_tree = false; |
| 3671 | mz->mem = mem; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 3672 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 3673 | return 0; |
| 3674 | } |
| 3675 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 3676 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 3677 | { |
| 3678 | kfree(mem->info.nodeinfo[node]); |
| 3679 | } |
| 3680 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 3681 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 3682 | { |
| 3683 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 3684 | int size = sizeof(struct mem_cgroup); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 3685 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 3686 | /* Can be very big if MAX_NUMNODES is very big */ |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3687 | if (size < PAGE_SIZE) |
| 3688 | mem = kmalloc(size, GFP_KERNEL); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 3689 | else |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3690 | mem = vmalloc(size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 3691 | |
Dan Carpenter | e7bbcdf | 2010-03-23 13:35:12 -0700 | [diff] [blame] | 3692 | if (!mem) |
| 3693 | return NULL; |
| 3694 | |
| 3695 | memset(mem, 0, size); |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 3696 | mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu); |
| 3697 | if (!mem->stat) { |
| 3698 | if (size < PAGE_SIZE) |
| 3699 | kfree(mem); |
| 3700 | else |
| 3701 | vfree(mem); |
| 3702 | mem = NULL; |
| 3703 | } |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 3704 | return mem; |
| 3705 | } |
| 3706 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3707 | /* |
| 3708 | * At destroying mem_cgroup, references from swap_cgroup can remain. |
| 3709 | * (scanning all at force_empty is too costly...) |
| 3710 | * |
| 3711 | * Instead of clearing all references at force_empty, we remember |
| 3712 | * the number of reference from swap_cgroup and free mem_cgroup when |
| 3713 | * it goes down to 0. |
| 3714 | * |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3715 | * Removal of cgroup itself succeeds regardless of refs from swap. |
| 3716 | */ |
| 3717 | |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 3718 | static void __mem_cgroup_free(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 3719 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3720 | int node; |
| 3721 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 3722 | mem_cgroup_remove_from_trees(mem); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 3723 | free_css_id(&mem_cgroup_subsys, &mem->css); |
| 3724 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3725 | for_each_node_state(node, N_POSSIBLE) |
| 3726 | free_mem_cgroup_per_zone_info(mem, node); |
| 3727 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 3728 | free_percpu(mem->stat); |
| 3729 | if (sizeof(struct mem_cgroup) < PAGE_SIZE) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 3730 | kfree(mem); |
| 3731 | else |
| 3732 | vfree(mem); |
| 3733 | } |
| 3734 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3735 | static void mem_cgroup_get(struct mem_cgroup *mem) |
| 3736 | { |
| 3737 | atomic_inc(&mem->refcnt); |
| 3738 | } |
| 3739 | |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3740 | static void __mem_cgroup_put(struct mem_cgroup *mem, int count) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3741 | { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3742 | if (atomic_sub_and_test(count, &mem->refcnt)) { |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 3743 | struct mem_cgroup *parent = parent_mem_cgroup(mem); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 3744 | __mem_cgroup_free(mem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 3745 | if (parent) |
| 3746 | mem_cgroup_put(parent); |
| 3747 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3748 | } |
| 3749 | |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3750 | static void mem_cgroup_put(struct mem_cgroup *mem) |
| 3751 | { |
| 3752 | __mem_cgroup_put(mem, 1); |
| 3753 | } |
| 3754 | |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 3755 | /* |
| 3756 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. |
| 3757 | */ |
| 3758 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem) |
| 3759 | { |
| 3760 | if (!mem->res.parent) |
| 3761 | return NULL; |
| 3762 | return mem_cgroup_from_res_counter(mem->res.parent, res); |
| 3763 | } |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 3764 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 3765 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 3766 | static void __init enable_swap_cgroup(void) |
| 3767 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3768 | if (!mem_cgroup_disabled() && really_do_swap_account) |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 3769 | do_swap_account = 1; |
| 3770 | } |
| 3771 | #else |
| 3772 | static void __init enable_swap_cgroup(void) |
| 3773 | { |
| 3774 | } |
| 3775 | #endif |
| 3776 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 3777 | static int mem_cgroup_soft_limit_tree_init(void) |
| 3778 | { |
| 3779 | struct mem_cgroup_tree_per_node *rtpn; |
| 3780 | struct mem_cgroup_tree_per_zone *rtpz; |
| 3781 | int tmp, node, zone; |
| 3782 | |
| 3783 | for_each_node_state(node, N_POSSIBLE) { |
| 3784 | tmp = node; |
| 3785 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 3786 | tmp = -1; |
| 3787 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp); |
| 3788 | if (!rtpn) |
| 3789 | return 1; |
| 3790 | |
| 3791 | soft_limit_tree.rb_tree_per_node[node] = rtpn; |
| 3792 | |
| 3793 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 3794 | rtpz = &rtpn->rb_tree_per_zone[zone]; |
| 3795 | rtpz->rb_root = RB_ROOT; |
| 3796 | spin_lock_init(&rtpz->lock); |
| 3797 | } |
| 3798 | } |
| 3799 | return 0; |
| 3800 | } |
| 3801 | |
Li Zefan | 0eb253e | 2009-01-15 13:51:25 -0800 | [diff] [blame] | 3802 | static struct cgroup_subsys_state * __ref |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3803 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 3804 | { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 3805 | struct mem_cgroup *mem, *parent; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 3806 | long error = -ENOMEM; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 3807 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3808 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3809 | mem = mem_cgroup_alloc(); |
| 3810 | if (!mem) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 3811 | return ERR_PTR(error); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 3812 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 3813 | for_each_node_state(node, N_POSSIBLE) |
| 3814 | if (alloc_mem_cgroup_per_zone_info(mem, node)) |
| 3815 | goto free_out; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 3816 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 3817 | /* root ? */ |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 3818 | if (cont->parent == NULL) { |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 3819 | int cpu; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 3820 | enable_swap_cgroup(); |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 3821 | parent = NULL; |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 3822 | root_mem_cgroup = mem; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 3823 | if (mem_cgroup_soft_limit_tree_init()) |
| 3824 | goto free_out; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 3825 | for_each_possible_cpu(cpu) { |
| 3826 | struct memcg_stock_pcp *stock = |
| 3827 | &per_cpu(memcg_stock, cpu); |
| 3828 | INIT_WORK(&stock->work, drain_local_stock); |
| 3829 | } |
| 3830 | hotcpu_notifier(memcg_stock_cpu_callback, 0); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3831 | } else { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 3832 | parent = mem_cgroup_from_cont(cont->parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3833 | mem->use_hierarchy = parent->use_hierarchy; |
| 3834 | } |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 3835 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3836 | if (parent && parent->use_hierarchy) { |
| 3837 | res_counter_init(&mem->res, &parent->res); |
| 3838 | res_counter_init(&mem->memsw, &parent->memsw); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 3839 | /* |
| 3840 | * We increment refcnt of the parent to ensure that we can |
| 3841 | * safely access it on res_counter_charge/uncharge. |
| 3842 | * This refcnt will be decremented when freeing this |
| 3843 | * mem_cgroup(see mem_cgroup_put). |
| 3844 | */ |
| 3845 | mem_cgroup_get(parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3846 | } else { |
| 3847 | res_counter_init(&mem->res, NULL); |
| 3848 | res_counter_init(&mem->memsw, NULL); |
| 3849 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 3850 | mem->last_scanned_child = 0; |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 3851 | spin_lock_init(&mem->reclaim_param_lock); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 3852 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 3853 | if (parent) |
| 3854 | mem->swappiness = get_swappiness(parent); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 3855 | atomic_set(&mem->refcnt, 1); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 3856 | mem->move_charge_at_immigrate = 0; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 3857 | mutex_init(&mem->thresholds_lock); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3858 | return &mem->css; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 3859 | free_out: |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 3860 | __mem_cgroup_free(mem); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 3861 | root_mem_cgroup = NULL; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 3862 | return ERR_PTR(error); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3863 | } |
| 3864 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 3865 | static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 3866 | struct cgroup *cont) |
| 3867 | { |
| 3868 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 3869 | |
| 3870 | return mem_cgroup_force_empty(mem, false); |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 3871 | } |
| 3872 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3873 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 3874 | struct cgroup *cont) |
| 3875 | { |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 3876 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 3877 | |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 3878 | mem_cgroup_put(mem); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3879 | } |
| 3880 | |
| 3881 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 3882 | struct cgroup *cont) |
| 3883 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3884 | int ret; |
| 3885 | |
| 3886 | ret = cgroup_add_files(cont, ss, mem_cgroup_files, |
| 3887 | ARRAY_SIZE(mem_cgroup_files)); |
| 3888 | |
| 3889 | if (!ret) |
| 3890 | ret = register_memsw_files(cont, ss); |
| 3891 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3892 | } |
| 3893 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3894 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 3895 | /* Handlers for move charge at task migration. */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 3896 | #define PRECHARGE_COUNT_AT_ONCE 256 |
| 3897 | static int mem_cgroup_do_precharge(unsigned long count) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 3898 | { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 3899 | int ret = 0; |
| 3900 | int batch_count = PRECHARGE_COUNT_AT_ONCE; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 3901 | struct mem_cgroup *mem = mc.to; |
| 3902 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 3903 | if (mem_cgroup_is_root(mem)) { |
| 3904 | mc.precharge += count; |
| 3905 | /* we don't need css_get for root */ |
| 3906 | return ret; |
| 3907 | } |
| 3908 | /* try to charge at once */ |
| 3909 | if (count > 1) { |
| 3910 | struct res_counter *dummy; |
| 3911 | /* |
| 3912 | * "mem" cannot be under rmdir() because we've already checked |
| 3913 | * by cgroup_lock_live_cgroup() that it is not removed and we |
| 3914 | * are still under the same cgroup_mutex. So we can postpone |
| 3915 | * css_get(). |
| 3916 | */ |
| 3917 | if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy)) |
| 3918 | goto one_by_one; |
| 3919 | if (do_swap_account && res_counter_charge(&mem->memsw, |
| 3920 | PAGE_SIZE * count, &dummy)) { |
| 3921 | res_counter_uncharge(&mem->res, PAGE_SIZE * count); |
| 3922 | goto one_by_one; |
| 3923 | } |
| 3924 | mc.precharge += count; |
| 3925 | VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags)); |
| 3926 | WARN_ON_ONCE(count > INT_MAX); |
| 3927 | __css_get(&mem->css, (int)count); |
| 3928 | return ret; |
| 3929 | } |
| 3930 | one_by_one: |
| 3931 | /* fall back to one by one charge */ |
| 3932 | while (count--) { |
| 3933 | if (signal_pending(current)) { |
| 3934 | ret = -EINTR; |
| 3935 | break; |
| 3936 | } |
| 3937 | if (!batch_count--) { |
| 3938 | batch_count = PRECHARGE_COUNT_AT_ONCE; |
| 3939 | cond_resched(); |
| 3940 | } |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 3941 | ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 3942 | if (ret || !mem) |
| 3943 | /* mem_cgroup_clear_mc() will do uncharge later */ |
| 3944 | return -ENOMEM; |
| 3945 | mc.precharge++; |
| 3946 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 3947 | return ret; |
| 3948 | } |
| 3949 | |
| 3950 | /** |
| 3951 | * is_target_pte_for_mc - check a pte whether it is valid for move charge |
| 3952 | * @vma: the vma the pte to be checked belongs |
| 3953 | * @addr: the address corresponding to the pte to be checked |
| 3954 | * @ptent: the pte to be checked |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3955 | * @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] | 3956 | * |
| 3957 | * Returns |
| 3958 | * 0(MC_TARGET_NONE): if the pte is not a target for move charge. |
| 3959 | * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for |
| 3960 | * move charge. if @target is not NULL, the page is stored in target->page |
| 3961 | * with extra refcnt got(Callers should handle it). |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3962 | * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a |
| 3963 | * target for charge migration. if @target is not NULL, the entry is stored |
| 3964 | * in target->ent. |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 3965 | * |
| 3966 | * Called with pte lock held. |
| 3967 | */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 3968 | union mc_target { |
| 3969 | struct page *page; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3970 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 3971 | }; |
| 3972 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 3973 | enum mc_target_type { |
| 3974 | MC_TARGET_NONE, /* not used */ |
| 3975 | MC_TARGET_PAGE, |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3976 | MC_TARGET_SWAP, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 3977 | }; |
| 3978 | |
| 3979 | static int is_target_pte_for_mc(struct vm_area_struct *vma, |
| 3980 | unsigned long addr, pte_t ptent, union mc_target *target) |
| 3981 | { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3982 | struct page *page = NULL; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 3983 | struct page_cgroup *pc; |
| 3984 | int ret = 0; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3985 | swp_entry_t ent = { .val = 0 }; |
| 3986 | int usage_count = 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 3987 | bool move_anon = test_bit(MOVE_CHARGE_TYPE_ANON, |
| 3988 | &mc.to->move_charge_at_immigrate); |
| 3989 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3990 | if (!pte_present(ptent)) { |
| 3991 | /* TODO: handle swap of shmes/tmpfs */ |
| 3992 | if (pte_none(ptent) || pte_file(ptent)) |
| 3993 | return 0; |
| 3994 | else if (is_swap_pte(ptent)) { |
| 3995 | ent = pte_to_swp_entry(ptent); |
| 3996 | if (!move_anon || non_swap_entry(ent)) |
| 3997 | return 0; |
| 3998 | usage_count = mem_cgroup_count_swap_user(ent, &page); |
| 3999 | } |
| 4000 | } else { |
| 4001 | page = vm_normal_page(vma, addr, ptent); |
| 4002 | if (!page || !page_mapped(page)) |
| 4003 | return 0; |
| 4004 | /* |
| 4005 | * TODO: We don't move charges of file(including shmem/tmpfs) |
| 4006 | * pages for now. |
| 4007 | */ |
| 4008 | if (!move_anon || !PageAnon(page)) |
| 4009 | return 0; |
| 4010 | if (!get_page_unless_zero(page)) |
| 4011 | return 0; |
| 4012 | usage_count = page_mapcount(page); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4013 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4014 | if (usage_count > 1) { |
| 4015 | /* |
| 4016 | * TODO: We don't move charges of shared(used by multiple |
| 4017 | * processes) pages for now. |
| 4018 | */ |
| 4019 | if (page) |
| 4020 | put_page(page); |
| 4021 | return 0; |
| 4022 | } |
| 4023 | if (page) { |
| 4024 | pc = lookup_page_cgroup(page); |
| 4025 | /* |
| 4026 | * Do only loose check w/o page_cgroup lock. |
| 4027 | * mem_cgroup_move_account() checks the pc is valid or not under |
| 4028 | * the lock. |
| 4029 | */ |
| 4030 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { |
| 4031 | ret = MC_TARGET_PAGE; |
| 4032 | if (target) |
| 4033 | target->page = page; |
| 4034 | } |
| 4035 | if (!ret || !target) |
| 4036 | put_page(page); |
| 4037 | } |
| 4038 | /* throught */ |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 4039 | if (ent.val && do_swap_account && !ret && |
| 4040 | css_id(&mc.from->css) == lookup_swap_cgroup(ent)) { |
| 4041 | ret = MC_TARGET_SWAP; |
| 4042 | if (target) |
| 4043 | target->ent = ent; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4044 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4045 | return ret; |
| 4046 | } |
| 4047 | |
| 4048 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, |
| 4049 | unsigned long addr, unsigned long end, |
| 4050 | struct mm_walk *walk) |
| 4051 | { |
| 4052 | struct vm_area_struct *vma = walk->private; |
| 4053 | pte_t *pte; |
| 4054 | spinlock_t *ptl; |
| 4055 | |
| 4056 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 4057 | for (; addr != end; pte++, addr += PAGE_SIZE) |
| 4058 | if (is_target_pte_for_mc(vma, addr, *pte, NULL)) |
| 4059 | mc.precharge++; /* increment precharge temporarily */ |
| 4060 | pte_unmap_unlock(pte - 1, ptl); |
| 4061 | cond_resched(); |
| 4062 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4063 | return 0; |
| 4064 | } |
| 4065 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4066 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
| 4067 | { |
| 4068 | unsigned long precharge; |
| 4069 | struct vm_area_struct *vma; |
| 4070 | |
| 4071 | down_read(&mm->mmap_sem); |
| 4072 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 4073 | struct mm_walk mem_cgroup_count_precharge_walk = { |
| 4074 | .pmd_entry = mem_cgroup_count_precharge_pte_range, |
| 4075 | .mm = mm, |
| 4076 | .private = vma, |
| 4077 | }; |
| 4078 | if (is_vm_hugetlb_page(vma)) |
| 4079 | continue; |
| 4080 | /* TODO: We don't move charges of shmem/tmpfs pages for now. */ |
| 4081 | if (vma->vm_flags & VM_SHARED) |
| 4082 | continue; |
| 4083 | walk_page_range(vma->vm_start, vma->vm_end, |
| 4084 | &mem_cgroup_count_precharge_walk); |
| 4085 | } |
| 4086 | up_read(&mm->mmap_sem); |
| 4087 | |
| 4088 | precharge = mc.precharge; |
| 4089 | mc.precharge = 0; |
| 4090 | |
| 4091 | return precharge; |
| 4092 | } |
| 4093 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4094 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
| 4095 | { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 4096 | return mem_cgroup_do_precharge(mem_cgroup_count_precharge(mm)); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4097 | } |
| 4098 | |
| 4099 | static void mem_cgroup_clear_mc(void) |
| 4100 | { |
| 4101 | /* we must uncharge all the leftover precharges from mc.to */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 4102 | if (mc.precharge) { |
| 4103 | __mem_cgroup_cancel_charge(mc.to, mc.precharge); |
| 4104 | mc.precharge = 0; |
| 4105 | } |
| 4106 | /* |
| 4107 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so |
| 4108 | * we must uncharge here. |
| 4109 | */ |
| 4110 | if (mc.moved_charge) { |
| 4111 | __mem_cgroup_cancel_charge(mc.from, mc.moved_charge); |
| 4112 | mc.moved_charge = 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4113 | } |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4114 | /* we must fixup refcnts and charges */ |
| 4115 | if (mc.moved_swap) { |
| 4116 | WARN_ON_ONCE(mc.moved_swap > INT_MAX); |
| 4117 | /* uncharge swap account from the old cgroup */ |
| 4118 | if (!mem_cgroup_is_root(mc.from)) |
| 4119 | res_counter_uncharge(&mc.from->memsw, |
| 4120 | PAGE_SIZE * mc.moved_swap); |
| 4121 | __mem_cgroup_put(mc.from, mc.moved_swap); |
| 4122 | |
| 4123 | if (!mem_cgroup_is_root(mc.to)) { |
| 4124 | /* |
| 4125 | * we charged both to->res and to->memsw, so we should |
| 4126 | * uncharge to->res. |
| 4127 | */ |
| 4128 | res_counter_uncharge(&mc.to->res, |
| 4129 | PAGE_SIZE * mc.moved_swap); |
| 4130 | VM_BUG_ON(test_bit(CSS_ROOT, &mc.to->css.flags)); |
| 4131 | __css_put(&mc.to->css, mc.moved_swap); |
| 4132 | } |
| 4133 | /* we've already done mem_cgroup_get(mc.to) */ |
| 4134 | |
| 4135 | mc.moved_swap = 0; |
| 4136 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4137 | mc.from = NULL; |
| 4138 | mc.to = NULL; |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 4139 | mc.moving_task = NULL; |
| 4140 | wake_up_all(&mc.waitq); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4141 | } |
| 4142 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4143 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 4144 | struct cgroup *cgroup, |
| 4145 | struct task_struct *p, |
| 4146 | bool threadgroup) |
| 4147 | { |
| 4148 | int ret = 0; |
| 4149 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup); |
| 4150 | |
| 4151 | if (mem->move_charge_at_immigrate) { |
| 4152 | struct mm_struct *mm; |
| 4153 | struct mem_cgroup *from = mem_cgroup_from_task(p); |
| 4154 | |
| 4155 | VM_BUG_ON(from == mem); |
| 4156 | |
| 4157 | mm = get_task_mm(p); |
| 4158 | if (!mm) |
| 4159 | return 0; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4160 | /* We move charges only when we move a owner of the mm */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4161 | if (mm->owner == p) { |
| 4162 | VM_BUG_ON(mc.from); |
| 4163 | VM_BUG_ON(mc.to); |
| 4164 | VM_BUG_ON(mc.precharge); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 4165 | VM_BUG_ON(mc.moved_charge); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4166 | VM_BUG_ON(mc.moved_swap); |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 4167 | VM_BUG_ON(mc.moving_task); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4168 | mc.from = from; |
| 4169 | mc.to = mem; |
| 4170 | mc.precharge = 0; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 4171 | mc.moved_charge = 0; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4172 | mc.moved_swap = 0; |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 4173 | mc.moving_task = current; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4174 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4175 | ret = mem_cgroup_precharge_mc(mm); |
| 4176 | if (ret) |
| 4177 | mem_cgroup_clear_mc(); |
| 4178 | } |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4179 | mmput(mm); |
| 4180 | } |
| 4181 | return ret; |
| 4182 | } |
| 4183 | |
| 4184 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 4185 | struct cgroup *cgroup, |
| 4186 | struct task_struct *p, |
| 4187 | bool threadgroup) |
| 4188 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4189 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4190 | } |
| 4191 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4192 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
| 4193 | unsigned long addr, unsigned long end, |
| 4194 | struct mm_walk *walk) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4195 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4196 | int ret = 0; |
| 4197 | struct vm_area_struct *vma = walk->private; |
| 4198 | pte_t *pte; |
| 4199 | spinlock_t *ptl; |
| 4200 | |
| 4201 | retry: |
| 4202 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 4203 | for (; addr != end; addr += PAGE_SIZE) { |
| 4204 | pte_t ptent = *(pte++); |
| 4205 | union mc_target target; |
| 4206 | int type; |
| 4207 | struct page *page; |
| 4208 | struct page_cgroup *pc; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4209 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4210 | |
| 4211 | if (!mc.precharge) |
| 4212 | break; |
| 4213 | |
| 4214 | type = is_target_pte_for_mc(vma, addr, ptent, &target); |
| 4215 | switch (type) { |
| 4216 | case MC_TARGET_PAGE: |
| 4217 | page = target.page; |
| 4218 | if (isolate_lru_page(page)) |
| 4219 | goto put; |
| 4220 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 4221 | if (!mem_cgroup_move_account(pc, |
| 4222 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4223 | mc.precharge--; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 4224 | /* we uncharge from mc.from later. */ |
| 4225 | mc.moved_charge++; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4226 | } |
| 4227 | putback_lru_page(page); |
| 4228 | put: /* is_target_pte_for_mc() gets the page */ |
| 4229 | put_page(page); |
| 4230 | break; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4231 | case MC_TARGET_SWAP: |
| 4232 | ent = target.ent; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4233 | if (!mem_cgroup_move_swap_account(ent, |
| 4234 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4235 | mc.precharge--; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4236 | /* we fixup refcnts and charges later. */ |
| 4237 | mc.moved_swap++; |
| 4238 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4239 | break; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4240 | default: |
| 4241 | break; |
| 4242 | } |
| 4243 | } |
| 4244 | pte_unmap_unlock(pte - 1, ptl); |
| 4245 | cond_resched(); |
| 4246 | |
| 4247 | if (addr != end) { |
| 4248 | /* |
| 4249 | * We have consumed all precharges we got in can_attach(). |
| 4250 | * We try charge one by one, but don't do any additional |
| 4251 | * charges to mc.to if we have failed in charge once in attach() |
| 4252 | * phase. |
| 4253 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 4254 | ret = mem_cgroup_do_precharge(1); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4255 | if (!ret) |
| 4256 | goto retry; |
| 4257 | } |
| 4258 | |
| 4259 | return ret; |
| 4260 | } |
| 4261 | |
| 4262 | static void mem_cgroup_move_charge(struct mm_struct *mm) |
| 4263 | { |
| 4264 | struct vm_area_struct *vma; |
| 4265 | |
| 4266 | lru_add_drain_all(); |
| 4267 | down_read(&mm->mmap_sem); |
| 4268 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 4269 | int ret; |
| 4270 | struct mm_walk mem_cgroup_move_charge_walk = { |
| 4271 | .pmd_entry = mem_cgroup_move_charge_pte_range, |
| 4272 | .mm = mm, |
| 4273 | .private = vma, |
| 4274 | }; |
| 4275 | if (is_vm_hugetlb_page(vma)) |
| 4276 | continue; |
| 4277 | /* TODO: We don't move charges of shmem/tmpfs pages for now. */ |
| 4278 | if (vma->vm_flags & VM_SHARED) |
| 4279 | continue; |
| 4280 | ret = walk_page_range(vma->vm_start, vma->vm_end, |
| 4281 | &mem_cgroup_move_charge_walk); |
| 4282 | if (ret) |
| 4283 | /* |
| 4284 | * means we have consumed all precharges and failed in |
| 4285 | * doing additional charge. Just abandon here. |
| 4286 | */ |
| 4287 | break; |
| 4288 | } |
| 4289 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4290 | } |
| 4291 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 4292 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 4293 | struct cgroup *cont, |
| 4294 | struct cgroup *old_cont, |
Ben Blum | be367d0 | 2009-09-23 15:56:31 -0700 | [diff] [blame] | 4295 | struct task_struct *p, |
| 4296 | bool threadgroup) |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 4297 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 4298 | struct mm_struct *mm; |
| 4299 | |
| 4300 | if (!mc.to) |
| 4301 | /* no need to move charge */ |
| 4302 | return; |
| 4303 | |
| 4304 | mm = get_task_mm(p); |
| 4305 | if (mm) { |
| 4306 | mem_cgroup_move_charge(mm); |
| 4307 | mmput(mm); |
| 4308 | } |
| 4309 | mem_cgroup_clear_mc(); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 4310 | } |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 4311 | #else /* !CONFIG_MMU */ |
| 4312 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 4313 | struct cgroup *cgroup, |
| 4314 | struct task_struct *p, |
| 4315 | bool threadgroup) |
| 4316 | { |
| 4317 | return 0; |
| 4318 | } |
| 4319 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 4320 | struct cgroup *cgroup, |
| 4321 | struct task_struct *p, |
| 4322 | bool threadgroup) |
| 4323 | { |
| 4324 | } |
| 4325 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 4326 | struct cgroup *cont, |
| 4327 | struct cgroup *old_cont, |
| 4328 | struct task_struct *p, |
| 4329 | bool threadgroup) |
| 4330 | { |
| 4331 | } |
| 4332 | #endif |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 4333 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4334 | struct cgroup_subsys mem_cgroup_subsys = { |
| 4335 | .name = "memory", |
| 4336 | .subsys_id = mem_cgroup_subsys_id, |
| 4337 | .create = mem_cgroup_create, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 4338 | .pre_destroy = mem_cgroup_pre_destroy, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4339 | .destroy = mem_cgroup_destroy, |
| 4340 | .populate = mem_cgroup_populate, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4341 | .can_attach = mem_cgroup_can_attach, |
| 4342 | .cancel_attach = mem_cgroup_cancel_attach, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 4343 | .attach = mem_cgroup_move_task, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4344 | .early_init = 0, |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4345 | .use_id = 1, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4346 | }; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4347 | |
| 4348 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 4349 | |
| 4350 | static int __init disable_swap_account(char *s) |
| 4351 | { |
| 4352 | really_do_swap_account = 0; |
| 4353 | return 1; |
| 4354 | } |
| 4355 | __setup("noswapaccount", disable_swap_account); |
| 4356 | #endif |