From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796322784; b=UL8bWwdkau8rWDTtngLivYT282DPJhyDnCIY0eQQUjb3SCd6d7z2pKwhpFTHOr8mKHRz J51GM0QJm7M79FoB60YqoZfe5ymVMVRbrEda0EQVDUwfcof8YGlrnUcUeFjuXCq8AxuDm o1Swql9vfHUPHGP7/O+PENhzTKAeM5u/Dx+jtTZWKRdLRS49MwYciqlM6MH/CRAa9VwpV lSEVETzUWxihbo5eza6ZEdIVe2DrVCnrS8OvfA3K0iF3G8c33uY/yCkaoiTlyqupMvWk/ /AH/iccZFuqf6jTEDQzSZwWltx1TSLISh34pHAjFUpGILj8fC9w6DfRVWbYDvXCe/iw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796322784; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=OhNv9ORobTGSPRuyeQPvZN3ynaJZjKFHKP9ZgkaMrBI=; b=H/JVmZtnln7YomHa7XAUMv/YXQ0yab8fJ9jiV+k1qonX/IsRXmtxnxqIz7hBdwbTQE7v BIFr3l0N9Qxcods/nAnqVsKkOrt01TZbYM+FjKCsAKJufoTE11NQKNYp1Cxc4YMyb4s+N PTsbWmpRhjgK1z1h2Zhfe6+loJp1AwEx/bKEYyGFDV5ZdsY3AR7IweJDct+zrKn3QzvPD CSQwwzzD2l3T/OlVQnSp+lTACqwKA0xg9GNcFeYxcLFbecdjMdI7rWiljLmh8R/DJGEa7 4kVxqzokEyGqw8syZAJFK5XcqrEhalG0nCH96uTzIcyCXPXVHmmiIv4hz03mSHNfc8g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=DK+zr27m; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 255C1606A32C; Thu, 3 Dec 2026 18:33:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=DK+zr27m 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-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 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