From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=40.93.201.30 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796108905; b=OM3q32jT+/ZPQx0Q/0f0Tcln6zpoA1k1+ZmggOFV6MNEdMdKK1uD31/G+jHsUVMHUTCI 83IrUKbjZVMd/MABRFfglODPo95Ay/U9+XFR0yI9bTR6w0QAC2I+vS3JHEz10lgpJYA8f UUzYYAFgm1Pj0XA2cwGA2R0yh8yKczTgWt/hc99LQ8FhM9jbPR1q5rgSjaw+6ONlpqpvr +5D/XqWMydMaXtJO7eKGkt78/xjZQdpAjp4+9VN7YJjzSh53rsY0jTrVhS15JHwgaLmAR L3wgC6jnjLhUIVoALgyAtDaddFzVVmOldfXPTBjledU2Xx1yfYvgVAYEyRxSIemIs5w== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796108905; h=DMARC-Filter:DKIM-Signature:Received:Received:From:Date:Subject: Content-Type:Content-Transfer-Encoding:Message-Id:References: In-Reply-To:To:Cc:X-Mailer:X-Developer-Signature:X-Developer-Key: X-ClientProxiedBy:MIME-Version:X-MS-PublicTrafficType: X-MS-TrafficTypeDiagnostic:X-MS-Office365-Filtering-Correlation-Id: X-MS-Exchange-SenderADCheck:X-MS-Exchange-AntiSpam-Relay: X-Microsoft-Antispam:X-Microsoft-Antispam-Message-Info: X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-CrossTenant-Id:X-MS-Exchange-CrossTenant-MailboxType: X-MS-Exchange-CrossTenant-UserPrincipalName: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=utpVECCuHX+LWR12iaveefmEf0AlHgRLLKRr8wLZ7rM=; b=4iTFGDtxLN6l1cFFFss2MUDkBURdZ3d8g/nzokRkzLmJi5VlEShm919zjMQs2TQ9fA6y swNvS5EIJtpBVRP+ZCnL15s0qamWArpvMsbMbBWvCJsVEpP/p/czmNA3DqjdcBvtU4MJU zVVC0cTJlQtJ4mOO2WUBEy4k92w8feQSAvgtogfu8mpjMCccrjWOEEravmo0LNUpv2vOV gXDwvhocdKOBHBbD/69XNnmKoNlKIKgs6S+cC95/GBWpBlhvCDNujHf+6Q6l209KzO41M jkig3iHhtp6HTGE1okFczyWW61Y/pHJlXkYY6spRwD0NMgkC5Wvdm9TucGb7hxVZFbA== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=oFU3+I2P; arc=pass header.oldest-pass=0 smtp.remote-ip=40.93.201.30 Received: from CY3PR05CU001.outbound.protection.outlook.com (mail-westcentralusazon11013030.outbound.protection.outlook.com [40.93.201.30]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 51343606A6AD; Tue, 1 Dec 2026 07:08:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 51343606A6AD 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, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=oFU3+I2P ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=B0EDuULBppYwwnhflY/hpJe89QFOX2Zeb6jLHQqQz/Z17rJrDGG30M0/LHDvD2+sOSqV+vXKQhock1tMmqAmmhv1HZMb59U/+iv6ji42F0KOvpgLknPVOetgJ0Fc8+CPElLrKMUXmZ9CNBv/QEZh3gAtU59uOIWU830NPe+ab6213XmMmSNhoOj/cYn45y+OiTrLY3PU0FD8S0ENqILHaco6AjQyoTAYVqSJLQPb83Ikh9GEIbb97EgkyZVHwlLT9esYSk/JCMYMHe365BAdMmoNXJVuJchkNQ9OA6+KXhaz1TBr8jRyAMFsNLKKbaOBa3k6HTwOx9p/ORYzJB2H+A== 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=utpVECCuHX+LWR12iaveefmEf0AlHgRLLKRr8wLZ7rM=; b=XrtdFIalKFhFSC/i2umvWlUrTXA+FLfi7bjs6GNEYfTesWRUTlSz2Gfu1CmToTynE+GIE5EtHu6sgEf0hZDg2xyuHA/rhB0H2sllOTKiwiho/oYkqkcEPneo8AMDpN0DKgEwseduBi8zmFcJVR/OEWp/lWkGaBP3jfU6VAYMK6Y+RrYaoHv+Eh9BRHiVDK+afnNcGABO/yXmPRTZDQ53DiYrbAgn6xud+ZNaP+w7jeIK1dqGCopOcZ3PJB8fFgqB/37QHbFiZFCMaotGa9m4UOeNs4aWYnbML/XnJBJr4wNQ795O6qgo1vmtrGgNzVg6KgnEQA3IXMKlhR18GGHdxQ== 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=utpVECCuHX+LWR12iaveefmEf0AlHgRLLKRr8wLZ7rM=; b=oFU3+I2PFHrYNZ9jReqhU6dXff9lfNVhIuLot96U30zMaEDck/TctVya3/d+pQFE+EeqaDRHogmnRHgNRWgfiu+A0rQiKuBpUgjydcwcFnNL55QmX2EBCZD2MuWW3PqeKMqhRojtJWnRw/JyS4kBhYHkezS36+f/hFPIK/Po13MGMSf0obOYsK812iQOYqaWvgzMqzAlr5lFdiGQ5GX4OCupgEwOFC3mF1dKjd3ouKK8IB9QrPInUl5gicC8Nphnt+eqAHGZVmezXj+ZHKhg5edHcYW2X9HLo3XpLhWAETh0acDY2HB5WY2dQnnnVJs8IunvhnEUCmDgfP1xsjLNCA== Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from SA3PR12MB598757.namprd12.prod.outlook.com (2603:10b6:806:52d::14) by PH8PR12MB6721.namprd12.prod.outlook.com (2603:10b6:510:1cc::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.662.12; Tue, 1 Dec 2026 07:08:10 +0000 Received: from SA3PR12MB598757.namprd12.prod.outlook.com ([fe80::e206:100:5946:a777]) by SA3PR12MB598757.namprd12.prod.outlook.com ([fe80::e206:100:5946:a777%5]) with mapi id 15.21.0688.009; Tue, 1 Dec 2026 07:08:10 +0000 From: Alistair Popple Date: Tue, 01 Dec 2026 18:06:29 +1100 Subject: [PATCH RFC 13/16] iommufd: Map DMA-bufs through dma_buf_iommu_map() Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-13-b672717c7fc9@nvidia.com> References: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com> In-Reply-To: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com> To: Karol Wachowski , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , Philipp Stanner , David Airlie , Simona Vetter , Rodrigo Vivi , Matthew Brost , =?utf-8?q?Thomas_Hellstr=C3=B6m?= , Jason Gunthorpe , Leon Romanovsky , Kees Cook , "Gustavo A. R. Silva" , Alex Williamson , Kevin Tian , "Joerg Roedel (AMD)" , Will Deacon , Robin Murphy Cc: dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, intel-xe@lists.freedesktop.org, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, kvm@vger.kernel.org, iommu@lists.linux.dev, Alexey Kardashevskiy , Mina Almasry , Baolu Lu , chaitanyak@nvidia.com, Dongwon Kim , Felix Kuehling , jhubbard@nvidia.com, Karl Mehltretter , Vivek Kasireddy , Xu Yilun , zhipingz@meta.com, Alistair Popple X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796108809; l=21432; i=apopple@nvidia.com; s=20261105; h=from:subject:message-id; bh=8+2BtWDJ4uRrg3XcykKaB38ajgXbqfbIrTlAsQ5XzYw=; b=O3E6YVfVcF59fVOdg7dSH3NFgCEM5UPNKwsgw6uA3vZgJPYGX3zzWG9HeK0HYtrmhSTb7cnOY 7IG618hVf0OBy601hD4hoI1UvHyG/QrjrWRIOOzEG9iCEr26dAGvfda X-Developer-Key: i=apopple@nvidia.com; a=ed25519; pk=WdWPT9rAq3YNfXldbGv1NSGVLGxzQGFGJvRUiShhNqs= X-ClientProxiedBy: SY8PR01CA0006.ausprd01.prod.outlook.com (2603:10c6:10:29c::31) To DS4PR12MB634945.namprd12.prod.outlook.com (2603:10b6:8:514::7) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA3PR12MB598757:EE_|PH8PR12MB6721:EE_ X-MS-Office365-Filtering-Correlation-Id: 269151ee-5875-4e19-b1e3-08df4f79db6c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016|23010399003|11063799006|261009223027099003|18002099003|22082099003|921020|10067099003|261028144727099003|56012099006|261026121527099003|261102130727099003|261026140227099003|261022075727099003|261021121427099003|261022105027099003|261102123227099003|261021123027099003; X-Microsoft-Antispam-Message-Info: tssbb5jTcplzpCMJrHaQGw2ZQ8+NeQp2N5mGe8SkzvLcXn4V4c6TusPZjosbJ6z2BKwRomS/ALx9Q7sLxMRGvI33xbPwCJi55J36rBbOu5RB/0a/SOBrzEn2H0z4qERMham1v7u1Xvo45ii5onuY4Zb1xDs04nz0DidcpERdHusN9Dde1Iif4oZukxhqcS5QNxn+/OG4N2Nm9YUgye+UiS9Z3E75LFken52RlMZn4qUXFH1jij96xNBnHO7qlpKCig0+cZCwZBk1EtsrTWPqrSPYgX2CtQvsUHyhdviqFwQX8ZupqvaFFA6rXesOYyR3mhABZWFy7vikLynHhlaMkUdx+JDaZviWiEwLbTZpj6OICu+n7HmD6mHek9TRAWFmYQ3cd8wO2Uzvh3k+fPfhSBzwNmYPMTVsftn/30UZvanA1JQ8rjFQgSaxyYiMwixRF8E2oBhC81TBBXpiJnF0tIow2IEiKscKcsna5G8qFEFIu0OQgmziCjepUDTGmB6Q6K4/b5ex2ylY4Ix/GKfj1c4woBuUew60aZBKcPp4DRe3zHXub2RuSvMR/S3BFaZp26gBXL1/cCa/CdbUAm2wXYdlPBAQdYX0UGmb9XiIqQBTOsKRsiSbpYneVLlqny/yPDUfrib+otJc3D16iXmoegQGpDLUlTf7/WpjIe/ATJzZJyqlx08HwWNWbz7FCUjRGFXawTuslX6W2cui4bPBMA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA3PR12MB598757.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(7416014)(1800799024)(366016)(23010399003)(11063799006)(261009223027099003)(18002099003)(22082099003)(921020)(10067099003)(261028144727099003)(56012099006)(261026121527099003)(261102130727099003)(261026140227099003)(261022075727099003)(261021121427099003)(261022105027099003)(261102123227099003)(261021123027099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?Y2JwWk1PRW1mNVk0ZHJsTVhHTGorVXBjUzFaT1lpalVrcVNpem1YQzhDQVQ0?= =?utf-8?B?ZEJIS2gzd1NWMG92NVRFMFBrOXh6aUpUazhvQ0hiTlVwcWlZYi9vK09KK0RZ?= =?utf-8?B?cE5lZ1lXcGNiZ2tBT0ZQbm5sTTc1VnpvQS9Wc1ZXV1JQSmJTRzhpOUlNOVJ0?= =?utf-8?B?UUthRzdiWWE0ejRqWFFTb254Q3UvK0xYVFAwRzdiQU1WWTdQSGVINDRNcVJV?= =?utf-8?B?YloyWDNIa3J1UVZzYTFmYllhdlEyNFREZGdaMnMwcCtBOHBHN09BRmNBdi9q?= =?utf-8?B?WEFNR2xHN041QkkrQ3ltZ1p1L0NOb241NFByKzc3Qkthc3Q2djFZY2ZJcEgy?= =?utf-8?B?TVJwODVQSyt1MDBWT0dqNUdkbUFGd3ZHOGI3S0Fpb3ZVM0tmdm12UG1YRnQr?= =?utf-8?B?K3BjMGd2Y3VRcVdleDZIZWpwaHB6a0lqbThjMHc0eGF2MEtJU1JOanI5WU1F?= =?utf-8?B?NFBxZ3FsUy9IbjBoNlM4aXljTSs4aklINzBQUE9JSm1vb3VNcFZpUmhoQ21N?= =?utf-8?B?aHhFMnRzWkx1aXVVb3hCYUY0UGQ3UEU4T1d0eHBDQjRWQjJVSi9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-Exchange-CrossTenant-Network-Message-Id: 269151ee-5875-4e19-b1e3-08df4f79db6c X-MS-Exchange-CrossTenant-AuthSource: DS4PR12MB634945.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Dec 2026 07:08:10.7288 (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: rqf+Bch8s+DGtoxKnViqozPNwVjrw+oWEe4U7A+PDVHoCl8f5uaYc/lfYcTkTfNH3hXt3q2hb2eC7CWV1e5ROQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB6721 Switch iommufd to using dma-shared-buf to program the iommu domains. iommufd is the only importer permitted to use dma_buf_iommu_map(), which the module restriction on the exports enforces. Only an exporter handing out a dma_shared_buf can be mapped this way so IOMMU_IOAS_MAP_FILE refuses any other buffer up front with dma_buf_iommu_mappable(), the same way the private vfio interface failed for anything but a vfio DMA-buf. Filling a domain no longer reads addresses through the pfn reader. Instead the area hands dma_buf_iommu_map() its domain, IOVA and window into the buffer and the core has the dma_shared_buf programmed, which also makes exporters with more than one range work. Unfilling and revoke go through dma_buf_iommu_unmap(). iommufd keeps no address at all, only a revoked flag that the exporter's invalidate_mappings() sets. Signed-off-by: Alistair Popple --- drivers/iommu/iommufd/io_pagetable.c | 1 + drivers/iommu/iommufd/io_pagetable.h | 27 +++- drivers/iommu/iommufd/iommufd_private.h | 8 -- drivers/iommu/iommufd/pages.c | 219 ++++++++++++++------------------ drivers/iommu/iommufd/selftest.c | 66 +++++----- 5 files changed, 158 insertions(+), 163 deletions(-) diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c index 5ac736cf5bb5..fc0c49a9ed76 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -1145,6 +1145,7 @@ static int iopt_fill_domain(struct io_pagetable *iopt, if (!pages) continue; + guard(iopt_dmabuf_resv)(pages); guard(mutex)(&pages->mutex); if (iopt_is_dmabuf(pages)) { rc = iopt_dmabuf_track_domain(pages, area, domain); diff --git a/drivers/iommu/iommufd/io_pagetable.h b/drivers/iommu/iommufd/io_pagetable.h index 27e3e311d395..f8d9523e896d 100644 --- a/drivers/iommu/iommufd/io_pagetable.h +++ b/drivers/iommu/iommufd/io_pagetable.h @@ -6,6 +6,7 @@ #define __IO_PAGETABLE_H #include +#include #include #include #include @@ -202,7 +203,8 @@ struct iopt_pages_dmabuf_track { struct iopt_pages_dmabuf { struct dma_buf_attachment *attach; - struct phys_vec phys; + /* Set by the exporter's invalidate_mappings(), never cleared */ + bool revoked; /* Always PAGE_SIZE aligned */ unsigned long start; struct list_head tracker; @@ -259,10 +261,31 @@ static inline bool iopt_dmabuf_revoked(struct iopt_pages *pages) { lockdep_assert_held(&pages->mutex); if (iopt_is_dmabuf(pages)) - return pages->dmabuf.phys.len == 0; + return pages->dmabuf.revoked; return false; } +/* + * Filling a domain from a dmabuf calls into the exporter, which needs the + * dma_resv lock. It nests outside pages->mutex, the order iopt_map_dmabuf() + * establishes, and the exporter's revoke takes the same two locks in the same + * order, so a fill never races a revoke. + */ +static inline void iopt_dmabuf_resv_lock(struct iopt_pages *pages) +{ + if (iopt_is_dmabuf(pages)) + dma_resv_lock(pages->dmabuf.attach->dmabuf->resv, NULL); +} + +static inline void iopt_dmabuf_resv_unlock(struct iopt_pages *pages) +{ + if (iopt_is_dmabuf(pages)) + dma_resv_unlock(pages->dmabuf.attach->dmabuf->resv); +} + +DEFINE_GUARD(iopt_dmabuf_resv, struct iopt_pages *, iopt_dmabuf_resv_lock(_T), + iopt_dmabuf_resv_unlock(_T)) + struct iopt_pages *iopt_alloc_user_pages(void __user *uptr, unsigned long length, bool writable); struct iopt_pages *iopt_alloc_file_pages(struct file *file, diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index eb2e85b27e42..6ffc0c22ccfc 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -739,8 +739,6 @@ bool iommufd_should_fail(void); int __init iommufd_test_init(void); void iommufd_test_exit(void); bool iommufd_selftest_is_mock_dev(struct device *dev); -int iommufd_test_dma_buf_iommufd_map(struct dma_buf_attachment *attachment, - struct phys_vec *phys); #else static inline void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd, unsigned int ioas_id, @@ -762,11 +760,5 @@ static inline bool iommufd_selftest_is_mock_dev(struct device *dev) { return false; } -static inline int -iommufd_test_dma_buf_iommufd_map(struct dma_buf_attachment *attachment, - struct phys_vec *phys) -{ - return -EOPNOTSUPP; -} #endif #endif diff --git a/drivers/iommu/iommufd/pages.c b/drivers/iommu/iommufd/pages.c index fcdbde007a9a..aeac509e574f 100644 --- a/drivers/iommu/iommufd/pages.c +++ b/drivers/iommu/iommufd/pages.c @@ -46,6 +46,7 @@ * ULONG_MAX so last_index + 1 cannot overflow. */ #include +#include #include #include #include @@ -55,7 +56,6 @@ #include #include #include -#include #include "double_span.h" #include "io_pagetable.h" @@ -250,6 +250,70 @@ static void iopt_area_unmap_domain_range(struct iopt_area *area, start_iova + 1); } +/* The dmabuf version of iopt_area_unmap_domain_range() */ +static void iopt_dmabuf_unmap_domain_range(struct iopt_area *area, + struct iopt_pages *pages, + struct iommu_domain *domain, + unsigned long start_index, + unsigned long last_index) +{ + unsigned long start_iova = iopt_area_index_to_iova(area, start_index); + + dma_buf_iommu_unmap(pages->dmabuf.attach, domain, start_iova, + iopt_area_index_to_iova_last(area, last_index) - + start_iova + 1); +} + +/* + * Map the whole area into @domain from the dmabuf. The core does the walk + * over the exporter's dma_shared_buf; this only hands it the window of the + * buffer the area covers. Requires the dma_resv lock, see + * iopt_dmabuf_resv_lock(). + */ +static int iopt_area_fill_domain_dmabuf(struct iopt_area *area, + struct iopt_pages *pages, + struct iommu_domain *domain) +{ + unsigned long iova = iopt_area_iova(area); + + dma_resv_assert_held(pages->dmabuf.attach->dmabuf->resv); + return dma_buf_iommu_map(pages->dmabuf.attach, domain, iova, + pages->dmabuf.start + + iopt_area_start_byte(area, iova), + iopt_area_length(area), area->iommu_prot, + GFP_KERNEL_ACCOUNT); +} + +/* Map the whole area into every domain of its iopt, or into none of them */ +static int iopt_area_fill_domains_dmabuf(struct iopt_area *area, + struct iopt_pages *pages) +{ + struct iommu_domain *domain; + unsigned long unmap_index; + unsigned long index; + int rc; + + if (iopt_dmabuf_revoked(pages)) + return 0; + + xa_for_each(&area->iopt->domains, index, domain) { + rc = iopt_area_fill_domain_dmabuf(area, pages, domain); + if (rc) + goto err_unmap; + } + return 0; + +err_unmap: + xa_for_each(&area->iopt->domains, unmap_index, domain) { + if (unmap_index >= index) + break; + iopt_dmabuf_unmap_domain_range(area, pages, domain, + iopt_area_index(area), + iopt_area_last_index(area)); + } + return rc; +} + static struct iopt_area *iopt_pages_find_domain_area(struct iopt_pages *pages, unsigned long index) { @@ -261,11 +325,6 @@ static struct iopt_area *iopt_pages_find_domain_area(struct iopt_pages *pages, return container_of(node, struct iopt_area, pages_node); } -enum batch_kind { - BATCH_CPU_MEMORY = 0, - BATCH_MMIO, -}; - /* * A simple datastructure to hold a vector of PFNs, optimized for contiguous * PFNs. This is used as a temporary holding memory for shuttling pfns from one @@ -279,7 +338,6 @@ struct pfn_batch { unsigned int array_size; unsigned int end; unsigned int total_pfns; - enum batch_kind kind; }; enum { MAX_NPFNS = type_max(typeof(((struct pfn_batch *)0)->npfns[0])) }; @@ -289,7 +347,6 @@ static void batch_clear(struct pfn_batch *batch) batch->end = 0; batch->pfns[0] = 0; batch->npfns[0] = 0; - batch->kind = 0; } /* @@ -359,17 +416,10 @@ static void batch_destroy(struct pfn_batch *batch, void *backup) } static bool batch_add_pfn_num(struct pfn_batch *batch, unsigned long pfn, - u32 nr, enum batch_kind kind) + u32 nr) { unsigned int end = batch->end; - if (batch->kind != kind) { - /* One kind per batch */ - if (batch->end != 0) - return false; - batch->kind = kind; - } - if (end && pfn == batch->pfns[end - 1] + batch->npfns[end - 1] && nr <= MAX_NPFNS - batch->npfns[end - 1]) { batch->npfns[end - 1] += nr; @@ -396,7 +446,7 @@ static void batch_remove_pfn_num(struct pfn_batch *batch, unsigned long nr) /* true if the pfn was added, false otherwise */ static bool batch_add_pfn(struct pfn_batch *batch, unsigned long pfn) { - return batch_add_pfn_num(batch, pfn, 1, BATCH_CPU_MEMORY); + return batch_add_pfn_num(batch, pfn, 1); } /* @@ -517,11 +567,6 @@ static int batch_to_domain(struct pfn_batch *batch, struct iommu_domain *domain, unsigned long iova; int rc; - if (batch->kind == BATCH_MMIO) { - iommu_prot &= ~IOMMU_CACHE; - iommu_prot |= IOMMU_MMIO; - } - /* The first index might be a partial page */ if (start_index == iopt_area_index(area)) page_offset = area->page_offset; @@ -675,7 +720,7 @@ static int batch_from_folios(struct pfn_batch *batch, struct folio ***folios_p, nr = min(nr, npages); npages -= nr; - if (!batch_add_pfn_num(batch, pfn, nr, BATCH_CPU_MEMORY)) + if (!batch_add_pfn_num(batch, pfn, nr)) break; if (nr > 1) { rc = folio_add_pins(folio, nr - 1); @@ -1084,41 +1129,6 @@ static int pfn_reader_user_update_pinned(struct pfn_reader_user *user, return iopt_pages_update_pinned(pages, npages, inc, user); } -struct pfn_reader_dmabuf { - struct phys_vec phys; - unsigned long start_offset; -}; - -static int pfn_reader_dmabuf_init(struct pfn_reader_dmabuf *dmabuf, - struct iopt_pages *pages) -{ - /* Callers must not get here if the dmabuf was already revoked */ - if (WARN_ON(iopt_dmabuf_revoked(pages))) - return -EINVAL; - - dmabuf->phys = pages->dmabuf.phys; - dmabuf->start_offset = pages->dmabuf.start; - return 0; -} - -static int pfn_reader_fill_dmabuf(struct pfn_reader_dmabuf *dmabuf, - struct pfn_batch *batch, - unsigned long start_index, - unsigned long last_index) -{ - unsigned long start = dmabuf->start_offset + start_index * PAGE_SIZE; - - /* - * start/last_index and start are all PAGE_SIZE aligned, the batch is - * always filled using page size aligned PFNs just like the other types. - * If the dmabuf has been sliced on a sub page offset then the common - * batch to domain code will adjust it before mapping to the domain. - */ - batch_add_pfn_num(batch, PHYS_PFN(dmabuf->phys.paddr + start), - last_index - start_index + 1, BATCH_MMIO); - return 0; -} - /* * PFNs are stored in three places, in order of preference: * - The iopt_pages xarray. This is only populated if there is a @@ -1137,10 +1147,7 @@ struct pfn_reader { unsigned long batch_end_index; unsigned long last_index; - union { - struct pfn_reader_user user; - struct pfn_reader_dmabuf dmabuf; - }; + struct pfn_reader_user user; }; static int pfn_reader_update_pinned(struct pfn_reader *pfns) @@ -1185,15 +1192,6 @@ static int pfn_reader_fill_span(struct pfn_reader *pfns) WARN_ON(span->last_used < start_index)) return -EINVAL; - /* - * Always read a dmabuf from its phys, even where a domain already - * maps it: a domain can only report CPU memory PFNs, losing - * BATCH_MMIO. last_hole aliases last_used, so this covers any span. - */ - if (iopt_is_dmabuf(pfns->pages)) - return pfn_reader_fill_dmabuf(&pfns->dmabuf, &pfns->batch, - start_index, span->last_hole); - if (span->is_used == 1) { batch_from_xarray(&pfns->batch, &pfns->pages->pinned_pfns, start_index, span->last_used); @@ -1283,14 +1281,15 @@ static int pfn_reader_init(struct pfn_reader *pfns, struct iopt_pages *pages, lockdep_assert_held(&pages->mutex); + /* dmabufs are mapped by the dma-buf core, not read as PFNs */ + if (WARN_ON(iopt_is_dmabuf(pages))) + return -EINVAL; + pfns->pages = pages; pfns->batch_start_index = start_index; pfns->batch_end_index = start_index; pfns->last_index = last_index; - if (iopt_is_dmabuf(pages)) - pfn_reader_dmabuf_init(&pfns->dmabuf, pages); - else - pfn_reader_user_init(&pfns->user, pages); + pfn_reader_user_init(&pfns->user, pages); rc = batch_init(&pfns->batch, last_index - start_index + 1); if (rc) return rc; @@ -1311,12 +1310,8 @@ static int pfn_reader_init(struct pfn_reader *pfns, struct iopt_pages *pages, static void pfn_reader_release_pins(struct pfn_reader *pfns) { struct iopt_pages *pages = pfns->pages; - struct pfn_reader_user *user; - - if (iopt_is_dmabuf(pages)) - return; + struct pfn_reader_user *user = &pfns->user; - user = &pfns->user; if (user->upages_end > pfns->batch_end_index) { /* Any pages not transferred to the batch are just unpinned */ @@ -1346,8 +1341,7 @@ static void pfn_reader_destroy(struct pfn_reader *pfns) struct iopt_pages *pages = pfns->pages; pfn_reader_release_pins(pfns); - if (!iopt_is_dmabuf(pfns->pages)) - pfn_reader_user_destroy(&pfns->user, pfns->pages); + pfn_reader_user_destroy(&pfns->user, pfns->pages); batch_destroy(&pfns->batch, NULL); WARN_ON(pages->last_npinned != pages->npinned); } @@ -1455,11 +1449,11 @@ static void iopt_revoke_notify(struct dma_buf_attachment *attach) list_for_each_entry(track, &pages->dmabuf.tracker, elm) { struct iopt_area *area = track->area; - iopt_area_unmap_domain_range(area, track->domain, - iopt_area_index(area), - iopt_area_last_index(area)); + iopt_dmabuf_unmap_domain_range(area, pages, track->domain, + iopt_area_index(area), + iopt_area_last_index(area)); } - pages->dmabuf.phys.len = 0; + pages->dmabuf.revoked = true; } static const struct dma_buf_attach_ops iopt_dmabuf_attach_revoke_ops = { @@ -1467,38 +1461,16 @@ static const struct dma_buf_attach_ops iopt_dmabuf_attach_revoke_ops = { .invalidate_mappings = iopt_revoke_notify, }; -/* - * iommufd and vfio have a circular dependency. Future work for a phys - * based private interconnect will remove this. - */ -static int -sym_vfio_pci_dma_buf_iommufd_map(struct dma_buf_attachment *attachment, - struct phys_vec *phys) -{ - typeof(&vfio_pci_dma_buf_iommufd_map) fn; - int rc; - - rc = iommufd_test_dma_buf_iommufd_map(attachment, phys); - if (rc != -EOPNOTSUPP) - return rc; - - if (!IS_ENABLED(CONFIG_VFIO_PCI_DMABUF)) - return -EOPNOTSUPP; - - fn = symbol_get(vfio_pci_dma_buf_iommufd_map); - if (!fn) - return -EOPNOTSUPP; - rc = fn(attachment, phys); - symbol_put(vfio_pci_dma_buf_iommufd_map); - return rc; -} - static int iopt_map_dmabuf(struct iommufd_ctx *ictx, struct iopt_pages *pages, struct dma_buf *dmabuf) { struct dma_buf_attachment *attach; int rc; + /* Fail at import rather than when a domain is first filled */ + if (!dma_buf_iommu_mappable(dmabuf)) + return -EOPNOTSUPP; + attach = dma_buf_dynamic_attach(dmabuf, iommufd_global_device(), &iopt_dmabuf_attach_revoke_ops, pages); if (IS_ERR(attach)) @@ -1518,18 +1490,12 @@ static int iopt_map_dmabuf(struct iommufd_ctx *ictx, struct iopt_pages *pages, if (rc) goto err_detach; - rc = sym_vfio_pci_dma_buf_iommufd_map(attach, &pages->dmabuf.phys); - if (rc) - goto err_unpin; - dma_resv_unlock(dmabuf->resv); /* On success iopt_release_pages() will detach and put the dmabuf. */ pages->dmabuf.attach = attach; return 0; -err_unpin: - dma_buf_unpin(attach); err_detach: dma_resv_unlock(dmabuf->resv); dma_buf_detach(dmabuf, attach); @@ -1764,8 +1730,8 @@ static void __iopt_area_unfill_domain(struct iopt_area *area, if (iopt_is_dmabuf(pages)) { if (WARN_ON(iopt_dmabuf_revoked(pages))) return; - iopt_area_unmap_domain_range(area, domain, start_index, - last_index); + iopt_dmabuf_unmap_domain_range(area, pages, domain, start_index, + last_index); return; } @@ -1870,6 +1836,9 @@ int iopt_area_fill_domain(struct iopt_area *area, struct iommu_domain *domain) if (iopt_dmabuf_revoked(area->pages)) return 0; + if (iopt_is_dmabuf(area->pages)) + return iopt_area_fill_domain_dmabuf(area, area->pages, domain); + rc = pfn_reader_first(&pfns, area->pages, iopt_area_index(area), iopt_area_last_index(area)); if (rc) @@ -1928,14 +1897,16 @@ int iopt_area_fill_domains(struct iopt_area *area, struct iopt_pages *pages) if (xa_empty(&area->iopt->domains)) return 0; + iopt_dmabuf_resv_lock(pages); mutex_lock(&pages->mutex); if (iopt_is_dmabuf(pages)) { rc = iopt_dmabuf_track_all_domains(area, pages); if (rc) goto out_unlock; - } - - if (!iopt_dmabuf_revoked(pages)) { + rc = iopt_area_fill_domains_dmabuf(area, pages); + if (rc) + goto out_untrack; + } else { rc = pfn_reader_first(&pfns, pages, iopt_area_index(area), iopt_area_last_index(area)); if (rc) @@ -1966,6 +1937,7 @@ int iopt_area_fill_domains(struct iopt_area *area, struct iopt_pages *pages) area->storage_domain = xa_load(&area->iopt->domains, 0); interval_tree_insert(&area->pages_node, &pages->domains_itree); mutex_unlock(&pages->mutex); + iopt_dmabuf_resv_unlock(pages); return 0; out_unmap: @@ -1999,6 +1971,7 @@ int iopt_area_fill_domains(struct iopt_area *area, struct iopt_pages *pages) iopt_dmabuf_untrack_all_domains(area, pages); out_unlock: mutex_unlock(&pages->mutex); + iopt_dmabuf_resv_unlock(pages); return rc; } diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 25387969ab75..f0ba2624f7b3 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -1959,9 +1959,14 @@ struct iommufd_test_dma_buf { bool revoked; }; +/* Like vfio, a revoked buffer refuses new importers at attach */ static int iommufd_test_dma_buf_attach(struct dma_buf *dmabuf, struct dma_buf_attachment *attachment) { + struct iommufd_test_dma_buf *priv = dmabuf->priv; + + if (priv->revoked) + return -ENODEV; return 0; } @@ -1970,19 +1975,6 @@ static void iommufd_test_dma_buf_detach(struct dma_buf *dmabuf, { } -static struct sg_table * -iommufd_test_dma_buf_map(struct dma_buf_attachment *attachment, - enum dma_data_direction dir) -{ - return ERR_PTR(-EOPNOTSUPP); -} - -static void iommufd_test_dma_buf_unmap(struct dma_buf_attachment *attachment, - struct sg_table *sgt, - enum dma_data_direction dir) -{ -} - static void iommufd_test_dma_buf_release(struct dma_buf *dmabuf) { struct iommufd_test_dma_buf *priv = dmabuf->priv; @@ -1991,32 +1983,46 @@ static void iommufd_test_dma_buf_release(struct dma_buf *dmabuf) kfree(priv); } -static const struct dma_buf_ops iommufd_test_dmabuf_ops = { - .attach = iommufd_test_dma_buf_attach, - .detach = iommufd_test_dma_buf_detach, - .map_dma_buf = iommufd_test_dma_buf_map, - .release = iommufd_test_dma_buf_release, - .unmap_dma_buf = iommufd_test_dma_buf_unmap, -}; - -int iommufd_test_dma_buf_iommufd_map(struct dma_buf_attachment *attachment, - struct phys_vec *phys) +static struct dma_shared_buf * +iommufd_test_dma_buf_map_dma_shared_buf(struct dma_buf_attachment *attachment) { struct iommufd_test_dma_buf *priv = attachment->dmabuf->priv; + struct dma_shared_buf *shared_buf; dma_resv_assert_held(attachment->dmabuf->resv); - if (attachment->dmabuf->ops != &iommufd_test_dmabuf_ops) - return -EOPNOTSUPP; - if (priv->revoked) - return -ENODEV; + return ERR_PTR(-ENODEV); - phys->paddr = virt_to_phys(priv->memory); - phys->len = priv->length; - return 0; + shared_buf = kvmalloc(struct_size(shared_buf, ranges, 1), GFP_KERNEL); + if (!shared_buf) + return ERR_PTR(-ENOMEM); + + /* System memory: no provider */ + shared_buf->provider = NULL; + shared_buf->attrs = 0; + shared_buf->size = priv->length; + shared_buf->nr_ranges = 1; + shared_buf->ranges[0].paddr = virt_to_phys(priv->memory); + shared_buf->ranges[0].len = priv->length; + return shared_buf; +} + +static void +iommufd_test_dma_buf_unmap_dma_shared_buf(struct dma_buf_attachment *attach, + struct dma_shared_buf *shared_buf) +{ + kvfree(shared_buf); } +static const struct dma_buf_ops iommufd_test_dmabuf_ops = { + .attach = iommufd_test_dma_buf_attach, + .detach = iommufd_test_dma_buf_detach, + .release = iommufd_test_dma_buf_release, + .map_dma_shared_buf = iommufd_test_dma_buf_map_dma_shared_buf, + .unmap_dma_shared_buf = iommufd_test_dma_buf_unmap_dma_shared_buf, +}; + static int iommufd_test_dmabuf_get(struct iommufd_ucmd *ucmd, unsigned int open_flags, size_t len) -- 2.54.0