Skip to contentSkip to conversation

Add some validation for vector, vector crypto and fp stuff

PATCH Superseded Conor Dooley 3 participants 13 messages Oct 24, 2024 – Jan 20, 2025

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add some validation for vector, vector crypto and fp stuff

From: Conor Dooley <[email protected]>

Yo,

This series is partly leveraging Clement's work adding a validate callback in the extension detection code so that things like checking for whether a vector crypto extension is usable can be done like:
has_extension(<vector crypto>)
rather than

	has_vector() && has_extension(<vector crypto>)

which Eric pointed out was a poor design some months ago.

The rest of this is adding some requirements to the bindings that prevent combinations of extensions disallowed by the ISA.

Cheers,
Conor.

v2:
- Fix an inverted clause Clément pointed out
- Add Zvbb validation, that I had missed accidentally
- Drop the todo about checking the number of validation rounds, I tried in w/ qemu's max cpu and things looked right

CC Conor Dooley <[email protected]>
CC Rob Herring <[email protected]>
CC Krzysztof Kozlowski <[email protected]>
CC Paul Walmsley <[email protected]>
CC Palmer Dabbelt <[email protected]>
CC "Clément Léger" <[email protected]>
CC Andy Chiu <[email protected]>

Conor Dooley (5):
RISC-V: add vector crypto extension validation checks
RISC-V: add f & d extension validation checks
dt-bindings: riscv: d requires f
dt-bindings: riscv: add vector sub-extension dependencies
dt-bindings: riscv: document vector crypto requirements

 .../devicetree/bindings/riscv/extensions.yaml |  84 ++++++++++
 arch/riscv/include/asm/cpufeature.h           |   3 +
 arch/riscv/kernel/cpufeature.c                | 148 ++++++++++++------
 3 files changed, 185 insertions(+), 50 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH v2 1/5 RISC-V: add vector crypto extension validation checks

From: Conor Dooley <[email protected]>

Using Clement's new validation callbacks, support checking that dependencies have been satisfied for the vector crpyto extensions. Currently riscv_isa_extension_available(<vector crypto>) will return true on systems that support the extensions but vector itself has been disabled by the kernel, adding validation callbacks will prevent such a scenario from occuring and make the behaviour of the extension detection functions more consistent with user expectations - it's not expected to have to check for vector AND the specific crypto extension.

The 1.0.0 Vector crypto spec states:
The Zvknhb and Zvbc Vector Crypto Extensions --and accordingly the composite extensions Zvkn and Zvks-- require a Zve64x base, or application ("V") base Vector Extension. All of the other Vector Crypto Extensions can be built on any embedded (Zve*) or application ("V") base Vector Extension.
and this could be used as the basis for checking that the correct base for individual crypto extensions, but that's not really the kernel's job in my opinion and it is sufficient to leave that sort of precision to the dt-bindings. The kernel only needs to make sure that vector, in some form, is available.

Since vector will now be disabled proactively, there's no need to clear the bit in elf_hwcap in riscv_fill_hwcap() any longer.

---
 arch/riscv/include/asm/cpufeature.h |   3 +
 arch/riscv/kernel/cpufeature.c      | 112 ++++++++++++++++++----------
 2 files changed, 76 insertions(+), 39 deletions(-)
arch/riscv/include/asm/cpufeature.h +3 -0
index 45f9c1171a486..1de408c3deee7 100644
@@ -51,6 +51,9 @@ void riscv_user_isa_enable(void);
 #define __RISCV_ISA_EXT_BUNDLE(_name, _bundled_exts) \
 	_RISCV_ISA_EXT_DATA(_name, RISCV_ISA_EXT_INVALID, _bundled_exts, \
 			    ARRAY_SIZE(_bundled_exts), NULL)
