Unify asm/unaligned.h around struct helper
Subsystems (21)
cover Unify asm/unaligned.h around struct helper
From: Arnd Bergmann <[email protected]>
The get_unaligned()/put_unaligned() helpers are traditionally architecture specific, with the two main variants being the "access-ok.h" version that assumes unaligned pointer accesses always work on a particular architecture, and the "le-struct.h" version that casts the data to a byte aligned type before dereferencing, for architectures that cannot always do unaligned accesses in hardware.
Based on the discussion linked below, it appears that the access-ok version is not realiable on any architecture, but the struct version probably has no downsides. This series changes the code to use the same implementation on all architectures, addressing the few exceptions separately.
I've pushed the patches to the asm-generic git tree for testing.
Arnd
Arnd Bergmann (12):
asm-generic: use asm-generic/unaligned.h for most architectures
openrisc: always use unaligned-struct header
sh: remove unaligned access for sh4a
m68k: select CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
powerpc: use linux/unaligned/le_struct.h on LE power7
asm-generic: unaligned: remove byteshift helpers
asm-generic: unaligned always use struct helpers
partitions: msdos: fix one-byte get_unaligned()
apparmor: use get_unaligned() only for multi-byte words
mwifiex: re-fix for unaligned accesses
netpoll: avoid put_unaligned() on single character
asm-generic: simplify asm/unaligned.h
arch/alpha/include/asm/unaligned.h | 12 -- arch/arm/include/asm/unaligned.h | 27 --- arch/ia64/include/asm/unaligned.h | 12 -- arch/m68k/Kconfig | 1 + arch/m68k/include/asm/unaligned.h | 26 --- arch/microblaze/include/asm/unaligned.h | 27 --- arch/mips/crypto/crc32-mips.c | 2 +- arch/openrisc/include/asm/unaligned.h | 47 ----- arch/parisc/include/asm/unaligned.h | 6 +- arch/powerpc/include/asm/unaligned.h | 22 --- arch/sh/include/asm/unaligned-sh4a.h | 199 -------------------- arch/sh/include/asm/unaligned.h | 13 -- arch/sparc/include/asm/unaligned.h | 11 -- arch/x86/include/asm/unaligned.h | 15 -- arch/xtensa/include/asm/unaligned.h | 29 --- block/partitions/ldm.h | 2 +- block/partitions/msdos.c | 2 +- drivers/net/wireless/marvell/mwifiex/pcie.c | 10 +- include/asm-generic/unaligned.h | 149 ++++++++++++--- include/linux/unaligned/access_ok.h | 68 ------- include/linux/unaligned/be_byteshift.h | 71 ------- include/linux/unaligned/be_memmove.h | 37 ---- include/linux/unaligned/be_struct.h | 37 ---- include/linux/unaligned/generic.h | 115 ----------- include/linux/unaligned/le_byteshift.h | 71 ------- include/linux/unaligned/le_memmove.h | 37 ---- include/linux/unaligned/le_struct.h | 37 ---- include/linux/unaligned/memmove.h | 46 ----- net/core/netpoll.c | 4 +- security/apparmor/policy_unpack.c | 2 +- 30 files changed, 137 insertions(+), 1000 deletions(-) delete mode 100644 arch/alpha/include/asm/unaligned.h delete mode 100644 arch/arm/include/asm/unaligned.h delete mode 100644 arch/ia64/include/asm/unaligned.h delete mode 100644 arch/m68k/include/asm/unaligned.h delete mode 100644 arch/microblaze/include/asm/unaligned.h delete mode 100644 arch/openrisc/include/asm/unaligned.h delete mode 100644 arch/powerpc/include/asm/unaligned.h delete mode 100644 arch/sh/include/asm/unaligned-sh4a.h delete mode 100644 arch/sh/include/asm/unaligned.h delete mode 100644 arch/sparc/include/asm/unaligned.h delete mode 100644 arch/x86/include/asm/unaligned.h delete mode 100644 arch/xtensa/include/asm/unaligned.h delete mode 100644 include/linux/unaligned/access_ok.h delete mode 100644 include/linux/unaligned/be_byteshift.h delete mode 100644 include/linux/unaligned/be_memmove.h delete mode 100644 include/linux/unaligned/be_struct.h delete mode 100644 include/linux/unaligned/generic.h delete mode 100644 include/linux/unaligned/le_byteshift.h delete mode 100644 include/linux/unaligned/le_memmove.h delete mode 100644 include/linux/unaligned/le_struct.h delete mode 100644 include/linux/unaligned/memmove.h
Signature · 1 line
2.29.2
RFC 1/12 asm-generic: use asm-generic/unaligned.h for most architectures
From: Arnd Bergmann <[email protected]>
There are several architectures that just duplicate the contents of asm-generic/unaligned.h, so change those over to use the file directly, to make future modifications easier.
The exceptions are:
- arm32 sets HAVE_EFFICIENT_UNALIGNED_ACCESS, but wants the unaligned-struct version
- ppc64le disables HAVE_EFFICIENT_UNALIGNED_ACCESS but includes the access-ok version
- m68k (non-dragonball) also uses the access-ok version without setting HAVE_EFFICIENT_UNALIGNED_ACCESS.
- sh4a has a custom inline asm version
- openrisc is the only one using the memmove version that generally leads to worse code.
--- arch/alpha/include/asm/unaligned.h | 12 ---------- arch/ia64/include/asm/unaligned.h | 12 ---------- arch/m68k/include/asm/unaligned.h | 9 +------- arch/microblaze/include/asm/unaligned.h | 27 ----------------------- arch/parisc/include/asm/unaligned.h | 6 +---- arch/sparc/include/asm/unaligned.h | 11 ---------- arch/x86/include/asm/unaligned.h | 15 ------------- arch/xtensa/include/asm/unaligned.h | 29 ------------------------- 8 files changed, 2 insertions(+), 119 deletions(-) delete mode 100644 arch/alpha/include/asm/unaligned.h delete mode 100644 arch/ia64/include/asm/unaligned.h delete mode 100644 arch/microblaze/include/asm/unaligned.h delete mode 100644 arch/sparc/include/asm/unaligned.h delete mode 100644 arch/x86/include/asm/unaligned.h delete mode 100644 arch/xtensa/include/asm/unaligned.h
arch/alpha/include/asm/unaligned.h deleted +0 -12
@@ -1,12 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _ASM_ALPHA_UNALIGNED_H -#define _ASM_ALPHA_UNALIGNED_H - -#include <linux/unaligned/le_struct.h> -#include <linux/unaligned/be_byteshift.h> -#include <linux/unaligned/generic.h> - -#define get_unaligned __get_unaligned_le -#define put_unaligned __put_unaligned_le - -#endif /* _ASM_ALPHA_UNALIGNED_H */
arch/ia64/include/asm/unaligned.h deleted +0 -12
@@ -1,12 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _ASM_IA64_UNALIGNED_H -#define _ASM_IA64_UNALIGNED_H - -#include <linux/unaligned/le_struct.h> -#include <linux/unaligned/be_byteshift.h> -#include <linux/unaligned/generic.h> - -#define get_unaligned __get_unaligned_le -#define put_unaligned __put_unaligned_le - -#endif /* _ASM_IA64_UNALIGNED_H */
arch/m68k/include/asm/unaligned.h +1 -8
@@ -2,15 +2,8 @@#ifndef _ASM_M68K_UNALIGNED_H#define _ASM_M68K_UNALIGNED_H -#ifdef CONFIG_CPU_HAS_NO_UNALIGNED -#include <linux/unaligned/be_struct.h> -#include <linux/unaligned/le_byteshift.h> -#include <linux/unaligned/generic.h> - -#define get_unaligned __get_unaligned_be -#define put_unaligned __put_unaligned_be - +#include <asm-generic/unaligned.h> #else /* * The m68k can do unaligned accesses itself.
arch/microblaze/include/asm/unaligned.h deleted +0 -27
@@ -1,27 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -/* -* Copyright (C) 2008 Michal Simek <[email protected]> -* Copyright (C) 2006 Atmark Techno, Inc. -*/ - -#ifndef _ASM_MICROBLAZE_UNALIGNED_H -#define _ASM_MICROBLAZE_UNALIGNED_H - -# ifdef __KERNEL__ - -# ifdef __MICROBLAZEEL__ -# include <linux/unaligned/le_struct.h> -# include <linux/unaligned/be_byteshift.h> -# define get_unaligned __get_unaligned_le -# define put_unaligned __put_unaligned_le -# else -# include <linux/unaligned/be_struct.h> -# include <linux/unaligned/le_byteshift.h> -# define get_unaligned __get_unaligned_be -# define put_unaligned __put_unaligned_be -# endif - -# include <linux/unaligned/generic.h> - -# endif /* __KERNEL__ */ -#endif /* _ASM_MICROBLAZE_UNALIGNED_H */
arch/parisc/include/asm/unaligned.h +1 -5
@@ -2,11 +2,7 @@#ifndef _ASM_PARISC_UNALIGNED_H#define _ASM_PARISC_UNALIGNED_H -#include <linux/unaligned/be_struct.h> -#include <linux/unaligned/le_byteshift.h> -#include <linux/unaligned/generic.h> -#define get_unaligned __get_unaligned_be -#define put_unaligned __put_unaligned_be +#include <asm-generic/unaligned.h> #ifdef __KERNEL__struct pt_regs ;
arch/sparc/include/asm/unaligned.h deleted +0 -11
@@ -1,11 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _ASM_SPARC_UNALIGNED_H -#define _ASM_SPARC_UNALIGNED_H - -#include <linux/unaligned/be_struct.h> -#include <linux/unaligned/le_byteshift.h> -#include <linux/unaligned/generic.h> -#define get_unaligned __get_unaligned_be -#define put_unaligned __put_unaligned_be - -#endif /* _ASM_SPARC_UNALIGNED_H */
arch/x86/include/asm/unaligned.h deleted +0 -15
@@ -1,15 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _ASM_X86_UNALIGNED_H -#define _ASM_X86_UNALIGNED_H - -/* -* The x86 can do unaligned accesses itself. -*/ - -#include <linux/unaligned/access_ok.h> -#include <linux/unaligned/generic.h> - -#define get_unaligned __get_unaligned_le -#define put_unaligned __put_unaligned_le - -#endif /* _ASM_X86_UNALIGNED_H */
arch/xtensa/include/asm/unaligned.h deleted +0 -29
@@ -1,29 +0,0 @@ -/* -* Xtensa doesn't handle unaligned accesses efficiently. -* -* This file is subject to the terms and conditions of the GNU General Public -* License. See the file "COPYING" in the main directory of this archive -* for more details. -* -* Copyright (C) 2001 - 2005 Tensilica Inc. -*/ -#ifndef _ASM_XTENSA_UNALIGNED_H -#define _ASM_XTENSA_UNALIGNED_H - -#include <asm/byteorder.h> - -#ifdef __LITTLE_ENDIAN -# include <linux/unaligned/le_struct.h> -# include <linux/unaligned/be_byteshift.h> -# include <linux/unaligned/generic.h> -# define get_unaligned __get_unaligned_le -# define put_unaligned __put_unaligned_le -#else -# include <linux/unaligned/be_struct.h> -# include <linux/unaligned/le_byteshift.h> -# include <linux/unaligned/generic.h> -# define get_unaligned __get_unaligned_be -# define put_unaligned __put_unaligned_be -#endif - -#endif /* _ASM_XTENSA_UNALIGNED_H */
Signature · 1 line
2.29.2
Re: [RFC 01/12] asm-generic: use asm-generic/unaligned.h for most architectures
Quoted 11 lines · Arnd Bergmann
arch/x86/include/asm/unaligned.h deleted +0
-5@@ -1,15 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _ASM_X86_UNALIGNED_H -#define _ASM_X86_UNALIGNED_H - -/*
arch/x86/include/asm/unaligned.h line 15
Quoted 10 lines
arch/x86/include/asm/unaligned.h deleted +0
-10-* The x86 can do unaligned accesses itself. -*/ - -#include <linux/unaligned/access_ok.h> -#include <linux/unaligned/generic.h> - -#define get_unaligned __get_unaligned_le -#define put_unaligned __put_unaligned_le - -#endif /* _ASM_X86_UNALIGNED_H */
Thanks for cleaning that up!
tglx
Re: [RFC 01/12] asm-generic: use asm-generic/unaligned.h for most architectures
In reply to Arnd BergmannHi Arnd,
Quoted 8 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
There are several architectures that just duplicate the contents of
asm-generic/unaligned.h, so change those over to use the file directly, to make future modifications easier.The exceptions are:
Quoted 8 lines
- arm32 sets
HAVE_EFFICIENT_UNALIGNED_ACCESS, but wants the unaligned-struct version- ppc64le disables
HAVE_EFFICIENT_UNALIGNED_ACCESSbut includes the access-ok version- m68k (non-dragonball) also uses the access-ok version without setting
HAVE_EFFICIENT_UNALIGNED_ACCESS.
This not only applies to dragonball, which has the CPU32 core, but also to plain 68000, and any SoCs including the 68EC000 core.
It also applies to early Coldfire, but AFAIK Linux doesn't support these (see dfe1d26d4a90287e ("m68knommu: Allow ColdFire CPUs to use unaligned
accesses")).
Quoted 8 lines
- sh4a has a custom inline asm version
- openrisc is the only one using the memmove version that generally leads to worse code.
Signed-off-by Arnd Bergmann <[email protected]>arch/m68k/include/asm/unaligned.h | 9 +-------
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [RFC 01/12] asm-generic: use asm-generic/unaligned.h for most architectures
Quoted 12 lines · Geert Uytterhoeven
Quoted 11 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
There are several architectures that just duplicate the contents of
asm-generic/unaligned.h, so change those over to use the file directly, to make future modifications easier.The exceptions are:
- arm32 sets
HAVE_EFFICIENT_UNALIGNED_ACCESS, but wants the unaligned-struct version
Quoted 8 lines
Quoted 5 lines
- ppc64le disables
HAVE_EFFICIENT_UNALIGNED_ACCESSbut includes the access-ok version- m68k (non-dragonball) also uses the access-ok version without setting
HAVE_EFFICIENT_UNALIGNED_ACCESS.This not only applies to dragonball, which has the CPU32 core, but also to plain 68000, and any SoCs including the 68EC000 core.
I meant out of the machines that are currently supported in the kernel. As far as I can tell, the only 68000 variants that are supported are all Dragonball CONFIG_M68328, CONFIG_M68EZ328 and CONFIG_M68VZ328.
CONFIG_MCPU32 has been dead since a3595962d824 ("m68knommu:
remove obsolete 68360 support")
Quoted 2 lines
Reviewed-by Geert Uytterhoeven <[email protected]>
Thanks!
Arnd
RFC 2/12 openrisc: always use unaligned-struct header
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
openrisc is the only architecture using the linux/unaligned/*memmove infrastructure. There is a comment saying that this version is more efficient, but this was added in 2011 before the openrisc gcc port was merged upstream.
I checked a couple of files to see what the actual difference is with the mainline gcc (9.4 and 11.1), and found that the generic header seems to produce better code now, regardless of the gcc version.
Specifically, the be_memmove leads to allocating a stack slot and copying the data one byte at a time, then reading the whole word from the stack:
00000000 <test_get_unaligned_memmove>:
0: 9c 21 ff f4 l.addi r1,r1,-12
4: d4 01 10 04 l.sw 4(r1),r2
8: 8e 63 00 00 l.lbz r19,0(r3)
c: 9c 41 00 0c l.addi r2,r1,12
10: 8e 23 00 01 l.lbz r17,1(r3)
14: db e2 9f f4 l.sb -12(r2),r19
18: db e2 8f f5 l.sb -11(r2),r17
1c: 8e 63 00 02 l.lbz r19,2(r3)
20: 8e 23 00 03 l.lbz r17,3(r3)
24: d4 01 48 08 l.sw 8(r1),r9
28: db e2 9f f6 l.sb -10(r2),r19
2c: db e2 8f f7 l.sb -9(r2),r17
30: 85 62 ff f4 l.lwz r11,-12(r2)
34: 85 21 00 08 l.lwz r9,8(r1)
38: 84 41 00 04 l.lwz r2,4(r1)
3c: 44 00 48 00 l.jr r9
40: 9c 21 00 0c l.addi r1,r1,12
while the be_struct version reads each byte into a register and does a shift to the right position:
00000000 <test_get_unaligned_struct>:
0: 9c 21 ff f8 l.addi r1,r1,-8
4: 8e 63 00 00 l.lbz r19,0(r3)
8: aa 20 00 18 l.ori r17,r0,0x18
c: e2 73 88 08 l.sll r19,r19,r17
10: 8d 63 00 01 l.lbz r11,1(r3)
14: aa 20 00 10 l.ori r17,r0,0x10
18: e1 6b 88 08 l.sll r11,r11,r17
1c: e1 6b 98 04 l.or r11,r11,r19
20: 8e 23 00 02 l.lbz r17,2(r3)
24: aa 60 00 08 l.ori r19,r0,0x8
28: e2 31 98 08 l.sll r17,r17,r19
2c: d4 01 10 00 l.sw 0(r1),r2
30: d4 01 48 04 l.sw 4(r1),r9
34: 9c 41 00 08 l.addi r2,r1,8
38: e2 31 58 04 l.or r17,r17,r11
3c: 8d 63 00 03 l.lbz r11,3(r3)
40: e1 6b 88 04 l.or r11,r11,r17
44: 84 41 00 00 l.lwz r2,0(r1)
48: 85 21 00 04 l.lwz r9,4(r1)
4c: 44 00 48 00 l.jr r9
50: 9c 21 00 08 l.addi r1,r1,8
I don't know how the loads/store perform compared to the shift version on a particular microarchitecture, but my guess is that the shifts are better.
In the trivial example, the struct version is a few instructions longer, but building a whole kernel shows an overall reduction in code size, presumably because it now has to manage fewer stack slots:
text data bss dec hex filename
4792010 181480 82324 5055814 4d2546 vmlinux-unaligned-memmove
4790642 181480 82324 5054446 4d1fee vmlinux-unaligned-struct
Remove the memmove version completely and let openrisc use the same code as everyone else, as a simplification.
--- arch/openrisc/include/asm/unaligned.h | 47 --------------------------- include/linux/unaligned/be_memmove.h | 37 --------------------- include/linux/unaligned/le_memmove.h | 37 --------------------- include/linux/unaligned/memmove.h | 46 -------------------------- 4 files changed, 167 deletions(-) delete mode 100644 arch/openrisc/include/asm/unaligned.h delete mode 100644 include/linux/unaligned/be_memmove.h delete mode 100644 include/linux/unaligned/le_memmove.h delete mode 100644 include/linux/unaligned/memmove.h
arch/openrisc/include/asm/unaligned.h deleted +0 -47
@@ -1,47 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* -* OpenRISC Linux -* -* Linux architectural port borrowing liberally from similar works of -* others. All original copyrights apply as per the original source -* declaration. -* -* OpenRISC implementation: -* Copyright (C) 2003 Matjaz Breskvar <[email protected]> -* Copyright (C) 2010-2011 Jonas Bonn <[email protected]> -* et al. -*/ - -#ifndef __ASM_OPENRISC_UNALIGNED_H -#define __ASM_OPENRISC_UNALIGNED_H - -/* -* This is copied from the generic implementation and the C-struct -* variant replaced with the memmove variant. The GCC compiler -* for the OR32 arch optimizes too aggressively for the C-struct -* variant to work, so use the memmove variant instead. -* -* It may be worth considering implementing the unaligned access -* exception handler and allowing unaligned accesses (access_ok.h)... -* not sure if it would be much of a performance win without further -* investigation. -*/ -#include <asm/byteorder.h> - -#if defined(__LITTLE_ENDIAN) -# include <linux/unaligned/le_memmove.h> -# include <linux/unaligned/be_byteshift.h> -# include <linux/unaligned/generic.h> -# define get_unaligned __get_unaligned_le -# define put_unaligned __put_unaligned_le -#elif defined(__BIG_ENDIAN) -# include <linux/unaligned/be_memmove.h> -# include <linux/unaligned/le_byteshift.h> -# include <linux/unaligned/generic.h> -# define get_unaligned __get_unaligned_be -# define put_unaligned __put_unaligned_be -#else -# error need to define endianess -#endif - -#endif /* __ASM_OPENRISC_UNALIGNED_H */
include/linux/unaligned/be_memmove.h deleted +0 -37
@@ -1,37 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _LINUX_UNALIGNED_BE_MEMMOVE_H -#define _LINUX_UNALIGNED_BE_MEMMOVE_H - -#include <linux/unaligned/memmove.h> - -static inline u16 get_unaligned_be16 (const void *p) -{ -return __get_unaligned_memmove16 ((const u8 *)p); -} - -static inline u32 get_unaligned_be32 (const void *p) -{ -return __get_unaligned_memmove32 ((const u8 *)p); -} - -static inline u64 get_unaligned_be64 (const void *p) -{ -return __get_unaligned_memmove64 ((const u8 *)p); -} - -static inline void put_unaligned_be16 (u16 val,void *p) -{ -__put_unaligned_memmove16 (val, p); -} - -static inline void put_unaligned_be32 (u32 val,void *p) -{ -__put_unaligned_memmove32 (val, p); -} - -static inline void put_unaligned_be64 (u64 val,void *p) -{ -__put_unaligned_memmove64 (val, p); -} - -#endif /* _LINUX_UNALIGNED_LE_MEMMOVE_H */
include/linux/unaligned/le_memmove.h deleted +0 -37
@@ -1,37 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _LINUX_UNALIGNED_LE_MEMMOVE_H -#define _LINUX_UNALIGNED_LE_MEMMOVE_H - -#include <linux/unaligned/memmove.h> - -static inline u16 get_unaligned_le16 (const void *p) -{ -return __get_unaligned_memmove16 ((const u8 *)p); -} - -static inline u32 get_unaligned_le32 (const void *p) -{ -return __get_unaligned_memmove32 ((const u8 *)p); -} - -static inline u64 get_unaligned_le64 (const void *p) -{ -return __get_unaligned_memmove64 ((const u8 *)p); -} - -static inline void put_unaligned_le16 (u16 val,void *p) -{ -__put_unaligned_memmove16 (val, p); -} - -static inline void put_unaligned_le32 (u32 val,void *p) -{ -__put_unaligned_memmove32 (val, p); -} - -static inline void put_unaligned_le64 (u64 val,void *p) -{ -__put_unaligned_memmove64 (val, p); -} - -#endif /* _LINUX_UNALIGNED_LE_MEMMOVE_H */
include/linux/unaligned/memmove.h deleted +0 -46
46 lines not on this page · raw message
Signature · 1 line
2.29.2
Re: [RFC 02/12] openrisc: always use unaligned-struct header
Quoted 61 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
openrisc is the only architecture using the linux/unaligned/*memmove infrastructure. There is a comment saying that this version is more efficient, but this was added in 2011 before the openrisc gcc port was merged upstream.
I checked a couple of files to see what the actual difference is with the mainline gcc (9.4 and 11.1), and found that the generic header seems to produce better code now, regardless of the gcc version.
Specifically, the
be_memmoveleads to allocating a stack slot and copying the data one byte at a time, then reading the whole word from the stack:00000000 <
test_get_unaligned_memmove>:0: 9c 21 ff f4 l.addi r1,r1,-12 4: d4 01 10 04 l.sw 4(r1),r2 8: 8e 63 00 00 l.lbz r19,0(r3) c: 9c 41 00 0c l.addi r2,r1,12 10: 8e 23 00 01 l.lbz r17,1(r3) 14: db e2 9f f4 l.sb -12(r2),r19 18: db e2 8f f5 l.sb -11(r2),r17 1c: 8e 63 00 02 l.lbz r19,2(r3) 20: 8e 23 00 03 l.lbz r17,3(r3) 24: d4 01 48 08 l.sw 8(r1),r9 28: db e2 9f f6 l.sb -10(r2),r19 2c: db e2 8f f7 l.sb -9(r2),r17 30: 85 62 ff f4 l.lwz r11,-12(r2) 34: 85 21 00 08 l.lwz r9,8(r1) 38: 84 41 00 04 l.lwz r2,4(r1) 3c: 44 00 48 00 l.jr r9 40: 9c 21 00 0c l.addi r1,r1,12while the
be_structversion reads each byte into a register and does a shift to the right position:00000000 <
test_get_unaligned_struct>:0: 9c 21 ff f8 l.addi r1,r1,-8 4: 8e 63 00 00 l.lbz r19,0(r3) 8: aa 20 00 18 l.ori r17,r0,0x18 c: e2 73 88 08 l.sll r19,r19,r17 10: 8d 63 00 01 l.lbz r11,1(r3) 14: aa 20 00 10 l.ori r17,r0,0x10 18: e1 6b 88 08 l.sll r11,r11,r17 1c: e1 6b 98 04 l.or r11,r11,r19 20: 8e 23 00 02 l.lbz r17,2(r3) 24: aa 60 00 08 l.ori r19,r0,0x8 28: e2 31 98 08 l.sll r17,r17,r19 2c: d4 01 10 00 l.sw 0(r1),r2 30: d4 01 48 04 l.sw 4(r1),r9 34: 9c 41 00 08 l.addi r2,r1,8 38: e2 31 58 04 l.or r17,r17,r11 3c: 8d 63 00 03 l.lbz r11,3(r3) 40: e1 6b 88 04 l.or r11,r11,r17 44: 84 41 00 00 l.lwz r2,0(r1) 48: 85 21 00 04 l.lwz r9,4(r1) 4c: 44 00 48 00 l.jr r9 50: 9c 21 00 08 l.addi r1,r1,8
Quoted 3 lines
I don't know how the loads/store perform compared to the shift version on a particular microarchitecture, but my guess is that the shifts are better.
Thanks for doing the investigation on this as well.
Load stores are slow like on most architectures. WIth caching it will be faster, but
still not faster than the shifts. So this looks good to me.
Quoted 7 lines
In the trivial example, the struct version is a few instructions longer, but building a whole kernel shows an overall reduction in code size, presumably because it now has to manage fewer stack slots:
text data bss dec hex filename
4792010 181480 82324 5055814 4d2546 vmlinux-unaligned-memmove 4790642 181480 82324 5054446 4d1fee vmlinux-unaligned-struct
That's a plus.
Quoted 4 lines
Remove the memmove version completely and let openrisc use the same code as everyone else, as a simplification.
Signed-off-by Arnd Bergmann <[email protected]>
Quoted 10 lines
--- arch/openrisc/include/asm/unaligned.h | 47 --------------------------- include/linux/unaligned/be_memmove.h | 37 --------------------- include/linux/unaligned/le_memmove.h | 37 --------------------- include/linux/unaligned/memmove.h | 46 -------------------------- 4 files changed, 167 deletions(-) delete mode 100644 arch/openrisc/include/asm/unaligned.h delete mode 100644 include/linux/unaligned/be_memmove.h delete mode 100644 include/linux/unaligned/le_memmove.h delete mode 100644 include/linux/unaligned/memmove.h
Thanks again,
-Stafford
RE: [RFC 02/12] openrisc: always use unaligned-struct header
In reply to Arnd BergmannFrom: Arnd Bergmann
Quoted 1 line
Sent: 07 May 2021 23:08
...
Quoted 3 lines
I don't know how the loads/store perform compared to the shift version on a particular microarchitecture, but my guess is that the shifts are better.
What does the nios use?
Shifts generate reasonable code for put_unaligned() but they get horrid for get_unaligned().
On the nios writing the 4 bytes to memory and reading back a 32bit value should generate shorter faster code.
You do need to generate 4 byte loads, 4 bytes stores, 32bit load. (The load will cause a stall if the data is needed for one of the next two instructions, and there is a (undocumented) stall between a write and read to the same memory area. The shift version requires 3 shifts and 3 ors - but I think gcc makes a bigger pig's breakfast of it.)
OTOH I'm not sure anyone in their right mind would run Linux on nios. It is a soft cpu for the altera (now intel) fpgas.
We use them with 4k code and sub 64k data for real time processing.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
cover Unify asm/unaligned.h around struct helper
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
The get_unaligned()/put_unaligned() helpers are traditionally architecture specific, with the two main variants being the "access-ok.h" version that assumes unaligned pointer accesses always work on a particular architecture, and the "le-struct.h" version that casts the data to a byte aligned type before dereferencing, for architectures that cannot always do unaligned accesses in hardware.
Based on the discussion linked below, it appears that the access-ok version is not realiable on any architecture, but the struct version probably has no downsides. This series changes the code to use the same implementation on all architectures, addressing the few exceptions separately.
I've pushed the patches to the asm-generic git tree for testing.
Arnd
Arnd Bergmann (12):
asm-generic: use asm-generic/unaligned.h for most architectures
openrisc: always use unaligned-struct header
sh: remove unaligned access for sh4a
m68k: select CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
powerpc: use linux/unaligned/le_struct.h on LE power7
asm-generic: unaligned: remove byteshift helpers
asm-generic: unaligned always use struct helpers
partitions: msdos: fix one-byte get_unaligned()
apparmor: use get_unaligned() only for multi-byte words
mwifiex: re-fix for unaligned accesses
netpoll: avoid put_unaligned() on single character
asm-generic: simplify asm/unaligned.h
arch/alpha/include/asm/unaligned.h | 12 -- arch/arm/include/asm/unaligned.h | 27 --- arch/ia64/include/asm/unaligned.h | 12 -- arch/m68k/Kconfig | 1 + arch/m68k/include/asm/unaligned.h | 26 --- arch/microblaze/include/asm/unaligned.h | 27 --- arch/mips/crypto/crc32-mips.c | 2 +- arch/openrisc/include/asm/unaligned.h | 47 ----- arch/parisc/include/asm/unaligned.h | 6 +- arch/powerpc/include/asm/unaligned.h | 22 --- arch/sh/include/asm/unaligned-sh4a.h | 199 -------------------- arch/sh/include/asm/unaligned.h | 13 -- arch/sparc/include/asm/unaligned.h | 11 -- arch/x86/include/asm/unaligned.h | 15 -- arch/xtensa/include/asm/unaligned.h | 29 --- block/partitions/ldm.h | 2 +- block/partitions/msdos.c | 2 +- drivers/net/wireless/marvell/mwifiex/pcie.c | 10 +- include/asm-generic/unaligned.h | 149 ++++++++++++--- include/linux/unaligned/access_ok.h | 68 ------- include/linux/unaligned/be_byteshift.h | 71 ------- include/linux/unaligned/be_memmove.h | 37 ---- include/linux/unaligned/be_struct.h | 37 ---- include/linux/unaligned/generic.h | 115 ----------- include/linux/unaligned/le_byteshift.h | 71 ------- include/linux/unaligned/le_memmove.h | 37 ---- include/linux/unaligned/le_struct.h | 37 ---- include/linux/unaligned/memmove.h | 46 ----- net/core/netpoll.c | 4 +- security/apparmor/policy_unpack.c | 2 +- 30 files changed, 137 insertions(+), 1000 deletions(-) delete mode 100644 arch/alpha/include/asm/unaligned.h delete mode 100644 arch/arm/include/asm/unaligned.h delete mode 100644 arch/ia64/include/asm/unaligned.h delete mode 100644 arch/m68k/include/asm/unaligned.h delete mode 100644 arch/microblaze/include/asm/unaligned.h delete mode 100644 arch/openrisc/include/asm/unaligned.h delete mode 100644 arch/powerpc/include/asm/unaligned.h delete mode 100644 arch/sh/include/asm/unaligned-sh4a.h delete mode 100644 arch/sh/include/asm/unaligned.h delete mode 100644 arch/sparc/include/asm/unaligned.h delete mode 100644 arch/x86/include/asm/unaligned.h delete mode 100644 arch/xtensa/include/asm/unaligned.h delete mode 100644 include/linux/unaligned/access_ok.h delete mode 100644 include/linux/unaligned/be_byteshift.h delete mode 100644 include/linux/unaligned/be_memmove.h delete mode 100644 include/linux/unaligned/be_struct.h delete mode 100644 include/linux/unaligned/generic.h delete mode 100644 include/linux/unaligned/le_byteshift.h delete mode 100644 include/linux/unaligned/le_memmove.h delete mode 100644 include/linux/unaligned/le_struct.h delete mode 100644 include/linux/unaligned/memmove.h
Signature · 1 line
2.29.2
RFC 3/12 sh: remove unaligned access for sh4a
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
Unlike every other architecture, sh4a uses an inline asm implementation for get_unaligned(). I have shown that this produces better object code than the asm-generic version. However, there are very few users of arch/sh/ overall, and most of those seem to use sh4 rather than sh4a CPU cores, so it seems not worth keeping the complexity in the architecture independent code.
Change over to the generic version to allow simplifying that in a follow-up patch.
If there are sh4a users that want the best performance, it would probably be best to add support for the movua instruction in gcc itself, as this would not just help get_unaligned() callers but any code that accesses a __packed variable in user space or kernel.
--- arch/sh/include/asm/unaligned-sh4a.h | 199 --------------------------- arch/sh/include/asm/unaligned.h | 13 -- 2 files changed, 212 deletions(-) delete mode 100644 arch/sh/include/asm/unaligned-sh4a.h delete mode 100644 arch/sh/include/asm/unaligned.h
arch/sh/include/asm/unaligned-sh4a.h deleted +0 -199
@@ -1,199 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef __ASM_SH_UNALIGNED_SH4A_H -#define __ASM_SH_UNALIGNED_SH4A_H - -/* -* SH-4A has support for unaligned 32-bit loads, and 32-bit loads only. -* Support for 64-bit accesses are done through shifting and masking -* relative to the endianness. Unaligned stores are not supported by the -* instruction encoding, so these continue to use the packed -* struct. -* -* The same note as with the movli.l/movco.l pair applies here, as long -* as the load is guaranteed to be inlined, nothing else will hook in to -* r0 and we get the return value for free. -* -* NOTE: Due to the fact we require r0 encoding, care should be taken to -* avoid mixing these heavily with other r0 consumers, such as the atomic -* ops. Failure to adhere to this can result in the compiler running out -* of spill registers and blowing up when building at low optimization -* levels. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34777. -*/ -#include <linux/unaligned/packed_struct.h> -#include <linux/types.h> -#include <asm/byteorder.h> - -static inline u16 sh4a_get_unaligned_cpu16 (const u8 *p) -{ -#ifdef __LITTLE_ENDIAN -return p[0 ] | p[1 ] <<8 ; -#else -return p[0 ] <<8 | p[1 ]; -#endif -} - -static __always_inline u32 sh4a_get_unaligned_cpu32(const u8 *p) -{ -unsigned long unaligned; - - __asm__ __volatile__ ( -"movua.l @%1, %0\n\t" - :"=z" (unaligned) - :"r" (p) - ); - -return unaligned; -} - -/* -* Even though movua.l supports auto-increment on the read side, it can -* only store to r0 due to instruction encoding constraints, so just let -* the compiler sort it out on its own. -*/ -static inline u64 sh4a_get_unaligned_cpu64 (const u8 *p) -{ -#ifdef __LITTLE_ENDIAN -return (u64 )sh4a_get_unaligned_cpu32 (p +4 ) <<32 | -sh4a_get_unaligned_cpu32 (p); -#else -return (u64 )sh4a_get_unaligned_cpu32 (p) <<32 | -sh4a_get_unaligned_cpu32 (p +4 ); -#endif -} - -static inline u16 get_unaligned_le16 (const void *p) -{ -return le16_to_cpu (sh4a_get_unaligned_cpu16 (p)); -} - -static inline u32 get_unaligned_le32 (const void *p) -{ -return le32_to_cpu (sh4a_get_unaligned_cpu32 (p)); -} - -static inline u64 get_unaligned_le64 (const void *p) -{ -return le64_to_cpu (sh4a_get_unaligned_cpu64 (p)); -} - -static inline u16 get_unaligned_be16 (const void *p) -{ -return be16_to_cpu (sh4a_get_unaligned_cpu16 (p)); -} - -static inline u32 get_unaligned_be32 (const void *p) -{ -return be32_to_cpu (sh4a_get_unaligned_cpu32 (p)); -} - -static inline u64 get_unaligned_be64 (const void *p) -{ -return be64_to_cpu (sh4a_get_unaligned_cpu64 (p)); -} - -static inline void nonnative_put_le16 (u16 val,u8 *p) -{ - *p++ = val; - *p++ = val >>8 ; -} - -static inline void nonnative_put_le32 (u32 val,u8 *p) -{ -nonnative_put_le16 (val, p); -nonnative_put_le16 (val >>16 , p +2 ); -} - -static inline void nonnative_put_le64 (u64 val,u8 *p) -{ -nonnative_put_le32 (val, p); -nonnative_put_le32 (val >>32 , p +4 ); -} - -static inline void nonnative_put_be16 (u16 val,u8 *p) -{ - *p++ = val >>8 ; - *p++ = val; -} - -static inline void nonnative_put_be32 (u32 val,u8 *p) -{ -nonnative_put_be16 (val >>16 , p); -nonnative_put_be16 (val, p +2 ); -} - -static inline void nonnative_put_be64 (u64 val,u8 *p) -{ -nonnative_put_be32 (val >>32 , p); -nonnative_put_be32 (val, p +4 ); -} - -static inline void put_unaligned_le16 (u16 val,void *p) -{ -#ifdef __LITTLE_ENDIAN -__put_unaligned_cpu16 (val, p); -#else -nonnative_put_le16 (val, p); -#endif -} - -static inline void put_unaligned_le32 (u32 val,void *p) -{ -#ifdef __LITTLE_ENDIAN -__put_unaligned_cpu32 (val, p); -#else -nonnative_put_le32 (val, p); -#endif -} - -static inline void put_unaligned_le64 (u64 val,void *p) -{ -#ifdef __LITTLE_ENDIAN -__put_unaligned_cpu64 (val, p); -#else -nonnative_put_le64 (val, p); -#endif -} - -static inline void put_unaligned_be16 (u16 val,void *p) -{ -#ifdef __BIG_ENDIAN -__put_unaligned_cpu16 (val, p); -#else -nonnative_put_be16 (val, p); -#endif -} - -static inline void put_unaligned_be32 (u32 val,void *p) -{ -#ifdef __BIG_ENDIAN -__put_unaligned_cpu32 (val, p); -#else -nonnative_put_be32 (val, p); -#endif -} - -static inline void put_unaligned_be64 (u64 val,void *p) -{ -#ifdef __BIG_ENDIAN -__put_unaligned_cpu64 (val, p); -#else -nonnative_put_be64 (val, p); -#endif -} - -/* -* While it's a bit non-obvious, even though the generic le/be wrappers -* use the __get/put_xxx prefixing, they actually wrap in to the -* non-prefixed get/put_xxx variants as provided above. -*/ -#include <linux/unaligned/generic.h> - -#ifdef __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_SH_UNALIGNED_SH4A_H */
arch/sh/include/asm/unaligned.h deleted +0 -13
@@ -1,13 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _ASM_SH_UNALIGNED_H -#define _ASM_SH_UNALIGNED_H - -#ifdef CONFIG_CPU_SH4A -/* SH-4A can handle unaligned loads in a relatively neutered fashion. */ -#include <asm/unaligned-sh4a.h> -#else -/* Otherwise, SH can't handle unaligned accesses. */ -#include <asm-generic/unaligned.h> -#endif - -#endif /* _ASM_SH_UNALIGNED_H */
Signature · 1 line
2.29.2
Re: [RFC 03/12] sh: remove unaligned access for sh4a
Quoted 6 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
Unlike every other architecture, sh4a uses an inline asm implementation for
get_unaligned(). I have shown that this produces better object code than the asm-generic version. However, there are very few users ofarch/sh/overall, and most of those seem to use sh4 rather than sh4a CPU
Hitachi's sh4, sh3, and sh2 are all out of patent, and have been cloned as open hardware. (Modulo the sh4 clone uses a different mmu design, but lots of regression testing gets done against qemu-system-sh4 and sh4 hardware using the original mmu.) Plus superh was the bestselling CPU architecture in the world for 3 years back in the 1990s, and there's a remaining userbase from that.
sh5 and the "a" designs like sh4a were created by renesas after hitachi spun off the IP to them, and are not out of patent. I do not have access to an sh4a test system.
Quoted 4 lines
Signed-off-by Arnd Bergmann <[email protected]>--- arch/sh/include/asm/unaligned-sh4a.h | 199 --------------------------- arch/sh/include/asm/unaligned.h | 13 --
All I can test is that it didn't cause regressions on my sh4 and j-core systems, which it (unsurprisingly) did not.
Rob
RFC 4/12 m68k: select CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
All supported CPUs other than the old dragonball use the include/linux/unaligned/access_ok.h implementation for accessing unaligned variables, so presumably this works everywhere.
However, m68k never selects CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, so none of the other conditionals in the kernel get the optimized implementation.
Select this based on CPU_HAS_NO_UNALIGNED to make the two settings always match, and then use the generic version of the header.
--- arch/m68k/Kconfig | 1 + arch/m68k/include/asm/unaligned.h | 19 ------------------- 2 files changed, 1 insertion(+), 19 deletions(-) delete mode 100644 arch/m68k/include/asm/unaligned.h
arch/m68k/Kconfig +1 -0
@@ -22,6 +22,7 @@ config M68K select HAVE_AOUT if MMU select HAVE_ASM_MODVERSIONS select HAVE_DEBUG_BUGVERBOSE + select HAVE_EFFICIENT_UNALIGNED_ACCESS if !CPU_HAS_NO_UNALIGNED select HAVE_FUTEX_CMPXCHG if MMU && FUTEX select HAVE_IDE select HAVE_LD_DEAD_CODE_DATA_ELIMINATION
arch/m68k/include/asm/unaligned.h deleted +0 -19
@@ -1,19 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _ASM_M68K_UNALIGNED_H -#define _ASM_M68K_UNALIGNED_H - -#ifdef CONFIG_CPU_HAS_NO_UNALIGNED -#include <asm-generic/unaligned.h> -#else -/* -* The m68k can do unaligned accesses itself. -*/ -#include <linux/unaligned/access_ok.h> -#include <linux/unaligned/generic.h> - -#define get_unaligned __get_unaligned_be -#define put_unaligned __put_unaligned_be - -#endif - -#endif /* _ASM_M68K_UNALIGNED_H */
Signature · 1 line
2.29.2
Re: [RFC 04/12] m68k: select CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Hi Arnd,
Quoted 3 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
All supported CPUs other than the old dragonball use the
Same comment about dragonball as for patch 01/12.
Quoted 15 lines
include/linux/unaligned/access_ok.himplementation for accessing unaligned variables, so presumably this works everywhere.However, m68k never selects
CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, so none of the other conditionals in the kernel get the optimized implementation.Select this based on
CPU_HAS_NO_UNALIGNEDto make the two settings always match, and then use the generic version of the header.Signed-off-by Arnd Bergmann <[email protected]>
arch/m68k/Kconfig config M68K · line 25
Quoted 4 lines
arch/m68k/Kconfig +1
-0@@ -22,6 +22,7 @@ config M68K select HAVE_AOUT if MMU select HAVE_ASM_MODVERSIONS select HAVE_DEBUG_BUGVERBOSE + select HAVE_EFFICIENT_UNALIGNED_ACCESS if !CPU_HAS_NO_UNALIGNED
This was clearly forgotten in commit 58340a07c194e0ae ("introduce HAVE_EFFICIENT_UNALIGNED_ACCESS Kconfig symbol"), which predates the existence of CPU_HAS_NO_UNALIGNED.
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
RFC 5/12 powerpc: use linux/unaligned/le_struct.h on LE power7
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
Little-endian POWER7 kernels disable CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS because that is not supported on the hardware, but the kernel still uses direct load/store for explicti get_unaligned()/put_unaligned().
I assume this is a mistake that leads to power7 having to trap and fix up all these unaligned accesses at a noticeable performance cost.
The fix is completely trivial, just remove the file and use the generic version that gets it right.
--- arch/powerpc/include/asm/unaligned.h | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 arch/powerpc/include/asm/unaligned.h
arch/powerpc/include/asm/unaligned.h deleted +0 -22
@@ -1,22 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _ASM_POWERPC_UNALIGNED_H -#define _ASM_POWERPC_UNALIGNED_H - -#ifdef __KERNEL__ - -/* -* The PowerPC can do unaligned accesses itself based on its endian mode. -*/ -#include <linux/unaligned/access_ok.h> -#include <linux/unaligned/generic.h> - -#ifdef __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 /* __KERNEL__ */ -#endif /* _ASM_POWERPC_UNALIGNED_H */
Signature · 1 line
2.29.2
RFC 6/12 asm-generic: unaligned: remove byteshift helpers
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
In theory, compilers should be able to work this out themselves so we can use a simpler version based on the swab() helpers.
I have verified that this works on all supported compiler versions (gcc-4.9 and up, clang-10 and up). Looking at the object code produced by gcc-11, I found that the impact is mostly a change in inlining decisions that lead to slightly larger code.
In other cases, this version produces explicit byte swaps in place of separate byte access, or comparing against pre-swapped constants.
While the source code is clearly simpler, I have not seen an indication of the new version actually producing better code on Arm, so maybe we want to skip this after all. From what I can tell, gcc recognizes the byteswap pattern in the byteshift.h header and can turn it into explicit instructions, but it does not turn a __builtin_bswap32() back into individual bytes when that would result in better output, e.g. when storing a byte-reversed constant.
---
I've included this patch in the series because Linus asked about removing the byteshift version, but after trying it out, I'd prefer to drop this and use the byteshift version for the generic code as well.
--- arch/arm/include/asm/unaligned.h | 2 - include/asm-generic/unaligned.h | 2 - include/linux/unaligned/be_byteshift.h | 71 -------------------------- include/linux/unaligned/be_struct.h | 30 +++++++++++ include/linux/unaligned/le_byteshift.h | 71 -------------------------- include/linux/unaligned/le_struct.h | 30 +++++++++++ 6 files changed, 60 insertions(+), 146 deletions(-) delete mode 100644 include/linux/unaligned/be_byteshift.h delete mode 100644 include/linux/unaligned/le_byteshift.h
arch/arm/include/asm/unaligned.h +0 -2
@@ -10,13 +10,11 @@#if defined(__LITTLE_ENDIAN)# include <linux/unaligned/le_struct.h> -# include <linux/unaligned/be_byteshift.h> # include <linux/unaligned/generic.h> # define get_unaligned __get_unaligned_le# define put_unaligned __put_unaligned_le#elif defined(__BIG_ENDIAN)# include <linux/unaligned/be_struct.h> -# include <linux/unaligned/le_byteshift.h> # include <linux/unaligned/generic.h> # define get_unaligned __get_unaligned_be# define put_unaligned __put_unaligned_be
include/asm-generic/unaligned.h +0 -2
@@ -16,7 +16,6 @@#if defined(__LITTLE_ENDIAN)# ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS # include <linux/unaligned/le_struct.h> -# include <linux/unaligned/be_byteshift.h> # endif # include <linux/unaligned/generic.h> # define get_unaligned __get_unaligned_le
@@ -24,7 +23,6 @@#elif defined(__BIG_ENDIAN)# ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS # include <linux/unaligned/be_struct.h> -# include <linux/unaligned/le_byteshift.h> # endif # include <linux/unaligned/generic.h> # define get_unaligned __get_unaligned_be
include/linux/unaligned/be_byteshift.h deleted +0 -71
71 lines not on this page · raw message
include/linux/unaligned/be_struct.h +30 -0
@@ -34,4 +34,34 @@ static inline void put_unaligned_be64(u64 val, void *p)__put_unaligned_cpu64 (val, p); } +static inline u16 get_unaligned_le16 (const void *p) +{ +return swab16 (__get_unaligned_cpu16 ((const u8 *)p)); +} + +static inline u32 get_unaligned_le32 (const void *p) +{ +return swab32 (__get_unaligned_cpu32 ((const u8 *)p)); +} + +static inline u64 get_unaligned_le64 (const void *p) +{ +return swab64 (__get_unaligned_cpu64 ((const u8 *)p)); +} + +static inline void put_unaligned_le16 (u16 val,void *p) +{ +__put_unaligned_cpu16 (swab16 (val), p); +} + +static inline void put_unaligned_le32 (u32 val,void *p) +{ +__put_unaligned_cpu32 (swab32 (val), p); +} + +static inline void put_unaligned_le64 (u64 val,void *p) +{ +__put_unaligned_cpu64 (swab64 (val), p); +} +#endif /* _LINUX_UNALIGNED_BE_STRUCT_H */
include/linux/unaligned/le_byteshift.h deleted +0 -71
71 lines not on this page · raw message
include/linux/unaligned/le_struct.h +30 -0
@@ -34,4 +34,34 @@ static inline void put_unaligned_le64(u64 val, void *p)__put_unaligned_cpu64 (val, p); } +static inline u16 get_unaligned_be16 (const void *p) +{ +return swab16 (__get_unaligned_cpu16 ((const u8 *)p)); +} + +static inline u32 get_unaligned_be32 (const void *p) +{ +return swab32 (__get_unaligned_cpu32 ((const u8 *)p)); +} + +static inline u64 get_unaligned_be64 (const void *p) +{ +return swab64 (__get_unaligned_cpu64 ((const u8 *)p)); +} + +static inline void put_unaligned_be16 (u16 val,void *p) +{ +__put_unaligned_cpu16 (swab16 (val), p); +} + +static inline void put_unaligned_be32 (u32 val,void *p) +{ +__put_unaligned_cpu32 (swab32 (val), p); +} + +static inline void put_unaligned_be64 (u64 val,void *p) +{ +__put_unaligned_cpu64 (swab64 (val), p); +} +#endif /* _LINUX_UNALIGNED_LE_STRUCT_H */
Signature · 1 line
2.29.2
Arnd Bergmann
maintainerRe: [RFC 06/12] asm-generic: unaligned: remove byteshift helpers
Quoted 8 lines · Arnd Bergmann
Suggested-by Linus Torvalds <[email protected]>Signed-off-by Arnd Bergmann <[email protected]>---
I've included this patch in the series because Linus asked about removing the byteshift version, but after trying it out, I'd prefer to drop this and use the byteshift version for the generic code as well.
Update: I've tried to create a small test case that illustrates the problem with using the swab() based version, however all cases combinations I tried showed either both producing identical output, or the shift version being worse, see https://godbolt.org/z/bTdsjnjfT
I'll rewrite the patch description accordingly and leave this in place.
Arnd
RFC 7/12 asm-generic: unaligned always use struct helpers
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
As found by Vineet Gupta and Linus Torvalds, gcc has somewhat unexpected behavior when faced with overlapping unaligned pointers. The kernel's unaligned/access-ok.h header technically invokes undefined behavior that happens to usually work on the architectures using it, but if the compiler optimizes code based on the assumption that undefined behavior doesn't happen, it can create output that actually causes data corruption.
A related problem was previously found on 32-bit ARMv7, where most instructions can be used on unaligned data, but 64-bit ldrd/strd causes an exception. The workaround was to always use the unaligned/le_struct.h helper instead of unaligned/access-ok.h, in commit 1cce91dfc8f7 ("ARM:
8715/1: add a private asm/unaligned.h").
The same solution should work on all other architectures as well, so remove the access-ok.h variant and use the other one unconditionally on all architectures, picking either the big-endian or little-endian version.
With this, the arm specific header can be removed as well, and the only file including linux/unaligned/access_ok.h gets moved to including the normal file.
Fortunately, this made almost no difference to the object code produced by gcc-11. On x86, s390, powerpc, and arc, the resulting binary appears to be identical to the previous version, while on arm64 and m68k there are minimal differences that looks like an optimization pass went into a different direction, usually using fewer stack spills on the new version.
--- arch/arm/include/asm/unaligned.h | 25 ----------- arch/mips/crypto/crc32-mips.c | 2 +- include/asm-generic/unaligned.h | 13 +----- include/linux/unaligned/access_ok.h | 68 ----------------------------- 4 files changed, 3 insertions(+), 105 deletions(-) delete mode 100644 arch/arm/include/asm/unaligned.h delete mode 100644 include/linux/unaligned/access_ok.h
arch/arm/include/asm/unaligned.h deleted +0 -25
@@ -1,25 +0,0 @@ -#ifndef __ASM_ARM_UNALIGNED_H -#define __ASM_ARM_UNALIGNED_H - -/* -* We generally want to set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS on ARMv6+, -* but we don't want to use linux/unaligned/access_ok.h since that can lead -* to traps on unaligned stm/ldm or strd/ldrd. -*/ -#include <asm/byteorder.h> - -#if defined(__LITTLE_ENDIAN) -# include <linux/unaligned/le_struct.h> -# include <linux/unaligned/generic.h> -# define get_unaligned __get_unaligned_le -# define put_unaligned __put_unaligned_le -#elif defined(__BIG_ENDIAN) -# include <linux/unaligned/be_struct.h> -# include <linux/unaligned/generic.h> -# define get_unaligned __get_unaligned_be -# define put_unaligned __put_unaligned_be -#else -# error need to define endianess -#endif - -#endif /* __ASM_ARM_UNALIGNED_H */
arch/mips/crypto/crc32-mips.c +1 -1
@@ -8,13 +8,13 @@* Copyright (C) 2018 MIPS Tech, LLC */ -#include <linux/unaligned/access_ok.h> #include <linux/cpufeature.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/string.h> #include <asm/mipsregs.h> +#include <asm/unaligned.h> #include <crypto/internal/hash.h>
include/asm-generic/unaligned.h +2 -11
@@ -8,22 +8,13 @@*/ #include <asm/byteorder.h> -/* Set by the arch if it can handle unaligned accesses in hardware. */ -#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS -# include <linux/unaligned/access_ok.h> -#endif -#if defined(__LITTLE_ENDIAN) -# ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS -# include <linux/unaligned/le_struct.h> -# endif +# include <linux/unaligned/le_struct.h> # include <linux/unaligned/generic.h> # define get_unaligned __get_unaligned_le# define put_unaligned __put_unaligned_le#elif defined(__BIG_ENDIAN) -# ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS -# include <linux/unaligned/be_struct.h> -# endif +# include <linux/unaligned/be_struct.h> # include <linux/unaligned/generic.h> # define get_unaligned __get_unaligned_be# define put_unaligned __put_unaligned_be
include/linux/unaligned/access_ok.h deleted +0 -68
68 lines not on this page · raw message
Signature · 1 line
2.29.2
RFC 8/12 partitions: msdos: fix one-byte get_unaligned()
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
A simplification of get_unaligned() clashes with callers that pass in a character pointer, causing a harmless warning like:
block/partitions/msdos.c: In function 'msdos_partition':include/asm-generic/unaligned.h:13:22: warning: 'packed' attribute ignored for field of type 'u8' {aka 'unsigned char'} [-Wattributes]
Remove the get_unaligned() call and just use the byte directly.
--- block/partitions/ldm.h | 2 +- block/partitions/msdos.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
block/partitions/ldm.h +1 -1
@@ -85,7 +85,7 @@ struct parsed_partitions;#define TOC_BITMAP2 "log"/* bitmaps in the TOCBLOCK. */ /* Borrowed from msdos.c */ -#define SYS_IND (p) (get_unaligned(&(p)->sys_ind)) +#define SYS_IND (p) ((p)->sys_ind)struct frag {/* VBLK Fragment handling */ struct list_head list;
block/partitions/msdos.c +1 -1
@@ -38,7 +38,7 @@*/ #include <asm/unaligned.h> -#define SYS_IND (p) get_unaligned(&p->sys_ind) +#define SYS_IND (p) (p->sys_ind)static inline sector_t nr_sects (struct msdos_partition *p) {
Signature · 1 line
2.29.2
RFC 9/12 apparmor: use get_unaligned() only for multi-byte words
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
Using get_unaligned() on a u8 pointer is pointless, and will result in a compiler warning after a planned cleanup:
In file included from arch/x86/include/generated/asm/unaligned.h:1,
from security/apparmor/policy_unpack.c:16:security/apparmor/policy_unpack.c: In function 'unpack_u8':include/asm-generic/unaligned.h:13:15: error: 'packed' attribute ignored for field of type 'u8' {aka 'unsigned char'} [-Werror=attributes]
13 | const struct { type x __packed; } *__pptr = (typeof(__pptr))(ptr); \
| ^
Simply dereference this pointer directly.
--- security/apparmor/policy_unpack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
security/apparmor/policy_unpack.c +1 -1
@@ -304,7 +304,7 @@ static bool unpack_u8(struct aa_ext *e, u8 *data, const char *name)if (!inbounds (e,sizeof (u8))) goto fail;if (data) - *data =get_unaligned ((u8 *)e->pos); + *data = *((u8 *)e->pos); e->pos +=sizeof (u8);return true; }
Signature · 1 line
2.29.2
Re: [RFC 09/12] apparmor: use get_unaligned() only for multi-byte words
Quoted 12 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
Using
get_unaligned()on a u8 pointer is pointless, and will result in a compiler warning after a planned cleanup:In file included from
arch/x86/include/generated/asm/unaligned.h:1,
fromsecurity/apparmor/policy_unpack.c:16:security/apparmor/policy_unpack.c: In function 'unpack_u8':include/asm-generic/unaligned.h:13:15: error: 'packed' attribute ignored for field of type 'u8' {aka 'unsigned char'} [-Werror=attributes]
13 | const struct { type x__packed; }*__pptr= (typeof(__pptr))(ptr); \
| ^
Quoted 3 lines
Simply dereference this pointer directly.
Signed-off-by Arnd Bergmann <[email protected]>
Quoted 18 lines
--- security/apparmor/policy_unpack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)security/apparmor/policy_unpack.c +1
-1@@ -304,7 +304,7 @@ static bool unpack_u8(struct aa_ext *e, u8 *data, const char *name)if (!inbounds (e,sizeof (u8))) goto fail;if (data) - *data =get_unaligned ((u8 *)e->pos); + *data = *((u8 *)e->pos); e->pos +=sizeof (u8);return true; }
RFC 10/12 mwifiex: re-fix for unaligned accesses
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
A patch from 2017 changed some accesses to DMA memory to use get_unaligned_le32() and similar interfaces, to avoid problems with doing unaligned accesson uncached memory.
However, the change in the mwifiex_pcie_alloc_sleep_cookie_buf() function ended up changing the size of the access instead, as it operates on a pointer to u8.
Change this function back to actually access the entire 32 bits. Note that the pointer is aligned by definition because it came from dma_alloc_coherent().
--- drivers/net/wireless/marvell/mwifiex/pcie.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/net/wireless/marvell/mwifiex/pcie.c +4 -6
@@ -1231,7 +1231,7 @@ static int mwifiex_pcie_delete_cmdrsp_buf(struct mwifiex_adapter *adapter)static int mwifiex_pcie_alloc_sleep_cookie_buf (struct mwifiex_adapter *adapter) {struct pcie_service_card *card = adapter->card; -u32 tmp; +u32 *cookie; card->sleep_cookie_vbase = dma_alloc_coherent(&card->dev->dev, sizeof(u32),
@@ -1242,13 +1242,11 @@ static int mwifiex_pcie_alloc_sleep_cookie_buf(struct mwifiex_adapter *adapter) "dma_alloc_coherent failed!\n");return -ENOMEM ; } + cookie = (u32 *)card->sleep_cookie_vbase;/* Init val of Sleep Cookie */ - tmp =FW_AWAKE_COOKIE ; -put_unaligned (tmp, card->sleep_cookie_vbase); + *cookie =FW_AWAKE_COOKIE ; -mwifiex_dbg (adapter,INFO , -"alloc_scook: sleep cookie=0x%x\n" , -get_unaligned (card->sleep_cookie_vbase)); +mwifiex_dbg (adapter,INFO ,"alloc_scook: sleep cookie=0x%x\n" , *cookie);return 0 ; }
Signature · 1 line
2.29.2
RFC 11/12 netpoll: avoid put_unaligned() on single character
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
With a planned cleanup, using put_unaligned() on a single character results in a harmless warning:
In file included from ./arch/x86/include/generated/asm/unaligned.h:1,
from include/linux/etherdevice.h:24,
from net/core/netpoll.c:18:
net/core/netpoll.c: In function 'netpoll_send_udp':
include/asm-generic/unaligned.h:23:9: error: 'packed' attribute ignored for field of type 'unsigned char' [-Werror=attributes]
net/core/netpoll.c:431:3: note: in expansion of macro 'put_unaligned'
431 | put_unaligned(0x60, (unsigned char *)ip6h);
| ^~~~~~~~~~~~~
include/asm-generic/unaligned.h:23:9: error: 'packed' attribute ignored for field of type 'unsigned char' [-Werror=attributes]
net/core/netpoll.c:459:3: note: in expansion of macro 'put_unaligned'
459 | put_unaligned(0x45, (unsigned char *)iph);
| ^~~~~~~~~~~~~Replace this with an open-coded pointer dereference.
--- net/core/netpoll.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/netpoll.c +2 -2
@@ -428,7 +428,7 @@ void netpoll_send_udp(struct netpoll *np, const char *msg, int len) ip6h =ipv6_hdr (skb);/* ip6h->version = 6; ip6h->priority = 0; */ -put_unaligned (0x60 , (unsigned char *)ip6h); + *(unsigned char *)ip6h =0x60 ; ip6h->flow_lbl[0 ] =0 ; ip6h->flow_lbl[1 ] =0 ; ip6h->flow_lbl[2 ] =0 ;
@@ -456,7 +456,7 @@ void netpoll_send_udp(struct netpoll *np, const char *msg, int len) iph =ip_hdr (skb);/* iph->version = 4; iph->ihl = 5; */ -put_unaligned (0x45 , (unsigned char *)iph); + *(unsigned char *)iph =0x45 ; iph->tos =0 ;put_unaligned (htons (ip_len), &(iph->tot_len)); iph->id =htons (atomic_inc_return (&ip_ident));
Signature · 1 line
2.29.2
RFC 12/12 asm-generic: simplify asm/unaligned.h
In reply to Arnd BergmannFrom: Arnd Bergmann <[email protected]>
The get_unaligned()/put_unaligned() implementations are much more complex than necessary, now that all architectures use the same code.
Move everything into one file and use a much more compact way to express the same logic.
I've compared the binary output using gcc-11 across defconfig builds for all architectures and found this patch to make no difference, except for a single function on powerpc that needs two additional register moves because of random differences in register allocation.
There are a handful of callers of the low-level __get_unaligned_cpu32, so leave that in place for the time being even though the common code no longer uses it.
This adds a warning for any caller of get_unaligned()/put_unaligned() that passes in a single-byte pointer, but I've sent patches for all instances that show up in x86 and randconfig builds. It would be nice to change the arguments of the endian-specific accessors to take the matching __be16/__be32/__be64/__le16/__le32/__le64 arguments instead of a void pointer, but that requires more changes to the rest of the kernel.
--- include/asm-generic/unaligned.h | 138 +++++++++++++++++++++++++--- include/linux/unaligned/be_struct.h | 67 -------------- include/linux/unaligned/generic.h | 115 ----------------------- include/linux/unaligned/le_struct.h | 67 -------------- 4 files changed, 125 insertions(+), 262 deletions(-) delete mode 100644 include/linux/unaligned/be_struct.h delete mode 100644 include/linux/unaligned/generic.h delete mode 100644 include/linux/unaligned/le_struct.h
include/asm-generic/unaligned.h +125 -13
@@ -6,20 +6,132 @@* This is the most generic implementation of unaligned accesses * and should work almost anywhere. */ +#include <linux/unaligned/packed_struct.h> #include <asm/byteorder.h> -#if defined(__LITTLE_ENDIAN) -# include <linux/unaligned/le_struct.h> -# include <linux/unaligned/generic.h> -# define get_unaligned __get_unaligned_le -# define put_unaligned __put_unaligned_le -#elif defined(__BIG_ENDIAN) -# include <linux/unaligned/be_struct.h> -# include <linux/unaligned/generic.h> -# define get_unaligned __get_unaligned_be -# define put_unaligned __put_unaligned_be -#else -# error need to define endianess -#endif +#define __get_unaligned_t (type, ptr) ({ \ + const struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr); \ + __pptr->x; \ +}) + +#define get_unaligned (ptr) ({ \ + __auto_type __ptr = (ptr); \ + __get_unaligned_t(typeof(*__ptr), __ptr); \ +}) + +#define __put_unaligned_t (type, val, ptr) ({ \ + struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr); \ + __pptr->x = (val); \ +}) + +#define put_unaligned (val, ptr) do { \ + __auto_type __ptr = (ptr); \ + __put_unaligned_t(typeof(*__ptr), (val), __ptr); \ +} while (0) + + +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); +} + +static inline u32 __get_unaligned_be24 (const u8 *p) +{ +return p[0 ] <<16 | p[1 ] <<8 | p[2 ]; +} + +static inline u32 get_unaligned_be24 (const void *p) +{ +return __get_unaligned_be24 (p); +} + +static inline u32 __get_unaligned_le24 (const u8 *p) +{ +return p[0 ] | p[1 ] <<8 | p[2 ] <<16 ; +} + +static inline u32 get_unaligned_le24 (const void *p) +{ +return __get_unaligned_le24 (p); +} + +static inline void __put_unaligned_be24 (const u32 val,u8 *p) +{ + *p++ = val >>16 ; + *p++ = val >>8 ; + *p++ = val; +} + +static inline void put_unaligned_be24 (const u32 val,void *p) +{ +__put_unaligned_be24 (val, p); +} + +static inline void __put_unaligned_le24 (const u32 val,u8 *p) +{ + *p++ = val; + *p++ = val >>8 ; + *p++ = val >>16 ; +} + +static inline void put_unaligned_le24 (const u32 val,void *p) +{ +__put_unaligned_le24 (val, p); +}#endif /* __ASM_GENERIC_UNALIGNED_H */
include/linux/unaligned/be_struct.h deleted +0 -67
67 lines not on this page · raw message
include/linux/unaligned/generic.h deleted +0 -115
115 lines not on this page · raw message
include/linux/unaligned/le_struct.h deleted +0 -67
67 lines not on this page · raw message
Signature · 1 line
2.29.2
Re: [RFC 12/12] asm-generic: simplify asm/unaligned.h
Quoted 5 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
The get_unaligned()/put_unaligned() implementations are much more complex than necessary, now that all architectures use the same code.
Thanks for doing this, it looks good to me.
I suspect it's still slightly unnecessarily complicated - why is that get_unaligned() not just
#define get_unaligned(ptr) \
__get_unaligned_t(typeof(*__ptr), __ptr)Because I'm not seeing the reason for doing that "__auto_type __ptr" thing - the argument to a "typeof()" isn't actually evaluated.
Maybe I'm just nit-picking, this certainly is a huge improvement regardless.
Linus
Re: [RFC 12/12] asm-generic: simplify asm/unaligned.h
Quoted 9 lines · Linus Torvalds
Quoted 5 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
The get_unaligned()/put_unaligned() implementations are much more complex than necessary, now that all architectures use the same code.
Thanks for doing this, it looks good to me.
Quoted 8 lines
I suspect it's still slightly unnecessarily complicated - why is that
get_unaligned()not just#define get_unaligned(ptr) \ __get_unaligned_t(typeof(*__ptr), __ptr)Because I'm not seeing the reason for doing that "
__auto_type__ptr" thing - the argument to a "typeof()" isn't actually evaluated.
Both versions are equally correct, I picked the __auto_type version because this tends to produce smaller preprocessor output when you have multiple layers of nested macros with 'ptr' expanding to something complicated, and the get_unaligned() itself being expanded multiple times again.
When I recently experimented with possible changes to cmpxchg() and get_user(), it had a measurable impact on compile time with clang on those macros.
get_unaligned() doesn't appear to be used much in nested macros at all, so it probably won't actually help here, and I can just do the simpler version instead.
I forgot to mention in the changelog that this version does not actually require the argument to be a scalar, not sure if this is something we want or not. It does allow developers to write something like
__be32 get_ip_saddr(struct sk_buff *skb)
{
struct iphdr *iph = ip_hdr(skb);
return get_unaligned(iph).saddr;
}and get the expected result. While this seems handy, it also makes it harder to change the macro back to one that only works on scalars after such usage becomes widespread.
Arnd
Re: [RFC 12/12] asm-generic: simplify asm/unaligned.h
Quoted 6 lines · Arnd Bergmann
Quoted 3 lines · Linus Torvalds
#define get_unaligned(ptr) \ __get_unaligned_t(typeof(*__ptr), __ptr)
That's missing a set of parentheses around that __ptr thing ('*__ptr' should be '*(__ptr)'), btw, so don't use that as-is.
Quoted 4 lines
Both versions are equally correct, I picked the
__auto_typeversion because this tends to produce smaller preprocessor output when you have multiple layers of nested macros with 'ptr' expanding to something complicated,
Ahh.
Yeah, that's probably not a problem for get_unaligned(), but it might happen in other situations.
Linus
RE: [RFC 12/12] asm-generic: simplify asm/unaligned.h
In reply to Arnd BergmannFrom: Arnd Bergmann
Quoted 5 lines
Sent: 07 May 2021 23:08
The get_unaligned()/put_unaligned() implementations are much more complex than necessary, now that all architectures use the same code.
...
include/asm-generic/unaligned.h line 15
Quoted 4 lines
include/asm-generic/unaligned.h +4
-0+#define __get_unaligned_t (type, ptr) ({ \ + const struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr); \ + __pptr->x; \ +})
I thought gcc was likely to track through the alignment of the variable holding the source pointer (through any (void *) casts implied by inlined function calls) through to the pointer used for the actual access - so it would tend to issue a single instruction that assumed an aligned address.
I know that has caused grief trying to copy unaligned data to an aligned structure.
Possibly adding:
asm ("" :: "+r" (__pptr)) );in the middle stops that assumption without generating any code.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
RE: [RFC 12/12] asm-generic: simplify asm/unaligned.h
From: David Laight
Quoted 20 lines
Sent: 08 May 2021 12:03
From: Arnd Bergmann
Quoted 5 lines
Sent: 07 May 2021 23:08
The get_unaligned()/put_unaligned() implementations are much more complex than necessary, now that all architectures use the same code.
...
include/asm-generic/unaligned.h line 15
Quoted 4 lines
include/asm-generic/unaligned.h +4
-0+#define __get_unaligned_t (type, ptr) ({ \ + const struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr); \ + __pptr->x; \ +})I thought gcc was likely to track through the alignment of the variable holding the source pointer (through any (void *) casts implied by inlined function calls) through to the pointer used for the actual access - so it would tend to issue a single instruction that assumed an aligned address.
Quoted 6 lines
I know that has caused grief trying to copy unaligned data to an aligned structure.
Possibly adding:
asm ("" :: "+r" (__pptr)) );in the middle stops that assumption without generating any code.
That is the wrong asm.
You need the one where an input operand and output operand share the same register and use it for the assignment.
I've been trying to get godbolt to do something useful. But it seems to be stuck in C++ mode and is missing something like sparc which definitely doesn't do misaligned accesses.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Geert Uytterhoeven
maintainerRe: [RFC 12/12] asm-generic: simplify asm/unaligned.h
In reply to Arnd BergmannHi Arnd,
Quoted 15 lines · Arnd Bergmann
The get_unaligned()/put_unaligned() implementations are much more complex than necessary, now that all architectures use the same code.
Move everything into one file and use a much more compact way to express the same logic.
I've compared the binary output using gcc-11 across defconfig builds for all architectures and found this patch to make no difference, except for a single function on powerpc that needs two additional register moves because of random differences in register allocation.
There are a handful of callers of the low-level
__get_unaligned_cpu32, so leave that in place for the time being even though the common code no longer uses it.
Quoted 8 lines
This adds a warning for any caller of get_unaligned()/put_unaligned() that passes in a single-byte pointer, but I've sent patches for all instances that show up in x86 and randconfig builds. It would be nice to change the arguments of the endian-specific accessors to take the matching __be16/__be32/__be64/__le16/__le32/__le64 arguments instead of a void pointer, but that requires more changes to the rest of the kernel.
Signed-off-by Arnd Bergmann <[email protected]>
Thanks for your patch!
Quoted 13 lines
include/asm-generic/unaligned.h +1
-6@@ -6,20 +6,132 @@* This is the most generic implementation of unaligned accesses * and should work almost anywhere. */ +#include <linux/unaligned/packed_struct.h> #include <asm/byteorder.h> -#if defined(__LITTLE_ENDIAN) -# include <linux/unaligned/le_struct.h> -# include <linux/unaligned/generic.h> -# define get_unaligned __get_unaligned_le -# define put_unaligned __put_unaligned_le -#elif defined(__BIG_ENDIAN)
include/asm-generic/unaligned.h line 14
Quoted 10 lines
include/asm-generic/unaligned.h +3
-7-# include <linux/unaligned/be_struct.h> -# include <linux/unaligned/generic.h> -# define get_unaligned __get_unaligned_be -# define put_unaligned __put_unaligned_be -#else -# error need to define endianess -#endif +#define __get_unaligned_t (type, ptr) ({ \ + const struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr); \ + __pptr->x; \
Space before tab (cfr. checkpatch).
include/asm-generic/unaligned.h line 24
Quoted 10 lines
include/asm-generic/unaligned.h +10
-0+}) + +#define get_unaligned (ptr) ({ \ + __auto_type __ptr = (ptr); \ + __get_unaligned_t(typeof(*__ptr), __ptr); \ +}) + +#define __put_unaligned_t (type, val, ptr) ({ \ + struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr); \ + __pptr->x = (val); \
Likewise
Review line 26
Quoted 2 lines
(quoted diff) +1
-0+})
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds