All of lore.kernel.org
 help / color / mirror / Atom feed
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 4/5] vfio/pci: Advertise the mmap areas around excluded ranges
Date: Wed, 2 Dec 2026 15:02:49 +0530	[thread overview]
Message-ID: <20261202093250.1683744-5-mhonap@nvidia.com> (raw)
In-Reply-To: <20261202093250.1683744-1-mhonap@nvidia.com>

From: Manish Honap <mhonap@nvidia.com>

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.

Signed-off-by: Manish Honap <mhonap@nvidia.com>
---
 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.
 	 */
-- 
2.53.0


  parent reply	other threads:[~2026-12-02  9:33 UTC|newest]

Thread overview: 9+ 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 ` [PATCH 1/5] vfio/pci: Move the excluded windows out of vfio_pci_core_do_io_rw() mhonap
2026-12-02  9:44   ` 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 ` mhonap [this message]
2026-12-02  9:32 ` [PATCH 5/5] vfio/pci: Refuse ioeventfds and dma-buf exports over excluded ranges mhonap
2026-12-02  9:42   ` sashiko-bot
2026-12-02  9:47 ` [PATCH 0/5] vfio/pci: Per-BAR " 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-5-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.