Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1 | /* memcontrol.c - Memory Controller |
| 2 | * |
| 3 | * Copyright IBM Corporation, 2007 |
| 4 | * Author Balbir Singh <balbir@linux.vnet.ibm.com> |
| 5 | * |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 6 | * Copyright 2007 OpenVZ SWsoft Inc |
| 7 | * Author: Pavel Emelianov <xemul@openvz.org> |
| 8 | * |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 9 | * Memory thresholds |
| 10 | * Copyright (C) 2009 Nokia Corporation |
| 11 | * Author: Kirill A. Shutemov |
| 12 | * |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/res_counter.h> |
| 25 | #include <linux/memcontrol.h> |
| 26 | #include <linux/cgroup.h> |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 27 | #include <linux/mm.h> |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 28 | #include <linux/hugetlb.h> |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 29 | #include <linux/pagemap.h> |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 30 | #include <linux/smp.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 31 | #include <linux/page-flags.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 32 | #include <linux/backing-dev.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 33 | #include <linux/bit_spinlock.h> |
| 34 | #include <linux/rcupdate.h> |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 35 | #include <linux/limits.h> |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 36 | #include <linux/mutex.h> |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 37 | #include <linux/rbtree.h> |
Hugh Dickins | 072441e | 2011-06-27 16:18:02 -0700 | [diff] [blame] | 38 | #include <linux/shmem_fs.h> |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 39 | #include <linux/slab.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 40 | #include <linux/swap.h> |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 41 | #include <linux/swapops.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 42 | #include <linux/spinlock.h> |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 43 | #include <linux/eventfd.h> |
| 44 | #include <linux/sort.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 45 | #include <linux/fs.h> |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 46 | #include <linux/seq_file.h> |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 47 | #include <linux/vmalloc.h> |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 48 | #include <linux/mm_inline.h> |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 49 | #include <linux/page_cgroup.h> |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 50 | #include <linux/cpu.h> |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 51 | #include <linux/oom.h> |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 52 | #include "internal.h" |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 53 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 54 | #include <asm/uaccess.h> |
| 55 | |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 56 | #include <trace/events/vmscan.h> |
| 57 | |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 58 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 59 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 60 | struct mem_cgroup *root_mem_cgroup __read_mostly; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 61 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 62 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 63 | /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */ |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 64 | int do_swap_account __read_mostly; |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 65 | |
| 66 | /* for remember boot option*/ |
| 67 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP_ENABLED |
| 68 | static int really_do_swap_account __initdata = 1; |
| 69 | #else |
| 70 | static int really_do_swap_account __initdata = 0; |
| 71 | #endif |
| 72 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 73 | #else |
| 74 | #define do_swap_account (0) |
| 75 | #endif |
| 76 | |
| 77 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 78 | /* |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 79 | * Statistics for memory cgroup. |
| 80 | */ |
| 81 | enum mem_cgroup_stat_index { |
| 82 | /* |
| 83 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. |
| 84 | */ |
| 85 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 86 | MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */ |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 87 | MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 88 | MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */ |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 89 | MEM_CGROUP_STAT_DATA, /* end of data requires synchronization */ |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 90 | MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 91 | MEM_CGROUP_STAT_NSTATS, |
| 92 | }; |
| 93 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 94 | enum mem_cgroup_events_index { |
| 95 | MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */ |
| 96 | MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */ |
| 97 | MEM_CGROUP_EVENTS_COUNT, /* # of pages paged in/out */ |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 98 | MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */ |
| 99 | MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */ |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 100 | MEM_CGROUP_EVENTS_NSTATS, |
| 101 | }; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 102 | /* |
| 103 | * Per memcg event counter is incremented at every pagein/pageout. With THP, |
| 104 | * it will be incremated by the number of pages. This counter is used for |
| 105 | * for trigger some periodic events. This is straightforward and better |
| 106 | * than using jiffies etc. to handle periodic memcg event. |
| 107 | */ |
| 108 | enum mem_cgroup_events_target { |
| 109 | MEM_CGROUP_TARGET_THRESH, |
| 110 | MEM_CGROUP_TARGET_SOFTLIMIT, |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 111 | MEM_CGROUP_TARGET_NUMAINFO, |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 112 | MEM_CGROUP_NTARGETS, |
| 113 | }; |
| 114 | #define THRESHOLDS_EVENTS_TARGET (128) |
| 115 | #define SOFTLIMIT_EVENTS_TARGET (1024) |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 116 | #define NUMAINFO_EVENTS_TARGET (1024) |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 117 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 118 | struct mem_cgroup_stat_cpu { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 119 | long count[MEM_CGROUP_STAT_NSTATS]; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 120 | unsigned long events[MEM_CGROUP_EVENTS_NSTATS]; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 121 | unsigned long targets[MEM_CGROUP_NTARGETS]; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 122 | }; |
| 123 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 124 | /* |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 125 | * per-zone information in memory controller. |
| 126 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 127 | struct mem_cgroup_per_zone { |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 128 | /* |
| 129 | * spin_lock to protect the per cgroup LRU |
| 130 | */ |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 131 | struct list_head lists[NR_LRU_LISTS]; |
| 132 | unsigned long count[NR_LRU_LISTS]; |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 133 | |
| 134 | struct zone_reclaim_stat reclaim_stat; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 135 | struct rb_node tree_node; /* RB tree node */ |
| 136 | unsigned long long usage_in_excess;/* Set to the value by which */ |
| 137 | /* the soft limit is exceeded*/ |
| 138 | bool on_tree; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 139 | struct mem_cgroup *mem; /* Back pointer, we cannot */ |
| 140 | /* use container_of */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 141 | }; |
| 142 | /* Macro for accessing counter */ |
| 143 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) |
| 144 | |
| 145 | struct mem_cgroup_per_node { |
| 146 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; |
| 147 | }; |
| 148 | |
| 149 | struct mem_cgroup_lru_info { |
| 150 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; |
| 151 | }; |
| 152 | |
| 153 | /* |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 154 | * Cgroups above their limits are maintained in a RB-Tree, independent of |
| 155 | * their hierarchy representation |
| 156 | */ |
| 157 | |
| 158 | struct mem_cgroup_tree_per_zone { |
| 159 | struct rb_root rb_root; |
| 160 | spinlock_t lock; |
| 161 | }; |
| 162 | |
| 163 | struct mem_cgroup_tree_per_node { |
| 164 | struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES]; |
| 165 | }; |
| 166 | |
| 167 | struct mem_cgroup_tree { |
| 168 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; |
| 169 | }; |
| 170 | |
| 171 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; |
| 172 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 173 | struct mem_cgroup_threshold { |
| 174 | struct eventfd_ctx *eventfd; |
| 175 | u64 threshold; |
| 176 | }; |
| 177 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 178 | /* For threshold */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 179 | struct mem_cgroup_threshold_ary { |
| 180 | /* An array index points to threshold just below usage. */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 181 | int current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 182 | /* Size of entries[] */ |
| 183 | unsigned int size; |
| 184 | /* Array of thresholds */ |
| 185 | struct mem_cgroup_threshold entries[0]; |
| 186 | }; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 187 | |
| 188 | struct mem_cgroup_thresholds { |
| 189 | /* Primary thresholds array */ |
| 190 | struct mem_cgroup_threshold_ary *primary; |
| 191 | /* |
| 192 | * Spare threshold array. |
| 193 | * This is needed to make mem_cgroup_unregister_event() "never fail". |
| 194 | * It must be able to store at least primary->size - 1 entries. |
| 195 | */ |
| 196 | struct mem_cgroup_threshold_ary *spare; |
| 197 | }; |
| 198 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 199 | /* for OOM */ |
| 200 | struct mem_cgroup_eventfd_list { |
| 201 | struct list_head list; |
| 202 | struct eventfd_ctx *eventfd; |
| 203 | }; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 204 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 205 | static void mem_cgroup_threshold(struct mem_cgroup *mem); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 206 | static void mem_cgroup_oom_notify(struct mem_cgroup *mem); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 207 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 208 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 209 | * The memory controller data structure. The memory controller controls both |
| 210 | * page cache and RSS per cgroup. We would eventually like to provide |
| 211 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, |
| 212 | * to help the administrator determine what knobs to tune. |
| 213 | * |
| 214 | * 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] | 215 | * we hit the water mark. May be even add a low water mark, such that |
| 216 | * no reclaim occurs from a cgroup at it's low water mark, this is |
| 217 | * a feature that will be implemented much later in the future. |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 218 | */ |
| 219 | struct mem_cgroup { |
| 220 | struct cgroup_subsys_state css; |
| 221 | /* |
| 222 | * the counter to account for memory usage |
| 223 | */ |
| 224 | struct res_counter res; |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 225 | /* |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 226 | * the counter to account for mem+swap usage. |
| 227 | */ |
| 228 | struct res_counter memsw; |
| 229 | /* |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 230 | * Per cgroup active and inactive list, similar to the |
| 231 | * per zone LRU lists. |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 232 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 233 | struct mem_cgroup_lru_info info; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 234 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 235 | * While reclaiming in a hierarchy, we cache the last child we |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 236 | * reclaimed from. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 237 | */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 238 | int last_scanned_child; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 239 | int last_scanned_node; |
| 240 | #if MAX_NUMNODES > 1 |
| 241 | nodemask_t scan_nodes; |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 242 | atomic_t numainfo_events; |
| 243 | atomic_t numainfo_updating; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 244 | #endif |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 245 | /* |
| 246 | * Should the accounting and control be hierarchical, per subtree? |
| 247 | */ |
| 248 | bool use_hierarchy; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame^] | 249 | |
| 250 | bool oom_lock; |
| 251 | atomic_t under_oom; |
| 252 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 253 | atomic_t refcnt; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 254 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 255 | int swappiness; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 256 | /* OOM-Killer disable */ |
| 257 | int oom_kill_disable; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 258 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 259 | /* set when res.limit == memsw.limit */ |
| 260 | bool memsw_is_minimum; |
| 261 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 262 | /* protect arrays of thresholds */ |
| 263 | struct mutex thresholds_lock; |
| 264 | |
| 265 | /* thresholds for memory usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 266 | struct mem_cgroup_thresholds thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 267 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 268 | /* thresholds for mem+swap usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 269 | struct mem_cgroup_thresholds memsw_thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 270 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 271 | /* For oom notifier event fd */ |
| 272 | struct list_head oom_notify; |
| 273 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 274 | /* |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 275 | * Should we move charges of a task when a task is moved into this |
| 276 | * mem_cgroup ? And what type of charges should we move ? |
| 277 | */ |
| 278 | unsigned long move_charge_at_immigrate; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 279 | /* |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 280 | * percpu counter. |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 281 | */ |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 282 | struct mem_cgroup_stat_cpu *stat; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 283 | /* |
| 284 | * used when a cpu is offlined or other synchronizations |
| 285 | * See mem_cgroup_read_stat(). |
| 286 | */ |
| 287 | struct mem_cgroup_stat_cpu nocpu_base; |
| 288 | spinlock_t pcp_counter_lock; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 289 | }; |
| 290 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 291 | /* Stuffs for move charges at task migration. */ |
| 292 | /* |
| 293 | * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a |
| 294 | * left-shifted bitmap of these types. |
| 295 | */ |
| 296 | enum move_type { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 297 | MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */ |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 298 | MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 299 | NR_MOVE_TYPE, |
| 300 | }; |
| 301 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 302 | /* "mc" and its members are protected by cgroup_mutex */ |
| 303 | static struct move_charge_struct { |
Daisuke Nishimura | b1dd693 | 2010-11-24 12:57:06 -0800 | [diff] [blame] | 304 | spinlock_t lock; /* for from, to */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 305 | struct mem_cgroup *from; |
| 306 | struct mem_cgroup *to; |
| 307 | unsigned long precharge; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 308 | unsigned long moved_charge; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 309 | unsigned long moved_swap; |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 310 | struct task_struct *moving_task; /* a task moving charges */ |
| 311 | wait_queue_head_t waitq; /* a waitq for other context */ |
| 312 | } mc = { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 313 | .lock = __SPIN_LOCK_UNLOCKED(mc.lock), |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 314 | .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq), |
| 315 | }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 316 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 317 | static bool move_anon(void) |
| 318 | { |
| 319 | return test_bit(MOVE_CHARGE_TYPE_ANON, |
| 320 | &mc.to->move_charge_at_immigrate); |
| 321 | } |
| 322 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 323 | static bool move_file(void) |
| 324 | { |
| 325 | return test_bit(MOVE_CHARGE_TYPE_FILE, |
| 326 | &mc.to->move_charge_at_immigrate); |
| 327 | } |
| 328 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 329 | /* |
| 330 | * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft |
| 331 | * limit reclaim to prevent infinite loops, if they ever occur. |
| 332 | */ |
| 333 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100) |
| 334 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2) |
| 335 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 336 | enum charge_type { |
| 337 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
| 338 | MEM_CGROUP_CHARGE_TYPE_MAPPED, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 339 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 340 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 341 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 342 | MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 343 | NR_CHARGE_TYPE, |
| 344 | }; |
| 345 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 346 | /* for encoding cft->private value on file */ |
| 347 | #define _MEM (0) |
| 348 | #define _MEMSWAP (1) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 349 | #define _OOM_TYPE (2) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 350 | #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) |
| 351 | #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff) |
| 352 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 353 | /* Used for OOM nofiier */ |
| 354 | #define OOM_CONTROL (0) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 355 | |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 356 | /* |
| 357 | * Reclaim flags for mem_cgroup_hierarchical_reclaim |
| 358 | */ |
| 359 | #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0 |
| 360 | #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT) |
| 361 | #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1 |
| 362 | #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 363 | #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2 |
| 364 | #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT) |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 365 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 366 | static void mem_cgroup_get(struct mem_cgroup *mem); |
| 367 | static void mem_cgroup_put(struct mem_cgroup *mem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 368 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 369 | static void drain_all_stock_async(struct mem_cgroup *mem); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 370 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 371 | static struct mem_cgroup_per_zone * |
| 372 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) |
| 373 | { |
| 374 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; |
| 375 | } |
| 376 | |
Wu Fengguang | d324236 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 377 | struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem) |
| 378 | { |
| 379 | return &mem->css; |
| 380 | } |
| 381 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 382 | static struct mem_cgroup_per_zone * |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 383 | page_cgroup_zoneinfo(struct mem_cgroup *mem, struct page *page) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 384 | { |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 385 | int nid = page_to_nid(page); |
| 386 | int zid = page_zonenum(page); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 387 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 388 | return mem_cgroup_zoneinfo(mem, nid, zid); |
| 389 | } |
| 390 | |
| 391 | static struct mem_cgroup_tree_per_zone * |
| 392 | soft_limit_tree_node_zone(int nid, int zid) |
| 393 | { |
| 394 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 395 | } |
| 396 | |
| 397 | static struct mem_cgroup_tree_per_zone * |
| 398 | soft_limit_tree_from_page(struct page *page) |
| 399 | { |
| 400 | int nid = page_to_nid(page); |
| 401 | int zid = page_zonenum(page); |
| 402 | |
| 403 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 404 | } |
| 405 | |
| 406 | static void |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 407 | __mem_cgroup_insert_exceeded(struct mem_cgroup *mem, |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 408 | struct mem_cgroup_per_zone *mz, |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 409 | struct mem_cgroup_tree_per_zone *mctz, |
| 410 | unsigned long long new_usage_in_excess) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 411 | { |
| 412 | struct rb_node **p = &mctz->rb_root.rb_node; |
| 413 | struct rb_node *parent = NULL; |
| 414 | struct mem_cgroup_per_zone *mz_node; |
| 415 | |
| 416 | if (mz->on_tree) |
| 417 | return; |
| 418 | |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 419 | mz->usage_in_excess = new_usage_in_excess; |
| 420 | if (!mz->usage_in_excess) |
| 421 | return; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 422 | while (*p) { |
| 423 | parent = *p; |
| 424 | mz_node = rb_entry(parent, struct mem_cgroup_per_zone, |
| 425 | tree_node); |
| 426 | if (mz->usage_in_excess < mz_node->usage_in_excess) |
| 427 | p = &(*p)->rb_left; |
| 428 | /* |
| 429 | * We can't avoid mem cgroups that are over their soft |
| 430 | * limit by the same amount |
| 431 | */ |
| 432 | else if (mz->usage_in_excess >= mz_node->usage_in_excess) |
| 433 | p = &(*p)->rb_right; |
| 434 | } |
| 435 | rb_link_node(&mz->tree_node, parent, p); |
| 436 | rb_insert_color(&mz->tree_node, &mctz->rb_root); |
| 437 | mz->on_tree = true; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static void |
| 441 | __mem_cgroup_remove_exceeded(struct mem_cgroup *mem, |
| 442 | struct mem_cgroup_per_zone *mz, |
| 443 | struct mem_cgroup_tree_per_zone *mctz) |
| 444 | { |
| 445 | if (!mz->on_tree) |
| 446 | return; |
| 447 | rb_erase(&mz->tree_node, &mctz->rb_root); |
| 448 | mz->on_tree = false; |
| 449 | } |
| 450 | |
| 451 | static void |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 452 | mem_cgroup_remove_exceeded(struct mem_cgroup *mem, |
| 453 | struct mem_cgroup_per_zone *mz, |
| 454 | struct mem_cgroup_tree_per_zone *mctz) |
| 455 | { |
| 456 | spin_lock(&mctz->lock); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 457 | __mem_cgroup_remove_exceeded(mem, mz, mctz); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 458 | spin_unlock(&mctz->lock); |
| 459 | } |
| 460 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 461 | |
| 462 | static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page) |
| 463 | { |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 464 | unsigned long long excess; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 465 | struct mem_cgroup_per_zone *mz; |
| 466 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 467 | int nid = page_to_nid(page); |
| 468 | int zid = page_zonenum(page); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 469 | mctz = soft_limit_tree_from_page(page); |
| 470 | |
| 471 | /* |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 472 | * Necessary to update all ancestors when hierarchy is used. |
| 473 | * because their event counter is not touched. |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 474 | */ |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 475 | for (; mem; mem = parent_mem_cgroup(mem)) { |
| 476 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 477 | excess = res_counter_soft_limit_excess(&mem->res); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 478 | /* |
| 479 | * We have to update the tree if mz is on RB-tree or |
| 480 | * mem is over its softlimit. |
| 481 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 482 | if (excess || mz->on_tree) { |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 483 | spin_lock(&mctz->lock); |
| 484 | /* if on-tree, remove it */ |
| 485 | if (mz->on_tree) |
| 486 | __mem_cgroup_remove_exceeded(mem, mz, mctz); |
| 487 | /* |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 488 | * Insert again. mz->usage_in_excess will be updated. |
| 489 | * If excess is 0, no tree ops. |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 490 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 491 | __mem_cgroup_insert_exceeded(mem, mz, mctz, excess); |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 492 | spin_unlock(&mctz->lock); |
| 493 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
| 497 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem) |
| 498 | { |
| 499 | int node, zone; |
| 500 | struct mem_cgroup_per_zone *mz; |
| 501 | struct mem_cgroup_tree_per_zone *mctz; |
| 502 | |
| 503 | for_each_node_state(node, N_POSSIBLE) { |
| 504 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 505 | mz = mem_cgroup_zoneinfo(mem, node, zone); |
| 506 | mctz = soft_limit_tree_node_zone(node, zone); |
| 507 | mem_cgroup_remove_exceeded(mem, mz, mctz); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 512 | static struct mem_cgroup_per_zone * |
| 513 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 514 | { |
| 515 | struct rb_node *rightmost = NULL; |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 516 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 517 | |
| 518 | retry: |
KAMEZAWA Hiroyuki | 26251ea | 2009-10-01 15:44:08 -0700 | [diff] [blame] | 519 | mz = NULL; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 520 | rightmost = rb_last(&mctz->rb_root); |
| 521 | if (!rightmost) |
| 522 | goto done; /* Nothing to reclaim from */ |
| 523 | |
| 524 | mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node); |
| 525 | /* |
| 526 | * Remove the node now but someone else can add it back, |
| 527 | * we will to add it back at the end of reclaim to its correct |
| 528 | * position in the tree. |
| 529 | */ |
| 530 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
| 531 | if (!res_counter_soft_limit_excess(&mz->mem->res) || |
| 532 | !css_tryget(&mz->mem->css)) |
| 533 | goto retry; |
| 534 | done: |
| 535 | return mz; |
| 536 | } |
| 537 | |
| 538 | static struct mem_cgroup_per_zone * |
| 539 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 540 | { |
| 541 | struct mem_cgroup_per_zone *mz; |
| 542 | |
| 543 | spin_lock(&mctz->lock); |
| 544 | mz = __mem_cgroup_largest_soft_limit_node(mctz); |
| 545 | spin_unlock(&mctz->lock); |
| 546 | return mz; |
| 547 | } |
| 548 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 549 | /* |
| 550 | * Implementation Note: reading percpu statistics for memcg. |
| 551 | * |
| 552 | * Both of vmstat[] and percpu_counter has threshold and do periodic |
| 553 | * synchronization to implement "quick" read. There are trade-off between |
| 554 | * reading cost and precision of value. Then, we may have a chance to implement |
| 555 | * a periodic synchronizion of counter in memcg's counter. |
| 556 | * |
| 557 | * But this _read() function is used for user interface now. The user accounts |
| 558 | * memory usage by memory cgroup and he _always_ requires exact value because |
| 559 | * he accounts memory. Even if we provide quick-and-fuzzy read, we always |
| 560 | * have to visit all online cpus and make sum. So, for now, unnecessary |
| 561 | * synchronization is not implemented. (just implemented for cpu hotplug) |
| 562 | * |
| 563 | * If there are kernel internal actions which can make use of some not-exact |
| 564 | * value, and reading all cpu value can be performance bottleneck in some |
| 565 | * common workload, threashold and synchonization as vmstat[] should be |
| 566 | * implemented. |
| 567 | */ |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 568 | static long mem_cgroup_read_stat(struct mem_cgroup *mem, |
| 569 | enum mem_cgroup_stat_index idx) |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 570 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 571 | long val = 0; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 572 | int cpu; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 573 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 574 | get_online_cpus(); |
| 575 | for_each_online_cpu(cpu) |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 576 | val += per_cpu(mem->stat->count[idx], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 577 | #ifdef CONFIG_HOTPLUG_CPU |
| 578 | spin_lock(&mem->pcp_counter_lock); |
| 579 | val += mem->nocpu_base.count[idx]; |
| 580 | spin_unlock(&mem->pcp_counter_lock); |
| 581 | #endif |
| 582 | put_online_cpus(); |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 583 | return val; |
| 584 | } |
| 585 | |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 586 | static void mem_cgroup_swap_statistics(struct mem_cgroup *mem, |
| 587 | bool charge) |
| 588 | { |
| 589 | int val = (charge) ? 1 : -1; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 590 | this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 593 | void mem_cgroup_pgfault(struct mem_cgroup *mem, int val) |
| 594 | { |
| 595 | this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val); |
| 596 | } |
| 597 | |
| 598 | void mem_cgroup_pgmajfault(struct mem_cgroup *mem, int val) |
| 599 | { |
| 600 | this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val); |
| 601 | } |
| 602 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 603 | static unsigned long mem_cgroup_read_events(struct mem_cgroup *mem, |
| 604 | enum mem_cgroup_events_index idx) |
| 605 | { |
| 606 | unsigned long val = 0; |
| 607 | int cpu; |
| 608 | |
| 609 | for_each_online_cpu(cpu) |
| 610 | val += per_cpu(mem->stat->events[idx], cpu); |
| 611 | #ifdef CONFIG_HOTPLUG_CPU |
| 612 | spin_lock(&mem->pcp_counter_lock); |
| 613 | val += mem->nocpu_base.events[idx]; |
| 614 | spin_unlock(&mem->pcp_counter_lock); |
| 615 | #endif |
| 616 | return val; |
| 617 | } |
| 618 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 619 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 620 | bool file, int nr_pages) |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 621 | { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 622 | preempt_disable(); |
| 623 | |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 624 | if (file) |
| 625 | __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], nr_pages); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 626 | else |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 627 | __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], nr_pages); |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 628 | |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 629 | /* pagein of a big page is an event. So, ignore page size */ |
| 630 | if (nr_pages > 0) |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 631 | __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGIN]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 632 | else { |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 633 | __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 634 | nr_pages = -nr_pages; /* for event */ |
| 635 | } |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 636 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 637 | __this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 638 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 639 | preempt_enable(); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 640 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 641 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 642 | unsigned long |
| 643 | mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *mem, int nid, int zid, |
| 644 | unsigned int lru_mask) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 645 | { |
| 646 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 647 | enum lru_list l; |
| 648 | unsigned long ret = 0; |
| 649 | |
| 650 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 651 | |
| 652 | for_each_lru(l) { |
| 653 | if (BIT(l) & lru_mask) |
| 654 | ret += MEM_CGROUP_ZSTAT(mz, l); |
| 655 | } |
| 656 | return ret; |
| 657 | } |
| 658 | |
| 659 | static unsigned long |
| 660 | mem_cgroup_node_nr_lru_pages(struct mem_cgroup *mem, |
| 661 | int nid, unsigned int lru_mask) |
| 662 | { |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 663 | u64 total = 0; |
| 664 | int zid; |
| 665 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 666 | for (zid = 0; zid < MAX_NR_ZONES; zid++) |
| 667 | total += mem_cgroup_zone_nr_lru_pages(mem, nid, zid, lru_mask); |
| 668 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 669 | return total; |
| 670 | } |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 671 | |
| 672 | static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *mem, |
| 673 | unsigned int lru_mask) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 674 | { |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 675 | int nid; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 676 | u64 total = 0; |
| 677 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 678 | for_each_node_state(nid, N_HIGH_MEMORY) |
| 679 | total += mem_cgroup_node_nr_lru_pages(mem, nid, lru_mask); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 680 | return total; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 683 | static bool __memcg_event_check(struct mem_cgroup *mem, int target) |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 684 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 685 | unsigned long val, next; |
| 686 | |
| 687 | val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]); |
| 688 | next = this_cpu_read(mem->stat->targets[target]); |
| 689 | /* from time_after() in jiffies.h */ |
| 690 | return ((long)next - (long)val < 0); |
| 691 | } |
| 692 | |
| 693 | static void __mem_cgroup_target_update(struct mem_cgroup *mem, int target) |
| 694 | { |
| 695 | unsigned long val, next; |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 696 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 697 | val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 698 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 699 | switch (target) { |
| 700 | case MEM_CGROUP_TARGET_THRESH: |
| 701 | next = val + THRESHOLDS_EVENTS_TARGET; |
| 702 | break; |
| 703 | case MEM_CGROUP_TARGET_SOFTLIMIT: |
| 704 | next = val + SOFTLIMIT_EVENTS_TARGET; |
| 705 | break; |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 706 | case MEM_CGROUP_TARGET_NUMAINFO: |
| 707 | next = val + NUMAINFO_EVENTS_TARGET; |
| 708 | break; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 709 | default: |
| 710 | return; |
| 711 | } |
| 712 | |
| 713 | this_cpu_write(mem->stat->targets[target], next); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Check events in order. |
| 718 | * |
| 719 | */ |
| 720 | static void memcg_check_events(struct mem_cgroup *mem, struct page *page) |
| 721 | { |
| 722 | /* threshold event is triggered in finer grain than soft limit */ |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 723 | if (unlikely(__memcg_event_check(mem, MEM_CGROUP_TARGET_THRESH))) { |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 724 | mem_cgroup_threshold(mem); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 725 | __mem_cgroup_target_update(mem, MEM_CGROUP_TARGET_THRESH); |
| 726 | if (unlikely(__memcg_event_check(mem, |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 727 | MEM_CGROUP_TARGET_SOFTLIMIT))) { |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 728 | mem_cgroup_update_tree(mem, page); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 729 | __mem_cgroup_target_update(mem, |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 730 | MEM_CGROUP_TARGET_SOFTLIMIT); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 731 | } |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 732 | #if MAX_NUMNODES > 1 |
| 733 | if (unlikely(__memcg_event_check(mem, |
| 734 | MEM_CGROUP_TARGET_NUMAINFO))) { |
| 735 | atomic_inc(&mem->numainfo_events); |
| 736 | __mem_cgroup_target_update(mem, |
| 737 | MEM_CGROUP_TARGET_NUMAINFO); |
| 738 | } |
| 739 | #endif |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 743 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 744 | { |
| 745 | return container_of(cgroup_subsys_state(cont, |
| 746 | mem_cgroup_subsys_id), struct mem_cgroup, |
| 747 | css); |
| 748 | } |
| 749 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 750 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 751 | { |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 752 | /* |
| 753 | * mm_update_next_owner() may clear mm->owner to NULL |
| 754 | * if it races with swapoff, page migration, etc. |
| 755 | * So this can be called with p == NULL. |
| 756 | */ |
| 757 | if (unlikely(!p)) |
| 758 | return NULL; |
| 759 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 760 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
| 761 | struct mem_cgroup, css); |
| 762 | } |
| 763 | |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 764 | struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 765 | { |
| 766 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 767 | |
| 768 | if (!mm) |
| 769 | return NULL; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 770 | /* |
| 771 | * Because we have no locks, mm->owner's may be being moved to other |
| 772 | * cgroup. We use css_tryget() here even if this looks |
| 773 | * pessimistic (rather than adding locks here). |
| 774 | */ |
| 775 | rcu_read_lock(); |
| 776 | do { |
| 777 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 778 | if (unlikely(!mem)) |
| 779 | break; |
| 780 | } while (!css_tryget(&mem->css)); |
| 781 | rcu_read_unlock(); |
| 782 | return mem; |
| 783 | } |
| 784 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 785 | /* The caller has to guarantee "mem" exists before calling this */ |
| 786 | static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 787 | { |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 788 | struct cgroup_subsys_state *css; |
| 789 | int found; |
| 790 | |
| 791 | if (!mem) /* ROOT cgroup has the smallest ID */ |
| 792 | return root_mem_cgroup; /*css_put/get against root is ignored*/ |
| 793 | if (!mem->use_hierarchy) { |
| 794 | if (css_tryget(&mem->css)) |
| 795 | return mem; |
| 796 | return NULL; |
| 797 | } |
| 798 | rcu_read_lock(); |
| 799 | /* |
| 800 | * searching a memory cgroup which has the smallest ID under given |
| 801 | * ROOT cgroup. (ID >= 1) |
| 802 | */ |
| 803 | css = css_get_next(&mem_cgroup_subsys, 1, &mem->css, &found); |
| 804 | if (css && css_tryget(css)) |
| 805 | mem = container_of(css, struct mem_cgroup, css); |
| 806 | else |
| 807 | mem = NULL; |
| 808 | rcu_read_unlock(); |
| 809 | return mem; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 810 | } |
| 811 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 812 | static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter, |
| 813 | struct mem_cgroup *root, |
| 814 | bool cond) |
| 815 | { |
| 816 | int nextid = css_id(&iter->css) + 1; |
| 817 | int found; |
| 818 | int hierarchy_used; |
| 819 | struct cgroup_subsys_state *css; |
| 820 | |
| 821 | hierarchy_used = iter->use_hierarchy; |
| 822 | |
| 823 | css_put(&iter->css); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 824 | /* If no ROOT, walk all, ignore hierarchy */ |
| 825 | if (!cond || (root && !hierarchy_used)) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 826 | return NULL; |
| 827 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 828 | if (!root) |
| 829 | root = root_mem_cgroup; |
| 830 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 831 | do { |
| 832 | iter = NULL; |
| 833 | rcu_read_lock(); |
| 834 | |
| 835 | css = css_get_next(&mem_cgroup_subsys, nextid, |
| 836 | &root->css, &found); |
| 837 | if (css && css_tryget(css)) |
| 838 | iter = container_of(css, struct mem_cgroup, css); |
| 839 | rcu_read_unlock(); |
| 840 | /* If css is NULL, no more cgroups will be found */ |
| 841 | nextid = found + 1; |
| 842 | } while (css && !iter); |
| 843 | |
| 844 | return iter; |
| 845 | } |
| 846 | /* |
| 847 | * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please |
| 848 | * be careful that "break" loop is not allowed. We have reference count. |
| 849 | * Instead of that modify "cond" to be false and "continue" to exit the loop. |
| 850 | */ |
| 851 | #define for_each_mem_cgroup_tree_cond(iter, root, cond) \ |
| 852 | for (iter = mem_cgroup_start_loop(root);\ |
| 853 | iter != NULL;\ |
| 854 | iter = mem_cgroup_get_next(iter, root, cond)) |
| 855 | |
| 856 | #define for_each_mem_cgroup_tree(iter, root) \ |
| 857 | for_each_mem_cgroup_tree_cond(iter, root, true) |
| 858 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 859 | #define for_each_mem_cgroup_all(iter) \ |
| 860 | for_each_mem_cgroup_tree_cond(iter, NULL, true) |
| 861 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 862 | |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 863 | static inline bool mem_cgroup_is_root(struct mem_cgroup *mem) |
| 864 | { |
| 865 | return (mem == root_mem_cgroup); |
| 866 | } |
| 867 | |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 868 | void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx) |
| 869 | { |
| 870 | struct mem_cgroup *mem; |
| 871 | |
| 872 | if (!mm) |
| 873 | return; |
| 874 | |
| 875 | rcu_read_lock(); |
| 876 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 877 | if (unlikely(!mem)) |
| 878 | goto out; |
| 879 | |
| 880 | switch (idx) { |
| 881 | case PGMAJFAULT: |
| 882 | mem_cgroup_pgmajfault(mem, 1); |
| 883 | break; |
| 884 | case PGFAULT: |
| 885 | mem_cgroup_pgfault(mem, 1); |
| 886 | break; |
| 887 | default: |
| 888 | BUG(); |
| 889 | } |
| 890 | out: |
| 891 | rcu_read_unlock(); |
| 892 | } |
| 893 | EXPORT_SYMBOL(mem_cgroup_count_vm_event); |
| 894 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 895 | /* |
| 896 | * Following LRU functions are allowed to be used without PCG_LOCK. |
| 897 | * Operations are called by routine of global LRU independently from memcg. |
| 898 | * What we have to take care of here is validness of pc->mem_cgroup. |
| 899 | * |
| 900 | * Changes to pc->mem_cgroup happens when |
| 901 | * 1. charge |
| 902 | * 2. moving account |
| 903 | * In typical case, "charge" is done before add-to-lru. Exception is SwapCache. |
| 904 | * It is added to LRU before charge. |
| 905 | * If PCG_USED bit is not set, page_cgroup is not added to this private LRU. |
| 906 | * When moving account, the page is not on LRU. It's isolated. |
| 907 | */ |
| 908 | |
| 909 | 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] | 910 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 911 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 912 | struct mem_cgroup_per_zone *mz; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 913 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 914 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 915 | return; |
| 916 | pc = lookup_page_cgroup(page); |
| 917 | /* can happen while we handle swapcache. */ |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 918 | if (!TestClearPageCgroupAcctLRU(pc)) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 919 | return; |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 920 | VM_BUG_ON(!pc->mem_cgroup); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 921 | /* |
| 922 | * We don't check PCG_USED bit. It's cleared when the "page" is finally |
| 923 | * removed from global LRU. |
| 924 | */ |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 925 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 926 | /* huge page split is done under lru_lock. so, we have no races. */ |
| 927 | MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 928 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 929 | return; |
| 930 | VM_BUG_ON(list_empty(&pc->lru)); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 931 | list_del_init(&pc->lru); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 932 | } |
| 933 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 934 | void mem_cgroup_del_lru(struct page *page) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 935 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 936 | mem_cgroup_del_lru_list(page, page_lru(page)); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 937 | } |
| 938 | |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 939 | /* |
| 940 | * Writeback is about to end against a page which has been marked for immediate |
| 941 | * reclaim. If it still appears to be reclaimable, move it to the tail of the |
| 942 | * inactive list. |
| 943 | */ |
| 944 | void mem_cgroup_rotate_reclaimable_page(struct page *page) |
| 945 | { |
| 946 | struct mem_cgroup_per_zone *mz; |
| 947 | struct page_cgroup *pc; |
| 948 | enum lru_list lru = page_lru(page); |
| 949 | |
| 950 | if (mem_cgroup_disabled()) |
| 951 | return; |
| 952 | |
| 953 | pc = lookup_page_cgroup(page); |
| 954 | /* unused or root page is not rotated. */ |
| 955 | if (!PageCgroupUsed(pc)) |
| 956 | return; |
| 957 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 958 | smp_rmb(); |
| 959 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 960 | return; |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 961 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 962 | list_move_tail(&pc->lru, &mz->lists[lru]); |
| 963 | } |
| 964 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 965 | 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] | 966 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 967 | struct mem_cgroup_per_zone *mz; |
| 968 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 969 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 970 | if (mem_cgroup_disabled()) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 971 | return; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 972 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 973 | pc = lookup_page_cgroup(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 974 | /* unused or root page is not rotated. */ |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 975 | if (!PageCgroupUsed(pc)) |
| 976 | return; |
| 977 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 978 | smp_rmb(); |
| 979 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 980 | return; |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 981 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 982 | list_move(&pc->lru, &mz->lists[lru]); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 983 | } |
| 984 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 985 | void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru) |
| 986 | { |
| 987 | struct page_cgroup *pc; |
| 988 | struct mem_cgroup_per_zone *mz; |
| 989 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 990 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 991 | return; |
| 992 | pc = lookup_page_cgroup(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 993 | VM_BUG_ON(PageCgroupAcctLRU(pc)); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 994 | if (!PageCgroupUsed(pc)) |
| 995 | return; |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 996 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 997 | smp_rmb(); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 998 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 999 | /* huge page split is done under lru_lock. so, we have no races. */ |
| 1000 | MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 1001 | SetPageCgroupAcctLRU(pc); |
| 1002 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
| 1003 | return; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1004 | list_add(&pc->lru, &mz->lists[lru]); |
| 1005 | } |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1006 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1007 | /* |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1008 | * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed |
| 1009 | * while it's linked to lru because the page may be reused after it's fully |
| 1010 | * uncharged. To handle that, unlink page_cgroup from LRU when charge it again. |
| 1011 | * It's done under lock_page and expected that zone->lru_lock isnever held. |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1012 | */ |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1013 | static void mem_cgroup_lru_del_before_commit(struct page *page) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1014 | { |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1015 | unsigned long flags; |
| 1016 | struct zone *zone = page_zone(page); |
| 1017 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 1018 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1019 | /* |
| 1020 | * Doing this check without taking ->lru_lock seems wrong but this |
| 1021 | * is safe. Because if page_cgroup's USED bit is unset, the page |
| 1022 | * will not be added to any memcg's LRU. If page_cgroup's USED bit is |
| 1023 | * set, the commit after this will fail, anyway. |
| 1024 | * This all charge/uncharge is done under some mutual execustion. |
| 1025 | * So, we don't need to taking care of changes in USED bit. |
| 1026 | */ |
| 1027 | if (likely(!PageLRU(page))) |
| 1028 | return; |
| 1029 | |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1030 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 1031 | /* |
| 1032 | * Forget old LRU when this page_cgroup is *not* used. This Used bit |
| 1033 | * is guarded by lock_page() because the page is SwapCache. |
| 1034 | */ |
| 1035 | if (!PageCgroupUsed(pc)) |
| 1036 | mem_cgroup_del_lru_list(page, page_lru(page)); |
| 1037 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1040 | static void mem_cgroup_lru_add_after_commit(struct page *page) |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1041 | { |
| 1042 | unsigned long flags; |
| 1043 | struct zone *zone = page_zone(page); |
| 1044 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 1045 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 1046 | /* taking care of that the page is added to LRU while we commit it */ |
| 1047 | if (likely(!PageLRU(page))) |
| 1048 | return; |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1049 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 1050 | /* 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] | 1051 | if (PageLRU(page) && !PageCgroupAcctLRU(pc)) |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1052 | mem_cgroup_add_lru_list(page, page_lru(page)); |
| 1053 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
| 1054 | } |
| 1055 | |
| 1056 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1057 | void mem_cgroup_move_lists(struct page *page, |
| 1058 | enum lru_list from, enum lru_list to) |
| 1059 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1060 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1061 | return; |
| 1062 | mem_cgroup_del_lru_list(page, from); |
| 1063 | mem_cgroup_add_lru_list(page, to); |
| 1064 | } |
| 1065 | |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1066 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
| 1067 | { |
| 1068 | int ret; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1069 | struct mem_cgroup *curr = NULL; |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1070 | struct task_struct *p; |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1071 | |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1072 | p = find_lock_task_mm(task); |
| 1073 | if (!p) |
| 1074 | return 0; |
| 1075 | curr = try_get_mem_cgroup_from_mm(p->mm); |
| 1076 | task_unlock(p); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1077 | if (!curr) |
| 1078 | return 0; |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1079 | /* |
| 1080 | * We should check use_hierarchy of "mem" not "curr". Because checking |
| 1081 | * use_hierarchy of "curr" here make this function true if hierarchy is |
| 1082 | * enabled in "curr" and "curr" is a child of "mem" in *cgroup* |
| 1083 | * hierarchy(even if use_hierarchy is disabled in "mem"). |
| 1084 | */ |
| 1085 | if (mem->use_hierarchy) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1086 | ret = css_is_ancestor(&curr->css, &mem->css); |
| 1087 | else |
| 1088 | ret = (curr == mem); |
| 1089 | css_put(&curr->css); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1090 | return ret; |
| 1091 | } |
| 1092 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1093 | 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] | 1094 | { |
| 1095 | unsigned long active; |
| 1096 | unsigned long inactive; |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1097 | unsigned long gb; |
| 1098 | unsigned long inactive_ratio; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1099 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1100 | inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON)); |
| 1101 | active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON)); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1102 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1103 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
| 1104 | if (gb) |
| 1105 | inactive_ratio = int_sqrt(10 * gb); |
| 1106 | else |
| 1107 | inactive_ratio = 1; |
| 1108 | |
| 1109 | if (present_pages) { |
| 1110 | present_pages[0] = inactive; |
| 1111 | present_pages[1] = active; |
| 1112 | } |
| 1113 | |
| 1114 | return inactive_ratio; |
| 1115 | } |
| 1116 | |
| 1117 | int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg) |
| 1118 | { |
| 1119 | unsigned long active; |
| 1120 | unsigned long inactive; |
| 1121 | unsigned long present_pages[2]; |
| 1122 | unsigned long inactive_ratio; |
| 1123 | |
| 1124 | inactive_ratio = calc_inactive_ratio(memcg, present_pages); |
| 1125 | |
| 1126 | inactive = present_pages[0]; |
| 1127 | active = present_pages[1]; |
| 1128 | |
| 1129 | if (inactive * inactive_ratio < active) |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1130 | return 1; |
| 1131 | |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
Rik van Riel | 56e49d2 | 2009-06-16 15:32:28 -0700 | [diff] [blame] | 1135 | int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg) |
| 1136 | { |
| 1137 | unsigned long active; |
| 1138 | unsigned long inactive; |
| 1139 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1140 | inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE)); |
| 1141 | active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE)); |
Rik van Riel | 56e49d2 | 2009-06-16 15:32:28 -0700 | [diff] [blame] | 1142 | |
| 1143 | return (active > inactive); |
| 1144 | } |
| 1145 | |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1146 | struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg, |
| 1147 | struct zone *zone) |
| 1148 | { |
KOSAKI Motohiro | 13d7e3a | 2010-08-10 18:03:06 -0700 | [diff] [blame] | 1149 | int nid = zone_to_nid(zone); |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1150 | int zid = zone_idx(zone); |
| 1151 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 1152 | |
| 1153 | return &mz->reclaim_stat; |
| 1154 | } |
| 1155 | |
| 1156 | struct zone_reclaim_stat * |
| 1157 | mem_cgroup_get_reclaim_stat_from_page(struct page *page) |
| 1158 | { |
| 1159 | struct page_cgroup *pc; |
| 1160 | struct mem_cgroup_per_zone *mz; |
| 1161 | |
| 1162 | if (mem_cgroup_disabled()) |
| 1163 | return NULL; |
| 1164 | |
| 1165 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 1166 | if (!PageCgroupUsed(pc)) |
| 1167 | return NULL; |
Johannes Weiner | 713735b | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 1168 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
| 1169 | smp_rmb(); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 1170 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 1171 | return &mz->reclaim_stat; |
| 1172 | } |
| 1173 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1174 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 1175 | struct list_head *dst, |
| 1176 | unsigned long *scanned, int order, |
| 1177 | int mode, struct zone *z, |
| 1178 | struct mem_cgroup *mem_cont, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1179 | int active, int file) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1180 | { |
| 1181 | unsigned long nr_taken = 0; |
| 1182 | struct page *page; |
| 1183 | unsigned long scan; |
| 1184 | LIST_HEAD(pc_list); |
| 1185 | struct list_head *src; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1186 | struct page_cgroup *pc, *tmp; |
KOSAKI Motohiro | 13d7e3a | 2010-08-10 18:03:06 -0700 | [diff] [blame] | 1187 | int nid = zone_to_nid(z); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1188 | int zid = zone_idx(z); |
| 1189 | struct mem_cgroup_per_zone *mz; |
Johannes Weiner | b7c46d1 | 2009-09-21 17:02:56 -0700 | [diff] [blame] | 1190 | int lru = LRU_FILE * file + active; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1191 | int ret; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1192 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 1193 | BUG_ON(!mem_cont); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1194 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1195 | src = &mz->lists[lru]; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1196 | |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1197 | scan = 0; |
| 1198 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1199 | if (scan >= nr_to_scan) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1200 | break; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1201 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1202 | if (unlikely(!PageCgroupUsed(pc))) |
| 1203 | continue; |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 1204 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 1205 | page = lookup_cgroup_page(pc); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 1206 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1207 | if (unlikely(!PageLRU(page))) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1208 | continue; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 1209 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 1210 | scan++; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1211 | ret = __isolate_lru_page(page, mode, file); |
| 1212 | switch (ret) { |
| 1213 | case 0: |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1214 | list_move(&page->lru, dst); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1215 | mem_cgroup_del_lru(page); |
Rik van Riel | 2c888cf | 2011-01-13 15:47:13 -0800 | [diff] [blame] | 1216 | nr_taken += hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1217 | break; |
| 1218 | case -EBUSY: |
| 1219 | /* we don't affect global LRU but rotate in our LRU */ |
| 1220 | mem_cgroup_rotate_lru_list(page, page_lru(page)); |
| 1221 | break; |
| 1222 | default: |
| 1223 | break; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1227 | *scanned = scan; |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 1228 | |
| 1229 | trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken, |
| 1230 | 0, 0, 0, mode); |
| 1231 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1232 | return nr_taken; |
| 1233 | } |
| 1234 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1235 | #define mem_cgroup_from_res_counter(counter, member) \ |
| 1236 | container_of(counter, struct mem_cgroup, member) |
| 1237 | |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1238 | /** |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1239 | * mem_cgroup_margin - calculate chargeable space of a memory cgroup |
| 1240 | * @mem: the memory cgroup |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1241 | * |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1242 | * Returns the maximum amount of memory @mem can be charged with, in |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1243 | * pages. |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1244 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1245 | static unsigned long mem_cgroup_margin(struct mem_cgroup *mem) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1246 | { |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1247 | unsigned long long margin; |
| 1248 | |
| 1249 | margin = res_counter_margin(&mem->res); |
| 1250 | if (do_swap_account) |
| 1251 | margin = min(margin, res_counter_margin(&mem->memsw)); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1252 | return margin >> PAGE_SHIFT; |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 1255 | int mem_cgroup_swappiness(struct mem_cgroup *memcg) |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1256 | { |
| 1257 | struct cgroup *cgrp = memcg->css.cgroup; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1258 | |
| 1259 | /* root ? */ |
| 1260 | if (cgrp->parent == NULL) |
| 1261 | return vm_swappiness; |
| 1262 | |
Johannes Weiner | bf1ff26 | 2011-03-23 16:42:32 -0700 | [diff] [blame] | 1263 | return memcg->swappiness; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1264 | } |
| 1265 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1266 | static void mem_cgroup_start_move(struct mem_cgroup *mem) |
| 1267 | { |
| 1268 | int cpu; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1269 | |
| 1270 | get_online_cpus(); |
| 1271 | spin_lock(&mem->pcp_counter_lock); |
| 1272 | for_each_online_cpu(cpu) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1273 | per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1274 | mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1; |
| 1275 | spin_unlock(&mem->pcp_counter_lock); |
| 1276 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1277 | |
| 1278 | synchronize_rcu(); |
| 1279 | } |
| 1280 | |
| 1281 | static void mem_cgroup_end_move(struct mem_cgroup *mem) |
| 1282 | { |
| 1283 | int cpu; |
| 1284 | |
| 1285 | if (!mem) |
| 1286 | return; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1287 | get_online_cpus(); |
| 1288 | spin_lock(&mem->pcp_counter_lock); |
| 1289 | for_each_online_cpu(cpu) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1290 | per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1291 | mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1; |
| 1292 | spin_unlock(&mem->pcp_counter_lock); |
| 1293 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1294 | } |
| 1295 | /* |
| 1296 | * 2 routines for checking "mem" is under move_account() or not. |
| 1297 | * |
| 1298 | * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used |
| 1299 | * for avoiding race in accounting. If true, |
| 1300 | * pc->mem_cgroup may be overwritten. |
| 1301 | * |
| 1302 | * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or |
| 1303 | * under hierarchy of moving cgroups. This is for |
| 1304 | * waiting at hith-memory prressure caused by "move". |
| 1305 | */ |
| 1306 | |
| 1307 | static bool mem_cgroup_stealed(struct mem_cgroup *mem) |
| 1308 | { |
| 1309 | VM_BUG_ON(!rcu_read_lock_held()); |
| 1310 | return this_cpu_read(mem->stat->count[MEM_CGROUP_ON_MOVE]) > 0; |
| 1311 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1312 | |
| 1313 | static bool mem_cgroup_under_move(struct mem_cgroup *mem) |
| 1314 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1315 | struct mem_cgroup *from; |
| 1316 | struct mem_cgroup *to; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1317 | bool ret = false; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1318 | /* |
| 1319 | * Unlike task_move routines, we access mc.to, mc.from not under |
| 1320 | * mutual exclusion by cgroup_mutex. Here, we take spinlock instead. |
| 1321 | */ |
| 1322 | spin_lock(&mc.lock); |
| 1323 | from = mc.from; |
| 1324 | to = mc.to; |
| 1325 | if (!from) |
| 1326 | goto unlock; |
| 1327 | if (from == mem || to == mem |
| 1328 | || (mem->use_hierarchy && css_is_ancestor(&from->css, &mem->css)) |
| 1329 | || (mem->use_hierarchy && css_is_ancestor(&to->css, &mem->css))) |
| 1330 | ret = true; |
| 1331 | unlock: |
| 1332 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1333 | return ret; |
| 1334 | } |
| 1335 | |
| 1336 | static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem) |
| 1337 | { |
| 1338 | if (mc.moving_task && current != mc.moving_task) { |
| 1339 | if (mem_cgroup_under_move(mem)) { |
| 1340 | DEFINE_WAIT(wait); |
| 1341 | prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE); |
| 1342 | /* moving charge context might have finished. */ |
| 1343 | if (mc.moving_task) |
| 1344 | schedule(); |
| 1345 | finish_wait(&mc.waitq, &wait); |
| 1346 | return true; |
| 1347 | } |
| 1348 | } |
| 1349 | return false; |
| 1350 | } |
| 1351 | |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1352 | /** |
Kirill A. Shutemov | 6a6135b | 2010-03-10 15:22:25 -0800 | [diff] [blame] | 1353 | * 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] | 1354 | * @memcg: The memory cgroup that went over limit |
| 1355 | * @p: Task that is going to be killed |
| 1356 | * |
| 1357 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is |
| 1358 | * enabled |
| 1359 | */ |
| 1360 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) |
| 1361 | { |
| 1362 | struct cgroup *task_cgrp; |
| 1363 | struct cgroup *mem_cgrp; |
| 1364 | /* |
| 1365 | * Need a buffer in BSS, can't rely on allocations. The code relies |
| 1366 | * on the assumption that OOM is serialized for memory controller. |
| 1367 | * If this assumption is broken, revisit this code. |
| 1368 | */ |
| 1369 | static char memcg_name[PATH_MAX]; |
| 1370 | int ret; |
| 1371 | |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1372 | if (!memcg || !p) |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1373 | return; |
| 1374 | |
| 1375 | |
| 1376 | rcu_read_lock(); |
| 1377 | |
| 1378 | mem_cgrp = memcg->css.cgroup; |
| 1379 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); |
| 1380 | |
| 1381 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); |
| 1382 | if (ret < 0) { |
| 1383 | /* |
| 1384 | * Unfortunately, we are unable to convert to a useful name |
| 1385 | * But we'll still print out the usage information |
| 1386 | */ |
| 1387 | rcu_read_unlock(); |
| 1388 | goto done; |
| 1389 | } |
| 1390 | rcu_read_unlock(); |
| 1391 | |
| 1392 | printk(KERN_INFO "Task in %s killed", memcg_name); |
| 1393 | |
| 1394 | rcu_read_lock(); |
| 1395 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); |
| 1396 | if (ret < 0) { |
| 1397 | rcu_read_unlock(); |
| 1398 | goto done; |
| 1399 | } |
| 1400 | rcu_read_unlock(); |
| 1401 | |
| 1402 | /* |
| 1403 | * Continues from above, so we don't need an KERN_ level |
| 1404 | */ |
| 1405 | printk(KERN_CONT " as a result of limit of %s\n", memcg_name); |
| 1406 | done: |
| 1407 | |
| 1408 | printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n", |
| 1409 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, |
| 1410 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, |
| 1411 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); |
| 1412 | printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, " |
| 1413 | "failcnt %llu\n", |
| 1414 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, |
| 1415 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, |
| 1416 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); |
| 1417 | } |
| 1418 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1419 | /* |
| 1420 | * This function returns the number of memcg under hierarchy tree. Returns |
| 1421 | * 1(self count) if no children. |
| 1422 | */ |
| 1423 | static int mem_cgroup_count_children(struct mem_cgroup *mem) |
| 1424 | { |
| 1425 | int num = 0; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1426 | struct mem_cgroup *iter; |
| 1427 | |
| 1428 | for_each_mem_cgroup_tree(iter, mem) |
| 1429 | num++; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1430 | return num; |
| 1431 | } |
| 1432 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1433 | /* |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1434 | * Return the memory (and swap, if configured) limit for a memcg. |
| 1435 | */ |
| 1436 | u64 mem_cgroup_get_limit(struct mem_cgroup *memcg) |
| 1437 | { |
| 1438 | u64 limit; |
| 1439 | u64 memsw; |
| 1440 | |
Johannes Weiner | f3e8eb7 | 2011-01-13 15:47:39 -0800 | [diff] [blame] | 1441 | limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 1442 | limit += total_swap_pages << PAGE_SHIFT; |
| 1443 | |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1444 | memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 1445 | /* |
| 1446 | * If memsw is finite and limits the amount of swap space available |
| 1447 | * to this memcg, return that limit. |
| 1448 | */ |
| 1449 | return min(limit, memsw); |
| 1450 | } |
| 1451 | |
| 1452 | /* |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1453 | * Visit the first child (need not be the first child as per the ordering |
| 1454 | * of the cgroup list, since we track last_scanned_child) of @mem and use |
| 1455 | * that to reclaim free pages from. |
| 1456 | */ |
| 1457 | static struct mem_cgroup * |
| 1458 | mem_cgroup_select_victim(struct mem_cgroup *root_mem) |
| 1459 | { |
| 1460 | struct mem_cgroup *ret = NULL; |
| 1461 | struct cgroup_subsys_state *css; |
| 1462 | int nextid, found; |
| 1463 | |
| 1464 | if (!root_mem->use_hierarchy) { |
| 1465 | css_get(&root_mem->css); |
| 1466 | ret = root_mem; |
| 1467 | } |
| 1468 | |
| 1469 | while (!ret) { |
| 1470 | rcu_read_lock(); |
| 1471 | nextid = root_mem->last_scanned_child + 1; |
| 1472 | css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css, |
| 1473 | &found); |
| 1474 | if (css && css_tryget(css)) |
| 1475 | ret = container_of(css, struct mem_cgroup, css); |
| 1476 | |
| 1477 | rcu_read_unlock(); |
| 1478 | /* Updates scanning parameter */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1479 | if (!css) { |
| 1480 | /* this means start scan from ID:1 */ |
| 1481 | root_mem->last_scanned_child = 0; |
| 1482 | } else |
| 1483 | root_mem->last_scanned_child = found; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | return ret; |
| 1487 | } |
| 1488 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1489 | /** |
| 1490 | * test_mem_cgroup_node_reclaimable |
| 1491 | * @mem: the target memcg |
| 1492 | * @nid: the node ID to be checked. |
| 1493 | * @noswap : specify true here if the user wants flle only information. |
| 1494 | * |
| 1495 | * This function returns whether the specified memcg contains any |
| 1496 | * reclaimable pages on a node. Returns true if there are any reclaimable |
| 1497 | * pages in the node. |
| 1498 | */ |
| 1499 | static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *mem, |
| 1500 | int nid, bool noswap) |
| 1501 | { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1502 | if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_FILE)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1503 | return true; |
| 1504 | if (noswap || !total_swap_pages) |
| 1505 | return false; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 1506 | if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_ANON)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1507 | return true; |
| 1508 | return false; |
| 1509 | |
| 1510 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1511 | #if MAX_NUMNODES > 1 |
| 1512 | |
| 1513 | /* |
| 1514 | * Always updating the nodemask is not very good - even if we have an empty |
| 1515 | * list or the wrong list here, we can start from some node and traverse all |
| 1516 | * nodes based on the zonelist. So update the list loosely once per 10 secs. |
| 1517 | * |
| 1518 | */ |
| 1519 | static void mem_cgroup_may_update_nodemask(struct mem_cgroup *mem) |
| 1520 | { |
| 1521 | int nid; |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1522 | /* |
| 1523 | * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET |
| 1524 | * pagein/pageout changes since the last update. |
| 1525 | */ |
| 1526 | if (!atomic_read(&mem->numainfo_events)) |
| 1527 | return; |
| 1528 | if (atomic_inc_return(&mem->numainfo_updating) > 1) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1529 | return; |
| 1530 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1531 | /* make a nodemask where this memcg uses memory from */ |
| 1532 | mem->scan_nodes = node_states[N_HIGH_MEMORY]; |
| 1533 | |
| 1534 | for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) { |
| 1535 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1536 | if (!test_mem_cgroup_node_reclaimable(mem, nid, false)) |
| 1537 | node_clear(nid, mem->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1538 | } |
KAMEZAWA Hiroyuki | 453a9bf3 | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1539 | |
| 1540 | atomic_set(&mem->numainfo_events, 0); |
| 1541 | atomic_set(&mem->numainfo_updating, 0); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | /* |
| 1545 | * Selecting a node where we start reclaim from. Because what we need is just |
| 1546 | * reducing usage counter, start from anywhere is O,K. Considering |
| 1547 | * memory reclaim from current node, there are pros. and cons. |
| 1548 | * |
| 1549 | * Freeing memory from current node means freeing memory from a node which |
| 1550 | * we'll use or we've used. So, it may make LRU bad. And if several threads |
| 1551 | * hit limits, it will see a contention on a node. But freeing from remote |
| 1552 | * node means more costs for memory reclaim because of memory latency. |
| 1553 | * |
| 1554 | * Now, we use round-robin. Better algorithm is welcomed. |
| 1555 | */ |
| 1556 | int mem_cgroup_select_victim_node(struct mem_cgroup *mem) |
| 1557 | { |
| 1558 | int node; |
| 1559 | |
| 1560 | mem_cgroup_may_update_nodemask(mem); |
| 1561 | node = mem->last_scanned_node; |
| 1562 | |
| 1563 | node = next_node(node, mem->scan_nodes); |
| 1564 | if (node == MAX_NUMNODES) |
| 1565 | node = first_node(mem->scan_nodes); |
| 1566 | /* |
| 1567 | * We call this when we hit limit, not when pages are added to LRU. |
| 1568 | * No LRU may hold pages because all pages are UNEVICTABLE or |
| 1569 | * memcg is too small and all pages are not on LRU. In that case, |
| 1570 | * we use curret node. |
| 1571 | */ |
| 1572 | if (unlikely(node == MAX_NUMNODES)) |
| 1573 | node = numa_node_id(); |
| 1574 | |
| 1575 | mem->last_scanned_node = node; |
| 1576 | return node; |
| 1577 | } |
| 1578 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1579 | /* |
| 1580 | * Check all nodes whether it contains reclaimable pages or not. |
| 1581 | * For quick scan, we make use of scan_nodes. This will allow us to skip |
| 1582 | * unused nodes. But scan_nodes is lazily updated and may not cotain |
| 1583 | * enough new information. We need to do double check. |
| 1584 | */ |
| 1585 | bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap) |
| 1586 | { |
| 1587 | int nid; |
| 1588 | |
| 1589 | /* |
| 1590 | * quick check...making use of scan_node. |
| 1591 | * We can skip unused nodes. |
| 1592 | */ |
| 1593 | if (!nodes_empty(mem->scan_nodes)) { |
| 1594 | for (nid = first_node(mem->scan_nodes); |
| 1595 | nid < MAX_NUMNODES; |
| 1596 | nid = next_node(nid, mem->scan_nodes)) { |
| 1597 | |
| 1598 | if (test_mem_cgroup_node_reclaimable(mem, nid, noswap)) |
| 1599 | return true; |
| 1600 | } |
| 1601 | } |
| 1602 | /* |
| 1603 | * Check rest of nodes. |
| 1604 | */ |
| 1605 | for_each_node_state(nid, N_HIGH_MEMORY) { |
| 1606 | if (node_isset(nid, mem->scan_nodes)) |
| 1607 | continue; |
| 1608 | if (test_mem_cgroup_node_reclaimable(mem, nid, noswap)) |
| 1609 | return true; |
| 1610 | } |
| 1611 | return false; |
| 1612 | } |
| 1613 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1614 | #else |
| 1615 | int mem_cgroup_select_victim_node(struct mem_cgroup *mem) |
| 1616 | { |
| 1617 | return 0; |
| 1618 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1619 | |
| 1620 | bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap) |
| 1621 | { |
| 1622 | return test_mem_cgroup_node_reclaimable(mem, 0, noswap); |
| 1623 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1624 | #endif |
| 1625 | |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1626 | /* |
| 1627 | * Scan the hierarchy if needed to reclaim memory. We remember the last child |
| 1628 | * we reclaimed from, so that we don't end up penalizing one child extensively |
| 1629 | * based on its position in the children list. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1630 | * |
| 1631 | * root_mem is the original ancestor that we've been reclaim from. |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1632 | * |
| 1633 | * We give up and return to the caller when we visit root_mem twice. |
| 1634 | * (other groups can be removed while we're walking....) |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1635 | * |
| 1636 | * If shrink==true, for avoiding to free too much, this returns immedieately. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1637 | */ |
| 1638 | static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem, |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1639 | struct zone *zone, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1640 | gfp_t gfp_mask, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1641 | unsigned long reclaim_options, |
| 1642 | unsigned long *total_scanned) |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1643 | { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1644 | struct mem_cgroup *victim; |
| 1645 | int ret, total = 0; |
| 1646 | int loop = 0; |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 1647 | bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP; |
| 1648 | bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1649 | bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1650 | unsigned long excess; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1651 | unsigned long nr_scanned; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1652 | |
| 1653 | excess = res_counter_soft_limit_excess(&root_mem->res) >> PAGE_SHIFT; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1654 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1655 | /* If memsw_is_minimum==1, swap-out is of-no-use. */ |
KAMEZAWA Hiroyuki | 7ae534d | 2011-06-15 15:08:44 -0700 | [diff] [blame] | 1656 | if (!check_soft && root_mem->memsw_is_minimum) |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1657 | noswap = true; |
| 1658 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1659 | while (1) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1660 | victim = mem_cgroup_select_victim(root_mem); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1661 | if (victim == root_mem) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1662 | loop++; |
KAMEZAWA Hiroyuki | fbc29a2 | 2011-06-15 15:08:46 -0700 | [diff] [blame] | 1663 | /* |
| 1664 | * We are not draining per cpu cached charges during |
| 1665 | * soft limit reclaim because global reclaim doesn't |
| 1666 | * care about charges. It tries to free some memory and |
| 1667 | * charges will not give any. |
| 1668 | */ |
| 1669 | if (!check_soft && loop >= 1) |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 1670 | drain_all_stock_async(root_mem); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1671 | if (loop >= 2) { |
| 1672 | /* |
| 1673 | * If we have not been able to reclaim |
| 1674 | * anything, it might because there are |
| 1675 | * no reclaimable pages under this hierarchy |
| 1676 | */ |
| 1677 | if (!check_soft || !total) { |
| 1678 | css_put(&victim->css); |
| 1679 | break; |
| 1680 | } |
| 1681 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1682 | * We want to do more targeted reclaim. |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1683 | * excess >> 2 is not to excessive so as to |
| 1684 | * reclaim too much, nor too less that we keep |
| 1685 | * coming back to reclaim from this cgroup |
| 1686 | */ |
| 1687 | if (total >= (excess >> 2) || |
| 1688 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) { |
| 1689 | css_put(&victim->css); |
| 1690 | break; |
| 1691 | } |
| 1692 | } |
| 1693 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1694 | if (!mem_cgroup_reclaimable(victim, noswap)) { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1695 | /* this cgroup's local usage == 0 */ |
| 1696 | css_put(&victim->css); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1697 | continue; |
| 1698 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1699 | /* we use swappiness of local cgroup */ |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1700 | if (check_soft) { |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1701 | ret = mem_cgroup_shrink_node_zone(victim, gfp_mask, |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 1702 | noswap, zone, &nr_scanned); |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 1703 | *total_scanned += nr_scanned; |
| 1704 | } else |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1705 | ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 1706 | noswap); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1707 | css_put(&victim->css); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1708 | /* |
| 1709 | * At shrinking usage, we can't check we should stop here or |
| 1710 | * reclaim more. It's depends on callers. last_scanned_child |
| 1711 | * will work enough for keeping fairness under tree. |
| 1712 | */ |
| 1713 | if (shrink) |
| 1714 | return ret; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1715 | total += ret; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1716 | if (check_soft) { |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1717 | if (!res_counter_soft_limit_excess(&root_mem->res)) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 1718 | return total; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1719 | } else if (mem_cgroup_margin(root_mem)) |
Johannes Weiner | 4fd14eb | 2011-05-26 16:25:35 -0700 | [diff] [blame] | 1720 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1721 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1722 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1723 | } |
| 1724 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1725 | /* |
| 1726 | * Check OOM-Killer is already running under our hierarchy. |
| 1727 | * If someone is running, return false. |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame^] | 1728 | * Has to be called with memcg_oom_mutex |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1729 | */ |
| 1730 | static bool mem_cgroup_oom_lock(struct mem_cgroup *mem) |
| 1731 | { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame^] | 1732 | int lock_count = -1; |
| 1733 | struct mem_cgroup *iter, *failed = NULL; |
| 1734 | bool cond = true; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1735 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame^] | 1736 | for_each_mem_cgroup_tree_cond(iter, mem, cond) { |
| 1737 | bool locked = iter->oom_lock; |
| 1738 | |
| 1739 | iter->oom_lock = true; |
| 1740 | if (lock_count == -1) |
| 1741 | lock_count = iter->oom_lock; |
| 1742 | else if (lock_count != locked) { |
| 1743 | /* |
| 1744 | * this subtree of our hierarchy is already locked |
| 1745 | * so we cannot give a lock. |
| 1746 | */ |
| 1747 | lock_count = 0; |
| 1748 | failed = iter; |
| 1749 | cond = false; |
| 1750 | } |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1751 | } |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1752 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame^] | 1753 | if (!failed) |
| 1754 | goto done; |
| 1755 | |
| 1756 | /* |
| 1757 | * OK, we failed to lock the whole subtree so we have to clean up |
| 1758 | * what we set up to the failing subtree |
| 1759 | */ |
| 1760 | cond = true; |
| 1761 | for_each_mem_cgroup_tree_cond(iter, mem, cond) { |
| 1762 | if (iter == failed) { |
| 1763 | cond = false; |
| 1764 | continue; |
| 1765 | } |
| 1766 | iter->oom_lock = false; |
| 1767 | } |
| 1768 | done: |
| 1769 | return lock_count; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1770 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1771 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame^] | 1772 | /* |
| 1773 | * Has to be called with memcg_oom_mutex |
| 1774 | */ |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1775 | static int mem_cgroup_oom_unlock(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1776 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1777 | struct mem_cgroup *iter; |
| 1778 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame^] | 1779 | for_each_mem_cgroup_tree(iter, mem) |
| 1780 | iter->oom_lock = false; |
| 1781 | return 0; |
| 1782 | } |
| 1783 | |
| 1784 | static void mem_cgroup_mark_under_oom(struct mem_cgroup *mem) |
| 1785 | { |
| 1786 | struct mem_cgroup *iter; |
| 1787 | |
| 1788 | for_each_mem_cgroup_tree(iter, mem) |
| 1789 | atomic_inc(&iter->under_oom); |
| 1790 | } |
| 1791 | |
| 1792 | static void mem_cgroup_unmark_under_oom(struct mem_cgroup *mem) |
| 1793 | { |
| 1794 | struct mem_cgroup *iter; |
| 1795 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1796 | /* |
| 1797 | * When a new child is created while the hierarchy is under oom, |
| 1798 | * mem_cgroup_oom_lock() may not be called. We have to use |
| 1799 | * atomic_add_unless() here. |
| 1800 | */ |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1801 | for_each_mem_cgroup_tree(iter, mem) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame^] | 1802 | atomic_add_unless(&iter->under_oom, -1, 0); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1803 | } |
| 1804 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1805 | static DEFINE_MUTEX(memcg_oom_mutex); |
| 1806 | static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq); |
| 1807 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1808 | struct oom_wait_info { |
| 1809 | struct mem_cgroup *mem; |
| 1810 | wait_queue_t wait; |
| 1811 | }; |
| 1812 | |
| 1813 | static int memcg_oom_wake_function(wait_queue_t *wait, |
| 1814 | unsigned mode, int sync, void *arg) |
| 1815 | { |
| 1816 | struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg; |
| 1817 | struct oom_wait_info *oom_wait_info; |
| 1818 | |
| 1819 | oom_wait_info = container_of(wait, struct oom_wait_info, wait); |
| 1820 | |
| 1821 | if (oom_wait_info->mem == wake_mem) |
| 1822 | goto wakeup; |
| 1823 | /* if no hierarchy, no match */ |
| 1824 | if (!oom_wait_info->mem->use_hierarchy || !wake_mem->use_hierarchy) |
| 1825 | return 0; |
| 1826 | /* |
| 1827 | * Both of oom_wait_info->mem and wake_mem are stable under us. |
| 1828 | * Then we can use css_is_ancestor without taking care of RCU. |
| 1829 | */ |
| 1830 | if (!css_is_ancestor(&oom_wait_info->mem->css, &wake_mem->css) && |
| 1831 | !css_is_ancestor(&wake_mem->css, &oom_wait_info->mem->css)) |
| 1832 | return 0; |
| 1833 | |
| 1834 | wakeup: |
| 1835 | return autoremove_wake_function(wait, mode, sync, arg); |
| 1836 | } |
| 1837 | |
| 1838 | static void memcg_wakeup_oom(struct mem_cgroup *mem) |
| 1839 | { |
| 1840 | /* for filtering, pass "mem" as argument. */ |
| 1841 | __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem); |
| 1842 | } |
| 1843 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1844 | static void memcg_oom_recover(struct mem_cgroup *mem) |
| 1845 | { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame^] | 1846 | if (mem && atomic_read(&mem->under_oom)) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1847 | memcg_wakeup_oom(mem); |
| 1848 | } |
| 1849 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1850 | /* |
| 1851 | * try to call OOM killer. returns false if we should exit memory-reclaim loop. |
| 1852 | */ |
| 1853 | bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask) |
| 1854 | { |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1855 | struct oom_wait_info owait; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1856 | bool locked, need_to_kill; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1857 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1858 | owait.mem = mem; |
| 1859 | owait.wait.flags = 0; |
| 1860 | owait.wait.func = memcg_oom_wake_function; |
| 1861 | owait.wait.private = current; |
| 1862 | INIT_LIST_HEAD(&owait.wait.task_list); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1863 | need_to_kill = true; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame^] | 1864 | mem_cgroup_mark_under_oom(mem); |
| 1865 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1866 | /* At first, try to OOM lock hierarchy under mem.*/ |
| 1867 | mutex_lock(&memcg_oom_mutex); |
| 1868 | locked = mem_cgroup_oom_lock(mem); |
| 1869 | /* |
| 1870 | * Even if signal_pending(), we can't quit charge() loop without |
| 1871 | * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL |
| 1872 | * under OOM is always welcomed, use TASK_KILLABLE here. |
| 1873 | */ |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1874 | prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE); |
| 1875 | if (!locked || mem->oom_kill_disable) |
| 1876 | need_to_kill = false; |
| 1877 | if (locked) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1878 | mem_cgroup_oom_notify(mem); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1879 | mutex_unlock(&memcg_oom_mutex); |
| 1880 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1881 | if (need_to_kill) { |
| 1882 | finish_wait(&memcg_oom_waitq, &owait.wait); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1883 | mem_cgroup_out_of_memory(mem, mask); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1884 | } else { |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1885 | schedule(); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1886 | finish_wait(&memcg_oom_waitq, &owait.wait); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1887 | } |
| 1888 | mutex_lock(&memcg_oom_mutex); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame^] | 1889 | if (locked) |
| 1890 | mem_cgroup_oom_unlock(mem); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1891 | memcg_wakeup_oom(mem); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1892 | mutex_unlock(&memcg_oom_mutex); |
| 1893 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame^] | 1894 | mem_cgroup_unmark_under_oom(mem); |
| 1895 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1896 | if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current)) |
| 1897 | return false; |
| 1898 | /* Give chance to dying process */ |
| 1899 | schedule_timeout(1); |
| 1900 | return true; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1901 | } |
| 1902 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1903 | /* |
| 1904 | * Currently used to update mapped file statistics, but the routine can be |
| 1905 | * generalized to update other statistics as well. |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1906 | * |
| 1907 | * Notes: Race condition |
| 1908 | * |
| 1909 | * We usually use page_cgroup_lock() for accessing page_cgroup member but |
| 1910 | * it tends to be costly. But considering some conditions, we doesn't need |
| 1911 | * to do so _always_. |
| 1912 | * |
| 1913 | * Considering "charge", lock_page_cgroup() is not required because all |
| 1914 | * file-stat operations happen after a page is attached to radix-tree. There |
| 1915 | * are no race with "charge". |
| 1916 | * |
| 1917 | * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup |
| 1918 | * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even |
| 1919 | * if there are race with "uncharge". Statistics itself is properly handled |
| 1920 | * by flags. |
| 1921 | * |
| 1922 | * Considering "move", this is an only case we see a race. To make the race |
| 1923 | * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are |
| 1924 | * possibility of race condition. If there is, we take a lock. |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1925 | */ |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1926 | |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1927 | void mem_cgroup_update_page_stat(struct page *page, |
| 1928 | enum mem_cgroup_page_stat_item idx, int val) |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1929 | { |
| 1930 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1931 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 1932 | bool need_unlock = false; |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 1933 | unsigned long uninitialized_var(flags); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1934 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1935 | if (unlikely(!pc)) |
| 1936 | return; |
| 1937 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1938 | rcu_read_lock(); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1939 | mem = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1940 | if (unlikely(!mem || !PageCgroupUsed(pc))) |
| 1941 | goto out; |
| 1942 | /* pc->mem_cgroup is unstable ? */ |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 1943 | if (unlikely(mem_cgroup_stealed(mem)) || PageTransHuge(page)) { |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1944 | /* take a lock against to access pc->mem_cgroup */ |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 1945 | move_lock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1946 | need_unlock = true; |
| 1947 | mem = pc->mem_cgroup; |
| 1948 | if (!mem || !PageCgroupUsed(pc)) |
| 1949 | goto out; |
| 1950 | } |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1951 | |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1952 | switch (idx) { |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1953 | case MEMCG_NR_FILE_MAPPED: |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1954 | if (val > 0) |
| 1955 | SetPageCgroupFileMapped(pc); |
| 1956 | else if (!page_mapped(page)) |
KAMEZAWA Hiroyuki | 0c270f8 | 2010-10-27 15:33:39 -0700 | [diff] [blame] | 1957 | ClearPageCgroupFileMapped(pc); |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1958 | idx = MEM_CGROUP_STAT_FILE_MAPPED; |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1959 | break; |
| 1960 | default: |
| 1961 | BUG(); |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 1962 | } |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1963 | |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1964 | this_cpu_add(mem->stat->count[idx], val); |
| 1965 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1966 | out: |
| 1967 | if (unlikely(need_unlock)) |
KAMEZAWA Hiroyuki | dbd4ea7 | 2011-01-13 15:47:38 -0800 | [diff] [blame] | 1968 | move_unlock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1969 | rcu_read_unlock(); |
| 1970 | return; |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1971 | } |
Greg Thelen | 2a7106f | 2011-01-13 15:47:37 -0800 | [diff] [blame] | 1972 | EXPORT_SYMBOL(mem_cgroup_update_page_stat); |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 1973 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1974 | /* |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1975 | * size of first charge trial. "32" comes from vmscan.c's magic value. |
| 1976 | * TODO: maybe necessary to use big numbers in big irons. |
| 1977 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1978 | #define CHARGE_BATCH 32U |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1979 | struct memcg_stock_pcp { |
| 1980 | struct mem_cgroup *cached; /* this never be root cgroup */ |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 1981 | unsigned int nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1982 | struct work_struct work; |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 1983 | unsigned long flags; |
| 1984 | #define FLUSHING_CACHED_CHARGE (0) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1985 | }; |
| 1986 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 1987 | static DEFINE_MUTEX(percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1988 | |
| 1989 | /* |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 1990 | * Try to consume stocked charge on this cpu. If success, one page is consumed |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 1991 | * from local stock and true is returned. If the stock is 0 or charges from a |
| 1992 | * cgroup which is not current target, returns false. This stock will be |
| 1993 | * refilled. |
| 1994 | */ |
| 1995 | static bool consume_stock(struct mem_cgroup *mem) |
| 1996 | { |
| 1997 | struct memcg_stock_pcp *stock; |
| 1998 | bool ret = true; |
| 1999 | |
| 2000 | stock = &get_cpu_var(memcg_stock); |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2001 | if (mem == stock->cached && stock->nr_pages) |
| 2002 | stock->nr_pages--; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2003 | else /* need to call res_counter_charge */ |
| 2004 | ret = false; |
| 2005 | put_cpu_var(memcg_stock); |
| 2006 | return ret; |
| 2007 | } |
| 2008 | |
| 2009 | /* |
| 2010 | * Returns stocks cached in percpu to res_counter and reset cached information. |
| 2011 | */ |
| 2012 | static void drain_stock(struct memcg_stock_pcp *stock) |
| 2013 | { |
| 2014 | struct mem_cgroup *old = stock->cached; |
| 2015 | |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2016 | if (stock->nr_pages) { |
| 2017 | unsigned long bytes = stock->nr_pages * PAGE_SIZE; |
| 2018 | |
| 2019 | res_counter_uncharge(&old->res, bytes); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2020 | if (do_swap_account) |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2021 | res_counter_uncharge(&old->memsw, bytes); |
| 2022 | stock->nr_pages = 0; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2023 | } |
| 2024 | stock->cached = NULL; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | /* |
| 2028 | * This must be called under preempt disabled or must be called by |
| 2029 | * a thread which is pinned to local cpu. |
| 2030 | */ |
| 2031 | static void drain_local_stock(struct work_struct *dummy) |
| 2032 | { |
| 2033 | struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock); |
| 2034 | drain_stock(stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2035 | clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | /* |
| 2039 | * 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] | 2040 | * This will be consumed by consume_stock() function, later. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2041 | */ |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2042 | static void refill_stock(struct mem_cgroup *mem, unsigned int nr_pages) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2043 | { |
| 2044 | struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock); |
| 2045 | |
| 2046 | if (stock->cached != mem) { /* reset if necessary */ |
| 2047 | drain_stock(stock); |
| 2048 | stock->cached = mem; |
| 2049 | } |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2050 | stock->nr_pages += nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2051 | put_cpu_var(memcg_stock); |
| 2052 | } |
| 2053 | |
| 2054 | /* |
| 2055 | * Tries to drain stocked charges in other cpus. This function is asynchronous |
| 2056 | * and just put a work per cpu for draining localy on each cpu. Caller can |
| 2057 | * expects some charges will be back to res_counter later but cannot wait for |
| 2058 | * it. |
| 2059 | */ |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2060 | static void drain_all_stock_async(struct mem_cgroup *root_mem) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2061 | { |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2062 | int cpu, curcpu; |
| 2063 | /* |
| 2064 | * If someone calls draining, avoid adding more kworker runs. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2065 | */ |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2066 | if (!mutex_trylock(&percpu_charge_mutex)) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2067 | return; |
| 2068 | /* Notify other cpus that system-wide "drain" is running */ |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2069 | get_online_cpus(); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2070 | /* |
| 2071 | * Get a hint for avoiding draining charges on the current cpu, |
| 2072 | * which must be exhausted by our charging. It is not required that |
| 2073 | * this be a precise check, so we use raw_smp_processor_id() instead of |
| 2074 | * getcpu()/putcpu(). |
| 2075 | */ |
| 2076 | curcpu = raw_smp_processor_id(); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2077 | for_each_online_cpu(cpu) { |
| 2078 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2079 | struct mem_cgroup *mem; |
| 2080 | |
| 2081 | if (cpu == curcpu) |
| 2082 | continue; |
| 2083 | |
| 2084 | mem = stock->cached; |
| 2085 | if (!mem) |
| 2086 | continue; |
| 2087 | if (mem != root_mem) { |
| 2088 | if (!root_mem->use_hierarchy) |
| 2089 | continue; |
| 2090 | /* check whether "mem" is under tree of "root_mem" */ |
| 2091 | if (!css_is_ancestor(&mem->css, &root_mem->css)) |
| 2092 | continue; |
| 2093 | } |
| 2094 | if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) |
| 2095 | schedule_work_on(cpu, &stock->work); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2096 | } |
| 2097 | put_online_cpus(); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2098 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2099 | /* We don't wait for flush_work */ |
| 2100 | } |
| 2101 | |
| 2102 | /* This is a synchronous drain interface. */ |
| 2103 | static void drain_all_stock_sync(void) |
| 2104 | { |
| 2105 | /* called when force_empty is called */ |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2106 | mutex_lock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2107 | schedule_on_each_cpu(drain_local_stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2108 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2109 | } |
| 2110 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2111 | /* |
| 2112 | * This function drains percpu counter value from DEAD cpu and |
| 2113 | * move it to local cpu. Note that this function can be preempted. |
| 2114 | */ |
| 2115 | static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *mem, int cpu) |
| 2116 | { |
| 2117 | int i; |
| 2118 | |
| 2119 | spin_lock(&mem->pcp_counter_lock); |
| 2120 | for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 2121 | long x = per_cpu(mem->stat->count[i], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2122 | |
| 2123 | per_cpu(mem->stat->count[i], cpu) = 0; |
| 2124 | mem->nocpu_base.count[i] += x; |
| 2125 | } |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2126 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) { |
| 2127 | unsigned long x = per_cpu(mem->stat->events[i], cpu); |
| 2128 | |
| 2129 | per_cpu(mem->stat->events[i], cpu) = 0; |
| 2130 | mem->nocpu_base.events[i] += x; |
| 2131 | } |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2132 | /* need to clear ON_MOVE value, works as a kind of lock. */ |
| 2133 | per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0; |
| 2134 | spin_unlock(&mem->pcp_counter_lock); |
| 2135 | } |
| 2136 | |
| 2137 | static void synchronize_mem_cgroup_on_move(struct mem_cgroup *mem, int cpu) |
| 2138 | { |
| 2139 | int idx = MEM_CGROUP_ON_MOVE; |
| 2140 | |
| 2141 | spin_lock(&mem->pcp_counter_lock); |
| 2142 | per_cpu(mem->stat->count[idx], cpu) = mem->nocpu_base.count[idx]; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2143 | spin_unlock(&mem->pcp_counter_lock); |
| 2144 | } |
| 2145 | |
| 2146 | static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb, |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2147 | unsigned long action, |
| 2148 | void *hcpu) |
| 2149 | { |
| 2150 | int cpu = (unsigned long)hcpu; |
| 2151 | struct memcg_stock_pcp *stock; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2152 | struct mem_cgroup *iter; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2153 | |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2154 | if ((action == CPU_ONLINE)) { |
| 2155 | for_each_mem_cgroup_all(iter) |
| 2156 | synchronize_mem_cgroup_on_move(iter, cpu); |
| 2157 | return NOTIFY_OK; |
| 2158 | } |
| 2159 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2160 | if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2161 | return NOTIFY_OK; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2162 | |
| 2163 | for_each_mem_cgroup_all(iter) |
| 2164 | mem_cgroup_drain_pcp_counter(iter, cpu); |
| 2165 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2166 | stock = &per_cpu(memcg_stock, cpu); |
| 2167 | drain_stock(stock); |
| 2168 | return NOTIFY_OK; |
| 2169 | } |
| 2170 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2171 | |
| 2172 | /* See __mem_cgroup_try_charge() for details */ |
| 2173 | enum { |
| 2174 | CHARGE_OK, /* success */ |
| 2175 | CHARGE_RETRY, /* need to retry but retry is not bad */ |
| 2176 | CHARGE_NOMEM, /* we can't do more. return -ENOMEM */ |
| 2177 | CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */ |
| 2178 | CHARGE_OOM_DIE, /* the current is killed because of OOM */ |
| 2179 | }; |
| 2180 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2181 | static int mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask, |
| 2182 | unsigned int nr_pages, bool oom_check) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2183 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2184 | unsigned long csize = nr_pages * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2185 | struct mem_cgroup *mem_over_limit; |
| 2186 | struct res_counter *fail_res; |
| 2187 | unsigned long flags = 0; |
| 2188 | int ret; |
| 2189 | |
| 2190 | ret = res_counter_charge(&mem->res, csize, &fail_res); |
| 2191 | |
| 2192 | if (likely(!ret)) { |
| 2193 | if (!do_swap_account) |
| 2194 | return CHARGE_OK; |
| 2195 | ret = res_counter_charge(&mem->memsw, csize, &fail_res); |
| 2196 | if (likely(!ret)) |
| 2197 | return CHARGE_OK; |
| 2198 | |
KAMEZAWA Hiroyuki | 01c88e2 | 2011-01-25 15:07:27 -0800 | [diff] [blame] | 2199 | res_counter_uncharge(&mem->res, csize); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2200 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw); |
| 2201 | flags |= MEM_CGROUP_RECLAIM_NOSWAP; |
| 2202 | } else |
| 2203 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, res); |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2204 | /* |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2205 | * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch |
| 2206 | * of regular pages (CHARGE_BATCH), or a single regular page (1). |
Johannes Weiner | 9221edb | 2011-02-01 15:52:42 -0800 | [diff] [blame] | 2207 | * |
| 2208 | * Never reclaim on behalf of optional batching, retry with a |
| 2209 | * single page instead. |
| 2210 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2211 | if (nr_pages == CHARGE_BATCH) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2212 | return CHARGE_RETRY; |
| 2213 | |
| 2214 | if (!(gfp_mask & __GFP_WAIT)) |
| 2215 | return CHARGE_WOULDBLOCK; |
| 2216 | |
| 2217 | ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 2218 | gfp_mask, flags, NULL); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2219 | if (mem_cgroup_margin(mem_over_limit) >= nr_pages) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2220 | return CHARGE_RETRY; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2221 | /* |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 2222 | * Even though the limit is exceeded at this point, reclaim |
| 2223 | * may have been able to free some pages. Retry the charge |
| 2224 | * before killing the task. |
| 2225 | * |
| 2226 | * Only for regular pages, though: huge pages are rather |
| 2227 | * unlikely to succeed so close to the limit, and we fall back |
| 2228 | * to regular pages anyway in case of failure. |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2229 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2230 | if (nr_pages == 1 && ret) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2231 | return CHARGE_RETRY; |
| 2232 | |
| 2233 | /* |
| 2234 | * At task move, charge accounts can be doubly counted. So, it's |
| 2235 | * better to wait until the end of task_move if something is going on. |
| 2236 | */ |
| 2237 | if (mem_cgroup_wait_acct_move(mem_over_limit)) |
| 2238 | return CHARGE_RETRY; |
| 2239 | |
| 2240 | /* If we don't need to call oom-killer at el, return immediately */ |
| 2241 | if (!oom_check) |
| 2242 | return CHARGE_NOMEM; |
| 2243 | /* check OOM */ |
| 2244 | if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask)) |
| 2245 | return CHARGE_OOM_DIE; |
| 2246 | |
| 2247 | return CHARGE_RETRY; |
| 2248 | } |
| 2249 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2250 | /* |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2251 | * Unlike exported interface, "oom" parameter is added. if oom==true, |
| 2252 | * oom-killer can be invoked. |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2253 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2254 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2255 | gfp_t gfp_mask, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2256 | unsigned int nr_pages, |
| 2257 | struct mem_cgroup **memcg, |
| 2258 | bool oom) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2259 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2260 | unsigned int batch = max(CHARGE_BATCH, nr_pages); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2261 | int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
| 2262 | struct mem_cgroup *mem = NULL; |
| 2263 | int ret; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2264 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2265 | /* |
| 2266 | * Unlike gloval-vm's OOM-kill, we're not in memory shortage |
| 2267 | * in system level. So, allow to go ahead dying process in addition to |
| 2268 | * MEMDIE process. |
| 2269 | */ |
| 2270 | if (unlikely(test_thread_flag(TIF_MEMDIE) |
| 2271 | || fatal_signal_pending(current))) |
| 2272 | goto bypass; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2273 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2274 | /* |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2275 | * We always charge the cgroup the mm_struct belongs to. |
| 2276 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2277 | * thread group leader migrates. It's possible that mm is not |
| 2278 | * set, if so charge the init_mm (happens for pagecache usage). |
| 2279 | */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2280 | if (!*memcg && !mm) |
| 2281 | goto bypass; |
| 2282 | again: |
| 2283 | if (*memcg) { /* css should be a valid one */ |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2284 | mem = *memcg; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2285 | VM_BUG_ON(css_is_removed(&mem->css)); |
| 2286 | if (mem_cgroup_is_root(mem)) |
| 2287 | goto done; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2288 | if (nr_pages == 1 && consume_stock(mem)) |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2289 | goto done; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2290 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2291 | } else { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2292 | struct task_struct *p; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2293 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2294 | rcu_read_lock(); |
| 2295 | p = rcu_dereference(mm->owner); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2296 | /* |
KAMEZAWA Hiroyuki | ebb76ce | 2010-12-29 14:07:11 -0800 | [diff] [blame] | 2297 | * Because we don't have task_lock(), "p" can exit. |
| 2298 | * In that case, "mem" can point to root or p can be NULL with |
| 2299 | * race with swapoff. Then, we have small risk of mis-accouning. |
| 2300 | * But such kind of mis-account by race always happens because |
| 2301 | * we don't have cgroup_mutex(). It's overkill and we allo that |
| 2302 | * small race, here. |
| 2303 | * (*) swapoff at el will charge against mm-struct not against |
| 2304 | * task-struct. So, mm->owner can be NULL. |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2305 | */ |
| 2306 | mem = mem_cgroup_from_task(p); |
KAMEZAWA Hiroyuki | ebb76ce | 2010-12-29 14:07:11 -0800 | [diff] [blame] | 2307 | if (!mem || mem_cgroup_is_root(mem)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2308 | rcu_read_unlock(); |
| 2309 | goto done; |
| 2310 | } |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2311 | if (nr_pages == 1 && consume_stock(mem)) { |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2312 | /* |
| 2313 | * It seems dagerous to access memcg without css_get(). |
| 2314 | * But considering how consume_stok works, it's not |
| 2315 | * necessary. If consume_stock success, some charges |
| 2316 | * from this memcg are cached on this cpu. So, we |
| 2317 | * don't need to call css_get()/css_tryget() before |
| 2318 | * calling consume_stock(). |
| 2319 | */ |
| 2320 | rcu_read_unlock(); |
| 2321 | goto done; |
| 2322 | } |
| 2323 | /* after here, we may be blocked. we need to get refcnt */ |
| 2324 | if (!css_tryget(&mem->css)) { |
| 2325 | rcu_read_unlock(); |
| 2326 | goto again; |
| 2327 | } |
| 2328 | rcu_read_unlock(); |
| 2329 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2330 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2331 | do { |
| 2332 | bool oom_check; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2333 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2334 | /* If killed, bypass charge */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2335 | if (fatal_signal_pending(current)) { |
| 2336 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2337 | goto bypass; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2338 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2339 | |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2340 | oom_check = false; |
| 2341 | if (oom && !nr_oom_retries) { |
| 2342 | oom_check = true; |
| 2343 | nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
| 2344 | } |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 2345 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2346 | ret = mem_cgroup_do_charge(mem, gfp_mask, batch, oom_check); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2347 | switch (ret) { |
| 2348 | case CHARGE_OK: |
| 2349 | break; |
| 2350 | case CHARGE_RETRY: /* not in OOM situation but retry */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2351 | batch = nr_pages; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2352 | css_put(&mem->css); |
| 2353 | mem = NULL; |
| 2354 | goto again; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2355 | case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2356 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2357 | goto nomem; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2358 | case CHARGE_NOMEM: /* OOM routine works */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2359 | if (!oom) { |
| 2360 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2361 | goto nomem; |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2362 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2363 | /* If oom, we never return -ENOMEM */ |
| 2364 | nr_oom_retries--; |
| 2365 | break; |
| 2366 | case CHARGE_OOM_DIE: /* Killed by OOM Killer */ |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2367 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2368 | goto bypass; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2369 | } |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2370 | } while (ret != CHARGE_OK); |
| 2371 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2372 | if (batch > nr_pages) |
| 2373 | refill_stock(mem, batch - nr_pages); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2374 | css_put(&mem->css); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2375 | done: |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2376 | *memcg = mem; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2377 | return 0; |
| 2378 | nomem: |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2379 | *memcg = NULL; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2380 | return -ENOMEM; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2381 | bypass: |
| 2382 | *memcg = NULL; |
| 2383 | return 0; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2384 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2385 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2386 | /* |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2387 | * Somemtimes we have to undo a charge we got by try_charge(). |
| 2388 | * This function is for that and do uncharge, put css's refcnt. |
| 2389 | * gotten by try_charge(). |
| 2390 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2391 | static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem, |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2392 | unsigned int nr_pages) |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2393 | { |
| 2394 | if (!mem_cgroup_is_root(mem)) { |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2395 | unsigned long bytes = nr_pages * PAGE_SIZE; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2396 | |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2397 | res_counter_uncharge(&mem->res, bytes); |
| 2398 | if (do_swap_account) |
| 2399 | res_counter_uncharge(&mem->memsw, bytes); |
| 2400 | } |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | /* |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2404 | * A helper function to get mem_cgroup from ID. must be called under |
| 2405 | * rcu_read_lock(). The caller must check css_is_removed() or some if |
| 2406 | * it's concern. (dropping refcnt from swap can be called against removed |
| 2407 | * memcg.) |
| 2408 | */ |
| 2409 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) |
| 2410 | { |
| 2411 | struct cgroup_subsys_state *css; |
| 2412 | |
| 2413 | /* ID 0 is unused ID */ |
| 2414 | if (!id) |
| 2415 | return NULL; |
| 2416 | css = css_lookup(&mem_cgroup_subsys, id); |
| 2417 | if (!css) |
| 2418 | return NULL; |
| 2419 | return container_of(css, struct mem_cgroup, css); |
| 2420 | } |
| 2421 | |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2422 | struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2423 | { |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2424 | struct mem_cgroup *mem = NULL; |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2425 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2426 | unsigned short id; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2427 | swp_entry_t ent; |
| 2428 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2429 | VM_BUG_ON(!PageLocked(page)); |
| 2430 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2431 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | c0bd3f63 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2432 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2433 | if (PageCgroupUsed(pc)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2434 | mem = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2435 | if (mem && !css_tryget(&mem->css)) |
| 2436 | mem = NULL; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2437 | } else if (PageSwapCache(page)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2438 | ent.val = page_private(page); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2439 | id = lookup_swap_cgroup(ent); |
| 2440 | rcu_read_lock(); |
| 2441 | mem = mem_cgroup_lookup(id); |
| 2442 | if (mem && !css_tryget(&mem->css)) |
| 2443 | mem = NULL; |
| 2444 | rcu_read_unlock(); |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2445 | } |
Daisuke Nishimura | c0bd3f63 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 2446 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2447 | return mem; |
| 2448 | } |
| 2449 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2450 | static void __mem_cgroup_commit_charge(struct mem_cgroup *mem, |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2451 | struct page *page, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2452 | unsigned int nr_pages, |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2453 | struct page_cgroup *pc, |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2454 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2455 | { |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2456 | lock_page_cgroup(pc); |
| 2457 | if (unlikely(PageCgroupUsed(pc))) { |
| 2458 | unlock_page_cgroup(pc); |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2459 | __mem_cgroup_cancel_charge(mem, nr_pages); |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2460 | return; |
| 2461 | } |
| 2462 | /* |
| 2463 | * we don't need page_cgroup_lock about tail pages, becase they are not |
| 2464 | * accessed by any other context at this point. |
| 2465 | */ |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2466 | pc->mem_cgroup = mem; |
KAMEZAWA Hiroyuki | 261fb61 | 2009-09-23 15:56:33 -0700 | [diff] [blame] | 2467 | /* |
| 2468 | * We access a page_cgroup asynchronously without lock_page_cgroup(). |
| 2469 | * Especially when a page_cgroup is taken from a page, pc->mem_cgroup |
| 2470 | * is accessed after testing USED bit. To make pc->mem_cgroup visible |
| 2471 | * before USED bit, we need memory barrier here. |
| 2472 | * See mem_cgroup_add_lru_list(), etc. |
| 2473 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2474 | smp_wmb(); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 2475 | switch (ctype) { |
| 2476 | case MEM_CGROUP_CHARGE_TYPE_CACHE: |
| 2477 | case MEM_CGROUP_CHARGE_TYPE_SHMEM: |
| 2478 | SetPageCgroupCache(pc); |
| 2479 | SetPageCgroupUsed(pc); |
| 2480 | break; |
| 2481 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
| 2482 | ClearPageCgroupCache(pc); |
| 2483 | SetPageCgroupUsed(pc); |
| 2484 | break; |
| 2485 | default: |
| 2486 | break; |
| 2487 | } |
Hugh Dickins | 3be91277 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2488 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2489 | mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), nr_pages); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2490 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 430e4863 | 2010-03-10 15:22:30 -0800 | [diff] [blame] | 2491 | /* |
| 2492 | * "charge_statistics" updated event counter. Then, check it. |
| 2493 | * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree. |
| 2494 | * if they exceeds softlimit. |
| 2495 | */ |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2496 | memcg_check_events(mem, page); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2497 | } |
| 2498 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2499 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 2500 | |
| 2501 | #define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\ |
| 2502 | (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION)) |
| 2503 | /* |
| 2504 | * Because tail pages are not marked as "used", set it. We're under |
| 2505 | * zone->lru_lock, 'splitting on pmd' and compund_lock. |
| 2506 | */ |
| 2507 | void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail) |
| 2508 | { |
| 2509 | struct page_cgroup *head_pc = lookup_page_cgroup(head); |
| 2510 | struct page_cgroup *tail_pc = lookup_page_cgroup(tail); |
| 2511 | unsigned long flags; |
| 2512 | |
KAMEZAWA Hiroyuki | 3d37c4a | 2011-01-25 15:07:28 -0800 | [diff] [blame] | 2513 | if (mem_cgroup_disabled()) |
| 2514 | return; |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2515 | /* |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2516 | * We have no races with charge/uncharge but will have races with |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2517 | * page state accounting. |
| 2518 | */ |
| 2519 | move_lock_page_cgroup(head_pc, &flags); |
| 2520 | |
| 2521 | tail_pc->mem_cgroup = head_pc->mem_cgroup; |
| 2522 | smp_wmb(); /* see __commit_charge() */ |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2523 | if (PageCgroupAcctLRU(head_pc)) { |
| 2524 | enum lru_list lru; |
| 2525 | struct mem_cgroup_per_zone *mz; |
| 2526 | |
| 2527 | /* |
| 2528 | * LRU flags cannot be copied because we need to add tail |
| 2529 | *.page to LRU by generic call and our hook will be called. |
| 2530 | * We hold lru_lock, then, reduce counter directly. |
| 2531 | */ |
| 2532 | lru = page_lru(head); |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 2533 | mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head); |
KAMEZAWA Hiroyuki | ece35ca | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2534 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
| 2535 | } |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2536 | tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT; |
| 2537 | move_unlock_page_cgroup(head_pc, &flags); |
| 2538 | } |
| 2539 | #endif |
| 2540 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2541 | /** |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2542 | * mem_cgroup_move_account - move account of the page |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2543 | * @page: the page |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2544 | * @nr_pages: number of regular pages (>1 for huge pages) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2545 | * @pc: page_cgroup of the page. |
| 2546 | * @from: mem_cgroup which the page is moved from. |
| 2547 | * @to: mem_cgroup which the page is moved to. @from != @to. |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2548 | * @uncharge: whether we should call uncharge and css_put against @from. |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2549 | * |
| 2550 | * The caller must confirm following. |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2551 | * - page is not on LRU (isolate_page() is useful.) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2552 | * - compound_lock is held when nr_pages > 1 |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2553 | * |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2554 | * This function doesn't do "charge" nor css_get to new cgroup. It should be |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2555 | * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2556 | * true, this function does "uncharge" from old cgroup, but it doesn't if |
| 2557 | * @uncharge is false, so a caller should do "uncharge". |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2558 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2559 | static int mem_cgroup_move_account(struct page *page, |
| 2560 | unsigned int nr_pages, |
| 2561 | struct page_cgroup *pc, |
| 2562 | struct mem_cgroup *from, |
| 2563 | struct mem_cgroup *to, |
| 2564 | bool uncharge) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2565 | { |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2566 | unsigned long flags; |
| 2567 | int ret; |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2568 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2569 | VM_BUG_ON(from == to); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2570 | VM_BUG_ON(PageLRU(page)); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2571 | /* |
| 2572 | * The page is isolated from LRU. So, collapse function |
| 2573 | * will not handle this page. But page splitting can happen. |
| 2574 | * Do this check under compound_page_lock(). The caller should |
| 2575 | * hold it. |
| 2576 | */ |
| 2577 | ret = -EBUSY; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2578 | if (nr_pages > 1 && !PageTransHuge(page)) |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2579 | goto out; |
| 2580 | |
| 2581 | lock_page_cgroup(pc); |
| 2582 | |
| 2583 | ret = -EINVAL; |
| 2584 | if (!PageCgroupUsed(pc) || pc->mem_cgroup != from) |
| 2585 | goto unlock; |
| 2586 | |
| 2587 | move_lock_page_cgroup(pc, &flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2588 | |
KAMEZAWA Hiroyuki | 8725d54 | 2010-04-06 14:35:05 -0700 | [diff] [blame] | 2589 | if (PageCgroupFileMapped(pc)) { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 2590 | /* Update mapped_file data for mem_cgroup */ |
| 2591 | preempt_disable(); |
| 2592 | __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 2593 | __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); |
| 2594 | preempt_enable(); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2595 | } |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2596 | mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2597 | if (uncharge) |
| 2598 | /* This is not "cancel", but cancel_charge does all we need. */ |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2599 | __mem_cgroup_cancel_charge(from, nr_pages); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2600 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2601 | /* caller should have done css_get */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2602 | pc->mem_cgroup = to; |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2603 | mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2604 | /* |
| 2605 | * We charges against "to" which may not have any tasks. Then, "to" |
| 2606 | * can be under rmdir(). But in current implementation, caller of |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 2607 | * this function is just force_empty() and move charge, so it's |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2608 | * guaranteed that "to" is never removed. So, we don't check rmdir |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 2609 | * status here. |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2610 | */ |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2611 | move_unlock_page_cgroup(pc, &flags); |
| 2612 | ret = 0; |
| 2613 | unlock: |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2614 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 2615 | /* |
| 2616 | * check events |
| 2617 | */ |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2618 | memcg_check_events(to, page); |
| 2619 | memcg_check_events(from, page); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 2620 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2621 | return ret; |
| 2622 | } |
| 2623 | |
| 2624 | /* |
| 2625 | * move charges to its parent. |
| 2626 | */ |
| 2627 | |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 2628 | static int mem_cgroup_move_parent(struct page *page, |
| 2629 | struct page_cgroup *pc, |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2630 | struct mem_cgroup *child, |
| 2631 | gfp_t gfp_mask) |
| 2632 | { |
| 2633 | struct cgroup *cg = child->css.cgroup; |
| 2634 | struct cgroup *pcg = cg->parent; |
| 2635 | struct mem_cgroup *parent; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2636 | unsigned int nr_pages; |
Andrew Morton | 4be4489 | 2011-03-23 16:42:39 -0700 | [diff] [blame] | 2637 | unsigned long uninitialized_var(flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2638 | int ret; |
| 2639 | |
| 2640 | /* Is ROOT ? */ |
| 2641 | if (!pcg) |
| 2642 | return -EINVAL; |
| 2643 | |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2644 | ret = -EBUSY; |
| 2645 | if (!get_page_unless_zero(page)) |
| 2646 | goto out; |
| 2647 | if (isolate_lru_page(page)) |
| 2648 | goto put; |
KAMEZAWA Hiroyuki | 52dbb90 | 2011-01-25 15:07:29 -0800 | [diff] [blame] | 2649 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2650 | nr_pages = hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2651 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2652 | parent = mem_cgroup_from_cont(pcg); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2653 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2654 | if (ret || !parent) |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2655 | goto put_back; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2656 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2657 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2658 | flags = compound_lock_irqsave(page); |
| 2659 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2660 | ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 2661 | if (ret) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2662 | __mem_cgroup_cancel_charge(parent, nr_pages); |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 2663 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2664 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 2665 | compound_unlock_irqrestore(page, flags); |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 2666 | put_back: |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2667 | putback_lru_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2668 | put: |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 2669 | put_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 2670 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2671 | return ret; |
| 2672 | } |
| 2673 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2674 | /* |
| 2675 | * Charge the memory controller for page usage. |
| 2676 | * Return |
| 2677 | * 0 if the charge was successful |
| 2678 | * < 0 if the cgroup is over its limit |
| 2679 | */ |
| 2680 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2681 | gfp_t gfp_mask, enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2682 | { |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2683 | struct mem_cgroup *mem = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2684 | unsigned int nr_pages = 1; |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 2685 | struct page_cgroup *pc; |
| 2686 | bool oom = true; |
| 2687 | int ret; |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2688 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2689 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2690 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2691 | VM_BUG_ON(!PageTransHuge(page)); |
Johannes Weiner | 8493ae4 | 2011-02-01 15:52:44 -0800 | [diff] [blame] | 2692 | /* |
| 2693 | * Never OOM-kill a process for a huge page. The |
| 2694 | * fault handler will fall back to regular pages. |
| 2695 | */ |
| 2696 | oom = false; |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2697 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2698 | |
| 2699 | pc = lookup_page_cgroup(page); |
Johannes Weiner | af4a662 | 2011-03-23 16:42:24 -0700 | [diff] [blame] | 2700 | BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */ |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2701 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2702 | ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &mem, oom); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2703 | if (ret || !mem) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2704 | return ret; |
| 2705 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2706 | __mem_cgroup_commit_charge(mem, page, nr_pages, pc, ctype); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2707 | return 0; |
| 2708 | } |
| 2709 | |
| 2710 | int mem_cgroup_newpage_charge(struct page *page, |
| 2711 | struct mm_struct *mm, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2712 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2713 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 2714 | return 0; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2715 | /* |
| 2716 | * If already mapped, we don't have to account. |
| 2717 | * If page cache, page->mapping has address_space. |
| 2718 | * But page->mapping may have out-of-use anon_vma pointer, |
| 2719 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping |
| 2720 | * is NULL. |
| 2721 | */ |
| 2722 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) |
| 2723 | return 0; |
| 2724 | if (unlikely(!mm)) |
| 2725 | mm = &init_mm; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2726 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2727 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 2728 | } |
| 2729 | |
Daisuke Nishimura | 83aae4c7 | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2730 | static void |
| 2731 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 2732 | enum charge_type ctype); |
| 2733 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2734 | static void |
| 2735 | __mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *mem, |
| 2736 | enum charge_type ctype) |
| 2737 | { |
| 2738 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 2739 | /* |
| 2740 | * In some case, SwapCache, FUSE(splice_buf->radixtree), the page |
| 2741 | * is already on LRU. It means the page may on some other page_cgroup's |
| 2742 | * LRU. Take care of it. |
| 2743 | */ |
| 2744 | mem_cgroup_lru_del_before_commit(page); |
| 2745 | __mem_cgroup_commit_charge(mem, page, 1, pc, ctype); |
| 2746 | mem_cgroup_lru_add_after_commit(page); |
| 2747 | return; |
| 2748 | } |
| 2749 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 2750 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 2751 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2752 | { |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2753 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2754 | int ret; |
| 2755 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2756 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 2757 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2758 | if (PageCompound(page)) |
| 2759 | return 0; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2760 | /* |
| 2761 | * Corner case handling. This is called from add_to_page_cache() |
| 2762 | * in usual. But some FS (shmem) precharges this page before calling it |
| 2763 | * and call add_to_page_cache() with GFP_NOWAIT. |
| 2764 | * |
| 2765 | * For GFP_NOWAIT case, the page may be pre-charged before calling |
| 2766 | * add_to_page_cache(). (See shmem.c) check it here and avoid to call |
| 2767 | * charge twice. (It works but has to pay a bit larger cost.) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2768 | * And when the page is SwapCache, it should take swap information |
| 2769 | * into account. This is under lock_page() now. |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2770 | */ |
| 2771 | if (!(gfp_mask & __GFP_WAIT)) { |
| 2772 | struct page_cgroup *pc; |
| 2773 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2774 | pc = lookup_page_cgroup(page); |
| 2775 | if (!pc) |
| 2776 | return 0; |
| 2777 | lock_page_cgroup(pc); |
| 2778 | if (PageCgroupUsed(pc)) { |
| 2779 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2780 | return 0; |
| 2781 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2782 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2783 | } |
| 2784 | |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2785 | if (unlikely(!mm)) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2786 | mm = &init_mm; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 2787 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2788 | if (page_is_file_cache(page)) { |
| 2789 | ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &mem, true); |
| 2790 | if (ret || !mem) |
| 2791 | return ret; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2792 | |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2793 | /* |
| 2794 | * FUSE reuses pages without going through the final |
| 2795 | * put that would remove them from the LRU list, make |
| 2796 | * sure that they get relinked properly. |
| 2797 | */ |
| 2798 | __mem_cgroup_commit_charge_lrucare(page, mem, |
| 2799 | MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 2800 | return ret; |
| 2801 | } |
Daisuke Nishimura | 83aae4c7 | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2802 | /* shmem */ |
| 2803 | if (PageSwapCache(page)) { |
| 2804 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
| 2805 | if (!ret) |
| 2806 | __mem_cgroup_commit_charge_swapin(page, mem, |
| 2807 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
| 2808 | } else |
| 2809 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, |
Daisuke Nishimura | 73045c4 | 2010-08-10 18:02:59 -0700 | [diff] [blame] | 2810 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2811 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2812 | return ret; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 2813 | } |
| 2814 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2815 | /* |
| 2816 | * While swap-in, try_charge -> commit or cancel, the page is locked. |
| 2817 | * 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] | 2818 | * struct page_cgroup is acquired. This refcnt will be consumed by |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2819 | * "commit()" or removed by "cancel()" |
| 2820 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2821 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
| 2822 | struct page *page, |
| 2823 | gfp_t mask, struct mem_cgroup **ptr) |
| 2824 | { |
| 2825 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2826 | int ret; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2827 | |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 2828 | *ptr = NULL; |
| 2829 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2830 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2831 | return 0; |
| 2832 | |
| 2833 | if (!do_swap_account) |
| 2834 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2835 | /* |
| 2836 | * A racing thread's fault, or swapoff, may have already updated |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2837 | * the pte, and even removed page from swap cache: in those cases |
| 2838 | * do_swap_page()'s pte_same() test will fail; but there's also a |
| 2839 | * KSM case which does need to charge the page. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2840 | */ |
| 2841 | if (!PageSwapCache(page)) |
Hugh Dickins | 407f9c8 | 2009-12-14 17:59:30 -0800 | [diff] [blame] | 2842 | goto charge_cur_mm; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2843 | mem = try_get_mem_cgroup_from_page(page); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2844 | if (!mem) |
| 2845 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2846 | *ptr = mem; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2847 | ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 2848 | css_put(&mem->css); |
| 2849 | return ret; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2850 | charge_cur_mm: |
| 2851 | if (unlikely(!mm)) |
| 2852 | mm = &init_mm; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2853 | return __mem_cgroup_try_charge(mm, mask, 1, ptr, true); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2854 | } |
| 2855 | |
Daisuke Nishimura | 83aae4c7 | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2856 | static void |
| 2857 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 2858 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2859 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2860 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2861 | return; |
| 2862 | if (!ptr) |
| 2863 | return; |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2864 | cgroup_exclude_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 5a6475a | 2011-03-23 16:42:42 -0700 | [diff] [blame] | 2865 | |
| 2866 | __mem_cgroup_commit_charge_lrucare(page, ptr, ctype); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2867 | /* |
| 2868 | * Now swap is on-memory. This means this page may be |
| 2869 | * counted both as mem and swap....double count. |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2870 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
| 2871 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() |
| 2872 | * may call delete_from_swap_cache() before reach here. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2873 | */ |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 2874 | if (do_swap_account && PageSwapCache(page)) { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2875 | swp_entry_t ent = {.val = page_private(page)}; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2876 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2877 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2878 | |
| 2879 | id = swap_cgroup_record(ent, 0); |
| 2880 | rcu_read_lock(); |
| 2881 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2882 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2883 | /* |
| 2884 | * This recorded memcg can be obsolete one. So, avoid |
| 2885 | * calling css_tryget |
| 2886 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2887 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 2888 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 2889 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2890 | mem_cgroup_put(memcg); |
| 2891 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2892 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2893 | } |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 2894 | /* |
| 2895 | * At swapin, we may charge account against cgroup which has no tasks. |
| 2896 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 2897 | * In that case, we need to call pre_destroy() again. check it here. |
| 2898 | */ |
| 2899 | cgroup_release_and_wakeup_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2900 | } |
| 2901 | |
Daisuke Nishimura | 83aae4c7 | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 2902 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
| 2903 | { |
| 2904 | __mem_cgroup_commit_charge_swapin(page, ptr, |
| 2905 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 2906 | } |
| 2907 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2908 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem) |
| 2909 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2910 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2911 | return; |
| 2912 | if (!mem) |
| 2913 | return; |
Johannes Weiner | e7018b8d | 2011-03-23 16:42:33 -0700 | [diff] [blame] | 2914 | __mem_cgroup_cancel_charge(mem, 1); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2915 | } |
| 2916 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2917 | static void mem_cgroup_do_uncharge(struct mem_cgroup *mem, |
| 2918 | unsigned int nr_pages, |
| 2919 | const enum charge_type ctype) |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2920 | { |
| 2921 | struct memcg_batch_info *batch = NULL; |
| 2922 | bool uncharge_memsw = true; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2923 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2924 | /* If swapout, usage of swap doesn't decrease */ |
| 2925 | if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) |
| 2926 | uncharge_memsw = false; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2927 | |
| 2928 | batch = ¤t->memcg_batch; |
| 2929 | /* |
| 2930 | * In usual, we do css_get() when we remember memcg pointer. |
| 2931 | * But in this case, we keep res->usage until end of a series of |
| 2932 | * uncharges. Then, it's ok to ignore memcg's refcnt. |
| 2933 | */ |
| 2934 | if (!batch->memcg) |
| 2935 | batch->memcg = mem; |
| 2936 | /* |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2937 | * do_batch > 0 when unmapping pages or inode invalidate/truncate. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2938 | * In those cases, all pages freed continuously can be expected to be in |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2939 | * the same cgroup and we have chance to coalesce uncharges. |
| 2940 | * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE) |
| 2941 | * because we want to do uncharge as soon as possible. |
| 2942 | */ |
| 2943 | |
| 2944 | if (!batch->do_batch || test_thread_flag(TIF_MEMDIE)) |
| 2945 | goto direct_uncharge; |
| 2946 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2947 | if (nr_pages > 1) |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 2948 | goto direct_uncharge; |
| 2949 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2950 | /* |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2951 | * In typical case, batch->memcg == mem. This means we can |
| 2952 | * merge a series of uncharges to an uncharge of res_counter. |
| 2953 | * If not, we uncharge res_counter ony by one. |
| 2954 | */ |
| 2955 | if (batch->memcg != mem) |
| 2956 | goto direct_uncharge; |
| 2957 | /* remember freed charge and uncharge it later */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 2958 | batch->nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2959 | if (uncharge_memsw) |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 2960 | batch->memsw_nr_pages++; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2961 | return; |
| 2962 | direct_uncharge: |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2963 | res_counter_uncharge(&mem->res, nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2964 | if (uncharge_memsw) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2965 | res_counter_uncharge(&mem->memsw, nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2966 | if (unlikely(batch->memcg != mem)) |
| 2967 | memcg_oom_recover(mem); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 2968 | return; |
| 2969 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2970 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2971 | /* |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2972 | * uncharge if !page_mapped(page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2973 | */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2974 | static struct mem_cgroup * |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 2975 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2976 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2977 | struct mem_cgroup *mem = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2978 | unsigned int nr_pages = 1; |
| 2979 | struct page_cgroup *pc; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2980 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2981 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2982 | return NULL; |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 2983 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2984 | if (PageSwapCache(page)) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2985 | return NULL; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2986 | |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2987 | if (PageTransHuge(page)) { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2988 | nr_pages <<= compound_order(page); |
Andrea Arcangeli | 37c2ac7 | 2011-01-13 15:47:16 -0800 | [diff] [blame] | 2989 | VM_BUG_ON(!PageTransHuge(page)); |
| 2990 | } |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2991 | /* |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 2992 | * Check if our page_cgroup is valid |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2993 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2994 | pc = lookup_page_cgroup(page); |
| 2995 | if (unlikely(!pc || !PageCgroupUsed(pc))) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2996 | return NULL; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2997 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 2998 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 2999 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3000 | mem = pc->mem_cgroup; |
| 3001 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3002 | if (!PageCgroupUsed(pc)) |
| 3003 | goto unlock_out; |
| 3004 | |
| 3005 | switch (ctype) { |
| 3006 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3007 | case MEM_CGROUP_CHARGE_TYPE_DROP: |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3008 | /* See mem_cgroup_prepare_migration() */ |
| 3009 | if (page_mapped(page) || PageCgroupMigration(pc)) |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3010 | goto unlock_out; |
| 3011 | break; |
| 3012 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: |
| 3013 | if (!PageAnon(page)) { /* Shared memory */ |
| 3014 | if (page->mapping && !page_is_file_cache(page)) |
| 3015 | goto unlock_out; |
| 3016 | } else if (page_mapped(page)) /* Anon */ |
| 3017 | goto unlock_out; |
| 3018 | break; |
| 3019 | default: |
| 3020 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3021 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3022 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3023 | mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), -nr_pages); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 3024 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3025 | ClearPageCgroupUsed(pc); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 3026 | /* |
| 3027 | * pc->mem_cgroup is not cleared here. It will be accessed when it's |
| 3028 | * freed from LRU. This is safe because uncharged page is expected not |
| 3029 | * to be reused (freed soon). Exception is SwapCache, it's handled by |
| 3030 | * special functions. |
| 3031 | */ |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 3032 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3033 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3034 | /* |
| 3035 | * even after unlock, we have mem->res.usage here and this memcg |
| 3036 | * will never be freed. |
| 3037 | */ |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 3038 | memcg_check_events(mem, page); |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3039 | if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) { |
| 3040 | mem_cgroup_swap_statistics(mem, true); |
| 3041 | mem_cgroup_get(mem); |
| 3042 | } |
| 3043 | if (!mem_cgroup_is_root(mem)) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3044 | mem_cgroup_do_uncharge(mem, nr_pages, ctype); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 3045 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3046 | return mem; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3047 | |
| 3048 | unlock_out: |
| 3049 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3050 | return NULL; |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 3051 | } |
| 3052 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3053 | void mem_cgroup_uncharge_page(struct page *page) |
| 3054 | { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3055 | /* early check. */ |
| 3056 | if (page_mapped(page)) |
| 3057 | return; |
| 3058 | if (page->mapping && !PageAnon(page)) |
| 3059 | return; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3060 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 3061 | } |
| 3062 | |
| 3063 | void mem_cgroup_uncharge_cache_page(struct page *page) |
| 3064 | { |
| 3065 | VM_BUG_ON(page_mapped(page)); |
KAMEZAWA Hiroyuki | b7abea9 | 2008-10-18 20:28:09 -0700 | [diff] [blame] | 3066 | VM_BUG_ON(page->mapping); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3067 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 3068 | } |
| 3069 | |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3070 | /* |
| 3071 | * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate. |
| 3072 | * In that cases, pages are freed continuously and we can expect pages |
| 3073 | * are in the same memcg. All these calls itself limits the number of |
| 3074 | * pages freed at once, then uncharge_start/end() is called properly. |
| 3075 | * This may be called prural(2) times in a context, |
| 3076 | */ |
| 3077 | |
| 3078 | void mem_cgroup_uncharge_start(void) |
| 3079 | { |
| 3080 | current->memcg_batch.do_batch++; |
| 3081 | /* We can do nest. */ |
| 3082 | if (current->memcg_batch.do_batch == 1) { |
| 3083 | current->memcg_batch.memcg = NULL; |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3084 | current->memcg_batch.nr_pages = 0; |
| 3085 | current->memcg_batch.memsw_nr_pages = 0; |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | void mem_cgroup_uncharge_end(void) |
| 3090 | { |
| 3091 | struct memcg_batch_info *batch = ¤t->memcg_batch; |
| 3092 | |
| 3093 | if (!batch->do_batch) |
| 3094 | return; |
| 3095 | |
| 3096 | batch->do_batch--; |
| 3097 | if (batch->do_batch) /* If stacked, do nothing. */ |
| 3098 | return; |
| 3099 | |
| 3100 | if (!batch->memcg) |
| 3101 | return; |
| 3102 | /* |
| 3103 | * This "batch->memcg" is valid without any css_get/put etc... |
| 3104 | * bacause we hide charges behind us. |
| 3105 | */ |
Johannes Weiner | 7ffd4ca | 2011-03-23 16:42:35 -0700 | [diff] [blame] | 3106 | if (batch->nr_pages) |
| 3107 | res_counter_uncharge(&batch->memcg->res, |
| 3108 | batch->nr_pages * PAGE_SIZE); |
| 3109 | if (batch->memsw_nr_pages) |
| 3110 | res_counter_uncharge(&batch->memcg->memsw, |
| 3111 | batch->memsw_nr_pages * PAGE_SIZE); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3112 | memcg_oom_recover(batch->memcg); |
KAMEZAWA Hiroyuki | 569b846 | 2009-12-15 16:47:03 -0800 | [diff] [blame] | 3113 | /* forget this pointer (for sanity check) */ |
| 3114 | batch->memcg = NULL; |
| 3115 | } |
| 3116 | |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3117 | #ifdef CONFIG_SWAP |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3118 | /* |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3119 | * called after __delete_from_swap_cache() and drop "page" account. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3120 | * memcg information is recorded to swap_cgroup of "ent" |
| 3121 | */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3122 | void |
| 3123 | 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] | 3124 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3125 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3126 | int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3127 | |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 3128 | if (!swapout) /* this was a swap cache but the swap is unused ! */ |
| 3129 | ctype = MEM_CGROUP_CHARGE_TYPE_DROP; |
| 3130 | |
| 3131 | memcg = __mem_cgroup_uncharge_common(page, ctype); |
| 3132 | |
KAMEZAWA Hiroyuki | f75ca96 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 3133 | /* |
| 3134 | * record memcg information, if swapout && memcg != NULL, |
| 3135 | * mem_cgroup_get() was called in uncharge(). |
| 3136 | */ |
| 3137 | if (do_swap_account && swapout && memcg) |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3138 | swap_cgroup_record(ent, css_id(&memcg->css)); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3139 | } |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 3140 | #endif |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 3141 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3142 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 3143 | /* |
| 3144 | * called from swap_entry_free(). remove record in swap_cgroup and |
| 3145 | * uncharge "memsw" account. |
| 3146 | */ |
| 3147 | void mem_cgroup_uncharge_swap(swp_entry_t ent) |
| 3148 | { |
| 3149 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3150 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3151 | |
| 3152 | if (!do_swap_account) |
| 3153 | return; |
| 3154 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3155 | id = swap_cgroup_record(ent, 0); |
| 3156 | rcu_read_lock(); |
| 3157 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3158 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3159 | /* |
| 3160 | * We uncharge this because swap is freed. |
| 3161 | * This memcg can be obsolete one. We avoid calling css_tryget |
| 3162 | */ |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3163 | if (!mem_cgroup_is_root(memcg)) |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 3164 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3165 | mem_cgroup_swap_statistics(memcg, false); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3166 | mem_cgroup_put(memcg); |
| 3167 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 3168 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3169 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3170 | |
| 3171 | /** |
| 3172 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. |
| 3173 | * @entry: swap entry to be moved |
| 3174 | * @from: mem_cgroup which the entry is moved from |
| 3175 | * @to: mem_cgroup which the entry is moved to |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3176 | * @need_fixup: whether we should fixup res_counters and refcounts. |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3177 | * |
| 3178 | * It succeeds only when the swap_cgroup's record for this entry is the same |
| 3179 | * as the mem_cgroup's id of @from. |
| 3180 | * |
| 3181 | * Returns 0 on success, -EINVAL on failure. |
| 3182 | * |
| 3183 | * The caller must have charged to @to, IOW, called res_counter_charge() about |
| 3184 | * both res and memsw, and called css_get(). |
| 3185 | */ |
| 3186 | static int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3187 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3188 | { |
| 3189 | unsigned short old_id, new_id; |
| 3190 | |
| 3191 | old_id = css_id(&from->css); |
| 3192 | new_id = css_id(&to->css); |
| 3193 | |
| 3194 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3195 | mem_cgroup_swap_statistics(from, false); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3196 | mem_cgroup_swap_statistics(to, true); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3197 | /* |
| 3198 | * This function is only called from task migration context now. |
| 3199 | * It postpones res_counter and refcount handling till the end |
| 3200 | * of task migration(mem_cgroup_clear_mc()) for performance |
| 3201 | * improvement. But we cannot postpone mem_cgroup_get(to) |
| 3202 | * because if the process that has been moved to @to does |
| 3203 | * swap-in, the refcount of @to might be decreased to 0. |
| 3204 | */ |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3205 | mem_cgroup_get(to); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3206 | if (need_fixup) { |
| 3207 | if (!mem_cgroup_is_root(from)) |
| 3208 | res_counter_uncharge(&from->memsw, PAGE_SIZE); |
| 3209 | mem_cgroup_put(from); |
| 3210 | /* |
| 3211 | * we charged both to->res and to->memsw, so we should |
| 3212 | * uncharge to->res. |
| 3213 | */ |
| 3214 | if (!mem_cgroup_is_root(to)) |
| 3215 | res_counter_uncharge(&to->res, PAGE_SIZE); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3216 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3217 | return 0; |
| 3218 | } |
| 3219 | return -EINVAL; |
| 3220 | } |
| 3221 | #else |
| 3222 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3223 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3224 | { |
| 3225 | return -EINVAL; |
| 3226 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3227 | #endif |
| 3228 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3229 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3230 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
| 3231 | * page belongs to. |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3232 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3233 | int mem_cgroup_prepare_migration(struct page *page, |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 3234 | struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3235 | { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3236 | struct mem_cgroup *mem = NULL; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3237 | struct page_cgroup *pc; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3238 | enum charge_type ctype; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3239 | int ret = 0; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 3240 | |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 3241 | *ptr = NULL; |
| 3242 | |
Andrea Arcangeli | ec16851 | 2011-01-13 15:46:56 -0800 | [diff] [blame] | 3243 | VM_BUG_ON(PageTransHuge(page)); |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3244 | if (mem_cgroup_disabled()) |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 3245 | return 0; |
| 3246 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3247 | pc = lookup_page_cgroup(page); |
| 3248 | lock_page_cgroup(pc); |
| 3249 | if (PageCgroupUsed(pc)) { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3250 | mem = pc->mem_cgroup; |
| 3251 | css_get(&mem->css); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3252 | /* |
| 3253 | * At migrating an anonymous page, its mapcount goes down |
| 3254 | * to 0 and uncharge() will be called. But, even if it's fully |
| 3255 | * unmapped, migration may fail and this page has to be |
| 3256 | * charged again. We set MIGRATION flag here and delay uncharge |
| 3257 | * until end_migration() is called |
| 3258 | * |
| 3259 | * Corner Case Thinking |
| 3260 | * A) |
| 3261 | * When the old page was mapped as Anon and it's unmap-and-freed |
| 3262 | * while migration was ongoing. |
| 3263 | * If unmap finds the old page, uncharge() of it will be delayed |
| 3264 | * until end_migration(). If unmap finds a new page, it's |
| 3265 | * uncharged when it make mapcount to be 1->0. If unmap code |
| 3266 | * finds swap_migration_entry, the new page will not be mapped |
| 3267 | * and end_migration() will find it(mapcount==0). |
| 3268 | * |
| 3269 | * B) |
| 3270 | * When the old page was mapped but migraion fails, the kernel |
| 3271 | * remaps it. A charge for it is kept by MIGRATION flag even |
| 3272 | * if mapcount goes down to 0. We can do remap successfully |
| 3273 | * without charging it again. |
| 3274 | * |
| 3275 | * C) |
| 3276 | * The "old" page is under lock_page() until the end of |
| 3277 | * migration, so, the old page itself will not be swapped-out. |
| 3278 | * If the new page is swapped out before end_migraton, our |
| 3279 | * hook to usual swap-out path will catch the event. |
| 3280 | */ |
| 3281 | if (PageAnon(page)) |
| 3282 | SetPageCgroupMigration(pc); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 3283 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3284 | unlock_page_cgroup(pc); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3285 | /* |
| 3286 | * If the page is not charged at this point, |
| 3287 | * we return here. |
| 3288 | */ |
| 3289 | if (!mem) |
| 3290 | return 0; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3291 | |
Andrea Arcangeli | 93d5c9b | 2010-04-23 13:17:39 -0400 | [diff] [blame] | 3292 | *ptr = mem; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3293 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false); |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3294 | css_put(&mem->css);/* drop extra refcnt */ |
| 3295 | if (ret || *ptr == NULL) { |
| 3296 | if (PageAnon(page)) { |
| 3297 | lock_page_cgroup(pc); |
| 3298 | ClearPageCgroupMigration(pc); |
| 3299 | unlock_page_cgroup(pc); |
| 3300 | /* |
| 3301 | * The old page may be fully unmapped while we kept it. |
| 3302 | */ |
| 3303 | mem_cgroup_uncharge_page(page); |
| 3304 | } |
| 3305 | return -ENOMEM; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3306 | } |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3307 | /* |
| 3308 | * We charge new page before it's used/mapped. So, even if unlock_page() |
| 3309 | * is called before end_migration, we can catch all events on this new |
| 3310 | * page. In the case new page is migrated but not remapped, new page's |
| 3311 | * mapcount will be finally 0 and we call uncharge in end_migration(). |
| 3312 | */ |
| 3313 | pc = lookup_page_cgroup(newpage); |
| 3314 | if (PageAnon(page)) |
| 3315 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; |
| 3316 | else if (page_is_file_cache(page)) |
| 3317 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; |
| 3318 | else |
| 3319 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3320 | __mem_cgroup_commit_charge(mem, page, 1, pc, ctype); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3321 | return ret; |
| 3322 | } |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 3323 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3324 | /* remove redundant charge if migration failed*/ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3325 | void mem_cgroup_end_migration(struct mem_cgroup *mem, |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 3326 | struct page *oldpage, struct page *newpage, bool migration_ok) |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 3327 | { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3328 | struct page *used, *unused; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3329 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3330 | |
| 3331 | if (!mem) |
| 3332 | return; |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3333 | /* blocks rmdir() */ |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3334 | cgroup_exclude_rmdir(&mem->css); |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 3335 | if (!migration_ok) { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3336 | used = oldpage; |
| 3337 | unused = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3338 | } else { |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3339 | used = newpage; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3340 | unused = oldpage; |
| 3341 | } |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3342 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3343 | * We disallowed uncharge of pages under migration because mapcount |
| 3344 | * of the page goes down to zero, temporarly. |
| 3345 | * Clear the flag and check the page should be charged. |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 3346 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3347 | pc = lookup_page_cgroup(oldpage); |
| 3348 | lock_page_cgroup(pc); |
| 3349 | ClearPageCgroupMigration(pc); |
| 3350 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3351 | |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3352 | __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE); |
| 3353 | |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3354 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3355 | * If a page is a file cache, radix-tree replacement is very atomic |
| 3356 | * and we can skip this check. When it was an Anon page, its mapcount |
| 3357 | * goes down to 0. But because we added MIGRATION flage, it's not |
| 3358 | * uncharged yet. There are several case but page->mapcount check |
| 3359 | * and USED bit check in mem_cgroup_uncharge_page() will do enough |
| 3360 | * check. (see prepare_charge() also) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 3361 | */ |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3362 | if (PageAnon(used)) |
| 3363 | mem_cgroup_uncharge_page(used); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3364 | /* |
akpm@linux-foundation.org | ac39cf8 | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 3365 | * At migration, we may charge account against cgroup which has no |
| 3366 | * tasks. |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3367 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 3368 | * In that case, we need to call pre_destroy() again. check it here. |
| 3369 | */ |
| 3370 | cgroup_release_and_wakeup_rmdir(&mem->css); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 3371 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 3372 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3373 | /* |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3374 | * A call to try to shrink memory usage on charge failure at shmem's swapin. |
| 3375 | * Calling hierarchical_reclaim is not enough because we should update |
| 3376 | * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM. |
| 3377 | * Moreover considering hierarchy, we should reclaim from the mem_over_limit, |
| 3378 | * not from the memcg which this page would be charged to. |
| 3379 | * try_charge_swapin does all of these works properly. |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3380 | */ |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3381 | int mem_cgroup_shmem_charge_fallback(struct page *page, |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 3382 | struct mm_struct *mm, |
| 3383 | gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3384 | { |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 3385 | struct mem_cgroup *mem; |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3386 | int ret; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3387 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 3388 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 3389 | return 0; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3390 | |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3391 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
| 3392 | if (!ret) |
| 3393 | mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */ |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3394 | |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 3395 | return ret; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3396 | } |
| 3397 | |
Daisuke Nishimura | f212ad7 | 2011-03-23 16:42:25 -0700 | [diff] [blame] | 3398 | #ifdef CONFIG_DEBUG_VM |
| 3399 | static struct page_cgroup *lookup_page_cgroup_used(struct page *page) |
| 3400 | { |
| 3401 | struct page_cgroup *pc; |
| 3402 | |
| 3403 | pc = lookup_page_cgroup(page); |
| 3404 | if (likely(pc) && PageCgroupUsed(pc)) |
| 3405 | return pc; |
| 3406 | return NULL; |
| 3407 | } |
| 3408 | |
| 3409 | bool mem_cgroup_bad_page_check(struct page *page) |
| 3410 | { |
| 3411 | if (mem_cgroup_disabled()) |
| 3412 | return false; |
| 3413 | |
| 3414 | return lookup_page_cgroup_used(page) != NULL; |
| 3415 | } |
| 3416 | |
| 3417 | void mem_cgroup_print_bad_page(struct page *page) |
| 3418 | { |
| 3419 | struct page_cgroup *pc; |
| 3420 | |
| 3421 | pc = lookup_page_cgroup_used(page); |
| 3422 | if (pc) { |
| 3423 | int ret = -1; |
| 3424 | char *path; |
| 3425 | |
| 3426 | printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p", |
| 3427 | pc, pc->flags, pc->mem_cgroup); |
| 3428 | |
| 3429 | path = kmalloc(PATH_MAX, GFP_KERNEL); |
| 3430 | if (path) { |
| 3431 | rcu_read_lock(); |
| 3432 | ret = cgroup_path(pc->mem_cgroup->css.cgroup, |
| 3433 | path, PATH_MAX); |
| 3434 | rcu_read_unlock(); |
| 3435 | } |
| 3436 | |
| 3437 | printk(KERN_CONT "(%s)\n", |
| 3438 | (ret < 0) ? "cannot get the path" : path); |
| 3439 | kfree(path); |
| 3440 | } |
| 3441 | } |
| 3442 | #endif |
| 3443 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3444 | static DEFINE_MUTEX(set_limit_mutex); |
| 3445 | |
KOSAKI Motohiro | d38d2a7 | 2009-01-06 14:39:44 -0800 | [diff] [blame] | 3446 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3447 | unsigned long long val) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3448 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3449 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3450 | u64 memswlimit, memlimit; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3451 | int ret = 0; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3452 | int children = mem_cgroup_count_children(memcg); |
| 3453 | u64 curusage, oldusage; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3454 | int enlarge; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3455 | |
| 3456 | /* |
| 3457 | * For keeping hierarchical_reclaim simple, how long we should retry |
| 3458 | * is depends on callers. We set our retry-count to be function |
| 3459 | * of # of children which we should visit in this loop. |
| 3460 | */ |
| 3461 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; |
| 3462 | |
| 3463 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3464 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3465 | enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3466 | while (retry_count) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3467 | if (signal_pending(current)) { |
| 3468 | ret = -EINTR; |
| 3469 | break; |
| 3470 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3471 | /* |
| 3472 | * Rather than hide all in some function, I do this in |
| 3473 | * open coded manner. You see what this really does. |
| 3474 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 3475 | */ |
| 3476 | mutex_lock(&set_limit_mutex); |
| 3477 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3478 | if (memswlimit < val) { |
| 3479 | ret = -EINVAL; |
| 3480 | mutex_unlock(&set_limit_mutex); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3481 | break; |
| 3482 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3483 | |
| 3484 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3485 | if (memlimit < val) |
| 3486 | enlarge = 1; |
| 3487 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3488 | ret = res_counter_set_limit(&memcg->res, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 3489 | if (!ret) { |
| 3490 | if (memswlimit == val) |
| 3491 | memcg->memsw_is_minimum = true; |
| 3492 | else |
| 3493 | memcg->memsw_is_minimum = false; |
| 3494 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3495 | mutex_unlock(&set_limit_mutex); |
| 3496 | |
| 3497 | if (!ret) |
| 3498 | break; |
| 3499 | |
Bob Liu | aa20d48 | 2009-12-15 16:47:14 -0800 | [diff] [blame] | 3500 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3501 | MEM_CGROUP_RECLAIM_SHRINK, |
| 3502 | NULL); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3503 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
| 3504 | /* Usage is reduced ? */ |
| 3505 | if (curusage >= oldusage) |
| 3506 | retry_count--; |
| 3507 | else |
| 3508 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3509 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3510 | if (!ret && enlarge) |
| 3511 | memcg_oom_recover(memcg); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 3512 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3513 | return ret; |
| 3514 | } |
| 3515 | |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 3516 | static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
| 3517 | unsigned long long val) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3518 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3519 | int retry_count; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3520 | u64 memlimit, memswlimit, oldusage, curusage; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3521 | int children = mem_cgroup_count_children(memcg); |
| 3522 | int ret = -EBUSY; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3523 | int enlarge = 0; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3524 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3525 | /* see mem_cgroup_resize_res_limit */ |
| 3526 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; |
| 3527 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3528 | while (retry_count) { |
| 3529 | if (signal_pending(current)) { |
| 3530 | ret = -EINTR; |
| 3531 | break; |
| 3532 | } |
| 3533 | /* |
| 3534 | * Rather than hide all in some function, I do this in |
| 3535 | * open coded manner. You see what this really does. |
| 3536 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 3537 | */ |
| 3538 | mutex_lock(&set_limit_mutex); |
| 3539 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3540 | if (memlimit > val) { |
| 3541 | ret = -EINVAL; |
| 3542 | mutex_unlock(&set_limit_mutex); |
| 3543 | break; |
| 3544 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3545 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3546 | if (memswlimit < val) |
| 3547 | enlarge = 1; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3548 | ret = res_counter_set_limit(&memcg->memsw, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 3549 | if (!ret) { |
| 3550 | if (memlimit == val) |
| 3551 | memcg->memsw_is_minimum = true; |
| 3552 | else |
| 3553 | memcg->memsw_is_minimum = false; |
| 3554 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3555 | mutex_unlock(&set_limit_mutex); |
| 3556 | |
| 3557 | if (!ret) |
| 3558 | break; |
| 3559 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3560 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 3561 | MEM_CGROUP_RECLAIM_NOSWAP | |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3562 | MEM_CGROUP_RECLAIM_SHRINK, |
| 3563 | NULL); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3564 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3565 | /* Usage is reduced ? */ |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3566 | if (curusage >= oldusage) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3567 | retry_count--; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3568 | else |
| 3569 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3570 | } |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3571 | if (!ret && enlarge) |
| 3572 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3573 | return ret; |
| 3574 | } |
| 3575 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3576 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3577 | gfp_t gfp_mask, |
| 3578 | unsigned long *total_scanned) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3579 | { |
| 3580 | unsigned long nr_reclaimed = 0; |
| 3581 | struct mem_cgroup_per_zone *mz, *next_mz = NULL; |
| 3582 | unsigned long reclaimed; |
| 3583 | int loop = 0; |
| 3584 | struct mem_cgroup_tree_per_zone *mctz; |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3585 | unsigned long long excess; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3586 | unsigned long nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3587 | |
| 3588 | if (order > 0) |
| 3589 | return 0; |
| 3590 | |
KOSAKI Motohiro | 00918b6 | 2010-08-10 18:03:05 -0700 | [diff] [blame] | 3591 | mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone)); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3592 | /* |
| 3593 | * This loop can run a while, specially if mem_cgroup's continuously |
| 3594 | * keep exceeding their soft limit and putting the system under |
| 3595 | * pressure |
| 3596 | */ |
| 3597 | do { |
| 3598 | if (next_mz) |
| 3599 | mz = next_mz; |
| 3600 | else |
| 3601 | mz = mem_cgroup_largest_soft_limit_node(mctz); |
| 3602 | if (!mz) |
| 3603 | break; |
| 3604 | |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3605 | nr_scanned = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3606 | reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone, |
| 3607 | gfp_mask, |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3608 | MEM_CGROUP_RECLAIM_SOFT, |
| 3609 | &nr_scanned); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3610 | nr_reclaimed += reclaimed; |
Ying Han | 0ae5e89 | 2011-05-26 16:25:25 -0700 | [diff] [blame] | 3611 | *total_scanned += nr_scanned; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3612 | spin_lock(&mctz->lock); |
| 3613 | |
| 3614 | /* |
| 3615 | * If we failed to reclaim anything from this memory cgroup |
| 3616 | * it is time to move on to the next cgroup |
| 3617 | */ |
| 3618 | next_mz = NULL; |
| 3619 | if (!reclaimed) { |
| 3620 | do { |
| 3621 | /* |
| 3622 | * Loop until we find yet another one. |
| 3623 | * |
| 3624 | * By the time we get the soft_limit lock |
| 3625 | * again, someone might have aded the |
| 3626 | * group back on the RB tree. Iterate to |
| 3627 | * make sure we get a different mem. |
| 3628 | * mem_cgroup_largest_soft_limit_node returns |
| 3629 | * NULL if no other cgroup is present on |
| 3630 | * the tree |
| 3631 | */ |
| 3632 | next_mz = |
| 3633 | __mem_cgroup_largest_soft_limit_node(mctz); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 3634 | if (next_mz == mz) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3635 | css_put(&next_mz->mem->css); |
Michal Hocko | 39cc98f | 2011-05-26 16:25:28 -0700 | [diff] [blame] | 3636 | else /* next_mz == NULL or other memcg */ |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3637 | break; |
| 3638 | } while (1); |
| 3639 | } |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3640 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3641 | excess = res_counter_soft_limit_excess(&mz->mem->res); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3642 | /* |
| 3643 | * One school of thought says that we should not add |
| 3644 | * back the node to the tree if reclaim returns 0. |
| 3645 | * But our reclaim could return 0, simply because due |
| 3646 | * to priority we are exposing a smaller subset of |
| 3647 | * memory to reclaim from. Consider this as a longer |
| 3648 | * term TODO. |
| 3649 | */ |
KAMEZAWA Hiroyuki | ef8745c | 2009-10-01 15:44:12 -0700 | [diff] [blame] | 3650 | /* If excess == 0, no tree ops */ |
| 3651 | __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess); |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 3652 | spin_unlock(&mctz->lock); |
| 3653 | css_put(&mz->mem->css); |
| 3654 | loop++; |
| 3655 | /* |
| 3656 | * Could not reclaim anything and there are no more |
| 3657 | * mem cgroups to try or we seem to be looping without |
| 3658 | * reclaiming anything. |
| 3659 | */ |
| 3660 | if (!nr_reclaimed && |
| 3661 | (next_mz == NULL || |
| 3662 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) |
| 3663 | break; |
| 3664 | } while (!nr_reclaimed); |
| 3665 | if (next_mz) |
| 3666 | css_put(&next_mz->mem->css); |
| 3667 | return nr_reclaimed; |
| 3668 | } |
| 3669 | |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 3670 | /* |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3671 | * This routine traverse page_cgroup in given list and drop them all. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3672 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
| 3673 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3674 | static int mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3675 | int node, int zid, enum lru_list lru) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3676 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3677 | struct zone *zone; |
| 3678 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3679 | struct page_cgroup *pc, *busy; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3680 | unsigned long flags, loop; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3681 | struct list_head *list; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3682 | int ret = 0; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3683 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3684 | zone = &NODE_DATA(node)->node_zones[zid]; |
| 3685 | mz = mem_cgroup_zoneinfo(mem, node, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3686 | list = &mz->lists[lru]; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3687 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3688 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
| 3689 | /* give some margin against EBUSY etc...*/ |
| 3690 | loop += 256; |
| 3691 | busy = NULL; |
| 3692 | while (loop--) { |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3693 | struct page *page; |
| 3694 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3695 | ret = 0; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3696 | spin_lock_irqsave(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3697 | if (list_empty(list)) { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3698 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3699 | break; |
| 3700 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3701 | pc = list_entry(list->prev, struct page_cgroup, lru); |
| 3702 | if (busy == pc) { |
| 3703 | list_move(&pc->lru, list); |
Thiago Farina | 648bcc7 | 2010-03-05 13:42:04 -0800 | [diff] [blame] | 3704 | busy = NULL; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3705 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3706 | continue; |
| 3707 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3708 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3709 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 3710 | page = lookup_cgroup_page(pc); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3711 | |
| 3712 | ret = mem_cgroup_move_parent(page, pc, mem, GFP_KERNEL); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3713 | if (ret == -ENOMEM) |
| 3714 | break; |
| 3715 | |
| 3716 | if (ret == -EBUSY || ret == -EINVAL) { |
| 3717 | /* found lock contention or "pc" is obsolete. */ |
| 3718 | busy = pc; |
| 3719 | cond_resched(); |
| 3720 | } else |
| 3721 | busy = NULL; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3722 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3723 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3724 | if (!ret && !list_empty(list)) |
| 3725 | return -EBUSY; |
| 3726 | return ret; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | /* |
| 3730 | * make mem_cgroup's charge to be 0 if there is no task. |
| 3731 | * This enables deleting this mem_cgroup. |
| 3732 | */ |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3733 | 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] | 3734 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3735 | int ret; |
| 3736 | int node, zid, shrink; |
| 3737 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3738 | struct cgroup *cgrp = mem->css.cgroup; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 3739 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3740 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3741 | |
| 3742 | shrink = 0; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3743 | /* should free all ? */ |
| 3744 | if (free_all) |
| 3745 | goto try_to_free; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3746 | move_account: |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3747 | do { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3748 | ret = -EBUSY; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3749 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3750 | goto out; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3751 | ret = -EINTR; |
| 3752 | if (signal_pending(current)) |
| 3753 | goto out; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3754 | /* This is for making all *used* pages to be on LRU. */ |
| 3755 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 3756 | drain_all_stock_sync(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3757 | ret = 0; |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 3758 | mem_cgroup_start_move(mem); |
KAMEZAWA Hiroyuki | 299b4ea | 2009-01-29 14:25:17 -0800 | [diff] [blame] | 3759 | for_each_node_state(node, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3760 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 3761 | enum lru_list l; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3762 | for_each_lru(l) { |
| 3763 | ret = mem_cgroup_force_empty_list(mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3764 | node, zid, l); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3765 | if (ret) |
| 3766 | break; |
| 3767 | } |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 3768 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3769 | if (ret) |
| 3770 | break; |
| 3771 | } |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 3772 | mem_cgroup_end_move(mem); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3773 | memcg_oom_recover(mem); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3774 | /* it seems parent cgroup doesn't have enough mem */ |
| 3775 | if (ret == -ENOMEM) |
| 3776 | goto try_to_free; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3777 | cond_resched(); |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3778 | /* "ret" should also be checked to ensure all lists are empty. */ |
| 3779 | } while (mem->res.usage > 0 || ret); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3780 | out: |
| 3781 | css_put(&mem->css); |
| 3782 | return ret; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3783 | |
| 3784 | try_to_free: |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3785 | /* returns EBUSY if there is a task or if we come here twice. */ |
| 3786 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3787 | ret = -EBUSY; |
| 3788 | goto out; |
| 3789 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3790 | /* we call try-to-free pages for make this cgroup empty */ |
| 3791 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3792 | /* try to free all pages in this cgroup */ |
| 3793 | shrink = 1; |
| 3794 | while (nr_retries && mem->res.usage > 0) { |
| 3795 | int progress; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3796 | |
| 3797 | if (signal_pending(current)) { |
| 3798 | ret = -EINTR; |
| 3799 | goto out; |
| 3800 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 3801 | progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL, |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 3802 | false); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3803 | if (!progress) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3804 | nr_retries--; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3805 | /* maybe some writeback is necessary */ |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 3806 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3807 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3808 | |
| 3809 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3810 | lru_add_drain(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3811 | /* try move_account...there may be some *locked* pages. */ |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3812 | goto move_account; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3813 | } |
| 3814 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3815 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
| 3816 | { |
| 3817 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); |
| 3818 | } |
| 3819 | |
| 3820 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3821 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
| 3822 | { |
| 3823 | return mem_cgroup_from_cont(cont)->use_hierarchy; |
| 3824 | } |
| 3825 | |
| 3826 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, |
| 3827 | u64 val) |
| 3828 | { |
| 3829 | int retval = 0; |
| 3830 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 3831 | struct cgroup *parent = cont->parent; |
| 3832 | struct mem_cgroup *parent_mem = NULL; |
| 3833 | |
| 3834 | if (parent) |
| 3835 | parent_mem = mem_cgroup_from_cont(parent); |
| 3836 | |
| 3837 | cgroup_lock(); |
| 3838 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 3839 | * 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] | 3840 | * in the child subtrees. If it is unset, then the change can |
| 3841 | * occur, provided the current cgroup has no children. |
| 3842 | * |
| 3843 | * For the root cgroup, parent_mem is NULL, we allow value to be |
| 3844 | * set if there are no children. |
| 3845 | */ |
| 3846 | if ((!parent_mem || !parent_mem->use_hierarchy) && |
| 3847 | (val == 1 || val == 0)) { |
| 3848 | if (list_empty(&cont->children)) |
| 3849 | mem->use_hierarchy = val; |
| 3850 | else |
| 3851 | retval = -EBUSY; |
| 3852 | } else |
| 3853 | retval = -EINVAL; |
| 3854 | cgroup_unlock(); |
| 3855 | |
| 3856 | return retval; |
| 3857 | } |
| 3858 | |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3859 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3860 | static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *mem, |
| 3861 | enum mem_cgroup_stat_index idx) |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3862 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3863 | struct mem_cgroup *iter; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3864 | long val = 0; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3865 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3866 | /* Per-cpu values can be negative, use a signed accumulator */ |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3867 | for_each_mem_cgroup_tree(iter, mem) |
| 3868 | val += mem_cgroup_read_stat(iter, idx); |
| 3869 | |
| 3870 | if (val < 0) /* race ? */ |
| 3871 | val = 0; |
| 3872 | return val; |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3873 | } |
| 3874 | |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3875 | static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap) |
| 3876 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3877 | u64 val; |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3878 | |
| 3879 | if (!mem_cgroup_is_root(mem)) { |
| 3880 | if (!swap) |
| 3881 | return res_counter_read_u64(&mem->res, RES_USAGE); |
| 3882 | else |
| 3883 | return res_counter_read_u64(&mem->memsw, RES_USAGE); |
| 3884 | } |
| 3885 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3886 | val = mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_CACHE); |
| 3887 | val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_RSS); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3888 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 3889 | if (swap) |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 3890 | val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_SWAPOUT); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3891 | |
| 3892 | return val << PAGE_SHIFT; |
| 3893 | } |
| 3894 | |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 3895 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3896 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3897 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3898 | u64 val; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3899 | int type, name; |
| 3900 | |
| 3901 | type = MEMFILE_TYPE(cft->private); |
| 3902 | name = MEMFILE_ATTR(cft->private); |
| 3903 | switch (type) { |
| 3904 | case _MEM: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3905 | if (name == RES_USAGE) |
| 3906 | val = mem_cgroup_usage(mem, false); |
| 3907 | else |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3908 | val = res_counter_read_u64(&mem->res, name); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3909 | break; |
| 3910 | case _MEMSWAP: |
Kirill A. Shutemov | 104f392 | 2010-03-10 15:22:21 -0800 | [diff] [blame] | 3911 | if (name == RES_USAGE) |
| 3912 | val = mem_cgroup_usage(mem, true); |
| 3913 | else |
Balbir Singh | 0c3e73e | 2009-09-23 15:56:42 -0700 | [diff] [blame] | 3914 | val = res_counter_read_u64(&mem->memsw, name); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3915 | break; |
| 3916 | default: |
| 3917 | BUG(); |
| 3918 | break; |
| 3919 | } |
| 3920 | return val; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3921 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3922 | /* |
| 3923 | * The user of this function is... |
| 3924 | * RES_LIMIT. |
| 3925 | */ |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 3926 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 3927 | const char *buffer) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3928 | { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3929 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3930 | int type, name; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3931 | unsigned long long val; |
| 3932 | int ret; |
| 3933 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3934 | type = MEMFILE_TYPE(cft->private); |
| 3935 | name = MEMFILE_ATTR(cft->private); |
| 3936 | switch (name) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3937 | case RES_LIMIT: |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 3938 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
| 3939 | ret = -EINVAL; |
| 3940 | break; |
| 3941 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3942 | /* This function does all necessary parse...reuse it */ |
| 3943 | ret = res_counter_memparse_write_strategy(buffer, &val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3944 | if (ret) |
| 3945 | break; |
| 3946 | if (type == _MEM) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3947 | ret = mem_cgroup_resize_limit(memcg, val); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3948 | else |
| 3949 | ret = mem_cgroup_resize_memsw_limit(memcg, val); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3950 | break; |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 3951 | case RES_SOFT_LIMIT: |
| 3952 | ret = res_counter_memparse_write_strategy(buffer, &val); |
| 3953 | if (ret) |
| 3954 | break; |
| 3955 | /* |
| 3956 | * For memsw, soft limits are hard to implement in terms |
| 3957 | * of semantics, for now, we support soft limits for |
| 3958 | * control without swap |
| 3959 | */ |
| 3960 | if (type == _MEM) |
| 3961 | ret = res_counter_set_soft_limit(&memcg->res, val); |
| 3962 | else |
| 3963 | ret = -EINVAL; |
| 3964 | break; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3965 | default: |
| 3966 | ret = -EINVAL; /* should be BUG() ? */ |
| 3967 | break; |
| 3968 | } |
| 3969 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3970 | } |
| 3971 | |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 3972 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
| 3973 | unsigned long long *mem_limit, unsigned long long *memsw_limit) |
| 3974 | { |
| 3975 | struct cgroup *cgroup; |
| 3976 | unsigned long long min_limit, min_memsw_limit, tmp; |
| 3977 | |
| 3978 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3979 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3980 | cgroup = memcg->css.cgroup; |
| 3981 | if (!memcg->use_hierarchy) |
| 3982 | goto out; |
| 3983 | |
| 3984 | while (cgroup->parent) { |
| 3985 | cgroup = cgroup->parent; |
| 3986 | memcg = mem_cgroup_from_cont(cgroup); |
| 3987 | if (!memcg->use_hierarchy) |
| 3988 | break; |
| 3989 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 3990 | min_limit = min(min_limit, tmp); |
| 3991 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 3992 | min_memsw_limit = min(min_memsw_limit, tmp); |
| 3993 | } |
| 3994 | out: |
| 3995 | *mem_limit = min_limit; |
| 3996 | *memsw_limit = min_memsw_limit; |
| 3997 | return; |
| 3998 | } |
| 3999 | |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4000 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4001 | { |
| 4002 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4003 | int type, name; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4004 | |
| 4005 | mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4006 | type = MEMFILE_TYPE(event); |
| 4007 | name = MEMFILE_ATTR(event); |
| 4008 | switch (name) { |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4009 | case RES_MAX_USAGE: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4010 | if (type == _MEM) |
| 4011 | res_counter_reset_max(&mem->res); |
| 4012 | else |
| 4013 | res_counter_reset_max(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4014 | break; |
| 4015 | case RES_FAILCNT: |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4016 | if (type == _MEM) |
| 4017 | res_counter_reset_failcnt(&mem->res); |
| 4018 | else |
| 4019 | res_counter_reset_failcnt(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4020 | break; |
| 4021 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4022 | |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 4023 | return 0; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4024 | } |
| 4025 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4026 | static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp, |
| 4027 | struct cftype *cft) |
| 4028 | { |
| 4029 | return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate; |
| 4030 | } |
| 4031 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4032 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4033 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 4034 | struct cftype *cft, u64 val) |
| 4035 | { |
| 4036 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4037 | |
| 4038 | if (val >= (1 << NR_MOVE_TYPE)) |
| 4039 | return -EINVAL; |
| 4040 | /* |
| 4041 | * We check this value several times in both in can_attach() and |
| 4042 | * attach(), so we need cgroup lock to prevent this value from being |
| 4043 | * inconsistent. |
| 4044 | */ |
| 4045 | cgroup_lock(); |
| 4046 | mem->move_charge_at_immigrate = val; |
| 4047 | cgroup_unlock(); |
| 4048 | |
| 4049 | return 0; |
| 4050 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4051 | #else |
| 4052 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
| 4053 | struct cftype *cft, u64 val) |
| 4054 | { |
| 4055 | return -ENOSYS; |
| 4056 | } |
| 4057 | #endif |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4058 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4059 | |
| 4060 | /* For read statistics */ |
| 4061 | enum { |
| 4062 | MCS_CACHE, |
| 4063 | MCS_RSS, |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 4064 | MCS_FILE_MAPPED, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4065 | MCS_PGPGIN, |
| 4066 | MCS_PGPGOUT, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4067 | MCS_SWAP, |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4068 | MCS_PGFAULT, |
| 4069 | MCS_PGMAJFAULT, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4070 | MCS_INACTIVE_ANON, |
| 4071 | MCS_ACTIVE_ANON, |
| 4072 | MCS_INACTIVE_FILE, |
| 4073 | MCS_ACTIVE_FILE, |
| 4074 | MCS_UNEVICTABLE, |
| 4075 | NR_MCS_STAT, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4076 | }; |
| 4077 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4078 | struct mcs_total_stat { |
| 4079 | s64 stat[NR_MCS_STAT]; |
| 4080 | }; |
| 4081 | |
| 4082 | struct { |
| 4083 | char *local_name; |
| 4084 | char *total_name; |
| 4085 | } memcg_stat_strings[NR_MCS_STAT] = { |
| 4086 | {"cache", "total_cache"}, |
| 4087 | {"rss", "total_rss"}, |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 4088 | {"mapped_file", "total_mapped_file"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4089 | {"pgpgin", "total_pgpgin"}, |
| 4090 | {"pgpgout", "total_pgpgout"}, |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4091 | {"swap", "total_swap"}, |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4092 | {"pgfault", "total_pgfault"}, |
| 4093 | {"pgmajfault", "total_pgmajfault"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4094 | {"inactive_anon", "total_inactive_anon"}, |
| 4095 | {"active_anon", "total_active_anon"}, |
| 4096 | {"inactive_file", "total_inactive_file"}, |
| 4097 | {"active_file", "total_active_file"}, |
| 4098 | {"unevictable", "total_unevictable"} |
| 4099 | }; |
| 4100 | |
| 4101 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4102 | static void |
| 4103 | mem_cgroup_get_local_stat(struct mem_cgroup *mem, struct mcs_total_stat *s) |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4104 | { |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4105 | s64 val; |
| 4106 | |
| 4107 | /* per cpu stat */ |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4108 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4109 | s->stat[MCS_CACHE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4110 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4111 | s->stat[MCS_RSS] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4112 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED); |
KAMEZAWA Hiroyuki | d804658 | 2009-12-15 16:47:09 -0800 | [diff] [blame] | 4113 | s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 4114 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGIN); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4115 | s->stat[MCS_PGPGIN] += val; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 4116 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGOUT); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4117 | s->stat[MCS_PGPGOUT] += val; |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4118 | if (do_swap_account) { |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4119 | val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4120 | s->stat[MCS_SWAP] += val * PAGE_SIZE; |
| 4121 | } |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 4122 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGFAULT); |
| 4123 | s->stat[MCS_PGFAULT] += val; |
| 4124 | val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGMAJFAULT); |
| 4125 | s->stat[MCS_PGMAJFAULT] += val; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4126 | |
| 4127 | /* per zone stat */ |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4128 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_ANON)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4129 | s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4130 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_ANON)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4131 | s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4132 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_FILE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4133 | s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4134 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_FILE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4135 | s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4136 | val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_UNEVICTABLE)); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4137 | s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4138 | } |
| 4139 | |
| 4140 | static void |
| 4141 | mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s) |
| 4142 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4143 | struct mem_cgroup *iter; |
| 4144 | |
| 4145 | for_each_mem_cgroup_tree(iter, mem) |
| 4146 | mem_cgroup_get_local_stat(iter, s); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4147 | } |
| 4148 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4149 | #ifdef CONFIG_NUMA |
| 4150 | static int mem_control_numa_stat_show(struct seq_file *m, void *arg) |
| 4151 | { |
| 4152 | int nid; |
| 4153 | unsigned long total_nr, file_nr, anon_nr, unevictable_nr; |
| 4154 | unsigned long node_nr; |
| 4155 | struct cgroup *cont = m->private; |
| 4156 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
| 4157 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4158 | total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4159 | seq_printf(m, "total=%lu", total_nr); |
| 4160 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4161 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, LRU_ALL); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4162 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4163 | } |
| 4164 | seq_putc(m, '\n'); |
| 4165 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4166 | file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4167 | seq_printf(m, "file=%lu", file_nr); |
| 4168 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4169 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4170 | LRU_ALL_FILE); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4171 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4172 | } |
| 4173 | seq_putc(m, '\n'); |
| 4174 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4175 | anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4176 | seq_printf(m, "anon=%lu", anon_nr); |
| 4177 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4178 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4179 | LRU_ALL_ANON); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4180 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4181 | } |
| 4182 | seq_putc(m, '\n'); |
| 4183 | |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4184 | unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE)); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4185 | seq_printf(m, "unevictable=%lu", unevictable_nr); |
| 4186 | for_each_node_state(nid, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 4187 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
| 4188 | BIT(LRU_UNEVICTABLE)); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4189 | seq_printf(m, " N%d=%lu", nid, node_nr); |
| 4190 | } |
| 4191 | seq_putc(m, '\n'); |
| 4192 | return 0; |
| 4193 | } |
| 4194 | #endif /* CONFIG_NUMA */ |
| 4195 | |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 4196 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
| 4197 | struct cgroup_map_cb *cb) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4198 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4199 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4200 | struct mcs_total_stat mystat; |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4201 | int i; |
| 4202 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4203 | memset(&mystat, 0, sizeof(mystat)); |
| 4204 | mem_cgroup_get_local_stat(mem_cont, &mystat); |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4205 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4206 | |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4207 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 4208 | if (i == MCS_SWAP && !do_swap_account) |
| 4209 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4210 | cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4211 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4212 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4213 | /* Hierarchical information */ |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 4214 | { |
| 4215 | unsigned long long limit, memsw_limit; |
| 4216 | memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit); |
| 4217 | cb->fill(cb, "hierarchical_memory_limit", limit); |
| 4218 | if (do_swap_account) |
| 4219 | cb->fill(cb, "hierarchical_memsw_limit", memsw_limit); |
| 4220 | } |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4221 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4222 | memset(&mystat, 0, sizeof(mystat)); |
| 4223 | mem_cgroup_get_total_stat(mem_cont, &mystat); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4224 | for (i = 0; i < NR_MCS_STAT; i++) { |
| 4225 | if (i == MCS_SWAP && !do_swap_account) |
| 4226 | continue; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4227 | cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4228 | } |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4229 | |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4230 | #ifdef CONFIG_DEBUG_VM |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 4231 | cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL)); |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4232 | |
| 4233 | { |
| 4234 | int nid, zid; |
| 4235 | struct mem_cgroup_per_zone *mz; |
| 4236 | unsigned long recent_rotated[2] = {0, 0}; |
| 4237 | unsigned long recent_scanned[2] = {0, 0}; |
| 4238 | |
| 4239 | for_each_online_node(nid) |
| 4240 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 4241 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
| 4242 | |
| 4243 | recent_rotated[0] += |
| 4244 | mz->reclaim_stat.recent_rotated[0]; |
| 4245 | recent_rotated[1] += |
| 4246 | mz->reclaim_stat.recent_rotated[1]; |
| 4247 | recent_scanned[0] += |
| 4248 | mz->reclaim_stat.recent_scanned[0]; |
| 4249 | recent_scanned[1] += |
| 4250 | mz->reclaim_stat.recent_scanned[1]; |
| 4251 | } |
| 4252 | cb->fill(cb, "recent_rotated_anon", recent_rotated[0]); |
| 4253 | cb->fill(cb, "recent_rotated_file", recent_rotated[1]); |
| 4254 | cb->fill(cb, "recent_scanned_anon", recent_scanned[0]); |
| 4255 | cb->fill(cb, "recent_scanned_file", recent_scanned[1]); |
| 4256 | } |
| 4257 | #endif |
| 4258 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4259 | return 0; |
| 4260 | } |
| 4261 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4262 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
| 4263 | { |
| 4264 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4265 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 4266 | return mem_cgroup_swappiness(memcg); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, |
| 4270 | u64 val) |
| 4271 | { |
| 4272 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4273 | struct mem_cgroup *parent; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4274 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4275 | if (val > 100) |
| 4276 | return -EINVAL; |
| 4277 | |
| 4278 | if (cgrp->parent == NULL) |
| 4279 | return -EINVAL; |
| 4280 | |
| 4281 | parent = mem_cgroup_from_cont(cgrp->parent); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4282 | |
| 4283 | cgroup_lock(); |
| 4284 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4285 | /* If under hierarchy, only empty-root can set this value */ |
| 4286 | if ((parent->use_hierarchy) || |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4287 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
| 4288 | cgroup_unlock(); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4289 | return -EINVAL; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4290 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4291 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4292 | memcg->swappiness = val; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4293 | |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4294 | cgroup_unlock(); |
| 4295 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4296 | return 0; |
| 4297 | } |
| 4298 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4299 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) |
| 4300 | { |
| 4301 | struct mem_cgroup_threshold_ary *t; |
| 4302 | u64 usage; |
| 4303 | int i; |
| 4304 | |
| 4305 | rcu_read_lock(); |
| 4306 | if (!swap) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4307 | t = rcu_dereference(memcg->thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4308 | else |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4309 | t = rcu_dereference(memcg->memsw_thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4310 | |
| 4311 | if (!t) |
| 4312 | goto unlock; |
| 4313 | |
| 4314 | usage = mem_cgroup_usage(memcg, swap); |
| 4315 | |
| 4316 | /* |
| 4317 | * current_threshold points to threshold just below usage. |
| 4318 | * If it's not true, a threshold was crossed after last |
| 4319 | * call of __mem_cgroup_threshold(). |
| 4320 | */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4321 | i = t->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4322 | |
| 4323 | /* |
| 4324 | * Iterate backward over array of thresholds starting from |
| 4325 | * current_threshold and check if a threshold is crossed. |
| 4326 | * If none of thresholds below usage is crossed, we read |
| 4327 | * only one element of the array here. |
| 4328 | */ |
| 4329 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) |
| 4330 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4331 | |
| 4332 | /* i = current_threshold + 1 */ |
| 4333 | i++; |
| 4334 | |
| 4335 | /* |
| 4336 | * Iterate forward over array of thresholds starting from |
| 4337 | * current_threshold+1 and check if a threshold is crossed. |
| 4338 | * If none of thresholds above usage is crossed, we read |
| 4339 | * only one element of the array here. |
| 4340 | */ |
| 4341 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) |
| 4342 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4343 | |
| 4344 | /* Update current_threshold */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4345 | t->current_threshold = i - 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4346 | unlock: |
| 4347 | rcu_read_unlock(); |
| 4348 | } |
| 4349 | |
| 4350 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) |
| 4351 | { |
Kirill A. Shutemov | ad4ca5f | 2010-10-07 12:59:27 -0700 | [diff] [blame] | 4352 | while (memcg) { |
| 4353 | __mem_cgroup_threshold(memcg, false); |
| 4354 | if (do_swap_account) |
| 4355 | __mem_cgroup_threshold(memcg, true); |
| 4356 | |
| 4357 | memcg = parent_mem_cgroup(memcg); |
| 4358 | } |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4359 | } |
| 4360 | |
| 4361 | static int compare_thresholds(const void *a, const void *b) |
| 4362 | { |
| 4363 | const struct mem_cgroup_threshold *_a = a; |
| 4364 | const struct mem_cgroup_threshold *_b = b; |
| 4365 | |
| 4366 | return _a->threshold - _b->threshold; |
| 4367 | } |
| 4368 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4369 | static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4370 | { |
| 4371 | struct mem_cgroup_eventfd_list *ev; |
| 4372 | |
| 4373 | list_for_each_entry(ev, &mem->oom_notify, list) |
| 4374 | eventfd_signal(ev->eventfd, 1); |
| 4375 | return 0; |
| 4376 | } |
| 4377 | |
| 4378 | static void mem_cgroup_oom_notify(struct mem_cgroup *mem) |
| 4379 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4380 | struct mem_cgroup *iter; |
| 4381 | |
| 4382 | for_each_mem_cgroup_tree(iter, mem) |
| 4383 | mem_cgroup_oom_notify_cb(iter); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4384 | } |
| 4385 | |
| 4386 | static int mem_cgroup_usage_register_event(struct cgroup *cgrp, |
| 4387 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4388 | { |
| 4389 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4390 | struct mem_cgroup_thresholds *thresholds; |
| 4391 | struct mem_cgroup_threshold_ary *new; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4392 | int type = MEMFILE_TYPE(cft->private); |
| 4393 | u64 threshold, usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4394 | int i, size, ret; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4395 | |
| 4396 | ret = res_counter_memparse_write_strategy(args, &threshold); |
| 4397 | if (ret) |
| 4398 | return ret; |
| 4399 | |
| 4400 | mutex_lock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4401 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4402 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4403 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4404 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4405 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4406 | else |
| 4407 | BUG(); |
| 4408 | |
| 4409 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 4410 | |
| 4411 | /* Check if a threshold crossed before adding a new one */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4412 | if (thresholds->primary) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4413 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4414 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4415 | size = thresholds->primary ? thresholds->primary->size + 1 : 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4416 | |
| 4417 | /* Allocate memory for new array of thresholds */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4418 | new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4419 | GFP_KERNEL); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4420 | if (!new) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4421 | ret = -ENOMEM; |
| 4422 | goto unlock; |
| 4423 | } |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4424 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4425 | |
| 4426 | /* Copy thresholds (if any) to new array */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4427 | if (thresholds->primary) { |
| 4428 | memcpy(new->entries, thresholds->primary->entries, (size - 1) * |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4429 | sizeof(struct mem_cgroup_threshold)); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4430 | } |
| 4431 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4432 | /* Add new threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4433 | new->entries[size - 1].eventfd = eventfd; |
| 4434 | new->entries[size - 1].threshold = threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4435 | |
| 4436 | /* Sort thresholds. Registering of new threshold isn't time-critical */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4437 | sort(new->entries, size, sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4438 | compare_thresholds, NULL); |
| 4439 | |
| 4440 | /* Find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4441 | new->current_threshold = -1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4442 | for (i = 0; i < size; i++) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4443 | if (new->entries[i].threshold < usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4444 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4445 | * new->current_threshold will not be used until |
| 4446 | * rcu_assign_pointer(), so it's safe to increment |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4447 | * it here. |
| 4448 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4449 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4450 | } |
| 4451 | } |
| 4452 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4453 | /* Free old spare buffer and save old primary buffer as spare */ |
| 4454 | kfree(thresholds->spare); |
| 4455 | thresholds->spare = thresholds->primary; |
| 4456 | |
| 4457 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4458 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4459 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4460 | synchronize_rcu(); |
| 4461 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4462 | unlock: |
| 4463 | mutex_unlock(&memcg->thresholds_lock); |
| 4464 | |
| 4465 | return ret; |
| 4466 | } |
| 4467 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4468 | static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4469 | struct cftype *cft, struct eventfd_ctx *eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4470 | { |
| 4471 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4472 | struct mem_cgroup_thresholds *thresholds; |
| 4473 | struct mem_cgroup_threshold_ary *new; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4474 | int type = MEMFILE_TYPE(cft->private); |
| 4475 | u64 usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4476 | int i, j, size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4477 | |
| 4478 | mutex_lock(&memcg->thresholds_lock); |
| 4479 | if (type == _MEM) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4480 | thresholds = &memcg->thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4481 | else if (type == _MEMSWAP) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4482 | thresholds = &memcg->memsw_thresholds; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4483 | else |
| 4484 | BUG(); |
| 4485 | |
| 4486 | /* |
| 4487 | * Something went wrong if we trying to unregister a threshold |
| 4488 | * if we don't have thresholds |
| 4489 | */ |
| 4490 | BUG_ON(!thresholds); |
| 4491 | |
| 4492 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
| 4493 | |
| 4494 | /* Check if a threshold crossed before removing */ |
| 4495 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4496 | |
| 4497 | /* Calculate new number of threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4498 | size = 0; |
| 4499 | for (i = 0; i < thresholds->primary->size; i++) { |
| 4500 | if (thresholds->primary->entries[i].eventfd != eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4501 | size++; |
| 4502 | } |
| 4503 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4504 | new = thresholds->spare; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4505 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4506 | /* Set thresholds array to NULL if we don't have thresholds */ |
| 4507 | if (!size) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4508 | kfree(new); |
| 4509 | new = NULL; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4510 | goto swap_buffers; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4511 | } |
| 4512 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4513 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4514 | |
| 4515 | /* Copy thresholds and find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4516 | new->current_threshold = -1; |
| 4517 | for (i = 0, j = 0; i < thresholds->primary->size; i++) { |
| 4518 | if (thresholds->primary->entries[i].eventfd == eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4519 | continue; |
| 4520 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4521 | new->entries[j] = thresholds->primary->entries[i]; |
| 4522 | if (new->entries[j].threshold < usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4523 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4524 | * new->current_threshold will not be used |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4525 | * until rcu_assign_pointer(), so it's safe to increment |
| 4526 | * it here. |
| 4527 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4528 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4529 | } |
| 4530 | j++; |
| 4531 | } |
| 4532 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4533 | swap_buffers: |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4534 | /* Swap primary and spare array */ |
| 4535 | thresholds->spare = thresholds->primary; |
| 4536 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4537 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4538 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4539 | synchronize_rcu(); |
| 4540 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4541 | mutex_unlock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4542 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4543 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4544 | static int mem_cgroup_oom_register_event(struct cgroup *cgrp, |
| 4545 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) |
| 4546 | { |
| 4547 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 4548 | struct mem_cgroup_eventfd_list *event; |
| 4549 | int type = MEMFILE_TYPE(cft->private); |
| 4550 | |
| 4551 | BUG_ON(type != _OOM_TYPE); |
| 4552 | event = kmalloc(sizeof(*event), GFP_KERNEL); |
| 4553 | if (!event) |
| 4554 | return -ENOMEM; |
| 4555 | |
| 4556 | mutex_lock(&memcg_oom_mutex); |
| 4557 | |
| 4558 | event->eventfd = eventfd; |
| 4559 | list_add(&event->list, &memcg->oom_notify); |
| 4560 | |
| 4561 | /* already in OOM ? */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame^] | 4562 | if (atomic_read(&memcg->under_oom)) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4563 | eventfd_signal(eventfd, 1); |
| 4564 | mutex_unlock(&memcg_oom_mutex); |
| 4565 | |
| 4566 | return 0; |
| 4567 | } |
| 4568 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4569 | static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4570 | struct cftype *cft, struct eventfd_ctx *eventfd) |
| 4571 | { |
| 4572 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4573 | struct mem_cgroup_eventfd_list *ev, *tmp; |
| 4574 | int type = MEMFILE_TYPE(cft->private); |
| 4575 | |
| 4576 | BUG_ON(type != _OOM_TYPE); |
| 4577 | |
| 4578 | mutex_lock(&memcg_oom_mutex); |
| 4579 | |
| 4580 | list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) { |
| 4581 | if (ev->eventfd == eventfd) { |
| 4582 | list_del(&ev->list); |
| 4583 | kfree(ev); |
| 4584 | } |
| 4585 | } |
| 4586 | |
| 4587 | mutex_unlock(&memcg_oom_mutex); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4588 | } |
| 4589 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4590 | static int mem_cgroup_oom_control_read(struct cgroup *cgrp, |
| 4591 | struct cftype *cft, struct cgroup_map_cb *cb) |
| 4592 | { |
| 4593 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4594 | |
| 4595 | cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable); |
| 4596 | |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame^] | 4597 | if (atomic_read(&mem->under_oom)) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4598 | cb->fill(cb, "under_oom", 1); |
| 4599 | else |
| 4600 | cb->fill(cb, "under_oom", 0); |
| 4601 | return 0; |
| 4602 | } |
| 4603 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4604 | static int mem_cgroup_oom_control_write(struct cgroup *cgrp, |
| 4605 | struct cftype *cft, u64 val) |
| 4606 | { |
| 4607 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); |
| 4608 | struct mem_cgroup *parent; |
| 4609 | |
| 4610 | /* cannot set to root cgroup and only 0 and 1 are allowed */ |
| 4611 | if (!cgrp->parent || !((val == 0) || (val == 1))) |
| 4612 | return -EINVAL; |
| 4613 | |
| 4614 | parent = mem_cgroup_from_cont(cgrp->parent); |
| 4615 | |
| 4616 | cgroup_lock(); |
| 4617 | /* oom-kill-disable is a flag for subhierarchy. */ |
| 4618 | if ((parent->use_hierarchy) || |
| 4619 | (mem->use_hierarchy && !list_empty(&cgrp->children))) { |
| 4620 | cgroup_unlock(); |
| 4621 | return -EINVAL; |
| 4622 | } |
| 4623 | mem->oom_kill_disable = val; |
KAMEZAWA Hiroyuki | 4d845eb | 2010-06-29 15:05:18 -0700 | [diff] [blame] | 4624 | if (!val) |
| 4625 | memcg_oom_recover(mem); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4626 | cgroup_unlock(); |
| 4627 | return 0; |
| 4628 | } |
| 4629 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4630 | #ifdef CONFIG_NUMA |
| 4631 | static const struct file_operations mem_control_numa_stat_file_operations = { |
| 4632 | .read = seq_read, |
| 4633 | .llseek = seq_lseek, |
| 4634 | .release = single_release, |
| 4635 | }; |
| 4636 | |
| 4637 | static int mem_control_numa_stat_open(struct inode *unused, struct file *file) |
| 4638 | { |
| 4639 | struct cgroup *cont = file->f_dentry->d_parent->d_fsdata; |
| 4640 | |
| 4641 | file->f_op = &mem_control_numa_stat_file_operations; |
| 4642 | return single_open(file, mem_control_numa_stat_show, cont); |
| 4643 | } |
| 4644 | #endif /* CONFIG_NUMA */ |
| 4645 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4646 | static struct cftype mem_cgroup_files[] = { |
| 4647 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4648 | .name = "usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4649 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4650 | .read_u64 = mem_cgroup_read, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4651 | .register_event = mem_cgroup_usage_register_event, |
| 4652 | .unregister_event = mem_cgroup_usage_unregister_event, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4653 | }, |
| 4654 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4655 | .name = "max_usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4656 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4657 | .trigger = mem_cgroup_reset, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4658 | .read_u64 = mem_cgroup_read, |
| 4659 | }, |
| 4660 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4661 | .name = "limit_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4662 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 4663 | .write_string = mem_cgroup_write, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4664 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4665 | }, |
| 4666 | { |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 4667 | .name = "soft_limit_in_bytes", |
| 4668 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), |
| 4669 | .write_string = mem_cgroup_write, |
| 4670 | .read_u64 = mem_cgroup_read, |
| 4671 | }, |
| 4672 | { |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4673 | .name = "failcnt", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4674 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4675 | .trigger = mem_cgroup_reset, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 4676 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4677 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 4678 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4679 | .name = "stat", |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 4680 | .read_map = mem_control_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4681 | }, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4682 | { |
| 4683 | .name = "force_empty", |
| 4684 | .trigger = mem_cgroup_force_empty_write, |
| 4685 | }, |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4686 | { |
| 4687 | .name = "use_hierarchy", |
| 4688 | .write_u64 = mem_cgroup_hierarchy_write, |
| 4689 | .read_u64 = mem_cgroup_hierarchy_read, |
| 4690 | }, |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4691 | { |
| 4692 | .name = "swappiness", |
| 4693 | .read_u64 = mem_cgroup_swappiness_read, |
| 4694 | .write_u64 = mem_cgroup_swappiness_write, |
| 4695 | }, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4696 | { |
| 4697 | .name = "move_charge_at_immigrate", |
| 4698 | .read_u64 = mem_cgroup_move_charge_read, |
| 4699 | .write_u64 = mem_cgroup_move_charge_write, |
| 4700 | }, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4701 | { |
| 4702 | .name = "oom_control", |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4703 | .read_map = mem_cgroup_oom_control_read, |
| 4704 | .write_u64 = mem_cgroup_oom_control_write, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4705 | .register_event = mem_cgroup_oom_register_event, |
| 4706 | .unregister_event = mem_cgroup_oom_unregister_event, |
| 4707 | .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL), |
| 4708 | }, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4709 | #ifdef CONFIG_NUMA |
| 4710 | { |
| 4711 | .name = "numa_stat", |
| 4712 | .open = mem_control_numa_stat_open, |
KAMEZAWA Hiroyuki | 8957712 | 2011-06-15 15:08:41 -0700 | [diff] [blame] | 4713 | .mode = S_IRUGO, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4714 | }, |
| 4715 | #endif |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4716 | }; |
| 4717 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4718 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 4719 | static struct cftype memsw_cgroup_files[] = { |
| 4720 | { |
| 4721 | .name = "memsw.usage_in_bytes", |
| 4722 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), |
| 4723 | .read_u64 = mem_cgroup_read, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4724 | .register_event = mem_cgroup_usage_register_event, |
| 4725 | .unregister_event = mem_cgroup_usage_unregister_event, |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4726 | }, |
| 4727 | { |
| 4728 | .name = "memsw.max_usage_in_bytes", |
| 4729 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), |
| 4730 | .trigger = mem_cgroup_reset, |
| 4731 | .read_u64 = mem_cgroup_read, |
| 4732 | }, |
| 4733 | { |
| 4734 | .name = "memsw.limit_in_bytes", |
| 4735 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), |
| 4736 | .write_string = mem_cgroup_write, |
| 4737 | .read_u64 = mem_cgroup_read, |
| 4738 | }, |
| 4739 | { |
| 4740 | .name = "memsw.failcnt", |
| 4741 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), |
| 4742 | .trigger = mem_cgroup_reset, |
| 4743 | .read_u64 = mem_cgroup_read, |
| 4744 | }, |
| 4745 | }; |
| 4746 | |
| 4747 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4748 | { |
| 4749 | if (!do_swap_account) |
| 4750 | return 0; |
| 4751 | return cgroup_add_files(cont, ss, memsw_cgroup_files, |
| 4752 | ARRAY_SIZE(memsw_cgroup_files)); |
| 4753 | }; |
| 4754 | #else |
| 4755 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 4756 | { |
| 4757 | return 0; |
| 4758 | } |
| 4759 | #endif |
| 4760 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4761 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 4762 | { |
| 4763 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4764 | struct mem_cgroup_per_zone *mz; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 4765 | enum lru_list l; |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 4766 | int zone, tmp = node; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4767 | /* |
| 4768 | * This routine is called against possible nodes. |
| 4769 | * But it's BUG to call kmalloc() against offline node. |
| 4770 | * |
| 4771 | * TODO: this routine can waste much memory for nodes which will |
| 4772 | * never be onlined. It's better to use memory hotplug callback |
| 4773 | * function. |
| 4774 | */ |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 4775 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 4776 | tmp = -1; |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4777 | pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4778 | if (!pn) |
| 4779 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4780 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4781 | mem->info.nodeinfo[node] = pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4782 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 4783 | mz = &pn->zoneinfo[zone]; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 4784 | for_each_lru(l) |
| 4785 | INIT_LIST_HEAD(&mz->lists[l]); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4786 | mz->usage_in_excess = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 4787 | mz->on_tree = false; |
| 4788 | mz->mem = mem; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4789 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4790 | return 0; |
| 4791 | } |
| 4792 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 4793 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 4794 | { |
| 4795 | kfree(mem->info.nodeinfo[node]); |
| 4796 | } |
| 4797 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4798 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 4799 | { |
| 4800 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4801 | int size = sizeof(struct mem_cgroup); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4802 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4803 | /* Can be very big if MAX_NUMNODES is very big */ |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4804 | if (size < PAGE_SIZE) |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4805 | mem = kzalloc(size, GFP_KERNEL); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4806 | else |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 4807 | mem = vzalloc(size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4808 | |
Dan Carpenter | e7bbcdf | 2010-03-23 13:35:12 -0700 | [diff] [blame] | 4809 | if (!mem) |
| 4810 | return NULL; |
| 4811 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4812 | mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu); |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 4813 | if (!mem->stat) |
| 4814 | goto out_free; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 4815 | spin_lock_init(&mem->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4816 | return mem; |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 4817 | |
| 4818 | out_free: |
| 4819 | if (size < PAGE_SIZE) |
| 4820 | kfree(mem); |
| 4821 | else |
| 4822 | vfree(mem); |
| 4823 | return NULL; |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4824 | } |
| 4825 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4826 | /* |
| 4827 | * At destroying mem_cgroup, references from swap_cgroup can remain. |
| 4828 | * (scanning all at force_empty is too costly...) |
| 4829 | * |
| 4830 | * Instead of clearing all references at force_empty, we remember |
| 4831 | * the number of reference from swap_cgroup and free mem_cgroup when |
| 4832 | * it goes down to 0. |
| 4833 | * |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4834 | * Removal of cgroup itself succeeds regardless of refs from swap. |
| 4835 | */ |
| 4836 | |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 4837 | static void __mem_cgroup_free(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4838 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4839 | int node; |
| 4840 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4841 | mem_cgroup_remove_from_trees(mem); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4842 | free_css_id(&mem_cgroup_subsys, &mem->css); |
| 4843 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 4844 | for_each_node_state(node, N_POSSIBLE) |
| 4845 | free_mem_cgroup_per_zone_info(mem, node); |
| 4846 | |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 4847 | free_percpu(mem->stat); |
| 4848 | if (sizeof(struct mem_cgroup) < PAGE_SIZE) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4849 | kfree(mem); |
| 4850 | else |
| 4851 | vfree(mem); |
| 4852 | } |
| 4853 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4854 | static void mem_cgroup_get(struct mem_cgroup *mem) |
| 4855 | { |
| 4856 | atomic_inc(&mem->refcnt); |
| 4857 | } |
| 4858 | |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4859 | static void __mem_cgroup_put(struct mem_cgroup *mem, int count) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4860 | { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4861 | if (atomic_sub_and_test(count, &mem->refcnt)) { |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4862 | struct mem_cgroup *parent = parent_mem_cgroup(mem); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 4863 | __mem_cgroup_free(mem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4864 | if (parent) |
| 4865 | mem_cgroup_put(parent); |
| 4866 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4867 | } |
| 4868 | |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 4869 | static void mem_cgroup_put(struct mem_cgroup *mem) |
| 4870 | { |
| 4871 | __mem_cgroup_put(mem, 1); |
| 4872 | } |
| 4873 | |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4874 | /* |
| 4875 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. |
| 4876 | */ |
| 4877 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem) |
| 4878 | { |
| 4879 | if (!mem->res.parent) |
| 4880 | return NULL; |
| 4881 | return mem_cgroup_from_res_counter(mem->res.parent, res); |
| 4882 | } |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 4883 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4884 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 4885 | static void __init enable_swap_cgroup(void) |
| 4886 | { |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 4887 | if (!mem_cgroup_disabled() && really_do_swap_account) |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4888 | do_swap_account = 1; |
| 4889 | } |
| 4890 | #else |
| 4891 | static void __init enable_swap_cgroup(void) |
| 4892 | { |
| 4893 | } |
| 4894 | #endif |
| 4895 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4896 | static int mem_cgroup_soft_limit_tree_init(void) |
| 4897 | { |
| 4898 | struct mem_cgroup_tree_per_node *rtpn; |
| 4899 | struct mem_cgroup_tree_per_zone *rtpz; |
| 4900 | int tmp, node, zone; |
| 4901 | |
| 4902 | for_each_node_state(node, N_POSSIBLE) { |
| 4903 | tmp = node; |
| 4904 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 4905 | tmp = -1; |
| 4906 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp); |
| 4907 | if (!rtpn) |
| 4908 | return 1; |
| 4909 | |
| 4910 | soft_limit_tree.rb_tree_per_node[node] = rtpn; |
| 4911 | |
| 4912 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 4913 | rtpz = &rtpn->rb_tree_per_zone[zone]; |
| 4914 | rtpz->rb_root = RB_ROOT; |
| 4915 | spin_lock_init(&rtpz->lock); |
| 4916 | } |
| 4917 | } |
| 4918 | return 0; |
| 4919 | } |
| 4920 | |
Li Zefan | 0eb253e | 2009-01-15 13:51:25 -0800 | [diff] [blame] | 4921 | static struct cgroup_subsys_state * __ref |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4922 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 4923 | { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4924 | struct mem_cgroup *mem, *parent; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4925 | long error = -ENOMEM; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4926 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4927 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 4928 | mem = mem_cgroup_alloc(); |
| 4929 | if (!mem) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4930 | return ERR_PTR(error); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 4931 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4932 | for_each_node_state(node, N_POSSIBLE) |
| 4933 | if (alloc_mem_cgroup_per_zone_info(mem, node)) |
| 4934 | goto free_out; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4935 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4936 | /* root ? */ |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4937 | if (cont->parent == NULL) { |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 4938 | int cpu; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 4939 | enable_swap_cgroup(); |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4940 | parent = NULL; |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 4941 | root_mem_cgroup = mem; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4942 | if (mem_cgroup_soft_limit_tree_init()) |
| 4943 | goto free_out; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 4944 | for_each_possible_cpu(cpu) { |
| 4945 | struct memcg_stock_pcp *stock = |
| 4946 | &per_cpu(memcg_stock, cpu); |
| 4947 | INIT_WORK(&stock->work, drain_local_stock); |
| 4948 | } |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 4949 | hotcpu_notifier(memcg_cpu_hotplug_callback, 0); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4950 | } else { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4951 | parent = mem_cgroup_from_cont(cont->parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4952 | mem->use_hierarchy = parent->use_hierarchy; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4953 | mem->oom_kill_disable = parent->oom_kill_disable; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4954 | } |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 4955 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4956 | if (parent && parent->use_hierarchy) { |
| 4957 | res_counter_init(&mem->res, &parent->res); |
| 4958 | res_counter_init(&mem->memsw, &parent->memsw); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 4959 | /* |
| 4960 | * We increment refcnt of the parent to ensure that we can |
| 4961 | * safely access it on res_counter_charge/uncharge. |
| 4962 | * This refcnt will be decremented when freeing this |
| 4963 | * mem_cgroup(see mem_cgroup_put). |
| 4964 | */ |
| 4965 | mem_cgroup_get(parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 4966 | } else { |
| 4967 | res_counter_init(&mem->res, NULL); |
| 4968 | res_counter_init(&mem->memsw, NULL); |
| 4969 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4970 | mem->last_scanned_child = 0; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 4971 | mem->last_scanned_node = MAX_NUMNODES; |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4972 | INIT_LIST_HEAD(&mem->oom_notify); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 4973 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4974 | if (parent) |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 4975 | mem->swappiness = mem_cgroup_swappiness(parent); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 4976 | atomic_set(&mem->refcnt, 1); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4977 | mem->move_charge_at_immigrate = 0; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4978 | mutex_init(&mem->thresholds_lock); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4979 | return &mem->css; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4980 | free_out: |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 4981 | __mem_cgroup_free(mem); |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 4982 | root_mem_cgroup = NULL; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 4983 | return ERR_PTR(error); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4984 | } |
| 4985 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4986 | static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 4987 | struct cgroup *cont) |
| 4988 | { |
| 4989 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4990 | |
| 4991 | return mem_cgroup_force_empty(mem, false); |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 4992 | } |
| 4993 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4994 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 4995 | struct cgroup *cont) |
| 4996 | { |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 4997 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 4998 | |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 4999 | mem_cgroup_put(mem); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5000 | } |
| 5001 | |
| 5002 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 5003 | struct cgroup *cont) |
| 5004 | { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5005 | int ret; |
| 5006 | |
| 5007 | ret = cgroup_add_files(cont, ss, mem_cgroup_files, |
| 5008 | ARRAY_SIZE(mem_cgroup_files)); |
| 5009 | |
| 5010 | if (!ret) |
| 5011 | ret = register_memsw_files(cont, ss); |
| 5012 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5013 | } |
| 5014 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5015 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5016 | /* Handlers for move charge at task migration. */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5017 | #define PRECHARGE_COUNT_AT_ONCE 256 |
| 5018 | static int mem_cgroup_do_precharge(unsigned long count) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5019 | { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5020 | int ret = 0; |
| 5021 | int batch_count = PRECHARGE_COUNT_AT_ONCE; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5022 | struct mem_cgroup *mem = mc.to; |
| 5023 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5024 | if (mem_cgroup_is_root(mem)) { |
| 5025 | mc.precharge += count; |
| 5026 | /* we don't need css_get for root */ |
| 5027 | return ret; |
| 5028 | } |
| 5029 | /* try to charge at once */ |
| 5030 | if (count > 1) { |
| 5031 | struct res_counter *dummy; |
| 5032 | /* |
| 5033 | * "mem" cannot be under rmdir() because we've already checked |
| 5034 | * by cgroup_lock_live_cgroup() that it is not removed and we |
| 5035 | * are still under the same cgroup_mutex. So we can postpone |
| 5036 | * css_get(). |
| 5037 | */ |
| 5038 | if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy)) |
| 5039 | goto one_by_one; |
| 5040 | if (do_swap_account && res_counter_charge(&mem->memsw, |
| 5041 | PAGE_SIZE * count, &dummy)) { |
| 5042 | res_counter_uncharge(&mem->res, PAGE_SIZE * count); |
| 5043 | goto one_by_one; |
| 5044 | } |
| 5045 | mc.precharge += count; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5046 | return ret; |
| 5047 | } |
| 5048 | one_by_one: |
| 5049 | /* fall back to one by one charge */ |
| 5050 | while (count--) { |
| 5051 | if (signal_pending(current)) { |
| 5052 | ret = -EINTR; |
| 5053 | break; |
| 5054 | } |
| 5055 | if (!batch_count--) { |
| 5056 | batch_count = PRECHARGE_COUNT_AT_ONCE; |
| 5057 | cond_resched(); |
| 5058 | } |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 5059 | ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, 1, &mem, false); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5060 | if (ret || !mem) |
| 5061 | /* mem_cgroup_clear_mc() will do uncharge later */ |
| 5062 | return -ENOMEM; |
| 5063 | mc.precharge++; |
| 5064 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5065 | return ret; |
| 5066 | } |
| 5067 | |
| 5068 | /** |
| 5069 | * is_target_pte_for_mc - check a pte whether it is valid for move charge |
| 5070 | * @vma: the vma the pte to be checked belongs |
| 5071 | * @addr: the address corresponding to the pte to be checked |
| 5072 | * @ptent: the pte to be checked |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5073 | * @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] | 5074 | * |
| 5075 | * Returns |
| 5076 | * 0(MC_TARGET_NONE): if the pte is not a target for move charge. |
| 5077 | * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for |
| 5078 | * move charge. if @target is not NULL, the page is stored in target->page |
| 5079 | * with extra refcnt got(Callers should handle it). |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5080 | * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a |
| 5081 | * target for charge migration. if @target is not NULL, the entry is stored |
| 5082 | * in target->ent. |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5083 | * |
| 5084 | * Called with pte lock held. |
| 5085 | */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5086 | union mc_target { |
| 5087 | struct page *page; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5088 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5089 | }; |
| 5090 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5091 | enum mc_target_type { |
| 5092 | MC_TARGET_NONE, /* not used */ |
| 5093 | MC_TARGET_PAGE, |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5094 | MC_TARGET_SWAP, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5095 | }; |
| 5096 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5097 | static struct page *mc_handle_present_pte(struct vm_area_struct *vma, |
| 5098 | unsigned long addr, pte_t ptent) |
| 5099 | { |
| 5100 | struct page *page = vm_normal_page(vma, addr, ptent); |
| 5101 | |
| 5102 | if (!page || !page_mapped(page)) |
| 5103 | return NULL; |
| 5104 | if (PageAnon(page)) { |
| 5105 | /* we don't move shared anon */ |
| 5106 | if (!move_anon() || page_mapcount(page) > 2) |
| 5107 | return NULL; |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5108 | } else if (!move_file()) |
| 5109 | /* we ignore mapcount for file pages */ |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5110 | return NULL; |
| 5111 | if (!get_page_unless_zero(page)) |
| 5112 | return NULL; |
| 5113 | |
| 5114 | return page; |
| 5115 | } |
| 5116 | |
| 5117 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
| 5118 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5119 | { |
| 5120 | int usage_count; |
| 5121 | struct page *page = NULL; |
| 5122 | swp_entry_t ent = pte_to_swp_entry(ptent); |
| 5123 | |
| 5124 | if (!move_anon() || non_swap_entry(ent)) |
| 5125 | return NULL; |
| 5126 | usage_count = mem_cgroup_count_swap_user(ent, &page); |
| 5127 | if (usage_count > 1) { /* we don't move shared anon */ |
| 5128 | if (page) |
| 5129 | put_page(page); |
| 5130 | return NULL; |
| 5131 | } |
| 5132 | if (do_swap_account) |
| 5133 | entry->val = ent.val; |
| 5134 | |
| 5135 | return page; |
| 5136 | } |
| 5137 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5138 | static struct page *mc_handle_file_pte(struct vm_area_struct *vma, |
| 5139 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5140 | { |
| 5141 | struct page *page = NULL; |
| 5142 | struct inode *inode; |
| 5143 | struct address_space *mapping; |
| 5144 | pgoff_t pgoff; |
| 5145 | |
| 5146 | if (!vma->vm_file) /* anonymous vma */ |
| 5147 | return NULL; |
| 5148 | if (!move_file()) |
| 5149 | return NULL; |
| 5150 | |
| 5151 | inode = vma->vm_file->f_path.dentry->d_inode; |
| 5152 | mapping = vma->vm_file->f_mapping; |
| 5153 | if (pte_none(ptent)) |
| 5154 | pgoff = linear_page_index(vma, addr); |
| 5155 | else /* pte_file(ptent) is true */ |
| 5156 | pgoff = pte_to_pgoff(ptent); |
| 5157 | |
| 5158 | /* page is moved even if it's not RSS of this task(page-faulted). */ |
| 5159 | if (!mapping_cap_swap_backed(mapping)) { /* normal file */ |
| 5160 | page = find_get_page(mapping, pgoff); |
| 5161 | } else { /* shmem/tmpfs file. we should take account of swap too. */ |
| 5162 | swp_entry_t ent; |
| 5163 | mem_cgroup_get_shmem_target(inode, pgoff, &page, &ent); |
| 5164 | if (do_swap_account) |
| 5165 | entry->val = ent.val; |
| 5166 | } |
| 5167 | |
| 5168 | return page; |
| 5169 | } |
| 5170 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5171 | static int is_target_pte_for_mc(struct vm_area_struct *vma, |
| 5172 | unsigned long addr, pte_t ptent, union mc_target *target) |
| 5173 | { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5174 | struct page *page = NULL; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5175 | struct page_cgroup *pc; |
| 5176 | int ret = 0; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5177 | swp_entry_t ent = { .val = 0 }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5178 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5179 | if (pte_present(ptent)) |
| 5180 | page = mc_handle_present_pte(vma, addr, ptent); |
| 5181 | else if (is_swap_pte(ptent)) |
| 5182 | page = mc_handle_swap_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5183 | else if (pte_none(ptent) || pte_file(ptent)) |
| 5184 | page = mc_handle_file_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5185 | |
| 5186 | if (!page && !ent.val) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5187 | return 0; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5188 | if (page) { |
| 5189 | pc = lookup_page_cgroup(page); |
| 5190 | /* |
| 5191 | * Do only loose check w/o page_cgroup lock. |
| 5192 | * mem_cgroup_move_account() checks the pc is valid or not under |
| 5193 | * the lock. |
| 5194 | */ |
| 5195 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { |
| 5196 | ret = MC_TARGET_PAGE; |
| 5197 | if (target) |
| 5198 | target->page = page; |
| 5199 | } |
| 5200 | if (!ret || !target) |
| 5201 | put_page(page); |
| 5202 | } |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5203 | /* There is a swap entry and a page doesn't exist or isn't charged */ |
| 5204 | if (ent.val && !ret && |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5205 | css_id(&mc.from->css) == lookup_swap_cgroup(ent)) { |
| 5206 | ret = MC_TARGET_SWAP; |
| 5207 | if (target) |
| 5208 | target->ent = ent; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5209 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5210 | return ret; |
| 5211 | } |
| 5212 | |
| 5213 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, |
| 5214 | unsigned long addr, unsigned long end, |
| 5215 | struct mm_walk *walk) |
| 5216 | { |
| 5217 | struct vm_area_struct *vma = walk->private; |
| 5218 | pte_t *pte; |
| 5219 | spinlock_t *ptl; |
| 5220 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 5221 | split_huge_page_pmd(walk->mm, pmd); |
| 5222 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5223 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5224 | for (; addr != end; pte++, addr += PAGE_SIZE) |
| 5225 | if (is_target_pte_for_mc(vma, addr, *pte, NULL)) |
| 5226 | mc.precharge++; /* increment precharge temporarily */ |
| 5227 | pte_unmap_unlock(pte - 1, ptl); |
| 5228 | cond_resched(); |
| 5229 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5230 | return 0; |
| 5231 | } |
| 5232 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5233 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
| 5234 | { |
| 5235 | unsigned long precharge; |
| 5236 | struct vm_area_struct *vma; |
| 5237 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5238 | down_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5239 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5240 | struct mm_walk mem_cgroup_count_precharge_walk = { |
| 5241 | .pmd_entry = mem_cgroup_count_precharge_pte_range, |
| 5242 | .mm = mm, |
| 5243 | .private = vma, |
| 5244 | }; |
| 5245 | if (is_vm_hugetlb_page(vma)) |
| 5246 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5247 | walk_page_range(vma->vm_start, vma->vm_end, |
| 5248 | &mem_cgroup_count_precharge_walk); |
| 5249 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5250 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5251 | |
| 5252 | precharge = mc.precharge; |
| 5253 | mc.precharge = 0; |
| 5254 | |
| 5255 | return precharge; |
| 5256 | } |
| 5257 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5258 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
| 5259 | { |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5260 | unsigned long precharge = mem_cgroup_count_precharge(mm); |
| 5261 | |
| 5262 | VM_BUG_ON(mc.moving_task); |
| 5263 | mc.moving_task = current; |
| 5264 | return mem_cgroup_do_precharge(precharge); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5265 | } |
| 5266 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5267 | /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */ |
| 5268 | static void __mem_cgroup_clear_mc(void) |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5269 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5270 | struct mem_cgroup *from = mc.from; |
| 5271 | struct mem_cgroup *to = mc.to; |
| 5272 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5273 | /* we must uncharge all the leftover precharges from mc.to */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5274 | if (mc.precharge) { |
| 5275 | __mem_cgroup_cancel_charge(mc.to, mc.precharge); |
| 5276 | mc.precharge = 0; |
| 5277 | } |
| 5278 | /* |
| 5279 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so |
| 5280 | * we must uncharge here. |
| 5281 | */ |
| 5282 | if (mc.moved_charge) { |
| 5283 | __mem_cgroup_cancel_charge(mc.from, mc.moved_charge); |
| 5284 | mc.moved_charge = 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5285 | } |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5286 | /* we must fixup refcnts and charges */ |
| 5287 | if (mc.moved_swap) { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5288 | /* uncharge swap account from the old cgroup */ |
| 5289 | if (!mem_cgroup_is_root(mc.from)) |
| 5290 | res_counter_uncharge(&mc.from->memsw, |
| 5291 | PAGE_SIZE * mc.moved_swap); |
| 5292 | __mem_cgroup_put(mc.from, mc.moved_swap); |
| 5293 | |
| 5294 | if (!mem_cgroup_is_root(mc.to)) { |
| 5295 | /* |
| 5296 | * we charged both to->res and to->memsw, so we should |
| 5297 | * uncharge to->res. |
| 5298 | */ |
| 5299 | res_counter_uncharge(&mc.to->res, |
| 5300 | PAGE_SIZE * mc.moved_swap); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5301 | } |
| 5302 | /* we've already done mem_cgroup_get(mc.to) */ |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5303 | mc.moved_swap = 0; |
| 5304 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5305 | memcg_oom_recover(from); |
| 5306 | memcg_oom_recover(to); |
| 5307 | wake_up_all(&mc.waitq); |
| 5308 | } |
| 5309 | |
| 5310 | static void mem_cgroup_clear_mc(void) |
| 5311 | { |
| 5312 | struct mem_cgroup *from = mc.from; |
| 5313 | |
| 5314 | /* |
| 5315 | * we must clear moving_task before waking up waiters at the end of |
| 5316 | * task migration. |
| 5317 | */ |
| 5318 | mc.moving_task = NULL; |
| 5319 | __mem_cgroup_clear_mc(); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5320 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5321 | mc.from = NULL; |
| 5322 | mc.to = NULL; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5323 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5324 | mem_cgroup_end_move(from); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5325 | } |
| 5326 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5327 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 5328 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5329 | struct task_struct *p) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5330 | { |
| 5331 | int ret = 0; |
| 5332 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup); |
| 5333 | |
| 5334 | if (mem->move_charge_at_immigrate) { |
| 5335 | struct mm_struct *mm; |
| 5336 | struct mem_cgroup *from = mem_cgroup_from_task(p); |
| 5337 | |
| 5338 | VM_BUG_ON(from == mem); |
| 5339 | |
| 5340 | mm = get_task_mm(p); |
| 5341 | if (!mm) |
| 5342 | return 0; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5343 | /* We move charges only when we move a owner of the mm */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5344 | if (mm->owner == p) { |
| 5345 | VM_BUG_ON(mc.from); |
| 5346 | VM_BUG_ON(mc.to); |
| 5347 | VM_BUG_ON(mc.precharge); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5348 | VM_BUG_ON(mc.moved_charge); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5349 | VM_BUG_ON(mc.moved_swap); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5350 | mem_cgroup_start_move(from); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5351 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5352 | mc.from = from; |
| 5353 | mc.to = mem; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5354 | spin_unlock(&mc.lock); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5355 | /* We set mc.moving_task later */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5356 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5357 | ret = mem_cgroup_precharge_mc(mm); |
| 5358 | if (ret) |
| 5359 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5360 | } |
| 5361 | mmput(mm); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5362 | } |
| 5363 | return ret; |
| 5364 | } |
| 5365 | |
| 5366 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 5367 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5368 | struct task_struct *p) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5369 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5370 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5371 | } |
| 5372 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5373 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
| 5374 | unsigned long addr, unsigned long end, |
| 5375 | struct mm_walk *walk) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5376 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5377 | int ret = 0; |
| 5378 | struct vm_area_struct *vma = walk->private; |
| 5379 | pte_t *pte; |
| 5380 | spinlock_t *ptl; |
| 5381 | |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 5382 | split_huge_page_pmd(walk->mm, pmd); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5383 | retry: |
| 5384 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5385 | for (; addr != end; addr += PAGE_SIZE) { |
| 5386 | pte_t ptent = *(pte++); |
| 5387 | union mc_target target; |
| 5388 | int type; |
| 5389 | struct page *page; |
| 5390 | struct page_cgroup *pc; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5391 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5392 | |
| 5393 | if (!mc.precharge) |
| 5394 | break; |
| 5395 | |
| 5396 | type = is_target_pte_for_mc(vma, addr, ptent, &target); |
| 5397 | switch (type) { |
| 5398 | case MC_TARGET_PAGE: |
| 5399 | page = target.page; |
| 5400 | if (isolate_lru_page(page)) |
| 5401 | goto put; |
| 5402 | pc = lookup_page_cgroup(page); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 5403 | if (!mem_cgroup_move_account(page, 1, pc, |
| 5404 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5405 | mc.precharge--; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5406 | /* we uncharge from mc.from later. */ |
| 5407 | mc.moved_charge++; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5408 | } |
| 5409 | putback_lru_page(page); |
| 5410 | put: /* is_target_pte_for_mc() gets the page */ |
| 5411 | put_page(page); |
| 5412 | break; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5413 | case MC_TARGET_SWAP: |
| 5414 | ent = target.ent; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5415 | if (!mem_cgroup_move_swap_account(ent, |
| 5416 | mc.from, mc.to, false)) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5417 | mc.precharge--; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5418 | /* we fixup refcnts and charges later. */ |
| 5419 | mc.moved_swap++; |
| 5420 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5421 | break; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5422 | default: |
| 5423 | break; |
| 5424 | } |
| 5425 | } |
| 5426 | pte_unmap_unlock(pte - 1, ptl); |
| 5427 | cond_resched(); |
| 5428 | |
| 5429 | if (addr != end) { |
| 5430 | /* |
| 5431 | * We have consumed all precharges we got in can_attach(). |
| 5432 | * We try charge one by one, but don't do any additional |
| 5433 | * charges to mc.to if we have failed in charge once in attach() |
| 5434 | * phase. |
| 5435 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5436 | ret = mem_cgroup_do_precharge(1); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5437 | if (!ret) |
| 5438 | goto retry; |
| 5439 | } |
| 5440 | |
| 5441 | return ret; |
| 5442 | } |
| 5443 | |
| 5444 | static void mem_cgroup_move_charge(struct mm_struct *mm) |
| 5445 | { |
| 5446 | struct vm_area_struct *vma; |
| 5447 | |
| 5448 | lru_add_drain_all(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5449 | retry: |
| 5450 | if (unlikely(!down_read_trylock(&mm->mmap_sem))) { |
| 5451 | /* |
| 5452 | * Someone who are holding the mmap_sem might be waiting in |
| 5453 | * waitq. So we cancel all extra charges, wake up all waiters, |
| 5454 | * and retry. Because we cancel precharges, we might not be able |
| 5455 | * to move enough charges, but moving charge is a best-effort |
| 5456 | * feature anyway, so it wouldn't be a big problem. |
| 5457 | */ |
| 5458 | __mem_cgroup_clear_mc(); |
| 5459 | cond_resched(); |
| 5460 | goto retry; |
| 5461 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5462 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5463 | int ret; |
| 5464 | struct mm_walk mem_cgroup_move_charge_walk = { |
| 5465 | .pmd_entry = mem_cgroup_move_charge_pte_range, |
| 5466 | .mm = mm, |
| 5467 | .private = vma, |
| 5468 | }; |
| 5469 | if (is_vm_hugetlb_page(vma)) |
| 5470 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5471 | ret = walk_page_range(vma->vm_start, vma->vm_end, |
| 5472 | &mem_cgroup_move_charge_walk); |
| 5473 | if (ret) |
| 5474 | /* |
| 5475 | * means we have consumed all precharges and failed in |
| 5476 | * doing additional charge. Just abandon here. |
| 5477 | */ |
| 5478 | break; |
| 5479 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5480 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5481 | } |
| 5482 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5483 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 5484 | struct cgroup *cont, |
| 5485 | struct cgroup *old_cont, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5486 | struct task_struct *p) |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5487 | { |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5488 | struct mm_struct *mm = get_task_mm(p); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5489 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5490 | if (mm) { |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5491 | if (mc.to) |
| 5492 | mem_cgroup_move_charge(mm); |
| 5493 | put_swap_token(mm); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5494 | mmput(mm); |
| 5495 | } |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5496 | if (mc.to) |
| 5497 | mem_cgroup_clear_mc(); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5498 | } |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5499 | #else /* !CONFIG_MMU */ |
| 5500 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
| 5501 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5502 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5503 | { |
| 5504 | return 0; |
| 5505 | } |
| 5506 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, |
| 5507 | struct cgroup *cgroup, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5508 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5509 | { |
| 5510 | } |
| 5511 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 5512 | struct cgroup *cont, |
| 5513 | struct cgroup *old_cont, |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 5514 | struct task_struct *p) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5515 | { |
| 5516 | } |
| 5517 | #endif |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5518 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5519 | struct cgroup_subsys mem_cgroup_subsys = { |
| 5520 | .name = "memory", |
| 5521 | .subsys_id = mem_cgroup_subsys_id, |
| 5522 | .create = mem_cgroup_create, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5523 | .pre_destroy = mem_cgroup_pre_destroy, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5524 | .destroy = mem_cgroup_destroy, |
| 5525 | .populate = mem_cgroup_populate, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5526 | .can_attach = mem_cgroup_can_attach, |
| 5527 | .cancel_attach = mem_cgroup_cancel_attach, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5528 | .attach = mem_cgroup_move_task, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5529 | .early_init = 0, |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5530 | .use_id = 1, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5531 | }; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5532 | |
| 5533 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5534 | static int __init enable_swap_account(char *s) |
| 5535 | { |
| 5536 | /* consider enabled if no parameter or 1 is given */ |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5537 | if (!strcmp(s, "1")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5538 | really_do_swap_account = 1; |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5539 | else if (!strcmp(s, "0")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 5540 | really_do_swap_account = 0; |
| 5541 | return 1; |
| 5542 | } |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 5543 | __setup("swapaccount=", enable_swap_account); |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5544 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5545 | #endif |