xref: /linux-6.15/include/linux/memcontrol.h (revision ee1f9d19)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* memcontrol.h - Memory Controller
3  *
4  * Copyright IBM Corporation, 2007
5  * Author Balbir Singh <[email protected]>
6  *
7  * Copyright 2007 OpenVZ SWsoft Inc
8  * Author: Pavel Emelianov <[email protected]>
9  */
10 
11 #ifndef _LINUX_MEMCONTROL_H
12 #define _LINUX_MEMCONTROL_H
13 #include <linux/cgroup.h>
14 #include <linux/vm_event_item.h>
15 #include <linux/hardirq.h>
16 #include <linux/jump_label.h>
17 #include <linux/page_counter.h>
18 #include <linux/vmpressure.h>
19 #include <linux/eventfd.h>
20 #include <linux/mm.h>
21 #include <linux/vmstat.h>
22 #include <linux/writeback.h>
23 #include <linux/page-flags.h>
24 
25 struct mem_cgroup;
26 struct obj_cgroup;
27 struct page;
28 struct mm_struct;
29 struct kmem_cache;
30 
31 /* Cgroup-specific page state, on top of universal node page state */
32 enum memcg_stat_item {
33 	MEMCG_SWAP = NR_VM_NODE_STAT_ITEMS,
34 	MEMCG_SOCK,
35 	MEMCG_PERCPU_B,
36 	MEMCG_NR_STAT,
37 };
38 
39 enum memcg_memory_event {
40 	MEMCG_LOW,
41 	MEMCG_HIGH,
42 	MEMCG_MAX,
43 	MEMCG_OOM,
44 	MEMCG_OOM_KILL,
45 	MEMCG_SWAP_HIGH,
46 	MEMCG_SWAP_MAX,
47 	MEMCG_SWAP_FAIL,
48 	MEMCG_NR_MEMORY_EVENTS,
49 };
50 
51 struct mem_cgroup_reclaim_cookie {
52 	pg_data_t *pgdat;
53 	unsigned int generation;
54 };
55 
56 #ifdef CONFIG_MEMCG
57 
58 #define MEM_CGROUP_ID_SHIFT	16
59 #define MEM_CGROUP_ID_MAX	USHRT_MAX
60 
61 struct mem_cgroup_id {
62 	int id;
63 	refcount_t ref;
64 };
65 
66 /*
67  * Per memcg event counter is incremented at every pagein/pageout. With THP,
68  * it will be incremented by the number of pages. This counter is used
69  * to trigger some periodic events. This is straightforward and better
70  * than using jiffies etc. to handle periodic memcg event.
71  */
72 enum mem_cgroup_events_target {
73 	MEM_CGROUP_TARGET_THRESH,
74 	MEM_CGROUP_TARGET_SOFTLIMIT,
75 	MEM_CGROUP_NTARGETS,
76 };
77 
78 struct memcg_vmstats_percpu {
79 	/* Local (CPU and cgroup) page state & events */
80 	long			state[MEMCG_NR_STAT];
81 	unsigned long		events[NR_VM_EVENT_ITEMS];
82 
83 	/* Delta calculation for lockless upward propagation */
84 	long			state_prev[MEMCG_NR_STAT];
85 	unsigned long		events_prev[NR_VM_EVENT_ITEMS];
86 
87 	/* Cgroup1: threshold notifications & softlimit tree updates */
88 	unsigned long		nr_page_events;
89 	unsigned long		targets[MEM_CGROUP_NTARGETS];
90 };
91 
92 struct memcg_vmstats {
93 	/* Aggregated (CPU and subtree) page state & events */
94 	long			state[MEMCG_NR_STAT];
95 	unsigned long		events[NR_VM_EVENT_ITEMS];
96 
97 	/* Pending child counts during tree propagation */
98 	long			state_pending[MEMCG_NR_STAT];
99 	unsigned long		events_pending[NR_VM_EVENT_ITEMS];
100 };
101 
102 struct mem_cgroup_reclaim_iter {
103 	struct mem_cgroup *position;
104 	/* scan generation, increased every round-trip */
105 	unsigned int generation;
106 };
107 
108 /*
109  * Bitmap and deferred work of shrinker::id corresponding to memcg-aware
110  * shrinkers, which have elements charged to this memcg.
111  */
112 struct shrinker_info {
113 	struct rcu_head rcu;
114 	atomic_long_t *nr_deferred;
115 	unsigned long *map;
116 };
117 
118 struct lruvec_stats_percpu {
119 	/* Local (CPU and cgroup) state */
120 	long state[NR_VM_NODE_STAT_ITEMS];
121 
122 	/* Delta calculation for lockless upward propagation */
123 	long state_prev[NR_VM_NODE_STAT_ITEMS];
124 };
125 
126 struct lruvec_stats {
127 	/* Aggregated (CPU and subtree) state */
128 	long state[NR_VM_NODE_STAT_ITEMS];
129 
130 	/* Pending child counts during tree propagation */
131 	long state_pending[NR_VM_NODE_STAT_ITEMS];
132 };
133 
134 /*
135  * per-node information in memory controller.
136  */
137 struct mem_cgroup_per_node {
138 	struct lruvec		lruvec;
139 
140 	struct lruvec_stats_percpu __percpu	*lruvec_stats_percpu;
141 	struct lruvec_stats			lruvec_stats;
142 
143 	unsigned long		lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
144 
145 	struct mem_cgroup_reclaim_iter	iter;
146 
147 	struct shrinker_info __rcu	*shrinker_info;
148 
149 	struct rb_node		tree_node;	/* RB tree node */
150 	unsigned long		usage_in_excess;/* Set to the value by which */
151 						/* the soft limit is exceeded*/
152 	bool			on_tree;
153 	struct mem_cgroup	*memcg;		/* Back pointer, we cannot */
154 						/* use container_of	   */
155 };
156 
157 struct mem_cgroup_threshold {
158 	struct eventfd_ctx *eventfd;
159 	unsigned long threshold;
160 };
161 
162 /* For threshold */
163 struct mem_cgroup_threshold_ary {
164 	/* An array index points to threshold just below or equal to usage. */
165 	int current_threshold;
166 	/* Size of entries[] */
167 	unsigned int size;
168 	/* Array of thresholds */
169 	struct mem_cgroup_threshold entries[];
170 };
171 
172 struct mem_cgroup_thresholds {
173 	/* Primary thresholds array */
174 	struct mem_cgroup_threshold_ary *primary;
175 	/*
176 	 * Spare threshold array.
177 	 * This is needed to make mem_cgroup_unregister_event() "never fail".
178 	 * It must be able to store at least primary->size - 1 entries.
179 	 */
180 	struct mem_cgroup_threshold_ary *spare;
181 };
182 
183 enum memcg_kmem_state {
184 	KMEM_NONE,
185 	KMEM_ALLOCATED,
186 	KMEM_ONLINE,
187 };
188 
189 #if defined(CONFIG_SMP)
190 struct memcg_padding {
191 	char x[0];
192 } ____cacheline_internodealigned_in_smp;
193 #define MEMCG_PADDING(name)      struct memcg_padding name
194 #else
195 #define MEMCG_PADDING(name)
196 #endif
197 
198 /*
199  * Remember four most recent foreign writebacks with dirty pages in this
200  * cgroup.  Inode sharing is expected to be uncommon and, even if we miss
201  * one in a given round, we're likely to catch it later if it keeps
202  * foreign-dirtying, so a fairly low count should be enough.
203  *
204  * See mem_cgroup_track_foreign_dirty_slowpath() for details.
205  */
206 #define MEMCG_CGWB_FRN_CNT	4
207 
208 struct memcg_cgwb_frn {
209 	u64 bdi_id;			/* bdi->id of the foreign inode */
210 	int memcg_id;			/* memcg->css.id of foreign inode */
211 	u64 at;				/* jiffies_64 at the time of dirtying */
212 	struct wb_completion done;	/* tracks in-flight foreign writebacks */
213 };
214 
215 /*
216  * Bucket for arbitrarily byte-sized objects charged to a memory
217  * cgroup. The bucket can be reparented in one piece when the cgroup
218  * is destroyed, without having to round up the individual references
219  * of all live memory objects in the wild.
220  */
221 struct obj_cgroup {
222 	struct percpu_ref refcnt;
223 	struct mem_cgroup *memcg;
224 	atomic_t nr_charged_bytes;
225 	union {
226 		struct list_head list;
227 		struct rcu_head rcu;
228 	};
229 };
230 
231 /*
232  * The memory controller data structure. The memory controller controls both
233  * page cache and RSS per cgroup. We would eventually like to provide
234  * statistics based on the statistics developed by Rik Van Riel for clock-pro,
235  * to help the administrator determine what knobs to tune.
236  */
237 struct mem_cgroup {
238 	struct cgroup_subsys_state css;
239 
240 	/* Private memcg ID. Used to ID objects that outlive the cgroup */
241 	struct mem_cgroup_id id;
242 
243 	/* Accounted resources */
244 	struct page_counter memory;		/* Both v1 & v2 */
245 
246 	union {
247 		struct page_counter swap;	/* v2 only */
248 		struct page_counter memsw;	/* v1 only */
249 	};
250 
251 	/* Legacy consumer-oriented counters */
252 	struct page_counter kmem;		/* v1 only */
253 	struct page_counter tcpmem;		/* v1 only */
254 
255 	/* Range enforcement for interrupt charges */
256 	struct work_struct high_work;
257 
258 	unsigned long soft_limit;
259 
260 	/* vmpressure notifications */
261 	struct vmpressure vmpressure;
262 
263 	/*
264 	 * Should the OOM killer kill all belonging tasks, had it kill one?
265 	 */
266 	bool oom_group;
267 
268 	/* protected by memcg_oom_lock */
269 	bool		oom_lock;
270 	int		under_oom;
271 
272 	int	swappiness;
273 	/* OOM-Killer disable */
274 	int		oom_kill_disable;
275 
276 	/* memory.events and memory.events.local */
277 	struct cgroup_file events_file;
278 	struct cgroup_file events_local_file;
279 
280 	/* handle for "memory.swap.events" */
281 	struct cgroup_file swap_events_file;
282 
283 	/* protect arrays of thresholds */
284 	struct mutex thresholds_lock;
285 
286 	/* thresholds for memory usage. RCU-protected */
287 	struct mem_cgroup_thresholds thresholds;
288 
289 	/* thresholds for mem+swap usage. RCU-protected */
290 	struct mem_cgroup_thresholds memsw_thresholds;
291 
292 	/* For oom notifier event fd */
293 	struct list_head oom_notify;
294 
295 	/*
296 	 * Should we move charges of a task when a task is moved into this
297 	 * mem_cgroup ? And what type of charges should we move ?
298 	 */
299 	unsigned long move_charge_at_immigrate;
300 	/* taken only while moving_account > 0 */
301 	spinlock_t		move_lock;
302 	unsigned long		move_lock_flags;
303 
304 	MEMCG_PADDING(_pad1_);
305 
306 	/* memory.stat */
307 	struct memcg_vmstats	vmstats;
308 
309 	/* memory.events */
310 	atomic_long_t		memory_events[MEMCG_NR_MEMORY_EVENTS];
311 	atomic_long_t		memory_events_local[MEMCG_NR_MEMORY_EVENTS];
312 
313 	unsigned long		socket_pressure;
314 
315 	/* Legacy tcp memory accounting */
316 	bool			tcpmem_active;
317 	int			tcpmem_pressure;
318 
319 #ifdef CONFIG_MEMCG_KMEM
320 	int kmemcg_id;
321 	enum memcg_kmem_state kmem_state;
322 	struct obj_cgroup __rcu *objcg;
323 	struct list_head objcg_list; /* list of inherited objcgs */
324 #endif
325 
326 	MEMCG_PADDING(_pad2_);
327 
328 	/*
329 	 * set > 0 if pages under this cgroup are moving to other cgroup.
330 	 */
331 	atomic_t		moving_account;
332 	struct task_struct	*move_lock_task;
333 
334 	struct memcg_vmstats_percpu __percpu *vmstats_percpu;
335 
336 #ifdef CONFIG_CGROUP_WRITEBACK
337 	struct list_head cgwb_list;
338 	struct wb_domain cgwb_domain;
339 	struct memcg_cgwb_frn cgwb_frn[MEMCG_CGWB_FRN_CNT];
340 #endif
341 
342 	/* List of events which userspace want to receive */
343 	struct list_head event_list;
344 	spinlock_t event_list_lock;
345 
346 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
347 	struct deferred_split deferred_split_queue;
348 #endif
349 
350 	struct mem_cgroup_per_node *nodeinfo[];
351 };
352 
353 /*
354  * size of first charge trial. "32" comes from vmscan.c's magic value.
355  * TODO: maybe necessary to use big numbers in big irons.
356  */
357 #define MEMCG_CHARGE_BATCH 32U
358 
359 extern struct mem_cgroup *root_mem_cgroup;
360 
361 enum page_memcg_data_flags {
362 	/* page->memcg_data is a pointer to an objcgs vector */
363 	MEMCG_DATA_OBJCGS = (1UL << 0),
364 	/* page has been accounted as a non-slab kernel page */
365 	MEMCG_DATA_KMEM = (1UL << 1),
366 	/* the next bit after the last actual flag */
367 	__NR_MEMCG_DATA_FLAGS  = (1UL << 2),
368 };
369 
370 #define MEMCG_DATA_FLAGS_MASK (__NR_MEMCG_DATA_FLAGS - 1)
371 
372 static inline bool folio_memcg_kmem(struct folio *folio);
373 
374 /*
375  * After the initialization objcg->memcg is always pointing at
376  * a valid memcg, but can be atomically swapped to the parent memcg.
377  *
378  * The caller must ensure that the returned memcg won't be released:
379  * e.g. acquire the rcu_read_lock or css_set_lock.
380  */
381 static inline struct mem_cgroup *obj_cgroup_memcg(struct obj_cgroup *objcg)
382 {
383 	return READ_ONCE(objcg->memcg);
384 }
385 
386 /*
387  * __folio_memcg - Get the memory cgroup associated with a non-kmem folio
388  * @folio: Pointer to the folio.
389  *
390  * Returns a pointer to the memory cgroup associated with the folio,
391  * or NULL. This function assumes that the folio is known to have a
392  * proper memory cgroup pointer. It's not safe to call this function
393  * against some type of folios, e.g. slab folios or ex-slab folios or
394  * kmem folios.
395  */
396 static inline struct mem_cgroup *__folio_memcg(struct folio *folio)
397 {
398 	unsigned long memcg_data = folio->memcg_data;
399 
400 	VM_BUG_ON_FOLIO(folio_test_slab(folio), folio);
401 	VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_OBJCGS, folio);
402 	VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_KMEM, folio);
403 
404 	return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
405 }
406 
407 /*
408  * __folio_objcg - get the object cgroup associated with a kmem folio.
409  * @folio: Pointer to the folio.
410  *
411  * Returns a pointer to the object cgroup associated with the folio,
412  * or NULL. This function assumes that the folio is known to have a
413  * proper object cgroup pointer. It's not safe to call this function
414  * against some type of folios, e.g. slab folios or ex-slab folios or
415  * LRU folios.
416  */
417 static inline struct obj_cgroup *__folio_objcg(struct folio *folio)
418 {
419 	unsigned long memcg_data = folio->memcg_data;
420 
421 	VM_BUG_ON_FOLIO(folio_test_slab(folio), folio);
422 	VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_OBJCGS, folio);
423 	VM_BUG_ON_FOLIO(!(memcg_data & MEMCG_DATA_KMEM), folio);
424 
425 	return (struct obj_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
426 }
427 
428 /*
429  * folio_memcg - Get the memory cgroup associated with a folio.
430  * @folio: Pointer to the folio.
431  *
432  * Returns a pointer to the memory cgroup associated with the folio,
433  * or NULL. This function assumes that the folio is known to have a
434  * proper memory cgroup pointer. It's not safe to call this function
435  * against some type of folios, e.g. slab folios or ex-slab folios.
436  *
437  * For a non-kmem folio any of the following ensures folio and memcg binding
438  * stability:
439  *
440  * - the folio lock
441  * - LRU isolation
442  * - lock_page_memcg()
443  * - exclusive reference
444  *
445  * For a kmem folio a caller should hold an rcu read lock to protect memcg
446  * associated with a kmem folio from being released.
447  */
448 static inline struct mem_cgroup *folio_memcg(struct folio *folio)
449 {
450 	if (folio_memcg_kmem(folio))
451 		return obj_cgroup_memcg(__folio_objcg(folio));
452 	return __folio_memcg(folio);
453 }
454 
455 static inline struct mem_cgroup *page_memcg(struct page *page)
456 {
457 	return folio_memcg(page_folio(page));
458 }
459 
460 /**
461  * folio_memcg_rcu - Locklessly get the memory cgroup associated with a folio.
462  * @folio: Pointer to the folio.
463  *
464  * This function assumes that the folio is known to have a
465  * proper memory cgroup pointer. It's not safe to call this function
466  * against some type of folios, e.g. slab folios or ex-slab folios.
467  *
468  * Return: A pointer to the memory cgroup associated with the folio,
469  * or NULL.
470  */
471 static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)
472 {
473 	unsigned long memcg_data = READ_ONCE(folio->memcg_data);
474 
475 	VM_BUG_ON_FOLIO(folio_test_slab(folio), folio);
476 	WARN_ON_ONCE(!rcu_read_lock_held());
477 
478 	if (memcg_data & MEMCG_DATA_KMEM) {
479 		struct obj_cgroup *objcg;
480 
481 		objcg = (void *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
482 		return obj_cgroup_memcg(objcg);
483 	}
484 
485 	return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
486 }
487 
488 /*
489  * page_memcg_check - get the memory cgroup associated with a page
490  * @page: a pointer to the page struct
491  *
492  * Returns a pointer to the memory cgroup associated with the page,
493  * or NULL. This function unlike page_memcg() can take any page
494  * as an argument. It has to be used in cases when it's not known if a page
495  * has an associated memory cgroup pointer or an object cgroups vector or
496  * an object cgroup.
497  *
498  * For a non-kmem page any of the following ensures page and memcg binding
499  * stability:
500  *
501  * - the page lock
502  * - LRU isolation
503  * - lock_page_memcg()
504  * - exclusive reference
505  *
506  * For a kmem page a caller should hold an rcu read lock to protect memcg
507  * associated with a kmem page from being released.
508  */
509 static inline struct mem_cgroup *page_memcg_check(struct page *page)
510 {
511 	/*
512 	 * Because page->memcg_data might be changed asynchronously
513 	 * for slab pages, READ_ONCE() should be used here.
514 	 */
515 	unsigned long memcg_data = READ_ONCE(page->memcg_data);
516 
517 	if (memcg_data & MEMCG_DATA_OBJCGS)
518 		return NULL;
519 
520 	if (memcg_data & MEMCG_DATA_KMEM) {
521 		struct obj_cgroup *objcg;
522 
523 		objcg = (void *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
524 		return obj_cgroup_memcg(objcg);
525 	}
526 
527 	return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
528 }
529 
530 #ifdef CONFIG_MEMCG_KMEM
531 /*
532  * folio_memcg_kmem - Check if the folio has the memcg_kmem flag set.
533  * @folio: Pointer to the folio.
534  *
535  * Checks if the folio has MemcgKmem flag set. The caller must ensure
536  * that the folio has an associated memory cgroup. It's not safe to call
537  * this function against some types of folios, e.g. slab folios.
538  */
539 static inline bool folio_memcg_kmem(struct folio *folio)
540 {
541 	VM_BUG_ON_PGFLAGS(PageTail(&folio->page), &folio->page);
542 	VM_BUG_ON_FOLIO(folio->memcg_data & MEMCG_DATA_OBJCGS, folio);
543 	return folio->memcg_data & MEMCG_DATA_KMEM;
544 }
545 
546 /*
547  * page_objcgs - get the object cgroups vector associated with a page
548  * @page: a pointer to the page struct
549  *
550  * Returns a pointer to the object cgroups vector associated with the page,
551  * or NULL. This function assumes that the page is known to have an
552  * associated object cgroups vector. It's not safe to call this function
553  * against pages, which might have an associated memory cgroup: e.g.
554  * kernel stack pages.
555  */
556 static inline struct obj_cgroup **page_objcgs(struct page *page)
557 {
558 	unsigned long memcg_data = READ_ONCE(page->memcg_data);
559 
560 	VM_BUG_ON_PAGE(memcg_data && !(memcg_data & MEMCG_DATA_OBJCGS), page);
561 	VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, page);
562 
563 	return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
564 }
565 
566 /*
567  * page_objcgs_check - get the object cgroups vector associated with a page
568  * @page: a pointer to the page struct
569  *
570  * Returns a pointer to the object cgroups vector associated with the page,
571  * or NULL. This function is safe to use if the page can be directly associated
572  * with a memory cgroup.
573  */
574 static inline struct obj_cgroup **page_objcgs_check(struct page *page)
575 {
576 	unsigned long memcg_data = READ_ONCE(page->memcg_data);
577 
578 	if (!memcg_data || !(memcg_data & MEMCG_DATA_OBJCGS))
579 		return NULL;
580 
581 	VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, page);
582 
583 	return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
584 }
585 
586 #else
587 static inline bool folio_memcg_kmem(struct folio *folio)
588 {
589 	return false;
590 }
591 
592 static inline struct obj_cgroup **page_objcgs(struct page *page)
593 {
594 	return NULL;
595 }
596 
597 static inline struct obj_cgroup **page_objcgs_check(struct page *page)
598 {
599 	return NULL;
600 }
601 #endif
602 
603 static inline bool PageMemcgKmem(struct page *page)
604 {
605 	return folio_memcg_kmem(page_folio(page));
606 }
607 
608 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
609 {
610 	return (memcg == root_mem_cgroup);
611 }
612 
613 static inline bool mem_cgroup_disabled(void)
614 {
615 	return !cgroup_subsys_enabled(memory_cgrp_subsys);
616 }
617 
618 static inline void mem_cgroup_protection(struct mem_cgroup *root,
619 					 struct mem_cgroup *memcg,
620 					 unsigned long *min,
621 					 unsigned long *low)
622 {
623 	*min = *low = 0;
624 
625 	if (mem_cgroup_disabled())
626 		return;
627 
628 	/*
629 	 * There is no reclaim protection applied to a targeted reclaim.
630 	 * We are special casing this specific case here because
631 	 * mem_cgroup_protected calculation is not robust enough to keep
632 	 * the protection invariant for calculated effective values for
633 	 * parallel reclaimers with different reclaim target. This is
634 	 * especially a problem for tail memcgs (as they have pages on LRU)
635 	 * which would want to have effective values 0 for targeted reclaim
636 	 * but a different value for external reclaim.
637 	 *
638 	 * Example
639 	 * Let's have global and A's reclaim in parallel:
640 	 *  |
641 	 *  A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G)
642 	 *  |\
643 	 *  | C (low = 1G, usage = 2.5G)
644 	 *  B (low = 1G, usage = 0.5G)
645 	 *
646 	 * For the global reclaim
647 	 * A.elow = A.low
648 	 * B.elow = min(B.usage, B.low) because children_low_usage <= A.elow
649 	 * C.elow = min(C.usage, C.low)
650 	 *
651 	 * With the effective values resetting we have A reclaim
652 	 * A.elow = 0
653 	 * B.elow = B.low
654 	 * C.elow = C.low
655 	 *
656 	 * If the global reclaim races with A's reclaim then
657 	 * B.elow = C.elow = 0 because children_low_usage > A.elow)
658 	 * is possible and reclaiming B would be violating the protection.
659 	 *
660 	 */
661 	if (root == memcg)
662 		return;
663 
664 	*min = READ_ONCE(memcg->memory.emin);
665 	*low = READ_ONCE(memcg->memory.elow);
666 }
667 
668 void mem_cgroup_calculate_protection(struct mem_cgroup *root,
669 				     struct mem_cgroup *memcg);
670 
671 static inline bool mem_cgroup_supports_protection(struct mem_cgroup *memcg)
672 {
673 	/*
674 	 * The root memcg doesn't account charges, and doesn't support
675 	 * protection.
676 	 */
677 	return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg);
678 
679 }
680 
681 static inline bool mem_cgroup_below_low(struct mem_cgroup *memcg)
682 {
683 	if (!mem_cgroup_supports_protection(memcg))
684 		return false;
685 
686 	return READ_ONCE(memcg->memory.elow) >=
687 		page_counter_read(&memcg->memory);
688 }
689 
690 static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg)
691 {
692 	if (!mem_cgroup_supports_protection(memcg))
693 		return false;
694 
695 	return READ_ONCE(memcg->memory.emin) >=
696 		page_counter_read(&memcg->memory);
697 }
698 
699 int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp);
700 
701 /**
702  * mem_cgroup_charge - Charge a newly allocated folio to a cgroup.
703  * @folio: Folio to charge.
704  * @mm: mm context of the allocating task.
705  * @gfp: Reclaim mode.
706  *
707  * Try to charge @folio to the memcg that @mm belongs to, reclaiming
708  * pages according to @gfp if necessary.  If @mm is NULL, try to
709  * charge to the active memcg.
710  *
711  * Do not use this for folios allocated for swapin.
712  *
713  * Return: 0 on success. Otherwise, an error code is returned.
714  */
715 static inline int mem_cgroup_charge(struct folio *folio, struct mm_struct *mm,
716 				    gfp_t gfp)
717 {
718 	if (mem_cgroup_disabled())
719 		return 0;
720 	return __mem_cgroup_charge(folio, mm, gfp);
721 }
722 
723 int mem_cgroup_swapin_charge_page(struct page *page, struct mm_struct *mm,
724 				  gfp_t gfp, swp_entry_t entry);
725 void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry);
726 
727 void __mem_cgroup_uncharge(struct folio *folio);
728 
729 /**
730  * mem_cgroup_uncharge - Uncharge a folio.
731  * @folio: Folio to uncharge.
732  *
733  * Uncharge a folio previously charged with mem_cgroup_charge().
734  */
735 static inline void mem_cgroup_uncharge(struct folio *folio)
736 {
737 	if (mem_cgroup_disabled())
738 		return;
739 	__mem_cgroup_uncharge(folio);
740 }
741 
742 void __mem_cgroup_uncharge_list(struct list_head *page_list);
743 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
744 {
745 	if (mem_cgroup_disabled())
746 		return;
747 	__mem_cgroup_uncharge_list(page_list);
748 }
749 
750 void mem_cgroup_migrate(struct folio *old, struct folio *new);
751 
752 /**
753  * mem_cgroup_lruvec - get the lru list vector for a memcg & node
754  * @memcg: memcg of the wanted lruvec
755  * @pgdat: pglist_data
756  *
757  * Returns the lru list vector holding pages for a given @memcg &
758  * @pgdat combination. This can be the node lruvec, if the memory
759  * controller is disabled.
760  */
761 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
762 					       struct pglist_data *pgdat)
763 {
764 	struct mem_cgroup_per_node *mz;
765 	struct lruvec *lruvec;
766 
767 	if (mem_cgroup_disabled()) {
768 		lruvec = &pgdat->__lruvec;
769 		goto out;
770 	}
771 
772 	if (!memcg)
773 		memcg = root_mem_cgroup;
774 
775 	mz = memcg->nodeinfo[pgdat->node_id];
776 	lruvec = &mz->lruvec;
777 out:
778 	/*
779 	 * Since a node can be onlined after the mem_cgroup was created,
780 	 * we have to be prepared to initialize lruvec->pgdat here;
781 	 * and if offlined then reonlined, we need to reinitialize it.
782 	 */
783 	if (unlikely(lruvec->pgdat != pgdat))
784 		lruvec->pgdat = pgdat;
785 	return lruvec;
786 }
787 
788 /**
789  * folio_lruvec - return lruvec for isolating/putting an LRU folio
790  * @folio: Pointer to the folio.
791  *
792  * This function relies on folio->mem_cgroup being stable.
793  */
794 static inline struct lruvec *folio_lruvec(struct folio *folio)
795 {
796 	struct mem_cgroup *memcg = folio_memcg(folio);
797 
798 	VM_WARN_ON_ONCE_FOLIO(!memcg && !mem_cgroup_disabled(), folio);
799 	return mem_cgroup_lruvec(memcg, folio_pgdat(folio));
800 }
801 
802 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
803 
804 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
805 
806 struct lruvec *folio_lruvec_lock(struct folio *folio);
807 struct lruvec *folio_lruvec_lock_irq(struct folio *folio);
808 struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
809 						unsigned long *flags);
810 
811 #ifdef CONFIG_DEBUG_VM
812 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio);
813 #else
814 static inline
815 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
816 {
817 }
818 #endif
819 
820 static inline
821 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
822 	return css ? container_of(css, struct mem_cgroup, css) : NULL;
823 }
824 
825 static inline bool obj_cgroup_tryget(struct obj_cgroup *objcg)
826 {
827 	return percpu_ref_tryget(&objcg->refcnt);
828 }
829 
830 static inline void obj_cgroup_get(struct obj_cgroup *objcg)
831 {
832 	percpu_ref_get(&objcg->refcnt);
833 }
834 
835 static inline void obj_cgroup_get_many(struct obj_cgroup *objcg,
836 				       unsigned long nr)
837 {
838 	percpu_ref_get_many(&objcg->refcnt, nr);
839 }
840 
841 static inline void obj_cgroup_put(struct obj_cgroup *objcg)
842 {
843 	percpu_ref_put(&objcg->refcnt);
844 }
845 
846 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
847 {
848 	if (memcg)
849 		css_put(&memcg->css);
850 }
851 
852 #define mem_cgroup_from_counter(counter, member)	\
853 	container_of(counter, struct mem_cgroup, member)
854 
855 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
856 				   struct mem_cgroup *,
857 				   struct mem_cgroup_reclaim_cookie *);
858 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
859 int mem_cgroup_scan_tasks(struct mem_cgroup *,
860 			  int (*)(struct task_struct *, void *), void *);
861 
862 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
863 {
864 	if (mem_cgroup_disabled())
865 		return 0;
866 
867 	return memcg->id.id;
868 }
869 struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
870 
871 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
872 {
873 	return mem_cgroup_from_css(seq_css(m));
874 }
875 
876 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
877 {
878 	struct mem_cgroup_per_node *mz;
879 
880 	if (mem_cgroup_disabled())
881 		return NULL;
882 
883 	mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
884 	return mz->memcg;
885 }
886 
887 /**
888  * parent_mem_cgroup - find the accounting parent of a memcg
889  * @memcg: memcg whose parent to find
890  *
891  * Returns the parent memcg, or NULL if this is the root or the memory
892  * controller is in legacy no-hierarchy mode.
893  */
894 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
895 {
896 	if (!memcg->memory.parent)
897 		return NULL;
898 	return mem_cgroup_from_counter(memcg->memory.parent, memory);
899 }
900 
901 static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
902 			      struct mem_cgroup *root)
903 {
904 	if (root == memcg)
905 		return true;
906 	return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
907 }
908 
909 static inline bool mm_match_cgroup(struct mm_struct *mm,
910 				   struct mem_cgroup *memcg)
911 {
912 	struct mem_cgroup *task_memcg;
913 	bool match = false;
914 
915 	rcu_read_lock();
916 	task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
917 	if (task_memcg)
918 		match = mem_cgroup_is_descendant(task_memcg, memcg);
919 	rcu_read_unlock();
920 	return match;
921 }
922 
923 struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
924 ino_t page_cgroup_ino(struct page *page);
925 
926 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
927 {
928 	if (mem_cgroup_disabled())
929 		return true;
930 	return !!(memcg->css.flags & CSS_ONLINE);
931 }
932 
933 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
934 		int zid, int nr_pages);
935 
936 static inline
937 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
938 		enum lru_list lru, int zone_idx)
939 {
940 	struct mem_cgroup_per_node *mz;
941 
942 	mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
943 	return READ_ONCE(mz->lru_zone_size[zone_idx][lru]);
944 }
945 
946 void mem_cgroup_handle_over_high(void);
947 
948 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
949 
950 unsigned long mem_cgroup_size(struct mem_cgroup *memcg);
951 
952 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
953 				struct task_struct *p);
954 
955 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg);
956 
957 static inline void mem_cgroup_enter_user_fault(void)
958 {
959 	WARN_ON(current->in_user_fault);
960 	current->in_user_fault = 1;
961 }
962 
963 static inline void mem_cgroup_exit_user_fault(void)
964 {
965 	WARN_ON(!current->in_user_fault);
966 	current->in_user_fault = 0;
967 }
968 
969 static inline bool task_in_memcg_oom(struct task_struct *p)
970 {
971 	return p->memcg_in_oom;
972 }
973 
974 bool mem_cgroup_oom_synchronize(bool wait);
975 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
976 					    struct mem_cgroup *oom_domain);
977 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg);
978 
979 #ifdef CONFIG_MEMCG_SWAP
980 extern bool cgroup_memory_noswap;
981 #endif
982 
983 void folio_memcg_lock(struct folio *folio);
984 void folio_memcg_unlock(struct folio *folio);
985 void lock_page_memcg(struct page *page);
986 void unlock_page_memcg(struct page *page);
987 
988 void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val);
989 
990 /* idx can be of type enum memcg_stat_item or node_stat_item */
991 static inline void mod_memcg_state(struct mem_cgroup *memcg,
992 				   int idx, int val)
993 {
994 	unsigned long flags;
995 
996 	local_irq_save(flags);
997 	__mod_memcg_state(memcg, idx, val);
998 	local_irq_restore(flags);
999 }
1000 
1001 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
1002 {
1003 	return READ_ONCE(memcg->vmstats.state[idx]);
1004 }
1005 
1006 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1007 					      enum node_stat_item idx)
1008 {
1009 	struct mem_cgroup_per_node *pn;
1010 
1011 	if (mem_cgroup_disabled())
1012 		return node_page_state(lruvec_pgdat(lruvec), idx);
1013 
1014 	pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
1015 	return READ_ONCE(pn->lruvec_stats.state[idx]);
1016 }
1017 
1018 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1019 						    enum node_stat_item idx)
1020 {
1021 	struct mem_cgroup_per_node *pn;
1022 	long x = 0;
1023 	int cpu;
1024 
1025 	if (mem_cgroup_disabled())
1026 		return node_page_state(lruvec_pgdat(lruvec), idx);
1027 
1028 	pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
1029 	for_each_possible_cpu(cpu)
1030 		x += per_cpu(pn->lruvec_stats_percpu->state[idx], cpu);
1031 #ifdef CONFIG_SMP
1032 	if (x < 0)
1033 		x = 0;
1034 #endif
1035 	return x;
1036 }
1037 
1038 void mem_cgroup_flush_stats(void);
1039 
1040 void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
1041 			      int val);
1042 void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val);
1043 
1044 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1045 					 int val)
1046 {
1047 	unsigned long flags;
1048 
1049 	local_irq_save(flags);
1050 	__mod_lruvec_kmem_state(p, idx, val);
1051 	local_irq_restore(flags);
1052 }
1053 
1054 static inline void mod_memcg_lruvec_state(struct lruvec *lruvec,
1055 					  enum node_stat_item idx, int val)
1056 {
1057 	unsigned long flags;
1058 
1059 	local_irq_save(flags);
1060 	__mod_memcg_lruvec_state(lruvec, idx, val);
1061 	local_irq_restore(flags);
1062 }
1063 
1064 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
1065 			  unsigned long count);
1066 
1067 static inline void count_memcg_events(struct mem_cgroup *memcg,
1068 				      enum vm_event_item idx,
1069 				      unsigned long count)
1070 {
1071 	unsigned long flags;
1072 
1073 	local_irq_save(flags);
1074 	__count_memcg_events(memcg, idx, count);
1075 	local_irq_restore(flags);
1076 }
1077 
1078 static inline void count_memcg_page_event(struct page *page,
1079 					  enum vm_event_item idx)
1080 {
1081 	struct mem_cgroup *memcg = page_memcg(page);
1082 
1083 	if (memcg)
1084 		count_memcg_events(memcg, idx, 1);
1085 }
1086 
1087 static inline void count_memcg_event_mm(struct mm_struct *mm,
1088 					enum vm_event_item idx)
1089 {
1090 	struct mem_cgroup *memcg;
1091 
1092 	if (mem_cgroup_disabled())
1093 		return;
1094 
1095 	rcu_read_lock();
1096 	memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1097 	if (likely(memcg))
1098 		count_memcg_events(memcg, idx, 1);
1099 	rcu_read_unlock();
1100 }
1101 
1102 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1103 				      enum memcg_memory_event event)
1104 {
1105 	bool swap_event = event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX ||
1106 			  event == MEMCG_SWAP_FAIL;
1107 
1108 	atomic_long_inc(&memcg->memory_events_local[event]);
1109 	if (!swap_event)
1110 		cgroup_file_notify(&memcg->events_local_file);
1111 
1112 	do {
1113 		atomic_long_inc(&memcg->memory_events[event]);
1114 		if (swap_event)
1115 			cgroup_file_notify(&memcg->swap_events_file);
1116 		else
1117 			cgroup_file_notify(&memcg->events_file);
1118 
1119 		if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1120 			break;
1121 		if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
1122 			break;
1123 	} while ((memcg = parent_mem_cgroup(memcg)) &&
1124 		 !mem_cgroup_is_root(memcg));
1125 }
1126 
1127 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1128 					 enum memcg_memory_event event)
1129 {
1130 	struct mem_cgroup *memcg;
1131 
1132 	if (mem_cgroup_disabled())
1133 		return;
1134 
1135 	rcu_read_lock();
1136 	memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1137 	if (likely(memcg))
1138 		memcg_memory_event(memcg, event);
1139 	rcu_read_unlock();
1140 }
1141 
1142 void split_page_memcg(struct page *head, unsigned int nr);
1143 
1144 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1145 						gfp_t gfp_mask,
1146 						unsigned long *total_scanned);
1147 
1148 #else /* CONFIG_MEMCG */
1149 
1150 #define MEM_CGROUP_ID_SHIFT	0
1151 #define MEM_CGROUP_ID_MAX	0
1152 
1153 static inline struct mem_cgroup *folio_memcg(struct folio *folio)
1154 {
1155 	return NULL;
1156 }
1157 
1158 static inline struct mem_cgroup *page_memcg(struct page *page)
1159 {
1160 	return NULL;
1161 }
1162 
1163 static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)
1164 {
1165 	WARN_ON_ONCE(!rcu_read_lock_held());
1166 	return NULL;
1167 }
1168 
1169 static inline struct mem_cgroup *page_memcg_check(struct page *page)
1170 {
1171 	return NULL;
1172 }
1173 
1174 static inline bool folio_memcg_kmem(struct folio *folio)
1175 {
1176 	return false;
1177 }
1178 
1179 static inline bool PageMemcgKmem(struct page *page)
1180 {
1181 	return false;
1182 }
1183 
1184 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
1185 {
1186 	return true;
1187 }
1188 
1189 static inline bool mem_cgroup_disabled(void)
1190 {
1191 	return true;
1192 }
1193 
1194 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1195 				      enum memcg_memory_event event)
1196 {
1197 }
1198 
1199 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1200 					 enum memcg_memory_event event)
1201 {
1202 }
1203 
1204 static inline void mem_cgroup_protection(struct mem_cgroup *root,
1205 					 struct mem_cgroup *memcg,
1206 					 unsigned long *min,
1207 					 unsigned long *low)
1208 {
1209 	*min = *low = 0;
1210 }
1211 
1212 static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root,
1213 						   struct mem_cgroup *memcg)
1214 {
1215 }
1216 
1217 static inline bool mem_cgroup_below_low(struct mem_cgroup *memcg)
1218 {
1219 	return false;
1220 }
1221 
1222 static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg)
1223 {
1224 	return false;
1225 }
1226 
1227 static inline int mem_cgroup_charge(struct folio *folio,
1228 		struct mm_struct *mm, gfp_t gfp)
1229 {
1230 	return 0;
1231 }
1232 
1233 static inline int mem_cgroup_swapin_charge_page(struct page *page,
1234 			struct mm_struct *mm, gfp_t gfp, swp_entry_t entry)
1235 {
1236 	return 0;
1237 }
1238 
1239 static inline void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry)
1240 {
1241 }
1242 
1243 static inline void mem_cgroup_uncharge(struct folio *folio)
1244 {
1245 }
1246 
1247 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
1248 {
1249 }
1250 
1251 static inline void mem_cgroup_migrate(struct folio *old, struct folio *new)
1252 {
1253 }
1254 
1255 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
1256 					       struct pglist_data *pgdat)
1257 {
1258 	return &pgdat->__lruvec;
1259 }
1260 
1261 static inline struct lruvec *folio_lruvec(struct folio *folio)
1262 {
1263 	struct pglist_data *pgdat = folio_pgdat(folio);
1264 	return &pgdat->__lruvec;
1265 }
1266 
1267 static inline
1268 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
1269 {
1270 }
1271 
1272 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
1273 {
1274 	return NULL;
1275 }
1276 
1277 static inline bool mm_match_cgroup(struct mm_struct *mm,
1278 		struct mem_cgroup *memcg)
1279 {
1280 	return true;
1281 }
1282 
1283 static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
1284 {
1285 	return NULL;
1286 }
1287 
1288 static inline
1289 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css)
1290 {
1291 	return NULL;
1292 }
1293 
1294 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
1295 {
1296 }
1297 
1298 static inline struct lruvec *folio_lruvec_lock(struct folio *folio)
1299 {
1300 	struct pglist_data *pgdat = folio_pgdat(folio);
1301 
1302 	spin_lock(&pgdat->__lruvec.lru_lock);
1303 	return &pgdat->__lruvec;
1304 }
1305 
1306 static inline struct lruvec *folio_lruvec_lock_irq(struct folio *folio)
1307 {
1308 	struct pglist_data *pgdat = folio_pgdat(folio);
1309 
1310 	spin_lock_irq(&pgdat->__lruvec.lru_lock);
1311 	return &pgdat->__lruvec;
1312 }
1313 
1314 static inline struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
1315 		unsigned long *flagsp)
1316 {
1317 	struct pglist_data *pgdat = folio_pgdat(folio);
1318 
1319 	spin_lock_irqsave(&pgdat->__lruvec.lru_lock, *flagsp);
1320 	return &pgdat->__lruvec;
1321 }
1322 
1323 static inline struct mem_cgroup *
1324 mem_cgroup_iter(struct mem_cgroup *root,
1325 		struct mem_cgroup *prev,
1326 		struct mem_cgroup_reclaim_cookie *reclaim)
1327 {
1328 	return NULL;
1329 }
1330 
1331 static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
1332 					 struct mem_cgroup *prev)
1333 {
1334 }
1335 
1336 static inline int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1337 		int (*fn)(struct task_struct *, void *), void *arg)
1338 {
1339 	return 0;
1340 }
1341 
1342 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
1343 {
1344 	return 0;
1345 }
1346 
1347 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
1348 {
1349 	WARN_ON_ONCE(id);
1350 	/* XXX: This should always return root_mem_cgroup */
1351 	return NULL;
1352 }
1353 
1354 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
1355 {
1356 	return NULL;
1357 }
1358 
1359 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
1360 {
1361 	return NULL;
1362 }
1363 
1364 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
1365 {
1366 	return true;
1367 }
1368 
1369 static inline
1370 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
1371 		enum lru_list lru, int zone_idx)
1372 {
1373 	return 0;
1374 }
1375 
1376 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
1377 {
1378 	return 0;
1379 }
1380 
1381 static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1382 {
1383 	return 0;
1384 }
1385 
1386 static inline void
1387 mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1388 {
1389 }
1390 
1391 static inline void
1392 mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
1393 {
1394 }
1395 
1396 static inline void lock_page_memcg(struct page *page)
1397 {
1398 }
1399 
1400 static inline void unlock_page_memcg(struct page *page)
1401 {
1402 }
1403 
1404 static inline void folio_memcg_lock(struct folio *folio)
1405 {
1406 }
1407 
1408 static inline void folio_memcg_unlock(struct folio *folio)
1409 {
1410 }
1411 
1412 static inline void mem_cgroup_handle_over_high(void)
1413 {
1414 }
1415 
1416 static inline void mem_cgroup_enter_user_fault(void)
1417 {
1418 }
1419 
1420 static inline void mem_cgroup_exit_user_fault(void)
1421 {
1422 }
1423 
1424 static inline bool task_in_memcg_oom(struct task_struct *p)
1425 {
1426 	return false;
1427 }
1428 
1429 static inline bool mem_cgroup_oom_synchronize(bool wait)
1430 {
1431 	return false;
1432 }
1433 
1434 static inline struct mem_cgroup *mem_cgroup_get_oom_group(
1435 	struct task_struct *victim, struct mem_cgroup *oom_domain)
1436 {
1437 	return NULL;
1438 }
1439 
1440 static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
1441 {
1442 }
1443 
1444 static inline void __mod_memcg_state(struct mem_cgroup *memcg,
1445 				     int idx,
1446 				     int nr)
1447 {
1448 }
1449 
1450 static inline void mod_memcg_state(struct mem_cgroup *memcg,
1451 				   int idx,
1452 				   int nr)
1453 {
1454 }
1455 
1456 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
1457 {
1458 	return 0;
1459 }
1460 
1461 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1462 					      enum node_stat_item idx)
1463 {
1464 	return node_page_state(lruvec_pgdat(lruvec), idx);
1465 }
1466 
1467 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1468 						    enum node_stat_item idx)
1469 {
1470 	return node_page_state(lruvec_pgdat(lruvec), idx);
1471 }
1472 
1473 static inline void mem_cgroup_flush_stats(void)
1474 {
1475 }
1476 
1477 static inline void __mod_memcg_lruvec_state(struct lruvec *lruvec,
1478 					    enum node_stat_item idx, int val)
1479 {
1480 }
1481 
1482 static inline void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1483 					   int val)
1484 {
1485 	struct page *page = virt_to_head_page(p);
1486 
1487 	__mod_node_page_state(page_pgdat(page), idx, val);
1488 }
1489 
1490 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1491 					 int val)
1492 {
1493 	struct page *page = virt_to_head_page(p);
1494 
1495 	mod_node_page_state(page_pgdat(page), idx, val);
1496 }
1497 
1498 static inline void count_memcg_events(struct mem_cgroup *memcg,
1499 				      enum vm_event_item idx,
1500 				      unsigned long count)
1501 {
1502 }
1503 
1504 static inline void __count_memcg_events(struct mem_cgroup *memcg,
1505 					enum vm_event_item idx,
1506 					unsigned long count)
1507 {
1508 }
1509 
1510 static inline void count_memcg_page_event(struct page *page,
1511 					  int idx)
1512 {
1513 }
1514 
1515 static inline
1516 void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
1517 {
1518 }
1519 
1520 static inline void split_page_memcg(struct page *head, unsigned int nr)
1521 {
1522 }
1523 
1524 static inline
1525 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1526 					    gfp_t gfp_mask,
1527 					    unsigned long *total_scanned)
1528 {
1529 	return 0;
1530 }
1531 #endif /* CONFIG_MEMCG */
1532 
1533 static inline void __inc_lruvec_kmem_state(void *p, enum node_stat_item idx)
1534 {
1535 	__mod_lruvec_kmem_state(p, idx, 1);
1536 }
1537 
1538 static inline void __dec_lruvec_kmem_state(void *p, enum node_stat_item idx)
1539 {
1540 	__mod_lruvec_kmem_state(p, idx, -1);
1541 }
1542 
1543 static inline struct lruvec *parent_lruvec(struct lruvec *lruvec)
1544 {
1545 	struct mem_cgroup *memcg;
1546 
1547 	memcg = lruvec_memcg(lruvec);
1548 	if (!memcg)
1549 		return NULL;
1550 	memcg = parent_mem_cgroup(memcg);
1551 	if (!memcg)
1552 		return NULL;
1553 	return mem_cgroup_lruvec(memcg, lruvec_pgdat(lruvec));
1554 }
1555 
1556 static inline void unlock_page_lruvec(struct lruvec *lruvec)
1557 {
1558 	spin_unlock(&lruvec->lru_lock);
1559 }
1560 
1561 static inline void unlock_page_lruvec_irq(struct lruvec *lruvec)
1562 {
1563 	spin_unlock_irq(&lruvec->lru_lock);
1564 }
1565 
1566 static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec,
1567 		unsigned long flags)
1568 {
1569 	spin_unlock_irqrestore(&lruvec->lru_lock, flags);
1570 }
1571 
1572 /* Test requires a stable page->memcg binding, see page_memcg() */
1573 static inline bool folio_matches_lruvec(struct folio *folio,
1574 		struct lruvec *lruvec)
1575 {
1576 	return lruvec_pgdat(lruvec) == folio_pgdat(folio) &&
1577 	       lruvec_memcg(lruvec) == folio_memcg(folio);
1578 }
1579 
1580 /* Don't lock again iff page's lruvec locked */
1581 static inline struct lruvec *folio_lruvec_relock_irq(struct folio *folio,
1582 		struct lruvec *locked_lruvec)
1583 {
1584 	if (locked_lruvec) {
1585 		if (folio_matches_lruvec(folio, locked_lruvec))
1586 			return locked_lruvec;
1587 
1588 		unlock_page_lruvec_irq(locked_lruvec);
1589 	}
1590 
1591 	return folio_lruvec_lock_irq(folio);
1592 }
1593 
1594 /* Don't lock again iff page's lruvec locked */
1595 static inline struct lruvec *folio_lruvec_relock_irqsave(struct folio *folio,
1596 		struct lruvec *locked_lruvec, unsigned long *flags)
1597 {
1598 	if (locked_lruvec) {
1599 		if (folio_matches_lruvec(folio, locked_lruvec))
1600 			return locked_lruvec;
1601 
1602 		unlock_page_lruvec_irqrestore(locked_lruvec, *flags);
1603 	}
1604 
1605 	return folio_lruvec_lock_irqsave(folio, flags);
1606 }
1607 
1608 #ifdef CONFIG_CGROUP_WRITEBACK
1609 
1610 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
1611 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1612 			 unsigned long *pheadroom, unsigned long *pdirty,
1613 			 unsigned long *pwriteback);
1614 
1615 void mem_cgroup_track_foreign_dirty_slowpath(struct folio *folio,
1616 					     struct bdi_writeback *wb);
1617 
1618 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio,
1619 						  struct bdi_writeback *wb)
1620 {
1621 	if (mem_cgroup_disabled())
1622 		return;
1623 
1624 	if (unlikely(&folio_memcg(folio)->css != wb->memcg_css))
1625 		mem_cgroup_track_foreign_dirty_slowpath(folio, wb);
1626 }
1627 
1628 void mem_cgroup_flush_foreign(struct bdi_writeback *wb);
1629 
1630 #else	/* CONFIG_CGROUP_WRITEBACK */
1631 
1632 static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1633 {
1634 	return NULL;
1635 }
1636 
1637 static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
1638 				       unsigned long *pfilepages,
1639 				       unsigned long *pheadroom,
1640 				       unsigned long *pdirty,
1641 				       unsigned long *pwriteback)
1642 {
1643 }
1644 
1645 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio,
1646 						  struct bdi_writeback *wb)
1647 {
1648 }
1649 
1650 static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
1651 {
1652 }
1653 
1654 #endif	/* CONFIG_CGROUP_WRITEBACK */
1655 
1656 struct sock;
1657 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
1658 			     gfp_t gfp_mask);
1659 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1660 #ifdef CONFIG_MEMCG
1661 extern struct static_key_false memcg_sockets_enabled_key;
1662 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
1663 void mem_cgroup_sk_alloc(struct sock *sk);
1664 void mem_cgroup_sk_free(struct sock *sk);
1665 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1666 {
1667 	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure)
1668 		return true;
1669 	do {
1670 		if (time_before(jiffies, memcg->socket_pressure))
1671 			return true;
1672 	} while ((memcg = parent_mem_cgroup(memcg)));
1673 	return false;
1674 }
1675 
1676 int alloc_shrinker_info(struct mem_cgroup *memcg);
1677 void free_shrinker_info(struct mem_cgroup *memcg);
1678 void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id);
1679 void reparent_shrinker_deferred(struct mem_cgroup *memcg);
1680 #else
1681 #define mem_cgroup_sockets_enabled 0
1682 static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1683 static inline void mem_cgroup_sk_free(struct sock *sk) { };
1684 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1685 {
1686 	return false;
1687 }
1688 
1689 static inline void set_shrinker_bit(struct mem_cgroup *memcg,
1690 				    int nid, int shrinker_id)
1691 {
1692 }
1693 #endif
1694 
1695 #ifdef CONFIG_MEMCG_KMEM
1696 bool mem_cgroup_kmem_disabled(void);
1697 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order);
1698 void __memcg_kmem_uncharge_page(struct page *page, int order);
1699 
1700 struct obj_cgroup *get_obj_cgroup_from_current(void);
1701 
1702 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size);
1703 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size);
1704 
1705 extern struct static_key_false memcg_kmem_enabled_key;
1706 
1707 extern int memcg_nr_cache_ids;
1708 void memcg_get_cache_ids(void);
1709 void memcg_put_cache_ids(void);
1710 
1711 /*
1712  * Helper macro to loop through all memcg-specific caches. Callers must still
1713  * check if the cache is valid (it is either valid or NULL).
1714  * the slab_mutex must be held when looping through those caches
1715  */
1716 #define for_each_memcg_cache_index(_idx)	\
1717 	for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
1718 
1719 static inline bool memcg_kmem_enabled(void)
1720 {
1721 	return static_branch_likely(&memcg_kmem_enabled_key);
1722 }
1723 
1724 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1725 					 int order)
1726 {
1727 	if (memcg_kmem_enabled())
1728 		return __memcg_kmem_charge_page(page, gfp, order);
1729 	return 0;
1730 }
1731 
1732 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1733 {
1734 	if (memcg_kmem_enabled())
1735 		__memcg_kmem_uncharge_page(page, order);
1736 }
1737 
1738 /*
1739  * A helper for accessing memcg's kmem_id, used for getting
1740  * corresponding LRU lists.
1741  */
1742 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1743 {
1744 	return memcg ? memcg->kmemcg_id : -1;
1745 }
1746 
1747 struct mem_cgroup *mem_cgroup_from_obj(void *p);
1748 
1749 #else
1750 static inline bool mem_cgroup_kmem_disabled(void)
1751 {
1752 	return true;
1753 }
1754 
1755 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1756 					 int order)
1757 {
1758 	return 0;
1759 }
1760 
1761 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1762 {
1763 }
1764 
1765 static inline int __memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1766 					   int order)
1767 {
1768 	return 0;
1769 }
1770 
1771 static inline void __memcg_kmem_uncharge_page(struct page *page, int order)
1772 {
1773 }
1774 
1775 #define for_each_memcg_cache_index(_idx)	\
1776 	for (; NULL; )
1777 
1778 static inline bool memcg_kmem_enabled(void)
1779 {
1780 	return false;
1781 }
1782 
1783 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1784 {
1785 	return -1;
1786 }
1787 
1788 static inline void memcg_get_cache_ids(void)
1789 {
1790 }
1791 
1792 static inline void memcg_put_cache_ids(void)
1793 {
1794 }
1795 
1796 static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)
1797 {
1798        return NULL;
1799 }
1800 
1801 #endif /* CONFIG_MEMCG_KMEM */
1802 
1803 #endif /* _LINUX_MEMCONTROL_H */
1804