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

source code of linux/include/linux/memcontrol.h