From mboxrd@z Thu Jan 1 00:00:00 1970 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 2413AC43334 for ; Fri, 8 Jul 2022 15:52:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5EA586B0072; Fri, 8 Jul 2022 11:52:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 59A936B0073; Fri, 8 Jul 2022 11:52:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 488836B0074; Fri, 8 Jul 2022 11:52:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 38E456B0072 for ; Fri, 8 Jul 2022 11:52:25 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id EDEE834E66 for ; Fri, 8 Jul 2022 15:52:24 +0000 (UTC) X-FDA: 79664374608.28.D1C7186 Received: from mail-lj1-f180.google.com (mail-lj1-f180.google.com [209.85.208.180]) by imf19.hostedemail.com (Postfix) with ESMTP id 8C6BD1A004B for ; Fri, 8 Jul 2022 15:52:23 +0000 (UTC) Received: by mail-lj1-f180.google.com with SMTP id bn33so5010546ljb.13 for ; Fri, 08 Jul 2022 08:52:23 -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=Ig0fLrjOD+WwRJX7jkWejWy3+d6k4pKBLJTMMKQa5iw=; b=Mqyh4QjFFCuoyNFr0CN3GLBew8pB06B9rmKexH6PXBNdnsPjA5Md58/XIpQPVB/SNx Fdg8T4Ihda5R1AerM47HKWqBiOFLE8uPspnFzm+sW8yi7Ohju85s8b7XnBWV0CHYkWUC 48mFRh6Ylo+QzrkP4bYNWMtxoR3Ao8BN4JbJKfy8P0TgVyJUqoX+wX1i7U0gDsQt5w00 n6vsEzz7vAVsXrPjUbIBtNB+N5rdmQt3277QmVWNukYdf5ZT/tPO1sUDJzL2oM3okjyQ FV4gg0QAMhTV3U3YiD9rXSbc9NVfTiYmcX6Nc+slay1zNuSVWQs6lf2ecXGwhwCJTuIa pXBQ== 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=Ig0fLrjOD+WwRJX7jkWejWy3+d6k4pKBLJTMMKQa5iw=; b=RsijL8eoBerCmp/VlNSkMxPypO2qYnd5Y5S15wAaWOfBwfy4hn3upphK5And6r4bXr 4LXH2bjvtF4LFzngHL/U/NqqmO/ahHFYXQSx/RrgFi9aOXpQAXHJK7phL16d2QAxu/4k 3r+RnLbrH3ZKr+HCy5l3sAOAzpmyWJdE8/fsqSTC/2/uuXf9nZFvv9sgIoj47cHj9VB1 4rottz5B9M1QUzqNXr4aoFn/rles3xlCpNFmdA1UT16WQ1T13Jqcd2g56dYZxgfVygAY VkfwmkOzwnUJkrjIJe1GYP/v/5XcfuGsp51mPkPPRElRiAT8S77VlMu2wJLh9oN0Axlj IwlQ== X-Gm-Message-State: AJIora+citOCY7DzQG4tVKVUavX0v+dvP1De75tanuODBjxLKwKqsou9 JcME+2m/DZ9+XTh7N85iabmI21CSaL/JPfklu9C2qA== X-Google-Smtp-Source: AGRyM1ugMc7DOpiIBjn/5wZrEmy+dmJqdHB2WXpFBf34um1n4FQVNb4QbC6I50CLT1WURSb9AoEHk6VCeeVSYQgB6NM= X-Received: by 2002:a2e:7006:0:b0:25c:3f3c:c6dc with SMTP id l6-20020a2e7006000000b0025c3f3cc6dcmr2272781ljc.469.1657295541820; Fri, 08 Jul 2022 08:52:21 -0700 (PDT) MIME-Version: 1.0 References: <20220624173656.2033256-1-jthoughton@google.com> <20220624173656.2033256-11-jthoughton@google.com> In-Reply-To: From: James Houghton Date: Fri, 8 Jul 2022 08:52:10 -0700 Message-ID: Subject: Re: [RFC PATCH 10/26] hugetlb: add for_each_hgm_shift To: Mina Almasry 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" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657295544; 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=Ig0fLrjOD+WwRJX7jkWejWy3+d6k4pKBLJTMMKQa5iw=; b=p+/T2U76jcVvBxAExCQ5KaBABqszYSbM0U44ODWii4dpRRvLndybHvQf7jWm+Og8bzj0J+ 06AT93QyBLl+b5bJzpeDdF9PE9ZQW8JnP1FX8oiQD3pItOzMzlMnrP+1+JCWTUs1LoXrx5 WHfzn7zrUpc8A9vRbEysselGO5R3i88= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657295544; a=rsa-sha256; cv=none; b=nJ10tkV5Lg/VWmYCnWrv3/D4YdvPpqmCfVLPuckZ/zLb1J6tDo+DN2UIMWO6XM5baqRv8X H7b4mTRfuLcB6LspzOneCo2dY3a6z4u5SypoAn0PlKiReh5YPOJbLjv7iEZPBa+q/1SP32 CI6l36QSBYnDrPN8z4Ca5Cw9XxAlWS8= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Mqyh4QjF; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of jthoughton@google.com designates 209.85.208.180 as permitted sender) smtp.mailfrom=jthoughton@google.com Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Mqyh4QjF; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of jthoughton@google.com designates 209.85.208.180 as permitted sender) smtp.mailfrom=jthoughton@google.com X-Rspamd-Server: rspam02 X-Stat-Signature: dfbj5pdgxzufjwmjjj9u1die55r6abg3 X-Rspamd-Queue-Id: 8C6BD1A004B X-Rspam-User: X-HE-Tag: 1657295543-491350 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: On Tue, Jun 28, 2022 at 2:58 PM Mina Almasry wrote: > > On Fri, Jun 24, 2022 at 10:37 AM James Houghton wrote: > > > > This is a helper macro to loop through all the usable page sizes for a > > high-granularity-enabled HugeTLB VMA. Given the VMA's hstate, it will > > loop, in descending order, through the page sizes that HugeTLB supports > > for this architecture; it always includes PAGE_SIZE. > > > > Signed-off-by: James Houghton > > --- > > mm/hugetlb.c | 10 ++++++++++ > > 1 file changed, 10 insertions(+) > > > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > > index 8b10b941458d..557b0afdb503 100644 > > --- a/mm/hugetlb.c > > +++ b/mm/hugetlb.c > > @@ -6989,6 +6989,16 @@ bool hugetlb_hgm_enabled(struct vm_area_struct *vma) > > /* All shared VMAs have HGM enabled. */ > > return vma->vm_flags & VM_SHARED; > > } > > +static unsigned int __shift_for_hstate(struct hstate *h) > > +{ > > + if (h >= &hstates[hugetlb_max_hstate]) > > + return PAGE_SHIFT; > > h > &hstates[hugetlb_max_hstate] means that h is out of bounds, no? am > I missing something here? Yeah, it goes out of bounds intentionally. Maybe I should have called this out. We need for_each_hgm_shift to include PAGE_SHIFT, and there is no hstate for it. So to handle it, we iterate past the end of the hstate array, and when we are past the end, we return PAGE_SHIFT and stop iterating further. This is admittedly kind of gross; if you have other suggestions for a way to get a clean `for_each_hgm_shift` macro like this, I'm all ears. :) > > So is this intending to do: > > if (h == hstates[hugetlb_max_hstate] > return PAGE_SHIFT; > > ? If so, could we write it as so? Yeah, this works. I'll write it this way instead. If that condition is true, `h` is out of bounds (`hugetlb_max_hstate` is past the end, not the index for the final element). I guess `hugetlb_max_hstate` is a bit of a misnomer. > > I'm also wondering why __shift_for_hstate(hstate[hugetlb_max_hstate]) > == PAGE_SHIFT? Isn't the last hstate the smallest hstate which should > be 2MB on x86? Shouldn't this return PMD_SHIFT in that case? `huge_page_shift(hstate[hugetlb_max_hstate-1])` is PMD_SHIFT on x86. Actually reading `hstate[hugetlb_max_hstate]` would be bad, which is why `__shift_for_hstate` exists: to return PAGE_SIZE when we would otherwise attempt to compute `huge_page_shift(hstate[hugetlb_max_hstate])`. > > > + return huge_page_shift(h); > > +} > > +#define for_each_hgm_shift(hstate, tmp_h, shift) \ > > + for ((tmp_h) = hstate; (shift) = __shift_for_hstate(tmp_h), \ > > + (tmp_h) <= &hstates[hugetlb_max_hstate]; \ Note the <= here. If we wanted to always remain inbounds here, we'd want < instead. But we don't have an hstate for PAGE_SIZE. > > + (tmp_h)++) > > #endif /* CONFIG_HUGETLB_HIGH_GRANULARITY_MAPPING */ > > > > /* > > -- > > 2.37.0.rc0.161.g10f37bed90-goog > >