kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Andrew Jones <drjones@redhat.com>, kvm@vger.kernel.org
Cc: lvivier@redhat.com, thuth@redhat.com
Subject: Re: [kvm-unit-tests PATCH v2 4/6] x86: lib/alloc: move heap management to lib
Date: Thu, 3 Nov 2016 18:21:37 +0100	[thread overview]
Message-ID: <21a2220f-cd28-015f-994e-35b45040aab3@redhat.com> (raw)
In-Reply-To: <1478119966-13252-5-git-send-email-drjones@redhat.com>



On 02/11/2016 21:52, Andrew Jones wrote:
> This will allow other arches to use {alloc,free}_page.

Let's make a new file lib/pagealloc.c.  This could potentially grow to
something like a buddy allocator.

Paolo

> Signed-off-by: Andrew Jones <drjones@redhat.com>
> ---
>  lib/alloc.c         | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
>  lib/alloc.h         | 10 ++++++++++
>  lib/x86/vm.c        | 48 ++----------------------------------------------
>  x86/Makefile.common |  1 +
>  4 files changed, 62 insertions(+), 46 deletions(-)
> 
> diff --git a/lib/alloc.c b/lib/alloc.c
> index 1d990a803825..ce1198e2977f 100644
> --- a/lib/alloc.c
> +++ b/lib/alloc.c
> @@ -5,6 +5,7 @@
>   */
>  #include "alloc.h"
>  #include "asm/spinlock.h"
> +#include "asm/page.h"
>  #include "asm/io.h"
>  
>  #define MIN(a, b)		((a) < (b) ? (a) : (b))
> @@ -150,6 +151,54 @@ phys_addr_t phys_zalloc(phys_addr_t size)
>  	return phys_zalloc_aligned(size, phys_alloc_align_min);
>  }
>  
> +static struct spinlock heap_lock;
> +static void *heap_free_head;
> +
> +void heap_init(void *start, size_t size)
> +{
> +	void *p = start;
> +
> +	assert(!((unsigned long)start & ~PAGE_MASK));
> +
> +	spin_lock(&heap_lock);
> +
> +	heap_free_head = NULL;
> +
> +	while (size >= PAGE_SIZE) {
> +		*(void **)p = heap_free_head;
> +		heap_free_head = p;
> +		p += PAGE_SIZE;
> +		size -= PAGE_SIZE;
> +	}
> +
> +	spin_unlock(&heap_lock);
> +}
> +
> +void *alloc_page(void)
> +{
> +	void *p;
> +
> +	spin_lock(&heap_lock);
> +
> +	if (!heap_free_head)
> +		return NULL;
> +
> +	p = heap_free_head;
> +	heap_free_head = *(void **)heap_free_head;
> +
> +	spin_unlock(&heap_lock);
> +
> +	return p;
> +}
> +
> +void free_page(void *page)
> +{
> +	spin_lock(&heap_lock);
> +	*(void **)page = heap_free_head;
> +	heap_free_head = page;
> +	spin_unlock(&heap_lock);
> +}
> +
>  static void *early_malloc(size_t size)
>  {
>  	phys_addr_t addr = phys_alloc_aligned_safe(size,
> diff --git a/lib/alloc.h b/lib/alloc.h
> index bd3c4e8ff3f6..a37330b3088a 100644
> --- a/lib/alloc.h
> +++ b/lib/alloc.h
> @@ -118,4 +118,14 @@ extern phys_addr_t phys_zalloc(phys_addr_t size);
>   */
>  extern void phys_alloc_show(void);
>  
> +/*
> + * Heap page allocator
> + *
> + * After initializing with heap_init, {alloc,free}_page can be used
> + * to easily manage pages.
> + */
> +extern void heap_init(void *start, size_t size);
> +extern void *alloc_page(void);
> +extern void free_page(void *page);
> +
>  #endif /* _ALLOC_H_ */
> diff --git a/lib/x86/vm.c b/lib/x86/vm.c
> index baea17e7f475..4e399f80dd31 100644
> --- a/lib/x86/vm.c
> +++ b/lib/x86/vm.c
> @@ -1,56 +1,12 @@
>  #include "fwcfg.h"
>  #include "vm.h"
>  #include "libcflat.h"
> +#include "alloc.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 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;
>  static unsigned long end_of_memory;
>  
> @@ -170,7 +126,7 @@ void setup_vm()
>  {
>      assert(!end_of_memory);
>      end_of_memory = fwcfg_get_u64(FW_CFG_RAM_SIZE);
> -    free_memory(&edata, end_of_memory - (unsigned long)&edata);
> +    heap_init(&edata, end_of_memory - (unsigned long)&edata);
>      setup_mmu(end_of_memory);
>  }
>  
> diff --git a/x86/Makefile.common b/x86/Makefile.common
> index 356d879a986b..88038e41af60 100644
> --- a/x86/Makefile.common
> +++ b/x86/Makefile.common
> @@ -3,6 +3,7 @@
>  all: test_cases
>  
>  cflatobjs += lib/pci.o
> +cflatobjs += lib/alloc.o
>  cflatobjs += lib/x86/io.o
>  cflatobjs += lib/x86/smp.o
>  cflatobjs += lib/x86/vm.o
> 

  parent reply	other threads:[~2016-11-03 17:21 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 ` [kvm-unit-tests PATCH v2 1/6] lib/x86/vm: collection of improvements Andrew Jones
2016-11-03  9:40   ` 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 [this message]
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=21a2220f-cd28-015f-994e-35b45040aab3@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=drjones@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=lvivier@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).