From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.10 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796059093; b=oRJPRiXPekJ+nSzRTsSTriFQe5xV4u1UENaZKoYghFwLYPDi8Z6KzKYC6tzALQ7SgiRV 9OxABQrZauAkLwCQxKRNxTLiFfRPDrzySXbYoqfeO40lPeZCy79Zx9X1wjti25iWlnsPB 58qdextdR57oqs3gAFea8lDJqHy4rsRLuRJGX9c6KjNqfuzFrsxs61FngIdWmLNOBFFb5 SIoQZQOPVE7iwoAN1Ycjuz6ZXaeR4F8HEh2cM8EY2rK7UAPg7QBEJ6ZJUMtF6ovzFE0v/ rPLcHZ0A5cBRVAuo//il9Nm12EWFLwl3FDSYRHKe00jReBKyHq4BcnULEV2A5HuIx9Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796059093; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Message-ID:Date: MIME-Version:User-Agent:Subject:To:Cc:References:From: Content-Language:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=u6+cPYxvLyI8cPFWOmetLSl5OO8vPSrhGl02kHgwWR4=; b=Jxg7sErgYktPdcjmiAI7glYmRP7k4X1egrq0n612gxd/UGo//a29e9OCPlkhJFmF4FXp MTguv1XxF2Kc6iL+cw58dlGbutOXcq0MaWUqiM3+5c/UV8ltqoOEY0GjleghhKvG96WPh 3mmZ7RQpRzPhrU1+POmQ4u1uCj+YBYBL09coPA6qlgNGwYwyp//BC+i1Km3L6I7qPaBSH sKFr44onTM9f779zIQ0GCE+5rI4psl7UlJfN2ebZPHS4LsY94f0vjH/u01GUQU/oy05Mx GQxnQaYf0jIcOfA3zUr4fiaWJ/wUrm2tqxqsk8sGCezz5G2UQoKln4ZdnROPOHEp5nA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=BIwwIR2w; arc=none smtp.remote-ip=198.175.65.10 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ADCF8606A320; Mon, 30 Nov 2026 17:18:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org ADCF8606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=BIwwIR2w DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796059093; x=1827595093; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=u6+cPYxvLyI8cPFWOmetLSl5OO8vPSrhGl02kHgwWR4=; b=BIwwIR2waPiGPxze48oSxLc30nohwNIMzdMPxZ3ZU+xHQqTooIMd5EL0 Xu/z3pK2dwNUOwfVAMCImflcxXIRNeRjapmnV72+VuHUHKhsR0ncvf2QW n1vMIJpdNPAKBuFSgrUAp8AZEsOOdkeawkevINOxEJh35z/b0kGOPrHGo p59wPIn9wQPFPgAjPF0FrlRnjU3z0DQz/mvQ8mOmph+m8VFYmLIazEY39 hZjV60ihAWBKWdvrH5Wr1LaLsNA6XeCCY17JIMQ5072dUqQWAIGr6Za0/ jxCbA5lkaAAW6ctyVUjFdGPuPJdbSP344T99jLbohGQk/k519xV+TN24v A==; X-CSE-ConnectionGUID: ws2rs4BxRqWwydPXbdrdSg== X-CSE-MsgGUID: Eu0/pDvITQi7cZ7Ti26bCA== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4836431" X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="4836431" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 09:18:12 -0800 X-CSE-ConnectionGUID: 8haQfrlyQImFvoOHDBb70g== X-CSE-MsgGUID: cW/2o7SrS56G0GQjqNTQVQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="12976692" Received: from cooperst-gp92.amr.corp.intel.com (HELO [10.125.110.218]) ([10.125.110.218]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 09:18:12 -0800 Message-ID: Date: Mon, 30 Nov 2026 10:18:10 -0700 X-Mailing-List: dmaengine@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 8/8] dmaengine: idxd: Don't flush the descriptors on terminate_all() To: Vinicius Costa Gomes , Vinod Koul , Frank Li , Fenghua Yu , Shuai Xue Cc: dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org References: <20261125-wt-idxd-fixes-v2-0-25a4691dfb9e@intel.com> <20261125-wt-idxd-fixes-v2-8-25a4691dfb9e@intel.com> From: Dave Jiang Content-Language: en-US In-Reply-To: <20261125-wt-idxd-fixes-v2-8-25a4691dfb9e@intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 Reviewed-by: Dave Jiang > --- > 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 > #include > #include > +#include > #include > #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); > >