From patchwork Fri Apr 21 00:49:55 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 9691547 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 7476A6038D for ; Fri, 21 Apr 2017 00:51:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 66F622847B for ; Fri, 21 Apr 2017 00:51:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5BE6228480; Fri, 21 Apr 2017 00:51:10 +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 1AB4C2847B for ; Fri, 21 Apr 2017 00:51:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1034205AbdDUAvJ (ORCPT ); Thu, 20 Apr 2017 20:51:09 -0400 Received: from mail-io0-f182.google.com ([209.85.223.182]:33360 "EHLO mail-io0-f182.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1034159AbdDUAug (ORCPT ); Thu, 20 Apr 2017 20:50:36 -0400 Received: by mail-io0-f182.google.com with SMTP id k87so95053820ioi.0 for ; Thu, 20 Apr 2017 17:50:31 -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=StKDVRL5nlU9KI7sImO+shI3EFQMv/T4d+aEslHMxnc=; b=Qov89c3SOop5Hm95eG3QYMTroj9qmr0LZ+Ip8Eiqbl7kDJLmcKO4OfxwZX8OBw9Py8 V6FRAh8C6L5EFmfqpyBXqocFW+fkECuee5tnGJm9jxitZYxsS1NAfQJq/yyiT231gdLA mj89JUsGPke/MK8RpwaBONsmNb4FeQF25F9oWdCKTNefoz/rzyhbzpHOvCTZvr+DrHIx 71NWsTvsX4e7O7YHMqVcxemtET+G8Xr48xpSlJ4b/yMb8+3h43Wrm/PNLL3/HETmiXxH MMQfqsshdI0txZ3yX0DyuqNyB0XnfYOOSvImXSdCSTrfgE6QJV21NyE52e0ogbPOFQkC WruA== 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=StKDVRL5nlU9KI7sImO+shI3EFQMv/T4d+aEslHMxnc=; b=KEkVg+xrL4MhJI1ntAF0z9m0lBDD28ia7SzxJMLViUpR3UAgT9uyWLjWGtv8QUM49S KaF9e/uewDAeGT0fBD3l4nSnN/1N2z5HMeZxNlDPcYk1qV15oUNiito5jBH7LKNIf3z/ UNWBsZSYLVOeYyU8ZrLRDwrOV5jrXTrhZD81s6TYQ1P0cr1nB4GgistF+6g6wet/EYv7 3LMICPfEKtFMDeaRfzN1rYnO17wm3MRPoDK35ZIFKXsTuDLxW5bsMBE+DGEw7n8A7P5V KhWvnbpEXJ8LgP7+9mcB+h0n6YzSuOw0WpOxEbT0rp7Rl6K9ko2EQL8utP9IaMwLRjHM G7+w== X-Gm-Message-State: AN3rC/6UawqR2RY8EGZu7GVR+vaDUBtBvhU/ifAOcCzvm6d3CizrDkQ0 cSXW30a2AAsUz7ir X-Received: by 10.84.135.3 with SMTP id 3mr12884982pli.65.1492735830476; Thu, 20 Apr 2017 17:50:30 -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.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 20 Apr 2017 17:50:29 -0700 (PDT) From: David Matlack To: kvm@vger.kernel.org Cc: Peter Feiner , David Matlack Subject: [kvm-unit-tests PATCH 23/32] lib: x86: store free pages in ascending order Date: Thu, 20 Apr 2017 17:49:55 -0700 Message-Id: <20170421005004.137260-24-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 Makes allocation of contiguous physical pages simpler. Signed-off-by: Peter Feiner Signed-off-by: David Matlack --- lib/x86/vm.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/x86/vm.c b/lib/x86/vm.c index cda4c5f46da3..8bd0c88e141e 100644 --- a/lib/x86/vm.c +++ b/lib/x86/vm.c @@ -7,12 +7,29 @@ static void *vfree_top = 0; static void free_memory(void *mem, unsigned long size) { - while (size >= PAGE_SIZE) { - *(void **)mem = free; + void *end; + + assert_msg((unsigned long) mem % PAGE_SIZE == 0, + "mem not page aligned: %p", mem); + + assert_msg(size % PAGE_SIZE == 0, "size not page aligned: 0x%lx", size); + + assert_msg(size == 0 || mem + size > mem, + "mem + size overflow: %p + 0x%lx", mem, size); + + if (size == 0) { + free = NULL; + return; + } + free = mem; - mem += PAGE_SIZE; - size -= PAGE_SIZE; - } + end = mem + size; + while (mem + PAGE_SIZE != end) { + *(void **)mem = (mem + PAGE_SIZE); + mem += PAGE_SIZE; + } + + *(void **)mem = NULL; } void *alloc_page()