Skip to contentSkip to conversation

drm/amdgpu: Detect if amdgpu in IOMMU isolation mode

PATCH Superseded Philip Yang 4 participants 6 messages Dec 7, 2021 – Dec 9, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 drm/amdgpu: Detect if amdgpu in IOMMU isolation mode

If host and amdgpu IOMMU is not enabled or IOMMU is pass through mode, dma_map_page return address is equal to page physical address, use this to set adev->iommu_isolation flag which will be used to optimize memory usage for multi GPU mappings.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 27 ++++++++++++++++++++++
 2 files changed, 29 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index c5cfe2926ca1..fbbe8c7b5d0c 100644
@@ -1097,6 +1097,8 @@ struct amdgpu_device {
 
 	struct amdgpu_reset_control     *reset_cntl;
 	uint32_t                        ip_versions[MAX_HWIP][HWIP_MAX_INSTANCE];
+
+	bool				iommu_isolation;
 };
 
 static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +27 -0
index 3c5afa45173c..6d0f3c477670 100644
@@ -3364,6 +3364,31 @@ static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
 	return ret;
 }
 
+/**
+ * amdgpu_device_check_iommu_isolation - check if IOMMU isolation is enabled
+ *
+ * @adev: amdgpu_device pointer
+ *
+ * device is in IOMMU isolation mode if dma_map_page return address is not equal
+ * to page physical address.
+ */
+static void amdgpu_device_check_iommu_isolation(struct amdgpu_device *adev)
+{
+	struct page *page;
+	dma_addr_t addr;
+
+	page = alloc_page(GFP_KERNEL);
+	if (!page)
+		return;
+	addr = dma_map_page(adev->dev, page, 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
+	if (dma_mapping_error(adev->dev, addr))
+		goto out_free_page;
+	adev->iommu_isolation = (addr != page_to_phys(page));
+	dma_unmap_page(adev->dev, addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
+out_free_page:
+	__free_page(page);
+}
+
 static const struct attribute *amdgpu_dev_attributes[] = {
 	&dev_attr_product_name.attr,
 	&dev_attr_product_number.attr,
@@ -3767,6 +3792,8 @@ int amdgpu_device_init(struct amdgpu_device *adev,
 		queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work,
 				   msecs_to_jiffies(AMDGPU_RESUME_MS));
 
+	amdgpu_device_check_iommu_isolation(adev);
+
 	return 0;
 
 release_ras_con:
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/2 drm/amdgpu: Reduce SG bo memory usage for mGPUs

For userptr bo, if adev is not in IOMMU isolation mode, multiple GPUs use same system memory page dma mapping address, they can share the original mem->bo in attachment to reduce dma address array memory usage.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +8 -4
index b8490789eef4..03807ed1f6e2 100644
@@ -708,10 +708,14 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
 		pr_debug("\t add VA 0x%llx - 0x%llx to vm %p\n", va,
 			 va + bo_size, vm);
 
-		if (adev == bo_adev || (mem->domain == AMDGPU_GEM_DOMAIN_VRAM &&
-					amdgpu_xgmi_same_hive(adev, bo_adev))) {
-			/* Mappings on the local GPU and VRAM mappings in the
-			 * local hive share the original BO
+		if (adev == bo_adev ||
+		    (amdgpu_ttm_tt_get_usermm(mem->bo->tbo.ttm) &&
+		     !adev->iommu_isolation) ||
+		    (mem->domain == AMDGPU_GEM_DOMAIN_VRAM &&
+		     amdgpu_xgmi_same_hive(adev, bo_adev))) {
+			/* Mappings on the local GPU, or VRAM mappings in the
+			 * local hive, or userptr mapping IOMMU is not isolation
+			 * mode share the original BO
 			 */
 			attachment[i]->type = KFD_MEM_ATT_SHARED;
 			bo[i] = mem->bo;
Signature · 1 line
2.17.1
maintainer
#2 lore raw

Re: [PATCH 2/2] drm/amdgpu: Reduce SG bo memory usage for mGPUs

Quoted 16 lines · Philip Yang

For userptr bo, if adev is not in IOMMU isolation mode, multiple GPUs use same system memory page dma mapping address, they can share the original mem->bo in attachment to reduce dma address array memory usage.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 12 ++++++++----
  1 file changed, 8 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
index b8490789eef4..03807ed1f6e2 100644
@@ -708,10 +708,14 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
  		pr_debug("\t add VA 0x%llx - 0x%llx to vm %p\n", va,
  			 va + bo_size, vm);

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem, · line 715

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +5 -4
 
-		if (adev == bo_adev || (mem->domain == AMDGPU_GEM_DOMAIN_VRAM &&
-					amdgpu_xgmi_same_hive(adev, bo_adev))) {
-			/* Mappings on the local GPU and VRAM mappings in the
-			 * local hive share the original BO
+		if (adev == bo_adev ||
+		    (amdgpu_ttm_tt_get_usermm(mem->bo->tbo.ttm) &&
+		     !adev->iommu_isolation) ||
+		    (mem->domain == AMDGPU_GEM_DOMAIN_VRAM &&
+		     amdgpu_xgmi_same_hive(adev, bo_adev))) {

For the sake of readability I'd ignore the 80-column rule here (which isn't a hard rule any more). It could apply in the same way to GTT. This would be clearer:

+ if (adev == bo_adev ||
+ (amdgpu_ttm_tt_get_usermm(mem->bo->tbo.ttm) && adev->ram_is_direct_mapped) ||
+ (mem->domain == AMDGPU_GEM_DOMAIN_VRAM && amdgpu_xgmi_same_hive(adev, bo_adev))) {

Regards,
  Felix

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
+			/* Mappings on the local GPU, or VRAM mappings in the
+			 * local hive, or userptr mapping IOMMU is not isolation
+			 * mode share the original BO
  			 */
  			attachment[i]->type = KFD_MEM_ATT_SHARED;
  			bo[i] = mem->bo;
maintainer
#3 lore raw
In reply to Philip Yang
Quoted 43 lines · Philip Yang

If host and amdgpu IOMMU is not enabled or IOMMU is pass through mode, dma_map_page return address is equal to page physical address, use this to set adev->iommu_isolation flag which will be used to optimize memory usage for multi GPU mappings.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  2 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 27 ++++++++++++++++++++++
  2 files changed, 29 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index c5cfe2926ca1..fbbe8c7b5d0c 100644
@@ -1097,6 +1097,8 @@ struct amdgpu_device {
 
  	struct amdgpu_reset_control     *reset_cntl;
  	uint32_t                        ip_versions[MAX_HWIP][HWIP_MAX_INSTANCE];
+
+	bool				iommu_isolation;
  };
 
  static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +11 -0
index 3c5afa45173c..6d0f3c477670 100644
@@ -3364,6 +3364,31 @@ static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
  	return ret;
  }
 
+/**
+ * amdgpu_device_check_iommu_isolation - check if IOMMU isolation is enabled
+ *
+ * @adev: amdgpu_device pointer
+ *
+ * device is in IOMMU isolation mode if dma_map_page return address is not equal
+ * to page physical address.
+ */
+static void amdgpu_device_check_iommu_isolation(struct amdgpu_device *adev)
+{
+	struct page *page;

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev) · line 3387

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +10 -0
+	dma_addr_t addr;
+
+	page = alloc_page(GFP_KERNEL);
+	if (!page)
+		return;
+	addr = dma_map_page(adev->dev, page, 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
+	if (dma_mapping_error(adev->dev, addr))
+		goto out_free_page;
+	adev->iommu_isolation = (addr != page_to_phys(page));
+	dma_unmap_page(adev->dev, addr, PAGE_SIZE, DMA_BIDIRECTIONAL);

This is a bit of a hack. Unfortunately it seems there isn't a much better way to do this. I guess you could copy the implementation of dma_map_direct in kernel/dma/mapping.c, but that's more brittle.

I think this hack only tells you whether system memory is direct-mapped. The answer may be different for peer VRAM (which isn't supported upstream yet, but it's coming). I think this can happen when the IOMMU is in pass-through mode by default but still used to DMA map physical addresses that are outside the dma mask of the GPU. So a more future proof way would be to store a direct-mapped flag for each GPU-GPU and GPU-System pair somehow. For the GPU->GPU direct mapping flag you'd need to try to DMA-map a page from one GPU's VRAM to the other device. Anyway, that can be done in a later change.

For now I'd just change the name of the flag from iommu_isolation to direct_map_ram or ram_is_direct_mapped or similar to be more specific about what it means.

Regards,
  Felix

Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -0
+out_free_page:
+	__free_page(page);
+}
+
  static const struct attribute *amdgpu_dev_attributes[] = {
  	&dev_attr_product_name.attr,
  	&dev_attr_product_number.attr,
@@ -3767,6 +3792,8 @@ int amdgpu_device_init(struct amdgpu_device *adev,
  		queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work,
  				   msecs_to_jiffies(AMDGPU_RESUME_MS));
 
+	amdgpu_device_check_iommu_isolation(adev);
+
  	return 0;
 
  release_ras_con:
#4 lore raw
Quoted 70 lines · Felix Kuehling
Quoted 43 lines · Philip Yang

If host and amdgpu IOMMU is not enabled or IOMMU is pass through mode, dma_map_page return address is equal to page physical address, use this to set adev->iommu_isolation flag which will be used to optimize memory usage for multi GPU mappings.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  2 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 27 ++++++++++++++++++++++
  2 files changed, 29 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index c5cfe2926ca1..fbbe8c7b5d0c 100644
@@ -1097,6 +1097,8 @@ struct amdgpu_device {
 
      struct amdgpu_reset_control     *reset_cntl;
      uint32_t                        ip_versions[MAX_HWIP][HWIP_MAX_INSTANCE];
+
+     bool                            iommu_isolation;
  };
 
  static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +11 -0
index 3c5afa45173c..6d0f3c477670 100644
@@ -3364,6 +3364,31 @@ static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
      return ret;
  }
 
+/**
+ * amdgpu_device_check_iommu_isolation - check if IOMMU isolation is enabled
+ *
+ * @adev: amdgpu_device pointer
+ *
+ * device is in IOMMU isolation mode if dma_map_page return address is not equal
+ * to page physical address.
+ */
+static void amdgpu_device_check_iommu_isolation(struct amdgpu_device *adev)
+{
+     struct page *page;

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev) · line 3387

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +10 -0
+     dma_addr_t addr;
+
+     page = alloc_page(GFP_KERNEL);
+     if (!page)
+             return;
+     addr = dma_map_page(adev->dev, page, 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
+     if (dma_mapping_error(adev->dev, addr))
+             goto out_free_page;
+     adev->iommu_isolation = (addr != page_to_phys(page));
+     dma_unmap_page(adev->dev, addr, PAGE_SIZE, DMA_BIDIRECTIONAL);

This is a bit of a hack. Unfortunately it seems there isn't a much better way to do this. I guess you could copy the implementation of dma_map_direct in kernel/dma/mapping.c, but that's more brittle.

I think this hack only tells you whether system memory is direct-mapped. The answer may be different for peer VRAM (which isn't supported upstream yet, but it's coming). I think this can happen when the IOMMU is in pass-through mode by default but still used to DMA map physical addresses that are outside the dma mask of the GPU. So a more future proof way would be to store a direct-mapped flag for each GPU-GPU and GPU-System pair somehow. For the GPU->GPU direct mapping flag you'd need to try to DMA-map a page from one GPU's VRAM to the other device. Anyway, that can be done in a later change.

Quoted 3 lines

For now I'd just change the name of the flag from iommu_isolation to direct_map_ram or ram_is_direct_mapped or similar to be more specific about what it means.

This thread might be useful:
https://lists.freedesktop.org/archives/dri-devel/2021-November/330557.html
https://lists.freedesktop.org/archives/dri-devel/2021-November/330583.html

Alex

Quoted 21 lines

Regards,
Felix

Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -0
+out_free_page:
+     __free_page(page);
+}
+
  static const struct attribute *amdgpu_dev_attributes[] = {
      &dev_attr_product_name.attr,
      &dev_attr_product_number.attr,
@@ -3767,6 +3792,8 @@ int amdgpu_device_init(struct amdgpu_device *adev,
              queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work,
                                 msecs_to_jiffies(AMDGPU_RESUME_MS));
 
+     amdgpu_device_check_iommu_isolation(adev);
+
      return 0;
 
  release_ras_con:
#5 lore raw

On 2021-12-07 5:16 p.m., Alex Deucher wrote:
On Tue, Dec 7, 2021 at 4:59 PM Felix Kuehling <[email protected]> wrote: On 2021-12-07 9:59 a.m., Philip Yang wrote: If host and amdgpu IOMMU is not enabled or IOMMU is pass through mode, dma_map_page return address is equal to page physical address, use this to set adev->iommu_isolation flag which will be used to optimize memory usage for multi GPU mappings. Signed-off-by: Philip Yang <[email protected]> --- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 2 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 27 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h b/drivers/gpu/drm/amd/amdgpu/amdgpu.h index c5cfe2926ca1..fbbe8c7b5d0c 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h @@ -1097,6 +1097,8 @@ struct amdgpu_device { struct amdgpu_reset_control *reset_cntl; uint32_t ip_versions[MAX_HWIP][HWIP_MAX_INSTANCE]; + + bool iommu_isolation; }; static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c index 3c5afa45173c..6d0f3c477670 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c @@ -3364,6 +3364,31 @@ static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev) return ret; } +/** + * amdgpu_device_check_iommu_isolation - check if IOMMU isolation is enabled + * + * @adev: amdgpu_device pointer + * + * device is in IOMMU isolation mode if dma_map_page return address is not equal + * to page physical address. + */ +static void amdgpu_device_check_iommu_isolation(struct amdgpu_device *adev) +{ + struct page *page; + dma_addr_t addr; + + page = alloc_page(GFP_KERNEL); + if (!page) + return; + addr = dma_map_page(adev->dev, page, 0, PAGE_SIZE, DMA_BIDIRECTIONAL); + if (dma_mapping_error(adev->dev, addr)) + goto out_free_page; + adev->iommu_isolation = (addr != page_to_phys(page)); + dma_unmap_page(adev->dev, addr, PAGE_SIZE, DMA_BIDIRECTIONAL); This is a bit of a hack. Unfortunately it seems there isn't a much better way to do this. I guess you could copy the implementation of dma_map_direct in kernel/dma/mapping.c, but that's more brittle. I think this hack only tells you whether system memory is direct-mapped. The answer may be different for peer VRAM (which isn't supported upstream yet, but it's coming). I think this can happen when the IOMMU is in pass-through mode by default but still used to DMA map physical addresses that are outside the dma mask of the GPU. So a more future proof way would be to store a direct-mapped flag for each GPU-GPU and GPU-System pair somehow. For the GPU->GPU direct mapping flag you'd need to try to DMA-map a page from one GPU's VRAM to the other device. Anyway, that can be done in a later change. For now I'd just change the name of the flag from iommu_isolation to direct_map_ram or ram_is_direct_mapped or similar to be more specific about what it means. This thread might be useful: https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Farchives%2Fdri-devel%2F2021-November%2F330557.html&amp;data=04%7C01%7CPhilip.Yang%40amd.com%7Cfe724d9cbcd743c6156e08d9b9cf464d%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637745122159896596%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=YW0Hxm8n5xONavU4skohVV3nbrDUEPn3ehX6f9hJe6M%3D&amp;reserved=0 https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Farchives%2Fdri-devel%2F2021-November%2F330583.html&amp;data=04%7C01%7CPhilip.Yang%40amd.com%7Cfe724d9cbcd743c6156e08d9b9cf464d%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637745122159896596%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=ZaJgJPDa3akycHAob1y5OWr5iHMhHgqGZSijMDCpLbQ%3D&amp;reserved=0 Thanks for the helpful info, after testing on different platforms, I will send out v2 patch.
Regards,
Philip

Alex Regards, Felix +out_free_page: + __free_page(page); +} + static const struct attribute *amdgpu_dev_attributes[] = { &dev_attr_product_name.attr, &dev_attr_product_number.attr, @@ -3767,6 +3792,8 @@ int amdgpu_device_init(struct amdgpu_device *adev, queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work, msecs_to_jiffies(AMDGPU_RESUME_MS)); + amdgpu_device_check_iommu_isolation(adev); + return 0; release_ras_con: