Skip to contentSkip to conversation

byteorder: sanity check toolchain vs kernel endianess

RFC PATCH In review Christoph Hellwig 6 participants 14 messages Apr 12, 2019 – May 30, 2019 Generic include/asm header files

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 byteorder: sanity check toolchain vs kernel endianess

When removing some dead big endian checks in the RISC-V code Nick suggested that we should have some generic sanity checks. I don't think we should have thos inside the RISC-V code, but maybe it might make sense to have these in the generic byteorder headers. Note that these are UAPI headers and some compilers might not actually define __BYTE_ORDER__, so we first check that it actually exists.

---
 include/uapi/linux/byteorder/big_endian.h    | 4 ++++
 include/uapi/linux/byteorder/little_endian.h | 4 ++++
 2 files changed, 8 insertions(+)
include/uapi/linux/byteorder/big_endian.h +4 -0
index 2199adc6a6c2..34a5864526d2 100644
@@ -2,6 +2,10 @@
 #ifndef _UAPI_LINUX_BYTEORDER_BIG_ENDIAN_H
 #define _UAPI_LINUX_BYTEORDER_BIG_ENDIAN_H
 
+#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__
+#error "Unsupported endianess, check your toolchain"
+#endif
+
 #ifndef __BIG_ENDIAN
 #define __BIG_ENDIAN 4321
 #endif
include/uapi/linux/byteorder/little_endian.h +4 -0
index 601c904fd5cd..0cdf3583e19f 100644
@@ -2,6 +2,10 @@
 #ifndef _UAPI_LINUX_BYTEORDER_LITTLE_ENDIAN_H
 #define _UAPI_LINUX_BYTEORDER_LITTLE_ENDIAN_H
 
+#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ !=  __ORDER_LITTLE_ENDIAN__
+#error "Unsupported endianess, check your toolchain"
+#endif
+
 #ifndef __LITTLE_ENDIAN
 #define __LITTLE_ENDIAN 1234
 #endif
Signature · 1 line
2.20.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Christoph Hellwig

When removing some dead big endian checks in the RISC-V code Nick suggested that we should have some generic sanity checks. I don't think we should have thos inside the RISC-V code, but maybe it might make sense to have these in the generic byteorder headers. Note that these are UAPI headers and some compilers might not actually define __BYTE_ORDER__, so we first check that it actually exists.

Extra checking like this is good in general, but I'm not sure I see exactly what kind of issue one might expect to prevent with this:

All architecture asm/byteorder.h headers either include the only possible option, or they check the compiler defined macros:

arch/arc/include/uapi/asm/byteorder.h:#ifdef __BIG_ENDIAN__ arch/arm/include/uapi/asm/byteorder.h:#ifdef __ARMEB__ arch/arm64/include/uapi/asm/byteorder.h:#ifdef __AARCH64EB__ arch/c6x/include/uapi/asm/byteorder.h:#ifdef _BIG_ENDIAN arch/microblaze/include/uapi/asm/byteorder.h:#ifdef __MICROBLAZEEL__ arch/mips/include/uapi/asm/byteorder.h:#if defined(__MIPSEB__) arch/nds32/include/uapi/asm/byteorder.h:#ifdef __NDS32_EB__ arch/powerpc/include/uapi/asm/byteorder.h:#ifdef __LITTLE_ENDIAN__ arch/sh/include/uapi/asm/byteorder.h:#ifdef __LITTLE_ENDIAN__ arch/xtensa/include/uapi/asm/byteorder.h:#ifdef __XTENSA_EL__

Are you worried about toolchains that define those differently from what these headers expect? Did you encounter such a case?

Arnd

#2 lore raw
Quoted 9 lines · Arnd Bergmann
Quoted 8 lines · Christoph Hellwig

When removing some dead big endian checks in the RISC-V code Nick suggested that we should have some generic sanity checks. I don't think we should have thos inside the RISC-V code, but maybe it might make sense to have these in the generic byteorder headers. Note that these are UAPI headers and some compilers might not actually define __BYTE_ORDER__, so we first check that it actually exists.

Quoted 7 lines
Quoted 2 lines

Extra checking like this is good in general, but I'm not sure I see exactly what kind of issue one might expect to prevent with this:

I'm personally not worried at all. Just trying to respond to Nicks review comment and make it reasonable generic if we have to have these checks at all. I personally would be ok without them, I just don't want them hidden somewhere in the RISC-V code (RISC-V is always little endian at least right now).

