Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH 2/2] misc: fastrpc: consume in-flight reference exactly once
@ 2026-10-30  8:20 bo cao
  2026-11-16 13:49 ` Srinivas Kandagatla
  0 siblings, 1 reply; 2+ messages in thread
From: bo cao @ 2026-10-30  8:20 UTC (permalink / raw)
  To: srini; +Cc: linux-kernel, gregkh, stable

The response path is supposed to drop the in-flight reference taken
by fastrpc_invoke_send() exactly once: fastrpc_rpmsg_callback() calls
complete() and schedules fastrpc_context_put_wq(), whose worker drops
the reference.  Nothing enforces this: a second delivery of the same
COMPLETE response (identical ctxid and sequence, so it passes the
sequence validation) re-queues put_work after the first worker has
run and drops the in-flight reference a second time.  The context is
then freed while the invoking thread is still processing the first
completion, and the second fastrpc_context_free() also drops the
fastrpc_user and channel context references a second time.

Track the first delivery with a plain bool checked under cctx->lock,
together with the sequence validation, and drop duplicates.

Fixes: c68cfb718c8f ("misc: fastrpc: Add support for context Invoke method")
Cc: stable@vger.kernel.org
Signed-off-by: bo cao <treebacker@foxmail.com>
---
 drivers/misc/fastrpc.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
index c77048076aee..f56ba5e44e1f 100644
--- a/drivers/misc/fastrpc.c
+++ b/drivers/misc/fastrpc.c
@@ -271,6 +271,8 @@ struct fastrpc_invoke_ctx {
 	bool is_work_done;
 	/* process updates poll memory instead of glink response */
 	bool is_polled;
+	/* first response already consumed the in-flight reference */
+	bool responded;
 	struct kref refcount;
 	struct list_head node; /* list of ctxs */
 	struct completion work;
@@ -2725,6 +2727,7 @@ static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
 	unsigned long flags;
 	unsigned long ctxid;
 	bool stale = false;
+	bool dup = false;
 
 	if (len < sizeof(*rsp))
 		return -EINVAL;
@@ -2749,8 +2752,13 @@ static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
 		ctx = NULL;
 	}
 	if (ctx && FIELD_GET(FASTRPC_CTXID_SEQ_MASK, rsp->ctx) !=
-		   FIELD_GET(FASTRPC_CTXID_SEQ_MASK, ctx->ctxid))
+		   FIELD_GET(FASTRPC_CTXID_SEQ_MASK, ctx->ctxid)) {
 		stale = true;
+	} else if (ctx && ctx->responded) {
+		dup = true;
+	} else if (ctx) {
+		ctx->responded = true;
+	}
 	spin_unlock_irqrestore(&cctx->lock, flags);
 
 	if (!ctx) {
@@ -2773,6 +2781,14 @@ static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
 		return 0;
 	}
 
+	if (dup) {
+		dev_dbg(&rpdev->dev,
+			"Duplicate glink response ctx 0x%llx, dropping\n",
+			rsp->ctx);
+		fastrpc_context_put(ctx);
+		return 0;
+	}
+
 	ctx->retval = rsp->retval;
 	ctx->is_work_done = true;
 	complete(&ctx->work);
-- 
2.39.5 (Apple Git-154)


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH 2/2] misc: fastrpc: consume in-flight reference exactly once
  2026-10-30  8:20 [PATCH 2/2] misc: fastrpc: consume in-flight reference exactly once bo cao
@ 2026-11-16 13:49 ` Srinivas Kandagatla
  0 siblings, 0 replies; 2+ messages in thread
From: Srinivas Kandagatla @ 2026-11-16 13:49 UTC (permalink / raw)
  To: bo cao, srini; +Cc: linux-kernel, gregkh, stable



On 10/30/26 8:20 AM, bo cao wrote:
> The response path is supposed to drop the in-flight reference taken
> by fastrpc_invoke_send() exactly once: fastrpc_rpmsg_callback() calls
> complete() and schedules fastrpc_context_put_wq(), whose worker drops
> the reference.  Nothing enforces this: a second delivery of the same
> COMPLETE response (identical ctxid and sequence, so it passes the

Why are we even getting the response twice for the same ctxid?


The driver expects only one response for a given request.
Is this firmware bug?
Which platform are you testing this?
how can I reproduce this?

> sequence validation) re-queues put_work after the first worker has
> run and drops the in-flight reference a second time.  The context is
> then freed while the invoking thread is still processing the first
> completion, and the second fastrpc_context_free() also drops the
> fastrpc_user and channel context references a second time.
> 
> Track the first delivery with a plain bool checked under cctx->lock,
> together with the sequence validation, and drop duplicates.
> 
> Fixes: c68cfb718c8f ("misc: fastrpc: Add support for context Invoke method")
> Cc: stable@vger.kernel.org
> Signed-off-by: bo cao <treebacker@foxmail.com>

Thankyou for the patch, Please use get_maintainers to get the list of
recipients for this patch, currently you have only send this to few
people making it difficult for others to review and test your patch.


Second this does not apply cleanly.
> ---
>  drivers/misc/fastrpc.c | 18 +++++++++++++++++-
>  1 file changed, 17 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
> index c77048076aee..f56ba5e44e1f 100644
> --- a/drivers/misc/fastrpc.c
> +++ b/drivers/misc/fastrpc.c
> @@ -271,6 +271,8 @@ struct fastrpc_invoke_ctx {
>  	bool is_work_done;
>  	/* process updates poll memory instead of glink response */
>  	bool is_polled;
> +	/* first response already consumed the in-flight reference */
> +	bool responded;
>  	struct kref refcount;
>  	struct list_head node; /* list of ctxs */
>  	struct completion work;
> @@ -2725,6 +2727,7 @@ static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
>  	unsigned long flags;
>  	unsigned long ctxid;
>  	bool stale = false;
> +	bool dup = false;
>  
>  	if (len < sizeof(*rsp))
>  		return -EINVAL;
> @@ -2749,8 +2752,13 @@ static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
>  		ctx = NULL;
>  	}
>  	if (ctx && FIELD_GET(FASTRPC_CTXID_SEQ_MASK, rsp->ctx) !=
> -		   FIELD_GET(FASTRPC_CTXID_SEQ_MASK, ctx->ctxid))
> +		   FIELD_GET(FASTRPC_CTXID_SEQ_MASK, ctx->ctxid)) {
>  		stale = true;
> +	} else if (ctx && ctx->responded) {
> +		dup = true;
> +	} else if (ctx) {
> +		ctx->responded = true;
> +	}
>  	spin_unlock_irqrestore(&cctx->lock, flags);
>  
>  	if (!ctx) {
> @@ -2773,6 +2781,14 @@ static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
>  		return 0;
>  	}
>  
> +	if (dup) {
> +		dev_dbg(&rpdev->dev,
> +			"Duplicate glink response ctx 0x%llx, dropping\n",
> +			rsp->ctx);
> +		fastrpc_context_put(ctx);
this seems to be an extra put

--srini
> +		return 0;
> +	}
> +
>  	ctx->retval = rsp->retval;
>  	ctx->is_work_done = true;
>  	complete(&ctx->work);
 	 	

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-16 13:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-30  8:20 [PATCH 2/2] misc: fastrpc: consume in-flight reference exactly once bo cao
2026-11-16 13:49 ` Srinivas Kandagatla

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox