Skip to contentSkip to conversation

Cleanup unaligned access macros

PATCH In review Jens Wiklander 4 participants 12 messages May 22, 2023 – May 28, 2023

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

cover Cleanup unaligned access macros

Hi,

There are two versions of get/set_unaligned, get_unaligned_be64, put_unaligned_le64 etc in U-Boot causing confusion (and bugs).

In this patch-set, I'm trying to fix that with a single unified version of the access macros to be used across all archs. This work is inspired by similar changes in this Linux kernel by Arnd Bergman,
https://lore.kernel.org/lkml/[email protected]/

Thanks,
Jens

Jens Wiklander (8):
arm: use asm-generic/unaligned.h
sh: use asm-generic/unaligned.h
mips: use asm-generic/unaligned.h
m68k: use asm-generic/unaligned.h
powerpc: use asm-generic/unaligned.h
fs/btrfs: use asm/unaligned.h
linux/unaligned: remove unused access_ok.h
asm-generic: simplify unaligned.h

 arch/arm/include/asm/unaligned.h     | 21 +------
 arch/m68k/include/asm/unaligned.h    | 17 +-----
 arch/mips/include/asm/unaligned.h    | 23 +------
 arch/powerpc/include/asm/unaligned.h | 18 +-----
 arch/sh/include/asm/unaligned.h      | 22 +------
 fs/btrfs/crypto/hash.c               |  2 +-
 include/asm-generic/unaligned.h      | 89 +++++++++++++++++++++++-----
 include/linux/unaligned/access_ok.h  | 66 ---------------------
 8 files changed, 83 insertions(+), 175 deletions(-)
 delete mode 100644 include/linux/unaligned/access_ok.h
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/8 arm: use asm-generic/unaligned.h

Arm duplicates the content of asm-generic/unaligned.h, so use that file directly instead.

---
 arch/arm/include/asm/unaligned.h | 21 ++-------------------
 1 file changed, 2 insertions(+), 19 deletions(-)
arch/arm/include/asm/unaligned.h +2 -19
index 0a228fb8eea8..7fb482abc383 100644
@@ -1,19 +1,2 @@
-#ifndef _ASM_ARM_UNALIGNED_H
-#define _ASM_ARM_UNALIGNED_H
-
-#include <linux/unaligned/le_byteshift.h>
-#include <linux/unaligned/be_byteshift.h>
-#include <linux/unaligned/generic.h>
-
-/*
- * Select endianness
- */
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-#define get_unaligned	__get_unaligned_le
-#define put_unaligned	__put_unaligned_le
-#else
-#define get_unaligned	__get_unaligned_be
-#define put_unaligned	__put_unaligned_be
-#endif
-
-#endif /* _ASM_ARM_UNALIGNED_H */
+/* SPDX-License-Identifier: GPL-2.0 */
+#include <asm-generic/unaligned.h>
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/8 sh: use asm-generic/unaligned.h

In reply to Jens Wiklander

Sh essentially duplicates the content of asm-generic/unaligned.h, so use that file directly instead.

---
 arch/sh/include/asm/unaligned.h | 22 ++--------------------
 1 file changed, 2 insertions(+), 20 deletions(-)
arch/sh/include/asm/unaligned.h +2 -20
index 5acf0819620e..7fb482abc383 100644
@@ -1,20 +1,2 @@
-#ifndef _ASM_SH_UNALIGNED_H
-#define _ASM_SH_UNALIGNED_H
-
-/* Copy from linux-kernel. */
-
-/* Other than SH4A, SH can't handle unaligned accesses. */
-#include <linux/compiler.h>
-#if defined(__BIG_ENDIAN__)
-#define get_unaligned   __get_unaligned_be
-#define put_unaligned   __put_unaligned_be
-#elif defined(__LITTLE_ENDIAN__)
-#define get_unaligned   __get_unaligned_le
-#define put_unaligned   __put_unaligned_le
-#endif
-
-#include <linux/unaligned/le_byteshift.h>
-#include <linux/unaligned/be_byteshift.h>
-#include <linux/unaligned/generic.h>
-
-#endif /* _ASM_SH_UNALIGNED_H */
+/* SPDX-License-Identifier: GPL-2.0 */
+#include <asm-generic/unaligned.h>
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/8 mips: use asm-generic/unaligned.h

