Skip to contentSkip to conversation

xen: have a more generic unaligned.h header

PATCH Superseded Juergen Gross 4 participants 20 messages Dec 5, 2023 Zstd

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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).

Juergen Gross (2):
xen: make include/xen/unaligned.h usable on all architectures
xen: remove asm/unaligned.h

 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          | 121 +++++++++++++++++++--------
 xen/lib/xxhash32.c                   |   2 +-
 xen/lib/xxhash64.c                   |   2 +-
 9 files changed, 93 insertions(+), 48 deletions(-)
 delete mode 100644 xen/arch/x86/include/asm/unaligned.h
Signature · 1 line
2.35.3
#1 lore raw

PATCH 1/2 xen: make include/xen/unaligned.h usable on all architectures

Instead 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.

Origin: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 803f4e1eab7a

---
 xen/include/xen/unaligned.h | 121 +++++++++++++++++++++++++-----------
 1 file changed, 86 insertions(+), 35 deletions(-)
xen/include/xen/unaligned.h +86 -35
index 0a2b16d05d..325d9f875f 100644
@@ -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,126 @@
 #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 *__pptr = (typeof(__pptr))(ptr);	\
+	__pptr->x;								\
+})
+
+#define __put_unaligned_t(type, val, ptr) do {					\
+	struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr);		\
+	__pptr->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 u16 get_unaligned_le16(const void *p)
+{
+	return le16_to_cpu(__get_unaligned_t(__le16, p));
+}
+
+static inline u32 get_unaligned_le32(const void *p)
+{
+	return le32_to_cpu(__get_unaligned_t(__le32, p));
+}
+
+static inline u64 get_unaligned_le64(const void *p)
+{
+	return le64_to_cpu(__get_unaligned_t(__le64, p));
+}
+
+static inline void put_unaligned_le16(u16 val, void *p)
+{
+	__put_unaligned_t(__le16, cpu_to_le16(val), p);
+}
+
+static inline void put_unaligned_le32(u32 val, void *p)
+{
+	__put_unaligned_t(__le32, cpu_to_le32(val), p);
+}
+
+static inline void put_unaligned_le64(u64 val, void *p)
+{
+	__put_unaligned_t(__le64, cpu_to_le64(val), p);
+}
+
+static inline u16 get_unaligned_be16(const void *p)
+{
+	return be16_to_cpu(__get_unaligned_t(__be16, p));
+}
+
+static inline u32 get_unaligned_be32(const void *p)
+{
+	return be32_to_cpu(__get_unaligned_t(__be32, p));
+}
 
-static inline uint16_t get_unaligned_be16(const void *p)
+static inline u64 get_unaligned_be64(const void *p)
 {
-	return be16_to_cpup(p);
+	return be64_to_cpu(__get_unaligned_t(__be64, p));
 }
 
-static inline void put_unaligned_be16(uint16_t val, void *p)
+static inline void put_unaligned_be16(u16 val, void *p)
 {
-	*(__force __be16*)p = cpu_to_be16(val);
+	__put_unaligned_t(__be16, cpu_to_be16(val), p);
 }
 
-static inline uint32_t get_unaligned_be32(const void *p)
+static inline void put_unaligned_be32(u32 val, void *p)
 {
-	return be32_to_cpup(p);
+	__put_unaligned_t(__be32, cpu_to_be32(val), p);
 }
 
-static inline void put_unaligned_be32(uint32_t val, void *p)
+static inline void put_unaligned_be64(u64 val, void *p)
 {
-	*(__force __be32*)p = cpu_to_be32(val);
+	__put_unaligned_t(__be64, cpu_to_be64(val), p);
 }
 
-static inline uint64_t get_unaligned_be64(const void *p)
+static inline u32 __get_unaligned_be24(const u8 *p)
 {
-	return be64_to_cpup(p);
+	return p[0] << 16 | p[1] << 8 | p[2];
 }
 
-static inline void put_unaligned_be64(uint64_t val, void *p)
+static inline u32 get_unaligned_be24(const void *p)
 {
-	*(__force __be64*)p = cpu_to_be64(val);
+	return __get_unaligned_be24(p);
 }
 
-static inline uint16_t get_unaligned_le16(const void *p)
+static inline u32 __get_unaligned_le24(const u8 *p)
 {
-	return le16_to_cpup(p);
+	return p[0] | p[1] << 8 | p[2] << 16;
 }
 
