Re: [PATCH] KVM: Fix a warning in __kvm_gfn_to_hva_cache_init()

Sean Christopherson
2020-05-05T01:39:33Z

On Mon, May 04, 2020 at 03:05:26PM -0400, Peter Xu wrote:
> GCC 10.0.1 gives me this warning when building KVM:
>
> warning: ‘nr_pages_avail’ may be used uninitialized in this function [-Wmaybe-uninitialized]
> 2442 | for ( ; start_gfn <= end_gfn; start_gfn += nr_pages_avail) {
>
> It should not happen, but silent it.

Heh, third times a charm? This has been reported and proposed twice
before[1][2]. Are you using any custom compiler flags? E.g. -O3 is known
to cause false positives with -Wmaybe-uninitialized.

If we do end up killing this warning, I'd still prefer to use
uninitialized_var() over zero-initializing the variable.

[1] https://lkml.kernel.org/r/20200218184756.242904-1-oupton@google.com
[2] https://bugzilla.kernel.org/show_bug.cgi?id=207173

>
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
> virt/kvm/kvm_main.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 74bdb7bf3295..2da293885a67 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -2425,7 +2425,7 @@ static int __kvm_gfn_to_hva_cache_init(struct kvm_memslots *slots,
> gfn_t start_gfn = gpa >> PAGE_SHIFT;
> gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT;
> gfn_t nr_pages_needed = end_gfn - start_gfn + 1;
> - gfn_t nr_pages_avail;
> + gfn_t nr_pages_avail = 0;
>
> /* Update ghc->generation before performing any error checks. */
> ghc->generation = slots->generation;
> --
> 2.26.2
>