Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH rdma-next v2 0/3] RDMA/mlx5: Support pinned revocable dma-buf import
@ 2026-11-29 15:25 Edward Srouji
  2026-11-29 15:25 ` [PATCH rdma-next v2 1/3] RDMA/uverbs: Permit dma-buf export to work with pinned importers Edward Srouji
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Edward Srouji @ 2026-11-29 15:25 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky, Sumit Semwal,
	Christian König, Philipp Stanner
  Cc: linux-rdma, linux-kernel, linux-media, dri-devel, linaro-mm-sig,
	Edward Srouji, Chiara Meiohas, Yishai Hadas

Today mlx5 imports every dma-buf as movable. The mkey is sized for the
worst case, one entry per PAGE_SIZE of the dma-buf, and an invalidation
zaps the mkey and relies on ODP page faults to map it again. This makes
dma-buf MRs depend on CONFIG_INFINIBAND_ON_DEMAND_PAGING, and wastes
translation entries for exporters that never move their memory, such as
when GPU or device memory is registered for peer-to-peer DMA.

Exporters of PCI BAR memory, VFIO and the uverbs dma-buf export, do not
move the memory, but they must be able to take it away on a reset or a
memory-disable. The dma-buf core supports this with pinned revocable
attachments (dma_buf_attach_revocable()), and irdma already imports
dma-bufs this way, see commit 4707bf5f6c86 ("RDMA/irdma: Add support for
revocable pinned dmabuf import").

This series adds the same support to mlx5:

- Patch 1 lets the uverbs dma-buf export accept pinned importers that
  can handle a revoke.

- Patch 2 moves the dma-buf MR init out of the ODP-only odp.c into mr.c,
  with no functional change.

- Patch 3 makes mlx5 try a pinned revocable import first. When the
  exporter grants it, the buffer cannot move, so the mkey is sized
  exactly to its mapping. Otherwise mlx5 falls back to the existing
  movable import, which is the only case that still requires ODP. On a
  revoke the mkey is disabled with a UMR, so the device stops accessing
  the buffer before the exporter takes the memory away.

Signed-off-by: Edward Srouji <edwards@nvidia.com>
---
Changes in v2:
- Rebase on top of wip/leon-for-next, adapting to the mlx5 device
  helper renames (mr_to_dev(), mr_to_mdev() returning the core device).
- The WARN_ON(!page_size) in alloc_cacheable_mr() that Sashiko reported
  on v1 is fixed separately for rdma-rc:
  https://lore.kernel.org/linux-rdma/20261129-mlx5-no-warn-page-size-v1-1-02b15ebaea8c@nvidia.com/
- Link to v1: https://lore.kernel.org/r/20261128-mlx5-pinned-revocable-dmabuf-v1-0-a4cde143a58e@nvidia.com

---
Chiara Meiohas (2):
      RDMA/uverbs: Permit dma-buf export to work with pinned importers
      RDMA/mlx5: Move dma-buf MR init out of ODP-only compilation

Yishai Hadas (1):
      RDMA/mlx5: Try pinned revocable dmabuf import by default

 drivers/infiniband/core/uverbs_std_types_dmabuf.c |  14 +-
 drivers/infiniband/hw/mlx5/mlx5_ib.h              |   7 +-
 drivers/infiniband/hw/mlx5/mr.c                   | 188 ++++++++++++++++++++--
 drivers/infiniband/hw/mlx5/odp.c                  |  59 +------
 4 files changed, 186 insertions(+), 82 deletions(-)
---
base-commit: a8671ee9bde18105775c2307bc71b0933bd24ed4
change-id: 20261128-mlx5-pinned-revocable-dmabuf-31e90029af9e

Best regards,
-- 
Edward Srouji <edwards@nvidia.com>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH rdma-next v2 1/3] RDMA/uverbs: Permit dma-buf export to work with pinned importers
  2026-11-29 15:25 [PATCH rdma-next v2 0/3] RDMA/mlx5: Support pinned revocable dma-buf import Edward Srouji
@ 2026-11-29 15:25 ` Edward Srouji
  2026-11-29 15:25 ` [PATCH rdma-next v2 2/3] RDMA/mlx5: Move dma-buf MR init out of ODP-only compilation Edward Srouji
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Edward Srouji @ 2026-11-29 15:25 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky, Sumit Semwal,
	Christian König, Philipp Stanner
  Cc: linux-rdma, linux-kernel, linux-media, dri-devel, linaro-mm-sig,
	Edward Srouji, Chiara Meiohas, Yishai Hadas

From: Chiara Meiohas <cmeiohas@nvidia.com>

Till now the uverbs dma-buf export path (used to export PCI based
memory regions, e.g. device memory) has rejected pinned importers,
unconditionally failing .pin since it had no way to distinguish
importers that can handle a revoke from those that can't.

Using dma_buf_attach_revocable() it can tell the difference between
pinned importers that support the flow described in
dma_buf_invalidate_mappings() and those that don't.

Thus permit compatible pinned importers: drop the hardcoded-failing
.pin/.unpin and gate admission in .attach on revocability instead.

This follows the same approach taken in commit 6ffe939bf94fb ("vfio:
Permit VFIO to work with pinned importers").

Signed-off-by: Chiara Meiohas <cmeiohas@nvidia.com>
Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Signed-off-by: Edward Srouji <edwards@nvidia.com>
---
 drivers/infiniband/core/uverbs_std_types_dmabuf.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)

diff --git a/drivers/infiniband/core/uverbs_std_types_dmabuf.c b/drivers/infiniband/core/uverbs_std_types_dmabuf.c
index 8d8c997a48834cfd6a8c4c6ba867cc996a2406eb..4de527af01d265effdc042e186693b52b0c1ccec 100644
--- a/drivers/infiniband/core/uverbs_std_types_dmabuf.c
+++ b/drivers/infiniband/core/uverbs_std_types_dmabuf.c
@@ -18,6 +18,9 @@ static int uverbs_dmabuf_attach(struct dma_buf *dmabuf,
 	if (!attachment->peer2peer)
 		return -EOPNOTSUPP;
 
+	if (!dma_buf_attach_revocable(attachment))
+		return -EOPNOTSUPP;
+
 	return 0;
 }
 
@@ -54,15 +57,6 @@ static void uverbs_dmabuf_unmap(struct dma_buf_attachment *attachment,
 	kref_put(&priv->kref, ib_uverbs_dmabuf_done);
 }
 
-static int uverbs_dmabuf_pin(struct dma_buf_attachment *attach)
-{
-	return -EOPNOTSUPP;
-}
-
-static void uverbs_dmabuf_unpin(struct dma_buf_attachment *attach)
-{
-}
-
 static void uverbs_dmabuf_release(struct dma_buf *dmabuf)
 {
 	struct ib_uverbs_dmabuf_file *priv = dmabuf->priv;
@@ -99,8 +93,6 @@ static const struct dma_buf_ops uverbs_dmabuf_ops = {
 	.attach = uverbs_dmabuf_attach,
 	.map_dma_buf = uverbs_dmabuf_map,
 	.unmap_dma_buf = uverbs_dmabuf_unmap,
-	.pin = uverbs_dmabuf_pin,
-	.unpin = uverbs_dmabuf_unpin,
 	.release = uverbs_dmabuf_release,
 };
 

-- 
2.49.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH rdma-next v2 2/3] RDMA/mlx5: Move dma-buf MR init out of ODP-only compilation
  2026-11-29 15:25 [PATCH rdma-next v2 0/3] RDMA/mlx5: Support pinned revocable dma-buf import Edward Srouji
  2026-11-29 15:25 ` [PATCH rdma-next v2 1/3] RDMA/uverbs: Permit dma-buf export to work with pinned importers Edward Srouji
@ 2026-11-29 15:25 ` Edward Srouji
  2026-11-29 15:25 ` [PATCH rdma-next v2 3/3] RDMA/mlx5: Try pinned revocable dmabuf import by default Edward Srouji
  2026-11-29 15:45 ` [PATCH rdma-next v2 0/3] RDMA/mlx5: Support pinned revocable dma-buf import sashiko-bot
  3 siblings, 0 replies; 6+ messages in thread
From: Edward Srouji @ 2026-11-29 15:25 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky, Sumit Semwal,
	Christian König, Philipp Stanner
  Cc: linux-rdma, linux-kernel, linux-media, dri-devel, linaro-mm-sig,
	Edward Srouji, Chiara Meiohas, Yishai Hadas

From: Chiara Meiohas <cmeiohas@nvidia.com>

dma-buf MR creation currently lives in odp.c, which is only built
under CONFIG_INFINIBAND_ON_DEMAND_PAGING, even though it has no actual
dependency on ODP. Move it into mr.c, which is always built.

This is a preparatory patch for the next commit, with no behavioral
change of its own.

Signed-off-by: Chiara Meiohas <cmeiohas@nvidia.com>
Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Signed-off-by: Edward Srouji <edwards@nvidia.com>
---
 drivers/infiniband/hw/mlx5/mlx5_ib.h |  7 ++---
 drivers/infiniband/hw/mlx5/mr.c      | 61 ++++++++++++++++++++++++++++++++++++
 drivers/infiniband/hw/mlx5/odp.c     | 59 ++--------------------------------
 3 files changed, 65 insertions(+), 62 deletions(-)

diff --git a/drivers/infiniband/hw/mlx5/mlx5_ib.h b/drivers/infiniband/hw/mlx5/mlx5_ib.h
index 462166c34d78ae7d3d93f6362e047ebf886e0ac3..2f0c0a108177d213f34d1772784667d15348aae5 100644
--- a/drivers/infiniband/hw/mlx5/mlx5_ib.h
+++ b/drivers/infiniband/hw/mlx5/mlx5_ib.h
@@ -1500,7 +1500,6 @@ int mlx5_ib_advise_mr_prefetch(struct ib_pd *pd,
 			       enum ib_uverbs_advise_mr_advice advice,
 			       u32 flags, struct ib_sge *sg_list, u32 num_sge);
 int mlx5_ib_init_odp_mr(struct mlx5_ib_mr *mr, struct ib_pd *pd);
-int mlx5_ib_init_dmabuf_mr(struct mlx5_ib_mr *mr, struct ib_pd *pd);
 #else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
 static inline int mlx5_ib_odp_init_one(struct mlx5_ib_dev *ibdev) { return 0; }
 static inline int mlx5r_odp_create_eq(struct mlx5_ib_dev *dev,
@@ -1532,12 +1531,10 @@ static inline int mlx5_ib_init_odp_mr(struct mlx5_ib_mr *mr, struct ib_pd *pd)
 {
 	return -EOPNOTSUPP;
 }
-static inline int mlx5_ib_init_dmabuf_mr(struct mlx5_ib_mr *mr, struct ib_pd *pd)
-{
-	return -EOPNOTSUPP;
-}
 #endif /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
 
+int mlx5_ib_dmabuf_mr_update_pas(struct mlx5_ib_mr *mr, u32 pdn, u32 flags);
+
 extern const struct mmu_interval_notifier_ops mlx5_mn_ops;
 
 /* Needed for rep profile */
diff --git a/drivers/infiniband/hw/mlx5/mr.c b/drivers/infiniband/hw/mlx5/mr.c
index 50c5f39c080f85f0cbefb886fb346e099ec07309..3c59cfbbbd19f2c2acf51848b94e8301b66811ae 100644
--- a/drivers/infiniband/hw/mlx5/mr.c
+++ b/drivers/infiniband/hw/mlx5/mr.c
@@ -916,6 +916,67 @@ static const struct dma_buf_attach_ops mlx5_ib_dmabuf_attach_ops = {
 	.invalidate_mappings = mlx5_ib_dmabuf_invalidate_cb,
 };
 
+int mlx5_ib_dmabuf_mr_update_pas(struct mlx5_ib_mr *mr, u32 pdn, u32 flags)
+{
+	struct ib_umem_dmabuf *umem_dmabuf = to_ib_umem_dmabuf(mr->umem);
+	int access_mode = mr->data_direct ? MLX5_MKC_ACCESS_MODE_KSM :
+					    MLX5_MKC_ACCESS_MODE_MTT;
+	unsigned int old_page_shift = mr->page_shift;
+	unsigned int page_shift;
+	unsigned long page_size;
+	int err;
+
+	dma_resv_lock(umem_dmabuf->attach->dmabuf->resv, NULL);
+	err = ib_umem_dmabuf_map_pages(umem_dmabuf);
+	if (err) {
+		dma_resv_unlock(umem_dmabuf->attach->dmabuf->resv);
+		return err;
+	}
+
+	page_size = mlx5_umem_dmabuf_find_best_pgsz(mr_to_mdev(mr), umem_dmabuf,
+						    access_mode);
+	if (!page_size) {
+		ib_umem_dmabuf_unmap_pages(umem_dmabuf);
+		err = -EINVAL;
+	} else {
+		page_shift = order_base_2(page_size);
+		if (page_shift != mr->page_shift && mr->dmabuf_faulted) {
+			err = mlx5r_umr_dmabuf_update_pgsz(mr, flags, pdn,
+							   page_shift);
+		} else {
+			mr->page_shift = page_shift;
+			if (mr->data_direct)
+				err = mlx5r_umr_update_data_direct_ksm_pas(
+					mr, flags);
+			else
+				err = mlx5r_umr_update_mr_pas(mr, flags,
+							      pdn);
+		}
+	}
+	dma_resv_unlock(umem_dmabuf->attach->dmabuf->resv);
+
+	if (err) {
+		mr->page_shift = old_page_shift;
+		return err;
+	}
+
+	mr->dmabuf_faulted = 1;
+	return 0;
+}
+
+static int mlx5_ib_init_dmabuf_mr(struct mlx5_ib_mr *mr, struct ib_pd *pd)
+{
+	struct mlx5_ib_dev *dev = mr_to_dev(mr);
+	u32 pdn;
+
+	if (mr->data_direct)
+		pdn = dev->mdev->data_direct->pdn;
+	else
+		pdn = to_mpd(pd)->pdn;
+
+	return mlx5_ib_dmabuf_mr_update_pas(mr, pdn, MLX5_IB_UPD_XLT_ENABLE);
+}
+
 static struct ib_mr *
 reg_user_mr_dmabuf(struct ib_pd *pd, struct device *dma_device,
 		   u64 offset, u64 length, u64 virt_addr,
diff --git a/drivers/infiniband/hw/mlx5/odp.c b/drivers/infiniband/hw/mlx5/odp.c
index e4978feccbe47e000d6c082f9a4ed2e9e43d663c..dee2cb9e469a23c5d2ae4753e7557ed0dcb063b9 100644
--- a/drivers/infiniband/hw/mlx5/odp.c
+++ b/drivers/infiniband/hw/mlx5/odp.c
@@ -844,53 +844,15 @@ static int pagefault_implicit_mr(struct mlx5_ib_mr *imr,
 static int pagefault_dmabuf_mr(struct mlx5_ib_mr *mr, size_t bcnt,
 			       u32 *bytes_mapped, u32 flags, u32 pdn)
 {
-	struct ib_umem_dmabuf *umem_dmabuf = to_ib_umem_dmabuf(mr->umem);
-	int access_mode = mr->data_direct ? MLX5_MKC_ACCESS_MODE_KSM :
-					    MLX5_MKC_ACCESS_MODE_MTT;
-	unsigned int old_page_shift = mr->page_shift;
-	unsigned int page_shift;
-	unsigned long page_size;
 	u32 xlt_flags = 0;
 	int err;
 
 	if (flags & MLX5_PF_FLAGS_ENABLE)
 		xlt_flags |= MLX5_IB_UPD_XLT_ENABLE;
 
-	dma_resv_lock(umem_dmabuf->attach->dmabuf->resv, NULL);
-	err = ib_umem_dmabuf_map_pages(umem_dmabuf);
-	if (err) {
-		dma_resv_unlock(umem_dmabuf->attach->dmabuf->resv);
-		return err;
-	}
-
-	page_size = mlx5_umem_dmabuf_find_best_pgsz(mr_to_mdev(mr), umem_dmabuf,
-						    access_mode);
-	if (!page_size) {
-		ib_umem_dmabuf_unmap_pages(umem_dmabuf);
-		err = -EINVAL;
-	} else {
-		page_shift = order_base_2(page_size);
-		if (page_shift != mr->page_shift && mr->dmabuf_faulted) {
-			err = mlx5r_umr_dmabuf_update_pgsz(mr, xlt_flags, pdn,
-							   page_shift);
-		} else {
-			mr->page_shift = page_shift;
-			if (mr->data_direct)
-				err = mlx5r_umr_update_data_direct_ksm_pas(
-					mr, xlt_flags);
-			else
-				err = mlx5r_umr_update_mr_pas(mr, xlt_flags,
-							      pdn);
-		}
-	}
-	dma_resv_unlock(umem_dmabuf->attach->dmabuf->resv);
-
-	if (err) {
-		mr->page_shift = old_page_shift;
+	err = mlx5_ib_dmabuf_mr_update_pas(mr, pdn, xlt_flags);
+	if (err)
 		return err;
-	}
-
-	mr->dmabuf_faulted = 1;
 
 	if (bytes_mapped)
 		*bytes_mapped += bcnt;
@@ -952,23 +914,6 @@ int mlx5_ib_init_odp_mr(struct mlx5_ib_mr *mr, struct ib_pd *pd)
 	return ret >= 0 ? 0 : ret;
 }
 
-int mlx5_ib_init_dmabuf_mr(struct mlx5_ib_mr *mr, struct ib_pd *pd)
-{
-	struct mlx5_ib_dev *dev = mr_to_dev(mr);
-	u32 pdn;
-	int ret;
-
-	if (mr->data_direct)
-		pdn = dev->mdev->data_direct->pdn;
-	else
-		pdn = to_mpd(pd)->pdn;
-
-	ret = pagefault_dmabuf_mr(mr, mr->umem->length, NULL,
-				  MLX5_PF_FLAGS_ENABLE, pdn);
-
-	return ret >= 0 ? 0 : ret;
-}
-
 struct pf_frame {
 	struct pf_frame *next;
 	u32 key;

-- 
2.49.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH rdma-next v2 3/3] RDMA/mlx5: Try pinned revocable dmabuf import by default
  2026-11-29 15:25 [PATCH rdma-next v2 0/3] RDMA/mlx5: Support pinned revocable dma-buf import Edward Srouji
  2026-11-29 15:25 ` [PATCH rdma-next v2 1/3] RDMA/uverbs: Permit dma-buf export to work with pinned importers Edward Srouji
  2026-11-29 15:25 ` [PATCH rdma-next v2 2/3] RDMA/mlx5: Move dma-buf MR init out of ODP-only compilation Edward Srouji
@ 2026-11-29 15:25 ` Edward Srouji
  2026-11-29 15:45   ` sashiko-bot
  2026-11-29 15:45 ` [PATCH rdma-next v2 0/3] RDMA/mlx5: Support pinned revocable dma-buf import sashiko-bot
  3 siblings, 1 reply; 6+ messages in thread
