drm/amdgpu: grab a PM reference on importing DMA-bufs
Subsystems (2)
PATCH 1/2 drm/amdgpu: grab a PM reference on importing DMA-bufs
We need the device alive and kicking for the move notify callback to work correctly. Not sure if we should have that here or in the callback itself, but go with the defensive variant for now.
Signed-off-by Christian König <[email protected]>
--- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 37 ++++++++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.h | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 4 +-- 3 files changed, 36 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +33 -4
@@ -512,6 +512,7 @@ struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev, {struct dma_buf_attachment *attach;struct drm_gem_object *obj; +int r;if (dma_buf->ops == &amdgpu_dmabuf_ops) { obj = dma_buf->priv;
@@ -525,20 +526,48 @@ struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev, } } + r =pm_runtime_resume_and_get (dev->dev); +if (r) +return ERR_PTR (r); + obj =amdgpu_dma_buf_create_obj (dev, dma_buf); -if (IS_ERR (obj)) -return obj; +if (IS_ERR (obj)) { + r =PTR_ERR (obj); + goto err_pm; + } attach =dma_buf_dynamic_attach (dma_buf, dev->dev, &amdgpu_dma_buf_attach_ops, obj);if (IS_ERR (attach)) { -drm_gem_object_put (obj); -return ERR_CAST (attach); + r =PTR_ERR (attach); + goto err_put; }get_dma_buf (dma_buf); obj->import_attach = attach;return obj; + +err_put: +drm_gem_object_put (obj); + +err_pm: +pm_runtime_put_autosuspend (dev->dev); +return ERR_PTR (r); +} + +/** +* amdgpu_gem_prime_destroy - destroy an imported BO again +* @bo: the imported BO +* +* Make sure to cleanup the SG table, detach from the DMA-buf and drop the PM +* reference we grabbed. +*/ +void amdgpu_gem_prime_destroy (struct amdgpu_bo *bo) +{ +struct amdgpu_device *adev =amdgpu_ttm_adev (bo->tbo.bdev); + +drm_prime_gem_destroy (&bo->tbo.base, bo->tbo.sg); +pm_runtime_put_autosuspend (adev_to_drm (adev)->dev); }/**
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.h +1 -0
@@ -29,6 +29,7 @@ struct dma_buf *amdgpu_gem_prime_export(struct drm_gem_object *gobj, int flags);struct drm_gem_object *amdgpu_gem_prime_import (struct drm_device *dev,struct dma_buf *dma_buf); +void amdgpu_gem_prime_destroy (struct amdgpu_bo *bo);bool amdgpu_dmabuf_is_xgmi_accessible (struct amdgpu_device *adev,struct amdgpu_bo *bo);
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -2
@@ -39,6 +39,7 @@#include "amdgpu.h" #include "amdgpu_trace.h" #include "amdgpu_amdkfd.h" +#include "amdgpu_dma_buf.h" /** * DOC: amdgpu_object
@@ -58,9 +59,8 @@ static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)struct amdgpu_bo *bo =ttm_to_amdgpu_bo (tbo);amdgpu_bo_kunmap (bo); -if (bo->tbo.base.import_attach) -drm_prime_gem_destroy (&bo->tbo.base, bo->tbo.sg); +amdgpu_gem_prime_destroy (bo);drm_gem_object_release (&bo->tbo.base);amdgpu_bo_unref (&bo->parent);kvfree (bo);
Signature · 1 line
2.25.1
PATCH 2/2 drm/amdgpu: protected amdgpu_dma_buf_move_notify against hotplug
Add the proper drm_dev_enter()/drm_dev_exit() calls here.
Signed-off-by Christian König <[email protected]>
--- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +7 -1
@@ -36,6 +36,7 @@#include "amdgpu_gem.h" #include "amdgpu_dma_buf.h" #include "amdgpu_xgmi.h" +#include <drm/drm_drv.h> #include <drm/amdgpu_drm.h> #include <linux/dma-buf.h> #include <linux/dma-fence-array.h>
@@ -447,14 +448,18 @@ amdgpu_dma_buf_move_notify(struct dma_buf_attachment *attach)struct ttm_operation_ctx ctx = { false, false };struct ttm_placement placement = {};struct amdgpu_vm_bo_base *bo_base; -int r; +int idx, r;if (bo->tbo.resource->mem_type ==TTM_PL_SYSTEM )return ; +if (!drm_dev_enter (adev_to_drm (adev), &idx)) +return ; + r =ttm_bo_validate (&bo->tbo, &placement, &ctx);if (r) {DRM_ERROR ("Failed to invalidate DMA-buf import (%d))\n" , r); +drm_dev_exit (idx);return ; }
@@ -490,6 +495,7 @@ amdgpu_dma_buf_move_notify(struct dma_buf_attachment *attach)dma_resv_unlock (resv); } +drm_dev_exit (idx); }static const struct dma_buf_attach_ops amdgpu_dma_buf_attach_ops = {
Signature · 1 line
2.25.1
Re: [PATCH 2/2] drm/amdgpu: protected amdgpu_dma_buf_move_notify against hotplug
Series is:
Reviewed-by Alex Deucher <[email protected]>
Quoted 51 lines · Christian König
Add the proper drm_dev_enter()/drm_dev_exit() calls here.
Signed-off-by Christian König <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +7
-2@@ -36,6 +36,7 @@#include "amdgpu_gem.h" #include "amdgpu_dma_buf.h" #include "amdgpu_xgmi.h" +#include <drm/drm_drv.h> #include <drm/amdgpu_drm.h> #include <linux/dma-buf.h> #include <linux/dma-fence-array.h> @@ -447,14 +448,18 @@ amdgpu_dma_buf_move_notify(struct dma_buf_attachment *attach)struct ttm_operation_ctx ctx = { false, false };struct ttm_placement placement = {};struct amdgpu_vm_bo_base *bo_base; -int r; +int idx, r;if (bo->tbo.resource->mem_type ==TTM_PL_SYSTEM )return ; +if (!drm_dev_enter (adev_to_drm (adev), &idx)) +return ; + r =ttm_bo_validate (&bo->tbo, &placement, &ctx);if (r) {DRM_ERROR ("Failed to invalidate DMA-buf import (%d))\n" , r); +drm_dev_exit (idx);return ; }@@ -490,6 +495,7 @@ amdgpu_dma_buf_move_notify(struct dma_buf_attachment *attach)dma_resv_unlock (resv); } +drm_dev_exit (idx); }static const struct dma_buf_attach_ops amdgpu_dma_buf_attach_ops = { --2.25.1