Linux Media Controller development
 help / color / mirror / Atom feed
* [PATCH 0/5] vfio/pci: Per-BAR excluded ranges
@ 2026-12-02  9:32 mhonap
  2026-12-02  9:32 ` [PATCH 1/5] vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw() mhonap
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: mhonap @ 2026-12-02  9:32 UTC (permalink / raw)
  To: jgg, yishaih, sumit.semwal
  Cc: kvm, virtualization, linux-media, dri-devel, linaro-mm-sig,
	mhonap, linux-cxl

From: Manish Honap <mhonap@nvidia.com>

vfio-pci keeps the user away from one range of a BAR, the MSI-X table:
reads through the device file return 0xff and writes are dropped. The
vfio-cxl series needs the same for the HDM decoder registers of a CXL
device, and Piotr's xe RFC [3] needs it for ranges of an xe BAR. Alex
pointed both at one generic interface [4]. This series adds it, with
the MSI-X table as its in-tree user.

Patch 1 moves the MSI-X and ROM windows out of the exported
vfio_pci_core_do_io_rw() into vfio_pci_bar_rw(). The VGA and
nvgrace-gpu callers lose two arguments. No functional change.

Patch 2 fixes an error path in virtio-vfio that skips
vfio_pci_core_release_dev(). Patch 3 allocates in
vfio_pci_core_init_dev() and would leak there.

Patch 3 adds a list of excluded ranges for each standard BAR, sorted by
start. Each range has flags for mmap, read and write. The lists are
built in .init and freed in .release, so readers take no lock. The
MSI-X table is the first range, excluded from read and write. If its
BIR or offset is outside a standard BAR, nothing is added and one
warning is printed.

Patch 4 refuses an mmap that overlaps a range excluded from mmap, and
reports the other areas of the BAR with a sparse mmap capability. The
MSI-X table is not excluded from mmap, so the MSI-X BAR stays mappable
as it has been since commit a32295c612c5 ("vfio-pci: Allow mapping MSIX
BAR").

Patch 5 refuses an ioeventfd over a range excluded from write, and a
dma-buf export over a range excluded from mmap. The generic test
replaces the open-coded MSI-X ioeventfd test.

After this series no in-tree range is excluded from mmap, so patch 4
and the dma-buf check in patch 5 have no in-tree trigger yet. The
vfio-cxl v7 series adds the first such range, the HDM decoder
registers, and exports vfio_pci_core_add_excluded_range() with it.
Piotr's xe ranges are the second. If you prefer, patch 4 can move into
the vfio-cxl series, in front of its first user.

Changes from vfio-cxl v6 08 to 10/31 [1]:
- Patches 1 and 2 are new.
- Patch 3 (v6 08): lists built in vfio_pci_core_init_dev() and freed in
  vfio_pci_core_release_dev() (Alex, 08/31 [2]). Entries use struct
  range. The ROM BAR keeps its range past the ROM image outside the
  list. The MSI-X table is added by its own helper.
- Patch 4 (v6 09): count the sparse areas, allocate and fill them once
  (Alex, 09/31 [6]). VFIO_REGION_INFO_CAP_MSIX_MAPPABLE is not reported
  when a range excluded from mmap covers the MSI-X table pages (Sashiko
  on Piotr's RFC 2/4 [5]).
- Patch 5 (v6 10): the dma-buf check is in validate_dmabuf_input()
  (Alex, 10/31 [7]). The open-coded MSI-X ioeventfd test is removed.
- The export of vfio_pci_core_add_excluded_range() moves to the
  vfio-cxl series, with its first caller.

Testing:
 - Series applies cleanly to v7.4-rc4 (b6a9fa34ce41)
 - Tested with a passthrough guest on VR-NVL72 system with running
   CUDA/UVM workloads in guest.
 - No vfio selftests, kmemleak or sparse were run.

[1] https://lore.kernel.org/all/20261016161839.2765592-1-mhonap@nvidia.com/
[2] https://lore.kernel.org/all/20261106085939.764b6dde@shazbot.org/
[3] https://lore.kernel.org/all/20261105153738.1938345-1-piotr.piorkowski@intel.com/
[4] https://lore.kernel.org/all/20261105085032.679881ff@shazbot.org/
[5] https://lore.kernel.org/all/sashiko-outbox-199483@kernel.org/
[6] https://lore.kernel.org/all/20261106133920.144f0665@shazbot.org/
[7] https://lore.kernel.org/all/20261106135245.2870aebb@shazbot.org/

base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f

Manish Honap (5):
  vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw()
  vfio/virtio: Release vfio-pci-core state when legacy I/O setup fails
  vfio/pci: Add per-BAR excluded ranges and put the MSI-X table on them
  vfio/pci: Advertise the mmap areas around excluded ranges
  vfio/pci: Refuse ioeventfds and dma-buf exports over excluded ranges

 drivers/vfio/pci/nvgrace-gpu/main.c |   4 +-
 drivers/vfio/pci/vfio_pci_core.c    | 178 +++++++++++++++++++++++++++-
 drivers/vfio/pci/vfio_pci_dmabuf.c  |  10 +-
 drivers/vfio/pci/vfio_pci_priv.h    |  14 +++
 drivers/vfio/pci/vfio_pci_rdwr.c    | 152 ++++++++++++++++--------
 drivers/vfio/pci/virtio/main.c      |   5 +-
 include/linux/vfio_pci_core.h       |   5 +-
 7 files changed, 312 insertions(+), 56 deletions(-)

-- 
2.53.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 1/5] vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw()
  2026-12-02  9:32 [PATCH 0/5] vfio/pci: Per-BAR excluded ranges mhonap
@ 2026-12-02  9:32 ` mhonap
  2026-12-02 23:19   ` Alex Williamson
  2026-12-02  9:32 ` [PATCH 2/5] vfio/virtio: Release vfio-pci-core state when legacy I/O setup fails mhonap
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 10+ messages in thread
From: mhonap @ 2026-12-02  9:32 UTC (permalink / raw)
  To: jgg, yishaih, sumit.semwal
  Cc: kvm, virtualization, linux-media, dri-devel, linaro-mm-sig,
	mhonap, linux-cxl

From: Manish Honap <mhonap@nvidia.com>

vfio_pci_core_do_io_rw() fills reads with 0xff and drops writes in one
excluded window given by its caller. vfio_pci_bar_rw() sets it to the
MSI-X table or to the part of a ROM BAR past the ROM image.
vfio_pci_vga_rw() and nvgrace-gpu pass an empty window.

The next patch for vfio-cxl series puts the MSI-X table on
a per-BAR list of excluded ranges, and a BAR can then have more than one
such range (CXL component register region range)

Add a helper in vfio_pci_bar_rw() that sends the bytes before a range to
the device in a call of their own, so no device access runs into the
range, and fills or drops the bytes of the range. Use it for the MSI-X
table and for the ROM BAR past the ROM image. Remove the window
arguments from vfio_pci_core_do_io_rw() and its callers.

No functional change.

Signed-off-by: Manish Honap <mhonap@nvidia.com>
---
 drivers/vfio/pci/nvgrace-gpu/main.c |   4 +-
 drivers/vfio/pci/vfio_pci_rdwr.c    | 115 ++++++++++++++++++----------
 include/linux/vfio_pci_core.h       |   3 +-
 3 files changed, 76 insertions(+), 46 deletions(-)

diff --git a/drivers/vfio/pci/nvgrace-gpu/main.c b/drivers/vfio/pci/nvgrace-gpu/main.c
index 5ddec340b73b..432f6b9c2842 100644
--- a/drivers/vfio/pci/nvgrace-gpu/main.c
+++ b/drivers/vfio/pci/nvgrace-gpu/main.c
@@ -787,7 +787,7 @@ nvgrace_gpu_map_and_read(struct nvgrace_gpu_pci_core_device *nvdev,
 		ret = vfio_pci_core_do_io_rw(&nvdev->core_device, false,
 					     nvdev->resmem.ioaddr,
 					     buf, offset, mem_count,
-					     0, 0, false, VFIO_PCI_IO_WIDTH_8);
+					     false, VFIO_PCI_IO_WIDTH_8);
 	}
 
 	return ret;
