Skip to contentSkip to conversation

xen/x86: cmpxchg cleanup

PATCH In review Roger Pau Monne 4 participants 17 messages Feb 22, 2024 – Feb 29, 2024

lore
Share & tools
lore
#0 lore raw

cover xen/x86: cmpxchg cleanup

Hello,

Following series replace a couple of cmpxchg loops with an atomic inc. The usage of such loops probably dates back to 32bit support, which didn't have an instruction to do an atomic 64bit addition.

Thanks, Roger.

Roger Pau Monne (2):
x86/memsharing: use an atomic add instead of a cmpxchg loop x86/hpet: use an atomic add instead of a cmpxchg loop

 xen/arch/x86/hpet.c           | 6 +-----
 xen/arch/x86/mm/mem_sharing.c | 8 +-------
 2 files changed, 2 insertions(+), 12 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 x86/memsharing: use an atomic add instead of a cmpxchg loop

The usage of a cmpxchg loop in get_next_handle() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

The cmpxchg usage is likely a remnant of 32bit support, which didn't have an instruction to do an atomic 64bit add, and instead a cmpxchg had to be used.

---
 xen/arch/x86/mm/mem_sharing.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
xen/arch/x86/mm/mem_sharing.c +1 -7
index 4f810706a315..fe299a2bf9aa 100644
@@ -179,13 +179,7 @@ static void mem_sharing_page_unlock(struct page_info *pg)
 
 static shr_handle_t get_next_handle(void)
 {
-    /* Get the next handle get_page style */
-    uint64_t x, y = next_handle;
-    do {
-        x = y;
-    }
-    while ( (y = cmpxchg(&next_handle, x, x + 1)) != x );
-    return x + 1;
+    return arch_fetch_and_add(&next_handle, 1) + 1;
 }
 
 static atomic_t nr_saved_mfns   = ATOMIC_INIT(0);
Signature · 1 line
2.43.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] x86/memsharing: use an atomic add instead of a cmpxchg loop

Quoted 9 lines · Roger Pau Monne

The usage of a cmpxchg loop in get_next_handle() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

The cmpxchg usage is likely a remnant of 32bit support, which didn't have an instruction to do an atomic 64bit add, and instead a cmpxchg had to be used.

albeit ...

Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +0 -0
@@ -179,13 +179,7 @@ static void mem_sharing_page_unlock(struct page_info *pg)
 
 static shr_handle_t get_next_handle(void)

xen/arch/x86/mm/mem_sharing.c static void mem_sharing_page_unlock(struct page_info *pg) · line 183

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +1 -7
 {
-    /* Get the next handle get_page style */
-    uint64_t x, y = next_handle;
-    do {
-        x = y;
-    }
-    while ( (y = cmpxchg(&next_handle, x, x + 1)) != x );
-    return x + 1;
+    return arch_fetch_and_add(&next_handle, 1) + 1;
 }

... the adding of 1 here is a little odd when taken together with next_handle's initializer. Tamas, you've not written that code, but do you have any thoughts towards the possible removal of either the initializer or the adding here? Plus that variable of course could very well do with moving into this function.

Jan

#3 lore raw

Re: [PATCH 1/2] x86/memsharing: use an atomic add instead of a cmpxchg loop

Quoted 28 lines · Jan Beulich
Quoted 9 lines · Roger Pau Monne

The usage of a cmpxchg loop in get_next_handle() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

The cmpxchg usage is likely a remnant of 32bit support, which didn't have an instruction to do an atomic 64bit add, and instead a cmpxchg had to be used.

albeit ...

Quoted 13 lines
xen/arch/x86/mm/mem_sharing.c +0 -7
@@ -179,13 +179,7 @@ static void mem_sharing_page_unlock(struct page_info *pg)
 
 static shr_handle_t get_next_handle(void)
 {
-    /* Get the next handle get_page style */
-    uint64_t x, y = next_handle;
-    do {
-        x = y;
-    }
-    while ( (y = cmpxchg(&next_handle, x, x + 1)) != x );
-    return x + 1;
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static void mem_sharing_page_unlock(struct page_info *pg) · line 183

Quoted 2 lines
xen/arch/x86/mm/mem_sharing.c +1 -0
+    return arch_fetch_and_add(&next_handle, 1) + 1;
 }

