From: Jiri Olsa <jolsa@kernel.org>
To: Oleg Nesterov <oleg@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Andrii Nakryiko <andrii@kernel.org>
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-trace-kernel@vger.kernel.org, x86@kernel.org,
"Song Liu" <songliubraving@fb.com>, "Yonghong Song" <yhs@fb.com>,
"John Fastabend" <john.fastabend@gmail.com>,
"Hao Luo" <haoluo@google.com>,
"Steven Rostedt" <rostedt@goodmis.org>,
"Masami Hiramatsu" <mhiramat@kernel.org>,
"Alan Maguire" <alan.maguire@oracle.com>,
"David Laight" <David.Laight@ACULAB.COM>,
"Thomas Weißschuh" <thomas@t-8ch.de>
Subject: [PATCH RFCv3 09/23] uprobes/x86: Add mapping for optimized uprobe trampolines
Date: Thu, 20 Mar 2025 12:41:44 +0100 [thread overview]
Message-ID: <20250320114200.14377-10-jolsa@kernel.org> (raw)
In-Reply-To: <20250320114200.14377-1-jolsa@kernel.org>
Adding support to add special mapping for for user space trampoline
with following functions:
uprobe_trampoline_get - find or add related uprobe_trampoline
uprobe_trampoline_put - remove ref or destroy uprobe_trampoline
The user space trampoline is exported as architecture specific user space
special mapping, which is provided by arch_uprobe_trampoline_mapping
function.
The uprobe trampoline needs to be callable/reachable from the probe address,
so while searching for available address we use uprobe_is_callable function
to decide if the uprobe trampoline is callable from the probe address.
All uprobe_trampoline objects are stored in uprobes_state object and are
cleaned up when the process mm_struct goes down. Adding new arch hooks
for that, because this change is x86_64 specific.
Locking is provided by callers in following changes.
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
arch/x86/kernel/uprobes.c | 123 ++++++++++++++++++++++++++++++++++++++
include/linux/uprobes.h | 6 ++
kernel/events/uprobes.c | 10 ++++
kernel/fork.c | 1 +
4 files changed, 140 insertions(+)
diff --git a/arch/x86/kernel/uprobes.c b/arch/x86/kernel/uprobes.c
index b11dcd47edaa..5ee2cce4c63e 100644
--- a/arch/x86/kernel/uprobes.c
+++ b/arch/x86/kernel/uprobes.c
@@ -742,6 +742,129 @@ static void riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
*sr = utask->autask.saved_scratch_register;
}
}
+
+struct uprobe_trampoline {
+ struct hlist_node node;
+ unsigned long vaddr;
+ atomic64_t ref;
+};
+
+static bool is_reachable_by_call(unsigned long vtramp, unsigned long vaddr)
+{
+ long delta = (long)(vaddr + 5 - vtramp);
+
+ return delta >= INT_MIN && delta <= INT_MAX;
+}
+
+static unsigned long find_nearest_page(unsigned long vaddr)
+{
+ struct vm_area_struct *vma, *prev = NULL;
+ unsigned long prev_vm_end = PAGE_SIZE;
+ VMA_ITERATOR(vmi, current->mm, 0);
+
+ vma = vma_next(&vmi);
+ while (vma) {
+ if (prev)
+ prev_vm_end = prev->vm_end;
+ if (vma->vm_start - prev_vm_end >= PAGE_SIZE) {
+ if (is_reachable_by_call(prev_vm_end, vaddr))
+ return prev_vm_end;
+ if (is_reachable_by_call(vma->vm_start - PAGE_SIZE, vaddr))
+ return vma->vm_start - PAGE_SIZE;
+ }
+ prev = vma;
+ vma = vma_next(&vmi);
+ }
+
+ return 0;
+}
+
+static struct uprobe_trampoline *create_uprobe_trampoline(unsigned long vaddr)
+{
+ struct pt_regs *regs = task_pt_regs(current);
+ const struct vm_special_mapping *mapping;
+ struct mm_struct *mm = current->mm;
+ struct vm_area_struct *vma;
+ struct uprobe_trampoline *tramp;
+
+ mapping = user_64bit_mode(regs) ? &tramp_mapping : NULL;
+ if (!mapping)
+ return NULL;
+
+ vaddr = find_nearest_page(vaddr);
+ if (!vaddr)
+ return NULL;
+
+ tramp = kzalloc(sizeof(*tramp), GFP_KERNEL);
+ if (unlikely(!tramp))
+ return NULL;
+
+ atomic64_set(&tramp->ref, 1);
+ tramp->vaddr = vaddr;
+
+ vma = _install_special_mapping(mm, tramp->vaddr, PAGE_SIZE,
+ VM_READ|VM_EXEC|VM_MAYEXEC|VM_MAYREAD|VM_DONTCOPY|VM_IO,
+ mapping);
+ if (IS_ERR(vma))
+ goto free_area;
+ return tramp;
+
+ free_area:
+ kfree(tramp);
+ return NULL;
+}
+
+__maybe_unused
+static struct uprobe_trampoline *uprobe_trampoline_get(unsigned long vaddr)
+{
+ struct uprobes_state *state = ¤t->mm->uprobes_state;
+ struct uprobe_trampoline *tramp = NULL;
+
+ hlist_for_each_entry(tramp, &state->head_tramps, node) {
+ if (is_reachable_by_call(tramp->vaddr, vaddr)) {
+ atomic64_inc(&tramp->ref);
+ return tramp;
+ }
+ }
+
+ tramp = create_uprobe_trampoline(vaddr);
+ if (!tramp)
+ return NULL;
+
+ hlist_add_head(&tramp->node, &state->head_tramps);
+ return tramp;
+}
+
+static void destroy_uprobe_trampoline(struct uprobe_trampoline *tramp)
+{
+ hlist_del(&tramp->node);
+ kfree(tramp);
+}
+
+__maybe_unused
+static void uprobe_trampoline_put(struct uprobe_trampoline *tramp)
+{
+ if (tramp == NULL)
+ return;
+
+ if (atomic64_dec_and_test(&tramp->ref))
+ destroy_uprobe_trampoline(tramp);
+}
+
+void arch_uprobe_init_state(struct mm_struct *mm)
+{
+ INIT_HLIST_HEAD(&mm->uprobes_state.head_tramps);
+}
+
+void arch_uprobe_clear_state(struct mm_struct *mm)
+{
+ struct uprobes_state *state = &mm->uprobes_state;
+ struct uprobe_trampoline *tramp;
+ struct hlist_node *n;
+
+ hlist_for_each_entry_safe(tramp, n, &state->head_tramps, node)
+ destroy_uprobe_trampoline(tramp);
+}
#else /* 32-bit: */
/*
* No RIP-relative addressing on 32-bit
diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h
index 4a2b950beefd..7bde68871150 100644
--- a/include/linux/uprobes.h
+++ b/include/linux/uprobes.h
@@ -17,6 +17,7 @@
#include <linux/wait.h>
#include <linux/timer.h>
#include <linux/seqlock.h>
+#include <linux/mutex.h>
struct uprobe;
struct vm_area_struct;
@@ -185,6 +186,9 @@ struct xol_area;
struct uprobes_state {
struct xol_area *xol_area;
+#ifdef CONFIG_X86_64
+ struct hlist_head head_tramps;
+#endif
};
typedef int (*uprobe_write_verify_t)(struct page *page, unsigned long vaddr, uprobe_opcode_t *opcode, int nbytes);
@@ -233,6 +237,8 @@ extern void *arch_uretprobe_trampoline(unsigned long *psize);
extern unsigned long uprobe_get_trampoline_vaddr(void);
extern void uprobe_copy_from_page(struct page *page, unsigned long vaddr, void *dst, int len);
extern void handle_syscall_uprobe(struct pt_regs *regs, unsigned long bp_vaddr);
+extern void arch_uprobe_clear_state(struct mm_struct *mm);
+extern void arch_uprobe_init_state(struct mm_struct *mm);
#else /* !CONFIG_UPROBES */
struct uprobes_state {
};
diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index 4de04075576c..9370df47ec71 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -1793,6 +1793,14 @@ static struct xol_area *get_xol_area(void)
return area;
}
+void __weak arch_uprobe_clear_state(struct mm_struct *mm)
+{
+}
+
+void __weak arch_uprobe_init_state(struct mm_struct *mm)
+{
+}
+
/*
* uprobe_clear_state - Free the area allocated for slots.
*/
@@ -1804,6 +1812,8 @@ void uprobe_clear_state(struct mm_struct *mm)
delayed_uprobe_remove(NULL, mm);
mutex_unlock(&delayed_uprobe_lock);
+ arch_uprobe_clear_state(mm);
+
if (!area)
return;
diff --git a/kernel/fork.c b/kernel/fork.c
index e27fe5d5a15c..36a0f073b913 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1260,6 +1260,7 @@ static void mm_init_uprobes_state(struct mm_struct *mm)
{
#ifdef CONFIG_UPROBES
mm->uprobes_state.xol_area = NULL;
+ arch_uprobe_init_state(mm);
#endif
}
--
2.49.0
next prev parent reply other threads:[~2025-03-20 11:43 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-20 11:41 [PATCH RFCv3 00/23] uprobes: Add support to optimize usdt probes on x86_64 Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 01/23] uprobes: Rename arch_uretprobe_trampoline function Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 02/23] uprobes: Make copy_from_page global Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 03/23] uprobes: Move ref_ctr_offset update out of uprobe_write_opcode Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 04/23] uprobes: Add uprobe_write function Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 05/23] uprobes: Add nbytes argument to uprobe_write_opcode Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 06/23] uprobes: Add orig argument to uprobe_write and uprobe_write_opcode Jiri Olsa
2025-04-04 20:33 ` Andrii Nakryiko
2025-04-07 11:13 ` Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 07/23] uprobes: Remove breakpoint in unapply_uprobe under mmap_write_lock Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 08/23] uprobes/x86: Add uprobe syscall to speed up uprobe Jiri Olsa
2025-04-04 20:33 ` Andrii Nakryiko
2025-04-07 10:58 ` Jiri Olsa
2025-03-20 11:41 ` Jiri Olsa [this message]
2025-03-20 11:41 ` [PATCH RFCv3 10/23] uprobes/x86: Add support to emulate nop5 instruction Jiri Olsa
2025-04-04 20:33 ` Andrii Nakryiko
2025-04-07 11:07 ` Jiri Olsa
2025-04-08 20:21 ` Jiri Olsa
2025-04-09 18:19 ` Andrii Nakryiko
2025-04-11 12:18 ` Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 11/23] uprobes/x86: Add support to optimize uprobes Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 12/23] selftests/bpf: Use 5-byte nop for x86 usdt probes Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 13/23] selftests/bpf: Reorg the uprobe_syscall test function Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 14/23] selftests/bpf: Rename uprobe_syscall_executed prog to test_uretprobe_multi Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 15/23] selftests/bpf: Add uprobe/usdt syscall tests Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 16/23] selftests/bpf: Add hit/attach/detach race optimized uprobe test Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 17/23] selftests/bpf: Add uprobe syscall sigill signal test Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 18/23] selftests/bpf: Add optimized usdt variant for basic usdt test Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 19/23] selftests/bpf: Add uprobe_regs_equal test Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 20/23] selftests/bpf: Change test_uretprobe_regs_change for uprobe and uretprobe Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 21/23] selftests/bpf: Add 5-byte nop uprobe trigger bench Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 22/23] seccomp: passthrough uprobe systemcall without filtering Jiri Olsa
2025-03-20 11:41 ` [PATCH RFCv3 23/23] selftests/seccomp: validate uprobe syscall passes through seccomp Jiri Olsa
2025-03-20 12:23 ` [PATCH RFCv3 00/23] uprobes: Add support to optimize usdt probes on x86_64 Oleg Nesterov
2025-03-20 13:51 ` Jiri Olsa
2025-04-04 20:36 ` Andrii Nakryiko
2025-04-07 11:17 ` Jiri Olsa
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250320114200.14377-10-jolsa@kernel.org \
--to=jolsa@kernel.org \
--cc=David.Laight@ACULAB.COM \
--cc=alan.maguire@oracle.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mhiramat@kernel.org \
--cc=oleg@redhat.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=songliubraving@fb.com \
--cc=thomas@t-8ch.de \
--cc=x86@kernel.org \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).