Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp5817005pxb; Sun, 7 Nov 2021 21:11:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJxjQSt+8pSip7W041cz3zR/YxtBqd0vU0uy5lUs9xMJZ4zKtgwza6kReEWP4ROSuCDEaivW X-Received: by 2002:a17:906:8051:: with SMTP id x17mr33528195ejw.473.1636348316524; Sun, 07 Nov 2021 21:11:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1636348316; cv=none; d=google.com; s=arc-20160816; b=CsOFMyKegMBpew8XtjOsCuLtp6BOhFsVCBjqPQhTTs/tA/86e/yByQ8+GhP6krHt7z QHPT3kNqam1xRP+Ql8Rfbbpx22j9ixk8UUKXbvzEUeefxv8/C2sv17ZM5IkXc9D6sijQ Z/D6v327Dqknx6/RSah3FA5avOqVT6cJjyY/wv8rz8nBzP70t5pv6cLV6wkYsnogF5JQ 7zvNEusHCqP5qaWNyhKP1J4OR5jsCFm4a+Qd6uRyLg19Sox8p2zg1Pqqon/vMh1v0uqI u1ooi2JoiHy0SJ/MA0nY1wkzqSmSlRHavMC5t1A3jk40JgiVi36tjrZMym1hx7Tc6GC/ ldOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:cc:from:subject:mime-version:message-id:date :dkim-signature; bh=R+t/urldu9YHqL+lDHl0HSLrbce7ecQGyIl+/EPH9I8=; b=rNEljkN6rfxWPRPC6DyMTccuQvs6JYzyUc34e6nIN0rsdaeR4UAtqM5BA0yiSOQcMh lZ7SHr2uibQrnYtsrTCMLBUZIv2LBS983lr8FQtEZ7+OTtHyF+bOj04fkQZm/xGQHXzI t0gesKtdpi7u7oykmfDn0J9SgzFpTA9fTcsbB2x00qmt014gbUHmiHm7/zH70LZgFqkH +XScAbKAbbJkKVL+7eC14xtfzE4IGwfxfrSKnOr4ZGTMUomlsALBzZ2hya35e/zsQQ+R G6syJp+mud/3HHhW/ixr87Sm1bGrRTlQEd0aZ2EnwbBtFvLm9qJgV0KrCe1OnogPzH87 /dhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=jtvTOQvO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jy1si21586137ejc.592.2021.11.07.21.11.33; Sun, 07 Nov 2021 21:11:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=jtvTOQvO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236127AbhKHAAm (ORCPT + 99 others); Sun, 7 Nov 2021 19:00:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236078AbhKHAAl (ORCPT ); Sun, 7 Nov 2021 19:00:41 -0500 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39FB6C061714 for ; Sun, 7 Nov 2021 15:57:58 -0800 (PST) Received: by mail-pg1-x549.google.com with SMTP id z4-20020a634c04000000b00299bdd9abdbso9023329pga.13 for ; Sun, 07 Nov 2021 15:57:58 -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=R+t/urldu9YHqL+lDHl0HSLrbce7ecQGyIl+/EPH9I8=; b=jtvTOQvOVdGNvyQyeQqOgGXvjQRC942kwerWX6M5FsjL3Tf2acXkUvzNi3m/soosln smuise7b6YoEQSDMWni5RrgsRkFHhCkqDkNg/SvV2OrqbcUp89NTGHyz9K3zMkml9h8C H2FfEfYy8l1yITzXaM5pqtHa+/VgyFAP57qmtbkovAq/FbBzve5NMVOD0aeba8x4rKyf w5sBJ5g/iG7tk815O9LKpRjk0SZdl8e7vfYWFMybz0XoV7TCIsdNqjcKcLv3ChUSt6Op ftNIT54rBT3hSlbv8PxpftPfFCmvpmlYscqi3G7h6BTnfPp/UC/ZGZJxL78o0aupiqgV BpcA== 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=R+t/urldu9YHqL+lDHl0HSLrbce7ecQGyIl+/EPH9I8=; b=uCOKdNIO4DudGS6Kt515nzhRVXM+VDuNg+lXeAOujigOy5q0s7l31WOQRtKHi2UM0J 32eO/XyjQ4us17QCuxuVL+2z3z16jbw3peB0oDl86CFmWaU6m5y7tScBFNxONE3aC19q 9mllMAIXWnF+o+idsk9osW+21W0lBWDjLqfmJAt4JO/jpQrqbKLdJaJGkewPJHi6IOpI vxh2uUZCYIAYkEYb5nhMHJYWKSmw4f5y/lLRNbPXgwwuxMM5bd36yBYVKr8UoVUAOGoI dnk8oKIXgh7LBBpQn3VBDmIikOLl3Hy0ClPLkKvrildfYiBwOSEH2HDJli/n7vbj1dnV DGFQ== X-Gm-Message-State: AOAM532zidHLv2N4U5Lvclr+f3iN1XDT1ysURmWNJtzflV5SRSFyVUJX +Qds9yB5e+lnq2s8U7qIiY1Diz7tpIzTziB2iA== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2cd:202:86c8:d4b:a94b:a8fe]) (user=almasrymina job=sendgmr) by 2002:a63:4847:: with SMTP id x7mr47288917pgk.239.1636329477365; Sun, 07 Nov 2021 15:57:57 -0800 (PST) Date: Sun, 7 Nov 2021 15:57:54 -0800 Message-Id: <20211107235754.1395488-1-almasrymina@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.34.0.rc0.344.g81b53c2807-goog Subject: [PATCH v4] 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 Content-Type: text/plain; charset="UTF-8" To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- Changes in v4: - Removed unnecessary moving of flags variable declaration Changes in v3: - Renamed PM_THP to PM_HUGE_THP_MAPPING - Fixed checks to set PM_HUGE_THP_MAPPING - Added PM_HUGE_THP_MAPPING docs --- Documentation/admin-guide/mm/pagemap.rst | 3 ++- fs/proc/task_mmu.c | 3 +++ tools/testing/selftests/vm/transhuge-stress.c | 21 +++++++++++++++---- 3 files changed, 22 insertions(+), 5 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..6f1403f83b310 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) @@ -1456,6 +1457,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