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