@@ -925,7 +925,7 @@ nvgrace_gpu_map_and_write(struct nvgrace_gpu_pci_core_device *nvdev,
 		ret = vfio_pci_core_do_io_rw(&nvdev->core_device, false,
 					     nvdev->resmem.ioaddr,
 					     (char __user *)buf, pos, mem_count,
-					     0, 0, true, VFIO_PCI_IO_WIDTH_8);
+					     true, VFIO_PCI_IO_WIDTH_8);
 	}
 
 	return ret;
diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
index df8848087681..68008f28bcbe 100644
--- a/drivers/vfio/pci/vfio_pci_rdwr.c
+++ b/drivers/vfio/pci/vfio_pci_rdwr.c
@@ -12,6 +12,7 @@
 
 #include <linux/fs.h>
 #include <linux/pci.h>
+#include <linux/range.h>
 #include <linux/uaccess.h>
 #include <linux/io.h>
 #include <linux/vfio.h>
@@ -126,66 +127,41 @@ VFIO_IORDWR(16)
 VFIO_IORDWR(32)
 VFIO_IORDWR(64)
 
-/*
- * Read or write from an __iomem region (MMIO or I/O port) with an excluded
- * range which is inaccessible.  The excluded range drops writes and fills
- * reads with -1.  This is intended for handling MSI-X vector tables and
- * leftover space for ROM BARs.
- */
+/* Read or write from an __iomem region (MMIO or I/O port) */
 ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,
 			       void __iomem *io, char __user *buf,
-			       loff_t off, size_t count, size_t x_start,
-			       size_t x_end, bool iswrite,
+			       loff_t off, size_t count, bool iswrite,
 			       enum vfio_pci_io_width max_width)
 {
 	ssize_t done = 0;
 	int ret;
 
 	while (count) {
-		size_t fillable, filled;
-
-		if (off < x_start)
-			fillable = min(count, (size_t)(x_start - off));
-		else if (off >= x_end)
-			fillable = count;
-		else
-			fillable = 0;
+		size_t filled;
 
-		if (fillable >= 8 && !(off % 8) && max_width >= 8) {
+		if (count >= 8 && !(off % 8) && max_width >= 8) {
 			ret = vfio_pci_iordwr64(vdev, iswrite, test_mem,
 						io, buf, off, &filled);
 			if (ret)
 				return ret;
 
-		} else if (fillable >= 4 && !(off % 4) && max_width >= 4) {
+		} else if (count >= 4 && !(off % 4) && max_width >= 4) {
 			ret = vfio_pci_iordwr32(vdev, iswrite, test_mem,
 						io, buf, off, &filled);
 			if (ret)
 				return ret;
 
-		} else if (fillable >= 2 && !(off % 2) && max_width >= 2) {
+		} else if (count >= 2 && !(off % 2) && max_width >= 2) {
 			ret = vfio_pci_iordwr16(vdev, iswrite, test_mem,
 						io, buf, off, &filled);
 			if (ret)
 				return ret;
 
-		} else if (fillable) {
+		} else {
 			ret = vfio_pci_iordwr8(vdev, iswrite, test_mem,
 					       io, buf, off, &filled);
 			if (ret)
 				return ret;
-
-		} else {
-			/* Fill reads with -1, drop writes */
-			filled = min(count, (size_t)(x_end - off));
-			if (!iswrite) {
-				u8 val = 0xFF;
-				size_t i;
-
-				for (i = 0; i < filled; i++)
-					if (copy_to_user(buf + i, &val, 1))
-						return -EFAULT;
-			}
 		}
 
 		count -= filled;
@@ -198,17 +174,55 @@ ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,
 }
 EXPORT_SYMBOL_GPL(vfio_pci_core_do_io_rw);
 
+static ssize_t vfio_pci_excluded_range_rw(struct vfio_pci_core_device *vdev,
+					  bool test_mem, void __iomem *io,
+					  char __user *buf, loff_t pos,
+					  size_t count, const struct range *x,
+					  bool iswrite,
+					  enum vfio_pci_io_width max_width)
+{
+	size_t before = 0, len, i;
+	ssize_t ret;
+
+	if (pos < x->start) {
+		before = min_t(u64, count, x->start - pos);
+		ret = vfio_pci_core_do_io_rw(vdev, test_mem, io, buf, pos,
+					     before, iswrite, max_width);
+		if (ret < 0)
+			return ret;
+		if (before == count)
+			return before;
+		pos += before;
+		buf += before;
+		count -= before;
+	}
+
+	if (pos > x->end)
+		return before;
+
+	len = min_t(u64, count, x->end - pos + 1);
+	if (!iswrite) {
+		u8 val = 0xFF;
+
+		for (i = 0; i < len; i++)
+			if (copy_to_user(buf + i, &val, 1))
+				return -EFAULT;
+	}
+
+	return before + len;
+}
+
 ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 			size_t count, loff_t *ppos, bool iswrite)
 {
 	struct pci_dev *pdev = vdev->pdev;
 	loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
 	int bar = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
-	size_t x_start = 0, x_end = 0;
 	resource_size_t end;
 	void __iomem *io;
 	struct resource *res = &vdev->pdev->resource[bar];
-	ssize_t done;
+	bool test_mem = res->flags & IORESOURCE_MEM;
+	ssize_t done = 0, ret;
 	enum vfio_pci_io_width max_width = VFIO_PCI_IO_WIDTH_8;
 
 	if (pci_resource_start(pdev, bar))
@@ -224,20 +238,21 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 	count = min(count, (size_t)(end - pos));
 
 	if (bar == PCI_ROM_RESOURCE) {
+		size_t rom_len;
+
 		/*
 		 * The ROM can fill less space than the BAR, so we start the
 		 * excluded range at the end of the actual ROM.  This makes
 		 * filling large ROM BARs much faster.
 		 */
 		if (pci_resource_start(pdev, bar)) {
-			io = pci_map_rom(pdev, &x_start);
+			io = pci_map_rom(pdev, &rom_len);
 		} else {
 			io = ioremap(pdev->rom, pdev->romlen);
-			x_start = pdev->romlen;
+			rom_len = pdev->romlen;
 		}
 		if (!io)
 			return -ENOMEM;
-		x_end = end;
 
 		/*
 		 * Certain devices (e.g. Intel X710) don't support qword
@@ -248,6 +263,14 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 		 * worked reliably for years before qword access is enabled.
 		 */
 		max_width = VFIO_PCI_IO_WIDTH_4;
+
+		if (rom_len < end) {
+			struct range x = DEFINE_RANGE(rom_len, end - 1);
+
+			done = vfio_pci_excluded_range_rw(vdev, test_mem, io, buf,
+							  pos, count, &x, iswrite,
+							  max_width);
+		}
 	} else {
 		io = vfio_pci_core_get_iomap(vdev, bar);
 		if (IS_ERR(io)) {
@@ -257,12 +280,20 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 	}
 
 	if (bar == vdev->msix_bar) {
-		x_start = vdev->msix_offset;
-		x_end = vdev->msix_offset + vdev->msix_size;
+		u64 start = vdev->msix_offset;
+		struct range x = DEFINE_RANGE(start,
+					      start + vdev->msix_size - 1);
+
+		done = vfio_pci_excluded_range_rw(vdev, test_mem, io, buf, pos,
+						  count, &x, iswrite, max_width);
 	}
 
-	done = vfio_pci_core_do_io_rw(vdev, res->flags & IORESOURCE_MEM, io, buf, pos,
-				      count, x_start, x_end, iswrite, max_width);
+	if (done >= 0) {
+		ret = vfio_pci_core_do_io_rw(vdev, test_mem, io, buf + done,
+					     pos + done, count - done, iswrite,
+					     max_width);
+		done = ret < 0 ? ret : done + ret;
+	}
 
 	if (done >= 0)
 		*ppos += done;
@@ -336,7 +367,7 @@ ssize_t vfio_pci_vga_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 	 * to the memory enable bit in the command register.
 	 */
 	done = vfio_pci_core_do_io_rw(vdev, false, iomem, buf, off, count,
-				      0, 0, iswrite, VFIO_PCI_IO_WIDTH_4);
+				      iswrite, VFIO_PCI_IO_WIDTH_4);
 
 	vga_put(vdev->pdev, rsrc);
 
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index dc25f737fe93..e472777afc16 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -197,8 +197,7 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev);
 void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev);
 ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,
 			       void __iomem *io, char __user *buf,
