From patchwork Tue Dec 17 05:12:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13911088 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2085.outbound.protection.outlook.com [40.107.223.85]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A081F1DE2B1; Tue, 17 Dec 2024 05:14:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.85 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734412471; cv=fail; b=RHO2x7R1hFPbsOXtzjRjYcz9m838mlVSSRcak3QdBsQMwS1/TD1DLzIYwuPSd7urlE+SGXHLDSfqrHejnlWi38w2cFhWpYrsNhw3f7tUkzblf81hY9lLinMLs0je5+r6HCNoVQY1NlraoLIwk8vOYTmBVN0PliORNwHGgo51Rj4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734412471; c=relaxed/simple; bh=/9nCN1XQPfgyA5VlyoRarJnrFqQw20cJtW1L8P7Lq0g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=YiyoWWK8TZdMNDFXW2fHKcpDxsv1qt72HK2mhIZU7gysLi7rHZBJH3hcDt+WXJgJJop4UGb+lbE2tw/fJqLlV4+sAEoTQ1I1EdheZ1HebH7WIwhzoPA8z6VIlSotNF29UATTkUP0oiP0/+RUSq9Z5tp9UfQMX21/9kzIcZz6hb8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=LLSmlK/L; arc=fail smtp.client-ip=40.107.223.85 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="LLSmlK/L" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Y/O4KcL0S2UBwEvonloLZ0oqEvS4VcJEEi5ZaCm+pibJaOF4EumsRJpI/JhPllMLdGrw6Qod4g99nwjzf95TqhuEIM/Mo+n8pLfn5xNqOacucjjpRajJOQCV8M/3od6uRHJwtwxPPSrn0n/xgALMHQD38L53fRwTNi2B1kxQr1ZhlpSPcxJFbVrx2BHIjEDoreRmHpryKoH2Ui8Ca1fjdMelgWJ97rzWtVKRERt2G+e8XyW8FO4PiWosDnSjRy7Ndllo+tKkLTeJopAH9yz/te4yGecUAsfzP0uryQ67+pRD1N8+qbTgNoGk4MJHVFn7CA2SXMD0loaoQ00FK33KLw== 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=QaSh+cBcmD4fAyVgZBd8ptmSZOPC9ILTc9cpqUKZh8s=; b=YMNwrH1EXtt9QQDyu+i6BS7A+r5bX+0BIQAXem1wfrogVBzKR+W0B3PUFjKS2IJM0dR0UR84gRfHf7CxXrREnRk3E7wqfGYxtU0LOnRBUXhvn9hz3t2J3d4t8yqeZ4QzOzJudcX2HqcLcfs1e0UYe2ZwERg/p/ChwJsdNqosmrRrTIsXK6tUide4oKnsL+6JMvDuRpQl3kRxVTMUKfjxx+GmTPCwE1hDWoD/i37z8iVVLzw5yMSTyhPmpfbpnyIrG81HYApr0/Su27i84thg/t/Gxq4Udg8o2cgoQZGf4qXObeitmwAhrB0hj6h5QXM2ijAfQoTLsbBEzbsMkR3P7g== 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=QaSh+cBcmD4fAyVgZBd8ptmSZOPC9ILTc9cpqUKZh8s=; b=LLSmlK/LjITICPJUbSqXY1OE7ZL9gTscmjMIT0yNzLHeXIXpV71erhtBdjIdEOGZhmwCoB4U12NoeZPub685r1AEVpe6W5FzsRKR7HU+/0SXko6Dn+e9vXkoe0JwL9S36J//2PUMDBot0XyfOZITQot1Ph8Lcuabqr3wfFG7ewbJ2a9F7qj4EfGHrVIS9VcZ+mMP5IlluYCTrUcj9mg2bhrWz7wRACrn4xozmeZb9qXCoE33PFwR2/QuOJXzDMqa3O1GQrGBElzJQ9WVFfP2CUbUeawPXFjq7xS/oarq8FrLZsj8CoARMk/nCjuQYBbr5Rvf20yRCl3A83WQ5+boGw== 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 CH3PR12MB8936.namprd12.prod.outlook.com (2603:10b6:610:179::17) 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:14:27 +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:14:27 +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 10/25] mm/mm_init: Move p2pdma page refcount initialisation to p2pdma Date: Tue, 17 Dec 2024 16:12:53 +1100 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: References: X-ClientProxiedBy: SY5P300CA0005.AUSP300.PROD.OUTLOOK.COM (2603:10c6:10:1fb::6) To DS0PR12MB7726.namprd12.prod.outlook.com (2603:10b6:8:130::6) Precedence: bulk X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR12MB7726:EE_|CH3PR12MB8936:EE_ X-MS-Office365-Filtering-Correlation-Id: 5e3fad04-c653-4db0-dc1a-08dd1e59add1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|1800799024|7416014|366016; X-Microsoft-Antispam-Message-Info: qZmYDa4cXf0EXnax6NFfPTUNdIDNBChsELUiVcoBHSWM1WO8iTOIbw9dbp37yGkBtDPV5Ubt31WqJn7Sfm0pTqrr1qL+22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fhmE2jpFHWT1p7fztdBlmS2DlDdOf8K9fdMX4wnyVEYfctbWudBDB23mDOQL+NBlHCrZlb5YhR5M/s7n+uHUiJTXuz722fugPK8oz07Jvntbt8lWbjsix1Vr/CjUfmpXImLPm9BRPMiuNFw7gN8FeBlh056TfMhpox6jlGPxlL2wXqEZmSz7zupJ4u5kctR332wQVHZWkHLGMcqvk/4hiS8FldmUJyU0Jfa7sW 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)(1800799024)(7416014)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Fnxc5f9Cjr5mEiK0BtcOITZuMYawuMOYmGZZRMUoiwXolXLT4BrVkyNlkAueQbwyw7dR0hprz5kXX6XVpLf8optoZeQftO5nh8Xw03KpDfD/dHTg/+gC0SzKmzPgReZCnf57bcri91qpig0yi6lbsUb8LF0FgCQkZqjcfRYi3n1MRE6piBtW18MBaxtjU4ihBtdy31s/MKUhgx5FTvjXw7x4w3ZUyMtmv3pZFPKsa0hACiUQFQyteKPOSOo/kdJVC4mhX2ogP0CzbnIYPySCiV9Afz+q4kQWMsSMZEOCoSI1brqTk/v8VZNGmRvYuViNFF+Ksl0UkA+t5ZU+zjfasgwfKB7eLiZfygkXRqIK2JwRNaNyjpT+49GTP/HC19ILUODR+TI+Qw6yqyl4FOPJjuEr7SRtEh00QsY0kNB8QATvfpz7vYobiQiQsbJxnPigZjr8QQW42WIqJKA/F0tByNHtAXG9rG/2HAQvWK/yldckhgACJohO6y1wOs5/CCfCKbx2JyHEhT225sKTFSFuTRviml2z1MSraLdHmnKwHy3tEYNNFBWyzalkktsuN0CYTq7sya/u9U9Hd7Upp+smBMplW0EdRQX1GIWvHdiE/NTefJakqdmfqT/qEIJ7AabAGDAau+/dDiUAlpMF9NDrq5M4zeL2CAZD3/HMCnMFfiEeQCMB2IEYxT6cFcksyU4/tBVaHLDbu8EwA5+G8IzFsSfLCenqYOveIuf9//1KdyW3p1m1lF30IqXJ896e1vYdNFcurMAaow3VIuArJd4pr98OS/WB3OLue44lLQDAatm0NRciynofjmRIHRbwPTKNGslKnX5PiRrc7MKkOJMhTRx2TCPHZB58ltnmiLVgmd/BlQ8T4yr5VZvX++i0woODtUd6cNJDwS2eo2CJRE8Tm4VjWJ9yfeE8sPYrjlILvj4l/ORYNThhQowDehZEE16FEp/TOYD/1l9aUnS8gGYV8QVjUo9gu7nJ27CvCH3tytgLWCCiHpDt3heJ54hXNrQYylj3U4QmY1k8pDxNWiLIjV9gSE2q9Ds4o66mI/QnKP9kYLnywULuWIV3n4smxUsOLkHZHMa0/draGFzywU4TXokfW26YPH67XuOF92tdBnOjB/bWWtOUGyXRliptu/SfWpG3jLV9srmys5xQLtMs9y/6XCS+gtBrAs/DvfkJ4IDfJ7sNP7JKbiyGhmwOyR8k1MV86UVt94vdWdX04ZmXdhpTD60iZrpPt/THLwg34VMaN1w3E6VxpzBjCJRP1sF+yWRrD2cd+d7qppfxeByKWJ6/6SeKkz/05s9pVYrXw+FYHHDleYJk4eM/WvgQAXzPHOhfwgs/nyR8RJJAOMNzF5Gb6J+Og2gDi9EOIR9exLsXhPlnReV01n5K2Rc/GxDchVPMDRPycbc4rHcmLRM9RRxVdy1yAs2JnbLemLyNIVrgIkX9HmUVQtHhMbun+glmpSnxEkkwAhnNEMNEuhYj3EcqyxQBRtimW+9IVVa5DPn/0BL34iANqgp5qvyqg7RurTDiIyrKqnYzd1nPV6OOa6cumXuNLwzOhiRvg1yyyE45JsNxo7zYecQLHvixjS1a X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5e3fad04-c653-4db0-dc1a-08dd1e59add1 X-MS-Exchange-CrossTenant-AuthSource: DS0PR12MB7726.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Dec 2024 05:14:27.6502 (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: /7IU1rlWvaFoKdupaHWizYoZo6EMMKaxzBW2umaJWGpoJPYPbVha0pm/OWAGAFymJZy1Rzn7jGIn0FCzYu2i8g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8936 Currently ZONE_DEVICE page reference counts are initialised by core memory management code in __init_zone_device_page() as part of the memremap() call which driver modules make to obtain ZONE_DEVICE pages. This initialises page refcounts to 1 before returning them to the driver. This was presumably done because it drivers had a reference of sorts on the page. It also ensured the page could always be mapped with vm_insert_page() for example and would never get freed (ie. have a zero refcount), freeing drivers of manipulating page reference counts. However it complicates figuring out whether or not a page is free from the mm perspective because it is no longer possible to just look at the refcount. Instead the page type must be known and if GUP is used a secondary pgmap reference is also sometimes needed. To simplify this it is desirable to remove the page reference count for the driver, so core mm can just use the refcount without having to account for page type or do other types of tracking. This is possible because drivers can always assume the page is valid as core kernel will never offline or remove the struct page. This means it is now up to drivers to initialise the page refcount as required. P2PDMA uses vm_insert_page() to map the page, and that requires a non-zero reference count when initialising the page so set that when the page is first mapped. Signed-off-by: Alistair Popple Reviewed-by: Dan Williams Acked-by: David Hildenbrand --- Changes since v2: - Initialise the page refcount for all pages covered by the kaddr --- drivers/pci/p2pdma.c | 13 +++++++++++-- mm/memremap.c | 17 +++++++++++++---- mm/mm_init.c | 22 ++++++++++++++++++---- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/drivers/pci/p2pdma.c b/drivers/pci/p2pdma.c index 0cb7e0a..04773a8 100644 --- a/drivers/pci/p2pdma.c +++ b/drivers/pci/p2pdma.c @@ -140,13 +140,22 @@ static int p2pmem_alloc_mmap(struct file *filp, struct kobject *kobj, rcu_read_unlock(); for (vaddr = vma->vm_start; vaddr < vma->vm_end; vaddr += PAGE_SIZE) { - ret = vm_insert_page(vma, vaddr, virt_to_page(kaddr)); + struct page *page = virt_to_page(kaddr); + + /* + * Initialise the refcount for the freshly allocated page. As + * we have just allocated the page no one else should be + * using it. + */ + VM_WARN_ON_ONCE_PAGE(!page_ref_count(page), page); + set_page_count(page, 1); + ret = vm_insert_page(vma, vaddr, page); if (ret) { gen_pool_free(p2pdma->pool, (uintptr_t)kaddr, len); return ret; } percpu_ref_get(ref); - put_page(virt_to_page(kaddr)); + put_page(page); kaddr += PAGE_SIZE; len -= PAGE_SIZE; } diff --git a/mm/memremap.c b/mm/memremap.c index 40d4547..07bbe0e 100644 --- a/mm/memremap.c +++ b/mm/memremap.c @@ -488,15 +488,24 @@ void free_zone_device_folio(struct folio *folio) folio->mapping = NULL; folio->page.pgmap->ops->page_free(folio_page(folio, 0)); - if (folio->page.pgmap->type != MEMORY_DEVICE_PRIVATE && - folio->page.pgmap->type != MEMORY_DEVICE_COHERENT) + switch (folio->page.pgmap->type) { + case MEMORY_DEVICE_PRIVATE: + case MEMORY_DEVICE_COHERENT: + put_dev_pagemap(folio->page.pgmap); + break; + + case MEMORY_DEVICE_FS_DAX: + case MEMORY_DEVICE_GENERIC: /* * Reset the refcount to 1 to prepare for handing out the page * again. */ folio_set_count(folio, 1); - else - put_dev_pagemap(folio->page.pgmap); + break; + + case MEMORY_DEVICE_PCI_P2PDMA: + break; + } } void zone_device_page_init(struct page *page) diff --git a/mm/mm_init.c b/mm/mm_init.c index 24b68b4..f021e63 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -1017,12 +1017,26 @@ static void __ref __init_zone_device_page(struct page *page, unsigned long pfn, } /* - * ZONE_DEVICE pages are released directly to the driver page allocator - * which will set the page count to 1 when allocating the page. + * ZONE_DEVICE pages other than MEMORY_TYPE_GENERIC and + * MEMORY_TYPE_FS_DAX pages are released directly to the driver page + * allocator which will set the page count to 1 when allocating the + * page. + * + * MEMORY_TYPE_GENERIC and MEMORY_TYPE_FS_DAX pages automatically have + * their refcount reset to one whenever they are freed (ie. after + * their refcount drops to 0). */ - if (pgmap->type == MEMORY_DEVICE_PRIVATE || - pgmap->type == MEMORY_DEVICE_COHERENT) + switch (pgmap->type) { + case MEMORY_DEVICE_PRIVATE: + case MEMORY_DEVICE_COHERENT: + case MEMORY_DEVICE_PCI_P2PDMA: set_page_count(page, 0); + break; + + case MEMORY_DEVICE_FS_DAX: + case MEMORY_DEVICE_GENERIC: + break; + } } /*