tracing: Updates for 6.4
Subsystems (15)
Steven Rostedt
maintainercover tracing: Updates for 6.4
git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git
trace/for-next
Head SHA1: 88fe1ec75fcb296579e05eaf3807da3ee83137e4
Beau Belgrave (12):
tracing/user_events: Split header into uapi and kernel tracing/user_events: Track fork/exec/exit for mm lifetime tracing/user_events: Use remote writes for event enablement tracing/user_events: Fixup enable faults asyncly tracing/user_events: Add ioctl for disabling addresses tracing/user_events: Update self-tests to write ABI tracing/user_events: Add ABI self-test tracing/user_events: Use write ABI in example tracing/user_events: Update documentation for ABI tracing/user_events: Charge event allocs to cgroups tracing/user_events: Limit global user_event count tracing/user_events: Align structs with tabs for readability
Masami Hiramatsu (Google) (7):
fprobe: Pass entry_data to handlers lib/test_fprobe: Add private entry_data testcases
fprobe: Add nr_maxactive to specify rethook_node pool size lib/test_fprobe: Add a test case for nr_maxactive
fprobe: Skip exit_handler if entry_handler returns !0 lib/test_fprobe: Add a testcase for skipping exit_handler
docs: tracing: Update fprobe documentation
Ross Zwisler (3):
selftests: use canonical ftrace path
leaking_addresses: also skip canonical ftrace path tools/kvm_stat: use canonical ftrace path
Steven Rostedt (Google) (3):
tracing: Add "fields" option to show raw trace event fields tracing/user_events: Use print_format_fields() for trace output
tracing: Unbreak user events
----
Documentation/trace/fprobe.rst | 16 +- Documentation/trace/ftrace.rst | 6 + Documentation/trace/user_events.rst | 167 ++-- fs/exec.c | 2 + include/linux/fprobe.h | 10 +- include/linux/sched.h | 5 + include/linux/user_events.h | 101 ++- include/uapi/linux/user_events.h | 81 ++ kernel/exit.c | 2 + kernel/fork.c | 2 + kernel/trace/Kconfig | 6 +- kernel/trace/bpf_trace.c | 17 +- kernel/trace/fprobe.c | 32 +- kernel/trace/trace.c | 7 +- kernel/trace/trace.h | 2 + kernel/trace/trace_events_user.c | 932 +++++++++++++++++----- kernel/trace/trace_output.c | 168 ++++ kernel/trace/trace_output.h | 2 + lib/test_fprobe.c | 105 ++- samples/fprobe/fprobe_example.c | 7 +- samples/user_events/example.c | 45 +- scripts/leaking_addresses.pl | 1 + tools/kvm/kvm_stat/kvm_stat | 2 +- tools/testing/selftests/mm/protection_keys.c | 4 +- tools/testing/selftests/user_events/Makefile | 2 +- tools/testing/selftests/user_events/abi_test.c | 226 ++++++ tools/testing/selftests/user_events/dyn_test.c | 2 +- tools/testing/selftests/user_events/ftrace_test.c | 162 ++-- tools/testing/selftests/user_events/perf_test.c | 39 +- 29 files changed, 1692 insertions(+), 461 deletions(-) create mode 100644 include/uapi/linux/user_events.h create mode 100644 tools/testing/selftests/user_events/abi_test.c
Steven Rostedt
maintainerPATCH 1/25 fprobe: Pass entry_data to handlers
From: "Masami Hiramatsu (Google)" <[email protected]>
Pass the private entry_data to the entry and exit handlers so that they can share the context data, something like saved function arguments etc.
User must specify the private entry_data size by @entry_data_size field before registering the fprobe.
--- include/linux/fprobe.h | 8 ++++++-- kernel/trace/bpf_trace.c | 2 +- kernel/trace/fprobe.c | 21 ++++++++++++++------- lib/test_fprobe.c | 6 ++++-- samples/fprobe/fprobe_example.c | 6 ++++-- 5 files changed, 29 insertions(+), 14 deletions(-)
include/linux/fprobe.h +6 -2
@@ -13,6 +13,7 @@* @nmissed: The counter for missing events. * @flags: The status flag. * @rethook: The rethook data structure. (internal data) +* @entry_data_size: The private data storage size. * @entry_handler: The callback function for function entry. * @exit_handler: The callback function for function exit. */
@@ -29,9 +30,12 @@ struct fprobe {unsigned long nmissed;unsigned int flags;struct rethook *rethook; +size_t entry_data_size; -void (*entry_handler)(struct fprobe *fp,unsigned long entry_ip,struct pt_regs *regs); -void (*exit_handler)(struct fprobe *fp,unsigned long entry_ip,struct pt_regs *regs); +void (*entry_handler)(struct fprobe *fp,unsigned long entry_ip, +struct pt_regs *regs,void *entry_data); +void (*exit_handler)(struct fprobe *fp,unsigned long entry_ip, +struct pt_regs *regs,void *entry_data); };/* This fprobe is soft-disabled. */
kernel/trace/bpf_trace.c +1 -1
@@ -2646,7 +2646,7 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,static void kprobe_multi_link_handler (struct fprobe *fp,unsigned long fentry_ip, -struct pt_regs *regs) +struct pt_regs *regs,void *data) {struct bpf_kprobe_multi_link *link;
kernel/trace/fprobe.c +14 -7
@@ -17,14 +17,16 @@struct fprobe_rethook_node {struct rethook_node node;unsigned long entry_ip; +char data[]; };static void fprobe_handler (unsigned long ip,unsigned long parent_ip,struct ftrace_ops *ops,struct ftrace_regs *fregs) {struct fprobe_rethook_node *fpr; -struct rethook_node *rh; +struct rethook_node *rh =NULL ;struct fprobe *fp; +void *entry_data =NULL ;int bit; fp = container_of(ops, struct fprobe, ops);
@@ -37,9 +39,6 @@ static void fprobe_handler(unsigned long ip, unsigned long parent_ip,return ; } -if (fp->entry_handler) - fp->entry_handler (fp, ip,ftrace_get_regs (fregs)); -if (fp->exit_handler) { rh =rethook_try_get (fp->rethook);if (!rh) {
@@ -48,9 +47,16 @@ static void fprobe_handler(unsigned long ip, unsigned long parent_ip, } fpr = container_of(rh, struct fprobe_rethook_node, node); fpr->entry_ip = ip; -rethook_hook (rh,ftrace_get_regs (fregs), true); +if (fp->entry_data_size) + entry_data = fpr->data; } +if (fp->entry_handler) + fp->entry_handler (fp, ip,ftrace_get_regs (fregs), entry_data); + +if (rh) +rethook_hook (rh,ftrace_get_regs (fregs), true); + out:ftrace_test_recursion_unlock (bit); }
@@ -81,7 +87,8 @@ static void fprobe_exit_handler(struct rethook_node *rh, void *data, fpr = container_of(rh, struct fprobe_rethook_node, node); - fp->exit_handler (fp, fpr->entry_ip, regs); + fp->exit_handler (fp, fpr->entry_ip, regs, + fp->entry_data_size ? (void *)fpr->data :NULL ); }NOKPROBE_SYMBOL (fprobe_exit_handler);
@@ -146,7 +153,7 @@ static int fprobe_init_rethook(struct fprobe *fp, int num)for (i =0 ; i < size; i++) {struct fprobe_rethook_node *node; - node =kzalloc (sizeof (*node),GFP_KERNEL ); + node =kzalloc (sizeof (*node) + fp->entry_data_size,GFP_KERNEL );if (!node) {rethook_free (fp->rethook); fp->rethook =NULL ;
lib/test_fprobe.c +4 -2
@@ -30,7 +30,8 @@ static noinline u32 fprobe_selftest_target2(u32 value)return (value / div_factor) +1 ; } -static notrace void fp_entry_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs) +static notrace void fp_entry_handler(struct fprobe *fp, unsigned long ip, +struct pt_regs *regs,void *data) {KUNIT_EXPECT_FALSE (current_test,preemptible ());/* This can be called on the fprobe_selftest_target and the fprobe_selftest_target2 */
@@ -39,7 +40,8 @@ static notrace void fp_entry_handler(struct fprobe *fp, unsigned long ip, struct entry_val = (rand1 / div_factor); } -static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs) +static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip, +struct pt_regs *regs,void *data) {unsigned long ret =regs_return_value (regs);
samples/fprobe/fprobe_example.c +4 -2
@@ -48,7 +48,8 @@ static void show_backtrace(void)stack_trace_print (stacks, len,24 ); } -static void sample_entry_handler (struct fprobe *fp,unsigned long ip,struct pt_regs *regs) +static void sample_entry_handler (struct fprobe *fp,unsigned long ip, +struct pt_regs *regs,void *data) {if (use_trace)/*
@@ -63,7 +64,8 @@ static void sample_entry_handler(struct fprobe *fp, unsigned long ip, struct pt_show_backtrace (); } -static void sample_exit_handler (struct fprobe *fp,unsigned long ip,struct pt_regs *regs) +static void sample_exit_handler (struct fprobe *fp,unsigned long ip,struct pt_regs *regs, +void *data) {unsigned long rip =instruction_pointer (regs);
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 2/25 lib/test_fprobe: Add private entry_data testcases
In reply to Steven RostedtFrom: "Masami Hiramatsu (Google)" <[email protected]>
Add test cases for checking whether private entry_data is correctly passed or not.
--- lib/test_fprobe.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
lib/test_fprobe.c +30 -0
@@ -38,6 +38,12 @@ static notrace void fp_entry_handler(struct fprobe *fp, unsigned long ip,if (ip != target_ip)KUNIT_EXPECT_EQ (current_test, ip, target2_ip); entry_val = (rand1 / div_factor); +if (fp->entry_data_size) { +KUNIT_EXPECT_NOT_NULL (current_test, data); +if (data) + *(u32 *)data = entry_val; + }else +KUNIT_EXPECT_NULL (current_test, data); } static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip,
@@ -53,6 +59,12 @@ static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip,KUNIT_EXPECT_EQ (current_test, ret, (rand1 / div_factor));KUNIT_EXPECT_EQ (current_test, entry_val, (rand1 / div_factor)); exit_val = entry_val + div_factor; +if (fp->entry_data_size) { +KUNIT_EXPECT_NOT_NULL (current_test, data); +if (data) +KUNIT_EXPECT_EQ (current_test, *(u32 *)data, entry_val); + }else +KUNIT_EXPECT_NULL (current_test, data); }/* Test entry only (no rethook) */
@@ -134,6 +146,23 @@ static void test_fprobe_syms(struct kunit *test)KUNIT_EXPECT_EQ (test,0 ,unregister_fprobe (&fp)); } +/* Test private entry_data */ +static void test_fprobe_data (struct kunit *test) +{ +struct fprobe fp = { + .entry_handler = fp_entry_handler, + .exit_handler = fp_exit_handler, + .entry_data_size =sizeof (u32), + }; + + current_test = test; +KUNIT_EXPECT_EQ (test,0 ,register_fprobe (&fp,"fprobe_selftest_target" ,NULL )); + +target (rand1); + +KUNIT_EXPECT_EQ (test,0 ,unregister_fprobe (&fp)); +} +static unsigned long get_ftrace_location (void *func) {unsigned long size, addr = (unsigned long )func;
@@ -159,6 +188,7 @@ static struct kunit_case fprobe_testcases[] = {KUNIT_CASE (test_fprobe_entry),KUNIT_CASE (test_fprobe),KUNIT_CASE (test_fprobe_syms), + KUNIT_CASE(test_fprobe_data), {} };
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 3/25 fprobe: Add nr_maxactive to specify rethook_node pool size
In reply to Steven RostedtFrom: "Masami Hiramatsu (Google)" <[email protected]>
Add nr_maxactive to specify rethook_node pool size. This means the maximum number of actively running target functions concurrently for probing by exit_handler. Note that if the running function is preempted or sleep, it is still counted as 'active'.
--- include/linux/fprobe.h | 2 ++ kernel/trace/fprobe.c | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-)
include/linux/fprobe.h +2 -0
@@ -14,6 +14,7 @@* @flags: The status flag. * @rethook: The rethook data structure. (internal data) * @entry_data_size: The private data storage size. +* @nr_maxactive: The max number of active functions. * @entry_handler: The callback function for function entry. * @exit_handler: The callback function for function exit. */
@@ -31,6 +32,7 @@ struct fprobe {unsigned int flags;struct rethook *rethook;size_t entry_data_size; +int nr_maxactive;void (*entry_handler)(struct fprobe *fp,unsigned long entry_ip,struct pt_regs *regs,void *entry_data);
kernel/trace/fprobe.c +4 -1
@@ -143,7 +143,10 @@ static int fprobe_init_rethook(struct fprobe *fp, int num) }/* Initialize rethook if needed */ - size = num *num_possible_cpus () *2 ; +if (fp->nr_maxactive) + size = fp->nr_maxactive; +else + size = num *num_possible_cpus () *2 ;if (size <0 )return -E2BIG ;
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 4/25 lib/test_fprobe: Add a test case for nr_maxactive
In reply to Steven RostedtFrom: "Masami Hiramatsu (Google)" <[email protected]>
Add a test case for nr_maxactive. If the number of active functions is more than nr_maxactive, it must be skipped.
--- lib/test_fprobe.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)
lib/test_fprobe.c +42 -0
@@ -17,8 +17,10 @@ static u32 rand1, entry_val, exit_val;/* Use indirect calls to avoid inlining the target functions */ static u32 (*target)(u32 value);static u32 (*target2)(u32 value); +static u32 (*target_nest)(u32 value,u32 (*nest)(u32 ));static unsigned long target_ip;static unsigned long target2_ip; +static unsigned long target_nest_ip; static noinline u32 fprobe_selftest_target(u32 value) {
@@ -30,6 +32,11 @@ static noinline u32 fprobe_selftest_target2(u32 value)return (value / div_factor) +1 ; } +static noinline u32 fprobe_selftest_nest_target(u32 value, u32 (*nest)(u32)) +{ +return nest (value +2 ); +} + static notrace void fp_entry_handler(struct fprobe *fp, unsigned long ip,struct pt_regs *regs,void *data) {
@@ -67,6 +74,19 @@ static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip,KUNIT_EXPECT_NULL (current_test, data); } +static notrace void nest_entry_handler(struct fprobe *fp, unsigned long ip, +struct pt_regs *regs,void *data) +{ +KUNIT_EXPECT_FALSE (current_test,preemptible ()); +} + +static notrace void nest_exit_handler(struct fprobe *fp, unsigned long ip, +struct pt_regs *regs,void *data) +{ +KUNIT_EXPECT_FALSE (current_test,preemptible ()); +KUNIT_EXPECT_EQ (current_test, ip, target_nest_ip); +} +/* Test entry only (no rethook) */ static void test_fprobe_entry (struct kunit *test) {
@@ -163,6 +183,25 @@ static void test_fprobe_data(struct kunit *test)KUNIT_EXPECT_EQ (test,0 ,unregister_fprobe (&fp)); } +/* Test nr_maxactive */ +static void test_fprobe_nest (struct kunit *test) +{ +static const char *syms[] = {"fprobe_selftest_target" ,"fprobe_selftest_nest_target" }; +struct fprobe fp = { + .entry_handler = nest_entry_handler, + .exit_handler = nest_exit_handler, + .nr_maxactive =1 , + }; + + current_test = test; +KUNIT_EXPECT_EQ (test,0 ,register_fprobe_syms (&fp, syms,2 )); + +target_nest (rand1, target); +KUNIT_EXPECT_EQ (test,1 , fp.nmissed); + +KUNIT_EXPECT_EQ (test,0 ,unregister_fprobe (&fp)); +} +static unsigned long get_ftrace_location (void *func) {unsigned long size, addr = (unsigned long )func;
@@ -178,8 +217,10 @@ static int fprobe_test_init(struct kunit *test) rand1 =get_random_u32_above (div_factor); target = fprobe_selftest_target; target2 = fprobe_selftest_target2; + target_nest = fprobe_selftest_nest_target; target_ip =get_ftrace_location (target); target2_ip =get_ftrace_location (target2); + target_nest_ip =get_ftrace_location (target_nest);return 0 ; }
@@ -189,6 +230,7 @@ static struct kunit_case fprobe_testcases[] = {KUNIT_CASE (test_fprobe),KUNIT_CASE (test_fprobe_syms),KUNIT_CASE (test_fprobe_data), + KUNIT_CASE(test_fprobe_nest), {} };
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 5/25 fprobe: Skip exit_handler if entry_handler returns !0
In reply to Steven RostedtFrom: "Masami Hiramatsu (Google)" <[email protected]>
Skip hooking function return and calling exit_handler if the entry_handler() returns !0.
--- include/linux/fprobe.h | 4 ++-- kernel/trace/bpf_trace.c | 15 +++++++++++++-- kernel/trace/fprobe.c | 14 +++++++++----- lib/test_fprobe.c | 7 +++++-- samples/fprobe/fprobe_example.c | 5 +++-- 5 files changed, 32 insertions(+), 13 deletions(-)
include/linux/fprobe.h +2 -2
@@ -34,8 +34,8 @@ struct fprobe {size_t entry_data_size;int nr_maxactive; -void (*entry_handler)(struct fprobe *fp,unsigned long entry_ip, -struct pt_regs *regs,void *entry_data); +int (*entry_handler)(struct fprobe *fp,unsigned long entry_ip, +struct pt_regs *regs,void *entry_data);void (*exit_handler)(struct fprobe *fp,unsigned long entry_ip,struct pt_regs *regs,void *entry_data); };
kernel/trace/bpf_trace.c +13 -2
@@ -2644,12 +2644,23 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,return err; } -static void +static int kprobe_multi_link_handler (struct fprobe *fp,unsigned long fentry_ip,struct pt_regs *regs,void *data) {struct bpf_kprobe_multi_link *link; + link = container_of(fp, struct bpf_kprobe_multi_link, fp); +kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs); +return 0 ; +} + +static void +kprobe_multi_link_exit_handler (struct fprobe *fp,unsigned long fentry_ip, +struct pt_regs *regs,void *data) +{ +struct bpf_kprobe_multi_link *link; + link = container_of(fp, struct bpf_kprobe_multi_link, fp);kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs); }
@@ -2848,7 +2859,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr goto error;if (flags &BPF_F_KPROBE_MULTI_RETURN ) - link->fp.exit_handler = kprobe_multi_link_handler; + link->fp.exit_handler = kprobe_multi_link_exit_handler;else link->fp.entry_handler = kprobe_multi_link_handler;
kernel/trace/fprobe.c +9 -5
@@ -27,7 +27,7 @@ static void fprobe_handler(unsigned long ip, unsigned long parent_ip,struct rethook_node *rh =NULL ;struct fprobe *fp;void *entry_data =NULL ; -int bit; +int bit, ret; fp = container_of(ops, struct fprobe, ops);if (fprobe_disabled (fp))
@@ -52,11 +52,15 @@ static void fprobe_handler(unsigned long ip, unsigned long parent_ip, }if (fp->entry_handler) - fp->entry_handler (fp, ip,ftrace_get_regs (fregs), entry_data); - -if (rh) -rethook_hook (rh,ftrace_get_regs (fregs), true); + ret = fp->entry_handler (fp, ip,ftrace_get_regs (fregs), entry_data); +/* If entry_handler returns !0, nmissed is not counted. */ +if (rh) { +if (ret) +rethook_recycle (rh); +else +rethook_hook (rh,ftrace_get_regs (fregs), true); + } out:ftrace_test_recursion_unlock (bit); }
lib/test_fprobe.c +5 -2
@@ -37,7 +37,7 @@ static noinline u32 fprobe_selftest_nest_target(u32 value, u32 (*nest)(u32))return nest (value +2 ); } -static notrace void fp_entry_handler(struct fprobe *fp, unsigned long ip, +static notrace int fp_entry_handler(struct fprobe *fp, unsigned long ip,struct pt_regs *regs,void *data) {KUNIT_EXPECT_FALSE (current_test,preemptible ());
@@ -51,6 +51,8 @@ static notrace void fp_entry_handler(struct fprobe *fp, unsigned long ip, *(u32 *)data = entry_val; }else KUNIT_EXPECT_NULL (current_test, data); + +return 0 ; } static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip,
@@ -74,10 +76,11 @@ static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip,KUNIT_EXPECT_NULL (current_test, data); } -static notrace void nest_entry_handler(struct fprobe *fp, unsigned long ip, +static notrace int nest_entry_handler(struct fprobe *fp, unsigned long ip,struct pt_regs *regs,void *data) {KUNIT_EXPECT_FALSE (current_test,preemptible ()); +return 0 ; } static notrace void nest_exit_handler(struct fprobe *fp, unsigned long ip,
samples/fprobe/fprobe_example.c +3 -2
@@ -48,8 +48,8 @@ static void show_backtrace(void)stack_trace_print (stacks, len,24 ); } -static void sample_entry_handler (struct fprobe *fp,unsigned long ip, -struct pt_regs *regs,void *data) +static int sample_entry_handler (struct fprobe *fp,unsigned long ip, +struct pt_regs *regs,void *data) {if (use_trace)/*
@@ -62,6 +62,7 @@ static void sample_entry_handler(struct fprobe *fp, unsigned long ip, nhit++;if (stackdump)show_backtrace (); +return 0 ; } static void sample_exit_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs,
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 6/25 lib/test_fprobe: Add a testcase for skipping exit_handler
In reply to Steven RostedtFrom: "Masami Hiramatsu (Google)" <[email protected]>
Add a testcase for skipping exit_handler if entry_handler returns !0.
--- lib/test_fprobe.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)
lib/test_fprobe.c +25 -1
@@ -21,6 +21,7 @@ static u32 (*target_nest)(u32 value, u32 (*nest)(u32));static unsigned long target_ip;static unsigned long target2_ip;static unsigned long target_nest_ip; +static int entry_return_value; static noinline u32 fprobe_selftest_target(u32 value) {
@@ -52,7 +53,7 @@ static notrace int fp_entry_handler(struct fprobe *fp, unsigned long ip, }else KUNIT_EXPECT_NULL (current_test, data); -return 0 ; +return entry_return_value; } static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip,
@@ -205,6 +206,28 @@ static void test_fprobe_nest(struct kunit *test)KUNIT_EXPECT_EQ (test,0 ,unregister_fprobe (&fp)); } +static void test_fprobe_skip (struct kunit *test) +{ +struct fprobe fp = { + .entry_handler = fp_entry_handler, + .exit_handler = fp_exit_handler, + }; + + current_test = test; +KUNIT_EXPECT_EQ (test,0 ,register_fprobe (&fp,"fprobe_selftest_target" ,NULL )); + + entry_return_value =1 ; + entry_val =0 ; + exit_val =0 ; +target (rand1); +KUNIT_EXPECT_NE (test,0 , entry_val); +KUNIT_EXPECT_EQ (test,0 , exit_val); +KUNIT_EXPECT_EQ (test,0 , fp.nmissed); + entry_return_value =0 ; + +KUNIT_EXPECT_EQ (test,0 ,unregister_fprobe (&fp)); +} +static unsigned long get_ftrace_location (void *func) {unsigned long size, addr = (unsigned long )func;
@@ -234,6 +257,7 @@ static struct kunit_case fprobe_testcases[] = {KUNIT_CASE (test_fprobe_syms),KUNIT_CASE (test_fprobe_data),KUNIT_CASE (test_fprobe_nest), + KUNIT_CASE(test_fprobe_skip), {} };
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 7/25 docs: tracing: Update fprobe documentation
In reply to Steven RostedtFrom: "Masami Hiramatsu (Google)" <[email protected]>
Update fprobe.rst for
- the private entry_data argument
- the return value of the entry handler
- the nr_rethook_node field.
--- Documentation/trace/fprobe.rst | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
Documentation/trace/fprobe.rst +12 -4
@@ -87,14 +87,16 @@ returns as same as unregister_ftrace_function(). The fprobe entry/exit handler ============================= -The prototype of the entry/exit callback function is as follows: +The prototype of the entry/exit callback function are as follows: .. code-block:: c - void callback_func(struct fprobe *fp, unsigned long entry_ip, struct pt_regs *regs); + int entry_callback(struct fprobe *fp, unsigned long entry_ip, struct pt_regs *regs, void *entry_data); -Note that both entry and exit callbacks have same ptototype. The @entry_ip is -saved at function entry and passed to exit handler. + void exit_callback(struct fprobe *fp, unsigned long entry_ip, struct pt_regs *regs, void *entry_data); + +Note that the @entry_ip is saved at function entry and passed to exit handler. +If the entry callback function returns !0, the corresponding exit callback will be cancelled. @fp This is the address of `fprobe` data structure related to this handler.
@@ -113,6 +115,12 @@ saved at function entry and passed to exit handler. to use @entry_ip. On the other hand, in the exit_handler, the instruction pointer of @regs is set to the currect return address. +@entry_data + This is a local storage to share the data between entry and exit handlers. + This storage is NULL by default. If the user specify `exit_handler` field + and `entry_data_size` field when registering the fprobe, the storage is + allocated and passed to both `entry_handler` and `exit_handler`. + Share the callbacks with kprobes ================================
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 8/25 selftests: use canonical ftrace path
In reply to Steven RostedtFrom: Ross Zwisler <[email protected]>
The canonical location for the tracefs filesystem is at /sys/kernel/tracing.
But, from Documentation/trace/ftrace.rst:
Before 4.1, all ftrace tracing control files were within the debugfs file system, which is typically located at /sys/kernel/debug/tracing. For backward compatibility, when mounting the debugfs file system, the tracefs file system will be automatically mounted at:
/sys/kernel/debug/tracing
A few spots in tools/testing/selftests still refer to this older debugfs path, so let's update them to avoid confusion.
--- tools/testing/selftests/mm/protection_keys.c | 4 ++-- tools/testing/selftests/user_events/dyn_test.c | 2 +- tools/testing/selftests/user_events/ftrace_test.c | 10 +++++----- tools/testing/selftests/user_events/perf_test.c | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-)
tools/testing/selftests/mm/protection_keys.c +2 -2
@@ -98,7 +98,7 @@ int tracing_root_ok(void)void tracing_on (void ) {#if CONTROL_TRACING >0 -#define TRACEDIR "/sys/kernel/debug/tracing" +#define TRACEDIR "/sys/kernel/tracing"char pidstr[32 ];if (!tracing_root_ok ())
@@ -124,7 +124,7 @@ void tracing_off(void)#if CONTROL_TRACING >0 if (!tracing_root_ok ())return ; -cat_into_file ("0" ,"/sys/kernel/debug/tracing/tracing_on" ); +cat_into_file ("0" ,"/sys/kernel/tracing/tracing_on" );#endif }
tools/testing/selftests/user_events/dyn_test.c +1 -1
@@ -16,7 +16,7 @@#include "../kselftest_harness.h" -const char *dyn_file ="/sys/kernel/debug/tracing/dynamic_events" ; +const char *dyn_file ="/sys/kernel/tracing/dynamic_events" ;const char *clear ="!u:__test_event" ;static int Append (const char *value)
tools/testing/selftests/user_events/ftrace_test.c +5 -5
@@ -16,11 +16,11 @@#include "../kselftest_harness.h" -const char *data_file ="/sys/kernel/debug/tracing/user_events_data" ; -const char *status_file ="/sys/kernel/debug/tracing/user_events_status" ; -const char *enable_file ="/sys/kernel/debug/tracing/events/user_events/__test_event/enable" ; -const char *trace_file ="/sys/kernel/debug/tracing/trace" ; -const char *fmt_file ="/sys/kernel/debug/tracing/events/user_events/__test_event/format" ; +const char *data_file ="/sys/kernel/tracing/user_events_data" ; +const char *status_file ="/sys/kernel/tracing/user_events_status" ; +const char *enable_file ="/sys/kernel/tracing/events/user_events/__test_event/enable" ; +const char *trace_file ="/sys/kernel/tracing/trace" ; +const char *fmt_file ="/sys/kernel/tracing/events/user_events/__test_event/format" ;static inline int status_check (char *status_page,int status_bit) {
tools/testing/selftests/user_events/perf_test.c +4 -4
@@ -18,10 +18,10 @@#include "../kselftest_harness.h" -const char *data_file ="/sys/kernel/debug/tracing/user_events_data" ; -const char *status_file ="/sys/kernel/debug/tracing/user_events_status" ; -const char *id_file ="/sys/kernel/debug/tracing/events/user_events/__test_event/id" ; -const char *fmt_file ="/sys/kernel/debug/tracing/events/user_events/__test_event/format" ; +const char *data_file ="/sys/kernel/tracing/user_events_data" ; +const char *status_file ="/sys/kernel/tracing/user_events_status" ; +const char *id_file ="/sys/kernel/tracing/events/user_events/__test_event/id" ; +const char *fmt_file ="/sys/kernel/tracing/events/user_events/__test_event/format" ;struct event {__u32 index;
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 9/25 leaking_addresses: also skip canonical ftrace path
In reply to Steven RostedtFrom: Ross Zwisler <[email protected]>
The canonical location for the tracefs filesystem is at /sys/kernel/tracing.
But, from Documentation/trace/ftrace.rst:
Before 4.1, all ftrace tracing control files were within the debugfs file system, which is typically located at /sys/kernel/debug/tracing. For backward compatibility, when mounting the debugfs file system, the tracefs file system will be automatically mounted at:
/sys/kernel/debug/tracing
scripts/leaking_addresses.pl only skipped this older debugfs path, so let's add the canonical path as well.
--- scripts/leaking_addresses.pl | 1 + 1 file changed, 1 insertion(+)
scripts/leaking_addresses.pl +1 -0
@@ -61,6 +61,7 @@ my @skip_abs = ( '/proc/device-tree', '/proc/1/syscall', '/sys/firmware/devicetree', + '/sys/kernel/tracing/trace_pipe', '/sys/kernel/debug/tracing/trace_pipe', '/sys/kernel/security/apparmor/revision');
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 10/25 tools/kvm_stat: use canonical ftrace path
In reply to Steven RostedtFrom: Ross Zwisler <[email protected]>
The canonical location for the tracefs filesystem is at /sys/kernel/tracing.
But, from Documentation/trace/ftrace.rst:
Before 4.1, all ftrace tracing control files were within the debugfs file system, which is typically located at /sys/kernel/debug/tracing. For backward compatibility, when mounting the debugfs file system, the tracefs file system will be automatically mounted at:
/sys/kernel/debug/tracing
A comment in kvm_stat still refers to this older debugfs path, so let's update it to avoid confusion.
--- tools/kvm/kvm_stat/kvm_stat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/kvm/kvm_stat/kvm_stat +1 -1
@@ -627,7 +627,7 @@ class TracepointProvider(Provider): name)'. All available events have directories under - /sys/kernel/debug/tracing/events/ which export information + /sys/kernel/tracing/events/ which export information about the specific event. Therefore, listing the dirs gives us a list of all available events.
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 11/25 tracing: Add "fields" option to show raw trace event fields
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
The hex, raw and bin formats come from the old PREEMPT_RT patch set latency tracer. That actually gave real alternatives to reading the ascii buffer. But they have started to bit rot and they do not give a good representation of the tracing data.
Add "fields" option that will read the trace event fields and parse the data from how the fields are defined:
With "fields" = 0 (default)
echo 1 > events/sched/sched_switch/enable
cat trace
<idle>-0 [003] d..2. 540.078653: sched_switch: prev_comm=swapper/3 prev_pid=0 prev_prio=120 prev_state=R ==> next_comm=kworker/3:1 next_pid=83 next_prio=120
kworker/3:1-83 [003] d..2. 540.078860: sched_switch: prev_comm=kworker/3:1 prev_pid=83 prev_prio=120 prev_state=I ==> next_comm=swapper/3 next_pid=0 next_prio=120
<idle>-0 [003] d..2. 540.206423: sched_switch: prev_comm=swapper/3 prev_pid=0 prev_prio=120 prev_state=R ==> next_comm=sshd next_pid=807 next_prio=120
sshd-807 [003] d..2. 540.206531: sched_switch: prev_comm=sshd prev_pid=807 prev_prio=120 prev_state=S ==> next_comm=swapper/3 next_pid=0 next_prio=120
<idle>-0 [001] d..2. 540.206597: sched_switch: prev_comm=swapper/1 prev_pid=0 prev_prio=120 prev_state=R ==> next_comm=kworker/u16:4 next_pid=58 next_prio=120
kworker/u16:4-58 [001] d..2. 540.206617: sched_switch: prev_comm=kworker/u16:4 prev_pid=58 prev_prio=120 prev_state=I ==> next_comm=bash next_pid=830 next_prio=120
bash-830 [001] d..2. 540.206678: sched_switch: prev_comm=bash prev_pid=830 prev_prio=120 prev_state=R ==> next_comm=kworker/u16:4 next_pid=58 next_prio=120
kworker/u16:4-58 [001] d..2. 540.206696: sched_switch: prev_comm=kworker/u16:4 prev_pid=58 prev_prio=120 prev_state=I ==> next_comm=bash next_pid=830 next_prio=120
bash-830 [001] d..2. 540.206713: sched_switch: prev_comm=bash prev_pid=830 prev_prio=120 prev_state=R ==> next_comm=kworker/u16:4 next_pid=58 next_prio=120echo 1 > options/fields
<...>-998 [002] d..2. 538.643732: sched_switch: next_prio=0x78 (120) next_pid=0x0 (0) next_comm=swapper/2 prev_state=0x20 (32) prev_prio=0x78 (120) prev_pid=0x3e6 (998) prev_comm=trace-cmd
<idle>-0 [001] d..2. 538.643806: sched_switch: next_prio=0x78 (120) next_pid=0x33e (830) next_comm=bash prev_state=0x0 (0) prev_prio=0x78 (120) prev_pid=0x0 (0) prev_comm=swapper/1
bash-830 [001] d..2. 538.644106: sched_switch: next_prio=0x78 (120) next_pid=0x3a (58) next_comm=kworker/u16:4 prev_state=0x0 (0) prev_prio=0x78 (120) prev_pid=0x33e (830) prev_comm=bash
kworker/u16:4-58 [001] d..2. 538.644130: sched_switch: next_prio=0x78 (120) next_pid=0x33e (830) next_comm=bash prev_state=0x80 (128) prev_prio=0x78 (120) prev_pid=0x3a (58) prev_comm=kworker/u16:4
bash-830 [001] d..2. 538.644180: sched_switch: next_prio=0x78 (120) next_pid=0x3a (58) next_comm=kworker/u16:4 prev_state=0x0 (0) prev_prio=0x78 (120) prev_pid=0x33e (830) prev_comm=bash
kworker/u16:4-58 [001] d..2. 538.644185: sched_switch: next_prio=0x78 (120) next_pid=0x33e (830) next_comm=bash prev_state=0x80 (128) prev_prio=0x78 (120) prev_pid=0x3a (58) prev_comm=kworker/u16:4
bash-830 [001] d..2. 538.644204: sched_switch: next_prio=0x78 (120) next_pid=0x0 (0) next_comm=swapper/1 prev_state=0x1 (1) prev_prio=0x78 (120) prev_pid=0x33e (830) prev_comm=bash
<idle>-0 [003] d..2. 538.644211: sched_switch: next_prio=0x78 (120) next_pid=0x327 (807) next_comm=sshd prev_state=0x0 (0) prev_prio=0x78 (120) prev_pid=0x0 (0) prev_comm=swapper/3
sshd-807 [003] d..2. 538.644340: sched_switch: next_prio=0x78 (120) next_pid=0x0 (0) next_comm=swapper/3 prev_state=0x1 (1) prev_prio=0x78 (120) prev_pid=0x327 (807) prev_comm=sshdIt traces the data safely without using the trace print formatting.
--- Documentation/trace/ftrace.rst | 6 ++ kernel/trace/trace.c | 7 +- kernel/trace/trace.h | 2 + kernel/trace/trace_output.c | 168 +++++++++++++++++++++++++++++++++ kernel/trace/trace_output.h | 2 + 5 files changed, 183 insertions(+), 2 deletions(-)
Documentation/trace/ftrace.rst +6 -0
@@ -1027,6 +1027,7 @@ To see what is available, simply cat the file:: nohex nobin noblock + nofields trace_printk annotate nouserstacktrace
@@ -1110,6 +1111,11 @@ Here are the available options: block When set, reading trace_pipe will not block when polled. + fields + Print the fields as described by their types. This is a better + option than using hex, bin or raw, as it gives a better parsing + of the content of the event. + trace_printk Can disable trace_printk() from writing into the buffer.
kernel/trace/trace.c +5 -2
@@ -3726,7 +3726,7 @@ __find_next_entry(struct trace_iterator *iter, int *ent_cpu,#define STATIC_FMT_BUF_SIZE 128static char static_fmt_buf[STATIC_FMT_BUF_SIZE ]; -static char *trace_iter_expand_format (struct trace_iterator *iter) +char *trace_iter_expand_format (struct trace_iterator *iter) {char *tmp;
@@ -4446,8 +4446,11 @@ static enum print_line_t print_trace_fmt(struct trace_iterator *iter)if (trace_seq_has_overflowed (s))return TRACE_TYPE_PARTIAL_LINE ; -if (event) +if (event) { +if (tr->trace_flags &TRACE_ITER_FIELDS ) +return print_event_fields (iter, event);return event->funcs->trace (iter, sym_flags, event); + }trace_seq_printf (s,"Unknown type %d\n" , entry->type);
kernel/trace/trace.h +2 -0
@@ -619,6 +619,7 @@ bool trace_is_tracepoint_string(const char *str);const char *trace_event_format (struct trace_iterator *iter,const char *fmt); void trace_check_vprintf(struct trace_iterator *iter, const char *fmt, va_list ap) __printf(2, 0); +char *trace_iter_expand_format (struct trace_iterator *iter);int trace_empty (struct trace_iterator *iter);
@@ -1199,6 +1200,7 @@ extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf,C (HEX ,"hex" ), \C (BIN ,"bin" ), \C (BLOCK ,"block" ), \ +C (FIELDS ,"fields" ), \C (PRINTK ,"trace_printk" ), \C (ANNOTATE ,"annotate" ), \ C(USERSTACKTRACE, "userstacktrace"), \
kernel/trace/trace_output.c +168 -0
174 lines not on this page · raw message
kernel/trace/trace_output.h +2 -0
@@ -19,6 +19,8 @@ seq_print_ip_sym(struct trace_seq *s, unsigned long ip,extern void trace_seq_print_sym (struct trace_seq *s,unsigned long address,bool offset);extern int trace_print_context (struct trace_iterator *iter);extern int trace_print_lat_context (struct trace_iterator *iter); +extern enum print_line_t print_event_fields (struct trace_iterator *iter, +struct trace_event *event);extern void trace_event_read_lock (void );extern void trace_event_read_unlock (void );
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 12/25 tracing/user_events: Split header into uapi and kernel
In reply to Steven RostedtFrom: Beau Belgrave <[email protected]>
The UAPI parts need to be split out from the kernel parts of user_events now that other parts of the kernel will reference it. Do so by moving the existing include/linux/user_events.h into include/uapi/linux/user_events.h.
--- include/linux/user_events.h | 52 ++++---------------------------- include/uapi/linux/user_events.h | 48 +++++++++++++++++++++++++++++ kernel/trace/trace_events_user.c | 5 --- 3 files changed, 54 insertions(+), 51 deletions(-) create mode 100644 include/uapi/linux/user_events.h
include/linux/user_events.h +6 -46
@@ -1,54 +1,14 @@ -/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* SPDX-License-Identifier: GPL-2.0-only */ /* -* Copyright (c) 2021, Microsoft Corporation. +* Copyright (c) 2022, Microsoft Corporation. * * Authors: * Beau Belgrave <[email protected]> */ -#ifndef _UAPI_LINUX_USER_EVENTS_H -#define _UAPI_LINUX_USER_EVENTS_H -#include <linux/types.h> -#include <linux/ioctl.h> +#ifndef _LINUX_USER_EVENTS_H +#define _LINUX_USER_EVENTS_H -#ifdef __KERNEL__ -#include <linux/uio.h> -#else -#include <sys/uio.h> -#endif +#include <uapi/linux/user_events.h> -#define USER_EVENTS_SYSTEM "user_events" -#define USER_EVENTS_PREFIX "u:" - -/* Create dynamic location entry within a 32-bit value */ -#define DYN_LOC (offset, size) ((size) << 16 | (offset)) - -/* -* Describes an event registration and stores the results of the registration. -* This structure is passed to the DIAG_IOCSREG ioctl, callers at a minimum -* must set the size and name_args before invocation. -*/ -struct user_reg { - -/* Input: Size of the user_reg structure being used */ -__u32 size; - -/* Input: Pointer to string with event name, description and flags */ -__u64 name_args; - -/* Output: Bitwise index of the event within the status page */ -__u32 status_bit; - -/* Output: Index of the event to use when writing data */ -__u32 write_index; -} __attribute__((__packed__ )); - -#define DIAG_IOC_MAGIC '*' - -/* Requests to register a user_event */ -#define DIAG_IOCSREG _IOWR(DIAG_IOC_MAGIC, 0, struct user_reg*) - -/* Requests to delete a user_event */ -#define DIAG_IOCSDEL _IOW(DIAG_IOC_MAGIC, 1, char*) - -#endif /* _UAPI_LINUX_USER_EVENTS_H */ +#endif /* _LINUX_USER_EVENTS_H */
include/uapi/linux/user_events.h new file +48 -0
48 lines not on this page · raw message
kernel/trace/trace_events_user.c +0 -5
@@ -19,12 +19,7 @@#include <linux/tracefs.h> #include <linux/types.h> #include <linux/uaccess.h> -/* Reminder to move to uapi when everything works */ -#ifdef CONFIG_COMPILE_TEST #include <linux/user_events.h> -#else -#include <uapi/linux/user_events.h> -#endif #include "trace.h" #include "trace_dynevent.h"
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 13/25 tracing/user_events: Track fork/exec/exit for mm lifetime
In reply to Steven RostedtFrom: Beau Belgrave <[email protected]>
During tracefs discussions it was decided instead of requiring a mapping within a user-process to track the lifetime of memory descriptors we should hook the appropriate calls. Do this by adding the minimal stubs required for task fork, exec, and exit. Currently this is just a NOP. Future patches will implement these calls fully.
--- fs/exec.c | 2 ++ include/linux/sched.h | 5 +++++ include/linux/user_events.h | 18 ++++++++++++++++++ kernel/exit.c | 2 ++ kernel/fork.c | 2 ++ 5 files changed, 29 insertions(+)
fs/exec.c +2 -0
@@ -65,6 +65,7 @@#include <linux/syscall_user_dispatch.h> #include <linux/coredump.h> #include <linux/time_namespace.h> +#include <linux/user_events.h> #include <linux/uaccess.h> #include <asm/mmu_context.h>
@@ -1859,6 +1860,7 @@ static int bprm_execve(struct linux_binprm *bprm, current->fs->in_exec =0 ; current->in_execve =0 ;rseq_execve (current); +user_events_execve (current);acct_update_integrals (current);task_numa_free (current, false);return retval;
include/linux/sched.h +5 -0
@@ -69,6 +69,7 @@ struct sighand_struct;struct signal_struct ;struct task_delay_info ;struct task_group ; +struct user_event_mm ;/* * Task state bitmask. NOTE! These bits are also
@@ -1528,6 +1529,10 @@ struct task_struct {union rv_task_monitor rv[RV_PER_TASK_MONITORS ];#endif +#ifdef CONFIG_USER_EVENTS +struct user_event_mm *user_event_mm; +#endif +/* * New fields for task_struct should be added above here, so that * they are included in the randomized portion of task_struct.
include/linux/user_events.h +18 -0
@@ -11,4 +11,22 @@#include <uapi/linux/user_events.h> +#ifdef CONFIG_USER_EVENTS +struct user_event_mm { +}; +#endif + +static inline void user_events_fork (struct task_struct *t, +unsigned long clone_flags) +{ +} + +static inline void user_events_execve (struct task_struct *t) +{ +} + +static inline void user_events_exit (struct task_struct *t) +{ +} +#endif /* _LINUX_USER_EVENTS_H */
kernel/exit.c +2 -0
@@ -68,6 +68,7 @@#include <linux/kprobes.h> #include <linux/rethook.h> #include <linux/sysfs.h> +#include <linux/user_events.h> #include <linux/uaccess.h> #include <asm/unistd.h>
@@ -818,6 +819,7 @@ void __noreturn do_exit(long code)coredump_task_exit (tsk);ptrace_event (PTRACE_EVENT_EXIT , code); +user_events_exit (tsk);validate_creds_for_do_exit (tsk);
kernel/fork.c +2 -0
@@ -97,6 +97,7 @@#include <linux/io_uring.h> #include <linux/bpf.h> #include <linux/stackprotector.h> +#include <linux/user_events.h> #include <asm/pgalloc.h> #include <linux/uaccess.h>
@@ -2505,6 +2506,7 @@ static __latent_entropy struct task_struct *copy_process(trace_task_newtask (p, clone_flags);uprobe_copy_process (p, clone_flags); +user_events_fork (p, clone_flags);copy_oom_score_adj (clone_flags, p);
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 14/25 tracing/user_events: Use remote writes for event enablement
In reply to Steven RostedtFrom: Beau Belgrave <[email protected]>
As part of the discussions for user_events aligned with user space tracers, it was determined that user programs should register a aligned value to set or clear a bit when an event becomes enabled. Currently a shared page is being used that requires mmap(). Remove the shared page implementation and move to a user registered address implementation.
In this new model during the event registration from user programs 3 new values are specified. The first is the address to update when the event is either enabled or disabled. The second is the bit to set/clear to reflect the event being enabled. The third is the size of the value at the specified address.
This allows for a local 32/64-bit value in user programs to support both kernel and user tracers. As an example, setting bit 31 for kernel tracers when the event becomes enabled allows for user tracers to use the other bits for ref counts or other flags. The kernel side updates the bit atomically, user programs need to also update these values atomically.
User provided addresses must be aligned on a natural boundary, this allows for single page checking and prevents odd behaviors such as a enable value straddling 2 pages instead of a single page. Currently page faults are only logged, future patches will handle these.
--- include/linux/user_events.h | 53 ++- include/uapi/linux/user_events.h | 15 +- kernel/trace/Kconfig | 5 +- kernel/trace/trace_events_user.c | 586 ++++++++++++++++++++++++------- 4 files changed, 517 insertions(+), 142 deletions(-)
include/linux/user_events.h +52 -1
@@ -9,13 +9,63 @@ #ifndef _LINUX_USER_EVENTS_H #define _LINUX_USER_EVENTS_H +#include <linux/list.h> +#include <linux/refcount.h> +#include <linux/mm_types.h> +#include <linux/workqueue.h> #include <uapi/linux/user_events.h> #ifdef CONFIG_USER_EVENTS struct user_event_mm { + struct list_head link; + struct list_head enablers; + struct mm_struct *mm; + struct user_event_mm *next; + refcount_t refcnt; + refcount_t tasks; + struct rcu_work put_rwork; }; -#endif +extern void user_event_mm_dup(struct task_struct *t, + struct user_event_mm *old_mm); + +extern void user_event_mm_remove(struct task_struct *t); + +static inline void user_events_fork(struct task_struct *t, + unsigned long clone_flags) +{ + struct user_event_mm *old_mm; + + if (!t || !current->user_event_mm) + return; + + old_mm = current->user_event_mm; + + if (clone_flags & CLONE_VM) { + t->user_event_mm = old_mm; + refcount_inc(&old_mm->tasks); + return; + } + + user_event_mm_dup(t, old_mm); +} + +static inline void user_events_execve(struct task_struct *t) +{ + if (!t || !t->user_event_mm) + return; + + user_event_mm_remove(t); +} + +static inline void user_events_exit(struct task_struct *t) +{ + if (!t || !t->user_event_mm) + return; + + user_event_mm_remove(t); +} +#else static inline void user_events_fork(struct task_struct *t, unsigned long clone_flags) {
@@ -28,5 +78,6 @@ static inline void user_events_execve(struct task_struct *t)static inline void user_events_exit (struct task_struct *t) { } +#endif /* CONFIG_USER_EVENTS */ #endif /* _LINUX_USER_EVENTS_H */
include/uapi/linux/user_events.h +12 -3
@@ -27,12 +27,21 @@ struct user_reg {/* Input: Size of the user_reg structure being used */ __u32 size; +/* Input: Bit in enable address to use */ +__u8 enable_bit; + +/* Input: Enable size in bytes at address */ +__u8 enable_size; + +/* Input: Flags for future use, set to 0 */ +__u16 flags; + +/* Input: Address to update when enabled */ +__u64 enable_addr; +/* Input: Pointer to string with event name, description and flags */ __u64 name_args; -/* Output: Bitwise index of the event within the status page */ -__u32 status_bit; -/* Output: Index of the event to use when writing data */ __u32 write_index; } __attribute__((__packed__));
kernel/trace/Kconfig +3 -2
@@ -798,9 +798,10 @@ config USER_EVENTS can be used like an existing kernel trace event. User trace events are generated by writing to a tracefs file. User processes can determine if their tracing events should be - generated by memory mapping a tracefs file and checking for - an associated byte being non-zero. + generated by registering a value and bit with the kernel + that reflects when it is enabled or not. + See Documentation/trace/user_events.rst. If in doubt, say N. config HIST_TRIGGERS
kernel/trace/trace_events_user.c +450 -136
766 lines not on this page · raw message
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 15/25 tracing/user_events: Fixup enable faults asyncly
In reply to Steven RostedtFrom: Beau Belgrave <[email protected]>
When events are enabled within the various tracing facilities, such as ftrace/perf, the event_mutex is held. As events are enabled pages are accessed. We do not want page faults to occur under this lock. Instead queue the fault to a workqueue to be handled in a process context safe way without the lock.
The enable address is marked faulting while the async fault-in occurs. This ensures that we don't attempt to fault-in more than is necessary. Once the page has been faulted in, an address write is re-attempted. If the page couldn't fault-in, then we wait until the next time the event is enabled to prevent any potential infinite loops.
--- kernel/trace/trace_events_user.c | 120 +++++++++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 6 deletions(-)
kernel/trace/trace_events_user.c +114 -6
@@ -99,9 +99,23 @@ struct user_event_enabler {/* Bits 0-5 are for the bit to update upon enable/disable (0-63 allowed) */ #define ENABLE_VAL_BIT_MASK 0x3F +/* Bit 6 is for faulting status of enablement */ +#define ENABLE_VAL_FAULTING_BIT 6 +/* Only duplicate the bit value */ #define ENABLE_VAL_DUP_MASK ENABLE_VAL_BIT_MASK +#define ENABLE_BITOPS (e) ((unsigned long *)&(e)->values) + +/* Used for asynchronous faulting in of pages */ +struct user_event_enabler_fault { +struct work_struct work; +struct user_event_mm *mm; +struct user_event_enabler *enabler; +}; + +static struct kmem_cache *fault_cache; +/* Global list of memory descriptors using user_events */ static LIST_HEAD (user_event_mms);static DEFINE_SPINLOCK (user_event_mms_lock);
@@ -263,7 +277,85 @@ static int user_event_mm_fault_in(struct user_event_mm *mm, unsigned long uaddr) }static int user_event_enabler_write (struct user_event_mm *mm, -struct user_event_enabler *enabler) +struct user_event_enabler *enabler, +bool fixup_fault); + +static void user_event_enabler_fault_fixup (struct work_struct *work) +{ +struct user_event_enabler_fault *fault =container_of ( + work, struct user_event_enabler_fault, work); +struct user_event_enabler *enabler = fault->enabler; +struct user_event_mm *mm = fault->mm; +unsigned long uaddr = enabler->addr; +int ret; + + ret =user_event_mm_fault_in (mm, uaddr); + +if (ret && ret != -ENOENT ) { +struct user_event *user = enabler->event; + +pr_warn ("user_events: Fault for mm: 0x%pK @ 0x%llx event: %s\n" , + mm->mm, (unsigned long long )uaddr,EVENT_NAME (user)); + } + +/* Prevent state changes from racing */ +mutex_lock (&event_mutex); + +/* +* If we managed to get the page, re-issue the write. We do not +* want to get into a possible infinite loop, which is why we only +* attempt again directly if the page came in. If we couldn't get +* the page here, then we will try again the next time the event is +* enabled/disabled. +*/ +clear_bit (ENABLE_VAL_FAULTING_BIT ,ENABLE_BITOPS (enabler)); + +if (!ret) { +mmap_read_lock (mm->mm); +user_event_enabler_write (mm, enabler, true); +mmap_read_unlock (mm->mm); + } + +mutex_unlock (&event_mutex); + +/* In all cases we no longer need the mm or fault */ +user_event_mm_put (mm); +kmem_cache_free (fault_cache, fault); +} + +static bool user_event_enabler_queue_fault (struct user_event_mm *mm, +struct user_event_enabler *enabler) +{ +struct user_event_enabler_fault *fault; + + fault =kmem_cache_zalloc (fault_cache,GFP_NOWAIT | __GFP_NOWARN); + +if (!fault) +return false; + +INIT_WORK (&fault->work, user_event_enabler_fault_fixup); + fault->mm =user_event_mm_get (mm); + fault->enabler = enabler; + +/* Don't try to queue in again while we have a pending fault */ +set_bit (ENABLE_VAL_FAULTING_BIT ,ENABLE_BITOPS (enabler)); + +if (!schedule_work (&fault->work)) { +/* Allow another attempt later */ +clear_bit (ENABLE_VAL_FAULTING_BIT ,ENABLE_BITOPS (enabler)); + +user_event_mm_put (mm); +kmem_cache_free (fault_cache, fault); + +return false; + } + +return true; +} + +static int user_event_enabler_write (struct user_event_mm *mm, +struct user_event_enabler *enabler, +bool fixup_fault) {unsigned long uaddr = enabler->addr;unsigned long *ptr;
@@ -278,11 +370,19 @@ static int user_event_enabler_write(struct user_event_mm *mm,if (refcount_read (&mm->tasks) ==0 )return -ENOENT ; +if (unlikely (test_bit (ENABLE_VAL_FAULTING_BIT ,ENABLE_BITOPS (enabler)))) +return -EBUSY ; + ret =pin_user_pages_remote (mm->mm, uaddr,1 ,FOLL_WRITE |FOLL_NOFAULT , &page,NULL ,NULL ); -if (ret <=0 ) { -pr_warn ("user_events: Enable write failed\n" ); +if (unlikely (ret <=0 )) { +if (!fixup_fault) +return -EFAULT ; + +if (!user_event_enabler_queue_fault (mm, enabler)) +pr_warn ("user_events: Unable to queue fault handler\n" ); +return -EFAULT ; }
@@ -314,7 +414,7 @@ static void user_event_enabler_update(struct user_event *user)list_for_each_entry_rcu (enabler, &mm->enablers, link)if (enabler->event == user) -user_event_enabler_write (mm, enabler); +user_event_enabler_write (mm, enabler, true);rcu_read_unlock ();mmap_read_unlock (mm->mm);
@@ -562,7 +662,7 @@ static struct user_event_enabler/* Attempt to reflect the current state within the process */ mmap_read_lock (user_mm->mm); - *write_result =user_event_enabler_write (user_mm, enabler); + *write_result =user_event_enabler_write (user_mm, enabler, false);mmap_read_unlock (user_mm->mm);/*
@@ -2201,16 +2301,24 @@ static int __init trace_events_user_init(void) {int ret; + fault_cache =KMEM_CACHE (user_event_enabler_fault,0 ); + +if (!fault_cache) +return -ENOMEM ; + init_group =user_event_group_create (&init_user_ns); -if (!init_group) +if (!init_group) { +kmem_cache_destroy (fault_cache);return -ENOMEM ; + } ret =create_user_tracefs ();if (ret) {pr_warn ("user_events could not register with tracefs\n" );user_event_group_destroy (init_group); +kmem_cache_destroy (fault_cache); init_group =NULL ;return ret; }
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 16/25 tracing/user_events: Add ioctl for disabling addresses
In reply to Steven RostedtFrom: Beau Belgrave <[email protected]>
Enablements are now tracked by the lifetime of the task/mm. User processes need to be able to disable their addresses if tracing is requested to be turned off. Before unmapping the page would suffice. However, we now need a stronger contract. Add an ioctl to enable this.
A new flag bit is added, freeing, to user_event_enabler to ensure that if the event is attempted to be removed while a fault is being handled that the remove is delayed until after the fault is reattempted.
--- include/uapi/linux/user_events.h | 24 ++++++++ kernel/trace/trace_events_user.c | 97 +++++++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 2 deletions(-)
include/uapi/linux/user_events.h +24 -0
@@ -46,6 +46,27 @@ struct user_reg {__u32 write_index; } __attribute__((__packed__)); +/* +* Describes an event unregister, callers must set the size, address and bit. +* This structure is passed to the DIAG_IOCSUNREG ioctl to disable bit updates. +*/ +struct user_unreg { +/* Input: Size of the user_unreg structure being used */ +__u32 size; + +/* Input: Bit to unregister */ +__u8 disable_bit; + +/* Input: Reserved, set to 0 */ +__u8 __reserved; + +/* Input: Reserved, set to 0 */ +__u16 __reserved2; + +/* Input: Address to unregister */ +__u64 disable_addr; +} __attribute__((__packed__ )); +#define DIAG_IOC_MAGIC '*'/* Request to register a user_event */
@@ -54,4 +75,7 @@ struct user_reg {/* Request to delete a user_event */ #define DIAG_IOCSDEL _IOW(DIAG_IOC_MAGIC, 1, char *) +/* Requests to unregister a user_event */ +#define DIAG_IOCSUNREG _IOW(DIAG_IOC_MAGIC, 2, struct user_unreg*) +#endif /* _UAPI_LINUX_USER_EVENTS_H */
kernel/trace/trace_events_user.c +95 -2
139 lines not on this page · raw message
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 17/25 tracing/user_events: Update self-tests to write ABI
In reply to Steven RostedtFrom: Beau Belgrave <[email protected]>
ABI has been changed to remote writes, update existing test cases to use this new ABI to ensure existing functionality continues to work.
--- .../selftests/user_events/ftrace_test.c | 152 ++++++++++-------- .../testing/selftests/user_events/perf_test.c | 33 ++-- 2 files changed, 96 insertions(+), 89 deletions(-)
tools/testing/selftests/user_events/ftrace_test.c +83 -69
@@ -12,6 +12,7 @@#include <fcntl.h> #include <sys/ioctl.h> #include <sys/stat.h> +#include <sys/uio.h> #include <unistd.h> #include "../kselftest_harness.h"
@@ -22,11 +23,6 @@ const char *enable_file = "/sys/kernel/tracing/events/user_events/__test_event/econst char *trace_file ="/sys/kernel/tracing/trace" ;const char *fmt_file ="/sys/kernel/tracing/events/user_events/__test_event/format" ; -static inline int status_check (char *status_page,int status_bit) -{ -return status_page[status_bit >>3 ] & (1 << (status_bit &7 )); -} -static int trace_bytes (void ) {int fd =open (trace_file,O_RDONLY );
@@ -106,13 +102,23 @@ static int get_print_fmt(char *buffer, int len)return -1 ; } -static int clear (void ) +static int clear (int *check) { +struct user_unreg unreg = {0 }; + + unreg.size =sizeof (unreg); + unreg.disable_bit =31 ; + unreg.disable_addr = (__u64 )check; +int fd =open (data_file,O_RDWR );if (fd ==-1 )return -1 ; +if (ioctl (fd,DIAG_IOCSUNREG , &unreg) ==-1 ) +if (errno !=ENOENT ) +return -1 ; +if (ioctl (fd,DIAG_IOCSDEL ,"__test_event" ) ==-1 )if (errno !=ENOENT )return -1 ;
@@ -122,7 +128,7 @@ static int clear(void)return 0 ; } -static int check_print_fmt (const char *event,const char *expected) +static int check_print_fmt (const char *event,const char *expected,int *check) {struct user_reg reg = {0 };char print_fmt[256 ];
@@ -130,7 +136,7 @@ static int check_print_fmt(const char *event, const char *expected)int fd;/* Ensure cleared */ - ret =clear (); + ret =clear (check);if (ret !=0 )return ret;
@@ -142,14 +148,19 @@ static int check_print_fmt(const char *event, const char *expected) reg.size =sizeof (reg); reg.name_args = (__u64 )event; + reg.enable_bit =31 ; + reg.enable_addr = (__u64 )check; + reg.enable_size =sizeof (*check);/* Register should work */ ret =ioctl (fd,DIAG_IOCSREG , ®);close (fd); -if (ret !=0 ) +if (ret !=0 ) { +printf ("Reg failed in fmt\n" );return ret; + }/* Ensure correct print_fmt */ ret =get_print_fmt (print_fmt,sizeof (print_fmt));
@@ -164,6 +175,7 @@ FIXTURE(user) {int status_fd;int data_fd;int enable_fd; +int check; };FIXTURE_SETUP (user) {
@@ -185,59 +197,56 @@ FIXTURE_TEARDOWN(user) {close (self->enable_fd); } -ASSERT_EQ (0 ,clear ()); +if (clear (&self->check) !=0 ) +printf ("WARNING: Clear didn't work!\n" ); } TEST_F(user, register_events) {struct user_reg reg = {0 }; -int page_size =sysconf (_SC_PAGESIZE); -char *status_page; +struct user_unreg unreg = {0 }; reg.size =sizeof (reg); reg.name_args = (__u64 )"__test_event u32 field1; u32 field2" ; + reg.enable_bit =31 ; + reg.enable_addr = (__u64)&self->check; + reg.enable_size =sizeof (self->check); - status_page =mmap (NULL , page_size,PROT_READ ,MAP_SHARED , - self->status_fd,0 ); + unreg.size =sizeof (unreg); + unreg.disable_bit =31 ; + unreg.disable_addr = (__u64)&self->check;/* Register should work */ ASSERT_EQ (0 ,ioctl (self->data_fd,DIAG_IOCSREG , ®));ASSERT_EQ (0 , reg.write_index); -ASSERT_NE (0 , reg.status_bit);/* Multiple registers should result in same index */ ASSERT_EQ (0 ,ioctl (self->data_fd,DIAG_IOCSREG , ®));ASSERT_EQ (0 , reg.write_index); -ASSERT_NE (0 , reg.status_bit);/* Ensure disabled */ self->enable_fd =open (enable_file,O_RDWR );ASSERT_NE (-1 , self->enable_fd);ASSERT_NE (-1 ,write (self->enable_fd,"0" ,sizeof ("0" ))) - /* MMAP should work and be zero'd */ -ASSERT_NE (MAP_FAILED , status_page); -ASSERT_NE (NULL , status_page); -ASSERT_EQ (0 ,status_check (status_page, reg.status_bit)); - /* Enable event and ensure bits updated in status */ ASSERT_NE(-1, write(self->enable_fd, "1", sizeof("1"))) -ASSERT_NE (0 ,status_check (status_page, reg.status_bit)); +ASSERT_EQ (1 << reg.enable_bit, self->check);/* Disable event and ensure bits updated in status */ ASSERT_NE(-1, write(self->enable_fd, "0", sizeof("0"))) -ASSERT_EQ (0 ,status_check (status_page, reg.status_bit)); +ASSERT_EQ (0 , self->check);/* File still open should return -EBUSY for delete */ ASSERT_EQ (-1 ,ioctl (self->data_fd,DIAG_IOCSDEL ,"__test_event" ));ASSERT_EQ (EBUSY , errno); -/* Delete should work only after close */ +/* Unregister */ +ASSERT_EQ (0 ,ioctl (self->data_fd,DIAG_IOCSUNREG , &unreg)); + +/* Delete should work only after close and unregister */ close (self->data_fd); self->data_fd =open (data_file,O_RDWR );ASSERT_EQ (0 ,ioctl (self->data_fd,DIAG_IOCSDEL ,"__test_event" )); - -/* Unmap should work */ -ASSERT_EQ (0 ,munmap (status_page, page_size)); } TEST_F(user, write_events) {
@@ -245,11 +254,12 @@ TEST_F(user, write_events) {struct iovec io[3 ];__u32 field1, field2;int before =0 , after =0 ; -int page_size =sysconf (_SC_PAGESIZE); -char *status_page; reg.size =sizeof (reg); reg.name_args = (__u64 )"__test_event u32 field1; u32 field2" ; + reg.enable_bit =31 ; + reg.enable_addr = (__u64)&self->check; + reg.enable_size =sizeof (self->check); field1 =1 ; field2 =2 ;
@@ -261,18 +271,10 @@ TEST_F(user, write_events) { io[2 ].iov_base = &field2; io[2 ].iov_len =sizeof (field2); - status_page =mmap (NULL , page_size,PROT_READ ,MAP_SHARED , - self->status_fd,0 ); -/* Register should work */ ASSERT_EQ (0 ,ioctl (self->data_fd,DIAG_IOCSREG , ®));ASSERT_EQ (0 , reg.write_index); -ASSERT_NE (0 , reg.status_bit); - -/* MMAP should work and be zero'd */ -ASSERT_NE (MAP_FAILED , status_page); -ASSERT_NE (NULL , status_page); -ASSERT_EQ (0 ,status_check (status_page, reg.status_bit)); +ASSERT_EQ (0 , self->check);/* Write should fail on invalid slot with ENOENT */ io[0 ].iov_base = &field2;
@@ -287,7 +289,7 @@ TEST_F(user, write_events) {ASSERT_NE (-1 ,write (self->enable_fd,"1" ,sizeof ("1" ))) /* Event should now be enabled */ -ASSERT_NE (0 ,status_check (status_page, reg.status_bit)); +ASSERT_NE (1 << reg.enable_bit, self->check);/* Write should make it out to ftrace buffers */ before =trace_bytes ();
@@ -304,6 +306,9 @@ TEST_F(user, write_fault) { reg.size =sizeof (reg); reg.name_args = (__u64 )"__test_event u64 anon" ; + reg.enable_bit =31 ; + reg.enable_addr = (__u64)&self->check; + reg.enable_size =sizeof (self->check); anon =mmap (NULL , l,PROT_READ ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 );ASSERT_NE (MAP_FAILED , anon);
@@ -316,7 +321,6 @@ TEST_F(user, write_fault) {/* Register should work */ ASSERT_EQ (0 ,ioctl (self->data_fd,DIAG_IOCSREG , ®));ASSERT_EQ (0 , reg.write_index); -ASSERT_NE (0 , reg.status_bit);/* Write should work normally */ ASSERT_NE (-1 ,writev (self->data_fd, (const struct iovec *)io,2 ));
@@ -333,24 +337,17 @@ TEST_F(user, write_validator) {int loc, bytes;char data[8 ];int before =0 , after =0 ; -int page_size =sysconf (_SC_PAGESIZE); -char *status_page; - - status_page =mmap (NULL , page_size,PROT_READ ,MAP_SHARED , - self->status_fd,0 ); reg.size =sizeof (reg); reg.name_args = (__u64 )"__test_event __rel_loc char[] data" ; + reg.enable_bit =31 ; + reg.enable_addr = (__u64)&self->check; + reg.enable_size =sizeof (self->check);/* Register should work */ ASSERT_EQ (0 ,ioctl (self->data_fd,DIAG_IOCSREG , ®));ASSERT_EQ (0 , reg.write_index); -ASSERT_NE (0 , reg.status_bit); - -/* MMAP should work and be zero'd */ -ASSERT_NE (MAP_FAILED , status_page); -ASSERT_NE (NULL , status_page); -ASSERT_EQ (0 ,status_check (status_page, reg.status_bit)); +ASSERT_EQ (0 , self->check); io[0 ].iov_base = ®.write_index; io[0 ].iov_len =sizeof (reg.write_index);
@@ -369,7 +366,7 @@ TEST_F(user, write_validator) {ASSERT_NE (-1 ,write (self->enable_fd,"1" ,sizeof ("1" ))) /* Event should now be enabled */ -ASSERT_NE (0 ,status_check (status_page, reg.status_bit)); +ASSERT_EQ (1 << reg.enable_bit, self->check);/* Full in-bounds write should work */ before =trace_bytes ();
@@ -409,71 +406,88 @@ TEST_F(user, print_fmt) {int ret; ret =check_print_fmt ("__test_event __rel_loc char[] data" , -"print fmt: \"data=%s\", __get_rel_str(data)" ); +"print fmt: \"data=%s\", __get_rel_str(data)" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event __data_loc char[] data" , -"print fmt: \"data=%s\", __get_str(data)" ); +"print fmt: \"data=%s\", __get_str(data)" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event s64 data" , -"print fmt: \"data=%lld\", REC->data" ); +"print fmt: \"data=%lld\", REC->data" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event u64 data" , -"print fmt: \"data=%llu\", REC->data" ); +"print fmt: \"data=%llu\", REC->data" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event s32 data" , -"print fmt: \"data=%d\", REC->data" ); +"print fmt: \"data=%d\", REC->data" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event u32 data" , -"print fmt: \"data=%u\", REC->data" ); +"print fmt: \"data=%u\", REC->data" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event int data" , -"print fmt: \"data=%d\", REC->data" ); +"print fmt: \"data=%d\", REC->data" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event unsigned int data" , -"print fmt: \"data=%u\", REC->data" ); +"print fmt: \"data=%u\", REC->data" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event s16 data" , -"print fmt: \"data=%d\", REC->data" ); +"print fmt: \"data=%d\", REC->data" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event u16 data" , -"print fmt: \"data=%u\", REC->data" ); +"print fmt: \"data=%u\", REC->data" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event short data" , -"print fmt: \"data=%d\", REC->data" ); +"print fmt: \"data=%d\", REC->data" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event unsigned short data" , -"print fmt: \"data=%u\", REC->data" ); +"print fmt: \"data=%u\", REC->data" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event s8 data" , -"print fmt: \"data=%d\", REC->data" ); +"print fmt: \"data=%d\", REC->data" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event u8 data" , -"print fmt: \"data=%u\", REC->data" ); +"print fmt: \"data=%u\", REC->data" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event char data" , -"print fmt: \"data=%d\", REC->data" ); +"print fmt: \"data=%d\", REC->data" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event unsigned char data" , -"print fmt: \"data=%u\", REC->data" ); +"print fmt: \"data=%u\", REC->data" , + &self->check);ASSERT_EQ (0 , ret); ret =check_print_fmt ("__test_event char[4] data" , -"print fmt: \"data=%s\", REC->data" ); +"print fmt: \"data=%s\", REC->data" , + &self->check);ASSERT_EQ (0 , ret); }
tools/testing/selftests/user_events/perf_test.c +13 -20
@@ -19,7 +19,6 @@#include "../kselftest_harness.h" const char *data_file ="/sys/kernel/tracing/user_events_data" ; -const char *status_file ="/sys/kernel/tracing/user_events_status" ;const char *id_file ="/sys/kernel/tracing/events/user_events/__test_event/id" ;const char *fmt_file ="/sys/kernel/tracing/events/user_events/__test_event/format" ;
@@ -35,11 +34,6 @@ static long perf_event_open(struct perf_event_attr *pe, pid_t pid,return syscall (__NR_perf_event_open, pe, pid, cpu, group_fd, flags); } -static inline int status_check (char *status_page,int status_bit) -{ -return status_page[status_bit >>3 ] & (1 << (status_bit &7 )); -} -static int get_id (void ) {FILE *fp =fopen (id_file,"r" );
@@ -88,45 +82,38 @@ static int get_offset(void) }FIXTURE (user) { -int status_fd;int data_fd; +int check; };FIXTURE_SETUP (user) { - self->status_fd =open (status_file,O_RDONLY ); -ASSERT_NE (-1 , self->status_fd); - self->data_fd =open (data_file,O_RDWR );ASSERT_NE (-1 , self->data_fd); }FIXTURE_TEARDOWN (user) { -close (self->status_fd);close (self->data_fd); } TEST_F(user, perf_write) {struct perf_event_attr pe = {0 };struct user_reg reg = {0 }; -int page_size =sysconf (_SC_PAGESIZE); -char *status_page;struct event event;struct perf_event_mmap_page *perf_page; +int page_size =sysconf (_SC_PAGESIZE);int id, fd, offset;__u32 *val; reg.size =sizeof (reg); reg.name_args = (__u64 )"__test_event u32 field1; u32 field2" ; - - status_page =mmap (NULL , page_size,PROT_READ ,MAP_SHARED , - self->status_fd,0 ); -ASSERT_NE (MAP_FAILED , status_page); + reg.enable_bit =31 ; + reg.enable_addr = (__u64)&self->check; + reg.enable_size =sizeof (self->check);/* Register should work */ ASSERT_EQ (0 ,ioctl (self->data_fd,DIAG_IOCSREG , ®));ASSERT_EQ (0 , reg.write_index); -ASSERT_NE (0 , reg.status_bit); -ASSERT_EQ (0 ,status_check (status_page, reg.status_bit)); +ASSERT_EQ (0 , self->check);/* Id should be there */ id =get_id ();
@@ -149,7 +136,7 @@ TEST_F(user, perf_write) {ASSERT_NE (MAP_FAILED , perf_page);/* Status should be updated */ -ASSERT_NE (0 ,status_check (status_page, reg.status_bit)); +ASSERT_EQ (1 << reg.enable_bit, self->check); event.index = reg.write_index; event.field1 =0xc001 ;
@@ -165,6 +152,12 @@ TEST_F(user, perf_write) {/* Ensure correct */ ASSERT_EQ (event.field1, *val++);ASSERT_EQ (event.field2, *val++); + +munmap (perf_page, page_size *2 ); +close (fd); + +/* Status should be updated */ +ASSERT_EQ (0 , self->check); }int main (int argc,char **argv)
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 18/25 tracing/user_events: Add ABI self-test
In reply to Steven RostedtFrom: Beau Belgrave <[email protected]>
Add ABI specific self-test to ensure enablements work in various scenarios such as fork, VM_CLONE, and basic event enable/disable. Ensure ABI contracts/limits are also being upheld, such as bit limits and data size limits.
--- tools/testing/selftests/user_events/Makefile | 2 +- .../testing/selftests/user_events/abi_test.c | 226 ++++++++++++++++++ 2 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/user_events/abi_test.c
tools/testing/selftests/user_events/Makefile +1 -1
@@ -10,7 +10,7 @@ LDLIBS += -lrt -lpthread -lm # This test will not compile until user_events.h is added # back to uapi. -TEST_GEN_PROGS = ftrace_test dyn_test perf_test +TEST_GEN_PROGS = ftrace_test dyn_test perf_test abi_test TEST_FILES := settings
tools/testing/selftests/user_events/abi_test.c new file +226 -0
226 lines not on this page · raw message
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 19/25 tracing/user_events: Use write ABI in example
In reply to Steven RostedtFrom: Beau Belgrave <[email protected]>
The ABI has changed to use a remote write approach. Update the example to show the expected use of this new ABI. Also remove debugfs path and use tracefs to ensure example works in more environments.
--- samples/user_events/example.c | 45 +++++++---------------------------- 1 file changed, 8 insertions(+), 37 deletions(-)
samples/user_events/example.c +8 -37
@@ -9,51 +9,28 @@#include <errno.h> #include <sys/ioctl.h> #include <sys/mman.h> +#include <sys/uio.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> -#include <asm/bitsperlong.h> -#include <endian.h> #include <linux/user_events.h> -#if __BITS_PER_LONG ==64 -#define endian_swap (x) htole64(x) -#else -#define endian_swap (x) htole32(x) -#endif - -/* Assumes debugfs is mounted */ const char *data_file ="/sys/kernel/tracing/user_events_data" ; -const char *status_file ="/sys/kernel/tracing/user_events_status" ; +int enabled =0 ; -static int event_status (long **status) -{ -int fd =open (status_file,O_RDONLY ); - - *status =mmap (NULL ,sysconf (_SC_PAGESIZE),PROT_READ , -MAP_SHARED , fd,0 ); - -close (fd); - -if (*status ==MAP_FAILED ) -return -1 ; - -return 0 ; -} - -static int event_reg (int fd,const char *command,long *index,long *mask, -int *write) +static int event_reg (int fd,const char *command,int *write,int *enabled) {struct user_reg reg = {0 }; reg.size =sizeof (reg); + reg.enable_bit =31 ; + reg.enable_size =sizeof (*enabled); + reg.enable_addr = (__u64 )enabled; reg.name_args = (__u64 )command;if (ioctl (fd,DIAG_IOCSREG , ®) ==-1 )return -1 ; - *index = reg.status_bit / __BITS_PER_LONG; - *mask =endian_swap (1L << (reg.status_bit % __BITS_PER_LONG)); *write = reg.write_index;return 0 ;
@@ -62,17 +39,12 @@ static int event_reg(int fd, const char *command, long *index, long *mask,int main (int argc,char **argv) {int data_fd, write; -long index, mask; -long *status_page;struct iovec io[2 ];__u32 count =0 ; -if (event_status (&status_page) ==-1 ) -return errno; - data_fd =open (data_file,O_RDWR ); -if (event_reg (data_fd,"test u32 count" , &index, &mask, &write) ==-1 ) +if (event_reg (data_fd,"test u32 count" , &write, &enabled) ==-1 )return errno;/* Setup iovec */
@@ -80,13 +52,12 @@ int main(int argc, char **argv) io[0 ].iov_len =sizeof (write); io[1 ].iov_base = &count; io[1 ].iov_len =sizeof (count); - ask:printf ("Press enter to check status...\n" );getchar ();/* Check if anyone is listening */ -if (status_page[index] & mask) { +if (enabled) {/* Yep, trace out our data */ writev (data_fd, (const struct iovec *)io,2 );
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 20/25 tracing/user_events: Update documentation for ABI
In reply to Steven RostedtFrom: Beau Belgrave <[email protected]>
The ABI for user_events has changed from mmap() based to remote writes. Update the documentation to reflect these changes, add new section for unregistering events since lifetime is now tied to tasks instead of files.
--- Documentation/trace/user_events.rst | 167 ++++++++++++++++------------ 1 file changed, 97 insertions(+), 70 deletions(-)
Documentation/trace/user_events.rst +97 -70
@@ -20,11 +20,10 @@ dynamic_events is the same as the ioctl with the u: prefix applied. Typically programs will register a set of events that they wish to expose to tools that can read trace_events (such as ftrace and perf). The registration -process gives back two ints to the program for each event. The first int is -the status bit. This describes which bit in little-endian format in the -/sys/kernel/tracing/user_events_status file represents this event. The -second int is the write index which describes the data when a write() or -writev() is called on the /sys/kernel/tracing/user_events_data file. +process tells the kernel which address and bit to reflect if any tool has +enabled the event and data should be written. The registration will give back +a write index which describes the data when a write() or writev() is called +on the /sys/kernel/tracing/user_events_data file. The structures referenced in this document are contained within the /include/uapi/linux/user_events.h file in the source tree.
@@ -41,23 +40,64 @@ DIAG_IOCSREG. This command takes a packed struct user_reg as an argument:: struct user_reg { - u32 size; - u64 name_args; - u32 status_bit; - u32 write_index; - }; + /* Input: Size of the user_reg structure being used */ + __u32 size; + + /* Input: Bit in enable address to use */ + __u8 enable_bit; + + /* Input: Enable size in bytes at address */ + __u8 enable_size; + + /* Input: Flags for future use, set to 0 */ + __u16 flags; + + /* Input: Address to update when enabled */ + __u64 enable_addr; + + /* Input: Pointer to string with event name, description and flags */ + __u64 name_args; + + /* Output: Index of the event to use when writing data */ + __u32 write_index; + } __attribute__((__packed__)); + +The struct user_reg requires all the above inputs to be set appropriately. + ++ size: This must be set to sizeof(struct user_reg). -The struct user_reg requires two inputs, the first is the size of the structure -to ensure forward and backward compatibility. The second is the command string -to issue for registering. Upon success two outputs are set, the status bit -and the write index. ++ enable_bit: The bit to reflect the event status at the address specified by + enable_addr. + ++ enable_size: The size of the value specified by enable_addr. + This must be 4 (32-bit) or 8 (64-bit). 64-bit values are only allowed to be + used on 64-bit kernels, however, 32-bit can be used on all kernels. + ++ flags: The flags to use, if any. For the initial version this must be 0. + Callers should first attempt to use flags and retry without flags to ensure + support for lower versions of the kernel. If a flag is not supported -EINVAL + is returned. + ++ enable_addr: The address of the value to use to reflect event status. This + must be naturally aligned and write accessible within the user program. + ++ name_args: The name and arguments to describe the event, see command format + for details. + +Upon successful registration the following is set. + ++ write_index: The index to use for this file descriptor that represents this + event when writing out data. The index is unique to this instance of the file + descriptor that was used for the registration. See writing data for details. User based events show up under tracefs like any other event under the subsystem named "user_events". This means tools that wish to attach to the events need to use /sys/kernel/tracing/events/user_events/[name]/enable or perf record -e user_events:[name] when attaching/recording. -**NOTE:** *The write_index returned is only valid for the FD that was used* +**NOTE:** The event subsystem name by default is "user_events". Callers should +not assume it will always be "user_events". Operators reserve the right in the +future to change the subsystem name per-process to accomodate event isolation. Command Format ^^^^^^^^^^^^^^
@@ -94,7 +134,7 @@ Would be represented by the following field:: struct mytype myname 20 Deleting ------------ +-------- Deleting an event from within a user process is done via ioctl() out to the /sys/kernel/tracing/user_events_data file. The command to issue is DIAG_IOCSDEL.
@@ -104,92 +144,79 @@ its name. Delete will only succeed if there are no references left to the event (in both user and kernel space). User programs should use a separate file to request deletes than the one used for registration due to this. -Status ------- -When tools attach/record user based events the status of the event is updated -in realtime. This allows user programs to only incur the cost of the write() or -writev() calls when something is actively attached to the event. - -User programs call mmap() on /sys/kernel/tracing/user_events_status to -check the status for each event that is registered. The bit to check in the -file is given back after the register ioctl() via user_reg.status_bit. The bit -is always in little-endian format. Programs can check if the bit is set either -using a byte-wise index with a mask or a long-wise index with a little-endian -mask. +Unregistering +------------- +If after registering an event it is no longer wanted to be updated then it can +be disabled via ioctl() out to the /sys/kernel/tracing/user_events_data file. +The command to issue is DIAG_IOCSUNREG. This is different than deleting, where +deleting actually removes the event from the system. Unregistering simply tells +the kernel your process is no longer interested in updates to the event. -Currently the size of user_events_status is a single page, however, custom -kernel configurations can change this size to allow more user based events. In -all cases the size of the file is a multiple of a page size. +This command takes a packed struct user_unreg as an argument:: -For example, if the register ioctl() gives back a status_bit of 3 you would -check byte 0 (3 / 8) of the returned mmap data and then AND the result with 8 -(1 << (3 % 8)) to see if anything is attached to that event. + struct user_unreg { + /* Input: Size of the user_unreg structure being used */ + __u32 size; -A byte-wise index check is performed as follows:: + /* Input: Bit to unregister */ + __u8 disable_bit; - int index, mask; - char *status_page; + /* Input: Reserved, set to 0 */ + __u8 __reserved; - index = status_bit / 8; - mask = 1 << (status_bit % 8); - - ... + /* Input: Reserved, set to 0 */ + __u16 __reserved2; - if (status_page[index] & mask) { - /* Enabled */ - } + /* Input: Address to unregister */ + __u64 disable_addr; + } __attribute__((__packed__)); -A long-wise index check is performed as follows:: +The struct user_unreg requires all the above inputs to be set appropriately. - #include <asm/bitsperlong.h> - #include <endian.h> ++ size: This must be set to sizeof(struct user_unreg). - #if __BITS_PER_LONG == 64 - #define endian_swap(x) htole64(x) - #else - #define endian_swap(x) htole32(x) - #endif ++ disable_bit: This must be set to the bit to disable (same bit that was + previously registered via enable_bit). - long index, mask, *status_page; ++ disable_addr: This must be set to the address to disable (same address that was + previously registered via enable_addr). - index = status_bit / __BITS_PER_LONG; - mask = 1L << (status_bit % __BITS_PER_LONG); - mask = endian_swap(mask); +**NOTE:** Events are automatically unregistered when execve() is invoked. During +fork() the registered events will be retained and must be unregistered manually +in each process if wanted. - ... +Status +------ +When tools attach/record user based events the status of the event is updated +in realtime. This allows user programs to only incur the cost of the write() or +writev() calls when something is actively attached to the event. - if (status_page[index] & mask) { - /* Enabled */ - } +The kernel will update the specified bit that was registered for the event as +tools attach/detach from the event. User programs simply check if the bit is set +to see if something is attached or not. Administrators can easily check the status of all registered events by reading the user_events_status file directly via a terminal. The output is as follows:: - Byte:Name [# Comments] + Name [# Comments] ... Active: ActiveCount Busy: BusyCount - Max: MaxCount For example, on a system that has a single event the output looks like this:: - 1:test + test Active: 1 Busy: 0 - Max: 32768 If a user enables the user event via ftrace, the output would change to this:: - 1:test # Used by ftrace + test # Used by ftrace Active: 1 Busy: 1 - Max: 32768 - -**NOTE:** *A status bit of 0 will never be returned. This allows user programs -to have a bit that can be used on error cases.* Writing Data ------------
@@ -217,7 +244,7 @@ For example, if I have a struct like this:: int src; int dst; int flags; - }; + } __attribute__((__packed__)); It's advised for user programs to do the following::
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 21/25 tracing/user_events: Charge event allocs to cgroups
In reply to Steven RostedtFrom: Beau Belgrave <[email protected]>
Operators need a way to limit how much memory cgroups use. User events need to be included into that accounting. Fix this by using GFP_KERNEL_ACCOUNT for allocations generated by user programs for user_event tracing.
--- kernel/trace/trace_events_user.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
kernel/trace/trace_events_user.c +10 -10
@@ -442,7 +442,7 @@ static bool user_event_enabler_dup(struct user_event_enabler *orig,if (unlikely (test_bit (ENABLE_VAL_FREEING_BIT ,ENABLE_BITOPS (orig))))return true; - enabler =kzalloc (sizeof (*enabler),GFP_NOWAIT ); + enabler =kzalloc (sizeof (*enabler),GFP_NOWAIT | __GFP_ACCOUNT);if (!enabler)return false;
@@ -502,7 +502,7 @@ static struct user_event_mm *user_event_mm_create(struct task_struct *t)struct user_event_mm *user_mm;unsigned long flags; - user_mm =kzalloc (sizeof (*user_mm),GFP_KERNEL ); + user_mm =kzalloc (sizeof (*user_mm),GFP_KERNEL_ACCOUNT );if (!user_mm)return NULL ;
@@ -662,7 +662,7 @@ static struct user_event_enablerif (!user_mm)return NULL ; - enabler =kzalloc (sizeof (*enabler),GFP_KERNEL ); + enabler =kzalloc (sizeof (*enabler),GFP_KERNEL_ACCOUNT );if (!enabler) goto out;
@@ -870,7 +870,7 @@ static int user_event_add_field(struct user_event *user, const char *type,struct ftrace_event_field *field;int validator_flags =0 ; - field =kmalloc (sizeof (*field),GFP_KERNEL ); + field =kmalloc (sizeof (*field),GFP_KERNEL_ACCOUNT );if (!field)return -ENOMEM ;
@@ -889,7 +889,7 @@ static int user_event_add_field(struct user_event *user, const char *type,if (strstr (type,"char" ) !=NULL ) validator_flags |=VALIDATOR_ENSURE_NULL ; - validator =kmalloc (sizeof (*validator),GFP_KERNEL ); + validator =kmalloc (sizeof (*validator),GFP_KERNEL_ACCOUNT );if (!validator) {kfree (field);
@@ -1175,7 +1175,7 @@ static int user_event_create_print_fmt(struct user_event *user) len =user_event_set_print_fmt (user,NULL ,0 ); - print_fmt =kmalloc (len,GFP_KERNEL ); + print_fmt =kmalloc (len,GFP_KERNEL_ACCOUNT );if (!print_fmt)return -ENOMEM ;
@@ -1508,7 +1508,7 @@ static int user_event_create(const char *raw_command) raw_command +=USER_EVENTS_PREFIX_LEN ; raw_command =skip_spaces (raw_command); - name =kstrdup (raw_command,GFP_KERNEL ); + name =kstrdup (raw_command,GFP_KERNEL_ACCOUNT );if (!name)return -ENOMEM ;
@@ -1704,7 +1704,7 @@ static int user_event_parse(struct user_event_group *group, char *name,return 0 ; } - user =kzalloc (sizeof (*user),GFP_KERNEL ); + user =kzalloc (sizeof (*user),GFP_KERNEL_ACCOUNT );if (!user)return -ENOMEM ;
@@ -1874,7 +1874,7 @@ static int user_events_open(struct inode *node, struct file *file)if (!group)return -ENOENT ; - info =kzalloc (sizeof (*info),GFP_KERNEL ); + info =kzalloc (sizeof (*info),GFP_KERNEL_ACCOUNT );if (!info)return -ENOMEM ;
@@ -1927,7 +1927,7 @@ static int user_events_ref_add(struct user_event_file_info *info, size =struct_size (refs, events, count +1 ); - new_refs =kzalloc (size,GFP_KERNEL ); + new_refs =kzalloc (size,GFP_KERNEL_ACCOUNT );if (!new_refs)return -ENOMEM ;
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 22/25 tracing/user_events: Limit global user_event count
In reply to Steven RostedtFrom: Beau Belgrave <[email protected]>
Operators want to be able to ensure enough tracepoints exist on the system for kernel components as well as for user components. Since there are only up to 64K events, by default allow up to half to be used by user events.
Add a kernel sysctl parameter (kernel.user_events_max) to set a global limit that is honored among all groups on the system. This ensures hard limits can be setup to prevent user processes from consuming all event IDs on the system.
--- kernel/trace/trace_events_user.c | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)
kernel/trace/trace_events_user.c +47 -0
@@ -20,6 +20,7 @@#include <linux/types.h> #include <linux/uaccess.h> #include <linux/highmem.h> +#include <linux/init.h> #include <linux/user_events.h> #include "trace.h" #include "trace_dynevent.h"
@@ -61,6 +62,12 @@ struct user_event_group {/* Group for init_user_ns mapping, top-most group */ static struct user_event_group *init_group; +/* Max allowed events for the whole system */ +static unsigned int max_user_events =32768 ; + +/* Current number of events on the whole system */ +static unsigned int current_user_events; +/* * Stores per-event properties, as users register events * within a file a user_event might be created if it does not
@@ -1241,6 +1248,8 @@ static int destroy_user_event(struct user_event *user) {int ret =0 ; +lockdep_assert_held (&event_mutex); +/* Must destroy fields before call removal */ user_event_destroy_fields (user);
@@ -1257,6 +1266,11 @@ static int destroy_user_event(struct user_event *user)kfree (EVENT_NAME (user));kfree (user); +if (current_user_events >0 ) + current_user_events--; +else +pr_alert ("BUG: Bad current_user_events\n" ); +return ret; }
@@ -1744,6 +1758,11 @@ static int user_event_parse(struct user_event_group *group, char *name,mutex_lock (&event_mutex); +if (current_user_events >= max_user_events) { + ret = -EMFILE ; + goto put_user_lock; + } + ret =user_event_trace_register (user);if (ret)
@@ -1755,6 +1774,7 @@ static int user_event_parse(struct user_event_group *group, char *name,dyn_event_init (&user->devent, &user_event_dops);dyn_event_add (&user->devent, &user->call);hash_add (group->register_table, &user->node, key); + current_user_events++;mutex_unlock (&event_mutex);
@@ -2390,6 +2410,31 @@ static int create_user_tracefs(void)return -ENODEV ; } +static int set_max_user_events_sysctl (struct ctl_table *table,int write, +void *buffer,size_t *lenp,loff_t *ppos) +{ +int ret; + +mutex_lock (&event_mutex); + + ret =proc_douintvec (table, write, buffer, lenp, ppos); + +mutex_unlock (&event_mutex); + +return ret; +} + +static struct ctl_table user_event_sysctls[] = { + { + .procname ="user_events_max" , + .data = &max_user_events, + .maxlen =sizeof (unsigned int ), + .mode =0644 , + .proc_handler = set_max_user_events_sysctl, + }, + {} +}; +static int __init trace_events_user_init (void ) {int ret;
@@ -2419,6 +2464,8 @@ static int __init trace_events_user_init(void)if (dyn_event_register (&user_event_dops))pr_warn ("user_events could not register with dyn_events\n" ); +register_sysctl_init ("kernel" , user_event_sysctls); +return 0 ; }
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 23/25 tracing/user_events: Align structs with tabs for readability
In reply to Steven RostedtFrom: Beau Belgrave <[email protected]>
Add tabs to make struct members easier to read and unify the style of the code.
--- include/linux/user_events.h | 14 +++--- include/uapi/linux/user_events.h | 24 +++++----- kernel/trace/trace_events_user.c | 82 ++++++++++++++++---------------- 3 files changed, 60 insertions(+), 60 deletions(-)
include/linux/user_events.h +7 -7
@@ -17,13 +17,13 @@#ifdef CONFIG_USER_EVENTS struct user_event_mm { -struct list_head link; -struct list_head enablers; -struct mm_struct *mm; -struct user_event_mm *next; -refcount_t refcnt; -refcount_t tasks; -struct rcu_work put_rwork; +struct list_head link; +struct list_head enablers; +struct mm_struct *mm; +struct user_event_mm *next; +refcount_t refcnt; +refcount_t tasks; +struct rcu_work put_rwork; }; extern void user_event_mm_dup(struct task_struct *t,
include/uapi/linux/user_events.h +12 -12
@@ -25,25 +25,25 @@struct user_reg {/* Input: Size of the user_reg structure being used */ -__u32 size; +__u32 size;/* Input: Bit in enable address to use */ -__u8 enable_bit; +__u8 enable_bit;/* Input: Enable size in bytes at address */ -__u8 enable_size; +__u8 enable_size;/* Input: Flags for future use, set to 0 */ -__u16 flags; +__u16 flags;/* Input: Address to update when enabled */ -__u64 enable_addr; +__u64 enable_addr;/* Input: Pointer to string with event name, description and flags */ -__u64 name_args; +__u64 name_args;/* Output: Index of the event to use when writing data */ -__u32 write_index; +__u32 write_index; } __attribute__((__packed__ ));/*
@@ -52,19 +52,19 @@ struct user_reg {*/ struct user_unreg {/* Input: Size of the user_unreg structure being used */ -__u32 size; +__u32 size;/* Input: Bit to unregister */ -__u8 disable_bit; +__u8 disable_bit;/* Input: Reserved, set to 0 */ -__u8 __reserved; +__u8 __reserved;/* Input: Reserved, set to 0 */ -__u16 __reserved2; +__u16 __reserved2;/* Input: Address to unregister */ -__u64 disable_addr; +__u64 disable_addr; } __attribute__((__packed__ ));#define DIAG_IOC_MAGIC '*'
kernel/trace/trace_events_user.c +41 -41
142 lines not on this page · raw message
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 24/25 tracing/user_events: Use print_format_fields() for trace output
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Currently, user events are shown using the "hex" output for "safety" reasons as one cannot trust user events behaving nicely. But the hex output is not the only utility for safe outputting of trace events. The print_event_fields() is just as safe and gives user readable output.
Before:
example-839 [001] ..... 43.222244:
00000000: b1 06 00 00 47 03 00 00 00 00 00 00 ....G.......
example-839 [001] ..... 43.564433:
00000000: b1 06 00 00 47 03 00 00 01 00 00 00 ....G.......
example-839 [001] ..... 43.763917:
00000000: b1 06 00 00 47 03 00 00 02 00 00 00 ....G.......
example-839 [001] ..... 43.967929:00000000: b1 06 00 00 47 03 00 00 03 00 00 00 ....G.......
After:
example-837 [006] ..... 55.739249: test: count=0x0 (0)
example-837 [006] ..... 111.104784: test: count=0x1 (1)
example-837 [006] ..... 111.268444: test: count=0x2 (2)
example-837 [006] ..... 111.416533: test: count=0x3 (3)
example-837 [006] ..... 111.542859: test: count=0x4 (4)--- kernel/trace/trace_events_user.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
kernel/trace/trace_events_user.c +3 -6
@@ -22,8 +22,9 @@#include <linux/highmem.h> #include <linux/init.h> #include <linux/user_events.h> -#include "trace.h" #include "trace_dynevent.h" +#include "trace_output.h" +#include "trace.h" #define USER_EVENTS_PREFIX_LEN (sizeof(USER_EVENTS_PREFIX)-1)
@@ -1198,11 +1199,7 @@ static enum print_line_t user_event_print_trace(struct trace_iterator *iter,int flags, struct trace_event *event) { -/* Unsafe to try to decode user provided print_fmt, use hex */ - trace_print_hex_dump_seq(&iter->seq, "", DUMP_PREFIX_OFFSET, 16, - 1, iter->ent, iter->ent_size, true); - -return trace_handle_return (&iter->seq); + return print_event_fields(iter, event); } static struct trace_event_functions user_event_funcs = {
Signature · 1 line
2.39.1
Steven Rostedt
maintainerPATCH 25/25 tracing: Unbreak user events
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
The user events was added a bit prematurely, and there were a few kernel developers that had issues with it. The API also needed a bit of work to make sure it would be stable. It was decided to make user events "broken" until this was settled. Now it has a new API that appears to be as stable as it will be without the use of a crystal ball. It's being used within Microsoft as is, which means the API has had some testing in real world use cases. It went through many discussions in the bi-weekly tracing meetings, and there's been no more comments about updates.
I feel this is good to go.
--- kernel/trace/Kconfig | 1 - 1 file changed, 1 deletion(-)
kernel/trace/Kconfig +0 -1
@@ -792,7 +792,6 @@ config USER_EVENTS bool "User trace events" select TRACING select DYNAMIC_EVENTS - depends on BROKEN || COMPILE_TEST # API needs to be straighten out help User trace events are user-defined trace events that can be used like an existing kernel trace event. User trace
Signature · 1 line
2.39.1
Steven Rostedt
maintainerRe: [for-next][PATCH 25/25] tracing: Unbreak user events
I forgot to Cc Beau on this.
-- Steve
Quoted 31 lines · On Wed, 29 Mar 2023 15:45:41 -0400 Steven Rostedt <[email protected]> wrote:
From: "Steven Rostedt (Google)" <[email protected]>
The user events was added a bit prematurely, and there were a few kernel developers that had issues with it. The API also needed a bit of work to make sure it would be stable. It was decided to make user events "broken" until this was settled. Now it has a new API that appears to be as stable as it will be without the use of a crystal ball. It's being used within Microsoft as is, which means the API has had some testing in real world use cases. It went through many discussions in the bi-weekly tracing meetings, and there's been no more comments about updates.
I feel this is good to go.
Cc Mathieu Desnoyers <[email protected]>Signed-off-by Steven Rostedt (Google) <[email protected]>--- kernel/trace/Kconfig | 1 - 1 file changed, 1 deletion(-)kernel/trace/Kconfig +0
-1@@ -792,7 +792,6 @@ config USER_EVENTS bool "User trace events" select TRACING select DYNAMIC_EVENTS - depends on BROKEN || COMPILE_TEST # API needs to be straighten out help User trace events are user-defined trace events that can be used like an existing kernel trace event. User trace