Skip to contentSkip to conversation

x86, bitops, variable_test_bit should return 1 not -1 on a match

PATCH Superseded Prarit Bhargava 3 participants 10 messages Aug 19, 2015 – Oct 8, 2015

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

PATCH 1/1 x86, bitops, variable_test_bit should return 1 not -1 on a match

This issue was noticed while debugging a CPU hotplug issue. On x86 with (NR_CPUS > 1) the cpu_online() define is cpumask_test_cpu(). cpumask_test_cpu() should return 1 if the cpu is set in cpumask and 0 otherwise.

However, cpumask_test_cpu() returns -1 if the cpu in the cpumask is set and 0 otherwise. This happens because cpumask_test_cpu() calls test_bit() which is a define that will call variable_test_bit().

variable_test_bit() calls the assembler instruction sbb (Subtract with Borrow, " Subtracts the source from the destination, and subtracts 1 extra if the Carry Flag is set. Results are returned in "dest".)

A bit match results in -1 being returned from variable_test_bit() if a match occurs, not 1 as the function is supposed to. This can be easily resolved by adding a "!!" to force 0 or 1 as a return.

It looks like the code never does, for example, (test_bit() == 1) so this change should not have any impact.

---
 arch/x86/include/asm/bitops.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/bitops.h +1 -1
index cfe3b95..a87a5fb 100644
@@ -320,7 +320,7 @@ static inline int variable_test_bit(long nr, volatile const unsigned long *addr)
 		     : "=r" (oldbit)
 		     : "m" (*(unsigned long *)addr), "Ir" (nr));
 
-	return oldbit;
+	return !!oldbit;
 }
 
 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
Signature · 1 line
1.7.9.3
#1 lore raw
Quoted 35 lines · Prarit Bhargava

This issue was noticed while debugging a CPU hotplug issue. On x86 with (NR_CPUS > 1) the cpu_online() define is cpumask_test_cpu(). cpumask_test_cpu() should return 1 if the cpu is set in cpumask and 0 otherwise.

However, cpumask_test_cpu() returns -1 if the cpu in the cpumask is set and 0 otherwise. This happens because cpumask_test_cpu() calls test_bit() which is a define that will call variable_test_bit().

variable_test_bit() calls the assembler instruction sbb (Subtract with Borrow, " Subtracts the source from the destination, and subtracts 1 extra if the Carry Flag is set. Results are returned in "dest".)

A bit match results in -1 being returned from variable_test_bit() if a match occurs, not 1 as the function is supposed to. This can be easily resolved by adding a "!!" to force 0 or 1 as a return.

It looks like the code never does, for example, (test_bit() == 1) so this change should not have any impact.

---
 arch/x86/include/asm/bitops.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/bitops.h +0 -0
index cfe3b95..a87a5fb 100644

arch/x86/include/asm/bitops.h static inline int variable_test_bit(long nr, volatile const unsigned long *addr) · line 326

Quoted 8 lines
arch/x86/include/asm/bitops.h +1 -1
@@ -320,7 +320,7 @@ static inline int variable_test_bit(long nr, volatile const unsigned long *addr)
 		     : "=r" (oldbit)
 		     : "m" (*(unsigned long *)addr), "Ir" (nr));
 
-	return oldbit;
+	return !!oldbit;
 }
 
 #if 0 /* Fool kernel-doc since it doesn't do macros yet */

Ok, I think this is a good fix to improve the robustness of this primitive, unless someone objects.

I tried to find the CPU hotplug code that broke with cpu_online() returning -1 but failed - all current mainline usage sites seem to be testing for nonzero in one way or another. Could you please point it out?

Thanks,

Ingo

#2 lore raw

Wrong fix, though. Instead we should change it to use the set instruction, which would also make it easier to use the CC_SET/CC_OUT proposed macros to use assembly out in the future.

The downside with set is that it only sets a single byte, the upside is that it always outputs 0 or 1, and apparently if the output variable is your bool gcc can use that for optimization.

Quoted 66 lines · Ingo Molnar
Quoted 11 lines · Prarit Bhargava

