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