All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Detlev Casanova" <detlev.casanova@collabora.com>
Cc: linux-trace-kernel@vger.kernel.org, media-ci@linuxtv.org
Subject: Re: [PATCH v3 3/9] media: Add tgid and fd fields in v4l2_fh struct
Date: Wed, 25 Nov 2026 18:59:09 +0000	[thread overview]
Message-ID: <sashiko-outbox-234026@kernel.org> (raw)
In-Reply-To: <20261125-v4l2-add-ftrace-v3-3-1d1db48372ed@collabora.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] media: v4l2: file descriptor 0 misrecorded as -1 in
  __video_do_ioctl()
- [Low] media: v4l2: mismatched tgid and fd across processes in
  __video_do_ioctl()
- [Low] media: v4l2: unlocked data race on vfh->fd init in
  __video_do_ioctl()
--

commit bce5397775deae4092f0cee0059ee45f433e0773
Author: Detlev Casanova <detlev.casanova@collabora.com>

media: Add tgid and fd fields in v4l2_fh struct

This commit adds tgid and fd fields to struct v4l2_fh, recording the
opening process's tgid in v4l2_fh_init() and resolving the file descriptor
number via iterate_fd() on the first ioctl in __video_do_ioctl().

> diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
> index 17ba1ae707355..5d7fbaa1b41a1 100644
> --- a/drivers/media/v4l2-core/v4l2-ioctl.c
> +++ b/drivers/media/v4l2-core/v4l2-ioctl.c
[ ... ]
> @@ -3063,6 +3064,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)
>  {
> @@ -3083,6 +3094,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;
> +	}

[Severity: Medium]
Does this misrecord valid file descriptor 0 as -1?

iterate_fd() stops only when the callback returns a non-zero value:

fs/file.c:iterate_fd() {
	...
	for (fdt = files_fdtable(files); n < fdt->max_fds; n++) {
		...
		res = f(p, file, n);
		if (res)
			break;
	}
	...
}

When a video device is opened at fd 0 (for example, in a daemon or test
process that closed stdin), _file_iterate() returns 0 on match, so
iterate_fd() treats it as a non-match, scans the rest of the fd table
under files->file_lock, and returns 0.  __video_do_ioctl() then sees
!vfh->fd and overwrites vfh->fd with -1.

Initializing fh->fd to -1 in v4l2_fh_init(), checking vfh->fd < 0, and
returning fd + 1 from _file_iterate() (then subtracting 1) would handle
fd 0 properly.

[Severity: Low]
Can concurrent ioctls on the same struct file race on vfh->fd here?

This initialization runs before v4l2_ioctl_get_lock() is acquired, so two
threads calling ioctl() on the same open file description can read and
write vfh->fd concurrently across the two-step assignment, triggering a
KCSAN data race and potentially observing an intermediate 0 value.

> diff --git a/include/media/v4l2-fh.h b/include/media/v4l2-fh.h
> index aad4b3689d7ea..080cb00e3867b 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

[Severity: Low]
Does vfh->fd always belong to the process identified by vfh->tgid?

v4l2_fh_init() records current->tgid at open() time, whereas
__video_do_ioctl() looks up vfh->fd in current->files of whichever process
issues the first ioctl without checking current->tgid == vfh->tgid.  If
the open file is inherited across fork() or passed via SCM_RIGHTS before
the first ioctl, vfh->tgid and vfh->fd will refer to different processes.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-v4l2-add-ftrace-v3-0-1d1db48372ed@collabora.com?part=3

  reply	other threads:[~2026-11-25 18:59 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 18:33 [PATCH v3 0/9] v4l2: Add tracing for stateless codecs Detlev Casanova
2026-11-25 18:33 ` Detlev Casanova
2026-11-25 18:33 ` [PATCH v3 1/9] media: Move visl traces to v4l2-core Detlev Casanova
2026-11-25 18:33   ` Detlev Casanova
2026-11-25 18:58   ` sashiko-bot
2026-11-25 18:33 ` [PATCH v3 2/9] media: Map each struct field to its own trace field Detlev Casanova
2026-11-25 18:33   ` Detlev Casanova
2026-11-25 18:33 ` [PATCH v3 3/9] media: Add tgid and fd fields in v4l2_fh struct Detlev Casanova
2026-11-25 18:33   ` Detlev Casanova
2026-11-25 18:59   ` sashiko-bot [this message]
2026-11-25 18:33 ` [PATCH v3 4/9] media: Add tgid and fd to the v4l2-requests trace fields Detlev Casanova
2026-11-25 18:33   ` Detlev Casanova
2026-11-25 18:56   ` sashiko-bot
2026-11-25 18:33 ` [PATCH v3 5/9] media: Add missing types to v4l2_ctrl_ptr Detlev Casanova
2026-11-25 18:33   ` Detlev Casanova
2026-11-25 18:33 ` [PATCH v3 6/9] media: Trace the stateless controls when set in v4l2-ctrls-core.c Detlev Casanova
2026-11-25 18:33   ` Detlev Casanova
2026-11-25 19:00   ` sashiko-bot
2026-11-25 18:33 ` [PATCH v3 7/9] media: Add stream on/off traces and run them in the ioctl Detlev Casanova
2026-11-25 18:33   ` Detlev Casanova
2026-11-25 18:57   ` sashiko-bot
2026-11-25 18:33 ` [PATCH v3 8/9] media: Add HW run/done trace events Detlev Casanova
2026-11-25 18:33   ` Detlev Casanova
2026-11-25 18:33 ` [PATCH v3 9/9] media: hantro: Add v4l2_hw run/done traces Detlev Casanova
2026-11-25 18:33   ` Detlev Casanova
2026-11-25 19:01   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-234026@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=detlev.casanova@collabora.com \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=media-ci@linuxtv.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.