Re: [PATCH 11/65] media: Replace file->private_data access with custom functions
From: Hans Verkuil <hverkuil+cisco@kernel.org>
Date: 2025-08-06 08:16:59
Also in:
imx, linux-amlogic, linux-arm-msm, linux-media, linux-mediatek, linux-renesas-soc, linux-rockchip, linux-samsung-soc, linux-staging, linux-sunxi, linux-tegra, linux-usb, lkml
On 02/08/2025 11:22, Jacopo Mondi wrote:
quoted hunk ↗ jump to hunk
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Accessing file->private_data manually to retrieve the v4l2_fh pointer is error-prone, as the field is a void * and will happily cast implicitly to any pointer type. Replace all remaining locations that read the v4l2_fh pointer directly from file->private_data and cast it to driver-specific file handle structures with driver-specific functions that use file_to_v4l2_fh() and perform the same cast. No functional change is intended, this only paves the way to remove direct accesses to file->private_data and make V4L2 drivers safer. Other accesses to the field will be addressed separately. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com> --- drivers/media/pci/ivtv/ivtv-driver.h | 5 ++++ drivers/media/pci/ivtv/ivtv-fileops.c | 10 +++---- drivers/media/pci/ivtv/ivtv-ioctl.c | 8 +++--- drivers/media/platform/allegro-dvt/allegro-core.c | 7 ++++- drivers/media/platform/amlogic/meson-ge2d/ge2d.c | 8 ++++-- .../media/platform/chips-media/coda/coda-common.c | 7 ++++- .../platform/chips-media/wave5/wave5-helper.c | 2 +- .../media/platform/chips-media/wave5/wave5-vpu.h | 5 ++++ drivers/media/platform/m2m-deinterlace.c | 7 ++++- .../media/platform/mediatek/jpeg/mtk_jpeg_core.c | 7 ++++- drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c | 7 ++++- .../media/platform/mediatek/mdp3/mtk-mdp3-m2m.c | 7 ++++- .../mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c | 2 +- .../mediatek/vcodec/decoder/mtk_vcodec_dec_drv.h | 5 ++++ .../mediatek/vcodec/encoder/mtk_vcodec_enc_drv.c | 2 +- .../mediatek/vcodec/encoder/mtk_vcodec_enc_drv.h | 5 ++++ drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c | 7 ++++- drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c | 7 ++++- drivers/media/platform/nxp/mx2_emmaprp.c | 7 ++++- drivers/media/platform/renesas/rcar_fdp1.c | 7 ++++- drivers/media/platform/renesas/rcar_jpu.c | 7 ++++- drivers/media/platform/rockchip/rga/rga.c | 3 +-- drivers/media/platform/rockchip/rga/rga.h | 5 ++++ drivers/media/platform/rockchip/rkvdec/rkvdec.c | 2 +- drivers/media/platform/rockchip/rkvdec/rkvdec.h | 5 ++++ .../media/platform/samsung/exynos-gsc/gsc-core.h | 6 +++++ .../media/platform/samsung/exynos-gsc/gsc-m2m.c | 6 ++--- .../media/platform/samsung/exynos4-is/fimc-core.h | 5 ++++ .../media/platform/samsung/exynos4-is/fimc-m2m.c | 2 +- drivers/media/platform/samsung/s5p-g2d/g2d.c | 7 +++-- .../media/platform/samsung/s5p-jpeg/jpeg-core.c | 9 +++++-- drivers/media/platform/samsung/s5p-mfc/s5p_mfc.c | 6 ++--- .../platform/samsung/s5p-mfc/s5p_mfc_common.h | 6 +++++ drivers/media/platform/st/sti/bdisp/bdisp-v4l2.c | 7 ++++- drivers/media/platform/st/sti/delta/delta-v4l2.c | 26 +++++++++++------- drivers/media/platform/st/sti/hva/hva-v4l2.c | 31 ++++++++++++---------- drivers/media/platform/st/sti/hva/hva.h | 2 -- drivers/media/platform/st/stm32/dma2d/dma2d.c | 7 +++-- drivers/media/platform/sunxi/sun8i-di/sun8i-di.c | 3 +-- .../platform/sunxi/sun8i-rotate/sun8i_rotate.c | 3 +-- drivers/media/platform/ti/omap3isp/ispvideo.c | 4 +-- drivers/media/platform/ti/omap3isp/ispvideo.h | 6 +++++ drivers/media/platform/verisilicon/hantro.h | 5 ++++ drivers/media/platform/verisilicon/hantro_drv.c | 3 +-- drivers/staging/media/imx/imx-media-csc-scaler.c | 7 ++++- drivers/staging/media/meson/vdec/vdec.c | 24 ++++++----------- drivers/staging/media/meson/vdec/vdec.h | 5 ++++ drivers/staging/media/sunxi/cedrus/cedrus.c | 3 +-- drivers/staging/media/sunxi/cedrus/cedrus.h | 5 ++++ drivers/staging/media/sunxi/cedrus/cedrus_video.c | 5 ---- 50 files changed, 237 insertions(+), 100 deletions(-)diff --git a/drivers/media/pci/ivtv/ivtv-driver.h b/drivers/media/pci/ivtv/ivtv-driver.h index a6ffa99e16bc64a5b7d3e48c1ab32b49a7989242..cad548b28e360ecfe2bcb9fcb5d12cd8823c3727 100644 --- a/drivers/media/pci/ivtv/ivtv-driver.h +++ b/drivers/media/pci/ivtv/ivtv-driver.h@@ -388,6 +388,11 @@ static inline struct ivtv_open_id *fh2id(struct v4l2_fh *fh) return container_of(fh, struct ivtv_open_id, fh); } +static inline struct ivtv_open_id *file2id(struct file *filp) +{ + return container_of(file_to_v4l2_fh(filp), struct ivtv_open_id, fh);
Why not write: return fh2id(file_to_v4l2_fh(filp)); Same for all other drivers that do this. I prefer to have the contained_of() in just one place.
+}
+
struct yuv_frame_info
{
u32 update;<snip>
quoted hunk ↗ jump to hunk
diff --git a/drivers/media/platform/allegro-dvt/allegro-core.c b/drivers/media/platform/allegro-dvt/allegro-core.c index 1f134e08923a528cc676f825da68951c97ac2f25..74977f3ae4844022c04de877f31b4fc6aaac0749 100644 --- a/drivers/media/platform/allegro-dvt/allegro-core.c +++ b/drivers/media/platform/allegro-dvt/allegro-core.c@@ -302,6 +302,11 @@ struct allegro_channel { unsigned int error; }; +static inline struct allegro_channel *file_to_channel(struct file *filp) +{ + return container_of(file_to_v4l2_fh(filp), struct allegro_channel, fh); +} + static inline int allegro_channel_get_i_frame_qp(struct allegro_channel *channel) {@@ -3229,7 +3234,7 @@ static int allegro_open(struct file *file) static int allegro_release(struct file *file) { - struct allegro_channel *channel = fh_to_channel(file->private_data); + struct allegro_channel *channel = file_to_channel(file);
So a file_to_channel inline function was added, but it is used in just one place. I would prefer to just drop the inline function and instead write: struct allegro_channel *channel = fh_to_channel(file_to_v4l2_fh(file)); If this is needed in two or more places, then the extra inline makes sense, but it is a fairly common pattern that it is only needed in the release function. Adding a new inline just for that seems overkill to me.
v4l2_m2m_ctx_release(channel->fh.m2m_ctx);
Regards, Hans