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