From: Edward Srouji @ 2026-11-29 15:25 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky, Sumit Semwal,
	Christian König, Philipp Stanner
  Cc: linux-rdma, linux-kernel, linux-media, dri-devel, linaro-mm-sig,
	Edward Srouji, Yishai Hadas, Chiara Meiohas

From: Yishai Hadas <yishaih@nvidia.com>

mlx5 imported dma-bufs assuming they needed to support being moved.
To support this, mkeys were sized for the worst case: dma-buf size /
PAGE_SIZE. Many exporters do not require movable support, making this
overkill, such as when applications register GPU memory as a dma-buf
for peer-to-peer DMA.

Try a pinned, revocable attach first instead. When the exporter
grants it, the buffer is guaranteed not to move, so the mkey can be
sized exactly. Otherwise, fall back to today's existing behavior,
unchanged. Only that fallback requires CONFIG_INFINIBAND_ON_DEMAND_PAGING.

A pinned revocable import is revoked permanently when the exporter
invalidates the buffer, e.g. VFIO on a memory-disable or a reset. Unlike
a movable import, the MR is not restored when the exporter restores
access, and userspace has to register it again.

On a revoke the mkey is disabled with a UMR, so the device stops
accessing the buffer. A revoke cannot run while the MR is being
registered, as the reservation lock is held until the mkey is enabled.