maintainer
#3 lore raw
Quoted 16 lines · Christoph Hellwig
Quoted 14 lines · Arnd Bergmann
Quoted 10 lines · Christoph Hellwig

When removing some dead big endian checks in the RISC-V code Nick suggested that we should have some generic sanity checks. I don't think we should have thos inside the RISC-V code, but maybe it might make sense to have these in the generic byteorder headers. Note that these are UAPI headers and some compilers might not actually define __BYTE_ORDER__, so we first check that it actually exists.

Quoted 8 lines
Quoted 2 lines

Extra checking like this is good in general, but I'm not sure I see exactly what kind of issue one might expect to prevent with this:

I'm personally not worried at all. Just trying to respond to Nicks review comment and make it reasonable generic if we have to have these checks at all. I personally would be ok without them, I just don't want them hidden somewhere in the RISC-V code (RISC-V is always little endian at least right now).

Ok, makes sense.

Note: I hope we won't ever see big-endian RISC-V, I think that ship has sailed long ago, regardless of any personal preferences one might hold.

The architectures that allow both (arm, arc, mips, c6x, microblaze, modern ppc64, superh) tend to just use little-endian in practice, and the ones that are hardcoded to big-endian (sparc, parisc, m68k, h8300, s390, ppc32, some mips) are all 25+ years old and do so only for historic reasons, with openrisc being the notable exception.

Arnd

#4 lore raw
In reply to Arnd Bergmann

Στις 2019-04-12 17:53, Arnd Bergmann έγραψε:

Quoted 31 lines · Christoph Hellwig
Quoted 10 lines

When removing some dead big endian checks in the RISC-V code Nick suggested that we should have some generic sanity checks. I don't think we should have thos inside the RISC-V code, but maybe it might make sense to have these in the generic byteorder headers. Note that these are UAPI headers and some compilers might not actually define __BYTE_ORDER__, so we first check that it actually exists.

Extra checking like this is good in general, but I'm not sure I see exactly what kind of issue one might expect to prevent with this:

All architecture asm/byteorder.h headers either include the only possible option, or they check the compiler defined macros:

arch/arc/include/uapi/asm/byteorder.h:#ifdef __BIG_ENDIAN__ arch/arm/include/uapi/asm/byteorder.h:#ifdef __ARMEB__ arch/arm64/include/uapi/asm/byteorder.h:#ifdef __AARCH64EB__ arch/c6x/include/uapi/asm/byteorder.h:#ifdef _BIG_ENDIAN arch/microblaze/include/uapi/asm/byteorder.h:#ifdef __MICROBLAZEEL__ arch/mips/include/uapi/asm/byteorder.h:#if defined(__MIPSEB__) arch/nds32/include/uapi/asm/byteorder.h:#ifdef __NDS32_EB__ arch/powerpc/include/uapi/asm/byteorder.h:#ifdef __LITTLE_ENDIAN__ arch/sh/include/uapi/asm/byteorder.h:#ifdef __LITTLE_ENDIAN__ arch/xtensa/include/uapi/asm/byteorder.h:#ifdef __XTENSA_EL__

Quoted 4 lines

Are you worried about toolchains that define those differently from what these headers expect? Did you encounter such a case?

Arnd

The following architectures just include the header file without checking for any compiler macro:

alpha: little_endian.h
csky: little_endian.h
h8300: big_endian.h
hexagon: little_endian.h
ia64: little_endian.h
m68k: big_endian.h
nios2: little_endian.h
openrisc: big_endian.h
parisc: big_endian.h
riscv: little_endian.h
s390: big_endian.h
sparc: big_endian.h
unicore32: little_endian.h
x86: little_endian.h

Of those who do check for a compiler macro, they don't use the generic macros (__ORDER_*_ENDIAN__) but arch-specific ones.

Only two architectures (mips and xtensa) that support both big and little endian return an error in case the endianess can't be determined, the rest will move on without including any of *_endian.h files.

I think it's good to have a sanity check in-place for consistency.

Regards,
Nick

#5 lore raw

From: Nick Kossifidis <[email protected]>
Date: Fri, Apr 12, 2019 at 6:08 PM
To: Arnd Bergmann
Cc: Christoph Hellwig, Linus Torvalds, Andrew Morton, linux-arch, <[email protected]>, Linux Kernel Mailing List

Quoted 59 lines

Στις 2019-04-12 17:53, Arnd Bergmann έγραψε:

