All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stefano Stabellini <sstabellini@kernel.org>
To: Oleksandr Tyshchenko <olekstysh@gmail.com>
Cc: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org,
	Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>,
	Stefano Stabellini <sstabellini@kernel.org>,
	Juergen Gross <jgross@suse.com>
Subject: Re: [PATCH 2/2] xen/virtio: Fix potential deadlock when accessing xen_grant_dma_devices
Date: Wed, 5 Oct 2022 17:19:07 -0700 (PDT)	[thread overview]
Message-ID: <alpine.DEB.2.22.394.2210051719000.3690179@ubuntu-linux-20-04-desktop> (raw)
In-Reply-To: <20221005174823.1800761-3-olekstysh@gmail.com>

On Wed, 5 Oct 2022, Oleksandr Tyshchenko wrote:
> From: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
> 
> As find_xen_grant_dma_data() is called from both interrupt and process
> contexts, the access to xen_grant_dma_devices XArray must be protected
> by xa_lock_irqsave to avoid deadlock scenario.
> As XArray API doesn't provide xa_store_irqsave helper, call lockless
> __xa_store directly and guard it externally.
> 
> Also move the storage of the XArray's entry to a separate helper.
> 
> Signed-off-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
> Fixes: d6aca3504c7d ("xen/grant-dma-ops: Add option to restrict memory access under Xen")

Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>


> ---
>  drivers/xen/grant-dma-ops.c | 24 +++++++++++++++++++-----
>  1 file changed, 19 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/xen/grant-dma-ops.c b/drivers/xen/grant-dma-ops.c
> index 1998d0e8ce82..c66f56d24013 100644
> --- a/drivers/xen/grant-dma-ops.c
> +++ b/drivers/xen/grant-dma-ops.c
> @@ -25,7 +25,7 @@ struct xen_grant_dma_data {
>  	bool broken;
>  };
>  
> -static DEFINE_XARRAY(xen_grant_dma_devices);
> +static DEFINE_XARRAY_FLAGS(xen_grant_dma_devices, XA_FLAGS_LOCK_IRQ);
>  
>  #define XEN_GRANT_DMA_ADDR_OFF	(1ULL << 63)
>  
> @@ -42,14 +42,29 @@ static inline grant_ref_t dma_to_grant(dma_addr_t dma)
>  static struct xen_grant_dma_data *find_xen_grant_dma_data(struct device *dev)
>  {
>  	struct xen_grant_dma_data *data;
> +	unsigned long flags;
>  
> -	xa_lock(&xen_grant_dma_devices);
> +	xa_lock_irqsave(&xen_grant_dma_devices, flags);
>  	data = xa_load(&xen_grant_dma_devices, (unsigned long)dev);
> -	xa_unlock(&xen_grant_dma_devices);
> +	xa_unlock_irqrestore(&xen_grant_dma_devices, flags);
>  
>  	return data;
>  }
>  
> +static int store_xen_grant_dma_data(struct device *dev,
> +				    struct xen_grant_dma_data *data)
> +{
> +	unsigned long flags;
> +	int ret;
> +
> +	xa_lock_irqsave(&xen_grant_dma_devices, flags);
> +	ret = xa_err(__xa_store(&xen_grant_dma_devices, (unsigned long)dev, data,
> +			GFP_ATOMIC));
> +	xa_unlock_irqrestore(&xen_grant_dma_devices, flags);
> +
> +	return ret;
> +}
> +
>  /*
>   * DMA ops for Xen frontends (e.g. virtio).
>   *
> @@ -338,8 +353,7 @@ void xen_grant_setup_dma_ops(struct device *dev)
>  	 */
>  	data->backend_domid = iommu_spec.args[0];
>  
> -	if (xa_err(xa_store(&xen_grant_dma_devices, (unsigned long)dev, data,
> -			GFP_KERNEL))) {
> +	if (store_xen_grant_dma_data(dev, data)) {
>  		dev_err(dev, "Cannot store Xen grant DMA data\n");
>  		goto err;
>  	}
> -- 
> 2.25.1
> 

  reply	other threads:[~2022-10-06  0:19 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-05 17:48 [PATCH 0/2] Misc fixes for Xen grant DMA-mapping layer Oleksandr Tyshchenko
2022-10-05 17:48 ` [PATCH 1/2] xen/virtio: Fix n_pages calculation in xen_grant_dma_map(unmap)_page() Oleksandr Tyshchenko
2022-10-06  0:07   ` Stefano Stabellini
2022-10-06  5:06   ` Juergen Gross
2022-10-06  7:35   ` Xenia Ragiadakou
2022-10-06  8:05     ` Juergen Gross
2022-10-06 10:14       ` Oleksandr Tyshchenko
2022-10-06 10:24         ` Juergen Gross
2022-10-06 10:30           ` Oleksandr Tyshchenko
2022-10-05 17:48 ` [PATCH 2/2] xen/virtio: Fix potential deadlock when accessing xen_grant_dma_devices Oleksandr Tyshchenko
2022-10-06  0:19   ` Stefano Stabellini [this message]
2022-10-06  5:08   ` Juergen Gross
2022-10-07  5:19 ` [PATCH 0/2] Misc fixes for Xen grant DMA-mapping layer Juergen Gross

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=alpine.DEB.2.22.394.2210051719000.3690179@ubuntu-linux-20-04-desktop \
    --to=sstabellini@kernel.org \
    --cc=jgross@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleksandr_tyshchenko@epam.com \
    --cc=olekstysh@gmail.com \
    --cc=xen-devel@lists.xenproject.org \
    /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.