[PATCH] media: raspberrypi: cfe: guard against empty dma_queue when scheduling

From: Haoran Wang

Date: Thu Dec 03 2026 - 06:43:01 EST


cfe_schedule_next_csi2_job() and cfe_schedule_next_pisp_job() call
list_first_entry() on node->dma_queue without checking that the queue
is non-empty. Both run only when cfe->job_ready is true, which
cfe_check_job_ready() establishes by requiring every enabled node to
have a non-empty queue, so the invariant currently holds but there is
no guard at the scheduling site. If it is ever broken, list_first_entry()
returns a pointer into struct cfe_node instead of a struct cfe_buffer,
list_del() corrupts the empty list head and the CSI2 path programs a
garbage DMA address into the hardware.

Use list_first_entry_or_null() and skip the node, or abort the FE
submit for the PISP path, if the queue is unexpectedly empty. A broken
invariant then yields a WARN_ON() instead of wild dereferences, list
corruption and a bogus DMA address.

This was found by static analysis and the trigger is theoretical, so
the change is a defensive guard at the scheduling site only.

Fixes: 6edb685abb2a ("media: raspberrypi: Add support for RP1-CFE")
Signed-off-by: Haoran Wang <haoranwangsec@xxxxxxxxx>
---
drivers/media/platform/raspberrypi/rp1-cfe/cfe.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/media/platform/raspberrypi/rp1-cfe/cfe.c b/drivers/media/platform/raspberrypi/rp1-cfe/cfe.c
index 8375ed3e97b9..9d0ac4262bb4 100644
--- a/drivers/media/platform/raspberrypi/rp1-cfe/cfe.c
+++ b/drivers/media/platform/raspberrypi/rp1-cfe/cfe.c
@@ -532,8 +532,10 @@ static void cfe_schedule_next_csi2_job(struct cfe_device *cfe)
if (!check_state(cfe, NODE_STREAMING, i))
continue;

- buf = list_first_entry(&node->dma_queue, struct cfe_buffer,
- list);
+ buf = list_first_entry_or_null(&node->dma_queue,
+ struct cfe_buffer, list);
+ if (WARN_ON(!buf))
+ continue;
node->next_frm = buf;
list_del(&buf->list);

@@ -565,8 +567,10 @@ static void cfe_schedule_next_pisp_job(struct cfe_device *cfe)
if (!check_state(cfe, NODE_STREAMING, i))
continue;

- buf = list_first_entry(&node->dma_queue, struct cfe_buffer,
- list);
+ buf = list_first_entry_or_null(&node->dma_queue,
+ struct cfe_buffer, list);
+ if (WARN_ON(!buf))
+ return;

trace_cfe_fe_schedule(node->id, &buf->vb.vb2_buf);