Skip to contentSkip to conversation

riscv: hwprobe: require both F and D extensions for RISCV_HWPROBE_IMA_FD

PATCH In review Ivy Lopez 4 participants 7 messages Aug 16 – Aug 25 RISC-V

lore
Share & tools
lore

Ivy Lopez

author
#0 lore raw

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
index 1659d31fd288..7f91beb82a1c 100644
@@ -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
#1 lore raw
Quoted 7 lines · Ivy Lopez

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.

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
index 1659d31fd288..7f91beb82a1c 100644
@@ -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
author
#2 lore raw
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!

#3 lore raw
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 since RISCV_HWPROBE_IMA_FD already 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.

#4 lore raw
In reply to Ivy Lopez
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."
author
#5 lore raw

PATCH v2 1/1 riscv: hwprobe: fix has_fpu() to require D extension only

In reply to 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 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
index 0e71eb82f920..8186cda88e17 100644
@@ -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
index 7f91beb82a1c..1659d31fd288 100644
@@ -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
#6 lore raw

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 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.

Quoted 7 lines

---
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]

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
index 0e71eb82f920..8186cda88e17 100644
@@ -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
index 7f91beb82a1c..1659d31fd288 100644

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.