Skip to contentSkip to conversation

x86/hpet: simplify hpet_get_channel()

PATCH In review Jan Beulich 2 participants 6 messages Mar 23, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/hpet: simplify hpet_get_channel()

There's no need for a lock here, elimination of which makes the function a leaf one, thus allowing for better (and smaller) code.

Further, use the variable next_channel according to its name - so far it represented the most recently used channel rather than the next one to use.

xen/arch/x86/hpet.c +15 -10
@@ -402,30 +402,35 @@ static void __init hpet_fsb_cap_lookup(v
 static struct hpet_event_channel *hpet_get_channel(unsigned int cpu)
 {
     static unsigned int next_channel;
-    static spinlock_t next_lock = SPIN_LOCK_UNLOCKED;
     unsigned int i, next;
     struct hpet_event_channel *ch;
 
     if ( num_hpets_used == 0 )
         return hpet_events;
 
-    spin_lock(&next_lock);
-    next = next_channel = (next_channel + 1) % num_hpets_used;
-    spin_unlock(&next_lock);
+    do {
+        next = next_channel;
+        if ( (i = next + 1) == num_hpets_used )
+            i = 0;
+    } while ( cmpxchg(&next_channel, next, i) != next );
 
     /* try unused channel first */
-    for ( i = next; i < next + num_hpets_used; i++ )
-    {
-        ch = &hpet_events[i % num_hpets_used];
+    ch = &hpet_events[i = next];
+    do {
         if ( !test_and_set_bit(HPET_EVT_USED_BIT, &ch->flags) )
         {
             ch->cpu = cpu;
             return ch;
         }
-    }
+        ++ch;
+        if ( ++i == num_hpets_used )
+        {
+            i = 0;
+            ch = hpet_events;
+        }
+    } while ( i != next );
 
-    /* share a in-use channel */
-    ch = &hpet_events[next];
+    /* Share an in-use channel. */
     if ( !test_and_set_bit(HPET_EVT_USED_BIT, &ch->flags) )
         ch->cpu = cpu;
 

x86/hpet: simplify hpet_get_channel()

There's no need for a lock here, elimination of which makes the function a leaf one, thus allowing for better (and smaller) code.

Further, use the variable next_channel according to its name - so far it represented the most recently used channel rather than the next one to use.

xen/arch/x86/hpet.c +15 -10
@@ -402,30 +402,35 @@ static void __init hpet_fsb_cap_lookup(v
 static struct hpet_event_channel *hpet_get_channel(unsigned int cpu)
 {
     static unsigned int next_channel;
-    static spinlock_t next_lock = SPIN_LOCK_UNLOCKED;
     unsigned int i, next;
     struct hpet_event_channel *ch;
 
     if ( num_hpets_used == 0 )
         return hpet_events;
 
-    spin_lock(&next_lock);
-    next = next_channel = (next_channel + 1) % num_hpets_used;
-    spin_unlock(&next_lock);
+    do {
+        next = next_channel;
+        if ( (i = next + 1) == num_hpets_used )
+            i = 0;
+    } while ( cmpxchg(&next_channel, next, i) != next );
 
     /* try unused channel first */
-    for ( i = next; i < next + num_hpets_used; i++ )
-    {
-        ch = &hpet_events[i % num_hpets_used];
+    ch = &hpet_events[i = next];
+    do {
         if ( !test_and_set_bit(HPET_EVT_USED_BIT, &ch->flags) )
         {
             ch->cpu = cpu;
             return ch;
         }
-    }
+        ++ch;
+        if ( ++i == num_hpets_used )
+        {
+            i = 0;
+            ch = hpet_events;
+        }
+    } while ( i != next );
 
-    /* share a in-use channel */
-    ch = &hpet_events[next];
+    /* Share an in-use channel. */
     if ( !test_and_set_bit(HPET_EVT_USED_BIT, &ch->flags) )
         ch->cpu = cpu;
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#1 lore raw
Quoted 6 lines · Jan Beulich

There's no need for a lock here, elimination of which makes the function a leaf one, thus allowing for better (and smaller) code.

Further, use the variable next_channel according to its name - so far it represented the most recently used channel rather than the next one to use.

Why is the following for-loop changed into do-while? It looks like it just makes the code longer, especially as you replace a % operator with open-coded equivalent for no reason I can see.

-- Keir

Quoted 57 lines
xen/arch/x86/hpet.c +15 -10
@@ -402,30 +402,35 @@ static void __init hpet_fsb_cap_lookup(v
 static struct hpet_event_channel *hpet_get_channel(unsigned int cpu)
 {
     static unsigned int next_channel;
-    static spinlock_t next_lock = SPIN_LOCK_UNLOCKED;
     unsigned int i, next;
     struct hpet_event_channel *ch;
 
     if ( num_hpets_used == 0 )
         return hpet_events;
 
-    spin_lock(&next_lock);
-    next = next_channel = (next_channel + 1) % num_hpets_used;
-    spin_unlock(&next_lock);
+    do {
+        next = next_channel;
+        if ( (i = next + 1) == num_hpets_used )
+            i = 0;
+    } while ( cmpxchg(&next_channel, next, i) != next );
 
     /* try unused channel first */
-    for ( i = next; i < next + num_hpets_used; i++ )
-    {
-        ch = &hpet_events[i % num_hpets_used];
+    ch = &hpet_events[i = next];
+    do {
         if ( !test_and_set_bit(HPET_EVT_USED_BIT, &ch->flags) )
         {
             ch->cpu = cpu;
             return ch;
         }
-    }
+        ++ch;
+        if ( ++i == num_hpets_used )
+        {
+            i = 0;
+            ch = hpet_events;
+        }
+    } while ( i != next );
 
-    /* share a in-use channel */
-    ch = &hpet_events[next];
+    /* Share an in-use channel. */
     if ( !test_and_set_bit(HPET_EVT_USED_BIT, &ch->flags) )
         ch->cpu = cpu;
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#2 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Keir Fraser
Quoted 3 lines · Jan Beulich

There's no need for a lock here, elimination of which makes the function a leaf one, thus allowing for better (and smaller) code.

Quoted 7 lines
Quoted 3 lines

Further, use the variable next_channel according to its name - so far it represented the most recently used channel rather than the next one to use.

Why is the following for-loop changed into do-while? It looks like it just makes the code longer, especially as you replace a % operator with open-coded equivalent for no reason I can see.

Simply because

    for ( i = next; i != next; ++i )

makes no sense.

Removing the % is an efficiency thing - the resulting code is not only faster, but also smaller (presumably because of the special register limitations div has).

Jan

Quoted 57 lines
Quoted 57 lines
xen/arch/x86/hpet.c +15 -10
@@ -402,30 +402,35 @@ static void __init hpet_fsb_cap_lookup(v
 static struct hpet_event_channel *hpet_get_channel(unsigned int cpu)
 {
     static unsigned int next_channel;
-    static spinlock_t next_lock = SPIN_LOCK_UNLOCKED;
     unsigned int i, next;
     struct hpet_event_channel *ch;
 
     if ( num_hpets_used == 0 )
         return hpet_events;
 
-    spin_lock(&next_lock);
-    next = next_channel = (next_channel + 1) % num_hpets_used;
-    spin_unlock(&next_lock);
+    do {
+        next = next_channel;
+        if ( (i = next + 1) == num_hpets_used )
+            i = 0;
+    } while ( cmpxchg(&next_channel, next, i) != next );
 
     /* try unused channel first */
-    for ( i = next; i < next + num_hpets_used; i++ )
-    {
-        ch = &hpet_events[i % num_hpets_used];
+    ch = &hpet_events[i = next];
+    do {
         if ( !test_and_set_bit(HPET_EVT_USED_BIT, &ch->flags) )
         {
             ch->cpu = cpu;
             return ch;
         }
-    }
+        ++ch;
+        if ( ++i == num_hpets_used )
+        {
+            i = 0;
+            ch = hpet_events;
+        }
+    } while ( i != next );
 
-    /* share a in-use channel */
-    ch = &hpet_events[next];
+    /* Share an in-use channel. */
     if ( !test_and_set_bit(HPET_EVT_USED_BIT, &ch->flags) )
         ch->cpu = cpu;
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw
Quoted 6 lines · Jan Beulich
Quoted 3 lines

Why is the following for-loop changed into do-while? It looks like it just makes the code longer, especially as you replace a % operator with open-coded equivalent for no reason I can see.

Simply because

Quoted 7 lines
    for ( i = next; i != next; ++i )

makes no sense.

Removing the % is an efficiency thing - the resulting code is not only faster, but also smaller (presumably because of the special register limitations div has).

Does that matter? It's the last thing done before going into deep sleep.

I suppose I don't mind that much, the whole thing just seems to make the C code longer.

-- Keir

#4 lore raw
Quoted 13 lines
Quoted 1 line
Quoted 1 line · Keir Fraser
Quoted 10 lines · Jan Beulich
Quoted 3 lines

Why is the following for-loop changed into do-while? It looks like it just makes the code longer, especially as you replace a % operator with open-coded equivalent for no reason I can see.

Simply because

    for ( i = next; i != next; ++i )

makes no sense.

Quoted 8 lines
Quoted 3 lines

Removing the % is an efficiency thing - the resulting code is not only faster, but also smaller (presumably because of the special register limitations div has).

Does that matter? It's the last thing done before going into deep sleep.

I suppose I don't mind that much, the whole thing just seems to make the C code longer.

Okay, let's forget about it then. (I could certainly make the code shorter using a few comma expressions, but I don't think you'd like that any better.)

Jan

#5 lore raw
Quoted 8 lines · Jan Beulich
Quoted 4 lines

Does that matter? It's the last thing done before going into deep sleep.

I suppose I don't mind that much, the whole thing just seems to make the C code longer.

Okay, let's forget about it then. (I could certainly make the code shorter using a few comma expressions, but I don't think you'd like that any better.)

Not really!

Getting rid of the lock and using cmpxchg, and making next_channel have the right meaning all makes sense. So a patch that just replaced the locked region, but didn't touch the following for-loop, would make sense to me. You can consider that Acked if you want to apply it.

-- Keir