From: Pranjal Shrivastava <praan@google.com>
To: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org,
kvm@vger.kernel.org
Cc: Bjorn Helgaas <bhelgaas@google.com>,
Logan Gunthorpe <logang@deltatee.com>,
Alex Williamson <alex@shazbot.org>,
Jason Gunthorpe <jgg@ziepe.ca>, Kevin Tian <kevin.tian@intel.com>,
Pranjal Shrivastava <praan@google.com>,
Ankit Agrawal <ankita@nvidia.com>, Matt Evans <mattev@meta.com>,
Vivek Kasireddy <vivek.kasireddy@intel.com>,
Leon Romanovsky <leon@kernel.org>,
Shivaji Kant <shivajikant@google.com>,
Samiullah Khawaja <skhawaja@google.com>
Subject: [RFC PATCH 5/5] PCI/P2PDMA: Introduce a helper to release P2P resources
Date: Wed, 10 Jun 2026 15:18:53 +0000 [thread overview]
Message-ID: <20260610151853.3608948-6-praan@google.com> (raw)
In-Reply-To: <20260610151853.3608948-1-praan@google.com>
Introduce pci_p2pdma_remove_resource() to allow manual teardown of a
device's P2P DMA pool. The new API enables exclusive owners of a device,
such as vfio-pci, to cleanly release P2P resources during session closure
or hardware reset.
Update vfio-pci to call this function during vfio_pci_zap_bars(),
ensuring that any BARs registered with ZONE_DEVICE P2P are released.
Signed-off-by: Pranjal Shrivastava <praan@google.com>
---
drivers/pci/p2pdma.c | 38 ++++++++++++++++++++++++++++++++
drivers/vfio/pci/vfio_pci_core.c | 5 +++++
include/linux/pci-p2pdma.h | 1 +
3 files changed, 44 insertions(+)
diff --git a/drivers/pci/p2pdma.c b/drivers/pci/p2pdma.c
index adb17a4f6939..2a48ffefa01c 100644
--- a/drivers/pci/p2pdma.c
+++ b/drivers/pci/p2pdma.c
@@ -26,6 +26,7 @@ struct pci_p2pdma {
bool p2pmem_published;
struct xarray map_types;
struct p2pdma_provider mem[PCI_STD_NUM_BARS];
+ struct pci_p2pdma_pagemap *pagemaps[PCI_STD_NUM_BARS];
};
struct pci_p2pdma_pagemap {
@@ -453,6 +454,8 @@ int pci_p2pdma_add_resource(struct pci_dev *pdev, int bar, size_t size,
if (error)
goto pages_free;
+ p2pdma->pagemaps[bar] = p2p_pgmap;
+
pci_info(pdev, "added peer-to-peer DMA memory %#llx-%#llx\n",
pgmap->range.start, pgmap->range.end);
@@ -466,6 +469,41 @@ int pci_p2pdma_add_resource(struct pci_dev *pdev, int bar, size_t size,
}
EXPORT_SYMBOL_GPL(pci_p2pdma_add_resource);
+/**
+ * pci_p2pdma_remove_resource - remove all p2p memory for a device
+ * @pdev: the device to remove the memory from
+ *
+ * Tear down the entire p2p DMA pool for the device. Zap any existinga
+ * userspace mappings of the p2pmem/allocate file
+ */
+void pci_p2pdma_remove_resource(struct pci_dev *pdev)
+{
+ struct pci_p2pdma *p2pdma;
+ int i;
+
+ p2pdma = rcu_dereference_protected(pdev->p2pdma, 1);
+ if (!p2pdma || !p2pdma->pool)
+ return;
+
+ for (i = 0; i < PCI_STD_NUM_BARS; i++) {
+ if (p2pdma->pagemaps[i]) {
+ devm_release_action(&pdev->dev, pci_p2pdma_unmap_mappings,
+ p2pdma->pagemaps[i]);
+ devm_memunmap_pages(&pdev->dev, &p2pdma->pagemaps[i]->pgmap);
+ devm_kfree(&pdev->dev, p2pdma->pagemaps[i]);
+ p2pdma->pagemaps[i] = NULL;
+ }
+ }
+
+ gen_pool_destroy(p2pdma->pool);
+ p2pdma->pool = NULL;
+
+ sysfs_remove_group(&pdev->dev.kobj, &p2pmem_group);
+
+ pci_info(pdev, "removed all peer-to-peer DMA memory\n");
+}
+EXPORT_SYMBOL_GPL(pci_p2pdma_remove_resource);
+
/*
* Note this function returns the parent PCI device with a
* reference taken. It is the caller's responsibility to drop
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 7913b8916df9..7b58cb344408 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -1704,6 +1704,11 @@ static void vfio_pci_zap_bars(struct vfio_pci_core_device *vdev)
loff_t len = end - start;
unmap_mapping_range(core_vdev->inode->i_mapping, start, len, true);
+
+ if (vdev->p2p_registered_bars) {
+ pci_p2pdma_remove_resource(vdev->pdev);
+ vdev->p2p_registered_bars = 0;
+ }
}
void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_core_device *vdev)
diff --git a/include/linux/pci-p2pdma.h b/include/linux/pci-p2pdma.h
index 873de20a2247..14ee2e59a43e 100644
--- a/include/linux/pci-p2pdma.h
+++ b/include/linux/pci-p2pdma.h
@@ -72,6 +72,7 @@ int pcim_p2pdma_init(struct pci_dev *pdev);
struct p2pdma_provider *pcim_p2pdma_provider(struct pci_dev *pdev, int bar);
int pci_p2pdma_add_resource(struct pci_dev *pdev, int bar, size_t size,
u64 offset);
+void pci_p2pdma_remove_resource(struct pci_dev *pdev);
int pci_p2pdma_distance_many(struct pci_dev *provider, struct device **clients,
int num_clients, bool verbose);
struct pci_dev *pci_p2pmem_find_many(struct device **clients, int num_clients);
--
2.54.0.1099.g489fc7bff1-goog
next prev parent reply other threads:[~2026-06-10 15:19 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-10 15:18 [RFC PATCH 0/5] vfio/pci: Support ZONE_DEVICE-backed P2P Registration Pranjal Shrivastava
2026-06-10 15:18 ` [RFC PATCH 1/5] vfio: Add UAPI for ZONE_DEVICE-backed P2P registration Pranjal Shrivastava
2026-06-10 15:18 ` [RFC PATCH 2/5] vfio/pci: Implement " Pranjal Shrivastava
2026-06-10 15:18 ` [RFC PATCH 3/5] vfio/pci: Block mmap & dmabuf export for ZONE_DEVICE-registered BARs Pranjal Shrivastava
2026-06-10 15:18 ` [RFC PATCH 4/5] vfio/pci: Block ZONE_DEVICE registration for BARs with active DMABUFs Pranjal Shrivastava
2026-06-10 15:18 ` Pranjal Shrivastava [this message]
2026-06-10 16:28 ` [RFC PATCH 0/5] vfio/pci: Support ZONE_DEVICE-backed P2P Registration Jason Gunthorpe
2026-06-10 18:32 ` Leon Romanovsky
2026-06-11 14:40 ` Pranjal Shrivastava
2026-06-11 14:43 ` Pranjal Shrivastava
2026-06-11 22:14 ` Jason Gunthorpe
2026-06-12 14:50 ` Pranjal Shrivastava
2026-06-16 0:42 ` Samiullah Khawaja
2026-06-16 6:38 ` 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=20260610151853.3608948-6-praan@google.com \
--to=praan@google.com \
--cc=alex@shazbot.org \
--cc=ankita@nvidia.com \
--cc=bhelgaas@google.com \
--cc=jgg@ziepe.ca \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=leon@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=logang@deltatee.com \
--cc=mattev@meta.com \
--cc=shivajikant@google.com \
--cc=skhawaja@google.com \
--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
all inboxes | Powered by JetHome®