... the adding of 1 here is a little odd when taken together with next_handle's initializer. Tamas, you've not written that code, but do you have any thoughts towards the possible removal of either the initializer or the adding here? Plus that variable of course could very well do with moving into this function.

I have to say I find the existing logic here hard to parse but by the looks I don't think we need the + 1 once we switch to arch_fetch_and_add. Also could go without initializing next_handle to
1. Moving it into the function would not really accomplish anything other than style AFAICT?

Tamas

#4 lore raw

Re: [PATCH 1/2] x86/memsharing: use an atomic add instead of a cmpxchg loop

Quoted 37 lines · Tamas K Lengyel
Quoted 27 lines · Jan Beulich
Quoted 9 lines · Roger Pau Monne

The usage of a cmpxchg loop in get_next_handle() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

The cmpxchg usage is likely a remnant of 32bit support, which didn't have an instruction to do an atomic 64bit add, and instead a cmpxchg had to be used.

albeit ...

Quoted 13 lines
xen/arch/x86/mm/mem_sharing.c +0 -7
@@ -179,13 +179,7 @@ static void mem_sharing_page_unlock(struct page_info *pg)
 
 static shr_handle_t get_next_handle(void)
 {
-    /* Get the next handle get_page style */
-    uint64_t x, y = next_handle;
-    do {
-        x = y;
-    }
-    while ( (y = cmpxchg(&next_handle, x, x + 1)) != x );
-    return x + 1;
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static void mem_sharing_page_unlock(struct page_info *pg) · line 183

Quoted 2 lines
xen/arch/x86/mm/mem_sharing.c +1 -0
+    return arch_fetch_and_add(&next_handle, 1) + 1;
 }

... the adding of 1 here is a little odd when taken together with next_handle's initializer. Tamas, you've not written that code, but do you have any thoughts towards the possible removal of either the initializer or the adding here? Plus that variable of course could very well do with moving into this function.

Quoted 5 lines

I have to say I find the existing logic here hard to parse but by the looks I don't think we need the + 1 once we switch to arch_fetch_and_add. Also could go without initializing next_handle to
1. Moving it into the function would not really accomplish anything other than style AFAICT?

Well, limiting scope of things can be viewed as purely style, but I think it's more than that: It makes intentions more clear and reduces the chance of abuse (deliberate or unintentional).

Jan

#5 lore raw

Re: [PATCH 1/2] x86/memsharing: use an atomic add instead of a cmpxchg loop

Quoted 44 lines · Jan Beulich
Quoted 37 lines · Tamas K Lengyel
Quoted 27 lines · Jan Beulich
Quoted 9 lines · Roger Pau Monne

The usage of a cmpxchg loop in get_next_handle() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

The cmpxchg usage is likely a remnant of 32bit support, which didn't have an instruction to do an atomic 64bit add, and instead a cmpxchg had to be used.

albeit ...

Quoted 13 lines
xen/arch/x86/mm/mem_sharing.c +0 -7
@@ -179,13 +179,7 @@ static void mem_sharing_page_unlock(struct page_info *pg)
 
 static shr_handle_t get_next_handle(void)
 {
-    /* Get the next handle get_page style */
-    uint64_t x, y = next_handle;
-    do {
-        x = y;
-    }
-    while ( (y = cmpxchg(&next_handle, x, x + 1)) != x );
-    return x + 1;
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static void mem_sharing_page_unlock(struct page_info *pg) · line 183

Quoted 2 lines
xen/arch/x86/mm/mem_sharing.c +1 -0
+    return arch_fetch_and_add(&next_handle, 1) + 1;
 }

... the adding of 1 here is a little odd when taken together with next_handle's initializer. Tamas, you've not written that code, but do you have any thoughts towards the possible removal of either the initializer or the adding here? Plus that variable of course could very well do with moving into this function.

