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