Skip to contentSkip to conversation

drm/amdgpu: reject CS on a VM that KFD has acquired

PATCH New Yongqiang Sun 2 participants 2 messages Nov 30

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: reject CS on a VM that KFD has acquired

KFD's userptr and SVM notifiers stop KFD queues only. On the eviction path they leave the GPU page tables valid, so a DRM_IOCTL_AMDGPU_CS job on the acquired VM can keep writing through a translation after the memory is gone. Jobs submitted before ACQUIRE_VM are already waited for in init_kfd_vm().

Refuse later submissions with -EPERM.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +10 -0
index 71e2b83faa12b..a0f4a0e779c38 100644
@@ -1147,6 +1147,16 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 	if (!amdgpu_vm_ready(vm))
 		return -EINVAL;
 
+	/*
+	 * KFD acquired this VM. Its notifiers stop KFD queues only, and for
+	 * an eviction they leave the page tables valid, so a kernel-queue job
+	 * submitted here can keep using translations after the memory is gone.
+	 * Jobs submitted before the acquire are already waited for in
+	 * init_kfd_vm().
+	 */
+	if (vm->is_compute_context)
+		return -EPERM;
+
 	r = amdgpu_vm_clear_freed(adev, vm, NULL);
 	if (r)
 		return r;
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/amdgpu: late vm->is_compute_context check in amdgpu_cs_vm_handling() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdgpu: late vm->is_compute_context check in amdgpu_cs_vm_handling()
--

commit 217554c6af3d7136df0f66880e6fb5b7119d400b
Author: Yongqiang Sun <[email protected]>

drm/amdgpu: reject CS on a VM that KFD has acquired

This commit rejects DRM_IOCTL_AMDGPU_CS command submissions with -EPERM in amdgpu_cs_vm_handling() when the VM has been acquired by KFD.

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -0
index 71e2b83faa12b..a0f4a0e779c38 100644
@@ -1147,6 +1147,16 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 	if (!amdgpu_vm_ready(vm))
 		return -EINVAL;
 
+	/*
+	 * KFD acquired this VM. Its notifiers stop KFD queues only, and for
+	 * an eviction they leave the page tables valid, so a kernel-queue job

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p) · line 1162

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +7 -0
+	 * submitted here can keep using translations after the memory is gone.
+	 * Jobs submitted before the acquire are already waited for in
+	 * init_kfd_vm().
+	 */
+	if (vm->is_compute_context)
+		return -EPERM;
+
 	r = amdgpu_vm_clear_freed(adev, vm, NULL);
 	if (r)
 		return r;

[Severity: High]
Does checking vm->is_compute_context in amdgpu_cs_vm_handling() happen too late, after amdgpu_cs_parser_bos() has already migrated BOs and mutated the KFD VM state without updating its page tables?

Before amdgpu_cs_ioctl() reaches amdgpu_cs_vm_handling(), amdgpu_cs_parser_bos() locks the VM and runs:

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c:amdgpu_cs_parser_bos() {
    ...
	r = amdgpu_vm_validate(p->adev, &fpriv->vm, NULL,
			       amdgpu_cs_bo_validate, p);
	if (r) {
		drm_err(adev_to_drm(p->adev), "amdgpu_vm_validate() failed.\n");
		goto out_free_user_pages;
	}

	drm_exec_for_each_locked_object(&p->exec, obj) {
		r = amdgpu_cs_bo_validate(p, gem_to_amdgpu_bo(obj));
		if (unlikely(r))
			goto out_free_user_pages;
	}
    ...
}

amdgpu_vm_validate() moves evicted page table BOs to needs_update and clears vm->evicting = false, and amdgpu_cs_bo_validate() validates every locked BO (including vm->root.bo and mapped BOs in p->bo_list) into bo->preferred_domains. When a BO or page table in a fallback domain such as GTT is migrated to VRAM and amdgpu_cs_vm_handling() then returns -EPERM before amdgpu_vm_bo_update(), amdgpu_vm_handle_moved() and amdgpu_vm_update_pdes() run, the old TTM resource is freed while the live KFD compute VM is left with unupdated PDEs/PTEs (and a stale vm->pd_phys_addr if vm->root.bo moved) and vm->evicting cleared.

Checking fpriv->vm.is_compute_context in amdgpu_cs_parser_bos() right after drm_exec_until_all_locked(&p->exec) locks the VM PD would reject the submission before any VM or BO state is modified.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1