fprobe: Ensure running fprobe_exit_handler() finished before calling rethook_free()
Message ID | 168796344232.46347.7947681068822514750.stgit@devnote2 |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp9003537vqr; Wed, 28 Jun 2023 08:16:40 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4EJEXYaqMaSlKTsOhQSaQQmXV8u5SvQ+5cAy4AAAse9MA/j9A2oPAz5Sj2Ml5JmBJaC/zI X-Received: by 2002:a05:6a00:1d18:b0:668:70af:b5c1 with SMTP id a24-20020a056a001d1800b0066870afb5c1mr123751pfx.1.1687965400182; Wed, 28 Jun 2023 08:16:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687965400; cv=none; d=google.com; s=arc-20160816; b=zQbzaDVtg0mgO+klQCp6rXBvWS7HRzcau64ij6AruN/Nv2O9F0SC10dLBKzKjzV4cm BPH48mUWlRw1RCGln5YONUqT+f8XiVUZijjbLb559Q+en17TE5WmjMSfKUqN9DHqa0GG 4/hkao2qa5L1La81xL+qFXR1y6AD2GaIj4dE3yRV5WIXtKALK+whuQcajSUMTox+oXYH TR88GAJyYQPFzmxz6ZTifERaAQJIBMvNl2pD/Ty5cHGvqMKTvKPtWI6L0+oMr5Zlffa2 mzQiN5uNRx2gB5GOGY4pNpW46HlrChzYcSUZXqRbvlw0MU7FWwE+2DUSY0gCbFwbKK5d /YaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=tN4V2XGQfSLF7xXwK0w0Hf97pkjIT9eRN853FGY+C8s=; fh=GF7GkLrIHJENbbWvQr5Knic5BHMSjR83F5wUWyuMrfw=; b=K7lE8I0OSAZHJaDR+ygqfoTNjmIZUbkEG0y2IfSeynHUsXiq9WTQTadU+4DzAumVRc 9+llNopKKIBpVWkNdiC5I/YidybnbaNNiOReiqZzgRoVifF0L/0Lsw5L8kVDF3TLzwNU Od4yMl2Uk/GWytTH7W1pCWRaT58WPWZPYYT6WA1Rqtz24jceW51j+0kYiUElMD4c/wIy ctN83Opqn39is/hmbBg2uqwIA9iu9MsuCre7nsFm9SyxSqdQyysMT5ke/vyyjk21QU40 kmqm63JFihBvXY2qyz0ejUjE/5vwLlQoEOBF0hOYJcwlK/sypb21u53RkoINTiQO1jXW fwFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JaKcEvpL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bq7-20020a056a000e0700b006762f8fe109si6477531pfb.0.2023.06.28.08.16.26; Wed, 28 Jun 2023 08:16:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JaKcEvpL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230265AbjF1OoQ (ORCPT <rfc822;ivan.orlov0322@gmail.com> + 99 others); Wed, 28 Jun 2023 10:44:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229789AbjF1OoJ (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 28 Jun 2023 10:44:09 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1920AA2; Wed, 28 Jun 2023 07:44:08 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 984396134E; Wed, 28 Jun 2023 14:44:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 29E79C433C0; Wed, 28 Jun 2023 14:44:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1687963446; bh=CftGqe7uSHGDIhGMtuKnY6LHqyYkAcjuQD1TCyu0PK0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JaKcEvpLCTUDffE/nH0JPp2K2Du1tThuKk5BM0oGygHn8wf+QK+vP6yssdtE5LrQQ BfCRurBIscWWv+hzIgI6oVxccIse66uxbcdp/oJFz1Ryh+TfrcaR45b26LIbczo+UQ nxqgPpKsl9Uy9VL2Mxl7+cl8Sgv01fdwHmTYpnCS4EbigCBFv9o8n8olJ/LtfmCjXH GXzGbAQffGUbLDmk7wniU6Gwg5l9SBTaJ8rEfPkiCD4rc4HXWwMHcMr/1IEL2wWj/F KdZX8/9b5rIILrUOI8mzMa6Un4sY5RVngrauCz4Ojju8FLEog8WnwgdbtJxsAS4asg 7K6YmX9KuwpQg== From: "Masami Hiramatsu (Google)" <mhiramat@kernel.org> To: Jiri Olsa <jolsa@kernel.org>, Steven Rostedt <rostedt@goodmis.org> Cc: Masami Hiramatsu <mhiramat@kernel.org>, Mark Rutland <mark.rutland@arm.com>, lkml <linux-kernel@vger.kernel.org>, linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH] fprobe: Ensure running fprobe_exit_handler() finished before calling rethook_free() Date: Wed, 28 Jun 2023 23:44:02 +0900 Message-Id: <168796344232.46347.7947681068822514750.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230628012305.978e34d44f1a53fe20327fde@kernel.org> References: <20230628012305.978e34d44f1a53fe20327fde@kernel.org> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769960007447654917?= X-GMAIL-MSGID: =?utf-8?q?1769960007447654917?= |
Series |
fprobe: Ensure running fprobe_exit_handler() finished before calling rethook_free()
|
|
Commit Message
Masami Hiramatsu (Google)
June 28, 2023, 2:44 p.m. UTC
From: Masami Hiramatsu (Google) <mhiramat@kernel.org> Ensure running fprobe_exit_handler() has finished before calling rethook_free() in the unregister_fprobe() so that caller can free the fprobe right after unregister_fprobe(). unregister_fprobe() ensured that all running fprobe_entry/exit_handler() have finished by calling unregister_ftrace_function() which synchronizes RCU. But commit 5f81018753df ("fprobe: Release rethook after the ftrace_ops is unregistered") changed to call rethook_free() after unregister_ftrace_function(). So call rethook_stop() to make rethook disabled before unregister_ftrace_function() and ensure it again. Fixes: 5f81018753df ("fprobe: Release rethook after the ftrace_ops is unregistered") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> --- include/linux/rethook.h | 1 + kernel/trace/fprobe.c | 3 +++ kernel/trace/rethook.c | 13 +++++++++++++ 3 files changed, 17 insertions(+)
Comments
On Wed, 28 Jun 2023 23:44:02 +0900 "Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote: > From: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > Ensure running fprobe_exit_handler() has finished before > calling rethook_free() in the unregister_fprobe() so that caller can free > the fprobe right after unregister_fprobe(). > > unregister_fprobe() ensured that all running fprobe_entry/exit_handler() > have finished by calling unregister_ftrace_function() which synchronizes > RCU. But commit 5f81018753df ("fprobe: Release rethook after the ftrace_ops > is unregistered") changed to call rethook_free() after > unregister_ftrace_function(). So call rethook_stop() to make rethook > disabled before unregister_ftrace_function() and ensure it again. > Steve, can you review this? without this fix, Jiri's patch may cause another timing issue. Thanks, > Fixes: 5f81018753df ("fprobe: Release rethook after the ftrace_ops is unregistered") > Cc: stable@vger.kernel.org > Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> > --- > include/linux/rethook.h | 1 + > kernel/trace/fprobe.c | 3 +++ > kernel/trace/rethook.c | 13 +++++++++++++ > 3 files changed, 17 insertions(+) > > diff --git a/include/linux/rethook.h b/include/linux/rethook.h > index c8ac1e5afcd1..bdbe6717f45a 100644 > --- a/include/linux/rethook.h > +++ b/include/linux/rethook.h > @@ -59,6 +59,7 @@ struct rethook_node { > }; > > struct rethook *rethook_alloc(void *data, rethook_handler_t handler); > +void rethook_stop(struct rethook *rh); > void rethook_free(struct rethook *rh); > void rethook_add_node(struct rethook *rh, struct rethook_node *node); > struct rethook_node *rethook_try_get(struct rethook *rh); > diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c > index 0121e8c0d54e..75517667b54f 100644 > --- a/kernel/trace/fprobe.c > +++ b/kernel/trace/fprobe.c > @@ -364,6 +364,9 @@ int unregister_fprobe(struct fprobe *fp) > fp->ops.saved_func != fprobe_kprobe_handler)) > return -EINVAL; > > + if (fp->rethook) > + rethook_stop(fp->rethook); > + > ret = unregister_ftrace_function(&fp->ops); > if (ret < 0) > return ret; > diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c > index 60f6cb2b486b..468006cce7ca 100644 > --- a/kernel/trace/rethook.c > +++ b/kernel/trace/rethook.c > @@ -53,6 +53,19 @@ static void rethook_free_rcu(struct rcu_head *head) > kfree(rh); > } > > +/** > + * rethook_stop() - Stop using a rethook. > + * @rh: the struct rethook to stop. > + * > + * Stop using a rethook to prepare for freeing it. If you want to wait for > + * all running rethook handler before calling rethook_free(), you need to > + * call this first and wait RCU, and call rethook_free(). > + */ > +void rethook_stop(struct rethook *rh) > +{ > + WRITE_ONCE(rh->handler, NULL); > +} > + > /** > * rethook_free() - Free struct rethook. > * @rh: the struct rethook to be freed. >
On Wed, 28 Jun 2023 23:44:02 +0900 "Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote: > From: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > Ensure running fprobe_exit_handler() has finished before > calling rethook_free() in the unregister_fprobe() so that caller can free > the fprobe right after unregister_fprobe(). > > unregister_fprobe() ensured that all running fprobe_entry/exit_handler() > have finished by calling unregister_ftrace_function() which synchronizes > RCU. But commit 5f81018753df ("fprobe: Release rethook after the > ftrace_ops is unregistered") changed to call rethook_free() after > unregister_ftrace_function(). So call rethook_stop() to make rethook > disabled before unregister_ftrace_function() and ensure it again. I'm confused. I still don't understand why it is bad to call unregister_ftrace_function() *before* rethook_free(). Can you show the race condition you are trying to avoid? -- Steve > > Fixes: 5f81018753df ("fprobe: Release rethook after the ftrace_ops is > unregistered") Cc: stable@vger.kernel.org > Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> > --- > include/linux/rethook.h | 1 + > kernel/trace/fprobe.c | 3 +++ > kernel/trace/rethook.c | 13 +++++++++++++ > 3 files changed, 17 insertions(+) > > diff --git a/include/linux/rethook.h b/include/linux/rethook.h > index c8ac1e5afcd1..bdbe6717f45a 100644 > --- a/include/linux/rethook.h > +++ b/include/linux/rethook.h > @@ -59,6 +59,7 @@ struct rethook_node { > }; > > struct rethook *rethook_alloc(void *data, rethook_handler_t handler); > +void rethook_stop(struct rethook *rh); > void rethook_free(struct rethook *rh); > void rethook_add_node(struct rethook *rh, struct rethook_node *node); > struct rethook_node *rethook_try_get(struct rethook *rh); > diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c > index 0121e8c0d54e..75517667b54f 100644 > --- a/kernel/trace/fprobe.c > +++ b/kernel/trace/fprobe.c > @@ -364,6 +364,9 @@ int unregister_fprobe(struct fprobe *fp) > fp->ops.saved_func != fprobe_kprobe_handler)) > return -EINVAL; > > + if (fp->rethook) > + rethook_stop(fp->rethook); > + > ret = unregister_ftrace_function(&fp->ops); > if (ret < 0) > return ret; > diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c > index 60f6cb2b486b..468006cce7ca 100644 > --- a/kernel/trace/rethook.c > +++ b/kernel/trace/rethook.c > @@ -53,6 +53,19 @@ static void rethook_free_rcu(struct rcu_head *head) > kfree(rh); > } > > +/** > + * rethook_stop() - Stop using a rethook. > + * @rh: the struct rethook to stop. > + * > + * Stop using a rethook to prepare for freeing it. If you want to wait > for > + * all running rethook handler before calling rethook_free(), you need to > + * call this first and wait RCU, and call rethook_free(). > + */ > +void rethook_stop(struct rethook *rh) > +{ > + WRITE_ONCE(rh->handler, NULL); > +} > + > /** > * rethook_free() - Free struct rethook. > * @rh: the struct rethook to be freed.
On Wed, 5 Jul 2023 21:26:57 -0400 Steven Rostedt <rostedt@goodmis.org> wrote: > On Wed, 28 Jun 2023 23:44:02 +0900 > "Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote: > > > From: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > > > Ensure running fprobe_exit_handler() has finished before > > calling rethook_free() in the unregister_fprobe() so that caller can free > > the fprobe right after unregister_fprobe(). > > > > unregister_fprobe() ensured that all running fprobe_entry/exit_handler() > > have finished by calling unregister_ftrace_function() which synchronizes > > RCU. But commit 5f81018753df ("fprobe: Release rethook after the > > ftrace_ops is unregistered") changed to call rethook_free() after > > unregister_ftrace_function(). So call rethook_stop() to make rethook > > disabled before unregister_ftrace_function() and ensure it again. > > I'm confused. I still don't understand why it is bad to call > unregister_ftrace_function() *before* rethook_free(). > > Can you show the race condition you are trying to avoid? Yes. This is ensuring all handlers exit when returning from unregister_fprobe() so that the caller can release the data which will be accessed from the handlers. The entry handler is safe because unregister_ftrace_function() waits for the ftrace handlers. But that is not enough for the exit handler. With only Jiri's patch, following flow can happen; ------ CPU1 CPU2 call unregister_fprobe() ... __fprobe_handler() rethook_hook() on probed function unregister_ftrace_function() return from probed function rethook hooks find rh->handler == fprobe_exit_handler call fprobe_exit_handler() rethook_free(): set rh->handler = NULL; return from unreigster_fprobe; call fp->exit_handler() <- (*) (*) In this point, the exit handler is called after returning from unregister_fprobe(). ------ So, this patch changes it as following; ------ CPU1 CPU2 call unregister_fprobe() ... rethook_stop(): set rh->handler = NULL; __fprobe_handler() rethook_hook() on probed function unregister_ftrace_function() return from probed function rethook hooks find rh->handler == NULL return from rethook rethook_free() return from unreigster_fprobe; ------ I can also just put a synchronize_sched_rcu() right after rethook_free() to wait for all running fprobe_exit_handler() too. Thank you, > > -- Steve > > > > > > > Fixes: 5f81018753df ("fprobe: Release rethook after the ftrace_ops is > > unregistered") Cc: stable@vger.kernel.org > > Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > --- > > include/linux/rethook.h | 1 + > > kernel/trace/fprobe.c | 3 +++ > > kernel/trace/rethook.c | 13 +++++++++++++ > > 3 files changed, 17 insertions(+) > > > > diff --git a/include/linux/rethook.h b/include/linux/rethook.h > > index c8ac1e5afcd1..bdbe6717f45a 100644 > > --- a/include/linux/rethook.h > > +++ b/include/linux/rethook.h > > @@ -59,6 +59,7 @@ struct rethook_node { > > }; > > > > struct rethook *rethook_alloc(void *data, rethook_handler_t handler); > > +void rethook_stop(struct rethook *rh); > > void rethook_free(struct rethook *rh); > > void rethook_add_node(struct rethook *rh, struct rethook_node *node); > > struct rethook_node *rethook_try_get(struct rethook *rh); > > diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c > > index 0121e8c0d54e..75517667b54f 100644 > > --- a/kernel/trace/fprobe.c > > +++ b/kernel/trace/fprobe.c > > @@ -364,6 +364,9 @@ int unregister_fprobe(struct fprobe *fp) > > fp->ops.saved_func != fprobe_kprobe_handler)) > > return -EINVAL; > > > > + if (fp->rethook) > > + rethook_stop(fp->rethook); > > + > > ret = unregister_ftrace_function(&fp->ops); > > if (ret < 0) > > return ret; > > diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c > > index 60f6cb2b486b..468006cce7ca 100644 > > --- a/kernel/trace/rethook.c > > +++ b/kernel/trace/rethook.c > > @@ -53,6 +53,19 @@ static void rethook_free_rcu(struct rcu_head *head) > > kfree(rh); > > } > > > > +/** > > + * rethook_stop() - Stop using a rethook. > > + * @rh: the struct rethook to stop. > > + * > > + * Stop using a rethook to prepare for freeing it. If you want to wait > > for > > + * all running rethook handler before calling rethook_free(), you need to > > + * call this first and wait RCU, and call rethook_free(). > > + */ > > +void rethook_stop(struct rethook *rh) > > +{ > > + WRITE_ONCE(rh->handler, NULL); > > +} > > + > > /** > > * rethook_free() - Free struct rethook. > > * @rh: the struct rethook to be freed. >
On Thu, 6 Jul 2023 14:10:12 +0900 Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote: > With only Jiri's patch, following flow can happen; > > ------ > CPU1 CPU2 > call unregister_fprobe() > ... > __fprobe_handler() > rethook_hook() on probed function > unregister_ftrace_function() > return from probed function > rethook hooks > find rh->handler == fprobe_exit_handler > call fprobe_exit_handler() > rethook_free(): > set rh->handler = NULL; > return from unreigster_fprobe; > call fp->exit_handler() <- (*) > > (*) In this point, the exit handler is called after returning from > unregister_fprobe(). > ------ > > So, this patch changes it as following; > ------ > CPU1 CPU2 > call unregister_fprobe() > ... > rethook_stop(): > set rh->handler = NULL; > __fprobe_handler() > rethook_hook() on probed function > unregister_ftrace_function() > return from probed function > rethook hooks > find rh->handler == NULL > return from rethook > rethook_free() > return from unreigster_fprobe; > ------ > > I can also just put a synchronize_sched_rcu() right after rethook_free() > to wait for all running fprobe_exit_handler() too. > This makes more sense. Can you please add the above to the change log. Thanks, -- Steve
On Thu, 6 Jul 2023 09:56:24 -0400 Steven Rostedt <rostedt@goodmis.org> wrote: > On Thu, 6 Jul 2023 14:10:12 +0900 > Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote: > > > With only Jiri's patch, following flow can happen; > > > > ------ > > CPU1 CPU2 > > call unregister_fprobe() > > ... > > __fprobe_handler() > > rethook_hook() on probed function > > unregister_ftrace_function() > > return from probed function > > rethook hooks > > find rh->handler == fprobe_exit_handler > > call fprobe_exit_handler() > > rethook_free(): > > set rh->handler = NULL; > > return from unreigster_fprobe; > > call fp->exit_handler() <- (*) > > > > (*) In this point, the exit handler is called after returning from > > unregister_fprobe(). > > ------ > > > > So, this patch changes it as following; > > ------ > > CPU1 CPU2 > > call unregister_fprobe() > > ... > > rethook_stop(): > > set rh->handler = NULL; > > __fprobe_handler() > > rethook_hook() on probed function > > unregister_ftrace_function() > > return from probed function > > rethook hooks > > find rh->handler == NULL > > return from rethook > > rethook_free() > > return from unreigster_fprobe; > > ------ > > > > I can also just put a synchronize_sched_rcu() right after rethook_free() > > to wait for all running fprobe_exit_handler() too. > > > > This makes more sense. Can you please add the above to the change log. OK, let me update it. Thanks! > > Thanks, > > -- Steve
diff --git a/include/linux/rethook.h b/include/linux/rethook.h index c8ac1e5afcd1..bdbe6717f45a 100644 --- a/include/linux/rethook.h +++ b/include/linux/rethook.h @@ -59,6 +59,7 @@ struct rethook_node { }; struct rethook *rethook_alloc(void *data, rethook_handler_t handler); +void rethook_stop(struct rethook *rh); void rethook_free(struct rethook *rh); void rethook_add_node(struct rethook *rh, struct rethook_node *node); struct rethook_node *rethook_try_get(struct rethook *rh); diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 0121e8c0d54e..75517667b54f 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -364,6 +364,9 @@ int unregister_fprobe(struct fprobe *fp) fp->ops.saved_func != fprobe_kprobe_handler)) return -EINVAL; + if (fp->rethook) + rethook_stop(fp->rethook); + ret = unregister_ftrace_function(&fp->ops); if (ret < 0) return ret; diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c index 60f6cb2b486b..468006cce7ca 100644 --- a/kernel/trace/rethook.c +++ b/kernel/trace/rethook.c @@ -53,6 +53,19 @@ static void rethook_free_rcu(struct rcu_head *head) kfree(rh); } +/** + * rethook_stop() - Stop using a rethook. + * @rh: the struct rethook to stop. + * + * Stop using a rethook to prepare for freeing it. If you want to wait for + * all running rethook handler before calling rethook_free(), you need to + * call this first and wait RCU, and call rethook_free(). + */ +void rethook_stop(struct rethook *rh) +{ + WRITE_ONCE(rh->handler, NULL); +} + /** * rethook_free() - Free struct rethook. * @rh: the struct rethook to be freed.