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 {
72 struct memcg_vmstats_percpu;
74 struct lruvec_stats_percpu;
77 struct mem_cgroup_reclaim_iter {
78 struct mem_cgroup *position;
79 /* scan generation, increased every round-trip */
80 unsigned int generation;
84 * per-node information in memory controller.
86 struct mem_cgroup_per_node {
87 /* Keep the read-only fields at the start */
88 struct mem_cgroup *memcg; /* Back pointer, we cannot */
89 /* use container_of */
91 struct lruvec_stats_percpu __percpu *lruvec_stats_percpu;
92 struct lruvec_stats *lruvec_stats;
93 struct shrinker_info __rcu *shrinker_info;
95 #ifdef CONFIG_MEMCG_V1
97 * Memcg-v1 only stuff in middle as buffer between read mostly fields
98 * and update often fields to avoid false sharing. If v1 stuff is
99 * not present, an explicit padding is needed.
102 struct rb_node tree_node; /* RB tree node */
103 unsigned long usage_in_excess;/* Set to the value by which */
104 /* the soft limit is exceeded*/
107 CACHELINE_PADDING(_pad1_);
110 /* Fields which get updated often at the end. */
111 struct lruvec lruvec;
112 CACHELINE_PADDING(_pad2_);
113 unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
114 struct mem_cgroup_reclaim_iter iter;
117 struct mem_cgroup_threshold {
118 struct eventfd_ctx *eventfd;
119 unsigned long threshold;
123 struct mem_cgroup_threshold_ary {
124 /* An array index points to threshold just below or equal to usage. */
125 int current_threshold;
126 /* Size of entries[] */
128 /* Array of thresholds */
129 struct mem_cgroup_threshold entries[] __counted_by(size);
132 struct mem_cgroup_thresholds {
133 /* Primary thresholds array */
134 struct mem_cgroup_threshold_ary *primary;
136 * Spare threshold array.
137 * This is needed to make mem_cgroup_unregister_event() "never fail".
138 * It must be able to store at least primary->size - 1 entries.
140 struct mem_cgroup_threshold_ary *spare;
144 * Remember four most recent foreign writebacks with dirty pages in this
145 * cgroup. Inode sharing is expected to be uncommon and, even if we miss
146 * one in a given round, we're likely to catch it later if it keeps
147 * foreign-dirtying, so a fairly low count should be enough.
149 * See mem_cgroup_track_foreign_dirty_slowpath() for details.
151 #define MEMCG_CGWB_FRN_CNT 4
153 struct memcg_cgwb_frn {
154 u64 bdi_id; /* bdi->id of the foreign inode */
155 int memcg_id; /* memcg->css.id of foreign inode */
156 u64 at; /* jiffies_64 at the time of dirtying */
157 struct wb_completion done; /* tracks in-flight foreign writebacks */
161 * Bucket for arbitrarily byte-sized objects charged to a memory
162 * cgroup. The bucket can be reparented in one piece when the cgroup
163 * is destroyed, without having to round up the individual references
164 * of all live memory objects in the wild.
167 struct percpu_ref refcnt;
168 struct mem_cgroup *memcg;
169 atomic_t nr_charged_bytes;
171 struct list_head list; /* protected by objcg_lock */
177 * The memory controller data structure. The memory controller controls both
178 * page cache and RSS per cgroup. We would eventually like to provide
179 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
180 * to help the administrator determine what knobs to tune.
183 struct cgroup_subsys_state css;
185 /* Private memcg ID. Used to ID objects that outlive the cgroup */
186 struct mem_cgroup_id id;
188 /* Accounted resources */
189 struct page_counter memory; /* Both v1 & v2 */
192 struct page_counter swap; /* v2 only */
193 struct page_counter memsw; /* v1 only */
196 /* Range enforcement for interrupt charges */
197 struct work_struct high_work;
200 unsigned long zswap_max;
203 * Prevent pages from this memcg from being written back from zswap to
204 * swap, and from being swapped out on zswap store failures.
206 bool zswap_writeback;
209 /* vmpressure notifications */
210 struct vmpressure vmpressure;
213 * Should the OOM killer kill all belonging tasks, had it kill one?
219 /* memory.events and memory.events.local */
220 struct cgroup_file events_file;
221 struct cgroup_file events_local_file;
223 /* handle for "memory.swap.events" */
224 struct cgroup_file swap_events_file;
227 struct memcg_vmstats *vmstats;
230 atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
231 atomic_long_t memory_events_local[MEMCG_NR_MEMORY_EVENTS];
234 * Hint of reclaim pressure for socket memroy management. Note
235 * that this indicator should NOT be used in legacy cgroup mode
236 * where socket memory is accounted/charged separately.
238 unsigned long socket_pressure;
242 * memcg->objcg is wiped out as a part of the objcg repaprenting
243 * process. memcg->orig_objcg preserves a pointer (and a reference)
244 * to the original objcg until the end of live of memcg.
246 struct obj_cgroup __rcu *objcg;
247 struct obj_cgroup *orig_objcg;
248 /* list of inherited objcgs, protected by objcg_lock */
249 struct list_head objcg_list;
251 struct memcg_vmstats_percpu __percpu *vmstats_percpu;
253 #ifdef CONFIG_CGROUP_WRITEBACK
254 struct list_head cgwb_list;
255 struct wb_domain cgwb_domain;
256 struct memcg_cgwb_frn cgwb_frn[MEMCG_CGWB_FRN_CNT];
259 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
260 struct deferred_split deferred_split_queue;
263 #ifdef CONFIG_LRU_GEN_WALKS_MMU
264 /* per-memcg mm_struct list */
265 struct lru_gen_mm_list mm_list;
268 #ifdef CONFIG_MEMCG_V1
269 /* Legacy consumer-oriented counters */
270 struct page_counter kmem; /* v1 only */
271 struct page_counter tcpmem; /* v1 only */
273 unsigned long soft_limit;
275 /* protected by memcg_oom_lock */
279 /* OOM-Killer disable */
280 int oom_kill_disable;
282 /* protect arrays of thresholds */
283 struct mutex thresholds_lock;
285 /* thresholds for memory usage. RCU-protected */
286 struct mem_cgroup_thresholds thresholds;
288 /* thresholds for mem+swap usage. RCU-protected */
289 struct mem_cgroup_thresholds memsw_thresholds;
291 /* For oom notifier event fd */
292 struct list_head oom_notify;
295 * Should we move charges of a task when a task is moved into this
296 * mem_cgroup ? And what type of charges should we move ?
298 unsigned long move_charge_at_immigrate;
299 /* taken only while moving_account > 0 */
300 spinlock_t move_lock;
301 unsigned long move_lock_flags;
303 /* Legacy tcp memory accounting */
308 * set > 0 if pages under this cgroup are moving to other cgroup.
310 atomic_t moving_account;
311 struct task_struct *move_lock_task;
313 /* List of events which userspace want to receive */
314 struct list_head event_list;
315 spinlock_t event_list_lock;
316 #endif /* CONFIG_MEMCG_V1 */
318 struct mem_cgroup_per_node *nodeinfo[];
322 * size of first charge trial.
323 * TODO: maybe necessary to use big numbers in big irons or dynamic based of the
326 #define MEMCG_CHARGE_BATCH 64U
328 extern struct mem_cgroup *root_mem_cgroup;
330 enum page_memcg_data_flags {
331 /* page->memcg_data is a pointer to an slabobj_ext vector */
332 MEMCG_DATA_OBJEXTS = (1UL << 0),
333 /* page has been accounted as a non-slab kernel page */
334 MEMCG_DATA_KMEM = (1UL << 1),
335 /* the next bit after the last actual flag */
336 __NR_MEMCG_DATA_FLAGS = (1UL << 2),
339 #define __FIRST_OBJEXT_FLAG __NR_MEMCG_DATA_FLAGS
341 #else /* CONFIG_MEMCG */
343 #define __FIRST_OBJEXT_FLAG (1UL << 0)
345 #endif /* CONFIG_MEMCG */
348 /* slabobj_ext vector failed to allocate */
349 OBJEXTS_ALLOC_FAIL = __FIRST_OBJEXT_FLAG,
350 /* the next bit after the last actual flag */
351 __NR_OBJEXTS_FLAGS = (__FIRST_OBJEXT_FLAG << 1),
354 #define OBJEXTS_FLAGS_MASK (__NR_OBJEXTS_FLAGS - 1)
358 static inline bool folio_memcg_kmem(struct folio *folio);
361 * After the initialization objcg->memcg is always pointing at
362 * a valid memcg, but can be atomically swapped to the parent memcg.
364 * The caller must ensure that the returned memcg won't be released:
365 * e.g. acquire the rcu_read_lock or css_set_lock.
367 static inline struct mem_cgroup *obj_cgroup_memcg(struct obj_cgroup *objcg)
369 return READ_ONCE(objcg->memcg);
373 * __folio_memcg - Get the memory cgroup associated with a non-kmem folio
374 * @folio: Pointer to the folio.
376 * Returns a pointer to the memory cgroup associated with the folio,
377 * or NULL. This function assumes that the folio is known to have a
378 * proper memory cgroup pointer. It's not safe to call this function
379 * against some type of folios, e.g. slab folios or ex-slab folios or
382 static inline struct mem_cgroup *__folio_memcg(struct folio *folio)
384 unsigned long memcg_data = folio->memcg_data;
386 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio);
387 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_OBJEXTS, folio);
388 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_KMEM, folio);
390 return (struct mem_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK);
394 * __folio_objcg - get the object cgroup associated with a kmem folio.
395 * @folio: Pointer to the folio.
397 * Returns a pointer to the object cgroup associated with the folio,
398 * or NULL. This function assumes that the folio is known to have a
399 * proper object cgroup pointer. It's not safe to call this function
400 * against some type of folios, e.g. slab folios or ex-slab folios or
403 static inline struct obj_cgroup *__folio_objcg(struct folio *folio)
405 unsigned long memcg_data = folio->memcg_data;
407 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio);
408 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_OBJEXTS, folio);
409 VM_BUG_ON_FOLIO(!(memcg_data & MEMCG_DATA_KMEM), folio);
411 return (struct obj_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK);
415 * folio_memcg - Get the memory cgroup associated with a folio.
416 * @folio: Pointer to the folio.
418 * Returns a pointer to the memory cgroup associated with the folio,
419 * or NULL. This function assumes that the folio is known to have a
420 * proper memory cgroup pointer. It's not safe to call this function
421 * against some type of folios, e.g. slab folios or ex-slab folios.
423 * For a non-kmem folio any of the following ensures folio and memcg binding
428 * - folio_memcg_lock()
429 * - exclusive reference
430 * - mem_cgroup_trylock_pages()
432 * For a kmem folio a caller should hold an rcu read lock to protect memcg
433 * associated with a kmem folio from being released.
435 static inline struct mem_cgroup *folio_memcg(struct folio *folio)
437 if (folio_memcg_kmem(folio))
438 return obj_cgroup_memcg(__folio_objcg(folio));
439 return __folio_memcg(folio);
443 * folio_memcg_rcu - Locklessly get the memory cgroup associated with a folio.
444 * @folio: Pointer to the folio.
446 * This function assumes that the folio is known to have a
447 * proper memory cgroup pointer. It's not safe to call this function
448 * against some type of folios, e.g. slab folios or ex-slab folios.
450 * Return: A pointer to the memory cgroup associated with the folio,
453 static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)
455 unsigned long memcg_data = READ_ONCE(folio->memcg_data);
457 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio);
458 WARN_ON_ONCE(!rcu_read_lock_held());
460 if (memcg_data & MEMCG_DATA_KMEM) {
461 struct obj_cgroup *objcg;
463 objcg = (void *)(memcg_data & ~OBJEXTS_FLAGS_MASK);
464 return obj_cgroup_memcg(objcg);
467 return (struct mem_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK);
471 * folio_memcg_check - Get the memory cgroup associated with a folio.
472 * @folio: Pointer to the folio.
474 * Returns a pointer to the memory cgroup associated with the folio,
475 * or NULL. This function unlike folio_memcg() can take any folio
476 * as an argument. It has to be used in cases when it's not known if a folio
477 * has an associated memory cgroup pointer or an object cgroups vector or
480 * For a non-kmem folio any of the following ensures folio and memcg binding
485 * - lock_folio_memcg()
486 * - exclusive reference
487 * - mem_cgroup_trylock_pages()
489 * For a kmem folio a caller should hold an rcu read lock to protect memcg
490 * associated with a kmem folio from being released.
492 static inline struct mem_cgroup *folio_memcg_check(struct folio *folio)
495 * Because folio->memcg_data might be changed asynchronously
496 * for slabs, READ_ONCE() should be used here.
498 unsigned long memcg_data = READ_ONCE(folio->memcg_data);
500 if (memcg_data & MEMCG_DATA_OBJEXTS)
503 if (memcg_data & MEMCG_DATA_KMEM) {
504 struct obj_cgroup *objcg;
506 objcg = (void *)(memcg_data & ~OBJEXTS_FLAGS_MASK);
507 return obj_cgroup_memcg(objcg);
510 return (struct mem_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK);
513 static inline struct mem_cgroup *page_memcg_check(struct page *page)
517 return folio_memcg_check((struct folio *)page);
520 static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *objcg)
522 struct mem_cgroup *memcg;
526 memcg = obj_cgroup_memcg(objcg);
527 if (unlikely(!css_tryget(&memcg->css)))
535 * folio_memcg_kmem - Check if the folio has the memcg_kmem flag set.
536 * @folio: Pointer to the folio.
538 * Checks if the folio has MemcgKmem flag set. The caller must ensure
539 * that the folio has an associated memory cgroup. It's not safe to call
540 * this function against some types of folios, e.g. slab folios.
542 static inline bool folio_memcg_kmem(struct folio *folio)
544 VM_BUG_ON_PGFLAGS(PageTail(&folio->page), &folio->page);
545 VM_BUG_ON_FOLIO(folio->memcg_data & MEMCG_DATA_OBJEXTS, folio);
546 return folio->memcg_data & MEMCG_DATA_KMEM;
549 static inline bool PageMemcgKmem(struct page *page)
551 return folio_memcg_kmem(page_folio(page));
554 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
556 return (memcg == root_mem_cgroup);
559 static inline bool mem_cgroup_disabled(void)
561 return !cgroup_subsys_enabled(memory_cgrp_subsys);
564 static inline void mem_cgroup_protection(struct mem_cgroup *root,
565 struct mem_cgroup *memcg,
571 if (mem_cgroup_disabled())
575 * There is no reclaim protection applied to a targeted reclaim.
576 * We are special casing this specific case here because
577 * mem_cgroup_calculate_protection is not robust enough to keep
578 * the protection invariant for calculated effective values for
579 * parallel reclaimers with different reclaim target. This is
580 * especially a problem for tail memcgs (as they have pages on LRU)
581 * which would want to have effective values 0 for targeted reclaim
582 * but a different value for external reclaim.
585 * Let's have global and A's reclaim in parallel:
587 * A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G)
589 * | C (low = 1G, usage = 2.5G)
590 * B (low = 1G, usage = 0.5G)
592 * For the global reclaim
594 * B.elow = min(B.usage, B.low) because children_low_usage <= A.elow
595 * C.elow = min(C.usage, C.low)
597 * With the effective values resetting we have A reclaim
602 * If the global reclaim races with A's reclaim then
603 * B.elow = C.elow = 0 because children_low_usage > A.elow)
604 * is possible and reclaiming B would be violating the protection.
610 *min = READ_ONCE(memcg->memory.emin);
611 *low = READ_ONCE(memcg->memory.elow);
614 void mem_cgroup_calculate_protection(struct mem_cgroup *root,
615 struct mem_cgroup *memcg);
617 static inline bool mem_cgroup_unprotected(struct mem_cgroup *target,
618 struct mem_cgroup *memcg)
621 * The root memcg doesn't account charges, and doesn't support
622 * protection. The target memcg's protection is ignored, see
623 * mem_cgroup_calculate_protection() and mem_cgroup_protection()
625 return mem_cgroup_disabled() || mem_cgroup_is_root(memcg) ||
629 static inline bool mem_cgroup_below_low(struct mem_cgroup *target,
630 struct mem_cgroup *memcg)
632 if (mem_cgroup_unprotected(target, memcg))
635 return READ_ONCE(memcg->memory.elow) >=
636 page_counter_read(&memcg->memory);
639 static inline bool mem_cgroup_below_min(struct mem_cgroup *target,
640 struct mem_cgroup *memcg)
642 if (mem_cgroup_unprotected(target, memcg))
645 return READ_ONCE(memcg->memory.emin) >=
646 page_counter_read(&memcg->memory);
649 void mem_cgroup_commit_charge(struct folio *folio, struct mem_cgroup *memcg);
651 int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp);
654 * mem_cgroup_charge - Charge a newly allocated folio to a cgroup.
655 * @folio: Folio to charge.
656 * @mm: mm context of the allocating task.
657 * @gfp: Reclaim mode.
659 * Try to charge @folio to the memcg that @mm belongs to, reclaiming
660 * pages according to @gfp if necessary. If @mm is NULL, try to
661 * charge to the active memcg.
663 * Do not use this for folios allocated for swapin.
665 * Return: 0 on success. Otherwise, an error code is returned.
667 static inline int mem_cgroup_charge(struct folio *folio, struct mm_struct *mm,
670 if (mem_cgroup_disabled())
672 return __mem_cgroup_charge(folio, mm, gfp);
675 int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, gfp_t gfp,
678 int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm,
679 gfp_t gfp, swp_entry_t entry);
680 void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry);
682 void __mem_cgroup_uncharge(struct folio *folio);
685 * mem_cgroup_uncharge - Uncharge a folio.
686 * @folio: Folio to uncharge.
688 * Uncharge a folio previously charged with mem_cgroup_charge().
690 static inline void mem_cgroup_uncharge(struct folio *folio)
692 if (mem_cgroup_disabled())
694 __mem_cgroup_uncharge(folio);
697 void __mem_cgroup_uncharge_folios(struct folio_batch *folios);
698 static inline void mem_cgroup_uncharge_folios(struct folio_batch *folios)
700 if (mem_cgroup_disabled())
702 __mem_cgroup_uncharge_folios(folios);
705 void mem_cgroup_cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages);
706 void mem_cgroup_replace_folio(struct folio *old, struct folio *new);
707 void mem_cgroup_migrate(struct folio *old, struct folio *new);
710 * mem_cgroup_lruvec - get the lru list vector for a memcg & node
711 * @memcg: memcg of the wanted lruvec
712 * @pgdat: pglist_data
714 * Returns the lru list vector holding pages for a given @memcg &
715 * @pgdat combination. This can be the node lruvec, if the memory
716 * controller is disabled.
718 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
719 struct pglist_data *pgdat)
721 struct mem_cgroup_per_node *mz;
722 struct lruvec *lruvec;
724 if (mem_cgroup_disabled()) {
725 lruvec = &pgdat->__lruvec;
730 memcg = root_mem_cgroup;
732 mz = memcg->nodeinfo[pgdat->node_id];
733 lruvec = &mz->lruvec;
736 * Since a node can be onlined after the mem_cgroup was created,
737 * we have to be prepared to initialize lruvec->pgdat here;
738 * and if offlined then reonlined, we need to reinitialize it.
740 if (unlikely(lruvec->pgdat != pgdat))
741 lruvec->pgdat = pgdat;
746 * folio_lruvec - return lruvec for isolating/putting an LRU folio
747 * @folio: Pointer to the folio.
749 * This function relies on folio->mem_cgroup being stable.
751 static inline struct lruvec *folio_lruvec(struct folio *folio)
753 struct mem_cgroup *memcg = folio_memcg(folio);
755 VM_WARN_ON_ONCE_FOLIO(!memcg && !mem_cgroup_disabled(), folio);
756 return mem_cgroup_lruvec(memcg, folio_pgdat(folio));
759 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
761 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
763 struct mem_cgroup *get_mem_cgroup_from_current(void);
765 struct lruvec *folio_lruvec_lock(struct folio *folio);
766 struct lruvec *folio_lruvec_lock_irq(struct folio *folio);
767 struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
768 unsigned long *flags);
770 #ifdef CONFIG_DEBUG_VM
771 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio);
774 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
780 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
781 return css ? container_of(css, struct mem_cgroup, css) : NULL;
784 static inline bool obj_cgroup_tryget(struct obj_cgroup *objcg)
786 return percpu_ref_tryget(&objcg->refcnt);
789 static inline void obj_cgroup_get(struct obj_cgroup *objcg)
791 percpu_ref_get(&objcg->refcnt);
794 static inline void obj_cgroup_get_many(struct obj_cgroup *objcg,
797 percpu_ref_get_many(&objcg->refcnt, nr);
800 static inline void obj_cgroup_put(struct obj_cgroup *objcg)
803 percpu_ref_put(&objcg->refcnt);
806 static inline bool mem_cgroup_tryget(struct mem_cgroup *memcg)
808 return !memcg || css_tryget(&memcg->css);
811 static inline bool mem_cgroup_tryget_online(struct mem_cgroup *memcg)
813 return !memcg || css_tryget_online(&memcg->css);
816 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
819 css_put(&memcg->css);
822 #define mem_cgroup_from_counter(counter, member) \
823 container_of(counter, struct mem_cgroup, member)
825 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
827 struct mem_cgroup_reclaim_cookie *);
828 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
829 void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
830 int (*)(struct task_struct *, void *), void *arg);
832 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
834 if (mem_cgroup_disabled())
839 struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
841 #ifdef CONFIG_SHRINKER_DEBUG
842 static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg)
844 return memcg ? cgroup_ino(memcg->css.cgroup) : 0;
847 struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino);
850 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
852 return mem_cgroup_from_css(seq_css(m));
855 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
857 struct mem_cgroup_per_node *mz;
859 if (mem_cgroup_disabled())
862 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
867 * parent_mem_cgroup - find the accounting parent of a memcg
868 * @memcg: memcg whose parent to find
870 * Returns the parent memcg, or NULL if this is the root.
872 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
874 return mem_cgroup_from_css(memcg->css.parent);
877 static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
878 struct mem_cgroup *root)
882 return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
885 static inline bool mm_match_cgroup(struct mm_struct *mm,
886 struct mem_cgroup *memcg)
888 struct mem_cgroup *task_memcg;
892 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
894 match = mem_cgroup_is_descendant(task_memcg, memcg);
899 struct cgroup_subsys_state *mem_cgroup_css_from_folio(struct folio *folio);
900 ino_t page_cgroup_ino(struct page *page);
902 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
904 if (mem_cgroup_disabled())
906 return !!(memcg->css.flags & CSS_ONLINE);
909 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
910 int zid, int nr_pages);
913 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
914 enum lru_list lru, int zone_idx)
916 struct mem_cgroup_per_node *mz;
918 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
919 return READ_ONCE(mz->lru_zone_size[zone_idx][lru]);
922 void mem_cgroup_handle_over_high(gfp_t gfp_mask);
924 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
926 unsigned long mem_cgroup_size(struct mem_cgroup *memcg);
928 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
929 struct task_struct *p);
931 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg);
933 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
934 struct mem_cgroup *oom_domain);
935 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg);
937 void __mod_memcg_state(struct mem_cgroup *memcg, enum memcg_stat_item idx,
940 /* idx can be of type enum memcg_stat_item or node_stat_item */
941 static inline void mod_memcg_state(struct mem_cgroup *memcg,
942 enum memcg_stat_item idx, int val)
946 local_irq_save(flags);
947 __mod_memcg_state(memcg, idx, val);
948 local_irq_restore(flags);
951 static inline void mod_memcg_page_state(struct page *page,
952 enum memcg_stat_item idx, int val)
954 struct mem_cgroup *memcg;
956 if (mem_cgroup_disabled())
960 memcg = folio_memcg(page_folio(page));
962 mod_memcg_state(memcg, idx, val);
966 unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx);
967 unsigned long lruvec_page_state(struct lruvec *lruvec, enum node_stat_item idx);
968 unsigned long lruvec_page_state_local(struct lruvec *lruvec,
969 enum node_stat_item idx);
971 void mem_cgroup_flush_stats(struct mem_cgroup *memcg);
972 void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg);
974 void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val);
976 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
981 local_irq_save(flags);
982 __mod_lruvec_kmem_state(p, idx, val);
983 local_irq_restore(flags);
986 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
987 unsigned long count);
989 static inline void count_memcg_events(struct mem_cgroup *memcg,
990 enum vm_event_item idx,
995 local_irq_save(flags);
996 __count_memcg_events(memcg, idx, count);
997 local_irq_restore(flags);
1000 static inline void count_memcg_folio_events(struct folio *folio,
1001 enum vm_event_item idx, unsigned long nr)
1003 struct mem_cgroup *memcg = folio_memcg(folio);
1006 count_memcg_events(memcg, idx, nr);
1009 static inline void count_memcg_event_mm(struct mm_struct *mm,
1010 enum vm_event_item idx)
1012 struct mem_cgroup *memcg;
1014 if (mem_cgroup_disabled())
1018 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1020 count_memcg_events(memcg, idx, 1);
1024 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1025 enum memcg_memory_event event)
1027 bool swap_event = event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX ||
1028 event == MEMCG_SWAP_FAIL;
1030 atomic_long_inc(&memcg->memory_events_local[event]);
1032 cgroup_file_notify(&memcg->events_local_file);
1035 atomic_long_inc(&memcg->memory_events[event]);
1037 cgroup_file_notify(&memcg->swap_events_file);
1039 cgroup_file_notify(&memcg->events_file);
1041 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1043 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
1045 } while ((memcg = parent_mem_cgroup(memcg)) &&
1046 !mem_cgroup_is_root(memcg));
1049 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1050 enum memcg_memory_event event)
1052 struct mem_cgroup *memcg;
1054 if (mem_cgroup_disabled())
1058 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1060 memcg_memory_event(memcg, event);
1064 void split_page_memcg(struct page *head, int old_order, int new_order);
1066 #else /* CONFIG_MEMCG */
1068 #define MEM_CGROUP_ID_SHIFT 0
1070 static inline struct mem_cgroup *folio_memcg(struct folio *folio)
1075 static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)
1077 WARN_ON_ONCE(!rcu_read_lock_held());
1081 static inline struct mem_cgroup *folio_memcg_check(struct folio *folio)
1086 static inline struct mem_cgroup *page_memcg_check(struct page *page)
1091 static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *objcg)
1096 static inline bool folio_memcg_kmem(struct folio *folio)
1101 static inline bool PageMemcgKmem(struct page *page)
1106 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
1111 static inline bool mem_cgroup_disabled(void)
1116 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1117 enum memcg_memory_event event)
1121 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1122 enum memcg_memory_event event)
1126 static inline void mem_cgroup_protection(struct mem_cgroup *root,
1127 struct mem_cgroup *memcg,
1134 static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root,
1135 struct mem_cgroup *memcg)
1139 static inline bool mem_cgroup_unprotected(struct mem_cgroup *target,
1140 struct mem_cgroup *memcg)
1144 static inline bool mem_cgroup_below_low(struct mem_cgroup *target,
1145 struct mem_cgroup *memcg)
1150 static inline bool mem_cgroup_below_min(struct mem_cgroup *target,
1151 struct mem_cgroup *memcg)
1156 static inline void mem_cgroup_commit_charge(struct folio *folio,
1157 struct mem_cgroup *memcg)
1161 static inline int mem_cgroup_charge(struct folio *folio,
1162 struct mm_struct *mm, gfp_t gfp)
1167 static inline int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg,
1168 gfp_t gfp, long nr_pages)
1173 static inline int mem_cgroup_swapin_charge_folio(struct folio *folio,
1174 struct mm_struct *mm, gfp_t gfp, swp_entry_t entry)
1179 static inline void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry)
1183 static inline void mem_cgroup_uncharge(struct folio *folio)
1187 static inline void mem_cgroup_uncharge_folios(struct folio_batch *folios)
1191 static inline void mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
1192 unsigned int nr_pages)
1196 static inline void mem_cgroup_replace_folio(struct folio *old,
1201 static inline void mem_cgroup_migrate(struct folio *old, struct folio *new)
1205 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
1206 struct pglist_data *pgdat)
1208 return &pgdat->__lruvec;
1211 static inline struct lruvec *folio_lruvec(struct folio *folio)
1213 struct pglist_data *pgdat = folio_pgdat(folio);
1214 return &pgdat->__lruvec;
1218 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
1222 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
1227 static inline bool mm_match_cgroup(struct mm_struct *mm,
1228 struct mem_cgroup *memcg)
1233 static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
1238 static inline struct mem_cgroup *get_mem_cgroup_from_current(void)
1244 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css)
1249 static inline void obj_cgroup_put(struct obj_cgroup *objcg)
1253 static inline bool mem_cgroup_tryget(struct mem_cgroup *memcg)
1258 static inline bool mem_cgroup_tryget_online(struct mem_cgroup *memcg)
1263 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
1267 static inline struct lruvec *folio_lruvec_lock(struct folio *folio)
1269 struct pglist_data *pgdat = folio_pgdat(folio);
1271 spin_lock(&pgdat->__lruvec.lru_lock);
1272 return &pgdat->__lruvec;
1275 static inline struct lruvec *folio_lruvec_lock_irq(struct folio *folio)
1277 struct pglist_data *pgdat = folio_pgdat(folio);
1279 spin_lock_irq(&pgdat->__lruvec.lru_lock);
1280 return &pgdat->__lruvec;
1283 static inline struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
1284 unsigned long *flagsp)
1286 struct pglist_data *pgdat = folio_pgdat(folio);
1288 spin_lock_irqsave(&pgdat->__lruvec.lru_lock, *flagsp);
1289 return &pgdat->__lruvec;
1292 static inline struct mem_cgroup *
1293 mem_cgroup_iter(struct mem_cgroup *root,
1294 struct mem_cgroup *prev,
1295 struct mem_cgroup_reclaim_cookie *reclaim)
1300 static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
1301 struct mem_cgroup *prev)
1305 static inline void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1306 int (*fn)(struct task_struct *, void *), void *arg)
1310 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
1315 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
1318 /* XXX: This should always return root_mem_cgroup */
1322 #ifdef CONFIG_SHRINKER_DEBUG
1323 static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg)
1328 static inline struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino)
1334 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
1339 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
1344 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
1350 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
1351 enum lru_list lru, int zone_idx)
1356 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
1361 static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1367 mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1372 mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
1376 static inline void mem_cgroup_handle_over_high(gfp_t gfp_mask)
1380 static inline struct mem_cgroup *mem_cgroup_get_oom_group(
1381 struct task_struct *victim, struct mem_cgroup *oom_domain)
1386 static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
1390 static inline void __mod_memcg_state(struct mem_cgroup *memcg,
1391 enum memcg_stat_item idx,
1396 static inline void mod_memcg_state(struct mem_cgroup *memcg,
1397 enum memcg_stat_item idx,
1402 static inline void mod_memcg_page_state(struct page *page,
1403 enum memcg_stat_item idx, int val)
1407 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
1412 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1413 enum node_stat_item idx)
1415 return node_page_state(lruvec_pgdat(lruvec), idx);
1418 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1419 enum node_stat_item idx)
1421 return node_page_state(lruvec_pgdat(lruvec), idx);
1424 static inline void mem_cgroup_flush_stats(struct mem_cgroup *memcg)
1428 static inline void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg)
1432 static inline void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1435 struct page *page = virt_to_head_page(p);
1437 __mod_node_page_state(page_pgdat(page), idx, val);
1440 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1443 struct page *page = virt_to_head_page(p);
1445 mod_node_page_state(page_pgdat(page), idx, val);
1448 static inline void count_memcg_events(struct mem_cgroup *memcg,
1449 enum vm_event_item idx,
1450 unsigned long count)
1454 static inline void __count_memcg_events(struct mem_cgroup *memcg,
1455 enum vm_event_item idx,
1456 unsigned long count)
1460 static inline void count_memcg_folio_events(struct folio *folio,
1461 enum vm_event_item idx, unsigned long nr)
1466 void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
1470 static inline void split_page_memcg(struct page *head, int old_order, int new_order)
1473 #endif /* CONFIG_MEMCG */
1476 * Extended information for slab objects stored as an array in page->memcg_data
1477 * if MEMCG_DATA_OBJEXTS is set.
1479 struct slabobj_ext {
1481 struct obj_cgroup *objcg;
1483 #ifdef CONFIG_MEM_ALLOC_PROFILING
1484 union codetag_ref ref;
1488 static inline void __inc_lruvec_kmem_state(void *p, enum node_stat_item idx)
1490 __mod_lruvec_kmem_state(p, idx, 1);
1493 static inline void __dec_lruvec_kmem_state(void *p, enum node_stat_item idx)
1495 __mod_lruvec_kmem_state(p, idx, -1);
1498 static inline struct lruvec *parent_lruvec(struct lruvec *lruvec)
1500 struct mem_cgroup *memcg;
1502 memcg = lruvec_memcg(lruvec);
1505 memcg = parent_mem_cgroup(memcg);
1508 return mem_cgroup_lruvec(memcg, lruvec_pgdat(lruvec));
1511 static inline void unlock_page_lruvec(struct lruvec *lruvec)
1513 spin_unlock(&lruvec->lru_lock);
1516 static inline void unlock_page_lruvec_irq(struct lruvec *lruvec)
1518 spin_unlock_irq(&lruvec->lru_lock);
1521 static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec,
1522 unsigned long flags)
1524 spin_unlock_irqrestore(&lruvec->lru_lock, flags);
1527 /* Test requires a stable folio->memcg binding, see folio_memcg() */
1528 static inline bool folio_matches_lruvec(struct folio *folio,
1529 struct lruvec *lruvec)
1531 return lruvec_pgdat(lruvec) == folio_pgdat(folio) &&
1532 lruvec_memcg(lruvec) == folio_memcg(folio);
1535 /* Don't lock again iff page's lruvec locked */
1536 static inline struct lruvec *folio_lruvec_relock_irq(struct folio *folio,
1537 struct lruvec *locked_lruvec)
1539 if (locked_lruvec) {
1540 if (folio_matches_lruvec(folio, locked_lruvec))
1541 return locked_lruvec;
1543 unlock_page_lruvec_irq(locked_lruvec);
1546 return folio_lruvec_lock_irq(folio);
1549 /* Don't lock again iff folio's lruvec locked */
1550 static inline void folio_lruvec_relock_irqsave(struct folio *folio,
1551 struct lruvec **lruvecp, unsigned long *flags)
1554 if (folio_matches_lruvec(folio, *lruvecp))
1557 unlock_page_lruvec_irqrestore(*lruvecp, *flags);
1560 *lruvecp = folio_lruvec_lock_irqsave(folio, flags);
1563 #ifdef CONFIG_CGROUP_WRITEBACK
1565 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
1566 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1567 unsigned long *pheadroom, unsigned long *pdirty,
1568 unsigned long *pwriteback);
1570 void mem_cgroup_track_foreign_dirty_slowpath(struct folio *folio,
1571 struct bdi_writeback *wb);
1573 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio,
1574 struct bdi_writeback *wb)
1576 struct mem_cgroup *memcg;
1578 if (mem_cgroup_disabled())
1581 memcg = folio_memcg(folio);
1582 if (unlikely(memcg && &memcg->css != wb->memcg_css))
1583 mem_cgroup_track_foreign_dirty_slowpath(folio, wb);
1586 void mem_cgroup_flush_foreign(struct bdi_writeback *wb);
1588 #else /* CONFIG_CGROUP_WRITEBACK */
1590 static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1595 static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
1596 unsigned long *pfilepages,
1597 unsigned long *pheadroom,
1598 unsigned long *pdirty,
1599 unsigned long *pwriteback)
1603 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio,
1604 struct bdi_writeback *wb)
1608 static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
1612 #endif /* CONFIG_CGROUP_WRITEBACK */
1615 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
1617 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1619 extern struct static_key_false memcg_sockets_enabled_key;
1620 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
1621 void mem_cgroup_sk_alloc(struct sock *sk);
1622 void mem_cgroup_sk_free(struct sock *sk);
1623 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1625 #ifdef CONFIG_MEMCG_V1
1626 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1627 return !!memcg->tcpmem_pressure;
1628 #endif /* CONFIG_MEMCG_V1 */
1630 if (time_before(jiffies, READ_ONCE(memcg->socket_pressure)))
1632 } while ((memcg = parent_mem_cgroup(memcg)));
1636 int alloc_shrinker_info(struct mem_cgroup *memcg);
1637 void free_shrinker_info(struct mem_cgroup *memcg);
1638 void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id);
1639 void reparent_shrinker_deferred(struct mem_cgroup *memcg);
1641 #define mem_cgroup_sockets_enabled 0
1642 static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1643 static inline void mem_cgroup_sk_free(struct sock *sk) { };
1644 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1649 static inline void set_shrinker_bit(struct mem_cgroup *memcg,
1650 int nid, int shrinker_id)
1656 bool mem_cgroup_kmem_disabled(void);
1657 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order);
1658 void __memcg_kmem_uncharge_page(struct page *page, int order);
1661 * The returned objcg pointer is safe to use without additional
1662 * protection within a scope. The scope is defined either by
1663 * the current task (similar to the "current" global variable)
1664 * or by set_active_memcg() pair.
1665 * Please, use obj_cgroup_get() to get a reference if the pointer
1666 * needs to be used outside of the local scope.
1668 struct obj_cgroup *current_obj_cgroup(void);
1669 struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio);
1671 static inline struct obj_cgroup *get_obj_cgroup_from_current(void)
1673 struct obj_cgroup *objcg = current_obj_cgroup();
1676 obj_cgroup_get(objcg);
1681 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size);
1682 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size);
1684 extern struct static_key_false memcg_bpf_enabled_key;
1685 static inline bool memcg_bpf_enabled(void)
1687 return static_branch_likely(&memcg_bpf_enabled_key);
1690 extern struct static_key_false memcg_kmem_online_key;
1692 static inline bool memcg_kmem_online(void)
1694 return static_branch_likely(&memcg_kmem_online_key);
1697 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1700 if (memcg_kmem_online())
1701 return __memcg_kmem_charge_page(page, gfp, order);
1705 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1707 if (memcg_kmem_online())
1708 __memcg_kmem_uncharge_page(page, order);
1712 * A helper for accessing memcg's kmem_id, used for getting
1713 * corresponding LRU lists.
1715 static inline int memcg_kmem_id(struct mem_cgroup *memcg)
1717 return memcg ? memcg->kmemcg_id : -1;
1720 struct mem_cgroup *mem_cgroup_from_obj(void *p);
1721 struct mem_cgroup *mem_cgroup_from_slab_obj(void *p);
1723 static inline void count_objcg_event(struct obj_cgroup *objcg,
1724 enum vm_event_item idx)
1726 struct mem_cgroup *memcg;
1728 if (!memcg_kmem_online())
1732 memcg = obj_cgroup_memcg(objcg);
1733 count_memcg_events(memcg, idx, 1);
1738 static inline bool mem_cgroup_kmem_disabled(void)
1743 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1749 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1753 static inline int __memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1759 static inline void __memcg_kmem_uncharge_page(struct page *page, int order)
1763 static inline struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio)
1768 static inline bool memcg_bpf_enabled(void)
1773 static inline bool memcg_kmem_online(void)
1778 static inline int memcg_kmem_id(struct mem_cgroup *memcg)
1783 static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)
1788 static inline struct mem_cgroup *mem_cgroup_from_slab_obj(void *p)
1793 static inline void count_objcg_event(struct obj_cgroup *objcg,
1794 enum vm_event_item idx)
1798 #endif /* CONFIG_MEMCG */
1800 #if defined(CONFIG_MEMCG) && defined(CONFIG_ZSWAP)
1801 bool obj_cgroup_may_zswap(struct obj_cgroup *objcg);
1802 void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size);
1803 void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size);
1804 bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg);
1806 static inline bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
1810 static inline void obj_cgroup_charge_zswap(struct obj_cgroup *objcg,
1814 static inline void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg,
1818 static inline bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg)
1820 /* if zswap is disabled, do not block pages going to the swapping device */
1826 /* Cgroup v1-related declarations */
1828 #ifdef CONFIG_MEMCG_V1
1829 unsigned long memcg1_soft_limit_reclaim(pg_data_t *pgdat, int order,
1831 unsigned long *total_scanned);
1833 bool mem_cgroup_oom_synchronize(bool wait);
1835 static inline bool task_in_memcg_oom(struct task_struct *p)
1837 return p->memcg_in_oom;
1840 void folio_memcg_lock(struct folio *folio);
1841 void folio_memcg_unlock(struct folio *folio);
1843 /* try to stablize folio_memcg() for all the pages in a memcg */
1844 static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
1848 if (mem_cgroup_disabled() || !atomic_read(&memcg->moving_account))
1855 static inline void mem_cgroup_unlock_pages(void)
1860 static inline void mem_cgroup_enter_user_fault(void)
1862 WARN_ON(current->in_user_fault);
1863 current->in_user_fault = 1;
1866 static inline void mem_cgroup_exit_user_fault(void)
1868 WARN_ON(!current->in_user_fault);
1869 current->in_user_fault = 0;
1872 #else /* CONFIG_MEMCG_V1 */
1874 unsigned long memcg1_soft_limit_reclaim(pg_data_t *pgdat, int order,
1876 unsigned long *total_scanned)
1881 static inline void folio_memcg_lock(struct folio *folio)
1885 static inline void folio_memcg_unlock(struct folio *folio)
1889 static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
1891 /* to match folio_memcg_rcu() */
1896 static inline void mem_cgroup_unlock_pages(void)
1901 static inline bool task_in_memcg_oom(struct task_struct *p)
1906 static inline bool mem_cgroup_oom_synchronize(bool wait)
1911 static inline void mem_cgroup_enter_user_fault(void)
1915 static inline void mem_cgroup_exit_user_fault(void)
1919 #endif /* CONFIG_MEMCG_V1 */
1921 #endif /* _LINUX_MEMCONTROL_H */