Compare

de9f6bcdb44f0e410031002e2f103826ea6b6259..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

Browse files
8 files changed, +39 −77
modified arch/x86/events/intel/uncore_snbep.c
@@ -474,6 +474,9 @@
474 474 /* DMR */
475 475 #define DMR_IMH1_HIOP_MMIO_BASE 0x1ffff6ae7000
476 476 #define DMR_HIOP_MMIO_SIZE 0x8000
477 + #define DMR_CXLCM_PMON_CTL_INVERT BIT(21)
478 + #define DMR_CXLCM_PMON_RAW_EVENT_MASK (GENERIC_PMON_RAW_EVENT_MASK | \
479 + DMR_CXLCM_PMON_CTL_INVERT)
477 480 #define DMR_CXLCM_EVENT_MASK_EXT 0xf
478 481 #define DMR_HAMVF_EVENT_MASK_EXT 0xffffffff
479 482 #define DMR_PCIE4_EVENT_MASK_EXT 0xffffff
@@ -6696,7 +6699,7 @@ static struct event_constraint dmr_uncore_cxlcm_constraints[] = {
6696 6699
6697 6700 static struct intel_uncore_type dmr_uncore_cxlcm = {
6698 6701 .name = "cxlcm",
6699 - .event_mask = GENERIC_PMON_RAW_EVENT_MASK,
6702 + .event_mask = DMR_CXLCM_PMON_RAW_EVENT_MASK,
6700 6703 .event_mask_ext = DMR_CXLCM_EVENT_MASK_EXT,
6701 6704 .constraints = dmr_uncore_cxlcm_constraints,
6702 6705 .format_group = &dmr_cxlcm_uncore_format_group,
modified drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c
@@ -1266,6 +1266,7 @@ static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
1266 1266 {
1267 1267 struct amdgpu_device *adev = f->f_inode->i_private;
1268 1268 int r;
1269 + ssize_t result = 0;
1269 1270 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
1270 1271
1271 1272 if (size > 4096 || size & 3 || *pos & 3)
@@ -1309,11 +1310,25 @@ static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
1309 1310 mutex_unlock(&adev->grbm_idx_mutex);
1310 1311
1311 1312 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1312 - amdgpu_virt_disable_access_debugfs(adev);
1313 1313
1314 - r = copy_to_user(buf, data, size) ? -EFAULT : size;
1314 + while (size) {
1315 + uint32_t value;
1316 +
1317 + value = data[result >> 2];
1318 + r = put_user(value, (uint32_t *)buf);
1319 + if (r) {
1320 + amdgpu_virt_disable_access_debugfs(adev);
1321 + goto err;
1322 + }
1323 +
1324 + result += 4;
1325 + buf += 4;
1326 + size -= 4;
1327 + }
1328 +
1315 1329 kfree(data);
1316 - return r;
1330 + amdgpu_virt_disable_access_debugfs(adev);
1331 + return result;
1317 1332
1318 1333 err:
1319 1334 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
modified drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
@@ -567,10 +567,8 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
567 567
568 568 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
569 569 range = amdgpu_hmm_range_alloc(NULL);
570 - if (unlikely(!range)) {
571 - r = -ENOMEM;
572 - goto release_object;
573 - }
570 + if (unlikely(!range))
571 + return -ENOMEM;
574 572 r = amdgpu_ttm_tt_get_user_pages(bo, range);
575 573 if (r) {
576 574 amdgpu_hmm_range_free(range);
modified drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
@@ -2065,15 +2065,14 @@ static int amdgpu_ttm_buffer_entity_init(struct amdgpu_gtt_mgr *mgr,
2065 2065 int i, r, num_pages;
2066 2066 const u64 GTT_MAX_PAGES = (AMDGPU_GTT_MAX_TRANSFER_SIZE >> PAGE_SHIFT);
2067 2067
2068 - if (ARRAY_SIZE(entity->gart_window_offs) < num_gart_windows)
2069 - return -EINVAL;
2070 -
2071 2068 r = drm_sched_entity_init(&entity->base, prio, scheds, num_schedulers, NULL);
2072 2069 if (r)
2073 2070 return r;
2074 2071
2075 2072 mutex_init(&entity->lock);
2076 2073
2074 + if (ARRAY_SIZE(entity->gart_window_offs) < num_gart_windows)
2075 + return -EINVAL;
2077 2076 if (num_gart_windows == 0)
2078 2077 return 0;
2079 2078
@@ -2081,7 +2080,6 @@ static int amdgpu_ttm_buffer_entity_init(struct amdgpu_gtt_mgr *mgr,
2081 2080 r = amdgpu_gtt_mgr_alloc_entries(mgr, &entity->gart_node, num_pages,
2082 2081 DRM_MM_INSERT_BEST);
2083 2082 if (r) {
2084 - mutex_destroy(&entity->lock);
2085 2083 drm_sched_entity_destroy(&entity->base);
2086 2084 return r;
2087 2085 }
@@ -2099,7 +2097,6 @@ static void amdgpu_ttm_buffer_entity_fini(struct amdgpu_gtt_mgr *mgr,
2099 2097 struct amdgpu_ttm_buffer_entity *entity)
2100 2098 {
2101 2099 amdgpu_gtt_mgr_free_entries(mgr, &entity->gart_node);
2102 - mutex_destroy(&entity->lock);
2103 2100 drm_sched_entity_destroy(&entity->base);
2104 2101 }
2105 2102
modified drivers/gpu/drm/amd/amdgpu/uvd_v3_1.c
@@ -288,7 +288,7 @@ static int uvd_v3_1_fw_validate(struct amdgpu_device *adev)
288 288 int i;
289 289 uint32_t keysel = adev->uvd.keyselect;
290 290
291 - if (RREG32(mmUVD_FW_START)) {
291 + if (RREG32(mmUVD_FW_START) & UVD_FW_STATUS__PASS_MASK) {
292 292 dev_dbg(adev->dev, "UVD keyselect already set: 0x%x (on CPU: 0x%x)\n",
293 293 RREG32(mmUVD_FW_START), adev->uvd.keyselect);
294 294 return 0;
modified drivers/gpu/drm/amd/pm/swsmu/smu14/smu_v14_0_2_ppt.c
@@ -89,7 +89,6 @@ static const struct smu_feature_bits smu_v14_0_2_dpm_features = {
89 89 #define PP_OD_FEATURE_FAN_TARGET_TEMPERATURE 9
90 90 #define PP_OD_FEATURE_FAN_MINIMUM_PWM 10
91 91 #define PP_OD_FEATURE_FAN_ZERO_RPM_ENABLE 11
92 - #define PP_OD_FEATURE_FAN_ZERO_RPM_STOP_TEMP 12
93 92
94 93 static struct cmn2asic_msg_mapping smu_v14_0_2_message_map[SMU_MSG_MAX_COUNT] = {
95 94 MSG_MAP(TestMessage, PPSMC_MSG_TestMessage, 1),
@@ -1021,10 +1020,6 @@ static void smu_v14_0_2_get_od_setting_limits(struct smu_context *smu,
1021 1020 od_min_setting = overdrive_lowerlimits->FanZeroRpmEnable;
1022 1021 od_max_setting = overdrive_upperlimits->FanZeroRpmEnable;
1023 1022 break;
1024 - case PP_OD_FEATURE_FAN_ZERO_RPM_STOP_TEMP:
1025 - od_min_setting = pptable->SkuTable.MsgLimits.FanStopTempMin;
1026 - od_max_setting = pptable->SkuTable.MsgLimits.FanStopTempMax;
1027 - break;
1028 1023 default:
1029 1024 od_min_setting = od_max_setting = INT_MAX;
1030 1025 break;
@@ -1249,24 +1244,6 @@ static int smu_v14_0_2_emit_clk_levels(struct smu_context *smu,
1249 1244 min_value, max_value);
1250 1245 break;
1251 1246
1252 - case SMU_OD_FAN_ZERO_RPM_STOP_TEMP:
1253 - if (!smu_v14_0_2_is_od_feature_supported(smu,
1254 - PP_OD_FEATURE_ZERO_FAN_BIT))
1255 - break;
1256 -
1257 - size += sysfs_emit_at(buf, size, "FAN_ZERO_RPM_STOP_TEMPERATURE:\n");
1258 - size += sysfs_emit_at(buf, size, "%d\n",
1259 - (int)od_table->OverDriveTable.FanZeroRpmStopTemp);
1260 -
1261 - size += sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
1262 - smu_v14_0_2_get_od_setting_limits(smu,
1263 - PP_OD_FEATURE_FAN_ZERO_RPM_STOP_TEMP,
1264 - &min_value,
1265 - &max_value);
1266 - size += sysfs_emit_at(buf, size, "ZERO_RPM_STOP_TEMPERATURE: %u %u\n",
1267 - min_value, max_value);
1268 - break;
1269 -
1270 1247 case SMU_OD_RANGE:
1271 1248 if (!smu_v14_0_2_is_od_feature_supported(smu, PP_OD_FEATURE_GFXCLK_BIT) &&
1272 1249 !smu_v14_0_2_is_od_feature_supported(smu, PP_OD_FEATURE_UCLK_BIT) &&
@@ -2334,9 +2311,7 @@ static void smu_v14_0_2_set_supported_od_feature_mask(struct smu_context *smu)
2334 2311 OD_OPS_SUPPORT_FAN_MINIMUM_PWM_RETRIEVE |
2335 2312 OD_OPS_SUPPORT_FAN_MINIMUM_PWM_SET |
2336 2313 OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_RETRIEVE |
2337 - OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_SET |
2338 - OD_OPS_SUPPORT_FAN_ZERO_RPM_STOP_TEMP_RETRIEVE |
2339 - OD_OPS_SUPPORT_FAN_ZERO_RPM_STOP_TEMP_SET;
2314 + OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_SET;
2340 2315 }
2341 2316
2342 2317 static int smu_v14_0_2_get_overdrive_table(struct smu_context *smu,
@@ -2417,8 +2392,6 @@ static int smu_v14_0_2_set_default_od_settings(struct smu_context *smu)
2417 2392 user_od_table_bak.OverDriveTable.FanMinimumPwm;
2418 2393 user_od_table->OverDriveTable.FanZeroRpmEnable =
2419 2394 user_od_table_bak.OverDriveTable.FanZeroRpmEnable;
2420 - user_od_table->OverDriveTable.FanZeroRpmStopTemp =
2421 - user_od_table_bak.OverDriveTable.FanZeroRpmStopTemp;
2422 2395 }
2423 2396
2424 2397 smu_v14_0_2_set_supported_od_feature_mask(smu);
@@ -2473,11 +2446,6 @@ static int smu_v14_0_2_od_restore_table_single(struct smu_context *smu, long inp
2473 2446 boot_overdrive_table->OverDriveTable.FanZeroRpmEnable;
2474 2447 od_table->OverDriveTable.FeatureCtrlMask |= BIT(PP_OD_FEATURE_ZERO_FAN_BIT);
2475 2448 break;
2476 - case PP_OD_EDIT_FAN_ZERO_RPM_STOP_TEMP:
2477 - od_table->OverDriveTable.FanZeroRpmStopTemp =
2478 - boot_overdrive_table->OverDriveTable.FanZeroRpmStopTemp;
2479 - od_table->OverDriveTable.FeatureCtrlMask |= BIT(PP_OD_FEATURE_ZERO_FAN_BIT);
2480 - break;
2481 2449 case PP_OD_EDIT_ACOUSTIC_LIMIT:
2482 2450 od_table->OverDriveTable.AcousticLimitRpmThreshold =
2483 2451 boot_overdrive_table->OverDriveTable.AcousticLimitRpmThreshold;
@@ -2784,27 +2752,6 @@ static int smu_v14_0_2_od_edit_dpm_table(struct smu_context *smu,
2784 2752 od_table->OverDriveTable.FeatureCtrlMask |= BIT(PP_OD_FEATURE_ZERO_FAN_BIT);
2785 2753 break;
2786 2754
2787 - case PP_OD_EDIT_FAN_ZERO_RPM_STOP_TEMP:
2788 - if (!smu_v14_0_2_is_od_feature_supported(smu, PP_OD_FEATURE_ZERO_FAN_BIT)) {
2789 - dev_warn(adev->dev, "Zero RPM setting not supported!\n");
2790 - return -ENOTSUPP;
2791 - }
2792 -
2793 - smu_v14_0_2_get_od_setting_limits(smu,
2794 - PP_OD_FEATURE_FAN_ZERO_RPM_STOP_TEMP,
2795 - &minimum,
2796 - &maximum);
2797 - if (input[0] < minimum ||
2798 - input[0] > maximum) {
2799 - dev_info(adev->dev, "zero RPM stop temperature setting(%ld) must be within [%d, %d]!\n",
2800 - input[0], minimum, maximum);
2801 - return -EINVAL;
2802 - }
2803 -
2804 - od_table->OverDriveTable.FanZeroRpmStopTemp = input[0];
2805 - od_table->OverDriveTable.FeatureCtrlMask |= BIT(PP_OD_FEATURE_ZERO_FAN_BIT);
2806 - break;
2807 -
2808 2755 case PP_OD_RESTORE_DEFAULT_TABLE:
2809 2756 if (size == 1) {
2810 2757 ret = smu_v14_0_2_od_restore_table_single(smu, input[0]);
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