From: Matt Evans <matt@ozlabs.org>
To: "Alex Williamson" <alex@shazbot.org>,
"Leon Romanovsky" <leon@kernel.org>,
"Jason Gunthorpe" <jgg@nvidia.com>,
"Alex Mastro" <amastro@fb.com>,
"Christian König" <christian.koenig@amd.com>,
"Bjorn Helgaas" <bhelgaas@google.com>,
"Logan Gunthorpe" <logang@deltatee.com>
Cc: "Mahmoud Adam" <mngyadam@amazon.de>,
"David Matlack" <dmatlack@google.com>,
"Björn Töpel" <bjorn@kernel.org>,
"Sumit Semwal" <sumit.semwal@linaro.org>,
"Kevin Tian" <kevin.tian@intel.com>,
"Ankit Agrawal" <ankita@nvidia.com>,
"Pranjal Shrivastava" <praan@google.com>,
"Alistair Popple" <apopple@nvidia.com>,
"Vivek Kasireddy" <vivek.kasireddy@intel.com>,
linux-kernel@vger.kernel.org, linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
kvm@vger.kernel.org, linux-pci@vger.kernel.org
Subject: [PATCH v3 9/9] vfio/pci: Add mmap() attributes to DMABUF feature
Date: Wed, 10 Jun 2026 16:43:23 +0100 [thread overview]
Message-ID: <20260610154327.37758-10-matt@ozlabs.org> (raw)
In-Reply-To: <20260610154327.37758-1-matt@ozlabs.org>
A new VFIO feature, VFIO_DEVICE_FEATURE_DMA_BUF_MEMATTR, is added to
set CPU-facing memory type attributes for a DMABUF exported from
vfio-pci. These are used for subsequent mmap()s of the buffer.
There are two attributes supported:
- The default, VFIO_DEVICE_FEATURE_DMA_BUF_MEMATTR_NC
- VFIO_DEVICE_FEATURE_DMA_BUF_MEMATTR_WC, which results in WC
PTEs for the DMABUF's BAR region.
Signed-off-by: Matt Evans <matt@ozlabs.org>
---
drivers/vfio/pci/vfio_pci_core.c | 2 ++
drivers/vfio/pci/vfio_pci_dmabuf.c | 57 +++++++++++++++++++++++++++++-
drivers/vfio/pci/vfio_pci_priv.h | 14 ++++++++
include/uapi/linux/vfio.h | 27 ++++++++++++++
4 files changed, 99 insertions(+), 1 deletion(-)
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 064906b25467..dc9c6f479e2c 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -1575,6 +1575,8 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
return vfio_pci_core_feature_dma_buf(vdev, flags, arg, argsz);
case VFIO_DEVICE_FEATURE_DMA_BUF_REVOKE:
return vfio_pci_core_feature_dma_buf_revoke(vdev, flags, arg, argsz);
+ case VFIO_DEVICE_FEATURE_DMA_BUF_MEMATTR:
+ return vfio_pci_core_feature_dma_buf_memattr(vdev, flags, arg, argsz);
default:
return -ENOTTY;
}
diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci_dmabuf.c
index b47411992ab6..58b769e65ab8 100644
--- a/drivers/vfio/pci/vfio_pci_dmabuf.c
+++ b/drivers/vfio/pci/vfio_pci_dmabuf.c
@@ -51,7 +51,10 @@ static int vfio_pci_dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *
* contained within the DMABUF size before calling this.
*/
- vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+ if (READ_ONCE(priv->memattr) == VFIO_DEVICE_FEATURE_DMA_BUF_MEMATTR_WC)
+ vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
+ else
+ vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
/* See comments in vfio_pci_core_mmap() re VM_ALLOW_ANY_UNCACHED. */
@@ -468,6 +471,7 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
priv->vdev = vdev;
priv->nr_ranges = get_dma_buf.nr_ranges;
priv->size = length;
+ priv->memattr = VFIO_DEVICE_FEATURE_DMA_BUF_MEMATTR_NC;
ret = vdev->pci_ops->get_dmabuf_phys(vdev, &priv->provider,
get_dma_buf.region_index,
priv->phys_vec, dma_ranges,
@@ -755,6 +759,57 @@ int vfio_pci_core_feature_dma_buf_revoke(
}
}
+out_put_buf:
+ dma_buf_put(dmabuf);
+
+ return ret;
+}
+
+int vfio_pci_core_feature_dma_buf_memattr(
+ struct vfio_pci_core_device *vdev, u32 flags,
+ struct vfio_device_feature_dma_buf_memattr __user *arg,
+ size_t argsz)
+{
+ struct vfio_device_feature_dma_buf_memattr db_attr;
+ struct vfio_pci_dma_buf *priv;
+ struct dma_buf *dmabuf;
+ int ret;
+
+ if (!vdev->pci_ops || !vdev->pci_ops->get_dmabuf_phys)
+ return -EOPNOTSUPP;
+
+ ret = vfio_check_feature(flags, argsz,
+ VFIO_DEVICE_FEATURE_SET,
+ sizeof(db_attr));
+ if (ret != 1)
+ return ret;
+
+ if (copy_from_user(&db_attr, arg, sizeof(db_attr)))
+ return -EFAULT;
+
+ dmabuf = dma_buf_get(db_attr.dmabuf_fd);
+ if (IS_ERR(dmabuf))
+ return PTR_ERR(dmabuf);
+
+ /* Verify DMABUF: see comments in vfio_pci_dma_buf_revoke() */
+ priv = dmabuf->priv;
+ if (dmabuf->ops != &vfio_pci_dmabuf_ops ||
+ READ_ONCE(priv->vdev) != vdev) {
+ ret = -ENODEV;
+ goto out_put_buf;
+ }
+
+ switch (db_attr.memattr) {
+ case VFIO_DEVICE_FEATURE_DMA_BUF_MEMATTR_NC:
+ case VFIO_DEVICE_FEATURE_DMA_BUF_MEMATTR_WC:
+ WRITE_ONCE(priv->memattr, db_attr.memattr);
+ ret = 0;
+ break;
+
+ default:
+ ret = -ENOENT;
+ }
+
out_put_buf:
dma_buf_put(dmabuf);
diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
index 3c2f2575b670..3ec8b62194f3 100644
--- a/drivers/vfio/pci/vfio_pci_priv.h
+++ b/drivers/vfio/pci/vfio_pci_priv.h
@@ -41,6 +41,7 @@ struct vfio_pci_dma_buf {
struct kref kref;
struct completion comp;
unsigned long vma_pgoff_adjust;
+ u32 memattr;
enum vfio_pci_dma_buf_status status;
};
@@ -158,6 +159,10 @@ int vfio_pci_core_feature_dma_buf_revoke(
struct vfio_pci_core_device *vdev, u32 flags,
struct vfio_device_feature_dma_buf_revoke __user *arg,
size_t argsz);
+int vfio_pci_core_feature_dma_buf_memattr(
+ struct vfio_pci_core_device *vdev, u32 flags,
+ struct vfio_device_feature_dma_buf_memattr __user *arg,
+ size_t argsz);
#else
static inline int
vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
@@ -166,6 +171,7 @@ vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
{
return -ENOTTY;
}
+
static inline int vfio_pci_core_feature_dma_buf_revoke(
struct vfio_pci_core_device *vdev, u32 flags,
struct vfio_device_feature_dma_buf_revoke __user *arg,
@@ -173,6 +179,14 @@ static inline int vfio_pci_core_feature_dma_buf_revoke(
{
return -ENOTTY;
}
+
+static inline int vfio_pci_core_feature_dma_buf_memattr(
+ struct vfio_pci_core_device *vdev, u32 flags,
+ struct vfio_device_feature_dma_buf_memattr __user *arg,
+ size_t argsz)
+{
+ return -ENOTTY;
+}
#endif
#endif
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index 697c0bb4b9bc..ab30b89399d0 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -1554,6 +1554,33 @@ struct vfio_device_feature_dma_buf_revoke {
__s32 dmabuf_fd;
};
+/**
+ * Given a dma_buf fd previously created by
+ * VFIO_DEVICE_FEATURE_DMA_BUF, SET the memory attribute that will be
+ * used by future mmap()s of that fd. SETting a new attribute does
+ * not affect existing VMAs.
+ *
+ * The default, if no previous SET has been performed, is NC.
+ *
+ * Return: 0 on success, -1 and errno is set on failure:
+ *
+ * EBADF, EINVAL: dmabuf_fd is not a DMABUF fd.
+ * ENODEV: The dmabuf_fd does not match this VFIO device.
+ * ENOENT: The given memattr is not supported.
+ */
+#define VFIO_DEVICE_FEATURE_DMA_BUF_MEMATTR 14
+
+/* Valid memory attributes for the memattr field */
+enum vfio_device_dma_buf_memattr {
+ VFIO_DEVICE_FEATURE_DMA_BUF_MEMATTR_NC = 0, /* pgprot_noncached */
+ VFIO_DEVICE_FEATURE_DMA_BUF_MEMATTR_WC = 1, /* pgprot_writecombine */
+};
+
+struct vfio_device_feature_dma_buf_memattr {
+ __s32 dmabuf_fd;
+ __u32 memattr;
+};
+
/* -------- API for Type1 VFIO IOMMU -------- */
/**
--
2.50.1 (Apple Git-155)
next prev parent reply other threads:[~2026-06-10 15:45 UTC|newest]
Thread overview: 70+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-10 15:43 [PATCH v3 0/9] vfio/pci: Add mmap() for DMABUFs Matt Evans
2026-06-10 15:43 ` [PATCH v3 1/9] PCI/P2PDMA: Add CONFIG_PCI_P2PDMA_CORE Matt Evans
2026-06-10 18:39 ` Leon Romanovsky
2026-06-11 16:07 ` Bjorn Helgaas
2026-06-11 17:44 ` Matt Evans
2026-06-11 18:37 ` Pranjal Shrivastava
2026-06-12 3:39 ` Tian, Kevin
2026-06-12 14:31 ` Matt Evans
2026-06-23 15:48 ` Robin Murphy
2026-06-23 15:59 ` Matt Evans
2026-06-23 17:47 ` Matt Evans
2026-06-24 11:42 ` Robin Murphy
2026-06-17 7:47 ` Tian, Kevin
2026-06-10 15:43 ` [PATCH v3 2/9] vfio/pci: Add a helper to look up PFNs for DMABUFs Matt Evans
2026-06-11 20:30 ` Pranjal Shrivastava
2026-06-12 17:37 ` Alex Williamson
2026-06-12 18:21 ` Pranjal Shrivastava
2026-06-15 14:27 ` Matt Evans
2026-06-15 15:07 ` Pranjal Shrivastava
2026-06-12 8:42 ` Tian, Kevin
2026-06-15 18:04 ` Matt Evans
2026-06-16 9:28 ` Tian, Kevin
2026-06-16 11:48 ` Matt Evans
2026-06-10 15:43 ` [PATCH v3 3/9] vfio/pci: Add a helper to create a DMABUF for a BAR-map VMA Matt Evans
2026-06-12 8:43 ` Tian, Kevin
2026-06-12 9:20 ` Pranjal Shrivastava
2026-06-10 15:43 ` [PATCH v3 4/9] vfio/pci: Convert BAR mmap() to use a DMABUF Matt Evans
2026-06-12 8:46 ` Tian, Kevin
2026-06-15 15:33 ` Matt Evans
2026-06-12 10:41 ` Pranjal Shrivastava
2026-06-12 15:22 ` Matt Evans
2026-06-12 19:43 ` Pranjal Shrivastava
2026-06-10 15:43 ` [PATCH v3 5/9] vfio/pci: Provide a user-facing name for BAR mappings Matt Evans
2026-06-12 8:46 ` Tian, Kevin
2026-06-12 14:06 ` Pranjal Shrivastava
2026-06-15 15:13 ` Matt Evans
2026-06-10 15:43 ` [PATCH v3 6/9] vfio/pci: Clean up BAR zap and revocation Matt Evans
2026-06-12 19:39 ` Pranjal Shrivastava
2026-06-16 9:48 ` Tian, Kevin
2026-06-16 18:51 ` Pranjal Shrivastava
2026-06-17 6:22 ` Tian, Kevin
2026-06-17 12:16 ` Jason Gunthorpe
2026-06-18 16:02 ` Matt Evans
2026-06-19 13:31 ` Jason Gunthorpe
2026-06-19 15:13 ` Matt Evans
2026-06-22 23:13 ` Alex Williamson
2026-06-23 11:08 ` Matt Evans
2026-06-23 12:35 ` Pranjal Shrivastava
2026-06-18 16:06 ` Matt Evans
2026-06-23 12:38 ` Pranjal Shrivastava
2026-06-16 9:18 ` Tian, Kevin
2026-06-29 17:26 ` Matt Evans
2026-06-10 15:43 ` [PATCH v3 7/9] vfio/pci: Support mmap() of a VFIO DMABUF Matt Evans
2026-06-12 20:35 ` Pranjal Shrivastava
2026-06-16 15:45 ` Matt Evans
2026-06-16 9:20 ` Tian, Kevin
2026-06-10 15:43 ` [PATCH v3 8/9] vfio/pci: Permanently revoke a DMABUF on request Matt Evans
2026-06-16 8:05 ` Pranjal Shrivastava
2026-06-17 16:22 ` Matt Evans
2026-06-16 9:26 ` Tian, Kevin
2026-06-17 16:08 ` Matt Evans
2026-06-29 16:32 ` Matt Evans
2026-06-10 15:43 ` Matt Evans [this message]
2026-06-16 8:47 ` [PATCH v3 9/9] vfio/pci: Add mmap() attributes to DMABUF feature Pranjal Shrivastava
2026-06-16 11:37 ` Matt Evans
2026-06-16 19:09 ` Pranjal Shrivastava
2026-06-16 9:26 ` Tian, Kevin
2026-06-12 8:27 ` [PATCH v3 0/9] vfio/pci: Add mmap() for DMABUFs Tian, Kevin
2026-06-12 15:11 ` Matt Evans
2026-06-12 15:17 ` Pranjal Shrivastava
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=20260610154327.37758-10-matt@ozlabs.org \
--to=matt@ozlabs.org \
--cc=alex@shazbot.org \
--cc=amastro@fb.com \
--cc=ankita@nvidia.com \
--cc=apopple@nvidia.com \
--cc=bhelgaas@google.com \
--cc=bjorn@kernel.org \
--cc=christian.koenig@amd.com \
--cc=dmatlack@google.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=jgg@nvidia.com \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=leon@kernel.org \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=logang@deltatee.com \
--cc=mngyadam@amazon.de \
--cc=praan@google.com \
--cc=sumit.semwal@linaro.org \
--cc=vivek.kasireddy@intel.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