From patchwork Tue Dec 17 05:13:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13911152 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8D29AE7716A for ; Tue, 17 Dec 2024 05:38:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:MIME-Version:Content-Type: Content-Transfer-Encoding:References:In-Reply-To:Message-ID:Date:Subject:Cc: To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=+RK8HcctKWrWGjHAxyN8k6xlAbP24zFsv0TFbZlW19A=; b=u/iKI4N4BqPeMU3ORHnJQUvu6F cvLD+HG4U7vuJS41dbiZfPNaTtKbRIFeiyR+lDaGrtMm0382kg9dpR07YVrbXeJw3ZqOHSOaUWnZx znzlKQHuY8kb1bMtBnDpW0tHD1te5u6aool0BQiNn6LBHJ2f2yR/fNkL2scMtqjuwQcebuaB95gL/ W4o38TZC1EVHAN5VSrCm0gVI1IBaY4/ARhpPBj7WXKxAngM4GnhYQY8vIzsY/bQPHxqRBPPdi6ofd vFf/tkCGO/lp6d/z2GUP59h2T64LwZ6DGCJtPn52l/vs0QfFn3gSgtkbxF+9ETlplbU3rPWizEJOc bcEl0a/Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tNQI6-0000000CJgv-2uR1; Tue, 17 Dec 2024 05:38:42 +0000 Received: from mail-mw2nam10on2061c.outbound.protection.outlook.com ([2a01:111:f403:2412::61c] helo=NAM10-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tNPvj-0000000CDsT-0rmy for linux-arm-kernel@lists.infradead.org; Tue, 17 Dec 2024 05:15:36 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=qF57srFH519e7P7RO2dcihHdh2eL/xzvm+2xqN3hIX3cH0nq4qIA2t36hnjBJRCN5sDJbcX9qdW9d/L8+Pfm3hz96U4wtE4/ZL/IIr8JY1f+or2vsIhiRJXgAmWh05fBt3TlYpexGnk2+5E8kmnqhZ5bA78Zs+eTthMWxVlw5keq02O9p5v6Y8aq+95pgGQ3MgXYkwx3xkNdx/SzrEcupPPREwPw/ZDp//ZByI/zM32TrSqLydRhHSWTygGKWi1PKS/d6RLXXPUB26olk79bE1dHdAHYTFnXnRZfY588Dglg2Wswtm0fx2WBLs9np1jGocOpRCPB6gBOHRZCsl8XLw== 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=+RK8HcctKWrWGjHAxyN8k6xlAbP24zFsv0TFbZlW19A=; b=opXrj7SbXd/MoF4o2gSLxss0S9/bWUkozbovykOXmWCzVBUeW7O35zUjU03tBV1/0UkKbSVxDZq6ndXiHakx8j8yNcwZZbZtj5gx1C8UjUagMshsJR/6SRH0nAaN/kvWj/UEmZ5ScrmjA5kwoQnLcgPI8qfiaXZLNagHJt7T0tkD9y6gtBcOWlbhXwp6khfEY4SB+02M2fQG0vRlDZ8eJdNAxbx2zo5Bl9wtXA7h0k3HL+Zgk7iRLmNRPxW8TqAn8IfFTplpGfpM0+c7vV2ZdfYYdcI5OA3p/lcQgcjSaEXt0vwbHE312ha8mTJ5IknbvLQF8cTZ8KxWKELbXGJ6YQ== 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=+RK8HcctKWrWGjHAxyN8k6xlAbP24zFsv0TFbZlW19A=; b=pxrbt5laaiD4HOnHxQi8mKJNCZkAbiIdFKK/H7B9luGn+IDh+k2ZqzwNLevYX+lvqw6fqVlutYdjLqi5PFVubkLT8sGBD128pHtQ9ZOC3Vq5qM8hENP3FgAj2CLS9eDLuN7RqZUOH1aSsBrOr9pce4XqWRkR6Wmhe48DtNtyI2orqWv+nohYbaT06upxujl/xBIKPd04l+Ri7PRAEH6wEfEvJCeVozqNa2+JeNBgmd9uqP8wTpd5eY7RTHX3oX/DDZ+lprEFnpsu0x+556oMTRcbpqizlu0gtU9xY6m0A7dUXxKSWuQvKnABOYIDMbzvP/zFt/UuC9trHUoIlHYpvg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DS0PR12MB7726.namprd12.prod.outlook.com (2603:10b6:8:130::6) by DM6PR12MB4388.namprd12.prod.outlook.com (2603:10b6:5:2a9::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8251.22; Tue, 17 Dec 2024 05:15:32 +0000 Received: from DS0PR12MB7726.namprd12.prod.outlook.com ([fe80::953f:2f80:90c5:67fe]) by DS0PR12MB7726.namprd12.prod.outlook.com ([fe80::953f:2f80:90c5:67fe%4]) with mapi id 15.20.8251.015; Tue, 17 Dec 2024 05:15:32 +0000 From: Alistair Popple To: akpm@linux-foundation.org, dan.j.williams@intel.com, linux-mm@kvack.org Cc: 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 Subject: [PATCH v4 22/25] device/dax: Properly refcount device dax pages when mapping Date: Tue, 17 Dec 2024 16:13:05 +1100 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: References: X-ClientProxiedBy: SY5P300CA0022.AUSP300.PROD.OUTLOOK.COM (2603:10c6:10:1ff::12) To DS0PR12MB7726.namprd12.prod.outlook.com (2603:10b6:8:130::6) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR12MB7726:EE_|DM6PR12MB4388:EE_ X-MS-Office365-Filtering-Correlation-Id: f81d557f-2c0f-49ee-8a3e-08dd1e59d483 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016; X-Microsoft-Antispam-Message-Info: zq3vf2bInKE36eN0X/iCid4SIT2hITWwMoWuLPtxLWILIM04N4dIfrSPcDS114GHgBtpICnF8GNjXjt2yrxg4Ak2UR0t2bH95wLGtBCRw0I8Y8DuTgddQJO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zKn5ZHw+Mi6ca+bAeOCwIIQn/enZa2Z/DLl1wk2q61kuyfBTKMlGQAWoZMbq8zJKrrezMLIEn6IKd2SOMH9G2yoJBsxO7ytCKnHjm/gk+xAQVSkgcQLtoiKgMfgcrbe42V0wUCv3RDkOXoa0DJ6uYOIyqbkXhV6mHGF6MzxkBIH1XsEQJpvWFYMOBwaWBrFTZSp/DobPJ+ysk2ndSOjJo 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)(376014)(7416014)(1800799024)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: LX9GV4FQNB08hEnE728R9r9FmMdM5tA6IFDivr6aQnrqZOv2BlQTjou++q04hEukp98hy9JM1cy/oZVMdEqs1IRVSTx8X0+KCcDZg/Qhbm7E2quiSgzm7msrj8jysi0Da2ZW45EGT+e0lO+deE0siNAWstXPgkHuQfTEs7mM7oJEws1xX6Pr6+IiLTIILMSi1PpanneiobfKWtWOmz9JbpWtoU7p/tUH8S5w9IQ/30KvtQqYmaNcSFFesf5Yu6ScyX/5wGES4PInCiAkzqYLEATfuBhpvKIZMCSQIeXNOSBTt2lL8b7Nyj2lYlCkzNDhrpL+hiqn5+/rqFu8wCIIOoHEEoD4nyPqk1t4hvfCPoxywpKHlPxC4OBFDOBgpWrVaQEpx4oCLYyFBLAueLTUHDNDLDrPKqABQocuwZDit6YADNKRfNhJH7aaT8MvqQfl+yWHUd7rMILy2j+7dK6jTM5LFkErJaES164oPoF7TdBiHvQPziLRAziX/zhIq25mh+O4ThR08Ei05ybGFdTyrcnMLt//NUiZTJkcwoXrLc1AJbnlAZBKl09VVQxsOCI1wZZcGTOfGEegES0SP2Bq0NBw5CthwOKIAiAkqdDI6nyHqEd0zJ94ummroixGS/7hvWVrVUY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namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Dec 2024 05:15:32.5649 (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: AsFC4umf4Px8cqi6FppADGLNXIEVP5M+7IoaVKLfevtAv3PjrDn9U7axRgZeeaAEgBbP66jCjvlqiA41LrAXzQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4388 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241216_211535_241300_C4244EFA X-CRM114-Status: GOOD ( 11.45 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Device DAX pages are currently not reference counted when mapped, instead relying on the devmap PTE bit to ensure mapping code will not get/put references. This requires special handling in various page table walkers, particularly GUP, to manage references on the underlying pgmap to ensure the pages remain valid. However there is no reason these pages can't be refcounted properly at map time. Doning so eliminates the need for the devmap PTE bit, freeing up a precious PTE bit. It also simplifies GUP as it no longer needs to manage the special pgmap references and can instead just treat the pages normally as defined by vm_normal_page(). Signed-off-by: Alistair Popple --- drivers/dax/device.c | 15 +++++++++------ mm/memremap.c | 13 ++++++------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/drivers/dax/device.c b/drivers/dax/device.c index 6d74e62..fd22dbf 100644 --- a/drivers/dax/device.c +++ b/drivers/dax/device.c @@ -126,11 +126,12 @@ static vm_fault_t __dev_dax_pte_fault(struct dev_dax *dev_dax, return VM_FAULT_SIGBUS; } - pfn = phys_to_pfn_t(phys, PFN_DEV|PFN_MAP); + pfn = phys_to_pfn_t(phys, 0); dax_set_mapping(vmf, pfn, fault_size); - return vmf_insert_mixed(vmf->vma, vmf->address, pfn); + return vmf_insert_page_mkwrite(vmf, pfn_t_to_page(pfn), + vmf->flags & FAULT_FLAG_WRITE); } static vm_fault_t __dev_dax_pmd_fault(struct dev_dax *dev_dax, @@ -169,11 +170,12 @@ static vm_fault_t __dev_dax_pmd_fault(struct dev_dax *dev_dax, return VM_FAULT_SIGBUS; } - pfn = phys_to_pfn_t(phys, PFN_DEV|PFN_MAP); + pfn = phys_to_pfn_t(phys, 0); dax_set_mapping(vmf, pfn, fault_size); - return vmf_insert_pfn_pmd(vmf, pfn, vmf->flags & FAULT_FLAG_WRITE); + return vmf_insert_folio_pmd(vmf, page_folio(pfn_t_to_page(pfn)), + vmf->flags & FAULT_FLAG_WRITE); } #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD @@ -214,11 +216,12 @@ static vm_fault_t __dev_dax_pud_fault(struct dev_dax *dev_dax, return VM_FAULT_SIGBUS; } - pfn = phys_to_pfn_t(phys, PFN_DEV|PFN_MAP); + pfn = phys_to_pfn_t(phys, 0); dax_set_mapping(vmf, pfn, fault_size); - return vmf_insert_pfn_pud(vmf, pfn, vmf->flags & FAULT_FLAG_WRITE); + return vmf_insert_folio_pud(vmf, page_folio(pfn_t_to_page(pfn)), + vmf->flags & FAULT_FLAG_WRITE); } #else static vm_fault_t __dev_dax_pud_fault(struct dev_dax *dev_dax, diff --git a/mm/memremap.c b/mm/memremap.c index 9a8879b..532a52a 100644 --- a/mm/memremap.c +++ b/mm/memremap.c @@ -460,11 +460,10 @@ void free_zone_device_folio(struct folio *folio) { struct dev_pagemap *pgmap = folio->pgmap; - if (WARN_ON_ONCE(!pgmap->ops)) - return; - - if (WARN_ON_ONCE(pgmap->type != MEMORY_DEVICE_FS_DAX && - !pgmap->ops->page_free)) + if (WARN_ON_ONCE((!pgmap->ops && + pgmap->type != MEMORY_DEVICE_GENERIC) || + (pgmap->ops && !pgmap->ops->page_free && + pgmap->type != MEMORY_DEVICE_FS_DAX))) return; mem_cgroup_uncharge(folio); @@ -494,7 +493,8 @@ void free_zone_device_folio(struct folio *folio) * zero which indicating the page has been removed from the file * system mapping. */ - if (pgmap->type != MEMORY_DEVICE_FS_DAX) + if (pgmap->type != MEMORY_DEVICE_FS_DAX && + pgmap->type != MEMORY_DEVICE_GENERIC) folio->mapping = NULL; switch (pgmap->type) { @@ -509,7 +509,6 @@ void free_zone_device_folio(struct folio *folio) * Reset the refcount to 1 to prepare for handing out the page * again. */ - pgmap->ops->page_free(folio_page(folio, 0)); folio_set_count(folio, 1); break;