Skip to contentSkip to conversation

RISC-V: Fix sparse warnings

PATCH New Andrew Jones 3 participants 8 messages Jan 31, 2024 – Feb 2, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover RISC-V: Fix sparse warnings

After the steal-time series was merged, LKP sent me mail with sparse warnings for the lack of using appropriate restricted types and a local variable not being static. This series makes the fixups necessary to appease sparse for 64-bit compiles. Changes to __get_user_8() and __put_user_8() are needed to completely quiet sparse for 32-bit compiles. I'll address those 32-bit sparse warnings separately, since it's not 100% clear how best to approach a couple of them.

Thanks,
drew

Andrew Jones (3):
RISC-V: paravirt: steal_time should be static
RISC-V: paravirt: Use correct restricted types
RISC-V: KVM: Use correct restricted types

 arch/riscv/kernel/paravirt.c  |  6 +++---
 arch/riscv/kvm/vcpu_sbi_sta.c | 20 ++++++++++++--------
 2 files changed, 15 insertions(+), 11 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 RISC-V: paravirt: steal_time should be static

steal_time is not used outside paravirt.c, make it static, as sparse suggested.

Fixes fdf68acccfc6 ("RISC-V: paravirt: Implement steal-time support")
---
 arch/riscv/kernel/paravirt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/paravirt.c +1 -1
index 8e114f5930ce..15c5d4048db5 100644
@@ -41,7 +41,7 @@ static int __init parse_no_stealacc(char *arg)
 
 early_param("no-steal-acc", parse_no_stealacc);
 