Quoted 5 lines

I have to say I find the existing logic here hard to parse but by the looks I don't think we need the + 1 once we switch to arch_fetch_and_add. Also could go without initializing next_handle to
1. Moving it into the function would not really accomplish anything other than style AFAICT?

Quoted 3 lines

Well, limiting scope of things can be viewed as purely style, but I think it's more than that: It makes intentions more clear and reduces the chance of abuse (deliberate or unintentional).

I'm afraid that whatever is the outcome here, I will defer it to a further commit, since the purpose here is to be a non-functional change.

Thanks, Roger.

#6 lore raw

Re: [PATCH 1/2] x86/memsharing: use an atomic add instead of a cmpxchg loop

Quoted 45 lines · Roger Pau Monné
Quoted 44 lines · Jan Beulich
Quoted 42 lines · Tamas K Lengyel
Quoted 27 lines · Jan Beulich
Quoted 9 lines · Roger Pau Monne

The usage of a cmpxchg loop in get_next_handle() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

The cmpxchg usage is likely a remnant of 32bit support, which didn't have an instruction to do an atomic 64bit add, and instead a cmpxchg had to be used.

albeit ...

Quoted 13 lines
xen/arch/x86/mm/mem_sharing.c +0 -7
@@ -179,13 +179,7 @@ static void mem_sharing_page_unlock(struct page_info *pg)
 
 static shr_handle_t get_next_handle(void)
 {
-    /* Get the next handle get_page style */
-    uint64_t x, y = next_handle;
-    do {
-        x = y;
-    }
-    while ( (y = cmpxchg(&next_handle, x, x + 1)) != x );
-    return x + 1;
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static void mem_sharing_page_unlock(struct page_info *pg) · line 183

Quoted 2 lines
xen/arch/x86/mm/mem_sharing.c +1 -0
+    return arch_fetch_and_add(&next_handle, 1) + 1;
 }

... the adding of 1 here is a little odd when taken together with next_handle's initializer. Tamas, you've not written that code, but do you have any thoughts towards the possible removal of either the initializer or the adding here? Plus that variable of course could very well do with moving into this function.

I have to say I find the existing logic here hard to parse but by the looks I don't think we need the + 1 once we switch to arch_fetch_and_add. Also could go without initializing next_handle to
1. Moving it into the function would not really accomplish anything other than style AFAICT?

Quoted 7 lines
Quoted 3 lines

Well, limiting scope of things can be viewed as purely style, but I think it's more than that: It makes intentions more clear and reduces the chance of abuse (deliberate or unintentional).

I'm afraid that whatever is the outcome here, I will defer it to a further commit, since the purpose here is to be a non-functional change.

That's fine with me, but an ack from Tamas is still pending, unless I missed something somewhere.

Jan

#7 lore raw

Re: [PATCH 1/2] x86/memsharing: use an atomic add instead of a cmpxchg loop

Quoted 50 lines · Jan Beulich
Quoted 49 lines · Roger Pau Monné
Quoted 47 lines · Jan Beulich
Quoted 37 lines · Tamas K Lengyel
Quoted 27 lines · Jan Beulich
Quoted 9 lines · Roger Pau Monne

The usage of a cmpxchg loop in get_next_handle() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

The cmpxchg usage is likely a remnant of 32bit support, which didn't have an instruction to do an atomic 64bit add, and instead a cmpxchg had to be used.

albeit ...

Quoted 13 lines
xen/arch/x86/mm/mem_sharing.c +0 -7
@@ -179,13 +179,7 @@ static void mem_sharing_page_unlock(struct page_info *pg)
 
 static shr_handle_t get_next_handle(void)
 {
-    /* Get the next handle get_page style */
-    uint64_t x, y = next_handle;
-    do {
-        x = y;
-    }
-    while ( (y = cmpxchg(&next_handle, x, x + 1)) != x );
-    return x + 1;
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static void mem_sharing_page_unlock(struct page_info *pg) · line 183

Quoted 2 lines
xen/arch/x86/mm/mem_sharing.c +1 -0
+    return arch_fetch_and_add(&next_handle, 1) + 1;
 }