-static inline void put_unaligned_le16(uint16_t val, void *p)
+static inline u32 get_unaligned_le24(const void *p)
 {
-	*(__force __le16*)p = cpu_to_le16(val);
+	return __get_unaligned_le24(p);
 }
 
-static inline uint32_t get_unaligned_le32(const void *p)
+static inline void __put_unaligned_be24(const u32 val, u8 *p)
 {
-	return le32_to_cpup(p);
+	*p++ = val >> 16;
+	*p++ = val >> 8;
+	*p++ = val;
 }
 
-static inline void put_unaligned_le32(uint32_t val, void *p)
+static inline void put_unaligned_be24(const u32 val, void *p)
 {
-	*(__force __le32*)p = cpu_to_le32(val);
+	__put_unaligned_be24(val, p);
 }
 
-static inline uint64_t get_unaligned_le64(const void *p)
+static inline void __put_unaligned_le24(const u32 val, u8 *p)
 {
-	return le64_to_cpup(p);
+	*p++ = val;
+	*p++ = val >> 8;
+	*p++ = val >> 16;
 }
 
-static inline void put_unaligned_le64(uint64_t val, void *p)
+static inline void put_unaligned_le24(const u32 val, void *p)
 {
-	*(__force __le64*)p = cpu_to_le64(val);
+	__put_unaligned_le24(val, p);
 }
 
 #endif /* __XEN_UNALIGNED_H__ */
Signature · 1 line
2.35.3
#2 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

Hi Juergen,

Quoted 5 lines · Juergen Gross

Instead 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.

Quoted 8 lines

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.

Origin: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 803f4e1eab7a

Can you outline your end goal? At least on arm32, I believe this will result to abort because event if the architecture support unaligned access, we are preventing them on Arm32.

Cheers,

Signature · 1 line
Julien Grall
#3 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

Quoted 11 lines · Julien Grall

Hi Juergen,

Quoted 8 lines · Juergen Gross

Instead 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.

Quoted 7 lines
Quoted 5 lines

The generated code has been checked to be the same on x86.

Origin: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 803f4e1eab7a

Can you outline your end goal? At least on arm32, I believe this will result to abort because event if the architecture support unaligned access, we are preventing them on Arm32.

I need something like that in Xen tools for supporting packed data accesses on the 9pfs ring page, so I looked into the hypervisor for related support.

I guess for arm32 using -mno-unaligned-access when building should avoid any unaligned accesses?

Juergen

#4 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

Hi Juergen,

Quoted 14 lines · Juergen Gross
Quoted 13 lines · Julien Grall

Hi Juergen,

Quoted 10 lines · Juergen Gross

Instead 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.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Origin: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 803f4e1eab7a

Can you outline your end goal? At least on arm32, I believe this will result to abort because event if the architecture support unaligned access, we are preventing them on Arm32.

I need something like that in Xen tools for supporting packed data accesses on the 9pfs ring page, so I looked into the hypervisor for related support.

Did we really introduce an ABI requiring unaligned access??? Or is this something you are coming up with?

Anyway, IIRC Linux allows unaligned access. So the problem I am describing is only for the hypervisor. Although, I would like to point out that unaligned access has no atomicity guarantee. I assume this is not going to be a concern for you?

Quoted 2 lines

I guess for arm32 using -mno-unaligned-access when building should avoid any unaligned accesses?

I am not sure. This is implies the compiler will be able to infer that the access will be unaligned. Is this always the case?

Anyway, given you don't seem to have a use-case yet, I would simply to consider to surround the declaration with an a config which can be selected if unaligned access is supported.

Cheers,

Signature · 1 line
Julien Grall
#5 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

Quoted 21 lines · Julien Grall

Hi Juergen,

Quoted 18 lines · Juergen Gross
Quoted 17 lines · Julien Grall

Hi Juergen,

Quoted 13 lines · Juergen Gross

Instead 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.

Origin: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 803f4e1eab7a

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Can you outline your end goal? At least on arm32, I believe this will result to abort because event if the architecture support unaligned access, we are preventing them on Arm32.

I need something like that in Xen tools for supporting packed data accesses on the 9pfs ring page, so I looked into the hypervisor for related support.

Did we really introduce an ABI requiring unaligned access??? Or is this something you are coming up with?

This is the 9pfs protocol (see [1]).

Quoted 1 line

Anyway, IIRC Linux allows unaligned access. So the problem I am describing is only for the hypervisor. Although, I would like to point out that unaligned access has no atomicity guarantee. I assume this is not going to be a concern for you?

