From patchwork Wed Jun 28 14:44:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 13295883 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 7C9E5290A for ; 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)" To: Jiri Olsa , Steven Rostedt Cc: Masami Hiramatsu , Mark Rutland , lkml , 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 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Masami Hiramatsu (Google) 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) --- 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.