Skip to content

Commit

Permalink
perf/core: Fix perf_mmap fail when CONFIG_PERF_USE_VMALLOC enabled
Browse files Browse the repository at this point in the history
This problem can be reproduced with CONFIG_PERF_USE_VMALLOC enabled on
both x86_64 and aarch64 arch when using sysdig -B(using ebpf)[1].
sysdig -B works fine after rebuilding the kernel with
CONFIG_PERF_USE_VMALLOC disabled.

I tracked it down to the if condition event->rb->nr_pages != nr_pages
in perf_mmap is true when CONFIG_PERF_USE_VMALLOC is enabled where
event->rb->nr_pages = 1 and nr_pages = 2048 resulting perf_mmap to
return -EINVAL. This is because when CONFIG_PERF_USE_VMALLOC is
enabled, rb->nr_pages is always equal to 1.

Arch with CONFIG_PERF_USE_VMALLOC enabled by default:
	arc/arm/csky/mips/sh/sparc/xtensa

Arch with CONFIG_PERF_USE_VMALLOC disabled by default:
	x86_64/aarch64/...

Fix this problem by using data_page_nr()

[1] https://github.com/draios/sysdig

Fixes: 906010b ("perf_event: Provide vmalloc() based mmap() backing")
Signed-off-by: Zhipeng Xie <xiezhipeng1@huawei.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20220209145417.6495-1-xiezhipeng1@huawei.com
  • Loading branch information
Zhipeng Xie authored and Peter Zijlstra committed Apr 19, 2022
1 parent b2d229d commit 60490e7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion kernel/events/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -6247,7 +6247,7 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
again:
mutex_lock(&event->mmap_mutex);
if (event->rb) {
if (event->rb->nr_pages != nr_pages) {
if (data_page_nr(event->rb) != nr_pages) {
ret = -EINVAL;
goto unlock;
}
Expand Down
5 changes: 5 additions & 0 deletions kernel/events/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ static inline int page_order(struct perf_buffer *rb)
}
#endif

static inline int data_page_nr(struct perf_buffer *rb)
{
return rb->nr_pages << page_order(rb);
}

static inline unsigned long perf_data_size(struct perf_buffer *rb)
{
return rb->nr_pages << (PAGE_SHIFT + page_order(rb));
Expand Down
5 changes: 0 additions & 5 deletions kernel/events/ring_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,6 @@ void rb_free(struct perf_buffer *rb)
}

#else
static int data_page_nr(struct perf_buffer *rb)
{
return rb->nr_pages << page_order(rb);
}

static struct page *
__perf_mmap_to_page(struct perf_buffer *rb, unsigned long pgoff)
{
Expand Down

0 comments on commit 60490e7

Please sign in to comment.