perf/core: fix locked_vm leak on alias mmap() and cross-MM pinned_vm underflow
Subsystems (2)
PATCH 1/1 perf/core: fix locked_vm leak on alias mmap() and cross-MM pinned_vm underflow
In perf_mmap(), mapping an output ring buffer onto an event via PERF_EVENT_IOC_SET_OUTPUT / alias mmap() increments user->locked_vm and mm->pinned_vm without recording the accounting MM on the ring_buffer or releasing user_extra onperf_mmap_close(), causing a permanent locked_vm leak and a cross-MM pinned_vm underflow when the mapping is closed in a different process. Store the accounted mm_struct on struct perf_buffer and unaccount against the original mm_struct on close.
---
kernel/events/core.c +12 -4
@@ -7050,7 +7050,11 @@ static void perf_mmap_close(struct vm_area_struct *vma)/* now it's safe to free the pages */ atomic_long_sub (rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->locked_vm); -atomic64_sub (rb->aux_mmap_locked, &vma->vm_mm->pinned_vm); +if (rb->aux_mmap_mm) { +atomic64_sub (rb->aux_mmap_locked, &rb->aux_mmap_mm->pinned_vm); +mmdrop (rb->aux_mmap_mm); + rb->aux_mmap_mm =NULL ; + }/* this has to be the last one */ rb_free_aux (rb);
@@ -7272,7 +7276,7 @@ static void perf_mmap_unaccount(struct vm_area_struct *vma, struct perf_buffer *atomic_long_sub ((perf_data_size (rb) >>PAGE_SHIFT ) +1 - rb->mmap_locked, &user->locked_vm); -atomic64_sub (rb->mmap_locked, &vma->vm_mm->pinned_vm); +atomic64_sub (rb->mmap_locked, &rb->mmap_mm->pinned_vm); } static int perf_mmap_rb(struct vm_area_struct *vma, struct perf_event *event,
@@ -7311,7 +7315,6 @@ static int perf_mmap_rb(struct vm_area_struct *vma, struct perf_event *event,* Success -- managed to mmap() the same buffer * multiple times. */ -perf_mmap_account (vma, user_extra, extra);refcount_inc (&event->mmap_count);return 0 ; }
@@ -7338,6 +7341,8 @@ static int perf_mmap_rb(struct vm_area_struct *vma, struct perf_event *event,return -ENOMEM ; rb->mmap_locked = extra; + rb->mmap_mm = vma->vm_mm; +mmgrab (rb->mmap_mm);ring_buffer_attach (event, rb);
@@ -7399,7 +7404,8 @@ static int perf_mmap_aux(struct vm_area_struct *vma, struct perf_event *event,if (rb_has_aux (rb)) {refcount_inc (&rb->aux_mmap_count); - + user_extra =0 ; + extra =0 ; }else {if (!perf_mmap_calc_limits (vma, &user_extra, &extra)) {refcount_dec (&rb->mmap_count);
@@ -7420,6 +7426,8 @@ static int perf_mmap_aux(struct vm_area_struct *vma, struct perf_event *event,refcount_set (&rb->aux_mmap_count,1 ); rb->aux_mmap_locked = extra; + rb->aux_mmap_mm = vma->vm_mm; +mmgrab (rb->aux_mmap_mm); }perf_mmap_account (vma, user_extra, extra);
kernel/events/internal.h +7 -0
@@ -5,6 +5,7 @@#include <linux/hardirq.h> #include <linux/uaccess.h> #include <linux/refcount.h> +#include <linux/sched/mm.h> /* Buffer handling */
@@ -38,6 +39,8 @@ struct perf_buffer {refcount_t mmap_count;unsigned long mmap_locked;struct user_struct *mmap_user; +struct mm_struct *mmap_mm; +struct mm_struct *aux_mmap_mm;/* AUX area */ struct mutex aux_mutex;
@@ -67,6 +70,10 @@ static inline void rb_free_rcu(struct rcu_head *rcu_head)struct perf_buffer *rb; rb = container_of(rcu_head, struct perf_buffer, rcu_head); +if (rb->aux_mmap_mm) +mmdrop (rb->aux_mmap_mm); +if (rb->mmap_mm) +mmdrop (rb->mmap_mm);free_uid (rb->mmap_user);rb_free (rb); }
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] perf: mmdrop() in RCU callback causes softirq context violation --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] perf: mmdrop() in RCU callback causes softirq context violation
--
commit f66e9c8b5f2a9c636e7dc5afd975c9e2ed17cf78
Author: Hui Peng <[email protected]>
perf/core: fix locked_vm leak on alias mmap() and cross-MM pinned_vm underflow
In perf_mmap(), mapping an output ring buffer onto an event via PERF_EVENT_IOC_SET_OUTPUT / alias mmap() increments user->locked_vm and mm->pinned_vm without recording the accounting MM on the ring_buffer or releasing user_extra onperf_mmap_close(), causing a permanent locked_vm leak and a cross-MM pinned_vm underflow when the mapping is closed in a different process. Store the accounted mm_struct on struct perf_buffer and unaccount against the original mm_struct on close.
Quoted 4 lines
kernel/events/internal.h +0
-0
[ ... ]
kernel/events/internal.h static inline void rb_free_rcu(struct rcu_head *rcu_head) · line 76
Quoted 8 lines
kernel/events/internal.h +4
-0@@ -67,6 +70,10 @@ static inline void rb_free_rcu(struct rcu_head *rcu_head)struct perf_buffer *rb; rb = container_of(rcu_head, struct perf_buffer, rcu_head); +if (rb->aux_mmap_mm) +mmdrop (rb->aux_mmap_mm); +if (rb->mmap_mm) +mmdrop (rb->mmap_mm);
[Severity: High]
Does this code call mmdrop() in an RCU callback, which executes in a softirq context?
If this mmdrop() drops the final mm_count reference (for instance, if the original process has already exited), it synchronously invokes __mmdrop(). __mmdrop() performs blocking teardown operations (such as pgd_free() and percpu_counter_destroy()) which can sleep.
Sleeping in a softirq context is illegal and leads to a kernel BUG or panic.
This could happen if a process mmaps a perf event to create a ring buffer, which uses mmgrab() to increment mm_count. If the process then exits, dropping its own mm reference, and later the ring buffer is unmapped (e.g., by another process closing the alias), this triggers ring_buffer_put(), which schedules rb_free_rcu() via call_rcu(). The RCU callback then drops the final reference and triggers the bug.
Should this teardown be deferred to the workqueue (rb_free_work) which runs in a process context, or perhaps use mmdrop_async() instead?
Quoted 3 lines
kernel/events/internal.h +0
-0free_uid (rb->mmap_user);rb_free (rb); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 perf/core: fix user->locked_vm leak on secondary mmap()
In reply to Hui PengCommit 0c8a4e4139ad ("perf/core: Further simplify perf_mmap()") hoisted user_extra = nr_pages ahead of the existing-buffer checks (if (event->rb) and if (rb_has_aux(rb))), and subsequently commit 5d299897f1e3 ("perf: Split out the RB allocation") and commit 2aee37682391 ("perf: Split out the AUX buffer allocation") carried long extra = 0, user_extra = nr_pages into perf_mmap_rb() and perf_mmap_aux().
As a result, when an already-allocated ring buffer (event->rb) or AUX buffer (rb_has_aux(rb)) is mapped again via mmap(), perf_mmap_account() is called with user_extra = nr_pages instead of 0, charging nr_pages to current_user()->locked_vm on every additional mapping. However, perf_mmap_unaccount() and perf_mmap_close() only subtract the ring buffer's and AUX buffer's pages once when the final rb->mmap_count / rb->aux_mmap_count reference drops to zero. Consequently, every secondary mmap() + munmap() cycle on a perf event permanently leaks nr_pages in user->locked_vm, eventually exhausting perf_event_mlock_kb and RLIMIT_MEMLOCK (-EPERM) for that user.
Fix this by only calling perf_mmap_account() when allocating a new ring buffer in perf_mmap_rb() or a new AUX buffer in perf_mmap_aux().
Tested in QEMU against Linux 7.3.0-rc3 with a standalone C reproducer running as an unprivileged user (UID 1000, RLIMIT_MEMLOCK=0, perf_event_paranoid=1) that opens a software perf event, maps a 65-page ring buffer, performs 10 secondary mmap() + munmap() cycles on the same event fd, and then unmaps and closes the event. On the unfixed kernel, subsequent perf_mmap() calls by UID 1000 permanently fail with -EPERM due to the leaked user->locked_vm (650 pages leaked), whereas with the fix applied user->locked_vm returns to 0 and subsequent perf_mmap() calls succeed.
---
Changes in v2:
- Dropped the cross-MM pinned_vm / rb->mmap_mm (mmgrab/mmdrop) changes (which caused an RCU softirq context violation in rb_free_rcu() in v1, flagged by sashiko-bot) to keep this patch focused on the user->locked_vm leak on secondary mmap().
- Updated the Fixes: tag to 0c8a4e4139ad ("perf/core: Further simplify perf_mmap()") and added QEMU reproducer test details to the commit message.
kernel/events/core.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
kernel/events/core.c +1 -3
@@ -7311,7 +7311,6 @@ static int perf_mmap_rb(struct vm_area_struct *vma, struct perf_event *event,* Success -- managed to mmap() the same buffer * multiple times. */ -perf_mmap_account (vma, user_extra, extra);refcount_inc (&event->mmap_count);return 0 ; }
@@ -7399,7 +7398,6 @@ static int perf_mmap_aux(struct vm_area_struct *vma, struct perf_event *event,if (rb_has_aux (rb)) {refcount_inc (&rb->aux_mmap_count); - }else {if (!perf_mmap_calc_limits (vma, &user_extra, &extra)) {refcount_dec (&rb->mmap_count);
@@ -7420,9 +7418,9 @@ static int perf_mmap_aux(struct vm_area_struct *vma, struct perf_event *event,refcount_set (&rb->aux_mmap_count,1 ); rb->aux_mmap_locked = extra; +perf_mmap_account (vma, user_extra, extra); } -perf_mmap_account (vma, user_extra, extra);refcount_inc (&event->mmap_count);return 0 ;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v2] perf/core: fix user->locked_vm leak on secondary mmap()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines
Commit
0c8a4e4139ad("perf/core: Further simplifyperf_mmap()") hoisteduser_extra = nr_pagesahead of the existing-buffer checks (if (event->rb)andif (rb_has_aux(rb))), and subsequently commit5d299897f1e3("perf: Split out the RB allocation") and commit2aee37682391("perf: Split out the AUX buffer allocation") carriedlong extra = 0, user_extra = nr_pagesintoperf_mmap_rb()andperf_mmap_aux().As a result, when an already-allocated ring buffer (
event->rb) or AUX buffer (rb_has_aux(rb)) is mapped again viammap(),perf_mmap_account()is called withuser_extra = nr_pagesinstead of0, chargingnr_pagestocurrent_user()->locked_vmon every additional mapping. However,perf_mmap_unaccount()andperf_mmap_close()only subtract the ring buffer's and AUX buffer's pages once when the finalrb->mmap_count/rb->aux_mmap_countreference drops to zero. Consequently, every secondary [ ... ]
Quoted 4 lines
Fixes 0c8a4e4139ad ("perf/core: Further simplify perf_mmap()")Assisted-by LLMSigned-off-by Hui Peng <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v2] perf/core: fix user->locked_vm leak on secondary mmap()
In reply to Hui PengQuoted 46 lines · Hui Peng
Commit
0c8a4e4139ad("perf/core: Further simplifyperf_mmap()") hoisteduser_extra = nr_pagesahead of the existing-buffer checks (if (event->rb)andif (rb_has_aux(rb))), and subsequently commit5d299897f1e3("perf: Split out the RB allocation") and commit2aee37682391("perf: Split out the AUX buffer allocation") carriedlong extra = 0, user_extra = nr_pagesintoperf_mmap_rb()andperf_mmap_aux().As a result, when an already-allocated ring buffer (
event->rb) or AUX buffer (rb_has_aux(rb)) is mapped again viammap(),perf_mmap_account()is called withuser_extra = nr_pagesinstead of0, chargingnr_pagestocurrent_user()->locked_vmon every additional mapping. However,perf_mmap_unaccount()andperf_mmap_close()only subtract the ring buffer's and AUX buffer's pages once when the finalrb->mmap_count/rb->aux_mmap_countreference drops to zero. Consequently, every secondarymmap()+munmap()cycle on a perf event permanently leaksnr_pagesinuser->locked_vm, eventually exhaustingperf_event_mlock_kbandRLIMIT_MEMLOCK(-EPERM) for that user.Fix this by only calling
perf_mmap_account()when allocating a new ring buffer inperf_mmap_rb()or a new AUX buffer inperf_mmap_aux().Tested in QEMU against Linux 7.3.0-rc3 with a standalone C reproducer running as an unprivileged user (UID 1000, RLIMIT_MEMLOCK=0, perf_event_paranoid=1) that opens a software perf event, maps a 65-page ring buffer, performs 10 secondary
mmap()+munmap()cycles on the same event fd, and then unmaps and closes the event. On the unfixed kernel, subsequentperf_mmap()calls by UID 1000 permanently fail with-EPERMdue to the leakeduser->locked_vm(650 pages leaked), whereas with the fix applieduser->locked_vmreturns to 0 and subsequentperf_mmap()calls succeed.Fixes 0c8a4e4139ad ("perf/core: Further simplify perf_mmap()")Assisted-by LLMSigned-off-by Hui Peng <[email protected]>---
Changes in v2:
- Dropped the cross-MMpinned_vm/rb->mmap_mm(mmgrab/mmdrop) changes (which caused an RCU softirq context violation inrb_free_rcu()in v1, flagged by sashiko-bot) to keep this patch focused on theuser->locked_vmleak on secondarymmap().
- Updated theFixes:tag to0c8a4e4139ad ("perf/core: Further simplify perf_mmap()")and added QEMU reproducer test details to the commit message.
Quoted 3 lines
kernel/events/core.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
You sent multiple independent patches, to multiple independent subsystems. The amount of these patches clearly suggest this was AI generated and most likely not tested.
More importantly, you sent all this work without properly organizing relevant patches into patchsets. This makes reviewing difficult and might cause multiple reviewers to address the same issue. Replying to the entire set is impossible and requires handling each patch independently, instead of applying or discarding the set. Maintainers also won't see the bigger picture of your work. Quite worrying.
This is on the verge of hostile patch: bomb us with so many contributions, we won't be able to handle them in efficient manner, like responding ONCE to ask you to slow down. Considering all this is untested and LLM generated, I have even more doubts whether this should be considered for review.
Please read kernel documentation BEFORE posting more work. It will explain you how to identify subsystems, how to organize your work per subsystem, how to document usage of LLM and how what you should not do if this was posted in a good faith.
Best regards,
Krzysztof
Re: [PATCH v2] perf/core: fix user->locked_vm leak on secondary mmap()
In reply to Hui PengQuoted 46 lines · Hui Peng
Commit
0c8a4e4139ad("perf/core: Further simplifyperf_mmap()") hoisteduser_extra = nr_pagesahead of the existing-buffer checks (if (event->rb)andif (rb_has_aux(rb))), and subsequently commit5d299897f1e3("perf: Split out the RB allocation") and commit2aee37682391("perf: Split out the AUX buffer allocation") carriedlong extra = 0, user_extra = nr_pagesintoperf_mmap_rb()andperf_mmap_aux().As a result, when an already-allocated ring buffer (
event->rb) or AUX buffer (rb_has_aux(rb)) is mapped again viammap(),perf_mmap_account()is called withuser_extra = nr_pagesinstead of0, chargingnr_pagestocurrent_user()->locked_vmon every additional mapping. However,perf_mmap_unaccount()andperf_mmap_close()only subtract the ring buffer's and AUX buffer's pages once when the finalrb->mmap_count/rb->aux_mmap_countreference drops to zero. Consequently, every secondarymmap()+munmap()cycle on a perf event permanently leaksnr_pagesinuser->locked_vm, eventually exhaustingperf_event_mlock_kbandRLIMIT_MEMLOCK(-EPERM) for that user.Fix this by only calling
perf_mmap_account()when allocating a new ring buffer inperf_mmap_rb()or a new AUX buffer inperf_mmap_aux().Tested in QEMU against Linux 7.3.0-rc3 with a standalone C reproducer running as an unprivileged user (UID 1000, RLIMIT_MEMLOCK=0, perf_event_paranoid=1) that opens a software perf event, maps a 65-page ring buffer, performs 10 secondary
mmap()+munmap()cycles on the same event fd, and then unmaps and closes the event. On the unfixed kernel, subsequentperf_mmap()calls by UID 1000 permanently fail with-EPERMdue to the leakeduser->locked_vm(650 pages leaked), whereas with the fix applieduser->locked_vmreturns to 0 and subsequentperf_mmap()calls succeed.Fixes 0c8a4e4139ad ("perf/core: Further simplify perf_mmap()")Assisted-by LLMSigned-off-by Hui Peng <[email protected]>---
Changes in v2:
- Dropped the cross-MMpinned_vm/rb->mmap_mm(mmgrab/mmdrop) changes (which caused an RCU softirq context violation inrb_free_rcu()in v1, flagged by sashiko-bot) to keep this patch focused on theuser->locked_vmleak on secondarymmap().
- Updated theFixes:tag to0c8a4e4139ad ("perf/core: Further simplify perf_mmap()")and added QEMU reproducer test details to the commit message.
Quoted 3 lines
kernel/events/core.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
You sent multiple independent patches, to multiple independent subsystems. The amount of these patches clearly suggest this was AI generated and most likely not tested.
More importantly, you sent all this work without properly organizing relevant patches into patchsets. This makes reviewing difficult and might cause multiple reviewers to address the same issue. Replying to the entire set is impossible and requires handling each patch independently, instead of applying or discarding the set. Maintainers also won't see the bigger picture of your work. Quite worrying.
This is on the verge of hostile patch: bomb us with so many contributions, we won't be able to handle them in efficient manner, like responding ONCE to ask you to slow down. Considering all this is untested and LLM generated, I have even more doubts whether this should be considered for review.
Please read kernel documentation BEFORE posting more work. It will explain you how to identify subsystems, how to organize your work per subsystem, how to document usage of LLM and how what you should not do if this was posted in a good faith.
Best regards,
Krzysztof
Re: [PATCH v2] perf/core: fix user->locked_vm leak on secondary mmap()
In reply to Hui PengQuoted 2 lines · Hui Peng
Fix this by only calling
perf_mmap_account()when allocating a new ring buffer inperf_mmap_rb()or a new AUX buffer inperf_mmap_aux().
Same thing I found reading perf_mmap_rb(), and I tested your patch. It applies to current mainline (0fcb05b72344) and I confirmed the leak and the fix there.
Instead of reading it off an EPERM I measured user->locked_vm directly:io_uring buffer registration charges the same counter against RLIMIT_MEMLOCK, so the largest buffer that still registers gives the page count. x86_64 defconfig under KVM, 2 and 4 vCPUs, each case on its own uid. Mapping a buffer three more times and unmapping all of them, pages left charged after the last munmap(), before -> after:
ring, one process 195 -> 0
ring, fd handed to workers 195 -> 0
AUX area 192 -> 0
The charge follows the event fd, not the mm, so a supervisor that opens per-CPU events and lets a worker map them leaks the buffer size on every worker restart.
Your reproducer covers the ring buffer, so I ran the AUX path too. With no PT/SPE/CoreSight here I used a throwaway test-only PMU (just setup_aux/free_aux, enough to drive the accounting); same leak, same fix.
Worth noting this is independent of Lorenzo's "track the perf mlock gift separately" [1]: that keeps perf from starving the other locked_vm users, this kills the per-buffer double charge. Both are needed.
The reproducer and test harness were LLM-assisted; the measurements and this report are mine.
[1] https://lore.kernel.org/all/[email protected]/
Thanks,
Geyslan