lib/vmalloc: fix potential race and non-standard pointer arithmetic
The pointer vfree_top should only be accessed with the lock held, so
make sure we return a local copy of the pointer taken safely inside the
lock.
Also avoid doing pointer arithmetic on void pointers. Gcc allows it but
it is ugly. Use uintptr_t for doing maths on the pointer.
This will also come useful in upcoming patches.
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
Message-Id: <20200622162141.279716-7-imbrenda@linux.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
diff --git a/lib/vmalloc.c b/lib/vmalloc.c
index 74b785c..83e34aa 100644
--- a/lib/vmalloc.c
+++ b/lib/vmalloc.c
@@ -20,10 +20,16 @@
void *alloc_vpages(ulong nr)
{
+ uintptr_t ptr;
+
spin_lock(&lock);
- vfree_top -= PAGE_SIZE * nr;
+ ptr = (uintptr_t)vfree_top;
+ ptr -= PAGE_SIZE * nr;
+ vfree_top = (void *)ptr;
spin_unlock(&lock);
- return vfree_top;
+
+ /* Cannot return vfree_top here, we are outside the lock! */
+ return (void *)ptr;
}
void *alloc_vpage(void)