-			       loff_t off, size_t count, size_t x_start,
-			       size_t x_end, bool iswrite,
+			       loff_t off, size_t count, bool iswrite,
 			       enum vfio_pci_io_width max_width);
 bool __vfio_pci_memory_enabled(struct vfio_pci_core_device *vdev);
 bool vfio_pci_core_range_intersect_range(loff_t buf_start, size_t buf_cnt,
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 2/5] vfio/virtio: Release vfio-pci-core state when legacy I/O setup fails
  2026-12-02  9:32 [PATCH 0/5] vfio/pci: Per-BAR excluded ranges mhonap
  2026-12-02  9:32 ` [PATCH 1/5] vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw() mhonap
@ 2026-12-02  9:32 ` mhonap
  2026-12-02 23:19   ` Alex Williamson
  2026-12-02  9:32 ` [PATCH 3/5] vfio/pci: Add per-BAR excluded ranges and put the MSI-X table on them mhonap
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 10+ messages in thread
From: mhonap @ 2026-12-02  9:32 UTC (permalink / raw)
  To: jgg, yishaih, sumit.semwal
  Cc: kvm, virtualization, linux-media, dri-devel, linaro-mm-sig,
	mhonap, linux-cxl

From: Manish Honap <mhonap@nvidia.com>

virtiovf_pci_init_device() returns the error of virtiovf_init_legacy_io()
without undoing vfio_pci_core_init_dev(). vfio_init_device() does not
call .release when .init fails, so the state vfio_pci_core_init_dev()
set up is not released.

Today that state needs no freeing. vfio_pci_core_init_dev() is about to
allocate an excluded-range entry for the MSI-X table, which would leak on
this path. Call vfio_pci_core_release_dev() when
virtiovf_init_legacy_io() fails.

Signed-off-by: Manish Honap <mhonap@nvidia.com>
---
 drivers/vfio/pci/virtio/main.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/vfio/pci/virtio/main.c b/drivers/vfio/pci/virtio/main.c
index 123ff5193812..f1ca774889f8 100644
--- a/drivers/vfio/pci/virtio/main.c
+++ b/drivers/vfio/pci/virtio/main.c
@@ -69,7 +69,10 @@ static int virtiovf_pci_init_device(struct vfio_device *core_vdev)
 	 * The vfio_device_ops.init() callback is set to virtiovf_pci_init_device()
 	 * only when legacy I/O is supported. Now, let's initialize it.
 	 */
-	return virtiovf_init_legacy_io(virtvdev);
+	ret = virtiovf_init_legacy_io(virtvdev);
+	if (ret)
+		vfio_pci_core_release_dev(core_vdev);
+	return ret;
 }
 #endif
 
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 3/5] vfio/pci: Add per-BAR excluded ranges and put the MSI-X table on them
  2026-12-02  9:32 [PATCH 0/5] vfio/pci: Per-BAR excluded ranges mhonap
  2026-12-02  9:32 ` [PATCH 1/5] vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw() mhonap
  2026-12-02  9:32 ` [PATCH 2/5] vfio/virtio: Release vfio-pci-core state when legacy I/O setup fails mhonap
