xref: /linux-6.15/include/linux/alloc_tag.h (revision 1438d349)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * allocation tagging
4  */
5 #ifndef _LINUX_ALLOC_TAG_H
6 #define _LINUX_ALLOC_TAG_H
7 
8 #include <linux/bug.h>
9 #include <linux/codetag.h>
10 #include <linux/container_of.h>
11 #include <linux/preempt.h>
12 #include <asm/percpu.h>
13 #include <linux/cpumask.h>
14 #include <linux/static_key.h>
15 #include <linux/irqflags.h>
16 
17 struct alloc_tag_counters {
18 	u64 bytes;
19 	u64 calls;
20 };
21 
22 /*
23  * An instance of this structure is created in a special ELF section at every
24  * allocation callsite. At runtime, the special section is treated as
25  * an array of these. Embedded codetag utilizes codetag framework.
26  */
27 struct alloc_tag {
28 	struct codetag			ct;
29 	struct alloc_tag_counters __percpu	*counters;
30 } __aligned(8);
31 
32 #ifdef CONFIG_MEM_ALLOC_PROFILING
33 
34 struct codetag_bytes {
35 	struct codetag *ct;
36 	s64 bytes;
37 };
38 
39 size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sleep);
40 
41 static inline struct alloc_tag *ct_to_alloc_tag(struct codetag *ct)
42 {
43 	return container_of(ct, struct alloc_tag, ct);
44 }
45 
46 #ifdef ARCH_NEEDS_WEAK_PER_CPU
47 /*
48  * When percpu variables are required to be defined as weak, static percpu
49  * variables can't be used inside a function (see comments for DECLARE_PER_CPU_SECTION).
50  * Instead we will accound all module allocations to a single counter.
51  */
52 DECLARE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag);
53 
54 #define DEFINE_ALLOC_TAG(_alloc_tag)						\
55 	static struct alloc_tag _alloc_tag __used __aligned(8)			\
56 	__section("alloc_tags") = {						\
57 		.ct = CODE_TAG_INIT,						\
58 		.counters = &_shared_alloc_tag };
59 
60 #else /* ARCH_NEEDS_WEAK_PER_CPU */
61 
62 #define DEFINE_ALLOC_TAG(_alloc_tag)						\
63 	static DEFINE_PER_CPU(struct alloc_tag_counters, _alloc_tag_cntr);	\
64 	static struct alloc_tag _alloc_tag __used __aligned(8)			\
65 	__section("alloc_tags") = {						\
66 		.ct = CODE_TAG_INIT,						\
67 		.counters = &_alloc_tag_cntr };
68 
69 #endif /* ARCH_NEEDS_WEAK_PER_CPU */
70 
71 DECLARE_STATIC_KEY_MAYBE(CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT,
72 			mem_alloc_profiling_key);
73 
74 static inline bool mem_alloc_profiling_enabled(void)
75 {
76 	return static_branch_maybe(CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT,
77 				   &mem_alloc_profiling_key);
78 }
79 
80 static inline struct alloc_tag_counters alloc_tag_read(struct alloc_tag *tag)
81 {
82 	struct alloc_tag_counters v = { 0, 0 };
83 	struct alloc_tag_counters *counter;
84 	int cpu;
85 
86 	for_each_possible_cpu(cpu) {
87 		counter = per_cpu_ptr(tag->counters, cpu);
88 		v.bytes += counter->bytes;
89 		v.calls += counter->calls;
90 	}
91 
92 	return v;
93 }
94 
95 #ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG
96 static inline void alloc_tag_add_check(union codetag_ref *ref, struct alloc_tag *tag)
97 {
98 	WARN_ONCE(ref && ref->ct,
99 		  "alloc_tag was not cleared (got tag for %s:%u)\n",
100 		  ref->ct->filename, ref->ct->lineno);
101 
102 	WARN_ONCE(!tag, "current->alloc_tag not set");
103 }
104 
105 static inline void alloc_tag_sub_check(union codetag_ref *ref)
106 {
107 	WARN_ONCE(ref && !ref->ct, "alloc_tag was not set\n");
108 }
109 #else
110 static inline void alloc_tag_add_check(union codetag_ref *ref, struct alloc_tag *tag) {}
111 static inline void alloc_tag_sub_check(union codetag_ref *ref) {}
112 #endif
113 
114 /* Caller should verify both ref and tag to be valid */
115 static inline void __alloc_tag_ref_set(union codetag_ref *ref, struct alloc_tag *tag)
116 {
117 	ref->ct = &tag->ct;
118 	/*
119 	 * We need in increment the call counter every time we have a new
120 	 * allocation or when we split a large allocation into smaller ones.
121 	 * Each new reference for every sub-allocation needs to increment call
122 	 * counter because when we free each part the counter will be decremented.
123 	 */
124 	this_cpu_inc(tag->counters->calls);
125 }
126 
127 static inline void alloc_tag_ref_set(union codetag_ref *ref, struct alloc_tag *tag)
128 {
129 	alloc_tag_add_check(ref, tag);
130 	if (!ref || !tag)
131 		return;
132 
133 	__alloc_tag_ref_set(ref, tag);
134 }
135 
136 static inline void alloc_tag_add(union codetag_ref *ref, struct alloc_tag *tag, size_t bytes)
137 {
138 	alloc_tag_add_check(ref, tag);
139 	if (!ref || !tag)
140 		return;
141 
142 	__alloc_tag_ref_set(ref, tag);
143 	this_cpu_add(tag->counters->bytes, bytes);
144 }
145 
146 static inline void alloc_tag_sub(union codetag_ref *ref, size_t bytes)
147 {
148 	struct alloc_tag *tag;
149 
150 	alloc_tag_sub_check(ref);
151 	if (!ref || !ref->ct)
152 		return;
153 
154 	tag = ct_to_alloc_tag(ref->ct);
155 
156 	this_cpu_sub(tag->counters->bytes, bytes);
157 	this_cpu_dec(tag->counters->calls);
158 
159 	ref->ct = NULL;
160 }
161 
162 #define alloc_tag_record(p)	((p) = current->alloc_tag)
163 
164 #else /* CONFIG_MEM_ALLOC_PROFILING */
165 
166 #define DEFINE_ALLOC_TAG(_alloc_tag)
167 static inline bool mem_alloc_profiling_enabled(void) { return false; }
168 static inline void alloc_tag_add(union codetag_ref *ref, struct alloc_tag *tag,
169 				 size_t bytes) {}
170 static inline void alloc_tag_sub(union codetag_ref *ref, size_t bytes) {}
171 #define alloc_tag_record(p)	do {} while (0)
172 
173 #endif /* CONFIG_MEM_ALLOC_PROFILING */
174 
175 #define alloc_hooks_tag(_tag, _do_alloc)				\
176 ({									\
177 	struct alloc_tag * __maybe_unused _old = alloc_tag_save(_tag);	\
178 	typeof(_do_alloc) _res = _do_alloc;				\
179 	alloc_tag_restore(_tag, _old);					\
180 	_res;								\
181 })
182 
183 #define alloc_hooks(_do_alloc)						\
184 ({									\
185 	DEFINE_ALLOC_TAG(_alloc_tag);					\
186 	alloc_hooks_tag(&_alloc_tag, _do_alloc);			\
187 })
188 
189 #endif /* _LINUX_ALLOC_TAG_H */
190