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=1796322769; b=pwzwi0MTk//z9bjxLljuyXtduYIUc6xkXVwYs0il0GchJerIOKSwv7tGj60vZSR9Uwi4 m/NRemwggUmrzCRl6+lYaxrnjscDD/0udSbQuKXxlrqhILgGfbkGMgFvsVtU8dvc8pwXH WuiFocbP+MyWUd0IcA7pRZUChVR32piigDJCfSI9C4zab+sFEzPJPRepxGQyA6WNqhBId 6iMYDbOqZHHQUjZHwuGw27SyFGcLRfiimX5hNVVfSBuaBA4JcrtIae7TZEZHOsvff89v1 dfQZfq4cSvXddiSadrc52YQZaYEXqO0FQX3uunlwibPdmNSH8n8uCgvrKGOKi1rZsoA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796322769; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=mEm9P6s4g+XsCQTSAP730sDh2l6vCK3tmuWBBGdSh+U=; b=KivTFoxL335JhipumsRdUKHWfqTWccYH4neBXX2sp7BE/If2ysJ67MP6n/FRv/lXIlEN QmCTWkI7GTloE/AjfnTe2wmjt+mctqi/ljGn+a/RW6qyO9jalBm7FVqpROYNcZkSxgJ6T 4ANFOzkaZgfyZdzbjqz33ODdXyKOeGcJ8UAk+htPNE4mJLnS+wa9EZ12rezEJi+N3cl7s IUWMiBbeoxZSQ3lshiu7NGxhNT3/jp/FEEgmC1ZeAfXo/aCwKpPGXWGJlF06BduMoHjq4 W/oAUudWk1QktyY8NDZ38iQENVpRkUGy2/s2k+kVv/Kus09xB6oMsKJ8V8bO4jBwKEQ== 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=GPudxakQ; 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 5DE8C606A332; Thu, 3 Dec 2026 18:32:49 +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=GPudxakQ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8B9401F0089B; Thu, 3 Dec 2026 18:32:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796322769; bh=mEm9P6s4g+XsCQTSAP730sDh2l6vCK3tmuWBBGdSh+U=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=GPudxakQXmHVyMEmvtdXZ4HJhu2z45OtQM+q2uPa0WZS6KKWdDE6gTRmkONfW8luo gRV0iuyK7vFFhZKs5J4Y5jXTOjhZFdI56q45mDf4/iWF3ag2FZFxtuMnd8sxdbBcDT 4OM2mxjWIfLdozokEhxFSHrzxsFuKLRGlixnofrFy8GHSx4V8wuJ+UhSKODrDDB/SW zbs1aTqYxTdqQt0zrzEyYssClVG7jyhdF9phBB5W9bUowzv9wprN0JP+PFGXgEIQVl Q/M74c/sJfGb79pkiZETTEGrMuvj1EXRh2wUJVHVh51N6qp14pRSNYNbNiyL/rTeA/ uV7AUdvU8XguA== From: Lee Jones To: lee@kernel.org, Wolfram Sang , Andi Shyti , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Philipp Stanner , Carlos Munoz , =?UTF-8?q?Niklas=20S=C3=B6derlund?= , linux-renesas-soc@vger.kernel.org, linux-i2c@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH 03/10] i2c: sh_mobile: Terminate DMA before freeing bounce buffer in sh_mobile_xfer() Date: Thu, 3 Dec 2026 18:31:07 +0000 Message-ID: <20261203183214.1939736-4-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-media@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 In sh_mobile_xfer(), i2c_put_dma_safe_msg_buf() is called immediately after wait_event_timeout(), before checking for a timeout or an active DMA transfer and calling sh_mobile_i2c_cleanup_dma(). On a timeout or early NACK wakeup while DMA is still running, the bounce buffer is freed while still mapped for DMA. Select the DMA channel from the immutable message direction flag rather than mutable pd->dma_direction, always synchronise and terminate the channel in sh_mobile_xfer() before calling i2c_put_dma_safe_msg_buf(), re-check pd->dma_direction after dmaengine_terminate_sync() to avoid double unmapping, and update pd->pos, pd->stop_after_dma, and ICIC before clearing pd->dma_direction in sh_mobile_i2c_dma_callback(). Fixes: cebc07d84ad7 ("i2c: sh_mobile: fix leak when using DMA bounce buffer") Closes: https://sashiko.dev/#/bug/linux-af86edab-c752-40c1-9d09-2abb42f140f3 Cc: stable@vger.kernel.org Signed-off-by: Lee Jones --- drivers/i2c/busses/i2c-sh_mobile.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/drivers/i2c/busses/i2c-sh_mobile.c b/drivers/i2c/busses/i2c-sh_mobile.c index 0d10742a8ec0..d7aa710767c7 100644 --- a/drivers/i2c/busses/i2c-sh_mobile.c +++ b/drivers/i2c/busses/i2c-sh_mobile.c @@ -445,12 +445,17 @@ static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id) static void sh_mobile_i2c_cleanup_dma(struct sh_mobile_i2c_data *pd, bool terminate) { - struct dma_chan *chan = pd->dma_direction == DMA_FROM_DEVICE + struct dma_chan *chan = (pd->msg->flags & I2C_M_RD) ? pd->dma_rx : pd->dma_tx; /* only allowed from thread context! */ - if (terminate) + if (terminate) { + if (IS_ERR_OR_NULL(chan)) + return; dmaengine_terminate_sync(chan); + if (pd->dma_direction == DMA_NONE) + return; + } dma_unmap_single(chan->device->dev, sg_dma_address(&pd->sg), pd->msg->len, pd->dma_direction); @@ -462,11 +467,10 @@ static void sh_mobile_i2c_dma_callback(void *data) { struct sh_mobile_i2c_data *pd = data; - sh_mobile_i2c_cleanup_dma(pd, false); pd->pos = pd->msg->len; pd->stop_after_dma = true; - iic_set_clr(pd, ICIC, 0, ICIC_TDMAE | ICIC_RDMAE); + sh_mobile_i2c_cleanup_dma(pd, false); } static struct dma_chan *sh_mobile_i2c_request_dma_chan(struct device *dev, @@ -682,15 +686,15 @@ static int sh_mobile_xfer(struct sh_mobile_i2c_data *pd, pd->sr & (ICSR_TACK | SW_DONE), pd->adap.timeout); + if (pd->dma_buf) + sh_mobile_i2c_cleanup_dma(pd, true); + /* 'stop_after_dma' tells if DMA xfer was complete */ i2c_put_dma_safe_msg_buf(pd->dma_buf, pd->msg, pd->stop_after_dma); } if (!time_left) { - if (pd->dma_direction != DMA_NONE) - sh_mobile_i2c_cleanup_dma(pd, true); - err = -ETIMEDOUT; break; } -- 2.56.1.911.g883d8bcb20-goog