]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
perf/core: Further simplify perf_mmap()
authorPeter Zijlstra <peterz@infradead.org>
Mon, 4 Nov 2024 13:39:25 +0000 (14:39 +0100)
committerIngo Molnar <mingo@kernel.org>
Tue, 4 Mar 2025 08:43:10 +0000 (09:43 +0100)
Perform CSE and such.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Ravi Bangoria <ravi.bangoria@amd.com>
Link: https://lore.kernel.org/r/20241104135519.354909594@infradead.org
kernel/events/core.c

index d1b04c8508810d86f5f79261b60dec68389c82e5..4cd3494c65e20ce0a2081bd5374ffaa93b327c14 100644 (file)
@@ -6698,9 +6698,18 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
                return ret;
 
        vma_size = vma->vm_end - vma->vm_start;
+       nr_pages = vma_size / PAGE_SIZE;
+
+       if (nr_pages > INT_MAX)
+               return -ENOMEM;
+
+       if (vma_size != PAGE_SIZE * nr_pages)
+               return -EINVAL;
+
+       user_extra = nr_pages;
 
        if (vma->vm_pgoff == 0) {
-               nr_pages = (vma_size / PAGE_SIZE) - 1;
+               nr_pages -= 1;
 
                /*
                 * If we have rb pages ensure they're a power-of-two number, so we
@@ -6709,9 +6718,6 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
                if (nr_pages != 0 && !is_power_of_2(nr_pages))
                        return -EINVAL;
 
-               if (vma_size != PAGE_SIZE * (1 + nr_pages))
-                       return -EINVAL;
-
                WARN_ON_ONCE(event->ctx->parent_ctx);
 again:
                mutex_lock(&event->mmap_mutex);
@@ -6735,8 +6741,6 @@ again:
                        rb = event->rb;
                        goto unlock;
                }
-
-               user_extra = nr_pages + 1;
        } else {
                /*
                 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
@@ -6748,10 +6752,6 @@ again:
                if (!event->rb)
                        return -EINVAL;
 
-               nr_pages = vma_size / PAGE_SIZE;
-               if (nr_pages > INT_MAX)
-                       return -ENOMEM;
-
                mutex_lock(&event->mmap_mutex);
                ret = -EINVAL;
 
@@ -6795,7 +6795,6 @@ again:
                }
 
                atomic_set(&rb->aux_mmap_count, 1);
-               user_extra = nr_pages;
        }
 
        user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);