1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 #ifndef __MM_MEMCONTROL_V1_H
4 #define __MM_MEMCONTROL_V1_H
6 #include <linux/cgroup-defs.h>
8 /* Cgroup v1 and v2 common declarations */
10 void mem_cgroup_charge_statistics(struct mem_cgroup *memcg, int nr_pages);
11 int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
12 unsigned int nr_pages);
14 static inline int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
15 unsigned int nr_pages)
17 if (mem_cgroup_is_root(memcg))
20 return try_charge_memcg(memcg, gfp_mask, nr_pages);
23 void mem_cgroup_id_get_many(struct mem_cgroup *memcg, unsigned int n);
24 void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n);
27 * Iteration constructs for visiting all cgroups (under a tree). If
28 * loops are exited prematurely (break), mem_cgroup_iter_break() must
29 * be used for reference counting.
31 #define for_each_mem_cgroup_tree(iter, root) \
32 for (iter = mem_cgroup_iter(root, NULL, NULL); \
34 iter = mem_cgroup_iter(root, iter, NULL))
36 #define for_each_mem_cgroup(iter) \
37 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
39 iter = mem_cgroup_iter(NULL, iter, NULL))
41 /* Whether legacy memory+swap accounting is active */
42 static bool do_memsw_account(void)
44 return !cgroup_subsys_on_dfl(memory_cgrp_subsys);
48 * Per memcg event counter is incremented at every pagein/pageout. With THP,
49 * it will be incremented by the number of pages. This counter is used
50 * to trigger some periodic events. This is straightforward and better
51 * than using jiffies etc. to handle periodic memcg event.
53 enum mem_cgroup_events_target {
54 MEM_CGROUP_TARGET_THRESH,
55 MEM_CGROUP_TARGET_SOFTLIMIT,
59 bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
60 enum mem_cgroup_events_target target);
61 unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap);
63 void drain_all_stock(struct mem_cgroup *root_memcg);
65 unsigned long memcg_events(struct mem_cgroup *memcg, int event);
66 unsigned long memcg_events_local(struct mem_cgroup *memcg, int event);
67 unsigned long memcg_page_state_local(struct mem_cgroup *memcg, int idx);
68 unsigned long memcg_page_state_output(struct mem_cgroup *memcg, int item);
69 unsigned long memcg_page_state_local_output(struct mem_cgroup *memcg, int item);
70 int memory_stat_show(struct seq_file *m, void *v);
72 /* Cgroup v1-specific declarations */
73 #ifdef CONFIG_MEMCG_V1
74 void memcg1_memcg_init(struct mem_cgroup *memcg);
75 void memcg1_remove_from_trees(struct mem_cgroup *memcg);
77 static inline void memcg1_soft_limit_reset(struct mem_cgroup *memcg)
79 WRITE_ONCE(memcg->soft_limit, PAGE_COUNTER_MAX);
82 bool memcg1_wait_acct_move(struct mem_cgroup *memcg);
84 struct cgroup_taskset;
85 int memcg1_can_attach(struct cgroup_taskset *tset);
86 void memcg1_cancel_attach(struct cgroup_taskset *tset);
87 void memcg1_move_task(void);
88 void memcg1_css_offline(struct mem_cgroup *memcg);
90 /* for encoding cft->private value on file */
98 bool memcg1_oom_prepare(struct mem_cgroup *memcg, bool *locked);
99 void memcg1_oom_finish(struct mem_cgroup *memcg, bool locked);
100 void memcg1_oom_recover(struct mem_cgroup *memcg);
102 void memcg1_check_events(struct mem_cgroup *memcg, int nid);
104 void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s);
106 void memcg1_account_kmem(struct mem_cgroup *memcg, int nr_pages);
107 static inline bool memcg1_tcpmem_active(struct mem_cgroup *memcg)
109 return memcg->tcpmem_active;
111 bool memcg1_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
113 static inline void memcg1_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
115 page_counter_uncharge(&memcg->tcpmem, nr_pages);
118 extern struct cftype memsw_files[];
119 extern struct cftype mem_cgroup_legacy_files[];
121 #else /* CONFIG_MEMCG_V1 */
123 static inline void memcg1_memcg_init(struct mem_cgroup *memcg) {}
124 static inline void memcg1_remove_from_trees(struct mem_cgroup *memcg) {}
125 static inline void memcg1_soft_limit_reset(struct mem_cgroup *memcg) {}
126 static inline bool memcg1_wait_acct_move(struct mem_cgroup *memcg) { return false; }
127 static inline void memcg1_css_offline(struct mem_cgroup *memcg) {}
129 static inline bool memcg1_oom_prepare(struct mem_cgroup *memcg, bool *locked) { return true; }
130 static inline void memcg1_oom_finish(struct mem_cgroup *memcg, bool locked) {}
131 static inline void memcg1_oom_recover(struct mem_cgroup *memcg) {}
133 static inline void memcg1_check_events(struct mem_cgroup *memcg, int nid) {}
135 static inline void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) {}
137 static inline void memcg1_account_kmem(struct mem_cgroup *memcg, int nr_pages) {}
138 static inline bool memcg1_tcpmem_active(struct mem_cgroup *memcg) { return false; }
139 static inline bool memcg1_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
140 gfp_t gfp_mask) { return true; }
141 static inline void memcg1_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) {}
143 extern struct cftype memsw_files[];
144 extern struct cftype mem_cgroup_legacy_files[];
145 #endif /* CONFIG_MEMCG_V1 */
147 #endif /* __MM_MEMCONTROL_V1_H */