| Message ID | 20260902-spi-sun4i-fixes-v1-1-19985ef75673@pengutronix.de (mailing list archive) |
|---|---|
| State | New |
| Headers |
Return-Path: <linux-sunxi+bounces-25487-sunxi=pue.re@lists.linux.dev> X-Original-To: noreply@patchwork.local Delivered-To: noreply@patchwork.local Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4]) by mxe881.netcup.net (Postfix) with ESMTPS id 807CA1C15D3 for <noreply@patchwork.local>; Wed, 2 Sep 2026 20:09:50 +0200 (CEST) Authentication-Results: mxe881; spf=pass (sender IP is 104.64.211.4) smtp.mailfrom=linux-sunxi+bounces-25487-noreply=patchwork.local@lists.linux.dev smtp.helo=sin.lore.kernel.org Received-SPF: pass (mxe881: domain of lists.linux.dev designates 104.64.211.4 as permitted sender) client-ip=104.64.211.4; envelope-from=linux-sunxi+bounces-25487-noreply=patchwork.local@lists.linux.dev; helo=sin.lore.kernel.org; Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1]) by sin.lore.kernel.org (Postfix) with ESMTP id 4765F3E4F6 for <noreply@patchwork.local>; Wed, 2 Sep 2026 15:36:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3DAFD4A484B; Wed, 2 Sep 2026 15:36:13 +0000 (UTC) X-Original-To: linux-sunxi@lists.linux.dev Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E081D3806CA for <linux-sunxi@lists.linux.dev>; Wed, 2 Sep 2026 15:36:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.200.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788363372; cv=none; b=t8qQGlbXQPxM6DZD8AJF7os9ssVlbomBgCi9Pyt3MGmn8jhASYa6x1CcGrZxjQ/A5xUaO6YRJcQEYW4L/p8r1WJ4GujJQC2yt5GxKt+3zQ8onuj8RsVReD0IwdunaOVXtDp2HtTcAdIqbqmZn2WdKN0tJLwsE8MN5j26WMG0nB8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788363372; c=relaxed/simple; bh=EQMkaOVTpT52GS7Jl1JQVmtzQ3gVPjCNyXibIxxmu6g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=F4ScfMi4wtgZoEgOdHNOkLmtTGPhbH8KMH+4JAc8ZxmcyytLtjjYI2u1nG7GFkOWBNjgRDlKpWEieUrInMFDtBG+UEkjKTUmWh1Jz+XZc+A/LnOLb7DfhedISrFc4OlwOuLyyNyUYd8JHjna+5M972TSn3i1Sbw8UljViEB6/tU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.200.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 034B9205882; Wed, 02 Sep 2026 17:35:54 +0200 (CEST) Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from <jre@pengutronix.de>) id 1x1n0D-004bxm-2z; Wed, 02 Sep 2026 17:35:53 +0200 From: Jonas Rebmann <jre@pengutronix.de> Date: Wed, 02 Sep 2026 17:35:43 +0200 Subject: [PATCH 1/5] spi: spi-sun4i: sun4i_spi_transfer_one(): fix transfers for SUN4I_FIFO_DEPTH Precedence: bulk X-Mailing-List: linux-sunxi@lists.linux.dev List-Id: <linux-sunxi.lists.linux.dev> List-Subscribe: <mailto:linux-sunxi+subscribe@lists.linux.dev> List-Unsubscribe: <mailto:linux-sunxi+unsubscribe@lists.linux.dev> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260902-spi-sun4i-fixes-v1-1-19985ef75673@pengutronix.de> References: <20260902-spi-sun4i-fixes-v1-0-19985ef75673@pengutronix.de> In-Reply-To: <20260902-spi-sun4i-fixes-v1-0-19985ef75673@pengutronix.de> To: Mark Brown <broonie@kernel.org>, Chen-Yu Tsai <wens@kernel.org>, Jernej Skrabec <jernej.skrabec@gmail.com>, Samuel Holland <samuel@sholland.org>, Maxime Ripard <mripard@kernel.org>, Alexandru Gagniuc <mr.nuke.me@gmail.com>, Olliver Schinagl <o.schinagl@ultimaker.com> Cc: linux-spi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org, Mark Brown <broonie@linaro.org>, kernel@pengutronix.de, Marc Kleine-Budde <mkl@pengutronix.de>, Jonas Rebmann <jre@pengutronix.de> X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1955; i=jre@pengutronix.de; h=from:subject:message-id; bh=wfQL1QSfCbWufR3wGkkwcQx1sGdHXFdbWwqElB1tnxg=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYsia4RTey9CwlVN3hsPVKXsmlU15f+dpgX2BVLCkrmPlh weSuzWLO0pZGMS4GGTFFFli1eQUhIz9r5tV2sXCzGFlAhnCwMUpABOJiWb4Z/Rdet99Q5ZP36+u LrumML+N/bDhZOd723L8f91W7/i4+Awjw5S+s7sELz4WlVurv6nX9l76pNZnSiv3n4ycu6Lv/y4 LDz4A X-Developer-Key: i=jre@pengutronix.de; a=openpgp; fpr=0B7B750D5D3CD21B3B130DE8B61515E135CD49B5 X-Rspamd-Server: rspamd-worker-8404 X-Spamd-Result: default: False [3.34 / 15.00]; RBL_SENDERSCORE(2.00)[104.64.211.4:from]; SUSPICIOUS_RECIPS(1.50)[]; MAILLIST(-0.15)[generic]; BAD_REP_POLICIES(0.10)[]; MIME_GOOD(-0.10)[text/plain]; HAS_LIST_UNSUB(-0.01)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; ARC_ALLOW(0.00)[subspace.kernel.org:s=arc-20240116:i=1]; PRECEDENCE_BULK(0.00)[]; DMARC_NA(0.00)[pengutronix.de]; RCPT_COUNT_TWELVE(0.00)[15]; TAGGED_RCPT(0.00)[]; FORGED_SENDER_MAILLIST(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; RCVD_COUNT_FIVE(0.00)[6]; ASN(0.00)[asn:63949, ipnet:104.64.192.0/19, country:SG]; R_SPF_ALLOW(0.00)[+ip4:104.64.211.4]; FREEMAIL_TO(0.00)[kernel.org,gmail.com,sholland.org,ultimaker.com]; TO_DN_SOME(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[100.90.174.1:received,2a0a:edc0:0:1101:1d::ac:received,185.203.200.13:received]; FROM_NEQ_ENVFROM(0.00)[jre@pengutronix.de,linux-sunxi@lists.linux.dev]; FORGED_RECIPIENTS_MAILLIST(0.00)[]; R_DKIM_NA(0.00)[]; RCVD_TLS_LAST(0.00)[]; TAGGED_FROM(0.00)[bounces-25487-noreply=patchwork.local]; MIME_TRACE(0.00)[0:+]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[104.64.211.4:from] X-Rspamd-Queue-Id: 807CA1C15D3 X-MORS-Enabled: yes X-MORS-DOMAIN: patchwork.local X-MORS-HOSTING: hosting172546 X-MORS-USER: hosting172546 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= |
| Series |
spi: spi-sun4i: assorted fixes
|
|
Commit Message
Jonas Rebmann
Sept. 2, 2026, 3:35 p.m. UTC
From: Marc Kleine-Budde <mkl@pengutronix.de> In commit 6d9fe44bd73d ("spi: sun4i: fix FIFO limit"), the TX-FIFO is filled max to SUN4I_FIFO_DEPTH - 1 (= 63) bytes to work around timeouts observed on A10s SoCs. Commit 196737912da5 ("spi: sun4i: Allow transfers larger than FIFO size") added support for transfers larger than the FIFO size. But this commit only enabled the TX-FIFO empty interrupt for transfers larger than the FIFO (= 64) bytes. This breaks transfers with exactly 64 bytes: the TX-FIFO is only filled with 63 bytes but the interrupt to refill the FIFO is not triggered. The problem can be reproduced with the following command: | spidev_test -D /dev/spidev0.1 -S 64 -s 20000000 -I 1 | | [ 7797.548745] spi_master spi0: spi0.1: timeout transferring 64 bytes@20000000Hz for 110(100)ms | [ 7797.557237] spidev spi0.1: SPI transfer failed: -110 | [ 7797.562308] spi_master spi0: failed to transfer one message from queue | [ 7797.568936] spi_master spi0: noqueue transfer failed To fix the problem enable the TX-FIFO interrupt if the total TX length is larger than SUN4I_FIFO_DEPTH - 1 (= 63) bytes. Fixes: 196737912da5 ("spi: sun4i: Allow transfers larger than FIFO size") Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de> Signed-off-by: Jonas Rebmann <jre@pengutronix.de> --- drivers/spi/spi-sun4i.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Comments
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: Pre-existing issues: - [High] If the hardware reports a TX FIFO count greater than SUN4I_FIFO_DEPTH - 1 (e.g., a full FIFO of 64), sun4i_spi_fill_fifo encounters an integer underflow and enters a nearly infinite loop. -- commit bfe90ee7eb547aed612b50ceef43a3538623eb4d Author: Marc Kleine-Budde <mkl@pengutronix.de> spi: spi-sun4i: sun4i_spi_transfer_one(): fix transfers for SUN4I_FIFO_DEPTH This commit fixes SPI transfers with exactly 64 bytes by enabling the TX-FIFO empty interrupt when the transfer length is greater than SUN4I_FIFO_DEPTH - 1. Previously, the interrupt was only enabled for transfers strictly larger than the 64-byte FIFO, causing timeouts since only 63 bytes are initially filled. > diff --git a/drivers/spi/spi-sun4i.c b/drivers/spi/spi-sun4i.c > index d5c16392cd4d7..2e23244539053 100644 > --- a/drivers/spi/spi-sun4i.c > +++ b/drivers/spi/spi-sun4i.c > @@ -322,7 +322,7 @@ static int sun4i_spi_transfer_one(struct spi_controller *host, > sun4i_spi_enable_interrupt(sspi, SUN4I_INT_CTL_TC | > SUN4I_INT_CTL_RF_F34); > /* Only enable Tx FIFO interrupt if we really need it */ > - if (tx_len > SUN4I_FIFO_DEPTH) > + if (tx_len > SUN4I_FIFO_DEPTH - 1) > sun4i_spi_enable_interrupt(sspi, SUN4I_INT_CTL_TF_E34); [Severity: High] This isn't a bug introduced by this patch, but does the hardware reporting a TX FIFO count greater than SUN4I_FIFO_DEPTH - 1 cause an integer underflow? If sun4i_spi_get_tx_fifo_count(sspi) returns a value >= 64 (which is structurally possible since SUN4I_FIFO_DEPTH is 64 and the register mask is 0x7f), the unsigned subtraction in sun4i_spi_fill_fifo() underflows: drivers/spi/spi-sun4i.c:sun4i_spi_fill_fifo() { cnt = SUN4I_FIFO_DEPTH - 1 - sun4i_spi_get_tx_fifo_count(sspi); len = min_t(int, cnt, sspi->len); while (len--) { ... } This underflow produces 0xFFFFFFFF, which min_t(int, cnt, sspi->len) casts to -1. This negative length could cause the while (len--) loop to evaluate -1 as true, turning it into an infinite loop that executes ~4 billion times and overruns sspi->tx_buf. This problem appears to be made more likely by the subsequent commit "spi: spi-sun4i: sun4i_spi_fill_fifo(): always apply A10 FIFO fill level workaround" which lowered the underflow threshold from > 64 to >= 64. Can this out-of-bounds memory access occur if the hardware FIFO is completely full (64 bytes) or reports spurious values? > > /* Start the transfer */
diff --git a/drivers/spi/spi-sun4i.c b/drivers/spi/spi-sun4i.c index d5c16392cd4d..2e2324453905 100644 --- a/drivers/spi/spi-sun4i.c +++ b/drivers/spi/spi-sun4i.c @@ -322,7 +322,7 @@ static int sun4i_spi_transfer_one(struct spi_controller *host, sun4i_spi_enable_interrupt(sspi, SUN4I_INT_CTL_TC | SUN4I_INT_CTL_RF_F34); /* Only enable Tx FIFO interrupt if we really need it */ - if (tx_len > SUN4I_FIFO_DEPTH) + if (tx_len > SUN4I_FIFO_DEPTH - 1) sun4i_spi_enable_interrupt(sspi, SUN4I_INT_CTL_TF_E34); /* Start the transfer */