In reply to Jens Wiklander

Mips essentially duplicates the content of asm-generic/unaligned.h, so use that file directly instead.

---
 arch/mips/include/asm/unaligned.h | 23 +----------------------
 1 file changed, 1 insertion(+), 22 deletions(-)
arch/mips/include/asm/unaligned.h +1 -22
index debb9cf7aba6..7fb482abc383 100644
@@ -1,23 +1,2 @@
 /* SPDX-License-Identifier: GPL-2.0 */
-/*
- * Copyright (C) 2007 Ralf Baechle ([email protected])
- */
-#ifndef _ASM_MIPS_UNALIGNED_H
-#define _ASM_MIPS_UNALIGNED_H
-
-#include <linux/compiler.h>
-#if defined(__MIPSEB__)
-#define get_unaligned	__get_unaligned_be
-#define put_unaligned	__put_unaligned_be
-#elif defined(__MIPSEL__)
-#define get_unaligned	__get_unaligned_le
-#define put_unaligned	__put_unaligned_le
-#else
-#error "MIPS, but neither __MIPSEB__, nor __MIPSEL__???"
-#endif
-
-#include <linux/unaligned/le_byteshift.h>
-#include <linux/unaligned/be_byteshift.h>
-#include <linux/unaligned/generic.h>
-
-#endif /* _ASM_MIPS_UNALIGNED_H */
+#include <asm-generic/unaligned.h>
Signature · 1 line
2.34.1
#4 lore raw

PATCH 4/8 m68k: use asm-generic/unaligned.h

In reply to Jens Wiklander

M68k essentially duplicates the content of asm-generic/unaligned.h, with an exception for non-Coldfire configurations. Coldfire configurations are apparently able to do unaligned accesses. But in an attempt to clean up and handle unaligned accesses in the same way we ignore that and use the common asm-generic/unaligned.h directly instead.

---
 arch/m68k/include/asm/unaligned.h | 17 ++---------------
 1 file changed, 2 insertions(+), 15 deletions(-)
arch/m68k/include/asm/unaligned.h +2 -15
index 328aa0c316c9..7fb482abc383 100644
@@ -1,15 +1,2 @@
-#ifndef _ASM_M68K_UNALIGNED_H
-#define _ASM_M68K_UNALIGNED_H
-
-#ifdef CONFIG_COLDFIRE
-#include <linux/unaligned/be_byteshift.h>
-#else
-#include <linux/unaligned/access_ok.h>
-#endif
-
-#include <linux/unaligned/generic.h>
-
-#define get_unaligned	__get_unaligned_be
-#define put_unaligned	__put_unaligned_be
-
-#endif /* _ASM_M68K_UNALIGNED_H */
+/* SPDX-License-Identifier: GPL-2.0 */
+#include <asm-generic/unaligned.h>
Signature · 1 line
2.34.1
Acked-by on v1 4/8
#5 lore raw

Re: [PATCH 4/8] m68k: use asm-generic/unaligned.h

Hi,

Quoted 23 lines · Jens Wiklander

M68k essentially duplicates the content of asm-generic/unaligned.h, with an exception for non-Coldfire configurations. Coldfire configurations are apparently able to do unaligned accesses. But in an attempt to clean up and handle unaligned accesses in the same way we ignore that and use the common asm-generic/unaligned.h directly instead.

---
  arch/m68k/include/asm/unaligned.h | 17 ++---------------
  1 file changed, 2 insertions(+), 15 deletions(-)
arch/m68k/include/asm/unaligned.h +0 -7
index 328aa0c316c9..7fb482abc383 100644
@@ -1,15 +1,2 @@
-#ifndef _ASM_M68K_UNALIGNED_H
-#define _ASM_M68K_UNALIGNED_H
-
-#ifdef CONFIG_COLDFIRE
-#include <linux/unaligned/be_byteshift.h>
-#else
-#include <linux/unaligned/access_ok.h>

arch/m68k/include/asm/unaligned.h line 2