... the adding of 1 here is a little odd when taken together with next_handle's initializer. Tamas, you've not written that code, but do you have any thoughts towards the possible removal of either the initializer or the adding here? Plus that variable of course could very well do with moving into this function.

Quoted 5 lines

I have to say I find the existing logic here hard to parse but by the looks I don't think we need the + 1 once we switch to arch_fetch_and_add. Also could go without initializing next_handle to
1. Moving it into the function would not really accomplish anything other than style AFAICT?

Well, limiting scope of things can be viewed as purely style, but I think it's more than that: It makes intentions more clear and reduces the chance of abuse (deliberate or unintentional).

Quoted 6 lines
Quoted 3 lines

I'm afraid that whatever is the outcome here, I will defer it to a further commit, since the purpose here is to be a non-functional change.

That's fine with me, but an ack from Tamas is still pending, unless I missed something somewhere.

No, just wanted to clarify that I wasn't expecting to do further changes here, FTAOD. Not sure if Tamas was expecting me to further adjust the code.

Thanks, Roger.

#8 lore raw

Re: [PATCH 1/2] x86/memsharing: use an atomic add instead of a cmpxchg loop

Quoted 56 lines · Roger Pau Monné
Quoted 54 lines · Jan Beulich
Quoted 52 lines · Roger Pau Monné
Quoted 44 lines · Jan Beulich
Quoted 37 lines · Tamas K Lengyel
Quoted 27 lines · Jan Beulich
Quoted 9 lines · Roger Pau Monne

The usage of a cmpxchg loop in get_next_handle() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

The cmpxchg usage is likely a remnant of 32bit support, which didn't have an instruction to do an atomic 64bit add, and instead a cmpxchg had to be used.

albeit ...

Quoted 13 lines
xen/arch/x86/mm/mem_sharing.c +0 -7
@@ -179,13 +179,7 @@ static void mem_sharing_page_unlock(struct page_info *pg)
 
 static shr_handle_t get_next_handle(void)
 {
-    /* Get the next handle get_page style */
-    uint64_t x, y = next_handle;
-    do {
-        x = y;
-    }
-    while ( (y = cmpxchg(&next_handle, x, x + 1)) != x );
-    return x + 1;
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static void mem_sharing_page_unlock(struct page_info *pg) · line 183

Quoted 2 lines
xen/arch/x86/mm/mem_sharing.c +1 -0
+    return arch_fetch_and_add(&next_handle, 1) + 1;
 }

... the adding of 1 here is a little odd when taken together with next_handle's initializer. Tamas, you've not written that code, but do you have any thoughts towards the possible removal of either the initializer or the adding here? Plus that variable of course could very well do with moving into this function.

Quoted 5 lines

I have to say I find the existing logic here hard to parse but by the looks I don't think we need the + 1 once we switch to arch_fetch_and_add. Also could go without initializing next_handle to
1. Moving it into the function would not really accomplish anything other than style AFAICT?

Quoted 3 lines

Well, limiting scope of things can be viewed as purely style, but I think it's more than that: It makes intentions more clear and reduces the chance of abuse (deliberate or unintentional).

I'm afraid that whatever is the outcome here, I will defer it to a further commit, since the purpose here is to be a non-functional change.

Quoted 6 lines
Quoted 2 lines

That's fine with me, but an ack from Tamas is still pending, unless I missed something somewhere.

No, just wanted to clarify that I wasn't expecting to do further changes here, FTAOD. Not sure if Tamas was expecting me to further adjust the code.

Reviewed-by on v1 1/2
#9 lore raw

Re: [PATCH 1/2] x86/memsharing: use an atomic add instead of a cmpxchg loop

In reply to Roger Pau Monne

Hi Roger,

Quoted 9 lines · Roger Pau Monne

The usage of a cmpxchg loop in get_next_handle() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

The cmpxchg usage is likely a remnant of 32bit support, which didn't have an instruction to do an atomic 64bit add, and instead a cmpxchg had to be used.

