Patchwork [v2] numactl: fix libnuma on big-endian 64-bit systems

login
register
mail settings
Submitter Arnd Bergmann
Date Dec. 4, 2008, 8:51 p.m.
Message ID <200812042151.58676.arnd@arndb.de>
Download mbox | patch
Permalink /patch/12314/
State Not Applicable, archived
Headers show

Comments

Arnd Bergmann - Dec. 4, 2008, 8:51 p.m.
The read-mask function assumes that it is running in 32-bit mode,
by addressing the bitmask as a series of int values, instead of
longs. This is broken as can easily be reproduced by running numademo
on a bit-endian 64-bit system.

Changing the addressing to use 'long' values fixes the problem.

Reported-by: Mijo Safradin <safradin@de.ibm.com>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>

---
 libnuma.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
Arnd Bergmann - Dec. 5, 2008, 3:12 p.m.
On Thursday 04 December 2008, Arnd Bergmann wrote:
> The read-mask function assumes that it is running in 32-bit mode,
> by addressing the bitmask as a series of int values, instead of
> longs. This is broken as can easily be reproduced by running numademo
> on a bit-endian 64-bit system.
> 
> Changing the addressing to use 'long' values fixes the problem.

Unfortunately, this is still wrong, as it tries recreating a kernel
data structure that is represented as a series of 'int' values, just
in a different order.

What we really need is the reverse of bitmap_scnprintf from
linux/lib/bitmap.c. I don't have access to a little-endian NUMA
machine with more than 64 CPUs, so I really don't want to send
another embarrassingly wrong patch for this.

Can anyone else try to come up with a version that handles endianess
correctly and still works on x86-64?

	Arnd <><

Patch

diff --git a/libnuma.c b/libnuma.c
index 4d26093..9a9fbbe 100755
--- a/libnuma.c
+++ b/libnuma.c
@@ -376,9 +376,9 @@  read_mask(char *s, struct bitmask *bmp)
 {
 	char *end = s;
 	char *prevend;
-	unsigned int *start = (unsigned int *)bmp->maskp;
-	unsigned int *p = start;
-	unsigned int *q;
+	unsigned long *start = bmp->maskp;
+	unsigned long *p = start;
+	unsigned long *q;
 	unsigned int i;
 	unsigned int n = 0;
 
@@ -415,14 +415,14 @@  read_mask(char *s, struct bitmask *bmp)
 	}
 
 	/* Poor mans fls() */
-	for(i = 31; i >= 0; i--)
+	for(i = sizeof(long) * 8 - 1; i >= 0; i--)
 		if (test_bit(i, start + n))
 			break;
 
 	/*
 	 * Return the last bit set
 	 */
-	return ((sizeof(unsigned int)*8) * n) + i;
+	return ((sizeof(unsigned long)*8) * n) + i;
 }
 
 /*