vfio/pci: Request resources and map BARs at enable time
Subsystems (6)
v2 cover vfio/pci: Request resources and map BARs at enable time
Hi,
This is a replacement for these patches posted to set up the VFIO barmap in the DMABUF export path, and add serialisation:
https://lore.kernel.org/kvm/[email protected]
Those patches were motivated by the DMABUF export originally missing a call to vfio_pci_core_setup_barmap() and so possibly exporting a range for which resources weren't yet requested.
Responses in the thread indicated there wasn't a strong historical reason to require the bar-mapping to be performed on-demand at BAR reference time. It's much simpler to move this to vfio_pci_core_enable(), and that then avoids having to deal with concurrent lazy requests.
The first patch requests PCI resources and pci_iomap() of the BARs in vfio_pci_core_enable(), moving this out of vfio_pci_core_setup_barmap().
Some callers relied on vfio_pci_core_setup_barmap() for its ioremap() effect, and other callers use it for its resource-acquiring effect. The function remains in the first patch, containing a cheap check that both these actions have occurred and keeping the same error behaviour.
The second patch then removes calls to vfio_pci_core_setup_barmap(), in favour of two small helper functions which make it clearer to distinguish code that needs just the resource (e.g. mmap) and code that needs the iomap as well (e.g. the rdwr routines, or virtio).
The third patch adds the resource check to VFIO DMABUF export, which was previously able to export an unrequested resource. Although patch 1 at first appears to fix this by requesting resources at enable time, code using the BAR still needs to check the resource really was acquired.
In future we could reconsider performing all the ioremap()s at startup time, since a lot of VFIO users will never access BARs in-kernel and won't need them. Mapping huge BARs will cost some time & memory, and if necessary this could be reduced by doing the ioremap() portion on demand.
=== Changes ===
v2:
- Don't fail if resources can't be requested or iomapped, even for valid BARs, as this would change the userspace-observable error behaviour. Specifically, if there was an issue with one particular BAR which happened to never be used, then userspace would never encounter an error for it. Track iomap and resource-acquisition status per BAR.
- Break out the checks for resource success from those for iomap success, in the form of the two new helpers.
- Third patch to add the check to VFIO DMABUF export, because init-time requests can now fail.
v1:
https://lore.kernel.org/kvm/[email protected]/
Matt Evans (3):
vfio/pci: Set up bar resources and maps in vfio_pci_core_enable() vfio/pci: Replace vfio_pci_core_setup_barmap() with checks for
resource/map
vfio/pci: Check BAR resources before exporting a DMABUF
drivers/vfio/pci/nvgrace-gpu/main.c | 8 ++-- drivers/vfio/pci/vfio_pci_core.c | 66 +++++++++++++++++++++++------ drivers/vfio/pci/vfio_pci_dmabuf.c | 6 ++- drivers/vfio/pci/vfio_pci_rdwr.c | 29 +------------ drivers/vfio/pci/virtio/legacy_io.c | 4 +- include/linux/vfio_pci_core.h | 24 ++++++++++- 6 files changed, 88 insertions(+), 49 deletions(-)
Signature · 1 line
2.47.3
PATCH v2 1/3 vfio/pci: Set up bar resources and maps in vfio_pci_core_enable()
Previously BAR resource requests and the corresponding pci_iomap() were performed on-demand and without synchronisation, which was racy. Rather than add synchronisation, it's simplest to address this by doing both activities from vfio_pci_core_enable().
The resource allocation and/or pci_iomap() can still fail; their status is tracked and existing calls to vfio_pci_core_setup_barmap() will fail in the same way as before. This keeps the point of failure as observed by userspace the same, i.e. failures to request/map unused BARs are benign.
--- drivers/vfio/pci/vfio_pci_core.c | 61 +++++++++++++++++++++++++++----- drivers/vfio/pci/vfio_pci_rdwr.c | 29 ++++++--------- include/linux/vfio_pci_core.h | 1 + 3 files changed, 64 insertions(+), 27 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +52 -9
@@ -482,6 +482,55 @@ static int vfio_pci_core_runtime_resume(struct device *dev) }#endif /* CONFIG_PM */ +static void __vfio_pci_core_unmap_bars (struct vfio_pci_core_device *vdev) +{ +struct pci_dev *pdev = vdev->pdev; +int i; + +for (i =0 ; i <PCI_STD_NUM_BARS ; i++) { +int bar = i +PCI_STD_RESOURCES ; + +if (vdev->barmap[bar]) +pci_iounmap (pdev, vdev->barmap[bar]); +if (vdev->have_bar_resource[bar]) +pci_release_selected_regions (pdev,1 << bar); + vdev->barmap[bar] =NULL ; + vdev->have_bar_resource[bar] = false; + } +} + +static void __vfio_pci_core_map_bars (struct vfio_pci_core_device *vdev) +{ +struct pci_dev *pdev = vdev->pdev; +int i; + +/* +* Eager-request BAR resources, and iomap; soft failures are +* allowed, and consumers must check before use. +*/ +for (i =0 ; i <PCI_STD_NUM_BARS ; i++) { +int ret; +int bar = i +PCI_STD_RESOURCES ; +void __iomem *io; + +if (pci_resource_len (pdev, i) ==0 ) +continue ; + + ret =pci_request_selected_regions (pdev,1 << bar,"vfio" ); +if (ret) { +pci_warn (vdev->pdev,"Failed to reserve region %d\n" , bar); +continue ; + } + vdev->have_bar_resource[bar] = true; + + io =pci_iomap (pdev, bar,0 ); +if (io) + vdev->barmap[bar] = io; +else +pci_warn (vdev->pdev,"Failed to iomap region %d\n" , bar); + } +} +/* * The pci-driver core runtime PM routines always save the device state * before going into suspended state. If the device is going into low power
@@ -568,6 +617,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)if (!vfio_vga_disabled () &&vfio_pci_is_vga (pdev)) vdev->has_vga = true; +__vfio_pci_core_map_bars (vdev);return 0 ;
@@ -591,7 +641,7 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)struct pci_dev *pdev = vdev->pdev;struct vfio_pci_dummy_resource *dummy_res, *tmp;struct vfio_pci_ioeventfd *ioeventfd, *ioeventfd_tmp; -int i, bar; +int i;/* For needs_reset */ lockdep_assert_held (&vdev->vdev.dev_set->lock);
@@ -646,14 +696,7 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)vfio_config_free (vdev); -for (i =0 ; i <PCI_STD_NUM_BARS ; i++) { - bar = i +PCI_STD_RESOURCES ; -if (!vdev->barmap[bar]) -continue ; -pci_iounmap (pdev, vdev->barmap[bar]); -pci_release_selected_regions (pdev,1 << bar); - vdev->barmap[bar] =NULL ; - } +__vfio_pci_core_unmap_bars (vdev);list_for_each_entry_safe (dummy_res, tmp, &vdev->dummy_resources_list, res_next) {
drivers/vfio/pci/vfio_pci_rdwr.c +11 -18
@@ -200,25 +200,18 @@ EXPORT_SYMBOL_GPL(vfio_pci_core_do_io_rw);int vfio_pci_core_setup_barmap (struct vfio_pci_core_device *vdev,int bar) { -struct pci_dev *pdev = vdev->pdev; -int ret; -void __iomem *io; - -if (vdev->barmap[bar]) -return 0 ; - - ret =pci_request_selected_regions (pdev,1 << bar,"vfio" ); -if (ret) -return ret; - - io =pci_iomap (pdev, bar,0 ); -if (!io) { -pci_release_selected_regions (pdev,1 << 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 ; - } - - vdev->barmap[bar] = io; - +if (!vdev->bar_has_rsrc[bar]) +return -EBUSY ;return 0 ; }EXPORT_SYMBOL_GPL (vfio_pci_core_setup_barmap);
include/linux/vfio_pci_core.h +1 -0
@@ -101,6 +101,7 @@ struct vfio_pci_core_device {const struct vfio_pci_device_ops *pci_ops;void __iomem *barmap[PCI_STD_NUM_BARS ];bool bar_mmap_supported[PCI_STD_NUM_BARS ]; +bool have_bar_resource[PCI_STD_NUM_BARS ];u8 *pci_config_map;u8 *vconfig;struct perm_bits *msi_perm;
Signature · 1 line
2.47.3
PATCH v2 2/3 vfio/pci: Replace vfio_pci_core_setup_barmap() with checks for resource/map
In reply to Matt EvansSince "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.
--- 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(-)
drivers/vfio/pci/nvgrace-gpu/main.c +3 -5
@@ -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;
drivers/vfio/pci/vfio_pci_core.c +2 -3
@@ -1804,10 +1804,9 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vmareturn -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;
drivers/vfio/pci/vfio_pci_rdwr.c +2 -20
@@ -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;
drivers/vfio/pci/virtio/legacy_io.c +2 -2
@@ -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;
include/linux/vfio_pci_core.h +22 -1
@@ -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,
Signature · 1 line
2.47.3
PATCH v2 3/3 vfio/pci: Check BAR resources before exporting a DMABUF
In reply to Matt EvansA DMABUF exports access to BAR resources and, although they are requested at startup time, we need to ensure they really were reserved before exporting. Otherwise, it's possible to access unreserved resources through the export.
Add a check to the DMABUF-creation path.
--- drivers/vfio/pci/vfio_pci_dmabuf.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/vfio/pci/vfio_pci_dmabuf.c +4 -2
@@ -244,9 +244,11 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,return -EINVAL ;/* -* For PCI the region_index is the BAR number like everything else. +* For PCI the region_index is the BAR number like everything +* else. Check that PCI resources have been claimed for it. */ -if (get_dma_buf.region_index >=VFIO_PCI_ROM_REGION_INDEX ) +if (get_dma_buf.region_index >=VFIO_PCI_ROM_REGION_INDEX || +vfio_pci_core_check_bar_rsrc (vdev, get_dma_buf.region_index) !=0 )return -ENODEV ; dma_ranges = memdup_array_user(&arg->dma_ranges, get_dma_buf.nr_ranges,
Signature · 1 line
2.47.3
Alex Williamson
maintainerRe: [PATCH v2 2/3] vfio/pci: Replace vfio_pci_core_setup_barmap() with checks for resource/map
In reply to Matt EvansQuoted 11 lines · On Thu, 23 Apr 2026 11:25:08 -0700 Matt Evans <[email protected]> wrote:
Since "vfio/pci: Set up barmap in
vfio_pci_core_enable()", the resource request and iomap for the BARs was performed early, andvfio_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 itThis replaces
vfio_pci_core_setup_barmap()with two helpers,vfio_pci_core_check_barmap_valid()andvfio_pci_core_check_bar_rsrc(), to make it clear which behaviour is required in each caller.
TBH, I don't see why we need the distinction. Thanks,
Alex
Alex Williamson
maintainerRe: [PATCH v2 1/3] vfio/pci: Set up bar resources and maps in vfio_pci_core_enable()
In reply to Matt EvansQuoted 44 lines · On Thu, 23 Apr 2026 11:25:07 -0700 Matt Evans <[email protected]> wrote:
Previously BAR resource requests and the corresponding
pci_iomap()were performed on-demand and without synchronisation, which was racy. Rather than add synchronisation, it's simplest to address this by doing both activities fromvfio_pci_core_enable().The resource allocation and/or
pci_iomap()can still fail; their status is tracked and existing calls tovfio_pci_core_setup_barmap()will fail in the same way as before. This keeps the point of failure as observed by userspace the same, i.e. failures to request/map unused BARs are benign.Fixes 7f5764e179c6 ("vfio: use vfio_pci_core_setup_barmap to map bar in mmap")Fixes 0d77ed3589ac0 ("vfio/pci: Pull BAR mapping setup from read-write path")Signed-off-by Matt Evans <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 61 +++++++++++++++++++++++++++----- drivers/vfio/pci/vfio_pci_rdwr.c | 29 ++++++--------- include/linux/vfio_pci_core.h | 1 + 3 files changed, 64 insertions(+), 27 deletions(-)drivers/vfio/pci/vfio_pci_core.c +16
-0@@ -482,6 +482,55 @@ static int vfio_pci_core_runtime_resume(struct device *dev) }#endif /* CONFIG_PM */ +static void __vfio_pci_core_unmap_bars (struct vfio_pci_core_device *vdev) +{ +struct pci_dev *pdev = vdev->pdev; +int i; + +for (i =0 ; i <PCI_STD_NUM_BARS ; i++) { +int bar = i +PCI_STD_RESOURCES ; + +if (vdev->barmap[bar]) +pci_iounmap (pdev, vdev->barmap[bar]); +if (vdev->have_bar_resource[bar]) +pci_release_selected_regions (pdev,1 << bar); + vdev->barmap[bar] =NULL ; + vdev->have_bar_resource[bar] = false; + } +}
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_core_runtime_resume(struct device *dev) · line 510
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+ +static void __vfio_pci_core_map_bars (struct vfio_pci_core_device *vdev) +{ +struct pci_dev *pdev = vdev->pdev; +int i; + +/* +* Eager-request BAR resources, and iomap; soft failures are +* allowed, and consumers must check before use. +*/
I'd use this to describe that soft failures maintain compatible error signatures to previously used on-demand mapping.
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_core_runtime_resume(struct device *dev) · line 514
Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +4
-0+for (i =0 ; i <PCI_STD_NUM_BARS ; i++) { +int ret; +int bar = i +PCI_STD_RESOURCES ; +void __iomem *io;
Reverse Christmas tree ordering.
Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c +8
-0+ +if (pci_resource_len (pdev, i) ==0 ) +continue ; + + ret =pci_request_selected_regions (pdev,1 << bar,"vfio" ); +if (ret) { +pci_warn (vdev->pdev,"Failed to reserve region %d\n" , bar); +continue ;
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_core_runtime_resume(struct device *dev) · line 532
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+ } + vdev->have_bar_resource[bar] = true; + + io =pci_iomap (pdev, bar,0 ); +if (io) + vdev->barmap[bar] = io; +else +pci_warn (vdev->pdev,"Failed to iomap region %d\n" , bar); + } +}
I see you making the point in the cover letter about the resource request vs the iomap resource, but we currently handle these together. If either fails, setup barmap fails and the path returns error. I don't see any justification for now allowing the request resource to succeed but the iomap fails.
These functions also don't need the double-underscore prefix.
Quoted 24 lines
drivers/vfio/pci/vfio_pci_core.c +3
-1+/* * The pci-driver core runtime PM routines always save the device state * before going into suspended state. If the device is going into low power @@ -568,6 +617,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)if (!vfio_vga_disabled () &&vfio_pci_is_vga (pdev)) vdev->has_vga = true; +__vfio_pci_core_map_bars (vdev);return 0 ;@@ -591,7 +641,7 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)struct pci_dev *pdev = vdev->pdev;struct vfio_pci_dummy_resource *dummy_res, *tmp;struct vfio_pci_ioeventfd *ioeventfd, *ioeventfd_tmp; -int i, bar; +int i;/* For needs_reset */ lockdep_assert_held (&vdev->vdev.dev_set->lock);@@ -646,14 +696,7 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)vfio_config_free (vdev);
drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) · line 699
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +1
-8-for (i =0 ; i <PCI_STD_NUM_BARS ; i++) { - bar = i +PCI_STD_RESOURCES ; -if (!vdev->barmap[bar]) -continue ; -pci_iounmap (pdev, vdev->barmap[bar]); -pci_release_selected_regions (pdev,1 << bar); - vdev->barmap[bar] =NULL ; - } +__vfio_pci_core_unmap_bars (vdev);
I expect this doesn't need to change if we drop the separation between resources and iomap.
Quoted 16 lines
drivers/vfio/pci/vfio_pci_core.c +0
-0list_for_each_entry_safe (dummy_res, tmp, &vdev->dummy_resources_list, res_next) {drivers/vfio/pci/vfio_pci_rdwr.c +0
-6@@ -200,25 +200,18 @@ EXPORT_SYMBOL_GPL(vfio_pci_core_do_io_rw);int vfio_pci_core_setup_barmap (struct vfio_pci_core_device *vdev,int bar) { -struct pci_dev *pdev = vdev->pdev; -int ret; -void __iomem *io; - -if (vdev->barmap[bar]) -return 0 ;
drivers/vfio/pci/vfio_pci_rdwr.c EXPORT_SYMBOL_GPL(vfio_pci_core_do_io_rw); · line 204
Quoted 10 lines
drivers/vfio/pci/vfio_pci_rdwr.c +2
-8- - ret =pci_request_selected_regions (pdev,1 << bar,"vfio" ); -if (ret) -return ret; - - io =pci_iomap (pdev, bar,0 ); -if (!io) { -pci_release_selected_regions (pdev,1 << bar); +/* +* The barmap is now always set up in vfio_pci_core_enable().
"now" is going to read strangely very quickly.
drivers/vfio/pci/vfio_pci_rdwr.c EXPORT_SYMBOL_GPL(vfio_pci_core_do_io_rw); · line 213
Quoted 13 lines
drivers/vfio/pci/vfio_pci_rdwr.c +8
-4+* 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 ; - } - - vdev->barmap[bar] = io; - +if (!vdev->bar_has_rsrc[bar])
Typo, this won't incrementally compile. Thanks,
Alex
Quoted 16 lines
drivers/vfio/pci/vfio_pci_rdwr.c +1
-0+return -EBUSY ;return 0 ; }EXPORT_SYMBOL_GPL (vfio_pci_core_setup_barmap);include/linux/vfio_pci_core.h +1
-0@@ -101,6 +101,7 @@ struct vfio_pci_core_device {const struct vfio_pci_device_ops *pci_ops;void __iomem *barmap[PCI_STD_NUM_BARS ];bool bar_mmap_supported[PCI_STD_NUM_BARS ]; +bool have_bar_resource[PCI_STD_NUM_BARS ];u8 *pci_config_map;u8 *vconfig;struct perm_bits *msi_perm;
Re: [PATCH v2 1/3] vfio/pci: Set up bar resources and maps in vfio_pci_core_enable()
Hi Alex,
Quoted 53 lines · Alex Williamson
Quoted 49 lines · On Thu, 23 Apr 2026 11:25:07 -0700 Matt Evans <[email protected]> wrote:
Previously BAR resource requests and the corresponding
pci_iomap()were performed on-demand and without synchronisation, which was racy. Rather than add synchronisation, it's simplest to address this by doing both activities fromvfio_pci_core_enable().The resource allocation and/or
pci_iomap()can still fail; their status is tracked and existing calls tovfio_pci_core_setup_barmap()will fail in the same way as before. This keeps the point of failure as observed by userspace the same, i.e. failures to request/map unused BARs are benign.Fixes 7f5764e179c6 ("vfio: use vfio_pci_core_setup_barmap to map bar in mmap")Fixes 0d77ed3589ac0 ("vfio/pci: Pull BAR mapping setup from read-write path")Signed-off-by Matt Evans <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 61 +++++++++++++++++++++++++++----- drivers/vfio/pci/vfio_pci_rdwr.c | 29 ++++++--------- include/linux/vfio_pci_core.h | 1 + 3 files changed, 64 insertions(+), 27 deletions(-)drivers/vfio/pci/vfio_pci_core.c +21
-0@@ -482,6 +482,55 @@ static int vfio_pci_core_runtime_resume(struct device *dev) }#endif /* CONFIG_PM */ +static void __vfio_pci_core_unmap_bars (struct vfio_pci_core_device *vdev) +{ +struct pci_dev *pdev = vdev->pdev; +int i; + +for (i =0 ; i <PCI_STD_NUM_BARS ; i++) { +int bar = i +PCI_STD_RESOURCES ; + +if (vdev->barmap[bar]) +pci_iounmap (pdev, vdev->barmap[bar]); +if (vdev->have_bar_resource[bar]) +pci_release_selected_regions (pdev,1 << bar); + vdev->barmap[bar] =NULL ; + vdev->have_bar_resource[bar] = false; + } +} + +static void __vfio_pci_core_map_bars (struct vfio_pci_core_device *vdev) +{ +struct pci_dev *pdev = vdev->pdev; +int i;
Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_core_runtime_resume(struct device *dev) · line 510
Quoted 5 lines
drivers/vfio/pci/vfio_pci_core.c +5
-0+ +/* +* Eager-request BAR resources, and iomap; soft failures are +* allowed, and consumers must check before use. +*/ I'd use this to describe that soft failures maintain compatible error signatures to previously used on-demand mapping.
Done.
Quoted 6 lines
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_core_runtime_resume(struct device *dev) · line 514
Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +4
-0+for (i =0 ; i <PCI_STD_NUM_BARS ; i++) { +int ret; +int bar = i +PCI_STD_RESOURCES ; +void __iomem *io;Reverse Christmas tree ordering.
Done.
Quoted 16 lines
Quoted 16 lines
drivers/vfio/pci/vfio_pci_core.c +16
-0+ +if (pci_resource_len (pdev, i) ==0 ) +continue ; + + ret =pci_request_selected_regions (pdev,1 << bar,"vfio" ); +if (ret) { +pci_warn (vdev->pdev,"Failed to reserve region %d\n" , bar); +continue ; + } + vdev->have_bar_resource[bar] = true; + + io =pci_iomap (pdev, bar,0 ); +if (io) + vdev->barmap[bar] = io; +else +pci_warn (vdev->pdev,"Failed to iomap region %d\n" , bar);
Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_core_runtime_resume(struct device *dev) · line 532
Quoted 2 lines
drivers/vfio/pci/vfio_pci_core.c +2
-0+ } +}I see you making the point in the cover letter about the resource request vs the iomap resource, but we currently handle these together. If either fails, setup barmap fails and the path returns error. I don't see any justification for now allowing the request resource to succeed but the iomap fails.
The primary effect was to let consumers see -EBUSY for a resource reservation failure or -ENOMEM for an iomap failure (whether through this patch's vfio_pci_core_setup_barmap() or the next patch's helpers), and that keeps the error signatures identical.
A weak secondary effect was that a BAR that gets resource but fails for whatever reason to iomap it can still be used by most clients (assuming the general usage is to mmap). The system's pretty sick if this is the case, so as I say it's weak.
OK, if you prefer the combined approach and don't feel the subsequent single-semantic check helpers (need mapping, need resource) are clearer to read then I'll recombine them, though:
- If vfio_pci_core_map_bars() just sets barmap[n] iff both resource
acquisition and iomap succeed, then a later check can only return one error from either cause. I'll go with -ENOMEM unless you prefer -EBUSY.
Using something else can again make userspace see previously-unseen
error values.
- IMHO vfio_pci_core_setup_barmap() should still be renamed (in a 2nd
patch) since it doesn't do any setting up anymore. Cosmetic, but cleaner to parse when the callers use vfio_pci_core_check_barmap_valid() no?
Quoted 1 line
These functions also don't need the double-underscore prefix.
Done.
Quoted 25 lines
Quoted 25 lines
drivers/vfio/pci/vfio_pci_core.c +3
-1+/* * The pci-driver core runtime PM routines always save the device state * before going into suspended state. If the device is going into low power @@ -568,6 +617,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)if (!vfio_vga_disabled () &&vfio_pci_is_vga (pdev)) vdev->has_vga = true; +__vfio_pci_core_map_bars (vdev);return 0 ;@@ -591,7 +641,7 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)struct pci_dev *pdev = vdev->pdev;struct vfio_pci_dummy_resource *dummy_res, *tmp;struct vfio_pci_ioeventfd *ioeventfd, *ioeventfd_tmp; -int i, bar; +int i;/* For needs_reset */ lockdep_assert_held (&vdev->vdev.dev_set->lock);@@ -646,14 +696,7 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)vfio_config_free (vdev);
Quoted 12 lines
drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) · line 699
Quoted 9 lines
drivers/vfio/pci/vfio_pci_core.c +1
-8-for (i =0 ; i <PCI_STD_NUM_BARS ; i++) { - bar = i +PCI_STD_RESOURCES ; -if (!vdev->barmap[bar]) -continue ; -pci_iounmap (pdev, vdev->barmap[bar]); -pci_release_selected_regions (pdev,1 << bar); - vdev->barmap[bar] =NULL ; - } +__vfio_pci_core_unmap_bars (vdev);I expect this doesn't need to change if we drop the separation between resources and iomap.
OK, restored.
Quoted 20 lines
Quoted 20 lines
drivers/vfio/pci/vfio_pci_core.c +0
-0list_for_each_entry_safe (dummy_res, tmp, &vdev->dummy_resources_list, res_next) {drivers/vfio/pci/vfio_pci_rdwr.c +0
-10@@ -200,25 +200,18 @@ EXPORT_SYMBOL_GPL(vfio_pci_core_do_io_rw);int vfio_pci_core_setup_barmap (struct vfio_pci_core_device *vdev,int bar) { -struct pci_dev *pdev = vdev->pdev; -int ret; -void __iomem *io; - -if (vdev->barmap[bar]) -return 0 ; - - ret =pci_request_selected_regions (pdev,1 << bar,"vfio" ); -if (ret) -return ret;
Quoted 8 lines
drivers/vfio/pci/vfio_pci_rdwr.c EXPORT_SYMBOL_GPL(vfio_pci_core_do_io_rw); · line 204
Quoted 6 lines
drivers/vfio/pci/vfio_pci_rdwr.c +2
-4- - io =pci_iomap (pdev, bar,0 ); -if (!io) { -pci_release_selected_regions (pdev,1 << bar); +/* +* The barmap is now always set up in vfio_pci_core_enable(). "now" is going to read strangely very quickly.
Hm, yeah, fixed.
Quoted 7 lines
Quoted 7 lines
drivers/vfio/pci/vfio_pci_rdwr.c +7
-0+* 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 )
Quoted 8 lines
drivers/vfio/pci/vfio_pci_rdwr.c EXPORT_SYMBOL_GPL(vfio_pci_core_do_io_rw); · line 213
Quoted 6 lines
drivers/vfio/pci/vfio_pci_rdwr.c +1
-4return -ENOMEM ; - } - - vdev->barmap[bar] = io; - +if (!vdev->bar_has_rsrc[bar])Typo, this won't incrementally compile. Thanks,
Fixed.
Alex, thanks for all your comments so far, I realise this is a pretty noddy fix but it's good to get it clean.
Matt
Quoted 20 lines
Alex
Quoted 16 lines
drivers/vfio/pci/vfio_pci_rdwr.c +1
-0+return -EBUSY ;return 0 ; }EXPORT_SYMBOL_GPL (vfio_pci_core_setup_barmap);include/linux/vfio_pci_core.h +1
-0@@ -101,6 +101,7 @@ struct vfio_pci_core_device {const struct vfio_pci_device_ops *pci_ops;void __iomem *barmap[PCI_STD_NUM_BARS ];bool bar_mmap_supported[PCI_STD_NUM_BARS ]; +bool have_bar_resource[PCI_STD_NUM_BARS ];u8 *pci_config_map;u8 *vconfig;struct perm_bits *msi_perm;
Alex Williamson
maintainerRe: [PATCH v2 1/3] vfio/pci: Set up bar resources and maps in vfio_pci_core_enable()
Quoted 33 lines · On Fri, 24 Apr 2026 16:15:06 +0100 Matt Evans <[email protected]> wrote:
Quoted 18 lines · Alex Williamson
Quoted 16 lines · On Thu, 23 Apr 2026 11:25:07 -0700 Matt Evans <[email protected]> wrote:
drivers/vfio/pci/vfio_pci_core.c +16
-0+ +if (pci_resource_len (pdev, i) ==0 ) +continue ; + + ret =pci_request_selected_regions (pdev,1 << bar,"vfio" ); +if (ret) { +pci_warn (vdev->pdev,"Failed to reserve region %d\n" , bar); +continue ; + } + vdev->have_bar_resource[bar] = true; + + io =pci_iomap (pdev, bar,0 ); +if (io) + vdev->barmap[bar] = io; +else +pci_warn (vdev->pdev,"Failed to iomap region %d\n" , bar);Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ } +}I see you making the point in the cover letter about the resource request vs the iomap resource, but we currently handle these together. If either fails, setup barmap fails and the path returns error. I don't see any justification for now allowing the request resource to succeed but the iomap fails.
The primary effect was to let consumers see
-EBUSYfor a resource reservation failure or-ENOMEMfor an iomap failure (whether through this patch'svfio_pci_core_setup_barmap()or the next patch's helpers), and that keeps the error signatures identical.
Quoted 4 lines
A weak secondary effect was that a BAR that gets resource but fails for whatever reason to iomap it can still be used by most clients (assuming the general usage is to mmap). The system's pretty sick if this is the case, so as I say it's weak.
Right, I don't see that's really a necessary add at this point. In fact while we expect users to access through the mmap when available, we don't actually have a way to specify that mmap works w/o read/write, which is effectively what this proposes is a valid state.
Quoted 11 lines
OK, if you prefer the combined approach and don't feel the subsequent single-semantic check helpers (need mapping, need resource) are clearer to read then I'll recombine them, though:
- If
vfio_pci_core_map_bars()just sets barmap[n] iff both resource
acquisition and iomap succeed, then a later check can only return one error from either cause. I'll go with-ENOMEMunless you prefer-EBUSY.
Using something else can again make userspace see previously-unseen
error values.
Quoted 3 lines
- IMHO
vfio_pci_core_setup_barmap()should still be renamed (in a 2nd
patch) since it doesn't do any setting up anymore. Cosmetic, but cleaner to parse when the callers usevfio_pci_core_check_barmap_valid()no?
I'm not sure how important it is to preserve the identical errno, but we can actually do that too. Make the enable time "setup" function store the ERR_PTR in the barmap and change the current callers from "setup" to "get-iomap", where get-iomap is a __must_check return that callers test against IS_ERR_OR_NULL().
Or maybe better, collapse NULL into -ENODEV so callers only test IS_ERR().
There's even one user in vfio_pci_bar_rw() where this provides a minor simplification. Likely the others are just wrapping the get-iomap call in the ERR/NULL test to get the equivalent behavior. Thoughts? Thanks,
Alex
Re: [PATCH v2 2/3] vfio/pci: Replace vfio_pci_core_setup_barmap() with checks for resource/map
In reply to Matt EvansQuoted 20 lines · Matt Evans
Since "vfio/pci: Set up barmap in
vfio_pci_core_enable()", the resource request and iomap for the BARs was performed early, andvfio_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 itThis replaces
vfio_pci_core_setup_barmap()with two helpers,vfio_pci_core_check_barmap_valid()andvfio_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(-)
<...>
include/linux/vfio_pci_core.h VFIO_IOREAD_DECLARATION(32) · line 233
Quoted 7 lines
include/linux/vfio_pci_core.h +7
-0+/* 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 )
I understand this line was copied from an earlier implementation, but barmap[bar] is a pointer, and it is set to NULL when it is released.
if (!dev->barmap[bar])
return -ENOMEM;
Thanks
Quoted 21 lines
include/linux/vfio_pci_core.h +15
-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,Signature · 1 line
2.47.3
Re: [PATCH v2 3/3] vfio/pci: Check BAR resources before exporting a DMABUF
In reply to Matt EvansQuoted 5 lines · Matt Evans
A DMABUF exports access to BAR resources and, although they are requested at startup time, we need to ensure they really were reserved before exporting. Otherwise, it's possible to access unreserved resources through the export.
Quoted 8 lines
Add a check to the DMABUF-creation path.
Fixes 5d74781ebc86c ("vfio/pci: Add dma-buf export support for MMIO regions")Signed-off-by Matt Evans <[email protected]>--- drivers/vfio/pci/vfio_pci_dmabuf.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
Thanks,
Re: [PATCH v2 1/3] vfio/pci: Set up bar resources and maps in vfio_pci_core_enable()
In reply to Alex WilliamsonHi Alex,
Quoted 61 lines · Alex Williamson
Quoted 33 lines · On Fri, 24 Apr 2026 16:15:06 +0100 Matt Evans <[email protected]> wrote:
Quoted 18 lines · Alex Williamson
Quoted 16 lines · On Thu, 23 Apr 2026 11:25:07 -0700 Matt Evans <[email protected]> wrote:
drivers/vfio/pci/vfio_pci_core.c +16
-0+ +if (pci_resource_len (pdev, i) ==0 ) +continue ; + + ret =pci_request_selected_regions (pdev,1 << bar,"vfio" ); +if (ret) { +pci_warn (vdev->pdev,"Failed to reserve region %d\n" , bar); +continue ; + } + vdev->have_bar_resource[bar] = true; + + io =pci_iomap (pdev, bar,0 ); +if (io) + vdev->barmap[bar] = io; +else +pci_warn (vdev->pdev,"Failed to iomap region %d\n" , bar);Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ } +}I see you making the point in the cover letter about the resource request vs the iomap resource, but we currently handle these together. If either fails, setup barmap fails and the path returns error. I don't see any justification for now allowing the request resource to succeed but the iomap fails.
The primary effect was to let consumers see
-EBUSYfor a resource reservation failure or-ENOMEMfor an iomap failure (whether through this patch'svfio_pci_core_setup_barmap()or the next patch's helpers), and that keeps the error signatures identical.Quoted 4 lines
A weak secondary effect was that a BAR that gets resource but fails for whatever reason to iomap it can still be used by most clients (assuming the general usage is to mmap). The system's pretty sick if this is the case, so as I say it's weak.
Right, I don't see that's really a necessary add at this point. In fact while we expect users to access through the mmap when available, we don't actually have a way to specify that mmap works w/o read/write, which is effectively what this proposes is a valid state.
Quoted 11 lines
OK, if you prefer the combined approach and don't feel the subsequent single-semantic check helpers (need mapping, need resource) are clearer to read then I'll recombine them, though:
- If
vfio_pci_core_map_bars()just sets barmap[n] iff both resource
acquisition and iomap succeed, then a later check can only return one error from either cause. I'll go with-ENOMEMunless you prefer-EBUSY.
Using something else can again make userspace see previously-unseen
error values.Quoted 3 lines
- IMHO
vfio_pci_core_setup_barmap()should still be renamed (in a 2nd
patch) since it doesn't do any setting up anymore. Cosmetic, but cleaner to parse when the callers usevfio_pci_core_check_barmap_valid()no?
Quoted 5 lines
I'm not sure how important it is to preserve the identical errno, but we can actually do that too. Make the enable time "setup" function store the
ERR_PTRin the barmap and change the current callers from "setup" to "get-iomap", where get-iomap is a__must_checkreturn that callers test againstIS_ERR_OR_NULL().
I like that! (My mental model of ERR_PTR was one of function return values and must admit it didn't occur to me to stash one longer-term in barmap.)
Quoted 6 lines
Or maybe better, collapse NULL into
-ENODEVso callers only testIS_ERR().There's even one user in
vfio_pci_bar_rw()where this provides a minor simplification. Likely the others are just wrapping the get-iomap call in the ERR/NULL test to get the equivalent behavior. Thoughts? Thanks,
Just implemented a vfio_pci_core_get_iomap() and I think it's a lot nicer. I also prefer the helper returning the BAR map pointer rather than the open-coded vdev->barmap[]s. I've moved the helper [back] to a header, since the additional index checks will then fold out at compile time in some cases.
Thanks much for the suggestion, posting a v3 shortly.
Matt
Re: [PATCH v2 2/3] vfio/pci: Replace vfio_pci_core_setup_barmap() with checks for resource/map
In reply to Leon RomanovskyHi Leon,
Quoted 36 lines · Leon Romanovsky
Quoted 1 line
Quoted 20 lines · Matt Evans
Since "vfio/pci: Set up barmap in
vfio_pci_core_enable()", the resource request and iomap for the BARs was performed early, andvfio_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 itThis replaces
vfio_pci_core_setup_barmap()with two helpers,vfio_pci_core_check_barmap_valid()andvfio_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(-)<...>
include/linux/vfio_pci_core.h VFIO_IOREAD_DECLARATION(32) · line 233
Quoted 7 lines
include/linux/vfio_pci_core.h +7
-0+/* 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 )I understand this line was copied from an earlier implementation, but barmap[bar] is a pointer, and it is set to NULL when it is released.
Quoted 2 lines
if (!dev->barmap[bar])
return -ENOMEM;
Absolutely. Fixed in upcoming v3, thank you,
Matt
Quoted 24 lines
Thanks
Quoted 21 lines
include/linux/vfio_pci_core.h +15
-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,Signature · 1 line
2.47.3