From nobody Wed Dec 17 17:26:11 2025 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 0390318B47C; Sun, 16 Mar 2025 12:21:27 +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=1742127688; cv=none; b=s7mSPpv0xGtxRIx/wvo7YRXu1TeES3kA76v8Qb3eQJBOjhwj7j9T2KOGBC/50knfh7dKwIp2RY5Z+FMa415XHJCrssrAojajwWqd35zJ9wvSqt+LQO/wnbxZEsG1uNG6GgdSHRFTOywZmqM6Hv8AYcj1ZRqluQsOTRZjId5BVGQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742127688; c=relaxed/simple; bh=gjmV4gbAkAhF5mQ6J/09/rRmSzsdFAZegE7iBok4gYk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=MYTr3Qr6RRP6qy+14Swvr42TODnu6WwCEs6oPoWl6r7eTGQrSGeCXZBBXA+6io2ae9YG7IiWhUiP6jZM+LOPJD2WfWlJ/Z/HEiR7bST4AfI9LkHV6wIUjJ4O7ibRuhYqes94wSltzHleb6Q1vm1naBReXWu0aosrqy+ivIeGCXY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OoC+FfGX; 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="OoC+FfGX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0D4E4C4CEDD; Sun, 16 Mar 2025 12:21:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742127687; bh=gjmV4gbAkAhF5mQ6J/09/rRmSzsdFAZegE7iBok4gYk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OoC+FfGXp+HTSN2a6ctff0Zp27I1tB0ukHqVXNPo6CZD0UjEE5GU9iiCAirrN9mXR 0X88AzygOEx60oRAemOzkc7dXVUh5BXj+REySR8UFPVXqbn7K2ylXKN/O2vnwAPVSr Mx0tWnUnJAkz5yHX24m1HNDaeYUWEUIw08cTz7p4yJCy7SFaf3JFgC/X/M2uiCaTCN ElGe3HEORFCpmYPWDMJuuduKS47Nv2WG62LEChg49VlQert878r3+eO5Knqg1VnCK9 wglqVU2+LIspduzOa6vwvKU7zvTHmLBcZbWvcYZ7uwJmtVph96DxLcigQ+1da2UYYL /J6cj+uutWcBQ== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH 1/4] tracing: tprobe-events: Remove mod field from tprobe-event Date: Sun, 16 Mar 2025 21:21:21 +0900 Message-ID: <174212768121.348872.11676145343580139357.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <174212767109.348872.18231451508464729427.stgit@devnote2> References: <174212767109.348872.18231451508464729427.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Remove unneeded 'mod' struct module pointer field from trace_fprobe because we don't need to save this info. Signed-off-by: Masami Hiramatsu (Google) --- kernel/trace/trace_fprobe.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/kernel/trace/trace_fprobe.c b/kernel/trace/trace_fprobe.c index 5d7ca80173ea..08def94f9ca6 100644 --- a/kernel/trace/trace_fprobe.c +++ b/kernel/trace/trace_fprobe.c @@ -46,7 +46,6 @@ struct trace_fprobe { struct fprobe fp; const char *symbol; struct tracepoint *tpoint; - struct module *mod; struct trace_probe tp; }; =20 @@ -426,7 +425,6 @@ static struct trace_fprobe *alloc_trace_fprobe(const ch= ar *group, const char *event, const char *symbol, struct tracepoint *tpoint, - struct module *mod, int nargs, bool is_return) { struct trace_fprobe *tf __free(free_trace_fprobe) =3D NULL; @@ -446,7 +444,6 @@ static struct trace_fprobe *alloc_trace_fprobe(const ch= ar *group, tf->fp.entry_handler =3D fentry_dispatcher; =20 tf->tpoint =3D tpoint; - tf->mod =3D mod; =20 ret =3D trace_probe_init(&tf->tp, event, group, false, nargs); if (ret < 0) @@ -776,7 +773,6 @@ static void __unregister_trace_fprobe(struct trace_fpro= be *tf) tracepoint_probe_unregister(tf->tpoint, tf->tpoint->probestub, NULL); tf->tpoint =3D NULL; - tf->mod =3D NULL; } } } @@ -992,7 +988,6 @@ static int __tracepoint_probe_module_cb(struct notifier= _block *self, tpoint =3D find_tracepoint_in_module(tp_mod->mod, tf->symbol); if (tpoint) { tf->tpoint =3D tpoint; - tf->mod =3D tp_mod->mod; if (!WARN_ON_ONCE(__regsiter_tracepoint_fprobe(tf)) && trace_probe_is_enabled(&tf->tp)) reenable_trace_fprobe(tf); @@ -1003,7 +998,6 @@ static int __tracepoint_probe_module_cb(struct notifie= r_block *self, tracepoint_probe_unregister(tf->tpoint, tf->tpoint->probestub, NULL); tf->tpoint =3D TRACEPOINT_STUB; - tf->mod =3D NULL; } } } @@ -1210,8 +1204,7 @@ static int trace_fprobe_create_internal(int argc, con= st char *argv[], return ret; =20 /* setup a probe */ - tf =3D alloc_trace_fprobe(group, event, symbol, tpoint, tp_mod, - argc, is_return); + tf =3D alloc_trace_fprobe(group, event, symbol, tpoint, argc, is_return); if (IS_ERR(tf)) { ret =3D PTR_ERR(tf); /* This must return -ENOMEM, else there is a bug */ From nobody Wed Dec 17 17:26:11 2025 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 8872518B47C; Sun, 16 Mar 2025 12:21:37 +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=1742127697; cv=none; b=HEMIuDESFUyMQdThPfrpjhZ5shbiV+9Cb/s+qEtj6gq0M/pgyl+QJ1xb9xfXXwgx57cWVBHw3VdBIqDoT0PZJ3W5xtE1Zdf0YcRMno/ceOZ4XyVC4kKwvB+q/lkNStSFOX25W0L0l5Do/Bj3HCxQUHUNg8ta1ljVce705TCgilk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742127697; c=relaxed/simple; bh=lp4YDB/hAqNvIoicxsUzWvZXZHDX4oGJ7AOP3B9m0Nw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=juwRL8iOjpad7929F7SreYUm6/jm+NsXbAiXiLh0jnOD7O/iFBVB5t8bUMV9I3WyM3mVLAkQiGh5TgPjoNnk/B5gzFH0usYu7Zpxa9ZDwdGUUpQdZ2F9EowI10ImjQNHdXbUuytawJhfINmGdpvD7mYoq+WfSnnU/c3d0c/ZYfo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=f0j/3HMD; 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="f0j/3HMD" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E160BC4CEDD; Sun, 16 Mar 2025 12:21:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742127697; bh=lp4YDB/hAqNvIoicxsUzWvZXZHDX4oGJ7AOP3B9m0Nw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f0j/3HMDdipVXotHjAVEtEspUV6zGkr5JasnS5aOCNiS8UicSBIsyxUhStx/ra9/E hFPuxRfCzIhE9jCdHMcBk9AvFJDFvaLUTJddTH2AsXjFViXaKc2Tf2k4txXdOMlhhO D8kA6R6iZp2U8vIuzkUdeB6mKqrjmyqdHQU1yloHwHYkjHrs2wPN3NOyVx0Nlr7BhY AHqRkzA9x+xZLyIbtqyK/j/O2ryvC2WyCxuwwKSO6WH4reghWkUyNoOhTy8lvfji82 ml6XzFmj3DAIh1/qX3PjDTYV8pcEgSsZ/JDKOdLdhGdYkXc8dAc+iQYjryT1Hjul/q mXNFGaJLDjsKQ== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH 2/4] tracing: tprobe-events: Support multiple tprobes on the same tracepoint Date: Sun, 16 Mar 2025 21:21:33 +0900 Message-ID: <174212769289.348872.12945848497242748827.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <174212767109.348872.18231451508464729427.stgit@devnote2> References: <174212767109.348872.18231451508464729427.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Allow user to set multiple tracepoint-probe events on the same tracepoint. After the last tprobe-event is removed, the tracepoint callback is unregistered. Signed-off-by: Masami Hiramatsu (Google) --- include/linux/module.h | 4 + kernel/trace/trace_fprobe.c | 259 ++++++++++++++++++++++++++++++++++-----= ---- 2 files changed, 208 insertions(+), 55 deletions(-) diff --git a/include/linux/module.h b/include/linux/module.h index 30e5b19bafa9..01d5208cf473 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -1027,4 +1028,7 @@ static inline unsigned long find_kallsyms_symbol_valu= e(struct module *mod, =20 #endif /* CONFIG_MODULES && CONFIG_KALLSYMS */ =20 +/* Define __free(module_put) macro for struct module *. */ +DEFINE_FREE(module_put, struct module *, if (_T) module_put(_T)) + #endif /* _LINUX_MODULE_H */ diff --git a/kernel/trace/trace_fprobe.c b/kernel/trace/trace_fprobe.c index 08def94f9ca6..863c9343a939 100644 --- a/kernel/trace/trace_fprobe.c +++ b/kernel/trace/trace_fprobe.c @@ -21,7 +21,6 @@ #define FPROBE_EVENT_SYSTEM "fprobes" #define TRACEPOINT_EVENT_SYSTEM "tracepoints" #define RETHOOK_MAXACTIVE_MAX 4096 -#define TRACEPOINT_STUB ERR_PTR(-ENOENT) =20 static int trace_fprobe_create(const char *raw_command); static int trace_fprobe_show(struct seq_file *m, struct dyn_event *ev); @@ -38,6 +37,89 @@ static struct dyn_event_operations trace_fprobe_ops =3D { .match =3D trace_fprobe_match, }; =20 +struct tracepoint_user { + struct tracepoint *tpoint; + unsigned int refcount; +}; + +static bool tracepoint_user_is_registered(struct tracepoint_user *tuser) +{ + return tuser && tuser->tpoint; +} + +static int tracepoint_user_register(struct tracepoint_user *tuser) +{ + struct tracepoint *tpoint =3D tuser->tpoint; + + if (!tpoint) + return 0; + + return tracepoint_probe_register_prio_may_exist(tpoint, + tpoint->probestub, NULL, 0); +} + +static void tracepoint_user_unregister(struct tracepoint_user *tuser) +{ + if (!tuser->tpoint) + return; + + WARN_ON_ONCE(tracepoint_probe_unregister(tuser->tpoint, tuser->tpoint->pr= obestub, NULL)); + tuser->tpoint =3D NULL; +} + +static unsigned long tracepoint_user_ip(struct tracepoint_user *tuser) +{ + if (!tuser->tpoint) + return 0UL; + + return (unsigned long)tuser->tpoint->probestub; +} + +static bool tracepoint_user_within_module(struct tracepoint_user *tuser, + struct module *mod) +{ + return within_module(tracepoint_user_ip(tuser), mod); +} + +static struct tracepoint_user *tracepoint_user_allocate(struct tracepoint = *tpoint) +{ + struct tracepoint_user *tuser __free(kfree) =3D NULL; + + tuser =3D kzalloc(sizeof(*tuser), GFP_KERNEL); + if (!tuser) + return NULL; + tuser->tpoint =3D tpoint; + tuser->refcount =3D 1; + + return_ptr(tuser); +} + +/* These must be called with event_mutex */ +static void tracepoint_user_get(struct tracepoint_user *tuser) +{ + tuser->refcount++; +} + +static void tracepoint_user_put(struct tracepoint_user *tuser) +{ + if (--tuser->refcount > 0) + return; + + if (tracepoint_user_is_registered(tuser)) + tracepoint_user_unregister(tuser); + kfree(tuser); +} + +static const char *tracepoint_user_lookup(struct tracepoint_user *tuser, c= har *buf) +{ + struct tracepoint *tpoint =3D tuser->tpoint; + + if (!tpoint) + return NULL; + + return kallsyms_lookup((unsigned long)tpoint->probestub, NULL, NULL, NULL= , buf); +} + /* * Fprobe event core functions */ @@ -45,7 +127,7 @@ struct trace_fprobe { struct dyn_event devent; struct fprobe fp; const char *symbol; - struct tracepoint *tpoint; + struct tracepoint_user *tuser; struct trace_probe tp; }; =20 @@ -75,7 +157,7 @@ static bool trace_fprobe_is_return(struct trace_fprobe *= tf) =20 static bool trace_fprobe_is_tracepoint(struct trace_fprobe *tf) { - return tf->tpoint !=3D NULL; + return tf->tuser !=3D NULL; } =20 static const char *trace_fprobe_symbol(struct trace_fprobe *tf) @@ -125,6 +207,57 @@ static bool trace_fprobe_is_registered(struct trace_fp= robe *tf) return fprobe_is_registered(&tf->fp); } =20 +static struct tracepoint *find_tracepoint(const char *tp_name, + struct module **tp_mod); + +/* + * Get tracepoint_user if exist, or allocate new one. If tracepoint is on a + * module, get its refcounter. + */ +static struct tracepoint_user * +trace_fprobe_get_tracepoint_user(const char *name, struct module **pmod) +{ + struct tracepoint_user *tuser __free(kfree) =3D NULL; + struct tracepoint *tpoint; + struct trace_fprobe *tf; + struct dyn_event *dpos; + struct module *mod __free(module_put) =3D NULL; + int ret; + + tpoint =3D find_tracepoint(name, &mod); + if (mod && !try_module_get(mod)) { + mod =3D NULL; + tpoint =3D NULL; + } + /* tpoint can be NULL, but we don't care here. */ + + /* Search existing tracepoint_user */ + for_each_trace_fprobe(tf, dpos) { + if (!trace_fprobe_is_tracepoint(tf)) + continue; + if (!strcmp(tf->symbol, name)) { + tracepoint_user_get(tf->tuser); + *pmod =3D no_free_ptr(mod); + return tf->tuser; + } + } + + /* Not found, allocate and register new tracepoint_user. */ + tuser =3D tracepoint_user_allocate(tpoint); + if (!tuser) + return NULL; + + if (tpoint) { + /* If the tracepoint is not loaded, tpoint can be NULL. */ + ret =3D tracepoint_user_register(tuser); + if (ret) + return ERR_PTR(ret); + } + + *pmod =3D no_free_ptr(mod); + return_ptr(tuser); +} + /* * Note that we don't verify the fetch_insn code, since it does not come * from user space. @@ -410,6 +543,8 @@ static void free_trace_fprobe(struct trace_fprobe *tf) { if (tf) { trace_probe_cleanup(&tf->tp); + if (tf->tuser) + tracepoint_user_put(tf->tuser); kfree(tf->symbol); kfree(tf); } @@ -424,7 +559,7 @@ DEFINE_FREE(free_trace_fprobe, struct trace_fprobe *, i= f (!IS_ERR_OR_NULL(_T)) f static struct trace_fprobe *alloc_trace_fprobe(const char *group, const char *event, const char *symbol, - struct tracepoint *tpoint, + struct tracepoint_user *tuser, int nargs, bool is_return) { struct trace_fprobe *tf __free(free_trace_fprobe) =3D NULL; @@ -443,7 +578,7 @@ static struct trace_fprobe *alloc_trace_fprobe(const ch= ar *group, else tf->fp.entry_handler =3D fentry_dispatcher; =20 - tf->tpoint =3D tpoint; + tf->tuser =3D tuser; =20 ret =3D trace_probe_init(&tf->tp, event, group, false, nargs); if (ret < 0) @@ -709,19 +844,11 @@ static int unregister_fprobe_event(struct trace_fprob= e *tf) =20 static int __regsiter_tracepoint_fprobe(struct trace_fprobe *tf) { - struct tracepoint *tpoint =3D tf->tpoint; - unsigned long ip =3D (unsigned long)tpoint->probestub; - int ret; + unsigned long ip =3D tracepoint_user_ip(tf->tuser); + + if (!ip) + return -ENOENT; =20 - /* - * 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 =3D tracepoint_probe_register_prio_may_exist(tpoint, - tpoint->probestub, NULL, 0); - if (ret < 0) - return ret; return register_fprobe_ips(&tf->fp, &ip, 1); } =20 @@ -753,7 +880,7 @@ static int __register_trace_fprobe(struct trace_fprobe = *tf) if (trace_fprobe_is_tracepoint(tf)) { =20 /* This tracepoint is not loaded yet */ - if (tf->tpoint =3D=3D TRACEPOINT_STUB) + if (!tracepoint_user_is_registered(tf->tuser)) return 0; =20 return __regsiter_tracepoint_fprobe(tf); @@ -770,9 +897,8 @@ static void __unregister_trace_fprobe(struct trace_fpro= be *tf) unregister_fprobe(&tf->fp); memset(&tf->fp, 0, sizeof(tf->fp)); if (trace_fprobe_is_tracepoint(tf)) { - tracepoint_probe_unregister(tf->tpoint, - tf->tpoint->probestub, NULL); - tf->tpoint =3D NULL; + tracepoint_user_put(tf->tuser); + tf->tuser =3D NULL; } } } @@ -975,7 +1101,7 @@ static int __tracepoint_probe_module_cb(struct notifie= r_block *self, unsigned long val, void *data) { struct tp_module *tp_mod =3D data; - struct tracepoint *tpoint; + struct tracepoint_user *tuser; struct trace_fprobe *tf; struct dyn_event *pos; =20 @@ -984,21 +1110,48 @@ static int __tracepoint_probe_module_cb(struct notif= ier_block *self, =20 mutex_lock(&event_mutex); for_each_trace_fprobe(tf, pos) { - if (val =3D=3D MODULE_STATE_COMING && tf->tpoint =3D=3D TRACEPOINT_STUB)= { + if (!trace_fprobe_is_tracepoint(tf)) + continue; + + if (val =3D=3D MODULE_STATE_COMING) { + /* + * If any tracepoint used by tprobe is in the module, + * register the stub. + */ + struct tracepoint *tpoint; + tpoint =3D find_tracepoint_in_module(tp_mod->mod, tf->symbol); - if (tpoint) { - tf->tpoint =3D tpoint; - if (!WARN_ON_ONCE(__regsiter_tracepoint_fprobe(tf)) && - trace_probe_is_enabled(&tf->tp)) - reenable_trace_fprobe(tf); - } - } else if (val =3D=3D MODULE_STATE_GOING && tp_mod->mod =3D=3D tf->mod) { - unregister_fprobe(&tf->fp); - if (trace_fprobe_is_tracepoint(tf)) { - tracepoint_probe_unregister(tf->tpoint, - tf->tpoint->probestub, NULL); - tf->tpoint =3D TRACEPOINT_STUB; + /* This is not a tracepoint in this module. Skip it. */ + if (!tpoint) + continue; + + tuser =3D tf->tuser; + /* If the tracepoint is no registered yet, register it. */ + if (!tracepoint_user_is_registered(tuser)) { + tuser->tpoint =3D tpoint; + if (WARN_ON_ONCE(tracepoint_user_register(tuser))) + continue; } + + /* Finally enable fprobe on this module. */ + if (!WARN_ON_ONCE(__regsiter_tracepoint_fprobe(tf)) && + trace_probe_is_enabled(&tf->tp)) + reenable_trace_fprobe(tf); + } else if (val =3D=3D MODULE_STATE_GOING) { + tuser =3D tf->tuser; + /* Unregister all tracepoint_user in this module. */ + if (tracepoint_user_is_registered(tuser) && + tracepoint_user_within_module(tuser, tp_mod->mod)) + tracepoint_user_unregister(tuser); + + /* + * Here we need to handle shared tracepoint_user case. + * Such tuser is unregistered, but trace_fprobe itself + * is registered. (Note this only handles tprobes.) + */ + if (!tracepoint_user_is_registered(tuser) && + trace_fprobe_is_registered(tf)) + unregister_fprobe(&tf->fp); } } mutex_unlock(&event_mutex); @@ -1067,7 +1220,9 @@ static int parse_symbol_and_return(int argc, const ch= ar *argv[], return 0; } =20 -DEFINE_FREE(module_put, struct module *, if (_T) module_put(_T)) +DEFINE_FREE(tuser_put, struct tracepoint_user *, + if (!IS_ERR_OR_NULL(_T)) + tracepoint_user_put(_T)) =20 static int trace_fprobe_create_internal(int argc, const char *argv[], struct traceprobe_parse_context *ctx) @@ -1097,6 +1252,8 @@ static int trace_fprobe_create_internal(int argc, con= st char *argv[], * FETCHARG:TYPE : use TYPE instead of unsigned long. */ struct trace_fprobe *tf __free(free_trace_fprobe) =3D NULL; + struct tracepoint_user *tuser __free(tuser_put) =3D NULL; + struct module *mod __free(module_put) =3D NULL; int i, new_argc =3D 0, ret =3D 0; bool is_return =3D false; char *symbol __free(kfree) =3D NULL; @@ -1108,8 +1265,6 @@ static int trace_fprobe_create_internal(int argc, con= st char *argv[], char abuf[MAX_BTF_ARGS_LEN]; char *dbuf __free(kfree) =3D NULL; bool is_tracepoint =3D false; - struct module *tp_mod __free(module_put) =3D NULL; - struct tracepoint *tpoint =3D NULL; =20 if ((argv[0][0] !=3D 'f' && argv[0][0] !=3D 't') || argc < 2) return -ECANCELED; @@ -1162,25 +1317,18 @@ static int trace_fprobe_create_internal(int argc, c= onst char *argv[], =20 if (is_tracepoint) { ctx->flags |=3D TPARG_FL_TPOINT; - tpoint =3D find_tracepoint(symbol, &tp_mod); - /* lock module until register this tprobe. */ - if (tp_mod && !try_module_get(tp_mod)) { - tpoint =3D NULL; - tp_mod =3D NULL; - } - if (tpoint) { - ctx->funcname =3D kallsyms_lookup( - (unsigned long)tpoint->probestub, - NULL, NULL, NULL, sbuf); - } else if (IS_ENABLED(CONFIG_MODULES)) { - /* This *may* be loaded afterwards */ - tpoint =3D TRACEPOINT_STUB; - ctx->funcname =3D symbol; - } else { + tuser =3D trace_fprobe_get_tracepoint_user(symbol, &mod); + if (!tuser) + return -ENOMEM; + if (IS_ERR(tuser)) { trace_probe_log_set_index(1); trace_probe_log_err(0, NO_TRACEPOINT); - return -EINVAL; + return PTR_ERR(tuser); } + ctx->funcname =3D tracepoint_user_lookup(tuser, sbuf); + /* If tracepoint is not loaded yet, use symbol name as funcname. */ + if (!ctx->funcname) + ctx->funcname =3D symbol; } else ctx->funcname =3D symbol; =20 @@ -1204,13 +1352,14 @@ static int trace_fprobe_create_internal(int argc, c= onst char *argv[], return ret; =20 /* setup a probe */ - tf =3D alloc_trace_fprobe(group, event, symbol, tpoint, argc, is_return); + tf =3D alloc_trace_fprobe(group, event, symbol, tuser, argc, is_return); if (IS_ERR(tf)) { ret =3D PTR_ERR(tf); /* This must return -ENOMEM, else there is a bug */ WARN_ON_ONCE(ret !=3D -ENOMEM); return ret; } + tuser =3D NULL; /* Move tuser to tf. */ =20 /* parse arguments */ for (i =3D 0; i < argc; i++) { From nobody Wed Dec 17 17:26:11 2025 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 A6DA71A3175; Sun, 16 Mar 2025 12:21:47 +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=1742127707; cv=none; b=JLLhBkJcdwnG5ObUGFrH3G1sionox4Gtwb3SoFebbmGFVsMTHmtwh5wCyr3l3aGXpXyLwZ0WTyG2W/a9ENLNtnYpR1SE72ODe9/MYG0iqVagLPTrrMAQzEzeO0fokUNOaNdEzZrGSRhHuv1NzAhFFl9ey5RtItqYerQ46OhZ1HI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742127707; c=relaxed/simple; bh=bBYHEjoEDIrgh2tzmkSng7ANeEMzqqTfDt0ky2Xlg1s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=nJrigUHC7gwjnTjfjxk9uQq3iEJn5Gma0P3/JitcXPllaFI43c995G1Dej/emGNZCOphW1lF/Pnw76StVw6ScLD179LdY+SWv3gC881jKhJqQ2fXCi8CBKe6O8mcvqAoURS4QYi9qbD33Ma0BbAuMtTLRxKC3TiEr/0FqRSqBT8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZuAKH3Vd; 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="ZuAKH3Vd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7BC54C4CEDD; Sun, 16 Mar 2025 12:21:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742127706; bh=bBYHEjoEDIrgh2tzmkSng7ANeEMzqqTfDt0ky2Xlg1s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZuAKH3VdYgKcNealxOivgsaSFEVYxXUhrqy7LSTGBCrTWi+TX1aH0s8GdFZF584Jv Mdg+Zcn6NDhLFIlqC8LKEwzuEjNBIKAhDAmY2GbPNXTWUu0v/5pzSDrlgD97UHkVD1 DJA3Plz8oe27e9gjmxX9IIMdWfOqhybarCitwzV0opapiJKHzJXd+S+qZdQzw+I8F8 q+1r7DlZ/bYBJJZlnZT8L2m2uvdj2acsnPZ5nmX20u0SaTJS9/zoJ3ndw2/C/mC5SF DbGiBcImReIyqUEIQROqHNn13YfC6WQpEKPdZa3VG8JYIsjCzP1bix10RJ3noOKNpG L3ilgPgCFK4Ew== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH 3/4] tracing: fprobe-events: Register fprobe-events only when it is enabled Date: Sun, 16 Mar 2025 21:21:42 +0900 Message-ID: <174212770238.348872.12106623852892827796.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <174212767109.348872.18231451508464729427.stgit@devnote2> References: <174212767109.348872.18231451508464729427.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Currently fprobe events are registered when it is defined. Thus it will give some overhead even if it is disabled. This changes it to register the fprobe only when it is enabled. Suggested-by: Steven Rostedt Signed-off-by: Masami Hiramatsu (Google) --- include/linux/fprobe.h | 8 + kernel/trace/fprobe.c | 29 +++-- kernel/trace/trace_fprobe.c | 234 +++++++++++++++++++++------------------= ---- 3 files changed, 140 insertions(+), 131 deletions(-) diff --git a/include/linux/fprobe.h b/include/linux/fprobe.h index 702099f08929..9635a24d5a25 100644 --- a/include/linux/fprobe.h +++ b/include/linux/fprobe.h @@ -94,6 +94,8 @@ int register_fprobe_ips(struct fprobe *fp, unsigned long = *addrs, int num); int register_fprobe_syms(struct fprobe *fp, const char **syms, int num); int unregister_fprobe(struct fprobe *fp); bool fprobe_is_registered(struct fprobe *fp); +int fprobe_alloc_ip_list_from_filter(const char *filter, const char *notfi= lter, + unsigned long **addrs); #else static inline int register_fprobe(struct fprobe *fp, const char *filter, c= onst char *notfilter) { @@ -115,6 +117,12 @@ static inline bool fprobe_is_registered(struct fprobe = *fp) { return false; } +static inline int fprobe_alloc_ip_list_from_filter(const char *filter, + const char *notfilter, + unsigned long **addrs) +{ + return -EOPNOTSUPP; +} #endif =20 /** diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 33082c4e8154..05050f1c2239 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -486,6 +486,24 @@ static int ip_list_from_filter(const char *filter, con= st char *notfilter, return match.index ?: -ENOENT; } =20 +#define FPROBE_IPS_MAX INT_MAX + +int fprobe_alloc_ip_list_from_filter(const char *filter, const char *notfi= lter, + unsigned long **addrs) +{ + int ret; + + /* Count the number of ips from filter. */ + ret =3D ip_list_from_filter(filter, notfilter, NULL, FPROBE_IPS_MAX); + if (ret < 0) + return ret; + + *addrs =3D kcalloc(ret, sizeof(unsigned long), GFP_KERNEL); + if (!*addrs) + return -ENOMEM; + return ip_list_from_filter(filter, notfilter, *addrs, ret); +} + static void fprobe_fail_cleanup(struct fprobe *fp) { kfree(fp->hlist_array); @@ -528,8 +546,6 @@ static int fprobe_init(struct fprobe *fp, unsigned long= *addrs, int num) return 0; } =20 -#define FPROBE_IPS_MAX INT_MAX - /** * register_fprobe() - Register fprobe to ftrace by pattern. * @fp: A fprobe data structure to be registered. @@ -549,14 +565,7 @@ int register_fprobe(struct fprobe *fp, const char *fil= ter, const char *notfilter if (!fp || !filter) return -EINVAL; =20 - ret =3D ip_list_from_filter(filter, notfilter, NULL, FPROBE_IPS_MAX); - if (ret < 0) - return ret; - - addrs =3D kcalloc(ret, sizeof(unsigned long), GFP_KERNEL); - if (!addrs) - return -ENOMEM; - ret =3D ip_list_from_filter(filter, notfilter, addrs, ret); + ret =3D fprobe_alloc_ip_list_from_filter(filter, notfilter, &addrs); if (ret > 0) ret =3D register_fprobe_ips(fp, addrs, ret); =20 diff --git a/kernel/trace/trace_fprobe.c b/kernel/trace/trace_fprobe.c index 863c9343a939..ac3b0a34810a 100644 --- a/kernel/trace/trace_fprobe.c +++ b/kernel/trace/trace_fprobe.c @@ -601,98 +601,6 @@ static struct trace_fprobe *find_trace_fprobe(const ch= ar *event, return NULL; } =20 -static inline int __enable_trace_fprobe(struct trace_fprobe *tf) -{ - if (trace_fprobe_is_registered(tf)) - enable_fprobe(&tf->fp); - - return 0; -} - -static void __disable_trace_fprobe(struct trace_probe *tp) -{ - struct trace_fprobe *tf; - - list_for_each_entry(tf, trace_probe_probe_list(tp), tp.list) { - if (!trace_fprobe_is_registered(tf)) - continue; - disable_fprobe(&tf->fp); - } -} - -/* - * Enable trace_probe - * if the file is NULL, enable "perf" handler, or enable "trace" handler. - */ -static int enable_trace_fprobe(struct trace_event_call *call, - struct trace_event_file *file) -{ - struct trace_probe *tp; - struct trace_fprobe *tf; - bool enabled; - int ret =3D 0; - - tp =3D trace_probe_primary_from_call(call); - if (WARN_ON_ONCE(!tp)) - return -ENODEV; - enabled =3D trace_probe_is_enabled(tp); - - /* This also changes "enabled" state */ - if (file) { - ret =3D trace_probe_add_file(tp, file); - if (ret) - return ret; - } else - trace_probe_set_flag(tp, TP_FLAG_PROFILE); - - if (!enabled) { - list_for_each_entry(tf, trace_probe_probe_list(tp), tp.list) { - /* TODO: check the fprobe is gone */ - __enable_trace_fprobe(tf); - } - } - - return 0; -} - -/* - * Disable trace_probe - * if the file is NULL, disable "perf" handler, or disable "trace" handler. - */ -static int disable_trace_fprobe(struct trace_event_call *call, - struct trace_event_file *file) -{ - struct trace_probe *tp; - - tp =3D trace_probe_primary_from_call(call); - if (WARN_ON_ONCE(!tp)) - return -ENODEV; - - if (file) { - if (!trace_probe_get_file_link(tp, file)) - return -ENOENT; - if (!trace_probe_has_single_file(tp)) - goto out; - trace_probe_clear_flag(tp, TP_FLAG_TRACE); - } else - trace_probe_clear_flag(tp, TP_FLAG_PROFILE); - - if (!trace_probe_is_enabled(tp)) - __disable_trace_fprobe(tp); - - out: - if (file) - /* - * Synchronization is done in below function. For perf event, - * file =3D=3D NULL and perf_trace_event_unreg() calls - * tracepoint_synchronize_unregister() to ensure synchronize - * event. We don't need to care about it. - */ - trace_probe_remove_file(tp, file); - - return 0; -} - /* Event entry printers */ static enum print_line_t print_fentry_event(struct trace_iterator *iter, int flags, @@ -852,6 +760,26 @@ static int __regsiter_tracepoint_fprobe(struct trace_f= probe *tf) return register_fprobe_ips(&tf->fp, &ip, 1); } =20 +/* Returns an error if the target function is not available, or 0 */ +static int trace_fprobe_verify_target(struct trace_fprobe *tf) +{ + unsigned long *addrs __free(kfree) =3D NULL; + int ret; + + if (trace_fprobe_is_tracepoint(tf)) { + + /* This tracepoint is not loaded yet */ + if (!tracepoint_user_is_registered(tf->tuser)) + return 0; + + /* We assume all stab function is tracable. */ + return tracepoint_user_ip(tf->tuser) ? 0 : -ENOENT; + } + + ret =3D fprobe_alloc_ip_list_from_filter(tf->symbol, NULL, &addrs); + return (ret < 0) ? ret : 0; +} + /* Internal register function - just handle fprobe and flags */ static int __register_trace_fprobe(struct trace_fprobe *tf) { @@ -871,11 +799,7 @@ static int __register_trace_fprobe(struct trace_fprobe= *tf) return ret; } =20 - /* Set/clear disabled flag according to tp->flag */ - if (trace_probe_is_enabled(&tf->tp)) - tf->fp.flags &=3D ~FPROBE_FL_DISABLED; - else - tf->fp.flags |=3D FPROBE_FL_DISABLED; + tf->fp.flags &=3D ~FPROBE_FL_DISABLED; =20 if (trace_fprobe_is_tracepoint(tf)) { =20 @@ -896,10 +820,10 @@ static void __unregister_trace_fprobe(struct trace_fp= robe *tf) if (trace_fprobe_is_registered(tf)) { unregister_fprobe(&tf->fp); memset(&tf->fp, 0, sizeof(tf->fp)); - if (trace_fprobe_is_tracepoint(tf)) { - tracepoint_user_put(tf->tuser); - tf->tuser =3D NULL; - } + } + if (trace_fprobe_is_tracepoint(tf)) { + tracepoint_user_put(tf->tuser); + tf->tuser =3D NULL; } } =20 @@ -959,7 +883,7 @@ static bool trace_fprobe_has_same_fprobe(struct trace_f= probe *orig, return false; } =20 -static int append_trace_fprobe(struct trace_fprobe *tf, struct trace_fprob= e *to) +static int append_trace_fprobe_event(struct trace_fprobe *tf, struct trace= _fprobe *to) { int ret; =20 @@ -987,7 +911,7 @@ static int append_trace_fprobe(struct trace_fprobe *tf,= struct trace_fprobe *to) if (ret) return ret; =20 - ret =3D __register_trace_fprobe(tf); + ret =3D trace_fprobe_verify_target(tf); if (ret) trace_probe_unlink(&tf->tp); else @@ -996,8 +920,8 @@ static int append_trace_fprobe(struct trace_fprobe *tf,= struct trace_fprobe *to) return ret; } =20 -/* Register a trace_probe and probe_event */ -static int register_trace_fprobe(struct trace_fprobe *tf) +/* Register a trace_probe and probe_event, and check the fprobe is availab= le. */ +static int register_trace_fprobe_event(struct trace_fprobe *tf) { struct trace_fprobe *old_tf; int ret; @@ -1007,7 +931,7 @@ static int register_trace_fprobe(struct trace_fprobe *= tf) old_tf =3D find_trace_fprobe(trace_probe_name(&tf->tp), trace_probe_group_name(&tf->tp)); if (old_tf) - return append_trace_fprobe(tf, old_tf); + return append_trace_fprobe_event(tf, old_tf); =20 /* Register new event */ ret =3D register_fprobe_event(tf); @@ -1020,8 +944,8 @@ static int register_trace_fprobe(struct trace_fprobe *= tf) return ret; } =20 - /* Register fprobe */ - ret =3D __register_trace_fprobe(tf); + /* Verify fprobe is sane. */ + ret =3D trace_fprobe_verify_target(tf); if (ret < 0) unregister_fprobe_event(tf); else @@ -1075,15 +999,6 @@ static struct tracepoint *find_tracepoint(const char = *tp_name, } =20 #ifdef CONFIG_MODULES -static void reenable_trace_fprobe(struct trace_fprobe *tf) -{ - struct trace_probe *tp =3D &tf->tp; - - list_for_each_entry(tf, trace_probe_probe_list(tp), tp.list) { - __enable_trace_fprobe(tf); - } -} - /* Find a tracepoint from specified module. */ static struct tracepoint *find_tracepoint_in_module(struct module *mod, const char *tp_name) @@ -1134,9 +1049,8 @@ static int __tracepoint_probe_module_cb(struct notifi= er_block *self, } =20 /* Finally enable fprobe on this module. */ - if (!WARN_ON_ONCE(__regsiter_tracepoint_fprobe(tf)) && - trace_probe_is_enabled(&tf->tp)) - reenable_trace_fprobe(tf); + if (trace_probe_is_enabled(&tf->tp) && !trace_fprobe_is_registered(tf)) + WARN_ON_ONCE(__regsiter_tracepoint_fprobe(tf)); } else if (val =3D=3D MODULE_STATE_GOING) { tuser =3D tf->tuser; /* Unregister all tracepoint_user in this module. */ @@ -1385,7 +1299,7 @@ static int trace_fprobe_create_internal(int argc, con= st char *argv[], if (ret < 0) return ret; =20 - ret =3D register_trace_fprobe(tf); + ret =3D register_trace_fprobe_event(tf); if (ret) { trace_probe_log_set_index(1); if (ret =3D=3D -EILSEQ) @@ -1454,6 +1368,84 @@ static int trace_fprobe_show(struct seq_file *m, str= uct dyn_event *ev) return 0; } =20 +/* + * Enable trace_probe + * if the file is NULL, enable "perf" handler, or enable "trace" handler. + */ +static int enable_trace_fprobe(struct trace_event_call *call, + struct trace_event_file *file) +{ + struct trace_probe *tp; + struct trace_fprobe *tf; + bool enabled; + int ret =3D 0; + + tp =3D trace_probe_primary_from_call(call); + if (WARN_ON_ONCE(!tp)) + return -ENODEV; + enabled =3D trace_probe_is_enabled(tp); + + /* This also changes "enabled" state */ + if (file) { + ret =3D trace_probe_add_file(tp, file); + if (ret) + return ret; + } else + trace_probe_set_flag(tp, TP_FLAG_PROFILE); + + if (!enabled) { + list_for_each_entry(tf, trace_probe_probe_list(tp), tp.list) { + ret =3D __register_trace_fprobe(tf); + if (ret < 0) + return ret; + } + } + + return 0; +} + +/* + * Disable trace_probe + * if the file is NULL, disable "perf" handler, or disable "trace" handler. + */ +static int disable_trace_fprobe(struct trace_event_call *call, + struct trace_event_file *file) +{ + struct trace_fprobe *tf; + struct trace_probe *tp; + + tp =3D trace_probe_primary_from_call(call); + if (WARN_ON_ONCE(!tp)) + return -ENODEV; + + if (file) { + if (!trace_probe_get_file_link(tp, file)) + return -ENOENT; + if (!trace_probe_has_single_file(tp)) + goto out; + trace_probe_clear_flag(tp, TP_FLAG_TRACE); + } else + trace_probe_clear_flag(tp, TP_FLAG_PROFILE); + + if (!trace_probe_is_enabled(tp)) { + list_for_each_entry(tf, trace_probe_probe_list(tp), tp.list) { + unregister_fprobe(&tf->fp); + } + } + + out: + if (file) + /* + * Synchronization is done in below function. For perf event, + * file =3D=3D NULL and perf_trace_event_unreg() calls + * tracepoint_synchronize_unregister() to ensure synchronize + * event. We don't need to care about it. + */ + trace_probe_remove_file(tp, file); + + return 0; +} + /* * called by perf_trace_init() or __ftrace_set_clr_event() under event_mut= ex. */ From nobody Wed Dec 17 17:26:11 2025 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 7FBD818B47C; Sun, 16 Mar 2025 12:21:57 +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=1742127717; cv=none; b=aJNZdi7f1FB2sngK8NmWfp0W6jaMeD6EHlIF0b2rZ8HJsN3mWNzrJtVxyq8tzVBSoiN7km+GxETHM6kPkfPpXHr7wlcCHl9zq4TVYQBCB50jUGTgLTWK9r+t8F0710wiUARwn/XF9ghBUrdo3LkBPImXayQH6QvPFonG+uXLIS0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742127717; c=relaxed/simple; bh=y26tfEd3RrwIby+c9q4PmhmcouiauWQ8HV8iy1l4U3M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=mEOfDLUQ4koRgudZSX/BXh+dQjT43OkMAZRkNER4UmxsayWcfAo+UJili18ZBoe5rDzVfflVtYmz4iJLatfoPzhSHJQpWuUUhhQO2w4dq7PnV2RimPXE5uEf8tjihwo4hBckL5+Kc7Rn32Pi5WLycYCA/QucyX3bq6kYyd/U3e8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=aZPActpl; 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="aZPActpl" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6641EC4CEDD; Sun, 16 Mar 2025 12:21:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742127717; bh=y26tfEd3RrwIby+c9q4PmhmcouiauWQ8HV8iy1l4U3M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aZPActplDB7iTUEFZYECTRc5twmgJVus7StJps9Bke4kX5VDlfPir5JuriSqc7gNS GWgAd2wz+oKPrho5rgWsNYIUIRxZZOcrfCVyKPHYGsWX+rNzcFKyjGqHzlLoTwhC/C F2UhWrXfod5voQ5hw90+I8rEm4SuLDvTFisUPYZeLzY/MDthNsFfgprdYqAdNHxGvP LPb5/S5yJx12AcOfRNLMzfTXv8Xnj4MGTqMxamnJALlraBYHSbxW6Rd/qyMXeEtUNI bNSyXKQa9Am3AA7Aw23XzdjKMokC6BaRyB7rVlnF7CazvLT0zDfdx9gMlnbL8NDo7k ijXLPyXbg6C6Q== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH 4/4] selftests: tracing: Enable fprobe events before checking enable_functions Date: Sun, 16 Mar 2025 21:21:52 +0900 Message-ID: <174212771234.348872.14455715715729686727.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <174212767109.348872.18231451508464729427.stgit@devnote2> References: <174212767109.348872.18231451508464729427.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Signed-off-by: Masami Hiramatsu (Google) --- .../ftrace/test.d/dynevent/add_remove_fprobe.tc | 30 +++++++++++++---= ---- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fpro= be.tc b/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe.tc index 449f9d8be746..603dad73e7e8 100644 --- a/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe.tc +++ b/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe.tc @@ -12,6 +12,18 @@ PLACE3=3D"schedule_timeout" =20 echo "f:myevent1 $PLACE" >> dynamic_events =20 +echo "f:myevent2 $PLACE%return" >> dynamic_events + +# add another event +echo "f:myevent3 $PLACE2" >> dynamic_events + +grep -q myevent1 dynamic_events +grep -q myevent2 dynamic_events +grep -q myevent3 dynamic_events +test -d events/fprobes/myevent1 +test -d events/fprobes/myevent2 + +echo 1 > events/fprobes/myevent1/enable # Make sure the event is attached and is the only one grep -q $PLACE enabled_functions cnt=3D`cat enabled_functions | wc -l` @@ -19,29 +31,22 @@ if [ $cnt -ne 1 ]; then exit_fail fi =20 -echo "f:myevent2 $PLACE%return" >> dynamic_events - +echo 1 > events/fprobes/myevent2/enable # It should till be the only attached function cnt=3D`cat enabled_functions | wc -l` if [ $cnt -ne 1 ]; then exit_fail fi =20 -# add another event -echo "f:myevent3 $PLACE2" >> dynamic_events - +echo 1 > events/fprobes/myevent3/enable +# If the function is different, the attached function should be increased grep -q $PLACE2 enabled_functions cnt=3D`cat enabled_functions | wc -l` if [ $cnt -ne 2 ]; then exit_fail fi =20 -grep -q myevent1 dynamic_events -grep -q myevent2 dynamic_events -grep -q myevent3 dynamic_events -test -d events/fprobes/myevent1 -test -d events/fprobes/myevent2 - +echo 0 > events/fprobes/myevent2/enable echo "-:myevent2" >> dynamic_events =20 grep -q myevent1 dynamic_events @@ -53,6 +58,7 @@ if [ $cnt -ne 2 ]; then exit_fail fi =20 +echo 0 > events/fprobes/enable echo > dynamic_events =20 # Should have none left @@ -63,12 +69,14 @@ fi =20 echo "f:myevent4 $PLACE" >> dynamic_events =20 +echo 1 > events/fprobes/myevent4/enable # Should only have one enabled cnt=3D`cat enabled_functions | wc -l` if [ $cnt -ne 1 ]; then exit_fail fi =20 +echo 0 > events/fprobes/enable echo > dynamic_events =20 # Should have none left From nobody Wed Dec 17 17:26:11 2025 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 DDF7D21ABCC; Mon, 17 Mar 2025 08:03: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=1742198601; cv=none; b=uRb9UWi7/jG9d6r+NcW5k7r+3I1hFKbNFr1djon0pdzGEYQSsfBizavSJq7FpyzXpEEbrQ7MCkCS0HBUlcdMP7/nExbs1U5hzsRlKS8zmfav1u40t2CqDapHKokOjvDahl+YwnZUqX5jUyDrS9rDa3jFSsWb9gIRd3UEYFD2Q5k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742198601; c=relaxed/simple; bh=tl7g7tnLHVkIpMwuft/E0gPaRqKFHy7aKobfLmft7/8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=i/8h1KPrJRFyC5HQyu3qLcX+HWWxCcMpHFmS8JlaF5uFX3YmL8e4Iq/z5616sQc4ADRD65x+mZW8adGu7p4O5TvSv2IBtC2kGMifG7/yprOvnzo1kTV6X4faee/FulvTkNBqrNN83XdgDgQc2wWlIxtXVx4ZJALcizqVdtF7TrI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=E6jyJ4Jg; 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="E6jyJ4Jg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 15EC3C4CEE3; Mon, 17 Mar 2025 08:03:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742198600; bh=tl7g7tnLHVkIpMwuft/E0gPaRqKFHy7aKobfLmft7/8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=E6jyJ4JgnJqWcyOKsNzUpfJrRqsU4ztN8Bsdxrli9HjMUSaiWS+TslQaFbiK04kbG DBMjE6MKkz6WrGTXyc0cEJbx3kxbbRh0OVZtViS24SlaAGJ56HH2EL0rnUucwhJGTZ Xgh4wpDFezE8n/YOgJgt1QmpJ03Z3FXFJleaSnzeyEQEk8X2nZtS9u4EwujYcGD+37 VzM2PVRT6dhundulCq2Ys4MDvm88LheKBf41RnJDiruO2yimrXegJgfa5Z2Nffnz1I ya8d1FcbWQ0ucksPfjFjW9PNIs+HoU5kYlEO0WiOyCizzqqM1raaFi+xTKjfVH+6qF WjPWpvYbP7SCA== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RFC PATCH] tracing: tprobe-events: Register tracepoint when enable tprobe event Date: Mon, 17 Mar 2025 17:03:16 +0900 Message-ID: <174219859655.549159.12584048778295152488.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <174212767109.348872.18231451508464729427.stgit@devnote2> References: <174212767109.348872.18231451508464729427.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) As same as fprobe, register tracepoint stub function only when enabling tprobe events. The major changes are introducing a list of tracepoint_user and its lock, and tprobe_event_module_nb, which is another module notifier for module loading/unloading. By spliting the lock from event_mutex and a module notifier for trace_fprobe, it solved AB-BA lock dependency issue between event_mutex and tracepoint_module_list_mutex. Signed-off-by: Masami Hiramatsu (Google) --- kernel/trace/trace_fprobe.c | 382 +++++++++++++++++++++++++--------------= ---- 1 file changed, 218 insertions(+), 164 deletions(-) diff --git a/kernel/trace/trace_fprobe.c b/kernel/trace/trace_fprobe.c index ac3b0a34810a..2c4352f4dc11 100644 --- a/kernel/trace/trace_fprobe.c +++ b/kernel/trace/trace_fprobe.c @@ -7,7 +7,9 @@ #include =20 #include +#include #include +#include #include #include #include @@ -37,15 +39,21 @@ static struct dyn_event_operations trace_fprobe_ops =3D= { .match =3D trace_fprobe_match, }; =20 +/* List of tracepoint_user */ +static LIST_HEAD(tracepoint_user_list); +static DEFINE_MUTEX(tracepoint_user_mutex); + +/* While living tracepoint_user, @tpoint can be NULL and @refcount !=3D 0.= */ struct tracepoint_user { + struct list_head list; + const char *name; struct tracepoint *tpoint; unsigned int refcount; }; =20 -static bool tracepoint_user_is_registered(struct tracepoint_user *tuser) -{ - return tuser && tuser->tpoint; -} +/* NOTE: you must lock tracepoint_user_mutex. */ +#define for_each_tracepoint_user(tuser) \ + list_for_each_entry(tuser, &tracepoint_user_list, list) =20 static int tracepoint_user_register(struct tracepoint_user *tuser) { @@ -75,58 +83,112 @@ static unsigned long tracepoint_user_ip(struct tracepo= int_user *tuser) return (unsigned long)tuser->tpoint->probestub; } =20 -static bool tracepoint_user_within_module(struct tracepoint_user *tuser, - struct module *mod) +static void __tracepoint_user_free(struct tracepoint_user *tuser) { - return within_module(tracepoint_user_ip(tuser), mod); + if (!tuser) + return; + kfree(tuser->name); + kfree(tuser); } =20 -static struct tracepoint_user *tracepoint_user_allocate(struct tracepoint = *tpoint) +DEFINE_FREE(tuser_free, struct tracepoint_user *, __tracepoint_user_free(_= T)) + +static struct tracepoint_user *__tracepoint_user_init(const char *name, st= ruct tracepoint *tpoint) { - struct tracepoint_user *tuser __free(kfree) =3D NULL; + struct tracepoint_user *tuser __free(tuser_free) =3D NULL; + int ret; =20 tuser =3D kzalloc(sizeof(*tuser), GFP_KERNEL); if (!tuser) return NULL; + tuser->name =3D kstrdup(name, GFP_KERNEL); + if (!tuser->name) + return NULL; + + if (tpoint) { + ret =3D tracepoint_user_register(tuser); + if (ret) + return ERR_PTR(ret); + } + tuser->tpoint =3D tpoint; tuser->refcount =3D 1; + INIT_LIST_HEAD(&tuser->list); + list_add(&tuser->list, &tracepoint_user_list); =20 return_ptr(tuser); } =20 -/* These must be called with event_mutex */ -static void tracepoint_user_get(struct tracepoint_user *tuser) -{ - tuser->refcount++; -} +static struct tracepoint *find_tracepoint(const char *tp_name, + struct module **tp_mod); =20 -static void tracepoint_user_put(struct tracepoint_user *tuser) +/* + * Get tracepoint_user if exist, or allocate new one and register it. + * If tracepoint is on a module, get its refcounter too. + * This returns errno or NULL (not loaded yet) or tracepoint_user. + */ +static struct tracepoint_user *tracepoint_user_find_get(const char *name, = struct module **pmod) { - if (--tuser->refcount > 0) - return; + struct module *mod __free(module_put) =3D NULL; + struct tracepoint_user *tuser; + struct tracepoint *tpoint; =20 - if (tracepoint_user_is_registered(tuser)) - tracepoint_user_unregister(tuser); - kfree(tuser); + /* Get and lock the module which has tracepoint. */ + tpoint =3D find_tracepoint(name, &mod); + if (mod && !try_module_get(mod)) { + mod =3D NULL; + tpoint =3D NULL; + } + + guard(mutex)(&tracepoint_user_mutex); + /* Search existing tracepoint_user */ + for_each_tracepoint_user(tuser) { + if (!strcmp(tuser->name, name)) { + tuser->refcount++; + *pmod =3D no_free_ptr(mod); + return tuser; + } + } + + /* The corresponding tracepoint_user is not found. */ + tuser =3D __tracepoint_user_init(name, tpoint); + if (!IS_ERR_OR_NULL(tuser)) + *pmod =3D no_free_ptr(mod); + + return tuser; } =20 -static const char *tracepoint_user_lookup(struct tracepoint_user *tuser, c= har *buf) +static void tracepoint_user_put(struct tracepoint_user *tuser) { - struct tracepoint *tpoint =3D tuser->tpoint; + scoped_guard(mutex, &tracepoint_user_mutex) { + if (--tuser->refcount > 0) + return; =20 - if (!tpoint) - return NULL; + list_del(&tuser->list); + tracepoint_user_unregister(tuser); + } =20 - return kallsyms_lookup((unsigned long)tpoint->probestub, NULL, NULL, NULL= , buf); + __tracepoint_user_free(tuser); } =20 +DEFINE_FREE(tuser_put, struct tracepoint_user *, + if (!IS_ERR_OR_NULL(_T)) + tracepoint_user_put(_T)) + /* * Fprobe event core functions */ + +/* + * @tprobe is true for tracepoint probe. + * @tuser can be NULL if the trace_fprobe is disabled or the tracepoint is= not + * loaded with a module. If @tuser !=3D NULL, this trace_fprobe is enabled. + */ struct trace_fprobe { struct dyn_event devent; struct fprobe fp; const char *symbol; + bool tprobe; struct tracepoint_user *tuser; struct trace_probe tp; }; @@ -157,7 +219,7 @@ static bool trace_fprobe_is_return(struct trace_fprobe = *tf) =20 static bool trace_fprobe_is_tracepoint(struct trace_fprobe *tf) { - return tf->tuser !=3D NULL; + return tf->tprobe; } =20 static const char *trace_fprobe_symbol(struct trace_fprobe *tf) @@ -207,57 +269,6 @@ static bool trace_fprobe_is_registered(struct trace_fp= robe *tf) return fprobe_is_registered(&tf->fp); } =20 -static struct tracepoint *find_tracepoint(const char *tp_name, - struct module **tp_mod); - -/* - * Get tracepoint_user if exist, or allocate new one. If tracepoint is on a - * module, get its refcounter. - */ -static struct tracepoint_user * -trace_fprobe_get_tracepoint_user(const char *name, struct module **pmod) -{ - struct tracepoint_user *tuser __free(kfree) =3D NULL; - struct tracepoint *tpoint; - struct trace_fprobe *tf; - struct dyn_event *dpos; - struct module *mod __free(module_put) =3D NULL; - int ret; - - tpoint =3D find_tracepoint(name, &mod); - if (mod && !try_module_get(mod)) { - mod =3D NULL; - tpoint =3D NULL; - } - /* tpoint can be NULL, but we don't care here. */ - - /* Search existing tracepoint_user */ - for_each_trace_fprobe(tf, dpos) { - if (!trace_fprobe_is_tracepoint(tf)) - continue; - if (!strcmp(tf->symbol, name)) { - tracepoint_user_get(tf->tuser); - *pmod =3D no_free_ptr(mod); - return tf->tuser; - } - } - - /* Not found, allocate and register new tracepoint_user. */ - tuser =3D tracepoint_user_allocate(tpoint); - if (!tuser) - return NULL; - - if (tpoint) { - /* If the tracepoint is not loaded, tpoint can be NULL. */ - ret =3D tracepoint_user_register(tuser); - if (ret) - return ERR_PTR(ret); - } - - *pmod =3D no_free_ptr(mod); - return_ptr(tuser); -} - /* * Note that we don't verify the fetch_insn code, since it does not come * from user space. @@ -559,8 +570,8 @@ DEFINE_FREE(free_trace_fprobe, struct trace_fprobe *, i= f (!IS_ERR_OR_NULL(_T)) f static struct trace_fprobe *alloc_trace_fprobe(const char *group, const char *event, const char *symbol, - struct tracepoint_user *tuser, - int nargs, bool is_return) + int nargs, bool is_return, + bool is_tracepoint) { struct trace_fprobe *tf __free(free_trace_fprobe) =3D NULL; int ret =3D -ENOMEM; @@ -578,7 +589,7 @@ static struct trace_fprobe *alloc_trace_fprobe(const ch= ar *group, else tf->fp.entry_handler =3D fentry_dispatcher; =20 - tf->tuser =3D tuser; + tf->tprobe =3D is_tracepoint; =20 ret =3D trace_probe_init(&tf->tp, event, group, false, nargs); if (ret < 0) @@ -752,12 +763,35 @@ static int unregister_fprobe_event(struct trace_fprob= e *tf) =20 static int __regsiter_tracepoint_fprobe(struct trace_fprobe *tf) { - unsigned long ip =3D tracepoint_user_ip(tf->tuser); + struct tracepoint_user *tuser __free(tuser_put) =3D NULL; + struct module *mod __free(module_put) =3D NULL; + unsigned long ip; + int ret; =20 - if (!ip) - return -ENOENT; + if (WARN_ON_ONCE(tf->tuser)) + return -EINVAL; + + /* If the tracepoint is in a module, it must be locked in this function. = */ + tuser =3D tracepoint_user_find_get(tf->symbol, &mod); + /* This tracepoint is not loaded yet */ + if (IS_ERR(tuser)) + return PTR_ERR(tuser); + if (!tuser) + return -ENOMEM; + + /* Register fprobe only if the tracepoint is loaded. */ + if (tuser->tpoint) { + ip =3D tracepoint_user_ip(tuser); + if (WARN_ON_ONCE(!ip)) + return -ENOENT; =20 - return register_fprobe_ips(&tf->fp, &ip, 1); + ret =3D register_fprobe_ips(&tf->fp, &ip, 1); + if (ret < 0) + return ret; + } + + tf->tuser =3D no_free_ptr(tuser); + return 0; } =20 /* Returns an error if the target function is not available, or 0 */ @@ -766,15 +800,9 @@ static int trace_fprobe_verify_target(struct trace_fpr= obe *tf) unsigned long *addrs __free(kfree) =3D NULL; int ret; =20 - if (trace_fprobe_is_tracepoint(tf)) { - - /* This tracepoint is not loaded yet */ - if (!tracepoint_user_is_registered(tf->tuser)) - return 0; - - /* We assume all stab function is tracable. */ - return tracepoint_user_ip(tf->tuser) ? 0 : -ENOENT; - } + /* Tracepoint should have a stub function. */ + if (trace_fprobe_is_tracepoint(tf)) + return 0; =20 ret =3D fprobe_alloc_ip_list_from_filter(tf->symbol, NULL, &addrs); return (ret < 0) ? ret : 0; @@ -801,14 +829,8 @@ static int __register_trace_fprobe(struct trace_fprobe= *tf) =20 tf->fp.flags &=3D ~FPROBE_FL_DISABLED; =20 - if (trace_fprobe_is_tracepoint(tf)) { - - /* This tracepoint is not loaded yet */ - if (!tracepoint_user_is_registered(tf->tuser)) - return 0; - + if (trace_fprobe_is_tracepoint(tf)) return __regsiter_tracepoint_fprobe(tf); - } =20 /* TODO: handle filter, nofilter or symbol list */ return register_fprobe(&tf->fp, tf->symbol, NULL); @@ -817,11 +839,9 @@ static int __register_trace_fprobe(struct trace_fprobe= *tf) /* Internal unregister function - just handle fprobe and flags */ static void __unregister_trace_fprobe(struct trace_fprobe *tf) { - if (trace_fprobe_is_registered(tf)) { + if (trace_fprobe_is_registered(tf)) unregister_fprobe(&tf->fp); - memset(&tf->fp, 0, sizeof(tf->fp)); - } - if (trace_fprobe_is_tracepoint(tf)) { + if (tf->tuser) { tracepoint_user_put(tf->tuser); tf->tuser =3D NULL; } @@ -1012,63 +1032,52 @@ static struct tracepoint *find_tracepoint_in_module= (struct module *mod, return data.tpoint; } =20 +/* These are CONFIG_MODULES=3Dy specific functions. */ +static bool tracepoint_user_within_module(struct tracepoint_user *tuser, + struct module *mod) +{ + return within_module(tracepoint_user_ip(tuser), mod); +} + +static int tracepoint_user_register_again(struct tracepoint_user *tuser, + struct tracepoint *tpoint) +{ + tuser->tpoint =3D tpoint; + return tracepoint_user_register(tuser); +} + +static void tracepoint_user_unregister_clear(struct tracepoint_user *tuser) +{ + tracepoint_user_unregister(tuser); + tuser->tpoint =3D NULL; +} + +/* module callback for tracepoint_user */ static int __tracepoint_probe_module_cb(struct notifier_block *self, unsigned long val, void *data) { struct tp_module *tp_mod =3D data; struct tracepoint_user *tuser; - struct trace_fprobe *tf; - struct dyn_event *pos; + struct tracepoint *tpoint; =20 if (val !=3D MODULE_STATE_GOING && val !=3D MODULE_STATE_COMING) return NOTIFY_DONE; =20 - mutex_lock(&event_mutex); - for_each_trace_fprobe(tf, pos) { - if (!trace_fprobe_is_tracepoint(tf)) - continue; - + mutex_lock(&tracepoint_user_mutex); + for_each_tracepoint_user(tuser) { if (val =3D=3D MODULE_STATE_COMING) { - /* - * If any tracepoint used by tprobe is in the module, - * register the stub. - */ - struct tracepoint *tpoint; - - tpoint =3D find_tracepoint_in_module(tp_mod->mod, tf->symbol); /* This is not a tracepoint in this module. Skip it. */ + tpoint =3D find_tracepoint_in_module(tp_mod->mod, tuser->name); if (!tpoint) continue; - - tuser =3D tf->tuser; - /* If the tracepoint is no registered yet, register it. */ - if (!tracepoint_user_is_registered(tuser)) { - tuser->tpoint =3D tpoint; - if (WARN_ON_ONCE(tracepoint_user_register(tuser))) - continue; - } - - /* Finally enable fprobe on this module. */ - if (trace_probe_is_enabled(&tf->tp) && !trace_fprobe_is_registered(tf)) - WARN_ON_ONCE(__regsiter_tracepoint_fprobe(tf)); - } else if (val =3D=3D MODULE_STATE_GOING) { - tuser =3D tf->tuser; + WARN_ON_ONCE(tracepoint_user_register_again(tuser, tpoint)); + } else if (val =3D=3D MODULE_STATE_GOING && + tracepoint_user_within_module(tuser, tp_mod->mod)) { /* Unregister all tracepoint_user in this module. */ - if (tracepoint_user_is_registered(tuser) && - tracepoint_user_within_module(tuser, tp_mod->mod)) - tracepoint_user_unregister(tuser); - - /* - * Here we need to handle shared tracepoint_user case. - * Such tuser is unregistered, but trace_fprobe itself - * is registered. (Note this only handles tprobes.) - */ - if (!tracepoint_user_is_registered(tuser) && - trace_fprobe_is_registered(tf)) - unregister_fprobe(&tf->fp); + tracepoint_user_unregister_clear(tuser); } } - mutex_unlock(&event_mutex); + mutex_unlock(&tracepoint_user_mutex); =20 return NOTIFY_DONE; } @@ -1076,6 +1085,54 @@ static int __tracepoint_probe_module_cb(struct notif= ier_block *self, static struct notifier_block tracepoint_module_nb =3D { .notifier_call =3D __tracepoint_probe_module_cb, }; + +/* module callback for tprobe events */ +static int __tprobe_event_module_cb(struct notifier_block *self, + unsigned long val, void *data) +{ + struct trace_fprobe *tf; + struct dyn_event *pos; + struct module *mod =3D data; + + if (val !=3D MODULE_STATE_GOING && val !=3D MODULE_STATE_COMING) + return NOTIFY_DONE; + + mutex_lock(&event_mutex); + for_each_trace_fprobe(tf, pos) { + /* Skip fprobe and disabled tprobe events. */ + if (!trace_fprobe_is_tracepoint(tf) || !tf->tuser) + continue; + + /* Before this notification, tracepoint notifier has already done. */ + if (val =3D=3D MODULE_STATE_COMING && + tracepoint_user_within_module(tf->tuser, mod)) { + unsigned long ip =3D tracepoint_user_ip(tf->tuser); + + WARN_ON_ONCE(register_fprobe_ips(&tf->fp, &ip, 1)); + } else if (val =3D=3D MODULE_STATE_GOING && + /* + * tracepoint_user_within_module() does not work here because + * tracepoint_user is already unregistered and cleared tpoint. + * Instead, checking whether the fprobe is registered but + * tpoint is cleared(unregistered). Such unbalance probes + * must be adjusted anyway. + */ + trace_fprobe_is_registered(tf) && + !tf->tuser->tpoint) { + unregister_fprobe(&tf->fp); + } + } + mutex_unlock(&event_mutex); + + return NOTIFY_DONE; +} + +/* NOTE: this must be called after tracepoint callback */ +static struct notifier_block tprobe_event_module_nb =3D { + .notifier_call =3D __tprobe_event_module_cb, + /* Make sure this is later than tracepoint module notifier. */ + .priority =3D -10, +}; #endif /* CONFIG_MODULES */ =20 static int parse_symbol_and_return(int argc, const char *argv[], @@ -1134,10 +1191,6 @@ static int parse_symbol_and_return(int argc, const c= har *argv[], return 0; } =20 -DEFINE_FREE(tuser_put, struct tracepoint_user *, - if (!IS_ERR_OR_NULL(_T)) - tracepoint_user_put(_T)) - static int trace_fprobe_create_internal(int argc, const char *argv[], struct traceprobe_parse_context *ctx) { @@ -1166,7 +1219,6 @@ static int trace_fprobe_create_internal(int argc, con= st char *argv[], * FETCHARG:TYPE : use TYPE instead of unsigned long. */ struct trace_fprobe *tf __free(free_trace_fprobe) =3D NULL; - struct tracepoint_user *tuser __free(tuser_put) =3D NULL; struct module *mod __free(module_put) =3D NULL; int i, new_argc =3D 0, ret =3D 0; bool is_return =3D false; @@ -1229,21 +1281,21 @@ static int trace_fprobe_create_internal(int argc, c= onst char *argv[], else ctx->flags |=3D TPARG_FL_FENTRY; =20 + ctx->funcname =3D NULL; if (is_tracepoint) { + /* Get tracepoint and lock its module until the end of the registration.= */ + struct tracepoint *tpoint =3D find_tracepoint(symbol, &mod); + ctx->flags |=3D TPARG_FL_TPOINT; - tuser =3D trace_fprobe_get_tracepoint_user(symbol, &mod); - if (!tuser) - return -ENOMEM; - if (IS_ERR(tuser)) { - trace_probe_log_set_index(1); - trace_probe_log_err(0, NO_TRACEPOINT); - return PTR_ERR(tuser); + if (mod && !try_module_get(mod)) { + mod =3D NULL; + tpoint =3D NULL; } - ctx->funcname =3D tracepoint_user_lookup(tuser, sbuf); - /* If tracepoint is not loaded yet, use symbol name as funcname. */ - if (!ctx->funcname) - ctx->funcname =3D symbol; - } else + if (tpoint) + ctx->funcname =3D kallsyms_lookup((unsigned long)tpoint->probestub, + NULL, NULL, NULL, sbuf); + } + if (!ctx->funcname) ctx->funcname =3D symbol; =20 argc -=3D 2; argv +=3D 2; @@ -1266,14 +1318,13 @@ static int trace_fprobe_create_internal(int argc, c= onst char *argv[], return ret; =20 /* setup a probe */ - tf =3D alloc_trace_fprobe(group, event, symbol, tuser, argc, is_return); + tf =3D alloc_trace_fprobe(group, event, symbol, argc, is_return, is_trace= point); if (IS_ERR(tf)) { ret =3D PTR_ERR(tf); /* This must return -ENOMEM, else there is a bug */ WARN_ON_ONCE(ret !=3D -ENOMEM); return ret; } - tuser =3D NULL; /* Move tuser to tf. */ =20 /* parse arguments */ for (i =3D 0; i < argc; i++) { @@ -1491,6 +1542,9 @@ static __init int init_fprobe_trace_early(void) ret =3D register_tracepoint_module_notifier(&tracepoint_module_nb); if (ret) return ret; + ret =3D register_module_notifier(&tprobe_event_module_nb); + if (ret) + return ret; #endif =20 return 0;