xref: /linux/mm/memcontrol-v1.h (revision f2c5101b)
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 int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
11 		     unsigned int nr_pages);
12 
try_charge(struct mem_cgroup * memcg,gfp_t gfp_mask,unsigned int nr_pages)13 static inline int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
14 			     unsigned int nr_pages)
15 {
16 	if (mem_cgroup_is_root(memcg))
17 		return 0;
18 
19 	return try_charge_memcg(memcg, gfp_mask, nr_pages);
20 }
21 
22 void mem_cgroup_id_get_many(struct mem_cgroup *memcg, unsigned int n);
23 void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n);
24 
25 /*
26  * Iteration constructs for visiting all cgroups (under a tree).  If
27  * loops are exited prematurely (break), mem_cgroup_iter_break() must
28  * be used for reference counting.
29  */
30 #define for_each_mem_cgroup_tree(iter, root)		\
31 	for (iter = mem_cgroup_iter(root, NULL, NULL);	\
32 	     iter != NULL;				\
33 	     iter = mem_cgroup_iter(root, iter, NULL))
34 
35 #define for_each_mem_cgroup(iter)			\
36 	for (iter = mem_cgroup_iter(NULL, NULL, NULL);	\
37 	     iter != NULL;				\
38 	     iter = mem_cgroup_iter(NULL, iter, NULL))
39 
40 /* Whether legacy memory+swap accounting is active */
do_memsw_account(void)41 static bool do_memsw_account(void)
42 {
43 	return !cgroup_subsys_on_dfl(memory_cgrp_subsys);
44 }
45 
46 /*
47  * Per memcg event counter is incremented at every pagein/pageout. With THP,
48  * it will be incremented by the number of pages. This counter is used
49  * to trigger some periodic events. This is straightforward and better
50  * than using jiffies etc. to handle periodic memcg event.
51  */
52 enum mem_cgroup_events_target {
53 	MEM_CGROUP_TARGET_THRESH,
54 	MEM_CGROUP_TARGET_SOFTLIMIT,
55 	MEM_CGROUP_NTARGETS,
56 };
57 
58 unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap);
59 
60 void drain_all_stock(struct mem_cgroup *root_memcg);
61 
62 unsigned long memcg_events(struct mem_cgroup *memcg, int event);
63 unsigned long memcg_events_local(struct mem_cgroup *memcg, int event);
64 unsigned long memcg_page_state_local(struct mem_cgroup *memcg, int idx);
65 unsigned long memcg_page_state_output(struct mem_cgroup *memcg, int item);
66 unsigned long memcg_page_state_local_output(struct mem_cgroup *memcg, int item);
67 int memory_stat_show(struct seq_file *m, void *v);
68 
69 /* Cgroup v1-specific declarations */
70 #ifdef CONFIG_MEMCG_V1
71 
72 bool memcg1_alloc_events(struct mem_cgroup *memcg);
73 void memcg1_free_events(struct mem_cgroup *memcg);
74 
75 void memcg1_memcg_init(struct mem_cgroup *memcg);
76 void memcg1_remove_from_trees(struct mem_cgroup *memcg);
77 
memcg1_soft_limit_reset(struct mem_cgroup * memcg)78 static inline void memcg1_soft_limit_reset(struct mem_cgroup *memcg)
79 {
80 	WRITE_ONCE(memcg->soft_limit, PAGE_COUNTER_MAX);
81 }
82 
83 bool memcg1_wait_acct_move(struct mem_cgroup *memcg);
84 
85 struct cgroup_taskset;
86 int memcg1_can_attach(struct cgroup_taskset *tset);
87 void memcg1_cancel_attach(struct cgroup_taskset *tset);
88 void memcg1_move_task(void);
89 void memcg1_css_offline(struct mem_cgroup *memcg);
90 
91 /* for encoding cft->private value on file */
92 enum res_type {
93 	_MEM,
94 	_MEMSWAP,
95 	_KMEM,
96 	_TCP,
97 };
98 
99 bool memcg1_oom_prepare(struct mem_cgroup *memcg, bool *locked);
100 void memcg1_oom_finish(struct mem_cgroup *memcg, bool locked);
101 void memcg1_oom_recover(struct mem_cgroup *memcg);
102 
103 void memcg1_commit_charge(struct folio *folio, struct mem_cgroup *memcg);
104 void memcg1_swapout(struct folio *folio, struct mem_cgroup *memcg);
105 void memcg1_uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,
106 			   unsigned long nr_memory, int nid);
107 
108 void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s);
109 
110 void memcg1_account_kmem(struct mem_cgroup *memcg, int nr_pages);
memcg1_tcpmem_active(struct mem_cgroup * memcg)111 static inline bool memcg1_tcpmem_active(struct mem_cgroup *memcg)
112 {
113 	return memcg->tcpmem_active;
114 }
115 bool memcg1_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
116 			 gfp_t gfp_mask);
memcg1_uncharge_skmem(struct mem_cgroup * memcg,unsigned int nr_pages)117 static inline void memcg1_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
118 {
119 	page_counter_uncharge(&memcg->tcpmem, nr_pages);
120 }
121 
122 extern struct cftype memsw_files[];
123 extern struct cftype mem_cgroup_legacy_files[];
124 
125 #else	/* CONFIG_MEMCG_V1 */
126 
memcg1_alloc_events(struct mem_cgroup * memcg)127 static inline bool memcg1_alloc_events(struct mem_cgroup *memcg) { return true; }
memcg1_free_events(struct mem_cgroup * memcg)128 static inline void memcg1_free_events(struct mem_cgroup *memcg) {}
129 
memcg1_memcg_init(struct mem_cgroup * memcg)130 static inline void memcg1_memcg_init(struct mem_cgroup *memcg) {}
memcg1_remove_from_trees(struct mem_cgroup * memcg)131 static inline void memcg1_remove_from_trees(struct mem_cgroup *memcg) {}
memcg1_soft_limit_reset(struct mem_cgroup * memcg)132 static inline void memcg1_soft_limit_reset(struct mem_cgroup *memcg) {}
memcg1_wait_acct_move(struct mem_cgroup * memcg)133 static inline bool memcg1_wait_acct_move(struct mem_cgroup *memcg) { return false; }
memcg1_css_offline(struct mem_cgroup * memcg)134 static inline void memcg1_css_offline(struct mem_cgroup *memcg) {}
135 
memcg1_oom_prepare(struct mem_cgroup * memcg,bool * locked)136 static inline bool memcg1_oom_prepare(struct mem_cgroup *memcg, bool *locked) { return true; }
memcg1_oom_finish(struct mem_cgroup * memcg,bool locked)137 static inline void memcg1_oom_finish(struct mem_cgroup *memcg, bool locked) {}
memcg1_oom_recover(struct mem_cgroup * memcg)138 static inline void memcg1_oom_recover(struct mem_cgroup *memcg) {}
139 
memcg1_commit_charge(struct folio * folio,struct mem_cgroup * memcg)140 static inline void memcg1_commit_charge(struct folio *folio,
141 					struct mem_cgroup *memcg) {}
142 
memcg1_swapout(struct folio * folio,struct mem_cgroup * memcg)143 static inline void memcg1_swapout(struct folio *folio, struct mem_cgroup *memcg) {}
144 
memcg1_uncharge_batch(struct mem_cgroup * memcg,unsigned long pgpgout,unsigned long nr_memory,int nid)145 static inline void memcg1_uncharge_batch(struct mem_cgroup *memcg,
146 					 unsigned long pgpgout,
147 					 unsigned long nr_memory, int nid) {}
148 
memcg1_stat_format(struct mem_cgroup * memcg,struct seq_buf * s)149 static inline void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) {}
150 
memcg1_account_kmem(struct mem_cgroup * memcg,int nr_pages)151 static inline void memcg1_account_kmem(struct mem_cgroup *memcg, int nr_pages) {}
memcg1_tcpmem_active(struct mem_cgroup * memcg)152 static inline bool memcg1_tcpmem_active(struct mem_cgroup *memcg) { return false; }
memcg1_charge_skmem(struct mem_cgroup * memcg,unsigned int nr_pages,gfp_t gfp_mask)153 static inline bool memcg1_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
154 				       gfp_t gfp_mask) { return true; }
memcg1_uncharge_skmem(struct mem_cgroup * memcg,unsigned int nr_pages)155 static inline void memcg1_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) {}
156 
157 #endif	/* CONFIG_MEMCG_V1 */
158 
159 #endif	/* __MM_MEMCONTROL_V1_H */
160