Skip to contentSkip to conversation

drm/amd/amdgpu: solve the issue of allocate continuous pages under xen dom0

PATCH In review Shane Xiao 3 participants 6 messages Sep 22, 2022

Subsystems (2)
lore
Share & tools
lore

Shane Xiao

author
#0 lore raw

PATCH 1/1 drm/amd/amdgpu: solve the issue of allocate continuous pages under xen dom0

[Why]
sg_alloc_table_from_pages alloc too large continuous PFN pages under xen dom0. However, xen should check continuous MFN pages in range_straddles_page_boundary. When range_straddles_page_boundary return false, some cases fall back into swiotlb process and the continuous allocable page is not enough.

[How]
In fact, xen swiotlb set max_segment default value as UINT_MAX and xen_swiotlb_init_early already change the value to PAGE_SIZE under xen dom0. However amdgpu driver doesn't use the value, which may cause issue such as swiotlb buffer full. Add amd_sg_segment_size according to iommu setting, the details are as follows:
iommu setting | amd_sg_segment_size
-------------------------------------------------------------------------------

	iommu=on		|	UINT_MAX
    iommu=off && swiotlb on	|	IO_TLB_DEFAULT_SIZE(64M)
	xen_swiotlb on		|	PAGE_SIZE(4K)

-------------------------------------------------------------------------------

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +20 -2
index 134575a3893c..d081fcd22d6b 100644
@@ -80,6 +80,23 @@ static int amdgpu_ttm_init_on_chip(struct amdgpu_device *adev,
 				  false, size_in_page);
 }
 
+static inline unsigned int amdgpu_sg_segment_size(void)
+{
+	unsigned int size = swiotlb_max_segment();
+
+	/* size=0 when amd iommu enabled */
+	if (size == 0)
+		size = UINT_MAX;
+
+	size = rounddown(size, PAGE_SIZE);
+	/* swiotlb_max_segment_size can return 1 byte when it means one page. */
+	if (size < PAGE_SIZE)
+		size = PAGE_SIZE;
+
+	return size;
+}
+
+
 /**
  * amdgpu_evict_flags - Compute placement flags
  *
@@ -760,9 +777,10 @@ static int amdgpu_ttm_tt_pin_userptr(struct ttm_device *bdev,
 	int r;
 
 	/* Allocate an SG array and squash pages into it */
-	r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
-				      (u64)ttm->num_pages << PAGE_SHIFT,
+	r = sg_alloc_table_from_pages_segment(ttm->sg, ttm->pages, ttm->num_pages, 0,
+				      (u64)ttm->num_pages << PAGE_SHIFT, amdgpu_sg_segment_size(),
 				      GFP_KERNEL);
+
 	if (r)
 		goto release_sg;
 
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw

[AMD Official Use Only - General]

--
Best Regards
Aaron Liu

Quoted 86 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Shane Xiao
Sent: Thursday, September 22, 2022 3:12 PM
To: [email protected]
Cc: Xiao, Shane <[email protected]>
Subject: [PATCH] drm/amd/amdgpu: solve the issue of allocate continuous pages under xen dom0

[Why]
sg_alloc_table_from_pages alloc too large continuous PFN pages under xen dom0.
However, xen should check continuous MFN pages in range_straddles_page_boundary.
When range_straddles_page_boundary return false, some cases fall back into swiotlb process and the continuous allocable page is not enough.

[How]
In fact, xen swiotlb set max_segment default value as UINT_MAX and xen_swiotlb_init_early already change the value to PAGE_SIZE under xen dom0.
However amdgpu driver doesn't use the value, which may cause issue such as swiotlb buffer full. Add amd_sg_segment_size according to iommu setting, the details are as follows:
iommu setting | amd_sg_segment_size
-------------------------------------------------------------------------------

	iommu=on		|	UINT_MAX
    iommu=off && swiotlb on	|	IO_TLB_DEFAULT_SIZE(64M)
	xen_swiotlb on		|	PAGE_SIZE(4K)

-------------------------------------------------------------------------------

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 22
++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +9 -0
index 134575a3893c..d081fcd22d6b 100644
@@ -80,6 +80,23 @@ static int amdgpu_ttm_init_on_chip(struct
 amdgpu_device *adev,
 				  false, size_in_page);
 }
 
