diff options
author | Claudio Imbrenda <imbrenda@linux.ibm.com> | 2020-06-22 18:21:39 +0200 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2020-06-22 13:49:05 -0400 |
commit | 4aabe7c09a6be56ca6a115b5f4775ae3050be06e (patch) | |
tree | 43641861fe295d514e7e695255151772826944c7 | |
parent | dee4223b46b90103b2fd953764e3e430802a9624 (diff) | |
download | kvm-unit-tests-4aabe7c09a6be56ca6a115b5f4775ae3050be06e.tar.gz |
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>
-rw-r--r-- | lib/vmalloc.c | 10 |
1 files changed, 8 insertions, 2 deletions
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) |