Skip to content

Commit

Permalink
perf/aux: Fix AUX buffer serialization
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Peter Zijlstra authored and gregkh committed Sep 12, 2024
1 parent 13123ef commit b9b6882
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
18 changes: 12 additions & 6 deletions kernel/events/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1255,8 +1255,9 @@ static void put_ctx(struct perf_event_context *ctx)
* perf_event_context::mutex
* perf_event::child_mutex;
* perf_event_context::lock
* perf_event::mmap_mutex
* mmap_lock
* perf_event::mmap_mutex
* perf_buffer::aux_mutex
* perf_addr_filters_head::lock
*
* cpu_hotplug_lock
Expand Down Expand Up @@ -6383,12 +6384,11 @@ static void perf_mmap_close(struct vm_area_struct *vma)
event->pmu->event_unmapped(event, vma->vm_mm);

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

mutex_unlock(&event->mmap_mutex);
mutex_unlock(&rb->aux_mutex);
}

if (atomic_dec_and_test(&rb->mmap_count))
Expand Down Expand Up @@ -6493,6 +6493,7 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
struct perf_event *event = file->private_data;
unsigned long user_locked, user_lock_limit;
struct user_struct *user = current_user();
struct mutex *aux_mutex = NULL;
struct perf_buffer *rb = NULL;
unsigned long locked, lock_limit;
unsigned long vma_size;
Expand Down Expand Up @@ -6541,6 +6542,9 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
if (!rb)
goto aux_unlock;

aux_mutex = &rb->aux_mutex;
mutex_lock(aux_mutex);

aux_offset = READ_ONCE(rb->user_page->aux_offset);
aux_size = READ_ONCE(rb->user_page->aux_size);

Expand Down Expand Up @@ -6691,6 +6695,8 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
atomic_dec(&rb->mmap_count);
}
aux_unlock:
if (aux_mutex)
mutex_unlock(aux_mutex);
mutex_unlock(&event->mmap_mutex);

/*
Expand Down
1 change: 1 addition & 0 deletions kernel/events/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct perf_buffer {
struct user_struct *mmap_user;

/* AUX area */
struct mutex aux_mutex;
long aux_head;
unsigned int aux_nest;
long aux_wakeup; /* last aux_watermark boundary crossed by aux_head */
Expand Down
2 changes: 2 additions & 0 deletions kernel/events/ring_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ ring_buffer_init(struct perf_buffer *rb, long watermark, int flags)
*/
if (!rb->nr_pages)
rb->paused = 1;

mutex_init(&rb->aux_mutex);
}

void perf_aux_output_flag(struct perf_output_handle *handle, u64 flags)
Expand Down

0 comments on commit b9b6882

Please sign in to comment.