blob: a8113b77b23a1f634f051f94c6e0039a74d921c0 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002/* memcontrol.c - Memory Controller
3 *
4 * Copyright IBM Corporation, 2007
5 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
6 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08007 * Copyright 2007 OpenVZ SWsoft Inc
8 * Author: Pavel Emelianov <xemul@openvz.org>
9 *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080010 * Memory thresholds
11 * Copyright (C) 2009 Nokia Corporation
12 * Author: Kirill A. Shutemov
13 *
Glauber Costa7ae1e1d2012-12-18 14:21:56 -080014 * Kernel Memory Controller
15 * Copyright (C) 2012 Parallels Inc. and Google Inc.
16 * Authors: Glauber Costa and Suleiman Souhlal
17 *
Johannes Weiner1575e682015-04-14 15:44:51 -070018 * Native page reclaim
19 * Charge lifetime sanitation
20 * Lockless page tracking & accounting
21 * Unified hierarchy configuration model
22 * Copyright (C) 2015 Red Hat, Inc., Johannes Weiner
Balbir Singh8cdea7c2008-02-07 00:13:50 -080023 */
24
Johannes Weiner3e32cb22014-12-10 15:42:31 -080025#include <linux/page_counter.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080026#include <linux/memcontrol.h>
27#include <linux/cgroup.h>
Christoph Hellwiga5201102019-08-28 16:19:53 +020028#include <linux/pagewalk.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010029#include <linux/sched/mm.h>
Hugh Dickins3a4f8a02017-02-24 14:59:36 -080030#include <linux/shmem_fs.h>
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -080031#include <linux/hugetlb.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080032#include <linux/pagemap.h>
Chris Down1ff9e6e2019-03-05 15:48:09 -080033#include <linux/vm_event_item.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>
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -070042#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070043#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080044#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080045#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080046#include <linux/spinlock.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080047#include <linux/eventfd.h>
Tejun Heo79bd9812013-11-22 18:20:42 -050048#include <linux/poll.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080049#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080050#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080051#include <linux/seq_file.h>
Anton Vorontsov70ddf632013-04-29 15:08:31 -070052#include <linux/vmpressure.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070053#include <linux/mm_inline.h>
Johannes Weiner5d1ea482014-12-10 15:44:55 -080054#include <linux/swap_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080055#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070056#include <linux/oom.h>
Johannes Weiner0056f4e2013-10-31 16:34:14 -070057#include <linux/lockdep.h>
Tejun Heo79bd9812013-11-22 18:20:42 -050058#include <linux/file.h>
Tejun Heob23afb92015-11-05 18:46:11 -080059#include <linux/tracehook.h>
Chris Down0e4b01d2019-09-23 15:34:55 -070060#include <linux/psi.h>
Johannes Weinerc8713d02019-07-11 20:55:59 -070061#include <linux/seq_buf.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080062#include "internal.h"
Glauber Costad1a4c0b2011-12-11 21:47:04 +000063#include <net/sock.h>
Michal Hocko4bd2c1e2012-10-08 16:33:10 -070064#include <net/ip.h>
Qiang Huangf35c3a82013-11-12 15:08:22 -080065#include "slab.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080066
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080067#include <linux/uaccess.h>
Balbir Singh8697d332008-02-07 00:13:59 -080068
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070069#include <trace/events/vmscan.h>
70
Tejun Heo073219e2014-02-08 10:36:58 -050071struct cgroup_subsys memory_cgrp_subsys __read_mostly;
72EXPORT_SYMBOL(memory_cgrp_subsys);
David Rientjes68ae5642012-12-12 13:51:57 -080073
Johannes Weiner7d828602016-01-14 15:20:56 -080074struct mem_cgroup *root_mem_cgroup __read_mostly;
75
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070076#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh8cdea7c2008-02-07 00:13:50 -080077
Johannes Weinerf7e1cb62016-01-14 15:21:29 -080078/* Socket memory accounting disabled? */
79static bool cgroup_memory_nosocket;
80
Vladimir Davydov04823c82016-01-20 15:02:38 -080081/* Kernel memory accounting disabled? */
82static bool cgroup_memory_nokmem;
83
Johannes Weiner21afa382015-02-11 15:26:36 -080084/* Whether the swap controller is active */
Andrew Mortonc255a452012-07-31 16:43:02 -070085#ifdef CONFIG_MEMCG_SWAP
Johannes Weinereccb52e2020-06-03 16:02:11 -070086bool cgroup_memory_noswap __read_mostly;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080087#else
Johannes Weinereccb52e2020-06-03 16:02:11 -070088#define cgroup_memory_noswap 1
Johannes Weiner2d1c4982020-06-03 16:02:14 -070089#endif
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080090
Tejun Heo97b27822019-08-26 09:06:56 -070091#ifdef CONFIG_CGROUP_WRITEBACK
92static DECLARE_WAIT_QUEUE_HEAD(memcg_cgwb_frn_waitq);
93#endif
94
Johannes Weiner7941d212016-01-14 15:21:23 -080095/* Whether legacy memory+swap accounting is active */
96static bool do_memsw_account(void)
97{
Johannes Weinereccb52e2020-06-03 16:02:11 -070098 return !cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_noswap;
Johannes Weiner7941d212016-01-14 15:21:23 -080099}
100
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700101#define THRESHOLDS_EVENTS_TARGET 128
102#define SOFTLIMIT_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700103
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700104/*
105 * Cgroups above their limits are maintained in a RB-Tree, independent of
106 * their hierarchy representation
107 */
108
Mel Gormanef8f2322016-07-28 15:46:05 -0700109struct mem_cgroup_tree_per_node {
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700110 struct rb_root rb_root;
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700111 struct rb_node *rb_rightmost;
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700112 spinlock_t lock;
113};
114
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700115struct mem_cgroup_tree {
116 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
117};
118
119static struct mem_cgroup_tree soft_limit_tree __read_mostly;
120
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700121/* for OOM */
122struct mem_cgroup_eventfd_list {
123 struct list_head list;
124 struct eventfd_ctx *eventfd;
125};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800126
Tejun Heo79bd9812013-11-22 18:20:42 -0500127/*
128 * cgroup_event represents events which userspace want to receive.
129 */
Tejun Heo3bc942f2013-11-22 18:20:44 -0500130struct mem_cgroup_event {
Tejun Heo79bd9812013-11-22 18:20:42 -0500131 /*
Tejun Heo59b6f872013-11-22 18:20:43 -0500132 * memcg which the event belongs to.
Tejun Heo79bd9812013-11-22 18:20:42 -0500133 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500134 struct mem_cgroup *memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -0500135 /*
Tejun Heo79bd9812013-11-22 18:20:42 -0500136 * eventfd to signal userspace about the event.
137 */
138 struct eventfd_ctx *eventfd;
139 /*
140 * Each of these stored in a list by the cgroup.
141 */
142 struct list_head list;
143 /*
Tejun Heofba94802013-11-22 18:20:43 -0500144 * register_event() callback will be used to add new userspace
145 * waiter for changes related to this event. Use eventfd_signal()
146 * on eventfd to send notification to userspace.
147 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500148 int (*register_event)(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -0500149 struct eventfd_ctx *eventfd, const char *args);
Tejun Heofba94802013-11-22 18:20:43 -0500150 /*
151 * unregister_event() callback will be called when userspace closes
152 * the eventfd or on cgroup removing. This callback must be set,
153 * if you want provide notification functionality.
154 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500155 void (*unregister_event)(struct mem_cgroup *memcg,
Tejun Heofba94802013-11-22 18:20:43 -0500156 struct eventfd_ctx *eventfd);
157 /*
Tejun Heo79bd9812013-11-22 18:20:42 -0500158 * All fields below needed to unregister event when
159 * userspace closes eventfd.
160 */
161 poll_table pt;
162 wait_queue_head_t *wqh;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200163 wait_queue_entry_t wait;
Tejun Heo79bd9812013-11-22 18:20:42 -0500164 struct work_struct remove;
165};
166
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700167static void mem_cgroup_threshold(struct mem_cgroup *memcg);
168static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800169
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800170/* Stuffs for move charges at task migration. */
171/*
Johannes Weiner1dfab5a2015-02-11 15:26:09 -0800172 * Types of charges to be moved.
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800173 */
Johannes Weiner1dfab5a2015-02-11 15:26:09 -0800174#define MOVE_ANON 0x1U
175#define MOVE_FILE 0x2U
176#define MOVE_MASK (MOVE_ANON | MOVE_FILE)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800177
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800178/* "mc" and its members are protected by cgroup_mutex */
179static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800180 spinlock_t lock; /* for from, to */
Tejun Heo264a0ae2016-04-21 19:09:02 -0400181 struct mm_struct *mm;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800182 struct mem_cgroup *from;
183 struct mem_cgroup *to;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -0800184 unsigned long flags;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800185 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800186 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800187 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800188 struct task_struct *moving_task; /* a task moving charges */
189 wait_queue_head_t waitq; /* a waitq for other context */
190} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700191 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800192 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
193};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800194
Balbir Singh4e416952009-09-23 15:56:39 -0700195/*
196 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
197 * limit reclaim to prevent infinite loops, if they ever occur.
198 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700199#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700200#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700201
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800202enum charge_type {
203 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700204 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800205 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700206 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700207 NR_CHARGE_TYPE,
208};
209
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800210/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800211enum res_type {
212 _MEM,
213 _MEMSWAP,
214 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800215 _KMEM,
Vladimir Davydovd55f90b2016-01-20 15:02:44 -0800216 _TCP,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800217};
218
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700219#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
220#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800221#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700222/* Used for OOM nofiier */
223#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800224
Kirill Tkhaib05706f2018-08-17 15:47:33 -0700225/*
226 * Iteration constructs for visiting all cgroups (under a tree). If
227 * loops are exited prematurely (break), mem_cgroup_iter_break() must
228 * be used for reference counting.
229 */
230#define for_each_mem_cgroup_tree(iter, root) \
231 for (iter = mem_cgroup_iter(root, NULL, NULL); \
232 iter != NULL; \
233 iter = mem_cgroup_iter(root, iter, NULL))
234
235#define for_each_mem_cgroup(iter) \
236 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
237 iter != NULL; \
238 iter = mem_cgroup_iter(NULL, iter, NULL))
239
Tetsuo Handa7775fac2019-03-05 15:46:47 -0800240static inline bool should_force_charge(void)
241{
242 return tsk_is_oom_victim(current) || fatal_signal_pending(current) ||
243 (current->flags & PF_EXITING);
244}
245
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700246/* Some nice accessors for the vmpressure. */
247struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
248{
249 if (!memcg)
250 memcg = root_mem_cgroup;
251 return &memcg->vmpressure;
252}
253
254struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr)
255{
256 return &container_of(vmpr, struct mem_cgroup, vmpressure)->css;
257}
258
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700259#ifdef CONFIG_MEMCG_KMEM
Roman Gushchinbf4f0592020-08-06 23:20:49 -0700260extern spinlock_t css_set_lock;
261
262static void obj_cgroup_release(struct percpu_ref *ref)
263{
264 struct obj_cgroup *objcg = container_of(ref, struct obj_cgroup, refcnt);
265 struct mem_cgroup *memcg;
266 unsigned int nr_bytes;
267 unsigned int nr_pages;
268 unsigned long flags;
269
270 /*
271 * At this point all allocated objects are freed, and
272 * objcg->nr_charged_bytes can't have an arbitrary byte value.
273 * However, it can be PAGE_SIZE or (x * PAGE_SIZE).
274 *
275 * The following sequence can lead to it:
276 * 1) CPU0: objcg == stock->cached_objcg
277 * 2) CPU1: we do a small allocation (e.g. 92 bytes),
278 * PAGE_SIZE bytes are charged
279 * 3) CPU1: a process from another memcg is allocating something,
280 * the stock if flushed,
281 * objcg->nr_charged_bytes = PAGE_SIZE - 92
282 * 5) CPU0: we do release this object,
283 * 92 bytes are added to stock->nr_bytes
284 * 6) CPU0: stock is flushed,
285 * 92 bytes are added to objcg->nr_charged_bytes
286 *
287 * In the result, nr_charged_bytes == PAGE_SIZE.
288 * This page will be uncharged in obj_cgroup_release().
289 */
290 nr_bytes = atomic_read(&objcg->nr_charged_bytes);
291 WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1));
292 nr_pages = nr_bytes >> PAGE_SHIFT;
293
294 spin_lock_irqsave(&css_set_lock, flags);
295 memcg = obj_cgroup_memcg(objcg);
296 if (nr_pages)
297 __memcg_kmem_uncharge(memcg, nr_pages);
298 list_del(&objcg->list);
299 mem_cgroup_put(memcg);
300 spin_unlock_irqrestore(&css_set_lock, flags);
301
302 percpu_ref_exit(ref);
303 kfree_rcu(objcg, rcu);
304}
305
306static struct obj_cgroup *obj_cgroup_alloc(void)
307{
308 struct obj_cgroup *objcg;
309 int ret;
310
311 objcg = kzalloc(sizeof(struct obj_cgroup), GFP_KERNEL);
312 if (!objcg)
313 return NULL;
314
315 ret = percpu_ref_init(&objcg->refcnt, obj_cgroup_release, 0,
316 GFP_KERNEL);
317 if (ret) {
318 kfree(objcg);
319 return NULL;
320 }
321 INIT_LIST_HEAD(&objcg->list);
322 return objcg;
323}
324
325static void memcg_reparent_objcgs(struct mem_cgroup *memcg,
326 struct mem_cgroup *parent)
327{
328 struct obj_cgroup *objcg, *iter;
329
330 objcg = rcu_replace_pointer(memcg->objcg, NULL, true);
331
332 spin_lock_irq(&css_set_lock);
333
334 /* Move active objcg to the parent's list */
335 xchg(&objcg->memcg, parent);
336 css_get(&parent->css);
337 list_add(&objcg->list, &parent->objcg_list);
338
339 /* Move already reparented objcgs to the parent's list */
340 list_for_each_entry(iter, &memcg->objcg_list, list) {
341 css_get(&parent->css);
342 xchg(&iter->memcg, parent);
343 css_put(&memcg->css);
344 }
345 list_splice(&memcg->objcg_list, &parent->objcg_list);
346
347 spin_unlock_irq(&css_set_lock);
348
349 percpu_ref_kill(&objcg->refcnt);
350}
351
Glauber Costa55007d82012-12-18 14:22:38 -0800352/*
Roman Gushchin98556092020-08-06 23:21:10 -0700353 * This will be used as a shrinker list's index.
Li Zefanb8627832013-09-23 16:56:47 +0800354 * The main reason for not using cgroup id for this:
355 * this works better in sparse environments, where we have a lot of memcgs,
356 * but only a few kmem-limited. Or also, if we have, for instance, 200
357 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
358 * 200 entry array for that.
Glauber Costa55007d82012-12-18 14:22:38 -0800359 *
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -0800360 * The current size of the caches array is stored in memcg_nr_cache_ids. It
361 * will double each time we have to increase it.
Glauber Costa55007d82012-12-18 14:22:38 -0800362 */
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -0800363static DEFINE_IDA(memcg_cache_ida);
364int memcg_nr_cache_ids;
Glauber Costa749c5412012-12-18 14:23:01 -0800365
Vladimir Davydov05257a12015-02-12 14:59:01 -0800366/* Protects memcg_nr_cache_ids */
367static DECLARE_RWSEM(memcg_cache_ids_sem);
368
369void memcg_get_cache_ids(void)
370{
371 down_read(&memcg_cache_ids_sem);
372}
373
374void memcg_put_cache_ids(void)
375{
376 up_read(&memcg_cache_ids_sem);
377}
378
Glauber Costa55007d82012-12-18 14:22:38 -0800379/*
380 * MIN_SIZE is different than 1, because we would like to avoid going through
381 * the alloc/free process all the time. In a small machine, 4 kmem-limited
382 * cgroups is a reasonable guess. In the future, it could be a parameter or
383 * tunable, but that is strictly not necessary.
384 *
Li Zefanb8627832013-09-23 16:56:47 +0800385 * MAX_SIZE should be as large as the number of cgrp_ids. Ideally, we could get
Glauber Costa55007d82012-12-18 14:22:38 -0800386 * this constant directly from cgroup, but it is understandable that this is
387 * better kept as an internal representation in cgroup.c. In any case, the
Li Zefanb8627832013-09-23 16:56:47 +0800388 * cgrp_id space is not getting any smaller, and we don't have to necessarily
Glauber Costa55007d82012-12-18 14:22:38 -0800389 * increase ours as well if it increases.
390 */
391#define MEMCG_CACHES_MIN_SIZE 4
Li Zefanb8627832013-09-23 16:56:47 +0800392#define MEMCG_CACHES_MAX_SIZE MEM_CGROUP_ID_MAX
Glauber Costa55007d82012-12-18 14:22:38 -0800393
Glauber Costad7f25f82012-12-18 14:22:40 -0800394/*
395 * A lot of the calls to the cache allocation functions are expected to be
Roman Gushchin272911a2020-08-06 23:21:17 -0700396 * inlined by the compiler. Since the calls to memcg_slab_pre_alloc_hook() are
Glauber Costad7f25f82012-12-18 14:22:40 -0800397 * conditional to this static branch, we'll have to allow modules that does
398 * kmem_cache_alloc and the such to see this symbol as well
399 */
Johannes Weineref129472016-01-14 15:21:34 -0800400DEFINE_STATIC_KEY_FALSE(memcg_kmem_enabled_key);
Glauber Costad7f25f82012-12-18 14:22:40 -0800401EXPORT_SYMBOL(memcg_kmem_enabled_key);
Yang Shi0a432dc2019-09-23 15:38:12 -0700402#endif
Tejun Heo17cc4df2017-02-22 15:41:36 -0800403
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700404static int memcg_shrinker_map_size;
405static DEFINE_MUTEX(memcg_shrinker_map_mutex);
406
407static void memcg_free_shrinker_map_rcu(struct rcu_head *head)
408{
409 kvfree(container_of(head, struct memcg_shrinker_map, rcu));
410}
411
412static int memcg_expand_one_shrinker_map(struct mem_cgroup *memcg,
413 int size, int old_size)
414{
415 struct memcg_shrinker_map *new, *old;
416 int nid;
417
418 lockdep_assert_held(&memcg_shrinker_map_mutex);
419
420 for_each_node(nid) {
421 old = rcu_dereference_protected(
422 mem_cgroup_nodeinfo(memcg, nid)->shrinker_map, true);
423 /* Not yet online memcg */
424 if (!old)
425 return 0;
426
Kirill Tkhai86daf942020-04-01 21:06:33 -0700427 new = kvmalloc_node(sizeof(*new) + size, GFP_KERNEL, nid);
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700428 if (!new)
429 return -ENOMEM;
430
431 /* Set all old bits, clear all new bits */
432 memset(new->map, (int)0xff, old_size);
433 memset((void *)new->map + old_size, 0, size - old_size);
434
435 rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_map, new);
436 call_rcu(&old->rcu, memcg_free_shrinker_map_rcu);
437 }
438
439 return 0;
440}
441
442static void memcg_free_shrinker_maps(struct mem_cgroup *memcg)
443{
444 struct mem_cgroup_per_node *pn;
445 struct memcg_shrinker_map *map;
446 int nid;
447
448 if (mem_cgroup_is_root(memcg))
449 return;
450
451 for_each_node(nid) {
452 pn = mem_cgroup_nodeinfo(memcg, nid);
453 map = rcu_dereference_protected(pn->shrinker_map, true);
454 if (map)
455 kvfree(map);
456 rcu_assign_pointer(pn->shrinker_map, NULL);
457 }
458}
459
460static int memcg_alloc_shrinker_maps(struct mem_cgroup *memcg)
461{
462 struct memcg_shrinker_map *map;
463 int nid, size, ret = 0;
464
465 if (mem_cgroup_is_root(memcg))
466 return 0;
467
468 mutex_lock(&memcg_shrinker_map_mutex);
469 size = memcg_shrinker_map_size;
470 for_each_node(nid) {
Kirill Tkhai86daf942020-04-01 21:06:33 -0700471 map = kvzalloc_node(sizeof(*map) + size, GFP_KERNEL, nid);
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700472 if (!map) {
473 memcg_free_shrinker_maps(memcg);
474 ret = -ENOMEM;
475 break;
476 }
477 rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_map, map);
478 }
479 mutex_unlock(&memcg_shrinker_map_mutex);
480
481 return ret;
482}
483
484int memcg_expand_shrinker_maps(int new_id)
485{
486 int size, old_size, ret = 0;
487 struct mem_cgroup *memcg;
488
489 size = DIV_ROUND_UP(new_id + 1, BITS_PER_LONG) * sizeof(unsigned long);
490 old_size = memcg_shrinker_map_size;
491 if (size <= old_size)
492 return 0;
493
494 mutex_lock(&memcg_shrinker_map_mutex);
495 if (!root_mem_cgroup)
496 goto unlock;
497
498 for_each_mem_cgroup(memcg) {
499 if (mem_cgroup_is_root(memcg))
500 continue;
501 ret = memcg_expand_one_shrinker_map(memcg, size, old_size);
Vasily Averin75866af2020-02-20 20:04:18 -0800502 if (ret) {
503 mem_cgroup_iter_break(NULL, memcg);
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700504 goto unlock;
Vasily Averin75866af2020-02-20 20:04:18 -0800505 }
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700506 }
507unlock:
508 if (!ret)
509 memcg_shrinker_map_size = size;
510 mutex_unlock(&memcg_shrinker_map_mutex);
511 return ret;
512}
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700513
514void memcg_set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id)
515{
516 if (shrinker_id >= 0 && memcg && !mem_cgroup_is_root(memcg)) {
517 struct memcg_shrinker_map *map;
518
519 rcu_read_lock();
520 map = rcu_dereference(memcg->nodeinfo[nid]->shrinker_map);
Kirill Tkhaif90280d2018-08-17 15:48:25 -0700521 /* Pairs with smp mb in shrink_slab() */
522 smp_mb__before_atomic();
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700523 set_bit(shrinker_id, map->map);
524 rcu_read_unlock();
525 }
526}
527
Tejun Heoad7fa852015-05-27 20:00:02 -0400528/**
529 * mem_cgroup_css_from_page - css of the memcg associated with a page
530 * @page: page of interest
531 *
532 * If memcg is bound to the default hierarchy, css of the memcg associated
533 * with @page is returned. The returned css remains associated with @page
534 * until it is released.
535 *
536 * If memcg is bound to a traditional hierarchy, the css of root_mem_cgroup
537 * is returned.
Tejun Heoad7fa852015-05-27 20:00:02 -0400538 */
539struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page)
540{
541 struct mem_cgroup *memcg;
542
Tejun Heoad7fa852015-05-27 20:00:02 -0400543 memcg = page->mem_cgroup;
544
Tejun Heo9e10a132015-09-18 11:56:28 -0400545 if (!memcg || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
Tejun Heoad7fa852015-05-27 20:00:02 -0400546 memcg = root_mem_cgroup;
547
Tejun Heoad7fa852015-05-27 20:00:02 -0400548 return &memcg->css;
549}
550
Vladimir Davydov2fc04522015-09-09 15:35:28 -0700551/**
552 * page_cgroup_ino - return inode number of the memcg a page is charged to
553 * @page: the page
554 *
555 * Look up the closest online ancestor of the memory cgroup @page is charged to
556 * and return its inode number or 0 if @page is not charged to any cgroup. It
557 * is safe to call this function without holding a reference to @page.
558 *
559 * Note, this function is inherently racy, because there is nothing to prevent
560 * the cgroup inode from getting torn down and potentially reallocated a moment
561 * after page_cgroup_ino() returns, so it only should be used by callers that
562 * do not care (such as procfs interfaces).
563 */
564ino_t page_cgroup_ino(struct page *page)
565{
566 struct mem_cgroup *memcg;
567 unsigned long ino = 0;
568
569 rcu_read_lock();
Roman Gushchin98556092020-08-06 23:21:10 -0700570 memcg = page->mem_cgroup;
Roman Gushchin286e04b2020-08-06 23:20:52 -0700571
Roman Gushchin98556092020-08-06 23:21:10 -0700572 /*
573 * The lowest bit set means that memcg isn't a valid
574 * memcg pointer, but a obj_cgroups pointer.
575 * In this case the page is shared and doesn't belong
576 * to any specific memory cgroup.
577 */
578 if ((unsigned long) memcg & 0x1UL)
579 memcg = NULL;
Roman Gushchin286e04b2020-08-06 23:20:52 -0700580
Vladimir Davydov2fc04522015-09-09 15:35:28 -0700581 while (memcg && !(memcg->css.flags & CSS_ONLINE))
582 memcg = parent_mem_cgroup(memcg);
583 if (memcg)
584 ino = cgroup_ino(memcg->css.cgroup);
585 rcu_read_unlock();
586 return ino;
587}
588
Mel Gormanef8f2322016-07-28 15:46:05 -0700589static struct mem_cgroup_per_node *
590mem_cgroup_page_nodeinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700591{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700592 int nid = page_to_nid(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700593
Mel Gormanef8f2322016-07-28 15:46:05 -0700594 return memcg->nodeinfo[nid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700595}
596
Mel Gormanef8f2322016-07-28 15:46:05 -0700597static struct mem_cgroup_tree_per_node *
598soft_limit_tree_node(int nid)
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700599{
Mel Gormanef8f2322016-07-28 15:46:05 -0700600 return soft_limit_tree.rb_tree_per_node[nid];
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700601}
602
Mel Gormanef8f2322016-07-28 15:46:05 -0700603static struct mem_cgroup_tree_per_node *
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700604soft_limit_tree_from_page(struct page *page)
605{
606 int nid = page_to_nid(page);
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700607
Mel Gormanef8f2322016-07-28 15:46:05 -0700608 return soft_limit_tree.rb_tree_per_node[nid];
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700609}
610
Mel Gormanef8f2322016-07-28 15:46:05 -0700611static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_node *mz,
612 struct mem_cgroup_tree_per_node *mctz,
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800613 unsigned long new_usage_in_excess)
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700614{
615 struct rb_node **p = &mctz->rb_root.rb_node;
616 struct rb_node *parent = NULL;
Mel Gormanef8f2322016-07-28 15:46:05 -0700617 struct mem_cgroup_per_node *mz_node;
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700618 bool rightmost = true;
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700619
620 if (mz->on_tree)
621 return;
622
623 mz->usage_in_excess = new_usage_in_excess;
624 if (!mz->usage_in_excess)
625 return;
626 while (*p) {
627 parent = *p;
Mel Gormanef8f2322016-07-28 15:46:05 -0700628 mz_node = rb_entry(parent, struct mem_cgroup_per_node,
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700629 tree_node);
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700630 if (mz->usage_in_excess < mz_node->usage_in_excess) {
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700631 p = &(*p)->rb_left;
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700632 rightmost = false;
633 }
634
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700635 /*
636 * We can't avoid mem cgroups that are over their soft
637 * limit by the same amount
638 */
639 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
640 p = &(*p)->rb_right;
641 }
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700642
643 if (rightmost)
644 mctz->rb_rightmost = &mz->tree_node;
645
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700646 rb_link_node(&mz->tree_node, parent, p);
647 rb_insert_color(&mz->tree_node, &mctz->rb_root);
648 mz->on_tree = true;
649}
650
Mel Gormanef8f2322016-07-28 15:46:05 -0700651static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz,
652 struct mem_cgroup_tree_per_node *mctz)
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700653{
654 if (!mz->on_tree)
655 return;
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700656
657 if (&mz->tree_node == mctz->rb_rightmost)
658 mctz->rb_rightmost = rb_prev(&mz->tree_node);
659
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700660 rb_erase(&mz->tree_node, &mctz->rb_root);
661 mz->on_tree = false;
662}
663
Mel Gormanef8f2322016-07-28 15:46:05 -0700664static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz,
665 struct mem_cgroup_tree_per_node *mctz)
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700666{
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700667 unsigned long flags;
668
669 spin_lock_irqsave(&mctz->lock, flags);
Johannes Weinercf2c8122014-06-06 14:38:21 -0700670 __mem_cgroup_remove_exceeded(mz, mctz);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700671 spin_unlock_irqrestore(&mctz->lock, flags);
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700672}
673
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800674static unsigned long soft_limit_excess(struct mem_cgroup *memcg)
675{
676 unsigned long nr_pages = page_counter_read(&memcg->memory);
Jason Low4db0c3c2015-04-15 16:14:08 -0700677 unsigned long soft_limit = READ_ONCE(memcg->soft_limit);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800678 unsigned long excess = 0;
679
680 if (nr_pages > soft_limit)
681 excess = nr_pages - soft_limit;
682
683 return excess;
684}
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700685
686static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
687{
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800688 unsigned long excess;
Mel Gormanef8f2322016-07-28 15:46:05 -0700689 struct mem_cgroup_per_node *mz;
690 struct mem_cgroup_tree_per_node *mctz;
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700691
Jianyu Zhane2318752014-06-06 14:38:20 -0700692 mctz = soft_limit_tree_from_page(page);
Laurent Dufourbfc72282017-03-09 16:17:06 -0800693 if (!mctz)
694 return;
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700695 /*
696 * Necessary to update all ancestors when hierarchy is used.
697 * because their event counter is not touched.
698 */
699 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
Mel Gormanef8f2322016-07-28 15:46:05 -0700700 mz = mem_cgroup_page_nodeinfo(memcg, page);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800701 excess = soft_limit_excess(memcg);
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700702 /*
703 * We have to update the tree if mz is on RB-tree or
704 * mem is over its softlimit.
705 */
706 if (excess || mz->on_tree) {
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700707 unsigned long flags;
708
709 spin_lock_irqsave(&mctz->lock, flags);
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700710 /* if on-tree, remove it */
711 if (mz->on_tree)
Johannes Weinercf2c8122014-06-06 14:38:21 -0700712 __mem_cgroup_remove_exceeded(mz, mctz);
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700713 /*
714 * Insert again. mz->usage_in_excess will be updated.
715 * If excess is 0, no tree ops.
716 */
Johannes Weinercf2c8122014-06-06 14:38:21 -0700717 __mem_cgroup_insert_exceeded(mz, mctz, excess);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700718 spin_unlock_irqrestore(&mctz->lock, flags);
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700719 }
720 }
721}
722
723static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
724{
Mel Gormanef8f2322016-07-28 15:46:05 -0700725 struct mem_cgroup_tree_per_node *mctz;
726 struct mem_cgroup_per_node *mz;
727 int nid;
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700728
Jianyu Zhane2318752014-06-06 14:38:20 -0700729 for_each_node(nid) {
Mel Gormanef8f2322016-07-28 15:46:05 -0700730 mz = mem_cgroup_nodeinfo(memcg, nid);
731 mctz = soft_limit_tree_node(nid);
Laurent Dufourbfc72282017-03-09 16:17:06 -0800732 if (mctz)
733 mem_cgroup_remove_exceeded(mz, mctz);
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700734 }
735}
736
Mel Gormanef8f2322016-07-28 15:46:05 -0700737static struct mem_cgroup_per_node *
738__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz)
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700739{
Mel Gormanef8f2322016-07-28 15:46:05 -0700740 struct mem_cgroup_per_node *mz;
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700741
742retry:
743 mz = NULL;
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700744 if (!mctz->rb_rightmost)
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700745 goto done; /* Nothing to reclaim from */
746
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700747 mz = rb_entry(mctz->rb_rightmost,
748 struct mem_cgroup_per_node, tree_node);
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700749 /*
750 * Remove the node now but someone else can add it back,
751 * we will to add it back at the end of reclaim to its correct
752 * position in the tree.
753 */
Johannes Weinercf2c8122014-06-06 14:38:21 -0700754 __mem_cgroup_remove_exceeded(mz, mctz);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800755 if (!soft_limit_excess(mz->memcg) ||
Shakeel Butt8965aa22020-04-01 21:07:10 -0700756 !css_tryget(&mz->memcg->css))
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700757 goto retry;
758done:
759 return mz;
760}
761
Mel Gormanef8f2322016-07-28 15:46:05 -0700762static struct mem_cgroup_per_node *
763mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz)
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700764{
Mel Gormanef8f2322016-07-28 15:46:05 -0700765 struct mem_cgroup_per_node *mz;
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700766
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700767 spin_lock_irq(&mctz->lock);
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700768 mz = __mem_cgroup_largest_soft_limit_node(mctz);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700769 spin_unlock_irq(&mctz->lock);
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700770 return mz;
771}
772
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700773/**
774 * __mod_memcg_state - update cgroup memory statistics
775 * @memcg: the memory cgroup
776 * @idx: the stat item - can be enum memcg_stat_item or enum node_stat_item
777 * @val: delta to add to the counter, can be negative
778 */
779void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val)
780{
Roman Gushchinea426c22020-08-06 23:20:35 -0700781 long x, threshold = MEMCG_CHARGE_BATCH;
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700782
783 if (mem_cgroup_disabled())
784 return;
785
Roman Gushchinea426c22020-08-06 23:20:35 -0700786 if (vmstat_item_in_bytes(idx))
787 threshold <<= PAGE_SHIFT;
788
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700789 x = val + __this_cpu_read(memcg->vmstats_percpu->stat[idx]);
Roman Gushchinea426c22020-08-06 23:20:35 -0700790 if (unlikely(abs(x) > threshold)) {
Johannes Weiner42a30032019-05-14 15:47:12 -0700791 struct mem_cgroup *mi;
792
Yafang Shao766a4c12019-07-16 16:26:06 -0700793 /*
794 * Batch local counters to keep them in sync with
795 * the hierarchical ones.
796 */
797 __this_cpu_add(memcg->vmstats_local->stat[idx], x);
Johannes Weiner42a30032019-05-14 15:47:12 -0700798 for (mi = memcg; mi; mi = parent_mem_cgroup(mi))
799 atomic_long_add(x, &mi->vmstats[idx]);
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700800 x = 0;
801 }
802 __this_cpu_write(memcg->vmstats_percpu->stat[idx], x);
803}
804
Johannes Weiner42a30032019-05-14 15:47:12 -0700805static struct mem_cgroup_per_node *
806parent_nodeinfo(struct mem_cgroup_per_node *pn, int nid)
807{
808 struct mem_cgroup *parent;
809
810 parent = parent_mem_cgroup(pn->memcg);
811 if (!parent)
812 return NULL;
813 return mem_cgroup_nodeinfo(parent, nid);
814}
815
Roman Gushchineedc4e52020-08-06 23:20:32 -0700816void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
817 int val)
818{
819 struct mem_cgroup_per_node *pn;
820 struct mem_cgroup *memcg;
Roman Gushchinea426c22020-08-06 23:20:35 -0700821 long x, threshold = MEMCG_CHARGE_BATCH;
Roman Gushchineedc4e52020-08-06 23:20:32 -0700822
823 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
824 memcg = pn->memcg;
825
826 /* Update memcg */
827 __mod_memcg_state(memcg, idx, val);
828
829 /* Update lruvec */
830 __this_cpu_add(pn->lruvec_stat_local->count[idx], val);
831
Roman Gushchinea426c22020-08-06 23:20:35 -0700832 if (vmstat_item_in_bytes(idx))
833 threshold <<= PAGE_SHIFT;
834
Roman Gushchineedc4e52020-08-06 23:20:32 -0700835 x = val + __this_cpu_read(pn->lruvec_stat_cpu->count[idx]);
Roman Gushchinea426c22020-08-06 23:20:35 -0700836 if (unlikely(abs(x) > threshold)) {
Roman Gushchineedc4e52020-08-06 23:20:32 -0700837 pg_data_t *pgdat = lruvec_pgdat(lruvec);
838 struct mem_cgroup_per_node *pi;
839
840 for (pi = pn; pi; pi = parent_nodeinfo(pi, pgdat->node_id))
841 atomic_long_add(x, &pi->lruvec_stat[idx]);
842 x = 0;
843 }
844 __this_cpu_write(pn->lruvec_stat_cpu->count[idx], x);
845}
846
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700847/**
848 * __mod_lruvec_state - update lruvec memory statistics
849 * @lruvec: the lruvec
850 * @idx: the stat item
851 * @val: delta to add to the counter, can be negative
852 *
853 * The lruvec is the intersection of the NUMA node and a cgroup. This
854 * function updates the all three counters that are affected by a
855 * change of state at this level: per-node, per-cgroup, per-lruvec.
856 */
857void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
858 int val)
859{
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700860 /* Update node */
Roman Gushchineedc4e52020-08-06 23:20:32 -0700861 __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700862
Roman Gushchineedc4e52020-08-06 23:20:32 -0700863 /* Update memcg and lruvec */
864 if (!mem_cgroup_disabled())
865 __mod_memcg_lruvec_state(lruvec, idx, val);
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700866}
867
Roman Gushchinec9f0232019-08-13 15:37:41 -0700868void __mod_lruvec_slab_state(void *p, enum node_stat_item idx, int val)
869{
Roman Gushchin4f103c62020-04-01 21:06:36 -0700870 pg_data_t *pgdat = page_pgdat(virt_to_page(p));
Roman Gushchinec9f0232019-08-13 15:37:41 -0700871 struct mem_cgroup *memcg;
872 struct lruvec *lruvec;
873
874 rcu_read_lock();
Roman Gushchin4f103c62020-04-01 21:06:36 -0700875 memcg = mem_cgroup_from_obj(p);
Roman Gushchinec9f0232019-08-13 15:37:41 -0700876
877 /* Untracked pages have no memcg, no lruvec. Update only the node */
878 if (!memcg || memcg == root_mem_cgroup) {
879 __mod_node_page_state(pgdat, idx, val);
880 } else {
Johannes Weiner867e5e12019-11-30 17:55:34 -0800881 lruvec = mem_cgroup_lruvec(memcg, pgdat);
Roman Gushchinec9f0232019-08-13 15:37:41 -0700882 __mod_lruvec_state(lruvec, idx, val);
883 }
884 rcu_read_unlock();
885}
886
Roman Gushchin8380ce42020-03-28 19:17:25 -0700887void mod_memcg_obj_state(void *p, int idx, int val)
888{
889 struct mem_cgroup *memcg;
890
891 rcu_read_lock();
892 memcg = mem_cgroup_from_obj(p);
893 if (memcg)
894 mod_memcg_state(memcg, idx, val);
895 rcu_read_unlock();
896}
897
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700898/**
899 * __count_memcg_events - account VM events in a cgroup
900 * @memcg: the memory cgroup
901 * @idx: the event item
902 * @count: the number of events that occured
903 */
904void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
905 unsigned long count)
906{
907 unsigned long x;
908
909 if (mem_cgroup_disabled())
910 return;
911
912 x = count + __this_cpu_read(memcg->vmstats_percpu->events[idx]);
913 if (unlikely(x > MEMCG_CHARGE_BATCH)) {
Johannes Weiner42a30032019-05-14 15:47:12 -0700914 struct mem_cgroup *mi;
915
Yafang Shao766a4c12019-07-16 16:26:06 -0700916 /*
917 * Batch local counters to keep them in sync with
918 * the hierarchical ones.
919 */
920 __this_cpu_add(memcg->vmstats_local->events[idx], x);
Johannes Weiner42a30032019-05-14 15:47:12 -0700921 for (mi = memcg; mi; mi = parent_mem_cgroup(mi))
922 atomic_long_add(x, &mi->vmevents[idx]);
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700923 x = 0;
924 }
925 __this_cpu_write(memcg->vmstats_percpu->events[idx], x);
926}
927
Johannes Weiner42a30032019-05-14 15:47:12 -0700928static unsigned long memcg_events(struct mem_cgroup *memcg, int event)
Johannes Weinere9f89742011-03-23 16:42:37 -0700929{
Chris Down871789d2019-05-14 15:46:57 -0700930 return atomic_long_read(&memcg->vmevents[event]);
Johannes Weinere9f89742011-03-23 16:42:37 -0700931}
932
Johannes Weiner42a30032019-05-14 15:47:12 -0700933static unsigned long memcg_events_local(struct mem_cgroup *memcg, int event)
934{
Johannes Weiner815744d2019-06-13 15:55:46 -0700935 long x = 0;
936 int cpu;
937
938 for_each_possible_cpu(cpu)
939 x += per_cpu(memcg->vmstats_local->events[event], cpu);
940 return x;
Johannes Weiner42a30032019-05-14 15:47:12 -0700941}
942
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700943static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
David Rientjesb070e652013-05-07 16:18:09 -0700944 struct page *page,
Johannes Weiner3fba69a2020-06-03 16:01:31 -0700945 int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800946{
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800947 /* pagein of a big page is an event. So, ignore page size */
948 if (nr_pages > 0)
Johannes Weinerc9019e92018-01-31 16:16:37 -0800949 __count_memcg_events(memcg, PGPGIN, 1);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800950 else {
Johannes Weinerc9019e92018-01-31 16:16:37 -0800951 __count_memcg_events(memcg, PGPGOUT, 1);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800952 nr_pages = -nr_pages; /* for event */
953 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800954
Chris Down871789d2019-05-14 15:46:57 -0700955 __this_cpu_add(memcg->vmstats_percpu->nr_page_events, nr_pages);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800956}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800957
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800958static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
959 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800960{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700961 unsigned long val, next;
962
Chris Down871789d2019-05-14 15:46:57 -0700963 val = __this_cpu_read(memcg->vmstats_percpu->nr_page_events);
964 next = __this_cpu_read(memcg->vmstats_percpu->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700965 /* from time_after() in jiffies.h */
Michal Hocko6a1a8b82017-07-10 15:48:53 -0700966 if ((long)(next - val) < 0) {
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800967 switch (target) {
968 case MEM_CGROUP_TARGET_THRESH:
969 next = val + THRESHOLDS_EVENTS_TARGET;
970 break;
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700971 case MEM_CGROUP_TARGET_SOFTLIMIT:
972 next = val + SOFTLIMIT_EVENTS_TARGET;
973 break;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800974 default:
975 break;
976 }
Chris Down871789d2019-05-14 15:46:57 -0700977 __this_cpu_write(memcg->vmstats_percpu->targets[target], next);
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800978 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700979 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800980 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800981}
982
983/*
984 * Check events in order.
985 *
986 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700987static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800988{
989 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800990 if (unlikely(mem_cgroup_event_ratelimit(memcg,
991 MEM_CGROUP_TARGET_THRESH))) {
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700992 bool do_softlimit;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800993
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700994 do_softlimit = mem_cgroup_event_ratelimit(memcg,
995 MEM_CGROUP_TARGET_SOFTLIMIT);
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800996 mem_cgroup_threshold(memcg);
Andrew Mortonbb4cc1a82013-09-24 15:27:40 -0700997 if (unlikely(do_softlimit))
998 mem_cgroup_update_tree(memcg, page);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700999 }
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001000}
1001
Balbir Singhcf475ad2008-04-29 01:00:16 -07001002struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001003{
Balbir Singh31a78f22008-09-28 23:09:31 +01001004 /*
1005 * mm_update_next_owner() may clear mm->owner to NULL
1006 * if it races with swapoff, page migration, etc.
1007 * So this can be called with p == NULL.
1008 */
1009 if (unlikely(!p))
1010 return NULL;
1011
Tejun Heo073219e2014-02-08 10:36:58 -05001012 return mem_cgroup_from_css(task_css(p, memory_cgrp_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001013}
Michal Hocko33398cf2015-09-08 15:01:02 -07001014EXPORT_SYMBOL(mem_cgroup_from_task);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001015
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001016/**
1017 * get_mem_cgroup_from_mm: Obtain a reference on given mm_struct's memcg.
1018 * @mm: mm from which memcg should be extracted. It can be NULL.
1019 *
1020 * Obtain a reference on mm->memcg and returns it if successful. Otherwise
1021 * root_mem_cgroup is returned. However if mem_cgroup is disabled, NULL is
1022 * returned.
1023 */
1024struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001025{
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001026 struct mem_cgroup *memcg;
1027
1028 if (mem_cgroup_disabled())
1029 return NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001030
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001031 rcu_read_lock();
1032 do {
Michal Hocko6f6acb02014-05-22 11:54:19 -07001033 /*
1034 * Page cache insertions can happen withou an
1035 * actual mm context, e.g. during disk probing
1036 * on boot, loopback IO, acct() writes etc.
1037 */
1038 if (unlikely(!mm))
Johannes Weinerdf381972014-04-07 15:37:43 -07001039 memcg = root_mem_cgroup;
Michal Hocko6f6acb02014-05-22 11:54:19 -07001040 else {
1041 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1042 if (unlikely(!memcg))
1043 memcg = root_mem_cgroup;
1044 }
Roman Gushchin00d484f2019-11-15 17:34:43 -08001045 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001046 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001047 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001048}
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001049EXPORT_SYMBOL(get_mem_cgroup_from_mm);
1050
1051/**
Shakeel Buttf745c6f2018-08-17 15:46:44 -07001052 * get_mem_cgroup_from_page: Obtain a reference on given page's memcg.
1053 * @page: page from which memcg should be extracted.
1054 *
1055 * Obtain a reference on page->memcg and returns it if successful. Otherwise
1056 * root_mem_cgroup is returned.
1057 */
1058struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
1059{
1060 struct mem_cgroup *memcg = page->mem_cgroup;
1061
1062 if (mem_cgroup_disabled())
1063 return NULL;
1064
1065 rcu_read_lock();
Shakeel Butt8965aa22020-04-01 21:07:10 -07001066 /* Page should not get uncharged and freed memcg under us. */
1067 if (!memcg || WARN_ON_ONCE(!css_tryget(&memcg->css)))
Shakeel Buttf745c6f2018-08-17 15:46:44 -07001068 memcg = root_mem_cgroup;
1069 rcu_read_unlock();
1070 return memcg;
1071}
1072EXPORT_SYMBOL(get_mem_cgroup_from_page);
1073
1074/**
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001075 * If current->active_memcg is non-NULL, do not fallback to current->mm->memcg.
1076 */
1077static __always_inline struct mem_cgroup *get_mem_cgroup_from_current(void)
1078{
1079 if (unlikely(current->active_memcg)) {
Shakeel Butt8965aa22020-04-01 21:07:10 -07001080 struct mem_cgroup *memcg;
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001081
1082 rcu_read_lock();
Shakeel Butt8965aa22020-04-01 21:07:10 -07001083 /* current->active_memcg must hold a ref. */
1084 if (WARN_ON_ONCE(!css_tryget(&current->active_memcg->css)))
1085 memcg = root_mem_cgroup;
1086 else
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001087 memcg = current->active_memcg;
1088 rcu_read_unlock();
1089 return memcg;
1090 }
1091 return get_mem_cgroup_from_mm(current->mm);
1092}
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001093
Johannes Weiner56600482012-01-12 17:17:59 -08001094/**
1095 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1096 * @root: hierarchy root
1097 * @prev: previously returned memcg, NULL on first invocation
1098 * @reclaim: cookie for shared reclaim walks, NULL for full walks
1099 *
1100 * Returns references to children of the hierarchy below @root, or
1101 * @root itself, or %NULL after a full round-trip.
1102 *
1103 * Caller must pass the return value in @prev on subsequent
1104 * invocations for reference counting, or use mem_cgroup_iter_break()
1105 * to cancel a hierarchy walk before the round-trip is complete.
1106 *
Honglei Wangb213b542018-03-28 16:01:12 -07001107 * Reclaimers can specify a node and a priority level in @reclaim to
Johannes Weiner56600482012-01-12 17:17:59 -08001108 * divide up the memcgs in the hierarchy among all concurrent
Honglei Wangb213b542018-03-28 16:01:12 -07001109 * reclaimers operating on the same node and priority.
Johannes Weiner56600482012-01-12 17:17:59 -08001110 */
Andrew Morton694fbc02013-09-24 15:27:37 -07001111struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
Johannes Weiner56600482012-01-12 17:17:59 -08001112 struct mem_cgroup *prev,
Andrew Morton694fbc02013-09-24 15:27:37 -07001113 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001114{
Kees Cook3f649ab2020-06-03 13:09:38 -07001115 struct mem_cgroup_reclaim_iter *iter;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001116 struct cgroup_subsys_state *css = NULL;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001117 struct mem_cgroup *memcg = NULL;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001118 struct mem_cgroup *pos = NULL;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001119
Andrew Morton694fbc02013-09-24 15:27:37 -07001120 if (mem_cgroup_disabled())
1121 return NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001122
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001123 if (!root)
1124 root = root_mem_cgroup;
1125
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001126 if (prev && !reclaim)
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001127 pos = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001128
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001129 if (!root->use_hierarchy && root != root_mem_cgroup) {
1130 if (prev)
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001131 goto out;
Andrew Morton694fbc02013-09-24 15:27:37 -07001132 return root;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001133 }
1134
Michal Hocko542f85f2013-04-29 15:07:15 -07001135 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001136
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001137 if (reclaim) {
Mel Gormanef8f2322016-07-28 15:46:05 -07001138 struct mem_cgroup_per_node *mz;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001139
Mel Gormanef8f2322016-07-28 15:46:05 -07001140 mz = mem_cgroup_nodeinfo(root, reclaim->pgdat->node_id);
Yafang Shao9da83f32019-11-30 17:50:03 -08001141 iter = &mz->iter;
Michal Hocko5f578162013-04-29 15:07:17 -07001142
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001143 if (prev && reclaim->generation != iter->generation)
Michal Hocko542f85f2013-04-29 15:07:15 -07001144 goto out_unlock;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001145
Vladimir Davydov6df38682015-12-29 14:54:10 -08001146 while (1) {
Jason Low4db0c3c2015-04-15 16:14:08 -07001147 pos = READ_ONCE(iter->position);
Vladimir Davydov6df38682015-12-29 14:54:10 -08001148 if (!pos || css_tryget(&pos->css))
1149 break;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001150 /*
Vladimir Davydov6df38682015-12-29 14:54:10 -08001151 * css reference reached zero, so iter->position will
1152 * be cleared by ->css_released. However, we should not
1153 * rely on this happening soon, because ->css_released
1154 * is called from a work queue, and by busy-waiting we
1155 * might block it. So we clear iter->position right
1156 * away.
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001157 */
Vladimir Davydov6df38682015-12-29 14:54:10 -08001158 (void)cmpxchg(&iter->position, pos, NULL);
1159 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001160 }
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001161
1162 if (pos)
1163 css = &pos->css;
1164
1165 for (;;) {
1166 css = css_next_descendant_pre(css, &root->css);
1167 if (!css) {
1168 /*
1169 * Reclaimers share the hierarchy walk, and a
1170 * new one might jump in right at the end of
1171 * the hierarchy - make sure they see at least
1172 * one group and restart from the beginning.
1173 */
1174 if (!prev)
1175 continue;
1176 break;
1177 }
1178
1179 /*
1180 * Verify the css and acquire a reference. The root
1181 * is provided by the caller, so we know it's alive
1182 * and kicking, and don't take an extra reference.
1183 */
1184 memcg = mem_cgroup_from_css(css);
1185
1186 if (css == &root->css)
1187 break;
1188
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08001189 if (css_tryget(css))
1190 break;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001191
1192 memcg = NULL;
1193 }
1194
1195 if (reclaim) {
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001196 /*
Vladimir Davydov6df38682015-12-29 14:54:10 -08001197 * The position could have already been updated by a competing
1198 * thread, so check that the value hasn't changed since we read
1199 * it to avoid reclaiming from the same cgroup twice.
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001200 */
Vladimir Davydov6df38682015-12-29 14:54:10 -08001201 (void)cmpxchg(&iter->position, pos, memcg);
1202
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001203 if (pos)
1204 css_put(&pos->css);
1205
1206 if (!memcg)
1207 iter->generation++;
1208 else if (!prev)
1209 reclaim->generation = iter->generation;
1210 }
1211
Michal Hocko542f85f2013-04-29 15:07:15 -07001212out_unlock:
1213 rcu_read_unlock();
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001214out:
Michal Hockoc40046f2013-04-29 15:07:14 -07001215 if (prev && prev != root)
1216 css_put(&prev->css);
1217
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001218 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001219}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001220
Johannes Weiner56600482012-01-12 17:17:59 -08001221/**
1222 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1223 * @root: hierarchy root
1224 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1225 */
1226void mem_cgroup_iter_break(struct mem_cgroup *root,
1227 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001228{
1229 if (!root)
1230 root = root_mem_cgroup;
1231 if (prev && prev != root)
1232 css_put(&prev->css);
1233}
1234
Miles Chen54a83d62019-08-13 15:37:28 -07001235static void __invalidate_reclaim_iterators(struct mem_cgroup *from,
1236 struct mem_cgroup *dead_memcg)
Vladimir Davydov6df38682015-12-29 14:54:10 -08001237{
Vladimir Davydov6df38682015-12-29 14:54:10 -08001238 struct mem_cgroup_reclaim_iter *iter;
Mel Gormanef8f2322016-07-28 15:46:05 -07001239 struct mem_cgroup_per_node *mz;
1240 int nid;
Vladimir Davydov6df38682015-12-29 14:54:10 -08001241
Miles Chen54a83d62019-08-13 15:37:28 -07001242 for_each_node(nid) {
1243 mz = mem_cgroup_nodeinfo(from, nid);
Yafang Shao9da83f32019-11-30 17:50:03 -08001244 iter = &mz->iter;
1245 cmpxchg(&iter->position, dead_memcg, NULL);
Vladimir Davydov6df38682015-12-29 14:54:10 -08001246 }
1247}
1248
Miles Chen54a83d62019-08-13 15:37:28 -07001249static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg)
1250{
1251 struct mem_cgroup *memcg = dead_memcg;
1252 struct mem_cgroup *last;
1253
1254 do {
1255 __invalidate_reclaim_iterators(memcg, dead_memcg);
1256 last = memcg;
1257 } while ((memcg = parent_mem_cgroup(memcg)));
1258
1259 /*
1260 * When cgruop1 non-hierarchy mode is used,
1261 * parent_mem_cgroup() does not walk all the way up to the
1262 * cgroup root (root_mem_cgroup). So we have to handle
1263 * dead_memcg from cgroup root separately.
1264 */
1265 if (last != root_mem_cgroup)
1266 __invalidate_reclaim_iterators(root_mem_cgroup,
1267 dead_memcg);
1268}
1269
Johannes Weiner925b7672012-01-12 17:18:15 -08001270/**
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001271 * mem_cgroup_scan_tasks - iterate over tasks of a memory cgroup hierarchy
1272 * @memcg: hierarchy root
1273 * @fn: function to call for each task
1274 * @arg: argument passed to @fn
1275 *
1276 * This function iterates over tasks attached to @memcg or to any of its
1277 * descendants and calls @fn for each task. If @fn returns a non-zero
1278 * value, the function breaks the iteration loop and returns the value.
1279 * Otherwise, it will iterate over all tasks and return 0.
1280 *
1281 * This function must not be called for the root memory cgroup.
1282 */
1283int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1284 int (*fn)(struct task_struct *, void *), void *arg)
1285{
1286 struct mem_cgroup *iter;
1287 int ret = 0;
1288
1289 BUG_ON(memcg == root_mem_cgroup);
1290
1291 for_each_mem_cgroup_tree(iter, memcg) {
1292 struct css_task_iter it;
1293 struct task_struct *task;
1294
Tetsuo Handaf168a9a2019-07-11 21:00:20 -07001295 css_task_iter_start(&iter->css, CSS_TASK_ITER_PROCS, &it);
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001296 while (!ret && (task = css_task_iter_next(&it)))
1297 ret = fn(task, arg);
1298 css_task_iter_end(&it);
1299 if (ret) {
1300 mem_cgroup_iter_break(memcg, iter);
1301 break;
1302 }
1303 }
1304 return ret;
1305}
1306
1307/**
Johannes Weinerdfe0e772014-12-10 15:43:43 -08001308 * mem_cgroup_page_lruvec - return lruvec for isolating/putting an LRU page
Johannes Weiner925b7672012-01-12 17:18:15 -08001309 * @page: the page
Mike Rapoportf144c392018-02-06 15:42:16 -08001310 * @pgdat: pgdat of the page
Johannes Weinerdfe0e772014-12-10 15:43:43 -08001311 *
Johannes Weinera0b5b412020-06-03 16:02:27 -07001312 * This function relies on page->mem_cgroup being stable - see the
1313 * access rules in commit_charge().
Minchan Kim3f58a822011-03-22 16:32:53 -07001314 */
Mel Gorman599d0c92016-07-28 15:45:31 -07001315struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct pglist_data *pgdat)
Minchan Kim3f58a822011-03-22 16:32:53 -07001316{
Mel Gormanef8f2322016-07-28 15:46:05 -07001317 struct mem_cgroup_per_node *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001318 struct mem_cgroup *memcg;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001319 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001320
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001321 if (mem_cgroup_disabled()) {
Johannes Weiner867e5e12019-11-30 17:55:34 -08001322 lruvec = &pgdat->__lruvec;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001323 goto out;
1324 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001325
Johannes Weiner1306a852014-12-10 15:44:52 -08001326 memcg = page->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001327 /*
Johannes Weinerdfe0e772014-12-10 15:43:43 -08001328 * Swapcache readahead pages are added to the LRU - and
Johannes Weiner29833312014-12-10 15:44:02 -08001329 * possibly migrated - before they are charged.
Hugh Dickins75121022012-03-05 14:59:18 -08001330 */
Johannes Weiner29833312014-12-10 15:44:02 -08001331 if (!memcg)
1332 memcg = root_mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001333
Mel Gormanef8f2322016-07-28 15:46:05 -07001334 mz = mem_cgroup_page_nodeinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001335 lruvec = &mz->lruvec;
1336out:
1337 /*
1338 * Since a node can be onlined after the mem_cgroup was created,
1339 * we have to be prepared to initialize lruvec->zone here;
1340 * and if offlined then reonlined, we need to reinitialize it.
1341 */
Mel Gorman599d0c92016-07-28 15:45:31 -07001342 if (unlikely(lruvec->pgdat != pgdat))
1343 lruvec->pgdat = pgdat;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001344 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001345}
1346
1347/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001348 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1349 * @lruvec: mem_cgroup per zone lru vector
1350 * @lru: index of lru list the page is sitting on
Michal Hockob4536f0c82017-01-10 16:58:04 -08001351 * @zid: zone id of the accounted pages
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001352 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001353 *
Hugh Dickinsca707232016-05-19 17:12:35 -07001354 * This function must be called under lru_lock, just before a page is added
1355 * to or just after a page is removed from an lru list (that ordering being
1356 * so as to allow it to check that lru_size 0 is consistent with list_empty).
Johannes Weiner925b7672012-01-12 17:18:15 -08001357 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001358void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
Michal Hockob4536f0c82017-01-10 16:58:04 -08001359 int zid, int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001360{
Mel Gormanef8f2322016-07-28 15:46:05 -07001361 struct mem_cgroup_per_node *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001362 unsigned long *lru_size;
Hugh Dickinsca707232016-05-19 17:12:35 -07001363 long size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001364
1365 if (mem_cgroup_disabled())
1366 return;
1367
Mel Gormanef8f2322016-07-28 15:46:05 -07001368 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
Michal Hockob4536f0c82017-01-10 16:58:04 -08001369 lru_size = &mz->lru_zone_size[zid][lru];
Hugh Dickinsca707232016-05-19 17:12:35 -07001370
1371 if (nr_pages < 0)
1372 *lru_size += nr_pages;
1373
1374 size = *lru_size;
Michal Hockob4536f0c82017-01-10 16:58:04 -08001375 if (WARN_ONCE(size < 0,
1376 "%s(%p, %d, %d): lru_size %ld\n",
1377 __func__, lruvec, lru, nr_pages, size)) {
Hugh Dickinsca707232016-05-19 17:12:35 -07001378 VM_BUG_ON(1);
1379 *lru_size = 0;
1380 }
1381
1382 if (nr_pages > 0)
1383 *lru_size += nr_pages;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001384}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001385
Johannes Weiner19942822011-02-01 15:52:43 -08001386/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001387 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001388 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001389 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001390 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001391 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001392 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001393static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001394{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001395 unsigned long margin = 0;
1396 unsigned long count;
1397 unsigned long limit;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001398
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001399 count = page_counter_read(&memcg->memory);
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001400 limit = READ_ONCE(memcg->memory.max);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001401 if (count < limit)
1402 margin = limit - count;
1403
Johannes Weiner7941d212016-01-14 15:21:23 -08001404 if (do_memsw_account()) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001405 count = page_counter_read(&memcg->memsw);
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001406 limit = READ_ONCE(memcg->memsw.max);
Kaixu Xia1c4448e2020-06-01 21:49:36 -07001407 if (count < limit)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001408 margin = min(margin, limit - count);
Li RongQingcbedbac2016-05-27 14:27:43 -07001409 else
1410 margin = 0;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001411 }
1412
1413 return margin;
Johannes Weiner19942822011-02-01 15:52:43 -08001414}
1415
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001416/*
Qiang Huangbdcbb652014-06-04 16:08:21 -07001417 * A routine for checking "mem" is under move_account() or not.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001418 *
Qiang Huangbdcbb652014-06-04 16:08:21 -07001419 * Checking a cgroup is mc.from or mc.to or under hierarchy of
1420 * moving cgroups. This is for waiting at high-memory pressure
1421 * caused by "move".
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001422 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001423static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001424{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001425 struct mem_cgroup *from;
1426 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001427 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001428 /*
1429 * Unlike task_move routines, we access mc.to, mc.from not under
1430 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1431 */
1432 spin_lock(&mc.lock);
1433 from = mc.from;
1434 to = mc.to;
1435 if (!from)
1436 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001437
Johannes Weiner2314b422014-12-10 15:44:33 -08001438 ret = mem_cgroup_is_descendant(from, memcg) ||
1439 mem_cgroup_is_descendant(to, memcg);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001440unlock:
1441 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001442 return ret;
1443}
1444
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001445static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001446{
1447 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001448 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001449 DEFINE_WAIT(wait);
1450 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1451 /* moving charge context might have finished. */
1452 if (mc.moving_task)
1453 schedule();
1454 finish_wait(&mc.waitq, &wait);
1455 return true;
1456 }
1457 }
1458 return false;
1459}
1460
Johannes Weinerc8713d02019-07-11 20:55:59 -07001461static char *memory_stat_format(struct mem_cgroup *memcg)
1462{
1463 struct seq_buf s;
1464 int i;
Johannes Weiner71cd3112017-05-03 14:55:13 -07001465
Johannes Weinerc8713d02019-07-11 20:55:59 -07001466 seq_buf_init(&s, kmalloc(PAGE_SIZE, GFP_KERNEL), PAGE_SIZE);
1467 if (!s.buffer)
1468 return NULL;
1469
1470 /*
1471 * Provide statistics on the state of the memory subsystem as
1472 * well as cumulative event counters that show past behavior.
1473 *
1474 * This list is ordered following a combination of these gradients:
1475 * 1) generic big picture -> specifics and details
1476 * 2) reflecting userspace activity -> reflecting kernel heuristics
1477 *
1478 * Current memory state:
1479 */
1480
1481 seq_buf_printf(&s, "anon %llu\n",
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001482 (u64)memcg_page_state(memcg, NR_ANON_MAPPED) *
Johannes Weinerc8713d02019-07-11 20:55:59 -07001483 PAGE_SIZE);
1484 seq_buf_printf(&s, "file %llu\n",
Johannes Weiner0d1c2072020-06-03 16:01:54 -07001485 (u64)memcg_page_state(memcg, NR_FILE_PAGES) *
Johannes Weinerc8713d02019-07-11 20:55:59 -07001486 PAGE_SIZE);
1487 seq_buf_printf(&s, "kernel_stack %llu\n",
1488 (u64)memcg_page_state(memcg, MEMCG_KERNEL_STACK_KB) *
1489 1024);
1490 seq_buf_printf(&s, "slab %llu\n",
Roman Gushchind42f3242020-08-06 23:20:39 -07001491 (u64)(memcg_page_state(memcg, NR_SLAB_RECLAIMABLE_B) +
1492 memcg_page_state(memcg, NR_SLAB_UNRECLAIMABLE_B)));
Johannes Weinerc8713d02019-07-11 20:55:59 -07001493 seq_buf_printf(&s, "sock %llu\n",
1494 (u64)memcg_page_state(memcg, MEMCG_SOCK) *
1495 PAGE_SIZE);
1496
1497 seq_buf_printf(&s, "shmem %llu\n",
1498 (u64)memcg_page_state(memcg, NR_SHMEM) *
1499 PAGE_SIZE);
1500 seq_buf_printf(&s, "file_mapped %llu\n",
1501 (u64)memcg_page_state(memcg, NR_FILE_MAPPED) *
1502 PAGE_SIZE);
1503 seq_buf_printf(&s, "file_dirty %llu\n",
1504 (u64)memcg_page_state(memcg, NR_FILE_DIRTY) *
1505 PAGE_SIZE);
1506 seq_buf_printf(&s, "file_writeback %llu\n",
1507 (u64)memcg_page_state(memcg, NR_WRITEBACK) *
1508 PAGE_SIZE);
1509
Johannes Weiner468c3982020-06-03 16:02:01 -07001510#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Johannes Weinerc8713d02019-07-11 20:55:59 -07001511 seq_buf_printf(&s, "anon_thp %llu\n",
Johannes Weiner468c3982020-06-03 16:02:01 -07001512 (u64)memcg_page_state(memcg, NR_ANON_THPS) *
1513 HPAGE_PMD_SIZE);
1514#endif
Johannes Weinerc8713d02019-07-11 20:55:59 -07001515
1516 for (i = 0; i < NR_LRU_LISTS; i++)
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08001517 seq_buf_printf(&s, "%s %llu\n", lru_list_name(i),
Johannes Weinerc8713d02019-07-11 20:55:59 -07001518 (u64)memcg_page_state(memcg, NR_LRU_BASE + i) *
1519 PAGE_SIZE);
1520
1521 seq_buf_printf(&s, "slab_reclaimable %llu\n",
Roman Gushchind42f3242020-08-06 23:20:39 -07001522 (u64)memcg_page_state(memcg, NR_SLAB_RECLAIMABLE_B));
Johannes Weinerc8713d02019-07-11 20:55:59 -07001523 seq_buf_printf(&s, "slab_unreclaimable %llu\n",
Roman Gushchind42f3242020-08-06 23:20:39 -07001524 (u64)memcg_page_state(memcg, NR_SLAB_UNRECLAIMABLE_B));
Johannes Weinerc8713d02019-07-11 20:55:59 -07001525
1526 /* Accumulated memory events */
1527
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08001528 seq_buf_printf(&s, "%s %lu\n", vm_event_name(PGFAULT),
1529 memcg_events(memcg, PGFAULT));
1530 seq_buf_printf(&s, "%s %lu\n", vm_event_name(PGMAJFAULT),
1531 memcg_events(memcg, PGMAJFAULT));
Johannes Weinerc8713d02019-07-11 20:55:59 -07001532
1533 seq_buf_printf(&s, "workingset_refault %lu\n",
1534 memcg_page_state(memcg, WORKINGSET_REFAULT));
1535 seq_buf_printf(&s, "workingset_activate %lu\n",
1536 memcg_page_state(memcg, WORKINGSET_ACTIVATE));
Yafang Shaoa6f55762020-06-01 21:49:32 -07001537 seq_buf_printf(&s, "workingset_restore %lu\n",
1538 memcg_page_state(memcg, WORKINGSET_RESTORE));
Johannes Weinerc8713d02019-07-11 20:55:59 -07001539 seq_buf_printf(&s, "workingset_nodereclaim %lu\n",
1540 memcg_page_state(memcg, WORKINGSET_NODERECLAIM));
1541
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08001542 seq_buf_printf(&s, "%s %lu\n", vm_event_name(PGREFILL),
1543 memcg_events(memcg, PGREFILL));
Johannes Weinerc8713d02019-07-11 20:55:59 -07001544 seq_buf_printf(&s, "pgscan %lu\n",
1545 memcg_events(memcg, PGSCAN_KSWAPD) +
1546 memcg_events(memcg, PGSCAN_DIRECT));
1547 seq_buf_printf(&s, "pgsteal %lu\n",
1548 memcg_events(memcg, PGSTEAL_KSWAPD) +
1549 memcg_events(memcg, PGSTEAL_DIRECT));
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08001550 seq_buf_printf(&s, "%s %lu\n", vm_event_name(PGACTIVATE),
1551 memcg_events(memcg, PGACTIVATE));
1552 seq_buf_printf(&s, "%s %lu\n", vm_event_name(PGDEACTIVATE),
1553 memcg_events(memcg, PGDEACTIVATE));
1554 seq_buf_printf(&s, "%s %lu\n", vm_event_name(PGLAZYFREE),
1555 memcg_events(memcg, PGLAZYFREE));
1556 seq_buf_printf(&s, "%s %lu\n", vm_event_name(PGLAZYFREED),
1557 memcg_events(memcg, PGLAZYFREED));
Johannes Weinerc8713d02019-07-11 20:55:59 -07001558
1559#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08001560 seq_buf_printf(&s, "%s %lu\n", vm_event_name(THP_FAULT_ALLOC),
Johannes Weinerc8713d02019-07-11 20:55:59 -07001561 memcg_events(memcg, THP_FAULT_ALLOC));
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08001562 seq_buf_printf(&s, "%s %lu\n", vm_event_name(THP_COLLAPSE_ALLOC),
Johannes Weinerc8713d02019-07-11 20:55:59 -07001563 memcg_events(memcg, THP_COLLAPSE_ALLOC));
1564#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1565
1566 /* The above should easily fit into one page */
1567 WARN_ON_ONCE(seq_buf_has_overflowed(&s));
1568
1569 return s.buffer;
1570}
Johannes Weiner71cd3112017-05-03 14:55:13 -07001571
Sha Zhengju58cf1882013-02-22 16:32:05 -08001572#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001573/**
yuzhoujianf0c867d2018-12-28 00:36:10 -08001574 * mem_cgroup_print_oom_context: Print OOM information relevant to
1575 * memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001576 * @memcg: The memory cgroup that went over limit
1577 * @p: Task that is going to be killed
1578 *
1579 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1580 * enabled
1581 */
yuzhoujianf0c867d2018-12-28 00:36:10 -08001582void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1583{
1584 rcu_read_lock();
1585
1586 if (memcg) {
1587 pr_cont(",oom_memcg=");
1588 pr_cont_cgroup_path(memcg->css.cgroup);
1589 } else
1590 pr_cont(",global_oom");
1591 if (p) {
1592 pr_cont(",task_memcg=");
1593 pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id));
1594 }
1595 rcu_read_unlock();
1596}
1597
1598/**
1599 * mem_cgroup_print_oom_meminfo: Print OOM memory information relevant to
1600 * memory controller.
1601 * @memcg: The memory cgroup that went over limit
1602 */
1603void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
Balbir Singhe2224322009-04-02 16:57:39 -07001604{
Johannes Weinerc8713d02019-07-11 20:55:59 -07001605 char *buf;
Balbir Singhe2224322009-04-02 16:57:39 -07001606
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001607 pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n",
1608 K((u64)page_counter_read(&memcg->memory)),
Chris Down15b42562020-04-01 21:07:20 -07001609 K((u64)READ_ONCE(memcg->memory.max)), memcg->memory.failcnt);
Johannes Weinerc8713d02019-07-11 20:55:59 -07001610 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
1611 pr_info("swap: usage %llukB, limit %llukB, failcnt %lu\n",
1612 K((u64)page_counter_read(&memcg->swap)),
Chris Down32d087c2020-04-01 21:07:30 -07001613 K((u64)READ_ONCE(memcg->swap.max)), memcg->swap.failcnt);
Johannes Weinerc8713d02019-07-11 20:55:59 -07001614 else {
1615 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n",
1616 K((u64)page_counter_read(&memcg->memsw)),
1617 K((u64)memcg->memsw.max), memcg->memsw.failcnt);
1618 pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n",
1619 K((u64)page_counter_read(&memcg->kmem)),
1620 K((u64)memcg->kmem.max), memcg->kmem.failcnt);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001621 }
Johannes Weinerc8713d02019-07-11 20:55:59 -07001622
1623 pr_info("Memory cgroup stats for ");
1624 pr_cont_cgroup_path(memcg->css.cgroup);
1625 pr_cont(":");
1626 buf = memory_stat_format(memcg);
1627 if (!buf)
1628 return;
1629 pr_info("%s", buf);
1630 kfree(buf);
Balbir Singhe2224322009-04-02 16:57:39 -07001631}
1632
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001633/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001634 * Return the memory (and swap, if configured) limit for a memcg.
1635 */
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001636unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001637{
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001638 unsigned long max;
David Rientjesa63d83f2010-08-09 17:19:46 -07001639
Chris Down15b42562020-04-01 21:07:20 -07001640 max = READ_ONCE(memcg->memory.max);
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001641 if (mem_cgroup_swappiness(memcg)) {
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001642 unsigned long memsw_max;
1643 unsigned long swap_max;
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001644
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001645 memsw_max = memcg->memsw.max;
Chris Down32d087c2020-04-01 21:07:30 -07001646 swap_max = READ_ONCE(memcg->swap.max);
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001647 swap_max = min(swap_max, (unsigned long)total_swap_pages);
1648 max = min(max + swap_max, memsw_max);
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001649 }
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001650 return max;
David Rientjesa63d83f2010-08-09 17:19:46 -07001651}
1652
Chris Down9783aa92019-10-06 17:58:32 -07001653unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1654{
1655 return page_counter_read(&memcg->memory);
1656}
1657
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07001658static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
David Rientjes19965462012-12-11 16:00:26 -08001659 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001660{
David Rientjes6e0fc462015-09-08 15:00:36 -07001661 struct oom_control oc = {
1662 .zonelist = NULL,
1663 .nodemask = NULL,
Vladimir Davydov2a966b72016-07-26 15:22:33 -07001664 .memcg = memcg,
David Rientjes6e0fc462015-09-08 15:00:36 -07001665 .gfp_mask = gfp_mask,
1666 .order = order,
David Rientjes6e0fc462015-09-08 15:00:36 -07001667 };
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001668 bool ret;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001669
Tetsuo Handa7775fac2019-03-05 15:46:47 -08001670 if (mutex_lock_killable(&oom_lock))
1671 return true;
1672 /*
1673 * A few threads which were not waiting at mutex_lock_killable() can
1674 * fail to bail out. Therefore, check again after holding oom_lock.
1675 */
1676 ret = should_force_charge() || out_of_memory(&oc);
Johannes Weinerdc564012015-06-24 16:57:19 -07001677 mutex_unlock(&oom_lock);
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001678 return ret;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001679}
1680
Andrew Morton0608f432013-09-24 15:27:41 -07001681static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
Mel Gormanef8f2322016-07-28 15:46:05 -07001682 pg_data_t *pgdat,
Andrew Morton0608f432013-09-24 15:27:41 -07001683 gfp_t gfp_mask,
1684 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001685{
Andrew Morton0608f432013-09-24 15:27:41 -07001686 struct mem_cgroup *victim = NULL;
1687 int total = 0;
1688 int loop = 0;
1689 unsigned long excess;
1690 unsigned long nr_scanned;
1691 struct mem_cgroup_reclaim_cookie reclaim = {
Mel Gormanef8f2322016-07-28 15:46:05 -07001692 .pgdat = pgdat,
Andrew Morton0608f432013-09-24 15:27:41 -07001693 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001694
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001695 excess = soft_limit_excess(root_memcg);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001696
Andrew Morton0608f432013-09-24 15:27:41 -07001697 while (1) {
1698 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
1699 if (!victim) {
1700 loop++;
1701 if (loop >= 2) {
1702 /*
1703 * If we have not been able to reclaim
1704 * anything, it might because there are
1705 * no reclaimable pages under this hierarchy
1706 */
1707 if (!total)
1708 break;
1709 /*
1710 * We want to do more targeted reclaim.
1711 * excess >> 2 is not to excessive so as to
1712 * reclaim too much, nor too less that we keep
1713 * coming back to reclaim from this cgroup
1714 */
1715 if (total >= (excess >> 2) ||
1716 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
1717 break;
1718 }
1719 continue;
1720 }
Mel Gormana9dd0a82016-07-28 15:46:02 -07001721 total += mem_cgroup_shrink_node(victim, gfp_mask, false,
Mel Gormanef8f2322016-07-28 15:46:05 -07001722 pgdat, &nr_scanned);
Andrew Morton0608f432013-09-24 15:27:41 -07001723 *total_scanned += nr_scanned;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001724 if (!soft_limit_excess(root_memcg))
Andrew Morton0608f432013-09-24 15:27:41 -07001725 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001726 }
Andrew Morton0608f432013-09-24 15:27:41 -07001727 mem_cgroup_iter_break(root_memcg, victim);
1728 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001729}
1730
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001731#ifdef CONFIG_LOCKDEP
1732static struct lockdep_map memcg_oom_lock_dep_map = {
1733 .name = "memcg_oom_lock",
1734};
1735#endif
1736
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001737static DEFINE_SPINLOCK(memcg_oom_lock);
1738
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001739/*
1740 * Check OOM-Killer is already running under our hierarchy.
1741 * If someone is running, return false.
1742 */
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001743static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001744{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001745 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001746
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001747 spin_lock(&memcg_oom_lock);
1748
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001749 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001750 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001751 /*
1752 * this subtree of our hierarchy is already locked
1753 * so we cannot give a lock.
1754 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001755 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001756 mem_cgroup_iter_break(memcg, iter);
1757 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001758 } else
1759 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001760 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001761
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001762 if (failed) {
1763 /*
1764 * OK, we failed to lock the whole subtree so we have
1765 * to clean up what we set up to the failing subtree
1766 */
1767 for_each_mem_cgroup_tree(iter, memcg) {
1768 if (iter == failed) {
1769 mem_cgroup_iter_break(memcg, iter);
1770 break;
1771 }
1772 iter->oom_lock = false;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001773 }
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001774 } else
1775 mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001776
1777 spin_unlock(&memcg_oom_lock);
1778
1779 return !failed;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001780}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001781
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001782static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001783{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001784 struct mem_cgroup *iter;
1785
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001786 spin_lock(&memcg_oom_lock);
Qian Cai5facae42019-09-19 12:09:40 -04001787 mutex_release(&memcg_oom_lock_dep_map, _RET_IP_);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001788 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001789 iter->oom_lock = false;
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001790 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001791}
1792
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001793static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001794{
1795 struct mem_cgroup *iter;
1796
Tejun Heoc2b42d32015-06-24 16:58:23 -07001797 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001798 for_each_mem_cgroup_tree(iter, memcg)
Tejun Heoc2b42d32015-06-24 16:58:23 -07001799 iter->under_oom++;
1800 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001801}
1802
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001803static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001804{
1805 struct mem_cgroup *iter;
1806
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001807 /*
1808 * When a new child is created while the hierarchy is under oom,
Tejun Heoc2b42d32015-06-24 16:58:23 -07001809 * mem_cgroup_oom_lock() may not be called. Watch for underflow.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001810 */
Tejun Heoc2b42d32015-06-24 16:58:23 -07001811 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001812 for_each_mem_cgroup_tree(iter, memcg)
Tejun Heoc2b42d32015-06-24 16:58:23 -07001813 if (iter->under_oom > 0)
1814 iter->under_oom--;
1815 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001816}
1817
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001818static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1819
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001820struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001821 struct mem_cgroup *memcg;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001822 wait_queue_entry_t wait;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001823};
1824
Ingo Molnarac6424b2017-06-20 12:06:13 +02001825static int memcg_oom_wake_function(wait_queue_entry_t *wait,
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001826 unsigned mode, int sync, void *arg)
1827{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001828 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1829 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001830 struct oom_wait_info *oom_wait_info;
1831
1832 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001833 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001834
Johannes Weiner2314b422014-12-10 15:44:33 -08001835 if (!mem_cgroup_is_descendant(wake_memcg, oom_wait_memcg) &&
1836 !mem_cgroup_is_descendant(oom_wait_memcg, wake_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001837 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001838 return autoremove_wake_function(wait, mode, sync, arg);
1839}
1840
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001841static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001842{
Tejun Heoc2b42d32015-06-24 16:58:23 -07001843 /*
1844 * For the following lockless ->under_oom test, the only required
1845 * guarantee is that it must see the state asserted by an OOM when
1846 * this function is called as a result of userland actions
1847 * triggered by the notification of the OOM. This is trivially
1848 * achieved by invoking mem_cgroup_mark_under_oom() before
1849 * triggering notification.
1850 */
1851 if (memcg && memcg->under_oom)
Tejun Heof4b90b702015-06-24 16:58:21 -07001852 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001853}
1854
Michal Hocko29ef6802018-08-17 15:47:11 -07001855enum oom_status {
1856 OOM_SUCCESS,
1857 OOM_FAILED,
1858 OOM_ASYNC,
1859 OOM_SKIPPED
1860};
1861
1862static enum oom_status mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001863{
Michal Hocko7056d3a2018-12-28 00:39:57 -08001864 enum oom_status ret;
1865 bool locked;
1866
Michal Hocko29ef6802018-08-17 15:47:11 -07001867 if (order > PAGE_ALLOC_COSTLY_ORDER)
1868 return OOM_SKIPPED;
1869
Roman Gushchin7a1adfd2018-10-26 15:09:48 -07001870 memcg_memory_event(memcg, MEMCG_OOM);
1871
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001872 /*
Johannes Weiner49426422013-10-16 13:46:59 -07001873 * We are in the middle of the charge context here, so we
1874 * don't want to block when potentially sitting on a callstack
1875 * that holds all kinds of filesystem and mm locks.
1876 *
Michal Hocko29ef6802018-08-17 15:47:11 -07001877 * cgroup1 allows disabling the OOM killer and waiting for outside
1878 * handling until the charge can succeed; remember the context and put
1879 * the task to sleep at the end of the page fault when all locks are
1880 * released.
Johannes Weiner49426422013-10-16 13:46:59 -07001881 *
Michal Hocko29ef6802018-08-17 15:47:11 -07001882 * On the other hand, in-kernel OOM killer allows for an async victim
1883 * memory reclaim (oom_reaper) and that means that we are not solely
1884 * relying on the oom victim to make a forward progress and we can
1885 * invoke the oom killer here.
1886 *
1887 * Please note that mem_cgroup_out_of_memory might fail to find a
1888 * victim and then we have to bail out from the charge path.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001889 */
Michal Hocko29ef6802018-08-17 15:47:11 -07001890 if (memcg->oom_kill_disable) {
1891 if (!current->in_user_fault)
1892 return OOM_SKIPPED;
1893 css_get(&memcg->css);
1894 current->memcg_in_oom = memcg;
1895 current->memcg_oom_gfp_mask = mask;
1896 current->memcg_oom_order = order;
1897
1898 return OOM_ASYNC;
1899 }
1900
Michal Hocko7056d3a2018-12-28 00:39:57 -08001901 mem_cgroup_mark_under_oom(memcg);
Michal Hocko29ef6802018-08-17 15:47:11 -07001902
Michal Hocko7056d3a2018-12-28 00:39:57 -08001903 locked = mem_cgroup_oom_trylock(memcg);
1904
1905 if (locked)
1906 mem_cgroup_oom_notify(memcg);
1907
1908 mem_cgroup_unmark_under_oom(memcg);
1909 if (mem_cgroup_out_of_memory(memcg, mask, order))
1910 ret = OOM_SUCCESS;
1911 else
1912 ret = OOM_FAILED;
1913
1914 if (locked)
1915 mem_cgroup_oom_unlock(memcg);
1916
1917 return ret;
Johannes Weiner49426422013-10-16 13:46:59 -07001918}
1919
1920/**
1921 * mem_cgroup_oom_synchronize - complete memcg OOM handling
1922 * @handle: actually kill/wait or just clean up the OOM state
1923 *
1924 * This has to be called at the end of a page fault if the memcg OOM
1925 * handler was enabled.
1926 *
1927 * Memcg supports userspace OOM handling where failed allocations must
1928 * sleep on a waitqueue until the userspace task resolves the
1929 * situation. Sleeping directly in the charge context with all kinds
1930 * of locks held is not a good idea, instead we remember an OOM state
1931 * in the task and mem_cgroup_oom_synchronize() has to be called at
1932 * the end of the page fault to complete the OOM handling.
1933 *
1934 * Returns %true if an ongoing memcg OOM situation was detected and
1935 * completed, %false otherwise.
1936 */
1937bool mem_cgroup_oom_synchronize(bool handle)
1938{
Tejun Heo626ebc42015-11-05 18:46:09 -08001939 struct mem_cgroup *memcg = current->memcg_in_oom;
Johannes Weiner49426422013-10-16 13:46:59 -07001940 struct oom_wait_info owait;
1941 bool locked;
1942
1943 /* OOM is global, do not handle */
1944 if (!memcg)
1945 return false;
1946
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001947 if (!handle)
Johannes Weiner49426422013-10-16 13:46:59 -07001948 goto cleanup;
1949
1950 owait.memcg = memcg;
1951 owait.wait.flags = 0;
1952 owait.wait.func = memcg_oom_wake_function;
1953 owait.wait.private = current;
Ingo Molnar2055da92017-06-20 12:06:46 +02001954 INIT_LIST_HEAD(&owait.wait.entry);
Johannes Weiner49426422013-10-16 13:46:59 -07001955
1956 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001957 mem_cgroup_mark_under_oom(memcg);
1958
1959 locked = mem_cgroup_oom_trylock(memcg);
1960
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001961 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001962 mem_cgroup_oom_notify(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001963
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001964 if (locked && !memcg->oom_kill_disable) {
1965 mem_cgroup_unmark_under_oom(memcg);
Johannes Weiner49426422013-10-16 13:46:59 -07001966 finish_wait(&memcg_oom_waitq, &owait.wait);
Tejun Heo626ebc42015-11-05 18:46:09 -08001967 mem_cgroup_out_of_memory(memcg, current->memcg_oom_gfp_mask,
1968 current->memcg_oom_order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001969 } else {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001970 schedule();
Johannes Weiner49426422013-10-16 13:46:59 -07001971 mem_cgroup_unmark_under_oom(memcg);
1972 finish_wait(&memcg_oom_waitq, &owait.wait);
1973 }
1974
1975 if (locked) {
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -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);
1983 }
Johannes Weiner49426422013-10-16 13:46:59 -07001984cleanup:
Tejun Heo626ebc42015-11-05 18:46:09 -08001985 current->memcg_in_oom = NULL;
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001986 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001987 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001988}
1989
Johannes Weinerd7365e72014-10-29 14:50:48 -07001990/**
Roman Gushchin3d8b38e2018-08-21 21:53:54 -07001991 * mem_cgroup_get_oom_group - get a memory cgroup to clean up after OOM
1992 * @victim: task to be killed by the OOM killer
1993 * @oom_domain: memcg in case of memcg OOM, NULL in case of system-wide OOM
1994 *
1995 * Returns a pointer to a memory cgroup, which has to be cleaned up
1996 * by killing all belonging OOM-killable tasks.
1997 *
1998 * Caller has to call mem_cgroup_put() on the returned non-NULL memcg.
1999 */
2000struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
2001 struct mem_cgroup *oom_domain)
2002{
2003 struct mem_cgroup *oom_group = NULL;
2004 struct mem_cgroup *memcg;
2005
2006 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
2007 return NULL;
2008
2009 if (!oom_domain)
2010 oom_domain = root_mem_cgroup;
2011
2012 rcu_read_lock();
2013
2014 memcg = mem_cgroup_from_task(victim);
2015 if (memcg == root_mem_cgroup)
2016 goto out;
2017
2018 /*
Roman Gushchin48fe2672020-04-01 21:07:39 -07002019 * If the victim task has been asynchronously moved to a different
2020 * memory cgroup, we might end up killing tasks outside oom_domain.
2021 * In this case it's better to ignore memory.group.oom.
2022 */
2023 if (unlikely(!mem_cgroup_is_descendant(memcg, oom_domain)))
2024 goto out;
2025
2026 /*
Roman Gushchin3d8b38e2018-08-21 21:53:54 -07002027 * Traverse the memory cgroup hierarchy from the victim task's
2028 * cgroup up to the OOMing cgroup (or root) to find the
2029 * highest-level memory cgroup with oom.group set.
2030 */
2031 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
2032 if (memcg->oom_group)
2033 oom_group = memcg;
2034
2035 if (memcg == oom_domain)
2036 break;
2037 }
2038
2039 if (oom_group)
2040 css_get(&oom_group->css);
2041out:
2042 rcu_read_unlock();
2043
2044 return oom_group;
2045}
2046
2047void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
2048{
2049 pr_info("Tasks in ");
2050 pr_cont_cgroup_path(memcg->css.cgroup);
2051 pr_cont(" are going to be killed due to memory.oom.group set\n");
2052}
2053
2054/**
Johannes Weiner81f8c3a2016-03-15 14:57:04 -07002055 * lock_page_memcg - lock a page->mem_cgroup binding
2056 * @page: the page
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002057 *
Johannes Weiner81f8c3a2016-03-15 14:57:04 -07002058 * This function protects unlocked LRU pages from being moved to
Johannes Weiner739f79f2017-08-18 15:15:48 -07002059 * another cgroup.
2060 *
2061 * It ensures lifetime of the returned memcg. Caller is responsible
2062 * for the lifetime of the page; __unlock_page_memcg() is available
2063 * when @page might get freed inside the locked section.
Balbir Singhd69b0422009-06-17 16:26:34 -07002064 */
Johannes Weiner739f79f2017-08-18 15:15:48 -07002065struct mem_cgroup *lock_page_memcg(struct page *page)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002066{
Johannes Weiner9da7b522020-06-03 16:01:51 -07002067 struct page *head = compound_head(page); /* rmap on tail pages */
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002068 struct mem_cgroup *memcg;
Johannes Weiner6de22612015-02-11 15:25:01 -08002069 unsigned long flags;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002070
Johannes Weiner6de22612015-02-11 15:25:01 -08002071 /*
2072 * The RCU lock is held throughout the transaction. The fast
2073 * path can get away without acquiring the memcg->move_lock
2074 * because page moving starts with an RCU grace period.
Johannes Weiner739f79f2017-08-18 15:15:48 -07002075 *
2076 * The RCU lock also protects the memcg from being freed when
2077 * the page state that is going to change is the only thing
2078 * preventing the page itself from being freed. E.g. writeback
2079 * doesn't hold a page reference and relies on PG_writeback to
2080 * keep off truncation, migration and so forth.
2081 */
Johannes Weinerd7365e72014-10-29 14:50:48 -07002082 rcu_read_lock();
2083
2084 if (mem_cgroup_disabled())
Johannes Weiner739f79f2017-08-18 15:15:48 -07002085 return NULL;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002086again:
Johannes Weiner9da7b522020-06-03 16:01:51 -07002087 memcg = head->mem_cgroup;
Johannes Weiner29833312014-12-10 15:44:02 -08002088 if (unlikely(!memcg))
Johannes Weiner739f79f2017-08-18 15:15:48 -07002089 return NULL;
Johannes Weinerd7365e72014-10-29 14:50:48 -07002090
Qiang Huangbdcbb652014-06-04 16:08:21 -07002091 if (atomic_read(&memcg->moving_account) <= 0)
Johannes Weiner739f79f2017-08-18 15:15:48 -07002092 return memcg;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002093
Johannes Weiner6de22612015-02-11 15:25:01 -08002094 spin_lock_irqsave(&memcg->move_lock, flags);
Johannes Weiner9da7b522020-06-03 16:01:51 -07002095 if (memcg != head->mem_cgroup) {
Johannes Weiner6de22612015-02-11 15:25:01 -08002096 spin_unlock_irqrestore(&memcg->move_lock, flags);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002097 goto again;
2098 }
Johannes Weiner6de22612015-02-11 15:25:01 -08002099
2100 /*
2101 * When charge migration first begins, we can have locked and
2102 * unlocked page stat updates happening concurrently. Track
Johannes Weiner81f8c3a2016-03-15 14:57:04 -07002103 * the task who has the lock for unlock_page_memcg().
Johannes Weiner6de22612015-02-11 15:25:01 -08002104 */
2105 memcg->move_lock_task = current;
2106 memcg->move_lock_flags = flags;
Johannes Weinerd7365e72014-10-29 14:50:48 -07002107
Johannes Weiner739f79f2017-08-18 15:15:48 -07002108 return memcg;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002109}
Johannes Weiner81f8c3a2016-03-15 14:57:04 -07002110EXPORT_SYMBOL(lock_page_memcg);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002111
Johannes Weinerd7365e72014-10-29 14:50:48 -07002112/**
Johannes Weiner739f79f2017-08-18 15:15:48 -07002113 * __unlock_page_memcg - unlock and unpin a memcg
2114 * @memcg: the memcg
2115 *
2116 * Unlock and unpin a memcg returned by lock_page_memcg().
Johannes Weinerd7365e72014-10-29 14:50:48 -07002117 */
Johannes Weiner739f79f2017-08-18 15:15:48 -07002118void __unlock_page_memcg(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002119{
Johannes Weiner6de22612015-02-11 15:25:01 -08002120 if (memcg && memcg->move_lock_task == current) {
2121 unsigned long flags = memcg->move_lock_flags;
2122
2123 memcg->move_lock_task = NULL;
2124 memcg->move_lock_flags = 0;
2125
2126 spin_unlock_irqrestore(&memcg->move_lock, flags);
2127 }
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002128
Johannes Weinerd7365e72014-10-29 14:50:48 -07002129 rcu_read_unlock();
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002130}
Johannes Weiner739f79f2017-08-18 15:15:48 -07002131
2132/**
2133 * unlock_page_memcg - unlock a page->mem_cgroup binding
2134 * @page: the page
2135 */
2136void unlock_page_memcg(struct page *page)
2137{
Johannes Weiner9da7b522020-06-03 16:01:51 -07002138 struct page *head = compound_head(page);
2139
2140 __unlock_page_memcg(head->mem_cgroup);
Johannes Weiner739f79f2017-08-18 15:15:48 -07002141}
Johannes Weiner81f8c3a2016-03-15 14:57:04 -07002142EXPORT_SYMBOL(unlock_page_memcg);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002143
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002144struct memcg_stock_pcp {
2145 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002146 unsigned int nr_pages;
Roman Gushchinbf4f0592020-08-06 23:20:49 -07002147
2148#ifdef CONFIG_MEMCG_KMEM
2149 struct obj_cgroup *cached_objcg;
2150 unsigned int nr_bytes;
2151#endif
2152
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002153 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002154 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002155#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002156};
2157static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002158static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002159
Roman Gushchinbf4f0592020-08-06 23:20:49 -07002160#ifdef CONFIG_MEMCG_KMEM
2161static void drain_obj_stock(struct memcg_stock_pcp *stock);
2162static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
2163 struct mem_cgroup *root_memcg);
2164
2165#else
2166static inline void drain_obj_stock(struct memcg_stock_pcp *stock)
2167{
2168}
2169static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
2170 struct mem_cgroup *root_memcg)
2171{
2172 return false;
2173}
2174#endif
2175
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;
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002190 unsigned long flags;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002191 bool ret = false;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002192
Johannes Weinera983b5e2018-01-31 16:16:45 -08002193 if (nr_pages > MEMCG_CHARGE_BATCH)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002194 return ret;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002195
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002196 local_irq_save(flags);
2197
2198 stock = this_cpu_ptr(&memcg_stock);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002199 if (memcg == stock->cached && stock->nr_pages >= nr_pages) {
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002200 stock->nr_pages -= nr_pages;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002201 ret = true;
2202 }
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002203
2204 local_irq_restore(flags);
2205
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002206 return ret;
2207}
2208
2209/*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002210 * Returns stocks cached in percpu and reset cached information.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002211 */
2212static void drain_stock(struct memcg_stock_pcp *stock)
2213{
2214 struct mem_cgroup *old = stock->cached;
2215
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07002216 if (!old)
2217 return;
2218
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002219 if (stock->nr_pages) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002220 page_counter_uncharge(&old->memory, stock->nr_pages);
Johannes Weiner7941d212016-01-14 15:21:23 -08002221 if (do_memsw_account())
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002222 page_counter_uncharge(&old->memsw, stock->nr_pages);
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002223 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002224 }
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07002225
2226 css_put(&old->css);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002227 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002228}
2229
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002230static void drain_local_stock(struct work_struct *dummy)
2231{
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002232 struct memcg_stock_pcp *stock;
2233 unsigned long flags;
2234
Michal Hocko72f01842017-10-03 16:14:53 -07002235 /*
2236 * The only protection from memory hotplug vs. drain_stock races is
2237 * that we always operate on local CPU stock here with IRQ disabled
2238 */
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002239 local_irq_save(flags);
2240
2241 stock = this_cpu_ptr(&memcg_stock);
Roman Gushchinbf4f0592020-08-06 23:20:49 -07002242 drain_obj_stock(stock);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002243 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002244 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002245
2246 local_irq_restore(flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002247}
2248
2249/*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002250 * Cache charges(val) to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002251 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002252 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002253static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002254{
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002255 struct memcg_stock_pcp *stock;
2256 unsigned long flags;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002257
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002258 local_irq_save(flags);
2259
2260 stock = this_cpu_ptr(&memcg_stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002261 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002262 drain_stock(stock);
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07002263 css_get(&memcg->css);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002264 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002265 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002266 stock->nr_pages += nr_pages;
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002267
Johannes Weinera983b5e2018-01-31 16:16:45 -08002268 if (stock->nr_pages > MEMCG_CHARGE_BATCH)
Roman Gushchin475d0482017-09-08 16:13:09 -07002269 drain_stock(stock);
2270
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002271 local_irq_restore(flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002272}
2273
2274/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002275 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002276 * of the hierarchy under it.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002277 */
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002278static void drain_all_stock(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002279{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002280 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002281
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002282 /* If someone's already draining, avoid adding running more workers. */
2283 if (!mutex_trylock(&percpu_charge_mutex))
2284 return;
Michal Hocko72f01842017-10-03 16:14:53 -07002285 /*
2286 * Notify other cpus that system-wide "drain" is running
2287 * We do not care about races with the cpu hotplug because cpu down
2288 * as well as workers from this path always operate on the local
2289 * per-cpu data. CPU up doesn't touch memcg_stock at all.
2290 */
Johannes Weiner5af12d02011-08-25 15:59:07 -07002291 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002292 for_each_online_cpu(cpu) {
2293 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002294 struct mem_cgroup *memcg;
Roman Gushchine1a366b2019-09-23 15:34:58 -07002295 bool flush = false;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002296
Roman Gushchine1a366b2019-09-23 15:34:58 -07002297 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002298 memcg = stock->cached;
Roman Gushchine1a366b2019-09-23 15:34:58 -07002299 if (memcg && stock->nr_pages &&
2300 mem_cgroup_is_descendant(memcg, root_memcg))
2301 flush = true;
Roman Gushchinbf4f0592020-08-06 23:20:49 -07002302 if (obj_stock_flush_required(stock, root_memcg))
2303 flush = true;
Roman Gushchine1a366b2019-09-23 15:34:58 -07002304 rcu_read_unlock();
2305
2306 if (flush &&
2307 !test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
Michal Hockod1a05b62011-07-26 16:08:27 -07002308 if (cpu == curcpu)
2309 drain_local_stock(&stock->work);
2310 else
2311 schedule_work_on(cpu, &stock->work);
2312 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002313 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002314 put_cpu();
Michal Hocko9f50fad2011-08-09 11:56:26 +02002315 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002316}
2317
Sebastian Andrzej Siewior308167f2016-11-03 15:49:59 +01002318static int memcg_hotplug_cpu_dead(unsigned int cpu)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002319{
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002320 struct memcg_stock_pcp *stock;
Johannes Weiner42a30032019-05-14 15:47:12 -07002321 struct mem_cgroup *memcg, *mi;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002322
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002323 stock = &per_cpu(memcg_stock, cpu);
2324 drain_stock(stock);
Johannes Weinera983b5e2018-01-31 16:16:45 -08002325
2326 for_each_mem_cgroup(memcg) {
2327 int i;
2328
2329 for (i = 0; i < MEMCG_NR_STAT; i++) {
2330 int nid;
2331 long x;
2332
Chris Down871789d2019-05-14 15:46:57 -07002333 x = this_cpu_xchg(memcg->vmstats_percpu->stat[i], 0);
Johannes Weiner815744d2019-06-13 15:55:46 -07002334 if (x)
Johannes Weiner42a30032019-05-14 15:47:12 -07002335 for (mi = memcg; mi; mi = parent_mem_cgroup(mi))
2336 atomic_long_add(x, &memcg->vmstats[i]);
Johannes Weinera983b5e2018-01-31 16:16:45 -08002337
2338 if (i >= NR_VM_NODE_STAT_ITEMS)
2339 continue;
2340
2341 for_each_node(nid) {
2342 struct mem_cgroup_per_node *pn;
2343
2344 pn = mem_cgroup_nodeinfo(memcg, nid);
2345 x = this_cpu_xchg(pn->lruvec_stat_cpu->count[i], 0);
Johannes Weiner815744d2019-06-13 15:55:46 -07002346 if (x)
Johannes Weiner42a30032019-05-14 15:47:12 -07002347 do {
2348 atomic_long_add(x, &pn->lruvec_stat[i]);
2349 } while ((pn = parent_nodeinfo(pn, nid)));
Johannes Weinera983b5e2018-01-31 16:16:45 -08002350 }
2351 }
2352
Johannes Weinere27be242018-04-10 16:29:45 -07002353 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
Johannes Weinera983b5e2018-01-31 16:16:45 -08002354 long x;
2355
Chris Down871789d2019-05-14 15:46:57 -07002356 x = this_cpu_xchg(memcg->vmstats_percpu->events[i], 0);
Johannes Weiner815744d2019-06-13 15:55:46 -07002357 if (x)
Johannes Weiner42a30032019-05-14 15:47:12 -07002358 for (mi = memcg; mi; mi = parent_mem_cgroup(mi))
2359 atomic_long_add(x, &memcg->vmevents[i]);
Johannes Weinera983b5e2018-01-31 16:16:45 -08002360 }
2361 }
2362
Sebastian Andrzej Siewior308167f2016-11-03 15:49:59 +01002363 return 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002364}
2365
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08002366static void reclaim_high(struct mem_cgroup *memcg,
2367 unsigned int nr_pages,
2368 gfp_t gfp_mask)
2369{
2370 do {
Jakub Kicinskid1663a92020-06-01 21:49:49 -07002371 if (page_counter_read(&memcg->memory) <=
2372 READ_ONCE(memcg->memory.high))
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08002373 continue;
Johannes Weinere27be242018-04-10 16:29:45 -07002374 memcg_memory_event(memcg, MEMCG_HIGH);
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08002375 try_to_free_mem_cgroup_pages(memcg, nr_pages, gfp_mask, true);
Chris Down4bf17302020-04-06 20:03:30 -07002376 } while ((memcg = parent_mem_cgroup(memcg)) &&
2377 !mem_cgroup_is_root(memcg));
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08002378}
2379
2380static void high_work_func(struct work_struct *work)
2381{
2382 struct mem_cgroup *memcg;
2383
2384 memcg = container_of(work, struct mem_cgroup, high_work);
Johannes Weinera983b5e2018-01-31 16:16:45 -08002385 reclaim_high(memcg, MEMCG_CHARGE_BATCH, GFP_KERNEL);
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08002386}
2387
Tejun Heob23afb92015-11-05 18:46:11 -08002388/*
Chris Down0e4b01d2019-09-23 15:34:55 -07002389 * Clamp the maximum sleep time per allocation batch to 2 seconds. This is
2390 * enough to still cause a significant slowdown in most cases, while still
2391 * allowing diagnostics and tracing to proceed without becoming stuck.
2392 */
2393#define MEMCG_MAX_HIGH_DELAY_JIFFIES (2UL*HZ)
2394
2395/*
2396 * When calculating the delay, we use these either side of the exponentiation to
2397 * maintain precision and scale to a reasonable number of jiffies (see the table
2398 * below.
2399 *
2400 * - MEMCG_DELAY_PRECISION_SHIFT: Extra precision bits while translating the
2401 * overage ratio to a delay.
2402 * - MEMCG_DELAY_SCALING_SHIFT: The number of bits to scale down down the
2403 * proposed penalty in order to reduce to a reasonable number of jiffies, and
2404 * to produce a reasonable delay curve.
2405 *
2406 * MEMCG_DELAY_SCALING_SHIFT just happens to be a number that produces a
2407 * reasonable delay curve compared to precision-adjusted overage, not
2408 * penalising heavily at first, but still making sure that growth beyond the
2409 * limit penalises misbehaviour cgroups by slowing them down exponentially. For
2410 * example, with a high of 100 megabytes:
2411 *
2412 * +-------+------------------------+
2413 * | usage | time to allocate in ms |
2414 * +-------+------------------------+
2415 * | 100M | 0 |
2416 * | 101M | 6 |
2417 * | 102M | 25 |
2418 * | 103M | 57 |
2419 * | 104M | 102 |
2420 * | 105M | 159 |
2421 * | 106M | 230 |
2422 * | 107M | 313 |
2423 * | 108M | 409 |
2424 * | 109M | 518 |
2425 * | 110M | 639 |
2426 * | 111M | 774 |
2427 * | 112M | 921 |
2428 * | 113M | 1081 |
2429 * | 114M | 1254 |
2430 * | 115M | 1439 |
2431 * | 116M | 1638 |
2432 * | 117M | 1849 |
2433 * | 118M | 2000 |
2434 * | 119M | 2000 |
2435 * | 120M | 2000 |
2436 * +-------+------------------------+
2437 */
2438 #define MEMCG_DELAY_PRECISION_SHIFT 20
2439 #define MEMCG_DELAY_SCALING_SHIFT 14
2440
Jakub Kicinski8a5dbc62020-06-01 21:49:42 -07002441static u64 calculate_overage(unsigned long usage, unsigned long high)
2442{
2443 u64 overage;
2444
2445 if (usage <= high)
2446 return 0;
2447
2448 /*
2449 * Prevent division by 0 in overage calculation by acting as if
2450 * it was a threshold of 1 page
2451 */
2452 high = max(high, 1UL);
2453
2454 overage = usage - high;
2455 overage <<= MEMCG_DELAY_PRECISION_SHIFT;
2456 return div64_u64(overage, high);
2457}
2458
2459static u64 mem_find_max_overage(struct mem_cgroup *memcg)
2460{
2461 u64 overage, max_overage = 0;
2462
2463 do {
2464 overage = calculate_overage(page_counter_read(&memcg->memory),
Jakub Kicinskid1663a92020-06-01 21:49:49 -07002465 READ_ONCE(memcg->memory.high));
Jakub Kicinski8a5dbc62020-06-01 21:49:42 -07002466 max_overage = max(overage, max_overage);
2467 } while ((memcg = parent_mem_cgroup(memcg)) &&
2468 !mem_cgroup_is_root(memcg));
2469
2470 return max_overage;
2471}
2472
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07002473static u64 swap_find_max_overage(struct mem_cgroup *memcg)
2474{
2475 u64 overage, max_overage = 0;
2476
2477 do {
2478 overage = calculate_overage(page_counter_read(&memcg->swap),
2479 READ_ONCE(memcg->swap.high));
2480 if (overage)
2481 memcg_memory_event(memcg, MEMCG_SWAP_HIGH);
2482 max_overage = max(overage, max_overage);
2483 } while ((memcg = parent_mem_cgroup(memcg)) &&
2484 !mem_cgroup_is_root(memcg));
2485
2486 return max_overage;
2487}
2488
Chris Down0e4b01d2019-09-23 15:34:55 -07002489/*
Chris Downe26733e2020-03-21 18:22:23 -07002490 * Get the number of jiffies that we should penalise a mischievous cgroup which
2491 * is exceeding its memory.high by checking both it and its ancestors.
Tejun Heob23afb92015-11-05 18:46:11 -08002492 */
Chris Downe26733e2020-03-21 18:22:23 -07002493static unsigned long calculate_high_delay(struct mem_cgroup *memcg,
Jakub Kicinski8a5dbc62020-06-01 21:49:42 -07002494 unsigned int nr_pages,
2495 u64 max_overage)
Tejun Heob23afb92015-11-05 18:46:11 -08002496{
Chris Downe26733e2020-03-21 18:22:23 -07002497 unsigned long penalty_jiffies;
Chris Downe26733e2020-03-21 18:22:23 -07002498
2499 if (!max_overage)
2500 return 0;
Chris Down0e4b01d2019-09-23 15:34:55 -07002501
2502 /*
Chris Down0e4b01d2019-09-23 15:34:55 -07002503 * We use overage compared to memory.high to calculate the number of
2504 * jiffies to sleep (penalty_jiffies). Ideally this value should be
2505 * fairly lenient on small overages, and increasingly harsh when the
2506 * memcg in question makes it clear that it has no intention of stopping
2507 * its crazy behaviour, so we exponentially increase the delay based on
2508 * overage amount.
2509 */
Chris Downe26733e2020-03-21 18:22:23 -07002510 penalty_jiffies = max_overage * max_overage * HZ;
2511 penalty_jiffies >>= MEMCG_DELAY_PRECISION_SHIFT;
2512 penalty_jiffies >>= MEMCG_DELAY_SCALING_SHIFT;
Chris Down0e4b01d2019-09-23 15:34:55 -07002513
2514 /*
2515 * Factor in the task's own contribution to the overage, such that four
2516 * N-sized allocations are throttled approximately the same as one
2517 * 4N-sized allocation.
2518 *
2519 * MEMCG_CHARGE_BATCH pages is nominal, so work out how much smaller or
2520 * larger the current charge patch is than that.
2521 */
Jakub Kicinskiff144e62020-06-01 21:49:45 -07002522 return penalty_jiffies * nr_pages / MEMCG_CHARGE_BATCH;
Chris Downe26733e2020-03-21 18:22:23 -07002523}
2524
2525/*
2526 * Scheduled by try_charge() to be executed from the userland return path
2527 * and reclaims memory over the high limit.
2528 */
2529void mem_cgroup_handle_over_high(void)
2530{
2531 unsigned long penalty_jiffies;
2532 unsigned long pflags;
2533 unsigned int nr_pages = current->memcg_nr_pages_over_high;
2534 struct mem_cgroup *memcg;
2535
2536 if (likely(!nr_pages))
2537 return;
2538
2539 memcg = get_mem_cgroup_from_mm(current->mm);
2540 reclaim_high(memcg, nr_pages, GFP_KERNEL);
2541 current->memcg_nr_pages_over_high = 0;
2542
2543 /*
2544 * memory.high is breached and reclaim is unable to keep up. Throttle
2545 * allocators proactively to slow down excessive growth.
2546 */
Jakub Kicinski8a5dbc62020-06-01 21:49:42 -07002547 penalty_jiffies = calculate_high_delay(memcg, nr_pages,
2548 mem_find_max_overage(memcg));
Chris Down0e4b01d2019-09-23 15:34:55 -07002549
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07002550 penalty_jiffies += calculate_high_delay(memcg, nr_pages,
2551 swap_find_max_overage(memcg));
2552
Chris Down0e4b01d2019-09-23 15:34:55 -07002553 /*
Jakub Kicinskiff144e62020-06-01 21:49:45 -07002554 * Clamp the max delay per usermode return so as to still keep the
2555 * application moving forwards and also permit diagnostics, albeit
2556 * extremely slowly.
2557 */
2558 penalty_jiffies = min(penalty_jiffies, MEMCG_MAX_HIGH_DELAY_JIFFIES);
2559
2560 /*
Chris Down0e4b01d2019-09-23 15:34:55 -07002561 * Don't sleep if the amount of jiffies this memcg owes us is so low
2562 * that it's not even worth doing, in an attempt to be nice to those who
2563 * go only a small amount over their memory.high value and maybe haven't
2564 * been aggressively reclaimed enough yet.
2565 */
2566 if (penalty_jiffies <= HZ / 100)
2567 goto out;
2568
2569 /*
2570 * If we exit early, we're guaranteed to die (since
2571 * schedule_timeout_killable sets TASK_KILLABLE). This means we don't
2572 * need to account for any ill-begotten jiffies to pay them off later.
2573 */
2574 psi_memstall_enter(&pflags);
2575 schedule_timeout_killable(penalty_jiffies);
2576 psi_memstall_leave(&pflags);
2577
2578out:
2579 css_put(&memcg->css);
Tejun Heob23afb92015-11-05 18:46:11 -08002580}
2581
Johannes Weiner00501b52014-08-08 14:19:20 -07002582static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
2583 unsigned int nr_pages)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002584{
Johannes Weinera983b5e2018-01-31 16:16:45 -08002585 unsigned int batch = max(MEMCG_CHARGE_BATCH, nr_pages);
Johannes Weiner9b130612014-08-06 16:05:51 -07002586 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002587 struct mem_cgroup *mem_over_limit;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002588 struct page_counter *counter;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002589 unsigned long nr_reclaimed;
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002590 bool may_swap = true;
2591 bool drained = false;
Michal Hocko29ef6802018-08-17 15:47:11 -07002592 enum oom_status oom_status;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002593
Johannes Weinerce00a962014-09-05 08:43:57 -04002594 if (mem_cgroup_is_root(memcg))
Tejun Heo10d53c72015-11-05 18:46:17 -08002595 return 0;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002596retry:
Michal Hockob6b6cc72014-04-07 15:37:44 -07002597 if (consume_stock(memcg, nr_pages))
Tejun Heo10d53c72015-11-05 18:46:17 -08002598 return 0;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002599
Johannes Weiner7941d212016-01-14 15:21:23 -08002600 if (!do_memsw_account() ||
Johannes Weiner6071ca52015-11-05 18:50:26 -08002601 page_counter_try_charge(&memcg->memsw, batch, &counter)) {
2602 if (page_counter_try_charge(&memcg->memory, batch, &counter))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002603 goto done_restock;
Johannes Weiner7941d212016-01-14 15:21:23 -08002604 if (do_memsw_account())
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002605 page_counter_uncharge(&memcg->memsw, batch);
2606 mem_over_limit = mem_cgroup_from_counter(counter, memory);
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002607 } else {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002608 mem_over_limit = mem_cgroup_from_counter(counter, memsw);
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002609 may_swap = false;
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002610 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002611
Johannes Weiner6539cc02014-08-06 16:05:42 -07002612 if (batch > nr_pages) {
2613 batch = nr_pages;
2614 goto retry;
2615 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002616
Johannes Weiner06b078f2014-08-06 16:05:44 -07002617 /*
Johannes Weiner869712f2019-11-05 21:17:13 -08002618 * Memcg doesn't have a dedicated reserve for atomic
2619 * allocations. But like the global atomic pool, we need to
2620 * put the burden of reclaim on regular allocation requests
2621 * and let these go through as privileged allocations.
2622 */
2623 if (gfp_mask & __GFP_ATOMIC)
2624 goto force;
2625
2626 /*
Johannes Weiner06b078f2014-08-06 16:05:44 -07002627 * Unlike in global OOM situations, memcg is not in a physical
2628 * memory shortage. Allow dying and OOM-killed tasks to
2629 * bypass the last charges so that they can exit quickly and
2630 * free their memory.
2631 */
Tetsuo Handa7775fac2019-03-05 15:46:47 -08002632 if (unlikely(should_force_charge()))
Tejun Heo10d53c72015-11-05 18:46:17 -08002633 goto force;
Johannes Weiner06b078f2014-08-06 16:05:44 -07002634
Johannes Weiner89a28482016-10-27 17:46:56 -07002635 /*
2636 * Prevent unbounded recursion when reclaim operations need to
2637 * allocate memory. This might exceed the limits temporarily,
2638 * but we prefer facilitating memory reclaim and getting back
2639 * under the limit over triggering OOM kills in these cases.
2640 */
2641 if (unlikely(current->flags & PF_MEMALLOC))
2642 goto force;
2643
Johannes Weiner06b078f2014-08-06 16:05:44 -07002644 if (unlikely(task_in_memcg_oom(current)))
2645 goto nomem;
2646
Mel Gormand0164ad2015-11-06 16:28:21 -08002647 if (!gfpflags_allow_blocking(gfp_mask))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002648 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002649
Johannes Weinere27be242018-04-10 16:29:45 -07002650 memcg_memory_event(mem_over_limit, MEMCG_MAX);
Johannes Weiner241994ed2015-02-11 15:26:06 -08002651
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002652 nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
2653 gfp_mask, may_swap);
Johannes Weiner6539cc02014-08-06 16:05:42 -07002654
Johannes Weiner61e02c72014-08-06 16:08:16 -07002655 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner6539cc02014-08-06 16:05:42 -07002656 goto retry;
Johannes Weiner28c34c22014-08-06 16:05:47 -07002657
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002658 if (!drained) {
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002659 drain_all_stock(mem_over_limit);
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002660 drained = true;
2661 goto retry;
2662 }
2663
Johannes Weiner28c34c22014-08-06 16:05:47 -07002664 if (gfp_mask & __GFP_NORETRY)
2665 goto nomem;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002666 /*
2667 * Even though the limit is exceeded at this point, reclaim
2668 * may have been able to free some pages. Retry the charge
2669 * before killing the task.
2670 *
2671 * Only for regular pages, though: huge pages are rather
2672 * unlikely to succeed so close to the limit, and we fall back
2673 * to regular pages anyway in case of failure.
2674 */
Johannes Weiner61e02c72014-08-06 16:08:16 -07002675 if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002676 goto retry;
2677 /*
2678 * At task move, charge accounts can be doubly counted. So, it's
2679 * better to wait until the end of task_move if something is going on.
2680 */
2681 if (mem_cgroup_wait_acct_move(mem_over_limit))
2682 goto retry;
2683
Johannes Weiner9b130612014-08-06 16:05:51 -07002684 if (nr_retries--)
2685 goto retry;
2686
Shakeel Butt38d38492019-07-11 20:55:48 -07002687 if (gfp_mask & __GFP_RETRY_MAYFAIL)
Michal Hocko29ef6802018-08-17 15:47:11 -07002688 goto nomem;
2689
Johannes Weiner06b078f2014-08-06 16:05:44 -07002690 if (gfp_mask & __GFP_NOFAIL)
Tejun Heo10d53c72015-11-05 18:46:17 -08002691 goto force;
Johannes Weiner06b078f2014-08-06 16:05:44 -07002692
Johannes Weiner6539cc02014-08-06 16:05:42 -07002693 if (fatal_signal_pending(current))
Tejun Heo10d53c72015-11-05 18:46:17 -08002694 goto force;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002695
Michal Hocko29ef6802018-08-17 15:47:11 -07002696 /*
2697 * keep retrying as long as the memcg oom killer is able to make
2698 * a forward progress or bypass the charge if the oom killer
2699 * couldn't make any progress.
2700 */
2701 oom_status = mem_cgroup_oom(mem_over_limit, gfp_mask,
Jerome Marchand3608de02015-11-05 18:47:29 -08002702 get_order(nr_pages * PAGE_SIZE));
Michal Hocko29ef6802018-08-17 15:47:11 -07002703 switch (oom_status) {
2704 case OOM_SUCCESS:
2705 nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Michal Hocko29ef6802018-08-17 15:47:11 -07002706 goto retry;
2707 case OOM_FAILED:
2708 goto force;
2709 default:
2710 goto nomem;
2711 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002712nomem:
Johannes Weiner6d1fdc42014-04-07 15:37:45 -07002713 if (!(gfp_mask & __GFP_NOFAIL))
Johannes Weiner3168ecb2013-10-31 16:34:13 -07002714 return -ENOMEM;
Tejun Heo10d53c72015-11-05 18:46:17 -08002715force:
2716 /*
2717 * The allocation either can't fail or will lead to more memory
2718 * being freed very soon. Allow memory usage go over the limit
2719 * temporarily by force charging it.
2720 */
2721 page_counter_charge(&memcg->memory, nr_pages);
Johannes Weiner7941d212016-01-14 15:21:23 -08002722 if (do_memsw_account())
Tejun Heo10d53c72015-11-05 18:46:17 -08002723 page_counter_charge(&memcg->memsw, nr_pages);
Tejun Heo10d53c72015-11-05 18:46:17 -08002724
2725 return 0;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002726
2727done_restock:
2728 if (batch > nr_pages)
2729 refill_stock(memcg, batch - nr_pages);
Tejun Heob23afb92015-11-05 18:46:11 -08002730
Johannes Weiner241994ed2015-02-11 15:26:06 -08002731 /*
Tejun Heob23afb92015-11-05 18:46:11 -08002732 * If the hierarchy is above the normal consumption range, schedule
2733 * reclaim on returning to userland. We can perform reclaim here
Mel Gorman71baba42015-11-06 16:28:28 -08002734 * if __GFP_RECLAIM but let's always punt for simplicity and so that
Tejun Heob23afb92015-11-05 18:46:11 -08002735 * GFP_KERNEL can consistently be used during reclaim. @memcg is
2736 * not recorded as it most likely matches current's and won't
2737 * change in the meantime. As high limit is checked again before
2738 * reclaim, the cost of mismatch is negligible.
Johannes Weiner241994ed2015-02-11 15:26:06 -08002739 */
2740 do {
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07002741 bool mem_high, swap_high;
2742
2743 mem_high = page_counter_read(&memcg->memory) >
2744 READ_ONCE(memcg->memory.high);
2745 swap_high = page_counter_read(&memcg->swap) >
2746 READ_ONCE(memcg->swap.high);
2747
2748 /* Don't bother a random interrupted task */
2749 if (in_interrupt()) {
2750 if (mem_high) {
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08002751 schedule_work(&memcg->high_work);
2752 break;
2753 }
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07002754 continue;
2755 }
2756
2757 if (mem_high || swap_high) {
2758 /*
2759 * The allocating tasks in this cgroup will need to do
2760 * reclaim or be throttled to prevent further growth
2761 * of the memory or swap footprints.
2762 *
2763 * Target some best-effort fairness between the tasks,
2764 * and distribute reclaim work and delay penalties
2765 * based on how much each task is actually allocating.
2766 */
Vladimir Davydov9516a182015-12-11 13:40:24 -08002767 current->memcg_nr_pages_over_high += batch;
Tejun Heob23afb92015-11-05 18:46:11 -08002768 set_notify_resume(current);
2769 break;
2770 }
Johannes Weiner241994ed2015-02-11 15:26:06 -08002771 } while ((memcg = parent_mem_cgroup(memcg)));
Tejun Heo10d53c72015-11-05 18:46:17 -08002772
2773 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002774}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002775
Johannes Weinerf0e45fb2020-06-03 16:02:07 -07002776#if defined(CONFIG_MEMCG_KMEM) || defined(CONFIG_MMU)
Johannes Weiner00501b52014-08-08 14:19:20 -07002777static void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002778{
Johannes Weinerce00a962014-09-05 08:43:57 -04002779 if (mem_cgroup_is_root(memcg))
2780 return;
2781
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002782 page_counter_uncharge(&memcg->memory, nr_pages);
Johannes Weiner7941d212016-01-14 15:21:23 -08002783 if (do_memsw_account())
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002784 page_counter_uncharge(&memcg->memsw, nr_pages);
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002785}
Johannes Weinerf0e45fb2020-06-03 16:02:07 -07002786#endif
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002787
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07002788static void commit_charge(struct page *page, struct mem_cgroup *memcg)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002789{
Johannes Weiner1306a852014-12-10 15:44:52 -08002790 VM_BUG_ON_PAGE(page->mem_cgroup, page);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002791 /*
Johannes Weinera0b5b412020-06-03 16:02:27 -07002792 * Any of the following ensures page->mem_cgroup stability:
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002793 *
Johannes Weinera0b5b412020-06-03 16:02:27 -07002794 * - the page lock
2795 * - LRU isolation
2796 * - lock_page_memcg()
2797 * - exclusive reference
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002798 */
Johannes Weiner1306a852014-12-10 15:44:52 -08002799 page->mem_cgroup = memcg;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002800}
2801
Kirill Tkhai84c07d12018-08-17 15:47:25 -07002802#ifdef CONFIG_MEMCG_KMEM
Roman Gushchin8380ce42020-03-28 19:17:25 -07002803/*
2804 * Returns a pointer to the memory cgroup to which the kernel object is charged.
2805 *
2806 * The caller must ensure the memcg lifetime, e.g. by taking rcu_read_lock(),
2807 * cgroup_mutex, etc.
2808 */
2809struct mem_cgroup *mem_cgroup_from_obj(void *p)
2810{
2811 struct page *page;
2812
2813 if (mem_cgroup_disabled())
2814 return NULL;
2815
2816 page = virt_to_head_page(p);
2817
2818 /*
Roman Gushchin98556092020-08-06 23:21:10 -07002819 * Slab objects are accounted individually, not per-page.
2820 * Memcg membership data for each individual object is saved in
2821 * the page->obj_cgroups.
Roman Gushchin8380ce42020-03-28 19:17:25 -07002822 */
Roman Gushchin98556092020-08-06 23:21:10 -07002823 if (page_has_obj_cgroups(page)) {
2824 struct obj_cgroup *objcg;
2825 unsigned int off;
2826
2827 off = obj_to_index(page->slab_cache, page, p);
2828 objcg = page_obj_cgroups(page)[off];
2829 return obj_cgroup_memcg(objcg);
2830 }
Roman Gushchin8380ce42020-03-28 19:17:25 -07002831
2832 /* All other pages use page->mem_cgroup */
2833 return page->mem_cgroup;
2834}
2835
Roman Gushchinbf4f0592020-08-06 23:20:49 -07002836__always_inline struct obj_cgroup *get_obj_cgroup_from_current(void)
2837{
2838 struct obj_cgroup *objcg = NULL;
2839 struct mem_cgroup *memcg;
2840
2841 if (unlikely(!current->mm && !current->active_memcg))
2842 return NULL;
2843
2844 rcu_read_lock();
2845 if (unlikely(current->active_memcg))
2846 memcg = rcu_dereference(current->active_memcg);
2847 else
2848 memcg = mem_cgroup_from_task(current);
2849
2850 for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) {
2851 objcg = rcu_dereference(memcg->objcg);
2852 if (objcg && obj_cgroup_tryget(objcg))
2853 break;
2854 }
2855 rcu_read_unlock();
2856
2857 return objcg;
2858}
2859
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002860static int memcg_alloc_cache_id(void)
Glauber Costa55007d82012-12-18 14:22:38 -08002861{
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002862 int id, size;
2863 int err;
Glauber Costa55007d82012-12-18 14:22:38 -08002864
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002865 id = ida_simple_get(&memcg_cache_ida,
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002866 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2867 if (id < 0)
2868 return id;
2869
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002870 if (id < memcg_nr_cache_ids)
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002871 return id;
2872
2873 /*
2874 * There's no space for the new id in memcg_caches arrays,
2875 * so we have to grow them.
2876 */
Vladimir Davydov05257a12015-02-12 14:59:01 -08002877 down_write(&memcg_cache_ids_sem);
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002878
2879 size = 2 * (id + 1);
Glauber Costa55007d82012-12-18 14:22:38 -08002880 if (size < MEMCG_CACHES_MIN_SIZE)
2881 size = MEMCG_CACHES_MIN_SIZE;
2882 else if (size > MEMCG_CACHES_MAX_SIZE)
2883 size = MEMCG_CACHES_MAX_SIZE;
2884
Roman Gushchin98556092020-08-06 23:21:10 -07002885 err = memcg_update_all_list_lrus(size);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -08002886 if (!err)
Vladimir Davydov05257a12015-02-12 14:59:01 -08002887 memcg_nr_cache_ids = size;
2888
2889 up_write(&memcg_cache_ids_sem);
2890
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002891 if (err) {
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002892 ida_simple_remove(&memcg_cache_ida, id);
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002893 return err;
2894 }
2895 return id;
2896}
2897
2898static void memcg_free_cache_id(int id)
2899{
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002900 ida_simple_remove(&memcg_cache_ida, id);
Glauber Costa55007d82012-12-18 14:22:38 -08002901}
2902
Vladimir Davydov45264772016-07-26 15:24:21 -07002903/**
Roman Gushchin4b13f642020-04-01 21:06:56 -07002904 * __memcg_kmem_charge: charge a number of kernel pages to a memcg
Roman Gushchin10eaec22020-04-01 21:06:39 -07002905 * @memcg: memory cgroup to charge
Vladimir Davydov45264772016-07-26 15:24:21 -07002906 * @gfp: reclaim mode
Roman Gushchin92d0510c2020-04-01 21:06:49 -07002907 * @nr_pages: number of pages to charge
Vladimir Davydov45264772016-07-26 15:24:21 -07002908 *
2909 * Returns 0 on success, an error code on failure.
2910 */
Roman Gushchin4b13f642020-04-01 21:06:56 -07002911int __memcg_kmem_charge(struct mem_cgroup *memcg, gfp_t gfp,
2912 unsigned int nr_pages)
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002913{
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002914 struct page_counter *counter;
Johannes Weiner6071ca52015-11-05 18:50:26 -08002915 int ret;
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002916
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002917 ret = try_charge(memcg, gfp, nr_pages);
Johannes Weiner52c29b02016-01-20 15:02:35 -08002918 if (ret)
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002919 return ret;
Johannes Weiner52c29b02016-01-20 15:02:35 -08002920
2921 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) &&
2922 !page_counter_try_charge(&memcg->kmem, nr_pages, &counter)) {
Michal Hockoe55d9d92019-09-25 16:45:53 -07002923
2924 /*
2925 * Enforce __GFP_NOFAIL allocation because callers are not
2926 * prepared to see failures and likely do not have any failure
2927 * handling code.
2928 */
2929 if (gfp & __GFP_NOFAIL) {
2930 page_counter_charge(&memcg->kmem, nr_pages);
2931 return 0;
2932 }
Johannes Weiner52c29b02016-01-20 15:02:35 -08002933 cancel_charge(memcg, nr_pages);
2934 return -ENOMEM;
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002935 }
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002936 return 0;
2937}
2938
Vladimir Davydov45264772016-07-26 15:24:21 -07002939/**
Roman Gushchin4b13f642020-04-01 21:06:56 -07002940 * __memcg_kmem_uncharge: uncharge a number of kernel pages from a memcg
2941 * @memcg: memcg to uncharge
2942 * @nr_pages: number of pages to uncharge
2943 */
2944void __memcg_kmem_uncharge(struct mem_cgroup *memcg, unsigned int nr_pages)
2945{
2946 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
2947 page_counter_uncharge(&memcg->kmem, nr_pages);
2948
2949 page_counter_uncharge(&memcg->memory, nr_pages);
2950 if (do_memsw_account())
2951 page_counter_uncharge(&memcg->memsw, nr_pages);
2952}
2953
2954/**
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07002955 * __memcg_kmem_charge_page: charge a kmem page to the current memory cgroup
Vladimir Davydov45264772016-07-26 15:24:21 -07002956 * @page: page to charge
2957 * @gfp: reclaim mode
2958 * @order: allocation order
2959 *
2960 * Returns 0 on success, an error code on failure.
2961 */
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07002962int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002963{
2964 struct mem_cgroup *memcg;
Vladimir Davydovfcff7d72016-03-17 14:17:29 -07002965 int ret = 0;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002966
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08002967 if (memcg_kmem_bypass())
Vladimir Davydov45264772016-07-26 15:24:21 -07002968 return 0;
2969
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07002970 memcg = get_mem_cgroup_from_current();
Vladimir Davydovc4159a72016-08-08 23:03:12 +03002971 if (!mem_cgroup_is_root(memcg)) {
Roman Gushchin4b13f642020-04-01 21:06:56 -07002972 ret = __memcg_kmem_charge(memcg, gfp, 1 << order);
Roman Gushchin4d96ba32019-07-11 20:56:31 -07002973 if (!ret) {
2974 page->mem_cgroup = memcg;
Vladimir Davydovc4159a72016-08-08 23:03:12 +03002975 __SetPageKmemcg(page);
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07002976 return 0;
Roman Gushchin4d96ba32019-07-11 20:56:31 -07002977 }
Vladimir Davydovc4159a72016-08-08 23:03:12 +03002978 }
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002979 css_put(&memcg->css);
Vladimir Davydovd05e83a2015-11-05 18:48:59 -08002980 return ret;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002981}
Roman Gushchin49a18ea2019-07-11 20:56:13 -07002982
2983/**
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07002984 * __memcg_kmem_uncharge_page: uncharge a kmem page
Vladimir Davydov45264772016-07-26 15:24:21 -07002985 * @page: page to uncharge
2986 * @order: allocation order
2987 */
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07002988void __memcg_kmem_uncharge_page(struct page *page, int order)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002989{
Johannes Weiner1306a852014-12-10 15:44:52 -08002990 struct mem_cgroup *memcg = page->mem_cgroup;
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002991 unsigned int nr_pages = 1 << order;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002992
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002993 if (!memcg)
2994 return;
2995
Sasha Levin309381fea2014-01-23 15:52:54 -08002996 VM_BUG_ON_PAGE(mem_cgroup_is_root(memcg), page);
Roman Gushchin4b13f642020-04-01 21:06:56 -07002997 __memcg_kmem_uncharge(memcg, nr_pages);
Johannes Weiner1306a852014-12-10 15:44:52 -08002998 page->mem_cgroup = NULL;
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07002999 css_put(&memcg->css);
Vladimir Davydovc4159a72016-08-08 23:03:12 +03003000
3001 /* slab pages do not have PageKmemcg flag set */
3002 if (PageKmemcg(page))
3003 __ClearPageKmemcg(page);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -08003004}
Roman Gushchinbf4f0592020-08-06 23:20:49 -07003005
3006static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes)
3007{
3008 struct memcg_stock_pcp *stock;
3009 unsigned long flags;
3010 bool ret = false;
3011
3012 local_irq_save(flags);
3013
3014 stock = this_cpu_ptr(&memcg_stock);
3015 if (objcg == stock->cached_objcg && stock->nr_bytes >= nr_bytes) {
3016 stock->nr_bytes -= nr_bytes;
3017 ret = true;
3018 }
3019
3020 local_irq_restore(flags);
3021
3022 return ret;
3023}
3024
3025static void drain_obj_stock(struct memcg_stock_pcp *stock)
3026{
3027 struct obj_cgroup *old = stock->cached_objcg;
3028
3029 if (!old)
3030 return;
3031
3032 if (stock->nr_bytes) {
3033 unsigned int nr_pages = stock->nr_bytes >> PAGE_SHIFT;
3034 unsigned int nr_bytes = stock->nr_bytes & (PAGE_SIZE - 1);
3035
3036 if (nr_pages) {
3037 rcu_read_lock();
3038 __memcg_kmem_uncharge(obj_cgroup_memcg(old), nr_pages);
3039 rcu_read_unlock();
3040 }
3041
3042 /*
3043 * The leftover is flushed to the centralized per-memcg value.
3044 * On the next attempt to refill obj stock it will be moved
3045 * to a per-cpu stock (probably, on an other CPU), see
3046 * refill_obj_stock().
3047 *
3048 * How often it's flushed is a trade-off between the memory
3049 * limit enforcement accuracy and potential CPU contention,
3050 * so it might be changed in the future.
3051 */
3052 atomic_add(nr_bytes, &old->nr_charged_bytes);
3053 stock->nr_bytes = 0;
3054 }
3055
3056 obj_cgroup_put(old);
3057 stock->cached_objcg = NULL;
3058}
3059
3060static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
3061 struct mem_cgroup *root_memcg)
3062{
3063 struct mem_cgroup *memcg;
3064
3065 if (stock->cached_objcg) {
3066 memcg = obj_cgroup_memcg(stock->cached_objcg);
3067 if (memcg && mem_cgroup_is_descendant(memcg, root_memcg))
3068 return true;
3069 }
3070
3071 return false;
3072}
3073
3074static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes)
3075{
3076 struct memcg_stock_pcp *stock;
3077 unsigned long flags;
3078
3079 local_irq_save(flags);
3080
3081 stock = this_cpu_ptr(&memcg_stock);
3082 if (stock->cached_objcg != objcg) { /* reset if necessary */
3083 drain_obj_stock(stock);
3084 obj_cgroup_get(objcg);
3085 stock->cached_objcg = objcg;
3086 stock->nr_bytes = atomic_xchg(&objcg->nr_charged_bytes, 0);
3087 }
3088 stock->nr_bytes += nr_bytes;
3089
3090 if (stock->nr_bytes > PAGE_SIZE)
3091 drain_obj_stock(stock);
3092
3093 local_irq_restore(flags);
3094}
3095
3096int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size)
3097{
3098 struct mem_cgroup *memcg;
3099 unsigned int nr_pages, nr_bytes;
3100 int ret;
3101
3102 if (consume_obj_stock(objcg, size))
3103 return 0;
3104
3105 /*
3106 * In theory, memcg->nr_charged_bytes can have enough
3107 * pre-charged bytes to satisfy the allocation. However,
3108 * flushing memcg->nr_charged_bytes requires two atomic
3109 * operations, and memcg->nr_charged_bytes can't be big,
3110 * so it's better to ignore it and try grab some new pages.
3111 * memcg->nr_charged_bytes will be flushed in
3112 * refill_obj_stock(), called from this function or
3113 * independently later.
3114 */
3115 rcu_read_lock();
3116 memcg = obj_cgroup_memcg(objcg);
3117 css_get(&memcg->css);
3118 rcu_read_unlock();
3119
3120 nr_pages = size >> PAGE_SHIFT;
3121 nr_bytes = size & (PAGE_SIZE - 1);
3122
3123 if (nr_bytes)
3124 nr_pages += 1;
3125
3126 ret = __memcg_kmem_charge(memcg, gfp, nr_pages);
3127 if (!ret && nr_bytes)
3128 refill_obj_stock(objcg, PAGE_SIZE - nr_bytes);
3129
3130 css_put(&memcg->css);
3131 return ret;
3132}
3133
3134void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size)
3135{
3136 refill_obj_stock(objcg, size);
3137}
3138
Kirill Tkhai84c07d12018-08-17 15:47:25 -07003139#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003140
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003141#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3142
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003143/*
3144 * Because tail pages are not marked as "used", set it. We're under
Andrey Ryabininf4b7e272019-03-05 15:49:39 -08003145 * pgdat->lru_lock and migration entries setup in all page mappings.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003146 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003147void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003148{
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07003149 struct mem_cgroup *memcg = head->mem_cgroup;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003150 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003151
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003152 if (mem_cgroup_disabled())
3153 return;
David Rientjesb070e652013-05-07 16:18:09 -07003154
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07003155 for (i = 1; i < HPAGE_PMD_NR; i++) {
3156 css_get(&memcg->css);
3157 head[i].mem_cgroup = memcg;
3158 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003159}
Hugh Dickins12d27102012-01-12 17:19:52 -08003160#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003161
Andrew Mortonc255a452012-07-31 16:43:02 -07003162#ifdef CONFIG_MEMCG_SWAP
Daisuke Nishimura02491442010-03-10 15:22:17 -08003163/**
3164 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3165 * @entry: swap entry to be moved
3166 * @from: mem_cgroup which the entry is moved from
3167 * @to: mem_cgroup which the entry is moved to
3168 *
3169 * It succeeds only when the swap_cgroup's record for this entry is the same
3170 * as the mem_cgroup's id of @from.
3171 *
3172 * Returns 0 on success, -EINVAL on failure.
3173 *
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003174 * The caller must have charged to @to, IOW, called page_counter_charge() about
Daisuke Nishimura02491442010-03-10 15:22:17 -08003175 * both res and memsw, and called css_get().
3176 */
3177static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003178 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003179{
3180 unsigned short old_id, new_id;
3181
Li Zefan34c00c32013-09-23 16:56:01 +08003182 old_id = mem_cgroup_id(from);
3183 new_id = mem_cgroup_id(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003184
3185 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Johannes Weinerc9019e92018-01-31 16:16:37 -08003186 mod_memcg_state(from, MEMCG_SWAP, -1);
3187 mod_memcg_state(to, MEMCG_SWAP, 1);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003188 return 0;
3189 }
3190 return -EINVAL;
3191}
3192#else
3193static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003194 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003195{
3196 return -EINVAL;
3197}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003198#endif
3199
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003200static DEFINE_MUTEX(memcg_max_mutex);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003201
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003202static int mem_cgroup_resize_max(struct mem_cgroup *memcg,
3203 unsigned long max, bool memsw)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003204{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003205 bool enlarge = false;
Shakeel Buttbb4a7ea2018-06-07 17:07:27 -07003206 bool drained = false;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003207 int ret;
Yu Zhaoc054a782018-01-31 16:20:02 -08003208 bool limits_invariant;
3209 struct page_counter *counter = memsw ? &memcg->memsw : &memcg->memory;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003210
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003211 do {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003212 if (signal_pending(current)) {
3213 ret = -EINTR;
3214 break;
3215 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003216
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003217 mutex_lock(&memcg_max_mutex);
Yu Zhaoc054a782018-01-31 16:20:02 -08003218 /*
3219 * Make sure that the new limit (memsw or memory limit) doesn't
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003220 * break our basic invariant rule memory.max <= memsw.max.
Yu Zhaoc054a782018-01-31 16:20:02 -08003221 */
Chris Down15b42562020-04-01 21:07:20 -07003222 limits_invariant = memsw ? max >= READ_ONCE(memcg->memory.max) :
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003223 max <= memcg->memsw.max;
Yu Zhaoc054a782018-01-31 16:20:02 -08003224 if (!limits_invariant) {
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003225 mutex_unlock(&memcg_max_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003226 ret = -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003227 break;
3228 }
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003229 if (max > counter->max)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003230 enlarge = true;
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003231 ret = page_counter_set_max(counter, max);
3232 mutex_unlock(&memcg_max_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003233
3234 if (!ret)
3235 break;
3236
Shakeel Buttbb4a7ea2018-06-07 17:07:27 -07003237 if (!drained) {
3238 drain_all_stock(memcg);
3239 drained = true;
3240 continue;
3241 }
3242
Andrey Ryabinin1ab5c052018-01-31 16:20:37 -08003243 if (!try_to_free_mem_cgroup_pages(memcg, 1,
3244 GFP_KERNEL, !memsw)) {
3245 ret = -EBUSY;
3246 break;
3247 }
3248 } while (true);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003249
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003250 if (!ret && enlarge)
3251 memcg_oom_recover(memcg);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003252
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003253 return ret;
3254}
3255
Mel Gormanef8f2322016-07-28 15:46:05 -07003256unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
Andrew Morton0608f432013-09-24 15:27:41 -07003257 gfp_t gfp_mask,
3258 unsigned long *total_scanned)
3259{
3260 unsigned long nr_reclaimed = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07003261 struct mem_cgroup_per_node *mz, *next_mz = NULL;
Andrew Morton0608f432013-09-24 15:27:41 -07003262 unsigned long reclaimed;
3263 int loop = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07003264 struct mem_cgroup_tree_per_node *mctz;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003265 unsigned long excess;
Andrew Morton0608f432013-09-24 15:27:41 -07003266 unsigned long nr_scanned;
3267
3268 if (order > 0)
3269 return 0;
3270
Mel Gormanef8f2322016-07-28 15:46:05 -07003271 mctz = soft_limit_tree_node(pgdat->node_id);
Michal Hockod6507ff2016-08-02 14:02:37 -07003272
3273 /*
3274 * Do not even bother to check the largest node if the root
3275 * is empty. Do it lockless to prevent lock bouncing. Races
3276 * are acceptable as soft limit is best effort anyway.
3277 */
Laurent Dufourbfc72282017-03-09 16:17:06 -08003278 if (!mctz || RB_EMPTY_ROOT(&mctz->rb_root))
Michal Hockod6507ff2016-08-02 14:02:37 -07003279 return 0;
3280
Andrew Morton0608f432013-09-24 15:27:41 -07003281 /*
3282 * This loop can run a while, specially if mem_cgroup's continuously
3283 * keep exceeding their soft limit and putting the system under
3284 * pressure
3285 */
3286 do {
3287 if (next_mz)
3288 mz = next_mz;
3289 else
3290 mz = mem_cgroup_largest_soft_limit_node(mctz);
3291 if (!mz)
3292 break;
3293
3294 nr_scanned = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07003295 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, pgdat,
Andrew Morton0608f432013-09-24 15:27:41 -07003296 gfp_mask, &nr_scanned);
3297 nr_reclaimed += reclaimed;
3298 *total_scanned += nr_scanned;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003299 spin_lock_irq(&mctz->lock);
Vladimir Davydovbc2f2e72014-12-10 15:43:40 -08003300 __mem_cgroup_remove_exceeded(mz, mctz);
Andrew Morton0608f432013-09-24 15:27:41 -07003301
3302 /*
3303 * If we failed to reclaim anything from this memory cgroup
3304 * it is time to move on to the next cgroup
3305 */
3306 next_mz = NULL;
Vladimir Davydovbc2f2e72014-12-10 15:43:40 -08003307 if (!reclaimed)
3308 next_mz = __mem_cgroup_largest_soft_limit_node(mctz);
3309
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003310 excess = soft_limit_excess(mz->memcg);
Andrew Morton0608f432013-09-24 15:27:41 -07003311 /*
3312 * One school of thought says that we should not add
3313 * back the node to the tree if reclaim returns 0.
3314 * But our reclaim could return 0, simply because due
3315 * to priority we are exposing a smaller subset of
3316 * memory to reclaim from. Consider this as a longer
3317 * term TODO.
3318 */
3319 /* If excess == 0, no tree ops */
Johannes Weinercf2c8122014-06-06 14:38:21 -07003320 __mem_cgroup_insert_exceeded(mz, mctz, excess);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003321 spin_unlock_irq(&mctz->lock);
Andrew Morton0608f432013-09-24 15:27:41 -07003322 css_put(&mz->memcg->css);
3323 loop++;
3324 /*
3325 * Could not reclaim anything and there are no more
3326 * mem cgroups to try or we seem to be looping without
3327 * reclaiming anything.
3328 */
3329 if (!nr_reclaimed &&
3330 (next_mz == NULL ||
3331 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3332 break;
3333 } while (!nr_reclaimed);
3334 if (next_mz)
3335 css_put(&next_mz->memcg->css);
3336 return nr_reclaimed;
3337}
3338
Tejun Heoea280e72014-05-16 13:22:48 -04003339/*
3340 * Test whether @memcg has children, dead or alive. Note that this
3341 * function doesn't care whether @memcg has use_hierarchy enabled and
3342 * returns %true if there are child csses according to the cgroup
Ethon Paulb8f29352020-06-04 16:49:28 -07003343 * hierarchy. Testing use_hierarchy is the caller's responsibility.
Tejun Heoea280e72014-05-16 13:22:48 -04003344 */
Glauber Costab5f99b52013-02-22 16:34:53 -08003345static inline bool memcg_has_children(struct mem_cgroup *memcg)
3346{
Tejun Heoea280e72014-05-16 13:22:48 -04003347 bool ret;
3348
Tejun Heoea280e72014-05-16 13:22:48 -04003349 rcu_read_lock();
3350 ret = css_next_child(NULL, &memcg->css);
3351 rcu_read_unlock();
3352 return ret;
Glauber Costab5f99b52013-02-22 16:34:53 -08003353}
3354
3355/*
Greg Thelen51038172016-05-20 16:58:18 -07003356 * Reclaims as many pages from the given memcg as possible.
Michal Hockoc26251f2012-10-26 13:37:28 +02003357 *
3358 * Caller is responsible for holding css reference for memcg.
3359 */
3360static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
3361{
3362 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Michal Hockoc26251f2012-10-26 13:37:28 +02003363
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003364 /* we call try-to-free pages for make this cgroup empty */
3365 lru_add_drain_all();
Junaid Shahidd12c60f2018-06-07 17:07:31 -07003366
3367 drain_all_stock(memcg);
3368
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003369 /* try to free all pages in this cgroup */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003370 while (nr_retries && page_counter_read(&memcg->memory)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003371 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003372
Michal Hockoc26251f2012-10-26 13:37:28 +02003373 if (signal_pending(current))
3374 return -EINTR;
3375
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003376 progress = try_to_free_mem_cgroup_pages(memcg, 1,
3377 GFP_KERNEL, true);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003378 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003379 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003380 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003381 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003382 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003383
3384 }
Michal Hockoab5196c2012-10-26 13:37:32 +02003385
3386 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003387}
3388
Tejun Heo6770c642014-05-13 12:16:21 -04003389static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of,
3390 char *buf, size_t nbytes,
3391 loff_t off)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003392{
Tejun Heo6770c642014-05-13 12:16:21 -04003393 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Michal Hockoc26251f2012-10-26 13:37:28 +02003394
Michal Hockod8423012012-10-26 13:37:29 +02003395 if (mem_cgroup_is_root(memcg))
3396 return -EINVAL;
Tejun Heo6770c642014-05-13 12:16:21 -04003397 return mem_cgroup_force_empty(memcg) ?: nbytes;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003398}
3399
Tejun Heo182446d2013-08-08 20:11:24 -04003400static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
3401 struct cftype *cft)
Balbir Singh18f59ea2009-01-07 18:08:07 -08003402{
Tejun Heo182446d2013-08-08 20:11:24 -04003403 return mem_cgroup_from_css(css)->use_hierarchy;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003404}
3405
Tejun Heo182446d2013-08-08 20:11:24 -04003406static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
3407 struct cftype *cft, u64 val)
Balbir Singh18f59ea2009-01-07 18:08:07 -08003408{
3409 int retval = 0;
Tejun Heo182446d2013-08-08 20:11:24 -04003410 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04003411 struct mem_cgroup *parent_memcg = mem_cgroup_from_css(memcg->css.parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003412
Glauber Costa567fb432012-07-31 16:43:07 -07003413 if (memcg->use_hierarchy == val)
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08003414 return 0;
Glauber Costa567fb432012-07-31 16:43:07 -07003415
Balbir Singh18f59ea2009-01-07 18:08:07 -08003416 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003417 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003418 * in the child subtrees. If it is unset, then the change can
3419 * occur, provided the current cgroup has no children.
3420 *
3421 * For the root cgroup, parent_mem is NULL, we allow value to be
3422 * set if there are no children.
3423 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003424 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003425 (val == 1 || val == 0)) {
Tejun Heoea280e72014-05-16 13:22:48 -04003426 if (!memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003427 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003428 else
3429 retval = -EBUSY;
3430 } else
3431 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07003432
Balbir Singh18f59ea2009-01-07 18:08:07 -08003433 return retval;
3434}
3435
Andrew Morton6f646152015-11-06 16:28:58 -08003436static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Johannes Weinerce00a962014-09-05 08:43:57 -04003437{
Johannes Weiner42a30032019-05-14 15:47:12 -07003438 unsigned long val;
Johannes Weinerce00a962014-09-05 08:43:57 -04003439
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003440 if (mem_cgroup_is_root(memcg)) {
Johannes Weiner0d1c2072020-06-03 16:01:54 -07003441 val = memcg_page_state(memcg, NR_FILE_PAGES) +
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07003442 memcg_page_state(memcg, NR_ANON_MAPPED);
Johannes Weiner42a30032019-05-14 15:47:12 -07003443 if (swap)
3444 val += memcg_page_state(memcg, MEMCG_SWAP);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003445 } else {
Johannes Weinerce00a962014-09-05 08:43:57 -04003446 if (!swap)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003447 val = page_counter_read(&memcg->memory);
Johannes Weinerce00a962014-09-05 08:43:57 -04003448 else
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003449 val = page_counter_read(&memcg->memsw);
Johannes Weinerce00a962014-09-05 08:43:57 -04003450 }
Michal Hockoc12176d2015-11-05 18:50:29 -08003451 return val;
Johannes Weinerce00a962014-09-05 08:43:57 -04003452}
3453
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003454enum {
3455 RES_USAGE,
3456 RES_LIMIT,
3457 RES_MAX_USAGE,
3458 RES_FAILCNT,
3459 RES_SOFT_LIMIT,
3460};
Johannes Weinerce00a962014-09-05 08:43:57 -04003461
Tejun Heo791badb2013-12-05 12:28:02 -05003462static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
Johannes Weiner05b84302014-08-06 16:05:59 -07003463 struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003464{
Tejun Heo182446d2013-08-08 20:11:24 -04003465 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003466 struct page_counter *counter;
Tejun Heoaf36f902012-04-01 12:09:55 -07003467
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003468 switch (MEMFILE_TYPE(cft->private)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003469 case _MEM:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003470 counter = &memcg->memory;
Glauber Costa510fc4e2012-12-18 14:21:47 -08003471 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003472 case _MEMSWAP:
3473 counter = &memcg->memsw;
3474 break;
3475 case _KMEM:
3476 counter = &memcg->kmem;
3477 break;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003478 case _TCP:
Johannes Weiner0db15292016-01-20 15:02:50 -08003479 counter = &memcg->tcpmem;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003480 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003481 default:
3482 BUG();
3483 }
3484
3485 switch (MEMFILE_ATTR(cft->private)) {
3486 case RES_USAGE:
3487 if (counter == &memcg->memory)
Michal Hockoc12176d2015-11-05 18:50:29 -08003488 return (u64)mem_cgroup_usage(memcg, false) * PAGE_SIZE;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003489 if (counter == &memcg->memsw)
Michal Hockoc12176d2015-11-05 18:50:29 -08003490 return (u64)mem_cgroup_usage(memcg, true) * PAGE_SIZE;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003491 return (u64)page_counter_read(counter) * PAGE_SIZE;
3492 case RES_LIMIT:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003493 return (u64)counter->max * PAGE_SIZE;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003494 case RES_MAX_USAGE:
3495 return (u64)counter->watermark * PAGE_SIZE;
3496 case RES_FAILCNT:
3497 return counter->failcnt;
3498 case RES_SOFT_LIMIT:
3499 return (u64)memcg->soft_limit * PAGE_SIZE;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003500 default:
3501 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003502 }
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003503}
Glauber Costa510fc4e2012-12-18 14:21:47 -08003504
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08003505static void memcg_flush_percpu_vmstats(struct mem_cgroup *memcg)
Roman Gushchinc350a992019-08-24 17:54:47 -07003506{
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08003507 unsigned long stat[MEMCG_NR_STAT] = {0};
Roman Gushchinc350a992019-08-24 17:54:47 -07003508 struct mem_cgroup *mi;
3509 int node, cpu, i;
Roman Gushchinc350a992019-08-24 17:54:47 -07003510
3511 for_each_online_cpu(cpu)
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08003512 for (i = 0; i < MEMCG_NR_STAT; i++)
Shakeel Butt6c1c2802019-08-30 16:04:53 -07003513 stat[i] += per_cpu(memcg->vmstats_percpu->stat[i], cpu);
Roman Gushchinc350a992019-08-24 17:54:47 -07003514
3515 for (mi = memcg; mi; mi = parent_mem_cgroup(mi))
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08003516 for (i = 0; i < MEMCG_NR_STAT; i++)
Roman Gushchinc350a992019-08-24 17:54:47 -07003517 atomic_long_add(stat[i], &mi->vmstats[i]);
3518
3519 for_each_node(node) {
3520 struct mem_cgroup_per_node *pn = memcg->nodeinfo[node];
3521 struct mem_cgroup_per_node *pi;
3522
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08003523 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
Roman Gushchinc350a992019-08-24 17:54:47 -07003524 stat[i] = 0;
3525
3526 for_each_online_cpu(cpu)
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08003527 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
Shakeel Butt6c1c2802019-08-30 16:04:53 -07003528 stat[i] += per_cpu(
3529 pn->lruvec_stat_cpu->count[i], cpu);
Roman Gushchinc350a992019-08-24 17:54:47 -07003530
3531 for (pi = pn; pi; pi = parent_nodeinfo(pi, node))
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08003532 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
Roman Gushchinc350a992019-08-24 17:54:47 -07003533 atomic_long_add(stat[i], &pi->lruvec_stat[i]);
3534 }
3535}
3536
Roman Gushchinbb65f892019-08-24 17:54:50 -07003537static void memcg_flush_percpu_vmevents(struct mem_cgroup *memcg)
3538{
3539 unsigned long events[NR_VM_EVENT_ITEMS];
3540 struct mem_cgroup *mi;
3541 int cpu, i;
3542
3543 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
3544 events[i] = 0;
3545
3546 for_each_online_cpu(cpu)
3547 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
Shakeel Butt6c1c2802019-08-30 16:04:53 -07003548 events[i] += per_cpu(memcg->vmstats_percpu->events[i],
3549 cpu);
Roman Gushchinbb65f892019-08-24 17:54:50 -07003550
3551 for (mi = memcg; mi; mi = parent_mem_cgroup(mi))
3552 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
3553 atomic_long_add(events[i], &mi->vmevents[i]);
3554}
3555
Kirill Tkhai84c07d12018-08-17 15:47:25 -07003556#ifdef CONFIG_MEMCG_KMEM
Johannes Weiner567e9ab2016-01-20 15:02:24 -08003557static int memcg_online_kmem(struct mem_cgroup *memcg)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003558{
Roman Gushchinbf4f0592020-08-06 23:20:49 -07003559 struct obj_cgroup *objcg;
Vladimir Davydovd6441632014-01-23 15:53:09 -08003560 int memcg_id;
3561
Vladimir Davydovb313aee2016-03-17 14:18:27 -07003562 if (cgroup_memory_nokmem)
3563 return 0;
3564
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003565 BUG_ON(memcg->kmemcg_id >= 0);
Johannes Weiner567e9ab2016-01-20 15:02:24 -08003566 BUG_ON(memcg->kmem_state);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003567
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07003568 memcg_id = memcg_alloc_cache_id();
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08003569 if (memcg_id < 0)
3570 return memcg_id;
Vladimir Davydovd6441632014-01-23 15:53:09 -08003571
Roman Gushchinbf4f0592020-08-06 23:20:49 -07003572 objcg = obj_cgroup_alloc();
3573 if (!objcg) {
3574 memcg_free_cache_id(memcg_id);
3575 return -ENOMEM;
3576 }
3577 objcg->memcg = memcg;
3578 rcu_assign_pointer(memcg->objcg, objcg);
3579
Roman Gushchind648bcc2020-08-06 23:20:28 -07003580 static_branch_enable(&memcg_kmem_enabled_key);
3581
Vladimir Davydovd6441632014-01-23 15:53:09 -08003582 /*
Johannes Weiner567e9ab2016-01-20 15:02:24 -08003583 * A memory cgroup is considered kmem-online as soon as it gets
Vladimir Davydov900a38f2014-12-12 16:55:10 -08003584 * kmemcg_id. Setting the id after enabling static branching will
Vladimir Davydovd6441632014-01-23 15:53:09 -08003585 * guarantee no one starts accounting before all call sites are
3586 * patched.
3587 */
Vladimir Davydov900a38f2014-12-12 16:55:10 -08003588 memcg->kmemcg_id = memcg_id;
Johannes Weiner567e9ab2016-01-20 15:02:24 -08003589 memcg->kmem_state = KMEM_ONLINE;
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08003590
3591 return 0;
Vladimir Davydovd6441632014-01-23 15:53:09 -08003592}
3593
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003594static void memcg_offline_kmem(struct mem_cgroup *memcg)
3595{
3596 struct cgroup_subsys_state *css;
3597 struct mem_cgroup *parent, *child;
3598 int kmemcg_id;
3599
3600 if (memcg->kmem_state != KMEM_ONLINE)
3601 return;
Roman Gushchin98556092020-08-06 23:21:10 -07003602
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003603 memcg->kmem_state = KMEM_ALLOCATED;
3604
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003605 parent = parent_mem_cgroup(memcg);
3606 if (!parent)
3607 parent = root_mem_cgroup;
3608
Roman Gushchinbf4f0592020-08-06 23:20:49 -07003609 memcg_reparent_objcgs(memcg, parent);
Roman Gushchinfb2f2b02019-07-11 20:56:34 -07003610
3611 kmemcg_id = memcg->kmemcg_id;
3612 BUG_ON(kmemcg_id < 0);
3613
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003614 /*
3615 * Change kmemcg_id of this cgroup and all its descendants to the
3616 * parent's id, and then move all entries from this cgroup's list_lrus
3617 * to ones of the parent. After we have finished, all list_lrus
3618 * corresponding to this cgroup are guaranteed to remain empty. The
3619 * ordering is imposed by list_lru_node->lock taken by
3620 * memcg_drain_all_list_lrus().
3621 */
Tejun Heo3a06bb72016-06-03 14:55:44 -07003622 rcu_read_lock(); /* can be called from css_free w/o cgroup_mutex */
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003623 css_for_each_descendant_pre(css, &memcg->css) {
3624 child = mem_cgroup_from_css(css);
3625 BUG_ON(child->kmemcg_id != kmemcg_id);
3626 child->kmemcg_id = parent->kmemcg_id;
3627 if (!memcg->use_hierarchy)
3628 break;
3629 }
Tejun Heo3a06bb72016-06-03 14:55:44 -07003630 rcu_read_unlock();
3631
Kirill Tkhai9bec5c32018-08-17 15:47:58 -07003632 memcg_drain_all_list_lrus(kmemcg_id, parent);
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003633
3634 memcg_free_cache_id(kmemcg_id);
3635}
3636
3637static void memcg_free_kmem(struct mem_cgroup *memcg)
3638{
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08003639 /* css_alloc() failed, offlining didn't happen */
3640 if (unlikely(memcg->kmem_state == KMEM_ONLINE))
3641 memcg_offline_kmem(memcg);
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003642}
Vladimir Davydovd6441632014-01-23 15:53:09 -08003643#else
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08003644static int memcg_online_kmem(struct mem_cgroup *memcg)
Johannes Weiner127424c2016-01-20 15:02:32 -08003645{
3646 return 0;
3647}
3648static void memcg_offline_kmem(struct mem_cgroup *memcg)
3649{
3650}
3651static void memcg_free_kmem(struct mem_cgroup *memcg)
3652{
3653}
Kirill Tkhai84c07d12018-08-17 15:47:25 -07003654#endif /* CONFIG_MEMCG_KMEM */
Johannes Weiner127424c2016-01-20 15:02:32 -08003655
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003656static int memcg_update_kmem_max(struct mem_cgroup *memcg,
3657 unsigned long max)
Johannes Weiner127424c2016-01-20 15:02:32 -08003658{
Vladimir Davydovb313aee2016-03-17 14:18:27 -07003659 int ret;
Johannes Weiner127424c2016-01-20 15:02:32 -08003660
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003661 mutex_lock(&memcg_max_mutex);
3662 ret = page_counter_set_max(&memcg->kmem, max);
3663 mutex_unlock(&memcg_max_mutex);
Johannes Weiner127424c2016-01-20 15:02:32 -08003664 return ret;
3665}
Glauber Costa510fc4e2012-12-18 14:21:47 -08003666
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003667static int memcg_update_tcp_max(struct mem_cgroup *memcg, unsigned long max)
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003668{
3669 int ret;
3670
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003671 mutex_lock(&memcg_max_mutex);
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003672
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003673 ret = page_counter_set_max(&memcg->tcpmem, max);
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003674 if (ret)
3675 goto out;
3676
Johannes Weiner0db15292016-01-20 15:02:50 -08003677 if (!memcg->tcpmem_active) {
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003678 /*
3679 * The active flag needs to be written after the static_key
3680 * update. This is what guarantees that the socket activation
Johannes Weiner2d758072016-10-07 17:00:58 -07003681 * function is the last one to run. See mem_cgroup_sk_alloc()
3682 * for details, and note that we don't mark any socket as
3683 * belonging to this memcg until that flag is up.
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003684 *
3685 * We need to do this, because static_keys will span multiple
3686 * sites, but we can't control their order. If we mark a socket
3687 * as accounted, but the accounting functions are not patched in
3688 * yet, we'll lose accounting.
3689 *
Johannes Weiner2d758072016-10-07 17:00:58 -07003690 * We never race with the readers in mem_cgroup_sk_alloc(),
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003691 * because when this value change, the code to process it is not
3692 * patched in yet.
3693 */
3694 static_branch_inc(&memcg_sockets_enabled_key);
Johannes Weiner0db15292016-01-20 15:02:50 -08003695 memcg->tcpmem_active = true;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003696 }
3697out:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003698 mutex_unlock(&memcg_max_mutex);
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003699 return ret;
3700}
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003701
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003702/*
3703 * The user of this function is...
3704 * RES_LIMIT.
3705 */
Tejun Heo451af502014-05-13 12:16:21 -04003706static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
3707 char *buf, size_t nbytes, loff_t off)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003708{
Tejun Heo451af502014-05-13 12:16:21 -04003709 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003710 unsigned long nr_pages;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003711 int ret;
3712
Tejun Heo451af502014-05-13 12:16:21 -04003713 buf = strstrip(buf);
Johannes Weiner650c5e52015-02-11 15:26:03 -08003714 ret = page_counter_memparse(buf, "-1", &nr_pages);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003715 if (ret)
3716 return ret;
Tejun Heoaf36f902012-04-01 12:09:55 -07003717
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003718 switch (MEMFILE_ATTR(of_cft(of)->private)) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003719 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003720 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3721 ret = -EINVAL;
3722 break;
3723 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003724 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3725 case _MEM:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003726 ret = mem_cgroup_resize_max(memcg, nr_pages, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003727 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003728 case _MEMSWAP:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003729 ret = mem_cgroup_resize_max(memcg, nr_pages, true);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003730 break;
3731 case _KMEM:
Michal Hocko01581152019-09-23 15:37:22 -07003732 pr_warn_once("kmem.limit_in_bytes is deprecated and will be removed. "
3733 "Please report your usecase to linux-mm@kvack.org if you "
3734 "depend on this functionality.\n");
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003735 ret = memcg_update_kmem_max(memcg, nr_pages);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003736 break;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003737 case _TCP:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003738 ret = memcg_update_tcp_max(memcg, nr_pages);
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003739 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003740 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003741 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003742 case RES_SOFT_LIMIT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003743 memcg->soft_limit = nr_pages;
3744 ret = 0;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003745 break;
3746 }
Tejun Heo451af502014-05-13 12:16:21 -04003747 return ret ?: nbytes;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003748}
3749
Tejun Heo6770c642014-05-13 12:16:21 -04003750static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf,
3751 size_t nbytes, loff_t off)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003752{
Tejun Heo6770c642014-05-13 12:16:21 -04003753 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003754 struct page_counter *counter;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003755
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003756 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3757 case _MEM:
3758 counter = &memcg->memory;
3759 break;
3760 case _MEMSWAP:
3761 counter = &memcg->memsw;
3762 break;
3763 case _KMEM:
3764 counter = &memcg->kmem;
3765 break;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003766 case _TCP:
Johannes Weiner0db15292016-01-20 15:02:50 -08003767 counter = &memcg->tcpmem;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003768 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003769 default:
3770 BUG();
3771 }
Tejun Heoaf36f902012-04-01 12:09:55 -07003772
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003773 switch (MEMFILE_ATTR(of_cft(of)->private)) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003774 case RES_MAX_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003775 page_counter_reset_watermark(counter);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003776 break;
3777 case RES_FAILCNT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003778 counter->failcnt = 0;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003779 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003780 default:
3781 BUG();
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003782 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003783
Tejun Heo6770c642014-05-13 12:16:21 -04003784 return nbytes;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003785}
3786
Tejun Heo182446d2013-08-08 20:11:24 -04003787static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003788 struct cftype *cft)
3789{
Tejun Heo182446d2013-08-08 20:11:24 -04003790 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003791}
3792
Daisuke Nishimura02491442010-03-10 15:22:17 -08003793#ifdef CONFIG_MMU
Tejun Heo182446d2013-08-08 20:11:24 -04003794static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003795 struct cftype *cft, u64 val)
3796{
Tejun Heo182446d2013-08-08 20:11:24 -04003797 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003798
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08003799 if (val & ~MOVE_MASK)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003800 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003801
Glauber Costaee5e8472013-02-22 16:34:50 -08003802 /*
3803 * No kind of locking is needed in here, because ->can_attach() will
3804 * check this value once in the beginning of the process, and then carry
3805 * on with stale data. This means that changes to this value will only
3806 * affect task migrations starting after the change.
3807 */
3808 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003809 return 0;
3810}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003811#else
Tejun Heo182446d2013-08-08 20:11:24 -04003812static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura02491442010-03-10 15:22:17 -08003813 struct cftype *cft, u64 val)
3814{
3815 return -ENOSYS;
3816}
3817#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003818
Ying Han406eb0c2011-05-26 16:25:37 -07003819#ifdef CONFIG_NUMA
Johannes Weiner113b7df2019-05-13 17:18:11 -07003820
3821#define LRU_ALL_FILE (BIT(LRU_INACTIVE_FILE) | BIT(LRU_ACTIVE_FILE))
3822#define LRU_ALL_ANON (BIT(LRU_INACTIVE_ANON) | BIT(LRU_ACTIVE_ANON))
3823#define LRU_ALL ((1 << NR_LRU_LISTS) - 1)
3824
3825static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
Shakeel Buttdd8657b2020-06-03 15:56:24 -07003826 int nid, unsigned int lru_mask, bool tree)
Johannes Weiner113b7df2019-05-13 17:18:11 -07003827{
Johannes Weiner867e5e12019-11-30 17:55:34 -08003828 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid));
Johannes Weiner113b7df2019-05-13 17:18:11 -07003829 unsigned long nr = 0;
3830 enum lru_list lru;
3831
3832 VM_BUG_ON((unsigned)nid >= nr_node_ids);
3833
3834 for_each_lru(lru) {
3835 if (!(BIT(lru) & lru_mask))
3836 continue;
Shakeel Buttdd8657b2020-06-03 15:56:24 -07003837 if (tree)
3838 nr += lruvec_page_state(lruvec, NR_LRU_BASE + lru);
3839 else
3840 nr += lruvec_page_state_local(lruvec, NR_LRU_BASE + lru);
Johannes Weiner113b7df2019-05-13 17:18:11 -07003841 }
3842 return nr;
3843}
3844
3845static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
Shakeel Buttdd8657b2020-06-03 15:56:24 -07003846 unsigned int lru_mask,
3847 bool tree)
Johannes Weiner113b7df2019-05-13 17:18:11 -07003848{
3849 unsigned long nr = 0;
3850 enum lru_list lru;
3851
3852 for_each_lru(lru) {
3853 if (!(BIT(lru) & lru_mask))
3854 continue;
Shakeel Buttdd8657b2020-06-03 15:56:24 -07003855 if (tree)
3856 nr += memcg_page_state(memcg, NR_LRU_BASE + lru);
3857 else
3858 nr += memcg_page_state_local(memcg, NR_LRU_BASE + lru);
Johannes Weiner113b7df2019-05-13 17:18:11 -07003859 }
3860 return nr;
3861}
3862
Tejun Heo2da8ca82013-12-05 12:28:04 -05003863static int memcg_numa_stat_show(struct seq_file *m, void *v)
Ying Han406eb0c2011-05-26 16:25:37 -07003864{
Greg Thelen25485de2013-11-12 15:07:40 -08003865 struct numa_stat {
3866 const char *name;
3867 unsigned int lru_mask;
3868 };
3869
3870 static const struct numa_stat stats[] = {
3871 { "total", LRU_ALL },
3872 { "file", LRU_ALL_FILE },
3873 { "anon", LRU_ALL_ANON },
3874 { "unevictable", BIT(LRU_UNEVICTABLE) },
3875 };
3876 const struct numa_stat *stat;
Ying Han406eb0c2011-05-26 16:25:37 -07003877 int nid;
Chris Downaa9694b2019-03-05 15:45:52 -08003878 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Ying Han406eb0c2011-05-26 16:25:37 -07003879
Greg Thelen25485de2013-11-12 15:07:40 -08003880 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
Shakeel Buttdd8657b2020-06-03 15:56:24 -07003881 seq_printf(m, "%s=%lu", stat->name,
3882 mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
3883 false));
3884 for_each_node_state(nid, N_MEMORY)
3885 seq_printf(m, " N%d=%lu", nid,
3886 mem_cgroup_node_nr_lru_pages(memcg, nid,
3887 stat->lru_mask, false));
Greg Thelen25485de2013-11-12 15:07:40 -08003888 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07003889 }
Ying Han406eb0c2011-05-26 16:25:37 -07003890
Ying Han071aee12013-11-12 15:07:41 -08003891 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
Ying Han406eb0c2011-05-26 16:25:37 -07003892
Shakeel Buttdd8657b2020-06-03 15:56:24 -07003893 seq_printf(m, "hierarchical_%s=%lu", stat->name,
3894 mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
3895 true));
3896 for_each_node_state(nid, N_MEMORY)
3897 seq_printf(m, " N%d=%lu", nid,
3898 mem_cgroup_node_nr_lru_pages(memcg, nid,
3899 stat->lru_mask, true));
Ying Han071aee12013-11-12 15:07:41 -08003900 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07003901 }
Ying Han406eb0c2011-05-26 16:25:37 -07003902
Ying Han406eb0c2011-05-26 16:25:37 -07003903 return 0;
3904}
3905#endif /* CONFIG_NUMA */
3906
Johannes Weinerc8713d02019-07-11 20:55:59 -07003907static const unsigned int memcg1_stats[] = {
Johannes Weiner0d1c2072020-06-03 16:01:54 -07003908 NR_FILE_PAGES,
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07003909 NR_ANON_MAPPED,
Johannes Weiner468c3982020-06-03 16:02:01 -07003910#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3911 NR_ANON_THPS,
3912#endif
Johannes Weinerc8713d02019-07-11 20:55:59 -07003913 NR_SHMEM,
3914 NR_FILE_MAPPED,
3915 NR_FILE_DIRTY,
3916 NR_WRITEBACK,
3917 MEMCG_SWAP,
3918};
3919
3920static const char *const memcg1_stat_names[] = {
3921 "cache",
3922 "rss",
Johannes Weiner468c3982020-06-03 16:02:01 -07003923#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Johannes Weinerc8713d02019-07-11 20:55:59 -07003924 "rss_huge",
Johannes Weiner468c3982020-06-03 16:02:01 -07003925#endif
Johannes Weinerc8713d02019-07-11 20:55:59 -07003926 "shmem",
3927 "mapped_file",
3928 "dirty",
3929 "writeback",
3930 "swap",
3931};
3932
Johannes Weinerdf0e53d2017-05-03 14:55:10 -07003933/* Universal VM events cgroup1 shows, original sort order */
Greg Thelen8dd53fd2018-06-07 17:07:23 -07003934static const unsigned int memcg1_events[] = {
Johannes Weinerdf0e53d2017-05-03 14:55:10 -07003935 PGPGIN,
3936 PGPGOUT,
3937 PGFAULT,
3938 PGMAJFAULT,
3939};
3940
Tejun Heo2da8ca82013-12-05 12:28:04 -05003941static int memcg_stat_show(struct seq_file *m, void *v)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003942{
Chris Downaa9694b2019-03-05 15:45:52 -08003943 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003944 unsigned long memory, memsw;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003945 struct mem_cgroup *mi;
3946 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003947
Johannes Weiner71cd3112017-05-03 14:55:13 -07003948 BUILD_BUG_ON(ARRAY_SIZE(memcg1_stat_names) != ARRAY_SIZE(memcg1_stats));
Rickard Strandqvist70bc0682014-12-12 16:56:41 -08003949
Johannes Weiner71cd3112017-05-03 14:55:13 -07003950 for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
Johannes Weiner468c3982020-06-03 16:02:01 -07003951 unsigned long nr;
3952
Johannes Weiner71cd3112017-05-03 14:55:13 -07003953 if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account())
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003954 continue;
Johannes Weiner468c3982020-06-03 16:02:01 -07003955 nr = memcg_page_state_local(memcg, memcg1_stats[i]);
3956#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3957 if (memcg1_stats[i] == NR_ANON_THPS)
3958 nr *= HPAGE_PMD_NR;
3959#endif
3960 seq_printf(m, "%s %lu\n", memcg1_stat_names[i], nr * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003961 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003962
Johannes Weinerdf0e53d2017-05-03 14:55:10 -07003963 for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08003964 seq_printf(m, "%s %lu\n", vm_event_name(memcg1_events[i]),
Johannes Weiner205b20c2019-05-14 15:47:06 -07003965 memcg_events_local(memcg, memcg1_events[i]));
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003966
3967 for (i = 0; i < NR_LRU_LISTS; i++)
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08003968 seq_printf(m, "%s %lu\n", lru_list_name(i),
Johannes Weiner205b20c2019-05-14 15:47:06 -07003969 memcg_page_state_local(memcg, NR_LRU_BASE + i) *
Johannes Weiner21d89d12019-05-13 17:18:08 -07003970 PAGE_SIZE);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003971
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003972 /* Hierarchical information */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003973 memory = memsw = PAGE_COUNTER_MAX;
3974 for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) {
Chris Down15b42562020-04-01 21:07:20 -07003975 memory = min(memory, READ_ONCE(mi->memory.max));
3976 memsw = min(memsw, READ_ONCE(mi->memsw.max));
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003977 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003978 seq_printf(m, "hierarchical_memory_limit %llu\n",
3979 (u64)memory * PAGE_SIZE);
Johannes Weiner7941d212016-01-14 15:21:23 -08003980 if (do_memsw_account())
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003981 seq_printf(m, "hierarchical_memsw_limit %llu\n",
3982 (u64)memsw * PAGE_SIZE);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003983
Shakeel Butt8de7ecc62018-08-21 21:53:17 -07003984 for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
Johannes Weiner71cd3112017-05-03 14:55:13 -07003985 if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account())
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003986 continue;
Shakeel Butt8de7ecc62018-08-21 21:53:17 -07003987 seq_printf(m, "total_%s %llu\n", memcg1_stat_names[i],
Yafang Shaodd923992019-07-11 20:52:11 -07003988 (u64)memcg_page_state(memcg, memcg1_stats[i]) *
3989 PAGE_SIZE);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003990 }
3991
Shakeel Butt8de7ecc62018-08-21 21:53:17 -07003992 for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08003993 seq_printf(m, "total_%s %llu\n",
3994 vm_event_name(memcg1_events[i]),
Yafang Shaodd923992019-07-11 20:52:11 -07003995 (u64)memcg_events(memcg, memcg1_events[i]));
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003996
Shakeel Butt8de7ecc62018-08-21 21:53:17 -07003997 for (i = 0; i < NR_LRU_LISTS; i++)
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08003998 seq_printf(m, "total_%s %llu\n", lru_list_name(i),
Johannes Weiner42a30032019-05-14 15:47:12 -07003999 (u64)memcg_page_state(memcg, NR_LRU_BASE + i) *
4000 PAGE_SIZE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004001
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004002#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004003 {
Mel Gormanef8f2322016-07-28 15:46:05 -07004004 pg_data_t *pgdat;
4005 struct mem_cgroup_per_node *mz;
Johannes Weiner1431d4d2020-06-03 16:02:53 -07004006 unsigned long anon_cost = 0;
4007 unsigned long file_cost = 0;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004008
Mel Gormanef8f2322016-07-28 15:46:05 -07004009 for_each_online_pgdat(pgdat) {
4010 mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004011
Johannes Weiner1431d4d2020-06-03 16:02:53 -07004012 anon_cost += mz->lruvec.anon_cost;
4013 file_cost += mz->lruvec.file_cost;
Mel Gormanef8f2322016-07-28 15:46:05 -07004014 }
Johannes Weiner1431d4d2020-06-03 16:02:53 -07004015 seq_printf(m, "anon_cost %lu\n", anon_cost);
4016 seq_printf(m, "file_cost %lu\n", file_cost);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004017 }
4018#endif
4019
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004020 return 0;
4021}
4022
Tejun Heo182446d2013-08-08 20:11:24 -04004023static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
4024 struct cftype *cft)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004025{
Tejun Heo182446d2013-08-08 20:11:24 -04004026 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004027
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004028 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004029}
4030
Tejun Heo182446d2013-08-08 20:11:24 -04004031static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
4032 struct cftype *cft, u64 val)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004033{
Tejun Heo182446d2013-08-08 20:11:24 -04004034 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Li Zefan068b38c2009-01-15 13:51:26 -08004035
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07004036 if (val > 100)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004037 return -EINVAL;
4038
Linus Torvalds14208b02014-06-09 15:03:33 -07004039 if (css->parent)
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07004040 memcg->swappiness = val;
4041 else
4042 vm_swappiness = val;
Li Zefan068b38c2009-01-15 13:51:26 -08004043
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004044 return 0;
4045}
4046
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004047static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4048{
4049 struct mem_cgroup_threshold_ary *t;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004050 unsigned long usage;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004051 int i;
4052
4053 rcu_read_lock();
4054 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004055 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004056 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004057 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004058
4059 if (!t)
4060 goto unlock;
4061
Johannes Weinerce00a962014-09-05 08:43:57 -04004062 usage = mem_cgroup_usage(memcg, swap);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004063
4064 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07004065 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004066 * If it's not true, a threshold was crossed after last
4067 * call of __mem_cgroup_threshold().
4068 */
Phil Carmody5407a562010-05-26 14:42:42 -07004069 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004070
4071 /*
4072 * Iterate backward over array of thresholds starting from
4073 * current_threshold and check if a threshold is crossed.
4074 * If none of thresholds below usage is crossed, we read
4075 * only one element of the array here.
4076 */
4077 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4078 eventfd_signal(t->entries[i].eventfd, 1);
4079
4080 /* i = current_threshold + 1 */
4081 i++;
4082
4083 /*
4084 * Iterate forward over array of thresholds starting from
4085 * current_threshold+1 and check if a threshold is crossed.
4086 * If none of thresholds above usage is crossed, we read
4087 * only one element of the array here.
4088 */
4089 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4090 eventfd_signal(t->entries[i].eventfd, 1);
4091
4092 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004093 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004094unlock:
4095 rcu_read_unlock();
4096}
4097
4098static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4099{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004100 while (memcg) {
4101 __mem_cgroup_threshold(memcg, false);
Johannes Weiner7941d212016-01-14 15:21:23 -08004102 if (do_memsw_account())
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004103 __mem_cgroup_threshold(memcg, true);
4104
4105 memcg = parent_mem_cgroup(memcg);
4106 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004107}
4108
4109static int compare_thresholds(const void *a, const void *b)
4110{
4111 const struct mem_cgroup_threshold *_a = a;
4112 const struct mem_cgroup_threshold *_b = b;
4113
Greg Thelen2bff24a2013-09-11 14:23:08 -07004114 if (_a->threshold > _b->threshold)
4115 return 1;
4116
4117 if (_a->threshold < _b->threshold)
4118 return -1;
4119
4120 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004121}
4122
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004123static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004124{
4125 struct mem_cgroup_eventfd_list *ev;
4126
Michal Hocko2bcf2e92014-07-30 16:08:33 -07004127 spin_lock(&memcg_oom_lock);
4128
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004129 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004130 eventfd_signal(ev->eventfd, 1);
Michal Hocko2bcf2e92014-07-30 16:08:33 -07004131
4132 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004133 return 0;
4134}
4135
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004136static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004137{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004138 struct mem_cgroup *iter;
4139
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004140 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004141 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004142}
4143
Tejun Heo59b6f872013-11-22 18:20:43 -05004144static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004145 struct eventfd_ctx *eventfd, const char *args, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004146{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004147 struct mem_cgroup_thresholds *thresholds;
4148 struct mem_cgroup_threshold_ary *new;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004149 unsigned long threshold;
4150 unsigned long usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004151 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004152
Johannes Weiner650c5e52015-02-11 15:26:03 -08004153 ret = page_counter_memparse(args, "-1", &threshold);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004154 if (ret)
4155 return ret;
4156
4157 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004158
Johannes Weiner05b84302014-08-06 16:05:59 -07004159 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004160 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004161 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07004162 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004163 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004164 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07004165 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004166 BUG();
4167
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004168 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004169 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004170 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4171
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004172 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004173
4174 /* Allocate memory for new array of thresholds */
Gustavo A. R. Silva67b80462019-03-05 15:44:05 -08004175 new = kmalloc(struct_size(new, entries, size), GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004176 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004177 ret = -ENOMEM;
4178 goto unlock;
4179 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004180 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004181
4182 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004183 if (thresholds->primary) {
4184 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004185 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004186 }
4187
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004188 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004189 new->entries[size - 1].eventfd = eventfd;
4190 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004191
4192 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004193 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004194 compare_thresholds, NULL);
4195
4196 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004197 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004198 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07004199 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004200 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004201 * new->current_threshold will not be used until
4202 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004203 * it here.
4204 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004205 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07004206 } else
4207 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004208 }
4209
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004210 /* Free old spare buffer and save old primary buffer as spare */
4211 kfree(thresholds->spare);
4212 thresholds->spare = thresholds->primary;
4213
4214 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004215
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004216 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004217 synchronize_rcu();
4218
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004219unlock:
4220 mutex_unlock(&memcg->thresholds_lock);
4221
4222 return ret;
4223}
4224
Tejun Heo59b6f872013-11-22 18:20:43 -05004225static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004226 struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004227{
Tejun Heo59b6f872013-11-22 18:20:43 -05004228 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05004229}
4230
Tejun Heo59b6f872013-11-22 18:20:43 -05004231static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004232 struct eventfd_ctx *eventfd, const char *args)
4233{
Tejun Heo59b6f872013-11-22 18:20:43 -05004234 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05004235}
4236
Tejun Heo59b6f872013-11-22 18:20:43 -05004237static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004238 struct eventfd_ctx *eventfd, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004239{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004240 struct mem_cgroup_thresholds *thresholds;
4241 struct mem_cgroup_threshold_ary *new;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004242 unsigned long usage;
Chunguang Xu7d366652020-03-21 18:22:10 -07004243 int i, j, size, entries;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004244
4245 mutex_lock(&memcg->thresholds_lock);
Johannes Weiner05b84302014-08-06 16:05:59 -07004246
4247 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004248 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004249 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07004250 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004251 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004252 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07004253 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004254 BUG();
4255
Anton Vorontsov371528c2012-02-24 05:14:46 +04004256 if (!thresholds->primary)
4257 goto unlock;
4258
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004259 /* Check if a threshold crossed before removing */
4260 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4261
4262 /* Calculate new number of threshold */
Chunguang Xu7d366652020-03-21 18:22:10 -07004263 size = entries = 0;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004264 for (i = 0; i < thresholds->primary->size; i++) {
4265 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004266 size++;
Chunguang Xu7d366652020-03-21 18:22:10 -07004267 else
4268 entries++;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004269 }
4270
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004271 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004272
Chunguang Xu7d366652020-03-21 18:22:10 -07004273 /* If no items related to eventfd have been cleared, nothing to do */
4274 if (!entries)
4275 goto unlock;
4276
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004277 /* Set thresholds array to NULL if we don't have thresholds */
4278 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004279 kfree(new);
4280 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004281 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004282 }
4283
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004284 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004285
4286 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004287 new->current_threshold = -1;
4288 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4289 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004290 continue;
4291
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004292 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07004293 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004294 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004295 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004296 * until rcu_assign_pointer(), so it's safe to increment
4297 * it here.
4298 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004299 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004300 }
4301 j++;
4302 }
4303
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004304swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004305 /* Swap primary and spare array */
4306 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07004307
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004308 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004309
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004310 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004311 synchronize_rcu();
Martijn Coenen6611d8d2016-01-15 16:57:49 -08004312
4313 /* If all events are unregistered, free the spare array */
4314 if (!new) {
4315 kfree(thresholds->spare);
4316 thresholds->spare = NULL;
4317 }
Anton Vorontsov371528c2012-02-24 05:14:46 +04004318unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004319 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004320}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004321
Tejun Heo59b6f872013-11-22 18:20:43 -05004322static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004323 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004324{
Tejun Heo59b6f872013-11-22 18:20:43 -05004325 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05004326}
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004327
Tejun Heo59b6f872013-11-22 18:20:43 -05004328static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004329 struct eventfd_ctx *eventfd)
4330{
Tejun Heo59b6f872013-11-22 18:20:43 -05004331 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05004332}
4333
Tejun Heo59b6f872013-11-22 18:20:43 -05004334static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004335 struct eventfd_ctx *eventfd, const char *args)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004336{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004337 struct mem_cgroup_eventfd_list *event;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004338
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004339 event = kmalloc(sizeof(*event), GFP_KERNEL);
4340 if (!event)
4341 return -ENOMEM;
4342
Michal Hocko1af8efe2011-07-26 16:08:24 -07004343 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004344
4345 event->eventfd = eventfd;
4346 list_add(&event->list, &memcg->oom_notify);
4347
4348 /* already in OOM ? */
Tejun Heoc2b42d32015-06-24 16:58:23 -07004349 if (memcg->under_oom)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004350 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004351 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004352
4353 return 0;
4354}
4355
Tejun Heo59b6f872013-11-22 18:20:43 -05004356static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004357 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004358{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004359 struct mem_cgroup_eventfd_list *ev, *tmp;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004360
Michal Hocko1af8efe2011-07-26 16:08:24 -07004361 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004362
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004363 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004364 if (ev->eventfd == eventfd) {
4365 list_del(&ev->list);
4366 kfree(ev);
4367 }
4368 }
4369
Michal Hocko1af8efe2011-07-26 16:08:24 -07004370 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004371}
4372
Tejun Heo2da8ca82013-12-05 12:28:04 -05004373static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004374{
Chris Downaa9694b2019-03-05 15:45:52 -08004375 struct mem_cgroup *memcg = mem_cgroup_from_seq(sf);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004376
Tejun Heo791badb2013-12-05 12:28:02 -05004377 seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
Tejun Heoc2b42d32015-06-24 16:58:23 -07004378 seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom);
Roman Gushchinfe6bdfc2018-06-14 15:28:05 -07004379 seq_printf(sf, "oom_kill %lu\n",
4380 atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL]));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004381 return 0;
4382}
4383
Tejun Heo182446d2013-08-08 20:11:24 -04004384static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004385 struct cftype *cft, u64 val)
4386{
Tejun Heo182446d2013-08-08 20:11:24 -04004387 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004388
4389 /* cannot set to root cgroup and only 0 and 1 are allowed */
Linus Torvalds14208b02014-06-09 15:03:33 -07004390 if (!css->parent || !((val == 0) || (val == 1)))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004391 return -EINVAL;
4392
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004393 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004394 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004395 memcg_oom_recover(memcg);
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07004396
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004397 return 0;
4398}
4399
Tejun Heo52ebea72015-05-22 17:13:37 -04004400#ifdef CONFIG_CGROUP_WRITEBACK
4401
Tejun Heo3a8e9ac2019-08-29 15:47:19 -07004402#include <trace/events/writeback.h>
4403
Tejun Heo841710a2015-05-22 18:23:33 -04004404static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
4405{
4406 return wb_domain_init(&memcg->cgwb_domain, gfp);
4407}
4408
4409static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
4410{
4411 wb_domain_exit(&memcg->cgwb_domain);
4412}
4413
Tejun Heo2529bb32015-05-22 18:23:34 -04004414static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
4415{
4416 wb_domain_size_changed(&memcg->cgwb_domain);
4417}
4418
Tejun Heo841710a2015-05-22 18:23:33 -04004419struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
4420{
4421 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4422
4423 if (!memcg->css.parent)
4424 return NULL;
4425
4426 return &memcg->cgwb_domain;
4427}
4428
Greg Thelen0b3d6e62019-04-05 18:39:18 -07004429/*
4430 * idx can be of type enum memcg_stat_item or node_stat_item.
4431 * Keep in sync with memcg_exact_page().
4432 */
4433static unsigned long memcg_exact_page_state(struct mem_cgroup *memcg, int idx)
4434{
Chris Down871789d2019-05-14 15:46:57 -07004435 long x = atomic_long_read(&memcg->vmstats[idx]);
Greg Thelen0b3d6e62019-04-05 18:39:18 -07004436 int cpu;
4437
4438 for_each_online_cpu(cpu)
Chris Down871789d2019-05-14 15:46:57 -07004439 x += per_cpu_ptr(memcg->vmstats_percpu, cpu)->stat[idx];
Greg Thelen0b3d6e62019-04-05 18:39:18 -07004440 if (x < 0)
4441 x = 0;
4442 return x;
4443}
4444
Tejun Heoc2aa7232015-05-22 18:23:35 -04004445/**
4446 * mem_cgroup_wb_stats - retrieve writeback related stats from its memcg
4447 * @wb: bdi_writeback in question
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004448 * @pfilepages: out parameter for number of file pages
4449 * @pheadroom: out parameter for number of allocatable pages according to memcg
Tejun Heoc2aa7232015-05-22 18:23:35 -04004450 * @pdirty: out parameter for number of dirty pages
4451 * @pwriteback: out parameter for number of pages under writeback
4452 *
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004453 * Determine the numbers of file, headroom, dirty, and writeback pages in
4454 * @wb's memcg. File, dirty and writeback are self-explanatory. Headroom
4455 * is a bit more involved.
Tejun Heoc2aa7232015-05-22 18:23:35 -04004456 *
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004457 * A memcg's headroom is "min(max, high) - used". In the hierarchy, the
4458 * headroom is calculated as the lowest headroom of itself and the
4459 * ancestors. Note that this doesn't consider the actual amount of
4460 * available memory in the system. The caller should further cap
4461 * *@pheadroom accordingly.
Tejun Heoc2aa7232015-05-22 18:23:35 -04004462 */
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004463void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
4464 unsigned long *pheadroom, unsigned long *pdirty,
4465 unsigned long *pwriteback)
Tejun Heoc2aa7232015-05-22 18:23:35 -04004466{
4467 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4468 struct mem_cgroup *parent;
Tejun Heoc2aa7232015-05-22 18:23:35 -04004469
Greg Thelen0b3d6e62019-04-05 18:39:18 -07004470 *pdirty = memcg_exact_page_state(memcg, NR_FILE_DIRTY);
Tejun Heoc2aa7232015-05-22 18:23:35 -04004471
Greg Thelen0b3d6e62019-04-05 18:39:18 -07004472 *pwriteback = memcg_exact_page_state(memcg, NR_WRITEBACK);
Johannes Weiner21d89d12019-05-13 17:18:08 -07004473 *pfilepages = memcg_exact_page_state(memcg, NR_INACTIVE_FILE) +
4474 memcg_exact_page_state(memcg, NR_ACTIVE_FILE);
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004475 *pheadroom = PAGE_COUNTER_MAX;
Tejun Heoc2aa7232015-05-22 18:23:35 -04004476
Tejun Heoc2aa7232015-05-22 18:23:35 -04004477 while ((parent = parent_mem_cgroup(memcg))) {
Chris Down15b42562020-04-01 21:07:20 -07004478 unsigned long ceiling = min(READ_ONCE(memcg->memory.max),
Jakub Kicinskid1663a92020-06-01 21:49:49 -07004479 READ_ONCE(memcg->memory.high));
Tejun Heoc2aa7232015-05-22 18:23:35 -04004480 unsigned long used = page_counter_read(&memcg->memory);
4481
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004482 *pheadroom = min(*pheadroom, ceiling - min(ceiling, used));
Tejun Heoc2aa7232015-05-22 18:23:35 -04004483 memcg = parent;
4484 }
Tejun Heoc2aa7232015-05-22 18:23:35 -04004485}
4486
Tejun Heo97b27822019-08-26 09:06:56 -07004487/*
4488 * Foreign dirty flushing
4489 *
4490 * There's an inherent mismatch between memcg and writeback. The former
4491 * trackes ownership per-page while the latter per-inode. This was a
4492 * deliberate design decision because honoring per-page ownership in the
4493 * writeback path is complicated, may lead to higher CPU and IO overheads
4494 * and deemed unnecessary given that write-sharing an inode across
4495 * different cgroups isn't a common use-case.
4496 *
4497 * Combined with inode majority-writer ownership switching, this works well
4498 * enough in most cases but there are some pathological cases. For
4499 * example, let's say there are two cgroups A and B which keep writing to
4500 * different but confined parts of the same inode. B owns the inode and
4501 * A's memory is limited far below B's. A's dirty ratio can rise enough to
4502 * trigger balance_dirty_pages() sleeps but B's can be low enough to avoid
4503 * triggering background writeback. A will be slowed down without a way to
4504 * make writeback of the dirty pages happen.
4505 *
4506 * Conditions like the above can lead to a cgroup getting repatedly and
4507 * severely throttled after making some progress after each
4508 * dirty_expire_interval while the underyling IO device is almost
4509 * completely idle.
4510 *
4511 * Solving this problem completely requires matching the ownership tracking
4512 * granularities between memcg and writeback in either direction. However,
4513 * the more egregious behaviors can be avoided by simply remembering the
4514 * most recent foreign dirtying events and initiating remote flushes on
4515 * them when local writeback isn't enough to keep the memory clean enough.
4516 *
4517 * The following two functions implement such mechanism. When a foreign
4518 * page - a page whose memcg and writeback ownerships don't match - is
4519 * dirtied, mem_cgroup_track_foreign_dirty() records the inode owning
4520 * bdi_writeback on the page owning memcg. When balance_dirty_pages()
4521 * decides that the memcg needs to sleep due to high dirty ratio, it calls
4522 * mem_cgroup_flush_foreign() which queues writeback on the recorded
4523 * foreign bdi_writebacks which haven't expired. Both the numbers of
4524 * recorded bdi_writebacks and concurrent in-flight foreign writebacks are
4525 * limited to MEMCG_CGWB_FRN_CNT.
4526 *
4527 * The mechanism only remembers IDs and doesn't hold any object references.
4528 * As being wrong occasionally doesn't matter, updates and accesses to the
4529 * records are lockless and racy.
4530 */
4531void mem_cgroup_track_foreign_dirty_slowpath(struct page *page,
4532 struct bdi_writeback *wb)
4533{
4534 struct mem_cgroup *memcg = page->mem_cgroup;
4535 struct memcg_cgwb_frn *frn;
4536 u64 now = get_jiffies_64();
4537 u64 oldest_at = now;
4538 int oldest = -1;
4539 int i;
4540
Tejun Heo3a8e9ac2019-08-29 15:47:19 -07004541 trace_track_foreign_dirty(page, wb);
4542
Tejun Heo97b27822019-08-26 09:06:56 -07004543 /*
4544 * Pick the slot to use. If there is already a slot for @wb, keep
4545 * using it. If not replace the oldest one which isn't being
4546 * written out.
4547 */
4548 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) {
4549 frn = &memcg->cgwb_frn[i];
4550 if (frn->bdi_id == wb->bdi->id &&
4551 frn->memcg_id == wb->memcg_css->id)
4552 break;
4553 if (time_before64(frn->at, oldest_at) &&
4554 atomic_read(&frn->done.cnt) == 1) {
4555 oldest = i;
4556 oldest_at = frn->at;
4557 }
4558 }
4559
4560 if (i < MEMCG_CGWB_FRN_CNT) {
4561 /*
4562 * Re-using an existing one. Update timestamp lazily to
4563 * avoid making the cacheline hot. We want them to be
4564 * reasonably up-to-date and significantly shorter than
4565 * dirty_expire_interval as that's what expires the record.
4566 * Use the shorter of 1s and dirty_expire_interval / 8.
4567 */
4568 unsigned long update_intv =
4569 min_t(unsigned long, HZ,
4570 msecs_to_jiffies(dirty_expire_interval * 10) / 8);
4571
4572 if (time_before64(frn->at, now - update_intv))
4573 frn->at = now;
4574 } else if (oldest >= 0) {
4575 /* replace the oldest free one */
4576 frn = &memcg->cgwb_frn[oldest];
4577 frn->bdi_id = wb->bdi->id;
4578 frn->memcg_id = wb->memcg_css->id;
4579 frn->at = now;
4580 }
4581}
4582
4583/* issue foreign writeback flushes for recorded foreign dirtying events */
4584void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
4585{
4586 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4587 unsigned long intv = msecs_to_jiffies(dirty_expire_interval * 10);
4588 u64 now = jiffies_64;
4589 int i;
4590
4591 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) {
4592 struct memcg_cgwb_frn *frn = &memcg->cgwb_frn[i];
4593
4594 /*
4595 * If the record is older than dirty_expire_interval,
4596 * writeback on it has already started. No need to kick it
4597 * off again. Also, don't start a new one if there's
4598 * already one in flight.
4599 */
4600 if (time_after64(frn->at, now - intv) &&
4601 atomic_read(&frn->done.cnt) == 1) {
4602 frn->at = 0;
Tejun Heo3a8e9ac2019-08-29 15:47:19 -07004603 trace_flush_foreign(wb, frn->bdi_id, frn->memcg_id);
Tejun Heo97b27822019-08-26 09:06:56 -07004604 cgroup_writeback_by_id(frn->bdi_id, frn->memcg_id, 0,
4605 WB_REASON_FOREIGN_FLUSH,
4606 &frn->done);
4607 }
4608 }
4609}
4610
Tejun Heo841710a2015-05-22 18:23:33 -04004611#else /* CONFIG_CGROUP_WRITEBACK */
4612
4613static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
4614{
4615 return 0;
4616}
4617
4618static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
4619{
4620}
4621
Tejun Heo2529bb32015-05-22 18:23:34 -04004622static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
4623{
4624}
4625
Tejun Heo52ebea72015-05-22 17:13:37 -04004626#endif /* CONFIG_CGROUP_WRITEBACK */
4627
Tejun Heo79bd9812013-11-22 18:20:42 -05004628/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05004629 * DO NOT USE IN NEW FILES.
4630 *
4631 * "cgroup.event_control" implementation.
4632 *
4633 * This is way over-engineered. It tries to support fully configurable
4634 * events for each user. Such level of flexibility is completely
4635 * unnecessary especially in the light of the planned unified hierarchy.
4636 *
4637 * Please deprecate this and replace with something simpler if at all
4638 * possible.
4639 */
4640
4641/*
Tejun Heo79bd9812013-11-22 18:20:42 -05004642 * Unregister event and free resources.
4643 *
4644 * Gets called from workqueue.
4645 */
Tejun Heo3bc942f2013-11-22 18:20:44 -05004646static void memcg_event_remove(struct work_struct *work)
Tejun Heo79bd9812013-11-22 18:20:42 -05004647{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004648 struct mem_cgroup_event *event =
4649 container_of(work, struct mem_cgroup_event, remove);
Tejun Heo59b6f872013-11-22 18:20:43 -05004650 struct mem_cgroup *memcg = event->memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004651
4652 remove_wait_queue(event->wqh, &event->wait);
4653
Tejun Heo59b6f872013-11-22 18:20:43 -05004654 event->unregister_event(memcg, event->eventfd);
Tejun Heo79bd9812013-11-22 18:20:42 -05004655
4656 /* Notify userspace the event is going away. */
4657 eventfd_signal(event->eventfd, 1);
4658
4659 eventfd_ctx_put(event->eventfd);
4660 kfree(event);
Tejun Heo59b6f872013-11-22 18:20:43 -05004661 css_put(&memcg->css);
Tejun Heo79bd9812013-11-22 18:20:42 -05004662}
4663
4664/*
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004665 * Gets called on EPOLLHUP on eventfd when user closes it.
Tejun Heo79bd9812013-11-22 18:20:42 -05004666 *
4667 * Called with wqh->lock held and interrupts disabled.
4668 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02004669static int memcg_event_wake(wait_queue_entry_t *wait, unsigned mode,
Tejun Heo3bc942f2013-11-22 18:20:44 -05004670 int sync, void *key)
Tejun Heo79bd9812013-11-22 18:20:42 -05004671{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004672 struct mem_cgroup_event *event =
4673 container_of(wait, struct mem_cgroup_event, wait);
Tejun Heo59b6f872013-11-22 18:20:43 -05004674 struct mem_cgroup *memcg = event->memcg;
Al Viro3ad6f932017-07-03 20:14:56 -04004675 __poll_t flags = key_to_poll(key);
Tejun Heo79bd9812013-11-22 18:20:42 -05004676
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004677 if (flags & EPOLLHUP) {
Tejun Heo79bd9812013-11-22 18:20:42 -05004678 /*
4679 * If the event has been detached at cgroup removal, we
4680 * can simply return knowing the other side will cleanup
4681 * for us.
4682 *
4683 * We can't race against event freeing since the other
4684 * side will require wqh->lock via remove_wait_queue(),
4685 * which we hold.
4686 */
Tejun Heofba94802013-11-22 18:20:43 -05004687 spin_lock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004688 if (!list_empty(&event->list)) {
4689 list_del_init(&event->list);
4690 /*
4691 * We are in atomic context, but cgroup_event_remove()
4692 * may sleep, so we have to call it in workqueue.
4693 */
4694 schedule_work(&event->remove);
4695 }
Tejun Heofba94802013-11-22 18:20:43 -05004696 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004697 }
4698
4699 return 0;
4700}
4701
Tejun Heo3bc942f2013-11-22 18:20:44 -05004702static void memcg_event_ptable_queue_proc(struct file *file,
Tejun Heo79bd9812013-11-22 18:20:42 -05004703 wait_queue_head_t *wqh, poll_table *pt)
4704{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004705 struct mem_cgroup_event *event =
4706 container_of(pt, struct mem_cgroup_event, pt);
Tejun Heo79bd9812013-11-22 18:20:42 -05004707
4708 event->wqh = wqh;
4709 add_wait_queue(wqh, &event->wait);
4710}
4711
4712/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05004713 * DO NOT USE IN NEW FILES.
4714 *
Tejun Heo79bd9812013-11-22 18:20:42 -05004715 * Parse input and register new cgroup event handler.
4716 *
4717 * Input must be in format '<event_fd> <control_fd> <args>'.
4718 * Interpretation of args is defined by control file implementation.
4719 */
Tejun Heo451af502014-05-13 12:16:21 -04004720static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
4721 char *buf, size_t nbytes, loff_t off)
Tejun Heo79bd9812013-11-22 18:20:42 -05004722{
Tejun Heo451af502014-05-13 12:16:21 -04004723 struct cgroup_subsys_state *css = of_css(of);
Tejun Heofba94802013-11-22 18:20:43 -05004724 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004725 struct mem_cgroup_event *event;
Tejun Heo79bd9812013-11-22 18:20:42 -05004726 struct cgroup_subsys_state *cfile_css;
4727 unsigned int efd, cfd;
4728 struct fd efile;
4729 struct fd cfile;
Tejun Heofba94802013-11-22 18:20:43 -05004730 const char *name;
Tejun Heo79bd9812013-11-22 18:20:42 -05004731 char *endp;
4732 int ret;
4733
Tejun Heo451af502014-05-13 12:16:21 -04004734 buf = strstrip(buf);
4735
4736 efd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05004737 if (*endp != ' ')
4738 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04004739 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05004740
Tejun Heo451af502014-05-13 12:16:21 -04004741 cfd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05004742 if ((*endp != ' ') && (*endp != '\0'))
4743 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04004744 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05004745
4746 event = kzalloc(sizeof(*event), GFP_KERNEL);
4747 if (!event)
4748 return -ENOMEM;
4749
Tejun Heo59b6f872013-11-22 18:20:43 -05004750 event->memcg = memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004751 INIT_LIST_HEAD(&event->list);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004752 init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc);
4753 init_waitqueue_func_entry(&event->wait, memcg_event_wake);
4754 INIT_WORK(&event->remove, memcg_event_remove);
Tejun Heo79bd9812013-11-22 18:20:42 -05004755
4756 efile = fdget(efd);
4757 if (!efile.file) {
4758 ret = -EBADF;
4759 goto out_kfree;
4760 }
4761
4762 event->eventfd = eventfd_ctx_fileget(efile.file);
4763 if (IS_ERR(event->eventfd)) {
4764 ret = PTR_ERR(event->eventfd);
4765 goto out_put_efile;
4766 }
4767
4768 cfile = fdget(cfd);
4769 if (!cfile.file) {
4770 ret = -EBADF;
4771 goto out_put_eventfd;
4772 }
4773
4774 /* the process need read permission on control file */
4775 /* AV: shouldn't we check that it's been opened for read instead? */
4776 ret = inode_permission(file_inode(cfile.file), MAY_READ);
4777 if (ret < 0)
4778 goto out_put_cfile;
4779
Tejun Heo79bd9812013-11-22 18:20:42 -05004780 /*
Tejun Heofba94802013-11-22 18:20:43 -05004781 * Determine the event callbacks and set them in @event. This used
4782 * to be done via struct cftype but cgroup core no longer knows
4783 * about these events. The following is crude but the whole thing
4784 * is for compatibility anyway.
Tejun Heo3bc942f2013-11-22 18:20:44 -05004785 *
4786 * DO NOT ADD NEW FILES.
Tejun Heofba94802013-11-22 18:20:43 -05004787 */
Al Virob5830432014-10-31 01:22:04 -04004788 name = cfile.file->f_path.dentry->d_name.name;
Tejun Heofba94802013-11-22 18:20:43 -05004789
4790 if (!strcmp(name, "memory.usage_in_bytes")) {
4791 event->register_event = mem_cgroup_usage_register_event;
4792 event->unregister_event = mem_cgroup_usage_unregister_event;
4793 } else if (!strcmp(name, "memory.oom_control")) {
4794 event->register_event = mem_cgroup_oom_register_event;
4795 event->unregister_event = mem_cgroup_oom_unregister_event;
4796 } else if (!strcmp(name, "memory.pressure_level")) {
4797 event->register_event = vmpressure_register_event;
4798 event->unregister_event = vmpressure_unregister_event;
4799 } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) {
Tejun Heo347c4a82013-11-22 18:20:43 -05004800 event->register_event = memsw_cgroup_usage_register_event;
4801 event->unregister_event = memsw_cgroup_usage_unregister_event;
Tejun Heofba94802013-11-22 18:20:43 -05004802 } else {
4803 ret = -EINVAL;
4804 goto out_put_cfile;
4805 }
4806
4807 /*
Tejun Heob5557c42013-11-22 18:20:42 -05004808 * Verify @cfile should belong to @css. Also, remaining events are
4809 * automatically removed on cgroup destruction but the removal is
4810 * asynchronous, so take an extra ref on @css.
Tejun Heo79bd9812013-11-22 18:20:42 -05004811 */
Al Virob5830432014-10-31 01:22:04 -04004812 cfile_css = css_tryget_online_from_dir(cfile.file->f_path.dentry->d_parent,
Tejun Heoec903c02014-05-13 12:11:01 -04004813 &memory_cgrp_subsys);
Tejun Heo79bd9812013-11-22 18:20:42 -05004814 ret = -EINVAL;
Tejun Heo5a17f542014-02-11 11:52:47 -05004815 if (IS_ERR(cfile_css))
Tejun Heo79bd9812013-11-22 18:20:42 -05004816 goto out_put_cfile;
Tejun Heo5a17f542014-02-11 11:52:47 -05004817 if (cfile_css != css) {
4818 css_put(cfile_css);
4819 goto out_put_cfile;
4820 }
Tejun Heo79bd9812013-11-22 18:20:42 -05004821
Tejun Heo451af502014-05-13 12:16:21 -04004822 ret = event->register_event(memcg, event->eventfd, buf);
Tejun Heo79bd9812013-11-22 18:20:42 -05004823 if (ret)
4824 goto out_put_css;
4825
Christoph Hellwig9965ed172018-03-05 07:26:05 -08004826 vfs_poll(efile.file, &event->pt);
Tejun Heo79bd9812013-11-22 18:20:42 -05004827
Tejun Heofba94802013-11-22 18:20:43 -05004828 spin_lock(&memcg->event_list_lock);
4829 list_add(&event->list, &memcg->event_list);
4830 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004831
4832 fdput(cfile);
4833 fdput(efile);
4834
Tejun Heo451af502014-05-13 12:16:21 -04004835 return nbytes;
Tejun Heo79bd9812013-11-22 18:20:42 -05004836
4837out_put_css:
Tejun Heob5557c42013-11-22 18:20:42 -05004838 css_put(css);
Tejun Heo79bd9812013-11-22 18:20:42 -05004839out_put_cfile:
4840 fdput(cfile);
4841out_put_eventfd:
4842 eventfd_ctx_put(event->eventfd);
4843out_put_efile:
4844 fdput(efile);
4845out_kfree:
4846 kfree(event);
4847
4848 return ret;
4849}
4850
Johannes Weiner241994ed2015-02-11 15:26:06 -08004851static struct cftype mem_cgroup_legacy_files[] = {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004852 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004853 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004854 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05004855 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004856 },
4857 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004858 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004859 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04004860 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004861 .read_u64 = mem_cgroup_read_u64,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004862 },
4863 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004864 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004865 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004866 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004867 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004868 },
4869 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004870 .name = "soft_limit_in_bytes",
4871 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004872 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004873 .read_u64 = mem_cgroup_read_u64,
Balbir Singh296c81d2009-09-23 15:56:36 -07004874 },
4875 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004876 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004877 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04004878 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004879 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004880 },
Balbir Singh8697d332008-02-07 00:13:59 -08004881 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004882 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004883 .seq_show = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004884 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004885 {
4886 .name = "force_empty",
Tejun Heo6770c642014-05-13 12:16:21 -04004887 .write = mem_cgroup_force_empty_write,
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004888 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004889 {
4890 .name = "use_hierarchy",
4891 .write_u64 = mem_cgroup_hierarchy_write,
4892 .read_u64 = mem_cgroup_hierarchy_read,
4893 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004894 {
Tejun Heo3bc942f2013-11-22 18:20:44 -05004895 .name = "cgroup.event_control", /* XXX: for compat */
Tejun Heo451af502014-05-13 12:16:21 -04004896 .write = memcg_write_event_control,
Tejun Heo7dbdb192015-09-18 17:54:23 -04004897 .flags = CFTYPE_NO_PREFIX | CFTYPE_WORLD_WRITABLE,
Tejun Heo79bd9812013-11-22 18:20:42 -05004898 },
4899 {
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004900 .name = "swappiness",
4901 .read_u64 = mem_cgroup_swappiness_read,
4902 .write_u64 = mem_cgroup_swappiness_write,
4903 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004904 {
4905 .name = "move_charge_at_immigrate",
4906 .read_u64 = mem_cgroup_move_charge_read,
4907 .write_u64 = mem_cgroup_move_charge_write,
4908 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004909 {
4910 .name = "oom_control",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004911 .seq_show = mem_cgroup_oom_control_read,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004912 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004913 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4914 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004915 {
4916 .name = "pressure_level",
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004917 },
Ying Han406eb0c2011-05-26 16:25:37 -07004918#ifdef CONFIG_NUMA
4919 {
4920 .name = "numa_stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004921 .seq_show = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07004922 },
4923#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08004924 {
4925 .name = "kmem.limit_in_bytes",
4926 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004927 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004928 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004929 },
4930 {
4931 .name = "kmem.usage_in_bytes",
4932 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05004933 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004934 },
4935 {
4936 .name = "kmem.failcnt",
4937 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04004938 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004939 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004940 },
4941 {
4942 .name = "kmem.max_usage_in_bytes",
4943 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04004944 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004945 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004946 },
Yafang Shaoa87425a2020-04-01 21:06:30 -07004947#if defined(CONFIG_MEMCG_KMEM) && \
4948 (defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG))
Glauber Costa749c5412012-12-18 14:23:01 -08004949 {
4950 .name = "kmem.slabinfo",
Vladimir Davydovb0475012014-12-10 15:44:19 -08004951 .seq_show = memcg_slab_show,
Glauber Costa749c5412012-12-18 14:23:01 -08004952 },
4953#endif
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08004954 {
4955 .name = "kmem.tcp.limit_in_bytes",
4956 .private = MEMFILE_PRIVATE(_TCP, RES_LIMIT),
4957 .write = mem_cgroup_write,
4958 .read_u64 = mem_cgroup_read_u64,
4959 },
4960 {
4961 .name = "kmem.tcp.usage_in_bytes",
4962 .private = MEMFILE_PRIVATE(_TCP, RES_USAGE),
4963 .read_u64 = mem_cgroup_read_u64,
4964 },
4965 {
4966 .name = "kmem.tcp.failcnt",
4967 .private = MEMFILE_PRIVATE(_TCP, RES_FAILCNT),
4968 .write = mem_cgroup_reset,
4969 .read_u64 = mem_cgroup_read_u64,
4970 },
4971 {
4972 .name = "kmem.tcp.max_usage_in_bytes",
4973 .private = MEMFILE_PRIVATE(_TCP, RES_MAX_USAGE),
4974 .write = mem_cgroup_reset,
4975 .read_u64 = mem_cgroup_read_u64,
4976 },
Tejun Heo6bc10342012-04-01 12:09:55 -07004977 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07004978};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004979
Johannes Weiner73f576c2016-07-20 15:44:57 -07004980/*
4981 * Private memory cgroup IDR
4982 *
4983 * Swap-out records and page cache shadow entries need to store memcg
4984 * references in constrained space, so we maintain an ID space that is
4985 * limited to 16 bit (MEM_CGROUP_ID_MAX), limiting the total number of
4986 * memory-controlled cgroups to 64k.
4987 *
Ethon Paulb8f29352020-06-04 16:49:28 -07004988 * However, there usually are many references to the offline CSS after
Johannes Weiner73f576c2016-07-20 15:44:57 -07004989 * the cgroup has been destroyed, such as page cache or reclaimable
4990 * slab objects, that don't need to hang on to the ID. We want to keep
4991 * those dead CSS from occupying IDs, or we might quickly exhaust the
4992 * relatively small ID space and prevent the creation of new cgroups
4993 * even when there are much fewer than 64k cgroups - possibly none.
4994 *
4995 * Maintain a private 16-bit ID space for memcg, and allow the ID to
4996 * be freed and recycled when it's no longer needed, which is usually
4997 * when the CSS is offlined.
4998 *
4999 * The only exception to that are records of swapped out tmpfs/shmem
5000 * pages that need to be attributed to live ancestors on swapin. But
5001 * those references are manageable from userspace.
5002 */
5003
5004static DEFINE_IDR(mem_cgroup_idr);
5005
Kirill Tkhai7e97de02018-08-02 15:36:01 -07005006static void mem_cgroup_id_remove(struct mem_cgroup *memcg)
5007{
5008 if (memcg->id.id > 0) {
5009 idr_remove(&mem_cgroup_idr, memcg->id.id);
5010 memcg->id.id = 0;
5011 }
5012}
5013
Vincenzo Frascinoc1514c02020-04-01 21:07:13 -07005014static void __maybe_unused mem_cgroup_id_get_many(struct mem_cgroup *memcg,
5015 unsigned int n)
Johannes Weiner73f576c2016-07-20 15:44:57 -07005016{
Kirill Tkhai1c2d4792018-10-26 15:09:28 -07005017 refcount_add(n, &memcg->id.ref);
Johannes Weiner73f576c2016-07-20 15:44:57 -07005018}
5019
Vladimir Davydov615d66c2016-08-11 15:33:03 -07005020static void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n)
Johannes Weiner73f576c2016-07-20 15:44:57 -07005021{
Kirill Tkhai1c2d4792018-10-26 15:09:28 -07005022 if (refcount_sub_and_test(n, &memcg->id.ref)) {
Kirill Tkhai7e97de02018-08-02 15:36:01 -07005023 mem_cgroup_id_remove(memcg);
Johannes Weiner73f576c2016-07-20 15:44:57 -07005024
5025 /* Memcg ID pins CSS */
5026 css_put(&memcg->css);
5027 }
5028}
5029
Vladimir Davydov615d66c2016-08-11 15:33:03 -07005030static inline void mem_cgroup_id_put(struct mem_cgroup *memcg)
5031{
5032 mem_cgroup_id_put_many(memcg, 1);
5033}
5034
Johannes Weiner73f576c2016-07-20 15:44:57 -07005035/**
5036 * mem_cgroup_from_id - look up a memcg from a memcg id
5037 * @id: the memcg id to look up
5038 *
5039 * Caller must hold rcu_read_lock().
5040 */
5041struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
5042{
5043 WARN_ON_ONCE(!rcu_read_lock_held());
5044 return idr_find(&mem_cgroup_idr, id);
5045}
5046
Mel Gormanef8f2322016-07-28 15:46:05 -07005047static int alloc_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005048{
5049 struct mem_cgroup_per_node *pn;
Mel Gormanef8f2322016-07-28 15:46:05 -07005050 int tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005051 /*
5052 * This routine is called against possible nodes.
5053 * But it's BUG to call kmalloc() against offline node.
5054 *
5055 * TODO: this routine can waste much memory for nodes which will
5056 * never be onlined. It's better to use memory hotplug callback
5057 * function.
5058 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005059 if (!node_state(node, N_NORMAL_MEMORY))
5060 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005061 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005062 if (!pn)
5063 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005064
Johannes Weiner815744d2019-06-13 15:55:46 -07005065 pn->lruvec_stat_local = alloc_percpu(struct lruvec_stat);
5066 if (!pn->lruvec_stat_local) {
5067 kfree(pn);
5068 return 1;
5069 }
5070
Johannes Weinera983b5e2018-01-31 16:16:45 -08005071 pn->lruvec_stat_cpu = alloc_percpu(struct lruvec_stat);
5072 if (!pn->lruvec_stat_cpu) {
Johannes Weiner815744d2019-06-13 15:55:46 -07005073 free_percpu(pn->lruvec_stat_local);
Johannes Weiner00f3ca22017-07-06 15:40:52 -07005074 kfree(pn);
5075 return 1;
5076 }
5077
Mel Gormanef8f2322016-07-28 15:46:05 -07005078 lruvec_init(&pn->lruvec);
5079 pn->usage_in_excess = 0;
5080 pn->on_tree = false;
5081 pn->memcg = memcg;
5082
Johannes Weiner54f72fe2013-07-08 15:59:49 -07005083 memcg->nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005084 return 0;
5085}
5086
Mel Gormanef8f2322016-07-28 15:46:05 -07005087static void free_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005088{
Johannes Weiner00f3ca22017-07-06 15:40:52 -07005089 struct mem_cgroup_per_node *pn = memcg->nodeinfo[node];
5090
Michal Hocko4eaf4312018-04-10 16:29:52 -07005091 if (!pn)
5092 return;
5093
Johannes Weinera983b5e2018-01-31 16:16:45 -08005094 free_percpu(pn->lruvec_stat_cpu);
Johannes Weiner815744d2019-06-13 15:55:46 -07005095 free_percpu(pn->lruvec_stat_local);
Johannes Weiner00f3ca22017-07-06 15:40:52 -07005096 kfree(pn);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005097}
5098
Tahsin Erdogan40e952f2017-03-09 16:17:26 -08005099static void __mem_cgroup_free(struct mem_cgroup *memcg)
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005100{
5101 int node;
5102
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005103 for_each_node(node)
Mel Gormanef8f2322016-07-28 15:46:05 -07005104 free_mem_cgroup_per_node_info(memcg, node);
Chris Down871789d2019-05-14 15:46:57 -07005105 free_percpu(memcg->vmstats_percpu);
Johannes Weiner815744d2019-06-13 15:55:46 -07005106 free_percpu(memcg->vmstats_local);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005107 kfree(memcg);
5108}
5109
Tahsin Erdogan40e952f2017-03-09 16:17:26 -08005110static void mem_cgroup_free(struct mem_cgroup *memcg)
5111{
5112 memcg_wb_domain_exit(memcg);
Shakeel Butt7961eee2019-11-05 21:16:21 -08005113 /*
5114 * Flush percpu vmstats and vmevents to guarantee the value correctness
5115 * on parent's and all ancestor levels.
5116 */
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08005117 memcg_flush_percpu_vmstats(memcg);
Shakeel Butt7961eee2019-11-05 21:16:21 -08005118 memcg_flush_percpu_vmevents(memcg);
Tahsin Erdogan40e952f2017-03-09 16:17:26 -08005119 __mem_cgroup_free(memcg);
5120}
5121
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005122static struct mem_cgroup *mem_cgroup_alloc(void)
5123{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005124 struct mem_cgroup *memcg;
Alexey Dobriyanb9726c22019-03-05 15:48:26 -08005125 unsigned int size;
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005126 int node;
Tejun Heo97b27822019-08-26 09:06:56 -07005127 int __maybe_unused i;
Yafang Shao11d67612020-05-07 18:35:43 -07005128 long error = -ENOMEM;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005129
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08005130 size = sizeof(struct mem_cgroup);
5131 size += nr_node_ids * sizeof(struct mem_cgroup_per_node *);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005132
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08005133 memcg = kzalloc(size, GFP_KERNEL);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005134 if (!memcg)
Yafang Shao11d67612020-05-07 18:35:43 -07005135 return ERR_PTR(error);
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005136
Johannes Weiner73f576c2016-07-20 15:44:57 -07005137 memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL,
5138 1, MEM_CGROUP_ID_MAX,
5139 GFP_KERNEL);
Yafang Shao11d67612020-05-07 18:35:43 -07005140 if (memcg->id.id < 0) {
5141 error = memcg->id.id;
Johannes Weiner73f576c2016-07-20 15:44:57 -07005142 goto fail;
Yafang Shao11d67612020-05-07 18:35:43 -07005143 }
Johannes Weiner73f576c2016-07-20 15:44:57 -07005144
Johannes Weiner815744d2019-06-13 15:55:46 -07005145 memcg->vmstats_local = alloc_percpu(struct memcg_vmstats_percpu);
5146 if (!memcg->vmstats_local)
5147 goto fail;
5148
Chris Down871789d2019-05-14 15:46:57 -07005149 memcg->vmstats_percpu = alloc_percpu(struct memcg_vmstats_percpu);
5150 if (!memcg->vmstats_percpu)
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005151 goto fail;
Pavel Emelianov78fb7462008-02-07 00:13:51 -08005152
Bob Liu3ed28fa2012-01-12 17:19:04 -08005153 for_each_node(node)
Mel Gormanef8f2322016-07-28 15:46:05 -07005154 if (alloc_mem_cgroup_per_node_info(memcg, node))
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005155 goto fail;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005156
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005157 if (memcg_wb_domain_init(memcg, GFP_KERNEL))
5158 goto fail;
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005159
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08005160 INIT_WORK(&memcg->high_work, high_work_func);
Glauber Costad142e3e2013-02-22 16:34:52 -08005161 INIT_LIST_HEAD(&memcg->oom_notify);
Glauber Costad142e3e2013-02-22 16:34:52 -08005162 mutex_init(&memcg->thresholds_lock);
5163 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07005164 vmpressure_init(&memcg->vmpressure);
Tejun Heofba94802013-11-22 18:20:43 -05005165 INIT_LIST_HEAD(&memcg->event_list);
5166 spin_lock_init(&memcg->event_list_lock);
Johannes Weinerd886f4e2016-01-20 15:02:47 -08005167 memcg->socket_pressure = jiffies;
Kirill Tkhai84c07d12018-08-17 15:47:25 -07005168#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydov900a38f2014-12-12 16:55:10 -08005169 memcg->kmemcg_id = -1;
Roman Gushchinbf4f0592020-08-06 23:20:49 -07005170 INIT_LIST_HEAD(&memcg->objcg_list);
Vladimir Davydov900a38f2014-12-12 16:55:10 -08005171#endif
Tejun Heo52ebea72015-05-22 17:13:37 -04005172#ifdef CONFIG_CGROUP_WRITEBACK
5173 INIT_LIST_HEAD(&memcg->cgwb_list);
Tejun Heo97b27822019-08-26 09:06:56 -07005174 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++)
5175 memcg->cgwb_frn[i].done =
5176 __WB_COMPLETION_INIT(&memcg_cgwb_frn_waitq);
Tejun Heo52ebea72015-05-22 17:13:37 -04005177#endif
Yang Shi87eaceb2019-09-23 15:38:15 -07005178#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5179 spin_lock_init(&memcg->deferred_split_queue.split_queue_lock);
5180 INIT_LIST_HEAD(&memcg->deferred_split_queue.split_queue);
5181 memcg->deferred_split_queue.split_queue_len = 0;
5182#endif
Johannes Weiner73f576c2016-07-20 15:44:57 -07005183 idr_replace(&mem_cgroup_idr, memcg, memcg->id.id);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005184 return memcg;
5185fail:
Kirill Tkhai7e97de02018-08-02 15:36:01 -07005186 mem_cgroup_id_remove(memcg);
Tahsin Erdogan40e952f2017-03-09 16:17:26 -08005187 __mem_cgroup_free(memcg);
Yafang Shao11d67612020-05-07 18:35:43 -07005188 return ERR_PTR(error);
Glauber Costad142e3e2013-02-22 16:34:52 -08005189}
5190
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005191static struct cgroup_subsys_state * __ref
5192mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Glauber Costad142e3e2013-02-22 16:34:52 -08005193{
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005194 struct mem_cgroup *parent = mem_cgroup_from_css(parent_css);
5195 struct mem_cgroup *memcg;
5196 long error = -ENOMEM;
Glauber Costad142e3e2013-02-22 16:34:52 -08005197
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005198 memcg = mem_cgroup_alloc();
Yafang Shao11d67612020-05-07 18:35:43 -07005199 if (IS_ERR(memcg))
5200 return ERR_CAST(memcg);
Li Zefan4219b2d2013-09-23 16:56:29 +08005201
Jakub Kicinskid1663a92020-06-01 21:49:49 -07005202 page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005203 memcg->soft_limit = PAGE_COUNTER_MAX;
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07005204 page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005205 if (parent) {
5206 memcg->swappiness = mem_cgroup_swappiness(parent);
5207 memcg->oom_kill_disable = parent->oom_kill_disable;
5208 }
5209 if (parent && parent->use_hierarchy) {
5210 memcg->use_hierarchy = true;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005211 page_counter_init(&memcg->memory, &parent->memory);
Vladimir Davydov37e84352016-01-20 15:02:56 -08005212 page_counter_init(&memcg->swap, &parent->swap);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005213 page_counter_init(&memcg->memsw, &parent->memsw);
5214 page_counter_init(&memcg->kmem, &parent->kmem);
Johannes Weiner0db15292016-01-20 15:02:50 -08005215 page_counter_init(&memcg->tcpmem, &parent->tcpmem);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005216 } else {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005217 page_counter_init(&memcg->memory, NULL);
Vladimir Davydov37e84352016-01-20 15:02:56 -08005218 page_counter_init(&memcg->swap, NULL);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005219 page_counter_init(&memcg->memsw, NULL);
5220 page_counter_init(&memcg->kmem, NULL);
Johannes Weiner0db15292016-01-20 15:02:50 -08005221 page_counter_init(&memcg->tcpmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07005222 /*
5223 * Deeper hierachy with use_hierarchy == false doesn't make
5224 * much sense so let cgroup subsystem know about this
5225 * unfortunate state in our controller.
5226 */
Glauber Costad142e3e2013-02-22 16:34:52 -08005227 if (parent != root_mem_cgroup)
Tejun Heo073219e2014-02-08 10:36:58 -05005228 memory_cgrp_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005229 }
Vladimir Davydovd6441632014-01-23 15:53:09 -08005230
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005231 /* The following stuff does not apply to the root */
5232 if (!parent) {
5233 root_mem_cgroup = memcg;
5234 return &memcg->css;
5235 }
5236
Vladimir Davydovb313aee2016-03-17 14:18:27 -07005237 error = memcg_online_kmem(memcg);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005238 if (error)
5239 goto fail;
Johannes Weiner127424c2016-01-20 15:02:32 -08005240
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08005241 if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket)
Johannes Weineref129472016-01-14 15:21:34 -08005242 static_branch_inc(&memcg_sockets_enabled_key);
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08005243
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005244 return &memcg->css;
5245fail:
Kirill Tkhai7e97de02018-08-02 15:36:01 -07005246 mem_cgroup_id_remove(memcg);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005247 mem_cgroup_free(memcg);
Yafang Shao11d67612020-05-07 18:35:43 -07005248 return ERR_PTR(error);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005249}
5250
Johannes Weiner73f576c2016-07-20 15:44:57 -07005251static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005252{
Vladimir Davydov58fa2a52016-10-07 16:57:29 -07005253 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5254
Kirill Tkhai0a4465d2018-08-17 15:47:37 -07005255 /*
5256 * A memcg must be visible for memcg_expand_shrinker_maps()
5257 * by the time the maps are allocated. So, we allocate maps
5258 * here, when for_each_mem_cgroup() can't skip it.
5259 */
5260 if (memcg_alloc_shrinker_maps(memcg)) {
5261 mem_cgroup_id_remove(memcg);
5262 return -ENOMEM;
5263 }
5264
Johannes Weiner73f576c2016-07-20 15:44:57 -07005265 /* Online state pins memcg ID, memcg ID pins CSS */
Kirill Tkhai1c2d4792018-10-26 15:09:28 -07005266 refcount_set(&memcg->id.ref, 1);
Johannes Weiner73f576c2016-07-20 15:44:57 -07005267 css_get(css);
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -07005268 return 0;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005269}
5270
Tejun Heoeb954192013-08-08 20:11:23 -04005271static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005272{
Tejun Heoeb954192013-08-08 20:11:23 -04005273 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05005274 struct mem_cgroup_event *event, *tmp;
Tejun Heo79bd9812013-11-22 18:20:42 -05005275
5276 /*
5277 * Unregister events and notify userspace.
5278 * Notify userspace about cgroup removing only after rmdir of cgroup
5279 * directory to avoid race between userspace and kernelspace.
5280 */
Tejun Heofba94802013-11-22 18:20:43 -05005281 spin_lock(&memcg->event_list_lock);
5282 list_for_each_entry_safe(event, tmp, &memcg->event_list, list) {
Tejun Heo79bd9812013-11-22 18:20:42 -05005283 list_del_init(&event->list);
5284 schedule_work(&event->remove);
5285 }
Tejun Heofba94802013-11-22 18:20:43 -05005286 spin_unlock(&memcg->event_list_lock);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005287
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005288 page_counter_set_min(&memcg->memory, 0);
Roman Gushchin23067152018-06-07 17:06:22 -07005289 page_counter_set_low(&memcg->memory, 0);
Roman Gushchin63677c742017-09-06 16:21:47 -07005290
Johannes Weiner567e9ab2016-01-20 15:02:24 -08005291 memcg_offline_kmem(memcg);
Tejun Heo52ebea72015-05-22 17:13:37 -04005292 wb_memcg_offline(memcg);
Johannes Weiner73f576c2016-07-20 15:44:57 -07005293
Roman Gushchin591edfb2018-10-26 15:03:23 -07005294 drain_all_stock(memcg);
5295
Johannes Weiner73f576c2016-07-20 15:44:57 -07005296 mem_cgroup_id_put(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005297}
5298
Vladimir Davydov6df38682015-12-29 14:54:10 -08005299static void mem_cgroup_css_released(struct cgroup_subsys_state *css)
5300{
5301 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5302
5303 invalidate_reclaim_iterators(memcg);
5304}
5305
Tejun Heoeb954192013-08-08 20:11:23 -04005306static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005307{
Tejun Heoeb954192013-08-08 20:11:23 -04005308 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo97b27822019-08-26 09:06:56 -07005309 int __maybe_unused i;
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005310
Tejun Heo97b27822019-08-26 09:06:56 -07005311#ifdef CONFIG_CGROUP_WRITEBACK
5312 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++)
5313 wb_wait_for_completion(&memcg->cgwb_frn[i].done);
5314#endif
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08005315 if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket)
Johannes Weineref129472016-01-14 15:21:34 -08005316 static_branch_dec(&memcg_sockets_enabled_key);
Johannes Weiner3893e302016-01-20 15:02:29 -08005317
Johannes Weiner0db15292016-01-20 15:02:50 -08005318 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_active)
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08005319 static_branch_dec(&memcg_sockets_enabled_key);
Johannes Weiner3893e302016-01-20 15:02:29 -08005320
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005321 vmpressure_cleanup(&memcg->vmpressure);
5322 cancel_work_sync(&memcg->high_work);
5323 mem_cgroup_remove_from_trees(memcg);
Kirill Tkhai0a4465d2018-08-17 15:47:37 -07005324 memcg_free_shrinker_maps(memcg);
Johannes Weinerd886f4e2016-01-20 15:02:47 -08005325 memcg_free_kmem(memcg);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005326 mem_cgroup_free(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005327}
5328
Tejun Heo1ced9532014-07-08 18:02:57 -04005329/**
5330 * mem_cgroup_css_reset - reset the states of a mem_cgroup
5331 * @css: the target css
5332 *
5333 * Reset the states of the mem_cgroup associated with @css. This is
5334 * invoked when the userland requests disabling on the default hierarchy
5335 * but the memcg is pinned through dependency. The memcg should stop
5336 * applying policies and should revert to the vanilla state as it may be
5337 * made visible again.
5338 *
5339 * The current implementation only resets the essential configurations.
5340 * This needs to be expanded to cover all the visible parts.
5341 */
5342static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
5343{
5344 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5345
Roman Gushchinbbec2e12018-06-07 17:06:18 -07005346 page_counter_set_max(&memcg->memory, PAGE_COUNTER_MAX);
5347 page_counter_set_max(&memcg->swap, PAGE_COUNTER_MAX);
5348 page_counter_set_max(&memcg->memsw, PAGE_COUNTER_MAX);
5349 page_counter_set_max(&memcg->kmem, PAGE_COUNTER_MAX);
5350 page_counter_set_max(&memcg->tcpmem, PAGE_COUNTER_MAX);
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005351 page_counter_set_min(&memcg->memory, 0);
Roman Gushchin23067152018-06-07 17:06:22 -07005352 page_counter_set_low(&memcg->memory, 0);
Jakub Kicinskid1663a92020-06-01 21:49:49 -07005353 page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX);
Johannes Weiner24d404d2015-01-08 14:32:35 -08005354 memcg->soft_limit = PAGE_COUNTER_MAX;
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07005355 page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
Tejun Heo2529bb32015-05-22 18:23:34 -04005356 memcg_wb_domain_size_changed(memcg);
Tejun Heo1ced9532014-07-08 18:02:57 -04005357}
5358
Daisuke Nishimura02491442010-03-10 15:22:17 -08005359#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005360/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005361static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005362{
Johannes Weiner05b84302014-08-06 16:05:59 -07005363 int ret;
Johannes Weiner9476db92014-08-06 16:05:55 -07005364
Mel Gormand0164ad2015-11-06 16:28:21 -08005365 /* Try a single bulk charge without reclaim first, kswapd may wake */
5366 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_DIRECT_RECLAIM, count);
Johannes Weiner9476db92014-08-06 16:05:55 -07005367 if (!ret) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005368 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005369 return ret;
5370 }
Johannes Weiner9476db92014-08-06 16:05:55 -07005371
David Rientjes36745342017-01-24 15:18:10 -08005372 /* Try charges one by one with reclaim, but do not retry */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005373 while (count--) {
David Rientjes36745342017-01-24 15:18:10 -08005374 ret = try_charge(mc.to, GFP_KERNEL | __GFP_NORETRY, 1);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005375 if (ret)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005376 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005377 mc.precharge++;
Johannes Weiner9476db92014-08-06 16:05:55 -07005378 cond_resched();
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005379 }
Johannes Weiner9476db92014-08-06 16:05:55 -07005380 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005381}
5382
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005383union mc_target {
5384 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005385 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005386};
5387
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005388enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005389 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005390 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005391 MC_TARGET_SWAP,
Jérôme Glissec733a822017-09-08 16:11:54 -07005392 MC_TARGET_DEVICE,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005393};
5394
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005395static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5396 unsigned long addr, pte_t ptent)
5397{
Christoph Hellwig25b29952019-06-13 22:50:49 +02005398 struct page *page = vm_normal_page(vma, addr, ptent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005399
5400 if (!page || !page_mapped(page))
5401 return NULL;
5402 if (PageAnon(page)) {
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005403 if (!(mc.flags & MOVE_ANON))
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005404 return NULL;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005405 } else {
5406 if (!(mc.flags & MOVE_FILE))
5407 return NULL;
5408 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005409 if (!get_page_unless_zero(page))
5410 return NULL;
5411
5412 return page;
5413}
5414
Jérôme Glissec733a822017-09-08 16:11:54 -07005415#if defined(CONFIG_SWAP) || defined(CONFIG_DEVICE_PRIVATE)
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005416static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
Li RongQing48406ef2016-07-26 15:22:14 -07005417 pte_t ptent, swp_entry_t *entry)
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005418{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005419 struct page *page = NULL;
5420 swp_entry_t ent = pte_to_swp_entry(ptent);
5421
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005422 if (!(mc.flags & MOVE_ANON) || non_swap_entry(ent))
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005423 return NULL;
Jérôme Glissec733a822017-09-08 16:11:54 -07005424
5425 /*
5426 * Handle MEMORY_DEVICE_PRIVATE which are ZONE_DEVICE page belonging to
5427 * a device and because they are not accessible by CPU they are store
5428 * as special swap entry in the CPU page table.
5429 */
5430 if (is_device_private_entry(ent)) {
5431 page = device_private_entry_to_page(ent);
5432 /*
5433 * MEMORY_DEVICE_PRIVATE means ZONE_DEVICE page and which have
5434 * a refcount of 1 when free (unlike normal page)
5435 */
5436 if (!page_ref_add_unless(page, 1, 1))
5437 return NULL;
5438 return page;
5439 }
5440
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005441 /*
5442 * Because lookup_swap_cache() updates some statistics counter,
5443 * we call find_get_page() with swapper_space directly.
5444 */
Huang Yingf6ab1f72016-10-07 17:00:21 -07005445 page = find_get_page(swap_address_space(ent), swp_offset(ent));
Johannes Weiner2d1c4982020-06-03 16:02:14 -07005446 entry->val = ent.val;
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005447
5448 return page;
5449}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005450#else
5451static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
Li RongQing48406ef2016-07-26 15:22:14 -07005452 pte_t ptent, swp_entry_t *entry)
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005453{
5454 return NULL;
5455}
5456#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005457
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005458static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5459 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5460{
5461 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005462 struct address_space *mapping;
5463 pgoff_t pgoff;
5464
5465 if (!vma->vm_file) /* anonymous vma */
5466 return NULL;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005467 if (!(mc.flags & MOVE_FILE))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005468 return NULL;
5469
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005470 mapping = vma->vm_file->f_mapping;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08005471 pgoff = linear_page_index(vma, addr);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005472
5473 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005474#ifdef CONFIG_SWAP
5475 /* shmem/tmpfs may report page out on swap: account for that too. */
Johannes Weiner139b6a62014-05-06 12:50:05 -07005476 if (shmem_mapping(mapping)) {
5477 page = find_get_entry(mapping, pgoff);
Matthew Wilcox3159f942017-11-03 13:30:42 -04005478 if (xa_is_value(page)) {
Johannes Weiner139b6a62014-05-06 12:50:05 -07005479 swp_entry_t swp = radix_to_swp_entry(page);
Johannes Weiner2d1c4982020-06-03 16:02:14 -07005480 *entry = swp;
Huang Yingf6ab1f72016-10-07 17:00:21 -07005481 page = find_get_page(swap_address_space(swp),
5482 swp_offset(swp));
Johannes Weiner139b6a62014-05-06 12:50:05 -07005483 }
5484 } else
5485 page = find_get_page(mapping, pgoff);
5486#else
5487 page = find_get_page(mapping, pgoff);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005488#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005489 return page;
5490}
5491
Chen Gangb1b0dea2015-04-14 15:47:35 -07005492/**
5493 * mem_cgroup_move_account - move account of the page
5494 * @page: the page
Li RongQing25843c22016-07-26 15:26:56 -07005495 * @compound: charge the page as compound or small page
Chen Gangb1b0dea2015-04-14 15:47:35 -07005496 * @from: mem_cgroup which the page is moved from.
5497 * @to: mem_cgroup which the page is moved to. @from != @to.
5498 *
Kirill A. Shutemov3ac808f2016-01-15 16:53:07 -08005499 * The caller must make sure the page is not on LRU (isolate_page() is useful.)
Chen Gangb1b0dea2015-04-14 15:47:35 -07005500 *
5501 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
5502 * from old cgroup.
5503 */
5504static int mem_cgroup_move_account(struct page *page,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005505 bool compound,
Chen Gangb1b0dea2015-04-14 15:47:35 -07005506 struct mem_cgroup *from,
5507 struct mem_cgroup *to)
5508{
Konstantin Khlebnikovae8af432019-10-18 20:20:11 -07005509 struct lruvec *from_vec, *to_vec;
5510 struct pglist_data *pgdat;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005511 unsigned int nr_pages = compound ? hpage_nr_pages(page) : 1;
Chen Gangb1b0dea2015-04-14 15:47:35 -07005512 int ret;
5513
5514 VM_BUG_ON(from == to);
5515 VM_BUG_ON_PAGE(PageLRU(page), page);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005516 VM_BUG_ON(compound && !PageTransHuge(page));
Chen Gangb1b0dea2015-04-14 15:47:35 -07005517
5518 /*
Johannes Weiner6a93ca82016-03-15 14:57:19 -07005519 * Prevent mem_cgroup_migrate() from looking at
Hugh Dickins45637ba2015-11-05 18:49:40 -08005520 * page->mem_cgroup of its source page while we change it.
Chen Gangb1b0dea2015-04-14 15:47:35 -07005521 */
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005522 ret = -EBUSY;
Chen Gangb1b0dea2015-04-14 15:47:35 -07005523 if (!trylock_page(page))
5524 goto out;
5525
5526 ret = -EINVAL;
5527 if (page->mem_cgroup != from)
5528 goto out_unlock;
5529
Konstantin Khlebnikovae8af432019-10-18 20:20:11 -07005530 pgdat = page_pgdat(page);
Johannes Weiner867e5e12019-11-30 17:55:34 -08005531 from_vec = mem_cgroup_lruvec(from, pgdat);
5532 to_vec = mem_cgroup_lruvec(to, pgdat);
Konstantin Khlebnikovae8af432019-10-18 20:20:11 -07005533
Johannes Weinerabb242f2020-06-03 16:01:28 -07005534 lock_page_memcg(page);
Chen Gangb1b0dea2015-04-14 15:47:35 -07005535
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07005536 if (PageAnon(page)) {
5537 if (page_mapped(page)) {
5538 __mod_lruvec_state(from_vec, NR_ANON_MAPPED, -nr_pages);
5539 __mod_lruvec_state(to_vec, NR_ANON_MAPPED, nr_pages);
Johannes Weiner468c3982020-06-03 16:02:01 -07005540 if (PageTransHuge(page)) {
5541 __mod_lruvec_state(from_vec, NR_ANON_THPS,
5542 -nr_pages);
5543 __mod_lruvec_state(to_vec, NR_ANON_THPS,
5544 nr_pages);
5545 }
5546
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07005547 }
5548 } else {
Johannes Weiner0d1c2072020-06-03 16:01:54 -07005549 __mod_lruvec_state(from_vec, NR_FILE_PAGES, -nr_pages);
5550 __mod_lruvec_state(to_vec, NR_FILE_PAGES, nr_pages);
5551
5552 if (PageSwapBacked(page)) {
5553 __mod_lruvec_state(from_vec, NR_SHMEM, -nr_pages);
5554 __mod_lruvec_state(to_vec, NR_SHMEM, nr_pages);
5555 }
5556
Johannes Weiner49e50d22020-06-03 16:01:47 -07005557 if (page_mapped(page)) {
5558 __mod_lruvec_state(from_vec, NR_FILE_MAPPED, -nr_pages);
5559 __mod_lruvec_state(to_vec, NR_FILE_MAPPED, nr_pages);
5560 }
Chen Gangb1b0dea2015-04-14 15:47:35 -07005561
Johannes Weiner49e50d22020-06-03 16:01:47 -07005562 if (PageDirty(page)) {
5563 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -04005564
Johannes Weiner49e50d22020-06-03 16:01:47 -07005565 if (mapping_cap_account_dirty(mapping)) {
5566 __mod_lruvec_state(from_vec, NR_FILE_DIRTY,
5567 -nr_pages);
5568 __mod_lruvec_state(to_vec, NR_FILE_DIRTY,
5569 nr_pages);
5570 }
Greg Thelenc4843a72015-05-22 17:13:16 -04005571 }
5572 }
5573
Chen Gangb1b0dea2015-04-14 15:47:35 -07005574 if (PageWriteback(page)) {
Konstantin Khlebnikovae8af432019-10-18 20:20:11 -07005575 __mod_lruvec_state(from_vec, NR_WRITEBACK, -nr_pages);
5576 __mod_lruvec_state(to_vec, NR_WRITEBACK, nr_pages);
Chen Gangb1b0dea2015-04-14 15:47:35 -07005577 }
5578
5579 /*
Johannes Weinerabb242f2020-06-03 16:01:28 -07005580 * All state has been migrated, let's switch to the new memcg.
5581 *
Chen Gangb1b0dea2015-04-14 15:47:35 -07005582 * It is safe to change page->mem_cgroup here because the page
Johannes Weinerabb242f2020-06-03 16:01:28 -07005583 * is referenced, charged, isolated, and locked: we can't race
5584 * with (un)charging, migration, LRU putback, or anything else
5585 * that would rely on a stable page->mem_cgroup.
5586 *
5587 * Note that lock_page_memcg is a memcg lock, not a page lock,
5588 * to save space. As soon as we switch page->mem_cgroup to a
5589 * new memcg that isn't locked, the above state can change
5590 * concurrently again. Make sure we're truly done with it.
Chen Gangb1b0dea2015-04-14 15:47:35 -07005591 */
Johannes Weinerabb242f2020-06-03 16:01:28 -07005592 smp_mb();
Chen Gangb1b0dea2015-04-14 15:47:35 -07005593
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07005594 css_get(&to->css);
5595 css_put(&from->css);
5596
5597 page->mem_cgroup = to;
Yang Shi87eaceb2019-09-23 15:38:15 -07005598
Johannes Weinerabb242f2020-06-03 16:01:28 -07005599 __unlock_page_memcg(from);
Chen Gangb1b0dea2015-04-14 15:47:35 -07005600
5601 ret = 0;
5602
5603 local_irq_disable();
Johannes Weiner3fba69a2020-06-03 16:01:31 -07005604 mem_cgroup_charge_statistics(to, page, nr_pages);
Chen Gangb1b0dea2015-04-14 15:47:35 -07005605 memcg_check_events(to, page);
Johannes Weiner3fba69a2020-06-03 16:01:31 -07005606 mem_cgroup_charge_statistics(from, page, -nr_pages);
Chen Gangb1b0dea2015-04-14 15:47:35 -07005607 memcg_check_events(from, page);
5608 local_irq_enable();
5609out_unlock:
5610 unlock_page(page);
5611out:
5612 return ret;
5613}
5614
Li RongQing7cf78062016-05-27 14:27:46 -07005615/**
5616 * get_mctgt_type - get target type of moving charge
5617 * @vma: the vma the pte to be checked belongs
5618 * @addr: the address corresponding to the pte to be checked
5619 * @ptent: the pte to be checked
5620 * @target: the pointer the target page or swap ent will be stored(can be NULL)
5621 *
5622 * Returns
5623 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5624 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5625 * move charge. if @target is not NULL, the page is stored in target->page
5626 * with extra refcnt got(Callers should handle it).
5627 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5628 * target for charge migration. if @target is not NULL, the entry is stored
5629 * in target->ent.
Christoph Hellwig25b29952019-06-13 22:50:49 +02005630 * 3(MC_TARGET_DEVICE): like MC_TARGET_PAGE but page is MEMORY_DEVICE_PRIVATE
5631 * (so ZONE_DEVICE page and thus not on the lru).
Jérôme Glissedf6ad692017-09-08 16:12:24 -07005632 * For now we such page is charge like a regular page would be as for all
5633 * intent and purposes it is just special memory taking the place of a
5634 * regular page.
Jérôme Glissec733a822017-09-08 16:11:54 -07005635 *
5636 * See Documentations/vm/hmm.txt and include/linux/hmm.h
Li RongQing7cf78062016-05-27 14:27:46 -07005637 *
5638 * Called with pte lock held.
5639 */
5640
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005641static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005642 unsigned long addr, pte_t ptent, union mc_target *target)
5643{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005644 struct page *page = NULL;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005645 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005646 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005647
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005648 if (pte_present(ptent))
5649 page = mc_handle_present_pte(vma, addr, ptent);
5650 else if (is_swap_pte(ptent))
Li RongQing48406ef2016-07-26 15:22:14 -07005651 page = mc_handle_swap_pte(vma, ptent, &ent);
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08005652 else if (pte_none(ptent))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005653 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005654
5655 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005656 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005657 if (page) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005658 /*
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005659 * Do only loose check w/o serialization.
Johannes Weiner1306a852014-12-10 15:44:52 -08005660 * mem_cgroup_move_account() checks the page is valid or
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005661 * not under LRU exclusion.
Daisuke Nishimura02491442010-03-10 15:22:17 -08005662 */
Johannes Weiner1306a852014-12-10 15:44:52 -08005663 if (page->mem_cgroup == mc.from) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005664 ret = MC_TARGET_PAGE;
Christoph Hellwig25b29952019-06-13 22:50:49 +02005665 if (is_device_private_page(page))
Jérôme Glissec733a822017-09-08 16:11:54 -07005666 ret = MC_TARGET_DEVICE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005667 if (target)
5668 target->page = page;
5669 }
5670 if (!ret || !target)
5671 put_page(page);
5672 }
Huang Ying3e14a572017-09-06 16:22:37 -07005673 /*
5674 * There is a swap entry and a page doesn't exist or isn't charged.
5675 * But we cannot move a tail-page in a THP.
5676 */
5677 if (ent.val && !ret && (!page || !PageTransCompound(page)) &&
Li Zefan34c00c32013-09-23 16:56:01 +08005678 mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005679 ret = MC_TARGET_SWAP;
5680 if (target)
5681 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005682 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005683 return ret;
5684}
5685
Naoya Horiguchi12724852012-03-21 16:34:28 -07005686#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5687/*
Huang Yingd6810d72017-09-06 16:22:45 -07005688 * We don't consider PMD mapped swapping or file mapped pages because THP does
5689 * not support them for now.
Naoya Horiguchi12724852012-03-21 16:34:28 -07005690 * Caller should make sure that pmd_trans_huge(pmd) is true.
5691 */
5692static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5693 unsigned long addr, pmd_t pmd, union mc_target *target)
5694{
5695 struct page *page = NULL;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005696 enum mc_target_type ret = MC_TARGET_NONE;
5697
Zi Yan84c3fc42017-09-08 16:11:01 -07005698 if (unlikely(is_swap_pmd(pmd))) {
5699 VM_BUG_ON(thp_migration_supported() &&
5700 !is_pmd_migration_entry(pmd));
5701 return ret;
5702 }
Naoya Horiguchi12724852012-03-21 16:34:28 -07005703 page = pmd_page(pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08005704 VM_BUG_ON_PAGE(!page || !PageHead(page), page);
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005705 if (!(mc.flags & MOVE_ANON))
Naoya Horiguchi12724852012-03-21 16:34:28 -07005706 return ret;
Johannes Weiner1306a852014-12-10 15:44:52 -08005707 if (page->mem_cgroup == mc.from) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005708 ret = MC_TARGET_PAGE;
5709 if (target) {
5710 get_page(page);
5711 target->page = page;
5712 }
5713 }
5714 return ret;
5715}
5716#else
5717static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5718 unsigned long addr, pmd_t pmd, union mc_target *target)
5719{
5720 return MC_TARGET_NONE;
5721}
5722#endif
5723
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005724static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5725 unsigned long addr, unsigned long end,
5726 struct mm_walk *walk)
5727{
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005728 struct vm_area_struct *vma = walk->vma;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005729 pte_t *pte;
5730 spinlock_t *ptl;
5731
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08005732 ptl = pmd_trans_huge_lock(pmd, vma);
5733 if (ptl) {
Jérôme Glissec733a822017-09-08 16:11:54 -07005734 /*
5735 * Note their can not be MC_TARGET_DEVICE for now as we do not
Christoph Hellwig25b29952019-06-13 22:50:49 +02005736 * support transparent huge page with MEMORY_DEVICE_PRIVATE but
5737 * this might change.
Jérôme Glissec733a822017-09-08 16:11:54 -07005738 */
Naoya Horiguchi12724852012-03-21 16:34:28 -07005739 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
5740 mc.precharge += HPAGE_PMD_NR;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005741 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005742 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005743 }
Dave Hansen03319322011-03-22 16:32:56 -07005744
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005745 if (pmd_trans_unstable(pmd))
5746 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005747 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5748 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005749 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005750 mc.precharge++; /* increment precharge temporarily */
5751 pte_unmap_unlock(pte - 1, ptl);
5752 cond_resched();
5753
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005754 return 0;
5755}
5756
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02005757static const struct mm_walk_ops precharge_walk_ops = {
5758 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5759};
5760
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005761static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5762{
5763 unsigned long precharge;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005764
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07005765 mmap_read_lock(mm);
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02005766 walk_page_range(mm, 0, mm->highest_vm_end, &precharge_walk_ops, NULL);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07005767 mmap_read_unlock(mm);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005768
5769 precharge = mc.precharge;
5770 mc.precharge = 0;
5771
5772 return precharge;
5773}
5774
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005775static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5776{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005777 unsigned long precharge = mem_cgroup_count_precharge(mm);
5778
5779 VM_BUG_ON(mc.moving_task);
5780 mc.moving_task = current;
5781 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005782}
5783
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005784/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5785static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005786{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005787 struct mem_cgroup *from = mc.from;
5788 struct mem_cgroup *to = mc.to;
5789
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005790 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005791 if (mc.precharge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005792 cancel_charge(mc.to, mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005793 mc.precharge = 0;
5794 }
5795 /*
5796 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5797 * we must uncharge here.
5798 */
5799 if (mc.moved_charge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005800 cancel_charge(mc.from, mc.moved_charge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005801 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005802 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005803 /* we must fixup refcnts and charges */
5804 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005805 /* uncharge swap account from the old cgroup */
Johannes Weinerce00a962014-09-05 08:43:57 -04005806 if (!mem_cgroup_is_root(mc.from))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005807 page_counter_uncharge(&mc.from->memsw, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005808
Vladimir Davydov615d66c2016-08-11 15:33:03 -07005809 mem_cgroup_id_put_many(mc.from, mc.moved_swap);
5810
Johannes Weiner05b84302014-08-06 16:05:59 -07005811 /*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005812 * we charged both to->memory and to->memsw, so we
5813 * should uncharge to->memory.
Johannes Weiner05b84302014-08-06 16:05:59 -07005814 */
Johannes Weinerce00a962014-09-05 08:43:57 -04005815 if (!mem_cgroup_is_root(mc.to))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005816 page_counter_uncharge(&mc.to->memory, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005817
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005818 mc.moved_swap = 0;
5819 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005820 memcg_oom_recover(from);
5821 memcg_oom_recover(to);
5822 wake_up_all(&mc.waitq);
5823}
5824
5825static void mem_cgroup_clear_mc(void)
5826{
Tejun Heo264a0ae2016-04-21 19:09:02 -04005827 struct mm_struct *mm = mc.mm;
5828
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005829 /*
5830 * we must clear moving_task before waking up waiters at the end of
5831 * task migration.
5832 */
5833 mc.moving_task = NULL;
5834 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005835 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005836 mc.from = NULL;
5837 mc.to = NULL;
Tejun Heo264a0ae2016-04-21 19:09:02 -04005838 mc.mm = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005839 spin_unlock(&mc.lock);
Tejun Heo264a0ae2016-04-21 19:09:02 -04005840
5841 mmput(mm);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005842}
5843
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005844static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005845{
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005846 struct cgroup_subsys_state *css;
Ross Zwislereed67d72015-12-23 14:53:27 -07005847 struct mem_cgroup *memcg = NULL; /* unneeded init to make gcc happy */
Tejun Heo9f2115f2015-09-08 15:01:10 -07005848 struct mem_cgroup *from;
Tejun Heo4530edd2015-09-11 15:00:19 -04005849 struct task_struct *leader, *p;
Tejun Heo9f2115f2015-09-08 15:01:10 -07005850 struct mm_struct *mm;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005851 unsigned long move_flags;
Tejun Heo9f2115f2015-09-08 15:01:10 -07005852 int ret = 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005853
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005854 /* charge immigration isn't supported on the default hierarchy */
5855 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
Tejun Heo9f2115f2015-09-08 15:01:10 -07005856 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005857
Tejun Heo4530edd2015-09-11 15:00:19 -04005858 /*
5859 * Multi-process migrations only happen on the default hierarchy
5860 * where charge immigration is not used. Perform charge
5861 * immigration if @tset contains a leader and whine if there are
5862 * multiple.
5863 */
5864 p = NULL;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005865 cgroup_taskset_for_each_leader(leader, css, tset) {
Tejun Heo4530edd2015-09-11 15:00:19 -04005866 WARN_ON_ONCE(p);
5867 p = leader;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005868 memcg = mem_cgroup_from_css(css);
Tejun Heo4530edd2015-09-11 15:00:19 -04005869 }
5870 if (!p)
5871 return 0;
5872
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005873 /*
5874 * We are now commited to this value whatever it is. Changes in this
5875 * tunable will only affect upcoming migrations, not the current one.
5876 * So we need to save it, and keep it going.
5877 */
5878 move_flags = READ_ONCE(memcg->move_charge_at_immigrate);
5879 if (!move_flags)
5880 return 0;
5881
Tejun Heo9f2115f2015-09-08 15:01:10 -07005882 from = mem_cgroup_from_task(p);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005883
Tejun Heo9f2115f2015-09-08 15:01:10 -07005884 VM_BUG_ON(from == memcg);
Johannes Weiner247b1442014-12-10 15:44:11 -08005885
Tejun Heo9f2115f2015-09-08 15:01:10 -07005886 mm = get_task_mm(p);
5887 if (!mm)
5888 return 0;
5889 /* We move charges only when we move a owner of the mm */
5890 if (mm->owner == p) {
5891 VM_BUG_ON(mc.from);
5892 VM_BUG_ON(mc.to);
5893 VM_BUG_ON(mc.precharge);
5894 VM_BUG_ON(mc.moved_charge);
5895 VM_BUG_ON(mc.moved_swap);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005896
Tejun Heo9f2115f2015-09-08 15:01:10 -07005897 spin_lock(&mc.lock);
Tejun Heo264a0ae2016-04-21 19:09:02 -04005898 mc.mm = mm;
Tejun Heo9f2115f2015-09-08 15:01:10 -07005899 mc.from = from;
5900 mc.to = memcg;
5901 mc.flags = move_flags;
5902 spin_unlock(&mc.lock);
5903 /* We set mc.moving_task later */
5904
5905 ret = mem_cgroup_precharge_mc(mm);
5906 if (ret)
5907 mem_cgroup_clear_mc();
Tejun Heo264a0ae2016-04-21 19:09:02 -04005908 } else {
5909 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005910 }
5911 return ret;
5912}
5913
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005914static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005915{
Johannes Weiner4e2f2452014-12-10 15:44:08 -08005916 if (mc.to)
5917 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005918}
5919
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005920static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5921 unsigned long addr, unsigned long end,
5922 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005923{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005924 int ret = 0;
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005925 struct vm_area_struct *vma = walk->vma;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005926 pte_t *pte;
5927 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005928 enum mc_target_type target_type;
5929 union mc_target target;
5930 struct page *page;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005931
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08005932 ptl = pmd_trans_huge_lock(pmd, vma);
5933 if (ptl) {
Hugh Dickins62ade862012-05-18 11:28:34 -07005934 if (mc.precharge < HPAGE_PMD_NR) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005935 spin_unlock(ptl);
Naoya Horiguchi12724852012-03-21 16:34:28 -07005936 return 0;
5937 }
5938 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
5939 if (target_type == MC_TARGET_PAGE) {
5940 page = target.page;
5941 if (!isolate_lru_page(page)) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005942 if (!mem_cgroup_move_account(page, true,
Johannes Weiner1306a852014-12-10 15:44:52 -08005943 mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005944 mc.precharge -= HPAGE_PMD_NR;
5945 mc.moved_charge += HPAGE_PMD_NR;
5946 }
5947 putback_lru_page(page);
5948 }
5949 put_page(page);
Jérôme Glissec733a822017-09-08 16:11:54 -07005950 } else if (target_type == MC_TARGET_DEVICE) {
5951 page = target.page;
5952 if (!mem_cgroup_move_account(page, true,
5953 mc.from, mc.to)) {
5954 mc.precharge -= HPAGE_PMD_NR;
5955 mc.moved_charge += HPAGE_PMD_NR;
5956 }
5957 put_page(page);
Naoya Horiguchi12724852012-03-21 16:34:28 -07005958 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005959 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005960 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005961 }
5962
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005963 if (pmd_trans_unstable(pmd))
5964 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005965retry:
5966 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5967 for (; addr != end; addr += PAGE_SIZE) {
5968 pte_t ptent = *(pte++);
Jérôme Glissec733a822017-09-08 16:11:54 -07005969 bool device = false;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005970 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005971
5972 if (!mc.precharge)
5973 break;
5974
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005975 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Jérôme Glissec733a822017-09-08 16:11:54 -07005976 case MC_TARGET_DEVICE:
5977 device = true;
Joe Perchese4a9bc52020-04-06 20:08:39 -07005978 fallthrough;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005979 case MC_TARGET_PAGE:
5980 page = target.page;
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08005981 /*
5982 * We can have a part of the split pmd here. Moving it
5983 * can be done but it would be too convoluted so simply
5984 * ignore such a partial THP and keep it in original
5985 * memcg. There should be somebody mapping the head.
5986 */
5987 if (PageTransCompound(page))
5988 goto put;
Jérôme Glissec733a822017-09-08 16:11:54 -07005989 if (!device && isolate_lru_page(page))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005990 goto put;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005991 if (!mem_cgroup_move_account(page, false,
5992 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005993 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005994 /* we uncharge from mc.from later. */
5995 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005996 }
Jérôme Glissec733a822017-09-08 16:11:54 -07005997 if (!device)
5998 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005999put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006000 put_page(page);
6001 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006002 case MC_TARGET_SWAP:
6003 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006004 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006005 mc.precharge--;
Hugh Dickins8d22a932020-07-23 21:15:24 -07006006 mem_cgroup_id_get_many(mc.to, 1);
6007 /* we fixup other refcnts and charges later. */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006008 mc.moved_swap++;
6009 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006010 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006011 default:
6012 break;
6013 }
6014 }
6015 pte_unmap_unlock(pte - 1, ptl);
6016 cond_resched();
6017
6018 if (addr != end) {
6019 /*
6020 * We have consumed all precharges we got in can_attach().
6021 * We try charge one by one, but don't do any additional
6022 * charges to mc.to if we have failed in charge once in attach()
6023 * phase.
6024 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006025 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006026 if (!ret)
6027 goto retry;
6028 }
6029
6030 return ret;
6031}
6032
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02006033static const struct mm_walk_ops charge_walk_ops = {
6034 .pmd_entry = mem_cgroup_move_charge_pte_range,
6035};
6036
Tejun Heo264a0ae2016-04-21 19:09:02 -04006037static void mem_cgroup_move_charge(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006038{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006039 lru_add_drain_all();
Johannes Weiner312722c2014-12-10 15:44:25 -08006040 /*
Johannes Weiner81f8c3a2016-03-15 14:57:04 -07006041 * Signal lock_page_memcg() to take the memcg's move_lock
6042 * while we're moving its pages to another memcg. Then wait
6043 * for already started RCU-only updates to finish.
Johannes Weiner312722c2014-12-10 15:44:25 -08006044 */
6045 atomic_inc(&mc.from->moving_account);
6046 synchronize_rcu();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006047retry:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07006048 if (unlikely(!mmap_read_trylock(mc.mm))) {
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006049 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07006050 * Someone who are holding the mmap_lock might be waiting in
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006051 * waitq. So we cancel all extra charges, wake up all waiters,
6052 * and retry. Because we cancel precharges, we might not be able
6053 * to move enough charges, but moving charge is a best-effort
6054 * feature anyway, so it wouldn't be a big problem.
6055 */
6056 __mem_cgroup_clear_mc();
6057 cond_resched();
6058 goto retry;
6059 }
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08006060 /*
6061 * When we have consumed all precharges and failed in doing
6062 * additional charge, the page walk just aborts.
6063 */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02006064 walk_page_range(mc.mm, 0, mc.mm->highest_vm_end, &charge_walk_ops,
6065 NULL);
James Morse0247f3f2016-10-07 17:00:12 -07006066
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07006067 mmap_read_unlock(mc.mm);
Johannes Weiner312722c2014-12-10 15:44:25 -08006068 atomic_dec(&mc.from->moving_account);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006069}
6070
Tejun Heo264a0ae2016-04-21 19:09:02 -04006071static void mem_cgroup_move_task(void)
Balbir Singh67e465a2008-02-07 00:13:54 -08006072{
Tejun Heo264a0ae2016-04-21 19:09:02 -04006073 if (mc.to) {
6074 mem_cgroup_move_charge();
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006075 mem_cgroup_clear_mc();
Tejun Heo264a0ae2016-04-21 19:09:02 -04006076 }
Balbir Singh67e465a2008-02-07 00:13:54 -08006077}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006078#else /* !CONFIG_MMU */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05006079static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006080{
6081 return 0;
6082}
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05006083static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006084{
6085}
Tejun Heo264a0ae2016-04-21 19:09:02 -04006086static void mem_cgroup_move_task(void)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006087{
6088}
6089#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006090
Tejun Heof00baae2013-04-15 13:41:15 -07006091/*
6092 * Cgroup retains root cgroups across [un]mount cycles making it necessary
Tejun Heoaa6ec292014-07-09 10:08:08 -04006093 * to verify whether we're attached to the default hierarchy on each mount
6094 * attempt.
Tejun Heof00baae2013-04-15 13:41:15 -07006095 */
Tejun Heoeb954192013-08-08 20:11:23 -04006096static void mem_cgroup_bind(struct cgroup_subsys_state *root_css)
Tejun Heof00baae2013-04-15 13:41:15 -07006097{
6098 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -04006099 * use_hierarchy is forced on the default hierarchy. cgroup core
Tejun Heof00baae2013-04-15 13:41:15 -07006100 * guarantees that @root doesn't have any children, so turning it
6101 * on for the root memcg is enough.
6102 */
Tejun Heo9e10a132015-09-18 11:56:28 -04006103 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
Vladimir Davydov7feee5902015-03-12 16:26:19 -07006104 root_mem_cgroup->use_hierarchy = true;
6105 else
6106 root_mem_cgroup->use_hierarchy = false;
Tejun Heof00baae2013-04-15 13:41:15 -07006107}
6108
Chris Down677dc972019-03-05 15:45:55 -08006109static int seq_puts_memcg_tunable(struct seq_file *m, unsigned long value)
6110{
6111 if (value == PAGE_COUNTER_MAX)
6112 seq_puts(m, "max\n");
6113 else
6114 seq_printf(m, "%llu\n", (u64)value * PAGE_SIZE);
6115
6116 return 0;
6117}
6118
Johannes Weiner241994ed2015-02-11 15:26:06 -08006119static u64 memory_current_read(struct cgroup_subsys_state *css,
6120 struct cftype *cft)
6121{
Johannes Weinerf5fc3c5d2015-11-05 18:50:23 -08006122 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
6123
6124 return (u64)page_counter_read(&memcg->memory) * PAGE_SIZE;
Johannes Weiner241994ed2015-02-11 15:26:06 -08006125}
6126
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006127static int memory_min_show(struct seq_file *m, void *v)
6128{
Chris Down677dc972019-03-05 15:45:55 -08006129 return seq_puts_memcg_tunable(m,
6130 READ_ONCE(mem_cgroup_from_seq(m)->memory.min));
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006131}
6132
6133static ssize_t memory_min_write(struct kernfs_open_file *of,
6134 char *buf, size_t nbytes, loff_t off)
6135{
6136 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6137 unsigned long min;
6138 int err;
6139
6140 buf = strstrip(buf);
6141 err = page_counter_memparse(buf, "max", &min);
6142 if (err)
6143 return err;
6144
6145 page_counter_set_min(&memcg->memory, min);
6146
6147 return nbytes;
6148}
6149
Johannes Weiner241994ed2015-02-11 15:26:06 -08006150static int memory_low_show(struct seq_file *m, void *v)
6151{
Chris Down677dc972019-03-05 15:45:55 -08006152 return seq_puts_memcg_tunable(m,
6153 READ_ONCE(mem_cgroup_from_seq(m)->memory.low));
Johannes Weiner241994ed2015-02-11 15:26:06 -08006154}
6155
6156static ssize_t memory_low_write(struct kernfs_open_file *of,
6157 char *buf, size_t nbytes, loff_t off)
6158{
6159 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6160 unsigned long low;
6161 int err;
6162
6163 buf = strstrip(buf);
Johannes Weinerd2973692015-02-27 15:52:04 -08006164 err = page_counter_memparse(buf, "max", &low);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006165 if (err)
6166 return err;
6167
Roman Gushchin23067152018-06-07 17:06:22 -07006168 page_counter_set_low(&memcg->memory, low);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006169
6170 return nbytes;
6171}
6172
6173static int memory_high_show(struct seq_file *m, void *v)
6174{
Jakub Kicinskid1663a92020-06-01 21:49:49 -07006175 return seq_puts_memcg_tunable(m,
6176 READ_ONCE(mem_cgroup_from_seq(m)->memory.high));
Johannes Weiner241994ed2015-02-11 15:26:06 -08006177}
6178
6179static ssize_t memory_high_write(struct kernfs_open_file *of,
6180 char *buf, size_t nbytes, loff_t off)
6181{
6182 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner8c8c3832019-11-30 17:50:09 -08006183 unsigned int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
6184 bool drained = false;
Johannes Weiner241994ed2015-02-11 15:26:06 -08006185 unsigned long high;
6186 int err;
6187
6188 buf = strstrip(buf);
Johannes Weinerd2973692015-02-27 15:52:04 -08006189 err = page_counter_memparse(buf, "max", &high);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006190 if (err)
6191 return err;
6192
Jakub Kicinskid1663a92020-06-01 21:49:49 -07006193 page_counter_set_high(&memcg->memory, high);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006194
Johannes Weiner8c8c3832019-11-30 17:50:09 -08006195 for (;;) {
6196 unsigned long nr_pages = page_counter_read(&memcg->memory);
6197 unsigned long reclaimed;
Johannes Weiner588083b2016-03-17 14:20:25 -07006198
Johannes Weiner8c8c3832019-11-30 17:50:09 -08006199 if (nr_pages <= high)
6200 break;
6201
6202 if (signal_pending(current))
6203 break;
6204
6205 if (!drained) {
6206 drain_all_stock(memcg);
6207 drained = true;
6208 continue;
6209 }
6210
6211 reclaimed = try_to_free_mem_cgroup_pages(memcg, nr_pages - high,
6212 GFP_KERNEL, true);
6213
6214 if (!reclaimed && !nr_retries--)
6215 break;
6216 }
6217
Johannes Weiner241994ed2015-02-11 15:26:06 -08006218 return nbytes;
6219}
6220
6221static int memory_max_show(struct seq_file *m, void *v)
6222{
Chris Down677dc972019-03-05 15:45:55 -08006223 return seq_puts_memcg_tunable(m,
6224 READ_ONCE(mem_cgroup_from_seq(m)->memory.max));
Johannes Weiner241994ed2015-02-11 15:26:06 -08006225}
6226
6227static ssize_t memory_max_write(struct kernfs_open_file *of,
6228 char *buf, size_t nbytes, loff_t off)
6229{
6230 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07006231 unsigned int nr_reclaims = MEM_CGROUP_RECLAIM_RETRIES;
6232 bool drained = false;
Johannes Weiner241994ed2015-02-11 15:26:06 -08006233 unsigned long max;
6234 int err;
6235
6236 buf = strstrip(buf);
Johannes Weinerd2973692015-02-27 15:52:04 -08006237 err = page_counter_memparse(buf, "max", &max);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006238 if (err)
6239 return err;
6240
Roman Gushchinbbec2e12018-06-07 17:06:18 -07006241 xchg(&memcg->memory.max, max);
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07006242
6243 for (;;) {
6244 unsigned long nr_pages = page_counter_read(&memcg->memory);
6245
6246 if (nr_pages <= max)
6247 break;
6248
Johannes Weiner7249c9f2019-11-30 17:50:06 -08006249 if (signal_pending(current))
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07006250 break;
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07006251
6252 if (!drained) {
6253 drain_all_stock(memcg);
6254 drained = true;
6255 continue;
6256 }
6257
6258 if (nr_reclaims) {
6259 if (!try_to_free_mem_cgroup_pages(memcg, nr_pages - max,
6260 GFP_KERNEL, true))
6261 nr_reclaims--;
6262 continue;
6263 }
6264
Johannes Weinere27be242018-04-10 16:29:45 -07006265 memcg_memory_event(memcg, MEMCG_OOM);
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07006266 if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0))
6267 break;
6268 }
Johannes Weiner241994ed2015-02-11 15:26:06 -08006269
Tejun Heo2529bb32015-05-22 18:23:34 -04006270 memcg_wb_domain_size_changed(memcg);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006271 return nbytes;
6272}
6273
Shakeel Butt1e577f92019-07-11 20:55:55 -07006274static void __memory_events_show(struct seq_file *m, atomic_long_t *events)
6275{
6276 seq_printf(m, "low %lu\n", atomic_long_read(&events[MEMCG_LOW]));
6277 seq_printf(m, "high %lu\n", atomic_long_read(&events[MEMCG_HIGH]));
6278 seq_printf(m, "max %lu\n", atomic_long_read(&events[MEMCG_MAX]));
6279 seq_printf(m, "oom %lu\n", atomic_long_read(&events[MEMCG_OOM]));
6280 seq_printf(m, "oom_kill %lu\n",
6281 atomic_long_read(&events[MEMCG_OOM_KILL]));
6282}
6283
Johannes Weiner241994ed2015-02-11 15:26:06 -08006284static int memory_events_show(struct seq_file *m, void *v)
6285{
Chris Downaa9694b2019-03-05 15:45:52 -08006286 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006287
Shakeel Butt1e577f92019-07-11 20:55:55 -07006288 __memory_events_show(m, memcg->memory_events);
6289 return 0;
6290}
Johannes Weiner241994ed2015-02-11 15:26:06 -08006291
Shakeel Butt1e577f92019-07-11 20:55:55 -07006292static int memory_events_local_show(struct seq_file *m, void *v)
6293{
6294 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
6295
6296 __memory_events_show(m, memcg->memory_events_local);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006297 return 0;
6298}
6299
Johannes Weiner587d9f72016-01-20 15:03:19 -08006300static int memory_stat_show(struct seq_file *m, void *v)
6301{
Chris Downaa9694b2019-03-05 15:45:52 -08006302 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Johannes Weinerc8713d02019-07-11 20:55:59 -07006303 char *buf;
Johannes Weiner587d9f72016-01-20 15:03:19 -08006304
Johannes Weinerc8713d02019-07-11 20:55:59 -07006305 buf = memory_stat_format(memcg);
6306 if (!buf)
6307 return -ENOMEM;
6308 seq_puts(m, buf);
6309 kfree(buf);
Johannes Weiner587d9f72016-01-20 15:03:19 -08006310 return 0;
6311}
6312
Roman Gushchin3d8b38e2018-08-21 21:53:54 -07006313static int memory_oom_group_show(struct seq_file *m, void *v)
6314{
Chris Downaa9694b2019-03-05 15:45:52 -08006315 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Roman Gushchin3d8b38e2018-08-21 21:53:54 -07006316
6317 seq_printf(m, "%d\n", memcg->oom_group);
6318
6319 return 0;
6320}
6321
6322static ssize_t memory_oom_group_write(struct kernfs_open_file *of,
6323 char *buf, size_t nbytes, loff_t off)
6324{
6325 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6326 int ret, oom_group;
6327
6328 buf = strstrip(buf);
6329 if (!buf)
6330 return -EINVAL;
6331
6332 ret = kstrtoint(buf, 0, &oom_group);
6333 if (ret)
6334 return ret;
6335
6336 if (oom_group != 0 && oom_group != 1)
6337 return -EINVAL;
6338
6339 memcg->oom_group = oom_group;
6340
6341 return nbytes;
6342}
6343
Johannes Weiner241994ed2015-02-11 15:26:06 -08006344static struct cftype memory_files[] = {
6345 {
6346 .name = "current",
Johannes Weinerf5fc3c5d2015-11-05 18:50:23 -08006347 .flags = CFTYPE_NOT_ON_ROOT,
Johannes Weiner241994ed2015-02-11 15:26:06 -08006348 .read_u64 = memory_current_read,
6349 },
6350 {
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006351 .name = "min",
6352 .flags = CFTYPE_NOT_ON_ROOT,
6353 .seq_show = memory_min_show,
6354 .write = memory_min_write,
6355 },
6356 {
Johannes Weiner241994ed2015-02-11 15:26:06 -08006357 .name = "low",
6358 .flags = CFTYPE_NOT_ON_ROOT,
6359 .seq_show = memory_low_show,
6360 .write = memory_low_write,
6361 },
6362 {
6363 .name = "high",
6364 .flags = CFTYPE_NOT_ON_ROOT,
6365 .seq_show = memory_high_show,
6366 .write = memory_high_write,
6367 },
6368 {
6369 .name = "max",
6370 .flags = CFTYPE_NOT_ON_ROOT,
6371 .seq_show = memory_max_show,
6372 .write = memory_max_write,
6373 },
6374 {
6375 .name = "events",
6376 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo472912a2015-09-18 18:01:59 -04006377 .file_offset = offsetof(struct mem_cgroup, events_file),
Johannes Weiner241994ed2015-02-11 15:26:06 -08006378 .seq_show = memory_events_show,
6379 },
Johannes Weiner587d9f72016-01-20 15:03:19 -08006380 {
Shakeel Butt1e577f92019-07-11 20:55:55 -07006381 .name = "events.local",
6382 .flags = CFTYPE_NOT_ON_ROOT,
6383 .file_offset = offsetof(struct mem_cgroup, events_local_file),
6384 .seq_show = memory_events_local_show,
6385 },
6386 {
Johannes Weiner587d9f72016-01-20 15:03:19 -08006387 .name = "stat",
Johannes Weiner587d9f72016-01-20 15:03:19 -08006388 .seq_show = memory_stat_show,
6389 },
Roman Gushchin3d8b38e2018-08-21 21:53:54 -07006390 {
6391 .name = "oom.group",
6392 .flags = CFTYPE_NOT_ON_ROOT | CFTYPE_NS_DELEGATABLE,
6393 .seq_show = memory_oom_group_show,
6394 .write = memory_oom_group_write,
6395 },
Johannes Weiner241994ed2015-02-11 15:26:06 -08006396 { } /* terminate */
6397};
6398
Tejun Heo073219e2014-02-08 10:36:58 -05006399struct cgroup_subsys memory_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08006400 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08006401 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08006402 .css_offline = mem_cgroup_css_offline,
Vladimir Davydov6df38682015-12-29 14:54:10 -08006403 .css_released = mem_cgroup_css_released,
Tejun Heo92fb9742012-11-19 08:13:38 -08006404 .css_free = mem_cgroup_css_free,
Tejun Heo1ced9532014-07-08 18:02:57 -04006405 .css_reset = mem_cgroup_css_reset,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006406 .can_attach = mem_cgroup_can_attach,
6407 .cancel_attach = mem_cgroup_cancel_attach,
Tejun Heo264a0ae2016-04-21 19:09:02 -04006408 .post_attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07006409 .bind = mem_cgroup_bind,
Johannes Weiner241994ed2015-02-11 15:26:06 -08006410 .dfl_cftypes = memory_files,
6411 .legacy_cftypes = mem_cgroup_legacy_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006412 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006413};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006414
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006415/*
6416 * This function calculates an individual cgroup's effective
6417 * protection which is derived from its own memory.min/low, its
6418 * parent's and siblings' settings, as well as the actual memory
6419 * distribution in the tree.
6420 *
6421 * The following rules apply to the effective protection values:
6422 *
6423 * 1. At the first level of reclaim, effective protection is equal to
6424 * the declared protection in memory.min and memory.low.
6425 *
6426 * 2. To enable safe delegation of the protection configuration, at
6427 * subsequent levels the effective protection is capped to the
6428 * parent's effective protection.
6429 *
6430 * 3. To make complex and dynamic subtrees easier to configure, the
6431 * user is allowed to overcommit the declared protection at a given
6432 * level. If that is the case, the parent's effective protection is
6433 * distributed to the children in proportion to how much protection
6434 * they have declared and how much of it they are utilizing.
6435 *
6436 * This makes distribution proportional, but also work-conserving:
6437 * if one cgroup claims much more protection than it uses memory,
6438 * the unused remainder is available to its siblings.
6439 *
6440 * 4. Conversely, when the declared protection is undercommitted at a
6441 * given level, the distribution of the larger parental protection
6442 * budget is NOT proportional. A cgroup's protection from a sibling
6443 * is capped to its own memory.min/low setting.
6444 *
Johannes Weiner8a931f82020-04-01 21:07:07 -07006445 * 5. However, to allow protecting recursive subtrees from each other
6446 * without having to declare each individual cgroup's fixed share
6447 * of the ancestor's claim to protection, any unutilized -
6448 * "floating" - protection from up the tree is distributed in
6449 * proportion to each cgroup's *usage*. This makes the protection
6450 * neutral wrt sibling cgroups and lets them compete freely over
6451 * the shared parental protection budget, but it protects the
6452 * subtree as a whole from neighboring subtrees.
6453 *
6454 * Note that 4. and 5. are not in conflict: 4. is about protecting
6455 * against immediate siblings whereas 5. is about protecting against
6456 * neighboring subtrees.
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006457 */
6458static unsigned long effective_protection(unsigned long usage,
Johannes Weiner8a931f82020-04-01 21:07:07 -07006459 unsigned long parent_usage,
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006460 unsigned long setting,
6461 unsigned long parent_effective,
6462 unsigned long siblings_protected)
6463{
6464 unsigned long protected;
Johannes Weiner8a931f82020-04-01 21:07:07 -07006465 unsigned long ep;
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006466
6467 protected = min(usage, setting);
6468 /*
6469 * If all cgroups at this level combined claim and use more
6470 * protection then what the parent affords them, distribute
6471 * shares in proportion to utilization.
6472 *
6473 * We are using actual utilization rather than the statically
6474 * claimed protection in order to be work-conserving: claimed
6475 * but unused protection is available to siblings that would
6476 * otherwise get a smaller chunk than what they claimed.
6477 */
6478 if (siblings_protected > parent_effective)
6479 return protected * parent_effective / siblings_protected;
6480
6481 /*
6482 * Ok, utilized protection of all children is within what the
6483 * parent affords them, so we know whatever this child claims
6484 * and utilizes is effectively protected.
6485 *
6486 * If there is unprotected usage beyond this value, reclaim
6487 * will apply pressure in proportion to that amount.
6488 *
6489 * If there is unutilized protection, the cgroup will be fully
6490 * shielded from reclaim, but we do return a smaller value for
6491 * protection than what the group could enjoy in theory. This
6492 * is okay. With the overcommit distribution above, effective
6493 * protection is always dependent on how memory is actually
6494 * consumed among the siblings anyway.
6495 */
Johannes Weiner8a931f82020-04-01 21:07:07 -07006496 ep = protected;
6497
6498 /*
6499 * If the children aren't claiming (all of) the protection
6500 * afforded to them by the parent, distribute the remainder in
6501 * proportion to the (unprotected) memory of each cgroup. That
6502 * way, cgroups that aren't explicitly prioritized wrt each
6503 * other compete freely over the allowance, but they are
6504 * collectively protected from neighboring trees.
6505 *
6506 * We're using unprotected memory for the weight so that if
6507 * some cgroups DO claim explicit protection, we don't protect
6508 * the same bytes twice.
Johannes Weinercd324ed2020-06-25 20:30:16 -07006509 *
6510 * Check both usage and parent_usage against the respective
6511 * protected values. One should imply the other, but they
6512 * aren't read atomically - make sure the division is sane.
Johannes Weiner8a931f82020-04-01 21:07:07 -07006513 */
6514 if (!(cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT))
6515 return ep;
Johannes Weinercd324ed2020-06-25 20:30:16 -07006516 if (parent_effective > siblings_protected &&
6517 parent_usage > siblings_protected &&
6518 usage > protected) {
Johannes Weiner8a931f82020-04-01 21:07:07 -07006519 unsigned long unclaimed;
6520
6521 unclaimed = parent_effective - siblings_protected;
6522 unclaimed *= usage - protected;
6523 unclaimed /= parent_usage - siblings_protected;
6524
6525 ep += unclaimed;
6526 }
6527
6528 return ep;
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006529}
6530
Johannes Weiner241994ed2015-02-11 15:26:06 -08006531/**
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006532 * mem_cgroup_protected - check if memory consumption is in the normal range
Sean Christopherson34c81052017-07-10 15:48:05 -07006533 * @root: the top ancestor of the sub-tree being checked
Johannes Weiner241994ed2015-02-11 15:26:06 -08006534 * @memcg: the memory cgroup to check
6535 *
Roman Gushchin23067152018-06-07 17:06:22 -07006536 * WARNING: This function is not stateless! It can only be used as part
6537 * of a top-down tree iteration, not for isolated queries.
Sean Christopherson34c81052017-07-10 15:48:05 -07006538 *
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006539 * Returns one of the following:
6540 * MEMCG_PROT_NONE: cgroup memory is not protected
6541 * MEMCG_PROT_LOW: cgroup memory is protected as long there is
6542 * an unprotected supply of reclaimable memory from other cgroups.
6543 * MEMCG_PROT_MIN: cgroup memory is protected
Johannes Weiner241994ed2015-02-11 15:26:06 -08006544 */
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006545enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root,
6546 struct mem_cgroup *memcg)
Johannes Weiner241994ed2015-02-11 15:26:06 -08006547{
Johannes Weiner8a931f82020-04-01 21:07:07 -07006548 unsigned long usage, parent_usage;
Roman Gushchin23067152018-06-07 17:06:22 -07006549 struct mem_cgroup *parent;
6550
Johannes Weiner241994ed2015-02-11 15:26:06 -08006551 if (mem_cgroup_disabled())
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006552 return MEMCG_PROT_NONE;
Johannes Weiner241994ed2015-02-11 15:26:06 -08006553
Sean Christopherson34c81052017-07-10 15:48:05 -07006554 if (!root)
6555 root = root_mem_cgroup;
6556 if (memcg == root)
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006557 return MEMCG_PROT_NONE;
Johannes Weiner241994ed2015-02-11 15:26:06 -08006558
Roman Gushchin23067152018-06-07 17:06:22 -07006559 usage = page_counter_read(&memcg->memory);
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006560 if (!usage)
6561 return MEMCG_PROT_NONE;
Sean Christopherson34c81052017-07-10 15:48:05 -07006562
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006563 parent = parent_mem_cgroup(memcg);
Roman Gushchindf2a4192018-06-14 15:26:17 -07006564 /* No parent means a non-hierarchical mode on v1 memcg */
6565 if (!parent)
6566 return MEMCG_PROT_NONE;
6567
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006568 if (parent == root) {
Chris Downc3d53202020-04-01 21:07:27 -07006569 memcg->memory.emin = READ_ONCE(memcg->memory.min);
Chris Down03960e32020-06-25 20:30:22 -07006570 memcg->memory.elow = READ_ONCE(memcg->memory.low);
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006571 goto out;
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006572 }
6573
Johannes Weiner8a931f82020-04-01 21:07:07 -07006574 parent_usage = page_counter_read(&parent->memory);
6575
Chris Downb3a78222020-04-01 21:07:33 -07006576 WRITE_ONCE(memcg->memory.emin, effective_protection(usage, parent_usage,
Chris Downc3d53202020-04-01 21:07:27 -07006577 READ_ONCE(memcg->memory.min),
6578 READ_ONCE(parent->memory.emin),
Chris Downb3a78222020-04-01 21:07:33 -07006579 atomic_long_read(&parent->memory.children_min_usage)));
Roman Gushchin23067152018-06-07 17:06:22 -07006580
Chris Downb3a78222020-04-01 21:07:33 -07006581 WRITE_ONCE(memcg->memory.elow, effective_protection(usage, parent_usage,
Chris Down03960e32020-06-25 20:30:22 -07006582 READ_ONCE(memcg->memory.low),
6583 READ_ONCE(parent->memory.elow),
Chris Downb3a78222020-04-01 21:07:33 -07006584 atomic_long_read(&parent->memory.children_low_usage)));
Roman Gushchin23067152018-06-07 17:06:22 -07006585
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006586out:
6587 if (usage <= memcg->memory.emin)
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006588 return MEMCG_PROT_MIN;
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006589 else if (usage <= memcg->memory.elow)
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006590 return MEMCG_PROT_LOW;
6591 else
6592 return MEMCG_PROT_NONE;
Johannes Weiner241994ed2015-02-11 15:26:06 -08006593}
6594
Johannes Weiner00501b52014-08-08 14:19:20 -07006595/**
Johannes Weinerf0e45fb2020-06-03 16:02:07 -07006596 * mem_cgroup_charge - charge a newly allocated page to a cgroup
Johannes Weiner00501b52014-08-08 14:19:20 -07006597 * @page: page to charge
6598 * @mm: mm context of the victim
6599 * @gfp_mask: reclaim mode
Johannes Weiner00501b52014-08-08 14:19:20 -07006600 *
6601 * Try to charge @page to the memcg that @mm belongs to, reclaiming
6602 * pages according to @gfp_mask if necessary.
6603 *
Johannes Weinerf0e45fb2020-06-03 16:02:07 -07006604 * Returns 0 on success. Otherwise, an error code is returned.
Johannes Weiner00501b52014-08-08 14:19:20 -07006605 */
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07006606int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
Johannes Weiner00501b52014-08-08 14:19:20 -07006607{
Johannes Weiner3fba69a2020-06-03 16:01:31 -07006608 unsigned int nr_pages = hpage_nr_pages(page);
Johannes Weiner00501b52014-08-08 14:19:20 -07006609 struct mem_cgroup *memcg = NULL;
Johannes Weiner00501b52014-08-08 14:19:20 -07006610 int ret = 0;
6611
6612 if (mem_cgroup_disabled())
6613 goto out;
6614
6615 if (PageSwapCache(page)) {
Johannes Weiner2d1c4982020-06-03 16:02:14 -07006616 swp_entry_t ent = { .val = page_private(page), };
6617 unsigned short id;
6618
Johannes Weiner00501b52014-08-08 14:19:20 -07006619 /*
6620 * Every swap fault against a single page tries to charge the
6621 * page, bail as early as possible. shmem_unuse() encounters
Johannes Weinereccb52e2020-06-03 16:02:11 -07006622 * already charged pages, too. page->mem_cgroup is protected
6623 * by the page lock, which serializes swap cache removal, which
Johannes Weiner00501b52014-08-08 14:19:20 -07006624 * in turn serializes uncharging.
6625 */
Vladimir Davydove993d902015-09-09 15:35:35 -07006626 VM_BUG_ON_PAGE(!PageLocked(page), page);
Huang Yingabe28952017-09-06 16:22:41 -07006627 if (compound_head(page)->mem_cgroup)
Johannes Weiner00501b52014-08-08 14:19:20 -07006628 goto out;
Vladimir Davydove993d902015-09-09 15:35:35 -07006629
Johannes Weiner2d1c4982020-06-03 16:02:14 -07006630 id = lookup_swap_cgroup_id(ent);
6631 rcu_read_lock();
6632 memcg = mem_cgroup_from_id(id);
6633 if (memcg && !css_tryget_online(&memcg->css))
6634 memcg = NULL;
6635 rcu_read_unlock();
Johannes Weiner00501b52014-08-08 14:19:20 -07006636 }
6637
Johannes Weiner00501b52014-08-08 14:19:20 -07006638 if (!memcg)
6639 memcg = get_mem_cgroup_from_mm(mm);
6640
6641 ret = try_charge(memcg, gfp_mask, nr_pages);
Johannes Weinerf0e45fb2020-06-03 16:02:07 -07006642 if (ret)
6643 goto out_put;
Johannes Weiner00501b52014-08-08 14:19:20 -07006644
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07006645 css_get(&memcg->css);
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07006646 commit_charge(page, memcg);
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006647
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006648 local_irq_disable();
Johannes Weiner3fba69a2020-06-03 16:01:31 -07006649 mem_cgroup_charge_statistics(memcg, page, nr_pages);
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006650 memcg_check_events(memcg, page);
6651 local_irq_enable();
Johannes Weiner00501b52014-08-08 14:19:20 -07006652
Johannes Weiner2d1c4982020-06-03 16:02:14 -07006653 if (PageSwapCache(page)) {
Johannes Weiner00501b52014-08-08 14:19:20 -07006654 swp_entry_t entry = { .val = page_private(page) };
6655 /*
6656 * The swap entry might not get freed for a long time,
6657 * let's not wait for it. The page already received a
6658 * memory+swap charge, drop the swap entry duplicate.
6659 */
Huang Ying38d8b4e2017-07-06 15:37:18 -07006660 mem_cgroup_uncharge_swap(entry, nr_pages);
Johannes Weiner00501b52014-08-08 14:19:20 -07006661 }
Johannes Weiner00501b52014-08-08 14:19:20 -07006662
Johannes Weinerf0e45fb2020-06-03 16:02:07 -07006663out_put:
6664 css_put(&memcg->css);
6665out:
6666 return ret;
Johannes Weiner3fea5a42020-06-03 16:01:41 -07006667}
6668
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006669struct uncharge_gather {
6670 struct mem_cgroup *memcg;
Johannes Weiner9f762db2020-06-03 16:01:44 -07006671 unsigned long nr_pages;
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006672 unsigned long pgpgout;
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006673 unsigned long nr_kmem;
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006674 struct page *dummy_page;
6675};
6676
6677static inline void uncharge_gather_clear(struct uncharge_gather *ug)
Johannes Weiner747db952014-08-08 14:19:24 -07006678{
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006679 memset(ug, 0, sizeof(*ug));
6680}
6681
6682static void uncharge_batch(const struct uncharge_gather *ug)
6683{
Johannes Weiner747db952014-08-08 14:19:24 -07006684 unsigned long flags;
6685
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006686 if (!mem_cgroup_is_root(ug->memcg)) {
Johannes Weiner9f762db2020-06-03 16:01:44 -07006687 page_counter_uncharge(&ug->memcg->memory, ug->nr_pages);
Johannes Weiner7941d212016-01-14 15:21:23 -08006688 if (do_memsw_account())
Johannes Weiner9f762db2020-06-03 16:01:44 -07006689 page_counter_uncharge(&ug->memcg->memsw, ug->nr_pages);
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006690 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && ug->nr_kmem)
6691 page_counter_uncharge(&ug->memcg->kmem, ug->nr_kmem);
6692 memcg_oom_recover(ug->memcg);
Johannes Weinerce00a962014-09-05 08:43:57 -04006693 }
Johannes Weiner747db952014-08-08 14:19:24 -07006694
6695 local_irq_save(flags);
Johannes Weinerc9019e92018-01-31 16:16:37 -08006696 __count_memcg_events(ug->memcg, PGPGOUT, ug->pgpgout);
Johannes Weiner9f762db2020-06-03 16:01:44 -07006697 __this_cpu_add(ug->memcg->vmstats_percpu->nr_page_events, ug->nr_pages);
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006698 memcg_check_events(ug->memcg, ug->dummy_page);
Johannes Weiner747db952014-08-08 14:19:24 -07006699 local_irq_restore(flags);
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006700}
6701
6702static void uncharge_page(struct page *page, struct uncharge_gather *ug)
6703{
Johannes Weiner9f762db2020-06-03 16:01:44 -07006704 unsigned long nr_pages;
6705
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006706 VM_BUG_ON_PAGE(PageLRU(page), page);
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006707
6708 if (!page->mem_cgroup)
6709 return;
6710
6711 /*
6712 * Nobody should be changing or seriously looking at
6713 * page->mem_cgroup at this point, we have fully
6714 * exclusive access to the page.
6715 */
6716
6717 if (ug->memcg != page->mem_cgroup) {
6718 if (ug->memcg) {
6719 uncharge_batch(ug);
6720 uncharge_gather_clear(ug);
6721 }
6722 ug->memcg = page->mem_cgroup;
6723 }
6724
Johannes Weiner9f762db2020-06-03 16:01:44 -07006725 nr_pages = compound_nr(page);
6726 ug->nr_pages += nr_pages;
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006727
Johannes Weiner9f762db2020-06-03 16:01:44 -07006728 if (!PageKmemcg(page)) {
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006729 ug->pgpgout++;
6730 } else {
Johannes Weiner9f762db2020-06-03 16:01:44 -07006731 ug->nr_kmem += nr_pages;
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006732 __ClearPageKmemcg(page);
6733 }
6734
6735 ug->dummy_page = page;
6736 page->mem_cgroup = NULL;
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07006737 css_put(&ug->memcg->css);
Johannes Weiner747db952014-08-08 14:19:24 -07006738}
6739
6740static void uncharge_list(struct list_head *page_list)
6741{
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006742 struct uncharge_gather ug;
Johannes Weiner747db952014-08-08 14:19:24 -07006743 struct list_head *next;
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006744
6745 uncharge_gather_clear(&ug);
Johannes Weiner747db952014-08-08 14:19:24 -07006746
Johannes Weiner8b592652016-03-17 14:20:31 -07006747 /*
6748 * Note that the list can be a single page->lru; hence the
6749 * do-while loop instead of a simple list_for_each_entry().
6750 */
Johannes Weiner747db952014-08-08 14:19:24 -07006751 next = page_list->next;
6752 do {
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006753 struct page *page;
6754
Johannes Weiner747db952014-08-08 14:19:24 -07006755 page = list_entry(next, struct page, lru);
6756 next = page->lru.next;
6757
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006758 uncharge_page(page, &ug);
Johannes Weiner747db952014-08-08 14:19:24 -07006759 } while (next != page_list);
6760
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006761 if (ug.memcg)
6762 uncharge_batch(&ug);
Johannes Weiner747db952014-08-08 14:19:24 -07006763}
6764
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006765/**
6766 * mem_cgroup_uncharge - uncharge a page
6767 * @page: page to uncharge
6768 *
Johannes Weinerf0e45fb2020-06-03 16:02:07 -07006769 * Uncharge a page previously charged with mem_cgroup_charge().
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006770 */
6771void mem_cgroup_uncharge(struct page *page)
6772{
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006773 struct uncharge_gather ug;
6774
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006775 if (mem_cgroup_disabled())
6776 return;
6777
Johannes Weiner747db952014-08-08 14:19:24 -07006778 /* Don't touch page->lru of any random page, pre-check: */
Johannes Weiner1306a852014-12-10 15:44:52 -08006779 if (!page->mem_cgroup)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006780 return;
6781
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006782 uncharge_gather_clear(&ug);
6783 uncharge_page(page, &ug);
6784 uncharge_batch(&ug);
Johannes Weiner747db952014-08-08 14:19:24 -07006785}
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006786
Johannes Weiner747db952014-08-08 14:19:24 -07006787/**
6788 * mem_cgroup_uncharge_list - uncharge a list of page
6789 * @page_list: list of pages to uncharge
6790 *
6791 * Uncharge a list of pages previously charged with
Johannes Weinerf0e45fb2020-06-03 16:02:07 -07006792 * mem_cgroup_charge().
Johannes Weiner747db952014-08-08 14:19:24 -07006793 */
6794void mem_cgroup_uncharge_list(struct list_head *page_list)
6795{
6796 if (mem_cgroup_disabled())
6797 return;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006798
Johannes Weiner747db952014-08-08 14:19:24 -07006799 if (!list_empty(page_list))
6800 uncharge_list(page_list);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006801}
6802
6803/**
Johannes Weiner6a93ca82016-03-15 14:57:19 -07006804 * mem_cgroup_migrate - charge a page's replacement
6805 * @oldpage: currently circulating page
6806 * @newpage: replacement page
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006807 *
Johannes Weiner6a93ca82016-03-15 14:57:19 -07006808 * Charge @newpage as a replacement page for @oldpage. @oldpage will
6809 * be uncharged upon free.
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006810 *
6811 * Both pages must be locked, @newpage->mapping must be set up.
6812 */
Johannes Weiner6a93ca82016-03-15 14:57:19 -07006813void mem_cgroup_migrate(struct page *oldpage, struct page *newpage)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006814{
Johannes Weiner29833312014-12-10 15:44:02 -08006815 struct mem_cgroup *memcg;
Johannes Weiner44b7a8d2016-01-20 15:03:16 -08006816 unsigned int nr_pages;
Tejun Heod93c4132016-06-24 14:49:54 -07006817 unsigned long flags;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006818
6819 VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage);
6820 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006821 VM_BUG_ON_PAGE(PageAnon(oldpage) != PageAnon(newpage), newpage);
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006822 VM_BUG_ON_PAGE(PageTransHuge(oldpage) != PageTransHuge(newpage),
6823 newpage);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006824
6825 if (mem_cgroup_disabled())
6826 return;
6827
6828 /* Page cache replacement: new page already charged? */
Johannes Weiner1306a852014-12-10 15:44:52 -08006829 if (newpage->mem_cgroup)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006830 return;
6831
Hugh Dickins45637ba2015-11-05 18:49:40 -08006832 /* Swapcache readahead pages can get replaced before being charged */
Johannes Weiner1306a852014-12-10 15:44:52 -08006833 memcg = oldpage->mem_cgroup;
Johannes Weiner29833312014-12-10 15:44:02 -08006834 if (!memcg)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006835 return;
6836
Johannes Weiner44b7a8d2016-01-20 15:03:16 -08006837 /* Force-charge the new page. The old one will be freed soon */
Kaitao Cheng928552702020-01-30 22:13:42 -08006838 nr_pages = hpage_nr_pages(newpage);
Johannes Weiner44b7a8d2016-01-20 15:03:16 -08006839
6840 page_counter_charge(&memcg->memory, nr_pages);
6841 if (do_memsw_account())
6842 page_counter_charge(&memcg->memsw, nr_pages);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006843
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07006844 css_get(&memcg->css);
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07006845 commit_charge(newpage, memcg);
Johannes Weiner44b7a8d2016-01-20 15:03:16 -08006846
Tejun Heod93c4132016-06-24 14:49:54 -07006847 local_irq_save(flags);
Johannes Weiner3fba69a2020-06-03 16:01:31 -07006848 mem_cgroup_charge_statistics(memcg, newpage, nr_pages);
Johannes Weiner44b7a8d2016-01-20 15:03:16 -08006849 memcg_check_events(memcg, newpage);
Tejun Heod93c4132016-06-24 14:49:54 -07006850 local_irq_restore(flags);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006851}
6852
Johannes Weineref129472016-01-14 15:21:34 -08006853DEFINE_STATIC_KEY_FALSE(memcg_sockets_enabled_key);
Johannes Weiner11092082016-01-14 15:21:26 -08006854EXPORT_SYMBOL(memcg_sockets_enabled_key);
6855
Johannes Weiner2d758072016-10-07 17:00:58 -07006856void mem_cgroup_sk_alloc(struct sock *sk)
Johannes Weiner11092082016-01-14 15:21:26 -08006857{
6858 struct mem_cgroup *memcg;
6859
Johannes Weiner2d758072016-10-07 17:00:58 -07006860 if (!mem_cgroup_sockets_enabled)
6861 return;
6862
Shakeel Butte876ecc2020-03-09 22:16:05 -07006863 /* Do not associate the sock with unrelated interrupted task's memcg. */
6864 if (in_interrupt())
6865 return;
6866
Johannes Weiner11092082016-01-14 15:21:26 -08006867 rcu_read_lock();
6868 memcg = mem_cgroup_from_task(current);
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006869 if (memcg == root_mem_cgroup)
6870 goto out;
Johannes Weiner0db15292016-01-20 15:02:50 -08006871 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !memcg->tcpmem_active)
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006872 goto out;
Shakeel Butt8965aa22020-04-01 21:07:10 -07006873 if (css_tryget(&memcg->css))
Johannes Weiner11092082016-01-14 15:21:26 -08006874 sk->sk_memcg = memcg;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006875out:
Johannes Weiner11092082016-01-14 15:21:26 -08006876 rcu_read_unlock();
6877}
Johannes Weiner11092082016-01-14 15:21:26 -08006878
Johannes Weiner2d758072016-10-07 17:00:58 -07006879void mem_cgroup_sk_free(struct sock *sk)
Johannes Weiner11092082016-01-14 15:21:26 -08006880{
Johannes Weiner2d758072016-10-07 17:00:58 -07006881 if (sk->sk_memcg)
6882 css_put(&sk->sk_memcg->css);
Johannes Weiner11092082016-01-14 15:21:26 -08006883}
6884
6885/**
6886 * mem_cgroup_charge_skmem - charge socket memory
6887 * @memcg: memcg to charge
6888 * @nr_pages: number of pages to charge
6889 *
6890 * Charges @nr_pages to @memcg. Returns %true if the charge fit within
6891 * @memcg's configured limit, %false if the charge had to be forced.
6892 */
6893bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
6894{
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006895 gfp_t gfp_mask = GFP_KERNEL;
Johannes Weiner11092082016-01-14 15:21:26 -08006896
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006897 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
Johannes Weiner0db15292016-01-20 15:02:50 -08006898 struct page_counter *fail;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006899
Johannes Weiner0db15292016-01-20 15:02:50 -08006900 if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) {
6901 memcg->tcpmem_pressure = 0;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006902 return true;
6903 }
Johannes Weiner0db15292016-01-20 15:02:50 -08006904 page_counter_charge(&memcg->tcpmem, nr_pages);
6905 memcg->tcpmem_pressure = 1;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006906 return false;
Johannes Weiner11092082016-01-14 15:21:26 -08006907 }
Johannes Weinerd886f4e2016-01-20 15:02:47 -08006908
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006909 /* Don't block in the packet receive path */
6910 if (in_softirq())
6911 gfp_mask = GFP_NOWAIT;
6912
Johannes Weinerc9019e92018-01-31 16:16:37 -08006913 mod_memcg_state(memcg, MEMCG_SOCK, nr_pages);
Johannes Weinerb2807f02016-01-20 15:03:22 -08006914
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006915 if (try_charge(memcg, gfp_mask, nr_pages) == 0)
6916 return true;
6917
6918 try_charge(memcg, gfp_mask|__GFP_NOFAIL, nr_pages);
Johannes Weiner11092082016-01-14 15:21:26 -08006919 return false;
6920}
6921
6922/**
6923 * mem_cgroup_uncharge_skmem - uncharge socket memory
Mike Rapoportb7701a52018-02-06 15:42:13 -08006924 * @memcg: memcg to uncharge
6925 * @nr_pages: number of pages to uncharge
Johannes Weiner11092082016-01-14 15:21:26 -08006926 */
6927void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
6928{
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006929 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
Johannes Weiner0db15292016-01-20 15:02:50 -08006930 page_counter_uncharge(&memcg->tcpmem, nr_pages);
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006931 return;
6932 }
Johannes Weinerd886f4e2016-01-20 15:02:47 -08006933
Johannes Weinerc9019e92018-01-31 16:16:37 -08006934 mod_memcg_state(memcg, MEMCG_SOCK, -nr_pages);
Johannes Weinerb2807f02016-01-20 15:03:22 -08006935
Roman Gushchin475d0482017-09-08 16:13:09 -07006936 refill_stock(memcg, nr_pages);
Johannes Weiner11092082016-01-14 15:21:26 -08006937}
6938
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006939static int __init cgroup_memory(char *s)
6940{
6941 char *token;
6942
6943 while ((token = strsep(&s, ",")) != NULL) {
6944 if (!*token)
6945 continue;
6946 if (!strcmp(token, "nosocket"))
6947 cgroup_memory_nosocket = true;
Vladimir Davydov04823c82016-01-20 15:02:38 -08006948 if (!strcmp(token, "nokmem"))
6949 cgroup_memory_nokmem = true;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006950 }
6951 return 0;
6952}
6953__setup("cgroup.memory=", cgroup_memory);
Johannes Weiner11092082016-01-14 15:21:26 -08006954
Michal Hocko2d110852013-02-22 16:34:43 -08006955/*
Michal Hocko10813122013-02-22 16:35:41 -08006956 * subsys_initcall() for memory controller.
6957 *
Sebastian Andrzej Siewior308167f2016-11-03 15:49:59 +01006958 * Some parts like memcg_hotplug_cpu_dead() have to be initialized from this
6959 * context because of lock dependencies (cgroup_lock -> cpu hotplug) but
6960 * basically everything that doesn't depend on a specific mem_cgroup structure
6961 * should be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08006962 */
6963static int __init mem_cgroup_init(void)
6964{
Johannes Weiner95a045f2015-02-11 15:26:33 -08006965 int cpu, node;
6966
Sebastian Andrzej Siewior308167f2016-11-03 15:49:59 +01006967 cpuhp_setup_state_nocalls(CPUHP_MM_MEMCQ_DEAD, "mm/memctrl:dead", NULL,
6968 memcg_hotplug_cpu_dead);
Johannes Weiner95a045f2015-02-11 15:26:33 -08006969
6970 for_each_possible_cpu(cpu)
6971 INIT_WORK(&per_cpu_ptr(&memcg_stock, cpu)->work,
6972 drain_local_stock);
6973
6974 for_each_node(node) {
6975 struct mem_cgroup_tree_per_node *rtpn;
Johannes Weiner95a045f2015-02-11 15:26:33 -08006976
6977 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL,
6978 node_online(node) ? node : NUMA_NO_NODE);
6979
Mel Gormanef8f2322016-07-28 15:46:05 -07006980 rtpn->rb_root = RB_ROOT;
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -07006981 rtpn->rb_rightmost = NULL;
Mel Gormanef8f2322016-07-28 15:46:05 -07006982 spin_lock_init(&rtpn->lock);
Johannes Weiner95a045f2015-02-11 15:26:33 -08006983 soft_limit_tree.rb_tree_per_node[node] = rtpn;
6984 }
6985
Michal Hocko2d110852013-02-22 16:34:43 -08006986 return 0;
6987}
6988subsys_initcall(mem_cgroup_init);
Johannes Weiner21afa382015-02-11 15:26:36 -08006989
6990#ifdef CONFIG_MEMCG_SWAP
Arnd Bergmann358c07f2016-08-25 15:17:08 -07006991static struct mem_cgroup *mem_cgroup_id_get_online(struct mem_cgroup *memcg)
6992{
Kirill Tkhai1c2d4792018-10-26 15:09:28 -07006993 while (!refcount_inc_not_zero(&memcg->id.ref)) {
Arnd Bergmann358c07f2016-08-25 15:17:08 -07006994 /*
6995 * The root cgroup cannot be destroyed, so it's refcount must
6996 * always be >= 1.
6997 */
6998 if (WARN_ON_ONCE(memcg == root_mem_cgroup)) {
6999 VM_BUG_ON(1);
7000 break;
7001 }
7002 memcg = parent_mem_cgroup(memcg);
7003 if (!memcg)
7004 memcg = root_mem_cgroup;
7005 }
7006 return memcg;
7007}
7008
Johannes Weiner21afa382015-02-11 15:26:36 -08007009/**
7010 * mem_cgroup_swapout - transfer a memsw charge to swap
7011 * @page: page whose memsw charge to transfer
7012 * @entry: swap entry to move the charge to
7013 *
7014 * Transfer the memsw charge of @page to @entry.
7015 */
7016void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
7017{
Vladimir Davydov1f47b612016-08-11 15:33:00 -07007018 struct mem_cgroup *memcg, *swap_memcg;
Huang Yingd6810d72017-09-06 16:22:45 -07007019 unsigned int nr_entries;
Johannes Weiner21afa382015-02-11 15:26:36 -08007020 unsigned short oldid;
7021
7022 VM_BUG_ON_PAGE(PageLRU(page), page);
7023 VM_BUG_ON_PAGE(page_count(page), page);
7024
Johannes Weiner2d1c4982020-06-03 16:02:14 -07007025 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
Johannes Weiner21afa382015-02-11 15:26:36 -08007026 return;
7027
7028 memcg = page->mem_cgroup;
7029
7030 /* Readahead page, never charged */
7031 if (!memcg)
7032 return;
7033
Vladimir Davydov1f47b612016-08-11 15:33:00 -07007034 /*
7035 * In case the memcg owning these pages has been offlined and doesn't
7036 * have an ID allocated to it anymore, charge the closest online
7037 * ancestor for the swap instead and transfer the memory+swap charge.
7038 */
7039 swap_memcg = mem_cgroup_id_get_online(memcg);
Huang Yingd6810d72017-09-06 16:22:45 -07007040 nr_entries = hpage_nr_pages(page);
7041 /* Get references for the tail pages, too */
7042 if (nr_entries > 1)
7043 mem_cgroup_id_get_many(swap_memcg, nr_entries - 1);
7044 oldid = swap_cgroup_record(entry, mem_cgroup_id(swap_memcg),
7045 nr_entries);
Johannes Weiner21afa382015-02-11 15:26:36 -08007046 VM_BUG_ON_PAGE(oldid, page);
Johannes Weinerc9019e92018-01-31 16:16:37 -08007047 mod_memcg_state(swap_memcg, MEMCG_SWAP, nr_entries);
Johannes Weiner21afa382015-02-11 15:26:36 -08007048
7049 page->mem_cgroup = NULL;
7050
7051 if (!mem_cgroup_is_root(memcg))
Huang Yingd6810d72017-09-06 16:22:45 -07007052 page_counter_uncharge(&memcg->memory, nr_entries);
Johannes Weiner21afa382015-02-11 15:26:36 -08007053
Johannes Weiner2d1c4982020-06-03 16:02:14 -07007054 if (!cgroup_memory_noswap && memcg != swap_memcg) {
Vladimir Davydov1f47b612016-08-11 15:33:00 -07007055 if (!mem_cgroup_is_root(swap_memcg))
Huang Yingd6810d72017-09-06 16:22:45 -07007056 page_counter_charge(&swap_memcg->memsw, nr_entries);
7057 page_counter_uncharge(&memcg->memsw, nr_entries);
Vladimir Davydov1f47b612016-08-11 15:33:00 -07007058 }
7059
Sebastian Andrzej Siewiorce9ce662015-09-04 15:47:50 -07007060 /*
7061 * Interrupts should be disabled here because the caller holds the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07007062 * i_pages lock which is taken with interrupts-off. It is
Sebastian Andrzej Siewiorce9ce662015-09-04 15:47:50 -07007063 * important here to have the interrupts disabled because it is the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07007064 * only synchronisation we have for updating the per-CPU variables.
Sebastian Andrzej Siewiorce9ce662015-09-04 15:47:50 -07007065 */
7066 VM_BUG_ON(!irqs_disabled());
Johannes Weiner3fba69a2020-06-03 16:01:31 -07007067 mem_cgroup_charge_statistics(memcg, page, -nr_entries);
Johannes Weiner21afa382015-02-11 15:26:36 -08007068 memcg_check_events(memcg, page);
Johannes Weiner73f576c2016-07-20 15:44:57 -07007069
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07007070 css_put(&memcg->css);
Johannes Weiner21afa382015-02-11 15:26:36 -08007071}
7072
Huang Ying38d8b4e2017-07-06 15:37:18 -07007073/**
7074 * mem_cgroup_try_charge_swap - try charging swap space for a page
Vladimir Davydov37e84352016-01-20 15:02:56 -08007075 * @page: page being added to swap
7076 * @entry: swap entry to charge
7077 *
Huang Ying38d8b4e2017-07-06 15:37:18 -07007078 * Try to charge @page's memcg for the swap space at @entry.
Vladimir Davydov37e84352016-01-20 15:02:56 -08007079 *
7080 * Returns 0 on success, -ENOMEM on failure.
7081 */
7082int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry)
7083{
Huang Ying38d8b4e2017-07-06 15:37:18 -07007084 unsigned int nr_pages = hpage_nr_pages(page);
Vladimir Davydov37e84352016-01-20 15:02:56 -08007085 struct page_counter *counter;
Huang Ying38d8b4e2017-07-06 15:37:18 -07007086 struct mem_cgroup *memcg;
Vladimir Davydov37e84352016-01-20 15:02:56 -08007087 unsigned short oldid;
7088
Johannes Weiner2d1c4982020-06-03 16:02:14 -07007089 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
Vladimir Davydov37e84352016-01-20 15:02:56 -08007090 return 0;
7091
7092 memcg = page->mem_cgroup;
7093
7094 /* Readahead page, never charged */
7095 if (!memcg)
7096 return 0;
7097
Tejun Heof3a53a32018-06-07 17:05:35 -07007098 if (!entry.val) {
7099 memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
Tejun Heobb98f2c2018-06-07 17:05:31 -07007100 return 0;
Tejun Heof3a53a32018-06-07 17:05:35 -07007101 }
Tejun Heobb98f2c2018-06-07 17:05:31 -07007102
Vladimir Davydov1f47b612016-08-11 15:33:00 -07007103 memcg = mem_cgroup_id_get_online(memcg);
Vladimir Davydov37e84352016-01-20 15:02:56 -08007104
Johannes Weiner2d1c4982020-06-03 16:02:14 -07007105 if (!cgroup_memory_noswap && !mem_cgroup_is_root(memcg) &&
Huang Ying38d8b4e2017-07-06 15:37:18 -07007106 !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) {
Tejun Heof3a53a32018-06-07 17:05:35 -07007107 memcg_memory_event(memcg, MEMCG_SWAP_MAX);
7108 memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
Vladimir Davydov1f47b612016-08-11 15:33:00 -07007109 mem_cgroup_id_put(memcg);
7110 return -ENOMEM;
7111 }
7112
Huang Ying38d8b4e2017-07-06 15:37:18 -07007113 /* Get references for the tail pages, too */
7114 if (nr_pages > 1)
7115 mem_cgroup_id_get_many(memcg, nr_pages - 1);
7116 oldid = swap_cgroup_record(entry, mem_cgroup_id(memcg), nr_pages);
Vladimir Davydov37e84352016-01-20 15:02:56 -08007117 VM_BUG_ON_PAGE(oldid, page);
Johannes Weinerc9019e92018-01-31 16:16:37 -08007118 mod_memcg_state(memcg, MEMCG_SWAP, nr_pages);
Vladimir Davydov37e84352016-01-20 15:02:56 -08007119
Vladimir Davydov37e84352016-01-20 15:02:56 -08007120 return 0;
7121}
7122
Johannes Weiner21afa382015-02-11 15:26:36 -08007123/**
Huang Ying38d8b4e2017-07-06 15:37:18 -07007124 * mem_cgroup_uncharge_swap - uncharge swap space
Johannes Weiner21afa382015-02-11 15:26:36 -08007125 * @entry: swap entry to uncharge
Huang Ying38d8b4e2017-07-06 15:37:18 -07007126 * @nr_pages: the amount of swap space to uncharge
Johannes Weiner21afa382015-02-11 15:26:36 -08007127 */
Huang Ying38d8b4e2017-07-06 15:37:18 -07007128void mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_pages)
Johannes Weiner21afa382015-02-11 15:26:36 -08007129{
7130 struct mem_cgroup *memcg;
7131 unsigned short id;
7132
Huang Ying38d8b4e2017-07-06 15:37:18 -07007133 id = swap_cgroup_record(entry, 0, nr_pages);
Johannes Weiner21afa382015-02-11 15:26:36 -08007134 rcu_read_lock();
Vladimir Davydovadbe4272015-04-15 16:13:00 -07007135 memcg = mem_cgroup_from_id(id);
Johannes Weiner21afa382015-02-11 15:26:36 -08007136 if (memcg) {
Johannes Weiner2d1c4982020-06-03 16:02:14 -07007137 if (!cgroup_memory_noswap && !mem_cgroup_is_root(memcg)) {
Vladimir Davydov37e84352016-01-20 15:02:56 -08007138 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
Huang Ying38d8b4e2017-07-06 15:37:18 -07007139 page_counter_uncharge(&memcg->swap, nr_pages);
Vladimir Davydov37e84352016-01-20 15:02:56 -08007140 else
Huang Ying38d8b4e2017-07-06 15:37:18 -07007141 page_counter_uncharge(&memcg->memsw, nr_pages);
Vladimir Davydov37e84352016-01-20 15:02:56 -08007142 }
Johannes Weinerc9019e92018-01-31 16:16:37 -08007143 mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages);
Huang Ying38d8b4e2017-07-06 15:37:18 -07007144 mem_cgroup_id_put_many(memcg, nr_pages);
Johannes Weiner21afa382015-02-11 15:26:36 -08007145 }
7146 rcu_read_unlock();
7147}
7148
Vladimir Davydovd8b38432016-01-20 15:03:07 -08007149long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)
7150{
7151 long nr_swap_pages = get_nr_swap_pages();
7152
Johannes Weinereccb52e2020-06-03 16:02:11 -07007153 if (cgroup_memory_noswap || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
Vladimir Davydovd8b38432016-01-20 15:03:07 -08007154 return nr_swap_pages;
7155 for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg))
7156 nr_swap_pages = min_t(long, nr_swap_pages,
Roman Gushchinbbec2e12018-06-07 17:06:18 -07007157 READ_ONCE(memcg->swap.max) -
Vladimir Davydovd8b38432016-01-20 15:03:07 -08007158 page_counter_read(&memcg->swap));
7159 return nr_swap_pages;
7160}
7161
Vladimir Davydov5ccc5ab2016-01-20 15:03:10 -08007162bool mem_cgroup_swap_full(struct page *page)
7163{
7164 struct mem_cgroup *memcg;
7165
7166 VM_BUG_ON_PAGE(!PageLocked(page), page);
7167
7168 if (vm_swap_full())
7169 return true;
Johannes Weinereccb52e2020-06-03 16:02:11 -07007170 if (cgroup_memory_noswap || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
Vladimir Davydov5ccc5ab2016-01-20 15:03:10 -08007171 return false;
7172
7173 memcg = page->mem_cgroup;
7174 if (!memcg)
7175 return false;
7176
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07007177 for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) {
7178 unsigned long usage = page_counter_read(&memcg->swap);
7179
7180 if (usage * 2 >= READ_ONCE(memcg->swap.high) ||
7181 usage * 2 >= READ_ONCE(memcg->swap.max))
Vladimir Davydov5ccc5ab2016-01-20 15:03:10 -08007182 return true;
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07007183 }
Vladimir Davydov5ccc5ab2016-01-20 15:03:10 -08007184
7185 return false;
7186}
7187
Johannes Weinereccb52e2020-06-03 16:02:11 -07007188static int __init setup_swap_account(char *s)
Johannes Weiner21afa382015-02-11 15:26:36 -08007189{
7190 if (!strcmp(s, "1"))
Johannes Weinereccb52e2020-06-03 16:02:11 -07007191 cgroup_memory_noswap = 0;
Johannes Weiner21afa382015-02-11 15:26:36 -08007192 else if (!strcmp(s, "0"))
Johannes Weinereccb52e2020-06-03 16:02:11 -07007193 cgroup_memory_noswap = 1;
Johannes Weiner21afa382015-02-11 15:26:36 -08007194 return 1;
7195}
Johannes Weinereccb52e2020-06-03 16:02:11 -07007196__setup("swapaccount=", setup_swap_account);
Johannes Weiner21afa382015-02-11 15:26:36 -08007197
Vladimir Davydov37e84352016-01-20 15:02:56 -08007198static u64 swap_current_read(struct cgroup_subsys_state *css,
7199 struct cftype *cft)
7200{
7201 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
7202
7203 return (u64)page_counter_read(&memcg->swap) * PAGE_SIZE;
7204}
7205
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07007206static int swap_high_show(struct seq_file *m, void *v)
7207{
7208 return seq_puts_memcg_tunable(m,
7209 READ_ONCE(mem_cgroup_from_seq(m)->swap.high));
7210}
7211
7212static ssize_t swap_high_write(struct kernfs_open_file *of,
7213 char *buf, size_t nbytes, loff_t off)
7214{
7215 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
7216 unsigned long high;
7217 int err;
7218
7219 buf = strstrip(buf);
7220 err = page_counter_memparse(buf, "max", &high);
7221 if (err)
7222 return err;
7223
7224 page_counter_set_high(&memcg->swap, high);
7225
7226 return nbytes;
7227}
7228
Vladimir Davydov37e84352016-01-20 15:02:56 -08007229static int swap_max_show(struct seq_file *m, void *v)
7230{
Chris Down677dc972019-03-05 15:45:55 -08007231 return seq_puts_memcg_tunable(m,
7232 READ_ONCE(mem_cgroup_from_seq(m)->swap.max));
Vladimir Davydov37e84352016-01-20 15:02:56 -08007233}
7234
7235static ssize_t swap_max_write(struct kernfs_open_file *of,
7236 char *buf, size_t nbytes, loff_t off)
7237{
7238 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
7239 unsigned long max;
7240 int err;
7241
7242 buf = strstrip(buf);
7243 err = page_counter_memparse(buf, "max", &max);
7244 if (err)
7245 return err;
7246
Tejun Heobe091022018-06-07 17:09:21 -07007247 xchg(&memcg->swap.max, max);
Vladimir Davydov37e84352016-01-20 15:02:56 -08007248
7249 return nbytes;
7250}
7251
Tejun Heof3a53a32018-06-07 17:05:35 -07007252static int swap_events_show(struct seq_file *m, void *v)
7253{
Chris Downaa9694b2019-03-05 15:45:52 -08007254 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Tejun Heof3a53a32018-06-07 17:05:35 -07007255
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07007256 seq_printf(m, "high %lu\n",
7257 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_HIGH]));
Tejun Heof3a53a32018-06-07 17:05:35 -07007258 seq_printf(m, "max %lu\n",
7259 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_MAX]));
7260 seq_printf(m, "fail %lu\n",
7261 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_FAIL]));
7262
7263 return 0;
7264}
7265
Vladimir Davydov37e84352016-01-20 15:02:56 -08007266static struct cftype swap_files[] = {
7267 {
7268 .name = "swap.current",
7269 .flags = CFTYPE_NOT_ON_ROOT,
7270 .read_u64 = swap_current_read,
7271 },
7272 {
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07007273 .name = "swap.high",
7274 .flags = CFTYPE_NOT_ON_ROOT,
7275 .seq_show = swap_high_show,
7276 .write = swap_high_write,
7277 },
7278 {
Vladimir Davydov37e84352016-01-20 15:02:56 -08007279 .name = "swap.max",
7280 .flags = CFTYPE_NOT_ON_ROOT,
7281 .seq_show = swap_max_show,
7282 .write = swap_max_write,
7283 },
Tejun Heof3a53a32018-06-07 17:05:35 -07007284 {
7285 .name = "swap.events",
7286 .flags = CFTYPE_NOT_ON_ROOT,
7287 .file_offset = offsetof(struct mem_cgroup, swap_events_file),
7288 .seq_show = swap_events_show,
7289 },
Vladimir Davydov37e84352016-01-20 15:02:56 -08007290 { } /* terminate */
7291};
7292
Johannes Weinereccb52e2020-06-03 16:02:11 -07007293static struct cftype memsw_files[] = {
Johannes Weiner21afa382015-02-11 15:26:36 -08007294 {
7295 .name = "memsw.usage_in_bytes",
7296 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
7297 .read_u64 = mem_cgroup_read_u64,
7298 },
7299 {
7300 .name = "memsw.max_usage_in_bytes",
7301 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
7302 .write = mem_cgroup_reset,
7303 .read_u64 = mem_cgroup_read_u64,
7304 },
7305 {
7306 .name = "memsw.limit_in_bytes",
7307 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
7308 .write = mem_cgroup_write,
7309 .read_u64 = mem_cgroup_read_u64,
7310 },
7311 {
7312 .name = "memsw.failcnt",
7313 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
7314 .write = mem_cgroup_reset,
7315 .read_u64 = mem_cgroup_read_u64,
7316 },
7317 { }, /* terminate */
7318};
7319
Bhupesh Sharma82ff1652020-07-23 21:15:21 -07007320/*
7321 * If mem_cgroup_swap_init() is implemented as a subsys_initcall()
7322 * instead of a core_initcall(), this could mean cgroup_memory_noswap still
7323 * remains set to false even when memcg is disabled via "cgroup_disable=memory"
7324 * boot parameter. This may result in premature OOPS inside
7325 * mem_cgroup_get_nr_swap_pages() function in corner cases.
7326 */
Johannes Weiner21afa382015-02-11 15:26:36 -08007327static int __init mem_cgroup_swap_init(void)
7328{
Johannes Weiner2d1c4982020-06-03 16:02:14 -07007329 /* No memory control -> no swap control */
7330 if (mem_cgroup_disabled())
7331 cgroup_memory_noswap = true;
7332
7333 if (cgroup_memory_noswap)
Johannes Weinereccb52e2020-06-03 16:02:11 -07007334 return 0;
7335
7336 WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, swap_files));
7337 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, memsw_files));
7338
Johannes Weiner21afa382015-02-11 15:26:36 -08007339 return 0;
7340}
Bhupesh Sharma82ff1652020-07-23 21:15:21 -07007341core_initcall(mem_cgroup_swap_init);
Johannes Weiner21afa382015-02-11 15:26:36 -08007342
7343#endif /* CONFIG_MEMCG_SWAP */