Skip to content

Commit 8eaec7b

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
perf/core: Remove retry loop from perf_mmap()
AFAICT there is no actual benefit from the mutex drop on re-try. The 'worst' case scenario is that we instantly re-gain the mutex without perf_mmap_close() getting it. So might as well make that the normal case. Reflow the code to make the ring buffer detach case naturally flow into the no ring buffer case. [ mingo: Forward ported it ] Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Reviewed-by: Ravi Bangoria <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 0c8a4e4 commit 8eaec7b

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

kernel/events/core.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6719,28 +6719,33 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
67196719
return -EINVAL;
67206720

67216721
WARN_ON_ONCE(event->ctx->parent_ctx);
6722-
again:
67236722
mutex_lock(&event->mmap_mutex);
6723+
67246724
if (event->rb) {
67256725
if (data_page_nr(event->rb) != nr_pages) {
67266726
ret = -EINVAL;
67276727
goto unlock;
67286728
}
67296729

6730-
if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
6730+
if (atomic_inc_not_zero(&event->rb->mmap_count)) {
67316731
/*
6732-
* Raced against perf_mmap_close(); remove the
6733-
* event and try again.
6732+
* Success -- managed to mmap() the same buffer
6733+
* multiple times.
67346734
*/
6735-
ring_buffer_attach(event, NULL);
6736-
mutex_unlock(&event->mmap_mutex);
6737-
goto again;
6735+
ret = 0;
6736+
/* We need the rb to map pages. */
6737+
rb = event->rb;
6738+
goto unlock;
67386739
}
67396740

6740-
/* We need the rb to map pages. */
6741-
rb = event->rb;
6742-
goto unlock;
6741+
/*
6742+
* Raced against perf_mmap_close()'s
6743+
* atomic_dec_and_mutex_lock() remove the
6744+
* event and continue as if !event->rb
6745+
*/
6746+
ring_buffer_attach(event, NULL);
67436747
}
6748+
67446749
} else {
67456750
/*
67466751
* AUX area mapping: if rb->aux_nr_pages != 0, it's already

0 commit comments

Comments
 (0)