[PATCH v2] platform/raspberrypi: vchiq: Stop kthreads on vchiq_platform_init() failure
From: Tomasz Unger
Date: Sat Nov 14 2026 - 09:05:03 EST
vchiq_init_state() creates and wakes the slot handler, recycle and sync
kthreads. If a later step in vchiq_platform_init() fails, the function
returns without stopping them and vchiq_probe() only propagates the
error. devres then frees what probe allocated, including the driver
data and the slot memory the kthreads operate on.
Forcing the devm_request_irq() error path on a Raspberry Pi 4 Model B
makes all three kthreads fault right after the failed probe:
bcm2835_vchiq fe00b840.mailbox: failed to register irq=34
bcm2835_vchiq fe00b840.mailbox: arm: Could not initialize vchiq platform
Unable to handle kernel paging request at virtual address ffff800080ba402c
Internal error: Oops: 0000000096000007 [#1] SMP
CPU: 1 UID: 0 PID: 370 Comm: vchiq-sync/0 Not tainted 7.4.0-rc1-trace #1
pc : sync_func+0x44/0x3a8 [vchiq]
Call trace:
sync_func+0x44/0x3a8 [vchiq] (P)
kthread+0x128/0x150
ret_from_fork+0x10/0x20
If the kthreads are asleep at that point they are left behind instead,
three per failed probe, still pointing at the freed memory.
Map the registers and look up the interrupt before vchiq_init_state().
Neither depends on the VCHIQ state, so these two failures need no
unwinding. On the remaining failures stop the kthreads with
vchiq_platform_uninit(), as is already done when registering the
character device fails. Add a forward declaration of
vchiq_platform_uninit() for that.
The missing unwind is older than the commit named below, but the
kthreads could not be stopped before it, so it is the earliest point
this fix applies to.
The bug was found and the fix drafted with the help of an AI coding
assistant (Claude).
Fixes: 863a756aaf49 ("staging: vc04_services: vchiq_core: Stop kthreads on vchiq module unload")
Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Tomasz Unger <tomasz.unger@xxxxxxxx>
---
Verified on a Raspberry Pi 4 Model B with a test-only one-line change
that forces the devm_request_irq() error path. Without this patch the
kthreads either oops as shown above or, as in the test run for v2, are
left behind: 12 of them after four failed probe attempts. With it probe
fails cleanly, no vchiq kthreads remain after four failed attempts and
the module unloads. Without the forced error the driver probes normally
and audio playback over VCHIQ works.
---
Changes in v2 (per Stefan Wahren's review):
- Use a forward declaration of vchiq_platform_uninit() instead of
moving it and vchiq_platform_get_arm_state() above
vchiq_platform_init(). vchiq_platform_get_arm_state() needs no
declaration, all its callers are below its definition.
- Link to v1: https://patch.msgid.link/20261114-vchiq-probe-kthread-leak-v1-1-b6b2f1e32050@xxxxxxxx
---
.../raspberrypi/vchiq-interface/vchiq_arm.c | 21 ++++++++++++++-------
1 file changed, 14 insertions(+), 7 deletions(-)
diff --git a/drivers/platform/raspberrypi/vchiq-interface/vchiq_arm.c b/drivers/platform/raspberrypi/vchiq-interface/vchiq_arm.c
index e78641703252..3e5c10039b4e 100644
--- a/drivers/platform/raspberrypi/vchiq-interface/vchiq_arm.c
+++ b/drivers/platform/raspberrypi/vchiq-interface/vchiq_arm.c
@@ -187,6 +187,8 @@ void vchiq_add_connected_callback(struct vchiq_device *device, void (*callback)(
}
EXPORT_SYMBOL(vchiq_add_connected_callback);
+static void vchiq_platform_uninit(struct vchiq_drv_mgmt *mgmt);
+
static int vchiq_platform_init(struct platform_device *pdev, struct vchiq_state *state)
{
struct device *dev = &pdev->dev;
@@ -243,10 +245,6 @@ static int vchiq_platform_init(struct platform_device *pdev, struct vchiq_state
sema_init(&drv_mgmt->free_fragments_sema, MAX_FRAGMENTS);
sema_init(&drv_mgmt->free_fragments_mutex, 1);
- err = vchiq_init_state(state, vchiq_slot_zero, dev);
- if (err)
- return err;
-
drv_mgmt->regs = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(drv_mgmt->regs))
return PTR_ERR(drv_mgmt->regs);
@@ -255,11 +253,15 @@ static int vchiq_platform_init(struct platform_device *pdev, struct vchiq_state
if (irq <= 0)
return irq;
+ err = vchiq_init_state(state, vchiq_slot_zero, dev);
+ if (err)
+ return err;
+
err = devm_request_irq(dev, irq, vchiq_doorbell_irq, IRQF_IRQPOLL,
"VCHIQ doorbell", state);
if (err) {
dev_err(dev, "failed to register irq=%d\n", irq);
- return err;
+ goto err_stop_threads;
}
/* Send the base address of the slots to VideoCore */
@@ -268,13 +270,14 @@ static int vchiq_platform_init(struct platform_device *pdev, struct vchiq_state
&channelbase, sizeof(channelbase));
if (err) {
dev_err(dev, "failed to send firmware property: %d\n", err);
- return err;
+ goto err_stop_threads;
}
if (channelbase) {
dev_err(dev, "failed to set channelbase (response: %x)\n",
channelbase);
- return -ENXIO;
+ err = -ENXIO;
+ goto err_stop_threads;
}
dev_dbg(&pdev->dev, "arm: vchiq_init - done (slots %p, phys %pad)\n",
@@ -284,6 +287,10 @@ static int vchiq_platform_init(struct platform_device *pdev, struct vchiq_state
vchiq_call_connected_callbacks(drv_mgmt);
return 0;
+
+err_stop_threads:
+ vchiq_platform_uninit(drv_mgmt);
+ return err;
}
int
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261114-vchiq-probe-kthread-leak-8f6178695a33
Best regards,
--
Tomasz Unger <tomasz.unger@xxxxxxxx>