Correct.

Quoted 5 lines
Quoted 3 lines

I guess for arm32 using -mno-unaligned-access when building should avoid any unaligned accesses?

I am not sure. This is implies the compiler will be able to infer that the access will be unaligned. Is this always the case?

This should happen through the "__packed" attribute on the access macros. As e.g. MIPS doesn't support unaligned accesses, but is working with those access macros in the Linux kernel, I suspect the attribute is doing its job.

Quoted 1 line

Anyway, given you don't seem to have a use-case yet, I would simply to consider to surround the declaration with an a config which can be selected if unaligned access is supported.

Like in xen/common/lzo.c et al? Those are compiled with CONFIG_X86 only today, but I guess other archs might need the decompressors in future, too.

Juergen

[1]: http://ericvh.github.io/9p-rfc/rfc9p2000.html

#6 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

Quoted 22 lines · Juergen Gross
Quoted 21 lines · Julien Grall

Hi Juergen,

Quoted 18 lines · Juergen Gross
Quoted 17 lines · Julien Grall

Hi Juergen,

Quoted 13 lines · Juergen Gross

Instead 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.

Origin: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 803f4e1eab7a

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Can you outline your end goal? At least on arm32, I believe this will result to abort because event if the architecture support unaligned access, we are preventing them on Arm32.

I need something like that in Xen tools for supporting packed data accesses on the 9pfs ring page, so I looked into the hypervisor for related support.

Did we really introduce an ABI requiring unaligned access??? Or is this something you are coming up with?

This is the 9pfs protocol (see [1]).

