kernel: add common infrastructure for unaligned access
PATCH 1/8 kernel: add common infrastructure for unaligned access
Create a linux/unaligned folder similar in spirit to the linux/byteorder folder to hold generic implementations collected from various arches.
Currently there are five implementations:
1) cpu_endian.h: C-struct based, heavily based on asm-generic/unaligned.h
2) little_endian.h: Open coded byte-swapping, heavily based on asm-arm
3) big_endian.h: Open coded byte-swapping, heavily based on asm-arm
4) no_builtin_memcpy.h: taken from multiple implementations in tree
5) access_ok.h: taken from x86 and others, unaligned access is ok.
API additions:
get_unaligned_{le16|le32|le64|be16|be32|be64}(p) which is meant to replace code of the form:le16_to_cpu(get_unaligned((__le16 *)p));
put_unaligned_{le16|le32|le64|be16|be32|be64}(val, pointer) which is meant to replace code of the form:put_unaligned(cpu_to_le16(val), (__le16 *)p);
Headers to create these based on the selected implementation and defining the appropriate get_unaligned() and put_unaligned() macros are:
generic_le.h: Use the C-struct for get/put_unaligned and the le helpers, use the opencoded be byteswapping implementation for big-endian.
generic_be.h: Use the C-struct for get/put_unaligned and the be helpers, use the opencoded le byteswapping implementation for little-endian.
generic.h: Use opencoded byteswapping for all helpers, leaves it to the arch to define get/put_unaligned.
--- include/linux/unaligned/access_ok.h | 70 +++++++++++++++++++++ include/linux/unaligned/big_endian.h | 84 +++++++++++++++++++++++++ include/linux/unaligned/cpu_endian.h | 90 +++++++++++++++++++++++++++ include/linux/unaligned/generic.h | 67 ++++++++++++++++++++ include/linux/unaligned/generic_be.h | 70 +++++++++++++++++++++ include/linux/unaligned/generic_le.h | 70 +++++++++++++++++++++ include/linux/unaligned/little_endian.h | 84 +++++++++++++++++++++++++ include/linux/unaligned/no_builtin_memcpy.h | 80 ++++++++++++++++++++++++ 8 files changed, 615 insertions(+), 0 deletions(-)
include/linux/unaligned/access_ok.h new file +70 -0
@@ -0,0 +1,70 @@ +#ifndef _LINUX_UNALIGNED_ACCESS_OK_H_ +#define _LINUX_UNALIGNED_ACCESS_OK_H_ + +#include <linux/kernel.h> +#include <asm/byteorder.h> + +#define get_unaligned (ptr) (*(ptr)) +#define put_unaligned (val, ptr) ((void)( *(ptr) = (val) )) + +static inline u16 get_unaligned_le16 (const __le16 *p) +{ +return le16_to_cpup (p); +} + +static inline u32 get_unaligned_le32 (const __le32 *p) +{ +return le32_to_cpup (p); +} + +static inline u64 get_unaligned_le64 (const __le64 *p) +{ +return le64_to_cpup (p); +} + +static inline u16 get_unaligned_be16 (const __be16 *p) +{ +return be16_to_cpup (p); +} + +static inline u32 get_unaligned_be32 (const __be32 *p) +{ +return be32_to_cpup (p); +} + +static inline u64 get_unaligned_be64 (const __be64 *p) +{ +return be64_to_cpup (p); +} + +static inline void put_unaligned_le16 (u16 val,void *p) +{ + *((__le16 *)p) =cpu_to_le16 (val); +} + +static inline void put_unaligned_le32 (u32 val,void *p) +{ + *((__le32 *)p) =cpu_to_le32 (val); +} + +static inline void put_unaligned_le64 (u64 val,void *p) +{ + *((__le64 *)p) =cpu_to_le64 (val); +} + +static inline void put_unaligned_be16 (u16 val,void *p) +{ + *((__be16 *)p) =cpu_to_be16 (val); +} + +static inline void put_unaligned_be32 (u32 val,void *p) +{ + *((__be32 *)p) =cpu_to_be32 (val); +} + +static inline void put_unaligned_be64 (u64 val,void *p) +{ + *((__be64 *)p) =cpu_to_be64 (val); +} + +#endif /* _LINUX_UNALIGNED_ACCESS_OK_H_ */
include/linux/unaligned/big_endian.h new file +84 -0
84 lines not on this page · raw message
include/linux/unaligned/cpu_endian.h new file +90 -0
90 lines not on this page · raw message
include/linux/unaligned/generic.h new file +67 -0
67 lines not on this page · raw message
include/linux/unaligned/generic_be.h new file +70 -0
70 lines not on this page · raw message
include/linux/unaligned/generic_le.h new file +70 -0
70 lines not on this page · raw message
include/linux/unaligned/little_endian.h new file +84 -0
84 lines not on this page · raw message
include/linux/unaligned/no_builtin_memcpy.h new file +80 -0
80 lines not on this page · raw message
Signature · 1 line
1.5.5.144.g3e42
include/linux/unaligned/little_endian.h line 9
Quoted 4 lines · Harvey Harrison
include/linux/unaligned/little_endian.h new file +4
-0+static inline u16 __get_unaligned_le16 (const u8 *p) +{ +return (u16)(p[0 ] | p[1 ] <<8 ); +}
You shouldn't need these casts. return is going to cast it anyway.
Actually, you probably _ought_ to have casts, but it should look like this:
return (u16)p[0] | (u16)p[1] << 8;
You are shifting an 8-bit value left by 8 bits, so the compiler may be at liberty to instruct the RHS to end up zero.
I presume the compiler is guaranteed not to merge the two memory accesses? It can't seem to make it do so, though I seem to remember there were cases where it did, though I can't reproduce them. I assume that's why you're passing in a u8 pointer and not a u16/u32/u64 pointer.
David
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 9 lines · David Howells
include/linux/unaligned/little_endian.h line 9
Quoted 4 lines · Harvey Harrison
include/linux/unaligned/little_endian.h new file +4
-0+static inline u16 __get_unaligned_le16 (const u8 *p) +{ +return (u16)(p[0 ] | p[1 ] <<8 ); +}You shouldn't need these casts. return is going to cast it anyway.
Quoted 3 lines
Actually, you probably _ought_ to have casts, but it should look like this:
return (u16)p[0] | (u16)p[1] << 8;
I've been looking at that thinking I needed something different, I believe it is ok as u8 will expand to int when shifted... correct? Or do I actually need the cast on each p[] term...anyone?
Quoted 8 lines
You are shifting an 8-bit value left by 8 bits, so the compiler may be at liberty to instruct the RHS to end up zero.
I presume the compiler is guaranteed not to merge the two memory accesses? It can't seem to make it do so, though I seem to remember there were cases where it did, though I can't reproduce them. I assume that's why you're passing in a u8 pointer and not a u16/u32/u64 pointer.
Yes, that is the reason. The implementation is nearly identical to the existing arm version in-tree (minus the register keywords of course).
Harvey
Quoted 7 lines · Harvey Harrison
Quoted 3 lines
Actually, you probably _ought_ to have casts, but it should look like this:
return (u16)p[0] | (u16)p[1] << 8;I've been looking at that thinking I needed something different, I believe it is ok as u8 will expand to int when shifted... correct? Or do I actually need the cast on each p[] term...anyone?
Hmmm... I think you may be right:
#include <stdio.h>
int main()
{
unsigned char x;
printf("%u, %u\n", sizeof(x), sizeof(x << 8));
return 0;
}Says:
1, 4
In which case, the cast you do have is superfluous, and casting the retrievals is unnecessary.
David
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 21 lines · David Howells
Quoted 7 lines · Harvey Harrison
Quoted 3 lines
Actually, you probably _ought_ to have casts, but it should look like this:
return (u16)p[0] | (u16)p[1] << 8;I've been looking at that thinking I needed something different, I believe it is ok as u8 will expand to int when shifted... correct? Or do I actually need the cast on each p[] term...anyone?
Hmmm... I think you may be right:
#include <stdio.h> int main() { unsigned char x; printf("%u, %u\n", sizeof(x), sizeof(x << 8)); return 0; }
Quoted 6 lines
Says:
1, 4In which case, the cast you do have is superfluous, and casting the retrievals is unnecessary.
Expands to int, not unsigned int, I think that cast is still needed?
Harvey
Quoted 1 line · Harvey Harrison
Expands to int, not unsigned int, I think that cast is still needed?
What for? The return is going to do the same cast anyway because of the function return type.
David
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 8 lines · David Howells
Quoted 1 line · Harvey Harrison
Expands to int, not unsigned int, I think that cast is still needed?
What for? The return is going to do the same cast anyway because of the function return type.
David
Well, for the u16 case, won't the compiler warn about truncating the return if I return an int when the function returns u16?
In the u64 case, I need the cast to ensure it expands to u64 rather than int from the shift. Agreed in the u32 case, but then it looks different than the u16/u64 case (cargo-cult I know, but nice for consistency).
Harvey
Quoted 2 lines · Harvey Harrison
Well, for the u16 case, won't the compiler warn about truncating the return if I return an int when the function returns u16?
It doesn't for me for:
u16 test(const u8 *p)
{
return (u16) (p[0] | p[1] << 8);
}Quoted 3 lines
In the u64 case, I need the cast to ensure it expands to u64 rather than int from the shift. Agreed in the u32 case, but then it looks different than the u16/u64 case (cargo-cult I know, but nice for consistency).
Agreed, the u64 cast is necessary, but I was talking about casts generally of:
<TYPE> function(...)
{
return (<TYPE>) (...);
}David
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 16 lines · David Howells
Quoted 2 lines · Harvey Harrison
Well, for the u16 case, won't the compiler warn about truncating the return if I return an int when the function returns u16?
It doesn't for me for:
u16 test(const u8 *p) { return (u16) (p[0] | p[1] << 8); }Quoted 3 lines
In the u64 case, I need the cast to ensure it expands to u64 rather than int from the shift. Agreed in the u32 case, but then it looks different than the u16/u64 case (cargo-cult I know, but nice for consistency).
Quoted 7 lines
Agreed, the u64 cast is necessary, but I was talking about casts generally of:
<TYPE> function(...) { return (<TYPE>) (...); }
OK, I'll see what other comments come in and keep it in mind for the next iteration. I'll also move frv over to the generic C code in that submission as we discussed.
Harvey
Small update changing the exposed API of get_unaligned_* take a void * to avoid casts in every single caller. We are already specifying what we want to get explicitly in the function name:
get_unaligned_le16...le32...le64..etc.
This will make 8/8 look even cleaner as a bunch of casts are not needed.
Harvey
include/linux/unaligned/access_ok.h new file +70 -0
@@ -0,0 +1,70 @@ +#ifndef _LINUX_UNALIGNED_ACCESS_OK_H_ +#define _LINUX_UNALIGNED_ACCESS_OK_H_ + +#include <linux/kernel.h> +#include <asm/byteorder.h> + +#define get_unaligned (ptr) (*(ptr)) +#define put_unaligned (val, ptr) ((void)( *(ptr) = (val) )) + +static inline u16 get_unaligned_le16 (const void *p) +{ +return le16_to_cpup ((__le16 *)p); +} + +static inline u32 get_unaligned_le32 (const void *p) +{ +return le32_to_cpup ((__le32 *)p); +} + +static inline u64 get_unaligned_le64 (const void *p) +{ +return le64_to_cpup ((__le64 *)p); +} + +static inline u16 get_unaligned_be16 (const void *p) +{ +return be16_to_cpup ((__be16 *)p); +} + +static inline u32 get_unaligned_be32 (const void *p) +{ +return be32_to_cpup ((__be32 *)p); +} + +static inline u64 get_unaligned_be64 (const void *p) +{ +return be64_to_cpup ((__be64 *)p); +} + +static inline void put_unaligned_le16 (u16 val,void *p) +{ + *((__le16 *)p) =cpu_to_le16 (val); +} + +static inline void put_unaligned_le32 (u32 val,void *p) +{ + *((__le32 *)p) =cpu_to_le32 (val); +} + +static inline void put_unaligned_le64 (u64 val,void *p) +{ + *((__le64 *)p) =cpu_to_le64 (val); +} + +static inline void put_unaligned_be16 (u16 val,void *p) +{ + *((__be16 *)p) =cpu_to_be16 (val); +} + +static inline void put_unaligned_be32 (u32 val,void *p) +{ + *((__be32 *)p) =cpu_to_be32 (val); +} + +static inline void put_unaligned_be64 (u64 val,void *p) +{ + *((__be64 *)p) =cpu_to_be64 (val); +} + +#endif /* _LINUX_UNALIGNED_ACCESS_OK_H_ */
include/linux/unaligned/big_endian.h new file +84 -0
84 lines not on this page · raw message
include/linux/unaligned/cpu_endian.h new file +90 -0
90 lines not on this page · raw message
include/linux/unaligned/generic.h new file +67 -0
67 lines not on this page · raw message
include/linux/unaligned/generic_be.h new file +70 -0
70 lines not on this page · raw message
include/linux/unaligned/generic_le.h new file +70 -0
70 lines not on this page · raw message
include/linux/unaligned/little_endian.h new file +84 -0
84 lines not on this page · raw message
include/linux/unaligned/no_builtin_memcpy.h new file +80 -0
80 lines not on this page · raw message
Quoted 11 lines · Harvey Harrison
include/linux/unaligned/cpu_endian.h new file +11
-0+#define __get_unaligned_cpu (ptr) ({ \ + const void *__gu_p = (ptr); \ + typeof(*(ptr)) __val; \ + switch (sizeof(*(ptr))) { \ + case 1: \ + __val = *(const u8 *)__gu_p; \ + break; \ + case 2: \ + __val = __get_unaligned_cpu16(__gu_p); \ + break; \ + case 4: \
include/linux/unaligned/cpu_endian.h line 48
Quoted 10 lines
include/linux/unaligned/cpu_endian.h new file +10
-0+ __val =__get_unaligned_cpu32 (__gu_p); \ +break ; \ +case 8 : \ + __val =__get_unaligned_cpu64 (__gu_p); \ +break ; \ +default : \ +BUILD_BUG_ON (1 ); \ +break ; \ + }; \ + __val; })
This won't work - on ARM we used to use this style, but it fails in some corner case, so we ended up switching to using GCC's __builtin_choose_expr() instead.
Such a corner case:
static unsigned long foo(const unsigned long *ptr)
{
return __get_unaligned_cpu(ptr);
}This results in '__val' being declared as const, and therefore the compiler errors out in the switch statement since the code tries to assign to a const '__val'.
See 17b602b1c1a38f3f0a4461bb1f571346e751b36b.
So, for the present set of patches, NAK for changing ARM.
Signature · 3 lines
Russell King Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/ maintainer of:
A message that is not in the archive (sent off-list or not yet received): 20080411180928.GA9137-f404yB8NqCZvn6HldHNs0ANdhmdF6hFW@public.gmane.org
Quoted 29 lines · Russell King
Quoted 11 lines · Harvey Harrison
include/linux/unaligned/cpu_endian.h new file +11
-0+#define __get_unaligned_cpu (ptr) ({ \ + const void *__gu_p = (ptr); \ + typeof(*(ptr)) __val; \ + switch (sizeof(*(ptr))) { \ + case 1: \ + __val = *(const u8 *)__gu_p; \ + break; \ + case 2: \ + __val = __get_unaligned_cpu16(__gu_p); \ + break; \ + case 4: \include/linux/unaligned/cpu_endian.h line 48
Quoted 10 lines
include/linux/unaligned/cpu_endian.h new file +10
-0+ __val =__get_unaligned_cpu32 (__gu_p); \ +break ; \ +case 8 : \ + __val =__get_unaligned_cpu64 (__gu_p); \ +break ; \ +default : \ +BUILD_BUG_ON (1 ); \ +break ; \ + }; \ + __val; })This won't work - on ARM we used to use this style, but it fails in some corner case, so we ended up switching to using GCC's
__builtin_choose_expr()instead.Such a corner case:
Quoted 8 lines
static unsigned long
foo(const unsigned long *ptr){ return __get_unaligned_cpu(ptr); }This results in '
__val' being declared as const, and therefore the compiler errors out in the switch statement since the code tries to assign to a const '__val'.
OK, will respin the set, I think I have a better way of doing it anyway.
Not that it matters, but wouldn't that be a gcc bug, for it to preserve const on __val, wouldn't that have to be:
const unsigned long * const ptr
Just curious.
Thanks.
Harvey