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