Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] accel/thames: race between thames_bo_bind() and
  thames_gem_close()
- [Medium] accel/thames: missing size overflow check in
  thames_ioctl_bo_create()
--

commit 8fe848e1127a19686a4f17b2a469e560c388e5ec
Author: Tomeu Vizoso <[email protected]>

accel/thames: Add IOCTLs for BO creation and mapping

This commit adds the BO_CREATE, BO_MMAP_OFFSET, and BO_BIND ioctls for
creating shmem- or DMA-backed buffer objects and binding them into DSP
context page tables.

> diff --git a/drivers/accel/thames/thames_gem.c 
> b/drivers/accel/thames/thames_gem.c
> index e1f36a0daf3b8..fd1ee8429e0c9 100644
> --- a/drivers/accel/thames/thames_gem.c
> +++ b/drivers/accel/thames/thames_gem.c

[ ... ]

> @@ -489,13 +677,166 @@ void thames_context_destroy(struct thames_file_priv 
> *priv)
>               priv->context_id, priv->asid);
>  }
>  

[ ... ]

> +static int thames_bo_bind(struct thames_file_priv *priv, struct 
> thames_gem_object *bo,
> +                       u64 dsp_va)
> +{
> +     size_t size = bo->base.base.size;
> +     int prot = thames_bo_flags_to_prot(bo->flags);
> +     struct thames_binding *binding;
> +     int ret;
> +
> +     if (!IS_ALIGNED(dsp_va, PAGE_SIZE))
> +             return -EINVAL;
> +
> +     if (dsp_va < THAMES_BIND_VA_START || dsp_va > THAMES_BIND_VA_END ||
> +         size > THAMES_BIND_VA_END - dsp_va)
> +             return -EINVAL;
> +
> +     binding = kzalloc_obj(*binding);
> +     if (!binding)
> +             return -ENOMEM;
> +
> +     binding->bo = bo;
> +     binding->priv = priv;
> +     binding->mm.start = dsp_va;
> +     binding->mm.size = size;
> +
> +     mutex_lock(&priv->tdev->bindings_lock);
> +
> +     if (thames_bo_find_binding(bo, priv)) {
> +             ret = -EBUSY;
> +             goto err_unlock;
> +     }
> +
> +     scoped_guard(mutex, &priv->mm_lock)
> +             ret = drm_mm_reserve_node(&priv->mm, &binding->mm);
> +     if (ret)
> +             goto err_unlock;
> +
> +     if (bo->flags & DRM_THAMES_BO_CONTIGUOUS)
> +             ret = thames_context_map(priv, dsp_va, prot, bo->base.dma_addr, 
> size);
> +     else
> +             ret = thames_context_map_sgt(priv, dsp_va, prot, bo->shmem.sgt);
> +     if (ret) {
> +             scoped_guard(mutex, &priv->mm_lock)
> +                     drm_mm_remove_node(&binding->mm);
> +             goto err_unlock;
> +     }
> +
> +     drm_gem_object_get(&bo->base.base);
> +     list_add_tail(&binding->bo_link, &bo->bindings);
> +     list_add_tail(&binding->context_link, &priv->bindings);
> +     mutex_unlock(&priv->tdev->bindings_lock);
> +
> +     return 0;
> +
> +err_unlock:
> +     mutex_unlock(&priv->tdev->bindings_lock);
> +     kfree(binding);
> +     return ret;
> +}

[ ... ]

> +static void thames_gem_close(struct drm_gem_object *obj, struct drm_file 
> *file)
> +{
> +     struct thames_file_priv *priv = file->driver_priv;
> +     struct thames_binding *binding;
> +     LIST_HEAD(put);
> +
> +     if (thames_file_has_other_handle(file, obj))
> +             return;
> +
> +     scoped_guard(mutex, &priv->tdev->bindings_lock) {
> +             binding = thames_bo_find_binding(to_thames_bo(obj), priv);
> +             if (binding)
> +                     thames_binding_remove(binding, &put);
> +     }
> +
> +     thames_bindings_put(&put);
> +}

[ ... ]

> @@ -503,3 +844,131 @@ static const struct drm_gem_object_funcs 
> thames_gem_funcs = {
>       .vm_ops = &drm_gem_dma_vm_ops,
>  };
>  

[ ... ]

