Re: [PATCH v4 13/25] accel/qda: Add the invocation ioctl and teardown on unplug

From: Ekansh Gupta

Date: Mon Nov 30 2026 - 04:21:38 EST


On 02-11-2026 03:51, Ekansh Gupta wrote:
> Tie the invocation pieces together behind an ioctl: allocate an
> invocation, take its arguments from user space, build the payload, send
> it to the DSP, wait, and act on the answer.
>
> The wait is interruptible, so a DSP that never answers does not leave
> the caller unkillable. An interrupted caller drops its own reference and
> returns, but the invocation lives on, because the DSP still owns the
> payload buffer and may write into it. It is freed once the DSP answers
> or the transport goes away.
>
> When the remote processor goes away, invocations may be waiting for
> answers that will never come. Those waiters are failed with -EPIPE
> and woken so they can release the buffers they hold. The flush runs
> after the device is unplugged, so the response handler is drained and
> the flush is the only thing completing invocations.
>
> The flush drops the reference the send path took for the response. An
> invocation that has not been sent yet, or whose response has already
> been handled, does not hold that reference, so the flush can drop one
> that is not its own. Both windows are narrow but open exactly when the
> DSP crashes.
>
> Signed-off-by: Ekansh Gupta <ekansh.gupta@xxxxxxxxxxxxxxxx>
> ---
> drivers/accel/qda/qda_drv.c | 1 +
> drivers/accel/qda/qda_fastrpc.c | 67 +++++++++++++++++++++++++++++++++++++++++
> drivers/accel/qda/qda_fastrpc.h | 2 ++
> drivers/accel/qda/qda_ioctl.c | 36 ++++++++++++++++++++++
> drivers/accel/qda/qda_ioctl.h | 1 +
> drivers/accel/qda/qda_rpmsg.c | 1 +
> include/uapi/drm/qda_accel.h | 3 ++
> 7 files changed, 111 insertions(+)
>
> diff --git a/drivers/accel/qda/qda_drv.c b/drivers/accel/qda/qda_drv.c
> index 99bd37fca438..7d44342d3f67 100644
> --- a/drivers/accel/qda/qda_drv.c
> +++ b/drivers/accel/qda/qda_drv.c
> @@ -110,6 +110,7 @@ static const struct drm_ioctl_desc qda_ioctls[] = {
> DRM_IOCTL_DEF_DRV(QDA_QUERY, qda_ioctl_query, 0),
> DRM_IOCTL_DEF_DRV(QDA_GEM_CREATE, qda_ioctl_gem_create, 0),
> DRM_IOCTL_DEF_DRV(QDA_GEM_MMAP_OFFSET, qda_ioctl_gem_mmap_offset, 0),
> + DRM_IOCTL_DEF_DRV(QDA_REMOTE_INVOKE, qda_ioctl_invoke, 0),
> };
>
> static const struct drm_driver qda_drm_driver = {
> diff --git a/drivers/accel/qda/qda_fastrpc.c b/drivers/accel/qda/qda_fastrpc.c
> index 2971f2f9461f..3a1959e2c53e 100644
> --- a/drivers/accel/qda/qda_fastrpc.c
> +++ b/drivers/accel/qda/qda_fastrpc.c
> @@ -10,6 +10,7 @@
> #include "qda_drv.h"
> #include "qda_fastrpc.h"
> #include "qda_gem.h"
> +#include "qda_rpmsg.h"
>
> /*
> * The caller owns the returned reference and must drop it with
> @@ -484,6 +485,72 @@ int qda_fastrpc_invoke_pack(struct qda_fastrpc_invoke_ctx *ctx, struct qda_msg *
> return 0;
> }
>
> +/**
> + * qda_fastrpc_flush_pending() - Fail every invocation still in flight
> + * @qdev: Device whose invocations should be failed
> + *
> + * The DSP has gone away and will never answer, so stand in for the response
> + * handler: wake each waiter with an error and drop the reference the send
> + * path took for that handler. Each waiter then tears its invocation down,
> + * releasing the buffers it holds.
> + *
> + * Must run after drm_dev_unplug() has drained the response handler, so this
> + * is the only path completing an invocation.
> + */
> +void qda_fastrpc_flush_pending(struct qda_dev *qdev)
> +{
> + struct qda_fastrpc_invoke_ctx *ctx;
> + unsigned long idx;
> +
> + xa_for_each(&qdev->ctx_xa, idx, ctx) {
> + ctx->retval = -EPIPE;
> + complete(&ctx->work);
> + kref_put(&ctx->refcount, qda_fastrpc_context_free);
> + }
> +}
Our internal security review flagged a lifetime problem with
qda_fastrpc_flush_pending().

qda_fastrpc_context_alloc() inserts the ctx into ctx_xa with refcount 1,
owned by the caller, before any message is sent. The extra reference for
the response handler is only taken later in qda_rpmsg_send_msg().
qda_fastrpc_flush_pending() walks ctx_xa and does an unconditional
kref_put() on every entry. If it runs while an invocation is still being
prepared, after a failed send, or after qda_rpmsg_cb() has queued
put_work, it drops a reference nobody took for it. That frees a ctx
still in use by the ioctl (UAF) and underflows or double-puts later.

xa_for_each() here, and xa_load() in qda_rpmsg_cb(), also use the ctx
without a reference or lock, so a concurrent free is not excluded.

The trigger is any rpmsg removal (DSP crash, SSR, remoteproc stop or
unbind) racing an in-flight ioctl.

I plan to fix this in v5.

> +
> +/**
> + * qda_fastrpc_invoke_common() - Shared tail of a FastRPC invocation
> + * @qdev: Device to invoke on
> + * @ctx: Invocation whose arguments are already prepared
> + *
> + * Packs the invocation into a message, sends it, waits for the DSP, and acts
> + * on the answer. Common to the accel UAPI and the legacy shim, once each has
> + * prepared ctx->args and ctx->gem_objs by its own means.
> + *
> + * Return: 0 on success, negative error code on failure
> + */
> +int qda_fastrpc_invoke_common(struct qda_dev *qdev, struct qda_fastrpc_invoke_ctx *ctx)
> +{
> + struct qda_msg msg = {};
> + int err;
> +
> + err = qda_fastrpc_invoke_pack(ctx, &msg);
> + if (err)
> + return err;
> +
> + err = qda_rpmsg_send_msg(qdev, &msg);
> + if (err)
> + return err;
> +
> + err = qda_rpmsg_wait_for_rsp(ctx);
> + if (err)
> + return err;
> +
> + /* Ensure the results written by the DSP are visible before reading */
> + dma_rmb();
> +
> + err = ctx->retval;
> + if (err) {
> + drm_dbg_driver(&qdev->drm_dev, "DSP returned status %d\n", ctx->retval);
> + return err;
> + }
> +
> + qda_fastrpc_cleanup_handlelist(ctx);
> +
> + return 0;
> +}
> +
> /*
> * INVOKE_DYNAMIC: the argument descriptors are supplied by user space, which
> * is also responsible for having imported every buffer to a GEM handle.
> diff --git a/drivers/accel/qda/qda_fastrpc.h b/drivers/accel/qda/qda_fastrpc.h
> index 8440efc21b77..61f80c64e6d2 100644
> --- a/drivers/accel/qda/qda_fastrpc.h
> +++ b/drivers/accel/qda/qda_fastrpc.h
> @@ -205,5 +205,7 @@ void qda_session_msg_buf_pool_destroy(struct qda_session *session);
> int qda_fastrpc_prepare_args(struct qda_fastrpc_invoke_ctx *ctx,
> struct qda_session *session, void *argp);
> int qda_fastrpc_invoke_pack(struct qda_fastrpc_invoke_ctx *ctx, struct qda_msg *msg);
> +int qda_fastrpc_invoke_common(struct qda_dev *qdev, struct qda_fastrpc_invoke_ctx *ctx);
> +void qda_fastrpc_flush_pending(struct qda_dev *qdev);
>
> #endif /* __QDA_FASTRPC_H__ */
> diff --git a/drivers/accel/qda/qda_ioctl.c b/drivers/accel/qda/qda_ioctl.c
> index b11743ab791e..e9f4151e344a 100644
> --- a/drivers/accel/qda/qda_ioctl.c
> +++ b/drivers/accel/qda/qda_ioctl.c
> @@ -6,6 +6,7 @@
> #include <drm/drm_gem.h>
> #include <drm/qda_accel.h>
> #include "qda_drv.h"
> +#include "qda_fastrpc.h"
> #include "qda_gem.h"
> #include "qda_ioctl.h"
>
> @@ -73,3 +74,38 @@ int qda_ioctl_gem_mmap_offset(struct drm_device *dev, void *data, struct drm_fil
>
> return drm_gem_dumb_map_offset(file_priv, dev, args->handle, &args->offset);
> }
> +
> +static int qda_fastrpc_invoke(void *data, struct qda_session *session)
> +{
> + struct qda_dev *qdev = session->qda_dev;
> + struct qda_fastrpc_invoke_ctx *ctx;
> + int err;
> +
> + ctx = qda_fastrpc_context_alloc(qdev);
> + if (IS_ERR(ctx))
> + return PTR_ERR(ctx);
> +
> + err = qda_fastrpc_prepare_args(ctx, session, data);
> + if (err)
> + goto out_put;
> +
> + err = qda_fastrpc_invoke_common(qdev, ctx);
> +
> +out_put:
> + kref_put(&ctx->refcount, qda_fastrpc_context_free);
> +
> + return err;
> +}
> +
> +/**
> + * qda_ioctl_invoke() - Run a method on the DSP
> + * @dev: DRM device structure
> + * @data: User-space data (struct drm_qda_invoke_args)
> + * @file_priv: DRM file private data
> + *
> + * Return: 0 on success, negative error code on failure
> + */
> +int qda_ioctl_invoke(struct drm_device *dev, void *data, struct drm_file *file_priv)
> +{
> + return qda_fastrpc_invoke(data, file_priv->driver_priv);
> +}
> diff --git a/drivers/accel/qda/qda_ioctl.h b/drivers/accel/qda/qda_ioctl.h
> index d1cbbfb6d965..3bb9cfd98370 100644
> --- a/drivers/accel/qda/qda_ioctl.h
> +++ b/drivers/accel/qda/qda_ioctl.h
> @@ -11,5 +11,6 @@
> int qda_ioctl_query(struct drm_device *dev, void *data, struct drm_file *file_priv);
> int qda_ioctl_gem_create(struct drm_device *dev, void *data, struct drm_file *file_priv);
> int qda_ioctl_gem_mmap_offset(struct drm_device *dev, void *data, struct drm_file *file_priv);
> +int qda_ioctl_invoke(struct drm_device *dev, void *data, struct drm_file *file_priv);
>
> #endif /* __QDA_IOCTL_H__ */
> diff --git a/drivers/accel/qda/qda_rpmsg.c b/drivers/accel/qda/qda_rpmsg.c
> index 43b56ab3f012..7e58df3eb2e1 100644
> --- a/drivers/accel/qda/qda_rpmsg.c
> +++ b/drivers/accel/qda/qda_rpmsg.c
> @@ -160,6 +160,7 @@ static void qda_rpmsg_remove(struct rpmsg_device *rpdev)
> */
> drm_dev_unplug(&qdev->drm_dev);
> qdev->rpdev = NULL;
> + qda_fastrpc_flush_pending(qdev);
> qda_cb_unpopulate(qdev);
> qda_deinit_device(qdev);
> }
> diff --git a/include/uapi/drm/qda_accel.h b/include/uapi/drm/qda_accel.h
> index 6d81f0ef47b6..c21bc5b2446c 100644
> --- a/include/uapi/drm/qda_accel.h
> +++ b/include/uapi/drm/qda_accel.h
> @@ -15,6 +15,7 @@ extern "C" {
> #define DRM_QDA_QUERY 0x00
> #define DRM_QDA_GEM_CREATE 0x01
> #define DRM_QDA_GEM_MMAP_OFFSET 0x02
> +#define DRM_QDA_REMOTE_INVOKE 0x03
>
> #define DRM_IOCTL_QDA_QUERY DRM_IOWR(DRM_COMMAND_BASE + DRM_QDA_QUERY, \
> struct drm_qda_query)
> @@ -22,6 +23,8 @@ extern "C" {
> struct drm_qda_gem_create)
> #define DRM_IOCTL_QDA_GEM_MMAP_OFFSET DRM_IOWR(DRM_COMMAND_BASE + DRM_QDA_GEM_MMAP_OFFSET, \
> struct drm_qda_gem_mmap_offset)
> +#define DRM_IOCTL_QDA_REMOTE_INVOKE DRM_IOWR(DRM_COMMAND_BASE + DRM_QDA_REMOTE_INVOKE, \
> + struct drm_qda_invoke_args)
>
> /* Query type definitions for drm_qda_query */
> #define QDA_QUERY_DSP_NAME 1
>