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