1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* memcontrol.h - Memory Controller
4 * Copyright IBM Corporation, 2007
5 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
7 * Copyright 2007 OpenVZ SWsoft Inc
8 * Author: Pavel Emelianov <xemul@openvz.org>
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,
38 enum memcg_memory_event {
47 MEMCG_NR_MEMORY_EVENTS,
50 struct mem_cgroup_reclaim_cookie {
52 unsigned int generation;
57 #define MEM_CGROUP_ID_SHIFT 16
58 #define MEM_CGROUP_ID_MAX USHRT_MAX
60 struct mem_cgroup_id {
66 * Per memcg event counter is incremented at every pagein/pageout. With THP,
67 * it will be incremated by the number of pages. This counter is used for
68 * for trigger some periodic events. This is straightforward and better
69 * than using jiffies etc. to handle periodic memcg event.
71 enum mem_cgroup_events_target {
72 MEM_CGROUP_TARGET_THRESH,
73 MEM_CGROUP_TARGET_SOFTLIMIT,
77 struct memcg_vmstats_percpu {
78 long stat[MEMCG_NR_STAT];
79 unsigned long events[NR_VM_EVENT_ITEMS];
80 unsigned long nr_page_events;
81 unsigned long targets[MEM_CGROUP_NTARGETS];
84 struct mem_cgroup_reclaim_iter {
85 struct mem_cgroup *position;
86 /* scan generation, increased every round-trip */
87 unsigned int generation;
91 long count[NR_VM_NODE_STAT_ITEMS];
95 * Bitmap of shrinker::id corresponding to memcg-aware shrinkers,
96 * which have elements charged to this memcg.
98 struct memcg_shrinker_map {
104 * per-node information in memory controller.
106 struct mem_cgroup_per_node {
107 struct lruvec lruvec;
109 /* Legacy local VM stats */
110 struct lruvec_stat __percpu *lruvec_stat_local;
112 /* Subtree VM stats (batched updates) */
113 struct lruvec_stat __percpu *lruvec_stat_cpu;
114 atomic_long_t lruvec_stat[NR_VM_NODE_STAT_ITEMS];
116 unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
118 struct mem_cgroup_reclaim_iter iter;
120 struct memcg_shrinker_map __rcu *shrinker_map;
122 struct rb_node tree_node; /* RB tree node */
123 unsigned long usage_in_excess;/* Set to the value by which */
124 /* the soft limit is exceeded*/
126 struct mem_cgroup *memcg; /* Back pointer, we cannot */
127 /* use container_of */
130 struct mem_cgroup_threshold {
131 struct eventfd_ctx *eventfd;
132 unsigned long threshold;
136 struct mem_cgroup_threshold_ary {
137 /* An array index points to threshold just below or equal to usage. */
138 int current_threshold;
139 /* Size of entries[] */
141 /* Array of thresholds */
142 struct mem_cgroup_threshold entries[];
145 struct mem_cgroup_thresholds {
146 /* Primary thresholds array */
147 struct mem_cgroup_threshold_ary *primary;
149 * Spare threshold array.
150 * This is needed to make mem_cgroup_unregister_event() "never fail".
151 * It must be able to store at least primary->size - 1 entries.
153 struct mem_cgroup_threshold_ary *spare;
156 enum memcg_kmem_state {
162 #if defined(CONFIG_SMP)
163 struct memcg_padding {
165 } ____cacheline_internodealigned_in_smp;
166 #define MEMCG_PADDING(name) struct memcg_padding name;
168 #define MEMCG_PADDING(name)
172 * Remember four most recent foreign writebacks with dirty pages in this
173 * cgroup. Inode sharing is expected to be uncommon and, even if we miss
174 * one in a given round, we're likely to catch it later if it keeps
175 * foreign-dirtying, so a fairly low count should be enough.
177 * See mem_cgroup_track_foreign_dirty_slowpath() for details.
179 #define MEMCG_CGWB_FRN_CNT 4
181 struct memcg_cgwb_frn {
182 u64 bdi_id; /* bdi->id of the foreign inode */
183 int memcg_id; /* memcg->css.id of foreign inode */
184 u64 at; /* jiffies_64 at the time of dirtying */
185 struct wb_completion done; /* tracks in-flight foreign writebacks */
189 * Bucket for arbitrarily byte-sized objects charged to a memory
190 * cgroup. The bucket can be reparented in one piece when the cgroup
191 * is destroyed, without having to round up the individual references
192 * of all live memory objects in the wild.
195 struct percpu_ref refcnt;
196 struct mem_cgroup *memcg;
197 atomic_t nr_charged_bytes;
199 struct list_head list;
205 * The memory controller data structure. The memory controller controls both
206 * page cache and RSS per cgroup. We would eventually like to provide
207 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
208 * to help the administrator determine what knobs to tune.
211 struct cgroup_subsys_state css;
213 /* Private memcg ID. Used to ID objects that outlive the cgroup */
214 struct mem_cgroup_id id;
216 /* Accounted resources */
217 struct page_counter memory;
218 struct page_counter swap;
220 /* Legacy consumer-oriented counters */
221 struct page_counter memsw;
222 struct page_counter kmem;
223 struct page_counter tcpmem;
225 /* Range enforcement for interrupt charges */
226 struct work_struct high_work;
228 unsigned long soft_limit;
230 /* vmpressure notifications */
231 struct vmpressure vmpressure;
234 * Should the accounting and control be hierarchical, per subtree?
239 * Should the OOM killer kill all belonging tasks, had it kill one?
243 /* protected by memcg_oom_lock */
248 /* OOM-Killer disable */
249 int oom_kill_disable;
251 /* memory.events and memory.events.local */
252 struct cgroup_file events_file;
253 struct cgroup_file events_local_file;
255 /* handle for "memory.swap.events" */
256 struct cgroup_file swap_events_file;
258 /* protect arrays of thresholds */
259 struct mutex thresholds_lock;
261 /* thresholds for memory usage. RCU-protected */
262 struct mem_cgroup_thresholds thresholds;
264 /* thresholds for mem+swap usage. RCU-protected */
265 struct mem_cgroup_thresholds memsw_thresholds;
267 /* For oom notifier event fd */
268 struct list_head oom_notify;
271 * Should we move charges of a task when a task is moved into this
272 * mem_cgroup ? And what type of charges should we move ?
274 unsigned long move_charge_at_immigrate;
275 /* taken only while moving_account > 0 */
276 spinlock_t move_lock;
277 unsigned long move_lock_flags;
279 MEMCG_PADDING(_pad1_);
282 * set > 0 if pages under this cgroup are moving to other cgroup.
284 atomic_t moving_account;
285 struct task_struct *move_lock_task;
287 /* Legacy local VM stats and events */
288 struct memcg_vmstats_percpu __percpu *vmstats_local;
290 /* Subtree VM stats and events (batched updates) */
291 struct memcg_vmstats_percpu __percpu *vmstats_percpu;
293 MEMCG_PADDING(_pad2_);
295 atomic_long_t vmstats[MEMCG_NR_STAT];
296 atomic_long_t vmevents[NR_VM_EVENT_ITEMS];
299 atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
300 atomic_long_t memory_events_local[MEMCG_NR_MEMORY_EVENTS];
302 unsigned long socket_pressure;
304 /* Legacy tcp memory accounting */
308 #ifdef CONFIG_MEMCG_KMEM
309 /* Index in the kmem_cache->memcg_params.memcg_caches array */
311 enum memcg_kmem_state kmem_state;
312 struct obj_cgroup __rcu *objcg;
313 struct list_head objcg_list; /* list of inherited objcgs */
316 #ifdef CONFIG_CGROUP_WRITEBACK
317 struct list_head cgwb_list;
318 struct wb_domain cgwb_domain;
319 struct memcg_cgwb_frn cgwb_frn[MEMCG_CGWB_FRN_CNT];
322 /* List of events which userspace want to receive */
323 struct list_head event_list;
324 spinlock_t event_list_lock;
326 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
327 struct deferred_split deferred_split_queue;
330 struct mem_cgroup_per_node *nodeinfo[0];
331 /* WARNING: nodeinfo must be the last member here */
335 * size of first charge trial. "32" comes from vmscan.c's magic value.
336 * TODO: maybe necessary to use big numbers in big irons.
338 #define MEMCG_CHARGE_BATCH 32U
340 extern struct mem_cgroup *root_mem_cgroup;
342 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
344 return (memcg == root_mem_cgroup);
347 static inline bool mem_cgroup_disabled(void)
349 return !cgroup_subsys_enabled(memory_cgrp_subsys);
352 static inline unsigned long mem_cgroup_protection(struct mem_cgroup *root,
353 struct mem_cgroup *memcg,
356 if (mem_cgroup_disabled())
360 * There is no reclaim protection applied to a targeted reclaim.
361 * We are special casing this specific case here because
362 * mem_cgroup_protected calculation is not robust enough to keep
363 * the protection invariant for calculated effective values for
364 * parallel reclaimers with different reclaim target. This is
365 * especially a problem for tail memcgs (as they have pages on LRU)
366 * which would want to have effective values 0 for targeted reclaim
367 * but a different value for external reclaim.
370 * Let's have global and A's reclaim in parallel:
372 * A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G)
374 * | C (low = 1G, usage = 2.5G)
375 * B (low = 1G, usage = 0.5G)
377 * For the global reclaim
379 * B.elow = min(B.usage, B.low) because children_low_usage <= A.elow
380 * C.elow = min(C.usage, C.low)
382 * With the effective values resetting we have A reclaim
387 * If the global reclaim races with A's reclaim then
388 * B.elow = C.elow = 0 because children_low_usage > A.elow)
389 * is possible and reclaiming B would be violating the protection.
396 return READ_ONCE(memcg->memory.emin);
398 return max(READ_ONCE(memcg->memory.emin),
399 READ_ONCE(memcg->memory.elow));
402 void mem_cgroup_calculate_protection(struct mem_cgroup *root,
403 struct mem_cgroup *memcg);
405 static inline bool mem_cgroup_supports_protection(struct mem_cgroup *memcg)
408 * The root memcg doesn't account charges, and doesn't support
411 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg);
415 static inline bool mem_cgroup_below_low(struct mem_cgroup *memcg)
417 if (!mem_cgroup_supports_protection(memcg))
420 return READ_ONCE(memcg->memory.elow) >=
421 page_counter_read(&memcg->memory);
424 static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg)
426 if (!mem_cgroup_supports_protection(memcg))
429 return READ_ONCE(memcg->memory.emin) >=
430 page_counter_read(&memcg->memory);
433 int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask);
435 void mem_cgroup_uncharge(struct page *page);
436 void mem_cgroup_uncharge_list(struct list_head *page_list);
438 void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
440 static struct mem_cgroup_per_node *
441 mem_cgroup_nodeinfo(struct mem_cgroup *memcg, int nid)
443 return memcg->nodeinfo[nid];
447 * mem_cgroup_lruvec - get the lru list vector for a memcg & node
448 * @memcg: memcg of the wanted lruvec
450 * Returns the lru list vector holding pages for a given @memcg &
451 * @node combination. This can be the node lruvec, if the memory
452 * controller is disabled.
454 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
455 struct pglist_data *pgdat)
457 struct mem_cgroup_per_node *mz;
458 struct lruvec *lruvec;
460 if (mem_cgroup_disabled()) {
461 lruvec = &pgdat->__lruvec;
466 memcg = root_mem_cgroup;
468 mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
469 lruvec = &mz->lruvec;
472 * Since a node can be onlined after the mem_cgroup was created,
473 * we have to be prepared to initialize lruvec->pgdat here;
474 * and if offlined then reonlined, we need to reinitialize it.
476 if (unlikely(lruvec->pgdat != pgdat))
477 lruvec->pgdat = pgdat;
481 struct lruvec *mem_cgroup_page_lruvec(struct page *, struct pglist_data *);
483 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
485 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
487 struct mem_cgroup *get_mem_cgroup_from_page(struct page *page);
490 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
491 return css ? container_of(css, struct mem_cgroup, css) : NULL;
494 static inline bool obj_cgroup_tryget(struct obj_cgroup *objcg)
496 return percpu_ref_tryget(&objcg->refcnt);
499 static inline void obj_cgroup_get(struct obj_cgroup *objcg)
501 percpu_ref_get(&objcg->refcnt);
504 static inline void obj_cgroup_put(struct obj_cgroup *objcg)
506 percpu_ref_put(&objcg->refcnt);
510 * After the initialization objcg->memcg is always pointing at
511 * a valid memcg, but can be atomically swapped to the parent memcg.
513 * The caller must ensure that the returned memcg won't be released:
514 * e.g. acquire the rcu_read_lock or css_set_lock.
516 static inline struct mem_cgroup *obj_cgroup_memcg(struct obj_cgroup *objcg)
518 return READ_ONCE(objcg->memcg);
521 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
524 css_put(&memcg->css);
527 #define mem_cgroup_from_counter(counter, member) \
528 container_of(counter, struct mem_cgroup, member)
530 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
532 struct mem_cgroup_reclaim_cookie *);
533 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
534 int mem_cgroup_scan_tasks(struct mem_cgroup *,
535 int (*)(struct task_struct *, void *), void *);
537 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
539 if (mem_cgroup_disabled())
544 struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
546 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
548 return mem_cgroup_from_css(seq_css(m));
551 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
553 struct mem_cgroup_per_node *mz;
555 if (mem_cgroup_disabled())
558 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
563 * parent_mem_cgroup - find the accounting parent of a memcg
564 * @memcg: memcg whose parent to find
566 * Returns the parent memcg, or NULL if this is the root or the memory
567 * controller is in legacy no-hierarchy mode.
569 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
571 if (!memcg->memory.parent)
573 return mem_cgroup_from_counter(memcg->memory.parent, memory);
576 static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
577 struct mem_cgroup *root)
581 if (!root->use_hierarchy)
583 return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
586 static inline bool mm_match_cgroup(struct mm_struct *mm,
587 struct mem_cgroup *memcg)
589 struct mem_cgroup *task_memcg;
593 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
595 match = mem_cgroup_is_descendant(task_memcg, memcg);
600 struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
601 ino_t page_cgroup_ino(struct page *page);
603 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
605 if (mem_cgroup_disabled())
607 return !!(memcg->css.flags & CSS_ONLINE);
611 * For memory reclaim.
613 int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
615 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
616 int zid, int nr_pages);
619 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
620 enum lru_list lru, int zone_idx)
622 struct mem_cgroup_per_node *mz;
624 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
625 return mz->lru_zone_size[zone_idx][lru];
628 void mem_cgroup_handle_over_high(void);
630 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
632 unsigned long mem_cgroup_size(struct mem_cgroup *memcg);
634 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
635 struct task_struct *p);
637 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg);
639 static inline void mem_cgroup_enter_user_fault(void)
641 WARN_ON(current->in_user_fault);
642 current->in_user_fault = 1;
645 static inline void mem_cgroup_exit_user_fault(void)
647 WARN_ON(!current->in_user_fault);
648 current->in_user_fault = 0;
651 static inline bool task_in_memcg_oom(struct task_struct *p)
653 return p->memcg_in_oom;
656 bool mem_cgroup_oom_synchronize(bool wait);
657 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
658 struct mem_cgroup *oom_domain);
659 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg);
661 #ifdef CONFIG_MEMCG_SWAP
662 extern bool cgroup_memory_noswap;
665 struct mem_cgroup *lock_page_memcg(struct page *page);
666 void __unlock_page_memcg(struct mem_cgroup *memcg);
667 void unlock_page_memcg(struct page *page);
670 * idx can be of type enum memcg_stat_item or node_stat_item.
671 * Keep in sync with memcg_exact_page_state().
673 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
675 long x = atomic_long_read(&memcg->vmstats[idx]);
684 * idx can be of type enum memcg_stat_item or node_stat_item.
685 * Keep in sync with memcg_exact_page_state().
687 static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
693 for_each_possible_cpu(cpu)
694 x += per_cpu(memcg->vmstats_local->stat[idx], cpu);
702 void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val);
704 /* idx can be of type enum memcg_stat_item or node_stat_item */
705 static inline void mod_memcg_state(struct mem_cgroup *memcg,
710 local_irq_save(flags);
711 __mod_memcg_state(memcg, idx, val);
712 local_irq_restore(flags);
716 * mod_memcg_page_state - update page state statistics
718 * @idx: page state item to account
719 * @val: number of pages (positive or negative)
721 * The @page must be locked or the caller must use lock_page_memcg()
722 * to prevent double accounting when the page is concurrently being
723 * moved to another memcg:
725 * lock_page(page) or lock_page_memcg(page)
726 * if (TestClearPageState(page))
727 * mod_memcg_page_state(page, state, -1);
728 * unlock_page(page) or unlock_page_memcg(page)
730 * Kernel pages are an exception to this, since they'll never move.
732 static inline void __mod_memcg_page_state(struct page *page,
735 if (page->mem_cgroup)
736 __mod_memcg_state(page->mem_cgroup, idx, val);
739 static inline void mod_memcg_page_state(struct page *page,
742 if (page->mem_cgroup)
743 mod_memcg_state(page->mem_cgroup, idx, val);
746 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
747 enum node_stat_item idx)
749 struct mem_cgroup_per_node *pn;
752 if (mem_cgroup_disabled())
753 return node_page_state(lruvec_pgdat(lruvec), idx);
755 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
756 x = atomic_long_read(&pn->lruvec_stat[idx]);
764 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
765 enum node_stat_item idx)
767 struct mem_cgroup_per_node *pn;
771 if (mem_cgroup_disabled())
772 return node_page_state(lruvec_pgdat(lruvec), idx);
774 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
775 for_each_possible_cpu(cpu)
776 x += per_cpu(pn->lruvec_stat_local->count[idx], cpu);
784 void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
786 void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
788 void __mod_lruvec_slab_state(void *p, enum node_stat_item idx, int val);
790 void mod_memcg_obj_state(void *p, int idx, int val);
792 static inline void mod_lruvec_slab_state(void *p, enum node_stat_item idx,
797 local_irq_save(flags);
798 __mod_lruvec_slab_state(p, idx, val);
799 local_irq_restore(flags);
802 static inline void mod_memcg_lruvec_state(struct lruvec *lruvec,
803 enum node_stat_item idx, int val)
807 local_irq_save(flags);
808 __mod_memcg_lruvec_state(lruvec, idx, val);
809 local_irq_restore(flags);
812 static inline void mod_lruvec_state(struct lruvec *lruvec,
813 enum node_stat_item idx, int val)
817 local_irq_save(flags);
818 __mod_lruvec_state(lruvec, idx, val);
819 local_irq_restore(flags);
822 static inline void __mod_lruvec_page_state(struct page *page,
823 enum node_stat_item idx, int val)
825 struct page *head = compound_head(page); /* rmap on tail pages */
826 pg_data_t *pgdat = page_pgdat(page);
827 struct lruvec *lruvec;
829 /* Untracked pages have no memcg, no lruvec. Update only the node */
830 if (!head->mem_cgroup) {
831 __mod_node_page_state(pgdat, idx, val);
835 lruvec = mem_cgroup_lruvec(head->mem_cgroup, pgdat);
836 __mod_lruvec_state(lruvec, idx, val);
839 static inline void mod_lruvec_page_state(struct page *page,
840 enum node_stat_item idx, int val)
844 local_irq_save(flags);
845 __mod_lruvec_page_state(page, idx, val);
846 local_irq_restore(flags);
849 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
851 unsigned long *total_scanned);
853 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
854 unsigned long count);
856 static inline void count_memcg_events(struct mem_cgroup *memcg,
857 enum vm_event_item idx,
862 local_irq_save(flags);
863 __count_memcg_events(memcg, idx, count);
864 local_irq_restore(flags);
867 static inline void count_memcg_page_event(struct page *page,
868 enum vm_event_item idx)
870 if (page->mem_cgroup)
871 count_memcg_events(page->mem_cgroup, idx, 1);
874 static inline void count_memcg_event_mm(struct mm_struct *mm,
875 enum vm_event_item idx)
877 struct mem_cgroup *memcg;
879 if (mem_cgroup_disabled())
883 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
885 count_memcg_events(memcg, idx, 1);
889 static inline void memcg_memory_event(struct mem_cgroup *memcg,
890 enum memcg_memory_event event)
892 atomic_long_inc(&memcg->memory_events_local[event]);
893 cgroup_file_notify(&memcg->events_local_file);
896 atomic_long_inc(&memcg->memory_events[event]);
897 cgroup_file_notify(&memcg->events_file);
899 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
901 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
903 } while ((memcg = parent_mem_cgroup(memcg)) &&
904 !mem_cgroup_is_root(memcg));
907 static inline void memcg_memory_event_mm(struct mm_struct *mm,
908 enum memcg_memory_event event)
910 struct mem_cgroup *memcg;
912 if (mem_cgroup_disabled())
916 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
918 memcg_memory_event(memcg, event);
922 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
923 void mem_cgroup_split_huge_fixup(struct page *head);
926 #else /* CONFIG_MEMCG */
928 #define MEM_CGROUP_ID_SHIFT 0
929 #define MEM_CGROUP_ID_MAX 0
933 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
938 static inline bool mem_cgroup_disabled(void)
943 static inline void memcg_memory_event(struct mem_cgroup *memcg,
944 enum memcg_memory_event event)
948 static inline void memcg_memory_event_mm(struct mm_struct *mm,
949 enum memcg_memory_event event)
953 static inline unsigned long mem_cgroup_protection(struct mem_cgroup *root,
954 struct mem_cgroup *memcg,
960 static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root,
961 struct mem_cgroup *memcg)
965 static inline bool mem_cgroup_below_low(struct mem_cgroup *memcg)
970 static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg)
975 static inline int mem_cgroup_charge(struct page *page, struct mm_struct *mm,
981 static inline void mem_cgroup_uncharge(struct page *page)
985 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
989 static inline void mem_cgroup_migrate(struct page *old, struct page *new)
993 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
994 struct pglist_data *pgdat)
996 return &pgdat->__lruvec;
999 static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
1000 struct pglist_data *pgdat)
1002 return &pgdat->__lruvec;
1005 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
1010 static inline bool mm_match_cgroup(struct mm_struct *mm,
1011 struct mem_cgroup *memcg)
1016 static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
1021 static inline struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
1026 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
1030 static inline struct mem_cgroup *
1031 mem_cgroup_iter(struct mem_cgroup *root,
1032 struct mem_cgroup *prev,
1033 struct mem_cgroup_reclaim_cookie *reclaim)
1038 static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
1039 struct mem_cgroup *prev)
1043 static inline int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1044 int (*fn)(struct task_struct *, void *), void *arg)
1049 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
1054 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
1057 /* XXX: This should always return root_mem_cgroup */
1061 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
1066 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
1071 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
1077 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
1078 enum lru_list lru, int zone_idx)
1083 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
1088 static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1094 mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1099 mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
1103 static inline struct mem_cgroup *lock_page_memcg(struct page *page)
1108 static inline void __unlock_page_memcg(struct mem_cgroup *memcg)
1112 static inline void unlock_page_memcg(struct page *page)
1116 static inline void mem_cgroup_handle_over_high(void)
1120 static inline void mem_cgroup_enter_user_fault(void)
1124 static inline void mem_cgroup_exit_user_fault(void)
1128 static inline bool task_in_memcg_oom(struct task_struct *p)
1133 static inline bool mem_cgroup_oom_synchronize(bool wait)
1138 static inline struct mem_cgroup *mem_cgroup_get_oom_group(
1139 struct task_struct *victim, struct mem_cgroup *oom_domain)
1144 static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
1148 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
1153 static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
1159 static inline void __mod_memcg_state(struct mem_cgroup *memcg,
1165 static inline void mod_memcg_state(struct mem_cgroup *memcg,
1171 static inline void __mod_memcg_page_state(struct page *page,
1177 static inline void mod_memcg_page_state(struct page *page,
1183 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1184 enum node_stat_item idx)
1186 return node_page_state(lruvec_pgdat(lruvec), idx);
1189 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1190 enum node_stat_item idx)
1192 return node_page_state(lruvec_pgdat(lruvec), idx);
1195 static inline void __mod_memcg_lruvec_state(struct lruvec *lruvec,
1196 enum node_stat_item idx, int val)
1200 static inline void __mod_lruvec_state(struct lruvec *lruvec,
1201 enum node_stat_item idx, int val)
1203 __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
1206 static inline void mod_lruvec_state(struct lruvec *lruvec,
1207 enum node_stat_item idx, int val)
1209 mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
1212 static inline void __mod_lruvec_page_state(struct page *page,
1213 enum node_stat_item idx, int val)
1215 __mod_node_page_state(page_pgdat(page), idx, val);
1218 static inline void mod_lruvec_page_state(struct page *page,
1219 enum node_stat_item idx, int val)
1221 mod_node_page_state(page_pgdat(page), idx, val);
1224 static inline void __mod_lruvec_slab_state(void *p, enum node_stat_item idx,
1227 struct page *page = virt_to_head_page(p);
1229 __mod_node_page_state(page_pgdat(page), idx, val);
1232 static inline void mod_lruvec_slab_state(void *p, enum node_stat_item idx,
1235 struct page *page = virt_to_head_page(p);
1237 mod_node_page_state(page_pgdat(page), idx, val);
1240 static inline void mod_memcg_obj_state(void *p, int idx, int val)
1245 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1247 unsigned long *total_scanned)
1252 static inline void mem_cgroup_split_huge_fixup(struct page *head)
1256 static inline void count_memcg_events(struct mem_cgroup *memcg,
1257 enum vm_event_item idx,
1258 unsigned long count)
1262 static inline void __count_memcg_events(struct mem_cgroup *memcg,
1263 enum vm_event_item idx,
1264 unsigned long count)
1268 static inline void count_memcg_page_event(struct page *page,
1274 void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
1277 #endif /* CONFIG_MEMCG */
1279 /* idx can be of type enum memcg_stat_item or node_stat_item */
1280 static inline void __inc_memcg_state(struct mem_cgroup *memcg,
1283 __mod_memcg_state(memcg, idx, 1);
1286 /* idx can be of type enum memcg_stat_item or node_stat_item */
1287 static inline void __dec_memcg_state(struct mem_cgroup *memcg,
1290 __mod_memcg_state(memcg, idx, -1);
1293 /* idx can be of type enum memcg_stat_item or node_stat_item */
1294 static inline void __inc_memcg_page_state(struct page *page,
1297 __mod_memcg_page_state(page, idx, 1);
1300 /* idx can be of type enum memcg_stat_item or node_stat_item */
1301 static inline void __dec_memcg_page_state(struct page *page,
1304 __mod_memcg_page_state(page, idx, -1);
1307 static inline void __inc_lruvec_state(struct lruvec *lruvec,
1308 enum node_stat_item idx)
1310 __mod_lruvec_state(lruvec, idx, 1);
1313 static inline void __dec_lruvec_state(struct lruvec *lruvec,
1314 enum node_stat_item idx)
1316 __mod_lruvec_state(lruvec, idx, -1);
1319 static inline void __inc_lruvec_page_state(struct page *page,
1320 enum node_stat_item idx)
1322 __mod_lruvec_page_state(page, idx, 1);
1325 static inline void __dec_lruvec_page_state(struct page *page,
1326 enum node_stat_item idx)
1328 __mod_lruvec_page_state(page, idx, -1);
1331 static inline void __inc_lruvec_slab_state(void *p, enum node_stat_item idx)
1333 __mod_lruvec_slab_state(p, idx, 1);
1336 static inline void __dec_lruvec_slab_state(void *p, enum node_stat_item idx)
1338 __mod_lruvec_slab_state(p, idx, -1);
1341 /* idx can be of type enum memcg_stat_item or node_stat_item */
1342 static inline void inc_memcg_state(struct mem_cgroup *memcg,
1345 mod_memcg_state(memcg, idx, 1);
1348 /* idx can be of type enum memcg_stat_item or node_stat_item */
1349 static inline void dec_memcg_state(struct mem_cgroup *memcg,
1352 mod_memcg_state(memcg, idx, -1);
1355 /* idx can be of type enum memcg_stat_item or node_stat_item */
1356 static inline void inc_memcg_page_state(struct page *page,
1359 mod_memcg_page_state(page, idx, 1);
1362 /* idx can be of type enum memcg_stat_item or node_stat_item */
1363 static inline void dec_memcg_page_state(struct page *page,
1366 mod_memcg_page_state(page, idx, -1);
1369 static inline void inc_lruvec_state(struct lruvec *lruvec,
1370 enum node_stat_item idx)
1372 mod_lruvec_state(lruvec, idx, 1);
1375 static inline void dec_lruvec_state(struct lruvec *lruvec,
1376 enum node_stat_item idx)
1378 mod_lruvec_state(lruvec, idx, -1);
1381 static inline void inc_lruvec_page_state(struct page *page,
1382 enum node_stat_item idx)
1384 mod_lruvec_page_state(page, idx, 1);
1387 static inline void dec_lruvec_page_state(struct page *page,
1388 enum node_stat_item idx)
1390 mod_lruvec_page_state(page, idx, -1);
1393 static inline struct lruvec *parent_lruvec(struct lruvec *lruvec)
1395 struct mem_cgroup *memcg;
1397 memcg = lruvec_memcg(lruvec);
1400 memcg = parent_mem_cgroup(memcg);
1403 return mem_cgroup_lruvec(memcg, lruvec_pgdat(lruvec));
1406 #ifdef CONFIG_CGROUP_WRITEBACK
1408 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
1409 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1410 unsigned long *pheadroom, unsigned long *pdirty,
1411 unsigned long *pwriteback);
1413 void mem_cgroup_track_foreign_dirty_slowpath(struct page *page,
1414 struct bdi_writeback *wb);
1416 static inline void mem_cgroup_track_foreign_dirty(struct page *page,
1417 struct bdi_writeback *wb)
1419 if (mem_cgroup_disabled())
1422 if (unlikely(&page->mem_cgroup->css != wb->memcg_css))
1423 mem_cgroup_track_foreign_dirty_slowpath(page, wb);
1426 void mem_cgroup_flush_foreign(struct bdi_writeback *wb);
1428 #else /* CONFIG_CGROUP_WRITEBACK */
1430 static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1435 static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
1436 unsigned long *pfilepages,
1437 unsigned long *pheadroom,
1438 unsigned long *pdirty,
1439 unsigned long *pwriteback)
1443 static inline void mem_cgroup_track_foreign_dirty(struct page *page,
1444 struct bdi_writeback *wb)
1448 static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
1452 #endif /* CONFIG_CGROUP_WRITEBACK */
1455 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1456 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1458 extern struct static_key_false memcg_sockets_enabled_key;
1459 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
1460 void mem_cgroup_sk_alloc(struct sock *sk);
1461 void mem_cgroup_sk_free(struct sock *sk);
1462 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1464 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure)
1467 if (time_before(jiffies, memcg->socket_pressure))
1469 } while ((memcg = parent_mem_cgroup(memcg)));
1473 extern int memcg_expand_shrinker_maps(int new_id);
1475 extern void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1476 int nid, int shrinker_id);
1478 #define mem_cgroup_sockets_enabled 0
1479 static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1480 static inline void mem_cgroup_sk_free(struct sock *sk) { };
1481 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1486 static inline void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1487 int nid, int shrinker_id)
1492 #ifdef CONFIG_MEMCG_KMEM
1493 int __memcg_kmem_charge(struct mem_cgroup *memcg, gfp_t gfp,
1494 unsigned int nr_pages);
1495 void __memcg_kmem_uncharge(struct mem_cgroup *memcg, unsigned int nr_pages);
1496 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order);
1497 void __memcg_kmem_uncharge_page(struct page *page, int order);
1499 struct obj_cgroup *get_obj_cgroup_from_current(void);
1501 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size);
1502 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size);
1504 extern struct static_key_false memcg_kmem_enabled_key;
1506 extern int memcg_nr_cache_ids;
1507 void memcg_get_cache_ids(void);
1508 void memcg_put_cache_ids(void);
1511 * Helper macro to loop through all memcg-specific caches. Callers must still
1512 * check if the cache is valid (it is either valid or NULL).
1513 * the slab_mutex must be held when looping through those caches
1515 #define for_each_memcg_cache_index(_idx) \
1516 for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
1518 static inline bool memcg_kmem_enabled(void)
1520 return static_branch_likely(&memcg_kmem_enabled_key);
1523 static inline bool memcg_kmem_bypass(void)
1528 /* Allow remote memcg charging in kthread contexts. */
1529 if ((!current->mm || (current->flags & PF_KTHREAD)) &&
1530 !current->active_memcg)
1535 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1538 if (memcg_kmem_enabled())
1539 return __memcg_kmem_charge_page(page, gfp, order);
1543 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1545 if (memcg_kmem_enabled())
1546 __memcg_kmem_uncharge_page(page, order);
1549 static inline int memcg_kmem_charge(struct mem_cgroup *memcg, gfp_t gfp,
1550 unsigned int nr_pages)
1552 if (memcg_kmem_enabled())
1553 return __memcg_kmem_charge(memcg, gfp, nr_pages);
1557 static inline void memcg_kmem_uncharge(struct mem_cgroup *memcg,
1558 unsigned int nr_pages)
1560 if (memcg_kmem_enabled())
1561 __memcg_kmem_uncharge(memcg, nr_pages);
1565 * helper for accessing a memcg's index. It will be used as an index in the
1566 * child cache array in kmem_cache, and also to derive its name. This function
1567 * will return -1 when this is not a kmem-limited memcg.
1569 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1571 return memcg ? memcg->kmemcg_id : -1;
1574 struct mem_cgroup *mem_cgroup_from_obj(void *p);
1578 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1584 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1588 static inline int __memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1594 static inline void __memcg_kmem_uncharge_page(struct page *page, int order)
1598 #define for_each_memcg_cache_index(_idx) \
1601 static inline bool memcg_kmem_enabled(void)
1606 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1611 static inline void memcg_get_cache_ids(void)
1615 static inline void memcg_put_cache_ids(void)
1619 static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)
1624 #endif /* CONFIG_MEMCG_KMEM */
1626 #endif /* _LINUX_MEMCONTROL_H */