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 2/6] lib/alloc: improve init
Date: Wed, 2 Nov 2016 21:52:42 +0100 [thread overview]
Message-ID: <1478119966-13252-3-git-send-email-drjones@redhat.com> (raw)
In-Reply-To: <1478119966-13252-1-git-send-email-drjones@redhat.com>
Add some asserts to make sure phys_alloc_init is called only once
and before any other alloc calls. Also require that an alignment
is set up just once and *before* init, or never.
Signed-off-by: Andrew Jones <drjones@redhat.com>
---
lib/alloc.c | 26 ++++++++++++++++++--------
lib/alloc.h | 7 +++++--
lib/arm/setup.c | 2 +-
lib/powerpc/setup.c | 2 +-
4 files changed, 25 insertions(+), 12 deletions(-)
diff --git a/lib/alloc.c b/lib/alloc.c
index e1d7b8a380ff..5b3ef5d00f8a 100644
--- a/lib/alloc.c
+++ b/lib/alloc.c
@@ -43,18 +43,28 @@ void phys_alloc_show(void)
void phys_alloc_init(phys_addr_t base_addr, phys_addr_t size)
{
spin_lock(&lock);
+
+ if (!align_min)
+ align_min = DEFAULT_MINIMUM_ALIGNMENT;
+
+ assert(!top);
+ assert(!(base_addr & (align_min - 1)));
+ assert(size);
+
base = base_addr;
top = base + size;
- align_min = DEFAULT_MINIMUM_ALIGNMENT;
- nr_regions = 0;
+
spin_unlock(&lock);
}
void phys_alloc_set_minimum_alignment(phys_addr_t align)
{
- assert(align && !(align & (align - 1)));
spin_lock(&lock);
+
+ assert(!align_min);
+ assert(align && !(align & (align - 1)));
align_min = align;
+
spin_unlock(&lock);
}
@@ -63,14 +73,14 @@ static phys_addr_t phys_alloc_aligned_safe(phys_addr_t size,
{
static bool warned = false;
phys_addr_t addr, size_orig = size;
- u64 top_safe;
+ u64 top_safe = top;
- spin_lock(&lock);
+ if (safe && sizeof(long) == 4)
+ top_safe = MIN(top, 1ULL << 32);
- top_safe = top;
+ assert(top_safe && base < top_safe);
- if (safe && sizeof(long) == 4)
- top_safe = MIN(top_safe, 1ULL << 32);
+ spin_lock(&lock);
align = MAX(align, align_min);
diff --git a/lib/alloc.h b/lib/alloc.h
index c12bd15f7afc..bd3c4e8ff3f6 100644
--- a/lib/alloc.h
+++ b/lib/alloc.h
@@ -73,13 +73,16 @@ static inline void *memalign(size_t alignment, size_t size)
/*
* phys_alloc_init creates the initial free memory region of size @size
- * at @base. The minimum alignment is set to DEFAULT_MINIMUM_ALIGNMENT.
+ * at @base. If a minimum alignment has not been set then
+ * DEFAULT_MINIMUM_ALIGNMENT is used. phys_alloc_init may only be called
+ * once.
*/
extern void phys_alloc_init(phys_addr_t base, phys_addr_t size);
/*
* phys_alloc_set_minimum_alignment sets the minimum alignment to
- * @align.
+ * @align. phys_alloc_set_minimum_alignment must be called before
+ * phys_alloc_init and only once.
*/
extern void phys_alloc_set_minimum_alignment(phys_addr_t align);
diff --git a/lib/arm/setup.c b/lib/arm/setup.c
index 7e7b39f11dde..d02d1a731d20 100644
--- a/lib/arm/setup.c
+++ b/lib/arm/setup.c
@@ -94,8 +94,8 @@ static void mem_init(phys_addr_t freemem_start)
__phys_offset = mem.start; /* PHYS_OFFSET */
__phys_end = mem.end; /* PHYS_END */
- phys_alloc_init(freemem_start, primary.end - freemem_start);
phys_alloc_set_minimum_alignment(SMP_CACHE_BYTES);
+ phys_alloc_init(freemem_start, primary.end - freemem_start);
mmu_enable_idmap();
}
diff --git a/lib/powerpc/setup.c b/lib/powerpc/setup.c
index 9153f7bebc6a..b390dadaebbe 100644
--- a/lib/powerpc/setup.c
+++ b/lib/powerpc/setup.c
@@ -146,9 +146,9 @@ static void mem_init(phys_addr_t freemem_start)
__physical_start = mem.start; /* PHYSICAL_START */
__physical_end = mem.end; /* PHYSICAL_END */
- phys_alloc_init(freemem_start, primary.end - freemem_start);
phys_alloc_set_minimum_alignment(__icache_bytes > __dcache_bytes
? __icache_bytes : __dcache_bytes);
+ phys_alloc_init(freemem_start, primary.end - freemem_start);
}
void setup(const void *fdt)
--
2.7.4
next prev parent 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 ` [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 ` Andrew Jones [this message]
2016-11-03 10:57 ` [kvm-unit-tests PATCH v2 2/6] lib/alloc: improve init 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-3-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).