1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* memcontrol.h - Memory Controller
4 * Copyright IBM Corporation, 2007
7 * Copyright 2007 OpenVZ SWsoft Inc
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>
21 #include <linux/vmstat.h>
22 #include <linux/writeback.h>
23 #include <linux/page-flags.h>
24 #include <linux/shrinker.h>
32 /* Cgroup-specific page state, on top of universal node page state */
33 enum memcg_stat_item {
34 MEMCG_SWAP = NR_VM_NODE_STAT_ITEMS,
44 enum memcg_memory_event {
54 MEMCG_NR_MEMORY_EVENTS,
57 struct mem_cgroup_reclaim_cookie {
59 unsigned int generation;
64 #define MEM_CGROUP_ID_SHIFT 16
66 struct mem_cgroup_id {
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.
77 enum mem_cgroup_events_target {
78 MEM_CGROUP_TARGET_THRESH,
79 MEM_CGROUP_TARGET_SOFTLIMIT,
83 struct memcg_vmstats_percpu;
86 struct mem_cgroup_reclaim_iter {
87 struct mem_cgroup *position;
88 /* scan generation, increased every round-trip */
89 unsigned int generation;
92 struct lruvec_stats_percpu {
93 /* Local (CPU and cgroup) state */
94 long state[NR_VM_NODE_STAT_ITEMS];
96 /* Delta calculation for lockless upward propagation */
97 long state_prev[NR_VM_NODE_STAT_ITEMS];
100 struct lruvec_stats {
101 /* Aggregated (CPU and subtree) state */
102 long state[NR_VM_NODE_STAT_ITEMS];
104 /* Non-hierarchical (CPU aggregated) state */
105 long state_local[NR_VM_NODE_STAT_ITEMS];
107 /* Pending child counts during tree propagation */
108 long state_pending[NR_VM_NODE_STAT_ITEMS];
112 * per-node information in memory controller.
114 struct mem_cgroup_per_node {
115 struct lruvec lruvec;
117 struct lruvec_stats_percpu __percpu *lruvec_stats_percpu;
118 struct lruvec_stats lruvec_stats;
120 unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
122 struct mem_cgroup_reclaim_iter iter;
124 struct shrinker_info __rcu *shrinker_info;
126 struct rb_node tree_node; /* RB tree node */
127 unsigned long usage_in_excess;/* Set to the value by which */
128 /* the soft limit is exceeded*/
130 struct mem_cgroup *memcg; /* Back pointer, we cannot */
131 /* use container_of */
134 struct mem_cgroup_threshold {
135 struct eventfd_ctx *eventfd;
136 unsigned long threshold;
140 struct mem_cgroup_threshold_ary {
141 /* An array index points to threshold just below or equal to usage. */
142 int current_threshold;
143 /* Size of entries[] */
145 /* Array of thresholds */
146 struct mem_cgroup_threshold entries[] __counted_by(size);
149 struct mem_cgroup_thresholds {
150 /* Primary thresholds array */
151 struct mem_cgroup_threshold_ary *primary;
153 * Spare threshold array.
154 * This is needed to make mem_cgroup_unregister_event() "never fail".
155 * It must be able to store at least primary->size - 1 entries.
157 struct mem_cgroup_threshold_ary *spare;
161 * Remember four most recent foreign writebacks with dirty pages in this
162 * cgroup. Inode sharing is expected to be uncommon and, even if we miss
163 * one in a given round, we're likely to catch it later if it keeps
164 * foreign-dirtying, so a fairly low count should be enough.
166 * See mem_cgroup_track_foreign_dirty_slowpath() for details.
168 #define MEMCG_CGWB_FRN_CNT 4
170 struct memcg_cgwb_frn {
171 u64 bdi_id; /* bdi->id of the foreign inode */
172 int memcg_id; /* memcg->css.id of foreign inode */
173 u64 at; /* jiffies_64 at the time of dirtying */
174 struct wb_completion done; /* tracks in-flight foreign writebacks */
178 * Bucket for arbitrarily byte-sized objects charged to a memory
179 * cgroup. The bucket can be reparented in one piece when the cgroup
180 * is destroyed, without having to round up the individual references
181 * of all live memory objects in the wild.
184 struct percpu_ref refcnt;
185 struct mem_cgroup *memcg;
186 atomic_t nr_charged_bytes;
188 struct list_head list; /* protected by objcg_lock */
194 * The memory controller data structure. The memory controller controls both
195 * page cache and RSS per cgroup. We would eventually like to provide
196 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
197 * to help the administrator determine what knobs to tune.
200 struct cgroup_subsys_state css;
202 /* Private memcg ID. Used to ID objects that outlive the cgroup */
203 struct mem_cgroup_id id;
205 /* Accounted resources */
206 struct page_counter memory; /* Both v1 & v2 */
209 struct page_counter swap; /* v2 only */
210 struct page_counter memsw; /* v1 only */
213 /* Legacy consumer-oriented counters */
214 struct page_counter kmem; /* v1 only */
215 struct page_counter tcpmem; /* v1 only */
217 /* Range enforcement for interrupt charges */
218 struct work_struct high_work;
220 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
221 unsigned long zswap_max;
224 * Prevent pages from this memcg from being written back from zswap to
225 * swap, and from being swapped out on zswap store failures.
227 bool zswap_writeback;
230 unsigned long soft_limit;
232 /* vmpressure notifications */
233 struct vmpressure vmpressure;
236 * Should the OOM killer kill all belonging tasks, had it kill one?
240 /* protected by memcg_oom_lock */
245 /* OOM-Killer disable */
246 int oom_kill_disable;
248 /* memory.events and memory.events.local */
249 struct cgroup_file events_file;
250 struct cgroup_file events_local_file;
252 /* handle for "memory.swap.events" */
253 struct cgroup_file swap_events_file;
255 /* protect arrays of thresholds */
256 struct mutex thresholds_lock;
258 /* thresholds for memory usage. RCU-protected */
259 struct mem_cgroup_thresholds thresholds;
261 /* thresholds for mem+swap usage. RCU-protected */
262 struct mem_cgroup_thresholds memsw_thresholds;
264 /* For oom notifier event fd */
265 struct list_head oom_notify;
268 * Should we move charges of a task when a task is moved into this
269 * mem_cgroup ? And what type of charges should we move ?
271 unsigned long move_charge_at_immigrate;
272 /* taken only while moving_account > 0 */
273 spinlock_t move_lock;
274 unsigned long move_lock_flags;
276 CACHELINE_PADDING(_pad1_);
279 struct memcg_vmstats *vmstats;
282 atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
283 atomic_long_t memory_events_local[MEMCG_NR_MEMORY_EVENTS];
286 * Hint of reclaim pressure for socket memroy management. Note
287 * that this indicator should NOT be used in legacy cgroup mode
288 * where socket memory is accounted/charged separately.
290 unsigned long socket_pressure;
292 /* Legacy tcp memory accounting */
296 #ifdef CONFIG_MEMCG_KMEM
299 * memcg->objcg is wiped out as a part of the objcg repaprenting
300 * process. memcg->orig_objcg preserves a pointer (and a reference)
301 * to the original objcg until the end of live of memcg.
303 struct obj_cgroup __rcu *objcg;
304 struct obj_cgroup *orig_objcg;
305 /* list of inherited objcgs, protected by objcg_lock */
306 struct list_head objcg_list;
309 CACHELINE_PADDING(_pad2_);
312 * set > 0 if pages under this cgroup are moving to other cgroup.
314 atomic_t moving_account;
315 struct task_struct *move_lock_task;
317 struct memcg_vmstats_percpu __percpu *vmstats_percpu;
319 #ifdef CONFIG_CGROUP_WRITEBACK
320 struct list_head cgwb_list;
321 struct wb_domain cgwb_domain;
322 struct memcg_cgwb_frn cgwb_frn[MEMCG_CGWB_FRN_CNT];
325 /* List of events which userspace want to receive */
326 struct list_head event_list;
327 spinlock_t event_list_lock;
329 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
330 struct deferred_split deferred_split_queue;
333 #ifdef CONFIG_LRU_GEN_WALKS_MMU
334 /* per-memcg mm_struct list */
335 struct lru_gen_mm_list mm_list;
338 struct mem_cgroup_per_node *nodeinfo[];
342 * size of first charge trial.
343 * TODO: maybe necessary to use big numbers in big irons or dynamic based of the
346 #define MEMCG_CHARGE_BATCH 64U
348 extern struct mem_cgroup *root_mem_cgroup;
350 enum page_memcg_data_flags {
351 /* page->memcg_data is a pointer to an objcgs vector */
352 MEMCG_DATA_OBJCGS = (1UL << 0),
353 /* page has been accounted as a non-slab kernel page */
354 MEMCG_DATA_KMEM = (1UL << 1),
355 /* the next bit after the last actual flag */
356 __NR_MEMCG_DATA_FLAGS = (1UL << 2),
359 #define MEMCG_DATA_FLAGS_MASK (__NR_MEMCG_DATA_FLAGS - 1)
361 static inline bool folio_memcg_kmem(struct folio *folio);
364 * After the initialization objcg->memcg is always pointing at
365 * a valid memcg, but can be atomically swapped to the parent memcg.
367 * The caller must ensure that the returned memcg won't be released:
368 * e.g. acquire the rcu_read_lock or css_set_lock.
370 static inline struct mem_cgroup *obj_cgroup_memcg(struct obj_cgroup *objcg)
372 return READ_ONCE(objcg->memcg);
376 * __folio_memcg - Get the memory cgroup associated with a non-kmem folio
377 * @folio: Pointer to the folio.
379 * Returns a pointer to the memory cgroup associated with the folio,
380 * or NULL. This function assumes that the folio is known to have a
381 * proper memory cgroup pointer. It's not safe to call this function
382 * against some type of folios, e.g. slab folios or ex-slab folios or
385 static inline struct mem_cgroup *__folio_memcg(struct folio *folio)
387 unsigned long memcg_data = folio->memcg_data;
389 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio);
390 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_OBJCGS, folio);
391 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_KMEM, folio);
393 return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
397 * __folio_objcg - get the object cgroup associated with a kmem folio.
398 * @folio: Pointer to the folio.
400 * Returns a pointer to the object cgroup associated with the folio,
401 * or NULL. This function assumes that the folio is known to have a
402 * proper object cgroup pointer. It's not safe to call this function
403 * against some type of folios, e.g. slab folios or ex-slab folios or
406 static inline struct obj_cgroup *__folio_objcg(struct folio *folio)
408 unsigned long memcg_data = folio->memcg_data;
410 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio);
411 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_OBJCGS, folio);
412 VM_BUG_ON_FOLIO(!(memcg_data & MEMCG_DATA_KMEM), folio);
414 return (struct obj_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
418 * folio_memcg - Get the memory cgroup associated with a folio.
419 * @folio: Pointer to the folio.
421 * Returns a pointer to the memory cgroup associated with the folio,
422 * or NULL. This function assumes that the folio is known to have a
423 * proper memory cgroup pointer. It's not safe to call this function
424 * against some type of folios, e.g. slab folios or ex-slab folios.
426 * For a non-kmem folio any of the following ensures folio and memcg binding
431 * - folio_memcg_lock()
432 * - exclusive reference
433 * - mem_cgroup_trylock_pages()
435 * For a kmem folio a caller should hold an rcu read lock to protect memcg
436 * associated with a kmem folio from being released.
438 static inline struct mem_cgroup *folio_memcg(struct folio *folio)
440 if (folio_memcg_kmem(folio))
441 return obj_cgroup_memcg(__folio_objcg(folio));
442 return __folio_memcg(folio);
445 static inline struct mem_cgroup *page_memcg(struct page *page)
447 return folio_memcg(page_folio(page));
451 * folio_memcg_rcu - Locklessly get the memory cgroup associated with a folio.
452 * @folio: Pointer to the folio.
454 * This function assumes that the folio is known to have a
455 * proper memory cgroup pointer. It's not safe to call this function
456 * against some type of folios, e.g. slab folios or ex-slab folios.
458 * Return: A pointer to the memory cgroup associated with the folio,
461 static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)
463 unsigned long memcg_data = READ_ONCE(folio->memcg_data);
465 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio);
466 WARN_ON_ONCE(!rcu_read_lock_held());
468 if (memcg_data & MEMCG_DATA_KMEM) {
469 struct obj_cgroup *objcg;
471 objcg = (void *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
472 return obj_cgroup_memcg(objcg);
475 return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
479 * folio_memcg_check - Get the memory cgroup associated with a folio.
480 * @folio: Pointer to the folio.
482 * Returns a pointer to the memory cgroup associated with the folio,
483 * or NULL. This function unlike folio_memcg() can take any folio
484 * as an argument. It has to be used in cases when it's not known if a folio
485 * has an associated memory cgroup pointer or an object cgroups vector or
488 * For a non-kmem folio any of the following ensures folio and memcg binding
493 * - lock_folio_memcg()
494 * - exclusive reference
495 * - mem_cgroup_trylock_pages()
497 * For a kmem folio a caller should hold an rcu read lock to protect memcg
498 * associated with a kmem folio from being released.
500 static inline struct mem_cgroup *folio_memcg_check(struct folio *folio)
503 * Because folio->memcg_data might be changed asynchronously
504 * for slabs, READ_ONCE() should be used here.
506 unsigned long memcg_data = READ_ONCE(folio->memcg_data);
508 if (memcg_data & MEMCG_DATA_OBJCGS)
511 if (memcg_data & MEMCG_DATA_KMEM) {
512 struct obj_cgroup *objcg;
514 objcg = (void *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
515 return obj_cgroup_memcg(objcg);
518 return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
521 static inline struct mem_cgroup *page_memcg_check(struct page *page)
525 return folio_memcg_check((struct folio *)page);
528 static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *objcg)
530 struct mem_cgroup *memcg;
534 memcg = obj_cgroup_memcg(objcg);
535 if (unlikely(!css_tryget(&memcg->css)))
542 #ifdef CONFIG_MEMCG_KMEM
544 * folio_memcg_kmem - Check if the folio has the memcg_kmem flag set.
545 * @folio: Pointer to the folio.
547 * Checks if the folio has MemcgKmem flag set. The caller must ensure
548 * that the folio has an associated memory cgroup. It's not safe to call
549 * this function against some types of folios, e.g. slab folios.
551 static inline bool folio_memcg_kmem(struct folio *folio)
553 VM_BUG_ON_PGFLAGS(PageTail(&folio->page), &folio->page);
554 VM_BUG_ON_FOLIO(folio->memcg_data & MEMCG_DATA_OBJCGS, folio);
555 return folio->memcg_data & MEMCG_DATA_KMEM;
560 static inline bool folio_memcg_kmem(struct folio *folio)
567 static inline bool PageMemcgKmem(struct page *page)
569 return folio_memcg_kmem(page_folio(page));
572 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
574 return (memcg == root_mem_cgroup);
577 static inline bool mem_cgroup_disabled(void)
579 return !cgroup_subsys_enabled(memory_cgrp_subsys);
582 static inline void mem_cgroup_protection(struct mem_cgroup *root,
583 struct mem_cgroup *memcg,
589 if (mem_cgroup_disabled())
593 * There is no reclaim protection applied to a targeted reclaim.
594 * We are special casing this specific case here because
595 * mem_cgroup_calculate_protection is not robust enough to keep
596 * the protection invariant for calculated effective values for
597 * parallel reclaimers with different reclaim target. This is
598 * especially a problem for tail memcgs (as they have pages on LRU)
599 * which would want to have effective values 0 for targeted reclaim
600 * but a different value for external reclaim.
603 * Let's have global and A's reclaim in parallel:
605 * A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G)
607 * | C (low = 1G, usage = 2.5G)
608 * B (low = 1G, usage = 0.5G)
610 * For the global reclaim
612 * B.elow = min(B.usage, B.low) because children_low_usage <= A.elow
613 * C.elow = min(C.usage, C.low)
615 * With the effective values resetting we have A reclaim
620 * If the global reclaim races with A's reclaim then
621 * B.elow = C.elow = 0 because children_low_usage > A.elow)
622 * is possible and reclaiming B would be violating the protection.
628 *min = READ_ONCE(memcg->memory.emin);
629 *low = READ_ONCE(memcg->memory.elow);
632 void mem_cgroup_calculate_protection(struct mem_cgroup *root,
633 struct mem_cgroup *memcg);
635 static inline bool mem_cgroup_unprotected(struct mem_cgroup *target,
636 struct mem_cgroup *memcg)
639 * The root memcg doesn't account charges, and doesn't support
640 * protection. The target memcg's protection is ignored, see
641 * mem_cgroup_calculate_protection() and mem_cgroup_protection()
643 return mem_cgroup_disabled() || mem_cgroup_is_root(memcg) ||
647 static inline bool mem_cgroup_below_low(struct mem_cgroup *target,
648 struct mem_cgroup *memcg)
650 if (mem_cgroup_unprotected(target, memcg))
653 return READ_ONCE(memcg->memory.elow) >=
654 page_counter_read(&memcg->memory);
657 static inline bool mem_cgroup_below_min(struct mem_cgroup *target,
658 struct mem_cgroup *memcg)
660 if (mem_cgroup_unprotected(target, memcg))
663 return READ_ONCE(memcg->memory.emin) >=
664 page_counter_read(&memcg->memory);
667 void mem_cgroup_commit_charge(struct folio *folio, struct mem_cgroup *memcg);
669 int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp);
672 * mem_cgroup_charge - Charge a newly allocated folio to a cgroup.
673 * @folio: Folio to charge.
674 * @mm: mm context of the allocating task.
675 * @gfp: Reclaim mode.
677 * Try to charge @folio to the memcg that @mm belongs to, reclaiming
678 * pages according to @gfp if necessary. If @mm is NULL, try to
679 * charge to the active memcg.
681 * Do not use this for folios allocated for swapin.
683 * Return: 0 on success. Otherwise, an error code is returned.
685 static inline int mem_cgroup_charge(struct folio *folio, struct mm_struct *mm,
688 if (mem_cgroup_disabled())
690 return __mem_cgroup_charge(folio, mm, gfp);
693 int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, gfp_t gfp,
696 int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm,
697 gfp_t gfp, swp_entry_t entry);
698 void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry);
700 void __mem_cgroup_uncharge(struct folio *folio);
703 * mem_cgroup_uncharge - Uncharge a folio.
704 * @folio: Folio to uncharge.
706 * Uncharge a folio previously charged with mem_cgroup_charge().
708 static inline void mem_cgroup_uncharge(struct folio *folio)
710 if (mem_cgroup_disabled())
712 __mem_cgroup_uncharge(folio);
715 void __mem_cgroup_uncharge_list(struct list_head *page_list);
716 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
718 if (mem_cgroup_disabled())
720 __mem_cgroup_uncharge_list(page_list);
723 void mem_cgroup_cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages);
725 void mem_cgroup_replace_folio(struct folio *old, struct folio *new);
727 void mem_cgroup_migrate(struct folio *old, struct folio *new);
730 * mem_cgroup_lruvec - get the lru list vector for a memcg & node
731 * @memcg: memcg of the wanted lruvec
732 * @pgdat: pglist_data
734 * Returns the lru list vector holding pages for a given @memcg &
735 * @pgdat combination. This can be the node lruvec, if the memory
736 * controller is disabled.
738 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
739 struct pglist_data *pgdat)
741 struct mem_cgroup_per_node *mz;
742 struct lruvec *lruvec;
744 if (mem_cgroup_disabled()) {
745 lruvec = &pgdat->__lruvec;
750 memcg = root_mem_cgroup;
752 mz = memcg->nodeinfo[pgdat->node_id];
753 lruvec = &mz->lruvec;
756 * Since a node can be onlined after the mem_cgroup was created,
757 * we have to be prepared to initialize lruvec->pgdat here;
758 * and if offlined then reonlined, we need to reinitialize it.
760 if (unlikely(lruvec->pgdat != pgdat))
761 lruvec->pgdat = pgdat;
766 * folio_lruvec - return lruvec for isolating/putting an LRU folio
767 * @folio: Pointer to the folio.
769 * This function relies on folio->mem_cgroup being stable.
771 static inline struct lruvec *folio_lruvec(struct folio *folio)
773 struct mem_cgroup *memcg = folio_memcg(folio);
775 VM_WARN_ON_ONCE_FOLIO(!memcg && !mem_cgroup_disabled(), folio);
776 return mem_cgroup_lruvec(memcg, folio_pgdat(folio));
779 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
781 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
783 struct mem_cgroup *get_mem_cgroup_from_current(void);
785 struct lruvec *folio_lruvec_lock(struct folio *folio);
786 struct lruvec *folio_lruvec_lock_irq(struct folio *folio);
787 struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
788 unsigned long *flags);
790 #ifdef CONFIG_DEBUG_VM
791 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio);
794 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
800 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
801 return css ? container_of(css, struct mem_cgroup, css) : NULL;
804 static inline bool obj_cgroup_tryget(struct obj_cgroup *objcg)
806 return percpu_ref_tryget(&objcg->refcnt);
809 static inline void obj_cgroup_get(struct obj_cgroup *objcg)
811 percpu_ref_get(&objcg->refcnt);
814 static inline void obj_cgroup_get_many(struct obj_cgroup *objcg,
817 percpu_ref_get_many(&objcg->refcnt, nr);
820 static inline void obj_cgroup_put(struct obj_cgroup *objcg)
822 percpu_ref_put(&objcg->refcnt);
825 static inline bool mem_cgroup_tryget(struct mem_cgroup *memcg)
827 return !memcg || css_tryget(&memcg->css);
830 static inline bool mem_cgroup_tryget_online(struct mem_cgroup *memcg)
832 return !memcg || css_tryget_online(&memcg->css);
835 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
838 css_put(&memcg->css);
841 #define mem_cgroup_from_counter(counter, member) \
842 container_of(counter, struct mem_cgroup, member)
844 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
846 struct mem_cgroup_reclaim_cookie *);
847 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
848 void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
849 int (*)(struct task_struct *, void *), void *arg);
851 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
853 if (mem_cgroup_disabled())
858 struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
860 #ifdef CONFIG_SHRINKER_DEBUG
861 static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg)
863 return memcg ? cgroup_ino(memcg->css.cgroup) : 0;
866 struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino);
869 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
871 return mem_cgroup_from_css(seq_css(m));
874 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
876 struct mem_cgroup_per_node *mz;
878 if (mem_cgroup_disabled())
881 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
886 * parent_mem_cgroup - find the accounting parent of a memcg
887 * @memcg: memcg whose parent to find
889 * Returns the parent memcg, or NULL if this is the root.
891 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
893 return mem_cgroup_from_css(memcg->css.parent);
896 static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
897 struct mem_cgroup *root)
901 return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
904 static inline bool mm_match_cgroup(struct mm_struct *mm,
905 struct mem_cgroup *memcg)
907 struct mem_cgroup *task_memcg;
911 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
913 match = mem_cgroup_is_descendant(task_memcg, memcg);
918 struct cgroup_subsys_state *mem_cgroup_css_from_folio(struct folio *folio);
919 ino_t page_cgroup_ino(struct page *page);
921 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
923 if (mem_cgroup_disabled())
925 return !!(memcg->css.flags & CSS_ONLINE);
928 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
929 int zid, int nr_pages);
932 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
933 enum lru_list lru, int zone_idx)
935 struct mem_cgroup_per_node *mz;
937 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
938 return READ_ONCE(mz->lru_zone_size[zone_idx][lru]);
941 void mem_cgroup_handle_over_high(gfp_t gfp_mask);
943 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
945 unsigned long mem_cgroup_size(struct mem_cgroup *memcg);
947 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
948 struct task_struct *p);
950 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg);
952 static inline void mem_cgroup_enter_user_fault(void)
954 WARN_ON(current->in_user_fault);
955 current->in_user_fault = 1;
958 static inline void mem_cgroup_exit_user_fault(void)
960 WARN_ON(!current->in_user_fault);
961 current->in_user_fault = 0;
964 static inline bool task_in_memcg_oom(struct task_struct *p)
966 return p->memcg_in_oom;
969 bool mem_cgroup_oom_synchronize(bool wait);
970 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
971 struct mem_cgroup *oom_domain);
972 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg);
974 void folio_memcg_lock(struct folio *folio);
975 void folio_memcg_unlock(struct folio *folio);
977 void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val);
979 /* try to stablize folio_memcg() for all the pages in a memcg */
980 static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
984 if (mem_cgroup_disabled() || !atomic_read(&memcg->moving_account))
991 static inline void mem_cgroup_unlock_pages(void)
996 /* idx can be of type enum memcg_stat_item or node_stat_item */
997 static inline void mod_memcg_state(struct mem_cgroup *memcg,
1000 unsigned long flags;
1002 local_irq_save(flags);
1003 __mod_memcg_state(memcg, idx, val);
1004 local_irq_restore(flags);
1007 static inline void mod_memcg_page_state(struct page *page,
1010 struct mem_cgroup *memcg;
1012 if (mem_cgroup_disabled())
1016 memcg = page_memcg(page);
1018 mod_memcg_state(memcg, idx, val);
1022 unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx);
1024 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1025 enum node_stat_item idx)
1027 struct mem_cgroup_per_node *pn;
1030 if (mem_cgroup_disabled())
1031 return node_page_state(lruvec_pgdat(lruvec), idx);
1033 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
1034 x = READ_ONCE(pn->lruvec_stats.state[idx]);
1042 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1043 enum node_stat_item idx)
1045 struct mem_cgroup_per_node *pn;
1048 if (mem_cgroup_disabled())
1049 return node_page_state(lruvec_pgdat(lruvec), idx);
1051 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
1052 x = READ_ONCE(pn->lruvec_stats.state_local[idx]);
1060 void mem_cgroup_flush_stats(struct mem_cgroup *memcg);
1061 void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg);
1063 void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
1065 void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val);
1067 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1070 unsigned long flags;
1072 local_irq_save(flags);
1073 __mod_lruvec_kmem_state(p, idx, val);
1074 local_irq_restore(flags);
1077 static inline void mod_memcg_lruvec_state(struct lruvec *lruvec,
1078 enum node_stat_item idx, int val)
1080 unsigned long flags;
1082 local_irq_save(flags);
1083 __mod_memcg_lruvec_state(lruvec, idx, val);
1084 local_irq_restore(flags);
1087 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
1088 unsigned long count);
1090 static inline void count_memcg_events(struct mem_cgroup *memcg,
1091 enum vm_event_item idx,
1092 unsigned long count)
1094 unsigned long flags;
1096 local_irq_save(flags);
1097 __count_memcg_events(memcg, idx, count);
1098 local_irq_restore(flags);
1101 static inline void count_memcg_folio_events(struct folio *folio,
1102 enum vm_event_item idx, unsigned long nr)
1104 struct mem_cgroup *memcg = folio_memcg(folio);
1107 count_memcg_events(memcg, idx, nr);
1110 static inline void count_memcg_event_mm(struct mm_struct *mm,
1111 enum vm_event_item idx)
1113 struct mem_cgroup *memcg;
1115 if (mem_cgroup_disabled())
1119 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1121 count_memcg_events(memcg, idx, 1);
1125 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1126 enum memcg_memory_event event)
1128 bool swap_event = event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX ||
1129 event == MEMCG_SWAP_FAIL;
1131 atomic_long_inc(&memcg->memory_events_local[event]);
1133 cgroup_file_notify(&memcg->events_local_file);
1136 atomic_long_inc(&memcg->memory_events[event]);
1138 cgroup_file_notify(&memcg->swap_events_file);
1140 cgroup_file_notify(&memcg->events_file);
1142 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1144 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
1146 } while ((memcg = parent_mem_cgroup(memcg)) &&
1147 !mem_cgroup_is_root(memcg));
1150 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1151 enum memcg_memory_event event)
1153 struct mem_cgroup *memcg;
1155 if (mem_cgroup_disabled())
1159 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1161 memcg_memory_event(memcg, event);
1165 void split_page_memcg(struct page *head, unsigned int nr);
1167 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1169 unsigned long *total_scanned);
1171 #else /* CONFIG_MEMCG */
1173 #define MEM_CGROUP_ID_SHIFT 0
1175 static inline struct mem_cgroup *folio_memcg(struct folio *folio)
1180 static inline struct mem_cgroup *page_memcg(struct page *page)
1185 static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)
1187 WARN_ON_ONCE(!rcu_read_lock_held());
1191 static inline struct mem_cgroup *folio_memcg_check(struct folio *folio)
1196 static inline struct mem_cgroup *page_memcg_check(struct page *page)
1201 static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *objcg)
1206 static inline bool folio_memcg_kmem(struct folio *folio)
1211 static inline bool PageMemcgKmem(struct page *page)
1216 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
1221 static inline bool mem_cgroup_disabled(void)
1226 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1227 enum memcg_memory_event event)
1231 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1232 enum memcg_memory_event event)
1236 static inline void mem_cgroup_protection(struct mem_cgroup *root,
1237 struct mem_cgroup *memcg,
1244 static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root,
1245 struct mem_cgroup *memcg)
1249 static inline bool mem_cgroup_unprotected(struct mem_cgroup *target,
1250 struct mem_cgroup *memcg)
1254 static inline bool mem_cgroup_below_low(struct mem_cgroup *target,
1255 struct mem_cgroup *memcg)
1260 static inline bool mem_cgroup_below_min(struct mem_cgroup *target,
1261 struct mem_cgroup *memcg)
1266 static inline void mem_cgroup_commit_charge(struct folio *folio,
1267 struct mem_cgroup *memcg)
1271 static inline int mem_cgroup_charge(struct folio *folio,
1272 struct mm_struct *mm, gfp_t gfp)
1277 static inline int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg,
1278 gfp_t gfp, long nr_pages)
1283 static inline int mem_cgroup_swapin_charge_folio(struct folio *folio,
1284 struct mm_struct *mm, gfp_t gfp, swp_entry_t entry)
1289 static inline void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry)
1293 static inline void mem_cgroup_uncharge(struct folio *folio)
1297 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
1301 static inline void mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
1302 unsigned int nr_pages)
1306 static inline void mem_cgroup_replace_folio(struct folio *old,
1311 static inline void mem_cgroup_migrate(struct folio *old, struct folio *new)
1315 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
1316 struct pglist_data *pgdat)
1318 return &pgdat->__lruvec;
1321 static inline struct lruvec *folio_lruvec(struct folio *folio)
1323 struct pglist_data *pgdat = folio_pgdat(folio);
1324 return &pgdat->__lruvec;
1328 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
1332 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
1337 static inline bool mm_match_cgroup(struct mm_struct *mm,
1338 struct mem_cgroup *memcg)
1343 static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
1348 static inline struct mem_cgroup *get_mem_cgroup_from_current(void)
1354 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css)
1359 static inline void obj_cgroup_put(struct obj_cgroup *objcg)
1363 static inline bool mem_cgroup_tryget(struct mem_cgroup *memcg)
1368 static inline bool mem_cgroup_tryget_online(struct mem_cgroup *memcg)
1373 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
1377 static inline struct lruvec *folio_lruvec_lock(struct folio *folio)
1379 struct pglist_data *pgdat = folio_pgdat(folio);
1381 spin_lock(&pgdat->__lruvec.lru_lock);
1382 return &pgdat->__lruvec;
1385 static inline struct lruvec *folio_lruvec_lock_irq(struct folio *folio)
1387 struct pglist_data *pgdat = folio_pgdat(folio);
1389 spin_lock_irq(&pgdat->__lruvec.lru_lock);
1390 return &pgdat->__lruvec;
1393 static inline struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
1394 unsigned long *flagsp)
1396 struct pglist_data *pgdat = folio_pgdat(folio);
1398 spin_lock_irqsave(&pgdat->__lruvec.lru_lock, *flagsp);
1399 return &pgdat->__lruvec;
1402 static inline struct mem_cgroup *
1403 mem_cgroup_iter(struct mem_cgroup *root,
1404 struct mem_cgroup *prev,
1405 struct mem_cgroup_reclaim_cookie *reclaim)
1410 static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
1411 struct mem_cgroup *prev)
1415 static inline void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1416 int (*fn)(struct task_struct *, void *), void *arg)
1420 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
1425 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
1428 /* XXX: This should always return root_mem_cgroup */
1432 #ifdef CONFIG_SHRINKER_DEBUG
1433 static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg)
1438 static inline struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino)
1444 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
1449 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
1454 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
1460 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
1461 enum lru_list lru, int zone_idx)
1466 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
1471 static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1477 mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1482 mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
1486 static inline void folio_memcg_lock(struct folio *folio)
1490 static inline void folio_memcg_unlock(struct folio *folio)
1494 static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
1496 /* to match folio_memcg_rcu() */
1501 static inline void mem_cgroup_unlock_pages(void)
1506 static inline void mem_cgroup_handle_over_high(gfp_t gfp_mask)
1510 static inline void mem_cgroup_enter_user_fault(void)
1514 static inline void mem_cgroup_exit_user_fault(void)
1518 static inline bool task_in_memcg_oom(struct task_struct *p)
1523 static inline bool mem_cgroup_oom_synchronize(bool wait)
1528 static inline struct mem_cgroup *mem_cgroup_get_oom_group(
1529 struct task_struct *victim, struct mem_cgroup *oom_domain)
1534 static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
1538 static inline void __mod_memcg_state(struct mem_cgroup *memcg,
1544 static inline void mod_memcg_state(struct mem_cgroup *memcg,
1550 static inline void mod_memcg_page_state(struct page *page,
1555 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
1560 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1561 enum node_stat_item idx)
1563 return node_page_state(lruvec_pgdat(lruvec), idx);
1566 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1567 enum node_stat_item idx)
1569 return node_page_state(lruvec_pgdat(lruvec), idx);
1572 static inline void mem_cgroup_flush_stats(struct mem_cgroup *memcg)
1576 static inline void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg)
1580 static inline void __mod_memcg_lruvec_state(struct lruvec *lruvec,
1581 enum node_stat_item idx, int val)
1585 static inline void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1588 struct page *page = virt_to_head_page(p);
1590 __mod_node_page_state(page_pgdat(page), idx, val);
1593 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1596 struct page *page = virt_to_head_page(p);
1598 mod_node_page_state(page_pgdat(page), idx, val);
1601 static inline void count_memcg_events(struct mem_cgroup *memcg,
1602 enum vm_event_item idx,
1603 unsigned long count)
1607 static inline void __count_memcg_events(struct mem_cgroup *memcg,
1608 enum vm_event_item idx,
1609 unsigned long count)
1613 static inline void count_memcg_folio_events(struct folio *folio,
1614 enum vm_event_item idx, unsigned long nr)
1619 void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
1623 static inline void split_page_memcg(struct page *head, unsigned int nr)
1628 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1630 unsigned long *total_scanned)
1634 #endif /* CONFIG_MEMCG */
1636 static inline void __inc_lruvec_kmem_state(void *p, enum node_stat_item idx)
1638 __mod_lruvec_kmem_state(p, idx, 1);
1641 static inline void __dec_lruvec_kmem_state(void *p, enum node_stat_item idx)
1643 __mod_lruvec_kmem_state(p, idx, -1);
1646 static inline struct lruvec *parent_lruvec(struct lruvec *lruvec)
1648 struct mem_cgroup *memcg;
1650 memcg = lruvec_memcg(lruvec);
1653 memcg = parent_mem_cgroup(memcg);
1656 return mem_cgroup_lruvec(memcg, lruvec_pgdat(lruvec));
1659 static inline void unlock_page_lruvec(struct lruvec *lruvec)
1661 spin_unlock(&lruvec->lru_lock);
1664 static inline void unlock_page_lruvec_irq(struct lruvec *lruvec)
1666 spin_unlock_irq(&lruvec->lru_lock);
1669 static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec,
1670 unsigned long flags)
1672 spin_unlock_irqrestore(&lruvec->lru_lock, flags);
1675 /* Test requires a stable page->memcg binding, see page_memcg() */
1676 static inline bool folio_matches_lruvec(struct folio *folio,
1677 struct lruvec *lruvec)
1679 return lruvec_pgdat(lruvec) == folio_pgdat(folio) &&
1680 lruvec_memcg(lruvec) == folio_memcg(folio);
1683 /* Don't lock again iff page's lruvec locked */
1684 static inline struct lruvec *folio_lruvec_relock_irq(struct folio *folio,
1685 struct lruvec *locked_lruvec)
1687 if (locked_lruvec) {
1688 if (folio_matches_lruvec(folio, locked_lruvec))
1689 return locked_lruvec;
1691 unlock_page_lruvec_irq(locked_lruvec);
1694 return folio_lruvec_lock_irq(folio);
1697 /* Don't lock again iff page's lruvec locked */
1698 static inline struct lruvec *folio_lruvec_relock_irqsave(struct folio *folio,
1699 struct lruvec *locked_lruvec, unsigned long *flags)
1701 if (locked_lruvec) {
1702 if (folio_matches_lruvec(folio, locked_lruvec))
1703 return locked_lruvec;
1705 unlock_page_lruvec_irqrestore(locked_lruvec, *flags);
1708 return folio_lruvec_lock_irqsave(folio, flags);
1711 #ifdef CONFIG_CGROUP_WRITEBACK
1713 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
1714 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1715 unsigned long *pheadroom, unsigned long *pdirty,
1716 unsigned long *pwriteback);
1718 void mem_cgroup_track_foreign_dirty_slowpath(struct folio *folio,
1719 struct bdi_writeback *wb);
1721 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio,
1722 struct bdi_writeback *wb)
1724 struct mem_cgroup *memcg;
1726 if (mem_cgroup_disabled())
1729 memcg = folio_memcg(folio);
1730 if (unlikely(memcg && &memcg->css != wb->memcg_css))
1731 mem_cgroup_track_foreign_dirty_slowpath(folio, wb);
1734 void mem_cgroup_flush_foreign(struct bdi_writeback *wb);
1736 #else /* CONFIG_CGROUP_WRITEBACK */
1738 static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1743 static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
1744 unsigned long *pfilepages,
1745 unsigned long *pheadroom,
1746 unsigned long *pdirty,
1747 unsigned long *pwriteback)
1751 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio,
1752 struct bdi_writeback *wb)
1756 static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
1760 #endif /* CONFIG_CGROUP_WRITEBACK */
1763 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
1765 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1767 extern struct static_key_false memcg_sockets_enabled_key;
1768 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
1769 void mem_cgroup_sk_alloc(struct sock *sk);
1770 void mem_cgroup_sk_free(struct sock *sk);
1771 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1773 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1774 return !!memcg->tcpmem_pressure;
1776 if (time_before(jiffies, READ_ONCE(memcg->socket_pressure)))
1778 } while ((memcg = parent_mem_cgroup(memcg)));
1782 int alloc_shrinker_info(struct mem_cgroup *memcg);
1783 void free_shrinker_info(struct mem_cgroup *memcg);
1784 void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id);
1785 void reparent_shrinker_deferred(struct mem_cgroup *memcg);
1787 #define mem_cgroup_sockets_enabled 0
1788 static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1789 static inline void mem_cgroup_sk_free(struct sock *sk) { };
1790 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1795 static inline void set_shrinker_bit(struct mem_cgroup *memcg,
1796 int nid, int shrinker_id)
1801 #ifdef CONFIG_MEMCG_KMEM
1802 bool mem_cgroup_kmem_disabled(void);
1803 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order);
1804 void __memcg_kmem_uncharge_page(struct page *page, int order);
1807 * The returned objcg pointer is safe to use without additional
1808 * protection within a scope. The scope is defined either by
1809 * the current task (similar to the "current" global variable)
1810 * or by set_active_memcg() pair.
1811 * Please, use obj_cgroup_get() to get a reference if the pointer
1812 * needs to be used outside of the local scope.
1814 struct obj_cgroup *current_obj_cgroup(void);
1815 struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio);
1817 static inline struct obj_cgroup *get_obj_cgroup_from_current(void)
1819 struct obj_cgroup *objcg = current_obj_cgroup();
1822 obj_cgroup_get(objcg);
1827 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size);
1828 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size);
1830 extern struct static_key_false memcg_bpf_enabled_key;
1831 static inline bool memcg_bpf_enabled(void)
1833 return static_branch_likely(&memcg_bpf_enabled_key);
1836 extern struct static_key_false memcg_kmem_online_key;
1838 static inline bool memcg_kmem_online(void)
1840 return static_branch_likely(&memcg_kmem_online_key);
1843 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1846 if (memcg_kmem_online())
1847 return __memcg_kmem_charge_page(page, gfp, order);
1851 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1853 if (memcg_kmem_online())
1854 __memcg_kmem_uncharge_page(page, order);
1858 * A helper for accessing memcg's kmem_id, used for getting
1859 * corresponding LRU lists.
1861 static inline int memcg_kmem_id(struct mem_cgroup *memcg)
1863 return memcg ? memcg->kmemcg_id : -1;
1866 struct mem_cgroup *mem_cgroup_from_obj(void *p);
1867 struct mem_cgroup *mem_cgroup_from_slab_obj(void *p);
1869 static inline void count_objcg_event(struct obj_cgroup *objcg,
1870 enum vm_event_item idx)
1872 struct mem_cgroup *memcg;
1874 if (!memcg_kmem_online())
1878 memcg = obj_cgroup_memcg(objcg);
1879 count_memcg_events(memcg, idx, 1);
1884 static inline bool mem_cgroup_kmem_disabled(void)
1889 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1895 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1899 static inline int __memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1905 static inline void __memcg_kmem_uncharge_page(struct page *page, int order)
1909 static inline struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio)
1914 static inline bool memcg_bpf_enabled(void)
1919 static inline bool memcg_kmem_online(void)
1924 static inline int memcg_kmem_id(struct mem_cgroup *memcg)
1929 static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)
1934 static inline struct mem_cgroup *mem_cgroup_from_slab_obj(void *p)
1939 static inline void count_objcg_event(struct obj_cgroup *objcg,
1940 enum vm_event_item idx)
1944 #endif /* CONFIG_MEMCG_KMEM */
1946 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
1947 bool obj_cgroup_may_zswap(struct obj_cgroup *objcg);
1948 void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size);
1949 void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size);
1950 bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg);
1952 static inline bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
1956 static inline void obj_cgroup_charge_zswap(struct obj_cgroup *objcg,
1960 static inline void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg,
1964 static inline bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg)
1966 /* if zswap is disabled, do not block pages going to the swapping device */
1971 #endif /* _LINUX_MEMCONTROL_H */