Fix races around IB_MR_REREG_PD and mr->pd
Subsystems (2)
Jason Gunthorpe
maintainercover Fix races around IB_MR_REREG_PD and mr->pd
Sashiko pointed out an existing bug related to mr->pd: when IB_MR_REREG_PD is used the mr->pd is changed while only holding the write side of the MR's uobject lock.
Effectively, because IB_MR_REREG_PD is usually implemented by changing the MR in-place, the mr->pd becomes unreadable outside an MR-specific system call that holds the uobject lock. All the readers in this series could race with an IB_MR_REREG_PD and potentially UAF the mr->pd.
https://sashiko.dev/#/patchset/20260427-security-bug-fixes-v3-0-4621fa52de0e%40nvidia.com?part=4
This was presented as a simple 'oh look it can race with nldev' which is correct. However, asking AI to fully audit mr->pd touches also revealed a much more convoluted issue inside mlx5 ODP that is also using mr->pd from the page fault work queue, advise mr work queue and advise mr system call without any locking.
It turns out this mlx5 problem is entirely unnecessary since outside implicit mr there are only three cases where the UMR actually flags the PDN to be read by HW, umr_rereg_pas(), mlx5_ib_init_odp_mr() and mlx5_ib_init_dmabuf_mr(). umr_rereg_pas() is particularly tricky because it illegaly updates mr->pd inside the driver. Reorganize the giant call chain from mlx5r_umr_set_update_xlt_mkey_seg() upward so that pdn is passed down from those three functions instead of unconditionally picked out at the bottom.
nldev however is trickier to fix. To avoid disurbing the happy paths build a synchronize barrier by removing the mr from the xarray and then putting it right back. The kref completion acts as a positive signal that the mr->pd is no longer being used.
Jason Gunthorpe (10):
IB/mlx5: Don't take the rereg_mr fallback without a new translation RDMA/mlx5: Create ODP EQ for non-pinned dmabuf MRs IB/mlx5: Properly support implicit ODP rereg_mr RDMA/nldev: Fix locking when accessing mr->pd
IB/mlx5: Remove unused mkc bits in mlx5r_umr_update_mr_page_shift() IB/mlx5: Pull the pdn out of the depths of the umr machinery IB/mlx5: Don't mangle the mr->pd inside the rereg callback IB/mlx5: Push pdn above mlx5r_umr_update_xlt()
IB/mlx5: Push pdn above pagfault_real_mr()
IB/mlx5: Push pdn above pagefault_dmabuf_mr()
drivers/infiniband/core/nldev.c | 15 +++-- drivers/infiniband/core/restrack.c | 49 +++++++++++++++ drivers/infiniband/core/restrack.h | 1 + drivers/infiniband/core/uverbs_cmd.c | 10 ++- drivers/infiniband/hw/mlx5/mlx5_ib.h | 12 ++-- drivers/infiniband/hw/mlx5/mr.c | 37 ++++++++--- drivers/infiniband/hw/mlx5/odp.c | 82 +++++++++++++++---------- drivers/infiniband/hw/mlx5/umr.c | 92 +++++++++++++--------------- drivers/infiniband/hw/mlx5/umr.h | 11 ++-- include/rdma/ib_verbs.h | 5 ++ 10 files changed, 203 insertions(+), 111 deletions(-)
base-commit: e43ffb69e0438cddd72aaa30898b4dc446f664f8
Signature · 1 line
2.43.0
Jason Gunthorpe
maintainerPATCH 1/10 IB/mlx5: Don't take the rereg_mr fallback without a new translation
Jumping to mlx5_ib_reg_user_mr() without IB_MR_REREG_TRANS set will use garbage values for start, length, and iova. Recovering the original mr parameters for ODP and DMABUF to properly recreate it is too hard in this flow, so just fail it.
--- drivers/infiniband/hw/mlx5/mr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/infiniband/hw/mlx5/mr.c +1 -1
@@ -1198,7 +1198,7 @@ struct ib_mr *mlx5_ib_rereg_user_mr(struct ib_mr *ib_mr, int flags, u64 start, }/* DM or ODP MR's don't have a normal umem so we can't re-use it */ if (!mr->umem ||is_odp_mr (mr) ||is_dmabuf_mr (mr)) - goto recreate; +return ERR_PTR (-EOPNOTSUPP );/* * Only one active MR can refer to a umem at one time, revoke
Signature · 1 line
2.43.0
Jason Gunthorpe
maintainerPATCH 2/10 RDMA/mlx5: Create ODP EQ for non-pinned dmabuf MRs
In reply to Jason GunthorpeDMABUF generally relies on the ODP EQ mechanism to safely implement the move semantics. ODP requires a device-global one time startup of the ODP machinery when the first MR is created, and this was missed on the DMABUF path.
Call mlx5r_odp_create_eq() when creating a ODP'able DMABUF.
The core code prevents using IB_ACCESS_ON_DEMAND unless the driver advertises IB_ODP_SUPPORT, so until now, mlx5r_odp_create_eq() cannot be called unless the device has ODP support.
However, DMABUF has no such protection and a second bug was allowing DMABUFs to be created on non-ODP capable HW. Add a guard at the start of mlx5r_odp_create_eq(). This is necessary here anyhow as the dev->odp_eq_mutex is not initialized without IB_ODP_SUPPORT.
--- drivers/infiniband/hw/mlx5/mr.c | 4 ++++ drivers/infiniband/hw/mlx5/odp.c | 3 +++ 2 files changed, 7 insertions(+)
drivers/infiniband/hw/mlx5/mr.c +4 -0
@@ -956,6 +956,10 @@ 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 (!pinned_mode) { + err =mlx5r_odp_create_eq (dev, &dev->odp_pf_eq); +if (err) + goto err_dereg_mr; + err =mlx5r_store_odp_mkey (dev, &mr->mmkey);if (err) goto err_dereg_mr;
drivers/infiniband/hw/mlx5/odp.c +3 -0
@@ -1807,6 +1807,9 @@ int mlx5r_odp_create_eq(struct mlx5_ib_dev *dev, struct mlx5_ib_pf_eq *eq)struct mlx5_eq_param param = {};int err =0 ; +if (!(dev->odp_caps.general_caps &IB_ODP_SUPPORT )) +return -EOPNOTSUPP ; +mutex_lock (&dev->odp_eq_mutex);if (eq->core) goto unlock;
Signature · 1 line
2.43.0
Jason Gunthorpe
maintainerPATCH 3/10 IB/mlx5: Properly support implicit ODP rereg_mr
In reply to Jason GunthorpeDue to all the child mkeys in the implicit ODP configuration we cannot change anything in place for the parent mkey. Instead the whole thing needs to be rebuilt if any change is requested. If the user does not specify a translation then force the implicit values which will then fall through the logic into mlx5_ib_reg_user_mr() to allocate a completely new MR.
Since implicit children were also touching the mr->pd, this removes another case where the access was racy.
--- drivers/infiniband/hw/mlx5/mr.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/infiniband/hw/mlx5/mr.c +15 -0
@@ -1188,6 +1188,21 @@ struct ib_mr *mlx5_ib_rereg_user_mr(struct ib_mr *ib_mr, int flags, u64 start,if (!(flags &IB_MR_REREG_PD )) new_pd = ib_mr->pd; +if (mr->is_odp_implicit && !(flags &IB_MR_REREG_TRANS )) { +if (!(new_access_flags &IB_ACCESS_ON_DEMAND )) +return ERR_PTR (-EOPNOTSUPP ); + +/* +* Due to all the child mkeys we cannot actually change an +* implicit MR in place. If the user did not specify a new +* translation then force the fixed implicit MR values. +*/ + start =0 ; + iova =0 ; + length =U64_MAX ; + flags |=IB_MR_REREG_TRANS ; + } +if (!(flags &IB_MR_REREG_TRANS )) {struct ib_umem *umem;
Signature · 1 line
2.43.0
Jason Gunthorpe
maintainerPATCH 4/10 RDMA/nldev: Fix locking when accessing mr->pd
In reply to Jason GunthorpeSashiko points out that, due to rereg_mr, the PD is actually variable and all the touches in nldev are racy.
Use mr->device instead of mr->pd->device.
Getting the PD restrack ID is more tricky. To avoid disturbing all the happy paths, add an rdma_restrack_sync() operation which is sort of like flush_workqueue() or synchronize_irq(): after it returns, all the old nldev touches to the mr are gone and everything sees the new PD. This makes it safe to reach into the PD pointer.
--- drivers/infiniband/core/nldev.c | 15 +++++---- drivers/infiniband/core/restrack.c | 49 ++++++++++++++++++++++++++++ drivers/infiniband/core/restrack.h | 1 + drivers/infiniband/core/uverbs_cmd.c | 10 ++++-- include/rdma/ib_verbs.h | 5 +++ 5 files changed, 72 insertions(+), 8 deletions(-)
drivers/infiniband/core/nldev.c +9 -6
@@ -695,7 +695,7 @@ static int fill_res_mr_entry(struct sk_buff *msg, bool has_cap_net_admin, struct rdma_restrack_entry *res, uint32_t port) { struct ib_mr *mr = container_of(res, struct ib_mr, res); -struct ib_device *dev = mr->pd->device; +struct ib_device *dev = mr->device;if (has_cap_net_admin) {if (nla_put_u32 (msg,RDMA_NLDEV_ATTR_RES_RKEY , mr->rkey))
@@ -711,9 +711,12 @@ static int fill_res_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,if (nla_put_u32 (msg,RDMA_NLDEV_ATTR_RES_MRN , res->id))return -EMSGSIZE ; -if (!rdma_is_kernel_res (res) && -nla_put_u32 (msg,RDMA_NLDEV_ATTR_RES_PDN , mr->pd->res.id)) -return -EMSGSIZE ; +if (!rdma_is_kernel_res (res)) { +struct ib_pd *pd =READ_ONCE (mr->pd); + +if (nla_put_u32 (msg,RDMA_NLDEV_ATTR_RES_PDN , pd->res.id)) +return -EMSGSIZE ; + }if (fill_res_name_pid (msg, res))return -EMSGSIZE ;
@@ -727,7 +730,7 @@ static int fill_res_mr_raw_entry(struct sk_buff *msg, bool has_cap_net_admin, struct rdma_restrack_entry *res, uint32_t port) { struct ib_mr *mr = container_of(res, struct ib_mr, res); -struct ib_device *dev = mr->pd->device; +struct ib_device *dev = mr->device;if (!dev->ops.fill_res_mr_entry_raw)return -EINVAL ;
@@ -1017,7 +1020,7 @@ static int fill_stat_mr_entry(struct sk_buff *msg, bool has_cap_net_admin, struct rdma_restrack_entry *res, uint32_t port) { struct ib_mr *mr = container_of(res, struct ib_mr, res); -struct ib_device *dev = mr->pd->device; +struct ib_device *dev = mr->device;if (nla_put_u32 (msg,RDMA_NLDEV_ATTR_RES_MRN , res->id)) goto err;
drivers/infiniband/core/restrack.c +49 -0
61 lines not on this page · raw message
drivers/infiniband/core/restrack.h +1 -0
@@ -27,6 +27,7 @@ int rdma_restrack_init(struct ib_device *dev);void rdma_restrack_clean (struct ib_device *dev);void rdma_restrack_add (struct rdma_restrack_entry *res);void rdma_restrack_del (struct rdma_restrack_entry *res); +void rdma_restrack_sync (struct rdma_restrack_entry *res);void rdma_restrack_new (struct rdma_restrack_entry *res,enum rdma_restrack_type type); void rdma_restrack_set_name(struct rdma_restrack_entry *res,
drivers/infiniband/core/uverbs_cmd.c +8 -2
@@ -47,6 +47,7 @@#include "uverbs.h" #include "core_priv.h" +#include "restrack.h" /* * Copy a response to userspace. If the provided 'resp' is larger than the
@@ -819,6 +820,10 @@ static int ib_uverbs_rereg_mr(struct uverbs_attr_bundle *attrs) ret =PTR_ERR (new_pd); goto put_uobjs; } +if (new_pd == orig_pd) { +uobj_put_obj_read (new_pd); + cmd.flags &= ~IB_MR_REREG_PD ; + } }else { new_pd = mr->pd; }
@@ -866,9 +871,10 @@ static int ib_uverbs_rereg_mr(struct uverbs_attr_bundle *attrs) mr = new_mr; }else {if (cmd.flags &IB_MR_REREG_PD ) { -atomic_dec (&orig_pd->usecnt); - mr->pd = new_pd;atomic_inc (&new_pd->usecnt); +WRITE_ONCE (mr->pd, new_pd); +rdma_restrack_sync (&mr->res); +atomic_dec (&orig_pd->usecnt); }if (cmd.flags &IB_MR_REREG_TRANS ) { mr->iova = cmd.hca_va;
include/rdma/ib_verbs.h +5 -0
@@ -1977,6 +1977,11 @@ struct ib_dmah {struct ib_mr {struct ib_device *device; +/* +* Due to IB_MR_REREG_PD pd is not a fixed pointer and can change. For a +* user MR, this value should only be read from a system call that holds +* the uobject lock, or the driver should disable in-place REREG_PD. +*/ struct ib_pd *pd;u32 lkey;u32 rkey;
Signature · 1 line
2.43.0
Jason Gunthorpe
maintainerPATCH 5/10 IB/mlx5: Remove unused mkc bits in mlx5r_umr_update_mr_page_shift()
In reply to Jason GunthorpeThe HW only processes mkc fields selected by mkey_mask. pd, qpn and mkey_7_0 are never selected so they can be left as zero.
This removes a racy read of mr->pd.
--- drivers/infiniband/hw/mlx5/umr.c | 16 +++------------- drivers/infiniband/hw/mlx5/umr.h | 3 +-- 2 files changed, 4 insertions(+), 15 deletions(-)
drivers/infiniband/hw/mlx5/umr.c +3 -13
@@ -937,8 +937,7 @@ int mlx5r_umr_update_xlt(struct mlx5_ib_mr *mr, u64 idx, int npages,* pinned and the HW can switch from 4K to huge-page alignment). */ int mlx5r_umr_update_mr_page_shift (struct mlx5_ib_mr *mr, -unsigned int page_shift, -bool dd) +unsigned int page_shift) {struct mlx5_ib_dev *dev =mr_to_mdev (mr);struct mlx5r_umr_wqe wqe = {};
@@ -953,16 +952,8 @@ int mlx5r_umr_update_mr_page_shift(struct mlx5_ib_mr *mr,/* Fill mkey segment with the new page size, keep the rest unchanged */ MLX5_SET (mkc, &wqe.mkey_seg, log_page_size, page_shift); -if (dd) -MLX5_SET (mkc, &wqe.mkey_seg, pd, dev->ddr.pdn); -else -MLX5_SET (mkc, &wqe.mkey_seg, pd,to_mpd (mr->ibmr.pd)->pdn); -MLX5_SET64 (mkc, &wqe.mkey_seg, start_addr, mr->ibmr.iova);MLX5_SET64 (mkc, &wqe.mkey_seg, len, mr->ibmr.length); -MLX5_SET (mkc, &wqe.mkey_seg, qpn,0xffffff ); -MLX5_SET (mkc, &wqe.mkey_seg, mkey_7_0, -mlx5_mkey_variant (mr->mmkey.key)); err =mlx5r_umr_post_send_wait (dev, mr->mmkey.key, &wqe, false);if (!err)
@@ -1049,7 +1040,7 @@ static int _mlx5r_umr_zap_mkey(struct mlx5_ib_mr *mr,* non-present. */ if (*nblocks) { - err =mlx5r_umr_update_mr_page_shift (mr, max_page_shift, dd); + err =mlx5r_umr_update_mr_page_shift (mr, max_page_shift);if (err) { mr->page_shift = old_page_shift;return err;
@@ -1114,8 +1105,7 @@ int mlx5r_umr_dmabuf_update_pgsz(struct mlx5_ib_mr *mr, u32 xlt_flags, goto err; } - err =mlx5r_umr_update_mr_page_shift (mr, mr->page_shift, - mr->data_direct); + err =mlx5r_umr_update_mr_page_shift (mr, mr->page_shift);if (err) goto err; err = _mlx5r_dmabuf_umr_update_pas(mr, xlt_flags, 0, zapped_blocks,
drivers/infiniband/hw/mlx5/umr.h +1 -2
@@ -106,8 +106,7 @@ int mlx5r_umr_update_mr_pas(struct mlx5_ib_mr *mr, unsigned int flags);int mlx5r_umr_update_xlt (struct mlx5_ib_mr *mr,u64 idx,int npages,int page_shift,int flags);int mlx5r_umr_update_mr_page_shift (struct mlx5_ib_mr *mr, -unsigned int page_shift, -bool dd); +unsigned int page_shift);int mlx5r_umr_dmabuf_update_pgsz (struct mlx5_ib_mr *mr,u32 xlt_flags,unsigned int page_shift);
Signature · 1 line
2.43.0
Jason Gunthorpe
maintainerPATCH 6/10 IB/mlx5: Pull the pdn out of the depths of the umr machinery
In reply to Jason GunthorpeInstead of getting the pdn deep inside the umr code, pass it in from the top. to_mpd(mr->ibmr.pd)->pdn is not safe due to the rereg races, so all the call sites need some revision to obtain the pdn in a safe way.
Mark them with mlx5_mr_pdn(); following patches will go through and remove these.
Cases where the XLT flags are known and do not require the PDN can pass 0, such as for mlx5_ib_dmabuf_invalidate_cb().
Also extract the DMABUF data_direct special case from inside the UMR code and into the only place that needs it, pagefault_dmabuf_mr(). The actual mr was created directly without using the UMR flow. Ultimately this will be moved into mlx5_ib_init_dmabuf_mr().
--- drivers/infiniband/hw/mlx5/mlx5_ib.h | 9 ++++ drivers/infiniband/hw/mlx5/mr.c | 8 +-- drivers/infiniband/hw/mlx5/odp.c | 12 +++-- drivers/infiniband/hw/mlx5/umr.c | 75 ++++++++++++++-------------- drivers/infiniband/hw/mlx5/umr.h | 6 +-- 5 files changed, 64 insertions(+), 46 deletions(-)
drivers/infiniband/hw/mlx5/mlx5_ib.h +9 -0
@@ -331,6 +331,10 @@ struct mlx5_ib_flow_db {#define MLX5_IB_QPT_DCT IB_QPT_RESERVED4#define MLX5_IB_WR_UMR IB_WR_RESERVED1 +/* +* A valid pdn is required when flags include MLX5_IB_UPD_XLT_ENABLE, +* MLX5_IB_UPD_XLT_PD or MLX5_IB_UPD_XLT_ACCESS. +*/ #define MLX5_IB_UPD_XLT_ZAP BIT(0)#define MLX5_IB_UPD_XLT_ENABLE BIT(1)#define MLX5_IB_UPD_XLT_ATOMIC BIT(2)
@@ -1209,6 +1213,11 @@ static inline struct mlx5_ib_pd *to_mpd(struct ib_pd *ibpd) return container_of(ibpd, struct mlx5_ib_pd, ibpd); } +static inline u32 mlx5_mr_pdn (struct mlx5_ib_mr *mr) +{ +return to_mpd (mr->ibmr.pd)->pdn; +} +static inline struct mlx5_ib_srq *to_msrq (struct ib_srq *ibsrq) { return container_of(ibsrq, struct mlx5_ib_srq, ibsrq);
drivers/infiniband/hw/mlx5/mr.c +5 -3
@@ -781,7 +781,8 @@ static struct ib_mr *create_real_mr(struct ib_pd *pd, struct ib_umem *umem,* configured properly but left disabled. It is safe to go ahead * and configure it again via UMR while enabling it. */ - err =mlx5r_umr_update_mr_pas (mr,MLX5_IB_UPD_XLT_ENABLE ); + err =mlx5r_umr_update_mr_pas (mr,MLX5_IB_UPD_XLT_ENABLE , +to_mpd (pd)->pdn);if (err) {mlx5_ib_dereg_mr (&mr->ibmr,NULL );return ERR_PTR (err);
@@ -890,7 +891,8 @@ static void mlx5_ib_dmabuf_invalidate_cb(struct dma_buf_attachment *attach)if (!umem_dmabuf->sgt || !mr)return ; -mlx5r_umr_update_mr_pas (mr,MLX5_IB_UPD_XLT_ZAP ); +/* MLX5_IB_UPD_XLT_ZAP does not change the pdn */ +mlx5r_umr_update_mr_pas (mr,MLX5_IB_UPD_XLT_ZAP ,0 );ib_umem_dmabuf_unmap_pages (umem_dmabuf); }
@@ -1145,7 +1147,7 @@ static int umr_rereg_pas(struct mlx5_ib_mr *mr, struct ib_pd *pd, mr->ibmr.length = new_umem->length; mr->page_shift =order_base_2 (page_size); mr->umem = new_umem; - err =mlx5r_umr_update_mr_pas (mr, upd_flags); + err =mlx5r_umr_update_mr_pas (mr, upd_flags,mlx5_mr_pdn (mr));if (err) {/* * The MR is revoked at this point so there is no issue to free
drivers/infiniband/hw/mlx5/odp.c +9 -3
@@ -833,12 +833,14 @@ static int pagefault_dmabuf_mr(struct mlx5_ib_mr *mr, size_t bcnt, u32 *bytes_mapped, u32 flags) {struct ib_umem_dmabuf *umem_dmabuf =to_ib_umem_dmabuf (mr->umem); +struct mlx5_ib_dev *dev =mr_to_mdev (mr);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 ; +u32 pdn =0 ;int err;if (flags &MLX5_PF_FLAGS_ENABLE )
@@ -857,8 +859,12 @@ static int pagefault_dmabuf_mr(struct mlx5_ib_mr *mr, size_t bcnt, err = -EINVAL ; }else { page_shift =order_base_2 (page_size); +if (mr->data_direct) + pdn = dev->ddr.pdn; +else + pdn =mlx5_mr_pdn (mr);if (page_shift != mr->page_shift && mr->dmabuf_faulted) { - err =mlx5r_umr_dmabuf_update_pgsz (mr, xlt_flags, + err =mlx5r_umr_dmabuf_update_pgsz (mr, xlt_flags, pdn, page_shift); }else { mr->page_shift = page_shift;
@@ -866,8 +872,8 @@ static int pagefault_dmabuf_mr(struct mlx5_ib_mr *mr, size_t bcnt, err =mlx5r_umr_update_data_direct_ksm_pas ( mr, xlt_flags); else - err =mlx5r_umr_update_mr_pas (mr, - xlt_flags); + err =mlx5r_umr_update_mr_pas (mr, xlt_flags, + pdn); } }dma_resv_unlock (umem_dmabuf->attach->dmabuf->resv);
drivers/infiniband/hw/mlx5/umr.c +38 -37
192 lines not on this page · raw message
drivers/infiniband/hw/mlx5/umr.h +3 -3
@@ -101,13 +101,13 @@ int mlx5r_umr_update_data_direct_ksm_pas_range(struct mlx5_ib_mr *mr, size_t nblocks);int mlx5r_umr_update_data_direct_ksm_pas (struct mlx5_ib_mr *mr,unsigned int flags);int mlx5r_umr_update_mr_pas_range (struct mlx5_ib_mr *mr,unsigned int flags, -size_t start_block,size_t nblocks); -int mlx5r_umr_update_mr_pas (struct mlx5_ib_mr *mr,unsigned int flags); +u32 pdn,size_t start_block,size_t nblocks); +int mlx5r_umr_update_mr_pas (struct mlx5_ib_mr *mr,unsigned int flags,u32 pdn);int mlx5r_umr_update_xlt (struct mlx5_ib_mr *mr,u64 idx,int npages,int page_shift,int flags);int mlx5r_umr_update_mr_page_shift (struct mlx5_ib_mr *mr,unsigned int page_shift); -int mlx5r_umr_dmabuf_update_pgsz (struct mlx5_ib_mr *mr,u32 xlt_flags, +int mlx5r_umr_dmabuf_update_pgsz (struct mlx5_ib_mr *mr,u32 xlt_flags,u32 pdn,unsigned int page_shift);#endif /* _MLX5_IB_UMR_H */
Signature · 1 line
2.43.0
Jason Gunthorpe
maintainerPATCH 7/10 IB/mlx5: Don't mangle the mr->pd inside the rereg callback
In reply to Jason GunthorpeThe rereg protocol expects the core code to change mr->pd and synchronize that change with the atomics and syncs. The driver should not touch it.
mlx5 needed to update it in umr_rereg_pas() because mlx5r_umr_update_mr_pas() required the updated mr->pd to build the UMR.
Simply switch mlx5r_umr_update_mr_pas() to use the pdn directly from the new pd and remove the mr->pd update.
--- drivers/infiniband/hw/mlx5/mr.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/infiniband/hw/mlx5/mr.c +2 -4
@@ -1134,10 +1134,8 @@ static int umr_rereg_pas(struct mlx5_ib_mr *mr, struct ib_pd *pd,if (err)return err; -if (flags &IB_MR_REREG_PD ) { - mr->ibmr.pd = pd; +if (flags &IB_MR_REREG_PD ) upd_flags |=MLX5_IB_UPD_XLT_PD ; - }if (flags &IB_MR_REREG_ACCESS ) { mr->access_flags = access_flags; upd_flags |=MLX5_IB_UPD_XLT_ACCESS ;
@@ -1147,7 +1145,7 @@ static int umr_rereg_pas(struct mlx5_ib_mr *mr, struct ib_pd *pd, mr->ibmr.length = new_umem->length; mr->page_shift =order_base_2 (page_size); mr->umem = new_umem; - err =mlx5r_umr_update_mr_pas (mr, upd_flags,mlx5_mr_pdn (mr)); + err =mlx5r_umr_update_mr_pas (mr, upd_flags,to_mpd (pd)->pdn);if (err) {/* * The MR is revoked at this point so there is no issue to free
Signature · 1 line
2.43.0
Jason Gunthorpe
maintainerPATCH 8/10 IB/mlx5: Push pdn above mlx5r_umr_update_xlt()
In reply to Jason GunthorpeKeep pushing the pdn higher to remove more places touching mr->pd:
- XLT combinations that don't use PDN can just pass 0
- Use local pd values instead of mr->pd
- Implicit MR does not have inplace rereg, so the mr->pd is safe
--- drivers/infiniband/hw/mlx5/odp.c | 40 +++++++++++++++++--------------- drivers/infiniband/hw/mlx5/umr.c | 3 +-- drivers/infiniband/hw/mlx5/umr.h | 2 +- 3 files changed, 23 insertions(+), 22 deletions(-)
drivers/infiniband/hw/mlx5/odp.c +21 -19
@@ -221,7 +221,8 @@ static void free_implicit_child_mr_work(struct work_struct *work)mutex_lock (&odp_imr->umem_mutex);mlx5r_umr_update_xlt (mr->parent,ib_umem_start (odp) >> mlx5_imr_mtt_shift,1 ,0 , -MLX5_IB_UPD_XLT_INDIRECT |MLX5_IB_UPD_XLT_ATOMIC ); +MLX5_IB_UPD_XLT_INDIRECT |MLX5_IB_UPD_XLT_ATOMIC , +0 );mutex_unlock (&odp_imr->umem_mutex);mlx5_ib_dereg_mr (&mr->ibmr,NULL );
@@ -318,10 +319,12 @@ static bool mlx5_ib_invalidate_range(struct mmu_interval_notifier *mni,u64 umr_offset = idx & umr_block_mask;if (in_block && umr_offset ==0 ) { -mlx5r_umr_update_xlt (mr, blk_start_idx, - idx - blk_start_idx,0 , -MLX5_IB_UPD_XLT_ZAP | -MLX5_IB_UPD_XLT_ATOMIC ); +mlx5r_umr_update_xlt ( + mr, blk_start_idx, idx - blk_start_idx, +0 , +MLX5_IB_UPD_XLT_ZAP | +MLX5_IB_UPD_XLT_ATOMIC , +0 ); in_block =0 ;/* Count page invalidations */ invalidations += idx - blk_start_idx +1 ;
@@ -329,10 +332,9 @@ static bool mlx5_ib_invalidate_range(struct mmu_interval_notifier *mni, } }if (in_block) { -mlx5r_umr_update_xlt (mr, blk_start_idx, - idx - blk_start_idx +1 ,0 , -MLX5_IB_UPD_XLT_ZAP | -MLX5_IB_UPD_XLT_ATOMIC ); +mlx5r_umr_update_xlt ( + mr, blk_start_idx, idx - blk_start_idx +1 ,0 , +MLX5_IB_UPD_XLT_ZAP |MLX5_IB_UPD_XLT_ATOMIC ,0 );/* Count page invalidations */ invalidations += idx - blk_start_idx +1 ; }
@@ -502,11 +504,9 @@ static struct mlx5_ib_mr *implicit_get_child_mr(struct mlx5_ib_mr *imr,*/ refcount_set (&mr->mmkey.usecount,2 ); - err =mlx5r_umr_update_xlt (mr,0 , - mlx5_imr_mtt_entries, -PAGE_SHIFT , -MLX5_IB_UPD_XLT_ZAP | -MLX5_IB_UPD_XLT_ENABLE ); + err =mlx5r_umr_update_xlt (mr,0 , mlx5_imr_mtt_entries,PAGE_SHIFT , +MLX5_IB_UPD_XLT_ZAP |MLX5_IB_UPD_XLT_ENABLE , +to_mpd (mr->ibmr.pd)->pdn);if (err) { ret =ERR_PTR (err); goto out_mr;
@@ -647,7 +647,8 @@ struct mlx5_ib_mr *mlx5_ib_alloc_implicit_mr(struct mlx5_ib_pd *pd, mlx5_imr_ksm_page_shift,MLX5_IB_UPD_XLT_INDIRECT |MLX5_IB_UPD_XLT_ZAP | - MLX5_IB_UPD_XLT_ENABLE); +MLX5_IB_UPD_XLT_ENABLE , + pd->pdn);if (err) goto out_mr;
@@ -720,7 +721,8 @@ static int pagefault_real_mr(struct mlx5_ib_mr *mr, struct ib_umem_odp *odp,* No need to check whether the MTTs really belong to this MR, since * ib_umem_odp_map_dma_and_lock already checks this. */ - ret =mlx5r_umr_update_xlt (mr, start_idx, np, page_shift, xlt_flags); + ret =mlx5r_umr_update_xlt (mr, start_idx, np, page_shift, xlt_flags, +mlx5_mr_pdn (mr));mutex_unlock (&odp->umem_mutex);if (ret <0 ) {
@@ -818,9 +820,9 @@ static int pagefault_implicit_mr(struct mlx5_ib_mr *imr,* next pagefault handler will see the new information. */ mutex_lock (&odp_imr->umem_mutex); - err =mlx5r_umr_update_xlt (imr, upd_start_idx, upd_len,0 , -MLX5_IB_UPD_XLT_INDIRECT | -MLX5_IB_UPD_XLT_ATOMIC ); + err =mlx5r_umr_update_xlt ( + imr, upd_start_idx, upd_len,0 , +MLX5_IB_UPD_XLT_INDIRECT |MLX5_IB_UPD_XLT_ATOMIC ,0 );mutex_unlock (&odp_imr->umem_mutex);if (err) {mlx5_ib_err (mr_to_mdev (imr),"Failed to update PAS\n" );
drivers/infiniband/hw/mlx5/umr.c +1 -2
@@ -846,7 +846,7 @@ static bool umr_can_use_indirect_mkey(struct mlx5_ib_dev *dev) }int mlx5r_umr_update_xlt (struct mlx5_ib_mr *mr,u64 idx,int npages, -int page_shift,int flags) +int page_shift,int flags,u32 pdn) {int desc_size = (flags &MLX5_IB_UPD_XLT_INDIRECT ) ? sizeof(struct mlx5_klm)
@@ -862,7 +862,6 @@ int mlx5r_umr_update_xlt(struct mlx5_ib_mr *mr, u64 idx, int npages,size_t orig_sg_length;size_t pages_iter;struct ib_sge sg; -u32 pdn =mlx5_mr_pdn (mr);int err =0 ;void *xlt;
drivers/infiniband/hw/mlx5/umr.h +1 -1
@@ -104,7 +104,7 @@ int mlx5r_umr_update_mr_pas_range(struct mlx5_ib_mr *mr, unsigned int flags, u32 pdn, size_t start_block, size_t nblocks);int mlx5r_umr_update_mr_pas (struct mlx5_ib_mr *mr,unsigned int flags,u32 pdn);int mlx5r_umr_update_xlt (struct mlx5_ib_mr *mr,u64 idx,int npages, -int page_shift,int flags); +int page_shift,int flags,u32 pdn);int mlx5r_umr_update_mr_page_shift (struct mlx5_ib_mr *mr,unsigned int page_shift); int mlx5r_umr_dmabuf_update_pgsz(struct mlx5_ib_mr *mr, u32 xlt_flags, u32 pdn,
Signature · 1 line
2.43.0
Jason Gunthorpe
maintainerPATCH 9/10 IB/mlx5: Push pdn above pagfault_real_mr()
In reply to Jason GunthorpeRemove the mlx5_mr_pdn() in pagefault_real_mr() by pushing the pdn up, all the callers use 0 since they don't pass MLX5_PF_FLAGS_ENABLE except the ioctl reg_mr path which can use the ioctl pd.
--- drivers/infiniband/hw/mlx5/mlx5_ib.h | 4 ++-- drivers/infiniband/hw/mlx5/mr.c | 2 +- drivers/infiniband/hw/mlx5/odp.c | 20 ++++++++++++-------- 3 files changed, 15 insertions(+), 11 deletions(-)
drivers/infiniband/hw/mlx5/mlx5_ib.h +2 -2
@@ -1422,7 +1422,7 @@ int mlx5_odp_populate_xlt(void *xlt, size_t idx, size_t nentries,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); +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);#else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */ static inline int mlx5_ib_odp_init_one (struct mlx5_ib_dev *ibdev) {return 0 ; }
@@ -1451,7 +1451,7 @@ mlx5_ib_advise_mr_prefetch(struct ib_pd *pd, {return -EOPNOTSUPP ; } -static inline int mlx5_ib_init_odp_mr (struct mlx5_ib_mr *mr) +static inline int mlx5_ib_init_odp_mr (struct mlx5_ib_mr *mr,struct ib_pd *pd) {return -EOPNOTSUPP ; }
drivers/infiniband/hw/mlx5/mr.c +1 -1
@@ -842,7 +842,7 @@ static struct ib_mr *create_user_odp_mr(struct ib_pd *pd, u64 start, u64 length,if (err) goto err_dereg_mr; - err =mlx5_ib_init_odp_mr (mr); + err =mlx5_ib_init_odp_mr (mr, pd);if (err) goto err_dereg_mr;return &mr->ibmr;
drivers/infiniband/hw/mlx5/odp.c +12 -8
@@ -687,12 +687,16 @@ void mlx5_ib_free_odp_mr(struct mlx5_ib_mr *mr) } } +/* +* pdn must be valid only when xlt_flags updates the mkey PD. In this path that +* is only MLX5_PF_FLAGS_ENABLE. DOWNGRADE and SNAPSHOT leave the PD masked out. +*/ #define MLX5_PF_FLAGS_DOWNGRADE BIT(1)#define MLX5_PF_FLAGS_SNAPSHOT BIT(2)#define MLX5_PF_FLAGS_ENABLE BIT(3)static int pagefault_real_mr (struct mlx5_ib_mr *mr,struct ib_umem_odp *odp,u64 user_va,size_t bcnt,u32 *bytes_mapped, -u32 flags) +u32 flags,u32 pdn) {int page_shift, ret, np;bool downgrade = flags &MLX5_PF_FLAGS_DOWNGRADE ;
@@ -722,7 +726,7 @@ static int pagefault_real_mr(struct mlx5_ib_mr *mr, struct ib_umem_odp *odp,* ib_umem_odp_map_dma_and_lock already checks this. */ ret =mlx5r_umr_update_xlt (mr, start_idx, np, page_shift, xlt_flags, -mlx5_mr_pdn (mr)); + pdn);mutex_unlock (&odp->umem_mutex);if (ret <0 ) {
@@ -788,7 +792,7 @@ static int pagefault_implicit_mr(struct mlx5_ib_mr *imr, user_va; ret =pagefault_real_mr (mtt, umem_odp, user_va, len, - bytes_mapped, flags); + bytes_mapped, flags,0 );mlx5r_deref_odp_mkey (&mtt->mmkey);
@@ -930,19 +934,20 @@ static int pagefault_mr(struct mlx5_ib_mr *mr, u64 io_virt, size_t bcnt, ib_umem_end(odp) - user_va < bcnt))return -EFAULT ;return pagefault_real_mr (mr, odp, user_va, bcnt, bytes_mapped, - flags); + flags,0 ); }return pagefault_implicit_mr (mr, odp, io_virt, bcnt, bytes_mapped, flags); } -int mlx5_ib_init_odp_mr (struct mlx5_ib_mr *mr) +int mlx5_ib_init_odp_mr (struct mlx5_ib_mr *mr,struct ib_pd *pd) {int ret; ret =pagefault_real_mr (mr,to_ib_umem_odp (mr->umem), mr->umem->address, mr->umem->length,NULL , -MLX5_PF_FLAGS_SNAPSHOT |MLX5_PF_FLAGS_ENABLE ); +MLX5_PF_FLAGS_SNAPSHOT |MLX5_PF_FLAGS_ENABLE , +to_mpd (pd)->pdn);return ret >=0 ?0 : ret; }
@@ -1575,8 +1580,7 @@ static void mlx5_ib_mr_memory_pfault_handler(struct mlx5_ib_dev *dev, ret =pagefault_mr (mr, prefetch_va, prefetch_size,NULL ,0 , true);if (ret <0 ) { ret =pagefault_mr (mr, pfault->memory.va, - pfault->memory.fault_byte_count,NULL ,0 , - true); + pfault->memory.fault_byte_count,NULL ,0 , true);if (ret <0 ) goto err; }
Signature · 1 line
2.43.0
Jason Gunthorpe
maintainerPATCH 10/10 IB/mlx5: Push pdn above pagefault_dmabuf_mr()
In reply to Jason GunthorpeRemove the mlx5_mr_pdn() inside pagefault_dmabuf_mr(), the only user of the pdn is the init path which is inside an ioctl.
--- drivers/infiniband/hw/mlx5/mlx5_ib.h | 9 ++------- drivers/infiniband/hw/mlx5/mr.c | 2 +- drivers/infiniband/hw/mlx5/odp.c | 21 +++++++++++---------- 3 files changed, 14 insertions(+), 18 deletions(-)
drivers/infiniband/hw/mlx5/mlx5_ib.h +2 -7
@@ -1213,11 +1213,6 @@ static inline struct mlx5_ib_pd *to_mpd(struct ib_pd *ibpd) return container_of(ibpd, struct mlx5_ib_pd, ibpd); } -static inline u32 mlx5_mr_pdn (struct mlx5_ib_mr *mr) -{ -return to_mpd (mr->ibmr.pd)->pdn; -} -static inline struct mlx5_ib_srq *to_msrq (struct ib_srq *ibsrq) { return container_of(ibsrq, struct mlx5_ib_srq, ibsrq);
@@ -1423,7 +1418,7 @@ 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); +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,
@@ -1455,7 +1450,7 @@ 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) +static inline int mlx5_ib_init_dmabuf_mr (struct mlx5_ib_mr *mr,struct ib_pd *pd) {return -EOPNOTSUPP ; }
drivers/infiniband/hw/mlx5/mr.c +1 -1
@@ -969,7 +969,7 @@ reg_user_mr_dmabuf(struct ib_pd *pd, struct device *dma_device, mr->data_direct = true; } - err =mlx5_ib_init_dmabuf_mr (mr); + err =mlx5_ib_init_dmabuf_mr (mr, pd);if (err) goto err_dereg_mr;return &mr->ibmr;
drivers/infiniband/hw/mlx5/odp.c +11 -10
@@ -836,17 +836,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 *bytes_mapped,u32 flags,u32 pdn) {struct ib_umem_dmabuf *umem_dmabuf =to_ib_umem_dmabuf (mr->umem); -struct mlx5_ib_dev *dev =mr_to_mdev (mr);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 ; -u32 pdn =0 ;int err;if (flags &MLX5_PF_FLAGS_ENABLE )
@@ -865,10 +863,6 @@ static int pagefault_dmabuf_mr(struct mlx5_ib_mr *mr, size_t bcnt, err = -EINVAL ; }else { page_shift =order_base_2 (page_size); -if (mr->data_direct) - pdn = dev->ddr.pdn; -else - pdn =mlx5_mr_pdn (mr);if (page_shift != mr->page_shift && mr->dmabuf_faulted) { err =mlx5r_umr_dmabuf_update_pgsz (mr, xlt_flags, pdn, page_shift);
@@ -915,7 +909,7 @@ static int pagefault_mr(struct mlx5_ib_mr *mr, u64 io_virt, size_t bcnt,return -EFAULT ;if (mr->umem->is_dmabuf) -return pagefault_dmabuf_mr (mr, bcnt, bytes_mapped, flags); +return pagefault_dmabuf_mr (mr, bcnt, bytes_mapped, flags,0 );if (!odp->is_implicit_odp) {u64 offset = io_virt < mr->ibmr.iova ?0 : io_virt - mr->ibmr.iova;
@@ -951,12 +945,19 @@ 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) +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->ddr.pdn; +else + pdn =to_mpd (pd)->pdn; + ret =pagefault_dmabuf_mr (mr, mr->umem->length,NULL , -MLX5_PF_FLAGS_ENABLE ); +MLX5_PF_FLAGS_ENABLE , pdn);return ret >=0 ?0 : ret; }
Signature · 1 line
2.43.0
Quoted 31 lines · Jason Gunthorpe
Sashiko pointed out an existing bug related to
mr->pd: whenIB_MR_REREG_PDis used themr->pdis changed while only holding the write side of the MR's uobject lock.Effectively, because
IB_MR_REREG_PDis usually implemented by changing the MR in-place, themr->pdbecomes unreadable outside an MR-specific system call that holds the uobject lock. All the readers in this series could race with anIB_MR_REREG_PDand potentially UAF themr->pd.https://sashiko.dev/#/patchset/20260427-security-bug-fixes-v3-0-4621fa52de0e%40nvidia.com?part=4
This was presented as a simple 'oh look it can race with nldev' which is correct. However, asking AI to fully audit
mr->pdtouches also revealed a much more convoluted issue inside mlx5 ODP that is also usingmr->pdfrom the page fault work queue, advise mr work queue and advise mr system call without any locking.It turns out this mlx5 problem is entirely unnecessary since outside implicit mr there are only three cases where the UMR actually flags the PDN to be read by HW,
umr_rereg_pas(),mlx5_ib_init_odp_mr()andmlx5_ib_init_dmabuf_mr().umr_rereg_pas()is particularly tricky because it illegaly updatesmr->pdinside the driver. Reorganize the giant call chain frommlx5r_umr_set_update_xlt_mkey_seg()upward so that pdn is passed down from those three functions instead of unconditionally picked out at the bottom.nldev however is trickier to fix. To avoid disurbing the happy paths build a synchronize barrier by removing the mr from the xarray and then putting it right back. The kref completion acts as a positive signal that the
mr->pdis no longer being used.
Quoted 11 lines
Jason Gunthorpe (10):
IB/mlx5: Don't take therereg_mrfallback without a new translation RDMA/mlx5: Create ODP EQ for non-pinned dmabuf MRs IB/mlx5: Properly support implicit ODPrereg_mrRDMA/nldev: Fix locking when accessingmr->pd
IB/mlx5: Remove unused mkc bits inmlx5r_umr_update_mr_page_shift()IB/mlx5: Pull the pdn out of the depths of the umr machinery IB/mlx5: Don't mangle themr->pdinside the rereg callback IB/mlx5: Push pdn abovemlx5r_umr_update_xlt()
IB/mlx5: Push pdn abovepagfault_real_mr()
IB/mlx5: Push pdn abovepagefault_dmabuf_mr()
Applied to for-next
Thanks,
Jason