Since "vfio/pci: Set up barmap in vfio_pci_core_enable()", the resource request and iomap for the BARs was performed early, and vfio_pci_core_setup_barmap() now just checks those actions succeeded.
There were two types of callers: - Those that need the iomap, because they'll access the BAR - Those that need the resource, because they'll map/export it This replaces vfio_pci_core_setup_barmap() with two helpers, vfio_pci_core_check_barmap_valid() and vfio_pci_core_check_bar_rsrc(), to make it clear which behaviour is required in each caller. Signed-off-by: Matt Evans <[email protected]> --- drivers/vfio/pci/nvgrace-gpu/main.c | 8 +++----- drivers/vfio/pci/vfio_pci_core.c | 5 ++--- drivers/vfio/pci/vfio_pci_rdwr.c | 22 ++-------------------- drivers/vfio/pci/virtio/legacy_io.c | 4 ++-- include/linux/vfio_pci_core.h | 23 ++++++++++++++++++++++- 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/drivers/vfio/pci/nvgrace-gpu/main.c b/drivers/vfio/pci/nvgrace-gpu/main.c index fa056b69f899..d5f09144ac84 100644 --- a/drivers/vfio/pci/nvgrace-gpu/main.c +++ b/drivers/vfio/pci/nvgrace-gpu/main.c @@ -184,12 +184,10 @@ static int nvgrace_gpu_open_device(struct vfio_device *core_vdev) /* * GPU readiness is checked by reading the BAR0 registers. - * - * ioremap BAR0 to ensure that the BAR0 mapping is present before - * register reads on first fault before establishing any GPU - * memory mapping. + * Ensure that the BAR0 mapping is present before that + * happens. */ - ret = vfio_pci_core_setup_barmap(vdev, 0); + ret = vfio_pci_core_check_barmap_valid(vdev, 0); if (ret) goto error_exit; diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index c59c61861d81..2771d0f21899 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -1804,10 +1804,9 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma return -EINVAL; /* - * Even though we don't make use of the barmap for the mmap, - * we need to request the region and the barmap tracks that. + * Ensure the BAR resource region is reserved for use. */ - ret = vfio_pci_core_setup_barmap(vdev, index); + ret = vfio_pci_core_check_bar_rsrc(vdev, index); if (ret) return ret; diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c index bf7152316db4..40c97d73ff95 100644 --- a/drivers/vfio/pci/vfio_pci_rdwr.c +++ b/drivers/vfio/pci/vfio_pci_rdwr.c @@ -198,24 +198,6 @@ 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); -int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar) -{ - /* - * The barmap is now always set up in vfio_pci_core_enable(). - * Some legacy callers use this function to ensure the BAR - * resources are requested, and others to ensure the - * pci_iomap() was done, so check here: - */ - if (bar < 0 || bar >= PCI_STD_NUM_BARS) - return -EINVAL; - if (vdev->barmap[bar] == 0) - return -ENOMEM; - if (!vdev->bar_has_rsrc[bar]) - return -EBUSY; - return 0; -} -EXPORT_SYMBOL_GPL(vfio_pci_core_setup_barmap); - ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf, size_t count, loff_t *ppos, bool iswrite) { @@ -267,7 +249,7 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf, */ max_width = VFIO_PCI_IO_WIDTH_4; } else { - int ret = vfio_pci_core_setup_barmap(vdev, bar); + int ret = vfio_pci_core_check_barmap_valid(vdev, bar); if (ret) { done = ret; goto out; @@ -445,7 +427,7 @@ int vfio_pci_ioeventfd(struct vfio_pci_core_device *vdev, loff_t offset, if (count == 8) return -EINVAL; - ret = vfio_pci_core_setup_barmap(vdev, bar); + ret = vfio_pci_core_check_barmap_valid(vdev, bar); if (ret) return ret; diff --git a/drivers/vfio/pci/virtio/legacy_io.c b/drivers/vfio/pci/virtio/legacy_io.c index 1ed349a55629..9c59d1600ac4 100644 --- a/drivers/vfio/pci/virtio/legacy_io.c +++ b/drivers/vfio/pci/virtio/legacy_io.c @@ -305,8 +305,8 @@ static int virtiovf_set_notify_addr(struct virtiovf_pci_core_device *virtvdev) * Setup the BAR where the 'notify' exists to be used by vfio as well * This will let us mmap it only once and use it when needed. */ - ret = vfio_pci_core_setup_barmap(core_device, - virtvdev->notify_bar); + ret = vfio_pci_core_check_barmap_valid(core_device, + virtvdev->notify_bar); if (ret) return ret; diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index 1f508b067d82..6a5384d57f1d 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -189,7 +189,6 @@ int vfio_pci_core_match_token_uuid(struct vfio_device *core_vdev, int vfio_pci_core_enable(struct vfio_pci_core_device *vdev); void vfio_pci_core_disable(struct vfio_pci_core_device *vdev); void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev); -int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar); pci_ers_result_t vfio_pci_core_aer_err_detected(struct pci_dev *pdev, pci_channel_state_t state); ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem, @@ -225,6 +224,28 @@ VFIO_IOREAD_DECLARATION(32) VFIO_IOREAD_DECLARATION(64) #endif +/* Returns 0 if vdev->barmap[bar] can be accessed, otherwise errno */ +static inline int +vfio_pci_core_check_barmap_valid(struct vfio_pci_core_device *vdev, int bar) +{ + if (bar < 0 || bar >= PCI_STD_NUM_BARS) + return -EINVAL; + if (vdev->barmap[bar] == 0) + return -ENOMEM; + return 0; +} + +/* Returns 0 if BAR has a valid resource reserved for use, otherwise errno */ +static inline int vfio_pci_core_check_bar_rsrc(struct vfio_pci_core_device *vdev, + int bar) +{ + if (bar < 0 || bar >= PCI_STD_NUM_BARS) + return -EINVAL; + if (!vdev->have_bar_resource[bar]) + return -EBUSY; + return 0; +} + static inline bool is_aligned_for_order(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn, -- 2.47.3

