Media: tegra: vi5 — capture enqueue kthread busy-spins a CPU core while the capture queue is full

Hi,

I’d like to report a CPU busy-loop bug in the Tegra VI (vi5) driver. I hit this on an Orin-based platform running JetPack 6.2 / L4T R36.4.3, and confirmed the same code is unchanged in the linux-nv-oot l4t-r39.2 branch:

drivers/media/platform/tegra/camera/vi/vi5_fops.c

Problem

tegra_channel_kthread_capture_enqueue() busy-spins at 100% of one CPU core whenever the pending list is non-empty while the hardware queue is already full — i.e. capture_reqs_enqueued >= capture_queue_depth * valid_ports — or while the channel is in CAPTURE_ERROR.

The outer wait only checks whether the pending list is non-empty:

wait_event_interruptible(chan->start_wait,
		(kthread_should_stop() || !list_empty(&chan->capture)));

Once the list is non-empty this condition is always true, so wait_event_interruptible() never sleeps. The inner loop then re-checks capacity under the lock and breaks:

while (!(kthread_should_stop() || list_empty(&chan->capture))) {
	spin_lock_irqsave(&chan->capture_state_lock, flags);
	if ((chan->capture_state == CAPTURE_ERROR)
			|| !(chan->capture_reqs_enqueued
			< (chan->capture_queue_depth * chan->valid_ports))) {
		spin_unlock_irqrestore(&chan->capture_state_lock, flags);
		break;
	}
	spin_unlock_irqrestore(&chan->capture_state_lock, flags);

The break jumps straight back to the outer wait, whose condition is still satisfied, so the thread spins without sleeping until a buffer completes DMA and the dequeue worker decrements capture_reqs_enqueued. The same happens during capture errors: the enqueue thread spins for the whole CAPTURE_ERROR window.

Normally this state is unreachable: with a standard REQBUFS(N) setup, capture_queue_depth equals the number of vb2 buffers, so a full hardware queue implies an empty pending list (we verified both kthreads sit at 0% during healthy streaming — which is likely why this has gone unnoticed). The state is reached in two real ways:

  1. Any capture error (CSI signal integrity, sensor stall, request timeout): during the whole CAPTURE_ERROR window the enqueue thread spins while the application keeps re-queueing buffers.
  2. Deterministically, purely from userspace: vi5_channel_setup_queue() overwrites capture_queue_depth on every queue_setup() call, and vb2’s CREATE_BUFS re-invokes queue_setup() with the incremental count — so REQBUFS(4) + CREATE_BUFS(2) leaves depth=2 with 6 queued buffers. The hardware queue is then permanently full with 4 buffers stuck on the pending list.

Reproduction

Deterministic trigger (no error injection needed), on R39.2.0 with an Orin NX + IMX219 (any Tegra camera video node):

  1. Open /dev/video0 directly via V4L2 (plain C program; nvarguscamerasrc does NOT work — see scope note below).

  2. REQBUFS(4, MMAP) + CREATE_BUFS(2, MMAP), mmap all 6 buffers, QBUF all 6, STREAMON.

  3. Run a plain DQBUF/QBUF loop and watch the kthreads:

    ps -eLo tid,pcpu,comm | grep vi-output

Happy to attach the ~100-line repro program if useful.

Scope note: on R39, nvargus-daemon (gst nvarguscamerasrc, DeepStream) uses the RCE capture path and never starts these per-channel kthreads, so only applications that open /dev/video* directly via V4L2 (OpenCV CAP_V4L2, custom capture code) are affected.

Proposed fix

Fold the queue-capacity and error-state checks into the outer wait condition so the thread actually sleeps when it cannot make progress. The wakeup added in vi5_channel_error_recover() is required: without it, a thread that went to sleep on the stricter wait condition during CAPTURE_ERROR would never be woken once the channel returns to CAPTURE_IDLE.

Full patch:

--- a/drivers/media/platform/tegra/camera/vi/vi5_fops.c
+++ b/drivers/media/platform/tegra/camera/vi/vi5_fops.c
@@ -779,35 +779,44 @@ static int vi5_channel_error_recover(struct tegra_channel *chan,
 	spin_lock_irqsave(&chan->capture_state_lock, flags);
 	chan->capture_state = CAPTURE_IDLE;
 	spin_unlock_irqrestore(&chan->capture_state_lock, flags);
+	wake_up_interruptible(&chan->start_wait);

 done:
 	return err;
 }

+static bool tegra_channel_capture_enqueue_ready(struct tegra_channel *chan)
+{
+	unsigned long flags;
+	bool ready;
+
+	spin_lock_irqsave(&chan->capture_state_lock, flags);
+	ready = chan->capture_state != CAPTURE_ERROR &&
+		chan->capture_reqs_enqueued <
+			(chan->capture_queue_depth * chan->valid_ports);
+	spin_unlock_irqrestore(&chan->capture_state_lock, flags);
+
+	return ready;
+}
+
 static int tegra_channel_kthread_capture_enqueue(void *data)
 {
 	struct tegra_channel *chan = data;
 	struct tegra_channel_buffer *buf;
-	unsigned long flags;
+
 	set_freezable();

 	while (1) {
 		try_to_freeze();

 		wait_event_interruptible(chan->start_wait,
-			(kthread_should_stop() || !list_empty(&chan->capture)));
+			kthread_should_stop() ||
+			(!list_empty(&chan->capture) &&
+			 tegra_channel_capture_enqueue_ready(chan)));

 		while (!(kthread_should_stop() || list_empty(&chan->capture))) {
-			spin_lock_irqsave(&chan->capture_state_lock, flags);
-			if ((chan->capture_state == CAPTURE_ERROR)
-					|| !(chan->capture_reqs_enqueued
-					< (chan->capture_queue_depth * chan->valid_ports))) {
-				spin_unlock_irqrestore(
-					&chan->capture_state_lock, flags);
+			if (!tegra_channel_capture_enqueue_ready(chan))
 				break;
-			}
-			spin_unlock_irqrestore(&chan->capture_state_lock,
-				flags);

 			buf = dequeue_buffer(chan, false);
 			if (!buf)

Verification

A/B tested on target (R39.2.0, Orin NX + IMX219, stock tegra-camera.ko vs. patched):

          enqueue kthread   dequeue kthread   capture

unpatched ~100% ~0% normal, ~17-20 fps
patched 0.0% 0.0% normal (no regression)

The kthread CPU stays at 0.0% across repeated samples during a 20 s streaming session, and error recovery is unaffected. Note the bug itself is completely silent — no frame drops, no kernel messages — only a wasted core (CPU load / power / thermal).

Could you confirm whether this is a known issue, and if the proposed fix looks correct? Happy to provide the repro program and any additional logs if needed.

Thanks!

Thanks for the sharing.

Suppose it’s the same with Bug: Tegra: VI: CPU busy loops waiting for a DMA write to a full queue

And we are making the change to review to merge to code base.

Thanks