From: Oleg Nesterov <oleg@redhat.com>
To: andrii@kernel.org, mhiramat@kernel.org, peterz@infradead.org
Cc: jolsa@kernel.org, rostedt@goodmis.org,
linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org
Subject: [PATCH v2 1/5] uprobes: document the usage of mm->mmap_lock
Date: Mon, 29 Jul 2024 15:45:14 +0200 [thread overview]
Message-ID: <20240729134514.GA12309@redhat.com> (raw)
In-Reply-To: <20240729134444.GA12293@redhat.com>
The comment above uprobe_write_opcode() is wrong, unapply_uprobe() calls
it under mmap_read_lock() and this is correct.
And it is completely unclear why register_for_each_vma() takes mmap_lock
for writing, add a comment to explain that mmap_write_lock() is needed to
avoid the following race:
- A task T hits the bp installed by uprobe and calls
find_active_uprobe()
- uprobe_unregister() removes this uprobe/bp
- T calls find_uprobe() which returns NULL
- another uprobe_register() installs the bp at the same address
- T calls is_trap_at_addr() which returns true
- T returns to handle_swbp() and gets SIGTRAP.
Reported-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/events/uprobes.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index 73cc47708679..73dd12b09a7b 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -453,7 +453,7 @@ static int update_ref_ctr(struct uprobe *uprobe, struct mm_struct *mm,
* @vaddr: the virtual address to store the opcode.
* @opcode: opcode to be written at @vaddr.
*
- * Called with mm->mmap_lock held for write.
+ * Called with mm->mmap_lock held for read or write.
* Return 0 (success) or a negative errno.
*/
int uprobe_write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm,
@@ -1046,7 +1046,13 @@ register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new)
if (err && is_register)
goto free;
-
+ /*
+ * We take mmap_lock for writing to avoid the race with
+ * find_active_uprobe() which takes mmap_lock for reading.
+ * Thus this install_breakpoint() can not make
+ * is_trap_at_addr() true right after find_uprobe()
+ * returns NULL in find_active_uprobe().
+ */
mmap_write_lock(mm);
vma = find_vma(mm, info->vaddr);
if (!vma || !valid_vma(vma, is_register) ||
--
2.25.1.362.g51ebf55
next prev parent reply other threads:[~2024-07-29 13:45 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-29 13:44 [PATCH v2 0/5] uprobes: misc cleanups/simplifications Oleg Nesterov
2024-07-29 13:45 ` Oleg Nesterov [this message]
2024-07-29 13:45 ` [PATCH v2 2/5] uprobes: is_trap_at_addr: don't use get_user_pages_remote() Oleg Nesterov
2024-07-29 13:45 ` [PATCH v2 3/5] uprobes: simplify error handling for alloc_uprobe() Oleg Nesterov
2024-07-29 13:45 ` [PATCH v2 4/5] uprobes: kill uprobe_register_refctr() Oleg Nesterov
2024-07-31 5:37 ` Masami Hiramatsu
2024-07-31 7:56 ` Oleg Nesterov
2024-07-31 8:10 ` [PATCH v3 " Oleg Nesterov
2024-07-29 13:45 ` [PATCH v2 5/5] uprobes: make uprobe_register() return struct uprobe * Oleg Nesterov
2024-07-31 5:39 ` Masami Hiramatsu
2024-07-31 7:44 ` Oleg Nesterov
2024-07-31 16:18 ` Andrii Nakryiko
2024-07-31 16:56 ` Peter Zijlstra
2024-07-31 17:01 ` Andrii Nakryiko
2024-07-31 17:05 ` Peter Zijlstra
2024-07-31 17:12 ` Andrii Nakryiko
2024-07-31 17:24 ` Oleg Nesterov
2024-07-31 17:17 ` Oleg Nesterov
2024-08-01 13:10 ` Peter Zijlstra
2024-08-01 11:32 ` Jiri Olsa
2024-08-01 12:00 ` Oleg Nesterov
2024-08-01 12:15 ` Jiri Olsa
2024-08-01 12:26 ` Oleg Nesterov
2024-08-01 13:07 ` Jiri Olsa
2024-07-30 7:42 ` [PATCH v2 0/5] uprobes: misc cleanups/simplifications 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=20240729134514.GA12309@redhat.com \
--to=oleg@redhat.com \
--cc=andrii@kernel.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mhiramat@kernel.org \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
/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).