From patchwork Sun Nov 7 22:59:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mina Almasry X-Patchwork-Id: 12607095 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 33C81C4332F for ; Sun, 7 Nov 2021 22:59:53 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E1A8761406 for ; Sun, 7 Nov 2021 22:59:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org E1A8761406 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 549416B00B5; Sun, 7 Nov 2021 17:59:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4F8EE6B00B6; Sun, 7 Nov 2021 17:59:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E7376B00B7; Sun, 7 Nov 2021 17:59:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0030.hostedemail.com [216.40.44.30]) by kanga.kvack.org (Postfix) with ESMTP id 2CA946B00B5 for ; Sun, 7 Nov 2021 17:59:52 -0500 (EST) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id E47BA8249980 for ; Sun, 7 Nov 2021 22:59:51 +0000 (UTC) X-FDA: 78783653382.06.F5C8C5F Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf14.hostedemail.com (Postfix) with ESMTP id 8252B6001E63 for ; Sun, 7 Nov 2021 22:59:52 +0000 (UTC) Received: by mail-pj1-f73.google.com with SMTP id p8-20020a17090a748800b001a6cceee8afso3640763pjk.4 for ; Sun, 07 Nov 2021 14:59:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:cc; bh=RGyd5fiyOhy5GGKEzKJh8xi/Umvf6q20k85EJTXajbk=; b=chNygS8CqeKmDlPQFveSwxc2tvI/R+tMYlS4e00i48Ehiwsc1GLRsWF3YTYTWutBct mGuSqWFklUyiydxgheXtB34yv9IXiDAIGcB2HvldMCrPVt9nEKXS9R4rsz+FMUOIOW6f TklNTnd4e+sUSjhuYdoNP+54QWjdy5msxUUx0kHxhBUQvYHIObvM5YQKu/N1/WVn/Dgk Bg+DzJKwqVDpmOFl4v5lkqR1EEBcasQTHmgZz8Fy26IhJjbN+l8+xaAEZIIiwtfyRsQc sjOU7+ciZfCSb1sCW3vYp0GxGHwjrw7l3NcKbdyhlbZeoIdToPjGRO4NvBAv/P3UqlxC +s1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:cc; bh=RGyd5fiyOhy5GGKEzKJh8xi/Umvf6q20k85EJTXajbk=; b=W2mbsovLYkPJGmerQsQxTBATprME9K682N7R9IQMvpY6vNgWgtxrnJp+AbAZXeyN8G gg8L6rXbZCMGwDto+VLkaKnkGThyeE6f1Jk6qil03JMCEME648x8mczMx8CFwuVcun15 anzGIk0jCKDKABcK2DkXft8uK8TNti1wRjfmE5MraQNL4YaoUcavDx1QWD/v/zSLpgoj zOhjKGdgqEAMXcZzbbB7D9F8DLTMW3g5YPYIV5yHvv1MGH5icqUbRCSL7+I6HCcDf6WI ItfqVBOgKsTn+3ANCfKhU3E3gdhURdYLFg4Oz+bgy0DIxALjzhMRGB5GARiITwUkDwRk pSDg== X-Gm-Message-State: AOAM531FmypKmK0mr/itymMpTtiFuCX32H+K5p/9UX9YVqoDmgK3Bxan fQDNi3cu6CCfgO17E4zZUz8ItA/vaKIz9riS5w== X-Google-Smtp-Source: ABdhPJwt5nUAcpy2chlqCQQlPa8udRSH/MwnDkgO0xpWaRUaU1F1uUiexJxGrX23pi143MUAQRLdVrDx6o2gteVIZg== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2cd:202:86c8:d4b:a94b:a8fe]) (user=almasrymina job=sendgmr) by 2002:a17:902:e88f:b0:141:f982:777 with SMTP id w15-20020a170902e88f00b00141f9820777mr44864402plg.68.1636325990117; Sun, 07 Nov 2021 14:59:50 -0800 (PST) Date: Sun, 7 Nov 2021 14:59:47 -0800 Message-Id: <20211107225947.1287388-1-almasrymina@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.34.0.rc0.344.g81b53c2807-goog Subject: [PATCH v3] mm: Add PM_HUGE_THP_MAPPING to /proc/pid/pagemap From: Mina Almasry Cc: Mina Almasry , 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 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 8252B6001E63 X-Stat-Signature: bsr7i534gnjchf6e33p5niieycjry7oa Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=chNygS8C; spf=pass (imf14.hostedemail.com: domain of 3ZlqIYQsKCE8r23r98F3z4rx55x2v.t532z4BE-331Crt1.58x@flex--almasrymina.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3ZlqIYQsKCE8r23r98F3z4rx55x2v.t532z4BE-331Crt1.58x@flex--almasrymina.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-HE-Tag: 1636325992-889979 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: 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(-) -- 2.34.0.rc0.344.g81b53c2807-goog 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; 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; }