From patchwork Fri Jan 10 06:00:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13933697 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 D194EE7719D for ; Fri, 10 Jan 2025 06:02:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 63E716B00A4; Fri, 10 Jan 2025 01:02:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5ECF66B00A6; Fri, 10 Jan 2025 01:02:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3CBDB6B00A7; Fri, 10 Jan 2025 01:02:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 1B9196B00A4 for ; Fri, 10 Jan 2025 01:02:27 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 86EC41207F7 for ; Fri, 10 Jan 2025 06:02:26 +0000 (UTC) X-FDA: 82990497492.09.A900D22 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2066.outbound.protection.outlook.com [40.107.220.66]) by imf21.hostedemail.com (Postfix) with ESMTP id C93651C0006 for ; Fri, 10 Jan 2025 06:02:23 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=pmQGkI0o; spf=pass (imf21.hostedemail.com: domain of apopple@nvidia.com designates 40.107.220.66 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736488943; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GvbMUAJxfC2ngJPxfsGXWXX3r9undPrsapqykpYUxYA=; b=NSW2Ksdbg+PcT5qZFVssCKHxdS9tpTGIl+PZB9Sddg6Bgc2OGbm4f84vPR1p+9b5k22pE2 VcT2/iZfy86VJO3ANUrJjZJsYNfyep5iZn5EKQ98XFZjdw950uw5+KVYv+Hq1tKv6z5K/j iFupw3Qt7Cg3faR206IH9gllNe5GjaM= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1736488943; a=rsa-sha256; cv=pass; b=u2iDWYhA6lTUzAHAwLs9w//1Aq7kFH9L6c5gAEU9FHJKtKMvOJIlHw7mGN6qEx+lZMwlwS TJnV+tRgSdobCZyLK4XpWdYFKv8+G+P/Czn7favV5XmN0EDNCpYKwo5gwUy+TO+PBdeANt KGVExbSyWxAWFKTVN+xRlzJ4qdLePJs= ARC-Authentication-Results: i=2; imf21.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=pmQGkI0o; spf=pass (imf21.hostedemail.com: domain of apopple@nvidia.com designates 40.107.220.66 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=qxph5V92nGK5XRyfZWS2FMg+9tyZX/Kiz4If90e/xtG9vduZ7nNWPu1AZnDq+wdxMfNQHOkGN7o16iByjN+YZYHNdnvnesppnRYc0gLmUnGmgjEzyI+LPY/zRrntVreAynSWJ1Qgx8LJb1vkex7zD9U+O9f1jcNQr4eLu2fR7TUwW3aB1TOKURKD7AZjaDNC3WMpLW30FWQ2V7W7zXclAdY7GMcsd7gPOiWpYgQQXEQ+i9kXhayeeuspLYNcBt1veTHDgPxuzlma/7djncKCU9zjIgjwhnPL+wQOOwm1oYsE1qilznLeT/EMUm8djpWGPnses6h3fIo0nSfoDQOSDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=GvbMUAJxfC2ngJPxfsGXWXX3r9undPrsapqykpYUxYA=; b=TyFbjjmwB5O+p0xf2p9Zj7+cm1SemPKIaFWgtF4FxcDvHg9W4ogv5Nh6X6q9GO3ikU3CsHWlGj5/qzelhWChNFjJ08ZOLWGAqX+7OeK6+idYFa2HsyMzIb/ZTsxRB4jz2TcHzi8SO1KEX0nRCNFcsBSJ/FO9I9SNE7QVQ9p0R/0DM7OKGx5DWOnr91ESMQw9pgb/7i+hKW2eJYF52uwt6iijY5VuKgcSyrxlRxkm6KPLmtKRmGDNxSFW5MD+LsxmhQw/Qm4GjElk8wEe++TVJ3pYKs2Cn9Xgfd9wif7efJianGilRYKvVFj9Ua1gmvdYDN3p2zUulFmNFN+E+F5K5A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=GvbMUAJxfC2ngJPxfsGXWXX3r9undPrsapqykpYUxYA=; b=pmQGkI0oTh34FL08STLDWYgIocTM8oab5FtbFaXx0OannfCpAZ37p+wWjN9fr9JvxQZ7JJoZhXMKqV1+z7Cj6FxM6LTeAYn/pYnsAHbRT7gpikxtYu31z+fR0dxGR48NskCfaBsPM61EPgoastkPqxj1AsRDBfoXEIiuhfJfusyscsq1wNFkfm8WQQDmcGyYvrm5xB35sw6R0wQP1Wx9FMOKvbVmgRuxFVba26jw+IW6y1MyljXpip3UElYKhgdAHA3brTY377aJooQdFkQ0c3lts3gq4+hI8dwrr1AY/l2JD/Tafa1ZS26G8YkIgmAU45+CT14S+Ow4Ugob9oaM+w== Received: from DS0PR12MB7726.namprd12.prod.outlook.com (2603:10b6:8:130::6) by BY5PR12MB4132.namprd12.prod.outlook.com (2603:10b6:a03:209::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8335.10; Fri, 10 Jan 2025 06:02:20 +0000 Received: from DS0PR12MB7726.namprd12.prod.outlook.com ([fe80::953f:2f80:90c5:67fe]) by DS0PR12MB7726.namprd12.prod.outlook.com ([fe80::953f:2f80:90c5:67fe%7]) with mapi id 15.20.8335.011; Fri, 10 Jan 2025 06:02:20 +0000 From: Alistair Popple To: akpm@linux-foundation.org, dan.j.williams@intel.com, linux-mm@kvack.org Cc: alison.schofield@intel.com, Alistair Popple , lina@asahilina.net, zhang.lyra@gmail.com, gerald.schaefer@linux.ibm.com, vishal.l.verma@intel.com, dave.jiang@intel.com, logang@deltatee.com, bhelgaas@google.com, jack@suse.cz, jgg@ziepe.ca, catalin.marinas@arm.com, will@kernel.org, mpe@ellerman.id.au, npiggin@gmail.com, dave.hansen@linux.intel.com, ira.weiny@intel.com, willy@infradead.org, djwong@kernel.org, tytso@mit.edu, linmiaohe@huawei.com, david@redhat.com, peterx@redhat.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-xfs@vger.kernel.org, jhubbard@nvidia.com, hch@lst.de, david@fromorbit.com, chenhuacai@kernel.org, kernel@xen0n.name, loongarch@lists.linux.dev Subject: [PATCH v6 12/26] mm/memory: Enhance insert_page_into_pte_locked() to create writable mappings Date: Fri, 10 Jan 2025 17:00:40 +1100 Message-ID: <68974d46091eea460f404f8ced3c6de5964c9ec4.1736488799.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: X-ClientProxiedBy: SYBPR01CA0163.ausprd01.prod.outlook.com (2603:10c6:10:d::31) To DS0PR12MB7726.namprd12.prod.outlook.com (2603:10b6:8:130::6) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR12MB7726:EE_|BY5PR12MB4132:EE_ X-MS-Office365-Filtering-Correlation-Id: 753297fb-2285-4134-462c-08dd313c582c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|7416014|366016; X-Microsoft-Antispam-Message-Info: SyS84vGNBXVO4ENNtxfNS8fUYSJjfR04qWa0gcuIX+jqM5+nfpUWDBHzx8OUZenh0U29OD8EXDyNDsIZVDU1zGQ37fwuhiRzxHX0ArDQsuDW0mOU19J22DVntIXgYAsqjMuLofhCu4OjmtfCBIY1TjrfWKPds8OXOMpUhZf5iqwkI2SlgIjpKhqkqVMzER7Xxan0m6MMMUdEvoeH5/0s81KK3eocfZCRxoXTJhXdd1NKxc2/dja2sgmbCnH9nejlJZFfGslnaiqNlTr5M280bMuxMlnzptCB/HHIWPjCwcHyZG9q1Bgv2Tc6wPBd/xeCE9iJJAyB62UnLG4q88tDHmKcNfCSq7wLrAxTynV+MeZNPo7PV+MD9zmMKaCI+iwtxMKSBBekEmJFWnG+LzJG9ln5rXOs72kdS8Ra4OcOhfej1JYg2yJ/H4rlC2/D/r/LBvczVxlJthGcT7dhamq48Oc376eMiQZeJyNj6mw+EoHNPy4IVo14XMthpM62LW7GgQ7CxFAZA0cF4ycuJpQVzfv+F+fR4knw4YeFcGqzguE98SpGjeuByyOFWI0/KYI9S3jRzKjb0MNMqL+vF0LzCL8uxjeyF2T2OQr5uSjQiW7oXnXOZyuCI611TbAds1RSkgx8ZktulJLOAtpngik4HydtXVMjcFdoR7J5/NYLbx8gBiF3ZEqDkRJ/FeeZGgISWKDRj/ZemvDDuJzUdTje4ihu8zl4i+o/pJWES1/+eqazK7crqjXu3rj4SSHxsiqVl2WxI4sqqWkxlb/AodLSe55mz1r/UNFxsrv2kSEJ6W+ETZWai23iH/fs8Lf+YHECPoy3oZ/BQMAMvBG1OfbJCCTPSqtzZGwbJTyBCdv9Lp4NmyWoepCdLRnlsK37xYtToS1Bm3QABEhh1Z2Ak5eqyYwBW0ErbFqYIDHNa0RxmA7TvbOjYvCye/HgWHxWPoW3/+UlrtNi0bXxtVzG4ELE0gHE+3YQzs13yNsqx/AJR29sB0abfRvrvxo7hy9miRMCZ+eBCLnAEuwuG+5NXND47+NgY+4NzVjmCaGAOLDAroP/qvmDMTZrSI6LY+NPDeFFvexdTHdGaBVF6V1GgGULUnnYlbGrqu1bNAAGXzT6txMZ5yuJwNwykf4NITl+uD1TaartzUlVmyXAKsECPqAKZ2i+SieE4XkC2W0oLaWG3kscOUUk0YyhB9NhJaZ6UTKI+pHbG2jKxePNUraVKnh5Bt5hdrJxvsF/TW2MiFgySgQx9bmPQJgRMjvKuvYP8PuEaBSahQFstonJrJ214E0AvtVNQxzRuZp1WFp+hi5oCPv0xQSCDy//WMBwbAZ5MoWjF6wItjnV6MTY8LKPNVqxhMQSPhNgjhTlj6Iog6rDrJtFR05pMR7c7qKHfsvmHu4F X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS0PR12MB7726.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(376014)(7416014)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkC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03lo+tQmZ9JgkHJg+VPu0Xj9ISxu7g5YI9xcaCZ90/ZL3sDLQVhEEz29RsVyluhLjTDxKBajhC3C2GLEyy36fshJZY8UcTXiT9RSXsWHbyg+PiriGSxoUC1ZV+YAKRLh7jbHNBQP+iRRBgT2Udes7dZMmKuNzCPzWWaQy6DLGvbRCSgfcElOsYvSblVGgwq2pFaL+WVwztT3t1WFU5Y2UmD+WKVPumLPHW3wWHdygeM2O+/a+XzuFFbQBDwKN1BWmBTAW2Llm+4zTkfMkUiWUcmupP8n6z6451ddJ6fRsGTcwijNjErUfKzZI9GRvdrcWp9mH4JEN102vtGQnyQxfDOxPMZySP2dsAsEoTIJ11N6SW5dEpb+RYOgu9A3HmsEml9wIbwYP9UZI4mn4YUTduisRSmr6YXWrO5YYahGAHLjkWhSugtLoRfovqjVnPwhVs1abgkIpbBoVLnwfKtW+0Yf7Adl9VUDThcKjP0s6CtmQsFyqOiQpLyTIqlEEYec5kBOPuY/vjofuKTg8nsY069dQrTUsaXzLpcz X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 753297fb-2285-4134-462c-08dd313c582c X-MS-Exchange-CrossTenant-AuthSource: DS0PR12MB7726.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jan 2025 06:02:20.6797 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 1STWTcYIrXMOy/zmLCZsPE/VLFA73pcOuhsPGr+iAHW1mcTHf367ZykIwAHfmomQjeXnJo32M8vf/ta2TzbJOA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4132 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: C93651C0006 X-Stat-Signature: sso8g3zcs34fbj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cAgns19rsHEpZuBRdd/pMIEnwo/z4bwuxycLKJjucKovqaz1ol25Q0URUB5C8nzDtulreP7hT8Wk6Kc1A6j2obZRLEiHARJr04uaEljHWKZpHBKklneqHF6yIxy5XjJbf03A1OfVy2EGRPEiIy7L2UYhtP+khkMPSZHL8G1YgzYotD6S6TGJH8smbAn4gEizbtUF4vI3UeYGDA7YvnXpRnmkp0hMjj4zdp6uiMGdmbbtO7cyF16PrcXdsT0M1XvoLvC6rUY8Eu/S8QbFfSce9Q3xWf1NdHkRNv5B1ZYq/E7hGxhswJ5Luizl3J7JHMPLqNXQdCEhzkOF2HpGzcUZBq61s6qUjfYDKHxF20m3gD3Mu3FgiBmGyGlqnb2cbmYhrKK7FYTNu7XnwvplnmWphav8kMTR/7hu5sotBiY3O2D6PgwIzNHepUvKPq/ElUuhHk8egnrFNs= 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: List-Subscribe: List-Unsubscribe: In preparation for using insert_page() for DAX, enhance insert_page_into_pte_locked() to handle establishing writable mappings. Recall that DAX returns VM_FAULT_NOPAGE after installing a PTE which bypasses the typical set_pte_range() in finish_fault. Signed-off-by: Alistair Popple Suggested-by: Dan Williams --- Changes for v5: - Minor comment/formatting fixes suggested by David Hildenbrand Changes since v2: - New patch split out from "mm/memory: Add dax_insert_pfn" --- mm/memory.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 06bb29e..8531acb 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2126,19 +2126,40 @@ static int validate_page_before_insert(struct vm_area_struct *vma, } static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte, - unsigned long addr, struct page *page, pgprot_t prot) + unsigned long addr, struct page *page, + pgprot_t prot, bool mkwrite) { struct folio *folio = page_folio(page); + pte_t entry = ptep_get(pte); pte_t pteval; - if (!pte_none(ptep_get(pte))) - return -EBUSY; + if (!pte_none(entry)) { + if (!mkwrite) + return -EBUSY; + + /* see insert_pfn(). */ + if (pte_pfn(entry) != page_to_pfn(page)) { + WARN_ON_ONCE(!is_zero_pfn(pte_pfn(entry))); + return -EFAULT; + } + entry = maybe_mkwrite(entry, vma); + entry = pte_mkyoung(entry); + if (ptep_set_access_flags(vma, addr, pte, entry, 1)) + update_mmu_cache(vma, addr, pte); + return 0; + } + /* Ok, finally just insert the thing.. */ pteval = mk_pte(page, prot); if (unlikely(is_zero_folio(folio))) { pteval = pte_mkspecial(pteval); } else { folio_get(folio); + entry = mk_pte(page, prot); + if (mkwrite) { + entry = pte_mkyoung(entry); + entry = maybe_mkwrite(pte_mkdirty(entry), vma); + } inc_mm_counter(vma->vm_mm, mm_counter_file(folio)); folio_add_file_rmap_pte(folio, page, vma); } @@ -2147,7 +2168,7 @@ static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte, } static int insert_page(struct vm_area_struct *vma, unsigned long addr, - struct page *page, pgprot_t prot) + struct page *page, pgprot_t prot, bool mkwrite) { int retval; pte_t *pte; @@ -2160,7 +2181,7 @@ static int insert_page(struct vm_area_struct *vma, unsigned long addr, pte = get_locked_pte(vma->vm_mm, addr, &ptl); if (!pte) goto out; - retval = insert_page_into_pte_locked(vma, pte, addr, page, prot); + retval = insert_page_into_pte_locked(vma, pte, addr, page, prot, mkwrite); pte_unmap_unlock(pte, ptl); out: return retval; @@ -2174,7 +2195,7 @@ static int insert_page_in_batch_locked(struct vm_area_struct *vma, pte_t *pte, err = validate_page_before_insert(vma, page); if (err) return err; - return insert_page_into_pte_locked(vma, pte, addr, page, prot); + return insert_page_into_pte_locked(vma, pte, addr, page, prot, false); } /* insert_pages() amortizes the cost of spinlock operations @@ -2310,7 +2331,7 @@ int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, BUG_ON(vma->vm_flags & VM_PFNMAP); vm_flags_set(vma, VM_MIXEDMAP); } - return insert_page(vma, addr, page, vma->vm_page_prot); + return insert_page(vma, addr, page, vma->vm_page_prot, false); } EXPORT_SYMBOL(vm_insert_page); @@ -2590,7 +2611,7 @@ static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma, * result in pfn_t_has_page() == false. */ page = pfn_to_page(pfn_t_to_pfn(pfn)); - err = insert_page(vma, addr, page, pgprot); + err = insert_page(vma, addr, page, pgprot, mkwrite); } else { return insert_pfn(vma, addr, pfn, pgprot, mkwrite); }