Skip to contentSkip to conversation

amd/amdkfd: Trigger segfault for early userptr unmmapping

PATCH In review Shane Xiao 2 participants 3 messages Apr 23, 2025 – Apr 24, 2025

Subsystems (3)
lore
Share & tools
lore

Shane Xiao

author
#0 lore raw

PATCH 1/1 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.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +10 -0
index d2ec4130a316..259b38424b7f 100644
@@ -2559,6 +2559,16 @@ static int update_invalid_user_pages(struct amdkfd_process_info *process_info,
 			if (ret != -EFAULT)
 				return ret;
 
+			/* If applications unmaps 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) {
+				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_sig(SIGSEGV, get_pid_task(process_info->pid, PIDTYPE_PID), 0);
+			}
+
 			ret = 0;
 		}
 
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 14 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.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
index d2ec4130a316..259b38424b7f 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct amdkfd_process_info *process_info, · line 2565

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +4 -0
@@ -2559,6 +2559,16 @@ static int update_invalid_user_pages(struct amdkfd_process_info *process_info,
 			if (ret != -EFAULT)
 				return ret;
 
+			/* If applications unmaps 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) {

Using debug_vm works for now, but maybe we should have a separate debug flag for this.

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct amdkfd_process_info *process_info, · line 2569

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +4 -0
+				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_sig(SIGSEGV, get_pid_task(process_info->pid, PIDTYPE_PID), 0);

Drivers should *never* mess with send_sig() directly. We made the mistake to allow that with the KFD already.

We should rather send this as GPU access fault or something like that.

Regards,
Christian.

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -0
+			}
+
 			ret = 0;
 		}
 
#2 lore raw

[Public]

Quoted 32 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, April 23, 2025 8:40 PM
To: Xiao, Shane <[email protected]>; [email protected]; Kuehling, Felix <[email protected]>; Yang, Philip <[email protected]>
Subject: Re: [PATCH] amd/amdkfd: Trigger segfault for early userptr unmmapping

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct

Quoted 15 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.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
index d2ec4130a316..259b38424b7f 100644
@@ -2559,6 +2559,16 @@ 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 -0
                    if (ret != -EFAULT)
                            return ret;
 
+                   /* If applications unmaps memory before destroying
Quoted 8 lines

the userptr

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct · line 2563

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 amdkfd_process_info *process_info, · line 2565

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -0
+                    */
+                   if (amdgpu_ttm_adev(bo->tbo.bdev)->debug_vm) {

Using debug_vm works for now, but maybe we should have a separate debug flag for this.

I have added a new debug_vm_userptr bit in the new patch series.

Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct · line 2566

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -0
+                           amdgpu_ttm_tt_get_userptr(&bo->tbo,

userptr);

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct · line 2567

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -0
+                           pr_err("User space unmap memory before

destroying a userptr that refers to it\n");

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct · line 2568

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -0
+                           pr_err("The unmap userptr address is

0x%llx\n", userptr);

Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int update_invalid_user_pages(struct · line 2571

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -0
+                           send_sig(SIGSEGV, get_pid_task(process_info-
 pid, PIDTYPE_PID),
+0);

Drivers should *never* mess with send_sig() directly. We made the mistake to allow that with the KFD already.

We should rather send this as GPU access fault or something like that.

Sure, already done it in the new patch.

Best regards,
Shane

Quoted 9 lines

Regards,
Christian.

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -0
+                   }
+
                    ret = 0;
            }