> +int thames_ioctl_bo_create(struct drm_device *ddev, void *data, struct 
> drm_file *file)
> +{
> +     struct drm_thames_bo_create *args = data;
> +     struct thames_gem_object *bo;
> +     struct drm_gem_dma_object *mem;
> +     int ret;
> +
> +     if (args->handle)
> +             return -EINVAL;
> +
> +     if (!args->size)
> +             return -EINVAL;
> +
> +     if (args->flags & ~(DRM_THAMES_BO_READ_ONLY | DRM_THAMES_BO_EXECUTABLE |
> +                         DRM_THAMES_BO_CONTIGUOUS))
> +             return -EINVAL;
> +
> +     if (args->flags & DRM_THAMES_BO_CONTIGUOUS) {
> +             mem = drm_gem_dma_create(ddev, args->size);

[Severity: Medium]
Can an oversized args->size overflow PAGE_ALIGN() or trigger uncleaned
object destruction?

When args->size > SIZE_MAX - PAGE_SIZE + 1, PAGE_ALIGN(size) in
thames_gem_create_shmem() and round_up(size, PAGE_SIZE) in
drm_gem_dma_create() wrap to 0, creating a 0-byte shmem file and failing
later in drm_vma_offset_add() with -ENOSPC instead of -EINVAL.

For args->size in [0x8000000000000000, 0xfffffffffffff000] with
DRM_THAMES_BO_CONTIGUOUS set, __drm_gem_dma_create() calls
drm_gem_object_init(), which initializes gem_obj->gpuva.lock and
gem_obj->_resv before shmem_file_setup() rejects the negative loff_t with
-EINVAL, and __drm_gem_dma_create() then frees dma_obj with kfree() without
destroying the initialized mutex and dma_resv.  On 32-bit COMPILE_TEST
builds, passing u64 args->size to size_t also truncates the upper 32 bits.

> +             if (IS_ERR(mem)) {
> +                     dev_err_ratelimited(ddev->dev,
> +                                         "Failed to allocate a contiguous 
> buffer of %llu bytes, the CMA pool may be too small (see the cma= kernel 
> parameter)\n",
> +                                         args->size);
> +                     return PTR_ERR(mem);
> +             }
> +             bo = to_thames_bo(&mem->base);
> +     } else {
> +             bo = thames_gem_create_shmem(ddev, args->size);
> +             if (IS_ERR(bo))
> +                     return PTR_ERR(bo);
> +     }

[ ... ]

> +static int thames_bind_exec_op(struct drm_file *file, const struct 
> drm_thames_bind_op *op)
> +{
> +     struct thames_file_priv *priv = file->driver_priv;
> +     struct drm_gem_object *obj;
> +     int ret;
> +
> +     if (op->flags & ~DRM_THAMES_BIND_OP_UNBIND)
> +             return -EINVAL;
> +
> +     if ((op->flags & DRM_THAMES_BIND_OP_UNBIND) && op->dsp_va)
> +             return -EINVAL;
> +
> +     obj = drm_gem_object_lookup(file, op->handle);
> +     if (!obj)
> +             return -ENOENT;
> +
> +     if (op->flags & DRM_THAMES_BIND_OP_UNBIND)
> +             ret = thames_bo_unbind(priv, to_thames_bo(obj));
> +     else
> +             ret = thames_bo_bind(priv, to_thames_bo(obj), op->dsp_va);

[Severity: Medium]
Can a concurrent DRM_IOCTL_GEM_CLOSE race with thames_bo_bind() and leave
a buffer permanently bound until the file is closed?

CPU0                                  CPU1
thames_bind_exec_op()
  obj = drm_gem_object_lookup(...)
                                      drm_gem_handle_delete()
                                        /* removes handle from object_idr */
                                        thames_gem_close()
                                          mutex_lock(&tdev->bindings_lock)
                                          thames_bo_find_binding() -> NULL
                                          mutex_unlock(&tdev->bindings_lock)
  thames_bo_bind()
    mutex_lock(&tdev->bindings_lock)
    drm_gem_object_get(&bo->base.base)
    list_add_tail(..., &priv->bindings)  <-- bound with no handle left

Because the file no longer holds a GEM handle to the buffer and
DRM_THAMES_BIND_OP_UNBIND looks up buffers by handle, the client cannot
unbind it (and re-importing the same buffer cannot bind it at a new
address due to -EBUSY), keeping the DSP VA range reserved and the GEM
object pinned until the file is closed.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=6

Reply via email to