[PATCH 03/11] media: Add tgid and fd fields in v4l2_fh struct

Nicolas Dufresne nicolas.dufresne at collabora.com
Tue Apr 28 12:31:37 PDT 2026


Le jeudi 12 février 2026 à 11:23 -0500, Detlev Casanova a écrit :
> These fields will be used in traces to help userspace tracing tools
> identify streams.
> 
> The tgid field will keep the PID of the process that opened the video
> file.
> That is needed because trace calls can happen in IRQs, for which there is
> no current PID.
> 
> The fd field helps identify the context in case the same process opens the
> video device multiple times.
> Note that the fd field is set in the __video_do_ioctl() function.
> That is because the file descriptor has not been allocated yet when
> v4l2_open() is called.

Unless someone have a better idea with this last part (in this case please
reply):

Reviewed-by: Nicolas Dufresne <nicolas.dufresne at collabora.com>

> 
> Signed-off-by: Detlev Casanova <detlev.casanova at collabora.com>
> ---
>  drivers/media/v4l2-core/v4l2-fh.c    |  1 +
>  drivers/media/v4l2-core/v4l2-ioctl.c | 17 +++++++++++++++++
>  include/media/v4l2-fh.h              |  4 ++++
>  3 files changed, 22 insertions(+)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-fh.c b/drivers/media/v4l2-core/v4l2-
> fh.c
> index df3ba9d4674b..86e8223b46cb 100644
> --- a/drivers/media/v4l2-core/v4l2-fh.c
> +++ b/drivers/media/v4l2-core/v4l2-fh.c
> @@ -37,6 +37,7 @@ void v4l2_fh_init(struct v4l2_fh *fh, struct video_device
> *vdev)
>  	INIT_LIST_HEAD(&fh->available);
>  	INIT_LIST_HEAD(&fh->subscribed);
>  	fh->sequence = -1;
> +	fh->tgid = current->tgid;
>  	mutex_init(&fh->subscribe_lock);
>  }
>  EXPORT_SYMBOL_GPL(v4l2_fh_init);
> diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-
> core/v4l2-ioctl.c
> index 37d33d4a363d..a3b6df0571d6 100644
> --- a/drivers/media/v4l2-core/v4l2-ioctl.c
> +++ b/drivers/media/v4l2-core/v4l2-ioctl.c
> @@ -9,6 +9,7 @@
>   */
>  
>  #include <linux/compat.h>
> +#include <linux/fdtable.h>
>  #include <linux/mm.h>
>  #include <linux/module.h>
>  #include <linux/slab.h>
> @@ -3061,6 +3062,16 @@ void v4l_printk_ioctl(const char *prefix, unsigned int
> cmd)
>  }
>  EXPORT_SYMBOL(v4l_printk_ioctl);
>  
> +static int _file_iterate(const void *priv, struct file *filp, unsigned int
> fd)
> +{
> +	const struct file *fh_filp = priv;
> +
> +	if (fh_filp == filp)
> +		return fd;
> +
> +	return 0;
> +}
> +
>  static long __video_do_ioctl(struct file *file,
>  		unsigned int cmd, void *arg)
>  {
> @@ -3081,6 +3092,12 @@ static long __video_do_ioctl(struct file *file,
>  		return ret;
>  	}
>  
> +	if (unlikely(!vfh->fd)) {
> +		vfh->fd = iterate_fd(current->files, 0, _file_iterate, file);
> +		if (!vfh->fd)
> +			vfh->fd = -1;
> +	}
> +
>  	/*
>  	 * We need to serialize streamon/off with queueing new requests.
>  	 * These ioctls may trigger the cancellation of a streaming
> diff --git a/include/media/v4l2-fh.h b/include/media/v4l2-fh.h
> index aad4b3689d7e..4ef4e58ab8d1 100644
> --- a/include/media/v4l2-fh.h
> +++ b/include/media/v4l2-fh.h
> @@ -28,6 +28,8 @@ struct v4l2_ctrl_handler;
>   * @vdev: pointer to &struct video_device
>   * @ctrl_handler: pointer to &struct v4l2_ctrl_handler
>   * @prio: priority of the file handler, as defined by &enum v4l2_priority
> + * @tgid: process id that initialized the v4l2_fh
> + * @fd: file descriptor associated to this v4l2_fh for the process id in tgid
>   *
>   * @wait: event' s wait queue
>   * @subscribe_lock: serialise changes to the subscribed list; guarantee that
> @@ -44,6 +46,8 @@ struct v4l2_fh {
>  	struct video_device	*vdev;
>  	struct v4l2_ctrl_handler *ctrl_handler;
>  	enum v4l2_priority	prio;
> +	uint32_t		tgid;
> +	int			fd;
>  
>  	/* Events */
>  	wait_queue_head_t	wait;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: This is a digitally signed message part
URL: <http://lists.infradead.org/pipermail/linux-rockchip/attachments/20260428/38e97597/attachment.sig>


More information about the Linux-rockchip mailing list