Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp715553iog; Fri, 24 Jun 2022 12:23:12 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sRRf8DVMfvtaC5TNMpx3L3hhVNQroOiyRk3T+OWJD5J+73HqzmoBsxDYfBWQdQSW47Crof X-Received: by 2002:a17:907:a0cb:b0:717:6218:3bf1 with SMTP id hw11-20020a170907a0cb00b0071762183bf1mr536211ejc.383.1656098592278; Fri, 24 Jun 2022 12:23:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656098592; cv=none; d=google.com; s=arc-20160816; b=i72FT3l1D1FH3vw5vyxtuuPRT2mmLL9U9IIk4XqriVis610tEe26Kq4/EMIv/OeU1x aarpJV/hJvmUootZ5iXccUBID0usKiT/s1+dTscf8otq7Yr+YElmJR4G2MK91g24yL4N a+NfqsZZ2UH3xvsVW6XzsusWiY43AdqrLvPDgG5h+R0MNtiSEeOI/835pE2NP4cWfRoW v5USXqndgTt6NxfbXZUuqDtSINHfa+l53GUaUsJTooZcUU14/3VKNX+FPK7nobaGGKi3 KcsYMWCTSavsa0HfsZ2Km7vSqxvAvS5tcNn3CF5wWFcZdJ4POY1UW9zr7jHM9rEA8vgh Uihg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=f+KB5A9ZpXRBOGLRH7DVETVdHE7Tly7PtMb324fnHg0=; b=RqVhH3WDZJ9AnmrdtCGKGzmjt/WRKQQgqiN5x4lJNEBUmiCIAJ+dXmlruKif3T+M8+ 7SSaQrzJI7LFc8+o1ZaiOWJ1rXRVTEV1W5DJob1P7RprMNEcwaJrYh9QViyDRgneZCNU 40Y3khTZemVUL5rj7gqjMe0RSYfeLG4cxajOo2TZQCl6imn8CUE5T5PFpBFVa1UihrTi of5s9LTbZrSLYDpjNyLUGiOJUsklqCxk6BCHngkohp0ikLcwDvO/+qJUh0a3+cVaV+mx 4D74NhaenLJw/rZBDh7xZyxGToKI4G1obmNvspnBX2aMy/aBZXQiObpc45TJ+jfXdYVl 1iKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Bb5vqOXA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t14-20020a056402524e00b004355ce84dd6si4649182edd.619.2022.06.24.12.22.46; Fri, 24 Jun 2022 12:23:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Bb5vqOXA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230423AbiFXSvx (ORCPT + 99 others); Fri, 24 Jun 2022 14:51:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229751AbiFXSvv (ORCPT ); Fri, 24 Jun 2022 14:51:51 -0400 Received: from mail-vk1-xa29.google.com (mail-vk1-xa29.google.com [IPv6:2607:f8b0:4864:20::a29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21CC681726 for ; Fri, 24 Jun 2022 11:51:51 -0700 (PDT) Received: by mail-vk1-xa29.google.com with SMTP id n187so1606937vkn.11 for ; Fri, 24 Jun 2022 11:51:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=f+KB5A9ZpXRBOGLRH7DVETVdHE7Tly7PtMb324fnHg0=; b=Bb5vqOXA7AxPyZxq8gpHAGLuTEdugkG+oQaCw8OF59I7gyQQL/F1tIh9p0B+Zw/J01 GZJyhBIIwVTeLO0wvEABar0lkjPdHp1P44B3cCUMb0sTaTMbuMKHo0Q60dIR7lsAqUe6 lLiGyEH1gkpH5LgBFecM4gCA3r3LugGWPRFTH3gy0xaERpEB4Idl/1Z46GQbMkoLaIml QwH2EVCeeaoKAtf07VekbnP511aRvslqoHt6iYZJcGh+KzFjP+02BYXXjKw2u41b5hIq dc/uEx4Kk0RbUMpD9ht7i4E26ed4S9wYIFh5DbnezOiDmLssWfaUdJDKmG/Bon0jud0g zVFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=f+KB5A9ZpXRBOGLRH7DVETVdHE7Tly7PtMb324fnHg0=; b=4Tqn7EzvanLdIiNj/t7Oq6YmX0LXjqxVuJyvZQ4d8cc4RDTp6LKAQikrszwK+hDcZr 2jt5xci3v39QWobFnEZx+qUyxocDoan7i4Y3hxDiezChcZaWj5mld2D9Jm2PnXhwwqHK stqWE7lauvb3J+EllPD5XwM9jD2w6Ip7dbLChgpvl1FMOkyGihbx1LFFB46jtzJGk+bS 5OmKIXasBnqXJQqZcrLkH6TxqzwwyhErX4UPmIvNkXQqrjTTCA/5pxzxg/JkeY4xmIFB hdseSzoQuPlhxfa3qkH/XyGOSXEA4H/AULEg9VP2rYIaFvwM6WpK7Ac96IWQ0Juqzj5/ Wz9A== X-Gm-Message-State: AJIora9tyqTZb9e7AZkt9iDKxFDtIpzIv9aNaHttGRGINQpobZngyM78 YmTbxXcd2TW23+TKodSvbdrHHvdLRMpOTQ47GH1ubw== X-Received: by 2002:a05:6122:1479:b0:36c:502b:fdda with SMTP id r25-20020a056122147900b0036c502bfddamr149274vkp.14.1656096710059; Fri, 24 Jun 2022 11:51:50 -0700 (PDT) MIME-Version: 1.0 References: <20220624173656.2033256-1-jthoughton@google.com> <20220624173656.2033256-3-jthoughton@google.com> In-Reply-To: <20220624173656.2033256-3-jthoughton@google.com> From: Mina Almasry Date: Fri, 24 Jun 2022 11:51:38 -0700 Message-ID: Subject: Re: [RFC PATCH 02/26] hugetlb: sort hstates in hugetlb_init_hstates To: James Houghton Cc: Mike Kravetz , Muchun Song , Peter Xu , David Hildenbrand , David Rientjes , Axel Rasmussen , Jue Wang , Manish Mishra , "Dr . David Alan Gilbert" , linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 24, 2022 at 10:37 AM James Houghton wrote: > > When using HugeTLB high-granularity mapping, we need to go through the > supported hugepage sizes in decreasing order so that we pick the largest > size that works. Consider the case where we're faulting in a 1G hugepage > for the first time: we want hugetlb_fault/hugetlb_no_page to map it with > a PUD. By going through the sizes in decreasing order, we will find that > PUD_SIZE works before finding out that PMD_SIZE or PAGE_SIZE work too. > Mostly nits: Reviewed-by: Mina Almasry > Signed-off-by: James Houghton > --- > mm/hugetlb.c | 40 +++++++++++++++++++++++++++++++++++++--- > 1 file changed, 37 insertions(+), 3 deletions(-) > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index a57e1be41401..5df838d86f32 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -33,6 +33,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -48,6 +49,10 @@ > > int hugetlb_max_hstate __read_mostly; > unsigned int default_hstate_idx; > +/* > + * After hugetlb_init_hstates is called, hstates will be sorted from largest > + * to smallest. > + */ > struct hstate hstates[HUGE_MAX_HSTATE]; > > #ifdef CONFIG_CMA > @@ -3144,14 +3149,43 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h) > kfree(node_alloc_noretry); > } > > +static int compare_hstates_decreasing(const void *a, const void *b) > +{ > + const int shift_a = huge_page_shift((const struct hstate *)a); > + const int shift_b = huge_page_shift((const struct hstate *)b); > + > + if (shift_a < shift_b) > + return 1; > + if (shift_a > shift_b) > + return -1; > + return 0; > +} > + > +static void sort_hstates(void) Maybe sort_hstates_descending(void) for extra clarity. > +{ > + unsigned long default_hstate_sz = huge_page_size(&default_hstate); > + > + /* Sort from largest to smallest. */ I'd remove this redundant comment; it's somewhat obvious what the next line does. > + sort(hstates, hugetlb_max_hstate, sizeof(*hstates), > + compare_hstates_decreasing, NULL); > + > + /* > + * We may have changed the location of the default hstate, so we need to > + * update it. > + */ > + default_hstate_idx = hstate_index(size_to_hstate(default_hstate_sz)); > +} > + > static void __init hugetlb_init_hstates(void) > { > struct hstate *h, *h2; > > - for_each_hstate(h) { > - if (minimum_order > huge_page_order(h)) > - minimum_order = huge_page_order(h); > + sort_hstates(); > > + /* The last hstate is now the smallest. */ Same, given that above is sort_hstates(). > + minimum_order = huge_page_order(&hstates[hugetlb_max_hstate - 1]); > + > + for_each_hstate(h) { > /* oversize hugepages were init'ed in early boot */ > if (!hstate_is_gigantic(h)) > hugetlb_hstate_alloc_pages(h); > -- > 2.37.0.rc0.161.g10f37bed90-goog >