Urgh :(.

Quoted 6 lines
Quoted 2 lines

Anyway, IIRC Linux allows unaligned access. So the problem I am describing is only for the hypervisor. Although, I would like to point out that unaligned access has no atomicity guarantee. I assume this is not going to be a concern for you?

Correct.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I guess for arm32 using -mno-unaligned-access when building should avoid any unaligned accesses?

I am not sure. This is implies the compiler will be able to infer that the access will be unaligned. Is this always the case?

This should happen through the "__packed" attribute on the access macros. As e.g. MIPS doesn't support unaligned accesses, but is working with those access macros in the Linux kernel, I suspect the attribute is doing its job.

Someone will need to dig a bit deeper to confirm and also the impact on the rest of the hypervisor.

Quoted 4 lines
Quoted 2 lines

Anyway, given you don't seem to have a use-case yet, I would simply to consider to surround the declaration with an a config which can be selected if unaligned access is supported.

Like in xen/common/lzo.c et al?

Just to clarify, I am suggesting to add in unaligned.h:

#ifdef CONFIG_HAS_UNALIGNED_ACCESS

your definitions

#endif

And then for X86, select CONFIG_HAS_UNALIGNED_ACCESS.

Those are compiled with CONFIG_X86 only

Quoted 2 lines

today,
but I guess other archs might need the decompressors in future, too.

Possibly yes. But my point is that you don't have to solve the problem today. Yet I don't think it is wise to allow the header to be used on arm32 until we have done some investigation.

And to clarify, I am not asking you to do the investigation.

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

Quoted 8 lines · Julien Grall
Quoted 6 lines · Juergen Gross
Quoted 3 lines · Julien Grall

Anyway, given you don't seem to have a use-case yet, I would simply to consider to surround the declaration with an a config which can be selected if unaligned access is supported.

Like in xen/common/lzo.c et al?

Quoted 7 lines

Just to clarify, I am suggesting to add in unaligned.h:

#ifdef CONFIG_HAS_UNALIGNED_ACCESS

your definitions

#endif

But that would be wrong: HAS_UNALIGNED_ACCESS would be there to indicate one does _not_ need any special accessors.

Jan

#8 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

Hi,

Quoted 11 lines · Jan Beulich
Quoted 10 lines · Julien Grall
Quoted 6 lines · Juergen Gross
Quoted 3 lines · Julien Grall

Anyway, given you don't seem to have a use-case yet, I would simply to consider to surround the declaration with an a config which can be selected if unaligned access is supported.

Like in xen/common/lzo.c et al?

Just to clarify, I am suggesting to add in unaligned.h:

Quoted 8 lines
Quoted 5 lines

#ifdef CONFIG_HAS_UNALIGNED_ACCESS

your definitions

#endif

But that would be wrong: HAS_UNALIGNED_ACCESS would be there to indicate one does _not_ need any special accessors.

I am guessing you are disagreeing on the name rather than the concept? If so, what about CONFIG_UNALIGNED_ACCESS_ALLOWED?

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

Quoted 16 lines · Julien Grall
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Julien Grall
Quoted 6 lines · Juergen Gross
Quoted 3 lines · Julien Grall

Anyway, given you don't seem to have a use-case yet, I would simply to consider to surround the declaration with an a config which can be selected if unaligned access is supported.

Like in xen/common/lzo.c et al?

Just to clarify, I am suggesting to add in unaligned.h:

#ifdef CONFIG_HAS_UNALIGNED_ACCESS

your definitions

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

#endif

But that would be wrong: HAS_UNALIGNED_ACCESS would be there to indicate one does _not_ need any special accessors.

I am guessing you are disagreeing on the name rather than the concept? If so, what about CONFIG_UNALIGNED_ACCESS_ALLOWED?

This would repeat the mistake that we had in Linux in the past (and still do in some drivers): Simply dereferencing a misaligned pointer is always a bug, even on architectures that have efficient unaligned load/store instructions, because C makes this undefined behavior and gcc has optimizations that assume e.g. 'int *' to never have the lower two bits set [1].

The helpers that Jürgen copied from Linux is what we use to abstract accesses to objects that we know may be misaligned. If the underlying ISA allows a direct access (e.g. on arm64 and x86), this is as efficient as a normal pointer access but prevents the dangerous optimizations in gcc. On architectures without these instructions, the access will be turned into safe bytewise access.

This is similar to a __packed annotation on a data structure, but also works in cases where such an annotation wouldn't work for other reasons.

Arnd

[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100363

#10 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

Hi Arnd,

Thanks for the answer.

Quoted 25 lines · Arnd Bergmann
Quoted 16 lines · Julien Grall
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Julien Grall
Quoted 6 lines · Juergen Gross
Quoted 3 lines · Julien Grall

Anyway, given you don't seem to have a use-case yet, I would simply to consider to surround the declaration with an a config which can be selected if unaligned access is supported.

Like in xen/common/lzo.c et al?

Just to clarify, I am suggesting to add in unaligned.h:

#ifdef CONFIG_HAS_UNALIGNED_ACCESS

your definitions

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

#endif

But that would be wrong: HAS_UNALIGNED_ACCESS would be there to indicate one does _not_ need any special accessors.

I am guessing you are disagreeing on the name rather than the concept? If so, what about CONFIG_UNALIGNED_ACCESS_ALLOWED?

Quoted 7 lines

This would repeat the mistake that we had in Linux in the past (and still do in some drivers): Simply dereferencing a misaligned pointer is always a bug, even on architectures that have efficient unaligned load/store instructions, because C makes this undefined behavior and gcc has optimizations that assume e.g. 'int *' to never have the lower two bits set [1].

Just to clarify, I haven't suggested to use 'int *'. My point was more that I don't think that the helpers would work as-is on arm32 because even if the ISA allows a direct access, we are setting the bit in SCTLR to disable unaligned access.

As Juergen is proposing a common header, then I could ask him to do the work to confirm that the helpers properly work on arm32. But I think this is unfair.

I also don't have the time to chase to look at it and this is not yet used in code reached by Arm. Hence my suggestion for now to protect the code so if someone tries to use them, then they know that it doesn't (yet) work on Arm.

Cheers,

Signature · 1 line
Julien Grall
#11 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

Quoted 12 lines · Julien Grall
Quoted 10 lines · Arnd Bergmann
Quoted 2 lines · Julien Grall
Quoted 1 line · Jan Beulich

On 05.12.2023 14:46, Julien Grall wrote:

This would repeat the mistake that we had in Linux in the past (and still do in some drivers): Simply dereferencing a misaligned pointer is always a bug, even on architectures that have efficient unaligned load/store instructions, because C makes this undefined behavior and gcc has optimizations that assume e.g. 'int *' to never have the lower two bits set [1].

Quoted 8 lines

Just to clarify, I haven't suggested to use 'int *'. My point was more that I don't think that the helpers would work as-is on arm32 because even if the ISA allows a direct access, we are setting the bit in SCTLR to disable unaligned access.

As Juergen is proposing a common header, then I could ask him to do the work to confirm that the helpers properly work on arm32. But I think this is unfair.

When I introduced the helpers in Linux, I showed that these produce the best output on all modern compilers (at least gcc-5, probably earlier) for both architectures that allow unaligned access and for those that don't. We used to have architecture specific helpers depending on what each architecture could do, but all the other variants we had were either wrong or less efficient.

If for some reason an Arm system is configured to trap all unaligned access, then you must already pass -mno-unaligned-access to the compiler to prevent certain optimizations, and then the helpers will still behave correctly (the same way they do on armv5, which never has unaligned access). On armv7 with -munaligned-access, the same functions only prevent the use of stm/ldm and strd/ldrd but still use ldr/str.

Arnd

#12 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

Hi Arnd,

Quoted 30 lines · Arnd Bergmann
Quoted 12 lines · Julien Grall
Quoted 10 lines · Arnd Bergmann
Quoted 2 lines · Julien Grall
Quoted 1 line · Jan Beulich

On 05.12.2023 14:46, Julien Grall wrote:

This would repeat the mistake that we had in Linux in the past (and still do in some drivers): Simply dereferencing a misaligned pointer is always a bug, even on architectures that have efficient unaligned load/store instructions, because C makes this undefined behavior and gcc has optimizations that assume e.g. 'int *' to never have the lower two bits set [1].

Quoted 8 lines

Just to clarify, I haven't suggested to use 'int *'. My point was more that I don't think that the helpers would work as-is on arm32 because even if the ISA allows a direct access, we are setting the bit in SCTLR to disable unaligned access.

As Juergen is proposing a common header, then I could ask him to do the work to confirm that the helpers properly work on arm32. But I think this is unfair.

When I introduced the helpers in Linux, I showed that these produce the best output on all modern compilers (at least gcc-5, probably earlier) for both architectures that allow unaligned access and for those that don't. We used to have architecture specific helpers depending on what each architecture could do, but all the other variants we had were either wrong or less efficient.

Quoted 8 lines

If for some reason an Arm system is configured to trap all unaligned access, then you must already pass -mno-unaligned-access to the compiler to prevent certain optimizations, and then the helpers will still behave correctly (the same way they do on armv5, which never has unaligned access). On armv7 with -munaligned-access, the same functions only prevent the use of stm/ldm and strd/ldrd but still use ldr/str.

Unfortunately we don't explicitely do. This would explain why I saw some issues with certain compiler [1].

So I agree that adding -mno-unaligned-access for arm32 makes sense.

@Juergen, do you want me to send a patch?

Cheers,

[1] [email protected]

Signature · 1 line
Julien Grall
#13 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

Quoted 42 lines · Julien Grall

Hi Arnd,

Quoted 30 lines · Arnd Bergmann
Quoted 12 lines · Julien Grall
Quoted 10 lines · Arnd Bergmann
Quoted 2 lines · Julien Grall
Quoted 1 line · Jan Beulich

On 05.12.2023 14:46, Julien Grall wrote:

This would repeat the mistake that we had in Linux in the past (and still do in some drivers): Simply dereferencing a misaligned pointer is always a bug, even on architectures that have efficient unaligned load/store instructions, because C makes this undefined behavior and gcc has optimizations that assume e.g. 'int *' to never have the lower two bits set [1].

Quoted 8 lines

Just to clarify, I haven't suggested to use 'int *'. My point was more that I don't think that the helpers would work as-is on arm32 because even if the ISA allows a direct access, we are setting the bit in SCTLR to disable unaligned access.

As Juergen is proposing a common header, then I could ask him to do the work to confirm that the helpers properly work on arm32. But I think this is unfair.

When I introduced the helpers in Linux, I showed that these produce the best output on all modern compilers (at least gcc-5, probably earlier) for both architectures that allow unaligned access and for those that don't. We used to have architecture specific helpers depending on what each architecture could do, but all the other variants we had were either wrong or less efficient.

Quoted 8 lines

If for some reason an Arm system is configured to trap all unaligned access, then you must already pass -mno-unaligned-access to the compiler to prevent certain optimizations, and then the helpers will still behave correctly (the same way they do on armv5, which never has unaligned access). On armv7 with -munaligned-access, the same functions only prevent the use of stm/ldm and strd/ldrd but still use ldr/str.

Quoted 5 lines

Unfortunately we don't explicitely do. This would explain why I saw some issues with certain compiler [1].

So I agree that adding -mno-unaligned-access for arm32 makes sense.

@Juergen, do you want me to send a patch?

Yes, will do.

Juergen

#14 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

In reply to Julien Grall
Quoted 16 lines · Julien Grall
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Julien Grall
Quoted 6 lines · Juergen Gross
Quoted 3 lines · Julien Grall

Anyway, given you don't seem to have a use-case yet, I would simply to consider to surround the declaration with an a config which can be selected if unaligned access is supported.

Like in xen/common/lzo.c et al?

Just to clarify, I am suggesting to add in unaligned.h:

#ifdef CONFIG_HAS_UNALIGNED_ACCESS

your definitions

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

#endif

But that would be wrong: HAS_UNALIGNED_ACCESS would be there to indicate one does _not_ need any special accessors.

I am guessing you are disagreeing on the name rather than the concept? If so, what about CONFIG_UNALIGNED_ACCESS_ALLOWED?

Not really, no. Of course the name needs to properly express the purpose. But I don't see why a Kconfig control would be appropriate here. You simply want to provide accessors to unaligned data. Nobody needs to use them, but when you have to, they ought to be there. A Kconfig control (of the name you suggested first) would be helpful to not penalize architectures which can do unaligned accesses without any helpers (in case the code generated through the helpers turned out sub-optimal).

Jan

#15 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

In reply to Julien Grall
Quoted 66 lines · Julien Grall
Quoted 22 lines · Juergen Gross
Quoted 21 lines · Julien Grall

Hi Juergen,

Quoted 18 lines · Juergen Gross
Quoted 17 lines · Julien Grall

Hi Juergen,

Quoted 13 lines · Juergen Gross

Instead 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.

Origin: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 803f4e1eab7a

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Can you outline your end goal? At least on arm32, I believe this will result to abort because event if the architecture support unaligned access, we are preventing them on Arm32.

I need something like that in Xen tools for supporting packed data accesses on the 9pfs ring page, so I looked into the hypervisor for related support.

Did we really introduce an ABI requiring unaligned access??? Or is this something you are coming up with?

This is the 9pfs protocol (see [1]).

Urgh :(.

Quoted 6 lines
Quoted 2 lines

Anyway, IIRC Linux allows unaligned access. So the problem I am describing is only for the hypervisor. Although, I would like to point out that unaligned access has no atomicity guarantee. I assume this is not going to be a concern for you?

Correct.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I guess for arm32 using -mno-unaligned-access when building should avoid any unaligned accesses?

I am not sure. This is implies the compiler will be able to infer that the access will be unaligned. Is this always the case?

This should happen through the "__packed" attribute on the access macros. As e.g. MIPS doesn't support unaligned accesses, but is working with those access macros in the Linux kernel, I suspect the attribute is doing its job.

Someone will need to dig a bit deeper to confirm and also the impact on the rest of the hypervisor.

Quoted 4 lines
Quoted 2 lines

Anyway, given you don't seem to have a use-case yet, I would simply to consider to surround the declaration with an a config which can be selected if unaligned access is supported.

Like in xen/common/lzo.c et al?

Just to clarify, I am suggesting to add in unaligned.h:

#ifdef CONFIG_HAS_UNALIGNED_ACCESS

your definitions

#endif

Quoted 8 lines

And then for X86, select CONFIG_HAS_UNALIGNED_ACCESS.

 Those are compiled with CONFIG_X86 only

Quoted 2 lines

today,
but I guess other archs might need the decompressors in future, too.

Possibly yes. But my point is that you don't have to solve the problem today. Yet I don't think it is wise to allow the header to be used on arm32 until we have done some investigation.

And to clarify, I am not asking you to do the investigation.

I've done a quick verification using gcc 7.5.

Using -mno-unaligned-access for 32-bit Arm seems to do the job:

#include <xen/unaligned.h>
int tst(const unsigned short *in)

{
    return get_unaligned(in);
}

results in:

00000000 <tst>:

   0:   e52db004        push    {fp}            @ (str fp, [sp, #-4]!)
   4:   e28db000        add     fp, sp, #0
   8:   e5d03000        ldrb    r3, [r0]
   c:   e5d00001        ldrb    r0, [r0, #1]
  10:   e1830400        orr     r0, r3, r0, lsl #8
  14:   e28bd000        add     sp, fp, #0
  18:   e49db004        pop     {fp}            @ (ldr fp, [sp], #4)
  1c:   e12fff1e        bx      lr

Without the -mno-unaligned-access I'm getting:

00000000 <tst>:

   0:   e52db004        push    {fp}            @ (str fp, [sp, #-4]!)
   4:   e28db000        add     fp, sp, #0
   8:   e1d000b0        ldrh    r0, [r0]
   c:   e28bd000        add     sp, fp, #0
  10:   e49db004        pop     {fp}            @ (ldr fp, [sp], #4)
  14:   e12fff1e        bx      lr

Juergen

#16 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

In reply to Juergen Gross
Quoted 10 lines · Juergen Gross
xen/include/xen/unaligned.h +4 -2
@@ -15,67 +7,126 @@
 #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.
+ */

xen/include/xen/unaligned.h line 23

Quoted 10 lines
xen/include/xen/unaligned.h +10 -0
+
+#define __get_unaligned_t(type, ptr) ({						\
+	const struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr);	\
+	__pptr->x;								\
+})
+
+#define __put_unaligned_t(type, val, ptr) do {					\
+	struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr);		\
+	__pptr->x = (val);							\
+} while (0)

Please can we avoid gaining new (even double) leading underscore symbols, especially when they're in helper (i.e. not intended to be used directly) constructs? No reason to introduce further issues wrt Misra adoption.

xen/include/xen/unaligned.h line 26

Quoted 2 lines
xen/include/xen/unaligned.h +2 -0
+#define get_unaligned(ptr)	__get_unaligned_t(typeof(*(ptr)), (ptr))
+#define put_unaligned(val, ptr) __put_unaligned_t(typeof(*(ptr)), (val), (ptr))

May I ask to omit excess parentheses where possible?

Quoted 60 lines
xen/include/xen/unaligned.h +45 -6
+static inline u16 get_unaligned_le16(const void *p)
+{
+	return le16_to_cpu(__get_unaligned_t(__le16, p));
+}
+
+static inline u32 get_unaligned_le32(const void *p)
+{
+	return le32_to_cpu(__get_unaligned_t(__le32, p));
+}
+
+static inline u64 get_unaligned_le64(const void *p)
+{
+	return le64_to_cpu(__get_unaligned_t(__le64, p));
+}
+
+static inline void put_unaligned_le16(u16 val, void *p)
+{
+	__put_unaligned_t(__le16, cpu_to_le16(val), p);
+}
+
+static inline void put_unaligned_le32(u32 val, void *p)
+{
+	__put_unaligned_t(__le32, cpu_to_le32(val), p);
+}
+
+static inline void put_unaligned_le64(u64 val, void *p)
+{
+	__put_unaligned_t(__le64, cpu_to_le64(val), p);
+}
+
+static inline u16 get_unaligned_be16(const void *p)
+{
+	return be16_to_cpu(__get_unaligned_t(__be16, p));
+}
+
+static inline u32 get_unaligned_be32(const void *p)
+{
+	return be32_to_cpu(__get_unaligned_t(__be32, p));
+}
 
-static inline uint16_t get_unaligned_be16(const void *p)
+static inline u64 get_unaligned_be64(const void *p)
 {
-	return be16_to_cpup(p);
+	return be64_to_cpu(__get_unaligned_t(__be64, p));
 }
 
-static inline void put_unaligned_be16(uint16_t val, void *p)
+static inline void put_unaligned_be16(u16 val, void *p)
 {
-	*(__force __be16*)p = cpu_to_be16(val);
+	__put_unaligned_t(__be16, cpu_to_be16(val), p);
 }
 
-static inline uint32_t get_unaligned_be32(const void *p)
+static inline void put_unaligned_be32(u32 val, void *p)
 {
-	return be32_to_cpup(p);
+	__put_unaligned_t(__be32, cpu_to_be32(val), p);
 }

xen/include/xen/unaligned.h line 88

Quoted 10 lines
xen/include/xen/unaligned.h +3 -3
 
-static inline void put_unaligned_be32(uint32_t val, void *p)
+static inline void put_unaligned_be64(u64 val, void *p)
 {
-	*(__force __be32*)p = cpu_to_be32(val);
+	__put_unaligned_t(__be64, cpu_to_be64(val), p);
 }
 
-static inline uint64_t get_unaligned_be64(const void *p)
+static inline u32 __get_unaligned_be24(const u8 *p)

Here and below - do you foresee use of these 24-bit helpers? They weren't there before, and the description also doesn't justify their introduction.

Jan

#17 lore raw

Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures

Quoted 99 lines · Jan Beulich
Quoted 10 lines · Juergen Gross
xen/include/xen/unaligned.h +4 -2
@@ -15,67 +7,126 @@
  #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.
+ */

xen/include/xen/unaligned.h line 23

Quoted 10 lines
xen/include/xen/unaligned.h +10 -0
+
+#define __get_unaligned_t(type, ptr) ({						\
+	const struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr);	\
+	__pptr->x;								\
+})
+
+#define __put_unaligned_t(type, val, ptr) do {					\
+	struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr);		\
+	__pptr->x = (val);							\
+} while (0)

