[PATCH 40/44] drm/amdkfd: Return pdd from kfd_process_device_from_gduid

Felix Kuehling Felix.Kuehling at amd.com
Mon Mar 22 10:58:56 UTC 2021


This saves callers from looking up the pdd with a linear search later.

Signed-off-by: Felix Kuehling <Felix.Kuehling at amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h    |  8 +++-
 drivers/gpu/drm/amd/amdkfd/kfd_process.c | 10 -----
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c     | 51 +++++++++++-------------
 3 files changed, 29 insertions(+), 40 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
index 2ccfdb218198..ca44547c46a0 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
@@ -865,11 +865,15 @@ struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm);
 int kfd_process_gpuid_from_gpuidx(struct kfd_process *p,
 					uint32_t gpu_idx, uint32_t *gpuid);
 int kfd_process_gpuidx_from_gpuid(struct kfd_process *p, uint32_t gpu_id);
-int kfd_process_device_from_gpuidx(struct kfd_process *p,
-					uint32_t gpu_idx, struct kfd_dev **gpu);
 int kfd_process_gpuid_from_kgd(struct kfd_process *p,
 			       struct amdgpu_device *adev, uint32_t *gpuid,
 			       uint32_t *gpuidx);
+
+static inline struct kfd_process_device *kfd_process_device_from_gpuidx(
+				struct kfd_process *p, uint32_t gpuidx) {
+	return gpuidx < p->n_pdds ? p->pdds[gpuidx] : NULL;
+}
+
 void kfd_unref_process(struct kfd_process *p);
 int kfd_process_evict_queues(struct kfd_process *p);
 int kfd_process_restore_queues(struct kfd_process *p);
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
index 48ea6f393353..cc988bf6057d 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
@@ -1674,16 +1674,6 @@ int kfd_process_gpuidx_from_gpuid(struct kfd_process *p, uint32_t gpu_id)
 	return -EINVAL;
 }
 
