From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02C5E606A337 for ; Thu, 3 Dec 2026 18:33:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=tsSwkLvF; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=DK+zr27m DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=OhNv9ORobTGSPRuyeQPvZN3ynaJZjKFHKP9ZgkaMrBI=; b=tsSwkLvFQfXQcK IO4ek4PB/6GkjG/ZXbzUCp/x3XjEvC3biXhxYq8M17ybVDFOx12qt4/oKzIAlFVNbGsb6sCxsN/LP OTVqvApeD+HvlyXg434QBCIhrIwJQx6JFhteFaEo7cUtpY39vvTf5yxoxdhRQq+r5gAqWvgtus3tI v4guFK+G8eksMkpnRrtUr7LhdQPeMAVZ4JfUK6u/+k2r64MJO/pJgzcW0DCXObwlJjTENAihFH26y wFa9nN9HlAKoh7SL9PyOG/Hr/sYbtiuR0qCWJfEjO+AN6xHBXghSYfSfUQgDUrib6anq24vsa9JmH Ggqkqj0BR87qni0vZZHg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZBcA-0000000FaKV-0YwW; Thu, 03 Dec 2026 18:33:07 +0000 Received: from tor.source.kernel.org ([172.105.4.254]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZBc8-0000000FaKK-2asR for linux-arm-kernel@lists.infradead.org; Thu, 03 Dec 2026 18:33:04 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 14BED628C3; Thu, 3 Dec 2026 18:33:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A8DFD1F00899; Thu, 3 Dec 2026 18:33:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796322783; bh=OhNv9ORobTGSPRuyeQPvZN3ynaJZjKFHKP9ZgkaMrBI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=DK+zr27m8RqteOHl4WUImiV/XiD5Jbc/AggbcSl5CsFD4FcGEHwaxT3DlYfUMSpTA MRLPg/4bAtO0f5X8vUtqxwDuqUY/eh2AiYP5Smd2f09fwllIby9G9iR7FGkwKp7Ob6 dqcGfeZMLlMg09kIkOkbRzdD8961iIz706QnzYOvr9NqC2TeYhmluGedNkMjIPjAoO oo0llkh4r1FuIWEjDEuGxVdgzleyw4qQjBS+qfQvzfU8nRsVBHYI+6XWlDb6KcHNkz ljg/lWmsAAFpgIuuUURo8lVgMRsQ5veosbi3E6DmSk9u1/k+fviXqbqtfoWiAWLmzK mIcjxwRSDlu4g== From: Lee Jones To: lee@kernel.org, Pierre-Yves MORDRET , Alain Volmat , Andi Shyti , Maxime Coquelin , Alexandre Torgue , Cedric Madianga , =?UTF-8?q?Cl=C3=A9ment=20Le=20Goffic?= , linux-i2c@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH 05/10] i2c: stm32f7: Prevent duplicate execution of stm32f7_i2c_dma_callback() Date: Thu, 3 Dec 2026 18:31:09 +0000 Message-ID: <20261203183214.1939736-6-lee@kernel.org> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog In-Reply-To: <20261203183214.1939736-1-lee@kernel.org> References: <20261203183214.1939736-1-lee@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 stm32f7_i2c_dma_callback() can be invoked both by the DMA engine upon transfer completion and directly by the threaded IRQ handler on NACK, error, or timeout conditions. When these race, dma_unmap_single() and i2c_put_dma_safe_msg_buf() execute twice on the same buffer, causing double DMA unmap and double free. Guard stm32f7_i2c_dma_callback() with an atomic dma_cleanup_pending flag so cleanup runs at most once per prepared DMA transfer, and route threaded IRQ error/timeout paths through a helper that calls dmaengine_terminate_sync() first so any in-flight DMA callback has completed before proceeding or advancing to the next message. Fixes: 6aae87fe7f18 ("i2c: stm32f7: unmap DMA mapped buffer") Closes: https://sashiko.dev/#/bug/linux-ec58601d-83aa-4888-8bad-aa0356a7ae85 Cc: stable@vger.kernel.org Signed-off-by: Lee Jones --- drivers/i2c/busses/i2c-stm32f7.c | 35 +++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/drivers/i2c/busses/i2c-stm32f7.c b/drivers/i2c/busses/i2c-stm32f7.c index 2e1cc6a92b81..1ebb2e0bc1b0 100644 --- a/drivers/i2c/busses/i2c-stm32f7.c +++ b/drivers/i2c/busses/i2c-stm32f7.c @@ -14,6 +14,7 @@ * This driver is based on i2c-stm32f4.c * */ +#include #include #include #include @@ -320,6 +321,7 @@ struct stm32f7_i2c_alert { * slave) * @dma: dma data * @use_dma: boolean to know if dma is used in the current transfer + * @dma_cleanup_pending: tracks whether a DMA transfer awaits cleanup * @regmap: holds SYSCFG phandle for Fast Mode Plus bits * @fmp_sreg: register address for setting Fast Mode Plus bits * @fmp_creg: register address for clearing Fast Mode Plus bits @@ -353,6 +355,7 @@ struct stm32f7_i2c_dev { bool master_mode; struct stm32_i2c_dma *dma; bool use_dma; + atomic_t dma_cleanup_pending; struct regmap *regmap; u32 fmp_sreg; u32 fmp_creg; @@ -735,6 +738,9 @@ static void stm32f7_i2c_dma_callback(void *arg) struct stm32_i2c_dma *dma = i2c_dev->dma; struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg; + if (!atomic_xchg(&i2c_dev->dma_cleanup_pending, 0)) + return; + stm32f7_i2c_disable_dma_req(i2c_dev); dmaengine_terminate_async(dma->chan_using); dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len, @@ -744,6 +750,12 @@ static void stm32f7_i2c_dma_callback(void *arg) complete(&dma->dma_complete); } +static void stm32f7_i2c_dma_abort(struct stm32f7_i2c_dev *i2c_dev) +{ + dmaengine_terminate_sync(i2c_dev->dma->chan_using); + stm32f7_i2c_dma_callback(i2c_dev); +} + static void stm32f7_i2c_hw_config(struct stm32f7_i2c_dev *i2c_dev) { struct stm32f7_i2c_timings *t = &i2c_dev->timing; @@ -928,12 +940,14 @@ static void stm32f7_i2c_xfer_msg(struct stm32f7_i2c_dev *i2c_dev, dma_buf = i2c_get_dma_safe_msg_buf(msg, STM32F7_I2C_DMA_LEN_MIN); if (dma_buf) { f7_msg->buf = dma_buf; + atomic_set(&i2c_dev->dma_cleanup_pending, 1); ret = stm32_i2c_prep_dma_xfer(i2c_dev->dev, i2c_dev->dma, msg->flags & I2C_M_RD, f7_msg->count, f7_msg->buf, stm32f7_i2c_dma_callback, i2c_dev); if (ret) { + atomic_set(&i2c_dev->dma_cleanup_pending, 0); dev_warn(i2c_dev->dev, "can't use DMA\n"); i2c_put_dma_safe_msg_buf(f7_msg->buf, msg, false); f7_msg->buf = msg->buf; @@ -1102,15 +1116,18 @@ static int stm32f7_i2c_smbus_xfer_msg(struct stm32f7_i2c_dev *i2c_dev, /* Configure DMA or enable RX/TX interrupt */ i2c_dev->use_dma = false; if (i2c_dev->dma && f7_msg->count >= STM32F7_I2C_DMA_LEN_MIN) { + atomic_set(&i2c_dev->dma_cleanup_pending, 1); ret = stm32_i2c_prep_dma_xfer(i2c_dev->dev, i2c_dev->dma, cr2 & STM32F7_I2C_CR2_RD_WRN, f7_msg->count, f7_msg->buf, stm32f7_i2c_dma_callback, i2c_dev); - if (!ret) + if (!ret) { i2c_dev->use_dma = true; - else + } else { + atomic_set(&i2c_dev->dma_cleanup_pending, 0); dev_warn(i2c_dev->dev, "can't use DMA\n"); + } } if (!i2c_dev->use_dma) { @@ -1196,16 +1213,19 @@ static void stm32f7_i2c_smbus_rep_start(struct stm32f7_i2c_dev *i2c_dev) if (i2c_dev->dma && f7_msg->count >= STM32F7_I2C_DMA_LEN_MIN && f7_msg->size != I2C_SMBUS_BLOCK_DATA && f7_msg->size != I2C_SMBUS_BLOCK_PROC_CALL) { + atomic_set(&i2c_dev->dma_cleanup_pending, 1); ret = stm32_i2c_prep_dma_xfer(i2c_dev->dev, i2c_dev->dma, cr2 & STM32F7_I2C_CR2_RD_WRN, f7_msg->count, f7_msg->buf, stm32f7_i2c_dma_callback, i2c_dev); - if (!ret) + if (!ret) { i2c_dev->use_dma = true; - else + } else { + atomic_set(&i2c_dev->dma_cleanup_pending, 0); dev_warn(i2c_dev->dev, "can't use DMA\n"); + } } if (!i2c_dev->use_dma) @@ -1552,7 +1572,7 @@ static irqreturn_t stm32f7_i2c_handle_isr_errs(struct stm32f7_i2c_dev *i2c_dev, /* Disable dma */ if (i2c_dev->use_dma) - stm32f7_i2c_dma_callback(i2c_dev); + stm32f7_i2c_dma_abort(i2c_dev); i2c_dev->master_mode = false; complete(&i2c_dev->complete); @@ -1617,7 +1637,7 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data) __func__, f7_msg->addr); writel_relaxed(STM32F7_I2C_ICR_NACKCF, base + STM32F7_I2C_ICR); if (i2c_dev->use_dma) - stm32f7_i2c_dma_callback(i2c_dev); + stm32f7_i2c_dma_abort(i2c_dev); f7_msg->result = -ENXIO; } @@ -1635,8 +1655,9 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data) ret = wait_for_completion_timeout(&i2c_dev->dma->dma_complete, HZ); if (!ret) { dev_dbg(i2c_dev->dev, "<%s>: Timed out\n", __func__); - stm32f7_i2c_dma_callback(i2c_dev); + stm32f7_i2c_dma_abort(i2c_dev); f7_msg->result = -ETIMEDOUT; + f7_msg->stop = true; } } if (f7_msg->stop) { -- 2.56.1.911.g883d8bcb20-goog