All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@nvidia.com>
To: Leon Romanovsky <leon@kernel.org>
Cc: Patrisious Haddad <phaddad@nvidia.com>,
	Artemy Kovalyov <artemyko@mellanox.com>,
	linux-rdma@vger.kernel.org
Subject: Re: [PATCH rdma-next v1] RDMA/mlx5: Fix implicit ODP use after free
Date: Mon, 20 Jan 2025 14:49:22 -0400	[thread overview]
Message-ID: <20250120184922.GS5556@nvidia.com> (raw)
In-Reply-To: <c96b8645a81085abff739e6b06e286a350d1283d.1737274283.git.leon@kernel.org>

On Sun, Jan 19, 2025 at 10:21:41AM +0200, Leon Romanovsky wrote:
 
> Fixes: 5256edcb98a1 ("RDMA/mlx5: Rework implicit ODP destroy")

Cc: stable

Fixes a user triggerable oops
> -	if (!refcount_inc_not_zero(&imr->mmkey.usecount))
> +	xa_lock(&imr->implicit_children);
> +	if (__xa_cmpxchg(&imr->implicit_children, idx, mr, NULL, GFP_KERNEL) !=
> +	    mr) {
> +		xa_unlock(&imr->implicit_children);
>  		return;
> +	}
>  
> -	xa_erase(&imr->implicit_children, idx);
>  	if (MLX5_CAP_ODP(mr_to_mdev(mr)->mdev, mem_page_fault))
> -		xa_erase(&mr_to_mdev(mr)->odp_mkeys,
> -			 mlx5_base_mkey(mr->mmkey.key));
> +		__xa_erase(&mr_to_mdev(mr)->odp_mkeys,
> +			   mlx5_base_mkey(mr->mmkey.key));
> +	xa_unlock(&imr->implicit_children);
> +
> +	if (!refcount_inc_not_zero(&imr->mmkey.usecount))
> +		return;

It seems the refcount must be done first:

	/*
	 * If userspace is racing freeing the parent implicit ODP MR
	 * then we can loose the race with parent destruction. In this
	 * case mlx5_ib_free_odp_mr() will free everything in the
	 * implicit_children xarray so NOP is fine. This child MR
	 * cannot be destroyed here because we are under its umem_mutex.
	 */
	if (!refcount_inc_not_zero(&imr->mmkey.usecount))
		return;

What we must not do is remove something from the xarray and then fail
to free it.

Jason

  reply	other threads:[~2025-01-20 18:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-19  8:21 [PATCH rdma-next v1] RDMA/mlx5: Fix implicit ODP use after free Leon Romanovsky
2025-01-20 18:49 ` Jason Gunthorpe [this message]
2025-01-21  8:39   ` Leon Romanovsky
2025-01-21 18:14 ` Jason Gunthorpe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250120184922.GS5556@nvidia.com \
    --to=jgg@nvidia.com \
    --cc=artemyko@mellanox.com \
    --cc=leon@kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=phaddad@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.