From patchwork Sun Aug 18 10:42:58 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: 13767382 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 7BDB817BBF; Sun, 18 Aug 2024 10:43:02 +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=1723977782; cv=none; b=MUTTGvV/lNfImkHvYoZKv6drS/HIMCiTgW+ug9avUy2DDPlAvQ0ZnvfK+dnU1PvjfCyxTx2IggK02ZiJ7Iy65NnuQxoEI2UEradDXZeHp/7hNsOZkVbwuMh8qBAk4L9oG/DwCHeobd3CxnI6Epgwyn/bMynOSEsCwktUPdKlHpg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723977782; c=relaxed/simple; bh=K+YhhYrm6wZmaypIZoaE8lkjUEUnYcCHGJ03mCynMzU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=ei8Cx31us0hc2fULWFzHsmHNN3HRY/8fpit6b+b3drfGB2nx47yOqS5AuRwdmZooj9XB3+d1Ao9gcjmHxgI4K1jI1SkfrpProwQ45veEuar8+pxjG9OtNcMhNs5q/ptdoIAxvylYcgNXNFow/tI23EeZjp5AG+LPvRJe/se4/w8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NQdxoY6/; 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="NQdxoY6/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8906EC32786; Sun, 18 Aug 2024 10:43:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723977782; bh=K+YhhYrm6wZmaypIZoaE8lkjUEUnYcCHGJ03mCynMzU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NQdxoY6/5vQZDSFrmpKhLxatsRhhfz7n+9brTbyy4ipMUWGqmZ1IYaVHrSiHRfsKQ czzCv0PAZR9IvqrpUj1YrhNQIaoz3rq9V+MbwS7O2aS8luovAgH/yZGKO7pOLan8cw n1u26jXmO2ZKkA3hdC7L92msz02JUBPWVc0/1fPtY4nIBD3ksXSJAsETYERzZgSS49 8tSGZBXYf6b06iZxQ7nfAidbNCuVHJRf69xCHlD4rjIO5v2+uT428mX3EKgiI1S8JX 2ZJkQbenCNyLyENhvlrwyCBb4f2egxNw41s6YVUezHIWennlsJC13DpSIaQCCO8t3y WjzLBtkogTd4A== 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 v3 1/5] tracepoint: Support iterating over tracepoints on modules Date: Sun, 18 Aug 2024 19:42:58 +0900 Message-Id: <172397777800.286558.14554748203446214056.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <172397776900.286558.8986646398199362026.stgit@devnote2> References: <172397776900.286558.8986646398199362026.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) --- Changes in v3: - Add kerneldoc for for_each_module_tracepoint. --- include/linux/tracepoint.h | 7 +++++++ kernel/tracepoint.c | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/include/linux/tracepoint.h b/include/linux/tracepoint.h index 6be396bb4297..837fcf8ec0d5 100644 --- a/include/linux/tracepoint.h +++ b/include/linux/tracepoint.h @@ -64,6 +64,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) { @@ -79,6 +81,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..bed4aad36d92 100644 --- a/kernel/tracepoint.c +++ b/kernel/tracepoint.c @@ -735,6 +735,27 @@ static __init int init_tracepoints(void) return ret; } __initcall(init_tracepoints); + +/** + * for_each_module_tracepoint - iteration on all tracepoints in all modules + * @fct: callback + * @priv: private data + */ +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 Sun Aug 18 10:43:07 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: 13767383 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 ADF4621A1C; Sun, 18 Aug 2024 10:43:11 +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=1723977791; cv=none; b=gJN/nhilbVTC2Ia+W2sv5lXnsQci45GlTMNyzZ7c7T1ah0nl/a8VbJ/3PO6rjUHUu6GVgzPwsrSC6guQ1/QFB6akJOAttDzgNQzLbDhMQ90Ic5KYJ0j754XhhwAg/xfpjHs3i63kTYObxadoANkVQmfFMo8ewGOloid1w21MqW0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723977791; c=relaxed/simple; bh=EvknbMhGf6LvKrk+oRW09wGeSmyN1oC8gV5vpMCp9L8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=scb8Plp6qohLYsS272sz9Ow5IuOoXL0y4Bs2cT93OzsT941F0zCVnjNJKTFDa3fo+Yjix9lAUAe7rRwGsYQSmIe0TmvI/PjpMJSudHjAfOZTvJ3c5o1Gl2BR6peErAP8EYte1aO8g0jBrzZD9R2ezqpha/KQT2ksQvdm3JLrNhk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Obyezy+q; 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="Obyezy+q" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A44DCC32786; Sun, 18 Aug 2024 10:43:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723977791; bh=EvknbMhGf6LvKrk+oRW09wGeSmyN1oC8gV5vpMCp9L8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Obyezy+qK84TS1wPFdk1yAvIzxceEAIFd8xEwEPcYcjF1a1tHihr1Ncb89ycxqGRM X6OSdiNdsrs0mOlNKxgxDAAq9g0E4D3QjYTQnFW/1gVGNYbhBuD3TqeZsuub8T8W6X Muf9SbPPf8RyUqO5u+UGecifchyF3qCYLalMHY9tgnC7e8Sw3RI3RiEdDnA10BAM8e Rf3ibz9y5kMqGs3RYblYVMdNCPH+NvrzScm2yVBtisKsSFDKzSiQZeAu7WLw0D2JI7 +n7cOwW5TCmumDCSmtPhsZanJ9xlQcHaHK6Ckg+FpBR4xNmMSY/r6uQL/w+/YQhfoE UXPBlN+i7o6mA== 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 v3 2/5] tracepoint: Support tterating tracepoints in a loading module Date: Sun, 18 Aug 2024 19:43:07 +0900 Message-Id: <172397778740.286558.15781131277732977643.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <172397776900.286558.8986646398199362026.stgit@devnote2> References: <172397776900.286558.8986646398199362026.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_tracepoint_in_module() function to iterate tracepoints in a module. This API is needed for handling tracepoints in a loading module from tracepoint_module_notifier callback function. This also update for_each_module_tracepoint() to pass the module to callback function so that it can find module easily. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v3: - Newly added. --- include/linux/tracepoint.h | 17 +++++++++++++++-- kernel/tracepoint.c | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/include/linux/tracepoint.h b/include/linux/tracepoint.h index 837fcf8ec0d5..93a9f3070b48 100644 --- a/include/linux/tracepoint.h +++ b/include/linux/tracepoint.h @@ -64,8 +64,13 @@ 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 for_each_module_tracepoint(void (*fct)(struct tracepoint *, + struct module *, void *), void *priv); +void for_each_tracepoint_in_module(struct module *, + void (*fct)(struct tracepoint *, + struct module *, void *), + void *priv); #else static inline bool trace_module_has_bad_taint(struct module *mod) { @@ -82,10 +87,18 @@ int unregister_tracepoint_module_notifier(struct notifier_block *nb) return 0; } static inline -void for_each_module_tracepoint(void (*fct)(struct tracepoint *, void *), +void for_each_module_tracepoint(void (*fct)(struct tracepoint *, + struct module *, void *), void *priv) { } +static inline +void for_each_tracepoint_in_module(struct module *mod, + void (*fct)(struct tracepoint *, + struct module *, void *), + void *priv) +{ +} #endif /* CONFIG_MODULES */ /* diff --git a/kernel/tracepoint.c b/kernel/tracepoint.c index bed4aad36d92..8879da16ef4d 100644 --- a/kernel/tracepoint.c +++ b/kernel/tracepoint.c @@ -736,24 +736,45 @@ static __init int init_tracepoints(void) } __initcall(init_tracepoints); +/** + * for_each_tracepoint_in_module - iteration on all tracepoints in a module + * @mod: module + * @fct: callback + * @priv: private data + */ +void for_each_tracepoint_in_module(struct module *mod, + void (*fct)(struct tracepoint *tp, + struct module *mod, void *priv), + void *priv) +{ + tracepoint_ptr_t *begin, *end, *iter; + + lockdep_assert_held(&tracepoint_module_list_mutex); + + if (!mod) + return; + + begin = mod->tracepoints_ptrs; + end = mod->tracepoints_ptrs + mod->num_tracepoints; + + for (iter = begin; iter < end; iter++) + fct(tracepoint_ptr_deref(iter), mod, priv); +} + /** * for_each_module_tracepoint - iteration on all tracepoints in all modules * @fct: callback * @priv: private data */ -void for_each_module_tracepoint(void (*fct)(struct tracepoint *tp, void *priv), +void for_each_module_tracepoint(void (*fct)(struct tracepoint *tp, + struct module *mod, 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); - } + list_for_each_entry(tp_mod, &tracepoint_module_list, list) + for_each_tracepoint_in_module(tp_mod->mod, fct, priv); mutex_unlock(&tracepoint_module_list_mutex); } #endif /* CONFIG_MODULES */ From patchwork Sun Aug 18 10:43:16 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: 13767384 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 E0436182B2; Sun, 18 Aug 2024 10:43:20 +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=1723977801; cv=none; b=cQSVcZyb6fzzlA5smYCVY3cUFM/RDBZaNNkgAfGKNkarujGf6MmhylqXarAY9xp4lF0lAmWfO++HR2bj4w5DEWEhCgSEG2C/esUtpxuAXWhNnU4b3axXH585SssTTOmOphxphcv+ywgkXBU5imM7LAg+BMCZIxWRHjAXsFDbXqY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723977801; c=relaxed/simple; bh=tqxj0+ceBL7w+7fMBUuPblOkbw1rKdQ8Tw3Bs0li64E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=b8kPqeGANfHoHkaAttbJhsrZSfSHSHUVF4gs033EhxaPWuYJsl8B++jOCyFXhFb7DK90Ch823v1Z3AlHBs6dOPZZogw5B9DEsCb++lAsLYy8OxNNi/RoeiRLitsjpwem5Bee2KcytMqufBoLjCoeQTTenfTF2132BT8hfR8EKGc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bkE2TUpz; 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="bkE2TUpz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id F23CCC32786; Sun, 18 Aug 2024 10:43:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723977800; bh=tqxj0+ceBL7w+7fMBUuPblOkbw1rKdQ8Tw3Bs0li64E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bkE2TUpzRcENCRM20CHpda9h4l7IIqBZBe5fQPxXSvNrrRvLWzMXoooDHZOsoHwvU pmj0hAb/+hg8wtYo40uiM2KY50/RBb+D/sntm/Ya+dT5KFeTarnz2AJk0OIoCYTfyr YrFuVEralMkAi1CPN6nXETJ2yenJcaIePGBXUmeg+22Tt0X4QGq6FeZRib1nr93DNg dN57BQXd6PHPDN3VTh0ikP/cUFcvF4S8CwMWi4dQATSENxpkpydUpZDuaEloySKn9n fcCOFuyyo+KIoIUZoDMX7aKRf0pNXBEfACsOGxlFPdJr0wVVnGFsaYX1NE9LzQI7YH ZAVHkK0bvNFBg== 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 v3 3/5] tracing/fprobe: Support raw tracepoint events on modules Date: Sun, 18 Aug 2024 19:43:16 +0900 Message-Id: <172397779651.286558.15903703620679186867.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <172397776900.286558.8986646398199362026.stgit@devnote2> References: <172397776900.286558.8986646398199362026.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..8b1127e37da5 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, struct module *mod, void *priv) +{ + struct __find_tracepoint_cb_data *data = priv; + + if (!data->tpoint && !strcmp(data->tp_name, tp->name)) { + data->tpoint = tp; + data->mod = mod; + 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 Sun Aug 18 10:43:26 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: 13767385 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 BC073381D9; Sun, 18 Aug 2024 10:43:29 +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=1723977809; cv=none; b=jl55fMHw0W0Y085rCj91HwC0uE2B59q8SluJAx09XuUf5IktARi07aKjju7gGiskdUU1xzsrNL5+TKlRd7jcBQq1lHRFXledSEX6Z3lnUPjdXf2iFfMVdAeAncH6r04D4FWLT5vg1N9lxxKvD3D867y/s4PWhr1Qi/MEAMQ7KuI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723977809; c=relaxed/simple; bh=MZOaid6cAqLQgv+JhjDgg9phuooFUeOYA88rwQdbU0k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=iwJ0Ad6jfICwbxfP0DG33M+G6PiUJFS3C0U/Y7dJQq2aKeASTMJul5s9Y8eC/J2dxr46x6m3ZvK+GDqaDH1LE1Xq8D2teAoXwxk2W9S7QVtvTekPXiRnRt0Q2RuQOysoCmNXmyvMHPp26Fi5do7iYxrAMxTSrcwf60iMNlYPqjo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eVyHLwGb; 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="eVyHLwGb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1A7B1C32786; Sun, 18 Aug 2024 10:43:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723977809; bh=MZOaid6cAqLQgv+JhjDgg9phuooFUeOYA88rwQdbU0k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eVyHLwGbuLe+x6UnSlfQgoF+DND7CeNHECP1XrQ69UN17zdyJwoZno3vjW8PiLni2 syC6cN44hlPHLDloumNdADw1t21bDV2f8UrLio5Hsds6AVrIMaOyamv3mxu0g7RJFX DZ9SUJTyu1C2Q2yjAHTrIH2DvzQk0Dj3Ueg/qQN8tQ/qqfMkLmxYRzqrHKVjcmylJb VEtHPk4ofNOuoFWABqIzQ1n1dASz1lcbHbSFStG1PmHIbCbnUPYrl3Ssr0VAWuLKfv HxGxcjf5yrkboVTZVe2hw41YjherhOIdULbTaYcJstjYalCR1j1ycVfJLuZwAPfSHX 48yQsELCKDwdg== 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 v3 4/5] tracing/fprobe: Support raw tracepoints on future loaded modules Date: Sun, 18 Aug 2024 19:43:26 +0900 Message-Id: <172397780593.286558.18360375226968537828.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <172397776900.286558.8986646398199362026.stgit@devnote2> References: <172397776900.286558.8986646398199362026.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 events on future loaded (unloaded) modules. This allows user to create raw tracepoint events which can be used from module's __init functions. Note: since the kernel does not have any information about the tracepoints in the unloaded modules, fprobe events can not check whether the tracepoint exists nor extend the BTF based arguments. Suggested-by: Mathieu Desnoyers Signed-off-by: Masami Hiramatsu (Google) --- Changes in v3: - Newly added. --- kernel/trace/trace_fprobe.c | 151 +++++++++++++------- .../ftrace/test.d/dynevent/tprobe_syntax_errors.tc | 1 2 files changed, 101 insertions(+), 51 deletions(-) diff --git a/kernel/trace/trace_fprobe.c b/kernel/trace/trace_fprobe.c index 8b1127e37da5..a079abd8955b 100644 --- a/kernel/trace/trace_fprobe.c +++ b/kernel/trace/trace_fprobe.c @@ -21,6 +21,7 @@ #define FPROBE_EVENT_SYSTEM "fprobes" #define TRACEPOINT_EVENT_SYSTEM "tracepoints" #define RETHOOK_MAXACTIVE_MAX 4096 +#define TRACEPOINT_STUB ERR_PTR(-ENOENT) static int trace_fprobe_create(const char *raw_command); static int trace_fprobe_show(struct seq_file *m, struct dyn_event *ev); @@ -674,6 +675,24 @@ static int unregister_fprobe_event(struct trace_fprobe *tf) return trace_probe_unregister_event_call(&tf->tp); } +static int __regsiter_tracepoint_fprobe(struct trace_fprobe *tf) +{ + struct tracepoint *tpoint = tf->tpoint; + unsigned long ip = (unsigned long)tpoint->probestub; + int ret; + + /* + * Here, we do 2 steps to enable fprobe on a tracepoint. + * At first, put __probestub_##TP function on the tracepoint + * and put a fprobe on the stub function. + */ + ret = tracepoint_probe_register_prio_may_exist(tpoint, + tpoint->probestub, NULL, 0); + if (ret < 0) + return ret; + return register_fprobe_ips(&tf->fp, &ip, 1); +} + /* Internal register function - just handle fprobe and flags */ static int __register_trace_fprobe(struct trace_fprobe *tf) { @@ -700,18 +719,12 @@ static int __register_trace_fprobe(struct trace_fprobe *tf) tf->fp.flags |= FPROBE_FL_DISABLED; if (trace_fprobe_is_tracepoint(tf)) { - struct tracepoint *tpoint = tf->tpoint; - unsigned long ip = (unsigned long)tpoint->probestub; - /* - * Here, we do 2 steps to enable fprobe on a tracepoint. - * At first, put __probestub_##TP function on the tracepoint - * and put a fprobe on the stub function. - */ - ret = tracepoint_probe_register_prio_may_exist(tpoint, - tpoint->probestub, NULL, 0); - if (ret < 0) - return ret; - return register_fprobe_ips(&tf->fp, &ip, 1); + + /* This tracepoint is not loaded yet */ + if (tf->tpoint == TRACEPOINT_STUB) + return 0; + + return __regsiter_tracepoint_fprobe(tf); } /* TODO: handle filter, nofilter or symbol list */ @@ -864,36 +877,6 @@ static int register_trace_fprobe(struct trace_fprobe *tf) return ret; } -#ifdef CONFIG_MODULES -static int __tracepoint_probe_module_cb(struct notifier_block *self, - unsigned long val, void *data) -{ - struct tp_module *tp_mod = data; - struct trace_fprobe *tf; - struct dyn_event *pos; - - if (val != MODULE_STATE_GOING) - return NOTIFY_DONE; - - mutex_lock(&event_mutex); - for_each_trace_fprobe(tf, pos) { - if (tp_mod->mod == tf->mod) { - tracepoint_probe_unregister(tf->tpoint, - tf->tpoint->probestub, NULL); - tf->tpoint = NULL; - tf->mod = NULL; - } - } - mutex_unlock(&event_mutex); - - return NOTIFY_DONE; -} - -static struct notifier_block tracepoint_module_nb = { - .notifier_call = __tracepoint_probe_module_cb, -}; -#endif /* CONFIG_MODULES */ - struct __find_tracepoint_cb_data { const char *tp_name; struct tracepoint *tpoint; @@ -906,10 +889,12 @@ static void __find_tracepoint_module_cb(struct tracepoint *tp, struct module *mo if (!data->tpoint && !strcmp(data->tp_name, tp->name)) { data->tpoint = tp; - data->mod = mod; - if (!try_module_get(data->mod)) { - data->tpoint = NULL; - data->mod = NULL; + if (!data->mod) { + data->mod = mod; + if (!try_module_get(data->mod)) { + data->tpoint = NULL; + data->mod = NULL; + } } } } @@ -947,6 +932,67 @@ static struct tracepoint *find_tracepoint(const char *tp_name, return data.tpoint; } +#ifdef CONFIG_MODULES +static void reenable_trace_fprobe(struct trace_fprobe *tf) +{ + struct trace_probe *tp = &tf->tp; + + list_for_each_entry(tf, trace_probe_probe_list(tp), tp.list) { + __enable_trace_fprobe(tf); + } +} + +static struct tracepoint *find_tracepoint_in_module(struct module *mod, + const char *tp_name) +{ + struct __find_tracepoint_cb_data data = { + .tp_name = tp_name, + .mod = mod, + }; + + for_each_tracepoint_in_module(mod, __find_tracepoint_module_cb, &data); + return data.tpoint; +} + +static int __tracepoint_probe_module_cb(struct notifier_block *self, + unsigned long val, void *data) +{ + struct tp_module *tp_mod = data; + struct tracepoint *tpoint; + struct trace_fprobe *tf; + struct dyn_event *pos; + + if (val != MODULE_STATE_GOING && val != MODULE_STATE_COMING) + return NOTIFY_DONE; + + mutex_lock(&event_mutex); + for_each_trace_fprobe(tf, pos) { + if (val == MODULE_STATE_COMING && tf->tpoint == TRACEPOINT_STUB) { + tpoint = find_tracepoint_in_module(tp_mod->mod, tf->symbol); + if (tpoint) { + tf->tpoint = tpoint; + tf->mod = tp_mod->mod; + if (!WARN_ON_ONCE(__regsiter_tracepoint_fprobe(tf)) && + trace_probe_is_enabled(&tf->tp)) + reenable_trace_fprobe(tf); + } + } else if (val == MODULE_STATE_GOING && tp_mod->mod == tf->mod) { + tracepoint_probe_unregister(tf->tpoint, + tf->tpoint->probestub, NULL); + tf->tpoint = NULL; + tf->mod = NULL; + } + } + mutex_unlock(&event_mutex); + + return NOTIFY_DONE; +} + +static struct notifier_block tracepoint_module_nb = { + .notifier_call = __tracepoint_probe_module_cb, +}; +#endif /* CONFIG_MODULES */ + static int parse_symbol_and_return(int argc, const char *argv[], char **symbol, bool *is_return, bool is_tracepoint) @@ -1113,14 +1159,19 @@ static int __trace_fprobe_create(int argc, const char *argv[]) if (is_tracepoint) { ctx.flags |= TPARG_FL_TPOINT; tpoint = find_tracepoint(symbol, &tp_mod); - if (!tpoint) { + if (tpoint) { + ctx.funcname = kallsyms_lookup( + (unsigned long)tpoint->probestub, + NULL, NULL, NULL, sbuf); + } else if (IS_ENABLED(CONFIG_MODULES)) { + /* This *may* be loaded afterwards */ + tpoint = TRACEPOINT_STUB; + ctx.funcname = symbol; + } else { trace_probe_log_set_index(1); trace_probe_log_err(0, NO_TRACEPOINT); goto parse_error; } - ctx.funcname = kallsyms_lookup( - (unsigned long)tpoint->probestub, - NULL, NULL, NULL, sbuf); } else ctx.funcname = symbol; diff --git a/tools/testing/selftests/ftrace/test.d/dynevent/tprobe_syntax_errors.tc b/tools/testing/selftests/ftrace/test.d/dynevent/tprobe_syntax_errors.tc index da117b8f1d12..ffe8ffef4027 100644 --- a/tools/testing/selftests/ftrace/test.d/dynevent/tprobe_syntax_errors.tc +++ b/tools/testing/selftests/ftrace/test.d/dynevent/tprobe_syntax_errors.tc @@ -9,7 +9,6 @@ check_error() { # command-with-error-pos-by-^ check_error 't^100 kfree' # BAD_MAXACT_TYPE -check_error 't ^non_exist_tracepoint' # NO_TRACEPOINT check_error 't:^/bar kfree' # NO_GROUP_NAME check_error 't:^12345678901234567890123456789012345678901234567890123456789012345/bar kfree' # GROUP_TOO_LONG From patchwork Sun Aug 18 10:43:35 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: 13767386 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 CB29238384; Sun, 18 Aug 2024 10:43:38 +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=1723977818; cv=none; b=jwp6ZY6A7Kj6DUYI7DuKAfot1aSLmY25ymaUaI31vcZ0XxZjC6uXUD3uxOPd+xAvfkA0eUayMateD2eUDP87CMWh94vAE8AixlHpdE8lhaSvto8wL0fyE2B61wws+7RIETvtKbjzapi/j+Z9TEp/Q8kfQiD+bNMaEsgVWSC2IQY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723977818; c=relaxed/simple; bh=rCdP5URlZYm+NPBg5WLaSak0xP7BHvZX5yXjGEZvYzE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=CH2a/eiu6c2BlmAxLTZiYxlwCSQFY036aaPXA8khCjWnunf6j67w3A4f8h6muohKdn2qlJWpusgrY2gjTOW3IL7NkdKNI5vA+soLOmxfPE3Bbj6/+oQCPjFaf3Uj0ulcd1jx43O9jEq3Ev1fFNEnucu7q30jhG+CXSMu9UC1Qhs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rB8D/Rah; 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="rB8D/Rah" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 121BAC32786; Sun, 18 Aug 2024 10:43:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723977818; bh=rCdP5URlZYm+NPBg5WLaSak0xP7BHvZX5yXjGEZvYzE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rB8D/RahpCQQCg6W6bLp6TlMu846obvdQ29lzrB45XX3KQZM2815ExD0G6EK5sHsD aJIBwjYCcnk9TNtKGM0nIho4gUxX4FczOMVt7DvPxq0YyKat4U6ntIsgSb5usWKc39 xvSYLWaesVl6a+w/Ecxj3ZvUsV2TN4ZNiM6Rx8WoxBbYbMYGX6fNPl6mZb4P1QTY7l EqncrjPsY1yNk1dotjSd4wc66EPgftsZ75QEV4W30L7Zin+wVr8QjvW8v12V4Sz/gf Tq/8FaJsuRyMio7gGxbpTTPe45MwdPDWdmMZ4tMqgEcR6+IFLA06kazM85gBZ6Nkg0 rX1mxoPj3xbaw== 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 v3 5/5] sefltests/tracing: Add a test for tracepoint events on modules Date: Sun, 18 Aug 2024 19:43:35 +0900 Message-Id: <172397781494.286558.7581515061075998225.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <172397776900.286558.8986646398199362026.stgit@devnote2> References: <172397776900.286558.8986646398199362026.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) --- Changes in v3: - Add not-loaded module test. --- tools/testing/selftests/ftrace/config | 1 .../test.d/dynevent/add_remove_tprobe_module.tc | 61 ++++++++++++++++++++ 2 files changed, 62 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..d319d5ed4226 --- /dev/null +++ b/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_tprobe_module.tc @@ -0,0 +1,61 @@ +#!/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 + +:;: "Try to put a probe on a tracepoint in non-loaded module" ;: +rmmod trace-events-sample + +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 1 > events/tracepoints/enable + +modprobe trace-events-sample + +sleep 2 + +grep -q "myevent1" trace +grep -q "myevent2" trace + +rmmod trace-events-sample +trap "" EXIT + +echo 0 > events/tracepoints/enable +echo > dynamic_events +clear_trace