All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dave Jiang <dave.jiang@intel.com>
To: Vinicius Costa Gomes <vinicius.gomes@intel.com>,
	Vinod Koul <vkoul@kernel.org>, Frank Li <Frank.Li@kernel.org>,
	Fenghua Yu <fenghuay@nvidia.com>,
	Shuai Xue <xueshuai@linux.alibaba.com>
Cc: dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 8/8] dmaengine: idxd: Don't flush the descriptors on terminate_all()
Date: Mon, 30 Nov 2026 10:18:10 -0700	[thread overview]
Message-ID: <a4c702ff-2095-4a80-a32c-548c70646eb8@intel.com> (raw)
In-Reply-To: <20261125-wt-idxd-fixes-v2-8-25a4691dfb9e@intel.com>



On 11/25/26 4:49 PM, Vinicius Costa Gomes wrote:
> idxd_dma_terminate_all() has two problems:
> 
> - It unmaps and frees the pending descriptors while the device may
>   still process them. The device can then write to unmapped buffers,
>   or to the completion record of a reused descriptor.
> 
> - It takes wq->wq_lock, a mutex, but terminate_all() can be called from
>   atomic context and must not sleep.
> 
> terminate_all() only has to keep the callbacks of the terminated
> transfers from running. Mark the channel terminated, and skip the
> callbacks while it is marked. synchronize() does the rest:
> 
>  idxd_dma_synchronize()
>  |-idxd_wq_drain()
>  |   The device has completed every descriptor.
>  |-synchronize_irq()
>  |   The IRQ thread has put back what it found still running.
>  |-idxd_flush_pending_descs()
>  |-synchronize_irq()
>  |   The IRQ thread is done with what it took before the flush.
>  \-Clear the mark.
> 
> The flush holds a wq_active reference, not wq->wq_lock:
> dma_release_channel() calls synchronize() with dma_list_mutex held, and
> idxd_dmaengine_drv_remove() takes dma_list_mutex under wq->wq_lock. The
> remove path waits for the wq_active references before it frees the
> descriptors.
> 
> Calling dma_async_issue_pending() between terminate_all() and
> synchronize() is undefined in the dmaengine API. idxd starts descriptors
> at tx_submit(), so this also relies on clients not submitting in that
> window: then the descriptors completed while the channel is marked were
> all submitted before the terminate.
> 
> Some users, like ntb_transport, terminate and release the channel
> without calling synchronize(). The mark is still cleared, as
> dma_release_channel() calls synchronize() itself.
> 
> Fixes: 2a93f5747d0e ("dmaengine: idxd: Flush all pending descriptors")
> Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>

Reviewed-by: Dave Jiang <dave.jiang@intel.com>

