Skip to contentSkip to conversation

media: aspeed: order probe cleanup and last close

PATCH In review Myeonghun Pak 3 participants 7 messages Nov 10 – Nov 13

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover media: aspeed: order probe cleanup and last close

This revision requests the IRQ after its dependencies and synchronizes IRQ/work before manual probe cleanup. The second patch fixes last-close ordering and keeps the engine available until VB2 release completes.

Compile-tested on ARM64 with LLVM, W=1.
No hardware testing.

Changes in v2:
- Rebase onto managed reserved-memory initialization.
- Remove the research sentence and Assisted-by tag.
- Join the IRQ and delayed work before manual probe-failure cleanup.
- Keep VB2 release before normal last-close hardware shutdown.
- Drain the IRQ producer before cancelling resolution work.
- Balance IRQ disable/enable within stop and check STOPPED before power-on.

Myeonghun Pak (2):
media: aspeed: join IRQ and work before probe cleanup
media: aspeed: drain IRQ work before stopping the engine

 drivers/media/platform/aspeed/aspeed-video.c | 38 +++++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 13 deletions(-)
#1 lore raw

PATCH v2 1/2 media: aspeed: join IRQ and work before probe cleanup

The setup failure path unprepares the clocks before devres frees the IRQ. An IRQ handler that has sampled an enabled status can still queue resolution work. Masking the source does not synchronize that handler.

Save the IRQ number after a successful request, then free the action and drain resolution work before the manual JPEG and clock cleanup. Turn the engine off after the work has finished. Request the IRQ after reset, clock and JPEG initialization so earlier init failures have no live IRQ.

Fixes 3536169f8531 ("media: aspeed: fix clock handling logic")

---
No hardware testing.

With reset-default masking, no probe IRQ failure has been demonstrated; this part hardens cleanup ordering.

Changes in v2:
- Join the IRQ and resolution work before manual probe-failure cleanup.
- Rebase onto managed reserved-memory initialization.
- Clean up JPEG and clocks if the late IRQ request fails.
- Split normal last-close ordering into patch 2.

 drivers/media/platform/aspeed/aspeed-video.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/media/platform/aspeed/aspeed-video.c +16 -8
@@ -308,6 +308,7 @@
  * perf:		holds the statistics primary for debugfs
  */
 struct aspeed_video {
+	int irq;
 	void __iomem *base;
 	struct clk *eclk;
 	struct clk *vclk;
@@ -2232,14 +2233,6 @@
 		return -ENODEV;
 	}
 
-	rc = devm_request_threaded_irq(dev, irq, NULL, aspeed_video_irq,
-				       IRQF_ONESHOT, DEVICE_NAME, video);
-	if (rc < 0) {
-		dev_err(dev, "Unable to request IRQ %d\n", irq);
-		return rc;
-	}
-	dev_info(video->dev, "irq %d\n", irq);
-
 	video->reset = devm_reset_control_get(dev, NULL);
 	if (IS_ERR(video->reset)) {
 		dev_err(dev, "Unable to get reset\n");
@@ -2286,8 +2279,19 @@
 
 	aspeed_video_init_jpeg_table(video->jpeg.virt, video->yuv420);
 
+	rc = devm_request_threaded_irq(dev, irq, NULL, aspeed_video_irq,
+				       IRQF_ONESHOT, DEVICE_NAME, video);
+	if (rc < 0) {
+		dev_err(dev, "Unable to request IRQ %d\n", irq);
+		goto err_free_jpeg;
+	}
+	video->irq = irq;
+	dev_info(video->dev, "irq %d\n", irq);
+
 	return 0;
 
+err_free_jpeg:
+	aspeed_video_free_buf(video, &video->jpeg);
 err_unprepare_vclk:
 	clk_unprepare(video->vclk);
 err_unprepare_eclk:
@@ -2341,6 +2345,10 @@
 
 	rc = aspeed_video_setup_video(video);
 	if (rc) {
+		set_bit(VIDEO_STOPPED, &video->flags);
+		devm_free_irq(video->dev, video->irq, video);
+		cancel_delayed_work_sync(&video->res_work);
+		aspeed_video_off(video);
 		aspeed_video_free_buf(video, &video->jpeg);
 		clk_unprepare(video->vclk);
 		clk_unprepare(video->eclk);
#2 lore raw

RE: [PATCH v2 1/2] media: aspeed: join IRQ and work before probe cleanup

Hi Myeonghun,

Thanks for the patch.

Quoted 3 lines

The setup failure path unprepares the clocks before devres frees the IRQ. An IRQ handler that has sampled an enabled status can still queue resolution work. Masking the source does not synchronize that handler.

Before aspeed_video_setup_video() registers the video node, nothing calls aspeed_video_on(). The engine stays clock-gated and VE_INTERRUPT_CTRL keeps its reset value, so I don't see how the handler can sample an enabled status during probe. You say as much below the '---'. Could you reword the commit message so it reads as hardening of the cleanup order, not as a fix for an observed race? Then please reconsider the Fixes: tag as well.

Quoted 31 lines

Save the IRQ number after a successful request, then free the action and drain resolution work before the manual JPEG and clock cleanup. Turn the engine off after the work has finished. Request the IRQ after reset, clock and JPEG initialization so earlier init failures have no live IRQ.

Fixes 3536169f8531 ("media: aspeed: fix clock handling logic")

---
No hardware testing.

With reset-default masking, no probe IRQ failure has been demonstrated; this part hardens cleanup ordering.

Changes in v2:
- Join the IRQ and resolution work before manual probe-failure cleanup.
- Rebase onto managed reserved-memory initialization.
- Clean up JPEG and clocks if the late IRQ request fails.
- Split normal last-close ordering into patch 2.

 drivers/media/platform/aspeed/aspeed-video.c | 24
++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

drivers/media/platform/aspeed/aspeed-video.c line 312

Quoted 5 lines
drivers/media/platform/aspeed/aspeed-video.c +1 -0
@@ -308,6 +308,7 @@
  * perf:		holds the statistics primary for debugfs
  */
 struct aspeed_video {
+	int irq;
 	void __iomem *base;

This struct has a kernel-doc comment, so the new member needs a description there, or W=1 builds will warn. Please also move it next to the other resources rather than making it the first member.

Quoted 37 lines
drivers/media/platform/aspeed/aspeed-video.c +0 -8
 	struct clk *eclk;
 	struct clk *vclk;
@@ -2232,14 +2233,6 @@
 		return -ENODEV;
 	}
 
-	rc = devm_request_threaded_irq(dev, irq, NULL, aspeed_video_irq,
-				       IRQF_ONESHOT, DEVICE_NAME, video);
-	if (rc < 0) {
-		dev_err(dev, "Unable to request IRQ %d\n", irq);
-		return rc;
-	}
-	dev_info(video->dev, "irq %d\n", irq);
-
 	video->reset = devm_reset_control_get(dev, NULL);
 	if (IS_ERR(video->reset)) {
 		dev_err(dev, "Unable to get reset\n"); @@ -2286,8 +2279,19 @@
drivers/media/platform/aspeed/aspeed-video.c +11 -0
 	aspeed_video_init_jpeg_table(video->jpeg.virt, video->yuv420);
 
+	rc = devm_request_threaded_irq(dev, irq, NULL, aspeed_video_irq,
+				       IRQF_ONESHOT, DEVICE_NAME, video);
+	if (rc < 0) {
+		dev_err(dev, "Unable to request IRQ %d\n", irq);
+		goto err_free_jpeg;
+	}
+	video->irq = irq;
+	dev_info(video->dev, "irq %d\n", irq);
+
 	return 0;
 
+err_free_jpeg:
+	aspeed_video_free_buf(video, &video->jpeg);
 err_unprepare_vclk:
 	clk_unprepare(video->vclk);
 err_unprepare_eclk:

drivers/media/platform/aspeed/aspeed-video.c line 2352

Quoted 8 lines
drivers/media/platform/aspeed/aspeed-video.c +4 -0
@@ -2341,6 +2345,10 @@
 
 	rc = aspeed_video_setup_video(video);
 	if (rc) {
+		set_bit(VIDEO_STOPPED, &video->flags);
+		devm_free_irq(video->dev, video->irq, video);
+		cancel_delayed_work_sync(&video->res_work);
+		aspeed_video_off(video);
 		aspeed_video_free_buf(video, &video->jpeg);

I don't think this hunk does anything. Until setup_video() has registered the video node, nothing calls aspeed_video_on(), so VIDEO_CLOCKS_ON is clear and VE_INTERRUPT_CTRL keeps its reset value of 0. The handler masks status with VE_INTERRUPT_CTRL and returns IRQ_NONE, so res_work can never be queued. cancel_delayed_work_sync() then has nothing to cancel, and aspeed_video_off() returns early.

Quoted 2 lines
drivers/media/platform/aspeed/aspeed-video.c +0 -0
 		clk_unprepare(video->vclk);
 		clk_unprepare(video->eclk);
#3 lore raw

PATCH v2 2/2 media: aspeed: drain IRQ work before stopping the engine

In reply to Myeonghun Pak

Last close cancels resolution work while its IRQ producer is still live. A handler can queue work after cancellation and buffer cleanup. The worker can then turn clocks back on after stop has cleared VIDEO_STOPPED.

Release the VB2 queue before shutting down the engine, since streamoff can need capture and mode-detect interrupts. Disable and join the IRQ, drain work, stop the engine and free its buffers before re-enabling the IRQ. Check VIDEO_STOPPED before enabling clocks in the worker.

Fixes d2b4387f3bdf ("media: platform: Add Aspeed Video Engine driver")

---
No hardware testing.

Changes in v2:
- Split normal last-close ordering into a separate patch.
- Balance IRQ disable/enable within stop; add no reopen state.

 drivers/media/platform/aspeed/aspeed-video.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/media/platform/aspeed/aspeed-video.c +9 -5
@@ -1423,6 +1423,7 @@
 static void aspeed_video_stop(struct aspeed_video *video)
 {
 	set_bit(VIDEO_STOPPED, &video->flags);
+	disable_irq(video->irq);
 	cancel_delayed_work_sync(&video->res_work);
 
 	aspeed_video_off(video);
@@ -1438,6 +1439,7 @@
 
 	video->v4l2_input_status = V4L2_IN_ST_NO_SIGNAL;
 	video->flags = 0;
+	enable_irq(video->irq);
 }
 
 static int aspeed_video_querycap(struct file *file, void *fh,
@@ -1847,11 +1849,11 @@
 	struct aspeed_video *video = container_of(dwork, struct aspeed_video,
 						  res_work);
 
-	aspeed_video_on(video);
-
 	/* Exit early in case no clients remain */
 	if (test_bit(VIDEO_STOPPED, &video->flags))
 		goto done;
+
+	aspeed_video_on(video);
 
 	aspeed_video_init_regs(video);
 
@@ -1901,15 +1903,17 @@
 
 static int aspeed_video_release(struct file *file)
 {
+	bool last;
 	int rc;
 	struct aspeed_video *video = video_drvdata(file);
 
 	mutex_lock(&video->video_lock);
 
-	if (v4l2_fh_is_singular_file(file))
+	last = v4l2_fh_is_singular_file(file);
+	rc = _vb2_fop_release(file, NULL);
+
+	if (last)
 		aspeed_video_stop(video);
-
-	rc = _vb2_fop_release(file, NULL);
 
 	mutex_unlock(&video->video_lock);
 
#4 lore raw

RE: [PATCH v2 2/2] media: aspeed: drain IRQ work before stopping the engine

Hi Myeonghun,

Quoted 4 lines

Last close cancels resolution work while its IRQ producer is still live. A handler can queue work after cancellation and buffer cleanup. The worker can then turn clocks back on after stop has cleared VIDEO_STOPPED.

The stop() and worker changes look right to me. The release reorder doesn't seem needed; details below.

Series structure: this patch fixes a real race, while 1/2 is hardening for a probe path that can't currently hit the problem. Please add the irq field here and put this patch first, so it can be backported on its own with the Fixes: tag, independent of how 1/2 changes.

Quoted 25 lines

Release the VB2 queue before shutting down the engine, since streamoff can need capture and mode-detect interrupts. Disable and join the IRQ, drain work, stop the engine and free its buffers before re-enabling the IRQ. Check VIDEO_STOPPED before enabling clocks in the worker.

Fixes d2b4387f3bdf ("media: platform: Add Aspeed Video Engine driver")

---
No hardware testing.

Changes in v2:
- Split normal last-close ordering into a separate patch.
- Balance IRQ disable/enable within stop; add no reopen state.

 drivers/media/platform/aspeed/aspeed-video.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

drivers/media/platform/aspeed/aspeed-video.c line 1426

Quoted 4 lines
drivers/media/platform/aspeed/aspeed-video.c +1 -0
@@ -1423,6 +1423,7 @@
 static void aspeed_video_stop(struct aspeed_video *video)  {
 	set_bit(VIDEO_STOPPED, &video->flags);
+	disable_irq(video->irq);
 	cancel_delayed_work_sync(&video->res_work);

OK. One side effect: if res_work is already in aspeed_video_get_resolution(), it waits for a mode-detect IRQ that can't arrive, so close may block for about MODE_DETECT_TIMEOUT while holding video_lock. That's acceptable around a mode change, but please add a comment here and mention it in the commit message.

Quoted 12 lines
drivers/media/platform/aspeed/aspeed-video.c +1 -0
 	aspeed_video_off(video);
@@ -1438,6 +1439,7 @@
 
 	video->v4l2_input_status = V4L2_IN_ST_NO_SIGNAL;
 	video->flags = 0;
+	enable_irq(video->irq);
 }
 
 

static int aspeed_video_querycap(struct file *file, void *fh, @@ -1847,11
+1849,11 @@

drivers/media/platform/aspeed/aspeed-video.c line 1856

Quoted 10 lines
drivers/media/platform/aspeed/aspeed-video.c +2 -2
 	struct aspeed_video *video = container_of(dwork, struct aspeed_video,
 						  res_work);
 
-	aspeed_video_on(video);
-
 	/* Exit early in case no clients remain */
 	if (test_bit(VIDEO_STOPPED, &video->flags))
 		goto done;
+
+	aspeed_video_on(video);

Looks fine for me.

Quoted 10 lines
drivers/media/platform/aspeed/aspeed-video.c +1 -0
 	aspeed_video_init_regs(video);
@@ -1901,15 +1903,17 @@
 
 static int aspeed_video_release(struct file *file)  {
+	bool last;
 	int rc;
 	struct aspeed_video *video = video_drvdata(file);
 

drivers/media/platform/aspeed/aspeed-video.c line 1911

Quoted 10 lines
drivers/media/platform/aspeed/aspeed-video.c +4 -3
 	mutex_lock(&video->video_lock);
 
-	if (v4l2_fh_is_singular_file(file))
+	last = v4l2_fh_is_singular_file(file);
+	rc = _vb2_fop_release(file, NULL);
+
+	if (last)
 		aspeed_video_stop(video);
-
-	rc = _vb2_fop_release(file, NULL);

I don't see what this fixes. In the original order, stop() gates the clocks, which halts the engine, and clears VIDEO_FRAME_INPRG, so stop_streaming() returns the buffers right away without needing any interrupt.

With the new order, closing while streaming can wait up to STOP_TIMEOUT, then reset and re-run mode detect (another MODE_DETECT_TIMEOUT), only for stop() to shut everything down.

Please drop this hunk and update the commit message and changelog to match.

Thanks,
Jammy>

Quoted 2 lines
drivers/media/platform/aspeed/aspeed-video.c +0 -0
 	mutex_unlock(&video->video_lock);
 
#5 lore raw
In reply to Myeonghun Pak

Eddie or Jammy,

Quoted 6 lines · Myeonghun Pak

This revision requests the IRQ after its dependencies and synchronizes IRQ/work before manual probe cleanup. The second patch fixes last-close ordering and keeps the engine available until VB2 release completes.

Compile-tested on ARM64 with LLVM, W=1.
No hardware testing.

Can you review/test this series?

I don't feel comfortable merging this without your OK.

Regards,

Hans

Quoted 16 lines

Changes in v2:
- Rebase onto managed reserved-memory initialization.
- Remove the research sentence and Assisted-by tag.
- Join the IRQ and delayed work before manual probe-failure cleanup.
- Keep VB2 release before normal last-close hardware shutdown.
- Drain the IRQ producer before cancelling resolution work.
- Balance IRQ disable/enable within stop and check STOPPED before power-on.

Myeonghun Pak (2):
media: aspeed: join IRQ and work before probe cleanup
media: aspeed: drain IRQ work before stopping the engine

 drivers/media/platform/aspeed/aspeed-video.c | 38 +++++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 13 deletions(-)
#6 lore raw

Hi Hans,

I'll test this series on our ASPEED platforms first. If everything looks good, I'll follow up with a Reviewed-by tag afterward.

Regards,
Jammy

Quoted 37 lines

Eddie or Jammy,

Quoted 3 lines · Myeonghun Pak

This revision requests the IRQ after its dependencies and synchronizes IRQ/work before manual probe cleanup. The second patch fixes last-close ordering and keeps the engine available until VB2 release

completes.

Quoted 3 lines

Compile-tested on ARM64 with LLVM, W=1.
No hardware testing.

Can you review/test this series?

I don't feel comfortable merging this without your OK.

Regards,

Hans

Quoted 8 lines

Changes in v2:
- Rebase onto managed reserved-memory initialization.
- Remove the research sentence and Assisted-by tag.
- Join the IRQ and delayed work before manual probe-failure cleanup.
- Keep VB2 release before normal last-close hardware shutdown.
- Drain the IRQ producer before cancelling resolution work.
- Balance IRQ disable/enable within stop and check STOPPED before

power-on.

Quoted 9 lines

Myeonghun Pak (2):
media: aspeed: join IRQ and work before probe cleanup
media: aspeed: drain IRQ work before stopping the engine

 drivers/media/platform/aspeed/aspeed-video.c | 38
+++++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 13 deletions(-)