Skip to contentSkip to conversation

amdkfd: don't give a GPU access to SVM ranges when no VM

PATCH In review Zhu Lingshan 3 participants 3 messages Nov 23

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

PATCH 1/1 amdkfd: don't give a GPU access to SVM ranges when no VM

If a kfd process does not perform ACQUIRE VM for its kfd_process_device, the VM of the pdd is NULL.

This commit fails svm_range_check_attr() with -EINVAL, before it changes anything when VM is null of a pdd.

When XNACK is enabled, this commit does not give GPUs that have not acquired a VM default access to new svm ranges.

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +17 -1
index 7401b7fdcb57..6e42b358ad59 100644
@@ -339,6 +339,7 @@ svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start,
 	uint64_t size = last - start + 1;
 	struct svm_range *prange;
 	struct kfd_process *p;
+	int i;
 
 	prange = kzalloc_obj(*prange);
 	if (!prange)
@@ -367,9 +368,17 @@ svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start,
 	mutex_init(&prange->migrate_mutex);
 	mutex_init(&prange->lock);
 
-	if (p->xnack_enabled)
+	if (p->xnack_enabled) {
 		bitmap_copy(prange->bitmap_access, svms->bitmap_supported,
 			    MAX_GPU_INSTANCE);
+		for_each_set_bit(i, svms->bitmap_supported, p->n_pdds) {
+			if (!p->pdds[i]->drm_priv) {
+				pr_debug("GPU %d has no VM acquired, skip mapping SVM range [0x%llx 0x%llx] to it\n",
+					 p->pdds[i]->user_gpu_id, start, last);
+				clear_bit(i, prange->bitmap_access);
+			}
+		}
+	}
 
 	svm_range_set_default_attributes(svms, &prange->preferred_loc,
 					 &prange->prefetch_loc,
@@ -722,6 +731,13 @@ svm_range_check_attr(struct kfd_process *p,
 			break;
 		case KFD_IOCTL_SVM_ATTR_ACCESS:
 		case KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE:
+			gpuidx = kfd_process_gpuidx_from_gpuid(p, val);
+			if (gpuidx >= 0 && !p->pdds[gpuidx]->drm_priv) {
+				pr_debug("GPU %d has no VM acquired, so SVM does not work on it, please acquire VM for it first\n",
+					 val);
+				return -EINVAL;
+			}
+			break;
 		case KFD_IOCTL_SVM_ATTR_NO_ACCESS:
 			gpuidx = kfd_process_gpuidx_from_gpuid(p, val);
 			break;
Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] drm/amdkfd: missing drm_priv check for location attrs in svm_range_check_attr() - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] drm/amdkfd: missing drm_priv check for location attrs in svm_range_check_attr()
- [Medium] drm/amdkfd: svm_range_get_attr() reports default access for VM-less GPUs
--

commit c034a8aa5ef1d29d58a7b5188aa7d91ba73e78a9
Author: Zhu Lingshan <[email protected]>

amdkfd: don't give a GPU access to SVM ranges when no VM

This commit prevents GPUs whose kfd_process_device has not acquired a VM from receiving default access to new SVM ranges when XNACK is enabled, and rejects setting SVM access attributes for such GPUs in svm_range_check_attr().

Quoted 18 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -1
index a571fda5c713a..042ac8081f082 100644
@@ -339,6 +339,7 @@ svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start,
 	uint64_t size = last - start + 1;
 	struct svm_range *prange;
 	struct kfd_process *p;
+	int i;
 
 	prange = kzalloc_obj(*prange);
 	if (!prange)
@@ -367,9 +368,17 @@ svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start,
 	mutex_init(&prange->migrate_mutex);
 	mutex_init(&prange->lock);
 
-	if (p->xnack_enabled)
+	if (p->xnack_enabled) {

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start, · line 381

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +8 -0
 		bitmap_copy(prange->bitmap_access, svms->bitmap_supported,
 			    MAX_GPU_INSTANCE);
+		for_each_set_bit(i, svms->bitmap_supported, p->n_pdds) {
+			if (!p->pdds[i]->drm_priv) {
+				pr_debug("GPU %d has no VM acquired, skip mapping SVM range [0x%llx 0x%llx] to it\n",
+					 p->pdds[i]->user_gpu_id, start, last);
+				clear_bit(i, prange->bitmap_access);
+			}
+		}
+	}

[Severity: Medium]
Should svm_range_get_attr() also clear bits for GPUs with !p->pdds[i]->drm_priv when reporting default attributes for an unregistered range?

