Skip to content

Commit c4b69be

Browse files
Peter Zijlstragregkh
authored andcommitted
perf/aux: Fix AUX buffer serialization
commit 2ab9d83 upstream. 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 <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 9faed52 commit c4b69be

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
@@ -6352,12 +6353,11 @@ static void perf_mmap_close(struct vm_area_struct *vma)
63526353
event->pmu->event_unmapped(event, vma->vm_mm);
63536354

63546355
/*
6355-
* rb->aux_mmap_count will always drop before rb->mmap_count and
6356-
* event->mmap_count, so it is ok to use event->mmap_mutex to
6357-
* serialize with perf_mmap here.
6356+
* The AUX buffer is strictly a sub-buffer, serialize using aux_mutex
6357+
* to avoid complications.
63586358
*/
63596359
if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
6360-
atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
6360+
atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &rb->aux_mutex)) {
63616361
/*
63626362
* Stop all AUX events that are writing to this buffer,
63636363
* so that we can free its AUX pages and corresponding PMU
@@ -6374,7 +6374,7 @@ static void perf_mmap_close(struct vm_area_struct *vma)
63746374
rb_free_aux(rb);
63756375
WARN_ON_ONCE(refcount_read(&rb->aux_refcount));
63766376

6377-
mutex_unlock(&event->mmap_mutex);
6377+
mutex_unlock(&rb->aux_mutex);
63786378
}
63796379

63806380
if (atomic_dec_and_test(&rb->mmap_count))
@@ -6462,6 +6462,7 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
64626462
struct perf_event *event = file->private_data;
64636463
unsigned long user_locked, user_lock_limit;
64646464
struct user_struct *user = current_user();
6465+
struct mutex *aux_mutex = NULL;
64656466
struct perf_buffer *rb = NULL;
64666467
unsigned long locked, lock_limit;
64676468
unsigned long vma_size;
@@ -6510,6 +6511,9 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
65106511
if (!rb)
65116512
goto aux_unlock;
65126513

6514+
aux_mutex = &rb->aux_mutex;
6515+
mutex_lock(aux_mutex);
6516+
65136517
aux_offset = READ_ONCE(rb->user_page->aux_offset);
65146518
aux_size = READ_ONCE(rb->user_page->aux_size);
65156519

@@ -6660,6 +6664,8 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
66606664
atomic_dec(&rb->mmap_count);
66616665
}
66626666
aux_unlock:
6667+
if (aux_mutex)
6668+
mutex_unlock(aux_mutex);
66636669
mutex_unlock(&event->mmap_mutex);
66646670

66656671
/*

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
@@ -333,6 +333,8 @@ ring_buffer_init(struct perf_buffer *rb, long watermark, int flags)
333333
*/
334334
if (!rb->nr_pages)
335335
rb->paused = 1;
336+
337+
mutex_init(&rb->aux_mutex);
336338
}
337339

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

0 commit comments

Comments
 (0)