From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.19 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796068704; b=5bQkPc3Vgre4WhDFvghWWs6n/aqSIOh1SmpU6yKifLUmnpEGJ7Wjx6PcKkZDwwQFtETo O4hlG76PWfhvocCuC+ZCvOm+A0hjjXynKdHcgNO85pUjmwQifboHIty5TK2GTf/CfMSPy wbnnJpagXKbG4DApYKFk8rwlXcj8efP2A+6tjhuAlxmavFi05744p1kghVmjml0BM1wlh iPIouxD3Lx3MEdBb04z42Hepr78qqQGObW1GbPyF/UUEM1dBXYqjpN2QL/BNUVGLGMtKY FqLS41vu9u76EEGQr77C8Gek623UT4foutVr6NU3AfQlDGl0qM5dhCYdSPNQVffFeAg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796068704; 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:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=vR3G4wL+yuyWaaynQVILC2tPkwrkYGoQNxb8bAbuDn8=; b=1hYWyz3wPVjzUM5t1NqwRLbTXchy4oCcAE21KaU3WlYVoN7op1rNK4bzk1ANiH8ekLpO Nz6lJhnv8zaMP2YqWFt0RLZg4XyEgp62A7FXNY2E0YlQP7Ou9ULySGhb5sdbrQLmdmH2T ilwtv3JQCu/kbZBNKOE+4ZeAMhKiGU1CeeDpITghHmYTVU+Y2Bq3VLP3KmCd9Hahu7Zl9 OvpLk27QZXmokZrtaVdtm811MzTvrwdXaR9F7h4F5mbhGSvvOlgFVrxsHJ3jTGHxxyuBw rWNZ+twV+LdkNue3XiKsh+PUEfwBfhUAtrDuKwyCgoeLKhsBujgw8LLWnLrV6narMXw== 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=NbGJg3wD; arc=none smtp.remote-ip=192.198.163.19 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.19]) (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 8628A606A32E; Mon, 30 Nov 2026 19:58:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8628A606A32E 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) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=NbGJg3wD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796068704; x=1827604704; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vR3G4wL+yuyWaaynQVILC2tPkwrkYGoQNxb8bAbuDn8=; b=NbGJg3wDMea9VDxYTol0BGjI9lWL7PU1Xe3ouKnsBM9NaLxdAffNmXr8 DxMKBED+DNujdczTmlgGGBCTMPavYK9VNMbISfImDgdl15laLgTLVWTf5 k0dCc8B3aEiYlDq7pGQheW1MxUdcPZZLGQxmNCQgkTQON2sgGtsRUWU6n LV2JeFjhePyTHWgsZvuMPg4G8Q947ZsWGvY4dbIXXMtFJTL1ymfB4H6f1 DshBRHQWJhKRIy9p4WzHh0+l4uCOFY44SOCVXgF9XeB6xNHHtakgKEEsM zn0NWoCkKNwzXnyTlmcEWYzmQwGQ3MqkOoKEeKiy8+PgB/DKsBIB7cshD w==; X-CSE-ConnectionGUID: tUZyq16NTqWVFKvLZuHU0g== X-CSE-MsgGUID: aVoPRBdAT8mDY3a3MlBE7A== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4936895" X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="4936895" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by fmvoesa113.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 11:58:22 -0800 X-CSE-ConnectionGUID: utiVHlgWSvGCi1fkaNJCdA== X-CSE-MsgGUID: XuhaokQJSW+VXbpbajAMMw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="4517790" Received: from jf5300-15a101203tc.jf.intel.com (HELO meta-crater2c.jf.intel.com) ([10.54.113.49]) by orviesa010.jf.intel.com with ESMTP; 30 Nov 2026 11:58:22 -0800 From: Daniel Byrne To: Vinod Koul , dmaengine@vger.kernel.org Cc: Vinicius Costa Gomes , Dave Jiang , Frank Li , Dan Williams , Ruoyu Wang , Zhejun Huang , Changyul Lee , linux-kernel@vger.kernel.org, Daniel Byrne , stable@vger.kernel.org Subject: [PATCH v2 2/4] dmaengine: idxd: list a new channel only once it is complete Date: Mon, 30 Nov 2026 11:52:01 -0800 Message-ID: <20261130195203.1901143-3-daniel1.byrne@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261130195203.1901143-1-daniel1.byrne@intel.com> References: <20261130195203.1901143-1-daniel1.byrne@intel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit idxd_register_dma_channel() links a new channel into the dma_device's channel list before it registers the channel, and sets the channel's work queue only after that. The dma_device is registered when the device is enabled, before any of its work queues, and a channel request walks the channel list of every registered device under dma_list_mutex and may hand out any listed channel without a client. A request that runs while a work queue is being enabled can therefore take the half-built channel, and the allocation callback dereferences its NULL work queue with dma_list_mutex held. After that every channel request and release hangs. The link runs without that mutex as well, so it races the walkers. When the registration fails, the channel is unlinked again before it is freed, since "dmaengine: idxd: Remove channel from list on registration failure" [1]. Until then it is on the list like any other, where a request can take the channel that is about to be freed, and the unlink runs without the mutex as the link does. Add dma_async_device_channel_add(), which registers a channel and only then links it into its device's list, under dma_list_mutex, and use it. The driver sets the channel's work queue and initializes its descriptors before the call, so a request finds the channel complete or not at all, and a channel that failed to register is never listed. An enable whose registration fails still fails, and has nothing to unlink, so drop the list_del() that [1] added on the error path. This is a new helper rather than a change to dma_async_device_channel_register(), whose contract requires the caller to have linked the channel into the device's list already. virt-dma's vchan_init() does that for stm32-dma3, the only other caller. That driver has the same window between the link and the registration and is left for a follow-up. The helper serves DMA_PRIVATE devices such as idxd's only. The channel list of a public device is also walked under RCU alone, and its channels hold references for the dmaengine_get() users, which the helper does not take. The unbind still unlinks the channel without the mutex, which needs a core helper that unlinks under it. The hang is reachable since the dma_device registration moved to the device enable in v5.13, so this is for stable too. Fixes: 8f47d1a5e545 ("dmaengine: idxd: connect idxd to dmaengine subsystem") Fixes: 397862855619 ("dmaengine: idxd: fix dma device lifetime") Cc: stable@vger.kernel.org # 5.13+ Signed-off-by: Daniel Byrne Link: https://lore.kernel.org/r/20260709062303.4167624-1-ruoyuw560@gmail.com [1] --- v2: dma_async_device_channel_add() is declared in drivers/dma/dmaengine.h, next to dma_async_device_channel_register() on the new base, instead of include/linux/dmaengine.h; the helper itself is unchanged. idxd_register_dma_channel() drops the list_del() that "dmaengine: idxd: Remove channel from list on registration failure" added to its error path, since a channel that failed to register is never listed. The message describes the failed registration as it is after that patch, with a Link to it, and no longer describes the -EEXIST id reuse; of the unbind's unlink without the mutex it now says that this needs a core helper, where v1 pointed at a later rework of the unbind. drivers/dma/dmaengine.c | 41 +++++++++++++++++++++++++++++++++++++++++ drivers/dma/dmaengine.h | 2 ++ drivers/dma/idxd/dma.c | 13 +++++++++---- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c index b1cb466f376f..1d8d2949a810 100644 --- a/drivers/dma/dmaengine.c +++ b/drivers/dma/dmaengine.c @@ -1164,6 +1164,47 @@ int dma_async_device_channel_register(struct dma_device *device, } EXPORT_SYMBOL_GPL(dma_async_device_channel_register); +/** + * dma_async_device_channel_add - register a channel and make it available + * @device: the dmaengine device, DMA_PRIVATE and registered already + * @chan: the channel, not on @device's channel list + * @name: the channel's name, or NULL for the default one + * + * Channel requests walk the channel list of every registered device under + * dma_list_mutex and may hand out any listed channel that has no client. + * A channel added to a registered device is therefore linked to the list + * only once it is registered, and under that mutex, so that a request + * never finds it half initialized. The driver's own state behind the + * channel must be complete before the call. A channel that fails to + * register is not linked. + * + * Only a DMA_PRIVATE device may add channels this way. The channel list + * of a public device is also walked under RCU alone, and its channels + * hold references for the dmaengine_get() users, which this function + * does not take. + * + * Return: 0 on success or a negative errno. + */ +int dma_async_device_channel_add(struct dma_device *device, + struct dma_chan *chan, const char *name) +{ + int rc; + + if (WARN_ON_ONCE(!dma_has_cap(DMA_PRIVATE, device->cap_mask))) + return -EINVAL; + + rc = __dma_async_device_channel_register(device, chan, name); + if (rc < 0) + return rc; + + mutex_lock(&dma_list_mutex); + list_add_tail(&chan->device_node, &device->channels); + dma_channel_rebalance(); + mutex_unlock(&dma_list_mutex); + return 0; +} +EXPORT_SYMBOL_GPL(dma_async_device_channel_add); + static void __dma_async_device_channel_unregister(struct dma_device *device, struct dma_chan *chan) { diff --git a/drivers/dma/dmaengine.h b/drivers/dma/dmaengine.h index 42a3cfa7e597..371509b26e6b 100644 --- a/drivers/dma/dmaengine.h +++ b/drivers/dma/dmaengine.h @@ -208,6 +208,8 @@ static inline bool is_slave_direction(enum dma_transfer_direction direction) int dma_async_device_register(struct dma_device *device); int dmaenginem_async_device_register(struct dma_device *device); void dma_async_device_unregister(struct dma_device *device); +int dma_async_device_channel_add(struct dma_device *device, + struct dma_chan *chan, const char *name); int dma_async_device_channel_register(struct dma_device *device, struct dma_chan *chan, const char *name); diff --git a/drivers/dma/idxd/dma.c b/drivers/dma/idxd/dma.c index f2c03f3cf192..6293842689fb 100644 --- a/drivers/dma/idxd/dma.c +++ b/drivers/dma/idxd/dma.c @@ -278,7 +278,7 @@ static int idxd_register_dma_channel(struct idxd_wq *wq) chan = &idxd_chan->chan; chan->device = dma; - list_add_tail(&chan->device_node, &dma->channels); + idxd_chan->wq = wq; for (i = 0; i < wq->num_descs; i++) { struct idxd_desc *desc = wq->descs[i]; @@ -287,15 +287,20 @@ static int idxd_register_dma_channel(struct idxd_wq *wq) desc->txd.tx_submit = idxd_dma_tx_submit; } - rc = dma_async_device_channel_register(dma, chan, NULL); + /* + * The dma_device is registered already, so a channel request may + * take the channel as soon as it is on the device's list. The core + * links it only once it is registered, and everything a client + * reaches through it is set up by then. What follows serves the + * unbind, which cannot run before this probe returns. + */ + rc = dma_async_device_channel_add(dma, chan, NULL); if (rc < 0) { - list_del(&chan->device_node); kfree(idxd_chan); return rc; } wq->idxd_chan = idxd_chan; - idxd_chan->wq = wq; get_device(wq_confdev(wq)); return 0; -- 2.43.0