Skip to contentSkip to conversation

misc: fastrpc: make put_work scheduling lock-free

PATCH Applied Vinayak Katoch 4 participants 4 messages Nov 27 – Dec 1

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 misc: fastrpc: make put_work scheduling lock-free

fastrpc_context_schedule_put_work() guards the once-only schedule_work() with fl->lock. That helper is called from fastrpc_rpmsg_callback(), which runs in hard-IRQ context on the glink rx interrupt, while every other user of fl->lock takes it with a plain spin_lock() in process context with interrupts enabled.

If the glink interrupt fires on a CPU that is already holding fl->lock, for example fastrpc_context_put_wq() -> fastrpc_context_free() or fastrpc_free_map() running in a kworker on that CPU, the handler spins on the lock the interrupted code holds. The holder can never run again, so the CPU hangs with interrupts masked, an RCU stall is reported for it, and the system is eventually reset by the watchdog. It is easy to hit because schedule_work() queues onto the local CPU's worker pool, so the worker that takes fl->lock tends to run on the very CPU that receives the glink interrupt.

The same helper is also called by fastrpc_notify_users() while it holds user->lock, and ctx->fl == user, so a context left on fl->pending or fl->interrupted when the channel is removed deadlocks on the same lock there too.

The flag is only used to make sure put_work is queued once, so no lock is needed. Turn it into an atomic_t and claim it with atomic_xchg(). Whichever caller sees the old value 0 queues the work; any later or racing caller sees 1 and does nothing. This makes the helper safe from hard-IRQ context and with fl->lock already held.

Fixes 3f645f6884b5 ("misc: fastrpc: fix context leak and hang on signal-interrupted invoke")
---
 drivers/misc/fastrpc.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
drivers/misc/fastrpc.c +7 -11
index 5375847d8c9a..3dd9892b11cc 100644
@@ -279,8 +279,8 @@ struct fastrpc_invoke_ctx {
 	bool is_work_done;
 	/* process updates poll memory instead of glink response */
 	bool is_polled;
-	/* set once put_work has been scheduled for this ctx, guarded by fl->lock */
-	bool put_work_scheduled;
+	/* set once put_work is queued; atomic as it's set from IRQ context */
+	atomic_t put_work_scheduled;
 	struct kref refcount;
 	struct list_head node; /* list of ctxs */
 	struct completion work;
@@ -661,17 +661,13 @@ static void fastrpc_context_put_wq(struct work_struct *work)
 	fastrpc_context_put(ctx);
 }
 
-/* Schedules put_work at most once per ctx; racing callers may both try. */
+/*
+ * Schedules put_work at most once per ctx. Must stay lock-free: it runs from
+ * the glink rx hard-IRQ and from fastrpc_notify_users() with fl->lock held.
+ */
 static void fastrpc_context_schedule_put_work(struct fastrpc_invoke_ctx *ctx)
 {
-	bool do_schedule;
-
-	spin_lock(&ctx->fl->lock);
-	do_schedule = !ctx->put_work_scheduled;
-	ctx->put_work_scheduled = true;
-	spin_unlock(&ctx->fl->lock);
-
-	if (do_schedule)
+	if (!atomic_xchg(&ctx->put_work_scheduled, 1))
 		schedule_work(&ctx->put_work);
 }
 

---
base-commit: a9c48428cbd8d96b26f92631d0db6f8519c5fcc5
change-id: 20261127-fix-put-work-irq-deadlock-940a4e7a166e

Best regards,

Signature · 1 line
Vinayak Katoch <[email protected]>
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Vinayak Katoch

fastrpc_context_schedule_put_work() guards the once-only schedule_work() with fl->lock. That helper is called from fastrpc_rpmsg_callback(), which runs in hard-IRQ context on the glink rx interrupt, while every other user of fl->lock takes it with a plain spin_lock() in process context with interrupts enabled.