drivers/gpu/drm/amd/amdkfd/kfd_svm.c:svm_range_get_attr() {
	...
	node = interval_tree_iter_first(&svms->objects, start, last);
	if (!node) {
		pr_debug("range attrs not found return default values\n");
		svm_range_set_default_attributes(svms, &location, &prefetch_loc,
						 &granularity, &flags_and);
		flags_or = flags_and;
		if (p->xnack_enabled)
			bitmap_copy(bitmap_access, svms->bitmap_supported,
				    MAX_GPU_INSTANCE);
	...
}

When p->xnack_enabled is true and an address range is not yet in svms->objects, KFD_IOCTL_SVM_OP_GET_ATTR still copies svms->bitmap_supported into bitmap_access without excluding GPUs where drm_priv is NULL. It then reports KFD_IOCTL_SVM_ATTR_ACCESS for those GPUs, contradicting the default bitmap_access initialized by svm_range_new() and causing a subsequent KFD_IOCTL_SVM_OP_SET_ATTR with the queried attributes to fail with -EINVAL in svm_range_check_attr().

Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
@@ -722,6 +731,13 @@ svm_range_check_attr(struct kfd_process *p,
 			break;
 		case KFD_IOCTL_SVM_ATTR_ACCESS:
 		case KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE:

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_check_attr(struct kfd_process *p, · line 743

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7 -0
+			gpuidx = kfd_process_gpuidx_from_gpuid(p, val);
+			if (gpuidx >= 0 && !p->pdds[gpuidx]->drm_priv) {
+				pr_debug("GPU %d has no VM acquired, so SVM does not work on it, please acquire VM for it first\n",
+					 val);
+				return -EINVAL;
+			}
+			break;
 		case KFD_IOCTL_SVM_ATTR_NO_ACCESS:
 			gpuidx = kfd_process_gpuidx_from_gpuid(p, val);
 			break;

[Severity: High]
Does svm_range_check_attr() also need to reject GPUs with !p->pdds[gpuidx]->drm_priv for KFD_IOCTL_SVM_ATTR_PREFERRED_LOC and KFD_IOCTL_SVM_ATTR_PREFETCH_LOC?

Because the drm_priv check is only performed for ACCESS and ACCESS_IN_PLACE, a process that has acquired a VM on one GPU can pass another GPU whose pdd->drm_priv is NULL as PREFETCH_LOC or PREFERRED_LOC. svm_range_set_attr() -> svm_range_trigger_migration() -> svm_migrate_to_vram() will then allocate a VRAM BO and migrate pages via SDMA onto an unacquired GPU that has not gone through kfd_bind_process_to_device().

When p->xnack_enabled is true and the range is later validated for another GPU, svm_range_validate_and_map() only adds prange->actual_loc to ctx->bitmap if test_bit(gpuidx, prange->bitmap_access) is set. Since svm_range_new() cleared the unacquired GPU's bit in prange->bitmap_access, actual_loc is omitted from ctx->bitmap and owner is not cleared to NULL, causing amdgpu_hmm_range_get_pages() -> hmm_range_fault() to fault on the unacquired GPU's device-private pages while svm_migrate_to_ram() skips migration because p->svms.faulting_task == current.

Checking !p->pdds[gpuidx]->drm_priv after the switch alongside the existing gpuidx validation would cover all GPU-targeted attributes.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Zhu Lingshan
Quoted 32 lines · Zhu Lingshan

If a kfd process does not perform ACQUIRE VM for its kfd_process_device, the VM of the pdd is NULL.

This commit fails svm_range_check_attr() with -EINVAL, before it changes anything when VM is null of a pdd.

When XNACK is enabled, this commit does not give GPUs that have not acquired a VM default access to new svm ranges.

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 18 +++++++++++++++++-
  1 file changed, 17 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -1
index 7401b7fdcb57..6e42b358ad59 100644
@@ -339,6 +339,7 @@ svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start,
  	uint64_t size = last - start + 1;
  	struct svm_range *prange;
  	struct kfd_process *p;
+	int i;
 
  	prange = kzalloc_obj(*prange);
  	if (!prange)
@@ -367,9 +368,17 @@ svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start,
  	mutex_init(&prange->migrate_mutex);
  	mutex_init(&prange->lock);
 
-	if (p->xnack_enabled)
+	if (p->xnack_enabled) {

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start, · line 381

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +8 -0
  		bitmap_copy(prange->bitmap_access, svms->bitmap_supported,
  			    MAX_GPU_INSTANCE);
+		for_each_set_bit(i, svms->bitmap_supported, p->n_pdds) {
+			if (!p->pdds[i]->drm_priv) {
+				pr_debug("GPU %d has no VM acquired, skip mapping SVM range [0x%llx 0x%llx] to it\n",
+					 p->pdds[i]->user_gpu_id, start, last);
+				clear_bit(i, prange->bitmap_access);
+			}
+		}
+	}

I suspect this issue is caused by a low-level test application that doesn’t initialize KFD through the Thunk.

Could we update svm_range_list_init() to check for drm_priv before marking SVM as supported? With that check in place, this change and the other related change below may no longer be necessary.

@@ -3468,7 +3468,8 @@ int svm_range_list_init(struct kfd_process *p) spin_lock_init(&svms->deferred_list_lock);

        for (i = 0; i < p->n_pdds; i++)
-               if (KFD_IS_SVM_API_SUPPORTED(p->pdds[i]->dev->adev)) +               if (KFD_IS_SVM_API_SUPPORTED(p->pdds[i]->dev->adev) && +                   p->pdds[i]->drm_priv) bitmap_set(svms->bitmap_supported, i, 1);

         /* Value of default granularity cannot exceed 0x1B, the

Regards,
Philip

Quoted 17 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7 -0
  	svm_range_set_default_attributes(svms, &prange->preferred_loc,
  					 &prange->prefetch_loc,
@@ -722,6 +731,13 @@ svm_range_check_attr(struct kfd_process *p,
  			break;
  		case KFD_IOCTL_SVM_ATTR_ACCESS:
  		case KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE:
+			gpuidx = kfd_process_gpuidx_from_gpuid(p, val);
+			if (gpuidx >= 0 && !p->pdds[gpuidx]->drm_priv) {
+				pr_debug("GPU %d has no VM acquired, so SVM does not work on it, please acquire VM for it first\n",
+					 val);
+				return -EINVAL;
+			}
+			break;
  		case KFD_IOCTL_SVM_ATTR_NO_ACCESS:
  			gpuidx = kfd_process_gpuidx_from_gpuid(p, val);
  			break;