> ---
>  drivers/dma/idxd/device.c |  9 ++++-----
>  drivers/dma/idxd/dma.c    | 47 ++++++++++++++++++++++++++++++++++++++---------
>  drivers/dma/idxd/idxd.h   |  3 +++
>  3 files changed, 45 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/dma/idxd/device.c b/drivers/dma/idxd/device.c
> index 21a0c32325ea..91cad6b0f955 100644
> --- a/drivers/dma/idxd/device.c
> +++ b/drivers/dma/idxd/device.c
> @@ -1269,7 +1269,7 @@ int idxd_device_load_config(struct idxd_device *idxd)
>  	return 0;
>  }
>  
> -static void idxd_flush_pending_descs(struct idxd_irq_entry *ie)
> +void idxd_flush_pending_descs(struct idxd_irq_entry *ie)
>  {
>  	struct idxd_desc *desc, *itr;
>  	struct llist_node *head;
> @@ -1293,10 +1293,9 @@ static void idxd_flush_pending_descs(struct idxd_irq_entry *ie)
>  		list_del(&desc->list);
>  		ctype = desc->completion->status ? IDXD_COMPLETE_NORMAL : IDXD_COMPLETE_ABORT;
>  		/*
> -		 * wq is being disabled. Any remaining descriptors are
> -		 * likely to be stuck and can be dropped. callback could
> -		 * point to code that is no longer accessible, for example
> -		 * if dmatest module has been unloaded.
> +		 * No callbacks: the transfers were terminated, or the WQ
> +		 * is being disabled or reset and they may point to
> +		 * unloaded code.
>  		 */
>  		tx = &desc->txd;
>  		tx->callback = NULL;
> diff --git a/drivers/dma/idxd/dma.c b/drivers/dma/idxd/dma.c
> index 9937b671f637..f8ce06274ef4 100644
> --- a/drivers/dma/idxd/dma.c
> +++ b/drivers/dma/idxd/dma.c
> @@ -7,17 +7,20 @@
>  #include <linux/device.h>
>  #include <linux/io-64-nonatomic-lo-hi.h>
>  #include <linux/dmaengine.h>
> +#include <linux/interrupt.h>
>  #include <uapi/linux/idxd.h>
>  #include "../dmaengine.h"
>  #include "registers.h"
>  #include "idxd.h"
>  
> -static inline struct idxd_wq *to_idxd_wq(struct dma_chan *c)
> +static inline struct idxd_dma_chan *to_idxd_dma_chan(struct dma_chan *c)
>  {
> -	struct idxd_dma_chan *idxd_chan;
> +	return container_of(c, struct idxd_dma_chan, chan);
> +}
>  
> -	idxd_chan = container_of(c, struct idxd_dma_chan, chan);
> -	return idxd_chan->wq;
> +static inline struct idxd_wq *to_idxd_wq(struct dma_chan *c)
> +{
> +	return to_idxd_dma_chan(c)->wq;
>  }
>  
>  void idxd_dma_complete_txd(struct idxd_desc *desc,
> @@ -47,7 +50,8 @@ void idxd_dma_complete_txd(struct idxd_desc *desc,
>  	if (complete && tx->cookie) {
>  		dma_cookie_complete(tx);
>  		dma_descriptor_unmap(tx);
> -		dmaengine_desc_get_callback_invoke(tx, &res);
> +		if (!READ_ONCE(to_idxd_dma_chan(tx->chan)->terminated))
> +			dmaengine_desc_get_callback_invoke(tx, &res);
>  		tx->callback = NULL;
>  		tx->callback_result = NULL;
>  	}
> @@ -196,18 +200,43 @@ static void idxd_dma_release(struct dma_device *device)
>  
>  static int idxd_dma_terminate_all(struct dma_chan *c)
>  {
> -	struct idxd_wq *wq = to_idxd_wq(c);
> -
> -	idxd_wq_flush_descs(wq);
> +	/* The device still runs them, synchronize() waits for it */
> +	WRITE_ONCE(to_idxd_dma_chan(c)->terminated, true);
>  
>  	return 0;
>  }
>  
>  static void idxd_dma_synchronize(struct dma_chan *c)
>  {
> -	struct idxd_wq *wq = to_idxd_wq(c);
> +	struct idxd_dma_chan *idxd_chan = to_idxd_dma_chan(c);
> +	struct idxd_wq *wq = idxd_chan->wq;
>  
>  	idxd_wq_drain(wq);
> +	if (!READ_ONCE(idxd_chan->terminated))
> +		return;
> +
> +	/*
> +	 * Not wq_lock: dma_release_channel() calls this under dma_list_mutex,
> +	 * which the WQ removal takes under wq_lock. The removal waits for
> +	 * wq_active instead.
> +	 */
> +	if (!percpu_ref_tryget_live(&wq->wq_active)) {
> +		wait_for_completion(&wq->wq_resurrect);
> +		/* The WQ is quiesced, disabling it flushes them */
> +		if (!percpu_ref_tryget_live(&wq->wq_active))
> +			return;
> +	}
> +
> +	/*
> +	 * The IRQ thread can still put back the descriptors it saw running
> +	 * before the drain, or complete the ones it took: wait for it on both
> +	 * sides of the flush.
> +	 */
> +	synchronize_irq(wq->ie.vector);
> +	idxd_flush_pending_descs(&wq->ie);
> +	synchronize_irq(wq->ie.vector);
> +	WRITE_ONCE(idxd_chan->terminated, false);
> +	percpu_ref_put(&wq->wq_active);
>  }
>  
>  int idxd_register_dma_device(struct idxd_device *idxd)
> diff --git a/drivers/dma/idxd/idxd.h b/drivers/dma/idxd/idxd.h
> index f4aed90560e0..3d0747be193a 100644
> --- a/drivers/dma/idxd/idxd.h
> +++ b/drivers/dma/idxd/idxd.h
> @@ -185,6 +185,8 @@ enum idxd_op_type {
>  struct idxd_dma_chan {
>  	struct dma_chan chan;
>  	struct idxd_wq *wq;
> +	/* Set by terminate_all(), until synchronize() */
> +	bool terminated;
>  };
>  
>  struct idxd_wq {
> @@ -803,6 +805,7 @@ void idxd_wq_quiesce(struct idxd_wq *wq);
>  int idxd_wq_init_percpu_ref(struct idxd_wq *wq);
>  void idxd_wq_free_irq(struct idxd_wq *wq);
>  int idxd_wq_request_irq(struct idxd_wq *wq);
> +void idxd_flush_pending_descs(struct idxd_irq_entry *ie);
>  void idxd_wq_flush_descs(struct idxd_wq *wq);
>  void idxd_wq_release_int_handle(struct idxd_wq *wq);
>  
> 


      parent reply	other threads:[~2026-11-30 17:18 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 23:48 [PATCH v2 0/8] dmaengine: idxd: More error handling fixes Vinicius Costa Gomes
2026-11-25 23:48 ` [PATCH v2 1/8] dmaengine: idxd: Do not call destroy_workqueue() with null idxd->wq Vinicius Costa Gomes
2026-11-25 23:49 ` [PATCH v2 2/8] dmaengine: idxd: Fix potential double free on probe error path Vinicius Costa Gomes
2026-11-25 23:49 ` [PATCH v2 3/8] dmaengine: idxd: fix duplicate memory frees on initialization " Vinicius Costa Gomes
2026-11-25 23:49 ` [PATCH v2 4/8] dmaengine: idxd: Fix the device registration error paths Vinicius Costa Gomes
2026-11-30 17:03   ` Dave Jiang
2026-11-25 23:49 ` [PATCH v2 5/8] dmaengine: idxd: Fix error handling in idxd_cdev_open() Vinicius Costa Gomes
2026-11-30 17:13   ` Dave Jiang
2026-11-25 23:49 ` [PATCH v2 6/8] dmaengine: idxd: Initialize the WQ XArray before the WQ can be released Vinicius Costa Gomes
2026-11-30 17:14   ` Dave Jiang
2026-11-25 23:49 ` [PATCH v2 7/8] dmaengine: idxd: Do not release interrupt handles on terminate_all() Vinicius Costa Gomes
2026-11-30 17:15   ` Dave Jiang
2026-11-25 23:49 ` [PATCH v2 8/8] dmaengine: idxd: Don't flush the descriptors " Vinicius Costa Gomes
2026-11-26  0:19   ` sashiko-bot
2026-11-30 17:18   ` Dave Jiang [this message]

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=a4c702ff-2095-4a80-a32c-548c70646eb8@intel.com \
    --to=dave.jiang@intel.com \
    --cc=Frank.Li@kernel.org \
    --cc=dmaengine@vger.kernel.org \
    --cc=fenghuay@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=vinicius.gomes@intel.com \
    --cc=vkoul@kernel.org \
    --cc=xueshuai@linux.alibaba.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.