From patchwork Fri Jun 24 17:36:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 12894927 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 43948CCA473 for ; Fri, 24 Jun 2022 17:37:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BA6C08E0240; Fri, 24 Jun 2022 13:37:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B2F1D8E020E; Fri, 24 Jun 2022 13:37:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 90DF58E0240; Fri, 24 Jun 2022 13:37:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 76B058E020E for ; Fri, 24 Jun 2022 13:37:14 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 337C360830 for ; Fri, 24 Jun 2022 17:37:14 +0000 (UTC) X-FDA: 79613835588.17.5739C30 Received: from mail-ua1-f74.google.com (mail-ua1-f74.google.com [209.85.222.74]) by imf02.hostedemail.com (Postfix) with ESMTP id BF3B88002E for ; Fri, 24 Jun 2022 17:37:13 +0000 (UTC) Received: by mail-ua1-f74.google.com with SMTP id y15-20020ab0638f000000b00368a2d9b075so1012400uao.13 for ; Fri, 24 Jun 2022 10:37:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=cBo7lIiy2jlsdOG5qT1oBCHlWVvIiK17uVsMNAgjZEA=; b=NJEF0grG6xcnUMzJ7Xx8DncxrM3Dg/CCXuZcQoPGVS8RMCRW7OVDsyTMq2msiIkgR3 QD3rc43cPJSTJK/tuSyrg1IgeA7F7F0rpMnzeXl2raeM1+E+9KIbYlUZ6X/l+oQ1o3X8 FetdG0D5zD5JUcSef1agw+Hok5ZWwdiJaZLaL/pl4egaQHqe4C6GaU0IcgckBRNz/QIi XvX1VQlmkldTFcvliG4NEQiL7t+Leo3XWSFsPzela+hWWv2mTwnOC5NDGzw04gi1JEMx Hu59PBmYCm43RJHjH32Pik1VWkbEEt2CtqEMLAHbbH1gUokwIOPzG/lqTGqFU3e6iia/ o9Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=cBo7lIiy2jlsdOG5qT1oBCHlWVvIiK17uVsMNAgjZEA=; b=o+d7ocXjihxG/nWga05noR8oASN53ts7cvQItCwbUvVGCyFVQ+KdfCjvxcrPyFgkO4 lG+E9blwNDF14OWYQbDbM+HfNoGm34OqXogTdfRyQg6M8oqXxn/BBcqexnUiRC9V3iFW lbq06TqKX0gPcwfXwcva0ywozBpCtdcpIZN42wJnYX9NdxgaUjC24zXy+RoU/RFN2TlP aPruVz4HLozl82cRQRToKYWwObKRiM7wR/62ENsQqaP+9Wmfb94ej19KHheM5pgkeTKT w6wt/1/AaCaocKrhStrLx2JpDvpm7Tb2MaS0UYuX86TvwcVxLyF7fiayqITP7NrskQDp n05w== X-Gm-Message-State: AJIora+ywPuRiyWEMmqDHVYSEnqQMba0eq9rHpY+uT7QzKx6m+co4TXB OMQspiIH5oZuvz0pqniIWMlSYc/+KO146U0s X-Google-Smtp-Source: AGRyM1uKLxV44YzBDP/q42+n2ejxUvrdr7EuCEClIr+ZyRlO8QyvjxzdjM1m0/iYLEcLM71c8MSRgqLgbnEZ/vLa X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6122:25b:b0:36c:5f1a:d94b with SMTP id t27-20020a056122025b00b0036c5f1ad94bmr11908478vko.31.1656092233083; Fri, 24 Jun 2022 10:37:13 -0700 (PDT) Date: Fri, 24 Jun 2022 17:36:32 +0000 In-Reply-To: <20220624173656.2033256-1-jthoughton@google.com> Message-Id: <20220624173656.2033256-3-jthoughton@google.com> Mime-Version: 1.0 References: <20220624173656.2033256-1-jthoughton@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [RFC PATCH 02/26] hugetlb: sort hstates in hugetlb_init_hstates From: James Houghton To: Mike Kravetz , Muchun Song , Peter Xu Cc: David Hildenbrand , David Rientjes , Axel Rasmussen , Mina Almasry , Jue Wang , Manish Mishra , "Dr . David Alan Gilbert" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, James Houghton ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=NJEF0grG; spf=pass (imf02.hostedemail.com: domain of 3Sfa1YgoKCCoPZNUaMNZUTMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--jthoughton.bounces.google.com designates 209.85.222.74 as permitted sender) smtp.mailfrom=3Sfa1YgoKCCoPZNUaMNZUTMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1656092233; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=cBo7lIiy2jlsdOG5qT1oBCHlWVvIiK17uVsMNAgjZEA=; b=Fu6E7okx6HfXouFZEOLfPOLWCS/T1/PJJFsmfVYgQKrJKlfXOmpVhsNqYb8TFTUFnXSQIA NryeuODQG2pywP9w/sS+v3jjTA4UE+vMx4wtIKDMrbnlxjoqgxK4B4CxswO3VP2T+HcDGs QZloblJ7+2z5Q7ecv7aqKrCMFKZe3Bk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1656092233; a=rsa-sha256; cv=none; b=QZlWY12xJlFK7uyeF76u9GCYmymreGgkK55FrQbjlfkANOTAT4vMrgyMzcPV60uss8x087 jnKfyQ6IK+H4L4MrhMM/WBoyqmICs6wG019rnVgEVzxtY3IoIqxhVDJcajiT5ON7P4e3uD cxs/P9IGpyLQ+p9UP8Xn312sHSvwO2Y= X-Stat-Signature: xurecfy9a5gw9tqpgcg54dzdyoyzjddr X-Rspamd-Queue-Id: BF3B88002E X-Rspam-User: Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=NJEF0grG; spf=pass (imf02.hostedemail.com: domain of 3Sfa1YgoKCCoPZNUaMNZUTMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--jthoughton.bounces.google.com designates 209.85.222.74 as permitted sender) smtp.mailfrom=3Sfa1YgoKCCoPZNUaMNZUTMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam02 X-HE-Tag: 1656092233-893495 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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. Signed-off-by: James Houghton Reviewed-by: Mina Almasry --- 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) +{ + unsigned long default_hstate_sz = huge_page_size(&default_hstate); + + /* Sort from largest to smallest. */ + 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. */ + 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);