public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] Implement endianness swap macros for RISC-V
@ 2025-03-19 21:09 Ignacio Encinas
  2025-03-19 21:09 ` [PATCH v2 1/2] include/uapi/linux/swab.h: move default implementation for swab macros into asm-generic Ignacio Encinas
  2025-03-19 21:09 ` [PATCH v2 2/2] riscv: introduce asm/swab.h Ignacio Encinas
  0 siblings, 2 replies; 11+ messages in thread
From: Ignacio Encinas @ 2025-03-19 21:09 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Alexandre Ghiti, Arnd Bergmann
  Cc: Eric Biggers, linux-riscv, linux-kernel, linux-kernel-mentees,
	skhan, Zhihang Shao, Björn Töpel, linux-arch,
	Ignacio Encinas

Motivated by [1]. A couple of things to note:

RISC-V needs a default implementation to fall back on. Such
implementation is available in include/uapi/linux/swab.h in the form of 
___constant_swabXX macros. As include/uapi/linux/swab.h can't be 
included from arch/riscv/include/asm/swab.h, the default implementation 
has been moved into asm-generic in the first patch of the series.

Tested with crc_kunit as pointed out in [2]. I can't provide performance 
numbers as I don't have RISC-V hardware yet.

[1] https://lore.kernel.org/all/20250302220426.GC2079@quark.localdomain/
[2] https://lore.kernel.org/all/20250216225530.306980-1-ebiggers@kernel.org/

Signed-off-by: Ignacio Encinas <ignacio@iencinas.com>
---
Changes in v2:
- Introduce first patch factoring out the default implementation into
  asm-generic

Patch 2:
- Remove blank line to make checkpatch happy 
- Instead of duplicating the default implementation for swap macros,
  leverage patch 1 and add an include to asm-generic/swab.h

- Link to v1: https://lore.kernel.org/r/20250310-riscv-swab-v1-1-34652ef1ee96@iencinas.com

---
Ignacio Encinas (2):
      include/uapi/linux/swab.h: move default implementation for swab macros into asm-generic
      riscv: introduce asm/swab.h

 arch/riscv/include/asm/swab.h   | 48 +++++++++++++++++++++++++++++++++++++++++
 include/uapi/asm-generic/swab.h | 32 +++++++++++++++++++++++++++
 include/uapi/linux/swab.h       | 33 +---------------------------
 3 files changed, 81 insertions(+), 32 deletions(-)
---
base-commit: a7f2e10ecd8f18b83951b0bab47ddaf48f93bf47
change-id: 20250307-riscv-swab-b81b94a9ac1b

Best regards,
-- 
Ignacio Encinas <ignacio@iencinas.com>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 1/2] include/uapi/linux/swab.h: move default implementation for swab macros into asm-generic
  2025-03-19 21:09 [PATCH v2 0/2] Implement endianness swap macros for RISC-V Ignacio Encinas
