Received: by 10.213.65.68 with SMTP id h4csp1056920imn; Wed, 21 Mar 2018 01:13:01 -0700 (PDT) X-Google-Smtp-Source: AG47ELvpKu0k6pSYkfkUIx36Fohh/Z65yQxdcm9nwPwdIBSq6G8GmJekagm6TPACEolyPIIlK7b+ X-Received: by 2002:a17:902:b10c:: with SMTP id q12-v6mr20249647plr.197.1521619981059; Wed, 21 Mar 2018 01:13:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521619981; cv=none; d=google.com; s=arc-20160816; b=QWAdmNHPIfKPnt6X8nrldxYKLa/sEG5BfW0W2je+3Kr3y0kjFJfomXg2G6kKqhq6kH p3IZo9qVSmDc5tzTFodPmZ2evUlY4mV2x4TwtciGq+YL6XiuuK8+ra0eIAiHJUdaWDF7 i82ZdkcCpXJVm0wl2KkHqftmVQY2FkjwsYtf2OOEHUlYsjPGPiaZp7E8B1O3zsKW1JH6 UzJjU1cbiawiZmfe5qMq/UAg8JRaoAYp0LkCyIPD8du22hdAwFfNBT+jVH8TItcDCR13 yZXhGNpB/KlBPSikLD3Iesqpmv6hdIJSzfwIUc74c/3QBB+S7TTwKavJxxPted8AOtCa oaCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=avmYRN6/vnItuyI7N23b9DdVPaH9+/pqg1OKGOcnB88=; b=zudzyOepA1CIwoqXQaEv7ox6pT4+q2+MStvSVvJdyQDqgkZRF5n1an6gWOKywk8oau /h7xWHnpAojS1mtBkUz6iJ5tY5tiBFruDlUd6lNYlkrL2KJC8zXLCcSIIHmfzampSPC0 yM/ii71L3/zrxqTPlWvX7do0Hu/kbtmyrolfJoc/27aNpJNcVRGqoW+ZkOGjXrF9+YrQ yeQ9kvJfaFzOF6F8HNNwpvioyrt4AdWDdHBkkpevck8uf6nHwl8588zkhmVV/+D6SC6A j05B+JnHkwhfnSUub7isTDnz0XjHeV5J1fM2iTLH6hn0MeP/m3SC7wSrjvbGpNZU2KQx 60yA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=oimjIJAo; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g4-v6si3346499plb.114.2018.03.21.01.12.47; Wed, 21 Mar 2018 01:13:01 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=oimjIJAo; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751881AbeCUIKf (ORCPT + 99 others); Wed, 21 Mar 2018 04:10:35 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:41123 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751736AbeCUIKb (ORCPT ); Wed, 21 Mar 2018 04:10:31 -0400 Received: by mail-pf0-f194.google.com with SMTP id f80so1703949pfa.8 for ; Wed, 21 Mar 2018 01:10:31 -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=avmYRN6/vnItuyI7N23b9DdVPaH9+/pqg1OKGOcnB88=; b=oimjIJAoKRMerXNNeRQR5clpfqdiMz+gkHBX7Nd8f3BxV5lGXHPkxGywgD+VcoCQ4n MiuUdMfHk1K6f4ab1xsUdk+8IZ57Xhp9u7xL1SyAVqsRw5HiIHVfOSR3ZevWSKdidM9N fnBWuMMFGrQ5ayfq2V4s9iY46ny7stmfin/XtyXNn7oc+cQHuv0ab2caeS+nV9aY7+bf jnE46Mgx86y2OEwOsrYUW95twoDYXMDyMHIHrctFB1JrG+lEltpdcQsce3yB27VYhHFv g+2f56/Ay2gwk50GUpEh3FdMM+Gxp/24YvpMSCBeamgwufX5zYX8JwKL6XtnpKLfwxUR yEwg== 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=avmYRN6/vnItuyI7N23b9DdVPaH9+/pqg1OKGOcnB88=; b=FWptZqUkYc7eoeOfZj8jQmHnY7jThSEU9CAzHXIohZ4gzFVI3tED+471ZzRxihU5JR mfw9NUM3X71AIYYDD9aQu2VRyyLWUJPhNXUYCBbSpkrVi7sRx6JHWaEZwCmEDB/uttiZ dBGM4khx5oFKpg2tUmPibTnRxtMhWtHC+xUx0M/4k32dPMLzJMXDbPgE3GaS18ZwNBK1 fM8kdRpfqt/cPZ7Ghi28cGBM3bmaCe8b83+xc2B599LjB9o3RRzYHN5+/wOiiF2yc4tJ XjinUIYR6EuiJbL6Yt8AR+zC5uIQzwTePvP59mCohBMKg3AN3Rnv1e72lPZlb4CRCDw0 /YVA== X-Gm-Message-State: AElRT7GQHTOBWOXbW/HyMcTiL/um1ME4X96oypQT+GlTp1xZa0Cd+rqX GSBlX5Fi7+ErBQ4nryzjZxE= X-Received: by 10.98.213.9 with SMTP id d9mr9561064pfg.234.1521619830643; Wed, 21 Mar 2018 01:10:30 -0700 (PDT) Received: from localhost.localdomain ([38.106.11.25]) by smtp.gmail.com with ESMTPSA id w10sm5950775pgr.57.2018.03.21.01.10.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 21 Mar 2018 01:10:30 -0700 (PDT) From: Jia He To: Andrew Morton , Michal Hocko , Catalin Marinas , Mel Gorman , Will Deacon , Mark Rutland , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" Cc: Pavel Tatashin , Daniel Jordan , AKASHI Takahiro , Gioh Kim , Steven Sistare , Daniel Vacek , Eugeniu Rosca , Vlastimil Babka , linux-kernel@vger.kernel.org, linux-mm@kvack.org, James Morse , Ard Biesheuvel , Steve Capper , x86@kernel.org, Greg Kroah-Hartman , Kate Stewart , Philippe Ombredanne , Johannes Weiner , Kemi Wang , Petr Tesarik , YASUAKI ISHIMATSU , Andrey Ryabinin , Nikolay Borisov , Jia He , Jia He Subject: [PATCH 1/4] mm: page_alloc: reduce unnecessary binary search in memblock_next_valid_pfn() Date: Wed, 21 Mar 2018 01:09:53 -0700 Message-Id: <1521619796-3846-2-git-send-email-hejianet@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1521619796-3846-1-git-send-email-hejianet@gmail.com> References: <1521619796-3846-1-git-send-email-hejianet@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Jia He --- include/linux/memblock.h | 3 +-- mm/memblock.c | 23 +++++++++++++++++++---- mm/page_alloc.c | 3 ++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/include/linux/memblock.h b/include/linux/memblock.h index b7aa3ff..9471db4 100644 --- a/include/linux/memblock.h +++ b/include/linux/memblock.h @@ -203,8 +203,7 @@ void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn, i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid)) #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ -unsigned long memblock_next_valid_pfn(unsigned long pfn); - +unsigned long memblock_next_valid_pfn(unsigned long pfn, int *last_idx); /** * for_each_free_mem_range - iterate through free memblock areas * @i: u64 used as loop variable diff --git a/mm/memblock.c b/mm/memblock.c index c87924d..a9e8da4 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -1133,13 +1133,26 @@ int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, } #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ -unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn) +unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn, + int *last_idx) { struct memblock_type *type = &memblock.memory; unsigned int right = type->cnt; unsigned int mid, left = 0; + unsigned long start_pfn, end_pfn; phys_addr_t addr = PFN_PHYS(++pfn); + /* fast path, return pfh+1 if next pfn is in the same region */ + if (*last_idx != -1) { + start_pfn = PFN_DOWN(type->regions[*last_idx].base); + end_pfn = PFN_DOWN(type->regions[*last_idx].base + + type->regions[*last_idx].size); + + if (pfn < end_pfn && pfn > start_pfn) + return pfn; + } + + /* slow path, do the binary searching */ do { mid = (right + left) / 2; @@ -1149,15 +1162,17 @@ unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn) type->regions[mid].size)) left = mid + 1; else { - /* addr is within the region, so pfn is valid */ + *last_idx = mid; return pfn; } } while (left < right); if (right == type->cnt) return -1UL; - else - return PHYS_PFN(type->regions[right].base); + + *last_idx = right; + + return PHYS_PFN(type->regions[*last_idx].base); } static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 3899209..f28c62c 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -5456,6 +5456,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone, unsigned long end_pfn = start_pfn + size; pg_data_t *pgdat = NODE_DATA(nid); unsigned long pfn; + int idx = -1; unsigned long nr_initialised = 0; struct page *page; #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP @@ -5487,7 +5488,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone, * end_pfn), such that we hit a valid pfn (or end_pfn) * on our next iteration of the loop. */ - pfn = memblock_next_valid_pfn(pfn) - 1; + pfn = memblock_next_valid_pfn(pfn, &idx) - 1; #endif continue; } -- 2.7.4