+static inline unsigned int amdgpu_sg_segment_size(void) {
+	unsigned int size = swiotlb_max_segment();
+
+	/* size=0 when amd iommu enabled */
+	if (size == 0)
+		size = UINT_MAX;
+
+	size = rounddown(size, PAGE_SIZE);
+	/* swiotlb_max_segment_size can return 1 byte when it means one

page. */

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -3
+	if (size < PAGE_SIZE)
+		size = PAGE_SIZE;
+
+	return size;
+}
+
+
 /**
  * amdgpu_evict_flags - Compute placement flags
  *
@@ -760,9 +777,10 @@ static int amdgpu_ttm_tt_pin_userptr(struct
 ttm_device *bdev,
 	int r;
 
 	/* Allocate an SG array and squash pages into it */
-	r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages,
 0,
-				      (u64)ttm->num_pages << PAGE_SHIFT,
+	r = sg_alloc_table_from_pages_segment(ttm->sg, ttm->pages, ttm-
 >num_pages, 0,
+				      (u64)ttm->num_pages << PAGE_SHIFT,
 amdgpu_sg_segment_size(),
 				      GFP_KERNEL);
+
 	if (r)
 		goto release_sg;
 
--

2.25.1

maintainer
#2 lore raw
In reply to Shane Xiao

Am 22.09.22 um 09:11 schrieb Shane Xiao:

Quoted 2 lines

[Why]
sg_alloc_table_from_pages alloc too large continuous PFN pages under xen dom0.

Well that sentence doesn't make much sense. Why should the resulting sg table be to large? For what to large?

Regards,
Christian.

Quoted 63 lines

However, xen should check continuous MFN pages in range_straddles_page_boundary. When range_straddles_page_boundary return false, some cases fall back into swiotlb process and the continuous allocable page is not enough.

[How]
In fact, xen swiotlb set max_segment default value as UINT_MAX and xen_swiotlb_init_early already change the value to PAGE_SIZE under xen dom0. However amdgpu driver doesn't use the value, which may cause issue such as swiotlb buffer full. Add amd_sg_segment_size according to iommu setting, the details are as follows:
iommu setting | amd_sg_segment_size
-------------------------------------------------------------------------------

	iommu=on		|	UINT_MAX
     iommu=off && swiotlb on	|	IO_TLB_DEFAULT_SIZE(64M)
	xen_swiotlb on		|	PAGE_SIZE(4K)

-------------------------------------------------------------------------------

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 22 ++++++++++++++++++++--
  1 file changed, 20 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +20 -2
index 134575a3893c..d081fcd22d6b 100644
@@ -80,6 +80,23 @@ static int amdgpu_ttm_init_on_chip(struct amdgpu_device *adev,
  				  false, size_in_page);
  }
 
+static inline unsigned int amdgpu_sg_segment_size(void)
+{
+	unsigned int size = swiotlb_max_segment();
+
+	/* size=0 when amd iommu enabled */
+	if (size == 0)
+		size = UINT_MAX;
+
+	size = rounddown(size, PAGE_SIZE);
+	/* swiotlb_max_segment_size can return 1 byte when it means one page. */
+	if (size < PAGE_SIZE)
+		size = PAGE_SIZE;
+
+	return size;
+}
+
+
  /**
   * amdgpu_evict_flags - Compute placement flags
   *
@@ -760,9 +777,10 @@ static int amdgpu_ttm_tt_pin_userptr(struct ttm_device *bdev,
  	int r;
 
  	/* Allocate an SG array and squash pages into it */
-	r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
-				      (u64)ttm->num_pages << PAGE_SHIFT,
+	r = sg_alloc_table_from_pages_segment(ttm->sg, ttm->pages, ttm->num_pages, 0,
+				      (u64)ttm->num_pages << PAGE_SHIFT, amdgpu_sg_segment_size(),
  				      GFP_KERNEL);
+
  	if (r)
  		goto release_sg;
 
#3 lore raw

[AMD Official Use Only - General]

Hi Christian,

Why should the resulting sg table be too large?
[Shane]
sg_alloc_table_from_pages will set max_segment field as default value UINT_MAX to sg_alloc_table_from_pages_segment. The filed max_segment works as follows:
“Contiguous ranges of the pages are squashed into a single scatterlist node up to the maximum size specified in @max_segment.”
If we don't set the max_segment field, the sg_alloc_table_from_pages may allocate 2M or more continuous ranges of pages.

For what too large?
[Shane]
However, these pages are called pseudo-physical pages on xen dom0, which means that the actual machine pages are not necessarily continuous.
When this happens, the xen_swiotlb will use bounce buffer to do dma operation by swiotlb_tbl_map_single.
But, the xen_swiotlb only allows IO_TLB_SEGSIZE*IO_TLB_SHIFT (256K) continuous pages, and the allocate 2M or more continuous ranges of pages will cause such error "swiotlb buffer is full".

