[PATCH] uprobe: add UPROBE_ALTER_PC flag for uprobe handlers

From: luolongjun
Date: Fri Sep 16 2022 - 03:31:10 EST


From: Longjun Luo <luolongjun@xxxxxxxxxx>

Within uprobe handlers, the pc register could be
modified. In this situation, there is no need to
do a single stepping. Just like the kprobe, we
skip it.

Signed-off-by: Longjun Luo <luolongjun@xxxxxxxxxx>
---
include/linux/uprobes.h | 5 +++--
kernel/events/uprobes.c | 16 +++++++++++++---
2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h
index f46e0ca0169c..0670fecbe1ce 100644
--- a/include/linux/uprobes.h
+++ b/include/linux/uprobes.h
@@ -22,8 +22,9 @@ struct inode;
struct notifier_block;
struct page;

-#define UPROBE_HANDLER_REMOVE 1
-#define UPROBE_HANDLER_MASK 1
+#define UPROBE_HANDLER_REMOVE 0x1
+#define UPROBE_ALTER_PC 0x2
+#define UPROBE_HANDLER_MASK 0x3 /* (UPROBE_HANDLER_REMOVE | UPROBE_ALTER_PC) */

#define MAX_URETPROBE_DEPTH 64

diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index 2eaa327f8158..d01a668fecae 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -2064,10 +2064,16 @@ static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
return uprobe;
}

-static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
+/*
+ * The return value of handler_chain tags events that happen during
+ * calling handlers. If UPROBE_ALTER_PC happens, we must skip the
+ * single stepping.
+ */
+static int handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
{
struct uprobe_consumer *uc;
int remove = UPROBE_HANDLER_REMOVE;
+ int all_events = 0;
bool need_prep = false; /* prepare return uprobe, when needed */

down_read(&uprobe->register_rwsem);
@@ -2084,6 +2090,7 @@ static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
need_prep = true;

remove &= rc;
+ all_events |= rc;
}

if (need_prep && !remove)
@@ -2094,6 +2101,7 @@ static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
unapply_uprobe(uprobe, current->mm);
}
up_read(&uprobe->register_rwsem);
+ return all_events;
}

static void
@@ -2183,7 +2191,7 @@ static void handle_swbp(struct pt_regs *regs)
{
struct uprobe *uprobe;
unsigned long bp_vaddr;
- int is_swbp;
+ int is_swbp, all_events;

bp_vaddr = uprobe_get_swbp_addr(regs);
if (bp_vaddr == get_trampoline_vaddr())
@@ -2235,7 +2243,9 @@ static void handle_swbp(struct pt_regs *regs)
if (arch_uprobe_ignore(&uprobe->arch, regs))
goto out;

- handler_chain(uprobe, regs);
+ all_events = handler_chain(uprobe, regs);
+ if (all_events & UPROBE_ALTER_PC)
+ goto out;

if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
goto out;
--
2.37.3