From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrew Jones Subject: [PATCH kvm-unit-tests v2 09/12] page_alloc: add yet another memalign Date: Wed, 17 Jan 2018 11:40:02 +0100 Message-ID: <20180117104005.29211-10-drjones@redhat.com> References: <20180117104005.29211-1-drjones@redhat.com> Cc: pbonzini@redhat.com, rkrcmar@redhat.com, cdall@linaro.org, david@redhat.com, lvivier@redhat.com, thuth@redhat.com To: kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:43430 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752267AbeAQKkm (ORCPT ); Wed, 17 Jan 2018 05:40:42 -0500 In-Reply-To: <20180117104005.29211-1-drjones@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: If we want both early alloc ops and alloc_page(), then it's best to just give all the memory to page_alloc and then base the early alloc ops on that. Signed-off-by: Andrew Jones --- lib/alloc_page.c | 31 +++++++++++++++++++++++++++++++ lib/alloc_page.h | 1 + 2 files changed, 32 insertions(+) diff --git a/lib/alloc_page.c b/lib/alloc_page.c index ca11496829a0..361b584cab2a 100644 --- a/lib/alloc_page.c +++ b/lib/alloc_page.c @@ -5,7 +5,9 @@ * with page granularity. */ #include "libcflat.h" +#include "alloc.h" #include "alloc_phys.h" +#include "bitops.h" #include #include #include @@ -134,3 +136,32 @@ void free_page(void *page) freelist = page; spin_unlock(&lock); } + +static void *page_memalign(size_t alignment, size_t size) +{ + unsigned long n = ALIGN(size, PAGE_SIZE) >> PAGE_SHIFT; + unsigned long order; + + if (!size) + return NULL; + + order = is_power_of_2(n) ? fls(n) : fls(n) + 1; + + return alloc_pages(order); +} + +static void page_free(void *mem, size_t size) +{ + free_pages(mem, size); +} + +static struct alloc_ops page_alloc_ops = { + .memalign = page_memalign, + .free = page_free, + .align_min = PAGE_SIZE, +}; + +void page_alloc_ops_enable(void) +{ + alloc_ops = &page_alloc_ops; +} diff --git a/lib/alloc_page.h b/lib/alloc_page.h index 1c2b3ec9add6..51d48414a47e 100644 --- a/lib/alloc_page.h +++ b/lib/alloc_page.h @@ -9,6 +9,7 @@ #define ALLOC_PAGE_H 1 bool page_alloc_initialized(void); +void page_alloc_ops_enable(void); void *alloc_page(); void *alloc_pages(unsigned long order); void free_page(void *page); -- 2.13.6