vfio/pci: Per-BAR excluded ranges
Subsystems (7)
cover vfio/pci: Per-BAR excluded ranges
From: Manish Honap <[email protected]>
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/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/
[6] https://lore.kernel.org/all/[email protected]/
[7] https://lore.kernel.org/all/[email protected]/
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(-)
Signature · 1 line
2.53.0
PATCH 1/5 vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw()
From: Manish Honap <[email protected]>
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.
--- 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(-)
drivers/vfio/pci/nvgrace-gpu/main.c +2 -2
@@ -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;
drivers/vfio/pci/vfio_pci_rdwr.c +73 -42
207 lines not on this page · raw message
include/linux/vfio_pci_core.h +1 -2
@@ -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,
Signature · 1 line
2.53.0
PATCH 2/5 vfio/virtio: Release vfio-pci-core state when legacy I/O setup fails
In reply to [email protected]From: Manish Honap <[email protected]>
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.
--- drivers/vfio/pci/virtio/main.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/vfio/pci/virtio/main.c +4 -1
@@ -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
Signature · 1 line
2.53.0
PATCH 3/5 vfio/pci: Add per-BAR excluded ranges and put the MSI-X table on them
In reply to [email protected]From: Manish Honap <[email protected]>
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.
--- 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(-)
drivers/vfio/pci/vfio_pci_core.c +105 -1
@@ -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);
drivers/vfio/pci/vfio_pci_priv.h +11 -0
@@ -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);
drivers/vfio/pci/vfio_pci_rdwr.c +35 -8
56 lines not on this page · raw message
include/linux/vfio_pci_core.h +2 -0
@@ -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 {
Signature · 1 line
2.53.0
PATCH 4/5 vfio/pci: Advertise the mmap areas around excluded ranges
In reply to [email protected]From: Manish Honap <[email protected]>
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.
--- drivers/vfio/pci/vfio_pci_core.c | 72 ++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+)
drivers/vfio/pci/vfio_pci_core.c +72 -0
@@ -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 *vmaif (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. */
Signature · 1 line
2.53.0
PATCH 5/5 vfio/pci: Refuse ioeventfds and dma-buf exports over excluded ranges
In reply to [email protected]From: Manish Honap <[email protected]>
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.
--- 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(-)
drivers/vfio/pci/vfio_pci_core.c +2 -2
@@ -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;
drivers/vfio/pci/vfio_pci_dmabuf.c +8 -2
@@ -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;
drivers/vfio/pci/vfio_pci_priv.h +3 -0
@@ -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);
drivers/vfio/pci/vfio_pci_rdwr.c +2 -4
@@ -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);
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 5/5] vfio/pci: Refuse ioeventfds and dma-buf exports over excluded ranges
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…
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 <[email protected]>
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.
Quoted 7 lines
drivers/vfio/pci/vfio_pci_rdwr.c +0
-0@@ -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 ;
drivers/vfio/pci/vfio_pci_rdwr.c int vfio_pci_ioeventfd(struct vfio_pci_core_device *vdev, loff_t offset, · line 482
Quoted 10 lines
drivers/vfio/pci/vfio_pci_rdwr.c +2
-4-/* 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
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH 1/5] vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw()
In reply to [email protected]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…
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 <[email protected]>
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.
Quoted 14 lines
drivers/vfio/pci/nvgrace-gpu/main.c +1
-1@@ -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;
drivers/vfio/pci/nvgrace-gpu/main.c nvgrace_gpu_map_and_write(struct nvgrace_gpu_pci_core_device *nvdev, · line 931
Quoted 8 lines
drivers/vfio/pci/nvgrace-gpu/main.c +1
-1@@ -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
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewSashiko has finished reviewing this 5-patch series:
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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
Alex Williamson
maintainerRe: [PATCH 5/5] vfio/pci: Refuse ioeventfds and dma-buf exports over excluded ranges
In reply to [email protected]Quoted 11 lines · On Wed, 2 Dec 2026 15:02:50 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
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.
Quoted 4 lines
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
Quoted 91 lines
Suggested-by Alex Williamson <[email protected]>Signed-off-by Manish Honap <[email protected]>--- 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(-)drivers/vfio/pci/vfio_pci_core.c +2
-2@@ -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;drivers/vfio/pci/vfio_pci_dmabuf.c +8
-2@@ -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;drivers/vfio/pci/vfio_pci_priv.h +3
-0@@ -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);drivers/vfio/pci/vfio_pci_rdwr.c +2
-4@@ -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);
Alex Williamson
maintainerRe: [PATCH 2/5] vfio/virtio: Release vfio-pci-core state when legacy I/O setup fails
In reply to [email protected]Quoted 23 lines · On Wed, 2 Dec 2026 15:02:47 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
virtiovf_pci_init_device()returns the error ofvirtiovf_init_legacy_io()without undoingvfio_pci_core_init_dev().vfio_init_device()does not call .release when .init fails, so the statevfio_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. Callvfio_pci_core_release_dev()whenvirtiovf_init_legacy_io()fails.Signed-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/virtio/main.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/vfio/pci/virtio/main.c +0
-0@@ -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()
drivers/vfio/pci/virtio/main.c static int virtiovf_pci_init_device(struct vfio_device *core_vdev) · line 78
Quoted 10 lines
drivers/vfio/pci/virtio/main.c +4
-1* 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
Alex Williamson
maintainerRe: [PATCH 4/5] vfio/pci: Advertise the mmap areas around excluded ranges
In reply to [email protected]Quoted 13 lines · On Wed, 2 Dec 2026 15:02:49 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
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_MAPPABLEis not reported when such a range covers the pages of the MSI-X table.
Quoted 3 lines
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
Quoted 114 lines
Signed-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 72 ++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+)drivers/vfio/pci/vfio_pci_core.c +72
-0@@ -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 *vmaif (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. */
Alex Williamson
maintainerRe: [PATCH 1/5] vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw()
In reply to [email protected]Quoted 10 lines · On Wed, 2 Dec 2026 15:02:46 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
vfio_pci_core_do_io_rw()fills reads with0xffand 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.
Quoted 56 lines
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 fromvfio_pci_core_do_io_rw()and its callers.No functional change.
Signed-off-by Manish Honap <[email protected]>--- 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(-)drivers/vfio/pci/nvgrace-gpu/main.c +2
-2@@ -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;drivers/vfio/pci/vfio_pci_rdwr.c +1
-3@@ -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
drivers/vfio/pci/vfio_pci_rdwr.c VFIO_IORDWR(16) · line 134
Quoted 10 lines
drivers/vfio/pci/vfio_pci_rdwr.c +2
-5-* 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
RE: [PATCH 1/5] vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw()
Quoted 26 lines
-----Original Message-----
From: Alex Williamson <[email protected]>
Sent: Thursday, December 3, 2026 4:49 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; Yishai Hadas <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linaro-mm- [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 1/5] vfio/pci: Move the excluded windows out ofvfio_pci_core_do_io_rw()External email: Use caution opening links or attachments
Quoted 7 lines · On Wed, 2 Dec 2026 15:02:46 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
vfio_pci_core_do_io_rw()fills reads with0xffand 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.
Quoted 8 lines
Quoted 3 lines
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.
Okay, for v2, I will mention vfio-cxl and HDM decoder registers as the user.
Quoted 80 lines
drivers/vfio/pci/nvgrace-gpu/main.c nvgrace_gpu_map_and_read(struct
Quoted 21 lines
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 fromvfio_pci_core_do_io_rw()and its callers.No functional change.
Signed-off-by Manish Honap <[email protected]>--- 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(-)drivers/vfio/pci/nvgrace-gpu/main.c +0
-0@@ -787,7 +787,7 @@ nvgrace_gpu_map_and_read(struct
nvgrace_gpu_pci_core_device*nvdev,drivers/vfio/pci/nvgrace-gpu/main.c nvgrace_gpu_map_and_write(struct
Quoted 10 lines
drivers/vfio/pci/nvgrace-gpu/main.c +2
-1ret =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,Quoted 28 lines
drivers/vfio/pci/nvgrace-gpu/main.c +1
-1ret =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;drivers/vfio/pci/vfio_pci_rdwr.c +1
-2@@ -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
Quoted 8 lines
- * 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__iomemregion (MMIO or I/O port) */
ssize_tvfio_pci_core_do_io_rw(structvfio_pci_core_device*vdev, bool
test_mem,drivers/vfio/pci/vfio_pci_rdwr.c VFIO_IORDWR(16) · line 134
Quoted 5 lines
void
__iomem*io, char__user*buf,drivers/vfio/pci/vfio_pci_rdwr.c +1
-2- 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:
Quoted 15 lines
1. Simple conversion from
x_start,x_endto a range. Pass NULL for an empty range rather than {0, 0}.
2. Split static__vfio_pci_do_io_rw()fromvfio_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 alist_head. Callers allocate the list on the stack andvfio_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 onpci_map_rom(). Commonvfio_pci_core_do_io_rw()call sites now use thelist_headon the BAR, while ROM, VGA, and variant driver decide whether to use alist_heador NULL. For example the nvgrace use case could pass thelist_headfrom the BAR index or NULL.
Okay, I will restructure the patch sequence as per suggestion in v2.
Thanks,
Manish
Quoted 2 lines
Thanks,
Alex
RE: [PATCH 2/5] vfio/virtio: Release vfio-pci-core state when legacy I/O setup fails
In reply to Alex WilliamsonQuoted 61 lines
-----Original Message-----
From: Alex Williamson <[email protected]>
Sent: Thursday, December 3, 2026 4:49 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; Yishai Hadas <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linaro-mm- [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/5] vfio/virtio: Release vfio-pci-core state when legacy I/O setup failsExternal email: Use caution opening links or attachments
drivers/vfio/pci/virtio/main.c static int virtiovf_pci_init_device(struct vfio_device
Quoted 23 lines · On Wed, 2 Dec 2026 15:02:47 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
virtiovf_pci_init_device()returns the error ofvirtiovf_init_legacy_io()without undoingvfio_pci_core_init_dev().vfio_init_device()does not call .release when .init fails, so the statevfio_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. Callvfio_pci_core_release_dev()whenvirtiovf_init_legacy_io()fails.Signed-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/virtio/main.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)(quoted diff) +0
-0
b/drivers/vfio/pci/virtio/main.cindex123ff5193812..f1ca774889f8100644drivers/vfio/pci/virtio/main.c +0
-0@@ -69,7 +69,10 @@ static int virtiovf_pci_init_device(struct vfio_device
*core_vdev)drivers/vfio/pci/virtio/main.c static int virtiovf_pci_init_device(struct vfio_device *core_vdev) · line 78
Quoted 11 lines
drivers/vfio/pci/virtio/main.c +4
-1* 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 toeb61eca0e8c3.
Quoted 3 lines
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 insidevfio_pci_core_init_dev(). Thanks,
Okay, I will update the series to mention this patch with a Fixes:
I will also update the vfio_pci_core_init_dev to destroy the mutices when MSI-X range add fails.
Thanks,
Manish
Quoted 2 lines
Alex
RE: [PATCH 4/5] vfio/pci: Advertise the mmap areas around excluded ranges
In reply to Alex WilliamsonQuoted 37 lines
-----Original Message-----
From: Alex Williamson <[email protected]>
Sent: Thursday, December 3, 2026 4:49 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; Yishai Hadas <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linaro-mm- [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 4/5] vfio/pci: Advertise the mmap areas around excluded rangesExternal email: Use caution opening links or attachments
Quoted 11 lines · On Wed, 2 Dec 2026 15:02:49 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
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_MAPPABLEisnot
Quoted 5 lines
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.
Quoted 7 lines
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_MMAPin the previous patch without the support here. Thanks,
Okay, I will drop the change msix_mmapable_cap()
In v2, I will only define _READ/_WRITE. _MMAP will move to vfio-cxl v7 series in the patch before HDM decoder register become the first range excluded from mmap.
Thanks,
Manish
Quoted 122 lines
Alex
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_for_each_slot_or_bus(struct
Quoted 13 lines
Signed-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 72 ++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+)drivers/vfio/pci/vfio_pci_core.c +0
-0@@ -988,17 +988,81 @@ static int vfio_pci_for_each_slot_or_bus(struct
pci_dev*pdev,drivers/vfio/pci/vfio_pci_core.c int vfio_pci_ioctl_get_region_info(struct
Quoted 82 lines
drivers/vfio/pci/vfio_pci_core.c +65
-0return 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,drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_mmap(struct vfio_device
Quoted 11 lines
drivers/vfio/pci/vfio_pci_core.c +4
-0if (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, structvm_area_struct*vmaQuoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +4
-0if (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. */
RE: [PATCH 5/5] vfio/pci: Refuse ioeventfds and dma-buf exports over excluded ranges
In reply to Alex WilliamsonQuoted 36 lines
-----Original Message-----
From: Alex Williamson <[email protected]>
Sent: Thursday, December 3, 2026 4:49 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; Yishai Hadas <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; dri- [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH 5/5] vfio/pci: Refuse ioeventfds and dma-buf exports over excluded rangesExternal email: Use caution opening links or attachments
Quoted 11 lines · On Wed, 2 Dec 2026 15:02:50 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
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.Quoted 4 lines
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.
Quoted 8 lines
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,
Agreed; In v2, I will keep the ioeventfd check as last patch of this series. dma-buf check will be moved to vfio-cxl series which introduces the EXCLUDE_MMAP flag/mmap check.
Thanks,
Manish
Quoted 105 lines
Alex
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_for_each_slot_or_bus(struct
Quoted 16 lines
Suggested-by Alex Williamson <[email protected]>Signed-off-by Manish Honap <[email protected]>--- 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(-)drivers/vfio/pci/vfio_pci_core.c +0
-0@@ -988,8 +988,8 @@ static int vfio_pci_for_each_slot_or_bus(struct
pci_dev*pdev,drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
Quoted 6 lines
drivers/vfio/pci/vfio_pci_core.c +1
-2return 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, intbar,
Quoted 20 lines
drivers/vfio/pci/vfio_pci_core.c +1
-0+ 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.cb/drivers/vfio/pci/vfio_pci_dmabuf.c
index7d1d34029b95..6f28cc285e78100644drivers/vfio/pci/vfio_pci_dmabuf.c +3
-1@@ -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,11Quoted 1 line
@@ static int validate_dmabuf_input(struct
vfio_device_feature_dma_buf
*dma_buf,drivers/vfio/pci/vfio_pci_dmabuf.c int vfio_pci_core_feature_dma_buf(struct
Quoted 12 lines
if (!len || !PAGE_ALIGNED(offset) || !PAGE_ALIGNED(len)) return -EINVAL;drivers/vfio/pci/vfio_pci_dmabuf.c +5
-0+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,Quoted 15 lines
drivers/vfio/pci/vfio_pci_dmabuf.c +2
-1if (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;drivers/vfio/pci/vfio_pci_priv.h +0
-0drivers/vfio/pci/vfio_pci_priv.h struct vfio_pci_excluded_range { · line 58
Quoted 4 lines
drivers/vfio/pci/vfio_pci_priv.h +1
-0@@ -55,6 +55,9 @@ struct vfio_pci_excluded_range {u32 flags; }; +bool vfio_pci_bar_range_excluded(struct vfio_pci_core_device *vdev, intbar,
drivers/vfio/pci/vfio_pci_rdwr.c int vfio_pci_ioeventfd(struct vfio_pci_core_device
Quoted 11 lines
drivers/vfio/pci/vfio_pci_priv.h +2
-1+ 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
index04aed725325c..e620798f9007100644drivers/vfio/pci/vfio_pci_rdwr.c +0
-0@@ -475,10 +475,8 @@ int vfio_pci_ioeventfd(struct vfio_pci_core_device*vdev,
loff_toffset,Quoted 12 lines
drivers/vfio/pci/vfio_pci_rdwr.c +2
-4if (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);