BTW: intel uses the same method to allocate page tables in i915_gem_userptr_get_pages.

Best Regards,
Shane

Quoted 91 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 22, 2022 3:19 PM
To: Xiao, Shane <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amd/amdgpu: solve the issue of allocate continuous pages under xen dom0

Am 22.09.22 um 09:11 schrieb Shane Xiao:

Quoted 2 lines

[Why]
sg_alloc_table_from_pages alloc too large continuous PFN pages under

xen dom0.

Well that sentence doesn't make much sense. Why should the resulting sg table be to large? For what to large?

Regards,
Christian.

Quoted 1 line

However, xen should check continuous MFN pages in

range_straddles_page_boundary.

Quoted 6 lines

When range_straddles_page_boundary return false, some cases fall back into swiotlb process and the continuous allocable page is not enough.

[How]
In fact, xen swiotlb set max_segment default value as UINT_MAX and xen_swiotlb_init_early already change the value to PAGE_SIZE under xen

dom0.

Quoted 8 lines

However amdgpu driver doesn't use the value, which may cause issue such as swiotlb buffer full. Add amd_sg_segment_size according to iommu setting, the details are as follows:
iommu setting | amd_sg_segment_size
-------------------------------------------------------------------------------

	iommu=on		|	UINT_MAX
     iommu=off && swiotlb on	|	IO_TLB_DEFAULT_SIZE(64M)
	xen_swiotlb on		|	PAGE_SIZE(4K)
Quoted 6 lines

----------------------------------------------------------------------
---------

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 22

