From patchwork Fri Apr 21 00:49:56 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 9691543 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 B86B76038D for ; Fri, 21 Apr 2017 00:51:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AA95B2847B for ; Fri, 21 Apr 2017 00:51:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9F78C28480; Fri, 21 Apr 2017 00:51:03 +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.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM 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 5512B2847B for ; Fri, 21 Apr 2017 00:51:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1034299AbdDUAvA (ORCPT ); Thu, 20 Apr 2017 20:51:00 -0400 Received: from mail-io0-f169.google.com ([209.85.223.169]:33376 "EHLO mail-io0-f169.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1033953AbdDUAuh (ORCPT ); Thu, 20 Apr 2017 20:50:37 -0400 Received: by mail-io0-f169.google.com with SMTP id k87so95054525ioi.0 for ; Thu, 20 Apr 2017 17:50:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=KbSVWan4HbK4WkBVUZVFxcC/T4BqAAwv6+XDp9h0Hgk=; b=A+J0tvInKcTKoSdH+Aiog1fb/xyNj4INM9no+CJqFr0x833DnpwcZy7/qILMW363m+ xqfI/Ph3ZSQ4W+XuxKc7bSEmMc9CPzlZhPy3z/zEU3L+vW2uZS5QvxLXiyj6RS3bKN8J kXSAxrQBPYR2PWIF5KOJQZh8LUDS9rxR2YPYNY8Dunlb7A8ZqKxg7feO8eyz03NZo8TJ dOPRAEfa64XvZRmKxDjyrKEjGmWxWkhVs1P9vx+nkFKz39qLWlWni1wc9TM1H51xVc6P +NEOoyhUhuZFh7JxhSUVXgjaZIAcWvc1Adqo4UQ6sBCDneLE7nw8Yc5iLi1R3g6NIzYE nDnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=KbSVWan4HbK4WkBVUZVFxcC/T4BqAAwv6+XDp9h0Hgk=; b=YuSE6BFZ1DOCbThDEaHFm9Rhxah9Hu1SFo0upV24Si+FUsewK3nyt4/FbK1hjomdLq 59ipQMg9bYcPfwv4YT2bEWm7472ER+k+86YCqEVzzw8amSHO7clluKKsvcnEGF3LCre0 38ARbjvI7BsvXzAGBj8fuyEROo4HPlYnIXlNNZnrWQbilFN8LmwMusv6awi86OYXW0LM UkSfqrX9ONm0EKyCr3Nnm5GGBaVXD9/PenROKqg2Iml4MM3vIaZ37kkoa8wOarWjWuds NefiXdPgf/iSDCDalFP0+xebW0rfnVbJatYrPULcFuWCD0FtkGKa8IlJc4p7huZyadJN 9Ndg== X-Gm-Message-State: AN3rC/5jDbHG6CI8dy82++EKvmAfEKu96L1VwUbuG59BrPgWlr4sAAdp Ws2ArmcrpYBJceDPcJZfWQ== X-Received: by 10.98.29.86 with SMTP id d83mr10231496pfd.68.1492735831396; Thu, 20 Apr 2017 17:50:31 -0700 (PDT) Received: from dmatlack.sea.corp.google.com ([100.100.206.82]) by smtp.gmail.com with ESMTPSA id e13sm12466486pfb.30.2017.04.20.17.50.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 20 Apr 2017 17:50:30 -0700 (PDT) From: David Matlack To: kvm@vger.kernel.org Cc: Peter Feiner , David Matlack Subject: [kvm-unit-tests PATCH 24/32] lib: x86: multi-page allocator Date: Thu, 20 Apr 2017 17:49:56 -0700 Message-Id: <20170421005004.137260-25-dmatlack@google.com> X-Mailer: git-send-email 2.12.2.816.g2cccc81164-goog In-Reply-To: <20170421005004.137260-1-dmatlack@google.com> References: <20170421005004.137260-1-dmatlack@google.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Peter Feiner Need this for testing huge pages. Change-Id: Iecf4f916a3d3d24844957dcd4e81c8aa10b1cdda Signed-off-by: David Matlack --- lib/x86/vm.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/x86/vm.h | 1 + 2 files changed, 58 insertions(+) diff --git a/lib/x86/vm.c b/lib/x86/vm.c index 8bd0c88e141e..438ab9c70ffa 100644 --- a/lib/x86/vm.c +++ b/lib/x86/vm.c @@ -45,6 +45,63 @@ void *alloc_page() return p; } +/* + * Allocates (1 << order) physically contiguous and naturally aligned pages. + * Returns NULL if there's no memory left. + */ +void *alloc_pages(unsigned long order) +{ + /* Generic list traversal. */ + void *prev; + void *curr = NULL; + void *next = free; + + /* Looking for a run of length (1 << order). */ + unsigned long run = 0; + const unsigned long n = 1ul << order; + const unsigned long align_mask = (n << PAGE_SHIFT) - 1; + void *run_start = NULL; + void *run_prev = NULL; + unsigned long run_next_pa = 0; + unsigned long pa; + + assert(order < sizeof(unsigned long) * 8); + + for (;;) { + prev = curr; + curr = next; + next = curr ? *((void **) curr) : NULL; + + if (!curr) + return 0; + + pa = virt_to_phys(curr); + + if (run == 0) { + if (!(pa & align_mask)) { + run_start = curr; + run_prev = prev; + run_next_pa = pa + PAGE_SIZE; + run = 1; + } + } else if (pa == run_next_pa) { + run_next_pa += PAGE_SIZE; + run += 1; + } else { + run = 0; + } + + if (run == n) { + if (run_prev) + *((void **) run_prev) = next; + else + free = next; + return run_start; + } + } +} + + void free_page(void *page) { *(void **)page = free; diff --git a/lib/x86/vm.h b/lib/x86/vm.h index 6a4384f5a48d..64e4b566333f 100644 --- a/lib/x86/vm.h +++ b/lib/x86/vm.h @@ -22,6 +22,7 @@ unsigned long *install_pte(unsigned long *cr3, unsigned long *pt_page); void *alloc_page(); +void *alloc_pages(unsigned long order); void free_page(void *page); unsigned long *install_large_page(unsigned long *cr3,unsigned long phys,