From patchwork Sat Jun 1 08:22:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 13682343 X-Patchwork-Delegate: mhiramat@kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 089C82BD0F; Sat, 1 Jun 2024 08:22:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717230161; cv=none; b=SBhMXb9NT/0sj/zo3eGhbi4ev1pk0hRGwQqiGr5HOQTDY46Q35TGuH8oCIz2V+YB5/+pvQr/h9qfRx1aEd0L5pqH2vzehwEx4Lu8sFjdrENIyVvpskf0kEkcZE8FEQ6swPVFw92DvLlHaweVI0i36OGCZ6gJZx/8UBYHk78QiKE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717230161; c=relaxed/simple; bh=SEa6/naGWiY7r2mBFfkW8iDQpAWKMrIxI6Kmz0cQV7c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=GUXaJI6kqfG2ppU7YUCqM8swPXf1hyJkLRd7Mf+PUAXc1ii8HNBD7KTYjDadnYphToqKSD8T4JFj0hMOaJco6quxGuK8ZAVchC0YKnJeQnbUrbIx7xr7iTloSKDblCgGM31qp5gt2g5kjy5YuNQHFRZToD0j/C6a1cr0Cj3LE1Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Dkgb3fBN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Dkgb3fBN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4C7E8C3277B; Sat, 1 Jun 2024 08:22:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717230160; bh=SEa6/naGWiY7r2mBFfkW8iDQpAWKMrIxI6Kmz0cQV7c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Dkgb3fBNELIPx0iqHx5H4BzpP0nrcQ2qs+LNXaqgfBhr11y4qu/TzTqnlNAVJ0ioG jcFiMfbQiAnK7Ets+2e70k6v3nIB2B3+78eKormQglI1p7Lj0KUa+t1bv/o0g3SrFw Yh8Kjti9jMxw3LayhLPvSp76HPQD/X/OnaQda7CAGxPXapctqEF0/KUTXmYRUTCUQf L4ZXMmNIbqiMI/SlgY00Ehb3+f0vJS7EgpujOnuYhBImG/yX+k/kwhkLsuDyHwDoET qKSxZ4MTd8GJvP2i9h1KqqlkCM4ax3AXqfPRXvJywb+u9vIIWXtdy7kfy9lZtWErIj rrRvGkY1mWT9w== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Mathieu Desnoyers , don , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, mhiramat@kernel.org Subject: [PATCH v2 1/3] tracepoint: Support iterating over tracepoints on modules Date: Sat, 1 Jun 2024 17:22:37 +0900 Message-Id: <171723015695.258703.10976621187063548530.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <171723014778.258703.6731294779199848686.stgit@devnote2> References: <171723014778.258703.6731294779199848686.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Masami Hiramatsu (Google) Add for_each_module_tracepoint() for iterating over tracepoints on modules. This is similar to the for_each_kernel_tracepoint() but only for the tracepoints on modules (not including kernel built-in tracepoints). Signed-off-by: Masami Hiramatsu (Google) --- include/linux/tracepoint.h | 7 +++++++ kernel/tracepoint.c | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/linux/tracepoint.h b/include/linux/tracepoint.h index 689b6d71590e..46e6a5e759fd 100644 --- a/include/linux/tracepoint.h +++ b/include/linux/tracepoint.h @@ -65,6 +65,8 @@ struct tp_module { bool trace_module_has_bad_taint(struct module *mod); extern int register_tracepoint_module_notifier(struct notifier_block *nb); extern int unregister_tracepoint_module_notifier(struct notifier_block *nb); +void for_each_module_tracepoint(void (*fct)(struct tracepoint *, void *), + void *priv); #else static inline bool trace_module_has_bad_taint(struct module *mod) { @@ -80,6 +82,11 @@ int unregister_tracepoint_module_notifier(struct notifier_block *nb) { return 0; } +static inline +void for_each_module_tracepoint(void (*fct)(struct tracepoint *, void *), + void *priv) +{ +} #endif /* CONFIG_MODULES */ /* diff --git a/kernel/tracepoint.c b/kernel/tracepoint.c index 8d1507dd0724..b9b90dc46ab1 100644 --- a/kernel/tracepoint.c +++ b/kernel/tracepoint.c @@ -735,6 +735,22 @@ static __init int init_tracepoints(void) return ret; } __initcall(init_tracepoints); + +void for_each_module_tracepoint(void (*fct)(struct tracepoint *tp, void *priv), + void *priv) +{ + struct tp_module *tp_mod; + struct module *mod; + + mutex_lock(&tracepoint_module_list_mutex); + list_for_each_entry(tp_mod, &tracepoint_module_list, list) { + mod = tp_mod->mod; + for_each_tracepoint_range(mod->tracepoints_ptrs, + mod->tracepoints_ptrs + mod->num_tracepoints, + fct, priv); + } + mutex_unlock(&tracepoint_module_list_mutex); +} #endif /* CONFIG_MODULES */ /** From patchwork Sat Jun 1 08:22:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 13682344 X-Patchwork-Delegate: mhiramat@kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B89A93A1BF; Sat, 1 Jun 2024 08:22:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717230169; cv=none; b=k4WnRgv0gqOCAvxa2V7PyfXy92H9+AbXT9qKqVto3v2HMDUm6cjjV3/kDuy1wFbyjLFn8yb2m+D9y7qSo5DUAT0o21BDgXZgv3qT/9qXdJkNWIsunI30gxAQ0mCRdM6Y70FMRN8U3Iof4LHhfVwtPSvkZPAku1HTKNSYH+/cpRY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717230169; c=relaxed/simple; bh=c0m0QDfRqXzY8JEBFpa5rsivo9DQtJWRdwquS1zNvaQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=sHxYlCyrTlNh4myv/3WhPQxTvfpodTpJLjuMIYg72TD+ZoqQ8EI/1rYwWGNCRh1I6tIETNqzDTn0ZVZJbAZc83vu5+q9CQ5uYNHutiHKDvcdbsfNBp8LUnjQ2l8dD/X+F4JNsvKywtlE0qMGMdmgSpVAKO9meHxRTI8xlN5U9fI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Fx16sp0u; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Fx16sp0u" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 38112C116B1; Sat, 1 Jun 2024 08:22:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717230169; bh=c0m0QDfRqXzY8JEBFpa5rsivo9DQtJWRdwquS1zNvaQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Fx16sp0uxID0l0j5B+rYTbqsNxgR+arVUIO2uderAxws59WgbTdvdz0SWeeCNozcZ imNAJyylAWO468epSHapMsVFK5YsKS1ZE5Anjhvv1FxaTwOD/2CmOTt4tWqqCDfVQn TLmQaoAKrinBPXBYEtq9ebbgXjbipr4ZXHM77Zq3nc4HyZKnhYCuOOv8OgSDIvpMrD 2lX5ib7JMsdns01x8EhKOytWTCmcSq0VnwtcAbwotaNmOKjeool6wW9l/D6CQ9YB72 s5lUq/qesZ6uvokWT46c8kI8Zr4xyODdfyTcFDU70eXUYuYaCyvctAYDdJ0MBTkDSN 4ld6zxcbX3cjw== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Mathieu Desnoyers , don , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, mhiramat@kernel.org Subject: [PATCH v2 2/3] tracing/fprobe: Support raw tracepoint events on modules Date: Sat, 1 Jun 2024 17:22:46 +0900 Message-Id: <171723016594.258703.1629777910752596529.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <171723014778.258703.6731294779199848686.stgit@devnote2> References: <171723014778.258703.6731294779199848686.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Masami Hiramatsu (Google) Support raw tracepoint event on module by fprobe events. Since it only uses for_each_kernel_tracepoint() to find a tracepoint, the tracepoints on modules are not handled. Thus if user specified a tracepoint on a module, it shows an error. This adds new for_each_module_tracepoint() API to tracepoint subsystem, and uses it to find tracepoints on modules. Reported-by: don Closes: https://lore.kernel.org/all/20240530215718.aeec973a1d0bf058d39cb1e3@kernel.org/ Signed-off-by: Masami Hiramatsu (Google) --- Changes in v2: - Fix build errors with CONFIG_MODULES=y. --- kernel/trace/trace_fprobe.c | 46 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/kernel/trace/trace_fprobe.c b/kernel/trace/trace_fprobe.c index 62e6a8f4aae9..1d8a983e1edc 100644 --- a/kernel/trace/trace_fprobe.c +++ b/kernel/trace/trace_fprobe.c @@ -385,6 +385,7 @@ static struct trace_fprobe *alloc_trace_fprobe(const char *group, const char *event, const char *symbol, struct tracepoint *tpoint, + struct module *mod, int maxactive, int nargs, bool is_return) { @@ -405,6 +406,7 @@ static struct trace_fprobe *alloc_trace_fprobe(const char *group, tf->fp.entry_handler = fentry_dispatcher; tf->tpoint = tpoint; + tf->mod = mod; tf->fp.nr_maxactive = maxactive; ret = trace_probe_init(&tf->tp, event, group, false, nargs); @@ -895,8 +897,23 @@ static struct notifier_block tracepoint_module_nb = { struct __find_tracepoint_cb_data { const char *tp_name; struct tracepoint *tpoint; + struct module *mod; }; +static void __find_tracepoint_module_cb(struct tracepoint *tp, void *priv) +{ + struct __find_tracepoint_cb_data *data = priv; + + if (!data->tpoint && !strcmp(data->tp_name, tp->name)) { + data->tpoint = tp; + data->mod = __module_text_address((unsigned long)tp->probestub); + if (!try_module_get(data->mod)) { + data->tpoint = NULL; + data->mod = NULL; + } + } +} + static void __find_tracepoint_cb(struct tracepoint *tp, void *priv) { struct __find_tracepoint_cb_data *data = priv; @@ -905,14 +922,28 @@ static void __find_tracepoint_cb(struct tracepoint *tp, void *priv) data->tpoint = tp; } -static struct tracepoint *find_tracepoint(const char *tp_name) +/* + * Find a tracepoint from kernel and module. If the tracepoint is in a module, + * this increments the module refcount to prevent unloading until the + * trace_fprobe is registered to the list. After registering the trace_fprobe + * on the trace_fprobe list, the module refcount is decremented because + * tracepoint_probe_module_cb will handle it. + */ +static struct tracepoint *find_tracepoint(const char *tp_name, + struct module **tp_mod) { struct __find_tracepoint_cb_data data = { .tp_name = tp_name, + .mod = NULL, }; for_each_kernel_tracepoint(__find_tracepoint_cb, &data); + if (!data.tpoint && IS_ENABLED(CONFIG_MODULES)) { + for_each_module_tracepoint(__find_tracepoint_module_cb, &data); + *tp_mod = data.mod; + } + return data.tpoint; } @@ -996,6 +1027,7 @@ static int __trace_fprobe_create(int argc, const char *argv[]) char abuf[MAX_BTF_ARGS_LEN]; char *dbuf = NULL; bool is_tracepoint = false; + struct module *tp_mod = NULL; struct tracepoint *tpoint = NULL; struct traceprobe_parse_context ctx = { .flags = TPARG_FL_KERNEL | TPARG_FL_FPROBE, @@ -1080,7 +1112,7 @@ static int __trace_fprobe_create(int argc, const char *argv[]) if (is_tracepoint) { ctx.flags |= TPARG_FL_TPOINT; - tpoint = find_tracepoint(symbol); + tpoint = find_tracepoint(symbol, &tp_mod); if (!tpoint) { trace_probe_log_set_index(1); trace_probe_log_err(0, NO_TRACEPOINT); @@ -1110,8 +1142,8 @@ static int __trace_fprobe_create(int argc, const char *argv[]) goto out; /* setup a probe */ - tf = alloc_trace_fprobe(group, event, symbol, tpoint, maxactive, - argc, is_return); + tf = alloc_trace_fprobe(group, event, symbol, tpoint, tp_mod, + maxactive, argc, is_return); if (IS_ERR(tf)) { ret = PTR_ERR(tf); /* This must return -ENOMEM, else there is a bug */ @@ -1119,10 +1151,6 @@ static int __trace_fprobe_create(int argc, const char *argv[]) goto out; /* We know tf is not allocated */ } - if (is_tracepoint) - tf->mod = __module_text_address( - (unsigned long)tf->tpoint->probestub); - /* parse arguments */ for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { trace_probe_log_set_index(i + 2); @@ -1155,6 +1183,8 @@ static int __trace_fprobe_create(int argc, const char *argv[]) } out: + if (tp_mod) + module_put(tp_mod); traceprobe_finish_parse(&ctx); trace_probe_log_clear(); kfree(new_argv); From patchwork Sat Jun 1 08:22:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 13682345 X-Patchwork-Delegate: mhiramat@kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1769811711; Sat, 1 Jun 2024 08:22:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717230179; cv=none; b=GRuZhhD8fQjWh3OYwxEw8Kp6gbgGfBxEmvtK6ZPzqBKTMQpoY80FrmMxNMLoGB47AGVO+4Q9VmR1HjGgAkazz17KRhJWepuUdRPqWI7wwhoHiM7XDjeu+C44WQNq+dgx3bDaOncKuYud2GG2TYgZvmXRuvJo39jipmi53PlhEmA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717230179; c=relaxed/simple; bh=ir1GPv/WVZeGeHO6Nly4BHiSZZOFn2vs7jsNY1Z2JXs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=rTIDbgukqSi4UnokVFjuOslOYnFT4l1ZiaiuCfJgk/qi/naBVnL1tK6Cvhv63HTA2FgMV6v+PROvoYcjkd6FzpQkbZ6ArVmRbVIJ7W2POJWTX6xej3bGd/AVnQ+WHKVYYShinH4Zib/vKq3Fm9D/4uWDThUkiFJDp2FA81mSm6g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JLkBfIXG; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JLkBfIXG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4E6C5C116B1; Sat, 1 Jun 2024 08:22:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717230179; bh=ir1GPv/WVZeGeHO6Nly4BHiSZZOFn2vs7jsNY1Z2JXs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JLkBfIXGZZFBMaImA+rmmP+F0KASs+OeR7q6vA4tFXZo7nUFBTfY7JDy240OCUv/0 pLIK5OqREwS4EwZtHzI0gxprdPLqLYE4/g6rI2lzFkTjA+Icl9xV1VkAghGLxABqZT v0phh+7Z9qugL5gMDGLLM5HGvPkoTv0uG7RczE9uMLb3pKcQY1nc6TdENGAU+1WMUK FZAlXfRZ35zmmtXbqR/cyqY5qzaWGJ72EWuW0jNery01qbW7ZTgcFudui509rCdTwa h4qDgNKAM8xdhOHJKKXYSpESOf3CpPF2CGigKtR4ZWXFJtxctJGU1P+g7qZnwW6qvN kVuGIXYaLndkA== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Mathieu Desnoyers , don , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, mhiramat@kernel.org Subject: [PATCH v2 3/3] sefltests/tracing: Add a test for tracepoint events on modules Date: Sat, 1 Jun 2024 17:22:55 +0900 Message-Id: <171723017495.258703.11572784928592807749.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <171723014778.258703.6731294779199848686.stgit@devnote2> References: <171723014778.258703.6731294779199848686.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Masami Hiramatsu (Google) Add a test case for tracepoint events on modules. This checks if it can add and remove the events correctly. Signed-off-by: Masami Hiramatsu (Google) Reviewed-by: Steven Rostedt (Google) --- tools/testing/selftests/ftrace/config | 1 + .../test.d/dynevent/add_remove_tprobe_module.tc | 34 ++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tools/testing/selftests/ftrace/test.d/dynevent/add_remove_tprobe_module.tc diff --git a/tools/testing/selftests/ftrace/config b/tools/testing/selftests/ftrace/config index 048a312abf40..544de0db5f58 100644 --- a/tools/testing/selftests/ftrace/config +++ b/tools/testing/selftests/ftrace/config @@ -20,6 +20,7 @@ CONFIG_PREEMPT_TRACER=y CONFIG_PROBE_EVENTS_BTF_ARGS=y CONFIG_SAMPLES=y CONFIG_SAMPLE_FTRACE_DIRECT=m +CONFIG_SAMPLE_TRACE_EVENTS=m CONFIG_SAMPLE_TRACE_PRINTK=m CONFIG_SCHED_TRACER=y CONFIG_STACK_TRACER=y diff --git a/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_tprobe_module.tc b/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_tprobe_module.tc new file mode 100644 index 000000000000..2caed9454caa --- /dev/null +++ b/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_tprobe_module.tc @@ -0,0 +1,34 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: Generic dynamic event - add/remove tracepoint probe events on module +# requires: dynamic_events "t[:[/][]] []":README + +rmmod trace-events-sample ||: +if ! modprobe trace-events-sample ; then + echo "No trace-events sample module - please make CONFIG_SAMPLE_TRACE_EVENTS=m" + exit_unresolved; +fi +trap "rmmod trace-events-sample" EXIT + +echo 0 > events/enable +echo > dynamic_events + +TRACEPOINT1=foo_bar +TRACEPOINT2=foo_bar_with_cond + +echo "t:myevent1 $TRACEPOINT1" >> dynamic_events +echo "t:myevent2 $TRACEPOINT2" >> dynamic_events + +grep -q myevent1 dynamic_events +grep -q myevent2 dynamic_events +test -d events/tracepoints/myevent1 +test -d events/tracepoints/myevent2 + +echo "-:myevent2" >> dynamic_events + +grep -q myevent1 dynamic_events +! grep -q myevent2 dynamic_events + +echo > dynamic_events + +clear_trace