xref: /linux/mm/memcontrol-v1.h (revision 9557b4376d02088a33e5f4116bcc324d35a3b64c)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #ifndef __MM_MEMCONTROL_V1_H
4 #define __MM_MEMCONTROL_V1_H
5 
6 #include <linux/cgroup-defs.h>
7 
8 /* Cgroup v1 and v2 common declarations */
9 
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);
13 
14 static inline int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
15 			     unsigned int nr_pages)
16 {
17 	if (mem_cgroup_is_root(memcg))
18 		return 0;
19 
20 	return try_charge_memcg(memcg, gfp_mask, nr_pages);
21 }
22 
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);
25 
26 /*
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.
30  */
31 #define for_each_mem_cgroup_tree(iter, root)		\
32 	for (iter = mem_cgroup_iter(root, NULL, NULL);	\
33 	     iter != NULL;				\
34 	     iter = mem_cgroup_iter(root, iter, NULL))
35 
36 #define for_each_mem_cgroup(iter)			\
37 	for (iter = mem_cgroup_iter(NULL, NULL, NULL);	\
38 	     iter != NULL;				\
39 	     iter = mem_cgroup_iter(NULL, iter, NULL))
40 
41 /* Whether legacy memory+swap accounting is active */
42 static bool do_memsw_account(void)
43 {
44 	return !cgroup_subsys_on_dfl(memory_cgrp_subsys);
45 }
46 
47 /*
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.
52  */
53 enum mem_cgroup_events_target {
54 	MEM_CGROUP_TARGET_THRESH,
55 	MEM_CGROUP_TARGET_SOFTLIMIT,
56 	MEM_CGROUP_NTARGETS,
57 };
58 
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);
62 
63 void drain_all_stock(struct mem_cgroup *root_memcg);
64 
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);
71 
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);
76 
77 static inline void memcg1_soft_limit_reset(struct mem_cgroup *memcg)
78 {
79 	WRITE_ONCE(memcg->soft_limit, PAGE_COUNTER_MAX);
80 }
81 
82 bool memcg1_wait_acct_move(struct mem_cgroup *memcg);
83 
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);
89 
90 /* for encoding cft->private value on file */
91 enum res_type {
92 	_MEM,
93 	_MEMSWAP,
94 	_KMEM,
95 	_TCP,
96 };
97 
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);
101 
102 void memcg1_check_events(struct mem_cgroup *memcg, int nid);
103 
104 void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s);
105 
106 void memcg1_account_kmem(struct mem_cgroup *memcg, int nr_pages);
107 static inline bool memcg1_tcpmem_active(struct mem_cgroup *memcg)
108 {
109 	return memcg->tcpmem_active;
110 }
111 bool memcg1_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
112 			 gfp_t gfp_mask);
113 static inline void memcg1_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
114 {
115 	page_counter_uncharge(&memcg->tcpmem, nr_pages);
116 }
117 
118 extern struct cftype memsw_files[];
119 extern struct cftype mem_cgroup_legacy_files[];
120 
121 #else	/* CONFIG_MEMCG_V1 */
122 
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) {}
128 
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) {}
132 
133 static inline void memcg1_check_events(struct mem_cgroup *memcg, int nid) {}
134 
135 static inline void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) {}
136 
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) {}
142 
143 extern struct cftype memsw_files[];
144 extern struct cftype mem_cgroup_legacy_files[];
145 #endif	/* CONFIG_MEMCG_V1 */
146 
147 #endif	/* __MM_MEMCONTROL_V1_H */
148