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 | * |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 13 | * Kernel Memory Controller |
| 14 | * Copyright (C) 2012 Parallels Inc. and Google Inc. |
| 15 | * Authors: Glauber Costa and Suleiman Souhlal |
| 16 | * |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 17 | * This program is free software; you can redistribute it and/or modify |
| 18 | * it under the terms of the GNU General Public License as published by |
| 19 | * the Free Software Foundation; either version 2 of the License, or |
| 20 | * (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | * GNU General Public License for more details. |
| 26 | */ |
| 27 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 28 | #include <linux/page_counter.h> |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 29 | #include <linux/memcontrol.h> |
| 30 | #include <linux/cgroup.h> |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 31 | #include <linux/mm.h> |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 32 | #include <linux/hugetlb.h> |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 33 | #include <linux/pagemap.h> |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 34 | #include <linux/smp.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 35 | #include <linux/page-flags.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 36 | #include <linux/backing-dev.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 37 | #include <linux/bit_spinlock.h> |
| 38 | #include <linux/rcupdate.h> |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 39 | #include <linux/limits.h> |
Paul Gortmaker | b9e15ba | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 40 | #include <linux/export.h> |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 41 | #include <linux/mutex.h> |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 42 | #include <linux/rbtree.h> |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 43 | #include <linux/slab.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 44 | #include <linux/swap.h> |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 45 | #include <linux/swapops.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 46 | #include <linux/spinlock.h> |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 47 | #include <linux/eventfd.h> |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 48 | #include <linux/poll.h> |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 49 | #include <linux/sort.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 50 | #include <linux/fs.h> |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 51 | #include <linux/seq_file.h> |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 52 | #include <linux/vmpressure.h> |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 53 | #include <linux/mm_inline.h> |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 54 | #include <linux/page_cgroup.h> |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 55 | #include <linux/cpu.h> |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 56 | #include <linux/oom.h> |
Johannes Weiner | 0056f4e | 2013-10-31 16:34:14 -0700 | [diff] [blame] | 57 | #include <linux/lockdep.h> |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 58 | #include <linux/file.h> |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 59 | #include "internal.h" |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 60 | #include <net/sock.h> |
Michal Hocko | 4bd2c1e | 2012-10-08 16:33:10 -0700 | [diff] [blame] | 61 | #include <net/ip.h> |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 62 | #include <net/tcp_memcontrol.h> |
Qiang Huang | f35c3a8 | 2013-11-12 15:08:22 -0800 | [diff] [blame] | 63 | #include "slab.h" |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 64 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 65 | #include <asm/uaccess.h> |
| 66 | |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 67 | #include <trace/events/vmscan.h> |
| 68 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 69 | struct cgroup_subsys memory_cgrp_subsys __read_mostly; |
| 70 | EXPORT_SYMBOL(memory_cgrp_subsys); |
David Rientjes | 68ae564 | 2012-12-12 13:51:57 -0800 | [diff] [blame] | 71 | |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 72 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
Kirill A. Shutemov | 6bbda35 | 2012-05-29 15:06:55 -0700 | [diff] [blame] | 73 | static struct mem_cgroup *root_mem_cgroup __read_mostly; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 74 | |
Andrew Morton | c255a45 | 2012-07-31 16:43:02 -0700 | [diff] [blame] | 75 | #ifdef CONFIG_MEMCG_SWAP |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 76 | /* 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] | 77 | int do_swap_account __read_mostly; |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 78 | |
| 79 | /* for remember boot option*/ |
Andrew Morton | c255a45 | 2012-07-31 16:43:02 -0700 | [diff] [blame] | 80 | #ifdef CONFIG_MEMCG_SWAP_ENABLED |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 81 | static int really_do_swap_account __initdata = 1; |
| 82 | #else |
Fabian Frederick | ada4ba5 | 2014-06-04 16:08:08 -0700 | [diff] [blame] | 83 | static int really_do_swap_account __initdata; |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 84 | #endif |
| 85 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 86 | #else |
Kirill A. Shutemov | a0db00f | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 87 | #define do_swap_account 0 |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 88 | #endif |
| 89 | |
| 90 | |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 91 | static const char * const mem_cgroup_stat_names[] = { |
| 92 | "cache", |
| 93 | "rss", |
David Rientjes | b070e65 | 2013-05-07 16:18:09 -0700 | [diff] [blame] | 94 | "rss_huge", |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 95 | "mapped_file", |
Sha Zhengju | 3ea67d0 | 2013-09-12 15:13:53 -0700 | [diff] [blame] | 96 | "writeback", |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 97 | "swap", |
| 98 | }; |
| 99 | |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 100 | enum mem_cgroup_events_index { |
| 101 | MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */ |
| 102 | MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */ |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 103 | MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */ |
| 104 | MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */ |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 105 | MEM_CGROUP_EVENTS_NSTATS, |
| 106 | }; |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 107 | |
| 108 | static const char * const mem_cgroup_events_names[] = { |
| 109 | "pgpgin", |
| 110 | "pgpgout", |
| 111 | "pgfault", |
| 112 | "pgmajfault", |
| 113 | }; |
| 114 | |
Sha Zhengju | 58cf188 | 2013-02-22 16:32:05 -0800 | [diff] [blame] | 115 | static const char * const mem_cgroup_lru_names[] = { |
| 116 | "inactive_anon", |
| 117 | "active_anon", |
| 118 | "inactive_file", |
| 119 | "active_file", |
| 120 | "unevictable", |
| 121 | }; |
| 122 | |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 123 | /* |
| 124 | * Per memcg event counter is incremented at every pagein/pageout. With THP, |
| 125 | * it will be incremated by the number of pages. This counter is used for |
| 126 | * for trigger some periodic events. This is straightforward and better |
| 127 | * than using jiffies etc. to handle periodic memcg event. |
| 128 | */ |
| 129 | enum mem_cgroup_events_target { |
| 130 | MEM_CGROUP_TARGET_THRESH, |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 131 | MEM_CGROUP_TARGET_SOFTLIMIT, |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 132 | MEM_CGROUP_TARGET_NUMAINFO, |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 133 | MEM_CGROUP_NTARGETS, |
| 134 | }; |
Kirill A. Shutemov | a0db00f | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 135 | #define THRESHOLDS_EVENTS_TARGET 128 |
| 136 | #define SOFTLIMIT_EVENTS_TARGET 1024 |
| 137 | #define NUMAINFO_EVENTS_TARGET 1024 |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 138 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 139 | struct mem_cgroup_stat_cpu { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 140 | long count[MEM_CGROUP_STAT_NSTATS]; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 141 | unsigned long events[MEM_CGROUP_EVENTS_NSTATS]; |
Johannes Weiner | 1311471 | 2012-05-29 15:07:07 -0700 | [diff] [blame] | 142 | unsigned long nr_page_events; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 143 | unsigned long targets[MEM_CGROUP_NTARGETS]; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 144 | }; |
| 145 | |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 146 | struct reclaim_iter { |
| 147 | struct mem_cgroup *position; |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 148 | /* scan generation, increased every round-trip */ |
| 149 | unsigned int generation; |
| 150 | }; |
| 151 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 152 | /* |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 153 | * per-zone information in memory controller. |
| 154 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 155 | struct mem_cgroup_per_zone { |
Johannes Weiner | 6290df5 | 2012-01-12 17:18:10 -0800 | [diff] [blame] | 156 | struct lruvec lruvec; |
Hugh Dickins | 1eb4927 | 2012-03-21 16:34:19 -0700 | [diff] [blame] | 157 | unsigned long lru_size[NR_LRU_LISTS]; |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 158 | |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 159 | struct reclaim_iter iter[DEF_PRIORITY + 1]; |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 160 | |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 161 | struct rb_node tree_node; /* RB tree node */ |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 162 | unsigned long usage_in_excess;/* Set to the value by which */ |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 163 | /* the soft limit is exceeded*/ |
| 164 | bool on_tree; |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 165 | struct mem_cgroup *memcg; /* Back pointer, we cannot */ |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 166 | /* use container_of */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 167 | }; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 168 | |
| 169 | struct mem_cgroup_per_node { |
| 170 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; |
| 171 | }; |
| 172 | |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 173 | /* |
| 174 | * Cgroups above their limits are maintained in a RB-Tree, independent of |
| 175 | * their hierarchy representation |
| 176 | */ |
| 177 | |
| 178 | struct mem_cgroup_tree_per_zone { |
| 179 | struct rb_root rb_root; |
| 180 | spinlock_t lock; |
| 181 | }; |
| 182 | |
| 183 | struct mem_cgroup_tree_per_node { |
| 184 | struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES]; |
| 185 | }; |
| 186 | |
| 187 | struct mem_cgroup_tree { |
| 188 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; |
| 189 | }; |
| 190 | |
| 191 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; |
| 192 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 193 | struct mem_cgroup_threshold { |
| 194 | struct eventfd_ctx *eventfd; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 195 | unsigned long threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 196 | }; |
| 197 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 198 | /* For threshold */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 199 | struct mem_cgroup_threshold_ary { |
Sha Zhengju | 748dad3 | 2012-05-29 15:06:57 -0700 | [diff] [blame] | 200 | /* An array index points to threshold just below or equal to usage. */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 201 | int current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 202 | /* Size of entries[] */ |
| 203 | unsigned int size; |
| 204 | /* Array of thresholds */ |
| 205 | struct mem_cgroup_threshold entries[0]; |
| 206 | }; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 207 | |
| 208 | struct mem_cgroup_thresholds { |
| 209 | /* Primary thresholds array */ |
| 210 | struct mem_cgroup_threshold_ary *primary; |
| 211 | /* |
| 212 | * Spare threshold array. |
| 213 | * This is needed to make mem_cgroup_unregister_event() "never fail". |
| 214 | * It must be able to store at least primary->size - 1 entries. |
| 215 | */ |
| 216 | struct mem_cgroup_threshold_ary *spare; |
| 217 | }; |
| 218 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 219 | /* for OOM */ |
| 220 | struct mem_cgroup_eventfd_list { |
| 221 | struct list_head list; |
| 222 | struct eventfd_ctx *eventfd; |
| 223 | }; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 224 | |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 225 | /* |
| 226 | * cgroup_event represents events which userspace want to receive. |
| 227 | */ |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 228 | struct mem_cgroup_event { |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 229 | /* |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 230 | * memcg which the event belongs to. |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 231 | */ |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 232 | struct mem_cgroup *memcg; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 233 | /* |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 234 | * eventfd to signal userspace about the event. |
| 235 | */ |
| 236 | struct eventfd_ctx *eventfd; |
| 237 | /* |
| 238 | * Each of these stored in a list by the cgroup. |
| 239 | */ |
| 240 | struct list_head list; |
| 241 | /* |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 242 | * register_event() callback will be used to add new userspace |
| 243 | * waiter for changes related to this event. Use eventfd_signal() |
| 244 | * on eventfd to send notification to userspace. |
| 245 | */ |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 246 | int (*register_event)(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 247 | struct eventfd_ctx *eventfd, const char *args); |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 248 | /* |
| 249 | * unregister_event() callback will be called when userspace closes |
| 250 | * the eventfd or on cgroup removing. This callback must be set, |
| 251 | * if you want provide notification functionality. |
| 252 | */ |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 253 | void (*unregister_event)(struct mem_cgroup *memcg, |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 254 | struct eventfd_ctx *eventfd); |
| 255 | /* |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 256 | * All fields below needed to unregister event when |
| 257 | * userspace closes eventfd. |
| 258 | */ |
| 259 | poll_table pt; |
| 260 | wait_queue_head_t *wqh; |
| 261 | wait_queue_t wait; |
| 262 | struct work_struct remove; |
| 263 | }; |
| 264 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 265 | static void mem_cgroup_threshold(struct mem_cgroup *memcg); |
| 266 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 267 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 268 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 269 | * The memory controller data structure. The memory controller controls both |
| 270 | * page cache and RSS per cgroup. We would eventually like to provide |
| 271 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, |
| 272 | * to help the administrator determine what knobs to tune. |
| 273 | * |
| 274 | * 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] | 275 | * we hit the water mark. May be even add a low water mark, such that |
| 276 | * no reclaim occurs from a cgroup at it's low water mark, this is |
| 277 | * a feature that will be implemented much later in the future. |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 278 | */ |
| 279 | struct mem_cgroup { |
| 280 | struct cgroup_subsys_state css; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 281 | |
| 282 | /* Accounted resources */ |
| 283 | struct page_counter memory; |
| 284 | struct page_counter memsw; |
| 285 | struct page_counter kmem; |
| 286 | |
| 287 | unsigned long soft_limit; |
Hugh Dickins | 59927fb | 2012-03-15 15:17:07 -0700 | [diff] [blame] | 288 | |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 289 | /* vmpressure notifications */ |
| 290 | struct vmpressure vmpressure; |
| 291 | |
Johannes Weiner | 2f7dd7a | 2014-10-02 16:16:57 -0700 | [diff] [blame] | 292 | /* css_online() has been completed */ |
| 293 | int initialized; |
| 294 | |
Li Zefan | 465939a | 2013-07-08 16:00:38 -0700 | [diff] [blame] | 295 | /* |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 296 | * Should the accounting and control be hierarchical, per subtree? |
| 297 | */ |
| 298 | bool use_hierarchy; |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 299 | unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 300 | |
| 301 | bool oom_lock; |
| 302 | atomic_t under_oom; |
Johannes Weiner | 3812c8c | 2013-09-12 15:13:44 -0700 | [diff] [blame] | 303 | atomic_t oom_wakeups; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 304 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 305 | int swappiness; |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 306 | /* OOM-Killer disable */ |
| 307 | int oom_kill_disable; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 308 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 309 | /* protect arrays of thresholds */ |
| 310 | struct mutex thresholds_lock; |
| 311 | |
| 312 | /* thresholds for memory usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 313 | struct mem_cgroup_thresholds thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 314 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 315 | /* thresholds for mem+swap usage. RCU-protected */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 316 | struct mem_cgroup_thresholds memsw_thresholds; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 317 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 318 | /* For oom notifier event fd */ |
| 319 | struct list_head oom_notify; |
Johannes Weiner | 185efc0 | 2011-09-14 16:21:58 -0700 | [diff] [blame] | 320 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 321 | /* |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 322 | * Should we move charges of a task when a task is moved into this |
| 323 | * mem_cgroup ? And what type of charges should we move ? |
| 324 | */ |
Andrew Morton | f894ffa | 2013-09-12 15:13:35 -0700 | [diff] [blame] | 325 | unsigned long move_charge_at_immigrate; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 326 | /* |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 327 | * set > 0 if pages under this cgroup are moving to other cgroup. |
| 328 | */ |
| 329 | atomic_t moving_account; |
KAMEZAWA Hiroyuki | 312734c0 | 2012-03-21 16:34:24 -0700 | [diff] [blame] | 330 | /* taken only while moving_account > 0 */ |
| 331 | spinlock_t move_lock; |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 332 | /* |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 333 | * percpu counter. |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 334 | */ |
Kirill A. Shutemov | 3a7951b | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 335 | struct mem_cgroup_stat_cpu __percpu *stat; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 336 | /* |
| 337 | * used when a cpu is offlined or other synchronizations |
| 338 | * See mem_cgroup_read_stat(). |
| 339 | */ |
| 340 | struct mem_cgroup_stat_cpu nocpu_base; |
| 341 | spinlock_t pcp_counter_lock; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 342 | |
Michal Hocko | 4bd2c1e | 2012-10-08 16:33:10 -0700 | [diff] [blame] | 343 | #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET) |
Eric W. Biederman | 2e685ca | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 344 | struct cg_proto tcp_mem; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 345 | #endif |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 346 | #if defined(CONFIG_MEMCG_KMEM) |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 347 | /* analogous to slab_common's slab_caches list, but per-memcg; |
| 348 | * protected by memcg_slab_mutex */ |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 349 | struct list_head memcg_slab_caches; |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 350 | /* Index in the kmem_cache->memcg_params->memcg_caches array */ |
| 351 | int kmemcg_id; |
| 352 | #endif |
Glauber Costa | 45cf7eb | 2013-02-22 16:34:49 -0800 | [diff] [blame] | 353 | |
| 354 | int last_scanned_node; |
| 355 | #if MAX_NUMNODES > 1 |
| 356 | nodemask_t scan_nodes; |
| 357 | atomic_t numainfo_events; |
| 358 | atomic_t numainfo_updating; |
| 359 | #endif |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 360 | |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 361 | /* List of events which userspace want to receive */ |
| 362 | struct list_head event_list; |
| 363 | spinlock_t event_list_lock; |
| 364 | |
Johannes Weiner | 54f72fe | 2013-07-08 15:59:49 -0700 | [diff] [blame] | 365 | struct mem_cgroup_per_node *nodeinfo[0]; |
| 366 | /* WARNING: nodeinfo must be the last member here */ |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 367 | }; |
| 368 | |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 369 | /* internal only representation about the status of kmem accounting. */ |
| 370 | enum { |
Vladimir Davydov | 6de64be | 2014-01-23 15:53:08 -0800 | [diff] [blame] | 371 | KMEM_ACCOUNTED_ACTIVE, /* accounted by this cgroup itself */ |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 372 | }; |
| 373 | |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 374 | #ifdef CONFIG_MEMCG_KMEM |
| 375 | static inline void memcg_kmem_set_active(struct mem_cgroup *memcg) |
| 376 | { |
| 377 | set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags); |
| 378 | } |
Glauber Costa | 7de3768 | 2012-12-18 14:22:07 -0800 | [diff] [blame] | 379 | |
| 380 | static bool memcg_kmem_is_active(struct mem_cgroup *memcg) |
| 381 | { |
| 382 | return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags); |
| 383 | } |
| 384 | |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 385 | #endif |
| 386 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 387 | /* Stuffs for move charges at task migration. */ |
| 388 | /* |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 389 | * Types of charges to be moved. "move_charge_at_immitgrate" and |
| 390 | * "immigrate_flags" are treated as a left-shifted bitmap of these types. |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 391 | */ |
| 392 | enum move_type { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 393 | MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */ |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 394 | MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 395 | NR_MOVE_TYPE, |
| 396 | }; |
| 397 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 398 | /* "mc" and its members are protected by cgroup_mutex */ |
| 399 | static struct move_charge_struct { |
Daisuke Nishimura | b1dd693 | 2010-11-24 12:57:06 -0800 | [diff] [blame] | 400 | spinlock_t lock; /* for from, to */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 401 | struct mem_cgroup *from; |
| 402 | struct mem_cgroup *to; |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 403 | unsigned long immigrate_flags; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 404 | unsigned long precharge; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 405 | unsigned long moved_charge; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 406 | unsigned long moved_swap; |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 407 | struct task_struct *moving_task; /* a task moving charges */ |
| 408 | wait_queue_head_t waitq; /* a waitq for other context */ |
| 409 | } mc = { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 410 | .lock = __SPIN_LOCK_UNLOCKED(mc.lock), |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 411 | .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq), |
| 412 | }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 413 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 414 | static bool move_anon(void) |
| 415 | { |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 416 | return test_bit(MOVE_CHARGE_TYPE_ANON, &mc.immigrate_flags); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 419 | static bool move_file(void) |
| 420 | { |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 421 | return test_bit(MOVE_CHARGE_TYPE_FILE, &mc.immigrate_flags); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 424 | /* |
| 425 | * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft |
| 426 | * limit reclaim to prevent infinite loops, if they ever occur. |
| 427 | */ |
Kirill A. Shutemov | a0db00f | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 428 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS 100 |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 429 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2 |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 430 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 431 | enum charge_type { |
| 432 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
Kamezawa Hiroyuki | 41326c1 | 2012-07-31 16:41:40 -0700 | [diff] [blame] | 433 | MEM_CGROUP_CHARGE_TYPE_ANON, |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 434 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 435 | MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 436 | NR_CHARGE_TYPE, |
| 437 | }; |
| 438 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 439 | /* for encoding cft->private value on file */ |
Glauber Costa | 86ae53e | 2012-12-18 14:21:45 -0800 | [diff] [blame] | 440 | enum res_type { |
| 441 | _MEM, |
| 442 | _MEMSWAP, |
| 443 | _OOM_TYPE, |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 444 | _KMEM, |
Glauber Costa | 86ae53e | 2012-12-18 14:21:45 -0800 | [diff] [blame] | 445 | }; |
| 446 | |
Kirill A. Shutemov | a0db00f | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 447 | #define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val)) |
| 448 | #define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 449 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 450 | /* Used for OOM nofiier */ |
| 451 | #define OOM_CONTROL (0) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 452 | |
Balbir Singh | 75822b4 | 2009-09-23 15:56:38 -0700 | [diff] [blame] | 453 | /* |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 454 | * The memcg_create_mutex will be held whenever a new cgroup is created. |
| 455 | * As a consequence, any change that needs to protect against new child cgroups |
| 456 | * appearing has to hold it as well. |
| 457 | */ |
| 458 | static DEFINE_MUTEX(memcg_create_mutex); |
| 459 | |
Wanpeng Li | b214514 | 2012-07-31 16:46:01 -0700 | [diff] [blame] | 460 | struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s) |
| 461 | { |
Tejun Heo | a7c6d55 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 462 | return s ? container_of(s, struct mem_cgroup, css) : NULL; |
Wanpeng Li | b214514 | 2012-07-31 16:46:01 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 465 | /* Some nice accessors for the vmpressure. */ |
| 466 | struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg) |
| 467 | { |
| 468 | if (!memcg) |
| 469 | memcg = root_mem_cgroup; |
| 470 | return &memcg->vmpressure; |
| 471 | } |
| 472 | |
| 473 | struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr) |
| 474 | { |
| 475 | return &container_of(vmpr, struct mem_cgroup, vmpressure)->css; |
| 476 | } |
| 477 | |
Michal Hocko | 7ffc0ed | 2012-10-08 16:33:13 -0700 | [diff] [blame] | 478 | static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg) |
| 479 | { |
| 480 | return (memcg == root_mem_cgroup); |
| 481 | } |
| 482 | |
Li Zefan | 4219b2d | 2013-09-23 16:56:29 +0800 | [diff] [blame] | 483 | /* |
| 484 | * We restrict the id in the range of [1, 65535], so it can fit into |
| 485 | * an unsigned short. |
| 486 | */ |
| 487 | #define MEM_CGROUP_ID_MAX USHRT_MAX |
| 488 | |
Li Zefan | 34c00c3 | 2013-09-23 16:56:01 +0800 | [diff] [blame] | 489 | static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg) |
| 490 | { |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 491 | return memcg->css.id; |
Li Zefan | 34c00c3 | 2013-09-23 16:56:01 +0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id) |
| 495 | { |
| 496 | struct cgroup_subsys_state *css; |
| 497 | |
Tejun Heo | 7d699dd | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 498 | css = css_from_id(id, &memory_cgrp_subsys); |
Li Zefan | 34c00c3 | 2013-09-23 16:56:01 +0800 | [diff] [blame] | 499 | return mem_cgroup_from_css(css); |
| 500 | } |
| 501 | |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 502 | /* Writing them here to avoid exposing memcg's inner layout */ |
Michal Hocko | 4bd2c1e | 2012-10-08 16:33:10 -0700 | [diff] [blame] | 503 | #if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM) |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 504 | |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 505 | void sock_update_memcg(struct sock *sk) |
| 506 | { |
Glauber Costa | 376be5f | 2012-01-20 04:57:14 +0000 | [diff] [blame] | 507 | if (mem_cgroup_sockets_enabled) { |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 508 | struct mem_cgroup *memcg; |
Glauber Costa | 3f13461 | 2012-05-29 15:07:11 -0700 | [diff] [blame] | 509 | struct cg_proto *cg_proto; |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 510 | |
| 511 | BUG_ON(!sk->sk_prot->proto_cgroup); |
| 512 | |
Glauber Costa | f3f511e | 2012-01-05 20:16:39 +0000 | [diff] [blame] | 513 | /* Socket cloning can throw us here with sk_cgrp already |
| 514 | * filled. It won't however, necessarily happen from |
| 515 | * process context. So the test for root memcg given |
| 516 | * the current task's memcg won't help us in this case. |
| 517 | * |
| 518 | * Respecting the original socket's memcg is a better |
| 519 | * decision in this case. |
| 520 | */ |
| 521 | if (sk->sk_cgrp) { |
| 522 | BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg)); |
Li Zefan | 5347e5a | 2013-07-08 16:00:30 -0700 | [diff] [blame] | 523 | css_get(&sk->sk_cgrp->memcg->css); |
Glauber Costa | f3f511e | 2012-01-05 20:16:39 +0000 | [diff] [blame] | 524 | return; |
| 525 | } |
| 526 | |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 527 | rcu_read_lock(); |
| 528 | memcg = mem_cgroup_from_task(current); |
Glauber Costa | 3f13461 | 2012-05-29 15:07:11 -0700 | [diff] [blame] | 529 | cg_proto = sk->sk_prot->proto_cgroup(memcg); |
Li Zefan | 5347e5a | 2013-07-08 16:00:30 -0700 | [diff] [blame] | 530 | if (!mem_cgroup_is_root(memcg) && |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 531 | memcg_proto_active(cg_proto) && |
| 532 | css_tryget_online(&memcg->css)) { |
Glauber Costa | 3f13461 | 2012-05-29 15:07:11 -0700 | [diff] [blame] | 533 | sk->sk_cgrp = cg_proto; |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 534 | } |
| 535 | rcu_read_unlock(); |
| 536 | } |
| 537 | } |
| 538 | EXPORT_SYMBOL(sock_update_memcg); |
| 539 | |
| 540 | void sock_release_memcg(struct sock *sk) |
| 541 | { |
Glauber Costa | 376be5f | 2012-01-20 04:57:14 +0000 | [diff] [blame] | 542 | if (mem_cgroup_sockets_enabled && sk->sk_cgrp) { |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 543 | struct mem_cgroup *memcg; |
| 544 | WARN_ON(!sk->sk_cgrp->memcg); |
| 545 | memcg = sk->sk_cgrp->memcg; |
Li Zefan | 5347e5a | 2013-07-08 16:00:30 -0700 | [diff] [blame] | 546 | css_put(&sk->sk_cgrp->memcg->css); |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 547 | } |
| 548 | } |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 549 | |
| 550 | struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg) |
| 551 | { |
| 552 | if (!memcg || mem_cgroup_is_root(memcg)) |
| 553 | return NULL; |
| 554 | |
Eric W. Biederman | 2e685ca | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 555 | return &memcg->tcp_mem; |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 556 | } |
| 557 | EXPORT_SYMBOL(tcp_proto_cgroup); |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 558 | |
Glauber Costa | 3f13461 | 2012-05-29 15:07:11 -0700 | [diff] [blame] | 559 | static void disarm_sock_keys(struct mem_cgroup *memcg) |
| 560 | { |
Eric W. Biederman | 2e685ca | 2013-10-19 16:26:19 -0700 | [diff] [blame] | 561 | if (!memcg_proto_activated(&memcg->tcp_mem)) |
Glauber Costa | 3f13461 | 2012-05-29 15:07:11 -0700 | [diff] [blame] | 562 | return; |
| 563 | static_key_slow_dec(&memcg_socket_limit_enabled); |
| 564 | } |
| 565 | #else |
| 566 | static void disarm_sock_keys(struct mem_cgroup *memcg) |
| 567 | { |
| 568 | } |
| 569 | #endif |
| 570 | |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 571 | #ifdef CONFIG_MEMCG_KMEM |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 572 | /* |
| 573 | * This will be the memcg's index in each cache's ->memcg_params->memcg_caches. |
Li Zefan | b862783 | 2013-09-23 16:56:47 +0800 | [diff] [blame] | 574 | * The main reason for not using cgroup id for this: |
| 575 | * this works better in sparse environments, where we have a lot of memcgs, |
| 576 | * but only a few kmem-limited. Or also, if we have, for instance, 200 |
| 577 | * memcgs, and none but the 200th is kmem-limited, we'd have to have a |
| 578 | * 200 entry array for that. |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 579 | * |
| 580 | * The current size of the caches array is stored in |
| 581 | * memcg_limited_groups_array_size. It will double each time we have to |
| 582 | * increase it. |
| 583 | */ |
| 584 | static DEFINE_IDA(kmem_limited_groups); |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 585 | int memcg_limited_groups_array_size; |
| 586 | |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 587 | /* |
| 588 | * MIN_SIZE is different than 1, because we would like to avoid going through |
| 589 | * the alloc/free process all the time. In a small machine, 4 kmem-limited |
| 590 | * cgroups is a reasonable guess. In the future, it could be a parameter or |
| 591 | * tunable, but that is strictly not necessary. |
| 592 | * |
Li Zefan | b862783 | 2013-09-23 16:56:47 +0800 | [diff] [blame] | 593 | * MAX_SIZE should be as large as the number of cgrp_ids. Ideally, we could get |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 594 | * this constant directly from cgroup, but it is understandable that this is |
| 595 | * better kept as an internal representation in cgroup.c. In any case, the |
Li Zefan | b862783 | 2013-09-23 16:56:47 +0800 | [diff] [blame] | 596 | * cgrp_id space is not getting any smaller, and we don't have to necessarily |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 597 | * increase ours as well if it increases. |
| 598 | */ |
| 599 | #define MEMCG_CACHES_MIN_SIZE 4 |
Li Zefan | b862783 | 2013-09-23 16:56:47 +0800 | [diff] [blame] | 600 | #define MEMCG_CACHES_MAX_SIZE MEM_CGROUP_ID_MAX |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 601 | |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 602 | /* |
| 603 | * A lot of the calls to the cache allocation functions are expected to be |
| 604 | * inlined by the compiler. Since the calls to memcg_kmem_get_cache are |
| 605 | * conditional to this static branch, we'll have to allow modules that does |
| 606 | * kmem_cache_alloc and the such to see this symbol as well |
| 607 | */ |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 608 | struct static_key memcg_kmem_enabled_key; |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 609 | EXPORT_SYMBOL(memcg_kmem_enabled_key); |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 610 | |
Vladimir Davydov | f3bb304 | 2014-10-09 15:28:45 -0700 | [diff] [blame] | 611 | static void memcg_free_cache_id(int id); |
| 612 | |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 613 | static void disarm_kmem_keys(struct mem_cgroup *memcg) |
| 614 | { |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 615 | if (memcg_kmem_is_active(memcg)) { |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 616 | static_key_slow_dec(&memcg_kmem_enabled_key); |
Vladimir Davydov | f3bb304 | 2014-10-09 15:28:45 -0700 | [diff] [blame] | 617 | memcg_free_cache_id(memcg->kmemcg_id); |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 618 | } |
Glauber Costa | bea207c | 2012-12-18 14:22:11 -0800 | [diff] [blame] | 619 | /* |
| 620 | * This check can't live in kmem destruction function, |
| 621 | * since the charges will outlive the cgroup |
| 622 | */ |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 623 | WARN_ON(page_counter_read(&memcg->kmem)); |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 624 | } |
| 625 | #else |
| 626 | static void disarm_kmem_keys(struct mem_cgroup *memcg) |
| 627 | { |
| 628 | } |
| 629 | #endif /* CONFIG_MEMCG_KMEM */ |
| 630 | |
| 631 | static void disarm_static_keys(struct mem_cgroup *memcg) |
| 632 | { |
| 633 | disarm_sock_keys(memcg); |
| 634 | disarm_kmem_keys(memcg); |
| 635 | } |
| 636 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 637 | static void drain_all_stock_async(struct mem_cgroup *memcg); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 638 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 639 | static struct mem_cgroup_per_zone * |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 640 | mem_cgroup_zone_zoneinfo(struct mem_cgroup *memcg, struct zone *zone) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 641 | { |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 642 | int nid = zone_to_nid(zone); |
| 643 | int zid = zone_idx(zone); |
| 644 | |
Johannes Weiner | 54f72fe | 2013-07-08 15:59:49 -0700 | [diff] [blame] | 645 | return &memcg->nodeinfo[nid]->zoneinfo[zid]; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 648 | struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg) |
Wu Fengguang | d324236 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 649 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 650 | return &memcg->css; |
Wu Fengguang | d324236 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 651 | } |
| 652 | |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 653 | static struct mem_cgroup_per_zone * |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 654 | mem_cgroup_page_zoneinfo(struct mem_cgroup *memcg, struct page *page) |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 655 | { |
Johannes Weiner | 97a6c37 | 2011-03-23 16:42:27 -0700 | [diff] [blame] | 656 | int nid = page_to_nid(page); |
| 657 | int zid = page_zonenum(page); |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 658 | |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 659 | return &memcg->nodeinfo[nid]->zoneinfo[zid]; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 662 | static struct mem_cgroup_tree_per_zone * |
| 663 | soft_limit_tree_node_zone(int nid, int zid) |
| 664 | { |
| 665 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 666 | } |
| 667 | |
| 668 | static struct mem_cgroup_tree_per_zone * |
| 669 | soft_limit_tree_from_page(struct page *page) |
| 670 | { |
| 671 | int nid = page_to_nid(page); |
| 672 | int zid = page_zonenum(page); |
| 673 | |
| 674 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; |
| 675 | } |
| 676 | |
Johannes Weiner | cf2c812 | 2014-06-06 14:38:21 -0700 | [diff] [blame] | 677 | static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_zone *mz, |
| 678 | struct mem_cgroup_tree_per_zone *mctz, |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 679 | unsigned long new_usage_in_excess) |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 680 | { |
| 681 | struct rb_node **p = &mctz->rb_root.rb_node; |
| 682 | struct rb_node *parent = NULL; |
| 683 | struct mem_cgroup_per_zone *mz_node; |
| 684 | |
| 685 | if (mz->on_tree) |
| 686 | return; |
| 687 | |
| 688 | mz->usage_in_excess = new_usage_in_excess; |
| 689 | if (!mz->usage_in_excess) |
| 690 | return; |
| 691 | while (*p) { |
| 692 | parent = *p; |
| 693 | mz_node = rb_entry(parent, struct mem_cgroup_per_zone, |
| 694 | tree_node); |
| 695 | if (mz->usage_in_excess < mz_node->usage_in_excess) |
| 696 | p = &(*p)->rb_left; |
| 697 | /* |
| 698 | * We can't avoid mem cgroups that are over their soft |
| 699 | * limit by the same amount |
| 700 | */ |
| 701 | else if (mz->usage_in_excess >= mz_node->usage_in_excess) |
| 702 | p = &(*p)->rb_right; |
| 703 | } |
| 704 | rb_link_node(&mz->tree_node, parent, p); |
| 705 | rb_insert_color(&mz->tree_node, &mctz->rb_root); |
| 706 | mz->on_tree = true; |
| 707 | } |
| 708 | |
Johannes Weiner | cf2c812 | 2014-06-06 14:38:21 -0700 | [diff] [blame] | 709 | static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_zone *mz, |
| 710 | struct mem_cgroup_tree_per_zone *mctz) |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 711 | { |
| 712 | if (!mz->on_tree) |
| 713 | return; |
| 714 | rb_erase(&mz->tree_node, &mctz->rb_root); |
| 715 | mz->on_tree = false; |
| 716 | } |
| 717 | |
Johannes Weiner | cf2c812 | 2014-06-06 14:38:21 -0700 | [diff] [blame] | 718 | static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_zone *mz, |
| 719 | struct mem_cgroup_tree_per_zone *mctz) |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 720 | { |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 721 | unsigned long flags; |
| 722 | |
| 723 | spin_lock_irqsave(&mctz->lock, flags); |
Johannes Weiner | cf2c812 | 2014-06-06 14:38:21 -0700 | [diff] [blame] | 724 | __mem_cgroup_remove_exceeded(mz, mctz); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 725 | spin_unlock_irqrestore(&mctz->lock, flags); |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 728 | static unsigned long soft_limit_excess(struct mem_cgroup *memcg) |
| 729 | { |
| 730 | unsigned long nr_pages = page_counter_read(&memcg->memory); |
| 731 | unsigned long soft_limit = ACCESS_ONCE(memcg->soft_limit); |
| 732 | unsigned long excess = 0; |
| 733 | |
| 734 | if (nr_pages > soft_limit) |
| 735 | excess = nr_pages - soft_limit; |
| 736 | |
| 737 | return excess; |
| 738 | } |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 739 | |
| 740 | static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page) |
| 741 | { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 742 | unsigned long excess; |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 743 | struct mem_cgroup_per_zone *mz; |
| 744 | struct mem_cgroup_tree_per_zone *mctz; |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 745 | |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 746 | mctz = soft_limit_tree_from_page(page); |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 747 | /* |
| 748 | * Necessary to update all ancestors when hierarchy is used. |
| 749 | * because their event counter is not touched. |
| 750 | */ |
| 751 | for (; memcg; memcg = parent_mem_cgroup(memcg)) { |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 752 | mz = mem_cgroup_page_zoneinfo(memcg, page); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 753 | excess = soft_limit_excess(memcg); |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 754 | /* |
| 755 | * We have to update the tree if mz is on RB-tree or |
| 756 | * mem is over its softlimit. |
| 757 | */ |
| 758 | if (excess || mz->on_tree) { |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 759 | unsigned long flags; |
| 760 | |
| 761 | spin_lock_irqsave(&mctz->lock, flags); |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 762 | /* if on-tree, remove it */ |
| 763 | if (mz->on_tree) |
Johannes Weiner | cf2c812 | 2014-06-06 14:38:21 -0700 | [diff] [blame] | 764 | __mem_cgroup_remove_exceeded(mz, mctz); |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 765 | /* |
| 766 | * Insert again. mz->usage_in_excess will be updated. |
| 767 | * If excess is 0, no tree ops. |
| 768 | */ |
Johannes Weiner | cf2c812 | 2014-06-06 14:38:21 -0700 | [diff] [blame] | 769 | __mem_cgroup_insert_exceeded(mz, mctz, excess); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 770 | spin_unlock_irqrestore(&mctz->lock, flags); |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg) |
| 776 | { |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 777 | struct mem_cgroup_tree_per_zone *mctz; |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 778 | struct mem_cgroup_per_zone *mz; |
| 779 | int nid, zid; |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 780 | |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 781 | for_each_node(nid) { |
| 782 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 783 | mz = &memcg->nodeinfo[nid]->zoneinfo[zid]; |
| 784 | mctz = soft_limit_tree_node_zone(nid, zid); |
Johannes Weiner | cf2c812 | 2014-06-06 14:38:21 -0700 | [diff] [blame] | 785 | mem_cgroup_remove_exceeded(mz, mctz); |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | static struct mem_cgroup_per_zone * |
| 791 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 792 | { |
| 793 | struct rb_node *rightmost = NULL; |
| 794 | struct mem_cgroup_per_zone *mz; |
| 795 | |
| 796 | retry: |
| 797 | mz = NULL; |
| 798 | rightmost = rb_last(&mctz->rb_root); |
| 799 | if (!rightmost) |
| 800 | goto done; /* Nothing to reclaim from */ |
| 801 | |
| 802 | mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node); |
| 803 | /* |
| 804 | * Remove the node now but someone else can add it back, |
| 805 | * we will to add it back at the end of reclaim to its correct |
| 806 | * position in the tree. |
| 807 | */ |
Johannes Weiner | cf2c812 | 2014-06-06 14:38:21 -0700 | [diff] [blame] | 808 | __mem_cgroup_remove_exceeded(mz, mctz); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 809 | if (!soft_limit_excess(mz->memcg) || |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 810 | !css_tryget_online(&mz->memcg->css)) |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 811 | goto retry; |
| 812 | done: |
| 813 | return mz; |
| 814 | } |
| 815 | |
| 816 | static struct mem_cgroup_per_zone * |
| 817 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) |
| 818 | { |
| 819 | struct mem_cgroup_per_zone *mz; |
| 820 | |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 821 | spin_lock_irq(&mctz->lock); |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 822 | mz = __mem_cgroup_largest_soft_limit_node(mctz); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 823 | spin_unlock_irq(&mctz->lock); |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 824 | return mz; |
| 825 | } |
| 826 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 827 | /* |
| 828 | * Implementation Note: reading percpu statistics for memcg. |
| 829 | * |
| 830 | * Both of vmstat[] and percpu_counter has threshold and do periodic |
| 831 | * synchronization to implement "quick" read. There are trade-off between |
| 832 | * reading cost and precision of value. Then, we may have a chance to implement |
| 833 | * a periodic synchronizion of counter in memcg's counter. |
| 834 | * |
| 835 | * But this _read() function is used for user interface now. The user accounts |
| 836 | * memory usage by memory cgroup and he _always_ requires exact value because |
| 837 | * he accounts memory. Even if we provide quick-and-fuzzy read, we always |
| 838 | * have to visit all online cpus and make sum. So, for now, unnecessary |
| 839 | * synchronization is not implemented. (just implemented for cpu hotplug) |
| 840 | * |
| 841 | * If there are kernel internal actions which can make use of some not-exact |
| 842 | * value, and reading all cpu value can be performance bottleneck in some |
| 843 | * common workload, threashold and synchonization as vmstat[] should be |
| 844 | * implemented. |
| 845 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 846 | static long mem_cgroup_read_stat(struct mem_cgroup *memcg, |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 847 | enum mem_cgroup_stat_index idx) |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 848 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 849 | long val = 0; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 850 | int cpu; |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 851 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 852 | get_online_cpus(); |
| 853 | for_each_online_cpu(cpu) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 854 | val += per_cpu(memcg->stat->count[idx], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 855 | #ifdef CONFIG_HOTPLUG_CPU |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 856 | spin_lock(&memcg->pcp_counter_lock); |
| 857 | val += memcg->nocpu_base.count[idx]; |
| 858 | spin_unlock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 859 | #endif |
| 860 | put_online_cpus(); |
KAMEZAWA Hiroyuki | c62b1a3 | 2010-03-10 15:22:29 -0800 | [diff] [blame] | 861 | return val; |
| 862 | } |
| 863 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 864 | static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg, |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 865 | enum mem_cgroup_events_index idx) |
| 866 | { |
| 867 | unsigned long val = 0; |
| 868 | int cpu; |
| 869 | |
David Rientjes | 9c56751 | 2013-10-16 13:46:43 -0700 | [diff] [blame] | 870 | get_online_cpus(); |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 871 | for_each_online_cpu(cpu) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 872 | val += per_cpu(memcg->stat->events[idx], cpu); |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 873 | #ifdef CONFIG_HOTPLUG_CPU |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 874 | spin_lock(&memcg->pcp_counter_lock); |
| 875 | val += memcg->nocpu_base.events[idx]; |
| 876 | spin_unlock(&memcg->pcp_counter_lock); |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 877 | #endif |
David Rientjes | 9c56751 | 2013-10-16 13:46:43 -0700 | [diff] [blame] | 878 | put_online_cpus(); |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 879 | return val; |
| 880 | } |
| 881 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 882 | static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg, |
David Rientjes | b070e65 | 2013-05-07 16:18:09 -0700 | [diff] [blame] | 883 | struct page *page, |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 884 | int nr_pages) |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 885 | { |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 886 | /* |
| 887 | * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is |
| 888 | * counted as CACHE even if it's on ANON LRU. |
| 889 | */ |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 890 | if (PageAnon(page)) |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 891 | __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS], |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 892 | nr_pages); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 893 | else |
KAMEZAWA Hiroyuki | b2402857 | 2012-03-21 16:34:22 -0700 | [diff] [blame] | 894 | __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE], |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 895 | nr_pages); |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 896 | |
David Rientjes | b070e65 | 2013-05-07 16:18:09 -0700 | [diff] [blame] | 897 | if (PageTransHuge(page)) |
| 898 | __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], |
| 899 | nr_pages); |
| 900 | |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 901 | /* pagein of a big page is an event. So, ignore page size */ |
| 902 | if (nr_pages > 0) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 903 | __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 904 | else { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 905 | __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 906 | nr_pages = -nr_pages; /* for event */ |
| 907 | } |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 908 | |
Johannes Weiner | 1311471 | 2012-05-29 15:07:07 -0700 | [diff] [blame] | 909 | __this_cpu_add(memcg->stat->nr_page_events, nr_pages); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 910 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 911 | |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 912 | unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru) |
Konstantin Khlebnikov | 074291f | 2012-05-29 15:07:00 -0700 | [diff] [blame] | 913 | { |
| 914 | struct mem_cgroup_per_zone *mz; |
| 915 | |
| 916 | mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec); |
| 917 | return mz->lru_size[lru]; |
| 918 | } |
| 919 | |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 920 | static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg, |
| 921 | int nid, |
| 922 | unsigned int lru_mask) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 923 | { |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 924 | unsigned long nr = 0; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 925 | int zid; |
| 926 | |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 927 | VM_BUG_ON((unsigned)nid >= nr_node_ids); |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 928 | |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 929 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 930 | struct mem_cgroup_per_zone *mz; |
| 931 | enum lru_list lru; |
| 932 | |
| 933 | for_each_lru(lru) { |
| 934 | if (!(BIT(lru) & lru_mask)) |
| 935 | continue; |
| 936 | mz = &memcg->nodeinfo[nid]->zoneinfo[zid]; |
| 937 | nr += mz->lru_size[lru]; |
| 938 | } |
| 939 | } |
| 940 | return nr; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 941 | } |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 942 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 943 | static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | bb2a0de | 2011-07-26 16:08:22 -0700 | [diff] [blame] | 944 | unsigned int lru_mask) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 945 | { |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 946 | unsigned long nr = 0; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 947 | int nid; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 948 | |
Lai Jiangshan | 31aaea4 | 2012-12-12 13:51:27 -0800 | [diff] [blame] | 949 | for_each_node_state(nid, N_MEMORY) |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 950 | nr += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask); |
| 951 | return nr; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 952 | } |
| 953 | |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 954 | static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg, |
| 955 | enum mem_cgroup_events_target target) |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 956 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 957 | unsigned long val, next; |
| 958 | |
Johannes Weiner | 1311471 | 2012-05-29 15:07:07 -0700 | [diff] [blame] | 959 | val = __this_cpu_read(memcg->stat->nr_page_events); |
Steven Rostedt | 4799401 | 2011-11-02 13:38:33 -0700 | [diff] [blame] | 960 | next = __this_cpu_read(memcg->stat->targets[target]); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 961 | /* from time_after() in jiffies.h */ |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 962 | if ((long)next - (long)val < 0) { |
| 963 | switch (target) { |
| 964 | case MEM_CGROUP_TARGET_THRESH: |
| 965 | next = val + THRESHOLDS_EVENTS_TARGET; |
| 966 | break; |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 967 | case MEM_CGROUP_TARGET_SOFTLIMIT: |
| 968 | next = val + SOFTLIMIT_EVENTS_TARGET; |
| 969 | break; |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 970 | case MEM_CGROUP_TARGET_NUMAINFO: |
| 971 | next = val + NUMAINFO_EVENTS_TARGET; |
| 972 | break; |
| 973 | default: |
| 974 | break; |
| 975 | } |
| 976 | __this_cpu_write(memcg->stat->targets[target], next); |
| 977 | return true; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 978 | } |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 979 | return false; |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | /* |
| 983 | * Check events in order. |
| 984 | * |
| 985 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 986 | static void memcg_check_events(struct mem_cgroup *memcg, struct page *page) |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 987 | { |
| 988 | /* threshold event is triggered in finer grain than soft limit */ |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 989 | if (unlikely(mem_cgroup_event_ratelimit(memcg, |
| 990 | MEM_CGROUP_TARGET_THRESH))) { |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 991 | bool do_softlimit; |
Andrew Morton | 82b3f2a | 2012-02-03 15:37:14 -0800 | [diff] [blame] | 992 | bool do_numainfo __maybe_unused; |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 993 | |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 994 | do_softlimit = mem_cgroup_event_ratelimit(memcg, |
| 995 | MEM_CGROUP_TARGET_SOFTLIMIT); |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 996 | #if MAX_NUMNODES > 1 |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 997 | do_numainfo = mem_cgroup_event_ratelimit(memcg, |
| 998 | MEM_CGROUP_TARGET_NUMAINFO); |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 999 | #endif |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 1000 | mem_cgroup_threshold(memcg); |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 1001 | if (unlikely(do_softlimit)) |
| 1002 | mem_cgroup_update_tree(memcg, page); |
Johannes Weiner | f53d7ce | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 1003 | #if MAX_NUMNODES > 1 |
| 1004 | if (unlikely(do_numainfo)) |
| 1005 | atomic_inc(&memcg->numainfo_events); |
| 1006 | #endif |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 1007 | } |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 1008 | } |
| 1009 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 1010 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1011 | { |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 1012 | /* |
| 1013 | * mm_update_next_owner() may clear mm->owner to NULL |
| 1014 | * if it races with swapoff, page migration, etc. |
| 1015 | * So this can be called with p == NULL. |
| 1016 | */ |
| 1017 | if (unlikely(!p)) |
| 1018 | return NULL; |
| 1019 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1020 | return mem_cgroup_from_css(task_css(p, memory_cgrp_id)); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1021 | } |
| 1022 | |
Johannes Weiner | df38197 | 2014-04-07 15:37:43 -0700 | [diff] [blame] | 1023 | static struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm) |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1024 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1025 | struct mem_cgroup *memcg = NULL; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1026 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1027 | rcu_read_lock(); |
| 1028 | do { |
Michal Hocko | 6f6acb0 | 2014-05-22 11:54:19 -0700 | [diff] [blame] | 1029 | /* |
| 1030 | * Page cache insertions can happen withou an |
| 1031 | * actual mm context, e.g. during disk probing |
| 1032 | * on boot, loopback IO, acct() writes etc. |
| 1033 | */ |
| 1034 | if (unlikely(!mm)) |
Johannes Weiner | df38197 | 2014-04-07 15:37:43 -0700 | [diff] [blame] | 1035 | memcg = root_mem_cgroup; |
Michal Hocko | 6f6acb0 | 2014-05-22 11:54:19 -0700 | [diff] [blame] | 1036 | else { |
| 1037 | memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 1038 | if (unlikely(!memcg)) |
| 1039 | memcg = root_mem_cgroup; |
| 1040 | } |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 1041 | } while (!css_tryget_online(&memcg->css)); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1042 | rcu_read_unlock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1043 | return memcg; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1046 | /** |
| 1047 | * mem_cgroup_iter - iterate over memory cgroup hierarchy |
| 1048 | * @root: hierarchy root |
| 1049 | * @prev: previously returned memcg, NULL on first invocation |
| 1050 | * @reclaim: cookie for shared reclaim walks, NULL for full walks |
| 1051 | * |
| 1052 | * Returns references to children of the hierarchy below @root, or |
| 1053 | * @root itself, or %NULL after a full round-trip. |
| 1054 | * |
| 1055 | * Caller must pass the return value in @prev on subsequent |
| 1056 | * invocations for reference counting, or use mem_cgroup_iter_break() |
| 1057 | * to cancel a hierarchy walk before the round-trip is complete. |
| 1058 | * |
| 1059 | * Reclaimers can specify a zone and a priority level in @reclaim to |
| 1060 | * divide up the memcgs in the hierarchy among all concurrent |
| 1061 | * reclaimers operating on the same zone and priority. |
| 1062 | */ |
Andrew Morton | 694fbc0 | 2013-09-24 15:27:37 -0700 | [diff] [blame] | 1063 | struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1064 | struct mem_cgroup *prev, |
Andrew Morton | 694fbc0 | 2013-09-24 15:27:37 -0700 | [diff] [blame] | 1065 | struct mem_cgroup_reclaim_cookie *reclaim) |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 1066 | { |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1067 | struct reclaim_iter *uninitialized_var(iter); |
| 1068 | struct cgroup_subsys_state *css = NULL; |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1069 | struct mem_cgroup *memcg = NULL; |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1070 | struct mem_cgroup *pos = NULL; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1071 | |
Andrew Morton | 694fbc0 | 2013-09-24 15:27:37 -0700 | [diff] [blame] | 1072 | if (mem_cgroup_disabled()) |
| 1073 | return NULL; |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1074 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1075 | if (!root) |
| 1076 | root = root_mem_cgroup; |
| 1077 | |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1078 | if (prev && !reclaim) |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1079 | pos = prev; |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1080 | |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1081 | if (!root->use_hierarchy && root != root_mem_cgroup) { |
| 1082 | if (prev) |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1083 | goto out; |
Andrew Morton | 694fbc0 | 2013-09-24 15:27:37 -0700 | [diff] [blame] | 1084 | return root; |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
Michal Hocko | 542f85f | 2013-04-29 15:07:15 -0700 | [diff] [blame] | 1087 | rcu_read_lock(); |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1088 | |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1089 | if (reclaim) { |
| 1090 | struct mem_cgroup_per_zone *mz; |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 1091 | |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1092 | mz = mem_cgroup_zone_zoneinfo(root, reclaim->zone); |
| 1093 | iter = &mz->iter[reclaim->priority]; |
Michal Hocko | 5f57816 | 2013-04-29 15:07:17 -0700 | [diff] [blame] | 1094 | |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1095 | if (prev && reclaim->generation != iter->generation) |
Michal Hocko | 542f85f | 2013-04-29 15:07:15 -0700 | [diff] [blame] | 1096 | goto out_unlock; |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1097 | |
| 1098 | do { |
| 1099 | pos = ACCESS_ONCE(iter->position); |
| 1100 | /* |
| 1101 | * A racing update may change the position and |
| 1102 | * put the last reference, hence css_tryget(), |
| 1103 | * or retry to see the updated position. |
| 1104 | */ |
| 1105 | } while (pos && !css_tryget(&pos->css)); |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1106 | } |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1107 | |
| 1108 | if (pos) |
| 1109 | css = &pos->css; |
| 1110 | |
| 1111 | for (;;) { |
| 1112 | css = css_next_descendant_pre(css, &root->css); |
| 1113 | if (!css) { |
| 1114 | /* |
| 1115 | * Reclaimers share the hierarchy walk, and a |
| 1116 | * new one might jump in right at the end of |
| 1117 | * the hierarchy - make sure they see at least |
| 1118 | * one group and restart from the beginning. |
| 1119 | */ |
| 1120 | if (!prev) |
| 1121 | continue; |
| 1122 | break; |
| 1123 | } |
| 1124 | |
| 1125 | /* |
| 1126 | * Verify the css and acquire a reference. The root |
| 1127 | * is provided by the caller, so we know it's alive |
| 1128 | * and kicking, and don't take an extra reference. |
| 1129 | */ |
| 1130 | memcg = mem_cgroup_from_css(css); |
| 1131 | |
| 1132 | if (css == &root->css) |
| 1133 | break; |
| 1134 | |
| 1135 | if (css_tryget_online(css)) { |
| 1136 | /* |
| 1137 | * Make sure the memcg is initialized: |
| 1138 | * mem_cgroup_css_online() orders the the |
| 1139 | * initialization against setting the flag. |
| 1140 | */ |
| 1141 | if (smp_load_acquire(&memcg->initialized)) |
| 1142 | break; |
| 1143 | |
| 1144 | css_put(css); |
| 1145 | } |
| 1146 | |
| 1147 | memcg = NULL; |
| 1148 | } |
| 1149 | |
| 1150 | if (reclaim) { |
| 1151 | if (cmpxchg(&iter->position, pos, memcg) == pos) { |
| 1152 | if (memcg) |
| 1153 | css_get(&memcg->css); |
| 1154 | if (pos) |
| 1155 | css_put(&pos->css); |
| 1156 | } |
| 1157 | |
| 1158 | /* |
| 1159 | * pairs with css_tryget when dereferencing iter->position |
| 1160 | * above. |
| 1161 | */ |
| 1162 | if (pos) |
| 1163 | css_put(&pos->css); |
| 1164 | |
| 1165 | if (!memcg) |
| 1166 | iter->generation++; |
| 1167 | else if (!prev) |
| 1168 | reclaim->generation = iter->generation; |
| 1169 | } |
| 1170 | |
Michal Hocko | 542f85f | 2013-04-29 15:07:15 -0700 | [diff] [blame] | 1171 | out_unlock: |
| 1172 | rcu_read_unlock(); |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1173 | out: |
Michal Hocko | c40046f | 2013-04-29 15:07:14 -0700 | [diff] [blame] | 1174 | if (prev && prev != root) |
| 1175 | css_put(&prev->css); |
| 1176 | |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1177 | return memcg; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1178 | } |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1179 | |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1180 | /** |
| 1181 | * mem_cgroup_iter_break - abort a hierarchy walk prematurely |
| 1182 | * @root: hierarchy root |
| 1183 | * @prev: last visited hierarchy member as returned by mem_cgroup_iter() |
| 1184 | */ |
| 1185 | void mem_cgroup_iter_break(struct mem_cgroup *root, |
| 1186 | struct mem_cgroup *prev) |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1187 | { |
| 1188 | if (!root) |
| 1189 | root = root_mem_cgroup; |
| 1190 | if (prev && prev != root) |
| 1191 | css_put(&prev->css); |
| 1192 | } |
| 1193 | |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1194 | /* |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1195 | * Iteration constructs for visiting all cgroups (under a tree). If |
| 1196 | * loops are exited prematurely (break), mem_cgroup_iter_break() must |
| 1197 | * be used for reference counting. |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1198 | */ |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1199 | #define for_each_mem_cgroup_tree(iter, root) \ |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 1200 | for (iter = mem_cgroup_iter(root, NULL, NULL); \ |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1201 | iter != NULL; \ |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 1202 | iter = mem_cgroup_iter(root, iter, NULL)) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1203 | |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1204 | #define for_each_mem_cgroup(iter) \ |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 1205 | for (iter = mem_cgroup_iter(NULL, NULL, NULL); \ |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1206 | iter != NULL; \ |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 1207 | iter = mem_cgroup_iter(NULL, iter, NULL)) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1208 | |
David Rientjes | 68ae564 | 2012-12-12 13:51:57 -0800 | [diff] [blame] | 1209 | void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx) |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 1210 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1211 | struct mem_cgroup *memcg; |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 1212 | |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 1213 | rcu_read_lock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1214 | memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 1215 | if (unlikely(!memcg)) |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 1216 | goto out; |
| 1217 | |
| 1218 | switch (idx) { |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 1219 | case PGFAULT: |
Johannes Weiner | 0e574a9 | 2012-01-12 17:18:35 -0800 | [diff] [blame] | 1220 | this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]); |
| 1221 | break; |
| 1222 | case PGMAJFAULT: |
| 1223 | this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 1224 | break; |
| 1225 | default: |
| 1226 | BUG(); |
| 1227 | } |
| 1228 | out: |
| 1229 | rcu_read_unlock(); |
| 1230 | } |
David Rientjes | 68ae564 | 2012-12-12 13:51:57 -0800 | [diff] [blame] | 1231 | EXPORT_SYMBOL(__mem_cgroup_count_vm_event); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 1232 | |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1233 | /** |
| 1234 | * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg |
| 1235 | * @zone: zone of the wanted lruvec |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1236 | * @memcg: memcg of the wanted lruvec |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1237 | * |
| 1238 | * Returns the lru list vector holding pages for the given @zone and |
| 1239 | * @mem. This can be the global zone lruvec, if the memory controller |
| 1240 | * is disabled. |
| 1241 | */ |
| 1242 | struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone, |
| 1243 | struct mem_cgroup *memcg) |
| 1244 | { |
| 1245 | struct mem_cgroup_per_zone *mz; |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 1246 | struct lruvec *lruvec; |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1247 | |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 1248 | if (mem_cgroup_disabled()) { |
| 1249 | lruvec = &zone->lruvec; |
| 1250 | goto out; |
| 1251 | } |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1252 | |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 1253 | mz = mem_cgroup_zone_zoneinfo(memcg, zone); |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 1254 | lruvec = &mz->lruvec; |
| 1255 | out: |
| 1256 | /* |
| 1257 | * Since a node can be onlined after the mem_cgroup was created, |
| 1258 | * we have to be prepared to initialize lruvec->zone here; |
| 1259 | * and if offlined then reonlined, we need to reinitialize it. |
| 1260 | */ |
| 1261 | if (unlikely(lruvec->zone != zone)) |
| 1262 | lruvec->zone = zone; |
| 1263 | return lruvec; |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1264 | } |
| 1265 | |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1266 | /** |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1267 | * mem_cgroup_page_lruvec - return lruvec for adding an lru page |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1268 | * @page: the page |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1269 | * @zone: zone of the page |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 1270 | */ |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1271 | struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone) |
Minchan Kim | 3f58a82 | 2011-03-22 16:32:53 -0700 | [diff] [blame] | 1272 | { |
| 1273 | struct mem_cgroup_per_zone *mz; |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1274 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1275 | struct page_cgroup *pc; |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 1276 | struct lruvec *lruvec; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1277 | |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 1278 | if (mem_cgroup_disabled()) { |
| 1279 | lruvec = &zone->lruvec; |
| 1280 | goto out; |
| 1281 | } |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1282 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1283 | pc = lookup_page_cgroup(page); |
KAMEZAWA Hiroyuki | 38c5d72 | 2012-01-12 17:19:01 -0800 | [diff] [blame] | 1284 | memcg = pc->mem_cgroup; |
Hugh Dickins | 7512102 | 2012-03-05 14:59:18 -0800 | [diff] [blame] | 1285 | |
| 1286 | /* |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1287 | * Surreptitiously switch any uncharged offlist page to root: |
Hugh Dickins | 7512102 | 2012-03-05 14:59:18 -0800 | [diff] [blame] | 1288 | * an uncharged page off lru does nothing to secure |
| 1289 | * its former mem_cgroup from sudden removal. |
| 1290 | * |
| 1291 | * Our caller holds lru_lock, and PageCgroupUsed is updated |
| 1292 | * under page_cgroup lock: between them, they make all uses |
| 1293 | * of pc->mem_cgroup safe. |
| 1294 | */ |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1295 | if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup) |
Hugh Dickins | 7512102 | 2012-03-05 14:59:18 -0800 | [diff] [blame] | 1296 | pc->mem_cgroup = memcg = root_mem_cgroup; |
| 1297 | |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 1298 | mz = mem_cgroup_page_zoneinfo(memcg, page); |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 1299 | lruvec = &mz->lruvec; |
| 1300 | out: |
| 1301 | /* |
| 1302 | * Since a node can be onlined after the mem_cgroup was created, |
| 1303 | * we have to be prepared to initialize lruvec->zone here; |
| 1304 | * and if offlined then reonlined, we need to reinitialize it. |
| 1305 | */ |
| 1306 | if (unlikely(lruvec->zone != zone)) |
| 1307 | lruvec->zone = zone; |
| 1308 | return lruvec; |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | /** |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1312 | * mem_cgroup_update_lru_size - account for adding or removing an lru page |
| 1313 | * @lruvec: mem_cgroup per zone lru vector |
| 1314 | * @lru: index of lru list the page is sitting on |
| 1315 | * @nr_pages: positive when adding or negative when removing |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1316 | * |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1317 | * This function must be called when a page is added to or removed from an |
| 1318 | * lru list. |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1319 | */ |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1320 | void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru, |
| 1321 | int nr_pages) |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1322 | { |
| 1323 | struct mem_cgroup_per_zone *mz; |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1324 | unsigned long *lru_size; |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1325 | |
| 1326 | if (mem_cgroup_disabled()) |
| 1327 | return; |
| 1328 | |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1329 | mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec); |
| 1330 | lru_size = mz->lru_size + lru; |
| 1331 | *lru_size += nr_pages; |
| 1332 | VM_BUG_ON((long)(*lru_size) < 0); |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1333 | } |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1334 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1335 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1336 | * Checks whether given mem is same or in the root_mem_cgroup's |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1337 | * hierarchy subtree |
| 1338 | */ |
Johannes Weiner | c3ac9a8 | 2012-05-29 15:06:25 -0700 | [diff] [blame] | 1339 | bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg, |
| 1340 | struct mem_cgroup *memcg) |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1341 | { |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 1342 | if (root_memcg == memcg) |
| 1343 | return true; |
Hugh Dickins | 3a981f4 | 2012-06-20 12:52:58 -0700 | [diff] [blame] | 1344 | if (!root_memcg->use_hierarchy || !memcg) |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 1345 | return false; |
Li Zefan | b47f77b | 2013-09-23 16:55:43 +0800 | [diff] [blame] | 1346 | return cgroup_is_descendant(memcg->css.cgroup, root_memcg->css.cgroup); |
Johannes Weiner | c3ac9a8 | 2012-05-29 15:06:25 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg, |
| 1350 | struct mem_cgroup *memcg) |
| 1351 | { |
| 1352 | bool ret; |
| 1353 | |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 1354 | rcu_read_lock(); |
Johannes Weiner | c3ac9a8 | 2012-05-29 15:06:25 -0700 | [diff] [blame] | 1355 | ret = __mem_cgroup_same_or_subtree(root_memcg, memcg); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 1356 | rcu_read_unlock(); |
| 1357 | return ret; |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1358 | } |
| 1359 | |
David Rientjes | ffbdccf | 2013-07-03 15:01:23 -0700 | [diff] [blame] | 1360 | bool task_in_mem_cgroup(struct task_struct *task, |
| 1361 | const struct mem_cgroup *memcg) |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1362 | { |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1363 | struct mem_cgroup *curr = NULL; |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1364 | struct task_struct *p; |
David Rientjes | ffbdccf | 2013-07-03 15:01:23 -0700 | [diff] [blame] | 1365 | bool ret; |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1366 | |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 1367 | p = find_lock_task_mm(task); |
David Rientjes | de077d2 | 2012-01-12 17:18:52 -0800 | [diff] [blame] | 1368 | if (p) { |
Johannes Weiner | df38197 | 2014-04-07 15:37:43 -0700 | [diff] [blame] | 1369 | curr = get_mem_cgroup_from_mm(p->mm); |
David Rientjes | de077d2 | 2012-01-12 17:18:52 -0800 | [diff] [blame] | 1370 | task_unlock(p); |
| 1371 | } else { |
| 1372 | /* |
| 1373 | * All threads may have already detached their mm's, but the oom |
| 1374 | * killer still needs to detect if they have already been oom |
| 1375 | * killed to prevent needlessly killing additional tasks. |
| 1376 | */ |
David Rientjes | ffbdccf | 2013-07-03 15:01:23 -0700 | [diff] [blame] | 1377 | rcu_read_lock(); |
David Rientjes | de077d2 | 2012-01-12 17:18:52 -0800 | [diff] [blame] | 1378 | curr = mem_cgroup_from_task(task); |
| 1379 | if (curr) |
| 1380 | css_get(&curr->css); |
David Rientjes | ffbdccf | 2013-07-03 15:01:23 -0700 | [diff] [blame] | 1381 | rcu_read_unlock(); |
David Rientjes | de077d2 | 2012-01-12 17:18:52 -0800 | [diff] [blame] | 1382 | } |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1383 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1384 | * We should check use_hierarchy of "memcg" not "curr". Because checking |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1385 | * use_hierarchy of "curr" here make this function true if hierarchy is |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1386 | * enabled in "curr" and "curr" is a child of "memcg" in *cgroup* |
| 1387 | * hierarchy(even if use_hierarchy is disabled in "memcg"). |
Daisuke Nishimura | d31f56d | 2009-12-15 16:47:12 -0800 | [diff] [blame] | 1388 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1389 | ret = mem_cgroup_same_or_subtree(memcg, curr); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1390 | css_put(&curr->css); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 1391 | return ret; |
| 1392 | } |
| 1393 | |
Konstantin Khlebnikov | c56d5c7 | 2012-05-29 15:07:00 -0700 | [diff] [blame] | 1394 | int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec) |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1395 | { |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1396 | unsigned long inactive_ratio; |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1397 | unsigned long inactive; |
| 1398 | unsigned long active; |
| 1399 | unsigned long gb; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1400 | |
Hugh Dickins | 4d7dcca | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 1401 | inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON); |
| 1402 | active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1403 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1404 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
| 1405 | if (gb) |
| 1406 | inactive_ratio = int_sqrt(10 * gb); |
| 1407 | else |
| 1408 | inactive_ratio = 1; |
| 1409 | |
Johannes Weiner | 9b27297 | 2011-11-02 13:38:23 -0700 | [diff] [blame] | 1410 | return inactive * inactive_ratio < active; |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1411 | } |
| 1412 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1413 | #define mem_cgroup_from_counter(counter, member) \ |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1414 | container_of(counter, struct mem_cgroup, member) |
| 1415 | |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1416 | /** |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1417 | * mem_cgroup_margin - calculate chargeable space of a memory cgroup |
Wanpeng Li | dad7557 | 2012-06-20 12:53:01 -0700 | [diff] [blame] | 1418 | * @memcg: the memory cgroup |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1419 | * |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1420 | * Returns the maximum amount of memory @mem can be charged with, in |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1421 | * pages. |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1422 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1423 | static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1424 | { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1425 | unsigned long margin = 0; |
| 1426 | unsigned long count; |
| 1427 | unsigned long limit; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1428 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1429 | count = page_counter_read(&memcg->memory); |
| 1430 | limit = ACCESS_ONCE(memcg->memory.limit); |
| 1431 | if (count < limit) |
| 1432 | margin = limit - count; |
| 1433 | |
| 1434 | if (do_swap_account) { |
| 1435 | count = page_counter_read(&memcg->memsw); |
| 1436 | limit = ACCESS_ONCE(memcg->memsw.limit); |
| 1437 | if (count <= limit) |
| 1438 | margin = min(margin, limit - count); |
| 1439 | } |
| 1440 | |
| 1441 | return margin; |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1442 | } |
| 1443 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 1444 | int mem_cgroup_swappiness(struct mem_cgroup *memcg) |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1445 | { |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1446 | /* root ? */ |
Linus Torvalds | 14208b0 | 2014-06-09 15:03:33 -0700 | [diff] [blame] | 1447 | if (mem_cgroup_disabled() || !memcg->css.parent) |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1448 | return vm_swappiness; |
| 1449 | |
Johannes Weiner | bf1ff26 | 2011-03-23 16:42:32 -0700 | [diff] [blame] | 1450 | return memcg->swappiness; |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1451 | } |
| 1452 | |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 1453 | /* |
| 1454 | * memcg->moving_account is used for checking possibility that some thread is |
| 1455 | * calling move_account(). When a thread on CPU-A starts moving pages under |
| 1456 | * a memcg, other threads should check memcg->moving_account under |
| 1457 | * rcu_read_lock(), like this: |
| 1458 | * |
| 1459 | * CPU-A CPU-B |
| 1460 | * rcu_read_lock() |
| 1461 | * memcg->moving_account+1 if (memcg->mocing_account) |
| 1462 | * take heavy locks. |
| 1463 | * synchronize_rcu() update something. |
| 1464 | * rcu_read_unlock() |
| 1465 | * start move here. |
| 1466 | */ |
KAMEZAWA Hiroyuki | 4331f7d | 2012-03-21 16:34:26 -0700 | [diff] [blame] | 1467 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1468 | static void mem_cgroup_start_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1469 | { |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 1470 | atomic_inc(&memcg->moving_account); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1471 | synchronize_rcu(); |
| 1472 | } |
| 1473 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1474 | static void mem_cgroup_end_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1475 | { |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 1476 | /* |
| 1477 | * Now, mem_cgroup_clear_mc() may call this function with NULL. |
| 1478 | * We check NULL in callee rather than caller. |
| 1479 | */ |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 1480 | if (memcg) |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 1481 | atomic_dec(&memcg->moving_account); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1482 | } |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 1483 | |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1484 | /* |
Qiang Huang | bdcbb65 | 2014-06-04 16:08:21 -0700 | [diff] [blame] | 1485 | * A routine for checking "mem" is under move_account() or not. |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1486 | * |
Qiang Huang | bdcbb65 | 2014-06-04 16:08:21 -0700 | [diff] [blame] | 1487 | * Checking a cgroup is mc.from or mc.to or under hierarchy of |
| 1488 | * moving cgroups. This is for waiting at high-memory pressure |
| 1489 | * caused by "move". |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1490 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1491 | static bool mem_cgroup_under_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1492 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1493 | struct mem_cgroup *from; |
| 1494 | struct mem_cgroup *to; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1495 | bool ret = false; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1496 | /* |
| 1497 | * Unlike task_move routines, we access mc.to, mc.from not under |
| 1498 | * mutual exclusion by cgroup_mutex. Here, we take spinlock instead. |
| 1499 | */ |
| 1500 | spin_lock(&mc.lock); |
| 1501 | from = mc.from; |
| 1502 | to = mc.to; |
| 1503 | if (!from) |
| 1504 | goto unlock; |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1505 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1506 | ret = mem_cgroup_same_or_subtree(memcg, from) |
| 1507 | || mem_cgroup_same_or_subtree(memcg, to); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1508 | unlock: |
| 1509 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1510 | return ret; |
| 1511 | } |
| 1512 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1513 | static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1514 | { |
| 1515 | if (mc.moving_task && current != mc.moving_task) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1516 | if (mem_cgroup_under_move(memcg)) { |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1517 | DEFINE_WAIT(wait); |
| 1518 | prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE); |
| 1519 | /* moving charge context might have finished. */ |
| 1520 | if (mc.moving_task) |
| 1521 | schedule(); |
| 1522 | finish_wait(&mc.waitq, &wait); |
| 1523 | return true; |
| 1524 | } |
| 1525 | } |
| 1526 | return false; |
| 1527 | } |
| 1528 | |
KAMEZAWA Hiroyuki | 312734c0 | 2012-03-21 16:34:24 -0700 | [diff] [blame] | 1529 | /* |
| 1530 | * Take this lock when |
| 1531 | * - a code tries to modify page's memcg while it's USED. |
| 1532 | * - a code tries to modify page state accounting in a memcg. |
KAMEZAWA Hiroyuki | 312734c0 | 2012-03-21 16:34:24 -0700 | [diff] [blame] | 1533 | */ |
| 1534 | static void move_lock_mem_cgroup(struct mem_cgroup *memcg, |
| 1535 | unsigned long *flags) |
| 1536 | { |
| 1537 | spin_lock_irqsave(&memcg->move_lock, *flags); |
| 1538 | } |
| 1539 | |
| 1540 | static void move_unlock_mem_cgroup(struct mem_cgroup *memcg, |
| 1541 | unsigned long *flags) |
| 1542 | { |
| 1543 | spin_unlock_irqrestore(&memcg->move_lock, *flags); |
| 1544 | } |
| 1545 | |
Sha Zhengju | 58cf188 | 2013-02-22 16:32:05 -0800 | [diff] [blame] | 1546 | #define K(x) ((x) << (PAGE_SHIFT-10)) |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1547 | /** |
Sha Zhengju | 58cf188 | 2013-02-22 16:32:05 -0800 | [diff] [blame] | 1548 | * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller. |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1549 | * @memcg: The memory cgroup that went over limit |
| 1550 | * @p: Task that is going to be killed |
| 1551 | * |
| 1552 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is |
| 1553 | * enabled |
| 1554 | */ |
| 1555 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) |
| 1556 | { |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1557 | /* oom_info_lock ensures that parallel ooms do not interleave */ |
Michal Hocko | 08088cb | 2014-02-25 15:01:44 -0800 | [diff] [blame] | 1558 | static DEFINE_MUTEX(oom_info_lock); |
Sha Zhengju | 58cf188 | 2013-02-22 16:32:05 -0800 | [diff] [blame] | 1559 | struct mem_cgroup *iter; |
| 1560 | unsigned int i; |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1561 | |
Sha Zhengju | 58cf188 | 2013-02-22 16:32:05 -0800 | [diff] [blame] | 1562 | if (!p) |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1563 | return; |
| 1564 | |
Michal Hocko | 08088cb | 2014-02-25 15:01:44 -0800 | [diff] [blame] | 1565 | mutex_lock(&oom_info_lock); |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1566 | rcu_read_lock(); |
| 1567 | |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1568 | pr_info("Task in "); |
| 1569 | pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id)); |
| 1570 | pr_info(" killed as a result of limit of "); |
| 1571 | pr_cont_cgroup_path(memcg->css.cgroup); |
| 1572 | pr_info("\n"); |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1573 | |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1574 | rcu_read_unlock(); |
| 1575 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1576 | pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n", |
| 1577 | K((u64)page_counter_read(&memcg->memory)), |
| 1578 | K((u64)memcg->memory.limit), memcg->memory.failcnt); |
| 1579 | pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n", |
| 1580 | K((u64)page_counter_read(&memcg->memsw)), |
| 1581 | K((u64)memcg->memsw.limit), memcg->memsw.failcnt); |
| 1582 | pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n", |
| 1583 | K((u64)page_counter_read(&memcg->kmem)), |
| 1584 | K((u64)memcg->kmem.limit), memcg->kmem.failcnt); |
Sha Zhengju | 58cf188 | 2013-02-22 16:32:05 -0800 | [diff] [blame] | 1585 | |
| 1586 | for_each_mem_cgroup_tree(iter, memcg) { |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1587 | pr_info("Memory cgroup stats for "); |
| 1588 | pr_cont_cgroup_path(iter->css.cgroup); |
Sha Zhengju | 58cf188 | 2013-02-22 16:32:05 -0800 | [diff] [blame] | 1589 | pr_cont(":"); |
| 1590 | |
| 1591 | for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) { |
| 1592 | if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account) |
| 1593 | continue; |
| 1594 | pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i], |
| 1595 | K(mem_cgroup_read_stat(iter, i))); |
| 1596 | } |
| 1597 | |
| 1598 | for (i = 0; i < NR_LRU_LISTS; i++) |
| 1599 | pr_cont(" %s:%luKB", mem_cgroup_lru_names[i], |
| 1600 | K(mem_cgroup_nr_lru_pages(iter, BIT(i)))); |
| 1601 | |
| 1602 | pr_cont("\n"); |
| 1603 | } |
Michal Hocko | 08088cb | 2014-02-25 15:01:44 -0800 | [diff] [blame] | 1604 | mutex_unlock(&oom_info_lock); |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1605 | } |
| 1606 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1607 | /* |
| 1608 | * This function returns the number of memcg under hierarchy tree. Returns |
| 1609 | * 1(self count) if no children. |
| 1610 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1611 | static int mem_cgroup_count_children(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1612 | { |
| 1613 | int num = 0; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1614 | struct mem_cgroup *iter; |
| 1615 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1616 | for_each_mem_cgroup_tree(iter, memcg) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1617 | num++; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1618 | return num; |
| 1619 | } |
| 1620 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1621 | /* |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1622 | * Return the memory (and swap, if configured) limit for a memcg. |
| 1623 | */ |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1624 | static unsigned long mem_cgroup_get_limit(struct mem_cgroup *memcg) |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1625 | { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1626 | unsigned long limit; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1627 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1628 | limit = memcg->memory.limit; |
Michal Hocko | 9a5a8f1 | 2012-11-16 14:14:49 -0800 | [diff] [blame] | 1629 | if (mem_cgroup_swappiness(memcg)) { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1630 | unsigned long memsw_limit; |
Michal Hocko | 9a5a8f1 | 2012-11-16 14:14:49 -0800 | [diff] [blame] | 1631 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1632 | memsw_limit = memcg->memsw.limit; |
| 1633 | limit = min(limit + total_swap_pages, memsw_limit); |
Michal Hocko | 9a5a8f1 | 2012-11-16 14:14:49 -0800 | [diff] [blame] | 1634 | } |
Michal Hocko | 9a5a8f1 | 2012-11-16 14:14:49 -0800 | [diff] [blame] | 1635 | return limit; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
David Rientjes | 1996546 | 2012-12-11 16:00:26 -0800 | [diff] [blame] | 1638 | static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, |
| 1639 | int order) |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1640 | { |
| 1641 | struct mem_cgroup *iter; |
| 1642 | unsigned long chosen_points = 0; |
| 1643 | unsigned long totalpages; |
| 1644 | unsigned int points = 0; |
| 1645 | struct task_struct *chosen = NULL; |
| 1646 | |
David Rientjes | 876aafb | 2012-07-31 16:43:48 -0700 | [diff] [blame] | 1647 | /* |
David Rientjes | 465adcf | 2013-04-29 15:08:45 -0700 | [diff] [blame] | 1648 | * If current has a pending SIGKILL or is exiting, then automatically |
| 1649 | * select it. The goal is to allow it to allocate so that it may |
| 1650 | * quickly exit and free its memory. |
David Rientjes | 876aafb | 2012-07-31 16:43:48 -0700 | [diff] [blame] | 1651 | */ |
David Rientjes | 465adcf | 2013-04-29 15:08:45 -0700 | [diff] [blame] | 1652 | if (fatal_signal_pending(current) || current->flags & PF_EXITING) { |
David Rientjes | 876aafb | 2012-07-31 16:43:48 -0700 | [diff] [blame] | 1653 | set_thread_flag(TIF_MEMDIE); |
| 1654 | return; |
| 1655 | } |
| 1656 | |
| 1657 | check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1658 | totalpages = mem_cgroup_get_limit(memcg) ? : 1; |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1659 | for_each_mem_cgroup_tree(iter, memcg) { |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 1660 | struct css_task_iter it; |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1661 | struct task_struct *task; |
| 1662 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 1663 | css_task_iter_start(&iter->css, &it); |
| 1664 | while ((task = css_task_iter_next(&it))) { |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1665 | switch (oom_scan_process_thread(task, totalpages, NULL, |
| 1666 | false)) { |
| 1667 | case OOM_SCAN_SELECT: |
| 1668 | if (chosen) |
| 1669 | put_task_struct(chosen); |
| 1670 | chosen = task; |
| 1671 | chosen_points = ULONG_MAX; |
| 1672 | get_task_struct(chosen); |
| 1673 | /* fall through */ |
| 1674 | case OOM_SCAN_CONTINUE: |
| 1675 | continue; |
| 1676 | case OOM_SCAN_ABORT: |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 1677 | css_task_iter_end(&it); |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1678 | mem_cgroup_iter_break(memcg, iter); |
| 1679 | if (chosen) |
| 1680 | put_task_struct(chosen); |
| 1681 | return; |
| 1682 | case OOM_SCAN_OK: |
| 1683 | break; |
| 1684 | }; |
| 1685 | points = oom_badness(task, memcg, NULL, totalpages); |
David Rientjes | d49ad93 | 2014-01-23 15:53:34 -0800 | [diff] [blame] | 1686 | if (!points || points < chosen_points) |
| 1687 | continue; |
| 1688 | /* Prefer thread group leaders for display purposes */ |
| 1689 | if (points == chosen_points && |
| 1690 | thread_group_leader(chosen)) |
| 1691 | continue; |
| 1692 | |
| 1693 | if (chosen) |
| 1694 | put_task_struct(chosen); |
| 1695 | chosen = task; |
| 1696 | chosen_points = points; |
| 1697 | get_task_struct(chosen); |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1698 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 1699 | css_task_iter_end(&it); |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | if (!chosen) |
| 1703 | return; |
| 1704 | points = chosen_points * 1000 / totalpages; |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1705 | oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg, |
| 1706 | NULL, "Memory cgroup out of memory"); |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1707 | } |
| 1708 | |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1709 | /** |
| 1710 | * test_mem_cgroup_node_reclaimable |
Wanpeng Li | dad7557 | 2012-06-20 12:53:01 -0700 | [diff] [blame] | 1711 | * @memcg: the target memcg |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1712 | * @nid: the node ID to be checked. |
| 1713 | * @noswap : specify true here if the user wants flle only information. |
| 1714 | * |
| 1715 | * This function returns whether the specified memcg contains any |
| 1716 | * reclaimable pages on a node. Returns true if there are any reclaimable |
| 1717 | * pages in the node. |
| 1718 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1719 | static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1720 | int nid, bool noswap) |
| 1721 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1722 | if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1723 | return true; |
| 1724 | if (noswap || !total_swap_pages) |
| 1725 | return false; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1726 | if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON)) |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1727 | return true; |
| 1728 | return false; |
| 1729 | |
| 1730 | } |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 1731 | #if MAX_NUMNODES > 1 |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1732 | |
| 1733 | /* |
| 1734 | * Always updating the nodemask is not very good - even if we have an empty |
| 1735 | * list or the wrong list here, we can start from some node and traverse all |
| 1736 | * nodes based on the zonelist. So update the list loosely once per 10 secs. |
| 1737 | * |
| 1738 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1739 | static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1740 | { |
| 1741 | int nid; |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1742 | /* |
| 1743 | * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET |
| 1744 | * pagein/pageout changes since the last update. |
| 1745 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1746 | if (!atomic_read(&memcg->numainfo_events)) |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1747 | return; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1748 | if (atomic_inc_return(&memcg->numainfo_updating) > 1) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1749 | return; |
| 1750 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1751 | /* make a nodemask where this memcg uses memory from */ |
Lai Jiangshan | 31aaea4 | 2012-12-12 13:51:27 -0800 | [diff] [blame] | 1752 | memcg->scan_nodes = node_states[N_MEMORY]; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1753 | |
Lai Jiangshan | 31aaea4 | 2012-12-12 13:51:27 -0800 | [diff] [blame] | 1754 | for_each_node_mask(nid, node_states[N_MEMORY]) { |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1755 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1756 | if (!test_mem_cgroup_node_reclaimable(memcg, nid, false)) |
| 1757 | node_clear(nid, memcg->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1758 | } |
KAMEZAWA Hiroyuki | 453a9bf | 2011-07-08 15:39:43 -0700 | [diff] [blame] | 1759 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1760 | atomic_set(&memcg->numainfo_events, 0); |
| 1761 | atomic_set(&memcg->numainfo_updating, 0); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | /* |
| 1765 | * Selecting a node where we start reclaim from. Because what we need is just |
| 1766 | * reducing usage counter, start from anywhere is O,K. Considering |
| 1767 | * memory reclaim from current node, there are pros. and cons. |
| 1768 | * |
| 1769 | * Freeing memory from current node means freeing memory from a node which |
| 1770 | * we'll use or we've used. So, it may make LRU bad. And if several threads |
| 1771 | * hit limits, it will see a contention on a node. But freeing from remote |
| 1772 | * node means more costs for memory reclaim because of memory latency. |
| 1773 | * |
| 1774 | * Now, we use round-robin. Better algorithm is welcomed. |
| 1775 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1776 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1777 | { |
| 1778 | int node; |
| 1779 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1780 | mem_cgroup_may_update_nodemask(memcg); |
| 1781 | node = memcg->last_scanned_node; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1782 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1783 | node = next_node(node, memcg->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1784 | if (node == MAX_NUMNODES) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1785 | node = first_node(memcg->scan_nodes); |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1786 | /* |
| 1787 | * We call this when we hit limit, not when pages are added to LRU. |
| 1788 | * No LRU may hold pages because all pages are UNEVICTABLE or |
| 1789 | * memcg is too small and all pages are not on LRU. In that case, |
| 1790 | * we use curret node. |
| 1791 | */ |
| 1792 | if (unlikely(node == MAX_NUMNODES)) |
| 1793 | node = numa_node_id(); |
| 1794 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1795 | memcg->last_scanned_node = node; |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1796 | return node; |
| 1797 | } |
| 1798 | |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 1799 | /* |
| 1800 | * Check all nodes whether it contains reclaimable pages or not. |
| 1801 | * For quick scan, we make use of scan_nodes. This will allow us to skip |
| 1802 | * unused nodes. But scan_nodes is lazily updated and may not cotain |
| 1803 | * enough new information. We need to do double check. |
| 1804 | */ |
| 1805 | static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap) |
| 1806 | { |
| 1807 | int nid; |
| 1808 | |
| 1809 | /* |
| 1810 | * quick check...making use of scan_node. |
| 1811 | * We can skip unused nodes. |
| 1812 | */ |
| 1813 | if (!nodes_empty(memcg->scan_nodes)) { |
| 1814 | for (nid = first_node(memcg->scan_nodes); |
| 1815 | nid < MAX_NUMNODES; |
| 1816 | nid = next_node(nid, memcg->scan_nodes)) { |
| 1817 | |
| 1818 | if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap)) |
| 1819 | return true; |
| 1820 | } |
| 1821 | } |
| 1822 | /* |
| 1823 | * Check rest of nodes. |
| 1824 | */ |
| 1825 | for_each_node_state(nid, N_MEMORY) { |
| 1826 | if (node_isset(nid, memcg->scan_nodes)) |
| 1827 | continue; |
| 1828 | if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap)) |
| 1829 | return true; |
| 1830 | } |
| 1831 | return false; |
| 1832 | } |
| 1833 | |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1834 | #else |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1835 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg) |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1836 | { |
| 1837 | return 0; |
| 1838 | } |
KAMEZAWA Hiroyuki | 4d0c066 | 2011-07-08 15:39:42 -0700 | [diff] [blame] | 1839 | |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 1840 | static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap) |
| 1841 | { |
| 1842 | return test_mem_cgroup_node_reclaimable(memcg, 0, noswap); |
| 1843 | } |
Ying Han | 889976d | 2011-05-26 16:25:33 -0700 | [diff] [blame] | 1844 | #endif |
| 1845 | |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 1846 | static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg, |
| 1847 | struct zone *zone, |
| 1848 | gfp_t gfp_mask, |
| 1849 | unsigned long *total_scanned) |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1850 | { |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 1851 | struct mem_cgroup *victim = NULL; |
| 1852 | int total = 0; |
| 1853 | int loop = 0; |
| 1854 | unsigned long excess; |
| 1855 | unsigned long nr_scanned; |
| 1856 | struct mem_cgroup_reclaim_cookie reclaim = { |
| 1857 | .zone = zone, |
| 1858 | .priority = 0, |
| 1859 | }; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1860 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1861 | excess = soft_limit_excess(root_memcg); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1862 | |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 1863 | while (1) { |
| 1864 | victim = mem_cgroup_iter(root_memcg, victim, &reclaim); |
| 1865 | if (!victim) { |
| 1866 | loop++; |
| 1867 | if (loop >= 2) { |
| 1868 | /* |
| 1869 | * If we have not been able to reclaim |
| 1870 | * anything, it might because there are |
| 1871 | * no reclaimable pages under this hierarchy |
| 1872 | */ |
| 1873 | if (!total) |
| 1874 | break; |
| 1875 | /* |
| 1876 | * We want to do more targeted reclaim. |
| 1877 | * excess >> 2 is not to excessive so as to |
| 1878 | * reclaim too much, nor too less that we keep |
| 1879 | * coming back to reclaim from this cgroup |
| 1880 | */ |
| 1881 | if (total >= (excess >> 2) || |
| 1882 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) |
| 1883 | break; |
| 1884 | } |
| 1885 | continue; |
| 1886 | } |
| 1887 | if (!mem_cgroup_reclaimable(victim, false)) |
| 1888 | continue; |
| 1889 | total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false, |
| 1890 | zone, &nr_scanned); |
| 1891 | *total_scanned += nr_scanned; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1892 | if (!soft_limit_excess(root_memcg)) |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 1893 | break; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1894 | } |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 1895 | mem_cgroup_iter_break(root_memcg, victim); |
| 1896 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1897 | } |
| 1898 | |
Johannes Weiner | 0056f4e | 2013-10-31 16:34:14 -0700 | [diff] [blame] | 1899 | #ifdef CONFIG_LOCKDEP |
| 1900 | static struct lockdep_map memcg_oom_lock_dep_map = { |
| 1901 | .name = "memcg_oom_lock", |
| 1902 | }; |
| 1903 | #endif |
| 1904 | |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1905 | static DEFINE_SPINLOCK(memcg_oom_lock); |
| 1906 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1907 | /* |
| 1908 | * Check OOM-Killer is already running under our hierarchy. |
| 1909 | * If someone is running, return false. |
| 1910 | */ |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1911 | static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1912 | { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1913 | struct mem_cgroup *iter, *failed = NULL; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1914 | |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1915 | spin_lock(&memcg_oom_lock); |
| 1916 | |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1917 | for_each_mem_cgroup_tree(iter, memcg) { |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 1918 | if (iter->oom_lock) { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1919 | /* |
| 1920 | * this subtree of our hierarchy is already locked |
| 1921 | * so we cannot give a lock. |
| 1922 | */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1923 | failed = iter; |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1924 | mem_cgroup_iter_break(memcg, iter); |
| 1925 | break; |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 1926 | } else |
| 1927 | iter->oom_lock = true; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1928 | } |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1929 | |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1930 | if (failed) { |
| 1931 | /* |
| 1932 | * OK, we failed to lock the whole subtree so we have |
| 1933 | * to clean up what we set up to the failing subtree |
| 1934 | */ |
| 1935 | for_each_mem_cgroup_tree(iter, memcg) { |
| 1936 | if (iter == failed) { |
| 1937 | mem_cgroup_iter_break(memcg, iter); |
| 1938 | break; |
| 1939 | } |
| 1940 | iter->oom_lock = false; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1941 | } |
Johannes Weiner | 0056f4e | 2013-10-31 16:34:14 -0700 | [diff] [blame] | 1942 | } else |
| 1943 | mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_); |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1944 | |
| 1945 | spin_unlock(&memcg_oom_lock); |
| 1946 | |
| 1947 | return !failed; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1948 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1949 | |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1950 | static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1951 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1952 | struct mem_cgroup *iter; |
| 1953 | |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1954 | spin_lock(&memcg_oom_lock); |
Johannes Weiner | 0056f4e | 2013-10-31 16:34:14 -0700 | [diff] [blame] | 1955 | mutex_release(&memcg_oom_lock_dep_map, 1, _RET_IP_); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1956 | for_each_mem_cgroup_tree(iter, memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1957 | iter->oom_lock = false; |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1958 | spin_unlock(&memcg_oom_lock); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1961 | static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1962 | { |
| 1963 | struct mem_cgroup *iter; |
| 1964 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1965 | for_each_mem_cgroup_tree(iter, memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1966 | atomic_inc(&iter->under_oom); |
| 1967 | } |
| 1968 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1969 | static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1970 | { |
| 1971 | struct mem_cgroup *iter; |
| 1972 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1973 | /* |
| 1974 | * When a new child is created while the hierarchy is under oom, |
| 1975 | * mem_cgroup_oom_lock() may not be called. We have to use |
| 1976 | * atomic_add_unless() here. |
| 1977 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1978 | for_each_mem_cgroup_tree(iter, memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1979 | atomic_add_unless(&iter->under_oom, -1, 0); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1980 | } |
| 1981 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1982 | static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq); |
| 1983 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1984 | struct oom_wait_info { |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 1985 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1986 | wait_queue_t wait; |
| 1987 | }; |
| 1988 | |
| 1989 | static int memcg_oom_wake_function(wait_queue_t *wait, |
| 1990 | unsigned mode, int sync, void *arg) |
| 1991 | { |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 1992 | struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg; |
| 1993 | struct mem_cgroup *oom_wait_memcg; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1994 | struct oom_wait_info *oom_wait_info; |
| 1995 | |
| 1996 | oom_wait_info = container_of(wait, struct oom_wait_info, wait); |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 1997 | oom_wait_memcg = oom_wait_info->memcg; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1998 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1999 | /* |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 2000 | * Both of oom_wait_info->memcg and wake_memcg are stable under us. |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2001 | * Then we can use css_is_ancestor without taking care of RCU. |
| 2002 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2003 | if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg) |
| 2004 | && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg)) |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2005 | return 0; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2006 | return autoremove_wake_function(wait, mode, sync, arg); |
| 2007 | } |
| 2008 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2009 | static void memcg_wakeup_oom(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2010 | { |
Johannes Weiner | 3812c8c | 2013-09-12 15:13:44 -0700 | [diff] [blame] | 2011 | atomic_inc(&memcg->oom_wakeups); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2012 | /* for filtering, pass "memcg" as argument. */ |
| 2013 | __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg); |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 2014 | } |
| 2015 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2016 | static void memcg_oom_recover(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2017 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2018 | if (memcg && atomic_read(&memcg->under_oom)) |
| 2019 | memcg_wakeup_oom(memcg); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
Johannes Weiner | 3812c8c | 2013-09-12 15:13:44 -0700 | [diff] [blame] | 2022 | static void mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order) |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2023 | { |
Johannes Weiner | 3812c8c | 2013-09-12 15:13:44 -0700 | [diff] [blame] | 2024 | if (!current->memcg_oom.may_oom) |
| 2025 | return; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2026 | /* |
Johannes Weiner | 4942642 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 2027 | * We are in the middle of the charge context here, so we |
| 2028 | * don't want to block when potentially sitting on a callstack |
| 2029 | * that holds all kinds of filesystem and mm locks. |
| 2030 | * |
| 2031 | * Also, the caller may handle a failed allocation gracefully |
| 2032 | * (like optional page cache readahead) and so an OOM killer |
| 2033 | * invocation might not even be necessary. |
| 2034 | * |
| 2035 | * That's why we don't do anything here except remember the |
| 2036 | * OOM context and then deal with it at the end of the page |
| 2037 | * fault when the stack is unwound, the locks are released, |
| 2038 | * and when we know whether the fault was overall successful. |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2039 | */ |
Johannes Weiner | 4942642 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 2040 | css_get(&memcg->css); |
| 2041 | current->memcg_oom.memcg = memcg; |
| 2042 | current->memcg_oom.gfp_mask = mask; |
| 2043 | current->memcg_oom.order = order; |
| 2044 | } |
| 2045 | |
| 2046 | /** |
| 2047 | * mem_cgroup_oom_synchronize - complete memcg OOM handling |
| 2048 | * @handle: actually kill/wait or just clean up the OOM state |
| 2049 | * |
| 2050 | * This has to be called at the end of a page fault if the memcg OOM |
| 2051 | * handler was enabled. |
| 2052 | * |
| 2053 | * Memcg supports userspace OOM handling where failed allocations must |
| 2054 | * sleep on a waitqueue until the userspace task resolves the |
| 2055 | * situation. Sleeping directly in the charge context with all kinds |
| 2056 | * of locks held is not a good idea, instead we remember an OOM state |
| 2057 | * in the task and mem_cgroup_oom_synchronize() has to be called at |
| 2058 | * the end of the page fault to complete the OOM handling. |
| 2059 | * |
| 2060 | * Returns %true if an ongoing memcg OOM situation was detected and |
| 2061 | * completed, %false otherwise. |
| 2062 | */ |
| 2063 | bool mem_cgroup_oom_synchronize(bool handle) |
| 2064 | { |
| 2065 | struct mem_cgroup *memcg = current->memcg_oom.memcg; |
| 2066 | struct oom_wait_info owait; |
| 2067 | bool locked; |
| 2068 | |
| 2069 | /* OOM is global, do not handle */ |
| 2070 | if (!memcg) |
| 2071 | return false; |
| 2072 | |
| 2073 | if (!handle) |
| 2074 | goto cleanup; |
| 2075 | |
| 2076 | owait.memcg = memcg; |
| 2077 | owait.wait.flags = 0; |
| 2078 | owait.wait.func = memcg_oom_wake_function; |
| 2079 | owait.wait.private = current; |
| 2080 | INIT_LIST_HEAD(&owait.wait.task_list); |
| 2081 | |
| 2082 | prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE); |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 2083 | mem_cgroup_mark_under_oom(memcg); |
| 2084 | |
| 2085 | locked = mem_cgroup_oom_trylock(memcg); |
| 2086 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2087 | if (locked) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2088 | mem_cgroup_oom_notify(memcg); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2089 | |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 2090 | if (locked && !memcg->oom_kill_disable) { |
| 2091 | mem_cgroup_unmark_under_oom(memcg); |
Johannes Weiner | 4942642 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 2092 | finish_wait(&memcg_oom_waitq, &owait.wait); |
| 2093 | mem_cgroup_out_of_memory(memcg, current->memcg_oom.gfp_mask, |
| 2094 | current->memcg_oom.order); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2095 | } else { |
Johannes Weiner | 3812c8c | 2013-09-12 15:13:44 -0700 | [diff] [blame] | 2096 | schedule(); |
Johannes Weiner | 4942642 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 2097 | mem_cgroup_unmark_under_oom(memcg); |
| 2098 | finish_wait(&memcg_oom_waitq, &owait.wait); |
| 2099 | } |
| 2100 | |
| 2101 | if (locked) { |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 2102 | mem_cgroup_oom_unlock(memcg); |
| 2103 | /* |
| 2104 | * There is no guarantee that an OOM-lock contender |
| 2105 | * sees the wakeups triggered by the OOM kill |
| 2106 | * uncharges. Wake any sleepers explicitely. |
| 2107 | */ |
| 2108 | memcg_oom_recover(memcg); |
| 2109 | } |
Johannes Weiner | 4942642 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 2110 | cleanup: |
| 2111 | current->memcg_oom.memcg = NULL; |
Johannes Weiner | 3812c8c | 2013-09-12 15:13:44 -0700 | [diff] [blame] | 2112 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2113 | return true; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 2114 | } |
| 2115 | |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2116 | /** |
| 2117 | * mem_cgroup_begin_page_stat - begin a page state statistics transaction |
| 2118 | * @page: page that is going to change accounted state |
| 2119 | * @locked: &memcg->move_lock slowpath was taken |
| 2120 | * @flags: IRQ-state flags for &memcg->move_lock |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2121 | * |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2122 | * This function must mark the beginning of an accounted page state |
| 2123 | * change to prevent double accounting when the page is concurrently |
| 2124 | * being moved to another memcg: |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2125 | * |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2126 | * memcg = mem_cgroup_begin_page_stat(page, &locked, &flags); |
| 2127 | * if (TestClearPageState(page)) |
| 2128 | * mem_cgroup_update_page_stat(memcg, state, -1); |
| 2129 | * mem_cgroup_end_page_stat(memcg, locked, flags); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2130 | * |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2131 | * The RCU lock is held throughout the transaction. The fast path can |
| 2132 | * get away without acquiring the memcg->move_lock (@locked is false) |
| 2133 | * because page moving starts with an RCU grace period. |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2134 | * |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2135 | * The RCU lock also protects the memcg from being freed when the page |
| 2136 | * state that is going to change is the only thing preventing the page |
| 2137 | * from being uncharged. E.g. end-writeback clearing PageWriteback(), |
| 2138 | * which allows migration to go ahead and uncharge the page before the |
| 2139 | * account transaction might be complete. |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2140 | */ |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2141 | struct mem_cgroup *mem_cgroup_begin_page_stat(struct page *page, |
| 2142 | bool *locked, |
| 2143 | unsigned long *flags) |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2144 | { |
| 2145 | struct mem_cgroup *memcg; |
| 2146 | struct page_cgroup *pc; |
| 2147 | |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2148 | rcu_read_lock(); |
| 2149 | |
| 2150 | if (mem_cgroup_disabled()) |
| 2151 | return NULL; |
| 2152 | |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2153 | pc = lookup_page_cgroup(page); |
| 2154 | again: |
| 2155 | memcg = pc->mem_cgroup; |
| 2156 | if (unlikely(!memcg || !PageCgroupUsed(pc))) |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2157 | return NULL; |
| 2158 | |
| 2159 | *locked = false; |
Qiang Huang | bdcbb65 | 2014-06-04 16:08:21 -0700 | [diff] [blame] | 2160 | if (atomic_read(&memcg->moving_account) <= 0) |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2161 | return memcg; |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2162 | |
| 2163 | move_lock_mem_cgroup(memcg, flags); |
| 2164 | if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) { |
| 2165 | move_unlock_mem_cgroup(memcg, flags); |
| 2166 | goto again; |
| 2167 | } |
| 2168 | *locked = true; |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2169 | |
| 2170 | return memcg; |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2171 | } |
| 2172 | |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2173 | /** |
| 2174 | * mem_cgroup_end_page_stat - finish a page state statistics transaction |
| 2175 | * @memcg: the memcg that was accounted against |
| 2176 | * @locked: value received from mem_cgroup_begin_page_stat() |
| 2177 | * @flags: value received from mem_cgroup_begin_page_stat() |
| 2178 | */ |
| 2179 | void mem_cgroup_end_page_stat(struct mem_cgroup *memcg, bool locked, |
| 2180 | unsigned long flags) |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2181 | { |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2182 | if (memcg && locked) |
| 2183 | move_unlock_mem_cgroup(memcg, &flags); |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2184 | |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2185 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2186 | } |
| 2187 | |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2188 | /** |
| 2189 | * mem_cgroup_update_page_stat - update page state statistics |
| 2190 | * @memcg: memcg to account against |
| 2191 | * @idx: page state item to account |
| 2192 | * @val: number of pages (positive or negative) |
| 2193 | * |
| 2194 | * See mem_cgroup_begin_page_stat() for locking requirements. |
| 2195 | */ |
| 2196 | void mem_cgroup_update_page_stat(struct mem_cgroup *memcg, |
Sha Zhengju | 68b4876 | 2013-09-12 15:13:50 -0700 | [diff] [blame] | 2197 | enum mem_cgroup_stat_index idx, int val) |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2198 | { |
Sha Zhengju | 658b72c | 2013-09-12 15:13:52 -0700 | [diff] [blame] | 2199 | VM_BUG_ON(!rcu_read_lock_held()); |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2200 | |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2201 | if (memcg) |
| 2202 | this_cpu_add(memcg->stat->count[idx], val); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2203 | } |
KAMEZAWA Hiroyuki | 26174ef | 2010-10-27 15:33:43 -0700 | [diff] [blame] | 2204 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2205 | /* |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2206 | * size of first charge trial. "32" comes from vmscan.c's magic value. |
| 2207 | * TODO: maybe necessary to use big numbers in big irons. |
| 2208 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2209 | #define CHARGE_BATCH 32U |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2210 | struct memcg_stock_pcp { |
| 2211 | struct mem_cgroup *cached; /* this never be root cgroup */ |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2212 | unsigned int nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2213 | struct work_struct work; |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2214 | unsigned long flags; |
Kirill A. Shutemov | a0db00f | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 2215 | #define FLUSHING_CACHED_CHARGE 0 |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2216 | }; |
| 2217 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2218 | static DEFINE_MUTEX(percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2219 | |
Suleiman Souhlal | a0956d5 | 2012-12-18 14:21:36 -0800 | [diff] [blame] | 2220 | /** |
| 2221 | * consume_stock: Try to consume stocked charge on this cpu. |
| 2222 | * @memcg: memcg to consume from. |
| 2223 | * @nr_pages: how many pages to charge. |
| 2224 | * |
| 2225 | * The charges will only happen if @memcg matches the current cpu's memcg |
| 2226 | * stock, and at least @nr_pages are available in that stock. Failure to |
| 2227 | * service an allocation will refill the stock. |
| 2228 | * |
| 2229 | * returns true if successful, false otherwise. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2230 | */ |
Suleiman Souhlal | a0956d5 | 2012-12-18 14:21:36 -0800 | [diff] [blame] | 2231 | static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2232 | { |
| 2233 | struct memcg_stock_pcp *stock; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2234 | bool ret = false; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2235 | |
Suleiman Souhlal | a0956d5 | 2012-12-18 14:21:36 -0800 | [diff] [blame] | 2236 | if (nr_pages > CHARGE_BATCH) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2237 | return ret; |
Suleiman Souhlal | a0956d5 | 2012-12-18 14:21:36 -0800 | [diff] [blame] | 2238 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2239 | stock = &get_cpu_var(memcg_stock); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2240 | if (memcg == stock->cached && stock->nr_pages >= nr_pages) { |
Suleiman Souhlal | a0956d5 | 2012-12-18 14:21:36 -0800 | [diff] [blame] | 2241 | stock->nr_pages -= nr_pages; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2242 | ret = true; |
| 2243 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2244 | put_cpu_var(memcg_stock); |
| 2245 | return ret; |
| 2246 | } |
| 2247 | |
| 2248 | /* |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2249 | * Returns stocks cached in percpu and reset cached information. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2250 | */ |
| 2251 | static void drain_stock(struct memcg_stock_pcp *stock) |
| 2252 | { |
| 2253 | struct mem_cgroup *old = stock->cached; |
| 2254 | |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2255 | if (stock->nr_pages) { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2256 | page_counter_uncharge(&old->memory, stock->nr_pages); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2257 | if (do_swap_account) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2258 | page_counter_uncharge(&old->memsw, stock->nr_pages); |
Johannes Weiner | e8ea14c | 2014-12-10 15:42:42 -0800 | [diff] [blame] | 2259 | css_put_many(&old->css, stock->nr_pages); |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2260 | stock->nr_pages = 0; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2261 | } |
| 2262 | stock->cached = NULL; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | /* |
| 2266 | * This must be called under preempt disabled or must be called by |
| 2267 | * a thread which is pinned to local cpu. |
| 2268 | */ |
| 2269 | static void drain_local_stock(struct work_struct *dummy) |
| 2270 | { |
Christoph Lameter | 7c8e018 | 2014-06-04 16:07:56 -0700 | [diff] [blame] | 2271 | struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2272 | drain_stock(stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2273 | clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2274 | } |
| 2275 | |
Michal Hocko | e477749 | 2013-02-22 16:35:40 -0800 | [diff] [blame] | 2276 | static void __init memcg_stock_init(void) |
| 2277 | { |
| 2278 | int cpu; |
| 2279 | |
| 2280 | for_each_possible_cpu(cpu) { |
| 2281 | struct memcg_stock_pcp *stock = |
| 2282 | &per_cpu(memcg_stock, cpu); |
| 2283 | INIT_WORK(&stock->work, drain_local_stock); |
| 2284 | } |
| 2285 | } |
| 2286 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2287 | /* |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2288 | * Cache charges(val) to local per_cpu area. |
Greg Thelen | 320cc51 | 2010-03-15 15:27:28 +0100 | [diff] [blame] | 2289 | * This will be consumed by consume_stock() function, later. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2290 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2291 | static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2292 | { |
| 2293 | struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock); |
| 2294 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2295 | if (stock->cached != memcg) { /* reset if necessary */ |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2296 | drain_stock(stock); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2297 | stock->cached = memcg; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2298 | } |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2299 | stock->nr_pages += nr_pages; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2300 | put_cpu_var(memcg_stock); |
| 2301 | } |
| 2302 | |
| 2303 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2304 | * Drains all per-CPU charge caches for given root_memcg resp. subtree |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2305 | * of the hierarchy under it. sync flag says whether we should block |
| 2306 | * until the work is done. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2307 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2308 | static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2309 | { |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2310 | int cpu, curcpu; |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2311 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2312 | /* Notify other cpus that system-wide "drain" is running */ |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2313 | get_online_cpus(); |
Johannes Weiner | 5af12d0 | 2011-08-25 15:59:07 -0700 | [diff] [blame] | 2314 | curcpu = get_cpu(); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2315 | for_each_online_cpu(cpu) { |
| 2316 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2317 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2318 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2319 | memcg = stock->cached; |
| 2320 | if (!memcg || !stock->nr_pages) |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2321 | continue; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2322 | if (!mem_cgroup_same_or_subtree(root_memcg, memcg)) |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 2323 | continue; |
Michal Hocko | d1a05b6 | 2011-07-26 16:08:27 -0700 | [diff] [blame] | 2324 | if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) { |
| 2325 | if (cpu == curcpu) |
| 2326 | drain_local_stock(&stock->work); |
| 2327 | else |
| 2328 | schedule_work_on(cpu, &stock->work); |
| 2329 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2330 | } |
Johannes Weiner | 5af12d0 | 2011-08-25 15:59:07 -0700 | [diff] [blame] | 2331 | put_cpu(); |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2332 | |
| 2333 | if (!sync) |
| 2334 | goto out; |
| 2335 | |
| 2336 | for_each_online_cpu(cpu) { |
| 2337 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2338 | if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2339 | flush_work(&stock->work); |
| 2340 | } |
| 2341 | out: |
Andrew Morton | f894ffa | 2013-09-12 15:13:35 -0700 | [diff] [blame] | 2342 | put_online_cpus(); |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | /* |
| 2346 | * Tries to drain stocked charges in other cpus. This function is asynchronous |
| 2347 | * and just put a work per cpu for draining localy on each cpu. Caller can |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2348 | * expects some charges will be back later but cannot wait for it. |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2349 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2350 | static void drain_all_stock_async(struct mem_cgroup *root_memcg) |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2351 | { |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2352 | /* |
| 2353 | * If someone calls draining, avoid adding more kworker runs. |
| 2354 | */ |
| 2355 | if (!mutex_trylock(&percpu_charge_mutex)) |
| 2356 | return; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2357 | drain_all_stock(root_memcg, false); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2358 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | /* This is a synchronous drain interface. */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2362 | static void drain_all_stock_sync(struct mem_cgroup *root_memcg) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2363 | { |
| 2364 | /* called when force_empty is called */ |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2365 | mutex_lock(&percpu_charge_mutex); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2366 | drain_all_stock(root_memcg, true); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2367 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2368 | } |
| 2369 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2370 | /* |
| 2371 | * This function drains percpu counter value from DEAD cpu and |
| 2372 | * move it to local cpu. Note that this function can be preempted. |
| 2373 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2374 | static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu) |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2375 | { |
| 2376 | int i; |
| 2377 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2378 | spin_lock(&memcg->pcp_counter_lock); |
Johannes Weiner | 6104621 | 2012-05-29 15:07:05 -0700 | [diff] [blame] | 2379 | for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2380 | long x = per_cpu(memcg->stat->count[i], cpu); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2381 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2382 | per_cpu(memcg->stat->count[i], cpu) = 0; |
| 2383 | memcg->nocpu_base.count[i] += x; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2384 | } |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2385 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2386 | unsigned long x = per_cpu(memcg->stat->events[i], cpu); |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2387 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2388 | per_cpu(memcg->stat->events[i], cpu) = 0; |
| 2389 | memcg->nocpu_base.events[i] += x; |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 2390 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2391 | spin_unlock(&memcg->pcp_counter_lock); |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2392 | } |
| 2393 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 2394 | static int memcg_cpu_hotplug_callback(struct notifier_block *nb, |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2395 | unsigned long action, |
| 2396 | void *hcpu) |
| 2397 | { |
| 2398 | int cpu = (unsigned long)hcpu; |
| 2399 | struct memcg_stock_pcp *stock; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2400 | struct mem_cgroup *iter; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2401 | |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 2402 | if (action == CPU_ONLINE) |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2403 | return NOTIFY_OK; |
KAMEZAWA Hiroyuki | 1489eba | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2404 | |
Kirill A. Shutemov | d833049 | 2012-04-12 12:49:11 -0700 | [diff] [blame] | 2405 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2406 | return NOTIFY_OK; |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2407 | |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 2408 | for_each_mem_cgroup(iter) |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 2409 | mem_cgroup_drain_pcp_counter(iter, cpu); |
| 2410 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2411 | stock = &per_cpu(memcg_stock, cpu); |
| 2412 | drain_stock(stock); |
| 2413 | return NOTIFY_OK; |
| 2414 | } |
| 2415 | |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 2416 | static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, |
| 2417 | unsigned int nr_pages) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2418 | { |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 2419 | unsigned int batch = max(CHARGE_BATCH, nr_pages); |
Johannes Weiner | 9b13061 | 2014-08-06 16:05:51 -0700 | [diff] [blame] | 2420 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2421 | struct mem_cgroup *mem_over_limit; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2422 | struct page_counter *counter; |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2423 | unsigned long nr_reclaimed; |
Johannes Weiner | b70a2a2 | 2014-10-09 15:28:56 -0700 | [diff] [blame] | 2424 | bool may_swap = true; |
| 2425 | bool drained = false; |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 2426 | int ret = 0; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2427 | |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 2428 | if (mem_cgroup_is_root(memcg)) |
| 2429 | goto done; |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2430 | retry: |
Michal Hocko | b6b6cc7 | 2014-04-07 15:37:44 -0700 | [diff] [blame] | 2431 | if (consume_stock(memcg, nr_pages)) |
| 2432 | goto done; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2433 | |
Johannes Weiner | 3fbe724 | 2014-10-09 15:28:54 -0700 | [diff] [blame] | 2434 | if (!do_swap_account || |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2435 | !page_counter_try_charge(&memcg->memsw, batch, &counter)) { |
| 2436 | if (!page_counter_try_charge(&memcg->memory, batch, &counter)) |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2437 | goto done_restock; |
Johannes Weiner | 3fbe724 | 2014-10-09 15:28:54 -0700 | [diff] [blame] | 2438 | if (do_swap_account) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2439 | page_counter_uncharge(&memcg->memsw, batch); |
| 2440 | mem_over_limit = mem_cgroup_from_counter(counter, memory); |
Johannes Weiner | 3fbe724 | 2014-10-09 15:28:54 -0700 | [diff] [blame] | 2441 | } else { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2442 | mem_over_limit = mem_cgroup_from_counter(counter, memsw); |
Johannes Weiner | b70a2a2 | 2014-10-09 15:28:56 -0700 | [diff] [blame] | 2443 | may_swap = false; |
Johannes Weiner | 3fbe724 | 2014-10-09 15:28:54 -0700 | [diff] [blame] | 2444 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2445 | |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2446 | if (batch > nr_pages) { |
| 2447 | batch = nr_pages; |
| 2448 | goto retry; |
| 2449 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2450 | |
Johannes Weiner | 06b078f | 2014-08-06 16:05:44 -0700 | [diff] [blame] | 2451 | /* |
| 2452 | * Unlike in global OOM situations, memcg is not in a physical |
| 2453 | * memory shortage. Allow dying and OOM-killed tasks to |
| 2454 | * bypass the last charges so that they can exit quickly and |
| 2455 | * free their memory. |
| 2456 | */ |
| 2457 | if (unlikely(test_thread_flag(TIF_MEMDIE) || |
| 2458 | fatal_signal_pending(current) || |
| 2459 | current->flags & PF_EXITING)) |
| 2460 | goto bypass; |
| 2461 | |
| 2462 | if (unlikely(task_in_memcg_oom(current))) |
| 2463 | goto nomem; |
| 2464 | |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2465 | if (!(gfp_mask & __GFP_WAIT)) |
| 2466 | goto nomem; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2467 | |
Johannes Weiner | b70a2a2 | 2014-10-09 15:28:56 -0700 | [diff] [blame] | 2468 | nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages, |
| 2469 | gfp_mask, may_swap); |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2470 | |
Johannes Weiner | 61e02c7 | 2014-08-06 16:08:16 -0700 | [diff] [blame] | 2471 | if (mem_cgroup_margin(mem_over_limit) >= nr_pages) |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2472 | goto retry; |
Johannes Weiner | 28c34c2 | 2014-08-06 16:05:47 -0700 | [diff] [blame] | 2473 | |
Johannes Weiner | b70a2a2 | 2014-10-09 15:28:56 -0700 | [diff] [blame] | 2474 | if (!drained) { |
| 2475 | drain_all_stock_async(mem_over_limit); |
| 2476 | drained = true; |
| 2477 | goto retry; |
| 2478 | } |
| 2479 | |
Johannes Weiner | 28c34c2 | 2014-08-06 16:05:47 -0700 | [diff] [blame] | 2480 | if (gfp_mask & __GFP_NORETRY) |
| 2481 | goto nomem; |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2482 | /* |
| 2483 | * Even though the limit is exceeded at this point, reclaim |
| 2484 | * may have been able to free some pages. Retry the charge |
| 2485 | * before killing the task. |
| 2486 | * |
| 2487 | * Only for regular pages, though: huge pages are rather |
| 2488 | * unlikely to succeed so close to the limit, and we fall back |
| 2489 | * to regular pages anyway in case of failure. |
| 2490 | */ |
Johannes Weiner | 61e02c7 | 2014-08-06 16:08:16 -0700 | [diff] [blame] | 2491 | if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER)) |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2492 | goto retry; |
| 2493 | /* |
| 2494 | * At task move, charge accounts can be doubly counted. So, it's |
| 2495 | * better to wait until the end of task_move if something is going on. |
| 2496 | */ |
| 2497 | if (mem_cgroup_wait_acct_move(mem_over_limit)) |
| 2498 | goto retry; |
| 2499 | |
Johannes Weiner | 9b13061 | 2014-08-06 16:05:51 -0700 | [diff] [blame] | 2500 | if (nr_retries--) |
| 2501 | goto retry; |
| 2502 | |
Johannes Weiner | 06b078f | 2014-08-06 16:05:44 -0700 | [diff] [blame] | 2503 | if (gfp_mask & __GFP_NOFAIL) |
| 2504 | goto bypass; |
| 2505 | |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2506 | if (fatal_signal_pending(current)) |
| 2507 | goto bypass; |
| 2508 | |
Johannes Weiner | 61e02c7 | 2014-08-06 16:08:16 -0700 | [diff] [blame] | 2509 | mem_cgroup_oom(mem_over_limit, gfp_mask, get_order(nr_pages)); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2510 | nomem: |
Johannes Weiner | 6d1fdc4 | 2014-04-07 15:37:45 -0700 | [diff] [blame] | 2511 | if (!(gfp_mask & __GFP_NOFAIL)) |
Johannes Weiner | 3168ecb | 2013-10-31 16:34:13 -0700 | [diff] [blame] | 2512 | return -ENOMEM; |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2513 | bypass: |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 2514 | return -EINTR; |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2515 | |
| 2516 | done_restock: |
Johannes Weiner | e8ea14c | 2014-12-10 15:42:42 -0800 | [diff] [blame] | 2517 | css_get_many(&memcg->css, batch); |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2518 | if (batch > nr_pages) |
| 2519 | refill_stock(memcg, batch - nr_pages); |
| 2520 | done: |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 2521 | return ret; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2522 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2523 | |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 2524 | static void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages) |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2525 | { |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 2526 | if (mem_cgroup_is_root(memcg)) |
| 2527 | return; |
| 2528 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2529 | page_counter_uncharge(&memcg->memory, nr_pages); |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 2530 | if (do_swap_account) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2531 | page_counter_uncharge(&memcg->memsw, nr_pages); |
Johannes Weiner | e8ea14c | 2014-12-10 15:42:42 -0800 | [diff] [blame] | 2532 | |
| 2533 | css_put_many(&memcg->css, nr_pages); |
KAMEZAWA Hiroyuki | d01dd17 | 2012-05-29 15:07:03 -0700 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | /* |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2537 | * A helper function to get mem_cgroup from ID. must be called under |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 2538 | * rcu_read_lock(). The caller is responsible for calling |
| 2539 | * css_tryget_online() if the mem_cgroup is used for charging. (dropping |
| 2540 | * refcnt from swap can be called against removed memcg.) |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2541 | */ |
| 2542 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) |
| 2543 | { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2544 | /* ID 0 is unused ID */ |
| 2545 | if (!id) |
| 2546 | return NULL; |
Li Zefan | 34c00c3 | 2013-09-23 16:56:01 +0800 | [diff] [blame] | 2547 | return mem_cgroup_from_id(id); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2548 | } |
| 2549 | |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 2550 | /* |
| 2551 | * try_get_mem_cgroup_from_page - look up page's memcg association |
| 2552 | * @page: the page |
| 2553 | * |
| 2554 | * Look up, get a css reference, and return the memcg that owns @page. |
| 2555 | * |
| 2556 | * The page must be locked to prevent racing with swap-in and page |
| 2557 | * cache charges. If coming from an unlocked page table, the caller |
| 2558 | * must ensure the page is on the LRU or this can race with charging. |
| 2559 | */ |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2560 | struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2561 | { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2562 | struct mem_cgroup *memcg = NULL; |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2563 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2564 | unsigned short id; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2565 | swp_entry_t ent; |
| 2566 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 2567 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2568 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2569 | pc = lookup_page_cgroup(page); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2570 | if (PageCgroupUsed(pc)) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2571 | memcg = pc->mem_cgroup; |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 2572 | if (memcg && !css_tryget_online(&memcg->css)) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2573 | memcg = NULL; |
Wu Fengguang | e42d9d5 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 2574 | } else if (PageSwapCache(page)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2575 | ent.val = page_private(page); |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 2576 | id = lookup_swap_cgroup_id(ent); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2577 | rcu_read_lock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2578 | memcg = mem_cgroup_lookup(id); |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 2579 | if (memcg && !css_tryget_online(&memcg->css)) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2580 | memcg = NULL; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 2581 | rcu_read_unlock(); |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 2582 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2583 | return memcg; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 2584 | } |
| 2585 | |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 2586 | static void lock_page_lru(struct page *page, int *isolated) |
| 2587 | { |
| 2588 | struct zone *zone = page_zone(page); |
| 2589 | |
| 2590 | spin_lock_irq(&zone->lru_lock); |
| 2591 | if (PageLRU(page)) { |
| 2592 | struct lruvec *lruvec; |
| 2593 | |
| 2594 | lruvec = mem_cgroup_page_lruvec(page, zone); |
| 2595 | ClearPageLRU(page); |
| 2596 | del_page_from_lru_list(page, lruvec, page_lru(page)); |
| 2597 | *isolated = 1; |
| 2598 | } else |
| 2599 | *isolated = 0; |
| 2600 | } |
| 2601 | |
| 2602 | static void unlock_page_lru(struct page *page, int isolated) |
| 2603 | { |
| 2604 | struct zone *zone = page_zone(page); |
| 2605 | |
| 2606 | if (isolated) { |
| 2607 | struct lruvec *lruvec; |
| 2608 | |
| 2609 | lruvec = mem_cgroup_page_lruvec(page, zone); |
| 2610 | VM_BUG_ON_PAGE(PageLRU(page), page); |
| 2611 | SetPageLRU(page); |
| 2612 | add_page_to_lru_list(page, lruvec, page_lru(page)); |
| 2613 | } |
| 2614 | spin_unlock_irq(&zone->lru_lock); |
| 2615 | } |
| 2616 | |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 2617 | static void commit_charge(struct page *page, struct mem_cgroup *memcg, |
Johannes Weiner | 6abb5a8 | 2014-08-08 14:19:33 -0700 | [diff] [blame] | 2618 | bool lrucare) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2619 | { |
Johannes Weiner | ce587e6 | 2012-04-24 20:22:33 +0200 | [diff] [blame] | 2620 | struct page_cgroup *pc = lookup_page_cgroup(page); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 2621 | int isolated; |
Hugh Dickins | 9ce70c0 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 2622 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 2623 | VM_BUG_ON_PAGE(PageCgroupUsed(pc), page); |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 2624 | /* |
| 2625 | * we don't need page_cgroup_lock about tail pages, becase they are not |
| 2626 | * accessed by any other context at this point. |
| 2627 | */ |
Hugh Dickins | 9ce70c0 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 2628 | |
| 2629 | /* |
| 2630 | * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page |
| 2631 | * may already be on some other mem_cgroup's LRU. Take care of it. |
| 2632 | */ |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 2633 | if (lrucare) |
| 2634 | lock_page_lru(page, &isolated); |
Hugh Dickins | 9ce70c0 | 2012-03-05 14:59:16 -0800 | [diff] [blame] | 2635 | |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 2636 | /* |
| 2637 | * Nobody should be changing or seriously looking at |
| 2638 | * pc->mem_cgroup and pc->flags at this point: |
| 2639 | * |
| 2640 | * - the page is uncharged |
| 2641 | * |
| 2642 | * - the page is off-LRU |
| 2643 | * |
| 2644 | * - an anonymous fault has exclusive page access, except for |
| 2645 | * a locked page table |
| 2646 | * |
| 2647 | * - a page cache insertion, a swapin fault, or a migration |
| 2648 | * have the page locked |
| 2649 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2650 | pc->mem_cgroup = memcg; |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 2651 | pc->flags = PCG_USED | PCG_MEM | (do_swap_account ? PCG_MEMSW : 0); |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 2652 | |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 2653 | if (lrucare) |
| 2654 | unlock_page_lru(page, isolated); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2655 | } |
| 2656 | |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2657 | #ifdef CONFIG_MEMCG_KMEM |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2658 | /* |
| 2659 | * The memcg_slab_mutex is held whenever a per memcg kmem cache is created or |
| 2660 | * destroyed. It protects memcg_caches arrays and memcg_slab_caches lists. |
| 2661 | */ |
| 2662 | static DEFINE_MUTEX(memcg_slab_mutex); |
| 2663 | |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 2664 | static DEFINE_MUTEX(activate_kmem_mutex); |
| 2665 | |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 2666 | /* |
| 2667 | * This is a bit cumbersome, but it is rarely used and avoids a backpointer |
| 2668 | * in the memcg_cache_params struct. |
| 2669 | */ |
| 2670 | static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p) |
| 2671 | { |
| 2672 | struct kmem_cache *cachep; |
| 2673 | |
| 2674 | VM_BUG_ON(p->is_root_cache); |
| 2675 | cachep = p->root_cache; |
Qiang Huang | 7a67d7a | 2013-11-12 15:08:24 -0800 | [diff] [blame] | 2676 | return cache_from_memcg_idx(cachep, memcg_cache_id(p->memcg)); |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 2677 | } |
| 2678 | |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 2679 | #ifdef CONFIG_SLABINFO |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2680 | static int mem_cgroup_slabinfo_read(struct seq_file *m, void *v) |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 2681 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2682 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 2683 | struct memcg_cache_params *params; |
| 2684 | |
Vladimir Davydov | cf2b8fb | 2014-10-09 15:28:59 -0700 | [diff] [blame] | 2685 | if (!memcg_kmem_is_active(memcg)) |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 2686 | return -EIO; |
| 2687 | |
| 2688 | print_slabinfo_header(m); |
| 2689 | |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2690 | mutex_lock(&memcg_slab_mutex); |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 2691 | list_for_each_entry(params, &memcg->memcg_slab_caches, list) |
| 2692 | cache_show(memcg_params_to_cache(params), m); |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2693 | mutex_unlock(&memcg_slab_mutex); |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 2694 | |
| 2695 | return 0; |
| 2696 | } |
| 2697 | #endif |
| 2698 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2699 | static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, |
| 2700 | unsigned long nr_pages) |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2701 | { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2702 | struct page_counter *counter; |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2703 | int ret = 0; |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2704 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2705 | ret = page_counter_try_charge(&memcg->kmem, nr_pages, &counter); |
| 2706 | if (ret < 0) |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2707 | return ret; |
| 2708 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2709 | ret = try_charge(memcg, gfp, nr_pages); |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2710 | if (ret == -EINTR) { |
| 2711 | /* |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 2712 | * try_charge() chose to bypass to root due to OOM kill or |
| 2713 | * fatal signal. Since our only options are to either fail |
| 2714 | * the allocation or charge it to this cgroup, do it as a |
| 2715 | * temporary condition. But we can't fail. From a kmem/slab |
| 2716 | * perspective, the cache has already been selected, by |
| 2717 | * mem_cgroup_kmem_get_cache(), so it is too late to change |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2718 | * our minds. |
| 2719 | * |
| 2720 | * This condition will only trigger if the task entered |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 2721 | * memcg_charge_kmem in a sane state, but was OOM-killed |
| 2722 | * during try_charge() above. Tasks that were already dying |
| 2723 | * when the allocation triggers should have been already |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2724 | * directed to the root cgroup in memcontrol.h |
| 2725 | */ |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2726 | page_counter_charge(&memcg->memory, nr_pages); |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2727 | if (do_swap_account) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2728 | page_counter_charge(&memcg->memsw, nr_pages); |
Johannes Weiner | e8ea14c | 2014-12-10 15:42:42 -0800 | [diff] [blame] | 2729 | css_get_many(&memcg->css, nr_pages); |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2730 | ret = 0; |
| 2731 | } else if (ret) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2732 | page_counter_uncharge(&memcg->kmem, nr_pages); |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2733 | |
| 2734 | return ret; |
| 2735 | } |
| 2736 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2737 | static void memcg_uncharge_kmem(struct mem_cgroup *memcg, |
| 2738 | unsigned long nr_pages) |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2739 | { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2740 | page_counter_uncharge(&memcg->memory, nr_pages); |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2741 | if (do_swap_account) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2742 | page_counter_uncharge(&memcg->memsw, nr_pages); |
Glauber Costa | 7de3768 | 2012-12-18 14:22:07 -0800 | [diff] [blame] | 2743 | |
Johannes Weiner | 64f2199 | 2014-12-10 15:42:45 -0800 | [diff] [blame^] | 2744 | page_counter_uncharge(&memcg->kmem, nr_pages); |
Johannes Weiner | e8ea14c | 2014-12-10 15:42:42 -0800 | [diff] [blame] | 2745 | |
| 2746 | css_put_many(&memcg->css, nr_pages); |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 2747 | } |
| 2748 | |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 2749 | /* |
| 2750 | * helper for acessing a memcg's index. It will be used as an index in the |
| 2751 | * child cache array in kmem_cache, and also to derive its name. This function |
| 2752 | * will return -1 when this is not a kmem-limited memcg. |
| 2753 | */ |
| 2754 | int memcg_cache_id(struct mem_cgroup *memcg) |
| 2755 | { |
| 2756 | return memcg ? memcg->kmemcg_id : -1; |
| 2757 | } |
| 2758 | |
Vladimir Davydov | f3bb304 | 2014-10-09 15:28:45 -0700 | [diff] [blame] | 2759 | static int memcg_alloc_cache_id(void) |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 2760 | { |
Vladimir Davydov | f3bb304 | 2014-10-09 15:28:45 -0700 | [diff] [blame] | 2761 | int id, size; |
| 2762 | int err; |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 2763 | |
Vladimir Davydov | f3bb304 | 2014-10-09 15:28:45 -0700 | [diff] [blame] | 2764 | id = ida_simple_get(&kmem_limited_groups, |
| 2765 | 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL); |
| 2766 | if (id < 0) |
| 2767 | return id; |
| 2768 | |
| 2769 | if (id < memcg_limited_groups_array_size) |
| 2770 | return id; |
| 2771 | |
| 2772 | /* |
| 2773 | * There's no space for the new id in memcg_caches arrays, |
| 2774 | * so we have to grow them. |
| 2775 | */ |
| 2776 | |
| 2777 | size = 2 * (id + 1); |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 2778 | if (size < MEMCG_CACHES_MIN_SIZE) |
| 2779 | size = MEMCG_CACHES_MIN_SIZE; |
| 2780 | else if (size > MEMCG_CACHES_MAX_SIZE) |
| 2781 | size = MEMCG_CACHES_MAX_SIZE; |
| 2782 | |
Vladimir Davydov | f3bb304 | 2014-10-09 15:28:45 -0700 | [diff] [blame] | 2783 | mutex_lock(&memcg_slab_mutex); |
| 2784 | err = memcg_update_all_caches(size); |
| 2785 | mutex_unlock(&memcg_slab_mutex); |
| 2786 | |
| 2787 | if (err) { |
| 2788 | ida_simple_remove(&kmem_limited_groups, id); |
| 2789 | return err; |
| 2790 | } |
| 2791 | return id; |
| 2792 | } |
| 2793 | |
| 2794 | static void memcg_free_cache_id(int id) |
| 2795 | { |
| 2796 | ida_simple_remove(&kmem_limited_groups, id); |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | /* |
| 2800 | * We should update the current array size iff all caches updates succeed. This |
| 2801 | * can only be done from the slab side. The slab mutex needs to be held when |
| 2802 | * calling this. |
| 2803 | */ |
| 2804 | void memcg_update_array_size(int num) |
| 2805 | { |
Vladimir Davydov | f3bb304 | 2014-10-09 15:28:45 -0700 | [diff] [blame] | 2806 | memcg_limited_groups_array_size = num; |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 2807 | } |
| 2808 | |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2809 | static void memcg_register_cache(struct mem_cgroup *memcg, |
| 2810 | struct kmem_cache *root_cache) |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 2811 | { |
Vladimir Davydov | 93f39ee | 2014-06-04 16:08:24 -0700 | [diff] [blame] | 2812 | static char memcg_name_buf[NAME_MAX + 1]; /* protected by |
| 2813 | memcg_slab_mutex */ |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2814 | struct kmem_cache *cachep; |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 2815 | int id; |
| 2816 | |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2817 | lockdep_assert_held(&memcg_slab_mutex); |
| 2818 | |
| 2819 | id = memcg_cache_id(memcg); |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 2820 | |
Vladimir Davydov | 2edefe1 | 2014-01-23 15:53:02 -0800 | [diff] [blame] | 2821 | /* |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2822 | * Since per-memcg caches are created asynchronously on first |
| 2823 | * allocation (see memcg_kmem_get_cache()), several threads can try to |
| 2824 | * create the same cache, but only one of them may succeed. |
Vladimir Davydov | 2edefe1 | 2014-01-23 15:53:02 -0800 | [diff] [blame] | 2825 | */ |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2826 | if (cache_from_memcg_idx(root_cache, id)) |
| 2827 | return; |
Vladimir Davydov | 2edefe1 | 2014-01-23 15:53:02 -0800 | [diff] [blame] | 2828 | |
Vladimir Davydov | 073ee1c | 2014-06-04 16:08:23 -0700 | [diff] [blame] | 2829 | cgroup_name(memcg->css.cgroup, memcg_name_buf, NAME_MAX + 1); |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2830 | cachep = memcg_create_kmem_cache(memcg, root_cache, memcg_name_buf); |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2831 | /* |
| 2832 | * If we could not create a memcg cache, do not complain, because |
| 2833 | * that's not critical at all as we can always proceed with the root |
| 2834 | * cache. |
| 2835 | */ |
| 2836 | if (!cachep) |
| 2837 | return; |
| 2838 | |
Vladimir Davydov | 33a690c | 2014-10-09 15:28:43 -0700 | [diff] [blame] | 2839 | css_get(&memcg->css); |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2840 | list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches); |
Vladimir Davydov | 1aa1325 | 2014-01-23 15:52:58 -0800 | [diff] [blame] | 2841 | |
Vladimir Davydov | 1aa1325 | 2014-01-23 15:52:58 -0800 | [diff] [blame] | 2842 | /* |
Vladimir Davydov | 959c896 | 2014-01-23 15:52:59 -0800 | [diff] [blame] | 2843 | * Since readers won't lock (see cache_from_memcg_idx()), we need a |
| 2844 | * barrier here to ensure nobody will see the kmem_cache partially |
| 2845 | * initialized. |
Vladimir Davydov | 1aa1325 | 2014-01-23 15:52:58 -0800 | [diff] [blame] | 2846 | */ |
Vladimir Davydov | 959c896 | 2014-01-23 15:52:59 -0800 | [diff] [blame] | 2847 | smp_wmb(); |
| 2848 | |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2849 | BUG_ON(root_cache->memcg_params->memcg_caches[id]); |
| 2850 | root_cache->memcg_params->memcg_caches[id] = cachep; |
Vladimir Davydov | 1aa1325 | 2014-01-23 15:52:58 -0800 | [diff] [blame] | 2851 | } |
| 2852 | |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2853 | static void memcg_unregister_cache(struct kmem_cache *cachep) |
Vladimir Davydov | 1aa1325 | 2014-01-23 15:52:58 -0800 | [diff] [blame] | 2854 | { |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2855 | struct kmem_cache *root_cache; |
Vladimir Davydov | 1aa1325 | 2014-01-23 15:52:58 -0800 | [diff] [blame] | 2856 | struct mem_cgroup *memcg; |
| 2857 | int id; |
| 2858 | |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2859 | lockdep_assert_held(&memcg_slab_mutex); |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 2860 | |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2861 | BUG_ON(is_root_cache(cachep)); |
Vladimir Davydov | 2edefe1 | 2014-01-23 15:53:02 -0800 | [diff] [blame] | 2862 | |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2863 | root_cache = cachep->memcg_params->root_cache; |
| 2864 | memcg = cachep->memcg_params->memcg; |
Vladimir Davydov | 96403da | 2014-01-23 15:53:01 -0800 | [diff] [blame] | 2865 | id = memcg_cache_id(memcg); |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 2866 | |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2867 | BUG_ON(root_cache->memcg_params->memcg_caches[id] != cachep); |
| 2868 | root_cache->memcg_params->memcg_caches[id] = NULL; |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 2869 | |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2870 | list_del(&cachep->memcg_params->list); |
| 2871 | |
| 2872 | kmem_cache_destroy(cachep); |
Vladimir Davydov | 33a690c | 2014-10-09 15:28:43 -0700 | [diff] [blame] | 2873 | |
| 2874 | /* drop the reference taken in memcg_register_cache */ |
| 2875 | css_put(&memcg->css); |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 2876 | } |
| 2877 | |
Glauber Costa | 0e9d92f | 2012-12-18 14:22:42 -0800 | [diff] [blame] | 2878 | /* |
| 2879 | * During the creation a new cache, we need to disable our accounting mechanism |
| 2880 | * altogether. This is true even if we are not creating, but rather just |
| 2881 | * enqueing new caches to be created. |
| 2882 | * |
| 2883 | * This is because that process will trigger allocations; some visible, like |
| 2884 | * explicit kmallocs to auxiliary data structures, name strings and internal |
| 2885 | * cache structures; some well concealed, like INIT_WORK() that can allocate |
| 2886 | * objects during debug. |
| 2887 | * |
| 2888 | * If any allocation happens during memcg_kmem_get_cache, we will recurse back |
| 2889 | * to it. This may not be a bounded recursion: since the first cache creation |
| 2890 | * failed to complete (waiting on the allocation), we'll just try to create the |
| 2891 | * cache again, failing at the same point. |
| 2892 | * |
| 2893 | * memcg_kmem_get_cache is prepared to abort after seeing a positive count of |
| 2894 | * memcg_kmem_skip_account. So we enclose anything that might allocate memory |
| 2895 | * inside the following two functions. |
| 2896 | */ |
| 2897 | static inline void memcg_stop_kmem_account(void) |
| 2898 | { |
| 2899 | VM_BUG_ON(!current->mm); |
| 2900 | current->memcg_kmem_skip_account++; |
| 2901 | } |
| 2902 | |
| 2903 | static inline void memcg_resume_kmem_account(void) |
| 2904 | { |
| 2905 | VM_BUG_ON(!current->mm); |
| 2906 | current->memcg_kmem_skip_account--; |
| 2907 | } |
| 2908 | |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2909 | int __memcg_cleanup_cache_params(struct kmem_cache *s) |
Glauber Costa | 7cf2798 | 2012-12-18 14:22:55 -0800 | [diff] [blame] | 2910 | { |
| 2911 | struct kmem_cache *c; |
Vladimir Davydov | b852990 | 2014-04-07 15:39:28 -0700 | [diff] [blame] | 2912 | int i, failed = 0; |
Glauber Costa | 7cf2798 | 2012-12-18 14:22:55 -0800 | [diff] [blame] | 2913 | |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2914 | mutex_lock(&memcg_slab_mutex); |
Qiang Huang | 7a67d7a | 2013-11-12 15:08:24 -0800 | [diff] [blame] | 2915 | for_each_memcg_cache_index(i) { |
| 2916 | c = cache_from_memcg_idx(s, i); |
Glauber Costa | 7cf2798 | 2012-12-18 14:22:55 -0800 | [diff] [blame] | 2917 | if (!c) |
| 2918 | continue; |
| 2919 | |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2920 | memcg_unregister_cache(c); |
Vladimir Davydov | b852990 | 2014-04-07 15:39:28 -0700 | [diff] [blame] | 2921 | |
| 2922 | if (cache_from_memcg_idx(s, i)) |
| 2923 | failed++; |
Glauber Costa | 7cf2798 | 2012-12-18 14:22:55 -0800 | [diff] [blame] | 2924 | } |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2925 | mutex_unlock(&memcg_slab_mutex); |
Vladimir Davydov | b852990 | 2014-04-07 15:39:28 -0700 | [diff] [blame] | 2926 | return failed; |
Glauber Costa | 7cf2798 | 2012-12-18 14:22:55 -0800 | [diff] [blame] | 2927 | } |
| 2928 | |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2929 | static void memcg_unregister_all_caches(struct mem_cgroup *memcg) |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 2930 | { |
| 2931 | struct kmem_cache *cachep; |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2932 | struct memcg_cache_params *params, *tmp; |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 2933 | |
| 2934 | if (!memcg_kmem_is_active(memcg)) |
| 2935 | return; |
| 2936 | |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2937 | mutex_lock(&memcg_slab_mutex); |
| 2938 | list_for_each_entry_safe(params, tmp, &memcg->memcg_slab_caches, list) { |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 2939 | cachep = memcg_params_to_cache(params); |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2940 | kmem_cache_shrink(cachep); |
| 2941 | if (atomic_read(&cachep->memcg_params->nr_pages) == 0) |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2942 | memcg_unregister_cache(cachep); |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 2943 | } |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2944 | mutex_unlock(&memcg_slab_mutex); |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 2945 | } |
| 2946 | |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2947 | struct memcg_register_cache_work { |
Vladimir Davydov | 5722d09 | 2014-04-07 15:39:24 -0700 | [diff] [blame] | 2948 | struct mem_cgroup *memcg; |
| 2949 | struct kmem_cache *cachep; |
| 2950 | struct work_struct work; |
| 2951 | }; |
| 2952 | |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2953 | static void memcg_register_cache_func(struct work_struct *w) |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 2954 | { |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2955 | struct memcg_register_cache_work *cw = |
| 2956 | container_of(w, struct memcg_register_cache_work, work); |
Vladimir Davydov | 5722d09 | 2014-04-07 15:39:24 -0700 | [diff] [blame] | 2957 | struct mem_cgroup *memcg = cw->memcg; |
| 2958 | struct kmem_cache *cachep = cw->cachep; |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 2959 | |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2960 | mutex_lock(&memcg_slab_mutex); |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2961 | memcg_register_cache(memcg, cachep); |
Vladimir Davydov | bd67314 | 2014-06-04 16:07:40 -0700 | [diff] [blame] | 2962 | mutex_unlock(&memcg_slab_mutex); |
| 2963 | |
Vladimir Davydov | 5722d09 | 2014-04-07 15:39:24 -0700 | [diff] [blame] | 2964 | css_put(&memcg->css); |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 2965 | kfree(cw); |
| 2966 | } |
| 2967 | |
| 2968 | /* |
| 2969 | * Enqueue the creation of a per-memcg kmem_cache. |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 2970 | */ |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2971 | static void __memcg_schedule_register_cache(struct mem_cgroup *memcg, |
| 2972 | struct kmem_cache *cachep) |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 2973 | { |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2974 | struct memcg_register_cache_work *cw; |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 2975 | |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2976 | cw = kmalloc(sizeof(*cw), GFP_NOWAIT); |
Li Zefan | ca0dde9 | 2013-04-29 15:08:57 -0700 | [diff] [blame] | 2977 | if (cw == NULL) { |
| 2978 | css_put(&memcg->css); |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 2979 | return; |
| 2980 | } |
| 2981 | |
| 2982 | cw->memcg = memcg; |
| 2983 | cw->cachep = cachep; |
| 2984 | |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2985 | INIT_WORK(&cw->work, memcg_register_cache_func); |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 2986 | schedule_work(&cw->work); |
| 2987 | } |
| 2988 | |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2989 | static void memcg_schedule_register_cache(struct mem_cgroup *memcg, |
| 2990 | struct kmem_cache *cachep) |
Glauber Costa | 0e9d92f | 2012-12-18 14:22:42 -0800 | [diff] [blame] | 2991 | { |
| 2992 | /* |
| 2993 | * We need to stop accounting when we kmalloc, because if the |
| 2994 | * corresponding kmalloc cache is not yet created, the first allocation |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 2995 | * in __memcg_schedule_register_cache will recurse. |
Glauber Costa | 0e9d92f | 2012-12-18 14:22:42 -0800 | [diff] [blame] | 2996 | * |
| 2997 | * However, it is better to enclose the whole function. Depending on |
| 2998 | * the debugging options enabled, INIT_WORK(), for instance, can |
| 2999 | * trigger an allocation. This too, will make us recurse. Because at |
| 3000 | * this point we can't allow ourselves back into memcg_kmem_get_cache, |
| 3001 | * the safest choice is to do it like this, wrapping the whole function. |
| 3002 | */ |
| 3003 | memcg_stop_kmem_account(); |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 3004 | __memcg_schedule_register_cache(memcg, cachep); |
Glauber Costa | 0e9d92f | 2012-12-18 14:22:42 -0800 | [diff] [blame] | 3005 | memcg_resume_kmem_account(); |
| 3006 | } |
Vladimir Davydov | c67a8a6 | 2014-06-04 16:07:39 -0700 | [diff] [blame] | 3007 | |
| 3008 | int __memcg_charge_slab(struct kmem_cache *cachep, gfp_t gfp, int order) |
| 3009 | { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3010 | unsigned int nr_pages = 1 << order; |
Vladimir Davydov | c67a8a6 | 2014-06-04 16:07:39 -0700 | [diff] [blame] | 3011 | int res; |
| 3012 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3013 | res = memcg_charge_kmem(cachep->memcg_params->memcg, gfp, nr_pages); |
Vladimir Davydov | c67a8a6 | 2014-06-04 16:07:39 -0700 | [diff] [blame] | 3014 | if (!res) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3015 | atomic_add(nr_pages, &cachep->memcg_params->nr_pages); |
Vladimir Davydov | c67a8a6 | 2014-06-04 16:07:39 -0700 | [diff] [blame] | 3016 | return res; |
| 3017 | } |
| 3018 | |
| 3019 | void __memcg_uncharge_slab(struct kmem_cache *cachep, int order) |
| 3020 | { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3021 | unsigned int nr_pages = 1 << order; |
| 3022 | |
| 3023 | memcg_uncharge_kmem(cachep->memcg_params->memcg, nr_pages); |
| 3024 | atomic_sub(nr_pages, &cachep->memcg_params->nr_pages); |
Vladimir Davydov | c67a8a6 | 2014-06-04 16:07:39 -0700 | [diff] [blame] | 3025 | } |
| 3026 | |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3027 | /* |
| 3028 | * Return the kmem_cache we're supposed to use for a slab allocation. |
| 3029 | * We try to use the current memcg's version of the cache. |
| 3030 | * |
| 3031 | * If the cache does not exist yet, if we are the first user of it, |
| 3032 | * we either create it immediately, if possible, or create it asynchronously |
| 3033 | * in a workqueue. |
| 3034 | * In the latter case, we will let the current allocation go through with |
| 3035 | * the original cache. |
| 3036 | * |
| 3037 | * Can't be called in interrupt context or from kernel threads. |
| 3038 | * This function needs to be called with rcu_read_lock() held. |
| 3039 | */ |
| 3040 | struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep, |
| 3041 | gfp_t gfp) |
| 3042 | { |
| 3043 | struct mem_cgroup *memcg; |
Vladimir Davydov | 959c896 | 2014-01-23 15:52:59 -0800 | [diff] [blame] | 3044 | struct kmem_cache *memcg_cachep; |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3045 | |
| 3046 | VM_BUG_ON(!cachep->memcg_params); |
| 3047 | VM_BUG_ON(!cachep->memcg_params->is_root_cache); |
| 3048 | |
Glauber Costa | 0e9d92f | 2012-12-18 14:22:42 -0800 | [diff] [blame] | 3049 | if (!current->mm || current->memcg_kmem_skip_account) |
| 3050 | return cachep; |
| 3051 | |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3052 | rcu_read_lock(); |
| 3053 | memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner)); |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3054 | |
Vladimir Davydov | cf2b8fb | 2014-10-09 15:28:59 -0700 | [diff] [blame] | 3055 | if (!memcg_kmem_is_active(memcg)) |
Li Zefan | ca0dde9 | 2013-04-29 15:08:57 -0700 | [diff] [blame] | 3056 | goto out; |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3057 | |
Vladimir Davydov | 959c896 | 2014-01-23 15:52:59 -0800 | [diff] [blame] | 3058 | memcg_cachep = cache_from_memcg_idx(cachep, memcg_cache_id(memcg)); |
| 3059 | if (likely(memcg_cachep)) { |
| 3060 | cachep = memcg_cachep; |
Li Zefan | ca0dde9 | 2013-04-29 15:08:57 -0700 | [diff] [blame] | 3061 | goto out; |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3062 | } |
| 3063 | |
Li Zefan | ca0dde9 | 2013-04-29 15:08:57 -0700 | [diff] [blame] | 3064 | /* The corresponding put will be done in the workqueue. */ |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 3065 | if (!css_tryget_online(&memcg->css)) |
Li Zefan | ca0dde9 | 2013-04-29 15:08:57 -0700 | [diff] [blame] | 3066 | goto out; |
| 3067 | rcu_read_unlock(); |
| 3068 | |
| 3069 | /* |
| 3070 | * If we are in a safe context (can wait, and not in interrupt |
| 3071 | * context), we could be be predictable and return right away. |
| 3072 | * This would guarantee that the allocation being performed |
| 3073 | * already belongs in the new cache. |
| 3074 | * |
| 3075 | * However, there are some clashes that can arrive from locking. |
| 3076 | * For instance, because we acquire the slab_mutex while doing |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 3077 | * memcg_create_kmem_cache, this means no further allocation |
| 3078 | * could happen with the slab_mutex held. So it's better to |
| 3079 | * defer everything. |
Li Zefan | ca0dde9 | 2013-04-29 15:08:57 -0700 | [diff] [blame] | 3080 | */ |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 3081 | memcg_schedule_register_cache(memcg, cachep); |
Li Zefan | ca0dde9 | 2013-04-29 15:08:57 -0700 | [diff] [blame] | 3082 | return cachep; |
| 3083 | out: |
| 3084 | rcu_read_unlock(); |
| 3085 | return cachep; |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3086 | } |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 3087 | |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3088 | /* |
| 3089 | * We need to verify if the allocation against current->mm->owner's memcg is |
| 3090 | * possible for the given order. But the page is not allocated yet, so we'll |
| 3091 | * need a further commit step to do the final arrangements. |
| 3092 | * |
| 3093 | * It is possible for the task to switch cgroups in this mean time, so at |
| 3094 | * commit time, we can't rely on task conversion any longer. We'll then use |
| 3095 | * the handle argument to return to the caller which cgroup we should commit |
| 3096 | * against. We could also return the memcg directly and avoid the pointer |
| 3097 | * passing, but a boolean return value gives better semantics considering |
| 3098 | * the compiled-out case as well. |
| 3099 | * |
| 3100 | * Returning true means the allocation is possible. |
| 3101 | */ |
| 3102 | bool |
| 3103 | __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order) |
| 3104 | { |
| 3105 | struct mem_cgroup *memcg; |
| 3106 | int ret; |
| 3107 | |
| 3108 | *_memcg = NULL; |
Glauber Costa | 6d42c23 | 2013-07-08 16:00:00 -0700 | [diff] [blame] | 3109 | |
| 3110 | /* |
| 3111 | * Disabling accounting is only relevant for some specific memcg |
| 3112 | * internal allocations. Therefore we would initially not have such |
Vladimir Davydov | 5238343 | 2014-06-04 16:06:39 -0700 | [diff] [blame] | 3113 | * check here, since direct calls to the page allocator that are |
| 3114 | * accounted to kmemcg (alloc_kmem_pages and friends) only happen |
| 3115 | * outside memcg core. We are mostly concerned with cache allocations, |
| 3116 | * and by having this test at memcg_kmem_get_cache, we are already able |
| 3117 | * to relay the allocation to the root cache and bypass the memcg cache |
| 3118 | * altogether. |
Glauber Costa | 6d42c23 | 2013-07-08 16:00:00 -0700 | [diff] [blame] | 3119 | * |
| 3120 | * There is one exception, though: the SLUB allocator does not create |
| 3121 | * large order caches, but rather service large kmallocs directly from |
| 3122 | * the page allocator. Therefore, the following sequence when backed by |
| 3123 | * the SLUB allocator: |
| 3124 | * |
Andrew Morton | f894ffa | 2013-09-12 15:13:35 -0700 | [diff] [blame] | 3125 | * memcg_stop_kmem_account(); |
| 3126 | * kmalloc(<large_number>) |
| 3127 | * memcg_resume_kmem_account(); |
Glauber Costa | 6d42c23 | 2013-07-08 16:00:00 -0700 | [diff] [blame] | 3128 | * |
| 3129 | * would effectively ignore the fact that we should skip accounting, |
| 3130 | * since it will drive us directly to this function without passing |
| 3131 | * through the cache selector memcg_kmem_get_cache. Such large |
| 3132 | * allocations are extremely rare but can happen, for instance, for the |
| 3133 | * cache arrays. We bring this test here. |
| 3134 | */ |
| 3135 | if (!current->mm || current->memcg_kmem_skip_account) |
| 3136 | return true; |
| 3137 | |
Johannes Weiner | df38197 | 2014-04-07 15:37:43 -0700 | [diff] [blame] | 3138 | memcg = get_mem_cgroup_from_mm(current->mm); |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3139 | |
Vladimir Davydov | cf2b8fb | 2014-10-09 15:28:59 -0700 | [diff] [blame] | 3140 | if (!memcg_kmem_is_active(memcg)) { |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3141 | css_put(&memcg->css); |
| 3142 | return true; |
| 3143 | } |
| 3144 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3145 | ret = memcg_charge_kmem(memcg, gfp, 1 << order); |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3146 | if (!ret) |
| 3147 | *_memcg = memcg; |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3148 | |
| 3149 | css_put(&memcg->css); |
| 3150 | return (ret == 0); |
| 3151 | } |
| 3152 | |
| 3153 | void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, |
| 3154 | int order) |
| 3155 | { |
| 3156 | struct page_cgroup *pc; |
| 3157 | |
| 3158 | VM_BUG_ON(mem_cgroup_is_root(memcg)); |
| 3159 | |
| 3160 | /* The page allocation failed. Revert */ |
| 3161 | if (!page) { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3162 | memcg_uncharge_kmem(memcg, 1 << order); |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3163 | return; |
| 3164 | } |
Johannes Weiner | a840cda | 2014-08-06 16:06:04 -0700 | [diff] [blame] | 3165 | /* |
| 3166 | * The page is freshly allocated and not visible to any |
| 3167 | * outside callers yet. Set up pc non-atomically. |
| 3168 | */ |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3169 | pc = lookup_page_cgroup(page); |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3170 | pc->mem_cgroup = memcg; |
Johannes Weiner | a840cda | 2014-08-06 16:06:04 -0700 | [diff] [blame] | 3171 | pc->flags = PCG_USED; |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | void __memcg_kmem_uncharge_pages(struct page *page, int order) |
| 3175 | { |
| 3176 | struct mem_cgroup *memcg = NULL; |
| 3177 | struct page_cgroup *pc; |
| 3178 | |
| 3179 | |
| 3180 | pc = lookup_page_cgroup(page); |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3181 | if (!PageCgroupUsed(pc)) |
| 3182 | return; |
| 3183 | |
Johannes Weiner | a840cda | 2014-08-06 16:06:04 -0700 | [diff] [blame] | 3184 | memcg = pc->mem_cgroup; |
| 3185 | pc->flags = 0; |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3186 | |
| 3187 | /* |
| 3188 | * We trust that only if there is a memcg associated with the page, it |
| 3189 | * is a valid allocation |
| 3190 | */ |
| 3191 | if (!memcg) |
| 3192 | return; |
| 3193 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 3194 | VM_BUG_ON_PAGE(mem_cgroup_is_root(memcg), page); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3195 | memcg_uncharge_kmem(memcg, 1 << order); |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3196 | } |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 3197 | #else |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 3198 | static inline void memcg_unregister_all_caches(struct mem_cgroup *memcg) |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 3199 | { |
| 3200 | } |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3201 | #endif /* CONFIG_MEMCG_KMEM */ |
| 3202 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3203 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 3204 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3205 | /* |
| 3206 | * Because tail pages are not marked as "used", set it. We're under |
KAMEZAWA Hiroyuki | e94c8a9 | 2012-01-12 17:18:20 -0800 | [diff] [blame] | 3207 | * zone->lru_lock, 'splitting on pmd' and compound_lock. |
| 3208 | * charge/uncharge will be never happen and move_account() is done under |
| 3209 | * compound_lock(), so we don't have to take care of races. |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3210 | */ |
KAMEZAWA Hiroyuki | e94c8a9 | 2012-01-12 17:18:20 -0800 | [diff] [blame] | 3211 | void mem_cgroup_split_huge_fixup(struct page *head) |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3212 | { |
| 3213 | struct page_cgroup *head_pc = lookup_page_cgroup(head); |
KAMEZAWA Hiroyuki | e94c8a9 | 2012-01-12 17:18:20 -0800 | [diff] [blame] | 3214 | struct page_cgroup *pc; |
David Rientjes | b070e65 | 2013-05-07 16:18:09 -0700 | [diff] [blame] | 3215 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | e94c8a9 | 2012-01-12 17:18:20 -0800 | [diff] [blame] | 3216 | int i; |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3217 | |
KAMEZAWA Hiroyuki | 3d37c4a | 2011-01-25 15:07:28 -0800 | [diff] [blame] | 3218 | if (mem_cgroup_disabled()) |
| 3219 | return; |
David Rientjes | b070e65 | 2013-05-07 16:18:09 -0700 | [diff] [blame] | 3220 | |
| 3221 | memcg = head_pc->mem_cgroup; |
KAMEZAWA Hiroyuki | e94c8a9 | 2012-01-12 17:18:20 -0800 | [diff] [blame] | 3222 | for (i = 1; i < HPAGE_PMD_NR; i++) { |
| 3223 | pc = head_pc + i; |
David Rientjes | b070e65 | 2013-05-07 16:18:09 -0700 | [diff] [blame] | 3224 | pc->mem_cgroup = memcg; |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 3225 | pc->flags = head_pc->flags; |
KAMEZAWA Hiroyuki | e94c8a9 | 2012-01-12 17:18:20 -0800 | [diff] [blame] | 3226 | } |
David Rientjes | b070e65 | 2013-05-07 16:18:09 -0700 | [diff] [blame] | 3227 | __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], |
| 3228 | HPAGE_PMD_NR); |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3229 | } |
Hugh Dickins | 12d2710 | 2012-01-12 17:19:52 -0800 | [diff] [blame] | 3230 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3231 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3232 | /** |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 3233 | * mem_cgroup_move_account - move account of the page |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3234 | * @page: the page |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3235 | * @nr_pages: number of regular pages (>1 for huge pages) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3236 | * @pc: page_cgroup of the page. |
| 3237 | * @from: mem_cgroup which the page is moved from. |
| 3238 | * @to: mem_cgroup which the page is moved to. @from != @to. |
| 3239 | * |
| 3240 | * The caller must confirm following. |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3241 | * - page is not on LRU (isolate_page() is useful.) |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3242 | * - compound_lock is held when nr_pages > 1 |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3243 | * |
KAMEZAWA Hiroyuki | 2f3479b | 2012-05-29 15:07:04 -0700 | [diff] [blame] | 3244 | * This function doesn't do "charge" to new cgroup and doesn't do "uncharge" |
| 3245 | * from old cgroup. |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3246 | */ |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3247 | static int mem_cgroup_move_account(struct page *page, |
| 3248 | unsigned int nr_pages, |
| 3249 | struct page_cgroup *pc, |
| 3250 | struct mem_cgroup *from, |
KAMEZAWA Hiroyuki | 2f3479b | 2012-05-29 15:07:04 -0700 | [diff] [blame] | 3251 | struct mem_cgroup *to) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3252 | { |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 3253 | unsigned long flags; |
| 3254 | int ret; |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 3255 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3256 | VM_BUG_ON(from == to); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 3257 | VM_BUG_ON_PAGE(PageLRU(page), page); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 3258 | /* |
| 3259 | * The page is isolated from LRU. So, collapse function |
| 3260 | * will not handle this page. But page splitting can happen. |
| 3261 | * Do this check under compound_page_lock(). The caller should |
| 3262 | * hold it. |
| 3263 | */ |
| 3264 | ret = -EBUSY; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3265 | if (nr_pages > 1 && !PageTransHuge(page)) |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 3266 | goto out; |
| 3267 | |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 3268 | /* |
| 3269 | * Prevent mem_cgroup_migrate() from looking at pc->mem_cgroup |
| 3270 | * of its source page while we change it: page migration takes |
| 3271 | * both pages off the LRU, but page cache replacement doesn't. |
| 3272 | */ |
| 3273 | if (!trylock_page(page)) |
| 3274 | goto out; |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 3275 | |
| 3276 | ret = -EINVAL; |
| 3277 | if (!PageCgroupUsed(pc) || pc->mem_cgroup != from) |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 3278 | goto out_unlock; |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 3279 | |
KAMEZAWA Hiroyuki | 312734c0 | 2012-03-21 16:34:24 -0700 | [diff] [blame] | 3280 | move_lock_mem_cgroup(from, &flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3281 | |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 3282 | if (!PageAnon(page) && page_mapped(page)) { |
Johannes Weiner | 59d1d25 | 2014-04-07 15:37:40 -0700 | [diff] [blame] | 3283 | __this_cpu_sub(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED], |
| 3284 | nr_pages); |
| 3285 | __this_cpu_add(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED], |
| 3286 | nr_pages); |
| 3287 | } |
Sha Zhengju | 3ea67d0 | 2013-09-12 15:13:53 -0700 | [diff] [blame] | 3288 | |
Johannes Weiner | 59d1d25 | 2014-04-07 15:37:40 -0700 | [diff] [blame] | 3289 | if (PageWriteback(page)) { |
| 3290 | __this_cpu_sub(from->stat->count[MEM_CGROUP_STAT_WRITEBACK], |
| 3291 | nr_pages); |
| 3292 | __this_cpu_add(to->stat->count[MEM_CGROUP_STAT_WRITEBACK], |
| 3293 | nr_pages); |
| 3294 | } |
Sha Zhengju | 3ea67d0 | 2013-09-12 15:13:53 -0700 | [diff] [blame] | 3295 | |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 3296 | /* |
| 3297 | * It is safe to change pc->mem_cgroup here because the page |
| 3298 | * is referenced, charged, and isolated - we can't race with |
| 3299 | * uncharging, charging, migration, or LRU putback. |
| 3300 | */ |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 3301 | |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 3302 | /* caller should have done css_get */ |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3303 | pc->mem_cgroup = to; |
KAMEZAWA Hiroyuki | 312734c0 | 2012-03-21 16:34:24 -0700 | [diff] [blame] | 3304 | move_unlock_mem_cgroup(from, &flags); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 3305 | ret = 0; |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 3306 | |
| 3307 | local_irq_disable(); |
| 3308 | mem_cgroup_charge_statistics(to, page, nr_pages); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3309 | memcg_check_events(to, page); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 3310 | mem_cgroup_charge_statistics(from, page, -nr_pages); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3311 | memcg_check_events(from, page); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 3312 | local_irq_enable(); |
| 3313 | out_unlock: |
| 3314 | unlock_page(page); |
Johannes Weiner | de3638d | 2011-03-23 16:42:28 -0700 | [diff] [blame] | 3315 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3316 | return ret; |
| 3317 | } |
| 3318 | |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3319 | /** |
| 3320 | * mem_cgroup_move_parent - moves page to the parent group |
| 3321 | * @page: the page to move |
| 3322 | * @pc: page_cgroup of the page |
| 3323 | * @child: page's cgroup |
| 3324 | * |
| 3325 | * move charges to its parent or the root cgroup if the group has no |
| 3326 | * parent (aka use_hierarchy==0). |
| 3327 | * Although this might fail (get_page_unless_zero, isolate_lru_page or |
| 3328 | * mem_cgroup_move_account fails) the failure is always temporary and |
| 3329 | * it signals a race with a page removal/uncharge or migration. In the |
| 3330 | * first case the page is on the way out and it will vanish from the LRU |
| 3331 | * on the next attempt and the call should be retried later. |
| 3332 | * Isolation from the LRU fails only if page has been isolated from |
| 3333 | * the LRU since we looked at it and that usually means either global |
| 3334 | * reclaim or migration going on. The page will either get back to the |
| 3335 | * LRU or vanish. |
| 3336 | * Finaly mem_cgroup_move_account fails only if the page got uncharged |
| 3337 | * (!PageCgroupUsed) or moved to a different group. The page will |
| 3338 | * disappear in the next attempt. |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3339 | */ |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3340 | static int mem_cgroup_move_parent(struct page *page, |
| 3341 | struct page_cgroup *pc, |
KAMEZAWA Hiroyuki | 6068bf0 | 2012-07-31 16:42:45 -0700 | [diff] [blame] | 3342 | struct mem_cgroup *child) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3343 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3344 | struct mem_cgroup *parent; |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3345 | unsigned int nr_pages; |
Andrew Morton | 4be4489 | 2011-03-23 16:42:39 -0700 | [diff] [blame] | 3346 | unsigned long uninitialized_var(flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3347 | int ret; |
| 3348 | |
Michal Hocko | d842301 | 2012-10-26 13:37:29 +0200 | [diff] [blame] | 3349 | VM_BUG_ON(mem_cgroup_is_root(child)); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3350 | |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 3351 | ret = -EBUSY; |
| 3352 | if (!get_page_unless_zero(page)) |
| 3353 | goto out; |
| 3354 | if (isolate_lru_page(page)) |
| 3355 | goto put; |
KAMEZAWA Hiroyuki | 52dbb90 | 2011-01-25 15:07:29 -0800 | [diff] [blame] | 3356 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3357 | nr_pages = hpage_nr_pages(page); |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3358 | |
KAMEZAWA Hiroyuki | cc926f7 | 2012-05-29 15:07:04 -0700 | [diff] [blame] | 3359 | parent = parent_mem_cgroup(child); |
| 3360 | /* |
| 3361 | * If no parent, move charges to root cgroup. |
| 3362 | */ |
| 3363 | if (!parent) |
| 3364 | parent = root_mem_cgroup; |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3365 | |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3366 | if (nr_pages > 1) { |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 3367 | VM_BUG_ON_PAGE(!PageTransHuge(page), page); |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 3368 | flags = compound_lock_irqsave(page); |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3369 | } |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 3370 | |
KAMEZAWA Hiroyuki | cc926f7 | 2012-05-29 15:07:04 -0700 | [diff] [blame] | 3371 | ret = mem_cgroup_move_account(page, nr_pages, |
KAMEZAWA Hiroyuki | 2f3479b | 2012-05-29 15:07:04 -0700 | [diff] [blame] | 3372 | pc, child, parent); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3373 | if (!ret) { |
Johannes Weiner | e8ea14c | 2014-12-10 15:42:42 -0800 | [diff] [blame] | 3374 | if (!mem_cgroup_is_root(parent)) |
| 3375 | css_get_many(&parent->css, nr_pages); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3376 | /* Take charge off the local counters */ |
| 3377 | page_counter_cancel(&child->memory, nr_pages); |
| 3378 | if (do_swap_account) |
| 3379 | page_counter_cancel(&child->memsw, nr_pages); |
Johannes Weiner | e8ea14c | 2014-12-10 15:42:42 -0800 | [diff] [blame] | 3380 | css_put_many(&child->css, nr_pages); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3381 | } |
Jesper Juhl | 8dba474 | 2011-01-25 15:07:24 -0800 | [diff] [blame] | 3382 | |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 3383 | if (nr_pages > 1) |
KAMEZAWA Hiroyuki | 987eba6 | 2011-01-20 14:44:25 -0800 | [diff] [blame] | 3384 | compound_unlock_irqrestore(page, flags); |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3385 | putback_lru_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 3386 | put: |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 3387 | put_page(page); |
Daisuke Nishimura | 57f9fd7d | 2009-12-15 16:47:11 -0800 | [diff] [blame] | 3388 | out: |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3389 | return ret; |
| 3390 | } |
| 3391 | |
Andrew Morton | c255a45 | 2012-07-31 16:43:02 -0700 | [diff] [blame] | 3392 | #ifdef CONFIG_MEMCG_SWAP |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 3393 | static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg, |
| 3394 | bool charge) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3395 | { |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 3396 | int val = (charge) ? 1 : -1; |
| 3397 | this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3398 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3399 | |
| 3400 | /** |
| 3401 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. |
| 3402 | * @entry: swap entry to be moved |
| 3403 | * @from: mem_cgroup which the entry is moved from |
| 3404 | * @to: mem_cgroup which the entry is moved to |
| 3405 | * |
| 3406 | * It succeeds only when the swap_cgroup's record for this entry is the same |
| 3407 | * as the mem_cgroup's id of @from. |
| 3408 | * |
| 3409 | * Returns 0 on success, -EINVAL on failure. |
| 3410 | * |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3411 | * The caller must have charged to @to, IOW, called page_counter_charge() about |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3412 | * both res and memsw, and called css_get(). |
| 3413 | */ |
| 3414 | static int mem_cgroup_move_swap_account(swp_entry_t entry, |
Hugh Dickins | e91cbb4 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 3415 | struct mem_cgroup *from, struct mem_cgroup *to) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3416 | { |
| 3417 | unsigned short old_id, new_id; |
| 3418 | |
Li Zefan | 34c00c3 | 2013-09-23 16:56:01 +0800 | [diff] [blame] | 3419 | old_id = mem_cgroup_id(from); |
| 3420 | new_id = mem_cgroup_id(to); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3421 | |
| 3422 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3423 | mem_cgroup_swap_statistics(from, false); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3424 | mem_cgroup_swap_statistics(to, true); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3425 | /* |
| 3426 | * This function is only called from task migration context now. |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3427 | * It postpones page_counter and refcount handling till the end |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3428 | * of task migration(mem_cgroup_clear_mc()) for performance |
Li Zefan | 4050377 | 2013-07-08 16:00:34 -0700 | [diff] [blame] | 3429 | * improvement. But we cannot postpone css_get(to) because if |
| 3430 | * the process that has been moved to @to does swap-in, the |
| 3431 | * refcount of @to might be decreased to 0. |
| 3432 | * |
| 3433 | * We are in attach() phase, so the cgroup is guaranteed to be |
| 3434 | * alive, so we can just call css_get(). |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 3435 | */ |
Li Zefan | 4050377 | 2013-07-08 16:00:34 -0700 | [diff] [blame] | 3436 | css_get(&to->css); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3437 | return 0; |
| 3438 | } |
| 3439 | return -EINVAL; |
| 3440 | } |
| 3441 | #else |
| 3442 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, |
Hugh Dickins | e91cbb4 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 3443 | struct mem_cgroup *from, struct mem_cgroup *to) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3444 | { |
| 3445 | return -EINVAL; |
| 3446 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3447 | #endif |
| 3448 | |
Daisuke Nishimura | f212ad7 | 2011-03-23 16:42:25 -0700 | [diff] [blame] | 3449 | #ifdef CONFIG_DEBUG_VM |
| 3450 | static struct page_cgroup *lookup_page_cgroup_used(struct page *page) |
| 3451 | { |
| 3452 | struct page_cgroup *pc; |
| 3453 | |
| 3454 | pc = lookup_page_cgroup(page); |
Johannes Weiner | cfa4494 | 2012-01-12 17:18:38 -0800 | [diff] [blame] | 3455 | /* |
| 3456 | * Can be NULL while feeding pages into the page allocator for |
| 3457 | * the first time, i.e. during boot or memory hotplug; |
| 3458 | * or when mem_cgroup_disabled(). |
| 3459 | */ |
Daisuke Nishimura | f212ad7 | 2011-03-23 16:42:25 -0700 | [diff] [blame] | 3460 | if (likely(pc) && PageCgroupUsed(pc)) |
| 3461 | return pc; |
| 3462 | return NULL; |
| 3463 | } |
| 3464 | |
| 3465 | bool mem_cgroup_bad_page_check(struct page *page) |
| 3466 | { |
| 3467 | if (mem_cgroup_disabled()) |
| 3468 | return false; |
| 3469 | |
| 3470 | return lookup_page_cgroup_used(page) != NULL; |
| 3471 | } |
| 3472 | |
| 3473 | void mem_cgroup_print_bad_page(struct page *page) |
| 3474 | { |
| 3475 | struct page_cgroup *pc; |
| 3476 | |
| 3477 | pc = lookup_page_cgroup_used(page); |
| 3478 | if (pc) { |
Andrew Morton | d045197 | 2013-02-22 16:32:06 -0800 | [diff] [blame] | 3479 | pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n", |
| 3480 | pc, pc->flags, pc->mem_cgroup); |
Daisuke Nishimura | f212ad7 | 2011-03-23 16:42:25 -0700 | [diff] [blame] | 3481 | } |
| 3482 | } |
| 3483 | #endif |
| 3484 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3485 | static DEFINE_MUTEX(memcg_limit_mutex); |
| 3486 | |
KOSAKI Motohiro | d38d2a7 | 2009-01-06 14:39:44 -0800 | [diff] [blame] | 3487 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3488 | unsigned long limit) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3489 | { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3490 | unsigned long curusage; |
| 3491 | unsigned long oldusage; |
| 3492 | bool enlarge = false; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3493 | int retry_count; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3494 | int ret; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3495 | |
| 3496 | /* |
| 3497 | * For keeping hierarchical_reclaim simple, how long we should retry |
| 3498 | * is depends on callers. We set our retry-count to be function |
| 3499 | * of # of children which we should visit in this loop. |
| 3500 | */ |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3501 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * |
| 3502 | mem_cgroup_count_children(memcg); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3503 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3504 | oldusage = page_counter_read(&memcg->memory); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3505 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3506 | do { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3507 | if (signal_pending(current)) { |
| 3508 | ret = -EINTR; |
| 3509 | break; |
| 3510 | } |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3511 | |
| 3512 | mutex_lock(&memcg_limit_mutex); |
| 3513 | if (limit > memcg->memsw.limit) { |
| 3514 | mutex_unlock(&memcg_limit_mutex); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3515 | ret = -EINVAL; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3516 | break; |
| 3517 | } |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3518 | if (limit > memcg->memory.limit) |
| 3519 | enlarge = true; |
| 3520 | ret = page_counter_limit(&memcg->memory, limit); |
| 3521 | mutex_unlock(&memcg_limit_mutex); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3522 | |
| 3523 | if (!ret) |
| 3524 | break; |
| 3525 | |
Johannes Weiner | b70a2a2 | 2014-10-09 15:28:56 -0700 | [diff] [blame] | 3526 | try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, true); |
| 3527 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3528 | curusage = page_counter_read(&memcg->memory); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3529 | /* Usage is reduced ? */ |
Andrew Morton | f894ffa | 2013-09-12 15:13:35 -0700 | [diff] [blame] | 3530 | if (curusage >= oldusage) |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3531 | retry_count--; |
| 3532 | else |
| 3533 | oldusage = curusage; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3534 | } while (retry_count); |
| 3535 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3536 | if (!ret && enlarge) |
| 3537 | memcg_oom_recover(memcg); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 3538 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3539 | return ret; |
| 3540 | } |
| 3541 | |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 3542 | static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3543 | unsigned long limit) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3544 | { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3545 | unsigned long curusage; |
| 3546 | unsigned long oldusage; |
| 3547 | bool enlarge = false; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3548 | int retry_count; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3549 | int ret; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3550 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3551 | /* see mem_cgroup_resize_res_limit */ |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3552 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * |
| 3553 | mem_cgroup_count_children(memcg); |
| 3554 | |
| 3555 | oldusage = page_counter_read(&memcg->memsw); |
| 3556 | |
| 3557 | do { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3558 | if (signal_pending(current)) { |
| 3559 | ret = -EINTR; |
| 3560 | break; |
| 3561 | } |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3562 | |
| 3563 | mutex_lock(&memcg_limit_mutex); |
| 3564 | if (limit < memcg->memory.limit) { |
| 3565 | mutex_unlock(&memcg_limit_mutex); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3566 | ret = -EINVAL; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3567 | break; |
| 3568 | } |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3569 | if (limit > memcg->memsw.limit) |
| 3570 | enlarge = true; |
| 3571 | ret = page_counter_limit(&memcg->memsw, limit); |
| 3572 | mutex_unlock(&memcg_limit_mutex); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3573 | |
| 3574 | if (!ret) |
| 3575 | break; |
| 3576 | |
Johannes Weiner | b70a2a2 | 2014-10-09 15:28:56 -0700 | [diff] [blame] | 3577 | try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, false); |
| 3578 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3579 | curusage = page_counter_read(&memcg->memsw); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3580 | /* Usage is reduced ? */ |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3581 | if (curusage >= oldusage) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3582 | retry_count--; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3583 | else |
| 3584 | oldusage = curusage; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3585 | } while (retry_count); |
| 3586 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3587 | if (!ret && enlarge) |
| 3588 | memcg_oom_recover(memcg); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3589 | |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3590 | return ret; |
| 3591 | } |
| 3592 | |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 3593 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
| 3594 | gfp_t gfp_mask, |
| 3595 | unsigned long *total_scanned) |
| 3596 | { |
| 3597 | unsigned long nr_reclaimed = 0; |
| 3598 | struct mem_cgroup_per_zone *mz, *next_mz = NULL; |
| 3599 | unsigned long reclaimed; |
| 3600 | int loop = 0; |
| 3601 | struct mem_cgroup_tree_per_zone *mctz; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3602 | unsigned long excess; |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 3603 | unsigned long nr_scanned; |
| 3604 | |
| 3605 | if (order > 0) |
| 3606 | return 0; |
| 3607 | |
| 3608 | mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone)); |
| 3609 | /* |
| 3610 | * This loop can run a while, specially if mem_cgroup's continuously |
| 3611 | * keep exceeding their soft limit and putting the system under |
| 3612 | * pressure |
| 3613 | */ |
| 3614 | do { |
| 3615 | if (next_mz) |
| 3616 | mz = next_mz; |
| 3617 | else |
| 3618 | mz = mem_cgroup_largest_soft_limit_node(mctz); |
| 3619 | if (!mz) |
| 3620 | break; |
| 3621 | |
| 3622 | nr_scanned = 0; |
| 3623 | reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone, |
| 3624 | gfp_mask, &nr_scanned); |
| 3625 | nr_reclaimed += reclaimed; |
| 3626 | *total_scanned += nr_scanned; |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 3627 | spin_lock_irq(&mctz->lock); |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 3628 | |
| 3629 | /* |
| 3630 | * If we failed to reclaim anything from this memory cgroup |
| 3631 | * it is time to move on to the next cgroup |
| 3632 | */ |
| 3633 | next_mz = NULL; |
| 3634 | if (!reclaimed) { |
| 3635 | do { |
| 3636 | /* |
| 3637 | * Loop until we find yet another one. |
| 3638 | * |
| 3639 | * By the time we get the soft_limit lock |
| 3640 | * again, someone might have aded the |
| 3641 | * group back on the RB tree. Iterate to |
| 3642 | * make sure we get a different mem. |
| 3643 | * mem_cgroup_largest_soft_limit_node returns |
| 3644 | * NULL if no other cgroup is present on |
| 3645 | * the tree |
| 3646 | */ |
| 3647 | next_mz = |
| 3648 | __mem_cgroup_largest_soft_limit_node(mctz); |
| 3649 | if (next_mz == mz) |
| 3650 | css_put(&next_mz->memcg->css); |
| 3651 | else /* next_mz == NULL or other memcg */ |
| 3652 | break; |
| 3653 | } while (1); |
| 3654 | } |
Johannes Weiner | cf2c812 | 2014-06-06 14:38:21 -0700 | [diff] [blame] | 3655 | __mem_cgroup_remove_exceeded(mz, mctz); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3656 | excess = soft_limit_excess(mz->memcg); |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 3657 | /* |
| 3658 | * One school of thought says that we should not add |
| 3659 | * back the node to the tree if reclaim returns 0. |
| 3660 | * But our reclaim could return 0, simply because due |
| 3661 | * to priority we are exposing a smaller subset of |
| 3662 | * memory to reclaim from. Consider this as a longer |
| 3663 | * term TODO. |
| 3664 | */ |
| 3665 | /* If excess == 0, no tree ops */ |
Johannes Weiner | cf2c812 | 2014-06-06 14:38:21 -0700 | [diff] [blame] | 3666 | __mem_cgroup_insert_exceeded(mz, mctz, excess); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 3667 | spin_unlock_irq(&mctz->lock); |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 3668 | css_put(&mz->memcg->css); |
| 3669 | loop++; |
| 3670 | /* |
| 3671 | * Could not reclaim anything and there are no more |
| 3672 | * mem cgroups to try or we seem to be looping without |
| 3673 | * reclaiming anything. |
| 3674 | */ |
| 3675 | if (!nr_reclaimed && |
| 3676 | (next_mz == NULL || |
| 3677 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) |
| 3678 | break; |
| 3679 | } while (!nr_reclaimed); |
| 3680 | if (next_mz) |
| 3681 | css_put(&next_mz->memcg->css); |
| 3682 | return nr_reclaimed; |
| 3683 | } |
| 3684 | |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3685 | /** |
| 3686 | * mem_cgroup_force_empty_list - clears LRU of a group |
| 3687 | * @memcg: group to clear |
| 3688 | * @node: NUMA node |
| 3689 | * @zid: zone id |
| 3690 | * @lru: lru to to clear |
| 3691 | * |
KAMEZAWA Hiroyuki | 3c935d1 | 2012-07-31 16:42:46 -0700 | [diff] [blame] | 3692 | * Traverse a specified page_cgroup list and try to drop them all. This doesn't |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3693 | * reclaim the pages page themselves - pages are moved to the parent (or root) |
| 3694 | * group. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3695 | */ |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3696 | static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3697 | int node, int zid, enum lru_list lru) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3698 | { |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 3699 | struct lruvec *lruvec; |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3700 | unsigned long flags; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3701 | struct list_head *list; |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 3702 | struct page *busy; |
| 3703 | struct zone *zone; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 3704 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3705 | zone = &NODE_DATA(node)->node_zones[zid]; |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 3706 | lruvec = mem_cgroup_zone_lruvec(zone, memcg); |
| 3707 | list = &lruvec->lists[lru]; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3708 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3709 | busy = NULL; |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3710 | do { |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 3711 | struct page_cgroup *pc; |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3712 | struct page *page; |
| 3713 | |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3714 | spin_lock_irqsave(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3715 | if (list_empty(list)) { |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3716 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3717 | break; |
| 3718 | } |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 3719 | page = list_entry(list->prev, struct page, lru); |
| 3720 | if (busy == page) { |
| 3721 | list_move(&page->lru, list); |
Thiago Farina | 648bcc7 | 2010-03-05 13:42:04 -0800 | [diff] [blame] | 3722 | busy = NULL; |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3723 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3724 | continue; |
| 3725 | } |
KAMEZAWA Hiroyuki | 08e552c6 | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 3726 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3727 | |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 3728 | pc = lookup_page_cgroup(page); |
Johannes Weiner | 5564e88 | 2011-03-23 16:42:29 -0700 | [diff] [blame] | 3729 | |
KAMEZAWA Hiroyuki | 3c935d1 | 2012-07-31 16:42:46 -0700 | [diff] [blame] | 3730 | if (mem_cgroup_move_parent(page, pc, memcg)) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3731 | /* found lock contention or "pc" is obsolete. */ |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 3732 | busy = page; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3733 | } else |
| 3734 | busy = NULL; |
Hugh Dickins | 2a7a0e0 | 2014-06-04 16:11:04 -0700 | [diff] [blame] | 3735 | cond_resched(); |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3736 | } while (!list_empty(list)); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | /* |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 3740 | * make mem_cgroup's charge to be 0 if there is no task by moving |
| 3741 | * all the charges and pages to the parent. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3742 | * This enables deleting this mem_cgroup. |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 3743 | * |
| 3744 | * Caller is responsible for holding css reference on the memcg. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3745 | */ |
Michal Hocko | ab5196c | 2012-10-26 13:37:32 +0200 | [diff] [blame] | 3746 | static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3747 | { |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 3748 | int node, zid; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 3749 | |
Daisuke Nishimura | fce6647 | 2010-01-15 17:01:30 -0800 | [diff] [blame] | 3750 | do { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3751 | /* This is for making all *used* pages to be on LRU. */ |
| 3752 | lru_add_drain_all(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3753 | drain_all_stock_sync(memcg); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3754 | mem_cgroup_start_move(memcg); |
Lai Jiangshan | 31aaea4 | 2012-12-12 13:51:27 -0800 | [diff] [blame] | 3755 | for_each_node_state(node, N_MEMORY) { |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3756 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
Hugh Dickins | f156ab9 | 2012-03-21 16:34:19 -0700 | [diff] [blame] | 3757 | enum lru_list lru; |
| 3758 | for_each_lru(lru) { |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3759 | mem_cgroup_force_empty_list(memcg, |
Hugh Dickins | f156ab9 | 2012-03-21 16:34:19 -0700 | [diff] [blame] | 3760 | node, zid, lru); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3761 | } |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 3762 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3763 | } |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3764 | mem_cgroup_end_move(memcg); |
| 3765 | memcg_oom_recover(memcg); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 3766 | cond_resched(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3767 | |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3768 | /* |
Glauber Costa | bea207c | 2012-12-18 14:22:11 -0800 | [diff] [blame] | 3769 | * Kernel memory may not necessarily be trackable to a specific |
| 3770 | * process. So they are not migrated, and therefore we can't |
| 3771 | * expect their value to drop to 0 here. |
| 3772 | * Having res filled up with kmem only is enough. |
| 3773 | * |
Michal Hocko | 2ef37d3 | 2012-10-26 13:37:30 +0200 | [diff] [blame] | 3774 | * This is a safety check because mem_cgroup_force_empty_list |
| 3775 | * could have raced with mem_cgroup_replace_page_cache callers |
| 3776 | * so the lru seemed empty but the page could have been added |
| 3777 | * right after the check. RES_USAGE should be safe as we always |
| 3778 | * charge before adding to the LRU. |
| 3779 | */ |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3780 | } while (page_counter_read(&memcg->memory) - |
| 3781 | page_counter_read(&memcg->kmem) > 0); |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 3782 | } |
| 3783 | |
Tejun Heo | ea280e7 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3784 | /* |
| 3785 | * Test whether @memcg has children, dead or alive. Note that this |
| 3786 | * function doesn't care whether @memcg has use_hierarchy enabled and |
| 3787 | * returns %true if there are child csses according to the cgroup |
| 3788 | * hierarchy. Testing use_hierarchy is the caller's responsiblity. |
| 3789 | */ |
Glauber Costa | b5f99b5 | 2013-02-22 16:34:53 -0800 | [diff] [blame] | 3790 | static inline bool memcg_has_children(struct mem_cgroup *memcg) |
| 3791 | { |
Tejun Heo | ea280e7 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3792 | bool ret; |
| 3793 | |
Johannes Weiner | 696ac17 | 2013-10-31 16:34:15 -0700 | [diff] [blame] | 3794 | /* |
Tejun Heo | ea280e7 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3795 | * The lock does not prevent addition or deletion of children, but |
| 3796 | * it prevents a new child from being initialized based on this |
| 3797 | * parent in css_online(), so it's enough to decide whether |
| 3798 | * hierarchically inherited attributes can still be changed or not. |
Johannes Weiner | 696ac17 | 2013-10-31 16:34:15 -0700 | [diff] [blame] | 3799 | */ |
Tejun Heo | ea280e7 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3800 | lockdep_assert_held(&memcg_create_mutex); |
| 3801 | |
| 3802 | rcu_read_lock(); |
| 3803 | ret = css_next_child(NULL, &memcg->css); |
| 3804 | rcu_read_unlock(); |
| 3805 | return ret; |
Glauber Costa | b5f99b5 | 2013-02-22 16:34:53 -0800 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | /* |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 3809 | * Reclaims as many pages from the given memcg as possible and moves |
| 3810 | * the rest to the parent. |
| 3811 | * |
| 3812 | * Caller is responsible for holding css reference for memcg. |
| 3813 | */ |
| 3814 | static int mem_cgroup_force_empty(struct mem_cgroup *memcg) |
| 3815 | { |
| 3816 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 3817 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3818 | /* we call try-to-free pages for make this cgroup empty */ |
| 3819 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3820 | /* try to free all pages in this cgroup */ |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3821 | while (nr_retries && page_counter_read(&memcg->memory)) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3822 | int progress; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3823 | |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 3824 | if (signal_pending(current)) |
| 3825 | return -EINTR; |
| 3826 | |
Johannes Weiner | b70a2a2 | 2014-10-09 15:28:56 -0700 | [diff] [blame] | 3827 | progress = try_to_free_mem_cgroup_pages(memcg, 1, |
| 3828 | GFP_KERNEL, true); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3829 | if (!progress) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3830 | nr_retries--; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3831 | /* maybe some writeback is necessary */ |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 3832 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3833 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3834 | |
| 3835 | } |
Michal Hocko | ab5196c | 2012-10-26 13:37:32 +0200 | [diff] [blame] | 3836 | |
| 3837 | return 0; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3838 | } |
| 3839 | |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3840 | static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of, |
| 3841 | char *buf, size_t nbytes, |
| 3842 | loff_t off) |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3843 | { |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3844 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 3845 | |
Michal Hocko | d842301 | 2012-10-26 13:37:29 +0200 | [diff] [blame] | 3846 | if (mem_cgroup_is_root(memcg)) |
| 3847 | return -EINVAL; |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3848 | return mem_cgroup_force_empty(memcg) ?: nbytes; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3849 | } |
| 3850 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3851 | static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css, |
| 3852 | struct cftype *cft) |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3853 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3854 | return mem_cgroup_from_css(css)->use_hierarchy; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3855 | } |
| 3856 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3857 | static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css, |
| 3858 | struct cftype *cft, u64 val) |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3859 | { |
| 3860 | int retval = 0; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3861 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3862 | struct mem_cgroup *parent_memcg = mem_cgroup_from_css(memcg->css.parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3863 | |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 3864 | mutex_lock(&memcg_create_mutex); |
Glauber Costa | 567fb43 | 2012-07-31 16:43:07 -0700 | [diff] [blame] | 3865 | |
| 3866 | if (memcg->use_hierarchy == val) |
| 3867 | goto out; |
| 3868 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3869 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 3870 | * 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] | 3871 | * in the child subtrees. If it is unset, then the change can |
| 3872 | * occur, provided the current cgroup has no children. |
| 3873 | * |
| 3874 | * For the root cgroup, parent_mem is NULL, we allow value to be |
| 3875 | * set if there are no children. |
| 3876 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3877 | if ((!parent_memcg || !parent_memcg->use_hierarchy) && |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3878 | (val == 1 || val == 0)) { |
Tejun Heo | ea280e7 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3879 | if (!memcg_has_children(memcg)) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 3880 | memcg->use_hierarchy = val; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3881 | else |
| 3882 | retval = -EBUSY; |
| 3883 | } else |
| 3884 | retval = -EINVAL; |
Glauber Costa | 567fb43 | 2012-07-31 16:43:07 -0700 | [diff] [blame] | 3885 | |
| 3886 | out: |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 3887 | mutex_unlock(&memcg_create_mutex); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3888 | |
| 3889 | return retval; |
| 3890 | } |
| 3891 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3892 | static unsigned long tree_stat(struct mem_cgroup *memcg, |
| 3893 | enum mem_cgroup_stat_index idx) |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 3894 | { |
| 3895 | struct mem_cgroup *iter; |
| 3896 | long val = 0; |
| 3897 | |
| 3898 | /* Per-cpu values can be negative, use a signed accumulator */ |
| 3899 | for_each_mem_cgroup_tree(iter, memcg) |
| 3900 | val += mem_cgroup_read_stat(iter, idx); |
| 3901 | |
| 3902 | if (val < 0) /* race ? */ |
| 3903 | val = 0; |
| 3904 | return val; |
| 3905 | } |
| 3906 | |
| 3907 | static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap) |
| 3908 | { |
| 3909 | u64 val; |
| 3910 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3911 | if (mem_cgroup_is_root(memcg)) { |
| 3912 | val = tree_stat(memcg, MEM_CGROUP_STAT_CACHE); |
| 3913 | val += tree_stat(memcg, MEM_CGROUP_STAT_RSS); |
| 3914 | if (swap) |
| 3915 | val += tree_stat(memcg, MEM_CGROUP_STAT_SWAP); |
| 3916 | } else { |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 3917 | if (!swap) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3918 | val = page_counter_read(&memcg->memory); |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 3919 | else |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3920 | val = page_counter_read(&memcg->memsw); |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 3921 | } |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 3922 | return val << PAGE_SHIFT; |
| 3923 | } |
| 3924 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3925 | enum { |
| 3926 | RES_USAGE, |
| 3927 | RES_LIMIT, |
| 3928 | RES_MAX_USAGE, |
| 3929 | RES_FAILCNT, |
| 3930 | RES_SOFT_LIMIT, |
| 3931 | }; |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 3932 | |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 3933 | static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css, |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 3934 | struct cftype *cft) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3935 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3936 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3937 | struct page_counter *counter; |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3938 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3939 | switch (MEMFILE_TYPE(cft->private)) { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3940 | case _MEM: |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3941 | counter = &memcg->memory; |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 3942 | break; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3943 | case _MEMSWAP: |
| 3944 | counter = &memcg->memsw; |
| 3945 | break; |
| 3946 | case _KMEM: |
| 3947 | counter = &memcg->kmem; |
| 3948 | break; |
| 3949 | default: |
| 3950 | BUG(); |
| 3951 | } |
| 3952 | |
| 3953 | switch (MEMFILE_ATTR(cft->private)) { |
| 3954 | case RES_USAGE: |
| 3955 | if (counter == &memcg->memory) |
| 3956 | return mem_cgroup_usage(memcg, false); |
| 3957 | if (counter == &memcg->memsw) |
| 3958 | return mem_cgroup_usage(memcg, true); |
| 3959 | return (u64)page_counter_read(counter) * PAGE_SIZE; |
| 3960 | case RES_LIMIT: |
| 3961 | return (u64)counter->limit * PAGE_SIZE; |
| 3962 | case RES_MAX_USAGE: |
| 3963 | return (u64)counter->watermark * PAGE_SIZE; |
| 3964 | case RES_FAILCNT: |
| 3965 | return counter->failcnt; |
| 3966 | case RES_SOFT_LIMIT: |
| 3967 | return (u64)memcg->soft_limit * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3968 | default: |
| 3969 | BUG(); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3970 | } |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3971 | } |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 3972 | |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 3973 | #ifdef CONFIG_MEMCG_KMEM |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 3974 | /* should be called with activate_kmem_mutex held */ |
| 3975 | static int __memcg_activate_kmem(struct mem_cgroup *memcg, |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3976 | unsigned long nr_pages) |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 3977 | { |
| 3978 | int err = 0; |
| 3979 | int memcg_id; |
| 3980 | |
| 3981 | if (memcg_kmem_is_active(memcg)) |
| 3982 | return 0; |
| 3983 | |
| 3984 | /* |
| 3985 | * We are going to allocate memory for data shared by all memory |
| 3986 | * cgroups so let's stop accounting here. |
| 3987 | */ |
| 3988 | memcg_stop_kmem_account(); |
| 3989 | |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 3990 | /* |
| 3991 | * For simplicity, we won't allow this to be disabled. It also can't |
| 3992 | * be changed if the cgroup has children already, or if tasks had |
| 3993 | * already joined. |
| 3994 | * |
| 3995 | * If tasks join before we set the limit, a person looking at |
| 3996 | * kmem.usage_in_bytes will have no way to determine when it took |
| 3997 | * place, which makes the value quite meaningless. |
| 3998 | * |
| 3999 | * After it first became limited, changes in the value of the limit are |
| 4000 | * of course permitted. |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 4001 | */ |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 4002 | mutex_lock(&memcg_create_mutex); |
Tejun Heo | ea280e7 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4003 | if (cgroup_has_tasks(memcg->css.cgroup) || |
| 4004 | (memcg->use_hierarchy && memcg_has_children(memcg))) |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4005 | err = -EBUSY; |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 4006 | mutex_unlock(&memcg_create_mutex); |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4007 | if (err) |
| 4008 | goto out; |
| 4009 | |
Vladimir Davydov | f3bb304 | 2014-10-09 15:28:45 -0700 | [diff] [blame] | 4010 | memcg_id = memcg_alloc_cache_id(); |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4011 | if (memcg_id < 0) { |
| 4012 | err = memcg_id; |
| 4013 | goto out; |
| 4014 | } |
| 4015 | |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4016 | memcg->kmemcg_id = memcg_id; |
| 4017 | INIT_LIST_HEAD(&memcg->memcg_slab_caches); |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4018 | |
| 4019 | /* |
| 4020 | * We couldn't have accounted to this cgroup, because it hasn't got the |
| 4021 | * active bit set yet, so this should succeed. |
| 4022 | */ |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4023 | err = page_counter_limit(&memcg->kmem, nr_pages); |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4024 | VM_BUG_ON(err); |
| 4025 | |
| 4026 | static_key_slow_inc(&memcg_kmem_enabled_key); |
| 4027 | /* |
| 4028 | * Setting the active bit after enabling static branching will |
| 4029 | * guarantee no one starts accounting before all call sites are |
| 4030 | * patched. |
| 4031 | */ |
| 4032 | memcg_kmem_set_active(memcg); |
| 4033 | out: |
| 4034 | memcg_resume_kmem_account(); |
| 4035 | return err; |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4036 | } |
| 4037 | |
| 4038 | static int memcg_activate_kmem(struct mem_cgroup *memcg, |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4039 | unsigned long nr_pages) |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4040 | { |
| 4041 | int ret; |
| 4042 | |
| 4043 | mutex_lock(&activate_kmem_mutex); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4044 | ret = __memcg_activate_kmem(memcg, nr_pages); |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4045 | mutex_unlock(&activate_kmem_mutex); |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 4046 | return ret; |
| 4047 | } |
| 4048 | |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4049 | static int memcg_update_kmem_limit(struct mem_cgroup *memcg, |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4050 | unsigned long limit) |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4051 | { |
| 4052 | int ret; |
| 4053 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4054 | mutex_lock(&memcg_limit_mutex); |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4055 | if (!memcg_kmem_is_active(memcg)) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4056 | ret = memcg_activate_kmem(memcg, limit); |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4057 | else |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4058 | ret = page_counter_limit(&memcg->kmem, limit); |
| 4059 | mutex_unlock(&memcg_limit_mutex); |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4060 | return ret; |
| 4061 | } |
| 4062 | |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 4063 | static int memcg_propagate_kmem(struct mem_cgroup *memcg) |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 4064 | { |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 4065 | int ret = 0; |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 4066 | struct mem_cgroup *parent = parent_mem_cgroup(memcg); |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4067 | |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 4068 | if (!parent) |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4069 | return 0; |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 4070 | |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4071 | mutex_lock(&activate_kmem_mutex); |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 4072 | /* |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4073 | * If the parent cgroup is not kmem-active now, it cannot be activated |
| 4074 | * after this point, because it has at least one child already. |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 4075 | */ |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4076 | if (memcg_kmem_is_active(parent)) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4077 | ret = __memcg_activate_kmem(memcg, PAGE_COUNTER_MAX); |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4078 | mutex_unlock(&activate_kmem_mutex); |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 4079 | return ret; |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 4080 | } |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4081 | #else |
| 4082 | static int memcg_update_kmem_limit(struct mem_cgroup *memcg, |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4083 | unsigned long limit) |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 4084 | { |
| 4085 | return -EINVAL; |
| 4086 | } |
Hugh Dickins | 6d043990 | 2013-02-22 16:35:50 -0800 | [diff] [blame] | 4087 | #endif /* CONFIG_MEMCG_KMEM */ |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 4088 | |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4089 | /* |
| 4090 | * The user of this function is... |
| 4091 | * RES_LIMIT. |
| 4092 | */ |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4093 | static ssize_t mem_cgroup_write(struct kernfs_open_file *of, |
| 4094 | char *buf, size_t nbytes, loff_t off) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4095 | { |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4096 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4097 | unsigned long nr_pages; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4098 | int ret; |
| 4099 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4100 | buf = strstrip(buf); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4101 | ret = page_counter_memparse(buf, &nr_pages); |
| 4102 | if (ret) |
| 4103 | return ret; |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4104 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4105 | switch (MEMFILE_ATTR(of_cft(of)->private)) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4106 | case RES_LIMIT: |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 4107 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
| 4108 | ret = -EINVAL; |
| 4109 | break; |
| 4110 | } |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4111 | switch (MEMFILE_TYPE(of_cft(of)->private)) { |
| 4112 | case _MEM: |
| 4113 | ret = mem_cgroup_resize_limit(memcg, nr_pages); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4114 | break; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4115 | case _MEMSWAP: |
| 4116 | ret = mem_cgroup_resize_memsw_limit(memcg, nr_pages); |
| 4117 | break; |
| 4118 | case _KMEM: |
| 4119 | ret = memcg_update_kmem_limit(memcg, nr_pages); |
| 4120 | break; |
| 4121 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4122 | break; |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 4123 | case RES_SOFT_LIMIT: |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4124 | memcg->soft_limit = nr_pages; |
| 4125 | ret = 0; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4126 | break; |
| 4127 | } |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4128 | return ret ?: nbytes; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4129 | } |
| 4130 | |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4131 | static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf, |
| 4132 | size_t nbytes, loff_t off) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4133 | { |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4134 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4135 | struct page_counter *counter; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4136 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4137 | switch (MEMFILE_TYPE(of_cft(of)->private)) { |
| 4138 | case _MEM: |
| 4139 | counter = &memcg->memory; |
| 4140 | break; |
| 4141 | case _MEMSWAP: |
| 4142 | counter = &memcg->memsw; |
| 4143 | break; |
| 4144 | case _KMEM: |
| 4145 | counter = &memcg->kmem; |
| 4146 | break; |
| 4147 | default: |
| 4148 | BUG(); |
| 4149 | } |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4150 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4151 | switch (MEMFILE_ATTR(of_cft(of)->private)) { |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4152 | case RES_MAX_USAGE: |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4153 | page_counter_reset_watermark(counter); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4154 | break; |
| 4155 | case RES_FAILCNT: |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4156 | counter->failcnt = 0; |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4157 | break; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4158 | default: |
| 4159 | BUG(); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4160 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4161 | |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4162 | return nbytes; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4163 | } |
| 4164 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4165 | static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4166 | struct cftype *cft) |
| 4167 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4168 | return mem_cgroup_from_css(css)->move_charge_at_immigrate; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4169 | } |
| 4170 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4171 | #ifdef CONFIG_MMU |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4172 | static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4173 | struct cftype *cft, u64 val) |
| 4174 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4175 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4176 | |
| 4177 | if (val >= (1 << NR_MOVE_TYPE)) |
| 4178 | return -EINVAL; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4179 | |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 4180 | /* |
| 4181 | * No kind of locking is needed in here, because ->can_attach() will |
| 4182 | * check this value once in the beginning of the process, and then carry |
| 4183 | * on with stale data. This means that changes to this value will only |
| 4184 | * affect task migrations starting after the change. |
| 4185 | */ |
| 4186 | memcg->move_charge_at_immigrate = val; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4187 | return 0; |
| 4188 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4189 | #else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4190 | static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css, |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4191 | struct cftype *cft, u64 val) |
| 4192 | { |
| 4193 | return -ENOSYS; |
| 4194 | } |
| 4195 | #endif |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4196 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4197 | #ifdef CONFIG_NUMA |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4198 | static int memcg_numa_stat_show(struct seq_file *m, void *v) |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4199 | { |
Greg Thelen | 25485de | 2013-11-12 15:07:40 -0800 | [diff] [blame] | 4200 | struct numa_stat { |
| 4201 | const char *name; |
| 4202 | unsigned int lru_mask; |
| 4203 | }; |
| 4204 | |
| 4205 | static const struct numa_stat stats[] = { |
| 4206 | { "total", LRU_ALL }, |
| 4207 | { "file", LRU_ALL_FILE }, |
| 4208 | { "anon", LRU_ALL_ANON }, |
| 4209 | { "unevictable", BIT(LRU_UNEVICTABLE) }, |
| 4210 | }; |
| 4211 | const struct numa_stat *stat; |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4212 | int nid; |
Greg Thelen | 25485de | 2013-11-12 15:07:40 -0800 | [diff] [blame] | 4213 | unsigned long nr; |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4214 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4215 | |
Greg Thelen | 25485de | 2013-11-12 15:07:40 -0800 | [diff] [blame] | 4216 | for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) { |
| 4217 | nr = mem_cgroup_nr_lru_pages(memcg, stat->lru_mask); |
| 4218 | seq_printf(m, "%s=%lu", stat->name, nr); |
| 4219 | for_each_node_state(nid, N_MEMORY) { |
| 4220 | nr = mem_cgroup_node_nr_lru_pages(memcg, nid, |
| 4221 | stat->lru_mask); |
| 4222 | seq_printf(m, " N%d=%lu", nid, nr); |
| 4223 | } |
| 4224 | seq_putc(m, '\n'); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4225 | } |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4226 | |
Ying Han | 071aee1 | 2013-11-12 15:07:41 -0800 | [diff] [blame] | 4227 | for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) { |
| 4228 | struct mem_cgroup *iter; |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4229 | |
Ying Han | 071aee1 | 2013-11-12 15:07:41 -0800 | [diff] [blame] | 4230 | nr = 0; |
| 4231 | for_each_mem_cgroup_tree(iter, memcg) |
| 4232 | nr += mem_cgroup_nr_lru_pages(iter, stat->lru_mask); |
| 4233 | seq_printf(m, "hierarchical_%s=%lu", stat->name, nr); |
| 4234 | for_each_node_state(nid, N_MEMORY) { |
| 4235 | nr = 0; |
| 4236 | for_each_mem_cgroup_tree(iter, memcg) |
| 4237 | nr += mem_cgroup_node_nr_lru_pages( |
| 4238 | iter, nid, stat->lru_mask); |
| 4239 | seq_printf(m, " N%d=%lu", nid, nr); |
| 4240 | } |
| 4241 | seq_putc(m, '\n'); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4242 | } |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4243 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4244 | return 0; |
| 4245 | } |
| 4246 | #endif /* CONFIG_NUMA */ |
| 4247 | |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 4248 | static inline void mem_cgroup_lru_names_not_uptodate(void) |
| 4249 | { |
| 4250 | BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS); |
| 4251 | } |
| 4252 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4253 | static int memcg_stat_show(struct seq_file *m, void *v) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4254 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4255 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4256 | unsigned long memory, memsw; |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 4257 | struct mem_cgroup *mi; |
| 4258 | unsigned int i; |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4259 | |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 4260 | for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) { |
Kamezawa Hiroyuki | bff6bb8 | 2012-07-31 16:41:38 -0700 | [diff] [blame] | 4261 | if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account) |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4262 | continue; |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 4263 | seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i], |
| 4264 | mem_cgroup_read_stat(memcg, i) * PAGE_SIZE); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4265 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4266 | |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 4267 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) |
| 4268 | seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i], |
| 4269 | mem_cgroup_read_events(memcg, i)); |
| 4270 | |
| 4271 | for (i = 0; i < NR_LRU_LISTS; i++) |
| 4272 | seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i], |
| 4273 | mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE); |
| 4274 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4275 | /* Hierarchical information */ |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4276 | memory = memsw = PAGE_COUNTER_MAX; |
| 4277 | for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) { |
| 4278 | memory = min(memory, mi->memory.limit); |
| 4279 | memsw = min(memsw, mi->memsw.limit); |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 4280 | } |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4281 | seq_printf(m, "hierarchical_memory_limit %llu\n", |
| 4282 | (u64)memory * PAGE_SIZE); |
| 4283 | if (do_swap_account) |
| 4284 | seq_printf(m, "hierarchical_memsw_limit %llu\n", |
| 4285 | (u64)memsw * PAGE_SIZE); |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4286 | |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 4287 | for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) { |
| 4288 | long long val = 0; |
| 4289 | |
Kamezawa Hiroyuki | bff6bb8 | 2012-07-31 16:41:38 -0700 | [diff] [blame] | 4290 | if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account) |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4291 | continue; |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 4292 | for_each_mem_cgroup_tree(mi, memcg) |
| 4293 | val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE; |
| 4294 | seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val); |
| 4295 | } |
| 4296 | |
| 4297 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) { |
| 4298 | unsigned long long val = 0; |
| 4299 | |
| 4300 | for_each_mem_cgroup_tree(mi, memcg) |
| 4301 | val += mem_cgroup_read_events(mi, i); |
| 4302 | seq_printf(m, "total_%s %llu\n", |
| 4303 | mem_cgroup_events_names[i], val); |
| 4304 | } |
| 4305 | |
| 4306 | for (i = 0; i < NR_LRU_LISTS; i++) { |
| 4307 | unsigned long long val = 0; |
| 4308 | |
| 4309 | for_each_mem_cgroup_tree(mi, memcg) |
| 4310 | val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE; |
| 4311 | seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4312 | } |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4313 | |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4314 | #ifdef CONFIG_DEBUG_VM |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4315 | { |
| 4316 | int nid, zid; |
| 4317 | struct mem_cgroup_per_zone *mz; |
Hugh Dickins | 89abfab | 2012-05-29 15:06:53 -0700 | [diff] [blame] | 4318 | struct zone_reclaim_stat *rstat; |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4319 | unsigned long recent_rotated[2] = {0, 0}; |
| 4320 | unsigned long recent_scanned[2] = {0, 0}; |
| 4321 | |
| 4322 | for_each_online_node(nid) |
| 4323 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 4324 | mz = &memcg->nodeinfo[nid]->zoneinfo[zid]; |
Hugh Dickins | 89abfab | 2012-05-29 15:06:53 -0700 | [diff] [blame] | 4325 | rstat = &mz->lruvec.reclaim_stat; |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4326 | |
Hugh Dickins | 89abfab | 2012-05-29 15:06:53 -0700 | [diff] [blame] | 4327 | recent_rotated[0] += rstat->recent_rotated[0]; |
| 4328 | recent_rotated[1] += rstat->recent_rotated[1]; |
| 4329 | recent_scanned[0] += rstat->recent_scanned[0]; |
| 4330 | recent_scanned[1] += rstat->recent_scanned[1]; |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4331 | } |
Johannes Weiner | 78ccf5b | 2012-05-29 15:07:06 -0700 | [diff] [blame] | 4332 | seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]); |
| 4333 | seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]); |
| 4334 | seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]); |
| 4335 | seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]); |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4336 | } |
| 4337 | #endif |
| 4338 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4339 | return 0; |
| 4340 | } |
| 4341 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4342 | static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css, |
| 4343 | struct cftype *cft) |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4344 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4345 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4346 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 4347 | return mem_cgroup_swappiness(memcg); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4348 | } |
| 4349 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4350 | static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css, |
| 4351 | struct cftype *cft, u64 val) |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4352 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4353 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4354 | |
Johannes Weiner | 3dae7fe | 2014-06-04 16:07:01 -0700 | [diff] [blame] | 4355 | if (val > 100) |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4356 | return -EINVAL; |
| 4357 | |
Linus Torvalds | 14208b0 | 2014-06-09 15:03:33 -0700 | [diff] [blame] | 4358 | if (css->parent) |
Johannes Weiner | 3dae7fe | 2014-06-04 16:07:01 -0700 | [diff] [blame] | 4359 | memcg->swappiness = val; |
| 4360 | else |
| 4361 | vm_swappiness = val; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4362 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4363 | return 0; |
| 4364 | } |
| 4365 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4366 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) |
| 4367 | { |
| 4368 | struct mem_cgroup_threshold_ary *t; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4369 | unsigned long usage; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4370 | int i; |
| 4371 | |
| 4372 | rcu_read_lock(); |
| 4373 | if (!swap) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4374 | t = rcu_dereference(memcg->thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4375 | else |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4376 | t = rcu_dereference(memcg->memsw_thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4377 | |
| 4378 | if (!t) |
| 4379 | goto unlock; |
| 4380 | |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 4381 | usage = mem_cgroup_usage(memcg, swap); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4382 | |
| 4383 | /* |
Sha Zhengju | 748dad3 | 2012-05-29 15:06:57 -0700 | [diff] [blame] | 4384 | * current_threshold points to threshold just below or equal to usage. |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4385 | * If it's not true, a threshold was crossed after last |
| 4386 | * call of __mem_cgroup_threshold(). |
| 4387 | */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4388 | i = t->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4389 | |
| 4390 | /* |
| 4391 | * Iterate backward over array of thresholds starting from |
| 4392 | * current_threshold and check if a threshold is crossed. |
| 4393 | * If none of thresholds below usage is crossed, we read |
| 4394 | * only one element of the array here. |
| 4395 | */ |
| 4396 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) |
| 4397 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4398 | |
| 4399 | /* i = current_threshold + 1 */ |
| 4400 | i++; |
| 4401 | |
| 4402 | /* |
| 4403 | * Iterate forward over array of thresholds starting from |
| 4404 | * current_threshold+1 and check if a threshold is crossed. |
| 4405 | * If none of thresholds above usage is crossed, we read |
| 4406 | * only one element of the array here. |
| 4407 | */ |
| 4408 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) |
| 4409 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4410 | |
| 4411 | /* Update current_threshold */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4412 | t->current_threshold = i - 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4413 | unlock: |
| 4414 | rcu_read_unlock(); |
| 4415 | } |
| 4416 | |
| 4417 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) |
| 4418 | { |
Kirill A. Shutemov | ad4ca5f | 2010-10-07 12:59:27 -0700 | [diff] [blame] | 4419 | while (memcg) { |
| 4420 | __mem_cgroup_threshold(memcg, false); |
| 4421 | if (do_swap_account) |
| 4422 | __mem_cgroup_threshold(memcg, true); |
| 4423 | |
| 4424 | memcg = parent_mem_cgroup(memcg); |
| 4425 | } |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | static int compare_thresholds(const void *a, const void *b) |
| 4429 | { |
| 4430 | const struct mem_cgroup_threshold *_a = a; |
| 4431 | const struct mem_cgroup_threshold *_b = b; |
| 4432 | |
Greg Thelen | 2bff24a | 2013-09-11 14:23:08 -0700 | [diff] [blame] | 4433 | if (_a->threshold > _b->threshold) |
| 4434 | return 1; |
| 4435 | |
| 4436 | if (_a->threshold < _b->threshold) |
| 4437 | return -1; |
| 4438 | |
| 4439 | return 0; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4440 | } |
| 4441 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4442 | static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4443 | { |
| 4444 | struct mem_cgroup_eventfd_list *ev; |
| 4445 | |
Michal Hocko | 2bcf2e9 | 2014-07-30 16:08:33 -0700 | [diff] [blame] | 4446 | spin_lock(&memcg_oom_lock); |
| 4447 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4448 | list_for_each_entry(ev, &memcg->oom_notify, list) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4449 | eventfd_signal(ev->eventfd, 1); |
Michal Hocko | 2bcf2e9 | 2014-07-30 16:08:33 -0700 | [diff] [blame] | 4450 | |
| 4451 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4452 | return 0; |
| 4453 | } |
| 4454 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4455 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4456 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4457 | struct mem_cgroup *iter; |
| 4458 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4459 | for_each_mem_cgroup_tree(iter, memcg) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4460 | mem_cgroup_oom_notify_cb(iter); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4461 | } |
| 4462 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4463 | static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4464 | struct eventfd_ctx *eventfd, const char *args, enum res_type type) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4465 | { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4466 | struct mem_cgroup_thresholds *thresholds; |
| 4467 | struct mem_cgroup_threshold_ary *new; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4468 | unsigned long threshold; |
| 4469 | unsigned long usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4470 | int i, size, ret; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4471 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4472 | ret = page_counter_memparse(args, &threshold); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4473 | if (ret) |
| 4474 | return ret; |
| 4475 | |
| 4476 | mutex_lock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4477 | |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 4478 | if (type == _MEM) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4479 | thresholds = &memcg->thresholds; |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 4480 | usage = mem_cgroup_usage(memcg, false); |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [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; |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 4483 | usage = mem_cgroup_usage(memcg, true); |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 4484 | } else |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4485 | BUG(); |
| 4486 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4487 | /* Check if a threshold crossed before adding a new one */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4488 | if (thresholds->primary) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4489 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4490 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4491 | size = thresholds->primary ? thresholds->primary->size + 1 : 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4492 | |
| 4493 | /* Allocate memory for new array of thresholds */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4494 | new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4495 | GFP_KERNEL); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4496 | if (!new) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4497 | ret = -ENOMEM; |
| 4498 | goto unlock; |
| 4499 | } |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4500 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4501 | |
| 4502 | /* Copy thresholds (if any) to new array */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4503 | if (thresholds->primary) { |
| 4504 | memcpy(new->entries, thresholds->primary->entries, (size - 1) * |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4505 | sizeof(struct mem_cgroup_threshold)); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4506 | } |
| 4507 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4508 | /* Add new threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4509 | new->entries[size - 1].eventfd = eventfd; |
| 4510 | new->entries[size - 1].threshold = threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4511 | |
| 4512 | /* Sort thresholds. Registering of new threshold isn't time-critical */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4513 | sort(new->entries, size, sizeof(struct mem_cgroup_threshold), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4514 | compare_thresholds, NULL); |
| 4515 | |
| 4516 | /* Find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4517 | new->current_threshold = -1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4518 | for (i = 0; i < size; i++) { |
Sha Zhengju | 748dad3 | 2012-05-29 15:06:57 -0700 | [diff] [blame] | 4519 | if (new->entries[i].threshold <= usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4520 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4521 | * new->current_threshold will not be used until |
| 4522 | * rcu_assign_pointer(), so it's safe to increment |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4523 | * it here. |
| 4524 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4525 | ++new->current_threshold; |
Sha Zhengju | 748dad3 | 2012-05-29 15:06:57 -0700 | [diff] [blame] | 4526 | } else |
| 4527 | break; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4528 | } |
| 4529 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4530 | /* Free old spare buffer and save old primary buffer as spare */ |
| 4531 | kfree(thresholds->spare); |
| 4532 | thresholds->spare = thresholds->primary; |
| 4533 | |
| 4534 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4535 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4536 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4537 | synchronize_rcu(); |
| 4538 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4539 | unlock: |
| 4540 | mutex_unlock(&memcg->thresholds_lock); |
| 4541 | |
| 4542 | return ret; |
| 4543 | } |
| 4544 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4545 | static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4546 | struct eventfd_ctx *eventfd, const char *args) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4547 | { |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4548 | return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM); |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4549 | } |
| 4550 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4551 | static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4552 | struct eventfd_ctx *eventfd, const char *args) |
| 4553 | { |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4554 | return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP); |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4555 | } |
| 4556 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4557 | static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4558 | struct eventfd_ctx *eventfd, enum res_type type) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4559 | { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4560 | struct mem_cgroup_thresholds *thresholds; |
| 4561 | struct mem_cgroup_threshold_ary *new; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4562 | unsigned long usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4563 | int i, j, size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4564 | |
| 4565 | mutex_lock(&memcg->thresholds_lock); |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 4566 | |
| 4567 | if (type == _MEM) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4568 | thresholds = &memcg->thresholds; |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 4569 | usage = mem_cgroup_usage(memcg, false); |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 4570 | } else if (type == _MEMSWAP) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4571 | thresholds = &memcg->memsw_thresholds; |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 4572 | usage = mem_cgroup_usage(memcg, true); |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 4573 | } else |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4574 | BUG(); |
| 4575 | |
Anton Vorontsov | 371528c | 2012-02-24 05:14:46 +0400 | [diff] [blame] | 4576 | if (!thresholds->primary) |
| 4577 | goto unlock; |
| 4578 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4579 | /* Check if a threshold crossed before removing */ |
| 4580 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4581 | |
| 4582 | /* Calculate new number of threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4583 | size = 0; |
| 4584 | for (i = 0; i < thresholds->primary->size; i++) { |
| 4585 | if (thresholds->primary->entries[i].eventfd != eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4586 | size++; |
| 4587 | } |
| 4588 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4589 | new = thresholds->spare; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4590 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4591 | /* Set thresholds array to NULL if we don't have thresholds */ |
| 4592 | if (!size) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4593 | kfree(new); |
| 4594 | new = NULL; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4595 | goto swap_buffers; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4596 | } |
| 4597 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4598 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4599 | |
| 4600 | /* Copy thresholds and find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4601 | new->current_threshold = -1; |
| 4602 | for (i = 0, j = 0; i < thresholds->primary->size; i++) { |
| 4603 | if (thresholds->primary->entries[i].eventfd == eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4604 | continue; |
| 4605 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4606 | new->entries[j] = thresholds->primary->entries[i]; |
Sha Zhengju | 748dad3 | 2012-05-29 15:06:57 -0700 | [diff] [blame] | 4607 | if (new->entries[j].threshold <= usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4608 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4609 | * new->current_threshold will not be used |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4610 | * until rcu_assign_pointer(), so it's safe to increment |
| 4611 | * it here. |
| 4612 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4613 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4614 | } |
| 4615 | j++; |
| 4616 | } |
| 4617 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4618 | swap_buffers: |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4619 | /* Swap primary and spare array */ |
| 4620 | thresholds->spare = thresholds->primary; |
Sha Zhengju | 8c75776 | 2012-05-10 13:01:45 -0700 | [diff] [blame] | 4621 | /* If all events are unregistered, free the spare array */ |
| 4622 | if (!new) { |
| 4623 | kfree(thresholds->spare); |
| 4624 | thresholds->spare = NULL; |
| 4625 | } |
| 4626 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4627 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4628 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4629 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4630 | synchronize_rcu(); |
Anton Vorontsov | 371528c | 2012-02-24 05:14:46 +0400 | [diff] [blame] | 4631 | unlock: |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4632 | mutex_unlock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4633 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4634 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4635 | static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4636 | struct eventfd_ctx *eventfd) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4637 | { |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4638 | return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM); |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4639 | } |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4640 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4641 | static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4642 | struct eventfd_ctx *eventfd) |
| 4643 | { |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4644 | return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP); |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4645 | } |
| 4646 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4647 | static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4648 | struct eventfd_ctx *eventfd, const char *args) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4649 | { |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4650 | struct mem_cgroup_eventfd_list *event; |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4651 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4652 | event = kmalloc(sizeof(*event), GFP_KERNEL); |
| 4653 | if (!event) |
| 4654 | return -ENOMEM; |
| 4655 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4656 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4657 | |
| 4658 | event->eventfd = eventfd; |
| 4659 | list_add(&event->list, &memcg->oom_notify); |
| 4660 | |
| 4661 | /* already in OOM ? */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 4662 | if (atomic_read(&memcg->under_oom)) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4663 | eventfd_signal(eventfd, 1); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4664 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4665 | |
| 4666 | return 0; |
| 4667 | } |
| 4668 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4669 | static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4670 | struct eventfd_ctx *eventfd) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4671 | { |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4672 | struct mem_cgroup_eventfd_list *ev, *tmp; |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4673 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4674 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4675 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4676 | list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) { |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4677 | if (ev->eventfd == eventfd) { |
| 4678 | list_del(&ev->list); |
| 4679 | kfree(ev); |
| 4680 | } |
| 4681 | } |
| 4682 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4683 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4684 | } |
| 4685 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4686 | static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4687 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4688 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf)); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4689 | |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 4690 | seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable); |
| 4691 | seq_printf(sf, "under_oom %d\n", (bool)atomic_read(&memcg->under_oom)); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4692 | return 0; |
| 4693 | } |
| 4694 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4695 | static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css, |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4696 | struct cftype *cft, u64 val) |
| 4697 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4698 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4699 | |
| 4700 | /* cannot set to root cgroup and only 0 and 1 are allowed */ |
Linus Torvalds | 14208b0 | 2014-06-09 15:03:33 -0700 | [diff] [blame] | 4701 | if (!css->parent || !((val == 0) || (val == 1))) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4702 | return -EINVAL; |
| 4703 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4704 | memcg->oom_kill_disable = val; |
KAMEZAWA Hiroyuki | 4d845eb | 2010-06-29 15:05:18 -0700 | [diff] [blame] | 4705 | if (!val) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4706 | memcg_oom_recover(memcg); |
Johannes Weiner | 3dae7fe | 2014-06-04 16:07:01 -0700 | [diff] [blame] | 4707 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4708 | return 0; |
| 4709 | } |
| 4710 | |
Andrew Morton | c255a45 | 2012-07-31 16:43:02 -0700 | [diff] [blame] | 4711 | #ifdef CONFIG_MEMCG_KMEM |
Glauber Costa | cbe128e3 | 2012-04-09 19:36:34 -0300 | [diff] [blame] | 4712 | static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss) |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 4713 | { |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 4714 | int ret; |
| 4715 | |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 4716 | memcg->kmemcg_id = -1; |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 4717 | ret = memcg_propagate_kmem(memcg); |
| 4718 | if (ret) |
| 4719 | return ret; |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 4720 | |
Glauber Costa | 1d62e43 | 2012-04-09 19:36:33 -0300 | [diff] [blame] | 4721 | return mem_cgroup_sockets_init(memcg, ss); |
Michel Lespinasse | 573b400 | 2013-04-29 15:08:13 -0700 | [diff] [blame] | 4722 | } |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 4723 | |
Li Zefan | 10d5ebf | 2013-07-08 16:00:33 -0700 | [diff] [blame] | 4724 | static void memcg_destroy_kmem(struct mem_cgroup *memcg) |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 4725 | { |
Glauber Costa | 1d62e43 | 2012-04-09 19:36:33 -0300 | [diff] [blame] | 4726 | mem_cgroup_sockets_destroy(memcg); |
Li Zefan | 10d5ebf | 2013-07-08 16:00:33 -0700 | [diff] [blame] | 4727 | } |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 4728 | #else |
Glauber Costa | cbe128e3 | 2012-04-09 19:36:34 -0300 | [diff] [blame] | 4729 | static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss) |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 4730 | { |
| 4731 | return 0; |
| 4732 | } |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 4733 | |
Li Zefan | 10d5ebf | 2013-07-08 16:00:33 -0700 | [diff] [blame] | 4734 | static void memcg_destroy_kmem(struct mem_cgroup *memcg) |
| 4735 | { |
| 4736 | } |
Glauber Costa | e5671df | 2011-12-11 21:47:01 +0000 | [diff] [blame] | 4737 | #endif |
| 4738 | |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4739 | /* |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4740 | * DO NOT USE IN NEW FILES. |
| 4741 | * |
| 4742 | * "cgroup.event_control" implementation. |
| 4743 | * |
| 4744 | * This is way over-engineered. It tries to support fully configurable |
| 4745 | * events for each user. Such level of flexibility is completely |
| 4746 | * unnecessary especially in the light of the planned unified hierarchy. |
| 4747 | * |
| 4748 | * Please deprecate this and replace with something simpler if at all |
| 4749 | * possible. |
| 4750 | */ |
| 4751 | |
| 4752 | /* |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4753 | * Unregister event and free resources. |
| 4754 | * |
| 4755 | * Gets called from workqueue. |
| 4756 | */ |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4757 | static void memcg_event_remove(struct work_struct *work) |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4758 | { |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4759 | struct mem_cgroup_event *event = |
| 4760 | container_of(work, struct mem_cgroup_event, remove); |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4761 | struct mem_cgroup *memcg = event->memcg; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4762 | |
| 4763 | remove_wait_queue(event->wqh, &event->wait); |
| 4764 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4765 | event->unregister_event(memcg, event->eventfd); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4766 | |
| 4767 | /* Notify userspace the event is going away. */ |
| 4768 | eventfd_signal(event->eventfd, 1); |
| 4769 | |
| 4770 | eventfd_ctx_put(event->eventfd); |
| 4771 | kfree(event); |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4772 | css_put(&memcg->css); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4773 | } |
| 4774 | |
| 4775 | /* |
| 4776 | * Gets called on POLLHUP on eventfd when user closes it. |
| 4777 | * |
| 4778 | * Called with wqh->lock held and interrupts disabled. |
| 4779 | */ |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4780 | static int memcg_event_wake(wait_queue_t *wait, unsigned mode, |
| 4781 | int sync, void *key) |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4782 | { |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4783 | struct mem_cgroup_event *event = |
| 4784 | container_of(wait, struct mem_cgroup_event, wait); |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4785 | struct mem_cgroup *memcg = event->memcg; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4786 | unsigned long flags = (unsigned long)key; |
| 4787 | |
| 4788 | if (flags & POLLHUP) { |
| 4789 | /* |
| 4790 | * If the event has been detached at cgroup removal, we |
| 4791 | * can simply return knowing the other side will cleanup |
| 4792 | * for us. |
| 4793 | * |
| 4794 | * We can't race against event freeing since the other |
| 4795 | * side will require wqh->lock via remove_wait_queue(), |
| 4796 | * which we hold. |
| 4797 | */ |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4798 | spin_lock(&memcg->event_list_lock); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4799 | if (!list_empty(&event->list)) { |
| 4800 | list_del_init(&event->list); |
| 4801 | /* |
| 4802 | * We are in atomic context, but cgroup_event_remove() |
| 4803 | * may sleep, so we have to call it in workqueue. |
| 4804 | */ |
| 4805 | schedule_work(&event->remove); |
| 4806 | } |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4807 | spin_unlock(&memcg->event_list_lock); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4808 | } |
| 4809 | |
| 4810 | return 0; |
| 4811 | } |
| 4812 | |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4813 | static void memcg_event_ptable_queue_proc(struct file *file, |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4814 | wait_queue_head_t *wqh, poll_table *pt) |
| 4815 | { |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4816 | struct mem_cgroup_event *event = |
| 4817 | container_of(pt, struct mem_cgroup_event, pt); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4818 | |
| 4819 | event->wqh = wqh; |
| 4820 | add_wait_queue(wqh, &event->wait); |
| 4821 | } |
| 4822 | |
| 4823 | /* |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4824 | * DO NOT USE IN NEW FILES. |
| 4825 | * |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4826 | * Parse input and register new cgroup event handler. |
| 4827 | * |
| 4828 | * Input must be in format '<event_fd> <control_fd> <args>'. |
| 4829 | * Interpretation of args is defined by control file implementation. |
| 4830 | */ |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4831 | static ssize_t memcg_write_event_control(struct kernfs_open_file *of, |
| 4832 | char *buf, size_t nbytes, loff_t off) |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4833 | { |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4834 | struct cgroup_subsys_state *css = of_css(of); |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4835 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4836 | struct mem_cgroup_event *event; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4837 | struct cgroup_subsys_state *cfile_css; |
| 4838 | unsigned int efd, cfd; |
| 4839 | struct fd efile; |
| 4840 | struct fd cfile; |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4841 | const char *name; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4842 | char *endp; |
| 4843 | int ret; |
| 4844 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4845 | buf = strstrip(buf); |
| 4846 | |
| 4847 | efd = simple_strtoul(buf, &endp, 10); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4848 | if (*endp != ' ') |
| 4849 | return -EINVAL; |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4850 | buf = endp + 1; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4851 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4852 | cfd = simple_strtoul(buf, &endp, 10); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4853 | if ((*endp != ' ') && (*endp != '\0')) |
| 4854 | return -EINVAL; |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4855 | buf = endp + 1; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4856 | |
| 4857 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 4858 | if (!event) |
| 4859 | return -ENOMEM; |
| 4860 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4861 | event->memcg = memcg; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4862 | INIT_LIST_HEAD(&event->list); |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4863 | init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc); |
| 4864 | init_waitqueue_func_entry(&event->wait, memcg_event_wake); |
| 4865 | INIT_WORK(&event->remove, memcg_event_remove); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4866 | |
| 4867 | efile = fdget(efd); |
| 4868 | if (!efile.file) { |
| 4869 | ret = -EBADF; |
| 4870 | goto out_kfree; |
| 4871 | } |
| 4872 | |
| 4873 | event->eventfd = eventfd_ctx_fileget(efile.file); |
| 4874 | if (IS_ERR(event->eventfd)) { |
| 4875 | ret = PTR_ERR(event->eventfd); |
| 4876 | goto out_put_efile; |
| 4877 | } |
| 4878 | |
| 4879 | cfile = fdget(cfd); |
| 4880 | if (!cfile.file) { |
| 4881 | ret = -EBADF; |
| 4882 | goto out_put_eventfd; |
| 4883 | } |
| 4884 | |
| 4885 | /* the process need read permission on control file */ |
| 4886 | /* AV: shouldn't we check that it's been opened for read instead? */ |
| 4887 | ret = inode_permission(file_inode(cfile.file), MAY_READ); |
| 4888 | if (ret < 0) |
| 4889 | goto out_put_cfile; |
| 4890 | |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4891 | /* |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4892 | * Determine the event callbacks and set them in @event. This used |
| 4893 | * to be done via struct cftype but cgroup core no longer knows |
| 4894 | * about these events. The following is crude but the whole thing |
| 4895 | * is for compatibility anyway. |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4896 | * |
| 4897 | * DO NOT ADD NEW FILES. |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4898 | */ |
| 4899 | name = cfile.file->f_dentry->d_name.name; |
| 4900 | |
| 4901 | if (!strcmp(name, "memory.usage_in_bytes")) { |
| 4902 | event->register_event = mem_cgroup_usage_register_event; |
| 4903 | event->unregister_event = mem_cgroup_usage_unregister_event; |
| 4904 | } else if (!strcmp(name, "memory.oom_control")) { |
| 4905 | event->register_event = mem_cgroup_oom_register_event; |
| 4906 | event->unregister_event = mem_cgroup_oom_unregister_event; |
| 4907 | } else if (!strcmp(name, "memory.pressure_level")) { |
| 4908 | event->register_event = vmpressure_register_event; |
| 4909 | event->unregister_event = vmpressure_unregister_event; |
| 4910 | } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) { |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4911 | event->register_event = memsw_cgroup_usage_register_event; |
| 4912 | event->unregister_event = memsw_cgroup_usage_unregister_event; |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4913 | } else { |
| 4914 | ret = -EINVAL; |
| 4915 | goto out_put_cfile; |
| 4916 | } |
| 4917 | |
| 4918 | /* |
Tejun Heo | b5557c4 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4919 | * Verify @cfile should belong to @css. Also, remaining events are |
| 4920 | * automatically removed on cgroup destruction but the removal is |
| 4921 | * asynchronous, so take an extra ref on @css. |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4922 | */ |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 4923 | cfile_css = css_tryget_online_from_dir(cfile.file->f_dentry->d_parent, |
| 4924 | &memory_cgrp_subsys); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4925 | ret = -EINVAL; |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4926 | if (IS_ERR(cfile_css)) |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4927 | goto out_put_cfile; |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4928 | if (cfile_css != css) { |
| 4929 | css_put(cfile_css); |
| 4930 | goto out_put_cfile; |
| 4931 | } |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4932 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4933 | ret = event->register_event(memcg, event->eventfd, buf); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4934 | if (ret) |
| 4935 | goto out_put_css; |
| 4936 | |
| 4937 | efile.file->f_op->poll(efile.file, &event->pt); |
| 4938 | |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4939 | spin_lock(&memcg->event_list_lock); |
| 4940 | list_add(&event->list, &memcg->event_list); |
| 4941 | spin_unlock(&memcg->event_list_lock); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4942 | |
| 4943 | fdput(cfile); |
| 4944 | fdput(efile); |
| 4945 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4946 | return nbytes; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4947 | |
| 4948 | out_put_css: |
Tejun Heo | b5557c4 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4949 | css_put(css); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4950 | out_put_cfile: |
| 4951 | fdput(cfile); |
| 4952 | out_put_eventfd: |
| 4953 | eventfd_ctx_put(event->eventfd); |
| 4954 | out_put_efile: |
| 4955 | fdput(efile); |
| 4956 | out_kfree: |
| 4957 | kfree(event); |
| 4958 | |
| 4959 | return ret; |
| 4960 | } |
| 4961 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4962 | static struct cftype mem_cgroup_files[] = { |
| 4963 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4964 | .name = "usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4965 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 4966 | .read_u64 = mem_cgroup_read_u64, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4967 | }, |
| 4968 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4969 | .name = "max_usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4970 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4971 | .write = mem_cgroup_reset, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 4972 | .read_u64 = mem_cgroup_read_u64, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4973 | }, |
| 4974 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 4975 | .name = "limit_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4976 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4977 | .write = mem_cgroup_write, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 4978 | .read_u64 = mem_cgroup_read_u64, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4979 | }, |
| 4980 | { |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 4981 | .name = "soft_limit_in_bytes", |
| 4982 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4983 | .write = mem_cgroup_write, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 4984 | .read_u64 = mem_cgroup_read_u64, |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 4985 | }, |
| 4986 | { |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4987 | .name = "failcnt", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4988 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4989 | .write = mem_cgroup_reset, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 4990 | .read_u64 = mem_cgroup_read_u64, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4991 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 4992 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4993 | .name = "stat", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4994 | .seq_show = memcg_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4995 | }, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4996 | { |
| 4997 | .name = "force_empty", |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4998 | .write = mem_cgroup_force_empty_write, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4999 | }, |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5000 | { |
| 5001 | .name = "use_hierarchy", |
| 5002 | .write_u64 = mem_cgroup_hierarchy_write, |
| 5003 | .read_u64 = mem_cgroup_hierarchy_read, |
| 5004 | }, |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5005 | { |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 5006 | .name = "cgroup.event_control", /* XXX: for compat */ |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5007 | .write = memcg_write_event_control, |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5008 | .flags = CFTYPE_NO_PREFIX, |
| 5009 | .mode = S_IWUGO, |
| 5010 | }, |
| 5011 | { |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5012 | .name = "swappiness", |
| 5013 | .read_u64 = mem_cgroup_swappiness_read, |
| 5014 | .write_u64 = mem_cgroup_swappiness_write, |
| 5015 | }, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5016 | { |
| 5017 | .name = "move_charge_at_immigrate", |
| 5018 | .read_u64 = mem_cgroup_move_charge_read, |
| 5019 | .write_u64 = mem_cgroup_move_charge_write, |
| 5020 | }, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5021 | { |
| 5022 | .name = "oom_control", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5023 | .seq_show = mem_cgroup_oom_control_read, |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 5024 | .write_u64 = mem_cgroup_oom_control_write, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5025 | .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL), |
| 5026 | }, |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 5027 | { |
| 5028 | .name = "pressure_level", |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 5029 | }, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5030 | #ifdef CONFIG_NUMA |
| 5031 | { |
| 5032 | .name = "numa_stat", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5033 | .seq_show = memcg_numa_stat_show, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5034 | }, |
| 5035 | #endif |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5036 | #ifdef CONFIG_MEMCG_KMEM |
| 5037 | { |
| 5038 | .name = "kmem.limit_in_bytes", |
| 5039 | .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT), |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5040 | .write = mem_cgroup_write, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5041 | .read_u64 = mem_cgroup_read_u64, |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5042 | }, |
| 5043 | { |
| 5044 | .name = "kmem.usage_in_bytes", |
| 5045 | .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE), |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5046 | .read_u64 = mem_cgroup_read_u64, |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5047 | }, |
| 5048 | { |
| 5049 | .name = "kmem.failcnt", |
| 5050 | .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT), |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5051 | .write = mem_cgroup_reset, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5052 | .read_u64 = mem_cgroup_read_u64, |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5053 | }, |
| 5054 | { |
| 5055 | .name = "kmem.max_usage_in_bytes", |
| 5056 | .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE), |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5057 | .write = mem_cgroup_reset, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5058 | .read_u64 = mem_cgroup_read_u64, |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5059 | }, |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 5060 | #ifdef CONFIG_SLABINFO |
| 5061 | { |
| 5062 | .name = "kmem.slabinfo", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5063 | .seq_show = mem_cgroup_slabinfo_read, |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 5064 | }, |
| 5065 | #endif |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5066 | #endif |
Tejun Heo | 6bc1034 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5067 | { }, /* terminate */ |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5068 | }; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5069 | |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 5070 | #ifdef CONFIG_MEMCG_SWAP |
| 5071 | static struct cftype memsw_cgroup_files[] = { |
| 5072 | { |
| 5073 | .name = "memsw.usage_in_bytes", |
| 5074 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5075 | .read_u64 = mem_cgroup_read_u64, |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 5076 | }, |
| 5077 | { |
| 5078 | .name = "memsw.max_usage_in_bytes", |
| 5079 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5080 | .write = mem_cgroup_reset, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5081 | .read_u64 = mem_cgroup_read_u64, |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 5082 | }, |
| 5083 | { |
| 5084 | .name = "memsw.limit_in_bytes", |
| 5085 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5086 | .write = mem_cgroup_write, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5087 | .read_u64 = mem_cgroup_read_u64, |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 5088 | }, |
| 5089 | { |
| 5090 | .name = "memsw.failcnt", |
| 5091 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5092 | .write = mem_cgroup_reset, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5093 | .read_u64 = mem_cgroup_read_u64, |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 5094 | }, |
| 5095 | { }, /* terminate */ |
| 5096 | }; |
| 5097 | #endif |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5098 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5099 | { |
| 5100 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5101 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 5102 | int zone, tmp = node; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5103 | /* |
| 5104 | * This routine is called against possible nodes. |
| 5105 | * But it's BUG to call kmalloc() against offline node. |
| 5106 | * |
| 5107 | * TODO: this routine can waste much memory for nodes which will |
| 5108 | * never be onlined. It's better to use memory hotplug callback |
| 5109 | * function. |
| 5110 | */ |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 5111 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 5112 | tmp = -1; |
Jesper Juhl | 17295c8 | 2011-01-13 15:47:42 -0800 | [diff] [blame] | 5113 | pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5114 | if (!pn) |
| 5115 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5116 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5117 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 5118 | mz = &pn->zoneinfo[zone]; |
Hugh Dickins | bea8c15 | 2012-11-16 14:14:54 -0800 | [diff] [blame] | 5119 | lruvec_init(&mz->lruvec); |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 5120 | mz->usage_in_excess = 0; |
| 5121 | mz->on_tree = false; |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5122 | mz->memcg = memcg; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5123 | } |
Johannes Weiner | 54f72fe | 2013-07-08 15:59:49 -0700 | [diff] [blame] | 5124 | memcg->nodeinfo[node] = pn; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5125 | return 0; |
| 5126 | } |
| 5127 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5128 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node) |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5129 | { |
Johannes Weiner | 54f72fe | 2013-07-08 15:59:49 -0700 | [diff] [blame] | 5130 | kfree(memcg->nodeinfo[node]); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5131 | } |
| 5132 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5133 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 5134 | { |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5135 | struct mem_cgroup *memcg; |
Vladimir Davydov | 8ff69e2 | 2014-01-23 15:52:52 -0800 | [diff] [blame] | 5136 | size_t size; |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5137 | |
Vladimir Davydov | 8ff69e2 | 2014-01-23 15:52:52 -0800 | [diff] [blame] | 5138 | size = sizeof(struct mem_cgroup); |
| 5139 | size += nr_node_ids * sizeof(struct mem_cgroup_per_node *); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5140 | |
Vladimir Davydov | 8ff69e2 | 2014-01-23 15:52:52 -0800 | [diff] [blame] | 5141 | memcg = kzalloc(size, GFP_KERNEL); |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5142 | if (!memcg) |
Dan Carpenter | e7bbcdf | 2010-03-23 13:35:12 -0700 | [diff] [blame] | 5143 | return NULL; |
| 5144 | |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5145 | memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu); |
| 5146 | if (!memcg->stat) |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 5147 | goto out_free; |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5148 | spin_lock_init(&memcg->pcp_counter_lock); |
| 5149 | return memcg; |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 5150 | |
| 5151 | out_free: |
Vladimir Davydov | 8ff69e2 | 2014-01-23 15:52:52 -0800 | [diff] [blame] | 5152 | kfree(memcg); |
Dan Carpenter | d2e61b8 | 2010-11-11 14:05:12 -0800 | [diff] [blame] | 5153 | return NULL; |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5154 | } |
| 5155 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5156 | /* |
Glauber Costa | c8b2a36 | 2012-12-18 14:22:13 -0800 | [diff] [blame] | 5157 | * At destroying mem_cgroup, references from swap_cgroup can remain. |
| 5158 | * (scanning all at force_empty is too costly...) |
| 5159 | * |
| 5160 | * Instead of clearing all references at force_empty, we remember |
| 5161 | * the number of reference from swap_cgroup and free mem_cgroup when |
| 5162 | * it goes down to 0. |
| 5163 | * |
| 5164 | * Removal of cgroup itself succeeds regardless of refs from swap. |
Hugh Dickins | 59927fb | 2012-03-15 15:17:07 -0700 | [diff] [blame] | 5165 | */ |
Glauber Costa | c8b2a36 | 2012-12-18 14:22:13 -0800 | [diff] [blame] | 5166 | |
| 5167 | static void __mem_cgroup_free(struct mem_cgroup *memcg) |
Hugh Dickins | 59927fb | 2012-03-15 15:17:07 -0700 | [diff] [blame] | 5168 | { |
Glauber Costa | c8b2a36 | 2012-12-18 14:22:13 -0800 | [diff] [blame] | 5169 | int node; |
Hugh Dickins | 59927fb | 2012-03-15 15:17:07 -0700 | [diff] [blame] | 5170 | |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 5171 | mem_cgroup_remove_from_trees(memcg); |
Glauber Costa | c8b2a36 | 2012-12-18 14:22:13 -0800 | [diff] [blame] | 5172 | |
| 5173 | for_each_node(node) |
| 5174 | free_mem_cgroup_per_zone_info(memcg, node); |
| 5175 | |
| 5176 | free_percpu(memcg->stat); |
| 5177 | |
Glauber Costa | 3f13461 | 2012-05-29 15:07:11 -0700 | [diff] [blame] | 5178 | /* |
| 5179 | * We need to make sure that (at least for now), the jump label |
| 5180 | * destruction code runs outside of the cgroup lock. This is because |
| 5181 | * get_online_cpus(), which is called from the static_branch update, |
| 5182 | * can't be called inside the cgroup_lock. cpusets are the ones |
| 5183 | * enforcing this dependency, so if they ever change, we might as well. |
| 5184 | * |
| 5185 | * schedule_work() will guarantee this happens. Be careful if you need |
| 5186 | * to move this code around, and make sure it is outside |
| 5187 | * the cgroup_lock. |
| 5188 | */ |
Glauber Costa | a8964b9 | 2012-12-18 14:22:09 -0800 | [diff] [blame] | 5189 | disarm_static_keys(memcg); |
Vladimir Davydov | 8ff69e2 | 2014-01-23 15:52:52 -0800 | [diff] [blame] | 5190 | kfree(memcg); |
Hugh Dickins | 59927fb | 2012-03-15 15:17:07 -0700 | [diff] [blame] | 5191 | } |
Glauber Costa | 3afe36b | 2012-05-29 15:07:10 -0700 | [diff] [blame] | 5192 | |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5193 | /* |
| 5194 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. |
| 5195 | */ |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 5196 | struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg) |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5197 | { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 5198 | if (!memcg->memory.parent) |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5199 | return NULL; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 5200 | return mem_cgroup_from_counter(memcg->memory.parent, memory); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5201 | } |
Glauber Costa | e1aab16 | 2011-12-11 21:47:03 +0000 | [diff] [blame] | 5202 | EXPORT_SYMBOL(parent_mem_cgroup); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5203 | |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 5204 | static void __init mem_cgroup_soft_limit_tree_init(void) |
| 5205 | { |
| 5206 | struct mem_cgroup_tree_per_node *rtpn; |
| 5207 | struct mem_cgroup_tree_per_zone *rtpz; |
| 5208 | int tmp, node, zone; |
| 5209 | |
| 5210 | for_each_node(node) { |
| 5211 | tmp = node; |
| 5212 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 5213 | tmp = -1; |
| 5214 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp); |
| 5215 | BUG_ON(!rtpn); |
| 5216 | |
| 5217 | soft_limit_tree.rb_tree_per_node[node] = rtpn; |
| 5218 | |
| 5219 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 5220 | rtpz = &rtpn->rb_tree_per_zone[zone]; |
| 5221 | rtpz->rb_root = RB_ROOT; |
| 5222 | spin_lock_init(&rtpz->lock); |
| 5223 | } |
| 5224 | } |
| 5225 | } |
| 5226 | |
Li Zefan | 0eb253e | 2009-01-15 13:51:25 -0800 | [diff] [blame] | 5227 | static struct cgroup_subsys_state * __ref |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5228 | mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5229 | { |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 5230 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5231 | long error = -ENOMEM; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5232 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5233 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5234 | memcg = mem_cgroup_alloc(); |
| 5235 | if (!memcg) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 5236 | return ERR_PTR(error); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 5237 | |
Bob Liu | 3ed28fa | 2012-01-12 17:19:04 -0800 | [diff] [blame] | 5238 | for_each_node(node) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5239 | if (alloc_mem_cgroup_per_zone_info(memcg, node)) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5240 | goto free_out; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5241 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 5242 | /* root ? */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5243 | if (parent_css == NULL) { |
Hillf Danton | a41c58a | 2011-12-19 17:11:57 -0800 | [diff] [blame] | 5244 | root_mem_cgroup = memcg; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 5245 | page_counter_init(&memcg->memory, NULL); |
| 5246 | page_counter_init(&memcg->memsw, NULL); |
| 5247 | page_counter_init(&memcg->kmem, NULL); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5248 | } |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5249 | |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 5250 | memcg->last_scanned_node = MAX_NUMNODES; |
| 5251 | INIT_LIST_HEAD(&memcg->oom_notify); |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 5252 | memcg->move_charge_at_immigrate = 0; |
| 5253 | mutex_init(&memcg->thresholds_lock); |
| 5254 | spin_lock_init(&memcg->move_lock); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 5255 | vmpressure_init(&memcg->vmpressure); |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 5256 | INIT_LIST_HEAD(&memcg->event_list); |
| 5257 | spin_lock_init(&memcg->event_list_lock); |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 5258 | |
| 5259 | return &memcg->css; |
| 5260 | |
| 5261 | free_out: |
| 5262 | __mem_cgroup_free(memcg); |
| 5263 | return ERR_PTR(error); |
| 5264 | } |
| 5265 | |
| 5266 | static int |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5267 | mem_cgroup_css_online(struct cgroup_subsys_state *css) |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 5268 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5269 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5270 | struct mem_cgroup *parent = mem_cgroup_from_css(css->parent); |
Johannes Weiner | 2f7dd7a | 2014-10-02 16:16:57 -0700 | [diff] [blame] | 5271 | int ret; |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 5272 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5273 | if (css->id > MEM_CGROUP_ID_MAX) |
Li Zefan | 4219b2d | 2013-09-23 16:56:29 +0800 | [diff] [blame] | 5274 | return -ENOSPC; |
| 5275 | |
Tejun Heo | 6387698 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5276 | if (!parent) |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 5277 | return 0; |
| 5278 | |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 5279 | mutex_lock(&memcg_create_mutex); |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 5280 | |
| 5281 | memcg->use_hierarchy = parent->use_hierarchy; |
| 5282 | memcg->oom_kill_disable = parent->oom_kill_disable; |
| 5283 | memcg->swappiness = mem_cgroup_swappiness(parent); |
| 5284 | |
| 5285 | if (parent->use_hierarchy) { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 5286 | page_counter_init(&memcg->memory, &parent->memory); |
| 5287 | page_counter_init(&memcg->memsw, &parent->memsw); |
| 5288 | page_counter_init(&memcg->kmem, &parent->kmem); |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 5289 | |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5290 | /* |
Li Zefan | 8d76a97 | 2013-07-08 16:00:36 -0700 | [diff] [blame] | 5291 | * No need to take a reference to the parent because cgroup |
| 5292 | * core guarantees its existence. |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 5293 | */ |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5294 | } else { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 5295 | page_counter_init(&memcg->memory, NULL); |
| 5296 | page_counter_init(&memcg->memsw, NULL); |
| 5297 | page_counter_init(&memcg->kmem, NULL); |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 5298 | /* |
| 5299 | * Deeper hierachy with use_hierarchy == false doesn't make |
| 5300 | * much sense so let cgroup subsystem know about this |
| 5301 | * unfortunate state in our controller. |
| 5302 | */ |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 5303 | if (parent != root_mem_cgroup) |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5304 | memory_cgrp_subsys.broken_hierarchy = true; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5305 | } |
Glauber Costa | 0999821 | 2013-02-22 16:34:55 -0800 | [diff] [blame] | 5306 | mutex_unlock(&memcg_create_mutex); |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 5307 | |
Johannes Weiner | 2f7dd7a | 2014-10-02 16:16:57 -0700 | [diff] [blame] | 5308 | ret = memcg_init_kmem(memcg, &memory_cgrp_subsys); |
| 5309 | if (ret) |
| 5310 | return ret; |
| 5311 | |
| 5312 | /* |
| 5313 | * Make sure the memcg is initialized: mem_cgroup_iter() |
| 5314 | * orders reading memcg->initialized against its callers |
| 5315 | * reading the memcg members. |
| 5316 | */ |
| 5317 | smp_store_release(&memcg->initialized, 1); |
| 5318 | |
| 5319 | return 0; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5320 | } |
| 5321 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5322 | static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5323 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5324 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 5325 | struct mem_cgroup_event *event, *tmp; |
Filipe Brandenburger | 4fb1a86 | 2014-03-03 15:38:25 -0800 | [diff] [blame] | 5326 | struct cgroup_subsys_state *iter; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5327 | |
| 5328 | /* |
| 5329 | * Unregister events and notify userspace. |
| 5330 | * Notify userspace about cgroup removing only after rmdir of cgroup |
| 5331 | * directory to avoid race between userspace and kernelspace. |
| 5332 | */ |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 5333 | spin_lock(&memcg->event_list_lock); |
| 5334 | list_for_each_entry_safe(event, tmp, &memcg->event_list, list) { |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5335 | list_del_init(&event->list); |
| 5336 | schedule_work(&event->remove); |
| 5337 | } |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 5338 | spin_unlock(&memcg->event_list_lock); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 5339 | |
Filipe Brandenburger | 4fb1a86 | 2014-03-03 15:38:25 -0800 | [diff] [blame] | 5340 | /* |
| 5341 | * This requires that offlining is serialized. Right now that is |
| 5342 | * guaranteed because css_killed_work_fn() holds the cgroup_mutex. |
| 5343 | */ |
| 5344 | css_for_each_descendant_post(iter, css) |
| 5345 | mem_cgroup_reparent_charges(mem_cgroup_from_css(iter)); |
| 5346 | |
Vladimir Davydov | 776ed0f | 2014-06-04 16:10:02 -0700 | [diff] [blame] | 5347 | memcg_unregister_all_caches(memcg); |
Michal Hocko | 33cb876 | 2013-07-31 13:53:51 -0700 | [diff] [blame] | 5348 | vmpressure_cleanup(&memcg->vmpressure); |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5349 | } |
| 5350 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5351 | static void mem_cgroup_css_free(struct cgroup_subsys_state *css) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5352 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5353 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Johannes Weiner | 96f1c58 | 2013-12-12 17:12:34 -0800 | [diff] [blame] | 5354 | /* |
| 5355 | * XXX: css_offline() would be where we should reparent all |
| 5356 | * memory to prepare the cgroup for destruction. However, |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 5357 | * memcg does not do css_tryget_online() and page_counter charging |
Johannes Weiner | 96f1c58 | 2013-12-12 17:12:34 -0800 | [diff] [blame] | 5358 | * under the same RCU lock region, which means that charging |
| 5359 | * could race with offlining. Offlining only happens to |
| 5360 | * cgroups with no tasks in them but charges can show up |
| 5361 | * without any tasks from the swapin path when the target |
| 5362 | * memcg is looked up from the swapout record and not from the |
| 5363 | * current task as it usually is. A race like this can leak |
| 5364 | * charges and put pages with stale cgroup pointers into |
| 5365 | * circulation: |
| 5366 | * |
| 5367 | * #0 #1 |
| 5368 | * lookup_swap_cgroup_id() |
| 5369 | * rcu_read_lock() |
| 5370 | * mem_cgroup_lookup() |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5371 | * css_tryget_online() |
Johannes Weiner | 96f1c58 | 2013-12-12 17:12:34 -0800 | [diff] [blame] | 5372 | * rcu_read_unlock() |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5373 | * disable css_tryget_online() |
Johannes Weiner | 96f1c58 | 2013-12-12 17:12:34 -0800 | [diff] [blame] | 5374 | * call_rcu() |
| 5375 | * offline_css() |
| 5376 | * reparent_charges() |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 5377 | * page_counter_try_charge() |
Johannes Weiner | 96f1c58 | 2013-12-12 17:12:34 -0800 | [diff] [blame] | 5378 | * css_put() |
| 5379 | * css_free() |
| 5380 | * pc->mem_cgroup = dead memcg |
| 5381 | * add page to lru |
| 5382 | * |
| 5383 | * The bulk of the charges are still moved in offline_css() to |
| 5384 | * avoid pinning a lot of pages in case a long-term reference |
| 5385 | * like a swapout record is deferring the css_free() to long |
| 5386 | * after offlining. But this makes sure we catch any charges |
| 5387 | * made after offlining: |
| 5388 | */ |
| 5389 | mem_cgroup_reparent_charges(memcg); |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 5390 | |
Li Zefan | 10d5ebf | 2013-07-08 16:00:33 -0700 | [diff] [blame] | 5391 | memcg_destroy_kmem(memcg); |
Li Zefan | 465939a | 2013-07-08 16:00:38 -0700 | [diff] [blame] | 5392 | __mem_cgroup_free(memcg); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5393 | } |
| 5394 | |
Tejun Heo | 1ced953 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 5395 | /** |
| 5396 | * mem_cgroup_css_reset - reset the states of a mem_cgroup |
| 5397 | * @css: the target css |
| 5398 | * |
| 5399 | * Reset the states of the mem_cgroup associated with @css. This is |
| 5400 | * invoked when the userland requests disabling on the default hierarchy |
| 5401 | * but the memcg is pinned through dependency. The memcg should stop |
| 5402 | * applying policies and should revert to the vanilla state as it may be |
| 5403 | * made visible again. |
| 5404 | * |
| 5405 | * The current implementation only resets the essential configurations. |
| 5406 | * This needs to be expanded to cover all the visible parts. |
| 5407 | */ |
| 5408 | static void mem_cgroup_css_reset(struct cgroup_subsys_state *css) |
| 5409 | { |
| 5410 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
| 5411 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 5412 | mem_cgroup_resize_limit(memcg, PAGE_COUNTER_MAX); |
| 5413 | mem_cgroup_resize_memsw_limit(memcg, PAGE_COUNTER_MAX); |
| 5414 | memcg_update_kmem_limit(memcg, PAGE_COUNTER_MAX); |
| 5415 | memcg->soft_limit = 0; |
Tejun Heo | 1ced953 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 5416 | } |
| 5417 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5418 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5419 | /* Handlers for move charge at task migration. */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5420 | static int mem_cgroup_do_precharge(unsigned long count) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5421 | { |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 5422 | int ret; |
Johannes Weiner | 9476db9 | 2014-08-06 16:05:55 -0700 | [diff] [blame] | 5423 | |
| 5424 | /* Try a single bulk charge without reclaim first */ |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 5425 | ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_WAIT, count); |
Johannes Weiner | 9476db9 | 2014-08-06 16:05:55 -0700 | [diff] [blame] | 5426 | if (!ret) { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5427 | mc.precharge += count; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5428 | return ret; |
| 5429 | } |
Johannes Weiner | 692e7c4 | 2014-08-06 16:05:57 -0700 | [diff] [blame] | 5430 | if (ret == -EINTR) { |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 5431 | cancel_charge(root_mem_cgroup, count); |
Johannes Weiner | 692e7c4 | 2014-08-06 16:05:57 -0700 | [diff] [blame] | 5432 | return ret; |
| 5433 | } |
Johannes Weiner | 9476db9 | 2014-08-06 16:05:55 -0700 | [diff] [blame] | 5434 | |
| 5435 | /* Try charges one by one with reclaim */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5436 | while (count--) { |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 5437 | ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_NORETRY, 1); |
Johannes Weiner | 9476db9 | 2014-08-06 16:05:55 -0700 | [diff] [blame] | 5438 | /* |
| 5439 | * In case of failure, any residual charges against |
| 5440 | * mc.to will be dropped by mem_cgroup_clear_mc() |
Johannes Weiner | 692e7c4 | 2014-08-06 16:05:57 -0700 | [diff] [blame] | 5441 | * later on. However, cancel any charges that are |
| 5442 | * bypassed to root right away or they'll be lost. |
Johannes Weiner | 9476db9 | 2014-08-06 16:05:55 -0700 | [diff] [blame] | 5443 | */ |
Johannes Weiner | 692e7c4 | 2014-08-06 16:05:57 -0700 | [diff] [blame] | 5444 | if (ret == -EINTR) |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 5445 | cancel_charge(root_mem_cgroup, 1); |
KAMEZAWA Hiroyuki | 38c5d72 | 2012-01-12 17:19:01 -0800 | [diff] [blame] | 5446 | if (ret) |
KAMEZAWA Hiroyuki | 38c5d72 | 2012-01-12 17:19:01 -0800 | [diff] [blame] | 5447 | return ret; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5448 | mc.precharge++; |
Johannes Weiner | 9476db9 | 2014-08-06 16:05:55 -0700 | [diff] [blame] | 5449 | cond_resched(); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5450 | } |
Johannes Weiner | 9476db9 | 2014-08-06 16:05:55 -0700 | [diff] [blame] | 5451 | return 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5452 | } |
| 5453 | |
| 5454 | /** |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 5455 | * get_mctgt_type - get target type of moving charge |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5456 | * @vma: the vma the pte to be checked belongs |
| 5457 | * @addr: the address corresponding to the pte to be checked |
| 5458 | * @ptent: the pte to be checked |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5459 | * @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] | 5460 | * |
| 5461 | * Returns |
| 5462 | * 0(MC_TARGET_NONE): if the pte is not a target for move charge. |
| 5463 | * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for |
| 5464 | * move charge. if @target is not NULL, the page is stored in target->page |
| 5465 | * with extra refcnt got(Callers should handle it). |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5466 | * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a |
| 5467 | * target for charge migration. if @target is not NULL, the entry is stored |
| 5468 | * in target->ent. |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5469 | * |
| 5470 | * Called with pte lock held. |
| 5471 | */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5472 | union mc_target { |
| 5473 | struct page *page; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5474 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5475 | }; |
| 5476 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5477 | enum mc_target_type { |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 5478 | MC_TARGET_NONE = 0, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5479 | MC_TARGET_PAGE, |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5480 | MC_TARGET_SWAP, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5481 | }; |
| 5482 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5483 | static struct page *mc_handle_present_pte(struct vm_area_struct *vma, |
| 5484 | unsigned long addr, pte_t ptent) |
| 5485 | { |
| 5486 | struct page *page = vm_normal_page(vma, addr, ptent); |
| 5487 | |
| 5488 | if (!page || !page_mapped(page)) |
| 5489 | return NULL; |
| 5490 | if (PageAnon(page)) { |
| 5491 | /* we don't move shared anon */ |
KAMEZAWA Hiroyuki | 4b91355 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 5492 | if (!move_anon()) |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5493 | return NULL; |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5494 | } else if (!move_file()) |
| 5495 | /* we ignore mapcount for file pages */ |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5496 | return NULL; |
| 5497 | if (!get_page_unless_zero(page)) |
| 5498 | return NULL; |
| 5499 | |
| 5500 | return page; |
| 5501 | } |
| 5502 | |
KAMEZAWA Hiroyuki | 4b91355 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 5503 | #ifdef CONFIG_SWAP |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5504 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
| 5505 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5506 | { |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5507 | struct page *page = NULL; |
| 5508 | swp_entry_t ent = pte_to_swp_entry(ptent); |
| 5509 | |
| 5510 | if (!move_anon() || non_swap_entry(ent)) |
| 5511 | return NULL; |
KAMEZAWA Hiroyuki | 4b91355 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 5512 | /* |
| 5513 | * Because lookup_swap_cache() updates some statistics counter, |
| 5514 | * we call find_get_page() with swapper_space directly. |
| 5515 | */ |
Shaohua Li | 33806f0 | 2013-02-22 16:34:37 -0800 | [diff] [blame] | 5516 | page = find_get_page(swap_address_space(ent), ent.val); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5517 | if (do_swap_account) |
| 5518 | entry->val = ent.val; |
| 5519 | |
| 5520 | return page; |
| 5521 | } |
KAMEZAWA Hiroyuki | 4b91355 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 5522 | #else |
| 5523 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
| 5524 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5525 | { |
| 5526 | return NULL; |
| 5527 | } |
| 5528 | #endif |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5529 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5530 | static struct page *mc_handle_file_pte(struct vm_area_struct *vma, |
| 5531 | unsigned long addr, pte_t ptent, swp_entry_t *entry) |
| 5532 | { |
| 5533 | struct page *page = NULL; |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5534 | struct address_space *mapping; |
| 5535 | pgoff_t pgoff; |
| 5536 | |
| 5537 | if (!vma->vm_file) /* anonymous vma */ |
| 5538 | return NULL; |
| 5539 | if (!move_file()) |
| 5540 | return NULL; |
| 5541 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5542 | mapping = vma->vm_file->f_mapping; |
| 5543 | if (pte_none(ptent)) |
| 5544 | pgoff = linear_page_index(vma, addr); |
| 5545 | else /* pte_file(ptent) is true */ |
| 5546 | pgoff = pte_to_pgoff(ptent); |
| 5547 | |
| 5548 | /* page is moved even if it's not RSS of this task(page-faulted). */ |
Hugh Dickins | aa3b189 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 5549 | #ifdef CONFIG_SWAP |
| 5550 | /* shmem/tmpfs may report page out on swap: account for that too. */ |
Johannes Weiner | 139b6a6 | 2014-05-06 12:50:05 -0700 | [diff] [blame] | 5551 | if (shmem_mapping(mapping)) { |
| 5552 | page = find_get_entry(mapping, pgoff); |
| 5553 | if (radix_tree_exceptional_entry(page)) { |
| 5554 | swp_entry_t swp = radix_to_swp_entry(page); |
| 5555 | if (do_swap_account) |
| 5556 | *entry = swp; |
| 5557 | page = find_get_page(swap_address_space(swp), swp.val); |
| 5558 | } |
| 5559 | } else |
| 5560 | page = find_get_page(mapping, pgoff); |
| 5561 | #else |
| 5562 | page = find_get_page(mapping, pgoff); |
Hugh Dickins | aa3b189 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 5563 | #endif |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5564 | return page; |
| 5565 | } |
| 5566 | |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 5567 | static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5568 | unsigned long addr, pte_t ptent, union mc_target *target) |
| 5569 | { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5570 | struct page *page = NULL; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5571 | struct page_cgroup *pc; |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 5572 | enum mc_target_type ret = MC_TARGET_NONE; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5573 | swp_entry_t ent = { .val = 0 }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5574 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5575 | if (pte_present(ptent)) |
| 5576 | page = mc_handle_present_pte(vma, addr, ptent); |
| 5577 | else if (is_swap_pte(ptent)) |
| 5578 | page = mc_handle_swap_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5579 | else if (pte_none(ptent) || pte_file(ptent)) |
| 5580 | page = mc_handle_file_pte(vma, addr, ptent, &ent); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5581 | |
| 5582 | if (!page && !ent.val) |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 5583 | return ret; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5584 | if (page) { |
| 5585 | pc = lookup_page_cgroup(page); |
| 5586 | /* |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 5587 | * Do only loose check w/o serialization. |
| 5588 | * mem_cgroup_move_account() checks the pc is valid or |
| 5589 | * not under LRU exclusion. |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5590 | */ |
| 5591 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { |
| 5592 | ret = MC_TARGET_PAGE; |
| 5593 | if (target) |
| 5594 | target->page = page; |
| 5595 | } |
| 5596 | if (!ret || !target) |
| 5597 | put_page(page); |
| 5598 | } |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5599 | /* There is a swap entry and a page doesn't exist or isn't charged */ |
| 5600 | if (ent.val && !ret && |
Li Zefan | 34c00c3 | 2013-09-23 16:56:01 +0800 | [diff] [blame] | 5601 | mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5602 | ret = MC_TARGET_SWAP; |
| 5603 | if (target) |
| 5604 | target->ent = ent; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5605 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5606 | return ret; |
| 5607 | } |
| 5608 | |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 5609 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 5610 | /* |
| 5611 | * We don't consider swapping or file mapped pages because THP does not |
| 5612 | * support them for now. |
| 5613 | * Caller should make sure that pmd_trans_huge(pmd) is true. |
| 5614 | */ |
| 5615 | static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma, |
| 5616 | unsigned long addr, pmd_t pmd, union mc_target *target) |
| 5617 | { |
| 5618 | struct page *page = NULL; |
| 5619 | struct page_cgroup *pc; |
| 5620 | enum mc_target_type ret = MC_TARGET_NONE; |
| 5621 | |
| 5622 | page = pmd_page(pmd); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 5623 | VM_BUG_ON_PAGE(!page || !PageHead(page), page); |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 5624 | if (!move_anon()) |
| 5625 | return ret; |
| 5626 | pc = lookup_page_cgroup(page); |
| 5627 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { |
| 5628 | ret = MC_TARGET_PAGE; |
| 5629 | if (target) { |
| 5630 | get_page(page); |
| 5631 | target->page = page; |
| 5632 | } |
| 5633 | } |
| 5634 | return ret; |
| 5635 | } |
| 5636 | #else |
| 5637 | static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma, |
| 5638 | unsigned long addr, pmd_t pmd, union mc_target *target) |
| 5639 | { |
| 5640 | return MC_TARGET_NONE; |
| 5641 | } |
| 5642 | #endif |
| 5643 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5644 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, |
| 5645 | unsigned long addr, unsigned long end, |
| 5646 | struct mm_walk *walk) |
| 5647 | { |
| 5648 | struct vm_area_struct *vma = walk->private; |
| 5649 | pte_t *pte; |
| 5650 | spinlock_t *ptl; |
| 5651 | |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 5652 | if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) { |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 5653 | if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE) |
| 5654 | mc.precharge += HPAGE_PMD_NR; |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 5655 | spin_unlock(ptl); |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 5656 | return 0; |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 5657 | } |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 5658 | |
Andrea Arcangeli | 45f83ce | 2012-03-28 14:42:40 -0700 | [diff] [blame] | 5659 | if (pmd_trans_unstable(pmd)) |
| 5660 | return 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5661 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5662 | for (; addr != end; pte++, addr += PAGE_SIZE) |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 5663 | if (get_mctgt_type(vma, addr, *pte, NULL)) |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5664 | mc.precharge++; /* increment precharge temporarily */ |
| 5665 | pte_unmap_unlock(pte - 1, ptl); |
| 5666 | cond_resched(); |
| 5667 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5668 | return 0; |
| 5669 | } |
| 5670 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5671 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
| 5672 | { |
| 5673 | unsigned long precharge; |
| 5674 | struct vm_area_struct *vma; |
| 5675 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5676 | down_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5677 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5678 | struct mm_walk mem_cgroup_count_precharge_walk = { |
| 5679 | .pmd_entry = mem_cgroup_count_precharge_pte_range, |
| 5680 | .mm = mm, |
| 5681 | .private = vma, |
| 5682 | }; |
| 5683 | if (is_vm_hugetlb_page(vma)) |
| 5684 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5685 | walk_page_range(vma->vm_start, vma->vm_end, |
| 5686 | &mem_cgroup_count_precharge_walk); |
| 5687 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5688 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5689 | |
| 5690 | precharge = mc.precharge; |
| 5691 | mc.precharge = 0; |
| 5692 | |
| 5693 | return precharge; |
| 5694 | } |
| 5695 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5696 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
| 5697 | { |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5698 | unsigned long precharge = mem_cgroup_count_precharge(mm); |
| 5699 | |
| 5700 | VM_BUG_ON(mc.moving_task); |
| 5701 | mc.moving_task = current; |
| 5702 | return mem_cgroup_do_precharge(precharge); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5703 | } |
| 5704 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5705 | /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */ |
| 5706 | static void __mem_cgroup_clear_mc(void) |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5707 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5708 | struct mem_cgroup *from = mc.from; |
| 5709 | struct mem_cgroup *to = mc.to; |
| 5710 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5711 | /* we must uncharge all the leftover precharges from mc.to */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5712 | if (mc.precharge) { |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 5713 | cancel_charge(mc.to, mc.precharge); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5714 | mc.precharge = 0; |
| 5715 | } |
| 5716 | /* |
| 5717 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so |
| 5718 | * we must uncharge here. |
| 5719 | */ |
| 5720 | if (mc.moved_charge) { |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 5721 | cancel_charge(mc.from, mc.moved_charge); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5722 | mc.moved_charge = 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5723 | } |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5724 | /* we must fixup refcnts and charges */ |
| 5725 | if (mc.moved_swap) { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5726 | /* uncharge swap account from the old cgroup */ |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 5727 | if (!mem_cgroup_is_root(mc.from)) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 5728 | page_counter_uncharge(&mc.from->memsw, mc.moved_swap); |
| 5729 | |
| 5730 | /* |
| 5731 | * we charged both to->memory and to->memsw, so we |
| 5732 | * should uncharge to->memory. |
| 5733 | */ |
| 5734 | if (!mem_cgroup_is_root(mc.to)) |
| 5735 | page_counter_uncharge(&mc.to->memory, mc.moved_swap); |
Li Zefan | 4050377 | 2013-07-08 16:00:34 -0700 | [diff] [blame] | 5736 | |
Johannes Weiner | e8ea14c | 2014-12-10 15:42:42 -0800 | [diff] [blame] | 5737 | css_put_many(&mc.from->css, mc.moved_swap); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5738 | |
Li Zefan | 4050377 | 2013-07-08 16:00:34 -0700 | [diff] [blame] | 5739 | /* we've already done css_get(mc.to) */ |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5740 | mc.moved_swap = 0; |
| 5741 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5742 | memcg_oom_recover(from); |
| 5743 | memcg_oom_recover(to); |
| 5744 | wake_up_all(&mc.waitq); |
| 5745 | } |
| 5746 | |
| 5747 | static void mem_cgroup_clear_mc(void) |
| 5748 | { |
| 5749 | struct mem_cgroup *from = mc.from; |
| 5750 | |
| 5751 | /* |
| 5752 | * we must clear moving_task before waking up waiters at the end of |
| 5753 | * task migration. |
| 5754 | */ |
| 5755 | mc.moving_task = NULL; |
| 5756 | __mem_cgroup_clear_mc(); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5757 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5758 | mc.from = NULL; |
| 5759 | mc.to = NULL; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5760 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5761 | mem_cgroup_end_move(from); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5762 | } |
| 5763 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5764 | static int mem_cgroup_can_attach(struct cgroup_subsys_state *css, |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5765 | struct cgroup_taskset *tset) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5766 | { |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 5767 | struct task_struct *p = cgroup_taskset_first(tset); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5768 | int ret = 0; |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5769 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 5770 | unsigned long move_charge_at_immigrate; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5771 | |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 5772 | /* |
| 5773 | * We are now commited to this value whatever it is. Changes in this |
| 5774 | * tunable will only affect upcoming migrations, not the current one. |
| 5775 | * So we need to save it, and keep it going. |
| 5776 | */ |
| 5777 | move_charge_at_immigrate = memcg->move_charge_at_immigrate; |
| 5778 | if (move_charge_at_immigrate) { |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5779 | struct mm_struct *mm; |
| 5780 | struct mem_cgroup *from = mem_cgroup_from_task(p); |
| 5781 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5782 | VM_BUG_ON(from == memcg); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5783 | |
| 5784 | mm = get_task_mm(p); |
| 5785 | if (!mm) |
| 5786 | return 0; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5787 | /* We move charges only when we move a owner of the mm */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5788 | if (mm->owner == p) { |
| 5789 | VM_BUG_ON(mc.from); |
| 5790 | VM_BUG_ON(mc.to); |
| 5791 | VM_BUG_ON(mc.precharge); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5792 | VM_BUG_ON(mc.moved_charge); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5793 | VM_BUG_ON(mc.moved_swap); |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 5794 | mem_cgroup_start_move(from); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5795 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5796 | mc.from = from; |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 5797 | mc.to = memcg; |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 5798 | mc.immigrate_flags = move_charge_at_immigrate; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 5799 | spin_unlock(&mc.lock); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5800 | /* We set mc.moving_task later */ |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5801 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5802 | ret = mem_cgroup_precharge_mc(mm); |
| 5803 | if (ret) |
| 5804 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5805 | } |
| 5806 | mmput(mm); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5807 | } |
| 5808 | return ret; |
| 5809 | } |
| 5810 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5811 | static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css, |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5812 | struct cgroup_taskset *tset) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5813 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5814 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5815 | } |
| 5816 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5817 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
| 5818 | unsigned long addr, unsigned long end, |
| 5819 | struct mm_walk *walk) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5820 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5821 | int ret = 0; |
| 5822 | struct vm_area_struct *vma = walk->private; |
| 5823 | pte_t *pte; |
| 5824 | spinlock_t *ptl; |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 5825 | enum mc_target_type target_type; |
| 5826 | union mc_target target; |
| 5827 | struct page *page; |
| 5828 | struct page_cgroup *pc; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5829 | |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 5830 | /* |
| 5831 | * We don't take compound_lock() here but no race with splitting thp |
| 5832 | * happens because: |
| 5833 | * - if pmd_trans_huge_lock() returns 1, the relevant thp is not |
| 5834 | * under splitting, which means there's no concurrent thp split, |
| 5835 | * - if another thread runs into split_huge_page() just after we |
| 5836 | * entered this if-block, the thread must wait for page table lock |
| 5837 | * to be unlocked in __split_huge_page_splitting(), where the main |
| 5838 | * part of thp split is not executed yet. |
| 5839 | */ |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 5840 | if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) { |
Hugh Dickins | 62ade86 | 2012-05-18 11:28:34 -0700 | [diff] [blame] | 5841 | if (mc.precharge < HPAGE_PMD_NR) { |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 5842 | spin_unlock(ptl); |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 5843 | return 0; |
| 5844 | } |
| 5845 | target_type = get_mctgt_type_thp(vma, addr, *pmd, &target); |
| 5846 | if (target_type == MC_TARGET_PAGE) { |
| 5847 | page = target.page; |
| 5848 | if (!isolate_lru_page(page)) { |
| 5849 | pc = lookup_page_cgroup(page); |
| 5850 | if (!mem_cgroup_move_account(page, HPAGE_PMD_NR, |
KAMEZAWA Hiroyuki | 2f3479b | 2012-05-29 15:07:04 -0700 | [diff] [blame] | 5851 | pc, mc.from, mc.to)) { |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 5852 | mc.precharge -= HPAGE_PMD_NR; |
| 5853 | mc.moved_charge += HPAGE_PMD_NR; |
| 5854 | } |
| 5855 | putback_lru_page(page); |
| 5856 | } |
| 5857 | put_page(page); |
| 5858 | } |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 5859 | spin_unlock(ptl); |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 5860 | return 0; |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 5861 | } |
| 5862 | |
Andrea Arcangeli | 45f83ce | 2012-03-28 14:42:40 -0700 | [diff] [blame] | 5863 | if (pmd_trans_unstable(pmd)) |
| 5864 | return 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5865 | retry: |
| 5866 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 5867 | for (; addr != end; addr += PAGE_SIZE) { |
| 5868 | pte_t ptent = *(pte++); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5869 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5870 | |
| 5871 | if (!mc.precharge) |
| 5872 | break; |
| 5873 | |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 5874 | switch (get_mctgt_type(vma, addr, ptent, &target)) { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5875 | case MC_TARGET_PAGE: |
| 5876 | page = target.page; |
| 5877 | if (isolate_lru_page(page)) |
| 5878 | goto put; |
| 5879 | pc = lookup_page_cgroup(page); |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 5880 | if (!mem_cgroup_move_account(page, 1, pc, |
KAMEZAWA Hiroyuki | 2f3479b | 2012-05-29 15:07:04 -0700 | [diff] [blame] | 5881 | mc.from, mc.to)) { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5882 | mc.precharge--; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5883 | /* we uncharge from mc.from later. */ |
| 5884 | mc.moved_charge++; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5885 | } |
| 5886 | putback_lru_page(page); |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 5887 | put: /* get_mctgt_type() gets the page */ |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5888 | put_page(page); |
| 5889 | break; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5890 | case MC_TARGET_SWAP: |
| 5891 | ent = target.ent; |
Hugh Dickins | e91cbb4 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 5892 | if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5893 | mc.precharge--; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 5894 | /* we fixup refcnts and charges later. */ |
| 5895 | mc.moved_swap++; |
| 5896 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5897 | break; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5898 | default: |
| 5899 | break; |
| 5900 | } |
| 5901 | } |
| 5902 | pte_unmap_unlock(pte - 1, ptl); |
| 5903 | cond_resched(); |
| 5904 | |
| 5905 | if (addr != end) { |
| 5906 | /* |
| 5907 | * We have consumed all precharges we got in can_attach(). |
| 5908 | * We try charge one by one, but don't do any additional |
| 5909 | * charges to mc.to if we have failed in charge once in attach() |
| 5910 | * phase. |
| 5911 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5912 | ret = mem_cgroup_do_precharge(1); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5913 | if (!ret) |
| 5914 | goto retry; |
| 5915 | } |
| 5916 | |
| 5917 | return ret; |
| 5918 | } |
| 5919 | |
| 5920 | static void mem_cgroup_move_charge(struct mm_struct *mm) |
| 5921 | { |
| 5922 | struct vm_area_struct *vma; |
| 5923 | |
| 5924 | lru_add_drain_all(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5925 | retry: |
| 5926 | if (unlikely(!down_read_trylock(&mm->mmap_sem))) { |
| 5927 | /* |
| 5928 | * Someone who are holding the mmap_sem might be waiting in |
| 5929 | * waitq. So we cancel all extra charges, wake up all waiters, |
| 5930 | * and retry. Because we cancel precharges, we might not be able |
| 5931 | * to move enough charges, but moving charge is a best-effort |
| 5932 | * feature anyway, so it wouldn't be a big problem. |
| 5933 | */ |
| 5934 | __mem_cgroup_clear_mc(); |
| 5935 | cond_resched(); |
| 5936 | goto retry; |
| 5937 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5938 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 5939 | int ret; |
| 5940 | struct mm_walk mem_cgroup_move_charge_walk = { |
| 5941 | .pmd_entry = mem_cgroup_move_charge_pte_range, |
| 5942 | .mm = mm, |
| 5943 | .private = vma, |
| 5944 | }; |
| 5945 | if (is_vm_hugetlb_page(vma)) |
| 5946 | continue; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5947 | ret = walk_page_range(vma->vm_start, vma->vm_end, |
| 5948 | &mem_cgroup_move_charge_walk); |
| 5949 | if (ret) |
| 5950 | /* |
| 5951 | * means we have consumed all precharges and failed in |
| 5952 | * doing additional charge. Just abandon here. |
| 5953 | */ |
| 5954 | break; |
| 5955 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5956 | up_read(&mm->mmap_sem); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5957 | } |
| 5958 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5959 | static void mem_cgroup_move_task(struct cgroup_subsys_state *css, |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5960 | struct cgroup_taskset *tset) |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5961 | { |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 5962 | struct task_struct *p = cgroup_taskset_first(tset); |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5963 | struct mm_struct *mm = get_task_mm(p); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5964 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5965 | if (mm) { |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5966 | if (mc.to) |
| 5967 | mem_cgroup_move_charge(mm); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 5968 | mmput(mm); |
| 5969 | } |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 5970 | if (mc.to) |
| 5971 | mem_cgroup_clear_mc(); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5972 | } |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5973 | #else /* !CONFIG_MMU */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5974 | static int mem_cgroup_can_attach(struct cgroup_subsys_state *css, |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5975 | struct cgroup_taskset *tset) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5976 | { |
| 5977 | return 0; |
| 5978 | } |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5979 | static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css, |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5980 | struct cgroup_taskset *tset) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5981 | { |
| 5982 | } |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5983 | static void mem_cgroup_move_task(struct cgroup_subsys_state *css, |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5984 | struct cgroup_taskset *tset) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 5985 | { |
| 5986 | } |
| 5987 | #endif |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 5988 | |
Tejun Heo | f00baae | 2013-04-15 13:41:15 -0700 | [diff] [blame] | 5989 | /* |
| 5990 | * Cgroup retains root cgroups across [un]mount cycles making it necessary |
Tejun Heo | aa6ec29 | 2014-07-09 10:08:08 -0400 | [diff] [blame] | 5991 | * to verify whether we're attached to the default hierarchy on each mount |
| 5992 | * attempt. |
Tejun Heo | f00baae | 2013-04-15 13:41:15 -0700 | [diff] [blame] | 5993 | */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5994 | static void mem_cgroup_bind(struct cgroup_subsys_state *root_css) |
Tejun Heo | f00baae | 2013-04-15 13:41:15 -0700 | [diff] [blame] | 5995 | { |
| 5996 | /* |
Tejun Heo | aa6ec29 | 2014-07-09 10:08:08 -0400 | [diff] [blame] | 5997 | * use_hierarchy is forced on the default hierarchy. cgroup core |
Tejun Heo | f00baae | 2013-04-15 13:41:15 -0700 | [diff] [blame] | 5998 | * guarantees that @root doesn't have any children, so turning it |
| 5999 | * on for the root memcg is enough. |
| 6000 | */ |
Tejun Heo | aa6ec29 | 2014-07-09 10:08:08 -0400 | [diff] [blame] | 6001 | if (cgroup_on_dfl(root_css->cgroup)) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 6002 | mem_cgroup_from_css(root_css)->use_hierarchy = true; |
Tejun Heo | f00baae | 2013-04-15 13:41:15 -0700 | [diff] [blame] | 6003 | } |
| 6004 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 6005 | struct cgroup_subsys memory_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 6006 | .css_alloc = mem_cgroup_css_alloc, |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 6007 | .css_online = mem_cgroup_css_online, |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 6008 | .css_offline = mem_cgroup_css_offline, |
| 6009 | .css_free = mem_cgroup_css_free, |
Tejun Heo | 1ced953 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 6010 | .css_reset = mem_cgroup_css_reset, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6011 | .can_attach = mem_cgroup_can_attach, |
| 6012 | .cancel_attach = mem_cgroup_cancel_attach, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 6013 | .attach = mem_cgroup_move_task, |
Tejun Heo | f00baae | 2013-04-15 13:41:15 -0700 | [diff] [blame] | 6014 | .bind = mem_cgroup_bind, |
Tejun Heo | 5577964 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 6015 | .legacy_cftypes = mem_cgroup_files, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 6016 | .early_init = 0, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 6017 | }; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 6018 | |
Andrew Morton | c255a45 | 2012-07-31 16:43:02 -0700 | [diff] [blame] | 6019 | #ifdef CONFIG_MEMCG_SWAP |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 6020 | static int __init enable_swap_account(char *s) |
| 6021 | { |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 6022 | if (!strcmp(s, "1")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 6023 | really_do_swap_account = 1; |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 6024 | else if (!strcmp(s, "0")) |
Michal Hocko | a42c390 | 2010-11-24 12:57:08 -0800 | [diff] [blame] | 6025 | really_do_swap_account = 0; |
| 6026 | return 1; |
| 6027 | } |
Michal Hocko | a2c8990 | 2011-05-24 17:12:50 -0700 | [diff] [blame] | 6028 | __setup("swapaccount=", enable_swap_account); |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 6029 | |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 6030 | static void __init memsw_file_init(void) |
| 6031 | { |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 6032 | WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, |
| 6033 | memsw_cgroup_files)); |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 6034 | } |
Michal Hocko | 6acc8b0 | 2013-02-22 16:34:45 -0800 | [diff] [blame] | 6035 | |
| 6036 | static void __init enable_swap_cgroup(void) |
| 6037 | { |
| 6038 | if (!mem_cgroup_disabled() && really_do_swap_account) { |
| 6039 | do_swap_account = 1; |
| 6040 | memsw_file_init(); |
| 6041 | } |
| 6042 | } |
| 6043 | |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 6044 | #else |
Michal Hocko | 6acc8b0 | 2013-02-22 16:34:45 -0800 | [diff] [blame] | 6045 | static void __init enable_swap_cgroup(void) |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 6046 | { |
| 6047 | } |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 6048 | #endif |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 6049 | |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6050 | #ifdef CONFIG_MEMCG_SWAP |
| 6051 | /** |
| 6052 | * mem_cgroup_swapout - transfer a memsw charge to swap |
| 6053 | * @page: page whose memsw charge to transfer |
| 6054 | * @entry: swap entry to move the charge to |
| 6055 | * |
| 6056 | * Transfer the memsw charge of @page to @entry. |
| 6057 | */ |
| 6058 | void mem_cgroup_swapout(struct page *page, swp_entry_t entry) |
| 6059 | { |
| 6060 | struct page_cgroup *pc; |
| 6061 | unsigned short oldid; |
| 6062 | |
| 6063 | VM_BUG_ON_PAGE(PageLRU(page), page); |
| 6064 | VM_BUG_ON_PAGE(page_count(page), page); |
| 6065 | |
| 6066 | if (!do_swap_account) |
| 6067 | return; |
| 6068 | |
| 6069 | pc = lookup_page_cgroup(page); |
| 6070 | |
| 6071 | /* Readahead page, never charged */ |
| 6072 | if (!PageCgroupUsed(pc)) |
| 6073 | return; |
| 6074 | |
| 6075 | VM_BUG_ON_PAGE(!(pc->flags & PCG_MEMSW), page); |
| 6076 | |
| 6077 | oldid = swap_cgroup_record(entry, mem_cgroup_id(pc->mem_cgroup)); |
| 6078 | VM_BUG_ON_PAGE(oldid, page); |
| 6079 | |
| 6080 | pc->flags &= ~PCG_MEMSW; |
| 6081 | css_get(&pc->mem_cgroup->css); |
| 6082 | mem_cgroup_swap_statistics(pc->mem_cgroup, true); |
| 6083 | } |
| 6084 | |
| 6085 | /** |
| 6086 | * mem_cgroup_uncharge_swap - uncharge a swap entry |
| 6087 | * @entry: swap entry to uncharge |
| 6088 | * |
| 6089 | * Drop the memsw charge associated with @entry. |
| 6090 | */ |
| 6091 | void mem_cgroup_uncharge_swap(swp_entry_t entry) |
| 6092 | { |
| 6093 | struct mem_cgroup *memcg; |
| 6094 | unsigned short id; |
| 6095 | |
| 6096 | if (!do_swap_account) |
| 6097 | return; |
| 6098 | |
| 6099 | id = swap_cgroup_record(entry, 0); |
| 6100 | rcu_read_lock(); |
| 6101 | memcg = mem_cgroup_lookup(id); |
| 6102 | if (memcg) { |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 6103 | if (!mem_cgroup_is_root(memcg)) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 6104 | page_counter_uncharge(&memcg->memsw, 1); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6105 | mem_cgroup_swap_statistics(memcg, false); |
| 6106 | css_put(&memcg->css); |
| 6107 | } |
| 6108 | rcu_read_unlock(); |
| 6109 | } |
| 6110 | #endif |
| 6111 | |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 6112 | /** |
| 6113 | * mem_cgroup_try_charge - try charging a page |
| 6114 | * @page: page to charge |
| 6115 | * @mm: mm context of the victim |
| 6116 | * @gfp_mask: reclaim mode |
| 6117 | * @memcgp: charged memcg return |
| 6118 | * |
| 6119 | * Try to charge @page to the memcg that @mm belongs to, reclaiming |
| 6120 | * pages according to @gfp_mask if necessary. |
| 6121 | * |
| 6122 | * Returns 0 on success, with *@memcgp pointing to the charged memcg. |
| 6123 | * Otherwise, an error code is returned. |
| 6124 | * |
| 6125 | * After page->mapping has been set up, the caller must finalize the |
| 6126 | * charge with mem_cgroup_commit_charge(). Or abort the transaction |
| 6127 | * with mem_cgroup_cancel_charge() in case page instantiation fails. |
| 6128 | */ |
| 6129 | int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm, |
| 6130 | gfp_t gfp_mask, struct mem_cgroup **memcgp) |
| 6131 | { |
| 6132 | struct mem_cgroup *memcg = NULL; |
| 6133 | unsigned int nr_pages = 1; |
| 6134 | int ret = 0; |
| 6135 | |
| 6136 | if (mem_cgroup_disabled()) |
| 6137 | goto out; |
| 6138 | |
| 6139 | if (PageSwapCache(page)) { |
| 6140 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 6141 | /* |
| 6142 | * Every swap fault against a single page tries to charge the |
| 6143 | * page, bail as early as possible. shmem_unuse() encounters |
| 6144 | * already charged pages, too. The USED bit is protected by |
| 6145 | * the page lock, which serializes swap cache removal, which |
| 6146 | * in turn serializes uncharging. |
| 6147 | */ |
| 6148 | if (PageCgroupUsed(pc)) |
| 6149 | goto out; |
| 6150 | } |
| 6151 | |
| 6152 | if (PageTransHuge(page)) { |
| 6153 | nr_pages <<= compound_order(page); |
| 6154 | VM_BUG_ON_PAGE(!PageTransHuge(page), page); |
| 6155 | } |
| 6156 | |
| 6157 | if (do_swap_account && PageSwapCache(page)) |
| 6158 | memcg = try_get_mem_cgroup_from_page(page); |
| 6159 | if (!memcg) |
| 6160 | memcg = get_mem_cgroup_from_mm(mm); |
| 6161 | |
| 6162 | ret = try_charge(memcg, gfp_mask, nr_pages); |
| 6163 | |
| 6164 | css_put(&memcg->css); |
| 6165 | |
| 6166 | if (ret == -EINTR) { |
| 6167 | memcg = root_mem_cgroup; |
| 6168 | ret = 0; |
| 6169 | } |
| 6170 | out: |
| 6171 | *memcgp = memcg; |
| 6172 | return ret; |
| 6173 | } |
| 6174 | |
| 6175 | /** |
| 6176 | * mem_cgroup_commit_charge - commit a page charge |
| 6177 | * @page: page to charge |
| 6178 | * @memcg: memcg to charge the page to |
| 6179 | * @lrucare: page might be on LRU already |
| 6180 | * |
| 6181 | * Finalize a charge transaction started by mem_cgroup_try_charge(), |
| 6182 | * after page->mapping has been set up. This must happen atomically |
| 6183 | * as part of the page instantiation, i.e. under the page table lock |
| 6184 | * for anonymous pages, under the page lock for page and swap cache. |
| 6185 | * |
| 6186 | * In addition, the page must not be on the LRU during the commit, to |
| 6187 | * prevent racing with task migration. If it might be, use @lrucare. |
| 6188 | * |
| 6189 | * Use mem_cgroup_cancel_charge() to cancel the transaction instead. |
| 6190 | */ |
| 6191 | void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg, |
| 6192 | bool lrucare) |
| 6193 | { |
| 6194 | unsigned int nr_pages = 1; |
| 6195 | |
| 6196 | VM_BUG_ON_PAGE(!page->mapping, page); |
| 6197 | VM_BUG_ON_PAGE(PageLRU(page) && !lrucare, page); |
| 6198 | |
| 6199 | if (mem_cgroup_disabled()) |
| 6200 | return; |
| 6201 | /* |
| 6202 | * Swap faults will attempt to charge the same page multiple |
| 6203 | * times. But reuse_swap_page() might have removed the page |
| 6204 | * from swapcache already, so we can't check PageSwapCache(). |
| 6205 | */ |
| 6206 | if (!memcg) |
| 6207 | return; |
| 6208 | |
Johannes Weiner | 6abb5a8 | 2014-08-08 14:19:33 -0700 | [diff] [blame] | 6209 | commit_charge(page, memcg, lrucare); |
| 6210 | |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 6211 | if (PageTransHuge(page)) { |
| 6212 | nr_pages <<= compound_order(page); |
| 6213 | VM_BUG_ON_PAGE(!PageTransHuge(page), page); |
| 6214 | } |
| 6215 | |
Johannes Weiner | 6abb5a8 | 2014-08-08 14:19:33 -0700 | [diff] [blame] | 6216 | local_irq_disable(); |
| 6217 | mem_cgroup_charge_statistics(memcg, page, nr_pages); |
| 6218 | memcg_check_events(memcg, page); |
| 6219 | local_irq_enable(); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 6220 | |
| 6221 | if (do_swap_account && PageSwapCache(page)) { |
| 6222 | swp_entry_t entry = { .val = page_private(page) }; |
| 6223 | /* |
| 6224 | * The swap entry might not get freed for a long time, |
| 6225 | * let's not wait for it. The page already received a |
| 6226 | * memory+swap charge, drop the swap entry duplicate. |
| 6227 | */ |
| 6228 | mem_cgroup_uncharge_swap(entry); |
| 6229 | } |
| 6230 | } |
| 6231 | |
| 6232 | /** |
| 6233 | * mem_cgroup_cancel_charge - cancel a page charge |
| 6234 | * @page: page to charge |
| 6235 | * @memcg: memcg to charge the page to |
| 6236 | * |
| 6237 | * Cancel a charge transaction started by mem_cgroup_try_charge(). |
| 6238 | */ |
| 6239 | void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg) |
| 6240 | { |
| 6241 | unsigned int nr_pages = 1; |
| 6242 | |
| 6243 | if (mem_cgroup_disabled()) |
| 6244 | return; |
| 6245 | /* |
| 6246 | * Swap faults will attempt to charge the same page multiple |
| 6247 | * times. But reuse_swap_page() might have removed the page |
| 6248 | * from swapcache already, so we can't check PageSwapCache(). |
| 6249 | */ |
| 6250 | if (!memcg) |
| 6251 | return; |
| 6252 | |
| 6253 | if (PageTransHuge(page)) { |
| 6254 | nr_pages <<= compound_order(page); |
| 6255 | VM_BUG_ON_PAGE(!PageTransHuge(page), page); |
| 6256 | } |
| 6257 | |
| 6258 | cancel_charge(memcg, nr_pages); |
| 6259 | } |
| 6260 | |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 6261 | static void uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout, |
| 6262 | unsigned long nr_mem, unsigned long nr_memsw, |
| 6263 | unsigned long nr_anon, unsigned long nr_file, |
| 6264 | unsigned long nr_huge, struct page *dummy_page) |
| 6265 | { |
| 6266 | unsigned long flags; |
| 6267 | |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 6268 | if (!mem_cgroup_is_root(memcg)) { |
| 6269 | if (nr_mem) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 6270 | page_counter_uncharge(&memcg->memory, nr_mem); |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 6271 | if (nr_memsw) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 6272 | page_counter_uncharge(&memcg->memsw, nr_memsw); |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 6273 | memcg_oom_recover(memcg); |
| 6274 | } |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 6275 | |
| 6276 | local_irq_save(flags); |
| 6277 | __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon); |
| 6278 | __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file); |
| 6279 | __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge); |
| 6280 | __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout); |
| 6281 | __this_cpu_add(memcg->stat->nr_page_events, nr_anon + nr_file); |
| 6282 | memcg_check_events(memcg, dummy_page); |
| 6283 | local_irq_restore(flags); |
Johannes Weiner | e8ea14c | 2014-12-10 15:42:42 -0800 | [diff] [blame] | 6284 | |
| 6285 | if (!mem_cgroup_is_root(memcg)) |
| 6286 | css_put_many(&memcg->css, max(nr_mem, nr_memsw)); |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 6287 | } |
| 6288 | |
| 6289 | static void uncharge_list(struct list_head *page_list) |
| 6290 | { |
| 6291 | struct mem_cgroup *memcg = NULL; |
| 6292 | unsigned long nr_memsw = 0; |
| 6293 | unsigned long nr_anon = 0; |
| 6294 | unsigned long nr_file = 0; |
| 6295 | unsigned long nr_huge = 0; |
| 6296 | unsigned long pgpgout = 0; |
| 6297 | unsigned long nr_mem = 0; |
| 6298 | struct list_head *next; |
| 6299 | struct page *page; |
| 6300 | |
| 6301 | next = page_list->next; |
| 6302 | do { |
| 6303 | unsigned int nr_pages = 1; |
| 6304 | struct page_cgroup *pc; |
| 6305 | |
| 6306 | page = list_entry(next, struct page, lru); |
| 6307 | next = page->lru.next; |
| 6308 | |
| 6309 | VM_BUG_ON_PAGE(PageLRU(page), page); |
| 6310 | VM_BUG_ON_PAGE(page_count(page), page); |
| 6311 | |
| 6312 | pc = lookup_page_cgroup(page); |
| 6313 | if (!PageCgroupUsed(pc)) |
| 6314 | continue; |
| 6315 | |
| 6316 | /* |
| 6317 | * Nobody should be changing or seriously looking at |
| 6318 | * pc->mem_cgroup and pc->flags at this point, we have |
| 6319 | * fully exclusive access to the page. |
| 6320 | */ |
| 6321 | |
| 6322 | if (memcg != pc->mem_cgroup) { |
| 6323 | if (memcg) { |
| 6324 | uncharge_batch(memcg, pgpgout, nr_mem, nr_memsw, |
| 6325 | nr_anon, nr_file, nr_huge, page); |
| 6326 | pgpgout = nr_mem = nr_memsw = 0; |
| 6327 | nr_anon = nr_file = nr_huge = 0; |
| 6328 | } |
| 6329 | memcg = pc->mem_cgroup; |
| 6330 | } |
| 6331 | |
| 6332 | if (PageTransHuge(page)) { |
| 6333 | nr_pages <<= compound_order(page); |
| 6334 | VM_BUG_ON_PAGE(!PageTransHuge(page), page); |
| 6335 | nr_huge += nr_pages; |
| 6336 | } |
| 6337 | |
| 6338 | if (PageAnon(page)) |
| 6339 | nr_anon += nr_pages; |
| 6340 | else |
| 6341 | nr_file += nr_pages; |
| 6342 | |
| 6343 | if (pc->flags & PCG_MEM) |
| 6344 | nr_mem += nr_pages; |
| 6345 | if (pc->flags & PCG_MEMSW) |
| 6346 | nr_memsw += nr_pages; |
| 6347 | pc->flags = 0; |
| 6348 | |
| 6349 | pgpgout++; |
| 6350 | } while (next != page_list); |
| 6351 | |
| 6352 | if (memcg) |
| 6353 | uncharge_batch(memcg, pgpgout, nr_mem, nr_memsw, |
| 6354 | nr_anon, nr_file, nr_huge, page); |
| 6355 | } |
| 6356 | |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6357 | /** |
| 6358 | * mem_cgroup_uncharge - uncharge a page |
| 6359 | * @page: page to uncharge |
| 6360 | * |
| 6361 | * Uncharge a page previously charged with mem_cgroup_try_charge() and |
| 6362 | * mem_cgroup_commit_charge(). |
| 6363 | */ |
| 6364 | void mem_cgroup_uncharge(struct page *page) |
| 6365 | { |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6366 | struct page_cgroup *pc; |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6367 | |
| 6368 | if (mem_cgroup_disabled()) |
| 6369 | return; |
| 6370 | |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 6371 | /* Don't touch page->lru of any random page, pre-check: */ |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6372 | pc = lookup_page_cgroup(page); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6373 | if (!PageCgroupUsed(pc)) |
| 6374 | return; |
| 6375 | |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 6376 | INIT_LIST_HEAD(&page->lru); |
| 6377 | uncharge_list(&page->lru); |
| 6378 | } |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6379 | |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 6380 | /** |
| 6381 | * mem_cgroup_uncharge_list - uncharge a list of page |
| 6382 | * @page_list: list of pages to uncharge |
| 6383 | * |
| 6384 | * Uncharge a list of pages previously charged with |
| 6385 | * mem_cgroup_try_charge() and mem_cgroup_commit_charge(). |
| 6386 | */ |
| 6387 | void mem_cgroup_uncharge_list(struct list_head *page_list) |
| 6388 | { |
| 6389 | if (mem_cgroup_disabled()) |
| 6390 | return; |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6391 | |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 6392 | if (!list_empty(page_list)) |
| 6393 | uncharge_list(page_list); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6394 | } |
| 6395 | |
| 6396 | /** |
| 6397 | * mem_cgroup_migrate - migrate a charge to another page |
| 6398 | * @oldpage: currently charged page |
| 6399 | * @newpage: page to transfer the charge to |
| 6400 | * @lrucare: both pages might be on the LRU already |
| 6401 | * |
| 6402 | * Migrate the charge from @oldpage to @newpage. |
| 6403 | * |
| 6404 | * Both pages must be locked, @newpage->mapping must be set up. |
| 6405 | */ |
| 6406 | void mem_cgroup_migrate(struct page *oldpage, struct page *newpage, |
| 6407 | bool lrucare) |
| 6408 | { |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6409 | struct page_cgroup *pc; |
| 6410 | int isolated; |
| 6411 | |
| 6412 | VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage); |
| 6413 | VM_BUG_ON_PAGE(!PageLocked(newpage), newpage); |
| 6414 | VM_BUG_ON_PAGE(!lrucare && PageLRU(oldpage), oldpage); |
| 6415 | VM_BUG_ON_PAGE(!lrucare && PageLRU(newpage), newpage); |
| 6416 | VM_BUG_ON_PAGE(PageAnon(oldpage) != PageAnon(newpage), newpage); |
Johannes Weiner | 6abb5a8 | 2014-08-08 14:19:33 -0700 | [diff] [blame] | 6417 | VM_BUG_ON_PAGE(PageTransHuge(oldpage) != PageTransHuge(newpage), |
| 6418 | newpage); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6419 | |
| 6420 | if (mem_cgroup_disabled()) |
| 6421 | return; |
| 6422 | |
| 6423 | /* Page cache replacement: new page already charged? */ |
| 6424 | pc = lookup_page_cgroup(newpage); |
| 6425 | if (PageCgroupUsed(pc)) |
| 6426 | return; |
| 6427 | |
| 6428 | /* Re-entrant migration: old page already uncharged? */ |
| 6429 | pc = lookup_page_cgroup(oldpage); |
| 6430 | if (!PageCgroupUsed(pc)) |
| 6431 | return; |
| 6432 | |
| 6433 | VM_BUG_ON_PAGE(!(pc->flags & PCG_MEM), oldpage); |
| 6434 | VM_BUG_ON_PAGE(do_swap_account && !(pc->flags & PCG_MEMSW), oldpage); |
| 6435 | |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6436 | if (lrucare) |
| 6437 | lock_page_lru(oldpage, &isolated); |
| 6438 | |
| 6439 | pc->flags = 0; |
| 6440 | |
| 6441 | if (lrucare) |
| 6442 | unlock_page_lru(oldpage, isolated); |
| 6443 | |
Johannes Weiner | 6abb5a8 | 2014-08-08 14:19:33 -0700 | [diff] [blame] | 6444 | commit_charge(newpage, pc->mem_cgroup, lrucare); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6445 | } |
| 6446 | |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 6447 | /* |
Michal Hocko | 1081312 | 2013-02-22 16:35:41 -0800 | [diff] [blame] | 6448 | * subsys_initcall() for memory controller. |
| 6449 | * |
| 6450 | * Some parts like hotcpu_notifier() have to be initialized from this context |
| 6451 | * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically |
| 6452 | * everything that doesn't depend on a specific mem_cgroup structure should |
| 6453 | * be initialized from here. |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 6454 | */ |
| 6455 | static int __init mem_cgroup_init(void) |
| 6456 | { |
| 6457 | hotcpu_notifier(memcg_cpu_hotplug_callback, 0); |
Michal Hocko | 6acc8b0 | 2013-02-22 16:34:45 -0800 | [diff] [blame] | 6458 | enable_swap_cgroup(); |
Andrew Morton | bb4cc1a | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 6459 | mem_cgroup_soft_limit_tree_init(); |
Michal Hocko | e477749 | 2013-02-22 16:35:40 -0800 | [diff] [blame] | 6460 | memcg_stock_init(); |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 6461 | return 0; |
| 6462 | } |
| 6463 | subsys_initcall(mem_cgroup_init); |