Skip to content

Commit 2ab9d83

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
perf/aux: Fix AUX buffer serialization
Ole reported that event->mmap_mutex is strictly insufficient to serialize the AUX buffer, add a per RB mutex to fully serialize it. Note that in the lock order comment the perf_event::mmap_mutex order was already wrong, that is, it nesting under mmap_lock is not new with this patch. Fixes: 45bfb2e ("perf: Add AUX area to ring buffer for raw data streams") Reported-by: Ole <ole@binarygecko.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent e240b0f commit 2ab9d83

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

kernel/events/core.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,8 +1255,9 @@ static void put_ctx(struct perf_event_context *ctx)
12551255
* perf_event_context::mutex
12561256
* perf_event::child_mutex;
12571257
* perf_event_context::lock
1258-
* perf_event::mmap_mutex
12591258
* mmap_lock
1259+
* perf_event::mmap_mutex
1260+
* perf_buffer::aux_mutex
12601261
* perf_addr_filters_head::lock
12611262
*
12621263
* cpu_hotplug_lock
@@ -6373,12 +6374,11 @@ static void perf_mmap_close(struct vm_area_struct *vma)
63736374
event->pmu->event_unmapped(event, vma->vm_mm);
63746375

63756376
/*
6376-
* rb->aux_mmap_count will always drop before rb->mmap_count and
6377-
* event->mmap_count, so it is ok to use event->mmap_mutex to
6378-
* serialize with perf_mmap here.
6377+
* The AUX buffer is strictly a sub-buffer, serialize using aux_mutex
6378+
* to avoid complications.
63796379
*/
63806380
if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
6381-
atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
6381+
atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &rb->aux_mutex)) {
63826382
/*
63836383
* Stop all AUX events that are writing to this buffer,
63846384
* so that we can free its AUX pages and corresponding PMU
@@ -6395,7 +6395,7 @@ static void perf_mmap_close(struct vm_area_struct *vma)
63956395
rb_free_aux(rb);
63966396
WARN_ON_ONCE(refcount_read(&rb->aux_refcount));
63976397

6398-
mutex_unlock(&event->mmap_mutex);
6398+
mutex_unlock(&rb->aux_mutex);
63996399
}
64006400

64016401
if (atomic_dec_and_test(&rb->mmap_count))
@@ -6483,6 +6483,7 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
64836483
struct perf_event *event = file->private_data;
64846484
unsigned long user_locked, user_lock_limit;
64856485
struct user_struct *user = current_user();
6486+
struct mutex *aux_mutex = NULL;
64866487
struct perf_buffer *rb = NULL;
64876488
unsigned long locked, lock_limit;
64886489
unsigned long vma_size;
@@ -6531,6 +6532,9 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
65316532
if (!rb)
65326533
goto aux_unlock;
65336534

6535+
aux_mutex = &rb->aux_mutex;
6536+
mutex_lock(aux_mutex);
6537+
65346538
aux_offset = READ_ONCE(rb->user_page->aux_offset);
65356539
aux_size = READ_ONCE(rb->user_page->aux_size);
65366540

@@ -6681,6 +6685,8 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
66816685
atomic_dec(&rb->mmap_count);
66826686
}
66836687
aux_unlock:
6688+
if (aux_mutex)
6689+
mutex_unlock(aux_mutex);
66846690
mutex_unlock(&event->mmap_mutex);
66856691

66866692
/*

kernel/events/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct perf_buffer {
4040
struct user_struct *mmap_user;
4141

4242
/* AUX area */
43+
struct mutex aux_mutex;
4344
long aux_head;
4445
unsigned int aux_nest;
4546
long aux_wakeup; /* last aux_watermark boundary crossed by aux_head */

kernel/events/ring_buffer.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ ring_buffer_init(struct perf_buffer *rb, long watermark, int flags)
337337
*/
338338
if (!rb->nr_pages)
339339
rb->paused = 1;
340+
341+
mutex_init(&rb->aux_mutex);
340342
}
341343

342344
void perf_aux_output_flag(struct perf_output_handle *handle, u64 flags)

0 commit comments

Comments
 (0)