From: Manish Honap 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 --- 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 #include /* 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