From patchwork Wed Nov 2 20:52:43 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Jones X-Patchwork-Id: 9409839 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 325A160585 for ; Wed, 2 Nov 2016 20:53:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 23F502A5B3 for ; Wed, 2 Nov 2016 20:53:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1864A2A5B5; Wed, 2 Nov 2016 20:53:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8C3282A5B1 for ; Wed, 2 Nov 2016 20:53:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757114AbcKBUw5 (ORCPT ); Wed, 2 Nov 2016 16:52:57 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42992 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757051AbcKBUwz (ORCPT ); Wed, 2 Nov 2016 16:52:55 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (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 C4FFE61BB7 for ; Wed, 2 Nov 2016 20:52:54 +0000 (UTC) Received: from kamzik.brq.redhat.com (kamzik.brq.redhat.com [10.34.1.143]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uA2KqmbX010366; Wed, 2 Nov 2016 16:52:53 -0400 From: Andrew Jones To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, lvivier@redhat.com, thuth@redhat.com Subject: [kvm-unit-tests PATCH v2 3/6] lib/alloc: prepare to extend alloc.c's purpose Date: Wed, 2 Nov 2016 21:52:43 +0100 Message-Id: <1478119966-13252-4-git-send-email-drjones@redhat.com> In-Reply-To: <1478119966-13252-1-git-send-email-drjones@redhat.com> References: <1478119966-13252-1-git-send-email-drjones@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 02 Nov 2016 20:52:54 +0000 (UTC) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Pick better names for phys_alloc state to tidy up a bit before we add new functions. (No functional change.) Signed-off-by: Andrew Jones Reviewed-by: Laurent Vivier --- lib/alloc.c | 97 ++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 51 insertions(+), 46 deletions(-) diff --git a/lib/alloc.c b/lib/alloc.c index 5b3ef5d00f8a..1d990a803825 100644 --- a/lib/alloc.c +++ b/lib/alloc.c @@ -10,62 +10,65 @@ #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) -#define PHYS_ALLOC_NR_REGIONS 256 +#define PHYS_ALLOC_NR_LOGS 256 struct phys_alloc_region { phys_addr_t base; phys_addr_t size; }; -static struct phys_alloc_region regions[PHYS_ALLOC_NR_REGIONS]; -static int nr_regions; +static struct phys_alloc_region phys_alloc_log[PHYS_ALLOC_NR_LOGS]; +static int phys_alloc_nr_logs; -static struct spinlock lock; -static phys_addr_t base, top, align_min; +static struct spinlock phys_alloc_lock; +static phys_addr_t phys_alloc_base; +static phys_addr_t phys_alloc_top; +static phys_addr_t phys_alloc_align_min; void phys_alloc_show(void) { int i; - spin_lock(&lock); + spin_lock(&phys_alloc_lock); printf("phys_alloc minimum alignment: 0x%" PRIx64 "\n", - (u64)align_min); - for (i = 0; i < nr_regions; ++i) + (u64)phys_alloc_align_min); + for (i = 0; i < phys_alloc_nr_logs; ++i) printf("%016" PRIx64 "-%016" PRIx64 " [%s]\n", - (u64)regions[i].base, - (u64)(regions[i].base + regions[i].size - 1), + (u64)phys_alloc_log[i].base, + (u64)(phys_alloc_log[i].base + + phys_alloc_log[i].size - 1), "USED"); printf("%016" PRIx64 "-%016" PRIx64 " [%s]\n", - (u64)base, (u64)(top - 1), "FREE"); - spin_unlock(&lock); + (u64)phys_alloc_base, (u64)(phys_alloc_top - 1), "FREE"); + spin_unlock(&phys_alloc_lock); } -void phys_alloc_init(phys_addr_t base_addr, phys_addr_t size) +void phys_alloc_init(phys_addr_t base, phys_addr_t size) { - spin_lock(&lock); + spin_lock(&phys_alloc_lock); - if (!align_min) - align_min = DEFAULT_MINIMUM_ALIGNMENT; + if (!phys_alloc_align_min) + phys_alloc_align_min = DEFAULT_MINIMUM_ALIGNMENT; - assert(!top); - assert(!(base_addr & (align_min - 1))); + assert(!phys_alloc_top); + assert(!(base & (phys_alloc_align_min - 1))); assert(size); - base = base_addr; - top = base + size; + phys_alloc_base = base; + phys_alloc_top = base + size; - spin_unlock(&lock); + spin_unlock(&phys_alloc_lock); } void phys_alloc_set_minimum_alignment(phys_addr_t align) { - spin_lock(&lock); + spin_lock(&phys_alloc_lock); - assert(!align_min); + assert(!phys_alloc_align_min); assert(align && !(align & (align - 1))); - align_min = align; + phys_alloc_align_min = align; - spin_unlock(&lock); + spin_unlock(&phys_alloc_lock); } static phys_addr_t phys_alloc_aligned_safe(phys_addr_t size, @@ -73,44 +76,45 @@ 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 = top; + u64 top_safe = phys_alloc_top; if (safe && sizeof(long) == 4) - top_safe = MIN(top, 1ULL << 32); + top_safe = MIN(phys_alloc_top, 1ULL << 32); - assert(top_safe && base < top_safe); + assert(top_safe && phys_alloc_base < top_safe); - spin_lock(&lock); + spin_lock(&phys_alloc_lock); - align = MAX(align, align_min); + align = MAX(align, phys_alloc_align_min); - addr = ALIGN(base, align); - size += addr - base; + addr = ALIGN(phys_alloc_base, align); + size += addr - phys_alloc_base; - if ((top_safe - base) < size) { + if ((top_safe - phys_alloc_base) < size) { printf("phys_alloc: requested=0x%" PRIx64 " (align=0x%" PRIx64 "), " "need=0x%" PRIx64 ", but free=0x%" PRIx64 ". " "top=0x%" PRIx64 ", top_safe=0x%" PRIx64 "\n", - (u64)size_orig, (u64)align, (u64)size, top_safe - base, - (u64)top, top_safe); - spin_unlock(&lock); + (u64)size_orig, (u64)align, (u64)size, + top_safe - phys_alloc_base, (u64)phys_alloc_top, + top_safe); + spin_unlock(&phys_alloc_lock); return INVALID_PHYS_ADDR; } - base += size; + phys_alloc_base += size; - if (nr_regions < PHYS_ALLOC_NR_REGIONS) { - regions[nr_regions].base = addr; - regions[nr_regions].size = size_orig; - ++nr_regions; + if (phys_alloc_nr_logs < PHYS_ALLOC_NR_LOGS) { + phys_alloc_log[phys_alloc_nr_logs].base = addr; + phys_alloc_log[phys_alloc_nr_logs].size = size_orig; + ++phys_alloc_nr_logs; } else if (!warned) { printf("WARNING: phys_alloc: No free log entries, " "can no longer log allocations...\n"); warned = true; } - spin_unlock(&lock); + spin_unlock(&phys_alloc_lock); return addr; } @@ -138,17 +142,18 @@ phys_addr_t phys_zalloc_aligned(phys_addr_t size, phys_addr_t align) phys_addr_t phys_alloc(phys_addr_t size) { - return phys_alloc_aligned(size, align_min); + return phys_alloc_aligned(size, phys_alloc_align_min); } phys_addr_t phys_zalloc(phys_addr_t size) { - return phys_zalloc_aligned(size, align_min); + return phys_zalloc_aligned(size, phys_alloc_align_min); } static void *early_malloc(size_t size) { - phys_addr_t addr = phys_alloc_aligned_safe(size, align_min, true); + phys_addr_t addr = phys_alloc_aligned_safe(size, + phys_alloc_align_min, true); if (addr == INVALID_PHYS_ADDR) return NULL; @@ -158,7 +163,7 @@ static void *early_malloc(size_t size) static void *early_calloc(size_t nmemb, size_t size) { phys_addr_t addr = phys_zalloc_aligned_safe(nmemb * size, - align_min, true); + phys_alloc_align_min, true); if (addr == INVALID_PHYS_ADDR) return NULL;