Commit 68e3c69
Peter Zijlstra
perf/core: Fix data race between perf_event_set_output() and perf_mmap_close()
Yang Jihing reported a race between perf_event_set_output() and
perf_mmap_close():
CPU1 CPU2
perf_mmap_close(e2)
if (atomic_dec_and_test(&e2->rb->mmap_count)) // 1 - > 0
detach_rest = true
ioctl(e1, IOC_SET_OUTPUT, e2)
perf_event_set_output(e1, e2)
...
list_for_each_entry_rcu(e, &e2->rb->event_list, rb_entry)
ring_buffer_attach(e, NULL);
// e1 isn't yet added and
// therefore not detached
ring_buffer_attach(e1, e2->rb)
list_add_rcu(&e1->rb_entry,
&e2->rb->event_list)
After this; e1 is attached to an unmapped rb and a subsequent
perf_mmap() will loop forever more:
again:
mutex_lock(&e->mmap_mutex);
if (event->rb) {
...
if (!atomic_inc_not_zero(&e->rb->mmap_count)) {
...
mutex_unlock(&e->mmap_mutex);
goto again;
}
}
The loop in perf_mmap_close() holds e2->mmap_mutex, while the attach
in perf_event_set_output() holds e1->mmap_mutex. As such there is no
serialization to avoid this race.
Change perf_event_set_output() to take both e1->mmap_mutex and
e2->mmap_mutex to alleviate that problem. Additionally, have the loop
in perf_mmap() detach the rb directly, this avoids having to wait for
the concurrent perf_mmap_close() to get around to doing it to make
progress.
Fixes: 9bb5d40 ("perf: Fix mmap() accounting hole")
Reported-by: Yang Jihong <yangjihong1@huawei.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Tested-by: Yang Jihong <yangjihong1@huawei.com>
Link: https://lkml.kernel.org/r/YsQ3jm2GR38SW7uD@worktop.programming.kicks-ass.net1 parent 88084a3 commit 68e3c69
1 file changed
Lines changed: 31 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6253 | 6253 | | |
6254 | 6254 | | |
6255 | 6255 | | |
6256 | | - | |
6257 | | - | |
6258 | | - | |
| 6256 | + | |
| 6257 | + | |
6259 | 6258 | | |
| 6259 | + | |
6260 | 6260 | | |
6261 | 6261 | | |
6262 | 6262 | | |
| |||
11825 | 11825 | | |
11826 | 11826 | | |
11827 | 11827 | | |
| 11828 | + | |
| 11829 | + | |
| 11830 | + | |
| 11831 | + | |
| 11832 | + | |
| 11833 | + | |
| 11834 | + | |
| 11835 | + | |
| 11836 | + | |
11828 | 11837 | | |
11829 | 11838 | | |
11830 | 11839 | | |
11831 | 11840 | | |
11832 | 11841 | | |
11833 | 11842 | | |
11834 | | - | |
| 11843 | + | |
| 11844 | + | |
11835 | 11845 | | |
| 11846 | + | |
11836 | 11847 | | |
11837 | 11848 | | |
11838 | 11849 | | |
| |||
11870 | 11881 | | |
11871 | 11882 | | |
11872 | 11883 | | |
| 11884 | + | |
| 11885 | + | |
| 11886 | + | |
| 11887 | + | |
| 11888 | + | |
| 11889 | + | |
| 11890 | + | |
| 11891 | + | |
11873 | 11892 | | |
11874 | | - | |
11875 | 11893 | | |
11876 | 11894 | | |
11877 | 11895 | | |
| |||
11881 | 11899 | | |
11882 | 11900 | | |
11883 | 11901 | | |
| 11902 | + | |
| 11903 | + | |
| 11904 | + | |
| 11905 | + | |
| 11906 | + | |
| 11907 | + | |
11884 | 11908 | | |
11885 | 11909 | | |
11886 | 11910 | | |
11887 | 11911 | | |
11888 | 11912 | | |
11889 | 11913 | | |
11890 | 11914 | | |
| 11915 | + | |
| 11916 | + | |
11891 | 11917 | | |
11892 | 11918 | | |
11893 | 11919 | | |
11894 | 11920 | | |
11895 | 11921 | | |
11896 | | - | |
11897 | | - | |
11898 | | - | |
11899 | | - | |
11900 | | - | |
11901 | | - | |
11902 | | - | |
11903 | | - | |
11904 | | - | |
11905 | 11922 | | |
11906 | 11923 | | |
11907 | 11924 | | |
| |||
0 commit comments