Quoted 10 lines
arch/m68k/include/asm/unaligned.h +2 -8
-#endif
-
-#include <linux/unaligned/generic.h>
-
-#define get_unaligned	__get_unaligned_be
-#define put_unaligned	__put_unaligned_be
-
-#endif /* _ASM_M68K_UNALIGNED_H */
+/* SPDX-License-Identifier: GPL-2.0 */
+#include <asm-generic/unaligned.h>

sorry if a bit late,
i tested this on coldfire mcf54415, no issues, so ok for me.

Regards,
angelo

#6 lore raw

PATCH 5/8 powerpc: use asm-generic/unaligned.h

In reply to Jens Wiklander

Powerpc configurations are apparently able to do unaligned accesses. But in an attempt to clean up and handle unaligned accesses in the same way we ignore that and use the common asm-generic/unaligned.h directly instead.

---
 arch/powerpc/include/asm/unaligned.h | 18 ++----------------
 1 file changed, 2 insertions(+), 16 deletions(-)
arch/powerpc/include/asm/unaligned.h +2 -16
index 5f1b1e3c2137..7fb482abc383 100644
@@ -1,16 +1,2 @@
-#ifndef _ASM_POWERPC_UNALIGNED_H
-#define _ASM_POWERPC_UNALIGNED_H
-
-#ifdef __KERNEL__
-
-/*
- * The PowerPC can do unaligned accesses itself in big endian mode.
- */
-#include <linux/unaligned/access_ok.h>
-#include <linux/unaligned/generic.h>
-
-#define get_unaligned	__get_unaligned_be
-#define put_unaligned	__put_unaligned_be
-
-#endif	/* __KERNEL__ */
-#endif	/* _ASM_POWERPC_UNALIGNED_H */
+/* SPDX-License-Identifier: GPL-2.0 */
+#include <asm-generic/unaligned.h>
Signature · 1 line
2.34.1
#7 lore raw

PATCH 6/8 fs/btrfs: use asm/unaligned.h

In reply to Jens Wiklander

Use asm/unaligned.h instead of linux/unaligned/access_ok.h for unaligned access. This is needed on architectures that doesn't handle unaligned accesses directly.

---
 fs/btrfs/crypto/hash.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/crypto/hash.c +1 -1
index 891a2974be05..0a0b35fe9b96 100644
@@ -1,7 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0+
 
+#include <asm/unaligned.h>
 #include <linux/xxhash.h>
-#include <linux/unaligned/access_ok.h>
 #include <linux/types.h>
 #include <u-boot/sha256.h>
 #include <u-boot/blake2.h>
Signature · 1 line
2.34.1
#8 lore raw

PATCH 7/8 linux/unaligned: remove unused access_ok.h

In reply to Jens Wiklander

linux/unaligned/access_ok.h is unused, so remove it.

---
 include/linux/unaligned/access_ok.h | 66 -----------------------------
 1 file changed, 66 deletions(-)
 delete mode 100644 include/linux/unaligned/access_ok.h
include/linux/unaligned/access_ok.h deleted +0 -66
deleted file mode 100644
index 5f46eee23c38..000000000000
@@ -1,66 +0,0 @@
-#ifndef _LINUX_UNALIGNED_ACCESS_OK_H
-#define _LINUX_UNALIGNED_ACCESS_OK_H
-
-#include <asm/byteorder.h>
-
-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 */
Signature · 1 line
2.34.1
#9 lore raw

PATCH 8/8 asm-generic: simplify unaligned.h

In reply to Jens Wiklander

The get_unaligned()/put_unaligned() implementations are more complex than necessary.

Move everything into one file and use a more compact implementation based on packed struct access and byte swapping macros.

This patch is based on the Linux kernel commit 803f4e1eab7a ("asm-generic: simplify asm/unaligned.h") by Arnd Bergmann.

---
 include/asm-generic/unaligned.h | 89 +++++++++++++++++++++++++++------
 1 file changed, 73 insertions(+), 16 deletions(-)
include/asm-generic/unaligned.h +73 -16
index 3d33a5a063e8..9e5d93ec3041 100644
@@ -1,24 +1,81 @@
+/* SPDX-License-Identifier: GPL-2.0 */
 #ifndef _GENERIC_UNALIGNED_H
 #define _GENERIC_UNALIGNED_H
 
 #include <asm/byteorder.h>
 
