xen: have a more generic unaligned.h header
v2 cover xen: have a more generic unaligned.h header
Update Xen's unaligned.h header to support all architectures, allowing to remove the architecture specific variants (x86 only until now).
Changes in V2:
- new patch 1 (Julien Grall)
- adjusted patch 2 (Jan Beulich)
Juergen Gross (3):
xen/arm: set -mno-unaligned-access compiler option for Arm32
xen: make include/xen/unaligned.h usable on all architectures
xen: remove asm/unaligned.h
xen/arch/arm/arch.mk | 1 + xen/arch/x86/include/asm/unaligned.h | 6 --- xen/common/lz4/defs.h | 2 +- xen/common/lzo.c | 2 +- xen/common/unlzo.c | 2 +- xen/common/xz/private.h | 2 +- xen/common/zstd/mem.h | 2 +- xen/include/xen/unaligned.h | 77 +++++++++++++++------------- xen/lib/xxhash32.c | 2 +- xen/lib/xxhash64.c | 2 +- 10 files changed, 50 insertions(+), 48 deletions(-) delete mode 100644 xen/arch/x86/include/asm/unaligned.h
Signature · 1 line
2.35.3
PATCH v2 1/3 xen/arm: set -mno-unaligned-access compiler option for Arm32
As the hypervisor is disabling unaligned accesses for Arm32, set the -mno-unaligned-access compiler option for building. This will prohibit unaligned accesses when e.g. accessing 2- or 4-byte data items in packed data structures.
---
V2:
- new patch
--- xen/arch/arm/arch.mk | 1 + 1 file changed, 1 insertion(+)
xen/arch/arm/arch.mk +1 -0
@@ -7,6 +7,7 @@ $(call cc-option-add,CFLAGS,CC,-Wnested-externs) # Prevent floating-point variables from creeping into Xen. CFLAGS-$(CONFIG_ARM_32) += -msoft-float CFLAGS-$(CONFIG_ARM_32) += -mcpu=cortex-a15 +CFLAGS-$(CONFIG_ARM_32) += -mno-unaligned-access CFLAGS-$(CONFIG_ARM_64) += -mcpu=generic CFLAGS-$(CONFIG_ARM_64) += -mgeneral-regs-only # No fp registers etc
Signature · 1 line
2.35.3
Re: [PATCH v2 1/3] xen/arm: set -mno-unaligned-access compiler option for Arm32
Quoted 6 lines · Juergen Gross
As the hypervisor is disabling unaligned accesses for Arm32, set the -mno-unaligned-access compiler option for building. This will prohibit unaligned accesses when e.g. accessing 2- or 4-byte data items in packed data structures.
Signed-off-by Juergen Gross <[email protected]>
Assuming this will want backporting, should it have some Fixes: tag?
Jan
Re: [PATCH v2 1/3] xen/arm: set -mno-unaligned-access compiler option for Arm32
Quoted 9 lines · Jan Beulich
Quoted 6 lines · Juergen Gross
As the hypervisor is disabling unaligned accesses for Arm32, set the -mno-unaligned-access compiler option for building. This will prohibit unaligned accesses when e.g. accessing 2- or 4-byte data items in packed data structures.
Signed-off-by Juergen Gross <[email protected]>Assuming this will want backporting, should it have some Fixes: tag?
IMHO this might not be needed, but the Arm maintainers should have the final say.
This option is especially important after the whole series has been applied, as get_unaligned() and put_unaligned() can then be used in Arm32 code, too. And without the added option this could cause crashes.
Juergen
Re: [PATCH v2 1/3] xen/arm: set -mno-unaligned-access compiler option for Arm32
Hi Juergen,
Quoted 7 lines · Juergen Gross
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Juergen Gross
As the hypervisor is disabling unaligned accesses for Arm32, set the -mno-unaligned-access compiler option for building. This will prohibit unaligned accesses when e.g. accessing 2- or 4-byte data items in packed data structures.
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Juergen Gross <[email protected]>Assuming this will want backporting, should it have some Fixes: tag?
IMHO this might not be needed
You probably miss the discussion between Arnd and I where I pointed out that this should fix [1]. I haven't yet tested to confirm.
[1] https://lore.kernel.org/xen-devel/[email protected]/
Quoted 3 lines
Juergen
Signature · 1 line
Julien Grall
Julien Grall
Acked-byRe: [PATCH v2 1/3] xen/arm: set -mno-unaligned-access compiler option for Arm32
Quoted 10 lines · Julien Grall
Hi Juergen,
Quoted 7 lines · Juergen Gross
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Juergen Gross
As the hypervisor is disabling unaligned accesses for Arm32, set the -mno-unaligned-access compiler option for building. This will prohibit unaligned accesses when e.g. accessing 2- or 4-byte data items in packed data structures.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Juergen Gross <[email protected]>Assuming this will want backporting, should it have some Fixes: tag?
IMHO this might not be needed
You probably miss the discussion between Arnd and I where I pointed out that this should fix [1]. I haven't yet tested to confirm.
I can't find the GCC binaries I was using anymore :(. But I still think it is a good idea to backport it.
@Stefano can you add it in your list?
Cheers,
Signature · 1 line
Julien Grall
PATCH v2 2/3 xen: make include/xen/unaligned.h usable on all architectures
In reply to Juergen GrossInstead of defining get_unaligned() and put_unaligned() in a way that is only supporting architectures allowing unaligned accesses, use the same approach as the Linux kernel and let the compiler do the decision how to generate the code for probably unaligned data accesses.
Update include/xen/unaligned.h from include/asm-generic/unaligned.h of the Linux kernel.
The generated code has been checked to be the same on x86.
Modify the Linux variant to not use underscore prefixed identifiers, avoid unneeded parentheses and drop the 24-bit accessors.
Origin: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 803f4e1eab7a
---
V2:
- drop 24-bit accessors (Jan Beulich)
- avoid underscore prefixed identifiers (Jan Beulich)
- drop unneeded parentheses (Jan Beulich)
--- xen/include/xen/unaligned.h | 77 ++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 35 deletions(-)
xen/include/xen/unaligned.h +42 -35
@@ -1,12 +1,4 @@ -/* -* This header can be used by architectures where unaligned accesses work -* without faulting, and at least reasonably efficiently. Other architectures -* will need to have a custom asm/unaligned.h. -*/ -#ifndef __ASM_UNALIGNED_H__ -#error "xen/unaligned.h should not be included directly - include asm/unaligned.h instead" -#endif - +/* SPDX-License-Identifier: GPL-2.0 */ #ifndef __XEN_UNALIGNED_H__#define __XEN_UNALIGNED_H__
@@ -15,67 +7,82 @@#include <asm/byteorder.h> #endif -#define get_unaligned (p) (*(p)) -#define put_unaligned (val, p) (*(p) = (val)) +/* +* This is the most generic implementation of unaligned accesses +* and should work almost anywhere. +*/ + +#define get_unaligned_t_ (type, ptr) ({ \ + const struct { type x; } __packed *ptr_ = (typeof(ptr_))(ptr); \ + ptr_->x; \ +}) + +#define put_unaligned_t_ (type, val, ptr) do { \ + struct { type x; } __packed *ptr_ = (typeof(ptr_))(ptr); \ + ptr_->x = val; \ +} while (0) + +#define get_unaligned (ptr) get_unaligned_t_(typeof(*(ptr)), ptr) +#define put_unaligned (val, ptr) put_unaligned_t_(typeof(*(ptr)), val, ptr) -static inline uint16_t get_unaligned_be16 (const void *p) +static inline u16 get_unaligned_le16 (const void *p) { -return be16_to_cpup (p); +return le16_to_cpu (get_unaligned_t_ (__le16, p)); } -static inline void put_unaligned_be16 (uint16_t val,void *p) +static inline u32 get_unaligned_le32 (const void *p) { - *(__force __be16 *)p =cpu_to_be16 (val); +return le32_to_cpu (get_unaligned_t_ (__le32, p)); } -static inline uint32_t get_unaligned_be32 (const void *p) +static inline u64 get_unaligned_le64 (const void *p) { -return be32_to_cpup (p); +return le64_to_cpu (get_unaligned_t_ (__le64, p)); } -static inline void put_unaligned_be32 (uint32_t val,void *p) +static inline void put_unaligned_le16 (u16 val,void *p) { - *(__force __be32 *)p =cpu_to_be32 (val); +put_unaligned_t_ (__le16,cpu_to_le16 (val), p); } -static inline uint64_t get_unaligned_be64 (const void *p) +static inline void put_unaligned_le32 (u32 val,void *p) { -return be64_to_cpup (p); +put_unaligned_t_ (__le32,cpu_to_le32 (val), p); } -static inline void put_unaligned_be64 (uint64_t val,void *p) +static inline void put_unaligned_le64 (u64 val,void *p) { - *(__force __be64 *)p =cpu_to_be64 (val); +put_unaligned_t_ (__le64,cpu_to_le64 (val), p); } -static inline uint16_t get_unaligned_le16 (const void *p) +static inline u16 get_unaligned_be16 (const void *p) { -return le16_to_cpup (p); +return be16_to_cpu (get_unaligned_t_ (__be16, p)); } -static inline void put_unaligned_le16 (uint16_t val,void *p) +static inline u32 get_unaligned_be32 (const void *p) { - *(__force __le16 *)p =cpu_to_le16 (val); +return be32_to_cpu (get_unaligned_t_ (__be32, p)); } -static inline uint32_t get_unaligned_le32 (const void *p) +static inline u64 get_unaligned_be64 (const void *p) { -return le32_to_cpup (p); +return be64_to_cpu (get_unaligned_t_ (__be64, p)); } -static inline void put_unaligned_le32 (uint32_t val,void *p) +static inline void put_unaligned_be16 (u16 val,void *p) { - *(__force __le32 *)p =cpu_to_le32 (val); +put_unaligned_t_ (__be16,cpu_to_be16 (val), p); } -static inline uint64_t get_unaligned_le64 (const void *p) +static inline void put_unaligned_be32 (u32 val,void *p) { -return le64_to_cpup (p); +put_unaligned_t_ (__be32,cpu_to_be32 (val), p); } -static inline void put_unaligned_le64 (uint64_t val,void *p) +static inline void put_unaligned_be64 (u64 val,void *p) { - *(__force __le64 *)p =cpu_to_le64 (val); +put_unaligned_t_ (__be64,cpu_to_be64 (val), p); }#endif /* __XEN_UNALIGNED_H__ */
Signature · 1 line
2.35.3
Re: [PATCH v2 2/3] xen: make include/xen/unaligned.h usable on all architectures
Quoted 8 lines · Juergen Gross
Instead of defining
get_unaligned()andput_unaligned()in a way that is only supporting architectures allowing unaligned accesses, use the same approach as the Linux kernel and let the compiler do the decision how to generate the code for probably unaligned data accesses.Update
include/xen/unaligned.hfrominclude/asm-generic/unaligned.hof the Linux kernel.
Quoted 8 lines
The generated code has been checked to be the same on x86.
Modify the Linux variant to not use underscore prefixed identifiers, avoid unneeded parentheses and drop the 24-bit accessors.
Signed-off-by Arnd Bergmann <[email protected]>Origin:
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git803f4e1eab7aSigned-off-by Juergen Gross <[email protected]>
Nevertheless ...
xen/include/xen/unaligned.h line 15
Quoted 12 lines
xen/include/xen/unaligned.h +6
-2@@ -15,67 +7,82 @@#include <asm/byteorder.h> #endif -#define get_unaligned (p) (*(p)) -#define put_unaligned (val, p) (*(p) = (val)) +/* +* This is the most generic implementation of unaligned accesses +* and should work almost anywhere. +*/ + +#define get_unaligned_t_ (type, ptr) ({ \
..., do we need the trailing underscores here in addition to the already sufficiently clear _t suffixes? (Leaving aside that ..._t generally is to denote types, not macros or functions.)
Jan
Re: [PATCH v2 2/3] xen: make include/xen/unaligned.h usable on all architectures
Quoted 26 lines · Jan Beulich
Quoted 8 lines · Juergen Gross
Instead of defining
get_unaligned()andput_unaligned()in a way that is only supporting architectures allowing unaligned accesses, use the same approach as the Linux kernel and let the compiler do the decision how to generate the code for probably unaligned data accesses.Update
include/xen/unaligned.hfrominclude/asm-generic/unaligned.hof the Linux kernel.Quoted 8 lines
The generated code has been checked to be the same on x86.
Modify the Linux variant to not use underscore prefixed identifiers, avoid unneeded parentheses and drop the 24-bit accessors.
Signed-off-by Arnd Bergmann <[email protected]>Origin:
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git803f4e1eab7aSigned-off-by Juergen Gross <[email protected]>Acked-by Jan Beulich <[email protected]>Nevertheless ...
Quoted 4 lines
xen/include/xen/unaligned.h +0
-0@@ -15,67 +7,82 @@#include <asm/byteorder.h> #endif
Quoted 12 lines
xen/include/xen/unaligned.h line 15
Quoted 8 lines
xen/include/xen/unaligned.h +6
-2-#define get_unaligned (p) (*(p)) -#define put_unaligned (val, p) (*(p) = (val)) +/* +* This is the most generic implementation of unaligned accesses +* and should work almost anywhere. +*/ + +#define get_unaligned_t_ (type, ptr) ({ \..., do we need the trailing underscores here in addition to the already sufficiently clear _t suffixes? (Leaving aside that ..._t generally is to denote types, not macros or functions.)
Maybe we should just name it get_unaligned_type()?
Juergen
Re: [PATCH v2 2/3] xen: make include/xen/unaligned.h usable on all architectures
Quoted 19 lines · Juergen Gross
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Juergen Gross
xen/include/xen/unaligned.h +0
-0@@ -15,67 +7,82 @@#include <asm/byteorder.h> #endif Quoted 12 lines
xen/include/xen/unaligned.h line 15
Quoted 8 lines
xen/include/xen/unaligned.h +6
-2-#define get_unaligned (p) (*(p)) -#define put_unaligned (val, p) (*(p) = (val)) +/* +* This is the most generic implementation of unaligned accesses +* and should work almost anywhere. +*/ + +#define get_unaligned_t_ (type, ptr) ({ \..., do we need the trailing underscores here in addition to the already sufficiently clear _t suffixes? (Leaving aside that ..._t generally is to denote types, not macros or functions.)
Quoted 1 line
Maybe we should just name it
get_unaligned_type()?
I wouldn't mind, but Andrew mentioning min_t() / max_t() suggests the present naming might be okay, too. (Still those two macros signal something quite different with their _t suffixes.)
Jan
Re: [PATCH v2 2/3] xen: make include/xen/unaligned.h usable on all architectures
In reply to Jan BeulichQuoted 25 lines · Jan Beulich
Quoted 8 lines · Juergen Gross
Instead of defining
get_unaligned()andput_unaligned()in a way that is only supporting architectures allowing unaligned accesses, use the same approach as the Linux kernel and let the compiler do the decision how to generate the code for probably unaligned data accesses.Update
include/xen/unaligned.hfrominclude/asm-generic/unaligned.hof the Linux kernel.Quoted 8 lines
The generated code has been checked to be the same on x86.
Modify the Linux variant to not use underscore prefixed identifiers, avoid unneeded parentheses and drop the 24-bit accessors.
Signed-off-by Arnd Bergmann <[email protected]>Origin:
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git803f4e1eab7aSigned-off-by Juergen Gross <[email protected]>Acked-by Jan Beulich <[email protected]>Nevertheless ...
Quoted 4 lines
xen/include/xen/unaligned.h +0
-0@@ -15,67 +7,82 @@#include <asm/byteorder.h> #endif
Quoted 11 lines
xen/include/xen/unaligned.h line 15
Quoted 8 lines
xen/include/xen/unaligned.h +6
-2-#define get_unaligned (p) (*(p)) -#define put_unaligned (val, p) (*(p) = (val)) +/* +* This is the most generic implementation of unaligned accesses +* and should work almost anywhere. +*/ + +#define get_unaligned_t_ (type, ptr) ({ \..., do we need the trailing underscores here in addition to the already sufficiently clear _t suffixes? (Leaving aside that ..._t generally is to denote types, not macros or functions.)
_t is fine. It's what we use for {min,max}_t() and friends too.
~Andrew
Re: [PATCH v2 2/3] xen: make include/xen/unaligned.h usable on all architectures
In reply to Juergen Grossxen/include/xen/unaligned.h line 2
Quoted 8 lines · Juergen Gross
xen/include/xen/unaligned.h +1
-1@@ -1,12 +1,4 @@ -static inline uint16_t get_unaligned_be16 (const void *p) +static inline u16 get_unaligned_le16 (const void *p)
I've done some cleanup for you.
You swapped away from using stdint types, and shuffled the order of functions, which made the diff basically illegible.
https://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=commitdiff;h=1d448a845ee4ad74cab76563b3a2552b10953186
is a much shorter diff.
~Andrew
Re: [PATCH v2 2/3] xen: make include/xen/unaligned.h usable on all architectures
Quoted 11 lines · Andrew Cooper
xen/include/xen/unaligned.h line 2
Quoted 8 lines · Juergen Gross
xen/include/xen/unaligned.h +1
-1@@ -1,12 +1,4 @@ -static inline uint16_t get_unaligned_be16 (const void *p) +static inline u16 get_unaligned_le16 (const void *p)I've done some cleanup for you.
Quoted 5 lines
You swapped away from using stdint types, and shuffled the order of functions, which made the diff basically illegible.
https://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=commitdiff;h=1d448a845ee4ad74cab76563b3a2552b10953186
is a much shorter diff.
Oh, and the Origin link ought to be
https://git.kernel.org/torvalds/c/803f4e1eab7a8938ba3a3c30dd4eb5e9eeef5e63
which is shorter and also lets people on the web view it.
~Andrew
Re: [PATCH v2 2/3] xen: make include/xen/unaligned.h usable on all architectures
Quoted 15 lines · Andrew Cooper
Quoted 14 lines · Andrew Cooper
xen/include/xen/unaligned.h line 2
Quoted 8 lines · Juergen Gross
xen/include/xen/unaligned.h +1
-1@@ -1,12 +1,4 @@ -static inline uint16_t get_unaligned_be16 (const void *p) +static inline u16 get_unaligned_le16 (const void *p)I've done some cleanup for you.
You swapped away from using stdint types, and shuffled the order of functions, which made the diff basically illegible.
Quoted 8 lines
Quoted 2 lines
https://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=commitdiff;h=1d448a845ee4ad74cab76563b3a2552b10953186
is a much shorter diff.Oh, and the Origin link ought to be
https://git.kernel.org/torvalds/c/803f4e1eab7a8938ba3a3c30dd4eb5e9eeef5e63
which is shorter and also lets people on the web view it.
Then we should update docs/process/sending-patches.pandoc to reflect that possibility to reference a commit (I wouldn't like that change, though).
Juergen
Re: [PATCH v2 2/3] xen: make include/xen/unaligned.h usable on all architectures
In reply to Andrew CooperQuoted 12 lines · Andrew Cooper
xen/include/xen/unaligned.h line 2
Quoted 8 lines · Juergen Gross
xen/include/xen/unaligned.h +1
-1@@ -1,12 +1,4 @@ -static inline uint16_t get_unaligned_be16 (const void *p) +static inline u16 get_unaligned_le16 (const void *p)I've done some cleanup for you.
Quoted 5 lines
You swapped away from using stdint types, and shuffled the order of functions, which made the diff basically illegible.
https://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=commitdiff;h=1d448a845ee4ad74cab76563b3a2552b10953186
is a much shorter diff.
Works for me.
Juergen
PATCH v2 3/3 xen: remove asm/unaligned.h
In reply to Juergen GrossWith include/xen/unaligned.h now dealing properly with unaligned accesses for all architectures, asm/unaligned.h can be removed and users can be switched to include xen/unaligned.h instead.
--- xen/arch/x86/include/asm/unaligned.h | 6 ------ xen/common/lz4/defs.h | 2 +- xen/common/lzo.c | 2 +- xen/common/unlzo.c | 2 +- xen/common/xz/private.h | 2 +- xen/common/zstd/mem.h | 2 +- xen/lib/xxhash32.c | 2 +- xen/lib/xxhash64.c | 2 +- 8 files changed, 7 insertions(+), 13 deletions(-) delete mode 100644 xen/arch/x86/include/asm/unaligned.h
xen/arch/x86/include/asm/unaligned.h deleted +0 -6
@@ -1,6 +0,0 @@ -#ifndef __ASM_UNALIGNED_H__ -#define __ASM_UNALIGNED_H__ - -#include <xen/unaligned.h> - -#endif /* __ASM_UNALIGNED_H__ */
xen/common/lz4/defs.h +1 -1
@@ -10,7 +10,7 @@#ifdef __XEN__#include <asm/byteorder.h> -#include <asm/unaligned.h> +#include <xen/unaligned.h> #else static inline u16 get_unaligned_le16 (const void *p)
xen/common/lzo.c +1 -1
@@ -97,7 +97,7 @@#ifdef __XEN__#include <xen/lib.h> #include <asm/byteorder.h> -#include <asm/unaligned.h> +#include <xen/unaligned.h> #else #define get_unaligned_le16 (_p) (*(u16 *)(_p))#endif
xen/common/unlzo.c +1 -1
@@ -34,7 +34,7 @@#ifdef __XEN__#include <asm/byteorder.h> -#include <asm/unaligned.h> +#include <xen/unaligned.h> #else static inline u16 get_unaligned_be16 (const void *p)
xen/common/xz/private.h +1 -1
@@ -13,7 +13,7 @@#ifdef __XEN__#include <xen/kernel.h> #include <asm/byteorder.h> -#include <asm/unaligned.h> +#include <xen/unaligned.h> #else static inline u32 get_unaligned_le32 (const void *p)
xen/common/zstd/mem.h +1 -1
@@ -23,7 +23,7 @@#ifdef __XEN__#include <xen/string.h> /* memcpy */ #include <xen/types.h> /* size_t, ptrdiff_t */ -#include <asm/unaligned.h> +#include <xen/unaligned.h> #endif /*-****************************************
xen/lib/xxhash32.c +1 -1
@@ -42,7 +42,7 @@#include <xen/errno.h> #include <xen/string.h> #include <xen/xxhash.h> -#include <asm/unaligned.h> +#include <xen/unaligned.h> /*-************************************* * Macros
xen/lib/xxhash64.c +1 -1
@@ -43,7 +43,7 @@#include <xen/errno.h> #include <xen/string.h> #include <xen/xxhash.h> -#include <asm/unaligned.h> +#include <xen/unaligned.h> #endif /*-*************************************
Signature · 1 line
2.35.3