KVM: squelch uninitialized variable warning
PATCH 1/1 KVM: squelch uninitialized variable warning
If gfn_to_hva_many() fails, __kvm_gfn_to_hva_cache_init() will return an error. Before it does, it might use nr_pages_avail, which my compiler complained about:
virt/kvm/kvm_main.c:2193:13: warning: 'nr_pages_avail' may be used uninitialized in this function [-Wmaybe-uninitialized] start_gfn += nr_pages_avail;
--- virt/kvm/kvm_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
virt/kvm/kvm_main.c +1 -1
@@ -2172,7 +2172,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 ;int r = start_gfn <= end_gfn ?0 : -EINVAL ; ghc->gpa = gpa;
Signature · 1 line
2.25.0.rc1.283.g88dfdc4193-goog
Quoted 8 lines · Barret Rhoden
If
gfn_to_hva_many()fails,__kvm_gfn_to_hva_cache_init()will return an error. Before it does, it might usenr_pages_avail, which my compiler complained about:virt/kvm/kvm_main.c:2193:13: warning: 'nr_pages_avail' may be used uninitialized in this function [-Wmaybe-uninitialized] start_gfn += nr_pages_avail;
Ugh, this whole flow is funky. The change is correct, and is certainly the minimal change if we want to backport this to stable, but IMO it's putting lipstick on a pig. I'd rather fix the underlying issues and make the code more readable in the process. I'll send a patch.
If we want to take this as a minimal fix,
Quoted 21 lines
Signed-off-by Barret Rhoden <[email protected]>--- virt/kvm/kvm_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)virt/kvm/kvm_main.c +1
-1@@ -2172,7 +2172,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 ;int r = start_gfn <= end_gfn ?0 : -EINVAL ; ghc->gpa = gpa;Signature · 1 line
2.25.0.rc1.283.g88dfdc4193-goog
Paolo Bonzini
maintainerQuoted 1 line · Sean Christopherson
Reviewed-by Sean Christopherson <[email protected]>
Thanks, I queued it so that at least it gets to stable.
Paolo