Please can we avoid gaining new (even double) leading underscore symbols, especially when they're in helper (i.e. not intended to be used directly) constructs? No reason to introduce further issues wrt Misra adoption.

xen/include/xen/unaligned.h line 26

Quoted 2 lines
xen/include/xen/unaligned.h +2 -0
+#define get_unaligned(ptr)	__get_unaligned_t(typeof(*(ptr)), (ptr))
+#define put_unaligned(val, ptr) __put_unaligned_t(typeof(*(ptr)), (val), (ptr))

May I ask to omit excess parentheses where possible?

Quoted 68 lines
xen/include/xen/unaligned.h +47 -8
+static inline u16 get_unaligned_le16(const void *p)
+{
+	return le16_to_cpu(__get_unaligned_t(__le16, p));
+}
+
+static inline u32 get_unaligned_le32(const void *p)
+{
+	return le32_to_cpu(__get_unaligned_t(__le32, p));
+}
+
+static inline u64 get_unaligned_le64(const void *p)
+{
+	return le64_to_cpu(__get_unaligned_t(__le64, p));
+}
+
+static inline void put_unaligned_le16(u16 val, void *p)
+{
+	__put_unaligned_t(__le16, cpu_to_le16(val), p);
+}
+
+static inline void put_unaligned_le32(u32 val, void *p)
+{
+	__put_unaligned_t(__le32, cpu_to_le32(val), p);
+}
+
+static inline void put_unaligned_le64(u64 val, void *p)
+{
+	__put_unaligned_t(__le64, cpu_to_le64(val), p);
+}
+
+static inline u16 get_unaligned_be16(const void *p)
+{
+	return be16_to_cpu(__get_unaligned_t(__be16, p));
+}
+
+static inline u32 get_unaligned_be32(const void *p)
+{
+	return be32_to_cpu(__get_unaligned_t(__be32, p));
+}
 
