* [PATCH rdma-next 0/3] RDMA/mlx5: Support pinned revocable dma-buf import
@ 2026-11-28 13:36 Edward Srouji
2026-11-28 13:36 ` [PATCH rdma-next 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-28 13:36 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>
---
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 | 187 ++++++++++++++++++++--
drivers/infiniband/hw/mlx5/odp.c | 58 +------
4 files changed, 185 insertions(+), 81 deletions(-)
---
base-commit: 94e81a1f3ffabb388f7d0a9674bb1d3acc067e00
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 1/3] RDMA/uverbs: Permit dma-buf export to work with pinned importers
2026-11-28 13:36 [PATCH rdma-next 0/3] RDMA/mlx5: Support pinned revocable dma-buf import Edward Srouji
@ 2026-11-28 13:36 ` Edward Srouji
2026-11-28 13:36 ` [PATCH rdma-next 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-28 13:36 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 2/3] RDMA/mlx5: Move dma-buf MR init out of ODP-only compilation
2026-11-28 13:36 [PATCH rdma-next 0/3] RDMA/mlx5: Support pinned revocable dma-buf import Edward Srouji
2026-11-28 13:36 ` [PATCH rdma-next 1/3] RDMA/uverbs: Permit dma-buf export to work with pinned importers Edward Srouji
@ 2026-11-28 13:36 ` Edward Srouji
2026-11-28 13:36 ` [PATCH rdma-next 3/3] RDMA/mlx5: Try pinned revocable dmabuf import by default Edward Srouji
2026-11-28 13:59 ` [PATCH rdma-next 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-28 13:36 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 | 60 ++++++++++++++++++++++++++++++++++++
drivers/infiniband/hw/mlx5/odp.c | 58 ++--------------------------------
3 files changed, 64 insertions(+), 61 deletions(-)
diff --git a/drivers/infiniband/hw/mlx5/mlx5_ib.h b/drivers/infiniband/hw/mlx5/mlx5_ib.h
index b2d4db344071bcc068a767f94ffb64b80df9499d..3b3ba6cb21fdd34250a7ba4f1d11cc06d5b59fbd 100644
--- a/drivers/infiniband/hw/mlx5/mlx5_ib.h
+++ b/drivers/infiniband/hw/mlx5/mlx5_ib.h
@@ -1415,7 +1415,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,
@@ -1447,12 +1446,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 7952d80349dc6c086758472da762f320ca8e5df8..4bd911e0596c676a8caa4165fca139db6852cbab 100644
--- a/drivers/infiniband/hw/mlx5/mr.c
+++ b/drivers/infiniband/hw/mlx5/mr.c
@@ -894,6 +894,66 @@ 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(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_mdev(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 b2643ff871a36a190293af2f6c4571d243818670..81c86f6145d0f509fa9964bf83f007e035cc86c6 100644
--- a/drivers/infiniband/hw/mlx5/odp.c
+++ b/drivers/infiniband/hw/mlx5/odp.c
@@ -840,52 +840,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(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;
@@ -947,23 +910,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_mdev(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 3/3] RDMA/mlx5: Try pinned revocable dmabuf import by default
2026-11-28 13:36 [PATCH rdma-next 0/3] RDMA/mlx5: Support pinned revocable dma-buf import Edward Srouji
2026-11-28 13:36 ` [PATCH rdma-next 1/3] RDMA/uverbs: Permit dma-buf export to work with pinned importers Edward Srouji
2026-11-28 13:36 ` [PATCH rdma-next 2/3] RDMA/mlx5: Move dma-buf MR init out of ODP-only compilation Edward Srouji
@ 2026-11-28 13:36 ` Edward Srouji
2026-11-28 13:59 ` sashiko-bot
2026-11-28 13:59 ` [PATCH rdma-next 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-28 13:36 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 4bd911e0596c676a8caa4165fca139db6852cbab..55e6d8656692e4d8a05f43bf2c5f5effa9d53c5f 100644
--- a/drivers/infiniband/hw/mlx5/mr.c
+++ b/drivers/infiniband/hw/mlx5/mr.c
@@ -430,7 +430,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(dev, umem, iova,
@@ -894,7 +894,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 :
@@ -904,12 +905,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(umem_dmabuf, access_mode);
if (!page_size) {
@@ -930,7 +930,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;
@@ -941,7 +940,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_mdev(mr);
u32 pdn;
@@ -951,7 +962,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_mdev(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 *
@@ -964,6 +1016,7 @@ reg_user_mr_dmabuf(struct ib_pd *pd, struct device *dma_device,
struct mlx5_ib_dev *dev = to_mdev(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;
@@ -972,12 +1025,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);
@@ -1002,6 +1066,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);
}
@@ -1010,6 +1076,23 @@ reg_user_mr_dmabuf(struct ib_pd *pd, struct device *dma_device,
atomic_add(ib_umem_num_pages(mr->umem), &dev->mdev->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)
@@ -1096,8 +1179,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)) {
@@ -1453,6 +1535,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) &&
@@ -1461,6 +1549,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);
@@ -1545,6 +1650,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),
@@ -1552,6 +1658,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 3/3] RDMA/mlx5: Try pinned revocable dmabuf import by default
2026-11-28 13:36 ` [PATCH rdma-next 3/3] RDMA/mlx5: Try pinned revocable dmabuf import by default Edward Srouji
@ 2026-11-28 13:59 ` sashiko-bot
0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-28 13:59 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 d5740621fb40d604a240c4d889b540a19d43d88d
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 dma-buf MRs so the mkey can be sized exactly, falling
back to movable ODP dma-buf import when the exporter does not support it.
> diff --git a/drivers/infiniband/hw/mlx5/mr.c b/drivers/infiniband/hw/mlx5/mr.c
> index 4bd911e0596c6..55e6d8656692e 100644
> --- a/drivers/infiniband/hw/mlx5/mr.c
> +++ b/drivers/infiniband/hw/mlx5/mr.c
> @@ -430,7 +430,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(dev, umem, iova,
[Severity: Medium]
Can an unprivileged user trigger the WARN_ON(!page_size) right below this
in alloc_cacheable_mr() when registering a pinned dmabuf MR?
UVERBS_METHOD_REG_DMABUF_MR only checks that offset and iova share the
same sub-page alignment, and ib_umem_dmabuf_get_pinned_revocable_and_lock()
validates offset + size against the dmabuf size without checking iova.
Previously, dmabuf MRs used mlx5_umem_dmabuf_default_pgsz() here and
checked mlx5_umem_dmabuf_find_best_pgsz() later in
mlx5_ib_dmabuf_mr_update_pas(), which returns -EINVAL without warning when
page_size is 0.
With pinned dmabuf MRs now calling mlx5_umem_mkc_find_best_pgsz() ->
ib_umem_find_best_pgsz() in alloc_cacheable_mr(), a user-supplied iova
where iova + length - 1 overflows u64 causes ib_umem_find_best_pgsz() to
return 0 and hit WARN_ON(!page_size) (likewise if KSM alignment constraints
clear the page-size bitmap).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261128-mlx5-pinned-revocable-dmabuf-v1-0-a4cde143a58e@nvidia.com?part=3
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH rdma-next 0/3] RDMA/mlx5: Support pinned revocable dma-buf import
2026-11-28 13:36 [PATCH rdma-next 0/3] RDMA/mlx5: Support pinned revocable dma-buf import Edward Srouji
` (2 preceding siblings ...)
2026-11-28 13:36 ` [PATCH rdma-next 3/3] RDMA/mlx5: Try pinned revocable dmabuf import by default Edward Srouji
@ 2026-11-28 13:59 ` sashiko-bot
3 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-28 13:59 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/20261128-mlx5-pinned-revocable-dmabuf-v1-0-a4cde143a58e@nvidia.com
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-28 13:59 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 13:36 [PATCH rdma-next 0/3] RDMA/mlx5: Support pinned revocable dma-buf import Edward Srouji
2026-11-28 13:36 ` [PATCH rdma-next 1/3] RDMA/uverbs: Permit dma-buf export to work with pinned importers Edward Srouji
2026-11-28 13:36 ` [PATCH rdma-next 2/3] RDMA/mlx5: Move dma-buf MR init out of ODP-only compilation Edward Srouji
2026-11-28 13:36 ` [PATCH rdma-next 3/3] RDMA/mlx5: Try pinned revocable dmabuf import by default Edward Srouji
2026-11-28 13:59 ` sashiko-bot
2026-11-28 13:59 ` [PATCH rdma-next 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