From patchwork Thu Aug 3 14:32:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13340194 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 42197C001DF for ; Thu, 3 Aug 2023 14:32:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 999D4280265; Thu, 3 Aug 2023 10:32:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 949EA28022C; Thu, 3 Aug 2023 10:32:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 81235280265; Thu, 3 Aug 2023 10:32:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 7383628022C for ; Thu, 3 Aug 2023 10:32:27 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id BB90B1611AB for ; Thu, 3 Aug 2023 14:32:26 +0000 (UTC) X-FDA: 81083033892.09.B319013 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf01.hostedemail.com (Postfix) with ESMTP id 32C2640031 for ; Thu, 3 Aug 2023 14:32:21 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TS2QemCL; spf=pass (imf01.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691073142; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=T4mj2S0TKyKcoVChRGDCiWswFcEXvTDuEU8gIwC6K6o=; b=5FKGDcpO0Jc0AEUe12ARN6nzviHbfW5Wv/khVRyUZiQAuBBa278hHcpyGyMJtI8G2TPRMY kCHbQrLlZMFQwElGKYZUYb7QZaJjCdBEdkQX7/pfnOgGLTS9cpQckKJiryDE19JXq7hNKk T5BYLxEb/YzSLm8pDoMZ/vSQSvA9la0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691073142; a=rsa-sha256; cv=none; b=yZG3XlBzBdQ8f57rkGhYKKuWWSUFirmKMm2SHRLi3vKvfuDDdE+3mOCq4fJ5E5+0uMDWlb QcTdsdJJVc+1QyHkWX0WW9ullhvVHLX/F0o+qlrC8yMCX27vu4KGjZihI2S0afLFoF18ib 8Ks+FkdOZZH3VqLDvoic21SZz+ocOEM= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TS2QemCL; spf=pass (imf01.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691073141; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=T4mj2S0TKyKcoVChRGDCiWswFcEXvTDuEU8gIwC6K6o=; b=TS2QemCLXeSFW7f8Imemx6PRIpZPaJbFhvWGanTb4F/IBMAlk/nwToEXimvQeKGGMILUNz 1bHAsmQO7/B4MAm0zJqs5QVSdPgKQB8FyepFSp1zlO93D7df4YcEgLOuXK+R2206bXJZ0e fpyn2WKVIE1yIb5GdjWa4HxkWZEc7+E= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-466-HCWQv9YRPgScaImvEsTDUw-1; Thu, 03 Aug 2023 10:32:18 -0400 X-MC-Unique: HCWQv9YRPgScaImvEsTDUw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 262F5891F20; Thu, 3 Aug 2023 14:32:17 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.129]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6F931200B66C; Thu, 3 Aug 2023 14:32:13 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, David Hildenbrand , Andrew Morton , Linus Torvalds , liubo , Peter Xu , Matthew Wilcox , Hugh Dickins , Jason Gunthorpe , John Hubbard , Mel Gorman , Shuah Khan , Paolo Bonzini , Mel Gorman , stable@vger.kernel.org Subject: [PATCH v3 1/7] mm/gup: reintroduce FOLL_NUMA as FOLL_HONOR_NUMA_FAULT Date: Thu, 3 Aug 2023 16:32:02 +0200 Message-ID: <20230803143208.383663-2-david@redhat.com> In-Reply-To: <20230803143208.383663-1-david@redhat.com> References: <20230803143208.383663-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Rspamd-Queue-Id: 32C2640031 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: tbfni1uowbumrmreozy6piroqfsb6gmp X-HE-Tag: 1691073141-442768 X-HE-Meta: U2FsdGVkX1/+KmV1b7z8B1g+19axWzTrOB+q9n7ONvZB98jkQailUocrkCO+fJjJ3B6hS2tMHLbcGc5swMdU6viR6WPTxLlfIHpLYUfMm5qudLdJ+plkBUUaL3fAMUx7rtl8eM2eW5LUxxjwWp5ZsHAwG1tr607PjyiP+nLZZGScEmFJPhP74mYiGx0Fm96Ct7z3Ivbjzko+q8/Fj/UESi2La0+ixq8uBI/NzbFJ8MSD2E+aZc2KH7gBxFgKVByKFb3J5DOyVjSwh9mgz3dL/7x1Zf2bRS+oBjtl+/ft0+hrEk+cSm0ys55Z8+Ht+GJzjyLMumULbHxCQg8pn3jTtXscxMDFzlKl7PcZMokbSrXupApH3yoASBnw9qKGuaKW0DAhjlwk9bTpl/E9aY/rJ3KbJilX58m4lo/7D18WSVNjx9GybQH2FNiWNrZP5vsuMeuk/y361GS0O6/y4xl1PGtCItaAk3+9VIh2f/EMiuSOBKaGnamsa4am6ax94196W5KeCpukPd0GT6a6oUEAapUhIIyvvLKfnMaLaM3dnjPkNioY6TKMDjQXqYWiisWeGbqtv1RAnBeKSJT3Dh1FK8acKpW8nnSTayNBQF+Vpsi9iv/azxghi5Zytd+2xHnVr8tu4JNA83tlYnzMcUtzUWHLeAyHzZ/IeO64oEByO9/04HuQ0cliScp7gXMU2YNWZzTKq2fCPAdWLa9RYCS2K8fJR/8MR1imDNgxOzkEjujYFZMM/3cw2GQd2gLZ48imV72sN2808EDyW/SppIy9ExRHY9UOGDuir//2Bj0ev2YO7lXltpx1K6Zp17PH9nwokY2W1GtlLC2FvbVbrlO0ypDWVkjwkXi+fgPm0FTGOtlJP0Tn/lBhOiv6iE9p+MdDhCGzpYFQcktSSlNwnbAqSO6Hv4NQArO720aXgcMPKTGxTbgKVQTr92HJBRoh8oVSBf6lOpCXd/czkopPmxc Js4m2QRo f7QsY/jR8DZLfcOcJHmnjrv+QIVuel6R4b3Cjzhq4h0+uPlI7RcrH0eK84MRAAgnWLlJu6dv7iqquIXglyWjqcQrquXg+qw+/D0G7ocFBmrpQ2dyv3PKNW6aL07hlWCAFfX0T1+xQXXIP1fQf5Pq8XyRUm+YCnaLcoX/i+Rpuq67RvGcfHjqJLGfFeHiqaGHYwso/iAbXkrTynVIuDuLbWJXQRNGHOAOoTL+mt0ADrLq+YDj17PY/Ef62CAuF8t5xi1bmnVnUolm3OZY5yWiNrSB/PuU4n2QRg9dKaKwuBNpGAAP6SIDhN3M5CoVsO7XHqZHrpEBNXhktWskOC4tw2XQm1e9O0aMoHnAOM/TaILGpMI6i0MLA6qKylpa/lqrJnTvq 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: Unfortunately commit 474098edac26 ("mm/gup: replace FOLL_NUMA by gup_can_follow_protnone()") missed that follow_page() and follow_trans_huge_pmd() never implicitly set FOLL_NUMA because they really don't want to fail on PROT_NONE-mapped pages -- either due to NUMA hinting or due to inaccessible (PROT_NONE) VMAs. As spelled out in commit 0b9d705297b2 ("mm: numa: Support NUMA hinting page faults from gup/gup_fast"): "Other follow_page callers like KSM should not use FOLL_NUMA, or they would fail to get the pages if they use follow_page instead of get_user_pages." liubo reported [1] that smaps_rollup results are imprecise, because they miss accounting of pages that are mapped PROT_NONE. Further, it's easy to reproduce that KSM no longer works on inaccessible VMAs on x86-64, because pte_protnone()/pmd_protnone() also indictaes "true" in inaccessible VMAs, and follow_page() refuses to return such pages right now. As KVM really depends on these NUMA hinting faults, removing the pte_protnone()/pmd_protnone() handling in GUP code completely is not really an option. To fix the issues at hand, let's revive FOLL_NUMA as FOLL_HONOR_NUMA_FAULT to restore the original behavior for now and add better comments. Set FOLL_HONOR_NUMA_FAULT independent of FOLL_FORCE in is_valid_gup_args(), to add that flag for all external GUP users. Note that there are three GUP-internal __get_user_pages() users that don't end up calling is_valid_gup_args() and consequently won't get FOLL_HONOR_NUMA_FAULT set. 1) get_dump_page(): we really don't want to handle NUMA hinting faults. It specifies FOLL_FORCE and wouldn't have honored NUMA hinting faults already. 2) populate_vma_page_range(): we really don't want to handle NUMA hinting faults. It specifies FOLL_FORCE on accessible VMAs, so it wouldn't have honored NUMA hinting faults already. 3) faultin_vma_page_range(): we similarly don't want to handle NUMA hinting faults. To make the combination of FOLL_FORCE and FOLL_HONOR_NUMA_FAULT work in inaccessible VMAs properly, we have to perform VMA accessibility checks in gup_can_follow_protnone(). As GUP-fast should reject such pages either way in pte_access_permitted()/pmd_access_permitted() -- for example on x86-64 and arm64 that both implement pte_protnone() -- let's just always fallback to ordinary GUP when stumbling over pte_protnone()/pmd_protnone(). As Linus notes [2], honoring NUMA faults might only make sense for selected GUP users. So we should really see if we can instead let relevant GUP callers specify it manually, and not trigger NUMA hinting faults from GUP as default. Prepare for that by making FOLL_HONOR_NUMA_FAULT an external GUP flag and adding appropriate documenation. While at it, remove a stale comment from follow_trans_huge_pmd(): That comment for pmd_protnone() was added in commit 2b4847e73004 ("mm: numa: serialise parallel get_user_page against THP migration"), which noted: THP does not unmap pages due to a lack of support for migration entries at a PMD level. This allows races with get_user_pages Nowadays, we do have PMD migration entries, so the comment no longer applies. Let's drop it. [1] https://lore.kernel.org/r/20230726073409.631838-1-liubo254@huawei.com [2] https://lore.kernel.org/r/CAHk-=wgRiP_9X0rRdZKT8nhemZGNateMtb366t37d8-x7VRs=g@mail.gmail.com Reported-by: liubo Closes: https://lore.kernel.org/r/20230726073409.631838-1-liubo254@huawei.com Reported-by: Peter Xu Closes: https://lore.kernel.org/all/ZMKJjDaqZ7FW0jfe@x1n/ Fixes: 474098edac26 ("mm/gup: replace FOLL_NUMA by gup_can_follow_protnone()") Acked-by: Mel Gorman Acked-by: Peter Xu Cc: Signed-off-by: David Hildenbrand --- include/linux/mm.h | 21 +++++++++++++++------ include/linux/mm_types.h | 9 +++++++++ mm/gup.c | 30 ++++++++++++++++++++++++------ mm/huge_memory.c | 3 +-- 4 files changed, 49 insertions(+), 14 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 63edff994c32..ba38b78a1b84 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3525,15 +3525,24 @@ static inline int vm_fault_to_errno(vm_fault_t vm_fault, int foll_flags) * Indicates whether GUP can follow a PROT_NONE mapped page, or whether * a (NUMA hinting) fault is required. */ -static inline bool gup_can_follow_protnone(unsigned int flags) +static inline bool gup_can_follow_protnone(struct vm_area_struct *vma, + unsigned int flags) { /* - * FOLL_FORCE has to be able to make progress even if the VMA is - * inaccessible. Further, FOLL_FORCE access usually does not represent - * application behaviour and we should avoid triggering NUMA hinting - * faults. + * If callers don't want to honor NUMA hinting faults, no need to + * determine if we would actually have to trigger a NUMA hinting fault. */ - return flags & FOLL_FORCE; + if (!(flags & FOLL_HONOR_NUMA_FAULT)) + return true; + + /* + * NUMA hinting faults don't apply in inaccessible (PROT_NONE) VMAs. + * + * Requiring a fault here even for inaccessible VMAs would mean that + * FOLL_FORCE cannot make any progress, because handle_mm_fault() + * refuses to process NUMA hinting faults in inaccessible VMAs. + */ + return !vma_is_accessible(vma); } typedef int (*pte_fn_t)(pte_t *pte, unsigned long addr, void *data); diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index aae6af098031..291c05cacd48 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1278,6 +1278,15 @@ enum { FOLL_PCI_P2PDMA = 1 << 10, /* allow interrupts from generic signals */ FOLL_INTERRUPTIBLE = 1 << 11, + /* + * Always honor (trigger) NUMA hinting faults. + * + * FOLL_WRITE implicitly honors NUMA hinting faults because a + * PROT_NONE-mapped page is not writable (exceptions with FOLL_FORCE + * apply). get_user_pages_fast_only() always implicitly honors NUMA + * hinting faults. + */ + FOLL_HONOR_NUMA_FAULT = 1 << 12, /* See also internal only FOLL flags in mm/internal.h */ }; diff --git a/mm/gup.c b/mm/gup.c index 2493ffa10f4b..3bbfae411880 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -597,7 +597,7 @@ static struct page *follow_page_pte(struct vm_area_struct *vma, pte = ptep_get(ptep); if (!pte_present(pte)) goto no_page; - if (pte_protnone(pte) && !gup_can_follow_protnone(flags)) + if (pte_protnone(pte) && !gup_can_follow_protnone(vma, flags)) goto no_page; page = vm_normal_page(vma, address, pte); @@ -714,7 +714,7 @@ static struct page *follow_pmd_mask(struct vm_area_struct *vma, if (likely(!pmd_trans_huge(pmdval))) return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); - if (pmd_protnone(pmdval) && !gup_can_follow_protnone(flags)) + if (pmd_protnone(pmdval) && !gup_can_follow_protnone(vma, flags)) return no_page_table(vma, flags); ptl = pmd_lock(mm, pmd); @@ -844,6 +844,10 @@ struct page *follow_page(struct vm_area_struct *vma, unsigned long address, if (WARN_ON_ONCE(foll_flags & FOLL_PIN)) return NULL; + /* + * We never set FOLL_HONOR_NUMA_FAULT because callers don't expect + * to fail on PROT_NONE-mapped pages. + */ page = follow_page_mask(vma, address, foll_flags, &ctx); if (ctx.pgmap) put_dev_pagemap(ctx.pgmap); @@ -2240,6 +2244,13 @@ static bool is_valid_gup_args(struct page **pages, int *locked, gup_flags |= FOLL_UNLOCKABLE; } + /* + * For now, always trigger NUMA hinting faults. Some GUP users like + * KVM require the hint to be as the calling context of GUP is + * functionally similar to a memory reference from task context. + */ + gup_flags |= FOLL_HONOR_NUMA_FAULT; + /* FOLL_GET and FOLL_PIN are mutually exclusive. */ if (WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) == (FOLL_PIN | FOLL_GET))) @@ -2564,7 +2575,14 @@ static int gup_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr, struct page *page; struct folio *folio; - if (pte_protnone(pte) && !gup_can_follow_protnone(flags)) + /* + * Always fallback to ordinary GUP on PROT_NONE-mapped pages: + * pte_access_permitted() better should reject these pages + * either way: otherwise, GUP-fast might succeed in + * cases where ordinary GUP would fail due to VMA access + * permissions. + */ + if (pte_protnone(pte)) goto pte_unmap; if (!pte_access_permitted(pte, flags & FOLL_WRITE)) @@ -2983,8 +3001,8 @@ static int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr, unsigned lo if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) || pmd_devmap(pmd))) { - if (pmd_protnone(pmd) && - !gup_can_follow_protnone(flags)) + /* See gup_pte_range() */ + if (pmd_protnone(pmd)) return 0; if (!gup_huge_pmd(pmd, pmdp, addr, next, flags, @@ -3164,7 +3182,7 @@ static int internal_get_user_pages_fast(unsigned long start, if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM | FOLL_FORCE | FOLL_PIN | FOLL_GET | FOLL_FAST_ONLY | FOLL_NOFAULT | - FOLL_PCI_P2PDMA))) + FOLL_PCI_P2PDMA | FOLL_HONOR_NUMA_FAULT))) return -EINVAL; if (gup_flags & FOLL_PIN) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 2e2e8a24cc71..0b709d2c46c6 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1467,8 +1467,7 @@ struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd)) return ERR_PTR(-EFAULT); - /* Full NUMA hinting faults to serialise migration in fault paths */ - if (pmd_protnone(*pmd) && !gup_can_follow_protnone(flags)) + if (pmd_protnone(*pmd) && !gup_can_follow_protnone(vma, flags)) return NULL; if (!pmd_write(*pmd) && gup_must_unshare(vma, flags, page)) From patchwork Thu Aug 3 14:32:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13340196 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 82474C001DF for ; Thu, 3 Aug 2023 14:32:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1AAE5280267; Thu, 3 Aug 2023 10:32:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 133CE28022C; Thu, 3 Aug 2023 10:32:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 02320280267; Thu, 3 Aug 2023 10:32:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E1CA028022C for ; Thu, 3 Aug 2023 10:32:31 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 910B7C1277 for ; Thu, 3 Aug 2023 14:32:31 +0000 (UTC) X-FDA: 81083034102.24.ED26DE4 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf29.hostedemail.com (Postfix) with ESMTP id D1D5312001E for ; Thu, 3 Aug 2023 14:32:27 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=EroY80DP; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf29.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691073147; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=600QZ17leEQmsQh51j1lyegsEoUslkBK3kcIUQ4PSKE=; b=OxEfn461DxTfgy3jtXrwaAbRVqPUdrZmJd16oTN8F2wGmDnB0WX1T/7BJQ0Nup6szozSmu 8ypaIOE7Zixo0nG/UzlO2z05B+95ErvEOcBoDZBLgBuAThkf0um56cRH03yuUCtI9Nr7iH Y8APfDdP/386pVtqNA5K8iOm6ICP6Ig= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=EroY80DP; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf29.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691073147; a=rsa-sha256; cv=none; b=waTAN/h8LJ5pc6QtzvvOft/QQB1E6l7EHo9/01ZOFa4wqGXZ4yZwIOD4hE5KhAeVFQEwZa Lv6JxbVNnEERf0gM57C4rxE3FYOT16eX6t9jo8LdL4VQnwZ6Ar+rBQpbTq+6blKdxkmwRu w3cYE41aBd6p3LOu0zvhnCYNhlGHKcY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691073146; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=600QZ17leEQmsQh51j1lyegsEoUslkBK3kcIUQ4PSKE=; b=EroY80DPcitezN5pzfi0jlhOZAUDRJUhlKmNOw+tc4wbce6wv0g1sLIaxRsaX6pQV3prBJ eDieb4sSwtBbHk4TmUJeZ5NmL3XaNrMQjUCyu5+aXL6s9fRyES6ZmCBtbRL54Z6wE42nVT rsnnNOVIW6kH8EiPubR40CG2Pv2MXtU= Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-645-bQOAl-2OPU6ER20m81kljg-1; Thu, 03 Aug 2023 10:32:22 -0400 X-MC-Unique: bQOAl-2OPU6ER20m81kljg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6C5C23C0DDBF; Thu, 3 Aug 2023 14:32:20 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.129]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8658A200BA76; Thu, 3 Aug 2023 14:32:17 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, David Hildenbrand , Andrew Morton , Linus Torvalds , liubo , Peter Xu , Matthew Wilcox , Hugh Dickins , Jason Gunthorpe , John Hubbard , Mel Gorman , Shuah Khan , Paolo Bonzini , Mel Gorman Subject: [PATCH v3 2/7] smaps: use vm_normal_page_pmd() instead of follow_trans_huge_pmd() Date: Thu, 3 Aug 2023 16:32:03 +0200 Message-ID: <20230803143208.383663-3-david@redhat.com> In-Reply-To: <20230803143208.383663-1-david@redhat.com> References: <20230803143208.383663-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Rspamd-Queue-Id: D1D5312001E X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: mnwn9ecxotuwtaoxjhtwsqknnyerw7gm X-HE-Tag: 1691073147-42050 X-HE-Meta: U2FsdGVkX1/sDhzr5nGQmDBMdErRpVzwxa3PvlTXV2JVSZfntxJshv1z5wk+bnZi5qDe2OGfb/WvrFMMcv8ReCHpknwVykOjE1eAbl5DdkIxoYAoeV7VCRHEs3OX18q8dpYQYUyfBIoPWDO2li5YvW3mYjWB/U/Ns4vnr8nap+X34++gLgub7UrqBmyWSy7G/P9wU0dvmO3g79Ld2wPZ5KIyJ95w7IzsQkc6a/VPqFEunqGtugSolxaDiXCbStzEywHshwz5JRtqMjTwl6vs5HGbKRF08mg8KEgO3Yz+UiCLIfzIxDWvdBGXuyKm7hiVbiAY1ccNMAt7TzzoScUO2Gu2AJ81QRNEfFKwPuksdF+EFzZwdHazySBDx5xs3/W6CoG0PtSnMiPI1tMxFWTt1udWRVim6bD4hPPGr3/E+kCzXWQMuWAE4IeXgtbfw3uSoHOSlp0+X600W+3k8lDqgZe6CYRT1oWKeVe4ZaEmDUsVZF28ksapSXlNe2Jx/KfZ/bthdqc85Bl2NaxudZEKTexq0iLZ/rp4lu4Mmdw0I8I5t68m5f4MxYRH2zZOTSIUzrM09xMJ0hKY7jnmjPW1Tk/7IlKUPRy5kM0iKq/ML1Ej8wCfL7XKnpASDO3kLh3NlxJY4YShhmroORVHy5IhrGiVRMaKnijGIFTLOHiBXOL49E4BA+J1Wj+RxbIxBG5rYU7T9G+Z8DmozlFf9KQ94ZUdxrgzg90Oyns53Xz3iXXkQhKqKeB49FSX/g0KxIW9WWTxZQjvWap/3SJ90+0qso3MDGWg7srF/wuKM6eUzjJ1/68NMPyFvd2MYb/Pj9458pNYiVog/TVtqUSoXN1s+YWjyQDwJwr9feYpT7M5qI4sC+fFRyBWl/CjXR4hUpVWsV0ZUE9+0vIieR1YJAq50JmbNHJNMLJtO4+wYX7a+Uv2PEEQeG21+jygXSkyRMyavFDDM7dNyhs3WL0yIG8 u1G6dLD1 EPe5Ca7jYG1hXJsNW6tYlJ2OPpthZePkJDTFcBABhzYeXtScr3+KFbtVIWwjuXgPwXpaJ7eMf7x3dNwclqoaFDvmS1F9UyymKjejCSZwBkRz1onotJamc3ND83Vq45zwTMvxW+AHNJ9KRgLClbDIIIfqFSStdYe4Rjp9l2HqwHyeN7NuAVHY/LpbFAb+ya8lxNuJFVMax8xQ8CWwDNqylnQD6xFCONyI/yTuYH9bxzA99PPZ2MAYo23bQOtx4U6XssIpD0dzW8UE02H+9arArACSpa9JwoVGcUiZarrnlIcLZFoHnKS0Oj+wISw== 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: We shouldn't be using a GUP-internal helper if it can be avoided. Similar to smaps_pte_entry() that uses vm_normal_page(), let's use vm_normal_page_pmd() that similarly refuses to return the huge zeropage. In contrast to follow_trans_huge_pmd(), vm_normal_page_pmd(): (1) Will always return the head page, not a tail page of a THP. If we'd ever call smaps_account with a tail page while setting "compound = true", we could be in trouble, because smaps_account() would look at the memmap of unrelated pages. If we're unlucky, that memmap does not exist at all. Before we removed PG_doublemap, we could have triggered something similar as in commit 24d7275ce279 ("fs/proc: task_mmu.c: don't read mapcount for migration entry"). This can theoretically happen ever since commit ff9f47f6f00c ("mm: proc: smaps_rollup: do not stall write attempts on mmap_lock"): (a) We're in show_smaps_rollup() and processed a VMA (b) We release the mmap lock in show_smaps_rollup() because it is contended (c) We merged that VMA with another VMA (d) We collapsed a THP in that merged VMA at that position If the end address of the original VMA falls into the middle of a THP area, we would call smap_gather_stats() with a start address that falls into a PMD-mapped THP. It's probably very rare to trigger when not really forced. (2) Will succeed on a is_pci_p2pdma_page(), like vm_normal_page() Treat such PMDs here just like smaps_pte_entry() would treat such PTEs. If such pages would be anonymous, we most certainly would want to account them. (3) Will skip over pmd_devmap(), like vm_normal_page() for pte_devmap() As noted in vm_normal_page(), that is only for handling legacy ZONE_DEVICE pages. So just like smaps_pte_entry(), we'll now also ignore such PMD entries. Especially, follow_pmd_mask() never ends up calling follow_trans_huge_pmd() on pmd_devmap(). Instead it calls follow_devmap_pmd() -- which will fail if neither FOLL_GET nor FOLL_PIN is set. So skipping pmd_devmap() pages seems to be the right thing to do. (4) Will properly handle VM_MIXEDMAP/VM_PFNMAP, like vm_normal_page() We won't be returning a memmap that should be ignored by core-mm, or worse, a memmap that does not even exist. Note that while walk_page_range() will skip VM_PFNMAP mappings, walk_page_vma() won't. Most probably this case doesn't currently really happen on the PMD level, otherwise we'd already be able to trigger kernel crashes when reading smaps / smaps_rollup. So most probably only (1) is relevant in practice as of now, but could only cause trouble in extreme corner cases. Let's move follow_trans_huge_pmd() to mm/internal.h to discourage future reuse in wrong context. Fixes: ff9f47f6f00c ("mm: proc: smaps_rollup: do not stall write attempts on mmap_lock") Acked-by: Mel Gorman Signed-off-by: David Hildenbrand --- fs/proc/task_mmu.c | 3 +-- include/linux/huge_mm.h | 3 --- mm/internal.h | 7 +++++++ 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index bf25178ae66a..7a7d6e2e6a14 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -571,8 +571,7 @@ static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, bool migration = false; if (pmd_present(*pmd)) { - /* FOLL_DUMP will return -EFAULT on huge zero page */ - page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP); + page = vm_normal_page_pmd(vma, addr, *pmd); } else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) { swp_entry_t entry = pmd_to_swp_entry(*pmd); diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 20284387b841..e718dbe928ba 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -25,9 +25,6 @@ static inline void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud) #endif vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf); -struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, - unsigned long addr, pmd_t *pmd, - unsigned int flags); bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long next); int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, diff --git a/mm/internal.h b/mm/internal.h index 5a03bc4782a2..c94eda536c4c 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -941,6 +941,13 @@ int migrate_device_coherent_page(struct page *page); struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags); int __must_check try_grab_page(struct page *page, unsigned int flags); +/* + * mm/huge_memory.c + */ +struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, + unsigned long addr, pmd_t *pmd, + unsigned int flags); + enum { /* mark page accessed */ FOLL_TOUCH = 1 << 16, From patchwork Thu Aug 3 14:32:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13340197 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 238C7EB64DD for ; Thu, 3 Aug 2023 14:32:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B6DFC280268; Thu, 3 Aug 2023 10:32:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B1E9C28022C; Thu, 3 Aug 2023 10:32:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A0DCE280268; Thu, 3 Aug 2023 10:32:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 934F628022C for ; Thu, 3 Aug 2023 10:32:36 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 1F0D8141262 for ; Thu, 3 Aug 2023 14:32:36 +0000 (UTC) X-FDA: 81083034312.04.ADD0248 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf06.hostedemail.com (Postfix) with ESMTP id D3CDE18002E for ; Thu, 3 Aug 2023 14:32:30 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="b/IRGEJ/"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf06.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691073151; a=rsa-sha256; cv=none; b=D/K857e8+a0aeR7CFI29EyyxZsphsqOZjRyzMMv5B4ebB4ybzLgH9fNhq4OgsicUz/aIdq ysVN1T3epaEpU+ut8hYPO4Hhc41hB931OAtXIjhI4TGtYPc1YNtd5jndo1gfn/f0avLjuW naayi+No9Vz5f1VZ5dI/VVMVjU/wTBo= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="b/IRGEJ/"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf06.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691073151; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=k5L4YtRFm7cOLJR+l6dUREkXQfCqKIU8byjm44bC064=; b=4AGuSsfdvOGg5c6j7WoX0YpoB8p1USZEfNV4BYZd3KVA9eKLrjtcOCsNFgwq31boqDWF/7 SrFomAeg+LF4ZumJXDSldR+P75xd/WqSBw+hDgRAiH6o+iHqd41BTaHYhXRQV2c4Hb+GOk G6g8ePIq3uBwnCn4rmgx3YrwLvkxTMg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691073147; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=k5L4YtRFm7cOLJR+l6dUREkXQfCqKIU8byjm44bC064=; b=b/IRGEJ/B0x87ZmXPNAeWF02FOF4gDIZTGrRn9l/NrjAT+POZI68gKWXdB7+MFKhICiuEC lrjIUr2Czi1dYzKPoBeJ01I4Yi6I2MDk689pKlak4y652O0Clc003Ip6hecIp4J/aCJVRM +IPfsrvGYcn6+uUw43abKVONX1+AcmU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-318-PirLHtypPEKo3Vn7YCBa1g-1; Thu, 03 Aug 2023 10:32:24 -0400 X-MC-Unique: PirLHtypPEKo3Vn7YCBa1g-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 391A1104D516; Thu, 3 Aug 2023 14:32:23 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.129]) by smtp.corp.redhat.com (Postfix) with ESMTP id A451B200B66C; Thu, 3 Aug 2023 14:32:20 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, David Hildenbrand , Andrew Morton , Linus Torvalds , liubo , Peter Xu , Matthew Wilcox , Hugh Dickins , Jason Gunthorpe , John Hubbard , Mel Gorman , Shuah Khan , Paolo Bonzini Subject: [PATCH v3 3/7] kvm: explicitly set FOLL_HONOR_NUMA_FAULT in hva_to_pfn_slow() Date: Thu, 3 Aug 2023 16:32:04 +0200 Message-ID: <20230803143208.383663-4-david@redhat.com> In-Reply-To: <20230803143208.383663-1-david@redhat.com> References: <20230803143208.383663-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: D3CDE18002E X-Stat-Signature: cc69deeqridw5ngwy8wq7xd8fuijdh6d X-HE-Tag: 1691073150-367772 X-HE-Meta: U2FsdGVkX1+d0mtKaLzlEG78IONNk1trnp+xDjQfIbqawWHoOHRgeakW6aX5aKvztWlsDQuvLGBn7e1tk811J5zGF8CrOKSf/+Uc4kdRI7oaVCLrG+DrDSGvYdYamTf/tkraEJzUJzXeE4qUnPULllYZwiaem/xzoW4G7FojvtDys0RsbGhia7NyU3OMXnYwP8Kn6uQnU6vrmNDSEaFYozpHO7nPSo5zETyxhlJ83JMwIm4/hkqglWitc9vFcLToLPujJ3nclhHsg5jGui1HBfClJ+8JBFzTPDFUTRy5J7mDbLQKhvPoKbajo9HZW9IqaXrIEAjsgpODNp+lyUZufrp5Vtjk9KDxX99lDsKT9f3UzFKNWUVANYtZVmKnvw9SpKY3tkPDkPWboVd5x0R1gggyjihbuUlgZBcz25QGx2vnk8Iy8JLDHLdN/wAtUr6MCHXmiwebpwcMBZo42xR5jQ+HTDsUTfQoBIUfnQxZIpkT9Y4A45/xl57Uw+v1rSaqR4FcXy7F1VmL4HYiODY96pCTNHg4UGNM7eYgsP40wyOoUwxiFoaDGD1h3/I2W4rL85bngmOLOjdacUhLVILdVbXtNE+UTd+4+4POmBwx1seueV3qNcv/4gJcofdyr97pcf+bPA1pfQy3iuzPsTIc5ZFcsALnhGswVy+B3hCGQVJUAc63GyZBbHOaKntZ9Oi+W4hvnQkZPPtFCY8HPrCf7tiuHbTxiC253AO3ceXI83nhfEUTsAxmwCi7HPjg4T5/AzM7CzZZl7GlW+rM9UyMVOjdntyNehKg98gGM7ux4Jrs397P2HZIULieFvMkusAqB+G4Hprpsn0VqJdHBGrznsH0XpOEvEOCkTtbn0XPdt870aJLqilMI95acN885+7S4mkZpU3DiB0IeE1rBrumu92uaem0IbKC2VxpJsA5ILB8FUKpiRjxDG4oaXCYhVNg2j9AYsS1FfhUUCtTkEK vqn8Lk2n yglEe1em4/DRIj6ijZLqy1pBzOQLSwd4+KiVNJFKIj+PW6ieVPYAeecN+JubG9hW3CxDqKHL7LyoL6a0I04MWacN81dTpK8sxt2uN+4xkvwVanib/i7BXUPc+f/04vcLSgvaC0Xt3uJk0P2QBOrN5F+ajblJCwu2r6FdbrJKHTHv+qInLHgAh82hpxR9zw8qMbqipMPCBfiUvhWIdKulHFZDqXg== 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: KVM is *the* case we know that really wants to honor NUMA hinting falls. As we want to stop setting FOLL_HONOR_NUMA_FAULT implicitly, set FOLL_HONOR_NUMA_FAULT whenever we might obtain pages on behalf of a VCPU to map them into a secondary MMU, and add a comment why. Do that unconditionally in hva_to_pfn_slow() when calling get_user_pages_unlocked(). kvmppc_book3s_instantiate_page(), hva_to_pfn_fast() and gfn_to_page_many_atomic() are similarly used to map pages into a secondary MMU. However, FOLL_WRITE and get_user_page_fast_only() always implicitly honor NUMA hinting faults -- as documented for FOLL_HONOR_NUMA_FAULT -- so we can limit this change to a single location for now. Don't set it in check_user_page_hwpoison(), where we really only want to check if the mapped page is HW-poisoned. We won't set it for other KVM users of get_user_pages()/pin_user_pages() * arch/powerpc/kvm/book3s_64_mmu_hv.c: not used to map pages into a secondary MMU. * arch/powerpc/kvm/e500_mmu.c: only used on shared TLB pages with userspace * arch/s390/kvm/*: s390x only supports a single NUMA node either way * arch/x86/kvm/svm/sev.c: not used to map pages into a secondary MMU. This is a preparation for making FOLL_HONOR_NUMA_FAULT no longer implicitly be set by get_user_pages() and friends. Signed-off-by: David Hildenbrand --- virt/kvm/kvm_main.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index dfbaafbe3a00..6e4f2b81541e 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2517,7 +2517,18 @@ static bool hva_to_pfn_fast(unsigned long addr, bool write_fault, static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, bool interruptible, bool *writable, kvm_pfn_t *pfn) { - unsigned int flags = FOLL_HWPOISON; + /* + * When a VCPU accesses a page that is not mapped into the secondary + * MMU, we lookup the page using GUP to map it, so the guest VCPU can + * make progress. We always want to honor NUMA hinting faults in that + * case, because GUP usage corresponds to memory accesses from the VCPU. + * Otherwise, we'd not trigger NUMA hinting faults once a page is + * mapped into the secondary MMU and gets accessed by a VCPU. + * + * Note that get_user_page_fast_only() and FOLL_WRITE for now + * implicitly honor NUMA hinting faults and don't need this flag. + */ + unsigned int flags = FOLL_HWPOISON | FOLL_HONOR_NUMA_FAULT; struct page *page; int npages; From patchwork Thu Aug 3 14:32:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13340199 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 D4A9BC04A6A for ; Thu, 3 Aug 2023 14:32:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE00128026A; Thu, 3 Aug 2023 10:32:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D902F28022C; Thu, 3 Aug 2023 10:32:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C30CC28026A; Thu, 3 Aug 2023 10:32:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id AB2C828022C for ; Thu, 3 Aug 2023 10:32:40 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 27CC21A03C2 for ; Thu, 3 Aug 2023 14:32:39 +0000 (UTC) X-FDA: 81083034480.22.0C1C64F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf02.hostedemail.com (Postfix) with ESMTP id 13F378008F for ; Thu, 3 Aug 2023 14:32:32 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="cFQu/4Y6"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf02.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691073153; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Xlcu86mCEhwN5RlV1rQQu0rKBbZXpgB+gRX7zo+2ELE=; b=gHImPBtJtmfujB7N0TtUb5TcCyOjuyQJFr9mGSpYGHt79sZwgwjaxXCbJQFJpntrV8sibr TftJyNknBfsEfDUCzCZ2S53NQV0NUJPjHrns1g5WRt0pgc/q63Zo0YB0jETygLgmtLdqcu H+664il9XX8WKKKwVWIvVZD7Xs/kTJw= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="cFQu/4Y6"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf02.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691073153; a=rsa-sha256; cv=none; b=1HySR7YakNuSvafClhXgX1NnHkRwL2KR3IEwodKC7BlvrWhcH6HnaN4uHrrciOYIc1zKOW +255l3WcYQX0iZFZ42Uxt4aDiLMszU+4e05yT4kjc79OF70hGFioK5TgX4EXAqp+CF0S8i A+sPBHag7z91JChx+4xe1buyyxKo1JA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691073152; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Xlcu86mCEhwN5RlV1rQQu0rKBbZXpgB+gRX7zo+2ELE=; b=cFQu/4Y6U9s/Qr86W3jKrY7IwhwYua0hV3gTrlXTpn9IgMNfVfORLDhFo+xxYHQWShbKYu VNhZWB/emhscNEtre9KTeuD8oEyCTKHIUG9MNI7MsH90HBCNx36H6+MfFGcQlGnHaiq/E7 yi7Dy/bSlo9HD/qr3nVW4Y+2zGltolU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-194-xKca5kjkPeqZ-tCl_i7x9A-1; Thu, 03 Aug 2023 10:32:26 -0400 X-MC-Unique: xKca5kjkPeqZ-tCl_i7x9A-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0BE7B805AF6; Thu, 3 Aug 2023 14:32:26 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.129]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7401F201EE6E; Thu, 3 Aug 2023 14:32:23 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, David Hildenbrand , Andrew Morton , Linus Torvalds , liubo , Peter Xu , Matthew Wilcox , Hugh Dickins , Jason Gunthorpe , John Hubbard , Mel Gorman , Shuah Khan , Paolo Bonzini Subject: [PATCH v3 4/7] mm/gup: don't implicitly set FOLL_HONOR_NUMA_FAULT Date: Thu, 3 Aug 2023 16:32:05 +0200 Message-ID: <20230803143208.383663-5-david@redhat.com> In-Reply-To: <20230803143208.383663-1-david@redhat.com> References: <20230803143208.383663-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Rspamd-Queue-Id: 13F378008F X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: g31pm6pc5m4g3qnnszqyii6y5cmb14r7 X-HE-Tag: 1691073152-717937 X-HE-Meta: U2FsdGVkX1+Ha6WqjkFQCNYDO+pLOq7oaRxlnOljZ0AIN0qVBtLD5snr+VsLF6Nu9XZALvRgOi3oFJNwEuxLxYDUp3ql+kkuIgwcBD5tvvxhaV+kBROd1avws2dpmT22zsEc/jKebcnBYzoiToegECCBQxOypoCnVjYIl5P7SVti1NirBcLWkiEp0x4MtBwX6HpJOyMyXZFUu2hbmJfx0rFnKzboi9NR/5Mf7x8QXvavXF72ehOjVys0vQ9zCOOLoeP7m5mk8M9Lp0smx74F1kpkiFgdHgGaRsAmJYdZVYdJ9bEnPGAyMO6X8Xrv58SxZEYYpptV5ysArVH5PzP9fPROTq3HUDvuLehNDJGMG3cpZgUs5QokkzTDyfFU9NXJhBEQgJgvt278IoIrRziDFi/4SKe/wej1Yl5a86v2/4aINifzzJBKLOdt8/bhw/74c8Qd+4G6zQp5NQoF2jze5slyLMW35Uw1AD38ZONWveXI9D5jlGXZF4XsKjc8YxTOjqlReO3yd45hdJDa6s3vjs/Aq8hp/YODlFpb/6ajszHAlWdg53uCqxy8VnU8JRGJBlgnE9kGhw8/BLDlcJ69UDtIyL7h0qa5cC9D6Ga2XRUqQdCrVMQN2wwbHbYM/8dH140R1Ss5b29+J4luk78JVLKoFNX9P1go33efpDhNyCxwNjMxGfcZfhfH2YYZTuYCn8Hn+3LRx6CIqJ0qX22LsRF9Xlgyc8+/6WTaj3CPf8UxAfRtNF6I47BzTxzH/TPyeeo4WNkOmoLJjUCq8yKScTHomCaaMqH3mgjlTvmqCTcFKByyKXCzkt0yfIQtVHL+8Q0t29NzkaAnG9bMDlPEqGSi++As5FfGYKdArv8BXtAFhXpdjJoxS3iMy3O3kywSiyPD2f6gbX0MCPPmWNUdI9QuxsfJcuJOPwOsPDX7qTN9QrUC4vnuSBocu9D8wC06wgIOxK1ps3wDLuuaBJK j/qAt4Ny Fb0HchLhG25/xBSGZe/NKnkdOlA8SHcI3TyQkKVQgCJKewbm0TFa4XTwQkB/hdeiQZbOehlZgTnuSmOy/lY5ylTTfeaXIj2i2spv3vcq1ImQ7aeUaqjiWkZBJzCsOdV4k7fzX/QA1GfHelg983QDxs8y/ESTT88RAyXWGKMeH/+p3rt+9/YxF+SRyeeK8WlW4EFRkvoHSqTrbSAD8u7SPNtSUKg== 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: Commit 0b9d705297b2 ("mm: numa: Support NUMA hinting page faults from gup/gup_fast") from 2012 documented as the primary reason why we would want to handle NUMA hinting faults from GUP: KVM secondary MMU page faults will trigger the NUMA hinting page faults through gup_fast -> get_user_pages -> follow_page -> handle_mm_fault. That is still the case today, and relevant KVM code has been converted to manually set FOLL_HONOR_NUMA_FAULT. So let's stop setting FOLL_HONOR_NUMA_FAULT for all GUP users and cross fingers that not that many other ones that really require such handling for autonuma remain. Possible interaction with MMU notifiers: Assume a driver obtains a page using get_user_pages() to map it into a secondary MMU, and uses the MMU notifier framework to get notified on changes. Assume get_user_pages() succeeded on a PROT_NONE-mapped page (because FOLL_HONOR_NUMA_FAULT is not set) in an accessible VMA and the page is mapped into a secondary MMU. Once user space would turn that mapping inaccessible using mprotect(PROT_NONE), the actual PTE in the page table might not change. If the MMU notifier would be smart and optimize for that case "why notify if the PTE didn't change", that could be problematic. At least change_pmd_range() with MMU_NOTIFY_PROTECTION_VMA for now does an unconditional mmu_notifier_invalidate_range_start() -> mmu_notifier_invalidate_range_end() and should be fine. Note that even if a PTE in an accessible VMA is pte_protnone(), the underlying page might be accessed by a secondary MMU that does not set FOLL_HONOR_NUMA_FAULT, and test_young() MMU notifiers would return "true". Signed-off-by: David Hildenbrand --- mm/gup.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 3bbfae411880..ee4fc15ce88e 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2244,13 +2244,6 @@ static bool is_valid_gup_args(struct page **pages, int *locked, gup_flags |= FOLL_UNLOCKABLE; } - /* - * For now, always trigger NUMA hinting faults. Some GUP users like - * KVM require the hint to be as the calling context of GUP is - * functionally similar to a memory reference from task context. - */ - gup_flags |= FOLL_HONOR_NUMA_FAULT; - /* FOLL_GET and FOLL_PIN are mutually exclusive. */ if (WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) == (FOLL_PIN | FOLL_GET))) From patchwork Thu Aug 3 14:32:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13340198 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 730FAEB64DD for ; Thu, 3 Aug 2023 14:32:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 04C63280269; Thu, 3 Aug 2023 10:32:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F3D9728022C; Thu, 3 Aug 2023 10:32:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E2C95280269; Thu, 3 Aug 2023 10:32:39 -0400 (EDT) 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 D34DE28022C for ; Thu, 3 Aug 2023 10:32:39 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9BCE64129A for ; Thu, 3 Aug 2023 14:32:39 +0000 (UTC) X-FDA: 81083034438.08.8A179CC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf30.hostedemail.com (Postfix) with ESMTP id 9D46580031 for ; Thu, 3 Aug 2023 14:32:35 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=HIt8oS7p; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf30.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691073157; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ymf0b+2Kqr9mLIxdkVxybggSGs/fK9hNLVEJOY2Qbdo=; b=b5YuIPsc31xBGcR/ibSsjryTn/RzUdTfrNgq+azAl9ZWpke4HrhjwgPeSow4xQ0XsVKDmB Qu8fF0+/ZVJhaKuZtB7GQUTa+jeQCb42NoVC+FBLkIgt9sPgo/e6IiU9JGxLqxvcI8TtSj uKvXEhdHsisFe9Xf6Mpb3v8GIvjAV8k= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=HIt8oS7p; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf30.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691073157; a=rsa-sha256; cv=none; b=qsSwt16TozaoYPqTxINP5AM8wAP4bTWfJfNBmHKzpTuh47t2aHiGRq+0n9lARBaOY4F4Rd xUR2k3Ilv+pyy3Zln+WNvNBIdVpJ/t4ZVpTMTtWBh5aL2ZJ3Au6sMnVNNPnmeSeEbR01Fx EteBFTul4U/S1/mxw02+RoAUPE2Hnys= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691073155; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ymf0b+2Kqr9mLIxdkVxybggSGs/fK9hNLVEJOY2Qbdo=; b=HIt8oS7pJVYEaVa4VuP3tEvW7/PfTGxOu6c8AVTWKnSSDoa8uv74fHMuwPI9UbvPe6q33Z U6xG01knbeglEweiKYo7LAz758BRgFZYFVWb4MFXkGmzr+Ebb2byZ1Zv9np7p+slka84ZX t3UfRx/DvqDdPgxq//2TNWI35XWGjWs= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-423-0ded6q7sOS-M6lJ3orix1Q-1; Thu, 03 Aug 2023 10:32:30 -0400 X-MC-Unique: 0ded6q7sOS-M6lJ3orix1Q-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3FD4C857FD1; Thu, 3 Aug 2023 14:32:29 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.129]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4653F201F11C; Thu, 3 Aug 2023 14:32:26 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, David Hildenbrand , Andrew Morton , Linus Torvalds , liubo , Peter Xu , Matthew Wilcox , Hugh Dickins , Jason Gunthorpe , John Hubbard , Mel Gorman , Shuah Khan , Paolo Bonzini , Mel Gorman Subject: [PATCH v3 5/7] pgtable: improve pte_protnone() comment Date: Thu, 3 Aug 2023 16:32:06 +0200 Message-ID: <20230803143208.383663-6-david@redhat.com> In-Reply-To: <20230803143208.383663-1-david@redhat.com> References: <20230803143208.383663-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 9D46580031 X-Stat-Signature: udcrsmuzrq73sptycd85m89yaxjng6oe X-HE-Tag: 1691073155-718587 X-HE-Meta: U2FsdGVkX1+TYGBm2xJjWdU9YViDfQOb1B0Ynp+8J4C9LqtyJmsSMgLwhiNrtmoZisAHMy7MaUskyNzlGGskE2J9XBrk5PYnZr7WhVfqof9SZx3f+mIn8z8ecNR6x416VONHlttPCmI+oIiyeluiMSwObAVqTJwhttk960suhj2K1Z5769wrd02gYM0hSifVHF77tRn84KbaRWMUDnz2WyndbEDWOTGZzTTGFhDBZe1Nv83vzAInND7DNDA5xyMpcloZs2gg/baRW/mfsNBxKgp0PwXW6dvkhRjf+uOjJWfgk4pPDmz+ObLHvt8m1rxze14W7TYf4meSNLN9hS7e+f+82lyngFK9Esz8NPHy9wuh74YpPsYUVlljUhmRvdWzR4PxmZ57Kkxn19HeTSpau4DomKjRbCpEOamdab6f3phlgsr/q7GQV61mNYw/hcUDq64yQgXQTdwn2aaKv6IYrsiAWAyN44hRPdhi3zts99tli8fOPWwBdzGb+Nb5gkXYu1e7QnOQuFfu6O+L/rdawmVKXqCM8cbJpzhi+dsVctlyU7Q3qq4bGtur2MnU/mCtN1JUCOMnxlX9lKEh03CPva3QYVO5s6vE4pLEvwPLC/UWq1BK4SswKfpK0BvGcwdn0ulyp2nID0IoNcUL2GNKftl7byZXFwbxXNCscOqHUPI1irhqnR7GDMlYHNaoyBJpx0VKz02x+8Mnc/FdmDYkex5CLxiMsJt1Z9mMaP2nAtkiHQUMqXZFYAOh/MZls2van0csuYEY1vxDoPT5zn7ftTUVasTgPJ1UY7YLYH68zO46Y+gOR2NunycUlLcl1BCCsfGJEFFqgKv5a1P0lpSYpWKCfR0womjregRhnD5XxBC9prjtx4AeBZCpstHHY8I4pTBmPqCqCI3jgxqPJLlfGpHfiW4/iNOdpTjRnx8q0JC2J1lgrY3XC+kyWKnicm8fKfwr+jShGW96J5PmRuA 5UUarM3T JD2hS7MCcGXC73hQyzzh8TLrdBT2D00dSOSrKPzdtRAXPfHyRt6LM9ouyRfxcJjE5J0iixEgu4I/mEpyPpFzYOG0xPL6FM2wu3TCp/Lo/QrhvRyu5ogW+sHmdq2kJ+WEAdAj/o6pAqcucGzy8tFVNVymbjyFu/UUSqlBO1I+qS7ZbUaw+MZsYCp2aUVLMCx/AKmF6wGa1ueoDQNdP8by3s6C4XSBNP/H51blwOObXed/vITDF6DyFKumXD5vSgkfIl2XKp0Efudwe39qQJAoKi5GXNg== 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: Especially the "For PROT_NONE VMAs, the PTEs are not marked _PAGE_PROTNONE" part is wrong: doing an mprotect(PROT_NONE) will end up marking all PTEs on x86_64 as _PAGE_PROTNONE, making pte_protnone() indicate "yes". So let's improve the comment, so it's easier to grasp which semantics pte_protnone() actually has. Acked-by: Mel Gorman Signed-off-by: David Hildenbrand --- include/linux/pgtable.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 6005b5dff0c1..222a33b9600d 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -1446,12 +1446,16 @@ static inline int pud_trans_unstable(pud_t *pud) #ifndef CONFIG_NUMA_BALANCING /* - * Technically a PTE can be PROTNONE even when not doing NUMA balancing but - * the only case the kernel cares is for NUMA balancing and is only ever set - * when the VMA is accessible. For PROT_NONE VMAs, the PTEs are not marked - * _PAGE_PROTNONE so by default, implement the helper as "always no". It - * is the responsibility of the caller to distinguish between PROT_NONE - * protections and NUMA hinting fault protections. + * In an inaccessible (PROT_NONE) VMA, pte_protnone() may indicate "yes". It is + * perfectly valid to indicate "no" in that case, which is why our default + * implementation defaults to "always no". + * + * In an accessible VMA, however, pte_protnone() reliably indicates PROT_NONE + * page protection due to NUMA hinting. NUMA hinting faults only apply in + * accessible VMAs. + * + * So, to reliably identify PROT_NONE PTEs that require a NUMA hinting fault, + * looking at the VMA accessibility is sufficient. */ static inline int pte_protnone(pte_t pte) { From patchwork Thu Aug 3 14:32:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13340200 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 26B44C001DF for ; Thu, 3 Aug 2023 14:32:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD23028026B; Thu, 3 Aug 2023 10:32:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A825A28022C; Thu, 3 Aug 2023 10:32:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 94BAD28026B; Thu, 3 Aug 2023 10:32:52 -0400 (EDT) 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 8353028022C for ; Thu, 3 Aug 2023 10:32:52 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id F2E31C12A9 for ; Thu, 3 Aug 2023 14:32:51 +0000 (UTC) X-FDA: 81083034942.21.2D40F64 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf14.hostedemail.com (Postfix) with ESMTP id 2AE3E100032 for ; Thu, 3 Aug 2023 14:32:35 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QqxBSzgB; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf14.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691073156; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xJsbOToPWAZq/BUuGCNQ1TQ79o7M/JDjOnzAfc0Kk/Q=; b=Fp29E8gyDecgJRxj2Srh6icbMmPy81RzcX6aLMARYwQdtTOOIyBOZtvz3OHi96oW1rJY+o pawPgX1o6YRQdes16ug1SmmKNYz5/5Y8VLqHhAvgRBL/wGeBTUerRGKd/GgbLC4D+zip+n 2GY0YUXByVgLq1ymP9FyBB2Z49GlZaM= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QqxBSzgB; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf14.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691073156; a=rsa-sha256; cv=none; b=C8iMRw7dJLQK/tM25qTb23NV0Gm3T5MvjX4gfTbsZq0oi4ilbW8tBdp5Oe1u3arWt4XcOB bQC3j9H51T7bbJnR1KwAuBR8+SkL5f/fJX8N7mOWWSbFwltBuw8kCaj1SaHVd6gGlOW9sY AEsE9cKo3sZgWUzUiwUetfShdKAHdno= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691073155; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xJsbOToPWAZq/BUuGCNQ1TQ79o7M/JDjOnzAfc0Kk/Q=; b=QqxBSzgBPLkKMOhrVKx6N9tWaKAzxcpLJ+5yw6qy6VW0wSNFE6TaTZnUnDuJ3GkJD+PCTd 92Q31oEW/jT2CWcRk1ZnSxZ8eSceGa2gnatewsvDj5hqrT8b97yWtWg2j0mJ24rA3JypYJ OTN1Rbcgu/at4XdDLEhulyIIZiBBBSw= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-367-OUJwVKCAPM2WrVMmmpFX1w-1; Thu, 03 Aug 2023 10:32:32 -0400 X-MC-Unique: OUJwVKCAPM2WrVMmmpFX1w-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1D1D5858290; Thu, 3 Aug 2023 14:32:32 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.129]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9AF2A200B66C; Thu, 3 Aug 2023 14:32:29 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, David Hildenbrand , Andrew Morton , Linus Torvalds , liubo , Peter Xu , Matthew Wilcox , Hugh Dickins , Jason Gunthorpe , John Hubbard , Mel Gorman , Shuah Khan , Paolo Bonzini Subject: [PATCH v3 6/7] selftest/mm: ksm_functional_tests: test in mmap_and_merge_range() if anything got merged Date: Thu, 3 Aug 2023 16:32:07 +0200 Message-ID: <20230803143208.383663-7-david@redhat.com> In-Reply-To: <20230803143208.383663-1-david@redhat.com> References: <20230803143208.383663-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Rspam-User: X-Stat-Signature: g76n6ni1wnwfzp79brmxwy8j6cdx8sux X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 2AE3E100032 X-HE-Tag: 1691073155-67822 X-HE-Meta: U2FsdGVkX19KccgAlqNVH4Zgg7iqlOUkwAOkabo0Gc8BgluITlxG7ialgLT7oieUg8gUdgdmLmCD5UjSYC+Y60ZqkblpMf/T2PsRNGtnqLTRBhwHXyZta0Kn7JATtCQTST+lldvm98mryPycyFSIAwGzftByw8DPdxIUoA2PzNzkXw2WFyB12gWsawsl575/AXd/w401AgDTgIR2617MawML1xDIgChN406VR0Vs4j0kOkfGR1v00C1R+eyd5kIMUS+3ML+IuD1A95c5OnCtnxKOzi2XtM4jiDdXPRgQzDRtgOYauGsD5cx++VAvVzT9cAqEKer52kJtLiSKLy1F72A3OU2PFPwt3OAlDZOh/PZv4gE8btIgmB1gqEqWqzWhcPPJnEip3WxoH4dJFZNMVze+THKnqHU1+GLu8s57xR5frr06XRgVg/JZi72zASHhSFxSXM/q+F9WKPiuE5bFz9hdeD/08mx6/+EjnQHzmxSXMuK7N+/cpmekeFmH9+mjNa28qFacT7jRnuZVjVmBphLCGOEnkInowx/b2niCPSnftp+I35oZ3LtdKHcOo4gLyNflnvfszSN+MVOS9n8EBRkJiOiSgEJZes8njPC/XgAleWIOZ7h2O+XK/sHqxKPJIZZ2h+nQrnFXBYnkgDF4RIIabYEYkv/AyVbDYx/Vzvc0WgMDwvng9AHbVS/N8D1Ypj98nET1wryqTQAAFuc43iq7YJcQ7Hd0V1lNsB55l0DiVgh23d6q+Zl6rGR6Vc7xhSIShHwnjev6IJfoTk4qh7FrMnoRP+SlLSIRf1kPTPnOWcftyDMv0JqAQ8DgZvNsL4WAgHtQwbm6BLZfmrQfInhaIjF1tH+0CZAhRBNSGd49FQ+gQNIPgvRUOgS3QKSiXx9FUTIHZS7F0i5eIXkLarcfXNWaLrBuez/GizTW0hNyxw4Qdm7mS3RqyHwE/Uy5+W6hrzLfTpb/0wf6BSn 0NayyVrT vVRPg3h8kY/wwOFy75v1VuT5x8gh8HCAZY57dm+mfMFrUbFQ6ExRvUyG1DpCpTpsRSLlGPlogF96fipU3IVMrlWt91DBClmOHObxGn31hdzfsfBabndvGGGth4xoil+rvCl7q/Mw3ac3WT5xydQgEdlvnC/YD83RKlZBAMcyqvpoj0W6CYbb9pYvR7wBFKAIrBmWmGCMLVA0368q3ekQgWKrhXA0lToEViIZf 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: Let's extend mmap_and_merge_range() to test if anything in the current process was merged. range_maps_duplicates() is too unreliable for that use case, so instead look at KSM stats. Trigger a complete unmerge first, to cleanup the stable tree and stabilize accounting of merged pages. Note that we're using /proc/self/ksm_merging_pages instead of /proc/self/ksm_stat, because that one is available in more existing kernels. If /proc/self/ksm_merging_pages can't be opened, we can't perform any checks and simply skip them. We have to special-case the shared zeropage for now. But the only user -- test_unmerge_zero_pages() -- performs its own merge checks. Signed-off-by: David Hildenbrand Acked-by: Peter Xu --- .../selftests/mm/ksm_functional_tests.c | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c index 0de9d33cd565..cb63b600cb4f 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -30,6 +30,7 @@ static int ksm_fd; static int ksm_full_scans_fd; static int proc_self_ksm_stat_fd; +static int proc_self_ksm_merging_pages_fd; static int ksm_use_zero_pages_fd; static int pagemap_fd; static size_t pagesize; @@ -88,6 +89,22 @@ static long get_my_ksm_zero_pages(void) return my_ksm_zero_pages; } +static long get_my_merging_pages(void) +{ + char buf[10]; + ssize_t ret; + + if (proc_self_ksm_merging_pages_fd < 0) + return proc_self_ksm_merging_pages_fd; + + ret = pread(proc_self_ksm_merging_pages_fd, buf, sizeof(buf) - 1, 0); + if (ret <= 0) + return -errno; + buf[ret] = 0; + + return strtol(buf, NULL, 10); +} + static long ksm_get_full_scans(void) { char buf[10]; @@ -120,11 +137,29 @@ static int ksm_merge(void) return 0; } +static int ksm_unmerge(void) +{ + if (write(ksm_fd, "2", 1) != 1) + return -errno; + return 0; +} + static char *mmap_and_merge_range(char val, unsigned long size, bool use_prctl) { char *map; int ret; + /* Stabilize accounting by disabling KSM completely. */ + if (ksm_unmerge()) { + ksft_test_result_fail("Disabling (unmerging) KSM failed\n"); + goto unmap; + } + + if (get_my_merging_pages() > 0) { + ksft_test_result_fail("Still pages merged\n"); + goto unmap; + } + map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); if (map == MAP_FAILED) { @@ -160,6 +195,16 @@ static char *mmap_and_merge_range(char val, unsigned long size, bool use_prctl) ksft_test_result_fail("Running KSM failed\n"); goto unmap; } + + /* + * Check if anything was merged at all. Ignore the zero page that is + * accounted differently (depending on kernel support). + */ + if (val && !get_my_merging_pages()) { + ksft_test_result_fail("No pages got merged\n"); + goto unmap; + } + return map; unmap: munmap(map, size); @@ -473,6 +518,8 @@ int main(int argc, char **argv) if (pagemap_fd < 0) ksft_exit_skip("open(\"/proc/self/pagemap\") failed\n"); proc_self_ksm_stat_fd = open("/proc/self/ksm_stat", O_RDONLY); + proc_self_ksm_merging_pages_fd = open("/proc/self/ksm_merging_pages", + O_RDONLY); ksm_use_zero_pages_fd = open("/sys/kernel/mm/ksm/use_zero_pages", O_RDWR); test_unmerge(); From patchwork Thu Aug 3 14:32:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13340201 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 F0B2CEB64DD for ; Thu, 3 Aug 2023 14:33:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D71B28026C; Thu, 3 Aug 2023 10:33:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 85FC228022C; Thu, 3 Aug 2023 10:33:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7028828026C; Thu, 3 Aug 2023 10:33:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 5B10F28022C for ; Thu, 3 Aug 2023 10:33:18 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id F24D98080E for ; Thu, 3 Aug 2023 14:33:17 +0000 (UTC) X-FDA: 81083036034.09.5EB9400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf13.hostedemail.com (Postfix) with ESMTP id 77EA320064 for ; Thu, 3 Aug 2023 14:32:41 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=M0ZiWK+a; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691073161; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hi/J18RARw6/Qxc4oFoZIMtkW25pVX+KOdV/ft2I3Ss=; b=wSyuiLd0qpT2GA2Estawqg/xJHM5HjhNUFEKOxKI28GGH8jODToBBkyOsix+bTwNGS/T0d aId7rXgmHSjno0wao6K5iStnVs4sBaFdg5OZ/bPCCFTBu9RK0l4sKcQ7folHbdXdE+J6DF mKnwRmTaptTlgKlRrEjIHHU++IXEG/4= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=M0ZiWK+a; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691073161; a=rsa-sha256; cv=none; b=AM7gI4h/PiWC9Js17oOfcivsWh3LzzmRynbAg2lUdqElpfx1am15D7aU9TCAqQbqyyaCSX Un6RT073BzUdZC7BCJR/bxxsp89VPl/ZN4jIWMwjUAktsIb0T1hapj+3VDJL8OuQdnRL4z B1tinRfNbgGtiI4wmZYgWgj/f04HGUQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691073160; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hi/J18RARw6/Qxc4oFoZIMtkW25pVX+KOdV/ft2I3Ss=; b=M0ZiWK+ayIqeG5BHo+BNiwNZTdvVd0U61HvjTBkrQUgFmtY+3sG+YFttw70UpK7aiFKA24 P7xZQsXB3UHD8riFZRpy5/mxq/4ohStJJ/8wxnHnQmyKuDu5jG8WJT7SpgwzHUA9N+0c2V mjT+A3TP+JgRCRpZ/oRTstSIj/GhGIU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-650-PGKiOyclP_iO75_O7B9M2w-1; Thu, 03 Aug 2023 10:32:36 -0400 X-MC-Unique: PGKiOyclP_iO75_O7B9M2w-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 686CE8DC664; Thu, 3 Aug 2023 14:32:35 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.129]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5569C201EE6E; Thu, 3 Aug 2023 14:32:32 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, David Hildenbrand , Andrew Morton , Linus Torvalds , liubo , Peter Xu , Matthew Wilcox , Hugh Dickins , Jason Gunthorpe , John Hubbard , Mel Gorman , Shuah Khan , Paolo Bonzini Subject: [PATCH v3 7/7] selftest/mm: ksm_functional_tests: Add PROT_NONE test Date: Thu, 3 Aug 2023 16:32:08 +0200 Message-ID: <20230803143208.383663-8-david@redhat.com> In-Reply-To: <20230803143208.383663-1-david@redhat.com> References: <20230803143208.383663-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 77EA320064 X-Stat-Signature: iaun7mxeciu1n6xxi7f1c5e9b15h3qta X-HE-Tag: 1691073161-157529 X-HE-Meta: U2FsdGVkX1+d14kPEtArllS3p6SyGF9AbUgW3secSILQuXhCLnAXNiKCJ+48wwWPTxefKYpsQqC+Zq98rPIYn0/Ox2IUYlfAbV9a/nB1xF3MHUe1bYWtcK6rHFuV1TFh7Rh8yOZgUssa2cQecpLVq/OY0NZKbCE7vipIM1o8mtrKjLNleI6ICFj0kSjoPXw2O09QAWAENCvs9bDWiat+RtLOTSxeZyQRQVnzMxovrXp58HFFMFm4TSnZzcBNoLV08qMckQd9vwpbcp34qc1nl24BZWY5xH//scFyTiX9WoUIGCpw9th6hKTH8go/8d7wjf4JaOMsCvu33E/KIwxM1hNQyIOD4uCID5VcKqpv8zI8P6wOjuzJ0wbd03y6CjIzSOePr5iVHcpUts8YkEbwzHK+mv+0EdqPXRdUSNB5XRL3QqPRwlcCGwsKB5CAzV13vnWlybpag60AJWK/Gjbo8Xtja9j0C3JyqCJnwHBM/d0i3Mz51R6fvuMUxf/4aBDxwgFqV7sNBXVmfBT8XSfiiROXA9kzj/Qw/J3MEAvvEm71nBDvMdF3oMOrKcxS41LaJDSG/XN3ihuSkBoCPKH4wu0HlnNIn5mPCt6vyzbuQmrj2cS4wEmbYuUdPFYpklM65WrRoxDNq+DU2RsjPktDHhi0Og+mNo9NMusng9r5Txk+egzQj1ljC78xzJv43JukE8PRYfvNbL+Qbofz+KNz4U0LUwfMEWagyL20ZbSHXLYYPHICB+GTCR0hDi4kmx4zS4RsrHZeoZkeaAbIUarGuAQ9mSYhkKYwV7F+5GXBwuigueQNWyeakOfCB+jGHiMv6P94sG+V7fD/8jZay/AQYHfYuXtHSEqQBihdYeKMQZ8IJuzfHETo8RNmFyCvqKddFMWwtnoPdNGwDLvcmxApOAk/VSZtVn3UqMhYJHlvVLIQoXpdBSFe1zIZx2brZ86pUvFsDUXtCWtOReY+QMV GLsrmf1B 9OIqzXiY3Rv0L0eOKX+Vv+OmczZl/DgdwIZTN7RyBHrD9XoDVhRgo+H19237PAw292S+mE9BZo4Ctt0apNFNSp2BLJRLZCRuAJxBiuTym3w/V+B3vSs3BvkhHn1ldIqCET7LzSTjBjrgA6TJ36Id1Y6qZUFSBJ5nsnCIq7YJSaFQuxg3ug1t4ezrJyRPzU4G+MqnoF34pA4rZ7LcjLWif+n6pKe+FKP3r8Htl 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: Let's test whether merging and unmerging in PROT_NONE areas works as expected. Pass a page protection to mmap_and_merge_range(), which will trigger an mprotect() after writing to the pages, but before enabling merging. Make sure that unsharing works as expected, by performing a ptrace write (using /proc/self/mem) and by setting MADV_UNMERGEABLE. Note that this implicitly tests that ptrace writes in an inaccessible (PROT_NONE) mapping work as expected. Signed-off-by: David Hildenbrand Acked-by: Peter Xu Signed-off-by: David Hildenbrand --- .../selftests/mm/ksm_functional_tests.c | 59 ++++++++++++++++--- 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c index cb63b600cb4f..8fa4889ab4f3 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -27,6 +27,7 @@ #define KiB 1024u #define MiB (1024 * KiB) +static int mem_fd; static int ksm_fd; static int ksm_full_scans_fd; static int proc_self_ksm_stat_fd; @@ -144,7 +145,8 @@ static int ksm_unmerge(void) return 0; } -static char *mmap_and_merge_range(char val, unsigned long size, bool use_prctl) +static char *mmap_and_merge_range(char val, unsigned long size, int prot, + bool use_prctl) { char *map; int ret; @@ -176,6 +178,11 @@ static char *mmap_and_merge_range(char val, unsigned long size, bool use_prctl) /* Make sure each page contains the same values to merge them. */ memset(map, val, size); + if (mprotect(map, size, prot)) { + ksft_test_result_skip("mprotect() failed\n"); + goto unmap; + } + if (use_prctl) { ret = prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0); if (ret < 0 && errno == EINVAL) { @@ -218,7 +225,7 @@ static void test_unmerge(void) ksft_print_msg("[RUN] %s\n", __func__); - map = mmap_and_merge_range(0xcf, size, false); + map = mmap_and_merge_range(0xcf, size, PROT_READ | PROT_WRITE, false); if (map == MAP_FAILED) return; @@ -256,7 +263,7 @@ static void test_unmerge_zero_pages(void) } /* Let KSM deduplicate zero pages. */ - map = mmap_and_merge_range(0x00, size, false); + map = mmap_and_merge_range(0x00, size, PROT_READ | PROT_WRITE, false); if (map == MAP_FAILED) return; @@ -304,7 +311,7 @@ static void test_unmerge_discarded(void) ksft_print_msg("[RUN] %s\n", __func__); - map = mmap_and_merge_range(0xcf, size, false); + map = mmap_and_merge_range(0xcf, size, PROT_READ | PROT_WRITE, false); if (map == MAP_FAILED) return; @@ -336,7 +343,7 @@ static void test_unmerge_uffd_wp(void) ksft_print_msg("[RUN] %s\n", __func__); - map = mmap_and_merge_range(0xcf, size, false); + map = mmap_and_merge_range(0xcf, size, PROT_READ | PROT_WRITE, false); if (map == MAP_FAILED) return; @@ -479,7 +486,7 @@ static void test_prctl_unmerge(void) ksft_print_msg("[RUN] %s\n", __func__); - map = mmap_and_merge_range(0xcf, size, true); + map = mmap_and_merge_range(0xcf, size, PROT_READ | PROT_WRITE, true); if (map == MAP_FAILED) return; @@ -494,9 +501,42 @@ static void test_prctl_unmerge(void) munmap(map, size); } +static void test_prot_none(void) +{ + const unsigned int size = 2 * MiB; + char *map; + int i; + + ksft_print_msg("[RUN] %s\n", __func__); + + map = mmap_and_merge_range(0x11, size, PROT_NONE, false); + if (map == MAP_FAILED) + goto unmap; + + /* Store a unique value in each page on one half using ptrace */ + for (i = 0; i < size / 2; i += pagesize) { + lseek(mem_fd, (uintptr_t) map + i, SEEK_SET); + if (write(mem_fd, &i, sizeof(size)) != sizeof(size)) { + ksft_test_result_fail("ptrace write failed\n"); + goto unmap; + } + } + + /* Trigger unsharing on the other half. */ + if (madvise(map + size / 2, size / 2, MADV_UNMERGEABLE)) { + ksft_test_result_fail("MADV_UNMERGEABLE failed\n"); + goto unmap; + } + + ksft_test_result(!range_maps_duplicates(map, size), + "Pages were unmerged\n"); +unmap: + munmap(map, size); +} + int main(int argc, char **argv) { - unsigned int tests = 6; + unsigned int tests = 7; int err; #ifdef __NR_userfaultfd @@ -508,6 +548,9 @@ int main(int argc, char **argv) pagesize = getpagesize(); + mem_fd = open("/proc/self/mem", O_RDWR); + if (mem_fd < 0) + ksft_exit_fail_msg("opening /proc/self/mem failed\n"); ksm_fd = open("/sys/kernel/mm/ksm/run", O_RDWR); if (ksm_fd < 0) ksft_exit_skip("open(\"/sys/kernel/mm/ksm/run\") failed\n"); @@ -529,6 +572,8 @@ int main(int argc, char **argv) test_unmerge_uffd_wp(); #endif + test_prot_none(); + test_prctl(); test_prctl_fork(); test_prctl_unmerge();