Skip to content

Commit a1461f1

Browse files
committed
rethook: Use __rcu pointer for rethook::handler
Since the rethook::handler is an RCU-maganged pointer so that it will notice readers the rethook is stopped (unregistered) or not, it should be an __rcu pointer and use appropriate functions to be accessed. This will use appropriate memory barrier when accessing it. OTOH, rethook::data is never changed, so we don't need to check it in get_kretprobe(). NOTE: To avoid sparse warning, rethook::handler is defined by a raw function pointer type with __rcu instead of rethook_handler_t. Link: https://lore.kernel.org/all/170126066201.398836.837498688669005979.stgit@devnote2/ Fixes: 54ecbe6 ("rethook: Add a generic return hook") Cc: stable@vger.kernel.org Reported-by: kernel test robot <lkp@intel.com> Closes: https://lore.kernel.org/oe-kbuild-all/202311241808.rv9ceuAh-lkp@intel.com/ Tested-by: JP Kobryn <inwardvessel@gmail.com> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
1 parent d839a65 commit a1461f1

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

include/linux/kprobes.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,8 @@ extern int arch_trampoline_kprobe(struct kprobe *p);
197197
#ifdef CONFIG_KRETPROBE_ON_RETHOOK
198198
static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
199199
{
200-
RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
201-
"Kretprobe is accessed from instance under preemptive context");
202-
203-
return (struct kretprobe *)READ_ONCE(ri->node.rethook->data);
200+
/* rethook::data is non-changed field, so that you can access it freely. */
201+
return (struct kretprobe *)ri->node.rethook->data;
204202
}
205203
static nokprobe_inline unsigned long get_kretprobe_retaddr(struct kretprobe_instance *ri)
206204
{

include/linux/rethook.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ typedef void (*rethook_handler_t) (struct rethook_node *, void *, unsigned long,
2828
*/
2929
struct rethook {
3030
void *data;
31-
rethook_handler_t handler;
31+
/*
32+
* To avoid sparse warnings, this uses a raw function pointer with
33+
* __rcu, instead of rethook_handler_t. But this must be same as
34+
* rethook_handler_t.
35+
*/
36+
void (__rcu *handler) (struct rethook_node *, void *, unsigned long, struct pt_regs *);
3237
struct objpool_head pool;
3338
struct rcu_head rcu;
3439
};

kernel/trace/rethook.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static void rethook_free_rcu(struct rcu_head *head)
4848
*/
4949
void rethook_stop(struct rethook *rh)
5050
{
51-
WRITE_ONCE(rh->handler, NULL);
51+
rcu_assign_pointer(rh->handler, NULL);
5252
}
5353

5454
/**
@@ -63,7 +63,7 @@ void rethook_stop(struct rethook *rh)
6363
*/
6464
void rethook_free(struct rethook *rh)
6565
{
66-
WRITE_ONCE(rh->handler, NULL);
66+
rethook_stop(rh);
6767

6868
call_rcu(&rh->rcu, rethook_free_rcu);
6969
}
@@ -82,6 +82,12 @@ static int rethook_fini_pool(struct objpool_head *head, void *context)
8282
return 0;
8383
}
8484

85+
static inline rethook_handler_t rethook_get_handler(struct rethook *rh)
86+
{
87+
return (rethook_handler_t)rcu_dereference_check(rh->handler,
88+
rcu_read_lock_any_held());
89+
}
90+
8591
/**
8692
* rethook_alloc() - Allocate struct rethook.
8793
* @data: a data to pass the @handler when hooking the return.
@@ -107,7 +113,7 @@ struct rethook *rethook_alloc(void *data, rethook_handler_t handler,
107113
return ERR_PTR(-ENOMEM);
108114

109115
rh->data = data;
110-
rh->handler = handler;
116+
rcu_assign_pointer(rh->handler, handler);
111117

112118
/* initialize the objpool for rethook nodes */
113119
if (objpool_init(&rh->pool, num, size, GFP_KERNEL, rh,
@@ -135,9 +141,10 @@ static void free_rethook_node_rcu(struct rcu_head *head)
135141
*/
136142
void rethook_recycle(struct rethook_node *node)
137143
{
138-
lockdep_assert_preemption_disabled();
144+
rethook_handler_t handler;
139145

140-
if (likely(READ_ONCE(node->rethook->handler)))
146+
handler = rethook_get_handler(node->rethook);
147+
if (likely(handler))
141148
objpool_push(node, &node->rethook->pool);
142149
else
143150
call_rcu(&node->rcu, free_rethook_node_rcu);
@@ -153,9 +160,7 @@ NOKPROBE_SYMBOL(rethook_recycle);
153160
*/
154161
struct rethook_node *rethook_try_get(struct rethook *rh)
155162
{
156-
rethook_handler_t handler = READ_ONCE(rh->handler);
157-
158-
lockdep_assert_preemption_disabled();
163+
rethook_handler_t handler = rethook_get_handler(rh);
159164

160165
/* Check whether @rh is going to be freed. */
161166
if (unlikely(!handler))
@@ -300,7 +305,7 @@ unsigned long rethook_trampoline_handler(struct pt_regs *regs,
300305
rhn = container_of(first, struct rethook_node, llist);
301306
if (WARN_ON_ONCE(rhn->frame != frame))
302307
break;
303-
handler = READ_ONCE(rhn->rethook->handler);
308+
handler = rethook_get_handler(rhn->rethook);
304309
if (handler)
305310
handler(rhn, rhn->rethook->data,
306311
correct_ret_addr, regs);

0 commit comments

Comments
 (0)