-#include <linux/unaligned/le_byteshift.h>
-#include <linux/unaligned/be_byteshift.h>
-#include <linux/unaligned/generic.h>
-
-/*
- * Select endianness
- */
-#if defined(__LITTLE_ENDIAN)
-#define get_unaligned	__get_unaligned_le
-#define put_unaligned	__put_unaligned_le
-#elif defined(__BIG_ENDIAN)
-#define get_unaligned	__get_unaligned_be
-#define put_unaligned	__put_unaligned_be
-#else
-#error invalid endian
-#endif
+#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 u64 get_unaligned_be64(const void *p)
+{
+	return be64_to_cpu(__get_unaligned_t(__be64, p));
+}
+
+static inline void put_unaligned_be16(u16 val, void *p)
+{
+	__put_unaligned_t(__be16, cpu_to_be16(val), p);
+}
+
+static inline void put_unaligned_be32(u32 val, void *p)
+{
+	__put_unaligned_t(__be32, cpu_to_be32(val), p);
+}
+
+static inline void put_unaligned_be64(u64 val, void *p)
+{
+	__put_unaligned_t(__be64, cpu_to_be64(val), p);
+}
 
 /* Allow unaligned memory access */
 void allow_unaligned(void);
Signature · 1 line
2.34.1
Reviewed-by on the v1 series
#10 lore raw
In reply to Jens Wiklander

Hi Jens

Quoted 5 lines · Jens Wiklander

Hi,

There are two versions of get/set_unaligned, get_unaligned_be64, put_unaligned_le64 etc in U-Boot causing confusion (and bugs).

Quoted 7 lines

In this patch-set, I'm trying to fix that with a single unified version of the access macros to be used across all archs. This work is inspired by similar changes in this Linux kernel by Arnd Bergman,
https://lore.kernel.org/lkml/[email protected]/

Thanks,
Jens

Thanks for the cleanup.

For the series

Although I'd like to hear from arch maintainers as well.

Tom, This did pass all the CI successfully, but regardless I think it should be pulled into -next. If you want me to pick it up via the TPM tree please let me know.

Thanks
/Ilias

Quoted 25 lines

Jens Wiklander (8):
arm: use asm-generic/unaligned.h
sh: use asm-generic/unaligned.h
mips: use asm-generic/unaligned.h
m68k: use asm-generic/unaligned.h
powerpc: use asm-generic/unaligned.h
fs/btrfs: use asm/unaligned.h
linux/unaligned: remove unused access_ok.h
asm-generic: simplify unaligned.h

 arch/arm/include/asm/unaligned.h     | 21 +------
 arch/m68k/include/asm/unaligned.h    | 17 +-----
 arch/mips/include/asm/unaligned.h    | 23 +------
 arch/powerpc/include/asm/unaligned.h | 18 +-----
 arch/sh/include/asm/unaligned.h      | 22 +------
 fs/btrfs/crypto/hash.c               |  2 +-
 include/asm-generic/unaligned.h      | 89 +++++++++++++++++++++++-----
 include/linux/unaligned/access_ok.h  | 66 ---------------------
 8 files changed, 83 insertions(+), 175 deletions(-)
 delete mode 100644 include/linux/unaligned/access_ok.h

--
2.34.1

#11 lore raw
Quoted 18 lines · Ilias Apalodimas

Hi Jens

Quoted 5 lines · Jens Wiklander

Hi,

There are two versions of get/set_unaligned, get_unaligned_be64, put_unaligned_le64 etc in U-Boot causing confusion (and bugs).

Quoted 7 lines

In this patch-set, I'm trying to fix that with a single unified version of the access macros to be used across all archs. This work is inspired by similar changes in this Linux kernel by Arnd Bergman,
https://lore.kernel.org/lkml/[email protected]/

Thanks,
Jens

Thanks for the cleanup.

Quoted 7 lines

For the series

Although I'd like to hear from arch maintainers as well.

Tom, This did pass all the CI successfully, but regardless I think it should be pulled into -next. If you want me to pick it up via the TPM tree please let me know.

I'll pick this up for -next after it's been around a little bit longer, to let people test / ack it, thanks.

Signature · 1 line
Tom