xref: /linux-6.15/include/linux/memcontrol.h (revision 50f9481e)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* memcontrol.h - Memory Controller
3  *
4  * Copyright IBM Corporation, 2007
5  * Author Balbir Singh <[email protected]>
6  *
7  * Copyright 2007 OpenVZ SWsoft Inc
8  * Author: Pavel Emelianov <[email protected]>
9  */
10 
11 #ifndef _LINUX_MEMCONTROL_H
12 #define _LINUX_MEMCONTROL_H
13 #include <linux/cgroup.h>
14 #include <linux/vm_event_item.h>
15 #include <linux/hardirq.h>
16 #include <linux/jump_label.h>
17 #include <linux/page_counter.h>
18 #include <linux/vmpressure.h>
19 #include <linux/eventfd.h>
20 #include <linux/mm.h>
21 #include <linux/vmstat.h>
22 #include <linux/writeback.h>
23 #include <linux/page-flags.h>
24 
25 struct mem_cgroup;
26 struct obj_cgroup;
27 struct page;
28 struct mm_struct;
29 struct kmem_cache;
30 
31 /* Cgroup-specific page state, on top of universal node page state */
32 enum memcg_stat_item {
33 	MEMCG_SWAP = NR_VM_NODE_STAT_ITEMS,
34 	MEMCG_SOCK,
35 	MEMCG_PERCPU_B,
36 	MEMCG_NR_STAT,
37 };
38 
39 enum memcg_memory_event {
40 	MEMCG_LOW,
41 	MEMCG_HIGH,
42 	MEMCG_MAX,
43 	MEMCG_OOM,
44 	MEMCG_OOM_KILL,
45 	MEMCG_SWAP_HIGH,
46 	MEMCG_SWAP_MAX,
47 	MEMCG_SWAP_FAIL,
48 	MEMCG_NR_MEMORY_EVENTS,
49 };
50 
51 struct mem_cgroup_reclaim_cookie {
52 	pg_data_t *pgdat;
53 	unsigned int generation;
54 };
55 
56 #ifdef CONFIG_MEMCG
57 
58 #define MEM_CGROUP_ID_SHIFT	16
59 #define MEM_CGROUP_ID_MAX	USHRT_MAX
60 
61 struct mem_cgroup_id {
62 	int id;
63 	refcount_t ref;
64 };
65 
66 /*
67  * Per memcg event counter is incremented at every pagein/pageout. With THP,
68  * it will be incremented by the number of pages. This counter is used
69  * to trigger some periodic events. This is straightforward and better
70  * than using jiffies etc. to handle periodic memcg event.
71  */
72 enum mem_cgroup_events_target {
73 	MEM_CGROUP_TARGET_THRESH,
74 	MEM_CGROUP_TARGET_SOFTLIMIT,
75 	MEM_CGROUP_NTARGETS,
76 };
77 
78 struct memcg_vmstats_percpu {
79 	/* Local (CPU and cgroup) page state & events */
80 	long			state[MEMCG_NR_STAT];
81 	unsigned long		events[NR_VM_EVENT_ITEMS];
82 
83 	/* Delta calculation for lockless upward propagation */
84 	long			state_prev[MEMCG_NR_STAT];
85 	unsigned long		events_prev[NR_VM_EVENT_ITEMS];
86 
87 	/* Cgroup1: threshold notifications & softlimit tree updates */
88 	unsigned long		nr_page_events;
89 	unsigned long		targets[MEM_CGROUP_NTARGETS];
90 };
91 
92 struct memcg_vmstats {
93 	/* Aggregated (CPU and subtree) page state & events */
94 	long			state[MEMCG_NR_STAT];
95 	unsigned long		events[NR_VM_EVENT_ITEMS];
96 
97 	/* Pending child counts during tree propagation */
98 	long			state_pending[MEMCG_NR_STAT];
99 	unsigned long		events_pending[NR_VM_EVENT_ITEMS];
100 };
101 
102 struct mem_cgroup_reclaim_iter {
103 	struct mem_cgroup *position;
104 	/* scan generation, increased every round-trip */
105 	unsigned int generation;
106 };
107 
108 /*
109  * Bitmap and deferred work of shrinker::id corresponding to memcg-aware
110  * shrinkers, which have elements charged to this memcg.
111  */
112 struct shrinker_info {
113 	struct rcu_head rcu;
114 	atomic_long_t *nr_deferred;
115 	unsigned long *map;
116 };
117 
118 struct lruvec_stats_percpu {
119 	/* Local (CPU and cgroup) state */
120 	long state[NR_VM_NODE_STAT_ITEMS];
121 
122 	/* Delta calculation for lockless upward propagation */
123 	long state_prev[NR_VM_NODE_STAT_ITEMS];
124 };
125 
126 struct lruvec_stats {
127 	/* Aggregated (CPU and subtree) state */
128 	long state[NR_VM_NODE_STAT_ITEMS];
129 
130 	/* Pending child counts during tree propagation */
131 	long state_pending[NR_VM_NODE_STAT_ITEMS];
132 };
133 
134 /*
135  * per-node information in memory controller.
136  */
137 struct mem_cgroup_per_node {
138 	struct lruvec		lruvec;
139 
140 	struct lruvec_stats_percpu __percpu	*lruvec_stats_percpu;
141 	struct lruvec_stats			lruvec_stats;
142 
143 	unsigned long		lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
144 
145 	struct mem_cgroup_reclaim_iter	iter;
146 
147 	struct shrinker_info __rcu	*shrinker_info;
148 
149 	struct rb_node		tree_node;	/* RB tree node */
150 	unsigned long		usage_in_excess;/* Set to the value by which */
151 						/* the soft limit is exceeded*/
152 	bool			on_tree;
153 	struct mem_cgroup	*memcg;		/* Back pointer, we cannot */
154 						/* use container_of	   */
155 };
156 
157 struct mem_cgroup_threshold {
158 	struct eventfd_ctx *eventfd;
159 	unsigned long threshold;
160 };
161 
162 /* For threshold */
163 struct mem_cgroup_threshold_ary {
164 	/* An array index points to threshold just below or equal to usage. */
165 	int current_threshold;
166 	/* Size of entries[] */
167 	unsigned int size;
168 	/* Array of thresholds */
169 	struct mem_cgroup_threshold entries[];
170 };
171 
172 struct mem_cgroup_thresholds {
173 	/* Primary thresholds array */
174 	struct mem_cgroup_threshold_ary *primary;
175 	/*
176 	 * Spare threshold array.
177 	 * This is needed to make mem_cgroup_unregister_event() "never fail".
178 	 * It must be able to store at least primary->size - 1 entries.
179 	 */
180 	struct mem_cgroup_threshold_ary *spare;
181 };
182 
183 #if defined(CONFIG_SMP)
184 struct memcg_padding {
185 	char x[0];
186 } ____cacheline_internodealigned_in_smp;
187 #define MEMCG_PADDING(name)      struct memcg_padding name
188 #else
189 #define MEMCG_PADDING(name)
190 #endif
191 
192 /*
193  * Remember four most recent foreign writebacks with dirty pages in this
194  * cgroup.  Inode sharing is expected to be uncommon and, even if we miss
195  * one in a given round, we're likely to catch it later if it keeps
196  * foreign-dirtying, so a fairly low count should be enough.
197  *
198  * See mem_cgroup_track_foreign_dirty_slowpath() for details.
199  */
200 #define MEMCG_CGWB_FRN_CNT	4
201 
202 struct memcg_cgwb_frn {
203 	u64 bdi_id;			/* bdi->id of the foreign inode */
204 	int memcg_id;			/* memcg->css.id of foreign inode */
205 	u64 at;				/* jiffies_64 at the time of dirtying */
206 	struct wb_completion done;	/* tracks in-flight foreign writebacks */
207 };
208 
209 /*
210  * Bucket for arbitrarily byte-sized objects charged to a memory
211  * cgroup. The bucket can be reparented in one piece when the cgroup
212  * is destroyed, without having to round up the individual references
213  * of all live memory objects in the wild.
214  */
215 struct obj_cgroup {
216 	struct percpu_ref refcnt;
217 	struct mem_cgroup *memcg;
218 	atomic_t nr_charged_bytes;
219 	union {
220 		struct list_head list;
221 		struct rcu_head rcu;
222 	};
223 };
224 
225 /*
226  * The memory controller data structure. The memory controller controls both
227  * page cache and RSS per cgroup. We would eventually like to provide
228  * statistics based on the statistics developed by Rik Van Riel for clock-pro,
229  * to help the administrator determine what knobs to tune.
230  */
231 struct mem_cgroup {
232 	struct cgroup_subsys_state css;
233 
234 	/* Private memcg ID. Used to ID objects that outlive the cgroup */
235 	struct mem_cgroup_id id;
236 
237 	/* Accounted resources */
238 	struct page_counter memory;		/* Both v1 & v2 */
239 
240 	union {
241 		struct page_counter swap;	/* v2 only */
242 		struct page_counter memsw;	/* v1 only */
243 	};
244 
245 	/* Legacy consumer-oriented counters */
246 	struct page_counter kmem;		/* v1 only */
247 	struct page_counter tcpmem;		/* v1 only */
248 
249 	/* Range enforcement for interrupt charges */
250 	struct work_struct high_work;
251 
252 	unsigned long soft_limit;
253 
254 	/* vmpressure notifications */
255 	struct vmpressure vmpressure;
256 
257 	/*
258 	 * Should the OOM killer kill all belonging tasks, had it kill one?
259 	 */
260 	bool oom_group;
261 
262 	/* protected by memcg_oom_lock */
263 	bool		oom_lock;
264 	int		under_oom;
265 
266 	int	swappiness;
267 	/* OOM-Killer disable */
268 	int		oom_kill_disable;
269 
270 	/* memory.events and memory.events.local */
271 	struct cgroup_file events_file;
272 	struct cgroup_file events_local_file;
273 
274 	/* handle for "memory.swap.events" */
275 	struct cgroup_file swap_events_file;
276 
277 	/* protect arrays of thresholds */
278 	struct mutex thresholds_lock;
279 
280 	/* thresholds for memory usage. RCU-protected */
281 	struct mem_cgroup_thresholds thresholds;
282 
283 	/* thresholds for mem+swap usage. RCU-protected */
284 	struct mem_cgroup_thresholds memsw_thresholds;
285 
286 	/* For oom notifier event fd */
287 	struct list_head oom_notify;
288 
289 	/*
290 	 * Should we move charges of a task when a task is moved into this
291 	 * mem_cgroup ? And what type of charges should we move ?
292 	 */
293 	unsigned long move_charge_at_immigrate;
294 	/* taken only while moving_account > 0 */
295 	spinlock_t		move_lock;
296 	unsigned long		move_lock_flags;
297 
298 	MEMCG_PADDING(_pad1_);
299 
300 	/* memory.stat */
301 	struct memcg_vmstats	vmstats;
302 
303 	/* memory.events */
304 	atomic_long_t		memory_events[MEMCG_NR_MEMORY_EVENTS];
305 	atomic_long_t		memory_events_local[MEMCG_NR_MEMORY_EVENTS];
306 
307 	unsigned long		socket_pressure;
308 
309 	/* Legacy tcp memory accounting */
310 	bool			tcpmem_active;
311 	int			tcpmem_pressure;
312 
313 #ifdef CONFIG_MEMCG_KMEM
314 	int kmemcg_id;
315 	struct obj_cgroup __rcu *objcg;
316 	struct list_head objcg_list; /* list of inherited objcgs */
317 #endif
318 
319 	MEMCG_PADDING(_pad2_);
320 
321 	/*
322 	 * set > 0 if pages under this cgroup are moving to other cgroup.
323 	 */
324 	atomic_t		moving_account;
325 	struct task_struct	*move_lock_task;
326 
327 	struct memcg_vmstats_percpu __percpu *vmstats_percpu;
328 
329 #ifdef CONFIG_CGROUP_WRITEBACK
330 	struct list_head cgwb_list;
331 	struct wb_domain cgwb_domain;
332 	struct memcg_cgwb_frn cgwb_frn[MEMCG_CGWB_FRN_CNT];
333 #endif
334 
335 	/* List of events which userspace want to receive */
336 	struct list_head event_list;
337 	spinlock_t event_list_lock;
338 
339 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
340 	struct deferred_split deferred_split_queue;
341 #endif
342 
343 	struct mem_cgroup_per_node *nodeinfo[];
344 };
345 
346 /*
347  * size of first charge trial. "32" comes from vmscan.c's magic value.
348  * TODO: maybe necessary to use big numbers in big irons.
349  */
350 #define MEMCG_CHARGE_BATCH 32U
351 
352 extern struct mem_cgroup *root_mem_cgroup;
353 
354 enum page_memcg_data_flags {
355 	/* page->memcg_data is a pointer to an objcgs vector */
356 	MEMCG_DATA_OBJCGS = (1UL << 0),
357 	/* page has been accounted as a non-slab kernel page */
358 	MEMCG_DATA_KMEM = (1UL << 1),
359 	/* the next bit after the last actual flag */
360 	__NR_MEMCG_DATA_FLAGS  = (1UL << 2),
361 };
362 
363 #define MEMCG_DATA_FLAGS_MASK (__NR_MEMCG_DATA_FLAGS - 1)
364 
365 static inline bool PageMemcgKmem(struct page *page);
366 
367 /*
368  * After the initialization objcg->memcg is always pointing at
369  * a valid memcg, but can be atomically swapped to the parent memcg.
370  *
371  * The caller must ensure that the returned memcg won't be released:
372  * e.g. acquire the rcu_read_lock or css_set_lock.
373  */
374 static inline struct mem_cgroup *obj_cgroup_memcg(struct obj_cgroup *objcg)
375 {
376 	return READ_ONCE(objcg->memcg);
377 }
378 
379 /*
380  * __page_memcg - get the memory cgroup associated with a non-kmem page
381  * @page: a pointer to the page struct
382  *
383  * Returns a pointer to the memory cgroup associated with the page,
384  * or NULL. This function assumes that the page is known to have a
385  * proper memory cgroup pointer. It's not safe to call this function
386  * against some type of pages, e.g. slab pages or ex-slab pages or
387  * kmem pages.
388  */
389 static inline struct mem_cgroup *__page_memcg(struct page *page)
390 {
391 	unsigned long memcg_data = page->memcg_data;
392 
393 	VM_BUG_ON_PAGE(PageSlab(page), page);
394 	VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_OBJCGS, page);
395 	VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, page);
396 
397 	return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
398 }
399 
400 /*
401  * __page_objcg - get the object cgroup associated with a kmem page
402  * @page: a pointer to the page struct
403  *
404  * Returns a pointer to the object cgroup associated with the page,
405  * or NULL. This function assumes that the page is known to have a
406  * proper object cgroup pointer. It's not safe to call this function
407  * against some type of pages, e.g. slab pages or ex-slab pages or
408  * LRU pages.
409  */
410 static inline struct obj_cgroup *__page_objcg(struct page *page)
411 {
412 	unsigned long memcg_data = page->memcg_data;
413 
414 	VM_BUG_ON_PAGE(PageSlab(page), page);
415 	VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_OBJCGS, page);
416 	VM_BUG_ON_PAGE(!(memcg_data & MEMCG_DATA_KMEM), page);
417 
418 	return (struct obj_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
419 }
420 
421 /*
422  * page_memcg - get the memory cgroup associated with a page
423  * @page: a pointer to the page struct
424  *
425  * Returns a pointer to the memory cgroup associated with the page,
426  * or NULL. This function assumes that the page is known to have a
427  * proper memory cgroup pointer. It's not safe to call this function
428  * against some type of pages, e.g. slab pages or ex-slab pages.
429  *
430  * For a non-kmem page any of the following ensures page and memcg binding
431  * stability:
432  *
433  * - the page lock
434  * - LRU isolation
435  * - lock_page_memcg()
436  * - exclusive reference
437  *
438  * For a kmem page a caller should hold an rcu read lock to protect memcg
439  * associated with a kmem page from being released.
440  */
441 static inline struct mem_cgroup *page_memcg(struct page *page)
442 {
443 	if (PageMemcgKmem(page))
444 		return obj_cgroup_memcg(__page_objcg(page));
445 	else
446 		return __page_memcg(page);
447 }
448 
449 /*
450  * page_memcg_rcu - locklessly get the memory cgroup associated with a page
451  * @page: a pointer to the page struct
452  *
453  * Returns a pointer to the memory cgroup associated with the page,
454  * or NULL. This function assumes that the page is known to have a
455  * proper memory cgroup pointer. It's not safe to call this function
456  * against some type of pages, e.g. slab pages or ex-slab pages.
457  */
458 static inline struct mem_cgroup *page_memcg_rcu(struct page *page)
459 {
460 	unsigned long memcg_data = READ_ONCE(page->memcg_data);
461 
462 	VM_BUG_ON_PAGE(PageSlab(page), page);
463 	WARN_ON_ONCE(!rcu_read_lock_held());
464 
465 	if (memcg_data & MEMCG_DATA_KMEM) {
466 		struct obj_cgroup *objcg;
467 
468 		objcg = (void *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
469 		return obj_cgroup_memcg(objcg);
470 	}
471 
472 	return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
473 }
474 
475 /*
476  * page_memcg_check - get the memory cgroup associated with a page
477  * @page: a pointer to the page struct
478  *
479  * Returns a pointer to the memory cgroup associated with the page,
480  * or NULL. This function unlike page_memcg() can take any page
481  * as an argument. It has to be used in cases when it's not known if a page
482  * has an associated memory cgroup pointer or an object cgroups vector or
483  * an object cgroup.
484  *
485  * For a non-kmem page any of the following ensures page and memcg binding
486  * stability:
487  *
488  * - the page lock
489  * - LRU isolation
490  * - lock_page_memcg()
491  * - exclusive reference
492  *
493  * For a kmem page a caller should hold an rcu read lock to protect memcg
494  * associated with a kmem page from being released.
495  */
496 static inline struct mem_cgroup *page_memcg_check(struct page *page)
497 {
498 	/*
499 	 * Because page->memcg_data might be changed asynchronously
500 	 * for slab pages, READ_ONCE() should be used here.
501 	 */
502 	unsigned long memcg_data = READ_ONCE(page->memcg_data);
503 
504 	if (memcg_data & MEMCG_DATA_OBJCGS)
505 		return NULL;
506 
507 	if (memcg_data & MEMCG_DATA_KMEM) {
508 		struct obj_cgroup *objcg;
509 
510 		objcg = (void *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
511 		return obj_cgroup_memcg(objcg);
512 	}
513 
514 	return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
515 }
516 
517 #ifdef CONFIG_MEMCG_KMEM
518 /*
519  * PageMemcgKmem - check if the page has MemcgKmem flag set
520  * @page: a pointer to the page struct
521  *
522  * Checks if the page has MemcgKmem flag set. The caller must ensure that
523  * the page has an associated memory cgroup. It's not safe to call this function
524  * against some types of pages, e.g. slab pages.
525  */
526 static inline bool PageMemcgKmem(struct page *page)
527 {
528 	VM_BUG_ON_PAGE(page->memcg_data & MEMCG_DATA_OBJCGS, page);
529 	return page->memcg_data & MEMCG_DATA_KMEM;
530 }
531 
532 /*
533  * page_objcgs - get the object cgroups vector associated with a page
534  * @page: a pointer to the page struct
535  *
536  * Returns a pointer to the object cgroups vector associated with the page,
537  * or NULL. This function assumes that the page is known to have an
538  * associated object cgroups vector. It's not safe to call this function
539  * against pages, which might have an associated memory cgroup: e.g.
540  * kernel stack pages.
541  */
542 static inline struct obj_cgroup **page_objcgs(struct page *page)
543 {
544 	unsigned long memcg_data = READ_ONCE(page->memcg_data);
545 
546 	VM_BUG_ON_PAGE(memcg_data && !(memcg_data & MEMCG_DATA_OBJCGS), page);
547 	VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, page);
548 
549 	return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
550 }
551 
552 /*
553  * page_objcgs_check - get the object cgroups vector associated with a page
554  * @page: a pointer to the page struct
555  *
556  * Returns a pointer to the object cgroups vector associated with the page,
557  * or NULL. This function is safe to use if the page can be directly associated
558  * with a memory cgroup.
559  */
560 static inline struct obj_cgroup **page_objcgs_check(struct page *page)
561 {
562 	unsigned long memcg_data = READ_ONCE(page->memcg_data);
563 
564 	if (!memcg_data || !(memcg_data & MEMCG_DATA_OBJCGS))
565 		return NULL;
566 
567 	VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, page);
568 
569 	return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
570 }
571 
572 #else
573 static inline bool PageMemcgKmem(struct page *page)
574 {
575 	return false;
576 }
577 
578 static inline struct obj_cgroup **page_objcgs(struct page *page)
579 {
580 	return NULL;
581 }
582 
583 static inline struct obj_cgroup **page_objcgs_check(struct page *page)
584 {
585 	return NULL;
586 }
587 #endif
588 
589 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
590 {
591 	return (memcg == root_mem_cgroup);
592 }
593 
594 static inline bool mem_cgroup_disabled(void)
595 {
596 	return !cgroup_subsys_enabled(memory_cgrp_subsys);
597 }
598 
599 static inline void mem_cgroup_protection(struct mem_cgroup *root,
600 					 struct mem_cgroup *memcg,
601 					 unsigned long *min,
602 					 unsigned long *low)
603 {
604 	*min = *low = 0;
605 
606 	if (mem_cgroup_disabled())
607 		return;
608 
609 	/*
610 	 * There is no reclaim protection applied to a targeted reclaim.
611 	 * We are special casing this specific case here because
612 	 * mem_cgroup_protected calculation is not robust enough to keep
613 	 * the protection invariant for calculated effective values for
614 	 * parallel reclaimers with different reclaim target. This is
615 	 * especially a problem for tail memcgs (as they have pages on LRU)
616 	 * which would want to have effective values 0 for targeted reclaim
617 	 * but a different value for external reclaim.
618 	 *
619 	 * Example
620 	 * Let's have global and A's reclaim in parallel:
621 	 *  |
622 	 *  A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G)
623 	 *  |\
624 	 *  | C (low = 1G, usage = 2.5G)
625 	 *  B (low = 1G, usage = 0.5G)
626 	 *
627 	 * For the global reclaim
628 	 * A.elow = A.low
629 	 * B.elow = min(B.usage, B.low) because children_low_usage <= A.elow
630 	 * C.elow = min(C.usage, C.low)
631 	 *
632 	 * With the effective values resetting we have A reclaim
633 	 * A.elow = 0
634 	 * B.elow = B.low
635 	 * C.elow = C.low
636 	 *
637 	 * If the global reclaim races with A's reclaim then
638 	 * B.elow = C.elow = 0 because children_low_usage > A.elow)
639 	 * is possible and reclaiming B would be violating the protection.
640 	 *
641 	 */
642 	if (root == memcg)
643 		return;
644 
645 	*min = READ_ONCE(memcg->memory.emin);
646 	*low = READ_ONCE(memcg->memory.elow);
647 }
648 
649 void mem_cgroup_calculate_protection(struct mem_cgroup *root,
650 				     struct mem_cgroup *memcg);
651 
652 static inline bool mem_cgroup_supports_protection(struct mem_cgroup *memcg)
653 {
654 	/*
655 	 * The root memcg doesn't account charges, and doesn't support
656 	 * protection.
657 	 */
658 	return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg);
659 
660 }
661 
662 static inline bool mem_cgroup_below_low(struct mem_cgroup *memcg)
663 {
664 	if (!mem_cgroup_supports_protection(memcg))
665 		return false;
666 
667 	return READ_ONCE(memcg->memory.elow) >=
668 		page_counter_read(&memcg->memory);
669 }
670 
671 static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg)
672 {
673 	if (!mem_cgroup_supports_protection(memcg))
674 		return false;
675 
676 	return READ_ONCE(memcg->memory.emin) >=
677 		page_counter_read(&memcg->memory);
678 }
679 
680 int __mem_cgroup_charge(struct page *page, struct mm_struct *mm,
681 			gfp_t gfp_mask);
682 static inline int mem_cgroup_charge(struct page *page, struct mm_struct *mm,
683 				    gfp_t gfp_mask)
684 {
685 	if (mem_cgroup_disabled())
686 		return 0;
687 	return __mem_cgroup_charge(page, mm, gfp_mask);
688 }
689 
690 int mem_cgroup_swapin_charge_page(struct page *page, struct mm_struct *mm,
691 				  gfp_t gfp, swp_entry_t entry);
692 void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry);
693 
694 void __mem_cgroup_uncharge(struct page *page);
695 static inline void mem_cgroup_uncharge(struct page *page)
696 {
697 	if (mem_cgroup_disabled())
698 		return;
699 	__mem_cgroup_uncharge(page);
700 }
701 
702 void __mem_cgroup_uncharge_list(struct list_head *page_list);
703 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
704 {
705 	if (mem_cgroup_disabled())
706 		return;
707 	__mem_cgroup_uncharge_list(page_list);
708 }
709 
710 void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
711 
712 /**
713  * mem_cgroup_lruvec - get the lru list vector for a memcg & node
714  * @memcg: memcg of the wanted lruvec
715  * @pgdat: pglist_data
716  *
717  * Returns the lru list vector holding pages for a given @memcg &
718  * @pgdat combination. This can be the node lruvec, if the memory
719  * controller is disabled.
720  */
721 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
722 					       struct pglist_data *pgdat)
723 {
724 	struct mem_cgroup_per_node *mz;
725 	struct lruvec *lruvec;
726 
727 	if (mem_cgroup_disabled()) {
728 		lruvec = &pgdat->__lruvec;
729 		goto out;
730 	}
731 
732 	if (!memcg)
733 		memcg = root_mem_cgroup;
734 
735 	mz = memcg->nodeinfo[pgdat->node_id];
736 	lruvec = &mz->lruvec;
737 out:
738 	/*
739 	 * Since a node can be onlined after the mem_cgroup was created,
740 	 * we have to be prepared to initialize lruvec->pgdat here;
741 	 * and if offlined then reonlined, we need to reinitialize it.
742 	 */
743 	if (unlikely(lruvec->pgdat != pgdat))
744 		lruvec->pgdat = pgdat;
745 	return lruvec;
746 }
747 
748 /**
749  * mem_cgroup_page_lruvec - return lruvec for isolating/putting an LRU page
750  * @page: the page
751  *
752  * This function relies on page->mem_cgroup being stable.
753  */
754 static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page)
755 {
756 	pg_data_t *pgdat = page_pgdat(page);
757 	struct mem_cgroup *memcg = page_memcg(page);
758 
759 	VM_WARN_ON_ONCE_PAGE(!memcg && !mem_cgroup_disabled(), page);
760 	return mem_cgroup_lruvec(memcg, pgdat);
761 }
762 
763 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
764 
765 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
766 
767 struct lruvec *lock_page_lruvec(struct page *page);
768 struct lruvec *lock_page_lruvec_irq(struct page *page);
769 struct lruvec *lock_page_lruvec_irqsave(struct page *page,
770 						unsigned long *flags);
771 
772 #ifdef CONFIG_DEBUG_VM
773 void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page);
774 #else
775 static inline void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page)
776 {
777 }
778 #endif
779 
780 static inline
781 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
782 	return css ? container_of(css, struct mem_cgroup, css) : NULL;
783 }
784 
785 static inline bool obj_cgroup_tryget(struct obj_cgroup *objcg)
786 {
787 	return percpu_ref_tryget(&objcg->refcnt);
788 }
789 
790 static inline void obj_cgroup_get(struct obj_cgroup *objcg)
791 {
792 	percpu_ref_get(&objcg->refcnt);
793 }
794 
795 static inline void obj_cgroup_get_many(struct obj_cgroup *objcg,
796 				       unsigned long nr)
797 {
798 	percpu_ref_get_many(&objcg->refcnt, nr);
799 }
800 
801 static inline void obj_cgroup_put(struct obj_cgroup *objcg)
802 {
803 	percpu_ref_put(&objcg->refcnt);
804 }
805 
806 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
807 {
808 	if (memcg)
809 		css_put(&memcg->css);
810 }
811 
812 #define mem_cgroup_from_counter(counter, member)	\
813 	container_of(counter, struct mem_cgroup, member)
814 
815 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
816 				   struct mem_cgroup *,
817 				   struct mem_cgroup_reclaim_cookie *);
818 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
819 int mem_cgroup_scan_tasks(struct mem_cgroup *,
820 			  int (*)(struct task_struct *, void *), void *);
821 
822 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
823 {
824 	if (mem_cgroup_disabled())
825 		return 0;
826 
827 	return memcg->id.id;
828 }
829 struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
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 or the memory
852  * controller is in legacy no-hierarchy mode.
853  */
854 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
855 {
856 	if (!memcg->memory.parent)
857 		return NULL;
858 	return mem_cgroup_from_counter(memcg->memory.parent, memory);
859 }
860 
861 static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
862 			      struct mem_cgroup *root)
863 {
864 	if (root == memcg)
865 		return true;
866 	return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
867 }
868 
869 static inline bool mm_match_cgroup(struct mm_struct *mm,
870 				   struct mem_cgroup *memcg)
871 {
872 	struct mem_cgroup *task_memcg;
873 	bool match = false;
874 
875 	rcu_read_lock();
876 	task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
877 	if (task_memcg)
878 		match = mem_cgroup_is_descendant(task_memcg, memcg);
879 	rcu_read_unlock();
880 	return match;
881 }
882 
883 struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
884 ino_t page_cgroup_ino(struct page *page);
885 
886 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
887 {
888 	if (mem_cgroup_disabled())
889 		return true;
890 	return !!(memcg->css.flags & CSS_ONLINE);
891 }
892 
893 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
894 		int zid, int nr_pages);
895 
896 static inline
897 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
898 		enum lru_list lru, int zone_idx)
899 {
900 	struct mem_cgroup_per_node *mz;
901 
902 	mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
903 	return READ_ONCE(mz->lru_zone_size[zone_idx][lru]);
904 }
905 
906 void mem_cgroup_handle_over_high(void);
907 
908 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
909 
910 unsigned long mem_cgroup_size(struct mem_cgroup *memcg);
911 
912 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
913 				struct task_struct *p);
914 
915 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg);
916 
917 static inline void mem_cgroup_enter_user_fault(void)
918 {
919 	WARN_ON(current->in_user_fault);
920 	current->in_user_fault = 1;
921 }
922 
923 static inline void mem_cgroup_exit_user_fault(void)
924 {
925 	WARN_ON(!current->in_user_fault);
926 	current->in_user_fault = 0;
927 }
928 
929 static inline bool task_in_memcg_oom(struct task_struct *p)
930 {
931 	return p->memcg_in_oom;
932 }
933 
934 bool mem_cgroup_oom_synchronize(bool wait);
935 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
936 					    struct mem_cgroup *oom_domain);
937 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg);
938 
939 #ifdef CONFIG_MEMCG_SWAP
940 extern bool cgroup_memory_noswap;
941 #endif
942 
943 void lock_page_memcg(struct page *page);
944 void unlock_page_memcg(struct page *page);
945 
946 void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val);
947 
948 /* idx can be of type enum memcg_stat_item or node_stat_item */
949 static inline void mod_memcg_state(struct mem_cgroup *memcg,
950 				   int idx, int val)
951 {
952 	unsigned long flags;
953 
954 	local_irq_save(flags);
955 	__mod_memcg_state(memcg, idx, val);
956 	local_irq_restore(flags);
957 }
958 
959 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
960 {
961 	return READ_ONCE(memcg->vmstats.state[idx]);
962 }
963 
964 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
965 					      enum node_stat_item idx)
966 {
967 	struct mem_cgroup_per_node *pn;
968 
969 	if (mem_cgroup_disabled())
970 		return node_page_state(lruvec_pgdat(lruvec), idx);
971 
972 	pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
973 	return READ_ONCE(pn->lruvec_stats.state[idx]);
974 }
975 
976 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
977 						    enum node_stat_item idx)
978 {
979 	struct mem_cgroup_per_node *pn;
980 	long x = 0;
981 	int cpu;
982 
983 	if (mem_cgroup_disabled())
984 		return node_page_state(lruvec_pgdat(lruvec), idx);
985 
986 	pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
987 	for_each_possible_cpu(cpu)
988 		x += per_cpu(pn->lruvec_stats_percpu->state[idx], cpu);
989 #ifdef CONFIG_SMP
990 	if (x < 0)
991 		x = 0;
992 #endif
993 	return x;
994 }
995 
996 void mem_cgroup_flush_stats(void);
997 
998 void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
999 			      int val);
1000 void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val);
1001 
1002 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1003 					 int val)
1004 {
1005 	unsigned long flags;
1006 
1007 	local_irq_save(flags);
1008 	__mod_lruvec_kmem_state(p, idx, val);
1009 	local_irq_restore(flags);
1010 }
1011 
1012 static inline void mod_memcg_lruvec_state(struct lruvec *lruvec,
1013 					  enum node_stat_item idx, int val)
1014 {
1015 	unsigned long flags;
1016 
1017 	local_irq_save(flags);
1018 	__mod_memcg_lruvec_state(lruvec, idx, val);
1019 	local_irq_restore(flags);
1020 }
1021 
1022 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
1023 			  unsigned long count);
1024 
1025 static inline void count_memcg_events(struct mem_cgroup *memcg,
1026 				      enum vm_event_item idx,
1027 				      unsigned long count)
1028 {
1029 	unsigned long flags;
1030 
1031 	local_irq_save(flags);
1032 	__count_memcg_events(memcg, idx, count);
1033 	local_irq_restore(flags);
1034 }
1035 
1036 static inline void count_memcg_page_event(struct page *page,
1037 					  enum vm_event_item idx)
1038 {
1039 	struct mem_cgroup *memcg = page_memcg(page);
1040 
1041 	if (memcg)
1042 		count_memcg_events(memcg, idx, 1);
1043 }
1044 
1045 static inline void count_memcg_event_mm(struct mm_struct *mm,
1046 					enum vm_event_item idx)
1047 {
1048 	struct mem_cgroup *memcg;
1049 
1050 	if (mem_cgroup_disabled())
1051 		return;
1052 
1053 	rcu_read_lock();
1054 	memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1055 	if (likely(memcg))
1056 		count_memcg_events(memcg, idx, 1);
1057 	rcu_read_unlock();
1058 }
1059 
1060 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1061 				      enum memcg_memory_event event)
1062 {
1063 	bool swap_event = event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX ||
1064 			  event == MEMCG_SWAP_FAIL;
1065 
1066 	atomic_long_inc(&memcg->memory_events_local[event]);
1067 	if (!swap_event)
1068 		cgroup_file_notify(&memcg->events_local_file);
1069 
1070 	do {
1071 		atomic_long_inc(&memcg->memory_events[event]);
1072 		if (swap_event)
1073 			cgroup_file_notify(&memcg->swap_events_file);
1074 		else
1075 			cgroup_file_notify(&memcg->events_file);
1076 
1077 		if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1078 			break;
1079 		if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
1080 			break;
1081 	} while ((memcg = parent_mem_cgroup(memcg)) &&
1082 		 !mem_cgroup_is_root(memcg));
1083 }
1084 
1085 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1086 					 enum memcg_memory_event event)
1087 {
1088 	struct mem_cgroup *memcg;
1089 
1090 	if (mem_cgroup_disabled())
1091 		return;
1092 
1093 	rcu_read_lock();
1094 	memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1095 	if (likely(memcg))
1096 		memcg_memory_event(memcg, event);
1097 	rcu_read_unlock();
1098 }
1099 
1100 void split_page_memcg(struct page *head, unsigned int nr);
1101 
1102 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1103 						gfp_t gfp_mask,
1104 						unsigned long *total_scanned);
1105 
1106 #else /* CONFIG_MEMCG */
1107 
1108 #define MEM_CGROUP_ID_SHIFT	0
1109 #define MEM_CGROUP_ID_MAX	0
1110 
1111 static inline struct mem_cgroup *page_memcg(struct page *page)
1112 {
1113 	return NULL;
1114 }
1115 
1116 static inline struct mem_cgroup *page_memcg_rcu(struct page *page)
1117 {
1118 	WARN_ON_ONCE(!rcu_read_lock_held());
1119 	return NULL;
1120 }
1121 
1122 static inline struct mem_cgroup *page_memcg_check(struct page *page)
1123 {
1124 	return NULL;
1125 }
1126 
1127 static inline bool PageMemcgKmem(struct page *page)
1128 {
1129 	return false;
1130 }
1131 
1132 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
1133 {
1134 	return true;
1135 }
1136 
1137 static inline bool mem_cgroup_disabled(void)
1138 {
1139 	return true;
1140 }
1141 
1142 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1143 				      enum memcg_memory_event event)
1144 {
1145 }
1146 
1147 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1148 					 enum memcg_memory_event event)
1149 {
1150 }
1151 
1152 static inline void mem_cgroup_protection(struct mem_cgroup *root,
1153 					 struct mem_cgroup *memcg,
1154 					 unsigned long *min,
1155 					 unsigned long *low)
1156 {
1157 	*min = *low = 0;
1158 }
1159 
1160 static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root,
1161 						   struct mem_cgroup *memcg)
1162 {
1163 }
1164 
1165 static inline bool mem_cgroup_below_low(struct mem_cgroup *memcg)
1166 {
1167 	return false;
1168 }
1169 
1170 static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg)
1171 {
1172 	return false;
1173 }
1174 
1175 static inline int mem_cgroup_charge(struct page *page, struct mm_struct *mm,
1176 				    gfp_t gfp_mask)
1177 {
1178 	return 0;
1179 }
1180 
1181 static inline int mem_cgroup_swapin_charge_page(struct page *page,
1182 			struct mm_struct *mm, gfp_t gfp, swp_entry_t entry)
1183 {
1184 	return 0;
1185 }
1186 
1187 static inline void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry)
1188 {
1189 }
1190 
1191 static inline void mem_cgroup_uncharge(struct page *page)
1192 {
1193 }
1194 
1195 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
1196 {
1197 }
1198 
1199 static inline void mem_cgroup_migrate(struct page *old, struct page *new)
1200 {
1201 }
1202 
1203 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
1204 					       struct pglist_data *pgdat)
1205 {
1206 	return &pgdat->__lruvec;
1207 }
1208 
1209 static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page)
1210 {
1211 	pg_data_t *pgdat = page_pgdat(page);
1212 
1213 	return &pgdat->__lruvec;
1214 }
1215 
1216 static inline void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page)
1217 {
1218 }
1219 
1220 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
1221 {
1222 	return NULL;
1223 }
1224 
1225 static inline bool mm_match_cgroup(struct mm_struct *mm,
1226 		struct mem_cgroup *memcg)
1227 {
1228 	return true;
1229 }
1230 
1231 static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
1232 {
1233 	return NULL;
1234 }
1235 
1236 static inline
1237 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css)
1238 {
1239 	return NULL;
1240 }
1241 
1242 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
1243 {
1244 }
1245 
1246 static inline struct lruvec *lock_page_lruvec(struct page *page)
1247 {
1248 	struct pglist_data *pgdat = page_pgdat(page);
1249 
1250 	spin_lock(&pgdat->__lruvec.lru_lock);
1251 	return &pgdat->__lruvec;
1252 }
1253 
1254 static inline struct lruvec *lock_page_lruvec_irq(struct page *page)
1255 {
1256 	struct pglist_data *pgdat = page_pgdat(page);
1257 
1258 	spin_lock_irq(&pgdat->__lruvec.lru_lock);
1259 	return &pgdat->__lruvec;
1260 }
1261 
1262 static inline struct lruvec *lock_page_lruvec_irqsave(struct page *page,
1263 		unsigned long *flagsp)
1264 {
1265 	struct pglist_data *pgdat = page_pgdat(page);
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 int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1285 		int (*fn)(struct task_struct *, void *), void *arg)
1286 {
1287 	return 0;
1288 }
1289 
1290 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
1291 {
1292 	return 0;
1293 }
1294 
1295 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
1296 {
1297 	WARN_ON_ONCE(id);
1298 	/* XXX: This should always return root_mem_cgroup */
1299 	return NULL;
1300 }
1301 
1302 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
1303 {
1304 	return NULL;
1305 }
1306 
1307 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
1308 {
1309 	return NULL;
1310 }
1311 
1312 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
1313 {
1314 	return true;
1315 }
1316 
1317 static inline
1318 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
1319 		enum lru_list lru, int zone_idx)
1320 {
1321 	return 0;
1322 }
1323 
1324 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
1325 {
1326 	return 0;
1327 }
1328 
1329 static inline unsigned long mem_cgroup_size(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 lock_page_memcg(struct page *page)
1345 {
1346 }
1347 
1348 static inline void unlock_page_memcg(struct page *page)
1349 {
1350 }
1351 
1352 static inline void mem_cgroup_handle_over_high(void)
1353 {
1354 }
1355 
1356 static inline void mem_cgroup_enter_user_fault(void)
1357 {
1358 }
1359 
1360 static inline void mem_cgroup_exit_user_fault(void)
1361 {
1362 }
1363 
1364 static inline bool task_in_memcg_oom(struct task_struct *p)
1365 {
1366 	return false;
1367 }
1368 
1369 static inline bool mem_cgroup_oom_synchronize(bool wait)
1370 {
1371 	return false;
1372 }
1373 
1374 static inline struct mem_cgroup *mem_cgroup_get_oom_group(
1375 	struct task_struct *victim, struct mem_cgroup *oom_domain)
1376 {
1377 	return NULL;
1378 }
1379 
1380 static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
1381 {
1382 }
1383 
1384 static inline void __mod_memcg_state(struct mem_cgroup *memcg,
1385 				     int idx,
1386 				     int nr)
1387 {
1388 }
1389 
1390 static inline void mod_memcg_state(struct mem_cgroup *memcg,
1391 				   int idx,
1392 				   int nr)
1393 {
1394 }
1395 
1396 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
1397 {
1398 	return 0;
1399 }
1400 
1401 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1402 					      enum node_stat_item idx)
1403 {
1404 	return node_page_state(lruvec_pgdat(lruvec), idx);
1405 }
1406 
1407 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1408 						    enum node_stat_item idx)
1409 {
1410 	return node_page_state(lruvec_pgdat(lruvec), idx);
1411 }
1412 
1413 static inline void mem_cgroup_flush_stats(void)
1414 {
1415 }
1416 
1417 static inline void __mod_memcg_lruvec_state(struct lruvec *lruvec,
1418 					    enum node_stat_item idx, int val)
1419 {
1420 }
1421 
1422 static inline void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1423 					   int val)
1424 {
1425 	struct page *page = virt_to_head_page(p);
1426 
1427 	__mod_node_page_state(page_pgdat(page), idx, val);
1428 }
1429 
1430 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1431 					 int val)
1432 {
1433 	struct page *page = virt_to_head_page(p);
1434 
1435 	mod_node_page_state(page_pgdat(page), idx, val);
1436 }
1437 
1438 static inline void count_memcg_events(struct mem_cgroup *memcg,
1439 				      enum vm_event_item idx,
1440 				      unsigned long count)
1441 {
1442 }
1443 
1444 static inline void __count_memcg_events(struct mem_cgroup *memcg,
1445 					enum vm_event_item idx,
1446 					unsigned long count)
1447 {
1448 }
1449 
1450 static inline void count_memcg_page_event(struct page *page,
1451 					  int idx)
1452 {
1453 }
1454 
1455 static inline
1456 void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
1457 {
1458 }
1459 
1460 static inline void split_page_memcg(struct page *head, unsigned int nr)
1461 {
1462 }
1463 
1464 static inline
1465 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1466 					    gfp_t gfp_mask,
1467 					    unsigned long *total_scanned)
1468 {
1469 	return 0;
1470 }
1471 #endif /* CONFIG_MEMCG */
1472 
1473 static inline void __inc_lruvec_kmem_state(void *p, enum node_stat_item idx)
1474 {
1475 	__mod_lruvec_kmem_state(p, idx, 1);
1476 }
1477 
1478 static inline void __dec_lruvec_kmem_state(void *p, enum node_stat_item idx)
1479 {
1480 	__mod_lruvec_kmem_state(p, idx, -1);
1481 }
1482 
1483 static inline struct lruvec *parent_lruvec(struct lruvec *lruvec)
1484 {
1485 	struct mem_cgroup *memcg;
1486 
1487 	memcg = lruvec_memcg(lruvec);
1488 	if (!memcg)
1489 		return NULL;
1490 	memcg = parent_mem_cgroup(memcg);
1491 	if (!memcg)
1492 		return NULL;
1493 	return mem_cgroup_lruvec(memcg, lruvec_pgdat(lruvec));
1494 }
1495 
1496 static inline void unlock_page_lruvec(struct lruvec *lruvec)
1497 {
1498 	spin_unlock(&lruvec->lru_lock);
1499 }
1500 
1501 static inline void unlock_page_lruvec_irq(struct lruvec *lruvec)
1502 {
1503 	spin_unlock_irq(&lruvec->lru_lock);
1504 }
1505 
1506 static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec,
1507 		unsigned long flags)
1508 {
1509 	spin_unlock_irqrestore(&lruvec->lru_lock, flags);
1510 }
1511 
1512 /* Test requires a stable page->memcg binding, see page_memcg() */
1513 static inline bool page_matches_lruvec(struct page *page, struct lruvec *lruvec)
1514 {
1515 	return lruvec_pgdat(lruvec) == page_pgdat(page) &&
1516 	       lruvec_memcg(lruvec) == page_memcg(page);
1517 }
1518 
1519 /* Don't lock again iff page's lruvec locked */
1520 static inline struct lruvec *relock_page_lruvec_irq(struct page *page,
1521 		struct lruvec *locked_lruvec)
1522 {
1523 	if (locked_lruvec) {
1524 		if (page_matches_lruvec(page, locked_lruvec))
1525 			return locked_lruvec;
1526 
1527 		unlock_page_lruvec_irq(locked_lruvec);
1528 	}
1529 
1530 	return lock_page_lruvec_irq(page);
1531 }
1532 
1533 /* Don't lock again iff page's lruvec locked */
1534 static inline struct lruvec *relock_page_lruvec_irqsave(struct page *page,
1535 		struct lruvec *locked_lruvec, unsigned long *flags)
1536 {
1537 	if (locked_lruvec) {
1538 		if (page_matches_lruvec(page, locked_lruvec))
1539 			return locked_lruvec;
1540 
1541 		unlock_page_lruvec_irqrestore(locked_lruvec, *flags);
1542 	}
1543 
1544 	return lock_page_lruvec_irqsave(page, 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 page *page,
1555 					     struct bdi_writeback *wb);
1556 
1557 static inline void mem_cgroup_track_foreign_dirty(struct page *page,
1558 						  struct bdi_writeback *wb)
1559 {
1560 	if (mem_cgroup_disabled())
1561 		return;
1562 
1563 	if (unlikely(&page_memcg(page)->css != wb->memcg_css))
1564 		mem_cgroup_track_foreign_dirty_slowpath(page, wb);
1565 }
1566 
1567 void mem_cgroup_flush_foreign(struct bdi_writeback *wb);
1568 
1569 #else	/* CONFIG_CGROUP_WRITEBACK */
1570 
1571 static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1572 {
1573 	return NULL;
1574 }
1575 
1576 static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
1577 				       unsigned long *pfilepages,
1578 				       unsigned long *pheadroom,
1579 				       unsigned long *pdirty,
1580 				       unsigned long *pwriteback)
1581 {
1582 }
1583 
1584 static inline void mem_cgroup_track_foreign_dirty(struct page *page,
1585 						  struct bdi_writeback *wb)
1586 {
1587 }
1588 
1589 static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
1590 {
1591 }
1592 
1593 #endif	/* CONFIG_CGROUP_WRITEBACK */
1594 
1595 struct sock;
1596 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
1597 			     gfp_t gfp_mask);
1598 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1599 #ifdef CONFIG_MEMCG
1600 extern struct static_key_false memcg_sockets_enabled_key;
1601 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
1602 void mem_cgroup_sk_alloc(struct sock *sk);
1603 void mem_cgroup_sk_free(struct sock *sk);
1604 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1605 {
1606 	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure)
1607 		return true;
1608 	do {
1609 		if (time_before(jiffies, READ_ONCE(memcg->socket_pressure)))
1610 			return true;
1611 	} while ((memcg = parent_mem_cgroup(memcg)));
1612 	return false;
1613 }
1614 
1615 int alloc_shrinker_info(struct mem_cgroup *memcg);
1616 void free_shrinker_info(struct mem_cgroup *memcg);
1617 void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id);
1618 void reparent_shrinker_deferred(struct mem_cgroup *memcg);
1619 #else
1620 #define mem_cgroup_sockets_enabled 0
1621 static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1622 static inline void mem_cgroup_sk_free(struct sock *sk) { };
1623 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1624 {
1625 	return false;
1626 }
1627 
1628 static inline void set_shrinker_bit(struct mem_cgroup *memcg,
1629 				    int nid, int shrinker_id)
1630 {
1631 }
1632 #endif
1633 
1634 #ifdef CONFIG_MEMCG_KMEM
1635 bool mem_cgroup_kmem_disabled(void);
1636 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order);
1637 void __memcg_kmem_uncharge_page(struct page *page, int order);
1638 
1639 struct obj_cgroup *get_obj_cgroup_from_current(void);
1640 
1641 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size);
1642 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size);
1643 
1644 extern struct static_key_false memcg_kmem_enabled_key;
1645 
1646 extern int memcg_nr_cache_ids;
1647 void memcg_get_cache_ids(void);
1648 void memcg_put_cache_ids(void);
1649 
1650 /*
1651  * Helper macro to loop through all memcg-specific caches. Callers must still
1652  * check if the cache is valid (it is either valid or NULL).
1653  * the slab_mutex must be held when looping through those caches
1654  */
1655 #define for_each_memcg_cache_index(_idx)	\
1656 	for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
1657 
1658 static inline bool memcg_kmem_enabled(void)
1659 {
1660 	return static_branch_likely(&memcg_kmem_enabled_key);
1661 }
1662 
1663 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1664 					 int order)
1665 {
1666 	if (memcg_kmem_enabled())
1667 		return __memcg_kmem_charge_page(page, gfp, order);
1668 	return 0;
1669 }
1670 
1671 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1672 {
1673 	if (memcg_kmem_enabled())
1674 		__memcg_kmem_uncharge_page(page, order);
1675 }
1676 
1677 /*
1678  * A helper for accessing memcg's kmem_id, used for getting
1679  * corresponding LRU lists.
1680  */
1681 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1682 {
1683 	return memcg ? memcg->kmemcg_id : -1;
1684 }
1685 
1686 struct mem_cgroup *mem_cgroup_from_obj(void *p);
1687 
1688 #else
1689 static inline bool mem_cgroup_kmem_disabled(void)
1690 {
1691 	return true;
1692 }
1693 
1694 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1695 					 int order)
1696 {
1697 	return 0;
1698 }
1699 
1700 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1701 {
1702 }
1703 
1704 static inline int __memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1705 					   int order)
1706 {
1707 	return 0;
1708 }
1709 
1710 static inline void __memcg_kmem_uncharge_page(struct page *page, int order)
1711 {
1712 }
1713 
1714 #define for_each_memcg_cache_index(_idx)	\
1715 	for (; NULL; )
1716 
1717 static inline bool memcg_kmem_enabled(void)
1718 {
1719 	return false;
1720 }
1721 
1722 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1723 {
1724 	return -1;
1725 }
1726 
1727 static inline void memcg_get_cache_ids(void)
1728 {
1729 }
1730 
1731 static inline void memcg_put_cache_ids(void)
1732 {
1733 }
1734 
1735 static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)
1736 {
1737        return NULL;
1738 }
1739 
1740 #endif /* CONFIG_MEMCG_KMEM */
1741 
1742 #endif /* _LINUX_MEMCONTROL_H */
1743