Re: [PATCH v4 11/36] x86/bugs: Restructure spectre_v2_user mitigation

From: Josh Poimboeuf
Date: Thu Apr 10 2025 - 12:27:37 EST


On Mon, Mar 10, 2025 at 11:39:58AM -0500, David Kaplan wrote:
> static inline bool spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)
> @@ -1446,10 +1436,10 @@ static inline bool spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)
> return spectre_v2_in_eibrs_mode(mode) || mode == SPECTRE_V2_IBRS;
> }
>
> +

Extra newline here.

> case SPECTRE_V2_USER_CMD_SECCOMP:
> - case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
> if (IS_ENABLED(CONFIG_SECCOMP))
> - mode = SPECTRE_V2_USER_SECCOMP;
> + spectre_v2_user_ibpb = SPECTRE_V2_USER_SECCOMP;
> else
> - mode = SPECTRE_V2_USER_PRCTL;
> + spectre_v2_user_ibpb = SPECTRE_V2_USER_PRCTL;
> + spectre_v2_user_stibp = spectre_v2_user_ibpb;
> + break;
> + case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
> + spectre_v2_user_ibpb = SPECTRE_V2_USER_STRICT;
> + spectre_v2_user_stibp = SPECTRE_V2_USER_PRCTL;
> break;
> }

For SPECTRE_V2_USER_CMD_SECCOMP_IBPB, shouldn't spectre_v2_user_stibp
be SPECTRE_V2_USER_SECCOMP if CONFIG_SECCOMP?

Also I think spectre_v2_user_ibpb needs to be cleared here if
X86_FEATURE_IBPB isn't set. And similar for spectre_v2_user_stibp and
X86_FEATURE_STIBP.

> - /* Initialize Indirect Branch Prediction Barrier */
> - if (boot_cpu_has(X86_FEATURE_IBPB)) {
> - static_branch_enable(&switch_vcpu_ibpb);
> + /*
> + * At this point, an STIBP mode other than "off" has been set.
> + * If STIBP support is not being forced, check if STIBP always-on
> + * is preferred.
> + */
> + if (spectre_v2_user_stibp != SPECTRE_V2_USER_STRICT &&
> + boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
> + spectre_v2_user_stibp = SPECTRE_V2_USER_STRICT_PREFERRED;

Instead of checking for !SPECTRE_V2_USER_STRICT it would probably be
better to check for SPECTRE_V2_USER_PRCTL or SPECTRE_V2_USER_SECCOMP
directly.

Then the returns added to the SPECTRE_V2_USER_CMD_AUTO case in
"x86/bugs: Add attack vector controls for spectre_v2_user" can be
converted to breaks, which simplifies the control flow and also allows
the above-suggested X86_FEATURE_IBPB/X86_FEATURE_STIBP checks to keep
working.

--
Josh