Skip to contentSkip to conversation

KVM: x86: fix cocci warnings

PATCH In review Vihas Mak 5 participants 9 messages Nov 14, 2021 – Nov 16, 2021

Subsystems (3)
lore
Share & tools
lore

Vihas Mak

author
#0 lore raw

PATCH 1/1 KVM: x86: fix cocci warnings

change 0 to false and 1 to true to fix following cocci warnings:

arch/x86/kvm/mmu/mmu.c:1485:9-10: WARNING: return of 0/1 in function 'kvm_set_pte_rmapp' with return type bool arch/x86/kvm/mmu/mmu.c:1636:10-11: WARNING: return of 0/1 in function 'kvm_test_age_rmapp' with return type bool

Cc Sean Christopherson <[email protected]>
Cc Vitaly Kuznetsov <[email protected]>
Cc Wanpeng Li <[email protected]>
Cc Jim Mattson <[email protected]>
Cc Joerg Roedel <[email protected]>
---
 arch/x86/kvm/mmu/mmu.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu/mmu.c +5 -5
index 337943799..2fcea4a78 100644
@@ -1454,7 +1454,7 @@ static bool kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
 {
 	u64 *sptep;
 	struct rmap_iterator iter;
-	int need_flush = 0;
+	bool need_flush = false;
 	u64 new_spte;
 	kvm_pfn_t new_pfn;
 
@@ -1466,7 +1466,7 @@ static bool kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
 		rmap_printk("spte %p %llx gfn %llx (%d)\n",
 			    sptep, *sptep, gfn, level);
 
-		need_flush = 1;
+		need_flush = true;
 
 		if (pte_write(pte)) {
 			pte_list_remove(kvm, rmap_head, sptep);
@@ -1482,7 +1482,7 @@ static bool kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
 
 	if (need_flush && kvm_available_flush_tlb_with_range()) {
 		kvm_flush_remote_tlbs_with_address(kvm, gfn, 1);
-		return 0;
+		return false;
 	}
 
 	return need_flush;
@@ -1623,8 +1623,8 @@ static bool kvm_test_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
 
 	for_each_rmap_spte(rmap_head, &iter, sptep)
 		if (is_accessed_spte(*sptep))
-			return 1;
-	return 0;
+			return true;
+	return false;
 }
 
 #define RMAP_RECYCLE_THRESHOLD 1000
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 47 lines · Vihas Mak

change 0 to false and 1 to true to fix following cocci warnings:

arch/x86/kvm/mmu/mmu.c:1485:9-10: WARNING: return of 0/1 in function 'kvm_set_pte_rmapp' with return type bool arch/x86/kvm/mmu/mmu.c:1636:10-11: WARNING: return of 0/1 in function 'kvm_test_age_rmapp' with return type bool

Cc Sean Christopherson <[email protected]>
Cc Vitaly Kuznetsov <[email protected]>
Cc Wanpeng Li <[email protected]>
Cc Jim Mattson <[email protected]>
Cc Joerg Roedel <[email protected]>
---
 arch/x86/kvm/mmu/mmu.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu/mmu.c +3 -3
index 337943799..2fcea4a78 100644
@@ -1454,7 +1454,7 @@ static bool kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
 {
 	u64 *sptep;
 	struct rmap_iterator iter;
-	int need_flush = 0;
+	bool need_flush = false;
 	u64 new_spte;
 	kvm_pfn_t new_pfn;
 
@@ -1466,7 +1466,7 @@ static bool kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
 		rmap_printk("spte %p %llx gfn %llx (%d)\n",
 			    sptep, *sptep, gfn, level);
 
-		need_flush = 1;
+		need_flush = true;
 
 		if (pte_write(pte)) {
 			pte_list_remove(kvm, rmap_head, sptep);
@@ -1482,7 +1482,7 @@ static bool kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
 
 	if (need_flush && kvm_available_flush_tlb_with_range()) {
 		kvm_flush_remote_tlbs_with_address(kvm, gfn, 1);
-		return 0;
+		return false;
 	}
 
 	return need_flush;

arch/x86/kvm/mmu/mmu.c static bool kvm_test_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head, · line 1630

Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +2 -2
@@ -1623,8 +1623,8 @@ static bool kvm_test_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
 
 	for_each_rmap_spte(rmap_head, &iter, sptep)
 		if (is_accessed_spte(*sptep))
-			return 1;
-	return 0;
+			return true;
+	return false;
 }
 
 #define RMAP_RECYCLE_THRESHOLD 1000

One minor remark: 'kvm_set_pte_rmapp()' handler is passed to 'kvm_handle_gfn_range()' which does

        bool ret = false;

        for_each_slot_rmap_range(...)
                ret |= handler(...);

and I find '|=' to not be very natural with booleans. I'm not sure it's worth changing though.

Signature · 1 line
Vitaly
author
#2 lore raw
Quoted 2 lines

and I find '|=' to not be very natural with booleans. I'm not sure it's worth changing though.

I see. But there are many functions in which '|=' is used on booleans. get_mmio_spte(), __rmap_write_protect(), kvm_handle_gfn_range and many more. That's why I thought it would be better if the code follows the same convention.

Thanks,
Vihas

Quoted 77 lines · Vitaly Kuznetsov
Quoted 47 lines · Vihas Mak

change 0 to false and 1 to true to fix following cocci warnings:

arch/x86/kvm/mmu/mmu.c:1485:9-10: WARNING: return of 0/1 in function 'kvm_set_pte_rmapp' with return type bool arch/x86/kvm/mmu/mmu.c:1636:10-11: WARNING: return of 0/1 in function 'kvm_test_age_rmapp' with return type bool

Cc Sean Christopherson <[email protected]>
Cc Vitaly Kuznetsov <[email protected]>
Cc Wanpeng Li <[email protected]>
Cc Jim Mattson <[email protected]>
Cc Joerg Roedel <[email protected]>
---
 arch/x86/kvm/mmu/mmu.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu/mmu.c +3 -3
index 337943799..2fcea4a78 100644
@@ -1454,7 +1454,7 @@ static bool kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
 {
      u64 *sptep;
      struct rmap_iterator iter;
-     int need_flush = 0;
+     bool need_flush = false;
      u64 new_spte;
      kvm_pfn_t new_pfn;
 
@@ -1466,7 +1466,7 @@ static bool kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
              rmap_printk("spte %p %llx gfn %llx (%d)\n",
                          sptep, *sptep, gfn, level);
 
-             need_flush = 1;
+             need_flush = true;
 
              if (pte_write(pte)) {
                      pte_list_remove(kvm, rmap_head, sptep);
@@ -1482,7 +1482,7 @@ static bool kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
 
      if (need_flush && kvm_available_flush_tlb_with_range()) {
              kvm_flush_remote_tlbs_with_address(kvm, gfn, 1);
-             return 0;
+             return false;
      }
 
      return need_flush;

arch/x86/kvm/mmu/mmu.c static bool kvm_test_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head, · line 1630

Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +2 -2
@@ -1623,8 +1623,8 @@ static bool kvm_test_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
 
      for_each_rmap_spte(rmap_head, &iter, sptep)
              if (is_accessed_spte(*sptep))
-                     return 1;
-     return 0;
+                     return true;
+     return false;
 }
 
 #define RMAP_RECYCLE_THRESHOLD 1000

One minor remark: 'kvm_set_pte_rmapp()' handler is passed to 'kvm_handle_gfn_range()' which does

        bool ret = false;

        for_each_slot_rmap_range(...)
                ret |= handler(...);

and I find '|=' to not be very natural with booleans. I'm not sure it's worth changing though.

--
Vitaly

maintainer
#3 lore raw
In reply to Vitaly Kuznetsov
Quoted 10 lines · Vitaly Kuznetsov

One minor remark: 'kvm_set_pte_rmapp()' handler is passed to 'kvm_handle_gfn_range()' which does

         bool ret = false;

         for_each_slot_rmap_range(...)
                 ret |= handler(...);

and I find '|=' to not be very natural with booleans. I'm not sure it's worth changing though.

Changing that would be "harder" than it seems because "ret = ret || handler(...)" is wrong, and "|" is even more unnatural than "|=" (so much that clang warns about it).

In fact I wonder if "|=" with a bool might end up warning with clang, which we should check before applying this patch. It doesn't seem to be in the original commit[1], but better safe than sorry: Nick, does clang intend to warn also about "ret |= fn()" and "ret &= fn()"? Technically, it is a bitwise operation with side-effects in the RHS.

Paolo

[1] https://github.com/llvm/llvm-project/commit/f59cc9542bfb461

#4 lore raw
Quoted 17 lines · Paolo Bonzini
Quoted 10 lines · Vitaly Kuznetsov

One minor remark: 'kvm_set_pte_rmapp()' handler is passed to 'kvm_handle_gfn_range()' which does

         bool ret = false;

         for_each_slot_rmap_range(...)
                 ret |= handler(...);

and I find '|=' to not be very natural with booleans. I'm not sure it's worth changing though.

Changing that would be "harder" than it seems because "ret = ret || handler(...)" is wrong, and "|" is even more unnatural than "|=" (so much that clang warns about it).

Quoted 5 lines

In fact I wonder if "|=" with a bool might end up warning with clang, which we should check before applying this patch. It doesn't seem to be in the original commit[1], but better safe than sorry: Nick, does clang intend to warn also about "ret |= fn()" and "ret &= fn()"? Technically, it is a bitwise operation with side-effects in the RHS.

I think that warning had more to due with typo's where || or && was meant (to short circuit the side effects) but | or & was typed by accident, keeping both side effects. I'm not sure what the typo would be in ret |= fn();.

Quoted 5 lines

Paolo

[1] https://github.com/llvm/llvm-project/commit/f59cc9542bfb461

Signature · 2 lines
Thanks,
~Nick Desaulniers
#5 lore raw
In reply to Vihas Mak
Quoted 4 lines · Vihas Mak

change 0 to false and 1 to true to fix following cocci warnings:

arch/x86/kvm/mmu/mmu.c:1485:9-10: WARNING: return of 0/1 in function 'kvm_set_pte_rmapp' with return type bool arch/x86/kvm/mmu/mmu.c:1636:10-11: WARNING: return of 0/1 in function 'kvm_test_age_rmapp' with return type bool

That script should be deleted, it's absolute garbage.

maintainer
#6 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 4 lines · Vihas Mak

change 0 to false and 1 to true to fix following cocci warnings:

arch/x86/kvm/mmu/mmu.c:1485:9-10: WARNING: return of 0/1 in function 'kvm_set_pte_rmapp' with return type bool arch/x86/kvm/mmu/mmu.c:1636:10-11: WARNING: return of 0/1 in function 'kvm_test_age_rmapp' with return type bool

That script should be deleted, it's absolute garbage.

Only a Sith deals in absolutes.

Paolo

#7 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 8 lines · Peter Zijlstra
Quoted 4 lines · Vihas Mak

change 0 to false and 1 to true to fix following cocci warnings:

arch/x86/kvm/mmu/mmu.c:1485:9-10: WARNING: return of 0/1 in function 'kvm_set_pte_rmapp' with return type bool arch/x86/kvm/mmu/mmu.c:1636:10-11: WARNING: return of 0/1 in function 'kvm_test_age_rmapp' with return type bool

That script should be deleted, it's absolute garbage.

Only a Sith deals in absolutes.

Is that a star-wars thingy?

In C 0 is a valid way to spell false, equally, any non-0 value is a valid way to spell true. Why would this rate a warn?

In fact, when casting _Bool to integer, you get 0 and 1. When looking at the memory content of the _Bool variable, you'll get 0 and 1. But we're not allowed to write 0 and 1?

maintainer
#8 lore raw
Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Paolo Bonzini
Quoted 3 lines · Peter Zijlstra
Quoted 2 lines · Vihas Mak

change 0 to false and 1 to true to fix following cocci warnings:

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

arch/x86/kvm/mmu/mmu.c:1485:9-10: WARNING: return of 0/1 in function 'kvm_set_pte_rmapp' with return type bool arch/x86/kvm/mmu/mmu.c:1636:10-11: WARNING: return of 0/1 in function 'kvm_test_age_rmapp' with return type bool

That script should be deleted, it's absolute garbage.

Only a Sith deals in absolutes.

Is that a star-wars thingy?

Yes, it is. "If you're not with me, then you're my enemy!" "Only a Sith deals in absolutes". :)

Quoted 2 lines

In C 0 is a valid way to spell false, equally, any non-0 value is a valid way to spell true. Why would this rate a warn?

Because often 0 means success (if -errno means failure). So if you write false/true consistently for bool and 0 only for int, it's one less thing that one can get wrong. At least that's the rationale.

Paolo

Quoted 4 lines

In fact, when casting _Bool to integer, you get 0 and 1. When looking at the memory content of the _Bool variable, you'll get 0 and 1. But we're not allowed to write 0 and 1?