diff options
author | Peter Zijlstra <a.p.zijlstra@chello.nl> | 2012-06-15 11:43:39 -0400 |
---|---|---|
committer | Ingo Molnar <mingo@kernel.org> | 2012-06-16 03:10:45 -0400 |
commit | c5784de2b351fe871bb57487878f7fc7ec5b075c (patch) | |
tree | 4b73571e8fed9af9b6ad6a4cc7f355d0b104a5ea /kernel/events | |
parent | 7a5bfb66b07f22d2429db776da7bb8b57bfb5cff (diff) |
uprobes: Document uprobe_register() vs uprobe_mmap() race
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>
Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
Cc: Anton Arapov <anton@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/20120615154339.GA9591@redhat.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Diffstat (limited to 'kernel/events')
-rw-r--r-- | 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 897417dbca8e..2671d9ad49be 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c | |||
@@ -44,6 +44,23 @@ static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */ | |||
44 | 44 | ||
45 | #define UPROBES_HASH_SZ 13 | 45 | #define UPROBES_HASH_SZ 13 |
46 | 46 | ||
47 | /* | ||
48 | * We need separate register/unregister and mmap/munmap lock hashes because | ||
49 | * of mmap_sem nesting. | ||
50 | * | ||
51 | * uprobe_register() needs to install probes on (potentially) all processes | ||
52 | * and thus needs to acquire multiple mmap_sems (consequtively, not | ||
53 | * concurrently), whereas uprobe_mmap() is called while holding mmap_sem | ||
54 | * for the particular process doing the mmap. | ||
55 | * | ||
56 | * uprobe_register()->register_for_each_vma() needs to drop/acquire mmap_sem | ||
57 | * because of lock order against i_mmap_mutex. This means there's a hole in | ||
58 | * the register vma iteration where a mmap() can happen. | ||
59 | * | ||
60 | * Thus uprobe_register() can race with uprobe_mmap() and we can try and | ||
61 | * install a probe where one is already installed. | ||
62 | */ | ||
63 | |||
47 | /* serialize (un)register */ | 64 | /* serialize (un)register */ |
48 | static struct mutex uprobes_mutex[UPROBES_HASH_SZ]; | 65 | static struct mutex uprobes_mutex[UPROBES_HASH_SZ]; |
49 | 66 | ||
@@ -339,7 +356,9 @@ out: | |||
339 | int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr) | 356 | int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr) |
340 | { | 357 | { |
341 | int result; | 358 | int result; |
342 | 359 | /* | |
360 | * See the comment near uprobes_hash(). | ||
361 | */ | ||
343 | result = is_swbp_at_addr(mm, vaddr); | 362 | result = is_swbp_at_addr(mm, vaddr); |
344 | if (result == 1) | 363 | if (result == 1) |
345 | return -EEXIST; | 364 | return -EEXIST; |
@@ -845,6 +864,10 @@ static int register_for_each_vma(struct uprobe *uprobe, bool is_register) | |||
845 | 864 | ||
846 | if (is_register) { | 865 | if (is_register) { |
847 | err = install_breakpoint(uprobe, mm, vma, info->vaddr); | 866 | err = install_breakpoint(uprobe, mm, vma, info->vaddr); |
867 | /* | ||
868 | * We can race against uprobe_mmap(), see the | ||
869 | * comment near uprobe_hash(). | ||
870 | */ | ||
848 | if (err == -EEXIST) | 871 | if (err == -EEXIST) |
849 | err = 0; | 872 | err = 0; |
850 | } else { | 873 | } else { |
@@ -1054,8 +1077,10 @@ int uprobe_mmap(struct vm_area_struct *vma) | |||
1054 | } | 1077 | } |
1055 | 1078 | ||
1056 | ret = install_breakpoint(uprobe, vma->vm_mm, vma, vaddr); | 1079 | ret = install_breakpoint(uprobe, vma->vm_mm, vma, vaddr); |
1057 | 1080 | /* | |
1058 | /* Ignore double add: */ | 1081 | * We can race against uprobe_register(), see the |
1082 | * comment near uprobe_hash(). | ||
1083 | */ | ||
1059 | if (ret == -EEXIST) { | 1084 | if (ret == -EEXIST) { |
1060 | ret = 0; | 1085 | ret = 0; |
1061 | 1086 | ||