Skip to contentSkip to conversation

v4l: event: Add subscription to list before calling "add" operation

PATCH Superseded Sakari Ailus 3 participants 9 messages Nov 14, 2018 – Nov 26, 2018 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 v4l: event: Add subscription to list before calling "add" operation

[ upstream commit 92539d3eda2c090b382699bbb896d4b54e9bdece ]

Patch ad608fbcf166 changed how events were subscribed to address an issue elsewhere. As a side effect of that change, the "add" callback was called before the event subscription was added to the list of subscribed events, causing the first event queued by the add callback (and possibly other events arriving soon afterwards) to be lost.

Fix this by adding the subscription to the list before calling the "add" callback, and clean up afterwards if that fails.

Fixes ad608fbcf166 ("media: v4l: event: Prevent freeing event subscriptions while accessed")

---
since v1 (as requested by Sasha):

- Add my final SoB
- Indicate specifically this is a backport
- Remove the extra cc stable

 drivers/media/v4l2-core/v4l2-event.c | 43 ++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 19 deletions(-)
drivers/media/v4l2-core/v4l2-event.c +24 -19
index b47ac4e053d0e..f5c8a952f0aa3 100644
@@ -197,6 +197,22 @@ int v4l2_event_pending(struct v4l2_fh *fh)
 }
 EXPORT_SYMBOL_GPL(v4l2_event_pending);
 
+static void __v4l2_event_unsubscribe(struct v4l2_subscribed_event *sev)
+{
+	struct v4l2_fh *fh = sev->fh;
+	unsigned int i;
+
+	lockdep_assert_held(&fh->subscribe_lock);
+	assert_spin_locked(&fh->vdev->fh_lock);
+
+	/* Remove any pending events for this subscription */
+	for (i = 0; i < sev->in_use; i++) {
+		list_del(&sev->events[sev_pos(sev, i)].list);
+		fh->navailable--;
+	}
+	list_del(&sev->list);
+}
+
 int v4l2_event_subscribe(struct v4l2_fh *fh,
 			 const struct v4l2_event_subscription *sub, unsigned elems,
 			 const struct v4l2_subscribed_event_ops *ops)
