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