[PATCH 09/10] perf/uprobe: Convert single-step and uretprobe to SRCU

From: Peter Zijlstra
Date: Mon Jul 08 2024 - 05:30:55 EST


Both single-step and uretprobes take a refcount on struct uprobe in
handle_swbp() in order to ensure struct uprobe stays extant until a
next trap.

Since uprobe_unregister() only cares about the uprobe_consumer
life-time, and these intra-trap sections can be arbitrarily large,
create a second SRCU domain to cover these.

Notably, a uretprobe with a registered return_instance that never
triggers -- because userspace -- will currently pin the
return_instance and related uprobe until the task dies. With this
convertion to SRCU this behaviour will inhibit freeing of all uprobes.

Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
---
include/linux/uprobes.h | 2 ++
kernel/events/uprobes.c | 23 ++++++++++++++++-------
2 files changed, 18 insertions(+), 7 deletions(-)

--- a/include/linux/uprobes.h
+++ b/include/linux/uprobes.h
@@ -78,6 +78,7 @@ struct uprobe_task {

struct return_instance *return_instances;
unsigned int depth;
+ unsigned int active_srcu_idx;
};

struct return_instance {
@@ -86,6 +87,7 @@ struct return_instance {
unsigned long stack; /* stack pointer */
unsigned long orig_ret_vaddr; /* original return address */
bool chained; /* true, if instance is nested */
+ int srcu_idx;

struct return_instance *next; /* keep as stack */
};
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -51,6 +51,7 @@ static struct mutex uprobes_mmap_mutex[U
DEFINE_STATIC_PERCPU_RWSEM(dup_mmap_sem);

DEFINE_STATIC_SRCU(uprobes_srcu);
+DEFINE_STATIC_SRCU(uretprobes_srcu);

/* Have a copy of original instruction */
#define UPROBE_COPY_INSN 0
@@ -598,12 +599,18 @@ static struct uprobe *get_uprobe(struct
return uprobe;
}

-static void uprobe_free_rcu(struct rcu_head *rcu)
+static void uprobe_free_stage2(struct rcu_head *rcu)
{
struct uprobe *uprobe = container_of(rcu, struct uprobe, rcu);
kfree(uprobe);
}

+static void uprobe_free_stage1(struct rcu_head *rcu)
+{
+ struct uprobe *uprobe = container_of(rcu, struct uprobe, rcu);
+ call_srcu(&uretprobes_srcu, &uprobe->rcu, uprobe_free_stage2);
+}
+
static void put_uprobe(struct uprobe *uprobe)
{
if (refcount_dec_and_test(&uprobe->ref)) {
@@ -615,7 +622,7 @@ static void put_uprobe(struct uprobe *up
mutex_lock(&delayed_uprobe_lock);
delayed_uprobe_remove(uprobe, NULL);
mutex_unlock(&delayed_uprobe_lock);
- call_srcu(&uprobes_srcu, &uprobe->rcu, uprobe_free_rcu);
+ call_srcu(&uprobes_srcu, &uprobe->rcu, uprobe_free_stage1);
}
}

@@ -1735,7 +1742,7 @@ unsigned long uprobe_get_trap_addr(struc
static struct return_instance *free_ret_instance(struct return_instance *ri)
{
struct return_instance *next = ri->next;
- put_uprobe(ri->uprobe);
+ srcu_read_unlock(&uretprobes_srcu, ri->srcu_idx);
kfree(ri);
return next;
}
@@ -1753,7 +1760,7 @@ void uprobe_free_utask(struct task_struc
return;

if (utask->active_uprobe)
- put_uprobe(utask->active_uprobe);
+ srcu_read_unlock(&uretprobes_srcu, utask->active_srcu_idx);

ri = utask->return_instances;
while (ri)
@@ -1796,7 +1803,7 @@ static int dup_utask(struct task_struct
return -ENOMEM;

*n = *o;
- get_uprobe(n->uprobe);
+ __srcu_clone_read_lock(&uretprobes_srcu, n->srcu_idx);
n->next = NULL;

*p = n;
@@ -1939,7 +1946,8 @@ static void prepare_uretprobe(struct upr
orig_ret_vaddr = utask->return_instances->orig_ret_vaddr;
}

- ri->uprobe = get_uprobe(uprobe);
+ ri->srcu_idx = srcu_read_lock(&uretprobes_srcu);
+ ri->uprobe = uprobe;
ri->func = instruction_pointer(regs);
ri->stack = user_stack_pointer(regs);
ri->orig_ret_vaddr = orig_ret_vaddr;
@@ -1979,7 +1987,8 @@ pre_ssout(struct uprobe *uprobe, struct
return err;
}

- utask->active_uprobe = get_uprobe(uprobe);
+ utask->active_srcu_idx = srcu_read_lock(&uretprobes_srcu);
+ utask->active_uprobe = uprobe;
utask->state = UTASK_SSTEP;
return 0;
}