From: Andrew Jones <drjones@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org,
christoffer.dall@linaro.org
Subject: Re: [PATCH v6 06/17] Introduce alloc_ops
Date: Tue, 15 Jul 2014 19:16:47 +0200 [thread overview]
Message-ID: <20140715171647.GA15967@hawk.usersys.redhat.com> (raw)
In-Reply-To: <20140711100725.GE6013@hawk.usersys.redhat.com>
On Fri, Jul 11, 2014 at 12:07:25PM +0200, Andrew Jones wrote:
> On Fri, Jul 11, 2014 at 11:41:34AM +0200, Paolo Bonzini wrote:
> > Il 11/07/2014 10:55, Andrew Jones ha scritto:
> > >On Fri, Jul 11, 2014 at 10:40:42AM +0200, Paolo Bonzini wrote:
> > >>Il 11/07/2014 10:19, Andrew Jones ha scritto:
> > >>>alloc_ops provide interfaces for alloc(), free() and friends, allowing
> > >>>unit tests and common code to use dynamic memory allocation.
> > >>>arch-specific code must provide the implementations.
> > >>>
> > >>>Signed-off-by: Andrew Jones <drjones@redhat.com>
> > >>>---
> > >>>lib/alloc.c | 2 ++
> > >>>lib/alloc.h | 31 +++++++++++++++++++++++++++++++
> > >>>2 files changed, 33 insertions(+)
> > >>>create mode 100644 lib/alloc.c
> > >>>create mode 100644 lib/alloc.h
> > >>>
> > >>>diff --git a/lib/alloc.c b/lib/alloc.c
> > >>>new file mode 100644
> > >>>index 0000000000000..868664b4dcaa3
> > >>>--- /dev/null
> > >>>+++ b/lib/alloc.c
> > >>>@@ -0,0 +1,2 @@
> > >>>+#include "alloc.h"
> > >>>+struct alloc_ops alloc_ops;
> > >>>diff --git a/lib/alloc.h b/lib/alloc.h
> > >>>new file mode 100644
> > >>>index 0000000000000..c8cd61b387a9a
> > >>>--- /dev/null
> > >>>+++ b/lib/alloc.h
> > >>>@@ -0,0 +1,31 @@
> > >>>+#ifndef _ALLOC_H_
> > >>>+#define _ALLOC_H_
> > >>>+#include "libcflat.h"
> > >>>+
> > >>>+struct alloc_ops {
> > >>>+ void *(*alloc)(size_t size);
> > >>>+ void *(*alloc_aligned)(size_t size, size_t align);
> > >>>+ void (*free)(const void *addr);
> > >>>+};
> > >>>+
> > >>>+extern struct alloc_ops alloc_ops;
> > >>>+
> > >>>+static inline void *alloc(size_t size)
> > >>>+{
> > >>>+ assert(alloc_ops.alloc);
> > >>>+ return alloc_ops.alloc(size);
> > >>>+}
> > >>>+
> > >>>+static inline void *alloc_aligned(size_t size, size_t align)
> > >>>+{
> > >>>+ assert(alloc_ops.alloc_aligned);
> > >>>+ return alloc_ops.alloc_aligned(size, align);
> > >>>+}
> > >>>+
> > >>>+static inline void free(const void *addr)
> > >>>+{
> > >>>+ assert(alloc_ops.free);
> > >>>+ alloc_ops.free(addr);
> > >>>+}
> > >>>+
> > >>>+#endif
> > >>>
> > >>
> > >>Why do you need the wrappers?
> > >
> > >A unit test may want to change the allocator after setting up paging.
> >
> > Ok, having actually read the code a bit more, it looks like (correct me if
> > I'm wrong) you really need the early allocator only for the physical
> > addresses of the vring. You are using it for the virtqueue structs too,
> > that is not strictly necessary but I won't complain if you leave it that
> > way.
> >
> > All you need is a pointer to the bottom and top of the free area, then you
> > can do a very simple allocator like sbrk.
> >
> > So the API can look like this:
> >
> > void phys_alloc_init(uintptr_t base, uintptr_t top);
> > uintptr_t phys_alloc(size_t size);
> > uintptr_t phys_zalloc(size_t size);
> > uintptr_t phys_alloc_aligned(size_t size, size_t align);
> > uintptr_t phys_zalloc_aligned(size_t size, size_t align);
> >
> > If you keep the code that allocates the virtqueue, you can just add a
> > phys_to_virt() on the result.
> >
> > In order to keep the debugging code, just have a
> >
> > struct phys_alloc_region {
> > void *ptr;
> > size_t size;
> > };
> >
> > struct phys_alloc_region *first_region;
> >
> > and place these structs at the end of the region passed to phys_alloc_init.
> > That is, phys_alloc will allocate SIZE bytes from the BASE, and a single
> > struct phys_alloc_region from the TOP. The FIRST_REGION is initialized with
> > the same value as TOP in phys_alloc_init, and remains fixed there; the last
> > region is represented by TOP. To dump all regions, just walk the array from
> > first_region down to top.
> >
> > When paging is enabled, it can itself allocate memory using phys_alloc for
> > page tables and the like.
> >
> > >If memregions look useful outside of arm, then I can certainly move
> > >them and early_[m]alloc, etc. to common code.
> >
> > Yeah, they can live in lib/. Only the call to phys_alloc_init needs to be
> > in lib/arm.
> >
>
> Yes, you are correct, and I like your design and API better, but with one
> change. We should introduce phys_addr_t=u64 to common code as well, and then
> use that instead of uintptr_t/size_t. I'll do this for v7.
>
I decided to merge the two designs (memregions and your proposal). I
took your API (except for the phys_addr_t change I already mentioned), but
kept the explicit array for the allocation logging. Keeping the array (which
should be allocated in low memory) ensures we can always walk it. Using TOP
as you suggest may not work in all cases. I set the array size to 256,
which should be [way] more than enough, so we don't really need a
"limitless" log anyway.
drew
next prev parent reply other threads:[~2014-07-15 17:16 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-11 8:19 [PATCH v6 00/17] kvm-unit-tests/arm: initial drop Andrew Jones
2014-07-11 8:19 ` [PATCH v6 02/17] libfdt: get libfdt to build Andrew Jones
2014-07-11 8:19 ` [PATCH v6 03/17] add support for Linux device trees Andrew Jones
2014-07-11 8:19 ` [PATCH v6 04/17] libcflat: add abort() and assert() Andrew Jones
2014-07-11 8:19 ` [PATCH v6 05/17] Introduce asm-generic/*.h files Andrew Jones
2014-07-11 8:19 ` [PATCH v6 06/17] Introduce alloc_ops Andrew Jones
2014-07-11 8:40 ` Paolo Bonzini
2014-07-11 8:55 ` Andrew Jones
2014-07-11 9:41 ` Paolo Bonzini
2014-07-11 10:07 ` Andrew Jones
2014-07-11 10:12 ` Paolo Bonzini
2014-07-15 17:16 ` Andrew Jones [this message]
2014-07-11 8:19 ` [PATCH v6 07/17] add minimal virtio support for devtree virtio-mmio Andrew Jones
2014-07-11 8:32 ` Paolo Bonzini
2014-07-11 9:08 ` Andrew Jones
2014-07-11 9:27 ` Paolo Bonzini
2014-07-11 9:36 ` Andrew Jones
2014-07-15 17:22 ` Andrew Jones
2014-07-11 8:19 ` [PATCH v6 08/17] lib: add asm/page.h and virt_to_phys/phys_to_virt Andrew Jones
2014-07-11 8:19 ` [PATCH v6 09/17] virtio: add minimal support for virtqueues Andrew Jones
2014-07-11 9:23 ` Paolo Bonzini
2014-07-11 9:32 ` Paolo Bonzini
2014-07-11 9:52 ` Andrew Jones
2014-07-11 8:19 ` [PATCH v6 10/17] Introduce chr-testdev Andrew Jones
2014-07-11 8:19 ` [PATCH v6 11/17] libcflat: clean up libcflat.h Andrew Jones
2014-07-11 8:19 ` [PATCH v6 12/17] arm: initial drop Andrew Jones
2014-07-11 8:19 ` [PATCH v6 13/17] arm: Add spinlock implementation Andrew Jones
2014-07-11 8:19 ` [PATCH v6 14/17] arm: Add IO accessors to avoid register-writeback Andrew Jones
2014-07-11 8:19 ` [PATCH v6 15/17] arm: Add arch-specific asm/page.h and __va/__pa Andrew Jones
2014-07-11 8:19 ` [PATCH v6 16/17] arm: add useful headers from the Linux kernel Andrew Jones
2014-07-11 8:19 ` [PATCH v6 17/17] arm: vectors support Andrew Jones
2014-07-11 8:47 ` [PATCH v6 00/17] kvm-unit-tests/arm: initial drop Paolo Bonzini
2014-07-15 17:24 ` Andrew Jones
2014-07-15 20: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=20140715171647.GA15967@hawk.usersys.redhat.com \
--to=drjones@redhat.com \
--cc=christoffer.dall@linaro.org \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.cs.columbia.edu \
--cc=pbonzini@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