@ 2026-12-02  9:32 ` mhonap
  2026-12-02  9:32 ` [PATCH 4/5] vfio/pci: Advertise the mmap areas around excluded ranges mhonap
  2026-12-02  9:32 ` [PATCH 5/5] vfio/pci: Refuse ioeventfds and dma-buf exports over " mhonap
  4 siblings, 0 replies; 10+ messages in thread
From: mhonap @ 2026-12-02  9:32 UTC (permalink / raw)
  To: jgg, yishaih, sumit.semwal
  Cc: kvm, virtualization, linux-media, dri-devel, linaro-mm-sig,
	mhonap, linux-cxl

From: Manish Honap <mhonap@nvidia.com>

vfio_pci_bar_rw() fills reads with 0xff and drops writes for one range
of a BAR, the MSI-X table. vfio-cxl will add the HDM decoder registers
of a CXL device as a second range, so a BAR can then hold more than
one excluded range.

Keep a list of excluded ranges for each standard BAR in struct
vfio_pci_core_device, sorted by start. Each entry holds a struct range
and flags that say whether mmap, read or write is excluded. Adding a
range that is empty, overlaps another range or ends past the BAR fails
with -EINVAL. The lists are built in vfio_pci_core_init_dev() and freed
in vfio_pci_core_release_dev(). They do not change while the device is
registered, so readers take no lock.

vfio_pci_bar_rw() walks the list in order, so one access can cross
several ranges. The bytes outside the ranges go to the device as before.
The ROM BAR has no list and keeps its own range past the ROM image.

Put the MSI-X table on the list, excluded from read and write. Only the
part of the table inside its BAR is added. If the table BIR or offset
does not point inside a standard BAR, nothing is added, a warning is
printed and the device still opens. An MSI-X table inside a standard
BAR behaves as before.

Signed-off-by: Manish Honap <mhonap@nvidia.com>
---
 drivers/vfio/pci/vfio_pci_core.c | 106 ++++++++++++++++++++++++++++++-
 drivers/vfio/pci/vfio_pci_priv.h |  11 ++++
 drivers/vfio/pci/vfio_pci_rdwr.c |  43 ++++++++++---
 include/linux/vfio_pci_core.h    |   2 +
 4 files changed, 153 insertions(+), 9 deletions(-)

diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index fcbb2da860e2..ec09d58405fd 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -2165,6 +2165,107 @@ static void vfio_pci_vga_uninit(struct vfio_pci_core_device *vdev)
 					      VGA_RSRC_LEGACY_MEM);
 }
 
+/**
+ * vfio_pci_core_add_excluded_range() - Exclude a BAR sub-range from user access
+ * @vdev: vfio PCI device
+ * @bar: BAR index, below PCI_STD_NUM_BARS
+ * @start: offset of the range in @bar
+ * @size: length of the range in bytes
+ * @flags: VFIO_PCI_EXCLUDE_* accesses the range is excluded from
+ *
+ * Ranges on one BAR do not overlap and lie inside the BAR.
+ *
+ * Return: 0, -EINVAL for an invalid or overlapping range, or -ENOMEM.
+ */
+static int vfio_pci_core_add_excluded_range(struct vfio_pci_core_device *vdev,
+					    int bar, u64 start, u64 size,
+					    u32 flags)
+{
+	const u32 valid = VFIO_PCI_EXCLUDE_MMAP | VFIO_PCI_EXCLUDE_READ |
+			  VFIO_PCI_EXCLUDE_WRITE;
+	struct vfio_pci_excluded_range *x, *pos;
+	struct range r, bar_range;
+	u64 bar_len, end;
+
+	if (bar < 0 || bar >= PCI_STD_NUM_BARS || !flags || (flags & ~valid))
+		return -EINVAL;
+
+	bar_len = pci_resource_len(vdev->pdev, bar);
+	if (!bar_len)
+		return -EINVAL;
+
+	bar_range = DEFINE_RANGE(0, bar_len - 1);
+
+	if (!size || check_add_overflow(start, size - 1, &end))
+		return -EINVAL;
+	r = DEFINE_RANGE(start, end);
+
+	if (!range_contains(&bar_range, &r))
+		return -EINVAL;
+
+	list_for_each_entry(pos, &vdev->excluded_ranges[bar], entry) {
+		if (range_overlaps(&pos->range, &r))
+			return -EINVAL;
+		if (pos->range.start > start)
+			break;
+	}
+
+	x = kmalloc_obj(*x);
+	if (!x)
+		return -ENOMEM;
+
+	x->range = r;
+	x->flags = flags;
+	list_add_tail(&x->entry, &pos->entry);
+
+	return 0;
+}
+
+static void vfio_pci_free_excluded_ranges(struct vfio_pci_core_device *vdev)
+{
+	struct vfio_pci_excluded_range *x, *tmp;
+
+	for (int bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
+		list_for_each_entry_safe(x, tmp, &vdev->excluded_ranges[bar],
+					 entry) {
+			list_del(&x->entry);
+			kfree(x);
+		}
+	}
+}
+
+static int vfio_pci_exclude_msix_table(struct vfio_pci_core_device *vdev)
+{
+	struct pci_dev *pdev = vdev->pdev;
+	u64 offset, size, bar_len;
+	u32 table;
+	u16 flags;
+	u8 bir;
+
+	if (!pdev->msix_cap)
+		return 0;
+
+	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &flags);
+	pci_read_config_dword(pdev, pdev->msix_cap + PCI_MSIX_TABLE, &table);
+	bir = table & PCI_MSIX_TABLE_BIR;
+	offset = table & PCI_MSIX_TABLE_OFFSET;
+	size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
+
+	if (bir >= PCI_STD_NUM_BARS ||
+	    offset >= pci_resource_len(pdev, bir)) {
+		pci_warn_once(pdev, "Invalid MSI-X table location (BIR %u, offset %#llx)\n",
+			      bir, offset);
+		return 0;
+	}
+
+	bar_len = pci_resource_len(pdev, bir);
+	size = min(size, bar_len - offset);
+
+	return vfio_pci_core_add_excluded_range(vdev, bir, offset, size,
+						VFIO_PCI_EXCLUDE_READ |
+						VFIO_PCI_EXCLUDE_WRITE);
+}
+
 int vfio_pci_core_init_dev(struct vfio_device *core_vdev)
 {
 	struct vfio_pci_core_device *vdev =
@@ -2185,8 +2286,10 @@ int vfio_pci_core_init_dev(struct vfio_device *core_vdev)
 	INIT_LIST_HEAD(&vdev->dmabufs);
 	init_rwsem(&vdev->memory_lock);
 	xa_init(&vdev->ctx);
+	for (int bar = 0; bar < PCI_STD_NUM_BARS; bar++)
+		INIT_LIST_HEAD(&vdev->excluded_ranges[bar]);
 
-	return 0;
+	return vfio_pci_exclude_msix_table(vdev);
 }
 EXPORT_SYMBOL_GPL(vfio_pci_core_init_dev);
 
@@ -2195,6 +2298,7 @@ void vfio_pci_core_release_dev(struct vfio_device *core_vdev)
 	struct vfio_pci_core_device *vdev =
 		container_of(core_vdev, struct vfio_pci_core_device, vdev);
 
+	vfio_pci_free_excluded_ranges(vdev);
 	mutex_destroy(&vdev->igate);
 	mutex_destroy(&vdev->ioeventfds_lock);
 	kfree(vdev->region);
diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
index 4e7162234a2e..6def038cb213 100644
--- a/drivers/vfio/pci/vfio_pci_priv.h
+++ b/drivers/vfio/pci/vfio_pci_priv.h
@@ -2,6 +2,7 @@
 #ifndef VFIO_PCI_PRIV_H
 #define VFIO_PCI_PRIV_H
 
+#include <linux/range.h>
 #include <linux/vfio_pci_core.h>
 
 /* Special capability IDs predefined access */
@@ -44,6 +45,16 @@ ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev,
 ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 			size_t count, loff_t *ppos, bool iswrite);
 
+#define VFIO_PCI_EXCLUDE_MMAP	BIT(0)
+#define VFIO_PCI_EXCLUDE_READ	BIT(1)
+#define VFIO_PCI_EXCLUDE_WRITE	BIT(2)
+
+struct vfio_pci_excluded_range {
+	struct list_head	entry;
+	struct range		range;
+	u32			flags;
+};
+
 #ifdef CONFIG_VFIO_PCI_VGA
 ssize_t vfio_pci_vga_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 			size_t count, loff_t *ppos, bool iswrite);
diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
index 68008f28bcbe..04aed725325c 100644
--- a/drivers/vfio/pci/vfio_pci_rdwr.c
+++ b/drivers/vfio/pci/vfio_pci_rdwr.c
@@ -212,6 +212,38 @@ static ssize_t vfio_pci_excluded_range_rw(struct vfio_pci_core_device *vdev,
 	return before + len;
 }
 
+static ssize_t vfio_pci_excluded_ranges_rw(struct vfio_pci_core_device *vdev,
+					   int bar, bool test_mem,
+					   void __iomem *io, char __user *buf,
+					   loff_t pos, size_t count,
+					   bool iswrite,
+					   enum vfio_pci_io_width max_width)
+{
+	u32 want = iswrite ? VFIO_PCI_EXCLUDE_WRITE : VFIO_PCI_EXCLUDE_READ;
+	struct range access = DEFINE_RANGE(pos, pos + count - 1);
+	struct vfio_pci_excluded_range *x;
+	ssize_t done = 0, ret;
+
+	list_for_each_entry(x, &vdev->excluded_ranges[bar], entry) {
+		if (!(x->flags & want) || !range_overlaps(&x->range, &access))
+			continue;
+
+		ret = vfio_pci_excluded_range_rw(vdev, test_mem, io, buf + done,
+						 access.start,
+						 range_len(&access), &x->range,
+						 iswrite, max_width);
+		if (ret < 0)
+			return ret;
+
+		done += ret;
+		access.start += ret;
+		if (access.start > access.end)
+			break;
+	}
+
+	return done;
+}
+
 ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 			size_t count, loff_t *ppos, bool iswrite)
 {
@@ -277,15 +309,10 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 			done = PTR_ERR(io);
 			goto out;
 		}
-	}
-
-	if (bar == vdev->msix_bar) {
-		u64 start = vdev->msix_offset;
-		struct range x = DEFINE_RANGE(start,
-					      start + vdev->msix_size - 1);
 
-		done = vfio_pci_excluded_range_rw(vdev, test_mem, io, buf, pos,
-						  count, &x, iswrite, max_width);
+		done = vfio_pci_excluded_ranges_rw(vdev, bar, test_mem, io,
+						   buf, pos, count, iswrite,
+						   max_width);
 	}
 
 	if (done >= 0) {
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index e472777afc16..baac2eaeb929 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -152,6 +152,8 @@ struct vfio_pci_core_device {
 	struct notifier_block	nb;
 	struct rw_semaphore	memory_lock;
 	struct list_head	dmabufs;
+	/* Set before registration and freed at release: readers take no lock */
+	struct list_head	excluded_ranges[PCI_STD_NUM_BARS];
 };
 
 enum vfio_pci_io_width {
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 4/5] vfio/pci: Advertise the mmap areas around excluded ranges
  2026-12-02  9:32 [PATCH 0/5] vfio/pci: Per-BAR excluded ranges mhonap
                   ` (2 preceding siblings ...)
  2026-12-02  9:32 ` [PATCH 3/5] vfio/pci: Add per-BAR excluded ranges and put the MSI-X table on them mhonap
@ 2026-12-02  9:32 ` mhonap
  2026-12-02 23:19   ` Alex Williamson
  2026-12-02  9:32 ` [PATCH 5/5] vfio/pci: Refuse ioeventfds and dma-buf exports over " mhonap
  4 siblings, 1 reply; 10+ messages in thread
From: mhonap @ 2026-12-02  9:32 UTC (permalink / raw)
  To: jgg, yishaih, sumit.semwal
  Cc: kvm, virtualization, linux-media, dri-devel, linaro-mm-sig,
	mhonap, linux-cxl

From: Manish Honap <mhonap@nvidia.com>

A range excluded from mmap must not be reachable through an mmap of its
BAR. Refuse an mmap that overlaps such a range, and advertise the BAR
with a sparse mmap capability that lists the areas between the excluded
ranges, so a VMM maps those and traps the rest.

Commit a32295c612c5 ("vfio-pci: Allow mapping MSIX BAR") removed such a
hole around the MSI-X table, because with pages larger than 4KB it also
hid the registers next to the table. The hole comes back only for ranges
excluded from mmap. VFIO_REGION_INFO_CAP_MSIX_MAPPABLE is not reported
when such a range covers the pages of the MSI-X table.

A BAR without mmap-excluded range does not get new capability. The MSI-X
table is excluded from read and write only, so the MSI-X BAR is
unchanged.

