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