Cheers,

Signature · 1 line
Julien Grall
#10 lore raw

Re: [PATCH 1/2] x86/memsharing: use an atomic add instead of a cmpxchg loop

In reply to Roger Pau Monne
Quoted 9 lines · Roger Pau Monne

The usage of a cmpxchg loop in get_next_handle() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

The cmpxchg usage is likely a remnant of 32bit support, which didn't have an instruction to do an atomic 64bit add, and instead a cmpxchg had to be used.

Hmm, same typo again, so my commit script again got the author wrong. I'm sorry again. I would consider adding an explicit confirmation step when there's no (well-formed) S-o-b, but I use this script elsewhere as well, and in e.g. binutils and gcc S-o-b are pretty uncommon. (And to avoid the question: I'm specifically avoiding From: itself in the common case, as for you and other people with non-ASCII characters in their names those typically don't properly show up there.)

Jan

#11 lore raw

PATCH 2/2 x86/hpet: use an atomic add instead of a cmpxchg loop

In reply to Roger Pau Monne

The usage of a cmpxchg loop in hpet_get_channel() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

Note there can be a small divergence in the channel returned if next_channel overflows, but returned channel will always be in the [0, num_hpets_used) range, and that's fine for the purpose of balancing HPET channels across CPUs. This is also theoretical, as there's no system currently with 2^32 CPUs (as long as next_channel is 32bit width).

---
 xen/arch/x86/hpet.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
xen/arch/x86/hpet.c +1 -5
index d982b0f6b2c9..4777dc859d96 100644
@@ -458,11 +458,7 @@ static struct hpet_event_channel *hpet_get_channel(unsigned int cpu)
     if ( num_hpets_used >= nr_cpu_ids )
         return &hpet_events[cpu];
 
-    do {
-        next = next_channel;
-        if ( (i = next + 1) == num_hpets_used )
-            i = 0;
-    } while ( cmpxchg(&next_channel, next, i) != next );
+    next = arch_fetch_and_add(&next_channel, 1) % num_hpets_used;
 
     /* try unused channel first */
     for ( i = next; i < next + num_hpets_used; i++ )
Signature · 1 line
2.43.0
#12 lore raw

Re: [PATCH 2/2] x86/hpet: use an atomic add instead of a cmpxchg loop

Quoted 9 lines · Roger Pau Monne

The usage of a cmpxchg loop in hpet_get_channel() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

Note there can be a small divergence in the channel returned if next_channel overflows, but returned channel will always be in the [0, num_hpets_used) range, and that's fine for the purpose of balancing HPET channels across CPUs. This is also theoretical, as there's no system currently with 2^32 CPUs (as long as next_channel is 32bit width).

The code change looks good to me, but I fail to see the connection to 2^32 CPUs. So it feels like I'm missing something, in which case I'd rather not offer any R-b.

Jan

Quoted 22 lines
---
 xen/arch/x86/hpet.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
xen/arch/x86/hpet.c +1 -5
index d982b0f6b2c9..4777dc859d96 100644
@@ -458,11 +458,7 @@ static struct hpet_event_channel *hpet_get_channel(unsigned int cpu)
     if ( num_hpets_used >= nr_cpu_ids )
         return &hpet_events[cpu];
 
-    do {
-        next = next_channel;
-        if ( (i = next + 1) == num_hpets_used )
-            i = 0;
-    } while ( cmpxchg(&next_channel, next, i) != next );
+    next = arch_fetch_and_add(&next_channel, 1) % num_hpets_used;
 
     /* try unused channel first */
     for ( i = next; i < next + num_hpets_used; i++ )
#13 lore raw

Re: [PATCH 2/2] x86/hpet: use an atomic add instead of a cmpxchg loop

Quoted 11 lines · Jan Beulich
Quoted 9 lines · Roger Pau Monne

The usage of a cmpxchg loop in hpet_get_channel() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