Signed-off-by: Manish Honap <mhonap@nvidia.com>
---
 drivers/vfio/pci/vfio_pci_core.c | 72 ++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)

diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index ec09d58405fd..0b9da823456e 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -988,17 +988,81 @@ static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
 	return walk.ret;
 }
 
+static bool vfio_pci_bar_range_excluded(struct vfio_pci_core_device *vdev,
+					int bar, u64 start, u64 len, u32 flags)
+{
+	struct range r = DEFINE_RANGE(start, start + len - 1);
+	struct vfio_pci_excluded_range *x;
+
+	list_for_each_entry(x, &vdev->excluded_ranges[bar], entry) {
+		if ((x->flags & flags) && range_overlaps(&x->range, &r))
+			return true;
+	}
+
+	return false;
+}
+
 static int msix_mmappable_cap(struct vfio_pci_core_device *vdev,
 			      struct vfio_info_cap *caps)
 {
+	u64 start = ALIGN_DOWN(vdev->msix_offset, PAGE_SIZE);
+	u64 end = ALIGN((u64)vdev->msix_offset + vdev->msix_size, PAGE_SIZE);
 	struct vfio_info_cap_header header = {
 		.id = VFIO_REGION_INFO_CAP_MSIX_MAPPABLE,
 		.version = 1
 	};
 
+	if (vfio_pci_bar_range_excluded(vdev, vdev->msix_bar, start,
+					end - start, VFIO_PCI_EXCLUDE_MMAP))
+		return 0;
+
 	return vfio_info_add_capability(caps, &header, sizeof(header));
 }
 
+static int vfio_pci_excluded_sparse_cap(struct vfio_pci_core_device *vdev,
+					int bar, struct vfio_info_cap *caps)
+{
+	u64 bar_len = pci_resource_len(vdev->pdev, bar);
+	struct vfio_pci_excluded_range *x;
+	u32 count = 0, i = 0;
+	u64 pos = 0;
+
+	list_for_each_entry(x, &vdev->excluded_ranges[bar], entry) {
+		if (x->flags & VFIO_PCI_EXCLUDE_MMAP)
+			count++;
+	}
+	if (!count)
+		return 0;
+
+	struct vfio_region_info_cap_sparse_mmap *sparse __free(kfree) =
+		kzalloc_flex(*sparse, areas, count + 1);
+	if (!sparse)
+		return -ENOMEM;
+
+	list_for_each_entry(x, &vdev->excluded_ranges[bar], entry) {
+		u64 hole = ALIGN_DOWN(x->range.start, PAGE_SIZE);
+
+		if (!(x->flags & VFIO_PCI_EXCLUDE_MMAP))
+			continue;
+		if (hole > pos) {
+			sparse->areas[i].offset = pos;
+			sparse->areas[i++].size = hole - pos;
+		}
+		pos = max(pos, ALIGN(x->range.end + 1, PAGE_SIZE));
+	}
+	if (pos < bar_len) {
+		sparse->areas[i].offset = pos;
+		sparse->areas[i++].size = bar_len - pos;
+	}
+
+	sparse->header.id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
+	sparse->header.version = 1;
+	sparse->nr_areas = i;
+
+	return vfio_info_add_capability(caps, &sparse->header,
+					struct_size(sparse, areas, i));
+}
+
 int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev,
 				      unsigned int type, unsigned int subtype,
 				      const struct vfio_pci_regops *ops,
@@ -1147,6 +1211,10 @@ int vfio_pci_ioctl_get_region_info(struct vfio_device *core_vdev,
 				if (ret)
 					return ret;
 			}
+			ret = vfio_pci_excluded_sparse_cap(vdev, info->index,
+							   caps);
+			if (ret)
+				return ret;
 		}
 
 		break;
@@ -1840,6 +1908,10 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma
 	if (req_start + req_len > phys_len)
 		return -EINVAL;
 
+	if (vfio_pci_bar_range_excluded(vdev, index, req_start, req_len,
+					VFIO_PCI_EXCLUDE_MMAP))
+		return -EINVAL;
+
 	/*
 	 * Ensure the BAR resource region is reserved for use.
 	 */
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 5/5] vfio/pci: Refuse ioeventfds and dma-buf exports over excluded ranges
  2026-12-02  9:32 [PATCH 0/5] vfio/pci: Per-BAR excluded ranges mhonap
                   ` (3 preceding siblings ...)
  2026-12-02  9:32 ` [PATCH 4/5] vfio/pci: Advertise the mmap areas around excluded ranges mhonap
@ 2026-12-02  9:32 ` mhonap
  2026-12-02 23:19   ` Alex Williamson
  4 siblings, 1 reply; 10+ messages in thread
From: mhonap @ 2026-12-02  9:32 UTC (permalink / raw)
  To: jgg, yishaih, sumit.semwal
  Cc: kvm, virtualization, linux-media, dri-devel, linaro-mm-sig,
	mhonap, linux-cxl, Alex Williamson

From: Manish Honap <mhonap@nvidia.com>

vfio_pci_bar_rw() keeps the excluded ranges of a BAR off the device, but
two other paths can reach BAR MMIO without it.

An ioeventfd writes the BAR directly when it fires, through
vfio_pci_core_iowriteN(). Refuse an ioeventfd whose range overlaps a
write-excluded range. This replaces the open-coded MSI-X test: the MSI-X
table is a write-excluded range, and the BAR length test already refuses
the part of a table past the BAR.

A dma-buf export of a BAR range lets an importer map it for DMA, which
reaches a range that the BAR keeps off mmap. Refuse an export whose
ranges overlap an mmap-excluded range in validate_dmabuf_input(), so the
dma-buf follows the same rule as mmap.

Suggested-by: Alex Williamson <alex@shazbot.org>
Signed-off-by: Manish Honap <mhonap@nvidia.com>
---
 drivers/vfio/pci/vfio_pci_core.c   |  4 ++--
 drivers/vfio/pci/vfio_pci_dmabuf.c | 10 ++++++++--
 drivers/vfio/pci/vfio_pci_priv.h   |  3 +++
 drivers/vfio/pci/vfio_pci_rdwr.c   |  6 ++----
 4 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 0b9da823456e..5d54d624982f 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -988,8 +988,8 @@ static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
 	return walk.ret;
 }
 
