From patchwork Tue Aug 1 12:48:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13336676 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 00385C00528 for ; Tue, 1 Aug 2023 12:49:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 81CA7940012; Tue, 1 Aug 2023 08:49:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7CC19940010; Tue, 1 Aug 2023 08:49:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6BAE9940012; Tue, 1 Aug 2023 08:49:01 -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 5C4BF940010 for ; Tue, 1 Aug 2023 08:49:01 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 39DCC1A0481 for ; Tue, 1 Aug 2023 12:49:01 +0000 (UTC) X-FDA: 81075515682.10.54AE323 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf21.hostedemail.com (Postfix) with ESMTP id 58C481C0017 for ; Tue, 1 Aug 2023 12:48:58 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TMtnSy9h; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf21.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=1690894139; 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=PelEjOaWZH6A1jlfDs5yVYOsc8ih/vy8IfMPFWkWCRI=; b=vgRs35ZWtnfBHxaPsHG2hdQJVRvqhZabzdOu36NP7kblYDkulOvuVDTfvOeSRxbRLzKxuS WfpokgYJgQNaZsBEy7YSSbWdey1CSHZJ1dzzTAYrthErdodWYhyoMboGNcNo4Z6IydZcKE kAsqr0I88qpDVtm3ZE7l/lOsWj6USvs= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TMtnSy9h; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf21.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=1690894139; a=rsa-sha256; cv=none; b=4BhP5VWS4bVIhuKAE3wUoW3GL38bk0pbfQhQ4brvRt8NLrHNcKLY49ALLypX/r+ZQmpYd8 SnAXT1hOD2D7BnulBIN+GHJuDYa4Psojn3zPT1DVPjYuaJ5J0UgFFRY61/LH1bRwdjSH1L ejf3ggbMjz5mIwXHkT1e23KwYx6F+sg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1690894138; 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=PelEjOaWZH6A1jlfDs5yVYOsc8ih/vy8IfMPFWkWCRI=; b=TMtnSy9hmbUPVkoHHYgfVJG6YBekdOqlG1/gKMQD5/9CgTmh2nNfDPEaXiGpqvxXo16M9/ l99lJGLlU1bS6zQ4i/4sy1tlt6dlivuaA+cRW6ahVo8RLD64hq2Yejqs0GG78XNxrg/2uZ 3aGbkW2CRyZzG490MvSH77C09LTc7wQ= 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-450-sCg6i6arPcC_qRSyqhkAOQ-1; Tue, 01 Aug 2023 08:48:55 -0400 X-MC-Unique: sCg6i6arPcC_qRSyqhkAOQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5A5D6104458A; Tue, 1 Aug 2023 12:48:54 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.232]) by smtp.corp.redhat.com (Postfix) with ESMTP id E8606C585A0; Tue, 1 Aug 2023 12:48:49 +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 , stable@vger.kernel.org Subject: [PATCH v2 1/8] mm/gup: reintroduce FOLL_NUMA as FOLL_HONOR_NUMA_FAULT Date: Tue, 1 Aug 2023 14:48:37 +0200 Message-ID: <20230801124844.278698-2-david@redhat.com> In-Reply-To: <20230801124844.278698-1-david@redhat.com> References: <20230801124844.278698-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Rspamd-Queue-Id: 58C481C0017 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: ozmgf3p5zpjnzgcdineijm7wrrqsmpb3 X-HE-Tag: 1690894138-146528 X-HE-Meta: U2FsdGVkX1+AhoStQWL2/LRbRFDtbXmm1lPGVl1FbSttM3n28YPvt8ei83Kn8vyHuw325ouOupdCpNqyNUi0XC4+fyRbOZt95XmD6A9mN78ssD9zkmtEBD3+eZT0Hn95qRZTPajGkgRlWGncBt18uAxOVu0XB9vTlcSsR0tqt1KfZdcb6okQfBbZy7aBBWKlKuw9GHTgWQozZQIS4o+lAfqkGzucIoupRo3cPVyMOQhma5d53RszXbAhE/6VVE0EEmty5Fg7IjR1zznZlm/R+mcO3KKH8pSjtkY7NqJMXhWzWma1W3PgzY1xT238F05j3DQTwDuWKaDMpwhDU43hb2tlh4YcBMUaGwFbhKvkUk0O9wQCvxNH8xdECStbS7QO7F08XHFjmBSFMn++kXHgLSx+CJOXk//klbkBvi7U53koH3W6j6NyOLRz2xN5xgABemlVc3knRsz65glaJ8smvl49Srxxo16wXvodXxGsMkhuMCHf2CAB7gcaJ1tXQjbsmM5ENcrQa/pE1tdYcVpfj0YTuLJXY6pgmR7bLHObsgnBB6FuVMBqT7HbkNcXt+SRDztbjakViJ4/XzNNrIxJ+SybdFIfkS/XFV9VXzW5tuEFspFHSEN5PtoPk9eCANiMX18j8IN4dDLRYbUbY5PXYd3iTXrF5rvDEyZ0jzf/Mj5kNE4jkR+WAhKjNHMG05XRKqyWDNv5yUMkVnJ1U+lYiJns/sRekS9T7uYgFZg3/d2eL/RTE2E2ShO5MZKkJrbdD8dau9Snbha6kQevqn3DQI3Rd9diQWIf5Ytp7h3BLv8xOeLPVE0Cg5Xlim/0ft9bAYLSVa0m89BzZCM+rdUKmDHgPiJJQPzn/9Ywk8NFla4yJv4oQgBasZLcFNJ2QNhATHedFEQzG4C11xfzmlbkStxLoZZSqoyoYFpVp3poh5ZgTCnhfZcT/JB/o6wbPnVaTvd7RaJxYCcNzfil1Cg gvImN9Bc bI2rrn3cIGq5jlMwnk2GExtabGYAcKQYQiNKnzjLV8cof5EhxOneJ00OV+Y095LwIITGgsnPv1ETvqQ98qry0htVIeqkRMjwcc5COVAgkWf34xVEk7pvNNd+CcxI5V9wuX5ZgUk1s3suQXu6T2SEAZOo8NvG9qDMSME2vDG0M/5Mu4hIqjYxa53HnGLMcmkIIfeO60Lms6avPE+khNfgh6zQniP737Y3mLVqATMltY6c46XHMTyKts9uz4u6RqVWwPGDq2ATWesszMUOgB7EK1Zq/sQoBMdw0KKLGyGRdOxwmquXgbGJwoNZdBNtAzQvZbhI8D+Sr8FXbP8o= 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. [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()") Cc: Signed-off-by: David Hildenbrand Acked-by: Peter Xu Acked-by: Mel Gorman --- include/linux/mm.h | 21 +++++++++++++++------ include/linux/mm_types.h | 9 +++++++++ mm/gup.c | 29 +++++++++++++++++++++++------ mm/huge_memory.c | 2 +- 4 files changed, 48 insertions(+), 13 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 2fbc6c631764..165830a95641 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3455,15 +3455,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 da538ff68953..18c8c3d793b0 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1296,6 +1296,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..f463d3004ddc 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,12 @@ 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 really require it to benefit from autonuma. + */ + 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 +2574,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 +3000,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 +3181,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..2cd3e5502180 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1468,7 +1468,7 @@ struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, 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 Tue Aug 1 12:48:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13336677 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 289D0C001DF for ; Tue, 1 Aug 2023 12:49:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A3EC3940013; Tue, 1 Aug 2023 08:49:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9EF4D940010; Tue, 1 Aug 2023 08:49:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B76C940013; Tue, 1 Aug 2023 08:49:08 -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 7BD11940010 for ; Tue, 1 Aug 2023 08:49:08 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id F06311A046E for ; Tue, 1 Aug 2023 12:49:07 +0000 (UTC) X-FDA: 81075515934.15.F9F2DCF 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 35D2380019 for ; Tue, 1 Aug 2023 12:49:05 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KhFuvxvj; 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=1690894146; 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=cwEeqAi+XLxejgBquA7kqk/OGacbT3ehK6piRAqjXKo=; b=DEzTkKMvqr6wRSD2prYqHHUKs5p1FA+aXXv6OoZvd+8f5Xm0KtUb54HmFQBS24YUt0g7hT VgsHZV2HR1LjH+MQ2H69abw4nflGwo61VsuKEfFuQVxnQ21ZMoVgDTmaF17aV/dut9P0X0 fMnR/4FALQnPHbgwYIHVHujzZySdico= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KhFuvxvj; 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=1690894146; a=rsa-sha256; cv=none; b=6m/1i86dIJLzsRXtupSbONJtJ0lA0Xs80VkzPCc83LM/D70uWYTVzLbrleW3Kd1AxOUlS1 glnOlK94Zo4R4F/+s8f/e/0lpiYJW9otv9MTHfwMHUEkVjsWd19Xq3l3mH+apYFxk/uTel 9AEFjAU/cUsibEWIXInFmHjkAODOOlQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1690894144; 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=cwEeqAi+XLxejgBquA7kqk/OGacbT3ehK6piRAqjXKo=; b=KhFuvxvjUXmVmVuFGa85VcEDlhLaEKGn8RRRzB5WLBlRn8rDc0kq9uphwWmOFMi7nxWJ6r LoT69wYjou8MpQ7XhkFh6d/yy26D4GTC+JfmYJH6CmnWoGb9HGHaIcf21xfC3uvqOqRD57 gsklMpLBZmAdoc3JGj1u59Z4d4PsA38= 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-171-pC7sraXaO5SQMP5Ldj5t_A-1; Tue, 01 Aug 2023 08:48:59 -0400 X-MC-Unique: pC7sraXaO5SQMP5Ldj5t_A-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D72DF803FDC; Tue, 1 Aug 2023 12:48:57 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.232]) by smtp.corp.redhat.com (Postfix) with ESMTP id BAFD1C585A0; Tue, 1 Aug 2023 12:48:54 +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 v2 2/8] smaps: use vm_normal_page_pmd() instead of follow_trans_huge_pmd() Date: Tue, 1 Aug 2023 14:48:38 +0200 Message-ID: <20230801124844.278698-3-david@redhat.com> In-Reply-To: <20230801124844.278698-1-david@redhat.com> References: <20230801124844.278698-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Rspamd-Queue-Id: 35D2380019 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: bpi4mbhktgrfequf75kfj95fx55fc3cu X-HE-Tag: 1690894145-27948 X-HE-Meta: U2FsdGVkX1/pdc7Q7z4R5RRpJl/01RuI0m+vOictyV2gdb/N0kOrAC0WdXAzJMLGXCG9SX0k4zWp4uCHi77cPb/c6i60S43EoW52tdcwXX+dEB25oRvh9rMI6qDA9dCWJ2t9RKxrxCCIJ/IXjUxAtU9dktZ8tKE1uRYYzRPqGdl8y4A5gH/GlOtPPJSubH77Fs5wsxgT/E+M06VXg9TMiu6dlbScFL2a4CoGPw1qAz3XXwRvRaZ//rVE1fdziIT+l5b3vDBgjVamp5So01qBR30TJjINTHMXwd9VxlZHosQNZTJNCsGFDTiscp1mEjJ2eDcyuTRusWcph5VVozIYrmJaSvaFUyICY18FPVhqQyat2VambmJhKz0xaBT5JPjMVXdKFNO16o+lMvkKkoOBC7rlDyOQqiLiT+J19CPEZyldouPiJ8c1k54n1qCOws30M4rGpGi2feKz9hZt8ckZRRuta4KViSC6b6YmB9GWfooAC490na0opsHSRZrxu+JS02VzeKxhWXd43sEpEsnv8zKSFrrFC4XDI3jqrhAD6WV0MRSPOYdAWhpO+dRmy7e7Df2k1P1DkRyb552sIrR7UWGdFG+kuOGUvBtxqa5VXPJh2wqDyQtmZRm7IM59tz0wKBAW0JvKu+Q7zvd3XYyCeHUu68Pl3RAqEuekXqcGhRSy/wfWIexweUMPdUz3QGVobHK+NWqNbeQfcgQ2afp5kSxlS5w07+rZIqKgYL8GtV3jW41ssK99afXlbWzGzoJRL1mJk1h4zNP7P8jwIGrMxbR/pwUbStYm+kDkO36kV6J9sWA6Px1pbr3iQYYrT67xxmzUsuvMvpmmTTRUVwc4HpvFgFOFfLHOQHZUk+Ecx8zNsLo281wzYvZIYmT50cynbdSZByejF/ZLjXHiXOqbPxuxBvLWDtggOUb3C4H/bkwHyH9pPaAwu2h7S++Sd7dX4ksZmqJMdpS0hfHIalC DduoAp4z fkcS69bAWfoaxIaBa4bfP9Jb77icsbWXv3kQT5VdY9WNucwqkRobvJpfvgsrkA/j+IrSnsvBmTnx07cvIednnNB8vtDoC3mMkIWKLh1H8pwcIHhvvmdOPlZBK5KjP9GQm5Z38TJkMyYSSfHOSzeG3Pu9CM+TA6IZVOOxgkTlrw/1dqhvzzJ90vCIQvsIoU5HWeParSUsTtuqU9IwBPNi5CTCMSIHOqm92ZdBg 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. Fixes: ff9f47f6f00c ("mm: proc: smaps_rollup: do not stall write attempts on mmap_lock") Signed-off-by: David Hildenbrand Acked-by: Mel Gorman --- fs/proc/task_mmu.c | 3 +-- 1 file changed, 1 insertion(+), 2 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); From patchwork Tue Aug 1 12:48:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13336678 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 1B2A2C001DF for ; Tue, 1 Aug 2023 12:49:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BA067940014; Tue, 1 Aug 2023 08:49:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B4F52940010; Tue, 1 Aug 2023 08:49:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A3F0D940014; Tue, 1 Aug 2023 08:49:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 93FB6940010 for ; Tue, 1 Aug 2023 08:49:15 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 799EF1C91F2 for ; Tue, 1 Aug 2023 12:49:15 +0000 (UTC) X-FDA: 81075516270.09.9C8785E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf17.hostedemail.com (Postfix) with ESMTP id A429B40010 for ; Tue, 1 Aug 2023 12:49:12 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=G3Rl7LR0; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf17.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=1690894152; 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=gUlJTdYajYQKFyuVKeOLkIAOkLqgD8Y9E0HL8uxtZSSiv+05UtzCS9A2xAbJ4qgtXiq51x tZrL9vUytSTWh0dojfU7hL4/tS6L2ib/9eko6h/Zx9u5TMCGb2GA0MyYE62xulZnRbY+es 1ELB1D+vitfK99ayWOzPRtrVKJo9bQM= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=G3Rl7LR0; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf17.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=1690894152; a=rsa-sha256; cv=none; b=qKY3i/OndVdNfb8kf/3NZzLKEcc+KQbHVhyb0sobq+2iJXTYj/h6yxsLg4xGC4XSPGh6ke oBH1K9Ompwo890FUvHWG7im37zzrtYdE7yJo35tzCSt4HQtaouTdNQddb1HelNUvrgXoLM W7ePU0YVN1aRSc06cgw88zlLU2S+5xM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1690894151; 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=G3Rl7LR0BR64fQkhORfeh+C60fWHAI5T+H36Eu1nIv2VVhzBp4BKiw2EmBUV6u21ib/EQl 0nGdY+5Y4YKxTdvf4o9rSwKYB69qDgQRGaAzRgSBPLSRwSDPQl3WC13okeKgc5ldhzUP4A JJlP+qeU/phDsE6SD6FLujJj5v6pzZ4= 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-275-nQOIVHn-OOObXZamCc7BOQ-1; Tue, 01 Aug 2023 08:49:08 -0400 X-MC-Unique: nQOIVHn-OOObXZamCc7BOQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B20E68007CE; Tue, 1 Aug 2023 12:49:07 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.232]) by smtp.corp.redhat.com (Postfix) with ESMTP id 266B5C585A0; Tue, 1 Aug 2023 12:48:57 +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 v2 3/8] kvm: explicitly set FOLL_HONOR_NUMA_FAULT in hva_to_pfn_slow() Date: Tue, 1 Aug 2023 14:48:39 +0200 Message-ID: <20230801124844.278698-4-david@redhat.com> In-Reply-To: <20230801124844.278698-1-david@redhat.com> References: <20230801124844.278698-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: A429B40010 X-Stat-Signature: 1qmehxebjbq15uxuoku9a917ejjx3nfe X-HE-Tag: 1690894152-888542 X-HE-Meta: U2FsdGVkX196bhBmu9dNxe9xwgc484b5tbO/BHOfZxCIxWnmTgWHiXmulkYzVPP02O9yaZpqUmBz5zpM04cNrQpwKcesfBwORWKm8pz2AkoHOJ/Af7JakKRFSd63efo8OxAB99PYoC6BrM9OOW/V0fbo/Z6xq1+SVZ9gBjs5WIPwEnVI2fYE/wZeEnoCe8V83m6SK1XBIv/qRvXVHsVFvRndIG3yKD6kJ76zLtAonNRfnGn7uqP2/OJQ0Z3aOLeZBnX0Oac4CJNqGL+VlkldiNkdIZH3FdrQGUgl24BLggucXpo0eBobM6U+MJhQCGnZMY64gsAVwK+YD2V8y62CGT1Z03su8RyQChTdFb1EDdXIc4yI7/KENtOHP1KVmD/NQNPUvb04TOouFIVPxXJVuijXS19Y7sYuwfHVDA49fleLNUXQVS7I86G1+1PwTI+Ni6eUMKzNFbVaPWUNUp7ZGYwlFzCIVRENUCkBp7IUe9YRqLhXo6ietlMNC1jSjiM4i+PeluD9XA24YQE460QO231ka4GTT9H6vOTesQvNYnTMe+HplIjvb9H3s3JvkxzeUb/JhUqlX5iiFY6ARhLrTnoGE6L2jbtm+4/eKFlh45H/xhLn3GWJF+5L6JZczhM3H/z18UlXbLLtBXxVEXAN8Lw+d2KuhudSX8p9k1jmnEJa2prEWM18X9T7OgVGd9ATWtAKa/FG4/Ky6q1EiGqyPthUX1i29+bDp4yKRk5tpBsOSsvBstQ6WRdFKWLIbVe5/TamBu/hO9E92kaC7iaeKlo7AUHftUh5uS7U/Nfn/hyZUPGgWKiX1BGApr+Vf6QTLEFlnLb6LeCffLRbKE5iFqPvD0beXI34qeSVhjNooHs7mBpReNZmHB71ngGUc/BhB15bjQO9UhBbN9GlmLobSsf6Q7iSsFWZkNeKJV6sMUs5VPSKkgsWcvo8aM1M1HIyg7zVHJmHdsxPvStJ+h1 nlEOYnqd ziO1f/G/j2OToeGhVfjX9mcb7yn5gXw7iypmGulNyRZS29UEMvdUpk6TYKS0nXzZGhAjnJ8524YelIVuYuugjOVillJqhCLtznB6l8GE/QzKpCECD67ArSB/F765iAAQIQjsw0coKP47xifyl90Z/VT3vf8S4/KISgWJBu4UIfC5B4d+2evkFVuvFcSoSveBpYYZmF9AmiV3Q/QPSp+18PROx2w== 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 Tue Aug 1 12:48:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13336679 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 2B437C001DF for ; Tue, 1 Aug 2023 12:49:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B291D940015; Tue, 1 Aug 2023 08:49:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AD9BE940010; Tue, 1 Aug 2023 08:49:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A27C940015; Tue, 1 Aug 2023 08:49:20 -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 8C263940010 for ; Tue, 1 Aug 2023 08:49:20 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 64FC6140563 for ; Tue, 1 Aug 2023 12:49:20 +0000 (UTC) X-FDA: 81075516480.05.4AF0ED1 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf05.hostedemail.com (Postfix) with ESMTP id 7FBD510001B for ; Tue, 1 Aug 2023 12:49:18 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=i2Zr5yCC; spf=pass (imf05.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=1690894158; 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=ZVj1eNQXOQKDlAdeuW3yrTqXO0l5971B6VcwmwMnAdw=; b=Cm82Vwhuf2nwdpIhTrrAoqQlkIABb0O4oniM+N38M2AZkhQnB2WTQSY7j55IHw0PYyp6I1 rtwNpoNz9BUZ585p/MSjEMSNNkmml4FLjOOofv+mT7/Duot0EPHNPZ8jbMvhZwJxjDYv7y IAKaqv7oWk3PdLjAWseDRU73Kq2yuK8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690894158; a=rsa-sha256; cv=none; b=wxjCpxFOVwm//Wx9Iq3bDy4TnpxYBdGOVq3n4Iw5zffmQh4sfPI+OzTyT0wooXgLnV5OBT zlF7mot8Ia9mqO0A6V9ywu3yO4NZeC5pqFOdvuTdG/ujcAVOn6HPJwmjA2RqcLK46IIBD1 IOqyEdPAVLPkl2RbU7rY+rqNBcHeHeE= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=i2Zr5yCC; spf=pass (imf05.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=1690894157; 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=ZVj1eNQXOQKDlAdeuW3yrTqXO0l5971B6VcwmwMnAdw=; b=i2Zr5yCCGAEqK44gaiou9vA4SZnQP46I7+L2srnvkBa4HMJ/lI0ZZhkZKnLhB//JvUJOSb h/XCHCKIyhojx7VqHb+6oKbFOaHkuITPVn7PXJ37yU/GgW0i3dgxmeYFiTj3SMXLCO4M/4 mO/WXxsqnUV/dutC74GbuohGbWp4f5g= 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-674-yaGk4Z3HMGqR7gEOj687BA-1; Tue, 01 Aug 2023 08:49:12 -0400 X-MC-Unique: yaGk4Z3HMGqR7gEOj687BA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8CE761044596; Tue, 1 Aug 2023 12:49:11 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.232]) by smtp.corp.redhat.com (Postfix) with ESMTP id 12865C585A0; Tue, 1 Aug 2023 12:49:07 +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 v2 4/8] mm/gup: don't implicitly set FOLL_HONOR_NUMA_FAULT Date: Tue, 1 Aug 2023 14:48:40 +0200 Message-ID: <20230801124844.278698-5-david@redhat.com> In-Reply-To: <20230801124844.278698-1-david@redhat.com> References: <20230801124844.278698-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Rspamd-Queue-Id: 7FBD510001B X-Rspam-User: X-Stat-Signature: 51uys3674xm13fb56u1fdptwmzjpktgu X-Rspamd-Server: rspam03 X-HE-Tag: 1690894158-362382 X-HE-Meta: U2FsdGVkX19/vJAfqNHR8RQRY9obeA6yvh7prjbT/ASA3Fj/HPtYKXBdh9zFzaBsuY5QtiA+UxXtdxyr9MTyo0hFAcr2rQlMb5JPitTXmEjUAxOUjiaoAySZtqt17/oCM52pu3pRJzLb52GiB9qd8kC4fvk8beNtdEOogTAOok6vayxKNWWDf4GH1c7URl+iEm/FM1Du5BMyN3QhTjVgGjI/9acil33IKwlZ4wUEIrzE30NkNuaMMvKRFdwsL34cYqgyQ0cHgQQhAU45jUYnKqBT5Z/vLZ0vjztfwi2C4Sn1oiLs7phrHZL/fflo5kvpc/figu3NVN3WbMP1Nlm8LxiTd0z4Vsy+L2IkbdHg45fw8HkUbRcotbiEB5pRH3pyMcfcRnx7OPGKOFYus2thqAXHln3vhs6CZVySsliPJdk6pgzDX1+EknuxudNA5Yzrb5YXuyRra3SvA769by8e0fSKKHC6+UE6r15e4GEeL3qnn4VBX9VV9IaUA0rRhfXLMpsPmG49/4QOE/iQ12MzRfKtBUZBCpFwprNGFj4cxUMbI0SR1eY3JmpqZo2bxSeKk4hfePO/sThTFRlDUuqwmBvphD0dm/JJUoF77D+Cxu7yDqSqmom5v+csDv66ikcmnPlY5faij8hHuZFQ4zQkQzqF9zphiAbQ7gsc46AOnKgmuSmZo5YGU4NiXBxF48YJH4InPKM5fizRP17JMrx7b/7wJLnAaBLu9xrmdqWFApyP9H7qMKbPpTepxLqK1ZzDZwOVwz7menmwTNIbEwfgzrhQ84U/ythYpfplnU/QFfhS3XtWhYvrdXUIahwvNd8ipt8qh7iFZ/6IBK+TvGvzSIz+MKbdOamOiyqXXYg+Qo2I9HVJsjf+iGmtaV72y1CKUmxzOQOpP4qMeBFbIVFMP7wXDZNtigrGhaqsWl+QNsUGRSkDCNxoGUv0a3zOwO/25AM8Oz6hpTK0klRwzty VnDPuP/K iJk0iANffqgFqSmxBNYT0nCOd23CPJYcSpfyhzxr6zqE03I7VLt98wGjvQPi17ugRcxTTb/rTOjjWCuP1u/nmsj+m7sMphdd98+ts0/i+h7BkQDoyxp8c8cHk1AGj7XHk6qRIJhnUaHF8B+/j6KdGiztxOgYS6xmSNDPcBDGkjv/kDfgdwcoPuUwdXh0ScsAUhpxfHX1cnXhASlaOYSVm+sQcJg== 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 | 6 ------ 1 file changed, 6 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index f463d3004ddc..ee4fc15ce88e 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2244,12 +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 really require it to benefit from autonuma. - */ - 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 Tue Aug 1 12:48:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13336680 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 19984C001DF for ; Tue, 1 Aug 2023 12:49:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB5FF940016; Tue, 1 Aug 2023 08:49:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A66BA940010; Tue, 1 Aug 2023 08:49:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 92C1B940016; Tue, 1 Aug 2023 08:49:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 7F42D940010 for ; Tue, 1 Aug 2023 08:49:23 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3CDA7140A59 for ; Tue, 1 Aug 2023 12:49:23 +0000 (UTC) X-FDA: 81075516606.11.9199975 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf09.hostedemail.com (Postfix) with ESMTP id 8045E140002 for ; Tue, 1 Aug 2023 12:49:21 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="dqf88m/h"; spf=pass (imf09.hostedemail.com: domain of david@redhat.com designates 170.10.129.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=1690894161; 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=O8i0szRefbgJeKSL2o0qG6DrCbaDUp44uTnfIIYA8MY=; b=ZC5JPNBjzBQWfODOYyPDvpPYU7DJUohMROjoFgvCFcbc5YJNkkcp0MYddeIeHq4rX+OBa/ mMhW0jE6qRz3rBRZ/w5cofdk+SnabTGkVjTyy2Z77QXUA9ttf8nIS3fIJhCM/dMGYbW34v aVZ3Hjbjwdsu2yqOrfhTIcCZmwZx0bw= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="dqf88m/h"; spf=pass (imf09.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690894161; a=rsa-sha256; cv=none; b=uE0emtALgaYP5L7QuKdQJjNVFUq9FdaMYeUnOovYh5LFvhtqbjgRVfIfO2TokP28P9iPNE LKqw23kZp4GcLtRC6nNrPA/uAhZjhL5b1cwruXLlreWTvmm4rjLBhW/BQMNYK/nGxhyjlB EP2Ek+fAdDCHshdAOO45XGphl1sJ61w= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1690894160; 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=O8i0szRefbgJeKSL2o0qG6DrCbaDUp44uTnfIIYA8MY=; b=dqf88m/hKiEH7Qp7VRNqaOgqpJJLS0Yy2467e+MQu5et00uAj7vxUt2Ft+m3vj4Yw+a+Ga n3T5ZM5UoFQ35EMYfxCfO5kHzduHhO/6lIzXkm36hGL5BjWE1e/txM/gdGOJTNn897bkQD 8IwC1fPPM1jxjhzX/1EWdteZioLyHDY= 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-587-gTEMugfTOwCxAfHJNfIDeg-1; Tue, 01 Aug 2023 08:49:17 -0400 X-MC-Unique: gTEMugfTOwCxAfHJNfIDeg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C82C71C09A44; Tue, 1 Aug 2023 12:49:16 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.232]) by smtp.corp.redhat.com (Postfix) with ESMTP id ECD06C585A1; Tue, 1 Aug 2023 12:49:11 +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 v2 5/8] pgtable: improve pte_protnone() comment Date: Tue, 1 Aug 2023 14:48:41 +0200 Message-ID: <20230801124844.278698-6-david@redhat.com> In-Reply-To: <20230801124844.278698-1-david@redhat.com> References: <20230801124844.278698-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Rspamd-Queue-Id: 8045E140002 X-Rspam-User: X-Stat-Signature: 7d4tc3ek118nc9x1s7d1so3gc1ntk5cc X-Rspamd-Server: rspam01 X-HE-Tag: 1690894161-134915 X-HE-Meta: U2FsdGVkX18C4bIs1ZxfEG1/ct4G/TZwM1DBH6eypsA/3d3sNjo12/dIaJcVS74lc9vnK9YLu3p4QZLfiy5i9gf7ooVLzhMP7fkn+HPOAgNCr3DnfZnJbgP83vIaEgquUl7rGM5JaoFNq3ZWVDFqqiMv/YbpOJP8eATBy3G1bQtYqcV9yNVFuW5SAcdEDtKySW6hZe2eqLRUDq0fqcKxjkQd6bKbp+DOsOWdH6vrwZVnUeWW0fmJLbRfYIhnZp/KI+Ixp9vKz3t/IThK6rKRMtWx6UdIsPLdCAcbIkHNj0spfxXzMyCRSgaUSFfU3zTheJBCE8RiNp0ulrnDleRXnivyHsQJM3oFKwPi5qDgA6098aHpYFrgkLwxeBN50TfxGB+QwPoxJCqPSa6iPCtVsWv44RhsnRuXrDvu+q5q8ZMlDPx5OG2xEEv7LaDMYZtMTsfDqIfh2yxo8S2gRUlvZXRttl3BzrP9K3J5II/8eb0aOWd83YIjiE3JAauIFshAlQOAo/hACScEIzjcFln1obWXkRaWBbDHkxhN2I74sMaWgRdeGTD2nNxNzdWj+HCS3h6PvJ08bADXxGu9GvY4h8pBi/U4lejFM2Ku1NXJB5SmQEV/+gTByH2uYF72gn8/4bJAnJSarFaWv/YeQDuA5etvQBA5l5/yvOL4rbS6NY+J5VklER34MUmKGNVOuAhpAIwSqMaq0n+YsNr1JLiaIqPRiJcRaUVc45he8OoAYuUDlVakkHu3+rl0Fa+QmaE0pQi23iCz+vhkUmGKAGPSEL4jsJBZhlusEEKHYCAkdzt6CkXAzhhdauV2z6YorwMGDwa2ua8kgGUMVqmj5GONX+8DhEkUgynq9B/roJJuDA0PUQ5dbmOMOSQHW6ka3z7bA9QeXAfEcvo84DIz83RDmNPWP0RqIrxqCIgvihxaTu1R7hnxC+wVeJc5OcQ8kMGrkKpx9szVd0yre+nyCnD XeClgzwb Rifj4eL9tYLNgZpuRuhKxqeRpAcIZxjJggxmAKg6dsvtVR7tmZcx2Ls7CyDbPeXrE0fq4TLtH06X7laK4o7LAuLhC4LQy68r+7MhBgfnDo5F+fkEfur05eAclBdSVwNI3Ex1rgtHd72zq4uMPBlFxvifDE2ImGmeQm0sKE6onFXN4rJYM5P17hHnfLCn+cEZ7IzD3+6BnMHta3+o0jw27pbs7jNyv7FMJWncf 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" is wrong: doing an mprotect(PROT_NONE) will end up marking all PTEs on x86 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. Signed-off-by: David Hildenbrand Acked-by: Mel Gorman --- 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 f34e0f2cb4d8..6064f454c8e3 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -1333,12 +1333,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 Tue Aug 1 12:48:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13336681 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 48FDBC001DF for ; Tue, 1 Aug 2023 12:49:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE8BD940017; Tue, 1 Aug 2023 08:49:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D9842940010; Tue, 1 Aug 2023 08:49:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C87EB940017; Tue, 1 Aug 2023 08:49:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B6F7D940010 for ; Tue, 1 Aug 2023 08:49:28 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9712CB17C9 for ; Tue, 1 Aug 2023 12:49:28 +0000 (UTC) X-FDA: 81075516816.29.EC031D2 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf03.hostedemail.com (Postfix) with ESMTP id D5D9A20024 for ; Tue, 1 Aug 2023 12:49:26 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=UK93yNpx; spf=pass (imf03.hostedemail.com: domain of david@redhat.com designates 170.10.129.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=1690894166; 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=ETNtty+qD9MfEou6PkXGN63LTSt4s/cNeotNKd9s/vU=; b=dM+R26fUlz8xa8GG7adfPFyO/J023uMGS5cIT81fK8gvJSl4aJfiHlCVqPjOPYlAvQYhIJ NsQ7JRD9jHFP7w+H8UmnxPIiha5o+KVmIZct07WlCtF9/WVq3UJ3jfZ5domoFAgqfiEUU3 YHp0k8YTxN375Jp+5bqUs7T4x6UkW0E= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690894166; a=rsa-sha256; cv=none; b=j+QDpNi0y3xS80C3/QSatRQ5X0Ji0EOIiUFKSMHdccqBvDF99YWMa4wVsWlqQXG2m8SHvr xEmEFKY2VKPHe9RcFUhJEec4zBm7vLjlueO5vc9kVdh19pepaM3Bb6oglr3MvRVk6sDLNG 49vroPXJ79jTWO9LDVEe/QwNZ83J300= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=UK93yNpx; spf=pass (imf03.hostedemail.com: domain of david@redhat.com designates 170.10.129.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=1690894166; 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=ETNtty+qD9MfEou6PkXGN63LTSt4s/cNeotNKd9s/vU=; b=UK93yNpxDbIxT3k7ddN7S7AnJ137QUogB4SeCupNzkElArGbQXVE9A/yC2087UkSt1RUIm vBvIb1bAhmD6KkYAxOe5F8dgBVH2JHHf4n+GDXEZ+2iTqfeZo0bA0ZzxSK2+tKTiNapuNN 3cLJxjTvV6GAomi9U+MOsyhMW9ziZHQ= 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-613-KCGrX7soNoWrUz7xASwZ2A-1; Tue, 01 Aug 2023 08:49:20 -0400 X-MC-Unique: KCGrX7soNoWrUz7xASwZ2A-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0FC5529ABA07; Tue, 1 Aug 2023 12:49:20 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.232]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0D58AC585A0; Tue, 1 Aug 2023 12:49:16 +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 v2 6/8] mm/huge_memory: remove stale NUMA hinting comment from follow_trans_huge_pmd() Date: Tue, 1 Aug 2023 14:48:42 +0200 Message-ID: <20230801124844.278698-7-david@redhat.com> In-Reply-To: <20230801124844.278698-1-david@redhat.com> References: <20230801124844.278698-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Rspamd-Queue-Id: D5D9A20024 X-Rspam-User: X-Stat-Signature: cggpoexwxn1mo656hqyzars6f4zn8uyj X-Rspamd-Server: rspam03 X-HE-Tag: 1690894166-598855 X-HE-Meta: U2FsdGVkX1+2XJWRd/8rXwi++Pk2pUm2HxkB2ni/NGsEJr739zhCZ910EPAPhPc4CZ7j6xNLH3IXre87DhkJpse07w1W1zoIR9YvYjsXwSgcrJebu28xd6GmEy0fV6Bn2Yf1Q+ZBKFtKEX38b2Ui3kQfP8z+kIHre7EvDfFcNSVba7QnNjPifubQBqLe5fITi1rBI1fG27MY6WMJypcZHZX8QXr98+2Qyrtq0oCHTd+I8bNLtmrA3UC3q24KNBoxwzs9H+3KaSBCWIpM5OcCLA0znoECGUOg1TyH/Y61ikwVbXjGAGpn3GqOMQXHiJ3qu9cvYqz9WvE69f822dSfMMojh3Sp+3HQPOIm+Dpb3pQlxwR5VIMZkDdVcd3IjkaW9iK2JzhFpqnuikIOkIfeglj9AKqxMPbqYp1otWvQ1YRJ7xGtEKhHZm98NaTK60Dy2Z+PX/Uov7aXazuoUSJlYnjzj+Jd51IrVAL9DFuuTQtSpbySBOnd/nFLPcVYlO2pq0cjSVRqjl8D6oFcs6y64XHP1Ph3NzoGM8flZRaU8tHLiP3zz6l6k50wLb2hEd6CTpw05Coz9ZP0R8y2oe73Gib3iBgpHUtuVdFwsLMr4pZ+wpP0EIDVW+Udke3tX6U+NsTq8BeSZtRG4q1D0ghphabPcAAvfQ22kXNXRssTRpcmDymeCerUJMFB6Mo/puebfI0ewaBFE0QfQBJF/LzAXJqHvW2nxtd+h2Tf35NbRkzrmfXz7QBIr1ZdN3zY88WRqdIt3G/U8WqQEJSrHn72eff176ULYfi50D5x5W95+0gGzx+R4MsfitauALf26r9Gz2AH8OuttrHT/9/NfWQtYmY62aotduJmK6re54KWsJaj/q+smrQvH5RYY88C4OR5s3Pilsq8Qt7L05nvS5DC4wm8K1rX2/wu4jhK+KU/qtxgsdben4jZ2DGIE3/Iuy7gBxH1giJMSsOtkCqh9h6 oFi3ig/T 8ZlucYc7pINXL5CIxzqRWjfAIMKl7ygWvqxzM3UOQ/4/QQrVimU9z9AWR/NfulMvU3FsNzJTsZ61ondzLBMasC9qW+Kbo5T65N5Kfu4emBvAJHfGL9vGMRZ/pvaxaJxKZOam0+unA9hTMdXc+48FuY5pwKc5p77hg7y9FksPLhSObxESEeO4hOU1vt3ELHZ7afZGU06eD1EO57WMlRT5zod2kgA== 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: 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. Signed-off-by: David Hildenbrand Acked-by: Mel Gorman --- mm/huge_memory.c | 1 - 1 file changed, 1 deletion(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 2cd3e5502180..0b709d2c46c6 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1467,7 +1467,6 @@ 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(vma, flags)) return NULL; From patchwork Tue Aug 1 12:48:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13336682 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 70F3EC001E0 for ; Tue, 1 Aug 2023 12:49:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0A494940018; Tue, 1 Aug 2023 08:49:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 00577940010; Tue, 1 Aug 2023 08:49:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D4C94940018; Tue, 1 Aug 2023 08:49:29 -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 C0C56940010 for ; Tue, 1 Aug 2023 08:49:29 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 86CCFB20A4 for ; Tue, 1 Aug 2023 12:49:29 +0000 (UTC) X-FDA: 81075516858.20.1D76E78 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf27.hostedemail.com (Postfix) with ESMTP id B4B3140011 for ; Tue, 1 Aug 2023 12:49:27 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Ua4+B2mx; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf27.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=1690894167; 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=PYDHfZ7HDY+aPKEtD0PrQ+DOMCCGs0BFldVoj8EdtRpoalyNphwJ7LUeT99DwIFQPaR5CJ Lz/e3eG8yp5WmNE12kS+18uPsqNF0hCB6M3kxW8GRmwCau2jy8sd33LPVAI7rzCG5CMiF7 xO2tpTtKZgssTEAUuxj49rgXZ8fj2r8= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Ua4+B2mx; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf27.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=1690894167; a=rsa-sha256; cv=none; b=HJlgQtDcvKMPDhJjDwfR1bGMjSv3Fdl+T5ZIEAfUaPbUq4lZcSjgqR18zhp3ePoQJasTwX 7wTfbrKnkAUkaRH1qa0n0l+mCpKb+2CY7SiTlYk4uQ2vAVkyhoLiyFoWfijHTZ7M/vzI0i ds6V4fzBOo4jxr6qm+s/fz6hF8TXCfc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1690894167; 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=Ua4+B2mxmEPzw2x5/SSVK3+3xQJZVjtBlbdPPyqAGOWMJ0zSy2J3r6bM7no2XHI9gjwNnf Qo49GzpIDiw/v/fQdF+vdlaoFfbYwN4O3JrQcQNdZE9wEWxWnVzyhrc3jiGeHE6zKO73/x ThB9vteLRuLkqJv0jcgVXynDbPpalf8= 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-617-GAfS87eYOzWzIWNTk4bPuA-1; Tue, 01 Aug 2023 08:49:23 -0400 X-MC-Unique: GAfS87eYOzWzIWNTk4bPuA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 14FB9800962; Tue, 1 Aug 2023 12:49:23 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.232]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4759CC585A1; Tue, 1 Aug 2023 12:49: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 v2 7/8] selftest/mm: ksm_functional_tests: test in mmap_and_merge_range() if anything got merged Date: Tue, 1 Aug 2023 14:48:43 +0200 Message-ID: <20230801124844.278698-8-david@redhat.com> In-Reply-To: <20230801124844.278698-1-david@redhat.com> References: <20230801124844.278698-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Rspamd-Queue-Id: B4B3140011 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: otrsa6qu6pki5tt438n9toy37mkcocu4 X-HE-Tag: 1690894167-828468 X-HE-Meta: U2FsdGVkX18ClIpwOj0dFcZbuUv8ncGJ12WY027NeLLdjJHmv/lCZXPmj8AD51w8NRpINlSt3GhaxzS09NR1XAS4WOGxrRBuuIs75mkrtM8zO6H7WMN+ZVN4L8cTZa3mUtaq/HNrS9LfwZWEk96pbvzOIBsiMJU6pjjc14JgV3+agDbjXZN4nWtYHKJywVDy/WGP4NzL5yRmcRUvCyk9xMWwibf8oiEJv3HEMGeLKwAs9O2IGknudPYSnxDL6WsF4Y63e3F0a72ncG3zxmZe3z+R77DQ9d8/GeNd1wLgNNOYWfn303PkWMD3q6Zz7QRU6H7lUYsarRUHqigAaFROIW1U92qwmE+HnYP8vSnFelSobPshQeevXGoDK2W3lpjW2hGfleV1upjJqJ1azAWw2VJPGzELG2PALWNX/GiYZdbHEy/1b8AqcXGAmRLQ54w/VJUU7Qa4jfmHspdVuzxNXUt6lkUYZyEWTvEQNyI+ZgCSWyQ6jlgMJlMX1NIG6KiS/WSAze71Cpw4VuBcDxSoXPsIqx+fo/5ZeHGGya2chWDnZdO5UK9LYbSB5Ap9+0+UvjNljVL1GJGcZ57cTyL8DLp8v6GG5439ptS6sNZxHr+2P53KVoeqvtRVTApLql6LajzBpgmmH89urYeimEC88Mn4ilALoTWoTVMtgHmk2KTSiV0I8Z4XDbgr/+X1RIJVvY0gO6+jZ/SCqd5TYSohpTfQ3uixq7sPQGeowrTyc51vRuy2vpLX6HfpM75A+nadn/UYpb7sXW9Hqadw7Rz5S5Aqg2MCkfvHSJddsq7/hC77R/1m/7X+8nCaIau+s63X4KbepgL/tLidvPZGyCTVDPiq+H0YqUnPsqynAPNGkqQvMkD0s5U02DpDoelDMEiAmo8t8XOmj7bzRP3Pu5rn9uXJ7fM5az3eYuWPjbFLrnffSY+N/sjHp8+QPkzvu14iiaSRfSEFJTYyssJvDQa kP3Q0XjR AZS9nOijhgslE0o1BFY1ML2UHjw9UX/CGArJZmA0ikbK7UDrPfH81pvitUnYboXTtlIjnujM8Kehpnm64VLxKXcLTIVg/Mc0pL4EJtUNivAzjLiVW6qe2O6aXIHB412xpBOydV5fcaVNaPNODKFZINqMYa9He5yo07fGWgyrBPx4qsVY6cU19dWVEHVmx2DvCbwhT2teaEpaiuVU4Gag2WJkZnUnyraZZ/6fB 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 --- .../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 Tue Aug 1 12:48:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13336683 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 E2136C001DF for ; Tue, 1 Aug 2023 12:49:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 86CCA940019; Tue, 1 Aug 2023 08:49:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 81AAB940010; Tue, 1 Aug 2023 08:49:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6E2CB940019; Tue, 1 Aug 2023 08:49:35 -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 5E95D940010 for ; Tue, 1 Aug 2023 08:49:35 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2E6B5C0CB2 for ; Tue, 1 Aug 2023 12:49:35 +0000 (UTC) X-FDA: 81075517110.28.ED8F956 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf09.hostedemail.com (Postfix) with ESMTP id 176C7140017 for ; Tue, 1 Aug 2023 12:49:32 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZidO4QKX; spf=pass (imf09.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=1690894173; 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=8lJybzHMuazwsMI3eeW6HI2AMLlOmpOVU7YALR+ZPkgkdQMNH0OxKTrGo/6a7xx6atb548 FoZRiUaY234aKyPkgPxFcKEE9aHQZHzAAhXF6tjuPDPrJzamFP33hckEqWfCf+EdCGO8TT RB+6SVub+FCN+ob+ntR8fOczxanG9KU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690894173; a=rsa-sha256; cv=none; b=NLNpdL/JyEnwGioqI+iRLXVF1ehrzRSS7306zSQKMDyUKBc1LGCtTWO5QPNKrFbRFpBUzd nGIyZuogCfKNiyEttp/TNzZ4huJ17P/fusWPRyYOgx/JDyP7IHjOaWtcFaqMf+ZSkUxVwO rlwEW2enyePEaiyL/ZlYl91e1NSIbAA= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZidO4QKX; spf=pass (imf09.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=1690894172; 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=ZidO4QKXdBxWkwb+DXCyX462fJjJpaxR3Oa8WlARuCX4DtPsVC7njU/rQAzSp5aK2ZiTjJ S1JnAvSJL3sLdUnRkqFQ7uLrr7tOYJhrVC43K4D5PvJpy8Om0EdjFsDcYpFeveWlGemEIZ UFATr5+VWIuepMWy0EjgKIexxZZK6vI= 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-284-oN0NdSRGOK6hQjSuybYWtw-1; Tue, 01 Aug 2023 08:49:27 -0400 X-MC-Unique: oN0NdSRGOK6hQjSuybYWtw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 73D17805C10; Tue, 1 Aug 2023 12:49:26 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.232]) by smtp.corp.redhat.com (Postfix) with ESMTP id 75021C585A0; Tue, 1 Aug 2023 12:49: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 v2 8/8] selftest/mm: ksm_functional_tests: Add PROT_NONE test Date: Tue, 1 Aug 2023 14:48:44 +0200 Message-ID: <20230801124844.278698-9-david@redhat.com> In-Reply-To: <20230801124844.278698-1-david@redhat.com> References: <20230801124844.278698-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Stat-Signature: dzm1bktdde7sb49dj1qy84c9wjdkb7aa X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 176C7140017 X-Rspam-User: X-HE-Tag: 1690894172-204512 X-HE-Meta: U2FsdGVkX1+yfHQPY7ejQRfNyY+AGdM/NgMwJ4zwjz7Ay2Ef9AdXj1NsZxXA6EUzVdVOQbV/2gkeSuZIWSHyy0droYcB6V6YKGNQSu+gS4X+E/NlhiOlgiAy3FhZ2T/o8P+R3D+R+HPGOuRC0ECimkY/c3r5U+Bz+lIB8tfQ2AjW40jLxJy38X0wSvYtUQuOu1skwyCcOH99n/4kSNjdKrXVH7odLAXTrkWwE1Qkd7lG0dAOnoz2M8MLFkFImzNgxFPfL1NfLStlh96OZNiI64BHfD73o2uFbJmL/7cpZ6MekguZwsvYyJCg5Y/XhHQfrJM31kG9y9r3/w45RkgbMaRFE1yvkGq6y4XXyfol+QOn9psh5dUknrQpTas+TS3crXIKOAM4W/0lM7Pjimjluyibwoupm6xrevugjbk039xUpEKgOyn3xLp7mL3CA4mhy70d/sASF/D4kI+1+hWiSPHSwIhhYJWpARVoPQbogIK9vurnpXpJU06yneOicC3htYaPRWKHIQkRPk0WpjbipuPXwjpr1REwEBjzshGaW2dcXEzV6EfEYl1p/cWJT+JBZ/QAezWqQC50izDW7fp1O98M901Hws2FIO/X+KPPU/cvKi+KrZrXbEocJVNum0ozBePXxH9XDXYm6x4rv4YxBeJeP7JUJDDYVMH7xnL4mdcKvtSogXJTKvsbKc2JAUbt9bYib0jd2CJvjofIOpv/Z5yPbSEKJiEnQwglS7JZI6uyddER9UmThqOqtf/aGb7AwfKFgV92ZguMz88wkpQFsWi8NGFHBEQs4+0+yZfOfm0jQNl/42TI7XRKmPFd/8W4EwWaW7Y2g+X6HBCzjI4UcEilHx0bwcCwkzx9wfY1tZoKVgePNqi6/hjBekV0Tln5cHXckF5Hxzmr6YtLATJxueWKdRz5dylbaueD0qftwd/qtE4xpu7EvSN7dhw27WVkCqbpgEOY1qg7Ykhb2K/ msP4uYSX ALHcPwE1/GflCZ7j0iyJNssWvOWRON4QZNUT/Szp6kqpqN7iOI/e6wyHN01GB3C0lFcTyr7iP2oFO75/7FHgH3ONX5FXu6jkWKZq4pl4M4DjktBoZ3Feq6RIe2BN2aPHBubg6gSUbvWhCqwj8PhD+ycAy+dI9MWK7/0aX4hYoWzxlOS1pDcI8CLBE7dpsDz7e9+XorSNtShMRk9QXCTEu4SR3hYghxLK87kbH 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 --- .../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();