This follows the same approach irdma already uses for pinned
revocable dmabuf import, commit 4707bf5f6c86 ("RDMA/irdma: Add
support for revocable pinned dmabuf import")

Co-developed-by: Chiara Meiohas <cmeiohas@nvidia.com>
Signed-off-by: Chiara Meiohas <cmeiohas@nvidia.com>
Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Signed-off-by: Edward Srouji <edwards@nvidia.com>
---
 drivers/infiniband/hw/mlx5/mr.c | 143 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 126 insertions(+), 17 deletions(-)

diff --git a/drivers/infiniband/hw/mlx5/mr.c b/drivers/infiniband/hw/mlx5/mr.c
index 3c59cfbbbd19f2c2acf51848b94e8301b66811ae..0ba82356963dd55646caaa46af4695ff87672144 100644
--- a/drivers/infiniband/hw/mlx5/mr.c
+++ b/drivers/infiniband/hw/mlx5/mr.c
@@ -443,7 +443,7 @@ static struct mlx5_ib_mr *alloc_cacheable_mr(struct ib_pd *pd,
 	struct mlx5_ib_mr *mr;
 	unsigned long page_size;
 
-	if (umem->is_dmabuf)
+	if (umem->is_dmabuf && !to_ib_umem_dmabuf(umem)->pinned)
 		page_size = mlx5_umem_dmabuf_default_pgsz(umem, iova);
 	else
 		page_size = mlx5_umem_mkc_find_best_pgsz(pd_to_mdev(pd), umem,
@@ -916,7 +916,8 @@ static const struct dma_buf_attach_ops mlx5_ib_dmabuf_attach_ops = {
 	.invalidate_mappings = mlx5_ib_dmabuf_invalidate_cb,
 };
 
-int mlx5_ib_dmabuf_mr_update_pas(struct mlx5_ib_mr *mr, u32 pdn, u32 flags)
+static int mlx5_ib_dmabuf_mr_update_pas_locked(struct mlx5_ib_mr *mr, u32 pdn,
+					       u32 flags)
 {
 	struct ib_umem_dmabuf *umem_dmabuf = to_ib_umem_dmabuf(mr->umem);
 	int access_mode = mr->data_direct ? MLX5_MKC_ACCESS_MODE_KSM :
@@ -926,12 +927,11 @@ int mlx5_ib_dmabuf_mr_update_pas(struct mlx5_ib_mr *mr, u32 pdn, u32 flags)
 	unsigned long page_size;
 	int err;
 
-	dma_resv_lock(umem_dmabuf->attach->dmabuf->resv, NULL);
+	dma_resv_assert_held(umem_dmabuf->attach->dmabuf->resv);
+
 	err = ib_umem_dmabuf_map_pages(umem_dmabuf);
-	if (err) {
-		dma_resv_unlock(umem_dmabuf->attach->dmabuf->resv);
+	if (err)
 		return err;
-	}
 
 	page_size = mlx5_umem_dmabuf_find_best_pgsz(mr_to_mdev(mr), umem_dmabuf,
 						    access_mode);
@@ -953,7 +953,6 @@ int mlx5_ib_dmabuf_mr_update_pas(struct mlx5_ib_mr *mr, u32 pdn, u32 flags)
 							      pdn);
 		}
 	}
-	dma_resv_unlock(umem_dmabuf->attach->dmabuf->resv);
 
 	if (err) {
 		mr->page_shift = old_page_shift;
@@ -964,7 +963,19 @@ int mlx5_ib_dmabuf_mr_update_pas(struct mlx5_ib_mr *mr, u32 pdn, u32 flags)
 	return 0;
 }
 
-static int mlx5_ib_init_dmabuf_mr(struct mlx5_ib_mr *mr, struct ib_pd *pd)
+int mlx5_ib_dmabuf_mr_update_pas(struct mlx5_ib_mr *mr, u32 pdn, u32 flags)
+{
+	struct dma_resv *resv = to_ib_umem_dmabuf(mr->umem)->attach->dmabuf->resv;
+	int err;
+
+	dma_resv_lock(resv, NULL);
+	err = mlx5_ib_dmabuf_mr_update_pas_locked(mr, pdn, flags);
+	dma_resv_unlock(resv);
+	return err;
+}
+
+static int mlx5_ib_init_dmabuf_mr_locked(struct mlx5_ib_mr *mr,
+					 struct ib_pd *pd)
 {
 	struct mlx5_ib_dev *dev = mr_to_dev(mr);
 	u32 pdn;
@@ -974,7 +985,48 @@ static int mlx5_ib_init_dmabuf_mr(struct mlx5_ib_mr *mr, struct ib_pd *pd)
 	else
 		pdn = to_mpd(pd)->pdn;
 
-	return mlx5_ib_dmabuf_mr_update_pas(mr, pdn, MLX5_IB_UPD_XLT_ENABLE);
+	return mlx5_ib_dmabuf_mr_update_pas_locked(mr, pdn,
+						   MLX5_IB_UPD_XLT_ENABLE);
+}
+
+static int mlx5_ib_init_dmabuf_mr(struct mlx5_ib_mr *mr, struct ib_pd *pd)
+{
+	struct dma_resv *resv = to_ib_umem_dmabuf(mr->umem)->attach->dmabuf->resv;
+	int err;
+
+	dma_resv_lock(resv, NULL);
+	err = mlx5_ib_init_dmabuf_mr_locked(mr, pd);
+	dma_resv_unlock(resv);
+	return err;
+}
+
+/*
+ * Revoke callback of a pinned revocable dmabuf MR. It runs under the dmabuf
+ * reservation lock, either on an exporter invalidation or from
+ * ib_umem_release() at deregistration. It returns void, and the caller
+ * unconditionally unmaps and unpins the buffer once it returns, so it
+ * disables the mkey with a UMR to stop the device from accessing it.
+ */
+static void mlx5_ib_dmabuf_pinned_revoke(void *priv)
+{
+	struct mlx5_ib_mr *mr = priv;
+	struct mlx5_ib_dev *dev = mr_to_dev(mr);
+	int err;
+
+	dma_resv_assert_held(to_ib_umem_dmabuf(mr->umem)->attach->dmabuf->resv);
+
+	err = mlx5r_umr_revoke_mr(mr);
+	if (err) {
+		mlx5_ib_warn(dev, "dmabuf mr revoke failed to disable mkey 0x%x, err %d\n",
+			     mr->mmkey.key, err);
+		return;
+	}
+
+	/*
+	 * Mark the key as disabled. If the UMR failed, deregistration destroys
+	 * the key instead of returning it to the pool.
+	 */
+	mr->revoked = true;
 }
 
 static struct ib_mr *
@@ -987,6 +1039,7 @@ reg_user_mr_dmabuf(struct ib_pd *pd, struct device *dma_device,
 	struct mlx5_ib_dev *dev = to_dev(pd->device);
 	struct mlx5_ib_mr *mr = NULL;
 	struct ib_umem_dmabuf *umem_dmabuf;
+	bool revocable_and_pinned = false;
 	u16 st_index = MLX5_MKC_PCIE_TPH_NO_STEERING_TAG_INDEX;
 	u8 ph = MLX5_IB_NO_PH;
 	int err;
@@ -995,12 +1048,23 @@ reg_user_mr_dmabuf(struct ib_pd *pd, struct device *dma_device,
 	if (err)
 		return ERR_PTR(err);
 
-	if (!pinned_mode)
-		umem_dmabuf = ib_umem_dmabuf_get(&dev->ib_dev,
-						 offset, length, fd,
-						 access_flags,
-						 &mlx5_ib_dmabuf_attach_ops);
-	else if (dma_device)
+	if (!pinned_mode) {
+		umem_dmabuf = ib_umem_dmabuf_get_pinned_revocable_and_lock(
+				&dev->ib_dev, offset, length, fd, access_flags);
+		if (!IS_ERR(umem_dmabuf)) {
+			revocable_and_pinned = true;
+		} else {
+			if (!IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
+				mlx5_ib_dbg(dev,
+					   "dmabuf pin failed (%pe) and ODP is not supported\n",
+					   umem_dmabuf);
+				return ERR_CAST(umem_dmabuf);
+			}
+			umem_dmabuf = ib_umem_dmabuf_get(
+				&dev->ib_dev, offset, length, fd,
+				access_flags, &mlx5_ib_dmabuf_attach_ops);
+		}
+	} else if (dma_device)
 		umem_dmabuf = ib_umem_dmabuf_get_pinned_with_dma_device(&dev->ib_dev,
 				dma_device, offset, length,
 				fd, access_flags);
@@ -1025,6 +1089,8 @@ reg_user_mr_dmabuf(struct ib_pd *pd, struct device *dma_device,
 				access_flags, access_mode,
 				st_index, ph);
 	if (IS_ERR(mr)) {
+		if (revocable_and_pinned)
+			ib_umem_dmabuf_revoke_unlock(umem_dmabuf);
 		ib_umem_release(&umem_dmabuf->umem);
 		return ERR_CAST(mr);
 	}
@@ -1034,6 +1100,23 @@ reg_user_mr_dmabuf(struct ib_pd *pd, struct device *dma_device,
 	atomic_add(ib_umem_num_pages(mr->umem),
 		   &mr_to_mdev(mr)->priv.reg_pages);
 	umem_dmabuf->private = mr;
+	if (revocable_and_pinned) {
+		/*
+		 * Keep the reservation lock until the key is enabled, so an
+		 * exporter revoke cannot run on a half registered MR. A revoke
+		 * that is pending runs once we unlock, on a complete MR.
+		 */
+		err = mlx5_ib_init_dmabuf_mr_locked(mr, pd);
+		if (!err)
+			ib_umem_dmabuf_set_revoke_locked(umem_dmabuf,
+							 mlx5_ib_dmabuf_pinned_revoke,
+							 mr);
+		ib_umem_dmabuf_revoke_unlock(umem_dmabuf);
+		if (err)
+			goto err_dereg_mr;
+		return &mr->ibmr;
+	}
+
 	if (!pinned_mode) {
 		err = mlx5r_odp_create_eq(dev, &dev->odp_pf_eq);
 		if (err)
@@ -1120,8 +1203,7 @@ struct ib_mr *mlx5_ib_reg_user_mr_dmabuf(struct ib_pd *pd, u64 offset,
 	int mlx5_access_flags = 0;
 	int err;
 
-	if (!IS_ENABLED(CONFIG_INFINIBAND_USER_MEM) ||
-	    !IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING))
+	if (!IS_ENABLED(CONFIG_INFINIBAND_USER_MEM))
 		return ERR_PTR(-EOPNOTSUPP);
 
 	if (uverbs_attr_is_valid(attrs, MLX5_IB_ATTR_REG_DMABUF_MR_ACCESS_FLAGS)) {
@@ -1476,6 +1558,12 @@ static int mlx5_umr_revoke_mr_with_lock(struct mlx5_ib_mr *mr)
 	return ret;
 }
 
+static bool is_pinned_revoke_dmabuf_mr(struct mlx5_ib_mr *mr)
+{
+	return mr->umem && mr->umem->is_dmabuf &&
+	       to_ib_umem_dmabuf(mr->umem)->pinned_revoke;
+}
+
 static int mlx5r_handle_mkey_cleanup(struct mlx5_ib_mr *mr)
 {
 	bool is_odp_dma_buf = is_dmabuf_mr(mr) &&
@@ -1484,6 +1572,23 @@ static int mlx5r_handle_mkey_cleanup(struct mlx5_ib_mr *mr)
 	bool is_odp = is_odp_mr(mr);
 	int ret;
 
+	if (is_pinned_revoke_dmabuf_mr(mr)) {
+		/*
+		 * Run the revoke callback if the exporter did not. A key revoked
+		 * by a UMR returns to the pool once the umem is released,
+		 * otherwise destroy it here. ib_umem_dmabuf_revoke() is
+		 * idempotent, so the release later does nothing more.
+		 */
+		ib_umem_dmabuf_revoke(to_ib_umem_dmabuf(mr->umem));
+		if (mr->revoked && mr->ibmr.frmr.pool)
+			return 0;
+
+		ret = destroy_mkey(dev, mr);
+		if (!ret && mr->ibmr.frmr.pool)
+			ib_frmr_pool_drop(&mr->ibmr);
+		return ret;
+	}
+
 	if (mr->ibmr.frmr.pool) {
 		if (!mlx5_umr_revoke_mr_with_lock(mr)) {
 			ib_frmr_pool_push(mr->ibmr.device, &mr->ibmr);
@@ -1569,6 +1674,7 @@ static int __mlx5_ib_dereg_mr(struct ib_mr *ibmr)
 
 	if (mr->umem) {
 		bool is_odp = is_odp_mr(mr);
+		bool is_pinned_revoke = is_pinned_revoke_dmabuf_mr(mr);
 
 		if (!is_odp)
 			atomic_sub(ib_umem_num_pages(mr->umem),
@@ -1576,6 +1682,9 @@ static int __mlx5_ib_dereg_mr(struct ib_mr *ibmr)
 		ib_umem_release(mr->umem);
 		if (is_odp)
 			mlx5_ib_free_odp_mr(mr);
+		/* Push after the release, mr->umem is not touched again */
+		if (is_pinned_revoke && mr->revoked && mr->ibmr.frmr.pool)
+			ib_frmr_pool_push(mr->ibmr.device, &mr->ibmr);
 	}
 
 	if (!mr->ibmr.frmr.pool)

-- 
2.49.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH rdma-next v2 3/3] RDMA/mlx5: Try pinned revocable dmabuf import by default
  2026-11-29 15:25 ` [PATCH rdma-next v2 3/3] RDMA/mlx5: Try pinned revocable dmabuf import by default Edward Srouji
@ 2026-11-29 15:45   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-29 15:45 UTC (permalink / raw)
  To: Edward Srouji; +Cc: linux-rdma, dri-devel, media-ci

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] RDMA/mlx5: user-triggerable WARN_ON(!page_size) in
  alloc_cacheable_mr()
--

commit 3a4c35010e9f8b10e62196ece5aade6e6065212c
Author: Yishai Hadas <yishaih@nvidia.com>

RDMA/mlx5: Try pinned revocable dmabuf import by default

This commit attempts a pinned, revocable dma-buf attachment by default
when registering a dma-buf MR so that the mkey can be sized exactly,
falling back to a movable ODP import only when the exporter does not
grant it.

> diff --git a/drivers/infiniband/hw/mlx5/mr.c b/drivers/infiniband/hw/mlx5/mr.c
> index 3c59cfbbbd19f..0ba82356963dd 100644
> --- a/drivers/infiniband/hw/mlx5/mr.c
> +++ b/drivers/infiniband/hw/mlx5/mr.c
> @@ -443,7 +443,7 @@ static struct mlx5_ib_mr *alloc_cacheable_mr(struct ib_pd *pd,
>  	struct mlx5_ib_mr *mr;
>  	unsigned long page_size;
>  
> -	if (umem->is_dmabuf)
> +	if (umem->is_dmabuf && !to_ib_umem_dmabuf(umem)->pinned)
>  		page_size = mlx5_umem_dmabuf_default_pgsz(umem, iova);
>  	else
>  		page_size = mlx5_umem_mkc_find_best_pgsz(pd_to_mdev(pd), umem,

[Severity: Medium]
Can userspace trigger WARN_ON(!page_size) right below this when
registering a pinned dma-buf MR?

Previously all dma-buf MRs took the mlx5_umem_dmabuf_default_pgsz()
branch here and checked mlx5_umem_dmabuf_find_best_pgsz() later in
mlx5_ib_dmabuf_mr_update_pas(), which returns -EINVAL without a warning
when page_size is 0.  Now pinned dma-buf MRs call
mlx5_umem_mkc_find_best_pgsz() -> ib_umem_find_best_pgsz() in
alloc_cacheable_mr().  Because UVERBS_METHOD_REG_DMABUF_MR only checks
that offset and iova share the same sub-page alignment, a user-supplied
iova where iova + umem->length - 1 overflows u64 causes
ib_umem_find_best_pgsz() to return 0 and hit WARN_ON(!page_size).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261129-mlx5-pinned-revocable-dmabuf-v2-0-830a412a3369@nvidia.com?part=3

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH rdma-next v2 0/3] RDMA/mlx5: Support pinned revocable dma-buf import
  2026-11-29 15:25 [PATCH rdma-next v2 0/3] RDMA/mlx5: Support pinned revocable dma-buf import Edward Srouji
                   ` (2 preceding siblings ...)
  2026-11-29 15:25 ` [PATCH rdma-next v2 3/3] RDMA/mlx5: Try pinned revocable dmabuf import by default Edward Srouji
@ 2026-11-29 15:45 ` sashiko-bot
  3 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-29 15:45 UTC (permalink / raw)
  To: Edward Srouji; +Cc: linux-rdma

Sashiko has finished reviewing this 3-patch series:

- Patches 1 and 2: no issues found.
- Patch 3 ("RDMA/mlx5: Try pinned revocable dmabuf import by def..."): 1
  potential issue (1 Medium)

A detailed review has been sent as a separate reply to patch 3.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261129-mlx5-pinned-revocable-dmabuf-v2-0-830a412a3369@nvidia.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-29 15:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 15:25 [PATCH rdma-next v2 0/3] RDMA/mlx5: Support pinned revocable dma-buf import Edward Srouji
2026-11-29 15:25 ` [PATCH rdma-next v2 1/3] RDMA/uverbs: Permit dma-buf export to work with pinned importers Edward Srouji
2026-11-29 15:25 ` [PATCH rdma-next v2 2/3] RDMA/mlx5: Move dma-buf MR init out of ODP-only compilation Edward Srouji
2026-11-29 15:25 ` [PATCH rdma-next v2 3/3] RDMA/mlx5: Try pinned revocable dmabuf import by default Edward Srouji
2026-11-29 15:45   ` sashiko-bot
2026-11-29 15:45 ` [PATCH rdma-next v2 0/3] RDMA/mlx5: Support pinned revocable dma-buf import sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox