xref: /linux-6.15/include/linux/memcontrol.h (revision 247dbcdb)
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 int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp);
656 
657 /**
658  * mem_cgroup_charge - Charge a newly allocated folio to a cgroup.
659  * @folio: Folio to charge.
660  * @mm: mm context of the allocating task.
661  * @gfp: Reclaim mode.
662  *
663  * Try to charge @folio to the memcg that @mm belongs to, reclaiming
664  * pages according to @gfp if necessary.  If @mm is NULL, try to
665  * charge to the active memcg.
666  *
667  * Do not use this for folios allocated for swapin.
668  *
669  * Return: 0 on success. Otherwise, an error code is returned.
670  */
671 static inline int mem_cgroup_charge(struct folio *folio, struct mm_struct *mm,
672 				    gfp_t gfp)
673 {
674 	if (mem_cgroup_disabled())
675 		return 0;
676 	return __mem_cgroup_charge(folio, mm, gfp);
677 }
678 
679 int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm,
680 				  gfp_t gfp, swp_entry_t entry);
681 void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry);
682 
683 void __mem_cgroup_uncharge(struct folio *folio);
684 
685 /**
686  * mem_cgroup_uncharge - Uncharge a folio.
687  * @folio: Folio to uncharge.
688  *
689  * Uncharge a folio previously charged with mem_cgroup_charge().
690  */
691 static inline void mem_cgroup_uncharge(struct folio *folio)
692 {
693 	if (mem_cgroup_disabled())
694 		return;
695 	__mem_cgroup_uncharge(folio);
696 }
697 
698 void __mem_cgroup_uncharge_list(struct list_head *page_list);
699 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
700 {
701 	if (mem_cgroup_disabled())
702 		return;
703 	__mem_cgroup_uncharge_list(page_list);
704 }
705 
706 void mem_cgroup_migrate(struct folio *old, struct folio *new);
707 
708 /**
709  * mem_cgroup_lruvec - get the lru list vector for a memcg & node
710  * @memcg: memcg of the wanted lruvec
711  * @pgdat: pglist_data
712  *
713  * Returns the lru list vector holding pages for a given @memcg &
714  * @pgdat combination. This can be the node lruvec, if the memory
715  * controller is disabled.
716  */
717 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
718 					       struct pglist_data *pgdat)
719 {
720 	struct mem_cgroup_per_node *mz;
721 	struct lruvec *lruvec;
722 
723 	if (mem_cgroup_disabled()) {
724 		lruvec = &pgdat->__lruvec;
725 		goto out;
726 	}
727 
728 	if (!memcg)
729 		memcg = root_mem_cgroup;
730 
731 	mz = memcg->nodeinfo[pgdat->node_id];
732 	lruvec = &mz->lruvec;
733 out:
734 	/*
735 	 * Since a node can be onlined after the mem_cgroup was created,
736 	 * we have to be prepared to initialize lruvec->pgdat here;
737 	 * and if offlined then reonlined, we need to reinitialize it.
738 	 */
739 	if (unlikely(lruvec->pgdat != pgdat))
740 		lruvec->pgdat = pgdat;
741 	return lruvec;
742 }
743 
744 /**
745  * folio_lruvec - return lruvec for isolating/putting an LRU folio
746  * @folio: Pointer to the folio.
747  *
748  * This function relies on folio->mem_cgroup being stable.
749  */
750 static inline struct lruvec *folio_lruvec(struct folio *folio)
751 {
752 	struct mem_cgroup *memcg = folio_memcg(folio);
753 
754 	VM_WARN_ON_ONCE_FOLIO(!memcg && !mem_cgroup_disabled(), folio);
755 	return mem_cgroup_lruvec(memcg, folio_pgdat(folio));
756 }
757 
758 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
759 
760 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
761 
762 struct lruvec *folio_lruvec_lock(struct folio *folio);
763 struct lruvec *folio_lruvec_lock_irq(struct folio *folio);
764 struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
765 						unsigned long *flags);
766 
767 #ifdef CONFIG_DEBUG_VM
768 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio);
769 #else
770 static inline
771 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
772 {
773 }
774 #endif
775 
776 static inline
777 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
778 	return css ? container_of(css, struct mem_cgroup, css) : NULL;
779 }
780 
781 static inline bool obj_cgroup_tryget(struct obj_cgroup *objcg)
782 {
783 	return percpu_ref_tryget(&objcg->refcnt);
784 }
785 
786 static inline void obj_cgroup_get(struct obj_cgroup *objcg)
787 {
788 	percpu_ref_get(&objcg->refcnt);
789 }
790 
791 static inline void obj_cgroup_get_many(struct obj_cgroup *objcg,
792 				       unsigned long nr)
793 {
794 	percpu_ref_get_many(&objcg->refcnt, nr);
795 }
796 
797 static inline void obj_cgroup_put(struct obj_cgroup *objcg)
798 {
799 	percpu_ref_put(&objcg->refcnt);
800 }
801 
802 static inline bool mem_cgroup_tryget(struct mem_cgroup *memcg)
803 {
804 	return !memcg || css_tryget(&memcg->css);
805 }
806 
807 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
808 {
809 	if (memcg)
810 		css_put(&memcg->css);
811 }
812 
813 #define mem_cgroup_from_counter(counter, member)	\
814 	container_of(counter, struct mem_cgroup, member)
815 
816 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
817 				   struct mem_cgroup *,
818 				   struct mem_cgroup_reclaim_cookie *);
819 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
820 void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
821 			   int (*)(struct task_struct *, void *), void *arg);
822 
823 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
824 {
825 	if (mem_cgroup_disabled())
826 		return 0;
827 
828 	return memcg->id.id;
829 }
830 struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
831 
832 #ifdef CONFIG_SHRINKER_DEBUG
833 static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg)
834 {
835 	return memcg ? cgroup_ino(memcg->css.cgroup) : 0;
836 }
837 
838 struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino);
839 #endif
840 
841 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
842 {
843 	return mem_cgroup_from_css(seq_css(m));
844 }
845 
846 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
847 {
848 	struct mem_cgroup_per_node *mz;
849 
850 	if (mem_cgroup_disabled())
851 		return NULL;
852 
853 	mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
854 	return mz->memcg;
855 }
856 
857 /**
858  * parent_mem_cgroup - find the accounting parent of a memcg
859  * @memcg: memcg whose parent to find
860  *
861  * Returns the parent memcg, or NULL if this is the root.
862  */
863 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
864 {
865 	return mem_cgroup_from_css(memcg->css.parent);
866 }
867 
868 static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
869 			      struct mem_cgroup *root)
870 {
871 	if (root == memcg)
872 		return true;
873 	return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
874 }
875 
876 static inline bool mm_match_cgroup(struct mm_struct *mm,
877 				   struct mem_cgroup *memcg)
878 {
879 	struct mem_cgroup *task_memcg;
880 	bool match = false;
881 
882 	rcu_read_lock();
883 	task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
884 	if (task_memcg)
885 		match = mem_cgroup_is_descendant(task_memcg, memcg);
886 	rcu_read_unlock();
887 	return match;
888 }
889 
890 struct cgroup_subsys_state *mem_cgroup_css_from_folio(struct folio *folio);
891 ino_t page_cgroup_ino(struct page *page);
892 
893 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
894 {
895 	if (mem_cgroup_disabled())
896 		return true;
897 	return !!(memcg->css.flags & CSS_ONLINE);
898 }
899 
900 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
901 		int zid, int nr_pages);
902 
903 static inline
904 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
905 		enum lru_list lru, int zone_idx)
906 {
907 	struct mem_cgroup_per_node *mz;
908 
909 	mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
910 	return READ_ONCE(mz->lru_zone_size[zone_idx][lru]);
911 }
912 
913 void mem_cgroup_handle_over_high(gfp_t gfp_mask);
914 
915 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
916 
917 unsigned long mem_cgroup_size(struct mem_cgroup *memcg);
918 
919 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
920 				struct task_struct *p);
921 
922 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg);
923 
924 static inline void mem_cgroup_enter_user_fault(void)
925 {
926 	WARN_ON(current->in_user_fault);
927 	current->in_user_fault = 1;
928 }
929 
930 static inline void mem_cgroup_exit_user_fault(void)
931 {
932 	WARN_ON(!current->in_user_fault);
933 	current->in_user_fault = 0;
934 }
935 
936 static inline bool task_in_memcg_oom(struct task_struct *p)
937 {
938 	return p->memcg_in_oom;
939 }
940 
941 bool mem_cgroup_oom_synchronize(bool wait);
942 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
943 					    struct mem_cgroup *oom_domain);
944 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg);
945 
946 void folio_memcg_lock(struct folio *folio);
947 void folio_memcg_unlock(struct folio *folio);
948 
949 void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val);
950 
951 /* try to stablize folio_memcg() for all the pages in a memcg */
952 static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
953 {
954 	rcu_read_lock();
955 
956 	if (mem_cgroup_disabled() || !atomic_read(&memcg->moving_account))
957 		return true;
958 
959 	rcu_read_unlock();
960 	return false;
961 }
962 
963 static inline void mem_cgroup_unlock_pages(void)
964 {
965 	rcu_read_unlock();
966 }
967 
968 /* idx can be of type enum memcg_stat_item or node_stat_item */
969 static inline void mod_memcg_state(struct mem_cgroup *memcg,
970 				   int idx, int val)
971 {
972 	unsigned long flags;
973 
974 	local_irq_save(flags);
975 	__mod_memcg_state(memcg, idx, val);
976 	local_irq_restore(flags);
977 }
978 
979 static inline void mod_memcg_page_state(struct page *page,
980 					int idx, int val)
981 {
982 	struct mem_cgroup *memcg;
983 
984 	if (mem_cgroup_disabled())
985 		return;
986 
987 	rcu_read_lock();
988 	memcg = page_memcg(page);
989 	if (memcg)
990 		mod_memcg_state(memcg, idx, val);
991 	rcu_read_unlock();
992 }
993 
994 unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx);
995 
996 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
997 					      enum node_stat_item idx)
998 {
999 	struct mem_cgroup_per_node *pn;
1000 	long x;
1001 
1002 	if (mem_cgroup_disabled())
1003 		return node_page_state(lruvec_pgdat(lruvec), idx);
1004 
1005 	pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
1006 	x = READ_ONCE(pn->lruvec_stats.state[idx]);
1007 #ifdef CONFIG_SMP
1008 	if (x < 0)
1009 		x = 0;
1010 #endif
1011 	return x;
1012 }
1013 
1014 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1015 						    enum node_stat_item idx)
1016 {
1017 	struct mem_cgroup_per_node *pn;
1018 	long x = 0;
1019 
1020 	if (mem_cgroup_disabled())
1021 		return node_page_state(lruvec_pgdat(lruvec), idx);
1022 
1023 	pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
1024 	x = READ_ONCE(pn->lruvec_stats.state_local[idx]);
1025 #ifdef CONFIG_SMP
1026 	if (x < 0)
1027 		x = 0;
1028 #endif
1029 	return x;
1030 }
1031 
1032 void mem_cgroup_flush_stats(void);
1033 void mem_cgroup_flush_stats_ratelimited(void);
1034 
1035 void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
1036 			      int val);
1037 void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val);
1038 
1039 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1040 					 int val)
1041 {
1042 	unsigned long flags;
1043 
1044 	local_irq_save(flags);
1045 	__mod_lruvec_kmem_state(p, idx, val);
1046 	local_irq_restore(flags);
1047 }
1048 
1049 static inline void mod_memcg_lruvec_state(struct lruvec *lruvec,
1050 					  enum node_stat_item idx, int val)
1051 {
1052 	unsigned long flags;
1053 
1054 	local_irq_save(flags);
1055 	__mod_memcg_lruvec_state(lruvec, idx, val);
1056 	local_irq_restore(flags);
1057 }
1058 
1059 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
1060 			  unsigned long count);
1061 
1062 static inline void count_memcg_events(struct mem_cgroup *memcg,
1063 				      enum vm_event_item idx,
1064 				      unsigned long count)
1065 {
1066 	unsigned long flags;
1067 
1068 	local_irq_save(flags);
1069 	__count_memcg_events(memcg, idx, count);
1070 	local_irq_restore(flags);
1071 }
1072 
1073 static inline void count_memcg_page_event(struct page *page,
1074 					  enum vm_event_item idx)
1075 {
1076 	struct mem_cgroup *memcg = page_memcg(page);
1077 
1078 	if (memcg)
1079 		count_memcg_events(memcg, idx, 1);
1080 }
1081 
1082 static inline void count_memcg_folio_events(struct folio *folio,
1083 		enum vm_event_item idx, unsigned long nr)
1084 {
1085 	struct mem_cgroup *memcg = folio_memcg(folio);
1086 
1087 	if (memcg)
1088 		count_memcg_events(memcg, idx, nr);
1089 }
1090 
1091 static inline void count_memcg_event_mm(struct mm_struct *mm,
1092 					enum vm_event_item idx)
1093 {
1094 	struct mem_cgroup *memcg;
1095 
1096 	if (mem_cgroup_disabled())
1097 		return;
1098 
1099 	rcu_read_lock();
1100 	memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1101 	if (likely(memcg))
1102 		count_memcg_events(memcg, idx, 1);
1103 	rcu_read_unlock();
1104 }
1105 
1106 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1107 				      enum memcg_memory_event event)
1108 {
1109 	bool swap_event = event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX ||
1110 			  event == MEMCG_SWAP_FAIL;
1111 
1112 	atomic_long_inc(&memcg->memory_events_local[event]);
1113 	if (!swap_event)
1114 		cgroup_file_notify(&memcg->events_local_file);
1115 
1116 	do {
1117 		atomic_long_inc(&memcg->memory_events[event]);
1118 		if (swap_event)
1119 			cgroup_file_notify(&memcg->swap_events_file);
1120 		else
1121 			cgroup_file_notify(&memcg->events_file);
1122 
1123 		if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1124 			break;
1125 		if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
1126 			break;
1127 	} while ((memcg = parent_mem_cgroup(memcg)) &&
1128 		 !mem_cgroup_is_root(memcg));
1129 }
1130 
1131 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1132 					 enum memcg_memory_event event)
1133 {
1134 	struct mem_cgroup *memcg;
1135 
1136 	if (mem_cgroup_disabled())
1137 		return;
1138 
1139 	rcu_read_lock();
1140 	memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1141 	if (likely(memcg))
1142 		memcg_memory_event(memcg, event);
1143 	rcu_read_unlock();
1144 }
1145 
1146 void split_page_memcg(struct page *head, unsigned int nr);
1147 
1148 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1149 						gfp_t gfp_mask,
1150 						unsigned long *total_scanned);
1151 
1152 #else /* CONFIG_MEMCG */
1153 
1154 #define MEM_CGROUP_ID_SHIFT	0
1155 
1156 static inline struct mem_cgroup *folio_memcg(struct folio *folio)
1157 {
1158 	return NULL;
1159 }
1160 
1161 static inline struct mem_cgroup *page_memcg(struct page *page)
1162 {
1163 	return NULL;
1164 }
1165 
1166 static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)
1167 {
1168 	WARN_ON_ONCE(!rcu_read_lock_held());
1169 	return NULL;
1170 }
1171 
1172 static inline struct mem_cgroup *folio_memcg_check(struct folio *folio)
1173 {
1174 	return NULL;
1175 }
1176 
1177 static inline struct mem_cgroup *page_memcg_check(struct page *page)
1178 {
1179 	return NULL;
1180 }
1181 
1182 static inline bool folio_memcg_kmem(struct folio *folio)
1183 {
1184 	return false;
1185 }
1186 
1187 static inline bool PageMemcgKmem(struct page *page)
1188 {
1189 	return false;
1190 }
1191 
1192 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
1193 {
1194 	return true;
1195 }
1196 
1197 static inline bool mem_cgroup_disabled(void)
1198 {
1199 	return true;
1200 }
1201 
1202 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1203 				      enum memcg_memory_event event)
1204 {
1205 }
1206 
1207 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1208 					 enum memcg_memory_event event)
1209 {
1210 }
1211 
1212 static inline void mem_cgroup_protection(struct mem_cgroup *root,
1213 					 struct mem_cgroup *memcg,
1214 					 unsigned long *min,
1215 					 unsigned long *low)
1216 {
1217 	*min = *low = 0;
1218 }
1219 
1220 static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root,
1221 						   struct mem_cgroup *memcg)
1222 {
1223 }
1224 
1225 static inline bool mem_cgroup_unprotected(struct mem_cgroup *target,
1226 					  struct mem_cgroup *memcg)
1227 {
1228 	return true;
1229 }
1230 static inline bool mem_cgroup_below_low(struct mem_cgroup *target,
1231 					struct mem_cgroup *memcg)
1232 {
1233 	return false;
1234 }
1235 
1236 static inline bool mem_cgroup_below_min(struct mem_cgroup *target,
1237 					struct mem_cgroup *memcg)
1238 {
1239 	return false;
1240 }
1241 
1242 static inline int mem_cgroup_charge(struct folio *folio,
1243 		struct mm_struct *mm, gfp_t gfp)
1244 {
1245 	return 0;
1246 }
1247 
1248 static inline int mem_cgroup_swapin_charge_folio(struct folio *folio,
1249 			struct mm_struct *mm, gfp_t gfp, swp_entry_t entry)
1250 {
1251 	return 0;
1252 }
1253 
1254 static inline void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry)
1255 {
1256 }
1257 
1258 static inline void mem_cgroup_uncharge(struct folio *folio)
1259 {
1260 }
1261 
1262 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
1263 {
1264 }
1265 
1266 static inline void mem_cgroup_migrate(struct folio *old, struct folio *new)
1267 {
1268 }
1269 
1270 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
1271 					       struct pglist_data *pgdat)
1272 {
1273 	return &pgdat->__lruvec;
1274 }
1275 
1276 static inline struct lruvec *folio_lruvec(struct folio *folio)
1277 {
1278 	struct pglist_data *pgdat = folio_pgdat(folio);
1279 	return &pgdat->__lruvec;
1280 }
1281 
1282 static inline
1283 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
1284 {
1285 }
1286 
1287 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
1288 {
1289 	return NULL;
1290 }
1291 
1292 static inline bool mm_match_cgroup(struct mm_struct *mm,
1293 		struct mem_cgroup *memcg)
1294 {
1295 	return true;
1296 }
1297 
1298 static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
1299 {
1300 	return NULL;
1301 }
1302 
1303 static inline
1304 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css)
1305 {
1306 	return NULL;
1307 }
1308 
1309 static inline void obj_cgroup_put(struct obj_cgroup *objcg)
1310 {
1311 }
1312 
1313 static inline bool mem_cgroup_tryget(struct mem_cgroup *memcg)
1314 {
1315 	return true;
1316 }
1317 
1318 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
1319 {
1320 }
1321 
1322 static inline struct lruvec *folio_lruvec_lock(struct folio *folio)
1323 {
1324 	struct pglist_data *pgdat = folio_pgdat(folio);
1325 
1326 	spin_lock(&pgdat->__lruvec.lru_lock);
1327 	return &pgdat->__lruvec;
1328 }
1329 
1330 static inline struct lruvec *folio_lruvec_lock_irq(struct folio *folio)
1331 {
1332 	struct pglist_data *pgdat = folio_pgdat(folio);
1333 
1334 	spin_lock_irq(&pgdat->__lruvec.lru_lock);
1335 	return &pgdat->__lruvec;
1336 }
1337 
1338 static inline struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
1339 		unsigned long *flagsp)
1340 {
1341 	struct pglist_data *pgdat = folio_pgdat(folio);
1342 
1343 	spin_lock_irqsave(&pgdat->__lruvec.lru_lock, *flagsp);
1344 	return &pgdat->__lruvec;
1345 }
1346 
1347 static inline struct mem_cgroup *
1348 mem_cgroup_iter(struct mem_cgroup *root,
1349 		struct mem_cgroup *prev,
1350 		struct mem_cgroup_reclaim_cookie *reclaim)
1351 {
1352 	return NULL;
1353 }
1354 
1355 static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
1356 					 struct mem_cgroup *prev)
1357 {
1358 }
1359 
1360 static inline void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1361 		int (*fn)(struct task_struct *, void *), void *arg)
1362 {
1363 }
1364 
1365 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
1366 {
1367 	return 0;
1368 }
1369 
1370 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
1371 {
1372 	WARN_ON_ONCE(id);
1373 	/* XXX: This should always return root_mem_cgroup */
1374 	return NULL;
1375 }
1376 
1377 #ifdef CONFIG_SHRINKER_DEBUG
1378 static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg)
1379 {
1380 	return 0;
1381 }
1382 
1383 static inline struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino)
1384 {
1385 	return NULL;
1386 }
1387 #endif
1388 
1389 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
1390 {
1391 	return NULL;
1392 }
1393 
1394 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
1395 {
1396 	return NULL;
1397 }
1398 
1399 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
1400 {
1401 	return true;
1402 }
1403 
1404 static inline
1405 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
1406 		enum lru_list lru, int zone_idx)
1407 {
1408 	return 0;
1409 }
1410 
1411 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
1412 {
1413 	return 0;
1414 }
1415 
1416 static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1417 {
1418 	return 0;
1419 }
1420 
1421 static inline void
1422 mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1423 {
1424 }
1425 
1426 static inline void
1427 mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
1428 {
1429 }
1430 
1431 static inline void folio_memcg_lock(struct folio *folio)
1432 {
1433 }
1434 
1435 static inline void folio_memcg_unlock(struct folio *folio)
1436 {
1437 }
1438 
1439 static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
1440 {
1441 	/* to match folio_memcg_rcu() */
1442 	rcu_read_lock();
1443 	return true;
1444 }
1445 
1446 static inline void mem_cgroup_unlock_pages(void)
1447 {
1448 	rcu_read_unlock();
1449 }
1450 
1451 static inline void mem_cgroup_handle_over_high(gfp_t gfp_mask)
1452 {
1453 }
1454 
1455 static inline void mem_cgroup_enter_user_fault(void)
1456 {
1457 }
1458 
1459 static inline void mem_cgroup_exit_user_fault(void)
1460 {
1461 }
1462 
1463 static inline bool task_in_memcg_oom(struct task_struct *p)
1464 {
1465 	return false;
1466 }
1467 
1468 static inline bool mem_cgroup_oom_synchronize(bool wait)
1469 {
1470 	return false;
1471 }
1472 
1473 static inline struct mem_cgroup *mem_cgroup_get_oom_group(
1474 	struct task_struct *victim, struct mem_cgroup *oom_domain)
1475 {
1476 	return NULL;
1477 }
1478 
1479 static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
1480 {
1481 }
1482 
1483 static inline void __mod_memcg_state(struct mem_cgroup *memcg,
1484 				     int idx,
1485 				     int nr)
1486 {
1487 }
1488 
1489 static inline void mod_memcg_state(struct mem_cgroup *memcg,
1490 				   int idx,
1491 				   int nr)
1492 {
1493 }
1494 
1495 static inline void mod_memcg_page_state(struct page *page,
1496 					int idx, int val)
1497 {
1498 }
1499 
1500 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
1501 {
1502 	return 0;
1503 }
1504 
1505 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1506 					      enum node_stat_item idx)
1507 {
1508 	return node_page_state(lruvec_pgdat(lruvec), idx);
1509 }
1510 
1511 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1512 						    enum node_stat_item idx)
1513 {
1514 	return node_page_state(lruvec_pgdat(lruvec), idx);
1515 }
1516 
1517 static inline void mem_cgroup_flush_stats(void)
1518 {
1519 }
1520 
1521 static inline void mem_cgroup_flush_stats_ratelimited(void)
1522 {
1523 }
1524 
1525 static inline void __mod_memcg_lruvec_state(struct lruvec *lruvec,
1526 					    enum node_stat_item idx, int val)
1527 {
1528 }
1529 
1530 static inline void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1531 					   int val)
1532 {
1533 	struct page *page = virt_to_head_page(p);
1534 
1535 	__mod_node_page_state(page_pgdat(page), idx, val);
1536 }
1537 
1538 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1539 					 int val)
1540 {
1541 	struct page *page = virt_to_head_page(p);
1542 
1543 	mod_node_page_state(page_pgdat(page), idx, val);
1544 }
1545 
1546 static inline void count_memcg_events(struct mem_cgroup *memcg,
1547 				      enum vm_event_item idx,
1548 				      unsigned long count)
1549 {
1550 }
1551 
1552 static inline void __count_memcg_events(struct mem_cgroup *memcg,
1553 					enum vm_event_item idx,
1554 					unsigned long count)
1555 {
1556 }
1557 
1558 static inline void count_memcg_page_event(struct page *page,
1559 					  int idx)
1560 {
1561 }
1562 
1563 static inline void count_memcg_folio_events(struct folio *folio,
1564 		enum vm_event_item idx, unsigned long nr)
1565 {
1566 }
1567 
1568 static inline
1569 void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
1570 {
1571 }
1572 
1573 static inline void split_page_memcg(struct page *head, unsigned int nr)
1574 {
1575 }
1576 
1577 static inline
1578 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1579 					    gfp_t gfp_mask,
1580 					    unsigned long *total_scanned)
1581 {
1582 	return 0;
1583 }
1584 #endif /* CONFIG_MEMCG */
1585 
1586 static inline void __inc_lruvec_kmem_state(void *p, enum node_stat_item idx)
1587 {
1588 	__mod_lruvec_kmem_state(p, idx, 1);
1589 }
1590 
1591 static inline void __dec_lruvec_kmem_state(void *p, enum node_stat_item idx)
1592 {
1593 	__mod_lruvec_kmem_state(p, idx, -1);
1594 }
1595 
1596 static inline struct lruvec *parent_lruvec(struct lruvec *lruvec)
1597 {
1598 	struct mem_cgroup *memcg;
1599 
1600 	memcg = lruvec_memcg(lruvec);
1601 	if (!memcg)
1602 		return NULL;
1603 	memcg = parent_mem_cgroup(memcg);
1604 	if (!memcg)
1605 		return NULL;
1606 	return mem_cgroup_lruvec(memcg, lruvec_pgdat(lruvec));
1607 }
1608 
1609 static inline void unlock_page_lruvec(struct lruvec *lruvec)
1610 {
1611 	spin_unlock(&lruvec->lru_lock);
1612 }
1613 
1614 static inline void unlock_page_lruvec_irq(struct lruvec *lruvec)
1615 {
1616 	spin_unlock_irq(&lruvec->lru_lock);
1617 }
1618 
1619 static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec,
1620 		unsigned long flags)
1621 {
1622 	spin_unlock_irqrestore(&lruvec->lru_lock, flags);
1623 }
1624 
1625 /* Test requires a stable page->memcg binding, see page_memcg() */
1626 static inline bool folio_matches_lruvec(struct folio *folio,
1627 		struct lruvec *lruvec)
1628 {
1629 	return lruvec_pgdat(lruvec) == folio_pgdat(folio) &&
1630 	       lruvec_memcg(lruvec) == folio_memcg(folio);
1631 }
1632 
1633 /* Don't lock again iff page's lruvec locked */
1634 static inline struct lruvec *folio_lruvec_relock_irq(struct folio *folio,
1635 		struct lruvec *locked_lruvec)
1636 {
1637 	if (locked_lruvec) {
1638 		if (folio_matches_lruvec(folio, locked_lruvec))
1639 			return locked_lruvec;
1640 
1641 		unlock_page_lruvec_irq(locked_lruvec);
1642 	}
1643 
1644 	return folio_lruvec_lock_irq(folio);
1645 }
1646 
1647 /* Don't lock again iff page's lruvec locked */
1648 static inline struct lruvec *folio_lruvec_relock_irqsave(struct folio *folio,
1649 		struct lruvec *locked_lruvec, unsigned long *flags)
1650 {
1651 	if (locked_lruvec) {
1652 		if (folio_matches_lruvec(folio, locked_lruvec))
1653 			return locked_lruvec;
1654 
1655 		unlock_page_lruvec_irqrestore(locked_lruvec, *flags);
1656 	}
1657 
1658 	return folio_lruvec_lock_irqsave(folio, flags);
1659 }
1660 
1661 #ifdef CONFIG_CGROUP_WRITEBACK
1662 
1663 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
1664 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1665 			 unsigned long *pheadroom, unsigned long *pdirty,
1666 			 unsigned long *pwriteback);
1667 
1668 void mem_cgroup_track_foreign_dirty_slowpath(struct folio *folio,
1669 					     struct bdi_writeback *wb);
1670 
1671 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio,
1672 						  struct bdi_writeback *wb)
1673 {
1674 	struct mem_cgroup *memcg;
1675 
1676 	if (mem_cgroup_disabled())
1677 		return;
1678 
1679 	memcg = folio_memcg(folio);
1680 	if (unlikely(memcg && &memcg->css != wb->memcg_css))
1681 		mem_cgroup_track_foreign_dirty_slowpath(folio, wb);
1682 }
1683 
1684 void mem_cgroup_flush_foreign(struct bdi_writeback *wb);
1685 
1686 #else	/* CONFIG_CGROUP_WRITEBACK */
1687 
1688 static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1689 {
1690 	return NULL;
1691 }
1692 
1693 static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
1694 				       unsigned long *pfilepages,
1695 				       unsigned long *pheadroom,
1696 				       unsigned long *pdirty,
1697 				       unsigned long *pwriteback)
1698 {
1699 }
1700 
1701 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio,
1702 						  struct bdi_writeback *wb)
1703 {
1704 }
1705 
1706 static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
1707 {
1708 }
1709 
1710 #endif	/* CONFIG_CGROUP_WRITEBACK */
1711 
1712 struct sock;
1713 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
1714 			     gfp_t gfp_mask);
1715 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1716 #ifdef CONFIG_MEMCG
1717 extern struct static_key_false memcg_sockets_enabled_key;
1718 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
1719 void mem_cgroup_sk_alloc(struct sock *sk);
1720 void mem_cgroup_sk_free(struct sock *sk);
1721 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1722 {
1723 	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1724 		return !!memcg->tcpmem_pressure;
1725 	do {
1726 		if (time_before(jiffies, READ_ONCE(memcg->socket_pressure)))
1727 			return true;
1728 	} while ((memcg = parent_mem_cgroup(memcg)));
1729 	return false;
1730 }
1731 
1732 int alloc_shrinker_info(struct mem_cgroup *memcg);
1733 void free_shrinker_info(struct mem_cgroup *memcg);
1734 void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id);
1735 void reparent_shrinker_deferred(struct mem_cgroup *memcg);
1736 #else
1737 #define mem_cgroup_sockets_enabled 0
1738 static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1739 static inline void mem_cgroup_sk_free(struct sock *sk) { };
1740 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1741 {
1742 	return false;
1743 }
1744 
1745 static inline void set_shrinker_bit(struct mem_cgroup *memcg,
1746 				    int nid, int shrinker_id)
1747 {
1748 }
1749 #endif
1750 
1751 #ifdef CONFIG_MEMCG_KMEM
1752 bool mem_cgroup_kmem_disabled(void);
1753 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order);
1754 void __memcg_kmem_uncharge_page(struct page *page, int order);
1755 
1756 struct obj_cgroup *get_obj_cgroup_from_current(void);
1757 struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio);
1758 
1759 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size);
1760 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size);
1761 
1762 extern struct static_key_false memcg_bpf_enabled_key;
1763 static inline bool memcg_bpf_enabled(void)
1764 {
1765 	return static_branch_likely(&memcg_bpf_enabled_key);
1766 }
1767 
1768 extern struct static_key_false memcg_kmem_online_key;
1769 
1770 static inline bool memcg_kmem_online(void)
1771 {
1772 	return static_branch_likely(&memcg_kmem_online_key);
1773 }
1774 
1775 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1776 					 int order)
1777 {
1778 	if (memcg_kmem_online())
1779 		return __memcg_kmem_charge_page(page, gfp, order);
1780 	return 0;
1781 }
1782 
1783 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1784 {
1785 	if (memcg_kmem_online())
1786 		__memcg_kmem_uncharge_page(page, order);
1787 }
1788 
1789 /*
1790  * A helper for accessing memcg's kmem_id, used for getting
1791  * corresponding LRU lists.
1792  */
1793 static inline int memcg_kmem_id(struct mem_cgroup *memcg)
1794 {
1795 	return memcg ? memcg->kmemcg_id : -1;
1796 }
1797 
1798 struct mem_cgroup *mem_cgroup_from_obj(void *p);
1799 struct mem_cgroup *mem_cgroup_from_slab_obj(void *p);
1800 
1801 static inline void count_objcg_event(struct obj_cgroup *objcg,
1802 				     enum vm_event_item idx)
1803 {
1804 	struct mem_cgroup *memcg;
1805 
1806 	if (!memcg_kmem_online())
1807 		return;
1808 
1809 	rcu_read_lock();
1810 	memcg = obj_cgroup_memcg(objcg);
1811 	count_memcg_events(memcg, idx, 1);
1812 	rcu_read_unlock();
1813 }
1814 
1815 #else
1816 static inline bool mem_cgroup_kmem_disabled(void)
1817 {
1818 	return true;
1819 }
1820 
1821 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1822 					 int order)
1823 {
1824 	return 0;
1825 }
1826 
1827 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1828 {
1829 }
1830 
1831 static inline int __memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1832 					   int order)
1833 {
1834 	return 0;
1835 }
1836 
1837 static inline void __memcg_kmem_uncharge_page(struct page *page, int order)
1838 {
1839 }
1840 
1841 static inline struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio)
1842 {
1843 	return NULL;
1844 }
1845 
1846 static inline bool memcg_bpf_enabled(void)
1847 {
1848 	return false;
1849 }
1850 
1851 static inline bool memcg_kmem_online(void)
1852 {
1853 	return false;
1854 }
1855 
1856 static inline int memcg_kmem_id(struct mem_cgroup *memcg)
1857 {
1858 	return -1;
1859 }
1860 
1861 static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)
1862 {
1863 	return NULL;
1864 }
1865 
1866 static inline struct mem_cgroup *mem_cgroup_from_slab_obj(void *p)
1867 {
1868 	return NULL;
1869 }
1870 
1871 static inline void count_objcg_event(struct obj_cgroup *objcg,
1872 				     enum vm_event_item idx)
1873 {
1874 }
1875 
1876 #endif /* CONFIG_MEMCG_KMEM */
1877 
1878 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
1879 bool obj_cgroup_may_zswap(struct obj_cgroup *objcg);
1880 void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size);
1881 void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size);
1882 #else
1883 static inline bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
1884 {
1885 	return true;
1886 }
1887 static inline void obj_cgroup_charge_zswap(struct obj_cgroup *objcg,
1888 					   size_t size)
1889 {
1890 }
1891 static inline void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg,
1892 					     size_t size)
1893 {
1894 }
1895 #endif
1896 
1897 #endif /* _LINUX_MEMCONTROL_H */
1898