tracing: Updates for v7.3
Subsystems (5)
cover tracing: Updates for v7.3
git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git
trace/for-next
Head SHA1: efdcfd9b4f4f07aa70663ee02a79dbdf980bf582
David Laight (1):
kernel/trace/trace_printk: Use kstrdup() instead of kmalloc() and strcpy()
Markus Elfring (3):
tracing/user_events: Use seq_putc() in two functions tracing/user_events: Replace a seq_printf() call by seq_puts() in user_seq_show()
fgraph: Use trace_seq_putc() in print_graph_return()
Mykyta Yatsenko (3):
bpf: Make btf_get_module_btf() and btf_relocate_id() non-static
tracing: Expose tracepoint BTF ids via tracefs selftests/bpf: Add test for tracepoint btf_ids tracefs file
Pengpeng Hou (3):
tracing: Use __free() for expr_str() buffer
tracing: Return ERR_PTR() from expr_str()
tracing: Bound histogram expression strings with seq_buf
Po-Sheng Lin (1):
tracing: Use strscpy() instead of strcpy() in trace_sched_switch
Samuel Moelius (2):
tracing: Reject invalid preemptirq_delay_test CPU affinity samples/ftrace: Prevent division by zero when nr_function_calls is zero
Steven Rostedt (1):
tracing: Warn when an event dereferences a pointer in TP_printk()
Woradorn Laodhanadhaworn (1):
tracing: Use seq_buf for string concatenation
Yu Peng (1):
tracing: Point constant hist field type to string literal
----
include/linux/btf.h | 2 + include/linux/trace_events.h | 9 ++ include/trace/trace_events.h | 24 ++++ kernel/bpf/btf.c | 4 +- kernel/trace/preemptirq_delay_test.c | 9 ++ kernel/trace/trace_events.c | 131 ++++++++++++++++++-- kernel/trace/trace_events_hist.c | 96 +++++++++------ kernel/trace/trace_events_user.c | 10 +- kernel/trace/trace_functions_graph.c | 2 +- kernel/trace/trace_printk.c | 3 +- kernel/trace/trace_sched_switch.c | 6 +- kernel/trace/trace_syscalls.c | 17 +++ samples/ftrace/ftrace-ops.c | 2 +- .../testing/selftests/bpf/prog_tests/tp_btf_ids.c | 132 +++++++++++++++++++++ 14 files changed, 385 insertions(+), 62 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/tp_btf_ids.c
PATCH 1/16 bpf: Make btf_get_module_btf() and btf_relocate_id() non-static
From: Mykyta Yatsenko <[email protected]>
Drop the static qualifier and add prototypes to <linux/btf.h> so the tracing core can look up module BTF and translate ids stored by resolve_btfids (which are local to a module's split BTF) into the runtime ids used by the kernel.
Used by the upcoming events/<sys>/<event>/btf_ids tracefs interface.
--- include/linux/btf.h | 2 ++ kernel/bpf/btf.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-)
include/linux/btf.h +2 -0
@@ -235,6 +235,8 @@ int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec);bool btf_type_is_void (const struct btf_type *t);s32 btf_find_by_name_kind (const struct btf *btf,const char *name,u8 kind);s32 bpf_find_btf_id (const char *name,u32 kind,struct btf **btf_p); +struct btf *btf_get_module_btf (const struct module *module); +__u32 btf_relocate_id (const struct btf *btf,__u32 id);const struct btf_type *btf_type_skip_modifiers (const struct btf *btf,u32 id,u32 *res_id); const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
kernel/bpf/btf.c +2 -2
@@ -6467,7 +6467,7 @@ struct btf *btf_parse_vmlinux(void)* split BTF ids will need to be mapped to actual base/split ids for * BTF now that it has been relocated. */ -static __u32 btf_relocate_id (const struct btf *btf,__u32 id) +__u32 btf_relocate_id (const struct btf *btf,__u32 id) {if (!btf->base_btf || !btf->base_id_map)return id;
@@ -8630,7 +8630,7 @@ struct module *btf_try_get_module(const struct btf *btf)/* Returns struct btf corresponding to the struct module. * This function can return NULL or ERR_PTR. */ -static struct btf *btf_get_module_btf (const struct module *module) +struct btf *btf_get_module_btf (const struct module *module) {#ifdef CONFIG_DEBUG_INFO_BTF_MODULES struct btf_module *btf_mod, *tmp;
Signature · 1 line
2.53.0
PATCH 2/16 tracing: Expose tracepoint BTF ids via tracefs
In reply to Steven RostedtFrom: Mykyta Yatsenko <[email protected]>
Add events/<sys>/<event>/btf_ids, a per-template file that exposes the BTF ids resolve_btfids fills in for each tracepoint:
btf_obj_id BTF object owning the ids below
raw_btf_id FUNC_PROTO of __bpf_trace_<call> (named args), consumed
by raw_tp / tp_btf BPF programs
tp_btf_id trace_event_raw_<call> ring-buffer record, consumed by
classic BPF_PROG_TYPE_TRACEPOINT programs
DECLARE_EVENT_CLASS now emits a 2-entry BTF_ID_LIST (FUNC __bpf_trace_* and STRUCT trace_event_raw_*) and stores the pointer in trace_event_class.
Per-syscall events under syscalls/ share the handcrafted classes event_class_syscall_{enter,exit} instead of going through DECLARE_EVENT_CLASS. Wire those classes to the BTF id lists generated for sys_enter / sys_exit so all ~700 per-syscall events expose the shared dispatcher prototype and record. The per-syscall events do not own their own tracepoint (they share sys_enter/sys_exit), so raw_btf_id is reported as 0 on those events; the meaningful raw_btf_id is exposed on raw_syscalls/sys_{enter,exit}/btf_ids where raw_tp / tp_btf programs can actually attach.
--- include/linux/trace_events.h | 9 ++++ include/trace/trace_events.h | 24 +++++++++++ kernel/trace/trace_events.c | 80 ++++++++++++++++++++++++++++++++++- kernel/trace/trace_syscalls.c | 17 ++++++++ 4 files changed, 129 insertions(+), 1 deletion(-)
include/linux/trace_events.h +9 -0
@@ -298,6 +298,15 @@ struct trace_event_class {struct list_head *(*get_fields)(struct trace_event_call *);struct list_head fields;int (*raw_init)(struct trace_event_call *); +#ifdef CONFIG_BPF_EVENTS +/* +* Per-template BTF ids set by DECLARE_EVENT_CLASS via BTF_ID() and +* patched by resolve_btfids at link time. NULL for handcrafted classes. +* [0] FUNC __bpf_trace_<template> +* [1] STRUCT trace_event_raw_<template> +*/ +const u32 *btf_ids; +#endif }; extern int trace_event_reg(struct trace_event_call *event,
include/trace/trace_events.h +24 -0
@@ -19,6 +19,7 @@*/ #include <linux/trace_events.h> +#include <linux/btf_ids.h> #ifndef TRACE_SYSTEM_VAR #define TRACE_SYSTEM_VAR TRACE_SYSTEM
@@ -397,6 +398,27 @@ static inline notrace int trace_event_get_offsets_##call( \#define _TRACE_PERF_INIT (call)#endif /* CONFIG_PERF_EVENTS */ +#ifdef CONFIG_BPF_EVENTS +/* +* Per-template BTF id list, populated at link time by resolve_btfids: +* [0] FUNC __bpf_trace_<call> (the BPF dispatcher) +* [1] STRUCT trace_event_raw_<call> (the ring-buffer record) +* Exposed via the events/<sys>/<name>/btf_ids tracefs file. +*/ +#define _TRACE_BTF_IDS_DECLARE (call) \ + extern u32 __bpf_trace_btf_ids_##call[]; \ + BTF_ID_LIST_GLOBAL(__bpf_trace_btf_ids_##call, 2) \ + BTF_ID(func, __bpf_trace_##call) \ + BTF_ID(struct, trace_event_raw_##call) + +#define _TRACE_BTF_IDS_INIT (call) \ + .btf_ids = __bpf_trace_btf_ids_##call, + +#else +#define _TRACE_BTF_IDS_DECLARE (call) +#define _TRACE_BTF_IDS_INIT (call) +#endif /* CONFIG_BPF_EVENTS */ +#include "stages/stage6_event_callback.h"
@@ -474,6 +496,7 @@ static inline void ftrace_test_probe_##call(void) \#undef DECLARE_EVENT_CLASS#define DECLARE_EVENT_CLASS (call, proto, args, tstruct, assign, print) \ _TRACE_PERF_PROTO(call, PARAMS(proto)); \ +_TRACE_BTF_IDS_DECLARE(call) \ static char print_fmt_##call[] = print; \ static struct trace_event_class __used __refdata event_class_##call = { \ .system = TRACE_SYSTEM_STRING, \
@@ -483,6 +506,7 @@ static struct trace_event_class __used __refdata event_class_##call = { \ .probe = trace_event_raw_event_##call, \ .reg = trace_event_reg, \ _TRACE_PERF_INIT(call) \ + _TRACE_BTF_IDS_INIT(call) \ };#undef DECLARE_EVENT_SYSCALL_CLASS
kernel/trace/trace_events.c +79 -1
110 lines not on this page · raw message
kernel/trace/trace_syscalls.c +17 -0
@@ -1303,12 +1303,26 @@ struct trace_event_functions exit_syscall_print_funcs = { .trace = print_syscall_exit, }; +#ifdef CONFIG_BPF_EVENTS +/* +* BTF id lists generated by DECLARE_EVENT_CLASS for the sys_enter and +* sys_exit tracepoints. The auto-generated event_class_sys_{enter,exit} +* is unused (per-syscall events share the handcrafted classes below), +* but the id lists themselves are global and reusable. +*/ +extern u32 __bpf_trace_btf_ids_sys_enter[]; +extern u32 __bpf_trace_btf_ids_sys_exit[]; +#endif +struct trace_event_class __refdata event_class_syscall_enter = { .system ="syscalls" , .reg = syscall_enter_register, .fields_array = syscall_enter_fields_array, .get_fields = syscall_get_enter_fields, .raw_init = init_syscall_trace, +#ifdef CONFIG_BPF_EVENTS + .btf_ids = __bpf_trace_btf_ids_sys_enter, +#endif };struct trace_event_class __refdata event_class_syscall_exit = {
@@ -1321,6 +1335,9 @@ struct trace_event_class __refdata event_class_syscall_exit = { }, .fields = LIST_HEAD_INIT(event_class_syscall_exit.fields), .raw_init = init_syscall_trace, +#ifdef CONFIG_BPF_EVENTS + .btf_ids = __bpf_trace_btf_ids_sys_exit, +#endif };unsigned long __init __weak arch_syscall_addr (int nr)
Signature · 1 line
2.53.0
PATCH 3/16 selftests/bpf: Add test for tracepoint btf_ids tracefs file
In reply to Steven RostedtFrom: Mykyta Yatsenko <[email protected]>
Read events/bpf_testmod/bpf_testmod_test_read/btf_ids and verify the exported FUNC_PROTO matches the testmod tracepoint signature (__data, struct task_struct *task, struct bpf_testmod_test_read_ctx *ctx) and the record struct trace_event_raw_bpf_testmod_test_read carries the fields declared by TP_STRUCT__entry.
Use the testmod tracepoint so the test exercises the module/split-BTF path (btf_relocate_id) rather than vmlinux only, and falls back from /sys/kernel/tracing to /sys/kernel/debug/tracing when tracefs is not mounted at the new location.
--- .../selftests/bpf/prog_tests/tp_btf_ids.c | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/tp_btf_ids.c
tools/testing/selftests/bpf/prog_tests/tp_btf_ids.c new file +132 -0
@@ -0,0 +1,132 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include <bpf/btf.h> + +#define TRACEFS "/sys/kernel/tracing" +#define DEBUGFS_TRACING "/sys/kernel/debug/tracing" +#define EVENT_SUBPATH "events/bpf_testmod/bpf_testmod_test_read/btf_ids" + +struct btf_ids_info { +__u32 obj_id; +__u32 raw_id; +__u32 tp_id; +}; + +static const char *btf_ids_path (char *buf,size_t sz) +{ +if (access (TRACEFS "/trace" ,F_OK ) ==0 ) +snprintf (buf, sz,"%s/%s" ,TRACEFS ,EVENT_SUBPATH ); +else +snprintf (buf, sz,"%s/%s" ,DEBUGFS_TRACING ,EVENT_SUBPATH ); +return buf; +} + +static int read_btf_ids (struct btf_ids_info *info) +{ +char path[256 ], buf[256 ]; +int fd, n; + + fd =open (btf_ids_path (path,sizeof (path)),O_RDONLY ); +if (fd <0 ) +return -errno; + + n =read (fd, buf,sizeof (buf) -1 ); +close (fd); +if (n <=0 ) +return -EIO ; + buf[n] ='\0' ; + +if (sscanf (buf, +"btf_obj_id: %u\nraw_btf_id: %u\ntp_btf_id: %u\n" , + &info->obj_id, &info->raw_id, &info->tp_id) !=3 ) +return -EINVAL ; +return 0 ; +} + +static const char *param_name (struct btf *btf,const struct btf_param *p) +{ +return btf__name_by_offset (btf, p->name_off); +} + +static const char *member_name (struct btf *btf,const struct btf_member *m) +{ +return btf__name_by_offset (btf, m->name_off); +} + +void test_tp_btf_ids (void ) +{ +const struct btf_type *proto_t, *rec_t; +const struct btf_param *params; +const struct btf_member *members; +struct btf_ids_info info; +struct btf *vmlinux_btf, *btf; +const char *name; +int err; + +if (!env.has_testmod) { +test__skip (); +return ; + } + + err =read_btf_ids (&info); +if (!ASSERT_OK (err,"read btf_ids" )) +return ; + +ASSERT_GT (info.obj_id,0 ,"obj_id non-zero" ); +ASSERT_GT (info.raw_id,0 ,"raw_id non-zero" ); +ASSERT_GT (info.tp_id,0 ,"tp_id non-zero" ); + + vmlinux_btf =btf__load_vmlinux_btf (); +if (!ASSERT_OK_PTR (vmlinux_btf,"load vmlinux BTF" )) +return ; + +/* Module BTF is split BTF; load with vmlinux as base. */ + btf =btf__load_from_kernel_by_id_split (info.obj_id, vmlinux_btf); +if (!ASSERT_OK_PTR (btf,"load module BTF" )) { +btf__free (vmlinux_btf); +return ; + } + +/* +* raw_btf_id should be the FUNC_PROTO of __bpf_trace_<call>: +* void *__data, struct task_struct *task, +* struct bpf_testmod_test_read_ctx *ctx +*/ + proto_t =btf__type_by_id (btf, info.raw_id); +if (!ASSERT_OK_PTR (proto_t,"raw type_by_id" )) + goto out; +if (!ASSERT_TRUE (btf_is_func_proto (proto_t),"raw is FUNC_PROTO" )) + goto out; +if (!ASSERT_EQ (btf_vlen (proto_t),3 ,"func_proto arg count" )) + goto out; + + params =btf_params (proto_t); +ASSERT_STREQ (param_name (btf, ¶ms[0 ]),"__data" ,"arg0 name" ); +ASSERT_STREQ (param_name (btf, ¶ms[1 ]),"task" ,"arg1 name" ); +ASSERT_STREQ (param_name (btf, ¶ms[2 ]),"ctx" ,"arg2 name" ); + +/* +* tp_btf_id should be STRUCT trace_event_raw_<call> with the +* fields declared by TP_STRUCT__entry plus the common header. +*/ + rec_t =btf__type_by_id (btf, info.tp_id); +if (!ASSERT_OK_PTR (rec_t,"tp type_by_id" )) + goto out; +if (!ASSERT_TRUE (btf_is_struct (rec_t),"tp is STRUCT" )) + goto out; + name =btf__name_by_offset (btf, rec_t->name_off); +ASSERT_STREQ (name,"trace_event_raw_bpf_testmod_test_read" , +"tp struct name" ); +if (!ASSERT_GE (btf_vlen (rec_t),5 ,"tp struct field count" )) + goto out; + + members =btf_members (rec_t); +ASSERT_STREQ (member_name (btf, &members[0 ]),"ent" ,"field0 name" ); +ASSERT_STREQ (member_name (btf, &members[1 ]),"pid" ,"field1 name" ); +ASSERT_STREQ (member_name (btf, &members[2 ]),"comm" ,"field2 name" ); +ASSERT_STREQ (member_name (btf, &members[3 ]),"off" ,"field3 name" ); +ASSERT_STREQ (member_name (btf, &members[4 ]),"len" ,"field4 name" ); +out: +btf__free (btf); +btf__free (vmlinux_btf); +}
Signature · 1 line
2.53.0
PATCH 4/16 tracing: Point constant hist field type to string literal
In reply to Steven RostedtFrom: Yu Peng <[email protected]>
The HIST_FIELD_FL_CONST path uses the fixed "u64" type string.
Point hist_field->type directly to the string literal, matching the HIST_FIELD_FL_HITCOUNT path. The release path already uses kfree_const(), so no duplication is needed.
--- kernel/trace/trace_events_hist.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/trace/trace_events_hist.c +1 -3
@@ -1984,9 +1984,7 @@ static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,if (flags &HIST_FIELD_FL_CONST ) { hist_field->fn_num =HIST_FIELD_FN_CONST ; hist_field->size =sizeof (u64); - hist_field->type =kstrdup ("u64" ,GFP_KERNEL ); -if (!hist_field->type) - goto free; + hist_field->type ="u64" ; goto out; }
Signature · 1 line
2.53.0
PATCH 5/16 kernel/trace/trace_printk: Use kstrdup() instead of kmalloc() and strcpy()
In reply to Steven RostedtFrom: David Laight <[email protected]>
--- kernel/trace/trace_printk.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/trace/trace_printk.c +1 -2
@@ -71,10 +71,9 @@ void hold_module_trace_bprintk_format(const char **start, const char **end) fmt =NULL ; tb_fmt =kmalloc_obj (*tb_fmt);if (tb_fmt) { - fmt =kmalloc (strlen (*iter) +1 ,GFP_KERNEL ); + fmt =kstrdup (*iter,GFP_KERNEL );if (fmt) {list_add_tail (&tb_fmt->list, &trace_bprintk_fmt_list); -strcpy (fmt, *iter); tb_fmt->fmt = fmt; }else kfree (tb_fmt);
Signature · 1 line
2.53.0
PATCH 6/16 tracing: Use __free() for expr_str() buffer
In reply to Steven RostedtFrom: Pengpeng Hou <[email protected]>
expr_str() allocates a temporary expression buffer and manually frees it on some error paths.
Convert the buffer to __free(kfree) and return it with return_ptr() on success. This keeps ownership handling separate from the later ERR_PTR() conversion and string-bound change.
--- kernel/trace/trace_events_hist.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
kernel/trace/trace_events_hist.c +6 -8
@@ -1759,7 +1759,7 @@ static void expr_field_str(struct hist_field *field, char *expr)static char *expr_str (struct hist_field *field,unsigned int level) { -char *expr; + char *expr __free(kfree) = NULL;if (level >1 )return NULL ;
@@ -1770,7 +1770,7 @@ static char *expr_str(struct hist_field *field, unsigned int level)if (!field->operands[0 ]) {expr_field_str (field, expr); -return expr; +return_ptr (expr); }if (field->operator ==FIELD_OP_UNARY_MINUS ) {
@@ -1778,16 +1778,15 @@ static char *expr_str(struct hist_field *field, unsigned int level)strcat (expr,"-(" ); subexpr =expr_str (field->operands[0 ], ++level); -if (!subexpr) { -kfree (expr); +if (!subexpr)return NULL ; - } +strcat (expr, subexpr);strcat (expr,")" );kfree (subexpr); -return expr; +return_ptr (expr); }expr_field_str (field->operands[0 ], expr);
@@ -1806,13 +1805,12 @@ static char *expr_str(struct hist_field *field, unsigned int level)strcat (expr,"*" );break ;default : -kfree (expr);return NULL ; }expr_field_str (field->operands[1 ], expr); -return expr; +return_ptr (expr); }/*
Signature · 1 line
2.53.0
PATCH 7/16 tracing: Return ERR_PTR() from expr_str()
In reply to Steven RostedtFrom: Pengpeng Hou <[email protected]>
expr_str() currently reports all failure cases as NULL, so callers cannot distinguish invalid recursion depth from allocation failure or later string construction errors.
Return ERR_PTR()-encoded errors from expr_str() and make parse_unary() and parse_expr() propagate them. Clear expr->name before destroying the hist field so the error pointer is not freed as a string.
--- kernel/trace/trace_events_hist.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
kernel/trace/trace_events_hist.c +20 -5
@@ -1762,11 +1762,11 @@ static char *expr_str(struct hist_field *field, unsigned int level) char *expr __free(kfree) = NULL;if (level >1 ) -return NULL ; +return ERR_PTR (-EINVAL ); expr =kzalloc (MAX_FILTER_STR_VAL ,GFP_KERNEL );if (!expr) -return NULL ; +return ERR_PTR (-ENOMEM );if (!field->operands[0 ]) {expr_field_str (field, expr);
@@ -1778,8 +1778,8 @@ static char *expr_str(struct hist_field *field, unsigned int level)strcat (expr,"-(" ); subexpr =expr_str (field->operands[0 ], ++level); -if (!subexpr) -return NULL ; +if (IS_ERR (subexpr)) +return subexpr;strcat (expr, subexpr);strcat (expr,")" );
@@ -1805,7 +1805,7 @@ static char *expr_str(struct hist_field *field, unsigned int level)strcat (expr,"*" );break ;default : -return NULL ; +return ERR_PTR (-EINVAL ); }expr_field_str (field->operands[1 ], expr);
@@ -2622,6 +2622,11 @@ static struct hist_field *parse_unary(struct hist_trigger_data *hist_data, expr->is_signed = operand1->is_signed; expr->operator =FIELD_OP_UNARY_MINUS ; expr->name =expr_str (expr,0 ); +if (IS_ERR (expr->name)) { + ret =PTR_ERR (expr->name); + expr->name =NULL ; + goto free; + } expr->type =kstrdup_const (operand1->type,GFP_KERNEL );if (!expr->type) { ret = -ENOMEM ;
@@ -2834,6 +2839,11 @@ static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,destroy_hist_field (operand1,0 ); expr->name =expr_str (expr,0 ); +if (IS_ERR (expr->name)) { + ret =PTR_ERR (expr->name); + expr->name =NULL ; + goto free_expr; + } }else {/* The operand sizes should be the same, so just pick one */ expr->size = operand1->size;
@@ -2847,6 +2857,11 @@ static struct hist_field *parse_expr(struct hist_trigger_data *hist_data, } expr->name =expr_str (expr,0 ); +if (IS_ERR (expr->name)) { + ret =PTR_ERR (expr->name); + expr->name =NULL ; + goto free_expr; + } }return expr;
Signature · 1 line
2.53.0
PATCH 8/16 tracing: Bound histogram expression strings with seq_buf
In reply to Steven RostedtFrom: Pengpeng Hou <[email protected]>
expr_str() allocates a fixed MAX_FILTER_STR_VAL buffer and then builds expression names with a series of raw strcat() appends. Nested operands, constants, field flags, and generated field names can push the rendered string past that fixed limit before the name is attached to the hist field.
Build expression strings with seq_buf and return -E2BIG when the rendered name would exceed MAX_FILTER_STR_VAL. This keeps the existing tracing-side limit while replacing the raw append logic with bounded construction.
--- kernel/trace/trace_events_hist.c | 57 ++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 24 deletions(-)
kernel/trace/trace_events_hist.c +33 -24
@@ -94,7 +94,6 @@ typedef u64 (*hist_field_fn_t) (struct hist_field *field,#define HIST_FIELD_OPERANDS_MAX 2#define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)#define HIST_ACTIONS_MAX 8 -#define HIST_CONST_DIGITS_MAX 21#define HIST_DIV_SHIFT 20/* For optimizing division by constants */ enum field_op_id {
@@ -1733,33 +1732,36 @@ static const char *get_hist_field_flags(struct hist_field *hist_field)return flags_str; } -static void expr_field_str (struct hist_field *field,char *expr) +static bool expr_field_str (struct hist_field *field,struct seq_buf *s) { +const char *field_name; +if (field->flags &HIST_FIELD_FL_VAR_REF ) {if (!field->system) -strcat (expr,"$" ); - }else if (field->flags &HIST_FIELD_FL_CONST ) { -char str[HIST_CONST_DIGITS_MAX ]; +seq_buf_putc (s,'$' ); + }else if (field->flags &HIST_FIELD_FL_CONST ) +seq_buf_printf (s,"%llu" , field->constant); -snprintf (str,HIST_CONST_DIGITS_MAX ,"%llu" , field->constant); -strcat (expr, str); - } + field_name =hist_field_name (field,0 ); +if (!field_name) +return false; -strcat (expr,hist_field_name (field,0 )); +seq_buf_puts (s, field_name);if (field->flags && !(field->flags &HIST_FIELD_FL_VAR_REF )) {const char *flags_str =get_hist_field_flags (field); -if (flags_str) { -strcat (expr,"." ); -strcat (expr, flags_str); - } +if (flags_str) +seq_buf_printf (s,".%s" , flags_str); } + +return !seq_buf_has_overflowed (s); }static char *expr_str (struct hist_field *field,unsigned int level) { char *expr __free(kfree) = NULL; +struct seq_buf s;if (level >1 )return ERR_PTR (-EINVAL );
@@ -1768,47 +1770,54 @@ static char *expr_str(struct hist_field *field, unsigned int level)if (!expr)return ERR_PTR (-ENOMEM ); +seq_buf_init (&s, expr,MAX_FILTER_STR_VAL ); +if (!field->operands[0 ]) { -expr_field_str (field, expr); +if (!expr_field_str (field, &s)) +return ERR_PTR (-E2BIG ); +return_ptr (expr); }if (field->operator ==FIELD_OP_UNARY_MINUS ) {char *subexpr; -strcat (expr,"-(" ); subexpr =expr_str (field->operands[0 ], ++level);if (IS_ERR (subexpr))return subexpr; -strcat (expr, subexpr); -strcat (expr,")" ); - +seq_buf_printf (&s,"-(%s)" , subexpr);kfree (subexpr); +if (seq_buf_has_overflowed (&s)) +return ERR_PTR (-E2BIG ); +return_ptr (expr); } -expr_field_str (field->operands[0 ], expr); +if (!expr_field_str (field->operands[0 ], &s)) +return ERR_PTR (-E2BIG );switch (field->operator) {case FIELD_OP_MINUS : -strcat (expr,"-" ); +seq_buf_putc (&s,'-' );break ;case FIELD_OP_PLUS : -strcat (expr,"+" ); +seq_buf_putc (&s,'+' );break ;case FIELD_OP_DIV : -strcat (expr,"/" ); +seq_buf_putc (&s,'/' );break ;case FIELD_OP_MULT : -strcat (expr,"*" ); +seq_buf_putc (&s,'*' );break ;default :return ERR_PTR (-EINVAL ); } -expr_field_str (field->operands[1 ], expr); +if (seq_buf_has_overflowed (&s) || + !expr_field_str (field->operands[1 ], &s)) +return ERR_PTR (-E2BIG );return_ptr (expr); }
Signature · 1 line
2.53.0
PATCH 9/16 tracing/user_events: Use seq_putc() in two functions
In reply to Steven RostedtFrom: Markus Elfring <[email protected]>
Single characters should be put into a sequence. Thus use the corresponding function “seq_putc” for selected calls.
The source code was transformed by using the Coccinelle software.
--- kernel/trace/trace_events_user.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/trace/trace_events_user.c +4 -4
@@ -1838,7 +1838,7 @@ static int user_event_show(struct seq_file *m, struct dyn_event *ev)list_for_each_entry_reverse (field, head, link) {if (depth ==0 ) -seq_puts (m," " ); +seq_putc (m,' ' );else seq_puts (m,"; " );
@@ -1850,7 +1850,7 @@ static int user_event_show(struct seq_file *m, struct dyn_event *ev) depth++; } -seq_puts (m,"\n" ); +seq_putc (m,'\n' );return 0 ; }
@@ -2819,13 +2819,13 @@ static int user_seq_show(struct seq_file *m, void *p) busy++; } -seq_puts (m,"\n" ); +seq_putc (m,'\n' ); active++; }mutex_unlock (&group->reg_mutex); -seq_puts (m,"\n" ); +seq_putc (m,'\n' );seq_printf (m,"Active: %d\n" , active);seq_printf (m,"Busy: %d\n" , busy);
Signature · 1 line
2.53.0
PATCH 10/16 tracing/user_events: Replace a seq_printf() call by seq_puts() in user_seq_show()
In reply to Steven RostedtFrom: Markus Elfring <[email protected]>
A single string should be put into a sequence within a loop. Thus use the corresponding function “seq_puts” for one selected call.
The source code was transformed by using the Coccinelle software.
--- kernel/trace/trace_events_user.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace_events_user.c +1 -1
@@ -2806,7 +2806,7 @@ static int user_seq_show(struct seq_file *m, void *p)hash_for_each (group->register_table, i, user, node) { status = user->status; -seq_printf (m,"%s" ,EVENT_TP_NAME (user)); +seq_puts (m,EVENT_TP_NAME (user));if (status !=0 ) {seq_puts (m," # Used by" );
Signature · 1 line
2.53.0
PATCH 12/16 tracing: Reject invalid preemptirq_delay_test CPU affinity
In reply to Steven RostedtFrom: Samuel Moelius <[email protected]>
preemptirq_delay_test accepts cpu_affinity as a module parameter and, when it is non-negative, writes that CPU directly into a temporary cpumask from the worker thread. Values outside nr_cpu_ids can set a bit outside the allocated cpumask before the test reports a normal affinity error.
Validate the requested CPU in preemptirq_delay_run() before setting it in the temporary cpumask. Invalid affinity requests are reported by the test thread and skipped before cpumask_set_cpu() can touch an out-of-range bit.
--- kernel/trace/preemptirq_delay_test.c | 9 +++++++++ 1 file changed, 9 insertions(+)
kernel/trace/preemptirq_delay_test.c +9 -0
@@ -6,6 +6,7 @@*/ #include <linux/trace_clock.h> +#include <linux/cpumask.h> #include <linux/delay.h> #include <linux/interrupt.h> #include <linux/irq.h>
@@ -123,6 +124,13 @@ static int preemptirq_delay_run(void *data)return -ENOMEM ;if (cpu_affinity >-1 ) { +unsigned int cpu = cpu_affinity; + +if (cpu >= nr_cpu_ids || !cpu_possible (cpu)) { +pr_err ("cpu_affinity:%d, invalid CPU\n" , cpu_affinity); + goto out; + } +cpumask_clear (cpu_mask);cpumask_set_cpu (cpu_affinity, cpu_mask);if (set_cpus_allowed_ptr (current, cpu_mask))
@@ -132,6 +140,7 @@ static int preemptirq_delay_run(void *data)for (i =0 ; i < s; i++) (testfuncs[i])(i); +out:complete (&done);set_current_state (TASK_INTERRUPTIBLE );
Signature · 1 line
2.53.0
PATCH 11/16 fgraph: Use trace_seq_putc() in print_graph_return()
In reply to Steven RostedtFrom: Markus Elfring <[email protected]>
A single closing curly bracket should be put into a trace sequence buffer. Thus use the corresponding function “trace_seq_putc”.
The source code was transformed by using the Coccinelle software.
--- kernel/trace/trace_functions_graph.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace_functions_graph.c +1 -1
@@ -1349,7 +1349,7 @@ print_graph_return(struct ftrace_graph_ret_entry *retentry, struct trace_seq *s,* that if the funcgraph-tail option is enabled. */ if (func_match && !(flags &TRACE_GRAPH_PRINT_TAIL )) -trace_seq_puts (s,"}" ); +trace_seq_putc (s,'}' );else trace_seq_printf (s,"} /* %ps */" , (void *)func); }
Signature · 1 line
2.53.0
PATCH 13/16 samples/ftrace: Prevent division by zero when nr_function_calls is zero
In reply to Steven RostedtFrom: Samuel Moelius <[email protected]>
The ftrace-ops sample exposes nr_function_calls as a module parameter and uses it as the divisor when printing the measured time per call. Loading the module with nr_function_calls=0 skips the benchmark loop and then divides the elapsed time by zero, crashing the kernel during sample module initialization.
Keep accepting the parameter value, but report -1LL as the per-call duration when the call count is zero instead of dividing by it.
--- samples/ftrace/ftrace-ops.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
samples/ftrace/ftrace-ops.c +1 -1
@@ -223,7 +223,7 @@ static int __init ftrace_ops_sample_init(void)pr_info ("Attempted %u calls to %ps in %lluns (%lluns / call)\n" , nr_function_calls, tracee_relevant, - period,div_u64 (period, nr_function_calls)); + period, nr_function_calls ?div_u64 (period, nr_function_calls) :-1LL );if (persist)return 0 ;
Signature · 1 line
2.53.0
PATCH 14/16 tracing: Warn when an event dereferences a pointer in TP_printk()
In reply to Steven RostedtFrom: Steven Rostedt <[email protected]>
Currently on boot up and when modules are loaded, the trace event infrastructure will examine the TP_printk's of every event looking to see if it dereferences pointers on the ring buffer via printk formats like "%pB" and such. What it doesn't do is check if the arguments themselves do a dereference from a pointer.
This was brought with a fix[1] to the fsl_edma event that had in the arguments of the TP_printk(): "__entry->edma->membase"
The __entry->edma is a pointer saved in the ring buffer. The dereference from TP_printk() happens when the user reads the "trace" file which can be seconds, minutes, hours, days, weeks, or even months later! There is no guarantee that the __entry->edma pointer will still be pointing to what it was when it was recorded, and could crash the kernel when a user reads the event.
Add logic to the test_event_printk() that also checks for this case and warn if the event dereferences a pointer from the ring buffer.
[1] https://lore.kernel.org/all/[email protected]/
--- kernel/trace/trace_events.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-)
kernel/trace/trace_events.c +29 -6
@@ -401,6 +401,31 @@ static bool process_string(const char *fmt, int len, struct trace_event_call *careturn true; } +static void test_double_dereference (const char *str,int len, +struct trace_event_call *call) +{ +const char *ptr; +const char *end = str + len; + + ptr =strstr (str,"REC->" ); + +while (ptr && ptr < end) { + + ptr +=5 ; +for (; ptr < end; ptr++) { +if (ptr[0 ] =='-' && ptr[1 ] =='>' ) { +WARN_ONCE (1 ,"Event %s has double dereference in TP_printk: %.*s\n" , +trace_event_name (call), len, str); +return ; + } +if (!isalnum (*ptr) && *ptr !='_' ) +break ; + } + + ptr =strstr (ptr,"REC->" ); + } +} +static void handle_dereference_arg (const char *arg_str,u64 string_flags,int len,u64 *dereference_flags,int arg,struct trace_event_call *call)
@@ -460,12 +485,6 @@ static void test_event_printk(struct trace_event_call *call)if (in_quote) { arg =0 ; first = false; -/* -* If there was no %p* uses -* the fmt is OK. -*/ -if (!dereference_flags) -return ; } }if (in_quote) {
@@ -577,6 +596,8 @@ static void test_event_printk(struct trace_event_call *call)continue ; } +test_double_dereference (fmt + start_arg, e - start_arg, call); +if (dereference_flags & (1ULL << arg)) { handle_dereference_arg(fmt + start_arg, string_flags, e - start_arg,
@@ -590,6 +611,8 @@ static void test_event_printk(struct trace_event_call *call) } } +test_double_dereference (fmt + start_arg, i - start_arg, call); +if (dereference_flags & (1ULL << arg)) { handle_dereference_arg(fmt + start_arg, string_flags, i - start_arg,
Signature · 1 line
2.53.0
PATCH 15/16 tracing: Use strscpy() instead of strcpy() in trace_sched_switch
In reply to Steven RostedtFrom: Po-Sheng Lin <[email protected]>
Replace strcpy() with strscpy() in __trace_find_cmdline() for consistency with the existing strscpy() call in the same function, and to avoid potential buffer overflow as flagged by the Kernel Self Protection Project.
--- kernel/trace/trace_sched_switch.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/trace/trace_sched_switch.c +3 -3
@@ -286,12 +286,12 @@ static void __trace_find_cmdline(int pid, char comm[])int tpid;if (!pid) { -strcpy (comm,"<idle>" ); +strscpy (comm,"<idle>" ,TASK_COMM_LEN );return ; }if (WARN_ON_ONCE (pid <0 )) { -strcpy (comm,"<XXX>" ); +strscpy (comm,"<XXX>" ,TASK_COMM_LEN );return ; }
@@ -304,7 +304,7 @@ static void __trace_find_cmdline(int pid, char comm[])return ; } } -strcpy (comm,"<...>" ); +strscpy (comm,"<...>" ,TASK_COMM_LEN ); }void trace_find_cmdline (int pid,char comm[])
Signature · 1 line
2.53.0
PATCH 16/16 tracing: Use seq_buf for string concatenation
In reply to Steven RostedtFrom: Woradorn Laodhanadhaworn <[email protected]>
In preparation for removing the strlcat API[1],
replace the string concatenation logic with a struct seq_buf, which tracks the current position and the remaining space internally.
Use seq_buf_str() to NUL-terminate before passing to early_enable_events().
--- kernel/trace/trace_events.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
kernel/trace/trace_events.c +13 -3
@@ -14,6 +14,7 @@#include <linux/workqueue.h> #include <linux/security.h> #include <linux/spinlock.h> +#include <linux/seq_buf.h> #include <linux/kthread.h> #include <linux/tracefs.h> #include <linux/uaccess.h>
@@ -4604,13 +4605,20 @@ extern struct trace_event_call *__start_ftrace_events[];extern struct trace_event_call *__stop_ftrace_events[];static char bootup_event_buf[COMMAND_LINE_SIZE ]__initdata ; +static struct seq_buf bootup_event_seq__initdata = { + .buffer = bootup_event_buf, + .size =sizeof (bootup_event_buf), +};static __init int setup_trace_event (char *str) { -if (bootup_event_buf[0 ] !='\0' ) -strlcat (bootup_event_buf,"," ,COMMAND_LINE_SIZE ); +if (seq_buf_used (&bootup_event_seq) >0 ) +seq_buf_puts (&bootup_event_seq,"," ); + +seq_buf_puts (&bootup_event_seq, str); -strlcat (bootup_event_buf, str,COMMAND_LINE_SIZE ); +if (seq_buf_has_overflowed (&bootup_event_seq)) +return -ENOMEM ;trace_set_ring_buffer_expanded (NULL );disable_tracing_selftest ("running event tracing" );
@@ -4869,6 +4877,7 @@ static __init int event_trace_enable(void)*/ __trace_early_add_events (tr); +seq_buf_str (&bootup_event_seq);early_enable_events (tr, bootup_event_buf, false);trace_printk_start_comm ();
@@ -4897,6 +4906,7 @@ static __init int event_trace_enable_again(void)if (!tr)return -ENODEV ; +seq_buf_str (&bootup_event_seq);early_enable_events (tr, bootup_event_buf, true);return 0 ;
Signature · 1 line
2.53.0