-static bool vfio_pci_bar_range_excluded(struct vfio_pci_core_device *vdev,
-					int bar, u64 start, u64 len, u32 flags)
+bool vfio_pci_bar_range_excluded(struct vfio_pci_core_device *vdev, int bar,
+				 u64 start, u64 len, u32 flags)
 {
 	struct range r = DEFINE_RANGE(start, start + len - 1);
 	struct vfio_pci_excluded_range *x;
diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci_dmabuf.c
index 7d1d34029b95..6f28cc285e78 100644
--- a/drivers/vfio/pci/vfio_pci_dmabuf.c
+++ b/drivers/vfio/pci/vfio_pci_dmabuf.c
@@ -193,7 +193,8 @@ int vfio_pci_core_get_dmabuf_phys(struct vfio_pci_core_device *vdev,
 }
 EXPORT_SYMBOL_GPL(vfio_pci_core_get_dmabuf_phys);
 
-static int validate_dmabuf_input(struct vfio_device_feature_dma_buf *dma_buf,
+static int validate_dmabuf_input(struct vfio_pci_core_device *vdev,
+				 struct vfio_device_feature_dma_buf *dma_buf,
 				 struct vfio_region_dma_range *dma_ranges,
 				 size_t *lengthp)
 {
@@ -207,6 +208,11 @@ static int validate_dmabuf_input(struct vfio_device_feature_dma_buf *dma_buf,
 		if (!len || !PAGE_ALIGNED(offset) || !PAGE_ALIGNED(len))
 			return -EINVAL;
 
+		if (vfio_pci_bar_range_excluded(vdev, dma_buf->region_index,
+						offset, len,
+						VFIO_PCI_EXCLUDE_MMAP))
+			return -EINVAL;
+
 		if (check_add_overflow(length, len, &length))
 			return -EINVAL;
 	}
@@ -260,7 +266,7 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
 	if (IS_ERR(dma_ranges))
 		return PTR_ERR(dma_ranges);
 
-	ret = validate_dmabuf_input(&get_dma_buf, dma_ranges, &length);
+	ret = validate_dmabuf_input(vdev, &get_dma_buf, dma_ranges, &length);
 	if (ret)
 		goto err_free_ranges;
 
diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
index 6def038cb213..a969ae3025c9 100644
--- a/drivers/vfio/pci/vfio_pci_priv.h
+++ b/drivers/vfio/pci/vfio_pci_priv.h
@@ -55,6 +55,9 @@ struct vfio_pci_excluded_range {
 	u32			flags;
 };
 
+bool vfio_pci_bar_range_excluded(struct vfio_pci_core_device *vdev, int bar,
+				 u64 start, u64 len, u32 flags);
+
 #ifdef CONFIG_VFIO_PCI_VGA
 ssize_t vfio_pci_vga_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 			size_t count, loff_t *ppos, bool iswrite);
diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
index 04aed725325c..e620798f9007 100644
--- a/drivers/vfio/pci/vfio_pci_rdwr.c
+++ b/drivers/vfio/pci/vfio_pci_rdwr.c
@@ -475,10 +475,8 @@ int vfio_pci_ioeventfd(struct vfio_pci_core_device *vdev, loff_t offset,
 	if (pos + count > pci_resource_len(pdev, bar))
 		return -EINVAL;
 
-	/* Disallow ioeventfds working around MSI-X table writes */
-	if (bar == vdev->msix_bar &&
-	    !(pos + count <= vdev->msix_offset ||
-	      pos >= vdev->msix_offset + vdev->msix_size))
+	if (vfio_pci_bar_range_excluded(vdev, bar, pos, count,
+					VFIO_PCI_EXCLUDE_WRITE))
 		return -EINVAL;
 
 	io = vfio_pci_core_get_iomap(vdev, bar);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH 5/5] vfio/pci: Refuse ioeventfds and dma-buf exports over excluded ranges
  2026-12-02  9:32 ` [PATCH 5/5] vfio/pci: Refuse ioeventfds and dma-buf exports over " mhonap
@ 2026-12-02 23:19   ` Alex Williamson
  0 siblings, 0 replies; 10+ messages in thread
From: Alex Williamson @ 2026-12-02 23:19 UTC (permalink / raw)
  To: mhonap
  Cc: jgg, yishaih, sumit.semwal, kvm, virtualization, linux-media,
	dri-devel, linaro-mm-sig, linux-cxl, alex

On Wed, 2 Dec 2026 15:02:50 +0530
<mhonap@nvidia.com> wrote:

> From: Manish Honap <mhonap@nvidia.com>
> 
> vfio_pci_bar_rw() keeps the excluded ranges of a BAR off the device, but
> two other paths can reach BAR MMIO without it.
> 
> An ioeventfd writes the BAR directly when it fires, through
> vfio_pci_core_iowriteN(). Refuse an ioeventfd whose range overlaps a
> write-excluded range. This replaces the open-coded MSI-X test: the MSI-X
> table is a write-excluded range, and the BAR length test already refuses
> the part of a table past the BAR.
> 
> A dma-buf export of a BAR range lets an importer map it for DMA, which
> reaches a range that the BAR keeps off mmap. Refuse an export whose
> ranges overlap an mmap-excluded range in validate_dmabuf_input(), so the
> dma-buf follows the same rule as mmap.

These are different things they fall into different categories.  An
ioeventfd is just a write triggered by an eventfd.  The WRITE exclusion
series is incomplete without it.  MSI-X shouldn't be left open coded
relative to ioeventfds.

dma-buf export is a memory access through DMA.  It's more related to
mmap.  It should be implemented along with VFIO_PCI_EXCLUDE_MMAP.
Thanks,

Alex

> 
> Suggested-by: Alex Williamson <alex@shazbot.org>
> Signed-off-by: Manish Honap <mhonap@nvidia.com>
> ---
>  drivers/vfio/pci/vfio_pci_core.c   |  4 ++--
>  drivers/vfio/pci/vfio_pci_dmabuf.c | 10 ++++++++--
>  drivers/vfio/pci/vfio_pci_priv.h   |  3 +++
>  drivers/vfio/pci/vfio_pci_rdwr.c   |  6 ++----
>  4 files changed, 15 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
> index 0b9da823456e..5d54d624982f 100644
> --- a/drivers/vfio/pci/vfio_pci_core.c
> +++ b/drivers/vfio/pci/vfio_pci_core.c
> @@ -988,8 +988,8 @@ static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
>  	return walk.ret;
>  }
>  
> -static bool vfio_pci_bar_range_excluded(struct vfio_pci_core_device *vdev,
> -					int bar, u64 start, u64 len, u32 flags)
> +bool vfio_pci_bar_range_excluded(struct vfio_pci_core_device *vdev, int bar,
> +				 u64 start, u64 len, u32 flags)
>  {
>  	struct range r = DEFINE_RANGE(start, start + len - 1);
>  	struct vfio_pci_excluded_range *x;
> diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci_dmabuf.c
> index 7d1d34029b95..6f28cc285e78 100644
> --- a/drivers/vfio/pci/vfio_pci_dmabuf.c
> +++ b/drivers/vfio/pci/vfio_pci_dmabuf.c
> @@ -193,7 +193,8 @@ int vfio_pci_core_get_dmabuf_phys(struct vfio_pci_core_device *vdev,
>  }
>  EXPORT_SYMBOL_GPL(vfio_pci_core_get_dmabuf_phys);
>  
> -static int validate_dmabuf_input(struct vfio_device_feature_dma_buf *dma_buf,
> +static int validate_dmabuf_input(struct vfio_pci_core_device *vdev,
> +				 struct vfio_device_feature_dma_buf *dma_buf,
>  				 struct vfio_region_dma_range *dma_ranges,
>  				 size_t *lengthp)
>  {
> @@ -207,6 +208,11 @@ static int validate_dmabuf_input(struct vfio_device_feature_dma_buf *dma_buf,
>  		if (!len || !PAGE_ALIGNED(offset) || !PAGE_ALIGNED(len))
>  			return -EINVAL;
>  
> +		if (vfio_pci_bar_range_excluded(vdev, dma_buf->region_index,
> +						offset, len,
> +						VFIO_PCI_EXCLUDE_MMAP))
> +			return -EINVAL;
> +
>  		if (check_add_overflow(length, len, &length))
>  			return -EINVAL;
>  	}
> @@ -260,7 +266,7 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
>  	if (IS_ERR(dma_ranges))
>  		return PTR_ERR(dma_ranges);
>  
> -	ret = validate_dmabuf_input(&get_dma_buf, dma_ranges, &length);
> +	ret = validate_dmabuf_input(vdev, &get_dma_buf, dma_ranges, &length);
>  	if (ret)
>  		goto err_free_ranges;
>  
> diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
> index 6def038cb213..a969ae3025c9 100644
> --- a/drivers/vfio/pci/vfio_pci_priv.h
> +++ b/drivers/vfio/pci/vfio_pci_priv.h
> @@ -55,6 +55,9 @@ struct vfio_pci_excluded_range {
>  	u32			flags;
>  };
>  
> +bool vfio_pci_bar_range_excluded(struct vfio_pci_core_device *vdev, int bar,
> +				 u64 start, u64 len, u32 flags);
> +
>  #ifdef CONFIG_VFIO_PCI_VGA
>  ssize_t vfio_pci_vga_rw(struct vfio_pci_core_device *vdev, char __user *buf,
>  			size_t count, loff_t *ppos, bool iswrite);
> diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
> index 04aed725325c..e620798f9007 100644
> --- a/drivers/vfio/pci/vfio_pci_rdwr.c
> +++ b/drivers/vfio/pci/vfio_pci_rdwr.c
> @@ -475,10 +475,8 @@ int vfio_pci_ioeventfd(struct vfio_pci_core_device *vdev, loff_t offset,
>  	if (pos + count > pci_resource_len(pdev, bar))
>  		return -EINVAL;
>  
> -	/* Disallow ioeventfds working around MSI-X table writes */
> -	if (bar == vdev->msix_bar &&
> -	    !(pos + count <= vdev->msix_offset ||
> -	      pos >= vdev->msix_offset + vdev->msix_size))
> +	if (vfio_pci_bar_range_excluded(vdev, bar, pos, count,
> +					VFIO_PCI_EXCLUDE_WRITE))
>  		return -EINVAL;
>  
>  	io = vfio_pci_core_get_iomap(vdev, bar);


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 4/5] vfio/pci: Advertise the mmap areas around excluded ranges
  2026-12-02  9:32 ` [PATCH 4/5] vfio/pci: Advertise the mmap areas around excluded ranges mhonap
@ 2026-12-02 23:19   ` Alex Williamson
  0 siblings, 0 replies; 10+ messages in thread
From: Alex Williamson @ 2026-12-02 23:19 UTC (permalink / raw)
  To: mhonap
  Cc: jgg, yishaih, sumit.semwal, kvm, virtualization, linux-media,
	dri-devel, linaro-mm-sig, linux-cxl, alex

On Wed, 2 Dec 2026 15:02:49 +0530
<mhonap@nvidia.com> wrote:

> From: Manish Honap <mhonap@nvidia.com>
> 
> A range excluded from mmap must not be reachable through an mmap of its
> BAR. Refuse an mmap that overlaps such a range, and advertise the BAR
> with a sparse mmap capability that lists the areas between the excluded
> ranges, so a VMM maps those and traps the rest.
> 
> Commit a32295c612c5 ("vfio-pci: Allow mapping MSIX BAR") removed such a
> hole around the MSI-X table, because with pages larger than 4KB it also
> hid the registers next to the table. The hole comes back only for ranges
> excluded from mmap. VFIO_REGION_INFO_CAP_MSIX_MAPPABLE is not reported
> when such a range covers the pages of the MSI-X table.
> 
> A BAR without mmap-excluded range does not get new capability. The MSI-X
> table is excluded from read and write only, so the MSI-X BAR is
> unchanged.

Why are we rehashing MSI-X here?

This should really go in with a series that actually defines an mmap
exclusion, otherwise it's dead code.  We also shouldn't preemptive
define VFIO_PCI_EXCLUDE_MMAP in the previous patch without the support
here.  Thanks,

Alex

> 
> Signed-off-by: Manish Honap <mhonap@nvidia.com>
> ---
>  drivers/vfio/pci/vfio_pci_core.c | 72 ++++++++++++++++++++++++++++++++
>  1 file changed, 72 insertions(+)
> 
> diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
> index ec09d58405fd..0b9da823456e 100644
> --- a/drivers/vfio/pci/vfio_pci_core.c
> +++ b/drivers/vfio/pci/vfio_pci_core.c
> @@ -988,17 +988,81 @@ static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
>  	return walk.ret;
>  }
>  
> +static bool vfio_pci_bar_range_excluded(struct vfio_pci_core_device *vdev,
> +					int bar, u64 start, u64 len, u32 flags)
> +{
> +	struct range r = DEFINE_RANGE(start, start + len - 1);
> +	struct vfio_pci_excluded_range *x;
> +
> +	list_for_each_entry(x, &vdev->excluded_ranges[bar], entry) {
> +		if ((x->flags & flags) && range_overlaps(&x->range, &r))
> +			return true;
> +	}
> +
> +	return false;
> +}
> +
>  static int msix_mmappable_cap(struct vfio_pci_core_device *vdev,
>  			      struct vfio_info_cap *caps)
>  {
> +	u64 start = ALIGN_DOWN(vdev->msix_offset, PAGE_SIZE);
> +	u64 end = ALIGN((u64)vdev->msix_offset + vdev->msix_size, PAGE_SIZE);
>  	struct vfio_info_cap_header header = {
>  		.id = VFIO_REGION_INFO_CAP_MSIX_MAPPABLE,
>  		.version = 1
>  	};
>  
> +	if (vfio_pci_bar_range_excluded(vdev, vdev->msix_bar, start,
> +					end - start, VFIO_PCI_EXCLUDE_MMAP))
> +		return 0;
> +
>  	return vfio_info_add_capability(caps, &header, sizeof(header));
>  }
>  
> +static int vfio_pci_excluded_sparse_cap(struct vfio_pci_core_device *vdev,
> +					int bar, struct vfio_info_cap *caps)
> +{
> +	u64 bar_len = pci_resource_len(vdev->pdev, bar);
> +	struct vfio_pci_excluded_range *x;
> +	u32 count = 0, i = 0;
> +	u64 pos = 0;
> +
> +	list_for_each_entry(x, &vdev->excluded_ranges[bar], entry) {
> +		if (x->flags & VFIO_PCI_EXCLUDE_MMAP)
> +			count++;
> +	}
> +	if (!count)
> +		return 0;
> +
> +	struct vfio_region_info_cap_sparse_mmap *sparse __free(kfree) =
> +		kzalloc_flex(*sparse, areas, count + 1);
> +	if (!sparse)
> +		return -ENOMEM;
> +
> +	list_for_each_entry(x, &vdev->excluded_ranges[bar], entry) {
> +		u64 hole = ALIGN_DOWN(x->range.start, PAGE_SIZE);
> +
> +		if (!(x->flags & VFIO_PCI_EXCLUDE_MMAP))
> +			continue;
> +		if (hole > pos) {
> +			sparse->areas[i].offset = pos;
> +			sparse->areas[i++].size = hole - pos;
> +		}
> +		pos = max(pos, ALIGN(x->range.end + 1, PAGE_SIZE));
> +	}
> +	if (pos < bar_len) {
> +		sparse->areas[i].offset = pos;
> +		sparse->areas[i++].size = bar_len - pos;
> +	}
> +
> +	sparse->header.id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
> +	sparse->header.version = 1;
> +	sparse->nr_areas = i;
> +
> +	return vfio_info_add_capability(caps, &sparse->header,
> +					struct_size(sparse, areas, i));
> +}
> +
>  int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev,
>  				      unsigned int type, unsigned int subtype,
>  				      const struct vfio_pci_regops *ops,
> @@ -1147,6 +1211,10 @@ int vfio_pci_ioctl_get_region_info(struct vfio_device *core_vdev,
>  				if (ret)
>  					return ret;
>  			}
> +			ret = vfio_pci_excluded_sparse_cap(vdev, info->index,
> +							   caps);
> +			if (ret)
> +				return ret;
>  		}
>  
>  		break;
> @@ -1840,6 +1908,10 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma
>  	if (req_start + req_len > phys_len)
>  		return -EINVAL;
>  
> +	if (vfio_pci_bar_range_excluded(vdev, index, req_start, req_len,
> +					VFIO_PCI_EXCLUDE_MMAP))
> +		return -EINVAL;
> +
>  	/*
>  	 * Ensure the BAR resource region is reserved for use.
>  	 */


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 2/5] vfio/virtio: Release vfio-pci-core state when legacy I/O setup fails
  2026-12-02  9:32 ` [PATCH 2/5] vfio/virtio: Release vfio-pci-core state when legacy I/O setup fails mhonap
@ 2026-12-02 23:19   ` Alex Williamson
  0 siblings, 0 replies; 10+ messages in thread
From: Alex Williamson @ 2026-12-02 23:19 UTC (permalink / raw)
  To: mhonap
  Cc: jgg, yishaih, sumit.semwal, kvm, virtualization, linux-media,
	dri-devel, linaro-mm-sig, linux-cxl, alex

On Wed, 2 Dec 2026 15:02:47 +0530
<mhonap@nvidia.com> wrote:

> From: Manish Honap <mhonap@nvidia.com>
> 
> virtiovf_pci_init_device() returns the error of virtiovf_init_legacy_io()
> without undoing vfio_pci_core_init_dev(). vfio_init_device() does not
> call .release when .init fails, so the state vfio_pci_core_init_dev()
> set up is not released.
> 
> Today that state needs no freeing. vfio_pci_core_init_dev() is about to
> allocate an excluded-range entry for the MSI-X table, which would leak on
> this path. Call vfio_pci_core_release_dev() when
> virtiovf_init_legacy_io() fails.
> 
> Signed-off-by: Manish Honap <mhonap@nvidia.com>
> ---
>  drivers/vfio/pci/virtio/main.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/vfio/pci/virtio/main.c b/drivers/vfio/pci/virtio/main.c
> index 123ff5193812..f1ca774889f8 100644
> --- a/drivers/vfio/pci/virtio/main.c
> +++ b/drivers/vfio/pci/virtio/main.c
> @@ -69,7 +69,10 @@ static int virtiovf_pci_init_device(struct vfio_device *core_vdev)
>  	 * The vfio_device_ops.init() callback is set to virtiovf_pci_init_device()
>  	 * only when legacy I/O is supported. Now, let's initialize it.
>  	 */
> -	return virtiovf_init_legacy_io(virtvdev);
> +	ret = virtiovf_init_legacy_io(virtvdev);
> +	if (ret)
> +		vfio_pci_core_release_dev(core_vdev);
> +	return ret;
>  }
>  #endif
>  

