diff mbox series

[kvm-unit-tests,v1,6/8] lib/vmalloc: fix potential race and non-standard pointer arithmetic

Message ID 20200622162141.279716-7-imbrenda@linux.ibm.com (mailing list archive)
State New, archived
Headers show
Series Minor fixes, improvements, and cleanup | expand

Commit Message

Claudio Imbrenda June 22, 2020, 4:21 p.m. UTC
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 an ugly hack. 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>
---
 lib/vmalloc.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
diff mbox series

Patch

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 @@  static void *page_root;
 
 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)