kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Jones <drjones@redhat.com>
To: kvm@vger.kernel.org
Cc: pbonzini@redhat.com, lvivier@redhat.com, thuth@redhat.com
Subject: [kvm-unit-tests PATCH v2 1/6] lib/x86/vm: collection of improvements
Date: Wed,  2 Nov 2016 21:52:41 +0100	[thread overview]
Message-ID: <1478119966-13252-2-git-send-email-drjones@redhat.com> (raw)
In-Reply-To: <1478119966-13252-1-git-send-email-drjones@redhat.com>

Ensure we're page aligned, add locking, just return if NULL is
passed to vfree().

Signed-off-by: Andrew Jones <drjones@redhat.com>
---
 lib/x86/asm/page.h |  2 ++
 lib/x86/vm.c       | 44 +++++++++++++++++++++++++++++++++++++-------
 2 files changed, 39 insertions(+), 7 deletions(-)

diff --git a/lib/x86/asm/page.h b/lib/x86/asm/page.h
index 5044a49ab0cc..dd999304f1f0 100644
--- a/lib/x86/asm/page.h
+++ b/lib/x86/asm/page.h
@@ -16,6 +16,8 @@
 
 #ifndef __ASSEMBLY__
 
+#define PAGE_ALIGN(addr)	ALIGN(addr, PAGE_SIZE)
+
 #ifdef __x86_64__
 #define LARGE_PAGE_SIZE	(512 * PAGE_SIZE)
 #else
diff --git a/lib/x86/vm.c b/lib/x86/vm.c
index f7e778b3779c..baea17e7f475 100644
--- a/lib/x86/vm.c
+++ b/lib/x86/vm.c
@@ -1,37 +1,54 @@
 #include "fwcfg.h"
 #include "vm.h"
 #include "libcflat.h"
+#include "asm/spinlock.h"
 
+static struct spinlock heap_lock;
+static struct spinlock vm_lock;
 static void *free = 0;
 static void *vfree_top = 0;
 
 static void free_memory(void *mem, unsigned long size)
 {
+    assert(!((unsigned long)mem & ~PAGE_MASK));
+
+    spin_lock(&heap_lock);
+
+    free = NULL;
+
     while (size >= PAGE_SIZE) {
 	*(void **)mem = free;
 	free = mem;
 	mem += PAGE_SIZE;
 	size -= PAGE_SIZE;
     }
+
+    spin_unlock(&heap_lock);
 }
 
 void *alloc_page()
 {
     void *p;
 
+    spin_lock(&heap_lock);
+
     if (!free)
-	return 0;
+	return NULL;
 
     p = free;
     free = *(void **)free;
 
+    spin_unlock(&heap_lock);
+
     return p;
 }
 
 void free_page(void *page)
 {
+    spin_lock(&heap_lock);
     *(void **)page = free;
     free = page;
+    spin_unlock(&heap_lock);
 }
 
 extern char edata;
@@ -162,11 +179,13 @@ void *vmalloc(unsigned long size)
     void *mem, *p;
     unsigned pages;
 
-    size += sizeof(unsigned long);
+    size = PAGE_ALIGN(size + sizeof(unsigned long));
 
-    size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
+    spin_lock(&vm_lock);
     vfree_top -= size;
     mem = p = vfree_top;
+    spin_unlock(&vm_lock);
+
     pages = size / PAGE_SIZE;
     while (pages--) {
 	install_page(phys_to_virt(read_cr3()), virt_to_phys(alloc_page()), p);
@@ -179,12 +198,18 @@ void *vmalloc(unsigned long size)
 
 uint64_t virt_to_phys_cr3(void *mem)
 {
-    return (*get_pte(phys_to_virt(read_cr3()), mem) & PT_ADDR_MASK) + ((ulong)mem & (PAGE_SIZE - 1));
+    return (*get_pte(phys_to_virt(read_cr3()), mem) & PT_ADDR_MASK) + ((ulong)mem & ~PAGE_MASK);
 }
 
 void vfree(void *mem)
 {
-    unsigned long size = ((unsigned long *)mem)[-1];
+    unsigned long size;
+
+    if (mem == NULL)
+	return;
+
+    mem -= sizeof(unsigned long);
+    size = *(unsigned long *)mem;
 
     while (size) {
 	free_page(phys_to_virt(*get_pte(phys_to_virt(read_cr3()), mem) & PT_ADDR_MASK));
@@ -198,11 +223,14 @@ void *vmap(unsigned long long phys, unsigned long size)
     void *mem, *p;
     unsigned pages;
 
-    size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
-    vfree_top -= size;
+    size = PAGE_ALIGN(size);
     phys &= ~(unsigned long long)(PAGE_SIZE - 1);
 
+    spin_lock(&vm_lock);
+    vfree_top -= size;
     mem = p = vfree_top;
+    spin_unlock(&vm_lock);
+
     pages = size / PAGE_SIZE;
     while (pages--) {
 	install_page(phys_to_virt(read_cr3()), phys, p);
@@ -214,7 +242,9 @@ void *vmap(unsigned long long phys, unsigned long size)
 
 void *alloc_vpages(ulong nr)
 {
+	spin_lock(&vm_lock);
 	vfree_top -= PAGE_SIZE * nr;
+	spin_unlock(&vm_lock);
 	return vfree_top;
 }
 
-- 
2.7.4


  reply	other threads:[~2016-11-02 20:52 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-02 20:52 [kvm-unit-tests PATCH v2 0/6] x86: enable malloc and friends Andrew Jones
2016-11-02 20:52 ` Andrew Jones [this message]
2016-11-03  9:40   ` [kvm-unit-tests PATCH v2 1/6] lib/x86/vm: collection of improvements Laurent Vivier
2016-11-03 10:42     ` Andrew Jones
2016-11-02 20:52 ` [kvm-unit-tests PATCH v2 2/6] lib/alloc: improve init Andrew Jones
2016-11-03 10:57   ` Laurent Vivier
2016-11-02 20:52 ` [kvm-unit-tests PATCH v2 3/6] lib/alloc: prepare to extend alloc.c's purpose Andrew Jones
2016-11-03 12:30   ` Laurent Vivier
2016-11-02 20:52 ` [kvm-unit-tests PATCH v2 4/6] x86: lib/alloc: move heap management to lib Andrew Jones
2016-11-03 13:28   ` Laurent Vivier
2016-11-03 13:38     ` Andrew Jones
2016-11-03 17:21   ` Paolo Bonzini
2016-11-03 17:53     ` Andrew Jones
2016-11-02 20:52 ` [kvm-unit-tests PATCH v2 5/6] x86: lib/alloc: introduce alloc_zeroed_page Andrew Jones
2016-11-03 12:02   ` Laurent Vivier
2016-11-03 12:47     ` Andrew Jones
2016-11-03 13:03       ` Laurent Vivier
2016-11-02 20:52 ` [kvm-unit-tests PATCH v2 6/6] lib/x86/vm: enable malloc and friends Andrew Jones
2016-11-03 14:12   ` Paolo Bonzini
2016-11-03 14:58     ` Andrew Jones
2016-11-03 16:00       ` Paolo Bonzini
2016-11-03 16:51         ` Andrew Jones
2016-11-03 17:34           ` Paolo Bonzini
2016-11-03 18:12             ` Andrew Jones
2016-11-03 18:20               ` Paolo Bonzini
2016-11-03 18:42                 ` Andrew Jones
2016-11-03 19:19                   ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1478119966-13252-2-git-send-email-drjones@redhat.com \
    --to=drjones@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).