riscv: kvm: Use endian-specific __lelong for NACL shared memory
Subsystems (3)
PATCH 1/1 riscv: kvm: Use endian-specific __lelong for NACL shared memory
When compiling with sparse enabled (C=2), bitwise type warnings are triggered in the RISC-V KVM implementation. This occurs because the user-space data unboxing macro '__get_user_asm' performs implicit casting on restricted types without forcing the compiler's compliance.
Additionally, raw 'unsigned long *' pointers are used to access the SBI NACL shared memory, whereas the RISC-V SBI specification mandates that these structures must follow little-endian byte ordering.
Fix these by:
1. Adding a '__force' cast to '__get_user_asm()' to safely suppress implicit cast warnings during user-space data fetching.
2. Introducing the '__lelong' type macro, which dynamically resolves to '__le32' or '__le64' depending on XLEN, and replacing 'unsigned long *' with '__lelong *' to enforce proper compile-time endianness checks.
--- arch/riscv/include/asm/kvm_nacl.h | 14 ++++++++------ arch/riscv/include/asm/uaccess.h | 2 +- arch/riscv/kvm/nacl.c | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-)
arch/riscv/include/asm/kvm_nacl.h +8 -6
@@ -60,9 +60,11 @@ int kvm_riscv_nacl_init(void);#ifdef CONFIG_32BIT #define lelong_to_cpu (__x) le32_to_cpu(__x)#define cpu_to_lelong (__x) cpu_to_le32(__x) +#define __lelong __le32#else #define lelong_to_cpu (__x) le64_to_cpu(__x)#define cpu_to_lelong (__x) cpu_to_le64(__x) +#define __lelong __le64#endif #define nacl_shmem () \
@@ -70,7 +72,7 @@ int kvm_riscv_nacl_init(void);#define nacl_scratch_read_long (__shmem, __offset) \ ({ \ - unsigned long *__p = (__shmem) + \ + __lelong *__p = (__shmem) + \ SBI_NACL_SHMEM_SCRATCH_OFFSET + \ (__offset); \ lelong_to_cpu(*__p); \
@@ -78,7 +80,7 @@ int kvm_riscv_nacl_init(void);#define nacl_scratch_write_long (__shmem, __offset, __val) \ do { \ - unsigned long *__p = (__shmem) + \ + __lelong *__p = (__shmem) + \ SBI_NACL_SHMEM_SCRATCH_OFFSET + \ (__offset); \ *__p = cpu_to_lelong(__val); \
@@ -87,7 +89,7 @@ do { \#define nacl_scratch_write_longs (__shmem, __offset, __array, __count) \ do { \ unsigned int __i; \ - unsigned long *__p = (__shmem) + \ + __lelong *__p = (__shmem) + \ SBI_NACL_SHMEM_SCRATCH_OFFSET + \ (__offset); \ for (__i = 0; __i < (__count); __i++) \
@@ -168,7 +170,7 @@ __kvm_riscv_nacl_hfence(__shmem, \#define nacl_csr_read (__shmem, __csr) \ ({ \ - unsigned long *__a = (__shmem) + SBI_NACL_SHMEM_CSR_OFFSET; \ + __lelong *__a = (__shmem) + SBI_NACL_SHMEM_CSR_OFFSET; \ lelong_to_cpu(__a[SBI_NACL_SHMEM_CSR_INDEX(__csr)]); \ })
@@ -176,7 +178,7 @@ __kvm_riscv_nacl_hfence(__shmem, \ do { \ void *__s = (__shmem); \ unsigned int __i = SBI_NACL_SHMEM_CSR_INDEX(__csr); \ - unsigned long *__a = (__s) + SBI_NACL_SHMEM_CSR_OFFSET; \ + __lelong *__a = (__s) + SBI_NACL_SHMEM_CSR_OFFSET; \ u8 *__b = (__s) + SBI_NACL_SHMEM_DBITMAP_OFFSET; \ __a[__i] = cpu_to_lelong(__val); \ __b[__i >> 3] |= 1U << (__i & 0x7); \
@@ -186,7 +188,7 @@ do { \ ({ \ void *__s = (__shmem); \ unsigned int __i = SBI_NACL_SHMEM_CSR_INDEX(__csr); \ - unsigned long *__a = (__s) + SBI_NACL_SHMEM_CSR_OFFSET; \ + __lelong *__a = (__s) + SBI_NACL_SHMEM_CSR_OFFSET; \ u8 *__b = (__s) + SBI_NACL_SHMEM_DBITMAP_OFFSET; \ unsigned long __r = lelong_to_cpu(__a[__i]); \ __a[__i] = cpu_to_lelong(__val); \
arch/riscv/include/asm/uaccess.h +1 -1
@@ -112,7 +112,7 @@ do { \ _ASM_EXTABLE_UACCESS_ERR(1b, %l2, %0) \ : "=&r" (__tmp) \ : "m" (*(ptr)) : : label); \ - (x) = (__typeof__(x))(unsigned long)__tmp; \ + (x) = (__force __typeof__(x))(unsigned long)__tmp; \ } while (0) #else /* !CONFIG_CC_HAS_ASM_GOTO_OUTPUT */ #define __get_user_asm(insn, x, ptr, label) \
arch/riscv/kvm/nacl.c +1 -1
@@ -20,7 +20,7 @@ void __kvm_riscv_nacl_hfence(void *shmem, unsigned long page_count) {int i, ent =-1 , try_count =5 ; -unsigned long *entp; +__lelong *entp; again:for (i =0 ; i <SBI_NACL_SHMEM_HFENCE_ENTRY_MAX ; i++) {
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Infinite loop in __kvm_riscv_nacl_hfence due to missing try_count decrement.
- [High] Race condition and memory ordering bugs in NACL shared memory updates. --
commit 041fd3e0bc4c133e6f04a198d4795da99daa15bb
Author: Sean Chang <[email protected]>
riscv: kvm: Use endian-specific __lelong for NACL shared memory
This patch fixes bitwise type warnings from sparse by introducing __lelong and replacing raw unsigned long pointers in SBI NACL shared memory. It also adds a __force cast to __get_user_asm to safely suppress implicit cast warnings.
Quoted 4 lines
arch/riscv/include/asm/kvm_nacl.h +0
-0
[ ... ]
arch/riscv/include/asm/kvm_nacl.h __kvm_riscv_nacl_hfence(__shmem, \ · line 184
Quoted 9 lines
arch/riscv/include/asm/kvm_nacl.h +1
-1@@ -176,7 +178,7 @@ __kvm_riscv_nacl_hfence(__shmem, \ do { \ void *__s = (__shmem); \ unsigned int __i = SBI_NACL_SHMEM_CSR_INDEX(__csr); \ - unsigned long *__a = (__s) + SBI_NACL_SHMEM_CSR_OFFSET; \ + __lelong *__a = (__s) + SBI_NACL_SHMEM_CSR_OFFSET; \ u8 *__b = (__s) + SBI_NACL_SHMEM_DBITMAP_OFFSET; \ __a[__i] = cpu_to_lelong(__val); \ __b[__i >> 3] |= 1U << (__i & 0x7); \
[Severity: High]
This is a pre-existing issue, but does the lack of a memory barrier between the CSR value update and the dirty bit update risk a race condition?
On RISC-V's relaxed memory model, these stores could be reordered without an smp_wmb(), potentially allowing the hardware or firmware to observe the dirty bit before the payload is visible in nacl_csr_write.
Quoted 10 lines
arch/riscv/include/asm/kvm_nacl.h +1
-1@@ -186,7 +188,7 @@ do { \ ({ \ void *__s = (__shmem); \ unsigned int __i = SBI_NACL_SHMEM_CSR_INDEX(__csr); \ - unsigned long *__a = (__s) + SBI_NACL_SHMEM_CSR_OFFSET; \ + __lelong *__a = (__s) + SBI_NACL_SHMEM_CSR_OFFSET; \ u8 *__b = (__s) + SBI_NACL_SHMEM_DBITMAP_OFFSET; \ unsigned long __r = lelong_to_cpu(__a[__i]); \ __a[__i] = cpu_to_lelong(__val); \__b[__i >> 3] |= 1U << (
__i&0x7); \
[Severity: High]
This isn't a bug introduced by this patch, but similar to nacl_csr_write, could these writes be reordered by the CPU in the nacl_csr_swap macro?
[ ... ]
arch/riscv/kvm/nacl.c void __kvm_riscv_nacl_hfence(void *shmem, · line 26
Quoted 13 lines
arch/riscv/kvm/nacl.c +1
-1@@ -20,7 +20,7 @@ void __kvm_riscv_nacl_hfence(void *shmem, unsigned long page_count) {int i, ent =-1 , try_count =5 ; -unsigned long *entp; +__lelong *entp; again:for (i =0 ; i <SBI_NACL_SHMEM_HFENCE_ENTRY_MAX ; i++) {
[Severity: High]
This is also a pre-existing issue, but does this retry loop run infinitely if the queue remains full?
Looking at __kvm_riscv_nacl_hfence(), it appears try_count is never decremented before taking the goto:
if (ent < 0) {
if (try_count) {
nacl_sync_hfence(-1UL);
goto again;
} else {
pr_warn("KVM: No free entry in NACL shared memory\n");
return;
}
}Additionally, there seems to be a pre-existing ordering hazard when writing the entry payload further down in __kvm_riscv_nacl_hfence():
entp = shmem + SBI_NACL_SHMEM_HFENCE_ENTRY_CONFIG(i);
*entp = cpu_to_lelong(control);
entp = shmem + SBI_NACL_SHMEM_HFENCE_ENTRY_PNUM(i);
*entp = cpu_to_lelong(page_num);
entp = shmem + SBI_NACL_SHMEM_HFENCE_ENTRY_PCOUNT(i);
*entp = cpu_to_lelong(page_count);Since the CONFIG element contains the valid bit, writing it before PNUM and PCOUNT could allow another vCPU on the same physical CPU to trigger nacl_sync_hfence() if KVM is preempted right after the CONFIG write. Would this cause the firmware to process uninitialized payload data?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 10 lines · Sean Chang
When compiling with sparse enabled (C=2), bitwise type warnings are triggered in the RISC-V KVM implementation. This occurs because the user-space data unboxing macro '
__get_user_asm' performs implicit casting on restricted types without forcing the compiler's compliance.Additionally, raw 'unsigned long *' pointers are used to access the SBI NACL shared memory, whereas the RISC-V SBI specification mandates that these structures must follow little-endian byte ordering.
Quoted 8 lines
Fix these by:
1. Adding a '__force' cast to '__get_user_asm()' to safely suppress implicit cast warnings during user-space data fetching.
2. Introducing the '__lelong' type macro, which dynamically resolves to '__le32' or '__le64' depending on XLEN, and replacing 'unsigned long *' with '__lelong*' to enforce proper compile-time endianness checks.Signed-off-by Sean Chang <[email protected]>
LGTM.
Queued this patch for Linux-7.2
Thanks,
Anup
Quoted 105 lines
--- arch/riscv/include/asm/kvm_nacl.h | 14 ++++++++------ arch/riscv/include/asm/uaccess.h | 2 +- arch/riscv/kvm/nacl.c | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-)arch/riscv/include/asm/kvm_nacl.h +8
-6@@ -60,9 +60,11 @@ int kvm_riscv_nacl_init(void);#ifdef CONFIG_32BIT #define lelong_to_cpu (__x) le32_to_cpu(__x)#define cpu_to_lelong (__x) cpu_to_le32(__x) +#define __lelong __le32#else #define lelong_to_cpu (__x) le64_to_cpu(__x)#define cpu_to_lelong (__x) cpu_to_le64(__x) +#define __lelong __le64#endif #define nacl_shmem () \@@ -70,7 +72,7 @@ int kvm_riscv_nacl_init(void);#define nacl_scratch_read_long (__shmem, __offset) \ ({ \ - unsigned long *__p = (__shmem) + \ + __lelong *__p = (__shmem) + \ SBI_NACL_SHMEM_SCRATCH_OFFSET + \ (__offset); \ lelong_to_cpu(*__p); \@@ -78,7 +80,7 @@ int kvm_riscv_nacl_init(void);#define nacl_scratch_write_long (__shmem, __offset, __val) \ do { \ - unsigned long *__p = (__shmem) + \ + __lelong *__p = (__shmem) + \ SBI_NACL_SHMEM_SCRATCH_OFFSET + \ (__offset); \ *__p = cpu_to_lelong(__val); \@@ -87,7 +89,7 @@ do { \#define nacl_scratch_write_longs (__shmem, __offset, __array, __count) \ do { \ unsigned int __i; \ - unsigned long *__p = (__shmem) + \ + __lelong *__p = (__shmem) + \ SBI_NACL_SHMEM_SCRATCH_OFFSET + \ (__offset); \ for (__i = 0; __i < (__count); __i++) \@@ -168,7 +170,7 @@ __kvm_riscv_nacl_hfence(__shmem, \#define nacl_csr_read (__shmem, __csr) \ ({ \ - unsigned long *__a = (__shmem) + SBI_NACL_SHMEM_CSR_OFFSET; \ + __lelong *__a = (__shmem) + SBI_NACL_SHMEM_CSR_OFFSET; \ lelong_to_cpu(__a[SBI_NACL_SHMEM_CSR_INDEX(__csr)]); \ })@@ -176,7 +178,7 @@ __kvm_riscv_nacl_hfence(__shmem, \ do { \ void *__s = (__shmem); \ unsigned int __i = SBI_NACL_SHMEM_CSR_INDEX(__csr); \ - unsigned long *__a = (__s) + SBI_NACL_SHMEM_CSR_OFFSET; \ + __lelong *__a = (__s) + SBI_NACL_SHMEM_CSR_OFFSET; \ u8 *__b = (__s) + SBI_NACL_SHMEM_DBITMAP_OFFSET; \ __a[__i] = cpu_to_lelong(__val); \ __b[__i >> 3] |= 1U << (__i & 0x7); \@@ -186,7 +188,7 @@ do { \ ({ \ void *__s = (__shmem); \ unsigned int __i = SBI_NACL_SHMEM_CSR_INDEX(__csr); \ - unsigned long *__a = (__s) + SBI_NACL_SHMEM_CSR_OFFSET; \ + __lelong *__a = (__s) + SBI_NACL_SHMEM_CSR_OFFSET; \ u8 *__b = (__s) + SBI_NACL_SHMEM_DBITMAP_OFFSET; \ unsigned long __r = lelong_to_cpu(__a[__i]); \ __a[__i] = cpu_to_lelong(__val); \arch/riscv/include/asm/uaccess.h +1
-1@@ -112,7 +112,7 @@ do { \ _ASM_EXTABLE_UACCESS_ERR(1b, %l2, %0) \ : "=&r" (__tmp) \ : "m" (*(ptr)) : : label); \ - (x) = (__typeof__(x))(unsigned long)__tmp; \ + (x) = (__force __typeof__(x))(unsigned long)__tmp; \ } while (0) #else /* !CONFIG_CC_HAS_ASM_GOTO_OUTPUT */ #define __get_user_asm(insn, x, ptr, label) \arch/riscv/kvm/nacl.c +1
-2@@ -20,7 +20,7 @@ void __kvm_riscv_nacl_hfence(void *shmem, unsigned long page_count) {int i, ent =-1 , try_count =5 ; -unsigned long *entp; +__lelong *entp; again:for (i =0 ; i <SBI_NACL_SHMEM_HFENCE_ENTRY_MAX ; i++) { --2.43.0
This patch was applied to riscv/linux.git (fixes) by Anup Patel <[email protected]>:
Hello:
This patch was applied to riscv/linux.git (fixes)
by Anup Patel <[email protected]>:
Quoted 10 lines · On Mon, 8 Jun 2026 23:52:52 +0800 you wrote:
When compiling with sparse enabled (C=2), bitwise type warnings are triggered in the RISC-V KVM implementation. This occurs because the user-space data unboxing macro '
__get_user_asm' performs implicit casting on restricted types without forcing the compiler's compliance.Additionally, raw 'unsigned long *' pointers are used to access the SBI NACL shared memory, whereas the RISC-V SBI specification mandates that these structures must follow little-endian byte ordering.
[...]
Here is the summary with links:
- [v1] riscv: kvm: Use endian-specific __lelong for NACL shared memory
https://git.kernel.org/riscv/c/52738352a6f2
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html