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
--- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 14 +++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_events.c | 20 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 3 +++ 3 files changed, 37 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +14 -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,19 @@ 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 address is 0x%llx\n" , userptr); + +// Send GPU VM fault to user space +kfd_signal_vm_fault_event_with_userptr (kfd_lookup_process_by_pid (process_info->pid), +amdgpu_ttm_adev (bo->tbo.bdev)->kfd.dev, userptr); + } + ret =0 ; }
drivers/gpu/drm/amd/amdkfd/kfd_events.c +20 -0
@@ -1177,6 +1177,26 @@ void kfd_signal_hw_exception_event(u32 pasid)kfd_unref_process (p); } +void kfd_signal_vm_fault_event_with_userptr (struct kfd_process *p, +struct kfd_dev *dev ,uint64_t userptr) +{ +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 = userptr; + 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 +3 -0
@@ -1507,6 +1507,9 @@ 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, +struct kfd_dev *dev ,uint64_t userptr); +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
Felix Kuehling
maintainerRe: [PATCH 2/2] amd/amdkfd: Trigger segfault for early userptr unmmapping
Quoted 5 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
See some comments inline.
Quoted 21 lines
Signed-off-by Shane Xiao <[email protected]>--- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 14 +++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_events.c | 20 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 3 +++ 3 files changed, 37 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,19 @@ 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);
I think you meant &userptr. But this will get you the CPU address of the userptr, not the GPU address. For reporting a GPU fault it would make more sense to use the GPU address in mem->va.
Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +8
-0+pr_err ("User space unmap memory before destroying a userptr that refers to it\n" ); +pr_err ("The unmap userptr address is 0x%llx\n" , userptr); + +// Send GPU VM fault to user space +kfd_signal_vm_fault_event_with_userptr (kfd_lookup_process_by_pid (process_info->pid), +amdgpu_ttm_adev (bo->tbo.bdev)->kfd.dev, userptr); + } + ret =0 ; }drivers/gpu/drm/amd/amdkfd/kfd_events.c +0
-0
drivers/gpu/drm/amd/amdkfd/kfd_events.c void kfd_signal_hw_exception_event(u32 pasid) · line 1181
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +2
-0@@ -1177,6 +1177,26 @@ void kfd_signal_hw_exception_event(u32 pasid)kfd_unref_process (p); } +void kfd_signal_vm_fault_event_with_userptr (struct kfd_process *p, +struct kfd_dev *dev ,uint64_t userptr)
dev seems to be unused. You can remove that parameter.
Regards,
Felix
Quoted 35 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +18
-0+{ +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 = userptr; + 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 +3
-0@@ -1507,6 +1507,9 @@ 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, +struct kfd_dev *dev ,uint64_t userptr); +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 55 lines
-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Thursday, April 24, 2025 9:56 PM
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 5 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
See some comments inline.
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct
Quoted 14 lines
Signed-off-by Shane Xiao <[email protected]>--- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 14 +++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_events.c | 20 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 3 +++ 3 files changed, 37 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,19 @@ 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,
Quoted 5 lines
userptr);
I think you meant &userptr. But this will get you the CPU address of the userptr, not the GPU address. For reporting a GPU fault it would make more sense to use the GPU address in
mem->va.
Yes, will use gpu address to notify user space.
Quoted 27 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct · line 2569
Quoted 3 lines
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 address is0x%llx\n", userptr);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct amdkfd_process_info *process_info, · line 2575
Quoted 3 lines
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),Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3
-0+amdgpu_ttm_adev (bo- tbo.bdev)->kfd.dev, userptr); + } + ret =0 ;}drivers/gpu/drm/amd/amdkfd/kfd_events.c +0
-0@@ -1177,6 +1177,26 @@ void kfd_signal_hw_exception_event(u32 pasid)kfd_unref_process (p); }
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c void kfd_signal_hw_exception_event(u32 pasid) · line 1181
Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +2
-0+void kfd_signal_vm_fault_event_with_userptr (struct kfd_process *p, +struct kfd_dev *dev ,uint64_t userptr)dev seems to be unused. You can remove that parameter.
Sure, will update it accordingly.
Best Regards,
Shane
Quoted 43 lines
Regards,
FelixQuoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +11
-0+{ +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 = userptr; + 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 1202
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..34f47dc1cbbd100644drivers/gpu/drm/amd/amdkfd/kfd_priv.h +0
-0@@ -1507,6 +1507,9 @@ 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 int kfd_event_create(struct file *devkfd, struct kfd_process *p,
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, +struct kfd_dev *dev ,uint64_t userptr); +void kfd_signal_vm_fault_event (struct kfd_process_device *pdd,struct kfd_vm_fault_info *info, struct kfd_hsa_memory_exception_data*data);
Quoted 6 lines · Shane Xiao
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.
Signed-off-by Shane Xiao <[email protected]>
Regards,
Christian.
Quoted 40 lines
--- 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)