1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/codetag.h>
4 #include <linux/kallsyms.h>
5 #include <linux/module.h>
6 #include <linux/seq_buf.h>
7 #include <linux/slab.h>
8 #include <linux/vmalloc.h>
11 struct list_head link
;
14 struct rw_semaphore mod_lock
; /* protects mod_idr */
15 struct codetag_type_desc desc
;
18 struct codetag_range
{
19 struct codetag
*start
;
23 struct codetag_module
{
25 struct codetag_range range
;
28 static DEFINE_MUTEX(codetag_lock
);
29 static LIST_HEAD(codetag_types
);
31 void codetag_lock_module_list(struct codetag_type
*cttype
, bool lock
)
34 down_read(&cttype
->mod_lock
);
36 up_read(&cttype
->mod_lock
);
39 bool codetag_trylock_module_list(struct codetag_type
*cttype
)
41 return down_read_trylock(&cttype
->mod_lock
) != 0;
44 struct codetag_iterator
codetag_get_ct_iter(struct codetag_type
*cttype
)
46 struct codetag_iterator iter
= {
56 static inline struct codetag
*get_first_module_ct(struct codetag_module
*cmod
)
58 return cmod
->range
.start
< cmod
->range
.stop
? cmod
->range
.start
: NULL
;
62 struct codetag
*get_next_module_ct(struct codetag_iterator
*iter
)
64 struct codetag
*res
= (struct codetag
*)
65 ((char *)iter
->ct
+ iter
->cttype
->desc
.tag_size
);
67 return res
< iter
->cmod
->range
.stop
? res
: NULL
;
70 struct codetag
*codetag_next_ct(struct codetag_iterator
*iter
)
72 struct codetag_type
*cttype
= iter
->cttype
;
73 struct codetag_module
*cmod
;
76 lockdep_assert_held(&cttype
->mod_lock
);
78 if (unlikely(idr_is_empty(&cttype
->mod_idr
)))
83 cmod
= idr_find(&cttype
->mod_idr
, iter
->mod_id
);
85 /* If module was removed move to the next one */
87 cmod
= idr_get_next_ul(&cttype
->mod_idr
,
90 /* Exit if no more modules */
94 if (cmod
!= iter
->cmod
) {
96 ct
= get_first_module_ct(cmod
);
98 ct
= get_next_module_ct(iter
);
110 void codetag_to_text(struct seq_buf
*out
, struct codetag
*ct
)
113 seq_buf_printf(out
, "%s:%u [%s] func:%s",
114 ct
->filename
, ct
->lineno
,
115 ct
->modname
, ct
->function
);
117 seq_buf_printf(out
, "%s:%u func:%s",
118 ct
->filename
, ct
->lineno
, ct
->function
);
121 static inline size_t range_size(const struct codetag_type
*cttype
,
122 const struct codetag_range
*range
)
124 return ((char *)range
->stop
- (char *)range
->start
) /
125 cttype
->desc
.tag_size
;
128 static void *get_symbol(struct module
*mod
, const char *prefix
, const char *name
)
130 DECLARE_SEQ_BUF(sb
, KSYM_NAME_LEN
);
134 seq_buf_printf(&sb
, "%s%s", prefix
, name
);
135 if (seq_buf_has_overflowed(&sb
))
138 buf
= seq_buf_str(&sb
);
141 (void *)find_kallsyms_symbol_value(mod
, buf
) :
142 (void *)kallsyms_lookup_name(buf
);
148 static struct codetag_range
get_section_range(struct module
*mod
,
151 return (struct codetag_range
) {
152 get_symbol(mod
, CODETAG_SECTION_START_PREFIX
, section
),
153 get_symbol(mod
, CODETAG_SECTION_STOP_PREFIX
, section
),
157 static const char *get_mod_name(__maybe_unused
struct module
*mod
)
159 #ifdef CONFIG_MODULES
166 static int codetag_module_init(struct codetag_type
*cttype
, struct module
*mod
)
168 struct codetag_range range
;
169 struct codetag_module
*cmod
;
172 range
= get_section_range(mod
, cttype
->desc
.section
);
173 if (!range
.start
|| !range
.stop
) {
174 pr_warn("Failed to load code tags of type %s from the module %s\n",
175 cttype
->desc
.section
, get_mod_name(mod
));
179 /* Ignore empty ranges */
180 if (range
.start
== range
.stop
)
183 BUG_ON(range
.start
> range
.stop
);
185 cmod
= kmalloc(sizeof(*cmod
), GFP_KERNEL
);
192 down_write(&cttype
->mod_lock
);
193 err
= idr_alloc(&cttype
->mod_idr
, cmod
, 0, 0, GFP_KERNEL
);
195 cttype
->count
+= range_size(cttype
, &range
);
196 if (cttype
->desc
.module_load
)
197 cttype
->desc
.module_load(cttype
, cmod
);
199 up_write(&cttype
->mod_lock
);
209 #ifdef CONFIG_MODULES
210 #define CODETAG_SECTION_PREFIX ".codetag."
212 /* Some codetag types need a separate module section */
213 bool codetag_needs_module_section(struct module
*mod
, const char *name
,
216 const char *type_name
;
217 struct codetag_type
*cttype
;
220 if (strncmp(name
, CODETAG_SECTION_PREFIX
, strlen(CODETAG_SECTION_PREFIX
)))
223 type_name
= name
+ strlen(CODETAG_SECTION_PREFIX
);
224 mutex_lock(&codetag_lock
);
225 list_for_each_entry(cttype
, &codetag_types
, link
) {
226 if (strcmp(type_name
, cttype
->desc
.section
) == 0) {
227 if (!cttype
->desc
.needs_section_mem
)
230 down_write(&cttype
->mod_lock
);
231 ret
= cttype
->desc
.needs_section_mem(mod
, size
);
232 up_write(&cttype
->mod_lock
);
236 mutex_unlock(&codetag_lock
);
241 void *codetag_alloc_module_section(struct module
*mod
, const char *name
,
242 unsigned long size
, unsigned int prepend
,
245 const char *type_name
= name
+ strlen(CODETAG_SECTION_PREFIX
);
246 struct codetag_type
*cttype
;
247 void *ret
= ERR_PTR(-EINVAL
);
249 mutex_lock(&codetag_lock
);
250 list_for_each_entry(cttype
, &codetag_types
, link
) {
251 if (strcmp(type_name
, cttype
->desc
.section
) == 0) {
252 if (WARN_ON(!cttype
->desc
.alloc_section_mem
))
255 down_write(&cttype
->mod_lock
);
256 ret
= cttype
->desc
.alloc_section_mem(mod
, size
, prepend
, align
);
257 up_write(&cttype
->mod_lock
);
261 mutex_unlock(&codetag_lock
);
266 void codetag_free_module_sections(struct module
*mod
)
268 struct codetag_type
*cttype
;
270 mutex_lock(&codetag_lock
);
271 list_for_each_entry(cttype
, &codetag_types
, link
) {
272 if (!cttype
->desc
.free_section_mem
)
275 down_write(&cttype
->mod_lock
);
276 cttype
->desc
.free_section_mem(mod
, false);
277 up_write(&cttype
->mod_lock
);
279 mutex_unlock(&codetag_lock
);
282 void codetag_module_replaced(struct module
*mod
, struct module
*new_mod
)
284 struct codetag_type
*cttype
;
286 mutex_lock(&codetag_lock
);
287 list_for_each_entry(cttype
, &codetag_types
, link
) {
288 if (!cttype
->desc
.module_replaced
)
291 down_write(&cttype
->mod_lock
);
292 cttype
->desc
.module_replaced(mod
, new_mod
);
293 up_write(&cttype
->mod_lock
);
295 mutex_unlock(&codetag_lock
);
298 void codetag_load_module(struct module
*mod
)
300 struct codetag_type
*cttype
;
305 mutex_lock(&codetag_lock
);
306 list_for_each_entry(cttype
, &codetag_types
, link
)
307 codetag_module_init(cttype
, mod
);
308 mutex_unlock(&codetag_lock
);
311 void codetag_unload_module(struct module
*mod
)
313 struct codetag_type
*cttype
;
318 /* await any module's kfree_rcu() operations to complete */
319 kvfree_rcu_barrier();
321 mutex_lock(&codetag_lock
);
322 list_for_each_entry(cttype
, &codetag_types
, link
) {
323 struct codetag_module
*found
= NULL
;
324 struct codetag_module
*cmod
;
325 unsigned long mod_id
, tmp
;
327 down_write(&cttype
->mod_lock
);
328 idr_for_each_entry_ul(&cttype
->mod_idr
, cmod
, tmp
, mod_id
) {
329 if (cmod
->mod
&& cmod
->mod
== mod
) {
335 if (cttype
->desc
.module_unload
)
336 cttype
->desc
.module_unload(cttype
, cmod
);
338 cttype
->count
-= range_size(cttype
, &cmod
->range
);
339 idr_remove(&cttype
->mod_idr
, mod_id
);
342 up_write(&cttype
->mod_lock
);
343 if (found
&& cttype
->desc
.free_section_mem
)
344 cttype
->desc
.free_section_mem(mod
, true);
346 mutex_unlock(&codetag_lock
);
348 #endif /* CONFIG_MODULES */
350 struct codetag_type
*
351 codetag_register_type(const struct codetag_type_desc
*desc
)
353 struct codetag_type
*cttype
;
356 BUG_ON(desc
->tag_size
<= 0);
358 cttype
= kzalloc(sizeof(*cttype
), GFP_KERNEL
);
359 if (unlikely(!cttype
))
360 return ERR_PTR(-ENOMEM
);
362 cttype
->desc
= *desc
;
363 idr_init(&cttype
->mod_idr
);
364 init_rwsem(&cttype
->mod_lock
);
366 err
= codetag_module_init(cttype
, NULL
);
372 mutex_lock(&codetag_lock
);
373 list_add_tail(&cttype
->link
, &codetag_types
);
374 mutex_unlock(&codetag_lock
);