This should be framed as a fix, even without the coming list
allocation, the mutex init vs destroy is unbalanced.  The QAT driver is
the only other variant driver with failures after
vfio_pci_core_init_dev() and it correctly calls release.  The bug seems
to date back to eb61eca0e8c3.

Also note that in patch 3, you're introducing a call that can fail at
the end of vfio_pci_core_init_dev() that also doesn't unwind.  That
needs to be fixed inside vfio_pci_core_init_dev().  Thanks,

Alex

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 1/5] vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw()
  2026-12-02  9:32 ` [PATCH 1/5] vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw() mhonap
@ 2026-12-02 23:19   ` Alex Williamson
  0 siblings, 0 replies; 10+ messages in thread
From: Alex Williamson @ 2026-12-02 23:19 UTC (permalink / raw)
  To: mhonap
  Cc: jgg, yishaih, sumit.semwal, kvm, virtualization, linux-media,
	dri-devel, linaro-mm-sig, linux-cxl, alex

On Wed, 2 Dec 2026 15:02:46 +0530
<mhonap@nvidia.com> wrote:

> From: Manish Honap <mhonap@nvidia.com>
> 
> vfio_pci_core_do_io_rw() fills reads with 0xff and drops writes in one
> excluded window given by its caller. vfio_pci_bar_rw() sets it to the
> MSI-X table or to the part of a ROM BAR past the ROM image.
> vfio_pci_vga_rw() and nvgrace-gpu pass an empty window.
> 
> The next patch for vfio-cxl series puts the MSI-X table on
> a per-BAR list of excluded ranges, and a BAR can then have more than one
> such range (CXL component register region range)

