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