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/kernel.h>
18 #include <linux/page_counter.h>
19 #include <linux/vmpressure.h>
20 #include <linux/eventfd.h>
22 #include <linux/vmstat.h>
23 #include <linux/writeback.h>
24 #include <linux/page-flags.h>
25 #include <linux/shrinker.h>
33 /* Cgroup-specific page state, on top of universal node page state */
34 enum memcg_stat_item {
35 MEMCG_SWAP = NR_VM_NODE_STAT_ITEMS,
45 enum memcg_memory_event {
55 MEMCG_NR_MEMORY_EVENTS,
58 struct mem_cgroup_reclaim_cookie {
60 unsigned int generation;
65 #define MEM_CGROUP_ID_SHIFT 16
67 struct mem_cgroup_id {
73 * Per memcg event counter is incremented at every pagein/pageout. With THP,
74 * it will be incremented by the number of pages. This counter is used
75 * to trigger some periodic events. This is straightforward and better
76 * than using jiffies etc. to handle periodic memcg event.
78 enum mem_cgroup_events_target {
79 MEM_CGROUP_TARGET_THRESH,
80 MEM_CGROUP_TARGET_SOFTLIMIT,
84 struct memcg_vmstats_percpu;
86 struct lruvec_stats_percpu;
89 struct mem_cgroup_reclaim_iter {
90 struct mem_cgroup *position;
91 /* scan generation, increased every round-trip */
92 unsigned int generation;
96 * per-node information in memory controller.
98 struct mem_cgroup_per_node {
101 struct lruvec_stats_percpu __percpu *lruvec_stats_percpu;
102 struct lruvec_stats *lruvec_stats;
104 unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
106 struct mem_cgroup_reclaim_iter iter;
108 struct shrinker_info __rcu *shrinker_info;
110 struct rb_node tree_node; /* RB tree node */
111 unsigned long usage_in_excess;/* Set to the value by which */
112 /* the soft limit is exceeded*/
114 struct mem_cgroup *memcg; /* Back pointer, we cannot */
115 /* use container_of */
118 struct mem_cgroup_threshold {
119 struct eventfd_ctx *eventfd;
120 unsigned long threshold;
124 struct mem_cgroup_threshold_ary {
125 /* An array index points to threshold just below or equal to usage. */
126 int current_threshold;
127 /* Size of entries[] */
129 /* Array of thresholds */
130 struct mem_cgroup_threshold entries[] __counted_by(size);
133 struct mem_cgroup_thresholds {
134 /* Primary thresholds array */
135 struct mem_cgroup_threshold_ary *primary;
137 * Spare threshold array.
138 * This is needed to make mem_cgroup_unregister_event() "never fail".
139 * It must be able to store at least primary->size - 1 entries.
141 struct mem_cgroup_threshold_ary *spare;
145 * Remember four most recent foreign writebacks with dirty pages in this
146 * cgroup. Inode sharing is expected to be uncommon and, even if we miss
147 * one in a given round, we're likely to catch it later if it keeps
148 * foreign-dirtying, so a fairly low count should be enough.
150 * See mem_cgroup_track_foreign_dirty_slowpath() for details.
152 #define MEMCG_CGWB_FRN_CNT 4
154 struct memcg_cgwb_frn {
155 u64 bdi_id; /* bdi->id of the foreign inode */
156 int memcg_id; /* memcg->css.id of foreign inode */
157 u64 at; /* jiffies_64 at the time of dirtying */
158 struct wb_completion done; /* tracks in-flight foreign writebacks */
162 * Bucket for arbitrarily byte-sized objects charged to a memory
163 * cgroup. The bucket can be reparented in one piece when the cgroup
164 * is destroyed, without having to round up the individual references
165 * of all live memory objects in the wild.
168 struct percpu_ref refcnt;
169 struct mem_cgroup *memcg;
170 atomic_t nr_charged_bytes;
172 struct list_head list; /* protected by objcg_lock */
178 * The memory controller data structure. The memory controller controls both
179 * page cache and RSS per cgroup. We would eventually like to provide
180 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
181 * to help the administrator determine what knobs to tune.
184 struct cgroup_subsys_state css;
186 /* Private memcg ID. Used to ID objects that outlive the cgroup */
187 struct mem_cgroup_id id;
189 /* Accounted resources */
190 struct page_counter memory; /* Both v1 & v2 */
193 struct page_counter swap; /* v2 only */
194 struct page_counter memsw; /* v1 only */
197 /* Legacy consumer-oriented counters */
198 struct page_counter kmem; /* v1 only */
199 struct page_counter tcpmem; /* v1 only */
201 /* Range enforcement for interrupt charges */
202 struct work_struct high_work;
204 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
205 unsigned long zswap_max;
208 * Prevent pages from this memcg from being written back from zswap to
209 * swap, and from being swapped out on zswap store failures.
211 bool zswap_writeback;
214 unsigned long soft_limit;
216 /* vmpressure notifications */
217 struct vmpressure vmpressure;
220 * Should the OOM killer kill all belonging tasks, had it kill one?
224 /* protected by memcg_oom_lock */
229 /* OOM-Killer disable */
230 int oom_kill_disable;
232 /* memory.events and memory.events.local */
233 struct cgroup_file events_file;
234 struct cgroup_file events_local_file;
236 /* handle for "memory.swap.events" */
237 struct cgroup_file swap_events_file;
239 /* protect arrays of thresholds */
240 struct mutex thresholds_lock;
242 /* thresholds for memory usage. RCU-protected */
243 struct mem_cgroup_thresholds thresholds;
245 /* thresholds for mem+swap usage. RCU-protected */
246 struct mem_cgroup_thresholds memsw_thresholds;
248 /* For oom notifier event fd */
249 struct list_head oom_notify;
252 * Should we move charges of a task when a task is moved into this
253 * mem_cgroup ? And what type of charges should we move ?
255 unsigned long move_charge_at_immigrate;
256 /* taken only while moving_account > 0 */
257 spinlock_t move_lock;
258 unsigned long move_lock_flags;
260 CACHELINE_PADDING(_pad1_);
263 struct memcg_vmstats *vmstats;
266 atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
267 atomic_long_t memory_events_local[MEMCG_NR_MEMORY_EVENTS];
270 * Hint of reclaim pressure for socket memroy management. Note
271 * that this indicator should NOT be used in legacy cgroup mode
272 * where socket memory is accounted/charged separately.
274 unsigned long socket_pressure;
276 /* Legacy tcp memory accounting */
280 #ifdef CONFIG_MEMCG_KMEM
283 * memcg->objcg is wiped out as a part of the objcg repaprenting
284 * process. memcg->orig_objcg preserves a pointer (and a reference)
285 * to the original objcg until the end of live of memcg.
287 struct obj_cgroup __rcu *objcg;
288 struct obj_cgroup *orig_objcg;
289 /* list of inherited objcgs, protected by objcg_lock */
290 struct list_head objcg_list;
293 CACHELINE_PADDING(_pad2_);
296 * set > 0 if pages under this cgroup are moving to other cgroup.
298 atomic_t moving_account;
299 struct task_struct *move_lock_task;
301 struct memcg_vmstats_percpu __percpu *vmstats_percpu;
303 #ifdef CONFIG_CGROUP_WRITEBACK
304 struct list_head cgwb_list;
305 struct wb_domain cgwb_domain;
306 struct memcg_cgwb_frn cgwb_frn[MEMCG_CGWB_FRN_CNT];
309 /* List of events which userspace want to receive */
310 struct list_head event_list;
311 spinlock_t event_list_lock;
313 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
314 struct deferred_split deferred_split_queue;
317 #ifdef CONFIG_LRU_GEN_WALKS_MMU
318 /* per-memcg mm_struct list */
319 struct lru_gen_mm_list mm_list;
322 struct mem_cgroup_per_node *nodeinfo[];
326 * size of first charge trial.
327 * TODO: maybe necessary to use big numbers in big irons or dynamic based of the
330 #define MEMCG_CHARGE_BATCH 64U
332 extern struct mem_cgroup *root_mem_cgroup;
334 enum page_memcg_data_flags {
335 /* page->memcg_data is a pointer to an slabobj_ext vector */
336 MEMCG_DATA_OBJEXTS = (1UL << 0),
337 /* page has been accounted as a non-slab kernel page */
338 MEMCG_DATA_KMEM = (1UL << 1),
339 /* the next bit after the last actual flag */
340 __NR_MEMCG_DATA_FLAGS = (1UL << 2),
343 #define __FIRST_OBJEXT_FLAG __NR_MEMCG_DATA_FLAGS
345 #else /* CONFIG_MEMCG */
347 #define __FIRST_OBJEXT_FLAG (1UL << 0)
349 #endif /* CONFIG_MEMCG */
352 /* slabobj_ext vector failed to allocate */
353 OBJEXTS_ALLOC_FAIL = __FIRST_OBJEXT_FLAG,
354 /* the next bit after the last actual flag */
355 __NR_OBJEXTS_FLAGS = (__FIRST_OBJEXT_FLAG << 1),
358 #define OBJEXTS_FLAGS_MASK (__NR_OBJEXTS_FLAGS - 1)
362 static inline bool folio_memcg_kmem(struct folio *folio);
365 * After the initialization objcg->memcg is always pointing at
366 * a valid memcg, but can be atomically swapped to the parent memcg.
368 * The caller must ensure that the returned memcg won't be released:
369 * e.g. acquire the rcu_read_lock or css_set_lock.
371 static inline struct mem_cgroup *obj_cgroup_memcg(struct obj_cgroup *objcg)
373 return READ_ONCE(objcg->memcg);
377 * __folio_memcg - Get the memory cgroup associated with a non-kmem folio
378 * @folio: Pointer to the folio.
380 * Returns a pointer to the memory cgroup associated with the folio,
381 * or NULL. This function assumes that the folio is known to have a
382 * proper memory cgroup pointer. It's not safe to call this function
383 * against some type of folios, e.g. slab folios or ex-slab folios or
386 static inline struct mem_cgroup *__folio_memcg(struct folio *folio)
388 unsigned long memcg_data = folio->memcg_data;
390 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio);
391 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_OBJEXTS, folio);
392 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_KMEM, folio);
394 return (struct mem_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK);
398 * __folio_objcg - get the object cgroup associated with a kmem folio.
399 * @folio: Pointer to the folio.
401 * Returns a pointer to the object cgroup associated with the folio,
402 * or NULL. This function assumes that the folio is known to have a
403 * proper object cgroup pointer. It's not safe to call this function
404 * against some type of folios, e.g. slab folios or ex-slab folios or
407 static inline struct obj_cgroup *__folio_objcg(struct folio *folio)
409 unsigned long memcg_data = folio->memcg_data;
411 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio);
412 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_OBJEXTS, folio);
413 VM_BUG_ON_FOLIO(!(memcg_data & MEMCG_DATA_KMEM), folio);
415 return (struct obj_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK);
419 * folio_memcg - Get the memory cgroup associated with a folio.
420 * @folio: Pointer to the folio.
422 * Returns a pointer to the memory cgroup associated with the folio,
423 * or NULL. This function assumes that the folio is known to have a
424 * proper memory cgroup pointer. It's not safe to call this function
425 * against some type of folios, e.g. slab folios or ex-slab folios.
427 * For a non-kmem folio any of the following ensures folio and memcg binding
432 * - folio_memcg_lock()
433 * - exclusive reference
434 * - mem_cgroup_trylock_pages()
436 * For a kmem folio a caller should hold an rcu read lock to protect memcg
437 * associated with a kmem folio from being released.
439 static inline struct mem_cgroup *folio_memcg(struct folio *folio)
441 if (folio_memcg_kmem(folio))
442 return obj_cgroup_memcg(__folio_objcg(folio));
443 return __folio_memcg(folio);
446 static inline struct mem_cgroup *page_memcg(struct page *page)
448 return folio_memcg(page_folio(page));
452 * folio_memcg_rcu - Locklessly get the memory cgroup associated with a folio.
453 * @folio: Pointer to the folio.
455 * This function assumes that the folio is known to have a
456 * proper memory cgroup pointer. It's not safe to call this function
457 * against some type of folios, e.g. slab folios or ex-slab folios.
459 * Return: A pointer to the memory cgroup associated with the folio,
462 static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)
464 unsigned long memcg_data = READ_ONCE(folio->memcg_data);
466 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio);
467 WARN_ON_ONCE(!rcu_read_lock_held());
469 if (memcg_data & MEMCG_DATA_KMEM) {
470 struct obj_cgroup *objcg;
472 objcg = (void *)(memcg_data & ~OBJEXTS_FLAGS_MASK);
473 return obj_cgroup_memcg(objcg);
476 return (struct mem_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK);
480 * folio_memcg_check - Get the memory cgroup associated with a folio.
481 * @folio: Pointer to the folio.
483 * Returns a pointer to the memory cgroup associated with the folio,
484 * or NULL. This function unlike folio_memcg() can take any folio
485 * as an argument. It has to be used in cases when it's not known if a folio
486 * has an associated memory cgroup pointer or an object cgroups vector or
489 * For a non-kmem folio any of the following ensures folio and memcg binding
494 * - lock_folio_memcg()
495 * - exclusive reference
496 * - mem_cgroup_trylock_pages()
498 * For a kmem folio a caller should hold an rcu read lock to protect memcg
499 * associated with a kmem folio from being released.
501 static inline struct mem_cgroup *folio_memcg_check(struct folio *folio)
504 * Because folio->memcg_data might be changed asynchronously
505 * for slabs, READ_ONCE() should be used here.
507 unsigned long memcg_data = READ_ONCE(folio->memcg_data);
509 if (memcg_data & MEMCG_DATA_OBJEXTS)
512 if (memcg_data & MEMCG_DATA_KMEM) {
513 struct obj_cgroup *objcg;
515 objcg = (void *)(memcg_data & ~OBJEXTS_FLAGS_MASK);
516 return obj_cgroup_memcg(objcg);
519 return (struct mem_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK);
522 static inline struct mem_cgroup *page_memcg_check(struct page *page)
526 return folio_memcg_check((struct folio *)page);
529 static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *objcg)
531 struct mem_cgroup *memcg;
535 memcg = obj_cgroup_memcg(objcg);
536 if (unlikely(!css_tryget(&memcg->css)))
543 #ifdef CONFIG_MEMCG_KMEM
545 * folio_memcg_kmem - Check if the folio has the memcg_kmem flag set.
546 * @folio: Pointer to the folio.
548 * Checks if the folio has MemcgKmem flag set. The caller must ensure
549 * that the folio has an associated memory cgroup. It's not safe to call
550 * this function against some types of folios, e.g. slab folios.
552 static inline bool folio_memcg_kmem(struct folio *folio)
554 VM_BUG_ON_PGFLAGS(PageTail(&folio->page), &folio->page);
555 VM_BUG_ON_FOLIO(folio->memcg_data & MEMCG_DATA_OBJEXTS, folio);
556 return folio->memcg_data & MEMCG_DATA_KMEM;
561 static inline bool folio_memcg_kmem(struct folio *folio)
568 static inline bool PageMemcgKmem(struct page *page)
570 return folio_memcg_kmem(page_folio(page));
573 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
575 return (memcg == root_mem_cgroup);
578 static inline bool mem_cgroup_disabled(void)
580 return !cgroup_subsys_enabled(memory_cgrp_subsys);
583 static inline void mem_cgroup_protection(struct mem_cgroup *root,
584 struct mem_cgroup *memcg,
590 if (mem_cgroup_disabled())
594 * There is no reclaim protection applied to a targeted reclaim.
595 * We are special casing this specific case here because
596 * mem_cgroup_calculate_protection is not robust enough to keep
597 * the protection invariant for calculated effective values for
598 * parallel reclaimers with different reclaim target. This is
599 * especially a problem for tail memcgs (as they have pages on LRU)
600 * which would want to have effective values 0 for targeted reclaim
601 * but a different value for external reclaim.
604 * Let's have global and A's reclaim in parallel:
606 * A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G)
608 * | C (low = 1G, usage = 2.5G)
609 * B (low = 1G, usage = 0.5G)
611 * For the global reclaim
613 * B.elow = min(B.usage, B.low) because children_low_usage <= A.elow
614 * C.elow = min(C.usage, C.low)
616 * With the effective values resetting we have A reclaim
621 * If the global reclaim races with A's reclaim then
622 * B.elow = C.elow = 0 because children_low_usage > A.elow)
623 * is possible and reclaiming B would be violating the protection.
629 *min = READ_ONCE(memcg->memory.emin);
630 *low = READ_ONCE(memcg->memory.elow);
633 void mem_cgroup_calculate_protection(struct mem_cgroup *root,
634 struct mem_cgroup *memcg);
636 static inline bool mem_cgroup_unprotected(struct mem_cgroup *target,
637 struct mem_cgroup *memcg)
640 * The root memcg doesn't account charges, and doesn't support
641 * protection. The target memcg's protection is ignored, see
642 * mem_cgroup_calculate_protection() and mem_cgroup_protection()
644 return mem_cgroup_disabled() || mem_cgroup_is_root(memcg) ||
648 static inline bool mem_cgroup_below_low(struct mem_cgroup *target,
649 struct mem_cgroup *memcg)
651 if (mem_cgroup_unprotected(target, memcg))
654 return READ_ONCE(memcg->memory.elow) >=
655 page_counter_read(&memcg->memory);
658 static inline bool mem_cgroup_below_min(struct mem_cgroup *target,
659 struct mem_cgroup *memcg)
661 if (mem_cgroup_unprotected(target, memcg))
664 return READ_ONCE(memcg->memory.emin) >=
665 page_counter_read(&memcg->memory);
668 void mem_cgroup_commit_charge(struct folio *folio, struct mem_cgroup *memcg);
670 int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp);
673 * mem_cgroup_charge - Charge a newly allocated folio to a cgroup.
674 * @folio: Folio to charge.
675 * @mm: mm context of the allocating task.
676 * @gfp: Reclaim mode.
678 * Try to charge @folio to the memcg that @mm belongs to, reclaiming
679 * pages according to @gfp if necessary. If @mm is NULL, try to
680 * charge to the active memcg.
682 * Do not use this for folios allocated for swapin.
684 * Return: 0 on success. Otherwise, an error code is returned.
686 static inline int mem_cgroup_charge(struct folio *folio, struct mm_struct *mm,
689 if (mem_cgroup_disabled())
691 return __mem_cgroup_charge(folio, mm, gfp);
694 int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, gfp_t gfp,
697 int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm,
698 gfp_t gfp, swp_entry_t entry);
699 void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry);
701 void __mem_cgroup_uncharge(struct folio *folio);
704 * mem_cgroup_uncharge - Uncharge a folio.
705 * @folio: Folio to uncharge.
707 * Uncharge a folio previously charged with mem_cgroup_charge().
709 static inline void mem_cgroup_uncharge(struct folio *folio)
711 if (mem_cgroup_disabled())
713 __mem_cgroup_uncharge(folio);
716 void __mem_cgroup_uncharge_folios(struct folio_batch *folios);
717 static inline void mem_cgroup_uncharge_folios(struct folio_batch *folios)
719 if (mem_cgroup_disabled())
721 __mem_cgroup_uncharge_folios(folios);
724 void mem_cgroup_cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages);
725 void mem_cgroup_replace_folio(struct folio *old, struct folio *new);
726 void mem_cgroup_migrate(struct folio *old, struct folio *new);
729 * mem_cgroup_lruvec - get the lru list vector for a memcg & node
730 * @memcg: memcg of the wanted lruvec
731 * @pgdat: pglist_data
733 * Returns the lru list vector holding pages for a given @memcg &
734 * @pgdat combination. This can be the node lruvec, if the memory
735 * controller is disabled.
737 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
738 struct pglist_data *pgdat)
740 struct mem_cgroup_per_node *mz;
741 struct lruvec *lruvec;
743 if (mem_cgroup_disabled()) {
744 lruvec = &pgdat->__lruvec;
749 memcg = root_mem_cgroup;
751 mz = memcg->nodeinfo[pgdat->node_id];
752 lruvec = &mz->lruvec;
755 * Since a node can be onlined after the mem_cgroup was created,
756 * we have to be prepared to initialize lruvec->pgdat here;
757 * and if offlined then reonlined, we need to reinitialize it.
759 if (unlikely(lruvec->pgdat != pgdat))
760 lruvec->pgdat = pgdat;
765 * folio_lruvec - return lruvec for isolating/putting an LRU folio
766 * @folio: Pointer to the folio.
768 * This function relies on folio->mem_cgroup being stable.
770 static inline struct lruvec *folio_lruvec(struct folio *folio)
772 struct mem_cgroup *memcg = folio_memcg(folio);
774 VM_WARN_ON_ONCE_FOLIO(!memcg && !mem_cgroup_disabled(), folio);
775 return mem_cgroup_lruvec(memcg, folio_pgdat(folio));
778 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
780 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
782 struct mem_cgroup *get_mem_cgroup_from_current(void);
784 struct lruvec *folio_lruvec_lock(struct folio *folio);
785 struct lruvec *folio_lruvec_lock_irq(struct folio *folio);
786 struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
787 unsigned long *flags);
789 #ifdef CONFIG_DEBUG_VM
790 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio);
793 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
799 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
800 return css ? container_of(css, struct mem_cgroup, css) : NULL;
803 static inline bool obj_cgroup_tryget(struct obj_cgroup *objcg)
805 return percpu_ref_tryget(&objcg->refcnt);
808 static inline void obj_cgroup_get(struct obj_cgroup *objcg)
810 percpu_ref_get(&objcg->refcnt);
813 static inline void obj_cgroup_get_many(struct obj_cgroup *objcg,
816 percpu_ref_get_many(&objcg->refcnt, nr);
819 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, enum memcg_stat_item idx,
980 /* try to stablize folio_memcg() for all the pages in a memcg */
981 static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
985 if (mem_cgroup_disabled() || !atomic_read(&memcg->moving_account))
992 static inline void mem_cgroup_unlock_pages(void)
997 /* idx can be of type enum memcg_stat_item or node_stat_item */
998 static inline void mod_memcg_state(struct mem_cgroup *memcg,
999 enum memcg_stat_item idx, int val)
1001 unsigned long flags;
1003 local_irq_save(flags);
1004 __mod_memcg_state(memcg, idx, val);
1005 local_irq_restore(flags);
1008 static inline void mod_memcg_page_state(struct page *page,
1009 enum memcg_stat_item idx, int val)
1011 struct mem_cgroup *memcg;
1013 if (mem_cgroup_disabled())
1017 memcg = page_memcg(page);
1019 mod_memcg_state(memcg, idx, val);
1023 unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx);
1024 unsigned long lruvec_page_state(struct lruvec *lruvec, enum node_stat_item idx);
1025 unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1026 enum node_stat_item idx);
1028 void mem_cgroup_flush_stats(struct mem_cgroup *memcg);
1029 void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg);
1031 void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val);
1033 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1036 unsigned long flags;
1038 local_irq_save(flags);
1039 __mod_lruvec_kmem_state(p, idx, val);
1040 local_irq_restore(flags);
1043 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
1044 unsigned long count);
1046 static inline void count_memcg_events(struct mem_cgroup *memcg,
1047 enum vm_event_item idx,
1048 unsigned long count)
1050 unsigned long flags;
1052 local_irq_save(flags);
1053 __count_memcg_events(memcg, idx, count);
1054 local_irq_restore(flags);
1057 static inline void count_memcg_folio_events(struct folio *folio,
1058 enum vm_event_item idx, unsigned long nr)
1060 struct mem_cgroup *memcg = folio_memcg(folio);
1063 count_memcg_events(memcg, idx, nr);
1066 static inline void count_memcg_event_mm(struct mm_struct *mm,
1067 enum vm_event_item idx)
1069 struct mem_cgroup *memcg;
1071 if (mem_cgroup_disabled())
1075 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1077 count_memcg_events(memcg, idx, 1);
1081 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1082 enum memcg_memory_event event)
1084 bool swap_event = event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX ||
1085 event == MEMCG_SWAP_FAIL;
1087 atomic_long_inc(&memcg->memory_events_local[event]);
1089 cgroup_file_notify(&memcg->events_local_file);
1092 atomic_long_inc(&memcg->memory_events[event]);
1094 cgroup_file_notify(&memcg->swap_events_file);
1096 cgroup_file_notify(&memcg->events_file);
1098 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1100 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
1102 } while ((memcg = parent_mem_cgroup(memcg)) &&
1103 !mem_cgroup_is_root(memcg));
1106 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1107 enum memcg_memory_event event)
1109 struct mem_cgroup *memcg;
1111 if (mem_cgroup_disabled())
1115 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1117 memcg_memory_event(memcg, event);
1121 void split_page_memcg(struct page *head, int old_order, int new_order);
1123 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1125 unsigned long *total_scanned);
1127 #else /* CONFIG_MEMCG */
1129 #define MEM_CGROUP_ID_SHIFT 0
1131 static inline struct mem_cgroup *folio_memcg(struct folio *folio)
1136 static inline struct mem_cgroup *page_memcg(struct page *page)
1141 static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)
1143 WARN_ON_ONCE(!rcu_read_lock_held());
1147 static inline struct mem_cgroup *folio_memcg_check(struct folio *folio)
1152 static inline struct mem_cgroup *page_memcg_check(struct page *page)
1157 static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *objcg)
1162 static inline bool folio_memcg_kmem(struct folio *folio)
1167 static inline bool PageMemcgKmem(struct page *page)
1172 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
1177 static inline bool mem_cgroup_disabled(void)
1182 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1183 enum memcg_memory_event event)
1187 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1188 enum memcg_memory_event event)
1192 static inline void mem_cgroup_protection(struct mem_cgroup *root,
1193 struct mem_cgroup *memcg,
1200 static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root,
1201 struct mem_cgroup *memcg)
1205 static inline bool mem_cgroup_unprotected(struct mem_cgroup *target,
1206 struct mem_cgroup *memcg)
1210 static inline bool mem_cgroup_below_low(struct mem_cgroup *target,
1211 struct mem_cgroup *memcg)
1216 static inline bool mem_cgroup_below_min(struct mem_cgroup *target,
1217 struct mem_cgroup *memcg)
1222 static inline void mem_cgroup_commit_charge(struct folio *folio,
1223 struct mem_cgroup *memcg)
1227 static inline int mem_cgroup_charge(struct folio *folio,
1228 struct mm_struct *mm, gfp_t gfp)
1233 static inline int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg,
1234 gfp_t gfp, long nr_pages)
1239 static inline int mem_cgroup_swapin_charge_folio(struct folio *folio,
1240 struct mm_struct *mm, gfp_t gfp, swp_entry_t entry)
1245 static inline void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry)
1249 static inline void mem_cgroup_uncharge(struct folio *folio)
1253 static inline void mem_cgroup_uncharge_folios(struct folio_batch *folios)
1257 static inline void mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
1258 unsigned int nr_pages)
1262 static inline void mem_cgroup_replace_folio(struct folio *old,
1267 static inline void mem_cgroup_migrate(struct folio *old, struct folio *new)
1271 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
1272 struct pglist_data *pgdat)
1274 return &pgdat->__lruvec;
1277 static inline struct lruvec *folio_lruvec(struct folio *folio)
1279 struct pglist_data *pgdat = folio_pgdat(folio);
1280 return &pgdat->__lruvec;
1284 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
1288 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
1293 static inline bool mm_match_cgroup(struct mm_struct *mm,
1294 struct mem_cgroup *memcg)
1299 static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
1304 static inline struct mem_cgroup *get_mem_cgroup_from_current(void)
1310 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css)
1315 static inline void obj_cgroup_put(struct obj_cgroup *objcg)
1319 static inline bool mem_cgroup_tryget(struct mem_cgroup *memcg)
1324 static inline bool mem_cgroup_tryget_online(struct mem_cgroup *memcg)
1329 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
1333 static inline struct lruvec *folio_lruvec_lock(struct folio *folio)
1335 struct pglist_data *pgdat = folio_pgdat(folio);
1337 spin_lock(&pgdat->__lruvec.lru_lock);
1338 return &pgdat->__lruvec;
1341 static inline struct lruvec *folio_lruvec_lock_irq(struct folio *folio)
1343 struct pglist_data *pgdat = folio_pgdat(folio);
1345 spin_lock_irq(&pgdat->__lruvec.lru_lock);
1346 return &pgdat->__lruvec;
1349 static inline struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
1350 unsigned long *flagsp)
1352 struct pglist_data *pgdat = folio_pgdat(folio);
1354 spin_lock_irqsave(&pgdat->__lruvec.lru_lock, *flagsp);
1355 return &pgdat->__lruvec;
1358 static inline struct mem_cgroup *
1359 mem_cgroup_iter(struct mem_cgroup *root,
1360 struct mem_cgroup *prev,
1361 struct mem_cgroup_reclaim_cookie *reclaim)
1366 static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
1367 struct mem_cgroup *prev)
1371 static inline void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1372 int (*fn)(struct task_struct *, void *), void *arg)
1376 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
1381 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
1384 /* XXX: This should always return root_mem_cgroup */
1388 #ifdef CONFIG_SHRINKER_DEBUG
1389 static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg)
1394 static inline struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino)
1400 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
1405 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
1410 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
1416 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
1417 enum lru_list lru, int zone_idx)
1422 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
1427 static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1433 mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1438 mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
1442 static inline void folio_memcg_lock(struct folio *folio)
1446 static inline void folio_memcg_unlock(struct folio *folio)
1450 static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
1452 /* to match folio_memcg_rcu() */
1457 static inline void mem_cgroup_unlock_pages(void)
1462 static inline void mem_cgroup_handle_over_high(gfp_t gfp_mask)
1466 static inline void mem_cgroup_enter_user_fault(void)
1470 static inline void mem_cgroup_exit_user_fault(void)
1474 static inline bool task_in_memcg_oom(struct task_struct *p)
1479 static inline bool mem_cgroup_oom_synchronize(bool wait)
1484 static inline struct mem_cgroup *mem_cgroup_get_oom_group(
1485 struct task_struct *victim, struct mem_cgroup *oom_domain)
1490 static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
1494 static inline void __mod_memcg_state(struct mem_cgroup *memcg,
1495 enum memcg_stat_item idx,
1500 static inline void mod_memcg_state(struct mem_cgroup *memcg,
1501 enum memcg_stat_item idx,
1506 static inline void mod_memcg_page_state(struct page *page,
1507 enum memcg_stat_item idx, int val)
1511 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
1516 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1517 enum node_stat_item idx)
1519 return node_page_state(lruvec_pgdat(lruvec), idx);
1522 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1523 enum node_stat_item idx)
1525 return node_page_state(lruvec_pgdat(lruvec), idx);
1528 static inline void mem_cgroup_flush_stats(struct mem_cgroup *memcg)
1532 static inline void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg)
1536 static inline void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1539 struct page *page = virt_to_head_page(p);
1541 __mod_node_page_state(page_pgdat(page), idx, val);
1544 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1547 struct page *page = virt_to_head_page(p);
1549 mod_node_page_state(page_pgdat(page), idx, val);
1552 static inline void count_memcg_events(struct mem_cgroup *memcg,
1553 enum vm_event_item idx,
1554 unsigned long count)
1558 static inline void __count_memcg_events(struct mem_cgroup *memcg,
1559 enum vm_event_item idx,
1560 unsigned long count)
1564 static inline void count_memcg_folio_events(struct folio *folio,
1565 enum vm_event_item idx, unsigned long nr)
1570 void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
1574 static inline void split_page_memcg(struct page *head, int old_order, int new_order)
1579 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1581 unsigned long *total_scanned)
1585 #endif /* CONFIG_MEMCG */
1588 * Extended information for slab objects stored as an array in page->memcg_data
1589 * if MEMCG_DATA_OBJEXTS is set.
1591 struct slabobj_ext {
1592 #ifdef CONFIG_MEMCG_KMEM
1593 struct obj_cgroup *objcg;
1595 #ifdef CONFIG_MEM_ALLOC_PROFILING
1596 union codetag_ref ref;
1600 static inline void __inc_lruvec_kmem_state(void *p, enum node_stat_item idx)
1602 __mod_lruvec_kmem_state(p, idx, 1);
1605 static inline void __dec_lruvec_kmem_state(void *p, enum node_stat_item idx)
1607 __mod_lruvec_kmem_state(p, idx, -1);
1610 static inline struct lruvec *parent_lruvec(struct lruvec *lruvec)
1612 struct mem_cgroup *memcg;
1614 memcg = lruvec_memcg(lruvec);
1617 memcg = parent_mem_cgroup(memcg);
1620 return mem_cgroup_lruvec(memcg, lruvec_pgdat(lruvec));
1623 static inline void unlock_page_lruvec(struct lruvec *lruvec)
1625 spin_unlock(&lruvec->lru_lock);
1628 static inline void unlock_page_lruvec_irq(struct lruvec *lruvec)
1630 spin_unlock_irq(&lruvec->lru_lock);
1633 static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec,
1634 unsigned long flags)
1636 spin_unlock_irqrestore(&lruvec->lru_lock, flags);
1639 /* Test requires a stable page->memcg binding, see page_memcg() */
1640 static inline bool folio_matches_lruvec(struct folio *folio,
1641 struct lruvec *lruvec)
1643 return lruvec_pgdat(lruvec) == folio_pgdat(folio) &&
1644 lruvec_memcg(lruvec) == folio_memcg(folio);
1647 /* Don't lock again iff page's lruvec locked */
1648 static inline struct lruvec *folio_lruvec_relock_irq(struct folio *folio,
1649 struct lruvec *locked_lruvec)
1651 if (locked_lruvec) {
1652 if (folio_matches_lruvec(folio, locked_lruvec))
1653 return locked_lruvec;
1655 unlock_page_lruvec_irq(locked_lruvec);
1658 return folio_lruvec_lock_irq(folio);
1661 /* Don't lock again iff folio's lruvec locked */
1662 static inline void folio_lruvec_relock_irqsave(struct folio *folio,
1663 struct lruvec **lruvecp, unsigned long *flags)
1666 if (folio_matches_lruvec(folio, *lruvecp))
1669 unlock_page_lruvec_irqrestore(*lruvecp, *flags);
1672 *lruvecp = folio_lruvec_lock_irqsave(folio, flags);
1675 #ifdef CONFIG_CGROUP_WRITEBACK
1677 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
1678 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1679 unsigned long *pheadroom, unsigned long *pdirty,
1680 unsigned long *pwriteback);
1682 void mem_cgroup_track_foreign_dirty_slowpath(struct folio *folio,
1683 struct bdi_writeback *wb);
1685 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio,
1686 struct bdi_writeback *wb)
1688 struct mem_cgroup *memcg;
1690 if (mem_cgroup_disabled())
1693 memcg = folio_memcg(folio);
1694 if (unlikely(memcg && &memcg->css != wb->memcg_css))
1695 mem_cgroup_track_foreign_dirty_slowpath(folio, wb);
1698 void mem_cgroup_flush_foreign(struct bdi_writeback *wb);
1700 #else /* CONFIG_CGROUP_WRITEBACK */
1702 static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1707 static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
1708 unsigned long *pfilepages,
1709 unsigned long *pheadroom,
1710 unsigned long *pdirty,
1711 unsigned long *pwriteback)
1715 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio,
1716 struct bdi_writeback *wb)
1720 static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
1724 #endif /* CONFIG_CGROUP_WRITEBACK */
1727 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
1729 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1731 extern struct static_key_false memcg_sockets_enabled_key;
1732 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
1733 void mem_cgroup_sk_alloc(struct sock *sk);
1734 void mem_cgroup_sk_free(struct sock *sk);
1735 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1737 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1738 return !!memcg->tcpmem_pressure;
1740 if (time_before(jiffies, READ_ONCE(memcg->socket_pressure)))
1742 } while ((memcg = parent_mem_cgroup(memcg)));
1746 int alloc_shrinker_info(struct mem_cgroup *memcg);
1747 void free_shrinker_info(struct mem_cgroup *memcg);
1748 void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id);
1749 void reparent_shrinker_deferred(struct mem_cgroup *memcg);
1751 #define mem_cgroup_sockets_enabled 0
1752 static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1753 static inline void mem_cgroup_sk_free(struct sock *sk) { };
1754 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1759 static inline void set_shrinker_bit(struct mem_cgroup *memcg,
1760 int nid, int shrinker_id)
1765 #ifdef CONFIG_MEMCG_KMEM
1766 bool mem_cgroup_kmem_disabled(void);
1767 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order);
1768 void __memcg_kmem_uncharge_page(struct page *page, int order);
1771 * The returned objcg pointer is safe to use without additional
1772 * protection within a scope. The scope is defined either by
1773 * the current task (similar to the "current" global variable)
1774 * or by set_active_memcg() pair.
1775 * Please, use obj_cgroup_get() to get a reference if the pointer
1776 * needs to be used outside of the local scope.
1778 struct obj_cgroup *current_obj_cgroup(void);
1779 struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio);
1781 static inline struct obj_cgroup *get_obj_cgroup_from_current(void)
1783 struct obj_cgroup *objcg = current_obj_cgroup();
1786 obj_cgroup_get(objcg);
1791 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size);
1792 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size);
1794 extern struct static_key_false memcg_bpf_enabled_key;
1795 static inline bool memcg_bpf_enabled(void)
1797 return static_branch_likely(&memcg_bpf_enabled_key);
1800 extern struct static_key_false memcg_kmem_online_key;
1802 static inline bool memcg_kmem_online(void)
1804 return static_branch_likely(&memcg_kmem_online_key);
1807 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1810 if (memcg_kmem_online())
1811 return __memcg_kmem_charge_page(page, gfp, order);
1815 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1817 if (memcg_kmem_online())
1818 __memcg_kmem_uncharge_page(page, order);
1822 * A helper for accessing memcg's kmem_id, used for getting
1823 * corresponding LRU lists.
1825 static inline int memcg_kmem_id(struct mem_cgroup *memcg)
1827 return memcg ? memcg->kmemcg_id : -1;
1830 struct mem_cgroup *mem_cgroup_from_obj(void *p);
1831 struct mem_cgroup *mem_cgroup_from_slab_obj(void *p);
1833 static inline void count_objcg_event(struct obj_cgroup *objcg,
1834 enum vm_event_item idx)
1836 struct mem_cgroup *memcg;
1838 if (!memcg_kmem_online())
1842 memcg = obj_cgroup_memcg(objcg);
1843 count_memcg_events(memcg, idx, 1);
1848 static inline bool mem_cgroup_kmem_disabled(void)
1853 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1859 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1863 static inline int __memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1869 static inline void __memcg_kmem_uncharge_page(struct page *page, int order)
1873 static inline struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio)
1878 static inline bool memcg_bpf_enabled(void)
1883 static inline bool memcg_kmem_online(void)
1888 static inline int memcg_kmem_id(struct mem_cgroup *memcg)
1893 static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)
1898 static inline struct mem_cgroup *mem_cgroup_from_slab_obj(void *p)
1903 static inline void count_objcg_event(struct obj_cgroup *objcg,
1904 enum vm_event_item idx)
1908 #endif /* CONFIG_MEMCG_KMEM */
1910 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
1911 bool obj_cgroup_may_zswap(struct obj_cgroup *objcg);
1912 void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size);
1913 void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size);
1914 bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg);
1916 static inline bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
1920 static inline void obj_cgroup_charge_zswap(struct obj_cgroup *objcg,
1924 static inline void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg,
1928 static inline bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg)
1930 /* if zswap is disabled, do not block pages going to the swapping device */
1935 #endif /* _LINUX_MEMCONTROL_H */