blob: a3e96336676943d30833c9e84ecdc0677126e738 [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",
Shakeel Butt991e7672020-08-06 23:21:37 -07001488 (u64)memcg_page_state(memcg, NR_KERNEL_STACK_KB) *
Johannes Weinerc8713d02019-07-11 20:55:59 -07001489 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 Gushchin10befea2020-08-06 23:21:27 -07002803int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
2804 gfp_t gfp)
2805{
2806 unsigned int objects = objs_per_slab_page(s, page);
2807 void *vec;
2808
2809 vec = kcalloc_node(objects, sizeof(struct obj_cgroup *), gfp,
2810 page_to_nid(page));
2811 if (!vec)
2812 return -ENOMEM;
2813
2814 if (cmpxchg(&page->obj_cgroups, NULL,
2815 (struct obj_cgroup **) ((unsigned long)vec | 0x1UL)))
2816 kfree(vec);
2817 else
2818 kmemleak_not_leak(vec);
2819
2820 return 0;
2821}
2822
Roman Gushchin8380ce42020-03-28 19:17:25 -07002823/*
2824 * Returns a pointer to the memory cgroup to which the kernel object is charged.
2825 *
2826 * The caller must ensure the memcg lifetime, e.g. by taking rcu_read_lock(),
2827 * cgroup_mutex, etc.
2828 */
2829struct mem_cgroup *mem_cgroup_from_obj(void *p)
2830{
2831 struct page *page;
2832
2833 if (mem_cgroup_disabled())
2834 return NULL;
2835
2836 page = virt_to_head_page(p);
2837
2838 /*
Roman Gushchin98556092020-08-06 23:21:10 -07002839 * Slab objects are accounted individually, not per-page.
2840 * Memcg membership data for each individual object is saved in
2841 * the page->obj_cgroups.
Roman Gushchin8380ce42020-03-28 19:17:25 -07002842 */
Roman Gushchin98556092020-08-06 23:21:10 -07002843 if (page_has_obj_cgroups(page)) {
2844 struct obj_cgroup *objcg;
2845 unsigned int off;
2846
2847 off = obj_to_index(page->slab_cache, page, p);
2848 objcg = page_obj_cgroups(page)[off];
Roman Gushchin10befea2020-08-06 23:21:27 -07002849 if (objcg)
2850 return obj_cgroup_memcg(objcg);
2851
2852 return NULL;
Roman Gushchin98556092020-08-06 23:21:10 -07002853 }
Roman Gushchin8380ce42020-03-28 19:17:25 -07002854
2855 /* All other pages use page->mem_cgroup */
2856 return page->mem_cgroup;
2857}
2858
Roman Gushchinbf4f0592020-08-06 23:20:49 -07002859__always_inline struct obj_cgroup *get_obj_cgroup_from_current(void)
2860{
2861 struct obj_cgroup *objcg = NULL;
2862 struct mem_cgroup *memcg;
2863
2864 if (unlikely(!current->mm && !current->active_memcg))
2865 return NULL;
2866
2867 rcu_read_lock();
2868 if (unlikely(current->active_memcg))
2869 memcg = rcu_dereference(current->active_memcg);
2870 else
2871 memcg = mem_cgroup_from_task(current);
2872
2873 for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) {
2874 objcg = rcu_dereference(memcg->objcg);
2875 if (objcg && obj_cgroup_tryget(objcg))
2876 break;
2877 }
2878 rcu_read_unlock();
2879
2880 return objcg;
2881}
2882
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002883static int memcg_alloc_cache_id(void)
Glauber Costa55007d82012-12-18 14:22:38 -08002884{
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002885 int id, size;
2886 int err;
Glauber Costa55007d82012-12-18 14:22:38 -08002887
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002888 id = ida_simple_get(&memcg_cache_ida,
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002889 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2890 if (id < 0)
2891 return id;
2892
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002893 if (id < memcg_nr_cache_ids)
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002894 return id;
2895
2896 /*
2897 * There's no space for the new id in memcg_caches arrays,
2898 * so we have to grow them.
2899 */
Vladimir Davydov05257a12015-02-12 14:59:01 -08002900 down_write(&memcg_cache_ids_sem);
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002901
2902 size = 2 * (id + 1);
Glauber Costa55007d82012-12-18 14:22:38 -08002903 if (size < MEMCG_CACHES_MIN_SIZE)
2904 size = MEMCG_CACHES_MIN_SIZE;
2905 else if (size > MEMCG_CACHES_MAX_SIZE)
2906 size = MEMCG_CACHES_MAX_SIZE;
2907
Roman Gushchin98556092020-08-06 23:21:10 -07002908 err = memcg_update_all_list_lrus(size);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -08002909 if (!err)
Vladimir Davydov05257a12015-02-12 14:59:01 -08002910 memcg_nr_cache_ids = size;
2911
2912 up_write(&memcg_cache_ids_sem);
2913
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002914 if (err) {
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002915 ida_simple_remove(&memcg_cache_ida, id);
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002916 return err;
2917 }
2918 return id;
2919}
2920
2921static void memcg_free_cache_id(int id)
2922{
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002923 ida_simple_remove(&memcg_cache_ida, id);
Glauber Costa55007d82012-12-18 14:22:38 -08002924}
2925
Vladimir Davydov45264772016-07-26 15:24:21 -07002926/**
Roman Gushchin4b13f642020-04-01 21:06:56 -07002927 * __memcg_kmem_charge: charge a number of kernel pages to a memcg
Roman Gushchin10eaec22020-04-01 21:06:39 -07002928 * @memcg: memory cgroup to charge
Vladimir Davydov45264772016-07-26 15:24:21 -07002929 * @gfp: reclaim mode
Roman Gushchin92d0510c2020-04-01 21:06:49 -07002930 * @nr_pages: number of pages to charge
Vladimir Davydov45264772016-07-26 15:24:21 -07002931 *
2932 * Returns 0 on success, an error code on failure.
2933 */
Roman Gushchin4b13f642020-04-01 21:06:56 -07002934int __memcg_kmem_charge(struct mem_cgroup *memcg, gfp_t gfp,
2935 unsigned int nr_pages)
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002936{
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002937 struct page_counter *counter;
Johannes Weiner6071ca52015-11-05 18:50:26 -08002938 int ret;
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002939
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002940 ret = try_charge(memcg, gfp, nr_pages);
Johannes Weiner52c29b02016-01-20 15:02:35 -08002941 if (ret)
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002942 return ret;
Johannes Weiner52c29b02016-01-20 15:02:35 -08002943
2944 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) &&
2945 !page_counter_try_charge(&memcg->kmem, nr_pages, &counter)) {
Michal Hockoe55d9d92019-09-25 16:45:53 -07002946
2947 /*
2948 * Enforce __GFP_NOFAIL allocation because callers are not
2949 * prepared to see failures and likely do not have any failure
2950 * handling code.
2951 */
2952 if (gfp & __GFP_NOFAIL) {
2953 page_counter_charge(&memcg->kmem, nr_pages);
2954 return 0;
2955 }
Johannes Weiner52c29b02016-01-20 15:02:35 -08002956 cancel_charge(memcg, nr_pages);
2957 return -ENOMEM;
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002958 }
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002959 return 0;
2960}
2961
Vladimir Davydov45264772016-07-26 15:24:21 -07002962/**
Roman Gushchin4b13f642020-04-01 21:06:56 -07002963 * __memcg_kmem_uncharge: uncharge a number of kernel pages from a memcg
2964 * @memcg: memcg to uncharge
2965 * @nr_pages: number of pages to uncharge
2966 */
2967void __memcg_kmem_uncharge(struct mem_cgroup *memcg, unsigned int nr_pages)
2968{
2969 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
2970 page_counter_uncharge(&memcg->kmem, nr_pages);
2971
2972 page_counter_uncharge(&memcg->memory, nr_pages);
2973 if (do_memsw_account())
2974 page_counter_uncharge(&memcg->memsw, nr_pages);
2975}
2976
2977/**
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07002978 * __memcg_kmem_charge_page: charge a kmem page to the current memory cgroup
Vladimir Davydov45264772016-07-26 15:24:21 -07002979 * @page: page to charge
2980 * @gfp: reclaim mode
2981 * @order: allocation order
2982 *
2983 * Returns 0 on success, an error code on failure.
2984 */
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07002985int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002986{
2987 struct mem_cgroup *memcg;
Vladimir Davydovfcff7d72016-03-17 14:17:29 -07002988 int ret = 0;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002989
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08002990 if (memcg_kmem_bypass())
Vladimir Davydov45264772016-07-26 15:24:21 -07002991 return 0;
2992
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07002993 memcg = get_mem_cgroup_from_current();
Vladimir Davydovc4159a72016-08-08 23:03:12 +03002994 if (!mem_cgroup_is_root(memcg)) {
Roman Gushchin4b13f642020-04-01 21:06:56 -07002995 ret = __memcg_kmem_charge(memcg, gfp, 1 << order);
Roman Gushchin4d96ba32019-07-11 20:56:31 -07002996 if (!ret) {
2997 page->mem_cgroup = memcg;
Vladimir Davydovc4159a72016-08-08 23:03:12 +03002998 __SetPageKmemcg(page);
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07002999 return 0;
Roman Gushchin4d96ba32019-07-11 20:56:31 -07003000 }
Vladimir Davydovc4159a72016-08-08 23:03:12 +03003001 }
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003002 css_put(&memcg->css);
Vladimir Davydovd05e83a2015-11-05 18:48:59 -08003003 return ret;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003004}
Roman Gushchin49a18ea2019-07-11 20:56:13 -07003005
3006/**
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07003007 * __memcg_kmem_uncharge_page: uncharge a kmem page
Vladimir Davydov45264772016-07-26 15:24:21 -07003008 * @page: page to uncharge
3009 * @order: allocation order
3010 */
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07003011void __memcg_kmem_uncharge_page(struct page *page, int order)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003012{
Johannes Weiner1306a852014-12-10 15:44:52 -08003013 struct mem_cgroup *memcg = page->mem_cgroup;
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08003014 unsigned int nr_pages = 1 << order;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003015
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003016 if (!memcg)
3017 return;
3018
Sasha Levin309381fea2014-01-23 15:52:54 -08003019 VM_BUG_ON_PAGE(mem_cgroup_is_root(memcg), page);
Roman Gushchin4b13f642020-04-01 21:06:56 -07003020 __memcg_kmem_uncharge(memcg, nr_pages);
Johannes Weiner1306a852014-12-10 15:44:52 -08003021 page->mem_cgroup = NULL;
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07003022 css_put(&memcg->css);
Vladimir Davydovc4159a72016-08-08 23:03:12 +03003023
3024 /* slab pages do not have PageKmemcg flag set */
3025 if (PageKmemcg(page))
3026 __ClearPageKmemcg(page);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -08003027}
Roman Gushchinbf4f0592020-08-06 23:20:49 -07003028
3029static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes)
3030{
3031 struct memcg_stock_pcp *stock;
3032 unsigned long flags;
3033 bool ret = false;
3034
3035 local_irq_save(flags);
3036
3037 stock = this_cpu_ptr(&memcg_stock);
3038 if (objcg == stock->cached_objcg && stock->nr_bytes >= nr_bytes) {
3039 stock->nr_bytes -= nr_bytes;
3040 ret = true;
3041 }
3042
3043 local_irq_restore(flags);
3044
3045 return ret;
3046}
3047
3048static void drain_obj_stock(struct memcg_stock_pcp *stock)
3049{
3050 struct obj_cgroup *old = stock->cached_objcg;
3051
3052 if (!old)
3053 return;
3054
3055 if (stock->nr_bytes) {
3056 unsigned int nr_pages = stock->nr_bytes >> PAGE_SHIFT;
3057 unsigned int nr_bytes = stock->nr_bytes & (PAGE_SIZE - 1);
3058
3059 if (nr_pages) {
3060 rcu_read_lock();
3061 __memcg_kmem_uncharge(obj_cgroup_memcg(old), nr_pages);
3062 rcu_read_unlock();
3063 }
3064
3065 /*
3066 * The leftover is flushed to the centralized per-memcg value.
3067 * On the next attempt to refill obj stock it will be moved
3068 * to a per-cpu stock (probably, on an other CPU), see
3069 * refill_obj_stock().
3070 *
3071 * How often it's flushed is a trade-off between the memory
3072 * limit enforcement accuracy and potential CPU contention,
3073 * so it might be changed in the future.
3074 */
3075 atomic_add(nr_bytes, &old->nr_charged_bytes);
3076 stock->nr_bytes = 0;
3077 }
3078
3079 obj_cgroup_put(old);
3080 stock->cached_objcg = NULL;
3081}
3082
3083static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
3084 struct mem_cgroup *root_memcg)
3085{
3086 struct mem_cgroup *memcg;
3087
3088 if (stock->cached_objcg) {
3089 memcg = obj_cgroup_memcg(stock->cached_objcg);
3090 if (memcg && mem_cgroup_is_descendant(memcg, root_memcg))
3091 return true;
3092 }
3093
3094 return false;
3095}
3096
3097static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes)
3098{
3099 struct memcg_stock_pcp *stock;
3100 unsigned long flags;
3101
3102 local_irq_save(flags);
3103
3104 stock = this_cpu_ptr(&memcg_stock);
3105 if (stock->cached_objcg != objcg) { /* reset if necessary */
3106 drain_obj_stock(stock);
3107 obj_cgroup_get(objcg);
3108 stock->cached_objcg = objcg;
3109 stock->nr_bytes = atomic_xchg(&objcg->nr_charged_bytes, 0);
3110 }
3111 stock->nr_bytes += nr_bytes;
3112
3113 if (stock->nr_bytes > PAGE_SIZE)
3114 drain_obj_stock(stock);
3115
3116 local_irq_restore(flags);
3117}
3118
3119int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size)
3120{
3121 struct mem_cgroup *memcg;
3122 unsigned int nr_pages, nr_bytes;
3123 int ret;
3124
3125 if (consume_obj_stock(objcg, size))
3126 return 0;
3127
3128 /*
3129 * In theory, memcg->nr_charged_bytes can have enough
3130 * pre-charged bytes to satisfy the allocation. However,
3131 * flushing memcg->nr_charged_bytes requires two atomic
3132 * operations, and memcg->nr_charged_bytes can't be big,
3133 * so it's better to ignore it and try grab some new pages.
3134 * memcg->nr_charged_bytes will be flushed in
3135 * refill_obj_stock(), called from this function or
3136 * independently later.
3137 */
3138 rcu_read_lock();
3139 memcg = obj_cgroup_memcg(objcg);
3140 css_get(&memcg->css);
3141 rcu_read_unlock();
3142
3143 nr_pages = size >> PAGE_SHIFT;
3144 nr_bytes = size & (PAGE_SIZE - 1);
3145
3146 if (nr_bytes)
3147 nr_pages += 1;
3148
3149 ret = __memcg_kmem_charge(memcg, gfp, nr_pages);
3150 if (!ret && nr_bytes)
3151 refill_obj_stock(objcg, PAGE_SIZE - nr_bytes);
3152
3153 css_put(&memcg->css);
3154 return ret;
3155}
3156
3157void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size)
3158{
3159 refill_obj_stock(objcg, size);
3160}
3161
Kirill Tkhai84c07d12018-08-17 15:47:25 -07003162#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003163
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003164#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3165
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003166/*
3167 * Because tail pages are not marked as "used", set it. We're under
Andrey Ryabininf4b7e272019-03-05 15:49:39 -08003168 * pgdat->lru_lock and migration entries setup in all page mappings.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003169 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003170void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003171{
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07003172 struct mem_cgroup *memcg = head->mem_cgroup;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003173 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003174
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003175 if (mem_cgroup_disabled())
3176 return;
David Rientjesb070e652013-05-07 16:18:09 -07003177
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07003178 for (i = 1; i < HPAGE_PMD_NR; i++) {
3179 css_get(&memcg->css);
3180 head[i].mem_cgroup = memcg;
3181 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003182}
Hugh Dickins12d27102012-01-12 17:19:52 -08003183#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003184
Andrew Mortonc255a452012-07-31 16:43:02 -07003185#ifdef CONFIG_MEMCG_SWAP
Daisuke Nishimura02491442010-03-10 15:22:17 -08003186/**
3187 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3188 * @entry: swap entry to be moved
3189 * @from: mem_cgroup which the entry is moved from
3190 * @to: mem_cgroup which the entry is moved to
3191 *
3192 * It succeeds only when the swap_cgroup's record for this entry is the same
3193 * as the mem_cgroup's id of @from.
3194 *
3195 * Returns 0 on success, -EINVAL on failure.
3196 *
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003197 * The caller must have charged to @to, IOW, called page_counter_charge() about
Daisuke Nishimura02491442010-03-10 15:22:17 -08003198 * both res and memsw, and called css_get().
3199 */
3200static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003201 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003202{
3203 unsigned short old_id, new_id;
3204
Li Zefan34c00c32013-09-23 16:56:01 +08003205 old_id = mem_cgroup_id(from);
3206 new_id = mem_cgroup_id(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003207
3208 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Johannes Weinerc9019e92018-01-31 16:16:37 -08003209 mod_memcg_state(from, MEMCG_SWAP, -1);
3210 mod_memcg_state(to, MEMCG_SWAP, 1);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003211 return 0;
3212 }
3213 return -EINVAL;
3214}
3215#else
3216static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003217 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003218{
3219 return -EINVAL;
3220}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003221#endif
3222
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003223static DEFINE_MUTEX(memcg_max_mutex);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003224
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003225static int mem_cgroup_resize_max(struct mem_cgroup *memcg,
3226 unsigned long max, bool memsw)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003227{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003228 bool enlarge = false;
Shakeel Buttbb4a7ea2018-06-07 17:07:27 -07003229 bool drained = false;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003230 int ret;
Yu Zhaoc054a782018-01-31 16:20:02 -08003231 bool limits_invariant;
3232 struct page_counter *counter = memsw ? &memcg->memsw : &memcg->memory;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003233
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003234 do {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003235 if (signal_pending(current)) {
3236 ret = -EINTR;
3237 break;
3238 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003239
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003240 mutex_lock(&memcg_max_mutex);
Yu Zhaoc054a782018-01-31 16:20:02 -08003241 /*
3242 * Make sure that the new limit (memsw or memory limit) doesn't
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003243 * break our basic invariant rule memory.max <= memsw.max.
Yu Zhaoc054a782018-01-31 16:20:02 -08003244 */
Chris Down15b42562020-04-01 21:07:20 -07003245 limits_invariant = memsw ? max >= READ_ONCE(memcg->memory.max) :
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003246 max <= memcg->memsw.max;
Yu Zhaoc054a782018-01-31 16:20:02 -08003247 if (!limits_invariant) {
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003248 mutex_unlock(&memcg_max_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003249 ret = -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003250 break;
3251 }
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003252 if (max > counter->max)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003253 enlarge = true;
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003254 ret = page_counter_set_max(counter, max);
3255 mutex_unlock(&memcg_max_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003256
3257 if (!ret)
3258 break;
3259
Shakeel Buttbb4a7ea2018-06-07 17:07:27 -07003260 if (!drained) {
3261 drain_all_stock(memcg);
3262 drained = true;
3263 continue;
3264 }
3265
Andrey Ryabinin1ab5c052018-01-31 16:20:37 -08003266 if (!try_to_free_mem_cgroup_pages(memcg, 1,
3267 GFP_KERNEL, !memsw)) {
3268 ret = -EBUSY;
3269 break;
3270 }
3271 } while (true);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003272
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003273 if (!ret && enlarge)
3274 memcg_oom_recover(memcg);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003275
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003276 return ret;
3277}
3278
Mel Gormanef8f2322016-07-28 15:46:05 -07003279unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
Andrew Morton0608f432013-09-24 15:27:41 -07003280 gfp_t gfp_mask,
3281 unsigned long *total_scanned)
3282{
3283 unsigned long nr_reclaimed = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07003284 struct mem_cgroup_per_node *mz, *next_mz = NULL;
Andrew Morton0608f432013-09-24 15:27:41 -07003285 unsigned long reclaimed;
3286 int loop = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07003287 struct mem_cgroup_tree_per_node *mctz;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003288 unsigned long excess;
Andrew Morton0608f432013-09-24 15:27:41 -07003289 unsigned long nr_scanned;
3290
3291 if (order > 0)
3292 return 0;
3293
Mel Gormanef8f2322016-07-28 15:46:05 -07003294 mctz = soft_limit_tree_node(pgdat->node_id);
Michal Hockod6507ff2016-08-02 14:02:37 -07003295
3296 /*
3297 * Do not even bother to check the largest node if the root
3298 * is empty. Do it lockless to prevent lock bouncing. Races
3299 * are acceptable as soft limit is best effort anyway.
3300 */
Laurent Dufourbfc72282017-03-09 16:17:06 -08003301 if (!mctz || RB_EMPTY_ROOT(&mctz->rb_root))
Michal Hockod6507ff2016-08-02 14:02:37 -07003302 return 0;
3303
Andrew Morton0608f432013-09-24 15:27:41 -07003304 /*
3305 * This loop can run a while, specially if mem_cgroup's continuously
3306 * keep exceeding their soft limit and putting the system under
3307 * pressure
3308 */
3309 do {
3310 if (next_mz)
3311 mz = next_mz;
3312 else
3313 mz = mem_cgroup_largest_soft_limit_node(mctz);
3314 if (!mz)
3315 break;
3316
3317 nr_scanned = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07003318 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, pgdat,
Andrew Morton0608f432013-09-24 15:27:41 -07003319 gfp_mask, &nr_scanned);
3320 nr_reclaimed += reclaimed;
3321 *total_scanned += nr_scanned;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003322 spin_lock_irq(&mctz->lock);
Vladimir Davydovbc2f2e72014-12-10 15:43:40 -08003323 __mem_cgroup_remove_exceeded(mz, mctz);
Andrew Morton0608f432013-09-24 15:27:41 -07003324
3325 /*
3326 * If we failed to reclaim anything from this memory cgroup
3327 * it is time to move on to the next cgroup
3328 */
3329 next_mz = NULL;
Vladimir Davydovbc2f2e72014-12-10 15:43:40 -08003330 if (!reclaimed)
3331 next_mz = __mem_cgroup_largest_soft_limit_node(mctz);
3332
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003333 excess = soft_limit_excess(mz->memcg);
Andrew Morton0608f432013-09-24 15:27:41 -07003334 /*
3335 * One school of thought says that we should not add
3336 * back the node to the tree if reclaim returns 0.
3337 * But our reclaim could return 0, simply because due
3338 * to priority we are exposing a smaller subset of
3339 * memory to reclaim from. Consider this as a longer
3340 * term TODO.
3341 */
3342 /* If excess == 0, no tree ops */
Johannes Weinercf2c8122014-06-06 14:38:21 -07003343 __mem_cgroup_insert_exceeded(mz, mctz, excess);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003344 spin_unlock_irq(&mctz->lock);
Andrew Morton0608f432013-09-24 15:27:41 -07003345 css_put(&mz->memcg->css);
3346 loop++;
3347 /*
3348 * Could not reclaim anything and there are no more
3349 * mem cgroups to try or we seem to be looping without
3350 * reclaiming anything.
3351 */
3352 if (!nr_reclaimed &&
3353 (next_mz == NULL ||
3354 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3355 break;
3356 } while (!nr_reclaimed);
3357 if (next_mz)
3358 css_put(&next_mz->memcg->css);
3359 return nr_reclaimed;
3360}
3361
Tejun Heoea280e72014-05-16 13:22:48 -04003362/*
3363 * Test whether @memcg has children, dead or alive. Note that this
3364 * function doesn't care whether @memcg has use_hierarchy enabled and
3365 * returns %true if there are child csses according to the cgroup
Ethon Paulb8f29352020-06-04 16:49:28 -07003366 * hierarchy. Testing use_hierarchy is the caller's responsibility.
Tejun Heoea280e72014-05-16 13:22:48 -04003367 */
Glauber Costab5f99b52013-02-22 16:34:53 -08003368static inline bool memcg_has_children(struct mem_cgroup *memcg)
3369{
Tejun Heoea280e72014-05-16 13:22:48 -04003370 bool ret;
3371
Tejun Heoea280e72014-05-16 13:22:48 -04003372 rcu_read_lock();
3373 ret = css_next_child(NULL, &memcg->css);
3374 rcu_read_unlock();
3375 return ret;
Glauber Costab5f99b52013-02-22 16:34:53 -08003376}
3377
3378/*
Greg Thelen51038172016-05-20 16:58:18 -07003379 * Reclaims as many pages from the given memcg as possible.
Michal Hockoc26251f2012-10-26 13:37:28 +02003380 *
3381 * Caller is responsible for holding css reference for memcg.
3382 */
3383static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
3384{
3385 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Michal Hockoc26251f2012-10-26 13:37:28 +02003386
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003387 /* we call try-to-free pages for make this cgroup empty */
3388 lru_add_drain_all();
Junaid Shahidd12c60f2018-06-07 17:07:31 -07003389
3390 drain_all_stock(memcg);
3391
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003392 /* try to free all pages in this cgroup */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003393 while (nr_retries && page_counter_read(&memcg->memory)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003394 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003395
Michal Hockoc26251f2012-10-26 13:37:28 +02003396 if (signal_pending(current))
3397 return -EINTR;
3398
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003399 progress = try_to_free_mem_cgroup_pages(memcg, 1,
3400 GFP_KERNEL, true);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003401 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003402 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003403 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003404 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003405 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003406
3407 }
Michal Hockoab5196c2012-10-26 13:37:32 +02003408
3409 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003410}
3411
Tejun Heo6770c642014-05-13 12:16:21 -04003412static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of,
3413 char *buf, size_t nbytes,
3414 loff_t off)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003415{
Tejun Heo6770c642014-05-13 12:16:21 -04003416 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Michal Hockoc26251f2012-10-26 13:37:28 +02003417
Michal Hockod8423012012-10-26 13:37:29 +02003418 if (mem_cgroup_is_root(memcg))
3419 return -EINVAL;
Tejun Heo6770c642014-05-13 12:16:21 -04003420 return mem_cgroup_force_empty(memcg) ?: nbytes;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003421}
3422
Tejun Heo182446d2013-08-08 20:11:24 -04003423static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
3424 struct cftype *cft)
Balbir Singh18f59ea2009-01-07 18:08:07 -08003425{
Tejun Heo182446d2013-08-08 20:11:24 -04003426 return mem_cgroup_from_css(css)->use_hierarchy;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003427}
3428
Tejun Heo182446d2013-08-08 20:11:24 -04003429static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
3430 struct cftype *cft, u64 val)
Balbir Singh18f59ea2009-01-07 18:08:07 -08003431{
3432 int retval = 0;
Tejun Heo182446d2013-08-08 20:11:24 -04003433 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04003434 struct mem_cgroup *parent_memcg = mem_cgroup_from_css(memcg->css.parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003435
Glauber Costa567fb432012-07-31 16:43:07 -07003436 if (memcg->use_hierarchy == val)
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08003437 return 0;
Glauber Costa567fb432012-07-31 16:43:07 -07003438
Balbir Singh18f59ea2009-01-07 18:08:07 -08003439 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003440 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003441 * in the child subtrees. If it is unset, then the change can
3442 * occur, provided the current cgroup has no children.
3443 *
3444 * For the root cgroup, parent_mem is NULL, we allow value to be
3445 * set if there are no children.
3446 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003447 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003448 (val == 1 || val == 0)) {
Tejun Heoea280e72014-05-16 13:22:48 -04003449 if (!memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003450 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003451 else
3452 retval = -EBUSY;
3453 } else
3454 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07003455
Balbir Singh18f59ea2009-01-07 18:08:07 -08003456 return retval;
3457}
3458
Andrew Morton6f646152015-11-06 16:28:58 -08003459static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Johannes Weinerce00a962014-09-05 08:43:57 -04003460{
Johannes Weiner42a30032019-05-14 15:47:12 -07003461 unsigned long val;
Johannes Weinerce00a962014-09-05 08:43:57 -04003462
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003463 if (mem_cgroup_is_root(memcg)) {
Johannes Weiner0d1c2072020-06-03 16:01:54 -07003464 val = memcg_page_state(memcg, NR_FILE_PAGES) +
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07003465 memcg_page_state(memcg, NR_ANON_MAPPED);
Johannes Weiner42a30032019-05-14 15:47:12 -07003466 if (swap)
3467 val += memcg_page_state(memcg, MEMCG_SWAP);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003468 } else {
Johannes Weinerce00a962014-09-05 08:43:57 -04003469 if (!swap)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003470 val = page_counter_read(&memcg->memory);
Johannes Weinerce00a962014-09-05 08:43:57 -04003471 else
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003472 val = page_counter_read(&memcg->memsw);
Johannes Weinerce00a962014-09-05 08:43:57 -04003473 }
Michal Hockoc12176d2015-11-05 18:50:29 -08003474 return val;
Johannes Weinerce00a962014-09-05 08:43:57 -04003475}
3476
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003477enum {
3478 RES_USAGE,
3479 RES_LIMIT,
3480 RES_MAX_USAGE,
3481 RES_FAILCNT,
3482 RES_SOFT_LIMIT,
3483};
Johannes Weinerce00a962014-09-05 08:43:57 -04003484
Tejun Heo791badb2013-12-05 12:28:02 -05003485static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
Johannes Weiner05b84302014-08-06 16:05:59 -07003486 struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003487{
Tejun Heo182446d2013-08-08 20:11:24 -04003488 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003489 struct page_counter *counter;
Tejun Heoaf36f902012-04-01 12:09:55 -07003490
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003491 switch (MEMFILE_TYPE(cft->private)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003492 case _MEM:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003493 counter = &memcg->memory;
Glauber Costa510fc4e2012-12-18 14:21:47 -08003494 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003495 case _MEMSWAP:
3496 counter = &memcg->memsw;
3497 break;
3498 case _KMEM:
3499 counter = &memcg->kmem;
3500 break;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003501 case _TCP:
Johannes Weiner0db15292016-01-20 15:02:50 -08003502 counter = &memcg->tcpmem;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003503 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003504 default:
3505 BUG();
3506 }
3507
3508 switch (MEMFILE_ATTR(cft->private)) {
3509 case RES_USAGE:
3510 if (counter == &memcg->memory)
Michal Hockoc12176d2015-11-05 18:50:29 -08003511 return (u64)mem_cgroup_usage(memcg, false) * PAGE_SIZE;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003512 if (counter == &memcg->memsw)
Michal Hockoc12176d2015-11-05 18:50:29 -08003513 return (u64)mem_cgroup_usage(memcg, true) * PAGE_SIZE;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003514 return (u64)page_counter_read(counter) * PAGE_SIZE;
3515 case RES_LIMIT:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003516 return (u64)counter->max * PAGE_SIZE;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003517 case RES_MAX_USAGE:
3518 return (u64)counter->watermark * PAGE_SIZE;
3519 case RES_FAILCNT:
3520 return counter->failcnt;
3521 case RES_SOFT_LIMIT:
3522 return (u64)memcg->soft_limit * PAGE_SIZE;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003523 default:
3524 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003525 }
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003526}
Glauber Costa510fc4e2012-12-18 14:21:47 -08003527
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08003528static void memcg_flush_percpu_vmstats(struct mem_cgroup *memcg)
Roman Gushchinc350a992019-08-24 17:54:47 -07003529{
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08003530 unsigned long stat[MEMCG_NR_STAT] = {0};
Roman Gushchinc350a992019-08-24 17:54:47 -07003531 struct mem_cgroup *mi;
3532 int node, cpu, i;
Roman Gushchinc350a992019-08-24 17:54:47 -07003533
3534 for_each_online_cpu(cpu)
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08003535 for (i = 0; i < MEMCG_NR_STAT; i++)
Shakeel Butt6c1c2802019-08-30 16:04:53 -07003536 stat[i] += per_cpu(memcg->vmstats_percpu->stat[i], cpu);
Roman Gushchinc350a992019-08-24 17:54:47 -07003537
3538 for (mi = memcg; mi; mi = parent_mem_cgroup(mi))
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08003539 for (i = 0; i < MEMCG_NR_STAT; i++)
Roman Gushchinc350a992019-08-24 17:54:47 -07003540 atomic_long_add(stat[i], &mi->vmstats[i]);
3541
3542 for_each_node(node) {
3543 struct mem_cgroup_per_node *pn = memcg->nodeinfo[node];
3544 struct mem_cgroup_per_node *pi;
3545
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08003546 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
Roman Gushchinc350a992019-08-24 17:54:47 -07003547 stat[i] = 0;
3548
3549 for_each_online_cpu(cpu)
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08003550 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
Shakeel Butt6c1c2802019-08-30 16:04:53 -07003551 stat[i] += per_cpu(
3552 pn->lruvec_stat_cpu->count[i], cpu);
Roman Gushchinc350a992019-08-24 17:54:47 -07003553
3554 for (pi = pn; pi; pi = parent_nodeinfo(pi, node))
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08003555 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
Roman Gushchinc350a992019-08-24 17:54:47 -07003556 atomic_long_add(stat[i], &pi->lruvec_stat[i]);
3557 }
3558}
3559
Roman Gushchinbb65f892019-08-24 17:54:50 -07003560static void memcg_flush_percpu_vmevents(struct mem_cgroup *memcg)
3561{
3562 unsigned long events[NR_VM_EVENT_ITEMS];
3563 struct mem_cgroup *mi;
3564 int cpu, i;
3565
3566 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
3567 events[i] = 0;
3568
3569 for_each_online_cpu(cpu)
3570 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
Shakeel Butt6c1c2802019-08-30 16:04:53 -07003571 events[i] += per_cpu(memcg->vmstats_percpu->events[i],
3572 cpu);
Roman Gushchinbb65f892019-08-24 17:54:50 -07003573
3574 for (mi = memcg; mi; mi = parent_mem_cgroup(mi))
3575 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
3576 atomic_long_add(events[i], &mi->vmevents[i]);
3577}
3578
Kirill Tkhai84c07d12018-08-17 15:47:25 -07003579#ifdef CONFIG_MEMCG_KMEM
Johannes Weiner567e9ab2016-01-20 15:02:24 -08003580static int memcg_online_kmem(struct mem_cgroup *memcg)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003581{
Roman Gushchinbf4f0592020-08-06 23:20:49 -07003582 struct obj_cgroup *objcg;
Vladimir Davydovd6441632014-01-23 15:53:09 -08003583 int memcg_id;
3584
Vladimir Davydovb313aee2016-03-17 14:18:27 -07003585 if (cgroup_memory_nokmem)
3586 return 0;
3587
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003588 BUG_ON(memcg->kmemcg_id >= 0);
Johannes Weiner567e9ab2016-01-20 15:02:24 -08003589 BUG_ON(memcg->kmem_state);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003590
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07003591 memcg_id = memcg_alloc_cache_id();
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08003592 if (memcg_id < 0)
3593 return memcg_id;
Vladimir Davydovd6441632014-01-23 15:53:09 -08003594
Roman Gushchinbf4f0592020-08-06 23:20:49 -07003595 objcg = obj_cgroup_alloc();
3596 if (!objcg) {
3597 memcg_free_cache_id(memcg_id);
3598 return -ENOMEM;
3599 }
3600 objcg->memcg = memcg;
3601 rcu_assign_pointer(memcg->objcg, objcg);
3602
Roman Gushchind648bcc2020-08-06 23:20:28 -07003603 static_branch_enable(&memcg_kmem_enabled_key);
3604
Vladimir Davydovd6441632014-01-23 15:53:09 -08003605 /*
Johannes Weiner567e9ab2016-01-20 15:02:24 -08003606 * A memory cgroup is considered kmem-online as soon as it gets
Vladimir Davydov900a38f2014-12-12 16:55:10 -08003607 * kmemcg_id. Setting the id after enabling static branching will
Vladimir Davydovd6441632014-01-23 15:53:09 -08003608 * guarantee no one starts accounting before all call sites are
3609 * patched.
3610 */
Vladimir Davydov900a38f2014-12-12 16:55:10 -08003611 memcg->kmemcg_id = memcg_id;
Johannes Weiner567e9ab2016-01-20 15:02:24 -08003612 memcg->kmem_state = KMEM_ONLINE;
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08003613
3614 return 0;
Vladimir Davydovd6441632014-01-23 15:53:09 -08003615}
3616
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003617static void memcg_offline_kmem(struct mem_cgroup *memcg)
3618{
3619 struct cgroup_subsys_state *css;
3620 struct mem_cgroup *parent, *child;
3621 int kmemcg_id;
3622
3623 if (memcg->kmem_state != KMEM_ONLINE)
3624 return;
Roman Gushchin98556092020-08-06 23:21:10 -07003625
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003626 memcg->kmem_state = KMEM_ALLOCATED;
3627
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003628 parent = parent_mem_cgroup(memcg);
3629 if (!parent)
3630 parent = root_mem_cgroup;
3631
Roman Gushchinbf4f0592020-08-06 23:20:49 -07003632 memcg_reparent_objcgs(memcg, parent);
Roman Gushchinfb2f2b02019-07-11 20:56:34 -07003633
3634 kmemcg_id = memcg->kmemcg_id;
3635 BUG_ON(kmemcg_id < 0);
3636
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003637 /*
3638 * Change kmemcg_id of this cgroup and all its descendants to the
3639 * parent's id, and then move all entries from this cgroup's list_lrus
3640 * to ones of the parent. After we have finished, all list_lrus
3641 * corresponding to this cgroup are guaranteed to remain empty. The
3642 * ordering is imposed by list_lru_node->lock taken by
3643 * memcg_drain_all_list_lrus().
3644 */
Tejun Heo3a06bb72016-06-03 14:55:44 -07003645 rcu_read_lock(); /* can be called from css_free w/o cgroup_mutex */
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003646 css_for_each_descendant_pre(css, &memcg->css) {
3647 child = mem_cgroup_from_css(css);
3648 BUG_ON(child->kmemcg_id != kmemcg_id);
3649 child->kmemcg_id = parent->kmemcg_id;
3650 if (!memcg->use_hierarchy)
3651 break;
3652 }
Tejun Heo3a06bb72016-06-03 14:55:44 -07003653 rcu_read_unlock();
3654
Kirill Tkhai9bec5c32018-08-17 15:47:58 -07003655 memcg_drain_all_list_lrus(kmemcg_id, parent);
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003656
3657 memcg_free_cache_id(kmemcg_id);
3658}
3659
3660static void memcg_free_kmem(struct mem_cgroup *memcg)
3661{
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08003662 /* css_alloc() failed, offlining didn't happen */
3663 if (unlikely(memcg->kmem_state == KMEM_ONLINE))
3664 memcg_offline_kmem(memcg);
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003665}
Vladimir Davydovd6441632014-01-23 15:53:09 -08003666#else
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08003667static int memcg_online_kmem(struct mem_cgroup *memcg)
Johannes Weiner127424c2016-01-20 15:02:32 -08003668{
3669 return 0;
3670}
3671static void memcg_offline_kmem(struct mem_cgroup *memcg)
3672{
3673}
3674static void memcg_free_kmem(struct mem_cgroup *memcg)
3675{
3676}
Kirill Tkhai84c07d12018-08-17 15:47:25 -07003677#endif /* CONFIG_MEMCG_KMEM */
Johannes Weiner127424c2016-01-20 15:02:32 -08003678
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003679static int memcg_update_kmem_max(struct mem_cgroup *memcg,
3680 unsigned long max)
Johannes Weiner127424c2016-01-20 15:02:32 -08003681{
Vladimir Davydovb313aee2016-03-17 14:18:27 -07003682 int ret;
Johannes Weiner127424c2016-01-20 15:02:32 -08003683
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003684 mutex_lock(&memcg_max_mutex);
3685 ret = page_counter_set_max(&memcg->kmem, max);
3686 mutex_unlock(&memcg_max_mutex);
Johannes Weiner127424c2016-01-20 15:02:32 -08003687 return ret;
3688}
Glauber Costa510fc4e2012-12-18 14:21:47 -08003689
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003690static int memcg_update_tcp_max(struct mem_cgroup *memcg, unsigned long max)
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003691{
3692 int ret;
3693
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003694 mutex_lock(&memcg_max_mutex);
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003695
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003696 ret = page_counter_set_max(&memcg->tcpmem, max);
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003697 if (ret)
3698 goto out;
3699
Johannes Weiner0db15292016-01-20 15:02:50 -08003700 if (!memcg->tcpmem_active) {
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003701 /*
3702 * The active flag needs to be written after the static_key
3703 * update. This is what guarantees that the socket activation
Johannes Weiner2d758072016-10-07 17:00:58 -07003704 * function is the last one to run. See mem_cgroup_sk_alloc()
3705 * for details, and note that we don't mark any socket as
3706 * belonging to this memcg until that flag is up.
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003707 *
3708 * We need to do this, because static_keys will span multiple
3709 * sites, but we can't control their order. If we mark a socket
3710 * as accounted, but the accounting functions are not patched in
3711 * yet, we'll lose accounting.
3712 *
Johannes Weiner2d758072016-10-07 17:00:58 -07003713 * We never race with the readers in mem_cgroup_sk_alloc(),
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003714 * because when this value change, the code to process it is not
3715 * patched in yet.
3716 */
3717 static_branch_inc(&memcg_sockets_enabled_key);
Johannes Weiner0db15292016-01-20 15:02:50 -08003718 memcg->tcpmem_active = true;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003719 }
3720out:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003721 mutex_unlock(&memcg_max_mutex);
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003722 return ret;
3723}
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003724
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003725/*
3726 * The user of this function is...
3727 * RES_LIMIT.
3728 */
Tejun Heo451af502014-05-13 12:16:21 -04003729static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
3730 char *buf, size_t nbytes, loff_t off)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003731{
Tejun Heo451af502014-05-13 12:16:21 -04003732 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003733 unsigned long nr_pages;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003734 int ret;
3735
Tejun Heo451af502014-05-13 12:16:21 -04003736 buf = strstrip(buf);
Johannes Weiner650c5e52015-02-11 15:26:03 -08003737 ret = page_counter_memparse(buf, "-1", &nr_pages);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003738 if (ret)
3739 return ret;
Tejun Heoaf36f902012-04-01 12:09:55 -07003740
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003741 switch (MEMFILE_ATTR(of_cft(of)->private)) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003742 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003743 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3744 ret = -EINVAL;
3745 break;
3746 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003747 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3748 case _MEM:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003749 ret = mem_cgroup_resize_max(memcg, nr_pages, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003750 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003751 case _MEMSWAP:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003752 ret = mem_cgroup_resize_max(memcg, nr_pages, true);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003753 break;
3754 case _KMEM:
Michal Hocko01581152019-09-23 15:37:22 -07003755 pr_warn_once("kmem.limit_in_bytes is deprecated and will be removed. "
3756 "Please report your usecase to linux-mm@kvack.org if you "
3757 "depend on this functionality.\n");
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003758 ret = memcg_update_kmem_max(memcg, nr_pages);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003759 break;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003760 case _TCP:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003761 ret = memcg_update_tcp_max(memcg, nr_pages);
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003762 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003763 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003764 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003765 case RES_SOFT_LIMIT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003766 memcg->soft_limit = nr_pages;
3767 ret = 0;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003768 break;
3769 }
Tejun Heo451af502014-05-13 12:16:21 -04003770 return ret ?: nbytes;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003771}
3772
Tejun Heo6770c642014-05-13 12:16:21 -04003773static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf,
3774 size_t nbytes, loff_t off)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003775{
Tejun Heo6770c642014-05-13 12:16:21 -04003776 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003777 struct page_counter *counter;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003778
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003779 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3780 case _MEM:
3781 counter = &memcg->memory;
3782 break;
3783 case _MEMSWAP:
3784 counter = &memcg->memsw;
3785 break;
3786 case _KMEM:
3787 counter = &memcg->kmem;
3788 break;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003789 case _TCP:
Johannes Weiner0db15292016-01-20 15:02:50 -08003790 counter = &memcg->tcpmem;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003791 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003792 default:
3793 BUG();
3794 }
Tejun Heoaf36f902012-04-01 12:09:55 -07003795
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003796 switch (MEMFILE_ATTR(of_cft(of)->private)) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003797 case RES_MAX_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003798 page_counter_reset_watermark(counter);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003799 break;
3800 case RES_FAILCNT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003801 counter->failcnt = 0;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003802 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003803 default:
3804 BUG();
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003805 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003806
Tejun Heo6770c642014-05-13 12:16:21 -04003807 return nbytes;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003808}
3809
Tejun Heo182446d2013-08-08 20:11:24 -04003810static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003811 struct cftype *cft)
3812{
Tejun Heo182446d2013-08-08 20:11:24 -04003813 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003814}
3815
Daisuke Nishimura02491442010-03-10 15:22:17 -08003816#ifdef CONFIG_MMU
Tejun Heo182446d2013-08-08 20:11:24 -04003817static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003818 struct cftype *cft, u64 val)
3819{
Tejun Heo182446d2013-08-08 20:11:24 -04003820 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003821
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08003822 if (val & ~MOVE_MASK)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003823 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003824
Glauber Costaee5e8472013-02-22 16:34:50 -08003825 /*
3826 * No kind of locking is needed in here, because ->can_attach() will
3827 * check this value once in the beginning of the process, and then carry
3828 * on with stale data. This means that changes to this value will only
3829 * affect task migrations starting after the change.
3830 */
3831 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003832 return 0;
3833}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003834#else
Tejun Heo182446d2013-08-08 20:11:24 -04003835static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura02491442010-03-10 15:22:17 -08003836 struct cftype *cft, u64 val)
3837{
3838 return -ENOSYS;
3839}
3840#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003841
Ying Han406eb0c2011-05-26 16:25:37 -07003842#ifdef CONFIG_NUMA
Johannes Weiner113b7df2019-05-13 17:18:11 -07003843
3844#define LRU_ALL_FILE (BIT(LRU_INACTIVE_FILE) | BIT(LRU_ACTIVE_FILE))
3845#define LRU_ALL_ANON (BIT(LRU_INACTIVE_ANON) | BIT(LRU_ACTIVE_ANON))
3846#define LRU_ALL ((1 << NR_LRU_LISTS) - 1)
3847
3848static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
Shakeel Buttdd8657b2020-06-03 15:56:24 -07003849 int nid, unsigned int lru_mask, bool tree)
Johannes Weiner113b7df2019-05-13 17:18:11 -07003850{
Johannes Weiner867e5e12019-11-30 17:55:34 -08003851 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid));
Johannes Weiner113b7df2019-05-13 17:18:11 -07003852 unsigned long nr = 0;
3853 enum lru_list lru;
3854
3855 VM_BUG_ON((unsigned)nid >= nr_node_ids);
3856
3857 for_each_lru(lru) {
3858 if (!(BIT(lru) & lru_mask))
3859 continue;
Shakeel Buttdd8657b2020-06-03 15:56:24 -07003860 if (tree)
3861 nr += lruvec_page_state(lruvec, NR_LRU_BASE + lru);
3862 else
3863 nr += lruvec_page_state_local(lruvec, NR_LRU_BASE + lru);
Johannes Weiner113b7df2019-05-13 17:18:11 -07003864 }
3865 return nr;
3866}
3867
3868static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
Shakeel Buttdd8657b2020-06-03 15:56:24 -07003869 unsigned int lru_mask,
3870 bool tree)
Johannes Weiner113b7df2019-05-13 17:18:11 -07003871{
3872 unsigned long nr = 0;
3873 enum lru_list lru;
3874
3875 for_each_lru(lru) {
3876 if (!(BIT(lru) & lru_mask))
3877 continue;
Shakeel Buttdd8657b2020-06-03 15:56:24 -07003878 if (tree)
3879 nr += memcg_page_state(memcg, NR_LRU_BASE + lru);
3880 else
3881 nr += memcg_page_state_local(memcg, NR_LRU_BASE + lru);
Johannes Weiner113b7df2019-05-13 17:18:11 -07003882 }
3883 return nr;
3884}
3885
Tejun Heo2da8ca82013-12-05 12:28:04 -05003886static int memcg_numa_stat_show(struct seq_file *m, void *v)
Ying Han406eb0c2011-05-26 16:25:37 -07003887{
Greg Thelen25485de2013-11-12 15:07:40 -08003888 struct numa_stat {
3889 const char *name;
3890 unsigned int lru_mask;
3891 };
3892
3893 static const struct numa_stat stats[] = {
3894 { "total", LRU_ALL },
3895 { "file", LRU_ALL_FILE },
3896 { "anon", LRU_ALL_ANON },
3897 { "unevictable", BIT(LRU_UNEVICTABLE) },
3898 };
3899 const struct numa_stat *stat;
Ying Han406eb0c2011-05-26 16:25:37 -07003900 int nid;
Chris Downaa9694b2019-03-05 15:45:52 -08003901 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Ying Han406eb0c2011-05-26 16:25:37 -07003902
Greg Thelen25485de2013-11-12 15:07:40 -08003903 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
Shakeel Buttdd8657b2020-06-03 15:56:24 -07003904 seq_printf(m, "%s=%lu", stat->name,
3905 mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
3906 false));
3907 for_each_node_state(nid, N_MEMORY)
3908 seq_printf(m, " N%d=%lu", nid,
3909 mem_cgroup_node_nr_lru_pages(memcg, nid,
3910 stat->lru_mask, false));
Greg Thelen25485de2013-11-12 15:07:40 -08003911 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07003912 }
Ying Han406eb0c2011-05-26 16:25:37 -07003913
Ying Han071aee12013-11-12 15:07:41 -08003914 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
Ying Han406eb0c2011-05-26 16:25:37 -07003915
Shakeel Buttdd8657b2020-06-03 15:56:24 -07003916 seq_printf(m, "hierarchical_%s=%lu", stat->name,
3917 mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
3918 true));
3919 for_each_node_state(nid, N_MEMORY)
3920 seq_printf(m, " N%d=%lu", nid,
3921 mem_cgroup_node_nr_lru_pages(memcg, nid,
3922 stat->lru_mask, true));
Ying Han071aee12013-11-12 15:07:41 -08003923 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07003924 }
Ying Han406eb0c2011-05-26 16:25:37 -07003925
Ying Han406eb0c2011-05-26 16:25:37 -07003926 return 0;
3927}
3928#endif /* CONFIG_NUMA */
3929
Johannes Weinerc8713d02019-07-11 20:55:59 -07003930static const unsigned int memcg1_stats[] = {
Johannes Weiner0d1c2072020-06-03 16:01:54 -07003931 NR_FILE_PAGES,
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07003932 NR_ANON_MAPPED,
Johannes Weiner468c3982020-06-03 16:02:01 -07003933#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3934 NR_ANON_THPS,
3935#endif
Johannes Weinerc8713d02019-07-11 20:55:59 -07003936 NR_SHMEM,
3937 NR_FILE_MAPPED,
3938 NR_FILE_DIRTY,
3939 NR_WRITEBACK,
3940 MEMCG_SWAP,
3941};
3942
3943static const char *const memcg1_stat_names[] = {
3944 "cache",
3945 "rss",
Johannes Weiner468c3982020-06-03 16:02:01 -07003946#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Johannes Weinerc8713d02019-07-11 20:55:59 -07003947 "rss_huge",
Johannes Weiner468c3982020-06-03 16:02:01 -07003948#endif
Johannes Weinerc8713d02019-07-11 20:55:59 -07003949 "shmem",
3950 "mapped_file",
3951 "dirty",
3952 "writeback",
3953 "swap",
3954};
3955
Johannes Weinerdf0e53d2017-05-03 14:55:10 -07003956/* Universal VM events cgroup1 shows, original sort order */
Greg Thelen8dd53fd2018-06-07 17:07:23 -07003957static const unsigned int memcg1_events[] = {
Johannes Weinerdf0e53d2017-05-03 14:55:10 -07003958 PGPGIN,
3959 PGPGOUT,
3960 PGFAULT,
3961 PGMAJFAULT,
3962};
3963
Tejun Heo2da8ca82013-12-05 12:28:04 -05003964static int memcg_stat_show(struct seq_file *m, void *v)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003965{
Chris Downaa9694b2019-03-05 15:45:52 -08003966 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003967 unsigned long memory, memsw;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003968 struct mem_cgroup *mi;
3969 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003970
Johannes Weiner71cd3112017-05-03 14:55:13 -07003971 BUILD_BUG_ON(ARRAY_SIZE(memcg1_stat_names) != ARRAY_SIZE(memcg1_stats));
Rickard Strandqvist70bc0682014-12-12 16:56:41 -08003972
Johannes Weiner71cd3112017-05-03 14:55:13 -07003973 for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
Johannes Weiner468c3982020-06-03 16:02:01 -07003974 unsigned long nr;
3975
Johannes Weiner71cd3112017-05-03 14:55:13 -07003976 if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account())
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003977 continue;
Johannes Weiner468c3982020-06-03 16:02:01 -07003978 nr = memcg_page_state_local(memcg, memcg1_stats[i]);
3979#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3980 if (memcg1_stats[i] == NR_ANON_THPS)
3981 nr *= HPAGE_PMD_NR;
3982#endif
3983 seq_printf(m, "%s %lu\n", memcg1_stat_names[i], nr * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003984 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003985
Johannes Weinerdf0e53d2017-05-03 14:55:10 -07003986 for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08003987 seq_printf(m, "%s %lu\n", vm_event_name(memcg1_events[i]),
Johannes Weiner205b20c2019-05-14 15:47:06 -07003988 memcg_events_local(memcg, memcg1_events[i]));
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003989
3990 for (i = 0; i < NR_LRU_LISTS; i++)
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08003991 seq_printf(m, "%s %lu\n", lru_list_name(i),
Johannes Weiner205b20c2019-05-14 15:47:06 -07003992 memcg_page_state_local(memcg, NR_LRU_BASE + i) *
Johannes Weiner21d89d12019-05-13 17:18:08 -07003993 PAGE_SIZE);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003994
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003995 /* Hierarchical information */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003996 memory = memsw = PAGE_COUNTER_MAX;
3997 for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) {
Chris Down15b42562020-04-01 21:07:20 -07003998 memory = min(memory, READ_ONCE(mi->memory.max));
3999 memsw = min(memsw, READ_ONCE(mi->memsw.max));
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004000 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004001 seq_printf(m, "hierarchical_memory_limit %llu\n",
4002 (u64)memory * PAGE_SIZE);
Johannes Weiner7941d212016-01-14 15:21:23 -08004003 if (do_memsw_account())
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004004 seq_printf(m, "hierarchical_memsw_limit %llu\n",
4005 (u64)memsw * PAGE_SIZE);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004006
Shakeel Butt8de7ecc62018-08-21 21:53:17 -07004007 for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
Johannes Weiner71cd3112017-05-03 14:55:13 -07004008 if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account())
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004009 continue;
Shakeel Butt8de7ecc62018-08-21 21:53:17 -07004010 seq_printf(m, "total_%s %llu\n", memcg1_stat_names[i],
Yafang Shaodd923992019-07-11 20:52:11 -07004011 (u64)memcg_page_state(memcg, memcg1_stats[i]) *
4012 PAGE_SIZE);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004013 }
4014
Shakeel Butt8de7ecc62018-08-21 21:53:17 -07004015 for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08004016 seq_printf(m, "total_%s %llu\n",
4017 vm_event_name(memcg1_events[i]),
Yafang Shaodd923992019-07-11 20:52:11 -07004018 (u64)memcg_events(memcg, memcg1_events[i]));
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004019
Shakeel Butt8de7ecc62018-08-21 21:53:17 -07004020 for (i = 0; i < NR_LRU_LISTS; i++)
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08004021 seq_printf(m, "total_%s %llu\n", lru_list_name(i),
Johannes Weiner42a30032019-05-14 15:47:12 -07004022 (u64)memcg_page_state(memcg, NR_LRU_BASE + i) *
4023 PAGE_SIZE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004024
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004025#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004026 {
Mel Gormanef8f2322016-07-28 15:46:05 -07004027 pg_data_t *pgdat;
4028 struct mem_cgroup_per_node *mz;
Johannes Weiner1431d4d2020-06-03 16:02:53 -07004029 unsigned long anon_cost = 0;
4030 unsigned long file_cost = 0;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004031
Mel Gormanef8f2322016-07-28 15:46:05 -07004032 for_each_online_pgdat(pgdat) {
4033 mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004034
Johannes Weiner1431d4d2020-06-03 16:02:53 -07004035 anon_cost += mz->lruvec.anon_cost;
4036 file_cost += mz->lruvec.file_cost;
Mel Gormanef8f2322016-07-28 15:46:05 -07004037 }
Johannes Weiner1431d4d2020-06-03 16:02:53 -07004038 seq_printf(m, "anon_cost %lu\n", anon_cost);
4039 seq_printf(m, "file_cost %lu\n", file_cost);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004040 }
4041#endif
4042
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004043 return 0;
4044}
4045
Tejun Heo182446d2013-08-08 20:11:24 -04004046static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
4047 struct cftype *cft)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004048{
Tejun Heo182446d2013-08-08 20:11:24 -04004049 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004050
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004051 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004052}
4053
Tejun Heo182446d2013-08-08 20:11:24 -04004054static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
4055 struct cftype *cft, u64 val)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004056{
Tejun Heo182446d2013-08-08 20:11:24 -04004057 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Li Zefan068b38c2009-01-15 13:51:26 -08004058
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07004059 if (val > 100)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004060 return -EINVAL;
4061
Linus Torvalds14208b02014-06-09 15:03:33 -07004062 if (css->parent)
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07004063 memcg->swappiness = val;
4064 else
4065 vm_swappiness = val;
Li Zefan068b38c2009-01-15 13:51:26 -08004066
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004067 return 0;
4068}
4069
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004070static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4071{
4072 struct mem_cgroup_threshold_ary *t;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004073 unsigned long usage;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004074 int i;
4075
4076 rcu_read_lock();
4077 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004078 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004079 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004080 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004081
4082 if (!t)
4083 goto unlock;
4084
Johannes Weinerce00a962014-09-05 08:43:57 -04004085 usage = mem_cgroup_usage(memcg, swap);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004086
4087 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07004088 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004089 * If it's not true, a threshold was crossed after last
4090 * call of __mem_cgroup_threshold().
4091 */
Phil Carmody5407a562010-05-26 14:42:42 -07004092 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004093
4094 /*
4095 * Iterate backward over array of thresholds starting from
4096 * current_threshold and check if a threshold is crossed.
4097 * If none of thresholds below usage is crossed, we read
4098 * only one element of the array here.
4099 */
4100 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4101 eventfd_signal(t->entries[i].eventfd, 1);
4102
4103 /* i = current_threshold + 1 */
4104 i++;
4105
4106 /*
4107 * Iterate forward over array of thresholds starting from
4108 * current_threshold+1 and check if a threshold is crossed.
4109 * If none of thresholds above usage is crossed, we read
4110 * only one element of the array here.
4111 */
4112 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4113 eventfd_signal(t->entries[i].eventfd, 1);
4114
4115 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004116 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004117unlock:
4118 rcu_read_unlock();
4119}
4120
4121static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4122{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004123 while (memcg) {
4124 __mem_cgroup_threshold(memcg, false);
Johannes Weiner7941d212016-01-14 15:21:23 -08004125 if (do_memsw_account())
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004126 __mem_cgroup_threshold(memcg, true);
4127
4128 memcg = parent_mem_cgroup(memcg);
4129 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004130}
4131
4132static int compare_thresholds(const void *a, const void *b)
4133{
4134 const struct mem_cgroup_threshold *_a = a;
4135 const struct mem_cgroup_threshold *_b = b;
4136
Greg Thelen2bff24a2013-09-11 14:23:08 -07004137 if (_a->threshold > _b->threshold)
4138 return 1;
4139
4140 if (_a->threshold < _b->threshold)
4141 return -1;
4142
4143 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004144}
4145
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004146static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004147{
4148 struct mem_cgroup_eventfd_list *ev;
4149
Michal Hocko2bcf2e92014-07-30 16:08:33 -07004150 spin_lock(&memcg_oom_lock);
4151
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004152 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004153 eventfd_signal(ev->eventfd, 1);
Michal Hocko2bcf2e92014-07-30 16:08:33 -07004154
4155 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004156 return 0;
4157}
4158
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004159static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004160{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004161 struct mem_cgroup *iter;
4162
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004163 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004164 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004165}
4166
Tejun Heo59b6f872013-11-22 18:20:43 -05004167static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004168 struct eventfd_ctx *eventfd, const char *args, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004169{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004170 struct mem_cgroup_thresholds *thresholds;
4171 struct mem_cgroup_threshold_ary *new;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004172 unsigned long threshold;
4173 unsigned long usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004174 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004175
Johannes Weiner650c5e52015-02-11 15:26:03 -08004176 ret = page_counter_memparse(args, "-1", &threshold);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004177 if (ret)
4178 return ret;
4179
4180 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004181
Johannes Weiner05b84302014-08-06 16:05:59 -07004182 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004183 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004184 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07004185 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004186 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004187 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07004188 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004189 BUG();
4190
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004191 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004192 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004193 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4194
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004195 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004196
4197 /* Allocate memory for new array of thresholds */
Gustavo A. R. Silva67b80462019-03-05 15:44:05 -08004198 new = kmalloc(struct_size(new, entries, size), GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004199 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004200 ret = -ENOMEM;
4201 goto unlock;
4202 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004203 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004204
4205 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004206 if (thresholds->primary) {
4207 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004208 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004209 }
4210
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004211 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004212 new->entries[size - 1].eventfd = eventfd;
4213 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004214
4215 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004216 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004217 compare_thresholds, NULL);
4218
4219 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004220 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004221 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07004222 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004223 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004224 * new->current_threshold will not be used until
4225 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004226 * it here.
4227 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004228 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07004229 } else
4230 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004231 }
4232
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004233 /* Free old spare buffer and save old primary buffer as spare */
4234 kfree(thresholds->spare);
4235 thresholds->spare = thresholds->primary;
4236
4237 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004238
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004239 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004240 synchronize_rcu();
4241
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004242unlock:
4243 mutex_unlock(&memcg->thresholds_lock);
4244
4245 return ret;
4246}
4247
Tejun Heo59b6f872013-11-22 18:20:43 -05004248static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004249 struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004250{
Tejun Heo59b6f872013-11-22 18:20:43 -05004251 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05004252}
4253
Tejun Heo59b6f872013-11-22 18:20:43 -05004254static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004255 struct eventfd_ctx *eventfd, const char *args)
4256{
Tejun Heo59b6f872013-11-22 18:20:43 -05004257 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05004258}
4259
Tejun Heo59b6f872013-11-22 18:20:43 -05004260static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004261 struct eventfd_ctx *eventfd, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004262{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004263 struct mem_cgroup_thresholds *thresholds;
4264 struct mem_cgroup_threshold_ary *new;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004265 unsigned long usage;
Chunguang Xu7d366652020-03-21 18:22:10 -07004266 int i, j, size, entries;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004267
4268 mutex_lock(&memcg->thresholds_lock);
Johannes Weiner05b84302014-08-06 16:05:59 -07004269
4270 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004271 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004272 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07004273 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004274 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004275 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07004276 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004277 BUG();
4278
Anton Vorontsov371528c2012-02-24 05:14:46 +04004279 if (!thresholds->primary)
4280 goto unlock;
4281
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004282 /* Check if a threshold crossed before removing */
4283 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4284
4285 /* Calculate new number of threshold */
Chunguang Xu7d366652020-03-21 18:22:10 -07004286 size = entries = 0;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004287 for (i = 0; i < thresholds->primary->size; i++) {
4288 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004289 size++;
Chunguang Xu7d366652020-03-21 18:22:10 -07004290 else
4291 entries++;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004292 }
4293
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004294 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004295
Chunguang Xu7d366652020-03-21 18:22:10 -07004296 /* If no items related to eventfd have been cleared, nothing to do */
4297 if (!entries)
4298 goto unlock;
4299
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004300 /* Set thresholds array to NULL if we don't have thresholds */
4301 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004302 kfree(new);
4303 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004304 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004305 }
4306
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004307 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004308
4309 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004310 new->current_threshold = -1;
4311 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4312 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004313 continue;
4314
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004315 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07004316 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004317 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004318 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004319 * until rcu_assign_pointer(), so it's safe to increment
4320 * it here.
4321 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004322 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004323 }
4324 j++;
4325 }
4326
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004327swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004328 /* Swap primary and spare array */
4329 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07004330
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004331 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004332
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004333 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004334 synchronize_rcu();
Martijn Coenen6611d8d2016-01-15 16:57:49 -08004335
4336 /* If all events are unregistered, free the spare array */
4337 if (!new) {
4338 kfree(thresholds->spare);
4339 thresholds->spare = NULL;
4340 }
Anton Vorontsov371528c2012-02-24 05:14:46 +04004341unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004342 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004343}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004344
Tejun Heo59b6f872013-11-22 18:20:43 -05004345static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004346 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004347{
Tejun Heo59b6f872013-11-22 18:20:43 -05004348 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05004349}
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004350
Tejun Heo59b6f872013-11-22 18:20:43 -05004351static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004352 struct eventfd_ctx *eventfd)
4353{
Tejun Heo59b6f872013-11-22 18:20:43 -05004354 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05004355}
4356
Tejun Heo59b6f872013-11-22 18:20:43 -05004357static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004358 struct eventfd_ctx *eventfd, const char *args)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004359{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004360 struct mem_cgroup_eventfd_list *event;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004361
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004362 event = kmalloc(sizeof(*event), GFP_KERNEL);
4363 if (!event)
4364 return -ENOMEM;
4365
Michal Hocko1af8efe2011-07-26 16:08:24 -07004366 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004367
4368 event->eventfd = eventfd;
4369 list_add(&event->list, &memcg->oom_notify);
4370
4371 /* already in OOM ? */
Tejun Heoc2b42d32015-06-24 16:58:23 -07004372 if (memcg->under_oom)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004373 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004374 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004375
4376 return 0;
4377}
4378
Tejun Heo59b6f872013-11-22 18:20:43 -05004379static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004380 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004381{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004382 struct mem_cgroup_eventfd_list *ev, *tmp;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004383
Michal Hocko1af8efe2011-07-26 16:08:24 -07004384 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004385
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004386 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004387 if (ev->eventfd == eventfd) {
4388 list_del(&ev->list);
4389 kfree(ev);
4390 }
4391 }
4392
Michal Hocko1af8efe2011-07-26 16:08:24 -07004393 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004394}
4395
Tejun Heo2da8ca82013-12-05 12:28:04 -05004396static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004397{
Chris Downaa9694b2019-03-05 15:45:52 -08004398 struct mem_cgroup *memcg = mem_cgroup_from_seq(sf);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004399
Tejun Heo791badb2013-12-05 12:28:02 -05004400 seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
Tejun Heoc2b42d32015-06-24 16:58:23 -07004401 seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom);
Roman Gushchinfe6bdfc2018-06-14 15:28:05 -07004402 seq_printf(sf, "oom_kill %lu\n",
4403 atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL]));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004404 return 0;
4405}
4406
Tejun Heo182446d2013-08-08 20:11:24 -04004407static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004408 struct cftype *cft, u64 val)
4409{
Tejun Heo182446d2013-08-08 20:11:24 -04004410 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004411
4412 /* cannot set to root cgroup and only 0 and 1 are allowed */
Linus Torvalds14208b02014-06-09 15:03:33 -07004413 if (!css->parent || !((val == 0) || (val == 1)))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004414 return -EINVAL;
4415
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004416 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004417 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004418 memcg_oom_recover(memcg);
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07004419
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004420 return 0;
4421}
4422
Tejun Heo52ebea72015-05-22 17:13:37 -04004423#ifdef CONFIG_CGROUP_WRITEBACK
4424
Tejun Heo3a8e9ac2019-08-29 15:47:19 -07004425#include <trace/events/writeback.h>
4426
Tejun Heo841710a2015-05-22 18:23:33 -04004427static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
4428{
4429 return wb_domain_init(&memcg->cgwb_domain, gfp);
4430}
4431
4432static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
4433{
4434 wb_domain_exit(&memcg->cgwb_domain);
4435}
4436
Tejun Heo2529bb32015-05-22 18:23:34 -04004437static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
4438{
4439 wb_domain_size_changed(&memcg->cgwb_domain);
4440}
4441
Tejun Heo841710a2015-05-22 18:23:33 -04004442struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
4443{
4444 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4445
4446 if (!memcg->css.parent)
4447 return NULL;
4448
4449 return &memcg->cgwb_domain;
4450}
4451
Greg Thelen0b3d6e62019-04-05 18:39:18 -07004452/*
4453 * idx can be of type enum memcg_stat_item or node_stat_item.
4454 * Keep in sync with memcg_exact_page().
4455 */
4456static unsigned long memcg_exact_page_state(struct mem_cgroup *memcg, int idx)
4457{
Chris Down871789d2019-05-14 15:46:57 -07004458 long x = atomic_long_read(&memcg->vmstats[idx]);
Greg Thelen0b3d6e62019-04-05 18:39:18 -07004459 int cpu;
4460
4461 for_each_online_cpu(cpu)
Chris Down871789d2019-05-14 15:46:57 -07004462 x += per_cpu_ptr(memcg->vmstats_percpu, cpu)->stat[idx];
Greg Thelen0b3d6e62019-04-05 18:39:18 -07004463 if (x < 0)
4464 x = 0;
4465 return x;
4466}
4467
Tejun Heoc2aa7232015-05-22 18:23:35 -04004468/**
4469 * mem_cgroup_wb_stats - retrieve writeback related stats from its memcg
4470 * @wb: bdi_writeback in question
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004471 * @pfilepages: out parameter for number of file pages
4472 * @pheadroom: out parameter for number of allocatable pages according to memcg
Tejun Heoc2aa7232015-05-22 18:23:35 -04004473 * @pdirty: out parameter for number of dirty pages
4474 * @pwriteback: out parameter for number of pages under writeback
4475 *
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004476 * Determine the numbers of file, headroom, dirty, and writeback pages in
4477 * @wb's memcg. File, dirty and writeback are self-explanatory. Headroom
4478 * is a bit more involved.
Tejun Heoc2aa7232015-05-22 18:23:35 -04004479 *
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004480 * A memcg's headroom is "min(max, high) - used". In the hierarchy, the
4481 * headroom is calculated as the lowest headroom of itself and the
4482 * ancestors. Note that this doesn't consider the actual amount of
4483 * available memory in the system. The caller should further cap
4484 * *@pheadroom accordingly.
Tejun Heoc2aa7232015-05-22 18:23:35 -04004485 */
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004486void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
4487 unsigned long *pheadroom, unsigned long *pdirty,
4488 unsigned long *pwriteback)
Tejun Heoc2aa7232015-05-22 18:23:35 -04004489{
4490 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4491 struct mem_cgroup *parent;
Tejun Heoc2aa7232015-05-22 18:23:35 -04004492
Greg Thelen0b3d6e62019-04-05 18:39:18 -07004493 *pdirty = memcg_exact_page_state(memcg, NR_FILE_DIRTY);
Tejun Heoc2aa7232015-05-22 18:23:35 -04004494
Greg Thelen0b3d6e62019-04-05 18:39:18 -07004495 *pwriteback = memcg_exact_page_state(memcg, NR_WRITEBACK);
Johannes Weiner21d89d12019-05-13 17:18:08 -07004496 *pfilepages = memcg_exact_page_state(memcg, NR_INACTIVE_FILE) +
4497 memcg_exact_page_state(memcg, NR_ACTIVE_FILE);
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004498 *pheadroom = PAGE_COUNTER_MAX;
Tejun Heoc2aa7232015-05-22 18:23:35 -04004499
Tejun Heoc2aa7232015-05-22 18:23:35 -04004500 while ((parent = parent_mem_cgroup(memcg))) {
Chris Down15b42562020-04-01 21:07:20 -07004501 unsigned long ceiling = min(READ_ONCE(memcg->memory.max),
Jakub Kicinskid1663a92020-06-01 21:49:49 -07004502 READ_ONCE(memcg->memory.high));
Tejun Heoc2aa7232015-05-22 18:23:35 -04004503 unsigned long used = page_counter_read(&memcg->memory);
4504
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004505 *pheadroom = min(*pheadroom, ceiling - min(ceiling, used));
Tejun Heoc2aa7232015-05-22 18:23:35 -04004506 memcg = parent;
4507 }
Tejun Heoc2aa7232015-05-22 18:23:35 -04004508}
4509
Tejun Heo97b27822019-08-26 09:06:56 -07004510/*
4511 * Foreign dirty flushing
4512 *
4513 * There's an inherent mismatch between memcg and writeback. The former
4514 * trackes ownership per-page while the latter per-inode. This was a
4515 * deliberate design decision because honoring per-page ownership in the
4516 * writeback path is complicated, may lead to higher CPU and IO overheads
4517 * and deemed unnecessary given that write-sharing an inode across
4518 * different cgroups isn't a common use-case.
4519 *
4520 * Combined with inode majority-writer ownership switching, this works well
4521 * enough in most cases but there are some pathological cases. For
4522 * example, let's say there are two cgroups A and B which keep writing to
4523 * different but confined parts of the same inode. B owns the inode and
4524 * A's memory is limited far below B's. A's dirty ratio can rise enough to
4525 * trigger balance_dirty_pages() sleeps but B's can be low enough to avoid
4526 * triggering background writeback. A will be slowed down without a way to
4527 * make writeback of the dirty pages happen.
4528 *
4529 * Conditions like the above can lead to a cgroup getting repatedly and
4530 * severely throttled after making some progress after each
4531 * dirty_expire_interval while the underyling IO device is almost
4532 * completely idle.
4533 *
4534 * Solving this problem completely requires matching the ownership tracking
4535 * granularities between memcg and writeback in either direction. However,
4536 * the more egregious behaviors can be avoided by simply remembering the
4537 * most recent foreign dirtying events and initiating remote flushes on
4538 * them when local writeback isn't enough to keep the memory clean enough.
4539 *
4540 * The following two functions implement such mechanism. When a foreign
4541 * page - a page whose memcg and writeback ownerships don't match - is
4542 * dirtied, mem_cgroup_track_foreign_dirty() records the inode owning
4543 * bdi_writeback on the page owning memcg. When balance_dirty_pages()
4544 * decides that the memcg needs to sleep due to high dirty ratio, it calls
4545 * mem_cgroup_flush_foreign() which queues writeback on the recorded
4546 * foreign bdi_writebacks which haven't expired. Both the numbers of
4547 * recorded bdi_writebacks and concurrent in-flight foreign writebacks are
4548 * limited to MEMCG_CGWB_FRN_CNT.
4549 *
4550 * The mechanism only remembers IDs and doesn't hold any object references.
4551 * As being wrong occasionally doesn't matter, updates and accesses to the
4552 * records are lockless and racy.
4553 */
4554void mem_cgroup_track_foreign_dirty_slowpath(struct page *page,
4555 struct bdi_writeback *wb)
4556{
4557 struct mem_cgroup *memcg = page->mem_cgroup;
4558 struct memcg_cgwb_frn *frn;
4559 u64 now = get_jiffies_64();
4560 u64 oldest_at = now;
4561 int oldest = -1;
4562 int i;
4563
Tejun Heo3a8e9ac2019-08-29 15:47:19 -07004564 trace_track_foreign_dirty(page, wb);
4565
Tejun Heo97b27822019-08-26 09:06:56 -07004566 /*
4567 * Pick the slot to use. If there is already a slot for @wb, keep
4568 * using it. If not replace the oldest one which isn't being
4569 * written out.
4570 */
4571 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) {
4572 frn = &memcg->cgwb_frn[i];
4573 if (frn->bdi_id == wb->bdi->id &&
4574 frn->memcg_id == wb->memcg_css->id)
4575 break;
4576 if (time_before64(frn->at, oldest_at) &&
4577 atomic_read(&frn->done.cnt) == 1) {
4578 oldest = i;
4579 oldest_at = frn->at;
4580 }
4581 }
4582
4583 if (i < MEMCG_CGWB_FRN_CNT) {
4584 /*
4585 * Re-using an existing one. Update timestamp lazily to
4586 * avoid making the cacheline hot. We want them to be
4587 * reasonably up-to-date and significantly shorter than
4588 * dirty_expire_interval as that's what expires the record.
4589 * Use the shorter of 1s and dirty_expire_interval / 8.
4590 */
4591 unsigned long update_intv =
4592 min_t(unsigned long, HZ,
4593 msecs_to_jiffies(dirty_expire_interval * 10) / 8);
4594
4595 if (time_before64(frn->at, now - update_intv))
4596 frn->at = now;
4597 } else if (oldest >= 0) {
4598 /* replace the oldest free one */
4599 frn = &memcg->cgwb_frn[oldest];
4600 frn->bdi_id = wb->bdi->id;
4601 frn->memcg_id = wb->memcg_css->id;
4602 frn->at = now;
4603 }
4604}
4605
4606/* issue foreign writeback flushes for recorded foreign dirtying events */
4607void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
4608{
4609 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4610 unsigned long intv = msecs_to_jiffies(dirty_expire_interval * 10);
4611 u64 now = jiffies_64;
4612 int i;
4613
4614 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) {
4615 struct memcg_cgwb_frn *frn = &memcg->cgwb_frn[i];
4616
4617 /*
4618 * If the record is older than dirty_expire_interval,
4619 * writeback on it has already started. No need to kick it
4620 * off again. Also, don't start a new one if there's
4621 * already one in flight.
4622 */
4623 if (time_after64(frn->at, now - intv) &&
4624 atomic_read(&frn->done.cnt) == 1) {
4625 frn->at = 0;
Tejun Heo3a8e9ac2019-08-29 15:47:19 -07004626 trace_flush_foreign(wb, frn->bdi_id, frn->memcg_id);
Tejun Heo97b27822019-08-26 09:06:56 -07004627 cgroup_writeback_by_id(frn->bdi_id, frn->memcg_id, 0,
4628 WB_REASON_FOREIGN_FLUSH,
4629 &frn->done);
4630 }
4631 }
4632}
4633
Tejun Heo841710a2015-05-22 18:23:33 -04004634#else /* CONFIG_CGROUP_WRITEBACK */
4635
4636static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
4637{
4638 return 0;
4639}
4640
4641static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
4642{
4643}
4644
Tejun Heo2529bb32015-05-22 18:23:34 -04004645static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
4646{
4647}
4648
Tejun Heo52ebea72015-05-22 17:13:37 -04004649#endif /* CONFIG_CGROUP_WRITEBACK */
4650
Tejun Heo79bd9812013-11-22 18:20:42 -05004651/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05004652 * DO NOT USE IN NEW FILES.
4653 *
4654 * "cgroup.event_control" implementation.
4655 *
4656 * This is way over-engineered. It tries to support fully configurable
4657 * events for each user. Such level of flexibility is completely
4658 * unnecessary especially in the light of the planned unified hierarchy.
4659 *
4660 * Please deprecate this and replace with something simpler if at all
4661 * possible.
4662 */
4663
4664/*
Tejun Heo79bd9812013-11-22 18:20:42 -05004665 * Unregister event and free resources.
4666 *
4667 * Gets called from workqueue.
4668 */
Tejun Heo3bc942f2013-11-22 18:20:44 -05004669static void memcg_event_remove(struct work_struct *work)
Tejun Heo79bd9812013-11-22 18:20:42 -05004670{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004671 struct mem_cgroup_event *event =
4672 container_of(work, struct mem_cgroup_event, remove);
Tejun Heo59b6f872013-11-22 18:20:43 -05004673 struct mem_cgroup *memcg = event->memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004674
4675 remove_wait_queue(event->wqh, &event->wait);
4676
Tejun Heo59b6f872013-11-22 18:20:43 -05004677 event->unregister_event(memcg, event->eventfd);
Tejun Heo79bd9812013-11-22 18:20:42 -05004678
4679 /* Notify userspace the event is going away. */
4680 eventfd_signal(event->eventfd, 1);
4681
4682 eventfd_ctx_put(event->eventfd);
4683 kfree(event);
Tejun Heo59b6f872013-11-22 18:20:43 -05004684 css_put(&memcg->css);
Tejun Heo79bd9812013-11-22 18:20:42 -05004685}
4686
4687/*
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004688 * Gets called on EPOLLHUP on eventfd when user closes it.
Tejun Heo79bd9812013-11-22 18:20:42 -05004689 *
4690 * Called with wqh->lock held and interrupts disabled.
4691 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02004692static int memcg_event_wake(wait_queue_entry_t *wait, unsigned mode,
Tejun Heo3bc942f2013-11-22 18:20:44 -05004693 int sync, void *key)
Tejun Heo79bd9812013-11-22 18:20:42 -05004694{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004695 struct mem_cgroup_event *event =
4696 container_of(wait, struct mem_cgroup_event, wait);
Tejun Heo59b6f872013-11-22 18:20:43 -05004697 struct mem_cgroup *memcg = event->memcg;
Al Viro3ad6f932017-07-03 20:14:56 -04004698 __poll_t flags = key_to_poll(key);
Tejun Heo79bd9812013-11-22 18:20:42 -05004699
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004700 if (flags & EPOLLHUP) {
Tejun Heo79bd9812013-11-22 18:20:42 -05004701 /*
4702 * If the event has been detached at cgroup removal, we
4703 * can simply return knowing the other side will cleanup
4704 * for us.
4705 *
4706 * We can't race against event freeing since the other
4707 * side will require wqh->lock via remove_wait_queue(),
4708 * which we hold.
4709 */
Tejun Heofba94802013-11-22 18:20:43 -05004710 spin_lock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004711 if (!list_empty(&event->list)) {
4712 list_del_init(&event->list);
4713 /*
4714 * We are in atomic context, but cgroup_event_remove()
4715 * may sleep, so we have to call it in workqueue.
4716 */
4717 schedule_work(&event->remove);
4718 }
Tejun Heofba94802013-11-22 18:20:43 -05004719 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004720 }
4721
4722 return 0;
4723}
4724
Tejun Heo3bc942f2013-11-22 18:20:44 -05004725static void memcg_event_ptable_queue_proc(struct file *file,
Tejun Heo79bd9812013-11-22 18:20:42 -05004726 wait_queue_head_t *wqh, poll_table *pt)
4727{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004728 struct mem_cgroup_event *event =
4729 container_of(pt, struct mem_cgroup_event, pt);
Tejun Heo79bd9812013-11-22 18:20:42 -05004730
4731 event->wqh = wqh;
4732 add_wait_queue(wqh, &event->wait);
4733}
4734
4735/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05004736 * DO NOT USE IN NEW FILES.
4737 *
Tejun Heo79bd9812013-11-22 18:20:42 -05004738 * Parse input and register new cgroup event handler.
4739 *
4740 * Input must be in format '<event_fd> <control_fd> <args>'.
4741 * Interpretation of args is defined by control file implementation.
4742 */
Tejun Heo451af502014-05-13 12:16:21 -04004743static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
4744 char *buf, size_t nbytes, loff_t off)
Tejun Heo79bd9812013-11-22 18:20:42 -05004745{
Tejun Heo451af502014-05-13 12:16:21 -04004746 struct cgroup_subsys_state *css = of_css(of);
Tejun Heofba94802013-11-22 18:20:43 -05004747 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004748 struct mem_cgroup_event *event;
Tejun Heo79bd9812013-11-22 18:20:42 -05004749 struct cgroup_subsys_state *cfile_css;
4750 unsigned int efd, cfd;
4751 struct fd efile;
4752 struct fd cfile;
Tejun Heofba94802013-11-22 18:20:43 -05004753 const char *name;
Tejun Heo79bd9812013-11-22 18:20:42 -05004754 char *endp;
4755 int ret;
4756
Tejun Heo451af502014-05-13 12:16:21 -04004757 buf = strstrip(buf);
4758
4759 efd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05004760 if (*endp != ' ')
4761 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04004762 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05004763
Tejun Heo451af502014-05-13 12:16:21 -04004764 cfd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05004765 if ((*endp != ' ') && (*endp != '\0'))
4766 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04004767 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05004768
4769 event = kzalloc(sizeof(*event), GFP_KERNEL);
4770 if (!event)
4771 return -ENOMEM;
4772
Tejun Heo59b6f872013-11-22 18:20:43 -05004773 event->memcg = memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004774 INIT_LIST_HEAD(&event->list);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004775 init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc);
4776 init_waitqueue_func_entry(&event->wait, memcg_event_wake);
4777 INIT_WORK(&event->remove, memcg_event_remove);
Tejun Heo79bd9812013-11-22 18:20:42 -05004778
4779 efile = fdget(efd);
4780 if (!efile.file) {
4781 ret = -EBADF;
4782 goto out_kfree;
4783 }
4784
4785 event->eventfd = eventfd_ctx_fileget(efile.file);
4786 if (IS_ERR(event->eventfd)) {
4787 ret = PTR_ERR(event->eventfd);
4788 goto out_put_efile;
4789 }
4790
4791 cfile = fdget(cfd);
4792 if (!cfile.file) {
4793 ret = -EBADF;
4794 goto out_put_eventfd;
4795 }
4796
4797 /* the process need read permission on control file */
4798 /* AV: shouldn't we check that it's been opened for read instead? */
4799 ret = inode_permission(file_inode(cfile.file), MAY_READ);
4800 if (ret < 0)
4801 goto out_put_cfile;
4802
Tejun Heo79bd9812013-11-22 18:20:42 -05004803 /*
Tejun Heofba94802013-11-22 18:20:43 -05004804 * Determine the event callbacks and set them in @event. This used
4805 * to be done via struct cftype but cgroup core no longer knows
4806 * about these events. The following is crude but the whole thing
4807 * is for compatibility anyway.
Tejun Heo3bc942f2013-11-22 18:20:44 -05004808 *
4809 * DO NOT ADD NEW FILES.
Tejun Heofba94802013-11-22 18:20:43 -05004810 */
Al Virob5830432014-10-31 01:22:04 -04004811 name = cfile.file->f_path.dentry->d_name.name;
Tejun Heofba94802013-11-22 18:20:43 -05004812
4813 if (!strcmp(name, "memory.usage_in_bytes")) {
4814 event->register_event = mem_cgroup_usage_register_event;
4815 event->unregister_event = mem_cgroup_usage_unregister_event;
4816 } else if (!strcmp(name, "memory.oom_control")) {
4817 event->register_event = mem_cgroup_oom_register_event;
4818 event->unregister_event = mem_cgroup_oom_unregister_event;
4819 } else if (!strcmp(name, "memory.pressure_level")) {
4820 event->register_event = vmpressure_register_event;
4821 event->unregister_event = vmpressure_unregister_event;
4822 } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) {
Tejun Heo347c4a82013-11-22 18:20:43 -05004823 event->register_event = memsw_cgroup_usage_register_event;
4824 event->unregister_event = memsw_cgroup_usage_unregister_event;
Tejun Heofba94802013-11-22 18:20:43 -05004825 } else {
4826 ret = -EINVAL;
4827 goto out_put_cfile;
4828 }
4829
4830 /*
Tejun Heob5557c42013-11-22 18:20:42 -05004831 * Verify @cfile should belong to @css. Also, remaining events are
4832 * automatically removed on cgroup destruction but the removal is
4833 * asynchronous, so take an extra ref on @css.
Tejun Heo79bd9812013-11-22 18:20:42 -05004834 */
Al Virob5830432014-10-31 01:22:04 -04004835 cfile_css = css_tryget_online_from_dir(cfile.file->f_path.dentry->d_parent,
Tejun Heoec903c02014-05-13 12:11:01 -04004836 &memory_cgrp_subsys);
Tejun Heo79bd9812013-11-22 18:20:42 -05004837 ret = -EINVAL;
Tejun Heo5a17f542014-02-11 11:52:47 -05004838 if (IS_ERR(cfile_css))
Tejun Heo79bd9812013-11-22 18:20:42 -05004839 goto out_put_cfile;
Tejun Heo5a17f542014-02-11 11:52:47 -05004840 if (cfile_css != css) {
4841 css_put(cfile_css);
4842 goto out_put_cfile;
4843 }
Tejun Heo79bd9812013-11-22 18:20:42 -05004844
Tejun Heo451af502014-05-13 12:16:21 -04004845 ret = event->register_event(memcg, event->eventfd, buf);
Tejun Heo79bd9812013-11-22 18:20:42 -05004846 if (ret)
4847 goto out_put_css;
4848
Christoph Hellwig9965ed172018-03-05 07:26:05 -08004849 vfs_poll(efile.file, &event->pt);
Tejun Heo79bd9812013-11-22 18:20:42 -05004850
Tejun Heofba94802013-11-22 18:20:43 -05004851 spin_lock(&memcg->event_list_lock);
4852 list_add(&event->list, &memcg->event_list);
4853 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004854
4855 fdput(cfile);
4856 fdput(efile);
4857
Tejun Heo451af502014-05-13 12:16:21 -04004858 return nbytes;
Tejun Heo79bd9812013-11-22 18:20:42 -05004859
4860out_put_css:
Tejun Heob5557c42013-11-22 18:20:42 -05004861 css_put(css);
Tejun Heo79bd9812013-11-22 18:20:42 -05004862out_put_cfile:
4863 fdput(cfile);
4864out_put_eventfd:
4865 eventfd_ctx_put(event->eventfd);
4866out_put_efile:
4867 fdput(efile);
4868out_kfree:
4869 kfree(event);
4870
4871 return ret;
4872}
4873
Johannes Weiner241994ed2015-02-11 15:26:06 -08004874static struct cftype mem_cgroup_legacy_files[] = {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004875 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004876 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004877 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05004878 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004879 },
4880 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004881 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004882 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04004883 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004884 .read_u64 = mem_cgroup_read_u64,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004885 },
4886 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004887 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004888 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004889 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004890 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004891 },
4892 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004893 .name = "soft_limit_in_bytes",
4894 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004895 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004896 .read_u64 = mem_cgroup_read_u64,
Balbir Singh296c81d2009-09-23 15:56:36 -07004897 },
4898 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004899 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004900 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04004901 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004902 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004903 },
Balbir Singh8697d332008-02-07 00:13:59 -08004904 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004905 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004906 .seq_show = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004907 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004908 {
4909 .name = "force_empty",
Tejun Heo6770c642014-05-13 12:16:21 -04004910 .write = mem_cgroup_force_empty_write,
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004911 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004912 {
4913 .name = "use_hierarchy",
4914 .write_u64 = mem_cgroup_hierarchy_write,
4915 .read_u64 = mem_cgroup_hierarchy_read,
4916 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004917 {
Tejun Heo3bc942f2013-11-22 18:20:44 -05004918 .name = "cgroup.event_control", /* XXX: for compat */
Tejun Heo451af502014-05-13 12:16:21 -04004919 .write = memcg_write_event_control,
Tejun Heo7dbdb192015-09-18 17:54:23 -04004920 .flags = CFTYPE_NO_PREFIX | CFTYPE_WORLD_WRITABLE,
Tejun Heo79bd9812013-11-22 18:20:42 -05004921 },
4922 {
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004923 .name = "swappiness",
4924 .read_u64 = mem_cgroup_swappiness_read,
4925 .write_u64 = mem_cgroup_swappiness_write,
4926 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004927 {
4928 .name = "move_charge_at_immigrate",
4929 .read_u64 = mem_cgroup_move_charge_read,
4930 .write_u64 = mem_cgroup_move_charge_write,
4931 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004932 {
4933 .name = "oom_control",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004934 .seq_show = mem_cgroup_oom_control_read,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004935 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004936 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4937 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004938 {
4939 .name = "pressure_level",
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004940 },
Ying Han406eb0c2011-05-26 16:25:37 -07004941#ifdef CONFIG_NUMA
4942 {
4943 .name = "numa_stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004944 .seq_show = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07004945 },
4946#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08004947 {
4948 .name = "kmem.limit_in_bytes",
4949 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004950 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004951 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004952 },
4953 {
4954 .name = "kmem.usage_in_bytes",
4955 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05004956 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004957 },
4958 {
4959 .name = "kmem.failcnt",
4960 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04004961 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004962 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004963 },
4964 {
4965 .name = "kmem.max_usage_in_bytes",
4966 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04004967 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004968 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004969 },
Yafang Shaoa87425a2020-04-01 21:06:30 -07004970#if defined(CONFIG_MEMCG_KMEM) && \
4971 (defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG))
Glauber Costa749c5412012-12-18 14:23:01 -08004972 {
4973 .name = "kmem.slabinfo",
Vladimir Davydovb0475012014-12-10 15:44:19 -08004974 .seq_show = memcg_slab_show,
Glauber Costa749c5412012-12-18 14:23:01 -08004975 },
4976#endif
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08004977 {
4978 .name = "kmem.tcp.limit_in_bytes",
4979 .private = MEMFILE_PRIVATE(_TCP, RES_LIMIT),
4980 .write = mem_cgroup_write,
4981 .read_u64 = mem_cgroup_read_u64,
4982 },
4983 {
4984 .name = "kmem.tcp.usage_in_bytes",
4985 .private = MEMFILE_PRIVATE(_TCP, RES_USAGE),
4986 .read_u64 = mem_cgroup_read_u64,
4987 },
4988 {
4989 .name = "kmem.tcp.failcnt",
4990 .private = MEMFILE_PRIVATE(_TCP, RES_FAILCNT),
4991 .write = mem_cgroup_reset,
4992 .read_u64 = mem_cgroup_read_u64,
4993 },
4994 {
4995 .name = "kmem.tcp.max_usage_in_bytes",
4996 .private = MEMFILE_PRIVATE(_TCP, RES_MAX_USAGE),
4997 .write = mem_cgroup_reset,
4998 .read_u64 = mem_cgroup_read_u64,
4999 },
Tejun Heo6bc10342012-04-01 12:09:55 -07005000 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005001};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005002
Johannes Weiner73f576c2016-07-20 15:44:57 -07005003/*
5004 * Private memory cgroup IDR
5005 *
5006 * Swap-out records and page cache shadow entries need to store memcg
5007 * references in constrained space, so we maintain an ID space that is
5008 * limited to 16 bit (MEM_CGROUP_ID_MAX), limiting the total number of
5009 * memory-controlled cgroups to 64k.
5010 *
Ethon Paulb8f29352020-06-04 16:49:28 -07005011 * However, there usually are many references to the offline CSS after
Johannes Weiner73f576c2016-07-20 15:44:57 -07005012 * the cgroup has been destroyed, such as page cache or reclaimable
5013 * slab objects, that don't need to hang on to the ID. We want to keep
5014 * those dead CSS from occupying IDs, or we might quickly exhaust the
5015 * relatively small ID space and prevent the creation of new cgroups
5016 * even when there are much fewer than 64k cgroups - possibly none.
5017 *
5018 * Maintain a private 16-bit ID space for memcg, and allow the ID to
5019 * be freed and recycled when it's no longer needed, which is usually
5020 * when the CSS is offlined.
5021 *
5022 * The only exception to that are records of swapped out tmpfs/shmem
5023 * pages that need to be attributed to live ancestors on swapin. But
5024 * those references are manageable from userspace.
5025 */
5026
5027static DEFINE_IDR(mem_cgroup_idr);
5028
Kirill Tkhai7e97de02018-08-02 15:36:01 -07005029static void mem_cgroup_id_remove(struct mem_cgroup *memcg)
5030{
5031 if (memcg->id.id > 0) {
5032 idr_remove(&mem_cgroup_idr, memcg->id.id);
5033 memcg->id.id = 0;
5034 }
5035}
5036
Vincenzo Frascinoc1514c02020-04-01 21:07:13 -07005037static void __maybe_unused mem_cgroup_id_get_many(struct mem_cgroup *memcg,
5038 unsigned int n)
Johannes Weiner73f576c2016-07-20 15:44:57 -07005039{
Kirill Tkhai1c2d4792018-10-26 15:09:28 -07005040 refcount_add(n, &memcg->id.ref);
Johannes Weiner73f576c2016-07-20 15:44:57 -07005041}
5042
Vladimir Davydov615d66c2016-08-11 15:33:03 -07005043static void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n)
Johannes Weiner73f576c2016-07-20 15:44:57 -07005044{
Kirill Tkhai1c2d4792018-10-26 15:09:28 -07005045 if (refcount_sub_and_test(n, &memcg->id.ref)) {
Kirill Tkhai7e97de02018-08-02 15:36:01 -07005046 mem_cgroup_id_remove(memcg);
Johannes Weiner73f576c2016-07-20 15:44:57 -07005047
5048 /* Memcg ID pins CSS */
5049 css_put(&memcg->css);
5050 }
5051}
5052
Vladimir Davydov615d66c2016-08-11 15:33:03 -07005053static inline void mem_cgroup_id_put(struct mem_cgroup *memcg)
5054{
5055 mem_cgroup_id_put_many(memcg, 1);
5056}
5057
Johannes Weiner73f576c2016-07-20 15:44:57 -07005058/**
5059 * mem_cgroup_from_id - look up a memcg from a memcg id
5060 * @id: the memcg id to look up
5061 *
5062 * Caller must hold rcu_read_lock().
5063 */
5064struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
5065{
5066 WARN_ON_ONCE(!rcu_read_lock_held());
5067 return idr_find(&mem_cgroup_idr, id);
5068}
5069
Mel Gormanef8f2322016-07-28 15:46:05 -07005070static int alloc_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005071{
5072 struct mem_cgroup_per_node *pn;
Mel Gormanef8f2322016-07-28 15:46:05 -07005073 int tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005074 /*
5075 * This routine is called against possible nodes.
5076 * But it's BUG to call kmalloc() against offline node.
5077 *
5078 * TODO: this routine can waste much memory for nodes which will
5079 * never be onlined. It's better to use memory hotplug callback
5080 * function.
5081 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005082 if (!node_state(node, N_NORMAL_MEMORY))
5083 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005084 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005085 if (!pn)
5086 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005087
Johannes Weiner815744d2019-06-13 15:55:46 -07005088 pn->lruvec_stat_local = alloc_percpu(struct lruvec_stat);
5089 if (!pn->lruvec_stat_local) {
5090 kfree(pn);
5091 return 1;
5092 }
5093
Johannes Weinera983b5e2018-01-31 16:16:45 -08005094 pn->lruvec_stat_cpu = alloc_percpu(struct lruvec_stat);
5095 if (!pn->lruvec_stat_cpu) {
Johannes Weiner815744d2019-06-13 15:55:46 -07005096 free_percpu(pn->lruvec_stat_local);
Johannes Weiner00f3ca22017-07-06 15:40:52 -07005097 kfree(pn);
5098 return 1;
5099 }
5100
Mel Gormanef8f2322016-07-28 15:46:05 -07005101 lruvec_init(&pn->lruvec);
5102 pn->usage_in_excess = 0;
5103 pn->on_tree = false;
5104 pn->memcg = memcg;
5105
Johannes Weiner54f72fe2013-07-08 15:59:49 -07005106 memcg->nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005107 return 0;
5108}
5109
Mel Gormanef8f2322016-07-28 15:46:05 -07005110static void free_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005111{
Johannes Weiner00f3ca22017-07-06 15:40:52 -07005112 struct mem_cgroup_per_node *pn = memcg->nodeinfo[node];
5113
Michal Hocko4eaf4312018-04-10 16:29:52 -07005114 if (!pn)
5115 return;
5116
Johannes Weinera983b5e2018-01-31 16:16:45 -08005117 free_percpu(pn->lruvec_stat_cpu);
Johannes Weiner815744d2019-06-13 15:55:46 -07005118 free_percpu(pn->lruvec_stat_local);
Johannes Weiner00f3ca22017-07-06 15:40:52 -07005119 kfree(pn);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005120}
5121
Tahsin Erdogan40e952f2017-03-09 16:17:26 -08005122static void __mem_cgroup_free(struct mem_cgroup *memcg)
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005123{
5124 int node;
5125
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005126 for_each_node(node)
Mel Gormanef8f2322016-07-28 15:46:05 -07005127 free_mem_cgroup_per_node_info(memcg, node);
Chris Down871789d2019-05-14 15:46:57 -07005128 free_percpu(memcg->vmstats_percpu);
Johannes Weiner815744d2019-06-13 15:55:46 -07005129 free_percpu(memcg->vmstats_local);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005130 kfree(memcg);
5131}
5132
Tahsin Erdogan40e952f2017-03-09 16:17:26 -08005133static void mem_cgroup_free(struct mem_cgroup *memcg)
5134{
5135 memcg_wb_domain_exit(memcg);
Shakeel Butt7961eee2019-11-05 21:16:21 -08005136 /*
5137 * Flush percpu vmstats and vmevents to guarantee the value correctness
5138 * on parent's and all ancestor levels.
5139 */
Roman Gushchin4a87e2a2020-01-13 16:29:16 -08005140 memcg_flush_percpu_vmstats(memcg);
Shakeel Butt7961eee2019-11-05 21:16:21 -08005141 memcg_flush_percpu_vmevents(memcg);
Tahsin Erdogan40e952f2017-03-09 16:17:26 -08005142 __mem_cgroup_free(memcg);
5143}
5144
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005145static struct mem_cgroup *mem_cgroup_alloc(void)
5146{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005147 struct mem_cgroup *memcg;
Alexey Dobriyanb9726c22019-03-05 15:48:26 -08005148 unsigned int size;
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005149 int node;
Tejun Heo97b27822019-08-26 09:06:56 -07005150 int __maybe_unused i;
Yafang Shao11d67612020-05-07 18:35:43 -07005151 long error = -ENOMEM;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005152
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08005153 size = sizeof(struct mem_cgroup);
5154 size += nr_node_ids * sizeof(struct mem_cgroup_per_node *);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005155
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08005156 memcg = kzalloc(size, GFP_KERNEL);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005157 if (!memcg)
Yafang Shao11d67612020-05-07 18:35:43 -07005158 return ERR_PTR(error);
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005159
Johannes Weiner73f576c2016-07-20 15:44:57 -07005160 memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL,
5161 1, MEM_CGROUP_ID_MAX,
5162 GFP_KERNEL);
Yafang Shao11d67612020-05-07 18:35:43 -07005163 if (memcg->id.id < 0) {
5164 error = memcg->id.id;
Johannes Weiner73f576c2016-07-20 15:44:57 -07005165 goto fail;
Yafang Shao11d67612020-05-07 18:35:43 -07005166 }
Johannes Weiner73f576c2016-07-20 15:44:57 -07005167
Johannes Weiner815744d2019-06-13 15:55:46 -07005168 memcg->vmstats_local = alloc_percpu(struct memcg_vmstats_percpu);
5169 if (!memcg->vmstats_local)
5170 goto fail;
5171
Chris Down871789d2019-05-14 15:46:57 -07005172 memcg->vmstats_percpu = alloc_percpu(struct memcg_vmstats_percpu);
5173 if (!memcg->vmstats_percpu)
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005174 goto fail;
Pavel Emelianov78fb7462008-02-07 00:13:51 -08005175
Bob Liu3ed28fa2012-01-12 17:19:04 -08005176 for_each_node(node)
Mel Gormanef8f2322016-07-28 15:46:05 -07005177 if (alloc_mem_cgroup_per_node_info(memcg, node))
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005178 goto fail;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005179
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005180 if (memcg_wb_domain_init(memcg, GFP_KERNEL))
5181 goto fail;
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005182
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08005183 INIT_WORK(&memcg->high_work, high_work_func);
Glauber Costad142e3e2013-02-22 16:34:52 -08005184 INIT_LIST_HEAD(&memcg->oom_notify);
Glauber Costad142e3e2013-02-22 16:34:52 -08005185 mutex_init(&memcg->thresholds_lock);
5186 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07005187 vmpressure_init(&memcg->vmpressure);
Tejun Heofba94802013-11-22 18:20:43 -05005188 INIT_LIST_HEAD(&memcg->event_list);
5189 spin_lock_init(&memcg->event_list_lock);
Johannes Weinerd886f4e2016-01-20 15:02:47 -08005190 memcg->socket_pressure = jiffies;
Kirill Tkhai84c07d12018-08-17 15:47:25 -07005191#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydov900a38f2014-12-12 16:55:10 -08005192 memcg->kmemcg_id = -1;
Roman Gushchinbf4f0592020-08-06 23:20:49 -07005193 INIT_LIST_HEAD(&memcg->objcg_list);
Vladimir Davydov900a38f2014-12-12 16:55:10 -08005194#endif
Tejun Heo52ebea72015-05-22 17:13:37 -04005195#ifdef CONFIG_CGROUP_WRITEBACK
5196 INIT_LIST_HEAD(&memcg->cgwb_list);
Tejun Heo97b27822019-08-26 09:06:56 -07005197 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++)
5198 memcg->cgwb_frn[i].done =
5199 __WB_COMPLETION_INIT(&memcg_cgwb_frn_waitq);
Tejun Heo52ebea72015-05-22 17:13:37 -04005200#endif
Yang Shi87eaceb2019-09-23 15:38:15 -07005201#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5202 spin_lock_init(&memcg->deferred_split_queue.split_queue_lock);
5203 INIT_LIST_HEAD(&memcg->deferred_split_queue.split_queue);
5204 memcg->deferred_split_queue.split_queue_len = 0;
5205#endif
Johannes Weiner73f576c2016-07-20 15:44:57 -07005206 idr_replace(&mem_cgroup_idr, memcg, memcg->id.id);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005207 return memcg;
5208fail:
Kirill Tkhai7e97de02018-08-02 15:36:01 -07005209 mem_cgroup_id_remove(memcg);
Tahsin Erdogan40e952f2017-03-09 16:17:26 -08005210 __mem_cgroup_free(memcg);
Yafang Shao11d67612020-05-07 18:35:43 -07005211 return ERR_PTR(error);
Glauber Costad142e3e2013-02-22 16:34:52 -08005212}
5213
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005214static struct cgroup_subsys_state * __ref
5215mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Glauber Costad142e3e2013-02-22 16:34:52 -08005216{
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005217 struct mem_cgroup *parent = mem_cgroup_from_css(parent_css);
5218 struct mem_cgroup *memcg;
5219 long error = -ENOMEM;
Glauber Costad142e3e2013-02-22 16:34:52 -08005220
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005221 memcg = mem_cgroup_alloc();
Yafang Shao11d67612020-05-07 18:35:43 -07005222 if (IS_ERR(memcg))
5223 return ERR_CAST(memcg);
Li Zefan4219b2d2013-09-23 16:56:29 +08005224
Jakub Kicinskid1663a92020-06-01 21:49:49 -07005225 page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005226 memcg->soft_limit = PAGE_COUNTER_MAX;
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07005227 page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005228 if (parent) {
5229 memcg->swappiness = mem_cgroup_swappiness(parent);
5230 memcg->oom_kill_disable = parent->oom_kill_disable;
5231 }
5232 if (parent && parent->use_hierarchy) {
5233 memcg->use_hierarchy = true;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005234 page_counter_init(&memcg->memory, &parent->memory);
Vladimir Davydov37e84352016-01-20 15:02:56 -08005235 page_counter_init(&memcg->swap, &parent->swap);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005236 page_counter_init(&memcg->memsw, &parent->memsw);
5237 page_counter_init(&memcg->kmem, &parent->kmem);
Johannes Weiner0db15292016-01-20 15:02:50 -08005238 page_counter_init(&memcg->tcpmem, &parent->tcpmem);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005239 } else {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005240 page_counter_init(&memcg->memory, NULL);
Vladimir Davydov37e84352016-01-20 15:02:56 -08005241 page_counter_init(&memcg->swap, NULL);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005242 page_counter_init(&memcg->memsw, NULL);
5243 page_counter_init(&memcg->kmem, NULL);
Johannes Weiner0db15292016-01-20 15:02:50 -08005244 page_counter_init(&memcg->tcpmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07005245 /*
5246 * Deeper hierachy with use_hierarchy == false doesn't make
5247 * much sense so let cgroup subsystem know about this
5248 * unfortunate state in our controller.
5249 */
Glauber Costad142e3e2013-02-22 16:34:52 -08005250 if (parent != root_mem_cgroup)
Tejun Heo073219e2014-02-08 10:36:58 -05005251 memory_cgrp_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005252 }
Vladimir Davydovd6441632014-01-23 15:53:09 -08005253
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005254 /* The following stuff does not apply to the root */
5255 if (!parent) {
5256 root_mem_cgroup = memcg;
5257 return &memcg->css;
5258 }
5259
Vladimir Davydovb313aee2016-03-17 14:18:27 -07005260 error = memcg_online_kmem(memcg);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005261 if (error)
5262 goto fail;
Johannes Weiner127424c2016-01-20 15:02:32 -08005263
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08005264 if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket)
Johannes Weineref129472016-01-14 15:21:34 -08005265 static_branch_inc(&memcg_sockets_enabled_key);
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08005266
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005267 return &memcg->css;
5268fail:
Kirill Tkhai7e97de02018-08-02 15:36:01 -07005269 mem_cgroup_id_remove(memcg);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005270 mem_cgroup_free(memcg);
Yafang Shao11d67612020-05-07 18:35:43 -07005271 return ERR_PTR(error);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005272}
5273
Johannes Weiner73f576c2016-07-20 15:44:57 -07005274static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005275{
Vladimir Davydov58fa2a52016-10-07 16:57:29 -07005276 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5277
Kirill Tkhai0a4465d2018-08-17 15:47:37 -07005278 /*
5279 * A memcg must be visible for memcg_expand_shrinker_maps()
5280 * by the time the maps are allocated. So, we allocate maps
5281 * here, when for_each_mem_cgroup() can't skip it.
5282 */
5283 if (memcg_alloc_shrinker_maps(memcg)) {
5284 mem_cgroup_id_remove(memcg);
5285 return -ENOMEM;
5286 }
5287
Johannes Weiner73f576c2016-07-20 15:44:57 -07005288 /* Online state pins memcg ID, memcg ID pins CSS */
Kirill Tkhai1c2d4792018-10-26 15:09:28 -07005289 refcount_set(&memcg->id.ref, 1);
Johannes Weiner73f576c2016-07-20 15:44:57 -07005290 css_get(css);
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -07005291 return 0;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005292}
5293
Tejun Heoeb954192013-08-08 20:11:23 -04005294static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005295{
Tejun Heoeb954192013-08-08 20:11:23 -04005296 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05005297 struct mem_cgroup_event *event, *tmp;
Tejun Heo79bd9812013-11-22 18:20:42 -05005298
5299 /*
5300 * Unregister events and notify userspace.
5301 * Notify userspace about cgroup removing only after rmdir of cgroup
5302 * directory to avoid race between userspace and kernelspace.
5303 */
Tejun Heofba94802013-11-22 18:20:43 -05005304 spin_lock(&memcg->event_list_lock);
5305 list_for_each_entry_safe(event, tmp, &memcg->event_list, list) {
Tejun Heo79bd9812013-11-22 18:20:42 -05005306 list_del_init(&event->list);
5307 schedule_work(&event->remove);
5308 }
Tejun Heofba94802013-11-22 18:20:43 -05005309 spin_unlock(&memcg->event_list_lock);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005310
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005311 page_counter_set_min(&memcg->memory, 0);
Roman Gushchin23067152018-06-07 17:06:22 -07005312 page_counter_set_low(&memcg->memory, 0);
Roman Gushchin63677c742017-09-06 16:21:47 -07005313
Johannes Weiner567e9ab2016-01-20 15:02:24 -08005314 memcg_offline_kmem(memcg);
Tejun Heo52ebea72015-05-22 17:13:37 -04005315 wb_memcg_offline(memcg);
Johannes Weiner73f576c2016-07-20 15:44:57 -07005316
Roman Gushchin591edfb2018-10-26 15:03:23 -07005317 drain_all_stock(memcg);
5318
Johannes Weiner73f576c2016-07-20 15:44:57 -07005319 mem_cgroup_id_put(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005320}
5321
Vladimir Davydov6df38682015-12-29 14:54:10 -08005322static void mem_cgroup_css_released(struct cgroup_subsys_state *css)
5323{
5324 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5325
5326 invalidate_reclaim_iterators(memcg);
5327}
5328
Tejun Heoeb954192013-08-08 20:11:23 -04005329static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005330{
Tejun Heoeb954192013-08-08 20:11:23 -04005331 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo97b27822019-08-26 09:06:56 -07005332 int __maybe_unused i;
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005333
Tejun Heo97b27822019-08-26 09:06:56 -07005334#ifdef CONFIG_CGROUP_WRITEBACK
5335 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++)
5336 wb_wait_for_completion(&memcg->cgwb_frn[i].done);
5337#endif
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08005338 if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket)
Johannes Weineref129472016-01-14 15:21:34 -08005339 static_branch_dec(&memcg_sockets_enabled_key);
Johannes Weiner3893e302016-01-20 15:02:29 -08005340
Johannes Weiner0db15292016-01-20 15:02:50 -08005341 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_active)
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08005342 static_branch_dec(&memcg_sockets_enabled_key);
Johannes Weiner3893e302016-01-20 15:02:29 -08005343
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005344 vmpressure_cleanup(&memcg->vmpressure);
5345 cancel_work_sync(&memcg->high_work);
5346 mem_cgroup_remove_from_trees(memcg);
Kirill Tkhai0a4465d2018-08-17 15:47:37 -07005347 memcg_free_shrinker_maps(memcg);
Johannes Weinerd886f4e2016-01-20 15:02:47 -08005348 memcg_free_kmem(memcg);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08005349 mem_cgroup_free(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005350}
5351
Tejun Heo1ced9532014-07-08 18:02:57 -04005352/**
5353 * mem_cgroup_css_reset - reset the states of a mem_cgroup
5354 * @css: the target css
5355 *
5356 * Reset the states of the mem_cgroup associated with @css. This is
5357 * invoked when the userland requests disabling on the default hierarchy
5358 * but the memcg is pinned through dependency. The memcg should stop
5359 * applying policies and should revert to the vanilla state as it may be
5360 * made visible again.
5361 *
5362 * The current implementation only resets the essential configurations.
5363 * This needs to be expanded to cover all the visible parts.
5364 */
5365static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
5366{
5367 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5368
Roman Gushchinbbec2e12018-06-07 17:06:18 -07005369 page_counter_set_max(&memcg->memory, PAGE_COUNTER_MAX);
5370 page_counter_set_max(&memcg->swap, PAGE_COUNTER_MAX);
5371 page_counter_set_max(&memcg->memsw, PAGE_COUNTER_MAX);
5372 page_counter_set_max(&memcg->kmem, PAGE_COUNTER_MAX);
5373 page_counter_set_max(&memcg->tcpmem, PAGE_COUNTER_MAX);
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005374 page_counter_set_min(&memcg->memory, 0);
Roman Gushchin23067152018-06-07 17:06:22 -07005375 page_counter_set_low(&memcg->memory, 0);
Jakub Kicinskid1663a92020-06-01 21:49:49 -07005376 page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX);
Johannes Weiner24d404d2015-01-08 14:32:35 -08005377 memcg->soft_limit = PAGE_COUNTER_MAX;
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07005378 page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
Tejun Heo2529bb32015-05-22 18:23:34 -04005379 memcg_wb_domain_size_changed(memcg);
Tejun Heo1ced9532014-07-08 18:02:57 -04005380}
5381
Daisuke Nishimura02491442010-03-10 15:22:17 -08005382#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005383/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005384static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005385{
Johannes Weiner05b84302014-08-06 16:05:59 -07005386 int ret;
Johannes Weiner9476db92014-08-06 16:05:55 -07005387
Mel Gormand0164ad2015-11-06 16:28:21 -08005388 /* Try a single bulk charge without reclaim first, kswapd may wake */
5389 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_DIRECT_RECLAIM, count);
Johannes Weiner9476db92014-08-06 16:05:55 -07005390 if (!ret) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005391 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005392 return ret;
5393 }
Johannes Weiner9476db92014-08-06 16:05:55 -07005394
David Rientjes36745342017-01-24 15:18:10 -08005395 /* Try charges one by one with reclaim, but do not retry */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005396 while (count--) {
David Rientjes36745342017-01-24 15:18:10 -08005397 ret = try_charge(mc.to, GFP_KERNEL | __GFP_NORETRY, 1);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005398 if (ret)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005399 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005400 mc.precharge++;
Johannes Weiner9476db92014-08-06 16:05:55 -07005401 cond_resched();
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005402 }
Johannes Weiner9476db92014-08-06 16:05:55 -07005403 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005404}
5405
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005406union mc_target {
5407 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005408 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005409};
5410
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005411enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005412 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005413 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005414 MC_TARGET_SWAP,
Jérôme Glissec733a822017-09-08 16:11:54 -07005415 MC_TARGET_DEVICE,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005416};
5417
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005418static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5419 unsigned long addr, pte_t ptent)
5420{
Christoph Hellwig25b29952019-06-13 22:50:49 +02005421 struct page *page = vm_normal_page(vma, addr, ptent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005422
5423 if (!page || !page_mapped(page))
5424 return NULL;
5425 if (PageAnon(page)) {
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005426 if (!(mc.flags & MOVE_ANON))
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005427 return NULL;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005428 } else {
5429 if (!(mc.flags & MOVE_FILE))
5430 return NULL;
5431 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005432 if (!get_page_unless_zero(page))
5433 return NULL;
5434
5435 return page;
5436}
5437
Jérôme Glissec733a822017-09-08 16:11:54 -07005438#if defined(CONFIG_SWAP) || defined(CONFIG_DEVICE_PRIVATE)
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005439static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
Li RongQing48406ef2016-07-26 15:22:14 -07005440 pte_t ptent, swp_entry_t *entry)
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005441{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005442 struct page *page = NULL;
5443 swp_entry_t ent = pte_to_swp_entry(ptent);
5444
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005445 if (!(mc.flags & MOVE_ANON) || non_swap_entry(ent))
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005446 return NULL;
Jérôme Glissec733a822017-09-08 16:11:54 -07005447
5448 /*
5449 * Handle MEMORY_DEVICE_PRIVATE which are ZONE_DEVICE page belonging to
5450 * a device and because they are not accessible by CPU they are store
5451 * as special swap entry in the CPU page table.
5452 */
5453 if (is_device_private_entry(ent)) {
5454 page = device_private_entry_to_page(ent);
5455 /*
5456 * MEMORY_DEVICE_PRIVATE means ZONE_DEVICE page and which have
5457 * a refcount of 1 when free (unlike normal page)
5458 */
5459 if (!page_ref_add_unless(page, 1, 1))
5460 return NULL;
5461 return page;
5462 }
5463
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005464 /*
5465 * Because lookup_swap_cache() updates some statistics counter,
5466 * we call find_get_page() with swapper_space directly.
5467 */
Huang Yingf6ab1f72016-10-07 17:00:21 -07005468 page = find_get_page(swap_address_space(ent), swp_offset(ent));
Johannes Weiner2d1c4982020-06-03 16:02:14 -07005469 entry->val = ent.val;
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005470
5471 return page;
5472}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005473#else
5474static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
Li RongQing48406ef2016-07-26 15:22:14 -07005475 pte_t ptent, swp_entry_t *entry)
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005476{
5477 return NULL;
5478}
5479#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005480
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005481static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5482 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5483{
5484 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005485 struct address_space *mapping;
5486 pgoff_t pgoff;
5487
5488 if (!vma->vm_file) /* anonymous vma */
5489 return NULL;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005490 if (!(mc.flags & MOVE_FILE))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005491 return NULL;
5492
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005493 mapping = vma->vm_file->f_mapping;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08005494 pgoff = linear_page_index(vma, addr);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005495
5496 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005497#ifdef CONFIG_SWAP
5498 /* shmem/tmpfs may report page out on swap: account for that too. */
Johannes Weiner139b6a62014-05-06 12:50:05 -07005499 if (shmem_mapping(mapping)) {
5500 page = find_get_entry(mapping, pgoff);
Matthew Wilcox3159f942017-11-03 13:30:42 -04005501 if (xa_is_value(page)) {
Johannes Weiner139b6a62014-05-06 12:50:05 -07005502 swp_entry_t swp = radix_to_swp_entry(page);
Johannes Weiner2d1c4982020-06-03 16:02:14 -07005503 *entry = swp;
Huang Yingf6ab1f72016-10-07 17:00:21 -07005504 page = find_get_page(swap_address_space(swp),
5505 swp_offset(swp));
Johannes Weiner139b6a62014-05-06 12:50:05 -07005506 }
5507 } else
5508 page = find_get_page(mapping, pgoff);
5509#else
5510 page = find_get_page(mapping, pgoff);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005511#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005512 return page;
5513}
5514
Chen Gangb1b0dea2015-04-14 15:47:35 -07005515/**
5516 * mem_cgroup_move_account - move account of the page
5517 * @page: the page
Li RongQing25843c22016-07-26 15:26:56 -07005518 * @compound: charge the page as compound or small page
Chen Gangb1b0dea2015-04-14 15:47:35 -07005519 * @from: mem_cgroup which the page is moved from.
5520 * @to: mem_cgroup which the page is moved to. @from != @to.
5521 *
Kirill A. Shutemov3ac808f2016-01-15 16:53:07 -08005522 * The caller must make sure the page is not on LRU (isolate_page() is useful.)
Chen Gangb1b0dea2015-04-14 15:47:35 -07005523 *
5524 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
5525 * from old cgroup.
5526 */
5527static int mem_cgroup_move_account(struct page *page,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005528 bool compound,
Chen Gangb1b0dea2015-04-14 15:47:35 -07005529 struct mem_cgroup *from,
5530 struct mem_cgroup *to)
5531{
Konstantin Khlebnikovae8af432019-10-18 20:20:11 -07005532 struct lruvec *from_vec, *to_vec;
5533 struct pglist_data *pgdat;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005534 unsigned int nr_pages = compound ? hpage_nr_pages(page) : 1;
Chen Gangb1b0dea2015-04-14 15:47:35 -07005535 int ret;
5536
5537 VM_BUG_ON(from == to);
5538 VM_BUG_ON_PAGE(PageLRU(page), page);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005539 VM_BUG_ON(compound && !PageTransHuge(page));
Chen Gangb1b0dea2015-04-14 15:47:35 -07005540
5541 /*
Johannes Weiner6a93ca82016-03-15 14:57:19 -07005542 * Prevent mem_cgroup_migrate() from looking at
Hugh Dickins45637ba2015-11-05 18:49:40 -08005543 * page->mem_cgroup of its source page while we change it.
Chen Gangb1b0dea2015-04-14 15:47:35 -07005544 */
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005545 ret = -EBUSY;
Chen Gangb1b0dea2015-04-14 15:47:35 -07005546 if (!trylock_page(page))
5547 goto out;
5548
5549 ret = -EINVAL;
5550 if (page->mem_cgroup != from)
5551 goto out_unlock;
5552
Konstantin Khlebnikovae8af432019-10-18 20:20:11 -07005553 pgdat = page_pgdat(page);
Johannes Weiner867e5e12019-11-30 17:55:34 -08005554 from_vec = mem_cgroup_lruvec(from, pgdat);
5555 to_vec = mem_cgroup_lruvec(to, pgdat);
Konstantin Khlebnikovae8af432019-10-18 20:20:11 -07005556
Johannes Weinerabb242f2020-06-03 16:01:28 -07005557 lock_page_memcg(page);
Chen Gangb1b0dea2015-04-14 15:47:35 -07005558
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07005559 if (PageAnon(page)) {
5560 if (page_mapped(page)) {
5561 __mod_lruvec_state(from_vec, NR_ANON_MAPPED, -nr_pages);
5562 __mod_lruvec_state(to_vec, NR_ANON_MAPPED, nr_pages);
Johannes Weiner468c3982020-06-03 16:02:01 -07005563 if (PageTransHuge(page)) {
5564 __mod_lruvec_state(from_vec, NR_ANON_THPS,
5565 -nr_pages);
5566 __mod_lruvec_state(to_vec, NR_ANON_THPS,
5567 nr_pages);
5568 }
5569
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07005570 }
5571 } else {
Johannes Weiner0d1c2072020-06-03 16:01:54 -07005572 __mod_lruvec_state(from_vec, NR_FILE_PAGES, -nr_pages);
5573 __mod_lruvec_state(to_vec, NR_FILE_PAGES, nr_pages);
5574
5575 if (PageSwapBacked(page)) {
5576 __mod_lruvec_state(from_vec, NR_SHMEM, -nr_pages);
5577 __mod_lruvec_state(to_vec, NR_SHMEM, nr_pages);
5578 }
5579
Johannes Weiner49e50d22020-06-03 16:01:47 -07005580 if (page_mapped(page)) {
5581 __mod_lruvec_state(from_vec, NR_FILE_MAPPED, -nr_pages);
5582 __mod_lruvec_state(to_vec, NR_FILE_MAPPED, nr_pages);
5583 }
Chen Gangb1b0dea2015-04-14 15:47:35 -07005584
Johannes Weiner49e50d22020-06-03 16:01:47 -07005585 if (PageDirty(page)) {
5586 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -04005587
Johannes Weiner49e50d22020-06-03 16:01:47 -07005588 if (mapping_cap_account_dirty(mapping)) {
5589 __mod_lruvec_state(from_vec, NR_FILE_DIRTY,
5590 -nr_pages);
5591 __mod_lruvec_state(to_vec, NR_FILE_DIRTY,
5592 nr_pages);
5593 }
Greg Thelenc4843a72015-05-22 17:13:16 -04005594 }
5595 }
5596
Chen Gangb1b0dea2015-04-14 15:47:35 -07005597 if (PageWriteback(page)) {
Konstantin Khlebnikovae8af432019-10-18 20:20:11 -07005598 __mod_lruvec_state(from_vec, NR_WRITEBACK, -nr_pages);
5599 __mod_lruvec_state(to_vec, NR_WRITEBACK, nr_pages);
Chen Gangb1b0dea2015-04-14 15:47:35 -07005600 }
5601
5602 /*
Johannes Weinerabb242f2020-06-03 16:01:28 -07005603 * All state has been migrated, let's switch to the new memcg.
5604 *
Chen Gangb1b0dea2015-04-14 15:47:35 -07005605 * It is safe to change page->mem_cgroup here because the page
Johannes Weinerabb242f2020-06-03 16:01:28 -07005606 * is referenced, charged, isolated, and locked: we can't race
5607 * with (un)charging, migration, LRU putback, or anything else
5608 * that would rely on a stable page->mem_cgroup.
5609 *
5610 * Note that lock_page_memcg is a memcg lock, not a page lock,
5611 * to save space. As soon as we switch page->mem_cgroup to a
5612 * new memcg that isn't locked, the above state can change
5613 * concurrently again. Make sure we're truly done with it.
Chen Gangb1b0dea2015-04-14 15:47:35 -07005614 */
Johannes Weinerabb242f2020-06-03 16:01:28 -07005615 smp_mb();
Chen Gangb1b0dea2015-04-14 15:47:35 -07005616
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07005617 css_get(&to->css);
5618 css_put(&from->css);
5619
5620 page->mem_cgroup = to;
Yang Shi87eaceb2019-09-23 15:38:15 -07005621
Johannes Weinerabb242f2020-06-03 16:01:28 -07005622 __unlock_page_memcg(from);
Chen Gangb1b0dea2015-04-14 15:47:35 -07005623
5624 ret = 0;
5625
5626 local_irq_disable();
Johannes Weiner3fba69a2020-06-03 16:01:31 -07005627 mem_cgroup_charge_statistics(to, page, nr_pages);
Chen Gangb1b0dea2015-04-14 15:47:35 -07005628 memcg_check_events(to, page);
Johannes Weiner3fba69a2020-06-03 16:01:31 -07005629 mem_cgroup_charge_statistics(from, page, -nr_pages);
Chen Gangb1b0dea2015-04-14 15:47:35 -07005630 memcg_check_events(from, page);
5631 local_irq_enable();
5632out_unlock:
5633 unlock_page(page);
5634out:
5635 return ret;
5636}
5637
Li RongQing7cf78062016-05-27 14:27:46 -07005638/**
5639 * get_mctgt_type - get target type of moving charge
5640 * @vma: the vma the pte to be checked belongs
5641 * @addr: the address corresponding to the pte to be checked
5642 * @ptent: the pte to be checked
5643 * @target: the pointer the target page or swap ent will be stored(can be NULL)
5644 *
5645 * Returns
5646 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5647 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5648 * move charge. if @target is not NULL, the page is stored in target->page
5649 * with extra refcnt got(Callers should handle it).
5650 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5651 * target for charge migration. if @target is not NULL, the entry is stored
5652 * in target->ent.
Christoph Hellwig25b29952019-06-13 22:50:49 +02005653 * 3(MC_TARGET_DEVICE): like MC_TARGET_PAGE but page is MEMORY_DEVICE_PRIVATE
5654 * (so ZONE_DEVICE page and thus not on the lru).
Jérôme Glissedf6ad692017-09-08 16:12:24 -07005655 * For now we such page is charge like a regular page would be as for all
5656 * intent and purposes it is just special memory taking the place of a
5657 * regular page.
Jérôme Glissec733a822017-09-08 16:11:54 -07005658 *
5659 * See Documentations/vm/hmm.txt and include/linux/hmm.h
Li RongQing7cf78062016-05-27 14:27:46 -07005660 *
5661 * Called with pte lock held.
5662 */
5663
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005664static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005665 unsigned long addr, pte_t ptent, union mc_target *target)
5666{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005667 struct page *page = NULL;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005668 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005669 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005670
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005671 if (pte_present(ptent))
5672 page = mc_handle_present_pte(vma, addr, ptent);
5673 else if (is_swap_pte(ptent))
Li RongQing48406ef2016-07-26 15:22:14 -07005674 page = mc_handle_swap_pte(vma, ptent, &ent);
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08005675 else if (pte_none(ptent))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005676 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005677
5678 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005679 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005680 if (page) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005681 /*
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005682 * Do only loose check w/o serialization.
Johannes Weiner1306a852014-12-10 15:44:52 -08005683 * mem_cgroup_move_account() checks the page is valid or
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005684 * not under LRU exclusion.
Daisuke Nishimura02491442010-03-10 15:22:17 -08005685 */
Johannes Weiner1306a852014-12-10 15:44:52 -08005686 if (page->mem_cgroup == mc.from) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005687 ret = MC_TARGET_PAGE;
Christoph Hellwig25b29952019-06-13 22:50:49 +02005688 if (is_device_private_page(page))
Jérôme Glissec733a822017-09-08 16:11:54 -07005689 ret = MC_TARGET_DEVICE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005690 if (target)
5691 target->page = page;
5692 }
5693 if (!ret || !target)
5694 put_page(page);
5695 }
Huang Ying3e14a572017-09-06 16:22:37 -07005696 /*
5697 * There is a swap entry and a page doesn't exist or isn't charged.
5698 * But we cannot move a tail-page in a THP.
5699 */
5700 if (ent.val && !ret && (!page || !PageTransCompound(page)) &&
Li Zefan34c00c32013-09-23 16:56:01 +08005701 mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005702 ret = MC_TARGET_SWAP;
5703 if (target)
5704 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005705 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005706 return ret;
5707}
5708
Naoya Horiguchi12724852012-03-21 16:34:28 -07005709#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5710/*
Huang Yingd6810d72017-09-06 16:22:45 -07005711 * We don't consider PMD mapped swapping or file mapped pages because THP does
5712 * not support them for now.
Naoya Horiguchi12724852012-03-21 16:34:28 -07005713 * Caller should make sure that pmd_trans_huge(pmd) is true.
5714 */
5715static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5716 unsigned long addr, pmd_t pmd, union mc_target *target)
5717{
5718 struct page *page = NULL;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005719 enum mc_target_type ret = MC_TARGET_NONE;
5720
Zi Yan84c3fc42017-09-08 16:11:01 -07005721 if (unlikely(is_swap_pmd(pmd))) {
5722 VM_BUG_ON(thp_migration_supported() &&
5723 !is_pmd_migration_entry(pmd));
5724 return ret;
5725 }
Naoya Horiguchi12724852012-03-21 16:34:28 -07005726 page = pmd_page(pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08005727 VM_BUG_ON_PAGE(!page || !PageHead(page), page);
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005728 if (!(mc.flags & MOVE_ANON))
Naoya Horiguchi12724852012-03-21 16:34:28 -07005729 return ret;
Johannes Weiner1306a852014-12-10 15:44:52 -08005730 if (page->mem_cgroup == mc.from) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005731 ret = MC_TARGET_PAGE;
5732 if (target) {
5733 get_page(page);
5734 target->page = page;
5735 }
5736 }
5737 return ret;
5738}
5739#else
5740static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5741 unsigned long addr, pmd_t pmd, union mc_target *target)
5742{
5743 return MC_TARGET_NONE;
5744}
5745#endif
5746
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005747static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5748 unsigned long addr, unsigned long end,
5749 struct mm_walk *walk)
5750{
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005751 struct vm_area_struct *vma = walk->vma;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005752 pte_t *pte;
5753 spinlock_t *ptl;
5754
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08005755 ptl = pmd_trans_huge_lock(pmd, vma);
5756 if (ptl) {
Jérôme Glissec733a822017-09-08 16:11:54 -07005757 /*
5758 * Note their can not be MC_TARGET_DEVICE for now as we do not
Christoph Hellwig25b29952019-06-13 22:50:49 +02005759 * support transparent huge page with MEMORY_DEVICE_PRIVATE but
5760 * this might change.
Jérôme Glissec733a822017-09-08 16:11:54 -07005761 */
Naoya Horiguchi12724852012-03-21 16:34:28 -07005762 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
5763 mc.precharge += HPAGE_PMD_NR;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005764 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005765 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005766 }
Dave Hansen03319322011-03-22 16:32:56 -07005767
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005768 if (pmd_trans_unstable(pmd))
5769 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005770 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5771 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005772 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005773 mc.precharge++; /* increment precharge temporarily */
5774 pte_unmap_unlock(pte - 1, ptl);
5775 cond_resched();
5776
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005777 return 0;
5778}
5779
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02005780static const struct mm_walk_ops precharge_walk_ops = {
5781 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5782};
5783
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005784static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5785{
5786 unsigned long precharge;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005787
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07005788 mmap_read_lock(mm);
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02005789 walk_page_range(mm, 0, mm->highest_vm_end, &precharge_walk_ops, NULL);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07005790 mmap_read_unlock(mm);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005791
5792 precharge = mc.precharge;
5793 mc.precharge = 0;
5794
5795 return precharge;
5796}
5797
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005798static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5799{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005800 unsigned long precharge = mem_cgroup_count_precharge(mm);
5801
5802 VM_BUG_ON(mc.moving_task);
5803 mc.moving_task = current;
5804 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005805}
5806
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005807/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5808static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005809{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005810 struct mem_cgroup *from = mc.from;
5811 struct mem_cgroup *to = mc.to;
5812
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005813 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005814 if (mc.precharge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005815 cancel_charge(mc.to, mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005816 mc.precharge = 0;
5817 }
5818 /*
5819 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5820 * we must uncharge here.
5821 */
5822 if (mc.moved_charge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005823 cancel_charge(mc.from, mc.moved_charge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005824 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005825 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005826 /* we must fixup refcnts and charges */
5827 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005828 /* uncharge swap account from the old cgroup */
Johannes Weinerce00a962014-09-05 08:43:57 -04005829 if (!mem_cgroup_is_root(mc.from))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005830 page_counter_uncharge(&mc.from->memsw, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005831
Vladimir Davydov615d66c2016-08-11 15:33:03 -07005832 mem_cgroup_id_put_many(mc.from, mc.moved_swap);
5833
Johannes Weiner05b84302014-08-06 16:05:59 -07005834 /*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005835 * we charged both to->memory and to->memsw, so we
5836 * should uncharge to->memory.
Johannes Weiner05b84302014-08-06 16:05:59 -07005837 */
Johannes Weinerce00a962014-09-05 08:43:57 -04005838 if (!mem_cgroup_is_root(mc.to))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005839 page_counter_uncharge(&mc.to->memory, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005840
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005841 mc.moved_swap = 0;
5842 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005843 memcg_oom_recover(from);
5844 memcg_oom_recover(to);
5845 wake_up_all(&mc.waitq);
5846}
5847
5848static void mem_cgroup_clear_mc(void)
5849{
Tejun Heo264a0ae2016-04-21 19:09:02 -04005850 struct mm_struct *mm = mc.mm;
5851
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005852 /*
5853 * we must clear moving_task before waking up waiters at the end of
5854 * task migration.
5855 */
5856 mc.moving_task = NULL;
5857 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005858 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005859 mc.from = NULL;
5860 mc.to = NULL;
Tejun Heo264a0ae2016-04-21 19:09:02 -04005861 mc.mm = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005862 spin_unlock(&mc.lock);
Tejun Heo264a0ae2016-04-21 19:09:02 -04005863
5864 mmput(mm);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005865}
5866
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005867static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005868{
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005869 struct cgroup_subsys_state *css;
Ross Zwislereed67d72015-12-23 14:53:27 -07005870 struct mem_cgroup *memcg = NULL; /* unneeded init to make gcc happy */
Tejun Heo9f2115f2015-09-08 15:01:10 -07005871 struct mem_cgroup *from;
Tejun Heo4530edd2015-09-11 15:00:19 -04005872 struct task_struct *leader, *p;
Tejun Heo9f2115f2015-09-08 15:01:10 -07005873 struct mm_struct *mm;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005874 unsigned long move_flags;
Tejun Heo9f2115f2015-09-08 15:01:10 -07005875 int ret = 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005876
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005877 /* charge immigration isn't supported on the default hierarchy */
5878 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
Tejun Heo9f2115f2015-09-08 15:01:10 -07005879 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005880
Tejun Heo4530edd2015-09-11 15:00:19 -04005881 /*
5882 * Multi-process migrations only happen on the default hierarchy
5883 * where charge immigration is not used. Perform charge
5884 * immigration if @tset contains a leader and whine if there are
5885 * multiple.
5886 */
5887 p = NULL;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005888 cgroup_taskset_for_each_leader(leader, css, tset) {
Tejun Heo4530edd2015-09-11 15:00:19 -04005889 WARN_ON_ONCE(p);
5890 p = leader;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005891 memcg = mem_cgroup_from_css(css);
Tejun Heo4530edd2015-09-11 15:00:19 -04005892 }
5893 if (!p)
5894 return 0;
5895
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005896 /*
5897 * We are now commited to this value whatever it is. Changes in this
5898 * tunable will only affect upcoming migrations, not the current one.
5899 * So we need to save it, and keep it going.
5900 */
5901 move_flags = READ_ONCE(memcg->move_charge_at_immigrate);
5902 if (!move_flags)
5903 return 0;
5904
Tejun Heo9f2115f2015-09-08 15:01:10 -07005905 from = mem_cgroup_from_task(p);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005906
Tejun Heo9f2115f2015-09-08 15:01:10 -07005907 VM_BUG_ON(from == memcg);
Johannes Weiner247b1442014-12-10 15:44:11 -08005908
Tejun Heo9f2115f2015-09-08 15:01:10 -07005909 mm = get_task_mm(p);
5910 if (!mm)
5911 return 0;
5912 /* We move charges only when we move a owner of the mm */
5913 if (mm->owner == p) {
5914 VM_BUG_ON(mc.from);
5915 VM_BUG_ON(mc.to);
5916 VM_BUG_ON(mc.precharge);
5917 VM_BUG_ON(mc.moved_charge);
5918 VM_BUG_ON(mc.moved_swap);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005919
Tejun Heo9f2115f2015-09-08 15:01:10 -07005920 spin_lock(&mc.lock);
Tejun Heo264a0ae2016-04-21 19:09:02 -04005921 mc.mm = mm;
Tejun Heo9f2115f2015-09-08 15:01:10 -07005922 mc.from = from;
5923 mc.to = memcg;
5924 mc.flags = move_flags;
5925 spin_unlock(&mc.lock);
5926 /* We set mc.moving_task later */
5927
5928 ret = mem_cgroup_precharge_mc(mm);
5929 if (ret)
5930 mem_cgroup_clear_mc();
Tejun Heo264a0ae2016-04-21 19:09:02 -04005931 } else {
5932 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005933 }
5934 return ret;
5935}
5936
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005937static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005938{
Johannes Weiner4e2f2452014-12-10 15:44:08 -08005939 if (mc.to)
5940 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005941}
5942
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005943static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5944 unsigned long addr, unsigned long end,
5945 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005946{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005947 int ret = 0;
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005948 struct vm_area_struct *vma = walk->vma;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005949 pte_t *pte;
5950 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005951 enum mc_target_type target_type;
5952 union mc_target target;
5953 struct page *page;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005954
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08005955 ptl = pmd_trans_huge_lock(pmd, vma);
5956 if (ptl) {
Hugh Dickins62ade862012-05-18 11:28:34 -07005957 if (mc.precharge < HPAGE_PMD_NR) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005958 spin_unlock(ptl);
Naoya Horiguchi12724852012-03-21 16:34:28 -07005959 return 0;
5960 }
5961 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
5962 if (target_type == MC_TARGET_PAGE) {
5963 page = target.page;
5964 if (!isolate_lru_page(page)) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005965 if (!mem_cgroup_move_account(page, true,
Johannes Weiner1306a852014-12-10 15:44:52 -08005966 mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005967 mc.precharge -= HPAGE_PMD_NR;
5968 mc.moved_charge += HPAGE_PMD_NR;
5969 }
5970 putback_lru_page(page);
5971 }
5972 put_page(page);
Jérôme Glissec733a822017-09-08 16:11:54 -07005973 } else if (target_type == MC_TARGET_DEVICE) {
5974 page = target.page;
5975 if (!mem_cgroup_move_account(page, true,
5976 mc.from, mc.to)) {
5977 mc.precharge -= HPAGE_PMD_NR;
5978 mc.moved_charge += HPAGE_PMD_NR;
5979 }
5980 put_page(page);
Naoya Horiguchi12724852012-03-21 16:34:28 -07005981 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005982 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005983 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005984 }
5985
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005986 if (pmd_trans_unstable(pmd))
5987 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005988retry:
5989 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5990 for (; addr != end; addr += PAGE_SIZE) {
5991 pte_t ptent = *(pte++);
Jérôme Glissec733a822017-09-08 16:11:54 -07005992 bool device = false;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005993 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005994
5995 if (!mc.precharge)
5996 break;
5997
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005998 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Jérôme Glissec733a822017-09-08 16:11:54 -07005999 case MC_TARGET_DEVICE:
6000 device = true;
Joe Perchese4a9bc52020-04-06 20:08:39 -07006001 fallthrough;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006002 case MC_TARGET_PAGE:
6003 page = target.page;
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08006004 /*
6005 * We can have a part of the split pmd here. Moving it
6006 * can be done but it would be too convoluted so simply
6007 * ignore such a partial THP and keep it in original
6008 * memcg. There should be somebody mapping the head.
6009 */
6010 if (PageTransCompound(page))
6011 goto put;
Jérôme Glissec733a822017-09-08 16:11:54 -07006012 if (!device && isolate_lru_page(page))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006013 goto put;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08006014 if (!mem_cgroup_move_account(page, false,
6015 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006016 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006017 /* we uncharge from mc.from later. */
6018 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006019 }
Jérôme Glissec733a822017-09-08 16:11:54 -07006020 if (!device)
6021 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006022put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006023 put_page(page);
6024 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006025 case MC_TARGET_SWAP:
6026 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006027 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006028 mc.precharge--;
Hugh Dickins8d22a932020-07-23 21:15:24 -07006029 mem_cgroup_id_get_many(mc.to, 1);
6030 /* we fixup other refcnts and charges later. */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006031 mc.moved_swap++;
6032 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006033 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006034 default:
6035 break;
6036 }
6037 }
6038 pte_unmap_unlock(pte - 1, ptl);
6039 cond_resched();
6040
6041 if (addr != end) {
6042 /*
6043 * We have consumed all precharges we got in can_attach().
6044 * We try charge one by one, but don't do any additional
6045 * charges to mc.to if we have failed in charge once in attach()
6046 * phase.
6047 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006048 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006049 if (!ret)
6050 goto retry;
6051 }
6052
6053 return ret;
6054}
6055
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02006056static const struct mm_walk_ops charge_walk_ops = {
6057 .pmd_entry = mem_cgroup_move_charge_pte_range,
6058};
6059
Tejun Heo264a0ae2016-04-21 19:09:02 -04006060static void mem_cgroup_move_charge(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006061{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006062 lru_add_drain_all();
Johannes Weiner312722c2014-12-10 15:44:25 -08006063 /*
Johannes Weiner81f8c3a2016-03-15 14:57:04 -07006064 * Signal lock_page_memcg() to take the memcg's move_lock
6065 * while we're moving its pages to another memcg. Then wait
6066 * for already started RCU-only updates to finish.
Johannes Weiner312722c2014-12-10 15:44:25 -08006067 */
6068 atomic_inc(&mc.from->moving_account);
6069 synchronize_rcu();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006070retry:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07006071 if (unlikely(!mmap_read_trylock(mc.mm))) {
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006072 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07006073 * Someone who are holding the mmap_lock might be waiting in
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006074 * waitq. So we cancel all extra charges, wake up all waiters,
6075 * and retry. Because we cancel precharges, we might not be able
6076 * to move enough charges, but moving charge is a best-effort
6077 * feature anyway, so it wouldn't be a big problem.
6078 */
6079 __mem_cgroup_clear_mc();
6080 cond_resched();
6081 goto retry;
6082 }
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08006083 /*
6084 * When we have consumed all precharges and failed in doing
6085 * additional charge, the page walk just aborts.
6086 */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02006087 walk_page_range(mc.mm, 0, mc.mm->highest_vm_end, &charge_walk_ops,
6088 NULL);
James Morse0247f3f2016-10-07 17:00:12 -07006089
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07006090 mmap_read_unlock(mc.mm);
Johannes Weiner312722c2014-12-10 15:44:25 -08006091 atomic_dec(&mc.from->moving_account);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006092}
6093
Tejun Heo264a0ae2016-04-21 19:09:02 -04006094static void mem_cgroup_move_task(void)
Balbir Singh67e465a2008-02-07 00:13:54 -08006095{
Tejun Heo264a0ae2016-04-21 19:09:02 -04006096 if (mc.to) {
6097 mem_cgroup_move_charge();
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006098 mem_cgroup_clear_mc();
Tejun Heo264a0ae2016-04-21 19:09:02 -04006099 }
Balbir Singh67e465a2008-02-07 00:13:54 -08006100}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006101#else /* !CONFIG_MMU */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05006102static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006103{
6104 return 0;
6105}
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05006106static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006107{
6108}
Tejun Heo264a0ae2016-04-21 19:09:02 -04006109static void mem_cgroup_move_task(void)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006110{
6111}
6112#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006113
Tejun Heof00baae2013-04-15 13:41:15 -07006114/*
6115 * Cgroup retains root cgroups across [un]mount cycles making it necessary
Tejun Heoaa6ec292014-07-09 10:08:08 -04006116 * to verify whether we're attached to the default hierarchy on each mount
6117 * attempt.
Tejun Heof00baae2013-04-15 13:41:15 -07006118 */
Tejun Heoeb954192013-08-08 20:11:23 -04006119static void mem_cgroup_bind(struct cgroup_subsys_state *root_css)
Tejun Heof00baae2013-04-15 13:41:15 -07006120{
6121 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -04006122 * use_hierarchy is forced on the default hierarchy. cgroup core
Tejun Heof00baae2013-04-15 13:41:15 -07006123 * guarantees that @root doesn't have any children, so turning it
6124 * on for the root memcg is enough.
6125 */
Tejun Heo9e10a132015-09-18 11:56:28 -04006126 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
Vladimir Davydov7feee5902015-03-12 16:26:19 -07006127 root_mem_cgroup->use_hierarchy = true;
6128 else
6129 root_mem_cgroup->use_hierarchy = false;
Tejun Heof00baae2013-04-15 13:41:15 -07006130}
6131
Chris Down677dc972019-03-05 15:45:55 -08006132static int seq_puts_memcg_tunable(struct seq_file *m, unsigned long value)
6133{
6134 if (value == PAGE_COUNTER_MAX)
6135 seq_puts(m, "max\n");
6136 else
6137 seq_printf(m, "%llu\n", (u64)value * PAGE_SIZE);
6138
6139 return 0;
6140}
6141
Johannes Weiner241994ed2015-02-11 15:26:06 -08006142static u64 memory_current_read(struct cgroup_subsys_state *css,
6143 struct cftype *cft)
6144{
Johannes Weinerf5fc3c5d2015-11-05 18:50:23 -08006145 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
6146
6147 return (u64)page_counter_read(&memcg->memory) * PAGE_SIZE;
Johannes Weiner241994ed2015-02-11 15:26:06 -08006148}
6149
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006150static int memory_min_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.min));
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006154}
6155
6156static ssize_t memory_min_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 min;
6161 int err;
6162
6163 buf = strstrip(buf);
6164 err = page_counter_memparse(buf, "max", &min);
6165 if (err)
6166 return err;
6167
6168 page_counter_set_min(&memcg->memory, min);
6169
6170 return nbytes;
6171}
6172
Johannes Weiner241994ed2015-02-11 15:26:06 -08006173static int memory_low_show(struct seq_file *m, void *v)
6174{
Chris Down677dc972019-03-05 15:45:55 -08006175 return seq_puts_memcg_tunable(m,
6176 READ_ONCE(mem_cgroup_from_seq(m)->memory.low));
Johannes Weiner241994ed2015-02-11 15:26:06 -08006177}
6178
6179static ssize_t memory_low_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));
6183 unsigned long low;
6184 int err;
6185
6186 buf = strstrip(buf);
Johannes Weinerd2973692015-02-27 15:52:04 -08006187 err = page_counter_memparse(buf, "max", &low);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006188 if (err)
6189 return err;
6190
Roman Gushchin23067152018-06-07 17:06:22 -07006191 page_counter_set_low(&memcg->memory, low);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006192
6193 return nbytes;
6194}
6195
6196static int memory_high_show(struct seq_file *m, void *v)
6197{
Jakub Kicinskid1663a92020-06-01 21:49:49 -07006198 return seq_puts_memcg_tunable(m,
6199 READ_ONCE(mem_cgroup_from_seq(m)->memory.high));
Johannes Weiner241994ed2015-02-11 15:26:06 -08006200}
6201
6202static ssize_t memory_high_write(struct kernfs_open_file *of,
6203 char *buf, size_t nbytes, loff_t off)
6204{
6205 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner8c8c3832019-11-30 17:50:09 -08006206 unsigned int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
6207 bool drained = false;
Johannes Weiner241994ed2015-02-11 15:26:06 -08006208 unsigned long high;
6209 int err;
6210
6211 buf = strstrip(buf);
Johannes Weinerd2973692015-02-27 15:52:04 -08006212 err = page_counter_memparse(buf, "max", &high);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006213 if (err)
6214 return err;
6215
Jakub Kicinskid1663a92020-06-01 21:49:49 -07006216 page_counter_set_high(&memcg->memory, high);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006217
Johannes Weiner8c8c3832019-11-30 17:50:09 -08006218 for (;;) {
6219 unsigned long nr_pages = page_counter_read(&memcg->memory);
6220 unsigned long reclaimed;
Johannes Weiner588083b2016-03-17 14:20:25 -07006221
Johannes Weiner8c8c3832019-11-30 17:50:09 -08006222 if (nr_pages <= high)
6223 break;
6224
6225 if (signal_pending(current))
6226 break;
6227
6228 if (!drained) {
6229 drain_all_stock(memcg);
6230 drained = true;
6231 continue;
6232 }
6233
6234 reclaimed = try_to_free_mem_cgroup_pages(memcg, nr_pages - high,
6235 GFP_KERNEL, true);
6236
6237 if (!reclaimed && !nr_retries--)
6238 break;
6239 }
6240
Johannes Weiner241994ed2015-02-11 15:26:06 -08006241 return nbytes;
6242}
6243
6244static int memory_max_show(struct seq_file *m, void *v)
6245{
Chris Down677dc972019-03-05 15:45:55 -08006246 return seq_puts_memcg_tunable(m,
6247 READ_ONCE(mem_cgroup_from_seq(m)->memory.max));
Johannes Weiner241994ed2015-02-11 15:26:06 -08006248}
6249
6250static ssize_t memory_max_write(struct kernfs_open_file *of,
6251 char *buf, size_t nbytes, loff_t off)
6252{
6253 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07006254 unsigned int nr_reclaims = MEM_CGROUP_RECLAIM_RETRIES;
6255 bool drained = false;
Johannes Weiner241994ed2015-02-11 15:26:06 -08006256 unsigned long max;
6257 int err;
6258
6259 buf = strstrip(buf);
Johannes Weinerd2973692015-02-27 15:52:04 -08006260 err = page_counter_memparse(buf, "max", &max);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006261 if (err)
6262 return err;
6263
Roman Gushchinbbec2e12018-06-07 17:06:18 -07006264 xchg(&memcg->memory.max, max);
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07006265
6266 for (;;) {
6267 unsigned long nr_pages = page_counter_read(&memcg->memory);
6268
6269 if (nr_pages <= max)
6270 break;
6271
Johannes Weiner7249c9f2019-11-30 17:50:06 -08006272 if (signal_pending(current))
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07006273 break;
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07006274
6275 if (!drained) {
6276 drain_all_stock(memcg);
6277 drained = true;
6278 continue;
6279 }
6280
6281 if (nr_reclaims) {
6282 if (!try_to_free_mem_cgroup_pages(memcg, nr_pages - max,
6283 GFP_KERNEL, true))
6284 nr_reclaims--;
6285 continue;
6286 }
6287
Johannes Weinere27be242018-04-10 16:29:45 -07006288 memcg_memory_event(memcg, MEMCG_OOM);
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07006289 if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0))
6290 break;
6291 }
Johannes Weiner241994ed2015-02-11 15:26:06 -08006292
Tejun Heo2529bb32015-05-22 18:23:34 -04006293 memcg_wb_domain_size_changed(memcg);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006294 return nbytes;
6295}
6296
Shakeel Butt1e577f92019-07-11 20:55:55 -07006297static void __memory_events_show(struct seq_file *m, atomic_long_t *events)
6298{
6299 seq_printf(m, "low %lu\n", atomic_long_read(&events[MEMCG_LOW]));
6300 seq_printf(m, "high %lu\n", atomic_long_read(&events[MEMCG_HIGH]));
6301 seq_printf(m, "max %lu\n", atomic_long_read(&events[MEMCG_MAX]));
6302 seq_printf(m, "oom %lu\n", atomic_long_read(&events[MEMCG_OOM]));
6303 seq_printf(m, "oom_kill %lu\n",
6304 atomic_long_read(&events[MEMCG_OOM_KILL]));
6305}
6306
Johannes Weiner241994ed2015-02-11 15:26:06 -08006307static int memory_events_show(struct seq_file *m, void *v)
6308{
Chris Downaa9694b2019-03-05 15:45:52 -08006309 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006310
Shakeel Butt1e577f92019-07-11 20:55:55 -07006311 __memory_events_show(m, memcg->memory_events);
6312 return 0;
6313}
Johannes Weiner241994ed2015-02-11 15:26:06 -08006314
Shakeel Butt1e577f92019-07-11 20:55:55 -07006315static int memory_events_local_show(struct seq_file *m, void *v)
6316{
6317 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
6318
6319 __memory_events_show(m, memcg->memory_events_local);
Johannes Weiner241994ed2015-02-11 15:26:06 -08006320 return 0;
6321}
6322
Johannes Weiner587d9f72016-01-20 15:03:19 -08006323static int memory_stat_show(struct seq_file *m, void *v)
6324{
Chris Downaa9694b2019-03-05 15:45:52 -08006325 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Johannes Weinerc8713d02019-07-11 20:55:59 -07006326 char *buf;
Johannes Weiner587d9f72016-01-20 15:03:19 -08006327
Johannes Weinerc8713d02019-07-11 20:55:59 -07006328 buf = memory_stat_format(memcg);
6329 if (!buf)
6330 return -ENOMEM;
6331 seq_puts(m, buf);
6332 kfree(buf);
Johannes Weiner587d9f72016-01-20 15:03:19 -08006333 return 0;
6334}
6335
Roman Gushchin3d8b38e2018-08-21 21:53:54 -07006336static int memory_oom_group_show(struct seq_file *m, void *v)
6337{
Chris Downaa9694b2019-03-05 15:45:52 -08006338 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Roman Gushchin3d8b38e2018-08-21 21:53:54 -07006339
6340 seq_printf(m, "%d\n", memcg->oom_group);
6341
6342 return 0;
6343}
6344
6345static ssize_t memory_oom_group_write(struct kernfs_open_file *of,
6346 char *buf, size_t nbytes, loff_t off)
6347{
6348 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6349 int ret, oom_group;
6350
6351 buf = strstrip(buf);
6352 if (!buf)
6353 return -EINVAL;
6354
6355 ret = kstrtoint(buf, 0, &oom_group);
6356 if (ret)
6357 return ret;
6358
6359 if (oom_group != 0 && oom_group != 1)
6360 return -EINVAL;
6361
6362 memcg->oom_group = oom_group;
6363
6364 return nbytes;
6365}
6366
Johannes Weiner241994ed2015-02-11 15:26:06 -08006367static struct cftype memory_files[] = {
6368 {
6369 .name = "current",
Johannes Weinerf5fc3c5d2015-11-05 18:50:23 -08006370 .flags = CFTYPE_NOT_ON_ROOT,
Johannes Weiner241994ed2015-02-11 15:26:06 -08006371 .read_u64 = memory_current_read,
6372 },
6373 {
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006374 .name = "min",
6375 .flags = CFTYPE_NOT_ON_ROOT,
6376 .seq_show = memory_min_show,
6377 .write = memory_min_write,
6378 },
6379 {
Johannes Weiner241994ed2015-02-11 15:26:06 -08006380 .name = "low",
6381 .flags = CFTYPE_NOT_ON_ROOT,
6382 .seq_show = memory_low_show,
6383 .write = memory_low_write,
6384 },
6385 {
6386 .name = "high",
6387 .flags = CFTYPE_NOT_ON_ROOT,
6388 .seq_show = memory_high_show,
6389 .write = memory_high_write,
6390 },
6391 {
6392 .name = "max",
6393 .flags = CFTYPE_NOT_ON_ROOT,
6394 .seq_show = memory_max_show,
6395 .write = memory_max_write,
6396 },
6397 {
6398 .name = "events",
6399 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo472912a2015-09-18 18:01:59 -04006400 .file_offset = offsetof(struct mem_cgroup, events_file),
Johannes Weiner241994ed2015-02-11 15:26:06 -08006401 .seq_show = memory_events_show,
6402 },
Johannes Weiner587d9f72016-01-20 15:03:19 -08006403 {
Shakeel Butt1e577f92019-07-11 20:55:55 -07006404 .name = "events.local",
6405 .flags = CFTYPE_NOT_ON_ROOT,
6406 .file_offset = offsetof(struct mem_cgroup, events_local_file),
6407 .seq_show = memory_events_local_show,
6408 },
6409 {
Johannes Weiner587d9f72016-01-20 15:03:19 -08006410 .name = "stat",
Johannes Weiner587d9f72016-01-20 15:03:19 -08006411 .seq_show = memory_stat_show,
6412 },
Roman Gushchin3d8b38e2018-08-21 21:53:54 -07006413 {
6414 .name = "oom.group",
6415 .flags = CFTYPE_NOT_ON_ROOT | CFTYPE_NS_DELEGATABLE,
6416 .seq_show = memory_oom_group_show,
6417 .write = memory_oom_group_write,
6418 },
Johannes Weiner241994ed2015-02-11 15:26:06 -08006419 { } /* terminate */
6420};
6421
Tejun Heo073219e2014-02-08 10:36:58 -05006422struct cgroup_subsys memory_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08006423 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08006424 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08006425 .css_offline = mem_cgroup_css_offline,
Vladimir Davydov6df38682015-12-29 14:54:10 -08006426 .css_released = mem_cgroup_css_released,
Tejun Heo92fb9742012-11-19 08:13:38 -08006427 .css_free = mem_cgroup_css_free,
Tejun Heo1ced9532014-07-08 18:02:57 -04006428 .css_reset = mem_cgroup_css_reset,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006429 .can_attach = mem_cgroup_can_attach,
6430 .cancel_attach = mem_cgroup_cancel_attach,
Tejun Heo264a0ae2016-04-21 19:09:02 -04006431 .post_attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07006432 .bind = mem_cgroup_bind,
Johannes Weiner241994ed2015-02-11 15:26:06 -08006433 .dfl_cftypes = memory_files,
6434 .legacy_cftypes = mem_cgroup_legacy_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006435 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006436};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006437
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006438/*
6439 * This function calculates an individual cgroup's effective
6440 * protection which is derived from its own memory.min/low, its
6441 * parent's and siblings' settings, as well as the actual memory
6442 * distribution in the tree.
6443 *
6444 * The following rules apply to the effective protection values:
6445 *
6446 * 1. At the first level of reclaim, effective protection is equal to
6447 * the declared protection in memory.min and memory.low.
6448 *
6449 * 2. To enable safe delegation of the protection configuration, at
6450 * subsequent levels the effective protection is capped to the
6451 * parent's effective protection.
6452 *
6453 * 3. To make complex and dynamic subtrees easier to configure, the
6454 * user is allowed to overcommit the declared protection at a given
6455 * level. If that is the case, the parent's effective protection is
6456 * distributed to the children in proportion to how much protection
6457 * they have declared and how much of it they are utilizing.
6458 *
6459 * This makes distribution proportional, but also work-conserving:
6460 * if one cgroup claims much more protection than it uses memory,
6461 * the unused remainder is available to its siblings.
6462 *
6463 * 4. Conversely, when the declared protection is undercommitted at a
6464 * given level, the distribution of the larger parental protection
6465 * budget is NOT proportional. A cgroup's protection from a sibling
6466 * is capped to its own memory.min/low setting.
6467 *
Johannes Weiner8a931f82020-04-01 21:07:07 -07006468 * 5. However, to allow protecting recursive subtrees from each other
6469 * without having to declare each individual cgroup's fixed share
6470 * of the ancestor's claim to protection, any unutilized -
6471 * "floating" - protection from up the tree is distributed in
6472 * proportion to each cgroup's *usage*. This makes the protection
6473 * neutral wrt sibling cgroups and lets them compete freely over
6474 * the shared parental protection budget, but it protects the
6475 * subtree as a whole from neighboring subtrees.
6476 *
6477 * Note that 4. and 5. are not in conflict: 4. is about protecting
6478 * against immediate siblings whereas 5. is about protecting against
6479 * neighboring subtrees.
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006480 */
6481static unsigned long effective_protection(unsigned long usage,
Johannes Weiner8a931f82020-04-01 21:07:07 -07006482 unsigned long parent_usage,
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006483 unsigned long setting,
6484 unsigned long parent_effective,
6485 unsigned long siblings_protected)
6486{
6487 unsigned long protected;
Johannes Weiner8a931f82020-04-01 21:07:07 -07006488 unsigned long ep;
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006489
6490 protected = min(usage, setting);
6491 /*
6492 * If all cgroups at this level combined claim and use more
6493 * protection then what the parent affords them, distribute
6494 * shares in proportion to utilization.
6495 *
6496 * We are using actual utilization rather than the statically
6497 * claimed protection in order to be work-conserving: claimed
6498 * but unused protection is available to siblings that would
6499 * otherwise get a smaller chunk than what they claimed.
6500 */
6501 if (siblings_protected > parent_effective)
6502 return protected * parent_effective / siblings_protected;
6503
6504 /*
6505 * Ok, utilized protection of all children is within what the
6506 * parent affords them, so we know whatever this child claims
6507 * and utilizes is effectively protected.
6508 *
6509 * If there is unprotected usage beyond this value, reclaim
6510 * will apply pressure in proportion to that amount.
6511 *
6512 * If there is unutilized protection, the cgroup will be fully
6513 * shielded from reclaim, but we do return a smaller value for
6514 * protection than what the group could enjoy in theory. This
6515 * is okay. With the overcommit distribution above, effective
6516 * protection is always dependent on how memory is actually
6517 * consumed among the siblings anyway.
6518 */
Johannes Weiner8a931f82020-04-01 21:07:07 -07006519 ep = protected;
6520
6521 /*
6522 * If the children aren't claiming (all of) the protection
6523 * afforded to them by the parent, distribute the remainder in
6524 * proportion to the (unprotected) memory of each cgroup. That
6525 * way, cgroups that aren't explicitly prioritized wrt each
6526 * other compete freely over the allowance, but they are
6527 * collectively protected from neighboring trees.
6528 *
6529 * We're using unprotected memory for the weight so that if
6530 * some cgroups DO claim explicit protection, we don't protect
6531 * the same bytes twice.
Johannes Weinercd324ed2020-06-25 20:30:16 -07006532 *
6533 * Check both usage and parent_usage against the respective
6534 * protected values. One should imply the other, but they
6535 * aren't read atomically - make sure the division is sane.
Johannes Weiner8a931f82020-04-01 21:07:07 -07006536 */
6537 if (!(cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT))
6538 return ep;
Johannes Weinercd324ed2020-06-25 20:30:16 -07006539 if (parent_effective > siblings_protected &&
6540 parent_usage > siblings_protected &&
6541 usage > protected) {
Johannes Weiner8a931f82020-04-01 21:07:07 -07006542 unsigned long unclaimed;
6543
6544 unclaimed = parent_effective - siblings_protected;
6545 unclaimed *= usage - protected;
6546 unclaimed /= parent_usage - siblings_protected;
6547
6548 ep += unclaimed;
6549 }
6550
6551 return ep;
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006552}
6553
Johannes Weiner241994ed2015-02-11 15:26:06 -08006554/**
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006555 * mem_cgroup_protected - check if memory consumption is in the normal range
Sean Christopherson34c81052017-07-10 15:48:05 -07006556 * @root: the top ancestor of the sub-tree being checked
Johannes Weiner241994ed2015-02-11 15:26:06 -08006557 * @memcg: the memory cgroup to check
6558 *
Roman Gushchin23067152018-06-07 17:06:22 -07006559 * WARNING: This function is not stateless! It can only be used as part
6560 * of a top-down tree iteration, not for isolated queries.
Sean Christopherson34c81052017-07-10 15:48:05 -07006561 *
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006562 * Returns one of the following:
6563 * MEMCG_PROT_NONE: cgroup memory is not protected
6564 * MEMCG_PROT_LOW: cgroup memory is protected as long there is
6565 * an unprotected supply of reclaimable memory from other cgroups.
6566 * MEMCG_PROT_MIN: cgroup memory is protected
Johannes Weiner241994ed2015-02-11 15:26:06 -08006567 */
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006568enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root,
6569 struct mem_cgroup *memcg)
Johannes Weiner241994ed2015-02-11 15:26:06 -08006570{
Johannes Weiner8a931f82020-04-01 21:07:07 -07006571 unsigned long usage, parent_usage;
Roman Gushchin23067152018-06-07 17:06:22 -07006572 struct mem_cgroup *parent;
6573
Johannes Weiner241994ed2015-02-11 15:26:06 -08006574 if (mem_cgroup_disabled())
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006575 return MEMCG_PROT_NONE;
Johannes Weiner241994ed2015-02-11 15:26:06 -08006576
Sean Christopherson34c81052017-07-10 15:48:05 -07006577 if (!root)
6578 root = root_mem_cgroup;
6579 if (memcg == root)
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006580 return MEMCG_PROT_NONE;
Johannes Weiner241994ed2015-02-11 15:26:06 -08006581
Roman Gushchin23067152018-06-07 17:06:22 -07006582 usage = page_counter_read(&memcg->memory);
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006583 if (!usage)
6584 return MEMCG_PROT_NONE;
Sean Christopherson34c81052017-07-10 15:48:05 -07006585
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006586 parent = parent_mem_cgroup(memcg);
Roman Gushchindf2a4192018-06-14 15:26:17 -07006587 /* No parent means a non-hierarchical mode on v1 memcg */
6588 if (!parent)
6589 return MEMCG_PROT_NONE;
6590
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006591 if (parent == root) {
Chris Downc3d53202020-04-01 21:07:27 -07006592 memcg->memory.emin = READ_ONCE(memcg->memory.min);
Chris Down03960e32020-06-25 20:30:22 -07006593 memcg->memory.elow = READ_ONCE(memcg->memory.low);
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006594 goto out;
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006595 }
6596
Johannes Weiner8a931f82020-04-01 21:07:07 -07006597 parent_usage = page_counter_read(&parent->memory);
6598
Chris Downb3a78222020-04-01 21:07:33 -07006599 WRITE_ONCE(memcg->memory.emin, effective_protection(usage, parent_usage,
Chris Downc3d53202020-04-01 21:07:27 -07006600 READ_ONCE(memcg->memory.min),
6601 READ_ONCE(parent->memory.emin),
Chris Downb3a78222020-04-01 21:07:33 -07006602 atomic_long_read(&parent->memory.children_min_usage)));
Roman Gushchin23067152018-06-07 17:06:22 -07006603
Chris Downb3a78222020-04-01 21:07:33 -07006604 WRITE_ONCE(memcg->memory.elow, effective_protection(usage, parent_usage,
Chris Down03960e32020-06-25 20:30:22 -07006605 READ_ONCE(memcg->memory.low),
6606 READ_ONCE(parent->memory.elow),
Chris Downb3a78222020-04-01 21:07:33 -07006607 atomic_long_read(&parent->memory.children_low_usage)));
Roman Gushchin23067152018-06-07 17:06:22 -07006608
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006609out:
6610 if (usage <= memcg->memory.emin)
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006611 return MEMCG_PROT_MIN;
Johannes Weinerbc50bcc2020-04-01 21:07:03 -07006612 else if (usage <= memcg->memory.elow)
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006613 return MEMCG_PROT_LOW;
6614 else
6615 return MEMCG_PROT_NONE;
Johannes Weiner241994ed2015-02-11 15:26:06 -08006616}
6617
Johannes Weiner00501b52014-08-08 14:19:20 -07006618/**
Johannes Weinerf0e45fb2020-06-03 16:02:07 -07006619 * mem_cgroup_charge - charge a newly allocated page to a cgroup
Johannes Weiner00501b52014-08-08 14:19:20 -07006620 * @page: page to charge
6621 * @mm: mm context of the victim
6622 * @gfp_mask: reclaim mode
Johannes Weiner00501b52014-08-08 14:19:20 -07006623 *
6624 * Try to charge @page to the memcg that @mm belongs to, reclaiming
6625 * pages according to @gfp_mask if necessary.
6626 *
Johannes Weinerf0e45fb2020-06-03 16:02:07 -07006627 * Returns 0 on success. Otherwise, an error code is returned.
Johannes Weiner00501b52014-08-08 14:19:20 -07006628 */
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07006629int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
Johannes Weiner00501b52014-08-08 14:19:20 -07006630{
Johannes Weiner3fba69a2020-06-03 16:01:31 -07006631 unsigned int nr_pages = hpage_nr_pages(page);
Johannes Weiner00501b52014-08-08 14:19:20 -07006632 struct mem_cgroup *memcg = NULL;
Johannes Weiner00501b52014-08-08 14:19:20 -07006633 int ret = 0;
6634
6635 if (mem_cgroup_disabled())
6636 goto out;
6637
6638 if (PageSwapCache(page)) {
Johannes Weiner2d1c4982020-06-03 16:02:14 -07006639 swp_entry_t ent = { .val = page_private(page), };
6640 unsigned short id;
6641
Johannes Weiner00501b52014-08-08 14:19:20 -07006642 /*
6643 * Every swap fault against a single page tries to charge the
6644 * page, bail as early as possible. shmem_unuse() encounters
Johannes Weinereccb52e2020-06-03 16:02:11 -07006645 * already charged pages, too. page->mem_cgroup is protected
6646 * by the page lock, which serializes swap cache removal, which
Johannes Weiner00501b52014-08-08 14:19:20 -07006647 * in turn serializes uncharging.
6648 */
Vladimir Davydove993d902015-09-09 15:35:35 -07006649 VM_BUG_ON_PAGE(!PageLocked(page), page);
Huang Yingabe28952017-09-06 16:22:41 -07006650 if (compound_head(page)->mem_cgroup)
Johannes Weiner00501b52014-08-08 14:19:20 -07006651 goto out;
Vladimir Davydove993d902015-09-09 15:35:35 -07006652
Johannes Weiner2d1c4982020-06-03 16:02:14 -07006653 id = lookup_swap_cgroup_id(ent);
6654 rcu_read_lock();
6655 memcg = mem_cgroup_from_id(id);
6656 if (memcg && !css_tryget_online(&memcg->css))
6657 memcg = NULL;
6658 rcu_read_unlock();
Johannes Weiner00501b52014-08-08 14:19:20 -07006659 }
6660
Johannes Weiner00501b52014-08-08 14:19:20 -07006661 if (!memcg)
6662 memcg = get_mem_cgroup_from_mm(mm);
6663
6664 ret = try_charge(memcg, gfp_mask, nr_pages);
Johannes Weinerf0e45fb2020-06-03 16:02:07 -07006665 if (ret)
6666 goto out_put;
Johannes Weiner00501b52014-08-08 14:19:20 -07006667
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07006668 css_get(&memcg->css);
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07006669 commit_charge(page, memcg);
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006670
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006671 local_irq_disable();
Johannes Weiner3fba69a2020-06-03 16:01:31 -07006672 mem_cgroup_charge_statistics(memcg, page, nr_pages);
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006673 memcg_check_events(memcg, page);
6674 local_irq_enable();
Johannes Weiner00501b52014-08-08 14:19:20 -07006675
Johannes Weiner2d1c4982020-06-03 16:02:14 -07006676 if (PageSwapCache(page)) {
Johannes Weiner00501b52014-08-08 14:19:20 -07006677 swp_entry_t entry = { .val = page_private(page) };
6678 /*
6679 * The swap entry might not get freed for a long time,
6680 * let's not wait for it. The page already received a
6681 * memory+swap charge, drop the swap entry duplicate.
6682 */
Huang Ying38d8b4e2017-07-06 15:37:18 -07006683 mem_cgroup_uncharge_swap(entry, nr_pages);
Johannes Weiner00501b52014-08-08 14:19:20 -07006684 }
Johannes Weiner00501b52014-08-08 14:19:20 -07006685
Johannes Weinerf0e45fb2020-06-03 16:02:07 -07006686out_put:
6687 css_put(&memcg->css);
6688out:
6689 return ret;
Johannes Weiner3fea5a42020-06-03 16:01:41 -07006690}
6691
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006692struct uncharge_gather {
6693 struct mem_cgroup *memcg;
Johannes Weiner9f762db2020-06-03 16:01:44 -07006694 unsigned long nr_pages;
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006695 unsigned long pgpgout;
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006696 unsigned long nr_kmem;
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006697 struct page *dummy_page;
6698};
6699
6700static inline void uncharge_gather_clear(struct uncharge_gather *ug)
Johannes Weiner747db952014-08-08 14:19:24 -07006701{
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006702 memset(ug, 0, sizeof(*ug));
6703}
6704
6705static void uncharge_batch(const struct uncharge_gather *ug)
6706{
Johannes Weiner747db952014-08-08 14:19:24 -07006707 unsigned long flags;
6708
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006709 if (!mem_cgroup_is_root(ug->memcg)) {
Johannes Weiner9f762db2020-06-03 16:01:44 -07006710 page_counter_uncharge(&ug->memcg->memory, ug->nr_pages);
Johannes Weiner7941d212016-01-14 15:21:23 -08006711 if (do_memsw_account())
Johannes Weiner9f762db2020-06-03 16:01:44 -07006712 page_counter_uncharge(&ug->memcg->memsw, ug->nr_pages);
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006713 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && ug->nr_kmem)
6714 page_counter_uncharge(&ug->memcg->kmem, ug->nr_kmem);
6715 memcg_oom_recover(ug->memcg);
Johannes Weinerce00a962014-09-05 08:43:57 -04006716 }
Johannes Weiner747db952014-08-08 14:19:24 -07006717
6718 local_irq_save(flags);
Johannes Weinerc9019e92018-01-31 16:16:37 -08006719 __count_memcg_events(ug->memcg, PGPGOUT, ug->pgpgout);
Johannes Weiner9f762db2020-06-03 16:01:44 -07006720 __this_cpu_add(ug->memcg->vmstats_percpu->nr_page_events, ug->nr_pages);
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006721 memcg_check_events(ug->memcg, ug->dummy_page);
Johannes Weiner747db952014-08-08 14:19:24 -07006722 local_irq_restore(flags);
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006723}
6724
6725static void uncharge_page(struct page *page, struct uncharge_gather *ug)
6726{
Johannes Weiner9f762db2020-06-03 16:01:44 -07006727 unsigned long nr_pages;
6728
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006729 VM_BUG_ON_PAGE(PageLRU(page), page);
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006730
6731 if (!page->mem_cgroup)
6732 return;
6733
6734 /*
6735 * Nobody should be changing or seriously looking at
6736 * page->mem_cgroup at this point, we have fully
6737 * exclusive access to the page.
6738 */
6739
6740 if (ug->memcg != page->mem_cgroup) {
6741 if (ug->memcg) {
6742 uncharge_batch(ug);
6743 uncharge_gather_clear(ug);
6744 }
6745 ug->memcg = page->mem_cgroup;
6746 }
6747
Johannes Weiner9f762db2020-06-03 16:01:44 -07006748 nr_pages = compound_nr(page);
6749 ug->nr_pages += nr_pages;
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006750
Johannes Weiner9f762db2020-06-03 16:01:44 -07006751 if (!PageKmemcg(page)) {
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006752 ug->pgpgout++;
6753 } else {
Johannes Weiner9f762db2020-06-03 16:01:44 -07006754 ug->nr_kmem += nr_pages;
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006755 __ClearPageKmemcg(page);
6756 }
6757
6758 ug->dummy_page = page;
6759 page->mem_cgroup = NULL;
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07006760 css_put(&ug->memcg->css);
Johannes Weiner747db952014-08-08 14:19:24 -07006761}
6762
6763static void uncharge_list(struct list_head *page_list)
6764{
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006765 struct uncharge_gather ug;
Johannes Weiner747db952014-08-08 14:19:24 -07006766 struct list_head *next;
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006767
6768 uncharge_gather_clear(&ug);
Johannes Weiner747db952014-08-08 14:19:24 -07006769
Johannes Weiner8b592652016-03-17 14:20:31 -07006770 /*
6771 * Note that the list can be a single page->lru; hence the
6772 * do-while loop instead of a simple list_for_each_entry().
6773 */
Johannes Weiner747db952014-08-08 14:19:24 -07006774 next = page_list->next;
6775 do {
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006776 struct page *page;
6777
Johannes Weiner747db952014-08-08 14:19:24 -07006778 page = list_entry(next, struct page, lru);
6779 next = page->lru.next;
6780
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006781 uncharge_page(page, &ug);
Johannes Weiner747db952014-08-08 14:19:24 -07006782 } while (next != page_list);
6783
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006784 if (ug.memcg)
6785 uncharge_batch(&ug);
Johannes Weiner747db952014-08-08 14:19:24 -07006786}
6787
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006788/**
6789 * mem_cgroup_uncharge - uncharge a page
6790 * @page: page to uncharge
6791 *
Johannes Weinerf0e45fb2020-06-03 16:02:07 -07006792 * Uncharge a page previously charged with mem_cgroup_charge().
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006793 */
6794void mem_cgroup_uncharge(struct page *page)
6795{
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006796 struct uncharge_gather ug;
6797
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006798 if (mem_cgroup_disabled())
6799 return;
6800
Johannes Weiner747db952014-08-08 14:19:24 -07006801 /* Don't touch page->lru of any random page, pre-check: */
Johannes Weiner1306a852014-12-10 15:44:52 -08006802 if (!page->mem_cgroup)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006803 return;
6804
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006805 uncharge_gather_clear(&ug);
6806 uncharge_page(page, &ug);
6807 uncharge_batch(&ug);
Johannes Weiner747db952014-08-08 14:19:24 -07006808}
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006809
Johannes Weiner747db952014-08-08 14:19:24 -07006810/**
6811 * mem_cgroup_uncharge_list - uncharge a list of page
6812 * @page_list: list of pages to uncharge
6813 *
6814 * Uncharge a list of pages previously charged with
Johannes Weinerf0e45fb2020-06-03 16:02:07 -07006815 * mem_cgroup_charge().
Johannes Weiner747db952014-08-08 14:19:24 -07006816 */
6817void mem_cgroup_uncharge_list(struct list_head *page_list)
6818{
6819 if (mem_cgroup_disabled())
6820 return;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006821
Johannes Weiner747db952014-08-08 14:19:24 -07006822 if (!list_empty(page_list))
6823 uncharge_list(page_list);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006824}
6825
6826/**
Johannes Weiner6a93ca82016-03-15 14:57:19 -07006827 * mem_cgroup_migrate - charge a page's replacement
6828 * @oldpage: currently circulating page
6829 * @newpage: replacement page
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006830 *
Johannes Weiner6a93ca82016-03-15 14:57:19 -07006831 * Charge @newpage as a replacement page for @oldpage. @oldpage will
6832 * be uncharged upon free.
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006833 *
6834 * Both pages must be locked, @newpage->mapping must be set up.
6835 */
Johannes Weiner6a93ca82016-03-15 14:57:19 -07006836void mem_cgroup_migrate(struct page *oldpage, struct page *newpage)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006837{
Johannes Weiner29833312014-12-10 15:44:02 -08006838 struct mem_cgroup *memcg;
Johannes Weiner44b7a8d2016-01-20 15:03:16 -08006839 unsigned int nr_pages;
Tejun Heod93c4132016-06-24 14:49:54 -07006840 unsigned long flags;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006841
6842 VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage);
6843 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006844 VM_BUG_ON_PAGE(PageAnon(oldpage) != PageAnon(newpage), newpage);
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006845 VM_BUG_ON_PAGE(PageTransHuge(oldpage) != PageTransHuge(newpage),
6846 newpage);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006847
6848 if (mem_cgroup_disabled())
6849 return;
6850
6851 /* Page cache replacement: new page already charged? */
Johannes Weiner1306a852014-12-10 15:44:52 -08006852 if (newpage->mem_cgroup)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006853 return;
6854
Hugh Dickins45637ba2015-11-05 18:49:40 -08006855 /* Swapcache readahead pages can get replaced before being charged */
Johannes Weiner1306a852014-12-10 15:44:52 -08006856 memcg = oldpage->mem_cgroup;
Johannes Weiner29833312014-12-10 15:44:02 -08006857 if (!memcg)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006858 return;
6859
Johannes Weiner44b7a8d2016-01-20 15:03:16 -08006860 /* Force-charge the new page. The old one will be freed soon */
Kaitao Cheng928552702020-01-30 22:13:42 -08006861 nr_pages = hpage_nr_pages(newpage);
Johannes Weiner44b7a8d2016-01-20 15:03:16 -08006862
6863 page_counter_charge(&memcg->memory, nr_pages);
6864 if (do_memsw_account())
6865 page_counter_charge(&memcg->memsw, nr_pages);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006866
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07006867 css_get(&memcg->css);
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07006868 commit_charge(newpage, memcg);
Johannes Weiner44b7a8d2016-01-20 15:03:16 -08006869
Tejun Heod93c4132016-06-24 14:49:54 -07006870 local_irq_save(flags);
Johannes Weiner3fba69a2020-06-03 16:01:31 -07006871 mem_cgroup_charge_statistics(memcg, newpage, nr_pages);
Johannes Weiner44b7a8d2016-01-20 15:03:16 -08006872 memcg_check_events(memcg, newpage);
Tejun Heod93c4132016-06-24 14:49:54 -07006873 local_irq_restore(flags);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006874}
6875
Johannes Weineref129472016-01-14 15:21:34 -08006876DEFINE_STATIC_KEY_FALSE(memcg_sockets_enabled_key);
Johannes Weiner11092082016-01-14 15:21:26 -08006877EXPORT_SYMBOL(memcg_sockets_enabled_key);
6878
Johannes Weiner2d758072016-10-07 17:00:58 -07006879void mem_cgroup_sk_alloc(struct sock *sk)
Johannes Weiner11092082016-01-14 15:21:26 -08006880{
6881 struct mem_cgroup *memcg;
6882
Johannes Weiner2d758072016-10-07 17:00:58 -07006883 if (!mem_cgroup_sockets_enabled)
6884 return;
6885
Shakeel Butte876ecc2020-03-09 22:16:05 -07006886 /* Do not associate the sock with unrelated interrupted task's memcg. */
6887 if (in_interrupt())
6888 return;
6889
Johannes Weiner11092082016-01-14 15:21:26 -08006890 rcu_read_lock();
6891 memcg = mem_cgroup_from_task(current);
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006892 if (memcg == root_mem_cgroup)
6893 goto out;
Johannes Weiner0db15292016-01-20 15:02:50 -08006894 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !memcg->tcpmem_active)
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006895 goto out;
Shakeel Butt8965aa22020-04-01 21:07:10 -07006896 if (css_tryget(&memcg->css))
Johannes Weiner11092082016-01-14 15:21:26 -08006897 sk->sk_memcg = memcg;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006898out:
Johannes Weiner11092082016-01-14 15:21:26 -08006899 rcu_read_unlock();
6900}
Johannes Weiner11092082016-01-14 15:21:26 -08006901
Johannes Weiner2d758072016-10-07 17:00:58 -07006902void mem_cgroup_sk_free(struct sock *sk)
Johannes Weiner11092082016-01-14 15:21:26 -08006903{
Johannes Weiner2d758072016-10-07 17:00:58 -07006904 if (sk->sk_memcg)
6905 css_put(&sk->sk_memcg->css);
Johannes Weiner11092082016-01-14 15:21:26 -08006906}
6907
6908/**
6909 * mem_cgroup_charge_skmem - charge socket memory
6910 * @memcg: memcg to charge
6911 * @nr_pages: number of pages to charge
6912 *
6913 * Charges @nr_pages to @memcg. Returns %true if the charge fit within
6914 * @memcg's configured limit, %false if the charge had to be forced.
6915 */
6916bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
6917{
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006918 gfp_t gfp_mask = GFP_KERNEL;
Johannes Weiner11092082016-01-14 15:21:26 -08006919
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006920 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
Johannes Weiner0db15292016-01-20 15:02:50 -08006921 struct page_counter *fail;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006922
Johannes Weiner0db15292016-01-20 15:02:50 -08006923 if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) {
6924 memcg->tcpmem_pressure = 0;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006925 return true;
6926 }
Johannes Weiner0db15292016-01-20 15:02:50 -08006927 page_counter_charge(&memcg->tcpmem, nr_pages);
6928 memcg->tcpmem_pressure = 1;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006929 return false;
Johannes Weiner11092082016-01-14 15:21:26 -08006930 }
Johannes Weinerd886f4e2016-01-20 15:02:47 -08006931
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006932 /* Don't block in the packet receive path */
6933 if (in_softirq())
6934 gfp_mask = GFP_NOWAIT;
6935
Johannes Weinerc9019e92018-01-31 16:16:37 -08006936 mod_memcg_state(memcg, MEMCG_SOCK, nr_pages);
Johannes Weinerb2807f02016-01-20 15:03:22 -08006937
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006938 if (try_charge(memcg, gfp_mask, nr_pages) == 0)
6939 return true;
6940
6941 try_charge(memcg, gfp_mask|__GFP_NOFAIL, nr_pages);
Johannes Weiner11092082016-01-14 15:21:26 -08006942 return false;
6943}
6944
6945/**
6946 * mem_cgroup_uncharge_skmem - uncharge socket memory
Mike Rapoportb7701a52018-02-06 15:42:13 -08006947 * @memcg: memcg to uncharge
6948 * @nr_pages: number of pages to uncharge
Johannes Weiner11092082016-01-14 15:21:26 -08006949 */
6950void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
6951{
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006952 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
Johannes Weiner0db15292016-01-20 15:02:50 -08006953 page_counter_uncharge(&memcg->tcpmem, nr_pages);
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006954 return;
6955 }
Johannes Weinerd886f4e2016-01-20 15:02:47 -08006956
Johannes Weinerc9019e92018-01-31 16:16:37 -08006957 mod_memcg_state(memcg, MEMCG_SOCK, -nr_pages);
Johannes Weinerb2807f02016-01-20 15:03:22 -08006958
Roman Gushchin475d0482017-09-08 16:13:09 -07006959 refill_stock(memcg, nr_pages);
Johannes Weiner11092082016-01-14 15:21:26 -08006960}
6961
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006962static int __init cgroup_memory(char *s)
6963{
6964 char *token;
6965
6966 while ((token = strsep(&s, ",")) != NULL) {
6967 if (!*token)
6968 continue;
6969 if (!strcmp(token, "nosocket"))
6970 cgroup_memory_nosocket = true;
Vladimir Davydov04823c82016-01-20 15:02:38 -08006971 if (!strcmp(token, "nokmem"))
6972 cgroup_memory_nokmem = true;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006973 }
6974 return 0;
6975}
6976__setup("cgroup.memory=", cgroup_memory);
Johannes Weiner11092082016-01-14 15:21:26 -08006977
Michal Hocko2d110852013-02-22 16:34:43 -08006978/*
Michal Hocko10813122013-02-22 16:35:41 -08006979 * subsys_initcall() for memory controller.
6980 *
Sebastian Andrzej Siewior308167f2016-11-03 15:49:59 +01006981 * Some parts like memcg_hotplug_cpu_dead() have to be initialized from this
6982 * context because of lock dependencies (cgroup_lock -> cpu hotplug) but
6983 * basically everything that doesn't depend on a specific mem_cgroup structure
6984 * should be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08006985 */
6986static int __init mem_cgroup_init(void)
6987{
Johannes Weiner95a045f2015-02-11 15:26:33 -08006988 int cpu, node;
6989
Sebastian Andrzej Siewior308167f2016-11-03 15:49:59 +01006990 cpuhp_setup_state_nocalls(CPUHP_MM_MEMCQ_DEAD, "mm/memctrl:dead", NULL,
6991 memcg_hotplug_cpu_dead);
Johannes Weiner95a045f2015-02-11 15:26:33 -08006992
6993 for_each_possible_cpu(cpu)
6994 INIT_WORK(&per_cpu_ptr(&memcg_stock, cpu)->work,
6995 drain_local_stock);
6996
6997 for_each_node(node) {
6998 struct mem_cgroup_tree_per_node *rtpn;
Johannes Weiner95a045f2015-02-11 15:26:33 -08006999
7000 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL,
7001 node_online(node) ? node : NUMA_NO_NODE);
7002
Mel Gormanef8f2322016-07-28 15:46:05 -07007003 rtpn->rb_root = RB_ROOT;
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -07007004 rtpn->rb_rightmost = NULL;
Mel Gormanef8f2322016-07-28 15:46:05 -07007005 spin_lock_init(&rtpn->lock);
Johannes Weiner95a045f2015-02-11 15:26:33 -08007006 soft_limit_tree.rb_tree_per_node[node] = rtpn;
7007 }
7008
Michal Hocko2d110852013-02-22 16:34:43 -08007009 return 0;
7010}
7011subsys_initcall(mem_cgroup_init);
Johannes Weiner21afa382015-02-11 15:26:36 -08007012
7013#ifdef CONFIG_MEMCG_SWAP
Arnd Bergmann358c07f2016-08-25 15:17:08 -07007014static struct mem_cgroup *mem_cgroup_id_get_online(struct mem_cgroup *memcg)
7015{
Kirill Tkhai1c2d4792018-10-26 15:09:28 -07007016 while (!refcount_inc_not_zero(&memcg->id.ref)) {
Arnd Bergmann358c07f2016-08-25 15:17:08 -07007017 /*
7018 * The root cgroup cannot be destroyed, so it's refcount must
7019 * always be >= 1.
7020 */
7021 if (WARN_ON_ONCE(memcg == root_mem_cgroup)) {
7022 VM_BUG_ON(1);
7023 break;
7024 }
7025 memcg = parent_mem_cgroup(memcg);
7026 if (!memcg)
7027 memcg = root_mem_cgroup;
7028 }
7029 return memcg;
7030}
7031
Johannes Weiner21afa382015-02-11 15:26:36 -08007032/**
7033 * mem_cgroup_swapout - transfer a memsw charge to swap
7034 * @page: page whose memsw charge to transfer
7035 * @entry: swap entry to move the charge to
7036 *
7037 * Transfer the memsw charge of @page to @entry.
7038 */
7039void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
7040{
Vladimir Davydov1f47b612016-08-11 15:33:00 -07007041 struct mem_cgroup *memcg, *swap_memcg;
Huang Yingd6810d72017-09-06 16:22:45 -07007042 unsigned int nr_entries;
Johannes Weiner21afa382015-02-11 15:26:36 -08007043 unsigned short oldid;
7044
7045 VM_BUG_ON_PAGE(PageLRU(page), page);
7046 VM_BUG_ON_PAGE(page_count(page), page);
7047
Johannes Weiner2d1c4982020-06-03 16:02:14 -07007048 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
Johannes Weiner21afa382015-02-11 15:26:36 -08007049 return;
7050
7051 memcg = page->mem_cgroup;
7052
7053 /* Readahead page, never charged */
7054 if (!memcg)
7055 return;
7056
Vladimir Davydov1f47b612016-08-11 15:33:00 -07007057 /*
7058 * In case the memcg owning these pages has been offlined and doesn't
7059 * have an ID allocated to it anymore, charge the closest online
7060 * ancestor for the swap instead and transfer the memory+swap charge.
7061 */
7062 swap_memcg = mem_cgroup_id_get_online(memcg);
Huang Yingd6810d72017-09-06 16:22:45 -07007063 nr_entries = hpage_nr_pages(page);
7064 /* Get references for the tail pages, too */
7065 if (nr_entries > 1)
7066 mem_cgroup_id_get_many(swap_memcg, nr_entries - 1);
7067 oldid = swap_cgroup_record(entry, mem_cgroup_id(swap_memcg),
7068 nr_entries);
Johannes Weiner21afa382015-02-11 15:26:36 -08007069 VM_BUG_ON_PAGE(oldid, page);
Johannes Weinerc9019e92018-01-31 16:16:37 -08007070 mod_memcg_state(swap_memcg, MEMCG_SWAP, nr_entries);
Johannes Weiner21afa382015-02-11 15:26:36 -08007071
7072 page->mem_cgroup = NULL;
7073
7074 if (!mem_cgroup_is_root(memcg))
Huang Yingd6810d72017-09-06 16:22:45 -07007075 page_counter_uncharge(&memcg->memory, nr_entries);
Johannes Weiner21afa382015-02-11 15:26:36 -08007076
Johannes Weiner2d1c4982020-06-03 16:02:14 -07007077 if (!cgroup_memory_noswap && memcg != swap_memcg) {
Vladimir Davydov1f47b612016-08-11 15:33:00 -07007078 if (!mem_cgroup_is_root(swap_memcg))
Huang Yingd6810d72017-09-06 16:22:45 -07007079 page_counter_charge(&swap_memcg->memsw, nr_entries);
7080 page_counter_uncharge(&memcg->memsw, nr_entries);
Vladimir Davydov1f47b612016-08-11 15:33:00 -07007081 }
7082
Sebastian Andrzej Siewiorce9ce662015-09-04 15:47:50 -07007083 /*
7084 * Interrupts should be disabled here because the caller holds the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07007085 * i_pages lock which is taken with interrupts-off. It is
Sebastian Andrzej Siewiorce9ce662015-09-04 15:47:50 -07007086 * important here to have the interrupts disabled because it is the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07007087 * only synchronisation we have for updating the per-CPU variables.
Sebastian Andrzej Siewiorce9ce662015-09-04 15:47:50 -07007088 */
7089 VM_BUG_ON(!irqs_disabled());
Johannes Weiner3fba69a2020-06-03 16:01:31 -07007090 mem_cgroup_charge_statistics(memcg, page, -nr_entries);
Johannes Weiner21afa382015-02-11 15:26:36 -08007091 memcg_check_events(memcg, page);
Johannes Weiner73f576c2016-07-20 15:44:57 -07007092
Johannes Weiner1a3e1f42020-08-06 23:20:45 -07007093 css_put(&memcg->css);
Johannes Weiner21afa382015-02-11 15:26:36 -08007094}
7095
Huang Ying38d8b4e2017-07-06 15:37:18 -07007096/**
7097 * mem_cgroup_try_charge_swap - try charging swap space for a page
Vladimir Davydov37e84352016-01-20 15:02:56 -08007098 * @page: page being added to swap
7099 * @entry: swap entry to charge
7100 *
Huang Ying38d8b4e2017-07-06 15:37:18 -07007101 * Try to charge @page's memcg for the swap space at @entry.
Vladimir Davydov37e84352016-01-20 15:02:56 -08007102 *
7103 * Returns 0 on success, -ENOMEM on failure.
7104 */
7105int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry)
7106{
Huang Ying38d8b4e2017-07-06 15:37:18 -07007107 unsigned int nr_pages = hpage_nr_pages(page);
Vladimir Davydov37e84352016-01-20 15:02:56 -08007108 struct page_counter *counter;
Huang Ying38d8b4e2017-07-06 15:37:18 -07007109 struct mem_cgroup *memcg;
Vladimir Davydov37e84352016-01-20 15:02:56 -08007110 unsigned short oldid;
7111
Johannes Weiner2d1c4982020-06-03 16:02:14 -07007112 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
Vladimir Davydov37e84352016-01-20 15:02:56 -08007113 return 0;
7114
7115 memcg = page->mem_cgroup;
7116
7117 /* Readahead page, never charged */
7118 if (!memcg)
7119 return 0;
7120
Tejun Heof3a53a32018-06-07 17:05:35 -07007121 if (!entry.val) {
7122 memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
Tejun Heobb98f2c2018-06-07 17:05:31 -07007123 return 0;
Tejun Heof3a53a32018-06-07 17:05:35 -07007124 }
Tejun Heobb98f2c2018-06-07 17:05:31 -07007125
Vladimir Davydov1f47b612016-08-11 15:33:00 -07007126 memcg = mem_cgroup_id_get_online(memcg);
Vladimir Davydov37e84352016-01-20 15:02:56 -08007127
Johannes Weiner2d1c4982020-06-03 16:02:14 -07007128 if (!cgroup_memory_noswap && !mem_cgroup_is_root(memcg) &&
Huang Ying38d8b4e2017-07-06 15:37:18 -07007129 !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) {
Tejun Heof3a53a32018-06-07 17:05:35 -07007130 memcg_memory_event(memcg, MEMCG_SWAP_MAX);
7131 memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
Vladimir Davydov1f47b612016-08-11 15:33:00 -07007132 mem_cgroup_id_put(memcg);
7133 return -ENOMEM;
7134 }
7135
Huang Ying38d8b4e2017-07-06 15:37:18 -07007136 /* Get references for the tail pages, too */
7137 if (nr_pages > 1)
7138 mem_cgroup_id_get_many(memcg, nr_pages - 1);
7139 oldid = swap_cgroup_record(entry, mem_cgroup_id(memcg), nr_pages);
Vladimir Davydov37e84352016-01-20 15:02:56 -08007140 VM_BUG_ON_PAGE(oldid, page);
Johannes Weinerc9019e92018-01-31 16:16:37 -08007141 mod_memcg_state(memcg, MEMCG_SWAP, nr_pages);
Vladimir Davydov37e84352016-01-20 15:02:56 -08007142
Vladimir Davydov37e84352016-01-20 15:02:56 -08007143 return 0;
7144}
7145
Johannes Weiner21afa382015-02-11 15:26:36 -08007146/**
Huang Ying38d8b4e2017-07-06 15:37:18 -07007147 * mem_cgroup_uncharge_swap - uncharge swap space
Johannes Weiner21afa382015-02-11 15:26:36 -08007148 * @entry: swap entry to uncharge
Huang Ying38d8b4e2017-07-06 15:37:18 -07007149 * @nr_pages: the amount of swap space to uncharge
Johannes Weiner21afa382015-02-11 15:26:36 -08007150 */
Huang Ying38d8b4e2017-07-06 15:37:18 -07007151void mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_pages)
Johannes Weiner21afa382015-02-11 15:26:36 -08007152{
7153 struct mem_cgroup *memcg;
7154 unsigned short id;
7155
Huang Ying38d8b4e2017-07-06 15:37:18 -07007156 id = swap_cgroup_record(entry, 0, nr_pages);
Johannes Weiner21afa382015-02-11 15:26:36 -08007157 rcu_read_lock();
Vladimir Davydovadbe4272015-04-15 16:13:00 -07007158 memcg = mem_cgroup_from_id(id);
Johannes Weiner21afa382015-02-11 15:26:36 -08007159 if (memcg) {
Johannes Weiner2d1c4982020-06-03 16:02:14 -07007160 if (!cgroup_memory_noswap && !mem_cgroup_is_root(memcg)) {
Vladimir Davydov37e84352016-01-20 15:02:56 -08007161 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
Huang Ying38d8b4e2017-07-06 15:37:18 -07007162 page_counter_uncharge(&memcg->swap, nr_pages);
Vladimir Davydov37e84352016-01-20 15:02:56 -08007163 else
Huang Ying38d8b4e2017-07-06 15:37:18 -07007164 page_counter_uncharge(&memcg->memsw, nr_pages);
Vladimir Davydov37e84352016-01-20 15:02:56 -08007165 }
Johannes Weinerc9019e92018-01-31 16:16:37 -08007166 mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages);
Huang Ying38d8b4e2017-07-06 15:37:18 -07007167 mem_cgroup_id_put_many(memcg, nr_pages);
Johannes Weiner21afa382015-02-11 15:26:36 -08007168 }
7169 rcu_read_unlock();
7170}
7171
Vladimir Davydovd8b38432016-01-20 15:03:07 -08007172long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)
7173{
7174 long nr_swap_pages = get_nr_swap_pages();
7175
Johannes Weinereccb52e2020-06-03 16:02:11 -07007176 if (cgroup_memory_noswap || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
Vladimir Davydovd8b38432016-01-20 15:03:07 -08007177 return nr_swap_pages;
7178 for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg))
7179 nr_swap_pages = min_t(long, nr_swap_pages,
Roman Gushchinbbec2e12018-06-07 17:06:18 -07007180 READ_ONCE(memcg->swap.max) -
Vladimir Davydovd8b38432016-01-20 15:03:07 -08007181 page_counter_read(&memcg->swap));
7182 return nr_swap_pages;
7183}
7184
Vladimir Davydov5ccc5ab2016-01-20 15:03:10 -08007185bool mem_cgroup_swap_full(struct page *page)
7186{
7187 struct mem_cgroup *memcg;
7188
7189 VM_BUG_ON_PAGE(!PageLocked(page), page);
7190
7191 if (vm_swap_full())
7192 return true;
Johannes Weinereccb52e2020-06-03 16:02:11 -07007193 if (cgroup_memory_noswap || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
Vladimir Davydov5ccc5ab2016-01-20 15:03:10 -08007194 return false;
7195
7196 memcg = page->mem_cgroup;
7197 if (!memcg)
7198 return false;
7199
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07007200 for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) {
7201 unsigned long usage = page_counter_read(&memcg->swap);
7202
7203 if (usage * 2 >= READ_ONCE(memcg->swap.high) ||
7204 usage * 2 >= READ_ONCE(memcg->swap.max))
Vladimir Davydov5ccc5ab2016-01-20 15:03:10 -08007205 return true;
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07007206 }
Vladimir Davydov5ccc5ab2016-01-20 15:03:10 -08007207
7208 return false;
7209}
7210
Johannes Weinereccb52e2020-06-03 16:02:11 -07007211static int __init setup_swap_account(char *s)
Johannes Weiner21afa382015-02-11 15:26:36 -08007212{
7213 if (!strcmp(s, "1"))
Johannes Weinereccb52e2020-06-03 16:02:11 -07007214 cgroup_memory_noswap = 0;
Johannes Weiner21afa382015-02-11 15:26:36 -08007215 else if (!strcmp(s, "0"))
Johannes Weinereccb52e2020-06-03 16:02:11 -07007216 cgroup_memory_noswap = 1;
Johannes Weiner21afa382015-02-11 15:26:36 -08007217 return 1;
7218}
Johannes Weinereccb52e2020-06-03 16:02:11 -07007219__setup("swapaccount=", setup_swap_account);
Johannes Weiner21afa382015-02-11 15:26:36 -08007220
Vladimir Davydov37e84352016-01-20 15:02:56 -08007221static u64 swap_current_read(struct cgroup_subsys_state *css,
7222 struct cftype *cft)
7223{
7224 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
7225
7226 return (u64)page_counter_read(&memcg->swap) * PAGE_SIZE;
7227}
7228
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07007229static int swap_high_show(struct seq_file *m, void *v)
7230{
7231 return seq_puts_memcg_tunable(m,
7232 READ_ONCE(mem_cgroup_from_seq(m)->swap.high));
7233}
7234
7235static ssize_t swap_high_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 high;
7240 int err;
7241
7242 buf = strstrip(buf);
7243 err = page_counter_memparse(buf, "max", &high);
7244 if (err)
7245 return err;
7246
7247 page_counter_set_high(&memcg->swap, high);
7248
7249 return nbytes;
7250}
7251
Vladimir Davydov37e84352016-01-20 15:02:56 -08007252static int swap_max_show(struct seq_file *m, void *v)
7253{
Chris Down677dc972019-03-05 15:45:55 -08007254 return seq_puts_memcg_tunable(m,
7255 READ_ONCE(mem_cgroup_from_seq(m)->swap.max));
Vladimir Davydov37e84352016-01-20 15:02:56 -08007256}
7257
7258static ssize_t swap_max_write(struct kernfs_open_file *of,
7259 char *buf, size_t nbytes, loff_t off)
7260{
7261 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
7262 unsigned long max;
7263 int err;
7264
7265 buf = strstrip(buf);
7266 err = page_counter_memparse(buf, "max", &max);
7267 if (err)
7268 return err;
7269
Tejun Heobe091022018-06-07 17:09:21 -07007270 xchg(&memcg->swap.max, max);
Vladimir Davydov37e84352016-01-20 15:02:56 -08007271
7272 return nbytes;
7273}
7274
Tejun Heof3a53a32018-06-07 17:05:35 -07007275static int swap_events_show(struct seq_file *m, void *v)
7276{
Chris Downaa9694b2019-03-05 15:45:52 -08007277 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Tejun Heof3a53a32018-06-07 17:05:35 -07007278
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07007279 seq_printf(m, "high %lu\n",
7280 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_HIGH]));
Tejun Heof3a53a32018-06-07 17:05:35 -07007281 seq_printf(m, "max %lu\n",
7282 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_MAX]));
7283 seq_printf(m, "fail %lu\n",
7284 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_FAIL]));
7285
7286 return 0;
7287}
7288
Vladimir Davydov37e84352016-01-20 15:02:56 -08007289static struct cftype swap_files[] = {
7290 {
7291 .name = "swap.current",
7292 .flags = CFTYPE_NOT_ON_ROOT,
7293 .read_u64 = swap_current_read,
7294 },
7295 {
Jakub Kicinski4b82ab42020-06-01 21:49:52 -07007296 .name = "swap.high",
7297 .flags = CFTYPE_NOT_ON_ROOT,
7298 .seq_show = swap_high_show,
7299 .write = swap_high_write,
7300 },
7301 {
Vladimir Davydov37e84352016-01-20 15:02:56 -08007302 .name = "swap.max",
7303 .flags = CFTYPE_NOT_ON_ROOT,
7304 .seq_show = swap_max_show,
7305 .write = swap_max_write,
7306 },
Tejun Heof3a53a32018-06-07 17:05:35 -07007307 {
7308 .name = "swap.events",
7309 .flags = CFTYPE_NOT_ON_ROOT,
7310 .file_offset = offsetof(struct mem_cgroup, swap_events_file),
7311 .seq_show = swap_events_show,
7312 },
Vladimir Davydov37e84352016-01-20 15:02:56 -08007313 { } /* terminate */
7314};
7315
Johannes Weinereccb52e2020-06-03 16:02:11 -07007316static struct cftype memsw_files[] = {
Johannes Weiner21afa382015-02-11 15:26:36 -08007317 {
7318 .name = "memsw.usage_in_bytes",
7319 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
7320 .read_u64 = mem_cgroup_read_u64,
7321 },
7322 {
7323 .name = "memsw.max_usage_in_bytes",
7324 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
7325 .write = mem_cgroup_reset,
7326 .read_u64 = mem_cgroup_read_u64,
7327 },
7328 {
7329 .name = "memsw.limit_in_bytes",
7330 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
7331 .write = mem_cgroup_write,
7332 .read_u64 = mem_cgroup_read_u64,
7333 },
7334 {
7335 .name = "memsw.failcnt",
7336 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
7337 .write = mem_cgroup_reset,
7338 .read_u64 = mem_cgroup_read_u64,
7339 },
7340 { }, /* terminate */
7341};
7342
Bhupesh Sharma82ff1652020-07-23 21:15:21 -07007343/*
7344 * If mem_cgroup_swap_init() is implemented as a subsys_initcall()
7345 * instead of a core_initcall(), this could mean cgroup_memory_noswap still
7346 * remains set to false even when memcg is disabled via "cgroup_disable=memory"
7347 * boot parameter. This may result in premature OOPS inside
7348 * mem_cgroup_get_nr_swap_pages() function in corner cases.
7349 */
Johannes Weiner21afa382015-02-11 15:26:36 -08007350static int __init mem_cgroup_swap_init(void)
7351{
Johannes Weiner2d1c4982020-06-03 16:02:14 -07007352 /* No memory control -> no swap control */
7353 if (mem_cgroup_disabled())
7354 cgroup_memory_noswap = true;
7355
7356 if (cgroup_memory_noswap)
Johannes Weinereccb52e2020-06-03 16:02:11 -07007357 return 0;
7358
7359 WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, swap_files));
7360 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, memsw_files));
7361
Johannes Weiner21afa382015-02-11 15:26:36 -08007362 return 0;
7363}
Bhupesh Sharma82ff1652020-07-23 21:15:21 -07007364core_initcall(mem_cgroup_swap_init);
Johannes Weiner21afa382015-02-11 15:26:36 -08007365
7366#endif /* CONFIG_MEMCG_SWAP */