From patchwork Wed Apr 11 07:21:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jia He X-Patchwork-Id: 10334947 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 4B4256053C for ; Wed, 11 Apr 2018 07:25:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3044028716 for ; Wed, 11 Apr 2018 07:25:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 244CA28744; Wed, 11 Apr 2018 07:25:29 +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=-2.9 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, DKIM_VALID, FREEMAIL_FROM, MAILING_LIST_MULTI autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id AA0C028716 for ; Wed, 11 Apr 2018 07:25:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=NhKESICNPQCNkvNtbcfq766gKKVpI5RW4ug8M8obo68=; b=lidIO2hFsA01U7cxg7RDfg9dqg VSIscmDamyaLGpspFFed5PeYsr5XKlzlw5S9hHe94kuOKKVrQ1fyzU4U1tg9Ov+KiUeC89h/LejA4 Fzy4c75r0VwzMBil1PWiGsCuqTDa7MW9XJwcRFx0lsOp8TU7Nc6MnJ40IkOxsTaGIf0jPewp/Hsg+ 1k/7mPKB8ivxRZpCD9niEyhpftdNGJSWGvmOMst6zhuTbPJL7JDdD1kVj/HNE8O25av/16qyVnSlN 57Z0kJxETByLzZ3QZ6qnkJfmRkcRAwdFjeeP75IrccmK75sprLg8EHXK+S3JqdL8ibgnGdve4YQWk OUP/rt1w==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1f6A88-0004Iq-WF; Wed, 11 Apr 2018 07:25:21 +0000 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1f6A6e-0002Am-KG for linux-arm-kernel@lists.infradead.org; Wed, 11 Apr 2018 07:24:01 +0000 Received: by mail-pg0-x244.google.com with SMTP id b9so370294pgf.6 for ; Wed, 11 Apr 2018 00:23:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=7woLN4AyZgMSpYIZf4ZwnIUfJApqj+oZJtk6oxVLENs=; b=boF/EtWIX4GtlScdrBjnUR/PKKvtp4rXA4/WkufSKEPQ6yiUWmsj9S03uVmE+AjWbP V71rt7FaXX2XtyUQLI1CS0k0Y1+0IDXCjMHtFp5Eel0AXMbWIGNWc31u+G1cNARF6ZnG bfx8raRDdE7NxQhmWtSR8N894e9Ac9HYyaxIwP9IZVlIskXnHzMWNWoA0S1g10tVQWwu iQiaL9BvRyOkPhkEomneDS6f1CBeJH7iXlaDhdRScs0G04KbgWFGRI2xcqKjYwZL1msl JRPaTiN4e15HbLEWWIJsERMe09r0PQmJn+wvKbYyXUIsdBt3xcoydZRJMLEJt+50uIVH TtlQ== 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=7woLN4AyZgMSpYIZf4ZwnIUfJApqj+oZJtk6oxVLENs=; b=MxATgw+rv6G0N2EkdgI3pGFSUABON3X1kr23U0K5BGNYLWEZc6nYGoNtd2kgVCzaae 0gi5IND8Hp4Jysma9imVKeWJxmyLn22Iwq8EHhd+XvtNsSqCnb2mDc9o51ne+6ZBVFxU 4Q8dC5Ip6YkrncjAYjzsDeVrU0d/OsA0WtrySKHdFNkp2N7BPeMVbt5cmIyWKVE5P4Vu xDjKMYs0rZLSsQPc5XSw1v9vHIEGW5U9HzYqlD3KKNMh+DAtm1TG6NYpBQC7lNxWn8F5 YOdZMXkliDAEUX8HqFo8W8A05Y435fwl7v5u6eFG/SATMxrVWka3T4J4ktfBS73ahTeC iTPA== X-Gm-Message-State: ALQs6tC4dszGmz1yX6pO/oB3wtYSso0H0sLTdQKqv93tSeBwCIda3LWX WoEshEiXyBkbwSQUJOUfSV0= X-Google-Smtp-Source: AIpwx4/OGJpQ9LXNiBfTKTRT33J8W7Km80zBAFBF2sV3GYfTtM7MnYhKTlUpi1vTFhOsszM8kOP3DQ== X-Received: by 10.101.68.129 with SMTP id l1mr2573784pgq.58.1523431417250; Wed, 11 Apr 2018 00:23:37 -0700 (PDT) Received: from localhost.localdomain ([38.106.11.25]) by smtp.gmail.com with ESMTPSA id o90sm1561898pfj.102.2018.04.11.00.23.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 11 Apr 2018 00:23:36 -0700 (PDT) From: Jia He To: Russell King , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Andrew Morton , Michal Hocko Subject: [PATCH v8 3/6] arm: arm64: page_alloc: reduce unnecessary binary search in memblock_next_valid_pfn() Date: Wed, 11 Apr 2018 00:21:54 -0700 Message-Id: <1523431317-30612-4-git-send-email-hejianet@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1523431317-30612-1-git-send-email-hejianet@gmail.com> References: <1523431317-30612-1-git-send-email-hejianet@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180411_002348_714376_196944EC X-CRM114-Status: GOOD ( 18.52 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Gioh Kim , Wei Yang , linux-mm@kvack.org, Eugeniu Rosca , Petr Tesarik , Nikolay Borisov , Jia He , Pavel Tatashin , Daniel Jordan , AKASHI Takahiro , Vlastimil Babka , Andrey Ryabinin , Laura Abbott , Daniel Vacek , Mel Gorman , Vladimir Murzin , Kees Cook , Philip Derrin , YASUAKI ISHIMATSU , Jia He , Kemi Wang , linux-arm-kernel@lists.infradead.org, Steve Capper , linux-kernel@vger.kernel.org, James Morse , Johannes Weiner MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But there is still some room for improvement. E.g. if pfn and pfn+1 are in the same memblock region, we can simply pfn++ instead of doing the binary search in memblock_next_valid_pfn. Furthermore, if the pfn is in a *gap* of two memory region, skip to next region directly if possible. Signed-off-by: Jia He --- include/linux/early_pfn.h | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/include/linux/early_pfn.h b/include/linux/early_pfn.h index 1b001c7..f9e40c3 100644 --- a/include/linux/early_pfn.h +++ b/include/linux/early_pfn.h @@ -3,31 +3,52 @@ #ifndef __EARLY_PFN_H #define __EARLY_PFN_H #ifdef CONFIG_HAVE_MEMBLOCK_PFN_VALID +static int early_region_idx __init_memblock = -1; ulong __init_memblock memblock_next_valid_pfn(ulong pfn) { struct memblock_type *type = &memblock.memory; - unsigned int right = type->cnt; - unsigned int mid, left = 0; + struct memblock_region *regions = type->regions; + uint right = type->cnt; + uint mid, left = 0; + ulong start_pfn, end_pfn, next_start_pfn; phys_addr_t addr = PFN_PHYS(++pfn); + /* fast path, return pfn+1 if next pfn is in the same region */ + if (early_region_idx != -1) { + start_pfn = PFN_DOWN(regions[early_region_idx].base); + end_pfn = PFN_DOWN(regions[early_region_idx].base + + regions[early_region_idx].size); + + if (pfn >= start_pfn && pfn < end_pfn) + return pfn; + + early_region_idx++; + next_start_pfn = PFN_DOWN(regions[early_region_idx].base); + + if (pfn >= end_pfn && pfn <= next_start_pfn) + return next_start_pfn; + } + + /* slow path, do the binary searching */ do { mid = (right + left) / 2; - if (addr < type->regions[mid].base) + if (addr < regions[mid].base) right = mid; - else if (addr >= (type->regions[mid].base + - type->regions[mid].size)) + else if (addr >= (regions[mid].base + regions[mid].size)) left = mid + 1; else { - /* addr is within the region, so pfn is valid */ + early_region_idx = mid; return pfn; } } while (left < right); if (right == type->cnt) return -1UL; - else - return PHYS_PFN(type->regions[right].base); + + early_region_idx = right; + + return PHYS_PFN(regions[early_region_idx].base); } EXPORT_SYMBOL(memblock_next_valid_pfn); #endif /*CONFIG_HAVE_MEMBLOCK_PFN_VALID*/