Received: by 10.213.65.68 with SMTP id h4csp2113486imn; Mon, 2 Apr 2018 01:03:56 -0700 (PDT) X-Google-Smtp-Source: AIpwx49wINSP8dY2sn/WSiYu1Hd25rkIU+Oyxu54zgCWNDIViPiavThBBJZ4NIS4HKS93upFY+yU X-Received: by 2002:a17:902:43e4:: with SMTP id j91-v6mr8833559pld.118.1522656236117; Mon, 02 Apr 2018 01:03:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522656236; cv=none; d=google.com; s=arc-20160816; b=JyffKgzyz5RCgQaYPKZSr11o0dtVmHeYS+dpBHHMM8sz4kdu81YqZpc1sj6sPGsYpP iYFfKWBP5sAvyONdCUNJkx8wnUdxp+v/YOI1aBdlgcJQN5iq2P4hf0dtVOndFNRrgNeO GtPv+6GO+byUapfcnVNoyU87yY9GCS7doa5LB9pkJbXVF/b2WL9prJ91mk9fhj6fJncv jpR+WIsE7EJjTktvmFhQXnwcUeXdgQ4qXWCeRyXveemUItHD4J+rn1eWLdBQ+oUdpra0 eUTW1i2gE0jWBaBIV1DAE1Qj6t7omDaZsK8Y6zODV+ldt1SsJlwcx1qE6exuWgONeI9l 3qiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:arc-authentication-results; bh=+hzhkwlqQPbISziV/ryF6NUP1cT5gR4Kp3UshIB7L0c=; b=eEL83VblfQNLAZpwEk0wlweJ+9IUG8gclG43LtcX7S6VStxnQJ6nD+u43q7yttvGZ2 vyNvmAyMNeeZLwUCxTxKzmw1JIWb+bd1wIZeVeYY1h/tJFDHsH4e1lC/xIzArHno4rND L+ei9cf01gvg1fKDPNGizg6T8ajyjEwAv1eshmxouQdCDycrCAxNCW1DNr+2c4bkmyo7 FBrwGHztJBARsfmQ5rA5d2v7vMbk15uUN2dMr8VYS7FhqIj5lwbx4PlK+Sa/Qq3j+FTw PIoa9ZCh8Y09c2uJ6YG1lsZy+luAXLDpFwnzXBZFLq5idRGo+cIM1SHnw1B+lQQovtHQ TPyg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f59-v6si14706554plf.38.2018.04.02.01.03.42; Mon, 02 Apr 2018 01:03:56 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754405AbeDBIB1 (ORCPT + 99 others); Mon, 2 Apr 2018 04:01:27 -0400 Received: from mail-ot0-f194.google.com ([74.125.82.194]:41674 "EHLO mail-ot0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754308AbeDBIB0 (ORCPT ); Mon, 2 Apr 2018 04:01:26 -0400 Received: by mail-ot0-f194.google.com with SMTP id i28-v6so14871729otf.8 for ; Mon, 02 Apr 2018 01:01:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=+hzhkwlqQPbISziV/ryF6NUP1cT5gR4Kp3UshIB7L0c=; b=eJJfuQ0hfjXBUTo3zYsMby19OdR94pKBijIc2Hv9Y+80ED4cGBYzDR62VcCVod5rKW oZF6jWGKTJl/+moSsmXV+g4NOXQE81T8LX8Ji77QZ36wVJTVTRrKA+mo2tdXlczDh6dp xXQtc6avHJsswjsiZkc5ecMXYQc68zCKpX2ipJmGXbMFtCJFReoAeRz2ogHsUojfB0Xa cKSqNEpzf+OVMHf4Y4lCX4dn/REDGyNsbC0Ams0jYLWpBsCZPgD+bTwMrqSscOdRJIGc 6TomYBf87oHWGKMajL8RMXoz85/TkgpLTYKVtgzlMxFFSrjFWes10d14AjGmHvzkustG wS+Q== X-Gm-Message-State: ALQs6tB3buLSxUm4hY0YY4csHTXC9Yzfw+ADyisIaQ941SBfoN8wQAXF ypeyMLv77GdLqJYYuwCx8LzSn+JJzNLm7ZX50I352w== X-Received: by 2002:a9d:510e:: with SMTP id c14-v6mr4620212oth.36.1522656085572; Mon, 02 Apr 2018 01:01:25 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a9d:cef:0:0:0:0:0 with HTTP; Mon, 2 Apr 2018 01:01:25 -0700 (PDT) In-Reply-To: <1522636236-12625-3-git-send-email-hejianet@gmail.com> References: <1522636236-12625-1-git-send-email-hejianet@gmail.com> <1522636236-12625-3-git-send-email-hejianet@gmail.com> From: Daniel Vacek Date: Mon, 2 Apr 2018 10:01:25 +0200 Message-ID: Subject: Re: [PATCH v5 2/5] arm: arm64: page_alloc: reduce unnecessary binary search in memblock_next_valid_pfn() To: Jia He Cc: Russell King , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Andrew Morton , Michal Hocko , Wei Yang , Kees Cook , Laura Abbott , Vladimir Murzin , Philip Derrin , Grygorii Strashko , AKASHI Takahiro , James Morse , Steve Capper , Pavel Tatashin , Gioh Kim , Vlastimil Babka , Mel Gorman , Johannes Weiner , Kemi Wang , Petr Tesarik , YASUAKI ISHIMATSU , Andrey Ryabinin , Nikolay Borisov , Daniel Jordan , Eugeniu Rosca , linux-arm-kernel , open list , linux-mm@kvack.org, Jia He Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 2, 2018 at 4:30 AM, Jia He wrote: > 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 | 28 ++++++++++++++++++++++------ > arch/arm64/include/asm/page.h | 1 + > arch/arm64/mm/init.c | 28 ++++++++++++++++++++++------ > 4 files changed, 46 insertions(+), 12 deletions(-) > > diff --git a/arch/arm/include/asm/page.h b/arch/arm/include/asm/page.h > index 489875c..f38909c 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; I believe this is not needed anymore. > extern int pfn_valid(unsigned long); > extern unsigned long memblock_next_valid_pfn(unsigned long pfn); > #define skip_to_last_invalid_pfn(pfn) (memblock_next_valid_pfn(pfn) - 1) > diff --git a/arch/arm/mm/init.c b/arch/arm/mm/init.c > index 0fb85ca..06ed190 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; static? > + > int pfn_valid(unsigned long pfn) > { > return memblock_is_map_memory(__pfn_to_phys(pfn)); > @@ -203,28 +205,42 @@ EXPORT_SYMBOL(pfn_valid); > unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn) > { > 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 (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; > + } > + > + /* 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_ARCH_PFN_VALID*/ > diff --git a/arch/arm64/include/asm/page.h b/arch/arm64/include/asm/page.h > index e57d3f2..f0d8c8e5 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; ditto > extern int pfn_valid(unsigned long); > extern unsigned long memblock_next_valid_pfn(unsigned long pfn); > #define skip_to_last_invalid_pfn(pfn) (memblock_next_valid_pfn(pfn) - 1) > diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c > index 13e43ff..342e4e2 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; ditto --nX > + > int pfn_valid(unsigned long pfn) > { > return memblock_is_map_memory(pfn << PAGE_SHIFT); > @@ -295,28 +297,42 @@ EXPORT_SYMBOL(pfn_valid); > unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn) > { > 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 (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; > + } > + > + /* 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_ARCH_PFN_VALID*/ > -- > 2.7.4 >