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