Skip to contentSkip to conversation

media: v4l2-event: keep place in event list when combining events

PATCH In review Hans Verkuil 3 participants 8 messages Oct 10, 2025 – Dec 2, 2025 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 media: v4l2-event: keep place in event list when combining events

When subscribing to an event a fixed circular buffer of N (N >= 1) events is allocated for that file handle.

New events of the same type are added to that buffer and eventually the oldest event is removed from the buffer when VIDIOC_DQEVENT is called.

If the circular buffer is full, then the event framework will merge the two oldest events (what 'merge' means is event type specific).

So far, so good.

There is also a per-filehandle list of pending events for all event types. VIDIOC_DQEVENT always dequeues the oldest of that event list, and that event is removed from the corresponding circular buffer.

The problem occurs when the circular buffer is full and a new event arrives. Then the two oldest events are merged, but instead of keeping the position in the list of pending events, the merged event is added to the *end* of the list of pending events.

So if a lot of events are generated, then events can continually be pushed to the end of the list of pending events, and so are never or much later dequeued by the application.

Effectively this is a denial-of-service situation were the event is never seen by the application even though there are actually a lot of events.

So if you subscribe to events from control A and B, then change control A, then change control B, then change control A again, and now call VIDIOC_DQEVENT, you will get the event from control B followed by A, even though A was changed first.

This patch keeps the oldest event in its place in the 'pending events' list rather then moving it to the end, and in the test above you will now receive the event from control A first.

Fixes c53c2549333b ("[media] v4l2-event: Add v4l2_subscribed_event_ops")

---
Changes since v1:
- fh->sequence was not incremented when replacing an event. Just drop that change and keep the original code (i.e. incrementing the sequence counter at the beginning).

---
 drivers/media/v4l2-core/v4l2-event.c | 59 +++++++++++++++++++++-------
 1 file changed, 44 insertions(+), 15 deletions(-)
