Bug: Tegra: VI: CPU busy loops waiting for a DMA write to a full queue

Attached below is pr #56 of the downstream seeed Tegra kernel, this issue also exists in the upstream Nvidia Tegra kernel here, drivers/media/platform/tegra/camera/vi/vi5_fops.c · l4t/l4t-r39.2 · nvidia / nv-tegra / linux-nv-oot · GitLab, on line 740s static int tegra_channel_kthread_capture_enqueue(void *data

Problem

Cases where the Linux pending list is non-empty chan->capture while chan->capture_reqs_enqueued >= chan->capture_queue_depth * chan->valid_ports or simply, capture_reqs_enqueued >= capture_queue_depth * valid_ports would cause the CPU to busy loop until a buffer is written to via DMA and the dequeue worker decrements capture_reqs_enqueued

This would happen because a non-empty list will cause wait_event_interruptible() to return by satisfying !list_empty and move onto

if ((chan->capture_state == CAPTURE_ERROR)
		|| !(chan->capture_reqs_enqueued
		< (chan->capture_queue_depth * chan->valid_ports))) {

where the capacity check will evaluate chan->capture_reqs_enqueued < (chan->capture_queue_depth * chan->valid_ports) to false and trigger a break. This will prompt the program to re-enter the while loop and repeat these checks until a frame has been written to a buffer over DMA, in which case a pending buffer is submitted to VI/RCE.

Similarly, the same thing would happen for capture errors.

Fix

Include capture state and queue capacity in the outer wait condition, allowing the thread to sleep while the queue is full or the channel is in CAPTURE_ERROR.

Below is the diff the pr offers.

diff --git a/source/nvidia-oot/drivers/media/platform/tegra/camera/vi/vi5_fops.c b/source/nvidia-oot/drivers/media/platform/tegra/camera/vi/vi5_fops.c
index e040adb2d0..c733fbc394 100644
--- a/source/nvidia-oot/drivers/media/platform/tegra/camera/vi/vi5_fops.c
+++ b/source/nvidia-oot/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)

Thank you!

Thanks for sharing this patch.

Thanks