Skip to contentSkip to conversation

media: Set missing .owner = THIS_MODULE in V4L2 and D-PHY ops

PATCH In review Ian Rogers 3 participants 9 messages Dec 3 – Dec 4

Subsystems (8)
lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

cover media: Set missing .owner = THIS_MODULE in V4L2 and D-PHY ops

Five modular media drivers omit .owner = THIS_MODULE in their const struct v4l2_file_operations or struct phy_ops definitions, leaving the module owner NULL for chrdev_open() or phy_get() reference counting.

Split per driver to simplify review and stable backporting.

Tooling note: Found using Clang's require_explicit_initialization attribute; an AI coding assistant helped audit call sites, verify Fixes commits, and draft the changelogs. All affected files were build-tested as modules (-DMODULE) with LLVM=1.

Ian Rogers (5):
media: amlogic: c3-isp: Set .owner = THIS_MODULE in
v4l2_file_operations
media: rockchip: rkcif: Set .owner = THIS_MODULE in
v4l2_file_operations
media: rkisp1: Set .owner = THIS_MODULE in v4l2_file_operations
staging: media: ipu3: Set .owner = THIS_MODULE in v4l2_file_operations
media: sunxi: a83t-mipi-csi2: Set .owner = THIS_MODULE in phy_ops

 drivers/media/platform/amlogic/c3/isp/c3-isp-capture.c         | 1 +
 drivers/media/platform/amlogic/c3/isp/c3-isp-params.c          | 1 +
 drivers/media/platform/amlogic/c3/isp/c3-isp-stats.c           | 1 +
 drivers/media/platform/rockchip/rkcif/rkcif-stream.c           | 1 +
 drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c        | 1 +
 drivers/media/platform/rockchip/rkisp1/rkisp1-params.c         | 3 ++-
 drivers/media/platform/rockchip/rkisp1/rkisp1-stats.c          | 3 ++-
 .../platform/sunxi/sun8i-a83t-mipi-csi2/sun8i_a83t_dphy.c      | 1 +
 drivers/staging/media/ipu3/ipu3-v4l2.c                         | 1 +
 9 files changed, 11 insertions(+), 2 deletions(-)

base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244

Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#1 lore raw

PATCH 1/5 media: amlogic: c3-isp: Set .owner = THIS_MODULE in v4l2_file_operations

video_register_device() passes vdev->fops->owner to __video_register_device(), which sets vdev->cdev->owner = owner so chrdev_open() pins the V4L2 driver module while the device node is open. When .owner is omitted in a const struct v4l2_file_operations, vdev->cdev->owner is NULL and the driver module is not reference counted on open().

Set .owner = THIS_MODULE in isp_cap_v4l2_fops, isp_params_v4l2_fops, and isp_stats_v4l2_fops, found with Clang's require_explicit_initialization attribute.

Fixes fb2e135208f3 ("media: platform: Add C3 ISP driver")
---
 drivers/media/platform/amlogic/c3/isp/c3-isp-capture.c | 1 +
 drivers/media/platform/amlogic/c3/isp/c3-isp-params.c  | 1 +
 drivers/media/platform/amlogic/c3/isp/c3-isp-stats.c   | 1 +
 3 files changed, 3 insertions(+)
drivers/media/platform/amlogic/c3/isp/c3-isp-capture.c +1 -0
index 11d85f5342f0..da0506548d34 100644
@@ -502,6 +502,7 @@ static const struct v4l2_file_operations isp_cap_v4l2_fops = {
 	.poll = vb2_fop_poll,
 	.unlocked_ioctl = video_ioctl2,
 	.mmap = vb2_fop_mmap,
+	.owner = THIS_MODULE,
 };
 
 static int c3_isp_cap_link_validate(struct media_link *link)
drivers/media/platform/amlogic/c3/isp/c3-isp-params.c +1 -0
index ae0777a20bda..686f22c99d69 100644
@@ -764,6 +764,7 @@ static const struct v4l2_file_operations isp_params_v4l2_fops = {
 	.poll = vb2_fop_poll,
 	.unlocked_ioctl = video_ioctl2,
 	.mmap = vb2_fop_mmap,
+	.owner = THIS_MODULE,
 };
 
 static int c3_isp_params_vb2_queue_setup(struct vb2_queue *q,
drivers/media/platform/amlogic/c3/isp/c3-isp-stats.c +1 -0
index 8a5d7e1a30c9..888a4a6434e8 100644
@@ -140,6 +140,7 @@ static const struct v4l2_file_operations isp_stats_v4l2_fops = {
 	.poll = vb2_fop_poll,
 	.unlocked_ioctl = video_ioctl2,
 	.mmap = vb2_fop_mmap,
+	.owner = THIS_MODULE,
 };
 
 static int c3_isp_stats_vb2_queue_setup(struct vb2_queue *q,
Signature · 1 line
2.56.1.839.g22a8290db9-goog
maintainer
#2 lore raw

Re: [PATCH 1/5] media: amlogic: c3-isp: Set .owner = THIS_MODULE in v4l2_file_operations

Quoted 10 lines · Ian Rogers

video_register_device() passes vdev->fops->owner to __video_register_device(), which sets vdev->cdev->owner = owner so chrdev_open() pins the V4L2 driver module while the device node is open. When .owner is omitted in a const struct v4l2_file_operations, vdev->cdev->owner is NULL and the driver module is not reference counted on open().

Set .owner = THIS_MODULE in isp_cap_v4l2_fops, isp_params_v4l2_fops, and isp_stats_v4l2_fops, found with Clang's require_explicit_initialization attribute.

This sounds like a game of whack-a-mole. A better solution would be to improve the API to make this error impossible in the first place. I'll send an RFC.

Quoted 44 lines
Fixes fb2e135208f3 ("media: platform: Add C3 ISP driver")
---
 drivers/media/platform/amlogic/c3/isp/c3-isp-capture.c | 1 +
 drivers/media/platform/amlogic/c3/isp/c3-isp-params.c  | 1 +
 drivers/media/platform/amlogic/c3/isp/c3-isp-stats.c   | 1 +
 3 files changed, 3 insertions(+)
drivers/media/platform/amlogic/c3/isp/c3-isp-capture.c +1 -0
index 11d85f5342f0..da0506548d34 100644
@@ -502,6 +502,7 @@ static const struct v4l2_file_operations isp_cap_v4l2_fops = {
 	.poll = vb2_fop_poll,
 	.unlocked_ioctl = video_ioctl2,
 	.mmap = vb2_fop_mmap,
+	.owner = THIS_MODULE,
 };
 
 static int c3_isp_cap_link_validate(struct media_link *link)
drivers/media/platform/amlogic/c3/isp/c3-isp-params.c +1 -0
index ae0777a20bda..686f22c99d69 100644
@@ -764,6 +764,7 @@ static const struct v4l2_file_operations isp_params_v4l2_fops = {
 	.poll = vb2_fop_poll,
 	.unlocked_ioctl = video_ioctl2,
 	.mmap = vb2_fop_mmap,
+	.owner = THIS_MODULE,
 };
 
 static int c3_isp_params_vb2_queue_setup(struct vb2_queue *q,
drivers/media/platform/amlogic/c3/isp/c3-isp-stats.c +1 -0
index 8a5d7e1a30c9..888a4a6434e8 100644
@@ -140,6 +140,7 @@ static const struct v4l2_file_operations isp_stats_v4l2_fops = {
 	.poll = vb2_fop_poll,
 	.unlocked_ioctl = video_ioctl2,
 	.mmap = vb2_fop_mmap,
+	.owner = THIS_MODULE,
 };
 
 static int c3_isp_stats_vb2_queue_setup(struct vb2_queue *q,
Signature · 3 lines
Regards,

Laurent Pinchart
author
#3 lore raw

Re: [PATCH 1/5] media: amlogic: c3-isp: Set .owner = THIS_MODULE in v4l2_file_operations

Quoted 9 lines · Laurent Pinchart
Quoted 7 lines · Ian Rogers

video_register_device() passes vdev->fops->owner to __video_register_device(), which sets vdev->cdev->owner = owner so chrdev_open() pins the V4L2 driver module while the device node is open. When .owner is omitted in a const struct v4l2_file_operations, vdev->cdev->owner is NULL and the driver module is not reference counted on open().

Quoted 7 lines
Quoted 3 lines

Set .owner = THIS_MODULE in isp_cap_v4l2_fops, isp_params_v4l2_fops, and isp_stats_v4l2_fops, found with Clang's require_explicit_initialization attribute.

This sounds like a game of whack-a-mole. A better solution would be to improve the API to make this error impossible in the first place. I'll send an RFC.

So the intent is to use __must_init an attribute that will cause compilation with clang to fail if the given variable isn't initialized when the struct is created. This should make the error impossible, but before I can add the attribute I need to fix the places where initialization is missing.

For context, I gave this presentation at LPC:
https://lpc.events/event/20/contributions/2392/
And the series adding the attribute is here, with the first user being kobject::release:
https://lore.kernel.org/lkml/[email protected]/

Thanks,
Ian

Quoted 50 lines
Quoted 38 lines
Fixes fb2e135208f3 ("media: platform: Add C3 ISP driver")
---
 drivers/media/platform/amlogic/c3/isp/c3-isp-capture.c | 1 +
 drivers/media/platform/amlogic/c3/isp/c3-isp-params.c  | 1 +
 drivers/media/platform/amlogic/c3/isp/c3-isp-stats.c   | 1 +
 3 files changed, 3 insertions(+)
drivers/media/platform/amlogic/c3/isp/c3-isp-capture.c +1 -0
index 11d85f5342f0..da0506548d34 100644
@@ -502,6 +502,7 @@ static const struct v4l2_file_operations isp_cap_v4l2_fops = {
      .poll = vb2_fop_poll,
      .unlocked_ioctl = video_ioctl2,
      .mmap = vb2_fop_mmap,
+     .owner = THIS_MODULE,
 };
 
 static int c3_isp_cap_link_validate(struct media_link *link)
drivers/media/platform/amlogic/c3/isp/c3-isp-params.c +1 -0
index ae0777a20bda..686f22c99d69 100644
@@ -764,6 +764,7 @@ static const struct v4l2_file_operations isp_params_v4l2_fops = {
      .poll = vb2_fop_poll,
      .unlocked_ioctl = video_ioctl2,
      .mmap = vb2_fop_mmap,
+     .owner = THIS_MODULE,
 };
 
 static int c3_isp_params_vb2_queue_setup(struct vb2_queue *q,
drivers/media/platform/amlogic/c3/isp/c3-isp-stats.c +0 -0
index 8a5d7e1a30c9..888a4a6434e8 100644

drivers/media/platform/amlogic/c3/isp/c3-isp-stats.c static const struct v4l2_file_operations isp_stats_v4l2_fops = { · line 146

Quoted 7 lines
drivers/media/platform/amlogic/c3/isp/c3-isp-stats.c +1 -0
@@ -140,6 +140,7 @@ static const struct v4l2_file_operations isp_stats_v4l2_fops = {
      .poll = vb2_fop_poll,
      .unlocked_ioctl = video_ioctl2,
      .mmap = vb2_fop_mmap,
+     .owner = THIS_MODULE,
 };
 
 static int c3_isp_stats_vb2_queue_setup(struct vb2_queue *q,

--
Regards,

Laurent Pinchart

author
#4 lore raw

PATCH 2/5 media: rockchip: rkcif: Set .owner = THIS_MODULE in v4l2_file_operations

In reply to Ian Rogers

video_register_device() passes vdev->fops->owner to __video_register_device(), which sets vdev->cdev->owner = owner so chrdev_open() pins the V4L2 driver module while the device node is open. When .owner is omitted in a const struct v4l2_file_operations, vdev->cdev->owner is NULL and the driver module is not reference counted on open().

Set .owner = THIS_MODULE in rkcif_stream_file_ops, found with Clang's require_explicit_initialization attribute.

Fixes 501802e2ad51 ("media: rockchip: rkcif: add abstraction for dma blocks")
---
 drivers/media/platform/rockchip/rkcif/rkcif-stream.c | 1 +
 1 file changed, 1 insertion(+)
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +1 -0
index 3130d420ad55..329c38d284f1 100644
@@ -506,6 +506,7 @@ static const struct v4l2_file_operations rkcif_stream_file_ops = {
 	.unlocked_ioctl = video_ioctl2,
 	.poll = vb2_fop_poll,
 	.mmap = vb2_fop_mmap,
+	.owner = THIS_MODULE,
 };
 
 static int rkcif_stream_init_vb2_queue(struct vb2_queue *q,
Signature · 1 line
2.56.1.839.g22a8290db9-goog
maintainer Reviewed-by on v1 2/5
#5 lore raw

Re: [PATCH 2/5] media: rockchip: rkcif: Set .owner = THIS_MODULE in v4l2_file_operations

Hi Ian,

Thanks for your patch!

Quoted 7 lines · Ian Rogers

video_register_device() passes vdev->fops->owner to __video_register_device(), which sets vdev->cdev->owner = owner so chrdev_open() pins the V4L2 driver module while the device node is open. When .owner is omitted in a const struct v4l2_file_operations, vdev->cdev->owner is NULL and the driver module is not reference counted on open().

Quoted 5 lines

Set .owner = THIS_MODULE in rkcif_stream_file_ops, found with Clang's require_explicit_initialization attribute.

Fixes 501802e2ad51 ("media: rockchip: rkcif: add abstraction for dma blocks")

Best regards,
Michael

Quoted 16 lines
---
 drivers/media/platform/rockchip/rkcif/rkcif-stream.c | 1 +
 1 file changed, 1 insertion(+)
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +1 -0
index 3130d420ad55..329c38d284f1 100644
@@ -506,6 +506,7 @@ static const struct v4l2_file_operations rkcif_stream_file_ops = {
 	.unlocked_ioctl = video_ioctl2,
 	.poll = vb2_fop_poll,
 	.mmap = vb2_fop_mmap,
+	.owner = THIS_MODULE,
 };
 
 static int rkcif_stream_init_vb2_queue(struct vb2_queue *q,
author
#6 lore raw

PATCH 3/5 media: rkisp1: Set .owner = THIS_MODULE in v4l2_file_operations

In reply to Ian Rogers

video_register_device() passes vdev->fops->owner to __video_register_device(), which sets vdev->cdev->owner = owner so chrdev_open() pins the V4L2 driver module while the device node is open. When .owner is omitted in a const struct v4l2_file_operations, vdev->cdev->owner is NULL and the driver module is not reference counted on open().

Set .owner = THIS_MODULE in rkisp1_fops, rkisp1_params_fops, and rkisp1_stats_fops, found with Clang's require_explicit_initialization attribute.

Fixes 56e3b29f9f6b ("media: staging: rkisp1: add streaming paths")
Fixes bae1155cf579 ("media: staging: rkisp1: add output device for parameters")
Fixes 9a28dbd65a88 ("media: staging: rkisp1: add capture device for statistics")
---
 drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c | 1 +
 drivers/media/platform/rockchip/rkisp1/rkisp1-params.c  | 3 ++-
 drivers/media/platform/rockchip/rkisp1/rkisp1-stats.c   | 3 ++-
 3 files changed, 5 insertions(+), 2 deletions(-)
drivers/media/platform/rockchip/rkisp1/rkisp1-capture.c +1 -0
index 20d88865081c..29a1f99046f0 100644
@@ -1503,6 +1503,7 @@ static const struct v4l2_file_operations rkisp1_fops = {
 	.unlocked_ioctl = video_ioctl2,
 	.poll = vb2_fop_poll,
 	.mmap = vb2_fop_mmap,
+	.owner = THIS_MODULE,
 };
 
 static void rkisp1_unregister_capture(struct rkisp1_capture *cap)
drivers/media/platform/rockchip/rkisp1/rkisp1-params.c +2 -1
index 042b759eba62..034470afadfd 100644
@@ -2806,7 +2806,8 @@ static const struct v4l2_file_operations rkisp1_params_fops = {
 	.unlocked_ioctl = video_ioctl2,
 	.poll = vb2_fop_poll,
 	.open = v4l2_fh_open,
-	.release = vb2_fop_release
+	.release = vb2_fop_release,
+	.owner = THIS_MODULE,
 };
 
 static int rkisp1_params_init_vb2_queue(struct vb2_queue *q,
drivers/media/platform/rockchip/rkisp1/rkisp1-stats.c +2 -1
index d5fdb8f82dc7..02dc144152e5 100644
@@ -85,7 +85,8 @@ static const struct v4l2_file_operations rkisp1_stats_fops = {
 	.unlocked_ioctl = video_ioctl2,
 	.poll = vb2_fop_poll,
 	.open = v4l2_fh_open,
-	.release = vb2_fop_release
+	.release = vb2_fop_release,
+	.owner = THIS_MODULE,
 };
 
 static int rkisp1_stats_vb2_queue_setup(struct vb2_queue *vq,
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#7 lore raw

PATCH 4/5 staging: media: ipu3: Set .owner = THIS_MODULE in v4l2_file_operations

In reply to Ian Rogers

video_register_device() passes vdev->fops->owner to __video_register_device(), which sets vdev->cdev->owner = owner so chrdev_open() pins the V4L2 driver module while the device node is open. When .owner is omitted in a const struct v4l2_file_operations, vdev->cdev->owner is NULL and the driver module is not reference counted on open().

Set .owner = THIS_MODULE in imgu_v4l2_fops, found with Clang's require_explicit_initialization attribute.

Fixes a0ca1627b450 ("media: staging/intel-ipu3: Add v4l2 driver based on media framework")
---
 drivers/staging/media/ipu3/ipu3-v4l2.c | 1 +
 1 file changed, 1 insertion(+)
drivers/staging/media/ipu3/ipu3-v4l2.c +1 -0
index 61fdefe78c3d..12b808c2bf7e 100644
@@ -951,6 +951,7 @@ static const struct v4l2_file_operations imgu_v4l2_fops = {
 	.release = vb2_fop_release,
 	.poll = vb2_fop_poll,
 	.mmap = vb2_fop_mmap,
+	.owner = THIS_MODULE,
 };
 
 /******************** v4l2_ioctl_ops ********************/
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#8 lore raw

PATCH 5/5 media: sunxi: a83t-mipi-csi2: Set .owner = THIS_MODULE in phy_ops

In reply to Ian Rogers

phy_get() and of_phy_get() call try_module_get(phy->ops->owner) to pin the PHY provider module while the PHY is in use, and of_phy_put() calls module_put(phy->ops->owner). sun8i-a83t-mipi-csi2 can be built as a module (CONFIG_VIDEO_SUN8I_A83T_MIPI_CSI2), so omitting .owner leaves phy->ops->owner NULL.

Set .owner = THIS_MODULE in sun8i_a83t_dphy_ops, found with Clang's require_explicit_initialization attribute.

Fixes 576d196c522b ("media: sunxi: Add support for the A83T MIPI CSI-2 controller")
---
 .../media/platform/sunxi/sun8i-a83t-mipi-csi2/sun8i_a83t_dphy.c  | 1 +
 1 file changed, 1 insertion(+)
drivers/media/platform/sunxi/sun8i-a83t-mipi-csi2/sun8i_a83t_dphy.c +1 -0
index 24bbcc85013d..b14982df5f25 100644
@@ -47,6 +47,7 @@ static const struct phy_ops sun8i_a83t_dphy_ops = {
 	.configure	= sun8i_a83t_dphy_configure,
 	.power_on	= sun8i_a83t_dphy_power_on,
 	.power_off	= sun8i_a83t_dphy_power_off,
+	.owner		= THIS_MODULE,
 };
 
 int sun8i_a83t_dphy_register(struct sun8i_a83t_mipi_csi2_device *csi2_dev)
Signature · 1 line
2.56.1.839.g22a8290db9-goog