The "next patch" reference should be avoided in commit logs, series are
an ephemeral thing that exist on the mailing list, and especially here,
it's not even referring to the next patch in this series.

> Add a helper in vfio_pci_bar_rw() that sends the bytes before a range to
> the device in a call of their own, so no device access runs into the
> range, and fills or drops the bytes of the range. Use it for the MSI-X
> table and for the ROM BAR past the ROM image. Remove the window
> arguments from vfio_pci_core_do_io_rw() and its callers.
> 
> No functional change.
> 
> Signed-off-by: Manish Honap <mhonap@nvidia.com>
> ---
>  drivers/vfio/pci/nvgrace-gpu/main.c |   4 +-
>  drivers/vfio/pci/vfio_pci_rdwr.c    | 115 ++++++++++++++++++----------
>  include/linux/vfio_pci_core.h       |   3 +-
>  3 files changed, 76 insertions(+), 46 deletions(-)
> 
> diff --git a/drivers/vfio/pci/nvgrace-gpu/main.c b/drivers/vfio/pci/nvgrace-gpu/main.c
> index 5ddec340b73b..432f6b9c2842 100644
> --- a/drivers/vfio/pci/nvgrace-gpu/main.c
> +++ b/drivers/vfio/pci/nvgrace-gpu/main.c
> @@ -787,7 +787,7 @@ nvgrace_gpu_map_and_read(struct nvgrace_gpu_pci_core_device *nvdev,
>  		ret = vfio_pci_core_do_io_rw(&nvdev->core_device, false,
>  					     nvdev->resmem.ioaddr,
>  					     buf, offset, mem_count,
> -					     0, 0, false, VFIO_PCI_IO_WIDTH_8);
> +					     false, VFIO_PCI_IO_WIDTH_8);
>  	}
>  
>  	return ret;
> @@ -925,7 +925,7 @@ nvgrace_gpu_map_and_write(struct nvgrace_gpu_pci_core_device *nvdev,
>  		ret = vfio_pci_core_do_io_rw(&nvdev->core_device, false,
>  					     nvdev->resmem.ioaddr,
>  					     (char __user *)buf, pos, mem_count,
> -					     0, 0, true, VFIO_PCI_IO_WIDTH_8);
> +					     true, VFIO_PCI_IO_WIDTH_8);
>  	}
>  
>  	return ret;
> diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
> index df8848087681..68008f28bcbe 100644
> --- a/drivers/vfio/pci/vfio_pci_rdwr.c
> +++ b/drivers/vfio/pci/vfio_pci_rdwr.c
> @@ -12,6 +12,7 @@
>  
>  #include <linux/fs.h>
>  #include <linux/pci.h>
> +#include <linux/range.h>
>  #include <linux/uaccess.h>
>  #include <linux/io.h>
>  #include <linux/vfio.h>
> @@ -126,66 +127,41 @@ VFIO_IORDWR(16)
>  VFIO_IORDWR(32)
>  VFIO_IORDWR(64)
>  
> -/*
> - * Read or write from an __iomem region (MMIO or I/O port) with an excluded
> - * range which is inaccessible.  The excluded range drops writes and fills
> - * reads with -1.  This is intended for handling MSI-X vector tables and
> - * leftover space for ROM BARs.
> - */
> +/* Read or write from an __iomem region (MMIO or I/O port) */
>  ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,
>  			       void __iomem *io, char __user *buf,
> -			       loff_t off, size_t count, size_t x_start,
> -			       size_t x_end, bool iswrite,
> +			       loff_t off, size_t count, bool iswrite,
>  			       enum vfio_pci_io_width max_width)

I think there's a better shape to this series, and I think we should
keep vfio_pci_core_do_io_rw() as the variant driver facing API.  I'd
propose the following sequence replacing 1 and 3, interleave the virtio
patch as necessary:

 1. Simple conversion from x_start, x_end to a range.  Pass NULL for an
    empty range rather than {0, 0}.
 2. Split static __vfio_pci_do_io_rw() from vfio_pci_core_do_io_rw() as
    the helper that reads contiguous chunks, called by the original
    function that retains the range information.
 3. Convert from a range to a list_head.  Callers allocate the list on
    the stack and vfio_pci_core_do_io_rw() learns to iterate a list of
    excluded ranges.
 4. Introduce the per BAR excluded range list, populating MSI-X.  ROM
    remains a stack allocated list based on pci_map_rom().  Common
    vfio_pci_core_do_io_rw() call sites now use the list_head on the
    BAR, while ROM, VGA, and variant driver decide whether to use a
    list_head or NULL.  For example the nvgrace use case could pass the
    list_head from the BAR index or NULL.

Thanks,
Alex

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2026-12-02 23:19 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  9:32 [PATCH 0/5] vfio/pci: Per-BAR excluded ranges mhonap
2026-12-02  9:32 ` [PATCH 1/5] vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw() mhonap
2026-12-02 23:19   ` Alex Williamson
2026-12-02  9:32 ` [PATCH 2/5] vfio/virtio: Release vfio-pci-core state when legacy I/O setup fails mhonap
2026-12-02 23:19   ` Alex Williamson
2026-12-02  9:32 ` [PATCH 3/5] vfio/pci: Add per-BAR excluded ranges and put the MSI-X table on them mhonap
2026-12-02  9:32 ` [PATCH 4/5] vfio/pci: Advertise the mmap areas around excluded ranges mhonap
2026-12-02 23:19   ` Alex Williamson
2026-12-02  9:32 ` [PATCH 5/5] vfio/pci: Refuse ioeventfds and dma-buf exports over " mhonap
2026-12-02 23:19   ` Alex Williamson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox