* [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 9:44 ` sashiko-bot
2026-12-02 9:32 ` [PATCH 2/5] vfio/virtio: Release vfio-pci-core state when legacy I/O setup fails mhonap
` (4 subsequent siblings)
5 siblings, 1 reply; 9+ 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] 9+ 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 9:44 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-12-02 9:44 UTC (permalink / raw)
To: mhonap
Cc: kvm, virtualization, dri-devel, linux-cxl, media-ci,
Michael S. Tsirkin, Eugenio Perez
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] vfio/nvgrace-gpu: vfio_pci_core_do_io_rw() byte count treated
as error [1]
--
commit 483fcda0dfc229fdf013b0a4d4193d8e61dc59ac
Author: Manish Honap <mhonap@nvidia.com>
vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw()
This commit moves the excluded range handling out of
vfio_pci_core_do_io_rw() into a new vfio_pci_excluded_range_rw() helper
in vfio_pci_bar_rw() and removes the window arguments from
vfio_pci_core_do_io_rw() and its callers.
> diff --git a/drivers/vfio/pci/nvgrace-gpu/main.c b/drivers/vfio/pci/nvgrace-gpu/main.c
> index 5ddec340b73ba..432f6b9c2842b 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;
[Severity: High]
This problem wasn't introduced by this patch, but do
nvgrace_gpu_map_and_read() and nvgrace_gpu_map_and_write() cause their
callers to treat a successful vfio_pci_core_do_io_rw() transfer as an
error [1]?
vfio_pci_core_do_io_rw() returns the positive byte count transferred on
success (or a negative errno on failure), whereas the USEMEM_REGION_INDEX
branch returns 0 on success. Both nvgrace_gpu_read_mem() and
nvgrace_gpu_write_mem() check "if (ret) return ret;", so any non-zero
transfer on RESMEM_REGION_INDEX returns early without advancing *ppos,
truncates the ssize_t byte count through int ret, and on reads straddling
memregion->memlength skips filling the unmapped tail [mem_count, count)
with 0xff and returns mem_count instead of count:
drivers/vfio/pci/nvgrace-gpu/main.c:nvgrace_gpu_read_mem() {
...
scoped_guard(rwsem_read, &vdev->memory_lock) {
...
ret = nvgrace_gpu_map_and_read(nvdev, buf, mem_count, ppos);
if (ret)
return ret;
}
for (i = mem_count; i < count; i++) {
ret = put_user(val, (unsigned char __user *)(buf + i));
if (ret)
return ret;
}
*ppos += count;
return count;
}
[1] https://sashiko.dev/#/bug/linux-1d672947-b9c7-46b6-896d-b4214a0879d3
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202093250.1683744-1-mhonap@nvidia.com?part=1
^ permalink raw reply [flat|nested] 9+ 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 9:32 ` [PATCH 3/5] vfio/pci: Add per-BAR excluded ranges and put the MSI-X table on them mhonap
` (3 subsequent siblings)
5 siblings, 0 replies; 9+ 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] 9+ 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
` (2 subsequent siblings)
5 siblings, 0 replies; 9+ 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] 9+ 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 9:32 ` [PATCH 5/5] vfio/pci: Refuse ioeventfds and dma-buf exports over " mhonap
2026-12-02 9:47 ` [PATCH 0/5] vfio/pci: Per-BAR " sashiko-bot
5 siblings, 0 replies; 9+ 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] 9+ 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 9:42 ` sashiko-bot
2026-12-02 9:47 ` [PATCH 0/5] vfio/pci: Per-BAR " sashiko-bot
5 siblings, 1 reply; 9+ 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] 9+ 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 9:42 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-12-02 9:42 UTC (permalink / raw)
To: mhonap
Cc: kvm, virtualization, dri-devel, linux-cxl, media-ci,
Michael S. Tsirkin, Eugenio Perez
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] vfio/pci: missing alignment check in vfio_pci_ioeventfd()
[1]
--
commit a0e0e83a63bca771356893cca68d9c824c8b8bc6
Author: Manish Honap <mhonap@nvidia.com>
vfio/pci: Refuse ioeventfds and dma-buf exports over excluded ranges
This commit rejects ioeventfds that overlap write-excluded BAR ranges and
dma-buf exports that overlap mmap-excluded BAR ranges.
> diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
> index 04aed725325c4..e620798f90078 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);
[Severity: Critical]
This problem wasn't introduced by this patch, but should
vfio_pci_ioeventfd() reject an offset where pos is not naturally aligned
to count [1]?
Without an alignment check (such as !IS_ALIGNED(pos, count)), userspace
can register an ioeventfd at an unaligned BAR offset. When the eventfd
fires, vfio_pci_ioeventfd_do_write() passes the unaligned io + pos address
to vfio_pci_core_iowriteN(), which triggers an unhandled alignment fault
on device memory on architectures with strict MMIO alignment such as
arm64.
[1] https://sashiko.dev/#/bug/linux-37b4aa33-2a11-48a3-939f-b70ba1577441
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202093250.1683744-1-mhonap@nvidia.com?part=5
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 0/5] vfio/pci: Per-BAR excluded ranges
2026-12-02 9:32 [PATCH 0/5] vfio/pci: Per-BAR excluded ranges mhonap
` (4 preceding siblings ...)
2026-12-02 9:32 ` [PATCH 5/5] vfio/pci: Refuse ioeventfds and dma-buf exports over " mhonap
@ 2026-12-02 9:47 ` sashiko-bot
5 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-12-02 9:47 UTC (permalink / raw)
To: mhonap; +Cc: virtualization, Michael S. Tsirkin, Eugenio Perez
Sashiko has finished reviewing this 5-patch series:
- Patches 2-4: no issues found.
- Patch 1 ("vfio/pci: Move the excluded windows out of vfio_pci_..."): 1
related pre-existing bug noted
- Patch 5 ("vfio/pci: Refuse ioeventfds and dma-buf exports over..."): 1
related pre-existing bug noted
Detailed reviews have been sent as separate replies to patches 1 and 5.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202093250.1683744-1-mhonap@nvidia.com
^ permalink raw reply [flat|nested] 9+ messages in thread