tracing: Export live module tracepoint strings in printk_formats
Subsystems (3)
PATCH v4 1/1 tracing: Export live module tracepoint strings in printk_formats
tracepoint_string() mappings for built-in strings are exported through printk_formats, but the module loader does not collect __tracepoint_str. Consequently, user space cannot resolve these module string pointers.
Collect the module section and expose its live address-to-text mappings. Serialize registration, removal and the full printk_formats seq_file iteration with btrace_mutex. Remove the range before module memory is freed; do not retain strings after unload.
Keep trace_is_tracepoint_string() and event string validation unchanged. Exporting decoder metadata does not require accepting module pointers in core or other modules' events, nor does it extend the strings' lifetime.
Count successfully retained unique module trace_printk formats so the iterator can locate the following module string ranges. These formats remain retained after unload, so the count is not decremented.
Add a sample TRACE_EVENT and a dedicated selftest for multiple exported strings, event output and mapping removal. Make unload and every residual string/read-error check fail explicitly.
---
Hi Steven,
Thanks for the review.
I have kept this revision focused on exporting live module tracepoint-string mappings through printk_formats.
I rebased it onto your for-next at d7ff48d92a0f and adjusted the sample header insertion to follow the new foo_timer_fn event. I reran the builds, ftrace tests and decoder checks on this base.
Quoted 1 line
Please use
list_for_each_entry_safe().
I now use list_for_each_entry_safe() for removal.
Quoted 1 line
Please add it to samples.
The example is now in samples/trace_events, with a TRACE_EVENT that records both test strings.
A dedicated ftrace selftest now checks their mappings, event output and removal on unload. The unload checks explicitly fail for either remaining string, a read error, or a failed rmmod. I also added a comment explaining why the retained-format count is not decremented on module unload.
Quoted 1 line
The order of headers goes by the length of their names.
There is one scope change from v3: I removed the module-string membership extension to trace_is_tracepoint_string(). The existing module-owner check already permits the sample event to print its own module's strings. Userspace decoding needs the exported mappings, without broadening the kernel's string safety checks. The module ranges are now used only by printk_formats and protected throughout iteration by btrace_mutex, so the RCU list operations and added rcupdate.h include are no longer needed.
I also verified the original use case with trace-cmd 3.3.1: record the sample event into trace.dat, then decode it with trace-cmd report without plugins. Both module strings resolve correctly, including after module unload and in a separate offline decoder on the host. With the export implementation removed, the same workload records the events but report shows only addresses. The kernel's sample-event text output still works in that control, confirming that the membership extension is unnecessary for this case. W=1 builds, CONFIG_MODULES=n, shell regression fixtures and the preceding QEMU tests with KASAN, lockdep and tp_printk also passed.
Allowing events owned by the core or another module to dereference these strings needs protection through the final %s use and a defined unload policy. My separate local changes use buffer cleanup for that, including offline CPUs, snapshot synchronization and hotplug protection; they remain outside this revision.
Would you prefer that additional support as a separate series, or included with this fix?
Thanks,
Cao Ruichuang
Changes since v3:
- Limit the patch to printk_formats export; leave event string validation unchanged.
- Use mutex-only range lifetime and list_for_each_entry_safe() on removal.
- Move the sample to trace_events and add a dedicated regression test.
- Fail explicitly on residual strings, read errors and final rmmod failure.
- Explain the permanently retained format count.
- Rebase onto tracing for-next and follow the new foo_timer_fn sample event.
AI assistance (including GPT-6.1-Sol) was used for code changes, selftests, validation scripts and preparation of this message. The requests were to address the v3 review feedback, keep this patch limited to live module string export, and investigate lifetime changes separately. Validation is summarized below.
Validation on this base:
- W=1 kernel/modules and CONFIG_MODULES=n object builds.
- Shell regression fixtures and QEMU ftrace/load-unload tests with KASAN/lockdep.
- Real trace-cmd record/report, module unload and host offline decoding.
- Export-removed negative controls reproduce missing mappings and raw addresses.
include/linux/module.h | 2 + include/linux/tracepoint.h | 9 +- kernel/module/main.c | 4 + kernel/trace/trace_printk.c | 111 ++++++++++++++++-- samples/trace_events/trace-events-sample.c | 3 + samples/trace_events/trace-events-sample.h | 17 +++ .../ftrace/test.d/event/tracepoint_string.tc | 47 ++++++++ 7 files changed, 178 insertions(+), 15 deletions(-) create mode 100644 tools/testing/selftests/ftrace/test.d/event/tracepoint_string.tc
include/linux/module.h +2 -0
@@ -509,7 +509,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 -5
@@ -508,11 +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 -* tracepoint_string() within a module. +* make sense to show a string that no longer exists. For modules, the string +* mapping is exported while the module is loaded and removed on unload. +* Exporting the mapping does not extend the lifetime of the string. The +* existing lifetime requirements for strings referenced by events still apply. */ #define tracepoint_string (str) \ ({ \
kernel/module/main.c +4 -0
@@ -2740,6 +2740,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 +101 -10
182 lines not on this page · raw message
samples/trace_events/trace-events-sample.c +3 -0
@@ -51,6 +51,9 @@ 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. *****/
tools/testing/selftests/ftrace/test.d/event/tracepoint_string.tc new file +47 -0
47 lines not on this page · raw message
base-commit: d7ff48d92a0f28bc0612da47c054efbc28c19562
Signature · 1 line
2.47.3