diff mbox series

[v19,24/30] drm/shmem-helper: Optimize unlocked get_pages_sgt()

Message ID 20240105184624.508603-25-dmitry.osipenko@collabora.com (mailing list archive)
State New, archived
Headers show
Series Add generic memory shrinker to VirtIO-GPU and Panfrost DRM drivers | expand

Commit Message

Dmitry Osipenko Jan. 5, 2024, 6:46 p.m. UTC
SGT isn't refcounted. Once SGT pointer has been obtained, it remains the
same for both locked and unlocked get_pages_sgt(). Return cached SGT
directly without taking a potentially expensive lock.

Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

Comments

Boris Brezillon Jan. 25, 2024, 9:28 a.m. UTC | #1
On Fri,  5 Jan 2024 21:46:18 +0300
Dmitry Osipenko <dmitry.osipenko@collabora.com> wrote:

> SGT isn't refcounted. Once SGT pointer has been obtained, it remains the
> same for both locked and unlocked get_pages_sgt(). Return cached SGT
> directly without taking a potentially expensive lock.
> 
> Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>

Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>

but I'm wondering if we should have made this change directly in
'drm/shmem-helper: Change sgt allocation policy'.

> ---
>  drivers/gpu/drm/drm_gem_shmem_helper.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
> index 8fd7851c088b..e6e6e693ab95 100644
> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
> @@ -962,6 +962,18 @@ struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem)
>  	    drm_WARN_ON(obj->dev, drm_gem_shmem_is_purgeable(shmem)))
>  		return ERR_PTR(-EBUSY);
>  
> +	/*
> +	 * Drivers that use shrinker should take into account that shrinker
> +	 * may relocate BO, thus invalidating the returned SGT pointer.
> +	 * Such drivers should pin GEM while they use SGT.
> +	 *
> +	 * Drivers that don't use shrinker should take into account that
> +	 * SGT is released together with the GEM pages. Pages should be kept
> +	 * alive while SGT is used.
> +	 */
> +	if (shmem->sgt)
> +		return shmem->sgt;
> +
>  	ret = dma_resv_lock_interruptible(shmem->base.resv, NULL);
>  	if (ret)
>  		return ERR_PTR(ret);
diff mbox series

Patch

diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
index 8fd7851c088b..e6e6e693ab95 100644
--- a/drivers/gpu/drm/drm_gem_shmem_helper.c
+++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
@@ -962,6 +962,18 @@  struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem)
 	    drm_WARN_ON(obj->dev, drm_gem_shmem_is_purgeable(shmem)))
 		return ERR_PTR(-EBUSY);
 
+	/*
+	 * Drivers that use shrinker should take into account that shrinker
+	 * may relocate BO, thus invalidating the returned SGT pointer.
+	 * Such drivers should pin GEM while they use SGT.
+	 *
+	 * Drivers that don't use shrinker should take into account that
+	 * SGT is released together with the GEM pages. Pages should be kept
+	 * alive while SGT is used.
+	 */
+	if (shmem->sgt)
+		return shmem->sgt;
+
 	ret = dma_resv_lock_interruptible(shmem->base.resv, NULL);
 	if (ret)
 		return ERR_PTR(ret);