Skip to content

Commit 9548783

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
perf/core: Simplify the perf_mmap() control flow
Identity-transform: if (c) { X1; } else { Y; goto l; } X2; l: into the simpler: if (c) { X1; X2; } else { Y; } [ 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 c5b9678 commit 9548783

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

kernel/events/core.c

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6701,6 +6701,42 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
67016701

67026702
if (vma->vm_pgoff == 0) {
67036703
nr_pages = (vma_size / PAGE_SIZE) - 1;
6704+
6705+
/*
6706+
* If we have rb pages ensure they're a power-of-two number, so we
6707+
* can do bitmasks instead of modulo.
6708+
*/
6709+
if (nr_pages != 0 && !is_power_of_2(nr_pages))
6710+
return -EINVAL;
6711+
6712+
if (vma_size != PAGE_SIZE * (1 + nr_pages))
6713+
return -EINVAL;
6714+
6715+
WARN_ON_ONCE(event->ctx->parent_ctx);
6716+
again:
6717+
mutex_lock(&event->mmap_mutex);
6718+
if (event->rb) {
6719+
if (data_page_nr(event->rb) != nr_pages) {
6720+
ret = -EINVAL;
6721+
goto unlock;
6722+
}
6723+
6724+
if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
6725+
/*
6726+
* Raced against perf_mmap_close(); remove the
6727+
* event and try again.
6728+
*/
6729+
ring_buffer_attach(event, NULL);
6730+
mutex_unlock(&event->mmap_mutex);
6731+
goto again;
6732+
}
6733+
6734+
/* We need the rb to map pages. */
6735+
rb = event->rb;
6736+
goto unlock;
6737+
}
6738+
6739+
user_extra = nr_pages + 1;
67046740
} else {
67056741
/*
67066742
* AUX area mapping: if rb->aux_nr_pages != 0, it's already
@@ -6760,47 +6796,8 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
67606796

67616797
atomic_set(&rb->aux_mmap_count, 1);
67626798
user_extra = nr_pages;
6763-
6764-
goto accounting;
6765-
}
6766-
6767-
/*
6768-
* If we have rb pages ensure they're a power-of-two number, so we
6769-
* can do bitmasks instead of modulo.
6770-
*/
6771-
if (nr_pages != 0 && !is_power_of_2(nr_pages))
6772-
return -EINVAL;
6773-
6774-
if (vma_size != PAGE_SIZE * (1 + nr_pages))
6775-
return -EINVAL;
6776-
6777-
WARN_ON_ONCE(event->ctx->parent_ctx);
6778-
again:
6779-
mutex_lock(&event->mmap_mutex);
6780-
if (event->rb) {
6781-
if (data_page_nr(event->rb) != nr_pages) {
6782-
ret = -EINVAL;
6783-
goto unlock;
6784-
}
6785-
6786-
if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
6787-
/*
6788-
* Raced against perf_mmap_close(); remove the
6789-
* event and try again.
6790-
*/
6791-
ring_buffer_attach(event, NULL);
6792-
mutex_unlock(&event->mmap_mutex);
6793-
goto again;
6794-
}
6795-
6796-
/* We need the rb to map pages. */
6797-
rb = event->rb;
6798-
goto unlock;
67996799
}
68006800

6801-
user_extra = nr_pages + 1;
6802-
6803-
accounting:
68046801
user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
68056802

68066803
/*

0 commit comments

Comments
 (0)