xref: /linux/mm/memcontrol-v1.h (revision 553c89ec31746ff96fc5562943fe5b1c9b1e9276)
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 inline 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 struct cgroup_taskset;
84 void memcg1_css_offline(struct mem_cgroup *memcg);
85 
86 /* for encoding cft->private value on file */
87 enum res_type {
88 	_MEM,
89 	_MEMSWAP,
90 	_KMEM,
91 	_TCP,
92 };
93 
94 bool memcg1_oom_prepare(struct mem_cgroup *memcg, bool *locked);
95 void memcg1_oom_finish(struct mem_cgroup *memcg, bool locked);
96 void memcg1_oom_recover(struct mem_cgroup *memcg);
97 
98 void memcg1_commit_charge(struct folio *folio, struct mem_cgroup *memcg);
99 void memcg1_swapout(struct folio *folio, struct mem_cgroup *memcg);
100 void memcg1_uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,
101 			   unsigned long nr_memory, int nid);
102 
103 void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s);
104 
105 void memcg1_account_kmem(struct mem_cgroup *memcg, int nr_pages);
memcg1_tcpmem_active(struct mem_cgroup * memcg)106 static inline bool memcg1_tcpmem_active(struct mem_cgroup *memcg)
107 {
108 	return memcg->tcpmem_active;
109 }
110 bool memcg1_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
111 			 gfp_t gfp_mask);
memcg1_uncharge_skmem(struct mem_cgroup * memcg,unsigned int nr_pages)112 static inline void memcg1_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
113 {
114 	page_counter_uncharge(&memcg->tcpmem, nr_pages);
115 }
116 
117 extern struct cftype memsw_files[];
118 extern struct cftype mem_cgroup_legacy_files[];
119 
120 #else	/* CONFIG_MEMCG_V1 */
121 
memcg1_alloc_events(struct mem_cgroup * memcg)122 static inline bool memcg1_alloc_events(struct mem_cgroup *memcg) { return true; }
memcg1_free_events(struct mem_cgroup * memcg)123 static inline void memcg1_free_events(struct mem_cgroup *memcg) {}
124 
memcg1_memcg_init(struct mem_cgroup * memcg)125 static inline void memcg1_memcg_init(struct mem_cgroup *memcg) {}
memcg1_remove_from_trees(struct mem_cgroup * memcg)126 static inline void memcg1_remove_from_trees(struct mem_cgroup *memcg) {}
memcg1_soft_limit_reset(struct mem_cgroup * memcg)127 static inline void memcg1_soft_limit_reset(struct mem_cgroup *memcg) {}
memcg1_css_offline(struct mem_cgroup * memcg)128 static inline void memcg1_css_offline(struct mem_cgroup *memcg) {}
129 
memcg1_oom_prepare(struct mem_cgroup * memcg,bool * locked)130 static inline bool memcg1_oom_prepare(struct mem_cgroup *memcg, bool *locked) { return true; }
memcg1_oom_finish(struct mem_cgroup * memcg,bool locked)131 static inline void memcg1_oom_finish(struct mem_cgroup *memcg, bool locked) {}
memcg1_oom_recover(struct mem_cgroup * memcg)132 static inline void memcg1_oom_recover(struct mem_cgroup *memcg) {}
133 
memcg1_commit_charge(struct folio * folio,struct mem_cgroup * memcg)134 static inline void memcg1_commit_charge(struct folio *folio,
135 					struct mem_cgroup *memcg) {}
136 
memcg1_swapout(struct folio * folio,struct mem_cgroup * memcg)137 static inline void memcg1_swapout(struct folio *folio, struct mem_cgroup *memcg) {}
138 
memcg1_uncharge_batch(struct mem_cgroup * memcg,unsigned long pgpgout,unsigned long nr_memory,int nid)139 static inline void memcg1_uncharge_batch(struct mem_cgroup *memcg,
140 					 unsigned long pgpgout,
141 					 unsigned long nr_memory, int nid) {}
142 
memcg1_stat_format(struct mem_cgroup * memcg,struct seq_buf * s)143 static inline void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) {}
144 
memcg1_account_kmem(struct mem_cgroup * memcg,int nr_pages)145 static inline void memcg1_account_kmem(struct mem_cgroup *memcg, int nr_pages) {}
memcg1_tcpmem_active(struct mem_cgroup * memcg)146 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)147 static inline bool memcg1_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
148 				       gfp_t gfp_mask) { return true; }
memcg1_uncharge_skmem(struct mem_cgroup * memcg,unsigned int nr_pages)149 static inline void memcg1_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) {}
150 
151 #endif	/* CONFIG_MEMCG_V1 */
152 
153 #endif	/* __MM_MEMCONTROL_V1_H */
154