Received: by 10.213.65.68 with SMTP id h4csp2140659imn; Mon, 2 Apr 2018 01:46:08 -0700 (PDT) X-Google-Smtp-Source: AIpwx49eS28W3u1CJP9BL3OVLHZDbqUkQFYttiUDCLesqoL3GpTouyntKntwHZAI///OqxdodsGh X-Received: by 2002:a17:902:28c3:: with SMTP id f61-v6mr9038486plb.114.1522658768615; Mon, 02 Apr 2018 01:46:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522658768; cv=none; d=google.com; s=arc-20160816; b=qgpIkO3JfvsEGYDLIK3JJD0Ch5MSSFEMP0WXiCk8I4wDZARN5yDiJ13O00vomQD4eR 7dotuyB0UXvBFmZlkPo7jC+OVOJL8Q752qtjqOxdymo58uvLoG69MkNR3axcsZm5TUfH kxM9j1vYr52DQZ1RvyQBViv5QXyV03H0sg8FJDftmhEWjUpUk3zokZmYbZ8oBrXm4HeJ 3wCMmITBgJ5aFGqz3EfUHefG9BRxNcTDmBBhx1mkzSo8sjTJSwzxaCskB/3Lwpo1aFva tBv5kGSE6rU78iYr7VERaCw2E0EHkEoPVZMkwuhHnlC8dHSRaiIVPCYJzPTRLQ1o08I8 Zf+w== 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=doC0zKq9fpd75wxdhydd0k1FGnH+ioKSseVB6klGTJY=; b=e/1fFBi4qd+aQn/qYqcZOGkpJMxIem/K3//Yup1/FJzXJs89I8U+SPDQb7J2tY7cRo lb4ltGxAQdHwFRt29SY4MTfTgXL80DfO4NVAEELJ50oapq0N2lfB6Qgv3ISDWj11rRUe dak2Cc3Tzv5QP3r/H11mS3PBOv4cmB3IO4RRYUxJl07Z3+XLuOV9964xYOG9PNMjuA2b cg0DcCF/TdgdIKl/PIkMFHQxqHE2gOiRn0C/SeqH5+M74+3cVGPKKFd1ZWK/xelEvnNO 124xXQJZUdbhxIcQlzk3nfCKNoBxiVbgkIB69q3jmRgdtDTPhjV23rBMZn1h8HGkeb6w ccTQ== 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 j11si10342705pff.363.2018.04.02.01.45.42; Mon, 02 Apr 2018 01:46:08 -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 S1752668AbeDBIoC (ORCPT + 99 others); Mon, 2 Apr 2018 04:44:02 -0400 Received: from mail-ot0-f194.google.com ([74.125.82.194]:41958 "EHLO mail-ot0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751588AbeDBIoA (ORCPT ); Mon, 2 Apr 2018 04:44:00 -0400 Received: by mail-ot0-f194.google.com with SMTP id i28-v6so14951948otf.8 for ; Mon, 02 Apr 2018 01:44:00 -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=doC0zKq9fpd75wxdhydd0k1FGnH+ioKSseVB6klGTJY=; b=rkXoHT0sLA2+Va3VstRJlVkYRom7WyOdk9YnNSVc4Fv1xUdW9+zMmdHXmvHIA+kF04 r7ZkbmpoPHZwTjh/Wu/tFyY92q6zfxRWYnVojE3BUlAmSfrmu3C6p3HDNSqiZL22mB+x RZie3A8KfKDYCRYhFmhDbTkh67Gmj72orHtWyvN5IbVbfacTg2cOPSK9h/hImOKV7O6v XQjW7pPptnVExBggpLGF71gLFZsM4r7mjtO8bqPZi8t8Mk5gLuQutQGXebSlfFlisHxD a9WwzW7v3s56WPiVVzi0/PGkaGtvWt9mNFd2bCcaT1uIxKu4x73FjrvXPoUUNW08fv2M 4ICQ== X-Gm-Message-State: ALQs6tBdzfVceEEdZWAh5axcYUgtQgGrYTQD4TXASwULSy6LjK1kmyHL 6+/V6d73UZGy+jmhHy5XTcTEDAnvA1/8XBRlyH4+TA== X-Received: by 2002:a9d:3844:: with SMTP id r4-v6mr5457791otd.90.1522658640059; Mon, 02 Apr 2018 01:44:00 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a9d:cef:0:0:0:0:0 with HTTP; Mon, 2 Apr 2018 01:43:59 -0700 (PDT) In-Reply-To: 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:43:59 +0200 Message-ID: Subject: Re: [PATCH v5 2/5] arm: arm64: page_alloc: reduce unnecessary binary search in memblock_next_valid_pfn() To: Ard Biesheuvel Cc: Jia He , Russell King , Catalin Marinas , Will Deacon , Mark Rutland , 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 , Linux Kernel Mailing List , Linux-MM , 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 8:57 AM, Ard Biesheuvel wrote: > On 2 April 2018 at 04:30, 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(-) >> > > Could we put this in a shared file somewhere? This is the second patch > where you put make identical changes to ARM and arm64. Ard, I was wondering if we can actually have this changed to something like CONFIG_MEMBLOCK_PFN_VALID and shared instead of it being arm specific? Is there a reason it's only usable for arm? The rest is dependent on this, hence I suggested to place it close-by. But generalizing it all would make it a lot cleaner. arch/arm/mm/init.c:196: #ifdef CONFIG_HAVE_ARCH_PFN_VALID int pfn_valid(unsigned long pfn) { return memblock_is_map_memory(__pfn_to_phys(pfn)); } EXPORT_SYMBOL(pfn_valid); #endif arch/arm64/mm/init.c:287: #ifdef CONFIG_HAVE_ARCH_PFN_VALID int pfn_valid(unsigned long pfn) { return memblock_is_map_memory(pfn << PAGE_SHIFT); } EXPORT_SYMBOL(pfn_valid); #endif --nX >> 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; >> 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; >> + >> 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; >> 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; >> + >> 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 >>