-int kfd_process_device_from_gpuidx(struct kfd_process *p,
-					uint32_t gpu_idx, struct kfd_dev **gpu)
-{
-	if (gpu_idx < p->n_pdds) {
-		*gpu = p->pdds[gpu_idx]->dev;
-		return 0;
-	}
-	return -EINVAL;
-}
-
 int
 kfd_process_gpuid_from_kgd(struct kfd_process *p, struct amdgpu_device *adev,
 			   uint32_t *gpuid, uint32_t *gpuidx)
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
index c48fe2f276b9..081d6bb75b09 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
@@ -175,12 +175,11 @@ void svm_range_dma_unmap(struct device *dev, dma_addr_t *dma_addr,
 
 void svm_range_free_dma_mappings(struct svm_range *prange)
 {
-	struct kfd_dev *kfd_dev;
+	struct kfd_process_device *pdd;
 	dma_addr_t *dma_addr;
 	struct device *dev;
 	struct kfd_process *p;
 	uint32_t gpuidx;
-	int r;
 
 	p = container_of(prange->svms, struct kfd_process, svms);
 
@@ -189,12 +188,12 @@ void svm_range_free_dma_mappings(struct svm_range *prange)
 		if (!dma_addr)
 			continue;
 
-		r = kfd_process_device_from_gpuidx(p, gpuidx, &kfd_dev);
-		if (r) {
+		pdd = kfd_process_device_from_gpuidx(p, gpuidx);
+		if (!pdd) {
 			pr_debug("failed to find device idx %d\n", gpuidx);
-			return;
+			continue;
 		}
-		dev = &kfd_dev->pdev->dev;
+		dev = &pdd->dev->pdev->dev;
 		svm_range_dma_unmap(dev, dma_addr, 0, prange->npages);
 		kvfree(dma_addr);
 		prange->dma_addr[gpuidx] = NULL;
@@ -549,10 +548,9 @@ void svm_range_vram_node_free(struct svm_range *prange)
 struct amdgpu_device *
 svm_range_get_adev_by_id(struct svm_range *prange, uint32_t gpu_id)
 {
+	struct kfd_process_device *pdd;
 	struct kfd_process *p;
-	struct kfd_dev *dev;
 	int32_t gpu_idx;
-	int r;
 
 	p = container_of(prange->svms, struct kfd_process, svms);
 
@@ -561,13 +559,13 @@ svm_range_get_adev_by_id(struct svm_range *prange, uint32_t gpu_id)
 		pr_debug("failed to get device by id 0x%x\n", gpu_id);
 		return NULL;
 	}
-	r = kfd_process_device_from_gpuidx(p, gpu_idx, &dev);
-	if (r < 0) {
+	pdd = kfd_process_device_from_gpuidx(p, gpu_idx);
+	if (!pdd) {
 		pr_debug("failed to get device by idx 0x%x\n", gpu_idx);
 		return NULL;
 	}
 
-	return (struct amdgpu_device *)dev->kgd;
+	return (struct amdgpu_device *)pdd->dev->kgd;
 }
 
 static int svm_range_validate_vram(struct svm_range *prange)
@@ -1120,7 +1118,6 @@ svm_range_unmap_from_gpus(struct svm_range *prange, unsigned long start,
 	struct dma_fence *fence = NULL;
 	struct amdgpu_device *adev;
 	struct kfd_process *p;
-	struct kfd_dev *dev;
 	uint32_t gpuidx;
 	int r = 0;
 
@@ -1130,17 +1127,12 @@ svm_range_unmap_from_gpus(struct svm_range *prange, unsigned long start,
 
 	for_each_set_bit(gpuidx, bitmap, MAX_GPU_INSTANCE) {
 		pr_debug("unmap from gpu idx 0x%x\n", gpuidx);
-		r = kfd_process_device_from_gpuidx(p, gpuidx, &dev);
-		if (r) {
+		pdd = kfd_process_device_from_gpuidx(p, gpuidx);
+		if (!pdd) {
 			pr_debug("failed to find device idx %d\n", gpuidx);
 			return -EINVAL;
 		}
-
-		pdd = kfd_get_process_device_data(dev, p);
-		if (!pdd)
-			return -EINVAL;
-
-		adev = (struct amdgpu_device *)dev->kgd;
+		adev = (struct amdgpu_device *)pdd->dev->kgd;
 
 		r = svm_range_unmap_from_gpu(adev, pdd->vm, start, last,
 					     &fence);
@@ -1260,7 +1252,6 @@ int svm_range_map_to_gpus(struct svm_range *prange, bool reserve_vm)
 	struct amdgpu_device *bo_adev;
 	struct amdgpu_device *adev;
 	struct kfd_process *p;
-	struct kfd_dev *dev;
 	struct dma_fence *fence = NULL;
 	uint32_t gpuidx;
 	int r = 0;
@@ -1296,16 +1287,16 @@ int svm_range_map_to_gpus(struct svm_range *prange, bool reserve_vm)
 
 	for_each_set_bit(gpuidx, bitmap, MAX_GPU_INSTANCE) {
 		pr_debug("mapping to gpu idx 0x%x\n", gpuidx);
-		r = kfd_process_device_from_gpuidx(p, gpuidx, &dev);
-		if (r) {
+		pdd = kfd_process_device_from_gpuidx(p, gpuidx);
+		if (!pdd) {
 			pr_debug("failed to find device idx %d\n", gpuidx);
 			return -EINVAL;
 		}
+		adev = (struct amdgpu_device *)pdd->dev->kgd;
 
-		pdd = kfd_bind_process_to_device(dev, p);
+		pdd = kfd_bind_process_to_device(pdd->dev, p);
 		if (IS_ERR(pdd))
 			return -EINVAL;
-		adev = (struct amdgpu_device *)dev->kgd;
 
 		if (bo_adev && adev != bo_adev &&
 		    !amdgpu_xgmi_same_hive(adev, bo_adev)) {
@@ -2334,9 +2325,9 @@ svm_range_best_prefetch_location(struct svm_range *prange)
 {
 	DECLARE_BITMAP(bitmap, MAX_GPU_INSTANCE);
 	uint32_t best_loc = prange->prefetch_loc;
+	struct kfd_process_device *pdd;
 	struct amdgpu_device *bo_adev;
 	struct amdgpu_device *adev;
-	struct kfd_dev *kfd_dev;
 	struct kfd_process *p;
 	uint32_t gpuidx;
 
@@ -2360,8 +2351,12 @@ svm_range_best_prefetch_location(struct svm_range *prange)
 		  MAX_GPU_INSTANCE);
 
 	for_each_set_bit(gpuidx, bitmap, MAX_GPU_INSTANCE) {
-		kfd_process_device_from_gpuidx(p, gpuidx, &kfd_dev);
-		adev = (struct amdgpu_device *)kfd_dev->kgd;
+		pdd = kfd_process_device_from_gpuidx(p, gpuidx);
+		if (!pdd) {
+			pr_debug("failed to get device by idx 0x%x\n", gpuidx);
+			continue;
+		}
+		adev = (struct amdgpu_device *)pdd->dev->kgd;
 
 		if (adev == bo_adev)
 			continue;
-- 
2.31.0



More information about the amd-gfx mailing list