Skip to contentSkip to conversation

PATCH Applied Yury Norov 4 participants 15 messages May 6 – Jun 26

Subsystems (11)
lore
Share & tools
lore

Yury Norov

author
#0 lore raw

Subject: [PATCH v2 0/5] lib: rework bitreverse

Cleanup bitreverse-related Kconfig dependency mechanism, and add new GENERIC_BITREVERSE option to allow architectures to pick the generic implementation as a fallback for the arch one.

Make RISCV to pick the generic implementation with the absence of ZBKB.

v1: https://lore.kernel.org/all/[email protected]/
v2:
- don't protect headers with the corresponding configs (Arnd, Erik);
- make HAVE_ARCH_BITREVERSE conditional on BITREVERSE;
- make GENERIC_BITREVERSE tri-state (sashiko);
- re-implement GENERIC_BITREVERSE and it's relation to BITREVERSE and HAVE_ARCH_BITREVERSE, thus taking over the authorship;
- RISCV: select GENERIC_BITREVERSE and HAVE_ARCH_BITREVERSE depending on BITREVERSE;

Jinjie Ruan (2):
bitops: Define generic___bitrev8/16/32 for reuse arch/riscv: Add bitrev.h file to support rev8 and brev8

Yury Norov (3):
arch: select HAVE_ARCH_BITREVERSE conditionally on BITREVERSE lib/bitrev: Introduce GENERIC_BITREVERSE
MAINTAINERS: BITOPS: include bitrev.[ch]

 MAINTAINERS                           |  2 ++
 arch/arm/Kconfig                      |  2 +-
 arch/arm64/Kconfig                    |  2 +-
 arch/loongarch/Kconfig                |  2 +-
 arch/mips/Kconfig                     |  2 +-
 arch/riscv/Kconfig                    |  2 ++
 arch/riscv/include/asm/bitrev.h       | 51 +++++++++++++++++++++++++++
 include/asm-generic/bitops/__bitrev.h | 23 ++++++++++++
 include/linux/bitrev.h                | 20 +++--------
 lib/Kconfig                           | 13 +++++++
 lib/Makefile                          |  2 +-
 lib/bitrev.c                          |  3 --
 12 files changed, 100 insertions(+), 24 deletions(-)
 create mode 100644 arch/riscv/include/asm/bitrev.h
 create mode 100644 include/asm-generic/bitops/__bitrev.h
Signature · 1 line
2.51.0
author
#1 lore raw

PATCH v2 1/5 arch: select HAVE_ARCH_BITREVERSE conditionally on BITREVERSE

Architectures may have bit reversal instructions, but if the API not needed, the corresponding option should not be selected because it may lead to generating the unneeded code.

---
 arch/arm/Kconfig       | 2 +-
 arch/arm64/Kconfig     | 2 +-
 arch/loongarch/Kconfig | 2 +-
 arch/mips/Kconfig      | 2 +-
 lib/Kconfig            | 1 +
 5 files changed, 5 insertions(+), 4 deletions(-)
arch/arm/Kconfig +1 -1
index 71fc5dd4123f..0e963e54fe06 100644
@@ -83,7 +83,7 @@ config ARM
 	select HARDIRQS_SW_RESEND
 	select HAS_IOPORT
 	select HAVE_ARCH_AUDITSYSCALL if AEABI && !OABI_COMPAT
-	select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6
+	select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6 && BITREVERSE
 	select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && MMU && (!PREEMPT_RT || !SMP)
 	select HAVE_ARCH_KFENCE if MMU && !XIP_KERNEL
 	select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32 && MMU
arch/arm64/Kconfig +1 -1
index fe60738e5943..f5bb62c2ba9c 100644
@@ -150,7 +150,7 @@ config ARM64
 	select HAVE_ACPI_APEI if (ACPI && EFI)
 	select HAVE_ALIGNED_STRUCT_PAGE
 	select HAVE_ARCH_AUDITSYSCALL
-	select HAVE_ARCH_BITREVERSE
+	select HAVE_ARCH_BITREVERSE if BITREVERSE
 	select HAVE_ARCH_COMPILER_H
 	select HAVE_ARCH_HUGE_VMALLOC
 	select HAVE_ARCH_HUGE_VMAP
arch/loongarch/Kconfig +1 -1
index 3b042dbb2c41..6c3444e31c0e 100644
@@ -115,7 +115,7 @@ config LOONGARCH
 	select HAS_IOPORT
 	select HAVE_ALIGNED_STRUCT_PAGE if 64BIT
 	select HAVE_ARCH_AUDITSYSCALL
-	select HAVE_ARCH_BITREVERSE if 64BIT
+	select HAVE_ARCH_BITREVERSE if 64BIT && BITREVERSE
 	select HAVE_ARCH_JUMP_LABEL
 	select HAVE_ARCH_JUMP_LABEL_RELATIVE
 	select HAVE_ARCH_KASAN if 64BIT
arch/mips/Kconfig +1 -1
index 4364f3dba688..7e1494e0dbfa 100644
@@ -2026,7 +2026,7 @@ config CPU_MIPSR6
 	default y if CPU_MIPS32_R6 || CPU_MIPS64_R6
 	select CPU_HAS_RIXI
 	select CPU_HAS_DIEI if !CPU_DIEI_BROKEN
-	select HAVE_ARCH_BITREVERSE
+	select HAVE_ARCH_BITREVERSE if BITREVERSE
 	select MIPS_ASID_BITS_VARIABLE
 	select MIPS_SPRAM
 
lib/Kconfig +1 -0
index 00a9509636c1..d8e7e89ae320 100644
@@ -58,6 +58,7 @@ config BITREVERSE
 config HAVE_ARCH_BITREVERSE
 	bool
 	default n
+	depends on BITREVERSE
 	help
 	  This option enables the use of hardware bit-reversal instructions on
 	  architectures which support such operations.
Signature · 1 line
2.51.0
author
#7 lore raw

PATCH v2 2/5 lib/bitrev: Introduce GENERIC_BITREVERSE

In reply to Yury Norov

The generic bit reversal implementation is controlled by !HAVE_ARCH_BITREVERSE. This makes it difficult for architectures to provide a hardware-accelerated implementation while being able to fall back to the generic version if needed.

This patch adds GENERIC_BITREVERSE, so bitreverse API is controlled by BITREVERSE, GENERIC_BITREVERSE and HAVE_ARCH_BITREVERSE options. The relationship between them is described as follows:

- BITREVERSE is selected by user code; it's required to generate the API;
- Architectures may select HAVE_ARCH_BITREVERSE and provide an arch implementation in arch/$(ARCH)/include/asm/bitrev.h.
- if HAVE_ARCH_BITREVERSE isn't set, BITREVERSE selects GENERIC_BITREVERSE;
- if GENERIC_BITREVERSE is set and HAVE_ARCH_BITREVERSE is not, the kernel provides generic implementation only, and wires bitrevXX() to it.
- if HAVE_ARCH_BITREVERSE is set and GENERIC_BITREVERSE is not, the arch code provides __arch_bitrevXX(), and it is wired to bitrevXX();
- if both GENERIC_BITREVERSE and HAVE_ARCH_BITREVERSE are selected, the kernel generates generic___bitrev(), but wires bitrev() to the __arch_bitrev().

The last option allows architectures to use generic___bitrev() as a fallback option.

Drivers and core code should never select GENERIC_BITREVERSE or HAVE_ARCH_BITREVERSE explicitly.

Architectures that require generic bitreverse API as a fallback should explicitly enable GENERIC_BITREVERSE together with HAVE_ARCH_BITREVERSE.

---
 lib/Kconfig  | 12 ++++++++++++
 lib/Makefile |  2 +-
 lib/bitrev.c |  3 ---
 3 files changed, 13 insertions(+), 4 deletions(-)
lib/Kconfig +12 -0
index d8e7e89ae320..a33988adfaa3 100644
@@ -54,6 +54,7 @@ config PACKING_KUNIT_TEST
 
 config BITREVERSE
 	tristate
+	select GENERIC_BITREVERSE if !HAVE_ARCH_BITREVERSE
 
 config HAVE_ARCH_BITREVERSE
 	bool
@@ -63,6 +64,17 @@ config HAVE_ARCH_BITREVERSE
 	  This option enables the use of hardware bit-reversal instructions on
 	  architectures which support such operations.
 
+config GENERIC_BITREVERSE
+	tristate
+	depends on BITREVERSE
+	help
+	  Generic bit reversal implementation. Drivers should never enable
+	  it explicitly. Instead, enable BITREVERSE.
+
+	  Architectures may want to select it as a fall-back option for
+	  HAVE_ARCH_BITREVERSE, when the hardware-accelerated bit reverse
+	  instruction set is optional, like RISC-V ZBKB extension.
+
 config ARCH_HAS_STRNCPY_FROM_USER
 	bool
 
lib/Makefile +1 -1
index f33a24bf1c19..23e07d19d01c 100644
@@ -145,7 +145,7 @@ obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o
 obj-$(CONFIG_LIST_HARDENED) += list_debug.o
 obj-$(CONFIG_DEBUG_OBJECTS) += debugobjects.o
 
-obj-$(CONFIG_BITREVERSE) += bitrev.o
+obj-$(CONFIG_GENERIC_BITREVERSE) += bitrev.o
 obj-$(CONFIG_LINEAR_RANGES) += linear_ranges.o
 obj-$(CONFIG_PACKING)	+= packing.o
 obj-$(CONFIG_PACKING_KUNIT_TEST) += packing_test.o
lib/bitrev.c +0 -3
index 81b56e0a7f32..05088231f31f 100644
@@ -1,5 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0-only
-#ifndef CONFIG_HAVE_ARCH_BITREVERSE
 #include <linux/types.h>
 #include <linux/module.h>
 #include <linux/bitrev.h>
@@ -43,5 +42,3 @@ const u8 byte_rev_table[256] = {
 	0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
 };
 EXPORT_SYMBOL_GPL(byte_rev_table);
-
-#endif /* CONFIG_HAVE_ARCH_BITREVERSE */
Signature · 7 lines
2.51.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#10 lore raw

PATCH v2 3/5 bitops: Define generic___bitrev8/16/32 for reuse

In reply to Yury Norov

From: Jinjie Ruan <[email protected]>

Define generic___bitrev8/16/32 using the implementation in <linux/bitrev.h>, so they can be reused in <asm/bitrev.h>, such as RISCV.

---
 include/asm-generic/bitops/__bitrev.h | 23 +++++++++++++++++++++++
 include/linux/bitrev.h                | 20 ++++----------------
 2 files changed, 27 insertions(+), 16 deletions(-)
 create mode 100644 include/asm-generic/bitops/__bitrev.h
include/asm-generic/bitops/__bitrev.h new file +23 -0
new file mode 100644
index 000000000000..4addbde14050
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_GENERIC_BITOPS___BITREV_H_
+#define _ASM_GENERIC_BITOPS___BITREV_H_
+
+#include <asm/types.h>
+
+extern u8 const byte_rev_table[256];
+static __always_inline __attribute_const__ u8 generic___bitrev8(u8 byte)
+{
+	return byte_rev_table[byte];
+}
+
+static __always_inline __attribute_const__ u16 generic___bitrev16(u16 x)
+{
+	return (generic___bitrev8(x & 0xff) << 8) | generic___bitrev8(x >> 8);
+}
+
+static __always_inline __attribute_const__ u32 generic___bitrev32(u32 x)
+{
+	return (generic___bitrev16(x & 0xffff) << 16) | generic___bitrev16(x >> 16);
+}
+
+#endif /* _ASM_GENERIC_BITOPS___BITREV_H_ */
include/linux/bitrev.h +4 -16
index d35b8ec1c485..11620a70e776 100644
@@ -12,22 +12,10 @@
 #define __bitrev8 __arch_bitrev8
 
 #else
-extern u8 const byte_rev_table[256];
-static inline u8 __bitrev8(u8 byte)
-{
-	return byte_rev_table[byte];
-}
-
-static inline u16 __bitrev16(u16 x)
-{
-	return (__bitrev8(x & 0xff) << 8) | __bitrev8(x >> 8);
-}
-
-static inline u32 __bitrev32(u32 x)
-{
-	return (__bitrev16(x & 0xffff) << 16) | __bitrev16(x >> 16);
-}
-
+#include <asm-generic/bitops/__bitrev.h>
+#define __bitrev32 generic___bitrev32
+#define __bitrev16 generic___bitrev16
+#define __bitrev8 generic___bitrev8
 #endif /* CONFIG_HAVE_ARCH_BITREVERSE */
 
 #define __bitrev8x4(x)	(__bitrev32(swab32(x)))
Signature · 7 lines
2.51.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#11 lore raw

PATCH v2 4/5 arch/riscv: Add bitrev.h file to support rev8 and brev8

In reply to Yury Norov

From: Jinjie Ruan <[email protected]>

The RISC-V Bit-manipulation Extension for Cryptography (Zbkb) provides the 'brev8' instruction, which reverses the bits within each byte. Combined with the 'rev8' instruction (from Zbb or Zbkb), which reverses the byte order of a register, we can efficiently implement 16-bit, 32-bit, and (on RV64) 64-bit bit reversal.

This is significantly faster than the default software table-lookup implementation in lib/bitrev.c, as it replaces memory accesses and multiple arithmetic operations with just two or three hardware instructions.

Select HAVE_ARCH_BITREVERSE as well as GENERIC_BITREVERSE, and provide <asm/bitrev.h> to utilize these instructions when the Zbkb extension is available at runtime via the alternatives mechanism.

[Yury: select the options conditionally on BITREVERSE]

---
 arch/riscv/Kconfig              |  2 ++
 arch/riscv/include/asm/bitrev.h | 51 +++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)
 create mode 100644 arch/riscv/include/asm/bitrev.h
arch/riscv/Kconfig +2 -0
index d235396c4514..a708583f785d 100644
@@ -104,6 +104,7 @@ config RISCV
 	select FUNCTION_ALIGNMENT_8B if DYNAMIC_FTRACE_WITH_CALL_OPS
 	select GENERIC_ARCH_TOPOLOGY
 	select GENERIC_ATOMIC64 if !64BIT
+	select GENERIC_BITREVERSE if HAVE_ARCH_BITREVERSE
 	select GENERIC_CLOCKEVENTS_BROADCAST if SMP
 	select GENERIC_CPU_DEVICES
 	select GENERIC_CPU_VULNERABILITIES
@@ -128,6 +129,7 @@ config RISCV
 	select HAS_IOPORT if MMU
 	select HAVE_ALIGNED_STRUCT_PAGE
 	select HAVE_ARCH_AUDITSYSCALL
+	select HAVE_ARCH_BITREVERSE if RISCV_ISA_ZBKB && BITREVERSE
 	select HAVE_ARCH_HUGE_VMALLOC if HAVE_ARCH_HUGE_VMAP
 	select HAVE_ARCH_HUGE_VMAP if MMU && 64BIT
 	select HAVE_ARCH_JUMP_LABEL
arch/riscv/include/asm/bitrev.h new file +51 -0

51 lines not on this page · raw message

Signature · 7 lines
2.51.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#14 lore raw

PATCH v2 5/5 MAINTAINERS: BITOPS: include bitrev.[ch]

In reply to Yury Norov

Arch bitrev API is covered in MAINTAINERS under the BITOPS entry, while generic bitrev is unmaintained. Move it under BITOPS too.

---
 MAINTAINERS | 2 ++
 1 file changed, 2 insertions(+)
MAINTAINERS +2 -0
index 882214b0e7db..30214ac2f06d 100644
@@ -4549,7 +4549,9 @@ F:	arch/*/lib/bitops.c
 F:	include/asm-generic/bitops
 F:	include/asm-generic/bitops.h
 F:	include/linux/bitops.h
+F:	include/linux/bitrev.h
 F:	include/linux/count_zeros.h
+F:	lib/bitrev.c
 F:	lib/hweight.c
 F:	lib/test_bitops.c
 F:	lib/tests/bitops_kunit.c
Signature · 7 lines
2.51.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#2 lore raw

Re: [PATCH v2 1/5] arch: select HAVE_ARCH_BITREVERSE conditionally on BITREVERSE

In reply to Yury Norov
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The commit introduces a performance regression in fbdev by making the hardware capability flag…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The commit introduces a performance regression in fbdev by making the hardware capability flag HAVE_ARCH_BITREVERSE dependent on the BITREVERSE API, based on a factually incorrect premise. --

commit 62f45d1f26fdd43913871c56d8d0139e89cc923a
Author: Yury Norov <[email protected]>

arch: select HAVE_ARCH_BITREVERSE conditionally on BITREVERSE

This commit modifies architecture Kconfig files to only select HAVE_ARCH_BITREVERSE if BITREVERSE is enabled. The stated goal is to avoid generating unneeded code on architectures that have bit reversal instructions but do not currently need the API.

The commit message states that selecting the option when the API is not needed "may lead to generating the unneeded code." Is this description accurate?

Looking at the implementation, HAVE_ARCH_BITREVERSE only enables inline assembly macros in linux/bitrev.h. The software fallback table in lib/bitrev.c is strictly gated by obj-$(CONFIG_BITREVERSE) += bitrev.o in lib/Makefile, so no out-of-line code is generated when BITREVERSE=n.

lib/Kconfig config BITREVERSE · line 64

Quoted 12 lines
lib/Kconfig +1 -0
index 00a9509636c18..d8e7e89ae3205 100644
@@ -58,6 +58,7 @@ config BITREVERSE
 config HAVE_ARCH_BITREVERSE
 	bool
 	default n
+	depends on BITREVERSE
 	help
 	  This option enables the use of hardware bit-reversal instructions on
 	  architectures which support such operations.

Does tying a hardware capability flag (HAVE_ARCH_BITREVERSE) to a software API (BITREVERSE) cause unintended performance regressions elsewhere?

For example, drivers/video/fbdev/core/fb_draw.h opportunistically checks for the hardware capability:

#if defined(CONFIG_HAVE_ARCH_BITREVERSE)

The fbdev core intentionally avoids selecting BITREVERSE to prevent pulling in the 256-byte lookup table, but uses the fast, native hardware instructions if the architecture supports them.

With this patch applied, if a user builds a kernel where BITREVERSE=n, fb_draw.h will believe the architecture lacks hardware support. Won't this force fbdev to fall back to the much slower manual bit-twiddling function fb_comp() in its tight pixel-drawing loops?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#8 lore raw

Re: [PATCH v2 2/5] lib/bitrev: Introduce GENERIC_BITREVERSE

In reply to Yury Norov
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The commit's instructions for architectures to explicitly enable GENERIC_BITREVERSE will trigger…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The commit's instructions for architectures to explicitly enable GENERIC_BITREVERSE will trigger Kconfig unmet dependency warnings. --

commit dfa2feef3c11e8e7f890a24d4a12c9d0c88f4918
Author: Yury Norov <[email protected]>

lib/bitrev: Introduce GENERIC_BITREVERSE

This commit introduces GENERIC_BITREVERSE to allow architectures to provide hardware-accelerated bit reversal implementations while still being able to fall back to the generic version. It modifies Kconfig and the Makefile to make GENERIC_BITREVERSE selectable.

Quoted 2 lines

Architectures that require generic bitreverse API as a fallback should explicitly enable GENERIC_BITREVERSE together with HAVE_ARCH_BITREVERSE.

Will this instruction for architectures to explicitly enable GENERIC_BITREVERSE trigger Kconfig unmet dependency warnings?

Quoted 4 lines
lib/Kconfig +0 -0
index d8e7e89ae3205..a33988adfaa3e 100644

[ ... ]

lib/Kconfig config HAVE_ARCH_BITREVERSE · line 69

Quoted 7 lines
lib/Kconfig +3 -0
@@ -63,6 +64,17 @@ config HAVE_ARCH_BITREVERSE
 	  This option enables the use of hardware bit-reversal instructions on
 	  architectures which support such operations.
 
+config GENERIC_BITREVERSE
+	tristate
+	depends on BITREVERSE

If an architecture follows the instruction in the commit message and uses select GENERIC_BITREVERSE unconditionally, Kconfig will force it to y even when BITREVERSE is n or m.

Even if an architecture attempts to select it conditionally via select GENERIC_BITREVERSE if HAVE_ARCH_BITREVERSE, a warning will still occur when BITREVERSE=m. Kconfig promotes the boolean condition HAVE_ARCH_BITREVERSE to y, which in turn forces the tristate GENERIC_BITREVERSE to y, violating its dependency on BITREVERSE (m).

Should architectures instead use select GENERIC_BITREVERSE if BITREVERSE to properly inherit the dependency, or should the Kconfig dependencies be restructured?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#12 lore raw

Re: [PATCH v2 4/5] arch/riscv: Add bitrev.h file to support rev8 and brev8

In reply to Yury Norov
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Selecting GENERIC_BITREVERSE conditionally on HAVE_ARCH_BITREVERSE causes a Kconfig unmet direct…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Selecting GENERIC_BITREVERSE conditionally on HAVE_ARCH_BITREVERSE causes a Kconfig unmet direct dependency warning when BITREVERSE is built as a module. --

commit 500623b90fe7e30bdea603dfb1e1f54158143ee4
Author: Jinjie Ruan <[email protected]>

arch/riscv: Add bitrev.h file to support rev8 and brev8

This commit utilizes the RISC-V Zbkb brev8 and rev8 instructions to efficiently implement bit reversal. It conditionally selects HAVE_ARCH_BITREVERSE and GENERIC_BITREVERSE when the extension is available.

arch/riscv/Kconfig config RISCV · line 107

Quoted 9 lines
arch/riscv/Kconfig +1 -0
index d235396c45141..a708583f785db 100644
@@ -104,6 +104,7 @@ config RISCV
 	select FUNCTION_ALIGNMENT_8B if DYNAMIC_FTRACE_WITH_CALL_OPS
 	select GENERIC_ARCH_TOPOLOGY
 	select GENERIC_ATOMIC64 if !64BIT
+	select GENERIC_BITREVERSE if HAVE_ARCH_BITREVERSE

Will this cause an unmet direct dependency warning if BITREVERSE is built as a module?

In lib/Kconfig, GENERIC_BITREVERSE is a tristate symbol that has a dependency on BITREVERSE. Because HAVE_ARCH_BITREVERSE is a boolean symbol, if BITREVERSE is configured as a module (=m), the condition for HAVE_ARCH_BITREVERSE below evaluates to m but Kconfig automatically promotes it to y.

This forces GENERIC_BITREVERSE to y while its required dependency BITREVERSE is only m.

Should this select also inherit the dependency (e.g., select GENERIC_BITREVERSE if HAVE_ARCH_BITREVERSE && BITREVERSE) to cap the selection strength and avoid the warning?

[ ... ]

Quoted 5 lines
arch/riscv/Kconfig +1 -0
@@ -128,6 +129,7 @@ config RISCV
 	select HAS_IOPORT if MMU
 	select HAVE_ALIGNED_STRUCT_PAGE
 	select HAVE_ARCH_AUDITSYSCALL
+	select HAVE_ARCH_BITREVERSE if RISCV_ISA_ZBKB && BITREVERSE
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author
#3 lore raw

Re: [PATCH v2 1/5] arch: select HAVE_ARCH_BITREVERSE conditionally on BITREVERSE

In reply to Yury Norov

Ping?

Quoted 79 lines · Yury Norov

Architectures may have bit reversal instructions, but if the API not needed, the corresponding option should not be selected because it may lead to generating the unneeded code.

---
 arch/arm/Kconfig       | 2 +-
 arch/arm64/Kconfig     | 2 +-
 arch/loongarch/Kconfig | 2 +-
 arch/mips/Kconfig      | 2 +-
 lib/Kconfig            | 1 +
 5 files changed, 5 insertions(+), 4 deletions(-)
arch/arm/Kconfig +1 -1
index 71fc5dd4123f..0e963e54fe06 100644
@@ -83,7 +83,7 @@ config ARM
 	select HARDIRQS_SW_RESEND
 	select HAS_IOPORT
 	select HAVE_ARCH_AUDITSYSCALL if AEABI && !OABI_COMPAT
-	select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6
+	select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6 && BITREVERSE
 	select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && MMU && (!PREEMPT_RT || !SMP)
 	select HAVE_ARCH_KFENCE if MMU && !XIP_KERNEL
 	select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32 && MMU
arch/arm64/Kconfig +1 -1
index fe60738e5943..f5bb62c2ba9c 100644
@@ -150,7 +150,7 @@ config ARM64
 	select HAVE_ACPI_APEI if (ACPI && EFI)
 	select HAVE_ALIGNED_STRUCT_PAGE
 	select HAVE_ARCH_AUDITSYSCALL
-	select HAVE_ARCH_BITREVERSE
+	select HAVE_ARCH_BITREVERSE if BITREVERSE
 	select HAVE_ARCH_COMPILER_H
 	select HAVE_ARCH_HUGE_VMALLOC
 	select HAVE_ARCH_HUGE_VMAP
arch/loongarch/Kconfig +1 -1
index 3b042dbb2c41..6c3444e31c0e 100644
@@ -115,7 +115,7 @@ config LOONGARCH
 	select HAS_IOPORT
 	select HAVE_ALIGNED_STRUCT_PAGE if 64BIT
 	select HAVE_ARCH_AUDITSYSCALL
-	select HAVE_ARCH_BITREVERSE if 64BIT
+	select HAVE_ARCH_BITREVERSE if 64BIT && BITREVERSE
 	select HAVE_ARCH_JUMP_LABEL
 	select HAVE_ARCH_JUMP_LABEL_RELATIVE
 	select HAVE_ARCH_KASAN if 64BIT
arch/mips/Kconfig +1 -1
index 4364f3dba688..7e1494e0dbfa 100644
@@ -2026,7 +2026,7 @@ config CPU_MIPSR6
 	default y if CPU_MIPS32_R6 || CPU_MIPS64_R6
 	select CPU_HAS_RIXI
 	select CPU_HAS_DIEI if !CPU_DIEI_BROKEN
-	select HAVE_ARCH_BITREVERSE
+	select HAVE_ARCH_BITREVERSE if BITREVERSE
 	select MIPS_ASID_BITS_VARIABLE
 	select MIPS_SPRAM
 
lib/Kconfig +1 -0
index 00a9509636c1..d8e7e89ae320 100644
@@ -58,6 +58,7 @@ config BITREVERSE
 config HAVE_ARCH_BITREVERSE
 	bool
 	default n
+	depends on BITREVERSE
 	help
 	  This option enables the use of hardware bit-reversal instructions on
 	  architectures which support such operations.
Signature · 1 line
2.51.0
author
#4 lore raw

Re: [PATCH v2 1/5] arch: select HAVE_ARCH_BITREVERSE conditionally on BITREVERSE

Ping again?

Quoted 82 lines · Yury Norov

Ping?

Quoted 79 lines · Yury Norov

Architectures may have bit reversal instructions, but if the API not needed, the corresponding option should not be selected because it may lead to generating the unneeded code.

---
 arch/arm/Kconfig       | 2 +-
 arch/arm64/Kconfig     | 2 +-
 arch/loongarch/Kconfig | 2 +-
 arch/mips/Kconfig      | 2 +-
 lib/Kconfig            | 1 +
 5 files changed, 5 insertions(+), 4 deletions(-)
arch/arm/Kconfig +1 -1
index 71fc5dd4123f..0e963e54fe06 100644
@@ -83,7 +83,7 @@ config ARM
 	select HARDIRQS_SW_RESEND
 	select HAS_IOPORT
 	select HAVE_ARCH_AUDITSYSCALL if AEABI && !OABI_COMPAT
-	select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6
+	select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6 && BITREVERSE
 	select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && MMU && (!PREEMPT_RT || !SMP)
 	select HAVE_ARCH_KFENCE if MMU && !XIP_KERNEL
 	select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32 && MMU
arch/arm64/Kconfig +1 -1
index fe60738e5943..f5bb62c2ba9c 100644
@@ -150,7 +150,7 @@ config ARM64
 	select HAVE_ACPI_APEI if (ACPI && EFI)
 	select HAVE_ALIGNED_STRUCT_PAGE
 	select HAVE_ARCH_AUDITSYSCALL
-	select HAVE_ARCH_BITREVERSE
+	select HAVE_ARCH_BITREVERSE if BITREVERSE
 	select HAVE_ARCH_COMPILER_H
 	select HAVE_ARCH_HUGE_VMALLOC
 	select HAVE_ARCH_HUGE_VMAP
arch/loongarch/Kconfig +1 -1
index 3b042dbb2c41..6c3444e31c0e 100644
@@ -115,7 +115,7 @@ config LOONGARCH
 	select HAS_IOPORT
 	select HAVE_ALIGNED_STRUCT_PAGE if 64BIT
 	select HAVE_ARCH_AUDITSYSCALL
-	select HAVE_ARCH_BITREVERSE if 64BIT
+	select HAVE_ARCH_BITREVERSE if 64BIT && BITREVERSE
 	select HAVE_ARCH_JUMP_LABEL
 	select HAVE_ARCH_JUMP_LABEL_RELATIVE
 	select HAVE_ARCH_KASAN if 64BIT
arch/mips/Kconfig +1 -1
index 4364f3dba688..7e1494e0dbfa 100644
@@ -2026,7 +2026,7 @@ config CPU_MIPSR6
 	default y if CPU_MIPS32_R6 || CPU_MIPS64_R6
 	select CPU_HAS_RIXI
 	select CPU_HAS_DIEI if !CPU_DIEI_BROKEN
-	select HAVE_ARCH_BITREVERSE
+	select HAVE_ARCH_BITREVERSE if BITREVERSE
 	select MIPS_ASID_BITS_VARIABLE
 	select MIPS_SPRAM
 
lib/Kconfig +1 -0
index 00a9509636c1..d8e7e89ae320 100644
@@ -58,6 +58,7 @@ config BITREVERSE
 config HAVE_ARCH_BITREVERSE
 	bool
 	default n
+	depends on BITREVERSE
 	help
 	  This option enables the use of hardware bit-reversal instructions on
 	  architectures which support such operations.
Signature · 1 line
2.51.0
#5 lore raw

Re: [PATCH v2 1/5] arch: select HAVE_ARCH_BITREVERSE conditionally on BITREVERSE

In reply to Yury Norov
Quoted 18 lines · Yury Norov

Architectures may have bit reversal instructions, but if the API not needed, the corresponding option should not be selected because it may lead to generating the unneeded code.

---
 arch/arm/Kconfig       | 2 +-
 arch/arm64/Kconfig     | 2 +-
 arch/loongarch/Kconfig | 2 +-
 arch/mips/Kconfig      | 2 +-
 lib/Kconfig            | 1 +
 5 files changed, 5 insertions(+), 4 deletions(-)
arch/arm/Kconfig +0 -0
index 71fc5dd4123f..0e963e54fe06 100644

arch/arm/Kconfig config ARM · line 86

Quoted 5 lines
arch/arm/Kconfig +1 -1
@@ -83,7 +83,7 @@ config ARM
 	select HARDIRQS_SW_RESEND
 	select HAS_IOPORT
 	select HAVE_ARCH_AUDITSYSCALL if AEABI && !OABI_COMPAT
-	select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6
+	select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6 && BITREVERSE

I think there is a semantic confusion:

HAVE_ARCH_BITREVERSE indicates that the architecture itself has an efficient bit‑reverse implementation (e.g., the RBIT instruction on ARMv7). It is a hardware capability declaration and should not depend on a higher‑level feature option like BITREVERSE.

Quoted 13 lines
arch/arm/Kconfig +0 -0
 	select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && MMU && (!PREEMPT_RT || !SMP)
 	select HAVE_ARCH_KFENCE if MMU && !XIP_KERNEL
 	select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32 && MMU
arch/arm64/Kconfig +1 -1
index fe60738e5943..f5bb62c2ba9c 100644
@@ -150,7 +150,7 @@ config ARM64
 	select HAVE_ACPI_APEI if (ACPI && EFI)
 	select HAVE_ALIGNED_STRUCT_PAGE
 	select HAVE_ARCH_AUDITSYSCALL
-	select HAVE_ARCH_BITREVERSE
+	select HAVE_ARCH_BITREVERSE if BITREVERSE

[..]

Quoted 6 lines
arch/arm64/Kconfig +1 -0
 	bool
 	default n
+	depends on BITREVERSE
 	help
 	  This option enables the use of hardware bit-reversal instructions on
 	  architectures which support such operations.
#13 lore raw

Re: [PATCH v2 4/5] arch/riscv: Add bitrev.h file to support rev8 and brev8

In reply to Yury Norov
Quoted 35 lines · Yury Norov

From: Jinjie Ruan <[email protected]>

The RISC-V Bit-manipulation Extension for Cryptography (Zbkb) provides the 'brev8' instruction, which reverses the bits within each byte. Combined with the 'rev8' instruction (from Zbb or Zbkb), which reverses the byte order of a register, we can efficiently implement 16-bit, 32-bit, and (on RV64) 64-bit bit reversal.

This is significantly faster than the default software table-lookup implementation in lib/bitrev.c, as it replaces memory accesses and multiple arithmetic operations with just two or three hardware instructions.

Select HAVE_ARCH_BITREVERSE as well as GENERIC_BITREVERSE, and provide <asm/bitrev.h> to utilize these instructions when the Zbkb extension is available at runtime via the alternatives mechanism.

[Yury: select the options conditionally on BITREVERSE]

---
 arch/riscv/Kconfig              |  2 ++
 arch/riscv/include/asm/bitrev.h | 51 +++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)
 create mode 100644 arch/riscv/include/asm/bitrev.h
arch/riscv/Kconfig +0 -0
index d235396c4514..a708583f785d 100644

arch/riscv/Kconfig config RISCV · line 107

Quoted 4 lines
arch/riscv/Kconfig +1 -0
@@ -104,6 +104,7 @@ config RISCV
 	select FUNCTION_ALIGNMENT_8B if DYNAMIC_FTRACE_WITH_CALL_OPS
 	select GENERIC_ARCH_TOPOLOGY
 	select GENERIC_ATOMIC64 if !64BIT
+	select GENERIC_BITREVERSE if HAVE_ARCH_BITREVERSE

Maybe 'select GENERIC_BITREVERSE if BITREVERSE' ?

Quoted 68 lines
arch/riscv/Kconfig +1 -0
 	select GENERIC_CLOCKEVENTS_BROADCAST if SMP
 	select GENERIC_CPU_DEVICES
 	select GENERIC_CPU_VULNERABILITIES
@@ -128,6 +129,7 @@ config RISCV
 	select HAS_IOPORT if MMU
 	select HAVE_ALIGNED_STRUCT_PAGE
 	select HAVE_ARCH_AUDITSYSCALL
+	select HAVE_ARCH_BITREVERSE if RISCV_ISA_ZBKB && BITREVERSE
 	select HAVE_ARCH_HUGE_VMALLOC if HAVE_ARCH_HUGE_VMAP
 	select HAVE_ARCH_HUGE_VMAP if MMU && 64BIT
 	select HAVE_ARCH_JUMP_LABEL
arch/riscv/include/asm/bitrev.h new file +51 -0
new file mode 100644
index 000000000000..4b9b8d34cc3b
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_BITREV_H
+#define __ASM_BITREV_H
+
+#include <linux/types.h>
+#include <asm/cpufeature-macros.h>
+#include <asm/hwcap.h>
+#include <asm-generic/bitops/__bitrev.h>
+
+static __always_inline __attribute_const__ u32 __arch_bitrev32(u32 x)
+{
+	unsigned long result;
+
+	if (!riscv_has_extension_likely(RISCV_ISA_EXT_ZBKB))
+		return generic___bitrev32(x);
+
+	asm volatile(
+		".option push\n"
+		".option arch,+zbkb\n"
+		"rev8 %0, %1\n"
+		"brev8 %0, %0\n"
+		".option pop"
+		: "=r" (result) : "r" ((long)x)
+	);
+
+	return result >> (__riscv_xlen - 32);
+}
+
+static __always_inline __attribute_const__ u16 __arch_bitrev16(u16 x)
+{
+	return __arch_bitrev32(x) >> 16;
+}
+
+static __always_inline __attribute_const__ u8 __arch_bitrev8(u8 x)
+{
+	unsigned long result;
+
+	if (!riscv_has_extension_likely(RISCV_ISA_EXT_ZBKB))
+		return generic___bitrev8(x);
+
+	asm volatile(
+		".option push\n"
+		".option arch,+zbkb\n"
+		"brev8 %0, %1\n"
+		".option pop"
+		: "=r" (result) : "r" ((long)x)
+	);
+
+	return result;
+}
+#endif
#9 lore raw

Re: [PATCH v2 2/5] lib/bitrev: Introduce GENERIC_BITREVERSE

In reply to Yury Norov
Quoted 49 lines · Yury Norov

The generic bit reversal implementation is controlled by !HAVE_ARCH_BITREVERSE. This makes it difficult for architectures to provide a hardware-accelerated implementation while being able to fall back to the generic version if needed.

This patch adds GENERIC_BITREVERSE, so bitreverse API is controlled by BITREVERSE, GENERIC_BITREVERSE and HAVE_ARCH_BITREVERSE options. The relationship between them is described as follows:

- BITREVERSE is selected by user code; it's required to generate the API;
- Architectures may select HAVE_ARCH_BITREVERSE and provide an arch implementation in arch/$(ARCH)/include/asm/bitrev.h.
- if HAVE_ARCH_BITREVERSE isn't set, BITREVERSE selects GENERIC_BITREVERSE;
- if GENERIC_BITREVERSE is set and HAVE_ARCH_BITREVERSE is not, the kernel provides generic implementation only, and wires bitrevXX() to it.
- if HAVE_ARCH_BITREVERSE is set and GENERIC_BITREVERSE is not, the arch code provides __arch_bitrevXX(), and it is wired to bitrevXX();
- if both GENERIC_BITREVERSE and HAVE_ARCH_BITREVERSE are selected, the kernel generates generic___bitrev(), but wires bitrev() to the __arch_bitrev().

The last option allows architectures to use generic___bitrev() as a fallback option.

Drivers and core code should never select GENERIC_BITREVERSE or HAVE_ARCH_BITREVERSE explicitly.

Architectures that require generic bitreverse API as a fallback should explicitly enable GENERIC_BITREVERSE together with HAVE_ARCH_BITREVERSE.

---
 lib/Kconfig  | 12 ++++++++++++
 lib/Makefile |  2 +-
 lib/bitrev.c |  3 ---
 3 files changed, 13 insertions(+), 4 deletions(-)
lib/Kconfig +1 -0
index d8e7e89ae320..a33988adfaa3 100644
@@ -54,6 +54,7 @@ config PACKING_KUNIT_TEST
 
 config BITREVERSE
 	tristate
+	select GENERIC_BITREVERSE if !HAVE_ARCH_BITREVERSE
 
 config HAVE_ARCH_BITREVERSE
 	bool

lib/Kconfig config HAVE_ARCH_BITREVERSE · line 72

Quoted 9 lines
lib/Kconfig +6 -0
@@ -63,6 +64,17 @@ config HAVE_ARCH_BITREVERSE
 	  This option enables the use of hardware bit-reversal instructions on
 	  architectures which support such operations.
 
+config GENERIC_BITREVERSE
+	tristate
+	depends on BITREVERSE
+	help
+	  Generic bit reversal implementation. Drivers should never enable
+	  it explicitly. Instead, enable BITREVERSE.

The later riscv implementation force GENERIC_BITREVERSE even when HAVE_ARCH_BITREVERSE=y but triggers a Kconfig unmet direct dependency warning as below:

warning: (RISCV) selects GENERIC_BITREVERSE which has unmet direct dependencies (BITREVERSE)

This happens because select ignores depends on clauses and can force a tristate symbol to y even when its dependency BITREVERSE is only =m. The warning is a symptom of an invalid dependency chain.

Link:
https://lore.kernel.org/all/[email protected]/

Quoted 37 lines
lib/Kconfig +5 -0
+
+	  Architectures may want to select it as a fall-back option for
+	  HAVE_ARCH_BITREVERSE, when the hardware-accelerated bit reverse
+	  instruction set is optional, like RISC-V ZBKB extension.
+
 config ARCH_HAS_STRNCPY_FROM_USER
 	bool
 
lib/Makefile +1 -1
index f33a24bf1c19..23e07d19d01c 100644
@@ -145,7 +145,7 @@ obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o
 obj-$(CONFIG_LIST_HARDENED) += list_debug.o
 obj-$(CONFIG_DEBUG_OBJECTS) += debugobjects.o
 
-obj-$(CONFIG_BITREVERSE) += bitrev.o
+obj-$(CONFIG_GENERIC_BITREVERSE) += bitrev.o
 obj-$(CONFIG_LINEAR_RANGES) += linear_ranges.o
 obj-$(CONFIG_PACKING)	+= packing.o
 obj-$(CONFIG_PACKING_KUNIT_TEST) += packing_test.o
lib/bitrev.c +0 -3
index 81b56e0a7f32..05088231f31f 100644
@@ -1,5 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0-only
-#ifndef CONFIG_HAVE_ARCH_BITREVERSE
 #include <linux/types.h>
 #include <linux/module.h>
 #include <linux/bitrev.h>
@@ -43,5 +42,3 @@ const u8 byte_rev_table[256] = {
 	0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
 };
 EXPORT_SYMBOL_GPL(byte_rev_table);
-
-#endif /* CONFIG_HAVE_ARCH_BITREVERSE */
#6 lore raw

Re: [PATCH v2 1/5] arch: select HAVE_ARCH_BITREVERSE conditionally on BITREVERSE

In reply to Yury Norov
This series was applied to riscv/linux.git (fixes) by Yury Norov <[email protected]>:

Hello:

This series was applied to riscv/linux.git (fixes) by Yury Norov <[email protected]>:

Quoted 4 lines · On Wed, 6 May 2026 13:52:02 -0400 you wrote:

Architectures may have bit reversal instructions, but if the API not needed, the corresponding option should not be selected because it may lead to generating the unneeded code.

Quoted 8 lines
---
 arch/arm/Kconfig       | 2 +-
 arch/arm64/Kconfig     | 2 +-
 arch/loongarch/Kconfig | 2 +-
 arch/mips/Kconfig      | 2 +-
 lib/Kconfig            | 1 +
 5 files changed, 5 insertions(+), 4 deletions(-)

Here is the summary with links:
- [v2,1/5] arch: select HAVE_ARCH_BITREVERSE conditionally on BITREVERSE

    https://git.kernel.org/riscv/c/42d9c75e8b9c
  - [v2,2/5] lib/bitrev: Introduce GENERIC_BITREVERSE
    https://git.kernel.org/riscv/c/00751d655ece
  - [v2,3/5] bitops: Define generic___bitrev8/16/32 for reuse
    https://git.kernel.org/riscv/c/83aede8131af
  - [v2,4/5] arch/riscv: Add bitrev.h file to support rev8 and brev8
    https://git.kernel.org/riscv/c/e8620bd7e5e0
  - [v2,5/5] MAINTAINERS: BITOPS: include bitrev.[ch]
    https://git.kernel.org/riscv/c/7b2c5b4e43aa

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html