Skip to contentSkip to conversation

riscv: vector: Fix data pointer constraints in context save/restore

PATCH In review Troy Mitchell 5 participants 6 messages Sep 8 – Sep 19 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: vector: Fix data pointer constraints in context save/restore

The standard vector save/restore asm advances datap but declares it as input-only. An inlined caller reusing the original pointer may therefore use the advanced address instead.

Declare datap as read-write so the compiler can preserve the original pointer when needed.

Fixes 03c3fcd9941a ("riscv: Introduce struct/helpers to save/restore per-task Vector state")
---
 arch/riscv/include/asm/vector.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/include/asm/vector.h +2 -2
index fffe72a772080..c7fd6d50a7a47 100644
@@ -230,7 +230,7 @@ static inline void __riscv_v_vstate_save(struct __riscv_v_ext_state *save_to,
 			"add		%1, %1, %0\n\t"
 			"vse8.v		v24, (%1)\n\t"
 			".option pop\n\t"
-			: "=&r" (vl) : "r" (datap) : "memory");
+			: "=&r" (vl), "+r" (datap) : : "memory");
 	}
 	riscv_v_disable();
 }
@@ -266,7 +266,7 @@ static inline void __riscv_v_vstate_restore(struct __riscv_v_ext_state *restore_
 			"add		%1, %1, %0\n\t"
 			"vle8.v		v24, (%1)\n\t"
 			".option pop\n\t"
-			: "=&r" (vl) : "r" (datap) : "memory");
+			: "=&r" (vl), "+r" (datap) : : "memory");
 	}
 	__vstate_csr_restore(restore_from);
 	riscv_v_disable();

---
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
change-id: 20260908-riscv-vector-asm-fix-27ed36623934

Best regards,

Signature · 1 line
Troy Mitchell <[email protected]>
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Troy Mitchell

The standard vector save/restore asm advances datap but declares it as input-only. An inlined caller reusing the original pointer may therefore use the advanced address instead.

Declare datap as read-write so the compiler can preserve the original pointer when needed.

Fixes 03c3fcd9941a ("riscv: Introduce struct/helpers to save/restore per-task Vector state")
---
 arch/riscv/include/asm/vector.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/include/asm/vector.h +0 -0
index fffe72a772080..c7fd6d50a7a47 100644

arch/riscv/include/asm/vector.h static inline void __riscv_v_vstate_save(struct __riscv_v_ext_state *save_to, · line 230

Quoted 1 line
arch/riscv/include/asm/vector.h +0 -0
@@ -230,7 +230,7 @@ static inline void __riscv_v_vstate_save(struct __riscv_v_ext_state *save_to,
                        "add            %1, %1, %0\n\t"

Yes, %1 is modified. Good catch!

PS:
Don't forget "if (has_xtheadvector()) {" part, thx :)

Quoted 25 lines
arch/riscv/include/asm/vector.h +2 -3
                        "vse8.v         v24, (%1)\n\t"
                        ".option pop\n\t"
-                       : "=&r" (vl) : "r" (datap) : "memory");
+                       : "=&r" (vl), "+r" (datap) : : "memory");
        }
        riscv_v_disable();
 }
@@ -266,7 +266,7 @@ static inline void __riscv_v_vstate_restore(struct __riscv_v_ext_state *restore_
                        "add            %1, %1, %0\n\t"
                        "vle8.v         v24, (%1)\n\t"
                        ".option pop\n\t"
-                       : "=&r" (vl) : "r" (datap) : "memory");
+                       : "=&r" (vl), "+r" (datap) : : "memory");
        }
        __vstate_csr_restore(restore_from);
        riscv_v_disable();
 
---

base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
change-id: 20260908-riscv-vector-asm-fix-27ed36623934

Best regards,
--
Troy Mitchell <[email protected]>

Signature · 2 lines
Best Regards
 Guo Ren
Reviewed-by on v1 1/1
#3 lore raw
In reply to Troy Mitchell
Quoted 4 lines · Troy Mitchell

The standard vector save/restore asm advances datap but declares it as input-only. An inlined caller reusing the original pointer may therefore use the advanced address instead.

Quoted 8 lines

Declare datap as read-write so the compiler can preserve the original pointer when needed.

Fixes 03c3fcd9941a ("riscv: Introduce struct/helpers to save/restore per-task Vector state")
---
 arch/riscv/include/asm/vector.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                     http://aurel32.net
Reviewed-by on v1 1/1
#4 lore raw
In reply to Troy Mitchell

Hi Troy,

Quoted 10 lines · Troy Mitchell

The standard vector save/restore asm advances datap but declares it as input-only. An inlined caller reusing the original pointer may therefore use the advanced address instead.

Declare datap as read-write so the compiler can preserve the original pointer when needed.

Fixes 03c3fcd9941a ("riscv: Introduce struct/helpers to save/restore per-task Vector state")

Thank you,
Andy Chiu

#2 lore raw
In reply to Guo Ren
Quoted 23 lines · Guo Ren
Quoted 19 lines · Troy Mitchell

The standard vector save/restore asm advances datap but declares it as input-only. An inlined caller reusing the original pointer may therefore use the advanced address instead.

Declare datap as read-write so the compiler can preserve the original pointer when needed.

Fixes 03c3fcd9941a ("riscv: Introduce struct/helpers to save/restore per-task Vector state")
---
 arch/riscv/include/asm/vector.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/include/asm/vector.h +0 -0
index fffe72a772080..c7fd6d50a7a47 100644

arch/riscv/include/asm/vector.h static inline void __riscv_v_vstate_save(struct __riscv_v_ext_state *save_to, · line 230

Quoted 1 line
arch/riscv/include/asm/vector.h +0 -0
@@ -230,7 +230,7 @@ static inline void __riscv_v_vstate_save(struct __riscv_v_ext_state *save_to,
                        "add            %1, %1, %0\n\t"

Yes, %1 is modified. Good catch!

Quoted 4 lines

PS:
Don't forget "if (has_xtheadvector()) {" part, thx :)

The XTheadVector path copies datap into t0 and lists t0/t4 as clobbers, so it doesn't have this issue. Thanks for the reminder!

Signature · 1 line
Troy Mitchell
maintainer
#5 lore raw
In reply to Troy Mitchell
Quoted 9 lines · Troy Mitchell

The standard vector save/restore asm advances datap but declares it as input-only. An inlined caller reusing the original pointer may therefore use the advanced address instead.

Declare datap as read-write so the compiler can preserve the original pointer when needed.

Fixes 03c3fcd9941a ("riscv: Introduce struct/helpers to save/restore per-task Vector state")

Thanks, queued for v7.3-rc.

- Paul