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 B1F98C61DA4 for ; Sat, 18 Feb 2023 05:24:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 94A296B0071; Sat, 18 Feb 2023 00:24:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FABA6B0072; Sat, 18 Feb 2023 00:24:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 79B1D6B0073; Sat, 18 Feb 2023 00:24:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6B65E6B0071 for ; Sat, 18 Feb 2023 00:24:39 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 32E61A0AB6 for ; Sat, 18 Feb 2023 05:24:39 +0000 (UTC) X-FDA: 80479272678.19.AF7F158 Received: from mail-il1-f182.google.com (mail-il1-f182.google.com [209.85.166.182]) by imf21.hostedemail.com (Postfix) with ESMTP id 67CE51C0007 for ; Sat, 18 Feb 2023 05:24:37 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=B5AHjdEs; spf=pass (imf21.hostedemail.com: domain of almasrymina@google.com designates 209.85.166.182 as permitted sender) smtp.mailfrom=almasrymina@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=1676697877; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HhiJ1/D2QxeIv9bIqqR07iYwFrWIf3Mp0nK6fpVQfRM=; b=mZBBvUgPgxAkd73YWMBob8B/WpXx41V8Xtz4tv8v97j1++D6EEbgmPomzmo0UwIssSb1vI U5rpv5x0OlDn15Sun5FD+q23gqwnhiCH6k33Kdyfu/CP3alGFKzuFJqt7mHxtRnAzY5Z++ xqKHHsjIOhpoYHI+ncvRpmJ4os+p8YE= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=B5AHjdEs; spf=pass (imf21.hostedemail.com: domain of almasrymina@google.com designates 209.85.166.182 as permitted sender) smtp.mailfrom=almasrymina@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676697877; a=rsa-sha256; cv=none; b=wK/+ZZnOKahoGt/eZ58b4QQ8FvYWlXYSI3YefFKKtYtblhGRTn2HPnjPd4HsSSkxwIOYsf eMT1G8UdrO3Yp4ue8qWth0X/OTLEM54RL37tHHHt1QL8527nepvydLoXnPzIkeg6nhXxI9 8vF+CqOpsHZ3kO5PWhMPhf8Kxrlfg84= Received: by mail-il1-f182.google.com with SMTP id d4so1151506ilm.1 for ; Fri, 17 Feb 2023 21:24:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=HhiJ1/D2QxeIv9bIqqR07iYwFrWIf3Mp0nK6fpVQfRM=; b=B5AHjdEs6uKyKkCTjXHEqP9qkra+r0YkBfV3Ukr+beq3Z6OGtNGJwX/QaX/xUX9g0N raOpNqCaVtBts85Vo5MW8/Ks0fMe0b5l2q9OkmzK86vDgZj3rI93CwNMW8VKUSyr3ptf MVTNg8Kdg+GWVNLJjuD09aM2KPg+Z9y/LjgQwsQZIqGIrUJCtqPfED1L/3esx1WBdw/9 iwzc//b8ZSsiOqfjz98NZ0nberI0Vzo+d+4jEG5jCUBA3xUEaoGA+PzPVEY1xmt7sDP0 bcC51FbUVOisA3gXZ3JSko8NNi0V+sLhChy+ITBOzExKoNOqO5bJTRlaosSMMuGUdPK7 jR1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HhiJ1/D2QxeIv9bIqqR07iYwFrWIf3Mp0nK6fpVQfRM=; b=V9/Dlc/J79/2wP3JQ7xaGnLMxYhXh/UaqbJrsRXF58q0HM823rUqzBGp3oQ7eufbfI 5s6HgYgtiMJGq1Wj+UXFFSQR21Jt4oCCbdHfixRIR8AkvuR23UEu38VT6HgZkMj3InM+ gPDMEULcMuSOvxlZfVGSRAwAPmmE0Z+0NsiKgaMD0LR4veeFs6yphzjjFDjpr6BbSx6/ HsPdae/BfkhPcAHXh8X32q+gO6hXixNbE3NRA2ImUurQynJtdNf4Jnsx4O6wFuqK8dKl wTc+QHUlDbBhxuNHHjS9B6/7M7O/TLrW7bQ/obt67X4LczSCMQybmX6OD+mlNU0k9ncw xZyg== X-Gm-Message-State: AO0yUKVnG1pbTJRUE9F8Yn7lFgJeCZPa6T7EPk/HAnqeLS8cDpM9cJ6c 37dbsYP86cTqkCyKX++gYPzxAkvdkkz2iyGSjrOBGw== X-Google-Smtp-Source: AK7set9uUebKdPMIPLmMs9VdI2KWQs5nCUTc2SQqMGLHyxTtJHhuOxcrrzXZIsykp/XMPRMfctB57tbOTsBnLDXgf18= X-Received: by 2002:a05:6e02:1311:b0:315:8f6c:50a6 with SMTP id g17-20020a056e02131100b003158f6c50a6mr1786793ilr.1.1676697876362; Fri, 17 Feb 2023 21:24:36 -0800 (PST) MIME-Version: 1.0 References: <20230218002819.1486479-1-jthoughton@google.com> <20230218002819.1486479-12-jthoughton@google.com> In-Reply-To: <20230218002819.1486479-12-jthoughton@google.com> From: Mina Almasry Date: Fri, 17 Feb 2023 21:24:24 -0800 Message-ID: Subject: Re: [PATCH v2 11/46] hugetlb: add hugetlb_pte to track HugeTLB page table entries To: James Houghton Cc: Mike Kravetz , Muchun Song , Peter Xu , Andrew Morton , David Hildenbrand , David Rientjes , Axel Rasmussen , "Zach O'Keefe" , Manish Mishra , Naoya Horiguchi , "Dr . David Alan Gilbert" , "Matthew Wilcox (Oracle)" , Vlastimil Babka , Baolin Wang , Miaohe Lin , Yang Shi , Frank van der Linden , Jiaqi Yan , linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 67CE51C0007 X-Stat-Signature: gkg9pracbnq6gu4b5jyna899ud494fn5 X-Rspam-User: X-HE-Tag: 1676697877-958891 X-HE-Meta: U2FsdGVkX1+hPtpR1H5eXgptpGwMj4QXBNvDEOHX+/ZW0TOTa5wdlKuxdGRRDWSoPTeyq+qQVrxf1W5cwF9VZpdW+KH1sS3gaMyUUgPK+PTgyeUgQ0LMYF/bXkxNIyot1RAU3pnJjSFZoC8SshTSYIEAu6x6i4PGcZHh3IUvIcYtUr8IFoJhvI5ufybAd61e19WQBhwCga8WWlBe+iMZAFJS0y92gSxsKV9mbW5fL89g6Ks8wnGCxWU9/52P9PwTQJR3fzlaedr2k2ZHRJ7qCIamI4GteASSMNKlzQnXgWKiJ0ulEKLkTbIadzfRyudUsqF4JUG79IgUDMTVYJjLaGPXOKg8ePhVEQ7rwF/OlWD92r3gB8dV9tYOlUnFnvzQoIsxo8b8QNrqSvE5d6f9yytc8XEJc0np8qY1bGpi8IJWoCavcMyzm2kIGFCAyBv8uv2zPDjgOcQs302LL8BXz2tKugyftljqEwo2mYB7/ZU2XPlLjiJgQsb1251Zx+WRNfBZzh2ohbAItk0AAVV4im8Hl+MGUoc6IeQUPCrh1MJpvgiF9oL1I+kX1eNCbUzGLDnXjqs5SywyRkaAUruCFXkcPMVSdOwgxRKxm0xb6cwQdChoccqOTgWYHx8LUOKtzTFkP0NZq1smDixUhSeOCUsbmH3+HyeBSRFggL/N+HdWwrTy+r0/ToHicA7smcmuLcLYd/AvghInAmJCWj+gogbu4C2PaY5nVYdsPx05UVIGZk/Hb5fwr2Rjnm7uh5MsNTKpM0yq9pKQVWWLIX2N1LXc9nMjSNS9BY5ADCkqyYVvkFPhud++lpyv2SuEzkVlcY/HuJuX53n5wdT0UVVt5r12++SN1So00VFbnVxOoVgGIqNYFRlHbaQrgAWBsm0YkwuK38UfK+PQK7KbnHPydryH+lSoheVFSF+Dn6pIUhyyFn0qEMIwOdzycQW48oWPHR/xYOme0FhpRoekOms v9HULOi7 6o9hhGGt1GeubmFxMh39bmNpOyqmUTgyifPlbwHgY+cYAOZg1DbWQJnyNyNCubHkiypa8XYbZKHxl0h6sYNu3aBfjGG/Z/ZFqg5dYpKNVNly5F2Wvv1e/RL6RffRm7EOTbnpfuTbLXRna8fYl4it/PfKzdXIlOXDs2nXClz3YoCTpu89UYizjwxfebdrR95XgrBZ11SlmwbcSMM5Mp4Pt2jMgNm5zZxXNNuj9YDtgCnq2O56fEvfGfQpg9ufX/5hDE9Sy81fIXmLbMNHJ6T3eTZBypxmzhuIvVhxW+gsGpu0YMR4wuDbU6wtphKqZEXXuB6Wp 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 Fri, Feb 17, 2023 at 4:28=E2=80=AFPM James Houghton wrote: > > After high-granularity mapping, page table entries for HugeTLB pages can > be of any size/type. (For example, we can have a 1G page mapped with a > mix of PMDs and PTEs.) This struct is to help keep track of a HugeTLB > PTE after we have done a page table walk. > > Without this, we'd have to pass around the "size" of the PTE everywhere. > We effectively did this before; it could be fetched from the hstate, > which we pass around pretty much everywhere. > > hugetlb_pte_present_leaf is included here as a helper function that will > be used frequently later on. > > Signed-off-by: James Houghton > Only nits. Reviewed-by: Mina Almasry > diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h > index a1ceb9417f01..eeacadf3272b 100644 > --- a/include/linux/hugetlb.h > +++ b/include/linux/hugetlb.h > @@ -26,6 +26,25 @@ typedef struct { unsigned long pd; } hugepd_t; > #define __hugepd(x) ((hugepd_t) { (x) }) > #endif > > +enum hugetlb_level { > + HUGETLB_LEVEL_PTE =3D 1, > + /* > + * We always include PMD, PUD, and P4D in this enum definition so= that, > + * when logged as an integer, we can easily tell which level it i= s. > + */ > + HUGETLB_LEVEL_PMD, > + HUGETLB_LEVEL_PUD, > + HUGETLB_LEVEL_P4D, > + HUGETLB_LEVEL_PGD, > +}; > + > +struct hugetlb_pte { > + pte_t *ptep; > + unsigned int shift; > + enum hugetlb_level level; > + spinlock_t *ptl; > +}; > + > #ifdef CONFIG_HUGETLB_PAGE > > #include > @@ -39,6 +58,20 @@ typedef struct { unsigned long pd; } hugepd_t; > */ > #define __NR_USED_SUBPAGE 3 > > +static inline > +unsigned long hugetlb_pte_size(const struct hugetlb_pte *hpte) > +{ > + return 1UL << hpte->shift; > +} > + > +static inline > +unsigned long hugetlb_pte_mask(const struct hugetlb_pte *hpte) > +{ > + return ~(hugetlb_pte_size(hpte) - 1); > +} > + > +bool hugetlb_pte_present_leaf(const struct hugetlb_pte *hpte, pte_t pte)= ; > + > struct hugepage_subpool { > spinlock_t lock; > long count; > @@ -1234,6 +1267,45 @@ static inline spinlock_t *huge_pte_lock(struct hst= ate *h, > return ptl; > } > > +static inline > +spinlock_t *hugetlb_pte_lockptr(struct hugetlb_pte *hpte) > +{ > + return hpte->ptl; > +} I find this helper unnecessary. I would remove it. > + > +static inline > +spinlock_t *hugetlb_pte_lock(struct hugetlb_pte *hpte) > +{ > + spinlock_t *ptl =3D hugetlb_pte_lockptr(hpte); > + > + spin_lock(ptl); Here 'spin_lock(hpte->ptl)' would be more immediately understandable IMO, for example. > + return ptl; > +} > + > +static inline > +void __hugetlb_pte_init(struct hugetlb_pte *hpte, pte_t *ptep, > + unsigned int shift, enum hugetlb_level level, > + spinlock_t *ptl) > +{ > + /* > + * If 'shift' indicates that this PTE is contiguous, then @ptep m= ust > + * be the first pte of the contiguous bunch. > + */ I would move the comment to above the function as a pseudo doc. It seems to instruct the user of the function of how to use it. > + hpte->ptl =3D ptl; > + hpte->ptep =3D ptep; > + hpte->shift =3D shift; > + hpte->level =3D level; > +} > + > +static inline > +void hugetlb_pte_init(struct mm_struct *mm, struct hugetlb_pte *hpte, > + pte_t *ptep, unsigned int shift, > + enum hugetlb_level level) > +{ > + __hugetlb_pte_init(hpte, ptep, shift, level, > + huge_pte_lockptr(shift, mm, ptep)); > +} > + > #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA) > extern void __init hugetlb_cma_reserve(int order); > #else > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index 5ca9eae0ac42..6c74adff43b6 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -1269,6 +1269,35 @@ static bool vma_has_reserves(struct vm_area_struct= *vma, long chg) > return false; > } > > +bool hugetlb_pte_present_leaf(const struct hugetlb_pte *hpte, pte_t pte) > +{ > + pgd_t pgd; > + p4d_t p4d; > + pud_t pud; > + pmd_t pmd; > + > + switch (hpte->level) { > + case HUGETLB_LEVEL_PGD: > + pgd =3D __pgd(pte_val(pte)); > + return pgd_present(pgd) && pgd_leaf(pgd); > + case HUGETLB_LEVEL_P4D: > + p4d =3D __p4d(pte_val(pte)); > + return p4d_present(p4d) && p4d_leaf(p4d); > + case HUGETLB_LEVEL_PUD: > + pud =3D __pud(pte_val(pte)); > + return pud_present(pud) && pud_leaf(pud); > + case HUGETLB_LEVEL_PMD: > + pmd =3D __pmd(pte_val(pte)); > + return pmd_present(pmd) && pmd_leaf(pmd); > + case HUGETLB_LEVEL_PTE: > + return pte_present(pte); > + default: > + WARN_ON_ONCE(1); > + return false; > + } > +} > + > + > static void enqueue_hugetlb_folio(struct hstate *h, struct folio *folio) > { > int nid =3D folio_nid(folio); > -- > 2.39.2.637.g21b0678d19-goog >