xref: /linux-6.15/include/linux/codetag.h (revision 0db6f8d7)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * code tagging framework
4  */
5 #ifndef _LINUX_CODETAG_H
6 #define _LINUX_CODETAG_H
7 
8 #include <linux/types.h>
9 
10 struct codetag_iterator;
11 struct codetag_type;
12 struct codetag_module;
13 struct seq_buf;
14 struct module;
15 
16 /*
17  * An instance of this structure is created in a special ELF section at every
18  * code location being tagged.  At runtime, the special section is treated as
19  * an array of these.
20  */
21 struct codetag {
22 	unsigned int flags; /* used in later patches */
23 	unsigned int lineno;
24 	const char *modname;
25 	const char *function;
26 	const char *filename;
27 } __aligned(8);
28 
29 union codetag_ref {
30 	struct codetag *ct;
31 };
32 
33 struct codetag_type_desc {
34 	const char *section;
35 	size_t tag_size;
36 	void (*module_load)(struct codetag_type *cttype,
37 			    struct codetag_module *cmod);
38 	void (*module_unload)(struct codetag_type *cttype,
39 			      struct codetag_module *cmod);
40 #ifdef CONFIG_MODULES
41 	void (*module_replaced)(struct module *mod, struct module *new_mod);
42 	bool (*needs_section_mem)(struct module *mod, unsigned long size);
43 	void *(*alloc_section_mem)(struct module *mod, unsigned long size,
44 				   unsigned int prepend, unsigned long align);
45 	void (*free_section_mem)(struct module *mod, bool used);
46 #endif
47 };
48 
49 struct codetag_iterator {
50 	struct codetag_type *cttype;
51 	struct codetag_module *cmod;
52 	unsigned long mod_id;
53 	struct codetag *ct;
54 };
55 
56 #ifdef MODULE
57 #define CT_MODULE_NAME KBUILD_MODNAME
58 #else
59 #define CT_MODULE_NAME NULL
60 #endif
61 
62 #define CODE_TAG_INIT {					\
63 	.modname	= CT_MODULE_NAME,		\
64 	.function	= __func__,			\
65 	.filename	= __FILE__,			\
66 	.lineno		= __LINE__,			\
67 	.flags		= 0,				\
68 }
69 
70 void codetag_lock_module_list(struct codetag_type *cttype, bool lock);
71 bool codetag_trylock_module_list(struct codetag_type *cttype);
72 struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype);
73 struct codetag *codetag_next_ct(struct codetag_iterator *iter);
74 
75 void codetag_to_text(struct seq_buf *out, struct codetag *ct);
76 
77 struct codetag_type *
78 codetag_register_type(const struct codetag_type_desc *desc);
79 
80 #if defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES)
81 
82 bool codetag_needs_module_section(struct module *mod, const char *name,
83 				  unsigned long size);
84 void *codetag_alloc_module_section(struct module *mod, const char *name,
85 				   unsigned long size, unsigned int prepend,
86 				   unsigned long align);
87 void codetag_free_module_sections(struct module *mod);
88 void codetag_module_replaced(struct module *mod, struct module *new_mod);
89 void codetag_load_module(struct module *mod);
90 void codetag_unload_module(struct module *mod);
91 
92 #else /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */
93 
94 static inline bool
95 codetag_needs_module_section(struct module *mod, const char *name,
96 			     unsigned long size) { return false; }
97 static inline void *
98 codetag_alloc_module_section(struct module *mod, const char *name,
99 			     unsigned long size, unsigned int prepend,
100 			     unsigned long align) { return NULL; }
101 static inline void codetag_free_module_sections(struct module *mod) {}
102 static inline void codetag_module_replaced(struct module *mod, struct module *new_mod) {}
103 static inline void codetag_load_module(struct module *mod) {}
104 static inline void codetag_unload_module(struct module *mod) {}
105 
106 #endif /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */
107 
108 #endif /* _LINUX_CODETAG_H */
109