From: Oleg Nesterov <oleg@redhat.com>
To: Ingo Molnar <mingo@elte.hu>
Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>,
Anton Arapov <anton@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Srikar Dronamraju <srikar@linux.vnet.ibm.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH 07/15] uprobes: document uprobe_register() vs uprobe_mmap() race
Date: Fri, 15 Jun 2012 17:43:39 +0200 [thread overview]
Message-ID: <20120615154339.GA9591@redhat.com> (raw)
In-Reply-To: <20120615154241.GA9524@redhat.com>
From: Peter Zijlstra <a.p.zijlstra@chello.nl>
Because the mind is treacherous and makes us forget we need to write
stuff down.
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/events/uprobes.c | 31 ++++++++++++++++++++++++++++---
1 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index 897417d..2671d9a 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -44,6 +44,23 @@ static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
#define UPROBES_HASH_SZ 13
+/*
+ * We need separate register/unregister and mmap/munmap lock hashes because
+ * of mmap_sem nesting.
+ *
+ * uprobe_register() needs to install probes on (potentially) all processes
+ * and thus needs to acquire multiple mmap_sems (consequtively, not
+ * concurrently), whereas uprobe_mmap() is called while holding mmap_sem
+ * for the particular process doing the mmap.
+ *
+ * uprobe_register()->register_for_each_vma() needs to drop/acquire mmap_sem
+ * because of lock order against i_mmap_mutex. This means there's a hole in
+ * the register vma iteration where a mmap() can happen.
+ *
+ * Thus uprobe_register() can race with uprobe_mmap() and we can try and
+ * install a probe where one is already installed.
+ */
+
/* serialize (un)register */
static struct mutex uprobes_mutex[UPROBES_HASH_SZ];
@@ -339,7 +356,9 @@ out:
int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
{
int result;
-
+ /*
+ * See the comment near uprobes_hash().
+ */
result = is_swbp_at_addr(mm, vaddr);
if (result == 1)
return -EEXIST;
@@ -845,6 +864,10 @@ static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
if (is_register) {
err = install_breakpoint(uprobe, mm, vma, info->vaddr);
+ /*
+ * We can race against uprobe_mmap(), see the
+ * comment near uprobe_hash().
+ */
if (err == -EEXIST)
err = 0;
} else {
@@ -1054,8 +1077,10 @@ int uprobe_mmap(struct vm_area_struct *vma)
}
ret = install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
-
- /* Ignore double add: */
+ /*
+ * We can race against uprobe_register(), see the
+ * comment near uprobe_hash().
+ */
if (ret == -EEXIST) {
ret = 0;
--
1.5.5.1
next prev parent reply other threads:[~2012-06-15 15:45 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-15 15:42 [PATCH 0/15] uprobes: misc Oleg Nesterov
2012-06-15 15:43 ` [PATCH 01/15] uprobes: valid_vma() should reject VM_HUGETLB Oleg Nesterov
2012-06-18 8:50 ` [tip:perf/core] uprobes: Valid_vma() " tip-bot for Oleg Nesterov
2012-06-15 15:43 ` [PATCH 02/15] uprobes: __copy_insn() should ensure a_ops->readpage != NULL Oleg Nesterov
2012-06-18 8:50 ` [tip:perf/core] uprobes: __copy_insn() should ensure a_ops-> readpage " tip-bot for Oleg Nesterov
2012-06-15 15:43 ` [PATCH 03/15] uprobes: write_opcode()->__replace_page() can race with try_to_unmap() Oleg Nesterov
2012-06-18 8:51 ` [tip:perf/core] uprobes: Write_opcode()->__replace_page() " tip-bot for Oleg Nesterov
2012-06-15 15:43 ` [PATCH 04/15] uprobes: install_breakpoint() should fail if is_swbp_insn() == T Oleg Nesterov
2012-06-18 8:52 ` [tip:perf/core] uprobes: Install_breakpoint() " tip-bot for Oleg Nesterov
2012-06-15 15:43 ` [PATCH 05/15] uprobes: rework register_for_each_vma() to make it O(n) Oleg Nesterov
2012-06-18 8:53 ` [tip:perf/core] uprobes: Rework " tip-bot for Oleg Nesterov
2012-06-15 15:43 ` [PATCH 06/15] uprobes: change build_map_info() to try kmalloc(GFP_NOWAIT) first Oleg Nesterov
2012-06-18 8:54 ` [tip:perf/core] uprobes: Change build_map_info() to try kmalloc( GFP_NOWAIT) first tip-bot for Oleg Nesterov
2012-06-15 15:43 ` Oleg Nesterov [this message]
2012-06-18 8:55 ` [tip:perf/core] uprobes: Document uprobe_register() vs uprobe_mmap () race tip-bot for Peter Zijlstra
2012-06-15 15:43 ` [PATCH 08/15] uprobes: copy_insn() shouldn't depend on mm/vma/vaddr Oleg Nesterov
2012-06-18 8:56 ` [tip:perf/core] uprobes: Copy_insn() shouldn't depend on mm/vma/ vaddr tip-bot for Oleg Nesterov
2012-06-15 15:43 ` [PATCH 09/15] uprobes: copy_insn() should not return -ENOMEM if __copy_insn() fails Oleg Nesterov
2012-06-18 8:57 ` [tip:perf/core] uprobes: Copy_insn() " tip-bot for Oleg Nesterov
2012-06-15 15:43 ` [PATCH 10/15] uprobes: no need to re-check vma_address() in write_opcode() Oleg Nesterov
2012-06-18 8:57 ` [tip:perf/core] uprobes: No " tip-bot for Oleg Nesterov
2012-06-15 15:43 ` [PATCH 11/15] uprobes: move BUG_ON(UPROBE_SWBP_INSN_SIZE) from write_opcode() to install_breakpoint() Oleg Nesterov
2012-06-15 16:36 ` Srikar Dronamraju
2012-06-15 17:52 ` Oleg Nesterov
2012-06-18 12:08 ` Srikar Dronamraju
2012-06-18 8:58 ` [tip:perf/core] uprobes: Move " tip-bot for Oleg Nesterov
2012-06-15 15:43 ` [PATCH 12/15] uprobes: simplify the usage of uprobe->pending_list Oleg Nesterov
2012-06-18 8:59 ` [tip:perf/core] uprobes: Simplify the usage of uprobe-> pending_list tip-bot for Oleg Nesterov
2012-06-15 15:43 ` [PATCH 13/15] uprobes: don't use loff_t for the valid virtual address Oleg Nesterov
2012-06-18 9:00 ` [tip:perf/core] uprobes: Don' t " tip-bot for Oleg Nesterov
2012-06-15 15:43 ` [PATCH 14/15] uprobes: __copy_insn() needs "loff_t offset" Oleg Nesterov
2012-06-18 9:01 ` [tip:perf/core] " tip-bot for Oleg Nesterov
2012-06-15 15:44 ` [PATCH 15/15] uprobes: remove the unnecessary initialization in add_utask() Oleg Nesterov
2012-06-18 9:02 ` [tip:perf/core] uprobes: Remove " tip-bot for Oleg Nesterov
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=20120615154339.GA9591@redhat.com \
--to=oleg@redhat.com \
--cc=ananth@in.ibm.com \
--cc=anton@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
--cc=peterz@infradead.org \
--cc=srikar@linux.vnet.ibm.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