From: Andrew Jones Subject: [kvm-unit-tests PATCH 1/4] lib/alloc: improve robustness Date: Mon, 31 Oct 2016 19:51:43 +0100 Message-ID: <1477939906-28802-2-git-send-email-drjones@redhat.com> References: <1477939906-28802-1-git-send-email-drjones@redhat.com> Cc: pbonzini@redhat.com, lvivier@redhat.com, thuth@redhat.com To: kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:59888 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S945372AbcJaSvv (ORCPT ); Mon, 31 Oct 2016 14:51:51 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AA5D13B714 for ; Mon, 31 Oct 2016 18:51:51 +0000 (UTC) In-Reply-To: <1477939906-28802-1-git-send-email-drjones@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: Add some asserts to make sure phys_alloc_init is called only once and before any other alloc calls. Signed-off-by: Andrew Jones --- lib/alloc.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/alloc.c b/lib/alloc.c index e1d7b8a380ff..a83c8c5db850 100644 --- a/lib/alloc.c +++ b/lib/alloc.c @@ -42,12 +42,13 @@ void phys_alloc_show(void) void phys_alloc_init(phys_addr_t base_addr, phys_addr_t size) { - spin_lock(&lock); + /* we should only be called once by one processor */ + assert(!top); + 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) @@ -63,14 +64,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(base < top_safe); - if (safe && sizeof(long) == 4) - top_safe = MIN(top_safe, 1ULL << 32); + spin_lock(&lock); align = MAX(align, align_min); -- 2.7.4