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