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>
31 /* Cgroup-specific page state, on top of universal node page state */
32 enum memcg_stat_item {
33 MEMCG_SWAP = NR_VM_NODE_STAT_ITEMS,
39 enum memcg_memory_event {
48 MEMCG_NR_MEMORY_EVENTS,
51 struct mem_cgroup_reclaim_cookie {
53 unsigned int generation;
58 #define MEM_CGROUP_ID_SHIFT 16
59 #define MEM_CGROUP_ID_MAX USHRT_MAX
61 struct mem_cgroup_id {
67 * Per memcg event counter is incremented at every pagein/pageout. With THP,
68 * it will be incremented by the number of pages. This counter is used
69 * to trigger some periodic events. This is straightforward and better
70 * than using jiffies etc. to handle periodic memcg event.
72 enum mem_cgroup_events_target {
73 MEM_CGROUP_TARGET_THRESH,
74 MEM_CGROUP_TARGET_SOFTLIMIT,
78 struct memcg_vmstats_percpu {
79 long stat[MEMCG_NR_STAT];
80 unsigned long events[NR_VM_EVENT_ITEMS];
81 unsigned long nr_page_events;
82 unsigned long targets[MEM_CGROUP_NTARGETS];
85 struct mem_cgroup_reclaim_iter {
86 struct mem_cgroup *position;
87 /* scan generation, increased every round-trip */
88 unsigned int generation;
92 long count[NR_VM_NODE_STAT_ITEMS];
96 * Bitmap of shrinker::id corresponding to memcg-aware shrinkers,
97 * which have elements charged to this memcg.
99 struct memcg_shrinker_map {
105 * per-node information in memory controller.
107 struct mem_cgroup_per_node {
108 struct lruvec lruvec;
110 /* Legacy local VM stats */
111 struct lruvec_stat __percpu *lruvec_stat_local;
113 /* Subtree VM stats (batched updates) */
114 struct lruvec_stat __percpu *lruvec_stat_cpu;
115 atomic_long_t lruvec_stat[NR_VM_NODE_STAT_ITEMS];
117 unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
119 struct mem_cgroup_reclaim_iter iter;
121 struct memcg_shrinker_map __rcu *shrinker_map;
123 struct rb_node tree_node; /* RB tree node */
124 unsigned long usage_in_excess;/* Set to the value by which */
125 /* the soft limit is exceeded*/
127 struct mem_cgroup *memcg; /* Back pointer, we cannot */
128 /* use container_of */
131 struct mem_cgroup_threshold {
132 struct eventfd_ctx *eventfd;
133 unsigned long threshold;
137 struct mem_cgroup_threshold_ary {
138 /* An array index points to threshold just below or equal to usage. */
139 int current_threshold;
140 /* Size of entries[] */
142 /* Array of thresholds */
143 struct mem_cgroup_threshold entries[];
146 struct mem_cgroup_thresholds {
147 /* Primary thresholds array */
148 struct mem_cgroup_threshold_ary *primary;
150 * Spare threshold array.
151 * This is needed to make mem_cgroup_unregister_event() "never fail".
152 * It must be able to store at least primary->size - 1 entries.
154 struct mem_cgroup_threshold_ary *spare;
157 enum memcg_kmem_state {
163 #if defined(CONFIG_SMP)
164 struct memcg_padding {
166 } ____cacheline_internodealigned_in_smp;
167 #define MEMCG_PADDING(name) struct memcg_padding name;
169 #define MEMCG_PADDING(name)
173 * Remember four most recent foreign writebacks with dirty pages in this
174 * cgroup. Inode sharing is expected to be uncommon and, even if we miss
175 * one in a given round, we're likely to catch it later if it keeps
176 * foreign-dirtying, so a fairly low count should be enough.
178 * See mem_cgroup_track_foreign_dirty_slowpath() for details.
180 #define MEMCG_CGWB_FRN_CNT 4
182 struct memcg_cgwb_frn {
183 u64 bdi_id; /* bdi->id of the foreign inode */
184 int memcg_id; /* memcg->css.id of foreign inode */
185 u64 at; /* jiffies_64 at the time of dirtying */
186 struct wb_completion done; /* tracks in-flight foreign writebacks */
190 * Bucket for arbitrarily byte-sized objects charged to a memory
191 * cgroup. The bucket can be reparented in one piece when the cgroup
192 * is destroyed, without having to round up the individual references
193 * of all live memory objects in the wild.
196 struct percpu_ref refcnt;
197 struct mem_cgroup *memcg;
198 atomic_t nr_charged_bytes;
200 struct list_head list;
206 * The memory controller data structure. The memory controller controls both
207 * page cache and RSS per cgroup. We would eventually like to provide
208 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
209 * to help the administrator determine what knobs to tune.
212 struct cgroup_subsys_state css;
214 /* Private memcg ID. Used to ID objects that outlive the cgroup */
215 struct mem_cgroup_id id;
217 /* Accounted resources */
218 struct page_counter memory; /* Both v1 & v2 */
221 struct page_counter swap; /* v2 only */
222 struct page_counter memsw; /* v1 only */
225 /* Legacy consumer-oriented counters */
226 struct page_counter kmem; /* v1 only */
227 struct page_counter tcpmem; /* v1 only */
229 /* Range enforcement for interrupt charges */
230 struct work_struct high_work;
232 unsigned long soft_limit;
234 /* vmpressure notifications */
235 struct vmpressure vmpressure;
238 * Should the OOM killer kill all belonging tasks, had it kill one?
242 /* protected by memcg_oom_lock */
247 /* OOM-Killer disable */
248 int oom_kill_disable;
250 /* memory.events and memory.events.local */
251 struct cgroup_file events_file;
252 struct cgroup_file events_local_file;
254 /* handle for "memory.swap.events" */
255 struct cgroup_file swap_events_file;
257 /* protect arrays of thresholds */
258 struct mutex thresholds_lock;
260 /* thresholds for memory usage. RCU-protected */
261 struct mem_cgroup_thresholds thresholds;
263 /* thresholds for mem+swap usage. RCU-protected */
264 struct mem_cgroup_thresholds memsw_thresholds;
266 /* For oom notifier event fd */
267 struct list_head oom_notify;
270 * Should we move charges of a task when a task is moved into this
271 * mem_cgroup ? And what type of charges should we move ?
273 unsigned long move_charge_at_immigrate;
274 /* taken only while moving_account > 0 */
275 spinlock_t move_lock;
276 unsigned long move_lock_flags;
278 MEMCG_PADDING(_pad1_);
280 atomic_long_t vmstats[MEMCG_NR_STAT];
281 atomic_long_t vmevents[NR_VM_EVENT_ITEMS];
284 atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
285 atomic_long_t memory_events_local[MEMCG_NR_MEMORY_EVENTS];
287 unsigned long socket_pressure;
289 /* Legacy tcp memory accounting */
293 #ifdef CONFIG_MEMCG_KMEM
295 enum memcg_kmem_state kmem_state;
296 struct obj_cgroup __rcu *objcg;
297 struct list_head objcg_list; /* list of inherited objcgs */
300 MEMCG_PADDING(_pad2_);
303 * set > 0 if pages under this cgroup are moving to other cgroup.
305 atomic_t moving_account;
306 struct task_struct *move_lock_task;
308 /* Legacy local VM stats and events */
309 struct memcg_vmstats_percpu __percpu *vmstats_local;
311 /* Subtree VM stats and events (batched updates) */
312 struct memcg_vmstats_percpu __percpu *vmstats_percpu;
314 #ifdef CONFIG_CGROUP_WRITEBACK
315 struct list_head cgwb_list;
316 struct wb_domain cgwb_domain;
317 struct memcg_cgwb_frn cgwb_frn[MEMCG_CGWB_FRN_CNT];
320 /* List of events which userspace want to receive */
321 struct list_head event_list;
322 spinlock_t event_list_lock;
324 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
325 struct deferred_split deferred_split_queue;
328 struct mem_cgroup_per_node *nodeinfo[0];
329 /* WARNING: nodeinfo must be the last member here */
333 * size of first charge trial. "32" comes from vmscan.c's magic value.
334 * TODO: maybe necessary to use big numbers in big irons.
336 #define MEMCG_CHARGE_BATCH 32U
338 extern struct mem_cgroup *root_mem_cgroup;
340 enum page_memcg_data_flags {
341 /* page->memcg_data is a pointer to an objcgs vector */
342 MEMCG_DATA_OBJCGS = (1UL << 0),
343 /* page has been accounted as a non-slab kernel page */
344 MEMCG_DATA_KMEM = (1UL << 1),
345 /* the next bit after the last actual flag */
346 __NR_MEMCG_DATA_FLAGS = (1UL << 2),
349 #define MEMCG_DATA_FLAGS_MASK (__NR_MEMCG_DATA_FLAGS - 1)
352 * page_memcg - get the memory cgroup associated with a page
353 * @page: a pointer to the page struct
355 * Returns a pointer to the memory cgroup associated with the page,
356 * or NULL. This function assumes that the page is known to have a
357 * proper memory cgroup pointer. It's not safe to call this function
358 * against some type of pages, e.g. slab pages or ex-slab pages.
360 * Any of the following ensures page and memcg binding stability:
363 * - lock_page_memcg()
364 * - exclusive reference
366 static inline struct mem_cgroup *page_memcg(struct page *page)
368 unsigned long memcg_data = page->memcg_data;
370 VM_BUG_ON_PAGE(PageSlab(page), page);
371 VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_OBJCGS, page);
373 return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
377 * page_memcg_rcu - locklessly get the memory cgroup associated with a page
378 * @page: a pointer to the page struct
380 * Returns a pointer to the memory cgroup associated with the page,
381 * or NULL. This function assumes that the page is known to have a
382 * proper memory cgroup pointer. It's not safe to call this function
383 * against some type of pages, e.g. slab pages or ex-slab pages.
385 static inline struct mem_cgroup *page_memcg_rcu(struct page *page)
387 VM_BUG_ON_PAGE(PageSlab(page), page);
388 WARN_ON_ONCE(!rcu_read_lock_held());
390 return (struct mem_cgroup *)(READ_ONCE(page->memcg_data) &
391 ~MEMCG_DATA_FLAGS_MASK);
395 * page_memcg_check - get the memory cgroup associated with a page
396 * @page: a pointer to the page struct
398 * Returns a pointer to the memory cgroup associated with the page,
399 * or NULL. This function unlike page_memcg() can take any page
400 * as an argument. It has to be used in cases when it's not known if a page
401 * has an associated memory cgroup pointer or an object cgroups vector.
403 * Any of the following ensures page and memcg binding stability:
406 * - lock_page_memcg()
407 * - exclusive reference
409 static inline struct mem_cgroup *page_memcg_check(struct page *page)
412 * Because page->memcg_data might be changed asynchronously
413 * for slab pages, READ_ONCE() should be used here.
415 unsigned long memcg_data = READ_ONCE(page->memcg_data);
417 if (memcg_data & MEMCG_DATA_OBJCGS)
420 return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
424 * PageMemcgKmem - check if the page has MemcgKmem flag set
425 * @page: a pointer to the page struct
427 * Checks if the page has MemcgKmem flag set. The caller must ensure that
428 * the page has an associated memory cgroup. It's not safe to call this function
429 * against some types of pages, e.g. slab pages.
431 static inline bool PageMemcgKmem(struct page *page)
433 VM_BUG_ON_PAGE(page->memcg_data & MEMCG_DATA_OBJCGS, page);
434 return page->memcg_data & MEMCG_DATA_KMEM;
437 #ifdef CONFIG_MEMCG_KMEM
439 * page_objcgs - get the object cgroups vector associated with a page
440 * @page: a pointer to the page struct
442 * Returns a pointer to the object cgroups vector associated with the page,
443 * or NULL. This function assumes that the page is known to have an
444 * associated object cgroups vector. It's not safe to call this function
445 * against pages, which might have an associated memory cgroup: e.g.
446 * kernel stack pages.
448 static inline struct obj_cgroup **page_objcgs(struct page *page)
450 unsigned long memcg_data = READ_ONCE(page->memcg_data);
452 VM_BUG_ON_PAGE(memcg_data && !(memcg_data & MEMCG_DATA_OBJCGS), page);
453 VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, page);
455 return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
459 * page_objcgs_check - get the object cgroups vector associated with a page
460 * @page: a pointer to the page struct
462 * Returns a pointer to the object cgroups vector associated with the page,
463 * or NULL. This function is safe to use if the page can be directly associated
464 * with a memory cgroup.
466 static inline struct obj_cgroup **page_objcgs_check(struct page *page)
468 unsigned long memcg_data = READ_ONCE(page->memcg_data);
470 if (!memcg_data || !(memcg_data & MEMCG_DATA_OBJCGS))
473 VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, page);
475 return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
479 * set_page_objcgs - associate a page with a object cgroups vector
480 * @page: a pointer to the page struct
481 * @objcgs: a pointer to the object cgroups vector
483 * Atomically associates a page with a vector of object cgroups.
485 static inline bool set_page_objcgs(struct page *page,
486 struct obj_cgroup **objcgs)
488 return !cmpxchg(&page->memcg_data, 0, (unsigned long)objcgs |
492 static inline struct obj_cgroup **page_objcgs(struct page *page)
497 static inline struct obj_cgroup **page_objcgs_check(struct page *page)
502 static inline bool set_page_objcgs(struct page *page,
503 struct obj_cgroup **objcgs)
509 static __always_inline bool memcg_stat_item_in_bytes(int idx)
511 if (idx == MEMCG_PERCPU_B)
513 return vmstat_item_in_bytes(idx);
516 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
518 return (memcg == root_mem_cgroup);
521 static inline bool mem_cgroup_disabled(void)
523 return !cgroup_subsys_enabled(memory_cgrp_subsys);
526 static inline unsigned long mem_cgroup_protection(struct mem_cgroup *root,
527 struct mem_cgroup *memcg,
530 if (mem_cgroup_disabled())
534 * There is no reclaim protection applied to a targeted reclaim.
535 * We are special casing this specific case here because
536 * mem_cgroup_protected calculation is not robust enough to keep
537 * the protection invariant for calculated effective values for
538 * parallel reclaimers with different reclaim target. This is
539 * especially a problem for tail memcgs (as they have pages on LRU)
540 * which would want to have effective values 0 for targeted reclaim
541 * but a different value for external reclaim.
544 * Let's have global and A's reclaim in parallel:
546 * A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G)
548 * | C (low = 1G, usage = 2.5G)
549 * B (low = 1G, usage = 0.5G)
551 * For the global reclaim
553 * B.elow = min(B.usage, B.low) because children_low_usage <= A.elow
554 * C.elow = min(C.usage, C.low)
556 * With the effective values resetting we have A reclaim
561 * If the global reclaim races with A's reclaim then
562 * B.elow = C.elow = 0 because children_low_usage > A.elow)
563 * is possible and reclaiming B would be violating the protection.
570 return READ_ONCE(memcg->memory.emin);
572 return max(READ_ONCE(memcg->memory.emin),
573 READ_ONCE(memcg->memory.elow));
576 void mem_cgroup_calculate_protection(struct mem_cgroup *root,
577 struct mem_cgroup *memcg);
579 static inline bool mem_cgroup_supports_protection(struct mem_cgroup *memcg)
582 * The root memcg doesn't account charges, and doesn't support
585 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg);
589 static inline bool mem_cgroup_below_low(struct mem_cgroup *memcg)
591 if (!mem_cgroup_supports_protection(memcg))
594 return READ_ONCE(memcg->memory.elow) >=
595 page_counter_read(&memcg->memory);
598 static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg)
600 if (!mem_cgroup_supports_protection(memcg))
603 return READ_ONCE(memcg->memory.emin) >=
604 page_counter_read(&memcg->memory);
607 int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask);
609 void mem_cgroup_uncharge(struct page *page);
610 void mem_cgroup_uncharge_list(struct list_head *page_list);
612 void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
614 static struct mem_cgroup_per_node *
615 mem_cgroup_nodeinfo(struct mem_cgroup *memcg, int nid)
617 return memcg->nodeinfo[nid];
621 * mem_cgroup_lruvec - get the lru list vector for a memcg & node
622 * @memcg: memcg of the wanted lruvec
623 * @pgdat: pglist_data
625 * Returns the lru list vector holding pages for a given @memcg &
626 * @pgdat combination. This can be the node lruvec, if the memory
627 * controller is disabled.
629 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
630 struct pglist_data *pgdat)
632 struct mem_cgroup_per_node *mz;
633 struct lruvec *lruvec;
635 if (mem_cgroup_disabled()) {
636 lruvec = &pgdat->__lruvec;
641 memcg = root_mem_cgroup;
643 mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
644 lruvec = &mz->lruvec;
647 * Since a node can be onlined after the mem_cgroup was created,
648 * we have to be prepared to initialize lruvec->pgdat here;
649 * and if offlined then reonlined, we need to reinitialize it.
651 if (unlikely(lruvec->pgdat != pgdat))
652 lruvec->pgdat = pgdat;
657 * mem_cgroup_page_lruvec - return lruvec for isolating/putting an LRU page
659 * @pgdat: pgdat of the page
661 * This function relies on page->mem_cgroup being stable.
663 static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
664 struct pglist_data *pgdat)
666 struct mem_cgroup *memcg = page_memcg(page);
668 VM_WARN_ON_ONCE_PAGE(!memcg, page);
669 return mem_cgroup_lruvec(memcg, pgdat);
672 static inline bool lruvec_holds_page_lru_lock(struct page *page,
673 struct lruvec *lruvec)
675 pg_data_t *pgdat = page_pgdat(page);
676 const struct mem_cgroup *memcg;
677 struct mem_cgroup_per_node *mz;
679 if (mem_cgroup_disabled())
680 return lruvec == &pgdat->__lruvec;
682 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
683 memcg = page_memcg(page) ? : root_mem_cgroup;
685 return lruvec->pgdat == pgdat && mz->memcg == memcg;
688 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
690 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
692 struct mem_cgroup *get_mem_cgroup_from_page(struct page *page);
694 struct lruvec *lock_page_lruvec(struct page *page);
695 struct lruvec *lock_page_lruvec_irq(struct page *page);
696 struct lruvec *lock_page_lruvec_irqsave(struct page *page,
697 unsigned long *flags);
699 #ifdef CONFIG_DEBUG_VM
700 void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page);
702 static inline void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page)
708 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
709 return css ? container_of(css, struct mem_cgroup, css) : NULL;
712 static inline bool obj_cgroup_tryget(struct obj_cgroup *objcg)
714 return percpu_ref_tryget(&objcg->refcnt);
717 static inline void obj_cgroup_get(struct obj_cgroup *objcg)
719 percpu_ref_get(&objcg->refcnt);
722 static inline void obj_cgroup_put(struct obj_cgroup *objcg)
724 percpu_ref_put(&objcg->refcnt);
728 * After the initialization objcg->memcg is always pointing at
729 * a valid memcg, but can be atomically swapped to the parent memcg.
731 * The caller must ensure that the returned memcg won't be released:
732 * e.g. acquire the rcu_read_lock or css_set_lock.
734 static inline struct mem_cgroup *obj_cgroup_memcg(struct obj_cgroup *objcg)
736 return READ_ONCE(objcg->memcg);
739 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
742 css_put(&memcg->css);
745 #define mem_cgroup_from_counter(counter, member) \
746 container_of(counter, struct mem_cgroup, member)
748 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
750 struct mem_cgroup_reclaim_cookie *);
751 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
752 int mem_cgroup_scan_tasks(struct mem_cgroup *,
753 int (*)(struct task_struct *, void *), void *);
755 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
757 if (mem_cgroup_disabled())
762 struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
764 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
766 return mem_cgroup_from_css(seq_css(m));
769 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
771 struct mem_cgroup_per_node *mz;
773 if (mem_cgroup_disabled())
776 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
781 * parent_mem_cgroup - find the accounting parent of a memcg
782 * @memcg: memcg whose parent to find
784 * Returns the parent memcg, or NULL if this is the root or the memory
785 * controller is in legacy no-hierarchy mode.
787 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
789 if (!memcg->memory.parent)
791 return mem_cgroup_from_counter(memcg->memory.parent, memory);
794 static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
795 struct mem_cgroup *root)
799 return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
802 static inline bool mm_match_cgroup(struct mm_struct *mm,
803 struct mem_cgroup *memcg)
805 struct mem_cgroup *task_memcg;
809 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
811 match = mem_cgroup_is_descendant(task_memcg, memcg);
816 struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
817 ino_t page_cgroup_ino(struct page *page);
819 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
821 if (mem_cgroup_disabled())
823 return !!(memcg->css.flags & CSS_ONLINE);
827 * For memory reclaim.
829 int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
831 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
832 int zid, int nr_pages);
835 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
836 enum lru_list lru, int zone_idx)
838 struct mem_cgroup_per_node *mz;
840 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
841 return READ_ONCE(mz->lru_zone_size[zone_idx][lru]);
844 void mem_cgroup_handle_over_high(void);
846 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
848 unsigned long mem_cgroup_size(struct mem_cgroup *memcg);
850 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
851 struct task_struct *p);
853 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg);
855 static inline void mem_cgroup_enter_user_fault(void)
857 WARN_ON(current->in_user_fault);
858 current->in_user_fault = 1;
861 static inline void mem_cgroup_exit_user_fault(void)
863 WARN_ON(!current->in_user_fault);
864 current->in_user_fault = 0;
867 static inline bool task_in_memcg_oom(struct task_struct *p)
869 return p->memcg_in_oom;
872 bool mem_cgroup_oom_synchronize(bool wait);
873 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
874 struct mem_cgroup *oom_domain);
875 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg);
877 #ifdef CONFIG_MEMCG_SWAP
878 extern bool cgroup_memory_noswap;
881 struct mem_cgroup *lock_page_memcg(struct page *page);
882 void __unlock_page_memcg(struct mem_cgroup *memcg);
883 void unlock_page_memcg(struct page *page);
886 * idx can be of type enum memcg_stat_item or node_stat_item.
887 * Keep in sync with memcg_exact_page_state().
889 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
891 long x = atomic_long_read(&memcg->vmstats[idx]);
900 * idx can be of type enum memcg_stat_item or node_stat_item.
901 * Keep in sync with memcg_exact_page_state().
903 static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
909 for_each_possible_cpu(cpu)
910 x += per_cpu(memcg->vmstats_local->stat[idx], cpu);
918 void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val);
920 /* idx can be of type enum memcg_stat_item or node_stat_item */
921 static inline void mod_memcg_state(struct mem_cgroup *memcg,
926 local_irq_save(flags);
927 __mod_memcg_state(memcg, idx, val);
928 local_irq_restore(flags);
931 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
932 enum node_stat_item idx)
934 struct mem_cgroup_per_node *pn;
937 if (mem_cgroup_disabled())
938 return node_page_state(lruvec_pgdat(lruvec), idx);
940 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
941 x = atomic_long_read(&pn->lruvec_stat[idx]);
949 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
950 enum node_stat_item idx)
952 struct mem_cgroup_per_node *pn;
956 if (mem_cgroup_disabled())
957 return node_page_state(lruvec_pgdat(lruvec), idx);
959 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
960 for_each_possible_cpu(cpu)
961 x += per_cpu(pn->lruvec_stat_local->count[idx], cpu);
969 void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
971 void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val);
973 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
978 local_irq_save(flags);
979 __mod_lruvec_kmem_state(p, idx, val);
980 local_irq_restore(flags);
983 static inline void mod_memcg_lruvec_state(struct lruvec *lruvec,
984 enum node_stat_item idx, int val)
988 local_irq_save(flags);
989 __mod_memcg_lruvec_state(lruvec, idx, val);
990 local_irq_restore(flags);
993 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
995 unsigned long *total_scanned);
997 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
998 unsigned long count);
1000 static inline void count_memcg_events(struct mem_cgroup *memcg,
1001 enum vm_event_item idx,
1002 unsigned long count)
1004 unsigned long flags;
1006 local_irq_save(flags);
1007 __count_memcg_events(memcg, idx, count);
1008 local_irq_restore(flags);
1011 static inline void count_memcg_page_event(struct page *page,
1012 enum vm_event_item idx)
1014 struct mem_cgroup *memcg = page_memcg(page);
1017 count_memcg_events(memcg, idx, 1);
1020 static inline void count_memcg_event_mm(struct mm_struct *mm,
1021 enum vm_event_item idx)
1023 struct mem_cgroup *memcg;
1025 if (mem_cgroup_disabled())
1029 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1031 count_memcg_events(memcg, idx, 1);
1035 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1036 enum memcg_memory_event event)
1038 bool swap_event = event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX ||
1039 event == MEMCG_SWAP_FAIL;
1041 atomic_long_inc(&memcg->memory_events_local[event]);
1043 cgroup_file_notify(&memcg->events_local_file);
1046 atomic_long_inc(&memcg->memory_events[event]);
1048 cgroup_file_notify(&memcg->swap_events_file);
1050 cgroup_file_notify(&memcg->events_file);
1052 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1054 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
1056 } while ((memcg = parent_mem_cgroup(memcg)) &&
1057 !mem_cgroup_is_root(memcg));
1060 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1061 enum memcg_memory_event event)
1063 struct mem_cgroup *memcg;
1065 if (mem_cgroup_disabled())
1069 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1071 memcg_memory_event(memcg, event);
1075 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1076 void mem_cgroup_split_huge_fixup(struct page *head);
1079 #else /* CONFIG_MEMCG */
1081 #define MEM_CGROUP_ID_SHIFT 0
1082 #define MEM_CGROUP_ID_MAX 0
1086 static inline struct mem_cgroup *page_memcg(struct page *page)
1091 static inline struct mem_cgroup *page_memcg_rcu(struct page *page)
1093 WARN_ON_ONCE(!rcu_read_lock_held());
1097 static inline struct mem_cgroup *page_memcg_check(struct page *page)
1102 static inline bool PageMemcgKmem(struct page *page)
1107 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
1112 static inline bool mem_cgroup_disabled(void)
1117 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1118 enum memcg_memory_event event)
1122 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1123 enum memcg_memory_event event)
1127 static inline unsigned long mem_cgroup_protection(struct mem_cgroup *root,
1128 struct mem_cgroup *memcg,
1129 bool in_low_reclaim)
1134 static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root,
1135 struct mem_cgroup *memcg)
1139 static inline bool mem_cgroup_below_low(struct mem_cgroup *memcg)
1144 static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg)
1149 static inline int mem_cgroup_charge(struct page *page, struct mm_struct *mm,
1155 static inline void mem_cgroup_uncharge(struct page *page)
1159 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
1163 static inline void mem_cgroup_migrate(struct page *old, struct page *new)
1167 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
1168 struct pglist_data *pgdat)
1170 return &pgdat->__lruvec;
1173 static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
1174 struct pglist_data *pgdat)
1176 return &pgdat->__lruvec;
1179 static inline bool lruvec_holds_page_lru_lock(struct page *page,
1180 struct lruvec *lruvec)
1182 pg_data_t *pgdat = page_pgdat(page);
1184 return lruvec == &pgdat->__lruvec;
1187 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
1192 static inline bool mm_match_cgroup(struct mm_struct *mm,
1193 struct mem_cgroup *memcg)
1198 static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
1203 static inline struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
1208 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
1212 static inline struct lruvec *lock_page_lruvec(struct page *page)
1214 struct pglist_data *pgdat = page_pgdat(page);
1216 spin_lock(&pgdat->__lruvec.lru_lock);
1217 return &pgdat->__lruvec;
1220 static inline struct lruvec *lock_page_lruvec_irq(struct page *page)
1222 struct pglist_data *pgdat = page_pgdat(page);
1224 spin_lock_irq(&pgdat->__lruvec.lru_lock);
1225 return &pgdat->__lruvec;
1228 static inline struct lruvec *lock_page_lruvec_irqsave(struct page *page,
1229 unsigned long *flagsp)
1231 struct pglist_data *pgdat = page_pgdat(page);
1233 spin_lock_irqsave(&pgdat->__lruvec.lru_lock, *flagsp);
1234 return &pgdat->__lruvec;
1237 static inline struct mem_cgroup *
1238 mem_cgroup_iter(struct mem_cgroup *root,
1239 struct mem_cgroup *prev,
1240 struct mem_cgroup_reclaim_cookie *reclaim)
1245 static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
1246 struct mem_cgroup *prev)
1250 static inline int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1251 int (*fn)(struct task_struct *, void *), void *arg)
1256 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
1261 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
1264 /* XXX: This should always return root_mem_cgroup */
1268 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
1273 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
1278 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
1284 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
1285 enum lru_list lru, int zone_idx)
1290 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
1295 static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1301 mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1306 mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
1310 static inline struct mem_cgroup *lock_page_memcg(struct page *page)
1315 static inline void __unlock_page_memcg(struct mem_cgroup *memcg)
1319 static inline void unlock_page_memcg(struct page *page)
1323 static inline void mem_cgroup_handle_over_high(void)
1327 static inline void mem_cgroup_enter_user_fault(void)
1331 static inline void mem_cgroup_exit_user_fault(void)
1335 static inline bool task_in_memcg_oom(struct task_struct *p)
1340 static inline bool mem_cgroup_oom_synchronize(bool wait)
1345 static inline struct mem_cgroup *mem_cgroup_get_oom_group(
1346 struct task_struct *victim, struct mem_cgroup *oom_domain)
1351 static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
1355 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
1360 static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
1366 static inline void __mod_memcg_state(struct mem_cgroup *memcg,
1372 static inline void mod_memcg_state(struct mem_cgroup *memcg,
1378 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1379 enum node_stat_item idx)
1381 return node_page_state(lruvec_pgdat(lruvec), idx);
1384 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1385 enum node_stat_item idx)
1387 return node_page_state(lruvec_pgdat(lruvec), idx);
1390 static inline void __mod_memcg_lruvec_state(struct lruvec *lruvec,
1391 enum node_stat_item idx, int val)
1395 static inline void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1398 struct page *page = virt_to_head_page(p);
1400 __mod_node_page_state(page_pgdat(page), idx, val);
1403 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1406 struct page *page = virt_to_head_page(p);
1408 mod_node_page_state(page_pgdat(page), idx, val);
1412 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1414 unsigned long *total_scanned)
1419 static inline void mem_cgroup_split_huge_fixup(struct page *head)
1423 static inline void count_memcg_events(struct mem_cgroup *memcg,
1424 enum vm_event_item idx,
1425 unsigned long count)
1429 static inline void __count_memcg_events(struct mem_cgroup *memcg,
1430 enum vm_event_item idx,
1431 unsigned long count)
1435 static inline void count_memcg_page_event(struct page *page,
1441 void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
1445 static inline void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page)
1448 #endif /* CONFIG_MEMCG */
1450 static inline void __inc_lruvec_kmem_state(void *p, enum node_stat_item idx)
1452 __mod_lruvec_kmem_state(p, idx, 1);
1455 static inline void __dec_lruvec_kmem_state(void *p, enum node_stat_item idx)
1457 __mod_lruvec_kmem_state(p, idx, -1);
1460 static inline struct lruvec *parent_lruvec(struct lruvec *lruvec)
1462 struct mem_cgroup *memcg;
1464 memcg = lruvec_memcg(lruvec);
1467 memcg = parent_mem_cgroup(memcg);
1470 return mem_cgroup_lruvec(memcg, lruvec_pgdat(lruvec));
1473 static inline void unlock_page_lruvec(struct lruvec *lruvec)
1475 spin_unlock(&lruvec->lru_lock);
1478 static inline void unlock_page_lruvec_irq(struct lruvec *lruvec)
1480 spin_unlock_irq(&lruvec->lru_lock);
1483 static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec,
1484 unsigned long flags)
1486 spin_unlock_irqrestore(&lruvec->lru_lock, flags);
1489 /* Don't lock again iff page's lruvec locked */
1490 static inline struct lruvec *relock_page_lruvec_irq(struct page *page,
1491 struct lruvec *locked_lruvec)
1493 if (locked_lruvec) {
1494 if (lruvec_holds_page_lru_lock(page, locked_lruvec))
1495 return locked_lruvec;
1497 unlock_page_lruvec_irq(locked_lruvec);
1500 return lock_page_lruvec_irq(page);
1503 /* Don't lock again iff page's lruvec locked */
1504 static inline struct lruvec *relock_page_lruvec_irqsave(struct page *page,
1505 struct lruvec *locked_lruvec, unsigned long *flags)
1507 if (locked_lruvec) {
1508 if (lruvec_holds_page_lru_lock(page, locked_lruvec))
1509 return locked_lruvec;
1511 unlock_page_lruvec_irqrestore(locked_lruvec, *flags);
1514 return lock_page_lruvec_irqsave(page, flags);
1517 #ifdef CONFIG_CGROUP_WRITEBACK
1519 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
1520 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1521 unsigned long *pheadroom, unsigned long *pdirty,
1522 unsigned long *pwriteback);
1524 void mem_cgroup_track_foreign_dirty_slowpath(struct page *page,
1525 struct bdi_writeback *wb);
1527 static inline void mem_cgroup_track_foreign_dirty(struct page *page,
1528 struct bdi_writeback *wb)
1530 if (mem_cgroup_disabled())
1533 if (unlikely(&page_memcg(page)->css != wb->memcg_css))
1534 mem_cgroup_track_foreign_dirty_slowpath(page, wb);
1537 void mem_cgroup_flush_foreign(struct bdi_writeback *wb);
1539 #else /* CONFIG_CGROUP_WRITEBACK */
1541 static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1546 static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
1547 unsigned long *pfilepages,
1548 unsigned long *pheadroom,
1549 unsigned long *pdirty,
1550 unsigned long *pwriteback)
1554 static inline void mem_cgroup_track_foreign_dirty(struct page *page,
1555 struct bdi_writeback *wb)
1559 static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
1563 #endif /* CONFIG_CGROUP_WRITEBACK */
1566 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1567 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1569 extern struct static_key_false memcg_sockets_enabled_key;
1570 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
1571 void mem_cgroup_sk_alloc(struct sock *sk);
1572 void mem_cgroup_sk_free(struct sock *sk);
1573 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1575 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure)
1578 if (time_before(jiffies, memcg->socket_pressure))
1580 } while ((memcg = parent_mem_cgroup(memcg)));
1584 extern int memcg_expand_shrinker_maps(int new_id);
1586 extern void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1587 int nid, int shrinker_id);
1589 #define mem_cgroup_sockets_enabled 0
1590 static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1591 static inline void mem_cgroup_sk_free(struct sock *sk) { };
1592 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1597 static inline void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1598 int nid, int shrinker_id)
1603 #ifdef CONFIG_MEMCG_KMEM
1604 int __memcg_kmem_charge(struct mem_cgroup *memcg, gfp_t gfp,
1605 unsigned int nr_pages);
1606 void __memcg_kmem_uncharge(struct mem_cgroup *memcg, unsigned int nr_pages);
1607 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order);
1608 void __memcg_kmem_uncharge_page(struct page *page, int order);
1610 struct obj_cgroup *get_obj_cgroup_from_current(void);
1612 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size);
1613 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size);
1615 extern struct static_key_false memcg_kmem_enabled_key;
1617 extern int memcg_nr_cache_ids;
1618 void memcg_get_cache_ids(void);
1619 void memcg_put_cache_ids(void);
1622 * Helper macro to loop through all memcg-specific caches. Callers must still
1623 * check if the cache is valid (it is either valid or NULL).
1624 * the slab_mutex must be held when looping through those caches
1626 #define for_each_memcg_cache_index(_idx) \
1627 for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
1629 static inline bool memcg_kmem_enabled(void)
1631 return static_branch_likely(&memcg_kmem_enabled_key);
1634 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1637 if (memcg_kmem_enabled())
1638 return __memcg_kmem_charge_page(page, gfp, order);
1642 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1644 if (memcg_kmem_enabled())
1645 __memcg_kmem_uncharge_page(page, order);
1649 * A helper for accessing memcg's kmem_id, used for getting
1650 * corresponding LRU lists.
1652 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1654 return memcg ? memcg->kmemcg_id : -1;
1657 struct mem_cgroup *mem_cgroup_from_obj(void *p);
1661 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1667 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1671 static inline int __memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1677 static inline void __memcg_kmem_uncharge_page(struct page *page, int order)
1681 #define for_each_memcg_cache_index(_idx) \
1684 static inline bool memcg_kmem_enabled(void)
1689 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1694 static inline void memcg_get_cache_ids(void)
1698 static inline void memcg_put_cache_ids(void)
1702 static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)
1707 #endif /* CONFIG_MEMCG_KMEM */
1709 #endif /* _LINUX_MEMCONTROL_H */