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 unsigned long soft_limit;
226 /* vmpressure notifications */
227 struct vmpressure vmpressure;
230 * Should the OOM killer kill all belonging tasks, had it kill one?
234 /* protected by memcg_oom_lock */
239 /* OOM-Killer disable */
240 int oom_kill_disable;
242 /* memory.events and memory.events.local */
243 struct cgroup_file events_file;
244 struct cgroup_file events_local_file;
246 /* handle for "memory.swap.events" */
247 struct cgroup_file swap_events_file;
249 /* protect arrays of thresholds */
250 struct mutex thresholds_lock;
252 /* thresholds for memory usage. RCU-protected */
253 struct mem_cgroup_thresholds thresholds;
255 /* thresholds for mem+swap usage. RCU-protected */
256 struct mem_cgroup_thresholds memsw_thresholds;
258 /* For oom notifier event fd */
259 struct list_head oom_notify;
262 * Should we move charges of a task when a task is moved into this
263 * mem_cgroup ? And what type of charges should we move ?
265 unsigned long move_charge_at_immigrate;
266 /* taken only while moving_account > 0 */
267 spinlock_t move_lock;
268 unsigned long move_lock_flags;
270 CACHELINE_PADDING(_pad1_);
273 struct memcg_vmstats *vmstats;
276 atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
277 atomic_long_t memory_events_local[MEMCG_NR_MEMORY_EVENTS];
280 * Hint of reclaim pressure for socket memroy management. Note
281 * that this indicator should NOT be used in legacy cgroup mode
282 * where socket memory is accounted/charged separately.
284 unsigned long socket_pressure;
286 /* Legacy tcp memory accounting */
290 #ifdef CONFIG_MEMCG_KMEM
293 * memcg->objcg is wiped out as a part of the objcg repaprenting
294 * process. memcg->orig_objcg preserves a pointer (and a reference)
295 * to the original objcg until the end of live of memcg.
297 struct obj_cgroup __rcu *objcg;
298 struct obj_cgroup *orig_objcg;
299 /* list of inherited objcgs, protected by objcg_lock */
300 struct list_head objcg_list;
303 CACHELINE_PADDING(_pad2_);
306 * set > 0 if pages under this cgroup are moving to other cgroup.
308 atomic_t moving_account;
309 struct task_struct *move_lock_task;
311 struct memcg_vmstats_percpu __percpu *vmstats_percpu;
313 #ifdef CONFIG_CGROUP_WRITEBACK
314 struct list_head cgwb_list;
315 struct wb_domain cgwb_domain;
316 struct memcg_cgwb_frn cgwb_frn[MEMCG_CGWB_FRN_CNT];
319 /* List of events which userspace want to receive */
320 struct list_head event_list;
321 spinlock_t event_list_lock;
323 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
324 struct deferred_split deferred_split_queue;
327 #ifdef CONFIG_LRU_GEN
328 /* per-memcg mm_struct list */
329 struct lru_gen_mm_list mm_list;
332 struct mem_cgroup_per_node *nodeinfo[];
336 * size of first charge trial.
337 * TODO: maybe necessary to use big numbers in big irons or dynamic based of the
340 #define MEMCG_CHARGE_BATCH 64U
342 extern struct mem_cgroup *root_mem_cgroup;
344 enum page_memcg_data_flags {
345 /* page->memcg_data is a pointer to an objcgs vector */
346 MEMCG_DATA_OBJCGS = (1UL << 0),
347 /* page has been accounted as a non-slab kernel page */
348 MEMCG_DATA_KMEM = (1UL << 1),
349 /* the next bit after the last actual flag */
350 __NR_MEMCG_DATA_FLAGS = (1UL << 2),
353 #define MEMCG_DATA_FLAGS_MASK (__NR_MEMCG_DATA_FLAGS - 1)
355 static inline bool folio_memcg_kmem(struct folio *folio);
358 * After the initialization objcg->memcg is always pointing at
359 * a valid memcg, but can be atomically swapped to the parent memcg.
361 * The caller must ensure that the returned memcg won't be released:
362 * e.g. acquire the rcu_read_lock or css_set_lock.
364 static inline struct mem_cgroup *obj_cgroup_memcg(struct obj_cgroup *objcg)
366 return READ_ONCE(objcg->memcg);
370 * __folio_memcg - Get the memory cgroup associated with a non-kmem folio
371 * @folio: Pointer to the folio.
373 * Returns a pointer to the memory cgroup associated with the folio,
374 * or NULL. This function assumes that the folio is known to have a
375 * proper memory cgroup pointer. It's not safe to call this function
376 * against some type of folios, e.g. slab folios or ex-slab folios or
379 static inline struct mem_cgroup *__folio_memcg(struct folio *folio)
381 unsigned long memcg_data = folio->memcg_data;
383 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio);
384 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_OBJCGS, folio);
385 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_KMEM, folio);
387 return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
391 * __folio_objcg - get the object cgroup associated with a kmem folio.
392 * @folio: Pointer to the folio.
394 * Returns a pointer to the object cgroup associated with the folio,
395 * or NULL. This function assumes that the folio is known to have a
396 * proper object cgroup pointer. It's not safe to call this function
397 * against some type of folios, e.g. slab folios or ex-slab folios or
400 static inline struct obj_cgroup *__folio_objcg(struct folio *folio)
402 unsigned long memcg_data = folio->memcg_data;
404 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio);
405 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_OBJCGS, folio);
406 VM_BUG_ON_FOLIO(!(memcg_data & MEMCG_DATA_KMEM), folio);
408 return (struct obj_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
412 * folio_memcg - Get the memory cgroup associated with a folio.
413 * @folio: Pointer to the folio.
415 * Returns a pointer to the memory cgroup associated with the folio,
416 * or NULL. This function assumes that the folio is known to have a
417 * proper memory cgroup pointer. It's not safe to call this function
418 * against some type of folios, e.g. slab folios or ex-slab folios.
420 * For a non-kmem folio any of the following ensures folio and memcg binding
425 * - folio_memcg_lock()
426 * - exclusive reference
427 * - mem_cgroup_trylock_pages()
429 * For a kmem folio a caller should hold an rcu read lock to protect memcg
430 * associated with a kmem folio from being released.
432 static inline struct mem_cgroup *folio_memcg(struct folio *folio)
434 if (folio_memcg_kmem(folio))
435 return obj_cgroup_memcg(__folio_objcg(folio));
436 return __folio_memcg(folio);
439 static inline struct mem_cgroup *page_memcg(struct page *page)
441 return folio_memcg(page_folio(page));
445 * folio_memcg_rcu - Locklessly get the memory cgroup associated with a folio.
446 * @folio: Pointer to the folio.
448 * This function assumes that the folio is known to have a
449 * proper memory cgroup pointer. It's not safe to call this function
450 * against some type of folios, e.g. slab folios or ex-slab folios.
452 * Return: A pointer to the memory cgroup associated with the folio,
455 static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)
457 unsigned long memcg_data = READ_ONCE(folio->memcg_data);
459 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio);
460 WARN_ON_ONCE(!rcu_read_lock_held());
462 if (memcg_data & MEMCG_DATA_KMEM) {
463 struct obj_cgroup *objcg;
465 objcg = (void *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
466 return obj_cgroup_memcg(objcg);
469 return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
473 * folio_memcg_check - Get the memory cgroup associated with a folio.
474 * @folio: Pointer to the folio.
476 * Returns a pointer to the memory cgroup associated with the folio,
477 * or NULL. This function unlike folio_memcg() can take any folio
478 * as an argument. It has to be used in cases when it's not known if a folio
479 * has an associated memory cgroup pointer or an object cgroups vector or
482 * For a non-kmem folio any of the following ensures folio and memcg binding
487 * - lock_folio_memcg()
488 * - exclusive reference
489 * - mem_cgroup_trylock_pages()
491 * For a kmem folio a caller should hold an rcu read lock to protect memcg
492 * associated with a kmem folio from being released.
494 static inline struct mem_cgroup *folio_memcg_check(struct folio *folio)
497 * Because folio->memcg_data might be changed asynchronously
498 * for slabs, READ_ONCE() should be used here.
500 unsigned long memcg_data = READ_ONCE(folio->memcg_data);
502 if (memcg_data & MEMCG_DATA_OBJCGS)
505 if (memcg_data & MEMCG_DATA_KMEM) {
506 struct obj_cgroup *objcg;
508 objcg = (void *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
509 return obj_cgroup_memcg(objcg);
512 return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
515 static inline struct mem_cgroup *page_memcg_check(struct page *page)
519 return folio_memcg_check((struct folio *)page);
522 static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *objcg)
524 struct mem_cgroup *memcg;
528 memcg = obj_cgroup_memcg(objcg);
529 if (unlikely(!css_tryget(&memcg->css)))
536 #ifdef CONFIG_MEMCG_KMEM
538 * folio_memcg_kmem - Check if the folio has the memcg_kmem flag set.
539 * @folio: Pointer to the folio.
541 * Checks if the folio has MemcgKmem flag set. The caller must ensure
542 * that the folio has an associated memory cgroup. It's not safe to call
543 * this function against some types of folios, e.g. slab folios.
545 static inline bool folio_memcg_kmem(struct folio *folio)
547 VM_BUG_ON_PGFLAGS(PageTail(&folio->page), &folio->page);
548 VM_BUG_ON_FOLIO(folio->memcg_data & MEMCG_DATA_OBJCGS, folio);
549 return folio->memcg_data & MEMCG_DATA_KMEM;
554 static inline bool folio_memcg_kmem(struct folio *folio)
561 static inline bool PageMemcgKmem(struct page *page)
563 return folio_memcg_kmem(page_folio(page));
566 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
568 return (memcg == root_mem_cgroup);
571 static inline bool mem_cgroup_disabled(void)
573 return !cgroup_subsys_enabled(memory_cgrp_subsys);
576 static inline void mem_cgroup_protection(struct mem_cgroup *root,
577 struct mem_cgroup *memcg,
583 if (mem_cgroup_disabled())
587 * There is no reclaim protection applied to a targeted reclaim.
588 * We are special casing this specific case here because
589 * mem_cgroup_calculate_protection is not robust enough to keep
590 * the protection invariant for calculated effective values for
591 * parallel reclaimers with different reclaim target. This is
592 * especially a problem for tail memcgs (as they have pages on LRU)
593 * which would want to have effective values 0 for targeted reclaim
594 * but a different value for external reclaim.
597 * Let's have global and A's reclaim in parallel:
599 * A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G)
601 * | C (low = 1G, usage = 2.5G)
602 * B (low = 1G, usage = 0.5G)
604 * For the global reclaim
606 * B.elow = min(B.usage, B.low) because children_low_usage <= A.elow
607 * C.elow = min(C.usage, C.low)
609 * With the effective values resetting we have A reclaim
614 * If the global reclaim races with A's reclaim then
615 * B.elow = C.elow = 0 because children_low_usage > A.elow)
616 * is possible and reclaiming B would be violating the protection.
622 *min = READ_ONCE(memcg->memory.emin);
623 *low = READ_ONCE(memcg->memory.elow);
626 void mem_cgroup_calculate_protection(struct mem_cgroup *root,
627 struct mem_cgroup *memcg);
629 static inline bool mem_cgroup_unprotected(struct mem_cgroup *target,
630 struct mem_cgroup *memcg)
633 * The root memcg doesn't account charges, and doesn't support
634 * protection. The target memcg's protection is ignored, see
635 * mem_cgroup_calculate_protection() and mem_cgroup_protection()
637 return mem_cgroup_disabled() || mem_cgroup_is_root(memcg) ||
641 static inline bool mem_cgroup_below_low(struct mem_cgroup *target,
642 struct mem_cgroup *memcg)
644 if (mem_cgroup_unprotected(target, memcg))
647 return READ_ONCE(memcg->memory.elow) >=
648 page_counter_read(&memcg->memory);
651 static inline bool mem_cgroup_below_min(struct mem_cgroup *target,
652 struct mem_cgroup *memcg)
654 if (mem_cgroup_unprotected(target, memcg))
657 return READ_ONCE(memcg->memory.emin) >=
658 page_counter_read(&memcg->memory);
661 void mem_cgroup_commit_charge(struct folio *folio, struct mem_cgroup *memcg);
663 int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp);
666 * mem_cgroup_charge - Charge a newly allocated folio to a cgroup.
667 * @folio: Folio to charge.
668 * @mm: mm context of the allocating task.
669 * @gfp: Reclaim mode.
671 * Try to charge @folio to the memcg that @mm belongs to, reclaiming
672 * pages according to @gfp if necessary. If @mm is NULL, try to
673 * charge to the active memcg.
675 * Do not use this for folios allocated for swapin.
677 * Return: 0 on success. Otherwise, an error code is returned.
679 static inline int mem_cgroup_charge(struct folio *folio, struct mm_struct *mm,
682 if (mem_cgroup_disabled())
684 return __mem_cgroup_charge(folio, mm, gfp);
687 int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, gfp_t gfp,
690 int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm,
691 gfp_t gfp, swp_entry_t entry);
692 void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry);
694 void __mem_cgroup_uncharge(struct folio *folio);
697 * mem_cgroup_uncharge - Uncharge a folio.
698 * @folio: Folio to uncharge.
700 * Uncharge a folio previously charged with mem_cgroup_charge().
702 static inline void mem_cgroup_uncharge(struct folio *folio)
704 if (mem_cgroup_disabled())
706 __mem_cgroup_uncharge(folio);
709 void __mem_cgroup_uncharge_list(struct list_head *page_list);
710 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
712 if (mem_cgroup_disabled())
714 __mem_cgroup_uncharge_list(page_list);
717 void mem_cgroup_cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages);
719 void mem_cgroup_replace_folio(struct folio *old, struct folio *new);
721 void mem_cgroup_migrate(struct folio *old, struct folio *new);
724 * mem_cgroup_lruvec - get the lru list vector for a memcg & node
725 * @memcg: memcg of the wanted lruvec
726 * @pgdat: pglist_data
728 * Returns the lru list vector holding pages for a given @memcg &
729 * @pgdat combination. This can be the node lruvec, if the memory
730 * controller is disabled.
732 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
733 struct pglist_data *pgdat)
735 struct mem_cgroup_per_node *mz;
736 struct lruvec *lruvec;
738 if (mem_cgroup_disabled()) {
739 lruvec = &pgdat->__lruvec;
744 memcg = root_mem_cgroup;
746 mz = memcg->nodeinfo[pgdat->node_id];
747 lruvec = &mz->lruvec;
750 * Since a node can be onlined after the mem_cgroup was created,
751 * we have to be prepared to initialize lruvec->pgdat here;
752 * and if offlined then reonlined, we need to reinitialize it.
754 if (unlikely(lruvec->pgdat != pgdat))
755 lruvec->pgdat = pgdat;
760 * folio_lruvec - return lruvec for isolating/putting an LRU folio
761 * @folio: Pointer to the folio.
763 * This function relies on folio->mem_cgroup being stable.
765 static inline struct lruvec *folio_lruvec(struct folio *folio)
767 struct mem_cgroup *memcg = folio_memcg(folio);
769 VM_WARN_ON_ONCE_FOLIO(!memcg && !mem_cgroup_disabled(), folio);
770 return mem_cgroup_lruvec(memcg, folio_pgdat(folio));
773 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
775 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
777 struct mem_cgroup *get_mem_cgroup_from_current(void);
779 struct lruvec *folio_lruvec_lock(struct folio *folio);
780 struct lruvec *folio_lruvec_lock_irq(struct folio *folio);
781 struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
782 unsigned long *flags);
784 #ifdef CONFIG_DEBUG_VM
785 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio);
788 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
794 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
795 return css ? container_of(css, struct mem_cgroup, css) : NULL;
798 static inline bool obj_cgroup_tryget(struct obj_cgroup *objcg)
800 return percpu_ref_tryget(&objcg->refcnt);
803 static inline void obj_cgroup_get(struct obj_cgroup *objcg)
805 percpu_ref_get(&objcg->refcnt);
808 static inline void obj_cgroup_get_many(struct obj_cgroup *objcg,
811 percpu_ref_get_many(&objcg->refcnt, nr);
814 static inline void obj_cgroup_put(struct obj_cgroup *objcg)
816 percpu_ref_put(&objcg->refcnt);
819 static inline bool mem_cgroup_tryget(struct mem_cgroup *memcg)
821 return !memcg || css_tryget(&memcg->css);
824 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
827 css_put(&memcg->css);
830 #define mem_cgroup_from_counter(counter, member) \
831 container_of(counter, struct mem_cgroup, member)
833 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
835 struct mem_cgroup_reclaim_cookie *);
836 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
837 void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
838 int (*)(struct task_struct *, void *), void *arg);
840 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
842 if (mem_cgroup_disabled())
847 struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
849 #ifdef CONFIG_SHRINKER_DEBUG
850 static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg)
852 return memcg ? cgroup_ino(memcg->css.cgroup) : 0;
855 struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino);
858 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
860 return mem_cgroup_from_css(seq_css(m));
863 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
865 struct mem_cgroup_per_node *mz;
867 if (mem_cgroup_disabled())
870 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
875 * parent_mem_cgroup - find the accounting parent of a memcg
876 * @memcg: memcg whose parent to find
878 * Returns the parent memcg, or NULL if this is the root.
880 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
882 return mem_cgroup_from_css(memcg->css.parent);
885 static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
886 struct mem_cgroup *root)
890 return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
893 static inline bool mm_match_cgroup(struct mm_struct *mm,
894 struct mem_cgroup *memcg)
896 struct mem_cgroup *task_memcg;
900 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
902 match = mem_cgroup_is_descendant(task_memcg, memcg);
907 struct cgroup_subsys_state *mem_cgroup_css_from_folio(struct folio *folio);
908 ino_t page_cgroup_ino(struct page *page);
910 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
912 if (mem_cgroup_disabled())
914 return !!(memcg->css.flags & CSS_ONLINE);
917 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
918 int zid, int nr_pages);
921 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
922 enum lru_list lru, int zone_idx)
924 struct mem_cgroup_per_node *mz;
926 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
927 return READ_ONCE(mz->lru_zone_size[zone_idx][lru]);
930 void mem_cgroup_handle_over_high(gfp_t gfp_mask);
932 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
934 unsigned long mem_cgroup_size(struct mem_cgroup *memcg);
936 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
937 struct task_struct *p);
939 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg);
941 static inline void mem_cgroup_enter_user_fault(void)
943 WARN_ON(current->in_user_fault);
944 current->in_user_fault = 1;
947 static inline void mem_cgroup_exit_user_fault(void)
949 WARN_ON(!current->in_user_fault);
950 current->in_user_fault = 0;
953 static inline bool task_in_memcg_oom(struct task_struct *p)
955 return p->memcg_in_oom;
958 bool mem_cgroup_oom_synchronize(bool wait);
959 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
960 struct mem_cgroup *oom_domain);
961 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg);
963 void folio_memcg_lock(struct folio *folio);
964 void folio_memcg_unlock(struct folio *folio);
966 void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val);
968 /* try to stablize folio_memcg() for all the pages in a memcg */
969 static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
973 if (mem_cgroup_disabled() || !atomic_read(&memcg->moving_account))
980 static inline void mem_cgroup_unlock_pages(void)
985 /* idx can be of type enum memcg_stat_item or node_stat_item */
986 static inline void mod_memcg_state(struct mem_cgroup *memcg,
991 local_irq_save(flags);
992 __mod_memcg_state(memcg, idx, val);
993 local_irq_restore(flags);
996 static inline void mod_memcg_page_state(struct page *page,
999 struct mem_cgroup *memcg;
1001 if (mem_cgroup_disabled())
1005 memcg = page_memcg(page);
1007 mod_memcg_state(memcg, idx, val);
1011 unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx);
1013 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1014 enum node_stat_item idx)
1016 struct mem_cgroup_per_node *pn;
1019 if (mem_cgroup_disabled())
1020 return node_page_state(lruvec_pgdat(lruvec), idx);
1022 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
1023 x = READ_ONCE(pn->lruvec_stats.state[idx]);
1031 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1032 enum node_stat_item idx)
1034 struct mem_cgroup_per_node *pn;
1037 if (mem_cgroup_disabled())
1038 return node_page_state(lruvec_pgdat(lruvec), idx);
1040 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
1041 x = READ_ONCE(pn->lruvec_stats.state_local[idx]);
1049 void mem_cgroup_flush_stats(void);
1050 void mem_cgroup_flush_stats_ratelimited(void);
1052 void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
1054 void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val);
1056 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1059 unsigned long flags;
1061 local_irq_save(flags);
1062 __mod_lruvec_kmem_state(p, idx, val);
1063 local_irq_restore(flags);
1066 static inline void mod_memcg_lruvec_state(struct lruvec *lruvec,
1067 enum node_stat_item idx, int val)
1069 unsigned long flags;
1071 local_irq_save(flags);
1072 __mod_memcg_lruvec_state(lruvec, idx, val);
1073 local_irq_restore(flags);
1076 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
1077 unsigned long count);
1079 static inline void count_memcg_events(struct mem_cgroup *memcg,
1080 enum vm_event_item idx,
1081 unsigned long count)
1083 unsigned long flags;
1085 local_irq_save(flags);
1086 __count_memcg_events(memcg, idx, count);
1087 local_irq_restore(flags);
1090 static inline void count_memcg_folio_events(struct folio *folio,
1091 enum vm_event_item idx, unsigned long nr)
1093 struct mem_cgroup *memcg = folio_memcg(folio);
1096 count_memcg_events(memcg, idx, nr);
1099 static inline void count_memcg_event_mm(struct mm_struct *mm,
1100 enum vm_event_item idx)
1102 struct mem_cgroup *memcg;
1104 if (mem_cgroup_disabled())
1108 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1110 count_memcg_events(memcg, idx, 1);
1114 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1115 enum memcg_memory_event event)
1117 bool swap_event = event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX ||
1118 event == MEMCG_SWAP_FAIL;
1120 atomic_long_inc(&memcg->memory_events_local[event]);
1122 cgroup_file_notify(&memcg->events_local_file);
1125 atomic_long_inc(&memcg->memory_events[event]);
1127 cgroup_file_notify(&memcg->swap_events_file);
1129 cgroup_file_notify(&memcg->events_file);
1131 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1133 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
1135 } while ((memcg = parent_mem_cgroup(memcg)) &&
1136 !mem_cgroup_is_root(memcg));
1139 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1140 enum memcg_memory_event event)
1142 struct mem_cgroup *memcg;
1144 if (mem_cgroup_disabled())
1148 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1150 memcg_memory_event(memcg, event);
1154 void split_page_memcg(struct page *head, unsigned int nr);
1156 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1158 unsigned long *total_scanned);
1160 #else /* CONFIG_MEMCG */
1162 #define MEM_CGROUP_ID_SHIFT 0
1164 static inline struct mem_cgroup *folio_memcg(struct folio *folio)
1169 static inline struct mem_cgroup *page_memcg(struct page *page)
1174 static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)
1176 WARN_ON_ONCE(!rcu_read_lock_held());
1180 static inline struct mem_cgroup *folio_memcg_check(struct folio *folio)
1185 static inline struct mem_cgroup *page_memcg_check(struct page *page)
1190 static inline bool folio_memcg_kmem(struct folio *folio)
1195 static inline bool PageMemcgKmem(struct page *page)
1200 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
1205 static inline bool mem_cgroup_disabled(void)
1210 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1211 enum memcg_memory_event event)
1215 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1216 enum memcg_memory_event event)
1220 static inline void mem_cgroup_protection(struct mem_cgroup *root,
1221 struct mem_cgroup *memcg,
1228 static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root,
1229 struct mem_cgroup *memcg)
1233 static inline bool mem_cgroup_unprotected(struct mem_cgroup *target,
1234 struct mem_cgroup *memcg)
1238 static inline bool mem_cgroup_below_low(struct mem_cgroup *target,
1239 struct mem_cgroup *memcg)
1244 static inline bool mem_cgroup_below_min(struct mem_cgroup *target,
1245 struct mem_cgroup *memcg)
1250 static inline void mem_cgroup_commit_charge(struct folio *folio,
1251 struct mem_cgroup *memcg)
1255 static inline int mem_cgroup_charge(struct folio *folio,
1256 struct mm_struct *mm, gfp_t gfp)
1261 static inline int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg,
1262 gfp_t gfp, long nr_pages)
1267 static inline int mem_cgroup_swapin_charge_folio(struct folio *folio,
1268 struct mm_struct *mm, gfp_t gfp, swp_entry_t entry)
1273 static inline void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry)
1277 static inline void mem_cgroup_uncharge(struct folio *folio)
1281 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
1285 static inline void mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
1286 unsigned int nr_pages)
1290 static inline void mem_cgroup_replace_folio(struct folio *old,
1295 static inline void mem_cgroup_migrate(struct folio *old, struct folio *new)
1299 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
1300 struct pglist_data *pgdat)
1302 return &pgdat->__lruvec;
1305 static inline struct lruvec *folio_lruvec(struct folio *folio)
1307 struct pglist_data *pgdat = folio_pgdat(folio);
1308 return &pgdat->__lruvec;
1312 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
1316 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
1321 static inline bool mm_match_cgroup(struct mm_struct *mm,
1322 struct mem_cgroup *memcg)
1327 static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
1332 static inline struct mem_cgroup *get_mem_cgroup_from_current(void)
1338 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css)
1343 static inline void obj_cgroup_put(struct obj_cgroup *objcg)
1347 static inline bool mem_cgroup_tryget(struct mem_cgroup *memcg)
1352 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
1356 static inline struct lruvec *folio_lruvec_lock(struct folio *folio)
1358 struct pglist_data *pgdat = folio_pgdat(folio);
1360 spin_lock(&pgdat->__lruvec.lru_lock);
1361 return &pgdat->__lruvec;
1364 static inline struct lruvec *folio_lruvec_lock_irq(struct folio *folio)
1366 struct pglist_data *pgdat = folio_pgdat(folio);
1368 spin_lock_irq(&pgdat->__lruvec.lru_lock);
1369 return &pgdat->__lruvec;
1372 static inline struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
1373 unsigned long *flagsp)
1375 struct pglist_data *pgdat = folio_pgdat(folio);
1377 spin_lock_irqsave(&pgdat->__lruvec.lru_lock, *flagsp);
1378 return &pgdat->__lruvec;
1381 static inline struct mem_cgroup *
1382 mem_cgroup_iter(struct mem_cgroup *root,
1383 struct mem_cgroup *prev,
1384 struct mem_cgroup_reclaim_cookie *reclaim)
1389 static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
1390 struct mem_cgroup *prev)
1394 static inline void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1395 int (*fn)(struct task_struct *, void *), void *arg)
1399 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
1404 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
1407 /* XXX: This should always return root_mem_cgroup */
1411 #ifdef CONFIG_SHRINKER_DEBUG
1412 static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg)
1417 static inline struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino)
1423 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
1428 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
1433 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
1439 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
1440 enum lru_list lru, int zone_idx)
1445 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
1450 static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1456 mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1461 mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
1465 static inline void folio_memcg_lock(struct folio *folio)
1469 static inline void folio_memcg_unlock(struct folio *folio)
1473 static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
1475 /* to match folio_memcg_rcu() */
1480 static inline void mem_cgroup_unlock_pages(void)
1485 static inline void mem_cgroup_handle_over_high(gfp_t gfp_mask)
1489 static inline void mem_cgroup_enter_user_fault(void)
1493 static inline void mem_cgroup_exit_user_fault(void)
1497 static inline bool task_in_memcg_oom(struct task_struct *p)
1502 static inline bool mem_cgroup_oom_synchronize(bool wait)
1507 static inline struct mem_cgroup *mem_cgroup_get_oom_group(
1508 struct task_struct *victim, struct mem_cgroup *oom_domain)
1513 static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
1517 static inline void __mod_memcg_state(struct mem_cgroup *memcg,
1523 static inline void mod_memcg_state(struct mem_cgroup *memcg,
1529 static inline void mod_memcg_page_state(struct page *page,
1534 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
1539 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1540 enum node_stat_item idx)
1542 return node_page_state(lruvec_pgdat(lruvec), idx);
1545 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1546 enum node_stat_item idx)
1548 return node_page_state(lruvec_pgdat(lruvec), idx);
1551 static inline void mem_cgroup_flush_stats(void)
1555 static inline void mem_cgroup_flush_stats_ratelimited(void)
1559 static inline void __mod_memcg_lruvec_state(struct lruvec *lruvec,
1560 enum node_stat_item idx, int val)
1564 static inline void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1567 struct page *page = virt_to_head_page(p);
1569 __mod_node_page_state(page_pgdat(page), idx, val);
1572 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1575 struct page *page = virt_to_head_page(p);
1577 mod_node_page_state(page_pgdat(page), idx, val);
1580 static inline void count_memcg_events(struct mem_cgroup *memcg,
1581 enum vm_event_item idx,
1582 unsigned long count)
1586 static inline void __count_memcg_events(struct mem_cgroup *memcg,
1587 enum vm_event_item idx,
1588 unsigned long count)
1592 static inline void count_memcg_folio_events(struct folio *folio,
1593 enum vm_event_item idx, unsigned long nr)
1598 void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
1602 static inline void split_page_memcg(struct page *head, unsigned int nr)
1607 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1609 unsigned long *total_scanned)
1613 #endif /* CONFIG_MEMCG */
1615 static inline void __inc_lruvec_kmem_state(void *p, enum node_stat_item idx)
1617 __mod_lruvec_kmem_state(p, idx, 1);
1620 static inline void __dec_lruvec_kmem_state(void *p, enum node_stat_item idx)
1622 __mod_lruvec_kmem_state(p, idx, -1);
1625 static inline struct lruvec *parent_lruvec(struct lruvec *lruvec)
1627 struct mem_cgroup *memcg;
1629 memcg = lruvec_memcg(lruvec);
1632 memcg = parent_mem_cgroup(memcg);
1635 return mem_cgroup_lruvec(memcg, lruvec_pgdat(lruvec));
1638 static inline void unlock_page_lruvec(struct lruvec *lruvec)
1640 spin_unlock(&lruvec->lru_lock);
1643 static inline void unlock_page_lruvec_irq(struct lruvec *lruvec)
1645 spin_unlock_irq(&lruvec->lru_lock);
1648 static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec,
1649 unsigned long flags)
1651 spin_unlock_irqrestore(&lruvec->lru_lock, flags);
1654 /* Test requires a stable page->memcg binding, see page_memcg() */
1655 static inline bool folio_matches_lruvec(struct folio *folio,
1656 struct lruvec *lruvec)
1658 return lruvec_pgdat(lruvec) == folio_pgdat(folio) &&
1659 lruvec_memcg(lruvec) == folio_memcg(folio);
1662 /* Don't lock again iff page's lruvec locked */
1663 static inline struct lruvec *folio_lruvec_relock_irq(struct folio *folio,
1664 struct lruvec *locked_lruvec)
1666 if (locked_lruvec) {
1667 if (folio_matches_lruvec(folio, locked_lruvec))
1668 return locked_lruvec;
1670 unlock_page_lruvec_irq(locked_lruvec);
1673 return folio_lruvec_lock_irq(folio);
1676 /* Don't lock again iff page's lruvec locked */
1677 static inline struct lruvec *folio_lruvec_relock_irqsave(struct folio *folio,
1678 struct lruvec *locked_lruvec, unsigned long *flags)
1680 if (locked_lruvec) {
1681 if (folio_matches_lruvec(folio, locked_lruvec))
1682 return locked_lruvec;
1684 unlock_page_lruvec_irqrestore(locked_lruvec, *flags);
1687 return folio_lruvec_lock_irqsave(folio, flags);
1690 #ifdef CONFIG_CGROUP_WRITEBACK
1692 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
1693 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1694 unsigned long *pheadroom, unsigned long *pdirty,
1695 unsigned long *pwriteback);
1697 void mem_cgroup_track_foreign_dirty_slowpath(struct folio *folio,
1698 struct bdi_writeback *wb);
1700 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio,
1701 struct bdi_writeback *wb)
1703 struct mem_cgroup *memcg;
1705 if (mem_cgroup_disabled())
1708 memcg = folio_memcg(folio);
1709 if (unlikely(memcg && &memcg->css != wb->memcg_css))
1710 mem_cgroup_track_foreign_dirty_slowpath(folio, wb);
1713 void mem_cgroup_flush_foreign(struct bdi_writeback *wb);
1715 #else /* CONFIG_CGROUP_WRITEBACK */
1717 static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1722 static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
1723 unsigned long *pfilepages,
1724 unsigned long *pheadroom,
1725 unsigned long *pdirty,
1726 unsigned long *pwriteback)
1730 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio,
1731 struct bdi_writeback *wb)
1735 static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
1739 #endif /* CONFIG_CGROUP_WRITEBACK */
1742 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
1744 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1746 extern struct static_key_false memcg_sockets_enabled_key;
1747 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
1748 void mem_cgroup_sk_alloc(struct sock *sk);
1749 void mem_cgroup_sk_free(struct sock *sk);
1750 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1752 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1753 return !!memcg->tcpmem_pressure;
1755 if (time_before(jiffies, READ_ONCE(memcg->socket_pressure)))
1757 } while ((memcg = parent_mem_cgroup(memcg)));
1761 int alloc_shrinker_info(struct mem_cgroup *memcg);
1762 void free_shrinker_info(struct mem_cgroup *memcg);
1763 void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id);
1764 void reparent_shrinker_deferred(struct mem_cgroup *memcg);
1766 #define mem_cgroup_sockets_enabled 0
1767 static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1768 static inline void mem_cgroup_sk_free(struct sock *sk) { };
1769 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1774 static inline void set_shrinker_bit(struct mem_cgroup *memcg,
1775 int nid, int shrinker_id)
1780 #ifdef CONFIG_MEMCG_KMEM
1781 bool mem_cgroup_kmem_disabled(void);
1782 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order);
1783 void __memcg_kmem_uncharge_page(struct page *page, int order);
1786 * The returned objcg pointer is safe to use without additional
1787 * protection within a scope. The scope is defined either by
1788 * the current task (similar to the "current" global variable)
1789 * or by set_active_memcg() pair.
1790 * Please, use obj_cgroup_get() to get a reference if the pointer
1791 * needs to be used outside of the local scope.
1793 struct obj_cgroup *current_obj_cgroup(void);
1794 struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio);
1796 static inline struct obj_cgroup *get_obj_cgroup_from_current(void)
1798 struct obj_cgroup *objcg = current_obj_cgroup();
1801 obj_cgroup_get(objcg);
1806 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size);
1807 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size);
1809 extern struct static_key_false memcg_bpf_enabled_key;
1810 static inline bool memcg_bpf_enabled(void)
1812 return static_branch_likely(&memcg_bpf_enabled_key);
1815 extern struct static_key_false memcg_kmem_online_key;
1817 static inline bool memcg_kmem_online(void)
1819 return static_branch_likely(&memcg_kmem_online_key);
1822 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1825 if (memcg_kmem_online())
1826 return __memcg_kmem_charge_page(page, gfp, order);
1830 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1832 if (memcg_kmem_online())
1833 __memcg_kmem_uncharge_page(page, order);
1837 * A helper for accessing memcg's kmem_id, used for getting
1838 * corresponding LRU lists.
1840 static inline int memcg_kmem_id(struct mem_cgroup *memcg)
1842 return memcg ? memcg->kmemcg_id : -1;
1845 struct mem_cgroup *mem_cgroup_from_obj(void *p);
1846 struct mem_cgroup *mem_cgroup_from_slab_obj(void *p);
1848 static inline void count_objcg_event(struct obj_cgroup *objcg,
1849 enum vm_event_item idx)
1851 struct mem_cgroup *memcg;
1853 if (!memcg_kmem_online())
1857 memcg = obj_cgroup_memcg(objcg);
1858 count_memcg_events(memcg, idx, 1);
1863 static inline bool mem_cgroup_kmem_disabled(void)
1868 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1874 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1878 static inline int __memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1884 static inline void __memcg_kmem_uncharge_page(struct page *page, int order)
1888 static inline struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio)
1893 static inline bool memcg_bpf_enabled(void)
1898 static inline bool memcg_kmem_online(void)
1903 static inline int memcg_kmem_id(struct mem_cgroup *memcg)
1908 static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)
1913 static inline struct mem_cgroup *mem_cgroup_from_slab_obj(void *p)
1918 static inline void count_objcg_event(struct obj_cgroup *objcg,
1919 enum vm_event_item idx)
1923 #endif /* CONFIG_MEMCG_KMEM */
1925 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
1926 bool obj_cgroup_may_zswap(struct obj_cgroup *objcg);
1927 void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size);
1928 void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size);
1930 static inline bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
1934 static inline void obj_cgroup_charge_zswap(struct obj_cgroup *objcg,
1938 static inline void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg,
1944 #endif /* _LINUX_MEMCONTROL_H */