Skip to contentSkip to conversation

media: vb2: prepare for vb2_ops_wait_prepare/finish removal

PATCH In review Hans Verkuil 3 participants 24 messages Sep 2, 2024 – Sep 10, 2024

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

cover media: vb2: prepare for vb2_ops_wait_prepare/finish removal

This patch series sits on top of:
https://patchwork.linuxtv.org/project/linux-media/list/?series=13513

This series makes it possible for drivers to drop the wait_prepare/finish callbacks if the queue lock pointer is set.

Analyzing the code shows that in a few places the lock is set after the call to vb2_queue_init, so the first 7 patches move that lock assignment up.

Then two additional WARN_ONs are added to vb2_queue_init to ensure that either q->lock is set, or wait_prepare/finish are set.

The last patch will just unlock/lock the queue lock if wait_prepare/finish isn't set.

Once this is in, we can start dropping the wait_prepare/finish ops in almost all drivers.

Regards,

Hans

Hans Verkuil (9):
media: staging: atomisp: set lock before calling vb2_queue_init()
media: pwc: set lock before calling vb2_queue_init()
media: msi2500: set lock before calling vb2_queue_init()
media: hackrf: set lock before calling vb2_queue_init()
media: airspy: set lock before calling vb2_queue_init()
media: rcar_drif.c: set lock before calling vb2_queue_init()
media: video-i2c: set lock before calling vb2_queue_init()
media: vb2: vb2_core_queue_init(): sanity check lock and
wait_prepare/finish
media: vb2: use lock if wait_prepare/finish are NULL

 .../media/common/videobuf2/videobuf2-core.c   | 21 ++++++++++++++++---
 drivers/media/i2c/video-i2c.c                 |  2 +-
 drivers/media/platform/renesas/rcar_drif.c    |  2 +-
 drivers/media/usb/airspy/airspy.c             |  2 +-
 drivers/media/usb/hackrf/hackrf.c             |  4 ++--
 drivers/media/usb/msi2500/msi2500.c           |  2 +-
 drivers/media/usb/pwc/pwc-if.c                |  2 +-
 .../media/atomisp/pci/atomisp_subdev.c        |  2 +-
 8 files changed, 26 insertions(+), 11 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/9 media: staging: atomisp: set lock before calling vb2_queue_init()

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

---
 drivers/staging/media/atomisp/pci/atomisp_subdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/media/atomisp/pci/atomisp_subdev.c +1 -1
index 3a3e84a035e2..202497695e46 100644
@@ -797,12 +797,12 @@ static int atomisp_init_subdev_pipe(struct atomisp_sub_device *asd,
 	pipe->vb_queue.ops = &atomisp_vb2_ops;
 	pipe->vb_queue.mem_ops = &vb2_vmalloc_memops;
 	pipe->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	pipe->vb_queue.lock = &pipe->vb_queue_mutex;
 	ret = vb2_queue_init(&pipe->vb_queue);
 	if (ret)
 		return ret;
 
 	pipe->vdev.queue = &pipe->vb_queue;
-	pipe->vdev.queue->lock = &pipe->vb_queue_mutex;
 
 	INIT_LIST_HEAD(&pipe->buffers_in_css);
 	INIT_LIST_HEAD(&pipe->activeq);
Signature · 1 line
2.34.1
Reviewed-by on v1 1/9
#2 lore raw

Re: [PATCH 1/9] media: staging: atomisp: set lock before calling vb2_queue_init()

Hi Hans,

Thank you for the patch.

Quoted 6 lines · Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

Quoted 22 lines
---
 drivers/staging/media/atomisp/pci/atomisp_subdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/media/atomisp/pci/atomisp_subdev.c +1 -1
index 3a3e84a035e2..202497695e46 100644
@@ -797,12 +797,12 @@ static int atomisp_init_subdev_pipe(struct atomisp_sub_device *asd,
 	pipe->vb_queue.ops = &atomisp_vb2_ops;
 	pipe->vb_queue.mem_ops = &vb2_vmalloc_memops;
 	pipe->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	pipe->vb_queue.lock = &pipe->vb_queue_mutex;
 	ret = vb2_queue_init(&pipe->vb_queue);
 	if (ret)
 		return ret;
 
 	pipe->vdev.queue = &pipe->vb_queue;
-	pipe->vdev.queue->lock = &pipe->vb_queue_mutex;
 
 	INIT_LIST_HEAD(&pipe->buffers_in_css);
 	INIT_LIST_HEAD(&pipe->activeq);
Signature · 3 lines
Regards,

Laurent Pinchart
#3 lore raw

PATCH 2/9 media: pwc: set lock before calling vb2_queue_init()

In reply to Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

---
 drivers/media/usb/pwc/pwc-if.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/usb/pwc/pwc-if.c +1 -1
index e342199711d3..4a8485ea0c81 100644
@@ -1054,6 +1054,7 @@ static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id
 	pdev->vb_queue.ops = &pwc_vb_queue_ops;
 	pdev->vb_queue.mem_ops = &vb2_vmalloc_memops;
 	pdev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	pdev->vb_queue.lock = &pdev->vb_queue_lock;
 	rc = vb2_queue_init(&pdev->vb_queue);
 	if (rc < 0) {
 		PWC_ERROR("Oops, could not initialize vb2 queue.\n");
@@ -1064,7 +1065,6 @@ static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id
 	pdev->vdev = pwc_template;
 	strscpy(pdev->vdev.name, name, sizeof(pdev->vdev.name));
 	pdev->vdev.queue = &pdev->vb_queue;
-	pdev->vdev.queue->lock = &pdev->vb_queue_lock;
 	video_set_drvdata(&pdev->vdev, pdev);
 
 	pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
Signature · 1 line
2.34.1
Reviewed-by on v1 2/9
#4 lore raw

Re: [PATCH 2/9] media: pwc: set lock before calling vb2_queue_init()

Hi Hans,

Thank you for the patch.

Quoted 5 lines · Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

Quoted 24 lines
---
 drivers/media/usb/pwc/pwc-if.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/usb/pwc/pwc-if.c +1 -1
index e342199711d3..4a8485ea0c81 100644
@@ -1054,6 +1054,7 @@ static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id
 	pdev->vb_queue.ops = &pwc_vb_queue_ops;
 	pdev->vb_queue.mem_ops = &vb2_vmalloc_memops;
 	pdev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	pdev->vb_queue.lock = &pdev->vb_queue_lock;
 	rc = vb2_queue_init(&pdev->vb_queue);
 	if (rc < 0) {
 		PWC_ERROR("Oops, could not initialize vb2 queue.\n");
@@ -1064,7 +1065,6 @@ static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id
 	pdev->vdev = pwc_template;
 	strscpy(pdev->vdev.name, name, sizeof(pdev->vdev.name));
 	pdev->vdev.queue = &pdev->vb_queue;
-	pdev->vdev.queue->lock = &pdev->vb_queue_lock;
 	video_set_drvdata(&pdev->vdev, pdev);
 
 	pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
Signature · 3 lines
Regards,

Laurent Pinchart
#5 lore raw

PATCH 3/9 media: msi2500: set lock before calling vb2_queue_init()

In reply to Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

---
 drivers/media/usb/msi2500/msi2500.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/usb/msi2500/msi2500.c +1 -1
index 5138486abfa0..c42fad1e3204 100644
@@ -1199,6 +1199,7 @@ static int msi2500_probe(struct usb_interface *intf,
 	dev->vb_queue.ops = &msi2500_vb2_ops;
 	dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
 	dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	dev->vb_queue.lock = &dev->vb_queue_lock;
 	ret = vb2_queue_init(&dev->vb_queue);
 	if (ret) {
 		dev_err(dev->dev, "Could not initialize vb2 queue\n");
@@ -1208,7 +1209,6 @@ static int msi2500_probe(struct usb_interface *intf,
 	/* Init video_device structure */
 	dev->vdev = msi2500_template;
 	dev->vdev.queue = &dev->vb_queue;
-	dev->vdev.queue->lock = &dev->vb_queue_lock;
 	video_set_drvdata(&dev->vdev, dev);
 
 	/* Register the v4l2_device structure */
Signature · 1 line
2.34.1
Reviewed-by on v1 3/9
#6 lore raw

Re: [PATCH 3/9] media: msi2500: set lock before calling vb2_queue_init()

Hi Hans,

Thank you for the patch.

Quoted 5 lines · Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

Quoted 24 lines
---
 drivers/media/usb/msi2500/msi2500.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/usb/msi2500/msi2500.c +1 -1
index 5138486abfa0..c42fad1e3204 100644
@@ -1199,6 +1199,7 @@ static int msi2500_probe(struct usb_interface *intf,
 	dev->vb_queue.ops = &msi2500_vb2_ops;
 	dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
 	dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	dev->vb_queue.lock = &dev->vb_queue_lock;
 	ret = vb2_queue_init(&dev->vb_queue);
 	if (ret) {
 		dev_err(dev->dev, "Could not initialize vb2 queue\n");
@@ -1208,7 +1209,6 @@ static int msi2500_probe(struct usb_interface *intf,
 	/* Init video_device structure */
 	dev->vdev = msi2500_template;
 	dev->vdev.queue = &dev->vb_queue;
-	dev->vdev.queue->lock = &dev->vb_queue_lock;
 	video_set_drvdata(&dev->vdev, dev);
 
 	/* Register the v4l2_device structure */
Signature · 3 lines
Regards,

Laurent Pinchart
#7 lore raw

PATCH 4/9 media: hackrf: set lock before calling vb2_queue_init()

In reply to Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

---
 drivers/media/usb/hackrf/hackrf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/hackrf/hackrf.c +2 -2
index 9c0ecd5f056c..a1a7690a344a 100644
@@ -1398,6 +1398,7 @@ static int hackrf_probe(struct usb_interface *intf,
 	dev->rx_vb2_queue.drv_priv = dev;
 	dev->rx_vb2_queue.buf_struct_size = sizeof(struct hackrf_buffer);
 	dev->rx_vb2_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	dev->rx_vb2_queue.lock = &dev->vb_queue_lock;
 	ret = vb2_queue_init(&dev->rx_vb2_queue);
 	if (ret) {
 		dev_err(dev->dev, "Could not initialize rx vb2 queue\n");
@@ -1413,6 +1414,7 @@ static int hackrf_probe(struct usb_interface *intf,
 	dev->tx_vb2_queue.drv_priv = dev;
 	dev->tx_vb2_queue.buf_struct_size = sizeof(struct hackrf_buffer);
 	dev->tx_vb2_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	dev->tx_vb2_queue.lock = &dev->vb_queue_lock;
 	ret = vb2_queue_init(&dev->tx_vb2_queue);
 	if (ret) {
 		dev_err(dev->dev, "Could not initialize tx vb2 queue\n");
@@ -1474,7 +1476,6 @@ static int hackrf_probe(struct usb_interface *intf,
 	/* Init video_device structure for receiver */
 	dev->rx_vdev = hackrf_template;
 	dev->rx_vdev.queue = &dev->rx_vb2_queue;
-	dev->rx_vdev.queue->lock = &dev->vb_queue_lock;
 	dev->rx_vdev.v4l2_dev = &dev->v4l2_dev;
 	dev->rx_vdev.ctrl_handler = &dev->rx_ctrl_handler;
 	dev->rx_vdev.lock = &dev->v4l2_lock;
@@ -1494,7 +1495,6 @@ static int hackrf_probe(struct usb_interface *intf,
 	/* Init video_device structure for transmitter */
 	dev->tx_vdev = hackrf_template;
 	dev->tx_vdev.queue = &dev->tx_vb2_queue;
-	dev->tx_vdev.queue->lock = &dev->vb_queue_lock;
 	dev->tx_vdev.v4l2_dev = &dev->v4l2_dev;
 	dev->tx_vdev.ctrl_handler = &dev->tx_ctrl_handler;
 	dev->tx_vdev.lock = &dev->v4l2_lock;
Signature · 1 line
2.34.1
Reviewed-by on v1 4/9
#8 lore raw

Re: [PATCH 4/9] media: hackrf: set lock before calling vb2_queue_init()

Hi Hans,

Thank you for the patch.

Quoted 22 lines · Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

---
 drivers/media/usb/hackrf/hackrf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/hackrf/hackrf.c +1 -0
index 9c0ecd5f056c..a1a7690a344a 100644
@@ -1398,6 +1398,7 @@ static int hackrf_probe(struct usb_interface *intf,
 	dev->rx_vb2_queue.drv_priv = dev;
 	dev->rx_vb2_queue.buf_struct_size = sizeof(struct hackrf_buffer);
 	dev->rx_vb2_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	dev->rx_vb2_queue.lock = &dev->vb_queue_lock;
 	ret = vb2_queue_init(&dev->rx_vb2_queue);
 	if (ret) {
 		dev_err(dev->dev, "Could not initialize rx vb2 queue\n");

drivers/media/usb/hackrf/hackrf.c static int hackrf_probe(struct usb_interface *intf, · line 1417

Quoted 4 lines
drivers/media/usb/hackrf/hackrf.c +1 -0
@@ -1413,6 +1414,7 @@ static int hackrf_probe(struct usb_interface *intf,
 	dev->tx_vb2_queue.drv_priv = dev;
 	dev->tx_vb2_queue.buf_struct_size = sizeof(struct hackrf_buffer);
 	dev->tx_vb2_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	dev->tx_vb2_queue.lock = &dev->vb_queue_lock;

I'm not sure if setting the same lock for both video queues is desired, but that's unrelated to this patch.

Quoted 19 lines
drivers/media/usb/hackrf/hackrf.c +0 -2
 	ret = vb2_queue_init(&dev->tx_vb2_queue);
 	if (ret) {
 		dev_err(dev->dev, "Could not initialize tx vb2 queue\n");
@@ -1474,7 +1476,6 @@ static int hackrf_probe(struct usb_interface *intf,
 	/* Init video_device structure for receiver */
 	dev->rx_vdev = hackrf_template;
 	dev->rx_vdev.queue = &dev->rx_vb2_queue;
-	dev->rx_vdev.queue->lock = &dev->vb_queue_lock;
 	dev->rx_vdev.v4l2_dev = &dev->v4l2_dev;
 	dev->rx_vdev.ctrl_handler = &dev->rx_ctrl_handler;
 	dev->rx_vdev.lock = &dev->v4l2_lock;
@@ -1494,7 +1495,6 @@ static int hackrf_probe(struct usb_interface *intf,
 	/* Init video_device structure for transmitter */
 	dev->tx_vdev = hackrf_template;
 	dev->tx_vdev.queue = &dev->tx_vb2_queue;
-	dev->tx_vdev.queue->lock = &dev->vb_queue_lock;
 	dev->tx_vdev.v4l2_dev = &dev->v4l2_dev;
 	dev->tx_vdev.ctrl_handler = &dev->tx_ctrl_handler;
 	dev->tx_vdev.lock = &dev->v4l2_lock;
Signature · 3 lines
Regards,

Laurent Pinchart
#9 lore raw

PATCH 5/9 media: airspy: set lock before calling vb2_queue_init()

In reply to Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

---
 drivers/media/usb/airspy/airspy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/usb/airspy/airspy.c +1 -1
index e24e655fb1db..3e2a3099a8fe 100644
@@ -1017,6 +1017,7 @@ static int airspy_probe(struct usb_interface *intf,
 	s->vb_queue.ops = &airspy_vb2_ops;
 	s->vb_queue.mem_ops = &vb2_vmalloc_memops;
 	s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	s->vb_queue.lock = &s->vb_queue_lock;
 	ret = vb2_queue_init(&s->vb_queue);
 	if (ret) {
 		dev_err(s->dev, "Could not initialize vb2 queue\n");
@@ -1026,7 +1027,6 @@ static int airspy_probe(struct usb_interface *intf,
 	/* Init video_device structure */
 	s->vdev = airspy_template;
 	s->vdev.queue = &s->vb_queue;
-	s->vdev.queue->lock = &s->vb_queue_lock;
 	video_set_drvdata(&s->vdev, s);
 
 	/* Register the v4l2_device structure */
Signature · 1 line
2.34.1
Reviewed-by on v1 5/9
#10 lore raw

Re: [PATCH 5/9] media: airspy: set lock before calling vb2_queue_init()

Hi Hans,

Thank you for the patch.

Quoted 5 lines · Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

Quoted 24 lines
---
 drivers/media/usb/airspy/airspy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/usb/airspy/airspy.c +1 -1
index e24e655fb1db..3e2a3099a8fe 100644
@@ -1017,6 +1017,7 @@ static int airspy_probe(struct usb_interface *intf,
 	s->vb_queue.ops = &airspy_vb2_ops;
 	s->vb_queue.mem_ops = &vb2_vmalloc_memops;
 	s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	s->vb_queue.lock = &s->vb_queue_lock;
 	ret = vb2_queue_init(&s->vb_queue);
 	if (ret) {
 		dev_err(s->dev, "Could not initialize vb2 queue\n");
@@ -1026,7 +1027,6 @@ static int airspy_probe(struct usb_interface *intf,
 	/* Init video_device structure */
 	s->vdev = airspy_template;
 	s->vdev.queue = &s->vb_queue;
-	s->vdev.queue->lock = &s->vb_queue_lock;
 	video_set_drvdata(&s->vdev, s);
 
 	/* Register the v4l2_device structure */
Signature · 3 lines
Regards,

Laurent Pinchart
#11 lore raw

PATCH 6/9 media: rcar_drif.c: set lock before calling vb2_queue_init()

In reply to Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

Cc Ramesh Shanmugasundaram <[email protected]>
Cc Jacopo Mondi <[email protected]>
---
 drivers/media/platform/renesas/rcar_drif.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/renesas/rcar_drif.c +1 -1
index f21d05054341..5d3109940ded 100644
@@ -1071,7 +1071,6 @@ static int rcar_drif_sdr_register(struct rcar_drif_sdr *sdr)
 	sdr->vdev->release = video_device_release;
 	sdr->vdev->lock = &sdr->v4l2_mutex;
 	sdr->vdev->queue = &sdr->vb_queue;
-	sdr->vdev->queue->lock = &sdr->vb_queue_mutex;
 	sdr->vdev->ctrl_handler = &sdr->ctrl_hdl;
 	sdr->vdev->v4l2_dev = &sdr->v4l2_dev;
 	sdr->vdev->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER |
@@ -1316,6 +1315,7 @@ static int rcar_drif_sdr_probe(struct rcar_drif_sdr *sdr)
 	sdr->vb_queue.ops = &rcar_drif_vb2_ops;
 	sdr->vb_queue.mem_ops = &vb2_vmalloc_memops;
 	sdr->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	sdr->vb_queue.lock = &sdr->vb_queue_mutex;
 
 	/* Init videobuf2 queue */
 	ret = vb2_queue_init(&sdr->vb_queue);
Signature · 1 line
2.34.1
#12 lore raw

Re: [PATCH 6/9] media: rcar_drif.c: set lock before calling vb2_queue_init()

Hi Hans,

Thank you for the patch.

Quoted 3 lines · Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

The commit message doesn't match the change below, you're moving the lock down, not up.

Quoted 27 lines
Cc Ramesh Shanmugasundaram <[email protected]>
Cc Jacopo Mondi <[email protected]>
---
 drivers/media/platform/renesas/rcar_drif.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/renesas/rcar_drif.c +1 -1
index f21d05054341..5d3109940ded 100644
@@ -1071,7 +1071,6 @@ static int rcar_drif_sdr_register(struct rcar_drif_sdr *sdr)
 	sdr->vdev->release = video_device_release;
 	sdr->vdev->lock = &sdr->v4l2_mutex;
 	sdr->vdev->queue = &sdr->vb_queue;
-	sdr->vdev->queue->lock = &sdr->vb_queue_mutex;
 	sdr->vdev->ctrl_handler = &sdr->ctrl_hdl;
 	sdr->vdev->v4l2_dev = &sdr->v4l2_dev;
 	sdr->vdev->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER |
@@ -1316,6 +1315,7 @@ static int rcar_drif_sdr_probe(struct rcar_drif_sdr *sdr)
 	sdr->vb_queue.ops = &rcar_drif_vb2_ops;
 	sdr->vb_queue.mem_ops = &vb2_vmalloc_memops;
 	sdr->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	sdr->vb_queue.lock = &sdr->vb_queue_mutex;
 
 	/* Init videobuf2 queue */
 	ret = vb2_queue_init(&sdr->vb_queue);
Signature · 3 lines
Regards,

Laurent Pinchart
#13 lore raw

Re: [PATCH 6/9] media: rcar_drif.c: set lock before calling vb2_queue_init()

Quoted 11 lines · Laurent Pinchart

Hi Hans,

Thank you for the patch.

Quoted 3 lines · Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

The commit message doesn't match the change below, you're moving the lock down, not up.

Actually, I am moving it up, as in 'set earlier', but in this case it is a bit confusing in the patch.

Regards,

Hans

Quoted 29 lines
Quoted 28 lines
Cc Ramesh Shanmugasundaram <[email protected]>
Cc Jacopo Mondi <[email protected]>
---
 drivers/media/platform/renesas/rcar_drif.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/renesas/rcar_drif.c +1 -1
index f21d05054341..5d3109940ded 100644
@@ -1071,7 +1071,6 @@ static int rcar_drif_sdr_register(struct rcar_drif_sdr *sdr)
 	sdr->vdev->release = video_device_release;
 	sdr->vdev->lock = &sdr->v4l2_mutex;
 	sdr->vdev->queue = &sdr->vb_queue;
-	sdr->vdev->queue->lock = &sdr->vb_queue_mutex;
 	sdr->vdev->ctrl_handler = &sdr->ctrl_hdl;
 	sdr->vdev->v4l2_dev = &sdr->v4l2_dev;
 	sdr->vdev->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER |
@@ -1316,6 +1315,7 @@ static int rcar_drif_sdr_probe(struct rcar_drif_sdr *sdr)
 	sdr->vb_queue.ops = &rcar_drif_vb2_ops;
 	sdr->vb_queue.mem_ops = &vb2_vmalloc_memops;
 	sdr->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	sdr->vb_queue.lock = &sdr->vb_queue_mutex;
 
 	/* Init videobuf2 queue */
 	ret = vb2_queue_init(&sdr->vb_queue);
Reviewed-by
#14 lore raw

Re: [PATCH 6/9] media: rcar_drif.c: set lock before calling vb2_queue_init()

Quoted 11 lines · Hans Verkuil
Quoted 7 lines · Laurent Pinchart
Quoted 3 lines · Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

The commit message doesn't match the change below, you're moving the lock down, not up.

Actually, I am moving it up, as in 'set earlier', but in this case it is a bit confusing in the patch.

Oops indeed, I thought both changes were in the same function.

Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Cc Ramesh Shanmugasundaram <[email protected]>
Cc Jacopo Mondi <[email protected]>
---
 drivers/media/platform/renesas/rcar_drif.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/renesas/rcar_drif.c +1 -1
index f21d05054341..5d3109940ded 100644
@@ -1071,7 +1071,6 @@ static int rcar_drif_sdr_register(struct rcar_drif_sdr *sdr)
 	sdr->vdev->release = video_device_release;
 	sdr->vdev->lock = &sdr->v4l2_mutex;
 	sdr->vdev->queue = &sdr->vb_queue;
-	sdr->vdev->queue->lock = &sdr->vb_queue_mutex;
 	sdr->vdev->ctrl_handler = &sdr->ctrl_hdl;
 	sdr->vdev->v4l2_dev = &sdr->v4l2_dev;
 	sdr->vdev->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER |
@@ -1316,6 +1315,7 @@ static int rcar_drif_sdr_probe(struct rcar_drif_sdr *sdr)
 	sdr->vb_queue.ops = &rcar_drif_vb2_ops;
 	sdr->vb_queue.mem_ops = &vb2_vmalloc_memops;
 	sdr->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	sdr->vb_queue.lock = &sdr->vb_queue_mutex;
 
 	/* Init videobuf2 queue */
 	ret = vb2_queue_init(&sdr->vb_queue);
Signature · 3 lines
Regards,

Laurent Pinchart
#15 lore raw

PATCH 7/9 media: video-i2c: set lock before calling vb2_queue_init()

In reply to Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

---
 drivers/media/i2c/video-i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/i2c/video-i2c.c +1 -1
index 56dbe07a1c99..ffb3d9d61a74 100644
@@ -798,13 +798,13 @@ static int video_i2c_probe(struct i2c_client *client)
 	queue->min_queued_buffers = 1;
 	queue->ops = &video_i2c_video_qops;
 	queue->mem_ops = &vb2_vmalloc_memops;
+	queue->lock = &data->queue_lock;
 
 	ret = vb2_queue_init(queue);
 	if (ret < 0)
 		goto error_unregister_device;
 
 	data->vdev.queue = queue;
-	data->vdev.queue->lock = &data->queue_lock;
 
 	snprintf(data->vdev.name, sizeof(data->vdev.name),
 				 "I2C %d-%d Transport Video",
Signature · 1 line
2.34.1
Acked-by on v1 7/9
#16 lore raw

Re: [PATCH 7/9] media: video-i2c: set lock before calling vb2_queue_init()

Sent with Proton Mail secure email.

Quoted 6 lines · Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

LGTM

Quoted 34 lines
Cc Matt Ranostay [email protected]

---
drivers/media/i2c/video-i2c.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/i2c/video-i2c.c +1 -0
index 56dbe07a1c99..ffb3d9d61a74 100644
@@ -798,13 +798,13 @@ static int video_i2c_probe(struct i2c_client *client)
 queue->min_queued_buffers = 1;
 
 queue->ops = &video_i2c_video_qops;
 
 queue->mem_ops = &vb2_vmalloc_memops;
 
+ queue->lock = &data->queue_lock;
 
 
 ret = vb2_queue_init(queue);
 if (ret < 0)
 goto error_unregister_device;
 

data->vdev.queue = queue;

- data->vdev.queue->lock = &data->queue_lock;

snprintf(data->vdev.name, sizeof(data->vdev.name),

"I2C %d-%d Transport Video",
--
2.34.1

Reviewed-by on v1 7/9
#17 lore raw

Re: [PATCH 7/9] media: video-i2c: set lock before calling vb2_queue_init()

In reply to Hans Verkuil

Hi Hans,

Thank you for the patch.

Quoted 6 lines · Hans Verkuil

The vb2_queue_init() will expect the vb2_queue lock pointer to be set in the future. So for those drivers that set the lock later, move it up to before the vb2_queue_init() call.

Quoted 23 lines
---
 drivers/media/i2c/video-i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/i2c/video-i2c.c +1 -1
index 56dbe07a1c99..ffb3d9d61a74 100644
@@ -798,13 +798,13 @@ static int video_i2c_probe(struct i2c_client *client)
 	queue->min_queued_buffers = 1;
 	queue->ops = &video_i2c_video_qops;
 	queue->mem_ops = &vb2_vmalloc_memops;
+	queue->lock = &data->queue_lock;
 
 	ret = vb2_queue_init(queue);
 	if (ret < 0)
 		goto error_unregister_device;
 
 	data->vdev.queue = queue;
-	data->vdev.queue->lock = &data->queue_lock;
 
 	snprintf(data->vdev.name, sizeof(data->vdev.name),
 				 "I2C %d-%d Transport Video",
Signature · 3 lines
Regards,

Laurent Pinchart
#18 lore raw

PATCH 8/9 media: vb2: vb2_core_queue_init(): sanity check lock and wait_prepare/finish

In reply to Hans Verkuil

Add two new checks:

1) wait_prepare and wait_finish callbacks are either both present or both unset, you can't mix.
2) if lock == NULL, then wait_prepare (and due to check 1 also wait_finish) must be present.

These checks should prevent the case where lock == NULL, but there is no way to release/reacquire whatever lock is used when waiting for a buffer to arrive in VIDIOC_DQBUF.

---
 drivers/media/common/videobuf2/videobuf2-core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/media/common/videobuf2/videobuf2-core.c +8 -0
index 29a8d876e6c2..6335ac7b771a 100644
@@ -2644,6 +2644,14 @@ int vb2_core_queue_init(struct vb2_queue *q)
 	if (WARN_ON(q->min_reqbufs_allocation > q->max_num_buffers))
 		return -EINVAL;
 
+	/* Either both or none are set */
+	if (WARN_ON(!q->ops->wait_prepare ^ !q->ops->wait_finish))
+		return -EINVAL;
+
+	/* Warn if q->lock is NULL and no custom wait_prepare is provided */
+	if (WARN_ON(!q->lock && !q->ops->wait_prepare))
+		return -EINVAL;
+
 	INIT_LIST_HEAD(&q->queued_list);
 	INIT_LIST_HEAD(&q->done_list);
 	spin_lock_init(&q->done_lock);
Signature · 1 line
2.34.1
#19 lore raw

Re: [PATCH 8/9] media: vb2: vb2_core_queue_init(): sanity check lock and wait_prepare/finish

Hi Hans,

Thank you for the patch.

Quoted 10 lines · Hans Verkuil

Add two new checks:

1) wait_prepare and wait_finish callbacks are either both present or both unset, you can't mix.
2) if lock == NULL, then wait_prepare (and due to check 1 also wait_finish) must be present.

These checks should prevent the case where lock == NULL, but there is no way to release/reacquire whatever lock is used when waiting for a buffer to arrive in VIDIOC_DQBUF.

Don't we use the video_device lock when the queue lock is NULL ? Shouldn't it be valid to not set wait_prepare and wait_finish in that case ?

Quoted 24 lines
---
 drivers/media/common/videobuf2/videobuf2-core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/media/common/videobuf2/videobuf2-core.c +8 -0
index 29a8d876e6c2..6335ac7b771a 100644
@@ -2644,6 +2644,14 @@ int vb2_core_queue_init(struct vb2_queue *q)
 	if (WARN_ON(q->min_reqbufs_allocation > q->max_num_buffers))
 		return -EINVAL;
 
+	/* Either both or none are set */
+	if (WARN_ON(!q->ops->wait_prepare ^ !q->ops->wait_finish))
+		return -EINVAL;
+
+	/* Warn if q->lock is NULL and no custom wait_prepare is provided */
+	if (WARN_ON(!q->lock && !q->ops->wait_prepare))
+		return -EINVAL;
+
 	INIT_LIST_HEAD(&q->queued_list);
 	INIT_LIST_HEAD(&q->done_list);
 	spin_lock_init(&q->done_lock);
Signature · 3 lines
Regards,

Laurent Pinchart
#20 lore raw

Re: [PATCH 8/9] media: vb2: vb2_core_queue_init(): sanity check lock and wait_prepare/finish

Quoted 12 lines · Laurent Pinchart

Hi Hans,

Thank you for the patch.

Quoted 7 lines · Hans Verkuil

Add two new checks:

1) wait_prepare and wait_finish callbacks are either both present or both unset, you can't mix.
2) if lock == NULL, then wait_prepare (and due to check 1 also wait_finish) must be present.

Quoted 7 lines
Quoted 3 lines

These checks should prevent the case where lock == NULL, but there is no way to release/reacquire whatever lock is used when waiting for a buffer to arrive in VIDIOC_DQBUF.

Don't we use the video_device lock when the queue lock is NULL ? Shouldn't it be valid to not set wait_prepare and wait_finish in that case ?

If q->lock is NULL, then vb2 doesn't know which mutex needs to be unlocked while waiting for a buffer to arrive (and reacquired afterwards). So in that case you need to provide a function. Remember that vb2 doesn't know about the video_device lock. It is the driver that normally fills in q->lock, and often (or even always?) that is indeed the video_device lock.

If q->lock is NULL, and these functions are not provided, then that is almost certainly a bug since there is almost certainly some serialization mutex. And if you don't unlock that while waiting, then you can't issue other ioctls. In the unlikely event that there really is no mutex involved, then you have to supply empty functions. There is no driver like that, though.

Regards,

Hans

Quoted 26 lines
Quoted 25 lines
---
 drivers/media/common/videobuf2/videobuf2-core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/media/common/videobuf2/videobuf2-core.c +8 -0
index 29a8d876e6c2..6335ac7b771a 100644
@@ -2644,6 +2644,14 @@ int vb2_core_queue_init(struct vb2_queue *q)
 	if (WARN_ON(q->min_reqbufs_allocation > q->max_num_buffers))
 		return -EINVAL;
 
+	/* Either both or none are set */
+	if (WARN_ON(!q->ops->wait_prepare ^ !q->ops->wait_finish))
+		return -EINVAL;
+
+	/* Warn if q->lock is NULL and no custom wait_prepare is provided */
+	if (WARN_ON(!q->lock && !q->ops->wait_prepare))
+		return -EINVAL;
+
 	INIT_LIST_HEAD(&q->queued_list);
 	INIT_LIST_HEAD(&q->done_list);
 	spin_lock_init(&q->done_lock);
#21 lore raw

PATCH 9/9 media: vb2: use lock if wait_prepare/finish are NULL

In reply to Hans Verkuil

If the wait_prepare or wait_finish callback is set, then call it. If it is NULL and the queue lock pointer is not NULL, then just unlock/lock that mutex.

This allows simplifying drivers by dropping the wait_prepare and wait_finish ops (and eventually the vb2_ops_wait_prepare/finish helpers).

---
 drivers/media/common/videobuf2/videobuf2-core.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/media/common/videobuf2/videobuf2-core.c +10 -3
index 6335ac7b771a..d064e0664851 100644
@@ -2035,7 +2035,10 @@ static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
 		 * become ready or for streamoff. Driver's lock is released to
 		 * allow streamoff or qbuf to be called while waiting.
 		 */
-		call_void_qop(q, wait_prepare, q);
+		if (q->ops->wait_prepare)
+			call_void_qop(q, wait_prepare, q);
+		else if (q->lock)
+			mutex_unlock(q->lock);
 
 		/*
 		 * All locks have been released, it is safe to sleep now.
@@ -2045,12 +2048,16 @@ static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
 				!list_empty(&q->done_list) || !q->streaming ||
 				q->error);
 
+		if (q->ops->wait_finish)
+			call_void_qop(q, wait_finish, q);
+		else if (q->lock)
+			mutex_lock(q->lock);
+
+		q->waiting_in_dqbuf = 0;
 		/*
 		 * We need to reevaluate both conditions again after reacquiring
 		 * the locks or return an error if one occurred.
 		 */
-		call_void_qop(q, wait_finish, q);
-		q->waiting_in_dqbuf = 0;
 		if (ret) {
 			dprintk(q, 1, "sleep was interrupted\n");
 			return ret;
Signature · 1 line
2.34.1
#22 lore raw

Re: [PATCH 9/9] media: vb2: use lock if wait_prepare/finish are NULL

Hi Hans,

Thank you for the patch.

Quoted 17 lines · Hans Verkuil

If the wait_prepare or wait_finish callback is set, then call it. If it is NULL and the queue lock pointer is not NULL, then just unlock/lock that mutex.

This allows simplifying drivers by dropping the wait_prepare and wait_finish ops (and eventually the vb2_ops_wait_prepare/finish helpers).

---
 drivers/media/common/videobuf2/videobuf2-core.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/media/common/videobuf2/videobuf2-core.c +0 -0
index 6335ac7b771a..d064e0664851 100644

drivers/media/common/videobuf2/videobuf2-core.c static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking) · line 2041

Quoted 8 lines
drivers/media/common/videobuf2/videobuf2-core.c +4 -1
@@ -2035,7 +2035,10 @@ static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
 		 * become ready or for streamoff. Driver's lock is released to
 		 * allow streamoff or qbuf to be called while waiting.
 		 */
-		call_void_qop(q, wait_prepare, q);
+		if (q->ops->wait_prepare)
+			call_void_qop(q, wait_prepare, q);
+		else if (q->lock)
+			mutex_unlock(q->lock);

How about calling vb2_ops_wait_prepare() here ? I think that would be more explicit. You would likely need to move the function to videobuf2-core.c. Same below for wait_finish.

Quoted 22 lines
drivers/media/common/videobuf2/videobuf2-core.c +6 -2
 		/*
 		 * All locks have been released, it is safe to sleep now.
@@ -2045,12 +2048,16 @@ static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
 				!list_empty(&q->done_list) || !q->streaming ||
 				q->error);
 
+		if (q->ops->wait_finish)
+			call_void_qop(q, wait_finish, q);
+		else if (q->lock)
+			mutex_lock(q->lock);
+
+		q->waiting_in_dqbuf = 0;
 		/*
 		 * We need to reevaluate both conditions again after reacquiring
 		 * the locks or return an error if one occurred.
 		 */
-		call_void_qop(q, wait_finish, q);
-		q->waiting_in_dqbuf = 0;
 		if (ret) {
 			dprintk(q, 1, "sleep was interrupted\n");
 			return ret;
Signature · 3 lines
Regards,

Laurent Pinchart
#23 lore raw

Re: [PATCH 9/9] media: vb2: use lock if wait_prepare/finish are NULL

Quoted 26 lines · Laurent Pinchart

Hi Hans,

Thank you for the patch.

Quoted 21 lines · Hans Verkuil

If the wait_prepare or wait_finish callback is set, then call it. If it is NULL and the queue lock pointer is not NULL, then just unlock/lock that mutex.

This allows simplifying drivers by dropping the wait_prepare and wait_finish ops (and eventually the vb2_ops_wait_prepare/finish helpers).

---
 drivers/media/common/videobuf2/videobuf2-core.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/media/common/videobuf2/videobuf2-core.c +0 -1
index 6335ac7b771a..d064e0664851 100644
@@ -2035,7 +2035,10 @@ static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
 		 * become ready or for streamoff. Driver's lock is released to
 		 * allow streamoff or qbuf to be called while waiting.
 		 */
-		call_void_qop(q, wait_prepare, q);
Quoted 8 lines

drivers/media/common/videobuf2/videobuf2-core.c static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking) · line 2041

Quoted 4 lines
drivers/media/common/videobuf2/videobuf2-core.c +4 -0
+		if (q->ops->wait_prepare)
+			call_void_qop(q, wait_prepare, q);
+		else if (q->lock)
+			mutex_unlock(q->lock);

How about calling vb2_ops_wait_prepare() here ? I think that would be more explicit. You would likely need to move the function to videobuf2-core.c. Same below for wait_finish.

Why would I call a function that does exactly this? It is just harder to see what it does since you need to look up that function.

Just in case it wasn't clear: once this series is in I can start removing vb2_ops_wait_prepare/finish from all media drivers, and eventually remove those two helpers from vb2.

Regards,

Hans

Quoted 24 lines
Quoted 23 lines
drivers/media/common/videobuf2/videobuf2-core.c +6 -2
 		/*
 		 * All locks have been released, it is safe to sleep now.
@@ -2045,12 +2048,16 @@ static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
 				!list_empty(&q->done_list) || !q->streaming ||
 				q->error);
 
+		if (q->ops->wait_finish)
+			call_void_qop(q, wait_finish, q);
+		else if (q->lock)
+			mutex_lock(q->lock);
+
+		q->waiting_in_dqbuf = 0;
 		/*
 		 * We need to reevaluate both conditions again after reacquiring
 		 * the locks or return an error if one occurred.
 		 */
-		call_void_qop(q, wait_finish, q);
-		q->waiting_in_dqbuf = 0;
 		if (ret) {
 			dprintk(q, 1, "sleep was interrupted\n");
 			return ret;