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