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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BE380C433EF for ; Sun, 7 Nov 2021 23:58:19 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 50A8661390 for ; Sun, 7 Nov 2021 23:58:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 50A8661390 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id DE1506B00B8; Sun, 7 Nov 2021 18:58:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DC38F6B00B9; Sun, 7 Nov 2021 18:58:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C57A46B00BA; Sun, 7 Nov 2021 18:58:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0021.hostedemail.com [216.40.44.21]) by kanga.kvack.org (Postfix) with ESMTP id B86A16B00B8 for ; Sun, 7 Nov 2021 18:58:18 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 7C37874D11 for ; Sun, 7 Nov 2021 23:58:18 +0000 (UTC) X-FDA: 78783800676.22.8E43761 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf05.hostedemail.com (Postfix) with ESMTP id 10B4750911E9 for ; Sun, 7 Nov 2021 23:57:58 +0000 (UTC) Received: by mail-pg1-f174.google.com with SMTP id g184so13624485pgc.6 for ; Sun, 07 Nov 2021 15:58:17 -0800 (PST) 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:cc; bh=T0VflAP+LrtUTYUjNUWRaGSujQrQr2/aR5rO20NzFrs=; b=W+r3HOQ0w9F7y1uT5xvnAeCFv1EX3j4X1d2HiD03Mdkf8UMTHUGZ7ZtePwsdMa26BW O3tpARFT5qzXX33gBRo+jTFQ6Dmmo+lONk9KO+agZW3/gauaMhZBhFk21lEBMbWknoi+ QcOPKcYT8AysxW8h75DFL+yPYuCrmegMDneLsKuRCormEjXe81MeBGOzUZdPFvot1Cdq /DOnzabyOaPF6cqpZR3s6LyFGfsUm8cryRidWIthjas6pGRle33kmbouTnQWdt0LoCd+ LNRL6VR6pHM62GwReZgqbCm1WPUuvDXEb9TdYVSmU4kkV4OoIuHx5DTlBqcc2sAkNZga d62A== 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:cc; bh=T0VflAP+LrtUTYUjNUWRaGSujQrQr2/aR5rO20NzFrs=; b=cKReNBLfjDiAAtk+A6imcgftMcfEzgPSKPYNDgmlwWE9v6StOvLeM6j2S427jlD5z5 9Fxf+HSJQJF9kc91YY3Llfj0d5mmyUwXho557SM29QDUcXyqWIyv/60kir7onq2xnK/I x9o9lU705fRrcHsdPkD9SpOsQx2XmL0FTXONBnRmDsdRa81X0agFdkWx09EibUdERdQs 5BxdAcN2EzFZ1oScDJSsdwGYDHVasWHIT3wGWkL1PAn3nqImUVLfoTf2+0RkJAanHs08 PK/pVMSSXrPeRQxtHDHKEZW0/YsoNPnG3Khlqo5ksmLZn+LS8cM7hzZ0V4Iucg4+qYKG JYRw== X-Gm-Message-State: AOAM531ExI0KkW3z/px+6XNGee98S/HF2CMNNOzNTb4wfmJKq+ybTP3C 9D1JAHOMsHxKOqDbBjoAnSJEjP6P6Pbzp6rKXB0HUWwqics= X-Received: by 2002:a63:4f4a:: with SMTP id p10mt31609020pgl.295.1636329128205; Sun, 07 Nov 2021 15:52:08 -0800 (PST) MIME-Version: 1.0 References: <20211107225947.1287388-1-almasrymina@google.com> In-Reply-To: <20211107225947.1287388-1-almasrymina@google.com> From: Mina Almasry Date: Sun, 7 Nov 2021 15:51:57 -0800 Message-ID: Subject: Re: [PATCH v3] mm: Add PM_HUGE_THP_MAPPING to /proc/pid/pagemap Cc: David Hildenbrand , Matthew Wilcox , "Paul E . McKenney" , Yu Zhao , Jonathan Corbet , Andrew Morton , Peter Xu , Ivan Teterevkov , Florian Schmidt , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 10B4750911E9 X-Stat-Signature: 4cbkcy6tqxncq1gmuz4np9mej3r5w61r Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=W+r3HOQ0; spf=pass (imf05.hostedemail.com: domain of almasrymina@google.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=almasrymina@google.com; dmarc=pass (policy=reject) header.from=google.com X-HE-Tag: 1636329478-739174 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 Sun, Nov 7, 2021 at 2:59 PM Mina Almasry wrote: > > Add PM_HUGE_THP MAPPING to allow userspace to detect whether a given virt > address is currently mapped by a transparent huge page or not. > > Example use case is a process requesting THPs from the kernel (via > a huge tmpfs mount for example), for a performance critical region of > memory. The userspace may want to query whether the kernel is actually > backing this memory by hugepages or not. > > PM_HUGE_THP_MAPPING bit is set if the virt address is mapped at the PMD > level and the underlying page is a transparent huge page. > > Tested manually by adding logging into transhuge-stress, and by > allocating THP and querying the PM_HUGE_THP_MAPPING flag at those > virtual addresses. > > Signed-off-by: Mina Almasry > > Cc: David Hildenbrand > Cc: Matthew Wilcox > Cc: David Rientjes rientjes@google.com > Cc: Paul E. McKenney > Cc: Yu Zhao > Cc: Jonathan Corbet > Cc: Andrew Morton > Cc: Peter Xu > Cc: Ivan Teterevkov > Cc: Florian Schmidt > Cc: linux-kernel@vger.kernel.org > Cc: linux-fsdevel@vger.kernel.org > Cc: linux-mm@kvack.org > > --- > Documentation/admin-guide/mm/pagemap.rst | 3 ++- > fs/proc/task_mmu.c | 6 +++++- > tools/testing/selftests/vm/transhuge-stress.c | 21 +++++++++++++++---- > 3 files changed, 24 insertions(+), 6 deletions(-) > > diff --git a/Documentation/admin-guide/mm/pagemap.rst b/Documentation/admin-guide/mm/pagemap.rst > index fdc19fbc10839..8a0f0064ff336 100644 > --- a/Documentation/admin-guide/mm/pagemap.rst > +++ b/Documentation/admin-guide/mm/pagemap.rst > @@ -23,7 +23,8 @@ There are four components to pagemap: > * Bit 56 page exclusively mapped (since 4.2) > * Bit 57 pte is uffd-wp write-protected (since 5.13) (see > :ref:`Documentation/admin-guide/mm/userfaultfd.rst `) > - * Bits 57-60 zero > + * Bit 58 page is a huge (PMD size) THP mapping > + * Bits 59-60 zero > * Bit 61 page is file-page or shared-anon (since 3.5) > * Bit 62 page swapped > * Bit 63 page present > diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c > index ad667dbc96f5c..e10b59064c0b9 100644 > --- a/fs/proc/task_mmu.c > +++ b/fs/proc/task_mmu.c > @@ -1302,6 +1302,7 @@ struct pagemapread { > #define PM_SOFT_DIRTY BIT_ULL(55) > #define PM_MMAP_EXCLUSIVE BIT_ULL(56) > #define PM_UFFD_WP BIT_ULL(57) > +#define PM_HUGE_THP_MAPPING BIT_ULL(58) > #define PM_FILE BIT_ULL(61) > #define PM_SWAP BIT_ULL(62) > #define PM_PRESENT BIT_ULL(63) > @@ -1409,12 +1410,13 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end, > struct pagemapread *pm = walk->private; > spinlock_t *ptl; > pte_t *pte, *orig_pte; > + u64 flags = 0; > int err = 0; > > #ifdef CONFIG_TRANSPARENT_HUGEPAGE > ptl = pmd_trans_huge_lock(pmdp, vma); > if (ptl) { > - u64 flags = 0, frame = 0; > + u64 frame = 0; Sorry again, I just noticed moving flags above is not necessary. I'll upload v4 with a fix shortly. > pmd_t pmd = *pmdp; > struct page *page = NULL; > > @@ -1456,6 +1458,8 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end, > > if (page && page_mapcount(page) == 1) > flags |= PM_MMAP_EXCLUSIVE; > + if (page && is_transparent_hugepage(page)) > + flags |= PM_HUGE_THP_MAPPING; > > for (; addr != end; addr += PAGE_SIZE) { > pagemap_entry_t pme = make_pme(frame, flags); > diff --git a/tools/testing/selftests/vm/transhuge-stress.c b/tools/testing/selftests/vm/transhuge-stress.c > index fd7f1b4a96f94..7dce18981fff5 100644 > --- a/tools/testing/selftests/vm/transhuge-stress.c > +++ b/tools/testing/selftests/vm/transhuge-stress.c > @@ -16,6 +16,12 @@ > #include > #include > > +/* > + * We can use /proc/pid/pagemap to detect whether the kernel was able to find > + * hugepages or no. This can be very noisy, so is disabled by default. > + */ > +#define NO_DETECT_HUGEPAGES > + > #define PAGE_SHIFT 12 > #define HPAGE_SHIFT 21 > > @@ -23,6 +29,7 @@ > #define HPAGE_SIZE (1 << HPAGE_SHIFT) > > #define PAGEMAP_PRESENT(ent) (((ent) & (1ull << 63)) != 0) > +#define PAGEMAP_THP(ent) (((ent) & (1ull << 58)) != 0) > #define PAGEMAP_PFN(ent) ((ent) & ((1ull << 55) - 1)) > > int pagemap_fd; > @@ -47,10 +54,16 @@ int64_t allocate_transhuge(void *ptr) > (uintptr_t)ptr >> (PAGE_SHIFT - 3)) != sizeof(ent)) > err(2, "read pagemap"); > > - if (PAGEMAP_PRESENT(ent[0]) && PAGEMAP_PRESENT(ent[1]) && > - PAGEMAP_PFN(ent[0]) + 1 == PAGEMAP_PFN(ent[1]) && > - !(PAGEMAP_PFN(ent[0]) & ((1 << (HPAGE_SHIFT - PAGE_SHIFT)) - 1))) > - return PAGEMAP_PFN(ent[0]); > + if (PAGEMAP_PRESENT(ent[0]) && PAGEMAP_PRESENT(ent[1])) { > +#ifndef NO_DETECT_HUGEPAGES > + if (!PAGEMAP_THP(ent[0])) > + fprintf(stderr, "WARNING: detected non THP page\n"); > +#endif > + if (PAGEMAP_PFN(ent[0]) + 1 == PAGEMAP_PFN(ent[1]) && > + !(PAGEMAP_PFN(ent[0]) & > + ((1 << (HPAGE_SHIFT - PAGE_SHIFT)) - 1))) > + return PAGEMAP_PFN(ent[0]); > + } > > return -1; > } > -- > 2.34.0.rc0.344.g81b53c2807-goog