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