blob: 916892c2b8e007bf1b69dacd090c2751c5baa0c6 [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08009 * Memory thresholds
10 * Copyright (C) 2009 Nokia Corporation
11 * Author: Kirill A. Shutemov
12 *
Glauber Costa7ae1e1d2012-12-18 14:21:56 -080013 * Kernel Memory Controller
14 * Copyright (C) 2012 Parallels Inc. and Google Inc.
15 * Authors: Glauber Costa and Suleiman Souhlal
16 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -080017 * 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
28#include <linux/res_counter.h>
29#include <linux/memcontrol.h>
30#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080031#include <linux/mm.h>
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -080032#include <linux/hugetlb.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080033#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080034#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080035#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080036#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080037#include <linux/bit_spinlock.h>
38#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070039#include <linux/limits.h>
Paul Gortmakerb9e15ba2011-05-26 16:00:52 -040040#include <linux/export.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080041#include <linux/mutex.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070042#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080043#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080044#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080045#include <linux/spinlock.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080046#include <linux/eventfd.h>
47#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080048#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080049#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070050#include <linux/vmalloc.h>
Anton Vorontsov70ddf632013-04-29 15:08:31 -070051#include <linux/vmpressure.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070052#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070053#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080054#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070055#include <linux/oom.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080056#include "internal.h"
Glauber Costad1a4c0b2011-12-11 21:47:04 +000057#include <net/sock.h>
Michal Hocko4bd2c1e2012-10-08 16:33:10 -070058#include <net/ip.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +000059#include <net/tcp_memcontrol.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080060
Balbir Singh8697d332008-02-07 00:13:59 -080061#include <asm/uaccess.h>
62
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070063#include <trace/events/vmscan.h>
64
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070065struct cgroup_subsys mem_cgroup_subsys __read_mostly;
David Rientjes68ae5642012-12-12 13:51:57 -080066EXPORT_SYMBOL(mem_cgroup_subsys);
67
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070068#define MEM_CGROUP_RECLAIM_RETRIES 5
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -070069static struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080070
Andrew Mortonc255a452012-07-31 16:43:02 -070071#ifdef CONFIG_MEMCG_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070072/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080073int do_swap_account __read_mostly;
Michal Hockoa42c3902010-11-24 12:57:08 -080074
75/* for remember boot option*/
Andrew Mortonc255a452012-07-31 16:43:02 -070076#ifdef CONFIG_MEMCG_SWAP_ENABLED
Michal Hockoa42c3902010-11-24 12:57:08 -080077static int really_do_swap_account __initdata = 1;
78#else
79static int really_do_swap_account __initdata = 0;
80#endif
81
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080082#else
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -070083#define do_swap_account 0
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080084#endif
85
86
Johannes Weineraf7c4b02012-05-29 15:07:08 -070087static const char * const mem_cgroup_stat_names[] = {
88 "cache",
89 "rss",
David Rientjesb070e652013-05-07 16:18:09 -070090 "rss_huge",
Johannes Weineraf7c4b02012-05-29 15:07:08 -070091 "mapped_file",
Sha Zhengju3ea67d02013-09-12 15:13:53 -070092 "writeback",
Johannes Weineraf7c4b02012-05-29 15:07:08 -070093 "swap",
94};
95
Johannes Weinere9f89742011-03-23 16:42:37 -070096enum mem_cgroup_events_index {
97 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
98 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
Ying Han456f9982011-05-26 16:25:38 -070099 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
100 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
Johannes Weinere9f89742011-03-23 16:42:37 -0700101 MEM_CGROUP_EVENTS_NSTATS,
102};
Johannes Weineraf7c4b02012-05-29 15:07:08 -0700103
104static const char * const mem_cgroup_events_names[] = {
105 "pgpgin",
106 "pgpgout",
107 "pgfault",
108 "pgmajfault",
109};
110
Sha Zhengju58cf1882013-02-22 16:32:05 -0800111static const char * const mem_cgroup_lru_names[] = {
112 "inactive_anon",
113 "active_anon",
114 "inactive_file",
115 "active_file",
116 "unevictable",
117};
118
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700119/*
120 * Per memcg event counter is incremented at every pagein/pageout. With THP,
121 * it will be incremated by the number of pages. This counter is used for
122 * for trigger some periodic events. This is straightforward and better
123 * than using jiffies etc. to handle periodic memcg event.
124 */
125enum mem_cgroup_events_target {
126 MEM_CGROUP_TARGET_THRESH,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700127 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700128 MEM_CGROUP_NTARGETS,
129};
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700130#define THRESHOLDS_EVENTS_TARGET 128
131#define SOFTLIMIT_EVENTS_TARGET 1024
132#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700133
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800134struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700135 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700136 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner13114712012-05-29 15:07:07 -0700137 unsigned long nr_page_events;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700138 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800139};
140
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800141struct mem_cgroup_reclaim_iter {
Michal Hocko5f578162013-04-29 15:07:17 -0700142 /*
143 * last scanned hierarchy member. Valid only if last_dead_count
144 * matches memcg->dead_count of the hierarchy root group.
145 */
Michal Hocko542f85f2013-04-29 15:07:15 -0700146 struct mem_cgroup *last_visited;
Michal Hocko5f578162013-04-29 15:07:17 -0700147 unsigned long last_dead_count;
148
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800149 /* scan generation, increased every round-trip */
150 unsigned int generation;
151};
152
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800153/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800154 * per-zone information in memory controller.
155 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800156struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800157 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700158 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800159
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800160 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
161
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700162 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700163 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800164};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800165
166struct mem_cgroup_per_node {
167 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
168};
169
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800170struct mem_cgroup_threshold {
171 struct eventfd_ctx *eventfd;
172 u64 threshold;
173};
174
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700175/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800176struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700177 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700178 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800179 /* Size of entries[] */
180 unsigned int size;
181 /* Array of thresholds */
182 struct mem_cgroup_threshold entries[0];
183};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700184
185struct mem_cgroup_thresholds {
186 /* Primary thresholds array */
187 struct mem_cgroup_threshold_ary *primary;
188 /*
189 * Spare threshold array.
190 * This is needed to make mem_cgroup_unregister_event() "never fail".
191 * It must be able to store at least primary->size - 1 entries.
192 */
193 struct mem_cgroup_threshold_ary *spare;
194};
195
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700196/* for OOM */
197struct mem_cgroup_eventfd_list {
198 struct list_head list;
199 struct eventfd_ctx *eventfd;
200};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800201
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700202static void mem_cgroup_threshold(struct mem_cgroup *memcg);
203static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800204
Balbir Singhf64c3f52009-09-23 15:56:37 -0700205/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800206 * The memory controller data structure. The memory controller controls both
207 * page cache and RSS per cgroup. We would eventually like to provide
208 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
209 * to help the administrator determine what knobs to tune.
210 *
211 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800212 * we hit the water mark. May be even add a low water mark, such that
213 * no reclaim occurs from a cgroup at it's low water mark, this is
214 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800215 */
216struct mem_cgroup {
217 struct cgroup_subsys_state css;
218 /*
219 * the counter to account for memory usage
220 */
221 struct res_counter res;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700222
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700223 /* vmpressure notifications */
224 struct vmpressure vmpressure;
225
Li Zefan465939a2013-07-08 16:00:38 -0700226 /*
227 * the counter to account for mem+swap usage.
228 */
229 struct res_counter memsw;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700230
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800231 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -0800232 * the counter to account for kernel memory usage.
233 */
234 struct res_counter kmem;
235 /*
Balbir Singh18f59ea2009-01-07 18:08:07 -0800236 * Should the accounting and control be hierarchical, per subtree?
237 */
238 bool use_hierarchy;
Glauber Costa510fc4e2012-12-18 14:21:47 -0800239 unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */
Michal Hocko79dfdac2011-07-26 16:08:23 -0700240
241 bool oom_lock;
242 atomic_t under_oom;
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700243 atomic_t oom_wakeups;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700244
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700245 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700246 /* OOM-Killer disable */
247 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800248
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700249 /* set when res.limit == memsw.limit */
250 bool memsw_is_minimum;
251
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800252 /* protect arrays of thresholds */
253 struct mutex thresholds_lock;
254
255 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700256 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700257
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800258 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700259 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700260
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700261 /* For oom notifier event fd */
262 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700263
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800264 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800265 * Should we move charges of a task when a task is moved into this
266 * mem_cgroup ? And what type of charges should we move ?
267 */
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700268 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800269 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700270 * set > 0 if pages under this cgroup are moving to other cgroup.
271 */
272 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700273 /* taken only while moving_account > 0 */
274 spinlock_t move_lock;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700275 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800276 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800277 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700278 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700279 /*
280 * used when a cpu is offlined or other synchronizations
281 * See mem_cgroup_read_stat().
282 */
283 struct mem_cgroup_stat_cpu nocpu_base;
284 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000285
Michal Hocko5f578162013-04-29 15:07:17 -0700286 atomic_t dead_count;
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700287#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000288 struct tcp_memcontrol tcp_mem;
289#endif
Glauber Costa2633d7a2012-12-18 14:22:34 -0800290#if defined(CONFIG_MEMCG_KMEM)
291 /* analogous to slab_common's slab_caches list. per-memcg */
292 struct list_head memcg_slab_caches;
293 /* Not a spinlock, we can take a lot of time walking the list */
294 struct mutex slab_caches_mutex;
295 /* Index in the kmem_cache->memcg_params->memcg_caches array */
296 int kmemcg_id;
297#endif
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800298
299 int last_scanned_node;
300#if MAX_NUMNODES > 1
301 nodemask_t scan_nodes;
302 atomic_t numainfo_events;
303 atomic_t numainfo_updating;
304#endif
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700305
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700306 struct mem_cgroup_per_node *nodeinfo[0];
307 /* WARNING: nodeinfo must be the last member here */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800308};
309
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800310static size_t memcg_size(void)
311{
312 return sizeof(struct mem_cgroup) +
313 nr_node_ids * sizeof(struct mem_cgroup_per_node);
314}
315
Glauber Costa510fc4e2012-12-18 14:21:47 -0800316/* internal only representation about the status of kmem accounting. */
317enum {
318 KMEM_ACCOUNTED_ACTIVE = 0, /* accounted by this cgroup itself */
Glauber Costaa8964b92012-12-18 14:22:09 -0800319 KMEM_ACCOUNTED_ACTIVATED, /* static key enabled. */
Glauber Costa7de37682012-12-18 14:22:07 -0800320 KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */
Glauber Costa510fc4e2012-12-18 14:21:47 -0800321};
322
Glauber Costaa8964b92012-12-18 14:22:09 -0800323/* We account when limit is on, but only after call sites are patched */
324#define KMEM_ACCOUNTED_MASK \
325 ((1 << KMEM_ACCOUNTED_ACTIVE) | (1 << KMEM_ACCOUNTED_ACTIVATED))
Glauber Costa510fc4e2012-12-18 14:21:47 -0800326
327#ifdef CONFIG_MEMCG_KMEM
328static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
329{
330 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
331}
Glauber Costa7de37682012-12-18 14:22:07 -0800332
333static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
334{
335 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
336}
337
Glauber Costaa8964b92012-12-18 14:22:09 -0800338static void memcg_kmem_set_activated(struct mem_cgroup *memcg)
339{
340 set_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
341}
342
Glauber Costa55007d82012-12-18 14:22:38 -0800343static void memcg_kmem_clear_activated(struct mem_cgroup *memcg)
344{
345 clear_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
346}
347
Glauber Costa7de37682012-12-18 14:22:07 -0800348static void memcg_kmem_mark_dead(struct mem_cgroup *memcg)
349{
Li Zefan10d5ebf2013-07-08 16:00:33 -0700350 /*
351 * Our caller must use css_get() first, because memcg_uncharge_kmem()
352 * will call css_put() if it sees the memcg is dead.
353 */
354 smp_wmb();
Glauber Costa7de37682012-12-18 14:22:07 -0800355 if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags))
356 set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags);
357}
358
359static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg)
360{
361 return test_and_clear_bit(KMEM_ACCOUNTED_DEAD,
362 &memcg->kmem_account_flags);
363}
Glauber Costa510fc4e2012-12-18 14:21:47 -0800364#endif
365
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800366/* Stuffs for move charges at task migration. */
367/*
Glauber Costaee5e8472013-02-22 16:34:50 -0800368 * Types of charges to be moved. "move_charge_at_immitgrate" and
369 * "immigrate_flags" are treated as a left-shifted bitmap of these types.
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800370 */
371enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800372 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700373 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800374 NR_MOVE_TYPE,
375};
376
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800377/* "mc" and its members are protected by cgroup_mutex */
378static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800379 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800380 struct mem_cgroup *from;
381 struct mem_cgroup *to;
Glauber Costaee5e8472013-02-22 16:34:50 -0800382 unsigned long immigrate_flags;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800383 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800384 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800385 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800386 struct task_struct *moving_task; /* a task moving charges */
387 wait_queue_head_t waitq; /* a waitq for other context */
388} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700389 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800390 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
391};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800392
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700393static bool move_anon(void)
394{
Glauber Costaee5e8472013-02-22 16:34:50 -0800395 return test_bit(MOVE_CHARGE_TYPE_ANON, &mc.immigrate_flags);
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700396}
397
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700398static bool move_file(void)
399{
Glauber Costaee5e8472013-02-22 16:34:50 -0800400 return test_bit(MOVE_CHARGE_TYPE_FILE, &mc.immigrate_flags);
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700401}
402
Balbir Singh4e416952009-09-23 15:56:39 -0700403/*
404 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
405 * limit reclaim to prevent infinite loops, if they ever occur.
406 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700407#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
Balbir Singh4e416952009-09-23 15:56:39 -0700408
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800409enum charge_type {
410 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700411 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800412 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700413 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700414 NR_CHARGE_TYPE,
415};
416
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800417/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800418enum res_type {
419 _MEM,
420 _MEMSWAP,
421 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800422 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800423};
424
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700425#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
426#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800427#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700428/* Used for OOM nofiier */
429#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800430
Balbir Singh75822b42009-09-23 15:56:38 -0700431/*
432 * Reclaim flags for mem_cgroup_hierarchical_reclaim
433 */
434#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
435#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
436#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
437#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
438
Glauber Costa09998212013-02-22 16:34:55 -0800439/*
440 * The memcg_create_mutex will be held whenever a new cgroup is created.
441 * As a consequence, any change that needs to protect against new child cgroups
442 * appearing has to hold it as well.
443 */
444static DEFINE_MUTEX(memcg_create_mutex);
445
Wanpeng Lib2145142012-07-31 16:46:01 -0700446struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
447{
Tejun Heoa7c6d552013-08-08 20:11:23 -0400448 return s ? container_of(s, struct mem_cgroup, css) : NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -0700449}
450
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700451/* Some nice accessors for the vmpressure. */
452struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
453{
454 if (!memcg)
455 memcg = root_mem_cgroup;
456 return &memcg->vmpressure;
457}
458
459struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr)
460{
461 return &container_of(vmpr, struct mem_cgroup, vmpressure)->css;
462}
463
464struct vmpressure *css_to_vmpressure(struct cgroup_subsys_state *css)
465{
466 return &mem_cgroup_from_css(css)->vmpressure;
467}
468
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700469static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
470{
471 return (memcg == root_mem_cgroup);
472}
473
Glauber Costae1aab162011-12-11 21:47:03 +0000474/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700475#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000476
Glauber Costae1aab162011-12-11 21:47:03 +0000477void sock_update_memcg(struct sock *sk)
478{
Glauber Costa376be5f2012-01-20 04:57:14 +0000479 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000480 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700481 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000482
483 BUG_ON(!sk->sk_prot->proto_cgroup);
484
Glauber Costaf3f511e2012-01-05 20:16:39 +0000485 /* Socket cloning can throw us here with sk_cgrp already
486 * filled. It won't however, necessarily happen from
487 * process context. So the test for root memcg given
488 * the current task's memcg won't help us in this case.
489 *
490 * Respecting the original socket's memcg is a better
491 * decision in this case.
492 */
493 if (sk->sk_cgrp) {
494 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
Li Zefan5347e5a2013-07-08 16:00:30 -0700495 css_get(&sk->sk_cgrp->memcg->css);
Glauber Costaf3f511e2012-01-05 20:16:39 +0000496 return;
497 }
498
Glauber Costae1aab162011-12-11 21:47:03 +0000499 rcu_read_lock();
500 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700501 cg_proto = sk->sk_prot->proto_cgroup(memcg);
Li Zefan5347e5a2013-07-08 16:00:30 -0700502 if (!mem_cgroup_is_root(memcg) &&
503 memcg_proto_active(cg_proto) && css_tryget(&memcg->css)) {
Glauber Costa3f134612012-05-29 15:07:11 -0700504 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000505 }
506 rcu_read_unlock();
507 }
508}
509EXPORT_SYMBOL(sock_update_memcg);
510
511void sock_release_memcg(struct sock *sk)
512{
Glauber Costa376be5f2012-01-20 04:57:14 +0000513 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000514 struct mem_cgroup *memcg;
515 WARN_ON(!sk->sk_cgrp->memcg);
516 memcg = sk->sk_cgrp->memcg;
Li Zefan5347e5a2013-07-08 16:00:30 -0700517 css_put(&sk->sk_cgrp->memcg->css);
Glauber Costae1aab162011-12-11 21:47:03 +0000518 }
519}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000520
521struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
522{
523 if (!memcg || mem_cgroup_is_root(memcg))
524 return NULL;
525
526 return &memcg->tcp_mem.cg_proto;
527}
528EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000529
Glauber Costa3f134612012-05-29 15:07:11 -0700530static void disarm_sock_keys(struct mem_cgroup *memcg)
531{
532 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
533 return;
534 static_key_slow_dec(&memcg_socket_limit_enabled);
535}
536#else
537static void disarm_sock_keys(struct mem_cgroup *memcg)
538{
539}
540#endif
541
Glauber Costaa8964b92012-12-18 14:22:09 -0800542#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800543/*
544 * This will be the memcg's index in each cache's ->memcg_params->memcg_caches.
545 * There are two main reasons for not using the css_id for this:
546 * 1) this works better in sparse environments, where we have a lot of memcgs,
547 * but only a few kmem-limited. Or also, if we have, for instance, 200
548 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
549 * 200 entry array for that.
550 *
551 * 2) In order not to violate the cgroup API, we would like to do all memory
552 * allocation in ->create(). At that point, we haven't yet allocated the
553 * css_id. Having a separate index prevents us from messing with the cgroup
554 * core for this
555 *
556 * The current size of the caches array is stored in
557 * memcg_limited_groups_array_size. It will double each time we have to
558 * increase it.
559 */
560static DEFINE_IDA(kmem_limited_groups);
Glauber Costa749c5412012-12-18 14:23:01 -0800561int memcg_limited_groups_array_size;
562
Glauber Costa55007d82012-12-18 14:22:38 -0800563/*
564 * MIN_SIZE is different than 1, because we would like to avoid going through
565 * the alloc/free process all the time. In a small machine, 4 kmem-limited
566 * cgroups is a reasonable guess. In the future, it could be a parameter or
567 * tunable, but that is strictly not necessary.
568 *
569 * MAX_SIZE should be as large as the number of css_ids. Ideally, we could get
570 * this constant directly from cgroup, but it is understandable that this is
571 * better kept as an internal representation in cgroup.c. In any case, the
572 * css_id space is not getting any smaller, and we don't have to necessarily
573 * increase ours as well if it increases.
574 */
575#define MEMCG_CACHES_MIN_SIZE 4
576#define MEMCG_CACHES_MAX_SIZE 65535
577
Glauber Costad7f25f82012-12-18 14:22:40 -0800578/*
579 * A lot of the calls to the cache allocation functions are expected to be
580 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
581 * conditional to this static branch, we'll have to allow modules that does
582 * kmem_cache_alloc and the such to see this symbol as well
583 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800584struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800585EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800586
587static void disarm_kmem_keys(struct mem_cgroup *memcg)
588{
Glauber Costa55007d82012-12-18 14:22:38 -0800589 if (memcg_kmem_is_active(memcg)) {
Glauber Costaa8964b92012-12-18 14:22:09 -0800590 static_key_slow_dec(&memcg_kmem_enabled_key);
Glauber Costa55007d82012-12-18 14:22:38 -0800591 ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id);
592 }
Glauber Costabea207c2012-12-18 14:22:11 -0800593 /*
594 * This check can't live in kmem destruction function,
595 * since the charges will outlive the cgroup
596 */
597 WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0);
Glauber Costaa8964b92012-12-18 14:22:09 -0800598}
599#else
600static void disarm_kmem_keys(struct mem_cgroup *memcg)
601{
602}
603#endif /* CONFIG_MEMCG_KMEM */
604
605static void disarm_static_keys(struct mem_cgroup *memcg)
606{
607 disarm_sock_keys(memcg);
608 disarm_kmem_keys(memcg);
609}
610
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700611static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800612
Balbir Singhf64c3f52009-09-23 15:56:37 -0700613static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700614mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700615{
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800616 VM_BUG_ON((unsigned)nid >= nr_node_ids);
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700617 return &memcg->nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700618}
619
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700620struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100621{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700622 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100623}
624
Balbir Singhf64c3f52009-09-23 15:56:37 -0700625static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700626page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700627{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700628 int nid = page_to_nid(page);
629 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700630
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700631 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700632}
633
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700634/*
635 * Implementation Note: reading percpu statistics for memcg.
636 *
637 * Both of vmstat[] and percpu_counter has threshold and do periodic
638 * synchronization to implement "quick" read. There are trade-off between
639 * reading cost and precision of value. Then, we may have a chance to implement
640 * a periodic synchronizion of counter in memcg's counter.
641 *
642 * But this _read() function is used for user interface now. The user accounts
643 * memory usage by memory cgroup and he _always_ requires exact value because
644 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
645 * have to visit all online cpus and make sum. So, for now, unnecessary
646 * synchronization is not implemented. (just implemented for cpu hotplug)
647 *
648 * If there are kernel internal actions which can make use of some not-exact
649 * value, and reading all cpu value can be performance bottleneck in some
650 * common workload, threashold and synchonization as vmstat[] should be
651 * implemented.
652 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700653static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700654 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800655{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700656 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800657 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800658
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700659 get_online_cpus();
660 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700661 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700662#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700663 spin_lock(&memcg->pcp_counter_lock);
664 val += memcg->nocpu_base.count[idx];
665 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700666#endif
667 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800668 return val;
669}
670
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700671static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700672 bool charge)
673{
674 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700675 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700676}
677
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700678static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700679 enum mem_cgroup_events_index idx)
680{
681 unsigned long val = 0;
682 int cpu;
683
684 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700685 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700686#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700687 spin_lock(&memcg->pcp_counter_lock);
688 val += memcg->nocpu_base.events[idx];
689 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700690#endif
691 return val;
692}
693
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700694static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
David Rientjesb070e652013-05-07 16:18:09 -0700695 struct page *page,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700696 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800697{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800698 preempt_disable();
699
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700700 /*
701 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
702 * counted as CACHE even if it's on ANON LRU.
703 */
704 if (anon)
705 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700706 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800707 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700708 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700709 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700710
David Rientjesb070e652013-05-07 16:18:09 -0700711 if (PageTransHuge(page))
712 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
713 nr_pages);
714
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800715 /* pagein of a big page is an event. So, ignore page size */
716 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700717 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800718 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700719 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800720 nr_pages = -nr_pages; /* for event */
721 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800722
Johannes Weiner13114712012-05-29 15:07:07 -0700723 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800724
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800725 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800726}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800727
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700728unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700729mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700730{
731 struct mem_cgroup_per_zone *mz;
732
733 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
734 return mz->lru_size[lru];
735}
736
737static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700738mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700739 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700740{
741 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab932012-03-21 16:34:19 -0700742 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700743 unsigned long ret = 0;
744
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700745 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700746
Hugh Dickinsf156ab932012-03-21 16:34:19 -0700747 for_each_lru(lru) {
748 if (BIT(lru) & lru_mask)
749 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700750 }
751 return ret;
752}
753
754static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700755mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700756 int nid, unsigned int lru_mask)
757{
Ying Han889976d2011-05-26 16:25:33 -0700758 u64 total = 0;
759 int zid;
760
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700761 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700762 total += mem_cgroup_zone_nr_lru_pages(memcg,
763 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700764
Ying Han889976d2011-05-26 16:25:33 -0700765 return total;
766}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700767
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700768static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700769 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800770{
Ying Han889976d2011-05-26 16:25:33 -0700771 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800772 u64 total = 0;
773
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800774 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700775 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800776 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800777}
778
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800779static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
780 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800781{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700782 unsigned long val, next;
783
Johannes Weiner13114712012-05-29 15:07:07 -0700784 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700785 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700786 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800787 if ((long)next - (long)val < 0) {
788 switch (target) {
789 case MEM_CGROUP_TARGET_THRESH:
790 next = val + THRESHOLDS_EVENTS_TARGET;
791 break;
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800792 case MEM_CGROUP_TARGET_NUMAINFO:
793 next = val + NUMAINFO_EVENTS_TARGET;
794 break;
795 default:
796 break;
797 }
798 __this_cpu_write(memcg->stat->targets[target], next);
799 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700800 }
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800801 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800802}
803
804/*
805 * Check events in order.
806 *
807 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700808static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800809{
Steven Rostedt47994012011-11-02 13:38:33 -0700810 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800811 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800812 if (unlikely(mem_cgroup_event_ratelimit(memcg,
813 MEM_CGROUP_TARGET_THRESH))) {
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800814 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800815
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700816#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800817 do_numainfo = mem_cgroup_event_ratelimit(memcg,
818 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700819#endif
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800820 preempt_enable();
821
822 mem_cgroup_threshold(memcg);
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800823#if MAX_NUMNODES > 1
824 if (unlikely(do_numainfo))
825 atomic_inc(&memcg->numainfo_events);
826#endif
827 } else
828 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800829}
830
Balbir Singhcf475ad2008-04-29 01:00:16 -0700831struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800832{
Balbir Singh31a78f22008-09-28 23:09:31 +0100833 /*
834 * mm_update_next_owner() may clear mm->owner to NULL
835 * if it races with swapoff, page migration, etc.
836 * So this can be called with p == NULL.
837 */
838 if (unlikely(!p))
839 return NULL;
840
Tejun Heo8af01f52013-08-08 20:11:22 -0400841 return mem_cgroup_from_css(task_css(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800842}
843
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700844struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800845{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700846 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700847
848 if (!mm)
849 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800850 /*
851 * Because we have no locks, mm->owner's may be being moved to other
852 * cgroup. We use css_tryget() here even if this looks
853 * pessimistic (rather than adding locks here).
854 */
855 rcu_read_lock();
856 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700857 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
858 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800859 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700860 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800861 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700862 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800863}
864
Michal Hockode577802013-09-12 15:13:26 -0700865static enum mem_cgroup_filter_t
866mem_cgroup_filter(struct mem_cgroup *memcg, struct mem_cgroup *root,
867 mem_cgroup_iter_filter cond)
868{
869 if (!cond)
870 return VISIT;
871 return cond(memcg, root);
872}
873
Michal Hocko16248d82013-04-29 15:07:19 -0700874/*
875 * Returns a next (in a pre-order walk) alive memcg (with elevated css
876 * ref. count) or NULL if the whole root's subtree has been visited.
877 *
878 * helper function to be used by mem_cgroup_iter
879 */
880static struct mem_cgroup *__mem_cgroup_iter_next(struct mem_cgroup *root,
Michal Hockode577802013-09-12 15:13:26 -0700881 struct mem_cgroup *last_visited, mem_cgroup_iter_filter cond)
Michal Hocko16248d82013-04-29 15:07:19 -0700882{
Tejun Heo492eb212013-08-08 20:11:25 -0400883 struct cgroup_subsys_state *prev_css, *next_css;
Michal Hocko16248d82013-04-29 15:07:19 -0700884
Tejun Heobd8815a2013-08-08 20:11:27 -0400885 prev_css = last_visited ? &last_visited->css : NULL;
Michal Hocko16248d82013-04-29 15:07:19 -0700886skip_node:
Tejun Heo492eb212013-08-08 20:11:25 -0400887 next_css = css_next_descendant_pre(prev_css, &root->css);
Michal Hocko16248d82013-04-29 15:07:19 -0700888
889 /*
890 * Even if we found a group we have to make sure it is
891 * alive. css && !memcg means that the groups should be
892 * skipped and we should continue the tree walk.
893 * last_visited css is safe to use because it is
894 * protected by css_get and the tree walk is rcu safe.
895 */
Tejun Heo492eb212013-08-08 20:11:25 -0400896 if (next_css) {
897 struct mem_cgroup *mem = mem_cgroup_from_css(next_css);
898
Michal Hockode577802013-09-12 15:13:26 -0700899 switch (mem_cgroup_filter(mem, root, cond)) {
900 case SKIP:
Tejun Heo492eb212013-08-08 20:11:25 -0400901 prev_css = next_css;
Michal Hocko16248d82013-04-29 15:07:19 -0700902 goto skip_node;
Michal Hockode577802013-09-12 15:13:26 -0700903 case SKIP_TREE:
904 if (mem == root)
905 return NULL;
906 /*
907 * css_rightmost_descendant is not an optimal way to
908 * skip through a subtree (especially for imbalanced
909 * trees leaning to right) but that's what we have right
910 * now. More effective solution would be traversing
911 * right-up for first non-NULL without calling
912 * css_next_descendant_pre afterwards.
913 */
914 prev_css = css_rightmost_descendant(next_css);
915 goto skip_node;
916 case VISIT:
917 if (css_tryget(&mem->css))
918 return mem;
919 else {
920 prev_css = next_css;
921 goto skip_node;
922 }
923 break;
Michal Hocko16248d82013-04-29 15:07:19 -0700924 }
925 }
926
927 return NULL;
928}
929
Johannes Weiner519ebea2013-07-03 15:04:51 -0700930static void mem_cgroup_iter_invalidate(struct mem_cgroup *root)
931{
932 /*
933 * When a group in the hierarchy below root is destroyed, the
934 * hierarchy iterator can no longer be trusted since it might
935 * have pointed to the destroyed group. Invalidate it.
936 */
937 atomic_inc(&root->dead_count);
938}
939
940static struct mem_cgroup *
941mem_cgroup_iter_load(struct mem_cgroup_reclaim_iter *iter,
942 struct mem_cgroup *root,
943 int *sequence)
944{
945 struct mem_cgroup *position = NULL;
946 /*
947 * A cgroup destruction happens in two stages: offlining and
948 * release. They are separated by a RCU grace period.
949 *
950 * If the iterator is valid, we may still race with an
951 * offlining. The RCU lock ensures the object won't be
952 * released, tryget will fail if we lost the race.
953 */
954 *sequence = atomic_read(&root->dead_count);
955 if (iter->last_dead_count == *sequence) {
956 smp_rmb();
957 position = iter->last_visited;
958 if (position && !css_tryget(&position->css))
959 position = NULL;
960 }
961 return position;
962}
963
964static void mem_cgroup_iter_update(struct mem_cgroup_reclaim_iter *iter,
965 struct mem_cgroup *last_visited,
966 struct mem_cgroup *new_position,
967 int sequence)
968{
969 if (last_visited)
970 css_put(&last_visited->css);
971 /*
972 * We store the sequence count from the time @last_visited was
973 * loaded successfully instead of rereading it here so that we
974 * don't lose destruction events in between. We could have
975 * raced with the destruction of @new_position after all.
976 */
977 iter->last_visited = new_position;
978 smp_wmb();
979 iter->last_dead_count = sequence;
980}
981
Johannes Weiner56600482012-01-12 17:17:59 -0800982/**
983 * mem_cgroup_iter - iterate over memory cgroup hierarchy
984 * @root: hierarchy root
985 * @prev: previously returned memcg, NULL on first invocation
986 * @reclaim: cookie for shared reclaim walks, NULL for full walks
Michal Hockode577802013-09-12 15:13:26 -0700987 * @cond: filter for visited nodes, NULL for no filter
Johannes Weiner56600482012-01-12 17:17:59 -0800988 *
989 * Returns references to children of the hierarchy below @root, or
990 * @root itself, or %NULL after a full round-trip.
991 *
992 * Caller must pass the return value in @prev on subsequent
993 * invocations for reference counting, or use mem_cgroup_iter_break()
994 * to cancel a hierarchy walk before the round-trip is complete.
995 *
996 * Reclaimers can specify a zone and a priority level in @reclaim to
997 * divide up the memcgs in the hierarchy among all concurrent
998 * reclaimers operating on the same zone and priority.
999 */
Michal Hockode577802013-09-12 15:13:26 -07001000struct mem_cgroup *mem_cgroup_iter_cond(struct mem_cgroup *root,
Johannes Weiner56600482012-01-12 17:17:59 -08001001 struct mem_cgroup *prev,
Michal Hockode577802013-09-12 15:13:26 -07001002 struct mem_cgroup_reclaim_cookie *reclaim,
1003 mem_cgroup_iter_filter cond)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001004{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001005 struct mem_cgroup *memcg = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001006 struct mem_cgroup *last_visited = NULL;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001007
Michal Hockode577802013-09-12 15:13:26 -07001008 if (mem_cgroup_disabled()) {
1009 /* first call must return non-NULL, second return NULL */
1010 return (struct mem_cgroup *)(unsigned long)!prev;
1011 }
Johannes Weiner56600482012-01-12 17:17:59 -08001012
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001013 if (!root)
1014 root = root_mem_cgroup;
1015
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001016 if (prev && !reclaim)
Michal Hocko542f85f2013-04-29 15:07:15 -07001017 last_visited = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001018
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001019 if (!root->use_hierarchy && root != root_mem_cgroup) {
1020 if (prev)
Michal Hockoc40046f2013-04-29 15:07:14 -07001021 goto out_css_put;
Michal Hockode577802013-09-12 15:13:26 -07001022 if (mem_cgroup_filter(root, root, cond) == VISIT)
1023 return root;
1024 return NULL;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001025 }
1026
Michal Hocko542f85f2013-04-29 15:07:15 -07001027 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001028 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001029 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner519ebea2013-07-03 15:04:51 -07001030 int uninitialized_var(seq);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001031
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001032 if (reclaim) {
1033 int nid = zone_to_nid(reclaim->zone);
1034 int zid = zone_idx(reclaim->zone);
1035 struct mem_cgroup_per_zone *mz;
1036
1037 mz = mem_cgroup_zoneinfo(root, nid, zid);
1038 iter = &mz->reclaim_iter[reclaim->priority];
Michal Hocko542f85f2013-04-29 15:07:15 -07001039 if (prev && reclaim->generation != iter->generation) {
Michal Hocko5f578162013-04-29 15:07:17 -07001040 iter->last_visited = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001041 goto out_unlock;
1042 }
Michal Hocko5f578162013-04-29 15:07:17 -07001043
Johannes Weiner519ebea2013-07-03 15:04:51 -07001044 last_visited = mem_cgroup_iter_load(iter, root, &seq);
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001045 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001046
Michal Hockode577802013-09-12 15:13:26 -07001047 memcg = __mem_cgroup_iter_next(root, last_visited, cond);
Michal Hocko542f85f2013-04-29 15:07:15 -07001048
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001049 if (reclaim) {
Johannes Weiner519ebea2013-07-03 15:04:51 -07001050 mem_cgroup_iter_update(iter, last_visited, memcg, seq);
Michal Hocko542f85f2013-04-29 15:07:15 -07001051
Michal Hocko19f39402013-04-29 15:07:18 -07001052 if (!memcg)
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001053 iter->generation++;
1054 else if (!prev && memcg)
1055 reclaim->generation = iter->generation;
1056 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001057
Michal Hockode577802013-09-12 15:13:26 -07001058 /*
1059 * We have finished the whole tree walk or no group has been
1060 * visited because filter told us to skip the root node.
1061 */
1062 if (!memcg && (prev || (cond && !last_visited)))
Michal Hocko542f85f2013-04-29 15:07:15 -07001063 goto out_unlock;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001064 }
Michal Hocko542f85f2013-04-29 15:07:15 -07001065out_unlock:
1066 rcu_read_unlock();
Michal Hockoc40046f2013-04-29 15:07:14 -07001067out_css_put:
1068 if (prev && prev != root)
1069 css_put(&prev->css);
1070
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001071 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001072}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001073
Johannes Weiner56600482012-01-12 17:17:59 -08001074/**
1075 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1076 * @root: hierarchy root
1077 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1078 */
1079void mem_cgroup_iter_break(struct mem_cgroup *root,
1080 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001081{
1082 if (!root)
1083 root = root_mem_cgroup;
1084 if (prev && prev != root)
1085 css_put(&prev->css);
1086}
1087
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001088/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001089 * Iteration constructs for visiting all cgroups (under a tree). If
1090 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1091 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001092 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001093#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001094 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001095 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001096 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001097
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001098#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001099 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001100 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001101 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001102
David Rientjes68ae5642012-12-12 13:51:57 -08001103void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001104{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001105 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001106
Ying Han456f9982011-05-26 16:25:38 -07001107 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001108 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1109 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001110 goto out;
1111
1112 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001113 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001114 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1115 break;
1116 case PGMAJFAULT:
1117 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001118 break;
1119 default:
1120 BUG();
1121 }
1122out:
1123 rcu_read_unlock();
1124}
David Rientjes68ae5642012-12-12 13:51:57 -08001125EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001126
Johannes Weiner925b7672012-01-12 17:18:15 -08001127/**
1128 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1129 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001130 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001131 *
1132 * Returns the lru list vector holding pages for the given @zone and
1133 * @mem. This can be the global zone lruvec, if the memory controller
1134 * is disabled.
1135 */
1136struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1137 struct mem_cgroup *memcg)
1138{
1139 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001140 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001141
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001142 if (mem_cgroup_disabled()) {
1143 lruvec = &zone->lruvec;
1144 goto out;
1145 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001146
1147 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001148 lruvec = &mz->lruvec;
1149out:
1150 /*
1151 * Since a node can be onlined after the mem_cgroup was created,
1152 * we have to be prepared to initialize lruvec->zone here;
1153 * and if offlined then reonlined, we need to reinitialize it.
1154 */
1155 if (unlikely(lruvec->zone != zone))
1156 lruvec->zone = zone;
1157 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001158}
1159
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001160/*
1161 * Following LRU functions are allowed to be used without PCG_LOCK.
1162 * Operations are called by routine of global LRU independently from memcg.
1163 * What we have to take care of here is validness of pc->mem_cgroup.
1164 *
1165 * Changes to pc->mem_cgroup happens when
1166 * 1. charge
1167 * 2. moving account
1168 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1169 * It is added to LRU before charge.
1170 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1171 * When moving account, the page is not on LRU. It's isolated.
1172 */
1173
Johannes Weiner925b7672012-01-12 17:18:15 -08001174/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001175 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001176 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001177 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001178 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001179struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001180{
1181 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001182 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001183 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001184 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001185
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001186 if (mem_cgroup_disabled()) {
1187 lruvec = &zone->lruvec;
1188 goto out;
1189 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001190
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001191 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001192 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001193
1194 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001195 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001196 * an uncharged page off lru does nothing to secure
1197 * its former mem_cgroup from sudden removal.
1198 *
1199 * Our caller holds lru_lock, and PageCgroupUsed is updated
1200 * under page_cgroup lock: between them, they make all uses
1201 * of pc->mem_cgroup safe.
1202 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001203 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001204 pc->mem_cgroup = memcg = root_mem_cgroup;
1205
Johannes Weiner925b7672012-01-12 17:18:15 -08001206 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001207 lruvec = &mz->lruvec;
1208out:
1209 /*
1210 * Since a node can be onlined after the mem_cgroup was created,
1211 * we have to be prepared to initialize lruvec->zone here;
1212 * and if offlined then reonlined, we need to reinitialize it.
1213 */
1214 if (unlikely(lruvec->zone != zone))
1215 lruvec->zone = zone;
1216 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001217}
1218
1219/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001220 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1221 * @lruvec: mem_cgroup per zone lru vector
1222 * @lru: index of lru list the page is sitting on
1223 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001224 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001225 * This function must be called when a page is added to or removed from an
1226 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001227 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001228void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1229 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001230{
1231 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001232 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001233
1234 if (mem_cgroup_disabled())
1235 return;
1236
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001237 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1238 lru_size = mz->lru_size + lru;
1239 *lru_size += nr_pages;
1240 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001241}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001242
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001243/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001244 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001245 * hierarchy subtree
1246 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001247bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1248 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001249{
Johannes Weiner91c637342012-05-29 15:06:24 -07001250 if (root_memcg == memcg)
1251 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001252 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001253 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001254 return css_is_ancestor(&memcg->css, &root_memcg->css);
1255}
1256
1257static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1258 struct mem_cgroup *memcg)
1259{
1260 bool ret;
1261
Johannes Weiner91c637342012-05-29 15:06:24 -07001262 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001263 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001264 rcu_read_unlock();
1265 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001266}
1267
David Rientjesffbdccf2013-07-03 15:01:23 -07001268bool task_in_mem_cgroup(struct task_struct *task,
1269 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001270{
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001271 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001272 struct task_struct *p;
David Rientjesffbdccf2013-07-03 15:01:23 -07001273 bool ret;
David Rientjes4c4a2212008-02-07 00:14:06 -08001274
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001275 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001276 if (p) {
1277 curr = try_get_mem_cgroup_from_mm(p->mm);
1278 task_unlock(p);
1279 } else {
1280 /*
1281 * All threads may have already detached their mm's, but the oom
1282 * killer still needs to detect if they have already been oom
1283 * killed to prevent needlessly killing additional tasks.
1284 */
David Rientjesffbdccf2013-07-03 15:01:23 -07001285 rcu_read_lock();
David Rientjesde077d22012-01-12 17:18:52 -08001286 curr = mem_cgroup_from_task(task);
1287 if (curr)
1288 css_get(&curr->css);
David Rientjesffbdccf2013-07-03 15:01:23 -07001289 rcu_read_unlock();
David Rientjesde077d22012-01-12 17:18:52 -08001290 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001291 if (!curr)
David Rientjesffbdccf2013-07-03 15:01:23 -07001292 return false;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001293 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001294 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001295 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001296 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1297 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001298 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001299 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001300 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001301 return ret;
1302}
1303
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001304int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001305{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001306 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001307 unsigned long inactive;
1308 unsigned long active;
1309 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001310
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001311 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1312 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001313
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001314 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1315 if (gb)
1316 inactive_ratio = int_sqrt(10 * gb);
1317 else
1318 inactive_ratio = 1;
1319
Johannes Weiner9b272972011-11-02 13:38:23 -07001320 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001321}
1322
Balbir Singh6d61ef42009-01-07 18:08:06 -08001323#define mem_cgroup_from_res_counter(counter, member) \
1324 container_of(counter, struct mem_cgroup, member)
1325
Johannes Weiner19942822011-02-01 15:52:43 -08001326/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001327 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001328 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001329 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001330 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001331 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001332 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001333static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001334{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001335 unsigned long long margin;
1336
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001337 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001338 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001339 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001340 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001341}
1342
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001343int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001344{
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001345 /* root ? */
Tejun Heo63876982013-08-08 20:11:23 -04001346 if (!css_parent(&memcg->css))
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001347 return vm_swappiness;
1348
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001349 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001350}
1351
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001352/*
1353 * memcg->moving_account is used for checking possibility that some thread is
1354 * calling move_account(). When a thread on CPU-A starts moving pages under
1355 * a memcg, other threads should check memcg->moving_account under
1356 * rcu_read_lock(), like this:
1357 *
1358 * CPU-A CPU-B
1359 * rcu_read_lock()
1360 * memcg->moving_account+1 if (memcg->mocing_account)
1361 * take heavy locks.
1362 * synchronize_rcu() update something.
1363 * rcu_read_unlock()
1364 * start move here.
1365 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001366
1367/* for quick checking without looking up memcg */
1368atomic_t memcg_moving __read_mostly;
1369
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001370static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001371{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001372 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001373 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001374 synchronize_rcu();
1375}
1376
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001377static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001378{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001379 /*
1380 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1381 * We check NULL in callee rather than caller.
1382 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001383 if (memcg) {
1384 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001385 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001386 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001387}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001388
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001389/*
1390 * 2 routines for checking "mem" is under move_account() or not.
1391 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001392 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1393 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001394 * pc->mem_cgroup may be overwritten.
1395 *
1396 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1397 * under hierarchy of moving cgroups. This is for
1398 * waiting at hith-memory prressure caused by "move".
1399 */
1400
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001401static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001402{
1403 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001404 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001405}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001406
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001407static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001408{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001409 struct mem_cgroup *from;
1410 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001411 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001412 /*
1413 * Unlike task_move routines, we access mc.to, mc.from not under
1414 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1415 */
1416 spin_lock(&mc.lock);
1417 from = mc.from;
1418 to = mc.to;
1419 if (!from)
1420 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001421
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001422 ret = mem_cgroup_same_or_subtree(memcg, from)
1423 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001424unlock:
1425 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001426 return ret;
1427}
1428
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001429static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001430{
1431 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001432 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001433 DEFINE_WAIT(wait);
1434 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1435 /* moving charge context might have finished. */
1436 if (mc.moving_task)
1437 schedule();
1438 finish_wait(&mc.waitq, &wait);
1439 return true;
1440 }
1441 }
1442 return false;
1443}
1444
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001445/*
1446 * Take this lock when
1447 * - a code tries to modify page's memcg while it's USED.
1448 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001449 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001450 */
1451static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1452 unsigned long *flags)
1453{
1454 spin_lock_irqsave(&memcg->move_lock, *flags);
1455}
1456
1457static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1458 unsigned long *flags)
1459{
1460 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1461}
1462
Sha Zhengju58cf1882013-02-22 16:32:05 -08001463#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001464/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001465 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001466 * @memcg: The memory cgroup that went over limit
1467 * @p: Task that is going to be killed
1468 *
1469 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1470 * enabled
1471 */
1472void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1473{
1474 struct cgroup *task_cgrp;
1475 struct cgroup *mem_cgrp;
1476 /*
1477 * Need a buffer in BSS, can't rely on allocations. The code relies
1478 * on the assumption that OOM is serialized for memory controller.
1479 * If this assumption is broken, revisit this code.
1480 */
1481 static char memcg_name[PATH_MAX];
1482 int ret;
Sha Zhengju58cf1882013-02-22 16:32:05 -08001483 struct mem_cgroup *iter;
1484 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001485
Sha Zhengju58cf1882013-02-22 16:32:05 -08001486 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001487 return;
1488
Balbir Singhe2224322009-04-02 16:57:39 -07001489 rcu_read_lock();
1490
1491 mem_cgrp = memcg->css.cgroup;
1492 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1493
1494 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1495 if (ret < 0) {
1496 /*
1497 * Unfortunately, we are unable to convert to a useful name
1498 * But we'll still print out the usage information
1499 */
1500 rcu_read_unlock();
1501 goto done;
1502 }
1503 rcu_read_unlock();
1504
Andrew Mortond0451972013-02-22 16:32:06 -08001505 pr_info("Task in %s killed", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001506
1507 rcu_read_lock();
1508 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1509 if (ret < 0) {
1510 rcu_read_unlock();
1511 goto done;
1512 }
1513 rcu_read_unlock();
1514
1515 /*
1516 * Continues from above, so we don't need an KERN_ level
1517 */
Andrew Mortond0451972013-02-22 16:32:06 -08001518 pr_cont(" as a result of limit of %s\n", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001519done:
1520
Andrew Mortond0451972013-02-22 16:32:06 -08001521 pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001522 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1523 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1524 res_counter_read_u64(&memcg->res, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001525 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001526 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1527 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1528 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001529 pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n",
Glauber Costa510fc4e2012-12-18 14:21:47 -08001530 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1531 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1532 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001533
1534 for_each_mem_cgroup_tree(iter, memcg) {
1535 pr_info("Memory cgroup stats");
1536
1537 rcu_read_lock();
1538 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1539 if (!ret)
1540 pr_cont(" for %s", memcg_name);
1541 rcu_read_unlock();
1542 pr_cont(":");
1543
1544 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1545 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1546 continue;
1547 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1548 K(mem_cgroup_read_stat(iter, i)));
1549 }
1550
1551 for (i = 0; i < NR_LRU_LISTS; i++)
1552 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1553 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1554
1555 pr_cont("\n");
1556 }
Balbir Singhe2224322009-04-02 16:57:39 -07001557}
1558
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001559/*
1560 * This function returns the number of memcg under hierarchy tree. Returns
1561 * 1(self count) if no children.
1562 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001563static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001564{
1565 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001566 struct mem_cgroup *iter;
1567
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001568 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001569 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001570 return num;
1571}
1572
Balbir Singh6d61ef42009-01-07 18:08:06 -08001573/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001574 * Return the memory (and swap, if configured) limit for a memcg.
1575 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001576static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001577{
1578 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001579
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001580 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001581
David Rientjesa63d83f2010-08-09 17:19:46 -07001582 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001583 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001584 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001585 if (mem_cgroup_swappiness(memcg)) {
1586 u64 memsw;
1587
1588 limit += total_swap_pages << PAGE_SHIFT;
1589 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1590
1591 /*
1592 * If memsw is finite and limits the amount of swap space
1593 * available to this memcg, return that limit.
1594 */
1595 limit = min(limit, memsw);
1596 }
1597
1598 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001599}
1600
David Rientjes19965462012-12-11 16:00:26 -08001601static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1602 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001603{
1604 struct mem_cgroup *iter;
1605 unsigned long chosen_points = 0;
1606 unsigned long totalpages;
1607 unsigned int points = 0;
1608 struct task_struct *chosen = NULL;
1609
David Rientjes876aafb2012-07-31 16:43:48 -07001610 /*
David Rientjes465adcf2013-04-29 15:08:45 -07001611 * If current has a pending SIGKILL or is exiting, then automatically
1612 * select it. The goal is to allow it to allocate so that it may
1613 * quickly exit and free its memory.
David Rientjes876aafb2012-07-31 16:43:48 -07001614 */
David Rientjes465adcf2013-04-29 15:08:45 -07001615 if (fatal_signal_pending(current) || current->flags & PF_EXITING) {
David Rientjes876aafb2012-07-31 16:43:48 -07001616 set_thread_flag(TIF_MEMDIE);
1617 return;
1618 }
1619
1620 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001621 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1622 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heo72ec7022013-08-08 20:11:26 -04001623 struct css_task_iter it;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001624 struct task_struct *task;
1625
Tejun Heo72ec7022013-08-08 20:11:26 -04001626 css_task_iter_start(&iter->css, &it);
1627 while ((task = css_task_iter_next(&it))) {
David Rientjes9cbb78b2012-07-31 16:43:44 -07001628 switch (oom_scan_process_thread(task, totalpages, NULL,
1629 false)) {
1630 case OOM_SCAN_SELECT:
1631 if (chosen)
1632 put_task_struct(chosen);
1633 chosen = task;
1634 chosen_points = ULONG_MAX;
1635 get_task_struct(chosen);
1636 /* fall through */
1637 case OOM_SCAN_CONTINUE:
1638 continue;
1639 case OOM_SCAN_ABORT:
Tejun Heo72ec7022013-08-08 20:11:26 -04001640 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001641 mem_cgroup_iter_break(memcg, iter);
1642 if (chosen)
1643 put_task_struct(chosen);
1644 return;
1645 case OOM_SCAN_OK:
1646 break;
1647 };
1648 points = oom_badness(task, memcg, NULL, totalpages);
1649 if (points > chosen_points) {
1650 if (chosen)
1651 put_task_struct(chosen);
1652 chosen = task;
1653 chosen_points = points;
1654 get_task_struct(chosen);
1655 }
1656 }
Tejun Heo72ec7022013-08-08 20:11:26 -04001657 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001658 }
1659
1660 if (!chosen)
1661 return;
1662 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001663 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1664 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001665}
1666
Johannes Weiner56600482012-01-12 17:17:59 -08001667static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1668 gfp_t gfp_mask,
1669 unsigned long flags)
1670{
1671 unsigned long total = 0;
1672 bool noswap = false;
1673 int loop;
1674
1675 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1676 noswap = true;
1677 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1678 noswap = true;
1679
1680 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1681 if (loop)
1682 drain_all_stock_async(memcg);
1683 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1684 /*
1685 * Allow limit shrinkers, which are triggered directly
1686 * by userspace, to catch signals and stop reclaim
1687 * after minimal progress, regardless of the margin.
1688 */
1689 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1690 break;
1691 if (mem_cgroup_margin(memcg))
1692 break;
1693 /*
1694 * If nothing was reclaimed after two attempts, there
1695 * may be no reclaimable pages in this hierarchy.
1696 */
1697 if (loop && !total)
1698 break;
1699 }
1700 return total;
1701}
1702
Michal Hockoe8831102013-09-12 15:13:23 -07001703#if MAX_NUMNODES > 1
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001704/**
1705 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001706 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001707 * @nid: the node ID to be checked.
1708 * @noswap : specify true here if the user wants flle only information.
1709 *
1710 * This function returns whether the specified memcg contains any
1711 * reclaimable pages on a node. Returns true if there are any reclaimable
1712 * pages in the node.
1713 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001714static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001715 int nid, bool noswap)
1716{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001717 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001718 return true;
1719 if (noswap || !total_swap_pages)
1720 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001721 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001722 return true;
1723 return false;
1724
1725}
Ying Han889976d2011-05-26 16:25:33 -07001726
1727/*
1728 * Always updating the nodemask is not very good - even if we have an empty
1729 * list or the wrong list here, we can start from some node and traverse all
1730 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1731 *
1732 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001733static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001734{
1735 int nid;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001736 /*
1737 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1738 * pagein/pageout changes since the last update.
1739 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001740 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001741 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001742 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001743 return;
1744
Ying Han889976d2011-05-26 16:25:33 -07001745 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001746 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001747
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001748 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001749
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001750 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1751 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001752 }
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001753
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001754 atomic_set(&memcg->numainfo_events, 0);
1755 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001756}
1757
1758/*
1759 * Selecting a node where we start reclaim from. Because what we need is just
1760 * reducing usage counter, start from anywhere is O,K. Considering
1761 * memory reclaim from current node, there are pros. and cons.
1762 *
1763 * Freeing memory from current node means freeing memory from a node which
1764 * we'll use or we've used. So, it may make LRU bad. And if several threads
1765 * hit limits, it will see a contention on a node. But freeing from remote
1766 * node means more costs for memory reclaim because of memory latency.
1767 *
1768 * Now, we use round-robin. Better algorithm is welcomed.
1769 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001770int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001771{
1772 int node;
1773
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001774 mem_cgroup_may_update_nodemask(memcg);
1775 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001776
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001777 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001778 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001779 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001780 /*
1781 * We call this when we hit limit, not when pages are added to LRU.
1782 * No LRU may hold pages because all pages are UNEVICTABLE or
1783 * memcg is too small and all pages are not on LRU. In that case,
1784 * we use curret node.
1785 */
1786 if (unlikely(node == MAX_NUMNODES))
1787 node = numa_node_id();
1788
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001789 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001790 return node;
1791}
1792
1793#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001794int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001795{
1796 return 0;
1797}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001798
Ying Han889976d2011-05-26 16:25:33 -07001799#endif
1800
Michal Hocko3b387222013-09-12 15:13:21 -07001801/*
Michal Hockoa5b7c872013-09-12 15:13:25 -07001802 * A group is eligible for the soft limit reclaim under the given root
1803 * hierarchy if
Andrew Mortonf894ffa2013-09-12 15:13:35 -07001804 * a) it is over its soft limit
1805 * b) any parent up the hierarchy is over its soft limit
Michal Hocko3b387222013-09-12 15:13:21 -07001806 */
Michal Hockode577802013-09-12 15:13:26 -07001807enum mem_cgroup_filter_t
1808mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
Michal Hockoa5b7c872013-09-12 15:13:25 -07001809 struct mem_cgroup *root)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001810{
Andrew Morton31200552013-09-24 15:27:35 -07001811 struct mem_cgroup *parent = memcg;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001812
Michal Hocko3b387222013-09-12 15:13:21 -07001813 if (res_counter_soft_limit_excess(&memcg->res))
Michal Hockode577802013-09-12 15:13:26 -07001814 return VISIT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001815
Michal Hocko3b387222013-09-12 15:13:21 -07001816 /*
Michal Hockoa5b7c872013-09-12 15:13:25 -07001817 * If any parent up to the root in the hierarchy is over its soft limit
1818 * then we have to obey and reclaim from this group as well.
Michal Hocko3b387222013-09-12 15:13:21 -07001819 */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07001820 while ((parent = parent_mem_cgroup(parent))) {
Michal Hocko3b387222013-09-12 15:13:21 -07001821 if (res_counter_soft_limit_excess(&parent->res))
Michal Hockode577802013-09-12 15:13:26 -07001822 return VISIT;
Michal Hockoa5b7c872013-09-12 15:13:25 -07001823 if (parent == root)
1824 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001825 }
Michal Hocko3b387222013-09-12 15:13:21 -07001826
Michal Hockode577802013-09-12 15:13:26 -07001827 return SKIP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001828}
1829
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001830static DEFINE_SPINLOCK(memcg_oom_lock);
1831
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001832/*
1833 * Check OOM-Killer is already running under our hierarchy.
1834 * If someone is running, return false.
1835 */
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001836static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001837{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001838 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001839
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001840 spin_lock(&memcg_oom_lock);
1841
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001842 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001843 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001844 /*
1845 * this subtree of our hierarchy is already locked
1846 * so we cannot give a lock.
1847 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001848 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001849 mem_cgroup_iter_break(memcg, iter);
1850 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001851 } else
1852 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001853 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001854
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001855 if (failed) {
1856 /*
1857 * OK, we failed to lock the whole subtree so we have
1858 * to clean up what we set up to the failing subtree
1859 */
1860 for_each_mem_cgroup_tree(iter, memcg) {
1861 if (iter == failed) {
1862 mem_cgroup_iter_break(memcg, iter);
1863 break;
1864 }
1865 iter->oom_lock = false;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001866 }
Michal Hocko79dfdac2011-07-26 16:08:23 -07001867 }
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001868
1869 spin_unlock(&memcg_oom_lock);
1870
1871 return !failed;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001872}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001873
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001874static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001875{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001876 struct mem_cgroup *iter;
1877
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001878 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001879 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001880 iter->oom_lock = false;
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001881 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001882}
1883
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001884static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001885{
1886 struct mem_cgroup *iter;
1887
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001888 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001889 atomic_inc(&iter->under_oom);
1890}
1891
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001892static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001893{
1894 struct mem_cgroup *iter;
1895
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001896 /*
1897 * When a new child is created while the hierarchy is under oom,
1898 * mem_cgroup_oom_lock() may not be called. We have to use
1899 * atomic_add_unless() here.
1900 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001901 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001902 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001903}
1904
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001905static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1906
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001907struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001908 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001909 wait_queue_t wait;
1910};
1911
1912static int memcg_oom_wake_function(wait_queue_t *wait,
1913 unsigned mode, int sync, void *arg)
1914{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001915 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1916 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001917 struct oom_wait_info *oom_wait_info;
1918
1919 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001920 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001921
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001922 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001923 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001924 * Then we can use css_is_ancestor without taking care of RCU.
1925 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001926 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
1927 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001928 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001929 return autoremove_wake_function(wait, mode, sync, arg);
1930}
1931
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001932static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001933{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001934 atomic_inc(&memcg->oom_wakeups);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001935 /* for filtering, pass "memcg" as argument. */
1936 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001937}
1938
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001939static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001940{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001941 if (memcg && atomic_read(&memcg->under_oom))
1942 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001943}
1944
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001945/*
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001946 * try to call OOM killer
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001947 */
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001948static void mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001949{
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001950 bool locked;
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001951 int wakeups;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001952
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001953 if (!current->memcg_oom.may_oom)
1954 return;
1955
1956 current->memcg_oom.in_memcg_oom = 1;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001957
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001958 /*
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001959 * As with any blocking lock, a contender needs to start
1960 * listening for wakeups before attempting the trylock,
1961 * otherwise it can miss the wakeup from the unlock and sleep
1962 * indefinitely. This is just open-coded because our locking
1963 * is so particular to memcg hierarchies.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001964 */
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001965 wakeups = atomic_read(&memcg->oom_wakeups);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001966 mem_cgroup_mark_under_oom(memcg);
1967
1968 locked = mem_cgroup_oom_trylock(memcg);
1969
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001970 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001971 mem_cgroup_oom_notify(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001972
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001973 if (locked && !memcg->oom_kill_disable) {
1974 mem_cgroup_unmark_under_oom(memcg);
David Rientjese845e192012-03-21 16:34:10 -07001975 mem_cgroup_out_of_memory(memcg, mask, order);
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001976 mem_cgroup_oom_unlock(memcg);
1977 /*
1978 * There is no guarantee that an OOM-lock contender
1979 * sees the wakeups triggered by the OOM kill
1980 * uncharges. Wake any sleepers explicitely.
1981 */
1982 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001983 } else {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001984 /*
1985 * A system call can just return -ENOMEM, but if this
1986 * is a page fault and somebody else is handling the
1987 * OOM already, we need to sleep on the OOM waitqueue
1988 * for this memcg until the situation is resolved.
1989 * Which can take some time because it might be
1990 * handled by a userspace task.
1991 *
1992 * However, this is the charge context, which means
1993 * that we may sit on a large call stack and hold
1994 * various filesystem locks, the mmap_sem etc. and we
1995 * don't want the OOM handler to deadlock on them
1996 * while we sit here and wait. Store the current OOM
1997 * context in the task_struct, then return -ENOMEM.
1998 * At the end of the page fault handler, with the
1999 * stack unwound, pagefault_out_of_memory() will check
2000 * back with us by calling
2001 * mem_cgroup_oom_synchronize(), possibly putting the
2002 * task to sleep.
2003 */
2004 current->memcg_oom.oom_locked = locked;
2005 current->memcg_oom.wakeups = wakeups;
2006 css_get(&memcg->css);
2007 current->memcg_oom.wait_on_memcg = memcg;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002008 }
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002009}
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002010
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002011/**
2012 * mem_cgroup_oom_synchronize - complete memcg OOM handling
2013 *
2014 * This has to be called at the end of a page fault if the the memcg
2015 * OOM handler was enabled and the fault is returning %VM_FAULT_OOM.
2016 *
2017 * Memcg supports userspace OOM handling, so failed allocations must
2018 * sleep on a waitqueue until the userspace task resolves the
2019 * situation. Sleeping directly in the charge context with all kinds
2020 * of locks held is not a good idea, instead we remember an OOM state
2021 * in the task and mem_cgroup_oom_synchronize() has to be called at
2022 * the end of the page fault to put the task to sleep and clean up the
2023 * OOM state.
2024 *
2025 * Returns %true if an ongoing memcg OOM situation was detected and
2026 * finalized, %false otherwise.
2027 */
2028bool mem_cgroup_oom_synchronize(void)
2029{
2030 struct oom_wait_info owait;
2031 struct mem_cgroup *memcg;
2032
2033 /* OOM is global, do not handle */
2034 if (!current->memcg_oom.in_memcg_oom)
2035 return false;
2036
2037 /*
2038 * We invoked the OOM killer but there is a chance that a kill
2039 * did not free up any charges. Everybody else might already
2040 * be sleeping, so restart the fault and keep the rampage
2041 * going until some charges are released.
2042 */
2043 memcg = current->memcg_oom.wait_on_memcg;
2044 if (!memcg)
2045 goto out;
2046
2047 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2048 goto out_memcg;
2049
2050 owait.memcg = memcg;
2051 owait.wait.flags = 0;
2052 owait.wait.func = memcg_oom_wake_function;
2053 owait.wait.private = current;
2054 INIT_LIST_HEAD(&owait.wait.task_list);
2055
2056 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
2057 /* Only sleep if we didn't miss any wakeups since OOM */
2058 if (atomic_read(&memcg->oom_wakeups) == current->memcg_oom.wakeups)
2059 schedule();
2060 finish_wait(&memcg_oom_waitq, &owait.wait);
2061out_memcg:
2062 mem_cgroup_unmark_under_oom(memcg);
2063 if (current->memcg_oom.oom_locked) {
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002064 mem_cgroup_oom_unlock(memcg);
2065 /*
2066 * There is no guarantee that an OOM-lock contender
2067 * sees the wakeups triggered by the OOM kill
2068 * uncharges. Wake any sleepers explicitely.
2069 */
2070 memcg_oom_recover(memcg);
2071 }
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002072 css_put(&memcg->css);
2073 current->memcg_oom.wait_on_memcg = NULL;
2074out:
2075 current->memcg_oom.in_memcg_oom = 0;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002076 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002077}
2078
Balbir Singhd69b0422009-06-17 16:26:34 -07002079/*
2080 * Currently used to update mapped file statistics, but the routine can be
2081 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002082 *
2083 * Notes: Race condition
2084 *
2085 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2086 * it tends to be costly. But considering some conditions, we doesn't need
2087 * to do so _always_.
2088 *
2089 * Considering "charge", lock_page_cgroup() is not required because all
2090 * file-stat operations happen after a page is attached to radix-tree. There
2091 * are no race with "charge".
2092 *
2093 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2094 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2095 * if there are race with "uncharge". Statistics itself is properly handled
2096 * by flags.
2097 *
2098 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002099 * small, we check mm->moving_account and detect there are possibility of race
2100 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002101 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002102
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002103void __mem_cgroup_begin_update_page_stat(struct page *page,
2104 bool *locked, unsigned long *flags)
2105{
2106 struct mem_cgroup *memcg;
2107 struct page_cgroup *pc;
2108
2109 pc = lookup_page_cgroup(page);
2110again:
2111 memcg = pc->mem_cgroup;
2112 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2113 return;
2114 /*
2115 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002116 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002117 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002118 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002119 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002120 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002121 return;
2122
2123 move_lock_mem_cgroup(memcg, flags);
2124 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2125 move_unlock_mem_cgroup(memcg, flags);
2126 goto again;
2127 }
2128 *locked = true;
2129}
2130
2131void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2132{
2133 struct page_cgroup *pc = lookup_page_cgroup(page);
2134
2135 /*
2136 * It's guaranteed that pc->mem_cgroup never changes while
2137 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002138 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002139 */
2140 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2141}
2142
Greg Thelen2a7106f2011-01-13 15:47:37 -08002143void mem_cgroup_update_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -07002144 enum mem_cgroup_stat_index idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002145{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002146 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002147 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002148 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002149
Johannes Weinercfa44942012-01-12 17:18:38 -08002150 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002151 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002152
Sha Zhengju658b72c2013-09-12 15:13:52 -07002153 VM_BUG_ON(!rcu_read_lock_held());
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002154 memcg = pc->mem_cgroup;
2155 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002156 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002157
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002158 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002159}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002160
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002161/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002162 * size of first charge trial. "32" comes from vmscan.c's magic value.
2163 * TODO: maybe necessary to use big numbers in big irons.
2164 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002165#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002166struct memcg_stock_pcp {
2167 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002168 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002169 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002170 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002171#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002172};
2173static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002174static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002175
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002176/**
2177 * consume_stock: Try to consume stocked charge on this cpu.
2178 * @memcg: memcg to consume from.
2179 * @nr_pages: how many pages to charge.
2180 *
2181 * The charges will only happen if @memcg matches the current cpu's memcg
2182 * stock, and at least @nr_pages are available in that stock. Failure to
2183 * service an allocation will refill the stock.
2184 *
2185 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002186 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002187static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002188{
2189 struct memcg_stock_pcp *stock;
2190 bool ret = true;
2191
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002192 if (nr_pages > CHARGE_BATCH)
2193 return false;
2194
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002195 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002196 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2197 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002198 else /* need to call res_counter_charge */
2199 ret = false;
2200 put_cpu_var(memcg_stock);
2201 return ret;
2202}
2203
2204/*
2205 * Returns stocks cached in percpu to res_counter and reset cached information.
2206 */
2207static void drain_stock(struct memcg_stock_pcp *stock)
2208{
2209 struct mem_cgroup *old = stock->cached;
2210
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002211 if (stock->nr_pages) {
2212 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2213
2214 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002215 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002216 res_counter_uncharge(&old->memsw, bytes);
2217 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002218 }
2219 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002220}
2221
2222/*
2223 * This must be called under preempt disabled or must be called by
2224 * a thread which is pinned to local cpu.
2225 */
2226static void drain_local_stock(struct work_struct *dummy)
2227{
2228 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2229 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002230 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002231}
2232
Michal Hockoe4777492013-02-22 16:35:40 -08002233static void __init memcg_stock_init(void)
2234{
2235 int cpu;
2236
2237 for_each_possible_cpu(cpu) {
2238 struct memcg_stock_pcp *stock =
2239 &per_cpu(memcg_stock, cpu);
2240 INIT_WORK(&stock->work, drain_local_stock);
2241 }
2242}
2243
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002244/*
2245 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002246 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002247 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002248static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002249{
2250 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2251
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002252 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002253 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002254 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002255 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002256 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002257 put_cpu_var(memcg_stock);
2258}
2259
2260/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002261 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002262 * of the hierarchy under it. sync flag says whether we should block
2263 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002264 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002265static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002266{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002267 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002268
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002269 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002270 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002271 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002272 for_each_online_cpu(cpu) {
2273 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002274 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002275
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002276 memcg = stock->cached;
2277 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002278 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002279 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002280 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002281 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2282 if (cpu == curcpu)
2283 drain_local_stock(&stock->work);
2284 else
2285 schedule_work_on(cpu, &stock->work);
2286 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002287 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002288 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002289
2290 if (!sync)
2291 goto out;
2292
2293 for_each_online_cpu(cpu) {
2294 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002295 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002296 flush_work(&stock->work);
2297 }
2298out:
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002299 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002300}
2301
2302/*
2303 * Tries to drain stocked charges in other cpus. This function is asynchronous
2304 * and just put a work per cpu for draining localy on each cpu. Caller can
2305 * expects some charges will be back to res_counter later but cannot wait for
2306 * it.
2307 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002308static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002309{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002310 /*
2311 * If someone calls draining, avoid adding more kworker runs.
2312 */
2313 if (!mutex_trylock(&percpu_charge_mutex))
2314 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002315 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002316 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002317}
2318
2319/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002320static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002321{
2322 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002323 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002324 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002325 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002326}
2327
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002328/*
2329 * This function drains percpu counter value from DEAD cpu and
2330 * move it to local cpu. Note that this function can be preempted.
2331 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002332static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002333{
2334 int i;
2335
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002336 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002337 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002338 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002339
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002340 per_cpu(memcg->stat->count[i], cpu) = 0;
2341 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002342 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002343 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002344 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002345
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002346 per_cpu(memcg->stat->events[i], cpu) = 0;
2347 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002348 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002349 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002350}
2351
Paul Gortmaker0db06282013-06-19 14:53:51 -04002352static int memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002353 unsigned long action,
2354 void *hcpu)
2355{
2356 int cpu = (unsigned long)hcpu;
2357 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002358 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002359
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002360 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002361 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002362
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002363 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002364 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002365
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002366 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002367 mem_cgroup_drain_pcp_counter(iter, cpu);
2368
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002369 stock = &per_cpu(memcg_stock, cpu);
2370 drain_stock(stock);
2371 return NOTIFY_OK;
2372}
2373
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002374
2375/* See __mem_cgroup_try_charge() for details */
2376enum {
2377 CHARGE_OK, /* success */
2378 CHARGE_RETRY, /* need to retry but retry is not bad */
2379 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2380 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002381};
2382
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002383static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002384 unsigned int nr_pages, unsigned int min_pages,
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002385 bool invoke_oom)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002386{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002387 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002388 struct mem_cgroup *mem_over_limit;
2389 struct res_counter *fail_res;
2390 unsigned long flags = 0;
2391 int ret;
2392
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002393 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002394
2395 if (likely(!ret)) {
2396 if (!do_swap_account)
2397 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002398 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002399 if (likely(!ret))
2400 return CHARGE_OK;
2401
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002402 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002403 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2404 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2405 } else
2406 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002407 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002408 * Never reclaim on behalf of optional batching, retry with a
2409 * single page instead.
2410 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002411 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002412 return CHARGE_RETRY;
2413
2414 if (!(gfp_mask & __GFP_WAIT))
2415 return CHARGE_WOULDBLOCK;
2416
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002417 if (gfp_mask & __GFP_NORETRY)
2418 return CHARGE_NOMEM;
2419
Johannes Weiner56600482012-01-12 17:17:59 -08002420 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002421 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002422 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002423 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002424 * Even though the limit is exceeded at this point, reclaim
2425 * may have been able to free some pages. Retry the charge
2426 * before killing the task.
2427 *
2428 * Only for regular pages, though: huge pages are rather
2429 * unlikely to succeed so close to the limit, and we fall back
2430 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002431 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002432 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002433 return CHARGE_RETRY;
2434
2435 /*
2436 * At task move, charge accounts can be doubly counted. So, it's
2437 * better to wait until the end of task_move if something is going on.
2438 */
2439 if (mem_cgroup_wait_acct_move(mem_over_limit))
2440 return CHARGE_RETRY;
2441
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002442 if (invoke_oom)
2443 mem_cgroup_oom(mem_over_limit, gfp_mask, get_order(csize));
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002444
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002445 return CHARGE_NOMEM;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002446}
2447
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002448/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002449 * __mem_cgroup_try_charge() does
2450 * 1. detect memcg to be charged against from passed *mm and *ptr,
2451 * 2. update res_counter
2452 * 3. call memory reclaim if necessary.
2453 *
2454 * In some special case, if the task is fatal, fatal_signal_pending() or
2455 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2456 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2457 * as possible without any hazards. 2: all pages should have a valid
2458 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2459 * pointer, that is treated as a charge to root_mem_cgroup.
2460 *
2461 * So __mem_cgroup_try_charge() will return
2462 * 0 ... on success, filling *ptr with a valid memcg pointer.
2463 * -ENOMEM ... charge failure because of resource limits.
2464 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2465 *
2466 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2467 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002468 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002469static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002470 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002471 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002472 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002473 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002474{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002475 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002476 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002477 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002478 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002479
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002480 /*
2481 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2482 * in system level. So, allow to go ahead dying process in addition to
2483 * MEMDIE process.
2484 */
2485 if (unlikely(test_thread_flag(TIF_MEMDIE)
2486 || fatal_signal_pending(current)))
2487 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002488
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002489 /*
Hugh Dickins3be912772008-02-07 00:14:19 -08002490 * We always charge the cgroup the mm_struct belongs to.
2491 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002492 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002493 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002494 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002495 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002496 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002497again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002498 if (*ptr) { /* css should be a valid one */
2499 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002500 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002501 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002502 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002503 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002504 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002505 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002506 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002507
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002508 rcu_read_lock();
2509 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002510 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002511 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002512 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002513 * race with swapoff. Then, we have small risk of mis-accouning.
2514 * But such kind of mis-account by race always happens because
2515 * we don't have cgroup_mutex(). It's overkill and we allo that
2516 * small race, here.
2517 * (*) swapoff at el will charge against mm-struct not against
2518 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002519 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002520 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002521 if (!memcg)
2522 memcg = root_mem_cgroup;
2523 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002524 rcu_read_unlock();
2525 goto done;
2526 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002527 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002528 /*
2529 * It seems dagerous to access memcg without css_get().
2530 * But considering how consume_stok works, it's not
2531 * necessary. If consume_stock success, some charges
2532 * from this memcg are cached on this cpu. So, we
2533 * don't need to call css_get()/css_tryget() before
2534 * calling consume_stock().
2535 */
2536 rcu_read_unlock();
2537 goto done;
2538 }
2539 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002540 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002541 rcu_read_unlock();
2542 goto again;
2543 }
2544 rcu_read_unlock();
2545 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002546
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002547 do {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002548 bool invoke_oom = oom && !nr_oom_retries;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002549
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002550 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002551 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002552 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002553 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002554 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002555
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002556 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch,
2557 nr_pages, invoke_oom);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002558 switch (ret) {
2559 case CHARGE_OK:
2560 break;
2561 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002562 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002563 css_put(&memcg->css);
2564 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002565 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002566 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002567 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002568 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002569 case CHARGE_NOMEM: /* OOM routine works */
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002570 if (!oom || invoke_oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002571 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002572 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002573 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002574 nr_oom_retries--;
2575 break;
Balbir Singh66e17072008-02-07 00:13:56 -08002576 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002577 } while (ret != CHARGE_OK);
2578
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002579 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002580 refill_stock(memcg, batch - nr_pages);
2581 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002582done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002583 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002584 return 0;
2585nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002586 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002587 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002588bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002589 *ptr = root_mem_cgroup;
2590 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002591}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002592
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002593/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002594 * Somemtimes we have to undo a charge we got by try_charge().
2595 * This function is for that and do uncharge, put css's refcnt.
2596 * gotten by try_charge().
2597 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002598static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002599 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002600{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002601 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002602 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002603
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002604 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002605 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002606 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002607 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002608}
2609
2610/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002611 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2612 * This is useful when moving usage to parent cgroup.
2613 */
2614static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2615 unsigned int nr_pages)
2616{
2617 unsigned long bytes = nr_pages * PAGE_SIZE;
2618
2619 if (mem_cgroup_is_root(memcg))
2620 return;
2621
2622 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2623 if (do_swap_account)
2624 res_counter_uncharge_until(&memcg->memsw,
2625 memcg->memsw.parent, bytes);
2626}
2627
2628/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002629 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002630 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2631 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2632 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002633 */
2634static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2635{
2636 struct cgroup_subsys_state *css;
2637
2638 /* ID 0 is unused ID */
2639 if (!id)
2640 return NULL;
2641 css = css_lookup(&mem_cgroup_subsys, id);
2642 if (!css)
2643 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002644 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002645}
2646
Wu Fengguange42d9d52009-12-16 12:19:59 +01002647struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002648{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002649 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002650 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002651 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002652 swp_entry_t ent;
2653
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002654 VM_BUG_ON(!PageLocked(page));
2655
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002656 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002657 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002658 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002659 memcg = pc->mem_cgroup;
2660 if (memcg && !css_tryget(&memcg->css))
2661 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002662 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002663 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002664 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002665 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002666 memcg = mem_cgroup_lookup(id);
2667 if (memcg && !css_tryget(&memcg->css))
2668 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002669 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002670 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002671 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002672 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002673}
2674
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002675static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002676 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002677 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002678 enum charge_type ctype,
2679 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002680{
Johannes Weinerce587e62012-04-24 20:22:33 +02002681 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002682 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002683 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002684 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002685 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002686
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002687 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002688 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002689 /*
2690 * we don't need page_cgroup_lock about tail pages, becase they are not
2691 * accessed by any other context at this point.
2692 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002693
2694 /*
2695 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2696 * may already be on some other mem_cgroup's LRU. Take care of it.
2697 */
2698 if (lrucare) {
2699 zone = page_zone(page);
2700 spin_lock_irq(&zone->lru_lock);
2701 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002702 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002703 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002704 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002705 was_on_lru = true;
2706 }
2707 }
2708
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002709 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002710 /*
2711 * We access a page_cgroup asynchronously without lock_page_cgroup().
2712 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2713 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2714 * before USED bit, we need memory barrier here.
2715 * See mem_cgroup_add_lru_list(), etc.
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002716 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002717 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002718 SetPageCgroupUsed(pc);
Hugh Dickins3be912772008-02-07 00:14:19 -08002719
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002720 if (lrucare) {
2721 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002722 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002723 VM_BUG_ON(PageLRU(page));
2724 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002725 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002726 }
2727 spin_unlock_irq(&zone->lru_lock);
2728 }
2729
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002730 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002731 anon = true;
2732 else
2733 anon = false;
2734
David Rientjesb070e652013-05-07 16:18:09 -07002735 mem_cgroup_charge_statistics(memcg, page, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002736 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002737
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002738 /*
Michal Hockoe8831102013-09-12 15:13:23 -07002739 * "charge_statistics" updated event counter.
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002740 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002741 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002742}
2743
Glauber Costa7cf27982012-12-18 14:22:55 -08002744static DEFINE_MUTEX(set_limit_mutex);
2745
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002746#ifdef CONFIG_MEMCG_KMEM
2747static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2748{
2749 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2750 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2751}
2752
Glauber Costa1f458cb2012-12-18 14:22:50 -08002753/*
2754 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2755 * in the memcg_cache_params struct.
2756 */
2757static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2758{
2759 struct kmem_cache *cachep;
2760
2761 VM_BUG_ON(p->is_root_cache);
2762 cachep = p->root_cache;
2763 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
2764}
2765
Glauber Costa749c5412012-12-18 14:23:01 -08002766#ifdef CONFIG_SLABINFO
Tejun Heo182446d2013-08-08 20:11:24 -04002767static int mem_cgroup_slabinfo_read(struct cgroup_subsys_state *css,
2768 struct cftype *cft, struct seq_file *m)
Glauber Costa749c5412012-12-18 14:23:01 -08002769{
Tejun Heo182446d2013-08-08 20:11:24 -04002770 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa749c5412012-12-18 14:23:01 -08002771 struct memcg_cache_params *params;
2772
2773 if (!memcg_can_account_kmem(memcg))
2774 return -EIO;
2775
2776 print_slabinfo_header(m);
2777
2778 mutex_lock(&memcg->slab_caches_mutex);
2779 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
2780 cache_show(memcg_params_to_cache(params), m);
2781 mutex_unlock(&memcg->slab_caches_mutex);
2782
2783 return 0;
2784}
2785#endif
2786
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002787static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2788{
2789 struct res_counter *fail_res;
2790 struct mem_cgroup *_memcg;
2791 int ret = 0;
2792 bool may_oom;
2793
2794 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2795 if (ret)
2796 return ret;
2797
2798 /*
2799 * Conditions under which we can wait for the oom_killer. Those are
2800 * the same conditions tested by the core page allocator
2801 */
2802 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2803
2804 _memcg = memcg;
2805 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2806 &_memcg, may_oom);
2807
2808 if (ret == -EINTR) {
2809 /*
2810 * __mem_cgroup_try_charge() chosed to bypass to root due to
2811 * OOM kill or fatal signal. Since our only options are to
2812 * either fail the allocation or charge it to this cgroup, do
2813 * it as a temporary condition. But we can't fail. From a
2814 * kmem/slab perspective, the cache has already been selected,
2815 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2816 * our minds.
2817 *
2818 * This condition will only trigger if the task entered
2819 * memcg_charge_kmem in a sane state, but was OOM-killed during
2820 * __mem_cgroup_try_charge() above. Tasks that were already
2821 * dying when the allocation triggers should have been already
2822 * directed to the root cgroup in memcontrol.h
2823 */
2824 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2825 if (do_swap_account)
2826 res_counter_charge_nofail(&memcg->memsw, size,
2827 &fail_res);
2828 ret = 0;
2829 } else if (ret)
2830 res_counter_uncharge(&memcg->kmem, size);
2831
2832 return ret;
2833}
2834
2835static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2836{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002837 res_counter_uncharge(&memcg->res, size);
2838 if (do_swap_account)
2839 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002840
2841 /* Not down to 0 */
2842 if (res_counter_uncharge(&memcg->kmem, size))
2843 return;
2844
Li Zefan10d5ebf2013-07-08 16:00:33 -07002845 /*
2846 * Releases a reference taken in kmem_cgroup_css_offline in case
2847 * this last uncharge is racing with the offlining code or it is
2848 * outliving the memcg existence.
2849 *
2850 * The memory barrier imposed by test&clear is paired with the
2851 * explicit one in memcg_kmem_mark_dead().
2852 */
Glauber Costa7de37682012-12-18 14:22:07 -08002853 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07002854 css_put(&memcg->css);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002855}
2856
Glauber Costa2633d7a2012-12-18 14:22:34 -08002857void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
2858{
2859 if (!memcg)
2860 return;
2861
2862 mutex_lock(&memcg->slab_caches_mutex);
2863 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
2864 mutex_unlock(&memcg->slab_caches_mutex);
2865}
2866
2867/*
2868 * helper for acessing a memcg's index. It will be used as an index in the
2869 * child cache array in kmem_cache, and also to derive its name. This function
2870 * will return -1 when this is not a kmem-limited memcg.
2871 */
2872int memcg_cache_id(struct mem_cgroup *memcg)
2873{
2874 return memcg ? memcg->kmemcg_id : -1;
2875}
2876
Glauber Costa55007d82012-12-18 14:22:38 -08002877/*
2878 * This ends up being protected by the set_limit mutex, during normal
2879 * operation, because that is its main call site.
2880 *
2881 * But when we create a new cache, we can call this as well if its parent
2882 * is kmem-limited. That will have to hold set_limit_mutex as well.
2883 */
2884int memcg_update_cache_sizes(struct mem_cgroup *memcg)
2885{
2886 int num, ret;
2887
2888 num = ida_simple_get(&kmem_limited_groups,
2889 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2890 if (num < 0)
2891 return num;
2892 /*
2893 * After this point, kmem_accounted (that we test atomically in
2894 * the beginning of this conditional), is no longer 0. This
2895 * guarantees only one process will set the following boolean
2896 * to true. We don't need test_and_set because we're protected
2897 * by the set_limit_mutex anyway.
2898 */
2899 memcg_kmem_set_activated(memcg);
2900
2901 ret = memcg_update_all_caches(num+1);
2902 if (ret) {
2903 ida_simple_remove(&kmem_limited_groups, num);
2904 memcg_kmem_clear_activated(memcg);
2905 return ret;
2906 }
2907
2908 memcg->kmemcg_id = num;
2909 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
2910 mutex_init(&memcg->slab_caches_mutex);
2911 return 0;
2912}
2913
2914static size_t memcg_caches_array_size(int num_groups)
2915{
2916 ssize_t size;
2917 if (num_groups <= 0)
2918 return 0;
2919
2920 size = 2 * num_groups;
2921 if (size < MEMCG_CACHES_MIN_SIZE)
2922 size = MEMCG_CACHES_MIN_SIZE;
2923 else if (size > MEMCG_CACHES_MAX_SIZE)
2924 size = MEMCG_CACHES_MAX_SIZE;
2925
2926 return size;
2927}
2928
2929/*
2930 * We should update the current array size iff all caches updates succeed. This
2931 * can only be done from the slab side. The slab mutex needs to be held when
2932 * calling this.
2933 */
2934void memcg_update_array_size(int num)
2935{
2936 if (num > memcg_limited_groups_array_size)
2937 memcg_limited_groups_array_size = memcg_caches_array_size(num);
2938}
2939
Konstantin Khlebnikov15cf17d2013-03-08 12:43:36 -08002940static void kmem_cache_destroy_work_func(struct work_struct *w);
2941
Glauber Costa55007d82012-12-18 14:22:38 -08002942int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
2943{
2944 struct memcg_cache_params *cur_params = s->memcg_params;
2945
2946 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
2947
2948 if (num_groups > memcg_limited_groups_array_size) {
2949 int i;
2950 ssize_t size = memcg_caches_array_size(num_groups);
2951
2952 size *= sizeof(void *);
Andrey Vagin90c7a792013-09-11 14:22:18 -07002953 size += offsetof(struct memcg_cache_params, memcg_caches);
Glauber Costa55007d82012-12-18 14:22:38 -08002954
2955 s->memcg_params = kzalloc(size, GFP_KERNEL);
2956 if (!s->memcg_params) {
2957 s->memcg_params = cur_params;
2958 return -ENOMEM;
2959 }
2960
2961 s->memcg_params->is_root_cache = true;
2962
2963 /*
2964 * There is the chance it will be bigger than
2965 * memcg_limited_groups_array_size, if we failed an allocation
2966 * in a cache, in which case all caches updated before it, will
2967 * have a bigger array.
2968 *
2969 * But if that is the case, the data after
2970 * memcg_limited_groups_array_size is certainly unused
2971 */
2972 for (i = 0; i < memcg_limited_groups_array_size; i++) {
2973 if (!cur_params->memcg_caches[i])
2974 continue;
2975 s->memcg_params->memcg_caches[i] =
2976 cur_params->memcg_caches[i];
2977 }
2978
2979 /*
2980 * Ideally, we would wait until all caches succeed, and only
2981 * then free the old one. But this is not worth the extra
2982 * pointer per-cache we'd have to have for this.
2983 *
2984 * It is not a big deal if some caches are left with a size
2985 * bigger than the others. And all updates will reset this
2986 * anyway.
2987 */
2988 kfree(cur_params);
2989 }
2990 return 0;
2991}
2992
Glauber Costa943a4512012-12-18 14:23:03 -08002993int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
2994 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08002995{
Andrey Vagin90c7a792013-09-11 14:22:18 -07002996 size_t size;
Glauber Costa2633d7a2012-12-18 14:22:34 -08002997
2998 if (!memcg_kmem_enabled())
2999 return 0;
3000
Andrey Vagin90c7a792013-09-11 14:22:18 -07003001 if (!memcg) {
3002 size = offsetof(struct memcg_cache_params, memcg_caches);
Glauber Costa55007d82012-12-18 14:22:38 -08003003 size += memcg_limited_groups_array_size * sizeof(void *);
Andrey Vagin90c7a792013-09-11 14:22:18 -07003004 } else
3005 size = sizeof(struct memcg_cache_params);
Glauber Costa55007d82012-12-18 14:22:38 -08003006
Glauber Costa2633d7a2012-12-18 14:22:34 -08003007 s->memcg_params = kzalloc(size, GFP_KERNEL);
3008 if (!s->memcg_params)
3009 return -ENOMEM;
3010
Glauber Costa943a4512012-12-18 14:23:03 -08003011 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003012 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003013 s->memcg_params->root_cache = root_cache;
Andrey Vagin3e6b11d2013-08-13 16:00:47 -07003014 INIT_WORK(&s->memcg_params->destroy,
3015 kmem_cache_destroy_work_func);
Glauber Costa4ba902b2013-02-12 13:46:22 -08003016 } else
3017 s->memcg_params->is_root_cache = true;
3018
Glauber Costa2633d7a2012-12-18 14:22:34 -08003019 return 0;
3020}
3021
3022void memcg_release_cache(struct kmem_cache *s)
3023{
Glauber Costad7f25f82012-12-18 14:22:40 -08003024 struct kmem_cache *root;
3025 struct mem_cgroup *memcg;
3026 int id;
3027
3028 /*
3029 * This happens, for instance, when a root cache goes away before we
3030 * add any memcg.
3031 */
3032 if (!s->memcg_params)
3033 return;
3034
3035 if (s->memcg_params->is_root_cache)
3036 goto out;
3037
3038 memcg = s->memcg_params->memcg;
3039 id = memcg_cache_id(memcg);
3040
3041 root = s->memcg_params->root_cache;
3042 root->memcg_params->memcg_caches[id] = NULL;
Glauber Costad7f25f82012-12-18 14:22:40 -08003043
3044 mutex_lock(&memcg->slab_caches_mutex);
3045 list_del(&s->memcg_params->list);
3046 mutex_unlock(&memcg->slab_caches_mutex);
3047
Li Zefan20f05312013-07-08 16:00:31 -07003048 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003049out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003050 kfree(s->memcg_params);
3051}
3052
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003053/*
3054 * During the creation a new cache, we need to disable our accounting mechanism
3055 * altogether. This is true even if we are not creating, but rather just
3056 * enqueing new caches to be created.
3057 *
3058 * This is because that process will trigger allocations; some visible, like
3059 * explicit kmallocs to auxiliary data structures, name strings and internal
3060 * cache structures; some well concealed, like INIT_WORK() that can allocate
3061 * objects during debug.
3062 *
3063 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3064 * to it. This may not be a bounded recursion: since the first cache creation
3065 * failed to complete (waiting on the allocation), we'll just try to create the
3066 * cache again, failing at the same point.
3067 *
3068 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3069 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3070 * inside the following two functions.
3071 */
3072static inline void memcg_stop_kmem_account(void)
3073{
3074 VM_BUG_ON(!current->mm);
3075 current->memcg_kmem_skip_account++;
3076}
3077
3078static inline void memcg_resume_kmem_account(void)
3079{
3080 VM_BUG_ON(!current->mm);
3081 current->memcg_kmem_skip_account--;
3082}
3083
Glauber Costa1f458cb2012-12-18 14:22:50 -08003084static void kmem_cache_destroy_work_func(struct work_struct *w)
3085{
3086 struct kmem_cache *cachep;
3087 struct memcg_cache_params *p;
3088
3089 p = container_of(w, struct memcg_cache_params, destroy);
3090
3091 cachep = memcg_params_to_cache(p);
3092
Glauber Costa22933152012-12-18 14:22:59 -08003093 /*
3094 * If we get down to 0 after shrink, we could delete right away.
3095 * However, memcg_release_pages() already puts us back in the workqueue
3096 * in that case. If we proceed deleting, we'll get a dangling
3097 * reference, and removing the object from the workqueue in that case
3098 * is unnecessary complication. We are not a fast path.
3099 *
3100 * Note that this case is fundamentally different from racing with
3101 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3102 * kmem_cache_shrink, not only we would be reinserting a dead cache
3103 * into the queue, but doing so from inside the worker racing to
3104 * destroy it.
3105 *
3106 * So if we aren't down to zero, we'll just schedule a worker and try
3107 * again
3108 */
3109 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3110 kmem_cache_shrink(cachep);
3111 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3112 return;
3113 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003114 kmem_cache_destroy(cachep);
3115}
3116
3117void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3118{
3119 if (!cachep->memcg_params->dead)
3120 return;
3121
3122 /*
Glauber Costa22933152012-12-18 14:22:59 -08003123 * There are many ways in which we can get here.
3124 *
3125 * We can get to a memory-pressure situation while the delayed work is
3126 * still pending to run. The vmscan shrinkers can then release all
3127 * cache memory and get us to destruction. If this is the case, we'll
3128 * be executed twice, which is a bug (the second time will execute over
3129 * bogus data). In this case, cancelling the work should be fine.
3130 *
3131 * But we can also get here from the worker itself, if
3132 * kmem_cache_shrink is enough to shake all the remaining objects and
3133 * get the page count to 0. In this case, we'll deadlock if we try to
3134 * cancel the work (the worker runs with an internal lock held, which
3135 * is the same lock we would hold for cancel_work_sync().)
3136 *
3137 * Since we can't possibly know who got us here, just refrain from
3138 * running if there is already work pending
3139 */
3140 if (work_pending(&cachep->memcg_params->destroy))
3141 return;
3142 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003143 * We have to defer the actual destroying to a workqueue, because
3144 * we might currently be in a context that cannot sleep.
3145 */
3146 schedule_work(&cachep->memcg_params->destroy);
3147}
3148
Glauber Costad7f25f82012-12-18 14:22:40 -08003149/*
3150 * This lock protects updaters, not readers. We want readers to be as fast as
3151 * they can, and they will either see NULL or a valid cache value. Our model
3152 * allow them to see NULL, in which case the root memcg will be selected.
3153 *
3154 * We need this lock because multiple allocations to the same cache from a non
3155 * will span more than one worker. Only one of them can create the cache.
3156 */
3157static DEFINE_MUTEX(memcg_cache_mutex);
Michal Hockod9c10dd2013-03-28 08:48:14 +01003158
3159/*
3160 * Called with memcg_cache_mutex held
3161 */
3162static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3163 struct kmem_cache *s)
3164{
3165 struct kmem_cache *new;
3166 static char *tmp_name = NULL;
3167
3168 lockdep_assert_held(&memcg_cache_mutex);
3169
3170 /*
3171 * kmem_cache_create_memcg duplicates the given name and
3172 * cgroup_name for this name requires RCU context.
3173 * This static temporary buffer is used to prevent from
3174 * pointless shortliving allocation.
3175 */
3176 if (!tmp_name) {
3177 tmp_name = kmalloc(PATH_MAX, GFP_KERNEL);
3178 if (!tmp_name)
3179 return NULL;
3180 }
3181
3182 rcu_read_lock();
3183 snprintf(tmp_name, PATH_MAX, "%s(%d:%s)", s->name,
3184 memcg_cache_id(memcg), cgroup_name(memcg->css.cgroup));
3185 rcu_read_unlock();
3186
3187 new = kmem_cache_create_memcg(memcg, tmp_name, s->object_size, s->align,
3188 (s->flags & ~SLAB_PANIC), s->ctor, s);
3189
3190 if (new)
3191 new->allocflags |= __GFP_KMEMCG;
3192
3193 return new;
3194}
3195
Glauber Costad7f25f82012-12-18 14:22:40 -08003196static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3197 struct kmem_cache *cachep)
3198{
3199 struct kmem_cache *new_cachep;
3200 int idx;
3201
3202 BUG_ON(!memcg_can_account_kmem(memcg));
3203
3204 idx = memcg_cache_id(memcg);
3205
3206 mutex_lock(&memcg_cache_mutex);
3207 new_cachep = cachep->memcg_params->memcg_caches[idx];
Li Zefan20f05312013-07-08 16:00:31 -07003208 if (new_cachep) {
3209 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003210 goto out;
Li Zefan20f05312013-07-08 16:00:31 -07003211 }
Glauber Costad7f25f82012-12-18 14:22:40 -08003212
3213 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003214 if (new_cachep == NULL) {
3215 new_cachep = cachep;
Li Zefan20f05312013-07-08 16:00:31 -07003216 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003217 goto out;
3218 }
3219
Glauber Costa1f458cb2012-12-18 14:22:50 -08003220 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003221
3222 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3223 /*
3224 * the readers won't lock, make sure everybody sees the updated value,
3225 * so they won't put stuff in the queue again for no reason
3226 */
3227 wmb();
3228out:
3229 mutex_unlock(&memcg_cache_mutex);
3230 return new_cachep;
3231}
3232
Glauber Costa7cf27982012-12-18 14:22:55 -08003233void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3234{
3235 struct kmem_cache *c;
3236 int i;
3237
3238 if (!s->memcg_params)
3239 return;
3240 if (!s->memcg_params->is_root_cache)
3241 return;
3242
3243 /*
3244 * If the cache is being destroyed, we trust that there is no one else
3245 * requesting objects from it. Even if there are, the sanity checks in
3246 * kmem_cache_destroy should caught this ill-case.
3247 *
3248 * Still, we don't want anyone else freeing memcg_caches under our
3249 * noses, which can happen if a new memcg comes to life. As usual,
3250 * we'll take the set_limit_mutex to protect ourselves against this.
3251 */
3252 mutex_lock(&set_limit_mutex);
3253 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3254 c = s->memcg_params->memcg_caches[i];
3255 if (!c)
3256 continue;
3257
3258 /*
3259 * We will now manually delete the caches, so to avoid races
3260 * we need to cancel all pending destruction workers and
3261 * proceed with destruction ourselves.
3262 *
3263 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3264 * and that could spawn the workers again: it is likely that
3265 * the cache still have active pages until this very moment.
3266 * This would lead us back to mem_cgroup_destroy_cache.
3267 *
3268 * But that will not execute at all if the "dead" flag is not
3269 * set, so flip it down to guarantee we are in control.
3270 */
3271 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003272 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003273 kmem_cache_destroy(c);
3274 }
3275 mutex_unlock(&set_limit_mutex);
3276}
3277
Glauber Costad7f25f82012-12-18 14:22:40 -08003278struct create_work {
3279 struct mem_cgroup *memcg;
3280 struct kmem_cache *cachep;
3281 struct work_struct work;
3282};
3283
Glauber Costa1f458cb2012-12-18 14:22:50 -08003284static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3285{
3286 struct kmem_cache *cachep;
3287 struct memcg_cache_params *params;
3288
3289 if (!memcg_kmem_is_active(memcg))
3290 return;
3291
3292 mutex_lock(&memcg->slab_caches_mutex);
3293 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3294 cachep = memcg_params_to_cache(params);
3295 cachep->memcg_params->dead = true;
Glauber Costa1f458cb2012-12-18 14:22:50 -08003296 schedule_work(&cachep->memcg_params->destroy);
3297 }
3298 mutex_unlock(&memcg->slab_caches_mutex);
3299}
3300
Glauber Costad7f25f82012-12-18 14:22:40 -08003301static void memcg_create_cache_work_func(struct work_struct *w)
3302{
3303 struct create_work *cw;
3304
3305 cw = container_of(w, struct create_work, work);
3306 memcg_create_kmem_cache(cw->memcg, cw->cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003307 kfree(cw);
3308}
3309
3310/*
3311 * Enqueue the creation of a per-memcg kmem_cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08003312 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003313static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3314 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003315{
3316 struct create_work *cw;
3317
3318 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
Li Zefanca0dde92013-04-29 15:08:57 -07003319 if (cw == NULL) {
3320 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003321 return;
3322 }
3323
3324 cw->memcg = memcg;
3325 cw->cachep = cachep;
3326
3327 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3328 schedule_work(&cw->work);
3329}
3330
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003331static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3332 struct kmem_cache *cachep)
3333{
3334 /*
3335 * We need to stop accounting when we kmalloc, because if the
3336 * corresponding kmalloc cache is not yet created, the first allocation
3337 * in __memcg_create_cache_enqueue will recurse.
3338 *
3339 * However, it is better to enclose the whole function. Depending on
3340 * the debugging options enabled, INIT_WORK(), for instance, can
3341 * trigger an allocation. This too, will make us recurse. Because at
3342 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3343 * the safest choice is to do it like this, wrapping the whole function.
3344 */
3345 memcg_stop_kmem_account();
3346 __memcg_create_cache_enqueue(memcg, cachep);
3347 memcg_resume_kmem_account();
3348}
Glauber Costad7f25f82012-12-18 14:22:40 -08003349/*
3350 * Return the kmem_cache we're supposed to use for a slab allocation.
3351 * We try to use the current memcg's version of the cache.
3352 *
3353 * If the cache does not exist yet, if we are the first user of it,
3354 * we either create it immediately, if possible, or create it asynchronously
3355 * in a workqueue.
3356 * In the latter case, we will let the current allocation go through with
3357 * the original cache.
3358 *
3359 * Can't be called in interrupt context or from kernel threads.
3360 * This function needs to be called with rcu_read_lock() held.
3361 */
3362struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3363 gfp_t gfp)
3364{
3365 struct mem_cgroup *memcg;
3366 int idx;
3367
3368 VM_BUG_ON(!cachep->memcg_params);
3369 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3370
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003371 if (!current->mm || current->memcg_kmem_skip_account)
3372 return cachep;
3373
Glauber Costad7f25f82012-12-18 14:22:40 -08003374 rcu_read_lock();
3375 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
Glauber Costad7f25f82012-12-18 14:22:40 -08003376
3377 if (!memcg_can_account_kmem(memcg))
Li Zefanca0dde92013-04-29 15:08:57 -07003378 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003379
3380 idx = memcg_cache_id(memcg);
3381
3382 /*
3383 * barrier to mare sure we're always seeing the up to date value. The
3384 * code updating memcg_caches will issue a write barrier to match this.
3385 */
3386 read_barrier_depends();
Li Zefanca0dde92013-04-29 15:08:57 -07003387 if (likely(cachep->memcg_params->memcg_caches[idx])) {
3388 cachep = cachep->memcg_params->memcg_caches[idx];
3389 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003390 }
3391
Li Zefanca0dde92013-04-29 15:08:57 -07003392 /* The corresponding put will be done in the workqueue. */
3393 if (!css_tryget(&memcg->css))
3394 goto out;
3395 rcu_read_unlock();
3396
3397 /*
3398 * If we are in a safe context (can wait, and not in interrupt
3399 * context), we could be be predictable and return right away.
3400 * This would guarantee that the allocation being performed
3401 * already belongs in the new cache.
3402 *
3403 * However, there are some clashes that can arrive from locking.
3404 * For instance, because we acquire the slab_mutex while doing
3405 * kmem_cache_dup, this means no further allocation could happen
3406 * with the slab_mutex held.
3407 *
3408 * Also, because cache creation issue get_online_cpus(), this
3409 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3410 * that ends up reversed during cpu hotplug. (cpuset allocates
3411 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3412 * better to defer everything.
3413 */
3414 memcg_create_cache_enqueue(memcg, cachep);
3415 return cachep;
3416out:
3417 rcu_read_unlock();
3418 return cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08003419}
3420EXPORT_SYMBOL(__memcg_kmem_get_cache);
3421
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003422/*
3423 * We need to verify if the allocation against current->mm->owner's memcg is
3424 * possible for the given order. But the page is not allocated yet, so we'll
3425 * need a further commit step to do the final arrangements.
3426 *
3427 * It is possible for the task to switch cgroups in this mean time, so at
3428 * commit time, we can't rely on task conversion any longer. We'll then use
3429 * the handle argument to return to the caller which cgroup we should commit
3430 * against. We could also return the memcg directly and avoid the pointer
3431 * passing, but a boolean return value gives better semantics considering
3432 * the compiled-out case as well.
3433 *
3434 * Returning true means the allocation is possible.
3435 */
3436bool
3437__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3438{
3439 struct mem_cgroup *memcg;
3440 int ret;
3441
3442 *_memcg = NULL;
Glauber Costa6d42c232013-07-08 16:00:00 -07003443
3444 /*
3445 * Disabling accounting is only relevant for some specific memcg
3446 * internal allocations. Therefore we would initially not have such
3447 * check here, since direct calls to the page allocator that are marked
3448 * with GFP_KMEMCG only happen outside memcg core. We are mostly
3449 * concerned with cache allocations, and by having this test at
3450 * memcg_kmem_get_cache, we are already able to relay the allocation to
3451 * the root cache and bypass the memcg cache altogether.
3452 *
3453 * There is one exception, though: the SLUB allocator does not create
3454 * large order caches, but rather service large kmallocs directly from
3455 * the page allocator. Therefore, the following sequence when backed by
3456 * the SLUB allocator:
3457 *
Andrew Mortonf894ffa2013-09-12 15:13:35 -07003458 * memcg_stop_kmem_account();
3459 * kmalloc(<large_number>)
3460 * memcg_resume_kmem_account();
Glauber Costa6d42c232013-07-08 16:00:00 -07003461 *
3462 * would effectively ignore the fact that we should skip accounting,
3463 * since it will drive us directly to this function without passing
3464 * through the cache selector memcg_kmem_get_cache. Such large
3465 * allocations are extremely rare but can happen, for instance, for the
3466 * cache arrays. We bring this test here.
3467 */
3468 if (!current->mm || current->memcg_kmem_skip_account)
3469 return true;
3470
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003471 memcg = try_get_mem_cgroup_from_mm(current->mm);
3472
3473 /*
3474 * very rare case described in mem_cgroup_from_task. Unfortunately there
3475 * isn't much we can do without complicating this too much, and it would
3476 * be gfp-dependent anyway. Just let it go
3477 */
3478 if (unlikely(!memcg))
3479 return true;
3480
3481 if (!memcg_can_account_kmem(memcg)) {
3482 css_put(&memcg->css);
3483 return true;
3484 }
3485
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003486 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3487 if (!ret)
3488 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003489
3490 css_put(&memcg->css);
3491 return (ret == 0);
3492}
3493
3494void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3495 int order)
3496{
3497 struct page_cgroup *pc;
3498
3499 VM_BUG_ON(mem_cgroup_is_root(memcg));
3500
3501 /* The page allocation failed. Revert */
3502 if (!page) {
3503 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003504 return;
3505 }
3506
3507 pc = lookup_page_cgroup(page);
3508 lock_page_cgroup(pc);
3509 pc->mem_cgroup = memcg;
3510 SetPageCgroupUsed(pc);
3511 unlock_page_cgroup(pc);
3512}
3513
3514void __memcg_kmem_uncharge_pages(struct page *page, int order)
3515{
3516 struct mem_cgroup *memcg = NULL;
3517 struct page_cgroup *pc;
3518
3519
3520 pc = lookup_page_cgroup(page);
3521 /*
3522 * Fast unlocked return. Theoretically might have changed, have to
3523 * check again after locking.
3524 */
3525 if (!PageCgroupUsed(pc))
3526 return;
3527
3528 lock_page_cgroup(pc);
3529 if (PageCgroupUsed(pc)) {
3530 memcg = pc->mem_cgroup;
3531 ClearPageCgroupUsed(pc);
3532 }
3533 unlock_page_cgroup(pc);
3534
3535 /*
3536 * We trust that only if there is a memcg associated with the page, it
3537 * is a valid allocation
3538 */
3539 if (!memcg)
3540 return;
3541
3542 VM_BUG_ON(mem_cgroup_is_root(memcg));
3543 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003544}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003545#else
3546static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3547{
3548}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003549#endif /* CONFIG_MEMCG_KMEM */
3550
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003551#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3552
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003553#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003554/*
3555 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003556 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3557 * charge/uncharge will be never happen and move_account() is done under
3558 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003559 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003560void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003561{
3562 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003563 struct page_cgroup *pc;
David Rientjesb070e652013-05-07 16:18:09 -07003564 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003565 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003566
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003567 if (mem_cgroup_disabled())
3568 return;
David Rientjesb070e652013-05-07 16:18:09 -07003569
3570 memcg = head_pc->mem_cgroup;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003571 for (i = 1; i < HPAGE_PMD_NR; i++) {
3572 pc = head_pc + i;
David Rientjesb070e652013-05-07 16:18:09 -07003573 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003574 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003575 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3576 }
David Rientjesb070e652013-05-07 16:18:09 -07003577 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
3578 HPAGE_PMD_NR);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003579}
Hugh Dickins12d27102012-01-12 17:19:52 -08003580#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003581
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003582static inline
3583void mem_cgroup_move_account_page_stat(struct mem_cgroup *from,
3584 struct mem_cgroup *to,
3585 unsigned int nr_pages,
3586 enum mem_cgroup_stat_index idx)
3587{
3588 /* Update stat data for mem_cgroup */
3589 preempt_disable();
3590 WARN_ON_ONCE(from->stat->count[idx] < nr_pages);
3591 __this_cpu_add(from->stat->count[idx], -nr_pages);
3592 __this_cpu_add(to->stat->count[idx], nr_pages);
3593 preempt_enable();
3594}
3595
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003596/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003597 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003598 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003599 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003600 * @pc: page_cgroup of the page.
3601 * @from: mem_cgroup which the page is moved from.
3602 * @to: mem_cgroup which the page is moved to. @from != @to.
3603 *
3604 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003605 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003606 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003607 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003608 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3609 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003610 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003611static int mem_cgroup_move_account(struct page *page,
3612 unsigned int nr_pages,
3613 struct page_cgroup *pc,
3614 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003615 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003616{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003617 unsigned long flags;
3618 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003619 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003620
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003621 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003622 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003623 /*
3624 * The page is isolated from LRU. So, collapse function
3625 * will not handle this page. But page splitting can happen.
3626 * Do this check under compound_page_lock(). The caller should
3627 * hold it.
3628 */
3629 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003630 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003631 goto out;
3632
3633 lock_page_cgroup(pc);
3634
3635 ret = -EINVAL;
3636 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3637 goto unlock;
3638
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003639 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003640
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003641 if (!anon && page_mapped(page))
3642 mem_cgroup_move_account_page_stat(from, to, nr_pages,
3643 MEM_CGROUP_STAT_FILE_MAPPED);
3644
3645 if (PageWriteback(page))
3646 mem_cgroup_move_account_page_stat(from, to, nr_pages,
3647 MEM_CGROUP_STAT_WRITEBACK);
3648
David Rientjesb070e652013-05-07 16:18:09 -07003649 mem_cgroup_charge_statistics(from, page, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003650
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003651 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003652 pc->mem_cgroup = to;
David Rientjesb070e652013-05-07 16:18:09 -07003653 mem_cgroup_charge_statistics(to, page, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003654 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003655 ret = 0;
3656unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003657 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003658 /*
3659 * check events
3660 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003661 memcg_check_events(to, page);
3662 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003663out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003664 return ret;
3665}
3666
Michal Hocko2ef37d32012-10-26 13:37:30 +02003667/**
3668 * mem_cgroup_move_parent - moves page to the parent group
3669 * @page: the page to move
3670 * @pc: page_cgroup of the page
3671 * @child: page's cgroup
3672 *
3673 * move charges to its parent or the root cgroup if the group has no
3674 * parent (aka use_hierarchy==0).
3675 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3676 * mem_cgroup_move_account fails) the failure is always temporary and
3677 * it signals a race with a page removal/uncharge or migration. In the
3678 * first case the page is on the way out and it will vanish from the LRU
3679 * on the next attempt and the call should be retried later.
3680 * Isolation from the LRU fails only if page has been isolated from
3681 * the LRU since we looked at it and that usually means either global
3682 * reclaim or migration going on. The page will either get back to the
3683 * LRU or vanish.
3684 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3685 * (!PageCgroupUsed) or moved to a different group. The page will
3686 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003687 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003688static int mem_cgroup_move_parent(struct page *page,
3689 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003690 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003691{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003692 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003693 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003694 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003695 int ret;
3696
Michal Hockod8423012012-10-26 13:37:29 +02003697 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003698
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003699 ret = -EBUSY;
3700 if (!get_page_unless_zero(page))
3701 goto out;
3702 if (isolate_lru_page(page))
3703 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003704
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003705 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003706
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003707 parent = parent_mem_cgroup(child);
3708 /*
3709 * If no parent, move charges to root cgroup.
3710 */
3711 if (!parent)
3712 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003713
Michal Hocko2ef37d32012-10-26 13:37:30 +02003714 if (nr_pages > 1) {
3715 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003716 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003717 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003718
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003719 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003720 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003721 if (!ret)
3722 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003723
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003724 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003725 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003726 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003727put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003728 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003729out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003730 return ret;
3731}
3732
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003733/*
3734 * Charge the memory controller for page usage.
3735 * Return
3736 * 0 if the charge was successful
3737 * < 0 if the cgroup is over its limit
3738 */
3739static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003740 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003741{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003742 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003743 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003744 bool oom = true;
3745 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003746
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003747 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003748 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003749 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003750 /*
3751 * Never OOM-kill a process for a huge page. The
3752 * fault handler will fall back to regular pages.
3753 */
3754 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003755 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003756
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003757 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003758 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003759 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003760 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003761 return 0;
3762}
3763
3764int mem_cgroup_newpage_charge(struct page *page,
3765 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003766{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003767 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003768 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003769 VM_BUG_ON(page_mapped(page));
3770 VM_BUG_ON(page->mapping && !PageAnon(page));
3771 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003772 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003773 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003774}
3775
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003776/*
3777 * While swap-in, try_charge -> commit or cancel, the page is locked.
3778 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003779 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003780 * "commit()" or removed by "cancel()"
3781 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003782static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3783 struct page *page,
3784 gfp_t mask,
3785 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003786{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003787 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003788 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003789 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003790
Johannes Weiner90deb782012-07-31 16:45:47 -07003791 pc = lookup_page_cgroup(page);
3792 /*
3793 * Every swap fault against a single page tries to charge the
3794 * page, bail as early as possible. shmem_unuse() encounters
3795 * already charged pages, too. The USED bit is protected by
3796 * the page lock, which serializes swap cache removal, which
3797 * in turn serializes uncharging.
3798 */
3799 if (PageCgroupUsed(pc))
3800 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003801 if (!do_swap_account)
3802 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003803 memcg = try_get_mem_cgroup_from_page(page);
3804 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003805 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003806 *memcgp = memcg;
3807 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003808 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003809 if (ret == -EINTR)
3810 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003811 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003812charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003813 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3814 if (ret == -EINTR)
3815 ret = 0;
3816 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003817}
3818
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003819int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3820 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3821{
3822 *memcgp = NULL;
3823 if (mem_cgroup_disabled())
3824 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003825 /*
3826 * A racing thread's fault, or swapoff, may have already
3827 * updated the pte, and even removed page from swap cache: in
3828 * those cases unuse_pte()'s pte_same() test will fail; but
3829 * there's also a KSM case which does need to charge the page.
3830 */
3831 if (!PageSwapCache(page)) {
3832 int ret;
3833
3834 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3835 if (ret == -EINTR)
3836 ret = 0;
3837 return ret;
3838 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003839 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3840}
3841
Johannes Weiner827a03d2012-07-31 16:45:36 -07003842void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3843{
3844 if (mem_cgroup_disabled())
3845 return;
3846 if (!memcg)
3847 return;
3848 __mem_cgroup_cancel_charge(memcg, 1);
3849}
3850
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003851static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003852__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003853 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003854{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003855 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003856 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003857 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003858 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003859
Johannes Weinerce587e62012-04-24 20:22:33 +02003860 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003861 /*
3862 * Now swap is on-memory. This means this page may be
3863 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003864 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3865 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3866 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003867 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003868 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003869 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003870 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003871 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003872}
3873
Johannes Weiner72835c82012-01-12 17:18:32 -08003874void mem_cgroup_commit_charge_swapin(struct page *page,
3875 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003876{
Johannes Weiner72835c82012-01-12 17:18:32 -08003877 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003878 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003879}
3880
Johannes Weiner827a03d2012-07-31 16:45:36 -07003881int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3882 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003883{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003884 struct mem_cgroup *memcg = NULL;
3885 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3886 int ret;
3887
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003888 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003889 return 0;
3890 if (PageCompound(page))
3891 return 0;
3892
Johannes Weiner827a03d2012-07-31 16:45:36 -07003893 if (!PageSwapCache(page))
3894 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3895 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003896 ret = __mem_cgroup_try_charge_swapin(mm, page,
3897 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003898 if (!ret)
3899 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3900 }
3901 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003902}
3903
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003904static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003905 unsigned int nr_pages,
3906 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003907{
3908 struct memcg_batch_info *batch = NULL;
3909 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003910
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003911 /* If swapout, usage of swap doesn't decrease */
3912 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3913 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003914
3915 batch = &current->memcg_batch;
3916 /*
3917 * In usual, we do css_get() when we remember memcg pointer.
3918 * But in this case, we keep res->usage until end of a series of
3919 * uncharges. Then, it's ok to ignore memcg's refcnt.
3920 */
3921 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003922 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003923 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003924 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003925 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003926 * the same cgroup and we have chance to coalesce uncharges.
3927 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3928 * because we want to do uncharge as soon as possible.
3929 */
3930
3931 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3932 goto direct_uncharge;
3933
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003934 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003935 goto direct_uncharge;
3936
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003937 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003938 * In typical case, batch->memcg == mem. This means we can
3939 * merge a series of uncharges to an uncharge of res_counter.
3940 * If not, we uncharge res_counter ony by one.
3941 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003942 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003943 goto direct_uncharge;
3944 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003945 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003946 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003947 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003948 return;
3949direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003950 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003951 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003952 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3953 if (unlikely(batch->memcg != memcg))
3954 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003955}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003956
Balbir Singh8697d332008-02-07 00:13:59 -08003957/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003958 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003959 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003960static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07003961__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3962 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003963{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003964 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003965 unsigned int nr_pages = 1;
3966 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003967 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003968
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003969 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003970 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003971
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003972 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003973 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003974 VM_BUG_ON(!PageTransHuge(page));
3975 }
Balbir Singh8697d332008-02-07 00:13:59 -08003976 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003977 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003978 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003979 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003980 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003981 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003982
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003983 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003984
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003985 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003986
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003987 if (!PageCgroupUsed(pc))
3988 goto unlock_out;
3989
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003990 anon = PageAnon(page);
3991
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003992 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003993 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003994 /*
3995 * Generally PageAnon tells if it's the anon statistics to be
3996 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
3997 * used before page reached the stage of being marked PageAnon.
3998 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003999 anon = true;
4000 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004001 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004002 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004003 if (page_mapped(page))
4004 goto unlock_out;
4005 /*
4006 * Pages under migration may not be uncharged. But
4007 * end_migration() /must/ be the one uncharging the
4008 * unused post-migration page and so it has to call
4009 * here with the migration bit still set. See the
4010 * res_counter handling below.
4011 */
4012 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004013 goto unlock_out;
4014 break;
4015 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4016 if (!PageAnon(page)) { /* Shared memory */
4017 if (page->mapping && !page_is_file_cache(page))
4018 goto unlock_out;
4019 } else if (page_mapped(page)) /* Anon */
4020 goto unlock_out;
4021 break;
4022 default:
4023 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004024 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004025
David Rientjesb070e652013-05-07 16:18:09 -07004026 mem_cgroup_charge_statistics(memcg, page, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004027
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004028 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004029 /*
4030 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4031 * freed from LRU. This is safe because uncharged page is expected not
4032 * to be reused (freed soon). Exception is SwapCache, it's handled by
4033 * special functions.
4034 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004035
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004036 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004037 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004038 * even after unlock, we have memcg->res.usage here and this memcg
Li Zefan40503772013-07-08 16:00:34 -07004039 * will never be freed, so it's safe to call css_get().
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004040 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004041 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004042 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004043 mem_cgroup_swap_statistics(memcg, true);
Li Zefan40503772013-07-08 16:00:34 -07004044 css_get(&memcg->css);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004045 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004046 /*
4047 * Migration does not charge the res_counter for the
4048 * replacement page, so leave it alone when phasing out the
4049 * page that is unused after the migration.
4050 */
4051 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004052 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004053
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004054 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004055
4056unlock_out:
4057 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004058 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004059}
4060
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004061void mem_cgroup_uncharge_page(struct page *page)
4062{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004063 /* early check. */
4064 if (page_mapped(page))
4065 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004066 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner28ccddf2013-05-24 15:55:15 -07004067 /*
4068 * If the page is in swap cache, uncharge should be deferred
4069 * to the swap path, which also properly accounts swap usage
4070 * and handles memcg lifetime.
4071 *
4072 * Note that this check is not stable and reclaim may add the
4073 * page to swap cache at any time after this. However, if the
4074 * page is not in swap cache by the time page->mapcount hits
4075 * 0, there won't be any page table references to the swap
4076 * slot, and reclaim will free it and not actually write the
4077 * page to disk.
4078 */
Johannes Weiner0c59b892012-07-31 16:45:31 -07004079 if (PageSwapCache(page))
4080 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004081 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004082}
4083
4084void mem_cgroup_uncharge_cache_page(struct page *page)
4085{
4086 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004087 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004088 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004089}
4090
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004091/*
4092 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4093 * In that cases, pages are freed continuously and we can expect pages
4094 * are in the same memcg. All these calls itself limits the number of
4095 * pages freed at once, then uncharge_start/end() is called properly.
4096 * This may be called prural(2) times in a context,
4097 */
4098
4099void mem_cgroup_uncharge_start(void)
4100{
4101 current->memcg_batch.do_batch++;
4102 /* We can do nest. */
4103 if (current->memcg_batch.do_batch == 1) {
4104 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004105 current->memcg_batch.nr_pages = 0;
4106 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004107 }
4108}
4109
4110void mem_cgroup_uncharge_end(void)
4111{
4112 struct memcg_batch_info *batch = &current->memcg_batch;
4113
4114 if (!batch->do_batch)
4115 return;
4116
4117 batch->do_batch--;
4118 if (batch->do_batch) /* If stacked, do nothing. */
4119 return;
4120
4121 if (!batch->memcg)
4122 return;
4123 /*
4124 * This "batch->memcg" is valid without any css_get/put etc...
4125 * bacause we hide charges behind us.
4126 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004127 if (batch->nr_pages)
4128 res_counter_uncharge(&batch->memcg->res,
4129 batch->nr_pages * PAGE_SIZE);
4130 if (batch->memsw_nr_pages)
4131 res_counter_uncharge(&batch->memcg->memsw,
4132 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004133 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004134 /* forget this pointer (for sanity check) */
4135 batch->memcg = NULL;
4136}
4137
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004138#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004139/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004140 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004141 * memcg information is recorded to swap_cgroup of "ent"
4142 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004143void
4144mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004145{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004146 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004147 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004148
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004149 if (!swapout) /* this was a swap cache but the swap is unused ! */
4150 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4151
Johannes Weiner0030f532012-07-31 16:45:25 -07004152 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004153
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004154 /*
4155 * record memcg information, if swapout && memcg != NULL,
Li Zefan40503772013-07-08 16:00:34 -07004156 * css_get() was called in uncharge().
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004157 */
4158 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004159 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004160}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004161#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004162
Andrew Mortonc255a452012-07-31 16:43:02 -07004163#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004164/*
4165 * called from swap_entry_free(). remove record in swap_cgroup and
4166 * uncharge "memsw" account.
4167 */
4168void mem_cgroup_uncharge_swap(swp_entry_t ent)
4169{
4170 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004171 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004172
4173 if (!do_swap_account)
4174 return;
4175
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004176 id = swap_cgroup_record(ent, 0);
4177 rcu_read_lock();
4178 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004179 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004180 /*
4181 * We uncharge this because swap is freed.
4182 * This memcg can be obsolete one. We avoid calling css_tryget
4183 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004184 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004185 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004186 mem_cgroup_swap_statistics(memcg, false);
Li Zefan40503772013-07-08 16:00:34 -07004187 css_put(&memcg->css);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004188 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004189 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004190}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004191
4192/**
4193 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4194 * @entry: swap entry to be moved
4195 * @from: mem_cgroup which the entry is moved from
4196 * @to: mem_cgroup which the entry is moved to
4197 *
4198 * It succeeds only when the swap_cgroup's record for this entry is the same
4199 * as the mem_cgroup's id of @from.
4200 *
4201 * Returns 0 on success, -EINVAL on failure.
4202 *
4203 * The caller must have charged to @to, IOW, called res_counter_charge() about
4204 * both res and memsw, and called css_get().
4205 */
4206static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004207 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004208{
4209 unsigned short old_id, new_id;
4210
4211 old_id = css_id(&from->css);
4212 new_id = css_id(&to->css);
4213
4214 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004215 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004216 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004217 /*
4218 * This function is only called from task migration context now.
4219 * It postpones res_counter and refcount handling till the end
4220 * of task migration(mem_cgroup_clear_mc()) for performance
Li Zefan40503772013-07-08 16:00:34 -07004221 * improvement. But we cannot postpone css_get(to) because if
4222 * the process that has been moved to @to does swap-in, the
4223 * refcount of @to might be decreased to 0.
4224 *
4225 * We are in attach() phase, so the cgroup is guaranteed to be
4226 * alive, so we can just call css_get().
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004227 */
Li Zefan40503772013-07-08 16:00:34 -07004228 css_get(&to->css);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004229 return 0;
4230 }
4231 return -EINVAL;
4232}
4233#else
4234static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004235 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004236{
4237 return -EINVAL;
4238}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004239#endif
4240
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004241/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004242 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4243 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004244 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004245void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4246 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004247{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004248 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004249 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004250 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004251 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004252
Johannes Weiner72835c82012-01-12 17:18:32 -08004253 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004254
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004255 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004256 return;
Balbir Singh40779602008-04-04 14:29:59 -07004257
Mel Gormanb32967f2012-11-19 12:35:47 +00004258 if (PageTransHuge(page))
4259 nr_pages <<= compound_order(page);
4260
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004261 pc = lookup_page_cgroup(page);
4262 lock_page_cgroup(pc);
4263 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004264 memcg = pc->mem_cgroup;
4265 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004266 /*
4267 * At migrating an anonymous page, its mapcount goes down
4268 * to 0 and uncharge() will be called. But, even if it's fully
4269 * unmapped, migration may fail and this page has to be
4270 * charged again. We set MIGRATION flag here and delay uncharge
4271 * until end_migration() is called
4272 *
4273 * Corner Case Thinking
4274 * A)
4275 * When the old page was mapped as Anon and it's unmap-and-freed
4276 * while migration was ongoing.
4277 * If unmap finds the old page, uncharge() of it will be delayed
4278 * until end_migration(). If unmap finds a new page, it's
4279 * uncharged when it make mapcount to be 1->0. If unmap code
4280 * finds swap_migration_entry, the new page will not be mapped
4281 * and end_migration() will find it(mapcount==0).
4282 *
4283 * B)
4284 * When the old page was mapped but migraion fails, the kernel
4285 * remaps it. A charge for it is kept by MIGRATION flag even
4286 * if mapcount goes down to 0. We can do remap successfully
4287 * without charging it again.
4288 *
4289 * C)
4290 * The "old" page is under lock_page() until the end of
4291 * migration, so, the old page itself will not be swapped-out.
4292 * If the new page is swapped out before end_migraton, our
4293 * hook to usual swap-out path will catch the event.
4294 */
4295 if (PageAnon(page))
4296 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004297 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004298 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004299 /*
4300 * If the page is not charged at this point,
4301 * we return here.
4302 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004303 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004304 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004305
Johannes Weiner72835c82012-01-12 17:18:32 -08004306 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004307 /*
4308 * We charge new page before it's used/mapped. So, even if unlock_page()
4309 * is called before end_migration, we can catch all events on this new
4310 * page. In the case new page is migrated but not remapped, new page's
4311 * mapcount will be finally 0 and we call uncharge in end_migration().
4312 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004313 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004314 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004315 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004316 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004317 /*
4318 * The page is committed to the memcg, but it's not actually
4319 * charged to the res_counter since we plan on replacing the
4320 * old one and only one page is going to be left afterwards.
4321 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004322 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004323}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004324
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004325/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004326void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004327 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004328{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004329 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004330 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004331 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004332
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004333 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004334 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004335
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004336 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004337 used = oldpage;
4338 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004339 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004340 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004341 unused = oldpage;
4342 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004343 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004344 __mem_cgroup_uncharge_common(unused,
4345 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4346 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4347 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004348 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004349 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004350 * We disallowed uncharge of pages under migration because mapcount
4351 * of the page goes down to zero, temporarly.
4352 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004353 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004354 pc = lookup_page_cgroup(oldpage);
4355 lock_page_cgroup(pc);
4356 ClearPageCgroupMigration(pc);
4357 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004358
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004359 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004360 * If a page is a file cache, radix-tree replacement is very atomic
4361 * and we can skip this check. When it was an Anon page, its mapcount
4362 * goes down to 0. But because we added MIGRATION flage, it's not
4363 * uncharged yet. There are several case but page->mapcount check
4364 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4365 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004366 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004367 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004368 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004369}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004370
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004371/*
4372 * At replace page cache, newpage is not under any memcg but it's on
4373 * LRU. So, this function doesn't touch res_counter but handles LRU
4374 * in correct way. Both pages are locked so we cannot race with uncharge.
4375 */
4376void mem_cgroup_replace_page_cache(struct page *oldpage,
4377 struct page *newpage)
4378{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004379 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004380 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004381 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004382
4383 if (mem_cgroup_disabled())
4384 return;
4385
4386 pc = lookup_page_cgroup(oldpage);
4387 /* fix accounting on old pages */
4388 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004389 if (PageCgroupUsed(pc)) {
4390 memcg = pc->mem_cgroup;
David Rientjesb070e652013-05-07 16:18:09 -07004391 mem_cgroup_charge_statistics(memcg, oldpage, false, -1);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004392 ClearPageCgroupUsed(pc);
4393 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004394 unlock_page_cgroup(pc);
4395
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004396 /*
4397 * When called from shmem_replace_page(), in some cases the
4398 * oldpage has already been charged, and in some cases not.
4399 */
4400 if (!memcg)
4401 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004402 /*
4403 * Even if newpage->mapping was NULL before starting replacement,
4404 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4405 * LRU while we overwrite pc->mem_cgroup.
4406 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004407 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004408}
4409
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004410#ifdef CONFIG_DEBUG_VM
4411static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4412{
4413 struct page_cgroup *pc;
4414
4415 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004416 /*
4417 * Can be NULL while feeding pages into the page allocator for
4418 * the first time, i.e. during boot or memory hotplug;
4419 * or when mem_cgroup_disabled().
4420 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004421 if (likely(pc) && PageCgroupUsed(pc))
4422 return pc;
4423 return NULL;
4424}
4425
4426bool mem_cgroup_bad_page_check(struct page *page)
4427{
4428 if (mem_cgroup_disabled())
4429 return false;
4430
4431 return lookup_page_cgroup_used(page) != NULL;
4432}
4433
4434void mem_cgroup_print_bad_page(struct page *page)
4435{
4436 struct page_cgroup *pc;
4437
4438 pc = lookup_page_cgroup_used(page);
4439 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08004440 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4441 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004442 }
4443}
4444#endif
4445
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004446static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004447 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004448{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004449 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004450 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004451 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004452 int children = mem_cgroup_count_children(memcg);
4453 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004454 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004455
4456 /*
4457 * For keeping hierarchical_reclaim simple, how long we should retry
4458 * is depends on callers. We set our retry-count to be function
4459 * of # of children which we should visit in this loop.
4460 */
4461 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4462
4463 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004464
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004465 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004466 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004467 if (signal_pending(current)) {
4468 ret = -EINTR;
4469 break;
4470 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004471 /*
4472 * Rather than hide all in some function, I do this in
4473 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004474 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004475 */
4476 mutex_lock(&set_limit_mutex);
4477 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4478 if (memswlimit < val) {
4479 ret = -EINVAL;
4480 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004481 break;
4482 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004483
4484 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4485 if (memlimit < val)
4486 enlarge = 1;
4487
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004488 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004489 if (!ret) {
4490 if (memswlimit == val)
4491 memcg->memsw_is_minimum = true;
4492 else
4493 memcg->memsw_is_minimum = false;
4494 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004495 mutex_unlock(&set_limit_mutex);
4496
4497 if (!ret)
4498 break;
4499
Johannes Weiner56600482012-01-12 17:17:59 -08004500 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4501 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004502 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4503 /* Usage is reduced ? */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07004504 if (curusage >= oldusage)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004505 retry_count--;
4506 else
4507 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004508 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004509 if (!ret && enlarge)
4510 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004511
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004512 return ret;
4513}
4514
Li Zefan338c8432009-06-17 16:27:15 -07004515static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4516 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004517{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004518 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004519 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004520 int children = mem_cgroup_count_children(memcg);
4521 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004522 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004523
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004524 /* see mem_cgroup_resize_res_limit */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07004525 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004526 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004527 while (retry_count) {
4528 if (signal_pending(current)) {
4529 ret = -EINTR;
4530 break;
4531 }
4532 /*
4533 * Rather than hide all in some function, I do this in
4534 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004535 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004536 */
4537 mutex_lock(&set_limit_mutex);
4538 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4539 if (memlimit > val) {
4540 ret = -EINVAL;
4541 mutex_unlock(&set_limit_mutex);
4542 break;
4543 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004544 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4545 if (memswlimit < val)
4546 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004547 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004548 if (!ret) {
4549 if (memlimit == val)
4550 memcg->memsw_is_minimum = true;
4551 else
4552 memcg->memsw_is_minimum = false;
4553 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004554 mutex_unlock(&set_limit_mutex);
4555
4556 if (!ret)
4557 break;
4558
Johannes Weiner56600482012-01-12 17:17:59 -08004559 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4560 MEM_CGROUP_RECLAIM_NOSWAP |
4561 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004562 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004563 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004564 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004565 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004566 else
4567 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004568 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004569 if (!ret && enlarge)
4570 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004571 return ret;
4572}
4573
Michal Hocko2ef37d32012-10-26 13:37:30 +02004574/**
4575 * mem_cgroup_force_empty_list - clears LRU of a group
4576 * @memcg: group to clear
4577 * @node: NUMA node
4578 * @zid: zone id
4579 * @lru: lru to to clear
4580 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004581 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004582 * reclaim the pages page themselves - pages are moved to the parent (or root)
4583 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004584 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004585static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004586 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004587{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004588 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004589 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004590 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004591 struct page *busy;
4592 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004593
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004594 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004595 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4596 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004597
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004598 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004599 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004600 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004601 struct page *page;
4602
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004603 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004604 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004605 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004606 break;
4607 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004608 page = list_entry(list->prev, struct page, lru);
4609 if (busy == page) {
4610 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004611 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004612 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004613 continue;
4614 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004615 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004616
Johannes Weiner925b7672012-01-12 17:18:15 -08004617 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004618
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004619 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004620 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004621 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004622 cond_resched();
4623 } else
4624 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004625 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004626}
4627
4628/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004629 * make mem_cgroup's charge to be 0 if there is no task by moving
4630 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004631 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004632 *
4633 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004634 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004635static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004636{
Michal Hockoc26251f2012-10-26 13:37:28 +02004637 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004638 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004639
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004640 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004641 /* This is for making all *used* pages to be on LRU. */
4642 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004643 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004644 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004645 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004646 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab932012-03-21 16:34:19 -07004647 enum lru_list lru;
4648 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004649 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab932012-03-21 16:34:19 -07004650 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004651 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004652 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004653 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004654 mem_cgroup_end_move(memcg);
4655 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004656 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004657
Michal Hocko2ef37d32012-10-26 13:37:30 +02004658 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004659 * Kernel memory may not necessarily be trackable to a specific
4660 * process. So they are not migrated, and therefore we can't
4661 * expect their value to drop to 0 here.
4662 * Having res filled up with kmem only is enough.
4663 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004664 * This is a safety check because mem_cgroup_force_empty_list
4665 * could have raced with mem_cgroup_replace_page_cache callers
4666 * so the lru seemed empty but the page could have been added
4667 * right after the check. RES_USAGE should be safe as we always
4668 * charge before adding to the LRU.
4669 */
Glauber Costabea207c2012-12-18 14:22:11 -08004670 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4671 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4672 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004673}
4674
4675/*
Glauber Costab5f99b52013-02-22 16:34:53 -08004676 * This mainly exists for tests during the setting of set of use_hierarchy.
4677 * Since this is the very setting we are changing, the current hierarchy value
4678 * is meaningless
4679 */
4680static inline bool __memcg_has_children(struct mem_cgroup *memcg)
4681{
Tejun Heo492eb212013-08-08 20:11:25 -04004682 struct cgroup_subsys_state *pos;
Glauber Costab5f99b52013-02-22 16:34:53 -08004683
4684 /* bounce at first found */
Tejun Heo492eb212013-08-08 20:11:25 -04004685 css_for_each_child(pos, &memcg->css)
Glauber Costab5f99b52013-02-22 16:34:53 -08004686 return true;
4687 return false;
4688}
4689
4690/*
Glauber Costa09998212013-02-22 16:34:55 -08004691 * Must be called with memcg_create_mutex held, unless the cgroup is guaranteed
4692 * to be already dead (as in mem_cgroup_force_empty, for instance). This is
Glauber Costab5f99b52013-02-22 16:34:53 -08004693 * from mem_cgroup_count_children(), in the sense that we don't really care how
4694 * many children we have; we only need to know if we have any. It also counts
4695 * any memcg without hierarchy as infertile.
4696 */
4697static inline bool memcg_has_children(struct mem_cgroup *memcg)
4698{
4699 return memcg->use_hierarchy && __memcg_has_children(memcg);
4700}
4701
4702/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004703 * Reclaims as many pages from the given memcg as possible and moves
4704 * the rest to the parent.
4705 *
4706 * Caller is responsible for holding css reference for memcg.
4707 */
4708static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4709{
4710 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4711 struct cgroup *cgrp = memcg->css.cgroup;
4712
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004713 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004714 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4715 return -EBUSY;
4716
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004717 /* we call try-to-free pages for make this cgroup empty */
4718 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004719 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004720 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004721 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004722
Michal Hockoc26251f2012-10-26 13:37:28 +02004723 if (signal_pending(current))
4724 return -EINTR;
4725
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004726 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004727 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004728 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004729 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004730 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004731 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004732 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004733
4734 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004735 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004736 mem_cgroup_reparent_charges(memcg);
4737
4738 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004739}
4740
Tejun Heo182446d2013-08-08 20:11:24 -04004741static int mem_cgroup_force_empty_write(struct cgroup_subsys_state *css,
4742 unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004743{
Tejun Heo182446d2013-08-08 20:11:24 -04004744 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Michal Hockoc26251f2012-10-26 13:37:28 +02004745
Michal Hockod8423012012-10-26 13:37:29 +02004746 if (mem_cgroup_is_root(memcg))
4747 return -EINVAL;
Li Zefanc33bd832013-09-12 15:13:19 -07004748 return mem_cgroup_force_empty(memcg);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004749}
4750
Tejun Heo182446d2013-08-08 20:11:24 -04004751static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
4752 struct cftype *cft)
Balbir Singh18f59ea2009-01-07 18:08:07 -08004753{
Tejun Heo182446d2013-08-08 20:11:24 -04004754 return mem_cgroup_from_css(css)->use_hierarchy;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004755}
4756
Tejun Heo182446d2013-08-08 20:11:24 -04004757static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
4758 struct cftype *cft, u64 val)
Balbir Singh18f59ea2009-01-07 18:08:07 -08004759{
4760 int retval = 0;
Tejun Heo182446d2013-08-08 20:11:24 -04004761 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04004762 struct mem_cgroup *parent_memcg = mem_cgroup_from_css(css_parent(&memcg->css));
Balbir Singh18f59ea2009-01-07 18:08:07 -08004763
Glauber Costa09998212013-02-22 16:34:55 -08004764 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07004765
4766 if (memcg->use_hierarchy == val)
4767 goto out;
4768
Balbir Singh18f59ea2009-01-07 18:08:07 -08004769 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004770 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004771 * in the child subtrees. If it is unset, then the change can
4772 * occur, provided the current cgroup has no children.
4773 *
4774 * For the root cgroup, parent_mem is NULL, we allow value to be
4775 * set if there are no children.
4776 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004777 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004778 (val == 1 || val == 0)) {
Glauber Costab5f99b52013-02-22 16:34:53 -08004779 if (!__memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004780 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004781 else
4782 retval = -EBUSY;
4783 } else
4784 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004785
4786out:
Glauber Costa09998212013-02-22 16:34:55 -08004787 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004788
4789 return retval;
4790}
4791
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004792
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004793static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004794 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004795{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004796 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004797 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004798
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004799 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004800 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004801 val += mem_cgroup_read_stat(iter, idx);
4802
4803 if (val < 0) /* race ? */
4804 val = 0;
4805 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004806}
4807
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004808static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004809{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004810 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004811
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004812 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004813 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004814 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004815 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004816 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004817 }
4818
David Rientjesb070e652013-05-07 16:18:09 -07004819 /*
4820 * Transparent hugepages are still accounted for in MEM_CGROUP_STAT_RSS
4821 * as well as in MEM_CGROUP_STAT_RSS_HUGE.
4822 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004823 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4824 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004825
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004826 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004827 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004828
4829 return val << PAGE_SHIFT;
4830}
4831
Tejun Heo182446d2013-08-08 20:11:24 -04004832static ssize_t mem_cgroup_read(struct cgroup_subsys_state *css,
4833 struct cftype *cft, struct file *file,
4834 char __user *buf, size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004835{
Tejun Heo182446d2013-08-08 20:11:24 -04004836 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heoaf36f902012-04-01 12:09:55 -07004837 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004838 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004839 int name, len;
4840 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004841
4842 type = MEMFILE_TYPE(cft->private);
4843 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004844
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004845 switch (type) {
4846 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004847 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004848 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004849 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004850 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004851 break;
4852 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004853 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004854 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004855 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004856 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004857 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004858 case _KMEM:
4859 val = res_counter_read_u64(&memcg->kmem, name);
4860 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004861 default:
4862 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004863 }
Tejun Heoaf36f902012-04-01 12:09:55 -07004864
4865 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4866 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004867}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004868
Tejun Heo182446d2013-08-08 20:11:24 -04004869static int memcg_update_kmem_limit(struct cgroup_subsys_state *css, u64 val)
Glauber Costa510fc4e2012-12-18 14:21:47 -08004870{
4871 int ret = -EINVAL;
4872#ifdef CONFIG_MEMCG_KMEM
Tejun Heo182446d2013-08-08 20:11:24 -04004873 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004874 /*
4875 * For simplicity, we won't allow this to be disabled. It also can't
4876 * be changed if the cgroup has children already, or if tasks had
4877 * already joined.
4878 *
4879 * If tasks join before we set the limit, a person looking at
4880 * kmem.usage_in_bytes will have no way to determine when it took
4881 * place, which makes the value quite meaningless.
4882 *
4883 * After it first became limited, changes in the value of the limit are
4884 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08004885 */
Glauber Costa09998212013-02-22 16:34:55 -08004886 mutex_lock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004887 mutex_lock(&set_limit_mutex);
Sha Zhengju6de5a8b2013-09-12 15:13:47 -07004888 if (!memcg->kmem_account_flags && val != RES_COUNTER_MAX) {
Tejun Heo182446d2013-08-08 20:11:24 -04004889 if (cgroup_task_count(css->cgroup) || memcg_has_children(memcg)) {
Glauber Costa510fc4e2012-12-18 14:21:47 -08004890 ret = -EBUSY;
4891 goto out;
4892 }
4893 ret = res_counter_set_limit(&memcg->kmem, val);
4894 VM_BUG_ON(ret);
4895
Glauber Costa55007d82012-12-18 14:22:38 -08004896 ret = memcg_update_cache_sizes(memcg);
4897 if (ret) {
Sha Zhengju6de5a8b2013-09-12 15:13:47 -07004898 res_counter_set_limit(&memcg->kmem, RES_COUNTER_MAX);
Glauber Costa55007d82012-12-18 14:22:38 -08004899 goto out;
4900 }
Glauber Costa692e89a2013-02-22 16:34:56 -08004901 static_key_slow_inc(&memcg_kmem_enabled_key);
4902 /*
4903 * setting the active bit after the inc will guarantee no one
4904 * starts accounting before all call sites are patched
4905 */
4906 memcg_kmem_set_active(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004907 } else
4908 ret = res_counter_set_limit(&memcg->kmem, val);
4909out:
4910 mutex_unlock(&set_limit_mutex);
Glauber Costa09998212013-02-22 16:34:55 -08004911 mutex_unlock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004912#endif
4913 return ret;
4914}
4915
Hugh Dickins6d0439902013-02-22 16:35:50 -08004916#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -08004917static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08004918{
Glauber Costa55007d82012-12-18 14:22:38 -08004919 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004920 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4921 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08004922 goto out;
4923
Glauber Costa510fc4e2012-12-18 14:21:47 -08004924 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costaa8964b92012-12-18 14:22:09 -08004925 /*
4926 * When that happen, we need to disable the static branch only on those
4927 * memcgs that enabled it. To achieve this, we would be forced to
4928 * complicate the code by keeping track of which memcgs were the ones
4929 * that actually enabled limits, and which ones got it from its
4930 * parents.
4931 *
4932 * It is a lot simpler just to do static_key_slow_inc() on every child
4933 * that is accounted.
4934 */
Glauber Costa55007d82012-12-18 14:22:38 -08004935 if (!memcg_kmem_is_active(memcg))
4936 goto out;
4937
4938 /*
Li Zefan10d5ebf2013-07-08 16:00:33 -07004939 * __mem_cgroup_free() will issue static_key_slow_dec() because this
4940 * memcg is active already. If the later initialization fails then the
4941 * cgroup core triggers the cleanup so we do not have to do it here.
Glauber Costa55007d82012-12-18 14:22:38 -08004942 */
Glauber Costa55007d82012-12-18 14:22:38 -08004943 static_key_slow_inc(&memcg_kmem_enabled_key);
4944
4945 mutex_lock(&set_limit_mutex);
Glauber Costa425c5982013-07-08 16:00:01 -07004946 memcg_stop_kmem_account();
Glauber Costa55007d82012-12-18 14:22:38 -08004947 ret = memcg_update_cache_sizes(memcg);
Glauber Costa425c5982013-07-08 16:00:01 -07004948 memcg_resume_kmem_account();
Glauber Costa55007d82012-12-18 14:22:38 -08004949 mutex_unlock(&set_limit_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08004950out:
4951 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004952}
Hugh Dickins6d0439902013-02-22 16:35:50 -08004953#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08004954
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004955/*
4956 * The user of this function is...
4957 * RES_LIMIT.
4958 */
Tejun Heo182446d2013-08-08 20:11:24 -04004959static int mem_cgroup_write(struct cgroup_subsys_state *css, struct cftype *cft,
Paul Menage856c13a2008-07-25 01:47:04 -07004960 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004961{
Tejun Heo182446d2013-08-08 20:11:24 -04004962 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa86ae53e2012-12-18 14:21:45 -08004963 enum res_type type;
4964 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004965 unsigned long long val;
4966 int ret;
4967
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004968 type = MEMFILE_TYPE(cft->private);
4969 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004970
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004971 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004972 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07004973 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
4974 ret = -EINVAL;
4975 break;
4976 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004977 /* This function does all necessary parse...reuse it */
4978 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004979 if (ret)
4980 break;
4981 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004982 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004983 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004984 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004985 else if (type == _KMEM)
Tejun Heo182446d2013-08-08 20:11:24 -04004986 ret = memcg_update_kmem_limit(css, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004987 else
4988 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004989 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07004990 case RES_SOFT_LIMIT:
4991 ret = res_counter_memparse_write_strategy(buffer, &val);
4992 if (ret)
4993 break;
4994 /*
4995 * For memsw, soft limits are hard to implement in terms
4996 * of semantics, for now, we support soft limits for
4997 * control without swap
4998 */
4999 if (type == _MEM)
5000 ret = res_counter_set_soft_limit(&memcg->res, val);
5001 else
5002 ret = -EINVAL;
5003 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005004 default:
5005 ret = -EINVAL; /* should be BUG() ? */
5006 break;
5007 }
5008 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005009}
5010
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005011static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5012 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5013{
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005014 unsigned long long min_limit, min_memsw_limit, tmp;
5015
5016 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5017 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005018 if (!memcg->use_hierarchy)
5019 goto out;
5020
Tejun Heo63876982013-08-08 20:11:23 -04005021 while (css_parent(&memcg->css)) {
5022 memcg = mem_cgroup_from_css(css_parent(&memcg->css));
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005023 if (!memcg->use_hierarchy)
5024 break;
5025 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5026 min_limit = min(min_limit, tmp);
5027 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5028 min_memsw_limit = min(min_memsw_limit, tmp);
5029 }
5030out:
5031 *mem_limit = min_limit;
5032 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005033}
5034
Tejun Heo182446d2013-08-08 20:11:24 -04005035static int mem_cgroup_reset(struct cgroup_subsys_state *css, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005036{
Tejun Heo182446d2013-08-08 20:11:24 -04005037 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005038 int name;
5039 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005040
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005041 type = MEMFILE_TYPE(event);
5042 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005043
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005044 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005045 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005046 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005047 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005048 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005049 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005050 else if (type == _KMEM)
5051 res_counter_reset_max(&memcg->kmem);
5052 else
5053 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005054 break;
5055 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005056 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005057 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005058 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005059 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005060 else if (type == _KMEM)
5061 res_counter_reset_failcnt(&memcg->kmem);
5062 else
5063 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005064 break;
5065 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005066
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005067 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005068}
5069
Tejun Heo182446d2013-08-08 20:11:24 -04005070static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005071 struct cftype *cft)
5072{
Tejun Heo182446d2013-08-08 20:11:24 -04005073 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005074}
5075
Daisuke Nishimura02491442010-03-10 15:22:17 -08005076#ifdef CONFIG_MMU
Tejun Heo182446d2013-08-08 20:11:24 -04005077static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005078 struct cftype *cft, u64 val)
5079{
Tejun Heo182446d2013-08-08 20:11:24 -04005080 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005081
5082 if (val >= (1 << NR_MOVE_TYPE))
5083 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005084
Glauber Costaee5e8472013-02-22 16:34:50 -08005085 /*
5086 * No kind of locking is needed in here, because ->can_attach() will
5087 * check this value once in the beginning of the process, and then carry
5088 * on with stale data. This means that changes to this value will only
5089 * affect task migrations starting after the change.
5090 */
5091 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005092 return 0;
5093}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005094#else
Tejun Heo182446d2013-08-08 20:11:24 -04005095static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005096 struct cftype *cft, u64 val)
5097{
5098 return -ENOSYS;
5099}
5100#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005101
Ying Han406eb0c2011-05-26 16:25:37 -07005102#ifdef CONFIG_NUMA
Tejun Heo182446d2013-08-08 20:11:24 -04005103static int memcg_numa_stat_show(struct cgroup_subsys_state *css,
5104 struct cftype *cft, struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005105{
5106 int nid;
5107 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5108 unsigned long node_nr;
Tejun Heo182446d2013-08-08 20:11:24 -04005109 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Ying Han406eb0c2011-05-26 16:25:37 -07005110
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005111 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005112 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005113 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005114 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005115 seq_printf(m, " N%d=%lu", nid, node_nr);
5116 }
5117 seq_putc(m, '\n');
5118
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005119 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005120 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005121 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005122 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005123 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005124 seq_printf(m, " N%d=%lu", nid, node_nr);
5125 }
5126 seq_putc(m, '\n');
5127
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005128 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005129 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005130 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005131 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005132 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005133 seq_printf(m, " N%d=%lu", nid, node_nr);
5134 }
5135 seq_putc(m, '\n');
5136
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005137 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005138 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005139 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005140 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005141 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005142 seq_printf(m, " N%d=%lu", nid, node_nr);
5143 }
5144 seq_putc(m, '\n');
5145 return 0;
5146}
5147#endif /* CONFIG_NUMA */
5148
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005149static inline void mem_cgroup_lru_names_not_uptodate(void)
5150{
5151 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5152}
5153
Tejun Heo182446d2013-08-08 20:11:24 -04005154static int memcg_stat_show(struct cgroup_subsys_state *css, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005155 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005156{
Tejun Heo182446d2013-08-08 20:11:24 -04005157 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005158 struct mem_cgroup *mi;
5159 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005160
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005161 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005162 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005163 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005164 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5165 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005166 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005167
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005168 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5169 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5170 mem_cgroup_read_events(memcg, i));
5171
5172 for (i = 0; i < NR_LRU_LISTS; i++)
5173 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5174 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5175
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005176 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005177 {
5178 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005179 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005180 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005181 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005182 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5183 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005184 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005185
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005186 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5187 long long val = 0;
5188
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005189 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005190 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005191 for_each_mem_cgroup_tree(mi, memcg)
5192 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5193 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5194 }
5195
5196 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5197 unsigned long long val = 0;
5198
5199 for_each_mem_cgroup_tree(mi, memcg)
5200 val += mem_cgroup_read_events(mi, i);
5201 seq_printf(m, "total_%s %llu\n",
5202 mem_cgroup_events_names[i], val);
5203 }
5204
5205 for (i = 0; i < NR_LRU_LISTS; i++) {
5206 unsigned long long val = 0;
5207
5208 for_each_mem_cgroup_tree(mi, memcg)
5209 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5210 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005211 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005212
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005213#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005214 {
5215 int nid, zid;
5216 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005217 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005218 unsigned long recent_rotated[2] = {0, 0};
5219 unsigned long recent_scanned[2] = {0, 0};
5220
5221 for_each_online_node(nid)
5222 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005223 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005224 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005225
Hugh Dickins89abfab2012-05-29 15:06:53 -07005226 recent_rotated[0] += rstat->recent_rotated[0];
5227 recent_rotated[1] += rstat->recent_rotated[1];
5228 recent_scanned[0] += rstat->recent_scanned[0];
5229 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005230 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005231 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5232 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5233 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5234 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005235 }
5236#endif
5237
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005238 return 0;
5239}
5240
Tejun Heo182446d2013-08-08 20:11:24 -04005241static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
5242 struct cftype *cft)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005243{
Tejun Heo182446d2013-08-08 20:11:24 -04005244 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005245
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005246 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005247}
5248
Tejun Heo182446d2013-08-08 20:11:24 -04005249static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
5250 struct cftype *cft, u64 val)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005251{
Tejun Heo182446d2013-08-08 20:11:24 -04005252 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005253 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
Li Zefan068b38c2009-01-15 13:51:26 -08005254
Tejun Heo63876982013-08-08 20:11:23 -04005255 if (val > 100 || !parent)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005256 return -EINVAL;
5257
Glauber Costa09998212013-02-22 16:34:55 -08005258 mutex_lock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005259
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005260 /* If under hierarchy, only empty-root can set this value */
Glauber Costab5f99b52013-02-22 16:34:53 -08005261 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005262 mutex_unlock(&memcg_create_mutex);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005263 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005264 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005265
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005266 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005267
Glauber Costa09998212013-02-22 16:34:55 -08005268 mutex_unlock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005269
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005270 return 0;
5271}
5272
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005273static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5274{
5275 struct mem_cgroup_threshold_ary *t;
5276 u64 usage;
5277 int i;
5278
5279 rcu_read_lock();
5280 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005281 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005282 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005283 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005284
5285 if (!t)
5286 goto unlock;
5287
5288 usage = mem_cgroup_usage(memcg, swap);
5289
5290 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005291 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005292 * If it's not true, a threshold was crossed after last
5293 * call of __mem_cgroup_threshold().
5294 */
Phil Carmody5407a562010-05-26 14:42:42 -07005295 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005296
5297 /*
5298 * Iterate backward over array of thresholds starting from
5299 * current_threshold and check if a threshold is crossed.
5300 * If none of thresholds below usage is crossed, we read
5301 * only one element of the array here.
5302 */
5303 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5304 eventfd_signal(t->entries[i].eventfd, 1);
5305
5306 /* i = current_threshold + 1 */
5307 i++;
5308
5309 /*
5310 * Iterate forward over array of thresholds starting from
5311 * current_threshold+1 and check if a threshold is crossed.
5312 * If none of thresholds above usage is crossed, we read
5313 * only one element of the array here.
5314 */
5315 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5316 eventfd_signal(t->entries[i].eventfd, 1);
5317
5318 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005319 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005320unlock:
5321 rcu_read_unlock();
5322}
5323
5324static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5325{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005326 while (memcg) {
5327 __mem_cgroup_threshold(memcg, false);
5328 if (do_swap_account)
5329 __mem_cgroup_threshold(memcg, true);
5330
5331 memcg = parent_mem_cgroup(memcg);
5332 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005333}
5334
5335static int compare_thresholds(const void *a, const void *b)
5336{
5337 const struct mem_cgroup_threshold *_a = a;
5338 const struct mem_cgroup_threshold *_b = b;
5339
Greg Thelen2bff24a2013-09-11 14:23:08 -07005340 if (_a->threshold > _b->threshold)
5341 return 1;
5342
5343 if (_a->threshold < _b->threshold)
5344 return -1;
5345
5346 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005347}
5348
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005349static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005350{
5351 struct mem_cgroup_eventfd_list *ev;
5352
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005353 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005354 eventfd_signal(ev->eventfd, 1);
5355 return 0;
5356}
5357
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005358static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005359{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005360 struct mem_cgroup *iter;
5361
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005362 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005363 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005364}
5365
Tejun Heo81eeaf02013-08-08 20:11:26 -04005366static int mem_cgroup_usage_register_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005367 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005368{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005369 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005370 struct mem_cgroup_thresholds *thresholds;
5371 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005372 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005373 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005374 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005375
5376 ret = res_counter_memparse_write_strategy(args, &threshold);
5377 if (ret)
5378 return ret;
5379
5380 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005381
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005382 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005383 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005384 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005385 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005386 else
5387 BUG();
5388
5389 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5390
5391 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005392 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005393 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5394
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005395 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005396
5397 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005398 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005399 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005400 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005401 ret = -ENOMEM;
5402 goto unlock;
5403 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005404 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005405
5406 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005407 if (thresholds->primary) {
5408 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005409 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005410 }
5411
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005412 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005413 new->entries[size - 1].eventfd = eventfd;
5414 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005415
5416 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005417 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005418 compare_thresholds, NULL);
5419
5420 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005421 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005422 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005423 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005424 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005425 * new->current_threshold will not be used until
5426 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005427 * it here.
5428 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005429 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005430 } else
5431 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005432 }
5433
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005434 /* Free old spare buffer and save old primary buffer as spare */
5435 kfree(thresholds->spare);
5436 thresholds->spare = thresholds->primary;
5437
5438 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005439
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005440 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005441 synchronize_rcu();
5442
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005443unlock:
5444 mutex_unlock(&memcg->thresholds_lock);
5445
5446 return ret;
5447}
5448
Tejun Heo81eeaf02013-08-08 20:11:26 -04005449static void mem_cgroup_usage_unregister_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005450 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005451{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005452 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005453 struct mem_cgroup_thresholds *thresholds;
5454 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005455 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005456 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005457 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005458
5459 mutex_lock(&memcg->thresholds_lock);
5460 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005461 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005462 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005463 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005464 else
5465 BUG();
5466
Anton Vorontsov371528c2012-02-24 05:14:46 +04005467 if (!thresholds->primary)
5468 goto unlock;
5469
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005470 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5471
5472 /* Check if a threshold crossed before removing */
5473 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5474
5475 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005476 size = 0;
5477 for (i = 0; i < thresholds->primary->size; i++) {
5478 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005479 size++;
5480 }
5481
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005482 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005483
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005484 /* Set thresholds array to NULL if we don't have thresholds */
5485 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005486 kfree(new);
5487 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005488 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005489 }
5490
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005491 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005492
5493 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005494 new->current_threshold = -1;
5495 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5496 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005497 continue;
5498
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005499 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005500 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005501 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005502 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005503 * until rcu_assign_pointer(), so it's safe to increment
5504 * it here.
5505 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005506 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005507 }
5508 j++;
5509 }
5510
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005511swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005512 /* Swap primary and spare array */
5513 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005514 /* If all events are unregistered, free the spare array */
5515 if (!new) {
5516 kfree(thresholds->spare);
5517 thresholds->spare = NULL;
5518 }
5519
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005520 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005521
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005522 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005523 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005524unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005525 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005526}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005527
Tejun Heo81eeaf02013-08-08 20:11:26 -04005528static int mem_cgroup_oom_register_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005529 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5530{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005531 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005532 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005533 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005534
5535 BUG_ON(type != _OOM_TYPE);
5536 event = kmalloc(sizeof(*event), GFP_KERNEL);
5537 if (!event)
5538 return -ENOMEM;
5539
Michal Hocko1af8efe2011-07-26 16:08:24 -07005540 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005541
5542 event->eventfd = eventfd;
5543 list_add(&event->list, &memcg->oom_notify);
5544
5545 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005546 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005547 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005548 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005549
5550 return 0;
5551}
5552
Tejun Heo81eeaf02013-08-08 20:11:26 -04005553static void mem_cgroup_oom_unregister_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005554 struct cftype *cft, struct eventfd_ctx *eventfd)
5555{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005556 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005557 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005558 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005559
5560 BUG_ON(type != _OOM_TYPE);
5561
Michal Hocko1af8efe2011-07-26 16:08:24 -07005562 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005563
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005564 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005565 if (ev->eventfd == eventfd) {
5566 list_del(&ev->list);
5567 kfree(ev);
5568 }
5569 }
5570
Michal Hocko1af8efe2011-07-26 16:08:24 -07005571 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005572}
5573
Tejun Heo182446d2013-08-08 20:11:24 -04005574static int mem_cgroup_oom_control_read(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005575 struct cftype *cft, struct cgroup_map_cb *cb)
5576{
Tejun Heo182446d2013-08-08 20:11:24 -04005577 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005578
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005579 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005580
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005581 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005582 cb->fill(cb, "under_oom", 1);
5583 else
5584 cb->fill(cb, "under_oom", 0);
5585 return 0;
5586}
5587
Tejun Heo182446d2013-08-08 20:11:24 -04005588static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005589 struct cftype *cft, u64 val)
5590{
Tejun Heo182446d2013-08-08 20:11:24 -04005591 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005592 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005593
5594 /* cannot set to root cgroup and only 0 and 1 are allowed */
Tejun Heo63876982013-08-08 20:11:23 -04005595 if (!parent || !((val == 0) || (val == 1)))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005596 return -EINVAL;
5597
Glauber Costa09998212013-02-22 16:34:55 -08005598 mutex_lock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005599 /* oom-kill-disable is a flag for subhierarchy. */
Glauber Costab5f99b52013-02-22 16:34:53 -08005600 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005601 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005602 return -EINVAL;
5603 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005604 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005605 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005606 memcg_oom_recover(memcg);
Glauber Costa09998212013-02-22 16:34:55 -08005607 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005608 return 0;
5609}
5610
Andrew Mortonc255a452012-07-31 16:43:02 -07005611#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005612static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005613{
Glauber Costa55007d82012-12-18 14:22:38 -08005614 int ret;
5615
Glauber Costa2633d7a2012-12-18 14:22:34 -08005616 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005617 ret = memcg_propagate_kmem(memcg);
5618 if (ret)
5619 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005620
Glauber Costa1d62e432012-04-09 19:36:33 -03005621 return mem_cgroup_sockets_init(memcg, ss);
Michel Lespinasse573b4002013-04-29 15:08:13 -07005622}
Glauber Costae5671df2011-12-11 21:47:01 +00005623
Li Zefan10d5ebf2013-07-08 16:00:33 -07005624static void memcg_destroy_kmem(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005625{
Glauber Costa1d62e432012-04-09 19:36:33 -03005626 mem_cgroup_sockets_destroy(memcg);
Li Zefan10d5ebf2013-07-08 16:00:33 -07005627}
5628
5629static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
5630{
5631 if (!memcg_kmem_is_active(memcg))
5632 return;
5633
5634 /*
5635 * kmem charges can outlive the cgroup. In the case of slab
5636 * pages, for instance, a page contain objects from various
5637 * processes. As we prevent from taking a reference for every
5638 * such allocation we have to be careful when doing uncharge
5639 * (see memcg_uncharge_kmem) and here during offlining.
5640 *
5641 * The idea is that that only the _last_ uncharge which sees
5642 * the dead memcg will drop the last reference. An additional
5643 * reference is taken here before the group is marked dead
5644 * which is then paired with css_put during uncharge resp. here.
5645 *
5646 * Although this might sound strange as this path is called from
5647 * css_offline() when the referencemight have dropped down to 0
5648 * and shouldn't be incremented anymore (css_tryget would fail)
5649 * we do not have other options because of the kmem allocations
5650 * lifetime.
5651 */
5652 css_get(&memcg->css);
Glauber Costa7de37682012-12-18 14:22:07 -08005653
5654 memcg_kmem_mark_dead(memcg);
5655
5656 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5657 return;
5658
Glauber Costa7de37682012-12-18 14:22:07 -08005659 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07005660 css_put(&memcg->css);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005661}
Glauber Costae5671df2011-12-11 21:47:01 +00005662#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005663static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005664{
5665 return 0;
5666}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005667
Li Zefan10d5ebf2013-07-08 16:00:33 -07005668static void memcg_destroy_kmem(struct mem_cgroup *memcg)
5669{
5670}
5671
5672static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005673{
5674}
Glauber Costae5671df2011-12-11 21:47:01 +00005675#endif
5676
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005677static struct cftype mem_cgroup_files[] = {
5678 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005679 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005680 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005681 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005682 .register_event = mem_cgroup_usage_register_event,
5683 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005684 },
5685 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005686 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005687 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005688 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005689 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005690 },
5691 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005692 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005693 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005694 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005695 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005696 },
5697 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005698 .name = "soft_limit_in_bytes",
5699 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5700 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005701 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005702 },
5703 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005704 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005705 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005706 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005707 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005708 },
Balbir Singh8697d332008-02-07 00:13:59 -08005709 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005710 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005711 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005712 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005713 {
5714 .name = "force_empty",
5715 .trigger = mem_cgroup_force_empty_write,
5716 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005717 {
5718 .name = "use_hierarchy",
Tejun Heof00baae2013-04-15 13:41:15 -07005719 .flags = CFTYPE_INSANE,
Balbir Singh18f59ea2009-01-07 18:08:07 -08005720 .write_u64 = mem_cgroup_hierarchy_write,
5721 .read_u64 = mem_cgroup_hierarchy_read,
5722 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005723 {
5724 .name = "swappiness",
5725 .read_u64 = mem_cgroup_swappiness_read,
5726 .write_u64 = mem_cgroup_swappiness_write,
5727 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005728 {
5729 .name = "move_charge_at_immigrate",
5730 .read_u64 = mem_cgroup_move_charge_read,
5731 .write_u64 = mem_cgroup_move_charge_write,
5732 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005733 {
5734 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005735 .read_map = mem_cgroup_oom_control_read,
5736 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005737 .register_event = mem_cgroup_oom_register_event,
5738 .unregister_event = mem_cgroup_oom_unregister_event,
5739 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5740 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07005741 {
5742 .name = "pressure_level",
5743 .register_event = vmpressure_register_event,
5744 .unregister_event = vmpressure_unregister_event,
5745 },
Ying Han406eb0c2011-05-26 16:25:37 -07005746#ifdef CONFIG_NUMA
5747 {
5748 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005749 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005750 },
5751#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005752#ifdef CONFIG_MEMCG_KMEM
5753 {
5754 .name = "kmem.limit_in_bytes",
5755 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5756 .write_string = mem_cgroup_write,
5757 .read = mem_cgroup_read,
5758 },
5759 {
5760 .name = "kmem.usage_in_bytes",
5761 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5762 .read = mem_cgroup_read,
5763 },
5764 {
5765 .name = "kmem.failcnt",
5766 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5767 .trigger = mem_cgroup_reset,
5768 .read = mem_cgroup_read,
5769 },
5770 {
5771 .name = "kmem.max_usage_in_bytes",
5772 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5773 .trigger = mem_cgroup_reset,
5774 .read = mem_cgroup_read,
5775 },
Glauber Costa749c5412012-12-18 14:23:01 -08005776#ifdef CONFIG_SLABINFO
5777 {
5778 .name = "kmem.slabinfo",
5779 .read_seq_string = mem_cgroup_slabinfo_read,
5780 },
5781#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005782#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005783 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005784};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005785
Michal Hocko2d110852013-02-22 16:34:43 -08005786#ifdef CONFIG_MEMCG_SWAP
5787static struct cftype memsw_cgroup_files[] = {
5788 {
5789 .name = "memsw.usage_in_bytes",
5790 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
5791 .read = mem_cgroup_read,
5792 .register_event = mem_cgroup_usage_register_event,
5793 .unregister_event = mem_cgroup_usage_unregister_event,
5794 },
5795 {
5796 .name = "memsw.max_usage_in_bytes",
5797 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5798 .trigger = mem_cgroup_reset,
5799 .read = mem_cgroup_read,
5800 },
5801 {
5802 .name = "memsw.limit_in_bytes",
5803 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5804 .write_string = mem_cgroup_write,
5805 .read = mem_cgroup_read,
5806 },
5807 {
5808 .name = "memsw.failcnt",
5809 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5810 .trigger = mem_cgroup_reset,
5811 .read = mem_cgroup_read,
5812 },
5813 { }, /* terminate */
5814};
5815#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005816static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005817{
5818 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005819 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005820 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005821 /*
5822 * This routine is called against possible nodes.
5823 * But it's BUG to call kmalloc() against offline node.
5824 *
5825 * TODO: this routine can waste much memory for nodes which will
5826 * never be onlined. It's better to use memory hotplug callback
5827 * function.
5828 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005829 if (!node_state(node, N_NORMAL_MEMORY))
5830 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005831 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005832 if (!pn)
5833 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005834
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005835 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5836 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005837 lruvec_init(&mz->lruvec);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005838 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005839 }
Johannes Weiner54f72fe2013-07-08 15:59:49 -07005840 memcg->nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005841 return 0;
5842}
5843
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005844static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005845{
Johannes Weiner54f72fe2013-07-08 15:59:49 -07005846 kfree(memcg->nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005847}
5848
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005849static struct mem_cgroup *mem_cgroup_alloc(void)
5850{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005851 struct mem_cgroup *memcg;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005852 size_t size = memcg_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005853
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005854 /* Can be very big if nr_node_ids is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005855 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005856 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005857 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005858 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005859
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005860 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005861 return NULL;
5862
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005863 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5864 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005865 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005866 spin_lock_init(&memcg->pcp_counter_lock);
5867 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005868
5869out_free:
5870 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005871 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005872 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005873 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005874 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005875}
5876
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005877/*
Glauber Costac8b2a362012-12-18 14:22:13 -08005878 * At destroying mem_cgroup, references from swap_cgroup can remain.
5879 * (scanning all at force_empty is too costly...)
5880 *
5881 * Instead of clearing all references at force_empty, we remember
5882 * the number of reference from swap_cgroup and free mem_cgroup when
5883 * it goes down to 0.
5884 *
5885 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07005886 */
Glauber Costac8b2a362012-12-18 14:22:13 -08005887
5888static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005889{
Glauber Costac8b2a362012-12-18 14:22:13 -08005890 int node;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005891 size_t size = memcg_size();
Hugh Dickins59927fb2012-03-15 15:17:07 -07005892
Glauber Costac8b2a362012-12-18 14:22:13 -08005893 free_css_id(&mem_cgroup_subsys, &memcg->css);
5894
5895 for_each_node(node)
5896 free_mem_cgroup_per_zone_info(memcg, node);
5897
5898 free_percpu(memcg->stat);
5899
Glauber Costa3f134612012-05-29 15:07:11 -07005900 /*
5901 * We need to make sure that (at least for now), the jump label
5902 * destruction code runs outside of the cgroup lock. This is because
5903 * get_online_cpus(), which is called from the static_branch update,
5904 * can't be called inside the cgroup_lock. cpusets are the ones
5905 * enforcing this dependency, so if they ever change, we might as well.
5906 *
5907 * schedule_work() will guarantee this happens. Be careful if you need
5908 * to move this code around, and make sure it is outside
5909 * the cgroup_lock.
5910 */
Glauber Costaa8964b92012-12-18 14:22:09 -08005911 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005912 if (size < PAGE_SIZE)
5913 kfree(memcg);
5914 else
5915 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005916}
Glauber Costa3afe36b2012-05-29 15:07:10 -07005917
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005918/*
5919 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
5920 */
Glauber Costae1aab162011-12-11 21:47:03 +00005921struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005922{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005923 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005924 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005925 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005926}
Glauber Costae1aab162011-12-11 21:47:03 +00005927EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005928
Li Zefan0eb253e2009-01-15 13:51:25 -08005929static struct cgroup_subsys_state * __ref
Tejun Heoeb954192013-08-08 20:11:23 -04005930mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005931{
Glauber Costad142e3e2013-02-22 16:34:52 -08005932 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005933 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005934 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005935
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005936 memcg = mem_cgroup_alloc();
5937 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005938 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08005939
Bob Liu3ed28fa2012-01-12 17:19:04 -08005940 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005941 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005942 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005943
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005944 /* root ? */
Tejun Heoeb954192013-08-08 20:11:23 -04005945 if (parent_css == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08005946 root_mem_cgroup = memcg;
Glauber Costad142e3e2013-02-22 16:34:52 -08005947 res_counter_init(&memcg->res, NULL);
5948 res_counter_init(&memcg->memsw, NULL);
5949 res_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005950 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005951
Glauber Costad142e3e2013-02-22 16:34:52 -08005952 memcg->last_scanned_node = MAX_NUMNODES;
5953 INIT_LIST_HEAD(&memcg->oom_notify);
Glauber Costad142e3e2013-02-22 16:34:52 -08005954 memcg->move_charge_at_immigrate = 0;
5955 mutex_init(&memcg->thresholds_lock);
5956 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07005957 vmpressure_init(&memcg->vmpressure);
Glauber Costad142e3e2013-02-22 16:34:52 -08005958
5959 return &memcg->css;
5960
5961free_out:
5962 __mem_cgroup_free(memcg);
5963 return ERR_PTR(error);
5964}
5965
5966static int
Tejun Heoeb954192013-08-08 20:11:23 -04005967mem_cgroup_css_online(struct cgroup_subsys_state *css)
Glauber Costad142e3e2013-02-22 16:34:52 -08005968{
Tejun Heoeb954192013-08-08 20:11:23 -04005969 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5970 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(css));
Glauber Costad142e3e2013-02-22 16:34:52 -08005971 int error = 0;
5972
Tejun Heo63876982013-08-08 20:11:23 -04005973 if (!parent)
Glauber Costad142e3e2013-02-22 16:34:52 -08005974 return 0;
5975
Glauber Costa09998212013-02-22 16:34:55 -08005976 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08005977
5978 memcg->use_hierarchy = parent->use_hierarchy;
5979 memcg->oom_kill_disable = parent->oom_kill_disable;
5980 memcg->swappiness = mem_cgroup_swappiness(parent);
5981
5982 if (parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005983 res_counter_init(&memcg->res, &parent->res);
5984 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005985 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08005986
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005987 /*
Li Zefan8d76a972013-07-08 16:00:36 -07005988 * No need to take a reference to the parent because cgroup
5989 * core guarantees its existence.
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005990 */
Balbir Singh18f59ea2009-01-07 18:08:07 -08005991 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005992 res_counter_init(&memcg->res, NULL);
5993 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005994 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07005995 /*
5996 * Deeper hierachy with use_hierarchy == false doesn't make
5997 * much sense so let cgroup subsystem know about this
5998 * unfortunate state in our controller.
5999 */
Glauber Costad142e3e2013-02-22 16:34:52 -08006000 if (parent != root_mem_cgroup)
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006001 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006002 }
Glauber Costacbe128e32012-04-09 19:36:34 -03006003
6004 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
Glauber Costa09998212013-02-22 16:34:55 -08006005 mutex_unlock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006006 return error;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006007}
6008
Michal Hocko5f578162013-04-29 15:07:17 -07006009/*
6010 * Announce all parents that a group from their hierarchy is gone.
6011 */
6012static void mem_cgroup_invalidate_reclaim_iterators(struct mem_cgroup *memcg)
6013{
6014 struct mem_cgroup *parent = memcg;
6015
6016 while ((parent = parent_mem_cgroup(parent)))
Johannes Weiner519ebea2013-07-03 15:04:51 -07006017 mem_cgroup_iter_invalidate(parent);
Michal Hocko5f578162013-04-29 15:07:17 -07006018
6019 /*
6020 * if the root memcg is not hierarchical we have to check it
6021 * explicitely.
6022 */
6023 if (!root_mem_cgroup->use_hierarchy)
Johannes Weiner519ebea2013-07-03 15:04:51 -07006024 mem_cgroup_iter_invalidate(root_mem_cgroup);
Michal Hocko5f578162013-04-29 15:07:17 -07006025}
6026
Tejun Heoeb954192013-08-08 20:11:23 -04006027static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006028{
Tejun Heoeb954192013-08-08 20:11:23 -04006029 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006030
Li Zefan10d5ebf2013-07-08 16:00:33 -07006031 kmem_cgroup_css_offline(memcg);
6032
Michal Hocko5f578162013-04-29 15:07:17 -07006033 mem_cgroup_invalidate_reclaim_iterators(memcg);
Michal Hockoab5196c2012-10-26 13:37:32 +02006034 mem_cgroup_reparent_charges(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08006035 mem_cgroup_destroy_all_caches(memcg);
Michal Hocko33cb8762013-07-31 13:53:51 -07006036 vmpressure_cleanup(&memcg->vmpressure);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006037}
6038
Tejun Heoeb954192013-08-08 20:11:23 -04006039static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006040{
Tejun Heoeb954192013-08-08 20:11:23 -04006041 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006042
Li Zefan10d5ebf2013-07-08 16:00:33 -07006043 memcg_destroy_kmem(memcg);
Li Zefan465939a2013-07-08 16:00:38 -07006044 __mem_cgroup_free(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006045}
6046
Daisuke Nishimura02491442010-03-10 15:22:17 -08006047#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006048/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006049#define PRECHARGE_COUNT_AT_ONCE 256
6050static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006051{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006052 int ret = 0;
6053 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006054 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006055
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006056 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006057 mc.precharge += count;
6058 /* we don't need css_get for root */
6059 return ret;
6060 }
6061 /* try to charge at once */
6062 if (count > 1) {
6063 struct res_counter *dummy;
6064 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006065 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006066 * by cgroup_lock_live_cgroup() that it is not removed and we
6067 * are still under the same cgroup_mutex. So we can postpone
6068 * css_get().
6069 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006070 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006071 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006072 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006073 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006074 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006075 goto one_by_one;
6076 }
6077 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006078 return ret;
6079 }
6080one_by_one:
6081 /* fall back to one by one charge */
6082 while (count--) {
6083 if (signal_pending(current)) {
6084 ret = -EINTR;
6085 break;
6086 }
6087 if (!batch_count--) {
6088 batch_count = PRECHARGE_COUNT_AT_ONCE;
6089 cond_resched();
6090 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006091 ret = __mem_cgroup_try_charge(NULL,
6092 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006093 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006094 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006095 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006096 mc.precharge++;
6097 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006098 return ret;
6099}
6100
6101/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006102 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006103 * @vma: the vma the pte to be checked belongs
6104 * @addr: the address corresponding to the pte to be checked
6105 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006106 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006107 *
6108 * Returns
6109 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6110 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6111 * move charge. if @target is not NULL, the page is stored in target->page
6112 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006113 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6114 * target for charge migration. if @target is not NULL, the entry is stored
6115 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006116 *
6117 * Called with pte lock held.
6118 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006119union mc_target {
6120 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006121 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006122};
6123
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006124enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006125 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006126 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006127 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006128};
6129
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006130static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6131 unsigned long addr, pte_t ptent)
6132{
6133 struct page *page = vm_normal_page(vma, addr, ptent);
6134
6135 if (!page || !page_mapped(page))
6136 return NULL;
6137 if (PageAnon(page)) {
6138 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006139 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006140 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006141 } else if (!move_file())
6142 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006143 return NULL;
6144 if (!get_page_unless_zero(page))
6145 return NULL;
6146
6147 return page;
6148}
6149
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006150#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006151static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6152 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6153{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006154 struct page *page = NULL;
6155 swp_entry_t ent = pte_to_swp_entry(ptent);
6156
6157 if (!move_anon() || non_swap_entry(ent))
6158 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006159 /*
6160 * Because lookup_swap_cache() updates some statistics counter,
6161 * we call find_get_page() with swapper_space directly.
6162 */
Shaohua Li33806f02013-02-22 16:34:37 -08006163 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006164 if (do_swap_account)
6165 entry->val = ent.val;
6166
6167 return page;
6168}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006169#else
6170static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6171 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6172{
6173 return NULL;
6174}
6175#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006176
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006177static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6178 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6179{
6180 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006181 struct address_space *mapping;
6182 pgoff_t pgoff;
6183
6184 if (!vma->vm_file) /* anonymous vma */
6185 return NULL;
6186 if (!move_file())
6187 return NULL;
6188
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006189 mapping = vma->vm_file->f_mapping;
6190 if (pte_none(ptent))
6191 pgoff = linear_page_index(vma, addr);
6192 else /* pte_file(ptent) is true */
6193 pgoff = pte_to_pgoff(ptent);
6194
6195 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006196 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006197
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006198#ifdef CONFIG_SWAP
6199 /* shmem/tmpfs may report page out on swap: account for that too. */
6200 if (radix_tree_exceptional_entry(page)) {
6201 swp_entry_t swap = radix_to_swp_entry(page);
6202 if (do_swap_account)
6203 *entry = swap;
Shaohua Li33806f02013-02-22 16:34:37 -08006204 page = find_get_page(swap_address_space(swap), swap.val);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006205 }
6206#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006207 return page;
6208}
6209
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006210static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006211 unsigned long addr, pte_t ptent, union mc_target *target)
6212{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006213 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006214 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006215 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006216 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006217
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006218 if (pte_present(ptent))
6219 page = mc_handle_present_pte(vma, addr, ptent);
6220 else if (is_swap_pte(ptent))
6221 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006222 else if (pte_none(ptent) || pte_file(ptent))
6223 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006224
6225 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006226 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006227 if (page) {
6228 pc = lookup_page_cgroup(page);
6229 /*
6230 * Do only loose check w/o page_cgroup lock.
6231 * mem_cgroup_move_account() checks the pc is valid or not under
6232 * the lock.
6233 */
6234 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6235 ret = MC_TARGET_PAGE;
6236 if (target)
6237 target->page = page;
6238 }
6239 if (!ret || !target)
6240 put_page(page);
6241 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006242 /* There is a swap entry and a page doesn't exist or isn't charged */
6243 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08006244 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006245 ret = MC_TARGET_SWAP;
6246 if (target)
6247 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006248 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006249 return ret;
6250}
6251
Naoya Horiguchi12724852012-03-21 16:34:28 -07006252#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6253/*
6254 * We don't consider swapping or file mapped pages because THP does not
6255 * support them for now.
6256 * Caller should make sure that pmd_trans_huge(pmd) is true.
6257 */
6258static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6259 unsigned long addr, pmd_t pmd, union mc_target *target)
6260{
6261 struct page *page = NULL;
6262 struct page_cgroup *pc;
6263 enum mc_target_type ret = MC_TARGET_NONE;
6264
6265 page = pmd_page(pmd);
6266 VM_BUG_ON(!page || !PageHead(page));
6267 if (!move_anon())
6268 return ret;
6269 pc = lookup_page_cgroup(page);
6270 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6271 ret = MC_TARGET_PAGE;
6272 if (target) {
6273 get_page(page);
6274 target->page = page;
6275 }
6276 }
6277 return ret;
6278}
6279#else
6280static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6281 unsigned long addr, pmd_t pmd, union mc_target *target)
6282{
6283 return MC_TARGET_NONE;
6284}
6285#endif
6286
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006287static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6288 unsigned long addr, unsigned long end,
6289 struct mm_walk *walk)
6290{
6291 struct vm_area_struct *vma = walk->private;
6292 pte_t *pte;
6293 spinlock_t *ptl;
6294
Naoya Horiguchi12724852012-03-21 16:34:28 -07006295 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6296 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6297 mc.precharge += HPAGE_PMD_NR;
6298 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006299 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006300 }
Dave Hansen03319322011-03-22 16:32:56 -07006301
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006302 if (pmd_trans_unstable(pmd))
6303 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006304 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6305 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006306 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006307 mc.precharge++; /* increment precharge temporarily */
6308 pte_unmap_unlock(pte - 1, ptl);
6309 cond_resched();
6310
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006311 return 0;
6312}
6313
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006314static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6315{
6316 unsigned long precharge;
6317 struct vm_area_struct *vma;
6318
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006319 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006320 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6321 struct mm_walk mem_cgroup_count_precharge_walk = {
6322 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6323 .mm = mm,
6324 .private = vma,
6325 };
6326 if (is_vm_hugetlb_page(vma))
6327 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006328 walk_page_range(vma->vm_start, vma->vm_end,
6329 &mem_cgroup_count_precharge_walk);
6330 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006331 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006332
6333 precharge = mc.precharge;
6334 mc.precharge = 0;
6335
6336 return precharge;
6337}
6338
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006339static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6340{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006341 unsigned long precharge = mem_cgroup_count_precharge(mm);
6342
6343 VM_BUG_ON(mc.moving_task);
6344 mc.moving_task = current;
6345 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006346}
6347
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006348/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6349static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006350{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006351 struct mem_cgroup *from = mc.from;
6352 struct mem_cgroup *to = mc.to;
Li Zefan40503772013-07-08 16:00:34 -07006353 int i;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006354
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006355 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006356 if (mc.precharge) {
6357 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6358 mc.precharge = 0;
6359 }
6360 /*
6361 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6362 * we must uncharge here.
6363 */
6364 if (mc.moved_charge) {
6365 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6366 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006367 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006368 /* we must fixup refcnts and charges */
6369 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006370 /* uncharge swap account from the old cgroup */
6371 if (!mem_cgroup_is_root(mc.from))
6372 res_counter_uncharge(&mc.from->memsw,
6373 PAGE_SIZE * mc.moved_swap);
Li Zefan40503772013-07-08 16:00:34 -07006374
6375 for (i = 0; i < mc.moved_swap; i++)
6376 css_put(&mc.from->css);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006377
6378 if (!mem_cgroup_is_root(mc.to)) {
6379 /*
6380 * we charged both to->res and to->memsw, so we should
6381 * uncharge to->res.
6382 */
6383 res_counter_uncharge(&mc.to->res,
6384 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006385 }
Li Zefan40503772013-07-08 16:00:34 -07006386 /* we've already done css_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006387 mc.moved_swap = 0;
6388 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006389 memcg_oom_recover(from);
6390 memcg_oom_recover(to);
6391 wake_up_all(&mc.waitq);
6392}
6393
6394static void mem_cgroup_clear_mc(void)
6395{
6396 struct mem_cgroup *from = mc.from;
6397
6398 /*
6399 * we must clear moving_task before waking up waiters at the end of
6400 * task migration.
6401 */
6402 mc.moving_task = NULL;
6403 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006404 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006405 mc.from = NULL;
6406 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006407 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006408 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006409}
6410
Tejun Heoeb954192013-08-08 20:11:23 -04006411static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef52012-01-31 13:47:36 +08006412 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006413{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006414 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006415 int ret = 0;
Tejun Heoeb954192013-08-08 20:11:23 -04006416 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costaee5e8472013-02-22 16:34:50 -08006417 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006418
Glauber Costaee5e8472013-02-22 16:34:50 -08006419 /*
6420 * We are now commited to this value whatever it is. Changes in this
6421 * tunable will only affect upcoming migrations, not the current one.
6422 * So we need to save it, and keep it going.
6423 */
6424 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
6425 if (move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006426 struct mm_struct *mm;
6427 struct mem_cgroup *from = mem_cgroup_from_task(p);
6428
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006429 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006430
6431 mm = get_task_mm(p);
6432 if (!mm)
6433 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006434 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006435 if (mm->owner == p) {
6436 VM_BUG_ON(mc.from);
6437 VM_BUG_ON(mc.to);
6438 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006439 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006440 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006441 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006442 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006443 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006444 mc.to = memcg;
Glauber Costaee5e8472013-02-22 16:34:50 -08006445 mc.immigrate_flags = move_charge_at_immigrate;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006446 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006447 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006448
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006449 ret = mem_cgroup_precharge_mc(mm);
6450 if (ret)
6451 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006452 }
6453 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006454 }
6455 return ret;
6456}
6457
Tejun Heoeb954192013-08-08 20:11:23 -04006458static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef52012-01-31 13:47:36 +08006459 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006460{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006461 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006462}
6463
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006464static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6465 unsigned long addr, unsigned long end,
6466 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006467{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006468 int ret = 0;
6469 struct vm_area_struct *vma = walk->private;
6470 pte_t *pte;
6471 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006472 enum mc_target_type target_type;
6473 union mc_target target;
6474 struct page *page;
6475 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006476
Naoya Horiguchi12724852012-03-21 16:34:28 -07006477 /*
6478 * We don't take compound_lock() here but no race with splitting thp
6479 * happens because:
6480 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
6481 * under splitting, which means there's no concurrent thp split,
6482 * - if another thread runs into split_huge_page() just after we
6483 * entered this if-block, the thread must wait for page table lock
6484 * to be unlocked in __split_huge_page_splitting(), where the main
6485 * part of thp split is not executed yet.
6486 */
6487 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07006488 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006489 spin_unlock(&vma->vm_mm->page_table_lock);
6490 return 0;
6491 }
6492 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6493 if (target_type == MC_TARGET_PAGE) {
6494 page = target.page;
6495 if (!isolate_lru_page(page)) {
6496 pc = lookup_page_cgroup(page);
6497 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006498 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006499 mc.precharge -= HPAGE_PMD_NR;
6500 mc.moved_charge += HPAGE_PMD_NR;
6501 }
6502 putback_lru_page(page);
6503 }
6504 put_page(page);
6505 }
6506 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006507 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006508 }
6509
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006510 if (pmd_trans_unstable(pmd))
6511 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006512retry:
6513 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6514 for (; addr != end; addr += PAGE_SIZE) {
6515 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08006516 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006517
6518 if (!mc.precharge)
6519 break;
6520
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006521 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006522 case MC_TARGET_PAGE:
6523 page = target.page;
6524 if (isolate_lru_page(page))
6525 goto put;
6526 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07006527 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006528 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006529 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006530 /* we uncharge from mc.from later. */
6531 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006532 }
6533 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006534put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006535 put_page(page);
6536 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006537 case MC_TARGET_SWAP:
6538 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006539 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006540 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006541 /* we fixup refcnts and charges later. */
6542 mc.moved_swap++;
6543 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006544 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006545 default:
6546 break;
6547 }
6548 }
6549 pte_unmap_unlock(pte - 1, ptl);
6550 cond_resched();
6551
6552 if (addr != end) {
6553 /*
6554 * We have consumed all precharges we got in can_attach().
6555 * We try charge one by one, but don't do any additional
6556 * charges to mc.to if we have failed in charge once in attach()
6557 * phase.
6558 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006559 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006560 if (!ret)
6561 goto retry;
6562 }
6563
6564 return ret;
6565}
6566
6567static void mem_cgroup_move_charge(struct mm_struct *mm)
6568{
6569 struct vm_area_struct *vma;
6570
6571 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006572retry:
6573 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6574 /*
6575 * Someone who are holding the mmap_sem might be waiting in
6576 * waitq. So we cancel all extra charges, wake up all waiters,
6577 * and retry. Because we cancel precharges, we might not be able
6578 * to move enough charges, but moving charge is a best-effort
6579 * feature anyway, so it wouldn't be a big problem.
6580 */
6581 __mem_cgroup_clear_mc();
6582 cond_resched();
6583 goto retry;
6584 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006585 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6586 int ret;
6587 struct mm_walk mem_cgroup_move_charge_walk = {
6588 .pmd_entry = mem_cgroup_move_charge_pte_range,
6589 .mm = mm,
6590 .private = vma,
6591 };
6592 if (is_vm_hugetlb_page(vma))
6593 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006594 ret = walk_page_range(vma->vm_start, vma->vm_end,
6595 &mem_cgroup_move_charge_walk);
6596 if (ret)
6597 /*
6598 * means we have consumed all precharges and failed in
6599 * doing additional charge. Just abandon here.
6600 */
6601 break;
6602 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006603 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006604}
6605
Tejun Heoeb954192013-08-08 20:11:23 -04006606static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef52012-01-31 13:47:36 +08006607 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006608{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006609 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006610 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006611
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006612 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006613 if (mc.to)
6614 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006615 mmput(mm);
6616 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006617 if (mc.to)
6618 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006619}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006620#else /* !CONFIG_MMU */
Tejun Heoeb954192013-08-08 20:11:23 -04006621static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef52012-01-31 13:47:36 +08006622 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006623{
6624 return 0;
6625}
Tejun Heoeb954192013-08-08 20:11:23 -04006626static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef52012-01-31 13:47:36 +08006627 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006628{
6629}
Tejun Heoeb954192013-08-08 20:11:23 -04006630static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef52012-01-31 13:47:36 +08006631 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006632{
6633}
6634#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006635
Tejun Heof00baae2013-04-15 13:41:15 -07006636/*
6637 * Cgroup retains root cgroups across [un]mount cycles making it necessary
6638 * to verify sane_behavior flag on each mount attempt.
6639 */
Tejun Heoeb954192013-08-08 20:11:23 -04006640static void mem_cgroup_bind(struct cgroup_subsys_state *root_css)
Tejun Heof00baae2013-04-15 13:41:15 -07006641{
6642 /*
6643 * use_hierarchy is forced with sane_behavior. cgroup core
6644 * guarantees that @root doesn't have any children, so turning it
6645 * on for the root memcg is enough.
6646 */
Tejun Heoeb954192013-08-08 20:11:23 -04006647 if (cgroup_sane_behavior(root_css->cgroup))
6648 mem_cgroup_from_css(root_css)->use_hierarchy = true;
Tejun Heof00baae2013-04-15 13:41:15 -07006649}
6650
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006651struct cgroup_subsys mem_cgroup_subsys = {
6652 .name = "memory",
6653 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08006654 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08006655 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08006656 .css_offline = mem_cgroup_css_offline,
6657 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006658 .can_attach = mem_cgroup_can_attach,
6659 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08006660 .attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07006661 .bind = mem_cgroup_bind,
Tejun Heo6bc10342012-04-01 12:09:55 -07006662 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006663 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006664 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006665};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006666
Andrew Mortonc255a452012-07-31 16:43:02 -07006667#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08006668static int __init enable_swap_account(char *s)
6669{
Michal Hockoa2c89902011-05-24 17:12:50 -07006670 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006671 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006672 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006673 really_do_swap_account = 0;
6674 return 1;
6675}
Michal Hockoa2c89902011-05-24 17:12:50 -07006676__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006677
Michal Hocko2d110852013-02-22 16:34:43 -08006678static void __init memsw_file_init(void)
6679{
Michal Hocko6acc8b02013-02-22 16:34:45 -08006680 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08006681}
Michal Hocko6acc8b02013-02-22 16:34:45 -08006682
6683static void __init enable_swap_cgroup(void)
6684{
6685 if (!mem_cgroup_disabled() && really_do_swap_account) {
6686 do_swap_account = 1;
6687 memsw_file_init();
6688 }
6689}
6690
Michal Hocko2d110852013-02-22 16:34:43 -08006691#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08006692static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08006693{
6694}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006695#endif
Michal Hocko2d110852013-02-22 16:34:43 -08006696
6697/*
Michal Hocko10813122013-02-22 16:35:41 -08006698 * subsys_initcall() for memory controller.
6699 *
6700 * Some parts like hotcpu_notifier() have to be initialized from this context
6701 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
6702 * everything that doesn't depend on a specific mem_cgroup structure should
6703 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08006704 */
6705static int __init mem_cgroup_init(void)
6706{
6707 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08006708 enable_swap_cgroup();
Michal Hockoe4777492013-02-22 16:35:40 -08006709 memcg_stock_init();
Michal Hocko2d110852013-02-22 16:34:43 -08006710 return 0;
6711}
6712subsys_initcall(mem_cgroup_init);