@ 2025-03-19 21:09 ` Ignacio Encinas
  2025-03-19 21:12   ` Arnd Bergmann
  2025-03-19 21:09 ` [PATCH v2 2/2] riscv: introduce asm/swab.h Ignacio Encinas
  1 sibling, 1 reply; 11+ messages in thread
From: Ignacio Encinas @ 2025-03-19 21:09 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Alexandre Ghiti, Arnd Bergmann
  Cc: Eric Biggers, linux-riscv, linux-kernel, linux-kernel-mentees,
	skhan, Zhihang Shao, Björn Töpel, linux-arch,
	Ignacio Encinas

Move the default byteswap implementation into asm-generic so that it can
be included from arch code.

This is required by RISC-V in order to have a fallback implementation
without duplicating it.

Signed-off-by: Ignacio Encinas <ignacio@iencinas.com>
---
 include/uapi/asm-generic/swab.h | 32 ++++++++++++++++++++++++++++++++
 include/uapi/linux/swab.h       | 33 +--------------------------------
 2 files changed, 33 insertions(+), 32 deletions(-)

diff --git a/include/uapi/asm-generic/swab.h b/include/uapi/asm-generic/swab.h
index f2da4e4fd4d129c43f904c5f1b6234036b57cc77..43d83df007a6fbfb0011452e12e71f429425cad5 100644
--- a/include/uapi/asm-generic/swab.h
+++ b/include/uapi/asm-generic/swab.h
@@ -16,4 +16,36 @@
 #endif
 #endif
 
+/*
+ * casts are necessary for constants, because we never know how for sure
+ * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
+ */
+#define ___constant_swab16(x) ((__u16)(				\
+	(((__u16)(x) & (__u16)0x00ffU) << 8) |			\
+	(((__u16)(x) & (__u16)0xff00U) >> 8)))
+
+#define ___constant_swab32(x) ((__u32)(				\
+	(((__u32)(x) & (__u32)0x000000ffUL) << 24) |		\
+	(((__u32)(x) & (__u32)0x0000ff00UL) <<  8) |		\
+	(((__u32)(x) & (__u32)0x00ff0000UL) >>  8) |		\
+	(((__u32)(x) & (__u32)0xff000000UL) >> 24)))
+
+#define ___constant_swab64(x) ((__u64)(				\
+	(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) |	\
+	(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) |	\
+	(((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) |	\
+	(((__u64)(x) & (__u64)0x00000000ff000000ULL) <<  8) |	\
+	(((__u64)(x) & (__u64)0x000000ff00000000ULL) >>  8) |	\
+	(((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) |	\
+	(((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) |	\
+	(((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56)))
+
+#define ___constant_swahw32(x) ((__u32)(			\
+	(((__u32)(x) & (__u32)0x0000ffffUL) << 16) |		\
+	(((__u32)(x) & (__u32)0xffff0000UL) >> 16)))
+
+#define ___constant_swahb32(x) ((__u32)(			\
+	(((__u32)(x) & (__u32)0x00ff00ffUL) << 8) |		\
+	(((__u32)(x) & (__u32)0xff00ff00UL) >> 8)))
+
 #endif /* _ASM_GENERIC_SWAB_H */
diff --git a/include/uapi/linux/swab.h b/include/uapi/linux/swab.h
index 01717181339eb0fb5128668ca13f38205c03fa28..ca808c492996f810ce417ce9701306070873847b 100644
--- a/include/uapi/linux/swab.h
+++ b/include/uapi/linux/swab.h
@@ -6,38 +6,7 @@
 #include <linux/stddef.h>
 #include <asm/bitsperlong.h>
 #include <asm/swab.h>
-
-/*
- * casts are necessary for constants, because we never know how for sure
- * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
- */
-#define ___constant_swab16(x) ((__u16)(				\
-	(((__u16)(x) & (__u16)0x00ffU) << 8) |			\
-	(((__u16)(x) & (__u16)0xff00U) >> 8)))
-
-#define ___constant_swab32(x) ((__u32)(				\
-	(((__u32)(x) & (__u32)0x000000ffUL) << 24) |		\
-	(((__u32)(x) & (__u32)0x0000ff00UL) <<  8) |		\
-	(((__u32)(x) & (__u32)0x00ff0000UL) >>  8) |		\
-	(((__u32)(x) & (__u32)0xff000000UL) >> 24)))
-
-#define ___constant_swab64(x) ((__u64)(				\
-	(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) |	\
-	(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) |	\
-	(((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) |	\
-	(((__u64)(x) & (__u64)0x00000000ff000000ULL) <<  8) |	\
-	(((__u64)(x) & (__u64)0x000000ff00000000ULL) >>  8) |	\
-	(((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) |	\
-	(((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) |	\
-	(((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56)))
-
-#define ___constant_swahw32(x) ((__u32)(			\
-	(((__u32)(x) & (__u32)0x0000ffffUL) << 16) |		\
-	(((__u32)(x) & (__u32)0xffff0000UL) >> 16)))
-
-#define ___constant_swahb32(x) ((__u32)(			\
-	(((__u32)(x) & (__u32)0x00ff00ffUL) << 8) |		\
-	(((__u32)(x) & (__u32)0xff00ff00UL) >> 8)))
+#include <asm-generic/swab.h>
 
 /*
  * Implement the following as inlines, but define the interface using

-- 
2.48.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 2/2] riscv: introduce asm/swab.h
  2025-03-19 21:09 [PATCH v2 0/2] Implement endianness swap macros for RISC-V Ignacio Encinas
  2025-03-19 21:09 ` [PATCH v2 1/2] include/uapi/linux/swab.h: move default implementation for swab macros into asm-generic Ignacio Encinas
@ 2025-03-19 21:09 ` Ignacio Encinas
  2025-03-21  3:37   ` Eric Biggers
  1 sibling, 1 reply; 11+ messages in thread
From: Ignacio Encinas @ 2025-03-19 21:09 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Alexandre Ghiti, Arnd Bergmann
  Cc: Eric Biggers, linux-riscv, linux-kernel, linux-kernel-mentees,
	skhan, Zhihang Shao, Björn Töpel, linux-arch,
	Ignacio Encinas

Implement endianness swap macros for RISC-V.

Use the rev8 instruction when Zbb is available. Otherwise, rely on the
default mask-and-shift implementation.

Signed-off-by: Ignacio Encinas <ignacio@iencinas.com>
---
 arch/riscv/include/asm/swab.h | 48 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/arch/riscv/include/asm/swab.h b/arch/riscv/include/asm/swab.h
new file mode 100644
index 0000000000000000000000000000000000000000..6cb40e8108c956dd445746d59bc1dd0a53475212
--- /dev/null
+++ b/arch/riscv/include/asm/swab.h
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASM_RISCV_SWAB_H
+#define _ASM_RISCV_SWAB_H
+
+#include <linux/types.h>
+#include <linux/compiler.h>
+#include <asm/alternative-macros.h>
+#include <asm/hwcap.h>
+#include <asm-generic/swab.h>
+
+#if defined(CONFIG_RISCV_ISA_ZBB) && !defined(NO_ALTERNATIVE)
+
+#define ARCH_SWAB(size) \
+static __always_inline unsigned long __arch_swab##size(__u##size value) \
+{									\
+	unsigned long x = value;					\
+									\
+	asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,			\
+			     RISCV_ISA_EXT_ZBB, 1)			\
+			     :::: legacy);				\
+									\
+	asm volatile (".option push\n"					\
+		      ".option arch,+zbb\n"				\
+		      "rev8 %0, %1\n"					\
+		      ".option pop\n"					\
+		      : "=r" (x) : "r" (x));				\
+									\
+	return x >> (BITS_PER_LONG - size);				\
+									\
+legacy:									\
+	return  ___constant_swab##size(value);				\
+}
+
+#ifdef CONFIG_64BIT
+ARCH_SWAB(64)
+#define __arch_swab64 __arch_swab64
+#endif
+
+ARCH_SWAB(32)
+#define __arch_swab32 __arch_swab32
+
+ARCH_SWAB(16)
+#define __arch_swab16 __arch_swab16
+
+#undef ARCH_SWAB
+
+#endif /* defined(CONFIG_RISCV_ISA_ZBB) && !defined(NO_ALTERNATIVE) */
+#endif /* _ASM_RISCV_SWAB_H */

-- 
2.48.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/2] include/uapi/linux/swab.h: move default implementation for swab macros into asm-generic
  2025-03-19 21:09 ` [PATCH v2 1/2] include/uapi/linux/swab.h: move default implementation for swab macros into asm-generic Ignacio Encinas
@ 2025-03-19 21:12   ` Arnd Bergmann
  2025-03-19 21:37     ` Ignacio Encinas Rubio
  0 siblings, 1 reply; 11+ messages in thread
From: Arnd Bergmann @ 2025-03-19 21:12 UTC (permalink / raw)
  To: Ignacio Encinas, Paul Walmsley, Palmer Dabbelt, Alexandre Ghiti
  Cc: Eric Biggers, linux-riscv, linux-kernel, linux-kernel-mentees,
	Shuah Khan, Zhihang Shao, Björn Töpel, Linux-Arch

On Wed, Mar 19, 2025, at 22:09, Ignacio Encinas wrote:
> Move the default byteswap implementation into asm-generic so that it can
> be included from arch code.
>
> This is required by RISC-V in order to have a fallback implementation
> without duplicating it.
>
> Signed-off-by: Ignacio Encinas <ignacio@iencinas.com>
> ---
>  include/uapi/asm-generic/swab.h | 32 ++++++++++++++++++++++++++++++++
>  include/uapi/linux/swab.h       | 33 +--------------------------------
>  2 files changed, 33 insertions(+), 32 deletions(-)
>

I think we should just remove these entirely in favor of the
compiler-povided built-ins.

    Arnd

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/2] include/uapi/linux/swab.h: move default implementation for swab macros into asm-generic
  2025-03-19 21:12   ` Arnd Bergmann
@ 2025-03-19 21:37     ` Ignacio Encinas Rubio
  2025-03-19 21:49       ` Arnd Bergmann
  0 siblings, 1 reply; 11+ messages in thread
From: Ignacio Encinas Rubio @ 2025-03-19 21:37 UTC (permalink / raw)
  To: Arnd Bergmann, Paul Walmsley, Palmer Dabbelt, Alexandre Ghiti
  Cc: Eric Biggers, linux-riscv, linux-kernel, linux-kernel-mentees,
	Shuah Khan, Zhihang Shao, Björn Töpel, Linux-Arch



On 19/3/25 22:12, Arnd Bergmann wrote:
> On Wed, Mar 19, 2025, at 22:09, Ignacio Encinas wrote:
>> Move the default byteswap implementation into asm-generic so that it can
>> be included from arch code.
>>
>> This is required by RISC-V in order to have a fallback implementation
>> without duplicating it.
>>
>> Signed-off-by: Ignacio Encinas <ignacio@iencinas.com>
>> ---
>>  include/uapi/asm-generic/swab.h | 32 ++++++++++++++++++++++++++++++++
>>  include/uapi/linux/swab.h       | 33 +--------------------------------
>>  2 files changed, 33 insertions(+), 32 deletions(-)
>>
> 
> I think we should just remove these entirely in favor of the
> compiler-povided built-ins.

Got it. I assumed they existed to explicitly avoid relying on
__builtin_bswap as they might not exist. However, I did a quick grep and
found that there are some uses in the wild.

I couldn't find compiler builtins for ___constant_swahb32 nor 
___constant_swahw32, so I guess I'll leave them as they are.

Thank you!

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/2] include/uapi/linux/swab.h: move default implementation for swab macros into asm-generic
  2025-03-19 21:37     ` Ignacio Encinas Rubio
@ 2025-03-19 21:49       ` Arnd Bergmann
  2025-03-20 22:36         ` Ignacio Encinas Rubio
  0 siblings, 1 reply; 11+ messages in thread
From: Arnd Bergmann @ 2025-03-19 21:49 UTC (permalink / raw)
  To: Ignacio Encinas, Paul Walmsley, Palmer Dabbelt, Alexandre Ghiti
  Cc: Eric Biggers, linux-riscv, linux-kernel, linux-kernel-mentees,
	Shuah Khan, Zhihang Shao, Björn Töpel, Linux-Arch

On Wed, Mar 19, 2025, at 22:37, Ignacio Encinas Rubio wrote:
> On 19/3/25 22:12, Arnd Bergmann wrote:
>> On Wed, Mar 19, 2025, at 22:09, Ignacio Encinas wrote:
>>> Move the default byteswap implementation into asm-generic so that it can
>>> be included from arch code.
>>>
>>> This is required by RISC-V in order to have a fallback implementation
>>> without duplicating it.
>>>
>>> Signed-off-by: Ignacio Encinas <ignacio@iencinas.com>
>>> ---
>>>  include/uapi/asm-generic/swab.h | 32 ++++++++++++++++++++++++++++++++
>>>  include/uapi/linux/swab.h       | 33 +--------------------------------
>>>  2 files changed, 33 insertions(+), 32 deletions(-)
>>>
>> 
>> I think we should just remove these entirely in favor of the
>> compiler-povided built-ins.
>
> Got it. I assumed they existed to explicitly avoid relying on
> __builtin_bswap as they might not exist. However, I did a quick grep and
> found that there are some uses in the wild.

Right, I do remember when we had a discussion about this maybe
15 years ago when gcc didn't have the builtins on all architectures
yet, but those versions are long gone, and we never cleaned it up.

> I couldn't find compiler builtins for ___constant_swahb32 nor 
> ___constant_swahw32, so I guess I'll leave them as they are.

Correct. There are also 24-bit and 48-bit swap functions
in include/linux/unaligned.h that have no corresponding builtins.

      Arnd

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/2] include/uapi/linux/swab.h: move default implementation for swab macros into asm-generic
  2025-03-19 21:49       ` Arnd Bergmann
@ 2025-03-20 22:36         ` Ignacio Encinas Rubio
  2025-03-21 10:23           ` Arnd Bergmann
  0 siblings, 1 reply; 11+ messages in thread
From: Ignacio Encinas Rubio @ 2025-03-20 22:36 UTC (permalink / raw)
  To: Arnd Bergmann, Paul Walmsley, Palmer Dabbelt, Alexandre Ghiti
  Cc: Eric Biggers, linux-riscv, linux-kernel, linux-kernel-mentees,
	Shuah Khan, Zhihang Shao, Björn Töpel, Linux-Arch



On 19/3/25 22:49, Arnd Bergmann wrote:
> On Wed, Mar 19, 2025, at 22:37, Ignacio Encinas Rubio wrote:
>> On 19/3/25 22:12, Arnd Bergmann wrote:
>>> On Wed, Mar 19, 2025, at 22:09, Ignacio Encinas wrote:
>>>> Move the default byteswap implementation into asm-generic so that it can
>>>> be included from arch code.
>>>>
>>>> This is required by RISC-V in order to have a fallback implementation
>>>> without duplicating it.
>>>>
>>>> Signed-off-by: Ignacio Encinas <ignacio@iencinas.com>
>>>> ---
>>>>  include/uapi/asm-generic/swab.h | 32 ++++++++++++++++++++++++++++++++
>>>>  include/uapi/linux/swab.h       | 33 +--------------------------------
>>>>  2 files changed, 33 insertions(+), 32 deletions(-)
>>>>
>>>
>>> I think we should just remove these entirely in favor of the
>>> compiler-povided built-ins.
>>
>> Got it. I assumed they existed to explicitly avoid relying on
>> __builtin_bswap as they might not exist. However, I did a quick grep and
>> found that there are some uses in the wild.
> 
> Right, I do remember when we had a discussion about this maybe
> 15 years ago when gcc didn't have the builtins on all architectures
> yet, but those versions are long gone, and we never cleaned it up.

I just had a chance to look at this and it looks a bit more complex than
I initially thought. ___constant_swab macros are used in more places
than I expected, and {little,big}_endian.h define their own macros that
are used elsewhere, ...

It is not clear to me how to proceed here. I could:

  1) Just remove ___constant_swab macros and replace them with
  __builtin_swap everywhere

  2) Go a step further and evaluate removing __constant_htonl and
  relatives

Let me know what you think is the best option :)

I'll resend this series without this patch (and make the RISC-V use
fall back into __builtin_bswap)
 
>> I couldn't find compiler builtins for ___constant_swahb32 nor 
>> ___constant_swahw32, so I guess I'll leave them as they are.
> 
> Correct. There are also 24-bit and 48-bit swap functions
> in include/linux/unaligned.h that have no corresponding builtins.

Thanks for clarifying!

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 2/2] riscv: introduce asm/swab.h
  2025-03-19 21:09 ` [PATCH v2 2/2] riscv: introduce asm/swab.h Ignacio Encinas
@ 2025-03-21  3:37   ` Eric Biggers
  2025-03-21 21:07     ` Ignacio Encinas Rubio
  0 siblings, 1 reply; 11+ messages in thread
From: Eric Biggers @ 2025-03-21  3:37 UTC (permalink / raw)
  To: Ignacio Encinas
  Cc: Paul Walmsley, Palmer Dabbelt, Alexandre Ghiti, Arnd Bergmann,
	linux-riscv, linux-kernel, linux-kernel-mentees, skhan,
	Zhihang Shao, Björn Töpel, linux-arch

On Wed, Mar 19, 2025 at 10:09:46PM +0100, Ignacio Encinas wrote:
> +#define ARCH_SWAB(size) \
> +static __always_inline unsigned long __arch_swab##size(__u##size value) \
> +{									\
> +	unsigned long x = value;					\
> +									\
> +	asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,			\
> +			     RISCV_ISA_EXT_ZBB, 1)			\
> +			     :::: legacy);				\

Is there a reason to use this instead of
riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) which seems to do the same thing,
including using a static branch?

- Eric

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/2] include/uapi/linux/swab.h: move default implementation for swab macros into asm-generic
  2025-03-20 22:36         ` Ignacio Encinas Rubio
@ 2025-03-21 10:23           ` Arnd Bergmann
  2025-03-21 18:38             ` Ignacio Encinas Rubio
  0 siblings, 1 reply; 11+ messages in thread
From: Arnd Bergmann @ 2025-03-21 10:23 UTC (permalink / raw)
  To: Ignacio Encinas, Paul Walmsley, Palmer Dabbelt, Alexandre Ghiti
  Cc: Eric Biggers, linux-riscv, linux-kernel, linux-kernel-mentees,
	Shuah Khan, Zhihang Shao, Björn Töpel, Linux-Arch

On Thu, Mar 20, 2025, at 23:36, Ignacio Encinas Rubio wrote:
> On 19/3/25 22:49, Arnd Bergmann wrote:
>> On Wed, Mar 19, 2025, at 22:37, Ignacio Encinas Rubio wrote:
>>> On 19/3/25 22:12, Arnd Bergmann wrote:
>> Right, I do remember when we had a discussion about this maybe
>> 15 years ago when gcc didn't have the builtins on all architectures
>> yet, but those versions are long gone, and we never cleaned it up.
>
> I just had a chance to look at this and it looks a bit more complex than
> I initially thought. ___constant_swab macros are used in more places
> than I expected, and {little,big}_endian.h define their own macros that
> are used elsewhere, ...
>
> It is not clear to me how to proceed here. I could:
>
>   1) Just remove ___constant_swab macros and replace them with
>   __builtin_swap everywhere
>
>   2) Go a step further and evaluate removing __constant_htonl and
>   relatives
>
> Let me know what you think is the best option :)

I think we can start enabling CONFIG_ARCH_USE_BUILTIN_BSWAP
on all architectures and removing the custom versions
from arch/*/include/uapi/asm/swab.h, which all seem to
predate the compiler builtins and likely produce worse code.

    Arnd

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/2] include/uapi/linux/swab.h: move default implementation for swab macros into asm-generic
  2025-03-21 10:23           ` Arnd Bergmann
@ 2025-03-21 18:38             ` Ignacio Encinas Rubio
  0 siblings, 0 replies; 11+ messages in thread
From: Ignacio Encinas Rubio @ 2025-03-21 18:38 UTC (permalink / raw)
  To: Arnd Bergmann, Paul Walmsley, Palmer Dabbelt, Alexandre Ghiti
  Cc: Eric Biggers, linux-riscv, linux-kernel, linux-kernel-mentees,
	Shuah Khan, Zhihang Shao, Björn Töpel, Linux-Arch



On 21/3/25 11:23, Arnd Bergmann wrote:
> On Thu, Mar 20, 2025, at 23:36, Ignacio Encinas Rubio wrote:
>> On 19/3/25 22:49, Arnd Bergmann wrote:
>>> On Wed, Mar 19, 2025, at 22:37, Ignacio Encinas Rubio wrote:
>>>> On 19/3/25 22:12, Arnd Bergmann wrote:
>>> Right, I do remember when we had a discussion about this maybe
>>> 15 years ago when gcc didn't have the builtins on all architectures
>>> yet, but those versions are long gone, and we never cleaned it up.
>>
>> I just had a chance to look at this and it looks a bit more complex than
>> I initially thought. ___constant_swab macros are used in more places
>> than I expected, and {little,big}_endian.h define their own macros that
>> are used elsewhere, ...
>>
>> It is not clear to me how to proceed here. I could:
>>
>>   1) Just remove ___constant_swab macros and replace them with
>>   __builtin_swap everywhere
>>
>>   2) Go a step further and evaluate removing __constant_htonl and
>>   relatives
>>
>> Let me know what you think is the best option :)
> 
> I think we can start enabling CONFIG_ARCH_USE_BUILTIN_BSWAP
> on all architectures and removing the custom versions
> from arch/*/include/uapi/asm/swab.h, which all seem to
> predate the compiler builtins and likely produce worse code.

This seems fine for some architectures but I don't think we can use
this approach for RISC-V. RISC-V code assumes that the bitmanip 
extension might not be available (see arch/riscv/include/asm/bitops.h).

The current approach [1] is to detect this at boot and patch the kernel 
to adapt it to the actual hardware running it (using specific 
instructions or not).

On the other hand, I tried using __builtin_swap for the RISC-V version 
as an alternative to the "optimized" one (instead of relying on
___constant_swab, see [2]) and I immediately got compilation errors. 

Some architectures seem to require definitions for __bswapsi2 and 
__bswapdi2 [3]. I'm guessing this happens for the architectures that
don't require bit manipulation instructions but have them as extensions.

