Re: [PATCH v2 2/2] zstd: use cpu_feature_enabled() for in-kernel BMI2 dispatch

From: Usama Arif

Date: Mon Aug 31 2026 - 08:10:59 EST




On 31/08/2026 00:06, Linus Torvalds wrote:
> On Sun, 30 Aug 2026 at 15:21, Usama Arif <usama.arif@xxxxxxxxx> wrote:
>>
>> Add ZSTD_USE_BMI2() and use it at every runtime BMI2/default selector.
>
> Thanks, this looks sane to me.
>
> I do still react to a couple of places. Notably, this part (repeated a
> couple of times):
>
>> +#if !defined(ZSTD_USE_KERNEL_CPU_FEATURES)
>> cctx->bmi2 = ZSTD_cpuSupportsBmi2();
>> +#endif
>
> should probably be an "set state" macro the same way ZSTD_USE_BMI2()
> is now a "get state" macro.
>

Ack, have added ZSTD_SET_BMI2():

#if !DYNAMIC_BMI2
# define ZSTD_USE_BMI2(bmi2) 0
# define ZSTD_SET_BMI2(state, value) do { } while (0)
#elif defined(ZSTD_USE_KERNEL_CPU_FEATURES)
# define ZSTD_USE_BMI2(bmi2) cpu_feature_enabled(X86_FEATURE_BMI2)
# define ZSTD_SET_BMI2(state, value) do { } while (0)
#else
# define ZSTD_USE_BMI2(bmi2) (bmi2)
# define ZSTD_SET_BMI2(state, value) do { (state) = (value); } while (0)
#endif

will use above in next revision.
> And:
>
>> +ccflags-$(CONFIG_X86) += -DZSTD_USE_KERNEL_CPU_FEATURES
>
> We don't have anything like this for any other ZSTD defines, so my gut
> feel is that either this is in the wrong place, and it should be in
> something like
>
> lib/zstd/common/zstd_deps.h
>
> which already has kernel-specific stuff in it, or it should just use
> #ifdef __KERNEL__ like we use elsewhere for things that are also used
> by user space.
>

Ack, removed it from the Makefile for next revision.

__KERNEL__ alone was insufficient because it is also defined for the x86
compressed boot code. That environment defines __DISABLE_EXPORTS and cannot
use the normal alternatives infrastructure, so it retains zstd’s raw-CPUID
path.

I have added

#if defined(__KERNEL__) && defined(CONFIG_X86) && \
!defined(__DISABLE_EXPORTS)
#define ZSTD_USE_KERNEL_CPU_FEATURES
#endif

in lib/zstd/common/zstd_deps.h for the next revision.


> The whole situation with ZSTD configuration is a bit strange. I note
> that there are things like ZSTD_ARCH_ARM_NEON that the kernel never
> uses.
>
> Why is "DYNAMIC_BMI2" a settimg but things like that
> ZSTD_ARCH_X86_SSE2 and ZSTD_ARCH_ARM_NEON are never set? This all
> looks messy.
>

DYNAMIC_BMI2 is defined in lib/zstd/common/portability_macros.h.

ZSTD_ARCH_X86_SSE2 and ZSTD_ARCH_ARM_NEON are upstream zstd configuration
knobs. The kernel import is generated with ZSTD_NO_INTRINSICS [1], so their
automatic definitions are disabled even though some guarded references
remain in the imported source. The kernel cannot use SSE/NEON transparently
because that requires architecture-specific SIMD/FPU state management, whereas
BMI2 operates only on general-purpose registers.

[1] https://github.com/facebook/zstd/blob/dev/contrib/linux-kernel/Makefile#L30

> Or is there some hidden config I didn't find?
>
> Strange.
>
> Linus