Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp427773imm; Thu, 28 Jun 2018 23:43:04 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIHDJeLvNMrwyjQgLFN4dfiL/JFWAvQJuZIbINo1JX505xHoLwrz4s4Uk/MP2aU9FHOntie X-Received: by 2002:a17:902:6e09:: with SMTP id u9-v6mr13260513plk.13.1530254584020; Thu, 28 Jun 2018 23:43:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530254583; cv=none; d=google.com; s=arc-20160816; b=zBnxe8utiCbnNoRM55PKqdqB6QQQxImf0kqy7w1DB2VLVPrQE/nOC4ayOloxnQE4+h xFlnWM3LDu0F2xEn7C9ZNzZS4h4uvABPXo/fyog3rD4XsTAs/+V/Di12r4+xdgjNqVnB 1Un7tayqSMoBf/rETU2WQHwBLpvLzH9qLuOXKKQ4rTQWpKs2I+EAvuvFV9WaiV4ZlPdK +/uphfmKBhZ6ldBXvRtYVdbr4v/eK9jU3AeCASGfrC3JCuA6rjlwGpwZgM6F3pJf9J/8 TyDTBB0q5TtynTMFknaulxkdmfa9uWVmbrkF2cUEPnlf5vOl8ASr6XoLYxNgWH/MXish vCrw== 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=prJMZsopAOGvd4i4ZRQwEORKw4GozwPrjoRsWm2lbeQ=; b=zbyetFTBQe07s2Ipi+0U91phPpxrgZ/nGRKIK7NczqQy2xypDObHJznURwIad4eu+O pfrrtsha+k0zh26OE/uYpJbzyFJgxj59at2cLnQgL38Oj9gI5Hene71Xp89XZSJKrtbE sLruzMKsAljkOU5Paz/9r/ztI7rmkOeHmgPZkDQb7D0eff+VigJYBYpgxZjC6cmI0pWt x7zGstXm14vcarJVl7v3jIeFm9fmRBYxAC4RqMYtqcoRvDK/RtuSaLL0b31mrj8FzhPs L1u4LD89qNXR1/f+m7mzd1qagG40/f9T+mkv5NTYn+SJwoU2mkMnomRK1rlA+99jT2V6 Xdaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=lgz2ZEIT; 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 l7-v6si2344924pgs.541.2018.06.28.23.42.49; Thu, 28 Jun 2018 23:43:03 -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=lgz2ZEIT; 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 S966740AbeF2Cal (ORCPT + 99 others); Thu, 28 Jun 2018 22:30:41 -0400 Received: from mail-qt0-f193.google.com ([209.85.216.193]:34236 "EHLO mail-qt0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S936532AbeF2Cai (ORCPT ); Thu, 28 Jun 2018 22:30:38 -0400 Received: by mail-qt0-f193.google.com with SMTP id d23-v6so6663798qtp.1 for ; Thu, 28 Jun 2018 19:30:38 -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=prJMZsopAOGvd4i4ZRQwEORKw4GozwPrjoRsWm2lbeQ=; b=lgz2ZEITaTVt04bNM0MT1SISuJTXx0Rrp9eDDycJTaX5aaZ2EjdEkQIohH1B4y5DJ1 UVtQ05IMUPPQDIkMJomg4YdUJZdcRLkstlBJEJrl0EphcxFg2qo31ZgwygThX1wsIBUS I7i4MMopE7BZN7yy/IdUjYiQBmMKyS118ZTsDrbJnogA8q3ub001IAScmRzo2y+dgS2N G1/ImPXpnQ7Cx+on6s9Qd5DXMS7oule9bzFoqJXSgqtJ3CLUNb0UU/3XXJwxNgLJ4N4y F411z6/P0abiJ8Eqsk4pIg7nfNf//HZ6Zab/BvUPZRskxzfOfDuir+diI5NAVptBt5lP uUjg== 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=prJMZsopAOGvd4i4ZRQwEORKw4GozwPrjoRsWm2lbeQ=; b=IaXYH18MAvH8mNL41+DCjd2BtnAr47ZCeT1tG9YRk4GHitoDMiMTV6V0tO+5lK2OFr XGlp6pXXWJtzw3p6qFZhXWLt8nOGuASOAMM3po64q20btR227dXUTRTlN4pPhATGRUL1 eA0TY1RDURYH+jvG2+K6tjxriT/eBmTgqJUJZWJYw2eHU3Gb/RwtUUuEz1v0798rTdLW EDwKLPO7d9oY+HspzM9o6oRQJUGHfHkANHlftJoFfgF5+IXkpb57alwwoSYXNRg3EfJe pgZRfNifDu0a4Sa85oCoQOUDZLcdTZQgtyv3BlxYpRysm89eeqcY7iV1Mf3bPm/jS33I JrPw== X-Gm-Message-State: APt69E0ZcB6zlD2Y3xgLb1abW4Q9eZkXPlF3jU4dLbtvv8BHLTZlJe3d +s68Quw/YKLpBpRAnh6jnkE= X-Received: by 2002:aed:2a4a:: with SMTP id k10-v6mr4494886qtf.102.1530239437883; Thu, 28 Jun 2018 19:30:37 -0700 (PDT) Received: from ct7host.localdomain ([38.106.11.25]) by smtp.gmail.com with ESMTPSA id y25-v6sm6390186qtc.48.2018.06.28.19.30.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 28 Jun 2018 19:30:37 -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 , 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 v9 3/6] arm: arm64: page_alloc: reduce unnecessary binary search in memblock_next_valid_pfn() Date: Fri, 29 Jun 2018 10:29:20 +0800 Message-Id: <1530239363-2356-4-git-send-email-hejianet@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1530239363-2356-1-git-send-email-hejianet@gmail.com> References: <1530239363-2356-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. 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*/ -- 1.8.3.1