Quoted 32 lines · Christoph Hellwig
Quoted 11 lines

When removing some dead big endian checks in the RISC-V code Nick suggested that we should have some generic sanity checks. I don't think
we should have thos inside the RISC-V code, but maybe it might make sense to have these in the generic byteorder headers. Note that these are UAPI headers and some compilers might not actually define __BYTE_ORDER__, so we first check that it actually exists.

Extra checking like this is good in general, but I'm not sure I see exactly what kind of issue one might expect to prevent with this:

All architecture asm/byteorder.h headers either include the only possible option, or they check the compiler defined macros:

arch/arc/include/uapi/asm/byteorder.h:#ifdef __BIG_ENDIAN__ arch/arm/include/uapi/asm/byteorder.h:#ifdef __ARMEB__ arch/arm64/include/uapi/asm/byteorder.h:#ifdef __AARCH64EB__ arch/c6x/include/uapi/asm/byteorder.h:#ifdef _BIG_ENDIAN arch/microblaze/include/uapi/asm/byteorder.h:#ifdef __MICROBLAZEEL__ arch/mips/include/uapi/asm/byteorder.h:#if defined(__MIPSEB__) arch/nds32/include/uapi/asm/byteorder.h:#ifdef __NDS32_EB__ arch/powerpc/include/uapi/asm/byteorder.h:#ifdef __LITTLE_ENDIAN__ arch/sh/include/uapi/asm/byteorder.h:#ifdef __LITTLE_ENDIAN__ arch/xtensa/include/uapi/asm/byteorder.h:#ifdef __XTENSA_EL__

Quoted 4 lines

Are you worried about toolchains that define those differently from what these headers expect? Did you encounter such a case?

Arnd

The following architectures just include the header file without checking for any compiler macro:

alpha: little_endian.h
csky: little_endian.h
h8300: big_endian.h
hexagon: little_endian.h
ia64: little_endian.h
m68k: big_endian.h
nios2: little_endian.h
openrisc: big_endian.h
parisc: big_endian.h
riscv: little_endian.h
s390: big_endian.h
sparc: big_endian.h
unicore32: little_endian.h
x86: little_endian.h

Of those who do check for a compiler macro, they don't use the generic macros (__ORDER_*_ENDIAN__) but arch-specific ones.

Quoted 6 lines

Only two architectures (mips and xtensa) that support both big and little endian return an error in case the endianess can't be determined, the rest will move on without including any of *_endian.h files.

I think it's good to have a sanity check in-place for consistency.

Hi,

This broke our cross-builds from x86. I am using:

$ powerpc64le-linux-gnu-gcc --version
powerpc64le-linux-gnu-gcc (Debian 7.2.0-7) 7.2.0

and it says that it's little-endian somehow:

$ powerpc64le-linux-gnu-gcc -dM -E - < /dev/null | grep BYTE_ORDER
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__

Is it broke compiler? Or I always hold it wrong? Is there some additional flag I need to add?

Thanks

maintainer
#6 lore raw
Quoted 11 lines · Dmitry Vyukov
Quoted 2 lines

I think it's good to have a sanity check in-place for consistency.

Hi,

This broke our cross-builds from x86. I am using:

$ powerpc64le-linux-gnu-gcc --version
powerpc64le-linux-gnu-gcc (Debian 7.2.0-7) 7.2.0
Quoted 7 lines

and it says that it's little-endian somehow:

$ powerpc64le-linux-gnu-gcc -dM -E - < /dev/null | grep BYTE_ORDER
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__

Is it broke compiler? Or I always hold it wrong? Is there some additional flag I need to add?

It looks like a bug in the kernel Makefiles to me. powerpc32 is always big-endian,
powerpc64 used to be big-endian but is now usually little-endian. There are often three separate toolchains that default to the respective user space targets
(ppc32be, ppc64be, ppc64le), but generally you should be able to build any of the
three kernel configurations with any of those compilers, and have the Makefile pass the correct -m32/-m64/-mbig-endian/-mlittle-endian command line options depending on the kernel configuration. It seems that this is not happening here. I have not checked why, but if this is the problem, it should be easy enough
to figure out.

Arnd

#7 lore raw

From: Arnd Bergmann <[email protected]>
Date: Sat, May 11, 2019 at 2:51 AM
To: Dmitry Vyukov
Cc: Nick Kossifidis, Christoph Hellwig, Linus Torvalds, Andrew Morton, linux-arch, Linux Kernel Mailing List, linuxppc-dev