-static inline uint16_t get_unaligned_be16(const void *p)
+static inline u64 get_unaligned_be64(const void *p)
  {
-	return be16_to_cpup(p);
+	return be64_to_cpu(__get_unaligned_t(__be64, p));
  }
 
-static inline void put_unaligned_be16(uint16_t val, void *p)
+static inline void put_unaligned_be16(u16 val, void *p)
  {
-	*(__force __be16*)p = cpu_to_be16(val);
+	__put_unaligned_t(__be16, cpu_to_be16(val), p);
  }
 
-static inline uint32_t get_unaligned_be32(const void *p)
+static inline void put_unaligned_be32(u32 val, void *p)
  {
-	return be32_to_cpup(p);
+	__put_unaligned_t(__be32, cpu_to_be32(val), p);
  }
 
-static inline void put_unaligned_be32(uint32_t val, void *p)
+static inline void put_unaligned_be64(u64 val, void *p)
  {
-	*(__force __be32*)p = cpu_to_be32(val);
+	__put_unaligned_t(__be64, cpu_to_be64(val), p);
  }
 
Quoted 5 lines

xen/include/xen/unaligned.h line 88

Quoted 2 lines
xen/include/xen/unaligned.h +1 -1
-static inline uint64_t get_unaligned_be64(const void *p)
+static inline u32 __get_unaligned_be24(const u8 *p)

Here and below - do you foresee use of these 24-bit helpers? They weren't there before, and the description also doesn't justify their introduction.

I have just applied the patch from the kernel (see Origin: tag).

I can change the patch according to your comments, of course.

Juergen

#18 lore raw

PATCH 2/2 xen: remove asm/unaligned.h

In reply to Juergen Gross

With 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
deleted file mode 100644
index 6070801d4a..0000000000
@@ -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
index 10609f5a53..6d81113266 100644
@@ -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
index a87c76dded..cc03f0f554 100644
@@ -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
index 74056778eb..bdcefa95b3 100644
@@ -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
index e6814250e8..2299705378 100644
@@ -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
index 2acae6a8ed..ae1e305126 100644
@@ -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
index e8d403e5ce..32efa651c5 100644
@@ -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
index 481e76fbcf..1858e236fe 100644
@@ -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