From: <mhonap@nvidia.com>
To: <jgg@ziepe.ca>, <yishaih@nvidia.com>, <sumit.semwal@linaro.org>
Cc: <kvm@vger.kernel.org>, <virtualization@lists.linux.dev>,
<linux-media@vger.kernel.org>, <dri-devel@lists.freedesktop.org>,
<linaro-mm-sig@lists.linaro.org>, <mhonap@nvidia.com>,
<linux-cxl@vger.kernel.org>
Subject: [PATCH 1/5] vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw()
Date: Wed, 2 Dec 2026 15:02:46 +0530 [thread overview]
Message-ID: <20261202093250.1683744-2-mhonap@nvidia.com> (raw)
In-Reply-To: <20261202093250.1683744-1-mhonap@nvidia.com>
From: Manish Honap <mhonap@nvidia.com>
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.
Signed-off-by: Manish Honap <mhonap@nvidia.com>
---
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(-)
diff --git a/drivers/vfio/pci/nvgrace-gpu/main.c b/drivers/vfio/pci/nvgrace-gpu/main.c
index 5ddec340b73b..432f6b9c2842 100644
--- a/drivers/vfio/pci/nvgrace-gpu/main.c
+++ b/drivers/vfio/pci/nvgrace-gpu/main.c
@@ -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;
diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
index df8848087681..68008f28bcbe 100644
--- a/drivers/vfio/pci/vfio_pci_rdwr.c
+++ b/drivers/vfio/pci/vfio_pci_rdwr.c
@@ -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
- * 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)
{
ssize_t done = 0;
int ret;
while (count) {
- size_t fillable, filled;
-
- if (off < x_start)
- fillable = min(count, (size_t)(x_start - off));
- else if (off >= x_end)
- fillable = count;
- else
- fillable = 0;
+ size_t filled;
- if (fillable >= 8 && !(off % 8) && max_width >= 8) {
+ if (count >= 8 && !(off % 8) && max_width >= 8) {
ret = vfio_pci_iordwr64(vdev, iswrite, test_mem,
io, buf, off, &filled);
if (ret)
return ret;
- } else if (fillable >= 4 && !(off % 4) && max_width >= 4) {
+ } else if (count >= 4 && !(off % 4) && max_width >= 4) {
ret = vfio_pci_iordwr32(vdev, iswrite, test_mem,
io, buf, off, &filled);
if (ret)
return ret;
- } else if (fillable >= 2 && !(off % 2) && max_width >= 2) {
+ } else if (count >= 2 && !(off % 2) && max_width >= 2) {
ret = vfio_pci_iordwr16(vdev, iswrite, test_mem,
io, buf, off, &filled);
if (ret)
return ret;
- } else if (fillable) {
+ } else {
ret = vfio_pci_iordwr8(vdev, iswrite, test_mem,
io, buf, off, &filled);
if (ret)
return ret;
-
- } else {
- /* Fill reads with -1, drop writes */
- filled = min(count, (size_t)(x_end - off));
- if (!iswrite) {
- u8 val = 0xFF;
- size_t i;
-
- for (i = 0; i < filled; i++)
- if (copy_to_user(buf + i, &val, 1))
- return -EFAULT;
- }
}
count -= filled;
@@ -198,17 +174,55 @@ 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);
+static ssize_t vfio_pci_excluded_range_rw(struct vfio_pci_core_device *vdev,
+ bool test_mem, void __iomem *io,
+ char __user *buf, loff_t pos,
+ size_t count, const struct range *x,
+ bool iswrite,
+ enum vfio_pci_io_width max_width)
+{
+ size_t before = 0, len, i;
+ ssize_t ret;
+
+ if (pos < x->start) {
+ before = min_t(u64, count, x->start - pos);
+ ret = vfio_pci_core_do_io_rw(vdev, test_mem, io, buf, pos,
+ before, iswrite, max_width);
+ if (ret < 0)
+ return ret;
+ if (before == count)
+ return before;
+ pos += before;
+ buf += before;
+ count -= before;
+ }
+
+ if (pos > x->end)
+ return before;
+
+ len = min_t(u64, count, x->end - pos + 1);
+ if (!iswrite) {
+ u8 val = 0xFF;
+
+ for (i = 0; i < len; i++)
+ if (copy_to_user(buf + i, &val, 1))
+ return -EFAULT;
+ }
+
+ return before + len;
+}
+
ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
size_t count, loff_t *ppos, bool iswrite)
{
struct pci_dev *pdev = vdev->pdev;
loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
int bar = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
- size_t x_start = 0, x_end = 0;
resource_size_t end;
void __iomem *io;
struct resource *res = &vdev->pdev->resource[bar];
- ssize_t done;
+ bool test_mem = res->flags & IORESOURCE_MEM;
+ ssize_t done = 0, ret;
enum vfio_pci_io_width max_width = VFIO_PCI_IO_WIDTH_8;
if (pci_resource_start(pdev, bar))
@@ -224,20 +238,21 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
count = min(count, (size_t)(end - pos));
if (bar == PCI_ROM_RESOURCE) {
+ size_t rom_len;
+
/*
* The ROM can fill less space than the BAR, so we start the
* excluded range at the end of the actual ROM. This makes
* filling large ROM BARs much faster.
*/
if (pci_resource_start(pdev, bar)) {
- io = pci_map_rom(pdev, &x_start);
+ io = pci_map_rom(pdev, &rom_len);
} else {
io = ioremap(pdev->rom, pdev->romlen);
- x_start = pdev->romlen;
+ rom_len = pdev->romlen;
}
if (!io)
return -ENOMEM;
- x_end = end;
/*
* Certain devices (e.g. Intel X710) don't support qword
@@ -248,6 +263,14 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
* worked reliably for years before qword access is enabled.
*/
max_width = VFIO_PCI_IO_WIDTH_4;
+
+ if (rom_len < end) {
+ struct range x = DEFINE_RANGE(rom_len, end - 1);
+
+ done = vfio_pci_excluded_range_rw(vdev, test_mem, io, buf,
+ pos, count, &x, iswrite,
+ max_width);
+ }
} else {
io = vfio_pci_core_get_iomap(vdev, bar);
if (IS_ERR(io)) {
@@ -257,12 +280,20 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
}
if (bar == vdev->msix_bar) {
- x_start = vdev->msix_offset;
- x_end = vdev->msix_offset + vdev->msix_size;
+ 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_core_do_io_rw(vdev, res->flags & IORESOURCE_MEM, io, buf, pos,
- count, x_start, x_end, iswrite, max_width);
+ if (done >= 0) {
+ ret = vfio_pci_core_do_io_rw(vdev, test_mem, io, buf + done,
+ pos + done, count - done, iswrite,
+ max_width);
+ done = ret < 0 ? ret : done + ret;
+ }
if (done >= 0)
*ppos += done;
@@ -336,7 +367,7 @@ ssize_t vfio_pci_vga_rw(struct vfio_pci_core_device *vdev, char __user *buf,
* to the memory enable bit in the command register.
*/
done = vfio_pci_core_do_io_rw(vdev, false, iomem, buf, off, count,
- 0, 0, iswrite, VFIO_PCI_IO_WIDTH_4);
+ iswrite, VFIO_PCI_IO_WIDTH_4);
vga_put(vdev->pdev, rsrc);
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index dc25f737fe93..e472777afc16 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -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,
--
2.53.0
next prev parent reply other threads:[~2026-12-02 9:33 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 9:32 [PATCH 0/5] vfio/pci: Per-BAR excluded ranges mhonap
2026-12-02 9:32 ` mhonap [this message]
2026-12-02 9:44 ` [PATCH 1/5] vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw() sashiko-bot
2026-12-02 9:32 ` [PATCH 2/5] vfio/virtio: Release vfio-pci-core state when legacy I/O setup fails mhonap
2026-12-02 9:32 ` [PATCH 3/5] vfio/pci: Add per-BAR excluded ranges and put the MSI-X table on them mhonap
2026-12-02 9:32 ` [PATCH 4/5] vfio/pci: Advertise the mmap areas around excluded ranges mhonap
2026-12-02 9:32 ` [PATCH 5/5] vfio/pci: Refuse ioeventfds and dma-buf exports over " mhonap
2026-12-02 9:42 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261202093250.1683744-2-mhonap@nvidia.com \
--to=mhonap@nvidia.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=jgg@ziepe.ca \
--cc=kvm@vger.kernel.org \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=sumit.semwal@linaro.org \
--cc=virtualization@lists.linux.dev \
--cc=yishaih@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox