Compare

0fa993581aaa58a34ae3dcebebe0fbf593fcb441..3c44cbcec5eb370713bbd2c315ed7c34d8ccec2b

drm/amdgpu: keep the eviction fence until the last handle is closed

amdgpu_gem_object_close() detaches the user queue eviction fence from
the BO before it checks bo_va->ref_count, so closing any handle of a BO
removes the fence, even when the file still has another handle to the
BO and the bo_va with its GPU mappings stays. The BO can then be evicted
without suspending the user queues of the file, which keep accessing it
through mappings that are only marked for update.

Seen on navi31 with a SDMA user queue: a BO opened twice in the same
file (GEM_FLINK + GEM_OPEN) loses its eviction fence when the second
handle is closed (traced attach/detach of the fence):

  attach bo=ffff8c78a6c84c00
  GEM_CLOSE handle=7
  detach bo=ffff8c78a6c84c00

while handle 6 and the mapping are still in use.

Only detach the fence when the last reference to the bo_va goes away,
matching amdgpu_gem_object_open(), which attaches it only when the
bo_va is created. With this the fence stays until the file is closed.

Fixes: 31f7efcdca4d ("drm/amdgpu: enable eviction fence")
Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
(cherry picked from commit 8387982f03d4026b7dc11b0b5e3118298eee4d6a)
Cc: stable@vger.kernel.org

Jesse Zhang <Jesse.Zhang@amd.com> · 2026-10-12 04:24 UTC · committed by Alex Deucher

Browse files
13 files changed, +114 −54
modified drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
@@ -1431,6 +1431,7 @@ static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser)
1431 1431 int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1432 1432 {
1433 1433 struct amdgpu_device *adev = drm_to_adev(dev);
1434 + struct amdgpu_bo_list_entry *e;
1434 1435 struct amdgpu_cs_parser parser;
1435 1436 int r;
1436 1437
@@ -1465,25 +1466,31 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1465 1466
1466 1467 r = amdgpu_cs_patch_jobs(&parser);
1467 1468 if (r)
1468 - goto error_fini;
1469 + goto error_free_user_pages;
1469 1470
1470 1471 r = amdgpu_cs_vm_handling(&parser);
1471 1472 if (r)
1472 - goto error_fini;
1473 + goto error_free_user_pages;
1473 1474
1474 1475 r = amdgpu_cs_sync_rings(&parser);
1475 1476 if (r)
1476 - goto error_fini;
1477 + goto error_free_user_pages;
1477 1478
1478 1479 trace_amdgpu_cs_ibs(&parser);
1479 1480
1480 1481 r = amdgpu_cs_submit(&parser, data);
1481 1482 if (r)
1482 - goto error_fini;
1483 + goto error_free_user_pages;
1483 1484
1484 1485 amdgpu_cs_parser_fini(&parser);
1485 1486 return 0;
1486 1487
1488 + error_free_user_pages:
1489 + /* amdgpu_cs_submit() only drops the ranges once it got that far. */
1490 + amdgpu_bo_list_for_each_userptr_entry(e, parser.bo_list) {
1491 + amdgpu_hmm_range_free(e->range);
1492 + e->range = NULL;
1493 + }
1487 1494 error_fini:
1488 1495 amdgpu_cs_parser_fini(&parser);
1489 1496 return r;
modified drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c
@@ -1266,7 +1266,6 @@ 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;
1270 1269 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
1271 1270
1272 1271 if (size > 4096 || size & 3 || *pos & 3)
@@ -1310,25 +1309,11 @@ static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
1310 1309 mutex_unlock(&adev->grbm_idx_mutex);
1311 1310
1312 1311 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1312 + amdgpu_virt_disable_access_debugfs(adev);
1313 1313
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 -
1314 + r = copy_to_user(buf, data, size) ? -EFAULT : size;
1329 1315 kfree(data);
1330 - amdgpu_virt_disable_access_debugfs(adev);
1331 - return result;
1316 + return r;
1332 1317
1333 1318 err:
1334 1319 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
modified drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
@@ -337,11 +337,14 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,
337 337 goto out_unlock;
338 338 }
339 339
340 + bo_va = amdgpu_vm_bo_find(vm, bo);
341 + /* the fence is attached once per bo_va, so keep it for other handles */
342 + if (bo_va && --bo_va->ref_count)
343 + goto out_unlock;
344 +
340 345 if (!amdgpu_vm_is_bo_always_valid(vm, bo))
341 346 amdgpu_evf_mgr_detach_fence(&fpriv->evf_mgr, bo);
342 -
343 - bo_va = amdgpu_vm_bo_find(vm, bo);
344 - if (!bo_va || --bo_va->ref_count)
347 + if (!bo_va)
345 348 goto out_unlock;
346 349
347 350 amdgpu_vm_bo_del(adev, bo_va);
@@ -567,8 +570,10 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
567 570
568 571 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
569 572 range = amdgpu_hmm_range_alloc(NULL);
570 - if (unlikely(!range))
571 - return -ENOMEM;
573 + if (unlikely(!range)) {
574 + r = -ENOMEM;
575 + goto release_object;
576 + }
572 577 r = amdgpu_ttm_tt_get_user_pages(bo, range);
573 578 if (r) {
574 579 amdgpu_hmm_range_free(range);
@@ -1236,7 +1241,7 @@ int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
1236 1241 bo_entry->alloc_flags = bo->flags & AMDGPU_GEM_CREATE_SETTABLE_MASK;
1237 1242 bo_entry->preferred_domains = bo->preferred_domains;
1238 1243 bo_entry->gem_handle = id;
1239 - bo_entry->alignment = bo->tbo.page_alignment;
1244 + bo_entry->alignment = (u64)bo->tbo.page_alignment << PAGE_SHIFT;
1240 1245
1241 1246 if (bo->tbo.base.import_attach)
1242 1247 bo_entry->flags |= AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT;
modified drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
@@ -2065,14 +2065,15 @@ 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 +
2068 2071 r = drm_sched_entity_init(&entity->base, prio, scheds, num_schedulers, NULL);
2069 2072 if (r)
2070 2073 return r;
2071 2074
2072 2075 mutex_init(&entity->lock);
2073 2076
2074 - if (ARRAY_SIZE(entity->gart_window_offs) < num_gart_windows)
2075 - return -EINVAL;
2076 2077 if (num_gart_windows == 0)
2077 2078 return 0;
2078 2079
@@ -2080,6 +2081,7 @@ static int amdgpu_ttm_buffer_entity_init(struct amdgpu_gtt_mgr *mgr,
2080 2081 r = amdgpu_gtt_mgr_alloc_entries(mgr, &entity->gart_node, num_pages,
2081 2082 DRM_MM_INSERT_BEST);
2082 2083 if (r) {
2084 + mutex_destroy(&entity->lock);
2083 2085 drm_sched_entity_destroy(&entity->base);
2084 2086 return r;
2085 2087 }
@@ -2097,6 +2099,7 @@ static void amdgpu_ttm_buffer_entity_fini(struct amdgpu_gtt_mgr *mgr,
2097 2099 struct amdgpu_ttm_buffer_entity *entity)
2098 2100 {
2099 2101 amdgpu_gtt_mgr_free_entries(mgr, &entity->gart_node);
2102 + mutex_destroy(&entity->lock);
2100 2103 drm_sched_entity_destroy(&entity->base);
2101 2104 }
2102 2105
modified drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
@@ -980,19 +980,21 @@ amdgpu_userq_bo_validate(struct amdgpu_device *adev, struct drm_exec *exec,
980 980 {
981 981 struct ttm_operation_ctx ctx = { false, false };
982 982 struct amdgpu_bo_va *bo_va;
983 - struct amdgpu_bo *bo;
983 + struct amdgpu_bo *bo, *tmp;
984 984 int ret;
985 985
986 986 spin_lock(&vm->individual_lock);
987 - while (!list_empty(&vm->always_valid.evicted)) {
988 - bo_va = list_first_entry(&vm->always_valid.evicted,
987 + while (!list_empty(&vm->individual.evicted)) {
988 + bo_va = list_first_entry(&vm->individual.evicted,
989 989 struct amdgpu_bo_va,
990 990 base.vm_status);
991 + bo = bo_va->base.bo;
992 + tmp = amdgpu_bo_ref(bo);
991 993 spin_unlock(&vm->individual_lock);
992 994
993 - bo = bo_va->base.bo;
994 995 ret = drm_exec_prepare_obj(exec, &bo->tbo.base,
995 996 TTM_NUM_MOVE_FENCES + 1);
997 + amdgpu_bo_unref(&tmp);
996 998 if (unlikely(ret))
997 999 return ret;
998 1000
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) & UVD_FW_STATUS__PASS_MASK) {
291 + if (RREG32(mmUVD_FW_START)) {
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,6 +89,7 @@ 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
92 93
93 94 static struct cmn2asic_msg_mapping smu_v14_0_2_message_map[SMU_MSG_MAX_COUNT] = {
94 95 MSG_MAP(TestMessage, PPSMC_MSG_TestMessage, 1),
@@ -1020,6 +1021,10 @@ static void smu_v14_0_2_get_od_setting_limits(struct smu_context *smu,
1020 1021 od_min_setting = overdrive_lowerlimits->FanZeroRpmEnable;
1021 1022 od_max_setting = overdrive_upperlimits->FanZeroRpmEnable;
1022 1023 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;
1023 1028 default:
1024 1029 od_min_setting = od_max_setting = INT_MAX;
1025 1030 break;
@@ -1244,6 +1249,24 @@ static int smu_v14_0_2_emit_clk_levels(struct smu_context *smu,
1244 1249 min_value, max_value);
1245 1250 break;
1246 1251
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 +
1247 1270 case SMU_OD_RANGE:
1248 1271 if (!smu_v14_0_2_is_od_feature_supported(smu, PP_OD_FEATURE_GFXCLK_BIT) &&
1249 1272 !smu_v14_0_2_is_od_feature_supported(smu, PP_OD_FEATURE_UCLK_BIT) &&
@@ -2311,7 +2334,9 @@ static void smu_v14_0_2_set_supported_od_feature_mask(struct smu_context *smu)
2311 2334 OD_OPS_SUPPORT_FAN_MINIMUM_PWM_RETRIEVE |
2312 2335 OD_OPS_SUPPORT_FAN_MINIMUM_PWM_SET |
2313 2336 OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_RETRIEVE |
2314 - OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_SET;
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;
2315 2340 }
2316 2341
2317 2342 static int smu_v14_0_2_get_overdrive_table(struct smu_context *smu,
@@ -2392,6 +2417,8 @@ static int smu_v14_0_2_set_default_od_settings(struct smu_context *smu)
2392 2417 user_od_table_bak.OverDriveTable.FanMinimumPwm;
2393 2418 user_od_table->OverDriveTable.FanZeroRpmEnable =
2394 2419 user_od_table_bak.OverDriveTable.FanZeroRpmEnable;
2420 + user_od_table->OverDriveTable.FanZeroRpmStopTemp =
2421 + user_od_table_bak.OverDriveTable.FanZeroRpmStopTemp;
2395 2422 }
2396 2423
2397 2424 smu_v14_0_2_set_supported_od_feature_mask(smu);
@@ -2446,6 +2473,11 @@ static int smu_v14_0_2_od_restore_table_single(struct smu_context *smu, long inp
2446 2473 boot_overdrive_table->OverDriveTable.FanZeroRpmEnable;
2447 2474 od_table->OverDriveTable.FeatureCtrlMask |= BIT(PP_OD_FEATURE_ZERO_FAN_BIT);
2448 2475 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;
2449 2481 case PP_OD_EDIT_ACOUSTIC_LIMIT:
2450 2482 od_table->OverDriveTable.AcousticLimitRpmThreshold =
2451 2483 boot_overdrive_table->OverDriveTable.AcousticLimitRpmThreshold;
@@ -2752,6 +2784,27 @@ static int smu_v14_0_2_od_edit_dpm_table(struct smu_context *smu,
2752 2784 od_table->OverDriveTable.FeatureCtrlMask |= BIT(PP_OD_FEATURE_ZERO_FAN_BIT);
2753 2785 break;
2754 2786
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 +
2755 2808 case PP_OD_RESTORE_DEFAULT_TABLE:
2756 2809 if (size == 1) {
2757 2810 ret = smu_v14_0_2_od_restore_table_single(smu, input[0]);
modified kernel/trace/Makefile
@@ -146,12 +146,11 @@ KASAN_SANITIZE_undefsyms_base.o := y
146 146
147 147 UNDEFINED_ALLOWLIST = __asan __gcov __kasan __kcsan __hwasan __sancov __sanitizer __tsan __ubsan __msan \
148 148 __aeabi_unwind_cpp __s390_indirect_jump __x86_indirect_thunk simple_ring_buffer \
149 - current_thread_info_reg \
150 - $(shell $(NM) -u $(obj)/undefsyms_base.o 2>/dev/null | awk '$$2 {print $$2}')
149 + $(shell $(NM) -u $(obj)/undefsyms_base.o 2>/dev/null | awk '{print $$2}')
151 150
152 151 quiet_cmd_check_undefined = NM $<
153 152 cmd_check_undefined = \
154 - undefsyms=$$($(NM) -u $< | awk '$$2 {print $$2}' | grep -v $(addprefix -e , $(UNDEFINED_ALLOWLIST)) || true); \
153 + undefsyms=$$($(NM) -u $< | grep -v $(addprefix -e , $(UNDEFINED_ALLOWLIST)) || true); \
155 154 if [ -n "$$undefsyms" ]; then \
156 155 echo "Unexpected symbols in $<:" >&2; \
157 156 echo "$$undefsyms" >&2; \
modified kernel/trace/ring_buffer.c
@@ -2380,9 +2380,6 @@ int ring_buffer_meta_seq_init(struct file *file, struct trace_buffer *buffer, in
2380 2380 struct seq_file *m;
2381 2381 int ret;
2382 2382
2383 - if (!cpumask_test_cpu(cpu, buffer->cpumask))
2384 - return -ENODEV;
2385 -
2386 2383 ret = seq_open(file, &rb_meta_seq_ops);
2387 2384 if (ret)
2388 2385 return ret;
@@ -3630,8 +3627,10 @@ static void rb_inc_iter(struct ring_buffer_iter *iter)
3630 3627 /* Return the index into the sub-buffers for a given sub-buffer */
3631 3628 static int rb_meta_subbuf_idx(struct ring_buffer_cpu_meta *meta, void *subbuf)
3632 3629 {
3633 - void *subbuf_array = rb_subbufs_from_meta(meta);
3630 + void *subbuf_array;
3634 3631
3632 + subbuf_array = (void *)meta + sizeof(int) * meta->nr_subbufs;
3633 + subbuf_array = (void *)ALIGN((unsigned long)subbuf_array, meta->subbuf_size);
3635 3634 return (subbuf - subbuf_array) / meta->subbuf_size;
3636 3635 }
3637 3636
modified kernel/trace/trace.c
@@ -5881,7 +5881,7 @@ static int tracing_buffer_meta_open(struct inode *inode, struct file *filp)
5881 5881
5882 5882 ret = ring_buffer_meta_seq_init(filp, tr->array_buffer.buffer, cpu);
5883 5883 if (ret < 0)
5884 - trace_array_put(tr);
5884 + __trace_array_put(tr);
5885 5885 return ret;
5886 5886 }
5887 5887
modified kernel/trace/trace_events_hist.c
@@ -289,7 +289,7 @@ static u64 hist_field_log2(struct hist_field *hist_field,
289 289
290 290 u64 val = hist_fn_call(operand, elt, buffer, rbe, event);
291 291
292 - return order_base_2(val);
292 + return (u64) ilog2(roundup_pow_of_two(val));
293 293 }
294 294
295 295 static u64 hist_field_bucket(struct hist_field *hist_field,
@@ -3090,7 +3090,7 @@ create_field_var_hist(struct hist_trigger_data *target_hist_data,
3090 3090 key_field = hist_data->fields[i];
3091 3091 if (!first)
3092 3092 seq_buf_putc(&s, ',');
3093 - expr_field_str(key_field, &s);
3093 + seq_buf_puts(&s, key_field->field->name);
3094 3094 first = false;
3095 3095 }
3096 3096
modified kernel/trace/undefsyms_base.c
@@ -9,7 +9,6 @@
9 9
10 10 #include <linux/atomic.h>
11 11 #include <linux/string.h>
12 - #include <asm/local.h>
13 12 #include <asm/page.h>
14 13
15 14 void undefsyms_base(void *p, int n);
@@ -19,13 +18,11 @@ static char page[PAGE_SIZE] __aligned(PAGE_SIZE);
19 18 void undefsyms_base(void *p, int n)
20 19 {
21 20 char buffer[256] = { 0 };
22 - u32 u = 0;
23 21
22 + u32 u = 0;
24 23 memset((char * volatile)page, 8, PAGE_SIZE);
25 24 memset((char * volatile)buffer, 8, sizeof(buffer));
26 25 memcpy((void * volatile)p, buffer, sizeof(buffer));
27 26 cmpxchg((u32 * volatile)&u, 0, 8);
28 - cmpxchg((unsigned long *)p, 0, 8);
29 - local_set((local_t *)p, 8);
30 27 WARN_ON(n == 0xdeadbeef);
31 28 }