Skip to content

Commit ddddeda

Browse files
asahilinamartinezjavier
authored andcommitted
drm/shmem-helper: Fix locking for drm_gem_shmem_get_pages_sgt()
Other functions touching shmem->sgt take the pages lock, so do that here too. drm_gem_shmem_get_pages() & co take the same lock, so move to the _locked() variants to avoid recursive locking. Discovered while auditing locking to write the Rust abstractions. Fixes: 2194a63 ("drm: Add library for shmem backed GEM objects") Fixes: 4fa3d66 ("drm/shmem: Do dma_unmap_sg before purging pages") Signed-off-by: Asahi Lina <[email protected]> Reviewed-by: Javier Martinez Canillas <[email protected]> Signed-off-by: Javier Martinez Canillas <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected] (cherry picked from commit aa8c85a) Signed-off-by: Javier Martinez Canillas <[email protected]>
1 parent 38b2d8e commit ddddeda

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

drivers/gpu/drm/drm_gem_shmem_helper.c

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -678,23 +678,7 @@ struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object *shmem)
678678
}
679679
EXPORT_SYMBOL_GPL(drm_gem_shmem_get_sg_table);
680680

681-
/**
682-
* drm_gem_shmem_get_pages_sgt - Pin pages, dma map them, and return a
683-
* scatter/gather table for a shmem GEM object.
684-
* @shmem: shmem GEM object
685-
*
686-
* This function returns a scatter/gather table suitable for driver usage. If
687-
* the sg table doesn't exist, the pages are pinned, dma-mapped, and a sg
688-
* table created.
689-
*
690-
* This is the main function for drivers to get at backing storage, and it hides
691-
* and difference between dma-buf imported and natively allocated objects.
692-
* drm_gem_shmem_get_sg_table() should not be directly called by drivers.
693-
*
694-
* Returns:
695-
* A pointer to the scatter/gather table of pinned pages or errno on failure.
696-
*/
697-
struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem)
681+
static struct sg_table *drm_gem_shmem_get_pages_sgt_locked(struct drm_gem_shmem_object *shmem)
698682
{
699683
struct drm_gem_object *obj = &shmem->base;
700684
int ret;
@@ -705,7 +689,7 @@ struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem)
705689

706690
WARN_ON(obj->import_attach);
707691

708-
ret = drm_gem_shmem_get_pages(shmem);
692+
ret = drm_gem_shmem_get_pages_locked(shmem);
709693
if (ret)
710694
return ERR_PTR(ret);
711695

@@ -727,10 +711,40 @@ struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem)
727711
sg_free_table(sgt);
728712
kfree(sgt);
729713
err_put_pages:
730-
drm_gem_shmem_put_pages(shmem);
714+
drm_gem_shmem_put_pages_locked(shmem);
731715
return ERR_PTR(ret);
732716
}
733-
EXPORT_SYMBOL_GPL(drm_gem_shmem_get_pages_sgt);
717+
718+
/**
719+
* drm_gem_shmem_get_pages_sgt - Pin pages, dma map them, and return a
720+
* scatter/gather table for a shmem GEM object.
721+
* @shmem: shmem GEM object
722+
*
723+
* This function returns a scatter/gather table suitable for driver usage. If
724+
* the sg table doesn't exist, the pages are pinned, dma-mapped, and a sg
725+
* table created.
726+
*
727+
* This is the main function for drivers to get at backing storage, and it hides
728+
* and difference between dma-buf imported and natively allocated objects.
729+
* drm_gem_shmem_get_sg_table() should not be directly called by drivers.
730+
*
731+
* Returns:
732+
* A pointer to the scatter/gather table of pinned pages or errno on failure.
733+
*/
734+
struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem)
735+
{
736+
int ret;
737+
struct sg_table *sgt;
738+
739+
ret = mutex_lock_interruptible(&shmem->pages_lock);
740+
if (ret)
741+
return ERR_PTR(ret);
742+
sgt = drm_gem_shmem_get_pages_sgt_locked(shmem);
743+
mutex_unlock(&shmem->pages_lock);
744+
745+
return sgt;
746+
}
747+
EXPORT_SYMBOL(drm_gem_shmem_get_pages_sgt);
734748

735749
/**
736750
* drm_gem_shmem_prime_import_sg_table - Produce a shmem GEM object from

0 commit comments

Comments
 (0)