From nobody Sat Jun 13 01:44:02 2026 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) (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 06F613FFADC; Mon, 11 May 2026 14:59:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=67.231.145.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778511547; cv=none; b=kFHeCJXe0kD1R9ZcTHc3EB3otYAKEB+GHQjn7CnAVVZIkfJc/z+S5y5mj1QuSpLGMJgn1XJ2bIUCIAuksFfJ3c2e9A8DfJkEpMEPzUQV8Z7AGDodCdYIMh2H4TSoMOhDjcneBrykd93wnhf1bVt0DMOzmlgFHNSW9TajEC8DgXk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778511547; c=relaxed/simple; bh=DImIVzk4Fp/d0uKSX5Q013I75BODcyG03vsky99bTq0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=SXlYF+3t9wJ7aClmXsjVjvje7GSV+o0n+Xj6GDbYDtjgNeL5VWkU4nsmuN/6+CXxB3X5ThOV4dLoa80AqnxP7pUYZ8j1xAjkJ2KRbE6Z9vCK72YdKtFp+OauWyqndDDNxdd55YFiTP3Y4xYAX43Y9lv4ksx9yGc2Ymjz4oHkVaQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com; spf=pass smtp.mailfrom=meta.com; dkim=pass (2048-bit key) header.d=meta.com header.i=@meta.com header.b=lvj2SSw9; arc=none smtp.client-ip=67.231.145.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=meta.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=meta.com header.i=@meta.com header.b="lvj2SSw9" Received: from pps.filterd (m0148461.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 64BEw5rj941942; Mon, 11 May 2026 07:58:55 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=meta.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=s2048-2025-q2; bh=DcBD5V6ipvKym87WVE2bt/Rih+AWDUPxJbgEfmukeaQ=; b=lvj2SSw9uy3v 4f7a4bL0ObXPyyp71WlhUVpgx8wUyV4hNJ8kTwdUH7PJohFL/uDy5Qc6OrU70dZb Ze3vqDR6jvZ09+k+bhvIi+beeviiHHFRNTtfRTxlkR5pO4IVojSUomNu+sORIPbC bXBBxiGRyrX1tObaXAVqMIhKYA2GWKOp2+BOESTAvurCOypYHRyI6PwXyya2+FmW MleurSS21/RFFYFriQikUalM42IGM9g2rIu1Go7KJo3w2vhqEJYgDDwHn+BOk69O 332kex60nmbZJUBvEZz29gEIiR7ZMB2kiZH/ICnbsF2yqqJkHXiRPWv/2GSRrXPP l4D/f9I0UQ== Received: from maileast.thefacebook.com ([163.114.135.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 4e221d2h9a-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Mon, 11 May 2026 07:58:55 -0700 (PDT) Received: from localhost (2620:10d:c0a8:fe::f072) by mail.thefacebook.com (2620:10d:c0a9:6f::8fd4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.37; Mon, 11 May 2026 14:58:53 +0000 From: Matt Evans To: Alex Williamson , Kevin Tian , Jason Gunthorpe , Ankit Agrawal , Alistair Popple , Leon Romanovsky , Kees Cook , Shameer Kolothum , Yishai Hadas CC: Alexey Kardashevskiy , Eric Auger , Peter Xu , Vivek Kasireddy , Zhi Wang , , , Subject: [PATCH v5 1/3] vfio/pci: Set up BAR resources and maps in vfio_pci_core_enable() Date: Mon, 11 May 2026 07:58:23 -0700 Message-ID: <20260511145829.2993601-2-mattev@meta.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260511145829.2993601-1-mattev@meta.com> References: <20260511145829.2993601-1-mattev@meta.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Authority-Analysis: v=2.4 cv=YJuvDxGx c=1 sm=1 tr=0 ts=6a01eeaf cx=c_pps a=MfjaFnPeirRr97d5FC5oHw==:117 a=MfjaFnPeirRr97d5FC5oHw==:17 a=NGcC8JguVDcA:10 a=VkNPw1HP01LnGYTKEx00:22 a=7x6HtfJdh03M6CCDgxCd:22 a=03ozwUkBphtHgyqjj1sw:22 a=VabnemYjAAAA:8 a=Lb-xxqHfFU9yCMAFiGgA:9 a=gKebqoRLp9LExxC7YDUY:22 X-Proofpoint-ORIG-GUID: dr9uc5bF20qs2VvA6RQ2qT2L72xsCqb8 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNTExMDE2NCBTYWx0ZWRfX7Ew39mdAfjHD FMZ2fh8w41ngBnwOWFPKS3HylKJpcypbOE2APcRslABVtgPLvWLyN/cWnJa9tv1XQPj5FNRLhgT oLUAko901hflOWc3GJ772xLw+0l+diCWaGTfDVTXi9NDmhNw4FgFxZPjzetVRplc3aFAkLspHXy T0HnRlThyn23+TQ16IB/ucfx+1DZqT1jLZr23eq4AzUGpsFnsi1bUPYNnCQqHJ09iFNOqR8kRfr +yuMj+8ddFuzQjTjRuuOsuVbhNX2n1o2PmlnhUMmP9IvPe3a3OZWu7LdKhyyVgg8IRKMJs39McE +TBaD80EmOEDWS4cNnsQHYOP55ncnHzAIeYu/JIuug/Ads4h7eKCCQQCGcE3M/u5Ea0c7NlAN/1 WQmfmZuKV7kme2pm6ymFxxT13n4UNYTmJimyJmmMOBkA8tR+ZNDOOSfu/SGFJbg5ktBpo7ACcQf Nj2Z1RQv46t63GS2fwQ== X-Proofpoint-GUID: dr9uc5bF20qs2VvA6RQ2qT2L72xsCqb8 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-05-11_04,2026-05-08_02,2025-10-01_01 Content-Type: text/plain; charset="utf-8" Previously BAR resource requests and the corresponding pci_iomap() were performed on-demand and without synchronisation, which was racy. Rather than add synchronisation, it's simplest to address this by doing both activities from vfio_pci_core_enable(). The resource allocation and/or pci_iomap() can still fail; their status is tracked and existing calls to vfio_pci_core_setup_barmap() will fail in a similar way to before. This keeps the point of failure as observed by userspace the same, i.e. failures to request/map unused BARs are benign. Fixes: 89e1f7d4c66d ("vfio: Add PCI device driver") Signed-off-by: Matt Evans --- drivers/vfio/pci/vfio_pci_core.c | 37 +++++++++++++++++++++++++++++++- drivers/vfio/pci/vfio_pci_rdwr.c | 26 ++++++---------------- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_c= ore.c index 3f8d093aacf8..0e4fc17d360f 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -482,6 +482,40 @@ static int vfio_pci_core_runtime_resume(struct device = *dev) } #endif /* CONFIG_PM */ =20 +/* + * Eager-request BAR resources, and iomap them. Soft failures are + * allowed, and consumers must check the barmap before use in order to + * give compatible user-visible behaviour with the previous on-demand + * allocation method. + */ +static void vfio_pci_core_map_bars(struct vfio_pci_core_device *vdev) +{ + struct pci_dev *pdev =3D vdev->pdev; + int i; + + for (i =3D 0; i < PCI_STD_NUM_BARS; i++) { + int bar =3D i + PCI_STD_RESOURCES; + + vdev->barmap[bar] =3D ERR_PTR(-ENODEV); + + if (!pci_resource_len(pdev, i)) + continue; + + if (pci_request_selected_regions(pdev, 1 << bar, "vfio")) { + pci_dbg(pdev, "Failed to reserve region %d\n", bar); + vdev->barmap[bar] =3D ERR_PTR(-EBUSY); + continue; + } + + vdev->barmap[bar] =3D pci_iomap(pdev, bar, 0); + if (!vdev->barmap[bar]) { + pci_dbg(pdev, "Failed to iomap region %d\n", bar); + pci_release_selected_regions(pdev, 1 << bar); + vdev->barmap[bar] =3D ERR_PTR(-ENOMEM); + } + } +} + /* * The pci-driver core runtime PM routines always save the device state * before going into suspended state. If the device is going into low power @@ -568,6 +602,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *v= dev) if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev)) vdev->has_vga =3D true; =20 + vfio_pci_core_map_bars(vdev); =20 return 0; =20 @@ -648,7 +683,7 @@ void vfio_pci_core_disable(struct vfio_pci_core_device = *vdev) =20 for (i =3D 0; i < PCI_STD_NUM_BARS; i++) { bar =3D i + PCI_STD_RESOURCES; - if (!vdev->barmap[bar]) + if (IS_ERR_OR_NULL(vdev->barmap[bar])) continue; pci_iounmap(pdev, vdev->barmap[bar]); pci_release_selected_regions(pdev, 1 << bar); diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_r= dwr.c index 4251ee03e146..3bfbb879a005 100644 --- a/drivers/vfio/pci/vfio_pci_rdwr.c +++ b/drivers/vfio/pci/vfio_pci_rdwr.c @@ -198,27 +198,15 @@ ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_d= evice *vdev, bool test_mem, } EXPORT_SYMBOL_GPL(vfio_pci_core_do_io_rw); =20 +/* + * The barmap is set up in vfio_pci_core_enable(). Callers use this + * function to check that the BAR resources are requested or that the + * pci_iomap() was done. + */ int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar) { - struct pci_dev *pdev =3D vdev->pdev; - int ret; - void __iomem *io; - - if (vdev->barmap[bar]) - return 0; - - ret =3D pci_request_selected_regions(pdev, 1 << bar, "vfio"); - if (ret) - return ret; - - io =3D pci_iomap(pdev, bar, 0); - if (!io) { - pci_release_selected_regions(pdev, 1 << bar); - return -ENOMEM; - } - - vdev->barmap[bar] =3D io; - + if (IS_ERR(vdev->barmap[bar])) + return PTR_ERR(vdev->barmap[bar]); return 0; } EXPORT_SYMBOL_GPL(vfio_pci_core_setup_barmap); --=20 2.47.3 From nobody Sat Jun 13 01:44:02 2026 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) (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 43C18401A12; Mon, 11 May 2026 14:59:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=67.231.153.30 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778511551; cv=none; b=cXsrWgeeSRG3aYX5+uNgORfvRzeIfV3vLeC/MUtTuJZeSib+fWDJAOMqa1pdCuJDP/Ok2jMBaWyzUhgqgoZ/6yVIs5UAxYWQ8KVkC51Dd/KjksYQyuAB/pmBAmJYP0q+bIog2mj6YzYb9LzXLOdTmND1FBNcN9Ho65cMEa0TYKY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778511551; c=relaxed/simple; bh=U9AZqRqsPkBEbLgz08qAx/7KNRXfInRn/Yp7MxLBCbk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=XcljYqdOdQD0tQTum/DKArkPzROzV44gTM2yFng9hNLbRmW6a9QaAgT1xxaWTO1Cbvu90u0FSEtW7QX/jHt0IN3KCrrBQRmOZUSqV65XM96gMpypgioEjiw6K8QmJ1mIhaCZ/vtVBUlNNitE8MffdmX4h0+32DtkWsjQk2yFH0k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com; spf=pass smtp.mailfrom=meta.com; dkim=pass (2048-bit key) header.d=meta.com header.i=@meta.com header.b=RZm17Ewj; arc=none smtp.client-ip=67.231.153.30 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=meta.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=meta.com header.i=@meta.com header.b="RZm17Ewj" Received: from pps.filterd (m0528005.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 64BEvmxK1589792; Mon, 11 May 2026 07:58:59 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=meta.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=s2048-2025-q2; bh=uh/fqGrNa5e0eFsOnebd5OIY5cvCMtHB5nxwnWVPhv8=; b=RZm17EwjpwXF La+Y9Mb0BuSpfGYTD/xW7FQ8JpNw3MGnOiI5PHg8jYM9/g3VsHkPD3OumppdeXSe EKAoe8J8dfy2cOnPfrMXQLj0TuZ6tDmsMT4oDQEMcs4ygm3vsr+6MhMGId1IV6ax r8g5dok0rPLU6r4UKxUz61YmZ7DDnuAFHBW4/5KRc0hvm1o/NUurUfDgTjEo509e DZGhmrBBtBK1QyLPT38Ahv6hZcL1mL5PVlMo1uECXg6pnbc8FVL2zIb1IOn4XXT6 0VjfxDVYyKbGFP8w0HoR4qL+H9wuUrlLYbF5/nPeDb2doxOszDcWSQKORtJIbKoS AVuV5+vWZQ== Received: from mail.thefacebook.com ([163.114.134.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 4e2pgjeeqy-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Mon, 11 May 2026 07:58:59 -0700 (PDT) Received: from localhost (2620:10d:c085:108::150d) by mail.thefacebook.com (2620:10d:c08b:78::2ac9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.37; Mon, 11 May 2026 14:58:57 +0000 From: Matt Evans To: Alex Williamson , Kevin Tian , Jason Gunthorpe , Ankit Agrawal , Alistair Popple , Leon Romanovsky , Kees Cook , Shameer Kolothum , Yishai Hadas CC: Alexey Kardashevskiy , Eric Auger , Peter Xu , Vivek Kasireddy , Zhi Wang , , , Subject: [PATCH v5 2/3] vfio/pci: Check BAR resources before exporting a DMABUF Date: Mon, 11 May 2026 07:58:24 -0700 Message-ID: <20260511145829.2993601-3-mattev@meta.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260511145829.2993601-1-mattev@meta.com> References: <20260511145829.2993601-1-mattev@meta.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Proofpoint-GUID: 54RNRLnUF1lY6K1K7ltrdrM5EUzRPpFp X-Proofpoint-ORIG-GUID: 54RNRLnUF1lY6K1K7ltrdrM5EUzRPpFp X-Authority-Analysis: v=2.4 cv=G8Qs1dk5 c=1 sm=1 tr=0 ts=6a01eeb3 cx=c_pps a=CB4LiSf2rd0gKozIdrpkBw==:117 a=CB4LiSf2rd0gKozIdrpkBw==:17 a=NGcC8JguVDcA:10 a=VkNPw1HP01LnGYTKEx00:22 a=7x6HtfJdh03M6CCDgxCd:22 a=jCddH8ec0KUNCymVuxII:22 a=VabnemYjAAAA:8 a=Cv6RvFvLZYHFA_XSKfkA:9 a=gKebqoRLp9LExxC7YDUY:22 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNTExMDE2NCBTYWx0ZWRfX974LmLLfTcX7 QvvMuVNyXjUc5yVyQ/+Yi30yqdEC/pLWLbfi7cy+HgjiWK9bEirZZ3iTEs1vvp3RqIMDscXOW2a oLywU98esJolfZx0+/rW4gN0yVJRP3rAOTC2Q87OqqYA0eAwXIWMW9VGRgQoTuRVVZTs2dWGOso 0B0GSof2mvglRFZ1aKDDZGHW6PPm1ipU7efScTFXfkvnDGaYjinGuXWip+mww6aRy8pKjZNjil7 ljNbmcmt78rJVtlK0xoftAITcaiiK9qswKOSBtRNxxapJJVNwKu0l1IFu6flje7TR1nIt7lHqlc JZAPZoeDGWgGmRD2XFUOjm4bdFCaNLa/eaADVBO4nVs4firugFzWPEtwJSs3/Ugy3VfSNhP23OP w3lhwXYtZ6DrlebtXoK/V6kM68XM9A0QgDkYgl58PmhB8OSYvAYdEM7LUj5byKDy6HHQK76rcE9 neVPe/K7r5bXkk4mvtQ== X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-05-11_04,2026-05-08_02,2025-10-01_01 Content-Type: text/plain; charset="utf-8" A DMABUF exports access to BAR resources and, although they are requested at startup time, we need to ensure they really were reserved before exporting. Otherwise, it's possible to access unreserved resources through the export. Add a check to the DMABUF-creation path. Fixes: 5d74781ebc86c ("vfio/pci: Add dma-buf export support for MMIO region= s") Signed-off-by: Matt Evans --- drivers/vfio/pci/vfio_pci_dmabuf.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci= _dmabuf.c index f87fd32e4a01..69a5c2d511e6 100644 --- a/drivers/vfio/pci/vfio_pci_dmabuf.c +++ b/drivers/vfio/pci/vfio_pci_dmabuf.c @@ -244,9 +244,11 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core= _device *vdev, u32 flags, return -EINVAL; =20 /* - * For PCI the region_index is the BAR number like everything else. + * For PCI the region_index is the BAR number like everything + * else. Check that PCI resources have been claimed for it. */ - if (get_dma_buf.region_index >=3D VFIO_PCI_ROM_REGION_INDEX) + if (get_dma_buf.region_index >=3D VFIO_PCI_ROM_REGION_INDEX || + vfio_pci_core_setup_barmap(vdev, get_dma_buf.region_index)) return -ENODEV; =20 dma_ranges =3D memdup_array_user(&arg->dma_ranges, get_dma_buf.nr_ranges, --=20 2.47.3 From nobody Sat Jun 13 01:44:02 2026 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) (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 9E923402B83; Mon, 11 May 2026 14:59:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=67.231.145.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778511555; cv=none; b=YKWcd2QPQDNrX0KkeX1wPQjsAcBzKm/VjZMaMHacLLusAvDnemobxge4YccSg/L5/dX+56M5ZGGB5ZyeOXM+ZMG1ILEa1X/gSJc8ekR/bmvKoijzKVvHBG4aM2whenmlnCa7H7PXusH0X3gCsbyjXtlafJ03bokONE1k1fPL0yI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778511555; c=relaxed/simple; bh=GH7OEUo0CSzSES0dKOKQScZ8FigFog7YHguLc3TLiZA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=WkMBUoiZs5lU5QI22/M3vV9pbdAB6/8pd9VZ8TTlRwNtaPtIAQYW+KvzwiQoV4AT4lvoepfNlHDHyGgwfN9HCGm+DUPZ5YP77yTf6Sdr388UTs8DmwCuOIIIv3Gf3f3SEueAJLd0gOZ0v1w6TbtwabF4OtitMMQAA8Cci9wT+WM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com; spf=pass smtp.mailfrom=meta.com; dkim=pass (2048-bit key) header.d=meta.com header.i=@meta.com header.b=vBOfU7gB; arc=none smtp.client-ip=67.231.145.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=meta.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=meta.com header.i=@meta.com header.b="vBOfU7gB" Received: from pps.filterd (m0528008.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 64BEvfSo1143196; Mon, 11 May 2026 07:59:02 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=meta.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=s2048-2025-q2; bh=VcB1y3QSglxI0gdP4uVXu3IQj3YglwiZAldWcHbVHp8=; b=vBOfU7gB8bso 1IjeUiKjgzPJqR/YtpjbYj+OsBoLzuf3DWgpU5K7Ck3wFlWBRHkfGttXjBMMhbPd e7WBa7+H3Wwovs5USWqWJZhmeHcWzckKlCcakzfGiW/JFwo6T8c6hWDKx0FFoi9J HoSnLuoC/+bo+LHt5YBa5i90hjrvZPUFfNOvAZcOUrfNouY6u+CxQVJbp56Q5E7F v+hAa/DYX9x1ve9sce0aRMEJUWxFZUKQRvbxGjxthPqewb6397fHXhUxY5AZ117F sjnN3EbYuvflMpWEJVcbdwkxWM2Z8+fS0mQyMImx2OMwJVre/9ldgZp+/tdQOO7s GEl9Zt4DHQ== Received: from maileast.thefacebook.com ([163.114.135.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 4e2shhwxjx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Mon, 11 May 2026 07:59:02 -0700 (PDT) Received: from localhost (2620:10d:c0a8:1c::1b) by mail.thefacebook.com (2620:10d:c0a9:6f::237c) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.37; Mon, 11 May 2026 14:59:00 +0000 From: Matt Evans To: Alex Williamson , Kevin Tian , Jason Gunthorpe , Ankit Agrawal , Alistair Popple , Leon Romanovsky , Kees Cook , Shameer Kolothum , Yishai Hadas CC: Alexey Kardashevskiy , Eric Auger , Peter Xu , Vivek Kasireddy , Zhi Wang , , , Subject: [PATCH v5 3/3] vfio/pci: Replace vfio_pci_core_setup_barmap() with vfio_pci_core_get_iomap() Date: Mon, 11 May 2026 07:58:25 -0700 Message-ID: <20260511145829.2993601-4-mattev@meta.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260511145829.2993601-1-mattev@meta.com> References: <20260511145829.2993601-1-mattev@meta.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNTExMDE2NCBTYWx0ZWRfXx1l9Nw81tlKS raj+ZdjZqr1TFthp/LdgJMTobrq9ByO2gOkyZcz9Drit55aCDxAgbQ6jW+goqM3JFoZ0F40ceS/ QIeWaHdn/rYv3OYpL8zcP04523/Hx+5UJ7R/7BgZ9LrUyeiVi0nnHqdNE+fdX88h5Zd8DbcysI8 rYWUymvE7TlMFjKyJb2EzHD5ViKArmtQw3edJjEljIOBLqoUCmcakKvYjS/VYBrrjnhB3FAeMKl gj4kjh03davrQBypKbhTathmjDyWn4G0Lp2dkT7tN+LnZabAHxu8KXitDsDZjpXewENwKHvi5J+ ecvSWiAbgs3kZEGpfd6OzwVKwYtpYKNruqEgjy15Ad3s9NnFuzCeCy6tnjUk7GIBNBoEZe9NQ0e bnLxk5EnEzVTJF+yq/m19t+dUE6g0M+iqt2GqmjsGs9VFXsTYtwOBac8cMpoFTffaX/Ecs0KBhm 9BgCkYx/2cRcqFuojhQ== X-Proofpoint-GUID: c73VIVWMBdTH9NasIsyQrHKwxxnepNAS X-Authority-Analysis: v=2.4 cv=euzvCIpX c=1 sm=1 tr=0 ts=6a01eeb6 cx=c_pps a=MfjaFnPeirRr97d5FC5oHw==:117 a=MfjaFnPeirRr97d5FC5oHw==:17 a=NGcC8JguVDcA:10 a=VkNPw1HP01LnGYTKEx00:22 a=7x6HtfJdh03M6CCDgxCd:22 a=_1IyUuN4QrATX339ibzo:22 a=VabnemYjAAAA:8 a=Dxuz5bgbLrCaPi_g5IEA:9 a=gKebqoRLp9LExxC7YDUY:22 X-Proofpoint-ORIG-GUID: c73VIVWMBdTH9NasIsyQrHKwxxnepNAS X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-05-11_04,2026-05-08_02,2025-10-01_01 Content-Type: text/plain; charset="utf-8" Since "vfio/pci: Set up barmap in vfio_pci_core_enable()", the resource request and iomap for the BARs was performed early, and vfio_pci_core_setup_barmap() just checks those actions succeeded. Move this logic to a new helper that checks success and returns the iomap address, replacing the various bare vdev->barmap[] lookups. This maintains the error behaviour of the previous on-demand vfio_pci_core_setup_barmap() scheme. Signed-off-by: Matt Evans --- drivers/vfio/pci/nvgrace-gpu/main.c | 20 ++++++++++++------- drivers/vfio/pci/vfio_pci_core.c | 11 +++++------ drivers/vfio/pci/vfio_pci_dmabuf.c | 2 +- drivers/vfio/pci/vfio_pci_rdwr.c | 30 ++++++++--------------------- drivers/vfio/pci/virtio/legacy_io.c | 13 ++++++------- include/linux/vfio_pci_core.h | 20 ++++++++++++++++++- 6 files changed, 52 insertions(+), 44 deletions(-) diff --git a/drivers/vfio/pci/nvgrace-gpu/main.c b/drivers/vfio/pci/nvgrace= -gpu/main.c index fa056b69f899..15e2f03c6cd4 100644 --- a/drivers/vfio/pci/nvgrace-gpu/main.c +++ b/drivers/vfio/pci/nvgrace-gpu/main.c @@ -61,6 +61,7 @@ struct nvgrace_gpu_pci_core_device { struct mem_region resmem; /* Lock to control device memory kernel mapping */ struct mutex remap_lock; + void __iomem *bar0_base; bool has_mig_hw_bug; /* GPU has just been reset */ bool reset_done; @@ -171,6 +172,7 @@ static int nvgrace_gpu_open_device(struct vfio_device *= core_vdev) struct nvgrace_gpu_pci_core_device *nvdev =3D container_of(core_vdev, struct nvgrace_gpu_pci_core_device, core_device.vdev); + void __iomem *io; int ret; =20 ret =3D vfio_pci_core_enable(vdev); @@ -184,14 +186,14 @@ static int nvgrace_gpu_open_device(struct vfio_device= *core_vdev) =20 /* * GPU readiness is checked by reading the BAR0 registers. - * - * ioremap BAR0 to ensure that the BAR0 mapping is present before - * register reads on first fault before establishing any GPU - * memory mapping. + * The BAR map was just set up by vfio_pci_core_enable(), so + * bail early if that wasn't successful: */ - ret =3D vfio_pci_core_setup_barmap(vdev, 0); - if (ret) + io =3D vfio_pci_core_get_iomap(vdev, 0); + if (IS_ERR(io)) { + ret =3D PTR_ERR(io); goto error_exit; + } =20 if (nvdev->resmem.memlength) { ret =3D nvgrace_gpu_vfio_pci_register_pfn_range(core_vdev, &nvdev->resme= m); @@ -204,6 +206,8 @@ static int nvgrace_gpu_open_device(struct vfio_device *= core_vdev) goto register_mem_failed; =20 vfio_pci_core_finish_enable(vdev); + nvdev->bar0_base =3D io; + return 0; =20 register_mem_failed: @@ -220,6 +224,8 @@ static void nvgrace_gpu_close_device(struct vfio_device= *core_vdev) container_of(core_vdev, struct nvgrace_gpu_pci_core_device, core_device.vdev); =20 + nvdev->bar0_base =3D NULL; + if (nvdev->resmem.memlength) unregister_pfn_address_space(&nvdev->resmem.pfn_address_space); =20 @@ -275,7 +281,7 @@ nvgrace_gpu_check_device_ready(struct nvgrace_gpu_pci_c= ore_device *nvdev) if (!__vfio_pci_memory_enabled(vdev)) return -EIO; =20 - ret =3D nvgrace_gpu_wait_device_ready(vdev->barmap[0]); + ret =3D nvgrace_gpu_wait_device_ready(nvdev->bar0_base); if (ret) return ret; =20 diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_c= ore.c index 0e4fc17d360f..b1b7fc5183a7 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -1762,7 +1762,7 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev,= struct vm_area_struct *vma struct pci_dev *pdev =3D vdev->pdev; unsigned int index; u64 phys_len, req_len, pgoff, req_start; - int ret; + void __iomem *bar_io; =20 index =3D vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT); =20 @@ -1796,12 +1796,11 @@ int vfio_pci_core_mmap(struct vfio_device *core_vde= v, struct vm_area_struct *vma return -EINVAL; =20 /* - * Even though we don't make use of the barmap for the mmap, - * we need to request the region and the barmap tracks that. + * Ensure the BAR resource region is reserved for use. */ - ret =3D vfio_pci_core_setup_barmap(vdev, index); - if (ret) - return ret; + bar_io =3D vfio_pci_core_get_iomap(vdev, index); + if (IS_ERR(bar_io)) + return PTR_ERR(bar_io); =20 vma->vm_private_data =3D vdev; vma->vm_page_prot =3D pgprot_noncached(vma->vm_page_prot); diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci= _dmabuf.c index 69a5c2d511e6..46cd44b22c9c 100644 --- a/drivers/vfio/pci/vfio_pci_dmabuf.c +++ b/drivers/vfio/pci/vfio_pci_dmabuf.c @@ -248,7 +248,7 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_= device *vdev, u32 flags, * else. Check that PCI resources have been claimed for it. */ if (get_dma_buf.region_index >=3D VFIO_PCI_ROM_REGION_INDEX || - vfio_pci_core_setup_barmap(vdev, get_dma_buf.region_index)) + IS_ERR(vfio_pci_core_get_iomap(vdev, get_dma_buf.region_index))) return -ENODEV; =20 dma_ranges =3D memdup_array_user(&arg->dma_ranges, get_dma_buf.nr_ranges, diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_r= dwr.c index 3bfbb879a005..7f14dd46de17 100644 --- a/drivers/vfio/pci/vfio_pci_rdwr.c +++ b/drivers/vfio/pci/vfio_pci_rdwr.c @@ -198,19 +198,6 @@ ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_de= vice *vdev, bool test_mem, } EXPORT_SYMBOL_GPL(vfio_pci_core_do_io_rw); =20 -/* - * The barmap is set up in vfio_pci_core_enable(). Callers use this - * function to check that the BAR resources are requested or that the - * pci_iomap() was done. - */ -int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar) -{ - if (IS_ERR(vdev->barmap[bar])) - return PTR_ERR(vdev->barmap[bar]); - return 0; -} -EXPORT_SYMBOL_GPL(vfio_pci_core_setup_barmap); - ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *bu= f, size_t count, loff_t *ppos, bool iswrite) { @@ -262,13 +249,11 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *= vdev, char __user *buf, */ max_width =3D VFIO_PCI_IO_WIDTH_4; } else { - int ret =3D vfio_pci_core_setup_barmap(vdev, bar); - if (ret) { - done =3D ret; + io =3D vfio_pci_core_get_iomap(vdev, bar); + if (IS_ERR(io)) { + done =3D PTR_ERR(io); goto out; } - - io =3D vdev->barmap[bar]; } =20 if (bar =3D=3D vdev->msix_bar) { @@ -423,6 +408,7 @@ int vfio_pci_ioeventfd(struct vfio_pci_core_device *vde= v, loff_t offset, loff_t pos =3D offset & VFIO_PCI_OFFSET_MASK; int ret, bar =3D VFIO_PCI_OFFSET_TO_INDEX(offset); struct vfio_pci_ioeventfd *ioeventfd; + void __iomem *io; =20 /* Only support ioeventfds into BARs */ if (bar > VFIO_PCI_BAR5_REGION_INDEX) @@ -440,9 +426,9 @@ int vfio_pci_ioeventfd(struct vfio_pci_core_device *vde= v, loff_t offset, if (count =3D=3D 8) return -EINVAL; =20 - ret =3D vfio_pci_core_setup_barmap(vdev, bar); - if (ret) - return ret; + io =3D vfio_pci_core_get_iomap(vdev, bar); + if (IS_ERR(io)) + return PTR_ERR(io); =20 mutex_lock(&vdev->ioeventfds_lock); =20 @@ -479,7 +465,7 @@ int vfio_pci_ioeventfd(struct vfio_pci_core_device *vde= v, loff_t offset, } =20 ioeventfd->vdev =3D vdev; - ioeventfd->addr =3D vdev->barmap[bar] + pos; + ioeventfd->addr =3D io + pos; ioeventfd->data =3D data; ioeventfd->pos =3D pos; ioeventfd->bar =3D bar; diff --git a/drivers/vfio/pci/virtio/legacy_io.c b/drivers/vfio/pci/virtio/= legacy_io.c index 1ed349a55629..c868b2177310 100644 --- a/drivers/vfio/pci/virtio/legacy_io.c +++ b/drivers/vfio/pci/virtio/legacy_io.c @@ -299,19 +299,18 @@ int virtiovf_pci_ioctl_get_region_info(struct vfio_de= vice *core_vdev, static int virtiovf_set_notify_addr(struct virtiovf_pci_core_device *virtv= dev) { struct vfio_pci_core_device *core_device =3D &virtvdev->core_device; - int ret; + void __iomem *io; =20 /* * Setup the BAR where the 'notify' exists to be used by vfio as well * This will let us mmap it only once and use it when needed. */ - ret =3D vfio_pci_core_setup_barmap(core_device, - virtvdev->notify_bar); - if (ret) - return ret; + io =3D vfio_pci_core_get_iomap(core_device, + virtvdev->notify_bar); + if (IS_ERR(io)) + return PTR_ERR(io); =20 - virtvdev->notify_addr =3D core_device->barmap[virtvdev->notify_bar] + - virtvdev->notify_offset; + virtvdev->notify_addr =3D io + virtvdev->notify_offset; return 0; } =20 diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index 89165b769e5c..7accd0eac457 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -188,7 +188,6 @@ int vfio_pci_core_match_token_uuid(struct vfio_device *= core_vdev, int vfio_pci_core_enable(struct vfio_pci_core_device *vdev); void vfio_pci_core_disable(struct vfio_pci_core_device *vdev); void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev); -int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar); pci_ers_result_t vfio_pci_core_aer_err_detected(struct pci_dev *pdev, pci_channel_state_t state); ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool tes= t_mem, @@ -234,6 +233,25 @@ static inline bool is_aligned_for_order(struct vm_area= _struct *vma, !IS_ALIGNED(pfn, 1 << order))); } =20 +/* + * Returns a BAR's iomap base or an ERR_PTR() if, for example, the + * BAR isn't valid, its resource wasn't acquired, or its iomap + * failed. This shall only be used after vfio_pci_core_enable() + * has set up the BAR maps and before vfio_pci_core_disable() + * tears them down. + */ +static inline void __iomem __must_check * +vfio_pci_core_get_iomap(struct vfio_pci_core_device *vdev, unsigned int ba= r) +{ + if (WARN_ON_ONCE(bar >=3D PCI_STD_NUM_BARS)) + return ERR_PTR(-EINVAL); + + if (WARN_ON_ONCE(!vdev->barmap[bar])) + return ERR_PTR(-ENODEV); + + return vdev->barmap[bar]; +} + int vfio_pci_dma_buf_iommufd_map(struct dma_buf_attachment *attachment, struct phys_vec *phys); =20 --=20 2.47.3