xref: /linux/mm/memcontrol.c (revision a9ce09b15761e0f3a413a4a79097d17e19bd3ec1)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* memcontrol.c - Memory Controller
3  *
4  * Copyright IBM Corporation, 2007
5  * Author Balbir Singh <balbir@linux.vnet.ibm.com>
6  *
7  * Copyright 2007 OpenVZ SWsoft Inc
8  * Author: Pavel Emelianov <xemul@openvz.org>
9  *
10  * Memory thresholds
11  * Copyright (C) 2009 Nokia Corporation
12  * Author: Kirill A. Shutemov
13  *
14  * Kernel Memory Controller
15  * Copyright (C) 2012 Parallels Inc. and Google Inc.
16  * Authors: Glauber Costa and Suleiman Souhlal
17  *
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
23  *
24  * Per memcg lru locking
25  * Copyright (C) 2020 Alibaba, Inc, Alex Shi
26  */
27 
28 #include <linux/cgroup-defs.h>
29 #include <linux/page_counter.h>
30 #include <linux/memcontrol.h>
31 #include <linux/cgroup.h>
32 #include <linux/cpuset.h>
33 #include <linux/sched/mm.h>
34 #include <linux/shmem_fs.h>
35 #include <linux/hugetlb.h>
36 #include <linux/pagemap.h>
37 #include <linux/pagevec.h>
38 #include <linux/vm_event_item.h>
39 #include <linux/smp.h>
40 #include <linux/page-flags.h>
41 #include <linux/backing-dev.h>
42 #include <linux/bit_spinlock.h>
43 #include <linux/rcupdate.h>
44 #include <linux/limits.h>
45 #include <linux/export.h>
46 #include <linux/list.h>
47 #include <linux/mutex.h>
48 #include <linux/rbtree.h>
49 #include <linux/slab.h>
50 #include <linux/swapops.h>
51 #include <linux/spinlock.h>
52 #include <linux/fs.h>
53 #include <linux/seq_file.h>
54 #include <linux/vmpressure.h>
55 #include <linux/memremap.h>
56 #include <linux/mm_inline.h>
57 #include <linux/swap_cgroup.h>
58 #include <linux/cpu.h>
59 #include <linux/oom.h>
60 #include <linux/lockdep.h>
61 #include <linux/resume_user_mode.h>
62 #include <linux/psi.h>
63 #include <linux/seq_buf.h>
64 #include <linux/sched/isolation.h>
65 #include <linux/kmemleak.h>
66 #include "internal.h"
67 #include <net/sock.h>
68 #include <net/ip.h>
69 #include "slab.h"
70 #include "memcontrol-v1.h"
71 
72 #include <linux/uaccess.h>
73 
74 #define CREATE_TRACE_POINTS
75 #include <trace/events/memcg.h>
76 #undef CREATE_TRACE_POINTS
77 
78 #include <trace/events/vmscan.h>
79 
80 struct cgroup_subsys memory_cgrp_subsys __read_mostly;
81 EXPORT_SYMBOL(memory_cgrp_subsys);
82 
83 struct mem_cgroup *root_mem_cgroup __read_mostly;
84 EXPORT_SYMBOL(root_mem_cgroup);
85 
86 /* Active memory cgroup to use from an interrupt context */
87 DEFINE_PER_CPU(struct mem_cgroup *, int_active_memcg);
88 EXPORT_PER_CPU_SYMBOL_GPL(int_active_memcg);
89 
90 /* Socket memory accounting disabled? */
91 static bool cgroup_memory_nosocket __ro_after_init;
92 
93 /* Kernel memory accounting disabled? */
94 static bool cgroup_memory_nokmem __ro_after_init;
95 
96 /* BPF memory accounting disabled? */
97 static bool cgroup_memory_nobpf __ro_after_init;
98 
99 static struct kmem_cache *memcg_cachep;
100 static struct kmem_cache *memcg_pn_cachep;
101 
102 #ifdef CONFIG_CGROUP_WRITEBACK
103 static DECLARE_WAIT_QUEUE_HEAD(memcg_cgwb_frn_waitq);
104 #endif
105 
106 static inline bool task_is_dying(void)
107 {
108 	return tsk_is_oom_victim(current) || fatal_signal_pending(current) ||
109 		(current->flags & PF_EXITING);
110 }
111 
112 /* Some nice accessors for the vmpressure. */
113 struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
114 {
115 	if (!memcg)
116 		memcg = root_mem_cgroup;
117 	return &memcg->vmpressure;
118 }
119 
120 struct mem_cgroup *vmpressure_to_memcg(struct vmpressure *vmpr)
121 {
122 	return container_of(vmpr, struct mem_cgroup, vmpressure);
123 }
124 
125 #define SEQ_BUF_SIZE SZ_4K
126 #define CURRENT_OBJCG_UPDATE_BIT 0
127 #define CURRENT_OBJCG_UPDATE_FLAG (1UL << CURRENT_OBJCG_UPDATE_BIT)
128 
129 static DEFINE_SPINLOCK(objcg_lock);
130 
131 bool mem_cgroup_kmem_disabled(void)
132 {
133 	return cgroup_memory_nokmem;
134 }
135 
136 static void memcg_uncharge(struct mem_cgroup *memcg, unsigned int nr_pages);
137 
138 static void obj_cgroup_release(struct percpu_ref *ref)
139 {
140 	struct obj_cgroup *objcg = container_of(ref, struct obj_cgroup, refcnt);
141 	unsigned int nr_bytes;
142 	unsigned int nr_pages;
143 	unsigned long flags;
144 
145 	/*
146 	 * At this point all allocated objects are freed, and
147 	 * objcg->nr_charged_bytes can't have an arbitrary byte value.
148 	 * However, it can be PAGE_SIZE or (x * PAGE_SIZE).
149 	 *
150 	 * The following sequence can lead to it:
151 	 * 1) CPU0: objcg == stock->cached_objcg
152 	 * 2) CPU1: we do a small allocation (e.g. 92 bytes),
153 	 *          PAGE_SIZE bytes are charged
154 	 * 3) CPU1: a process from another memcg is allocating something,
155 	 *          the stock if flushed,
156 	 *          objcg->nr_charged_bytes = PAGE_SIZE - 92
157 	 * 5) CPU0: we do release this object,
158 	 *          92 bytes are added to stock->nr_bytes
159 	 * 6) CPU0: stock is flushed,
160 	 *          92 bytes are added to objcg->nr_charged_bytes
161 	 *
162 	 * In the result, nr_charged_bytes == PAGE_SIZE.
163 	 * This page will be uncharged in obj_cgroup_release().
164 	 */
165 	nr_bytes = atomic_read(&objcg->nr_charged_bytes);
166 	WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1));
167 	nr_pages = nr_bytes >> PAGE_SHIFT;
168 
169 	if (nr_pages) {
170 		struct mem_cgroup *memcg;
171 
172 		memcg = get_mem_cgroup_from_objcg(objcg);
173 		mod_memcg_state(memcg, MEMCG_KMEM, -nr_pages);
174 		memcg1_account_kmem(memcg, -nr_pages);
175 		if (!mem_cgroup_is_root(memcg))
176 			memcg_uncharge(memcg, nr_pages);
177 		mem_cgroup_put(memcg);
178 	}
179 
180 	spin_lock_irqsave(&objcg_lock, flags);
181 	list_del(&objcg->list);
182 	spin_unlock_irqrestore(&objcg_lock, flags);
183 
184 	percpu_ref_exit(ref);
185 	kfree_rcu(objcg, rcu);
186 }
187 
188 static struct obj_cgroup *obj_cgroup_alloc(void)
189 {
190 	struct obj_cgroup *objcg;
191 	int ret;
192 
193 	objcg = kzalloc(sizeof(struct obj_cgroup), GFP_KERNEL);
194 	if (!objcg)
195 		return NULL;
196 
197 	ret = percpu_ref_init(&objcg->refcnt, obj_cgroup_release, 0,
198 			      GFP_KERNEL);
199 	if (ret) {
200 		kfree(objcg);
201 		return NULL;
202 	}
203 	INIT_LIST_HEAD(&objcg->list);
204 	return objcg;
205 }
206 
207 static void memcg_reparent_objcgs(struct mem_cgroup *memcg,
208 				  struct mem_cgroup *parent)
209 {
210 	struct obj_cgroup *objcg, *iter;
211 
212 	objcg = rcu_replace_pointer(memcg->objcg, NULL, true);
213 
214 	spin_lock_irq(&objcg_lock);
215 
216 	/* 1) Ready to reparent active objcg. */
217 	list_add(&objcg->list, &memcg->objcg_list);
218 	/* 2) Reparent active objcg and already reparented objcgs to parent. */
219 	list_for_each_entry(iter, &memcg->objcg_list, list)
220 		WRITE_ONCE(iter->memcg, parent);
221 	/* 3) Move already reparented objcgs to the parent's list */
222 	list_splice(&memcg->objcg_list, &parent->objcg_list);
223 
224 	spin_unlock_irq(&objcg_lock);
225 
226 	percpu_ref_kill(&objcg->refcnt);
227 }
228 
229 /*
230  * A lot of the calls to the cache allocation functions are expected to be
231  * inlined by the compiler. Since the calls to memcg_slab_post_alloc_hook() are
232  * conditional to this static branch, we'll have to allow modules that does
233  * kmem_cache_alloc and the such to see this symbol as well
234  */
235 DEFINE_STATIC_KEY_FALSE(memcg_kmem_online_key);
236 EXPORT_SYMBOL(memcg_kmem_online_key);
237 
238 DEFINE_STATIC_KEY_FALSE(memcg_bpf_enabled_key);
239 EXPORT_SYMBOL(memcg_bpf_enabled_key);
240 
241 /**
242  * mem_cgroup_css_from_folio - css of the memcg associated with a folio
243  * @folio: folio of interest
244  *
245  * If memcg is bound to the default hierarchy, css of the memcg associated
246  * with @folio is returned.  The returned css remains associated with @folio
247  * until it is released.
248  *
249  * If memcg is bound to a traditional hierarchy, the css of root_mem_cgroup
250  * is returned.
251  */
252 struct cgroup_subsys_state *mem_cgroup_css_from_folio(struct folio *folio)
253 {
254 	struct mem_cgroup *memcg = folio_memcg(folio);
255 
256 	if (!memcg || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
257 		memcg = root_mem_cgroup;
258 
259 	return &memcg->css;
260 }
261 
262 /**
263  * page_cgroup_ino - return inode number of the memcg a page is charged to
264  * @page: the page
265  *
266  * Look up the closest online ancestor of the memory cgroup @page is charged to
267  * and return its inode number or 0 if @page is not charged to any cgroup. It
268  * is safe to call this function without holding a reference to @page.
269  *
270  * Note, this function is inherently racy, because there is nothing to prevent
271  * the cgroup inode from getting torn down and potentially reallocated a moment
272  * after page_cgroup_ino() returns, so it only should be used by callers that
273  * do not care (such as procfs interfaces).
274  */
275 ino_t page_cgroup_ino(struct page *page)
276 {
277 	struct mem_cgroup *memcg;
278 	unsigned long ino = 0;
279 
280 	rcu_read_lock();
281 	/* page_folio() is racy here, but the entire function is racy anyway */
282 	memcg = folio_memcg_check(page_folio(page));
283 
284 	while (memcg && !(memcg->css.flags & CSS_ONLINE))
285 		memcg = parent_mem_cgroup(memcg);
286 	if (memcg)
287 		ino = cgroup_ino(memcg->css.cgroup);
288 	rcu_read_unlock();
289 	return ino;
290 }
291 EXPORT_SYMBOL_GPL(page_cgroup_ino);
292 
293 /* Subset of node_stat_item for memcg stats */
294 static const unsigned int memcg_node_stat_items[] = {
295 	NR_INACTIVE_ANON,
296 	NR_ACTIVE_ANON,
297 	NR_INACTIVE_FILE,
298 	NR_ACTIVE_FILE,
299 	NR_UNEVICTABLE,
300 	NR_SLAB_RECLAIMABLE_B,
301 	NR_SLAB_UNRECLAIMABLE_B,
302 	WORKINGSET_REFAULT_ANON,
303 	WORKINGSET_REFAULT_FILE,
304 	WORKINGSET_ACTIVATE_ANON,
305 	WORKINGSET_ACTIVATE_FILE,
306 	WORKINGSET_RESTORE_ANON,
307 	WORKINGSET_RESTORE_FILE,
308 	WORKINGSET_NODERECLAIM,
309 	NR_ANON_MAPPED,
310 	NR_FILE_MAPPED,
311 	NR_FILE_PAGES,
312 	NR_FILE_DIRTY,
313 	NR_WRITEBACK,
314 	NR_SHMEM,
315 	NR_SHMEM_THPS,
316 	NR_FILE_THPS,
317 	NR_ANON_THPS,
318 	NR_KERNEL_STACK_KB,
319 	NR_PAGETABLE,
320 	NR_SECONDARY_PAGETABLE,
321 #ifdef CONFIG_SWAP
322 	NR_SWAPCACHE,
323 #endif
324 #ifdef CONFIG_NUMA_BALANCING
325 	PGPROMOTE_SUCCESS,
326 #endif
327 	PGDEMOTE_KSWAPD,
328 	PGDEMOTE_DIRECT,
329 	PGDEMOTE_KHUGEPAGED,
330 	PGDEMOTE_PROACTIVE,
331 #ifdef CONFIG_HUGETLB_PAGE
332 	NR_HUGETLB,
333 #endif
334 };
335 
336 static const unsigned int memcg_stat_items[] = {
337 	MEMCG_SWAP,
338 	MEMCG_SOCK,
339 	MEMCG_PERCPU_B,
340 	MEMCG_VMALLOC,
341 	MEMCG_KMEM,
342 	MEMCG_ZSWAP_B,
343 	MEMCG_ZSWAPPED,
344 };
345 
346 #define NR_MEMCG_NODE_STAT_ITEMS ARRAY_SIZE(memcg_node_stat_items)
347 #define MEMCG_VMSTAT_SIZE (NR_MEMCG_NODE_STAT_ITEMS + \
348 			   ARRAY_SIZE(memcg_stat_items))
349 #define BAD_STAT_IDX(index) ((u32)(index) >= U8_MAX)
350 static u8 mem_cgroup_stats_index[MEMCG_NR_STAT] __read_mostly;
351 
352 static void init_memcg_stats(void)
353 {
354 	u8 i, j = 0;
355 
356 	BUILD_BUG_ON(MEMCG_NR_STAT >= U8_MAX);
357 
358 	memset(mem_cgroup_stats_index, U8_MAX, sizeof(mem_cgroup_stats_index));
359 
360 	for (i = 0; i < NR_MEMCG_NODE_STAT_ITEMS; ++i, ++j)
361 		mem_cgroup_stats_index[memcg_node_stat_items[i]] = j;
362 
363 	for (i = 0; i < ARRAY_SIZE(memcg_stat_items); ++i, ++j)
364 		mem_cgroup_stats_index[memcg_stat_items[i]] = j;
365 }
366 
367 static inline int memcg_stats_index(int idx)
368 {
369 	return mem_cgroup_stats_index[idx];
370 }
371 
372 struct lruvec_stats_percpu {
373 	/* Local (CPU and cgroup) state */
374 	long state[NR_MEMCG_NODE_STAT_ITEMS];
375 
376 	/* Delta calculation for lockless upward propagation */
377 	long state_prev[NR_MEMCG_NODE_STAT_ITEMS];
378 };
379 
380 struct lruvec_stats {
381 	/* Aggregated (CPU and subtree) state */
382 	long state[NR_MEMCG_NODE_STAT_ITEMS];
383 
384 	/* Non-hierarchical (CPU aggregated) state */
385 	long state_local[NR_MEMCG_NODE_STAT_ITEMS];
386 
387 	/* Pending child counts during tree propagation */
388 	long state_pending[NR_MEMCG_NODE_STAT_ITEMS];
389 };
390 
391 unsigned long lruvec_page_state(struct lruvec *lruvec, enum node_stat_item idx)
392 {
393 	struct mem_cgroup_per_node *pn;
394 	long x;
395 	int i;
396 
397 	if (mem_cgroup_disabled())
398 		return node_page_state(lruvec_pgdat(lruvec), idx);
399 
400 	i = memcg_stats_index(idx);
401 	if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx))
402 		return 0;
403 
404 	pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
405 	x = READ_ONCE(pn->lruvec_stats->state[i]);
406 #ifdef CONFIG_SMP
407 	if (x < 0)
408 		x = 0;
409 #endif
410 	return x;
411 }
412 
413 unsigned long lruvec_page_state_local(struct lruvec *lruvec,
414 				      enum node_stat_item idx)
415 {
416 	struct mem_cgroup_per_node *pn;
417 	long x;
418 	int i;
419 
420 	if (mem_cgroup_disabled())
421 		return node_page_state(lruvec_pgdat(lruvec), idx);
422 
423 	i = memcg_stats_index(idx);
424 	if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx))
425 		return 0;
426 
427 	pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
428 	x = READ_ONCE(pn->lruvec_stats->state_local[i]);
429 #ifdef CONFIG_SMP
430 	if (x < 0)
431 		x = 0;
432 #endif
433 	return x;
434 }
435 
436 /* Subset of vm_event_item to report for memcg event stats */
437 static const unsigned int memcg_vm_event_stat[] = {
438 #ifdef CONFIG_MEMCG_V1
439 	PGPGIN,
440 	PGPGOUT,
441 #endif
442 	PSWPIN,
443 	PSWPOUT,
444 	PGSCAN_KSWAPD,
445 	PGSCAN_DIRECT,
446 	PGSCAN_KHUGEPAGED,
447 	PGSCAN_PROACTIVE,
448 	PGSTEAL_KSWAPD,
449 	PGSTEAL_DIRECT,
450 	PGSTEAL_KHUGEPAGED,
451 	PGSTEAL_PROACTIVE,
452 	PGFAULT,
453 	PGMAJFAULT,
454 	PGREFILL,
455 	PGACTIVATE,
456 	PGDEACTIVATE,
457 	PGLAZYFREE,
458 	PGLAZYFREED,
459 #ifdef CONFIG_SWAP
460 	SWPIN_ZERO,
461 	SWPOUT_ZERO,
462 #endif
463 #ifdef CONFIG_ZSWAP
464 	ZSWPIN,
465 	ZSWPOUT,
466 	ZSWPWB,
467 #endif
468 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
469 	THP_FAULT_ALLOC,
470 	THP_COLLAPSE_ALLOC,
471 	THP_SWPOUT,
472 	THP_SWPOUT_FALLBACK,
473 #endif
474 #ifdef CONFIG_NUMA_BALANCING
475 	NUMA_PAGE_MIGRATE,
476 	NUMA_PTE_UPDATES,
477 	NUMA_HINT_FAULTS,
478 #endif
479 };
480 
481 #define NR_MEMCG_EVENTS ARRAY_SIZE(memcg_vm_event_stat)
482 static u8 mem_cgroup_events_index[NR_VM_EVENT_ITEMS] __read_mostly;
483 
484 static void init_memcg_events(void)
485 {
486 	u8 i;
487 
488 	BUILD_BUG_ON(NR_VM_EVENT_ITEMS >= U8_MAX);
489 
490 	memset(mem_cgroup_events_index, U8_MAX,
491 	       sizeof(mem_cgroup_events_index));
492 
493 	for (i = 0; i < NR_MEMCG_EVENTS; ++i)
494 		mem_cgroup_events_index[memcg_vm_event_stat[i]] = i;
495 }
496 
497 static inline int memcg_events_index(enum vm_event_item idx)
498 {
499 	return mem_cgroup_events_index[idx];
500 }
501 
502 struct memcg_vmstats_percpu {
503 	/* Stats updates since the last flush */
504 	unsigned int			stats_updates;
505 
506 	/* Cached pointers for fast iteration in memcg_rstat_updated() */
507 	struct memcg_vmstats_percpu __percpu	*parent_pcpu;
508 	struct memcg_vmstats			*vmstats;
509 
510 	/* The above should fit a single cacheline for memcg_rstat_updated() */
511 
512 	/* Local (CPU and cgroup) page state & events */
513 	long			state[MEMCG_VMSTAT_SIZE];
514 	unsigned long		events[NR_MEMCG_EVENTS];
515 
516 	/* Delta calculation for lockless upward propagation */
517 	long			state_prev[MEMCG_VMSTAT_SIZE];
518 	unsigned long		events_prev[NR_MEMCG_EVENTS];
519 } ____cacheline_aligned;
520 
521 struct memcg_vmstats {
522 	/* Aggregated (CPU and subtree) page state & events */
523 	long			state[MEMCG_VMSTAT_SIZE];
524 	unsigned long		events[NR_MEMCG_EVENTS];
525 
526 	/* Non-hierarchical (CPU aggregated) page state & events */
527 	long			state_local[MEMCG_VMSTAT_SIZE];
528 	unsigned long		events_local[NR_MEMCG_EVENTS];
529 
530 	/* Pending child counts during tree propagation */
531 	long			state_pending[MEMCG_VMSTAT_SIZE];
532 	unsigned long		events_pending[NR_MEMCG_EVENTS];
533 
534 	/* Stats updates since the last flush */
535 	atomic_t		stats_updates;
536 };
537 
538 /*
539  * memcg and lruvec stats flushing
540  *
541  * Many codepaths leading to stats update or read are performance sensitive and
542  * adding stats flushing in such codepaths is not desirable. So, to optimize the
543  * flushing the kernel does:
544  *
545  * 1) Periodically and asynchronously flush the stats every 2 seconds to not let
546  *    rstat update tree grow unbounded.
547  *
548  * 2) Flush the stats synchronously on reader side only when there are more than
549  *    (MEMCG_CHARGE_BATCH * nr_cpus) update events. Though this optimization
550  *    will let stats be out of sync by atmost (MEMCG_CHARGE_BATCH * nr_cpus) but
551  *    only for 2 seconds due to (1).
552  */
553 static void flush_memcg_stats_dwork(struct work_struct *w);
554 static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork);
555 static u64 flush_last_time;
556 
557 #define FLUSH_TIME (2UL*HZ)
558 
559 static bool memcg_vmstats_needs_flush(struct memcg_vmstats *vmstats)
560 {
561 	return atomic_read(&vmstats->stats_updates) >
562 		MEMCG_CHARGE_BATCH * num_online_cpus();
563 }
564 
565 static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val,
566 				       int cpu)
567 {
568 	struct memcg_vmstats_percpu __percpu *statc_pcpu;
569 	struct memcg_vmstats_percpu *statc;
570 	unsigned int stats_updates;
571 
572 	if (!val)
573 		return;
574 
575 	css_rstat_updated(&memcg->css, cpu);
576 	statc_pcpu = memcg->vmstats_percpu;
577 	for (; statc_pcpu; statc_pcpu = statc->parent_pcpu) {
578 		statc = this_cpu_ptr(statc_pcpu);
579 		/*
580 		 * If @memcg is already flushable then all its ancestors are
581 		 * flushable as well and also there is no need to increase
582 		 * stats_updates.
583 		 */
584 		if (memcg_vmstats_needs_flush(statc->vmstats))
585 			break;
586 
587 		stats_updates = this_cpu_add_return(statc_pcpu->stats_updates,
588 						    abs(val));
589 		if (stats_updates < MEMCG_CHARGE_BATCH)
590 			continue;
591 
592 		stats_updates = this_cpu_xchg(statc_pcpu->stats_updates, 0);
593 		atomic_add(stats_updates, &statc->vmstats->stats_updates);
594 	}
595 }
596 
597 static void __mem_cgroup_flush_stats(struct mem_cgroup *memcg, bool force)
598 {
599 	bool needs_flush = memcg_vmstats_needs_flush(memcg->vmstats);
600 
601 	trace_memcg_flush_stats(memcg, atomic_read(&memcg->vmstats->stats_updates),
602 		force, needs_flush);
603 
604 	if (!force && !needs_flush)
605 		return;
606 
607 	if (mem_cgroup_is_root(memcg))
608 		WRITE_ONCE(flush_last_time, jiffies_64);
609 
610 	css_rstat_flush(&memcg->css);
611 }
612 
613 /*
614  * mem_cgroup_flush_stats - flush the stats of a memory cgroup subtree
615  * @memcg: root of the subtree to flush
616  *
617  * Flushing is serialized by the underlying global rstat lock. There is also a
618  * minimum amount of work to be done even if there are no stat updates to flush.
619  * Hence, we only flush the stats if the updates delta exceeds a threshold. This
620  * avoids unnecessary work and contention on the underlying lock.
621  */
622 void mem_cgroup_flush_stats(struct mem_cgroup *memcg)
623 {
624 	if (mem_cgroup_disabled())
625 		return;
626 
627 	if (!memcg)
628 		memcg = root_mem_cgroup;
629 
630 	__mem_cgroup_flush_stats(memcg, false);
631 }
632 
633 void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg)
634 {
635 	/* Only flush if the periodic flusher is one full cycle late */
636 	if (time_after64(jiffies_64, READ_ONCE(flush_last_time) + 2*FLUSH_TIME))
637 		mem_cgroup_flush_stats(memcg);
638 }
639 
640 static void flush_memcg_stats_dwork(struct work_struct *w)
641 {
642 	/*
643 	 * Deliberately ignore memcg_vmstats_needs_flush() here so that flushing
644 	 * in latency-sensitive paths is as cheap as possible.
645 	 */
646 	__mem_cgroup_flush_stats(root_mem_cgroup, true);
647 	queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME);
648 }
649 
650 unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
651 {
652 	long x;
653 	int i = memcg_stats_index(idx);
654 
655 	if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx))
656 		return 0;
657 
658 	x = READ_ONCE(memcg->vmstats->state[i]);
659 #ifdef CONFIG_SMP
660 	if (x < 0)
661 		x = 0;
662 #endif
663 	return x;
664 }
665 
666 static int memcg_page_state_unit(int item);
667 
668 /*
669  * Normalize the value passed into memcg_rstat_updated() to be in pages. Round
670  * up non-zero sub-page updates to 1 page as zero page updates are ignored.
671  */
672 static int memcg_state_val_in_pages(int idx, int val)
673 {
674 	int unit = memcg_page_state_unit(idx);
675 
676 	if (!val || unit == PAGE_SIZE)
677 		return val;
678 	else
679 		return max(val * unit / PAGE_SIZE, 1UL);
680 }
681 
682 /**
683  * mod_memcg_state - update cgroup memory statistics
684  * @memcg: the memory cgroup
685  * @idx: the stat item - can be enum memcg_stat_item or enum node_stat_item
686  * @val: delta to add to the counter, can be negative
687  */
688 void mod_memcg_state(struct mem_cgroup *memcg, enum memcg_stat_item idx,
689 		       int val)
690 {
691 	int i = memcg_stats_index(idx);
692 	int cpu;
693 
694 	if (mem_cgroup_disabled())
695 		return;
696 
697 	if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx))
698 		return;
699 
700 	cpu = get_cpu();
701 
702 	this_cpu_add(memcg->vmstats_percpu->state[i], val);
703 	val = memcg_state_val_in_pages(idx, val);
704 	memcg_rstat_updated(memcg, val, cpu);
705 	trace_mod_memcg_state(memcg, idx, val);
706 
707 	put_cpu();
708 }
709 
710 #ifdef CONFIG_MEMCG_V1
711 /* idx can be of type enum memcg_stat_item or node_stat_item. */
712 unsigned long memcg_page_state_local(struct mem_cgroup *memcg, int idx)
713 {
714 	long x;
715 	int i = memcg_stats_index(idx);
716 
717 	if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx))
718 		return 0;
719 
720 	x = READ_ONCE(memcg->vmstats->state_local[i]);
721 #ifdef CONFIG_SMP
722 	if (x < 0)
723 		x = 0;
724 #endif
725 	return x;
726 }
727 #endif
728 
729 static void mod_memcg_lruvec_state(struct lruvec *lruvec,
730 				     enum node_stat_item idx,
731 				     int val)
732 {
733 	struct mem_cgroup_per_node *pn;
734 	struct mem_cgroup *memcg;
735 	int i = memcg_stats_index(idx);
736 	int cpu;
737 
738 	if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx))
739 		return;
740 
741 	pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
742 	memcg = pn->memcg;
743 
744 	cpu = get_cpu();
745 
746 	/* Update memcg */
747 	this_cpu_add(memcg->vmstats_percpu->state[i], val);
748 
749 	/* Update lruvec */
750 	this_cpu_add(pn->lruvec_stats_percpu->state[i], val);
751 
752 	val = memcg_state_val_in_pages(idx, val);
753 	memcg_rstat_updated(memcg, val, cpu);
754 	trace_mod_memcg_lruvec_state(memcg, idx, val);
755 
756 	put_cpu();
757 }
758 
759 /**
760  * mod_lruvec_state - update lruvec memory statistics
761  * @lruvec: the lruvec
762  * @idx: the stat item
763  * @val: delta to add to the counter, can be negative
764  *
765  * The lruvec is the intersection of the NUMA node and a cgroup. This
766  * function updates the all three counters that are affected by a
767  * change of state at this level: per-node, per-cgroup, per-lruvec.
768  */
769 void mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
770 			int val)
771 {
772 	/* Update node */
773 	mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
774 
775 	/* Update memcg and lruvec */
776 	if (!mem_cgroup_disabled())
777 		mod_memcg_lruvec_state(lruvec, idx, val);
778 }
779 
780 void lruvec_stat_mod_folio(struct folio *folio, enum node_stat_item idx,
781 			     int val)
782 {
783 	struct mem_cgroup *memcg;
784 	pg_data_t *pgdat = folio_pgdat(folio);
785 	struct lruvec *lruvec;
786 
787 	rcu_read_lock();
788 	memcg = folio_memcg(folio);
789 	/* Untracked pages have no memcg, no lruvec. Update only the node */
790 	if (!memcg) {
791 		rcu_read_unlock();
792 		mod_node_page_state(pgdat, idx, val);
793 		return;
794 	}
795 
796 	lruvec = mem_cgroup_lruvec(memcg, pgdat);
797 	mod_lruvec_state(lruvec, idx, val);
798 	rcu_read_unlock();
799 }
800 EXPORT_SYMBOL(lruvec_stat_mod_folio);
801 
802 void mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val)
803 {
804 	pg_data_t *pgdat = page_pgdat(virt_to_page(p));
805 	struct mem_cgroup *memcg;
806 	struct lruvec *lruvec;
807 
808 	rcu_read_lock();
809 	memcg = mem_cgroup_from_slab_obj(p);
810 
811 	/*
812 	 * Untracked pages have no memcg, no lruvec. Update only the
813 	 * node. If we reparent the slab objects to the root memcg,
814 	 * when we free the slab object, we need to update the per-memcg
815 	 * vmstats to keep it correct for the root memcg.
816 	 */
817 	if (!memcg) {
818 		mod_node_page_state(pgdat, idx, val);
819 	} else {
820 		lruvec = mem_cgroup_lruvec(memcg, pgdat);
821 		mod_lruvec_state(lruvec, idx, val);
822 	}
823 	rcu_read_unlock();
824 }
825 
826 /**
827  * count_memcg_events - account VM events in a cgroup
828  * @memcg: the memory cgroup
829  * @idx: the event item
830  * @count: the number of events that occurred
831  */
832 void count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
833 			  unsigned long count)
834 {
835 	int i = memcg_events_index(idx);
836 	int cpu;
837 
838 	if (mem_cgroup_disabled())
839 		return;
840 
841 	if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx))
842 		return;
843 
844 	cpu = get_cpu();
845 
846 	this_cpu_add(memcg->vmstats_percpu->events[i], count);
847 	memcg_rstat_updated(memcg, count, cpu);
848 	trace_count_memcg_events(memcg, idx, count);
849 
850 	put_cpu();
851 }
852 
853 unsigned long memcg_events(struct mem_cgroup *memcg, int event)
854 {
855 	int i = memcg_events_index(event);
856 
857 	if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, event))
858 		return 0;
859 
860 	return READ_ONCE(memcg->vmstats->events[i]);
861 }
862 
863 #ifdef CONFIG_MEMCG_V1
864 unsigned long memcg_events_local(struct mem_cgroup *memcg, int event)
865 {
866 	int i = memcg_events_index(event);
867 
868 	if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, event))
869 		return 0;
870 
871 	return READ_ONCE(memcg->vmstats->events_local[i]);
872 }
873 #endif
874 
875 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
876 {
877 	/*
878 	 * mm_update_next_owner() may clear mm->owner to NULL
879 	 * if it races with swapoff, page migration, etc.
880 	 * So this can be called with p == NULL.
881 	 */
882 	if (unlikely(!p))
883 		return NULL;
884 
885 	return mem_cgroup_from_css(task_css(p, memory_cgrp_id));
886 }
887 EXPORT_SYMBOL(mem_cgroup_from_task);
888 
889 static __always_inline struct mem_cgroup *active_memcg(void)
890 {
891 	if (!in_task())
892 		return this_cpu_read(int_active_memcg);
893 	else
894 		return current->active_memcg;
895 }
896 
897 /**
898  * get_mem_cgroup_from_mm: Obtain a reference on given mm_struct's memcg.
899  * @mm: mm from which memcg should be extracted. It can be NULL.
900  *
901  * Obtain a reference on mm->memcg and returns it if successful. If mm
902  * is NULL, then the memcg is chosen as follows:
903  * 1) The active memcg, if set.
904  * 2) current->mm->memcg, if available
905  * 3) root memcg
906  * If mem_cgroup is disabled, NULL is returned.
907  */
908 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
909 {
910 	struct mem_cgroup *memcg;
911 
912 	if (mem_cgroup_disabled())
913 		return NULL;
914 
915 	/*
916 	 * Page cache insertions can happen without an
917 	 * actual mm context, e.g. during disk probing
918 	 * on boot, loopback IO, acct() writes etc.
919 	 *
920 	 * No need to css_get on root memcg as the reference
921 	 * counting is disabled on the root level in the
922 	 * cgroup core. See CSS_NO_REF.
923 	 */
924 	if (unlikely(!mm)) {
925 		memcg = active_memcg();
926 		if (unlikely(memcg)) {
927 			/* remote memcg must hold a ref */
928 			css_get(&memcg->css);
929 			return memcg;
930 		}
931 		mm = current->mm;
932 		if (unlikely(!mm))
933 			return root_mem_cgroup;
934 	}
935 
936 	rcu_read_lock();
937 	do {
938 		memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
939 		if (unlikely(!memcg))
940 			memcg = root_mem_cgroup;
941 	} while (!css_tryget(&memcg->css));
942 	rcu_read_unlock();
943 	return memcg;
944 }
945 EXPORT_SYMBOL(get_mem_cgroup_from_mm);
946 
947 /**
948  * get_mem_cgroup_from_current - Obtain a reference on current task's memcg.
949  */
950 struct mem_cgroup *get_mem_cgroup_from_current(void)
951 {
952 	struct mem_cgroup *memcg;
953 
954 	if (mem_cgroup_disabled())
955 		return NULL;
956 
957 again:
958 	rcu_read_lock();
959 	memcg = mem_cgroup_from_task(current);
960 	if (!css_tryget(&memcg->css)) {
961 		rcu_read_unlock();
962 		goto again;
963 	}
964 	rcu_read_unlock();
965 	return memcg;
966 }
967 
968 /**
969  * get_mem_cgroup_from_folio - Obtain a reference on a given folio's memcg.
970  * @folio: folio from which memcg should be extracted.
971  */
972 struct mem_cgroup *get_mem_cgroup_from_folio(struct folio *folio)
973 {
974 	struct mem_cgroup *memcg = folio_memcg(folio);
975 
976 	if (mem_cgroup_disabled())
977 		return NULL;
978 
979 	rcu_read_lock();
980 	if (!memcg || WARN_ON_ONCE(!css_tryget(&memcg->css)))
981 		memcg = root_mem_cgroup;
982 	rcu_read_unlock();
983 	return memcg;
984 }
985 
986 /**
987  * mem_cgroup_iter - iterate over memory cgroup hierarchy
988  * @root: hierarchy root
989  * @prev: previously returned memcg, NULL on first invocation
990  * @reclaim: cookie for shared reclaim walks, NULL for full walks
991  *
992  * Returns references to children of the hierarchy below @root, or
993  * @root itself, or %NULL after a full round-trip.
994  *
995  * Caller must pass the return value in @prev on subsequent
996  * invocations for reference counting, or use mem_cgroup_iter_break()
997  * to cancel a hierarchy walk before the round-trip is complete.
998  *
999  * Reclaimers can specify a node in @reclaim to divide up the memcgs
1000  * in the hierarchy among all concurrent reclaimers operating on the
1001  * same node.
1002  */
1003 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
1004 				   struct mem_cgroup *prev,
1005 				   struct mem_cgroup_reclaim_cookie *reclaim)
1006 {
1007 	struct mem_cgroup_reclaim_iter *iter;
1008 	struct cgroup_subsys_state *css;
1009 	struct mem_cgroup *pos;
1010 	struct mem_cgroup *next;
1011 
1012 	if (mem_cgroup_disabled())
1013 		return NULL;
1014 
1015 	if (!root)
1016 		root = root_mem_cgroup;
1017 
1018 	rcu_read_lock();
1019 restart:
1020 	next = NULL;
1021 
1022 	if (reclaim) {
1023 		int gen;
1024 		int nid = reclaim->pgdat->node_id;
1025 
1026 		iter = &root->nodeinfo[nid]->iter;
1027 		gen = atomic_read(&iter->generation);
1028 
1029 		/*
1030 		 * On start, join the current reclaim iteration cycle.
1031 		 * Exit when a concurrent walker completes it.
1032 		 */
1033 		if (!prev)
1034 			reclaim->generation = gen;
1035 		else if (reclaim->generation != gen)
1036 			goto out_unlock;
1037 
1038 		pos = READ_ONCE(iter->position);
1039 	} else
1040 		pos = prev;
1041 
1042 	css = pos ? &pos->css : NULL;
1043 
1044 	while ((css = css_next_descendant_pre(css, &root->css))) {
1045 		/*
1046 		 * Verify the css and acquire a reference.  The root
1047 		 * is provided by the caller, so we know it's alive
1048 		 * and kicking, and don't take an extra reference.
1049 		 */
1050 		if (css == &root->css || css_tryget(css))
1051 			break;
1052 	}
1053 
1054 	next = mem_cgroup_from_css(css);
1055 
1056 	if (reclaim) {
1057 		/*
1058 		 * The position could have already been updated by a competing
1059 		 * thread, so check that the value hasn't changed since we read
1060 		 * it to avoid reclaiming from the same cgroup twice.
1061 		 */
1062 		if (cmpxchg(&iter->position, pos, next) != pos) {
1063 			if (css && css != &root->css)
1064 				css_put(css);
1065 			goto restart;
1066 		}
1067 
1068 		if (!next) {
1069 			atomic_inc(&iter->generation);
1070 
1071 			/*
1072 			 * Reclaimers share the hierarchy walk, and a
1073 			 * new one might jump in right at the end of
1074 			 * the hierarchy - make sure they see at least
1075 			 * one group and restart from the beginning.
1076 			 */
1077 			if (!prev)
1078 				goto restart;
1079 		}
1080 	}
1081 
1082 out_unlock:
1083 	rcu_read_unlock();
1084 	if (prev && prev != root)
1085 		css_put(&prev->css);
1086 
1087 	return next;
1088 }
1089 
1090 /**
1091  * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1092  * @root: hierarchy root
1093  * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1094  */
1095 void mem_cgroup_iter_break(struct mem_cgroup *root,
1096 			   struct mem_cgroup *prev)
1097 {
1098 	if (!root)
1099 		root = root_mem_cgroup;
1100 	if (prev && prev != root)
1101 		css_put(&prev->css);
1102 }
1103 
1104 static void __invalidate_reclaim_iterators(struct mem_cgroup *from,
1105 					struct mem_cgroup *dead_memcg)
1106 {
1107 	struct mem_cgroup_reclaim_iter *iter;
1108 	struct mem_cgroup_per_node *mz;
1109 	int nid;
1110 
1111 	for_each_node(nid) {
1112 		mz = from->nodeinfo[nid];
1113 		iter = &mz->iter;
1114 		cmpxchg(&iter->position, dead_memcg, NULL);
1115 	}
1116 }
1117 
1118 static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg)
1119 {
1120 	struct mem_cgroup *memcg = dead_memcg;
1121 	struct mem_cgroup *last;
1122 
1123 	do {
1124 		__invalidate_reclaim_iterators(memcg, dead_memcg);
1125 		last = memcg;
1126 	} while ((memcg = parent_mem_cgroup(memcg)));
1127 
1128 	/*
1129 	 * When cgroup1 non-hierarchy mode is used,
1130 	 * parent_mem_cgroup() does not walk all the way up to the
1131 	 * cgroup root (root_mem_cgroup). So we have to handle
1132 	 * dead_memcg from cgroup root separately.
1133 	 */
1134 	if (!mem_cgroup_is_root(last))
1135 		__invalidate_reclaim_iterators(root_mem_cgroup,
1136 						dead_memcg);
1137 }
1138 
1139 /**
1140  * mem_cgroup_scan_tasks - iterate over tasks of a memory cgroup hierarchy
1141  * @memcg: hierarchy root
1142  * @fn: function to call for each task
1143  * @arg: argument passed to @fn
1144  *
1145  * This function iterates over tasks attached to @memcg or to any of its
1146  * descendants and calls @fn for each task. If @fn returns a non-zero
1147  * value, the function breaks the iteration loop. Otherwise, it will iterate
1148  * over all tasks and return 0.
1149  *
1150  * This function must not be called for the root memory cgroup.
1151  */
1152 void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1153 			   int (*fn)(struct task_struct *, void *), void *arg)
1154 {
1155 	struct mem_cgroup *iter;
1156 	int ret = 0;
1157 
1158 	BUG_ON(mem_cgroup_is_root(memcg));
1159 
1160 	for_each_mem_cgroup_tree(iter, memcg) {
1161 		struct css_task_iter it;
1162 		struct task_struct *task;
1163 
1164 		css_task_iter_start(&iter->css, CSS_TASK_ITER_PROCS, &it);
1165 		while (!ret && (task = css_task_iter_next(&it))) {
1166 			ret = fn(task, arg);
1167 			/* Avoid potential softlockup warning */
1168 			cond_resched();
1169 		}
1170 		css_task_iter_end(&it);
1171 		if (ret) {
1172 			mem_cgroup_iter_break(memcg, iter);
1173 			break;
1174 		}
1175 	}
1176 }
1177 
1178 #ifdef CONFIG_DEBUG_VM
1179 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
1180 {
1181 	struct mem_cgroup *memcg;
1182 
1183 	if (mem_cgroup_disabled())
1184 		return;
1185 
1186 	memcg = folio_memcg(folio);
1187 
1188 	if (!memcg)
1189 		VM_BUG_ON_FOLIO(!mem_cgroup_is_root(lruvec_memcg(lruvec)), folio);
1190 	else
1191 		VM_BUG_ON_FOLIO(lruvec_memcg(lruvec) != memcg, folio);
1192 }
1193 #endif
1194 
1195 /**
1196  * folio_lruvec_lock - Lock the lruvec for a folio.
1197  * @folio: Pointer to the folio.
1198  *
1199  * These functions are safe to use under any of the following conditions:
1200  * - folio locked
1201  * - folio_test_lru false
1202  * - folio frozen (refcount of 0)
1203  *
1204  * Return: The lruvec this folio is on with its lock held.
1205  */
1206 struct lruvec *folio_lruvec_lock(struct folio *folio)
1207 {
1208 	struct lruvec *lruvec = folio_lruvec(folio);
1209 
1210 	spin_lock(&lruvec->lru_lock);
1211 	lruvec_memcg_debug(lruvec, folio);
1212 
1213 	return lruvec;
1214 }
1215 
1216 /**
1217  * folio_lruvec_lock_irq - Lock the lruvec for a folio.
1218  * @folio: Pointer to the folio.
1219  *
1220  * These functions are safe to use under any of the following conditions:
1221  * - folio locked
1222  * - folio_test_lru false
1223  * - folio frozen (refcount of 0)
1224  *
1225  * Return: The lruvec this folio is on with its lock held and interrupts
1226  * disabled.
1227  */
1228 struct lruvec *folio_lruvec_lock_irq(struct folio *folio)
1229 {
1230 	struct lruvec *lruvec = folio_lruvec(folio);
1231 
1232 	spin_lock_irq(&lruvec->lru_lock);
1233 	lruvec_memcg_debug(lruvec, folio);
1234 
1235 	return lruvec;
1236 }
1237 
1238 /**
1239  * folio_lruvec_lock_irqsave - Lock the lruvec for a folio.
1240  * @folio: Pointer to the folio.
1241  * @flags: Pointer to irqsave flags.
1242  *
1243  * These functions are safe to use under any of the following conditions:
1244  * - folio locked
1245  * - folio_test_lru false
1246  * - folio frozen (refcount of 0)
1247  *
1248  * Return: The lruvec this folio is on with its lock held and interrupts
1249  * disabled.
1250  */
1251 struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
1252 		unsigned long *flags)
1253 {
1254 	struct lruvec *lruvec = folio_lruvec(folio);
1255 
1256 	spin_lock_irqsave(&lruvec->lru_lock, *flags);
1257 	lruvec_memcg_debug(lruvec, folio);
1258 
1259 	return lruvec;
1260 }
1261 
1262 /**
1263  * mem_cgroup_update_lru_size - account for adding or removing an lru page
1264  * @lruvec: mem_cgroup per zone lru vector
1265  * @lru: index of lru list the page is sitting on
1266  * @zid: zone id of the accounted pages
1267  * @nr_pages: positive when adding or negative when removing
1268  *
1269  * This function must be called under lru_lock, just before a page is added
1270  * to or just after a page is removed from an lru list.
1271  */
1272 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1273 				int zid, int nr_pages)
1274 {
1275 	struct mem_cgroup_per_node *mz;
1276 	unsigned long *lru_size;
1277 	long size;
1278 
1279 	if (mem_cgroup_disabled())
1280 		return;
1281 
1282 	mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
1283 	lru_size = &mz->lru_zone_size[zid][lru];
1284 
1285 	if (nr_pages < 0)
1286 		*lru_size += nr_pages;
1287 
1288 	size = *lru_size;
1289 	if (WARN_ONCE(size < 0,
1290 		"%s(%p, %d, %d): lru_size %ld\n",
1291 		__func__, lruvec, lru, nr_pages, size)) {
1292 		VM_BUG_ON(1);
1293 		*lru_size = 0;
1294 	}
1295 
1296 	if (nr_pages > 0)
1297 		*lru_size += nr_pages;
1298 }
1299 
1300 /**
1301  * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1302  * @memcg: the memory cgroup
1303  *
1304  * Returns the maximum amount of memory @mem can be charged with, in
1305  * pages.
1306  */
1307 static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
1308 {
1309 	unsigned long margin = 0;
1310 	unsigned long count;
1311 	unsigned long limit;
1312 
1313 	count = page_counter_read(&memcg->memory);
1314 	limit = READ_ONCE(memcg->memory.max);
1315 	if (count < limit)
1316 		margin = limit - count;
1317 
1318 	if (do_memsw_account()) {
1319 		count = page_counter_read(&memcg->memsw);
1320 		limit = READ_ONCE(memcg->memsw.max);
1321 		if (count < limit)
1322 			margin = min(margin, limit - count);
1323 		else
1324 			margin = 0;
1325 	}
1326 
1327 	return margin;
1328 }
1329 
1330 struct memory_stat {
1331 	const char *name;
1332 	unsigned int idx;
1333 };
1334 
1335 static const struct memory_stat memory_stats[] = {
1336 	{ "anon",			NR_ANON_MAPPED			},
1337 	{ "file",			NR_FILE_PAGES			},
1338 	{ "kernel",			MEMCG_KMEM			},
1339 	{ "kernel_stack",		NR_KERNEL_STACK_KB		},
1340 	{ "pagetables",			NR_PAGETABLE			},
1341 	{ "sec_pagetables",		NR_SECONDARY_PAGETABLE		},
1342 	{ "percpu",			MEMCG_PERCPU_B			},
1343 	{ "sock",			MEMCG_SOCK			},
1344 	{ "vmalloc",			MEMCG_VMALLOC			},
1345 	{ "shmem",			NR_SHMEM			},
1346 #ifdef CONFIG_ZSWAP
1347 	{ "zswap",			MEMCG_ZSWAP_B			},
1348 	{ "zswapped",			MEMCG_ZSWAPPED			},
1349 #endif
1350 	{ "file_mapped",		NR_FILE_MAPPED			},
1351 	{ "file_dirty",			NR_FILE_DIRTY			},
1352 	{ "file_writeback",		NR_WRITEBACK			},
1353 #ifdef CONFIG_SWAP
1354 	{ "swapcached",			NR_SWAPCACHE			},
1355 #endif
1356 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1357 	{ "anon_thp",			NR_ANON_THPS			},
1358 	{ "file_thp",			NR_FILE_THPS			},
1359 	{ "shmem_thp",			NR_SHMEM_THPS			},
1360 #endif
1361 	{ "inactive_anon",		NR_INACTIVE_ANON		},
1362 	{ "active_anon",		NR_ACTIVE_ANON			},
1363 	{ "inactive_file",		NR_INACTIVE_FILE		},
1364 	{ "active_file",		NR_ACTIVE_FILE			},
1365 	{ "unevictable",		NR_UNEVICTABLE			},
1366 	{ "slab_reclaimable",		NR_SLAB_RECLAIMABLE_B		},
1367 	{ "slab_unreclaimable",		NR_SLAB_UNRECLAIMABLE_B		},
1368 #ifdef CONFIG_HUGETLB_PAGE
1369 	{ "hugetlb",			NR_HUGETLB			},
1370 #endif
1371 
1372 	/* The memory events */
1373 	{ "workingset_refault_anon",	WORKINGSET_REFAULT_ANON		},
1374 	{ "workingset_refault_file",	WORKINGSET_REFAULT_FILE		},
1375 	{ "workingset_activate_anon",	WORKINGSET_ACTIVATE_ANON	},
1376 	{ "workingset_activate_file",	WORKINGSET_ACTIVATE_FILE	},
1377 	{ "workingset_restore_anon",	WORKINGSET_RESTORE_ANON		},
1378 	{ "workingset_restore_file",	WORKINGSET_RESTORE_FILE		},
1379 	{ "workingset_nodereclaim",	WORKINGSET_NODERECLAIM		},
1380 
1381 	{ "pgdemote_kswapd",		PGDEMOTE_KSWAPD		},
1382 	{ "pgdemote_direct",		PGDEMOTE_DIRECT		},
1383 	{ "pgdemote_khugepaged",	PGDEMOTE_KHUGEPAGED	},
1384 	{ "pgdemote_proactive",		PGDEMOTE_PROACTIVE	},
1385 #ifdef CONFIG_NUMA_BALANCING
1386 	{ "pgpromote_success",		PGPROMOTE_SUCCESS	},
1387 #endif
1388 };
1389 
1390 /* The actual unit of the state item, not the same as the output unit */
1391 static int memcg_page_state_unit(int item)
1392 {
1393 	switch (item) {
1394 	case MEMCG_PERCPU_B:
1395 	case MEMCG_ZSWAP_B:
1396 	case NR_SLAB_RECLAIMABLE_B:
1397 	case NR_SLAB_UNRECLAIMABLE_B:
1398 		return 1;
1399 	case NR_KERNEL_STACK_KB:
1400 		return SZ_1K;
1401 	default:
1402 		return PAGE_SIZE;
1403 	}
1404 }
1405 
1406 /* Translate stat items to the correct unit for memory.stat output */
1407 static int memcg_page_state_output_unit(int item)
1408 {
1409 	/*
1410 	 * Workingset state is actually in pages, but we export it to userspace
1411 	 * as a scalar count of events, so special case it here.
1412 	 *
1413 	 * Demotion and promotion activities are exported in pages, consistent
1414 	 * with their global counterparts.
1415 	 */
1416 	switch (item) {
1417 	case WORKINGSET_REFAULT_ANON:
1418 	case WORKINGSET_REFAULT_FILE:
1419 	case WORKINGSET_ACTIVATE_ANON:
1420 	case WORKINGSET_ACTIVATE_FILE:
1421 	case WORKINGSET_RESTORE_ANON:
1422 	case WORKINGSET_RESTORE_FILE:
1423 	case WORKINGSET_NODERECLAIM:
1424 	case PGDEMOTE_KSWAPD:
1425 	case PGDEMOTE_DIRECT:
1426 	case PGDEMOTE_KHUGEPAGED:
1427 	case PGDEMOTE_PROACTIVE:
1428 #ifdef CONFIG_NUMA_BALANCING
1429 	case PGPROMOTE_SUCCESS:
1430 #endif
1431 		return 1;
1432 	default:
1433 		return memcg_page_state_unit(item);
1434 	}
1435 }
1436 
1437 unsigned long memcg_page_state_output(struct mem_cgroup *memcg, int item)
1438 {
1439 	return memcg_page_state(memcg, item) *
1440 		memcg_page_state_output_unit(item);
1441 }
1442 
1443 #ifdef CONFIG_MEMCG_V1
1444 unsigned long memcg_page_state_local_output(struct mem_cgroup *memcg, int item)
1445 {
1446 	return memcg_page_state_local(memcg, item) *
1447 		memcg_page_state_output_unit(item);
1448 }
1449 #endif
1450 
1451 #ifdef CONFIG_HUGETLB_PAGE
1452 static bool memcg_accounts_hugetlb(void)
1453 {
1454 	return cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING;
1455 }
1456 #else /* CONFIG_HUGETLB_PAGE */
1457 static bool memcg_accounts_hugetlb(void)
1458 {
1459 	return false;
1460 }
1461 #endif /* CONFIG_HUGETLB_PAGE */
1462 
1463 static void memcg_stat_format(struct mem_cgroup *memcg, struct seq_buf *s)
1464 {
1465 	int i;
1466 
1467 	/*
1468 	 * Provide statistics on the state of the memory subsystem as
1469 	 * well as cumulative event counters that show past behavior.
1470 	 *
1471 	 * This list is ordered following a combination of these gradients:
1472 	 * 1) generic big picture -> specifics and details
1473 	 * 2) reflecting userspace activity -> reflecting kernel heuristics
1474 	 *
1475 	 * Current memory state:
1476 	 */
1477 	mem_cgroup_flush_stats(memcg);
1478 
1479 	for (i = 0; i < ARRAY_SIZE(memory_stats); i++) {
1480 		u64 size;
1481 
1482 #ifdef CONFIG_HUGETLB_PAGE
1483 		if (unlikely(memory_stats[i].idx == NR_HUGETLB) &&
1484 			!memcg_accounts_hugetlb())
1485 			continue;
1486 #endif
1487 		size = memcg_page_state_output(memcg, memory_stats[i].idx);
1488 		seq_buf_printf(s, "%s %llu\n", memory_stats[i].name, size);
1489 
1490 		if (unlikely(memory_stats[i].idx == NR_SLAB_UNRECLAIMABLE_B)) {
1491 			size += memcg_page_state_output(memcg,
1492 							NR_SLAB_RECLAIMABLE_B);
1493 			seq_buf_printf(s, "slab %llu\n", size);
1494 		}
1495 	}
1496 
1497 	/* Accumulated memory events */
1498 	seq_buf_printf(s, "pgscan %lu\n",
1499 		       memcg_events(memcg, PGSCAN_KSWAPD) +
1500 		       memcg_events(memcg, PGSCAN_DIRECT) +
1501 		       memcg_events(memcg, PGSCAN_PROACTIVE) +
1502 		       memcg_events(memcg, PGSCAN_KHUGEPAGED));
1503 	seq_buf_printf(s, "pgsteal %lu\n",
1504 		       memcg_events(memcg, PGSTEAL_KSWAPD) +
1505 		       memcg_events(memcg, PGSTEAL_DIRECT) +
1506 		       memcg_events(memcg, PGSTEAL_PROACTIVE) +
1507 		       memcg_events(memcg, PGSTEAL_KHUGEPAGED));
1508 
1509 	for (i = 0; i < ARRAY_SIZE(memcg_vm_event_stat); i++) {
1510 #ifdef CONFIG_MEMCG_V1
1511 		if (memcg_vm_event_stat[i] == PGPGIN ||
1512 		    memcg_vm_event_stat[i] == PGPGOUT)
1513 			continue;
1514 #endif
1515 		seq_buf_printf(s, "%s %lu\n",
1516 			       vm_event_name(memcg_vm_event_stat[i]),
1517 			       memcg_events(memcg, memcg_vm_event_stat[i]));
1518 	}
1519 }
1520 
1521 static void memory_stat_format(struct mem_cgroup *memcg, struct seq_buf *s)
1522 {
1523 	if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
1524 		memcg_stat_format(memcg, s);
1525 	else
1526 		memcg1_stat_format(memcg, s);
1527 	if (seq_buf_has_overflowed(s))
1528 		pr_warn("%s: Warning, stat buffer overflow, please report\n", __func__);
1529 }
1530 
1531 /**
1532  * mem_cgroup_print_oom_context: Print OOM information relevant to
1533  * memory controller.
1534  * @memcg: The memory cgroup that went over limit
1535  * @p: Task that is going to be killed
1536  *
1537  * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1538  * enabled
1539  */
1540 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1541 {
1542 	rcu_read_lock();
1543 
1544 	if (memcg) {
1545 		pr_cont(",oom_memcg=");
1546 		pr_cont_cgroup_path(memcg->css.cgroup);
1547 	} else
1548 		pr_cont(",global_oom");
1549 	if (p) {
1550 		pr_cont(",task_memcg=");
1551 		pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id));
1552 	}
1553 	rcu_read_unlock();
1554 }
1555 
1556 /**
1557  * mem_cgroup_print_oom_meminfo: Print OOM memory information relevant to
1558  * memory controller.
1559  * @memcg: The memory cgroup that went over limit
1560  */
1561 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
1562 {
1563 	/* Use static buffer, for the caller is holding oom_lock. */
1564 	static char buf[SEQ_BUF_SIZE];
1565 	struct seq_buf s;
1566 	unsigned long memory_failcnt;
1567 
1568 	lockdep_assert_held(&oom_lock);
1569 
1570 	if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
1571 		memory_failcnt = atomic_long_read(&memcg->memory_events[MEMCG_MAX]);
1572 	else
1573 		memory_failcnt = memcg->memory.failcnt;
1574 
1575 	pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n",
1576 		K((u64)page_counter_read(&memcg->memory)),
1577 		K((u64)READ_ONCE(memcg->memory.max)), memory_failcnt);
1578 	if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
1579 		pr_info("swap: usage %llukB, limit %llukB, failcnt %lu\n",
1580 			K((u64)page_counter_read(&memcg->swap)),
1581 			K((u64)READ_ONCE(memcg->swap.max)),
1582 			atomic_long_read(&memcg->memory_events[MEMCG_SWAP_MAX]));
1583 #ifdef CONFIG_MEMCG_V1
1584 	else {
1585 		pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n",
1586 			K((u64)page_counter_read(&memcg->memsw)),
1587 			K((u64)memcg->memsw.max), memcg->memsw.failcnt);
1588 		pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n",
1589 			K((u64)page_counter_read(&memcg->kmem)),
1590 			K((u64)memcg->kmem.max), memcg->kmem.failcnt);
1591 	}
1592 #endif
1593 
1594 	pr_info("Memory cgroup stats for ");
1595 	pr_cont_cgroup_path(memcg->css.cgroup);
1596 	pr_cont(":");
1597 	seq_buf_init(&s, buf, SEQ_BUF_SIZE);
1598 	memory_stat_format(memcg, &s);
1599 	seq_buf_do_printk(&s, KERN_INFO);
1600 }
1601 
1602 /*
1603  * Return the memory (and swap, if configured) limit for a memcg.
1604  */
1605 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
1606 {
1607 	unsigned long max = READ_ONCE(memcg->memory.max);
1608 
1609 	if (do_memsw_account()) {
1610 		if (mem_cgroup_swappiness(memcg)) {
1611 			/* Calculate swap excess capacity from memsw limit */
1612 			unsigned long swap = READ_ONCE(memcg->memsw.max) - max;
1613 
1614 			max += min(swap, (unsigned long)total_swap_pages);
1615 		}
1616 	} else {
1617 		if (mem_cgroup_swappiness(memcg))
1618 			max += min(READ_ONCE(memcg->swap.max),
1619 				   (unsigned long)total_swap_pages);
1620 	}
1621 	return max;
1622 }
1623 
1624 unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1625 {
1626 	return page_counter_read(&memcg->memory);
1627 }
1628 
1629 void __memcg_memory_event(struct mem_cgroup *memcg,
1630 			  enum memcg_memory_event event, bool allow_spinning)
1631 {
1632 	bool swap_event = event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX ||
1633 			  event == MEMCG_SWAP_FAIL;
1634 
1635 	/* For now only MEMCG_MAX can happen with !allow_spinning context. */
1636 	VM_WARN_ON_ONCE(!allow_spinning && event != MEMCG_MAX);
1637 
1638 	atomic_long_inc(&memcg->memory_events_local[event]);
1639 	if (!swap_event && allow_spinning)
1640 		cgroup_file_notify(&memcg->events_local_file);
1641 
1642 	do {
1643 		atomic_long_inc(&memcg->memory_events[event]);
1644 		if (allow_spinning) {
1645 			if (swap_event)
1646 				cgroup_file_notify(&memcg->swap_events_file);
1647 			else
1648 				cgroup_file_notify(&memcg->events_file);
1649 		}
1650 
1651 		if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1652 			break;
1653 		if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
1654 			break;
1655 	} while ((memcg = parent_mem_cgroup(memcg)) &&
1656 		 !mem_cgroup_is_root(memcg));
1657 }
1658 EXPORT_SYMBOL_GPL(__memcg_memory_event);
1659 
1660 static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1661 				     int order)
1662 {
1663 	struct oom_control oc = {
1664 		.zonelist = NULL,
1665 		.nodemask = NULL,
1666 		.memcg = memcg,
1667 		.gfp_mask = gfp_mask,
1668 		.order = order,
1669 	};
1670 	bool ret = true;
1671 
1672 	if (mutex_lock_killable(&oom_lock))
1673 		return true;
1674 
1675 	if (mem_cgroup_margin(memcg) >= (1 << order))
1676 		goto unlock;
1677 
1678 	/*
1679 	 * A few threads which were not waiting at mutex_lock_killable() can
1680 	 * fail to bail out. Therefore, check again after holding oom_lock.
1681 	 */
1682 	ret = out_of_memory(&oc);
1683 
1684 unlock:
1685 	mutex_unlock(&oom_lock);
1686 	return ret;
1687 }
1688 
1689 /*
1690  * Returns true if successfully killed one or more processes. Though in some
1691  * corner cases it can return true even without killing any process.
1692  */
1693 static bool mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
1694 {
1695 	bool locked, ret;
1696 
1697 	if (order > PAGE_ALLOC_COSTLY_ORDER)
1698 		return false;
1699 
1700 	memcg_memory_event(memcg, MEMCG_OOM);
1701 
1702 	if (!memcg1_oom_prepare(memcg, &locked))
1703 		return false;
1704 
1705 	ret = mem_cgroup_out_of_memory(memcg, mask, order);
1706 
1707 	memcg1_oom_finish(memcg, locked);
1708 
1709 	return ret;
1710 }
1711 
1712 /**
1713  * mem_cgroup_get_oom_group - get a memory cgroup to clean up after OOM
1714  * @victim: task to be killed by the OOM killer
1715  * @oom_domain: memcg in case of memcg OOM, NULL in case of system-wide OOM
1716  *
1717  * Returns a pointer to a memory cgroup, which has to be cleaned up
1718  * by killing all belonging OOM-killable tasks.
1719  *
1720  * Caller has to call mem_cgroup_put() on the returned non-NULL memcg.
1721  */
1722 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
1723 					    struct mem_cgroup *oom_domain)
1724 {
1725 	struct mem_cgroup *oom_group = NULL;
1726 	struct mem_cgroup *memcg;
1727 
1728 	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1729 		return NULL;
1730 
1731 	if (!oom_domain)
1732 		oom_domain = root_mem_cgroup;
1733 
1734 	rcu_read_lock();
1735 
1736 	memcg = mem_cgroup_from_task(victim);
1737 	if (mem_cgroup_is_root(memcg))
1738 		goto out;
1739 
1740 	/*
1741 	 * If the victim task has been asynchronously moved to a different
1742 	 * memory cgroup, we might end up killing tasks outside oom_domain.
1743 	 * In this case it's better to ignore memory.group.oom.
1744 	 */
1745 	if (unlikely(!mem_cgroup_is_descendant(memcg, oom_domain)))
1746 		goto out;
1747 
1748 	/*
1749 	 * Traverse the memory cgroup hierarchy from the victim task's
1750 	 * cgroup up to the OOMing cgroup (or root) to find the
1751 	 * highest-level memory cgroup with oom.group set.
1752 	 */
1753 	for (; memcg; memcg = parent_mem_cgroup(memcg)) {
1754 		if (READ_ONCE(memcg->oom_group))
1755 			oom_group = memcg;
1756 
1757 		if (memcg == oom_domain)
1758 			break;
1759 	}
1760 
1761 	if (oom_group)
1762 		css_get(&oom_group->css);
1763 out:
1764 	rcu_read_unlock();
1765 
1766 	return oom_group;
1767 }
1768 
1769 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
1770 {
1771 	pr_info("Tasks in ");
1772 	pr_cont_cgroup_path(memcg->css.cgroup);
1773 	pr_cont(" are going to be killed due to memory.oom.group set\n");
1774 }
1775 
1776 /*
1777  * The value of NR_MEMCG_STOCK is selected to keep the cached memcgs and their
1778  * nr_pages in a single cacheline. This may change in future.
1779  */
1780 #define NR_MEMCG_STOCK 7
1781 #define FLUSHING_CACHED_CHARGE	0
1782 struct memcg_stock_pcp {
1783 	local_trylock_t lock;
1784 	uint8_t nr_pages[NR_MEMCG_STOCK];
1785 	struct mem_cgroup *cached[NR_MEMCG_STOCK];
1786 
1787 	struct work_struct work;
1788 	unsigned long flags;
1789 };
1790 
1791 static DEFINE_PER_CPU_ALIGNED(struct memcg_stock_pcp, memcg_stock) = {
1792 	.lock = INIT_LOCAL_TRYLOCK(lock),
1793 };
1794 
1795 struct obj_stock_pcp {
1796 	local_trylock_t lock;
1797 	unsigned int nr_bytes;
1798 	struct obj_cgroup *cached_objcg;
1799 	struct pglist_data *cached_pgdat;
1800 	int nr_slab_reclaimable_b;
1801 	int nr_slab_unreclaimable_b;
1802 
1803 	struct work_struct work;
1804 	unsigned long flags;
1805 };
1806 
1807 static DEFINE_PER_CPU_ALIGNED(struct obj_stock_pcp, obj_stock) = {
1808 	.lock = INIT_LOCAL_TRYLOCK(lock),
1809 };
1810 
1811 static DEFINE_MUTEX(percpu_charge_mutex);
1812 
1813 static void drain_obj_stock(struct obj_stock_pcp *stock);
1814 static bool obj_stock_flush_required(struct obj_stock_pcp *stock,
1815 				     struct mem_cgroup *root_memcg);
1816 
1817 /**
1818  * consume_stock: Try to consume stocked charge on this cpu.
1819  * @memcg: memcg to consume from.
1820  * @nr_pages: how many pages to charge.
1821  *
1822  * Consume the cached charge if enough nr_pages are present otherwise return
1823  * failure. Also return failure for charge request larger than
1824  * MEMCG_CHARGE_BATCH or if the local lock is already taken.
1825  *
1826  * returns true if successful, false otherwise.
1827  */
1828 static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
1829 {
1830 	struct memcg_stock_pcp *stock;
1831 	uint8_t stock_pages;
1832 	bool ret = false;
1833 	int i;
1834 
1835 	if (nr_pages > MEMCG_CHARGE_BATCH ||
1836 	    !local_trylock(&memcg_stock.lock))
1837 		return ret;
1838 
1839 	stock = this_cpu_ptr(&memcg_stock);
1840 
1841 	for (i = 0; i < NR_MEMCG_STOCK; ++i) {
1842 		if (memcg != READ_ONCE(stock->cached[i]))
1843 			continue;
1844 
1845 		stock_pages = READ_ONCE(stock->nr_pages[i]);
1846 		if (stock_pages >= nr_pages) {
1847 			WRITE_ONCE(stock->nr_pages[i], stock_pages - nr_pages);
1848 			ret = true;
1849 		}
1850 		break;
1851 	}
1852 
1853 	local_unlock(&memcg_stock.lock);
1854 
1855 	return ret;
1856 }
1857 
1858 static void memcg_uncharge(struct mem_cgroup *memcg, unsigned int nr_pages)
1859 {
1860 	page_counter_uncharge(&memcg->memory, nr_pages);
1861 	if (do_memsw_account())
1862 		page_counter_uncharge(&memcg->memsw, nr_pages);
1863 }
1864 
1865 /*
1866  * Returns stocks cached in percpu and reset cached information.
1867  */
1868 static void drain_stock(struct memcg_stock_pcp *stock, int i)
1869 {
1870 	struct mem_cgroup *old = READ_ONCE(stock->cached[i]);
1871 	uint8_t stock_pages;
1872 
1873 	if (!old)
1874 		return;
1875 
1876 	stock_pages = READ_ONCE(stock->nr_pages[i]);
1877 	if (stock_pages) {
1878 		memcg_uncharge(old, stock_pages);
1879 		WRITE_ONCE(stock->nr_pages[i], 0);
1880 	}
1881 
1882 	css_put(&old->css);
1883 	WRITE_ONCE(stock->cached[i], NULL);
1884 }
1885 
1886 static void drain_stock_fully(struct memcg_stock_pcp *stock)
1887 {
1888 	int i;
1889 
1890 	for (i = 0; i < NR_MEMCG_STOCK; ++i)
1891 		drain_stock(stock, i);
1892 }
1893 
1894 static void drain_local_memcg_stock(struct work_struct *dummy)
1895 {
1896 	struct memcg_stock_pcp *stock;
1897 
1898 	if (WARN_ONCE(!in_task(), "drain in non-task context"))
1899 		return;
1900 
1901 	local_lock(&memcg_stock.lock);
1902 
1903 	stock = this_cpu_ptr(&memcg_stock);
1904 	drain_stock_fully(stock);
1905 	clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
1906 
1907 	local_unlock(&memcg_stock.lock);
1908 }
1909 
1910 static void drain_local_obj_stock(struct work_struct *dummy)
1911 {
1912 	struct obj_stock_pcp *stock;
1913 
1914 	if (WARN_ONCE(!in_task(), "drain in non-task context"))
1915 		return;
1916 
1917 	local_lock(&obj_stock.lock);
1918 
1919 	stock = this_cpu_ptr(&obj_stock);
1920 	drain_obj_stock(stock);
1921 	clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
1922 
1923 	local_unlock(&obj_stock.lock);
1924 }
1925 
1926 static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
1927 {
1928 	struct memcg_stock_pcp *stock;
1929 	struct mem_cgroup *cached;
1930 	uint8_t stock_pages;
1931 	bool success = false;
1932 	int empty_slot = -1;
1933 	int i;
1934 
1935 	/*
1936 	 * For now limit MEMCG_CHARGE_BATCH to 127 and less. In future if we
1937 	 * decide to increase it more than 127 then we will need more careful
1938 	 * handling of nr_pages[] in struct memcg_stock_pcp.
1939 	 */
1940 	BUILD_BUG_ON(MEMCG_CHARGE_BATCH > S8_MAX);
1941 
1942 	VM_WARN_ON_ONCE(mem_cgroup_is_root(memcg));
1943 
1944 	if (nr_pages > MEMCG_CHARGE_BATCH ||
1945 	    !local_trylock(&memcg_stock.lock)) {
1946 		/*
1947 		 * In case of larger than batch refill or unlikely failure to
1948 		 * lock the percpu memcg_stock.lock, uncharge memcg directly.
1949 		 */
1950 		memcg_uncharge(memcg, nr_pages);
1951 		return;
1952 	}
1953 
1954 	stock = this_cpu_ptr(&memcg_stock);
1955 	for (i = 0; i < NR_MEMCG_STOCK; ++i) {
1956 		cached = READ_ONCE(stock->cached[i]);
1957 		if (!cached && empty_slot == -1)
1958 			empty_slot = i;
1959 		if (memcg == READ_ONCE(stock->cached[i])) {
1960 			stock_pages = READ_ONCE(stock->nr_pages[i]) + nr_pages;
1961 			WRITE_ONCE(stock->nr_pages[i], stock_pages);
1962 			if (stock_pages > MEMCG_CHARGE_BATCH)
1963 				drain_stock(stock, i);
1964 			success = true;
1965 			break;
1966 		}
1967 	}
1968 
1969 	if (!success) {
1970 		i = empty_slot;
1971 		if (i == -1) {
1972 			i = get_random_u32_below(NR_MEMCG_STOCK);
1973 			drain_stock(stock, i);
1974 		}
1975 		css_get(&memcg->css);
1976 		WRITE_ONCE(stock->cached[i], memcg);
1977 		WRITE_ONCE(stock->nr_pages[i], nr_pages);
1978 	}
1979 
1980 	local_unlock(&memcg_stock.lock);
1981 }
1982 
1983 static bool is_memcg_drain_needed(struct memcg_stock_pcp *stock,
1984 				  struct mem_cgroup *root_memcg)
1985 {
1986 	struct mem_cgroup *memcg;
1987 	bool flush = false;
1988 	int i;
1989 
1990 	rcu_read_lock();
1991 	for (i = 0; i < NR_MEMCG_STOCK; ++i) {
1992 		memcg = READ_ONCE(stock->cached[i]);
1993 		if (!memcg)
1994 			continue;
1995 
1996 		if (READ_ONCE(stock->nr_pages[i]) &&
1997 		    mem_cgroup_is_descendant(memcg, root_memcg)) {
1998 			flush = true;
1999 			break;
2000 		}
2001 	}
2002 	rcu_read_unlock();
2003 	return flush;
2004 }
2005 
2006 /*
2007  * Drains all per-CPU charge caches for given root_memcg resp. subtree
2008  * of the hierarchy under it.
2009  */
2010 void drain_all_stock(struct mem_cgroup *root_memcg)
2011 {
2012 	int cpu, curcpu;
2013 
2014 	/* If someone's already draining, avoid adding running more workers. */
2015 	if (!mutex_trylock(&percpu_charge_mutex))
2016 		return;
2017 	/*
2018 	 * Notify other cpus that system-wide "drain" is running
2019 	 * We do not care about races with the cpu hotplug because cpu down
2020 	 * as well as workers from this path always operate on the local
2021 	 * per-cpu data. CPU up doesn't touch memcg_stock at all.
2022 	 */
2023 	migrate_disable();
2024 	curcpu = smp_processor_id();
2025 	for_each_online_cpu(cpu) {
2026 		struct memcg_stock_pcp *memcg_st = &per_cpu(memcg_stock, cpu);
2027 		struct obj_stock_pcp *obj_st = &per_cpu(obj_stock, cpu);
2028 
2029 		if (!test_bit(FLUSHING_CACHED_CHARGE, &memcg_st->flags) &&
2030 		    is_memcg_drain_needed(memcg_st, root_memcg) &&
2031 		    !test_and_set_bit(FLUSHING_CACHED_CHARGE,
2032 				      &memcg_st->flags)) {
2033 			if (cpu == curcpu)
2034 				drain_local_memcg_stock(&memcg_st->work);
2035 			else if (!cpu_is_isolated(cpu))
2036 				schedule_work_on(cpu, &memcg_st->work);
2037 		}
2038 
2039 		if (!test_bit(FLUSHING_CACHED_CHARGE, &obj_st->flags) &&
2040 		    obj_stock_flush_required(obj_st, root_memcg) &&
2041 		    !test_and_set_bit(FLUSHING_CACHED_CHARGE,
2042 				      &obj_st->flags)) {
2043 			if (cpu == curcpu)
2044 				drain_local_obj_stock(&obj_st->work);
2045 			else if (!cpu_is_isolated(cpu))
2046 				schedule_work_on(cpu, &obj_st->work);
2047 		}
2048 	}
2049 	migrate_enable();
2050 	mutex_unlock(&percpu_charge_mutex);
2051 }
2052 
2053 static int memcg_hotplug_cpu_dead(unsigned int cpu)
2054 {
2055 	/* no need for the local lock */
2056 	drain_obj_stock(&per_cpu(obj_stock, cpu));
2057 	drain_stock_fully(&per_cpu(memcg_stock, cpu));
2058 
2059 	return 0;
2060 }
2061 
2062 static unsigned long reclaim_high(struct mem_cgroup *memcg,
2063 				  unsigned int nr_pages,
2064 				  gfp_t gfp_mask)
2065 {
2066 	unsigned long nr_reclaimed = 0;
2067 
2068 	do {
2069 		unsigned long pflags;
2070 
2071 		if (page_counter_read(&memcg->memory) <=
2072 		    READ_ONCE(memcg->memory.high))
2073 			continue;
2074 
2075 		memcg_memory_event(memcg, MEMCG_HIGH);
2076 
2077 		psi_memstall_enter(&pflags);
2078 		nr_reclaimed += try_to_free_mem_cgroup_pages(memcg, nr_pages,
2079 							gfp_mask,
2080 							MEMCG_RECLAIM_MAY_SWAP,
2081 							NULL);
2082 		psi_memstall_leave(&pflags);
2083 	} while ((memcg = parent_mem_cgroup(memcg)) &&
2084 		 !mem_cgroup_is_root(memcg));
2085 
2086 	return nr_reclaimed;
2087 }
2088 
2089 static void high_work_func(struct work_struct *work)
2090 {
2091 	struct mem_cgroup *memcg;
2092 
2093 	memcg = container_of(work, struct mem_cgroup, high_work);
2094 	reclaim_high(memcg, MEMCG_CHARGE_BATCH, GFP_KERNEL);
2095 }
2096 
2097 /*
2098  * Clamp the maximum sleep time per allocation batch to 2 seconds. This is
2099  * enough to still cause a significant slowdown in most cases, while still
2100  * allowing diagnostics and tracing to proceed without becoming stuck.
2101  */
2102 #define MEMCG_MAX_HIGH_DELAY_JIFFIES (2UL*HZ)
2103 
2104 /*
2105  * When calculating the delay, we use these either side of the exponentiation to
2106  * maintain precision and scale to a reasonable number of jiffies (see the table
2107  * below.
2108  *
2109  * - MEMCG_DELAY_PRECISION_SHIFT: Extra precision bits while translating the
2110  *   overage ratio to a delay.
2111  * - MEMCG_DELAY_SCALING_SHIFT: The number of bits to scale down the
2112  *   proposed penalty in order to reduce to a reasonable number of jiffies, and
2113  *   to produce a reasonable delay curve.
2114  *
2115  * MEMCG_DELAY_SCALING_SHIFT just happens to be a number that produces a
2116  * reasonable delay curve compared to precision-adjusted overage, not
2117  * penalising heavily at first, but still making sure that growth beyond the
2118  * limit penalises misbehaviour cgroups by slowing them down exponentially. For
2119  * example, with a high of 100 megabytes:
2120  *
2121  *  +-------+------------------------+
2122  *  | usage | time to allocate in ms |
2123  *  +-------+------------------------+
2124  *  | 100M  |                      0 |
2125  *  | 101M  |                      6 |
2126  *  | 102M  |                     25 |
2127  *  | 103M  |                     57 |
2128  *  | 104M  |                    102 |
2129  *  | 105M  |                    159 |
2130  *  | 106M  |                    230 |
2131  *  | 107M  |                    313 |
2132  *  | 108M  |                    409 |
2133  *  | 109M  |                    518 |
2134  *  | 110M  |                    639 |
2135  *  | 111M  |                    774 |
2136  *  | 112M  |                    921 |
2137  *  | 113M  |                   1081 |
2138  *  | 114M  |                   1254 |
2139  *  | 115M  |                   1439 |
2140  *  | 116M  |                   1638 |
2141  *  | 117M  |                   1849 |
2142  *  | 118M  |                   2000 |
2143  *  | 119M  |                   2000 |
2144  *  | 120M  |                   2000 |
2145  *  +-------+------------------------+
2146  */
2147  #define MEMCG_DELAY_PRECISION_SHIFT 20
2148  #define MEMCG_DELAY_SCALING_SHIFT 14
2149 
2150 static u64 calculate_overage(unsigned long usage, unsigned long high)
2151 {
2152 	u64 overage;
2153 
2154 	if (usage <= high)
2155 		return 0;
2156 
2157 	/*
2158 	 * Prevent division by 0 in overage calculation by acting as if
2159 	 * it was a threshold of 1 page
2160 	 */
2161 	high = max(high, 1UL);
2162 
2163 	overage = usage - high;
2164 	overage <<= MEMCG_DELAY_PRECISION_SHIFT;
2165 	return div64_u64(overage, high);
2166 }
2167 
2168 static u64 mem_find_max_overage(struct mem_cgroup *memcg)
2169 {
2170 	u64 overage, max_overage = 0;
2171 
2172 	do {
2173 		overage = calculate_overage(page_counter_read(&memcg->memory),
2174 					    READ_ONCE(memcg->memory.high));
2175 		max_overage = max(overage, max_overage);
2176 	} while ((memcg = parent_mem_cgroup(memcg)) &&
2177 		 !mem_cgroup_is_root(memcg));
2178 
2179 	return max_overage;
2180 }
2181 
2182 static u64 swap_find_max_overage(struct mem_cgroup *memcg)
2183 {
2184 	u64 overage, max_overage = 0;
2185 
2186 	do {
2187 		overage = calculate_overage(page_counter_read(&memcg->swap),
2188 					    READ_ONCE(memcg->swap.high));
2189 		if (overage)
2190 			memcg_memory_event(memcg, MEMCG_SWAP_HIGH);
2191 		max_overage = max(overage, max_overage);
2192 	} while ((memcg = parent_mem_cgroup(memcg)) &&
2193 		 !mem_cgroup_is_root(memcg));
2194 
2195 	return max_overage;
2196 }
2197 
2198 /*
2199  * Get the number of jiffies that we should penalise a mischievous cgroup which
2200  * is exceeding its memory.high by checking both it and its ancestors.
2201  */
2202 static unsigned long calculate_high_delay(struct mem_cgroup *memcg,
2203 					  unsigned int nr_pages,
2204 					  u64 max_overage)
2205 {
2206 	unsigned long penalty_jiffies;
2207 
2208 	if (!max_overage)
2209 		return 0;
2210 
2211 	/*
2212 	 * We use overage compared to memory.high to calculate the number of
2213 	 * jiffies to sleep (penalty_jiffies). Ideally this value should be
2214 	 * fairly lenient on small overages, and increasingly harsh when the
2215 	 * memcg in question makes it clear that it has no intention of stopping
2216 	 * its crazy behaviour, so we exponentially increase the delay based on
2217 	 * overage amount.
2218 	 */
2219 	penalty_jiffies = max_overage * max_overage * HZ;
2220 	penalty_jiffies >>= MEMCG_DELAY_PRECISION_SHIFT;
2221 	penalty_jiffies >>= MEMCG_DELAY_SCALING_SHIFT;
2222 
2223 	/*
2224 	 * Factor in the task's own contribution to the overage, such that four
2225 	 * N-sized allocations are throttled approximately the same as one
2226 	 * 4N-sized allocation.
2227 	 *
2228 	 * MEMCG_CHARGE_BATCH pages is nominal, so work out how much smaller or
2229 	 * larger the current charge patch is than that.
2230 	 */
2231 	return penalty_jiffies * nr_pages / MEMCG_CHARGE_BATCH;
2232 }
2233 
2234 /*
2235  * Reclaims memory over the high limit. Called directly from
2236  * try_charge() (context permitting), as well as from the userland
2237  * return path where reclaim is always able to block.
2238  */
2239 void __mem_cgroup_handle_over_high(gfp_t gfp_mask)
2240 {
2241 	unsigned long penalty_jiffies;
2242 	unsigned long pflags;
2243 	unsigned long nr_reclaimed;
2244 	unsigned int nr_pages = current->memcg_nr_pages_over_high;
2245 	int nr_retries = MAX_RECLAIM_RETRIES;
2246 	struct mem_cgroup *memcg;
2247 	bool in_retry = false;
2248 
2249 	memcg = get_mem_cgroup_from_mm(current->mm);
2250 	current->memcg_nr_pages_over_high = 0;
2251 
2252 retry_reclaim:
2253 	/*
2254 	 * Bail if the task is already exiting. Unlike memory.max,
2255 	 * memory.high enforcement isn't as strict, and there is no
2256 	 * OOM killer involved, which means the excess could already
2257 	 * be much bigger (and still growing) than it could for
2258 	 * memory.max; the dying task could get stuck in fruitless
2259 	 * reclaim for a long time, which isn't desirable.
2260 	 */
2261 	if (task_is_dying())
2262 		goto out;
2263 
2264 	/*
2265 	 * The allocating task should reclaim at least the batch size, but for
2266 	 * subsequent retries we only want to do what's necessary to prevent oom
2267 	 * or breaching resource isolation.
2268 	 *
2269 	 * This is distinct from memory.max or page allocator behaviour because
2270 	 * memory.high is currently batched, whereas memory.max and the page
2271 	 * allocator run every time an allocation is made.
2272 	 */
2273 	nr_reclaimed = reclaim_high(memcg,
2274 				    in_retry ? SWAP_CLUSTER_MAX : nr_pages,
2275 				    gfp_mask);
2276 
2277 	/*
2278 	 * memory.high is breached and reclaim is unable to keep up. Throttle
2279 	 * allocators proactively to slow down excessive growth.
2280 	 */
2281 	penalty_jiffies = calculate_high_delay(memcg, nr_pages,
2282 					       mem_find_max_overage(memcg));
2283 
2284 	penalty_jiffies += calculate_high_delay(memcg, nr_pages,
2285 						swap_find_max_overage(memcg));
2286 
2287 	/*
2288 	 * Clamp the max delay per usermode return so as to still keep the
2289 	 * application moving forwards and also permit diagnostics, albeit
2290 	 * extremely slowly.
2291 	 */
2292 	penalty_jiffies = min(penalty_jiffies, MEMCG_MAX_HIGH_DELAY_JIFFIES);
2293 
2294 	/*
2295 	 * Don't sleep if the amount of jiffies this memcg owes us is so low
2296 	 * that it's not even worth doing, in an attempt to be nice to those who
2297 	 * go only a small amount over their memory.high value and maybe haven't
2298 	 * been aggressively reclaimed enough yet.
2299 	 */
2300 	if (penalty_jiffies <= HZ / 100)
2301 		goto out;
2302 
2303 	/*
2304 	 * If reclaim is making forward progress but we're still over
2305 	 * memory.high, we want to encourage that rather than doing allocator
2306 	 * throttling.
2307 	 */
2308 	if (nr_reclaimed || nr_retries--) {
2309 		in_retry = true;
2310 		goto retry_reclaim;
2311 	}
2312 
2313 	/*
2314 	 * Reclaim didn't manage to push usage below the limit, slow
2315 	 * this allocating task down.
2316 	 *
2317 	 * If we exit early, we're guaranteed to die (since
2318 	 * schedule_timeout_killable sets TASK_KILLABLE). This means we don't
2319 	 * need to account for any ill-begotten jiffies to pay them off later.
2320 	 */
2321 	psi_memstall_enter(&pflags);
2322 	schedule_timeout_killable(penalty_jiffies);
2323 	psi_memstall_leave(&pflags);
2324 
2325 out:
2326 	css_put(&memcg->css);
2327 }
2328 
2329 static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
2330 			    unsigned int nr_pages)
2331 {
2332 	unsigned int batch = max(MEMCG_CHARGE_BATCH, nr_pages);
2333 	int nr_retries = MAX_RECLAIM_RETRIES;
2334 	struct mem_cgroup *mem_over_limit;
2335 	struct page_counter *counter;
2336 	unsigned long nr_reclaimed;
2337 	bool passed_oom = false;
2338 	unsigned int reclaim_options = MEMCG_RECLAIM_MAY_SWAP;
2339 	bool drained = false;
2340 	bool raised_max_event = false;
2341 	unsigned long pflags;
2342 	bool allow_spinning = gfpflags_allow_spinning(gfp_mask);
2343 
2344 retry:
2345 	if (consume_stock(memcg, nr_pages))
2346 		return 0;
2347 
2348 	if (!allow_spinning)
2349 		/* Avoid the refill and flush of the older stock */
2350 		batch = nr_pages;
2351 
2352 	if (!do_memsw_account() ||
2353 	    page_counter_try_charge(&memcg->memsw, batch, &counter)) {
2354 		if (page_counter_try_charge(&memcg->memory, batch, &counter))
2355 			goto done_restock;
2356 		if (do_memsw_account())
2357 			page_counter_uncharge(&memcg->memsw, batch);
2358 		mem_over_limit = mem_cgroup_from_counter(counter, memory);
2359 	} else {
2360 		mem_over_limit = mem_cgroup_from_counter(counter, memsw);
2361 		reclaim_options &= ~MEMCG_RECLAIM_MAY_SWAP;
2362 	}
2363 
2364 	if (batch > nr_pages) {
2365 		batch = nr_pages;
2366 		goto retry;
2367 	}
2368 
2369 	/*
2370 	 * Prevent unbounded recursion when reclaim operations need to
2371 	 * allocate memory. This might exceed the limits temporarily,
2372 	 * but we prefer facilitating memory reclaim and getting back
2373 	 * under the limit over triggering OOM kills in these cases.
2374 	 */
2375 	if (unlikely(current->flags & PF_MEMALLOC))
2376 		goto force;
2377 
2378 	if (unlikely(task_in_memcg_oom(current)))
2379 		goto nomem;
2380 
2381 	if (!gfpflags_allow_blocking(gfp_mask))
2382 		goto nomem;
2383 
2384 	__memcg_memory_event(mem_over_limit, MEMCG_MAX, allow_spinning);
2385 	raised_max_event = true;
2386 
2387 	psi_memstall_enter(&pflags);
2388 	nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
2389 						    gfp_mask, reclaim_options, NULL);
2390 	psi_memstall_leave(&pflags);
2391 
2392 	if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
2393 		goto retry;
2394 
2395 	if (!drained) {
2396 		drain_all_stock(mem_over_limit);
2397 		drained = true;
2398 		goto retry;
2399 	}
2400 
2401 	if (gfp_mask & __GFP_NORETRY)
2402 		goto nomem;
2403 	/*
2404 	 * Even though the limit is exceeded at this point, reclaim
2405 	 * may have been able to free some pages.  Retry the charge
2406 	 * before killing the task.
2407 	 *
2408 	 * Only for regular pages, though: huge pages are rather
2409 	 * unlikely to succeed so close to the limit, and we fall back
2410 	 * to regular pages anyway in case of failure.
2411 	 */
2412 	if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
2413 		goto retry;
2414 
2415 	if (nr_retries--)
2416 		goto retry;
2417 
2418 	if (gfp_mask & __GFP_RETRY_MAYFAIL)
2419 		goto nomem;
2420 
2421 	/* Avoid endless loop for tasks bypassed by the oom killer */
2422 	if (passed_oom && task_is_dying())
2423 		goto nomem;
2424 
2425 	/*
2426 	 * keep retrying as long as the memcg oom killer is able to make
2427 	 * a forward progress or bypass the charge if the oom killer
2428 	 * couldn't make any progress.
2429 	 */
2430 	if (mem_cgroup_oom(mem_over_limit, gfp_mask,
2431 			   get_order(nr_pages * PAGE_SIZE))) {
2432 		passed_oom = true;
2433 		nr_retries = MAX_RECLAIM_RETRIES;
2434 		goto retry;
2435 	}
2436 nomem:
2437 	/*
2438 	 * Memcg doesn't have a dedicated reserve for atomic
2439 	 * allocations. But like the global atomic pool, we need to
2440 	 * put the burden of reclaim on regular allocation requests
2441 	 * and let these go through as privileged allocations.
2442 	 */
2443 	if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH)))
2444 		return -ENOMEM;
2445 force:
2446 	/*
2447 	 * If the allocation has to be enforced, don't forget to raise
2448 	 * a MEMCG_MAX event.
2449 	 */
2450 	if (!raised_max_event)
2451 		__memcg_memory_event(mem_over_limit, MEMCG_MAX, allow_spinning);
2452 
2453 	/*
2454 	 * The allocation either can't fail or will lead to more memory
2455 	 * being freed very soon.  Allow memory usage go over the limit
2456 	 * temporarily by force charging it.
2457 	 */
2458 	page_counter_charge(&memcg->memory, nr_pages);
2459 	if (do_memsw_account())
2460 		page_counter_charge(&memcg->memsw, nr_pages);
2461 
2462 	return 0;
2463 
2464 done_restock:
2465 	if (batch > nr_pages)
2466 		refill_stock(memcg, batch - nr_pages);
2467 
2468 	/*
2469 	 * If the hierarchy is above the normal consumption range, schedule
2470 	 * reclaim on returning to userland.  We can perform reclaim here
2471 	 * if __GFP_RECLAIM but let's always punt for simplicity and so that
2472 	 * GFP_KERNEL can consistently be used during reclaim.  @memcg is
2473 	 * not recorded as it most likely matches current's and won't
2474 	 * change in the meantime.  As high limit is checked again before
2475 	 * reclaim, the cost of mismatch is negligible.
2476 	 */
2477 	do {
2478 		bool mem_high, swap_high;
2479 
2480 		mem_high = page_counter_read(&memcg->memory) >
2481 			READ_ONCE(memcg->memory.high);
2482 		swap_high = page_counter_read(&memcg->swap) >
2483 			READ_ONCE(memcg->swap.high);
2484 
2485 		/* Don't bother a random interrupted task */
2486 		if (!in_task()) {
2487 			if (mem_high) {
2488 				schedule_work(&memcg->high_work);
2489 				break;
2490 			}
2491 			continue;
2492 		}
2493 
2494 		if (mem_high || swap_high) {
2495 			/*
2496 			 * The allocating tasks in this cgroup will need to do
2497 			 * reclaim or be throttled to prevent further growth
2498 			 * of the memory or swap footprints.
2499 			 *
2500 			 * Target some best-effort fairness between the tasks,
2501 			 * and distribute reclaim work and delay penalties
2502 			 * based on how much each task is actually allocating.
2503 			 */
2504 			current->memcg_nr_pages_over_high += batch;
2505 			set_notify_resume(current);
2506 			break;
2507 		}
2508 	} while ((memcg = parent_mem_cgroup(memcg)));
2509 
2510 	/*
2511 	 * Reclaim is set up above to be called from the userland
2512 	 * return path. But also attempt synchronous reclaim to avoid
2513 	 * excessive overrun while the task is still inside the
2514 	 * kernel. If this is successful, the return path will see it
2515 	 * when it rechecks the overage and simply bail out.
2516 	 */
2517 	if (current->memcg_nr_pages_over_high > MEMCG_CHARGE_BATCH &&
2518 	    !(current->flags & PF_MEMALLOC) &&
2519 	    gfpflags_allow_blocking(gfp_mask))
2520 		__mem_cgroup_handle_over_high(gfp_mask);
2521 	return 0;
2522 }
2523 
2524 static inline int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
2525 			     unsigned int nr_pages)
2526 {
2527 	if (mem_cgroup_is_root(memcg))
2528 		return 0;
2529 
2530 	return try_charge_memcg(memcg, gfp_mask, nr_pages);
2531 }
2532 
2533 static void commit_charge(struct folio *folio, struct mem_cgroup *memcg)
2534 {
2535 	VM_BUG_ON_FOLIO(folio_memcg_charged(folio), folio);
2536 	/*
2537 	 * Any of the following ensures page's memcg stability:
2538 	 *
2539 	 * - the page lock
2540 	 * - LRU isolation
2541 	 * - exclusive reference
2542 	 */
2543 	folio->memcg_data = (unsigned long)memcg;
2544 }
2545 
2546 #ifdef CONFIG_MEMCG_NMI_SAFETY_REQUIRES_ATOMIC
2547 static inline void account_slab_nmi_safe(struct mem_cgroup *memcg,
2548 					 struct pglist_data *pgdat,
2549 					 enum node_stat_item idx, int nr)
2550 {
2551 	struct lruvec *lruvec;
2552 
2553 	if (likely(!in_nmi())) {
2554 		lruvec = mem_cgroup_lruvec(memcg, pgdat);
2555 		mod_memcg_lruvec_state(lruvec, idx, nr);
2556 	} else {
2557 		struct mem_cgroup_per_node *pn = memcg->nodeinfo[pgdat->node_id];
2558 
2559 		/* preemption is disabled in_nmi(). */
2560 		css_rstat_updated(&memcg->css, smp_processor_id());
2561 		if (idx == NR_SLAB_RECLAIMABLE_B)
2562 			atomic_add(nr, &pn->slab_reclaimable);
2563 		else
2564 			atomic_add(nr, &pn->slab_unreclaimable);
2565 	}
2566 }
2567 #else
2568 static inline void account_slab_nmi_safe(struct mem_cgroup *memcg,
2569 					 struct pglist_data *pgdat,
2570 					 enum node_stat_item idx, int nr)
2571 {
2572 	struct lruvec *lruvec;
2573 
2574 	lruvec = mem_cgroup_lruvec(memcg, pgdat);
2575 	mod_memcg_lruvec_state(lruvec, idx, nr);
2576 }
2577 #endif
2578 
2579 static inline void mod_objcg_mlstate(struct obj_cgroup *objcg,
2580 				       struct pglist_data *pgdat,
2581 				       enum node_stat_item idx, int nr)
2582 {
2583 	struct mem_cgroup *memcg;
2584 
2585 	rcu_read_lock();
2586 	memcg = obj_cgroup_memcg(objcg);
2587 	account_slab_nmi_safe(memcg, pgdat, idx, nr);
2588 	rcu_read_unlock();
2589 }
2590 
2591 static __always_inline
2592 struct mem_cgroup *mem_cgroup_from_obj_folio(struct folio *folio, void *p)
2593 {
2594 	/*
2595 	 * Slab objects are accounted individually, not per-page.
2596 	 * Memcg membership data for each individual object is saved in
2597 	 * slab->obj_exts.
2598 	 */
2599 	if (folio_test_slab(folio)) {
2600 		struct slabobj_ext *obj_exts;
2601 		struct slab *slab;
2602 		unsigned int off;
2603 
2604 		slab = folio_slab(folio);
2605 		obj_exts = slab_obj_exts(slab);
2606 		if (!obj_exts)
2607 			return NULL;
2608 
2609 		off = obj_to_index(slab->slab_cache, slab, p);
2610 		if (obj_exts[off].objcg)
2611 			return obj_cgroup_memcg(obj_exts[off].objcg);
2612 
2613 		return NULL;
2614 	}
2615 
2616 	/*
2617 	 * folio_memcg_check() is used here, because in theory we can encounter
2618 	 * a folio where the slab flag has been cleared already, but
2619 	 * slab->obj_exts has not been freed yet
2620 	 * folio_memcg_check() will guarantee that a proper memory
2621 	 * cgroup pointer or NULL will be returned.
2622 	 */
2623 	return folio_memcg_check(folio);
2624 }
2625 
2626 /*
2627  * Returns a pointer to the memory cgroup to which the kernel object is charged.
2628  * It is not suitable for objects allocated using vmalloc().
2629  *
2630  * A passed kernel object must be a slab object or a generic kernel page.
2631  *
2632  * The caller must ensure the memcg lifetime, e.g. by taking rcu_read_lock(),
2633  * cgroup_mutex, etc.
2634  */
2635 struct mem_cgroup *mem_cgroup_from_slab_obj(void *p)
2636 {
2637 	if (mem_cgroup_disabled())
2638 		return NULL;
2639 
2640 	return mem_cgroup_from_obj_folio(virt_to_folio(p), p);
2641 }
2642 
2643 static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg)
2644 {
2645 	struct obj_cgroup *objcg = NULL;
2646 
2647 	for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) {
2648 		objcg = rcu_dereference(memcg->objcg);
2649 		if (likely(objcg && obj_cgroup_tryget(objcg)))
2650 			break;
2651 		objcg = NULL;
2652 	}
2653 	return objcg;
2654 }
2655 
2656 static struct obj_cgroup *current_objcg_update(void)
2657 {
2658 	struct mem_cgroup *memcg;
2659 	struct obj_cgroup *old, *objcg = NULL;
2660 
2661 	do {
2662 		/* Atomically drop the update bit. */
2663 		old = xchg(&current->objcg, NULL);
2664 		if (old) {
2665 			old = (struct obj_cgroup *)
2666 				((unsigned long)old & ~CURRENT_OBJCG_UPDATE_FLAG);
2667 			obj_cgroup_put(old);
2668 
2669 			old = NULL;
2670 		}
2671 
2672 		/* If new objcg is NULL, no reason for the second atomic update. */
2673 		if (!current->mm || (current->flags & PF_KTHREAD))
2674 			return NULL;
2675 
2676 		/*
2677 		 * Release the objcg pointer from the previous iteration,
2678 		 * if try_cmpxcg() below fails.
2679 		 */
2680 		if (unlikely(objcg)) {
2681 			obj_cgroup_put(objcg);
2682 			objcg = NULL;
2683 		}
2684 
2685 		/*
2686 		 * Obtain the new objcg pointer. The current task can be
2687 		 * asynchronously moved to another memcg and the previous
2688 		 * memcg can be offlined. So let's get the memcg pointer
2689 		 * and try get a reference to objcg under a rcu read lock.
2690 		 */
2691 
2692 		rcu_read_lock();
2693 		memcg = mem_cgroup_from_task(current);
2694 		objcg = __get_obj_cgroup_from_memcg(memcg);
2695 		rcu_read_unlock();
2696 
2697 		/*
2698 		 * Try set up a new objcg pointer atomically. If it
2699 		 * fails, it means the update flag was set concurrently, so
2700 		 * the whole procedure should be repeated.
2701 		 */
2702 	} while (!try_cmpxchg(&current->objcg, &old, objcg));
2703 
2704 	return objcg;
2705 }
2706 
2707 __always_inline struct obj_cgroup *current_obj_cgroup(void)
2708 {
2709 	struct mem_cgroup *memcg;
2710 	struct obj_cgroup *objcg;
2711 
2712 	if (IS_ENABLED(CONFIG_MEMCG_NMI_UNSAFE) && in_nmi())
2713 		return NULL;
2714 
2715 	if (in_task()) {
2716 		memcg = current->active_memcg;
2717 		if (unlikely(memcg))
2718 			goto from_memcg;
2719 
2720 		objcg = READ_ONCE(current->objcg);
2721 		if (unlikely((unsigned long)objcg & CURRENT_OBJCG_UPDATE_FLAG))
2722 			objcg = current_objcg_update();
2723 		/*
2724 		 * Objcg reference is kept by the task, so it's safe
2725 		 * to use the objcg by the current task.
2726 		 */
2727 		return objcg;
2728 	}
2729 
2730 	memcg = this_cpu_read(int_active_memcg);
2731 	if (unlikely(memcg))
2732 		goto from_memcg;
2733 
2734 	return NULL;
2735 
2736 from_memcg:
2737 	objcg = NULL;
2738 	for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) {
2739 		/*
2740 		 * Memcg pointer is protected by scope (see set_active_memcg())
2741 		 * and is pinning the corresponding objcg, so objcg can't go
2742 		 * away and can be used within the scope without any additional
2743 		 * protection.
2744 		 */
2745 		objcg = rcu_dereference_check(memcg->objcg, 1);
2746 		if (likely(objcg))
2747 			break;
2748 	}
2749 
2750 	return objcg;
2751 }
2752 
2753 struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio)
2754 {
2755 	struct obj_cgroup *objcg;
2756 
2757 	if (!memcg_kmem_online())
2758 		return NULL;
2759 
2760 	if (folio_memcg_kmem(folio)) {
2761 		objcg = __folio_objcg(folio);
2762 		obj_cgroup_get(objcg);
2763 	} else {
2764 		struct mem_cgroup *memcg;
2765 
2766 		rcu_read_lock();
2767 		memcg = __folio_memcg(folio);
2768 		if (memcg)
2769 			objcg = __get_obj_cgroup_from_memcg(memcg);
2770 		else
2771 			objcg = NULL;
2772 		rcu_read_unlock();
2773 	}
2774 	return objcg;
2775 }
2776 
2777 #ifdef CONFIG_MEMCG_NMI_SAFETY_REQUIRES_ATOMIC
2778 static inline void account_kmem_nmi_safe(struct mem_cgroup *memcg, int val)
2779 {
2780 	if (likely(!in_nmi())) {
2781 		mod_memcg_state(memcg, MEMCG_KMEM, val);
2782 	} else {
2783 		/* preemption is disabled in_nmi(). */
2784 		css_rstat_updated(&memcg->css, smp_processor_id());
2785 		atomic_add(val, &memcg->kmem_stat);
2786 	}
2787 }
2788 #else
2789 static inline void account_kmem_nmi_safe(struct mem_cgroup *memcg, int val)
2790 {
2791 	mod_memcg_state(memcg, MEMCG_KMEM, val);
2792 }
2793 #endif
2794 
2795 /*
2796  * obj_cgroup_uncharge_pages: uncharge a number of kernel pages from a objcg
2797  * @objcg: object cgroup to uncharge
2798  * @nr_pages: number of pages to uncharge
2799  */
2800 static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg,
2801 				      unsigned int nr_pages)
2802 {
2803 	struct mem_cgroup *memcg;
2804 
2805 	memcg = get_mem_cgroup_from_objcg(objcg);
2806 
2807 	account_kmem_nmi_safe(memcg, -nr_pages);
2808 	memcg1_account_kmem(memcg, -nr_pages);
2809 	if (!mem_cgroup_is_root(memcg))
2810 		refill_stock(memcg, nr_pages);
2811 
2812 	css_put(&memcg->css);
2813 }
2814 
2815 /*
2816  * obj_cgroup_charge_pages: charge a number of kernel pages to a objcg
2817  * @objcg: object cgroup to charge
2818  * @gfp: reclaim mode
2819  * @nr_pages: number of pages to charge
2820  *
2821  * Returns 0 on success, an error code on failure.
2822  */
2823 static int obj_cgroup_charge_pages(struct obj_cgroup *objcg, gfp_t gfp,
2824 				   unsigned int nr_pages)
2825 {
2826 	struct mem_cgroup *memcg;
2827 	int ret;
2828 
2829 	memcg = get_mem_cgroup_from_objcg(objcg);
2830 
2831 	ret = try_charge_memcg(memcg, gfp, nr_pages);
2832 	if (ret)
2833 		goto out;
2834 
2835 	account_kmem_nmi_safe(memcg, nr_pages);
2836 	memcg1_account_kmem(memcg, nr_pages);
2837 out:
2838 	css_put(&memcg->css);
2839 
2840 	return ret;
2841 }
2842 
2843 static struct obj_cgroup *page_objcg(const struct page *page)
2844 {
2845 	unsigned long memcg_data = page->memcg_data;
2846 
2847 	if (mem_cgroup_disabled() || !memcg_data)
2848 		return NULL;
2849 
2850 	VM_BUG_ON_PAGE((memcg_data & OBJEXTS_FLAGS_MASK) != MEMCG_DATA_KMEM,
2851 			page);
2852 	return (struct obj_cgroup *)(memcg_data - MEMCG_DATA_KMEM);
2853 }
2854 
2855 static void page_set_objcg(struct page *page, const struct obj_cgroup *objcg)
2856 {
2857 	page->memcg_data = (unsigned long)objcg | MEMCG_DATA_KMEM;
2858 }
2859 
2860 /**
2861  * __memcg_kmem_charge_page: charge a kmem page to the current memory cgroup
2862  * @page: page to charge
2863  * @gfp: reclaim mode
2864  * @order: allocation order
2865  *
2866  * Returns 0 on success, an error code on failure.
2867  */
2868 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order)
2869 {
2870 	struct obj_cgroup *objcg;
2871 	int ret = 0;
2872 
2873 	objcg = current_obj_cgroup();
2874 	if (objcg) {
2875 		ret = obj_cgroup_charge_pages(objcg, gfp, 1 << order);
2876 		if (!ret) {
2877 			obj_cgroup_get(objcg);
2878 			page_set_objcg(page, objcg);
2879 			return 0;
2880 		}
2881 	}
2882 	return ret;
2883 }
2884 
2885 /**
2886  * __memcg_kmem_uncharge_page: uncharge a kmem page
2887  * @page: page to uncharge
2888  * @order: allocation order
2889  */
2890 void __memcg_kmem_uncharge_page(struct page *page, int order)
2891 {
2892 	struct obj_cgroup *objcg = page_objcg(page);
2893 	unsigned int nr_pages = 1 << order;
2894 
2895 	if (!objcg)
2896 		return;
2897 
2898 	obj_cgroup_uncharge_pages(objcg, nr_pages);
2899 	page->memcg_data = 0;
2900 	obj_cgroup_put(objcg);
2901 }
2902 
2903 static void __account_obj_stock(struct obj_cgroup *objcg,
2904 				struct obj_stock_pcp *stock, int nr,
2905 				struct pglist_data *pgdat, enum node_stat_item idx)
2906 {
2907 	int *bytes;
2908 
2909 	/*
2910 	 * Save vmstat data in stock and skip vmstat array update unless
2911 	 * accumulating over a page of vmstat data or when pgdat changes.
2912 	 */
2913 	if (stock->cached_pgdat != pgdat) {
2914 		/* Flush the existing cached vmstat data */
2915 		struct pglist_data *oldpg = stock->cached_pgdat;
2916 
2917 		if (stock->nr_slab_reclaimable_b) {
2918 			mod_objcg_mlstate(objcg, oldpg, NR_SLAB_RECLAIMABLE_B,
2919 					  stock->nr_slab_reclaimable_b);
2920 			stock->nr_slab_reclaimable_b = 0;
2921 		}
2922 		if (stock->nr_slab_unreclaimable_b) {
2923 			mod_objcg_mlstate(objcg, oldpg, NR_SLAB_UNRECLAIMABLE_B,
2924 					  stock->nr_slab_unreclaimable_b);
2925 			stock->nr_slab_unreclaimable_b = 0;
2926 		}
2927 		stock->cached_pgdat = pgdat;
2928 	}
2929 
2930 	bytes = (idx == NR_SLAB_RECLAIMABLE_B) ? &stock->nr_slab_reclaimable_b
2931 					       : &stock->nr_slab_unreclaimable_b;
2932 	/*
2933 	 * Even for large object >= PAGE_SIZE, the vmstat data will still be
2934 	 * cached locally at least once before pushing it out.
2935 	 */
2936 	if (!*bytes) {
2937 		*bytes = nr;
2938 		nr = 0;
2939 	} else {
2940 		*bytes += nr;
2941 		if (abs(*bytes) > PAGE_SIZE) {
2942 			nr = *bytes;
2943 			*bytes = 0;
2944 		} else {
2945 			nr = 0;
2946 		}
2947 	}
2948 	if (nr)
2949 		mod_objcg_mlstate(objcg, pgdat, idx, nr);
2950 }
2951 
2952 static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes,
2953 			      struct pglist_data *pgdat, enum node_stat_item idx)
2954 {
2955 	struct obj_stock_pcp *stock;
2956 	bool ret = false;
2957 
2958 	if (!local_trylock(&obj_stock.lock))
2959 		return ret;
2960 
2961 	stock = this_cpu_ptr(&obj_stock);
2962 	if (objcg == READ_ONCE(stock->cached_objcg) && stock->nr_bytes >= nr_bytes) {
2963 		stock->nr_bytes -= nr_bytes;
2964 		ret = true;
2965 
2966 		if (pgdat)
2967 			__account_obj_stock(objcg, stock, nr_bytes, pgdat, idx);
2968 	}
2969 
2970 	local_unlock(&obj_stock.lock);
2971 
2972 	return ret;
2973 }
2974 
2975 static void drain_obj_stock(struct obj_stock_pcp *stock)
2976 {
2977 	struct obj_cgroup *old = READ_ONCE(stock->cached_objcg);
2978 
2979 	if (!old)
2980 		return;
2981 
2982 	if (stock->nr_bytes) {
2983 		unsigned int nr_pages = stock->nr_bytes >> PAGE_SHIFT;
2984 		unsigned int nr_bytes = stock->nr_bytes & (PAGE_SIZE - 1);
2985 
2986 		if (nr_pages) {
2987 			struct mem_cgroup *memcg;
2988 
2989 			memcg = get_mem_cgroup_from_objcg(old);
2990 
2991 			mod_memcg_state(memcg, MEMCG_KMEM, -nr_pages);
2992 			memcg1_account_kmem(memcg, -nr_pages);
2993 			if (!mem_cgroup_is_root(memcg))
2994 				memcg_uncharge(memcg, nr_pages);
2995 
2996 			css_put(&memcg->css);
2997 		}
2998 
2999 		/*
3000 		 * The leftover is flushed to the centralized per-memcg value.
3001 		 * On the next attempt to refill obj stock it will be moved
3002 		 * to a per-cpu stock (probably, on an other CPU), see
3003 		 * refill_obj_stock().
3004 		 *
3005 		 * How often it's flushed is a trade-off between the memory
3006 		 * limit enforcement accuracy and potential CPU contention,
3007 		 * so it might be changed in the future.
3008 		 */
3009 		atomic_add(nr_bytes, &old->nr_charged_bytes);
3010 		stock->nr_bytes = 0;
3011 	}
3012 
3013 	/*
3014 	 * Flush the vmstat data in current stock
3015 	 */
3016 	if (stock->nr_slab_reclaimable_b || stock->nr_slab_unreclaimable_b) {
3017 		if (stock->nr_slab_reclaimable_b) {
3018 			mod_objcg_mlstate(old, stock->cached_pgdat,
3019 					  NR_SLAB_RECLAIMABLE_B,
3020 					  stock->nr_slab_reclaimable_b);
3021 			stock->nr_slab_reclaimable_b = 0;
3022 		}
3023 		if (stock->nr_slab_unreclaimable_b) {
3024 			mod_objcg_mlstate(old, stock->cached_pgdat,
3025 					  NR_SLAB_UNRECLAIMABLE_B,
3026 					  stock->nr_slab_unreclaimable_b);
3027 			stock->nr_slab_unreclaimable_b = 0;
3028 		}
3029 		stock->cached_pgdat = NULL;
3030 	}
3031 
3032 	WRITE_ONCE(stock->cached_objcg, NULL);
3033 	obj_cgroup_put(old);
3034 }
3035 
3036 static bool obj_stock_flush_required(struct obj_stock_pcp *stock,
3037 				     struct mem_cgroup *root_memcg)
3038 {
3039 	struct obj_cgroup *objcg = READ_ONCE(stock->cached_objcg);
3040 	struct mem_cgroup *memcg;
3041 	bool flush = false;
3042 
3043 	rcu_read_lock();
3044 	if (objcg) {
3045 		memcg = obj_cgroup_memcg(objcg);
3046 		if (memcg && mem_cgroup_is_descendant(memcg, root_memcg))
3047 			flush = true;
3048 	}
3049 	rcu_read_unlock();
3050 
3051 	return flush;
3052 }
3053 
3054 static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes,
3055 		bool allow_uncharge, int nr_acct, struct pglist_data *pgdat,
3056 		enum node_stat_item idx)
3057 {
3058 	struct obj_stock_pcp *stock;
3059 	unsigned int nr_pages = 0;
3060 
3061 	if (!local_trylock(&obj_stock.lock)) {
3062 		if (pgdat)
3063 			mod_objcg_mlstate(objcg, pgdat, idx, nr_bytes);
3064 		nr_pages = nr_bytes >> PAGE_SHIFT;
3065 		nr_bytes = nr_bytes & (PAGE_SIZE - 1);
3066 		atomic_add(nr_bytes, &objcg->nr_charged_bytes);
3067 		goto out;
3068 	}
3069 
3070 	stock = this_cpu_ptr(&obj_stock);
3071 	if (READ_ONCE(stock->cached_objcg) != objcg) { /* reset if necessary */
3072 		drain_obj_stock(stock);
3073 		obj_cgroup_get(objcg);
3074 		stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes)
3075 				? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0;
3076 		WRITE_ONCE(stock->cached_objcg, objcg);
3077 
3078 		allow_uncharge = true;	/* Allow uncharge when objcg changes */
3079 	}
3080 	stock->nr_bytes += nr_bytes;
3081 
3082 	if (pgdat)
3083 		__account_obj_stock(objcg, stock, nr_acct, pgdat, idx);
3084 
3085 	if (allow_uncharge && (stock->nr_bytes > PAGE_SIZE)) {
3086 		nr_pages = stock->nr_bytes >> PAGE_SHIFT;
3087 		stock->nr_bytes &= (PAGE_SIZE - 1);
3088 	}
3089 
3090 	local_unlock(&obj_stock.lock);
3091 out:
3092 	if (nr_pages)
3093 		obj_cgroup_uncharge_pages(objcg, nr_pages);
3094 }
3095 
3096 static int obj_cgroup_charge_account(struct obj_cgroup *objcg, gfp_t gfp, size_t size,
3097 				     struct pglist_data *pgdat, enum node_stat_item idx)
3098 {
3099 	unsigned int nr_pages, nr_bytes;
3100 	int ret;
3101 
3102 	if (likely(consume_obj_stock(objcg, size, pgdat, idx)))
3103 		return 0;
3104 
3105 	/*
3106 	 * In theory, objcg->nr_charged_bytes can have enough
3107 	 * pre-charged bytes to satisfy the allocation. However,
3108 	 * flushing objcg->nr_charged_bytes requires two atomic
3109 	 * operations, and objcg->nr_charged_bytes can't be big.
3110 	 * The shared objcg->nr_charged_bytes can also become a
3111 	 * performance bottleneck if all tasks of the same memcg are
3112 	 * trying to update it. So it's better to ignore it and try
3113 	 * grab some new pages. The stock's nr_bytes will be flushed to
3114 	 * objcg->nr_charged_bytes later on when objcg changes.
3115 	 *
3116 	 * The stock's nr_bytes may contain enough pre-charged bytes
3117 	 * to allow one less page from being charged, but we can't rely
3118 	 * on the pre-charged bytes not being changed outside of
3119 	 * consume_obj_stock() or refill_obj_stock(). So ignore those
3120 	 * pre-charged bytes as well when charging pages. To avoid a
3121 	 * page uncharge right after a page charge, we set the
3122 	 * allow_uncharge flag to false when calling refill_obj_stock()
3123 	 * to temporarily allow the pre-charged bytes to exceed the page
3124 	 * size limit. The maximum reachable value of the pre-charged
3125 	 * bytes is (sizeof(object) + PAGE_SIZE - 2) if there is no data
3126 	 * race.
3127 	 */
3128 	nr_pages = size >> PAGE_SHIFT;
3129 	nr_bytes = size & (PAGE_SIZE - 1);
3130 
3131 	if (nr_bytes)
3132 		nr_pages += 1;
3133 
3134 	ret = obj_cgroup_charge_pages(objcg, gfp, nr_pages);
3135 	if (!ret && (nr_bytes || pgdat))
3136 		refill_obj_stock(objcg, nr_bytes ? PAGE_SIZE - nr_bytes : 0,
3137 					 false, size, pgdat, idx);
3138 
3139 	return ret;
3140 }
3141 
3142 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size)
3143 {
3144 	return obj_cgroup_charge_account(objcg, gfp, size, NULL, 0);
3145 }
3146 
3147 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size)
3148 {
3149 	refill_obj_stock(objcg, size, true, 0, NULL, 0);
3150 }
3151 
3152 static inline size_t obj_full_size(struct kmem_cache *s)
3153 {
3154 	/*
3155 	 * For each accounted object there is an extra space which is used
3156 	 * to store obj_cgroup membership. Charge it too.
3157 	 */
3158 	return s->size + sizeof(struct obj_cgroup *);
3159 }
3160 
3161 bool __memcg_slab_post_alloc_hook(struct kmem_cache *s, struct list_lru *lru,
3162 				  gfp_t flags, size_t size, void **p)
3163 {
3164 	struct obj_cgroup *objcg;
3165 	struct slab *slab;
3166 	unsigned long off;
3167 	size_t i;
3168 
3169 	/*
3170 	 * The obtained objcg pointer is safe to use within the current scope,
3171 	 * defined by current task or set_active_memcg() pair.
3172 	 * obj_cgroup_get() is used to get a permanent reference.
3173 	 */
3174 	objcg = current_obj_cgroup();
3175 	if (!objcg)
3176 		return true;
3177 
3178 	/*
3179 	 * slab_alloc_node() avoids the NULL check, so we might be called with a
3180 	 * single NULL object. kmem_cache_alloc_bulk() aborts if it can't fill
3181 	 * the whole requested size.
3182 	 * return success as there's nothing to free back
3183 	 */
3184 	if (unlikely(*p == NULL))
3185 		return true;
3186 
3187 	flags &= gfp_allowed_mask;
3188 
3189 	if (lru) {
3190 		int ret;
3191 		struct mem_cgroup *memcg;
3192 
3193 		memcg = get_mem_cgroup_from_objcg(objcg);
3194 		ret = memcg_list_lru_alloc(memcg, lru, flags);
3195 		css_put(&memcg->css);
3196 
3197 		if (ret)
3198 			return false;
3199 	}
3200 
3201 	for (i = 0; i < size; i++) {
3202 		slab = virt_to_slab(p[i]);
3203 
3204 		if (!slab_obj_exts(slab) &&
3205 		    alloc_slab_obj_exts(slab, s, flags, false)) {
3206 			continue;
3207 		}
3208 
3209 		/*
3210 		 * if we fail and size is 1, memcg_alloc_abort_single() will
3211 		 * just free the object, which is ok as we have not assigned
3212 		 * objcg to its obj_ext yet
3213 		 *
3214 		 * for larger sizes, kmem_cache_free_bulk() will uncharge
3215 		 * any objects that were already charged and obj_ext assigned
3216 		 *
3217 		 * TODO: we could batch this until slab_pgdat(slab) changes
3218 		 * between iterations, with a more complicated undo
3219 		 */
3220 		if (obj_cgroup_charge_account(objcg, flags, obj_full_size(s),
3221 					slab_pgdat(slab), cache_vmstat_idx(s)))
3222 			return false;
3223 
3224 		off = obj_to_index(s, slab, p[i]);
3225 		obj_cgroup_get(objcg);
3226 		slab_obj_exts(slab)[off].objcg = objcg;
3227 	}
3228 
3229 	return true;
3230 }
3231 
3232 void __memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
3233 			    void **p, int objects, struct slabobj_ext *obj_exts)
3234 {
3235 	size_t obj_size = obj_full_size(s);
3236 
3237 	for (int i = 0; i < objects; i++) {
3238 		struct obj_cgroup *objcg;
3239 		unsigned int off;
3240 
3241 		off = obj_to_index(s, slab, p[i]);
3242 		objcg = obj_exts[off].objcg;
3243 		if (!objcg)
3244 			continue;
3245 
3246 		obj_exts[off].objcg = NULL;
3247 		refill_obj_stock(objcg, obj_size, true, -obj_size,
3248 				 slab_pgdat(slab), cache_vmstat_idx(s));
3249 		obj_cgroup_put(objcg);
3250 	}
3251 }
3252 
3253 /*
3254  * The objcg is only set on the first page, so transfer it to all the
3255  * other pages.
3256  */
3257 void split_page_memcg(struct page *page, unsigned order)
3258 {
3259 	struct obj_cgroup *objcg = page_objcg(page);
3260 	unsigned int i, nr = 1 << order;
3261 
3262 	if (!objcg)
3263 		return;
3264 
3265 	for (i = 1; i < nr; i++)
3266 		page_set_objcg(&page[i], objcg);
3267 
3268 	obj_cgroup_get_many(objcg, nr - 1);
3269 }
3270 
3271 void folio_split_memcg_refs(struct folio *folio, unsigned old_order,
3272 		unsigned new_order)
3273 {
3274 	unsigned new_refs;
3275 
3276 	if (mem_cgroup_disabled() || !folio_memcg_charged(folio))
3277 		return;
3278 
3279 	new_refs = (1 << (old_order - new_order)) - 1;
3280 	css_get_many(&__folio_memcg(folio)->css, new_refs);
3281 }
3282 
3283 unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
3284 {
3285 	unsigned long val;
3286 
3287 	if (mem_cgroup_is_root(memcg)) {
3288 		/*
3289 		 * Approximate root's usage from global state. This isn't
3290 		 * perfect, but the root usage was always an approximation.
3291 		 */
3292 		val = global_node_page_state(NR_FILE_PAGES) +
3293 			global_node_page_state(NR_ANON_MAPPED);
3294 		if (swap)
3295 			val += total_swap_pages - get_nr_swap_pages();
3296 	} else {
3297 		if (!swap)
3298 			val = page_counter_read(&memcg->memory);
3299 		else
3300 			val = page_counter_read(&memcg->memsw);
3301 	}
3302 	return val;
3303 }
3304 
3305 static int memcg_online_kmem(struct mem_cgroup *memcg)
3306 {
3307 	struct obj_cgroup *objcg;
3308 
3309 	if (mem_cgroup_kmem_disabled())
3310 		return 0;
3311 
3312 	if (unlikely(mem_cgroup_is_root(memcg)))
3313 		return 0;
3314 
3315 	objcg = obj_cgroup_alloc();
3316 	if (!objcg)
3317 		return -ENOMEM;
3318 
3319 	objcg->memcg = memcg;
3320 	rcu_assign_pointer(memcg->objcg, objcg);
3321 	obj_cgroup_get(objcg);
3322 	memcg->orig_objcg = objcg;
3323 
3324 	static_branch_enable(&memcg_kmem_online_key);
3325 
3326 	memcg->kmemcg_id = memcg->id.id;
3327 
3328 	return 0;
3329 }
3330 
3331 static void memcg_offline_kmem(struct mem_cgroup *memcg)
3332 {
3333 	struct mem_cgroup *parent;
3334 
3335 	if (mem_cgroup_kmem_disabled())
3336 		return;
3337 
3338 	if (unlikely(mem_cgroup_is_root(memcg)))
3339 		return;
3340 
3341 	parent = parent_mem_cgroup(memcg);
3342 	if (!parent)
3343 		parent = root_mem_cgroup;
3344 
3345 	memcg_reparent_list_lrus(memcg, parent);
3346 
3347 	/*
3348 	 * Objcg's reparenting must be after list_lru's, make sure list_lru
3349 	 * helpers won't use parent's list_lru until child is drained.
3350 	 */
3351 	memcg_reparent_objcgs(memcg, parent);
3352 }
3353 
3354 #ifdef CONFIG_CGROUP_WRITEBACK
3355 
3356 #include <trace/events/writeback.h>
3357 
3358 static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
3359 {
3360 	return wb_domain_init(&memcg->cgwb_domain, gfp);
3361 }
3362 
3363 static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
3364 {
3365 	wb_domain_exit(&memcg->cgwb_domain);
3366 }
3367 
3368 static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
3369 {
3370 	wb_domain_size_changed(&memcg->cgwb_domain);
3371 }
3372 
3373 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
3374 {
3375 	struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
3376 
3377 	if (!memcg->css.parent)
3378 		return NULL;
3379 
3380 	return &memcg->cgwb_domain;
3381 }
3382 
3383 /**
3384  * mem_cgroup_wb_stats - retrieve writeback related stats from its memcg
3385  * @wb: bdi_writeback in question
3386  * @pfilepages: out parameter for number of file pages
3387  * @pheadroom: out parameter for number of allocatable pages according to memcg
3388  * @pdirty: out parameter for number of dirty pages
3389  * @pwriteback: out parameter for number of pages under writeback
3390  *
3391  * Determine the numbers of file, headroom, dirty, and writeback pages in
3392  * @wb's memcg.  File, dirty and writeback are self-explanatory.  Headroom
3393  * is a bit more involved.
3394  *
3395  * A memcg's headroom is "min(max, high) - used".  In the hierarchy, the
3396  * headroom is calculated as the lowest headroom of itself and the
3397  * ancestors.  Note that this doesn't consider the actual amount of
3398  * available memory in the system.  The caller should further cap
3399  * *@pheadroom accordingly.
3400  */
3401 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
3402 			 unsigned long *pheadroom, unsigned long *pdirty,
3403 			 unsigned long *pwriteback)
3404 {
3405 	struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
3406 	struct mem_cgroup *parent;
3407 
3408 	mem_cgroup_flush_stats_ratelimited(memcg);
3409 
3410 	*pdirty = memcg_page_state(memcg, NR_FILE_DIRTY);
3411 	*pwriteback = memcg_page_state(memcg, NR_WRITEBACK);
3412 	*pfilepages = memcg_page_state(memcg, NR_INACTIVE_FILE) +
3413 			memcg_page_state(memcg, NR_ACTIVE_FILE);
3414 
3415 	*pheadroom = PAGE_COUNTER_MAX;
3416 	while ((parent = parent_mem_cgroup(memcg))) {
3417 		unsigned long ceiling = min(READ_ONCE(memcg->memory.max),
3418 					    READ_ONCE(memcg->memory.high));
3419 		unsigned long used = page_counter_read(&memcg->memory);
3420 
3421 		*pheadroom = min(*pheadroom, ceiling - min(ceiling, used));
3422 		memcg = parent;
3423 	}
3424 }
3425 
3426 /*
3427  * Foreign dirty flushing
3428  *
3429  * There's an inherent mismatch between memcg and writeback.  The former
3430  * tracks ownership per-page while the latter per-inode.  This was a
3431  * deliberate design decision because honoring per-page ownership in the
3432  * writeback path is complicated, may lead to higher CPU and IO overheads
3433  * and deemed unnecessary given that write-sharing an inode across
3434  * different cgroups isn't a common use-case.
3435  *
3436  * Combined with inode majority-writer ownership switching, this works well
3437  * enough in most cases but there are some pathological cases.  For
3438  * example, let's say there are two cgroups A and B which keep writing to
3439  * different but confined parts of the same inode.  B owns the inode and
3440  * A's memory is limited far below B's.  A's dirty ratio can rise enough to
3441  * trigger balance_dirty_pages() sleeps but B's can be low enough to avoid
3442  * triggering background writeback.  A will be slowed down without a way to
3443  * make writeback of the dirty pages happen.
3444  *
3445  * Conditions like the above can lead to a cgroup getting repeatedly and
3446  * severely throttled after making some progress after each
3447  * dirty_expire_interval while the underlying IO device is almost
3448  * completely idle.
3449  *
3450  * Solving this problem completely requires matching the ownership tracking
3451  * granularities between memcg and writeback in either direction.  However,
3452  * the more egregious behaviors can be avoided by simply remembering the
3453  * most recent foreign dirtying events and initiating remote flushes on
3454  * them when local writeback isn't enough to keep the memory clean enough.
3455  *
3456  * The following two functions implement such mechanism.  When a foreign
3457  * page - a page whose memcg and writeback ownerships don't match - is
3458  * dirtied, mem_cgroup_track_foreign_dirty() records the inode owning
3459  * bdi_writeback on the page owning memcg.  When balance_dirty_pages()
3460  * decides that the memcg needs to sleep due to high dirty ratio, it calls
3461  * mem_cgroup_flush_foreign() which queues writeback on the recorded
3462  * foreign bdi_writebacks which haven't expired.  Both the numbers of
3463  * recorded bdi_writebacks and concurrent in-flight foreign writebacks are
3464  * limited to MEMCG_CGWB_FRN_CNT.
3465  *
3466  * The mechanism only remembers IDs and doesn't hold any object references.
3467  * As being wrong occasionally doesn't matter, updates and accesses to the
3468  * records are lockless and racy.
3469  */
3470 void mem_cgroup_track_foreign_dirty_slowpath(struct folio *folio,
3471 					     struct bdi_writeback *wb)
3472 {
3473 	struct mem_cgroup *memcg = folio_memcg(folio);
3474 	struct memcg_cgwb_frn *frn;
3475 	u64 now = get_jiffies_64();
3476 	u64 oldest_at = now;
3477 	int oldest = -1;
3478 	int i;
3479 
3480 	trace_track_foreign_dirty(folio, wb);
3481 
3482 	/*
3483 	 * Pick the slot to use.  If there is already a slot for @wb, keep
3484 	 * using it.  If not replace the oldest one which isn't being
3485 	 * written out.
3486 	 */
3487 	for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) {
3488 		frn = &memcg->cgwb_frn[i];
3489 		if (frn->bdi_id == wb->bdi->id &&
3490 		    frn->memcg_id == wb->memcg_css->id)
3491 			break;
3492 		if (time_before64(frn->at, oldest_at) &&
3493 		    atomic_read(&frn->done.cnt) == 1) {
3494 			oldest = i;
3495 			oldest_at = frn->at;
3496 		}
3497 	}
3498 
3499 	if (i < MEMCG_CGWB_FRN_CNT) {
3500 		/*
3501 		 * Re-using an existing one.  Update timestamp lazily to
3502 		 * avoid making the cacheline hot.  We want them to be
3503 		 * reasonably up-to-date and significantly shorter than
3504 		 * dirty_expire_interval as that's what expires the record.
3505 		 * Use the shorter of 1s and dirty_expire_interval / 8.
3506 		 */
3507 		unsigned long update_intv =
3508 			min_t(unsigned long, HZ,
3509 			      msecs_to_jiffies(dirty_expire_interval * 10) / 8);
3510 
3511 		if (time_before64(frn->at, now - update_intv))
3512 			frn->at = now;
3513 	} else if (oldest >= 0) {
3514 		/* replace the oldest free one */
3515 		frn = &memcg->cgwb_frn[oldest];
3516 		frn->bdi_id = wb->bdi->id;
3517 		frn->memcg_id = wb->memcg_css->id;
3518 		frn->at = now;
3519 	}
3520 }
3521 
3522 /* issue foreign writeback flushes for recorded foreign dirtying events */
3523 void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
3524 {
3525 	struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
3526 	unsigned long intv = msecs_to_jiffies(dirty_expire_interval * 10);
3527 	u64 now = jiffies_64;
3528 	int i;
3529 
3530 	for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) {
3531 		struct memcg_cgwb_frn *frn = &memcg->cgwb_frn[i];
3532 
3533 		/*
3534 		 * If the record is older than dirty_expire_interval,
3535 		 * writeback on it has already started.  No need to kick it
3536 		 * off again.  Also, don't start a new one if there's
3537 		 * already one in flight.
3538 		 */
3539 		if (time_after64(frn->at, now - intv) &&
3540 		    atomic_read(&frn->done.cnt) == 1) {
3541 			frn->at = 0;
3542 			trace_flush_foreign(wb, frn->bdi_id, frn->memcg_id);
3543 			cgroup_writeback_by_id(frn->bdi_id, frn->memcg_id,
3544 					       WB_REASON_FOREIGN_FLUSH,
3545 					       &frn->done);
3546 		}
3547 	}
3548 }
3549 
3550 #else	/* CONFIG_CGROUP_WRITEBACK */
3551 
3552 static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
3553 {
3554 	return 0;
3555 }
3556 
3557 static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
3558 {
3559 }
3560 
3561 static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
3562 {
3563 }
3564 
3565 #endif	/* CONFIG_CGROUP_WRITEBACK */
3566 
3567 /*
3568  * Private memory cgroup IDR
3569  *
3570  * Swap-out records and page cache shadow entries need to store memcg
3571  * references in constrained space, so we maintain an ID space that is
3572  * limited to 16 bit (MEM_CGROUP_ID_MAX), limiting the total number of
3573  * memory-controlled cgroups to 64k.
3574  *
3575  * However, there usually are many references to the offline CSS after
3576  * the cgroup has been destroyed, such as page cache or reclaimable
3577  * slab objects, that don't need to hang on to the ID. We want to keep
3578  * those dead CSS from occupying IDs, or we might quickly exhaust the
3579  * relatively small ID space and prevent the creation of new cgroups
3580  * even when there are much fewer than 64k cgroups - possibly none.
3581  *
3582  * Maintain a private 16-bit ID space for memcg, and allow the ID to
3583  * be freed and recycled when it's no longer needed, which is usually
3584  * when the CSS is offlined.
3585  *
3586  * The only exception to that are records of swapped out tmpfs/shmem
3587  * pages that need to be attributed to live ancestors on swapin. But
3588  * those references are manageable from userspace.
3589  */
3590 
3591 #define MEM_CGROUP_ID_MAX	((1UL << MEM_CGROUP_ID_SHIFT) - 1)
3592 static DEFINE_XARRAY_ALLOC1(mem_cgroup_ids);
3593 
3594 static void mem_cgroup_id_remove(struct mem_cgroup *memcg)
3595 {
3596 	if (memcg->id.id > 0) {
3597 		xa_erase(&mem_cgroup_ids, memcg->id.id);
3598 		memcg->id.id = 0;
3599 	}
3600 }
3601 
3602 void __maybe_unused mem_cgroup_id_get_many(struct mem_cgroup *memcg,
3603 					   unsigned int n)
3604 {
3605 	refcount_add(n, &memcg->id.ref);
3606 }
3607 
3608 static void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n)
3609 {
3610 	if (refcount_sub_and_test(n, &memcg->id.ref)) {
3611 		mem_cgroup_id_remove(memcg);
3612 
3613 		/* Memcg ID pins CSS */
3614 		css_put(&memcg->css);
3615 	}
3616 }
3617 
3618 static inline void mem_cgroup_id_put(struct mem_cgroup *memcg)
3619 {
3620 	mem_cgroup_id_put_many(memcg, 1);
3621 }
3622 
3623 struct mem_cgroup *mem_cgroup_id_get_online(struct mem_cgroup *memcg)
3624 {
3625 	while (!refcount_inc_not_zero(&memcg->id.ref)) {
3626 		/*
3627 		 * The root cgroup cannot be destroyed, so it's refcount must
3628 		 * always be >= 1.
3629 		 */
3630 		if (WARN_ON_ONCE(mem_cgroup_is_root(memcg))) {
3631 			VM_BUG_ON(1);
3632 			break;
3633 		}
3634 		memcg = parent_mem_cgroup(memcg);
3635 		if (!memcg)
3636 			memcg = root_mem_cgroup;
3637 	}
3638 	return memcg;
3639 }
3640 
3641 /**
3642  * mem_cgroup_from_id - look up a memcg from a memcg id
3643  * @id: the memcg id to look up
3644  *
3645  * Caller must hold rcu_read_lock().
3646  */
3647 struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
3648 {
3649 	WARN_ON_ONCE(!rcu_read_lock_held());
3650 	return xa_load(&mem_cgroup_ids, id);
3651 }
3652 
3653 #ifdef CONFIG_SHRINKER_DEBUG
3654 struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino)
3655 {
3656 	struct cgroup *cgrp;
3657 	struct cgroup_subsys_state *css;
3658 	struct mem_cgroup *memcg;
3659 
3660 	cgrp = cgroup_get_from_id(ino);
3661 	if (IS_ERR(cgrp))
3662 		return ERR_CAST(cgrp);
3663 
3664 	css = cgroup_get_e_css(cgrp, &memory_cgrp_subsys);
3665 	if (css)
3666 		memcg = container_of(css, struct mem_cgroup, css);
3667 	else
3668 		memcg = ERR_PTR(-ENOENT);
3669 
3670 	cgroup_put(cgrp);
3671 
3672 	return memcg;
3673 }
3674 #endif
3675 
3676 static void free_mem_cgroup_per_node_info(struct mem_cgroup_per_node *pn)
3677 {
3678 	if (!pn)
3679 		return;
3680 
3681 	free_percpu(pn->lruvec_stats_percpu);
3682 	kfree(pn->lruvec_stats);
3683 	kfree(pn);
3684 }
3685 
3686 static bool alloc_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node)
3687 {
3688 	struct mem_cgroup_per_node *pn;
3689 
3690 	pn = kmem_cache_alloc_node(memcg_pn_cachep, GFP_KERNEL | __GFP_ZERO,
3691 				   node);
3692 	if (!pn)
3693 		return false;
3694 
3695 	pn->lruvec_stats = kzalloc_node(sizeof(struct lruvec_stats),
3696 					GFP_KERNEL_ACCOUNT, node);
3697 	if (!pn->lruvec_stats)
3698 		goto fail;
3699 
3700 	pn->lruvec_stats_percpu = alloc_percpu_gfp(struct lruvec_stats_percpu,
3701 						   GFP_KERNEL_ACCOUNT);
3702 	if (!pn->lruvec_stats_percpu)
3703 		goto fail;
3704 
3705 	lruvec_init(&pn->lruvec);
3706 	pn->memcg = memcg;
3707 
3708 	memcg->nodeinfo[node] = pn;
3709 	return true;
3710 fail:
3711 	free_mem_cgroup_per_node_info(pn);
3712 	return false;
3713 }
3714 
3715 static void __mem_cgroup_free(struct mem_cgroup *memcg)
3716 {
3717 	int node;
3718 
3719 	obj_cgroup_put(memcg->orig_objcg);
3720 
3721 	for_each_node(node)
3722 		free_mem_cgroup_per_node_info(memcg->nodeinfo[node]);
3723 	memcg1_free_events(memcg);
3724 	kfree(memcg->vmstats);
3725 	free_percpu(memcg->vmstats_percpu);
3726 	kfree(memcg);
3727 }
3728 
3729 static void mem_cgroup_free(struct mem_cgroup *memcg)
3730 {
3731 	lru_gen_exit_memcg(memcg);
3732 	memcg_wb_domain_exit(memcg);
3733 	__mem_cgroup_free(memcg);
3734 }
3735 
3736 static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent)
3737 {
3738 	struct memcg_vmstats_percpu *statc;
3739 	struct memcg_vmstats_percpu __percpu *pstatc_pcpu;
3740 	struct mem_cgroup *memcg;
3741 	int node, cpu;
3742 	int __maybe_unused i;
3743 	long error;
3744 
3745 	memcg = kmem_cache_zalloc(memcg_cachep, GFP_KERNEL);
3746 	if (!memcg)
3747 		return ERR_PTR(-ENOMEM);
3748 
3749 	error = xa_alloc(&mem_cgroup_ids, &memcg->id.id, NULL,
3750 			 XA_LIMIT(1, MEM_CGROUP_ID_MAX), GFP_KERNEL);
3751 	if (error)
3752 		goto fail;
3753 	error = -ENOMEM;
3754 
3755 	memcg->vmstats = kzalloc(sizeof(struct memcg_vmstats),
3756 				 GFP_KERNEL_ACCOUNT);
3757 	if (!memcg->vmstats)
3758 		goto fail;
3759 
3760 	memcg->vmstats_percpu = alloc_percpu_gfp(struct memcg_vmstats_percpu,
3761 						 GFP_KERNEL_ACCOUNT);
3762 	if (!memcg->vmstats_percpu)
3763 		goto fail;
3764 
3765 	if (!memcg1_alloc_events(memcg))
3766 		goto fail;
3767 
3768 	for_each_possible_cpu(cpu) {
3769 		if (parent)
3770 			pstatc_pcpu = parent->vmstats_percpu;
3771 		statc = per_cpu_ptr(memcg->vmstats_percpu, cpu);
3772 		statc->parent_pcpu = parent ? pstatc_pcpu : NULL;
3773 		statc->vmstats = memcg->vmstats;
3774 	}
3775 
3776 	for_each_node(node)
3777 		if (!alloc_mem_cgroup_per_node_info(memcg, node))
3778 			goto fail;
3779 
3780 	if (memcg_wb_domain_init(memcg, GFP_KERNEL))
3781 		goto fail;
3782 
3783 	INIT_WORK(&memcg->high_work, high_work_func);
3784 	vmpressure_init(&memcg->vmpressure);
3785 	INIT_LIST_HEAD(&memcg->memory_peaks);
3786 	INIT_LIST_HEAD(&memcg->swap_peaks);
3787 	spin_lock_init(&memcg->peaks_lock);
3788 	memcg->socket_pressure = get_jiffies_64();
3789 #if BITS_PER_LONG < 64
3790 	seqlock_init(&memcg->socket_pressure_seqlock);
3791 #endif
3792 	memcg1_memcg_init(memcg);
3793 	memcg->kmemcg_id = -1;
3794 	INIT_LIST_HEAD(&memcg->objcg_list);
3795 #ifdef CONFIG_CGROUP_WRITEBACK
3796 	INIT_LIST_HEAD(&memcg->cgwb_list);
3797 	for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++)
3798 		memcg->cgwb_frn[i].done =
3799 			__WB_COMPLETION_INIT(&memcg_cgwb_frn_waitq);
3800 #endif
3801 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
3802 	spin_lock_init(&memcg->deferred_split_queue.split_queue_lock);
3803 	INIT_LIST_HEAD(&memcg->deferred_split_queue.split_queue);
3804 	memcg->deferred_split_queue.split_queue_len = 0;
3805 #endif
3806 	lru_gen_init_memcg(memcg);
3807 	return memcg;
3808 fail:
3809 	mem_cgroup_id_remove(memcg);
3810 	__mem_cgroup_free(memcg);
3811 	return ERR_PTR(error);
3812 }
3813 
3814 static struct cgroup_subsys_state * __ref
3815 mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
3816 {
3817 	struct mem_cgroup *parent = mem_cgroup_from_css(parent_css);
3818 	struct mem_cgroup *memcg, *old_memcg;
3819 	bool memcg_on_dfl = cgroup_subsys_on_dfl(memory_cgrp_subsys);
3820 
3821 	old_memcg = set_active_memcg(parent);
3822 	memcg = mem_cgroup_alloc(parent);
3823 	set_active_memcg(old_memcg);
3824 	if (IS_ERR(memcg))
3825 		return ERR_CAST(memcg);
3826 
3827 	page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX);
3828 	memcg1_soft_limit_reset(memcg);
3829 #ifdef CONFIG_ZSWAP
3830 	memcg->zswap_max = PAGE_COUNTER_MAX;
3831 	WRITE_ONCE(memcg->zswap_writeback, true);
3832 #endif
3833 	page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
3834 	if (parent) {
3835 		WRITE_ONCE(memcg->swappiness, mem_cgroup_swappiness(parent));
3836 
3837 		page_counter_init(&memcg->memory, &parent->memory, memcg_on_dfl);
3838 		page_counter_init(&memcg->swap, &parent->swap, false);
3839 #ifdef CONFIG_MEMCG_V1
3840 		memcg->memory.track_failcnt = !memcg_on_dfl;
3841 		WRITE_ONCE(memcg->oom_kill_disable, READ_ONCE(parent->oom_kill_disable));
3842 		page_counter_init(&memcg->kmem, &parent->kmem, false);
3843 		page_counter_init(&memcg->tcpmem, &parent->tcpmem, false);
3844 #endif
3845 	} else {
3846 		init_memcg_stats();
3847 		init_memcg_events();
3848 		page_counter_init(&memcg->memory, NULL, true);
3849 		page_counter_init(&memcg->swap, NULL, false);
3850 #ifdef CONFIG_MEMCG_V1
3851 		page_counter_init(&memcg->kmem, NULL, false);
3852 		page_counter_init(&memcg->tcpmem, NULL, false);
3853 #endif
3854 		root_mem_cgroup = memcg;
3855 		return &memcg->css;
3856 	}
3857 
3858 	if (memcg_on_dfl && !cgroup_memory_nosocket)
3859 		static_branch_inc(&memcg_sockets_enabled_key);
3860 
3861 	if (!cgroup_memory_nobpf)
3862 		static_branch_inc(&memcg_bpf_enabled_key);
3863 
3864 	return &memcg->css;
3865 }
3866 
3867 static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
3868 {
3869 	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
3870 
3871 	if (memcg_online_kmem(memcg))
3872 		goto remove_id;
3873 
3874 	/*
3875 	 * A memcg must be visible for expand_shrinker_info()
3876 	 * by the time the maps are allocated. So, we allocate maps
3877 	 * here, when for_each_mem_cgroup() can't skip it.
3878 	 */
3879 	if (alloc_shrinker_info(memcg))
3880 		goto offline_kmem;
3881 
3882 	if (unlikely(mem_cgroup_is_root(memcg)) && !mem_cgroup_disabled())
3883 		queue_delayed_work(system_unbound_wq, &stats_flush_dwork,
3884 				   FLUSH_TIME);
3885 	lru_gen_online_memcg(memcg);
3886 
3887 	/* Online state pins memcg ID, memcg ID pins CSS */
3888 	refcount_set(&memcg->id.ref, 1);
3889 	css_get(css);
3890 
3891 	/*
3892 	 * Ensure mem_cgroup_from_id() works once we're fully online.
3893 	 *
3894 	 * We could do this earlier and require callers to filter with
3895 	 * css_tryget_online(). But right now there are no users that
3896 	 * need earlier access, and the workingset code relies on the
3897 	 * cgroup tree linkage (mem_cgroup_get_nr_swap_pages()). So
3898 	 * publish it here at the end of onlining. This matches the
3899 	 * regular ID destruction during offlining.
3900 	 */
3901 	xa_store(&mem_cgroup_ids, memcg->id.id, memcg, GFP_KERNEL);
3902 
3903 	return 0;
3904 offline_kmem:
3905 	memcg_offline_kmem(memcg);
3906 remove_id:
3907 	mem_cgroup_id_remove(memcg);
3908 	return -ENOMEM;
3909 }
3910 
3911 static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
3912 {
3913 	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
3914 
3915 	memcg1_css_offline(memcg);
3916 
3917 	page_counter_set_min(&memcg->memory, 0);
3918 	page_counter_set_low(&memcg->memory, 0);
3919 
3920 	zswap_memcg_offline_cleanup(memcg);
3921 
3922 	memcg_offline_kmem(memcg);
3923 	reparent_deferred_split_queue(memcg);
3924 	reparent_shrinker_deferred(memcg);
3925 	wb_memcg_offline(memcg);
3926 	lru_gen_offline_memcg(memcg);
3927 
3928 	drain_all_stock(memcg);
3929 
3930 	mem_cgroup_id_put(memcg);
3931 }
3932 
3933 static void mem_cgroup_css_released(struct cgroup_subsys_state *css)
3934 {
3935 	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
3936 
3937 	invalidate_reclaim_iterators(memcg);
3938 	lru_gen_release_memcg(memcg);
3939 }
3940 
3941 static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
3942 {
3943 	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
3944 	int __maybe_unused i;
3945 
3946 #ifdef CONFIG_CGROUP_WRITEBACK
3947 	for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++)
3948 		wb_wait_for_completion(&memcg->cgwb_frn[i].done);
3949 #endif
3950 	if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket)
3951 		static_branch_dec(&memcg_sockets_enabled_key);
3952 
3953 	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg1_tcpmem_active(memcg))
3954 		static_branch_dec(&memcg_sockets_enabled_key);
3955 
3956 	if (!cgroup_memory_nobpf)
3957 		static_branch_dec(&memcg_bpf_enabled_key);
3958 
3959 	vmpressure_cleanup(&memcg->vmpressure);
3960 	cancel_work_sync(&memcg->high_work);
3961 	memcg1_remove_from_trees(memcg);
3962 	free_shrinker_info(memcg);
3963 	mem_cgroup_free(memcg);
3964 }
3965 
3966 /**
3967  * mem_cgroup_css_reset - reset the states of a mem_cgroup
3968  * @css: the target css
3969  *
3970  * Reset the states of the mem_cgroup associated with @css.  This is
3971  * invoked when the userland requests disabling on the default hierarchy
3972  * but the memcg is pinned through dependency.  The memcg should stop
3973  * applying policies and should revert to the vanilla state as it may be
3974  * made visible again.
3975  *
3976  * The current implementation only resets the essential configurations.
3977  * This needs to be expanded to cover all the visible parts.
3978  */
3979 static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
3980 {
3981 	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
3982 
3983 	page_counter_set_max(&memcg->memory, PAGE_COUNTER_MAX);
3984 	page_counter_set_max(&memcg->swap, PAGE_COUNTER_MAX);
3985 #ifdef CONFIG_MEMCG_V1
3986 	page_counter_set_max(&memcg->kmem, PAGE_COUNTER_MAX);
3987 	page_counter_set_max(&memcg->tcpmem, PAGE_COUNTER_MAX);
3988 #endif
3989 	page_counter_set_min(&memcg->memory, 0);
3990 	page_counter_set_low(&memcg->memory, 0);
3991 	page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX);
3992 	memcg1_soft_limit_reset(memcg);
3993 	page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
3994 	memcg_wb_domain_size_changed(memcg);
3995 }
3996 
3997 struct aggregate_control {
3998 	/* pointer to the aggregated (CPU and subtree aggregated) counters */
3999 	long *aggregate;
4000 	/* pointer to the non-hierarchichal (CPU aggregated) counters */
4001 	long *local;
4002 	/* pointer to the pending child counters during tree propagation */
4003 	long *pending;
4004 	/* pointer to the parent's pending counters, could be NULL */
4005 	long *ppending;
4006 	/* pointer to the percpu counters to be aggregated */
4007 	long *cstat;
4008 	/* pointer to the percpu counters of the last aggregation*/
4009 	long *cstat_prev;
4010 	/* size of the above counters */
4011 	int size;
4012 };
4013 
4014 static void mem_cgroup_stat_aggregate(struct aggregate_control *ac)
4015 {
4016 	int i;
4017 	long delta, delta_cpu, v;
4018 
4019 	for (i = 0; i < ac->size; i++) {
4020 		/*
4021 		 * Collect the aggregated propagation counts of groups
4022 		 * below us. We're in a per-cpu loop here and this is
4023 		 * a global counter, so the first cycle will get them.
4024 		 */
4025 		delta = ac->pending[i];
4026 		if (delta)
4027 			ac->pending[i] = 0;
4028 
4029 		/* Add CPU changes on this level since the last flush */
4030 		delta_cpu = 0;
4031 		v = READ_ONCE(ac->cstat[i]);
4032 		if (v != ac->cstat_prev[i]) {
4033 			delta_cpu = v - ac->cstat_prev[i];
4034 			delta += delta_cpu;
4035 			ac->cstat_prev[i] = v;
4036 		}
4037 
4038 		/* Aggregate counts on this level and propagate upwards */
4039 		if (delta_cpu)
4040 			ac->local[i] += delta_cpu;
4041 
4042 		if (delta) {
4043 			ac->aggregate[i] += delta;
4044 			if (ac->ppending)
4045 				ac->ppending[i] += delta;
4046 		}
4047 	}
4048 }
4049 
4050 #ifdef CONFIG_MEMCG_NMI_SAFETY_REQUIRES_ATOMIC
4051 static void flush_nmi_stats(struct mem_cgroup *memcg, struct mem_cgroup *parent,
4052 			    int cpu)
4053 {
4054 	int nid;
4055 
4056 	if (atomic_read(&memcg->kmem_stat)) {
4057 		int kmem = atomic_xchg(&memcg->kmem_stat, 0);
4058 		int index = memcg_stats_index(MEMCG_KMEM);
4059 
4060 		memcg->vmstats->state[index] += kmem;
4061 		if (parent)
4062 			parent->vmstats->state_pending[index] += kmem;
4063 	}
4064 
4065 	for_each_node_state(nid, N_MEMORY) {
4066 		struct mem_cgroup_per_node *pn = memcg->nodeinfo[nid];
4067 		struct lruvec_stats *lstats = pn->lruvec_stats;
4068 		struct lruvec_stats *plstats = NULL;
4069 
4070 		if (parent)
4071 			plstats = parent->nodeinfo[nid]->lruvec_stats;
4072 
4073 		if (atomic_read(&pn->slab_reclaimable)) {
4074 			int slab = atomic_xchg(&pn->slab_reclaimable, 0);
4075 			int index = memcg_stats_index(NR_SLAB_RECLAIMABLE_B);
4076 
4077 			lstats->state[index] += slab;
4078 			if (plstats)
4079 				plstats->state_pending[index] += slab;
4080 		}
4081 		if (atomic_read(&pn->slab_unreclaimable)) {
4082 			int slab = atomic_xchg(&pn->slab_unreclaimable, 0);
4083 			int index = memcg_stats_index(NR_SLAB_UNRECLAIMABLE_B);
4084 
4085 			lstats->state[index] += slab;
4086 			if (plstats)
4087 				plstats->state_pending[index] += slab;
4088 		}
4089 	}
4090 }
4091 #else
4092 static void flush_nmi_stats(struct mem_cgroup *memcg, struct mem_cgroup *parent,
4093 			    int cpu)
4094 {}
4095 #endif
4096 
4097 static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu)
4098 {
4099 	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4100 	struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4101 	struct memcg_vmstats_percpu *statc;
4102 	struct aggregate_control ac;
4103 	int nid;
4104 
4105 	flush_nmi_stats(memcg, parent, cpu);
4106 
4107 	statc = per_cpu_ptr(memcg->vmstats_percpu, cpu);
4108 
4109 	ac = (struct aggregate_control) {
4110 		.aggregate = memcg->vmstats->state,
4111 		.local = memcg->vmstats->state_local,
4112 		.pending = memcg->vmstats->state_pending,
4113 		.ppending = parent ? parent->vmstats->state_pending : NULL,
4114 		.cstat = statc->state,
4115 		.cstat_prev = statc->state_prev,
4116 		.size = MEMCG_VMSTAT_SIZE,
4117 	};
4118 	mem_cgroup_stat_aggregate(&ac);
4119 
4120 	ac = (struct aggregate_control) {
4121 		.aggregate = memcg->vmstats->events,
4122 		.local = memcg->vmstats->events_local,
4123 		.pending = memcg->vmstats->events_pending,
4124 		.ppending = parent ? parent->vmstats->events_pending : NULL,
4125 		.cstat = statc->events,
4126 		.cstat_prev = statc->events_prev,
4127 		.size = NR_MEMCG_EVENTS,
4128 	};
4129 	mem_cgroup_stat_aggregate(&ac);
4130 
4131 	for_each_node_state(nid, N_MEMORY) {
4132 		struct mem_cgroup_per_node *pn = memcg->nodeinfo[nid];
4133 		struct lruvec_stats *lstats = pn->lruvec_stats;
4134 		struct lruvec_stats *plstats = NULL;
4135 		struct lruvec_stats_percpu *lstatc;
4136 
4137 		if (parent)
4138 			plstats = parent->nodeinfo[nid]->lruvec_stats;
4139 
4140 		lstatc = per_cpu_ptr(pn->lruvec_stats_percpu, cpu);
4141 
4142 		ac = (struct aggregate_control) {
4143 			.aggregate = lstats->state,
4144 			.local = lstats->state_local,
4145 			.pending = lstats->state_pending,
4146 			.ppending = plstats ? plstats->state_pending : NULL,
4147 			.cstat = lstatc->state,
4148 			.cstat_prev = lstatc->state_prev,
4149 			.size = NR_MEMCG_NODE_STAT_ITEMS,
4150 		};
4151 		mem_cgroup_stat_aggregate(&ac);
4152 
4153 	}
4154 	WRITE_ONCE(statc->stats_updates, 0);
4155 	/* We are in a per-cpu loop here, only do the atomic write once */
4156 	if (atomic_read(&memcg->vmstats->stats_updates))
4157 		atomic_set(&memcg->vmstats->stats_updates, 0);
4158 }
4159 
4160 static void mem_cgroup_fork(struct task_struct *task)
4161 {
4162 	/*
4163 	 * Set the update flag to cause task->objcg to be initialized lazily
4164 	 * on the first allocation. It can be done without any synchronization
4165 	 * because it's always performed on the current task, so does
4166 	 * current_objcg_update().
4167 	 */
4168 	task->objcg = (struct obj_cgroup *)CURRENT_OBJCG_UPDATE_FLAG;
4169 }
4170 
4171 static void mem_cgroup_exit(struct task_struct *task)
4172 {
4173 	struct obj_cgroup *objcg = task->objcg;
4174 
4175 	objcg = (struct obj_cgroup *)
4176 		((unsigned long)objcg & ~CURRENT_OBJCG_UPDATE_FLAG);
4177 	obj_cgroup_put(objcg);
4178 
4179 	/*
4180 	 * Some kernel allocations can happen after this point,
4181 	 * but let's ignore them. It can be done without any synchronization
4182 	 * because it's always performed on the current task, so does
4183 	 * current_objcg_update().
4184 	 */
4185 	task->objcg = NULL;
4186 }
4187 
4188 #ifdef CONFIG_LRU_GEN
4189 static void mem_cgroup_lru_gen_attach(struct cgroup_taskset *tset)
4190 {
4191 	struct task_struct *task;
4192 	struct cgroup_subsys_state *css;
4193 
4194 	/* find the first leader if there is any */
4195 	cgroup_taskset_for_each_leader(task, css, tset)
4196 		break;
4197 
4198 	if (!task)
4199 		return;
4200 
4201 	task_lock(task);
4202 	if (task->mm && READ_ONCE(task->mm->owner) == task)
4203 		lru_gen_migrate_mm(task->mm);
4204 	task_unlock(task);
4205 }
4206 #else
4207 static void mem_cgroup_lru_gen_attach(struct cgroup_taskset *tset) {}
4208 #endif /* CONFIG_LRU_GEN */
4209 
4210 static void mem_cgroup_kmem_attach(struct cgroup_taskset *tset)
4211 {
4212 	struct task_struct *task;
4213 	struct cgroup_subsys_state *css;
4214 
4215 	cgroup_taskset_for_each(task, css, tset) {
4216 		/* atomically set the update bit */
4217 		set_bit(CURRENT_OBJCG_UPDATE_BIT, (unsigned long *)&task->objcg);
4218 	}
4219 }
4220 
4221 static void mem_cgroup_attach(struct cgroup_taskset *tset)
4222 {
4223 	mem_cgroup_lru_gen_attach(tset);
4224 	mem_cgroup_kmem_attach(tset);
4225 }
4226 
4227 static int seq_puts_memcg_tunable(struct seq_file *m, unsigned long value)
4228 {
4229 	if (value == PAGE_COUNTER_MAX)
4230 		seq_puts(m, "max\n");
4231 	else
4232 		seq_printf(m, "%llu\n", (u64)value * PAGE_SIZE);
4233 
4234 	return 0;
4235 }
4236 
4237 static u64 memory_current_read(struct cgroup_subsys_state *css,
4238 			       struct cftype *cft)
4239 {
4240 	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4241 
4242 	return (u64)page_counter_read(&memcg->memory) * PAGE_SIZE;
4243 }
4244 
4245 #define OFP_PEAK_UNSET (((-1UL)))
4246 
4247 static int peak_show(struct seq_file *sf, void *v, struct page_counter *pc)
4248 {
4249 	struct cgroup_of_peak *ofp = of_peak(sf->private);
4250 	u64 fd_peak = READ_ONCE(ofp->value), peak;
4251 
4252 	/* User wants global or local peak? */
4253 	if (fd_peak == OFP_PEAK_UNSET)
4254 		peak = pc->watermark;
4255 	else
4256 		peak = max(fd_peak, READ_ONCE(pc->local_watermark));
4257 
4258 	seq_printf(sf, "%llu\n", peak * PAGE_SIZE);
4259 	return 0;
4260 }
4261 
4262 static int memory_peak_show(struct seq_file *sf, void *v)
4263 {
4264 	struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf));
4265 
4266 	return peak_show(sf, v, &memcg->memory);
4267 }
4268 
4269 static int peak_open(struct kernfs_open_file *of)
4270 {
4271 	struct cgroup_of_peak *ofp = of_peak(of);
4272 
4273 	ofp->value = OFP_PEAK_UNSET;
4274 	return 0;
4275 }
4276 
4277 static void peak_release(struct kernfs_open_file *of)
4278 {
4279 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
4280 	struct cgroup_of_peak *ofp = of_peak(of);
4281 
4282 	if (ofp->value == OFP_PEAK_UNSET) {
4283 		/* fast path (no writes on this fd) */
4284 		return;
4285 	}
4286 	spin_lock(&memcg->peaks_lock);
4287 	list_del(&ofp->list);
4288 	spin_unlock(&memcg->peaks_lock);
4289 }
4290 
4291 static ssize_t peak_write(struct kernfs_open_file *of, char *buf, size_t nbytes,
4292 			  loff_t off, struct page_counter *pc,
4293 			  struct list_head *watchers)
4294 {
4295 	unsigned long usage;
4296 	struct cgroup_of_peak *peer_ctx;
4297 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
4298 	struct cgroup_of_peak *ofp = of_peak(of);
4299 
4300 	spin_lock(&memcg->peaks_lock);
4301 
4302 	usage = page_counter_read(pc);
4303 	WRITE_ONCE(pc->local_watermark, usage);
4304 
4305 	list_for_each_entry(peer_ctx, watchers, list)
4306 		if (usage > peer_ctx->value)
4307 			WRITE_ONCE(peer_ctx->value, usage);
4308 
4309 	/* initial write, register watcher */
4310 	if (ofp->value == OFP_PEAK_UNSET)
4311 		list_add(&ofp->list, watchers);
4312 
4313 	WRITE_ONCE(ofp->value, usage);
4314 	spin_unlock(&memcg->peaks_lock);
4315 
4316 	return nbytes;
4317 }
4318 
4319 static ssize_t memory_peak_write(struct kernfs_open_file *of, char *buf,
4320 				 size_t nbytes, loff_t off)
4321 {
4322 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
4323 
4324 	return peak_write(of, buf, nbytes, off, &memcg->memory,
4325 			  &memcg->memory_peaks);
4326 }
4327 
4328 #undef OFP_PEAK_UNSET
4329 
4330 static int memory_min_show(struct seq_file *m, void *v)
4331 {
4332 	return seq_puts_memcg_tunable(m,
4333 		READ_ONCE(mem_cgroup_from_seq(m)->memory.min));
4334 }
4335 
4336 static ssize_t memory_min_write(struct kernfs_open_file *of,
4337 				char *buf, size_t nbytes, loff_t off)
4338 {
4339 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
4340 	unsigned long min;
4341 	int err;
4342 
4343 	buf = strstrip(buf);
4344 	err = page_counter_memparse(buf, "max", &min);
4345 	if (err)
4346 		return err;
4347 
4348 	page_counter_set_min(&memcg->memory, min);
4349 
4350 	return nbytes;
4351 }
4352 
4353 static int memory_low_show(struct seq_file *m, void *v)
4354 {
4355 	return seq_puts_memcg_tunable(m,
4356 		READ_ONCE(mem_cgroup_from_seq(m)->memory.low));
4357 }
4358 
4359 static ssize_t memory_low_write(struct kernfs_open_file *of,
4360 				char *buf, size_t nbytes, loff_t off)
4361 {
4362 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
4363 	unsigned long low;
4364 	int err;
4365 
4366 	buf = strstrip(buf);
4367 	err = page_counter_memparse(buf, "max", &low);
4368 	if (err)
4369 		return err;
4370 
4371 	page_counter_set_low(&memcg->memory, low);
4372 
4373 	return nbytes;
4374 }
4375 
4376 static int memory_high_show(struct seq_file *m, void *v)
4377 {
4378 	return seq_puts_memcg_tunable(m,
4379 		READ_ONCE(mem_cgroup_from_seq(m)->memory.high));
4380 }
4381 
4382 static ssize_t memory_high_write(struct kernfs_open_file *of,
4383 				 char *buf, size_t nbytes, loff_t off)
4384 {
4385 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
4386 	unsigned int nr_retries = MAX_RECLAIM_RETRIES;
4387 	bool drained = false;
4388 	unsigned long high;
4389 	int err;
4390 
4391 	buf = strstrip(buf);
4392 	err = page_counter_memparse(buf, "max", &high);
4393 	if (err)
4394 		return err;
4395 
4396 	page_counter_set_high(&memcg->memory, high);
4397 
4398 	if (of->file->f_flags & O_NONBLOCK)
4399 		goto out;
4400 
4401 	for (;;) {
4402 		unsigned long nr_pages = page_counter_read(&memcg->memory);
4403 		unsigned long reclaimed;
4404 
4405 		if (nr_pages <= high)
4406 			break;
4407 
4408 		if (signal_pending(current))
4409 			break;
4410 
4411 		if (!drained) {
4412 			drain_all_stock(memcg);
4413 			drained = true;
4414 			continue;
4415 		}
4416 
4417 		reclaimed = try_to_free_mem_cgroup_pages(memcg, nr_pages - high,
4418 					GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP, NULL);
4419 
4420 		if (!reclaimed && !nr_retries--)
4421 			break;
4422 	}
4423 out:
4424 	memcg_wb_domain_size_changed(memcg);
4425 	return nbytes;
4426 }
4427 
4428 static int memory_max_show(struct seq_file *m, void *v)
4429 {
4430 	return seq_puts_memcg_tunable(m,
4431 		READ_ONCE(mem_cgroup_from_seq(m)->memory.max));
4432 }
4433 
4434 static ssize_t memory_max_write(struct kernfs_open_file *of,
4435 				char *buf, size_t nbytes, loff_t off)
4436 {
4437 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
4438 	unsigned int nr_reclaims = MAX_RECLAIM_RETRIES;
4439 	bool drained = false;
4440 	unsigned long max;
4441 	int err;
4442 
4443 	buf = strstrip(buf);
4444 	err = page_counter_memparse(buf, "max", &max);
4445 	if (err)
4446 		return err;
4447 
4448 	xchg(&memcg->memory.max, max);
4449 
4450 	if (of->file->f_flags & O_NONBLOCK)
4451 		goto out;
4452 
4453 	for (;;) {
4454 		unsigned long nr_pages = page_counter_read(&memcg->memory);
4455 
4456 		if (nr_pages <= max)
4457 			break;
4458 
4459 		if (signal_pending(current))
4460 			break;
4461 
4462 		if (!drained) {
4463 			drain_all_stock(memcg);
4464 			drained = true;
4465 			continue;
4466 		}
4467 
4468 		if (nr_reclaims) {
4469 			if (!try_to_free_mem_cgroup_pages(memcg, nr_pages - max,
4470 					GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP, NULL))
4471 				nr_reclaims--;
4472 			continue;
4473 		}
4474 
4475 		memcg_memory_event(memcg, MEMCG_OOM);
4476 		if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0))
4477 			break;
4478 		cond_resched();
4479 	}
4480 out:
4481 	memcg_wb_domain_size_changed(memcg);
4482 	return nbytes;
4483 }
4484 
4485 /*
4486  * Note: don't forget to update the 'samples/cgroup/memcg_event_listener'
4487  * if any new events become available.
4488  */
4489 static void __memory_events_show(struct seq_file *m, atomic_long_t *events)
4490 {
4491 	seq_printf(m, "low %lu\n", atomic_long_read(&events[MEMCG_LOW]));
4492 	seq_printf(m, "high %lu\n", atomic_long_read(&events[MEMCG_HIGH]));
4493 	seq_printf(m, "max %lu\n", atomic_long_read(&events[MEMCG_MAX]));
4494 	seq_printf(m, "oom %lu\n", atomic_long_read(&events[MEMCG_OOM]));
4495 	seq_printf(m, "oom_kill %lu\n",
4496 		   atomic_long_read(&events[MEMCG_OOM_KILL]));
4497 	seq_printf(m, "oom_group_kill %lu\n",
4498 		   atomic_long_read(&events[MEMCG_OOM_GROUP_KILL]));
4499 	seq_printf(m, "sock_throttled %lu\n",
4500 		   atomic_long_read(&events[MEMCG_SOCK_THROTTLED]));
4501 }
4502 
4503 static int memory_events_show(struct seq_file *m, void *v)
4504 {
4505 	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
4506 
4507 	__memory_events_show(m, memcg->memory_events);
4508 	return 0;
4509 }
4510 
4511 static int memory_events_local_show(struct seq_file *m, void *v)
4512 {
4513 	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
4514 
4515 	__memory_events_show(m, memcg->memory_events_local);
4516 	return 0;
4517 }
4518 
4519 int memory_stat_show(struct seq_file *m, void *v)
4520 {
4521 	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
4522 	char *buf = kmalloc(SEQ_BUF_SIZE, GFP_KERNEL);
4523 	struct seq_buf s;
4524 
4525 	if (!buf)
4526 		return -ENOMEM;
4527 	seq_buf_init(&s, buf, SEQ_BUF_SIZE);
4528 	memory_stat_format(memcg, &s);
4529 	seq_puts(m, buf);
4530 	kfree(buf);
4531 	return 0;
4532 }
4533 
4534 #ifdef CONFIG_NUMA
4535 static inline unsigned long lruvec_page_state_output(struct lruvec *lruvec,
4536 						     int item)
4537 {
4538 	return lruvec_page_state(lruvec, item) *
4539 		memcg_page_state_output_unit(item);
4540 }
4541 
4542 static int memory_numa_stat_show(struct seq_file *m, void *v)
4543 {
4544 	int i;
4545 	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
4546 
4547 	mem_cgroup_flush_stats(memcg);
4548 
4549 	for (i = 0; i < ARRAY_SIZE(memory_stats); i++) {
4550 		int nid;
4551 
4552 		if (memory_stats[i].idx >= NR_VM_NODE_STAT_ITEMS)
4553 			continue;
4554 
4555 		seq_printf(m, "%s", memory_stats[i].name);
4556 		for_each_node_state(nid, N_MEMORY) {
4557 			u64 size;
4558 			struct lruvec *lruvec;
4559 
4560 			lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid));
4561 			size = lruvec_page_state_output(lruvec,
4562 							memory_stats[i].idx);
4563 			seq_printf(m, " N%d=%llu", nid, size);
4564 		}
4565 		seq_putc(m, '\n');
4566 	}
4567 
4568 	return 0;
4569 }
4570 #endif
4571 
4572 static int memory_oom_group_show(struct seq_file *m, void *v)
4573 {
4574 	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
4575 
4576 	seq_printf(m, "%d\n", READ_ONCE(memcg->oom_group));
4577 
4578 	return 0;
4579 }
4580 
4581 static ssize_t memory_oom_group_write(struct kernfs_open_file *of,
4582 				      char *buf, size_t nbytes, loff_t off)
4583 {
4584 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
4585 	int ret, oom_group;
4586 
4587 	buf = strstrip(buf);
4588 	if (!buf)
4589 		return -EINVAL;
4590 
4591 	ret = kstrtoint(buf, 0, &oom_group);
4592 	if (ret)
4593 		return ret;
4594 
4595 	if (oom_group != 0 && oom_group != 1)
4596 		return -EINVAL;
4597 
4598 	WRITE_ONCE(memcg->oom_group, oom_group);
4599 
4600 	return nbytes;
4601 }
4602 
4603 static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
4604 			      size_t nbytes, loff_t off)
4605 {
4606 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
4607 	int ret;
4608 
4609 	ret = user_proactive_reclaim(buf, memcg, NULL);
4610 	if (ret)
4611 		return ret;
4612 
4613 	return nbytes;
4614 }
4615 
4616 static struct cftype memory_files[] = {
4617 	{
4618 		.name = "current",
4619 		.flags = CFTYPE_NOT_ON_ROOT,
4620 		.read_u64 = memory_current_read,
4621 	},
4622 	{
4623 		.name = "peak",
4624 		.flags = CFTYPE_NOT_ON_ROOT,
4625 		.open = peak_open,
4626 		.release = peak_release,
4627 		.seq_show = memory_peak_show,
4628 		.write = memory_peak_write,
4629 	},
4630 	{
4631 		.name = "min",
4632 		.flags = CFTYPE_NOT_ON_ROOT,
4633 		.seq_show = memory_min_show,
4634 		.write = memory_min_write,
4635 	},
4636 	{
4637 		.name = "low",
4638 		.flags = CFTYPE_NOT_ON_ROOT,
4639 		.seq_show = memory_low_show,
4640 		.write = memory_low_write,
4641 	},
4642 	{
4643 		.name = "high",
4644 		.flags = CFTYPE_NOT_ON_ROOT,
4645 		.seq_show = memory_high_show,
4646 		.write = memory_high_write,
4647 	},
4648 	{
4649 		.name = "max",
4650 		.flags = CFTYPE_NOT_ON_ROOT,
4651 		.seq_show = memory_max_show,
4652 		.write = memory_max_write,
4653 	},
4654 	{
4655 		.name = "events",
4656 		.flags = CFTYPE_NOT_ON_ROOT,
4657 		.file_offset = offsetof(struct mem_cgroup, events_file),
4658 		.seq_show = memory_events_show,
4659 	},
4660 	{
4661 		.name = "events.local",
4662 		.flags = CFTYPE_NOT_ON_ROOT,
4663 		.file_offset = offsetof(struct mem_cgroup, events_local_file),
4664 		.seq_show = memory_events_local_show,
4665 	},
4666 	{
4667 		.name = "stat",
4668 		.seq_show = memory_stat_show,
4669 	},
4670 #ifdef CONFIG_NUMA
4671 	{
4672 		.name = "numa_stat",
4673 		.seq_show = memory_numa_stat_show,
4674 	},
4675 #endif
4676 	{
4677 		.name = "oom.group",
4678 		.flags = CFTYPE_NOT_ON_ROOT | CFTYPE_NS_DELEGATABLE,
4679 		.seq_show = memory_oom_group_show,
4680 		.write = memory_oom_group_write,
4681 	},
4682 	{
4683 		.name = "reclaim",
4684 		.flags = CFTYPE_NS_DELEGATABLE,
4685 		.write = memory_reclaim,
4686 	},
4687 	{ }	/* terminate */
4688 };
4689 
4690 struct cgroup_subsys memory_cgrp_subsys = {
4691 	.css_alloc = mem_cgroup_css_alloc,
4692 	.css_online = mem_cgroup_css_online,
4693 	.css_offline = mem_cgroup_css_offline,
4694 	.css_released = mem_cgroup_css_released,
4695 	.css_free = mem_cgroup_css_free,
4696 	.css_reset = mem_cgroup_css_reset,
4697 	.css_rstat_flush = mem_cgroup_css_rstat_flush,
4698 	.attach = mem_cgroup_attach,
4699 	.fork = mem_cgroup_fork,
4700 	.exit = mem_cgroup_exit,
4701 	.dfl_cftypes = memory_files,
4702 #ifdef CONFIG_MEMCG_V1
4703 	.legacy_cftypes = mem_cgroup_legacy_files,
4704 #endif
4705 	.early_init = 0,
4706 };
4707 
4708 /**
4709  * mem_cgroup_calculate_protection - check if memory consumption is in the normal range
4710  * @root: the top ancestor of the sub-tree being checked
4711  * @memcg: the memory cgroup to check
4712  *
4713  * WARNING: This function is not stateless! It can only be used as part
4714  *          of a top-down tree iteration, not for isolated queries.
4715  */
4716 void mem_cgroup_calculate_protection(struct mem_cgroup *root,
4717 				     struct mem_cgroup *memcg)
4718 {
4719 	bool recursive_protection =
4720 		cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT;
4721 
4722 	if (mem_cgroup_disabled())
4723 		return;
4724 
4725 	if (!root)
4726 		root = root_mem_cgroup;
4727 
4728 	page_counter_calculate_protection(&root->memory, &memcg->memory, recursive_protection);
4729 }
4730 
4731 static int charge_memcg(struct folio *folio, struct mem_cgroup *memcg,
4732 			gfp_t gfp)
4733 {
4734 	int ret;
4735 
4736 	ret = try_charge(memcg, gfp, folio_nr_pages(folio));
4737 	if (ret)
4738 		goto out;
4739 
4740 	css_get(&memcg->css);
4741 	commit_charge(folio, memcg);
4742 	memcg1_commit_charge(folio, memcg);
4743 out:
4744 	return ret;
4745 }
4746 
4747 int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp)
4748 {
4749 	struct mem_cgroup *memcg;
4750 	int ret;
4751 
4752 	memcg = get_mem_cgroup_from_mm(mm);
4753 	ret = charge_memcg(folio, memcg, gfp);
4754 	css_put(&memcg->css);
4755 
4756 	return ret;
4757 }
4758 
4759 /**
4760  * mem_cgroup_charge_hugetlb - charge the memcg for a hugetlb folio
4761  * @folio: folio being charged
4762  * @gfp: reclaim mode
4763  *
4764  * This function is called when allocating a huge page folio, after the page has
4765  * already been obtained and charged to the appropriate hugetlb cgroup
4766  * controller (if it is enabled).
4767  *
4768  * Returns ENOMEM if the memcg is already full.
4769  * Returns 0 if either the charge was successful, or if we skip the charging.
4770  */
4771 int mem_cgroup_charge_hugetlb(struct folio *folio, gfp_t gfp)
4772 {
4773 	struct mem_cgroup *memcg = get_mem_cgroup_from_current();
4774 	int ret = 0;
4775 
4776 	/*
4777 	 * Even memcg does not account for hugetlb, we still want to update
4778 	 * system-level stats via lruvec_stat_mod_folio. Return 0, and skip
4779 	 * charging the memcg.
4780 	 */
4781 	if (mem_cgroup_disabled() || !memcg_accounts_hugetlb() ||
4782 		!memcg || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
4783 		goto out;
4784 
4785 	if (charge_memcg(folio, memcg, gfp))
4786 		ret = -ENOMEM;
4787 
4788 out:
4789 	mem_cgroup_put(memcg);
4790 	return ret;
4791 }
4792 
4793 /**
4794  * mem_cgroup_swapin_charge_folio - Charge a newly allocated folio for swapin.
4795  * @folio: folio to charge.
4796  * @mm: mm context of the victim
4797  * @gfp: reclaim mode
4798  * @entry: swap entry for which the folio is allocated
4799  *
4800  * This function charges a folio allocated for swapin. Please call this before
4801  * adding the folio to the swapcache.
4802  *
4803  * Returns 0 on success. Otherwise, an error code is returned.
4804  */
4805 int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm,
4806 				  gfp_t gfp, swp_entry_t entry)
4807 {
4808 	struct mem_cgroup *memcg;
4809 	unsigned short id;
4810 	int ret;
4811 
4812 	if (mem_cgroup_disabled())
4813 		return 0;
4814 
4815 	id = lookup_swap_cgroup_id(entry);
4816 	rcu_read_lock();
4817 	memcg = mem_cgroup_from_id(id);
4818 	if (!memcg || !css_tryget_online(&memcg->css))
4819 		memcg = get_mem_cgroup_from_mm(mm);
4820 	rcu_read_unlock();
4821 
4822 	ret = charge_memcg(folio, memcg, gfp);
4823 
4824 	css_put(&memcg->css);
4825 	return ret;
4826 }
4827 
4828 struct uncharge_gather {
4829 	struct mem_cgroup *memcg;
4830 	unsigned long nr_memory;
4831 	unsigned long pgpgout;
4832 	unsigned long nr_kmem;
4833 	int nid;
4834 };
4835 
4836 static inline void uncharge_gather_clear(struct uncharge_gather *ug)
4837 {
4838 	memset(ug, 0, sizeof(*ug));
4839 }
4840 
4841 static void uncharge_batch(const struct uncharge_gather *ug)
4842 {
4843 	if (ug->nr_memory) {
4844 		memcg_uncharge(ug->memcg, ug->nr_memory);
4845 		if (ug->nr_kmem) {
4846 			mod_memcg_state(ug->memcg, MEMCG_KMEM, -ug->nr_kmem);
4847 			memcg1_account_kmem(ug->memcg, -ug->nr_kmem);
4848 		}
4849 		memcg1_oom_recover(ug->memcg);
4850 	}
4851 
4852 	memcg1_uncharge_batch(ug->memcg, ug->pgpgout, ug->nr_memory, ug->nid);
4853 
4854 	/* drop reference from uncharge_folio */
4855 	css_put(&ug->memcg->css);
4856 }
4857 
4858 static void uncharge_folio(struct folio *folio, struct uncharge_gather *ug)
4859 {
4860 	long nr_pages;
4861 	struct mem_cgroup *memcg;
4862 	struct obj_cgroup *objcg;
4863 
4864 	VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
4865 
4866 	/*
4867 	 * Nobody should be changing or seriously looking at
4868 	 * folio memcg or objcg at this point, we have fully
4869 	 * exclusive access to the folio.
4870 	 */
4871 	if (folio_memcg_kmem(folio)) {
4872 		objcg = __folio_objcg(folio);
4873 		/*
4874 		 * This get matches the put at the end of the function and
4875 		 * kmem pages do not hold memcg references anymore.
4876 		 */
4877 		memcg = get_mem_cgroup_from_objcg(objcg);
4878 	} else {
4879 		memcg = __folio_memcg(folio);
4880 	}
4881 
4882 	if (!memcg)
4883 		return;
4884 
4885 	if (ug->memcg != memcg) {
4886 		if (ug->memcg) {
4887 			uncharge_batch(ug);
4888 			uncharge_gather_clear(ug);
4889 		}
4890 		ug->memcg = memcg;
4891 		ug->nid = folio_nid(folio);
4892 
4893 		/* pairs with css_put in uncharge_batch */
4894 		css_get(&memcg->css);
4895 	}
4896 
4897 	nr_pages = folio_nr_pages(folio);
4898 
4899 	if (folio_memcg_kmem(folio)) {
4900 		ug->nr_memory += nr_pages;
4901 		ug->nr_kmem += nr_pages;
4902 
4903 		folio->memcg_data = 0;
4904 		obj_cgroup_put(objcg);
4905 	} else {
4906 		/* LRU pages aren't accounted at the root level */
4907 		if (!mem_cgroup_is_root(memcg))
4908 			ug->nr_memory += nr_pages;
4909 		ug->pgpgout++;
4910 
4911 		WARN_ON_ONCE(folio_unqueue_deferred_split(folio));
4912 		folio->memcg_data = 0;
4913 	}
4914 
4915 	css_put(&memcg->css);
4916 }
4917 
4918 void __mem_cgroup_uncharge(struct folio *folio)
4919 {
4920 	struct uncharge_gather ug;
4921 
4922 	/* Don't touch folio->lru of any random page, pre-check: */
4923 	if (!folio_memcg_charged(folio))
4924 		return;
4925 
4926 	uncharge_gather_clear(&ug);
4927 	uncharge_folio(folio, &ug);
4928 	uncharge_batch(&ug);
4929 }
4930 
4931 void __mem_cgroup_uncharge_folios(struct folio_batch *folios)
4932 {
4933 	struct uncharge_gather ug;
4934 	unsigned int i;
4935 
4936 	uncharge_gather_clear(&ug);
4937 	for (i = 0; i < folios->nr; i++)
4938 		uncharge_folio(folios->folios[i], &ug);
4939 	if (ug.memcg)
4940 		uncharge_batch(&ug);
4941 }
4942 
4943 /**
4944  * mem_cgroup_replace_folio - Charge a folio's replacement.
4945  * @old: Currently circulating folio.
4946  * @new: Replacement folio.
4947  *
4948  * Charge @new as a replacement folio for @old. @old will
4949  * be uncharged upon free.
4950  *
4951  * Both folios must be locked, @new->mapping must be set up.
4952  */
4953 void mem_cgroup_replace_folio(struct folio *old, struct folio *new)
4954 {
4955 	struct mem_cgroup *memcg;
4956 	long nr_pages = folio_nr_pages(new);
4957 
4958 	VM_BUG_ON_FOLIO(!folio_test_locked(old), old);
4959 	VM_BUG_ON_FOLIO(!folio_test_locked(new), new);
4960 	VM_BUG_ON_FOLIO(folio_test_anon(old) != folio_test_anon(new), new);
4961 	VM_BUG_ON_FOLIO(folio_nr_pages(old) != nr_pages, new);
4962 
4963 	if (mem_cgroup_disabled())
4964 		return;
4965 
4966 	/* Page cache replacement: new folio already charged? */
4967 	if (folio_memcg_charged(new))
4968 		return;
4969 
4970 	memcg = folio_memcg(old);
4971 	VM_WARN_ON_ONCE_FOLIO(!memcg, old);
4972 	if (!memcg)
4973 		return;
4974 
4975 	/* Force-charge the new page. The old one will be freed soon */
4976 	if (!mem_cgroup_is_root(memcg)) {
4977 		page_counter_charge(&memcg->memory, nr_pages);
4978 		if (do_memsw_account())
4979 			page_counter_charge(&memcg->memsw, nr_pages);
4980 	}
4981 
4982 	css_get(&memcg->css);
4983 	commit_charge(new, memcg);
4984 	memcg1_commit_charge(new, memcg);
4985 }
4986 
4987 /**
4988  * mem_cgroup_migrate - Transfer the memcg data from the old to the new folio.
4989  * @old: Currently circulating folio.
4990  * @new: Replacement folio.
4991  *
4992  * Transfer the memcg data from the old folio to the new folio for migration.
4993  * The old folio's data info will be cleared. Note that the memory counters
4994  * will remain unchanged throughout the process.
4995  *
4996  * Both folios must be locked, @new->mapping must be set up.
4997  */
4998 void mem_cgroup_migrate(struct folio *old, struct folio *new)
4999 {
5000 	struct mem_cgroup *memcg;
5001 
5002 	VM_BUG_ON_FOLIO(!folio_test_locked(old), old);
5003 	VM_BUG_ON_FOLIO(!folio_test_locked(new), new);
5004 	VM_BUG_ON_FOLIO(folio_test_anon(old) != folio_test_anon(new), new);
5005 	VM_BUG_ON_FOLIO(folio_nr_pages(old) != folio_nr_pages(new), new);
5006 	VM_BUG_ON_FOLIO(folio_test_lru(old), old);
5007 
5008 	if (mem_cgroup_disabled())
5009 		return;
5010 
5011 	memcg = folio_memcg(old);
5012 	/*
5013 	 * Note that it is normal to see !memcg for a hugetlb folio.
5014 	 * For e.g, itt could have been allocated when memory_hugetlb_accounting
5015 	 * was not selected.
5016 	 */
5017 	VM_WARN_ON_ONCE_FOLIO(!folio_test_hugetlb(old) && !memcg, old);
5018 	if (!memcg)
5019 		return;
5020 
5021 	/* Transfer the charge and the css ref */
5022 	commit_charge(new, memcg);
5023 
5024 	/* Warning should never happen, so don't worry about refcount non-0 */
5025 	WARN_ON_ONCE(folio_unqueue_deferred_split(old));
5026 	old->memcg_data = 0;
5027 }
5028 
5029 DEFINE_STATIC_KEY_FALSE(memcg_sockets_enabled_key);
5030 EXPORT_SYMBOL(memcg_sockets_enabled_key);
5031 
5032 void mem_cgroup_sk_alloc(struct sock *sk)
5033 {
5034 	struct mem_cgroup *memcg;
5035 
5036 	if (!mem_cgroup_sockets_enabled)
5037 		return;
5038 
5039 	/* Do not associate the sock with unrelated interrupted task's memcg. */
5040 	if (!in_task())
5041 		return;
5042 
5043 	rcu_read_lock();
5044 	memcg = mem_cgroup_from_task(current);
5045 	if (mem_cgroup_is_root(memcg))
5046 		goto out;
5047 	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !memcg1_tcpmem_active(memcg))
5048 		goto out;
5049 	if (css_tryget(&memcg->css))
5050 		sk->sk_memcg = memcg;
5051 out:
5052 	rcu_read_unlock();
5053 }
5054 
5055 void mem_cgroup_sk_free(struct sock *sk)
5056 {
5057 	struct mem_cgroup *memcg = mem_cgroup_from_sk(sk);
5058 
5059 	if (memcg)
5060 		css_put(&memcg->css);
5061 }
5062 
5063 void mem_cgroup_sk_inherit(const struct sock *sk, struct sock *newsk)
5064 {
5065 	struct mem_cgroup *memcg;
5066 
5067 	if (sk->sk_memcg == newsk->sk_memcg)
5068 		return;
5069 
5070 	mem_cgroup_sk_free(newsk);
5071 
5072 	memcg = mem_cgroup_from_sk(sk);
5073 	if (memcg)
5074 		css_get(&memcg->css);
5075 
5076 	newsk->sk_memcg = sk->sk_memcg;
5077 }
5078 
5079 /**
5080  * mem_cgroup_sk_charge - charge socket memory
5081  * @sk: socket in memcg to charge
5082  * @nr_pages: number of pages to charge
5083  * @gfp_mask: reclaim mode
5084  *
5085  * Charges @nr_pages to @memcg. Returns %true if the charge fit within
5086  * @memcg's configured limit, %false if it doesn't.
5087  */
5088 bool mem_cgroup_sk_charge(const struct sock *sk, unsigned int nr_pages,
5089 			  gfp_t gfp_mask)
5090 {
5091 	struct mem_cgroup *memcg = mem_cgroup_from_sk(sk);
5092 
5093 	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
5094 		return memcg1_charge_skmem(memcg, nr_pages, gfp_mask);
5095 
5096 	if (try_charge_memcg(memcg, gfp_mask, nr_pages) == 0) {
5097 		mod_memcg_state(memcg, MEMCG_SOCK, nr_pages);
5098 		return true;
5099 	}
5100 
5101 	return false;
5102 }
5103 
5104 /**
5105  * mem_cgroup_sk_uncharge - uncharge socket memory
5106  * @sk: socket in memcg to uncharge
5107  * @nr_pages: number of pages to uncharge
5108  */
5109 void mem_cgroup_sk_uncharge(const struct sock *sk, unsigned int nr_pages)
5110 {
5111 	struct mem_cgroup *memcg = mem_cgroup_from_sk(sk);
5112 
5113 	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
5114 		memcg1_uncharge_skmem(memcg, nr_pages);
5115 		return;
5116 	}
5117 
5118 	mod_memcg_state(memcg, MEMCG_SOCK, -nr_pages);
5119 
5120 	refill_stock(memcg, nr_pages);
5121 }
5122 
5123 static int __init cgroup_memory(char *s)
5124 {
5125 	char *token;
5126 
5127 	while ((token = strsep(&s, ",")) != NULL) {
5128 		if (!*token)
5129 			continue;
5130 		if (!strcmp(token, "nosocket"))
5131 			cgroup_memory_nosocket = true;
5132 		if (!strcmp(token, "nokmem"))
5133 			cgroup_memory_nokmem = true;
5134 		if (!strcmp(token, "nobpf"))
5135 			cgroup_memory_nobpf = true;
5136 	}
5137 	return 1;
5138 }
5139 __setup("cgroup.memory=", cgroup_memory);
5140 
5141 /*
5142  * Memory controller init before cgroup_init() initialize root_mem_cgroup.
5143  *
5144  * Some parts like memcg_hotplug_cpu_dead() have to be initialized from this
5145  * context because of lock dependencies (cgroup_lock -> cpu hotplug) but
5146  * basically everything that doesn't depend on a specific mem_cgroup structure
5147  * should be initialized from here.
5148  */
5149 int __init mem_cgroup_init(void)
5150 {
5151 	unsigned int memcg_size;
5152 	int cpu;
5153 
5154 	/*
5155 	 * Currently s32 type (can refer to struct batched_lruvec_stat) is
5156 	 * used for per-memcg-per-cpu caching of per-node statistics. In order
5157 	 * to work fine, we should make sure that the overfill threshold can't
5158 	 * exceed S32_MAX / PAGE_SIZE.
5159 	 */
5160 	BUILD_BUG_ON(MEMCG_CHARGE_BATCH > S32_MAX / PAGE_SIZE);
5161 
5162 	cpuhp_setup_state_nocalls(CPUHP_MM_MEMCQ_DEAD, "mm/memctrl:dead", NULL,
5163 				  memcg_hotplug_cpu_dead);
5164 
5165 	for_each_possible_cpu(cpu) {
5166 		INIT_WORK(&per_cpu_ptr(&memcg_stock, cpu)->work,
5167 			  drain_local_memcg_stock);
5168 		INIT_WORK(&per_cpu_ptr(&obj_stock, cpu)->work,
5169 			  drain_local_obj_stock);
5170 	}
5171 
5172 	memcg_size = struct_size_t(struct mem_cgroup, nodeinfo, nr_node_ids);
5173 	memcg_cachep = kmem_cache_create("mem_cgroup", memcg_size, 0,
5174 					 SLAB_PANIC | SLAB_HWCACHE_ALIGN, NULL);
5175 
5176 	memcg_pn_cachep = KMEM_CACHE(mem_cgroup_per_node,
5177 				     SLAB_PANIC | SLAB_HWCACHE_ALIGN);
5178 
5179 	return 0;
5180 }
5181 
5182 #ifdef CONFIG_SWAP
5183 /**
5184  * __mem_cgroup_try_charge_swap - try charging swap space for a folio
5185  * @folio: folio being added to swap
5186  * @entry: swap entry to charge
5187  *
5188  * Try to charge @folio's memcg for the swap space at @entry.
5189  *
5190  * Returns 0 on success, -ENOMEM on failure.
5191  */
5192 int __mem_cgroup_try_charge_swap(struct folio *folio, swp_entry_t entry)
5193 {
5194 	unsigned int nr_pages = folio_nr_pages(folio);
5195 	struct page_counter *counter;
5196 	struct mem_cgroup *memcg;
5197 
5198 	if (do_memsw_account())
5199 		return 0;
5200 
5201 	memcg = folio_memcg(folio);
5202 
5203 	VM_WARN_ON_ONCE_FOLIO(!memcg, folio);
5204 	if (!memcg)
5205 		return 0;
5206 
5207 	if (!entry.val) {
5208 		memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
5209 		return 0;
5210 	}
5211 
5212 	memcg = mem_cgroup_id_get_online(memcg);
5213 
5214 	if (!mem_cgroup_is_root(memcg) &&
5215 	    !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) {
5216 		memcg_memory_event(memcg, MEMCG_SWAP_MAX);
5217 		memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
5218 		mem_cgroup_id_put(memcg);
5219 		return -ENOMEM;
5220 	}
5221 
5222 	/* Get references for the tail pages, too */
5223 	if (nr_pages > 1)
5224 		mem_cgroup_id_get_many(memcg, nr_pages - 1);
5225 	mod_memcg_state(memcg, MEMCG_SWAP, nr_pages);
5226 
5227 	swap_cgroup_record(folio, mem_cgroup_id(memcg), entry);
5228 
5229 	return 0;
5230 }
5231 
5232 /**
5233  * __mem_cgroup_uncharge_swap - uncharge swap space
5234  * @entry: swap entry to uncharge
5235  * @nr_pages: the amount of swap space to uncharge
5236  */
5237 void __mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_pages)
5238 {
5239 	struct mem_cgroup *memcg;
5240 	unsigned short id;
5241 
5242 	id = swap_cgroup_clear(entry, nr_pages);
5243 	rcu_read_lock();
5244 	memcg = mem_cgroup_from_id(id);
5245 	if (memcg) {
5246 		if (!mem_cgroup_is_root(memcg)) {
5247 			if (do_memsw_account())
5248 				page_counter_uncharge(&memcg->memsw, nr_pages);
5249 			else
5250 				page_counter_uncharge(&memcg->swap, nr_pages);
5251 		}
5252 		mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages);
5253 		mem_cgroup_id_put_many(memcg, nr_pages);
5254 	}
5255 	rcu_read_unlock();
5256 }
5257 
5258 long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)
5259 {
5260 	long nr_swap_pages = get_nr_swap_pages();
5261 
5262 	if (mem_cgroup_disabled() || do_memsw_account())
5263 		return nr_swap_pages;
5264 	for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg))
5265 		nr_swap_pages = min_t(long, nr_swap_pages,
5266 				      READ_ONCE(memcg->swap.max) -
5267 				      page_counter_read(&memcg->swap));
5268 	return nr_swap_pages;
5269 }
5270 
5271 bool mem_cgroup_swap_full(struct folio *folio)
5272 {
5273 	struct mem_cgroup *memcg;
5274 
5275 	VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
5276 
5277 	if (vm_swap_full())
5278 		return true;
5279 	if (do_memsw_account())
5280 		return false;
5281 
5282 	memcg = folio_memcg(folio);
5283 	if (!memcg)
5284 		return false;
5285 
5286 	for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) {
5287 		unsigned long usage = page_counter_read(&memcg->swap);
5288 
5289 		if (usage * 2 >= READ_ONCE(memcg->swap.high) ||
5290 		    usage * 2 >= READ_ONCE(memcg->swap.max))
5291 			return true;
5292 	}
5293 
5294 	return false;
5295 }
5296 
5297 static int __init setup_swap_account(char *s)
5298 {
5299 	bool res;
5300 
5301 	if (!kstrtobool(s, &res) && !res)
5302 		pr_warn_once("The swapaccount=0 commandline option is deprecated "
5303 			     "in favor of configuring swap control via cgroupfs. "
5304 			     "Please report your usecase to linux-mm@kvack.org if you "
5305 			     "depend on this functionality.\n");
5306 	return 1;
5307 }
5308 __setup("swapaccount=", setup_swap_account);
5309 
5310 static u64 swap_current_read(struct cgroup_subsys_state *css,
5311 			     struct cftype *cft)
5312 {
5313 	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5314 
5315 	return (u64)page_counter_read(&memcg->swap) * PAGE_SIZE;
5316 }
5317 
5318 static int swap_peak_show(struct seq_file *sf, void *v)
5319 {
5320 	struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf));
5321 
5322 	return peak_show(sf, v, &memcg->swap);
5323 }
5324 
5325 static ssize_t swap_peak_write(struct kernfs_open_file *of, char *buf,
5326 			       size_t nbytes, loff_t off)
5327 {
5328 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5329 
5330 	return peak_write(of, buf, nbytes, off, &memcg->swap,
5331 			  &memcg->swap_peaks);
5332 }
5333 
5334 static int swap_high_show(struct seq_file *m, void *v)
5335 {
5336 	return seq_puts_memcg_tunable(m,
5337 		READ_ONCE(mem_cgroup_from_seq(m)->swap.high));
5338 }
5339 
5340 static ssize_t swap_high_write(struct kernfs_open_file *of,
5341 			       char *buf, size_t nbytes, loff_t off)
5342 {
5343 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5344 	unsigned long high;
5345 	int err;
5346 
5347 	buf = strstrip(buf);
5348 	err = page_counter_memparse(buf, "max", &high);
5349 	if (err)
5350 		return err;
5351 
5352 	page_counter_set_high(&memcg->swap, high);
5353 
5354 	return nbytes;
5355 }
5356 
5357 static int swap_max_show(struct seq_file *m, void *v)
5358 {
5359 	return seq_puts_memcg_tunable(m,
5360 		READ_ONCE(mem_cgroup_from_seq(m)->swap.max));
5361 }
5362 
5363 static ssize_t swap_max_write(struct kernfs_open_file *of,
5364 			      char *buf, size_t nbytes, loff_t off)
5365 {
5366 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5367 	unsigned long max;
5368 	int err;
5369 
5370 	buf = strstrip(buf);
5371 	err = page_counter_memparse(buf, "max", &max);
5372 	if (err)
5373 		return err;
5374 
5375 	xchg(&memcg->swap.max, max);
5376 
5377 	return nbytes;
5378 }
5379 
5380 static int swap_events_show(struct seq_file *m, void *v)
5381 {
5382 	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
5383 
5384 	seq_printf(m, "high %lu\n",
5385 		   atomic_long_read(&memcg->memory_events[MEMCG_SWAP_HIGH]));
5386 	seq_printf(m, "max %lu\n",
5387 		   atomic_long_read(&memcg->memory_events[MEMCG_SWAP_MAX]));
5388 	seq_printf(m, "fail %lu\n",
5389 		   atomic_long_read(&memcg->memory_events[MEMCG_SWAP_FAIL]));
5390 
5391 	return 0;
5392 }
5393 
5394 static struct cftype swap_files[] = {
5395 	{
5396 		.name = "swap.current",
5397 		.flags = CFTYPE_NOT_ON_ROOT,
5398 		.read_u64 = swap_current_read,
5399 	},
5400 	{
5401 		.name = "swap.high",
5402 		.flags = CFTYPE_NOT_ON_ROOT,
5403 		.seq_show = swap_high_show,
5404 		.write = swap_high_write,
5405 	},
5406 	{
5407 		.name = "swap.max",
5408 		.flags = CFTYPE_NOT_ON_ROOT,
5409 		.seq_show = swap_max_show,
5410 		.write = swap_max_write,
5411 	},
5412 	{
5413 		.name = "swap.peak",
5414 		.flags = CFTYPE_NOT_ON_ROOT,
5415 		.open = peak_open,
5416 		.release = peak_release,
5417 		.seq_show = swap_peak_show,
5418 		.write = swap_peak_write,
5419 	},
5420 	{
5421 		.name = "swap.events",
5422 		.flags = CFTYPE_NOT_ON_ROOT,
5423 		.file_offset = offsetof(struct mem_cgroup, swap_events_file),
5424 		.seq_show = swap_events_show,
5425 	},
5426 	{ }	/* terminate */
5427 };
5428 
5429 #ifdef CONFIG_ZSWAP
5430 /**
5431  * obj_cgroup_may_zswap - check if this cgroup can zswap
5432  * @objcg: the object cgroup
5433  *
5434  * Check if the hierarchical zswap limit has been reached.
5435  *
5436  * This doesn't check for specific headroom, and it is not atomic
5437  * either. But with zswap, the size of the allocation is only known
5438  * once compression has occurred, and this optimistic pre-check avoids
5439  * spending cycles on compression when there is already no room left
5440  * or zswap is disabled altogether somewhere in the hierarchy.
5441  */
5442 bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
5443 {
5444 	struct mem_cgroup *memcg, *original_memcg;
5445 	bool ret = true;
5446 
5447 	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
5448 		return true;
5449 
5450 	original_memcg = get_mem_cgroup_from_objcg(objcg);
5451 	for (memcg = original_memcg; !mem_cgroup_is_root(memcg);
5452 	     memcg = parent_mem_cgroup(memcg)) {
5453 		unsigned long max = READ_ONCE(memcg->zswap_max);
5454 		unsigned long pages;
5455 
5456 		if (max == PAGE_COUNTER_MAX)
5457 			continue;
5458 		if (max == 0) {
5459 			ret = false;
5460 			break;
5461 		}
5462 
5463 		/* Force flush to get accurate stats for charging */
5464 		__mem_cgroup_flush_stats(memcg, true);
5465 		pages = memcg_page_state(memcg, MEMCG_ZSWAP_B) / PAGE_SIZE;
5466 		if (pages < max)
5467 			continue;
5468 		ret = false;
5469 		break;
5470 	}
5471 	mem_cgroup_put(original_memcg);
5472 	return ret;
5473 }
5474 
5475 /**
5476  * obj_cgroup_charge_zswap - charge compression backend memory
5477  * @objcg: the object cgroup
5478  * @size: size of compressed object
5479  *
5480  * This forces the charge after obj_cgroup_may_zswap() allowed
5481  * compression and storage in zswap for this cgroup to go ahead.
5482  */
5483 void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size)
5484 {
5485 	struct mem_cgroup *memcg;
5486 
5487 	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
5488 		return;
5489 
5490 	VM_WARN_ON_ONCE(!(current->flags & PF_MEMALLOC));
5491 
5492 	/* PF_MEMALLOC context, charging must succeed */
5493 	if (obj_cgroup_charge(objcg, GFP_KERNEL, size))
5494 		VM_WARN_ON_ONCE(1);
5495 
5496 	rcu_read_lock();
5497 	memcg = obj_cgroup_memcg(objcg);
5498 	mod_memcg_state(memcg, MEMCG_ZSWAP_B, size);
5499 	mod_memcg_state(memcg, MEMCG_ZSWAPPED, 1);
5500 	rcu_read_unlock();
5501 }
5502 
5503 /**
5504  * obj_cgroup_uncharge_zswap - uncharge compression backend memory
5505  * @objcg: the object cgroup
5506  * @size: size of compressed object
5507  *
5508  * Uncharges zswap memory on page in.
5509  */
5510 void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size)
5511 {
5512 	struct mem_cgroup *memcg;
5513 
5514 	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
5515 		return;
5516 
5517 	obj_cgroup_uncharge(objcg, size);
5518 
5519 	rcu_read_lock();
5520 	memcg = obj_cgroup_memcg(objcg);
5521 	mod_memcg_state(memcg, MEMCG_ZSWAP_B, -size);
5522 	mod_memcg_state(memcg, MEMCG_ZSWAPPED, -1);
5523 	rcu_read_unlock();
5524 }
5525 
5526 bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg)
5527 {
5528 	/* if zswap is disabled, do not block pages going to the swapping device */
5529 	if (!zswap_is_enabled())
5530 		return true;
5531 
5532 	for (; memcg; memcg = parent_mem_cgroup(memcg))
5533 		if (!READ_ONCE(memcg->zswap_writeback))
5534 			return false;
5535 
5536 	return true;
5537 }
5538 
5539 static u64 zswap_current_read(struct cgroup_subsys_state *css,
5540 			      struct cftype *cft)
5541 {
5542 	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5543 
5544 	mem_cgroup_flush_stats(memcg);
5545 	return memcg_page_state(memcg, MEMCG_ZSWAP_B);
5546 }
5547 
5548 static int zswap_max_show(struct seq_file *m, void *v)
5549 {
5550 	return seq_puts_memcg_tunable(m,
5551 		READ_ONCE(mem_cgroup_from_seq(m)->zswap_max));
5552 }
5553 
5554 static ssize_t zswap_max_write(struct kernfs_open_file *of,
5555 			       char *buf, size_t nbytes, loff_t off)
5556 {
5557 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5558 	unsigned long max;
5559 	int err;
5560 
5561 	buf = strstrip(buf);
5562 	err = page_counter_memparse(buf, "max", &max);
5563 	if (err)
5564 		return err;
5565 
5566 	xchg(&memcg->zswap_max, max);
5567 
5568 	return nbytes;
5569 }
5570 
5571 static int zswap_writeback_show(struct seq_file *m, void *v)
5572 {
5573 	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
5574 
5575 	seq_printf(m, "%d\n", READ_ONCE(memcg->zswap_writeback));
5576 	return 0;
5577 }
5578 
5579 static ssize_t zswap_writeback_write(struct kernfs_open_file *of,
5580 				char *buf, size_t nbytes, loff_t off)
5581 {
5582 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5583 	int zswap_writeback;
5584 	ssize_t parse_ret = kstrtoint(strstrip(buf), 0, &zswap_writeback);
5585 
5586 	if (parse_ret)
5587 		return parse_ret;
5588 
5589 	if (zswap_writeback != 0 && zswap_writeback != 1)
5590 		return -EINVAL;
5591 
5592 	WRITE_ONCE(memcg->zswap_writeback, zswap_writeback);
5593 	return nbytes;
5594 }
5595 
5596 static struct cftype zswap_files[] = {
5597 	{
5598 		.name = "zswap.current",
5599 		.flags = CFTYPE_NOT_ON_ROOT,
5600 		.read_u64 = zswap_current_read,
5601 	},
5602 	{
5603 		.name = "zswap.max",
5604 		.flags = CFTYPE_NOT_ON_ROOT,
5605 		.seq_show = zswap_max_show,
5606 		.write = zswap_max_write,
5607 	},
5608 	{
5609 		.name = "zswap.writeback",
5610 		.seq_show = zswap_writeback_show,
5611 		.write = zswap_writeback_write,
5612 	},
5613 	{ }	/* terminate */
5614 };
5615 #endif /* CONFIG_ZSWAP */
5616 
5617 static int __init mem_cgroup_swap_init(void)
5618 {
5619 	if (mem_cgroup_disabled())
5620 		return 0;
5621 
5622 	WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, swap_files));
5623 #ifdef CONFIG_MEMCG_V1
5624 	WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, memsw_files));
5625 #endif
5626 #ifdef CONFIG_ZSWAP
5627 	WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, zswap_files));
5628 #endif
5629 	return 0;
5630 }
5631 subsys_initcall(mem_cgroup_swap_init);
5632 
5633 #endif /* CONFIG_SWAP */
5634 
5635 bool mem_cgroup_node_allowed(struct mem_cgroup *memcg, int nid)
5636 {
5637 	return memcg ? cpuset_node_allowed(memcg->css.cgroup, nid) : true;
5638 }
5639 
5640 void mem_cgroup_show_protected_memory(struct mem_cgroup *memcg)
5641 {
5642 	if (mem_cgroup_disabled() || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
5643 		return;
5644 
5645 	if (!memcg)
5646 		memcg = root_mem_cgroup;
5647 
5648 	pr_warn("Memory cgroup min protection %lukB -- low protection %lukB",
5649 		K(atomic_long_read(&memcg->memory.children_min_usage)*PAGE_SIZE),
5650 		K(atomic_long_read(&memcg->memory.children_low_usage)*PAGE_SIZE));
5651 }
5652