Re: [BUG]: mm/vmalloc: uninitialized variable access in pcpu_get_vm_areas

From: Arnd Bergmann
Date: Mon Jun 17 2019 - 10:56:01 EST


On Mon, Jun 17, 2019 at 4:44 PM Arnd Bergmann <arnd@xxxxxxxx> wrote:
> On Mon, Jun 17, 2019 at 4:12 PM Uladzislau Rezki <urezki@xxxxxxxxx> wrote:
> >
> > On Mon, Jun 17, 2019 at 02:14:11PM +0200, Arnd Bergmann wrote:
> > > gcc points out some obviously broken code in linux-next
> > >
> > > mm/vmalloc.c: In function 'pcpu_get_vm_areas':
> > > mm/vmalloc.c:991:4: error: 'lva' may be used uninitialized in this function [-Werror=maybe-uninitialized]
> > > insert_vmap_area_augment(lva, &va->rb_node,
> > > ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > > &free_vmap_area_root, &free_vmap_area_list);
> > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > > mm/vmalloc.c:916:20: note: 'lva' was declared here
> > > struct vmap_area *lva;
> > > ^~~
> > >
> > > Remove the obviously broken code. This is almost certainly
> > > not the correct solution, but it's what I have applied locally
> > > to get a clean build again.
> > >
> > > Please fix this properly.
> > >
>
> > >
> > Please do not apply this. It will just break everything.
>
> As I wrote in my description, this was purely meant as a bug
> report, not a patch to be applied.
>
> > As Roman pointed we can just set lva = NULL; in the beginning to make GCC happy.
> > For some reason GCC decides that it can be used uninitialized, but that
> > is not true.
>
> I got confused by the similarly named FL_FIT_TYPE/NE_FIT_TYPE
> constants and misread this as only getting run in the case where it is
> not initialized, but you are right that it always is initialized here.
>
> I see now that the actual cause of the warning is the 'while' loop in
> augment_tree_propagate_from(). gcc is unable to keep track of
> the state of the 'lva' variable beyond that and prints a bogus warning.

I managed to un-confuse gcc-8 by turning the if/else if/else into
a switch statement. If you all think this is an acceptable solution,
I'll submit that after some more testing to ensure it addresses
all configurations:

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index a9213fc3802d..5b7e50de008b 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -915,7 +915,8 @@ adjust_va_to_fit_type(struct vmap_area *va,
{
struct vmap_area *lva;

- if (type == FL_FIT_TYPE) {
+ switch (type) {
+ case FL_FIT_TYPE:
/*
* No need to split VA, it fully fits.
*
@@ -925,7 +926,8 @@ adjust_va_to_fit_type(struct vmap_area *va,
*/
unlink_va(va, &free_vmap_area_root);
kmem_cache_free(vmap_area_cachep, va);
- } else if (type == LE_FIT_TYPE) {
+ break;
+ case LE_FIT_TYPE:
/*
* Split left edge of fit VA.
*
@@ -934,7 +936,8 @@ adjust_va_to_fit_type(struct vmap_area *va,
* |-------|-------|
*/
va->va_start += size;
- } else if (type == RE_FIT_TYPE) {
+ break;
+ case RE_FIT_TYPE:
/*
* Split right edge of fit VA.
*
@@ -943,7 +946,8 @@ adjust_va_to_fit_type(struct vmap_area *va,
* |-------|-------|
*/
va->va_end = nva_start_addr;
- } else if (type == NE_FIT_TYPE) {
+ break;
+ case NE_FIT_TYPE:
/*
* Split no edge of fit VA.
*
@@ -980,7 +984,8 @@ adjust_va_to_fit_type(struct vmap_area *va,
* Shrink this VA to remaining size.
*/
va->va_start = nva_start_addr + size;
- } else {
+ break;
+ default:
return -1;
}

Arnd