Note there can be a small divergence in the channel returned if next_channel overflows, but returned channel will always be in the [0, num_hpets_used) range, and that's fine for the purpose of balancing HPET channels across CPUs. This is also theoretical, as there's no system currently with 2^32 CPUs (as long as next_channel is 32bit width).

Quoted 3 lines

The code change looks good to me, but I fail to see the connection to 2^32 CPUs. So it feels like I'm missing something, in which case I'd rather not offer any R-b.

The purpose of hpet_get_channel() is to distribute HPET channels across CPUs, so that each CPU gets assigned an HPET channel, balancing the number of CPUs that use each channel.

This is done by (next_channel++ % num_hpets_used), so the value of next_channel after this change can be > num_hpets_used, which previously wasn't. This can lead to a different channel returned for the 2^32 call to the function, as the counter would overflow. Note calls to the function are restricted to the number of CPUs in the host, as per-CPU channel assignment is done only once (and the channel is then stored in a per-cpu variable).

Feel free to adjust the commit message if you think all this is too much data, or too confusing.

Thanks, Roger.

Reviewed-by
#14 lore raw

Re: [PATCH 2/2] x86/hpet: use an atomic add instead of a cmpxchg loop

Quoted 20 lines · Roger Pau Monné
Quoted 11 lines · Jan Beulich
Quoted 9 lines · Roger Pau Monne

The usage of a cmpxchg loop in hpet_get_channel() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

Note there can be a small divergence in the channel returned if next_channel overflows, but returned channel will always be in the [0, num_hpets_used) range, and that's fine for the purpose of balancing HPET channels across CPUs. This is also theoretical, as there's no system currently with 2^32 CPUs (as long as next_channel is 32bit width).

Quoted 3 lines

The code change looks good to me, but I fail to see the connection to 2^32 CPUs. So it feels like I'm missing something, in which case I'd rather not offer any R-b.

The purpose of hpet_get_channel() is to distribute HPET channels across CPUs, so that each CPU gets assigned an HPET channel, balancing the number of CPUs that use each channel.

Quoted 7 lines

This is done by (next_channel++ % num_hpets_used), so the value of next_channel after this change can be > num_hpets_used, which previously wasn't. This can lead to a different channel returned for the 2^32 call to the function, as the counter would overflow. Note calls to the function are restricted to the number of CPUs in the host, as per-CPU channel assignment is done only once (and the channel is then stored in a per-cpu variable).

That's once per CPU being brought up, not once per CPU in the system.

Quoted 2 lines

Feel free to adjust the commit message if you think all this is too much data, or too confusing.

I'll simply drop that last sentence then, without which

Jan

Reviewed-by on v1 2/2
#15 lore raw

Re: [PATCH 2/2] x86/hpet: use an atomic add instead of a cmpxchg loop

In reply to Roger Pau Monne

Hi Roger,

Quoted 9 lines · Roger Pau Monne

The usage of a cmpxchg loop in hpet_get_channel() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

Note there can be a small divergence in the channel returned if next_channel overflows, but returned channel will always be in the [0, num_hpets_used) range, and that's fine for the purpose of balancing HPET channels across CPUs. This is also theoretical, as there's no system currently with 2^32 CPUs (as long as next_channel is 32bit width).

This would also happen if we decide to reduce the size next_channel.

Cheers,

Signature · 1 line
Julien Grall
#16 lore raw

Re: [PATCH 2/2] x86/hpet: use an atomic add instead of a cmpxchg loop

In reply to Roger Pau Monne
Quoted 11 lines · Roger Pau Monne

The usage of a cmpxchg loop in hpet_get_channel() is unnecessary, as the same can be achieved with an atomic increment, which is both simpler to read, and avoid any need for a loop.

Note there can be a small divergence in the channel returned if next_channel overflows, but returned channel will always be in the [0, num_hpets_used) range, and that's fine for the purpose of balancing HPET channels across CPUs. This is also theoretical, as there's no system currently with 2^32 CPUs (as long as next_channel is 32bit width).

Hmm, I'm sorry - it's now me who is recorded as the author of the patch, for my script not finding any Signed-off-by: here (note the typo).

Jan