Commit
78436f3882fb8d848c823bf8cf754988202c5ca5
perf: Track the perf mlock gift separately
The locked_vm field in struct user_struct was originally introduced by
commit 789f90fcf6b0 ("perf_counter: per user mlock gift") to provide an
mlock-like budget for perf measured against mm->pinned_vm (since 2011).
perf grants each user a 'gift' of perf_event_mlock_kb * online CPUs of
buffer pages which is charged to user->locked_vm without any RLIMIT_MEMLOCK
check.
Only pages beyond the gift are checked against RLIMIT_MEMLOCK, per-mm, via
mm->pinned_vm, which makes it different from the standard mlock() check,
which is per-mm and made against RLIMIT_MEMLOCK.
However, since this was introduced, a number of other components have
utilised this field where a shared resource needed to be limited against
RLIMIT_MEMLOCK.
The other users are currently MSG_ZEROCOPY, io_uring, AF_XDP, iommufd,
s390 KVM zPCI and most recently, secretmem in commit 97d34aa65c29
("mm/secretmem: properly account locked pages").
This field is shared between all of these, but because the gift is not
checked against RLIMIT_MEMLOCK, user->locked_vm alone can exceed it - 16
CPUs * 516 KiB is already more than the default 8 MiB - after which every
other user of the field fails unconditionally.
This was reported by a user who saw secretmem failing while running a
simultaneous perf record.
Resolve the issue by simply giving perf its own field separate from the
rest.
BPF used user->locked_vm up until commit 80ee81e0403c ("bpf: Eliminate
rlimit-based memory accounting infra for bpf maps") which landed in 5.11.
Thus also eliminate the now-defunct ifdef for CONFIG_BPF_SYSCALL for
user->locked_vm.
This issue exists between all of the components which use user->locked_vm
and perf, however secretmem is the first user who has caused a report over
many years and has been backported, so target the fix at that.
Fixes: 97d34aa65c29 ("mm/secretmem: properly account locked pages")
Closes: https://lore.kernel.org/20261002175651.811343-1-ameer.hamza@truenas.com
Reported-by: Ameer Hamza <ameer.hamza@truenas.com>
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20261003-perf-locked-vm-fix-v1-1-d214dcaf0d76@kernel.org
Lorenzo Stoakes (ARM) <ljs@kernel.org> · 2026-10-03 16:29 UTC · committed by Peter Zijlstra
2 files changed,
+11
−9
modified include/linux/sched/user.h
| @@ -23,11 +23,13 @@ struct user_struct { | |||
| 23 | 23 | struct hlist_node uidhash_node; | |
| 24 | 24 | kuid_t uid; | |
| 25 | 25 | ||
| 26 | - | #if defined(CONFIG_PERF_EVENTS) || defined(CONFIG_BPF_SYSCALL) || \ | |
| 27 | - | defined(CONFIG_NET) || defined(CONFIG_IO_URING) || \ | |
| 26 | + | #if defined(CONFIG_NET) || defined(CONFIG_IO_URING) || \ | |
| 28 | 27 | defined(CONFIG_VFIO_PCI_ZDEV_KVM) || IS_ENABLED(CONFIG_IOMMUFD) || \ | |
| 29 | 28 | defined(CONFIG_SECRETMEM) | |
| 30 | - | atomic_long_t locked_vm; | |
| 29 | + | atomic_long_t locked_vm; /* pinned pages charged to RLIMIT_MEMLOCK */ | |
| 30 | + | #endif | |
| 31 | + | #ifdef CONFIG_PERF_EVENTS | |
| 32 | + | atomic_long_t perf_mlock; /* perf buffer pages within perf_event_mlock_kb */ | |
| 31 | 33 | #endif | |
| 32 | 34 | #ifdef CONFIG_WATCH_QUEUE | |
| 33 | 35 | atomic_t nr_watches; /* The number of watches this user currently has */ | |
modified kernel/events/core.c
| @@ -7059,7 +7059,7 @@ static void perf_mmap_close(struct vm_area_struct *vma) | |||
| 7059 | 7059 | perf_pmu_output_stop(event); | |
| 7060 | 7060 | ||
| 7061 | 7061 | /* now it's safe to free the pages */ | |
| 7062 | - | atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->locked_vm); | |
| 7062 | + | atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->perf_mlock); | |
| 7063 | 7063 | atomic64_sub(rb->aux_mmap_locked, &vma->vm_mm->pinned_vm); | |
| 7064 | 7064 | ||
| 7065 | 7065 | /* this has to be the last one */ | |
| @@ -7242,11 +7242,11 @@ static bool perf_mmap_calc_limits(struct vm_area_struct *vma, long *user_extra, | |||
| 7242 | 7242 | /* Increase the limit linearly with more CPUs */ | |
| 7243 | 7243 | user_lock_limit *= num_online_cpus(); | |
| 7244 | 7244 | ||
| 7245 | - | user_locked = atomic_long_read(&user->locked_vm); | |
| 7245 | + | user_locked = atomic_long_read(&user->perf_mlock); | |
| 7246 | 7246 | ||
| 7247 | 7247 | /* | |
| 7248 | 7248 | * sysctl_perf_event_mlock may have changed, so that | |
| 7249 | - | * user->locked_vm > user_lock_limit | |
| 7249 | + | * user->perf_mlock > user_lock_limit | |
| 7250 | 7250 | */ | |
| 7251 | 7251 | if (user_locked > user_lock_limit) | |
| 7252 | 7252 | user_locked = user_lock_limit; | |
| @@ -7254,7 +7254,7 @@ static bool perf_mmap_calc_limits(struct vm_area_struct *vma, long *user_extra, | |||
| 7254 | 7254 | ||
| 7255 | 7255 | if (user_locked > user_lock_limit) { | |
| 7256 | 7256 | /* | |
| 7257 | - | * charge locked_vm until it hits user_lock_limit; | |
| 7257 | + | * charge perf_mlock until it hits user_lock_limit; | |
| 7258 | 7258 | * charge the rest from pinned_vm | |
| 7259 | 7259 | */ | |
| 7260 | 7260 | *extra = user_locked - user_lock_limit; | |
| @@ -7272,7 +7272,7 @@ static void perf_mmap_account(struct vm_area_struct *vma, long user_extra, long | |||
| 7272 | 7272 | { | |
| 7273 | 7273 | struct user_struct *user = current_user(); | |
| 7274 | 7274 | ||
| 7275 | - | atomic_long_add(user_extra, &user->locked_vm); | |
| 7275 | + | atomic_long_add(user_extra, &user->perf_mlock); | |
| 7276 | 7276 | atomic64_add(extra, &vma->vm_mm->pinned_vm); | |
| 7277 | 7277 | } | |
| 7278 | 7278 | ||
| @@ -7281,7 +7281,7 @@ static void perf_mmap_unaccount(struct vm_area_struct *vma, struct perf_buffer * | |||
| 7281 | 7281 | struct user_struct *user = rb->mmap_user; | |
| 7282 | 7282 | ||
| 7283 | 7283 | atomic_long_sub((perf_data_size(rb) >> PAGE_SHIFT) + 1 - rb->mmap_locked, | |
| 7284 | - | &user->locked_vm); | |
| 7284 | + | &user->perf_mlock); | |
| 7285 | 7285 | atomic64_sub(rb->mmap_locked, &vma->vm_mm->pinned_vm); | |
| 7286 | 7286 | } | |
| 7287 | 7287 | ||