Skip to content

Commit

Permalink
uprobes: Decouple return_instance list traversal and freeing
Browse files Browse the repository at this point in the history
free_ret_instance() has two unrelated responsibilities: actually
cleaning up return_instance's resources and freeing memory, and also
helping with utask->return_instances list traversal by returning the
next alive pointer.

There is no reason why these two aspects have to be mixed together, so
turn free_ret_instance() into void-returning function and make callers
do list traversal on their own.

We'll use this simplification in the next patch that will guarantee that
to-be-freed return_instance isn't reachable from utask->return_instances
list.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Link: https://lore.kernel.org/r/20241206002417.3295533-3-andrii@kernel.org
  • Loading branch information
Andrii Nakryiko authored and Ingo Molnar committed Dec 9, 2024
1 parent 2ff913a commit 636666a
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions kernel/events/uprobes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1888,10 +1888,8 @@ unsigned long uprobe_get_trap_addr(struct pt_regs *regs)
return instruction_pointer(regs);
}

static struct return_instance *free_ret_instance(struct return_instance *ri, bool cleanup_hprobe)
static void free_ret_instance(struct return_instance *ri, bool cleanup_hprobe)
{
struct return_instance *next = ri->next;

if (cleanup_hprobe) {
enum hprobe_state hstate;

Expand All @@ -1901,7 +1899,6 @@ static struct return_instance *free_ret_instance(struct return_instance *ri, boo

kfree(ri->extra_consumers);
kfree_rcu(ri, rcu);
return next;
}

/*
Expand All @@ -1911,7 +1908,7 @@ static struct return_instance *free_ret_instance(struct return_instance *ri, boo
void uprobe_free_utask(struct task_struct *t)
{
struct uprobe_task *utask = t->utask;
struct return_instance *ri;
struct return_instance *ri, *ri_next;

if (!utask)
return;
Expand All @@ -1921,8 +1918,11 @@ void uprobe_free_utask(struct task_struct *t)
timer_delete_sync(&utask->ri_timer);

ri = utask->return_instances;
while (ri)
ri = free_ret_instance(ri, true /* cleanup_hprobe */);
while (ri) {
ri_next = ri->next;
free_ret_instance(ri, true /* cleanup_hprobe */);
ri = ri_next;
}

kfree(utask);
t->utask = NULL;
Expand Down Expand Up @@ -2111,12 +2111,15 @@ unsigned long uprobe_get_trampoline_vaddr(void)
static void cleanup_return_instances(struct uprobe_task *utask, bool chained,
struct pt_regs *regs)
{
struct return_instance *ri = utask->return_instances;
struct return_instance *ri = utask->return_instances, *ri_next;
enum rp_check ctx = chained ? RP_CHECK_CHAIN_CALL : RP_CHECK_CALL;

while (ri && !arch_uretprobe_is_alive(ri, ctx, regs)) {
ri = free_ret_instance(ri, true /* cleanup_hprobe */);
ri_next = ri->next;
utask->depth--;

free_ret_instance(ri, true /* cleanup_hprobe */);
ri = ri_next;
}
rcu_assign_pointer(utask->return_instances, ri);
}
Expand Down Expand Up @@ -2508,7 +2511,7 @@ static struct return_instance *find_next_ret_chain(struct return_instance *ri)
void uprobe_handle_trampoline(struct pt_regs *regs)
{
struct uprobe_task *utask;
struct return_instance *ri, *next;
struct return_instance *ri, *ri_next, *next_chain;
struct uprobe *uprobe;
enum hprobe_state hstate;
bool valid;
Expand All @@ -2528,8 +2531,8 @@ void uprobe_handle_trampoline(struct pt_regs *regs)
* or NULL; the latter case means that nobody but ri->func
* could hit this trampoline on return. TODO: sigaltstack().
*/
next = find_next_ret_chain(ri);
valid = !next || arch_uretprobe_is_alive(next, RP_CHECK_RET, regs);
next_chain = find_next_ret_chain(ri);
valid = !next_chain || arch_uretprobe_is_alive(next_chain, RP_CHECK_RET, regs);

instruction_pointer_set(regs, ri->orig_ret_vaddr);
do {
Expand All @@ -2541,17 +2544,19 @@ void uprobe_handle_trampoline(struct pt_regs *regs)
* trampoline addresses on the stack are replaced with correct
* original return addresses
*/
rcu_assign_pointer(utask->return_instances, ri->next);
ri_next = ri->next;
rcu_assign_pointer(utask->return_instances, ri_next);
utask->depth--;

uprobe = hprobe_consume(&ri->hprobe, &hstate);
if (valid)
handle_uretprobe_chain(ri, uprobe, regs);
hprobe_finalize(&ri->hprobe, hstate);

/* We already took care of hprobe, no need to waste more time on that. */
ri = free_ret_instance(ri, false /* !cleanup_hprobe */);
utask->depth--;
} while (ri != next);
free_ret_instance(ri, false /* !cleanup_hprobe */);
ri = ri_next;
} while (ri != next_chain);
} while (!valid);

return;
Expand Down

0 comments on commit 636666a

Please sign in to comment.