Skip to contentSkip to conversation

drm/amdgpu: place VRAM-capable dma-buf imports in the PREEMPT domain

PATCH In review Mukul Joshi 4 participants 4 messages Sep 24 – Sep 25

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

PATCH v2 1/1 drm/amdgpu: place VRAM-capable dma-buf imports in the PREEMPT domain

An imported dma-buf BO backed by a VRAM-capable exporter holds no host pages of its own, yet when validated into TTM_PL_TT it is charged against the GTT page budget; importing many such buffers can exhaust that budget and fail with -ENOMEM even though no host memory is used. Set AMDGPU_GEM_CREATE_PREEMPTIBLE on these imports so they land in the PREEMPT domain instead, which is GTT-like and still bound (keeping the exporter resident) but carries no host-page limit.

---
v1->v2:
- Check if p2p access is allowed. If not, clear the PREEMPT flag since the BO will be put in GTT domain.
- v1 reviewed by Felix.

 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +20 -0
index 4031ebe075d8..bbe518757907 100644
@@ -472,6 +472,15 @@ amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)
 					 AMDGPU_GEM_CREATE_COHERENT |
 					 AMDGPU_GEM_CREATE_EXT_COHERENT |
 					 AMDGPU_GEM_CREATE_UNCACHED);
+
+		/*
+		 * A VRAM-capable import is backed by the exporter's device
+		 * memory, not host pages of its own. Place it in the PREEMPT
+		 * domain (GTT-like, but with no host-page budget) so many such
+		 * imports cannot exhaust the GTT limit.
+		 */
+		if (other->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM)
+			flags |= AMDGPU_GEM_CREATE_PREEMPTIBLE;
 	}
 
 	ret = amdgpu_gem_object_create(adev, dma_buf->size, PAGE_SIZE,
@@ -618,6 +627,17 @@ struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
 		return ERR_CAST(attach);
 	}
 
+	/*
+	 * The PREEMPT domain is only correct when the attachment actually
+	 * resolves to P2P access, in which case the import lives in the
+	 * exporter's VRAM and consumes no host GTT pages.  Without P2P the
+	 * import is staged in system memory and must count against the GTT
+	 * budget normally, so drop AMDGPU_GEM_CREATE_PREEMPTIBLE again.  The
+	 * BO is not published yet, so clearing the flag here is race-free.
+	 */
+	if (!attach->peer2peer)
+		gem_to_amdgpu_bo(obj)->flags &= ~AMDGPU_GEM_CREATE_PREEMPTIBLE;
+
 	get_dma_buf(dma_buf);
 	obj->import_attach = attach;
 	return obj;
Signature · 1 line
2.55.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines · Mukul Joshi

An imported dma-buf BO backed by a VRAM-capable exporter holds no host pages of its own, yet when validated into TTM_PL_TT it is charged against the GTT page budget; importing many such buffers can exhaust that budget and fail with -ENOMEM even though no host memory is used. Set AMDGPU_GEM_CREATE_PREEMPTIBLE on these imports so they land in the PREEMPT domain instead, which is GTT-like and still bound (keeping the exporter resident) but carries no host-page limit.

Quoted 50 lines

---
v1->v2:
- Check if p2p access is allowed. If not, clear the PREEMPT flag since the BO will be put in GTT domain.
- v1 reviewed by Felix.

 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +20 -1
index 4031ebe075d8..bbe518757907 100644
@@ -472,6 +472,15 @@ amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)
                                         AMDGPU_GEM_CREATE_COHERENT |
                                         AMDGPU_GEM_CREATE_EXT_COHERENT |
                                         AMDGPU_GEM_CREATE_UNCACHED);
+
+               /*
+                * A VRAM-capable import is backed by the exporter's device
+                * memory, not host pages of its own. Place it in the PREEMPT
+                * domain (GTT-like, but with no host-page budget) so many such
+                * imports cannot exhaust the GTT limit.
+                */
+               if (other->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM)
+                       flags |= AMDGPU_GEM_CREATE_PREEMPTIBLE;
        }
 
        ret = amdgpu_gem_object_create(adev, dma_buf->size, PAGE_SIZE,
@@ -618,6 +627,17 @@ struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
                return ERR_CAST(attach);
        }
 
+       /*
+        * The PREEMPT domain is only correct when the attachment actually
+        * resolves to P2P access, in which case the import lives in the
+        * exporter's VRAM and consumes no host GTT pages.  Without P2P the
+        * import is staged in system memory and must count against the GTT
+        * budget normally, so drop AMDGPU_GEM_CREATE_PREEMPTIBLE again.  The
+        * BO is not published yet, so clearing the flag here is race-free.
+        */
+       if (!attach->peer2peer)
+               gem_to_amdgpu_bo(obj)->flags &= ~AMDGPU_GEM_CREATE_PREEMPTIBLE;
+
        get_dma_buf(dma_buf);
        obj->import_attach = attach;
        return obj;
--

2.55.0

