* [PATCH 0/2] xen: have a more generic unaligned.h header (take 2)
@ 2023-12-12 16:27 Juergen Gross
2023-12-12 16:27 ` [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures Juergen Gross
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Juergen Gross @ 2023-12-12 16:27 UTC (permalink / raw)
To: xen-devel
Cc: Juergen Gross, Wei Liu, Anthony PERARD, Andrew Cooper,
George Dunlap, Jan Beulich, Julien Grall, Stefano Stabellini,
Roger Pau Monné
Second try for the generic unaligned.h approach.
This time including a fix for building stubdom with libxenguest,
which is using a cruel hack to reuse the hypervisor's decompressing
code.
Juergen Gross (2):
xen: make include/xen/unaligned.h usable on all architectures
xen: remove asm/unaligned.h
.../guest/xg_dom_decompress_unsafe_zstd.c | 2 +-
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 | 53 +++++++++++--------
xen/lib/xxhash32.c | 2 +-
xen/lib/xxhash64.c | 2 +-
10 files changed, 38 insertions(+), 37 deletions(-)
delete mode 100644 xen/arch/x86/include/asm/unaligned.h
--
2.35.3
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures
2023-12-12 16:27 [PATCH 0/2] xen: have a more generic unaligned.h header (take 2) Juergen Gross
@ 2023-12-12 16:27 ` Juergen Gross
2023-12-12 16:47 ` Jan Beulich
2023-12-12 16:27 ` [PATCH 2/2] xen: remove asm/unaligned.h Juergen Gross
2023-12-21 9:37 ` [PATCH 0/2] xen: have a more generic unaligned.h header (take 2) Juergen Gross
2 siblings, 1 reply; 6+ messages in thread
From: Juergen Gross @ 2023-12-12 16:27 UTC (permalink / raw)
To: xen-devel
Cc: Juergen Gross, Wei Liu, Anthony PERARD, Andrew Cooper,
George Dunlap, Jan Beulich, Julien Grall, Stefano Stabellini,
Arnd Bergmann
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.
Modify the Linux variant to not use underscore prefixed identifiers,
avoid unneeded parentheses and drop the 24-bit accessors.
Add the definition of __packed to xg_dom_decompress_unsafe_zstd.c in
libxenguest as it is using a cruel hack to reuse the hypervisor's
decompressing code for stubdom.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Origin: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 803f4e1eab7a
Signed-off-by: Juergen Gross <jgross@suse.com>
---
.../guest/xg_dom_decompress_unsafe_zstd.c | 2 +-
xen/include/xen/unaligned.h | 53 +++++++++++--------
2 files changed, 31 insertions(+), 24 deletions(-)
diff --git a/tools/libs/guest/xg_dom_decompress_unsafe_zstd.c b/tools/libs/guest/xg_dom_decompress_unsafe_zstd.c
index 01eafaaaa6..7cd266444b 100644
--- a/tools/libs/guest/xg_dom_decompress_unsafe_zstd.c
+++ b/tools/libs/guest/xg_dom_decompress_unsafe_zstd.c
@@ -26,13 +26,13 @@ typedef uint64_t __be64;
#define __force
#define always_inline
#define noinline
+#define __packed __attribute__((__packed__))
#undef ERROR
#define __BYTEORDER_HAS_U64__
#define __TYPES_H__ /* xen/types.h guard */
#include "../../xen/include/xen/byteorder/little_endian.h"
-#define __ASM_UNALIGNED_H__ /* asm/unaligned.h guard */
#include "../../xen/include/xen/unaligned.h"
#include "../../xen/include/xen/xxhash.h"
#include "../../xen/lib/xxhash64.c"
diff --git a/xen/include/xen/unaligned.h b/xen/include/xen/unaligned.h
index 0a2b16d05d..3eda0ece11 100644
--- a/xen/include/xen/unaligned.h
+++ b/xen/include/xen/unaligned.h
@@ -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)
{
- return be16_to_cpup(p);
+ return be16_to_cpu(get_unaligned_t(__be16, p));
}
static inline void put_unaligned_be16(uint16_t 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)
{
- return be32_to_cpup(p);
+ return be32_to_cpu(get_unaligned_t(__be32, p));
}
static inline void put_unaligned_be32(uint32_t val, void *p)
{
- *(__force __be32*)p = cpu_to_be32(val);
+ put_unaligned_t(__be32, cpu_to_be32(val), p);
}
static inline uint64_t get_unaligned_be64(const void *p)
{
- return be64_to_cpup(p);
+ return be64_to_cpu(get_unaligned_t(__be64, p));
}
static inline void put_unaligned_be64(uint64_t val, void *p)
{
- *(__force __be64*)p = cpu_to_be64(val);
+ put_unaligned_t(__be64, cpu_to_be64(val), p);
}
static inline uint16_t get_unaligned_le16(const void *p)
{
- return le16_to_cpup(p);
+ return le16_to_cpu(get_unaligned_t(__le16, p));
}
static inline void put_unaligned_le16(uint16_t val, void *p)
{
- *(__force __le16*)p = cpu_to_le16(val);
+ put_unaligned_t(__le16, cpu_to_le16(val), p);
}
static inline uint32_t get_unaligned_le32(const void *p)
{
- return le32_to_cpup(p);
+ return le32_to_cpu(get_unaligned_t(__le32, p));
}
static inline void put_unaligned_le32(uint32_t val, void *p)
{
- *(__force __le32*)p = cpu_to_le32(val);
+ put_unaligned_t(__le32, cpu_to_le32(val), p);
}
static inline uint64_t get_unaligned_le64(const void *p)
{
- return le64_to_cpup(p);
+ return le64_to_cpu(get_unaligned_t(__le64, p));
}
static inline void put_unaligned_le64(uint64_t val, void *p)
{
- *(__force __le64*)p = cpu_to_le64(val);
+ put_unaligned_t(__le64, cpu_to_le64(val), p);
}
#endif /* __XEN_UNALIGNED_H__ */
--
2.35.3
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 2/2] xen: remove asm/unaligned.h
2023-12-12 16:27 [PATCH 0/2] xen: have a more generic unaligned.h header (take 2) Juergen Gross
2023-12-12 16:27 ` [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures Juergen Gross
@ 2023-12-12 16:27 ` Juergen Gross
2023-12-21 9:37 ` [PATCH 0/2] xen: have a more generic unaligned.h header (take 2) Juergen Gross
2 siblings, 0 replies; 6+ messages in thread
From: Juergen Gross @ 2023-12-12 16:27 UTC (permalink / raw)
To: xen-devel
Cc: Juergen Gross, Jan Beulich, Andrew Cooper, Roger Pau Monné,
Wei Liu, George Dunlap, Julien Grall, Stefano Stabellini
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.
Signed-off-by: Juergen Gross <jgross@suse.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
---
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
diff --git a/xen/arch/x86/include/asm/unaligned.h b/xen/arch/x86/include/asm/unaligned.h
deleted file mode 100644
index 6070801d4a..0000000000
--- a/xen/arch/x86/include/asm/unaligned.h
+++ /dev/null
@@ -1,6 +0,0 @@
-#ifndef __ASM_UNALIGNED_H__
-#define __ASM_UNALIGNED_H__
-
-#include <xen/unaligned.h>
-
-#endif /* __ASM_UNALIGNED_H__ */
diff --git a/xen/common/lz4/defs.h b/xen/common/lz4/defs.h
index 10609f5a53..6d81113266 100644
--- a/xen/common/lz4/defs.h
+++ b/xen/common/lz4/defs.h
@@ -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)
diff --git a/xen/common/lzo.c b/xen/common/lzo.c
index a87c76dded..cc03f0f554 100644
--- a/xen/common/lzo.c
+++ b/xen/common/lzo.c
@@ -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
diff --git a/xen/common/unlzo.c b/xen/common/unlzo.c
index 74056778eb..bdcefa95b3 100644
--- a/xen/common/unlzo.c
+++ b/xen/common/unlzo.c
@@ -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)
diff --git a/xen/common/xz/private.h b/xen/common/xz/private.h
index e6814250e8..2299705378 100644
--- a/xen/common/xz/private.h
+++ b/xen/common/xz/private.h
@@ -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)
diff --git a/xen/common/zstd/mem.h b/xen/common/zstd/mem.h
index 2acae6a8ed..ae1e305126 100644
--- a/xen/common/zstd/mem.h
+++ b/xen/common/zstd/mem.h
@@ -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
/*-****************************************
diff --git a/xen/lib/xxhash32.c b/xen/lib/xxhash32.c
index e8d403e5ce..32efa651c5 100644
--- a/xen/lib/xxhash32.c
+++ b/xen/lib/xxhash32.c
@@ -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
diff --git a/xen/lib/xxhash64.c b/xen/lib/xxhash64.c
index 481e76fbcf..1858e236fe 100644
--- a/xen/lib/xxhash64.c
+++ b/xen/lib/xxhash64.c
@@ -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
/*-*************************************
--
2.35.3
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures
2023-12-12 16:27 ` [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures Juergen Gross
@ 2023-12-12 16:47 ` Jan Beulich
0 siblings, 0 replies; 6+ messages in thread
From: Jan Beulich @ 2023-12-12 16:47 UTC (permalink / raw)
To: Juergen Gross
Cc: Wei Liu, Anthony PERARD, Andrew Cooper, George Dunlap,
Julien Grall, Stefano Stabellini, Arnd Bergmann, xen-devel
On 12.12.2023 17:27, Juergen Gross wrote:
> 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.
>
> Modify the Linux variant to not use underscore prefixed identifiers,
> avoid unneeded parentheses and drop the 24-bit accessors.
>
> Add the definition of __packed to xg_dom_decompress_unsafe_zstd.c in
> libxenguest as it is using a cruel hack to reuse the hypervisor's
> decompressing code for stubdom.
>
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> Origin: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 803f4e1eab7a
> Signed-off-by: Juergen Gross <jgross@suse.com>
Acked-by: Jan Beulich <jbeulich@suse.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 0/2] xen: have a more generic unaligned.h header (take 2)
2023-12-12 16:27 [PATCH 0/2] xen: have a more generic unaligned.h header (take 2) Juergen Gross
2023-12-12 16:27 ` [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures Juergen Gross
2023-12-12 16:27 ` [PATCH 2/2] xen: remove asm/unaligned.h Juergen Gross
@ 2023-12-21 9:37 ` Juergen Gross
2023-12-21 9:38 ` Andrew Cooper
2 siblings, 1 reply; 6+ messages in thread
From: Juergen Gross @ 2023-12-21 9:37 UTC (permalink / raw)
To: xen-devel
Cc: Wei Liu, Anthony PERARD, Andrew Cooper, George Dunlap,
Jan Beulich, Julien Grall, Stefano Stabellini,
Roger Pau Monné
[-- Attachment #1.1.1: Type: text/plain, Size: 1169 bytes --]
On 12.12.23 17:27, Juergen Gross wrote:
> Second try for the generic unaligned.h approach.
>
> This time including a fix for building stubdom with libxenguest,
> which is using a cruel hack to reuse the hypervisor's decompressing
> code.
>
> Juergen Gross (2):
> xen: make include/xen/unaligned.h usable on all architectures
> xen: remove asm/unaligned.h
>
> .../guest/xg_dom_decompress_unsafe_zstd.c | 2 +-
> 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 | 53 +++++++++++--------
> xen/lib/xxhash32.c | 2 +-
> xen/lib/xxhash64.c | 2 +-
> 10 files changed, 38 insertions(+), 37 deletions(-)
> delete mode 100644 xen/arch/x86/include/asm/unaligned.h
>
Is anything missing for this series to go in?
Juergen
[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3743 bytes --]
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 0/2] xen: have a more generic unaligned.h header (take 2)
2023-12-21 9:37 ` [PATCH 0/2] xen: have a more generic unaligned.h header (take 2) Juergen Gross
@ 2023-12-21 9:38 ` Andrew Cooper
0 siblings, 0 replies; 6+ messages in thread
From: Andrew Cooper @ 2023-12-21 9:38 UTC (permalink / raw)
To: Juergen Gross, xen-devel
Cc: Wei Liu, Anthony PERARD, George Dunlap, Jan Beulich, Julien Grall,
Stefano Stabellini, Roger Pau Monné
On 21/12/2023 9:37 am, Juergen Gross wrote:
> On 12.12.23 17:27, Juergen Gross wrote:
>> Second try for the generic unaligned.h approach.
>>
>> This time including a fix for building stubdom with libxenguest,
>> which is using a cruel hack to reuse the hypervisor's decompressing
>> code.
>>
>> Juergen Gross (2):
>> xen: make include/xen/unaligned.h usable on all architectures
>> xen: remove asm/unaligned.h
>>
>> .../guest/xg_dom_decompress_unsafe_zstd.c | 2 +-
>> 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 | 53 +++++++++++--------
>> xen/lib/xxhash32.c | 2 +-
>> xen/lib/xxhash64.c | 2 +-
>> 10 files changed, 38 insertions(+), 37 deletions(-)
>> delete mode 100644 xen/arch/x86/include/asm/unaligned.h
>>
>
> Is anything missing for this series to go in?
Oh - I'd not spotted it. Lemme throw it through a full Gitlab CI run
just to be sure.
~Andrew
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2023-12-21 9:38 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-12 16:27 [PATCH 0/2] xen: have a more generic unaligned.h header (take 2) Juergen Gross
2023-12-12 16:27 ` [PATCH 1/2] xen: make include/xen/unaligned.h usable on all architectures Juergen Gross
2023-12-12 16:47 ` Jan Beulich
2023-12-12 16:27 ` [PATCH 2/2] xen: remove asm/unaligned.h Juergen Gross
2023-12-21 9:37 ` [PATCH 0/2] xen: have a more generic unaligned.h header (take 2) Juergen Gross
2023-12-21 9:38 ` Andrew Cooper
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.