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