Quoted 20 lines · Dmitry Vyukov
Quoted 11 lines
Quoted 2 lines

I think it's good to have a sanity check in-place for consistency.

Hi,

This broke our cross-builds from x86. I am using:

$ powerpc64le-linux-gnu-gcc --version
powerpc64le-linux-gnu-gcc (Debian 7.2.0-7) 7.2.0
Quoted 7 lines

and it says that it's little-endian somehow:

$ powerpc64le-linux-gnu-gcc -dM -E - < /dev/null | grep BYTE_ORDER
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__

Is it broke compiler? Or I always hold it wrong? Is there some additional flag I need to add?

Quoted 13 lines

It looks like a bug in the kernel Makefiles to me. powerpc32 is always big-endian,
powerpc64 used to be big-endian but is now usually little-endian. There are often three separate toolchains that default to the respective user space targets
(ppc32be, ppc64be, ppc64le), but generally you should be able to build any of the
three kernel configurations with any of those compilers, and have the Makefile pass the correct -m32/-m64/-mbig-endian/-mlittle-endian command line options depending on the kernel configuration. It seems that this is not happening here. I have not checked why, but if this is the problem, it should be easy enough
to figure out.

Thanks! This clears a lot.
This may be a bug in our magic as we try to build kernel files outside of make with own flags (required to extract parts of kernel interfaces).
So don't spend time looking for the Makefile bugs yet.

#8 lore raw
Quoted 41 lines · Dmitry Vyukov

From: Arnd Bergmann <[email protected]>
Date: Sat, May 11, 2019 at 2:51 AM
To: Dmitry Vyukov
Cc: Nick Kossifidis, Christoph Hellwig, Linus Torvalds, Andrew Morton, linux-arch, Linux Kernel Mailing List, linuxppc-dev

Quoted 20 lines · Dmitry Vyukov
Quoted 11 lines
Quoted 2 lines

I think it's good to have a sanity check in-place for consistency.

Hi,

This broke our cross-builds from x86. I am using:

$ powerpc64le-linux-gnu-gcc --version
powerpc64le-linux-gnu-gcc (Debian 7.2.0-7) 7.2.0
Quoted 7 lines

and it says that it's little-endian somehow:

$ powerpc64le-linux-gnu-gcc -dM -E - < /dev/null | grep BYTE_ORDER
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__

Is it broke compiler? Or I always hold it wrong? Is there some additional flag I need to add?

Quoted 13 lines

It looks like a bug in the kernel Makefiles to me. powerpc32 is always big-endian,
powerpc64 used to be big-endian but is now usually little-endian. There are often three separate toolchains that default to the respective user space targets
(ppc32be, ppc64be, ppc64le), but generally you should be able to build any of the
three kernel configurations with any of those compilers, and have the Makefile pass the correct -m32/-m64/-mbig-endian/-mlittle-endian command line options depending on the kernel configuration. It seems that this is not happening here. I have not checked why, but if this is the problem, it should be easy enough
to figure out.

Quoted 5 lines

Thanks! This clears a lot.
This may be a bug in our magic as we try to build kernel files outside of make with own flags (required to extract parts of kernel interfaces).
So don't spend time looking for the Makefile bugs yet.

OK :)

We did have some bugs in the past (~1-2 y/ago) but AFAIK they are all fixed now. These days I build most of my kernels with a bi-endian 64-bit toolchain, and switching endian without running make clean also works.

cheers

#9 lore raw

From: Michael Ellerman <[email protected]>
Date: Mon, May 13, 2019 at 1:33 PM
To: Dmitry Vyukov, Arnd Bergmann
Cc: Nick Kossifidis, Christoph Hellwig, Linus Torvalds, Andrew Morton, linux-arch, Linux Kernel Mailing List, linuxppc-dev

Quoted 48 lines
Quoted 41 lines · Dmitry Vyukov

From: Arnd Bergmann <[email protected]>
Date: Sat, May 11, 2019 at 2:51 AM
To: Dmitry Vyukov
Cc: Nick Kossifidis, Christoph Hellwig, Linus Torvalds, Andrew Morton, linux-arch, Linux Kernel Mailing List, linuxppc-dev

Quoted 20 lines · Dmitry Vyukov
Quoted 11 lines
Quoted 2 lines

I think it's good to have a sanity check in-place for consistency.

Hi,

This broke our cross-builds from x86. I am using:

$ powerpc64le-linux-gnu-gcc --version
powerpc64le-linux-gnu-gcc (Debian 7.2.0-7) 7.2.0
Quoted 7 lines

and it says that it's little-endian somehow:

$ powerpc64le-linux-gnu-gcc -dM -E - < /dev/null | grep BYTE_ORDER
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__

Is it broke compiler? Or I always hold it wrong? Is there some additional flag I need to add?

Quoted 13 lines

It looks like a bug in the kernel Makefiles to me. powerpc32 is always big-endian,
powerpc64 used to be big-endian but is now usually little-endian. There are often three separate toolchains that default to the respective user space targets
(ppc32be, ppc64be, ppc64le), but generally you should be able to build any of the
three kernel configurations with any of those compilers, and have the Makefile pass the correct -m32/-m64/-mbig-endian/-mlittle-endian command line options depending on the kernel configuration. It seems that this is not happening here. I have not checked why, but if this is the problem, it should be easy enough
to figure out.

Quoted 5 lines

Thanks! This clears a lot.
This may be a bug in our magic as we try to build kernel files outside of make with own flags (required to extract parts of kernel interfaces).
So don't spend time looking for the Makefile bugs yet.

Quoted 5 lines

OK :)

We did have some bugs in the past (~1-2 y/ago) but AFAIK they are all fixed now. These days I build most of my kernels with a bi-endian 64-bit toolchain, and switching endian without running make clean also works.

For the record, yes, it turn out to be a problem in our code (a latent bug). We actually used host (x86) gcc to build as-if ppc code that can run on the host, so it defined neither LE no BE macros. It just happened to work in the past :)
+Andrew

#10 lore raw
Quoted 8 lines · Dmitry Vyukov
Quoted 3 lines

We did have some bugs in the past (~1-2 y/ago) but AFAIK they are all fixed now. These days I build most of my kernels with a bi-endian 64-bit toolchain, and switching endian without running make clean also works.

For the record, yes, it turn out to be a problem in our code (a latent bug). We actually used host (x86) gcc to build as-if ppc code that can run on the host, so it defined neither LE no BE macros. It just happened to work in the past :)

So Nick was right and these checks actually are useful..

maintainer
#11 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Dmitry Vyukov
Quoted 3 lines

We did have some bugs in the past (~1-2 y/ago) but AFAIK they are all fixed now. These days I build most of my kernels with a bi-endian 64-bit toolchain, and switching endian without running make clean also works.

Quoted 6 lines
Quoted 4 lines

For the record, yes, it turn out to be a problem in our code (a latent bug). We actually used host (x86) gcc to build as-if ppc code that can run on the host, so it defined neither LE no BE macros. It just happened to work in the past :)

So Nick was right and these checks actually are useful..

Yes, definitely. I wonder if we should also bring back the word size check from include/asm-generic/bitsperlong.h, which was disabled right after I originally added that.

Arnd

#12 lore raw
In reply to Christoph Hellwig

include/uapi/linux/byteorder/big_endian.h line 6

Quoted 10 lines · Christoph Hellwig
include/uapi/linux/byteorder/big_endian.h +2 -0
index 2199adc6a6c2..34a5864526d2 100644
@@ -2,6 +2,10 @@
 #ifndef _UAPI_LINUX_BYTEORDER_BIG_ENDIAN_H
 #define _UAPI_LINUX_BYTEORDER_BIG_ENDIAN_H
 
+#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__
+#error "Unsupported endianess, check your toolchain"

Typo here: s/endianess/endianness/.

include/uapi/linux/byteorder/little_endian.h line 6

Quoted 10 lines
include/uapi/linux/byteorder/little_endian.h +2 -0
index 601c904fd5cd..0cdf3583e19f 100644
@@ -2,6 +2,10 @@
 #ifndef _UAPI_LINUX_BYTEORDER_LITTLE_ENDIAN_H
 #define _UAPI_LINUX_BYTEORDER_LITTLE_ENDIAN_H
 
+#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ !=  __ORDER_LITTLE_ENDIAN__
+#error "Unsupported endianess, check your toolchain"

Likewise.

Maciej

#13 lore raw
Quoted 4 lines · Maciej Rozycki

include/uapi/linux/byteorder/big_endian.h line 6

Quoted 2 lines
include/uapi/linux/byteorder/big_endian.h +2 -0
+#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__
+#error "Unsupported endianess, check your toolchain"

Typo here: s/endianess/endianness/.

The original patch has already been merged, please send a fixup.