[PATCH v4 7/9] media: Add stream on/off traces and run them in the ioctl

From: Detlev Casanova

Date: Fri Nov 27 2026 - 11:46:16 EST


Automatically trace stream on/off events for all v4l2 drivers.

The queue type is also logged so that userspace can identify the possible
multiple stream on/off events per stream.

Signed-off-by: Detlev Casanova <detlev.casanova@xxxxxxxxxxxxx>
---
drivers/media/v4l2-core/v4l2-ioctl.c | 22 +++++++++++++++++++--
include/trace/events/v4l2.h | 37 ++++++++++++++++++++++++++++++++++++
2 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
index 29ca2f7959ef..71dec6618d08 100644
--- a/drivers/media/v4l2-core/v4l2-ioctl.c
+++ b/drivers/media/v4l2-core/v4l2-ioctl.c
@@ -1965,13 +1965,31 @@ static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops, struct file *file,
static int v4l_streamon(const struct v4l2_ioctl_ops *ops, struct file *file,
void *arg)
{
- return ops->vidioc_streamon(file, NULL, *(unsigned int *)arg);
+ struct v4l2_fh *fh = file_to_v4l2_fh(file);
+ unsigned int type = *(unsigned int *)arg;
+ int err;
+
+ err = ops->vidioc_streamon(file, NULL, type);
+
+ if (!err)
+ trace_v4l2_streamon(fh->tgid, fh->fd, type);
+
+ return err;
}

static int v4l_streamoff(const struct v4l2_ioctl_ops *ops, struct file *file,
void *arg)
{
- return ops->vidioc_streamoff(file, NULL, *(unsigned int *)arg);
+ struct v4l2_fh *fh = file_to_v4l2_fh(file);
+ unsigned int type = *(unsigned int *)arg;
+ int err;
+
+ err = ops->vidioc_streamoff(file, NULL, type);
+
+ if (!err)
+ trace_v4l2_streamoff(fh->tgid, fh->fd, type);
+
+ return err;
}

static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops, struct file *file,
diff --git a/include/trace/events/v4l2.h b/include/trace/events/v4l2.h
index 248bc09bfc99..b11293365123 100644
--- a/include/trace/events/v4l2.h
+++ b/include/trace/events/v4l2.h
@@ -262,6 +262,43 @@ DEFINE_EVENT(vb2_v4l2_event_class, vb2_v4l2_qbuf,
TP_ARGS(q, vb)
);

+/* Events for stream on/off.
+ *
+ * These events will be fired every time userspace starts or stops a stream.
+ * tgid and fd are used to identify the process that opened the video device.
+ * type is the &enum v4l2_buf_type of the queue being started or stopped.
+ *
+ * Note that this even can be fired multiple times for a given tgid/fd pair.
+ * E.g.: mem2mem drivers expect stream on/off on both output and capture queues.
+ */
+DECLARE_EVENT_CLASS(v4l2_stream_class,
+ TP_PROTO(u32 tgid, s32 fd, u32 type),
+ TP_ARGS(tgid, fd, type),
+
+ TP_STRUCT__entry(__field(u32, tgid)
+ __field(s32, fd)
+ __field(u32, type)
+ ),
+
+ TP_fast_assign(__entry->tgid = tgid;
+ __entry->fd = fd;
+ __entry->type = type;
+ ),
+
+ TP_printk("tgid = %u, fd = %d, type = %s",
+ __entry->tgid, __entry->fd, show_type(__entry->type))
+);
+
+DEFINE_EVENT(v4l2_stream_class, v4l2_streamon,
+ TP_PROTO(u32 tgid, s32 fd, u32 type),
+ TP_ARGS(tgid, fd, type)
+);
+
+DEFINE_EVENT(v4l2_stream_class, v4l2_streamoff,
+ TP_PROTO(u32 tgid, s32 fd, u32 type),
+ TP_ARGS(tgid, fd, type)
+);
+
#endif /* if !defined(_TRACE_V4L2_H) || defined(TRACE_HEADER_MULTI_READ) */

/* This part must be outside protection */

--
2.56.1