@@ -228,27 +244,23 @@ int v4l2_event_subscribe(struct v4l2_fh *fh,
 
 	spin_lock_irqsave(&fh->vdev->fh_lock, flags);
 	found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
+	if (!found_ev)
+		list_add(&sev->list, &fh->subscribed);
 	spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
 
 	if (found_ev) {
 		/* Already listening */
 		kfree(sev);
-		goto out_unlock;
-	}
-
-	if (sev->ops && sev->ops->add) {
+	} else if (sev->ops && sev->ops->add) {
 		ret = sev->ops->add(sev, elems);
 		if (ret) {
+			spin_lock_irqsave(&fh->vdev->fh_lock, flags);
+			__v4l2_event_unsubscribe(sev);
+			spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
 			kfree(sev);
-			goto out_unlock;
 		}
 	}
 
-	spin_lock_irqsave(&fh->vdev->fh_lock, flags);
-	list_add(&sev->list, &fh->subscribed);
-	spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
-
-out_unlock:
 	mutex_unlock(&fh->subscribe_lock);
 
 	return ret;
@@ -283,7 +295,6 @@ int v4l2_event_unsubscribe(struct v4l2_fh *fh,
 {
 	struct v4l2_subscribed_event *sev;
 	unsigned long flags;
-	int i;
 
 	if (sub->type == V4L2_EVENT_ALL) {
 		v4l2_event_unsubscribe_all(fh);
@@ -295,14 +306,8 @@ int v4l2_event_unsubscribe(struct v4l2_fh *fh,
 	spin_lock_irqsave(&fh->vdev->fh_lock, flags);
 
 	sev = v4l2_event_subscribed(fh, sub->type, sub->id);
-	if (sev != NULL) {
-		/* Remove any pending events for this subscription */
-		for (i = 0; i < sev->in_use; i++) {
-			list_del(&sev->events[sev_pos(sev, i)].list);
-			fh->navailable--;
-		}
-		list_del(&sev->list);
-	}
+	if (sev != NULL)
+		__v4l2_event_unsubscribe(sev);
 
 	spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
 
Signature · 1 line
2.11.0
#1 lore raw
Quoted 1 line · Sakari Ailus

[ upstream commit 92539d3eda2c090b382699bbb896d4b54e9bdece ]

There is no such git commit id in Linus's tree :(

#2 lore raw

Hi Greg,

Quoted 4 lines · Greg Kroah-Hartman
Quoted 1 line · Sakari Ailus

[ upstream commit 92539d3eda2c090b382699bbb896d4b54e9bdece ]

There is no such git commit id in Linus's tree :(

Right. At the moment it's in the media tree only. I expect it'll end up to Linus's tree once Mauro will send the next pull request from the media tree to Linus.

Signature · 4 lines
Regards,

Sakari Ailus
[email protected]
#3 lore raw
Quoted 11 lines · Sakari Ailus

Hi Greg,

Quoted 4 lines · Greg Kroah-Hartman
Quoted 1 line · Sakari Ailus

[ upstream commit 92539d3eda2c090b382699bbb896d4b54e9bdece ]

There is no such git commit id in Linus's tree :(

Right. At the moment it's in the media tree only. I expect it'll end up to Linus's tree once Mauro will send the next pull request from the media tree to Linus.

Ok, please do not send requests for stable tree inclusion until _AFTER_ the patch is in Linus's tree, otherwise it just wastes the stable tree maintainer's time :(

greg k-h

#4 lore raw

Hi Greg,

Quoted 9 lines · Greg Kroah-Hartman
Quoted 8 lines · Sakari Ailus

Hi Greg,

Quoted 4 lines · Greg Kroah-Hartman
Quoted 1 line · Sakari Ailus

[ upstream commit 92539d3eda2c090b382699bbb896d4b54e9bdece ]

There is no such git commit id in Linus's tree :(

Quoted 7 lines
Quoted 3 lines

Right. At the moment it's in the media tree only. I expect it'll end up to Linus's tree once Mauro will send the next pull request from the media tree to Linus.

Ok, please do not send requests for stable tree inclusion until _AFTER_ the patch is in Linus's tree, otherwise it just wastes the stable tree maintainer's time :(

Apologies for the noise. I'll send you a note once the patches are in Linus's tree.

Thanks.

Signature · 4 lines
Kind regards,

Sakari Ailus
[email protected]
#5 lore raw

Em Tue, 20 Nov 2018 12:49:46 +0200
Sakari Ailus <[email protected]> escreveu:

Quoted 16 lines

Hi Greg,

Quoted 13 lines · Greg Kroah-Hartman
Quoted 11 lines · Sakari Ailus

Hi Greg,

Quoted 4 lines · Greg Kroah-Hartman
Quoted 1 line · Sakari Ailus

[ upstream commit 92539d3eda2c090b382699bbb896d4b54e9bdece ]

There is no such git commit id in Linus's tree :(

Right. At the moment it's in the media tree only. I expect it'll end up to Linus's tree once Mauro will send the next pull request from the media tree to Linus.

Quoted 6 lines
Quoted 3 lines

Ok, please do not send requests for stable tree inclusion until _AFTER_ the patch is in Linus's tree, otherwise it just wastes the stable tree maintainer's time :(

Apologies for the noise. I'll send you a note once the patches are in Linus's tree.

Btw, just sent a pull request with this patch.

I wanted to send this two weeks ago, but I had to do two trips (the final one to be at KS/LPC). This ended by delaying the pull request.

Thanks,
Mauro

#6 lore raw
Quoted 23 lines · Mauro Carvalho Chehab

Em Tue, 20 Nov 2018 12:49:46 +0200
Sakari Ailus <[email protected]> escreveu:

Quoted 20 lines

Hi Greg,

Quoted 16 lines · Greg Kroah-Hartman
Quoted 11 lines · Sakari Ailus

Hi Greg,

Quoted 4 lines · Greg Kroah-Hartman
Quoted 1 line · Sakari Ailus

[ upstream commit 92539d3eda2c090b382699bbb896d4b54e9bdece ]

There is no such git commit id in Linus's tree :(

Right. At the moment it's in the media tree only. I expect it'll end up to Linus's tree once Mauro will send the next pull request from the media tree to Linus.

Ok, please do not send requests for stable tree inclusion until _AFTER_ the patch is in Linus's tree, otherwise it just wastes the stable tree maintainer's time :(

Quoted 7 lines
Quoted 2 lines

Apologies for the noise. I'll send you a note once the patches are in Linus's tree.

Btw, just sent a pull request with this patch.

I wanted to send this two weeks ago, but I had to do two trips (the final one to be at KS/LPC). This ended by delaying the pull request.

The patch is in Linus's tree now.

Signature · 4 lines
Regards,

Sakari Ailus
[email protected]
#7 lore raw
Quoted 27 lines · Sakari Ailus
Quoted 26 lines · Mauro Carvalho Chehab

Em Tue, 20 Nov 2018 12:49:46 +0200
Sakari Ailus <[email protected]> escreveu:

Quoted 22 lines

Hi Greg,

Quoted 9 lines · Greg Kroah-Hartman
Quoted 8 lines · Sakari Ailus

Hi Greg,

Quoted 4 lines · Greg Kroah-Hartman
Quoted 1 line · Sakari Ailus

[ upstream commit 92539d3eda2c090b382699bbb896d4b54e9bdece ]

There is no such git commit id in Linus's tree :(

Quoted 7 lines
Quoted 3 lines

Right. At the moment it's in the media tree only. I expect it'll end up to Linus's tree once Mauro will send the next pull request from the media tree to Linus.

Ok, please do not send requests for stable tree inclusion until _AFTER_ the patch is in Linus's tree, otherwise it just wastes the stable tree maintainer's time :(

Apologies for the noise. I'll send you a note once the patches are in Linus's tree.

Quoted 6 lines
Quoted 4 lines

Btw, just sent a pull request with this patch.

I wanted to send this two weeks ago, but I had to do two trips (the final one to be at KS/LPC). This ended by delaying the pull request.

The patch is in Linus's tree now.

And what is the git commit id?

thanks,

gre k-h

#8 lore raw
Quoted 28 lines · Greg Kroah-Hartman
Quoted 27 lines · Sakari Ailus
Quoted 26 lines · Mauro Carvalho Chehab

Em Tue, 20 Nov 2018 12:49:46 +0200
Sakari Ailus <[email protected]> escreveu:

Quoted 22 lines

Hi Greg,

Quoted 9 lines · Greg Kroah-Hartman
Quoted 8 lines · Sakari Ailus

Hi Greg,

Quoted 4 lines · Greg Kroah-Hartman
Quoted 1 line · Sakari Ailus

[ upstream commit 92539d3eda2c090b382699bbb896d4b54e9bdece ]

There is no such git commit id in Linus's tree :(

Quoted 7 lines
Quoted 3 lines

Right. At the moment it's in the media tree only. I expect it'll end up to Linus's tree once Mauro will send the next pull request from the media tree to Linus.

Ok, please do not send requests for stable tree inclusion until _AFTER_ the patch is in Linus's tree, otherwise it just wastes the stable tree maintainer's time :(

Apologies for the noise. I'll send you a note once the patches are in Linus's tree.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Btw, just sent a pull request with this patch.

I wanted to send this two weeks ago, but I had to do two trips (the final one to be at KS/LPC). This ended by delaying the pull request.

The patch is in Linus's tree now.

And what is the git commit id?

Nevermind, I see it...