This issue was noticed while debugging a CPU hotplug issue. On x86 with (NR_CPUS > 1) the cpu_online() define is cpumask_test_cpu(). cpumask_test_cpu() should return 1 if the cpu is set in cpumask and 0 otherwise.

However, cpumask_test_cpu() returns -1 if the cpu in the cpumask is set and 0 otherwise. This happens because cpumask_test_cpu() calls test_bit() which is a define that will call variable_test_bit().

variable_test_bit() calls the assembler instruction sbb (Subtract with Borrow, " Subtracts the source from the destination, and

subtracts 1

Quoted 3 lines

extra if the Carry Flag is set. Results are returned in "dest".)

A bit match results in -1 being returned from variable_test_bit() if

a

Quoted 1 line

match occurs, not 1 as the function is supposed to. This can be

easily

Quoted 3 lines

resolved by adding a "!!" to force 0 or 1 as a return.

It looks like the code never does, for example, (test_bit() == 1) so

this

Quoted 8 lines

change should not have any impact.

Review

Quoted 5 lines
---
 arch/x86/include/asm/bitops.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
(quoted diff) +0 -0

b/arch/x86/include/asm/bitops.h

arch/x86/include/asm/bitops.h static inline int variable_test_bit(long nr,

Quoted 4 lines

index cfe3b95..a87a5fb 100644

arch/x86/include/asm/bitops.h +0 -0
@@ -320,7 +320,7 @@ static inline int variable_test_bit(long nr,

volatile const unsigned long *addr)

arch/x86/include/asm/bitops.h static inline int variable_test_bit(long nr, volatile const unsigned long *addr) · line 326

Quoted 8 lines
arch/x86/include/asm/bitops.h +1 -1
 		     : "=r" (oldbit)
 		     : "m" (*(unsigned long *)addr), "Ir" (nr));
 
-	return oldbit;
+	return !!oldbit;
 }
 
 #if 0 /* Fool kernel-doc since it doesn't do macros yet */

Ok, I think this is a good fix to improve the robustness of this primitive, unless
someone objects.

I tried to find the CPU hotplug code that broke with cpu_online() returning -1 but
failed - all current mainline usage sites seem to be testing for nonzero in one
way or another. Could you please point it out?

Thanks,

Ingo

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
#3 lore raw
Quoted 4 lines · H. Peter Anvin

Wrong fix, though. Instead we should change it to use the set instruction, which would also make it easier to use the CC_SET/CC_OUT proposed macros to use assembly out in the future.

The downside with set is that it only sets a single byte, the upside is that it always outputs 0 or 1, and apparently if the output variable is your bool gcc can use that for optimization.

hpa -- can you send a pointer to that discussion?

Thanks,

P.

#4 lore raw

PATCH v2 1/1 x86, bitops, variable_test_bit should return 1 not -1 on a match

In reply to H. Peter Anvin

This issue was noticed while debugging a CPU hotplug issue. On x86 with (NR_CPUS > 1) the cpu_online() define is cpumask_test_cpu(). cpumask_test_cpu() should return 1 if the cpu is set in cpumask and 0 otherwise.

However, cpumask_test_cpu() returns -1 if the cpu in the cpumask is set and 0 otherwise. This happens because cpumask_test_cpu() calls test_bit() which is a define that will call variable_test_bit().