arm,csky,mips and xtensa seem to fit this description as they 
feature their own __bswapsi2 implementations. Note that they simply
call ___constant_swab or are ___constant_swab written in assembly
language [4] [5].

Unless I'm missing something, it seems to me that using compiler 
builtins (at least for RISC-V, and potentially others) is even more 
problematic than keeping ___constant_swab around. What do you think, 
should we keep patch 1 after all?

We could remove __arch_swab for architectures that always assume bit 
manipulation instructions availability, but then the kernel would fall
back into ___constant_swab when CONFIG_ARCH_USE_BUILTIN_BSWAP=n. Turning
their custom implementations into 

	#define __arch_swabXY __builtin_bswapXY

would solve this issue, but I'm not sure it is an acceptable approach.

Thanks!

[1] https://lore.kernel.org/all/ce034f2b-2f6e-403a-81f1-680af4c72929@ghiti.fr/
[2] https://lore.kernel.org/all/20250319-riscv-swab-v2-2-d53b6d6ab915@iencinas.com/
[3] https://gcc.gnu.org/onlinedocs/gcc-13.3.0/gccint.pdf
[4] https://lore.kernel.org/all/20230512164815.2150839-1-jcmvbkbc@gmail.com/
[5] https://lore.kernel.org/all/1664437198-31260-3-git-send-email-yangtiezhu@loongson.cn/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 2/2] riscv: introduce asm/swab.h
  2025-03-21  3:37   ` Eric Biggers
@ 2025-03-21 21:07     ` Ignacio Encinas Rubio
  0 siblings, 0 replies; 11+ messages in thread
From: Ignacio Encinas Rubio @ 2025-03-21 21:07 UTC (permalink / raw)
  To: Eric Biggers
  Cc: Paul Walmsley, Palmer Dabbelt, Alexandre Ghiti, Arnd Bergmann,
	linux-riscv, linux-kernel, linux-kernel-mentees, skhan,
	Zhihang Shao, Björn Töpel, linux-arch



On 21/3/25 4:37, Eric Biggers wrote:
> On Wed, Mar 19, 2025 at 10:09:46PM +0100, Ignacio Encinas wrote:
>> +#define ARCH_SWAB(size) \
>> +static __always_inline unsigned long __arch_swab##size(__u##size value) \
>> +{									\
>> +	unsigned long x = value;					\
>> +									\
>> +	asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,			\
>> +			     RISCV_ISA_EXT_ZBB, 1)			\
>> +			     :::: legacy);				\
> 
> Is there a reason to use this instead of
> riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) which seems to do the same thing,
> including using a static branch?

I just followed what's already in arch/riscv/include/asm/bitops.h

However, I changed it to

	if(riscv_has_extension_likely(RISCV_ISA_EXT_ZBB)) {
		asm volatile (".option push\n"
			      ".option arch,+zbb\n"
			      "rev8 %0, %1\n"
			      ".option pop\n"
			      : "=r" (x) : "r" (x));
		return x >> (BITS_PER_LONG - size);
	}

	return  ___constant_swab##size(value);

and it seems gcc generates the exact same code. I tested it with 
arch/riscv/lib/csum.c (which uses swab32) and both versions generate the
exact same object file.

This certainly looks easier to read. If there are no complaints I'll
send a v3 using a plain if with riscv_has_extension_likely.

Thanks for pointing it out!

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2025-03-21 21:07 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-19 21:09 [PATCH v2 0/2] Implement endianness swap macros for RISC-V Ignacio Encinas
2025-03-19 21:09 ` [PATCH v2 1/2] include/uapi/linux/swab.h: move default implementation for swab macros into asm-generic Ignacio Encinas
2025-03-19 21:12   ` Arnd Bergmann
2025-03-19 21:37     ` Ignacio Encinas Rubio
2025-03-19 21:49       ` Arnd Bergmann
2025-03-20 22:36         ` Ignacio Encinas Rubio
2025-03-21 10:23           ` Arnd Bergmann
2025-03-21 18:38             ` Ignacio Encinas Rubio
2025-03-19 21:09 ` [PATCH v2 2/2] riscv: introduce asm/swab.h Ignacio Encinas
2025-03-21  3:37   ` Eric Biggers
2025-03-21 21:07     ` Ignacio Encinas Rubio

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox