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