From patchwork Thu Nov 4 21:46:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mina Almasry X-Patchwork-Id: 12603821 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 525B7C433EF for ; Thu, 4 Nov 2021 21:46:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E8F7D61213 for ; Thu, 4 Nov 2021 21:46:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org E8F7D61213 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 50C2C6B0071; Thu, 4 Nov 2021 17:46:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4BAC56B0072; Thu, 4 Nov 2021 17:46:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3AAA66B0073; Thu, 4 Nov 2021 17:46:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0166.hostedemail.com [216.40.44.166]) by kanga.kvack.org (Postfix) with ESMTP id 2DC666B0071 for ; Thu, 4 Nov 2021 17:46:41 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id F029F5C0B0 for ; Thu, 4 Nov 2021 21:46:40 +0000 (UTC) X-FDA: 78772582560.20.BE25760 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf01.hostedemail.com (Postfix) with ESMTP id 4ACBA508F101 for ; Thu, 4 Nov 2021 21:46:29 +0000 (UTC) Received: by mail-pl1-f202.google.com with SMTP id e10-20020a17090301ca00b00141fbe2569dso4056407plh.14 for ; Thu, 04 Nov 2021 14:46:40 -0700 (PDT) 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=XbB9YGB9sL1l4LUP+NB0RzevoG1AS/xVDDgvda7S2FE=; b=BahDfqNybrfQMKv8Fw5uQzouGUKWqfI06TTAjlzVF2Fm2QObOgA/NEHhVX6t9kjXLN qDvLqdTvtdMNXn1yxtSpnDtJdFKcnai60aGboMQT00XA2MKropwwnQjkVc1V/EasZcSK EEu9w87obvlAF8GLNZ89A4+3wFzjWABnOtcnScRq5d/dQ8AQUi+56Mr7XzsC3ONOtblG 2+LUDnEpFNPYUAMTE7qi7EcG2D+vU+VZs38He8kjL+CayZ1lr93fq6BYVoc5YFgwccUi EPpmXaW+2qEIFxuNrFyJoFc8IEgIw1zAkp7/1RNiJ42luHYRiIN3E3N13bd4C5VqQTeC LG9w== 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=XbB9YGB9sL1l4LUP+NB0RzevoG1AS/xVDDgvda7S2FE=; b=yO+sxdZZT063xWrYvLbnwgYJVppfLdQ9uFD5lPFkEGSvjCcl+3/8U3mvYzExTqZg30 CUkGtYwfeSQUm1vn8NlbvAMJWyhABzLUZbmLPwSOXDkcrO9mhww8OxbIjn8h486gSGJw DlnK6CE32GMKiP6fwFE7+G3PLZVDqBVn8d8iHks9vyzj8uB0BZEyB01XGXoODrHAU0xs HrJt0w2c2mbh9VJ7rvdcQIFgHbpKsyaUMb2KvjdHc7vpvmhbd1mjU7nOT1VLY0emLcYw QXXXYXq2KpuzjeglqTt5r1xqHYVCRHPjK2txyUmpBr9nQK9fyDmxRfnAL6b4D6VFVU+A TekA== X-Gm-Message-State: AOAM533Y5vex5zl+Ll5AskNdqhEr3VkAq4V7nRMwHb25BbrXrFPy4LUn UFPYY+/tFP7tLV6JKg7CItrm+vj7DwaJWgj6VA== X-Google-Smtp-Source: ABdhPJw6ni3y7lAn9ZTVUyYmmDy/A/Is/Ee5sb+M3gLrJ9xBRI6Ytbksgo2nfO2mKbKzDvU+YDRQiHxtyDQGlDcgeA== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2cd:202:a75b:b6d9:b323:af05]) (user=almasrymina job=sendgmr) by 2002:aa7:914f:0:b0:44d:6f5e:f11a with SMTP id 15-20020aa7914f000000b0044d6f5ef11amr54109373pfi.10.1636062399468; Thu, 04 Nov 2021 14:46:39 -0700 (PDT) Date: Thu, 4 Nov 2021 14:46:35 -0700 Message-Id: <20211104214636.450782-1-almasrymina@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.34.0.rc0.344.g81b53c2807-goog Subject: [PATCH v2] mm: Add PM_THP to /proc/pid/pagemap From: Mina Almasry Cc: Mina Almasry , "Paul E . McKenney" , Yu Zhao , Jonathan Corbet , Andrew Morton , Peter Xu , Ivan Teterevkov , David Hildenbrand , Matthew Wilcox , Florian Schmidt , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 4ACBA508F101 X-Stat-Signature: dufzf6sbmjwj83f3rhoymqfxipmkwexr Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=BahDfqNy; spf=pass (imf01.hostedemail.com: domain of 3v1SEYQsKCIwq12q87E2y3qw44w1u.s421y3AD-220Bqs0.47w@flex--almasrymina.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3v1SEYQsKCIwq12q87E2y3qw44w1u.s421y3AD-220Bqs0.47w@flex--almasrymina.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-HE-Tag: 1636062389-373566 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_THP to allow userspace to detect whether a given virt address is currently mapped by a hugepage or not. Example use case is a process requesting hugepages 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. Tested manually by adding logging into transhuge-stress. Signed-off-by: Mina Almasry 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: David Hildenbrand Cc: Matthew Wilcox Cc: Florian Schmidt Cc: linux-kernel@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org Cc: linux-mm@kvack.org --- fs/proc/task_mmu.c | 5 +++++ tools/testing/selftests/vm/transhuge-stress.c | 21 +++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) -- 2.34.0.rc0.344.g81b53c2807-goog diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index ad667dbc96f5c..9847514937fc7 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_THP BIT_ULL(58) #define PM_FILE BIT_ULL(61) #define PM_SWAP BIT_ULL(62) #define PM_PRESENT BIT_ULL(63) @@ -1396,6 +1397,8 @@ static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm, flags |= PM_FILE; if (page && page_mapcount(page) == 1) flags |= PM_MMAP_EXCLUSIVE; + if (page && PageTransCompound(page)) + flags |= PM_THP; if (vma->vm_flags & VM_SOFTDIRTY) flags |= PM_SOFT_DIRTY; @@ -1456,6 +1459,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 && PageTransCompound(page)) + flags |= PM_THP; 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; }