++++++++++++++++++++--

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_init_on_chip(struct

Quoted 8 lines
  1 file changed, 20 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
index 134575a3893c..d081fcd22d6b 100644
@@ -80,6 +80,23 @@ static int amdgpu_ttm_init_on_chip(struct

amdgpu_device *adev,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_init_on_chip(struct amdgpu_device *adev,

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +9 -0
  				  false, size_in_page);
  }
 
+static inline unsigned int amdgpu_sg_segment_size(void) {
+	unsigned int size = swiotlb_max_segment();
+
+	/* size=0 when amd iommu enabled */
+	if (size == 0)
+		size = UINT_MAX;
+
+	size = rounddown(size, PAGE_SIZE);
+	/* swiotlb_max_segment_size can return 1 byte when it means one

page. */

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_tt_pin_userptr(struct

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +7 -0
+	if (size < PAGE_SIZE)
+		size = PAGE_SIZE;
+
+	return size;
+}
+
+
  /**
   * amdgpu_evict_flags - Compute placement flags
   *
@@ -760,9 +777,10 @@ static int amdgpu_ttm_tt_pin_userptr(struct

ttm_device *bdev,

Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -2
  	int r;
 
  	/* Allocate an SG array and squash pages into it */
-	r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm-
 num_pages, 0,
-				      (u64)ttm->num_pages << PAGE_SHIFT,
+	r = sg_alloc_table_from_pages_segment(ttm->sg, ttm->pages, ttm-
 num_pages, 0,
+				      (u64)ttm->num_pages << PAGE_SHIFT,
+amdgpu_sg_segment_size(),
  				      GFP_KERNEL);
+
  	if (r)
  		goto release_sg;
maintainer
#4 lore raw

Well this patch is a pretty clear NAK.

Using xen_swiotlb with userptr in the first place sounds illegal to me since we need coherent buffers which are incompatible with swiotlb.

In other words using userptrs in our driver stack are fundamentally incompatible with swiotlb, so it doesn't make sense to try o get this working using those workarounds.

So the real question is why are you trying to use userptr with XEN dom0? That won't work correctly at all.

Regards,
Christian.

Am 22.09.22 um 11:00 schrieb Xiao, Shane:

Quoted 113 lines

[AMD Official Use Only - General]

Hi Christian,

Why should the resulting sg table be too large?
[Shane]
sg_alloc_table_from_pages will set max_segment field as default value UINT_MAX to sg_alloc_table_from_pages_segment. The filed max_segment works as follows:
“Contiguous ranges of the pages are squashed into a single scatterlist node up to the maximum size specified in @max_segment.”
If we don't set the max_segment field, the sg_alloc_table_from_pages may allocate 2M or more continuous ranges of pages.

For what too large?
[Shane]
However, these pages are called pseudo-physical pages on xen dom0, which means that the actual machine pages are not necessarily continuous.
When this happens, the xen_swiotlb will use bounce buffer to do dma operation by swiotlb_tbl_map_single.
But, the xen_swiotlb only allows IO_TLB_SEGSIZE*IO_TLB_SHIFT (256K) continuous pages, and the allocate 2M or more continuous ranges of pages will cause such error "swiotlb buffer is full".

BTW: intel uses the same method to allocate page tables in i915_gem_userptr_get_pages.

Best Regards,
Shane

Quoted 91 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 22, 2022 3:19 PM
To: Xiao, Shane <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amd/amdgpu: solve the issue of allocate continuous pages under xen dom0

Am 22.09.22 um 09:11 schrieb Shane Xiao:

Quoted 2 lines

[Why]
sg_alloc_table_from_pages alloc too large continuous PFN pages under

xen dom0.

Well that sentence doesn't make much sense. Why should the resulting sg table be to large? For what to large?

Regards,
Christian.

Quoted 1 line

However, xen should check continuous MFN pages in

range_straddles_page_boundary.

Quoted 6 lines

When range_straddles_page_boundary return false, some cases fall back into swiotlb process and the continuous allocable page is not enough.

[How]
In fact, xen swiotlb set max_segment default value as UINT_MAX and xen_swiotlb_init_early already change the value to PAGE_SIZE under xen

dom0.

Quoted 8 lines

However amdgpu driver doesn't use the value, which may cause issue such as swiotlb buffer full. Add amd_sg_segment_size according to iommu setting, the details are as follows:
iommu setting | amd_sg_segment_size
-------------------------------------------------------------------------------

	iommu=on		|	UINT_MAX
      iommu=off && swiotlb on	|	IO_TLB_DEFAULT_SIZE(64M)
	xen_swiotlb on		|	PAGE_SIZE(4K)
Quoted 6 lines

----------------------------------------------------------------------
---------

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 22

++++++++++++++++++++--

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_init_on_chip(struct

Quoted 8 lines
   1 file changed, 20 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
index 134575a3893c..d081fcd22d6b 100644
@@ -80,6 +80,23 @@ static int amdgpu_ttm_init_on_chip(struct

amdgpu_device *adev,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_init_on_chip(struct amdgpu_device *adev,

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +9 -0
   				  false, size_in_page);
   }
 
+static inline unsigned int amdgpu_sg_segment_size(void) {
+	unsigned int size = swiotlb_max_segment();
+
+	/* size=0 when amd iommu enabled */
+	if (size == 0)
+		size = UINT_MAX;
+
+	size = rounddown(size, PAGE_SIZE);
+	/* swiotlb_max_segment_size can return 1 byte when it means one

page. */

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_tt_pin_userptr(struct

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +7 -0
+	if (size < PAGE_SIZE)
+		size = PAGE_SIZE;
+
+	return size;
+}
+
+
   /**
    * amdgpu_evict_flags - Compute placement flags
    *
@@ -760,9 +777,10 @@ static int amdgpu_ttm_tt_pin_userptr(struct

ttm_device *bdev,

Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -2
   	int r;
 
   	/* Allocate an SG array and squash pages into it */
-	r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm-
 num_pages, 0,
-				      (u64)ttm->num_pages << PAGE_SHIFT,
+	r = sg_alloc_table_from_pages_segment(ttm->sg, ttm->pages, ttm-
 num_pages, 0,
+				      (u64)ttm->num_pages << PAGE_SHIFT,
+amdgpu_sg_segment_size(),
   				      GFP_KERNEL);
+
   	if (r)
   		goto release_sg;
#5 lore raw

[AMD Official Use Only - General]

Hi Christian,

So the real question is why are you trying to use userptr with XEN dom0?
[Shane]
Kfd test KFDMemoryTest.MMBench use userptr. And the test fails on Xen dom0 and succeeds on baremetal. We can also use GTT to allocate coherent buffers by setting HSA_USERPTR_FOR_PAGED_MEM to 0 on xen dom0.

As you mentioned that "In other words using userptrs in our driver stack are fundamentally incompatible with swiotlb ", it seems that IOMMU is not enabled on xen dom0, so the kfd test case falls back into xen swiotlb when using userptr. I really appreciate that you could give me some advices on this issue without IOMMU enabled on xen dom0.

Best Regards,
Shane

Quoted 148 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 22, 2022 5:09 PM
To: Xiao, Shane <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amd/amdgpu: solve the issue of allocate continuous pages under xen dom0

Well this patch is a pretty clear NAK.

Using xen_swiotlb with userptr in the first place sounds illegal to me since we need coherent buffers which are incompatible with swiotlb.

In other words using userptrs in our driver stack are fundamentally incompatible with swiotlb, so it doesn't make sense to try o get this working using those workarounds.

So the real question is why are you trying to use userptr with XEN dom0? That won't work correctly at all.

Regards,
Christian.

Am 22.09.22 um 11:00 schrieb Xiao, Shane:

Quoted 7 lines

[AMD Official Use Only - General]

Hi Christian,

Why should the resulting sg table be too large?
[Shane]
sg_alloc_table_from_pages will set max_segment field as default value

UINT_MAX to sg_alloc_table_from_pages_segment. The filed max_segment works as follows:

Quoted 1 line

“Contiguous ranges of the pages are squashed into a single scatterlist node

up to the maximum size specified in @max_segment.”

Quoted 1 line

If we don't set the max_segment field, the sg_alloc_table_from_pages

may allocate 2M or more continuous ranges of pages.

Quoted 5 lines

For what too large?
[Shane]
However, these pages are called pseudo-physical pages on xen dom0,

which means that the actual machine pages are not necessarily continuous.

Quoted 1 line

When this happens, the xen_swiotlb will use bounce buffer to do dma

operation by swiotlb_tbl_map_single.

Quoted 1 line

But, the xen_swiotlb only allows IO_TLB_SEGSIZE*IO_TLB_SHIFT (256K)

continuous pages, and the allocate 2M or more continuous ranges of pages will cause such error "swiotlb buffer is full".

Quoted 2 lines

BTW: intel uses the same method to allocate page tables in

i915_gem_userptr_get_pages.

Quoted 26 lines

Best Regards,
Shane

Quoted 22 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 22, 2022 3:19 PM
To: Xiao, Shane <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amd/amdgpu: solve the issue of allocate continuous pages under xen dom0

Am 22.09.22 um 09:11 schrieb Shane Xiao:

Quoted 2 lines

[Why]
sg_alloc_table_from_pages alloc too large continuous PFN pages under

xen dom0.

Well that sentence doesn't make much sense. Why should the resulting sg table be to large? For what to large?

Regards,
Christian.

Quoted 1 line

However, xen should check continuous MFN pages in

range_straddles_page_boundary.

Quoted 2 lines

When range_straddles_page_boundary return false, some cases fall back into swiotlb process and the continuous allocable page is not

enough.

Quoted 71 lines
Quoted 71 lines
Quoted 5 lines

[How]
In fact, xen swiotlb set max_segment default value as UINT_MAX and xen_swiotlb_init_early already change the value to PAGE_SIZE under xen

dom0.

Quoted 8 lines

However amdgpu driver doesn't use the value, which may cause issue such as swiotlb buffer full. Add amd_sg_segment_size according to iommu setting, the details are as follows:
iommu setting | amd_sg_segment_size
-------------------------------------------------------------------------------

	iommu=on		|	UINT_MAX
      iommu=off && swiotlb on	|	IO_TLB_DEFAULT_SIZE(64M)
	xen_swiotlb on		|	PAGE_SIZE(4K)
Quoted 7 lines

--------------------------------------------------------------------
--
---------

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 22

++++++++++++++++++++--

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_init_on_chip(struct

Quoted 8 lines
   1 file changed, 20 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
index 134575a3893c..d081fcd22d6b 100644
@@ -80,6 +80,23 @@ static int amdgpu_ttm_init_on_chip(struct

amdgpu_device *adev,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_init_on_chip(struct amdgpu_device *adev,

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +9 -0
   				  false, size_in_page);
   }
 
+static inline unsigned int amdgpu_sg_segment_size(void) {
+	unsigned int size = swiotlb_max_segment();
+
+	/* size=0 when amd iommu enabled */
+	if (size == 0)
+		size = UINT_MAX;
+
+	size = rounddown(size, PAGE_SIZE);
+	/* swiotlb_max_segment_size can return 1 byte when it means one

page. */

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_tt_pin_userptr(struct

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +7 -0
+	if (size < PAGE_SIZE)
+		size = PAGE_SIZE;
+
+	return size;
+}
+
+
   /**
    * amdgpu_evict_flags - Compute placement flags
    *
@@ -760,9 +777,10 @@ static int amdgpu_ttm_tt_pin_userptr(struct

ttm_device *bdev,

Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -2
   	int r;
 
   	/* Allocate an SG array and squash pages into it */
-	r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm-
 num_pages, 0,
-				      (u64)ttm->num_pages << PAGE_SHIFT,
+	r = sg_alloc_table_from_pages_segment(ttm->sg, ttm->pages, ttm-
 num_pages, 0,
+				      (u64)ttm->num_pages << PAGE_SHIFT,
+amdgpu_sg_segment_size(),
   				      GFP_KERNEL);
+
   	if (r)
   		goto release_sg;