[PATCH 2/3] media: cxd2880-spi: keep the TS thread alive until kthread_stop()
From: Enrico Lorenzoni
Date: Wed Nov 18 2026 - 17:08:05 EST
cxd2880_ts_read() is started with kthread_run() when the first feed is
started, and cxd2880_stop_feed() calls kthread_stop() on it when the last
feed goes away. The thread, however, returns as soon as an SPI command
fails, both for the initial TS buffer clear and for the buffer info read
in the main loop.
Returning from the thread function ends the task just as kthread_exit()
does, and the task can be freed before cxd2880_stop_feed() runs; the
kthread_stop() call then uses a task_struct that may no longer exist.
The kthread_stop() documentation asks the caller to make sure the
task_struct cannot go away if the thread may exit by itself, and this
driver does not hold a reference to it.
Keep the thread in its loop until kthread_should_stop() is true: report
SPI errors with a rate-limited message, wait for the same 10 ms that the
loop uses when no data is ready, and retry.
A change with the same loop behaviour (plus extra locking and counters
not included here) has been running in an out-of-tree build of this
driver on Raspberry Pi systems with TV HATs for several weeks; a 60 s
full-mux stream (19.5 Mbit/s) with concurrent register reads showed no
continuity errors. I do not have a reproducer for the use-after-free
itself, which needs an SPI error at the right time; this fix is from code
inspection.
The change and this changelog were prepared with the help of an AI coding
assistant (Anthropic Claude). I have reviewed them.
Fixes: bd24fcddf6b8 ("media: cxd2880-spi: Add support for CXD2880 SPI interface")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Enrico Lorenzoni <enricolorenzoni59@xxxxxxxxx>
---
drivers/media/spi/cxd2880-spi.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/drivers/media/spi/cxd2880-spi.c b/drivers/media/spi/cxd2880-spi.c
index 36207940b..8e31771ac 100644
--- a/drivers/media/spi/cxd2880-spi.c
+++ b/drivers/media/spi/cxd2880-spi.c
@@ -278,18 +278,17 @@ static int cxd2880_ts_read(void *arg)
}
ret = cxd2880_spi_clear_ts_buffer(dvb_spi->spi);
- if (ret) {
+ if (ret)
pr_err("set_clear_ts_buffer failed\n");
- return ret;
- }
start = ktime_get();
while (!kthread_should_stop()) {
ret = cxd2880_spi_read_ts_buffer_info(dvb_spi->spi,
&info);
if (ret) {
- pr_err("spi_read_ts_buffer_info error\n");
- return ret;
+ pr_err_ratelimited("spi_read_ts_buffer_info error\n");
+ usleep_range(10000, 11000);
+ continue;
}
if (info.pkt_num > MAX_TRANS_PKT) {
--
2.47.3