+#define __RISCV_ISA_EXT_BUNDLE_VALIDATE(_name, _bundled_exts, _validate) \
+	_RISCV_ISA_EXT_DATA(_name, RISCV_ISA_EXT_INVALID, _bundled_exts, \
+			    ARRAY_SIZE(_bundled_exts), _validate)
 
 /* Used to declare extensions that are a superset of other extensions (Zvbb for instance) */
 #define __RISCV_ISA_EXT_SUPERSET(_name, _id, _sub_exts) \
arch/riscv/kernel/cpufeature.c +73 -39

147 lines not on this page · raw message

Signature · 1 line
2.45.2
#2 lore raw

Re: [PATCH v2 1/5] RISC-V: add vector crypto extension validation checks

Thanks for working on this!

arch/riscv/kernel/cpufeature.c const struct riscv_isa_ext_data riscv_isa_ext[] = { · line 357

Quoted 6 lines · Conor Dooley
arch/riscv/kernel/cpufeature.c +1 -1
@@ -308,12 +354,10 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = {
 	__RISCV_ISA_EXT_DATA(d, RISCV_ISA_EXT_d),
 	__RISCV_ISA_EXT_DATA(q, RISCV_ISA_EXT_q),
 	__RISCV_ISA_EXT_SUPERSET(c, RISCV_ISA_EXT_c, riscv_c_exts),
-	__RISCV_ISA_EXT_SUPERSET(v, RISCV_ISA_EXT_v, riscv_v_exts),
+	__RISCV_ISA_EXT_SUPERSET_VALIDATE(v, RISCV_ISA_EXT_v, riscv_v_exts, riscv_ext_vector_float_validate),

This patch adds validation for not just the vector crypto extensions but also v, zve32f, zve32x, zve64d, zve64f, and zve64x. I think that should be split into a separate patch or at least called out explicitly in the commit message.

arch/riscv/kernel/cpufeature.c const struct riscv_isa_ext_data riscv_isa_ext[] = { · line 392

Quoted 8 lines
arch/riscv/kernel/cpufeature.c +3 -1
+	__RISCV_ISA_EXT_BUNDLE_VALIDATE(zk, riscv_zk_bundled_exts, riscv_ext_vector_crypto_validate),
+	__RISCV_ISA_EXT_BUNDLE_VALIDATE(zkn, riscv_zkn_bundled_exts, riscv_ext_vector_crypto_validate),
 	__RISCV_ISA_EXT_DATA(zknd, RISCV_ISA_EXT_ZKND),
 	__RISCV_ISA_EXT_DATA(zkne, RISCV_ISA_EXT_ZKNE),
 	__RISCV_ISA_EXT_DATA(zknh, RISCV_ISA_EXT_ZKNH),
 	__RISCV_ISA_EXT_DATA(zkr, RISCV_ISA_EXT_ZKR),
-	__RISCV_ISA_EXT_BUNDLE(zks, riscv_zks_bundled_exts),
+	__RISCV_ISA_EXT_BUNDLE_VALIDATE(zks, riscv_zks_bundled_exts, riscv_ext_vector_crypto_validate),

zk* are the scalar crypto extensions, which don't require vector.

- Eric

#3 lore raw

Re: [PATCH v2 1/5] RISC-V: add vector crypto extension validation checks

Quoted 12 lines · Eric Biggers

arch/riscv/kernel/cpufeature.c const struct riscv_isa_ext_data riscv_isa_ext[] = { · line 357

Quoted 6 lines · Conor Dooley
arch/riscv/kernel/cpufeature.c +1 -1
@@ -308,12 +354,10 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = {
 	__RISCV_ISA_EXT_DATA(d, RISCV_ISA_EXT_d),
 	__RISCV_ISA_EXT_DATA(q, RISCV_ISA_EXT_q),
 	__RISCV_ISA_EXT_SUPERSET(c, RISCV_ISA_EXT_c, riscv_c_exts),
-	__RISCV_ISA_EXT_SUPERSET(v, RISCV_ISA_EXT_v, riscv_v_exts),
+	__RISCV_ISA_EXT_SUPERSET_VALIDATE(v, RISCV_ISA_EXT_v, riscv_v_exts, riscv_ext_vector_float_validate),

This patch adds validation for not just the vector crypto extensions but also v, zve32f, zve32x, zve64d, zve64f, and zve64x. I think that should be split into a separate patch or at least called out explicitly in the commit message.

Sure. I think I even had it like that originally and must have waywardly squashed it. I actually checked before sending this to make sure that I hadn't do so by accident between v1 and v2 and I had not.

Quoted 11 lines

arch/riscv/kernel/cpufeature.c const struct riscv_isa_ext_data riscv_isa_ext[] = { · line 392

Quoted 8 lines
arch/riscv/kernel/cpufeature.c +3 -1
+	__RISCV_ISA_EXT_BUNDLE_VALIDATE(zk, riscv_zk_bundled_exts, riscv_ext_vector_crypto_validate),
+	__RISCV_ISA_EXT_BUNDLE_VALIDATE(zkn, riscv_zkn_bundled_exts, riscv_ext_vector_crypto_validate),
 	__RISCV_ISA_EXT_DATA(zknd, RISCV_ISA_EXT_ZKND),
 	__RISCV_ISA_EXT_DATA(zkne, RISCV_ISA_EXT_ZKNE),
 	__RISCV_ISA_EXT_DATA(zknh, RISCV_ISA_EXT_ZKNH),
 	__RISCV_ISA_EXT_DATA(zkr, RISCV_ISA_EXT_ZKR),
-	__RISCV_ISA_EXT_BUNDLE(zks, riscv_zks_bundled_exts),
+	__RISCV_ISA_EXT_BUNDLE_VALIDATE(zks, riscv_zks_bundled_exts, riscv_ext_vector_crypto_validate),

zk* are the scalar crypto extensions, which don't require vector.

Quoted 1 line

Thanks for working on this!

Thanks for taking a look. I'm surprised I didn't make more mistakes tbh.

#4 lore raw

PATCH v2 2/5 RISC-V: add f & d extension validation checks

In reply to Conor Dooley

From: Conor Dooley <[email protected]>

Using Clement's new validation callbacks, support checking that dependencies have been satisfied for the floating point extensions.

The check for "d" might be slightly confusingly shorter than that of "f", despite "d" depending on "f". This is because the requirement that a hart supporting double precision must also support single precision, should be validated by dt-bindings etc, not the kernel but lack of support for single precision only is a limitation of the kernel.

Since vector will now be disabled proactively, there's no need to clear the bit in elf_hwcap in riscv_fill_hwcap() any longer.

---
 arch/riscv/kernel/cpufeature.c | 36 +++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)
arch/riscv/kernel/cpufeature.c +25 -11
index 020b19edee2e8..1326049d2ac3b 100644
@@ -101,6 +101,29 @@ static int riscv_ext_zicboz_validate(const struct riscv_isa_ext_data *data,
 	return 0;
 }
 
+static int riscv_ext_f_validate(const struct riscv_isa_ext_data *data,
+				const unsigned long *isa_bitmap)
+{
+	if (!__riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_d)) {
+		pr_warn_once("This kernel does not support systems with F but not D\n");
+		return -EINVAL;
+	}
+
+	if (!IS_ENABLED(CONFIG_FPU))
+		return -EINVAL;
+
+	return 0;
+}
+
+static int riscv_ext_d_validate(const struct riscv_isa_ext_data *data,
+				const unsigned long *isa_bitmap)
+{
+	if (!IS_ENABLED(CONFIG_FPU))
+		return -EINVAL;
+
+	return 0;
+}
+
 static int riscv_ext_vector_x_validate(const struct riscv_isa_ext_data *data,
 				     const unsigned long *isa_bitmap)
 {
@@ -350,8 +373,8 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = {
 	__RISCV_ISA_EXT_DATA(i, RISCV_ISA_EXT_i),
 	__RISCV_ISA_EXT_DATA(m, RISCV_ISA_EXT_m),
 	__RISCV_ISA_EXT_DATA(a, RISCV_ISA_EXT_a),
-	__RISCV_ISA_EXT_DATA(f, RISCV_ISA_EXT_f),
-	__RISCV_ISA_EXT_DATA(d, RISCV_ISA_EXT_d),
+	__RISCV_ISA_EXT_DATA_VALIDATE(f, RISCV_ISA_EXT_f, riscv_ext_f_validate),
+	__RISCV_ISA_EXT_DATA_VALIDATE(d, RISCV_ISA_EXT_d, riscv_ext_d_validate),
 	__RISCV_ISA_EXT_DATA(q, RISCV_ISA_EXT_q),
 	__RISCV_ISA_EXT_SUPERSET(c, RISCV_ISA_EXT_c, riscv_c_exts),
 	__RISCV_ISA_EXT_SUPERSET_VALIDATE(v, RISCV_ISA_EXT_v, riscv_v_exts, riscv_ext_vector_float_validate),
@@ -910,15 +933,6 @@ void __init riscv_fill_hwcap(void)
 		}
 	}
 
-	/*
-	 * We don't support systems with F but without D, so mask those out
-	 * here.
-	 */
-	if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) {
-		pr_info("This kernel does not support systems with F but not D\n");
-		elf_hwcap &= ~COMPAT_HWCAP_ISA_F;
-	}
-
 	if (__riscv_isa_extension_available(NULL, RISCV_ISA_EXT_ZVE32X)) {
 		/*
 		 * This cannot fail when called on the boot hart
Signature · 1 line
2.45.2
#5 lore raw

PATCH v2 3/5 dt-bindings: riscv: d requires f

In reply to Conor Dooley

From: Conor Dooley <[email protected]>

Per the specifications, the d extension for double-precision floating point operations depends on the f extension for single-precision floating point. Add that requirement to the bindings. This differs from the Linux implementation, where single-precious only is not supported.

---
 Documentation/devicetree/bindings/riscv/extensions.yaml | 6 ++++++
 1 file changed, 6 insertions(+)
Documentation/devicetree/bindings/riscv/extensions.yaml +6 -0
index 2cf2026cff574..c697be64d3bfc 100644
@@ -571,6 +571,12 @@ properties:
             https://www.andestech.com/wp-content/uploads/AX45MP-1C-Rev.-5.0.0-Datasheet.pdf
 
     allOf:
+      - if:
+          contains:
+            const: d
+        then:
+          contains:
+            const: f
       # Zcb depends on Zca
       - if:
           contains:
Signature · 1 line
2.45.2
#6 lore raw

PATCH v2 4/5 dt-bindings: riscv: add vector sub-extension dependencies

In reply to Conor Dooley

From: Conor Dooley <[email protected]>

Section 33.18.2. Zve*: Vector Extensions for Embedded Processors
in [1] says:
| The Zve32f and Zve64x extensions depend on the Zve32x extension. The Zve64f extension depends | on the Zve32f and Zve64x extensions. The Zve64d extension depends on the Zve64f extension

| The Zve32x extension depends on the Zicsr extension. The Zve32f and Zve64f extensions depend | upon the F extension

| The Zve64d extension depends upon the D extension

Apply these rules to the bindings to help prevent invalid combinations.

---
 .../devicetree/bindings/riscv/extensions.yaml | 46 +++++++++++++++++++
 1 file changed, 46 insertions(+)
Documentation/devicetree/bindings/riscv/extensions.yaml +46 -0
index c697be64d3bfc..20cead7d8af71 100644
@@ -612,6 +612,52 @@ properties:
           contains:
             const: zca
 
+      - if:
+          contains:
+            const: zve32x
+        then:
+          contains:
+            const: zicsr
+
+      - if:
+          contains:
+            const: zve32f
+        then:
+          allOf:
+            - contains:
+                const: f
+            - contains:
+                const: zve32x
+
+      - if:
+          contains:
+            const: zve64x
+        then:
+          contains:
+            const: zve32x
+
+      - if:
+          contains:
+            const: zve64f
+        then:
+          allOf:
+            - contains:
+                const: f
+            - contains:
+                const: zve32f
+            - contains:
+                const: zve64x
+
+      - if:
+          contains:
+            const: zve64d
+        then:
+          allOf:
+            - contains:
+                const: d
+            - contains:
+                const: zve64f
+
 allOf:
   # Zcf extension does not exist on rv64
   - if:
Signature · 1 line
2.45.2
#7 lore raw

PATCH v2 5/5 dt-bindings: riscv: document vector crypto requirements

In reply to Conor Dooley

From: Conor Dooley <[email protected]>

Section 35.2. Extensions Overview of [1] says:

| The Zvknhb and Zvbc Vector Crypto Extensions --and accordingly the composite extensions Zvkn and
| Zvks-- (sic) require a Zve64x base, or application ("V") base Vector Extension.
| All of the other Vector Crypto Extensions can be built on any embedded (Zve*) or application ("V") base
| Vector Extension

Apply these rules in the binding, so that invalid combinations can be avoided.

---
 .../devicetree/bindings/riscv/extensions.yaml | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)
Documentation/devicetree/bindings/riscv/extensions.yaml +32 -0
index 20cead7d8af71..38d77043552a3 100644
@@ -658,6 +658,38 @@ properties:
             - contains:
                 const: zve64f
 
+      - if:
+          contains:
+            anyOf:
+              - const: zvbc
+              - const: zvkn
+              - const: zvknhb
+              - const: zvks
+        then:
+          contains:
+            anyOf:
+              - const: v
+              - const: zve64x
+
+      - if:
+          contains:
+            anyOf:
+              - const: zvbb
+              - const: zvkb
+              - const: zvkg
+              - const: zvkned
+              - const: zvknha
+              - const: zvksed
+              - const: zvksh
+              - const: zvknc
+              - const: zvkng
+              - const: zvkt
+        then:
+          contains:
+            anyOf:
+              - const: v
+              - const: zve32x
+
 allOf:
   # Zcf extension does not exist on rv64
   - if:
Signature · 1 line
2.45.2
#8 lore raw

Re: [PATCH v2 5/5] dt-bindings: riscv: document vector crypto requirements

Quoted 10 lines · Conor Dooley

From: Conor Dooley <[email protected]>

Section 35.2. Extensions Overview of [1] says:

| The Zvknhb and Zvbc Vector Crypto Extensions --and accordingly the composite extensions Zvkn and
| Zvks-- (sic) require a Zve64x base, or application ("V") base Vector Extension.
| All of the other Vector Crypto Extensions can be built on any embedded (Zve*) or application ("V") base
| Vector Extension

Apply these rules in the binding, so that invalid combinations can be avoided.

It looks like that part of the spec is wrong, though. The Zvknhb and Zvbc are correct, but the list of the composite extensions that at least one of them is included in is: Zvkn, Zvknc, Zvkng, Zvksc.

- Eric

#9 lore raw

Re: [PATCH v2 5/5] dt-bindings: riscv: document vector crypto requirements

Quoted 9 lines · Eric Biggers
Quoted 8 lines · Conor Dooley

From: Conor Dooley <[email protected]>

Section 35.2. Extensions Overview of [1] says:

| The Zvknhb and Zvbc Vector Crypto Extensions --and accordingly the composite extensions Zvkn and
| Zvks-- (sic) require a Zve64x base, or application ("V") base Vector Extension.
| All of the other Vector Crypto Extensions can be built on any embedded (Zve*) or application ("V") base
| Vector Extension
Quoted 7 lines
Quoted 2 lines

Apply these rules in the binding, so that invalid combinations can be avoided.

It looks like that part of the spec is wrong, though. The Zvknhb and Zvbc are correct, but the list of the composite extensions that at least one of them is included in is: Zvkn, Zvknc, Zvkng, Zvksc.

I am attempting to fix this in
https://github.com/riscv/riscv-isa-manual/pull/1697

- Eric

#10 lore raw

Re: [PATCH v2 5/5] dt-bindings: riscv: document vector crypto requirements

Quoted 13 lines · Eric Biggers
Quoted 12 lines · Eric Biggers
Quoted 10 lines · Conor Dooley

From: Conor Dooley <[email protected]>

Section 35.2. Extensions Overview of [1] says:

| The Zvknhb and Zvbc Vector Crypto Extensions --and accordingly the composite extensions Zvkn and
| Zvks-- (sic) require a Zve64x base, or application ("V") base Vector Extension.
| All of the other Vector Crypto Extensions can be built on any embedded (Zve*) or application ("V") base
| Vector Extension

Apply these rules in the binding, so that invalid combinations can be avoided.

Quoted 7 lines
Quoted 4 lines

It looks like that part of the spec is wrong, though. The Zvknhb and Zvbc are correct, but the list of the composite extensions that at least one of them is included in is: Zvkn, Zvknc, Zvkng, Zvksc.

I am attempting to fix this in
https://github.com/riscv/riscv-isa-manual/pull/1697

Looks like at least one person agrees with you, but I'll wait til that's merged before submitting another version. Thanks for reporting it.

#11 lore raw

Re: [PATCH v2 5/5] dt-bindings: riscv: document vector crypto requirements

Quoted 19 lines · Conor Dooley
Quoted 18 lines · Eric Biggers
Quoted 16 lines · Eric Biggers
Quoted 10 lines · Conor Dooley

From: Conor Dooley <[email protected]>

Section 35.2. Extensions Overview of [1] says:

| The Zvknhb and Zvbc Vector Crypto Extensions --and accordingly the composite extensions Zvkn and
| Zvks-- (sic) require a Zve64x base, or application ("V") base Vector Extension.
| All of the other Vector Crypto Extensions can be built on any embedded (Zve*) or application ("V") base
| Vector Extension

Apply these rules in the binding, so that invalid combinations can be avoided.

It looks like that part of the spec is wrong, though. The Zvknhb and Zvbc are correct, but the list of the composite extensions that at least one of them is included in is: Zvkn, Zvknc, Zvkng, Zvksc.

Quoted 5 lines
Quoted 2 lines

I am attempting to fix this in
https://github.com/riscv/riscv-isa-manual/pull/1697

Looks like at least one person agrees with you, but I'll wait til that's merged before submitting another version. Thanks for reporting it.

It's been merged now :)

Quoted 4 lines

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

#12 lore raw

Re: [PATCH v2 5/5] dt-bindings: riscv: document vector crypto requirements

Quoted 20 lines · Charlie Jenkins
Quoted 19 lines · Conor Dooley
Quoted 18 lines · Eric Biggers
Quoted 16 lines · Eric Biggers
Quoted 10 lines · Conor Dooley

From: Conor Dooley <[email protected]>

Section 35.2. Extensions Overview of [1] says:

| The Zvknhb and Zvbc Vector Crypto Extensions --and accordingly the composite extensions Zvkn and
| Zvks-- (sic) require a Zve64x base, or application ("V") base Vector Extension.
| All of the other Vector Crypto Extensions can be built on any embedded (Zve*) or application ("V") base
| Vector Extension

Apply these rules in the binding, so that invalid combinations can be avoided.

It looks like that part of the spec is wrong, though. The Zvknhb and Zvbc are correct, but the list of the composite extensions that at least one of them is included in is: Zvkn, Zvknc, Zvkng, Zvksc.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

I am attempting to fix this in
https://github.com/riscv/riscv-isa-manual/pull/1697

Looks like at least one person agrees with you, but I'll wait til that's merged before submitting another version. Thanks for reporting it.

It's been merged now :)

Ye, I actually respun this last week, but opted to wait until after the merge window to send another revision.