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_MAPPABLE is not reported > when such a range covers the pages of the MSI-X table. > > A BAR without mmap-excluded range does not get new capability. The MSI-X > table is excluded from read and write only, so the MSI-X BAR is > unchanged.
Why are we rehashing MSI-X here? This should really go in with a series that actually defines an mmap exclusion, otherwise it's dead code. We also shouldn't preemptive define VFIO_PCI_EXCLUDE_MMAP in the previous patch without the support here. Thanks, Alex > > Signed-off-by: Manish Honap <[email protected]> > --- > 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. > */
