drm/amdgpu: Add debug bit for userptr usage
Subsystems (3)
PATCH 1/2 drm/amdgpu: Add debug bit for userptr usage
In VM debug mode, it is desirable to notify the application to correct the freeing sequence by unmapping the memory before destroying the userptr in the old userptr path. Add a bitmask to decide whether to send gpu vm fault to the applition.
--- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 5 +++++ 2 files changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +1 -0
@@ -1230,6 +1230,7 @@ struct amdgpu_device {bool debug_enable_ras_aca;bool debug_exp_resets;bool debug_disable_gpu_ring_reset; +bool debug_vm_userptr;/* Protection for the following isolation structure */ struct mutex enforce_isolation_mutex;
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +5 -0
@@ -143,6 +143,7 @@ enum AMDGPU_DEBUG_MASK {AMDGPU_DEBUG_ENABLE_EXP_RESETS =BIT (5 ),AMDGPU_DEBUG_DISABLE_GPU_RING_RESET =BIT (6 ),AMDGPU_DEBUG_SMU_POOL =BIT (7 ), + AMDGPU_DEBUG_VM_USERPTR = BIT(8), };unsigned int amdgpu_vram_limit =UINT_MAX ;
@@ -2272,6 +2273,10 @@ static void amdgpu_init_debug_options(struct amdgpu_device *adev)pr_info ("debug: use vram for smu pool\n" ); adev->pm.smu_debug_mask |=SMU_DEBUG_POOL_USE_VRAM ; } +if (amdgpu_debug_mask &AMDGPU_DEBUG_VM_USERPTR ) { +pr_info ("debug: VM mode debug for userptr is enabled\n" ); + adev->debug_vm_userptr = true; + } }static unsigned long amdgpu_fix_asic_type (struct pci_dev *pdev,unsigned long flags)
Signature · 1 line
2.25.1
PATCH 2/2 amd/amdkfd: Trigger segfault for early userptr unmmapping
If applications unmap the memory before destroying the userptr, it needs trigger a segfault to notify user space to correct the free sequence in VM debug mode.
v2: Send GPU access fault to user space
v3: Report gpu address to user space, remove unnecessary params
--- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 15 +++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_events.c | 19 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 ++ 3 files changed, 36 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +15 -0
@@ -2496,6 +2496,7 @@ static int update_invalid_user_pages(struct amdkfd_process_info *process_info,struct ttm_operation_ctx ctx = { false, false };uint32_t invalid;int ret =0 ; +uint64_t userptr =0 ;mutex_lock (&process_info->notifier_lock);
@@ -2559,6 +2560,20 @@ static int update_invalid_user_pages(struct amdkfd_process_info *process_info,if (ret != -EFAULT )return ret; +/* If applications unmap memory before destroying the userptr +* from the KFD, trigger a segmentation fault in VM debug mode. +*/ +if (amdgpu_ttm_adev (bo->tbo.bdev)->debug_vm_userptr) { +amdgpu_ttm_tt_get_userptr (&bo->tbo, &userptr); +pr_err ("User space unmap memory before destroying a userptr that refers to it\n" ); +pr_err ("The unmap userptr cpu address is 0x%llx, gpu address is 0x%llx\n" , + userptr, mem->va); + +// Send GPU VM fault to user space +kfd_signal_vm_fault_event_with_userptr (kfd_lookup_process_by_pid (process_info->pid), + mem->va); + } + ret =0 ; }
drivers/gpu/drm/amd/amdkfd/kfd_events.c +19 -0
@@ -1177,6 +1177,25 @@ void kfd_signal_hw_exception_event(u32 pasid)kfd_unref_process (p); } +void kfd_signal_vm_fault_event_with_userptr (struct kfd_process *p,uint64_t gpu_va) +{ +struct kfd_process_device *pdd; +struct kfd_hsa_memory_exception_data exception_data; +int i; + +memset (&exception_data,0 ,sizeof (exception_data)); + exception_data.va = gpu_va; + exception_data.failure.NotPresent =1 ; + +// Send VM seg fault to all kfd process device +for (i =0 ; i < p->n_pdds; i++) { + pdd = p->pdds[i]; + exception_data.gpu_id = pdd->user_gpu_id; +kfd_evict_process_device (pdd); +kfd_signal_vm_fault_event (pdd,NULL , &exception_data); + } +} +void kfd_signal_vm_fault_event (struct kfd_process_device *pdd,struct kfd_vm_fault_info *info,struct kfd_hsa_memory_exception_data *data)
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2 -0
@@ -1507,6 +1507,8 @@ int kfd_event_create(struct file *devkfd, struct kfd_process *p,int kfd_get_num_events (struct kfd_process *p);int kfd_event_destroy (struct kfd_process *p,uint32_t event_id); +void kfd_signal_vm_fault_event_with_userptr (struct kfd_process *p,uint64_t gpu_va); +void kfd_signal_vm_fault_event (struct kfd_process_device *pdd,struct kfd_vm_fault_info *info,struct kfd_hsa_memory_exception_data *data);
Signature · 1 line
2.25.1
RE: [PATCH 2/2] amd/amdkfd: Trigger segfault for early userptr unmmapping
[Public]
Ping
Quoted 118 lines
-----Original Message-----
From: Xiao, Shane <[email protected]>
Sent: Friday, April 25, 2025 11:35 AM
To: [email protected]; Kuehling, Felix <[email protected]>; Koenig, Christian <[email protected]>; Yang, Philip <[email protected]>
Cc: Xiao, Shane <[email protected]>
Subject: [PATCH 2/2] amd/amdkfd: Trigger segfault for early userptr unmmappingIf applications unmap the memory before destroying the userptr, it needs trigger a segfault to notify user space to correct the free sequence in VM debug mode.
v2: Send GPU access fault to user space
v3: Report gpu address to user space, remove unnecessary paramsSigned-off-by Shane Xiao <[email protected]>--- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 15 +++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_events.c | 19 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 ++ 3 files changed, 36 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2
-0@@ -2496,6 +2496,7 @@ static int update_invalid_user_pages(structamdkfd_process_info *process_info, struct ttm_operation_ctx ctx = { false, false };uint32_t invalid;int ret =0 ; +uint64_t userptr =0 ;mutex_lock (&process_info->notifier_lock);@@ -2559,6 +2560,20 @@ static int update_invalid_user_pages(struct amdkfd_process_info *process_info, if (ret != -EFAULT)return ret; +/* If applications unmap memory before destroying the userptr
+ * from the KFD, trigger a segmentation fault in VM
debug mode.drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2
-0+*/ + if (amdgpu_ttm_adev(bo->tbo.bdev)-Quoted 1 line
debug_vm_userptr) {+ amdgpu_ttm_tt_get_userptr(&bo->tbo,
&userptr);
+ pr_err("User space unmap memory before
destroying a userptr that refers to it\n");
+ pr_err("The unmap userptr cpu address is
0x%llx, gpu address is 0x%llx\n",
+ userptr, mem-Quoted 1 line
va);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3
-0+ +// Send GPU VM fault to user space + kfd_signal_vm_fault_event_with_userptr(kfd_lookup_process_by_pid(pr
ocess_info->pid),drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3
-0+ mem->va); + } + ret =0 ; }drivers/gpu/drm/amd/amdkfd/kfd_events.c +19
-0@@ -1177,6 +1177,25 @@ void kfd_signal_hw_exception_event(u32 pasid)kfd_unref_process (p); } +void kfd_signal_vm_fault_event_with_userptr (struct kfd_process *p, +uint64_t gpu_va) { +struct kfd_process_device *pdd; +struct kfd_hsa_memory_exception_data exception_data; +int i; + +memset (&exception_data,0 ,sizeof (exception_data)); + exception_data.va = gpu_va; + exception_data.failure.NotPresent =1 ; + +// Send VM seg fault to all kfd process device +for (i =0 ; i < p->n_pdds; i++) { + pdd = p->pdds[i]; + exception_data.gpu_id = pdd->user_gpu_id; +kfd_evict_process_device (pdd); +kfd_signal_vm_fault_event (pdd,NULL , &exception_data); + } +} +void kfd_signal_vm_fault_event (struct kfd_process_device *pdd,struct kfd_vm_fault_info *info,struct kfd_hsa_memory_exception_data *data)drivers/gpu/drm/amd/amdkfd/kfd_priv.h +0
-0@@ -1507,6 +1507,8 @@ int kfd_event_create(struct file *devkfd, struct
kfd_process*p, intkfd_get_num_events(struct kfd_process *p); intkfd_event_destroy(struct kfd_process *p, uint32_t event_id);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +3
-0+void kfd_signal_vm_fault_event_with_userptr (struct kfd_process *p, +uint64_t gpu_va); +void kfd_signal_vm_fault_event (struct kfd_process_device *pdd,struct kfd_vm_fault_info *info, struct kfd_hsa_memory_exception_data*data);
--
2.25.1
Re: [PATCH 2/2] amd/amdkfd: Trigger segfault for early userptr unmmapping
In reply to Shane XiaoQuoted 27 lines · Shane Xiao
If applications unmap the memory before destroying the userptr, it needs trigger a segfault to notify user space to correct the free sequence in VM debug mode.
v2: Send GPU access fault to user space
v3: Report gpu address to user space, remove unnecessary paramsSigned-off-by Shane Xiao <[email protected]>--- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 15 +++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_events.c | 19 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 ++ 3 files changed, 36 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1
-0@@ -2496,6 +2496,7 @@ static int update_invalid_user_pages(struct amdkfd_process_info *process_info,struct ttm_operation_ctx ctx = { false, false };uint32_t invalid;int ret =0 ; +uint64_t userptr =0 ;mutex_lock (&process_info->notifier_lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct amdkfd_process_info *process_info, · line 2567
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +5
-0@@ -2559,6 +2560,20 @@ static int update_invalid_user_pages(struct amdkfd_process_info *process_info,if (ret != -EFAULT )return ret; +/* If applications unmap memory before destroying the userptr +* from the KFD, trigger a segmentation fault in VM debug mode. +*/ +if (amdgpu_ttm_adev (bo->tbo.bdev)->debug_vm_userptr) { +amdgpu_ttm_tt_get_userptr (&bo->tbo, &userptr);
Userptr is only used for printing the message. That's probably unnecessary. You should get that address from user mode as well when it handles the fault event and error or warning messages are enabled (HSAKMT_DEBUG_LEVEL=4). The kernel log doesn't need to be overly verbose. IMO it should fit on one line. It may be useful to include the PID of the offending process. E.g.
pr_err("Pid %d unmapped memory before destroying userptr at GPU addr %llx",
pid_nr(process_info->pid), mem->va);If you remove the useptr variable, the amdgpu_ttm_tt_get_userptr call and shorten pr_err messages to one line, the series is
Quoted 55 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +9
-0+pr_err ("User space unmap memory before destroying a userptr that refers to it\n" ); +pr_err ("The unmap userptr cpu address is 0x%llx, gpu address is 0x%llx\n" , + userptr, mem->va); + +// Send GPU VM fault to user space +kfd_signal_vm_fault_event_with_userptr (kfd_lookup_process_by_pid (process_info->pid), + mem->va); + } + ret =0 ; }drivers/gpu/drm/amd/amdkfd/kfd_events.c +19
-0@@ -1177,6 +1177,25 @@ void kfd_signal_hw_exception_event(u32 pasid)kfd_unref_process (p); } +void kfd_signal_vm_fault_event_with_userptr (struct kfd_process *p,uint64_t gpu_va) +{ +struct kfd_process_device *pdd; +struct kfd_hsa_memory_exception_data exception_data; +int i; + +memset (&exception_data,0 ,sizeof (exception_data)); + exception_data.va = gpu_va; + exception_data.failure.NotPresent =1 ; + +// Send VM seg fault to all kfd process device +for (i =0 ; i < p->n_pdds; i++) { + pdd = p->pdds[i]; + exception_data.gpu_id = pdd->user_gpu_id; +kfd_evict_process_device (pdd); +kfd_signal_vm_fault_event (pdd,NULL , &exception_data); + } +} +void kfd_signal_vm_fault_event (struct kfd_process_device *pdd,struct kfd_vm_fault_info *info,struct kfd_hsa_memory_exception_data *data)drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2
-0@@ -1507,6 +1507,8 @@ int kfd_event_create(struct file *devkfd, struct kfd_process *p,int kfd_get_num_events (struct kfd_process *p);int kfd_event_destroy (struct kfd_process *p,uint32_t event_id); +void kfd_signal_vm_fault_event_with_userptr (struct kfd_process *p,uint64_t gpu_va); +void kfd_signal_vm_fault_event (struct kfd_process_device *pdd,struct kfd_vm_fault_info *info,struct kfd_hsa_memory_exception_data *data);
RE: [PATCH 2/2] amd/amdkfd: Trigger segfault for early userptr unmmapping
[Public]
Quoted 61 lines
-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Wednesday, April 30, 2025 7:58 AM
To: Xiao, Shane <[email protected]>; [email protected]; Koenig, Christian <[email protected]>; Yang, Philip <[email protected]>
Subject: Re: [PATCH 2/2] amd/amdkfd: Trigger segfault for early userptr unmmappingQuoted 10 lines · Shane Xiao
If applications unmap the memory before destroying the userptr, it needs trigger a segfault to notify user space to correct the free sequence in VM debug mode.
v2: Send GPU access fault to user space
v3: Report gpu address to user space, remove unnecessary paramsSigned-off-by Shane Xiao <[email protected]>--- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 15+++++++++++++++
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c | 19 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 ++ 3 files changed, 36 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0
-0@@ -2496,6 +2496,7 @@ static int update_invalid_user_pages(struct
amdkfd_process_info*process_info,drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1
-0struct ttm_operation_ctx ctx = { false, false };uint32_t invalid;int ret =0 ; +uint64_t userptr =0 ;mutex_lock (&process_info->notifier_lock);@@ -2559,6 +2560,20 @@ static int update_invalid_user_pages(struct
amdkfd_process_info*process_info,drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct amdkfd_process_info *process_info,
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1
-0if (ret != -EFAULT )return ret; +/* If applications unmap memory before destroying the userptr
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct · line 2564
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1
-0+* from the KFD, trigger a segmentation fault in VM debug mode.
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct · line 2568
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3
-0+*/ +if (amdgpu_ttm_adev (bo->tbo.bdev)- debug_vm_userptr) { + amdgpu_ttm_tt_get_userptr(&bo->tbo,&userptr);
Userptr is only used for printing the message. That's probably unnecessary. You should get that address from user mode as well when it handles the fault event and error or warning messages are enabled (HSAKMT_DEBUG_LEVEL=4). The kernel log doesn't need to be overly verbose. IMO it should fit on one line. It may be useful to include the PID of the offending process. E.g.
Quoted 6 lines
pr_err("Pid %d unmapped memory before destroying userptr at GPU
addr %llx",pid_nr(process_info->pid), mem->va);If you remove the useptr variable, the
amdgpu_ttm_tt_get_userptrcall and shortenpr_errmessages to one line, the series is
Thanks. I will correct it accordingly.
Best Regards,
Shane
Quoted 69 lines
Reviewed-by Felix Kuehling <[email protected]>drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct · line 2569
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1
-0+ pr_err("User space unmap memory beforedestroying a userptr that refers to it\n");
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct · line 2570
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1
-0+ pr_err("The unmap userptr cpu address is0x%llx, gpu address is 0x%llx\n",
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct amdkfd_process_info *process_info, · line 2576
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +4
-0+ userptr, mem- va); + +// Send GPU VM fault to user space +kfd_signal_vm_fault_event_with_userptr(kfd_lookup_process_by_pid(pr
ocess_info->pid),Quoted 27 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3
-0+ mem->va); + } + ret =0 ;}drivers/gpu/drm/amd/amdkfd/kfd_events.c +12
-0@@ -1177,6 +1177,25 @@ void kfd_signal_hw_exception_event(u32 pasid)kfd_unref_process (p); } +void kfd_signal_vm_fault_event_with_userptr (struct kfd_process *p, +uint64_t gpu_va) { +struct kfd_process_device *pdd; +struct kfd_hsa_memory_exception_data exception_data; +int i; + +memset (&exception_data,0 ,sizeof (exception_data)); + exception_data.va = gpu_va; + exception_data.failure.NotPresent =1 ; + +// Send VM seg fault to all kfd process device +for (i =0 ; i < p->n_pdds; i++) {drivers/gpu/drm/amd/amdkfd/kfd_events.c void kfd_signal_hw_exception_event(u32 pasid) · line 1201
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +7
-0+ pdd = p->pdds[i]; + exception_data.gpu_id = pdd->user_gpu_id; +kfd_evict_process_device (pdd); +kfd_signal_vm_fault_event (pdd,NULL , &exception_data); + } +} +void kfd_signal_vm_fault_event (struct kfd_process_device *pdd,struct kfd_vm_fault_info *info,struct kfd_hsa_memory_exception_data *data)diff --git
Quoted 9 lines
a/drivers/gpu/drm/amd/amdkfd/kfd_priv.hb/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
indexf6aedf69c644..8703be8077b0100644drivers/gpu/drm/amd/amdkfd/kfd_priv.h +0
-0@@ -1507,6 +1507,8 @@ int kfd_event_create(struct file *devkfd, struct
kfd_process*p, intkfd_get_num_events(struct kfd_process *p); intkfd_event_destroy(struct kfd_process *p, uint32_t event_id);drivers/gpu/drm/amd/amdkfd/kfd_priv.h
Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +3
-0+void kfd_signal_vm_fault_event_with_userptr (struct kfd_process *p, +uint64_t gpu_va); +void kfd_signal_vm_fault_event (struct kfd_process_device *pdd,struct kfd_vm_fault_info *info, struct kfd_hsa_memory_exception_data*data);