Skip to content

Commit 7f41350

Browse files
committed
fprobe: Ensure running fprobe_exit_handler() finished before calling rethook_free()
JIRA: https://issues.redhat.com/browse/RHEL-26131 commit 195b9cb Author: Masami Hiramatsu (Google) <mhiramat@kernel.org> Date: Fri Jul 7 23:03:19 2023 +0900 fprobe: Ensure running fprobe_exit_handler() finished before calling rethook_free() 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 5f81018 ("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. Here is the possible code flow that can call the exit handler after unregister_fprobe(). ------ CPU1 CPU2 call unregister_fprobe(fp) ... __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() <- (*) ------ (*) At this point, the exit handler is called after returning from unregister_fprobe(). This fixes 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; ------ Link: https://lore.kernel.org/all/168873859949.156157.13039240432299335849.stgit@devnote2/ Fixes: 5f81018 ("fprobe: Release rethook after the ftrace_ops is unregistered") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org> Signed-off-by: Viktor Malik <vmalik@redhat.com>
1 parent 7381f24 commit 7f41350

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

include/linux/rethook.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct rethook_node {
5959
};
6060

6161
struct rethook *rethook_alloc(void *data, rethook_handler_t handler);
62+
void rethook_stop(struct rethook *rh);
6263
void rethook_free(struct rethook *rh);
6364
void rethook_add_node(struct rethook *rh, struct rethook_node *node);
6465
struct rethook_node *rethook_try_get(struct rethook *rh);

kernel/trace/fprobe.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ int unregister_fprobe(struct fprobe *fp)
349349
fp->ops.saved_func != fprobe_kprobe_handler))
350350
return -EINVAL;
351351

352+
if (fp->rethook)
353+
rethook_stop(fp->rethook);
354+
352355
ret = unregister_ftrace_function(&fp->ops);
353356
if (ret < 0)
354357
return ret;

kernel/trace/rethook.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ static void rethook_free_rcu(struct rcu_head *head)
5353
kfree(rh);
5454
}
5555

56+
/**
57+
* rethook_stop() - Stop using a rethook.
58+
* @rh: the struct rethook to stop.
59+
*
60+
* Stop using a rethook to prepare for freeing it. If you want to wait for
61+
* all running rethook handler before calling rethook_free(), you need to
62+
* call this first and wait RCU, and call rethook_free().
63+
*/
64+
void rethook_stop(struct rethook *rh)
65+
{
66+
WRITE_ONCE(rh->handler, NULL);
67+
}
68+
5669
/**
5770
* rethook_free() - Free struct rethook.
5871
* @rh: the struct rethook to be freed.

0 commit comments

Comments
 (0)