-DEFINE_PER_CPU(struct sbi_sta_struct, steal_time) __aligned(64);
+static DEFINE_PER_CPU(struct sbi_sta_struct, steal_time) __aligned(64);
 
 static bool __init has_pv_steal_clock(void)
 {
Signature · 1 line
2.43.0
#3 lore raw

PATCH 2/3 RISC-V: paravirt: Use correct restricted types

In reply to Andrew Jones

__le32 and __le64 types should be used with le32_to_cpu() and le64_to_cpu(), as sparse helpfully points out.

Fixes fdf68acccfc6 ("RISC-V: paravirt: Implement steal-time support")
---
 arch/riscv/kernel/paravirt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/paravirt.c +2 -2
index 15c5d4048db5..0d6225fd3194 100644
@@ -91,8 +91,8 @@ static int pv_time_cpu_down_prepare(unsigned int cpu)
 static u64 pv_time_steal_clock(int cpu)
 {
 	struct sbi_sta_struct *st = per_cpu_ptr(&steal_time, cpu);
-	u32 sequence;
-	u64 steal;
+	__le32 sequence;
+	__le64 steal;
 
 	/*
 	 * Check the sequence field before and after reading the steal
Signature · 1 line
2.43.0
#5 lore raw

PATCH 3/3 RISC-V: KVM: Use correct restricted types

In reply to Andrew Jones

__le32 and __le64 types should be used with le32_to_cpu() and le64_to_cpu() and __user is needed for pointers referencing guest memory, as sparse helpfully points out.

Fixes e9f12b5fff8a ("RISC-V: KVM: Implement SBI STA extension")
---
 arch/riscv/kvm/vcpu_sbi_sta.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
arch/riscv/kvm/vcpu_sbi_sta.c +12 -8
index 01f09fe8c3b0..d8cf9ca28c61 100644
@@ -26,8 +26,12 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
 {
 	gpa_t shmem = vcpu->arch.sta.shmem;
 	u64 last_steal = vcpu->arch.sta.last_steal;
-	u32 *sequence_ptr, sequence;
-	u64 *steal_ptr, steal;
+	__le32 __user *sequence_ptr;
+	__le64 __user *steal_ptr;
+	__le32 sequence_le;
+	__le64 steal_le;
+	u32 sequence;
+	u64 steal;
 	unsigned long hva;
 	gfn_t gfn;
 
@@ -47,22 +51,22 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
 		return;
 	}
 
-	sequence_ptr = (u32 *)(hva + offset_in_page(shmem) +
+	sequence_ptr = (__le32 __user *)(hva + offset_in_page(shmem) +
 			       offsetof(struct sbi_sta_struct, sequence));
-	steal_ptr = (u64 *)(hva + offset_in_page(shmem) +
+	steal_ptr = (__le64 __user *)(hva + offset_in_page(shmem) +
 			    offsetof(struct sbi_sta_struct, steal));
 
-	if (WARN_ON(get_user(sequence, sequence_ptr)))
+	if (WARN_ON(get_user(sequence_le, sequence_ptr)))
 		return;
 
-	sequence = le32_to_cpu(sequence);
+	sequence = le32_to_cpu(sequence_le);
 	sequence += 1;
 
 	if (WARN_ON(put_user(cpu_to_le32(sequence), sequence_ptr)))
 		return;
 
-	if (!WARN_ON(get_user(steal, steal_ptr))) {
-		steal = le64_to_cpu(steal);
+	if (!WARN_ON(get_user(steal_le, steal_ptr))) {
+		steal = le64_to_cpu(steal_le);
 		vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
 		steal += vcpu->arch.sta.last_steal - last_steal;
 		WARN_ON(put_user(cpu_to_le64(steal), steal_ptr));
Signature · 1 line
2.43.0
#2 lore raw

PATCH 1/3 RISC-V: paravirt: steal_time should be static

In reply to Andrew Jones
Quoted 15 lines · On Wed, Jan 31, 2024 at 4:05?AM Andrew Jones <[email protected]> wrote:

steal_time is not used outside paravirt.c, make it static, as sparse suggested.

Fixes fdf68acccfc6 ("RISC-V: paravirt: Implement steal-time support")
---
 arch/riscv/kernel/paravirt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/paravirt.c +0 -0
index 8e114f5930ce..15c5d4048db5 100644

arch/riscv/kernel/paravirt.c static int __init parse_no_stealacc(char *arg) · line 45

Quoted 6 lines
arch/riscv/kernel/paravirt.c +1 -1
@@ -41,7 +41,7 @@ static int __init parse_no_stealacc(char *arg)
 
 early_param("no-steal-acc", parse_no_stealacc);
 
-DEFINE_PER_CPU(struct sbi_sta_struct, steal_time) __aligned(64);
+static DEFINE_PER_CPU(struct sbi_sta_struct, steal_time) __aligned(64);
 
Quoted 5 lines
arch/riscv/kernel/paravirt.c +0 -1
 static bool __init has_pv_steal_clock(void)
 {
--

2.43.0

Signature · 2 lines
Regards,
Atish
#4 lore raw

PATCH 2/3 RISC-V: paravirt: Use correct restricted types

In reply to Andrew Jones
Quoted 28 lines · On Wed, Jan 31, 2024 at 4:05?AM Andrew Jones <[email protected]> wrote:

__le32 and __le64 types should be used with le32_to_cpu() and le64_to_cpu(), as sparse helpfully points out.

Fixes fdf68acccfc6 ("RISC-V: paravirt: Implement steal-time support")
---
 arch/riscv/kernel/paravirt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/paravirt.c +2 -3
index 15c5d4048db5..0d6225fd3194 100644
@@ -91,8 +91,8 @@ static int pv_time_cpu_down_prepare(unsigned int cpu)
 static u64 pv_time_steal_clock(int cpu)
 {
        struct sbi_sta_struct *st = per_cpu_ptr(&steal_time, cpu);
-       u32 sequence;
-       u64 steal;
+       __le32 sequence;
+       __le64 steal;
 
        /*
         * Check the sequence field before and after reading the steal
--
Quoted 2 lines

2.43.0

Signature · 2 lines
Regards,
Atish
#6 lore raw

PATCH 3/3 RISC-V: KVM: Use correct restricted types

In reply to Andrew Jones
Quoted 62 lines · On Wed, Jan 31, 2024 at 4:05?AM Andrew Jones <[email protected]> wrote:

__le32 and __le64 types should be used with le32_to_cpu() and le64_to_cpu() and __user is needed for pointers referencing guest memory, as sparse helpfully points out.

Fixes e9f12b5fff8a ("RISC-V: KVM: Implement SBI STA extension")
---
 arch/riscv/kvm/vcpu_sbi_sta.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
arch/riscv/kvm/vcpu_sbi_sta.c +12 -9
index 01f09fe8c3b0..d8cf9ca28c61 100644
@@ -26,8 +26,12 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
 {
        gpa_t shmem = vcpu->arch.sta.shmem;
        u64 last_steal = vcpu->arch.sta.last_steal;
-       u32 *sequence_ptr, sequence;
-       u64 *steal_ptr, steal;
+       __le32 __user *sequence_ptr;
+       __le64 __user *steal_ptr;
+       __le32 sequence_le;
+       __le64 steal_le;
+       u32 sequence;
+       u64 steal;
        unsigned long hva;
        gfn_t gfn;
 
@@ -47,22 +51,22 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
                return;
        }
 
-       sequence_ptr = (u32 *)(hva + offset_in_page(shmem) +
+       sequence_ptr = (__le32 __user *)(hva + offset_in_page(shmem) +
                               offsetof(struct sbi_sta_struct, sequence));
-       steal_ptr = (u64 *)(hva + offset_in_page(shmem) +
+       steal_ptr = (__le64 __user *)(hva + offset_in_page(shmem) +
                            offsetof(struct sbi_sta_struct, steal));
 
-       if (WARN_ON(get_user(sequence, sequence_ptr)))
+       if (WARN_ON(get_user(sequence_le, sequence_ptr)))
                return;
 
-       sequence = le32_to_cpu(sequence);
+       sequence = le32_to_cpu(sequence_le);
        sequence += 1;
 
        if (WARN_ON(put_user(cpu_to_le32(sequence), sequence_ptr)))
                return;
 
-       if (!WARN_ON(get_user(steal, steal_ptr))) {
-               steal = le64_to_cpu(steal);
+       if (!WARN_ON(get_user(steal_le, steal_ptr))) {
+               steal = le64_to_cpu(steal_le);
                vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
                steal += vcpu->arch.sta.last_steal - last_steal;
                WARN_ON(put_user(cpu_to_le64(steal), steal_ptr));
--
Quoted 2 lines

2.43.0

Signature · 2 lines
Regards,
Atish
maintainer
#7 lore raw

cover RISC-V: Fix sparse warnings

In reply to Andrew Jones
Quoted 10 lines · On Wed, Jan 31, 2024 at 5:35?PM Andrew Jones <[email protected]> wrote:

After the steal-time series was merged, LKP sent me mail with sparse warnings for the lack of using appropriate restricted types and a local variable not being static. This series makes the fixups necessary to appease sparse for 64-bit compiles. Changes to __get_user_8() and __put_user_8() are needed to completely quiet sparse for 32-bit compiles. I'll address those 32-bit sparse warnings separately, since it's not 100% clear how best to approach a couple of them.

Quoted 8 lines

Thanks,
drew

Andrew Jones (3):
RISC-V: paravirt: steal_time should be static
RISC-V: paravirt: Use correct restricted types
RISC-V: KVM: Use correct restricted types

Queued this series as fixes for Linux-6.8

Thanks,
Anup

Quoted 8 lines
 arch/riscv/kernel/paravirt.c  |  6 +++---
 arch/riscv/kvm/vcpu_sbi_sta.c | 20 ++++++++++++--------
 2 files changed, 15 insertions(+), 11 deletions(-)

--
2.43.0