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 RFCv2 10/18] uprobes/x86: Add mm_uprobe objects to track uprobes within mm
Date: Mon, 24 Feb 2025 15:01:42 +0100 [thread overview]
Message-ID: <20250224140151.667679-11-jolsa@kernel.org> (raw)
In-Reply-To: <20250224140151.667679-1-jolsa@kernel.org>
We keep track of global uprobe instances, because with just 2 types
of update - writing breakpoint or original opcode - we don't need to
track the state of the specific uprobe state for mm_struct.
With optimized uprobe support we will need to make several instructions
updates and make sure we keep the state of the update per mm_struct.
Adding the mm_uprobe object to keep track of installed uprobes per
mm_struct. It's kept in rb_tree for fast lookups and the tree is
cleaned up when the breakpoint is uninstalled or the mm_struct is
released.
The key is uprobe object's address together with virtual address of
the breakpoint. The reason for the adding the latter to the key is
that we can have multiple virtual addresses for single uprobe,
because the code (for given offset) can be loaded multiple times.
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
arch/x86/kernel/uprobes.c | 115 ++++++++++++++++++++++++++++++++++++++
include/linux/uprobes.h | 1 +
2 files changed, 116 insertions(+)
diff --git a/arch/x86/kernel/uprobes.c b/arch/x86/kernel/uprobes.c
index e0c3fb01a43c..8d4eb8133221 100644
--- a/arch/x86/kernel/uprobes.c
+++ b/arch/x86/kernel/uprobes.c
@@ -798,19 +798,134 @@ static __maybe_unused void uprobe_trampoline_put(struct uprobe_trampoline *tramp
destroy_uprobe_trampoline(tramp);
}
+struct mm_uprobe {
+ struct rb_node rb_node;
+ unsigned long auprobe;
+ unsigned long vaddr;
+};
+
+#define __node_2_mm_uprobe(node) rb_entry((node), struct mm_uprobe, rb_node)
+
+struct __mm_uprobe_key {
+ unsigned long auprobe;
+ unsigned long vaddr;
+};
+
+static inline int mm_uprobe_cmp(unsigned long l_auprobe, unsigned long l_vaddr,
+ const struct mm_uprobe *r_mmu)
+{
+ if (l_auprobe < r_mmu->auprobe)
+ return -1;
+ if (l_auprobe > r_mmu->auprobe)
+ return 1;
+ if (l_vaddr < r_mmu->vaddr)
+ return -1;
+ if (l_vaddr > r_mmu->vaddr)
+ return 1;
+
+ return 0;
+}
+
+static inline int __mm_uprobe_cmp(struct rb_node *a, const struct rb_node *b)
+{
+ struct mm_uprobe *mmu_a = __node_2_mm_uprobe(a);
+
+ return mm_uprobe_cmp(mmu_a->auprobe, mmu_a->vaddr, __node_2_mm_uprobe(b));
+}
+
+static inline bool __mm_uprobe_less(struct rb_node *a, const struct rb_node *b)
+{
+ struct mm_uprobe *mmu_a = __node_2_mm_uprobe(a);
+
+ return mm_uprobe_cmp(mmu_a->auprobe, mmu_a->vaddr, __node_2_mm_uprobe(b)) < 0;
+}
+
+static inline int __mm_uprobe_cmp_key(const void *key, const struct rb_node *b)
+{
+ const struct __mm_uprobe_key *a = key;
+
+ return mm_uprobe_cmp(a->auprobe, a->vaddr, __node_2_mm_uprobe(b));
+}
+
+static struct mm_uprobe *find_mm_uprobe(struct mm_struct *mm, struct arch_uprobe *auprobe,
+ unsigned long vaddr)
+{
+ struct __mm_uprobe_key key = {
+ .auprobe = (unsigned long) auprobe,
+ .vaddr = vaddr,
+ };
+ struct rb_node *node;
+
+ node = rb_find(&key, &mm->uprobes_state.root_uprobes, __mm_uprobe_cmp_key);
+ return node ? __node_2_mm_uprobe(node) : NULL;
+}
+
+static struct mm_uprobe *insert_mm_uprobe(struct mm_struct *mm, struct arch_uprobe *auprobe,
+ unsigned long vaddr)
+{
+ struct mm_uprobe *mmu;
+
+ mmu = kmalloc(sizeof(*mmu), GFP_KERNEL);
+ if (mmu) {
+ mmu->auprobe = (unsigned long) auprobe;
+ mmu->vaddr = vaddr;
+ RB_CLEAR_NODE(&mmu->rb_node);
+ rb_add(&mmu->rb_node, &mm->uprobes_state.root_uprobes, __mm_uprobe_less);
+ }
+ return mmu;
+}
+
+static void destroy_mm_uprobe(struct mm_uprobe *mmu, struct rb_root *root)
+{
+ rb_erase(&mmu->rb_node, root);
+ kfree(mmu);
+}
+
+int set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
+{
+ struct mm_uprobe *mmu;
+
+ if (find_mm_uprobe(mm, auprobe, vaddr))
+ return 0;
+ mmu = insert_mm_uprobe(mm, auprobe, vaddr);
+ if (!mmu)
+ return -ENOMEM;
+ return uprobe_write_opcode(auprobe, mm, vaddr, UPROBE_SWBP_INSN, false);
+}
+
+int set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
+{
+ struct mm_uprobe *mmu;
+
+ mmu = find_mm_uprobe(mm, auprobe, vaddr);
+ if (!mmu)
+ return 0;
+ destroy_mm_uprobe(mmu, &mm->uprobes_state.root_uprobes);
+ return uprobe_write_opcode(auprobe, mm, vaddr, *(uprobe_opcode_t *)&auprobe->insn, true);
+}
+
void arch_uprobe_init_state(struct mm_struct *mm)
{
INIT_HLIST_HEAD(&mm->uprobes_state.head_tramps);
+ mm->uprobes_state.root_uprobes = RB_ROOT;
}
void arch_uprobe_clear_state(struct mm_struct *mm)
{
struct uprobes_state *state = &mm->uprobes_state;
struct uprobe_trampoline *tramp;
+ struct rb_node *node, *next;
struct hlist_node *n;
hlist_for_each_entry_safe(tramp, n, &state->head_tramps, node)
destroy_uprobe_trampoline(tramp);
+
+ node = rb_first(&state->root_uprobes);
+ while (node) {
+ next = rb_next(node);
+ destroy_mm_uprobe(__node_2_mm_uprobe(node), &state->root_uprobes);
+ node = next;
+ }
}
#else /* 32-bit: */
/*
diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h
index 05a156750e8d..bd726daa4428 100644
--- a/include/linux/uprobes.h
+++ b/include/linux/uprobes.h
@@ -186,6 +186,7 @@ struct uprobes_state {
struct xol_area *xol_area;
#ifdef CONFIG_X86_64
struct hlist_head head_tramps;
+ struct rb_root root_uprobes;
#endif
};
--
2.48.1
next prev parent reply other threads:[~2025-02-24 14:03 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-24 14:01 [PATCH RFCv2 00/18] uprobes: Add support to optimize usdt probes on x86_64 Jiri Olsa
2025-02-24 14:01 ` [PATCH RFCv2 01/18] uprobes: Rename arch_uretprobe_trampoline function Jiri Olsa
2025-02-24 14:01 ` [PATCH RFCv2 02/18] uprobes: Make copy_from_page global Jiri Olsa
2025-02-24 14:01 ` [PATCH RFCv2 03/18] uprobes: Move ref_ctr_offset update out of uprobe_write_opcode Jiri Olsa
2025-02-24 14:01 ` [PATCH RFCv2 04/18] uprobes: Add uprobe_write function Jiri Olsa
2025-02-24 14:01 ` [PATCH RFCv2 05/18] uprobes: Add nbytes argument to uprobe_write_opcode Jiri Olsa
2025-02-24 14:01 ` [PATCH RFCv2 06/18] uprobes: Add orig argument to uprobe_write and uprobe_write_opcode Jiri Olsa
2025-02-28 19:07 ` Andrii Nakryiko
2025-02-28 23:12 ` Jiri Olsa
2025-02-24 14:01 ` [PATCH RFCv2 07/18] uprobes: Add swbp argument to arch_uretprobe_hijack_return_addr Jiri Olsa
2025-02-24 14:01 ` [PATCH RFCv2 08/18] uprobes/x86: Add uprobe syscall to speed up uprobe Jiri Olsa
2025-02-24 19:22 ` Alexei Starovoitov
2025-02-25 13:35 ` Jiri Olsa
2025-02-25 17:10 ` Andrii Nakryiko
2025-02-25 18:06 ` Alexei Starovoitov
2025-02-26 2:36 ` Alexei Starovoitov
2025-02-24 14:01 ` [PATCH RFCv2 09/18] uprobes/x86: Add mapping for optimized uprobe trampolines Jiri Olsa
2025-02-24 14:01 ` Jiri Olsa [this message]
2025-02-24 14:01 ` [PATCH RFCv2 11/18] uprobes/x86: Add support to emulate nop5 instruction Jiri Olsa
2025-02-24 14:01 ` [PATCH RFCv2 12/18] uprobes/x86: Add support to optimize uprobes Jiri Olsa
2025-02-28 18:55 ` Andrii Nakryiko
2025-02-28 22:55 ` Jiri Olsa
2025-02-28 23:00 ` Andrii Nakryiko
2025-02-28 23:18 ` Jiri Olsa
2025-02-28 23:27 ` Andrii Nakryiko
2025-02-28 23:00 ` Jiri Olsa
2025-02-24 14:01 ` [PATCH RFCv2 13/18] selftests/bpf: Reorg the uprobe_syscall test function Jiri Olsa
2025-02-24 14:01 ` [PATCH RFCv2 14/18] selftests/bpf: Use 5-byte nop for x86 usdt probes Jiri Olsa
2025-02-24 14:01 ` [PATCH RFCv2 15/18] selftests/bpf: Add uprobe/usdt syscall tests Jiri Olsa
2025-02-24 14:01 ` [PATCH RFCv2 16/18] selftests/bpf: Add hit/attach/detach race optimized uprobe test Jiri Olsa
2025-02-24 14:01 ` [PATCH RFCv2 17/18] selftests/bpf: Add uprobe syscall sigill signal test Jiri Olsa
2025-02-24 14:01 ` [PATCH RFCv2 18/18] selftests/bpf: Add 5-byte nop uprobe trigger bench Jiri Olsa
2025-02-24 18:46 ` [PATCH RFCv2 00/18] uprobes: Add support to optimize usdt probes on x86_64 Ingo Molnar
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=20250224140151.667679-11-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