Merge tag 'erofs-for-6.8-rc3-fixes' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / mm / memcontrol.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
8cdea7c0
BS
2/* memcontrol.c - Memory Controller
3 *
4 * Copyright IBM Corporation, 2007
5 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
6 *
78fb7466
PE
7 * Copyright 2007 OpenVZ SWsoft Inc
8 * Author: Pavel Emelianov <xemul@openvz.org>
9 *
2e72b634
KS
10 * Memory thresholds
11 * Copyright (C) 2009 Nokia Corporation
12 * Author: Kirill A. Shutemov
13 *
7ae1e1d0
GC
14 * Kernel Memory Controller
15 * Copyright (C) 2012 Parallels Inc. and Google Inc.
16 * Authors: Glauber Costa and Suleiman Souhlal
17 *
1575e68b
JW
18 * 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
6168d0da
AS
23 *
24 * Per memcg lru locking
25 * Copyright (C) 2020 Alibaba, Inc, Alex Shi
8cdea7c0
BS
26 */
27
3e32cb2e 28#include <linux/page_counter.h>
8cdea7c0
BS
29#include <linux/memcontrol.h>
30#include <linux/cgroup.h>
a520110e 31#include <linux/pagewalk.h>
6e84f315 32#include <linux/sched/mm.h>
3a4f8a0b 33#include <linux/shmem_fs.h>
4ffef5fe 34#include <linux/hugetlb.h>
d13d1443 35#include <linux/pagemap.h>
1ff9e6e1 36#include <linux/vm_event_item.h>
d52aa412 37#include <linux/smp.h>
8a9f3ccd 38#include <linux/page-flags.h>
66e1707b 39#include <linux/backing-dev.h>
8a9f3ccd
BS
40#include <linux/bit_spinlock.h>
41#include <linux/rcupdate.h>
e222432b 42#include <linux/limits.h>
b9e15baf 43#include <linux/export.h>
8c7c6e34 44#include <linux/mutex.h>
bb4cc1a8 45#include <linux/rbtree.h>
b6ac57d5 46#include <linux/slab.h>
66e1707b 47#include <linux/swap.h>
02491447 48#include <linux/swapops.h>
66e1707b 49#include <linux/spinlock.h>
2e72b634 50#include <linux/eventfd.h>
79bd9814 51#include <linux/poll.h>
2e72b634 52#include <linux/sort.h>
66e1707b 53#include <linux/fs.h>
d2ceb9b7 54#include <linux/seq_file.h>
70ddf637 55#include <linux/vmpressure.h>
dc90f084 56#include <linux/memremap.h>
b69408e8 57#include <linux/mm_inline.h>
5d1ea48b 58#include <linux/swap_cgroup.h>
cdec2e42 59#include <linux/cpu.h>
158e0a2d 60#include <linux/oom.h>
0056f4e6 61#include <linux/lockdep.h>
79bd9814 62#include <linux/file.h>
03248add 63#include <linux/resume_user_mode.h>
0e4b01df 64#include <linux/psi.h>
c8713d0b 65#include <linux/seq_buf.h>
6a792697 66#include <linux/sched/isolation.h>
6011be59 67#include <linux/kmemleak.h>
08e552c6 68#include "internal.h"
d1a4c0b3 69#include <net/sock.h>
4bd2c1ee 70#include <net/ip.h>
f35c3a8e 71#include "slab.h"
014bb1de 72#include "swap.h"
8cdea7c0 73
7c0f6ba6 74#include <linux/uaccess.h>
8697d331 75
cc8e970c
KM
76#include <trace/events/vmscan.h>
77
073219e9
TH
78struct cgroup_subsys memory_cgrp_subsys __read_mostly;
79EXPORT_SYMBOL(memory_cgrp_subsys);
68ae564b 80
7d828602
JW
81struct mem_cgroup *root_mem_cgroup __read_mostly;
82
37d5985c
RG
83/* Active memory cgroup to use from an interrupt context */
84DEFINE_PER_CPU(struct mem_cgroup *, int_active_memcg);
c74d40e8 85EXPORT_PER_CPU_SYMBOL_GPL(int_active_memcg);
37d5985c 86
f7e1cb6e 87/* Socket memory accounting disabled? */
0f0cace3 88static bool cgroup_memory_nosocket __ro_after_init;
f7e1cb6e 89
04823c83 90/* Kernel memory accounting disabled? */
17c17367 91static bool cgroup_memory_nokmem __ro_after_init;
04823c83 92
b6c1a8af
YS
93/* BPF memory accounting disabled? */
94static bool cgroup_memory_nobpf __ro_after_init;
95
97b27821
TH
96#ifdef CONFIG_CGROUP_WRITEBACK
97static DECLARE_WAIT_QUEUE_HEAD(memcg_cgwb_frn_waitq);
98#endif
99
7941d214
JW
100/* Whether legacy memory+swap accounting is active */
101static bool do_memsw_account(void)
102{
b25806dc 103 return !cgroup_subsys_on_dfl(memory_cgrp_subsys);
7941d214
JW
104}
105
a0db00fc
KS
106#define THRESHOLDS_EVENTS_TARGET 128
107#define SOFTLIMIT_EVENTS_TARGET 1024
e9f8974f 108
bb4cc1a8
AM
109/*
110 * Cgroups above their limits are maintained in a RB-Tree, independent of
111 * their hierarchy representation
112 */
113
ef8f2327 114struct mem_cgroup_tree_per_node {
bb4cc1a8 115 struct rb_root rb_root;
fa90b2fd 116 struct rb_node *rb_rightmost;
bb4cc1a8
AM
117 spinlock_t lock;
118};
119
bb4cc1a8
AM
120struct mem_cgroup_tree {
121 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
122};
123
124static struct mem_cgroup_tree soft_limit_tree __read_mostly;
125
9490ff27
KH
126/* for OOM */
127struct mem_cgroup_eventfd_list {
128 struct list_head list;
129 struct eventfd_ctx *eventfd;
130};
2e72b634 131
79bd9814
TH
132/*
133 * cgroup_event represents events which userspace want to receive.
134 */
3bc942f3 135struct mem_cgroup_event {
79bd9814 136 /*
59b6f873 137 * memcg which the event belongs to.
79bd9814 138 */
59b6f873 139 struct mem_cgroup *memcg;
79bd9814
TH
140 /*
141 * eventfd to signal userspace about the event.
142 */
143 struct eventfd_ctx *eventfd;
144 /*
145 * Each of these stored in a list by the cgroup.
146 */
147 struct list_head list;
fba94807
TH
148 /*
149 * register_event() callback will be used to add new userspace
150 * waiter for changes related to this event. Use eventfd_signal()
151 * on eventfd to send notification to userspace.
152 */
59b6f873 153 int (*register_event)(struct mem_cgroup *memcg,
347c4a87 154 struct eventfd_ctx *eventfd, const char *args);
fba94807
TH
155 /*
156 * unregister_event() callback will be called when userspace closes
157 * the eventfd or on cgroup removing. This callback must be set,
158 * if you want provide notification functionality.
159 */
59b6f873 160 void (*unregister_event)(struct mem_cgroup *memcg,
fba94807 161 struct eventfd_ctx *eventfd);
79bd9814
TH
162 /*
163 * All fields below needed to unregister event when
164 * userspace closes eventfd.
165 */
166 poll_table pt;
167 wait_queue_head_t *wqh;
ac6424b9 168 wait_queue_entry_t wait;
79bd9814
TH
169 struct work_struct remove;
170};
171
c0ff4b85
R
172static void mem_cgroup_threshold(struct mem_cgroup *memcg);
173static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
2e72b634 174
7dc74be0
DN
175/* Stuffs for move charges at task migration. */
176/*
1dfab5ab 177 * Types of charges to be moved.
7dc74be0 178 */
1dfab5ab
JW
179#define MOVE_ANON 0x1U
180#define MOVE_FILE 0x2U
181#define MOVE_MASK (MOVE_ANON | MOVE_FILE)
7dc74be0 182
4ffef5fe
DN
183/* "mc" and its members are protected by cgroup_mutex */
184static struct move_charge_struct {
b1dd693e 185 spinlock_t lock; /* for from, to */
264a0ae1 186 struct mm_struct *mm;
4ffef5fe
DN
187 struct mem_cgroup *from;
188 struct mem_cgroup *to;
1dfab5ab 189 unsigned long flags;
4ffef5fe 190 unsigned long precharge;
854ffa8d 191 unsigned long moved_charge;
483c30b5 192 unsigned long moved_swap;
8033b97c
DN
193 struct task_struct *moving_task; /* a task moving charges */
194 wait_queue_head_t waitq; /* a waitq for other context */
195} mc = {
2bd9bb20 196 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
8033b97c
DN
197 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
198};
4ffef5fe 199
4e416953 200/*
f4d005af 201 * Maximum loops in mem_cgroup_soft_reclaim(), used for soft
4e416953
BS
202 * limit reclaim to prevent infinite loops, if they ever occur.
203 */
a0db00fc 204#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
bb4cc1a8 205#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
4e416953 206
8c7c6e34 207/* for encoding cft->private value on file */
86ae53e1
GC
208enum res_type {
209 _MEM,
210 _MEMSWAP,
510fc4e1 211 _KMEM,
d55f90bf 212 _TCP,
86ae53e1
GC
213};
214
a0db00fc
KS
215#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
216#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
8c7c6e34
KH
217#define MEMFILE_ATTR(val) ((val) & 0xffff)
218
b05706f1
KT
219/*
220 * Iteration constructs for visiting all cgroups (under a tree). If
221 * loops are exited prematurely (break), mem_cgroup_iter_break() must
222 * be used for reference counting.
223 */
224#define for_each_mem_cgroup_tree(iter, root) \
225 for (iter = mem_cgroup_iter(root, NULL, NULL); \
226 iter != NULL; \
227 iter = mem_cgroup_iter(root, iter, NULL))
228
229#define for_each_mem_cgroup(iter) \
230 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
231 iter != NULL; \
232 iter = mem_cgroup_iter(NULL, iter, NULL))
233
a4ebf1b6 234static inline bool task_is_dying(void)
7775face
TH
235{
236 return tsk_is_oom_victim(current) || fatal_signal_pending(current) ||
237 (current->flags & PF_EXITING);
238}
239
70ddf637
AV
240/* Some nice accessors for the vmpressure. */
241struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
242{
243 if (!memcg)
244 memcg = root_mem_cgroup;
245 return &memcg->vmpressure;
246}
247
9647875b 248struct mem_cgroup *vmpressure_to_memcg(struct vmpressure *vmpr)
70ddf637 249{
9647875b 250 return container_of(vmpr, struct mem_cgroup, vmpressure);
70ddf637
AV
251}
252
1aacbd35
RG
253#define CURRENT_OBJCG_UPDATE_BIT 0
254#define CURRENT_OBJCG_UPDATE_FLAG (1UL << CURRENT_OBJCG_UPDATE_BIT)
255
84c07d11 256#ifdef CONFIG_MEMCG_KMEM
0764db9b 257static DEFINE_SPINLOCK(objcg_lock);
bf4f0599 258
4d5c8aed
RG
259bool mem_cgroup_kmem_disabled(void)
260{
261 return cgroup_memory_nokmem;
262}
263
f1286fae
MS
264static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg,
265 unsigned int nr_pages);
c1a660de 266
bf4f0599
RG
267static void obj_cgroup_release(struct percpu_ref *ref)
268{
269 struct obj_cgroup *objcg = container_of(ref, struct obj_cgroup, refcnt);
bf4f0599
RG
270 unsigned int nr_bytes;
271 unsigned int nr_pages;
272 unsigned long flags;
273
274 /*
275 * At this point all allocated objects are freed, and
276 * objcg->nr_charged_bytes can't have an arbitrary byte value.
277 * However, it can be PAGE_SIZE or (x * PAGE_SIZE).
278 *
279 * The following sequence can lead to it:
280 * 1) CPU0: objcg == stock->cached_objcg
281 * 2) CPU1: we do a small allocation (e.g. 92 bytes),
282 * PAGE_SIZE bytes are charged
283 * 3) CPU1: a process from another memcg is allocating something,
284 * the stock if flushed,
285 * objcg->nr_charged_bytes = PAGE_SIZE - 92
286 * 5) CPU0: we do release this object,
287 * 92 bytes are added to stock->nr_bytes
288 * 6) CPU0: stock is flushed,
289 * 92 bytes are added to objcg->nr_charged_bytes
290 *
291 * In the result, nr_charged_bytes == PAGE_SIZE.
292 * This page will be uncharged in obj_cgroup_release().
293 */
294 nr_bytes = atomic_read(&objcg->nr_charged_bytes);
295 WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1));
296 nr_pages = nr_bytes >> PAGE_SHIFT;
297
bf4f0599 298 if (nr_pages)
f1286fae 299 obj_cgroup_uncharge_pages(objcg, nr_pages);
271dd6b1 300
0764db9b 301 spin_lock_irqsave(&objcg_lock, flags);
bf4f0599 302 list_del(&objcg->list);
0764db9b 303 spin_unlock_irqrestore(&objcg_lock, flags);
bf4f0599
RG
304
305 percpu_ref_exit(ref);
306 kfree_rcu(objcg, rcu);
307}
308
309static struct obj_cgroup *obj_cgroup_alloc(void)
310{
311 struct obj_cgroup *objcg;
312 int ret;
313
314 objcg = kzalloc(sizeof(struct obj_cgroup), GFP_KERNEL);
315 if (!objcg)
316 return NULL;
317
318 ret = percpu_ref_init(&objcg->refcnt, obj_cgroup_release, 0,
319 GFP_KERNEL);
320 if (ret) {
321 kfree(objcg);
322 return NULL;
323 }
324 INIT_LIST_HEAD(&objcg->list);
325 return objcg;
326}
327
328static void memcg_reparent_objcgs(struct mem_cgroup *memcg,
329 struct mem_cgroup *parent)
330{
331 struct obj_cgroup *objcg, *iter;
332
333 objcg = rcu_replace_pointer(memcg->objcg, NULL, true);
334
0764db9b 335 spin_lock_irq(&objcg_lock);
bf4f0599 336
9838354e
MS
337 /* 1) Ready to reparent active objcg. */
338 list_add(&objcg->list, &memcg->objcg_list);
339 /* 2) Reparent active objcg and already reparented objcgs to parent. */
340 list_for_each_entry(iter, &memcg->objcg_list, list)
341 WRITE_ONCE(iter->memcg, parent);
342 /* 3) Move already reparented objcgs to the parent's list */
bf4f0599
RG
343 list_splice(&memcg->objcg_list, &parent->objcg_list);
344
0764db9b 345 spin_unlock_irq(&objcg_lock);
bf4f0599
RG
346
347 percpu_ref_kill(&objcg->refcnt);
348}
349
d7f25f8a
GC
350/*
351 * A lot of the calls to the cache allocation functions are expected to be
272911a4 352 * inlined by the compiler. Since the calls to memcg_slab_pre_alloc_hook() are
d7f25f8a
GC
353 * conditional to this static branch, we'll have to allow modules that does
354 * kmem_cache_alloc and the such to see this symbol as well
355 */
f7a449f7
RG
356DEFINE_STATIC_KEY_FALSE(memcg_kmem_online_key);
357EXPORT_SYMBOL(memcg_kmem_online_key);
b6c1a8af
YS
358
359DEFINE_STATIC_KEY_FALSE(memcg_bpf_enabled_key);
360EXPORT_SYMBOL(memcg_bpf_enabled_key);
0a432dcb 361#endif
17cc4dfe 362
ad7fa852 363/**
75376c6f
MWO
364 * mem_cgroup_css_from_folio - css of the memcg associated with a folio
365 * @folio: folio of interest
ad7fa852
TH
366 *
367 * If memcg is bound to the default hierarchy, css of the memcg associated
75376c6f 368 * with @folio is returned. The returned css remains associated with @folio
ad7fa852
TH
369 * until it is released.
370 *
371 * If memcg is bound to a traditional hierarchy, the css of root_mem_cgroup
372 * is returned.
ad7fa852 373 */
75376c6f 374struct cgroup_subsys_state *mem_cgroup_css_from_folio(struct folio *folio)
ad7fa852 375{
75376c6f 376 struct mem_cgroup *memcg = folio_memcg(folio);
ad7fa852 377
9e10a130 378 if (!memcg || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
ad7fa852
TH
379 memcg = root_mem_cgroup;
380
ad7fa852
TH
381 return &memcg->css;
382}
383
2fc04524
VD
384/**
385 * page_cgroup_ino - return inode number of the memcg a page is charged to
386 * @page: the page
387 *
388 * Look up the closest online ancestor of the memory cgroup @page is charged to
389 * and return its inode number or 0 if @page is not charged to any cgroup. It
390 * is safe to call this function without holding a reference to @page.
391 *
392 * Note, this function is inherently racy, because there is nothing to prevent
393 * the cgroup inode from getting torn down and potentially reallocated a moment
394 * after page_cgroup_ino() returns, so it only should be used by callers that
395 * do not care (such as procfs interfaces).
396 */
397ino_t page_cgroup_ino(struct page *page)
398{
399 struct mem_cgroup *memcg;
400 unsigned long ino = 0;
401
402 rcu_read_lock();
ec342603
YA
403 /* page_folio() is racy here, but the entire function is racy anyway */
404 memcg = folio_memcg_check(page_folio(page));
286e04b8 405
2fc04524
VD
406 while (memcg && !(memcg->css.flags & CSS_ONLINE))
407 memcg = parent_mem_cgroup(memcg);
408 if (memcg)
409 ino = cgroup_ino(memcg->css.cgroup);
410 rcu_read_unlock();
411 return ino;
412}
413
ef8f2327
MG
414static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_node *mz,
415 struct mem_cgroup_tree_per_node *mctz,
3e32cb2e 416 unsigned long new_usage_in_excess)
bb4cc1a8
AM
417{
418 struct rb_node **p = &mctz->rb_root.rb_node;
419 struct rb_node *parent = NULL;
ef8f2327 420 struct mem_cgroup_per_node *mz_node;
fa90b2fd 421 bool rightmost = true;
bb4cc1a8
AM
422
423 if (mz->on_tree)
424 return;
425
426 mz->usage_in_excess = new_usage_in_excess;
427 if (!mz->usage_in_excess)
428 return;
429 while (*p) {
430 parent = *p;
ef8f2327 431 mz_node = rb_entry(parent, struct mem_cgroup_per_node,
bb4cc1a8 432 tree_node);
fa90b2fd 433 if (mz->usage_in_excess < mz_node->usage_in_excess) {
bb4cc1a8 434 p = &(*p)->rb_left;
fa90b2fd 435 rightmost = false;
378876b0 436 } else {
bb4cc1a8 437 p = &(*p)->rb_right;
378876b0 438 }
bb4cc1a8 439 }
fa90b2fd
DB
440
441 if (rightmost)
442 mctz->rb_rightmost = &mz->tree_node;
443
bb4cc1a8
AM
444 rb_link_node(&mz->tree_node, parent, p);
445 rb_insert_color(&mz->tree_node, &mctz->rb_root);
446 mz->on_tree = true;
447}
448
ef8f2327
MG
449static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz,
450 struct mem_cgroup_tree_per_node *mctz)
bb4cc1a8
AM
451{
452 if (!mz->on_tree)
453 return;
fa90b2fd
DB
454
455 if (&mz->tree_node == mctz->rb_rightmost)
456 mctz->rb_rightmost = rb_prev(&mz->tree_node);
457
bb4cc1a8
AM
458 rb_erase(&mz->tree_node, &mctz->rb_root);
459 mz->on_tree = false;
460}
461
ef8f2327
MG
462static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz,
463 struct mem_cgroup_tree_per_node *mctz)
bb4cc1a8 464{
0a31bc97
JW
465 unsigned long flags;
466
467 spin_lock_irqsave(&mctz->lock, flags);
cf2c8127 468 __mem_cgroup_remove_exceeded(mz, mctz);
0a31bc97 469 spin_unlock_irqrestore(&mctz->lock, flags);
bb4cc1a8
AM
470}
471
3e32cb2e
JW
472static unsigned long soft_limit_excess(struct mem_cgroup *memcg)
473{
474 unsigned long nr_pages = page_counter_read(&memcg->memory);
4db0c3c2 475 unsigned long soft_limit = READ_ONCE(memcg->soft_limit);
3e32cb2e
JW
476 unsigned long excess = 0;
477
478 if (nr_pages > soft_limit)
479 excess = nr_pages - soft_limit;
480
481 return excess;
482}
bb4cc1a8 483
658b69c9 484static void mem_cgroup_update_tree(struct mem_cgroup *memcg, int nid)
bb4cc1a8 485{
3e32cb2e 486 unsigned long excess;
ef8f2327
MG
487 struct mem_cgroup_per_node *mz;
488 struct mem_cgroup_tree_per_node *mctz;
bb4cc1a8 489
e4dde56c 490 if (lru_gen_enabled()) {
36c7b4db 491 if (soft_limit_excess(memcg))
5c7e7a0d 492 lru_gen_soft_reclaim(memcg, nid);
e4dde56c
YZ
493 return;
494 }
495
2ab082ba 496 mctz = soft_limit_tree.rb_tree_per_node[nid];
bfc7228b
LD
497 if (!mctz)
498 return;
bb4cc1a8
AM
499 /*
500 * Necessary to update all ancestors when hierarchy is used.
501 * because their event counter is not touched.
502 */
503 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
658b69c9 504 mz = memcg->nodeinfo[nid];
3e32cb2e 505 excess = soft_limit_excess(memcg);
bb4cc1a8
AM
506 /*
507 * We have to update the tree if mz is on RB-tree or
508 * mem is over its softlimit.
509 */
510 if (excess || mz->on_tree) {
0a31bc97
JW
511 unsigned long flags;
512
513 spin_lock_irqsave(&mctz->lock, flags);
bb4cc1a8
AM
514 /* if on-tree, remove it */
515 if (mz->on_tree)
cf2c8127 516 __mem_cgroup_remove_exceeded(mz, mctz);
bb4cc1a8
AM
517 /*
518 * Insert again. mz->usage_in_excess will be updated.
519 * If excess is 0, no tree ops.
520 */
cf2c8127 521 __mem_cgroup_insert_exceeded(mz, mctz, excess);
0a31bc97 522 spin_unlock_irqrestore(&mctz->lock, flags);
bb4cc1a8
AM
523 }
524 }
525}
526
527static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
528{
ef8f2327
MG
529 struct mem_cgroup_tree_per_node *mctz;
530 struct mem_cgroup_per_node *mz;
531 int nid;
bb4cc1a8 532
e231875b 533 for_each_node(nid) {
a3747b53 534 mz = memcg->nodeinfo[nid];
2ab082ba 535 mctz = soft_limit_tree.rb_tree_per_node[nid];
bfc7228b
LD
536 if (mctz)
537 mem_cgroup_remove_exceeded(mz, mctz);
bb4cc1a8
AM
538 }
539}
540
ef8f2327
MG
541static struct mem_cgroup_per_node *
542__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz)
bb4cc1a8 543{
ef8f2327 544 struct mem_cgroup_per_node *mz;
bb4cc1a8
AM
545
546retry:
547 mz = NULL;
fa90b2fd 548 if (!mctz->rb_rightmost)
bb4cc1a8
AM
549 goto done; /* Nothing to reclaim from */
550
fa90b2fd
DB
551 mz = rb_entry(mctz->rb_rightmost,
552 struct mem_cgroup_per_node, tree_node);
bb4cc1a8
AM
553 /*
554 * Remove the node now but someone else can add it back,
555 * we will to add it back at the end of reclaim to its correct
556 * position in the tree.
557 */
cf2c8127 558 __mem_cgroup_remove_exceeded(mz, mctz);
3e32cb2e 559 if (!soft_limit_excess(mz->memcg) ||
8965aa28 560 !css_tryget(&mz->memcg->css))
bb4cc1a8
AM
561 goto retry;
562done:
563 return mz;
564}
565
ef8f2327
MG
566static struct mem_cgroup_per_node *
567mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz)
bb4cc1a8 568{
ef8f2327 569 struct mem_cgroup_per_node *mz;
bb4cc1a8 570
0a31bc97 571 spin_lock_irq(&mctz->lock);
bb4cc1a8 572 mz = __mem_cgroup_largest_soft_limit_node(mctz);
0a31bc97 573 spin_unlock_irq(&mctz->lock);
bb4cc1a8
AM
574 return mz;
575}
576
d396def5
SB
577/* Subset of vm_event_item to report for memcg event stats */
578static const unsigned int memcg_vm_event_stat[] = {
8278f1c7
SB
579 PGPGIN,
580 PGPGOUT,
d396def5
SB
581 PGSCAN_KSWAPD,
582 PGSCAN_DIRECT,
57e9cc50 583 PGSCAN_KHUGEPAGED,
d396def5
SB
584 PGSTEAL_KSWAPD,
585 PGSTEAL_DIRECT,
57e9cc50 586 PGSTEAL_KHUGEPAGED,
d396def5
SB
587 PGFAULT,
588 PGMAJFAULT,
589 PGREFILL,
590 PGACTIVATE,
591 PGDEACTIVATE,
592 PGLAZYFREE,
593 PGLAZYFREED,
594#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
595 ZSWPIN,
596 ZSWPOUT,
e0bf1dc8 597 ZSWPWB,
d396def5
SB
598#endif
599#ifdef CONFIG_TRANSPARENT_HUGEPAGE
600 THP_FAULT_ALLOC,
601 THP_COLLAPSE_ALLOC,
811244a5
XH
602 THP_SWPOUT,
603 THP_SWPOUT_FALLBACK,
d396def5
SB
604#endif
605};
606
8278f1c7
SB
607#define NR_MEMCG_EVENTS ARRAY_SIZE(memcg_vm_event_stat)
608static int mem_cgroup_events_index[NR_VM_EVENT_ITEMS] __read_mostly;
609
610static void init_memcg_events(void)
611{
612 int i;
613
614 for (i = 0; i < NR_MEMCG_EVENTS; ++i)
615 mem_cgroup_events_index[memcg_vm_event_stat[i]] = i + 1;
616}
617
618static inline int memcg_events_index(enum vm_event_item idx)
619{
620 return mem_cgroup_events_index[idx] - 1;
621}
622
410f8e82
SB
623struct memcg_vmstats_percpu {
624 /* Local (CPU and cgroup) page state & events */
625 long state[MEMCG_NR_STAT];
8278f1c7 626 unsigned long events[NR_MEMCG_EVENTS];
410f8e82
SB
627
628 /* Delta calculation for lockless upward propagation */
629 long state_prev[MEMCG_NR_STAT];
8278f1c7 630 unsigned long events_prev[NR_MEMCG_EVENTS];
410f8e82
SB
631
632 /* Cgroup1: threshold notifications & softlimit tree updates */
633 unsigned long nr_page_events;
634 unsigned long targets[MEM_CGROUP_NTARGETS];
8d59d221
YA
635
636 /* Stats updates since the last flush */
637 unsigned int stats_updates;
410f8e82
SB
638};
639
640struct memcg_vmstats {
641 /* Aggregated (CPU and subtree) page state & events */
642 long state[MEMCG_NR_STAT];
8278f1c7 643 unsigned long events[NR_MEMCG_EVENTS];
410f8e82 644
f82e6bf9
YA
645 /* Non-hierarchical (CPU aggregated) page state & events */
646 long state_local[MEMCG_NR_STAT];
647 unsigned long events_local[NR_MEMCG_EVENTS];
648
410f8e82
SB
649 /* Pending child counts during tree propagation */
650 long state_pending[MEMCG_NR_STAT];
8278f1c7 651 unsigned long events_pending[NR_MEMCG_EVENTS];
8d59d221
YA
652
653 /* Stats updates since the last flush */
654 atomic64_t stats_updates;
410f8e82
SB
655};
656
11192d9c
SB
657/*
658 * memcg and lruvec stats flushing
659 *
660 * Many codepaths leading to stats update or read are performance sensitive and
661 * adding stats flushing in such codepaths is not desirable. So, to optimize the
662 * flushing the kernel does:
663 *
664 * 1) Periodically and asynchronously flush the stats every 2 seconds to not let
665 * rstat update tree grow unbounded.
666 *
667 * 2) Flush the stats synchronously on reader side only when there are more than
668 * (MEMCG_CHARGE_BATCH * nr_cpus) update events. Though this optimization
669 * will let stats be out of sync by atmost (MEMCG_CHARGE_BATCH * nr_cpus) but
670 * only for 2 seconds due to (1).
671 */
672static void flush_memcg_stats_dwork(struct work_struct *w);
673static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork);
508bed88 674static u64 flush_last_time;
9b301615
SB
675
676#define FLUSH_TIME (2UL*HZ)
11192d9c 677
be3e67b5
SAS
678/*
679 * Accessors to ensure that preemption is disabled on PREEMPT_RT because it can
680 * not rely on this as part of an acquired spinlock_t lock. These functions are
681 * never used in hardirq context on PREEMPT_RT and therefore disabling preemtion
682 * is sufficient.
683 */
684static void memcg_stats_lock(void)
685{
e575d401
TG
686 preempt_disable_nested();
687 VM_WARN_ON_IRQS_ENABLED();
be3e67b5
SAS
688}
689
690static void __memcg_stats_lock(void)
691{
e575d401 692 preempt_disable_nested();
be3e67b5
SAS
693}
694
695static void memcg_stats_unlock(void)
696{
e575d401 697 preempt_enable_nested();
be3e67b5
SAS
698}
699
8d59d221
YA
700
701static bool memcg_should_flush_stats(struct mem_cgroup *memcg)
702{
703 return atomic64_read(&memcg->vmstats->stats_updates) >
704 MEMCG_CHARGE_BATCH * num_online_cpus();
705}
706
5b3be698 707static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val)
11192d9c 708{
8d59d221 709 int cpu = smp_processor_id();
5b3be698
SB
710 unsigned int x;
711
f9d911ca
YA
712 if (!val)
713 return;
714
8d59d221
YA
715 cgroup_rstat_updated(memcg->css.cgroup, cpu);
716
717 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
718 x = __this_cpu_add_return(memcg->vmstats_percpu->stats_updates,
719 abs(val));
720
721 if (x < MEMCG_CHARGE_BATCH)
722 continue;
5b3be698 723
873f64b7 724 /*
8d59d221
YA
725 * If @memcg is already flush-able, increasing stats_updates is
726 * redundant. Avoid the overhead of the atomic update.
873f64b7 727 */
8d59d221
YA
728 if (!memcg_should_flush_stats(memcg))
729 atomic64_add(x, &memcg->vmstats->stats_updates);
730 __this_cpu_write(memcg->vmstats_percpu->stats_updates, 0);
5b3be698 731 }
11192d9c
SB
732}
733
7d7ef0a4 734static void do_flush_stats(struct mem_cgroup *memcg)
11192d9c 735{
7d7ef0a4
YA
736 if (mem_cgroup_is_root(memcg))
737 WRITE_ONCE(flush_last_time, jiffies_64);
9fad9aee 738
7d7ef0a4 739 cgroup_rstat_flush(memcg->css.cgroup);
11192d9c
SB
740}
741
7d7ef0a4
YA
742/*
743 * mem_cgroup_flush_stats - flush the stats of a memory cgroup subtree
744 * @memcg: root of the subtree to flush
745 *
746 * Flushing is serialized by the underlying global rstat lock. There is also a
747 * minimum amount of work to be done even if there are no stat updates to flush.
748 * Hence, we only flush the stats if the updates delta exceeds a threshold. This
749 * avoids unnecessary work and contention on the underlying lock.
750 */
751void mem_cgroup_flush_stats(struct mem_cgroup *memcg)
11192d9c 752{
7d7ef0a4
YA
753 if (mem_cgroup_disabled())
754 return;
755
756 if (!memcg)
757 memcg = root_mem_cgroup;
758
759 if (memcg_should_flush_stats(memcg))
760 do_flush_stats(memcg);
9fad9aee
YA
761}
762
7d7ef0a4 763void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg)
9b301615 764{
508bed88
YA
765 /* Only flush if the periodic flusher is one full cycle late */
766 if (time_after64(jiffies_64, READ_ONCE(flush_last_time) + 2*FLUSH_TIME))
7d7ef0a4 767 mem_cgroup_flush_stats(memcg);
9b301615
SB
768}
769
11192d9c
SB
770static void flush_memcg_stats_dwork(struct work_struct *w)
771{
9fad9aee 772 /*
8d59d221
YA
773 * Deliberately ignore memcg_should_flush_stats() here so that flushing
774 * in latency-sensitive paths is as cheap as possible.
9fad9aee 775 */
7d7ef0a4 776 do_flush_stats(root_mem_cgroup);
9b301615 777 queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME);
11192d9c
SB
778}
779
410f8e82
SB
780unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
781{
782 long x = READ_ONCE(memcg->vmstats->state[idx]);
783#ifdef CONFIG_SMP
784 if (x < 0)
785 x = 0;
786#endif
787 return x;
788}
789
7bd5bc3c
YA
790static int memcg_page_state_unit(int item);
791
792/*
793 * Normalize the value passed into memcg_rstat_updated() to be in pages. Round
794 * up non-zero sub-page updates to 1 page as zero page updates are ignored.
795 */
796static int memcg_state_val_in_pages(int idx, int val)
797{
798 int unit = memcg_page_state_unit(idx);
799
800 if (!val || unit == PAGE_SIZE)
801 return val;
802 else
803 return max(val * unit / PAGE_SIZE, 1UL);
804}
805
db9adbcb
JW
806/**
807 * __mod_memcg_state - update cgroup memory statistics
808 * @memcg: the memory cgroup
809 * @idx: the stat item - can be enum memcg_stat_item or enum node_stat_item
810 * @val: delta to add to the counter, can be negative
811 */
812void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val)
813{
db9adbcb
JW
814 if (mem_cgroup_disabled())
815 return;
816
2d146aa3 817 __this_cpu_add(memcg->vmstats_percpu->state[idx], val);
7bd5bc3c 818 memcg_rstat_updated(memcg, memcg_state_val_in_pages(idx, val));
db9adbcb
JW
819}
820
2d146aa3 821/* idx can be of type enum memcg_stat_item or node_stat_item. */
a18e6e6e
JW
822static unsigned long memcg_page_state_local(struct mem_cgroup *memcg, int idx)
823{
f82e6bf9 824 long x = READ_ONCE(memcg->vmstats->state_local[idx]);
a18e6e6e 825
a18e6e6e
JW
826#ifdef CONFIG_SMP
827 if (x < 0)
828 x = 0;
829#endif
830 return x;
831}
832
eedc4e5a
RG
833void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
834 int val)
db9adbcb
JW
835{
836 struct mem_cgroup_per_node *pn;
42a30035 837 struct mem_cgroup *memcg;
db9adbcb 838
db9adbcb 839 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
42a30035 840 memcg = pn->memcg;
db9adbcb 841
be3e67b5 842 /*
be16dd76 843 * The caller from rmap relies on disabled preemption because they never
be3e67b5
SAS
844 * update their counter from in-interrupt context. For these two
845 * counters we check that the update is never performed from an
846 * interrupt context while other caller need to have disabled interrupt.
847 */
848 __memcg_stats_lock();
e575d401 849 if (IS_ENABLED(CONFIG_DEBUG_VM)) {
be3e67b5
SAS
850 switch (idx) {
851 case NR_ANON_MAPPED:
852 case NR_FILE_MAPPED:
853 case NR_ANON_THPS:
854 case NR_SHMEM_PMDMAPPED:
855 case NR_FILE_PMDMAPPED:
856 WARN_ON_ONCE(!in_task());
857 break;
858 default:
e575d401 859 VM_WARN_ON_IRQS_ENABLED();
be3e67b5
SAS
860 }
861 }
862
db9adbcb 863 /* Update memcg */
11192d9c 864 __this_cpu_add(memcg->vmstats_percpu->state[idx], val);
db9adbcb 865
b4c46484 866 /* Update lruvec */
7e1c0d6f 867 __this_cpu_add(pn->lruvec_stats_percpu->state[idx], val);
11192d9c 868
7bd5bc3c 869 memcg_rstat_updated(memcg, memcg_state_val_in_pages(idx, val));
be3e67b5 870 memcg_stats_unlock();
db9adbcb
JW
871}
872
eedc4e5a
RG
873/**
874 * __mod_lruvec_state - update lruvec memory statistics
875 * @lruvec: the lruvec
876 * @idx: the stat item
877 * @val: delta to add to the counter, can be negative
878 *
879 * The lruvec is the intersection of the NUMA node and a cgroup. This
880 * function updates the all three counters that are affected by a
881 * change of state at this level: per-node, per-cgroup, per-lruvec.
882 */
883void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
884 int val)
885{
886 /* Update node */
887 __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
888
889 /* Update memcg and lruvec */
890 if (!mem_cgroup_disabled())
891 __mod_memcg_lruvec_state(lruvec, idx, val);
892}
893
c701123b 894void __lruvec_stat_mod_folio(struct folio *folio, enum node_stat_item idx,
c47d5032
SB
895 int val)
896{
b4e0b68f 897 struct mem_cgroup *memcg;
c701123b 898 pg_data_t *pgdat = folio_pgdat(folio);
c47d5032
SB
899 struct lruvec *lruvec;
900
b4e0b68f 901 rcu_read_lock();
c701123b 902 memcg = folio_memcg(folio);
c47d5032 903 /* Untracked pages have no memcg, no lruvec. Update only the node */
d635a69d 904 if (!memcg) {
b4e0b68f 905 rcu_read_unlock();
c47d5032
SB
906 __mod_node_page_state(pgdat, idx, val);
907 return;
908 }
909
d635a69d 910 lruvec = mem_cgroup_lruvec(memcg, pgdat);
c47d5032 911 __mod_lruvec_state(lruvec, idx, val);
b4e0b68f 912 rcu_read_unlock();
c47d5032 913}
c701123b 914EXPORT_SYMBOL(__lruvec_stat_mod_folio);
c47d5032 915
da3ceeff 916void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val)
ec9f0238 917{
4f103c63 918 pg_data_t *pgdat = page_pgdat(virt_to_page(p));
ec9f0238
RG
919 struct mem_cgroup *memcg;
920 struct lruvec *lruvec;
921
922 rcu_read_lock();
fc4db90f 923 memcg = mem_cgroup_from_slab_obj(p);
ec9f0238 924
8faeb1ff
MS
925 /*
926 * Untracked pages have no memcg, no lruvec. Update only the
927 * node. If we reparent the slab objects to the root memcg,
928 * when we free the slab object, we need to update the per-memcg
929 * vmstats to keep it correct for the root memcg.
930 */
931 if (!memcg) {
ec9f0238
RG
932 __mod_node_page_state(pgdat, idx, val);
933 } else {
867e5e1d 934 lruvec = mem_cgroup_lruvec(memcg, pgdat);
ec9f0238
RG
935 __mod_lruvec_state(lruvec, idx, val);
936 }
937 rcu_read_unlock();
938}
939
db9adbcb
JW
940/**
941 * __count_memcg_events - account VM events in a cgroup
942 * @memcg: the memory cgroup
943 * @idx: the event item
f0953a1b 944 * @count: the number of events that occurred
db9adbcb
JW
945 */
946void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
947 unsigned long count)
948{
8278f1c7
SB
949 int index = memcg_events_index(idx);
950
951 if (mem_cgroup_disabled() || index < 0)
db9adbcb
JW
952 return;
953
be3e67b5 954 memcg_stats_lock();
8278f1c7 955 __this_cpu_add(memcg->vmstats_percpu->events[index], count);
5b3be698 956 memcg_rstat_updated(memcg, count);
be3e67b5 957 memcg_stats_unlock();
db9adbcb
JW
958}
959
42a30035 960static unsigned long memcg_events(struct mem_cgroup *memcg, int event)
e9f8974f 961{
8278f1c7
SB
962 int index = memcg_events_index(event);
963
964 if (index < 0)
965 return 0;
966 return READ_ONCE(memcg->vmstats->events[index]);
e9f8974f
JW
967}
968
42a30035
JW
969static unsigned long memcg_events_local(struct mem_cgroup *memcg, int event)
970{
8278f1c7
SB
971 int index = memcg_events_index(event);
972
973 if (index < 0)
974 return 0;
815744d7 975
f82e6bf9 976 return READ_ONCE(memcg->vmstats->events_local[index]);
42a30035
JW
977}
978
c0ff4b85 979static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
3fba69a5 980 int nr_pages)
d52aa412 981{
e401f176
KH
982 /* pagein of a big page is an event. So, ignore page size */
983 if (nr_pages > 0)
c9019e9b 984 __count_memcg_events(memcg, PGPGIN, 1);
3751d604 985 else {
c9019e9b 986 __count_memcg_events(memcg, PGPGOUT, 1);
3751d604
KH
987 nr_pages = -nr_pages; /* for event */
988 }
e401f176 989
871789d4 990 __this_cpu_add(memcg->vmstats_percpu->nr_page_events, nr_pages);
6d12e2d8
KH
991}
992
f53d7ce3
JW
993static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
994 enum mem_cgroup_events_target target)
7a159cc9
JW
995{
996 unsigned long val, next;
997
871789d4
CD
998 val = __this_cpu_read(memcg->vmstats_percpu->nr_page_events);
999 next = __this_cpu_read(memcg->vmstats_percpu->targets[target]);
7a159cc9 1000 /* from time_after() in jiffies.h */
6a1a8b80 1001 if ((long)(next - val) < 0) {
f53d7ce3
JW
1002 switch (target) {
1003 case MEM_CGROUP_TARGET_THRESH:
1004 next = val + THRESHOLDS_EVENTS_TARGET;
1005 break;
bb4cc1a8
AM
1006 case MEM_CGROUP_TARGET_SOFTLIMIT:
1007 next = val + SOFTLIMIT_EVENTS_TARGET;
1008 break;
f53d7ce3
JW
1009 default:
1010 break;
1011 }
871789d4 1012 __this_cpu_write(memcg->vmstats_percpu->targets[target], next);
f53d7ce3 1013 return true;
7a159cc9 1014 }
f53d7ce3 1015 return false;
d2265e6f
KH
1016}
1017
1018/*
1019 * Check events in order.
1020 *
1021 */
8e88bd2d 1022static void memcg_check_events(struct mem_cgroup *memcg, int nid)
d2265e6f 1023{
2343e88d
SAS
1024 if (IS_ENABLED(CONFIG_PREEMPT_RT))
1025 return;
1026
d2265e6f 1027 /* threshold event is triggered in finer grain than soft limit */
f53d7ce3
JW
1028 if (unlikely(mem_cgroup_event_ratelimit(memcg,
1029 MEM_CGROUP_TARGET_THRESH))) {
bb4cc1a8 1030 bool do_softlimit;
f53d7ce3 1031
bb4cc1a8
AM
1032 do_softlimit = mem_cgroup_event_ratelimit(memcg,
1033 MEM_CGROUP_TARGET_SOFTLIMIT);
c0ff4b85 1034 mem_cgroup_threshold(memcg);
bb4cc1a8 1035 if (unlikely(do_softlimit))
8e88bd2d 1036 mem_cgroup_update_tree(memcg, nid);
0a31bc97 1037 }
d2265e6f
KH
1038}
1039
cf475ad2 1040struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
78fb7466 1041{
31a78f23
BS
1042 /*
1043 * mm_update_next_owner() may clear mm->owner to NULL
1044 * if it races with swapoff, page migration, etc.
1045 * So this can be called with p == NULL.
1046 */
1047 if (unlikely(!p))
1048 return NULL;
1049
073219e9 1050 return mem_cgroup_from_css(task_css(p, memory_cgrp_id));
78fb7466 1051}
33398cf2 1052EXPORT_SYMBOL(mem_cgroup_from_task);
78fb7466 1053
04f94e3f
DS
1054static __always_inline struct mem_cgroup *active_memcg(void)
1055{
55a68c82 1056 if (!in_task())
04f94e3f
DS
1057 return this_cpu_read(int_active_memcg);
1058 else
1059 return current->active_memcg;
1060}
1061
d46eb14b
SB
1062/**
1063 * get_mem_cgroup_from_mm: Obtain a reference on given mm_struct's memcg.
1064 * @mm: mm from which memcg should be extracted. It can be NULL.
1065 *
04f94e3f
DS
1066 * Obtain a reference on mm->memcg and returns it if successful. If mm
1067 * is NULL, then the memcg is chosen as follows:
1068 * 1) The active memcg, if set.
1069 * 2) current->mm->memcg, if available
1070 * 3) root memcg
1071 * If mem_cgroup is disabled, NULL is returned.
d46eb14b
SB
1072 */
1073struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
54595fe2 1074{
d46eb14b
SB
1075 struct mem_cgroup *memcg;
1076
1077 if (mem_cgroup_disabled())
1078 return NULL;
0b7f569e 1079
2884b6b7
MS
1080 /*
1081 * Page cache insertions can happen without an
1082 * actual mm context, e.g. during disk probing
1083 * on boot, loopback IO, acct() writes etc.
1084 *
1085 * No need to css_get on root memcg as the reference
1086 * counting is disabled on the root level in the
1087 * cgroup core. See CSS_NO_REF.
1088 */
04f94e3f
DS
1089 if (unlikely(!mm)) {
1090 memcg = active_memcg();
1091 if (unlikely(memcg)) {
1092 /* remote memcg must hold a ref */
1093 css_get(&memcg->css);
1094 return memcg;
1095 }
1096 mm = current->mm;
1097 if (unlikely(!mm))
1098 return root_mem_cgroup;
1099 }
2884b6b7 1100
54595fe2
KH
1101 rcu_read_lock();
1102 do {
2884b6b7
MS
1103 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1104 if (unlikely(!memcg))
df381975 1105 memcg = root_mem_cgroup;
00d484f3 1106 } while (!css_tryget(&memcg->css));
54595fe2 1107 rcu_read_unlock();
c0ff4b85 1108 return memcg;
54595fe2 1109}
d46eb14b
SB
1110EXPORT_SYMBOL(get_mem_cgroup_from_mm);
1111
4b569387
NP
1112/**
1113 * get_mem_cgroup_from_current - Obtain a reference on current task's memcg.
1114 */
1115struct mem_cgroup *get_mem_cgroup_from_current(void)
1116{
1117 struct mem_cgroup *memcg;
1118
1119 if (mem_cgroup_disabled())
1120 return NULL;
1121
1122again:
1123 rcu_read_lock();
1124 memcg = mem_cgroup_from_task(current);
1125 if (!css_tryget(&memcg->css)) {
1126 rcu_read_unlock();
1127 goto again;
1128 }
1129 rcu_read_unlock();
1130 return memcg;
1131}
1132
5660048c
JW
1133/**
1134 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1135 * @root: hierarchy root
1136 * @prev: previously returned memcg, NULL on first invocation
1137 * @reclaim: cookie for shared reclaim walks, NULL for full walks
1138 *
1139 * Returns references to children of the hierarchy below @root, or
1140 * @root itself, or %NULL after a full round-trip.
1141 *
1142 * Caller must pass the return value in @prev on subsequent
1143 * invocations for reference counting, or use mem_cgroup_iter_break()
1144 * to cancel a hierarchy walk before the round-trip is complete.
1145 *
05bdc520
ML
1146 * Reclaimers can specify a node in @reclaim to divide up the memcgs
1147 * in the hierarchy among all concurrent reclaimers operating on the
1148 * same node.
5660048c 1149 */
694fbc0f 1150struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
5660048c 1151 struct mem_cgroup *prev,
694fbc0f 1152 struct mem_cgroup_reclaim_cookie *reclaim)
14067bb3 1153{
3f649ab7 1154 struct mem_cgroup_reclaim_iter *iter;
5ac8fb31 1155 struct cgroup_subsys_state *css = NULL;
9f3a0d09 1156 struct mem_cgroup *memcg = NULL;
5ac8fb31 1157 struct mem_cgroup *pos = NULL;
711d3d2c 1158
694fbc0f
AM
1159 if (mem_cgroup_disabled())
1160 return NULL;
5660048c 1161
9f3a0d09
JW
1162 if (!root)
1163 root = root_mem_cgroup;
7d74b06f 1164
542f85f9 1165 rcu_read_lock();
5f578161 1166
5ac8fb31 1167 if (reclaim) {
ef8f2327 1168 struct mem_cgroup_per_node *mz;
5ac8fb31 1169
a3747b53 1170 mz = root->nodeinfo[reclaim->pgdat->node_id];
9da83f3f 1171 iter = &mz->iter;
5ac8fb31 1172
a9320aae
WY
1173 /*
1174 * On start, join the current reclaim iteration cycle.
1175 * Exit when a concurrent walker completes it.
1176 */
1177 if (!prev)
1178 reclaim->generation = iter->generation;
1179 else if (reclaim->generation != iter->generation)
5ac8fb31
JW
1180 goto out_unlock;
1181
6df38689 1182 while (1) {
4db0c3c2 1183 pos = READ_ONCE(iter->position);
6df38689
VD
1184 if (!pos || css_tryget(&pos->css))
1185 break;
5ac8fb31 1186 /*
6df38689
VD
1187 * css reference reached zero, so iter->position will
1188 * be cleared by ->css_released. However, we should not
1189 * rely on this happening soon, because ->css_released
1190 * is called from a work queue, and by busy-waiting we
1191 * might block it. So we clear iter->position right
1192 * away.
5ac8fb31 1193 */
6df38689
VD
1194 (void)cmpxchg(&iter->position, pos, NULL);
1195 }
89d8330c
WY
1196 } else if (prev) {
1197 pos = prev;
5ac8fb31
JW
1198 }
1199
1200 if (pos)
1201 css = &pos->css;
1202
1203 for (;;) {
1204 css = css_next_descendant_pre(css, &root->css);
1205 if (!css) {
1206 /*
1207 * Reclaimers share the hierarchy walk, and a
1208 * new one might jump in right at the end of
1209 * the hierarchy - make sure they see at least
1210 * one group and restart from the beginning.
1211 */
1212 if (!prev)
1213 continue;
1214 break;
527a5ec9 1215 }
7d74b06f 1216
5ac8fb31
JW
1217 /*
1218 * Verify the css and acquire a reference. The root
1219 * is provided by the caller, so we know it's alive
1220 * and kicking, and don't take an extra reference.
1221 */
41555dad
WY
1222 if (css == &root->css || css_tryget(css)) {
1223 memcg = mem_cgroup_from_css(css);
0b8f73e1 1224 break;
41555dad 1225 }
9f3a0d09 1226 }
5ac8fb31
JW
1227
1228 if (reclaim) {
5ac8fb31 1229 /*
6df38689
VD
1230 * The position could have already been updated by a competing
1231 * thread, so check that the value hasn't changed since we read
1232 * it to avoid reclaiming from the same cgroup twice.
5ac8fb31 1233 */
6df38689
VD
1234 (void)cmpxchg(&iter->position, pos, memcg);
1235
5ac8fb31
JW
1236 if (pos)
1237 css_put(&pos->css);
1238
1239 if (!memcg)
1240 iter->generation++;
9f3a0d09 1241 }
5ac8fb31 1242
542f85f9
MH
1243out_unlock:
1244 rcu_read_unlock();
c40046f3
MH
1245 if (prev && prev != root)
1246 css_put(&prev->css);
1247
9f3a0d09 1248 return memcg;
14067bb3 1249}
7d74b06f 1250
5660048c
JW
1251/**
1252 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1253 * @root: hierarchy root
1254 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1255 */
1256void mem_cgroup_iter_break(struct mem_cgroup *root,
1257 struct mem_cgroup *prev)
9f3a0d09
JW
1258{
1259 if (!root)
1260 root = root_mem_cgroup;
1261 if (prev && prev != root)
1262 css_put(&prev->css);
1263}
7d74b06f 1264
54a83d6b
MC
1265static void __invalidate_reclaim_iterators(struct mem_cgroup *from,
1266 struct mem_cgroup *dead_memcg)
6df38689 1267{
6df38689 1268 struct mem_cgroup_reclaim_iter *iter;
ef8f2327
MG
1269 struct mem_cgroup_per_node *mz;
1270 int nid;
6df38689 1271
54a83d6b 1272 for_each_node(nid) {
a3747b53 1273 mz = from->nodeinfo[nid];
9da83f3f
YS
1274 iter = &mz->iter;
1275 cmpxchg(&iter->position, dead_memcg, NULL);
6df38689
VD
1276 }
1277}
1278
54a83d6b
MC
1279static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg)
1280{
1281 struct mem_cgroup *memcg = dead_memcg;
1282 struct mem_cgroup *last;
1283
1284 do {
1285 __invalidate_reclaim_iterators(memcg, dead_memcg);
1286 last = memcg;
1287 } while ((memcg = parent_mem_cgroup(memcg)));
1288
1289 /*
b8dd3ee9 1290 * When cgroup1 non-hierarchy mode is used,
54a83d6b
MC
1291 * parent_mem_cgroup() does not walk all the way up to the
1292 * cgroup root (root_mem_cgroup). So we have to handle
1293 * dead_memcg from cgroup root separately.
1294 */
7848ed62 1295 if (!mem_cgroup_is_root(last))
54a83d6b
MC
1296 __invalidate_reclaim_iterators(root_mem_cgroup,
1297 dead_memcg);
1298}
1299
7c5f64f8
VD
1300/**
1301 * mem_cgroup_scan_tasks - iterate over tasks of a memory cgroup hierarchy
1302 * @memcg: hierarchy root
1303 * @fn: function to call for each task
1304 * @arg: argument passed to @fn
1305 *
1306 * This function iterates over tasks attached to @memcg or to any of its
1307 * descendants and calls @fn for each task. If @fn returns a non-zero
025b7799
Z
1308 * value, the function breaks the iteration loop. Otherwise, it will iterate
1309 * over all tasks and return 0.
7c5f64f8
VD
1310 *
1311 * This function must not be called for the root memory cgroup.
1312 */
025b7799
Z
1313void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1314 int (*fn)(struct task_struct *, void *), void *arg)
7c5f64f8
VD
1315{
1316 struct mem_cgroup *iter;
1317 int ret = 0;
1318
7848ed62 1319 BUG_ON(mem_cgroup_is_root(memcg));
7c5f64f8
VD
1320
1321 for_each_mem_cgroup_tree(iter, memcg) {
1322 struct css_task_iter it;
1323 struct task_struct *task;
1324
f168a9a5 1325 css_task_iter_start(&iter->css, CSS_TASK_ITER_PROCS, &it);
7c5f64f8
VD
1326 while (!ret && (task = css_task_iter_next(&it)))
1327 ret = fn(task, arg);
1328 css_task_iter_end(&it);
1329 if (ret) {
1330 mem_cgroup_iter_break(memcg, iter);
1331 break;
1332 }
1333 }
7c5f64f8
VD
1334}
1335
6168d0da 1336#ifdef CONFIG_DEBUG_VM
e809c3fe 1337void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
6168d0da
AS
1338{
1339 struct mem_cgroup *memcg;
1340
1341 if (mem_cgroup_disabled())
1342 return;
1343
e809c3fe 1344 memcg = folio_memcg(folio);
6168d0da
AS
1345
1346 if (!memcg)
7848ed62 1347 VM_BUG_ON_FOLIO(!mem_cgroup_is_root(lruvec_memcg(lruvec)), folio);
6168d0da 1348 else
e809c3fe 1349 VM_BUG_ON_FOLIO(lruvec_memcg(lruvec) != memcg, folio);
6168d0da
AS
1350}
1351#endif
1352
6168d0da 1353/**
e809c3fe
MWO
1354 * folio_lruvec_lock - Lock the lruvec for a folio.
1355 * @folio: Pointer to the folio.
6168d0da 1356 *
d7e3aba5 1357 * These functions are safe to use under any of the following conditions:
e809c3fe
MWO
1358 * - folio locked
1359 * - folio_test_lru false
1360 * - folio_memcg_lock()
1361 * - folio frozen (refcount of 0)
1362 *
1363 * Return: The lruvec this folio is on with its lock held.
6168d0da 1364 */
e809c3fe 1365struct lruvec *folio_lruvec_lock(struct folio *folio)
6168d0da 1366{
e809c3fe 1367 struct lruvec *lruvec = folio_lruvec(folio);
6168d0da 1368
6168d0da 1369 spin_lock(&lruvec->lru_lock);
e809c3fe 1370 lruvec_memcg_debug(lruvec, folio);
6168d0da
AS
1371
1372 return lruvec;
1373}
1374
e809c3fe
MWO
1375/**
1376 * folio_lruvec_lock_irq - Lock the lruvec for a folio.
1377 * @folio: Pointer to the folio.
1378 *
1379 * These functions are safe to use under any of the following conditions:
1380 * - folio locked
1381 * - folio_test_lru false
1382 * - folio_memcg_lock()
1383 * - folio frozen (refcount of 0)
1384 *
1385 * Return: The lruvec this folio is on with its lock held and interrupts
1386 * disabled.
1387 */
1388struct lruvec *folio_lruvec_lock_irq(struct folio *folio)
6168d0da 1389{
e809c3fe 1390 struct lruvec *lruvec = folio_lruvec(folio);
6168d0da 1391
6168d0da 1392 spin_lock_irq(&lruvec->lru_lock);
e809c3fe 1393 lruvec_memcg_debug(lruvec, folio);
6168d0da
AS
1394
1395 return lruvec;
1396}
1397
e809c3fe
MWO
1398/**
1399 * folio_lruvec_lock_irqsave - Lock the lruvec for a folio.
1400 * @folio: Pointer to the folio.
1401 * @flags: Pointer to irqsave flags.
1402 *
1403 * These functions are safe to use under any of the following conditions:
1404 * - folio locked
1405 * - folio_test_lru false
1406 * - folio_memcg_lock()
1407 * - folio frozen (refcount of 0)
1408 *
1409 * Return: The lruvec this folio is on with its lock held and interrupts
1410 * disabled.
1411 */
1412struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
1413 unsigned long *flags)
6168d0da 1414{
e809c3fe 1415 struct lruvec *lruvec = folio_lruvec(folio);
6168d0da 1416
6168d0da 1417 spin_lock_irqsave(&lruvec->lru_lock, *flags);
e809c3fe 1418 lruvec_memcg_debug(lruvec, folio);
6168d0da
AS
1419
1420 return lruvec;
1421}
1422
925b7673 1423/**
fa9add64
HD
1424 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1425 * @lruvec: mem_cgroup per zone lru vector
1426 * @lru: index of lru list the page is sitting on
b4536f0c 1427 * @zid: zone id of the accounted pages
fa9add64 1428 * @nr_pages: positive when adding or negative when removing
925b7673 1429 *
ca707239 1430 * This function must be called under lru_lock, just before a page is added
07ca7606 1431 * to or just after a page is removed from an lru list.
3f58a829 1432 */
fa9add64 1433void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
b4536f0c 1434 int zid, int nr_pages)
3f58a829 1435{
ef8f2327 1436 struct mem_cgroup_per_node *mz;
fa9add64 1437 unsigned long *lru_size;
ca707239 1438 long size;
3f58a829
MK
1439
1440 if (mem_cgroup_disabled())
1441 return;
1442
ef8f2327 1443 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
b4536f0c 1444 lru_size = &mz->lru_zone_size[zid][lru];
ca707239
HD
1445
1446 if (nr_pages < 0)
1447 *lru_size += nr_pages;
1448
1449 size = *lru_size;
b4536f0c
MH
1450 if (WARN_ONCE(size < 0,
1451 "%s(%p, %d, %d): lru_size %ld\n",
1452 __func__, lruvec, lru, nr_pages, size)) {
ca707239
HD
1453 VM_BUG_ON(1);
1454 *lru_size = 0;
1455 }
1456
1457 if (nr_pages > 0)
1458 *lru_size += nr_pages;
08e552c6 1459}
544122e5 1460
19942822 1461/**
9d11ea9f 1462 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
dad7557e 1463 * @memcg: the memory cgroup
19942822 1464 *
9d11ea9f 1465 * Returns the maximum amount of memory @mem can be charged with, in
7ec99d62 1466 * pages.
19942822 1467 */
c0ff4b85 1468static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
19942822 1469{
3e32cb2e
JW
1470 unsigned long margin = 0;
1471 unsigned long count;
1472 unsigned long limit;
9d11ea9f 1473
3e32cb2e 1474 count = page_counter_read(&memcg->memory);
bbec2e15 1475 limit = READ_ONCE(memcg->memory.max);
3e32cb2e
JW
1476 if (count < limit)
1477 margin = limit - count;
1478
7941d214 1479 if (do_memsw_account()) {
3e32cb2e 1480 count = page_counter_read(&memcg->memsw);
bbec2e15 1481 limit = READ_ONCE(memcg->memsw.max);
1c4448ed 1482 if (count < limit)
3e32cb2e 1483 margin = min(margin, limit - count);
cbedbac3
LR
1484 else
1485 margin = 0;
3e32cb2e
JW
1486 }
1487
1488 return margin;
19942822
JW
1489}
1490
32047e2a 1491/*
bdcbb659 1492 * A routine for checking "mem" is under move_account() or not.
32047e2a 1493 *
bdcbb659
QH
1494 * Checking a cgroup is mc.from or mc.to or under hierarchy of
1495 * moving cgroups. This is for waiting at high-memory pressure
1496 * caused by "move".
32047e2a 1497 */
c0ff4b85 1498static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
4b534334 1499{
2bd9bb20
KH
1500 struct mem_cgroup *from;
1501 struct mem_cgroup *to;
4b534334 1502 bool ret = false;
2bd9bb20
KH
1503 /*
1504 * Unlike task_move routines, we access mc.to, mc.from not under
1505 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1506 */
1507 spin_lock(&mc.lock);
1508 from = mc.from;
1509 to = mc.to;
1510 if (!from)
1511 goto unlock;
3e92041d 1512
2314b42d
JW
1513 ret = mem_cgroup_is_descendant(from, memcg) ||
1514 mem_cgroup_is_descendant(to, memcg);
2bd9bb20
KH
1515unlock:
1516 spin_unlock(&mc.lock);
4b534334
KH
1517 return ret;
1518}
1519
c0ff4b85 1520static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
4b534334
KH
1521{
1522 if (mc.moving_task && current != mc.moving_task) {
c0ff4b85 1523 if (mem_cgroup_under_move(memcg)) {
4b534334
KH
1524 DEFINE_WAIT(wait);
1525 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1526 /* moving charge context might have finished. */
1527 if (mc.moving_task)
1528 schedule();
1529 finish_wait(&mc.waitq, &wait);
1530 return true;
1531 }
1532 }
1533 return false;
1534}
1535
5f9a4f4a
MS
1536struct memory_stat {
1537 const char *name;
5f9a4f4a
MS
1538 unsigned int idx;
1539};
1540
57b2847d 1541static const struct memory_stat memory_stats[] = {
fff66b79
MS
1542 { "anon", NR_ANON_MAPPED },
1543 { "file", NR_FILE_PAGES },
a8c49af3 1544 { "kernel", MEMCG_KMEM },
fff66b79
MS
1545 { "kernel_stack", NR_KERNEL_STACK_KB },
1546 { "pagetables", NR_PAGETABLE },
ebc97a52 1547 { "sec_pagetables", NR_SECONDARY_PAGETABLE },
fff66b79
MS
1548 { "percpu", MEMCG_PERCPU_B },
1549 { "sock", MEMCG_SOCK },
4e5aa1f4 1550 { "vmalloc", MEMCG_VMALLOC },
fff66b79 1551 { "shmem", NR_SHMEM },
f4840ccf
JW
1552#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
1553 { "zswap", MEMCG_ZSWAP_B },
1554 { "zswapped", MEMCG_ZSWAPPED },
1555#endif
fff66b79
MS
1556 { "file_mapped", NR_FILE_MAPPED },
1557 { "file_dirty", NR_FILE_DIRTY },
1558 { "file_writeback", NR_WRITEBACK },
b6038942
SB
1559#ifdef CONFIG_SWAP
1560 { "swapcached", NR_SWAPCACHE },
1561#endif
5f9a4f4a 1562#ifdef CONFIG_TRANSPARENT_HUGEPAGE
fff66b79
MS
1563 { "anon_thp", NR_ANON_THPS },
1564 { "file_thp", NR_FILE_THPS },
1565 { "shmem_thp", NR_SHMEM_THPS },
5f9a4f4a 1566#endif
fff66b79
MS
1567 { "inactive_anon", NR_INACTIVE_ANON },
1568 { "active_anon", NR_ACTIVE_ANON },
1569 { "inactive_file", NR_INACTIVE_FILE },
1570 { "active_file", NR_ACTIVE_FILE },
1571 { "unevictable", NR_UNEVICTABLE },
1572 { "slab_reclaimable", NR_SLAB_RECLAIMABLE_B },
1573 { "slab_unreclaimable", NR_SLAB_UNRECLAIMABLE_B },
5f9a4f4a
MS
1574
1575 /* The memory events */
fff66b79
MS
1576 { "workingset_refault_anon", WORKINGSET_REFAULT_ANON },
1577 { "workingset_refault_file", WORKINGSET_REFAULT_FILE },
1578 { "workingset_activate_anon", WORKINGSET_ACTIVATE_ANON },
1579 { "workingset_activate_file", WORKINGSET_ACTIVATE_FILE },
1580 { "workingset_restore_anon", WORKINGSET_RESTORE_ANON },
1581 { "workingset_restore_file", WORKINGSET_RESTORE_FILE },
1582 { "workingset_nodereclaim", WORKINGSET_NODERECLAIM },
5f9a4f4a
MS
1583};
1584
ff841a06 1585/* The actual unit of the state item, not the same as the output unit */
fff66b79
MS
1586static int memcg_page_state_unit(int item)
1587{
1588 switch (item) {
1589 case MEMCG_PERCPU_B:
f4840ccf 1590 case MEMCG_ZSWAP_B:
fff66b79
MS
1591 case NR_SLAB_RECLAIMABLE_B:
1592 case NR_SLAB_UNRECLAIMABLE_B:
ff841a06
YA
1593 return 1;
1594 case NR_KERNEL_STACK_KB:
1595 return SZ_1K;
1596 default:
1597 return PAGE_SIZE;
1598 }
1599}
1600
1601/* Translate stat items to the correct unit for memory.stat output */
1602static int memcg_page_state_output_unit(int item)
1603{
1604 /*
1605 * Workingset state is actually in pages, but we export it to userspace
1606 * as a scalar count of events, so special case it here.
1607 */
1608 switch (item) {
fff66b79
MS
1609 case WORKINGSET_REFAULT_ANON:
1610 case WORKINGSET_REFAULT_FILE:
1611 case WORKINGSET_ACTIVATE_ANON:
1612 case WORKINGSET_ACTIVATE_FILE:
1613 case WORKINGSET_RESTORE_ANON:
1614 case WORKINGSET_RESTORE_FILE:
1615 case WORKINGSET_NODERECLAIM:
1616 return 1;
fff66b79 1617 default:
ff841a06 1618 return memcg_page_state_unit(item);
fff66b79
MS
1619 }
1620}
1621
1622static inline unsigned long memcg_page_state_output(struct mem_cgroup *memcg,
1623 int item)
1624{
ff841a06
YA
1625 return memcg_page_state(memcg, item) *
1626 memcg_page_state_output_unit(item);
1627}
1628
1629static inline unsigned long memcg_page_state_local_output(
1630 struct mem_cgroup *memcg, int item)
1631{
1632 return memcg_page_state_local(memcg, item) *
1633 memcg_page_state_output_unit(item);
fff66b79
MS
1634}
1635
dddb44ff 1636static void memcg_stat_format(struct mem_cgroup *memcg, struct seq_buf *s)
c8713d0b 1637{
c8713d0b 1638 int i;
71cd3113 1639
c8713d0b
JW
1640 /*
1641 * Provide statistics on the state of the memory subsystem as
1642 * well as cumulative event counters that show past behavior.
1643 *
1644 * This list is ordered following a combination of these gradients:
1645 * 1) generic big picture -> specifics and details
1646 * 2) reflecting userspace activity -> reflecting kernel heuristics
1647 *
1648 * Current memory state:
1649 */
7d7ef0a4 1650 mem_cgroup_flush_stats(memcg);
c8713d0b 1651
5f9a4f4a
MS
1652 for (i = 0; i < ARRAY_SIZE(memory_stats); i++) {
1653 u64 size;
c8713d0b 1654
fff66b79 1655 size = memcg_page_state_output(memcg, memory_stats[i].idx);
5b42360c 1656 seq_buf_printf(s, "%s %llu\n", memory_stats[i].name, size);
c8713d0b 1657
5f9a4f4a 1658 if (unlikely(memory_stats[i].idx == NR_SLAB_UNRECLAIMABLE_B)) {
fff66b79
MS
1659 size += memcg_page_state_output(memcg,
1660 NR_SLAB_RECLAIMABLE_B);
5b42360c 1661 seq_buf_printf(s, "slab %llu\n", size);
5f9a4f4a
MS
1662 }
1663 }
c8713d0b
JW
1664
1665 /* Accumulated memory events */
5b42360c 1666 seq_buf_printf(s, "pgscan %lu\n",
c8713d0b 1667 memcg_events(memcg, PGSCAN_KSWAPD) +
57e9cc50
JW
1668 memcg_events(memcg, PGSCAN_DIRECT) +
1669 memcg_events(memcg, PGSCAN_KHUGEPAGED));
5b42360c 1670 seq_buf_printf(s, "pgsteal %lu\n",
c8713d0b 1671 memcg_events(memcg, PGSTEAL_KSWAPD) +
57e9cc50
JW
1672 memcg_events(memcg, PGSTEAL_DIRECT) +
1673 memcg_events(memcg, PGSTEAL_KHUGEPAGED));
c8713d0b 1674
8278f1c7
SB
1675 for (i = 0; i < ARRAY_SIZE(memcg_vm_event_stat); i++) {
1676 if (memcg_vm_event_stat[i] == PGPGIN ||
1677 memcg_vm_event_stat[i] == PGPGOUT)
1678 continue;
1679
5b42360c 1680 seq_buf_printf(s, "%s %lu\n",
673520f8
QZ
1681 vm_event_name(memcg_vm_event_stat[i]),
1682 memcg_events(memcg, memcg_vm_event_stat[i]));
8278f1c7 1683 }
c8713d0b
JW
1684
1685 /* The above should easily fit into one page */
5b42360c 1686 WARN_ON_ONCE(seq_buf_has_overflowed(s));
c8713d0b 1687}
71cd3113 1688
dddb44ff
YA
1689static void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s);
1690
1691static void memory_stat_format(struct mem_cgroup *memcg, struct seq_buf *s)
1692{
1693 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
1694 memcg_stat_format(memcg, s);
1695 else
1696 memcg1_stat_format(memcg, s);
1697 WARN_ON_ONCE(seq_buf_has_overflowed(s));
1698}
1699
e222432b 1700/**
f0c867d9 1701 * mem_cgroup_print_oom_context: Print OOM information relevant to
1702 * memory controller.
e222432b
BS
1703 * @memcg: The memory cgroup that went over limit
1704 * @p: Task that is going to be killed
1705 *
1706 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1707 * enabled
1708 */
f0c867d9 1709void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
e222432b 1710{
e222432b
BS
1711 rcu_read_lock();
1712
f0c867d9 1713 if (memcg) {
1714 pr_cont(",oom_memcg=");
1715 pr_cont_cgroup_path(memcg->css.cgroup);
1716 } else
1717 pr_cont(",global_oom");
2415b9f5 1718 if (p) {
f0c867d9 1719 pr_cont(",task_memcg=");
2415b9f5 1720 pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id));
2415b9f5 1721 }
e222432b 1722 rcu_read_unlock();
f0c867d9 1723}
1724
1725/**
1726 * mem_cgroup_print_oom_meminfo: Print OOM memory information relevant to
1727 * memory controller.
1728 * @memcg: The memory cgroup that went over limit
1729 */
1730void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
1731{
68aaee14
TH
1732 /* Use static buffer, for the caller is holding oom_lock. */
1733 static char buf[PAGE_SIZE];
5b42360c 1734 struct seq_buf s;
68aaee14
TH
1735
1736 lockdep_assert_held(&oom_lock);
e222432b 1737
3e32cb2e
JW
1738 pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n",
1739 K((u64)page_counter_read(&memcg->memory)),
15b42562 1740 K((u64)READ_ONCE(memcg->memory.max)), memcg->memory.failcnt);
c8713d0b
JW
1741 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
1742 pr_info("swap: usage %llukB, limit %llukB, failcnt %lu\n",
1743 K((u64)page_counter_read(&memcg->swap)),
32d087cd 1744 K((u64)READ_ONCE(memcg->swap.max)), memcg->swap.failcnt);
c8713d0b
JW
1745 else {
1746 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n",
1747 K((u64)page_counter_read(&memcg->memsw)),
1748 K((u64)memcg->memsw.max), memcg->memsw.failcnt);
1749 pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n",
1750 K((u64)page_counter_read(&memcg->kmem)),
1751 K((u64)memcg->kmem.max), memcg->kmem.failcnt);
58cf188e 1752 }
c8713d0b
JW
1753
1754 pr_info("Memory cgroup stats for ");
1755 pr_cont_cgroup_path(memcg->css.cgroup);
1756 pr_cont(":");
5b42360c
YA
1757 seq_buf_init(&s, buf, sizeof(buf));
1758 memory_stat_format(memcg, &s);
1759 seq_buf_do_printk(&s, KERN_INFO);
e222432b
BS
1760}
1761
a63d83f4
DR
1762/*
1763 * Return the memory (and swap, if configured) limit for a memcg.
1764 */
bbec2e15 1765unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
a63d83f4 1766{
8d387a5f
WL
1767 unsigned long max = READ_ONCE(memcg->memory.max);
1768
b94c4e94 1769 if (do_memsw_account()) {
8d387a5f
WL
1770 if (mem_cgroup_swappiness(memcg)) {
1771 /* Calculate swap excess capacity from memsw limit */
1772 unsigned long swap = READ_ONCE(memcg->memsw.max) - max;
1773
1774 max += min(swap, (unsigned long)total_swap_pages);
1775 }
b94c4e94
JW
1776 } else {
1777 if (mem_cgroup_swappiness(memcg))
1778 max += min(READ_ONCE(memcg->swap.max),
1779 (unsigned long)total_swap_pages);
9a5a8f19 1780 }
bbec2e15 1781 return max;
a63d83f4
DR
1782}
1783
9783aa99
CD
1784unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1785{
1786 return page_counter_read(&memcg->memory);
1787}
1788
b6e6edcf 1789static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
19965460 1790 int order)
9cbb78bb 1791{
6e0fc46d
DR
1792 struct oom_control oc = {
1793 .zonelist = NULL,
1794 .nodemask = NULL,
2a966b77 1795 .memcg = memcg,
6e0fc46d
DR
1796 .gfp_mask = gfp_mask,
1797 .order = order,
6e0fc46d 1798 };
1378b37d 1799 bool ret = true;
9cbb78bb 1800
7775face
TH
1801 if (mutex_lock_killable(&oom_lock))
1802 return true;
1378b37d
YS
1803
1804 if (mem_cgroup_margin(memcg) >= (1 << order))
1805 goto unlock;
1806
7775face
TH
1807 /*
1808 * A few threads which were not waiting at mutex_lock_killable() can
1809 * fail to bail out. Therefore, check again after holding oom_lock.
1810 */
a4ebf1b6 1811 ret = task_is_dying() || out_of_memory(&oc);
1378b37d
YS
1812
1813unlock:
dc56401f 1814 mutex_unlock(&oom_lock);
7c5f64f8 1815 return ret;
9cbb78bb
DR
1816}
1817
0608f43d 1818static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
ef8f2327 1819 pg_data_t *pgdat,
0608f43d
AM
1820 gfp_t gfp_mask,
1821 unsigned long *total_scanned)
1822{
1823 struct mem_cgroup *victim = NULL;
1824 int total = 0;
1825 int loop = 0;
1826 unsigned long excess;
1827 unsigned long nr_scanned;
1828 struct mem_cgroup_reclaim_cookie reclaim = {
ef8f2327 1829 .pgdat = pgdat,
0608f43d
AM
1830 };
1831
3e32cb2e 1832 excess = soft_limit_excess(root_memcg);
0608f43d
AM
1833
1834 while (1) {
1835 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
1836 if (!victim) {
1837 loop++;
1838 if (loop >= 2) {
1839 /*
1840 * If we have not been able to reclaim
1841 * anything, it might because there are
1842 * no reclaimable pages under this hierarchy
1843 */
1844 if (!total)
1845 break;
1846 /*
1847 * We want to do more targeted reclaim.
1848 * excess >> 2 is not to excessive so as to
1849 * reclaim too much, nor too less that we keep
1850 * coming back to reclaim from this cgroup
1851 */
1852 if (total >= (excess >> 2) ||
1853 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
1854 break;
1855 }
1856 continue;
1857 }
a9dd0a83 1858 total += mem_cgroup_shrink_node(victim, gfp_mask, false,
ef8f2327 1859 pgdat, &nr_scanned);
0608f43d 1860 *total_scanned += nr_scanned;
3e32cb2e 1861 if (!soft_limit_excess(root_memcg))
0608f43d 1862 break;
6d61ef40 1863 }
0608f43d
AM
1864 mem_cgroup_iter_break(root_memcg, victim);
1865 return total;
6d61ef40
BS
1866}
1867
0056f4e6
JW
1868#ifdef CONFIG_LOCKDEP
1869static struct lockdep_map memcg_oom_lock_dep_map = {
1870 .name = "memcg_oom_lock",
1871};
1872#endif
1873
fb2a6fc5
JW
1874static DEFINE_SPINLOCK(memcg_oom_lock);
1875
867578cb
KH
1876/*
1877 * Check OOM-Killer is already running under our hierarchy.
1878 * If someone is running, return false.
1879 */
fb2a6fc5 1880static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
867578cb 1881{
79dfdacc 1882 struct mem_cgroup *iter, *failed = NULL;
a636b327 1883
fb2a6fc5
JW
1884 spin_lock(&memcg_oom_lock);
1885
9f3a0d09 1886 for_each_mem_cgroup_tree(iter, memcg) {
23751be0 1887 if (iter->oom_lock) {
79dfdacc
MH
1888 /*
1889 * this subtree of our hierarchy is already locked
1890 * so we cannot give a lock.
1891 */
79dfdacc 1892 failed = iter;
9f3a0d09
JW
1893 mem_cgroup_iter_break(memcg, iter);
1894 break;
23751be0
JW
1895 } else
1896 iter->oom_lock = true;
7d74b06f 1897 }
867578cb 1898
fb2a6fc5
JW
1899 if (failed) {
1900 /*
1901 * OK, we failed to lock the whole subtree so we have
1902 * to clean up what we set up to the failing subtree
1903 */
1904 for_each_mem_cgroup_tree(iter, memcg) {
1905 if (iter == failed) {
1906 mem_cgroup_iter_break(memcg, iter);
1907 break;
1908 }
1909 iter->oom_lock = false;
79dfdacc 1910 }
0056f4e6
JW
1911 } else
1912 mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_);
fb2a6fc5
JW
1913
1914 spin_unlock(&memcg_oom_lock);
1915
1916 return !failed;
a636b327 1917}
0b7f569e 1918
fb2a6fc5 1919static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
0b7f569e 1920{
7d74b06f
KH
1921 struct mem_cgroup *iter;
1922
fb2a6fc5 1923 spin_lock(&memcg_oom_lock);
5facae4f 1924 mutex_release(&memcg_oom_lock_dep_map, _RET_IP_);
c0ff4b85 1925 for_each_mem_cgroup_tree(iter, memcg)
79dfdacc 1926 iter->oom_lock = false;
fb2a6fc5 1927 spin_unlock(&memcg_oom_lock);
79dfdacc
MH
1928}
1929
c0ff4b85 1930static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
79dfdacc
MH
1931{
1932 struct mem_cgroup *iter;
1933
c2b42d3c 1934 spin_lock(&memcg_oom_lock);
c0ff4b85 1935 for_each_mem_cgroup_tree(iter, memcg)
c2b42d3c
TH
1936 iter->under_oom++;
1937 spin_unlock(&memcg_oom_lock);
79dfdacc
MH
1938}
1939
c0ff4b85 1940static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
79dfdacc
MH
1941{
1942 struct mem_cgroup *iter;
1943
867578cb 1944 /*
f0953a1b 1945 * Be careful about under_oom underflows because a child memcg
7a52d4d8 1946 * could have been added after mem_cgroup_mark_under_oom.
867578cb 1947 */
c2b42d3c 1948 spin_lock(&memcg_oom_lock);
c0ff4b85 1949 for_each_mem_cgroup_tree(iter, memcg)
c2b42d3c
TH
1950 if (iter->under_oom > 0)
1951 iter->under_oom--;
1952 spin_unlock(&memcg_oom_lock);
0b7f569e
KH
1953}
1954
867578cb
KH
1955static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1956
dc98df5a 1957struct oom_wait_info {
d79154bb 1958 struct mem_cgroup *memcg;
ac6424b9 1959 wait_queue_entry_t wait;
dc98df5a
KH
1960};
1961
ac6424b9 1962static int memcg_oom_wake_function(wait_queue_entry_t *wait,
dc98df5a
KH
1963 unsigned mode, int sync, void *arg)
1964{
d79154bb
HD
1965 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1966 struct mem_cgroup *oom_wait_memcg;
dc98df5a
KH
1967 struct oom_wait_info *oom_wait_info;
1968
1969 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
d79154bb 1970 oom_wait_memcg = oom_wait_info->memcg;
dc98df5a 1971
2314b42d
JW
1972 if (!mem_cgroup_is_descendant(wake_memcg, oom_wait_memcg) &&
1973 !mem_cgroup_is_descendant(oom_wait_memcg, wake_memcg))
dc98df5a 1974 return 0;
dc98df5a
KH
1975 return autoremove_wake_function(wait, mode, sync, arg);
1976}
1977
c0ff4b85 1978static void memcg_oom_recover(struct mem_cgroup *memcg)
3c11ecf4 1979{
c2b42d3c
TH
1980 /*
1981 * For the following lockless ->under_oom test, the only required
1982 * guarantee is that it must see the state asserted by an OOM when
1983 * this function is called as a result of userland actions
1984 * triggered by the notification of the OOM. This is trivially
1985 * achieved by invoking mem_cgroup_mark_under_oom() before
1986 * triggering notification.
1987 */
1988 if (memcg && memcg->under_oom)
f4b90b70 1989 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
3c11ecf4
KH
1990}
1991
becdf89d
SB
1992/*
1993 * Returns true if successfully killed one or more processes. Though in some
1994 * corner cases it can return true even without killing any process.
1995 */
1996static bool mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
0b7f569e 1997{
becdf89d 1998 bool locked, ret;
7056d3a3 1999
29ef680a 2000 if (order > PAGE_ALLOC_COSTLY_ORDER)
becdf89d 2001 return false;
29ef680a 2002
7a1adfdd
RG
2003 memcg_memory_event(memcg, MEMCG_OOM);
2004
867578cb 2005 /*
49426420
JW
2006 * We are in the middle of the charge context here, so we
2007 * don't want to block when potentially sitting on a callstack
2008 * that holds all kinds of filesystem and mm locks.
2009 *
29ef680a
MH
2010 * cgroup1 allows disabling the OOM killer and waiting for outside
2011 * handling until the charge can succeed; remember the context and put
2012 * the task to sleep at the end of the page fault when all locks are
2013 * released.
49426420 2014 *
29ef680a
MH
2015 * On the other hand, in-kernel OOM killer allows for an async victim
2016 * memory reclaim (oom_reaper) and that means that we are not solely
2017 * relying on the oom victim to make a forward progress and we can
2018 * invoke the oom killer here.
2019 *
2020 * Please note that mem_cgroup_out_of_memory might fail to find a
2021 * victim and then we have to bail out from the charge path.
867578cb 2022 */
17c56de6 2023 if (READ_ONCE(memcg->oom_kill_disable)) {
becdf89d
SB
2024 if (current->in_user_fault) {
2025 css_get(&memcg->css);
2026 current->memcg_in_oom = memcg;
2027 current->memcg_oom_gfp_mask = mask;
2028 current->memcg_oom_order = order;
2029 }
2030 return false;
29ef680a
MH
2031 }
2032
7056d3a3
MH
2033 mem_cgroup_mark_under_oom(memcg);
2034
2035 locked = mem_cgroup_oom_trylock(memcg);
2036
2037 if (locked)
2038 mem_cgroup_oom_notify(memcg);
2039
2040 mem_cgroup_unmark_under_oom(memcg);
becdf89d 2041 ret = mem_cgroup_out_of_memory(memcg, mask, order);
7056d3a3
MH
2042
2043 if (locked)
2044 mem_cgroup_oom_unlock(memcg);
29ef680a 2045
7056d3a3 2046 return ret;
3812c8c8
JW
2047}
2048
2049/**
2050 * mem_cgroup_oom_synchronize - complete memcg OOM handling
49426420 2051 * @handle: actually kill/wait or just clean up the OOM state
3812c8c8 2052 *
49426420
JW
2053 * This has to be called at the end of a page fault if the memcg OOM
2054 * handler was enabled.
3812c8c8 2055 *
49426420 2056 * Memcg supports userspace OOM handling where failed allocations must
3812c8c8
JW
2057 * sleep on a waitqueue until the userspace task resolves the
2058 * situation. Sleeping directly in the charge context with all kinds
2059 * of locks held is not a good idea, instead we remember an OOM state
2060 * in the task and mem_cgroup_oom_synchronize() has to be called at
49426420 2061 * the end of the page fault to complete the OOM handling.
3812c8c8
JW
2062 *
2063 * Returns %true if an ongoing memcg OOM situation was detected and
49426420 2064 * completed, %false otherwise.
3812c8c8 2065 */
49426420 2066bool mem_cgroup_oom_synchronize(bool handle)
3812c8c8 2067{
626ebc41 2068 struct mem_cgroup *memcg = current->memcg_in_oom;
3812c8c8 2069 struct oom_wait_info owait;
49426420 2070 bool locked;
3812c8c8
JW
2071
2072 /* OOM is global, do not handle */
3812c8c8 2073 if (!memcg)
49426420 2074 return false;
3812c8c8 2075
7c5f64f8 2076 if (!handle)
49426420 2077 goto cleanup;
3812c8c8
JW
2078
2079 owait.memcg = memcg;
2080 owait.wait.flags = 0;
2081 owait.wait.func = memcg_oom_wake_function;
2082 owait.wait.private = current;
2055da97 2083 INIT_LIST_HEAD(&owait.wait.entry);
867578cb 2084
3812c8c8 2085 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
49426420
JW
2086 mem_cgroup_mark_under_oom(memcg);
2087
2088 locked = mem_cgroup_oom_trylock(memcg);
2089
2090 if (locked)
2091 mem_cgroup_oom_notify(memcg);
2092
857f2139
HX
2093 schedule();
2094 mem_cgroup_unmark_under_oom(memcg);
2095 finish_wait(&memcg_oom_waitq, &owait.wait);
49426420 2096
18b1d18b 2097 if (locked)
fb2a6fc5 2098 mem_cgroup_oom_unlock(memcg);
49426420 2099cleanup:
626ebc41 2100 current->memcg_in_oom = NULL;
3812c8c8 2101 css_put(&memcg->css);
867578cb 2102 return true;
0b7f569e
KH
2103}
2104
3d8b38eb
RG
2105/**
2106 * mem_cgroup_get_oom_group - get a memory cgroup to clean up after OOM
2107 * @victim: task to be killed by the OOM killer
2108 * @oom_domain: memcg in case of memcg OOM, NULL in case of system-wide OOM
2109 *
2110 * Returns a pointer to a memory cgroup, which has to be cleaned up
2111 * by killing all belonging OOM-killable tasks.
2112 *
2113 * Caller has to call mem_cgroup_put() on the returned non-NULL memcg.
2114 */
2115struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
2116 struct mem_cgroup *oom_domain)
2117{
2118 struct mem_cgroup *oom_group = NULL;
2119 struct mem_cgroup *memcg;
2120
2121 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
2122 return NULL;
2123
2124 if (!oom_domain)
2125 oom_domain = root_mem_cgroup;
2126
2127 rcu_read_lock();
2128
2129 memcg = mem_cgroup_from_task(victim);
7848ed62 2130 if (mem_cgroup_is_root(memcg))
3d8b38eb
RG
2131 goto out;
2132
48fe267c
RG
2133 /*
2134 * If the victim task has been asynchronously moved to a different
2135 * memory cgroup, we might end up killing tasks outside oom_domain.
2136 * In this case it's better to ignore memory.group.oom.
2137 */
2138 if (unlikely(!mem_cgroup_is_descendant(memcg, oom_domain)))
2139 goto out;
2140
3d8b38eb
RG
2141 /*
2142 * Traverse the memory cgroup hierarchy from the victim task's
2143 * cgroup up to the OOMing cgroup (or root) to find the
2144 * highest-level memory cgroup with oom.group set.
2145 */
2146 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
eaf7b66b 2147 if (READ_ONCE(memcg->oom_group))
3d8b38eb
RG
2148 oom_group = memcg;
2149
2150 if (memcg == oom_domain)
2151 break;
2152 }
2153
2154 if (oom_group)
2155 css_get(&oom_group->css);
2156out:
2157 rcu_read_unlock();
2158
2159 return oom_group;
2160}
2161
2162void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
2163{
2164 pr_info("Tasks in ");
2165 pr_cont_cgroup_path(memcg->css.cgroup);
2166 pr_cont(" are going to be killed due to memory.oom.group set\n");
2167}
2168
d7365e78 2169/**
f70ad448
MWO
2170 * folio_memcg_lock - Bind a folio to its memcg.
2171 * @folio: The folio.
32047e2a 2172 *
f70ad448 2173 * This function prevents unlocked LRU folios from being moved to
739f79fc
JW
2174 * another cgroup.
2175 *
f70ad448
MWO
2176 * It ensures lifetime of the bound memcg. The caller is responsible
2177 * for the lifetime of the folio.
d69b042f 2178 */
f70ad448 2179void folio_memcg_lock(struct folio *folio)
89c06bd5
KH
2180{
2181 struct mem_cgroup *memcg;
6de22619 2182 unsigned long flags;
89c06bd5 2183
6de22619
JW
2184 /*
2185 * The RCU lock is held throughout the transaction. The fast
2186 * path can get away without acquiring the memcg->move_lock
2187 * because page moving starts with an RCU grace period.
739f79fc 2188 */
d7365e78
JW
2189 rcu_read_lock();
2190
2191 if (mem_cgroup_disabled())
1c824a68 2192 return;
89c06bd5 2193again:
f70ad448 2194 memcg = folio_memcg(folio);
29833315 2195 if (unlikely(!memcg))
1c824a68 2196 return;
d7365e78 2197
20ad50d6
AS
2198#ifdef CONFIG_PROVE_LOCKING
2199 local_irq_save(flags);
2200 might_lock(&memcg->move_lock);
2201 local_irq_restore(flags);
2202#endif
2203
bdcbb659 2204 if (atomic_read(&memcg->moving_account) <= 0)
1c824a68 2205 return;
89c06bd5 2206
6de22619 2207 spin_lock_irqsave(&memcg->move_lock, flags);
f70ad448 2208 if (memcg != folio_memcg(folio)) {
6de22619 2209 spin_unlock_irqrestore(&memcg->move_lock, flags);
89c06bd5
KH
2210 goto again;
2211 }
6de22619
JW
2212
2213 /*
1c824a68
JW
2214 * When charge migration first begins, we can have multiple
2215 * critical sections holding the fast-path RCU lock and one
2216 * holding the slowpath move_lock. Track the task who has the
6c77b607 2217 * move_lock for folio_memcg_unlock().
6de22619
JW
2218 */
2219 memcg->move_lock_task = current;
2220 memcg->move_lock_flags = flags;
89c06bd5 2221}
f70ad448 2222
f70ad448 2223static void __folio_memcg_unlock(struct mem_cgroup *memcg)
89c06bd5 2224{
6de22619
JW
2225 if (memcg && memcg->move_lock_task == current) {
2226 unsigned long flags = memcg->move_lock_flags;
2227
2228 memcg->move_lock_task = NULL;
2229 memcg->move_lock_flags = 0;
2230
2231 spin_unlock_irqrestore(&memcg->move_lock, flags);
2232 }
89c06bd5 2233
d7365e78 2234 rcu_read_unlock();
89c06bd5 2235}
739f79fc
JW
2236
2237/**
f70ad448
MWO
2238 * folio_memcg_unlock - Release the binding between a folio and its memcg.
2239 * @folio: The folio.
2240 *
2241 * This releases the binding created by folio_memcg_lock(). This does
2242 * not change the accounting of this folio to its memcg, but it does
2243 * permit others to change it.
739f79fc 2244 */
f70ad448 2245void folio_memcg_unlock(struct folio *folio)
739f79fc 2246{
f70ad448
MWO
2247 __folio_memcg_unlock(folio_memcg(folio));
2248}
9da7b521 2249
fead2b86 2250struct memcg_stock_pcp {
56751146 2251 local_lock_t stock_lock;
fead2b86
MH
2252 struct mem_cgroup *cached; /* this never be root cgroup */
2253 unsigned int nr_pages;
2254
bf4f0599
RG
2255#ifdef CONFIG_MEMCG_KMEM
2256 struct obj_cgroup *cached_objcg;
68ac5b3c 2257 struct pglist_data *cached_pgdat;
bf4f0599 2258 unsigned int nr_bytes;
68ac5b3c
WL
2259 int nr_slab_reclaimable_b;
2260 int nr_slab_unreclaimable_b;
bf4f0599
RG
2261#endif
2262
cdec2e42 2263 struct work_struct work;
26fe6168 2264 unsigned long flags;
a0db00fc 2265#define FLUSHING_CACHED_CHARGE 0
cdec2e42 2266};
56751146
SAS
2267static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock) = {
2268 .stock_lock = INIT_LOCAL_LOCK(stock_lock),
2269};
9f50fad6 2270static DEFINE_MUTEX(percpu_charge_mutex);
cdec2e42 2271
bf4f0599 2272#ifdef CONFIG_MEMCG_KMEM
56751146 2273static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock);
bf4f0599
RG
2274static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
2275 struct mem_cgroup *root_memcg);
a8c49af3 2276static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages);
bf4f0599
RG
2277
2278#else
56751146 2279static inline struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock)
bf4f0599 2280{
56751146 2281 return NULL;
bf4f0599
RG
2282}
2283static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
2284 struct mem_cgroup *root_memcg)
2285{
2286 return false;
2287}
a8c49af3
YA
2288static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages)
2289{
2290}
bf4f0599
RG
2291#endif
2292
a0956d54
SS
2293/**
2294 * consume_stock: Try to consume stocked charge on this cpu.
2295 * @memcg: memcg to consume from.
2296 * @nr_pages: how many pages to charge.
2297 *
2298 * The charges will only happen if @memcg matches the current cpu's memcg
2299 * stock, and at least @nr_pages are available in that stock. Failure to
2300 * service an allocation will refill the stock.
2301 *
2302 * returns true if successful, false otherwise.
cdec2e42 2303 */
a0956d54 2304static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
cdec2e42
KH
2305{
2306 struct memcg_stock_pcp *stock;
db2ba40c 2307 unsigned long flags;
3e32cb2e 2308 bool ret = false;
cdec2e42 2309
a983b5eb 2310 if (nr_pages > MEMCG_CHARGE_BATCH)
3e32cb2e 2311 return ret;
a0956d54 2312
56751146 2313 local_lock_irqsave(&memcg_stock.stock_lock, flags);
db2ba40c
JW
2314
2315 stock = this_cpu_ptr(&memcg_stock);
f785a8f2 2316 if (memcg == READ_ONCE(stock->cached) && stock->nr_pages >= nr_pages) {
a0956d54 2317 stock->nr_pages -= nr_pages;
3e32cb2e
JW
2318 ret = true;
2319 }
db2ba40c 2320
56751146 2321 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
db2ba40c 2322
cdec2e42
KH
2323 return ret;
2324}
2325
2326/*
3e32cb2e 2327 * Returns stocks cached in percpu and reset cached information.
cdec2e42
KH
2328 */
2329static void drain_stock(struct memcg_stock_pcp *stock)
2330{
f785a8f2 2331 struct mem_cgroup *old = READ_ONCE(stock->cached);
cdec2e42 2332
1a3e1f40
JW
2333 if (!old)
2334 return;
2335
11c9ea4e 2336 if (stock->nr_pages) {
3e32cb2e 2337 page_counter_uncharge(&old->memory, stock->nr_pages);
7941d214 2338 if (do_memsw_account())
3e32cb2e 2339 page_counter_uncharge(&old->memsw, stock->nr_pages);
11c9ea4e 2340 stock->nr_pages = 0;
cdec2e42 2341 }
1a3e1f40
JW
2342
2343 css_put(&old->css);
f785a8f2 2344 WRITE_ONCE(stock->cached, NULL);
cdec2e42
KH
2345}
2346
cdec2e42
KH
2347static void drain_local_stock(struct work_struct *dummy)
2348{
db2ba40c 2349 struct memcg_stock_pcp *stock;
56751146 2350 struct obj_cgroup *old = NULL;
db2ba40c
JW
2351 unsigned long flags;
2352
72f0184c 2353 /*
5c49cf9a
MH
2354 * The only protection from cpu hotplug (memcg_hotplug_cpu_dead) vs.
2355 * drain_stock races is that we always operate on local CPU stock
2356 * here with IRQ disabled
72f0184c 2357 */
56751146 2358 local_lock_irqsave(&memcg_stock.stock_lock, flags);
db2ba40c
JW
2359
2360 stock = this_cpu_ptr(&memcg_stock);
56751146 2361 old = drain_obj_stock(stock);
cdec2e42 2362 drain_stock(stock);
26fe6168 2363 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
db2ba40c 2364
56751146
SAS
2365 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
2366 if (old)
2367 obj_cgroup_put(old);
cdec2e42
KH
2368}
2369
2370/*
3e32cb2e 2371 * Cache charges(val) to local per_cpu area.
320cc51d 2372 * This will be consumed by consume_stock() function, later.
cdec2e42 2373 */
af9a3b69 2374static void __refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
cdec2e42 2375{
db2ba40c 2376 struct memcg_stock_pcp *stock;
cdec2e42 2377
db2ba40c 2378 stock = this_cpu_ptr(&memcg_stock);
f785a8f2 2379 if (READ_ONCE(stock->cached) != memcg) { /* reset if necessary */
cdec2e42 2380 drain_stock(stock);
1a3e1f40 2381 css_get(&memcg->css);
f785a8f2 2382 WRITE_ONCE(stock->cached, memcg);
cdec2e42 2383 }
11c9ea4e 2384 stock->nr_pages += nr_pages;
db2ba40c 2385
a983b5eb 2386 if (stock->nr_pages > MEMCG_CHARGE_BATCH)
475d0487 2387 drain_stock(stock);
af9a3b69
JW
2388}
2389
2390static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
2391{
2392 unsigned long flags;
475d0487 2393
56751146 2394 local_lock_irqsave(&memcg_stock.stock_lock, flags);
af9a3b69 2395 __refill_stock(memcg, nr_pages);
56751146 2396 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
cdec2e42
KH
2397}
2398
2399/*
c0ff4b85 2400 * Drains all per-CPU charge caches for given root_memcg resp. subtree
6d3d6aa2 2401 * of the hierarchy under it.
cdec2e42 2402 */
6d3d6aa2 2403static void drain_all_stock(struct mem_cgroup *root_memcg)
cdec2e42 2404{
26fe6168 2405 int cpu, curcpu;
d38144b7 2406
6d3d6aa2
JW
2407 /* If someone's already draining, avoid adding running more workers. */
2408 if (!mutex_trylock(&percpu_charge_mutex))
2409 return;
72f0184c
MH
2410 /*
2411 * Notify other cpus that system-wide "drain" is running
2412 * We do not care about races with the cpu hotplug because cpu down
2413 * as well as workers from this path always operate on the local
2414 * per-cpu data. CPU up doesn't touch memcg_stock at all.
2415 */
0790ed62
SAS
2416 migrate_disable();
2417 curcpu = smp_processor_id();
cdec2e42
KH
2418 for_each_online_cpu(cpu) {
2419 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
c0ff4b85 2420 struct mem_cgroup *memcg;
e1a366be 2421 bool flush = false;
26fe6168 2422
e1a366be 2423 rcu_read_lock();
f785a8f2 2424 memcg = READ_ONCE(stock->cached);
e1a366be
RG
2425 if (memcg && stock->nr_pages &&
2426 mem_cgroup_is_descendant(memcg, root_memcg))
2427 flush = true;
27fb0956 2428 else if (obj_stock_flush_required(stock, root_memcg))
bf4f0599 2429 flush = true;
e1a366be
RG
2430 rcu_read_unlock();
2431
2432 if (flush &&
2433 !test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
d1a05b69
MH
2434 if (cpu == curcpu)
2435 drain_local_stock(&stock->work);
6a792697 2436 else if (!cpu_is_isolated(cpu))
d1a05b69
MH
2437 schedule_work_on(cpu, &stock->work);
2438 }
cdec2e42 2439 }
0790ed62 2440 migrate_enable();
9f50fad6 2441 mutex_unlock(&percpu_charge_mutex);
cdec2e42
KH
2442}
2443
2cd21c89
JW
2444static int memcg_hotplug_cpu_dead(unsigned int cpu)
2445{
2446 struct memcg_stock_pcp *stock;
a3d4c05a 2447
2cd21c89
JW
2448 stock = &per_cpu(memcg_stock, cpu);
2449 drain_stock(stock);
a3d4c05a 2450
308167fc 2451 return 0;
cdec2e42
KH
2452}
2453
b3ff9291
CD
2454static unsigned long reclaim_high(struct mem_cgroup *memcg,
2455 unsigned int nr_pages,
2456 gfp_t gfp_mask)
f7e1cb6e 2457{
b3ff9291
CD
2458 unsigned long nr_reclaimed = 0;
2459
f7e1cb6e 2460 do {
e22c6ed9
JW
2461 unsigned long pflags;
2462
d1663a90
JK
2463 if (page_counter_read(&memcg->memory) <=
2464 READ_ONCE(memcg->memory.high))
f7e1cb6e 2465 continue;
e22c6ed9 2466
e27be240 2467 memcg_memory_event(memcg, MEMCG_HIGH);
e22c6ed9
JW
2468
2469 psi_memstall_enter(&pflags);
b3ff9291 2470 nr_reclaimed += try_to_free_mem_cgroup_pages(memcg, nr_pages,
73b73bac 2471 gfp_mask,
55ab834a 2472 MEMCG_RECLAIM_MAY_SWAP);
e22c6ed9 2473 psi_memstall_leave(&pflags);
4bf17307
CD
2474 } while ((memcg = parent_mem_cgroup(memcg)) &&
2475 !mem_cgroup_is_root(memcg));
b3ff9291
CD
2476
2477 return nr_reclaimed;
f7e1cb6e
JW
2478}
2479
2480static void high_work_func(struct work_struct *work)
2481{
2482 struct mem_cgroup *memcg;
2483
2484 memcg = container_of(work, struct mem_cgroup, high_work);
a983b5eb 2485 reclaim_high(memcg, MEMCG_CHARGE_BATCH, GFP_KERNEL);
f7e1cb6e
JW
2486}
2487
0e4b01df
CD
2488/*
2489 * Clamp the maximum sleep time per allocation batch to 2 seconds. This is
2490 * enough to still cause a significant slowdown in most cases, while still
2491 * allowing diagnostics and tracing to proceed without becoming stuck.
2492 */
2493#define MEMCG_MAX_HIGH_DELAY_JIFFIES (2UL*HZ)
2494
2495/*
2496 * When calculating the delay, we use these either side of the exponentiation to
2497 * maintain precision and scale to a reasonable number of jiffies (see the table
2498 * below.
2499 *
2500 * - MEMCG_DELAY_PRECISION_SHIFT: Extra precision bits while translating the
2501 * overage ratio to a delay.
ac5ddd0f 2502 * - MEMCG_DELAY_SCALING_SHIFT: The number of bits to scale down the
0e4b01df
CD
2503 * proposed penalty in order to reduce to a reasonable number of jiffies, and
2504 * to produce a reasonable delay curve.
2505 *
2506 * MEMCG_DELAY_SCALING_SHIFT just happens to be a number that produces a
2507 * reasonable delay curve compared to precision-adjusted overage, not
2508 * penalising heavily at first, but still making sure that growth beyond the
2509 * limit penalises misbehaviour cgroups by slowing them down exponentially. For
2510 * example, with a high of 100 megabytes:
2511 *
2512 * +-------+------------------------+
2513 * | usage | time to allocate in ms |
2514 * +-------+------------------------+
2515 * | 100M | 0 |
2516 * | 101M | 6 |
2517 * | 102M | 25 |
2518 * | 103M | 57 |
2519 * | 104M | 102 |
2520 * | 105M | 159 |
2521 * | 106M | 230 |
2522 * | 107M | 313 |
2523 * | 108M | 409 |
2524 * | 109M | 518 |
2525 * | 110M | 639 |
2526 * | 111M | 774 |
2527 * | 112M | 921 |
2528 * | 113M | 1081 |
2529 * | 114M | 1254 |
2530 * | 115M | 1439 |
2531 * | 116M | 1638 |
2532 * | 117M | 1849 |
2533 * | 118M | 2000 |
2534 * | 119M | 2000 |
2535 * | 120M | 2000 |
2536 * +-------+------------------------+
2537 */
2538 #define MEMCG_DELAY_PRECISION_SHIFT 20
2539 #define MEMCG_DELAY_SCALING_SHIFT 14
2540
8a5dbc65 2541static u64 calculate_overage(unsigned long usage, unsigned long high)
b23afb93 2542{
8a5dbc65 2543 u64 overage;
b23afb93 2544
8a5dbc65
JK
2545 if (usage <= high)
2546 return 0;
e26733e0 2547
8a5dbc65
JK
2548 /*
2549 * Prevent division by 0 in overage calculation by acting as if
2550 * it was a threshold of 1 page
2551 */
2552 high = max(high, 1UL);
9b8b1754 2553
8a5dbc65
JK
2554 overage = usage - high;
2555 overage <<= MEMCG_DELAY_PRECISION_SHIFT;
2556 return div64_u64(overage, high);
2557}
e26733e0 2558
8a5dbc65
JK
2559static u64 mem_find_max_overage(struct mem_cgroup *memcg)
2560{
2561 u64 overage, max_overage = 0;
e26733e0 2562
8a5dbc65
JK
2563 do {
2564 overage = calculate_overage(page_counter_read(&memcg->memory),
d1663a90 2565 READ_ONCE(memcg->memory.high));
8a5dbc65 2566 max_overage = max(overage, max_overage);
e26733e0
CD
2567 } while ((memcg = parent_mem_cgroup(memcg)) &&
2568 !mem_cgroup_is_root(memcg));
2569
8a5dbc65
JK
2570 return max_overage;
2571}
2572
4b82ab4f
JK
2573static u64 swap_find_max_overage(struct mem_cgroup *memcg)
2574{
2575 u64 overage, max_overage = 0;
2576
2577 do {
2578 overage = calculate_overage(page_counter_read(&memcg->swap),
2579 READ_ONCE(memcg->swap.high));
2580 if (overage)
2581 memcg_memory_event(memcg, MEMCG_SWAP_HIGH);
2582 max_overage = max(overage, max_overage);
2583 } while ((memcg = parent_mem_cgroup(memcg)) &&
2584 !mem_cgroup_is_root(memcg));
2585
2586 return max_overage;
2587}
2588
8a5dbc65
JK
2589/*
2590 * Get the number of jiffies that we should penalise a mischievous cgroup which
2591 * is exceeding its memory.high by checking both it and its ancestors.
2592 */
2593static unsigned long calculate_high_delay(struct mem_cgroup *memcg,
2594 unsigned int nr_pages,
2595 u64 max_overage)
2596{
2597 unsigned long penalty_jiffies;
2598
e26733e0
CD
2599 if (!max_overage)
2600 return 0;
0e4b01df
CD
2601
2602 /*
0e4b01df
CD
2603 * We use overage compared to memory.high to calculate the number of
2604 * jiffies to sleep (penalty_jiffies). Ideally this value should be
2605 * fairly lenient on small overages, and increasingly harsh when the
2606 * memcg in question makes it clear that it has no intention of stopping
2607 * its crazy behaviour, so we exponentially increase the delay based on
2608 * overage amount.
2609 */
e26733e0
CD
2610 penalty_jiffies = max_overage * max_overage * HZ;
2611 penalty_jiffies >>= MEMCG_DELAY_PRECISION_SHIFT;
2612 penalty_jiffies >>= MEMCG_DELAY_SCALING_SHIFT;
0e4b01df
CD
2613
2614 /*
2615 * Factor in the task's own contribution to the overage, such that four
2616 * N-sized allocations are throttled approximately the same as one
2617 * 4N-sized allocation.
2618 *
2619 * MEMCG_CHARGE_BATCH pages is nominal, so work out how much smaller or
2620 * larger the current charge patch is than that.
2621 */
ff144e69 2622 return penalty_jiffies * nr_pages / MEMCG_CHARGE_BATCH;
e26733e0
CD
2623}
2624
2625/*
63fd3270
JW
2626 * Reclaims memory over the high limit. Called directly from
2627 * try_charge() (context permitting), as well as from the userland
2628 * return path where reclaim is always able to block.
e26733e0 2629 */
9ea9cb00 2630void mem_cgroup_handle_over_high(gfp_t gfp_mask)
e26733e0
CD
2631{
2632 unsigned long penalty_jiffies;
2633 unsigned long pflags;
b3ff9291 2634 unsigned long nr_reclaimed;
e26733e0 2635 unsigned int nr_pages = current->memcg_nr_pages_over_high;
d977aa93 2636 int nr_retries = MAX_RECLAIM_RETRIES;
e26733e0 2637 struct mem_cgroup *memcg;
b3ff9291 2638 bool in_retry = false;
e26733e0
CD
2639
2640 if (likely(!nr_pages))
2641 return;
2642
2643 memcg = get_mem_cgroup_from_mm(current->mm);
e26733e0
CD
2644 current->memcg_nr_pages_over_high = 0;
2645
b3ff9291 2646retry_reclaim:
63fd3270
JW
2647 /*
2648 * Bail if the task is already exiting. Unlike memory.max,
2649 * memory.high enforcement isn't as strict, and there is no
2650 * OOM killer involved, which means the excess could already
2651 * be much bigger (and still growing) than it could for
2652 * memory.max; the dying task could get stuck in fruitless
2653 * reclaim for a long time, which isn't desirable.
2654 */
2655 if (task_is_dying())
2656 goto out;
2657
b3ff9291
CD
2658 /*
2659 * The allocating task should reclaim at least the batch size, but for
2660 * subsequent retries we only want to do what's necessary to prevent oom
2661 * or breaching resource isolation.
2662 *
2663 * This is distinct from memory.max or page allocator behaviour because
2664 * memory.high is currently batched, whereas memory.max and the page
2665 * allocator run every time an allocation is made.
2666 */
2667 nr_reclaimed = reclaim_high(memcg,
2668 in_retry ? SWAP_CLUSTER_MAX : nr_pages,
9ea9cb00 2669 gfp_mask);
b3ff9291 2670
e26733e0
CD
2671 /*
2672 * memory.high is breached and reclaim is unable to keep up. Throttle
2673 * allocators proactively to slow down excessive growth.
2674 */
8a5dbc65
JK
2675 penalty_jiffies = calculate_high_delay(memcg, nr_pages,
2676 mem_find_max_overage(memcg));
0e4b01df 2677
4b82ab4f
JK
2678 penalty_jiffies += calculate_high_delay(memcg, nr_pages,
2679 swap_find_max_overage(memcg));
2680
ff144e69
JK
2681 /*
2682 * Clamp the max delay per usermode return so as to still keep the
2683 * application moving forwards and also permit diagnostics, albeit
2684 * extremely slowly.
2685 */
2686 penalty_jiffies = min(penalty_jiffies, MEMCG_MAX_HIGH_DELAY_JIFFIES);
2687
0e4b01df
CD
2688 /*
2689 * Don't sleep if the amount of jiffies this memcg owes us is so low
2690 * that it's not even worth doing, in an attempt to be nice to those who
2691 * go only a small amount over their memory.high value and maybe haven't
2692 * been aggressively reclaimed enough yet.
2693 */
2694 if (penalty_jiffies <= HZ / 100)
2695 goto out;
2696
b3ff9291
CD
2697 /*
2698 * If reclaim is making forward progress but we're still over
2699 * memory.high, we want to encourage that rather than doing allocator
2700 * throttling.
2701 */
2702 if (nr_reclaimed || nr_retries--) {
2703 in_retry = true;
2704 goto retry_reclaim;
2705 }
2706
0e4b01df 2707 /*
63fd3270
JW
2708 * Reclaim didn't manage to push usage below the limit, slow
2709 * this allocating task down.
2710 *
0e4b01df
CD
2711 * If we exit early, we're guaranteed to die (since
2712 * schedule_timeout_killable sets TASK_KILLABLE). This means we don't
2713 * need to account for any ill-begotten jiffies to pay them off later.
2714 */
2715 psi_memstall_enter(&pflags);
2716 schedule_timeout_killable(penalty_jiffies);
2717 psi_memstall_leave(&pflags);
2718
2719out:
2720 css_put(&memcg->css);
b23afb93
TH
2721}
2722
c5c8b16b
MS
2723static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
2724 unsigned int nr_pages)
8a9f3ccd 2725{
a983b5eb 2726 unsigned int batch = max(MEMCG_CHARGE_BATCH, nr_pages);
d977aa93 2727 int nr_retries = MAX_RECLAIM_RETRIES;
6539cc05 2728 struct mem_cgroup *mem_over_limit;
3e32cb2e 2729 struct page_counter *counter;
6539cc05 2730 unsigned long nr_reclaimed;
a4ebf1b6 2731 bool passed_oom = false;
73b73bac 2732 unsigned int reclaim_options = MEMCG_RECLAIM_MAY_SWAP;
b70a2a21 2733 bool drained = false;
d6e103a7 2734 bool raised_max_event = false;
e22c6ed9 2735 unsigned long pflags;
a636b327 2736
6539cc05 2737retry:
b6b6cc72 2738 if (consume_stock(memcg, nr_pages))
10d53c74 2739 return 0;
8a9f3ccd 2740
7941d214 2741 if (!do_memsw_account() ||
6071ca52
JW
2742 page_counter_try_charge(&memcg->memsw, batch, &counter)) {
2743 if (page_counter_try_charge(&memcg->memory, batch, &counter))
6539cc05 2744 goto done_restock;
7941d214 2745 if (do_memsw_account())
3e32cb2e
JW
2746 page_counter_uncharge(&memcg->memsw, batch);
2747 mem_over_limit = mem_cgroup_from_counter(counter, memory);
3fbe7244 2748 } else {
3e32cb2e 2749 mem_over_limit = mem_cgroup_from_counter(counter, memsw);
73b73bac 2750 reclaim_options &= ~MEMCG_RECLAIM_MAY_SWAP;
3fbe7244 2751 }
7a81b88c 2752
6539cc05
JW
2753 if (batch > nr_pages) {
2754 batch = nr_pages;
2755 goto retry;
2756 }
6d61ef40 2757
89a28483
JW
2758 /*
2759 * Prevent unbounded recursion when reclaim operations need to
2760 * allocate memory. This might exceed the limits temporarily,
2761 * but we prefer facilitating memory reclaim and getting back
2762 * under the limit over triggering OOM kills in these cases.
2763 */
2764 if (unlikely(current->flags & PF_MEMALLOC))
2765 goto force;
2766
06b078fc
JW
2767 if (unlikely(task_in_memcg_oom(current)))
2768 goto nomem;
2769
d0164adc 2770 if (!gfpflags_allow_blocking(gfp_mask))
6539cc05 2771 goto nomem;
4b534334 2772
e27be240 2773 memcg_memory_event(mem_over_limit, MEMCG_MAX);
d6e103a7 2774 raised_max_event = true;
241994ed 2775
e22c6ed9 2776 psi_memstall_enter(&pflags);
b70a2a21 2777 nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
55ab834a 2778 gfp_mask, reclaim_options);
e22c6ed9 2779 psi_memstall_leave(&pflags);
6539cc05 2780
61e02c74 2781 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
6539cc05 2782 goto retry;
28c34c29 2783
b70a2a21 2784 if (!drained) {
6d3d6aa2 2785 drain_all_stock(mem_over_limit);
b70a2a21
JW
2786 drained = true;
2787 goto retry;
2788 }
2789
28c34c29
JW
2790 if (gfp_mask & __GFP_NORETRY)
2791 goto nomem;
6539cc05
JW
2792 /*
2793 * Even though the limit is exceeded at this point, reclaim
2794 * may have been able to free some pages. Retry the charge
2795 * before killing the task.
2796 *
2797 * Only for regular pages, though: huge pages are rather
2798 * unlikely to succeed so close to the limit, and we fall back
2799 * to regular pages anyway in case of failure.
2800 */
61e02c74 2801 if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
6539cc05
JW
2802 goto retry;
2803 /*
2804 * At task move, charge accounts can be doubly counted. So, it's
2805 * better to wait until the end of task_move if something is going on.
2806 */
2807 if (mem_cgroup_wait_acct_move(mem_over_limit))
2808 goto retry;
2809
9b130619
JW
2810 if (nr_retries--)
2811 goto retry;
2812
38d38493 2813 if (gfp_mask & __GFP_RETRY_MAYFAIL)
29ef680a
MH
2814 goto nomem;
2815
a4ebf1b6
VA
2816 /* Avoid endless loop for tasks bypassed by the oom killer */
2817 if (passed_oom && task_is_dying())
2818 goto nomem;
6539cc05 2819
29ef680a
MH
2820 /*
2821 * keep retrying as long as the memcg oom killer is able to make
2822 * a forward progress or bypass the charge if the oom killer
2823 * couldn't make any progress.
2824 */
becdf89d
SB
2825 if (mem_cgroup_oom(mem_over_limit, gfp_mask,
2826 get_order(nr_pages * PAGE_SIZE))) {
a4ebf1b6 2827 passed_oom = true;
d977aa93 2828 nr_retries = MAX_RECLAIM_RETRIES;
29ef680a 2829 goto retry;
29ef680a 2830 }
7a81b88c 2831nomem:
1461e8c2
SB
2832 /*
2833 * Memcg doesn't have a dedicated reserve for atomic
2834 * allocations. But like the global atomic pool, we need to
2835 * put the burden of reclaim on regular allocation requests
2836 * and let these go through as privileged allocations.
2837 */
2838 if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH)))
3168ecbe 2839 return -ENOMEM;
10d53c74 2840force:
d6e103a7
RG
2841 /*
2842 * If the allocation has to be enforced, don't forget to raise
2843 * a MEMCG_MAX event.
2844 */
2845 if (!raised_max_event)
2846 memcg_memory_event(mem_over_limit, MEMCG_MAX);
2847
10d53c74
TH
2848 /*
2849 * The allocation either can't fail or will lead to more memory
2850 * being freed very soon. Allow memory usage go over the limit
2851 * temporarily by force charging it.
2852 */
2853 page_counter_charge(&memcg->memory, nr_pages);
7941d214 2854 if (do_memsw_account())
10d53c74 2855 page_counter_charge(&memcg->memsw, nr_pages);
10d53c74
TH
2856
2857 return 0;
6539cc05
JW
2858
2859done_restock:
2860 if (batch > nr_pages)
2861 refill_stock(memcg, batch - nr_pages);
b23afb93 2862
241994ed 2863 /*
b23afb93
TH
2864 * If the hierarchy is above the normal consumption range, schedule
2865 * reclaim on returning to userland. We can perform reclaim here
71baba4b 2866 * if __GFP_RECLAIM but let's always punt for simplicity and so that
b23afb93
TH
2867 * GFP_KERNEL can consistently be used during reclaim. @memcg is
2868 * not recorded as it most likely matches current's and won't
2869 * change in the meantime. As high limit is checked again before
2870 * reclaim, the cost of mismatch is negligible.
241994ed
JW
2871 */
2872 do {
4b82ab4f
JK
2873 bool mem_high, swap_high;
2874
2875 mem_high = page_counter_read(&memcg->memory) >
2876 READ_ONCE(memcg->memory.high);
2877 swap_high = page_counter_read(&memcg->swap) >
2878 READ_ONCE(memcg->swap.high);
2879
2880 /* Don't bother a random interrupted task */
086f694a 2881 if (!in_task()) {
4b82ab4f 2882 if (mem_high) {
f7e1cb6e
JW
2883 schedule_work(&memcg->high_work);
2884 break;
2885 }
4b82ab4f
JK
2886 continue;
2887 }
2888
2889 if (mem_high || swap_high) {
2890 /*
2891 * The allocating tasks in this cgroup will need to do
2892 * reclaim or be throttled to prevent further growth
2893 * of the memory or swap footprints.
2894 *
2895 * Target some best-effort fairness between the tasks,
2896 * and distribute reclaim work and delay penalties
2897 * based on how much each task is actually allocating.
2898 */
9516a18a 2899 current->memcg_nr_pages_over_high += batch;
b23afb93
TH
2900 set_notify_resume(current);
2901 break;
2902 }
241994ed 2903 } while ((memcg = parent_mem_cgroup(memcg)));
10d53c74 2904
63fd3270
JW
2905 /*
2906 * Reclaim is set up above to be called from the userland
2907 * return path. But also attempt synchronous reclaim to avoid
2908 * excessive overrun while the task is still inside the
2909 * kernel. If this is successful, the return path will see it
2910 * when it rechecks the overage and simply bail out.
2911 */
c9afe31e
SB
2912 if (current->memcg_nr_pages_over_high > MEMCG_CHARGE_BATCH &&
2913 !(current->flags & PF_MEMALLOC) &&
63fd3270 2914 gfpflags_allow_blocking(gfp_mask))
9ea9cb00 2915 mem_cgroup_handle_over_high(gfp_mask);
10d53c74 2916 return 0;
7a81b88c 2917}
8a9f3ccd 2918
c5c8b16b
MS
2919static inline int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
2920 unsigned int nr_pages)
2921{
2922 if (mem_cgroup_is_root(memcg))
2923 return 0;
2924
2925 return try_charge_memcg(memcg, gfp_mask, nr_pages);
2926}
2927
4b569387
NP
2928/**
2929 * mem_cgroup_cancel_charge() - cancel an uncommitted try_charge() call.
2930 * @memcg: memcg previously charged.
2931 * @nr_pages: number of pages previously charged.
2932 */
2933void mem_cgroup_cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages)
a3032a2c 2934{
ce00a967
JW
2935 if (mem_cgroup_is_root(memcg))
2936 return;
2937
3e32cb2e 2938 page_counter_uncharge(&memcg->memory, nr_pages);
7941d214 2939 if (do_memsw_account())
3e32cb2e 2940 page_counter_uncharge(&memcg->memsw, nr_pages);
d01dd17f
KH
2941}
2942
118f2875 2943static void commit_charge(struct folio *folio, struct mem_cgroup *memcg)
0a31bc97 2944{
118f2875 2945 VM_BUG_ON_FOLIO(folio_memcg(folio), folio);
0a31bc97 2946 /*
a5eb011a 2947 * Any of the following ensures page's memcg stability:
0a31bc97 2948 *
a0b5b414
JW
2949 * - the page lock
2950 * - LRU isolation
6c77b607 2951 * - folio_memcg_lock()
a0b5b414 2952 * - exclusive reference
018ee47f 2953 * - mem_cgroup_trylock_pages()
0a31bc97 2954 */
118f2875 2955 folio->memcg_data = (unsigned long)memcg;
7a81b88c 2956}
66e1707b 2957
4b569387
NP
2958/**
2959 * mem_cgroup_commit_charge - commit a previously successful try_charge().
2960 * @folio: folio to commit the charge to.
2961 * @memcg: memcg previously charged.
2962 */
2963void mem_cgroup_commit_charge(struct folio *folio, struct mem_cgroup *memcg)
2964{
2965 css_get(&memcg->css);
2966 commit_charge(folio, memcg);
2967
2968 local_irq_disable();
2969 mem_cgroup_charge_statistics(memcg, folio_nr_pages(folio));
2970 memcg_check_events(memcg, folio_nid(folio));
2971 local_irq_enable();
2972}
2973
84c07d11 2974#ifdef CONFIG_MEMCG_KMEM
41eb5df1
WL
2975/*
2976 * The allocated objcg pointers array is not accounted directly.
2977 * Moreover, it should not come from DMA buffer and is not readily
2978 * reclaimable. So those GFP bits should be masked off.
2979 */
24948e3b
RG
2980#define OBJCGS_CLEAR_MASK (__GFP_DMA | __GFP_RECLAIMABLE | \
2981 __GFP_ACCOUNT | __GFP_NOFAIL)
41eb5df1 2982
a7ebf564
WL
2983/*
2984 * mod_objcg_mlstate() may be called with irq enabled, so
2985 * mod_memcg_lruvec_state() should be used.
2986 */
2987static inline void mod_objcg_mlstate(struct obj_cgroup *objcg,
2988 struct pglist_data *pgdat,
2989 enum node_stat_item idx, int nr)
2990{
2991 struct mem_cgroup *memcg;
2992 struct lruvec *lruvec;
2993
2994 rcu_read_lock();
2995 memcg = obj_cgroup_memcg(objcg);
2996 lruvec = mem_cgroup_lruvec(memcg, pgdat);
2997 mod_memcg_lruvec_state(lruvec, idx, nr);
2998 rcu_read_unlock();
2999}
3000
4b5f8d9a
VB
3001int memcg_alloc_slab_cgroups(struct slab *slab, struct kmem_cache *s,
3002 gfp_t gfp, bool new_slab)
10befea9 3003{
4b5f8d9a 3004 unsigned int objects = objs_per_slab(s, slab);
2e9bd483 3005 unsigned long memcg_data;
10befea9
RG
3006 void *vec;
3007
41eb5df1 3008 gfp &= ~OBJCGS_CLEAR_MASK;
10befea9 3009 vec = kcalloc_node(objects, sizeof(struct obj_cgroup *), gfp,
4b5f8d9a 3010 slab_nid(slab));
10befea9
RG
3011 if (!vec)
3012 return -ENOMEM;
3013
2e9bd483 3014 memcg_data = (unsigned long) vec | MEMCG_DATA_OBJCGS;
4b5f8d9a 3015 if (new_slab) {
2e9bd483 3016 /*
4b5f8d9a
VB
3017 * If the slab is brand new and nobody can yet access its
3018 * memcg_data, no synchronization is required and memcg_data can
3019 * be simply assigned.
2e9bd483 3020 */
4b5f8d9a
VB
3021 slab->memcg_data = memcg_data;
3022 } else if (cmpxchg(&slab->memcg_data, 0, memcg_data)) {
2e9bd483 3023 /*
4b5f8d9a
VB
3024 * If the slab is already in use, somebody can allocate and
3025 * assign obj_cgroups in parallel. In this case the existing
2e9bd483
RG
3026 * objcg vector should be reused.
3027 */
10befea9 3028 kfree(vec);
2e9bd483
RG
3029 return 0;
3030 }
10befea9 3031
2e9bd483 3032 kmemleak_not_leak(vec);
10befea9
RG
3033 return 0;
3034}
3035
fc4db90f
RG
3036static __always_inline
3037struct mem_cgroup *mem_cgroup_from_obj_folio(struct folio *folio, void *p)
8380ce47 3038{
8380ce47 3039 /*
9855609b
RG
3040 * Slab objects are accounted individually, not per-page.
3041 * Memcg membership data for each individual object is saved in
4b5f8d9a 3042 * slab->memcg_data.
8380ce47 3043 */
4b5f8d9a
VB
3044 if (folio_test_slab(folio)) {
3045 struct obj_cgroup **objcgs;
3046 struct slab *slab;
9855609b
RG
3047 unsigned int off;
3048
4b5f8d9a
VB
3049 slab = folio_slab(folio);
3050 objcgs = slab_objcgs(slab);
3051 if (!objcgs)
3052 return NULL;
3053
3054 off = obj_to_index(slab->slab_cache, slab, p);
3055 if (objcgs[off])
3056 return obj_cgroup_memcg(objcgs[off]);
10befea9
RG
3057
3058 return NULL;
9855609b 3059 }
8380ce47 3060
bcfe06bf 3061 /*
becacb04 3062 * folio_memcg_check() is used here, because in theory we can encounter
4b5f8d9a
VB
3063 * a folio where the slab flag has been cleared already, but
3064 * slab->memcg_data has not been freed yet
becacb04 3065 * folio_memcg_check() will guarantee that a proper memory
bcfe06bf
RG
3066 * cgroup pointer or NULL will be returned.
3067 */
becacb04 3068 return folio_memcg_check(folio);
8380ce47
RG
3069}
3070
fc4db90f
RG
3071/*
3072 * Returns a pointer to the memory cgroup to which the kernel object is charged.
3073 *
3074 * A passed kernel object can be a slab object, vmalloc object or a generic
3075 * kernel page, so different mechanisms for getting the memory cgroup pointer
3076 * should be used.
3077 *
3078 * In certain cases (e.g. kernel stacks or large kmallocs with SLUB) the caller
3079 * can not know for sure how the kernel object is implemented.
3080 * mem_cgroup_from_obj() can be safely used in such cases.
3081 *
3082 * The caller must ensure the memcg lifetime, e.g. by taking rcu_read_lock(),
3083 * cgroup_mutex, etc.
3084 */
3085struct mem_cgroup *mem_cgroup_from_obj(void *p)
3086{
3087 struct folio *folio;
3088
3089 if (mem_cgroup_disabled())
3090 return NULL;
3091
3092 if (unlikely(is_vmalloc_addr(p)))
3093 folio = page_folio(vmalloc_to_page(p));
3094 else
3095 folio = virt_to_folio(p);
3096
3097 return mem_cgroup_from_obj_folio(folio, p);
3098}
3099
3100/*
3101 * Returns a pointer to the memory cgroup to which the kernel object is charged.
3102 * Similar to mem_cgroup_from_obj(), but faster and not suitable for objects,
3103 * allocated using vmalloc().
3104 *
3105 * A passed kernel object must be a slab object or a generic kernel page.
3106 *
3107 * The caller must ensure the memcg lifetime, e.g. by taking rcu_read_lock(),
3108 * cgroup_mutex, etc.
3109 */
3110struct mem_cgroup *mem_cgroup_from_slab_obj(void *p)
3111{
3112 if (mem_cgroup_disabled())
3113 return NULL;
3114
3115 return mem_cgroup_from_obj_folio(virt_to_folio(p), p);
3116}
3117
f4840ccf
JW
3118static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg)
3119{
3120 struct obj_cgroup *objcg = NULL;
3121
7848ed62 3122 for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) {
f4840ccf 3123 objcg = rcu_dereference(memcg->objcg);
7d0715d0 3124 if (likely(objcg && obj_cgroup_tryget(objcg)))
f4840ccf
JW
3125 break;
3126 objcg = NULL;
3127 }
3128 return objcg;
3129}
3130
1aacbd35
RG
3131static struct obj_cgroup *current_objcg_update(void)
3132{
3133 struct mem_cgroup *memcg;
3134 struct obj_cgroup *old, *objcg = NULL;
3135
3136 do {
3137 /* Atomically drop the update bit. */
3138 old = xchg(&current->objcg, NULL);
3139 if (old) {
3140 old = (struct obj_cgroup *)
3141 ((unsigned long)old & ~CURRENT_OBJCG_UPDATE_FLAG);
3142 if (old)
3143 obj_cgroup_put(old);
3144
3145 old = NULL;
3146 }
3147
3148 /* If new objcg is NULL, no reason for the second atomic update. */
3149 if (!current->mm || (current->flags & PF_KTHREAD))
3150 return NULL;
3151
3152 /*
3153 * Release the objcg pointer from the previous iteration,
3154 * if try_cmpxcg() below fails.
3155 */
3156 if (unlikely(objcg)) {
3157 obj_cgroup_put(objcg);
3158 objcg = NULL;
3159 }
3160
3161 /*
3162 * Obtain the new objcg pointer. The current task can be
3163 * asynchronously moved to another memcg and the previous
3164 * memcg can be offlined. So let's get the memcg pointer
3165 * and try get a reference to objcg under a rcu read lock.
3166 */
3167
3168 rcu_read_lock();
3169 memcg = mem_cgroup_from_task(current);
3170 objcg = __get_obj_cgroup_from_memcg(memcg);
3171 rcu_read_unlock();
3172
3173 /*
3174 * Try set up a new objcg pointer atomically. If it
3175 * fails, it means the update flag was set concurrently, so
3176 * the whole procedure should be repeated.
3177 */
3178 } while (!try_cmpxchg(&current->objcg, &old, objcg));
3179
3180 return objcg;
3181}
3182
e86828e5
RG
3183__always_inline struct obj_cgroup *current_obj_cgroup(void)
3184{
3185 struct mem_cgroup *memcg;
3186 struct obj_cgroup *objcg;
3187
3188 if (in_task()) {
3189 memcg = current->active_memcg;
3190 if (unlikely(memcg))
3191 goto from_memcg;
3192
3193 objcg = READ_ONCE(current->objcg);
3194 if (unlikely((unsigned long)objcg & CURRENT_OBJCG_UPDATE_FLAG))
3195 objcg = current_objcg_update();
3196 /*
3197 * Objcg reference is kept by the task, so it's safe
3198 * to use the objcg by the current task.
3199 */
3200 return objcg;
3201 }
3202
3203 memcg = this_cpu_read(int_active_memcg);
3204 if (unlikely(memcg))
3205 goto from_memcg;
3206
3207 return NULL;
3208
3209from_memcg:
5f79489a 3210 objcg = NULL;
e86828e5
RG
3211 for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) {
3212 /*
3213 * Memcg pointer is protected by scope (see set_active_memcg())
3214 * and is pinning the corresponding objcg, so objcg can't go
3215 * away and can be used within the scope without any additional
3216 * protection.
3217 */
3218 objcg = rcu_dereference_check(memcg->objcg, 1);
3219 if (likely(objcg))
3220 break;
e86828e5
RG
3221 }
3222
3223 return objcg;
3224}
3225
074e3e26 3226struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio)
f4840ccf
JW
3227{
3228 struct obj_cgroup *objcg;
3229
f7a449f7 3230 if (!memcg_kmem_online())
f4840ccf
JW
3231 return NULL;
3232
074e3e26
MWO
3233 if (folio_memcg_kmem(folio)) {
3234 objcg = __folio_objcg(folio);
f4840ccf
JW
3235 obj_cgroup_get(objcg);
3236 } else {
3237 struct mem_cgroup *memcg;
bf4f0599 3238
f4840ccf 3239 rcu_read_lock();
074e3e26 3240 memcg = __folio_memcg(folio);
f4840ccf
JW
3241 if (memcg)
3242 objcg = __get_obj_cgroup_from_memcg(memcg);
3243 else
3244 objcg = NULL;
3245 rcu_read_unlock();
3246 }
bf4f0599
RG
3247 return objcg;
3248}
3249
a8c49af3
YA
3250static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages)
3251{
3252 mod_memcg_state(memcg, MEMCG_KMEM, nr_pages);
3253 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
3254 if (nr_pages > 0)
3255 page_counter_charge(&memcg->kmem, nr_pages);
3256 else
3257 page_counter_uncharge(&memcg->kmem, -nr_pages);
3258 }
3259}
3260
3261
f1286fae
MS
3262/*
3263 * obj_cgroup_uncharge_pages: uncharge a number of kernel pages from a objcg
3264 * @objcg: object cgroup to uncharge
3265 * @nr_pages: number of pages to uncharge
3266 */
e74d2259
MS
3267static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg,
3268 unsigned int nr_pages)
3269{
3270 struct mem_cgroup *memcg;
3271
3272 memcg = get_mem_cgroup_from_objcg(objcg);
e74d2259 3273
a8c49af3 3274 memcg_account_kmem(memcg, -nr_pages);
f1286fae 3275 refill_stock(memcg, nr_pages);
e74d2259 3276
e74d2259 3277 css_put(&memcg->css);
e74d2259
MS
3278}
3279
f1286fae
MS
3280/*
3281 * obj_cgroup_charge_pages: charge a number of kernel pages to a objcg
3282 * @objcg: object cgroup to charge
45264778 3283 * @gfp: reclaim mode
92d0510c 3284 * @nr_pages: number of pages to charge
45264778
VD
3285 *
3286 * Returns 0 on success, an error code on failure.
3287 */
f1286fae
MS
3288static int obj_cgroup_charge_pages(struct obj_cgroup *objcg, gfp_t gfp,
3289 unsigned int nr_pages)
7ae1e1d0 3290{
f1286fae 3291 struct mem_cgroup *memcg;
7ae1e1d0
GC
3292 int ret;
3293
f1286fae
MS
3294 memcg = get_mem_cgroup_from_objcg(objcg);
3295
c5c8b16b 3296 ret = try_charge_memcg(memcg, gfp, nr_pages);
52c29b04 3297 if (ret)
f1286fae 3298 goto out;
52c29b04 3299
a8c49af3 3300 memcg_account_kmem(memcg, nr_pages);
f1286fae
MS
3301out:
3302 css_put(&memcg->css);
4b13f64d 3303
f1286fae 3304 return ret;
4b13f64d
RG
3305}
3306
45264778 3307/**
f4b00eab 3308 * __memcg_kmem_charge_page: charge a kmem page to the current memory cgroup
45264778
VD
3309 * @page: page to charge
3310 * @gfp: reclaim mode
3311 * @order: allocation order
3312 *
3313 * Returns 0 on success, an error code on failure.
3314 */
f4b00eab 3315int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order)
7ae1e1d0 3316{
b4e0b68f 3317 struct obj_cgroup *objcg;
fcff7d7e 3318 int ret = 0;
7ae1e1d0 3319
e86828e5 3320 objcg = current_obj_cgroup();
b4e0b68f
MS
3321 if (objcg) {
3322 ret = obj_cgroup_charge_pages(objcg, gfp, 1 << order);
4d96ba35 3323 if (!ret) {
e86828e5 3324 obj_cgroup_get(objcg);
b4e0b68f 3325 page->memcg_data = (unsigned long)objcg |
18b2db3b 3326 MEMCG_DATA_KMEM;
1a3e1f40 3327 return 0;
4d96ba35 3328 }
c4159a75 3329 }
d05e83a6 3330 return ret;
7ae1e1d0 3331}
49a18eae 3332
45264778 3333/**
f4b00eab 3334 * __memcg_kmem_uncharge_page: uncharge a kmem page
45264778
VD
3335 * @page: page to uncharge
3336 * @order: allocation order
3337 */
f4b00eab 3338void __memcg_kmem_uncharge_page(struct page *page, int order)
7ae1e1d0 3339{
1b7e4464 3340 struct folio *folio = page_folio(page);
b4e0b68f 3341 struct obj_cgroup *objcg;
f3ccb2c4 3342 unsigned int nr_pages = 1 << order;
7ae1e1d0 3343
1b7e4464 3344 if (!folio_memcg_kmem(folio))
7ae1e1d0
GC
3345 return;
3346
1b7e4464 3347 objcg = __folio_objcg(folio);
b4e0b68f 3348 obj_cgroup_uncharge_pages(objcg, nr_pages);
1b7e4464 3349 folio->memcg_data = 0;
b4e0b68f 3350 obj_cgroup_put(objcg);
60d3fd32 3351}
bf4f0599 3352
68ac5b3c
WL
3353void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat,
3354 enum node_stat_item idx, int nr)
3355{
fead2b86 3356 struct memcg_stock_pcp *stock;
56751146 3357 struct obj_cgroup *old = NULL;
68ac5b3c
WL
3358 unsigned long flags;
3359 int *bytes;
3360
56751146 3361 local_lock_irqsave(&memcg_stock.stock_lock, flags);
fead2b86
MH
3362 stock = this_cpu_ptr(&memcg_stock);
3363
68ac5b3c
WL
3364 /*
3365 * Save vmstat data in stock and skip vmstat array update unless
3366 * accumulating over a page of vmstat data or when pgdat or idx
3367 * changes.
3368 */
3b8abb32 3369 if (READ_ONCE(stock->cached_objcg) != objcg) {
56751146 3370 old = drain_obj_stock(stock);
68ac5b3c
WL
3371 obj_cgroup_get(objcg);
3372 stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes)
3373 ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0;
3b8abb32 3374 WRITE_ONCE(stock->cached_objcg, objcg);
68ac5b3c
WL
3375 stock->cached_pgdat = pgdat;
3376 } else if (stock->cached_pgdat != pgdat) {
3377 /* Flush the existing cached vmstat data */
7fa0dacb
WL
3378 struct pglist_data *oldpg = stock->cached_pgdat;
3379
68ac5b3c 3380 if (stock->nr_slab_reclaimable_b) {
7fa0dacb 3381 mod_objcg_mlstate(objcg, oldpg, NR_SLAB_RECLAIMABLE_B,
68ac5b3c
WL
3382 stock->nr_slab_reclaimable_b);
3383 stock->nr_slab_reclaimable_b = 0;
3384 }
3385 if (stock->nr_slab_unreclaimable_b) {
7fa0dacb 3386 mod_objcg_mlstate(objcg, oldpg, NR_SLAB_UNRECLAIMABLE_B,
68ac5b3c
WL
3387 stock->nr_slab_unreclaimable_b);
3388 stock->nr_slab_unreclaimable_b = 0;
3389 }
3390 stock->cached_pgdat = pgdat;
3391 }
3392
3393 bytes = (idx == NR_SLAB_RECLAIMABLE_B) ? &stock->nr_slab_reclaimable_b
3394 : &stock->nr_slab_unreclaimable_b;
3395 /*
3396 * Even for large object >= PAGE_SIZE, the vmstat data will still be
3397 * cached locally at least once before pushing it out.
3398 */
3399 if (!*bytes) {
3400 *bytes = nr;
3401 nr = 0;
3402 } else {
3403 *bytes += nr;
3404 if (abs(*bytes) > PAGE_SIZE) {
3405 nr = *bytes;
3406 *bytes = 0;
3407 } else {
3408 nr = 0;
3409 }
3410 }
3411 if (nr)
3412 mod_objcg_mlstate(objcg, pgdat, idx, nr);
3413
56751146
SAS
3414 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
3415 if (old)
3416 obj_cgroup_put(old);
68ac5b3c
WL
3417}
3418
bf4f0599
RG
3419static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes)
3420{
fead2b86 3421 struct memcg_stock_pcp *stock;
bf4f0599
RG
3422 unsigned long flags;
3423 bool ret = false;
3424
56751146 3425 local_lock_irqsave(&memcg_stock.stock_lock, flags);
fead2b86
MH
3426
3427 stock = this_cpu_ptr(&memcg_stock);
3b8abb32 3428 if (objcg == READ_ONCE(stock->cached_objcg) && stock->nr_bytes >= nr_bytes) {
bf4f0599
RG
3429 stock->nr_bytes -= nr_bytes;
3430 ret = true;
3431 }
3432
56751146 3433 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
bf4f0599
RG
3434
3435 return ret;
3436}
3437
56751146 3438static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock)
bf4f0599 3439{
3b8abb32 3440 struct obj_cgroup *old = READ_ONCE(stock->cached_objcg);
bf4f0599
RG
3441
3442 if (!old)
56751146 3443 return NULL;
bf4f0599
RG
3444
3445 if (stock->nr_bytes) {
3446 unsigned int nr_pages = stock->nr_bytes >> PAGE_SHIFT;
3447 unsigned int nr_bytes = stock->nr_bytes & (PAGE_SIZE - 1);
3448
af9a3b69
JW
3449 if (nr_pages) {
3450 struct mem_cgroup *memcg;
3451
3452 memcg = get_mem_cgroup_from_objcg(old);
3453
3454 memcg_account_kmem(memcg, -nr_pages);
3455 __refill_stock(memcg, nr_pages);
3456
3457 css_put(&memcg->css);
3458 }
bf4f0599
RG
3459
3460 /*
3461 * The leftover is flushed to the centralized per-memcg value.
3462 * On the next attempt to refill obj stock it will be moved
3463 * to a per-cpu stock (probably, on an other CPU), see
3464 * refill_obj_stock().
3465 *
3466 * How often it's flushed is a trade-off between the memory
3467 * limit enforcement accuracy and potential CPU contention,
3468 * so it might be changed in the future.
3469 */
3470 atomic_add(nr_bytes, &old->nr_charged_bytes);
3471 stock->nr_bytes = 0;
3472 }
3473
68ac5b3c
WL
3474 /*
3475 * Flush the vmstat data in current stock
3476 */
3477 if (stock->nr_slab_reclaimable_b || stock->nr_slab_unreclaimable_b) {
3478 if (stock->nr_slab_reclaimable_b) {
3479 mod_objcg_mlstate(old, stock->cached_pgdat,
3480 NR_SLAB_RECLAIMABLE_B,
3481 stock->nr_slab_reclaimable_b);
3482 stock->nr_slab_reclaimable_b = 0;
3483 }
3484 if (stock->nr_slab_unreclaimable_b) {
3485 mod_objcg_mlstate(old, stock->cached_pgdat,
3486 NR_SLAB_UNRECLAIMABLE_B,
3487 stock->nr_slab_unreclaimable_b);
3488 stock->nr_slab_unreclaimable_b = 0;
3489 }
3490 stock->cached_pgdat = NULL;
3491 }
3492
3b8abb32 3493 WRITE_ONCE(stock->cached_objcg, NULL);
56751146
SAS
3494 /*
3495 * The `old' objects needs to be released by the caller via
3496 * obj_cgroup_put() outside of memcg_stock_pcp::stock_lock.
3497 */
3498 return old;
bf4f0599
RG
3499}
3500
3501static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
3502 struct mem_cgroup *root_memcg)
3503{
3b8abb32 3504 struct obj_cgroup *objcg = READ_ONCE(stock->cached_objcg);
bf4f0599
RG
3505 struct mem_cgroup *memcg;
3506
3b8abb32
RG
3507 if (objcg) {
3508 memcg = obj_cgroup_memcg(objcg);
bf4f0599
RG
3509 if (memcg && mem_cgroup_is_descendant(memcg, root_memcg))
3510 return true;
3511 }
3512
3513 return false;
3514}
3515
5387c904
WL
3516static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes,
3517 bool allow_uncharge)
bf4f0599 3518{
fead2b86 3519 struct memcg_stock_pcp *stock;
56751146 3520 struct obj_cgroup *old = NULL;
bf4f0599 3521 unsigned long flags;
5387c904 3522 unsigned int nr_pages = 0;
bf4f0599 3523
56751146 3524 local_lock_irqsave(&memcg_stock.stock_lock, flags);
fead2b86
MH
3525
3526 stock = this_cpu_ptr(&memcg_stock);
3b8abb32 3527 if (READ_ONCE(stock->cached_objcg) != objcg) { /* reset if necessary */
56751146 3528 old = drain_obj_stock(stock);
bf4f0599 3529 obj_cgroup_get(objcg);
3b8abb32 3530 WRITE_ONCE(stock->cached_objcg, objcg);
5387c904
WL
3531 stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes)
3532 ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0;
3533 allow_uncharge = true; /* Allow uncharge when objcg changes */
bf4f0599
RG
3534 }
3535 stock->nr_bytes += nr_bytes;
3536
5387c904
WL
3537 if (allow_uncharge && (stock->nr_bytes > PAGE_SIZE)) {
3538 nr_pages = stock->nr_bytes >> PAGE_SHIFT;
3539 stock->nr_bytes &= (PAGE_SIZE - 1);
3540 }
bf4f0599 3541
56751146
SAS
3542 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
3543 if (old)
3544 obj_cgroup_put(old);
5387c904
WL
3545
3546 if (nr_pages)
3547 obj_cgroup_uncharge_pages(objcg, nr_pages);
bf4f0599
RG
3548}
3549
3550int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size)
3551{
bf4f0599
RG
3552 unsigned int nr_pages, nr_bytes;
3553 int ret;
3554
3555 if (consume_obj_stock(objcg, size))
3556 return 0;
3557
3558 /*
5387c904 3559 * In theory, objcg->nr_charged_bytes can have enough
bf4f0599 3560 * pre-charged bytes to satisfy the allocation. However,
5387c904
WL
3561 * flushing objcg->nr_charged_bytes requires two atomic
3562 * operations, and objcg->nr_charged_bytes can't be big.
3563 * The shared objcg->nr_charged_bytes can also become a
3564 * performance bottleneck if all tasks of the same memcg are
3565 * trying to update it. So it's better to ignore it and try
3566 * grab some new pages. The stock's nr_bytes will be flushed to
3567 * objcg->nr_charged_bytes later on when objcg changes.
3568 *
3569 * The stock's nr_bytes may contain enough pre-charged bytes
3570 * to allow one less page from being charged, but we can't rely
3571 * on the pre-charged bytes not being changed outside of
3572 * consume_obj_stock() or refill_obj_stock(). So ignore those
3573 * pre-charged bytes as well when charging pages. To avoid a
3574 * page uncharge right after a page charge, we set the
3575 * allow_uncharge flag to false when calling refill_obj_stock()
3576 * to temporarily allow the pre-charged bytes to exceed the page
3577 * size limit. The maximum reachable value of the pre-charged
3578 * bytes is (sizeof(object) + PAGE_SIZE - 2) if there is no data
3579 * race.
bf4f0599 3580 */
bf4f0599
RG
3581 nr_pages = size >> PAGE_SHIFT;
3582 nr_bytes = size & (PAGE_SIZE - 1);
3583
3584 if (nr_bytes)
3585 nr_pages += 1;
3586
e74d2259 3587 ret = obj_cgroup_charge_pages(objcg, gfp, nr_pages);
bf4f0599 3588 if (!ret && nr_bytes)
5387c904 3589 refill_obj_stock(objcg, PAGE_SIZE - nr_bytes, false);
bf4f0599 3590
bf4f0599
RG
3591 return ret;
3592}
3593
3594void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size)
3595{
5387c904 3596 refill_obj_stock(objcg, size, true);
bf4f0599
RG
3597}
3598
84c07d11 3599#endif /* CONFIG_MEMCG_KMEM */
7ae1e1d0 3600
ca3e0214 3601/*
be6c8982 3602 * Because page_memcg(head) is not set on tails, set it now.
ca3e0214 3603 */
be6c8982 3604void split_page_memcg(struct page *head, unsigned int nr)
ca3e0214 3605{
1b7e4464
MWO
3606 struct folio *folio = page_folio(head);
3607 struct mem_cgroup *memcg = folio_memcg(folio);
e94c8a9c 3608 int i;
ca3e0214 3609
be6c8982 3610 if (mem_cgroup_disabled() || !memcg)
3d37c4a9 3611 return;
b070e65c 3612
be6c8982 3613 for (i = 1; i < nr; i++)
1b7e4464 3614 folio_page(folio, i)->memcg_data = folio->memcg_data;
b4e0b68f 3615
1b7e4464
MWO
3616 if (folio_memcg_kmem(folio))
3617 obj_cgroup_get_many(__folio_objcg(folio), nr - 1);
b4e0b68f
MS
3618 else
3619 css_get_many(&memcg->css, nr - 1);
ca3e0214 3620}
ca3e0214 3621
e55b9f96 3622#ifdef CONFIG_SWAP
02491447
DN
3623/**
3624 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3625 * @entry: swap entry to be moved
3626 * @from: mem_cgroup which the entry is moved from
3627 * @to: mem_cgroup which the entry is moved to
3628 *
3629 * It succeeds only when the swap_cgroup's record for this entry is the same
3630 * as the mem_cgroup's id of @from.
3631 *
3632 * Returns 0 on success, -EINVAL on failure.
3633 *
3e32cb2e 3634 * The caller must have charged to @to, IOW, called page_counter_charge() about
02491447
DN
3635 * both res and memsw, and called css_get().
3636 */
3637static int mem_cgroup_move_swap_account(swp_entry_t entry,
e91cbb42 3638 struct mem_cgroup *from, struct mem_cgroup *to)
02491447
DN
3639{
3640 unsigned short old_id, new_id;
3641
34c00c31
LZ
3642 old_id = mem_cgroup_id(from);
3643 new_id = mem_cgroup_id(to);
02491447
DN
3644
3645 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
c9019e9b
JW
3646 mod_memcg_state(from, MEMCG_SWAP, -1);
3647 mod_memcg_state(to, MEMCG_SWAP, 1);
02491447
DN
3648 return 0;
3649 }
3650 return -EINVAL;
3651}
3652#else
3653static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
e91cbb42 3654 struct mem_cgroup *from, struct mem_cgroup *to)
02491447
DN
3655{
3656 return -EINVAL;
3657}
8c7c6e34 3658#endif
d13d1443 3659
bbec2e15 3660static DEFINE_MUTEX(memcg_max_mutex);
f212ad7c 3661
bbec2e15
RG
3662static int mem_cgroup_resize_max(struct mem_cgroup *memcg,
3663 unsigned long max, bool memsw)
628f4235 3664{
3e32cb2e 3665 bool enlarge = false;
bb4a7ea2 3666 bool drained = false;
3e32cb2e 3667 int ret;
c054a78c
YZ
3668 bool limits_invariant;
3669 struct page_counter *counter = memsw ? &memcg->memsw : &memcg->memory;
81d39c20 3670
3e32cb2e 3671 do {
628f4235
KH
3672 if (signal_pending(current)) {
3673 ret = -EINTR;
3674 break;
3675 }
3e32cb2e 3676
bbec2e15 3677 mutex_lock(&memcg_max_mutex);
c054a78c
YZ
3678 /*
3679 * Make sure that the new limit (memsw or memory limit) doesn't
bbec2e15 3680 * break our basic invariant rule memory.max <= memsw.max.
c054a78c 3681 */
15b42562 3682 limits_invariant = memsw ? max >= READ_ONCE(memcg->memory.max) :
bbec2e15 3683 max <= memcg->memsw.max;
c054a78c 3684 if (!limits_invariant) {
bbec2e15 3685 mutex_unlock(&memcg_max_mutex);
8c7c6e34 3686 ret = -EINVAL;
8c7c6e34
KH
3687 break;
3688 }
bbec2e15 3689 if (max > counter->max)
3e32cb2e 3690 enlarge = true;
bbec2e15
RG
3691 ret = page_counter_set_max(counter, max);
3692 mutex_unlock(&memcg_max_mutex);
8c7c6e34
KH
3693
3694 if (!ret)
3695 break;
3696
bb4a7ea2
SB
3697 if (!drained) {
3698 drain_all_stock(memcg);
3699 drained = true;
3700 continue;
3701 }
3702
73b73bac 3703 if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL,
55ab834a 3704 memsw ? 0 : MEMCG_RECLAIM_MAY_SWAP)) {
1ab5c056
AR
3705 ret = -EBUSY;
3706 break;
3707 }
3708 } while (true);
3e32cb2e 3709
3c11ecf4
KH
3710 if (!ret && enlarge)
3711 memcg_oom_recover(memcg);
3e32cb2e 3712
628f4235
KH
3713 return ret;
3714}
3715
ef8f2327 3716unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
0608f43d
AM
3717 gfp_t gfp_mask,
3718 unsigned long *total_scanned)
3719{
3720 unsigned long nr_reclaimed = 0;
ef8f2327 3721 struct mem_cgroup_per_node *mz, *next_mz = NULL;
0608f43d
AM
3722 unsigned long reclaimed;
3723 int loop = 0;
ef8f2327 3724 struct mem_cgroup_tree_per_node *mctz;
3e32cb2e 3725 unsigned long excess;
0608f43d 3726
e4dde56c
YZ
3727 if (lru_gen_enabled())
3728 return 0;
3729
0608f43d
AM
3730 if (order > 0)
3731 return 0;
3732
2ab082ba 3733 mctz = soft_limit_tree.rb_tree_per_node[pgdat->node_id];
d6507ff5
MH
3734
3735 /*
3736 * Do not even bother to check the largest node if the root
3737 * is empty. Do it lockless to prevent lock bouncing. Races
3738 * are acceptable as soft limit is best effort anyway.
3739 */
bfc7228b 3740 if (!mctz || RB_EMPTY_ROOT(&mctz->rb_root))
d6507ff5
MH
3741 return 0;
3742
0608f43d
AM
3743 /*
3744 * This loop can run a while, specially if mem_cgroup's continuously
3745 * keep exceeding their soft limit and putting the system under
3746 * pressure
3747 */
3748 do {
3749 if (next_mz)
3750 mz = next_mz;
3751 else
3752 mz = mem_cgroup_largest_soft_limit_node(mctz);
3753 if (!mz)
3754 break;
3755
ef8f2327 3756 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, pgdat,
d8f65338 3757 gfp_mask, total_scanned);
0608f43d 3758 nr_reclaimed += reclaimed;
0a31bc97 3759 spin_lock_irq(&mctz->lock);
0608f43d
AM
3760
3761 /*
3762 * If we failed to reclaim anything from this memory cgroup
3763 * it is time to move on to the next cgroup
3764 */
3765 next_mz = NULL;
bc2f2e7f
VD
3766 if (!reclaimed)
3767 next_mz = __mem_cgroup_largest_soft_limit_node(mctz);
3768
3e32cb2e 3769 excess = soft_limit_excess(mz->memcg);
0608f43d
AM
3770 /*
3771 * One school of thought says that we should not add
3772 * back the node to the tree if reclaim returns 0.
3773 * But our reclaim could return 0, simply because due
3774 * to priority we are exposing a smaller subset of
3775 * memory to reclaim from. Consider this as a longer
3776 * term TODO.
3777 */
3778 /* If excess == 0, no tree ops */
cf2c8127 3779 __mem_cgroup_insert_exceeded(mz, mctz, excess);
0a31bc97 3780 spin_unlock_irq(&mctz->lock);
0608f43d
AM
3781 css_put(&mz->memcg->css);
3782 loop++;
3783 /*
3784 * Could not reclaim anything and there are no more
3785 * mem cgroups to try or we seem to be looping without
3786 * reclaiming anything.
3787 */
3788 if (!nr_reclaimed &&
3789 (next_mz == NULL ||
3790 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3791 break;
3792 } while (!nr_reclaimed);
3793 if (next_mz)
3794 css_put(&next_mz->memcg->css);
3795 return nr_reclaimed;
3796}
3797
c26251f9 3798/*
51038171 3799 * Reclaims as many pages from the given memcg as possible.
c26251f9
MH
3800 *
3801 * Caller is responsible for holding css reference for memcg.
3802 */
3803static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
3804{
d977aa93 3805 int nr_retries = MAX_RECLAIM_RETRIES;
c26251f9 3806
c1e862c1
KH
3807 /* we call try-to-free pages for make this cgroup empty */
3808 lru_add_drain_all();
d12c60f6
JS
3809
3810 drain_all_stock(memcg);
3811
f817ed48 3812 /* try to free all pages in this cgroup */
3e32cb2e 3813 while (nr_retries && page_counter_read(&memcg->memory)) {
c26251f9
MH
3814 if (signal_pending(current))
3815 return -EINTR;
3816
73b73bac 3817 if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL,
55ab834a 3818 MEMCG_RECLAIM_MAY_SWAP))
f817ed48 3819 nr_retries--;
f817ed48 3820 }
ab5196c2
MH
3821
3822 return 0;
cc847582
KH
3823}
3824
6770c64e
TH
3825static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of,
3826 char *buf, size_t nbytes,
3827 loff_t off)
c1e862c1 3828{
6770c64e 3829 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
c26251f9 3830
d8423011
MH
3831 if (mem_cgroup_is_root(memcg))
3832 return -EINVAL;
6770c64e 3833 return mem_cgroup_force_empty(memcg) ?: nbytes;
c1e862c1
KH
3834}
3835
182446d0
TH
3836static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
3837 struct cftype *cft)
18f59ea7 3838{
bef8620c 3839 return 1;
18f59ea7
BS
3840}
3841
182446d0
TH
3842static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
3843 struct cftype *cft, u64 val)
18f59ea7 3844{
bef8620c 3845 if (val == 1)
0b8f73e1 3846 return 0;
567fb435 3847
bef8620c
RG
3848 pr_warn_once("Non-hierarchical mode is deprecated. "
3849 "Please report your usecase to linux-mm@kvack.org if you "
3850 "depend on this functionality.\n");
567fb435 3851
bef8620c 3852 return -EINVAL;
18f59ea7
BS
3853}
3854
6f646156 3855static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
ce00a967 3856{
42a30035 3857 unsigned long val;
ce00a967 3858
3e32cb2e 3859 if (mem_cgroup_is_root(memcg)) {
a2174e95 3860 /*
f82a7a86
YA
3861 * Approximate root's usage from global state. This isn't
3862 * perfect, but the root usage was always an approximation.
a2174e95 3863 */
f82a7a86
YA
3864 val = global_node_page_state(NR_FILE_PAGES) +
3865 global_node_page_state(NR_ANON_MAPPED);
42a30035 3866 if (swap)
f82a7a86 3867 val += total_swap_pages - get_nr_swap_pages();
3e32cb2e 3868 } else {
ce00a967 3869 if (!swap)
3e32cb2e 3870 val = page_counter_read(&memcg->memory);
ce00a967 3871 else
3e32cb2e 3872 val = page_counter_read(&memcg->memsw);
ce00a967 3873 }
c12176d3 3874 return val;
ce00a967
JW
3875}
3876
3e32cb2e
JW
3877enum {
3878 RES_USAGE,
3879 RES_LIMIT,
3880 RES_MAX_USAGE,
3881 RES_FAILCNT,
3882 RES_SOFT_LIMIT,
3883};
ce00a967 3884
791badbd 3885static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
05b84301 3886 struct cftype *cft)
8cdea7c0 3887{
182446d0 3888 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
3e32cb2e 3889 struct page_counter *counter;
af36f906 3890
3e32cb2e 3891 switch (MEMFILE_TYPE(cft->private)) {
8c7c6e34 3892 case _MEM:
3e32cb2e
JW
3893 counter = &memcg->memory;
3894 break;
8c7c6e34 3895 case _MEMSWAP:
3e32cb2e
JW
3896 counter = &memcg->memsw;
3897 break;
510fc4e1 3898 case _KMEM:
3e32cb2e 3899 counter = &memcg->kmem;
510fc4e1 3900 break;
d55f90bf 3901 case _TCP:
0db15298 3902 counter = &memcg->tcpmem;
d55f90bf 3903 break;
8c7c6e34
KH
3904 default:
3905 BUG();
8c7c6e34 3906 }
3e32cb2e
JW
3907
3908 switch (MEMFILE_ATTR(cft->private)) {
3909 case RES_USAGE:
3910 if (counter == &memcg->memory)
c12176d3 3911 return (u64)mem_cgroup_usage(memcg, false) * PAGE_SIZE;
3e32cb2e 3912 if (counter == &memcg->memsw)
c12176d3 3913 return (u64)mem_cgroup_usage(memcg, true) * PAGE_SIZE;
3e32cb2e
JW
3914 return (u64)page_counter_read(counter) * PAGE_SIZE;
3915 case RES_LIMIT:
bbec2e15 3916 return (u64)counter->max * PAGE_SIZE;
3e32cb2e
JW
3917 case RES_MAX_USAGE:
3918 return (u64)counter->watermark * PAGE_SIZE;
3919 case RES_FAILCNT:
3920 return counter->failcnt;
3921 case RES_SOFT_LIMIT:
2178e20c 3922 return (u64)READ_ONCE(memcg->soft_limit) * PAGE_SIZE;
3e32cb2e
JW
3923 default:
3924 BUG();
3925 }
8cdea7c0 3926}
510fc4e1 3927
6b0ba2ab
FS
3928/*
3929 * This function doesn't do anything useful. Its only job is to provide a read
3930 * handler for a file so that cgroup_file_mode() will add read permissions.
3931 */
3932static int mem_cgroup_dummy_seq_show(__always_unused struct seq_file *m,
3933 __always_unused void *v)
3934{
3935 return -EINVAL;
3936}
3937
84c07d11 3938#ifdef CONFIG_MEMCG_KMEM
567e9ab2 3939static int memcg_online_kmem(struct mem_cgroup *memcg)
d6441637 3940{
bf4f0599 3941 struct obj_cgroup *objcg;
d6441637 3942
9c94bef9 3943 if (mem_cgroup_kmem_disabled())
b313aeee
VD
3944 return 0;
3945
da0efe30
MS
3946 if (unlikely(mem_cgroup_is_root(memcg)))
3947 return 0;
d6441637 3948
bf4f0599 3949 objcg = obj_cgroup_alloc();
f9c69d63 3950 if (!objcg)
bf4f0599 3951 return -ENOMEM;
f9c69d63 3952
bf4f0599
RG
3953 objcg->memcg = memcg;
3954 rcu_assign_pointer(memcg->objcg, objcg);
675d6c9b
RG
3955 obj_cgroup_get(objcg);
3956 memcg->orig_objcg = objcg;
bf4f0599 3957
f7a449f7 3958 static_branch_enable(&memcg_kmem_online_key);
d648bcc7 3959
f9c69d63 3960 memcg->kmemcg_id = memcg->id.id;
0b8f73e1
JW
3961
3962 return 0;
d6441637
VD
3963}
3964
8e0a8912
JW
3965static void memcg_offline_kmem(struct mem_cgroup *memcg)
3966{
64268868 3967 struct mem_cgroup *parent;
8e0a8912 3968
9c94bef9 3969 if (mem_cgroup_kmem_disabled())
da0efe30
MS
3970 return;
3971
3972 if (unlikely(mem_cgroup_is_root(memcg)))
8e0a8912 3973 return;
9855609b 3974
8e0a8912
JW
3975 parent = parent_mem_cgroup(memcg);
3976 if (!parent)
3977 parent = root_mem_cgroup;
3978
bf4f0599 3979 memcg_reparent_objcgs(memcg, parent);
fb2f2b0a 3980
8e0a8912 3981 /*
64268868
MS
3982 * After we have finished memcg_reparent_objcgs(), all list_lrus
3983 * corresponding to this cgroup are guaranteed to remain empty.
3984 * The ordering is imposed by list_lru_node->lock taken by
1f391eb2 3985 * memcg_reparent_list_lrus().
8e0a8912 3986 */
1f391eb2 3987 memcg_reparent_list_lrus(memcg, parent);
8e0a8912 3988}
d6441637 3989#else
0b8f73e1 3990static int memcg_online_kmem(struct mem_cgroup *memcg)
127424c8
JW
3991{
3992 return 0;
3993}
3994static void memcg_offline_kmem(struct mem_cgroup *memcg)
3995{
3996}
84c07d11 3997#endif /* CONFIG_MEMCG_KMEM */
127424c8 3998
bbec2e15 3999static int memcg_update_tcp_max(struct mem_cgroup *memcg, unsigned long max)
d55f90bf
VD
4000{
4001 int ret;
4002
bbec2e15 4003 mutex_lock(&memcg_max_mutex);
d55f90bf 4004
bbec2e15 4005 ret = page_counter_set_max(&memcg->tcpmem, max);
d55f90bf
VD
4006 if (ret)
4007 goto out;
4008
0db15298 4009 if (!memcg->tcpmem_active) {
d55f90bf
VD
4010 /*
4011 * The active flag needs to be written after the static_key
4012 * update. This is what guarantees that the socket activation
2d758073
JW
4013 * function is the last one to run. See mem_cgroup_sk_alloc()
4014 * for details, and note that we don't mark any socket as
4015 * belonging to this memcg until that flag is up.
d55f90bf
VD
4016 *
4017 * We need to do this, because static_keys will span multiple
4018 * sites, but we can't control their order. If we mark a socket
4019 * as accounted, but the accounting functions are not patched in
4020 * yet, we'll lose accounting.
4021 *
2d758073 4022 * We never race with the readers in mem_cgroup_sk_alloc(),
d55f90bf
VD
4023 * because when this value change, the code to process it is not
4024 * patched in yet.
4025 */
4026 static_branch_inc(&memcg_sockets_enabled_key);
0db15298 4027 memcg->tcpmem_active = true;
d55f90bf
VD
4028 }
4029out:
bbec2e15 4030 mutex_unlock(&memcg_max_mutex);
d55f90bf
VD
4031 return ret;
4032}
d55f90bf 4033
628f4235
KH
4034/*
4035 * The user of this function is...
4036 * RES_LIMIT.
4037 */
451af504
TH
4038static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
4039 char *buf, size_t nbytes, loff_t off)
8cdea7c0 4040{
451af504 4041 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
3e32cb2e 4042 unsigned long nr_pages;
628f4235
KH
4043 int ret;
4044
451af504 4045 buf = strstrip(buf);
650c5e56 4046 ret = page_counter_memparse(buf, "-1", &nr_pages);
3e32cb2e
JW
4047 if (ret)
4048 return ret;
af36f906 4049
3e32cb2e 4050 switch (MEMFILE_ATTR(of_cft(of)->private)) {
628f4235 4051 case RES_LIMIT:
4b3bde4c
BS
4052 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
4053 ret = -EINVAL;
4054 break;
4055 }
3e32cb2e
JW
4056 switch (MEMFILE_TYPE(of_cft(of)->private)) {
4057 case _MEM:
bbec2e15 4058 ret = mem_cgroup_resize_max(memcg, nr_pages, false);
8c7c6e34 4059 break;
3e32cb2e 4060 case _MEMSWAP:
bbec2e15 4061 ret = mem_cgroup_resize_max(memcg, nr_pages, true);
296c81d8 4062 break;
4597648f
MH
4063 case _KMEM:
4064 pr_warn_once("kmem.limit_in_bytes is deprecated and will be removed. "
4065 "Writing any value to this file has no effect. "
4066 "Please report your usecase to linux-mm@kvack.org if you "
4067 "depend on this functionality.\n");
4068 ret = 0;
4069 break;
d55f90bf 4070 case _TCP:
bbec2e15 4071 ret = memcg_update_tcp_max(memcg, nr_pages);
d55f90bf 4072 break;
3e32cb2e 4073 }
296c81d8 4074 break;
3e32cb2e 4075 case RES_SOFT_LIMIT:
2343e88d
SAS
4076 if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
4077 ret = -EOPNOTSUPP;
4078 } else {
2178e20c 4079 WRITE_ONCE(memcg->soft_limit, nr_pages);
2343e88d
SAS
4080 ret = 0;
4081 }
628f4235
KH
4082 break;
4083 }
451af504 4084 return ret ?: nbytes;
8cdea7c0
BS
4085}
4086
6770c64e
TH
4087static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf,
4088 size_t nbytes, loff_t off)
c84872e1 4089{
6770c64e 4090 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
3e32cb2e 4091 struct page_counter *counter;
c84872e1 4092
3e32cb2e
JW
4093 switch (MEMFILE_TYPE(of_cft(of)->private)) {
4094 case _MEM:
4095 counter = &memcg->memory;
4096 break;
4097 case _MEMSWAP:
4098 counter = &memcg->memsw;
4099 break;
4100 case _KMEM:
4101 counter = &memcg->kmem;
4102 break;
d55f90bf 4103 case _TCP:
0db15298 4104 counter = &memcg->tcpmem;
d55f90bf 4105 break;
3e32cb2e
JW
4106 default:
4107 BUG();
4108 }
af36f906 4109
3e32cb2e 4110 switch (MEMFILE_ATTR(of_cft(of)->private)) {
29f2a4da 4111 case RES_MAX_USAGE:
3e32cb2e 4112 page_counter_reset_watermark(counter);
29f2a4da
PE
4113 break;
4114 case RES_FAILCNT:
3e32cb2e 4115 counter->failcnt = 0;
29f2a4da 4116 break;
3e32cb2e
JW
4117 default:
4118 BUG();
29f2a4da 4119 }
f64c3f54 4120
6770c64e 4121 return nbytes;
c84872e1
PE
4122}
4123
182446d0 4124static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
7dc74be0
DN
4125 struct cftype *cft)
4126{
182446d0 4127 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
7dc74be0
DN
4128}
4129
02491447 4130#ifdef CONFIG_MMU
182446d0 4131static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
7dc74be0
DN
4132 struct cftype *cft, u64 val)
4133{
182446d0 4134 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
7dc74be0 4135
da34a848
JW
4136 pr_warn_once("Cgroup memory moving (move_charge_at_immigrate) is deprecated. "
4137 "Please report your usecase to linux-mm@kvack.org if you "
4138 "depend on this functionality.\n");
4139
1dfab5ab 4140 if (val & ~MOVE_MASK)
7dc74be0 4141 return -EINVAL;
ee5e8472 4142
7dc74be0 4143 /*
ee5e8472
GC
4144 * No kind of locking is needed in here, because ->can_attach() will
4145 * check this value once in the beginning of the process, and then carry
4146 * on with stale data. This means that changes to this value will only
4147 * affect task migrations starting after the change.
7dc74be0 4148 */
c0ff4b85 4149 memcg->move_charge_at_immigrate = val;
7dc74be0
DN
4150 return 0;
4151}
02491447 4152#else
182446d0 4153static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
02491447
DN
4154 struct cftype *cft, u64 val)
4155{
4156 return -ENOSYS;
4157}
4158#endif
7dc74be0 4159
406eb0c9 4160#ifdef CONFIG_NUMA
113b7dfd
JW
4161
4162#define LRU_ALL_FILE (BIT(LRU_INACTIVE_FILE) | BIT(LRU_ACTIVE_FILE))
4163#define LRU_ALL_ANON (BIT(LRU_INACTIVE_ANON) | BIT(LRU_ACTIVE_ANON))
4164#define LRU_ALL ((1 << NR_LRU_LISTS) - 1)
4165
4166static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
dd8657b6 4167 int nid, unsigned int lru_mask, bool tree)
113b7dfd 4168{
867e5e1d 4169 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid));
113b7dfd
JW
4170 unsigned long nr = 0;
4171 enum lru_list lru;
4172
4173 VM_BUG_ON((unsigned)nid >= nr_node_ids);
4174
4175 for_each_lru(lru) {
4176 if (!(BIT(lru) & lru_mask))
4177 continue;
dd8657b6
SB
4178 if (tree)
4179 nr += lruvec_page_state(lruvec, NR_LRU_BASE + lru);
4180 else
4181 nr += lruvec_page_state_local(lruvec, NR_LRU_BASE + lru);
113b7dfd
JW
4182 }
4183 return nr;
4184}
4185
4186static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
dd8657b6
SB
4187 unsigned int lru_mask,
4188 bool tree)
113b7dfd
JW
4189{
4190 unsigned long nr = 0;
4191 enum lru_list lru;
4192
4193 for_each_lru(lru) {
4194 if (!(BIT(lru) & lru_mask))
4195 continue;
dd8657b6
SB
4196 if (tree)
4197 nr += memcg_page_state(memcg, NR_LRU_BASE + lru);
4198 else
4199 nr += memcg_page_state_local(memcg, NR_LRU_BASE + lru);
113b7dfd
JW
4200 }
4201 return nr;
4202}
4203
2da8ca82 4204static int memcg_numa_stat_show(struct seq_file *m, void *v)
406eb0c9 4205{
25485de6
GT
4206 struct numa_stat {
4207 const char *name;
4208 unsigned int lru_mask;
4209 };
4210
4211 static const struct numa_stat stats[] = {
4212 { "total", LRU_ALL },
4213 { "file", LRU_ALL_FILE },
4214 { "anon", LRU_ALL_ANON },
4215 { "unevictable", BIT(LRU_UNEVICTABLE) },
4216 };
4217 const struct numa_stat *stat;
406eb0c9 4218 int nid;
aa9694bb 4219 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
406eb0c9 4220
7d7ef0a4 4221 mem_cgroup_flush_stats(memcg);
2d146aa3 4222
25485de6 4223 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
dd8657b6
SB
4224 seq_printf(m, "%s=%lu", stat->name,
4225 mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
4226 false));
4227 for_each_node_state(nid, N_MEMORY)
4228 seq_printf(m, " N%d=%lu", nid,
4229 mem_cgroup_node_nr_lru_pages(memcg, nid,
4230 stat->lru_mask, false));
25485de6 4231 seq_putc(m, '\n');
406eb0c9 4232 }
406eb0c9 4233
071aee13 4234 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
dd8657b6
SB
4235
4236 seq_printf(m, "hierarchical_%s=%lu", stat->name,
4237 mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
4238 true));
4239 for_each_node_state(nid, N_MEMORY)
4240 seq_printf(m, " N%d=%lu", nid,
4241 mem_cgroup_node_nr_lru_pages(memcg, nid,
4242 stat->lru_mask, true));
071aee13 4243 seq_putc(m, '\n');
406eb0c9 4244 }
406eb0c9 4245
406eb0c9
YH
4246 return 0;
4247}
4248#endif /* CONFIG_NUMA */
4249
c8713d0b 4250static const unsigned int memcg1_stats[] = {
0d1c2072 4251 NR_FILE_PAGES,
be5d0a74 4252 NR_ANON_MAPPED,
468c3982
JW
4253#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4254 NR_ANON_THPS,
4255#endif
c8713d0b
JW
4256 NR_SHMEM,
4257 NR_FILE_MAPPED,
4258 NR_FILE_DIRTY,
4259 NR_WRITEBACK,
e09b0b61
YS
4260 WORKINGSET_REFAULT_ANON,
4261 WORKINGSET_REFAULT_FILE,
72a14e82 4262#ifdef CONFIG_SWAP
c8713d0b 4263 MEMCG_SWAP,
72a14e82
LS
4264 NR_SWAPCACHE,
4265#endif
c8713d0b
JW
4266};
4267
4268static const char *const memcg1_stat_names[] = {
4269 "cache",
4270 "rss",
468c3982 4271#ifdef CONFIG_TRANSPARENT_HUGEPAGE
c8713d0b 4272 "rss_huge",
468c3982 4273#endif
c8713d0b
JW
4274 "shmem",
4275 "mapped_file",
4276 "dirty",
4277 "writeback",
e09b0b61
YS
4278 "workingset_refault_anon",
4279 "workingset_refault_file",
72a14e82 4280#ifdef CONFIG_SWAP
c8713d0b 4281 "swap",
72a14e82
LS
4282 "swapcached",
4283#endif
c8713d0b
JW
4284};
4285
df0e53d0 4286/* Universal VM events cgroup1 shows, original sort order */
8dd53fd3 4287static const unsigned int memcg1_events[] = {
df0e53d0
JW
4288 PGPGIN,
4289 PGPGOUT,
4290 PGFAULT,
4291 PGMAJFAULT,
4292};
4293
dddb44ff 4294static void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s)
d2ceb9b7 4295{
3e32cb2e 4296 unsigned long memory, memsw;
af7c4b0e
JW
4297 struct mem_cgroup *mi;
4298 unsigned int i;
406eb0c9 4299
71cd3113 4300 BUILD_BUG_ON(ARRAY_SIZE(memcg1_stat_names) != ARRAY_SIZE(memcg1_stats));
70bc068c 4301
7d7ef0a4 4302 mem_cgroup_flush_stats(memcg);
2d146aa3 4303
71cd3113 4304 for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
468c3982
JW
4305 unsigned long nr;
4306
ff841a06
YA
4307 nr = memcg_page_state_local_output(memcg, memcg1_stats[i]);
4308 seq_buf_printf(s, "%s %lu\n", memcg1_stat_names[i], nr);
1dd3a273 4309 }
7b854121 4310
df0e53d0 4311 for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
dddb44ff
YA
4312 seq_buf_printf(s, "%s %lu\n", vm_event_name(memcg1_events[i]),
4313 memcg_events_local(memcg, memcg1_events[i]));
af7c4b0e
JW
4314
4315 for (i = 0; i < NR_LRU_LISTS; i++)
dddb44ff
YA
4316 seq_buf_printf(s, "%s %lu\n", lru_list_name(i),
4317 memcg_page_state_local(memcg, NR_LRU_BASE + i) *
4318 PAGE_SIZE);
af7c4b0e 4319
14067bb3 4320 /* Hierarchical information */
3e32cb2e
JW
4321 memory = memsw = PAGE_COUNTER_MAX;
4322 for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) {
15b42562
CD
4323 memory = min(memory, READ_ONCE(mi->memory.max));
4324 memsw = min(memsw, READ_ONCE(mi->memsw.max));
fee7b548 4325 }
dddb44ff
YA
4326 seq_buf_printf(s, "hierarchical_memory_limit %llu\n",
4327 (u64)memory * PAGE_SIZE);
840ea53a
LS
4328 seq_buf_printf(s, "hierarchical_memsw_limit %llu\n",
4329 (u64)memsw * PAGE_SIZE);
7f016ee8 4330
8de7ecc6 4331 for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
7de2e9f1 4332 unsigned long nr;
4333
ff841a06 4334 nr = memcg_page_state_output(memcg, memcg1_stats[i]);
dddb44ff 4335 seq_buf_printf(s, "total_%s %llu\n", memcg1_stat_names[i],
ff841a06 4336 (u64)nr);
af7c4b0e
JW
4337 }
4338
8de7ecc6 4339 for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
dddb44ff
YA
4340 seq_buf_printf(s, "total_%s %llu\n",
4341 vm_event_name(memcg1_events[i]),
4342 (u64)memcg_events(memcg, memcg1_events[i]));
af7c4b0e 4343
8de7ecc6 4344 for (i = 0; i < NR_LRU_LISTS; i++)
dddb44ff
YA
4345 seq_buf_printf(s, "total_%s %llu\n", lru_list_name(i),
4346 (u64)memcg_page_state(memcg, NR_LRU_BASE + i) *
4347 PAGE_SIZE);
14067bb3 4348
7f016ee8 4349#ifdef CONFIG_DEBUG_VM
7f016ee8 4350 {
ef8f2327
MG
4351 pg_data_t *pgdat;
4352 struct mem_cgroup_per_node *mz;
1431d4d1
JW
4353 unsigned long anon_cost = 0;
4354 unsigned long file_cost = 0;
7f016ee8 4355
ef8f2327 4356 for_each_online_pgdat(pgdat) {
a3747b53 4357 mz = memcg->nodeinfo[pgdat->node_id];
7f016ee8 4358
1431d4d1
JW
4359 anon_cost += mz->lruvec.anon_cost;
4360 file_cost += mz->lruvec.file_cost;
ef8f2327 4361 }
dddb44ff
YA
4362 seq_buf_printf(s, "anon_cost %lu\n", anon_cost);
4363 seq_buf_printf(s, "file_cost %lu\n", file_cost);
7f016ee8
KM
4364 }
4365#endif
d2ceb9b7
KH
4366}
4367
182446d0
TH
4368static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
4369 struct cftype *cft)
a7885eb8 4370{
182446d0 4371 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
a7885eb8 4372
1f4c025b 4373 return mem_cgroup_swappiness(memcg);
a7885eb8
KM
4374}
4375
182446d0
TH
4376static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
4377 struct cftype *cft, u64 val)
a7885eb8 4378{
182446d0 4379 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
a7885eb8 4380
37bc3cb9 4381 if (val > 200)
a7885eb8
KM
4382 return -EINVAL;
4383
a4792030 4384 if (!mem_cgroup_is_root(memcg))
82b3aa26 4385 WRITE_ONCE(memcg->swappiness, val);
3dae7fec 4386 else
82b3aa26 4387 WRITE_ONCE(vm_swappiness, val);
068b38c1 4388
a7885eb8
KM
4389 return 0;
4390}
4391
2e72b634
KS
4392static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4393{
4394 struct mem_cgroup_threshold_ary *t;
3e32cb2e 4395 unsigned long usage;
2e72b634
KS
4396 int i;
4397
4398 rcu_read_lock();
4399 if (!swap)
2c488db2 4400 t = rcu_dereference(memcg->thresholds.primary);
2e72b634 4401 else
2c488db2 4402 t = rcu_dereference(memcg->memsw_thresholds.primary);
2e72b634
KS
4403
4404 if (!t)
4405 goto unlock;
4406
ce00a967 4407 usage = mem_cgroup_usage(memcg, swap);
2e72b634
KS
4408
4409 /*
748dad36 4410 * current_threshold points to threshold just below or equal to usage.
2e72b634
KS
4411 * If it's not true, a threshold was crossed after last
4412 * call of __mem_cgroup_threshold().
4413 */
5407a562 4414 i = t->current_threshold;
2e72b634
KS
4415
4416 /*
4417 * Iterate backward over array of thresholds starting from
4418 * current_threshold and check if a threshold is crossed.
4419 * If none of thresholds below usage is crossed, we read
4420 * only one element of the array here.
4421 */
4422 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
3652117f 4423 eventfd_signal(t->entries[i].eventfd);
2e72b634
KS
4424
4425 /* i = current_threshold + 1 */
4426 i++;
4427
4428 /*
4429 * Iterate forward over array of thresholds starting from
4430 * current_threshold+1 and check if a threshold is crossed.
4431 * If none of thresholds above usage is crossed, we read
4432 * only one element of the array here.
4433 */
4434 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
3652117f 4435 eventfd_signal(t->entries[i].eventfd);
2e72b634
KS
4436
4437 /* Update current_threshold */
5407a562 4438 t->current_threshold = i - 1;
2e72b634
KS
4439unlock:
4440 rcu_read_unlock();
4441}
4442
4443static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4444{
ad4ca5f4
KS
4445 while (memcg) {
4446 __mem_cgroup_threshold(memcg, false);
7941d214 4447 if (do_memsw_account())
ad4ca5f4
KS
4448 __mem_cgroup_threshold(memcg, true);
4449
4450 memcg = parent_mem_cgroup(memcg);
4451 }
2e72b634
KS
4452}
4453
4454static int compare_thresholds(const void *a, const void *b)
4455{
4456 const struct mem_cgroup_threshold *_a = a;
4457 const struct mem_cgroup_threshold *_b = b;
4458
2bff24a3
GT
4459 if (_a->threshold > _b->threshold)
4460 return 1;
4461
4462 if (_a->threshold < _b->threshold)
4463 return -1;
4464
4465 return 0;
2e72b634
KS
4466}
4467
c0ff4b85 4468static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
9490ff27
KH
4469{
4470 struct mem_cgroup_eventfd_list *ev;
4471
2bcf2e92
MH
4472 spin_lock(&memcg_oom_lock);
4473
c0ff4b85 4474 list_for_each_entry(ev, &memcg->oom_notify, list)
3652117f 4475 eventfd_signal(ev->eventfd);
2bcf2e92
MH
4476
4477 spin_unlock(&memcg_oom_lock);
9490ff27
KH
4478 return 0;
4479}
4480
c0ff4b85 4481static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
9490ff27 4482{
7d74b06f
KH
4483 struct mem_cgroup *iter;
4484
c0ff4b85 4485 for_each_mem_cgroup_tree(iter, memcg)
7d74b06f 4486 mem_cgroup_oom_notify_cb(iter);
9490ff27
KH
4487}
4488
59b6f873 4489static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
347c4a87 4490 struct eventfd_ctx *eventfd, const char *args, enum res_type type)
2e72b634 4491{
2c488db2
KS
4492 struct mem_cgroup_thresholds *thresholds;
4493 struct mem_cgroup_threshold_ary *new;
3e32cb2e
JW
4494 unsigned long threshold;
4495 unsigned long usage;
2c488db2 4496 int i, size, ret;
2e72b634 4497
650c5e56 4498 ret = page_counter_memparse(args, "-1", &threshold);
2e72b634
KS
4499 if (ret)
4500 return ret;
4501
4502 mutex_lock(&memcg->thresholds_lock);
2c488db2 4503
05b84301 4504 if (type == _MEM) {
2c488db2 4505 thresholds = &memcg->thresholds;
ce00a967 4506 usage = mem_cgroup_usage(memcg, false);
05b84301 4507 } else if (type == _MEMSWAP) {
2c488db2 4508 thresholds = &memcg->memsw_thresholds;
ce00a967 4509 usage = mem_cgroup_usage(memcg, true);
05b84301 4510 } else
2e72b634
KS
4511 BUG();
4512
2e72b634 4513 /* Check if a threshold crossed before adding a new one */
2c488db2 4514 if (thresholds->primary)
2e72b634
KS
4515 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4516
2c488db2 4517 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
2e72b634
KS
4518
4519 /* Allocate memory for new array of thresholds */
67b8046f 4520 new = kmalloc(struct_size(new, entries, size), GFP_KERNEL);
2c488db2 4521 if (!new) {
2e72b634
KS
4522 ret = -ENOMEM;
4523 goto unlock;
4524 }
2c488db2 4525 new->size = size;
2e72b634
KS
4526
4527 /* Copy thresholds (if any) to new array */
e90342e6
GS
4528 if (thresholds->primary)
4529 memcpy(new->entries, thresholds->primary->entries,
4530 flex_array_size(new, entries, size - 1));
2c488db2 4531
2e72b634 4532 /* Add new threshold */
2c488db2
KS
4533 new->entries[size - 1].eventfd = eventfd;
4534 new->entries[size - 1].threshold = threshold;
2e72b634
KS
4535
4536 /* Sort thresholds. Registering of new threshold isn't time-critical */
61e604e6 4537 sort(new->entries, size, sizeof(*new->entries),
2e72b634
KS
4538 compare_thresholds, NULL);
4539
4540 /* Find current threshold */
2c488db2 4541 new->current_threshold = -1;
2e72b634 4542 for (i = 0; i < size; i++) {
748dad36 4543 if (new->entries[i].threshold <= usage) {
2e72b634 4544 /*
2c488db2
KS
4545 * new->current_threshold will not be used until
4546 * rcu_assign_pointer(), so it's safe to increment
2e72b634
KS
4547 * it here.
4548 */
2c488db2 4549 ++new->current_threshold;
748dad36
SZ
4550 } else
4551 break;
2e72b634
KS
4552 }
4553
2c488db2
KS
4554 /* Free old spare buffer and save old primary buffer as spare */
4555 kfree(thresholds->spare);
4556 thresholds->spare = thresholds->primary;
4557
4558 rcu_assign_pointer(thresholds->primary, new);
2e72b634 4559
907860ed 4560 /* To be sure that nobody uses thresholds */
2e72b634
KS
4561 synchronize_rcu();
4562
2e72b634
KS
4563unlock:
4564 mutex_unlock(&memcg->thresholds_lock);
4565
4566 return ret;
4567}
4568
59b6f873 4569static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
347c4a87
TH
4570 struct eventfd_ctx *eventfd, const char *args)
4571{
59b6f873 4572 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM);
347c4a87
TH
4573}
4574
59b6f873 4575static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg,
347c4a87
TH
4576 struct eventfd_ctx *eventfd, const char *args)
4577{
59b6f873 4578 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP);
347c4a87
TH
4579}
4580
59b6f873 4581static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
347c4a87 4582 struct eventfd_ctx *eventfd, enum res_type type)
2e72b634 4583{
2c488db2
KS
4584 struct mem_cgroup_thresholds *thresholds;
4585 struct mem_cgroup_threshold_ary *new;
3e32cb2e 4586 unsigned long usage;
7d36665a 4587 int i, j, size, entries;
2e72b634
KS
4588
4589 mutex_lock(&memcg->thresholds_lock);
05b84301
JW
4590
4591 if (type == _MEM) {
2c488db2 4592 thresholds = &memcg->thresholds;
ce00a967 4593 usage = mem_cgroup_usage(memcg, false);
05b84301 4594 } else if (type == _MEMSWAP) {
2c488db2 4595 thresholds = &memcg->memsw_thresholds;
ce00a967 4596 usage = mem_cgroup_usage(memcg, true);
05b84301 4597 } else
2e72b634
KS
4598 BUG();
4599
371528ca
AV
4600 if (!thresholds->primary)
4601 goto unlock;
4602
2e72b634
KS
4603 /* Check if a threshold crossed before removing */
4604 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4605
4606 /* Calculate new number of threshold */
7d36665a 4607 size = entries = 0;
2c488db2
KS
4608 for (i = 0; i < thresholds->primary->size; i++) {
4609 if (thresholds->primary->entries[i].eventfd != eventfd)
2e72b634 4610 size++;
7d36665a
CX
4611 else
4612 entries++;
2e72b634
KS
4613 }
4614
2c488db2 4615 new = thresholds->spare;
907860ed 4616
7d36665a
CX
4617 /* If no items related to eventfd have been cleared, nothing to do */
4618 if (!entries)
4619 goto unlock;
4620
2e72b634
KS
4621 /* Set thresholds array to NULL if we don't have thresholds */
4622 if (!size) {
2c488db2
KS
4623 kfree(new);
4624 new = NULL;
907860ed 4625 goto swap_buffers;
2e72b634
KS
4626 }
4627
2c488db2 4628 new->size = size;
2e72b634
KS
4629
4630 /* Copy thresholds and find current threshold */
2c488db2
KS
4631 new->current_threshold = -1;
4632 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4633 if (thresholds->primary->entries[i].eventfd == eventfd)
2e72b634
KS
4634 continue;
4635
2c488db2 4636 new->entries[j] = thresholds->primary->entries[i];
748dad36 4637 if (new->entries[j].threshold <= usage) {
2e72b634 4638 /*
2c488db2 4639 * new->current_threshold will not be used
2e72b634
KS
4640 * until rcu_assign_pointer(), so it's safe to increment
4641 * it here.
4642 */
2c488db2 4643 ++new->current_threshold;
2e72b634
KS
4644 }
4645 j++;
4646 }
4647
907860ed 4648swap_buffers:
2c488db2
KS
4649 /* Swap primary and spare array */
4650 thresholds->spare = thresholds->primary;
8c757763 4651
2c488db2 4652 rcu_assign_pointer(thresholds->primary, new);
2e72b634 4653
907860ed 4654 /* To be sure that nobody uses thresholds */
2e72b634 4655 synchronize_rcu();
6611d8d7
MC
4656
4657 /* If all events are unregistered, free the spare array */
4658 if (!new) {
4659 kfree(thresholds->spare);
4660 thresholds->spare = NULL;
4661 }
371528ca 4662unlock:
2e72b634 4663 mutex_unlock(&memcg->thresholds_lock);
2e72b634 4664}
c1e862c1 4665
59b6f873 4666static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
347c4a87
TH
4667 struct eventfd_ctx *eventfd)
4668{
59b6f873 4669 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM);
347c4a87
TH
4670}
4671
59b6f873 4672static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
347c4a87
TH
4673 struct eventfd_ctx *eventfd)
4674{
59b6f873 4675 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP);
347c4a87
TH
4676}
4677
59b6f873 4678static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg,
347c4a87 4679 struct eventfd_ctx *eventfd, const char *args)
9490ff27 4680{
9490ff27 4681 struct mem_cgroup_eventfd_list *event;
9490ff27 4682
9490ff27
KH
4683 event = kmalloc(sizeof(*event), GFP_KERNEL);
4684 if (!event)
4685 return -ENOMEM;
4686
1af8efe9 4687 spin_lock(&memcg_oom_lock);
9490ff27
KH
4688
4689 event->eventfd = eventfd;
4690 list_add(&event->list, &memcg->oom_notify);
4691
4692 /* already in OOM ? */
c2b42d3c 4693 if (memcg->under_oom)
3652117f 4694 eventfd_signal(eventfd);
1af8efe9 4695 spin_unlock(&memcg_oom_lock);
9490ff27
KH
4696
4697 return 0;
4698}
4699
59b6f873 4700static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg,
347c4a87 4701 struct eventfd_ctx *eventfd)
9490ff27 4702{
9490ff27 4703 struct mem_cgroup_eventfd_list *ev, *tmp;
9490ff27 4704
1af8efe9 4705 spin_lock(&memcg_oom_lock);
9490ff27 4706
c0ff4b85 4707 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
9490ff27
KH
4708 if (ev->eventfd == eventfd) {
4709 list_del(&ev->list);
4710 kfree(ev);
4711 }
4712 }
4713
1af8efe9 4714 spin_unlock(&memcg_oom_lock);
9490ff27
KH
4715}
4716
2da8ca82 4717static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
3c11ecf4 4718{
aa9694bb 4719 struct mem_cgroup *memcg = mem_cgroup_from_seq(sf);
3c11ecf4 4720
17c56de6 4721 seq_printf(sf, "oom_kill_disable %d\n", READ_ONCE(memcg->oom_kill_disable));
c2b42d3c 4722 seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom);
fe6bdfc8
RG
4723 seq_printf(sf, "oom_kill %lu\n",
4724 atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL]));
3c11ecf4
KH
4725 return 0;
4726}
4727
182446d0 4728static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
3c11ecf4
KH
4729 struct cftype *cft, u64 val)
4730{
182446d0 4731 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
3c11ecf4
KH
4732
4733 /* cannot set to root cgroup and only 0 and 1 are allowed */
a4792030 4734 if (mem_cgroup_is_root(memcg) || !((val == 0) || (val == 1)))
3c11ecf4
KH
4735 return -EINVAL;
4736
17c56de6 4737 WRITE_ONCE(memcg->oom_kill_disable, val);
4d845ebf 4738 if (!val)
c0ff4b85 4739 memcg_oom_recover(memcg);
3dae7fec 4740
3c11ecf4
KH
4741 return 0;
4742}
4743
52ebea74
TH
4744#ifdef CONFIG_CGROUP_WRITEBACK
4745
3a8e9ac8
TH
4746#include <trace/events/writeback.h>
4747
841710aa
TH
4748static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
4749{
4750 return wb_domain_init(&memcg->cgwb_domain, gfp);
4751}
4752
4753static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
4754{
4755 wb_domain_exit(&memcg->cgwb_domain);
4756}
4757
2529bb3a
TH
4758static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
4759{
4760 wb_domain_size_changed(&memcg->cgwb_domain);
4761}
4762
841710aa
TH
4763struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
4764{
4765 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4766
4767 if (!memcg->css.parent)
4768 return NULL;
4769
4770 return &memcg->cgwb_domain;
4771}
4772
c2aa723a
TH
4773/**
4774 * mem_cgroup_wb_stats - retrieve writeback related stats from its memcg
4775 * @wb: bdi_writeback in question
c5edf9cd
TH
4776 * @pfilepages: out parameter for number of file pages
4777 * @pheadroom: out parameter for number of allocatable pages according to memcg
c2aa723a
TH
4778 * @pdirty: out parameter for number of dirty pages
4779 * @pwriteback: out parameter for number of pages under writeback
4780 *
c5edf9cd
TH
4781 * Determine the numbers of file, headroom, dirty, and writeback pages in
4782 * @wb's memcg. File, dirty and writeback are self-explanatory. Headroom
4783 * is a bit more involved.
c2aa723a 4784 *
c5edf9cd
TH
4785 * A memcg's headroom is "min(max, high) - used". In the hierarchy, the
4786 * headroom is calculated as the lowest headroom of itself and the
4787 * ancestors. Note that this doesn't consider the actual amount of
4788 * available memory in the system. The caller should further cap
4789 * *@pheadroom accordingly.
c2aa723a 4790 */
c5edf9cd
TH
4791void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
4792 unsigned long *pheadroom, unsigned long *pdirty,
4793 unsigned long *pwriteback)
c2aa723a
TH
4794{
4795 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4796 struct mem_cgroup *parent;
c2aa723a 4797
7d7ef0a4 4798 mem_cgroup_flush_stats(memcg);
c2aa723a 4799
2d146aa3
JW
4800 *pdirty = memcg_page_state(memcg, NR_FILE_DIRTY);
4801 *pwriteback = memcg_page_state(memcg, NR_WRITEBACK);
4802 *pfilepages = memcg_page_state(memcg, NR_INACTIVE_FILE) +
4803 memcg_page_state(memcg, NR_ACTIVE_FILE);
c2aa723a 4804
2d146aa3 4805 *pheadroom = PAGE_COUNTER_MAX;
c2aa723a 4806 while ((parent = parent_mem_cgroup(memcg))) {
15b42562 4807 unsigned long ceiling = min(READ_ONCE(memcg->memory.max),
d1663a90 4808 READ_ONCE(memcg->memory.high));
c2aa723a
TH
4809 unsigned long used = page_counter_read(&memcg->memory);
4810
c5edf9cd 4811 *pheadroom = min(*pheadroom, ceiling - min(ceiling, used));
c2aa723a
TH
4812 memcg = parent;
4813 }
c2aa723a
TH
4814}
4815
97b27821
TH
4816/*
4817 * Foreign dirty flushing
4818 *
4819 * There's an inherent mismatch between memcg and writeback. The former
f0953a1b 4820 * tracks ownership per-page while the latter per-inode. This was a
97b27821
TH
4821 * deliberate design decision because honoring per-page ownership in the
4822 * writeback path is complicated, may lead to higher CPU and IO overheads
4823 * and deemed unnecessary given that write-sharing an inode across
4824 * different cgroups isn't a common use-case.
4825 *
4826 * Combined with inode majority-writer ownership switching, this works well
4827 * enough in most cases but there are some pathological cases. For
4828 * example, let's say there are two cgroups A and B which keep writing to
4829 * different but confined parts of the same inode. B owns the inode and
4830 * A's memory is limited far below B's. A's dirty ratio can rise enough to
4831 * trigger balance_dirty_pages() sleeps but B's can be low enough to avoid
4832 * triggering background writeback. A will be slowed down without a way to
4833 * make writeback of the dirty pages happen.
4834 *
f0953a1b 4835 * Conditions like the above can lead to a cgroup getting repeatedly and
97b27821 4836 * severely throttled after making some progress after each
f0953a1b 4837 * dirty_expire_interval while the underlying IO device is almost
97b27821
TH
4838 * completely idle.
4839 *
4840 * Solving this problem completely requires matching the ownership tracking
4841 * granularities between memcg and writeback in either direction. However,
4842 * the more egregious behaviors can be avoided by simply remembering the
4843 * most recent foreign dirtying events and initiating remote flushes on
4844 * them when local writeback isn't enough to keep the memory clean enough.
4845 *
4846 * The following two functions implement such mechanism. When a foreign
4847 * page - a page whose memcg and writeback ownerships don't match - is
4848 * dirtied, mem_cgroup_track_foreign_dirty() records the inode owning
4849 * bdi_writeback on the page owning memcg. When balance_dirty_pages()
4850 * decides that the memcg needs to sleep due to high dirty ratio, it calls
4851 * mem_cgroup_flush_foreign() which queues writeback on the recorded
4852 * foreign bdi_writebacks which haven't expired. Both the numbers of
4853 * recorded bdi_writebacks and concurrent in-flight foreign writebacks are
4854 * limited to MEMCG_CGWB_FRN_CNT.
4855 *
4856 * The mechanism only remembers IDs and doesn't hold any object references.
4857 * As being wrong occasionally doesn't matter, updates and accesses to the
4858 * records are lockless and racy.
4859 */
9d8053fc 4860void mem_cgroup_track_foreign_dirty_slowpath(struct folio *folio,
97b27821
TH
4861 struct bdi_writeback *wb)
4862{
9d8053fc 4863 struct mem_cgroup *memcg = folio_memcg(folio);
97b27821
TH
4864 struct memcg_cgwb_frn *frn;
4865 u64 now = get_jiffies_64();
4866 u64 oldest_at = now;
4867 int oldest = -1;
4868 int i;
4869
9d8053fc 4870 trace_track_foreign_dirty(folio, wb);
3a8e9ac8 4871
97b27821
TH
4872 /*
4873 * Pick the slot to use. If there is already a slot for @wb, keep
4874 * using it. If not replace the oldest one which isn't being
4875 * written out.
4876 */
4877 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) {
4878 frn = &memcg->cgwb_frn[i];
4879 if (frn->bdi_id == wb->bdi->id &&
4880 frn->memcg_id == wb->memcg_css->id)
4881 break;
4882 if (time_before64(frn->at, oldest_at) &&
4883 atomic_read(&frn->done.cnt) == 1) {
4884 oldest = i;
4885 oldest_at = frn->at;
4886 }
4887 }
4888
4889 if (i < MEMCG_CGWB_FRN_CNT) {
4890 /*
4891 * Re-using an existing one. Update timestamp lazily to
4892 * avoid making the cacheline hot. We want them to be
4893 * reasonably up-to-date and significantly shorter than
4894 * dirty_expire_interval as that's what expires the record.
4895 * Use the shorter of 1s and dirty_expire_interval / 8.
4896 */
4897 unsigned long update_intv =
4898 min_t(unsigned long, HZ,
4899 msecs_to_jiffies(dirty_expire_interval * 10) / 8);
4900
4901 if (time_before64(frn->at, now - update_intv))
4902 frn->at = now;
4903 } else if (oldest >= 0) {
4904 /* replace the oldest free one */
4905 frn = &memcg->cgwb_frn[oldest];
4906 frn->bdi_id = wb->bdi->id;
4907 frn->memcg_id = wb->memcg_css->id;
4908 frn->at = now;
4909 }
4910}
4911
4912/* issue foreign writeback flushes for recorded foreign dirtying events */
4913void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
4914{
4915 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4916 unsigned long intv = msecs_to_jiffies(dirty_expire_interval * 10);
4917 u64 now = jiffies_64;
4918 int i;
4919
4920 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) {
4921 struct memcg_cgwb_frn *frn = &memcg->cgwb_frn[i];
4922
4923 /*
4924 * If the record is older than dirty_expire_interval,
4925 * writeback on it has already started. No need to kick it
4926 * off again. Also, don't start a new one if there's
4927 * already one in flight.
4928 */
4929 if (time_after64(frn->at, now - intv) &&
4930 atomic_read(&frn->done.cnt) == 1) {
4931 frn->at = 0;
3a8e9ac8 4932 trace_flush_foreign(wb, frn->bdi_id, frn->memcg_id);
7490a2d2 4933 cgroup_writeback_by_id(frn->bdi_id, frn->memcg_id,
97b27821
TH
4934 WB_REASON_FOREIGN_FLUSH,
4935 &frn->done);
4936 }
4937 }
4938}
4939
841710aa
TH
4940#else /* CONFIG_CGROUP_WRITEBACK */
4941
4942static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
4943{
4944 return 0;
4945}
4946
4947static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
4948{
4949}
4950
2529bb3a
TH
4951static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
4952{
4953}
4954
52ebea74
TH
4955#endif /* CONFIG_CGROUP_WRITEBACK */
4956
3bc942f3
TH
4957/*
4958 * DO NOT USE IN NEW FILES.
4959 *
4960 * "cgroup.event_control" implementation.
4961 *
4962 * This is way over-engineered. It tries to support fully configurable
4963 * events for each user. Such level of flexibility is completely
4964 * unnecessary especially in the light of the planned unified hierarchy.
4965 *
4966 * Please deprecate this and replace with something simpler if at all
4967 * possible.
4968 */
4969
79bd9814
TH
4970/*
4971 * Unregister event and free resources.
4972 *
4973 * Gets called from workqueue.
4974 */
3bc942f3 4975static void memcg_event_remove(struct work_struct *work)
79bd9814 4976{
3bc942f3
TH
4977 struct mem_cgroup_event *event =
4978 container_of(work, struct mem_cgroup_event, remove);
59b6f873 4979 struct mem_cgroup *memcg = event->memcg;
79bd9814
TH
4980
4981 remove_wait_queue(event->wqh, &event->wait);
4982
59b6f873 4983 event->unregister_event(memcg, event->eventfd);
79bd9814
TH
4984
4985 /* Notify userspace the event is going away. */
3652117f 4986 eventfd_signal(event->eventfd);
79bd9814
TH
4987
4988 eventfd_ctx_put(event->eventfd);
4989 kfree(event);
59b6f873 4990 css_put(&memcg->css);
79bd9814
TH
4991}
4992
4993/*
a9a08845 4994 * Gets called on EPOLLHUP on eventfd when user closes it.
79bd9814
TH
4995 *
4996 * Called with wqh->lock held and interrupts disabled.
4997 */
ac6424b9 4998static int memcg_event_wake(wait_queue_entry_t *wait, unsigned mode,
3bc942f3 4999 int sync, void *key)
79bd9814 5000{
3bc942f3
TH
5001 struct mem_cgroup_event *event =
5002 container_of(wait, struct mem_cgroup_event, wait);
59b6f873 5003 struct mem_cgroup *memcg = event->memcg;
3ad6f93e 5004 __poll_t flags = key_to_poll(key);
79bd9814 5005
a9a08845 5006 if (flags & EPOLLHUP) {
79bd9814
TH
5007 /*
5008 * If the event has been detached at cgroup removal, we
5009 * can simply return knowing the other side will cleanup
5010 * for us.
5011 *
5012 * We can't race against event freeing since the other
5013 * side will require wqh->lock via remove_wait_queue(),
5014 * which we hold.
5015 */
fba94807 5016 spin_lock(&memcg->event_list_lock);
79bd9814
TH
5017 if (!list_empty(&event->list)) {
5018 list_del_init(&event->list);
5019 /*
5020 * We are in atomic context, but cgroup_event_remove()
5021 * may sleep, so we have to call it in workqueue.
5022 */
5023 schedule_work(&event->remove);
5024 }
fba94807 5025 spin_unlock(&memcg->event_list_lock);
79bd9814
TH
5026 }
5027
5028 return 0;
5029}
5030
3bc942f3 5031static void memcg_event_ptable_queue_proc(struct file *file,
79bd9814
TH
5032 wait_queue_head_t *wqh, poll_table *pt)
5033{
3bc942f3
TH
5034 struct mem_cgroup_event *event =
5035 container_of(pt, struct mem_cgroup_event, pt);
79bd9814
TH
5036
5037 event->wqh = wqh;
5038 add_wait_queue(wqh, &event->wait);
5039}
5040
5041/*
3bc942f3
TH
5042 * DO NOT USE IN NEW FILES.
5043 *
79bd9814
TH
5044 * Parse input and register new cgroup event handler.
5045 *
5046 * Input must be in format '<event_fd> <control_fd> <args>'.
5047 * Interpretation of args is defined by control file implementation.
5048 */
451af504
TH
5049static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
5050 char *buf, size_t nbytes, loff_t off)
79bd9814 5051{
451af504 5052 struct cgroup_subsys_state *css = of_css(of);
fba94807 5053 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
3bc942f3 5054 struct mem_cgroup_event *event;
79bd9814
TH
5055 struct cgroup_subsys_state *cfile_css;
5056 unsigned int efd, cfd;
5057 struct fd efile;
5058 struct fd cfile;
4a7ba45b 5059 struct dentry *cdentry;
fba94807 5060 const char *name;
79bd9814
TH
5061 char *endp;
5062 int ret;
5063
2343e88d
SAS
5064 if (IS_ENABLED(CONFIG_PREEMPT_RT))
5065 return -EOPNOTSUPP;
5066
451af504
TH
5067 buf = strstrip(buf);
5068
5069 efd = simple_strtoul(buf, &endp, 10);
79bd9814
TH
5070 if (*endp != ' ')
5071 return -EINVAL;
451af504 5072 buf = endp + 1;
79bd9814 5073
451af504 5074 cfd = simple_strtoul(buf, &endp, 10);
79bd9814
TH
5075 if ((*endp != ' ') && (*endp != '\0'))
5076 return -EINVAL;
451af504 5077 buf = endp + 1;
79bd9814
TH
5078
5079 event = kzalloc(sizeof(*event), GFP_KERNEL);
5080 if (!event)
5081 return -ENOMEM;
5082
59b6f873 5083 event->memcg = memcg;
79bd9814 5084 INIT_LIST_HEAD(&event->list);
3bc942f3
TH
5085 init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc);
5086 init_waitqueue_func_entry(&event->wait, memcg_event_wake);
5087 INIT_WORK(&event->remove, memcg_event_remove);
79bd9814
TH
5088
5089 efile = fdget(efd);
5090 if (!efile.file) {
5091 ret = -EBADF;
5092 goto out_kfree;
5093 }
5094
5095 event->eventfd = eventfd_ctx_fileget(efile.file);
5096 if (IS_ERR(event->eventfd)) {
5097 ret = PTR_ERR(event->eventfd);
5098 goto out_put_efile;
5099 }
5100
5101 cfile = fdget(cfd);
5102 if (!cfile.file) {
5103 ret = -EBADF;
5104 goto out_put_eventfd;
5105 }
5106
5107 /* the process need read permission on control file */
5108 /* AV: shouldn't we check that it's been opened for read instead? */
02f92b38 5109 ret = file_permission(cfile.file, MAY_READ);
79bd9814
TH
5110 if (ret < 0)
5111 goto out_put_cfile;
5112
4a7ba45b
TH
5113 /*
5114 * The control file must be a regular cgroup1 file. As a regular cgroup
5115 * file can't be renamed, it's safe to access its name afterwards.
5116 */
5117 cdentry = cfile.file->f_path.dentry;
5118 if (cdentry->d_sb->s_type != &cgroup_fs_type || !d_is_reg(cdentry)) {
5119 ret = -EINVAL;
5120 goto out_put_cfile;
5121 }
5122
fba94807
TH
5123 /*
5124 * Determine the event callbacks and set them in @event. This used
5125 * to be done via struct cftype but cgroup core no longer knows
5126 * about these events. The following is crude but the whole thing
5127 * is for compatibility anyway.
3bc942f3
TH
5128 *
5129 * DO NOT ADD NEW FILES.
fba94807 5130 */
4a7ba45b 5131 name = cdentry->d_name.name;
fba94807
TH
5132
5133 if (!strcmp(name, "memory.usage_in_bytes")) {
5134 event->register_event = mem_cgroup_usage_register_event;
5135 event->unregister_event = mem_cgroup_usage_unregister_event;
5136 } else if (!strcmp(name, "memory.oom_control")) {
5137 event->register_event = mem_cgroup_oom_register_event;
5138 event->unregister_event = mem_cgroup_oom_unregister_event;
5139 } else if (!strcmp(name, "memory.pressure_level")) {
5140 event->register_event = vmpressure_register_event;
5141 event->unregister_event = vmpressure_unregister_event;
5142 } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) {
347c4a87
TH
5143 event->register_event = memsw_cgroup_usage_register_event;
5144 event->unregister_event = memsw_cgroup_usage_unregister_event;
fba94807
TH
5145 } else {
5146 ret = -EINVAL;
5147 goto out_put_cfile;
5148 }
5149
79bd9814 5150 /*
b5557c4c
TH
5151 * Verify @cfile should belong to @css. Also, remaining events are
5152 * automatically removed on cgroup destruction but the removal is
5153 * asynchronous, so take an extra ref on @css.
79bd9814 5154 */
4a7ba45b 5155 cfile_css = css_tryget_online_from_dir(cdentry->d_parent,
ec903c0c 5156 &memory_cgrp_subsys);
79bd9814 5157 ret = -EINVAL;
5a17f543 5158 if (IS_ERR(cfile_css))
79bd9814 5159 goto out_put_cfile;
5a17f543
TH
5160 if (cfile_css != css) {
5161 css_put(cfile_css);
79bd9814 5162 goto out_put_cfile;
5a17f543 5163 }
79bd9814 5164
451af504 5165 ret = event->register_event(memcg, event->eventfd, buf);
79bd9814
TH
5166 if (ret)
5167 goto out_put_css;
5168
9965ed17 5169 vfs_poll(efile.file, &event->pt);
79bd9814 5170
4ba9515d 5171 spin_lock_irq(&memcg->event_list_lock);
fba94807 5172 list_add(&event->list, &memcg->event_list);
4ba9515d 5173 spin_unlock_irq(&memcg->event_list_lock);
79bd9814
TH
5174
5175 fdput(cfile);
5176 fdput(efile);
5177
451af504 5178 return nbytes;
79bd9814
TH
5179
5180out_put_css:
b5557c4c 5181 css_put(css);
79bd9814
TH
5182out_put_cfile:
5183 fdput(cfile);
5184out_put_eventfd:
5185 eventfd_ctx_put(event->eventfd);
5186out_put_efile:
5187 fdput(efile);
5188out_kfree:
5189 kfree(event);
5190
5191 return ret;
5192}
5193
bc3dcb85 5194#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_SLUB_DEBUG)
c29b5b3d
MS
5195static int mem_cgroup_slab_show(struct seq_file *m, void *p)
5196{
5197 /*
5198 * Deprecated.
df4ae285 5199 * Please, take a look at tools/cgroup/memcg_slabinfo.py .
c29b5b3d
MS
5200 */
5201 return 0;
5202}
5203#endif
5204
dddb44ff
YA
5205static int memory_stat_show(struct seq_file *m, void *v);
5206
241994ed 5207static struct cftype mem_cgroup_legacy_files[] = {
8cdea7c0 5208 {
0eea1030 5209 .name = "usage_in_bytes",
8c7c6e34 5210 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
791badbd 5211 .read_u64 = mem_cgroup_read_u64,
8cdea7c0 5212 },
c84872e1
PE
5213 {
5214 .name = "max_usage_in_bytes",
8c7c6e34 5215 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
6770c64e 5216 .write = mem_cgroup_reset,
791badbd 5217 .read_u64 = mem_cgroup_read_u64,
c84872e1 5218 },
8cdea7c0 5219 {
0eea1030 5220 .name = "limit_in_bytes",
8c7c6e34 5221 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
451af504 5222 .write = mem_cgroup_write,
791badbd 5223 .read_u64 = mem_cgroup_read_u64,
8cdea7c0 5224 },
296c81d8
BS
5225 {
5226 .name = "soft_limit_in_bytes",
5227 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
451af504 5228 .write = mem_cgroup_write,
791badbd 5229 .read_u64 = mem_cgroup_read_u64,
296c81d8 5230 },
8cdea7c0
BS
5231 {
5232 .name = "failcnt",
8c7c6e34 5233 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
6770c64e 5234 .write = mem_cgroup_reset,
791badbd 5235 .read_u64 = mem_cgroup_read_u64,
8cdea7c0 5236 },
d2ceb9b7
KH
5237 {
5238 .name = "stat",
dddb44ff 5239 .seq_show = memory_stat_show,
d2ceb9b7 5240 },
c1e862c1
KH
5241 {
5242 .name = "force_empty",
6770c64e 5243 .write = mem_cgroup_force_empty_write,
c1e862c1 5244 },
18f59ea7
BS
5245 {
5246 .name = "use_hierarchy",
5247 .write_u64 = mem_cgroup_hierarchy_write,
5248 .read_u64 = mem_cgroup_hierarchy_read,
5249 },
79bd9814 5250 {
3bc942f3 5251 .name = "cgroup.event_control", /* XXX: for compat */
451af504 5252 .write = memcg_write_event_control,
7dbdb199 5253 .flags = CFTYPE_NO_PREFIX | CFTYPE_WORLD_WRITABLE,
79bd9814 5254 },
a7885eb8
KM
5255 {
5256 .name = "swappiness",
5257 .read_u64 = mem_cgroup_swappiness_read,
5258 .write_u64 = mem_cgroup_swappiness_write,
5259 },
7dc74be0
DN
5260 {
5261 .name = "move_charge_at_immigrate",
5262 .read_u64 = mem_cgroup_move_charge_read,
5263 .write_u64 = mem_cgroup_move_charge_write,
5264 },
9490ff27
KH
5265 {
5266 .name = "oom_control",
2da8ca82 5267 .seq_show = mem_cgroup_oom_control_read,
3c11ecf4 5268 .write_u64 = mem_cgroup_oom_control_write,
9490ff27 5269 },
70ddf637
AV
5270 {
5271 .name = "pressure_level",
6b0ba2ab 5272 .seq_show = mem_cgroup_dummy_seq_show,
70ddf637 5273 },
406eb0c9
YH
5274#ifdef CONFIG_NUMA
5275 {
5276 .name = "numa_stat",
2da8ca82 5277 .seq_show = memcg_numa_stat_show,
406eb0c9
YH
5278 },
5279#endif
4597648f
MH
5280 {
5281 .name = "kmem.limit_in_bytes",
5282 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5283 .write = mem_cgroup_write,
5284 .read_u64 = mem_cgroup_read_u64,
5285 },
510fc4e1
GC
5286 {
5287 .name = "kmem.usage_in_bytes",
5288 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
791badbd 5289 .read_u64 = mem_cgroup_read_u64,
510fc4e1
GC
5290 },
5291 {
5292 .name = "kmem.failcnt",
5293 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
6770c64e 5294 .write = mem_cgroup_reset,
791badbd 5295 .read_u64 = mem_cgroup_read_u64,
510fc4e1
GC
5296 },
5297 {
5298 .name = "kmem.max_usage_in_bytes",
5299 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
6770c64e 5300 .write = mem_cgroup_reset,
791badbd 5301 .read_u64 = mem_cgroup_read_u64,
510fc4e1 5302 },
bc3dcb85 5303#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_SLUB_DEBUG)
749c5415
GC
5304 {
5305 .name = "kmem.slabinfo",
c29b5b3d 5306 .seq_show = mem_cgroup_slab_show,
749c5415
GC
5307 },
5308#endif
d55f90bf
VD
5309 {
5310 .name = "kmem.tcp.limit_in_bytes",
5311 .private = MEMFILE_PRIVATE(_TCP, RES_LIMIT),
5312 .write = mem_cgroup_write,
5313 .read_u64 = mem_cgroup_read_u64,
5314 },
5315 {
5316 .name = "kmem.tcp.usage_in_bytes",
5317 .private = MEMFILE_PRIVATE(_TCP, RES_USAGE),
5318 .read_u64 = mem_cgroup_read_u64,
5319 },
5320 {
5321 .name = "kmem.tcp.failcnt",
5322 .private = MEMFILE_PRIVATE(_TCP, RES_FAILCNT),
5323 .write = mem_cgroup_reset,
5324 .read_u64 = mem_cgroup_read_u64,
5325 },
5326 {
5327 .name = "kmem.tcp.max_usage_in_bytes",
5328 .private = MEMFILE_PRIVATE(_TCP, RES_MAX_USAGE),
5329 .write = mem_cgroup_reset,
5330 .read_u64 = mem_cgroup_read_u64,
5331 },
6bc10349 5332 { }, /* terminate */
af36f906 5333};
8c7c6e34 5334
73f576c0
JW
5335/*
5336 * Private memory cgroup IDR
5337 *
5338 * Swap-out records and page cache shadow entries need to store memcg
5339 * references in constrained space, so we maintain an ID space that is
5340 * limited to 16 bit (MEM_CGROUP_ID_MAX), limiting the total number of
5341 * memory-controlled cgroups to 64k.
5342 *
b8f2935f 5343 * However, there usually are many references to the offline CSS after
73f576c0
JW
5344 * the cgroup has been destroyed, such as page cache or reclaimable
5345 * slab objects, that don't need to hang on to the ID. We want to keep
5346 * those dead CSS from occupying IDs, or we might quickly exhaust the
5347 * relatively small ID space and prevent the creation of new cgroups
5348 * even when there are much fewer than 64k cgroups - possibly none.
5349 *
5350 * Maintain a private 16-bit ID space for memcg, and allow the ID to
5351 * be freed and recycled when it's no longer needed, which is usually
5352 * when the CSS is offlined.
5353 *
5354 * The only exception to that are records of swapped out tmpfs/shmem
5355 * pages that need to be attributed to live ancestors on swapin. But
5356 * those references are manageable from userspace.
5357 */
5358
60b1e24c 5359#define MEM_CGROUP_ID_MAX ((1UL << MEM_CGROUP_ID_SHIFT) - 1)
73f576c0
JW
5360static DEFINE_IDR(mem_cgroup_idr);
5361
7e97de0b
KT
5362static void mem_cgroup_id_remove(struct mem_cgroup *memcg)
5363{
5364 if (memcg->id.id > 0) {
5365 idr_remove(&mem_cgroup_idr, memcg->id.id);
5366 memcg->id.id = 0;
5367 }
5368}
5369
c1514c0a
VF
5370static void __maybe_unused mem_cgroup_id_get_many(struct mem_cgroup *memcg,
5371 unsigned int n)
73f576c0 5372{
1c2d479a 5373 refcount_add(n, &memcg->id.ref);
73f576c0
JW
5374}
5375
615d66c3 5376static void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n)
73f576c0 5377{
1c2d479a 5378 if (refcount_sub_and_test(n, &memcg->id.ref)) {
7e97de0b 5379 mem_cgroup_id_remove(memcg);
73f576c0
JW
5380
5381 /* Memcg ID pins CSS */
5382 css_put(&memcg->css);
5383 }
5384}
5385
615d66c3
VD
5386static inline void mem_cgroup_id_put(struct mem_cgroup *memcg)
5387{
5388 mem_cgroup_id_put_many(memcg, 1);
5389}
5390
73f576c0
JW
5391/**
5392 * mem_cgroup_from_id - look up a memcg from a memcg id
5393 * @id: the memcg id to look up
5394 *
5395 * Caller must hold rcu_read_lock().
5396 */
5397struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
5398{
5399 WARN_ON_ONCE(!rcu_read_lock_held());
5400 return idr_find(&mem_cgroup_idr, id);
5401}
5402
c15187a4
RG
5403#ifdef CONFIG_SHRINKER_DEBUG
5404struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino)
5405{
5406 struct cgroup *cgrp;
5407 struct cgroup_subsys_state *css;
5408 struct mem_cgroup *memcg;
5409
5410 cgrp = cgroup_get_from_id(ino);
fa7e439c 5411 if (IS_ERR(cgrp))
c0f2df49 5412 return ERR_CAST(cgrp);
c15187a4
RG
5413
5414 css = cgroup_get_e_css(cgrp, &memory_cgrp_subsys);
5415 if (css)
5416 memcg = container_of(css, struct mem_cgroup, css);
5417 else
5418 memcg = ERR_PTR(-ENOENT);
5419
5420 cgroup_put(cgrp);
5421
5422 return memcg;
5423}
5424#endif
5425
ef8f2327 5426static int alloc_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node)
6d12e2d8
KH
5427{
5428 struct mem_cgroup_per_node *pn;
8c9bb398
WY
5429
5430 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, node);
6d12e2d8
KH
5431 if (!pn)
5432 return 1;
1ecaab2b 5433
7e1c0d6f
SB
5434 pn->lruvec_stats_percpu = alloc_percpu_gfp(struct lruvec_stats_percpu,
5435 GFP_KERNEL_ACCOUNT);
5436 if (!pn->lruvec_stats_percpu) {
00f3ca2c
JW
5437 kfree(pn);
5438 return 1;
5439 }
5440
ef8f2327 5441 lruvec_init(&pn->lruvec);
ef8f2327
MG
5442 pn->memcg = memcg;
5443
54f72fe0 5444 memcg->nodeinfo[node] = pn;
6d12e2d8
KH
5445 return 0;
5446}
5447
ef8f2327 5448static void free_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node)
1ecaab2b 5449{
00f3ca2c
JW
5450 struct mem_cgroup_per_node *pn = memcg->nodeinfo[node];
5451
4eaf431f
MH
5452 if (!pn)
5453 return;
5454
7e1c0d6f 5455 free_percpu(pn->lruvec_stats_percpu);
00f3ca2c 5456 kfree(pn);
1ecaab2b
KH
5457}
5458
40e952f9 5459static void __mem_cgroup_free(struct mem_cgroup *memcg)
59927fb9 5460{
c8b2a36f 5461 int node;
59927fb9 5462
675d6c9b
RG
5463 if (memcg->orig_objcg)
5464 obj_cgroup_put(memcg->orig_objcg);
5465
c8b2a36f 5466 for_each_node(node)
ef8f2327 5467 free_mem_cgroup_per_node_info(memcg, node);
410f8e82 5468 kfree(memcg->vmstats);
871789d4 5469 free_percpu(memcg->vmstats_percpu);
8ff69e2c 5470 kfree(memcg);
59927fb9 5471}
3afe36b1 5472
40e952f9
TE
5473static void mem_cgroup_free(struct mem_cgroup *memcg)
5474{
ec1c86b2 5475 lru_gen_exit_memcg(memcg);
40e952f9
TE
5476 memcg_wb_domain_exit(memcg);
5477 __mem_cgroup_free(memcg);
5478}
5479
0b8f73e1 5480static struct mem_cgroup *mem_cgroup_alloc(void)
8cdea7c0 5481{
d142e3e6 5482 struct mem_cgroup *memcg;
6d12e2d8 5483 int node;
97b27821 5484 int __maybe_unused i;
11d67612 5485 long error = -ENOMEM;
8cdea7c0 5486
06b2c3b0 5487 memcg = kzalloc(struct_size(memcg, nodeinfo, nr_node_ids), GFP_KERNEL);
c0ff4b85 5488 if (!memcg)
11d67612 5489 return ERR_PTR(error);
0b8f73e1 5490
73f576c0 5491 memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL,
be740503 5492 1, MEM_CGROUP_ID_MAX + 1, GFP_KERNEL);
11d67612
YS
5493 if (memcg->id.id < 0) {
5494 error = memcg->id.id;
73f576c0 5495 goto fail;
11d67612 5496 }
73f576c0 5497
410f8e82
SB
5498 memcg->vmstats = kzalloc(sizeof(struct memcg_vmstats), GFP_KERNEL);
5499 if (!memcg->vmstats)
5500 goto fail;
5501
3e38e0aa
RG
5502 memcg->vmstats_percpu = alloc_percpu_gfp(struct memcg_vmstats_percpu,
5503 GFP_KERNEL_ACCOUNT);
871789d4 5504 if (!memcg->vmstats_percpu)
0b8f73e1 5505 goto fail;
78fb7466 5506
3ed28fa1 5507 for_each_node(node)
ef8f2327 5508 if (alloc_mem_cgroup_per_node_info(memcg, node))
0b8f73e1 5509 goto fail;
f64c3f54 5510
0b8f73e1
JW
5511 if (memcg_wb_domain_init(memcg, GFP_KERNEL))
5512 goto fail;
28dbc4b6 5513
f7e1cb6e 5514 INIT_WORK(&memcg->high_work, high_work_func);
d142e3e6 5515 INIT_LIST_HEAD(&memcg->oom_notify);
d142e3e6
GC
5516 mutex_init(&memcg->thresholds_lock);
5517 spin_lock_init(&memcg->move_lock);
70ddf637 5518 vmpressure_init(&memcg->vmpressure);
fba94807
TH
5519 INIT_LIST_HEAD(&memcg->event_list);
5520 spin_lock_init(&memcg->event_list_lock);
d886f4e4 5521 memcg->socket_pressure = jiffies;
84c07d11 5522#ifdef CONFIG_MEMCG_KMEM
900a38f0 5523 memcg->kmemcg_id = -1;
bf4f0599 5524 INIT_LIST_HEAD(&memcg->objcg_list);
900a38f0 5525#endif
52ebea74
TH
5526#ifdef CONFIG_CGROUP_WRITEBACK
5527 INIT_LIST_HEAD(&memcg->cgwb_list);
97b27821
TH
5528 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++)
5529 memcg->cgwb_frn[i].done =
5530 __WB_COMPLETION_INIT(&memcg_cgwb_frn_waitq);
87eaceb3
YS
5531#endif
5532#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5533 spin_lock_init(&memcg->deferred_split_queue.split_queue_lock);
5534 INIT_LIST_HEAD(&memcg->deferred_split_queue.split_queue);
5535 memcg->deferred_split_queue.split_queue_len = 0;
52ebea74 5536#endif
ec1c86b2 5537 lru_gen_init_memcg(memcg);
0b8f73e1
JW
5538 return memcg;
5539fail:
7e97de0b 5540 mem_cgroup_id_remove(memcg);
40e952f9 5541 __mem_cgroup_free(memcg);
11d67612 5542 return ERR_PTR(error);
d142e3e6
GC
5543}
5544
0b8f73e1
JW
5545static struct cgroup_subsys_state * __ref
5546mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
d142e3e6 5547{
0b8f73e1 5548 struct mem_cgroup *parent = mem_cgroup_from_css(parent_css);
b87d8cef 5549 struct mem_cgroup *memcg, *old_memcg;
d142e3e6 5550
b87d8cef 5551 old_memcg = set_active_memcg(parent);
0b8f73e1 5552 memcg = mem_cgroup_alloc();
b87d8cef 5553 set_active_memcg(old_memcg);
11d67612
YS
5554 if (IS_ERR(memcg))
5555 return ERR_CAST(memcg);
d142e3e6 5556
d1663a90 5557 page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX);
2178e20c 5558 WRITE_ONCE(memcg->soft_limit, PAGE_COUNTER_MAX);
f4840ccf
JW
5559#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
5560 memcg->zswap_max = PAGE_COUNTER_MAX;
501a06fe
NP
5561 WRITE_ONCE(memcg->zswap_writeback,
5562 !parent || READ_ONCE(parent->zswap_writeback));
f4840ccf 5563#endif
4b82ab4f 5564 page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
0b8f73e1 5565 if (parent) {
82b3aa26 5566 WRITE_ONCE(memcg->swappiness, mem_cgroup_swappiness(parent));
17c56de6 5567 WRITE_ONCE(memcg->oom_kill_disable, READ_ONCE(parent->oom_kill_disable));
bef8620c 5568
3e32cb2e 5569 page_counter_init(&memcg->memory, &parent->memory);
37e84351 5570 page_counter_init(&memcg->swap, &parent->swap);
3e32cb2e 5571 page_counter_init(&memcg->kmem, &parent->kmem);
0db15298 5572 page_counter_init(&memcg->tcpmem, &parent->tcpmem);
18f59ea7 5573 } else {
8278f1c7 5574 init_memcg_events();
bef8620c
RG
5575 page_counter_init(&memcg->memory, NULL);
5576 page_counter_init(&memcg->swap, NULL);
5577 page_counter_init(&memcg->kmem, NULL);
5578 page_counter_init(&memcg->tcpmem, NULL);
d6441637 5579
0b8f73e1
JW
5580 root_mem_cgroup = memcg;
5581 return &memcg->css;
5582 }
5583
f7e1cb6e 5584 if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket)
ef12947c 5585 static_branch_inc(&memcg_sockets_enabled_key);
f7e1cb6e 5586
b6c1a8af
YS
5587#if defined(CONFIG_MEMCG_KMEM)
5588 if (!cgroup_memory_nobpf)
5589 static_branch_inc(&memcg_bpf_enabled_key);
5590#endif
5591
0b8f73e1 5592 return &memcg->css;
0b8f73e1
JW
5593}
5594
73f576c0 5595static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
0b8f73e1 5596{
58fa2a55
VD
5597 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5598
da0efe30
MS
5599 if (memcg_online_kmem(memcg))
5600 goto remove_id;
5601
0a4465d3 5602 /*
e4262c4f 5603 * A memcg must be visible for expand_shrinker_info()
0a4465d3
KT
5604 * by the time the maps are allocated. So, we allocate maps
5605 * here, when for_each_mem_cgroup() can't skip it.
5606 */
da0efe30
MS
5607 if (alloc_shrinker_info(memcg))
5608 goto offline_kmem;
0a4465d3 5609
aa48e47e
SB
5610 if (unlikely(mem_cgroup_is_root(memcg)))
5611 queue_delayed_work(system_unbound_wq, &stats_flush_dwork,
396faf88 5612 FLUSH_TIME);
e4dde56c 5613 lru_gen_online_memcg(memcg);
6f0df8e1
JW
5614
5615 /* Online state pins memcg ID, memcg ID pins CSS */
5616 refcount_set(&memcg->id.ref, 1);
5617 css_get(css);
5618
5619 /*
5620 * Ensure mem_cgroup_from_id() works once we're fully online.
5621 *
5622 * We could do this earlier and require callers to filter with
5623 * css_tryget_online(). But right now there are no users that
5624 * need earlier access, and the workingset code relies on the
5625 * cgroup tree linkage (mem_cgroup_get_nr_swap_pages()). So
5626 * publish it here at the end of onlining. This matches the
5627 * regular ID destruction during offlining.
5628 */
5629 idr_replace(&mem_cgroup_idr, memcg, memcg->id.id);
5630
2f7dd7a4 5631 return 0;
da0efe30
MS
5632offline_kmem:
5633 memcg_offline_kmem(memcg);
5634remove_id:
5635 mem_cgroup_id_remove(memcg);
5636 return -ENOMEM;
8cdea7c0
BS
5637}
5638
eb95419b 5639static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
df878fb0 5640{
eb95419b 5641 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
3bc942f3 5642 struct mem_cgroup_event *event, *tmp;
79bd9814
TH
5643
5644 /*
5645 * Unregister events and notify userspace.
5646 * Notify userspace about cgroup removing only after rmdir of cgroup
5647 * directory to avoid race between userspace and kernelspace.
5648 */
4ba9515d 5649 spin_lock_irq(&memcg->event_list_lock);
fba94807 5650 list_for_each_entry_safe(event, tmp, &memcg->event_list, list) {
79bd9814
TH
5651 list_del_init(&event->list);
5652 schedule_work(&event->remove);
5653 }
4ba9515d 5654 spin_unlock_irq(&memcg->event_list_lock);
ec64f515 5655
bf8d5d52 5656 page_counter_set_min(&memcg->memory, 0);
23067153 5657 page_counter_set_low(&memcg->memory, 0);
63677c74 5658
a65b0e76
DC
5659 zswap_memcg_offline_cleanup(memcg);
5660
567e9ab2 5661 memcg_offline_kmem(memcg);
a178015c 5662 reparent_shrinker_deferred(memcg);
52ebea74 5663 wb_memcg_offline(memcg);
e4dde56c 5664 lru_gen_offline_memcg(memcg);
73f576c0 5665
591edfb1
RG
5666 drain_all_stock(memcg);
5667
73f576c0 5668 mem_cgroup_id_put(memcg);
df878fb0
KH
5669}
5670
6df38689
VD
5671static void mem_cgroup_css_released(struct cgroup_subsys_state *css)
5672{
5673 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5674
5675 invalidate_reclaim_iterators(memcg);
e4dde56c 5676 lru_gen_release_memcg(memcg);
6df38689
VD
5677}
5678
eb95419b 5679static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
8cdea7c0 5680{
eb95419b 5681 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
97b27821 5682 int __maybe_unused i;
c268e994 5683
97b27821
TH
5684#ifdef CONFIG_CGROUP_WRITEBACK
5685 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++)
5686 wb_wait_for_completion(&memcg->cgwb_frn[i].done);
5687#endif
f7e1cb6e 5688 if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket)
ef12947c 5689 static_branch_dec(&memcg_sockets_enabled_key);
127424c8 5690
0db15298 5691 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_active)
d55f90bf 5692 static_branch_dec(&memcg_sockets_enabled_key);
3893e302 5693
b6c1a8af
YS
5694#if defined(CONFIG_MEMCG_KMEM)
5695 if (!cgroup_memory_nobpf)
5696 static_branch_dec(&memcg_bpf_enabled_key);
5697#endif
5698
0b8f73e1
JW
5699 vmpressure_cleanup(&memcg->vmpressure);
5700 cancel_work_sync(&memcg->high_work);
5701 mem_cgroup_remove_from_trees(memcg);
e4262c4f 5702 free_shrinker_info(memcg);
0b8f73e1 5703 mem_cgroup_free(memcg);
8cdea7c0
BS
5704}
5705
1ced953b
TH
5706/**
5707 * mem_cgroup_css_reset - reset the states of a mem_cgroup
5708 * @css: the target css
5709 *
5710 * Reset the states of the mem_cgroup associated with @css. This is
5711 * invoked when the userland requests disabling on the default hierarchy
5712 * but the memcg is pinned through dependency. The memcg should stop
5713 * applying policies and should revert to the vanilla state as it may be
5714 * made visible again.
5715 *
5716 * The current implementation only resets the essential configurations.
5717 * This needs to be expanded to cover all the visible parts.
5718 */
5719static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
5720{
5721 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5722
bbec2e15
RG
5723 page_counter_set_max(&memcg->memory, PAGE_COUNTER_MAX);
5724 page_counter_set_max(&memcg->swap, PAGE_COUNTER_MAX);
bbec2e15
RG
5725 page_counter_set_max(&memcg->kmem, PAGE_COUNTER_MAX);
5726 page_counter_set_max(&memcg->tcpmem, PAGE_COUNTER_MAX);
bf8d5d52 5727 page_counter_set_min(&memcg->memory, 0);
23067153 5728 page_counter_set_low(&memcg->memory, 0);
d1663a90 5729 page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX);
2178e20c 5730 WRITE_ONCE(memcg->soft_limit, PAGE_COUNTER_MAX);
4b82ab4f 5731 page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
2529bb3a 5732 memcg_wb_domain_size_changed(memcg);
1ced953b
TH
5733}
5734
2d146aa3
JW
5735static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu)
5736{
5737 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5738 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5739 struct memcg_vmstats_percpu *statc;
f82e6bf9 5740 long delta, delta_cpu, v;
7e1c0d6f 5741 int i, nid;
2d146aa3
JW
5742
5743 statc = per_cpu_ptr(memcg->vmstats_percpu, cpu);
5744
5745 for (i = 0; i < MEMCG_NR_STAT; i++) {
5746 /*
5747 * Collect the aggregated propagation counts of groups
5748 * below us. We're in a per-cpu loop here and this is
5749 * a global counter, so the first cycle will get them.
5750 */
410f8e82 5751 delta = memcg->vmstats->state_pending[i];
2d146aa3 5752 if (delta)
410f8e82 5753 memcg->vmstats->state_pending[i] = 0;
2d146aa3
JW
5754
5755 /* Add CPU changes on this level since the last flush */
f82e6bf9 5756 delta_cpu = 0;
2d146aa3
JW
5757 v = READ_ONCE(statc->state[i]);
5758 if (v != statc->state_prev[i]) {
f82e6bf9
YA
5759 delta_cpu = v - statc->state_prev[i];
5760 delta += delta_cpu;
2d146aa3
JW
5761 statc->state_prev[i] = v;
5762 }
5763
2d146aa3 5764 /* Aggregate counts on this level and propagate upwards */
f82e6bf9
YA
5765 if (delta_cpu)
5766 memcg->vmstats->state_local[i] += delta_cpu;
5767
5768 if (delta) {
5769 memcg->vmstats->state[i] += delta;
5770 if (parent)
5771 parent->vmstats->state_pending[i] += delta;
5772 }
2d146aa3
JW
5773 }
5774
8278f1c7 5775 for (i = 0; i < NR_MEMCG_EVENTS; i++) {
410f8e82 5776 delta = memcg->vmstats->events_pending[i];
2d146aa3 5777 if (delta)
410f8e82 5778 memcg->vmstats->events_pending[i] = 0;
2d146aa3 5779
f82e6bf9 5780 delta_cpu = 0;
2d146aa3
JW
5781 v = READ_ONCE(statc->events[i]);
5782 if (v != statc->events_prev[i]) {
f82e6bf9
YA
5783 delta_cpu = v - statc->events_prev[i];
5784 delta += delta_cpu;
2d146aa3
JW
5785 statc->events_prev[i] = v;
5786 }
5787
f82e6bf9
YA
5788 if (delta_cpu)
5789 memcg->vmstats->events_local[i] += delta_cpu;
2d146aa3 5790
f82e6bf9
YA
5791 if (delta) {
5792 memcg->vmstats->events[i] += delta;
5793 if (parent)
5794 parent->vmstats->events_pending[i] += delta;
5795 }
2d146aa3 5796 }
7e1c0d6f
SB
5797
5798 for_each_node_state(nid, N_MEMORY) {
5799 struct mem_cgroup_per_node *pn = memcg->nodeinfo[nid];
5800 struct mem_cgroup_per_node *ppn = NULL;
5801 struct lruvec_stats_percpu *lstatc;
5802
5803 if (parent)
5804 ppn = parent->nodeinfo[nid];
5805
5806 lstatc = per_cpu_ptr(pn->lruvec_stats_percpu, cpu);
5807
5808 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
5809 delta = pn->lruvec_stats.state_pending[i];
5810 if (delta)
5811 pn->lruvec_stats.state_pending[i] = 0;
5812
f82e6bf9 5813 delta_cpu = 0;
7e1c0d6f
SB
5814 v = READ_ONCE(lstatc->state[i]);
5815 if (v != lstatc->state_prev[i]) {
f82e6bf9
YA
5816 delta_cpu = v - lstatc->state_prev[i];
5817 delta += delta_cpu;
7e1c0d6f
SB
5818 lstatc->state_prev[i] = v;
5819 }
5820
f82e6bf9
YA
5821 if (delta_cpu)
5822 pn->lruvec_stats.state_local[i] += delta_cpu;
7e1c0d6f 5823
f82e6bf9
YA
5824 if (delta) {
5825 pn->lruvec_stats.state[i] += delta;
5826 if (ppn)
5827 ppn->lruvec_stats.state_pending[i] += delta;
5828 }
7e1c0d6f
SB
5829 }
5830 }
8d59d221
YA
5831 statc->stats_updates = 0;
5832 /* We are in a per-cpu loop here, only do the atomic write once */
5833 if (atomic64_read(&memcg->vmstats->stats_updates))
5834 atomic64_set(&memcg->vmstats->stats_updates, 0);
2d146aa3
JW
5835}
5836
02491447 5837#ifdef CONFIG_MMU
7dc74be0 5838/* Handlers for move charge at task migration. */
854ffa8d 5839static int mem_cgroup_do_precharge(unsigned long count)
7dc74be0 5840{
05b84301 5841 int ret;
9476db97 5842
d0164adc
MG
5843 /* Try a single bulk charge without reclaim first, kswapd may wake */
5844 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_DIRECT_RECLAIM, count);
9476db97 5845 if (!ret) {
854ffa8d 5846 mc.precharge += count;
854ffa8d
DN
5847 return ret;
5848 }
9476db97 5849
3674534b 5850 /* Try charges one by one with reclaim, but do not retry */
854ffa8d 5851 while (count--) {
3674534b 5852 ret = try_charge(mc.to, GFP_KERNEL | __GFP_NORETRY, 1);
38c5d72f 5853 if (ret)
38c5d72f 5854 return ret;
854ffa8d 5855 mc.precharge++;
9476db97 5856 cond_resched();
854ffa8d 5857 }
9476db97 5858 return 0;
4ffef5fe
DN
5859}
5860
4ffef5fe
DN
5861union mc_target {
5862 struct page *page;
02491447 5863 swp_entry_t ent;
4ffef5fe
DN
5864};
5865
4ffef5fe 5866enum mc_target_type {
8d32ff84 5867 MC_TARGET_NONE = 0,
4ffef5fe 5868 MC_TARGET_PAGE,
02491447 5869 MC_TARGET_SWAP,
c733a828 5870 MC_TARGET_DEVICE,
4ffef5fe
DN
5871};
5872
90254a65
DN
5873static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5874 unsigned long addr, pte_t ptent)
4ffef5fe 5875{
25b2995a 5876 struct page *page = vm_normal_page(vma, addr, ptent);
4ffef5fe 5877
58f341f7 5878 if (!page)
90254a65
DN
5879 return NULL;
5880 if (PageAnon(page)) {
1dfab5ab 5881 if (!(mc.flags & MOVE_ANON))
90254a65 5882 return NULL;
1dfab5ab
JW
5883 } else {
5884 if (!(mc.flags & MOVE_FILE))
5885 return NULL;
5886 }
58f341f7 5887 get_page(page);
90254a65
DN
5888
5889 return page;
5890}
5891
c733a828 5892#if defined(CONFIG_SWAP) || defined(CONFIG_DEVICE_PRIVATE)
90254a65 5893static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
48406ef8 5894 pte_t ptent, swp_entry_t *entry)
90254a65 5895{
90254a65
DN
5896 struct page *page = NULL;
5897 swp_entry_t ent = pte_to_swp_entry(ptent);
5898
9a137153 5899 if (!(mc.flags & MOVE_ANON))
90254a65 5900 return NULL;
c733a828
JG
5901
5902 /*
27674ef6
CH
5903 * Handle device private pages that are not accessible by the CPU, but
5904 * stored as special swap entries in the page table.
c733a828
JG
5905 */
5906 if (is_device_private_entry(ent)) {
af5cdaf8 5907 page = pfn_swap_entry_to_page(ent);
27674ef6 5908 if (!get_page_unless_zero(page))
c733a828
JG
5909 return NULL;
5910 return page;
5911 }
5912
9a137153
RC
5913 if (non_swap_entry(ent))
5914 return NULL;
5915
4b91355e 5916 /*
cb691e2f 5917 * Because swap_cache_get_folio() updates some statistics counter,
4b91355e
KH
5918 * we call find_get_page() with swapper_space directly.
5919 */
f6ab1f7f 5920 page = find_get_page(swap_address_space(ent), swp_offset(ent));
2d1c4980 5921 entry->val = ent.val;
90254a65
DN
5922
5923 return page;
5924}
4b91355e
KH
5925#else
5926static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
48406ef8 5927 pte_t ptent, swp_entry_t *entry)
4b91355e
KH
5928{
5929 return NULL;
5930}
5931#endif
90254a65 5932
87946a72 5933static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
48384b0b 5934 unsigned long addr, pte_t ptent)
87946a72 5935{
524984ff
MWO
5936 unsigned long index;
5937 struct folio *folio;
5938
87946a72
DN
5939 if (!vma->vm_file) /* anonymous vma */
5940 return NULL;
1dfab5ab 5941 if (!(mc.flags & MOVE_FILE))
87946a72
DN
5942 return NULL;
5943
524984ff 5944 /* folio is moved even if it's not RSS of this task(page-faulted). */
aa3b1895 5945 /* shmem/tmpfs may report page out on swap: account for that too. */
524984ff
MWO
5946 index = linear_page_index(vma, addr);
5947 folio = filemap_get_incore_folio(vma->vm_file->f_mapping, index);
66dabbb6 5948 if (IS_ERR(folio))
524984ff
MWO
5949 return NULL;
5950 return folio_file_page(folio, index);
87946a72
DN
5951}
5952
b1b0deab
CG
5953/**
5954 * mem_cgroup_move_account - move account of the page
5955 * @page: the page
25843c2b 5956 * @compound: charge the page as compound or small page
b1b0deab
CG
5957 * @from: mem_cgroup which the page is moved from.
5958 * @to: mem_cgroup which the page is moved to. @from != @to.
5959 *
4e0cf05f 5960 * The page must be locked and not on the LRU.
b1b0deab
CG
5961 *
5962 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
5963 * from old cgroup.
5964 */
5965static int mem_cgroup_move_account(struct page *page,
f627c2f5 5966 bool compound,
b1b0deab
CG
5967 struct mem_cgroup *from,
5968 struct mem_cgroup *to)
5969{
fcce4672 5970 struct folio *folio = page_folio(page);
ae8af438
KK
5971 struct lruvec *from_vec, *to_vec;
5972 struct pglist_data *pgdat;
fcce4672 5973 unsigned int nr_pages = compound ? folio_nr_pages(folio) : 1;
8e88bd2d 5974 int nid, ret;
b1b0deab
CG
5975
5976 VM_BUG_ON(from == to);
4e0cf05f 5977 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
fcce4672 5978 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
9c325215 5979 VM_BUG_ON(compound && !folio_test_large(folio));
b1b0deab 5980
b1b0deab 5981 ret = -EINVAL;
fcce4672 5982 if (folio_memcg(folio) != from)
4e0cf05f 5983 goto out;
b1b0deab 5984
fcce4672 5985 pgdat = folio_pgdat(folio);
867e5e1d
JW
5986 from_vec = mem_cgroup_lruvec(from, pgdat);
5987 to_vec = mem_cgroup_lruvec(to, pgdat);
ae8af438 5988
fcce4672 5989 folio_memcg_lock(folio);
b1b0deab 5990
fcce4672
MWO
5991 if (folio_test_anon(folio)) {
5992 if (folio_mapped(folio)) {
be5d0a74
JW
5993 __mod_lruvec_state(from_vec, NR_ANON_MAPPED, -nr_pages);
5994 __mod_lruvec_state(to_vec, NR_ANON_MAPPED, nr_pages);
6199277b 5995 if (folio_test_pmd_mappable(folio)) {
69473e5d
MS
5996 __mod_lruvec_state(from_vec, NR_ANON_THPS,
5997 -nr_pages);
5998 __mod_lruvec_state(to_vec, NR_ANON_THPS,
5999 nr_pages);
468c3982 6000 }
be5d0a74
JW
6001 }
6002 } else {
0d1c2072
JW
6003 __mod_lruvec_state(from_vec, NR_FILE_PAGES, -nr_pages);
6004 __mod_lruvec_state(to_vec, NR_FILE_PAGES, nr_pages);
6005
fcce4672 6006 if (folio_test_swapbacked(folio)) {
0d1c2072
JW
6007 __mod_lruvec_state(from_vec, NR_SHMEM, -nr_pages);
6008 __mod_lruvec_state(to_vec, NR_SHMEM, nr_pages);
6009 }
6010
fcce4672 6011 if (folio_mapped(folio)) {
49e50d27
JW
6012 __mod_lruvec_state(from_vec, NR_FILE_MAPPED, -nr_pages);
6013 __mod_lruvec_state(to_vec, NR_FILE_MAPPED, nr_pages);
6014 }
b1b0deab 6015
fcce4672
MWO
6016 if (folio_test_dirty(folio)) {
6017 struct address_space *mapping = folio_mapping(folio);
c4843a75 6018
f56753ac 6019 if (mapping_can_writeback(mapping)) {
49e50d27
JW
6020 __mod_lruvec_state(from_vec, NR_FILE_DIRTY,
6021 -nr_pages);
6022 __mod_lruvec_state(to_vec, NR_FILE_DIRTY,
6023 nr_pages);
6024 }
c4843a75
GT
6025 }
6026 }
6027
c449deb2
HD
6028#ifdef CONFIG_SWAP
6029 if (folio_test_swapcache(folio)) {
6030 __mod_lruvec_state(from_vec, NR_SWAPCACHE, -nr_pages);
6031 __mod_lruvec_state(to_vec, NR_SWAPCACHE, nr_pages);
6032 }
6033#endif
fcce4672 6034 if (folio_test_writeback(folio)) {
ae8af438
KK
6035 __mod_lruvec_state(from_vec, NR_WRITEBACK, -nr_pages);
6036 __mod_lruvec_state(to_vec, NR_WRITEBACK, nr_pages);
b1b0deab
CG
6037 }
6038
6039 /*
abb242f5
JW
6040 * All state has been migrated, let's switch to the new memcg.
6041 *
bcfe06bf 6042 * It is safe to change page's memcg here because the page
abb242f5
JW
6043 * is referenced, charged, isolated, and locked: we can't race
6044 * with (un)charging, migration, LRU putback, or anything else
bcfe06bf 6045 * that would rely on a stable page's memory cgroup.
abb242f5 6046 *
6c77b607 6047 * Note that folio_memcg_lock is a memcg lock, not a page lock,
bcfe06bf 6048 * to save space. As soon as we switch page's memory cgroup to a
abb242f5
JW
6049 * new memcg that isn't locked, the above state can change
6050 * concurrently again. Make sure we're truly done with it.
b1b0deab 6051 */
abb242f5 6052 smp_mb();
b1b0deab 6053
1a3e1f40
JW
6054 css_get(&to->css);
6055 css_put(&from->css);
6056
fcce4672 6057 folio->memcg_data = (unsigned long)to;
87eaceb3 6058
f70ad448 6059 __folio_memcg_unlock(from);
b1b0deab
CG
6060
6061 ret = 0;
fcce4672 6062 nid = folio_nid(folio);
b1b0deab
CG
6063
6064 local_irq_disable();
6e0110c2 6065 mem_cgroup_charge_statistics(to, nr_pages);
8e88bd2d 6066 memcg_check_events(to, nid);
6e0110c2 6067 mem_cgroup_charge_statistics(from, -nr_pages);
8e88bd2d 6068 memcg_check_events(from, nid);
b1b0deab 6069 local_irq_enable();
b1b0deab
CG
6070out:
6071 return ret;
6072}
6073
7cf7806c
LR
6074/**
6075 * get_mctgt_type - get target type of moving charge
6076 * @vma: the vma the pte to be checked belongs
6077 * @addr: the address corresponding to the pte to be checked
6078 * @ptent: the pte to be checked
6079 * @target: the pointer the target page or swap ent will be stored(can be NULL)
6080 *
853f62a3
MWO
6081 * Context: Called with pte lock held.
6082 * Return:
6083 * * MC_TARGET_NONE - If the pte is not a target for move charge.
6084 * * MC_TARGET_PAGE - If the page corresponding to this pte is a target for
6085 * move charge. If @target is not NULL, the page is stored in target->page
6086 * with extra refcnt taken (Caller should release it).
6087 * * MC_TARGET_SWAP - If the swap entry corresponding to this pte is a
6088 * target for charge migration. If @target is not NULL, the entry is
6089 * stored in target->ent.
6090 * * MC_TARGET_DEVICE - Like MC_TARGET_PAGE but page is device memory and
6091 * thus not on the lru. For now such page is charged like a regular page
6092 * would be as it is just special memory taking the place of a regular page.
6093 * See Documentations/vm/hmm.txt and include/linux/hmm.h
7cf7806c 6094 */
8d32ff84 6095static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
90254a65
DN
6096 unsigned long addr, pte_t ptent, union mc_target *target)
6097{
6098 struct page *page = NULL;
8d32ff84 6099 enum mc_target_type ret = MC_TARGET_NONE;
90254a65
DN
6100 swp_entry_t ent = { .val = 0 };
6101
6102 if (pte_present(ptent))
6103 page = mc_handle_present_pte(vma, addr, ptent);
5c041f5d
PX
6104 else if (pte_none_mostly(ptent))
6105 /*
6106 * PTE markers should be treated as a none pte here, separated
6107 * from other swap handling below.
6108 */
6109 page = mc_handle_file_pte(vma, addr, ptent);
90254a65 6110 else if (is_swap_pte(ptent))
48406ef8 6111 page = mc_handle_swap_pte(vma, ptent, &ent);
90254a65 6112
4e0cf05f
JW
6113 if (target && page) {
6114 if (!trylock_page(page)) {
6115 put_page(page);
6116 return ret;
6117 }
6118 /*
6119 * page_mapped() must be stable during the move. This
6120 * pte is locked, so if it's present, the page cannot
6121 * become unmapped. If it isn't, we have only partial
6122 * control over the mapped state: the page lock will
6123 * prevent new faults against pagecache and swapcache,
6124 * so an unmapped page cannot become mapped. However,
6125 * if the page is already mapped elsewhere, it can
6126 * unmap, and there is nothing we can do about it.
6127 * Alas, skip moving the page in this case.
6128 */
6129 if (!pte_present(ptent) && page_mapped(page)) {
6130 unlock_page(page);
6131 put_page(page);
6132 return ret;
6133 }
6134 }
6135
90254a65 6136 if (!page && !ent.val)
8d32ff84 6137 return ret;
02491447 6138 if (page) {
02491447 6139 /*
0a31bc97 6140 * Do only loose check w/o serialization.
1306a85a 6141 * mem_cgroup_move_account() checks the page is valid or
0a31bc97 6142 * not under LRU exclusion.
02491447 6143 */
bcfe06bf 6144 if (page_memcg(page) == mc.from) {
02491447 6145 ret = MC_TARGET_PAGE;
f25cbb7a
AS
6146 if (is_device_private_page(page) ||
6147 is_device_coherent_page(page))
c733a828 6148 ret = MC_TARGET_DEVICE;
02491447
DN
6149 if (target)
6150 target->page = page;
6151 }
4e0cf05f
JW
6152 if (!ret || !target) {
6153 if (target)
6154 unlock_page(page);
02491447 6155 put_page(page);
4e0cf05f 6156 }
02491447 6157 }
3e14a57b
HY
6158 /*
6159 * There is a swap entry and a page doesn't exist or isn't charged.
6160 * But we cannot move a tail-page in a THP.
6161 */
6162 if (ent.val && !ret && (!page || !PageTransCompound(page)) &&
34c00c31 6163 mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) {
7f0f1546
KH
6164 ret = MC_TARGET_SWAP;
6165 if (target)
6166 target->ent = ent;
4ffef5fe 6167 }
4ffef5fe
DN
6168 return ret;
6169}
6170
12724850
NH
6171#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6172/*
d6810d73
HY
6173 * We don't consider PMD mapped swapping or file mapped pages because THP does
6174 * not support them for now.
12724850
NH
6175 * Caller should make sure that pmd_trans_huge(pmd) is true.
6176 */
6177static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6178 unsigned long addr, pmd_t pmd, union mc_target *target)
6179{
6180 struct page *page = NULL;
12724850
NH
6181 enum mc_target_type ret = MC_TARGET_NONE;
6182
84c3fc4e
ZY
6183 if (unlikely(is_swap_pmd(pmd))) {
6184 VM_BUG_ON(thp_migration_supported() &&
6185 !is_pmd_migration_entry(pmd));
6186 return ret;
6187 }
12724850 6188 page = pmd_page(pmd);
309381fe 6189 VM_BUG_ON_PAGE(!page || !PageHead(page), page);
1dfab5ab 6190 if (!(mc.flags & MOVE_ANON))
12724850 6191 return ret;
bcfe06bf 6192 if (page_memcg(page) == mc.from) {
12724850
NH
6193 ret = MC_TARGET_PAGE;
6194 if (target) {
6195 get_page(page);
4e0cf05f
JW
6196 if (!trylock_page(page)) {
6197 put_page(page);
6198 return MC_TARGET_NONE;
6199 }
12724850
NH
6200 target->page = page;
6201 }
6202 }
6203 return ret;
6204}
6205#else
6206static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6207 unsigned long addr, pmd_t pmd, union mc_target *target)
6208{
6209 return MC_TARGET_NONE;
6210}
6211#endif
6212
4ffef5fe
DN
6213static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6214 unsigned long addr, unsigned long end,
6215 struct mm_walk *walk)
6216{
26bcd64a 6217 struct vm_area_struct *vma = walk->vma;
4ffef5fe
DN
6218 pte_t *pte;
6219 spinlock_t *ptl;
6220
b6ec57f4
KS
6221 ptl = pmd_trans_huge_lock(pmd, vma);
6222 if (ptl) {
c733a828
JG
6223 /*
6224 * Note their can not be MC_TARGET_DEVICE for now as we do not
25b2995a
CH
6225 * support transparent huge page with MEMORY_DEVICE_PRIVATE but
6226 * this might change.
c733a828 6227 */
12724850
NH
6228 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6229 mc.precharge += HPAGE_PMD_NR;
bf929152 6230 spin_unlock(ptl);
1a5a9906 6231 return 0;
12724850 6232 }
03319327 6233
4ffef5fe 6234 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
04dee9e8
HD
6235 if (!pte)
6236 return 0;
4ffef5fe 6237 for (; addr != end; pte++, addr += PAGE_SIZE)
c33c7948 6238 if (get_mctgt_type(vma, addr, ptep_get(pte), NULL))
4ffef5fe
DN
6239 mc.precharge++; /* increment precharge temporarily */
6240 pte_unmap_unlock(pte - 1, ptl);
6241 cond_resched();
6242
7dc74be0
DN
6243 return 0;
6244}
6245
7b86ac33
CH
6246static const struct mm_walk_ops precharge_walk_ops = {
6247 .pmd_entry = mem_cgroup_count_precharge_pte_range,
49b06385 6248 .walk_lock = PGWALK_RDLOCK,
7b86ac33
CH
6249};
6250
4ffef5fe
DN
6251static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6252{
6253 unsigned long precharge;
4ffef5fe 6254
d8ed45c5 6255 mmap_read_lock(mm);
ba0aff8e 6256 walk_page_range(mm, 0, ULONG_MAX, &precharge_walk_ops, NULL);
d8ed45c5 6257 mmap_read_unlock(mm);
4ffef5fe
DN
6258
6259 precharge = mc.precharge;
6260 mc.precharge = 0;
6261
6262 return precharge;
6263}
6264
4ffef5fe
DN
6265static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6266{
dfe076b0
DN
6267 unsigned long precharge = mem_cgroup_count_precharge(mm);
6268
6269 VM_BUG_ON(mc.moving_task);
6270 mc.moving_task = current;
6271 return mem_cgroup_do_precharge(precharge);
4ffef5fe
DN
6272}
6273
dfe076b0
DN
6274/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6275static void __mem_cgroup_clear_mc(void)
4ffef5fe 6276{
2bd9bb20
KH
6277 struct mem_cgroup *from = mc.from;
6278 struct mem_cgroup *to = mc.to;
6279
4ffef5fe 6280 /* we must uncharge all the leftover precharges from mc.to */
854ffa8d 6281 if (mc.precharge) {
4b569387 6282 mem_cgroup_cancel_charge(mc.to, mc.precharge);
854ffa8d
DN
6283 mc.precharge = 0;
6284 }
6285 /*
6286 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6287 * we must uncharge here.
6288 */
6289 if (mc.moved_charge) {
4b569387 6290 mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
854ffa8d 6291 mc.moved_charge = 0;
4ffef5fe 6292 }
483c30b5
DN
6293 /* we must fixup refcnts and charges */
6294 if (mc.moved_swap) {
483c30b5 6295 /* uncharge swap account from the old cgroup */
ce00a967 6296 if (!mem_cgroup_is_root(mc.from))
3e32cb2e 6297 page_counter_uncharge(&mc.from->memsw, mc.moved_swap);
483c30b5 6298
615d66c3
VD
6299 mem_cgroup_id_put_many(mc.from, mc.moved_swap);
6300
05b84301 6301 /*
3e32cb2e
JW
6302 * we charged both to->memory and to->memsw, so we
6303 * should uncharge to->memory.
05b84301 6304 */
ce00a967 6305 if (!mem_cgroup_is_root(mc.to))
3e32cb2e
JW
6306 page_counter_uncharge(&mc.to->memory, mc.moved_swap);
6307
483c30b5
DN
6308 mc.moved_swap = 0;
6309 }
dfe076b0
DN
6310 memcg_oom_recover(from);
6311 memcg_oom_recover(to);
6312 wake_up_all(&mc.waitq);
6313}
6314
6315static void mem_cgroup_clear_mc(void)
6316{
264a0ae1
TH
6317 struct mm_struct *mm = mc.mm;
6318
dfe076b0
DN
6319 /*
6320 * we must clear moving_task before waking up waiters at the end of
6321 * task migration.
6322 */
6323 mc.moving_task = NULL;
6324 __mem_cgroup_clear_mc();
2bd9bb20 6325 spin_lock(&mc.lock);
4ffef5fe
DN
6326 mc.from = NULL;
6327 mc.to = NULL;
264a0ae1 6328 mc.mm = NULL;
2bd9bb20 6329 spin_unlock(&mc.lock);
264a0ae1
TH
6330
6331 mmput(mm);
4ffef5fe
DN
6332}
6333
1f7dd3e5 6334static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
7dc74be0 6335{
1f7dd3e5 6336 struct cgroup_subsys_state *css;
eed67d75 6337 struct mem_cgroup *memcg = NULL; /* unneeded init to make gcc happy */
9f2115f9 6338 struct mem_cgroup *from;
4530eddb 6339 struct task_struct *leader, *p;
9f2115f9 6340 struct mm_struct *mm;
1dfab5ab 6341 unsigned long move_flags;
9f2115f9 6342 int ret = 0;
7dc74be0 6343
1f7dd3e5
TH
6344 /* charge immigration isn't supported on the default hierarchy */
6345 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
9f2115f9
TH
6346 return 0;
6347
4530eddb
TH
6348 /*
6349 * Multi-process migrations only happen on the default hierarchy
6350 * where charge immigration is not used. Perform charge
6351 * immigration if @tset contains a leader and whine if there are
6352 * multiple.
6353 */
6354 p = NULL;
1f7dd3e5 6355 cgroup_taskset_for_each_leader(leader, css, tset) {
4530eddb
TH
6356 WARN_ON_ONCE(p);
6357 p = leader;
1f7dd3e5 6358 memcg = mem_cgroup_from_css(css);
4530eddb
TH
6359 }
6360 if (!p)
6361 return 0;
6362
1f7dd3e5 6363 /*
f0953a1b 6364 * We are now committed to this value whatever it is. Changes in this
1f7dd3e5
TH
6365 * tunable will only affect upcoming migrations, not the current one.
6366 * So we need to save it, and keep it going.
6367 */
6368 move_flags = READ_ONCE(memcg->move_charge_at_immigrate);
6369 if (!move_flags)
6370 return 0;
6371
9f2115f9
TH
6372 from = mem_cgroup_from_task(p);
6373
6374 VM_BUG_ON(from == memcg);
6375
6376 mm = get_task_mm(p);
6377 if (!mm)
6378 return 0;
6379 /* We move charges only when we move a owner of the mm */
6380 if (mm->owner == p) {
6381 VM_BUG_ON(mc.from);
6382 VM_BUG_ON(mc.to);
6383 VM_BUG_ON(mc.precharge);
6384 VM_BUG_ON(mc.moved_charge);
6385 VM_BUG_ON(mc.moved_swap);
6386
6387 spin_lock(&mc.lock);
264a0ae1 6388 mc.mm = mm;
9f2115f9
TH
6389 mc.from = from;
6390 mc.to = memcg;
6391 mc.flags = move_flags;
6392 spin_unlock(&mc.lock);
6393 /* We set mc.moving_task later */
6394
6395 ret = mem_cgroup_precharge_mc(mm);
6396 if (ret)
6397 mem_cgroup_clear_mc();
264a0ae1
TH
6398 } else {
6399 mmput(mm);
7dc74be0
DN
6400 }
6401 return ret;
6402}
6403
1f7dd3e5 6404static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset)
7dc74be0 6405{
4e2f245d
JW
6406 if (mc.to)
6407 mem_cgroup_clear_mc();
7dc74be0
DN
6408}
6409
4ffef5fe
DN
6410static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6411 unsigned long addr, unsigned long end,
6412 struct mm_walk *walk)
7dc74be0 6413{
4ffef5fe 6414 int ret = 0;
26bcd64a 6415 struct vm_area_struct *vma = walk->vma;
4ffef5fe
DN
6416 pte_t *pte;
6417 spinlock_t *ptl;
12724850
NH
6418 enum mc_target_type target_type;
6419 union mc_target target;
6420 struct page *page;
4ffef5fe 6421
b6ec57f4
KS
6422 ptl = pmd_trans_huge_lock(pmd, vma);
6423 if (ptl) {
62ade86a 6424 if (mc.precharge < HPAGE_PMD_NR) {
bf929152 6425 spin_unlock(ptl);
12724850
NH
6426 return 0;
6427 }
6428 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6429 if (target_type == MC_TARGET_PAGE) {
6430 page = target.page;
f7f9c00d 6431 if (isolate_lru_page(page)) {
f627c2f5 6432 if (!mem_cgroup_move_account(page, true,
1306a85a 6433 mc.from, mc.to)) {
12724850
NH
6434 mc.precharge -= HPAGE_PMD_NR;
6435 mc.moved_charge += HPAGE_PMD_NR;
6436 }
6437 putback_lru_page(page);
6438 }
4e0cf05f 6439 unlock_page(page);
12724850 6440 put_page(page);
c733a828
JG
6441 } else if (target_type == MC_TARGET_DEVICE) {
6442 page = target.page;
6443 if (!mem_cgroup_move_account(page, true,
6444 mc.from, mc.to)) {
6445 mc.precharge -= HPAGE_PMD_NR;
6446 mc.moved_charge += HPAGE_PMD_NR;
6447 }
4e0cf05f 6448 unlock_page(page);
c733a828 6449 put_page(page);
12724850 6450 }
bf929152 6451 spin_unlock(ptl);
1a5a9906 6452 return 0;
12724850
NH
6453 }
6454
4ffef5fe
DN
6455retry:
6456 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
04dee9e8
HD
6457 if (!pte)
6458 return 0;
4ffef5fe 6459 for (; addr != end; addr += PAGE_SIZE) {
c33c7948 6460 pte_t ptent = ptep_get(pte++);
c733a828 6461 bool device = false;
02491447 6462 swp_entry_t ent;
4ffef5fe
DN
6463
6464 if (!mc.precharge)
6465 break;
6466
8d32ff84 6467 switch (get_mctgt_type(vma, addr, ptent, &target)) {
c733a828
JG
6468 case MC_TARGET_DEVICE:
6469 device = true;
e4a9bc58 6470 fallthrough;
4ffef5fe
DN
6471 case MC_TARGET_PAGE:
6472 page = target.page;
53f9263b
KS
6473 /*
6474 * We can have a part of the split pmd here. Moving it
6475 * can be done but it would be too convoluted so simply
6476 * ignore such a partial THP and keep it in original
6477 * memcg. There should be somebody mapping the head.
6478 */
6479 if (PageTransCompound(page))
6480 goto put;
f7f9c00d 6481 if (!device && !isolate_lru_page(page))
4ffef5fe 6482 goto put;
f627c2f5
KS
6483 if (!mem_cgroup_move_account(page, false,
6484 mc.from, mc.to)) {
4ffef5fe 6485 mc.precharge--;
854ffa8d
DN
6486 /* we uncharge from mc.from later. */
6487 mc.moved_charge++;
4ffef5fe 6488 }
c733a828
JG
6489 if (!device)
6490 putback_lru_page(page);
4e0cf05f
JW
6491put: /* get_mctgt_type() gets & locks the page */
6492 unlock_page(page);
4ffef5fe
DN
6493 put_page(page);
6494 break;
02491447
DN
6495 case MC_TARGET_SWAP:
6496 ent = target.ent;
e91cbb42 6497 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
02491447 6498 mc.precharge--;
8d22a935
HD
6499 mem_cgroup_id_get_many(mc.to, 1);
6500 /* we fixup other refcnts and charges later. */
483c30b5
DN
6501 mc.moved_swap++;
6502 }
02491447 6503 break;
4ffef5fe
DN
6504 default:
6505 break;
6506 }
6507 }
6508 pte_unmap_unlock(pte - 1, ptl);
6509 cond_resched();
6510
6511 if (addr != end) {
6512 /*
6513 * We have consumed all precharges we got in can_attach().
6514 * We try charge one by one, but don't do any additional
6515 * charges to mc.to if we have failed in charge once in attach()
6516 * phase.
6517 */
854ffa8d 6518 ret = mem_cgroup_do_precharge(1);
4ffef5fe
DN
6519 if (!ret)
6520 goto retry;
6521 }
6522
6523 return ret;
6524}
6525
7b86ac33
CH
6526static const struct mm_walk_ops charge_walk_ops = {
6527 .pmd_entry = mem_cgroup_move_charge_pte_range,
49b06385 6528 .walk_lock = PGWALK_RDLOCK,
7b86ac33
CH
6529};
6530
264a0ae1 6531static void mem_cgroup_move_charge(void)
4ffef5fe 6532{
4ffef5fe 6533 lru_add_drain_all();
312722cb 6534 /*
6c77b607 6535 * Signal folio_memcg_lock() to take the memcg's move_lock
81f8c3a4
JW
6536 * while we're moving its pages to another memcg. Then wait
6537 * for already started RCU-only updates to finish.
312722cb
JW
6538 */
6539 atomic_inc(&mc.from->moving_account);
6540 synchronize_rcu();
dfe076b0 6541retry:
d8ed45c5 6542 if (unlikely(!mmap_read_trylock(mc.mm))) {
dfe076b0 6543 /*
c1e8d7c6 6544 * Someone who are holding the mmap_lock might be waiting in
dfe076b0
DN
6545 * waitq. So we cancel all extra charges, wake up all waiters,
6546 * and retry. Because we cancel precharges, we might not be able
6547 * to move enough charges, but moving charge is a best-effort
6548 * feature anyway, so it wouldn't be a big problem.
6549 */
6550 __mem_cgroup_clear_mc();
6551 cond_resched();
6552 goto retry;
6553 }
26bcd64a
NH
6554 /*
6555 * When we have consumed all precharges and failed in doing
6556 * additional charge, the page walk just aborts.
6557 */
ba0aff8e 6558 walk_page_range(mc.mm, 0, ULONG_MAX, &charge_walk_ops, NULL);
d8ed45c5 6559 mmap_read_unlock(mc.mm);
312722cb 6560 atomic_dec(&mc.from->moving_account);
7dc74be0
DN
6561}
6562
264a0ae1 6563static void mem_cgroup_move_task(void)
67e465a7 6564{
264a0ae1
TH
6565 if (mc.to) {
6566 mem_cgroup_move_charge();
a433658c 6567 mem_cgroup_clear_mc();
264a0ae1 6568 }
67e465a7 6569}
1aacbd35 6570
5cfb80a7 6571#else /* !CONFIG_MMU */
1f7dd3e5 6572static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
5cfb80a7
DN
6573{
6574 return 0;
6575}
1f7dd3e5 6576static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset)
5cfb80a7
DN
6577{
6578}
264a0ae1 6579static void mem_cgroup_move_task(void)
5cfb80a7
DN
6580{
6581}
6582#endif
67e465a7 6583
1aacbd35
RG
6584#ifdef CONFIG_MEMCG_KMEM
6585static void mem_cgroup_fork(struct task_struct *task)
6586{
6587 /*
6588 * Set the update flag to cause task->objcg to be initialized lazily
6589 * on the first allocation. It can be done without any synchronization
6590 * because it's always performed on the current task, so does
6591 * current_objcg_update().
6592 */
6593 task->objcg = (struct obj_cgroup *)CURRENT_OBJCG_UPDATE_FLAG;
6594}
6595
6596static void mem_cgroup_exit(struct task_struct *task)
6597{
6598 struct obj_cgroup *objcg = task->objcg;
6599
6600 objcg = (struct obj_cgroup *)
6601 ((unsigned long)objcg & ~CURRENT_OBJCG_UPDATE_FLAG);
6602 if (objcg)
6603 obj_cgroup_put(objcg);
6604
6605 /*
6606 * Some kernel allocations can happen after this point,
6607 * but let's ignore them. It can be done without any synchronization
6608 * because it's always performed on the current task, so does
6609 * current_objcg_update().
6610 */
6611 task->objcg = NULL;
6612}
6613#endif
6614
bd74fdae 6615#ifdef CONFIG_LRU_GEN
1aacbd35 6616static void mem_cgroup_lru_gen_attach(struct cgroup_taskset *tset)
bd74fdae
YZ
6617{
6618 struct task_struct *task;
6619 struct cgroup_subsys_state *css;
6620
6621 /* find the first leader if there is any */
6622 cgroup_taskset_for_each_leader(task, css, tset)
6623 break;
6624
6625 if (!task)
6626 return;
6627
6628 task_lock(task);
6629 if (task->mm && READ_ONCE(task->mm->owner) == task)
6630 lru_gen_migrate_mm(task->mm);
6631 task_unlock(task);
6632}
6633#else
1aacbd35
RG
6634static void mem_cgroup_lru_gen_attach(struct cgroup_taskset *tset) {}
6635#endif /* CONFIG_LRU_GEN */
6636
6637#ifdef CONFIG_MEMCG_KMEM
6638static void mem_cgroup_kmem_attach(struct cgroup_taskset *tset)
6639{
6640 struct task_struct *task;
6641 struct cgroup_subsys_state *css;
6642
6643 cgroup_taskset_for_each(task, css, tset) {
6644 /* atomically set the update bit */
6645 set_bit(CURRENT_OBJCG_UPDATE_BIT, (unsigned long *)&task->objcg);
6646 }
6647}
6648#else
6649static void mem_cgroup_kmem_attach(struct cgroup_taskset *tset) {}
6650#endif /* CONFIG_MEMCG_KMEM */
6651
6652#if defined(CONFIG_LRU_GEN) || defined(CONFIG_MEMCG_KMEM)
bd74fdae
YZ
6653static void mem_cgroup_attach(struct cgroup_taskset *tset)
6654{
1aacbd35
RG
6655 mem_cgroup_lru_gen_attach(tset);
6656 mem_cgroup_kmem_attach(tset);
bd74fdae 6657}
1aacbd35 6658#endif
bd74fdae 6659
677dc973
CD
6660static int seq_puts_memcg_tunable(struct seq_file *m, unsigned long value)
6661{
6662 if (value == PAGE_COUNTER_MAX)
6663 seq_puts(m, "max\n");
6664 else
6665 seq_printf(m, "%llu\n", (u64)value * PAGE_SIZE);
6666
6667 return 0;
6668}
6669
241994ed
JW
6670static u64 memory_current_read(struct cgroup_subsys_state *css,
6671 struct cftype *cft)
6672{
f5fc3c5d
JW
6673 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
6674
6675 return (u64)page_counter_read(&memcg->memory) * PAGE_SIZE;
241994ed
JW
6676}
6677
8e20d4b3
GR
6678static u64 memory_peak_read(struct cgroup_subsys_state *css,
6679 struct cftype *cft)
6680{
6681 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
6682
6683 return (u64)memcg->memory.watermark * PAGE_SIZE;
6684}
6685
bf8d5d52
RG
6686static int memory_min_show(struct seq_file *m, void *v)
6687{
677dc973
CD
6688 return seq_puts_memcg_tunable(m,
6689 READ_ONCE(mem_cgroup_from_seq(m)->memory.min));
bf8d5d52
RG
6690}
6691
6692static ssize_t memory_min_write(struct kernfs_open_file *of,
6693 char *buf, size_t nbytes, loff_t off)
6694{
6695 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6696 unsigned long min;
6697 int err;
6698
6699 buf = strstrip(buf);
6700 err = page_counter_memparse(buf, "max", &min);
6701 if (err)
6702 return err;
6703
6704 page_counter_set_min(&memcg->memory, min);
6705
6706 return nbytes;
6707}
6708
241994ed
JW
6709static int memory_low_show(struct seq_file *m, void *v)
6710{
677dc973
CD
6711 return seq_puts_memcg_tunable(m,
6712 READ_ONCE(mem_cgroup_from_seq(m)->memory.low));
241994ed
JW
6713}
6714
6715static ssize_t memory_low_write(struct kernfs_open_file *of,
6716 char *buf, size_t nbytes, loff_t off)
6717{
6718 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6719 unsigned long low;
6720 int err;
6721
6722 buf = strstrip(buf);
d2973697 6723 err = page_counter_memparse(buf, "max", &low);
241994ed
JW
6724 if (err)
6725 return err;
6726
23067153 6727 page_counter_set_low(&memcg->memory, low);
241994ed
JW
6728
6729 return nbytes;
6730}
6731
6732static int memory_high_show(struct seq_file *m, void *v)
6733{
d1663a90
JK
6734 return seq_puts_memcg_tunable(m,
6735 READ_ONCE(mem_cgroup_from_seq(m)->memory.high));
241994ed
JW
6736}
6737
6738static ssize_t memory_high_write(struct kernfs_open_file *of,
6739 char *buf, size_t nbytes, loff_t off)
6740{
6741 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
d977aa93 6742 unsigned int nr_retries = MAX_RECLAIM_RETRIES;
8c8c383c 6743 bool drained = false;
241994ed
JW
6744 unsigned long high;
6745 int err;
6746
6747 buf = strstrip(buf);
d2973697 6748 err = page_counter_memparse(buf, "max", &high);
241994ed
JW
6749 if (err)
6750 return err;
6751
e82553c1
JW
6752 page_counter_set_high(&memcg->memory, high);
6753
8c8c383c
JW
6754 for (;;) {
6755 unsigned long nr_pages = page_counter_read(&memcg->memory);
6756 unsigned long reclaimed;
6757
6758 if (nr_pages <= high)
6759 break;
6760
6761 if (signal_pending(current))
6762 break;
6763
6764 if (!drained) {
6765 drain_all_stock(memcg);
6766 drained = true;
6767 continue;
6768 }
6769
6770 reclaimed = try_to_free_mem_cgroup_pages(memcg, nr_pages - high,
55ab834a 6771 GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP);
8c8c383c
JW
6772
6773 if (!reclaimed && !nr_retries--)
6774 break;
6775 }
588083bb 6776
19ce33ac 6777 memcg_wb_domain_size_changed(memcg);
241994ed
JW
6778 return nbytes;
6779}
6780
6781static int memory_max_show(struct seq_file *m, void *v)
6782{
677dc973
CD
6783 return seq_puts_memcg_tunable(m,
6784 READ_ONCE(mem_cgroup_from_seq(m)->memory.max));
241994ed
JW
6785}
6786
6787static ssize_t memory_max_write(struct kernfs_open_file *of,
6788 char *buf, size_t nbytes, loff_t off)
6789{
6790 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
d977aa93 6791 unsigned int nr_reclaims = MAX_RECLAIM_RETRIES;
b6e6edcf 6792 bool drained = false;
241994ed
JW
6793 unsigned long max;
6794 int err;
6795
6796 buf = strstrip(buf);
d2973697 6797 err = page_counter_memparse(buf, "max", &max);
241994ed
JW
6798 if (err)
6799 return err;
6800
bbec2e15 6801 xchg(&memcg->memory.max, max);
b6e6edcf
JW
6802
6803 for (;;) {
6804 unsigned long nr_pages = page_counter_read(&memcg->memory);
6805
6806 if (nr_pages <= max)
6807 break;
6808
7249c9f0 6809 if (signal_pending(current))
b6e6edcf 6810 break;
b6e6edcf
JW
6811
6812 if (!drained) {
6813 drain_all_stock(memcg);
6814 drained = true;
6815 continue;
6816 }
6817
6818 if (nr_reclaims) {
6819 if (!try_to_free_mem_cgroup_pages(memcg, nr_pages - max,
55ab834a 6820 GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP))
b6e6edcf
JW
6821 nr_reclaims--;
6822 continue;
6823 }
6824
e27be240 6825 memcg_memory_event(memcg, MEMCG_OOM);
b6e6edcf
JW
6826 if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0))
6827 break;
6828 }
241994ed 6829
2529bb3a 6830 memcg_wb_domain_size_changed(memcg);
241994ed
JW
6831 return nbytes;
6832}
6833
664dc218
DR
6834/*
6835 * Note: don't forget to update the 'samples/cgroup/memcg_event_listener'
6836 * if any new events become available.
6837 */
1e577f97
SB
6838static void __memory_events_show(struct seq_file *m, atomic_long_t *events)
6839{
6840 seq_printf(m, "low %lu\n", atomic_long_read(&events[MEMCG_LOW]));
6841 seq_printf(m, "high %lu\n", atomic_long_read(&events[MEMCG_HIGH]));
6842 seq_printf(m, "max %lu\n", atomic_long_read(&events[MEMCG_MAX]));
6843 seq_printf(m, "oom %lu\n", atomic_long_read(&events[MEMCG_OOM]));
6844 seq_printf(m, "oom_kill %lu\n",
6845 atomic_long_read(&events[MEMCG_OOM_KILL]));
b6bf9abb
DS
6846 seq_printf(m, "oom_group_kill %lu\n",
6847 atomic_long_read(&events[MEMCG_OOM_GROUP_KILL]));
1e577f97
SB
6848}
6849
241994ed
JW
6850static int memory_events_show(struct seq_file *m, void *v)
6851{
aa9694bb 6852 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
241994ed 6853
1e577f97
SB
6854 __memory_events_show(m, memcg->memory_events);
6855 return 0;
6856}
6857
6858static int memory_events_local_show(struct seq_file *m, void *v)
6859{
6860 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
241994ed 6861
1e577f97 6862 __memory_events_show(m, memcg->memory_events_local);
241994ed
JW
6863 return 0;
6864}
6865
587d9f72
JW
6866static int memory_stat_show(struct seq_file *m, void *v)
6867{
aa9694bb 6868 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
68aaee14 6869 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5b42360c 6870 struct seq_buf s;
1ff9e6e1 6871
c8713d0b
JW
6872 if (!buf)
6873 return -ENOMEM;
5b42360c
YA
6874 seq_buf_init(&s, buf, PAGE_SIZE);
6875 memory_stat_format(memcg, &s);
c8713d0b
JW
6876 seq_puts(m, buf);
6877 kfree(buf);
587d9f72
JW
6878 return 0;
6879}
6880
5f9a4f4a 6881#ifdef CONFIG_NUMA
fff66b79
MS
6882static inline unsigned long lruvec_page_state_output(struct lruvec *lruvec,
6883 int item)
6884{
ff841a06
YA
6885 return lruvec_page_state(lruvec, item) *
6886 memcg_page_state_output_unit(item);
fff66b79
MS
6887}
6888
5f9a4f4a
MS
6889static int memory_numa_stat_show(struct seq_file *m, void *v)
6890{
6891 int i;
6892 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
6893
7d7ef0a4 6894 mem_cgroup_flush_stats(memcg);
7e1c0d6f 6895
5f9a4f4a
MS
6896 for (i = 0; i < ARRAY_SIZE(memory_stats); i++) {
6897 int nid;
6898
6899 if (memory_stats[i].idx >= NR_VM_NODE_STAT_ITEMS)
6900 continue;
6901
6902 seq_printf(m, "%s", memory_stats[i].name);
6903 for_each_node_state(nid, N_MEMORY) {
6904 u64 size;
6905 struct lruvec *lruvec;
6906
6907 lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid));
fff66b79
MS
6908 size = lruvec_page_state_output(lruvec,
6909 memory_stats[i].idx);
5f9a4f4a
MS
6910 seq_printf(m, " N%d=%llu", nid, size);
6911 }
6912 seq_putc(m, '\n');
6913 }
6914
6915 return 0;
6916}
6917#endif
6918
3d8b38eb
RG
6919static int memory_oom_group_show(struct seq_file *m, void *v)
6920{
aa9694bb 6921 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
3d8b38eb 6922
eaf7b66b 6923 seq_printf(m, "%d\n", READ_ONCE(memcg->oom_group));
3d8b38eb
RG
6924
6925 return 0;
6926}
6927
6928static ssize_t memory_oom_group_write(struct kernfs_open_file *of,
6929 char *buf, size_t nbytes, loff_t off)
6930{
6931 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6932 int ret, oom_group;
6933
6934 buf = strstrip(buf);
6935 if (!buf)
6936 return -EINVAL;
6937
6938 ret = kstrtoint(buf, 0, &oom_group);
6939 if (ret)
6940 return ret;
6941
6942 if (oom_group != 0 && oom_group != 1)
6943 return -EINVAL;
6944
eaf7b66b 6945 WRITE_ONCE(memcg->oom_group, oom_group);
3d8b38eb
RG
6946
6947 return nbytes;
6948}
6949
94968384
SB
6950static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
6951 size_t nbytes, loff_t off)
6952{
6953 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6954 unsigned int nr_retries = MAX_RECLAIM_RETRIES;
6955 unsigned long nr_to_reclaim, nr_reclaimed = 0;
55ab834a
MH
6956 unsigned int reclaim_options;
6957 int err;
12a5d395
MA
6958
6959 buf = strstrip(buf);
55ab834a
MH
6960 err = page_counter_memparse(buf, "", &nr_to_reclaim);
6961 if (err)
6962 return err;
12a5d395 6963
55ab834a 6964 reclaim_options = MEMCG_RECLAIM_MAY_SWAP | MEMCG_RECLAIM_PROACTIVE;
94968384
SB
6965 while (nr_reclaimed < nr_to_reclaim) {
6966 unsigned long reclaimed;
6967
6968 if (signal_pending(current))
6969 return -EINTR;
6970
6971 /*
6972 * This is the final attempt, drain percpu lru caches in the
6973 * hope of introducing more evictable pages for
6974 * try_to_free_mem_cgroup_pages().
6975 */
6976 if (!nr_retries)
6977 lru_add_drain_all();
6978
6979 reclaimed = try_to_free_mem_cgroup_pages(memcg,
0388536a
EY
6980 min(nr_to_reclaim - nr_reclaimed, SWAP_CLUSTER_MAX),
6981 GFP_KERNEL, reclaim_options);
94968384
SB
6982
6983 if (!reclaimed && !nr_retries--)
6984 return -EAGAIN;
6985
6986 nr_reclaimed += reclaimed;
6987 }
6988
6989 return nbytes;
6990}
6991
241994ed
JW
6992static struct cftype memory_files[] = {
6993 {
6994 .name = "current",
f5fc3c5d 6995 .flags = CFTYPE_NOT_ON_ROOT,
241994ed
JW
6996 .read_u64 = memory_current_read,
6997 },
8e20d4b3
GR
6998 {
6999 .name = "peak",
7000 .flags = CFTYPE_NOT_ON_ROOT,
7001 .read_u64 = memory_peak_read,
7002 },
bf8d5d52
RG
7003 {
7004 .name = "min",
7005 .flags = CFTYPE_NOT_ON_ROOT,
7006 .seq_show = memory_min_show,
7007 .write = memory_min_write,
7008 },
241994ed
JW
7009 {
7010 .name = "low",
7011 .flags = CFTYPE_NOT_ON_ROOT,
7012 .seq_show = memory_low_show,
7013 .write = memory_low_write,
7014 },
7015 {
7016 .name = "high",
7017 .flags = CFTYPE_NOT_ON_ROOT,
7018 .seq_show = memory_high_show,
7019 .write = memory_high_write,
7020 },
7021 {
7022 .name = "max",
7023 .flags = CFTYPE_NOT_ON_ROOT,
7024 .seq_show = memory_max_show,
7025 .write = memory_max_write,
7026 },
7027 {
7028 .name = "events",
7029 .flags = CFTYPE_NOT_ON_ROOT,
472912a2 7030 .file_offset = offsetof(struct mem_cgroup, events_file),
241994ed
JW
7031 .seq_show = memory_events_show,
7032 },
1e577f97
SB
7033 {
7034 .name = "events.local",
7035 .flags = CFTYPE_NOT_ON_ROOT,
7036 .file_offset = offsetof(struct mem_cgroup, events_local_file),
7037 .seq_show = memory_events_local_show,
7038 },
587d9f72
JW
7039 {
7040 .name = "stat",
587d9f72
JW
7041 .seq_show = memory_stat_show,
7042 },
5f9a4f4a
MS
7043#ifdef CONFIG_NUMA
7044 {
7045 .name = "numa_stat",
7046 .seq_show = memory_numa_stat_show,
7047 },
7048#endif
3d8b38eb
RG
7049 {
7050 .name = "oom.group",
7051 .flags = CFTYPE_NOT_ON_ROOT | CFTYPE_NS_DELEGATABLE,
7052 .seq_show = memory_oom_group_show,
7053 .write = memory_oom_group_write,
7054 },
94968384
SB
7055 {
7056 .name = "reclaim",
7057 .flags = CFTYPE_NS_DELEGATABLE,
7058 .write = memory_reclaim,
7059 },
241994ed
JW
7060 { } /* terminate */
7061};
7062
073219e9 7063struct cgroup_subsys memory_cgrp_subsys = {
92fb9748 7064 .css_alloc = mem_cgroup_css_alloc,
d142e3e6 7065 .css_online = mem_cgroup_css_online,
92fb9748 7066 .css_offline = mem_cgroup_css_offline,
6df38689 7067 .css_released = mem_cgroup_css_released,
92fb9748 7068 .css_free = mem_cgroup_css_free,
1ced953b 7069 .css_reset = mem_cgroup_css_reset,
2d146aa3 7070 .css_rstat_flush = mem_cgroup_css_rstat_flush,
7dc74be0 7071 .can_attach = mem_cgroup_can_attach,
1aacbd35 7072#if defined(CONFIG_LRU_GEN) || defined(CONFIG_MEMCG_KMEM)
bd74fdae 7073 .attach = mem_cgroup_attach,
1aacbd35 7074#endif
7dc74be0 7075 .cancel_attach = mem_cgroup_cancel_attach,
264a0ae1 7076 .post_attach = mem_cgroup_move_task,
1aacbd35
RG
7077#ifdef CONFIG_MEMCG_KMEM
7078 .fork = mem_cgroup_fork,
7079 .exit = mem_cgroup_exit,
7080#endif
241994ed
JW
7081 .dfl_cftypes = memory_files,
7082 .legacy_cftypes = mem_cgroup_legacy_files,
6d12e2d8 7083 .early_init = 0,
8cdea7c0 7084};
c077719b 7085
bc50bcc6
JW
7086/*
7087 * This function calculates an individual cgroup's effective
7088 * protection which is derived from its own memory.min/low, its
7089 * parent's and siblings' settings, as well as the actual memory
7090 * distribution in the tree.
7091 *
7092 * The following rules apply to the effective protection values:
7093 *
7094 * 1. At the first level of reclaim, effective protection is equal to
7095 * the declared protection in memory.min and memory.low.
7096 *
7097 * 2. To enable safe delegation of the protection configuration, at
7098 * subsequent levels the effective protection is capped to the
7099 * parent's effective protection.
7100 *
7101 * 3. To make complex and dynamic subtrees easier to configure, the
7102 * user is allowed to overcommit the declared protection at a given
7103 * level. If that is the case, the parent's effective protection is
7104 * distributed to the children in proportion to how much protection
7105 * they have declared and how much of it they are utilizing.
7106 *
7107 * This makes distribution proportional, but also work-conserving:
7108 * if one cgroup claims much more protection than it uses memory,
7109 * the unused remainder is available to its siblings.
7110 *
7111 * 4. Conversely, when the declared protection is undercommitted at a
7112 * given level, the distribution of the larger parental protection
7113 * budget is NOT proportional. A cgroup's protection from a sibling
7114 * is capped to its own memory.min/low setting.
7115 *
8a931f80
JW
7116 * 5. However, to allow protecting recursive subtrees from each other
7117 * without having to declare each individual cgroup's fixed share
7118 * of the ancestor's claim to protection, any unutilized -
7119 * "floating" - protection from up the tree is distributed in
7120 * proportion to each cgroup's *usage*. This makes the protection
7121 * neutral wrt sibling cgroups and lets them compete freely over
7122 * the shared parental protection budget, but it protects the
7123 * subtree as a whole from neighboring subtrees.
7124 *
7125 * Note that 4. and 5. are not in conflict: 4. is about protecting
7126 * against immediate siblings whereas 5. is about protecting against
7127 * neighboring subtrees.
bc50bcc6
JW
7128 */
7129static unsigned long effective_protection(unsigned long usage,
8a931f80 7130 unsigned long parent_usage,
bc50bcc6
JW
7131 unsigned long setting,
7132 unsigned long parent_effective,
7133 unsigned long siblings_protected)
7134{
7135 unsigned long protected;
8a931f80 7136 unsigned long ep;
bc50bcc6
JW
7137
7138 protected = min(usage, setting);
7139 /*
7140 * If all cgroups at this level combined claim and use more
08e0f49e 7141 * protection than what the parent affords them, distribute
bc50bcc6
JW
7142 * shares in proportion to utilization.
7143 *
7144 * We are using actual utilization rather than the statically
7145 * claimed protection in order to be work-conserving: claimed
7146 * but unused protection is available to siblings that would
7147 * otherwise get a smaller chunk than what they claimed.
7148 */
7149 if (siblings_protected > parent_effective)
7150 return protected * parent_effective / siblings_protected;
7151
7152 /*
7153 * Ok, utilized protection of all children is within what the
7154 * parent affords them, so we know whatever this child claims
7155 * and utilizes is effectively protected.
7156 *
7157 * If there is unprotected usage beyond this value, reclaim
7158 * will apply pressure in proportion to that amount.
7159 *
7160 * If there is unutilized protection, the cgroup will be fully
7161 * shielded from reclaim, but we do return a smaller value for
7162 * protection than what the group could enjoy in theory. This
7163 * is okay. With the overcommit distribution above, effective
7164 * protection is always dependent on how memory is actually
7165 * consumed among the siblings anyway.
7166 */
8a931f80
JW
7167 ep = protected;
7168
7169 /*
7170 * If the children aren't claiming (all of) the protection
7171 * afforded to them by the parent, distribute the remainder in
7172 * proportion to the (unprotected) memory of each cgroup. That
7173 * way, cgroups that aren't explicitly prioritized wrt each
7174 * other compete freely over the allowance, but they are
7175 * collectively protected from neighboring trees.
7176 *
7177 * We're using unprotected memory for the weight so that if
7178 * some cgroups DO claim explicit protection, we don't protect
7179 * the same bytes twice.
cd324edc
JW
7180 *
7181 * Check both usage and parent_usage against the respective
7182 * protected values. One should imply the other, but they
7183 * aren't read atomically - make sure the division is sane.
8a931f80
JW
7184 */
7185 if (!(cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT))
7186 return ep;
cd324edc
JW
7187 if (parent_effective > siblings_protected &&
7188 parent_usage > siblings_protected &&
7189 usage > protected) {
8a931f80
JW
7190 unsigned long unclaimed;
7191
7192 unclaimed = parent_effective - siblings_protected;
7193 unclaimed *= usage - protected;
7194 unclaimed /= parent_usage - siblings_protected;
7195
7196 ep += unclaimed;
7197 }
7198
7199 return ep;
bc50bcc6
JW
7200}
7201
241994ed 7202/**
05395718 7203 * mem_cgroup_calculate_protection - check if memory consumption is in the normal range
34c81057 7204 * @root: the top ancestor of the sub-tree being checked
241994ed
JW
7205 * @memcg: the memory cgroup to check
7206 *
23067153
RG
7207 * WARNING: This function is not stateless! It can only be used as part
7208 * of a top-down tree iteration, not for isolated queries.
241994ed 7209 */
45c7f7e1
CD
7210void mem_cgroup_calculate_protection(struct mem_cgroup *root,
7211 struct mem_cgroup *memcg)
241994ed 7212{
8a931f80 7213 unsigned long usage, parent_usage;
23067153
RG
7214 struct mem_cgroup *parent;
7215
241994ed 7216 if (mem_cgroup_disabled())
45c7f7e1 7217 return;
241994ed 7218
34c81057
SC
7219 if (!root)
7220 root = root_mem_cgroup;
22f7496f
YS
7221
7222 /*
7223 * Effective values of the reclaim targets are ignored so they
7224 * can be stale. Have a look at mem_cgroup_protection for more
7225 * details.
7226 * TODO: calculation should be more robust so that we do not need
7227 * that special casing.
7228 */
34c81057 7229 if (memcg == root)
45c7f7e1 7230 return;
241994ed 7231
23067153 7232 usage = page_counter_read(&memcg->memory);
bf8d5d52 7233 if (!usage)
45c7f7e1 7234 return;
bf8d5d52 7235
bf8d5d52 7236 parent = parent_mem_cgroup(memcg);
df2a4196 7237
bc50bcc6 7238 if (parent == root) {
c3d53200 7239 memcg->memory.emin = READ_ONCE(memcg->memory.min);
03960e33 7240 memcg->memory.elow = READ_ONCE(memcg->memory.low);
45c7f7e1 7241 return;
bf8d5d52
RG
7242 }
7243
8a931f80
JW
7244 parent_usage = page_counter_read(&parent->memory);
7245
b3a7822e 7246 WRITE_ONCE(memcg->memory.emin, effective_protection(usage, parent_usage,
c3d53200
CD
7247 READ_ONCE(memcg->memory.min),
7248 READ_ONCE(parent->memory.emin),
b3a7822e 7249 atomic_long_read(&parent->memory.children_min_usage)));
23067153 7250
b3a7822e 7251 WRITE_ONCE(memcg->memory.elow, effective_protection(usage, parent_usage,
03960e33
CD
7252 READ_ONCE(memcg->memory.low),
7253 READ_ONCE(parent->memory.elow),
b3a7822e 7254 atomic_long_read(&parent->memory.children_low_usage)));
241994ed
JW
7255}
7256
8f425e4e
MWO
7257static int charge_memcg(struct folio *folio, struct mem_cgroup *memcg,
7258 gfp_t gfp)
0add0c77 7259{
0add0c77
SB
7260 int ret;
7261
4b569387 7262 ret = try_charge(memcg, gfp, folio_nr_pages(folio));
0add0c77
SB
7263 if (ret)
7264 goto out;
7265
4b569387 7266 mem_cgroup_commit_charge(folio, memcg);
0add0c77
SB
7267out:
7268 return ret;
7269}
7270
8f425e4e 7271int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp)
00501b53 7272{
0add0c77
SB
7273 struct mem_cgroup *memcg;
7274 int ret;
00501b53 7275
0add0c77 7276 memcg = get_mem_cgroup_from_mm(mm);
8f425e4e 7277 ret = charge_memcg(folio, memcg, gfp);
0add0c77 7278 css_put(&memcg->css);
2d1c4980 7279
0add0c77
SB
7280 return ret;
7281}
e993d905 7282
8cba9576
NP
7283/**
7284 * mem_cgroup_hugetlb_try_charge - try to charge the memcg for a hugetlb folio
7285 * @memcg: memcg to charge.
7286 * @gfp: reclaim mode.
7287 * @nr_pages: number of pages to charge.
7288 *
7289 * This function is called when allocating a huge page folio to determine if
7290 * the memcg has the capacity for it. It does not commit the charge yet,
7291 * as the hugetlb folio itself has not been obtained from the hugetlb pool.
7292 *
7293 * Once we have obtained the hugetlb folio, we can call
7294 * mem_cgroup_commit_charge() to commit the charge. If we fail to obtain the
7295 * folio, we should instead call mem_cgroup_cancel_charge() to undo the effect
7296 * of try_charge().
7297 *
7298 * Returns 0 on success. Otherwise, an error code is returned.
7299 */
7300int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, gfp_t gfp,
7301 long nr_pages)
7302{
7303 /*
7304 * If hugetlb memcg charging is not enabled, do not fail hugetlb allocation,
7305 * but do not attempt to commit charge later (or cancel on error) either.
7306 */
7307 if (mem_cgroup_disabled() || !memcg ||
7308 !cgroup_subsys_on_dfl(memory_cgrp_subsys) ||
7309 !(cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING))
7310 return -EOPNOTSUPP;
7311
7312 if (try_charge(memcg, gfp, nr_pages))
7313 return -ENOMEM;
7314
7315 return 0;
7316}
7317
0add0c77 7318/**
65995918
MWO
7319 * mem_cgroup_swapin_charge_folio - Charge a newly allocated folio for swapin.
7320 * @folio: folio to charge.
0add0c77
SB
7321 * @mm: mm context of the victim
7322 * @gfp: reclaim mode
65995918 7323 * @entry: swap entry for which the folio is allocated
0add0c77 7324 *
65995918
MWO
7325 * This function charges a folio allocated for swapin. Please call this before
7326 * adding the folio to the swapcache.
0add0c77
SB
7327 *
7328 * Returns 0 on success. Otherwise, an error code is returned.
7329 */
65995918 7330int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm,
0add0c77
SB
7331 gfp_t gfp, swp_entry_t entry)
7332{
7333 struct mem_cgroup *memcg;
7334 unsigned short id;
7335 int ret;
00501b53 7336
0add0c77
SB
7337 if (mem_cgroup_disabled())
7338 return 0;
00501b53 7339
0add0c77
SB
7340 id = lookup_swap_cgroup_id(entry);
7341 rcu_read_lock();
7342 memcg = mem_cgroup_from_id(id);
7343 if (!memcg || !css_tryget_online(&memcg->css))
7344 memcg = get_mem_cgroup_from_mm(mm);
7345 rcu_read_unlock();
00501b53 7346
8f425e4e 7347 ret = charge_memcg(folio, memcg, gfp);
6abb5a86 7348
0add0c77
SB
7349 css_put(&memcg->css);
7350 return ret;
7351}
00501b53 7352
0add0c77
SB
7353/*
7354 * mem_cgroup_swapin_uncharge_swap - uncharge swap slot
7355 * @entry: swap entry for which the page is charged
7356 *
7357 * Call this function after successfully adding the charged page to swapcache.
7358 *
7359 * Note: This function assumes the page for which swap slot is being uncharged
7360 * is order 0 page.
7361 */
7362void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry)
7363{
cae3af62
MS
7364 /*
7365 * Cgroup1's unified memory+swap counter has been charged with the
7366 * new swapcache page, finish the transfer by uncharging the swap
7367 * slot. The swap slot would also get uncharged when it dies, but
7368 * it can stick around indefinitely and we'd count the page twice
7369 * the entire time.
7370 *
7371 * Cgroup2 has separate resource counters for memory and swap,
7372 * so this is a non-issue here. Memory and swap charge lifetimes
7373 * correspond 1:1 to page and swap slot lifetimes: we charge the
7374 * page to memory here, and uncharge swap when the slot is freed.
7375 */
0add0c77 7376 if (!mem_cgroup_disabled() && do_memsw_account()) {
00501b53
JW
7377 /*
7378 * The swap entry might not get freed for a long time,
7379 * let's not wait for it. The page already received a
7380 * memory+swap charge, drop the swap entry duplicate.
7381 */
0add0c77 7382 mem_cgroup_uncharge_swap(entry, 1);
00501b53 7383 }
3fea5a49
JW
7384}
7385
a9d5adee
JG
7386struct uncharge_gather {
7387 struct mem_cgroup *memcg;
b4e0b68f 7388 unsigned long nr_memory;
a9d5adee 7389 unsigned long pgpgout;
a9d5adee 7390 unsigned long nr_kmem;
8e88bd2d 7391 int nid;
a9d5adee
JG
7392};
7393
7394static inline void uncharge_gather_clear(struct uncharge_gather *ug)
747db954 7395{
a9d5adee
JG
7396 memset(ug, 0, sizeof(*ug));
7397}
7398
7399static void uncharge_batch(const struct uncharge_gather *ug)
7400{
747db954
JW
7401 unsigned long flags;
7402
b4e0b68f
MS
7403 if (ug->nr_memory) {
7404 page_counter_uncharge(&ug->memcg->memory, ug->nr_memory);
7941d214 7405 if (do_memsw_account())
b4e0b68f 7406 page_counter_uncharge(&ug->memcg->memsw, ug->nr_memory);
a8c49af3
YA
7407 if (ug->nr_kmem)
7408 memcg_account_kmem(ug->memcg, -ug->nr_kmem);
a9d5adee 7409 memcg_oom_recover(ug->memcg);
ce00a967 7410 }
747db954
JW
7411
7412 local_irq_save(flags);
c9019e9b 7413 __count_memcg_events(ug->memcg, PGPGOUT, ug->pgpgout);
b4e0b68f 7414 __this_cpu_add(ug->memcg->vmstats_percpu->nr_page_events, ug->nr_memory);
8e88bd2d 7415 memcg_check_events(ug->memcg, ug->nid);
747db954 7416 local_irq_restore(flags);
f1796544 7417
c4ed6ebf 7418 /* drop reference from uncharge_folio */
f1796544 7419 css_put(&ug->memcg->css);
a9d5adee
JG
7420}
7421
c4ed6ebf 7422static void uncharge_folio(struct folio *folio, struct uncharge_gather *ug)
a9d5adee 7423{
c4ed6ebf 7424 long nr_pages;
b4e0b68f
MS
7425 struct mem_cgroup *memcg;
7426 struct obj_cgroup *objcg;
9f762dbe 7427
c4ed6ebf 7428 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
a9d5adee 7429
a9d5adee
JG
7430 /*
7431 * Nobody should be changing or seriously looking at
c4ed6ebf
MWO
7432 * folio memcg or objcg at this point, we have fully
7433 * exclusive access to the folio.
a9d5adee 7434 */
fead2b86 7435 if (folio_memcg_kmem(folio)) {
1b7e4464 7436 objcg = __folio_objcg(folio);
b4e0b68f
MS
7437 /*
7438 * This get matches the put at the end of the function and
7439 * kmem pages do not hold memcg references anymore.
7440 */
7441 memcg = get_mem_cgroup_from_objcg(objcg);
7442 } else {
1b7e4464 7443 memcg = __folio_memcg(folio);
b4e0b68f 7444 }
a9d5adee 7445
b4e0b68f
MS
7446 if (!memcg)
7447 return;
7448
7449 if (ug->memcg != memcg) {
a9d5adee
JG
7450 if (ug->memcg) {
7451 uncharge_batch(ug);
7452 uncharge_gather_clear(ug);
7453 }
b4e0b68f 7454 ug->memcg = memcg;
c4ed6ebf 7455 ug->nid = folio_nid(folio);
f1796544
MH
7456
7457 /* pairs with css_put in uncharge_batch */
b4e0b68f 7458 css_get(&memcg->css);
a9d5adee
JG
7459 }
7460
c4ed6ebf 7461 nr_pages = folio_nr_pages(folio);
a9d5adee 7462
fead2b86 7463 if (folio_memcg_kmem(folio)) {
b4e0b68f 7464 ug->nr_memory += nr_pages;
9f762dbe 7465 ug->nr_kmem += nr_pages;
b4e0b68f 7466
c4ed6ebf 7467 folio->memcg_data = 0;
b4e0b68f
MS
7468 obj_cgroup_put(objcg);
7469 } else {
7470 /* LRU pages aren't accounted at the root level */
7471 if (!mem_cgroup_is_root(memcg))
7472 ug->nr_memory += nr_pages;
18b2db3b 7473 ug->pgpgout++;
a9d5adee 7474
c4ed6ebf 7475 folio->memcg_data = 0;
b4e0b68f
MS
7476 }
7477
7478 css_put(&memcg->css);
747db954
JW
7479}
7480
bbc6b703 7481void __mem_cgroup_uncharge(struct folio *folio)
0a31bc97 7482{
a9d5adee
JG
7483 struct uncharge_gather ug;
7484
bbc6b703
MWO
7485 /* Don't touch folio->lru of any random page, pre-check: */
7486 if (!folio_memcg(folio))
0a31bc97
JW
7487 return;
7488
a9d5adee 7489 uncharge_gather_clear(&ug);
bbc6b703 7490 uncharge_folio(folio, &ug);
a9d5adee 7491 uncharge_batch(&ug);
747db954 7492}
0a31bc97 7493
747db954 7494/**
2c8d8f97 7495 * __mem_cgroup_uncharge_list - uncharge a list of page
747db954
JW
7496 * @page_list: list of pages to uncharge
7497 *
7498 * Uncharge a list of pages previously charged with
2c8d8f97 7499 * __mem_cgroup_charge().
747db954 7500 */
2c8d8f97 7501void __mem_cgroup_uncharge_list(struct list_head *page_list)
747db954 7502{
c41a40b6 7503 struct uncharge_gather ug;
c4ed6ebf 7504 struct folio *folio;
c41a40b6 7505
c41a40b6 7506 uncharge_gather_clear(&ug);
c4ed6ebf
MWO
7507 list_for_each_entry(folio, page_list, lru)
7508 uncharge_folio(folio, &ug);
c41a40b6
MS
7509 if (ug.memcg)
7510 uncharge_batch(&ug);
0a31bc97
JW
7511}
7512
7513/**
85ce2c51 7514 * mem_cgroup_replace_folio - Charge a folio's replacement.
d21bba2b
MWO
7515 * @old: Currently circulating folio.
7516 * @new: Replacement folio.
0a31bc97 7517 *
d21bba2b 7518 * Charge @new as a replacement folio for @old. @old will
85ce2c51
NP
7519 * be uncharged upon free. This is only used by the page cache
7520 * (in replace_page_cache_folio()).
0a31bc97 7521 *
d21bba2b 7522 * Both folios must be locked, @new->mapping must be set up.
0a31bc97 7523 */
85ce2c51 7524void mem_cgroup_replace_folio(struct folio *old, struct folio *new)
0a31bc97 7525{
29833315 7526 struct mem_cgroup *memcg;
d21bba2b 7527 long nr_pages = folio_nr_pages(new);
d93c4130 7528 unsigned long flags;
0a31bc97 7529
d21bba2b
MWO
7530 VM_BUG_ON_FOLIO(!folio_test_locked(old), old);
7531 VM_BUG_ON_FOLIO(!folio_test_locked(new), new);
7532 VM_BUG_ON_FOLIO(folio_test_anon(old) != folio_test_anon(new), new);
7533 VM_BUG_ON_FOLIO(folio_nr_pages(old) != nr_pages, new);
0a31bc97
JW
7534
7535 if (mem_cgroup_disabled())
7536 return;
7537
d21bba2b
MWO
7538 /* Page cache replacement: new folio already charged? */
7539 if (folio_memcg(new))
0a31bc97
JW
7540 return;
7541
d21bba2b
MWO
7542 memcg = folio_memcg(old);
7543 VM_WARN_ON_ONCE_FOLIO(!memcg, old);
29833315 7544 if (!memcg)
0a31bc97
JW
7545 return;
7546
44b7a8d3 7547 /* Force-charge the new page. The old one will be freed soon */
8dc87c7d
MS
7548 if (!mem_cgroup_is_root(memcg)) {
7549 page_counter_charge(&memcg->memory, nr_pages);
7550 if (do_memsw_account())
7551 page_counter_charge(&memcg->memsw, nr_pages);
7552 }
0a31bc97 7553
1a3e1f40 7554 css_get(&memcg->css);
d21bba2b 7555 commit_charge(new, memcg);
44b7a8d3 7556
d93c4130 7557 local_irq_save(flags);
6e0110c2 7558 mem_cgroup_charge_statistics(memcg, nr_pages);
d21bba2b 7559 memcg_check_events(memcg, folio_nid(new));
d93c4130 7560 local_irq_restore(flags);
0a31bc97
JW
7561}
7562
85ce2c51
NP
7563/**
7564 * mem_cgroup_migrate - Transfer the memcg data from the old to the new folio.
7565 * @old: Currently circulating folio.
7566 * @new: Replacement folio.
7567 *
7568 * Transfer the memcg data from the old folio to the new folio for migration.
7569 * The old folio's data info will be cleared. Note that the memory counters
7570 * will remain unchanged throughout the process.
7571 *
7572 * Both folios must be locked, @new->mapping must be set up.
7573 */
7574void mem_cgroup_migrate(struct folio *old, struct folio *new)
7575{
7576 struct mem_cgroup *memcg;
7577
7578 VM_BUG_ON_FOLIO(!folio_test_locked(old), old);
7579 VM_BUG_ON_FOLIO(!folio_test_locked(new), new);
7580 VM_BUG_ON_FOLIO(folio_test_anon(old) != folio_test_anon(new), new);
7581 VM_BUG_ON_FOLIO(folio_nr_pages(old) != folio_nr_pages(new), new);
7582
7583 if (mem_cgroup_disabled())
7584 return;
7585
7586 memcg = folio_memcg(old);
8cba9576
NP
7587 /*
7588 * Note that it is normal to see !memcg for a hugetlb folio.
7589 * For e.g, itt could have been allocated when memory_hugetlb_accounting
7590 * was not selected.
7591 */
7592 VM_WARN_ON_ONCE_FOLIO(!folio_test_hugetlb(old) && !memcg, old);
85ce2c51
NP
7593 if (!memcg)
7594 return;
7595
7596 /* Transfer the charge and the css ref */
7597 commit_charge(new, memcg);
9bcef597
BW
7598 /*
7599 * If the old folio is a large folio and is in the split queue, it needs
7600 * to be removed from the split queue now, in case getting an incorrect
7601 * split queue in destroy_large_folio() after the memcg of the old folio
7602 * is cleared.
7603 *
7604 * In addition, the old folio is about to be freed after migration, so
7605 * removing from the split queue a bit earlier seems reasonable.
7606 */
7607 if (folio_test_large(old) && folio_test_large_rmappable(old))
7608 folio_undo_large_rmappable(old);
85ce2c51
NP
7609 old->memcg_data = 0;
7610}
7611
ef12947c 7612DEFINE_STATIC_KEY_FALSE(memcg_sockets_enabled_key);
11092087
JW
7613EXPORT_SYMBOL(memcg_sockets_enabled_key);
7614
2d758073 7615void mem_cgroup_sk_alloc(struct sock *sk)
11092087
JW
7616{
7617 struct mem_cgroup *memcg;
7618
2d758073
JW
7619 if (!mem_cgroup_sockets_enabled)
7620 return;
7621
e876ecc6 7622 /* Do not associate the sock with unrelated interrupted task's memcg. */
086f694a 7623 if (!in_task())
e876ecc6
SB
7624 return;
7625
11092087
JW
7626 rcu_read_lock();
7627 memcg = mem_cgroup_from_task(current);
7848ed62 7628 if (mem_cgroup_is_root(memcg))
f7e1cb6e 7629 goto out;
0db15298 7630 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !memcg->tcpmem_active)
f7e1cb6e 7631 goto out;
8965aa28 7632 if (css_tryget(&memcg->css))
11092087 7633 sk->sk_memcg = memcg;
f7e1cb6e 7634out:
11092087
JW
7635 rcu_read_unlock();
7636}
11092087 7637
2d758073 7638void mem_cgroup_sk_free(struct sock *sk)
11092087 7639{
2d758073
JW
7640 if (sk->sk_memcg)
7641 css_put(&sk->sk_memcg->css);
11092087
JW
7642}
7643
7644/**
7645 * mem_cgroup_charge_skmem - charge socket memory
7646 * @memcg: memcg to charge
7647 * @nr_pages: number of pages to charge
4b1327be 7648 * @gfp_mask: reclaim mode
11092087
JW
7649 *
7650 * Charges @nr_pages to @memcg. Returns %true if the charge fit within
4b1327be 7651 * @memcg's configured limit, %false if it doesn't.
11092087 7652 */
4b1327be
WW
7653bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
7654 gfp_t gfp_mask)
11092087 7655{
f7e1cb6e 7656 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
0db15298 7657 struct page_counter *fail;
f7e1cb6e 7658
0db15298
JW
7659 if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) {
7660 memcg->tcpmem_pressure = 0;
f7e1cb6e
JW
7661 return true;
7662 }
0db15298 7663 memcg->tcpmem_pressure = 1;
4b1327be
WW
7664 if (gfp_mask & __GFP_NOFAIL) {
7665 page_counter_charge(&memcg->tcpmem, nr_pages);
7666 return true;
7667 }
f7e1cb6e 7668 return false;
11092087 7669 }
d886f4e4 7670
4b1327be
WW
7671 if (try_charge(memcg, gfp_mask, nr_pages) == 0) {
7672 mod_memcg_state(memcg, MEMCG_SOCK, nr_pages);
f7e1cb6e 7673 return true;
4b1327be 7674 }
f7e1cb6e 7675
11092087
JW
7676 return false;
7677}
7678
7679/**
7680 * mem_cgroup_uncharge_skmem - uncharge socket memory
b7701a5f
MR
7681 * @memcg: memcg to uncharge
7682 * @nr_pages: number of pages to uncharge
11092087
JW
7683 */
7684void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
7685{
f7e1cb6e 7686 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
0db15298 7687 page_counter_uncharge(&memcg->tcpmem, nr_pages);
f7e1cb6e
JW
7688 return;
7689 }
d886f4e4 7690
c9019e9b 7691 mod_memcg_state(memcg, MEMCG_SOCK, -nr_pages);
b2807f07 7692
475d0487 7693 refill_stock(memcg, nr_pages);
11092087
JW
7694}
7695
f7e1cb6e
JW
7696static int __init cgroup_memory(char *s)
7697{
7698 char *token;
7699
7700 while ((token = strsep(&s, ",")) != NULL) {
7701 if (!*token)
7702 continue;
7703 if (!strcmp(token, "nosocket"))
7704 cgroup_memory_nosocket = true;
04823c83
VD
7705 if (!strcmp(token, "nokmem"))
7706 cgroup_memory_nokmem = true;
b6c1a8af
YS
7707 if (!strcmp(token, "nobpf"))
7708 cgroup_memory_nobpf = true;
f7e1cb6e 7709 }
460a79e1 7710 return 1;
f7e1cb6e
JW
7711}
7712__setup("cgroup.memory=", cgroup_memory);
11092087 7713
2d11085e 7714/*
1081312f
MH
7715 * subsys_initcall() for memory controller.
7716 *
308167fc
SAS
7717 * Some parts like memcg_hotplug_cpu_dead() have to be initialized from this
7718 * context because of lock dependencies (cgroup_lock -> cpu hotplug) but
7719 * basically everything that doesn't depend on a specific mem_cgroup structure
7720 * should be initialized from here.
2d11085e
MH
7721 */
7722static int __init mem_cgroup_init(void)
7723{
95a045f6
JW
7724 int cpu, node;
7725
f3344adf
MS
7726 /*
7727 * Currently s32 type (can refer to struct batched_lruvec_stat) is
7728 * used for per-memcg-per-cpu caching of per-node statistics. In order
7729 * to work fine, we should make sure that the overfill threshold can't
7730 * exceed S32_MAX / PAGE_SIZE.
7731 */
7732 BUILD_BUG_ON(MEMCG_CHARGE_BATCH > S32_MAX / PAGE_SIZE);
7733
308167fc
SAS
7734 cpuhp_setup_state_nocalls(CPUHP_MM_MEMCQ_DEAD, "mm/memctrl:dead", NULL,
7735 memcg_hotplug_cpu_dead);
95a045f6
JW
7736
7737 for_each_possible_cpu(cpu)
7738 INIT_WORK(&per_cpu_ptr(&memcg_stock, cpu)->work,
7739 drain_local_stock);
7740
7741 for_each_node(node) {
7742 struct mem_cgroup_tree_per_node *rtpn;
95a045f6 7743
91f0dcce 7744 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, node);
95a045f6 7745
ef8f2327 7746 rtpn->rb_root = RB_ROOT;
fa90b2fd 7747 rtpn->rb_rightmost = NULL;
ef8f2327 7748 spin_lock_init(&rtpn->lock);
95a045f6
JW
7749 soft_limit_tree.rb_tree_per_node[node] = rtpn;
7750 }
7751
2d11085e
MH
7752 return 0;
7753}
7754subsys_initcall(mem_cgroup_init);
21afa38e 7755
e55b9f96 7756#ifdef CONFIG_SWAP
358c07fc
AB
7757static struct mem_cgroup *mem_cgroup_id_get_online(struct mem_cgroup *memcg)
7758{
1c2d479a 7759 while (!refcount_inc_not_zero(&memcg->id.ref)) {
358c07fc
AB
7760 /*
7761 * The root cgroup cannot be destroyed, so it's refcount must
7762 * always be >= 1.
7763 */
7848ed62 7764 if (WARN_ON_ONCE(mem_cgroup_is_root(memcg))) {
358c07fc
AB
7765 VM_BUG_ON(1);
7766 break;
7767 }
7768 memcg = parent_mem_cgroup(memcg);
7769 if (!memcg)
7770 memcg = root_mem_cgroup;
7771 }
7772 return memcg;
7773}
7774
21afa38e
JW
7775/**
7776 * mem_cgroup_swapout - transfer a memsw charge to swap
3ecb0087 7777 * @folio: folio whose memsw charge to transfer
21afa38e
JW
7778 * @entry: swap entry to move the charge to
7779 *
3ecb0087 7780 * Transfer the memsw charge of @folio to @entry.
21afa38e 7781 */
3ecb0087 7782void mem_cgroup_swapout(struct folio *folio, swp_entry_t entry)
21afa38e 7783{
1f47b61f 7784 struct mem_cgroup *memcg, *swap_memcg;
d6810d73 7785 unsigned int nr_entries;
21afa38e
JW
7786 unsigned short oldid;
7787
3ecb0087
MWO
7788 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
7789 VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
21afa38e 7790
76358ab5
AS
7791 if (mem_cgroup_disabled())
7792 return;
7793
b94c4e94 7794 if (!do_memsw_account())
21afa38e
JW
7795 return;
7796
3ecb0087 7797 memcg = folio_memcg(folio);
21afa38e 7798
3ecb0087 7799 VM_WARN_ON_ONCE_FOLIO(!memcg, folio);
21afa38e
JW
7800 if (!memcg)
7801 return;
7802
1f47b61f
VD
7803 /*
7804 * In case the memcg owning these pages has been offlined and doesn't
7805 * have an ID allocated to it anymore, charge the closest online
7806 * ancestor for the swap instead and transfer the memory+swap charge.
7807 */
7808 swap_memcg = mem_cgroup_id_get_online(memcg);
3ecb0087 7809 nr_entries = folio_nr_pages(folio);
d6810d73
HY
7810 /* Get references for the tail pages, too */
7811 if (nr_entries > 1)
7812 mem_cgroup_id_get_many(swap_memcg, nr_entries - 1);
7813 oldid = swap_cgroup_record(entry, mem_cgroup_id(swap_memcg),
7814 nr_entries);
3ecb0087 7815 VM_BUG_ON_FOLIO(oldid, folio);
c9019e9b 7816 mod_memcg_state(swap_memcg, MEMCG_SWAP, nr_entries);
21afa38e 7817
3ecb0087 7818 folio->memcg_data = 0;
21afa38e
JW
7819
7820 if (!mem_cgroup_is_root(memcg))
d6810d73 7821 page_counter_uncharge(&memcg->memory, nr_entries);
21afa38e 7822
b25806dc 7823 if (memcg != swap_memcg) {
1f47b61f 7824 if (!mem_cgroup_is_root(swap_memcg))
d6810d73
HY
7825 page_counter_charge(&swap_memcg->memsw, nr_entries);
7826 page_counter_uncharge(&memcg->memsw, nr_entries);
1f47b61f
VD
7827 }
7828
ce9ce665
SAS
7829 /*
7830 * Interrupts should be disabled here because the caller holds the
b93b0163 7831 * i_pages lock which is taken with interrupts-off. It is
ce9ce665 7832 * important here to have the interrupts disabled because it is the
b93b0163 7833 * only synchronisation we have for updating the per-CPU variables.
ce9ce665 7834 */
be3e67b5 7835 memcg_stats_lock();
6e0110c2 7836 mem_cgroup_charge_statistics(memcg, -nr_entries);
be3e67b5 7837 memcg_stats_unlock();
3ecb0087 7838 memcg_check_events(memcg, folio_nid(folio));
73f576c0 7839
1a3e1f40 7840 css_put(&memcg->css);
21afa38e
JW
7841}
7842
38d8b4e6 7843/**
e2e3fdc7
MWO
7844 * __mem_cgroup_try_charge_swap - try charging swap space for a folio
7845 * @folio: folio being added to swap
37e84351
VD
7846 * @entry: swap entry to charge
7847 *
e2e3fdc7 7848 * Try to charge @folio's memcg for the swap space at @entry.
37e84351
VD
7849 *
7850 * Returns 0 on success, -ENOMEM on failure.
7851 */
e2e3fdc7 7852int __mem_cgroup_try_charge_swap(struct folio *folio, swp_entry_t entry)
37e84351 7853{
e2e3fdc7 7854 unsigned int nr_pages = folio_nr_pages(folio);
37e84351 7855 struct page_counter *counter;
38d8b4e6 7856 struct mem_cgroup *memcg;
37e84351
VD
7857 unsigned short oldid;
7858
b94c4e94 7859 if (do_memsw_account())
37e84351
VD
7860 return 0;
7861
e2e3fdc7 7862 memcg = folio_memcg(folio);
37e84351 7863
e2e3fdc7 7864 VM_WARN_ON_ONCE_FOLIO(!memcg, folio);
37e84351
VD
7865 if (!memcg)
7866 return 0;
7867
f3a53a3a
TH
7868 if (!entry.val) {
7869 memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
bb98f2c5 7870 return 0;
f3a53a3a 7871 }
bb98f2c5 7872
1f47b61f
VD
7873 memcg = mem_cgroup_id_get_online(memcg);
7874
b25806dc 7875 if (!mem_cgroup_is_root(memcg) &&
38d8b4e6 7876 !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) {
f3a53a3a
TH
7877 memcg_memory_event(memcg, MEMCG_SWAP_MAX);
7878 memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
1f47b61f 7879 mem_cgroup_id_put(memcg);
37e84351 7880 return -ENOMEM;
1f47b61f 7881 }
37e84351 7882
38d8b4e6
HY
7883 /* Get references for the tail pages, too */
7884 if (nr_pages > 1)
7885 mem_cgroup_id_get_many(memcg, nr_pages - 1);
7886 oldid = swap_cgroup_record(entry, mem_cgroup_id(memcg), nr_pages);
e2e3fdc7 7887 VM_BUG_ON_FOLIO(oldid, folio);
c9019e9b 7888 mod_memcg_state(memcg, MEMCG_SWAP, nr_pages);
37e84351 7889
37e84351
VD
7890 return 0;
7891}
7892
21afa38e 7893/**
01c4b28c 7894 * __mem_cgroup_uncharge_swap - uncharge swap space
21afa38e 7895 * @entry: swap entry to uncharge
38d8b4e6 7896 * @nr_pages: the amount of swap space to uncharge
21afa38e 7897 */
01c4b28c 7898void __mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_pages)
21afa38e
JW
7899{
7900 struct mem_cgroup *memcg;
7901 unsigned short id;
7902
38d8b4e6 7903 id = swap_cgroup_record(entry, 0, nr_pages);
21afa38e 7904 rcu_read_lock();
adbe427b 7905 memcg = mem_cgroup_from_id(id);
21afa38e 7906 if (memcg) {
b25806dc 7907 if (!mem_cgroup_is_root(memcg)) {
b94c4e94 7908 if (do_memsw_account())
38d8b4e6 7909 page_counter_uncharge(&memcg->memsw, nr_pages);
b94c4e94
JW
7910 else
7911 page_counter_uncharge(&memcg->swap, nr_pages);
37e84351 7912 }
c9019e9b 7913 mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages);
38d8b4e6 7914 mem_cgroup_id_put_many(memcg, nr_pages);
21afa38e
JW
7915 }
7916 rcu_read_unlock();
7917}
7918
d8b38438
VD
7919long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)
7920{
7921 long nr_swap_pages = get_nr_swap_pages();
7922
b25806dc 7923 if (mem_cgroup_disabled() || do_memsw_account())
d8b38438 7924 return nr_swap_pages;
7848ed62 7925 for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg))
d8b38438 7926 nr_swap_pages = min_t(long, nr_swap_pages,
bbec2e15 7927 READ_ONCE(memcg->swap.max) -
d8b38438
VD
7928 page_counter_read(&memcg->swap));
7929 return nr_swap_pages;
7930}
7931
9202d527 7932bool mem_cgroup_swap_full(struct folio *folio)
5ccc5aba
VD
7933{
7934 struct mem_cgroup *memcg;
7935
9202d527 7936 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
5ccc5aba
VD
7937
7938 if (vm_swap_full())
7939 return true;
b25806dc 7940 if (do_memsw_account())
5ccc5aba
VD
7941 return false;
7942
9202d527 7943 memcg = folio_memcg(folio);
5ccc5aba
VD
7944 if (!memcg)
7945 return false;
7946
7848ed62 7947 for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) {
4b82ab4f
JK
7948 unsigned long usage = page_counter_read(&memcg->swap);
7949
7950 if (usage * 2 >= READ_ONCE(memcg->swap.high) ||
7951 usage * 2 >= READ_ONCE(memcg->swap.max))
5ccc5aba 7952 return true;
4b82ab4f 7953 }
5ccc5aba
VD
7954
7955 return false;
7956}
7957
eccb52e7 7958static int __init setup_swap_account(char *s)
21afa38e 7959{
b25806dc
JW
7960 pr_warn_once("The swapaccount= commandline option is deprecated. "
7961 "Please report your usecase to linux-mm@kvack.org if you "
7962 "depend on this functionality.\n");
21afa38e
JW
7963 return 1;
7964}
eccb52e7 7965__setup("swapaccount=", setup_swap_account);
21afa38e 7966
37e84351
VD
7967static u64 swap_current_read(struct cgroup_subsys_state *css,
7968 struct cftype *cft)
7969{
7970 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
7971
7972 return (u64)page_counter_read(&memcg->swap) * PAGE_SIZE;
7973}
7974
e0e0b412
LD
7975static u64 swap_peak_read(struct cgroup_subsys_state *css,
7976 struct cftype *cft)
7977{
7978 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
7979
7980 return (u64)memcg->swap.watermark * PAGE_SIZE;
7981}
7982
4b82ab4f
JK
7983static int swap_high_show(struct seq_file *m, void *v)
7984{
7985 return seq_puts_memcg_tunable(m,
7986 READ_ONCE(mem_cgroup_from_seq(m)->swap.high));
7987}
7988
7989static ssize_t swap_high_write(struct kernfs_open_file *of,
7990 char *buf, size_t nbytes, loff_t off)
7991{
7992 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
7993 unsigned long high;
7994 int err;
7995
7996 buf = strstrip(buf);
7997 err = page_counter_memparse(buf, "max", &high);
7998 if (err)
7999 return err;
8000
8001 page_counter_set_high(&memcg->swap, high);
8002
8003 return nbytes;
8004}
8005
37e84351
VD
8006static int swap_max_show(struct seq_file *m, void *v)
8007{
677dc973
CD
8008 return seq_puts_memcg_tunable(m,
8009 READ_ONCE(mem_cgroup_from_seq(m)->swap.max));
37e84351
VD
8010}
8011
8012static ssize_t swap_max_write(struct kernfs_open_file *of,
8013 char *buf, size_t nbytes, loff_t off)
8014{
8015 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
8016 unsigned long max;
8017 int err;
8018
8019 buf = strstrip(buf);
8020 err = page_counter_memparse(buf, "max", &max);
8021 if (err)
8022 return err;
8023
be09102b 8024 xchg(&memcg->swap.max, max);
37e84351
VD
8025
8026 return nbytes;
8027}
8028
f3a53a3a
TH
8029static int swap_events_show(struct seq_file *m, void *v)
8030{
aa9694bb 8031 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
f3a53a3a 8032
4b82ab4f
JK
8033 seq_printf(m, "high %lu\n",
8034 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_HIGH]));
f3a53a3a
TH
8035 seq_printf(m, "max %lu\n",
8036 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_MAX]));
8037 seq_printf(m, "fail %lu\n",
8038 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_FAIL]));
8039
8040 return 0;
8041}
8042
37e84351
VD
8043static struct cftype swap_files[] = {
8044 {
8045 .name = "swap.current",
8046 .flags = CFTYPE_NOT_ON_ROOT,
8047 .read_u64 = swap_current_read,
8048 },
4b82ab4f
JK
8049 {
8050 .name = "swap.high",
8051 .flags = CFTYPE_NOT_ON_ROOT,
8052 .seq_show = swap_high_show,
8053 .write = swap_high_write,
8054 },
37e84351
VD
8055 {
8056 .name = "swap.max",
8057 .flags = CFTYPE_NOT_ON_ROOT,
8058 .seq_show = swap_max_show,
8059 .write = swap_max_write,
8060 },
e0e0b412
LD
8061 {
8062 .name = "swap.peak",
8063 .flags = CFTYPE_NOT_ON_ROOT,
8064 .read_u64 = swap_peak_read,
8065 },
f3a53a3a
TH
8066 {
8067 .name = "swap.events",
8068 .flags = CFTYPE_NOT_ON_ROOT,
8069 .file_offset = offsetof(struct mem_cgroup, swap_events_file),
8070 .seq_show = swap_events_show,
8071 },
37e84351
VD
8072 { } /* terminate */
8073};
8074
eccb52e7 8075static struct cftype memsw_files[] = {
21afa38e
JW
8076 {
8077 .name = "memsw.usage_in_bytes",
8078 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
8079 .read_u64 = mem_cgroup_read_u64,
8080 },
8081 {
8082 .name = "memsw.max_usage_in_bytes",
8083 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
8084 .write = mem_cgroup_reset,
8085 .read_u64 = mem_cgroup_read_u64,
8086 },
8087 {
8088 .name = "memsw.limit_in_bytes",
8089 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
8090 .write = mem_cgroup_write,
8091 .read_u64 = mem_cgroup_read_u64,
8092 },
8093 {
8094 .name = "memsw.failcnt",
8095 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
8096 .write = mem_cgroup_reset,
8097 .read_u64 = mem_cgroup_read_u64,
8098 },
8099 { }, /* terminate */
8100};
8101
f4840ccf
JW
8102#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
8103/**
8104 * obj_cgroup_may_zswap - check if this cgroup can zswap
8105 * @objcg: the object cgroup
8106 *
8107 * Check if the hierarchical zswap limit has been reached.
8108 *
8109 * This doesn't check for specific headroom, and it is not atomic
8110 * either. But with zswap, the size of the allocation is only known
be16dd76 8111 * once compression has occurred, and this optimistic pre-check avoids
f4840ccf
JW
8112 * spending cycles on compression when there is already no room left
8113 * or zswap is disabled altogether somewhere in the hierarchy.
8114 */
8115bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
8116{
8117 struct mem_cgroup *memcg, *original_memcg;
8118 bool ret = true;
8119
8120 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
8121 return true;
8122
8123 original_memcg = get_mem_cgroup_from_objcg(objcg);
7848ed62 8124 for (memcg = original_memcg; !mem_cgroup_is_root(memcg);
f4840ccf
JW
8125 memcg = parent_mem_cgroup(memcg)) {
8126 unsigned long max = READ_ONCE(memcg->zswap_max);
8127 unsigned long pages;
8128
8129 if (max == PAGE_COUNTER_MAX)
8130 continue;
8131 if (max == 0) {
8132 ret = false;
8133 break;
8134 }
8135
7d7ef0a4
YA
8136 /*
8137 * mem_cgroup_flush_stats() ignores small changes. Use
8138 * do_flush_stats() directly to get accurate stats for charging.
8139 */
8140 do_flush_stats(memcg);
f4840ccf
JW
8141 pages = memcg_page_state(memcg, MEMCG_ZSWAP_B) / PAGE_SIZE;
8142 if (pages < max)
8143 continue;
8144 ret = false;
8145 break;
8146 }
8147 mem_cgroup_put(original_memcg);
8148 return ret;
8149}
8150
8151/**
8152 * obj_cgroup_charge_zswap - charge compression backend memory
8153 * @objcg: the object cgroup
8154 * @size: size of compressed object
8155 *
3a1060c2 8156 * This forces the charge after obj_cgroup_may_zswap() allowed
f4840ccf
JW
8157 * compression and storage in zwap for this cgroup to go ahead.
8158 */
8159void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size)
8160{
8161 struct mem_cgroup *memcg;
8162
8163 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
8164 return;
8165
8166 VM_WARN_ON_ONCE(!(current->flags & PF_MEMALLOC));
8167
8168 /* PF_MEMALLOC context, charging must succeed */
8169 if (obj_cgroup_charge(objcg, GFP_KERNEL, size))
8170 VM_WARN_ON_ONCE(1);
8171
8172 rcu_read_lock();
8173 memcg = obj_cgroup_memcg(objcg);
8174 mod_memcg_state(memcg, MEMCG_ZSWAP_B, size);
8175 mod_memcg_state(memcg, MEMCG_ZSWAPPED, 1);
8176 rcu_read_unlock();
8177}
8178
8179/**
8180 * obj_cgroup_uncharge_zswap - uncharge compression backend memory
8181 * @objcg: the object cgroup
8182 * @size: size of compressed object
8183 *
8184 * Uncharges zswap memory on page in.
8185 */
8186void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size)
8187{
8188 struct mem_cgroup *memcg;
8189
8190 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
8191 return;
8192
8193 obj_cgroup_uncharge(objcg, size);
8194
8195 rcu_read_lock();
8196 memcg = obj_cgroup_memcg(objcg);
8197 mod_memcg_state(memcg, MEMCG_ZSWAP_B, -size);
8198 mod_memcg_state(memcg, MEMCG_ZSWAPPED, -1);
8199 rcu_read_unlock();
8200}
8201
501a06fe
NP
8202bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg)
8203{
8204 /* if zswap is disabled, do not block pages going to the swapping device */
8205 return !is_zswap_enabled() || !memcg || READ_ONCE(memcg->zswap_writeback);
8206}
8207
f4840ccf
JW
8208static u64 zswap_current_read(struct cgroup_subsys_state *css,
8209 struct cftype *cft)
8210{
7d7ef0a4
YA
8211 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
8212
8213 mem_cgroup_flush_stats(memcg);
8214 return memcg_page_state(memcg, MEMCG_ZSWAP_B);
f4840ccf
JW
8215}
8216
8217static int zswap_max_show(struct seq_file *m, void *v)
8218{
8219 return seq_puts_memcg_tunable(m,
8220 READ_ONCE(mem_cgroup_from_seq(m)->zswap_max));
8221}
8222
8223static ssize_t zswap_max_write(struct kernfs_open_file *of,
8224 char *buf, size_t nbytes, loff_t off)
8225{
8226 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
8227 unsigned long max;
8228 int err;
8229
8230 buf = strstrip(buf);
8231 err = page_counter_memparse(buf, "max", &max);
8232 if (err)
8233 return err;
8234
8235 xchg(&memcg->zswap_max, max);
8236
8237 return nbytes;
8238}
8239
501a06fe
NP
8240static int zswap_writeback_show(struct seq_file *m, void *v)
8241{
8242 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
8243
8244 seq_printf(m, "%d\n", READ_ONCE(memcg->zswap_writeback));
8245 return 0;
8246}
8247
8248static ssize_t zswap_writeback_write(struct kernfs_open_file *of,
8249 char *buf, size_t nbytes, loff_t off)
8250{
8251 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
8252 int zswap_writeback;
8253 ssize_t parse_ret = kstrtoint(strstrip(buf), 0, &zswap_writeback);
8254
8255 if (parse_ret)
8256 return parse_ret;
8257
8258 if (zswap_writeback != 0 && zswap_writeback != 1)
8259 return -EINVAL;
8260
8261 WRITE_ONCE(memcg->zswap_writeback, zswap_writeback);
8262 return nbytes;
8263}
8264
f4840ccf
JW
8265static struct cftype zswap_files[] = {
8266 {
8267 .name = "zswap.current",
8268 .flags = CFTYPE_NOT_ON_ROOT,
8269 .read_u64 = zswap_current_read,
8270 },
8271 {
8272 .name = "zswap.max",
8273 .flags = CFTYPE_NOT_ON_ROOT,
8274 .seq_show = zswap_max_show,
8275 .write = zswap_max_write,
8276 },
501a06fe
NP
8277 {
8278 .name = "zswap.writeback",
8279 .seq_show = zswap_writeback_show,
8280 .write = zswap_writeback_write,
8281 },
f4840ccf
JW
8282 { } /* terminate */
8283};
8284#endif /* CONFIG_MEMCG_KMEM && CONFIG_ZSWAP */
8285
21afa38e
JW
8286static int __init mem_cgroup_swap_init(void)
8287{
2d1c4980 8288 if (mem_cgroup_disabled())
eccb52e7
JW
8289 return 0;
8290
8291 WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, swap_files));
8292 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, memsw_files));
f4840ccf
JW
8293#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
8294 WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, zswap_files));
8295#endif
21afa38e
JW
8296 return 0;
8297}
b25806dc 8298subsys_initcall(mem_cgroup_swap_init);
21afa38e 8299
e55b9f96 8300#endif /* CONFIG_SWAP */