variable_test_bit() calls the assembler instruction sbb (Subtract with Borrow, " Subtracts the source from the destination, and subtracts 1 extra if the Carry Flag is set. Results are returned in "dest".)

A bit match results in -1 being returned from variable_test_bit() if a match occurs, not 1 as the function is supposed to.

It looks like the code never does, for example, (test_bit() == 1) so this change should not have any impact.

[v2]: hpa: Use setc, (Set if Carry, "Sets the byte in the operand to 1 if the Carry Flag is set, otherwise sets the operand to 0.") instead of !!

---
 arch/x86/include/asm/bitops.h |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/bitops.h +2 -2
index cfe3b95..c0bff87 100644
@@ -313,10 +313,10 @@ static __always_inline int constant_test_bit(long nr, const volatile unsigned lo
 
 static inline int variable_test_bit(long nr, volatile const unsigned long *addr)
 {
-	int oldbit;
+	u8 oldbit;
 
 	asm volatile("bt %2,%1\n\t"
-		     "sbb %0,%0"
+		     "setc %0"
 		     : "=r" (oldbit)
 		     : "m" (*(unsigned long *)addr), "Ir" (nr));
 
Signature · 1 line
1.7.9.3
#5 lore raw

re-ping on this. Just making sure this wasn't dropped on the floor.

P.

Quoted 50 lines · Prarit Bhargava

This issue was noticed while debugging a CPU hotplug issue. On x86 with (NR_CPUS > 1) the cpu_online() define is cpumask_test_cpu(). cpumask_test_cpu() should return 1 if the cpu is set in cpumask and 0 otherwise.

However, cpumask_test_cpu() returns -1 if the cpu in the cpumask is set and 0 otherwise. This happens because cpumask_test_cpu() calls test_bit() which is a define that will call variable_test_bit().

variable_test_bit() calls the assembler instruction sbb (Subtract with Borrow, " Subtracts the source from the destination, and subtracts 1 extra if the Carry Flag is set. Results are returned in "dest".)

A bit match results in -1 being returned from variable_test_bit() if a match occurs, not 1 as the function is supposed to.

It looks like the code never does, for example, (test_bit() == 1) so this change should not have any impact.

[v2]: hpa: Use setc, (Set if Carry, "Sets the byte in the operand to 1 if the Carry Flag is set, otherwise sets the operand to 0.") instead of !!

---
 arch/x86/include/asm/bitops.h |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/bitops.h +2 -2
index cfe3b95..c0bff87 100644
@@ -313,10 +313,10 @@ static __always_inline int constant_test_bit(long nr, const volatile unsigned lo
 
 static inline int variable_test_bit(long nr, volatile const unsigned long *addr)
 {
-	int oldbit;
+	u8 oldbit;
 
 	asm volatile("bt %2,%1\n\t"
-		     "sbb %0,%0"
+		     "setc %0"
 		     : "=r" (oldbit)
 		     : "m" (*(unsigned long *)addr), "Ir" (nr));
 
#6 lore raw
Quoted 1 line · Prarit Bhargava

re-ping on this. Just making sure this wasn't dropped on the floor.

So I didn't apply it back when I saw your patch because I didn't see where you addressed/analyzed the second paragraph of hpa's review:

"The downside with set is that it only sets a single byte, the upside is that it
always outputs 0 or 1, and apparently if the output variable is your bool gcc can use that for optimization."

That's a valid concern and should be addressed.

Thanks,

Ingo

#7 lore raw
In reply to H. Peter Anvin
Quoted 4 lines · H. Peter Anvin

Wrong fix, though. Instead we should change it to use the set instruction, which would also make it easier to use the CC_SET/CC_OUT proposed macros to use assembly out in the future.

The downside with set is that it only sets a single byte, the upside is that it always outputs 0 or 1, and apparently if the output variable is your bool gcc can use that for optimization.

hpa, I didn't realize your comment was suggesting a change. I've done a google search on "gcc bool optimization" (and various incantations of that) and didn't find anything that lead me in the direction of making a change. Could you elaborate on what the issue is?

Thanks,

P.

#8 lore raw
In reply to Ingo Molnar
Quoted 45 lines · Ingo Molnar
Quoted 42 lines · Prarit Bhargava

This issue was noticed while debugging a CPU hotplug issue. On x86 with (NR_CPUS > 1) the cpu_online() define is cpumask_test_cpu(). cpumask_test_cpu() should return 1 if the cpu is set in cpumask and 0 otherwise.

However, cpumask_test_cpu() returns -1 if the cpu in the cpumask is set and 0 otherwise. This happens because cpumask_test_cpu() calls test_bit() which is a define that will call variable_test_bit().

variable_test_bit() calls the assembler instruction sbb (Subtract with Borrow, " Subtracts the source from the destination, and subtracts 1 extra if the Carry Flag is set. Results are returned in "dest".)

A bit match results in -1 being returned from variable_test_bit() if a match occurs, not 1 as the function is supposed to. This can be easily resolved by adding a "!!" to force 0 or 1 as a return.

It looks like the code never does, for example, (test_bit() == 1) so this change should not have any impact.

---
 arch/x86/include/asm/bitops.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/bitops.h +1 -1
index cfe3b95..a87a5fb 100644
@@ -320,7 +320,7 @@ static inline int variable_test_bit(long nr, volatile const unsigned long *addr)
 		     : "=r" (oldbit)
 		     : "m" (*(unsigned long *)addr), "Ir" (nr));
 
-	return oldbit;
+	return !!oldbit;
 }
 
Quoted 8 lines

arch/x86/include/asm/bitops.h static inline int variable_test_bit(long nr, volatile const unsigned long *addr) · line 326

Quoted 1 line
arch/x86/include/asm/bitops.h +0 -0
 #if 0 /* Fool kernel-doc since it doesn't do macros yet */

Ok, I think this is a good fix to improve the robustness of this primitive, unless someone objects.

I tried to find the CPU hotplug code that broke with cpu_online() returning -1 but failed - all current mainline usage sites seem to be testing for nonzero in one way or another. Could you please point it out?

I'm sorry Ingo, I think my description may have confused you. I was debugging a cpu hotplug issue[1] and did

printk("cpu %d cpu online status %d\n", cpu, cpu_online(cpu));

as a debug printk. This printed out

cpu 3 cpu online status -1

which was really confusing. That lead me down the rabbit hole of looking at the sbb assembler instruction in variable_test_bit() to figure out why I was seeing -1.

P.

[1] The bug looks like it has to do with the system's firmware, not cpu hotplug.

#9 lore raw
Quoted 60 lines · Prarit Bhargava
Quoted 45 lines · Ingo Molnar
Quoted 42 lines · Prarit Bhargava

This issue was noticed while debugging a CPU hotplug issue. On x86 with (NR_CPUS > 1) the cpu_online() define is cpumask_test_cpu(). cpumask_test_cpu() should return 1 if the cpu is set in cpumask and 0 otherwise.

However, cpumask_test_cpu() returns -1 if the cpu in the cpumask is set and 0 otherwise. This happens because cpumask_test_cpu() calls test_bit() which is a define that will call variable_test_bit().

variable_test_bit() calls the assembler instruction sbb (Subtract with Borrow, " Subtracts the source from the destination, and subtracts 1 extra if the Carry Flag is set. Results are returned in "dest".)

A bit match results in -1 being returned from variable_test_bit() if a match occurs, not 1 as the function is supposed to. This can be easily resolved by adding a "!!" to force 0 or 1 as a return.

It looks like the code never does, for example, (test_bit() == 1) so this change should not have any impact.

---
 arch/x86/include/asm/bitops.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/bitops.h +1 -1
index cfe3b95..a87a5fb 100644
@@ -320,7 +320,7 @@ static inline int variable_test_bit(long nr, volatile const unsigned long *addr)
 		     : "=r" (oldbit)
 		     : "m" (*(unsigned long *)addr), "Ir" (nr));
 
-	return oldbit;
+	return !!oldbit;
 }
 
Quoted 8 lines

arch/x86/include/asm/bitops.h static inline int variable_test_bit(long nr, volatile const unsigned long *addr) · line 326

Quoted 1 line
arch/x86/include/asm/bitops.h +0 -0
 #if 0 /* Fool kernel-doc since it doesn't do macros yet */

Ok, I think this is a good fix to improve the robustness of this primitive, unless someone objects.

I tried to find the CPU hotplug code that broke with cpu_online() returning -1 but failed - all current mainline usage sites seem to be testing for nonzero in one way or another. Could you please point it out?

I'm sorry Ingo, I think my description may have confused you. I was debugging a cpu hotplug issue[1] and did

Quoted 8 lines

printk("cpu %d cpu online status %d\n", cpu, cpu_online(cpu));

as a debug printk. This printed out

cpu 3 cpu online status -1

which was really confusing. That lead me down the rabbit hole of looking at the sbb assembler instruction in variable_test_bit() to figure out why I was seeing -1.

Ok, fair enough!

Still worth fixing IMHO.

Thanks,

Ingo