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