drivers/media/v4l2-core/v4l2-event.c +44 -15
index 3898ff7edddb..919cdd46a2c2 100644
@@ -104,7 +104,6 @@ static void __v4l2_event_queue_fh(struct v4l2_fh *fh,
 {
 	struct v4l2_subscribed_event *sev;
 	struct v4l2_kevent *kev;
-	bool copy_payload = true;
 
 	/* Are we subscribed? */
 	sev = v4l2_event_subscribed(fh, ev->type, ev->id);
@@ -116,29 +115,59 @@ static void __v4l2_event_queue_fh(struct v4l2_fh *fh,
 
 	/* Do we have any free events? */
 	if (sev->in_use == sev->elems) {
-		/* no, remove the oldest one */
-		kev = sev->events + sev_pos(sev, 0);
-		list_del(&kev->list);
-		sev->in_use--;
-		sev->first = sev_pos(sev, 1);
-		fh->navailable--;
+		/*
+		 * No, so we have to make space.
+		 *
+		 * This is a bit tricky: the easy solution is to drop the oldest
+		 * event from the fh->available list and add the new one to
+		 * the end of the list. However, that can lead to situation
+		 * were, if there are a lot of events, the oldest event keeps
+		 * being removed before it can be dequeued by the application.
+		 * Effectively this is a denial-of-service situation were the
+		 * event is never seen by the application even though there are
+		 * actually a lot of events.
+		 *
+		 * So instead we take care to keep the oldest event in its
+		 * place, and instead either replace the event content with
+		 * the new event (if sev->elems == 1) or merge the 2nd oldest
+		 * event with the oldest event.
+		 */
+		struct v4l2_kevent *oldest = sev->events + sev_pos(sev, 0);
+		struct v4l2_kevent *second_oldest;
+
 		if (sev->elems == 1) {
 			if (sev->ops && sev->ops->replace) {
-				sev->ops->replace(&kev->event, ev);
-				copy_payload = false;
+				/* Replace the oldest event with the new event */
+				sev->ops->replace(&oldest->event, ev);
+			} else {
+				oldest->event.u = ev->u;
 			}
-		} else if (sev->ops && sev->ops->merge) {
-			struct v4l2_kevent *second_oldest =
-				sev->events + sev_pos(sev, 0);
-			sev->ops->merge(&kev->event, &second_oldest->event);
+			wake_up_all(&fh->wait);
+			return;
 		}
+		second_oldest = sev->events + sev_pos(sev, 1);
+		if (sev->ops && sev->ops->merge) {
+			/* Merge the oldest event with the 2nd oldest event */
+			sev->ops->merge(&oldest->event, &second_oldest->event);
+		}
+
+		/*
+		 * Replace the oldest event with the second oldest event in the
+		 * event list.
+		 */
+		second_oldest->event.sequence = oldest->event.sequence;
+		second_oldest->ts = oldest->ts;
+		list_del(&second_oldest->list);
+		list_replace_init(&oldest->list, &second_oldest->list);
+		sev->first = sev_pos(sev, 1);
+		sev->in_use--;
+		fh->navailable--;
 	}
 
 	/* Take one and fill it. */
 	kev = sev->events + sev_pos(sev, sev->in_use);
 	kev->event.type = ev->type;
-	if (copy_payload)
-		kev->event.u = ev->u;
+	kev->event.u = ev->u;
 	kev->event.id = ev->id;
 	kev->ts = ts;
 	kev->event.sequence = fh->sequence;
Signature · 1 line
2.51.0
#1 lore raw
Quoted 29 lines · Hans Verkuil

When subscribing to an event a fixed circular buffer of N (N >= 1) events is allocated for that file handle.

New events of the same type are added to that buffer and eventually the oldest event is removed from the buffer when VIDIOC_DQEVENT is called.

If the circular buffer is full, then the event framework will merge the two oldest events (what 'merge' means is event type specific).

So far, so good.

There is also a per-filehandle list of pending events for all event types. VIDIOC_DQEVENT always dequeues the oldest of that event list, and that event is removed from the corresponding circular buffer.

The problem occurs when the circular buffer is full and a new event arrives. Then the two oldest events are merged, but instead of keeping the position in the list of pending events, the merged event is added to the *end* of the list of pending events.

So if a lot of events are generated, then events can continually be pushed to the end of the list of pending events, and so are never or much later dequeued by the application.

Effectively this is a denial-of-service situation were the event is never seen by the application even though there are actually a lot of events.

Quoted 8 lines

So if you subscribe to events from control A and B, then change control A, then change control B, then change control A again, and now call VIDIOC_DQEVENT, you will get the event from control B followed by A, even though A was changed first.

This patch keeps the oldest event in its place in the 'pending events' list rather then moving it to the end, and in the test above you will now receive the event from control A first.

FYI: The same issue is present in the cec subsystem. Far less likely to be a problem for that subsystem, but I'm working on a patch for that as well.

Regards,

Hans

Quoted 99 lines
Fixes c53c2549333b ("[media] v4l2-event: Add v4l2_subscribed_event_ops")

---
Changes since v1:
- fh->sequence was not incremented when replacing an event. Just drop that change and keep the original code (i.e. incrementing the sequence counter at the beginning).

---
 drivers/media/v4l2-core/v4l2-event.c | 59 +++++++++++++++++++++-------
 1 file changed, 44 insertions(+), 15 deletions(-)
drivers/media/v4l2-core/v4l2-event.c +44 -15
index 3898ff7edddb..919cdd46a2c2 100644
@@ -104,7 +104,6 @@ static void __v4l2_event_queue_fh(struct v4l2_fh *fh,
 {
 	struct v4l2_subscribed_event *sev;
 	struct v4l2_kevent *kev;
-	bool copy_payload = true;
 
 	/* Are we subscribed? */
 	sev = v4l2_event_subscribed(fh, ev->type, ev->id);
@@ -116,29 +115,59 @@ static void __v4l2_event_queue_fh(struct v4l2_fh *fh,
 
 	/* Do we have any free events? */
 	if (sev->in_use == sev->elems) {
-		/* no, remove the oldest one */
-		kev = sev->events + sev_pos(sev, 0);
-		list_del(&kev->list);
-		sev->in_use--;
-		sev->first = sev_pos(sev, 1);
-		fh->navailable--;
+		/*
+		 * No, so we have to make space.
+		 *
+		 * This is a bit tricky: the easy solution is to drop the oldest
+		 * event from the fh->available list and add the new one to
+		 * the end of the list. However, that can lead to situation
+		 * were, if there are a lot of events, the oldest event keeps
+		 * being removed before it can be dequeued by the application.
+		 * Effectively this is a denial-of-service situation were the
+		 * event is never seen by the application even though there are
+		 * actually a lot of events.
+		 *
+		 * So instead we take care to keep the oldest event in its
+		 * place, and instead either replace the event content with
+		 * the new event (if sev->elems == 1) or merge the 2nd oldest
+		 * event with the oldest event.
+		 */
+		struct v4l2_kevent *oldest = sev->events + sev_pos(sev, 0);
+		struct v4l2_kevent *second_oldest;
+
 		if (sev->elems == 1) {
 			if (sev->ops && sev->ops->replace) {
-				sev->ops->replace(&kev->event, ev);
-				copy_payload = false;
+				/* Replace the oldest event with the new event */
+				sev->ops->replace(&oldest->event, ev);
+			} else {
+				oldest->event.u = ev->u;
 			}
-		} else if (sev->ops && sev->ops->merge) {
-			struct v4l2_kevent *second_oldest =
-				sev->events + sev_pos(sev, 0);
-			sev->ops->merge(&kev->event, &second_oldest->event);
+			wake_up_all(&fh->wait);
+			return;
 		}
+		second_oldest = sev->events + sev_pos(sev, 1);
+		if (sev->ops && sev->ops->merge) {
+			/* Merge the oldest event with the 2nd oldest event */
+			sev->ops->merge(&oldest->event, &second_oldest->event);
+		}
+
+		/*
+		 * Replace the oldest event with the second oldest event in the
+		 * event list.
+		 */
+		second_oldest->event.sequence = oldest->event.sequence;
+		second_oldest->ts = oldest->ts;
+		list_del(&second_oldest->list);
+		list_replace_init(&oldest->list, &second_oldest->list);
+		sev->first = sev_pos(sev, 1);
+		sev->in_use--;
+		fh->navailable--;
 	}
 
 	/* Take one and fill it. */
 	kev = sev->events + sev_pos(sev, sev->in_use);
 	kev->event.type = ev->type;
-	if (copy_payload)
-		kev->event.u = ev->u;
+	kev->event.u = ev->u;
 	kev->event.id = ev->id;
 	kev->ts = ts;
 	kev->event.sequence = fh->sequence;
#2 lore raw
In reply to Hans Verkuil

Hi Hans,

Quoted 29 lines · Hans Verkuil

When subscribing to an event a fixed circular buffer of N (N >= 1) events is allocated for that file handle.

New events of the same type are added to that buffer and eventually the oldest event is removed from the buffer when VIDIOC_DQEVENT is called.

If the circular buffer is full, then the event framework will merge the two oldest events (what 'merge' means is event type specific).

So far, so good.

There is also a per-filehandle list of pending events for all event types. VIDIOC_DQEVENT always dequeues the oldest of that event list, and that event is removed from the corresponding circular buffer.

The problem occurs when the circular buffer is full and a new event arrives. Then the two oldest events are merged, but instead of keeping the position in the list of pending events, the merged event is added to the *end* of the list of pending events.

So if a lot of events are generated, then events can continually be pushed to the end of the list of pending events, and so are never or much later dequeued by the application.

Effectively this is a denial-of-service situation were the event is never seen by the application even though there are actually a lot of events.

Quoted 8 lines

So if you subscribe to events from control A and B, then change control A, then change control B, then change control A again, and now call VIDIOC_DQEVENT, you will get the event from control B followed by A, even though A was changed first.

This patch keeps the oldest event in its place in the 'pending events' list rather then moving it to the end, and in the test above you will now receive the event from control A first.

Typically events are queued faster than they're generated, it is an exception when they are not. The application certainly won't work very well if it consumes events slower than they're produced, whether or not the ordering of events fits for the use case.

For instance, for frame sync events, the latest event is more relevant than than earlier such events and the latest event should be kept instead of an earlier event. I wouldn't change this, or alternatively I'd make it dependent on the event type, but in I wonder if that's worth the complexity.

Quoted 99 lines
Fixes c53c2549333b ("[media] v4l2-event: Add v4l2_subscribed_event_ops")

---
Changes since v1:
- fh->sequence was not incremented when replacing an event. Just drop that change and keep the original code (i.e. incrementing the sequence counter at the beginning).

---
 drivers/media/v4l2-core/v4l2-event.c | 59 +++++++++++++++++++++-------
 1 file changed, 44 insertions(+), 15 deletions(-)
drivers/media/v4l2-core/v4l2-event.c +44 -15
index 3898ff7edddb..919cdd46a2c2 100644
@@ -104,7 +104,6 @@ static void __v4l2_event_queue_fh(struct v4l2_fh *fh,
 {
 	struct v4l2_subscribed_event *sev;
 	struct v4l2_kevent *kev;
-	bool copy_payload = true;
 
 	/* Are we subscribed? */
 	sev = v4l2_event_subscribed(fh, ev->type, ev->id);
@@ -116,29 +115,59 @@ static void __v4l2_event_queue_fh(struct v4l2_fh *fh,
 
 	/* Do we have any free events? */
 	if (sev->in_use == sev->elems) {
-		/* no, remove the oldest one */
-		kev = sev->events + sev_pos(sev, 0);
-		list_del(&kev->list);
-		sev->in_use--;
-		sev->first = sev_pos(sev, 1);
-		fh->navailable--;
+		/*
+		 * No, so we have to make space.
+		 *
+		 * This is a bit tricky: the easy solution is to drop the oldest
+		 * event from the fh->available list and add the new one to
+		 * the end of the list. However, that can lead to situation
+		 * were, if there are a lot of events, the oldest event keeps
+		 * being removed before it can be dequeued by the application.
+		 * Effectively this is a denial-of-service situation were the
+		 * event is never seen by the application even though there are
+		 * actually a lot of events.
+		 *
+		 * So instead we take care to keep the oldest event in its
+		 * place, and instead either replace the event content with
+		 * the new event (if sev->elems == 1) or merge the 2nd oldest
+		 * event with the oldest event.
+		 */
+		struct v4l2_kevent *oldest = sev->events + sev_pos(sev, 0);
+		struct v4l2_kevent *second_oldest;
+
 		if (sev->elems == 1) {
 			if (sev->ops && sev->ops->replace) {
-				sev->ops->replace(&kev->event, ev);
-				copy_payload = false;
+				/* Replace the oldest event with the new event */
+				sev->ops->replace(&oldest->event, ev);
+			} else {
+				oldest->event.u = ev->u;
 			}
-		} else if (sev->ops && sev->ops->merge) {
-			struct v4l2_kevent *second_oldest =
-				sev->events + sev_pos(sev, 0);
-			sev->ops->merge(&kev->event, &second_oldest->event);
+			wake_up_all(&fh->wait);
+			return;
 		}
+		second_oldest = sev->events + sev_pos(sev, 1);
+		if (sev->ops && sev->ops->merge) {
+			/* Merge the oldest event with the 2nd oldest event */
+			sev->ops->merge(&oldest->event, &second_oldest->event);
+		}
+
+		/*
+		 * Replace the oldest event with the second oldest event in the
+		 * event list.
+		 */
+		second_oldest->event.sequence = oldest->event.sequence;
+		second_oldest->ts = oldest->ts;
+		list_del(&second_oldest->list);
+		list_replace_init(&oldest->list, &second_oldest->list);
+		sev->first = sev_pos(sev, 1);
+		sev->in_use--;
+		fh->navailable--;
 	}
 
 	/* Take one and fill it. */
 	kev = sev->events + sev_pos(sev, sev->in_use);
 	kev->event.type = ev->type;
-	if (copy_payload)
-		kev->event.u = ev->u;
+	kev->event.u = ev->u;
 	kev->event.id = ev->id;
 	kev->ts = ts;
 	kev->event.sequence = fh->sequence;
Signature · 3 lines
Kind regards,

Sakari Ailus
#3 lore raw
Quoted 37 lines · Sakari Ailus

Hi Hans,

Quoted 34 lines · Hans Verkuil

When subscribing to an event a fixed circular buffer of N (N >= 1) events is allocated for that file handle.

New events of the same type are added to that buffer and eventually the oldest event is removed from the buffer when VIDIOC_DQEVENT is called.

If the circular buffer is full, then the event framework will merge the two oldest events (what 'merge' means is event type specific).

So far, so good.

There is also a per-filehandle list of pending events for all event types. VIDIOC_DQEVENT always dequeues the oldest of that event list, and that event is removed from the corresponding circular buffer.

The problem occurs when the circular buffer is full and a new event arrives. Then the two oldest events are merged, but instead of keeping the position in the list of pending events, the merged event is added to the *end* of the list of pending events.

So if a lot of events are generated, then events can continually be pushed to the end of the list of pending events, and so are never or much later dequeued by the application.

Effectively this is a denial-of-service situation were the event is never seen by the application even though there are actually a lot of events.

So if you subscribe to events from control A and B, then change control A, then change control B, then change control A again, and now call VIDIOC_DQEVENT, you will get the event from control B followed by A, even though A was changed first.

Quoted 5 lines
Quoted 3 lines

This patch keeps the oldest event in its place in the 'pending events' list rather then moving it to the end, and in the test above you will now receive the event from control A first.

Typically events are queued faster than they're generated, it is an

You mean 'dequeued' instead of 'queued'?

Quoted 3 lines

exception when they are not. The application certainly won't work very well if it consumes events slower than they're produced, whether or not the ordering of events fits for the use case.

The problem isn't the application, the problem is that with the current code events never reach the application, they can be continuously pushed back to the end of the queue. This is not theory, it's how I discovered something was wrong. Some events were delayed by seconds.

Quoted 5 lines

For instance, for frame sync events, the latest event is more relevant than than earlier such events and the latest event should be kept instead of an earlier event. I wouldn't change this, or alternatively I'd make it dependent on the event type, but in I wonder if that's worth the complexity.

This might be an option. Another option is to extend struct v4l2_event_subscription:
a flag that says how to handle this specific case (drop the oldest event or update the contents of the oldest event), and a field to set the size of the queue for that event.

Right now the number of events for a (type, id) tuple is set by the driver, but for situations like this it can be useful to be able to configure it by the application.

It would certainly help if I can configure this for the specific use-case I am dealing with.

Regards,

Hans

Quoted 101 lines
Quoted 100 lines
Fixes c53c2549333b ("[media] v4l2-event: Add v4l2_subscribed_event_ops")

---
Changes since v1:
- fh->sequence was not incremented when replacing an event. Just drop that change and keep the original code (i.e. incrementing the sequence counter at the beginning).

---
 drivers/media/v4l2-core/v4l2-event.c | 59 +++++++++++++++++++++-------
 1 file changed, 44 insertions(+), 15 deletions(-)
drivers/media/v4l2-core/v4l2-event.c +44 -15
index 3898ff7edddb..919cdd46a2c2 100644
@@ -104,7 +104,6 @@ static void __v4l2_event_queue_fh(struct v4l2_fh *fh,
 {
 	struct v4l2_subscribed_event *sev;
 	struct v4l2_kevent *kev;
-	bool copy_payload = true;
 
 	/* Are we subscribed? */
 	sev = v4l2_event_subscribed(fh, ev->type, ev->id);
@@ -116,29 +115,59 @@ static void __v4l2_event_queue_fh(struct v4l2_fh *fh,
 
 	/* Do we have any free events? */
 	if (sev->in_use == sev->elems) {
-		/* no, remove the oldest one */
-		kev = sev->events + sev_pos(sev, 0);
-		list_del(&kev->list);
-		sev->in_use--;
-		sev->first = sev_pos(sev, 1);
-		fh->navailable--;
+		/*
+		 * No, so we have to make space.
+		 *
+		 * This is a bit tricky: the easy solution is to drop the oldest
+		 * event from the fh->available list and add the new one to
+		 * the end of the list. However, that can lead to situation
+		 * were, if there are a lot of events, the oldest event keeps
+		 * being removed before it can be dequeued by the application.
+		 * Effectively this is a denial-of-service situation were the
+		 * event is never seen by the application even though there are
+		 * actually a lot of events.
+		 *
+		 * So instead we take care to keep the oldest event in its
+		 * place, and instead either replace the event content with
+		 * the new event (if sev->elems == 1) or merge the 2nd oldest
+		 * event with the oldest event.
+		 */
+		struct v4l2_kevent *oldest = sev->events + sev_pos(sev, 0);
+		struct v4l2_kevent *second_oldest;
+
 		if (sev->elems == 1) {
 			if (sev->ops && sev->ops->replace) {
-				sev->ops->replace(&kev->event, ev);
-				copy_payload = false;
+				/* Replace the oldest event with the new event */
+				sev->ops->replace(&oldest->event, ev);
+			} else {
+				oldest->event.u = ev->u;
 			}
-		} else if (sev->ops && sev->ops->merge) {
-			struct v4l2_kevent *second_oldest =
-				sev->events + sev_pos(sev, 0);
-			sev->ops->merge(&kev->event, &second_oldest->event);
+			wake_up_all(&fh->wait);
+			return;
 		}
+		second_oldest = sev->events + sev_pos(sev, 1);
+		if (sev->ops && sev->ops->merge) {
+			/* Merge the oldest event with the 2nd oldest event */
+			sev->ops->merge(&oldest->event, &second_oldest->event);
+		}
+
+		/*
+		 * Replace the oldest event with the second oldest event in the
+		 * event list.
+		 */
+		second_oldest->event.sequence = oldest->event.sequence;
+		second_oldest->ts = oldest->ts;
+		list_del(&second_oldest->list);
+		list_replace_init(&oldest->list, &second_oldest->list);
+		sev->first = sev_pos(sev, 1);
+		sev->in_use--;
+		fh->navailable--;
 	}
 
 	/* Take one and fill it. */
 	kev = sev->events + sev_pos(sev, sev->in_use);
 	kev->event.type = ev->type;
-	if (copy_payload)
-		kev->event.u = ev->u;
+	kev->event.u = ev->u;
 	kev->event.id = ev->id;
 	kev->ts = ts;
 	kev->event.sequence = fh->sequence;
#4 lore raw

Hi Hans,

Quoted 66 lines · Hans Verkuil
Quoted 37 lines · Sakari Ailus

Hi Hans,

Quoted 34 lines · Hans Verkuil

When subscribing to an event a fixed circular buffer of N (N >= 1) events is allocated for that file handle.

New events of the same type are added to that buffer and eventually the oldest event is removed from the buffer when VIDIOC_DQEVENT is called.

If the circular buffer is full, then the event framework will merge the two oldest events (what 'merge' means is event type specific).

So far, so good.

There is also a per-filehandle list of pending events for all event types. VIDIOC_DQEVENT always dequeues the oldest of that event list, and that event is removed from the corresponding circular buffer.

The problem occurs when the circular buffer is full and a new event arrives. Then the two oldest events are merged, but instead of keeping the position in the list of pending events, the merged event is added to the *end* of the list of pending events.

So if a lot of events are generated, then events can continually be pushed to the end of the list of pending events, and so are never or much later dequeued by the application.

Effectively this is a denial-of-service situation were the event is never seen by the application even though there are actually a lot of events.

So if you subscribe to events from control A and B, then change control A, then change control B, then change control A again, and now call VIDIOC_DQEVENT, you will get the event from control B followed by A, even though A was changed first.

Quoted 5 lines
Quoted 3 lines

This patch keeps the oldest event in its place in the 'pending events' list rather then moving it to the end, and in the test above you will now receive the event from control A first.

Typically events are queued faster than they're generated, it is an

You mean 'dequeued' instead of 'queued'?

Quoted 3 lines

exception when they are not. The application certainly won't work very well if it consumes events slower than they're produced, whether or not the ordering of events fits for the use case.

The problem isn't the application, the problem is that with the current code events never reach the application, they can be continuously pushed back to the end of the queue. This is not theory, it's how I discovered something was wrong. Some events were delayed by seconds.

Quoted 5 lines

For instance, for frame sync events, the latest event is more relevant than than earlier such events and the latest event should be kept instead of an earlier event. I wouldn't change this, or alternatively I'd make it dependent on the event type, but in I wonder if that's worth the complexity.

This might be an option. Another option is to extend struct v4l2_event_subscription:
a flag that says how to handle this specific case (drop the oldest event or update the contents of the oldest event), and a field to set the size of the queue for that event.

Quoted 6 lines

Right now the number of events for a (type, id) tuple is set by the driver, but for situations like this it can be useful to be able to configure it by the application.

It would certainly help if I can configure this for the specific use-case I am dealing with.

Sounds good to me, implementation-wise. But to benefit from that, applications would need to set the flag. If that's enough, then we don't need e.g. defaults per event type (and don't need another flag).

Signature · 1 line
Sakari Ailus
#5 lore raw
Quoted 45 lines · Sakari Ailus
Quoted 44 lines · Hans Verkuil
Quoted 35 lines · Sakari Ailus
Quoted 34 lines · Hans Verkuil

When subscribing to an event a fixed circular buffer of N (N >= 1) events is allocated for that file handle.

New events of the same type are added to that buffer and eventually the oldest event is removed from the buffer when VIDIOC_DQEVENT is called.

If the circular buffer is full, then the event framework will merge the two oldest events (what 'merge' means is event type specific).

So far, so good.

There is also a per-filehandle list of pending events for all event types. VIDIOC_DQEVENT always dequeues the oldest of that event list, and that event is removed from the corresponding circular buffer.

The problem occurs when the circular buffer is full and a new event arrives. Then the two oldest events are merged, but instead of keeping the position in the list of pending events, the merged event is added to the *end* of the list of pending events.

So if a lot of events are generated, then events can continually be pushed to the end of the list of pending events, and so are never or much later dequeued by the application.

Effectively this is a denial-of-service situation were the event is never seen by the application even though there are actually a lot of events.

So if you subscribe to events from control A and B, then change control A, then change control B, then change control A again, and now call VIDIOC_DQEVENT, you will get the event from control B followed by A, even though A was changed first.

Quoted 5 lines
Quoted 3 lines

This patch keeps the oldest event in its place in the 'pending events' list rather then moving it to the end, and in the test above you will now receive the event from control A first.

Typically events are queued faster than they're generated, it is an

You mean 'dequeued' instead of 'queued'?

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

exception when they are not. The application certainly won't work very well if it consumes events slower than they're produced, whether or not the ordering of events fits for the use case.

The problem isn't the application, the problem is that with the current code events never reach the application, they can be continuously pushed back to the end of the queue. This is not theory, it's how I discovered something was wrong. Some events were delayed by seconds.

But that's still related to applications not dequeuing events fast enough, isn't it ? If there's enough space in the events queue for new events, would the problem still occur ?

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

For instance, for frame sync events, the latest event is more relevant than than earlier such events and the latest event should be kept instead of an earlier event.

Sure, but I don't think this patch prevents that, does it ? When merging frame sync events we can drop the older event and keep the newer one while storing the newer event in the position of the old one in the queue, can't we ?

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

I wouldn't change this, or alternatively I'd make it dependent on the event type, but in I wonder if that's worth the complexity.

This might be an option. Another option is to extend struct v4l2_event_subscription:
a flag that says how to handle this specific case (drop the oldest event or update the contents of the oldest event), and a field to set the size of the queue for that event.

Unless I'm missing something, this problem seems solvable without having to increase the completexity of the UAPI.

Quoted 10 lines
Quoted 6 lines

Right now the number of events for a (type, id) tuple is set by the driver, but for situations like this it can be useful to be able to configure it by the application.

It would certainly help if I can configure this for the specific use-case I am dealing with.

Sounds good to me, implementation-wise. But to benefit from that, applications would need to set the flag. If that's enough, then we don't need e.g. defaults per event type (and don't need another flag).

Signature · 3 lines
Regards,

Laurent Pinchart
#6 lore raw
Quoted 72 lines · Laurent Pinchart
Quoted 45 lines · Sakari Ailus
Quoted 44 lines · Hans Verkuil
Quoted 35 lines · Sakari Ailus
Quoted 34 lines · Hans Verkuil

When subscribing to an event a fixed circular buffer of N (N >= 1) events is allocated for that file handle.

New events of the same type are added to that buffer and eventually the oldest event is removed from the buffer when VIDIOC_DQEVENT is called.

If the circular buffer is full, then the event framework will merge the two oldest events (what 'merge' means is event type specific).

So far, so good.

There is also a per-filehandle list of pending events for all event types. VIDIOC_DQEVENT always dequeues the oldest of that event list, and that event is removed from the corresponding circular buffer.

The problem occurs when the circular buffer is full and a new event arrives. Then the two oldest events are merged, but instead of keeping the position in the list of pending events, the merged event is added to the *end* of the list of pending events.

So if a lot of events are generated, then events can continually be pushed to the end of the list of pending events, and so are never or much later dequeued by the application.

Effectively this is a denial-of-service situation were the event is never seen by the application even though there are actually a lot of events.

So if you subscribe to events from control A and B, then change control A, then change control B, then change control A again, and now call VIDIOC_DQEVENT, you will get the event from control B followed by A, even though A was changed first.

Quoted 5 lines
Quoted 3 lines

This patch keeps the oldest event in its place in the 'pending events' list rather then moving it to the end, and in the test above you will now receive the event from control A first.

Typically events are queued faster than they're generated, it is an

You mean 'dequeued' instead of 'queued'?

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

exception when they are not. The application certainly won't work very well if it consumes events slower than they're produced, whether or not the ordering of events fits for the use case.

The problem isn't the application, the problem is that with the current code events never reach the application, they can be continuously pushed back to the end of the queue. This is not theory, it's how I discovered something was wrong. Some events were delayed by seconds.

But that's still related to applications not dequeuing events fast enough, isn't it ? If there's enough space in the events queue for new events, would the problem still occur ?

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

For instance, for frame sync events, the latest event is more relevant than than earlier such events and the latest event should be kept instead of an earlier event.

Sure, but I don't think this patch prevents that, does it ? When merging frame sync events we can drop the older event and keep the newer one while storing the newer event in the position of the old one in the queue, can't we ?

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

I wouldn't change this, or alternatively I'd make it dependent on the event type, but in I wonder if that's worth the complexity.

Quoted 19 lines
Quoted 4 lines
Quoted 4 lines

This might be an option. Another option is to extend struct v4l2_event_subscription:
a flag that says how to handle this specific case (drop the oldest event or update the contents of the oldest event), and a field to set the size of the queue for that event.

Unless I'm missing something, this problem seems solvable without having to increase the completexity of the UAPI.

Quoted 10 lines
Quoted 6 lines

Right now the number of events for a (type, id) tuple is set by the driver, but for situations like this it can be useful to be able to configure it by the application.

It would certainly help if I can configure this for the specific use-case I am dealing with.

Sounds good to me, implementation-wise. But to benefit from that, applications would need to set the flag. If that's enough, then we don't need e.g. defaults per event type (and don't need another flag).

I plan to rework this, once I find some more time. Allowing userspace to set the size of the event queue will help a lot, and that's actually a trivial change.

In the end it comes down to what to do when a new event arrives and the queue is
full: do you want to drop the oldest event or the new event? That depends on what event it is. Allowing the driver to make that choice would be nice.

Regards,

Hans

#7 lore raw
Quoted 88 lines · Hans Verkuil
Quoted 87 lines · Laurent Pinchart
Quoted 45 lines · Sakari Ailus
Quoted 44 lines · Hans Verkuil
Quoted 35 lines · Sakari Ailus
Quoted 34 lines · Hans Verkuil

When subscribing to an event a fixed circular buffer of N (N >= 1) events is allocated for that file handle.

New events of the same type are added to that buffer and eventually the oldest event is removed from the buffer when VIDIOC_DQEVENT is called.

If the circular buffer is full, then the event framework will merge the two oldest events (what 'merge' means is event type specific).

So far, so good.

There is also a per-filehandle list of pending events for all event types. VIDIOC_DQEVENT always dequeues the oldest of that event list, and that event is removed from the corresponding circular buffer.

The problem occurs when the circular buffer is full and a new event arrives. Then the two oldest events are merged, but instead of keeping the position in the list of pending events, the merged event is added to the *end* of the list of pending events.

So if a lot of events are generated, then events can continually be pushed to the end of the list of pending events, and so are never or much later dequeued by the application.

Effectively this is a denial-of-service situation were the event is never seen by the application even though there are actually a lot of events.

So if you subscribe to events from control A and B, then change control A, then change control B, then change control A again, and now call VIDIOC_DQEVENT, you will get the event from control B followed by A, even though A was changed first.

Quoted 5 lines
Quoted 3 lines

This patch keeps the oldest event in its place in the 'pending events' list rather then moving it to the end, and in the test above you will now receive the event from control A first.

Typically events are queued faster than they're generated, it is an

You mean 'dequeued' instead of 'queued'?

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

exception when they are not. The application certainly won't work very well if it consumes events slower than they're produced, whether or not the ordering of events fits for the use case.

The problem isn't the application, the problem is that with the current code events never reach the application, they can be continuously pushed back to the end of the queue. This is not theory, it's how I discovered something was wrong. Some events were delayed by seconds.

But that's still related to applications not dequeuing events fast enough, isn't it ? If there's enough space in the events queue for new events, would the problem still occur ?

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

For instance, for frame sync events, the latest event is more relevant than than earlier such events and the latest event should be kept instead of an earlier event.

Sure, but I don't think this patch prevents that, does it ? When merging frame sync events we can drop the older event and keep the newer one while storing the newer event in the position of the old one in the queue, can't we ?

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

I wouldn't change this, or alternatively I'd make it dependent on the event type, but in I wonder if that's worth the complexity.

This might be an option. Another option is to extend struct v4l2_event_subscription:
a flag that says how to handle this specific case (drop the oldest event or update the contents of the oldest event), and a field to set the size of the queue for that event.

Unless I'm missing something, this problem seems solvable without having to increase the completexity of the UAPI.

Quoted 7 lines
Quoted 6 lines

Right now the number of events for a (type, id) tuple is set by the driver, but for situations like this it can be useful to be able to configure it by the application.

It would certainly help if I can configure this for the specific use-case I am dealing with.

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Sounds good to me, implementation-wise. But to benefit from that, applications would need to set the flag. If that's enough, then we don't need e.g. defaults per event type (and don't need another flag).

I plan to rework this, once I find some more time. Allowing userspace to set the size of the event queue will help a lot, and that's actually a trivial change.

That probably makes sense. I'd be interested in knowing more about your use cases when you'll have time.

Quoted 3 lines

In the end it comes down to what to do when a new event arrives and the queue is
full: do you want to drop the oldest event or the new event? That depends on what event it is. Allowing the driver to make that choice would be nice.

For which event types do you envision a driver would prefer dropping the newest event ?

Signature · 3 lines
Regards,

Laurent Pinchart