Skip to contentSkip to conversation

drm/amdgpu: allow pinning DMA-bufs into VRAM if all importers can do P2P

PATCH In review Felix Kuehling 5 participants 5 messages Jan 9, 2025 – Mar 31, 2025

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

PATCH 1/1 drm/amdgpu: allow pinning DMA-bufs into VRAM if all importers can do P2P

From: Christian König <[email protected]>

Try pinning into VRAM to allow P2P with RDMA NICs without ODP support if all attachments can do P2P. If any attachment can't do P2P just pin into GTT instead.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 25 +++++++++++++++------
 1 file changed, 18 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +18 -7
index 8e81a83d37d84..83390143c2e9f 100644
@@ -72,11 +72,25 @@ static int amdgpu_dma_buf_attach(struct dma_buf *dmabuf,
  */
 static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach)
 {
-	struct drm_gem_object *obj = attach->dmabuf->priv;
-	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
+	struct dma_buf *dmabuf = attach->dmabuf;
+	struct amdgpu_bo *bo = gem_to_amdgpu_bo(dmabuf->priv);
+	u32 domains = bo->preferred_domains;
 
-	/* pin buffer into GTT */
-	return amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
+	dma_resv_assert_held(dmabuf->resv);
+
+	/*
+	 * Try pinning into VRAM to allow P2P with RDMA NICs without ODP
+	 * support if all attachments can do P2P. If any attachment can't do
+	 * P2P just pin into GTT instead.
+	 */
+	list_for_each_entry(attach, &dmabuf->attachments, node)
+		if (!attach->peer2peer)
+			domains &= ~AMDGPU_GEM_DOMAIN_VRAM;
+
+	if (domains & AMDGPU_GEM_DOMAIN_VRAM)
+		bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
+
+	return amdgpu_bo_pin(bo, domains);
 }
 
 /**
@@ -131,9 +145,6 @@ static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach,
 		r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
 		if (r)
 			return ERR_PTR(r);
-
-	} else if (bo->tbo.resource->mem_type != TTM_PL_TT) {
-		return ERR_PTR(-EBUSY);
 	}
 
 	switch (bo->tbo.resource->mem_type) {
Signature · 1 line
2.34.1
#1 lore raw
Quoted 7 lines · Felix Kuehling

From: Christian König <[email protected]>

Try pinning into VRAM to allow P2P with RDMA NICs without ODP support if all attachments can do P2P. If any attachment can't do P2P just pin into GTT instead.

Quoted 5 lines

I thought the big reason we never did this was that there's no way to account vram pinning properly, until now? But with the cgroup stuff finally moving that's no longer a concern I guess, but I'd at least note that down in the commit message. And maybe land this only after the cgroup stuff is in?

Anyway code looks like how we designed this all to work, so ack. -Sima

Quoted 51 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 25 +++++++++++++++------
 1 file changed, 18 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +18 -8
index 8e81a83d37d84..83390143c2e9f 100644
@@ -72,11 +72,25 @@ static int amdgpu_dma_buf_attach(struct dma_buf *dmabuf,
  */
 static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach)
 {
-       struct drm_gem_object *obj = attach->dmabuf->priv;
-       struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
+       struct dma_buf *dmabuf = attach->dmabuf;
+       struct amdgpu_bo *bo = gem_to_amdgpu_bo(dmabuf->priv);
+       u32 domains = bo->preferred_domains;
 
-       /* pin buffer into GTT */
-       return amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
+       dma_resv_assert_held(dmabuf->resv);
+
+       /*
+        * Try pinning into VRAM to allow P2P with RDMA NICs without ODP
+        * support if all attachments can do P2P. If any attachment can't do
+        * P2P just pin into GTT instead.
+        */
+       list_for_each_entry(attach, &dmabuf->attachments, node)
+               if (!attach->peer2peer)
+                       domains &= ~AMDGPU_GEM_DOMAIN_VRAM;
+
+       if (domains & AMDGPU_GEM_DOMAIN_VRAM)
+               bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
+
+       return amdgpu_bo_pin(bo, domains);
 }
 
 /**
@@ -131,9 +145,6 @@ static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach,
                r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
                if (r)
                        return ERR_PTR(r);
-
-       } else if (bo->tbo.resource->mem_type != TTM_PL_TT) {
-               return ERR_PTR(-EBUSY);
        }
 
        switch (bo->tbo.resource->mem_type) {
--

2.34.1

Signature · 3 lines
Simona Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#2 lore raw
In reply to Felix Kuehling
Quoted 31 lines · Felix Kuehling

From: Christian König <[email protected]>

Try pinning into VRAM to allow P2P with RDMA NICs without ODP support if all attachments can do P2P. If any attachment can't do P2P just pin into GTT instead.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 25 +++++++++++++++------
  1 file changed, 18 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +3 -4
index 8e81a83d37d84..83390143c2e9f 100644
@@ -72,11 +72,25 @@ static int amdgpu_dma_buf_attach(struct dma_buf *dmabuf,
   */
  static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach)
  {
-	struct drm_gem_object *obj = attach->dmabuf->priv;
-	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
+	struct dma_buf *dmabuf = attach->dmabuf;
+	struct amdgpu_bo *bo = gem_to_amdgpu_bo(dmabuf->priv);
+	u32 domains = bo->preferred_domains;
 
-	/* pin buffer into GTT */
-	return amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);

drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c static int amdgpu_dma_buf_attach(struct dma_buf *dmabuf, · line 88

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +10 -0
+	dma_resv_assert_held(dmabuf->resv);
+
+	/*
+	 * Try pinning into VRAM to allow P2P with RDMA NICs without ODP
+	 * support if all attachments can do P2P. If any attachment can't do
+	 * P2P just pin into GTT instead.
+	 */
+	list_for_each_entry(attach, &dmabuf->attachments, node)
+		if (!attach->peer2peer)
+			domains &= ~AMDGPU_GEM_DOMAIN_VRAM;

[JZ] I think we can stop check once one  entry has no p2p support.

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +5 -0
+
+	if (domains & AMDGPU_GEM_DOMAIN_VRAM)
+		bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
+
+	return amdgpu_bo_pin(bo, domains);
  }
 
  /**

drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach, · line 136

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +0 -3
@@ -131,9 +145,6 @@ static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach,
  		r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  		if (r)
  			return ERR_PTR(r);
-
-	} else if (bo->tbo.resource->mem_type != TTM_PL_TT) {
-		return ERR_PTR(-EBUSY);

[JZ] Not sure if kfd_mem_dmamap_dmabuf needs update also.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +0 -0
  	}
 
  	switch (bo->tbo.resource->mem_type) {
#3 lore raw
In reply to Felix Kuehling
Quoted 7 lines · Felix Kuehling

From: Christian König <[email protected]>

Try pinning into VRAM to allow P2P with RDMA NICs without ODP support if all attachments can do P2P. If any attachment can't do P2P just pin into GTT instead.

Quoted 5 lines

Applied along with the cgroups patch for amdgpu.

Alex

Quoted 51 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 25 +++++++++++++++------
 1 file changed, 18 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +18 -8
index 8e81a83d37d84..83390143c2e9f 100644
@@ -72,11 +72,25 @@ static int amdgpu_dma_buf_attach(struct dma_buf *dmabuf,
  */
 static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach)
 {
-       struct drm_gem_object *obj = attach->dmabuf->priv;
-       struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
+       struct dma_buf *dmabuf = attach->dmabuf;
+       struct amdgpu_bo *bo = gem_to_amdgpu_bo(dmabuf->priv);
+       u32 domains = bo->preferred_domains;
 
-       /* pin buffer into GTT */
-       return amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
+       dma_resv_assert_held(dmabuf->resv);
+
+       /*
+        * Try pinning into VRAM to allow P2P with RDMA NICs without ODP
+        * support if all attachments can do P2P. If any attachment can't do
+        * P2P just pin into GTT instead.
+        */
+       list_for_each_entry(attach, &dmabuf->attachments, node)
+               if (!attach->peer2peer)
+                       domains &= ~AMDGPU_GEM_DOMAIN_VRAM;
+
+       if (domains & AMDGPU_GEM_DOMAIN_VRAM)
+               bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
+
+       return amdgpu_bo_pin(bo, domains);
 }
 
 /**
@@ -131,9 +145,6 @@ static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach,
                r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
                if (r)
                        return ERR_PTR(r);
-
-       } else if (bo->tbo.resource->mem_type != TTM_PL_TT) {
-               return ERR_PTR(-EBUSY);
        }
 
        switch (bo->tbo.resource->mem_type) {
--

2.34.1

maintainer
#4 lore raw

Am 28.03.25 um 20:14 schrieb Alex Deucher:

Quoted 7 lines · Felix Kuehling
Quoted 6 lines

From: Christian König <[email protected]>

Try pinning into VRAM to allow P2P with RDMA NICs without ODP support if all attachments can do P2P. If any attachment can't do P2P just pin into GTT instead.

Quoted 6 lines
Quoted 5 lines

Applied along with the cgroups patch for amdgpu.

Nice, going to update the internal people as well.

Christian.

Quoted 54 lines

Alex

Quoted 51 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 25 +++++++++++++++------
 1 file changed, 18 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +18 -8
index 8e81a83d37d84..83390143c2e9f 100644
@@ -72,11 +72,25 @@ static int amdgpu_dma_buf_attach(struct dma_buf *dmabuf,
  */
 static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach)
 {
-       struct drm_gem_object *obj = attach->dmabuf->priv;
-       struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
+       struct dma_buf *dmabuf = attach->dmabuf;
+       struct amdgpu_bo *bo = gem_to_amdgpu_bo(dmabuf->priv);
+       u32 domains = bo->preferred_domains;
 
-       /* pin buffer into GTT */
-       return amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
+       dma_resv_assert_held(dmabuf->resv);
+
+       /*
+        * Try pinning into VRAM to allow P2P with RDMA NICs without ODP
+        * support if all attachments can do P2P. If any attachment can't do
+        * P2P just pin into GTT instead.
+        */
+       list_for_each_entry(attach, &dmabuf->attachments, node)
+               if (!attach->peer2peer)
+                       domains &= ~AMDGPU_GEM_DOMAIN_VRAM;
+
+       if (domains & AMDGPU_GEM_DOMAIN_VRAM)
+               bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
+
+       return amdgpu_bo_pin(bo, domains);
 }
 
 /**
@@ -131,9 +145,6 @@ static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach,
                r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
                if (r)
                        return ERR_PTR(r);
-
-       } else if (bo->tbo.resource->mem_type != TTM_PL_TT) {
-               return ERR_PTR(-EBUSY);
        }
 
        switch (bo->tbo.resource->mem_type) {
--

2.34.1