riscv: hwprobe: require both F and D extensions for RISCV_HWPROBE_IMA_FD
PATCH 1/1 riscv: hwprobe: require both F and D extensions for RISCV_HWPROBE_IMA_FD
Documentation/arch/riscv/hwprobe.rst states that RISCV_HWPROBE_IMA_FD means both the F and D extensions are supported. However, the implementation uses has_fpu(), which returns true if either extension is present, since that's sufficient to determine whether FPU state needs to be saved/restored. Reusing it here silently weakens the hwprobe semantics from "F and D" to "F or D", giving userspace no way to know which extension is actually present when only one is.
Check both extensions explicitly with riscv_isa_extension_available(), matching the pattern already used for the neighboring C and V checks.
--- arch/riscv/kernel/sys_hwprobe.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/sys_hwprobe.c +1 -1
@@ -85,7 +85,7 @@ static void hwprobe_isa_ext0(struct riscv_hwprobe *pair,u64 missing =0 ; pair->value =0 ; -if (has_fpu ()) +if (riscv_isa_extension_available (NULL , f) &&riscv_isa_extension_available (NULL , d)) pair->value |=RISCV_HWPROBE_IMA_FD ;if (riscv_isa_extension_available (NULL , c))
Signature · 1 line
2.55.0
Quoted 7 lines · Ivy Lopez
Documentation/arch/riscv/hwprobe.rststates thatRISCV_HWPROBE_IMA_FDmeans both the F and D extensions are supported. However, the implementation useshas_fpu(), which returns true if either extension is present, since that's sufficient to determine whether FPU state needs to be saved/restored. Reusing it here silently weakens the hwprobe semantics from "F and D" to "F or D", giving userspace no way to know which extension is actually present when only one is.
The kernel never supports F without D, so this cannot occur. riscv_isa_extension_available(NULL, f) always returns false if D is not also present.
If anything, the code in has_fpu() should be changed to only check D and not F to match expectations elsewhere, or F and D for the same reason.
Quoted 26 lines
Check both extensions explicitly with
riscv_isa_extension_available(), matching the pattern already used for the neighboring C and V checks.--- arch/riscv/kernel/sys_hwprobe.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/riscv/kernel/sys_hwprobe.c +1
-1@@ -85,7 +85,7 @@ static void hwprobe_isa_ext0(struct riscv_hwprobe *pair,u64 missing =0 ; pair->value =0 ; -if (has_fpu ()) +if (riscv_isa_extension_available (NULL , f) &&riscv_isa_extension_available (NULL , d)) pair->value |=RISCV_HWPROBE_IMA_FD ;if (riscv_isa_extension_available (NULL , c))Signature · 1 line
2.55.0
Quoted 6 lines · Conor Dooley
The kernel never supports F without D, so this cannot occur.
riscv_isa_extension_available(NULL, f)always returns false if D is not also present.If anything, the code in
has_fpu()should be changed to only check D and not F to match expectations elsewhere, or F and D for the same reason.
Ah ok, thanks for the clarification.. I hadn't realized F-without-D is already structurally impossible here, so this patch doesn't change actual behavior.
Would you prefer a v2 that fixes has_fpu() instead (checking D only, or F && D, whichever matches the intended semantics elsewhere), or would you rather I just drop this patch since RISCV_HWPROBE_IMA_FD already behaves correctly in practice?
Happy to send whichever you'd find useful!
Quoted 12 lines · Ivy Lopez
Quoted 6 lines · Conor Dooley
The kernel never supports F without D, so this cannot occur.
riscv_isa_extension_available(NULL, f)always returns false if D is not also present.If anything, the code in
has_fpu()should be changed to only check D and not F to match expectations elsewhere, or F and D for the same reason.Ah ok, thanks for the clarification.. I hadn't realized F-without-D is already structurally impossible here, so this patch doesn't change actual behavior.
Quoted 6 lines
Would you prefer a v2 that fixes
has_fpu()instead (checking D only, or F && D, whichever matches the intended semantics elsewhere), or would you rather I just drop this patch sinceRISCV_HWPROBE_IMA_FDalready behaves correctly in practice?Happy to send whichever you'd find useful!
I think I would rather see has_fpu() get changed. I think checking either D only (ideally with a comment explaining why that's correct to do) or checking both are equally valid, which to do is up to you.
I don't think there's really any other equivalents to this has_fpu() code because anything that would do it just calls has_fpu() instead! The closest thing to it that I found is code in cpufeature.c that only checks D (with comments), but that code is part of the wider extension parsing logic that where the block on F without D happens in the first place, so it may not be the best example.
In the future, when replying, please try to keep more than just one mail of context, to make it easier for people to follow the conversation if this is the only mail they read or if they forgot exactly what was going on in the previous ones.
Cheers,
Conor.
Quoted 2 lines · Ivy Lopez
Would you prefer a v2 that fixes
has_fpu()instead (checking D only, or F && D, whichever matches the intended semantics elsewhere), or
The D extension depends on F.
Signature · 3 lines
Andreas Schwab, SUSE Labs, [email protected] GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7 "And now for something completely different."
PATCH v2 1/1 riscv: hwprobe: fix has_fpu() to require D extension only
In reply to Ivy LopezThe kernel never supports F without D, since D depends on F. As such, has_fpu() checking either extension with '||' is incorrect: it reports FPU support when only F is present, which is not sufficient for D-dependent state save/restore, and weakens RISCV_HWPROBE_IMA_FD semantics to "F or D" instead of "F and D".
Fix has_fpu() to check D only, which is equivalent to requiring both extensions given the dependency. Revert the explicit F && D check in sys_hwprobe.c back to calling has_fpu(), which is now correct.
---
Changes in v2:
- Original v1 approach (explicit F && D check in sys_hwprobe.c) was a no-op since F-without-D is structurally impossible. Per Conor and Andreas, fix has_fpu() itself instead, checking D only. Revert the sys_hwprobe.c change back to calling has_fpu().
- Link to v1: https://lore.kernel.org/r/[email protected]
arch/riscv/include/asm/switch_to.h | 4 ++-- arch/riscv/kernel/sys_hwprobe.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
arch/riscv/include/asm/switch_to.h +2 -2
@@ -60,8 +60,8 @@ static inline void __switch_to_fpu(struct task_struct *prev, static __always_inline bool has_fpu(void) { -return riscv_has_extension_likely (RISCV_ISA_EXT_f) || -riscv_has_extension_likely (RISCV_ISA_EXT_d); +/* D extension depends on F, so checking D alone is sufficient. */ +return riscv_has_extension_likely (RISCV_ISA_EXT_d); }#else static __always_inline bool has_fpu(void) { return false; }
arch/riscv/kernel/sys_hwprobe.c +1 -1
@@ -85,7 +85,7 @@ static void hwprobe_isa_ext0(struct riscv_hwprobe *pair,u64 missing =0 ; pair->value =0 ; -if (riscv_isa_extension_available (NULL , f) &&riscv_isa_extension_available (NULL , d)) +if (has_fpu ()) pair->value |=RISCV_HWPROBE_IMA_FD ;if (riscv_isa_extension_available (NULL , c))
Signature · 1 line
2.55.0
Re: [PATCH v2] riscv: hwprobe: fix has_fpu() to require D extension only
Quoted 14 lines · Ivy Lopez
The kernel never supports F without D, since D depends on F. As such,
has_fpu()checking either extension with '||' is incorrect: it reports FPU support when only F is present, which is not sufficient for D-dependent state save/restore, and weakensRISCV_HWPROBE_IMA_FDsemantics to "F or D" instead of "F and D".Fix
has_fpu()to check D only, which is equivalent to requiring both extensions given the dependency. Revert the explicit F && D check insys_hwprobe.cback to callinghas_fpu(), which is now correct.Suggested-by Conor Dooley <[email protected]>Suggested-by Andreas Schwab <[email protected]>Signed-off-by Ivy Lopez <[email protected]>
Quoted 7 lines
---
Changes in v2:
- Original v1 approach (explicit F && D check insys_hwprobe.c) was a no-op since F-without-D is structurally impossible. Per Conor and Andreas, fixhas_fpu()itself instead, checking D only. Revert thesys_hwprobe.cchange back to callinghas_fpu().
- Link to v1: https://lore.kernel.org/r/[email protected]
This v2 looks like a diff on top of your v1, but your v1 didn't get applied (AFAIK) so this patch doesn't make sense.
Quoted 25 lines
arch/riscv/include/asm/switch_to.h | 4 ++-- arch/riscv/kernel/sys_hwprobe.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)arch/riscv/include/asm/switch_to.h +2
-2@@ -60,8 +60,8 @@ static inline void __switch_to_fpu(struct task_struct *prev, static __always_inline bool has_fpu(void) { -return riscv_has_extension_likely (RISCV_ISA_EXT_f) || -riscv_has_extension_likely (RISCV_ISA_EXT_d); +/* D extension depends on F, so checking D alone is sufficient. */ +return riscv_has_extension_likely (RISCV_ISA_EXT_d); }#else static __always_inline bool has_fpu(void) { return false; }arch/riscv/kernel/sys_hwprobe.c +0
-0
arch/riscv/kernel/sys_hwprobe.c static void hwprobe_isa_ext0(struct riscv_hwprobe *pair, · line 91
Quoted 8 lines
arch/riscv/kernel/sys_hwprobe.c +1
-1@@ -85,7 +85,7 @@ static void hwprobe_isa_ext0(struct riscv_hwprobe *pair,u64 missing =0 ; pair->value =0 ; -if (riscv_isa_extension_available (NULL , f) &&riscv_isa_extension_available (NULL , d)) +if (has_fpu ()) pair->value |=RISCV_HWPROBE_IMA_FD ;if (riscv_isa_extension_available (NULL , c))
This second portion should be dropped, since it is a reversion to how things were before.
Additionally, please don't submit new versions as a reply to old ones.
Thanks,
Conor.