media: Set missing .owner = THIS_MODULE in V4L2 and D-PHY ops
Subsystems (8)
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
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.
--- 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
@@ -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
@@ -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
@@ -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
Laurent Pinchart
maintainerRe: [PATCH 1/5] media: amlogic: c3-isp: Set .owner = THIS_MODULE in v4l2_file_operations
Quoted 10 lines · Ian Rogers
video_register_device()passesvdev->fops->ownerto__video_register_device(), which setsvdev->cdev->owner= owner sochrdev_open()pins the V4L2 driver module while the device node is open. When .owner is omitted in a const structv4l2_file_operations,vdev->cdev->owneris NULL and the driver module is not reference counted onopen().Set .owner =
THIS_MODULEinisp_cap_v4l2_fops,isp_params_v4l2_fops, andisp_stats_v4l2_fops, found with Clang'srequire_explicit_initializationattribute.
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")Signed-off-by Ian Rogers <[email protected]>--- 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@@ -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@@ -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@@ -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
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()passesvdev->fops->ownerto__video_register_device(), which setsvdev->cdev->owner= owner sochrdev_open()pins the V4L2 driver module while the device node is open. When .owner is omitted in a const structv4l2_file_operations,vdev->cdev->owneris NULL and the driver module is not reference counted onopen().
Quoted 7 lines
Quoted 3 lines
Set .owner =
THIS_MODULEinisp_cap_v4l2_fops,isp_params_v4l2_fops, andisp_stats_v4l2_fops, found with Clang'srequire_explicit_initializationattribute.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")Signed-off-by Ian Rogers <[email protected]>--- 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@@ -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@@ -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
-0drivers/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
PATCH 2/5 media: rockchip: rkcif: Set .owner = THIS_MODULE in v4l2_file_operations
In reply to Ian Rogersvideo_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.
--- drivers/media/platform/rockchip/rkcif/rkcif-stream.c | 1 + 1 file changed, 1 insertion(+)
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +1 -0
@@ -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
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()passesvdev->fops->ownerto__video_register_device(), which setsvdev->cdev->owner= owner sochrdev_open()pins the V4L2 driver module while the device node is open. When .owner is omitted in a const structv4l2_file_operations,vdev->cdev->owneris NULL and the driver module is not reference counted onopen().
Quoted 5 lines
Set .owner =
THIS_MODULEinrkcif_stream_file_ops, found with Clang'srequire_explicit_initializationattribute.Fixes 501802e2ad51 ("media: rockchip: rkcif: add abstraction for dma blocks")Signed-off-by Ian Rogers <[email protected]>
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@@ -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,
PATCH 3/5 media: rkisp1: Set .owner = THIS_MODULE in v4l2_file_operations
In reply to Ian Rogersvideo_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.
--- 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
@@ -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
@@ -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
@@ -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
PATCH 4/5 staging: media: ipu3: Set .owner = THIS_MODULE in v4l2_file_operations
In reply to Ian Rogersvideo_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.
--- drivers/staging/media/ipu3/ipu3-v4l2.c | 1 + 1 file changed, 1 insertion(+)
drivers/staging/media/ipu3/ipu3-v4l2.c +1 -0
@@ -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
PATCH 5/5 media: sunxi: a83t-mipi-csi2: Set .owner = THIS_MODULE in phy_ops
In reply to Ian Rogersphy_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.
--- .../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
@@ -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