If the glink interrupt fires on a CPU that is already holding fl->lock, for example fastrpc_context_put_wq() -> fastrpc_context_free() or fastrpc_free_map() running in a kworker on that CPU, the handler spins on the lock the interrupted code holds. The holder can never run again, so the CPU hangs with interrupts masked, an RCU stall is reported for it, and the system is eventually reset by the watchdog. It is easy to hit because schedule_work() queues onto the local CPU's worker pool, so the worker that takes fl->lock tends to run on the very CPU that receives the glink interrupt.

The same helper is also called by fastrpc_notify_users() while it holds user->lock, and ctx->fl == user, so a context left on fl->pending or fl->interrupted when the channel is removed deadlocks on the same lock there too.

The flag is only used to make sure put_work is queued once, so no lock is needed. Turn it into an atomic_t and claim it with atomic_xchg(). Whichever caller sees the old value 0 queues the work; any later or racing caller sees 1 and does nothing. This makes the helper safe from hard-IRQ context and with fl->lock already held.

Quoted 3 lines
Fixes 3f645f6884b5 ("misc: fastrpc: fix context leak and hang on signal-interrupted invoke")
Tested-by on v1 1/1
#2 lore raw
In reply to Vinayak Katoch
Quoted 27 lines · Vinayak Katoch

fastrpc_context_schedule_put_work() guards the once-only schedule_work() with fl->lock. That helper is called from fastrpc_rpmsg_callback(), which runs in hard-IRQ context on the glink rx interrupt, while every other user of fl->lock takes it with a plain spin_lock() in process context with interrupts enabled.

If the glink interrupt fires on a CPU that is already holding fl->lock, for example fastrpc_context_put_wq() -> fastrpc_context_free() or fastrpc_free_map() running in a kworker on that CPU, the handler spins on the lock the interrupted code holds. The holder can never run again, so the CPU hangs with interrupts masked, an RCU stall is reported for it, and the system is eventually reset by the watchdog. It is easy to hit because schedule_work() queues onto the local CPU's worker pool, so the worker that takes fl->lock tends to run on the very CPU that receives the glink interrupt.

The same helper is also called by fastrpc_notify_users() while it holds user->lock, and ctx->fl == user, so a context left on fl->pending or fl->interrupted when the channel is removed deadlocks on the same lock there too.

The flag is only used to make sure put_work is queued once, so no lock is needed. Turn it into an atomic_t and claim it with atomic_xchg(). Whichever caller sees the old value 0 queues the work; any later or racing caller sees 1 and does nothing. This makes the helper safe from hard-IRQ context and with fl->lock already held.

Quoted 3 lines
Fixes 3f645f6884b5 ("misc: fastrpc: fix context leak and hang on signal-interrupted invoke")

It fixes the regression [1] on Nord I reported yesterday.

Shawn

[1] https://lore.kernel.org/all/awz0TMG1-NdBWqyZ@QCOM-aGQu4IUr3Y/

maintainer Applied
#3 lore raw
In reply to Vinayak Katoch
Quoted 16 lines · Vinayak Katoch

fastrpc_context_schedule_put_work() guards the once-only schedule_work() with fl->lock. That helper is called from fastrpc_rpmsg_callback(), which runs in hard-IRQ context on the glink rx interrupt, while every other user of fl->lock takes it with a plain spin_lock() in process context with interrupts enabled.

If the glink interrupt fires on a CPU that is already holding fl->lock, for example fastrpc_context_put_wq() -> fastrpc_context_free() or fastrpc_free_map() running in a kworker on that CPU, the handler spins on the lock the interrupted code holds. The holder can never run again, so the CPU hangs with interrupts masked, an RCU stall is reported for it, and the system is eventually reset by the watchdog. It is easy to hit because schedule_work() queues onto the local CPU's worker pool, so the worker that takes fl->lock tends to run on the very CPU that receives the glink interrupt.

Quoted 1 line

[...]

Applied, thanks!

[1/1] misc: fastrpc: make put_work scheduling lock-free

      commit: a14e8ddc12f6f7ef91e4a6a7724783d66b7af63a

Best regards,

Signature · 1 line
Srinivas Kandagatla <[email protected]>