Skip to contentSkip to conversation

udev: fix problem due to unsorted events

PATCH In review Andrey Vagin 4 participants 7 messages Mar 6, 2012 – Mar 7, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 udev: fix problem due to unsorted events

From: Andrey Vagin <[email protected]>

A kernel gives events, but this events can be unsorted. For example, here is log from system:
udevd[77]: seq 924 queued, 'add' 'bdi' udevd[77]: seq 926 queued, 'add' 'block' udevd[77]: seq 927 queued, 'add' 'block' udevd[77]: seq 928 queued, 'add' 'block udevd[77]: seq 925 queued, 'add' 'drivers'

In this case "udevadm settle" returns an error:
queue is empty but kernel events still pending [928]<->[925]

Let's look at update_queue(). It contains the follow code:

/* now write to the queue */

if (state = DEVICE_QUEUED) {
        udev_queue_export->queued_count++;
        udev_queue_export->seqnum_min = seqnum;
}

where seqnum_min is latest sequence number in queue file. Probably we should check that seqnum is not less than seqnum_min and update it only in this case.

https://bugzilla.redhat.com/show_bug.cgi?idy9834

---
 src/libudev-queue-private.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
src/libudev-queue-private.c +2 -1
index 7177195..6a689c4 100644
@@ -376,7 +376,8 @@ static int update_queue(struct udev_queue_export *udev_queue_export,
         /* now write to the queue */
         if (state = DEVICE_QUEUED) {
                 udev_queue_export->queued_count++;
-                udev_queue_export->seqnum_min = seqnum;
+                if (udev_queue_export->seqnum_min < seqnum)
+                        udev_queue_export->seqnum_min = seqnum;
         } else {
                 udev_queue_export->waste_bytes += queue_record_size(devpath_len) + queue_record_size(0);
                 udev_queue_export->queued_count--;
Signature · 1 line
1.7.7.6
#1 lore raw
Quoted 9 lines · Andrey Vagin

From: Andrey Vagin <[email protected]>

A kernel gives events, but this events can be unsorted. For example, here is log from system:
udevd[77]: seq 924 queued, 'add' 'bdi' udevd[77]: seq 926 queued, 'add' 'block' udevd[77]: seq 927 queued, 'add' 'block' udevd[77]: seq 928 queued, 'add' 'block udevd[77]: seq 925 queued, 'add' 'drivers'

The kernel cannot send un-ordered events. Please try to reproduce that with 'udevadm monitor'.

I doubt that this is a problem udev should try to work around, its logic is entirely based on the assumption that kernel events are always in order.

Kay

#2 lore raw
In reply to Andrey Vagin
Quoted 26 lines · Andrew Wagin

2012/3/6 Kay Sievers <[email protected]>:

Quoted 11 lines · Andrey Vagin
Quoted 9 lines

From: Andrey Vagin <[email protected]>

A kernel gives events, but this events can be unsorted. For example, here is log from system:
udevd[77]: seq 924 queued, 'add' 'bdi' udevd[77]: seq 926 queued, 'add' 'block' udevd[77]: seq 927 queued, 'add' 'block' udevd[77]: seq 928 queued, 'add' 'block udevd[77]: seq 925 queued, 'add' 'drivers'

Quoted 2 lines

The kernel cannot send un-ordered events. Please try to reproduce that with 'udevadm monitor'.

I can't reproduce this bug, because I have not this host already. I've attached the console.log. I have not other logs.
I showed you messages from event_queue_insert(). Actually it's what you ask. I suppose that a kernel should not send un-ordered events, but it does.

Let's look at kobject_uevent_env() in kernel source:

       spin_lock(&sequence_lock);
       seq = ++uevent_seqnum;
       spin_unlock(&sequence_lock);

Quoted 8 lines

.... <-- here someone can send its event

       /* send netlink message */
       mutex_lock(&uevent_sock_mutex);
.....

I want to say that code contains a window between incrementing seqnum and sending an event.

Yeah, that looks suspicious. We need to find out what and how to fix that. The queue handling in the daemon assumes that the events are ordered, so there might not only a problem with 'settle'.

Kay

#3 lore raw

PATCH 1/1 uevent: send events in correct order according to seqnum

The queue handling in the udev daemon assumes that the events are ordered.

Before this patch uevent_seqnum is incremented under sequence_lock, than an event is send uner uevent_sock_mutex. I want to say that code contained a window between incrementing seqnum and sending an event.

This patch locks uevent_sock_mutex before incrementing uevent_seqnum.

---
 lib/kobject_uevent.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
lib/kobject_uevent.c +3 -1
index e66e9b6..596c40d 100644
@@ -243,6 +243,9 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
 	else if (action == KOBJ_REMOVE)
 		kobj->state_remove_uevent_sent = 1;
 
+#if defined(CONFIG_NET)
+	mutex_lock(&uevent_sock_mutex);
+#endif
 	/* we will send an event, so request a new sequence number */
 	spin_lock(&sequence_lock);
 	seq = ++uevent_seqnum;
@@ -253,7 +256,6 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
 
 #if defined(CONFIG_NET)
 	/* send netlink message */
-	mutex_lock(&uevent_sock_mutex);
 	list_for_each_entry(ue_sk, &uevent_sock_list, list) {
 		struct sock *uevent_sock = ue_sk->sk;
 		struct sk_buff *skb;
#4 lore raw

Re: [PATCH] uevent: send events in correct order according to seqnum

Quoted 9 lines · Andrew Vagin

The queue handling in the udev daemon assumes that the events are ordered.

Before this patch uevent_seqnum is incremented under sequence_lock, than an event is send uner uevent_sock_mutex. I want to say that code contained a window between incrementing seqnum and sending an event.

This patch locks uevent_sock_mutex before incrementing uevent_seqnum.

I think we can remove the spin_lock(&sequence_lock); entirely now, right?

Also the section with:
seq = ++uevent_seqnum;
can just be:
add_uevent_var(env, "SEQNUM=%llu", (unsigned long long) ++uevent_seqnum);
right?

And the:
mutex_lock(&uevent_sock_mutex);
can just move outside of the _NET ifdef and we always use the mutex instead of the spinlock?

That could look much simpler than the current code, I think.

Thanks,
Kay

#5 lore raw

Re: [PATCH] uevent: send events in correct order according to seqnum

Quoted 5 lines · Kay Sievers
Quoted 4 lines · Andrew Vagin

The queue handling in the udev daemon assumes that the events are ordered.

Quoted 7 lines
Quoted 5 lines

Before this patch uevent_seqnum is incremented under sequence_lock, than an event is send uner uevent_sock_mutex. I want to say that code contained a window between incrementing seqnum and sending an event.

This patch locks uevent_sock_mutex before incrementing uevent_seqnum.

I think we can remove the spin_lock(&sequence_lock); entirely now, right?

I thought about that too. sequence_lock is used when CONFIG_NET isn't defined. I've looked on this code one more time and we may leave only uevent_sock_mutex and use it even when CONFIG_NET isn't defined. Thanks for the comment.

Greg, do you have other objections about this patch?

Quoted 16 lines

Also the section with:

   seq = ++uevent_seqnum;
can just be:
   add_uevent_var(env, "SEQNUM=%llu", (unsigned long long) ++uevent_seqnum);

right?

And the:

   mutex_lock(&uevent_sock_mutex);

can just move outside of the _NET ifdef and we always use the mutex instead of the spinlock?

That could look much simpler than the current code, I think.

Thanks,
Kay

#6 lore raw

Re: [PATCH] uevent: send events in correct order according to seqnum

Quoted 14 lines · On Wed, Mar 07, 2012 at 01:14:04AM +0400, [email protected] wrote:
Quoted 5 lines · Kay Sievers
Quoted 4 lines · Andrew Vagin

The queue handling in the udev daemon assumes that the events are ordered.

Quoted 7 lines
Quoted 5 lines

Before this patch uevent_seqnum is incremented under sequence_lock, than an event is send uner uevent_sock_mutex. I want to say that code contained a window between incrementing seqnum and sending an event.

This patch locks uevent_sock_mutex before incrementing uevent_seqnum.

I think we can remove the spin_lock(&sequence_lock); entirely now, right?

Quoted 7 lines

I thought about that too. sequence_lock is used when CONFIG_NET isn't defined. I've looked on this code one more time and we may leave only uevent_sock_mutex and use it even when CONFIG_NET isn't defined.
Thanks for the comment.

Greg, do you have other objections about this patch?

Let's see the one based on Kay's comments first please.

thanks,

greg k-h