tracing: preserve module tracepoint strings
Subsystems (3)
PATCH 1/1 tracing: preserve module tracepoint strings
tracepoint_string() is documented as exporting constant strings through printk_formats, including when it is used from modules. That currently does not work.
A small test module that calls tracepoint_string("tracepoint_string_test_module_string") loads successfully and gets a pointer back, but the string never appears in /sys/kernel/tracing/printk_formats. The loader only collects __trace_printk_fmt from modules and ignores __tracepoint_str.
Collect module __tracepoint_str entries too, copy them to stable tracing-managed storage like module trace_printk formats, and let trace_is_tracepoint_string() recognize those copied strings. This makes module tracepoint strings visible through printk_formats and keeps them accepted by the trace string safety checks.
--- include/linux/module.h | 2 ++ kernel/module/main.c | 4 +++ kernel/trace/trace_printk.c | 63 ++++++++++++++++++++++++++++--------- 3 files changed, 54 insertions(+), 15 deletions(-)
include/linux/module.h +2 -0
@@ -515,6 +515,8 @@ struct module {#ifdef CONFIG_TRACING unsigned int num_trace_bprintk_fmt;const char **trace_bprintk_fmt_start; +unsigned int num_tracepoint_strings; +const char **tracepoint_strings_start;#endif #ifdef CONFIG_EVENT_TRACING struct trace_event_call **trace_events;
kernel/module/main.c +4 -0
@@ -2672,6 +2672,10 @@ static int find_module_sections(struct module *mod, struct load_info *info) mod->trace_bprintk_fmt_start =section_objs (info,"__trace_printk_fmt" ,sizeof (*mod->trace_bprintk_fmt_start), &mod->num_trace_bprintk_fmt); + mod->tracepoint_strings_start = +section_objs (info,"__tracepoint_str" , +sizeof (*mod->tracepoint_strings_start), + &mod->num_tracepoint_strings);#endif #ifdef CONFIG_DYNAMIC_FTRACE /* sechdrs[0].sh_size is always zero */
kernel/trace/trace_printk.c +48 -15
143 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
Steven Rostedt
maintainerQuoted 16 lines · On Tue, 7 Apr 2026 01:09:44 +0800 Cao Ruichuang <[email protected]> wrote:
tracepoint_string()is documented as exporting constant strings throughprintk_formats, including when it is used from modules. That currently does not work.A small test module that calls
tracepoint_string("tracepoint_string_test_module_string")loads successfully and gets a pointer back, but the string never appears in/sys/kernel/tracing/printk_formats. The loader only collects__trace_printk_fmtfrom modules and ignores__tracepoint_str.Collect module
__tracepoint_strentries too, copy them to stable tracing-managed storage like moduletrace_printkformats, and lettrace_is_tracepoint_string()recognize those copied strings. This makes module tracepoint strings visible throughprintk_formatsand keeps them accepted by the trace string safety checks.
Quoted 3 lines
Signed-off-by Cao Ruichuang <[email protected]>---
As this is not a trivial change, and affects module code, I'm going to hold off until after v7.1-rc1 to apply it.
-- Steve
Petr Pavlu
maintainerQuoted 10 lines · Cao Ruichuang
tracepoint_string()is documented as exporting constant strings throughprintk_formats, including when it is used from modules. That currently does not work.A small test module that calls
tracepoint_string("tracepoint_string_test_module_string")loads successfully and gets a pointer back, but the string never appears in/sys/kernel/tracing/printk_formats. The loader only collects__trace_printk_fmtfrom modules and ignores__tracepoint_str.
Quoted 5 lines
Collect module
__tracepoint_strentries too, copy them to stable tracing-managed storage like moduletrace_printkformats, and lettrace_is_tracepoint_string()recognize those copied strings. This makes module tracepoint strings visible throughprintk_formatsand keeps them accepted by the trace string safety checks.
The documentation for tracepoint_string() in include/linux/tracepoint.h contains:
* The @str used must be a constant string and persistent as it would not
* make sense to show a string that no longer exists. But it is still fine
* to be used with modules, because when modules are unloaded, if they
* had tracepoints, the ring buffers are cleared too. As long as the string
* does not change during the life of the module, it is fine to use
* tracepoint_string() within a module.
The implemented approach copies all strings referenced by __tracepoint_str and keeps them for the kernel's lifetime. I wonder if the code could directly reference the original data and handle its removal when the module is unloaded, or if the quoted comment should be updated to reflect the new behavior.
Signature · 2 lines
Thanks, Petr
PATCH v2 1/1 tracing: preserve module tracepoint strings
In reply to Cao Ruichuangtracepoint_string() is documented as exporting constant strings through printk_formats, including when it is used from modules. That currently does not work.
A small test module that calls tracepoint_string("tracepoint_string_test_module_string") loads successfully and gets a pointer back, but the string never appears in /sys/kernel/tracing/printk_formats. The loader only collects __trace_printk_fmt from modules and ignores __tracepoint_str.
Collect module __tracepoint_str entries too, copy them to stable tracing-managed storage like module trace_printk formats, and let trace_is_tracepoint_string() recognize those copied strings. This makes module tracepoint strings visible through printk_formats and keeps them accepted by the trace string safety checks.
Update the tracepoint_string() documentation to describe this module behavior explicitly, so the comment matches the preserved module-string mappings exported by tracing.
---
v2:
- update tracepoint_string() documentation to describe the preserved module-string mapping explicitly
- address Petr Pavlu's review about the comment not matching the implemented module behavior
include/linux/module.h | 2 ++ include/linux/tracepoint.h | 14 ++++++--- kernel/module/main.c | 4 +++ kernel/trace/trace_printk.c | 63 ++++++++++++++++++++++++++++--------- 4 files changed, 63 insertions(+), 20 deletions(-)
include/linux/module.h +2 -0
@@ -515,6 +515,8 @@ struct module {#ifdef CONFIG_TRACING unsigned int num_trace_bprintk_fmt;const char **trace_bprintk_fmt_start; +unsigned int num_tracepoint_strings; +const char **tracepoint_strings_start;#endif #ifdef CONFIG_EVENT_TRACING struct trace_event_call **trace_events;
include/linux/tracepoint.h +9 -5
@@ -475,11 +475,15 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)* the ASCII strings they represent. * * The @str used must be a constant string and persistent as it would not -* make sense to show a string that no longer exists. But it is still fine -* to be used with modules, because when modules are unloaded, if they -* had tracepoints, the ring buffers are cleared too. As long as the string -* does not change during the life of the module, it is fine to use -* tracepoint_string() within a module. +* make sense to show a string that no longer exists. +* +* For built-in code, the tracing system uses the original string address. +* For modules, the tracing code saves tracepoint strings into +* tracing-managed storage when the module loads, so their mappings remain +* available through printk_formats and trace string checks even after the +* module's own memory goes away. As long as the string does not change +* during the life of the module, it is fine to use tracepoint_string() +* within a module. */ #define tracepoint_string (str) \ ({ \
kernel/module/main.c +4 -0
@@ -2672,6 +2672,10 @@ static int find_module_sections(struct module *mod, struct load_info *info) mod->trace_bprintk_fmt_start =section_objs (info,"__trace_printk_fmt" ,sizeof (*mod->trace_bprintk_fmt_start), &mod->num_trace_bprintk_fmt); + mod->tracepoint_strings_start = +section_objs (info,"__tracepoint_str" , +sizeof (*mod->tracepoint_strings_start), + &mod->num_tracepoint_strings);#endif #ifdef CONFIG_DYNAMIC_FTRACE /* sechdrs[0].sh_size is always zero */
kernel/trace/trace_printk.c +48 -15
143 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
Petr Pavlu
maintainerQuoted 20 lines · Cao Ruichuang
tracepoint_string()is documented as exporting constant strings throughprintk_formats, including when it is used from modules. That currently does not work.A small test module that calls
tracepoint_string("tracepoint_string_test_module_string")loads successfully and gets a pointer back, but the string never appears in/sys/kernel/tracing/printk_formats. The loader only collects__trace_printk_fmtfrom modules and ignores__tracepoint_str.Collect module
__tracepoint_strentries too, copy them to stable tracing-managed storage like moduletrace_printkformats, and lettrace_is_tracepoint_string()recognize those copied strings. This makes module tracepoint strings visible throughprintk_formatsand keeps them accepted by the trace string safety checks.Update the
tracepoint_string()documentation to describe this module behavior explicitly, so the comment matches the preserved module-string mappings exported by tracing.
Quoted 8 lines
Signed-off-by Cao Ruichuang <[email protected]>---
v2:
- updatetracepoint_string()documentation to describe the preserved module-string mapping explicitly
- address Petr Pavlu's review about the comment not matching the implemented module behavior
I questioned in my previous comment whether the data associated with tracepoint_string() could be dropped when the module that created it is unloaded. Typically, modules should not leave any data behind when they are removed. Note how kernel/trace/trace_events.c tracks event fields using add_str_to_module() and releases them in trace_module_remove_events(). In practice, I suppose this isn't a large problem because the usage of tracepoint_string() is limited and one won't typically load/unload different modules that use this facility. Nonetheless, what is the reason for keeping the tracepoint_string() data for unloaded modules?
Signature · 2 lines
Thanks, Petr
PATCH 1/1 tracing: separate module tracepoint strings from trace_printk formats
The previous module tracepoint_string() fix took the smallest implementation path and reused the existing module trace_printk format storage.
That was enough to make module __tracepoint_str entries show up in printk_formats and be accepted by trace_is_tracepoint_string(), but it also made those copied mappings persist after module unload. That does not match the expected module lifetime semantics.
Handle module tracepoint_string() mappings separately instead of mixing them into the module trace_printk format list. Keep copying the strings into tracing-managed storage while the module is loaded, but track them on their own list and drop them again on MODULE_STATE_GOING.
Keep module trace_printk format handling unchanged.
This split is intentional: module trace_printk formats and module tracepoint_string() mappings do not have the same lifetime requirements. Keeping them in one shared structure would either preserve tracepoint_string() mappings too long again, or require mixed ownership/refcount rules in a trace_printk-oriented structure.
The separate module tracepoint_string() list intentionally keeps one copied mapping per module entry instead of trying to share copies across modules by string contents. printk_formats is address-based, and sharing those copies would add another layer of shared ownership/refcounting without changing the lifetime rule this fix is trying to restore.
--- include/linux/tracepoint.h | 9 +- kernel/trace/trace_printk.c | 250 ++++++++++++++++++++++-------------- 2 files changed, 157 insertions(+), 102 deletions(-)
include/linux/tracepoint.h +4 -5
@@ -479,11 +479,10 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)* * For built-in code, the tracing system uses the original string address. * For modules, the tracing code saves tracepoint strings into -* tracing-managed storage when the module loads, so their mappings remain -* available through printk_formats and trace string checks even after the -* module's own memory goes away. As long as the string does not change -* during the life of the module, it is fine to use tracepoint_string() -* within a module. +* tracing-managed storage while the module is loaded, and drops those +* mappings again when the module unloads. As long as the string does not +* change during the life of the module, it is fine to use +* tracepoint_string() within a module. */ #define tracepoint_string (str) \ ({ \
kernel/trace/trace_printk.c +153 -97
354 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
Petr Pavlu
maintainerRe: [PATCH] tracing: separate module tracepoint strings from trace_printk formats
Quoted 22 lines · Cao Ruichuang
The previous module
tracepoint_string()fix took the smallest implementation path and reused the existing moduletrace_printkformat storage.That was enough to make module
__tracepoint_strentries show up inprintk_formatsand be accepted bytrace_is_tracepoint_string(), but it also made those copied mappings persist after module unload. That does not match the expected module lifetime semantics.Handle module
tracepoint_string()mappings separately instead of mixing them into the moduletrace_printkformat list. Keep copying the strings into tracing-managed storage while the module is loaded, but track them on their own list and drop them again onMODULE_STATE_GOING.Keep module
trace_printkformat handling unchanged.This split is intentional: module
trace_printkformats and moduletracepoint_string()mappings do not have the same lifetime requirements. Keeping them in one shared structure would either preservetracepoint_string()mappings too long again, or require mixed ownership/refcount rules in a trace_printk-oriented structure.
Quoted 5 lines
The separate module
tracepoint_string()list intentionally keeps one copied mapping per module entry instead of trying to share copies across modules by string contents.printk_formatsis address-based, and sharing those copies would add another layer of shared ownership/refcounting without changing the lifetime rule this fix is trying to restore.
I believe it would be more productive to first answer the question in my previous email and give others time to comment on the noted issue and discuss a possible solution. The previous patch hasn't been accepted yet, so sending a new patch on top of it just hours after my comment seems unusual.
The patch contains some unrelated changes, such as renaming last_index to next_index in find_next(). I suspect it was generated with the help of AI but it isn't attributed as such. Please check Documentation/process/coding-assistants.rst.
The implementation splits the trace_bprintk_fmt_list into two lists. While this looks sensible, it creates nearly duplicate code for handling the two lists, for example, hold_module_trace_format() and hold_module_trace_format(), or lookup_mod_format_ptr() and lookup_mod_tracepoint_ptr(). An alternative would be to keep one list and have a 'struct module *mod' member to differentiate between the strings. 'mod == NULL' for printk fmts and 'mod != NULL' for tracepoint strings.
I also considered whether kernel/trace/trace_printk.c could avoid tracking tracepoint strings altogether and instead have find_next_mod_entry() directly traverse the modules list and module::tracepoint_strings_start. However, this would require taking both btrace_mutex and the RCU lock, and I believe it would be more complex.
Since the tracepoint strings are now tracked only for the duration of a module's lifetime, kernel/trace/trace_printk.c doesn't need to copy the strings and can reference directly the original module data. Alternatively, the original strings could be placed in an .init section so that once trace_printk.c copies them and the module is initialized, they get dropped.
Signature · 2 lines
Thanks, Petr
Quoted 395 lines
Signed-off-by Cao Ruichuang <[email protected]>--- include/linux/tracepoint.h | 9 +- kernel/trace/trace_printk.c | 250 ++++++++++++++++++++++-------------- 2 files changed, 157 insertions(+), 102 deletions(-)include/linux/tracepoint.h +4
-5@@ -479,11 +479,10 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)* * For built-in code, the tracing system uses the original string address. * For modules, the tracing code saves tracepoint strings into -* tracing-managed storage when the module loads, so their mappings remain -* available through printk_formats and trace string checks even after the -* module's own memory goes away. As long as the string does not change -* during the life of the module, it is fine to use tracepoint_string() -* within a module. +* tracing-managed storage while the module is loaded, and drops those +* mappings again when the module unloads. As long as the string does not +* change during the life of the module, it is fine to use +* tracepoint_string() within a module. */ #define tracepoint_string (str) \ ({ \kernel/trace/trace_printk.c +153
-97@@ -21,24 +21,24 @@#ifdef CONFIG_MODULES -/* -* modules trace_printk() formats and tracepoint_string() strings are -* autosaved in struct trace_bprintk_fmt, which are queued on -* trace_bprintk_fmt_list. -*/ +/* module trace_printk() formats are autosaved on trace_bprintk_fmt_list. */ static LIST_HEAD (trace_bprintk_fmt_list); +/* module tracepoint_string() copies live on tracepoint_string_list. */ +static LIST_HEAD (tracepoint_string_list); -/* serialize accesses to trace_bprintk_fmt_list */ +/* serialize accesses to module format and tracepoint-string lists */ static DEFINE_MUTEX (btrace_mutex);struct trace_bprintk_fmt {struct list_head list;const char *fmt; -unsigned int type; }; -#define TRACE_BPRINTK_TYPE BIT(0) -#define TRACE_TRACEPOINT_TYPE BIT(1) +struct tracepoint_string_entry { +struct list_head list; +struct module *mod; +const char *str; +};static inline struct trace_bprintk_fmt *lookup_format (const char *fmt) {@@ -54,24 +54,21 @@ static inline struct trace_bprintk_fmt *lookup_format(const char *fmt)return NULL ; } -static void hold_module_trace_format (const char **start,const char **end, -unsigned int type) +static void hold_module_trace_bprintk_format (const char **start,const char **end) {const char **iter;char *fmt;/* allocate the trace_printk per cpu buffers */ -if ((type &TRACE_BPRINTK_TYPE ) && start != end) +if (start != end)trace_printk_init_buffers ();mutex_lock (&btrace_mutex);for (iter = start; iter < end; iter++) {struct trace_bprintk_fmt *tb_fmt =lookup_format (*iter);if (tb_fmt) { -if (!IS_ERR (tb_fmt)) { - tb_fmt->type |= type; +if (!IS_ERR (tb_fmt)) *iter = tb_fmt->fmt; - }continue ; }@@ -83,7 +80,6 @@ static void hold_module_trace_format(const char **start, const char **end,list_add_tail (&tb_fmt->list, &trace_bprintk_fmt_list);strcpy (fmt, *iter); tb_fmt->fmt = fmt; - tb_fmt->type = type; }else kfree (tb_fmt); }@@ -93,89 +89,156 @@ static void hold_module_trace_format(const char **start, const char **end,mutex_unlock (&btrace_mutex); } +static void hold_module_tracepoint_strings (struct module *mod, +const char **start, +const char **end) +{ +const char **iter; + +mutex_lock (&btrace_mutex); +for (iter = start; iter < end; iter++) { +struct tracepoint_string_entry *tp_entry; +char *str; + + tp_entry =kmalloc_obj (*tp_entry); +if (!tp_entry) +continue ; + + str =kstrdup (*iter,GFP_KERNEL ); +if (!str) { +kfree (tp_entry); +continue ; + } + + tp_entry->mod = mod; + tp_entry->str = str; +list_add_tail (&tp_entry->list, &tracepoint_string_list); + *iter = tp_entry->str; + } +mutex_unlock (&btrace_mutex); +} + +static void release_module_tracepoint_strings (struct module *mod) +{ +struct tracepoint_string_entry *tp_entry, *n; + +mutex_lock (&btrace_mutex); +list_for_each_entry_safe (tp_entry, n, &tracepoint_string_list, list) { +if (tp_entry->mod != mod) +continue ; +list_del (&tp_entry->list); +kfree (tp_entry->str); +kfree (tp_entry); + } +mutex_unlock (&btrace_mutex); +} +static int module_trace_format_notify (struct notifier_block *self,unsigned long val,void *data) {struct module *mod = data; -if (val !=MODULE_STATE_COMING ) -return NOTIFY_OK ; +switch (val) { +case MODULE_STATE_COMING : +if (mod->num_trace_bprintk_fmt) { +const char **start = mod->trace_bprintk_fmt_start; +const char **end = start + mod->num_trace_bprintk_fmt; -if (mod->num_trace_bprintk_fmt) { -const char **start = mod->trace_bprintk_fmt_start; -const char **end = start + mod->num_trace_bprintk_fmt; +hold_module_trace_bprintk_format (start, end); + } + +if (mod->num_tracepoint_strings) { +const char **start = mod->tracepoint_strings_start; +const char **end = start + mod->num_tracepoint_strings; -hold_module_trace_format (start, end,TRACE_BPRINTK_TYPE ); +hold_module_tracepoint_strings (mod, start, end); + } +break ; +case MODULE_STATE_GOING : +release_module_tracepoint_strings (mod); +break ; } -if (mod->num_tracepoint_strings) { -const char **start = mod->tracepoint_strings_start; -const char **end = start + mod->num_tracepoint_strings; +return NOTIFY_OK ; +} -hold_module_trace_format (start, end,TRACE_TRACEPOINT_TYPE ); +static const char **find_first_mod_entry (void ) +{ +struct trace_bprintk_fmt *tb_fmt; +struct tracepoint_string_entry *tp_entry; + +if (!list_empty (&trace_bprintk_fmt_list)) { + tb_fmt =list_first_entry (&trace_bprintk_fmt_list, +typeof (*tb_fmt), list); +return &tb_fmt->fmt; } -return NOTIFY_OK ; +if (!list_empty (&tracepoint_string_list)) { + tp_entry =list_first_entry (&tracepoint_string_list, +typeof (*tp_entry), list); +return &tp_entry->str; + } + +return NULL ; } -/* -* The debugfs/tracing/printk_formats file maps the addresses with -* the ASCII formats that are used in the bprintk events in the -* buffer. For userspace tools to be able to decode the events from -* the buffer, they need to be able to map the address with the format. -* -* The addresses of the bprintk formats are in their own section -* __trace_printk_fmt. But for modules we copy them into a link list. -* The code to print the formats and their addresses passes around the -* address of the fmt string. If the fmt address passed into the seq -* functions is within the kernel core __trace_printk_fmt section, then -* it simply uses the next pointer in the list. -* -* When the fmt pointer is outside the kernel core __trace_printk_fmt -* section, then we need to read the link list pointers. The trick is -* we pass the address of the string to the seq function just like -* we do for the kernel core formats. To get back the structure that -* holds the format, we simply use container_of() and then go to the -* next format in the list. -*/ -static const char ** -find_next_mod_format (int start_index,void *v,const char **fmt,loff_t *pos) +static struct trace_bprintk_fmt *lookup_mod_format_ptr (const char **fmt_ptr) { -struct trace_bprintk_fmt *mod_fmt; +struct trace_bprintk_fmt *tb_fmt; -if (list_empty (&trace_bprintk_fmt_list)) -return NULL ; +list_for_each_entry (tb_fmt, &trace_bprintk_fmt_list, list) { +if (fmt_ptr == &tb_fmt->fmt) +return tb_fmt; + } -/* -* v will point to the address of the fmt record from t_next -* v will be NULL from t_start. -* If this is the first pointer or called from start -* then we need to walk the list. -*/ -if (!v || start_index == *pos) { -struct trace_bprintk_fmt *p; - -/* search the module list */ -list_for_each_entry (p, &trace_bprintk_fmt_list, list) { -if (start_index == *pos) -return &p->fmt; - start_index++; +return NULL ; +} + +static struct tracepoint_string_entry *lookup_mod_tracepoint_ptr (const char **str_ptr) +{ +struct tracepoint_string_entry *tp_entry; + +list_for_each_entry (tp_entry, &tracepoint_string_list, list) { +if (str_ptr == &tp_entry->str) +return tp_entry; + } + +return NULL ; +} + +static const char **find_next_mod_entry (int start_index,void *v,loff_t *pos) +{ +struct trace_bprintk_fmt *tb_fmt; +struct tracepoint_string_entry *tp_entry; + +if (!v || start_index == *pos) +return find_first_mod_entry (); + + tb_fmt =lookup_mod_format_ptr (v); +if (tb_fmt) { +if (tb_fmt->list.next != &trace_bprintk_fmt_list) { + tb_fmt =list_next_entry (tb_fmt, list); +return &tb_fmt->fmt; } -/* pos > index */ + +if (!list_empty (&tracepoint_string_list)) { + tp_entry =list_first_entry (&tracepoint_string_list, +typeof (*tp_entry), list); +return &tp_entry->str; + } +return NULL ; } -/* -* v points to the address of the fmt field in the mod list -* structure that holds the module print format. -*/ - mod_fmt =container_of (v,typeof (*mod_fmt), fmt); -if (mod_fmt->list.next == &trace_bprintk_fmt_list) + tp_entry =lookup_mod_tracepoint_ptr (v); +if (!tp_entry)return NULL ; - mod_fmt =container_of (mod_fmt->list.next,typeof (*mod_fmt), list); +if (tp_entry->list.next == &tracepoint_string_list) +return NULL ; -return &mod_fmt->fmt; + tp_entry =list_next_entry (tp_entry, list); +return &tp_entry->str; }static void format_mod_start (void )@@ -195,8 +258,8 @@ module_trace_format_notify(struct notifier_block *self, {return NOTIFY_OK ; } -static inline const char ** -find_next_mod_format (int start_index,void *v,const char **fmt,loff_t *pos) +static inline const char **find_next_mod_entry (int start_index,void *v, +loff_t *pos) {return NULL ; }@@ -274,7 +337,7 @@ bool trace_is_tracepoint_string(const char *str) {const char **ptr = __start___tracepoint_str;#ifdef CONFIG_MODULES -struct trace_bprintk_fmt *tb_fmt; +struct tracepoint_string_entry *tp_entry;#endif for (ptr = __start___tracepoint_str; ptr < __stop___tracepoint_str; ptr++) {@@ -284,8 +347,8 @@ bool trace_is_tracepoint_string(const char *str)#ifdef CONFIG_MODULES mutex_lock (&btrace_mutex); -list_for_each_entry (tb_fmt, &trace_bprintk_fmt_list, list) { -if ((tb_fmt->type &TRACE_TRACEPOINT_TYPE ) && str == tb_fmt->fmt) { +list_for_each_entry (tp_entry, &tracepoint_string_list, list) { +if (str == tp_entry->str) {mutex_unlock (&btrace_mutex);return true; }@@ -297,9 +360,8 @@ bool trace_is_tracepoint_string(const char *str)static const char **find_next (void *v,loff_t *pos) { -const char **fmt = v;int start_index; -int last_index; +int next_index; start_index = __stop___trace_bprintk_fmt - __start___trace_bprintk_fmt;@@ -307,25 +369,19 @@ static const char **find_next(void *v, loff_t *pos)return __start___trace_bprintk_fmt + *pos;/* -* The __tracepoint_str section is treated the same as the -* __trace_printk_fmt section. The difference is that the -* __trace_printk_fmt section should only be used by trace_printk() -* in a debugging environment, as if anything exists in that section -* the trace_prink() helper buffers are allocated, which would just -* waste space in a production environment. -* -* The __tracepoint_str sections on the other hand are used by -* tracepoints which need to map pointers to their strings to -* the ASCII text for userspace. +* Built-in __tracepoint_str entries are exported directly from the +* core section. Module tracepoint_string() mappings are kept on a +* separate tracing-managed list below, because their lifetime is tied +* to module load/unload and differs from module trace_printk() formats. */ - last_index = start_index; + next_index = start_index; start_index = __stop___tracepoint_str - __start___tracepoint_str; -if (*pos < last_index + start_index) -return __start___tracepoint_str + (*pos - last_index); +if (*pos < next_index + start_index) +return __start___tracepoint_str + (*pos - next_index); - start_index += last_index; -return find_next_mod_format (start_index, v, fmt, pos); + start_index += next_index; +return find_next_mod_entry (start_index, v, pos); } static void *
Re: [PATCH] tracing: separate module tracepoint strings from trace_printk formats
Hi Petr,
Sorry for the noise.
The previous revisions were not sent automatically by an AI agent, but I did use AI assistance while working on this thread and I should have disclosed that according to Documentation/process/coding-assistants.rst. More importantly, I should have slowed down and answered the design questions first instead of posting another implementation so quickly.
After going back through the bug report, the code, and a local reproducer, I think the original bug report is valid: module tracepoint_string() entries do not currently show up in printk_formats.
The implementation gap seems to be that the module path handles __trace_printk_fmt, but there is no equivalent handling for a module's __tracepoint_str entries, so printk_formats has no module-side source from which to enumerate them.
What I got wrong in the previous versions was the direction of the fix. I treated the missing module __tracepoint_str handling as if it should reuse the module trace_printk storage model, which made the mapping persist beyond module unload. I agree that this is not the right lifetime semantics for tracepoint_string().
So I do not plan to send another patch revision until the intended module lifetime behavior is clear. At this point, the direction that seems right to me is to make module tracepoint_string() mappings visible while the module is alive, without simply reusing the trace_printk persistence model.
Thanks,
Cao
PATCH v2 1/1 tracing: export live module tracepoint strings in printk_formats
In reply to Cao Ruichuangtracepoint_string() documents that its strings are exported through printk_formats so that user space can decode pointer fields recorded in trace buffers.
That already works for built-in __tracepoint_str entries, but module __tracepoint_str sections are not collected or exported today. As a result, module tracepoint_string() users still show raw pointer values in printk_formats consumers such as trace.dat decoders.
Record module __tracepoint_str sections when modules are loaded, expose their live ranges through printk_formats, and teach trace_is_tracepoint_string() to accept those live module strings too.
Keep the lifetime semantics tied to the module itself. This does not copy strings into tracing-owned storage and does not preserve the mappings after module unload.
On MODULE_STATE_GOING, the live module string ranges are removed again. This relies on the existing tracing module notifier ordering: trace event teardown runs first and resets module event buffers before these auxiliary string mappings are dropped.
If the small auxiliary registry allocation fails, warn and continue loading the module. printk_formats exposure is degraded in that case, but tracing should not fail module load for missing debug metadata.
---
v2:
- replace the previous copied-string approach with live module section ranges
- record module __tracepoint_str ranges in struct module
- export only live module tracepoint strings in printk_formats
- remove module mappings on MODULE_STATE_GOING
- keep auxiliary registry allocation failure non-fatal and warn instead
- add explicit notifier priority and document the teardown ordering dependency
Tested in QEMU:
- basic repro showing module tracepoint_string() entries in printk_formats
- load/unload validation confirming mappings are removed after rmmod
- failed module init after MODULE_STATE_COMING with no stale mapping left
- targeted failslab injection on the notifier-time auxiliary allocation, confirming module load still succeeds, a warning is emitted, and the module mapping is not exported
include/linux/module.h | 2 + kernel/module/main.c | 4 + kernel/trace/trace_printk.c | 153 ++++++++++++++++++++++++++++++++++-- 3 files changed, 152 insertions(+), 7 deletions(-)
include/linux/module.h +2 -0
@@ -515,6 +515,8 @@ struct module {#ifdef CONFIG_TRACING unsigned int num_trace_bprintk_fmt;const char **trace_bprintk_fmt_start; +unsigned int num_tracepoint_strings; +const char **tracepoint_strings_start;#endif #ifdef CONFIG_EVENT_TRACING struct trace_event_call **trace_events;
kernel/module/main.c +4 -0
@@ -2672,6 +2672,10 @@ static int find_module_sections(struct module *mod, struct load_info *info) mod->trace_bprintk_fmt_start =section_objs (info,"__trace_printk_fmt" ,sizeof (*mod->trace_bprintk_fmt_start), &mod->num_trace_bprintk_fmt); + mod->tracepoint_strings_start = +section_objs (info,"__tracepoint_str" , +sizeof (*mod->tracepoint_strings_start), + &mod->num_tracepoint_strings);#endif #ifdef CONFIG_DYNAMIC_FTRACE /* sechdrs[0].sh_size is always zero */
kernel/trace/trace_printk.c +146 -7
221 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
Steven Rostedt
maintainerRe: [PATCH v2] tracing: export live module tracepoint strings in printk_formats
[ Adding more module maintainers ]
Note, tracing subsystem uses capital letters in subject:
[PATCH v2] tracing: Export live module tracepoint strings in printk_formats
Quoted 56 lines · On Mon, 20 Apr 2026 14:19:11 +0800 Cao Ruichuang <[email protected]> wrote:
tracepoint_string()documents that its strings are exported throughprintk_formatsso that user space can decode pointer fields recorded in trace buffers.That already works for built-in
__tracepoint_strentries, but module__tracepoint_strsections are not collected or exported today. As a result, moduletracepoint_string()users still show raw pointer values inprintk_formatsconsumers such as trace.dat decoders.Record module
__tracepoint_strsections when modules are loaded, expose their live ranges throughprintk_formats, and teachtrace_is_tracepoint_string()to accept those live module strings too.Keep the lifetime semantics tied to the module itself. This does not copy strings into tracing-owned storage and does not preserve the mappings after module unload.
On
MODULE_STATE_GOING, the live module string ranges are removed again. This relies on the existing tracing module notifier ordering: trace event teardown runs first and resets module event buffers before these auxiliary string mappings are dropped.If the small auxiliary registry allocation fails, warn and continue loading the module.
printk_formatsexposure is degraded in that case, but tracing should not fail module load for missing debug metadata.Assisted-by Codex:GPT-5.4Signed-off-by Cao Ruichuang <[email protected]>---
v2:
- replace the previous copied-string approach with live module section ranges
- record module__tracepoint_strranges in struct module
- export only live module tracepoint strings inprintk_formats
- remove module mappings onMODULE_STATE_GOING
- keep auxiliary registry allocation failure non-fatal and warn instead
- add explicit notifier priority and document the teardown ordering dependencyTested in QEMU:
- basic repro showing moduletracepoint_string()entries inprintk_formats
- load/unload validation confirming mappings are removed after rmmod
- failed module init afterMODULE_STATE_COMINGwith no stale mapping left
- targeted failslab injection on the notifier-time auxiliary allocation, confirming module load still succeeds, a warning is emitted, and the module mapping is not exportedinclude/linux/module.h | 2 + kernel/module/main.c | 4 + kernel/trace/trace_printk.c | 153 ++++++++++++++++++++++++++++++++++-- 3 files changed, 152 insertions(+), 7 deletions(-)include/linux/module.h +0
-0
include/linux/module.h struct module { · line 519
Quoted 5 lines
include/linux/module.h +2
-0@@ -515,6 +515,8 @@ struct module {#ifdef CONFIG_TRACING unsigned int num_trace_bprintk_fmt;const char **trace_bprintk_fmt_start; +unsigned int num_tracepoint_strings; +const char **tracepoint_strings_start;
Would be more compact on 64 bit systems to put the two ints together.
unsigned int num_trace_bprintk_fmt; unsigned int num_tracepoint_strings; const char **trace_bprintk_fmt_start; const char **tracepoint_strings_start;
Quoted 48 lines
include/linux/module.h +0
-0#endif #ifdef CONFIG_EVENT_TRACING struct trace_event_call **trace_events;kernel/module/main.c +4
-0@@ -2672,6 +2672,10 @@ static int find_module_sections(struct module *mod, struct load_info *info) mod->trace_bprintk_fmt_start =section_objs (info,"__trace_printk_fmt" ,sizeof (*mod->trace_bprintk_fmt_start), &mod->num_trace_bprintk_fmt); + mod->tracepoint_strings_start = +section_objs (info,"__tracepoint_str" , +sizeof (*mod->tracepoint_strings_start), + &mod->num_tracepoint_strings);#endif #ifdef CONFIG_DYNAMIC_FTRACE /* sechdrs[0].sh_size is always zero */ kernel/trace/trace_printk.c +7
-1@@ -13,6 +13,7 @@#include <linux/string.h> #include <linux/module.h> #include <linux/mutex.h> +#include <linux/rcupdate.h> #include <linux/ctype.h> #include <linux/list.h> #include <linux/slab.h> @@ -24,10 +25,15 @@/* * modules trace_printk()'s formats are autosaved in struct trace_bprintk_fmt * which are queued on trace_bprintk_fmt_list. +* +* modules tracepoint_string() entries are kept as ranges into the owning +* module's __tracepoint_str section and are removed again when the module +* goes away. */ static LIST_HEAD (trace_bprintk_fmt_list); +static LIST_HEAD (tracepoint_str_list); -/* serialize accesses to trace_bprintk_fmt_list */ +/* serialize accesses to module trace printk and tracepoint string lists */ static DEFINE_MUTEX (btrace_mutex);struct trace_bprintk_fmt {
kernel/trace/trace_printk.c struct trace_bprintk_fmt { · line 45
Quoted 5 lines
kernel/trace/trace_printk.c +2
-0@@ -35,6 +41,13 @@ struct trace_bprintk_fmt {const char *fmt; }; +struct tracepoint_mod_str { +struct list_head list;
Perhaps add a rcu_head here?
Quoted 36 lines
kernel/trace/trace_printk.c +29
-0+struct module *mod; +const char **start; +unsigned int num; +}; +static inline struct trace_bprintk_fmt *lookup_format (const char *fmt) {struct trace_bprintk_fmt *pos;@@ -85,16 +98,70 @@ void hold_module_trace_bprintk_format(const char **start, const char **end)mutex_unlock (&btrace_mutex); } +static void hold_module_tracepoint_strings (struct module *mod) +{ +struct tracepoint_mod_str *tp_str; + +if (!mod->num_tracepoint_strings) +return ; + + tp_str =kmalloc_obj (*tp_str); +if (!tp_str) { +pr_warn ("tracing: Failed to expose module tracepoint strings for %s\n" , + mod->name); +return ; + } + + tp_str->mod = mod; + tp_str->start = mod->tracepoint_strings_start; + tp_str->num = mod->num_tracepoint_strings; + +mutex_lock (&btrace_mutex); +list_add_tail_rcu (&tp_str->list, &tracepoint_str_list); +mutex_unlock (&btrace_mutex); +} + +static void release_module_tracepoint_strings (struct module *mod)
kernel/trace/trace_printk.c void hold_module_trace_bprintk_format(const char **start, const char **end) · line 134
Quoted 10 lines
kernel/trace/trace_printk.c +10
-0+{ +struct tracepoint_mod_str *tp_str, *next; +struct tracepoint_mod_str *found =NULL ; + +mutex_lock (&btrace_mutex); +list_for_each_entry_safe (tp_str, next, &tracepoint_str_list, list) { +if (tp_str->mod != mod) +continue ; + +list_del_rcu (&tp_str->list);
Does this really need to be rcu protected? (See below).
kernel/trace/trace_printk.c void hold_module_trace_bprintk_format(const char **start, const char **end) · line 142
Quoted 8 lines
kernel/trace/trace_printk.c +8
-0+ found = tp_str; +break ; + } +mutex_unlock (&btrace_mutex); + +if (found) { +synchronize_rcu (); +kfree (found);
Should use kfree_rcu() to not slow down module unload anymore than needed (hence the need for the rcu_head above). But if we keep it used under the mutex instead of RCU, we don't even need to do that.
And honestly, the iteration happens only on module load, so the only time the read would intersect with the write is if a module was being loaded at the same time another module was being unloaded. I don't think that can happen.
Quoted 30 lines
kernel/trace/trace_printk.c +16
-4+ } +} +static int module_trace_bprintk_format_notify (struct notifier_block *self,unsigned long val,void *data) {struct module *mod = data; -if (mod->num_trace_bprintk_fmt) { -const char **start = mod->trace_bprintk_fmt_start; -const char **end = start + mod->num_trace_bprintk_fmt; -if (val ==MODULE_STATE_COMING ) +switch (val) { +case MODULE_STATE_COMING : +if (mod->num_trace_bprintk_fmt) { +const char **start = mod->trace_bprintk_fmt_start; +const char **end = start + mod->num_trace_bprintk_fmt; +hold_module_trace_bprintk_format (start, end); + } +hold_module_tracepoint_strings (mod); +break ; +case MODULE_STATE_GOING : +/* trace event teardown runs first and clears module event buffers. */ +release_module_tracepoint_strings (mod); +break ; }return NOTIFY_OK ; }
kernel/trace/trace_printk.c find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos) · line 235
Quoted 10 lines
kernel/trace/trace_printk.c +7
-0@@ -159,6 +226,55 @@ find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos)return &mod_fmt->fmt; } +static int count_mod_formats (void ) +{ +struct trace_bprintk_fmt *p; +int count =0 ; + +list_for_each_entry (p, &trace_bprintk_fmt_list, list) + count++;
This counter needs to be stored somewhere and not calculated every time.
int trace_bprintk_fmt_cnt;
And updated on creation and deletion.
Quoted 7 lines
kernel/trace/trace_printk.c +7
-0+ +return count; +} + +static const char ** +find_next_mod_tracepoint_str (int start_index,loff_t *pos) +{
kernel/trace/trace_printk.c find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos) · line 252
Quoted 10 lines
kernel/trace/trace_printk.c +10
-0+struct tracepoint_mod_str *tp_str; +int index = start_index; +unsigned int i; + +list_for_each_entry (tp_str, &tracepoint_str_list, list) { +for (i =0 ; i < tp_str->num; i++) { +if (index == *pos) +return tp_str->start + i; + index++; + }
The above is horribly inefficient. Should have:
list_for_each_entry(tp_str, &tracepoint_str_list, list) {
if (index + tp_str->num <= *pos) {
index += tp_str->num;
continue;
}
for (i = 0; i < tp_str->num; i++) {
if (index == *pos)
return tp_str->start + i;
index++;
}
/* Should not hit here */
WARN_ONCE(1);
return NULL;
}kernel/trace/trace_printk.c find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos) · line 264
Quoted 12 lines
kernel/trace/trace_printk.c +12
-0+ } + +return NULL ; +} + +static bool is_module_tracepoint_string (const char *str) +{ +struct tracepoint_mod_str *tp_str; +unsigned int i; +bool found = false; + +rcu_read_lock ();
This is not a fast path, why not take the btrace_mutex?
Either way, for rcu, this should be:
guard(rcu)();
or for the mutex:
guard(mutex)(&btrace_mutex);
kernel/trace/trace_printk.c find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos) · line 268
Quoted 4 lines
kernel/trace/trace_printk.c +4
-0+list_for_each_entry_rcu (tp_str, &tracepoint_str_list, list) { +for (i =0 ; i < tp_str->num; i++) { +if (str == tp_str->start[i]) { + found = true;
This should be:
return true;
kernel/trace/trace_printk.c find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos) · line 272
Quoted 4 lines
kernel/trace/trace_printk.c +4
-0+ goto out; + } + } + }
Remove the below and have:
return false;
-- Steve
Quoted 68 lines
kernel/trace/trace_printk.c +30
-2+out: +rcu_read_unlock (); +return found; +} +static void format_mod_start (void ) {mutex_lock (&btrace_mutex);@@ -181,6 +297,22 @@ find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos) {return NULL ; } + +static inline int count_mod_formats (void ) +{ +return 0 ; +} + +static inline const char ** +find_next_mod_tracepoint_str (int start_index,loff_t *pos) +{ +return NULL ; +} + +static inline bool is_module_tracepoint_string (const char *str) +{ +return false; +}static inline void format_mod_start (void ) { }static inline void format_mod_stop (void ) { }#endif /* CONFIG_MODULES */ @@ -195,6 +327,7 @@ void trace_printk_control(bool enabled)__initdata_or_module static struct notifier_block module_trace_bprintk_format_nb = { .notifier_call = module_trace_bprintk_format_notify, + .priority =0 , };int __trace_bprintk (unsigned long ip,const char *fmt, ...)@@ -259,12 +392,13 @@ bool trace_is_tracepoint_string(const char *str)if (str == *ptr)return true; } -return false; +return is_module_tracepoint_string (str); }static const char **find_next (void *v,loff_t *pos) {const char **fmt = v; +int mod_formats;int start_index;int last_index;@@ -292,7 +426,12 @@ static const char **find_next(void *v, loff_t *pos)return __start___tracepoint_str + (*pos - last_index); start_index += last_index; -return find_next_mod_format (start_index, v, fmt, pos); + mod_formats =count_mod_formats (); +if (*pos < start_index + mod_formats) +return find_next_mod_format (start_index, v, fmt, pos); + + start_index += mod_formats; +return find_next_mod_tracepoint_str (start_index, pos); } static void *
PATCH v3 1/1 tracing: Export live module tracepoint strings in printk_formats
tracepoint_string() places constant string pointers in __tracepoint_str and exports their address-to-text mappings through printk_formats. This lets user space decode pointer fields without copying each string into every ring buffer record.
The linker collects the built-in __tracepoint_str section, but the module loader does not collect the corresponding module section. Consequently, module strings never reach printk_formats and tools such as trace-cmd show raw addresses when decoding trace.dat.
Collect module __tracepoint_str sections and register one live range for each module on MODULE_STATE_COMING. Include these ranges in printk_formats and trace_is_tracepoint_string() lookups.
Reference the module section directly instead of copying its strings, and remove the range on MODULE_STATE_GOING. The trace event notifier, which runs at priority 1, removes module events and resets the trace buffers before this priority-0 notifier drops the mapping.
Serialize range updates and printk_formats iteration with btrace_mutex. Keep the membership lookup under RCU because the tp_printk path can call it while holding tracepoint_iter_lock with interrupts disabled. Free the range descriptor with kfree_rcu(); module teardown waits for an RCU grace period before freeing the section that backs the range.
Cache the number of successfully inserted module trace_printk formats and directly index the matching module tracepoint-string range. Extend the trace_printk sample and ftrace selftest to verify that multiple module strings are exported while loaded and removed on unload.
---
Changes since v2:
- Capitalize Export in the subject as requested.
- Group the two counts and two pointers in struct module.
- Cache only successfully inserted unique trace_bprintk_fmt entries.
- Skip complete module ranges and directly index the matching range.
- Use guard() and early returns to simplify registry operations.
- Keep the IRQ-safe RCU read side and use kfree_rcu() for removal.
- Document the live module mapping and RCU lifetime.
- Add trace_printk sample coverage and an ftrace regression test.
- Add Fixes, Closes, Suggested-by, and review Link tags.
Testing:
- git diff --check
- scripts/checkpatch.pl --strict --no-tree
- sh -n tools/testing/selftests/ftrace/test.d/event/trace_printk.tc
- x86_64 defconfig W=1 builds of all touched kernel and sample objects
- CONFIG_MODULES=n W=1 build of kernel/trace/trace_printk.o
- Minimal x86_64 bzImage and modules build
- QEMU load/unload test, including 10 insmod/rmmod cycles concurrent with 100 reads of printk_formats; both module strings were present while the module was loaded and absent after unload, with no kernel diagnostics
v2: https://lore.kernel.org/r/[email protected]
include/linux/module.h | 2 + include/linux/tracepoint.h | 8 +- kernel/module/main.c | 4 + kernel/trace/trace_printk.c | 138 ++++++++++++++++-- samples/trace_printk/trace-printk.c | 4 + .../ftrace/test.d/event/trace_printk.tc | 5 + 6 files changed, 146 insertions(+), 15 deletions(-)
include/linux/module.h +2 -0
@@ -513,7 +513,9 @@ struct module {#endif #ifdef CONFIG_TRACING unsigned int num_trace_bprintk_fmt; +unsigned int num_tracepoint_strings;const char **trace_bprintk_fmt_start; +const char **tracepoint_strings_start;#endif #ifdef CONFIG_EVENT_TRACING struct trace_event_call **trace_events;
include/linux/tracepoint.h +4 -4
@@ -508,10 +508,10 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)* the ASCII strings they represent. * * The @str used must be a constant string and persistent as it would not -* make sense to show a string that no longer exists. But it is still fine -* to be used with modules, because when modules are unloaded, if they -* had tracepoints, the ring buffers are cleared too. As long as the string -* does not change during the life of the module, it is fine to use +* make sense to show a string that no longer exists. For modules, the string +* mapping is exported while the module is loaded. When the module is unloaded, +* its trace buffers are cleared before the mapping is removed. As long as the +* string does not change during the life of the module, it is fine to use * tracepoint_string() within a module. */ #define tracepoint_string (str) \
kernel/module/main.c +4 -0
@@ -2729,6 +2729,10 @@ static int find_module_sections(struct module *mod, struct load_info *info) mod->trace_bprintk_fmt_start =section_objs (info,"__trace_printk_fmt" ,sizeof (*mod->trace_bprintk_fmt_start), &mod->num_trace_bprintk_fmt); + mod->tracepoint_strings_start = +section_objs (info,"__tracepoint_str" , +sizeof (*mod->tracepoint_strings_start), + &mod->num_tracepoint_strings);#endif #ifdef CONFIG_DYNAMIC_FTRACE /* sechdrs[0].sh_size is always zero */
kernel/trace/trace_printk.c +127 -11
226 lines not on this page · raw message
samples/trace_printk/trace-printk.c +4 -0
@@ -2,6 +2,7 @@#include <linux/module.h> #include <linux/kthread.h> #include <linux/irq_work.h> +#include <linux/tracepoint.h> /* Must not be static to force gcc to consider these non constant */ char *trace_printk_test_global_str =
@@ -31,6 +32,9 @@ static int __init trace_printk_init(void) {init_irq_work (&irqwork, trace_printk_irq_work); + (void )tracepoint_string ("tracepoint_string_test_module_string_1" ); + (void )tracepoint_string ("tracepoint_string_test_module_string_2" ); +trace_printk ("This is a static string that will use trace_bputs\n" );trace_printk (trace_printk_test_global_str);
tools/testing/selftests/ftrace/test.d/event/trace_printk.tc +5 -0
@@ -23,5 +23,10 @@ grep -q ": (irq) .* trace_printk" trace grep -q "This is a %s that will use trace_bprintk" printk_formats grep -q "(irq) This is a static string that will use trace_bputs" printk_formats +grep -q "tracepoint_string_test_module_string_1" printk_formats +grep -q "tracepoint_string_test_module_string_2" printk_formats rmmod trace-printk ||: + +! grep -q "tracepoint_string_test_module_string_1" printk_formats +! grep -q "tracepoint_string_test_module_string_2" printk_formats
base-commit: d5b201ed4feb7a11da242a97669424efeb724031
Signature · 1 line
2.43.5
Re: [PATCH v3] tracing: Export live module tracepoint strings in printk_formats
Hi Steven,
Gentle ping on this v3.
While rechecking the selftest, I found that the unload checks can miss a
failure: if the first test string remains in printk_formats but the second is gone, the test still passes. The first negated grep returns nonzero, but set -e does not terminate the script for a negated command. I reproduced this with a fixture containing only the first string.
I would add explicit failure handling (|| exit_fail) to both checks. Would you prefer a v4 with this correction?
I can also make the final rmmod failure fatal and add a comment explaining that trace_bprintk_fmt_cnt counts permanently retained, unique formats and therefore is not decremented on module unload.
The current test checks the exported mappings, but does not exercise the new membership lookup through a TRACE_EVENT and the tp_printk path. I can extend coverage there as well if you would like it included in v4.
Thanks,
Cao Ruichuang
Steven Rostedt
maintainerRe: [PATCH v3] tracing: Export live module tracepoint strings in printk_formats
In reply to Cao RuichuangOn Tue, 11 Aug 2026 18:15:13 +0000
Cao Ruichuang <[email protected]> wrote:
Hi Cao,
Sorry for the late reply, with AI bugs coming in, as well as travel and conferences, I fell very behind in submitted patches.
Quoted 53 lines
include/linux/module.h +2
-0@@ -513,7 +513,9 @@ struct module {#endif #ifdef CONFIG_TRACING unsigned int num_trace_bprintk_fmt; +unsigned int num_tracepoint_strings;const char **trace_bprintk_fmt_start; +const char **tracepoint_strings_start;#endif #ifdef CONFIG_EVENT_TRACING struct trace_event_call **trace_events;include/linux/tracepoint.h +4
-4@@ -508,10 +508,10 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)* the ASCII strings they represent. * * The @str used must be a constant string and persistent as it would not -* make sense to show a string that no longer exists. But it is still fine -* to be used with modules, because when modules are unloaded, if they -* had tracepoints, the ring buffers are cleared too. As long as the string -* does not change during the life of the module, it is fine to use +* make sense to show a string that no longer exists. For modules, the string +* mapping is exported while the module is loaded. When the module is unloaded, +* its trace buffers are cleared before the mapping is removed. As long as the +* string does not change during the life of the module, it is fine to use * tracepoint_string() within a module. */ #define tracepoint_string (str) \kernel/module/main.c +4
-0@@ -2729,6 +2729,10 @@ static int find_module_sections(struct module *mod, struct load_info *info) mod->trace_bprintk_fmt_start =section_objs (info,"__trace_printk_fmt" ,sizeof (*mod->trace_bprintk_fmt_start), &mod->num_trace_bprintk_fmt); + mod->tracepoint_strings_start = +section_objs (info,"__tracepoint_str" , +sizeof (*mod->tracepoint_strings_start), + &mod->num_tracepoint_strings);#endif #ifdef CONFIG_DYNAMIC_FTRACE /* sechdrs[0].sh_size is always zero */ kernel/trace/trace_printk.c +0
-0
kernel/trace/trace_printk.c line 16
Quoted 4 lines
kernel/trace/trace_printk.c +1
-0@@ -13,21 +13,29 @@#include <linux/string.h> #include <linux/module.h> #include <linux/mutex.h> +#include <linux/rcupdate.h>
Please keep this in "upside-down x-mas tree" format. The order of headers goes by the length of their names.
Quoted 71 lines
kernel/trace/trace_printk.c +35
-1#include <linux/ctype.h> #include <linux/list.h> #include <linux/slab.h> #include "trace.h" +static int trace_bprintk_fmt_cnt; +#ifdef CONFIG_MODULES /* * modules trace_printk()'s formats are autosaved in struct trace_bprintk_fmt * which are queued on trace_bprintk_fmt_list. +* +* modules tracepoint_string() entries are kept as ranges into the owning +* module's __tracepoint_str section and are removed again when the module +* goes away. */ static LIST_HEAD (trace_bprintk_fmt_list); +static LIST_HEAD (tracepoint_str_list); -/* serialize accesses to trace_bprintk_fmt_list */ +/* serialize updates and printk_formats iteration of the module lists */ static DEFINE_MUTEX (btrace_mutex); struct trace_bprintk_fmt {@@ -35,6 +43,14 @@ struct trace_bprintk_fmt {const char *fmt; }; +struct tracepoint_mod_str { +struct list_head list; +struct rcu_head rcu; +struct module *mod; +const char **start; +unsigned int num; +}; +static inline struct trace_bprintk_fmt *lookup_format (const char *fmt) {struct trace_bprintk_fmt *pos;@@ -75,6 +91,7 @@ void hold_module_trace_bprintk_format(const char **start, const char **end)if (fmt) {list_add_tail (&tb_fmt->list, &trace_bprintk_fmt_list); tb_fmt->fmt = fmt; + trace_bprintk_fmt_cnt++; }else kfree (tb_fmt); }@@ -84,16 +101,63 @@ void hold_module_trace_bprintk_format(const char **start, const char **end)mutex_unlock (&btrace_mutex); } +static void hold_module_tracepoint_strings (struct module *mod) +{ +struct tracepoint_mod_str *tp_str; + +if (!mod->num_tracepoint_strings) +return ; + + tp_str =kmalloc_obj (*tp_str); +if (!tp_str) +return ; + + tp_str->mod = mod; + tp_str->start = mod->tracepoint_strings_start; + tp_str->num = mod->num_tracepoint_strings; + +guard (mutex)(&btrace_mutex); +list_add_tail_rcu (&tp_str->list, &tracepoint_str_list); +}
kernel/trace/trace_printk.c void hold_module_trace_bprintk_format(const char **start, const char **end) · line 131
Quoted 10 lines
kernel/trace/trace_printk.c +10
-0+ +static void release_module_tracepoint_strings (struct module *mod) +{ +struct tracepoint_mod_str *tp_str; + +if (!mod->num_tracepoint_strings) +return ; + +guard (mutex)(&btrace_mutex); +list_for_each_entry (tp_str, &tracepoint_str_list, list) {
Please use list_for_each_entry_safe(). You are removing tp_str from the list. Do not rely on internals of RCU to protect this. If the lock was a spin lock, then it may have been safe (even though I would still ask for this change). But because it is a mutex, this could be preempted, the RCU grace period finish, the tp_str freed and then the loop will crash.
Quoted 163 lines
kernel/trace/trace_printk.c +81
-10+if (tp_str->mod != mod) +continue ; + +list_del_rcu (&tp_str->list); +/* Module teardown waits for RCU before freeing the section. */ +kfree_rcu (tp_str, rcu); +return ; + } +} +static int module_trace_bprintk_format_notify (struct notifier_block *self,unsigned long val,void *data) {struct module *mod = data; -if (mod->num_trace_bprintk_fmt) { -const char **start = mod->trace_bprintk_fmt_start; -const char **end = start + mod->num_trace_bprintk_fmt; -if (val ==MODULE_STATE_COMING ) +switch (val) { +case MODULE_STATE_COMING : +if (mod->num_trace_bprintk_fmt) { +const char **start = mod->trace_bprintk_fmt_start; +const char **end = start + mod->num_trace_bprintk_fmt; +hold_module_trace_bprintk_format (start, end); + } +hold_module_tracepoint_strings (mod); +break ; +case MODULE_STATE_GOING : +/* Trace event teardown runs first and clears module event buffers. */ +release_module_tracepoint_strings (mod); +break ; }return NOTIFY_OK ; }@@ -119,7 +183,7 @@ static int module_trace_bprintk_format_notify(struct notifier_block *self,* next format in the list. */ static const char ** -find_next_mod_format (int start_index,void *v,const char **fmt,loff_t *pos) +find_next_mod_format (loff_t start_index,void *v,const char **fmt,loff_t *pos) {struct trace_bprintk_fmt *mod_fmt;@@ -158,6 +222,42 @@ find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos)return &mod_fmt->fmt; } +static const char ** +find_next_mod_tracepoint_str (loff_t start_index,loff_t *pos) +{ +struct tracepoint_mod_str *tp_str; +loff_t offset = *pos - start_index; + +if (offset <0 ) +return NULL ; + +list_for_each_entry (tp_str, &tracepoint_str_list, list) { +if (offset < (loff_t )tp_str->num) +return tp_str->start + offset; + + offset -= tp_str->num; + } + +return NULL ; +} + +static bool is_module_tracepoint_string (const char *str) +{ +struct tracepoint_mod_str *tp_str; +unsigned int i; + +/* The tracepoint printk path can call this with IRQs disabled. */ +guard (rcu)(); +list_for_each_entry_rcu (tp_str, &tracepoint_str_list, list) { +for (i =0 ; i < tp_str->num; i++) { +if (str == tp_str->start[i]) +return true; + } + } + +return false; +} +static void format_mod_start (void ) {mutex_lock (&btrace_mutex);@@ -176,10 +276,21 @@ module_trace_bprintk_format_notify(struct notifier_block *self, return NOTIFY_OK; }static inline const char ** -find_next_mod_format (int start_index,void *v,const char **fmt,loff_t *pos) +find_next_mod_format (loff_t start_index,void *v,const char **fmt,loff_t *pos) +{ +return NULL ; +} + +static inline const char ** +find_next_mod_tracepoint_str (loff_t start_index,loff_t *pos) {return NULL ; } + +static inline bool is_module_tracepoint_string (const char *str) +{ +return false; +}static inline void format_mod_start (void ) { }static inline void format_mod_stop (void ) { }#endif /* CONFIG_MODULES */ @@ -194,6 +305,7 @@ void trace_printk_control(bool enabled)__initdata_or_module static struct notifier_block module_trace_bprintk_format_nb = { .notifier_call = module_trace_bprintk_format_notify, + .priority =0 ,/* lower than the trace event notifier */ };__printf (2 ,3 )@@ -259,14 +371,14 @@ bool trace_is_tracepoint_string(const char *str)if (str == *ptr)return true; } -return false; +return is_module_tracepoint_string (str); }static const char **find_next (void *v,loff_t *pos) {const char **fmt = v; -int start_index; -int last_index; +loff_t start_index; +loff_t last_index; start_index = __stop___trace_bprintk_fmt - __start___trace_bprintk_fmt;@@ -292,7 +404,11 @@ static const char **find_next(void *v, loff_t *pos)return __start___tracepoint_str + (*pos - last_index); start_index += last_index; -return find_next_mod_format (start_index, v, fmt, pos); +if (*pos - start_index < trace_bprintk_fmt_cnt) +return find_next_mod_format (start_index, v, fmt, pos); + + start_index += trace_bprintk_fmt_cnt; +return find_next_mod_tracepoint_str (start_index, pos); } static void *samples/trace_printk/trace-printk.c +1
-0@@ -2,6 +2,7 @@#include <linux/module.h> #include <linux/kthread.h> #include <linux/irq_work.h> +#include <linux/tracepoint.h> /* Must not be static to force gcc to consider these non constant */ char *trace_printk_test_global_str =
samples/trace_printk/trace-printk.c static int __init trace_printk_init(void) · line 36
Quoted 5 lines
samples/trace_printk/trace-printk.c +2
-0@@ -31,6 +32,9 @@ static int __init trace_printk_init(void) {init_irq_work (&irqwork, trace_printk_irq_work); + (void )tracepoint_string ("tracepoint_string_test_module_string_1" ); + (void )tracepoint_string ("tracepoint_string_test_module_string_2" );
Actually this shouldn't be in trace_printk() as that's not how it would be used. Please add it to samples. Something like this:
samples/trace_events/trace-events-sample.c +4 -0
@@ -51,6 +51,10 @@ static void do_simple_thread_func(int cnt, const char *fmt, ...)trace_foo_with_template_print ("I have to be different" , cnt);trace_foo_rel_loc ("Hello __rel_loc" , cnt, bitmask, current->cpus_ptr); + +trace_foo_tracepoint_str (tracepoint_string ("tracepoint_string_test_module_string_1" )); +trace_foo_tracepoint_str (tracepoint_string ("tracepoint_string_test_module_string_2" )); + }static void simple_thread_func (int cnt)
samples/trace_events/trace-events-sample.h +17 -0
@@ -623,6 +623,23 @@ TRACE_EVENT(foo_timer_fn,TP_printk ("name=%s count=%d" ,__get_str (name), __entry->count) ); + +TRACE_EVENT (foo_tracepoint_str, + + TP_PROTO(const char *str), + +TP_ARGS (str), + +TP_STRUCT__entry ( + __field( const char *, str ) + ), + +TP_fast_assign ( + __entry->str = str; + ), + +TP_printk ("str=%s" , __entry->str) +);#endif /***** NOTICE! The #if protection ends here. *****/
Quoted 9 lines
samples/trace_printk/trace-printk.c +1
-0+trace_printk ("This is a static string that will use trace_bputs\n" );trace_printk (trace_printk_test_global_str);tools/testing/selftests/ftrace/test.d/event/trace_printk.tc +0
-0
tools/testing/selftests/ftrace/test.d/event/trace_printk.tc grep -q ": (irq) .* trace_printk" trace · line 32
Quoted 10 lines
tools/testing/selftests/ftrace/test.d/event/trace_printk.tc +5
-0@@ -23,5 +23,10 @@ grep -q ": (irq) .* trace_printk" trace grep -q "This is a %s that will use trace_bprintk" printk_formats grep -q "(irq) This is a static string that will use trace_bputs" printk_formats +grep -q "tracepoint_string_test_module_string_1" printk_formats +grep -q "tracepoint_string_test_module_string_2" printk_formats rmmod trace-printk ||: + +! grep -q "tracepoint_string_test_module_string_1" printk_formats +! grep -q "tracepoint_string_test_module_string_2" printk_formats
You would need different test that relies on the trace event sample code and do this test there.
Quoted 2 lines
base-commit:
d5b201ed4feb7a11da242a97669424efeb724031
Thanks,
-- Steve