Received: by 10.213.65.68 with SMTP id h4csp88508imn; Fri, 30 Mar 2018 01:18:27 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+FHm+VAQbARyiE5BK2E9dUpJTXy8BW6GedsfaROM5JZK8iYsCxaF+QthSxodTn490j3tc4 X-Received: by 10.98.157.6 with SMTP id i6mr9170494pfd.52.1522397907011; Fri, 30 Mar 2018 01:18:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522397906; cv=none; d=google.com; s=arc-20160816; b=wKJ3KucNDtP1FPTYhNiwnW0MzIkj4ms/zgZhz38ExNSivjuV235vJJOydHmeeWZaKD ELOLPWyvutjlf58EScS+PaAHQ+UDNrUHJKuIV95BsBwPeL6q4D0kuJ6hF6XnWRWsgBGW IJJeqc37OOzI3i3d21E2jl68DDRMNtMQ5+sVa1NapCityU9Rurx2n1vfGhR7UM1J0maE BHFwgoRdu5uQuAm82fqHieJz8OGPH4ozZ+T/nO0EIozfr416ghLapFOCH1wtAceeV3uz gtfnjJXl0tAuNtIuRxvA9IkJHjiLF2cYS66EbBr2LL/Tze1qaKSVnPXugqHE/07PJkYw 4E8w== 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=47sVWvofKdNwuFJvbzgAe8kFJ6Tq+S03IUJifBHaXXU=; b=xIrKZC7b5P5oalYV5thQAqucytUbGBmK4xkJvI7UXm1WZG1JFSPXS5eQ/FsA+hx2MP ijpJUjr3lWx10HgTzATxRT4nymWfYsIA6JR3g5FuUMCBvspGFPKJcejaIiPDzfOPkkSb i4cLd4AMQQbOPaElk4wKDZBO9is86hhXJcPQ54hlIcLedP6sRy+rMRzq26MIPC2GTktC LxIGEm72L98LVv1FpZokcOMwFfEtk5d/la2mMo8HyLDhYosRUAHSMsBlosJO1qGRR2nq 7ehP60wr8oY3BLEgdzMjH7B8Oyu84LxRmzBQ0reuDMWHj0TvU9jSu2eAByqyUxVwbQ/H yPIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Yjzc+gRf; 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 l85si3839449pfb.231.2018.03.30.01.18.13; Fri, 30 Mar 2018 01:18:26 -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=Yjzc+gRf; 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 S1751271AbeC3IQy (ORCPT + 99 others); Fri, 30 Mar 2018 04:16:54 -0400 Received: from mail-pf0-f196.google.com ([209.85.192.196]:39608 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751249AbeC3IQt (ORCPT ); Fri, 30 Mar 2018 04:16:49 -0400 Received: by mail-pf0-f196.google.com with SMTP id c78so5018961pfj.6 for ; Fri, 30 Mar 2018 01:16:49 -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=47sVWvofKdNwuFJvbzgAe8kFJ6Tq+S03IUJifBHaXXU=; b=Yjzc+gRfQPmAX2H+vx3S8xAQhZ7sqpFzqYzBggK7sqV1XsZQ8G57pBsNGhyAZYfHHa LfOy/rUncF8KuWoJbgNtzCQFtu5pCHi8LHPjVdA+HUN15uzS1bysWo2TskC1ce2iMmlP mpS+/pZB6VoOHk7TqSRru9p35fMeHFEzrNsi4QpInGzIuwRcfPRXB//4VAoeKXRSAV1w lLF27kLHxuWXsuxS1LAo8WkwRNQ7NIwnK6IErHM2ywKLM7/++gdrFC4VAY+wAR4ZXyol SRhAfLd1kAWffCYI8SLesecnVi+jtUdLv9e5v5uNBg/fx4Ra+VAAQkFgyXQQnWMWvE9O QlGg== 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=47sVWvofKdNwuFJvbzgAe8kFJ6Tq+S03IUJifBHaXXU=; b=Lw5Wl+DgeuA6sIWrXvIaXGa0+qBeZ0XD7shg7SoQ62wAOWkDhC1bItDsuUAgqs6JD5 d3RlzQDHTHJDe+AqLHu8BGbT3cdvEmDo/ra+JYPokJ1Rt0Eizz82LLAYhh9w3/8zI/F0 fYas97F+AEn6qGVBGzoophz5qdnDQYyvyLZIK4xWRtnOo7/x9zOng0OvoeglyLjNlGm5 OgxNVpY/5+6lX8upIyNUF5s3zUFXRPef+53NkUirbMmbH2+1Ju2vxx+UxqVQ81WE+rWL vUD7zqlfNnHjyhyCTa53p5i9f3fGEXGuz1TbHzx2WK5iPLOSFiFW3dOeqS27AWVPwXO+ Ddhg== X-Gm-Message-State: AElRT7Hmuxpg2c7FkhJfA1ClJCBl2OCjy0OEOxUiZsJ4T5OTL01Xlazg Hh7xVKDHLcPmPfyfJHVIcBY= X-Received: by 10.101.98.153 with SMTP id f25mr5581955pgv.6.1522397809246; Fri, 30 Mar 2018 01:16:49 -0700 (PDT) Received: from localhost.localdomain ([38.106.11.25]) by smtp.gmail.com with ESMTPSA id l8sm4269837pff.126.2018.03.30.01.16.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 30 Mar 2018 01:16:48 -0700 (PDT) From: Jia He To: Russell King , Andrew Morton , Michal Hocko , Catalin Marinas , Mel Gorman , Will Deacon , Mark Rutland , "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 , Thomas Gleixner , Ingo Molnar , x86@kernel.org, Greg Kroah-Hartman , Kate Stewart , Philippe Ombredanne , Johannes Weiner , Kemi Wang , Petr Tesarik , YASUAKI ISHIMATSU , Andrey Ryabinin , Nikolay Borisov , richard.weiyang@gmail.com, Jia He , Jia He Subject: [PATCH v4 2/5] arm: arm64: page_alloc: reduce unnecessary binary search in memblock_next_valid_pfn() Date: Fri, 30 Mar 2018 01:15:52 -0700 Message-Id: <1522397755-33393-3-git-send-email-hejianet@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1522397755-33393-1-git-send-email-hejianet@gmail.com> References: <1522397755-33393-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 --- arch/arm/include/asm/page.h | 1 + arch/arm/mm/init.c | 31 ++++++++++++++++++++++++------- arch/arm64/include/asm/page.h | 1 + arch/arm64/mm/init.c | 31 ++++++++++++++++++++++++------- mm/page_alloc.c | 5 +++-- 5 files changed, 53 insertions(+), 16 deletions(-) diff --git a/arch/arm/include/asm/page.h b/arch/arm/include/asm/page.h index 4355f0e..7a0404f 100644 --- a/arch/arm/include/asm/page.h +++ b/arch/arm/include/asm/page.h @@ -157,6 +157,7 @@ extern void copy_page(void *to, const void *from); typedef struct page *pgtable_t; #ifdef CONFIG_HAVE_ARCH_PFN_VALID +extern int early_region_idx; extern int pfn_valid(unsigned long); #endif diff --git a/arch/arm/mm/init.c b/arch/arm/mm/init.c index 0fb85ca..7779804 100644 --- a/arch/arm/mm/init.c +++ b/arch/arm/mm/init.c @@ -193,6 +193,8 @@ static void __init zone_sizes_init(unsigned long min, unsigned long max_low, } #ifdef CONFIG_HAVE_ARCH_PFN_VALID +int early_region_idx __meminitdata = -1; + int pfn_valid(unsigned long pfn) { return memblock_is_map_memory(__pfn_to_phys(pfn)); @@ -200,31 +202,46 @@ int pfn_valid(unsigned long pfn) EXPORT_SYMBOL(pfn_valid); /* HAVE_MEMBLOCK is always enabled on arm */ -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; + struct memblock_region *regions = type->regions; 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 pfn+1 if next pfn is in the same region */ + if (*last_idx != -1) { + start_pfn = PFN_DOWN(regions[*last_idx].base); + end_pfn = PFN_DOWN(regions[*last_idx].base + + regions[*last_idx].size); + + if (pfn >= start_pfn && pfn < end_pfn) + return 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 */ + *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(regions[*last_idx].base); } EXPORT_SYMBOL(memblock_next_valid_pfn); #endif /*CONFIG_HAVE_ARCH_PFN_VALID*/ diff --git a/arch/arm64/include/asm/page.h b/arch/arm64/include/asm/page.h index 60d02c8..84b503a 100644 --- a/arch/arm64/include/asm/page.h +++ b/arch/arm64/include/asm/page.h @@ -38,6 +38,7 @@ extern void clear_page(void *to); typedef struct page *pgtable_t; #ifdef CONFIG_HAVE_ARCH_PFN_VALID +extern int early_region_idx; extern int pfn_valid(unsigned long); #endif diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index 13e43ff..cd9b473 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -285,6 +285,8 @@ static void __init zone_sizes_init(unsigned long min, unsigned long max) #endif /* CONFIG_NUMA */ #ifdef CONFIG_HAVE_ARCH_PFN_VALID +int early_region_idx __meminitdata = -1; + int pfn_valid(unsigned long pfn) { return memblock_is_map_memory(pfn << PAGE_SHIFT); @@ -292,31 +294,46 @@ int pfn_valid(unsigned long pfn) EXPORT_SYMBOL(pfn_valid); /* HAVE_MEMBLOCK is always enabled on arm64 */ -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; + struct memblock_region *regions = type->regions; 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 pfn+1 if next pfn is in the same region */ + if (*last_idx != -1) { + start_pfn = PFN_DOWN(regions[*last_idx].base); + end_pfn = PFN_DOWN(regions[*last_idx].base + + regions[*last_idx].size); + + if (pfn >= start_pfn && pfn < end_pfn) + return 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 */ + *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(regions[*last_idx].base); } EXPORT_SYMBOL(memblock_next_valid_pfn); #endif /*CONFIG_HAVE_ARCH_PFN_VALID*/ diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 8a92df7..f99b513 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -5453,8 +5453,9 @@ void __ref build_all_zonelists(pg_data_t *pgdat) * done. Non-atomic initialization, single-pass. */ #if (defined CONFIG_HAVE_MEMBLOCK) && (defined CONFIG_HAVE_ARCH_PFN_VALID) -extern unsigned long memblock_next_valid_pfn(unsigned long pfn); -#define skip_to_last_invalid_pfn(pfn) (memblock_next_valid_pfn(pfn) - 1) +extern unsigned long memblock_next_valid_pfn(unsigned long pfn, int *last_idx); +#define skip_to_last_invalid_pfn(pfn) \ + (memblock_next_valid_pfn(pfn, &early_region_idx) - 1) #endif #ifndef skip_to_last_invalid_pfn -- 2.7.4