Reviewed-by on v2 1/1
#2 lore raw
In reply to Mukul Joshi
Quoted 9 lines · Mukul Joshi

An imported dma-buf BO backed by a VRAM-capable exporter holds no host pages of its own, yet when validated into TTM_PL_TT it is charged against the GTT page budget; importing many such buffers can exhaust that budget and fail with -ENOMEM even though no host memory is used. Set AMDGPU_GEM_CREATE_PREEMPTIBLE on these imports so they land in the PREEMPT domain instead, which is GTT-like and still bound (keeping the exporter resident) but carries no host-page limit.

Quoted 47 lines

---
v1->v2:
- Check if p2p access is allowed. If not, clear the PREEMPT flag since the BO will be put in GTT domain.
- v1 reviewed by Felix.

  drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 20 ++++++++++++++++++++
  1 file changed, 20 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +20 -0
index 4031ebe075d8..bbe518757907 100644
@@ -472,6 +472,15 @@ amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)
  					 AMDGPU_GEM_CREATE_COHERENT |
  					 AMDGPU_GEM_CREATE_EXT_COHERENT |
  					 AMDGPU_GEM_CREATE_UNCACHED);
+
+		/*
+		 * A VRAM-capable import is backed by the exporter's device
+		 * memory, not host pages of its own. Place it in the PREEMPT
+		 * domain (GTT-like, but with no host-page budget) so many such
+		 * imports cannot exhaust the GTT limit.
+		 */
+		if (other->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM)
+			flags |= AMDGPU_GEM_CREATE_PREEMPTIBLE;
  	}
 
  	ret = amdgpu_gem_object_create(adev, dma_buf->size, PAGE_SIZE,
@@ -618,6 +627,17 @@ struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
  		return ERR_CAST(attach);
  	}
 
+	/*
+	 * The PREEMPT domain is only correct when the attachment actually
+	 * resolves to P2P access, in which case the import lives in the
+	 * exporter's VRAM and consumes no host GTT pages.  Without P2P the
+	 * import is staged in system memory and must count against the GTT
+	 * budget normally, so drop AMDGPU_GEM_CREATE_PREEMPTIBLE again.  The
+	 * BO is not published yet, so clearing the flag here is race-free.
+	 */
+	if (!attach->peer2peer)
+		gem_to_amdgpu_bo(obj)->flags &= ~AMDGPU_GEM_CREATE_PREEMPTIBLE;
+
  	get_dma_buf(dma_buf);
  	obj->import_attach = attach;
  	return obj;
maintainer Reviewed-by on v2 1/1
#3 lore raw
In reply to Mukul Joshi
Quoted 10 lines · Mukul Joshi

An imported dma-buf BO backed by a VRAM-capable exporter holds no host pages of its own, yet when validated into TTM_PL_TT it is charged against the GTT page budget; importing many such buffers can exhaust that budget and fail with -ENOMEM even though no host memory is used. Set AMDGPU_GEM_CREATE_PREEMPTIBLE on these imports so they land in the PREEMPT domain instead, which is GTT-like and still bound (keeping the exporter resident) but carries no host-page limit.

Quoted 47 lines

---
v1->v2:
- Check if p2p access is allowed. If not, clear the PREEMPT flag since the BO will be put in GTT domain.
- v1 reviewed by Felix.

 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +20 -0
index 4031ebe075d8..bbe518757907 100644
@@ -472,6 +472,15 @@ amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)
 					 AMDGPU_GEM_CREATE_COHERENT |
 					 AMDGPU_GEM_CREATE_EXT_COHERENT |
 					 AMDGPU_GEM_CREATE_UNCACHED);
+
+		/*
+		 * A VRAM-capable import is backed by the exporter's device
+		 * memory, not host pages of its own. Place it in the PREEMPT
+		 * domain (GTT-like, but with no host-page budget) so many such
+		 * imports cannot exhaust the GTT limit.
+		 */
+		if (other->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM)
+			flags |= AMDGPU_GEM_CREATE_PREEMPTIBLE;
 	}
 
 	ret = amdgpu_gem_object_create(adev, dma_buf->size, PAGE_SIZE,
@@ -618,6 +627,17 @@ struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
 		return ERR_CAST(attach);
 	}
 
+	/*
+	 * The PREEMPT domain is only correct when the attachment actually
+	 * resolves to P2P access, in which case the import lives in the
+	 * exporter's VRAM and consumes no host GTT pages.  Without P2P the
+	 * import is staged in system memory and must count against the GTT
+	 * budget normally, so drop AMDGPU_GEM_CREATE_PREEMPTIBLE again.  The
+	 * BO is not published yet, so clearing the flag here is race-free.
+	 */
+	if (!attach->peer2peer)
+		gem_to_amdgpu_bo(obj)->flags &= ~AMDGPU_GEM_CREATE_PREEMPTIBLE;
+
 	get_dma_buf(dma_buf);
 	obj->import_attach = attach;
 	return obj;