Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793638826; b=CcxBcWMUHr2NzNwfEOAT0Kg6ZADQgfH68gH9vEfRrOMiw7y8SKhyxm+MQmaaly1WLHaD c7IylukLKe+hVlp1ZMeqRl++lIOLDRk6ey6MyAjPWQ6XB+qNd6rr4e7E6rVodhG+X+pGi rR+C2RyA4MxgUUdb70ktb42u5QPKnFdCCYz5pnhMhHO15SzR+3hnEERP8G2XDO6k3MWc4 ZsvxjynTrXG74KI9TfEJMVU6TDbSMRQrvu4nXeQplc4Aa0aDaam6SdpA+oW18KF4peYR5 Ftb/GCtzAk7tQPlR9SreTbabw3JFrSq/uaLxkWgPAygx5PCnO8ZAsTiLxiW8+Re0VsQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793638826; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc:References: Content-Language:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=FtWaiLD+f8JJX5hI+Nls/16AS+lNKkNzrClo+6vywTo=; b=Utz3UZ0c1WvZQE1orDFC1NLeOocseiygs0RSqDEsWk2Tm3pmGKh0FCfvG+EAEX6vliZ3 qN+S5TIP6tk0km8NuPkbSSURmBmwq0Fr3VgLCvwlvacdSj6V9e885cvGvYjLluXTR3CRm TcPUrXkdmbbhHl5Cqp46rCD+qde6In4JRIAW4XBxiqJjw9yWPTJiFBc4aJkTYhqn2BU4N I8Xos5D1YLi16TUwaj4hIhYLEs1YEXwVDFIjo+sVirLB7gr7DGwynEKxLNEzUOXBhX74u Yt4+Jv1sNUS7HJYXP57RfeozFCBDDX8CdIQIN5hEbD+OENg9xIhl5VFZxsbWqyV78zg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass header.d=baylibre.com header.i=@baylibre.com header.a=rsa-sha256 header.s=google header.b=W3fXLbuU; arc=none smtp.remote-ip=209.85.161.53 Received: from mail-oo1-f53.google.com (mail-oo1-f53.google.com [209.85.161.53]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 28D1F60AC6E2 for ; Mon, 2 Nov 2026 17:00:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 28D1F60AC6E2 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=baylibre.com header.i=@baylibre.com header.a=rsa-sha256 header.s=google header.b=W3fXLbuU Received: by mail-oo1-f53.google.com with SMTP id 006d021491bc7-702fd86d674so2189886eaf.2 for ; Mon, 02 Nov 2026 09:00:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre.com; s=google; t=1793638824; x=1794243624; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=FtWaiLD+f8JJX5hI+Nls/16AS+lNKkNzrClo+6vywTo=; b=W3fXLbuUCj41YRNXQ8QSL17+xIIok+s+9VAejKVy635IEGLImlDbDTcrQGhM9/0At9 +q7LFoYt3a5s+v2cFDpAqeOeI4QJx010QfD/csw7hC4mINmPzM1oQ82HEnZngerVxdi8 cg/7V84BX4JX9Y7CZxE+WFY+WLqLVB56oHs2Cw5spRrvwHmuMXL/l5d4UMaHxvFWSWaV I3isPMxMcr3O3YjBOSoFYXdwqZ5oa/bMqYNSJ1rpZWZHV1rThvr+rr9oYA8Ux34CBufD KGQDQPAAZyNdXYa1Fd2k+f/Z5Fzil+ZZXu/D1lTLvn9qJTxtbh9xy6ZEEX7eppCiM973 dQWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793638824; x=1794243624; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=FtWaiLD+f8JJX5hI+Nls/16AS+lNKkNzrClo+6vywTo=; b=1sSLiBF6EVzwU5O+Uj932k/BgYbiSs7bUMo/PW4IxqrB09ZbFmrHTDm65xZN7MG3CP khQwwlw7v1DZDcr3Sg0u21lxEMDyP0Ux1ZzHj1k0Ysx4k0KeLOpzo7TVhVHIf48NqdsE TqdGqT2MX786S3A1E36/KShnGgzzy4K7nWK4S9XUDu5Kbj0xYBPZ/p8APmBrnBpxb/sZ h4ozelsXYFg/JmT/Vz8A+hsyUVzvK6ilIOhlsRgoVoRaLRG+KUG4pOad26J/lttA2pgz 8P1opONrSCuZA55jmQ/2BEcDvrMudfPi4Bm7n8jPr81L8blTBzLGUlN3/JKuIhE5SUPi ZlPA== X-Forwarded-Encrypted: i=1; AKwUvByaDjhvzT1IUUN5SMB3fwbfzgfkK9pKA7nxmdPoduJUkP5OclgPGFaPEs6I+UkMN/dEFlw89hBzWFA=@vger.kernel.org X-Gm-Message-State: AFq9FYLQRtAHByUCulLt1EUUGzTcDGISAWIkHVBXSLoika26jqGzb9lp K6BcQEaEEpwCJa/4taPx2aCju/3Qb3ewD47x4uJFZPQyXKnXPOH9DdvytaQSwdXD+C4= X-Gm-Gg: AYBFou1B5UTGK0x1FcZ2x3yaxo98oMt+ehP4s7e3ecHOV3ghqpJuMJh1xcxke3/6Dzs bQrbJyJYXDRcr38EdiQZuiQaev77bvZttuFPqWD6cb+UQGAb8Msg/Vidlo8nZDBXiFpQOsxVGH3 iO89Alwahodrr3BLU9Hy37WVRFredo8KtinBguJGbe8QgoPo+a907jHUMkxZILkrR9dbe3alRcm wP+t0XNqF+5o7xLIlQnIqBun2G69gcbNKo42/CqdJnQHblu9vtpz0XmrwZycEUYsgwCQvH4LdWy 5QHEmkQ7faNCZ5JCDQjD4ITMB+48FzEUICNfvRzOkCgi9eY4CKpxvyCmhBmVKgo0ijE0vMb61Kw nLSk0k8pZqOI54TRKsJuaVMSlkrHNGVAqlDTHKV1nZSIruq0CTKLUW0dnFsYYM+UF7ywgqVqzkZ seV0YNPRpVgPSFwB/jGKunGFSXRkJdEbXt9xWR0psGXY+kHmEx8c7nKK1QoYdH58c4JD92UBjdu 5UvzyxDb6YQj0cqULvIZKJk+6NEGyTnMzc8+Gpb6HrOxsLVwbvH6OCX1wIChA1XL3jT5CtuFUSR K0O8oWgDQPdtVvHcWx0u17PetnfOgZcLj1WhgxW0bljWVZhnyn5tfN7t61nGnunIPon07DCkDAf muA== X-Received: by 2002:a05:6820:8913:20b0:705:cb2d:e0d7 with SMTP id 006d021491bc7-714f238254fmr3733659eaf.26.1793638823919; Mon, 02 Nov 2026 09:00:23 -0800 (PST) Received: from ?IPV6:2600:8803:e7e4:500:1432:12c3:1aa3:969f? ([2600:8803:e7e4:500:1432:12c3:1aa3:969f]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-85d3545bfebsm9359434a34.4.2026.11.02.09.00.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 02 Nov 2026 09:00:21 -0800 (PST) Message-ID: <6b7e13ee-ca5a-4dd7-92d2-3537369c78e8@baylibre.com> Date: Mon, 2 Nov 2026 11:00:20 -0600 X-Mailing-List: linux-spi@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 01/14] spi: unoptimize message when queuing it fails To: Joseph CT Chan , Mark Brown Cc: Bart Van Assche , Conor Dooley , linux-spi@vger.kernel.org, Joseph CT Chan , stable@vger.kernel.org References: <20261102163023.2574632-1-jctchan@gmail.com> <20261102163023.2574632-2-jctchan@gmail.com> Content-Language: en-US From: David Lechner In-Reply-To: <20261102163023.2574632-2-jctchan@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 11/2/26 10:30 AM, Joseph CT Chan wrote: > From: Joseph CT Chan > > spi_async() and spi_sync() optimize a message before they queue it. > When queuing then fails, the message is never finalized, but finalizing > is the only place where the core undoes its own optimization > (spi_finalize_current_message() calls spi_maybe_unoptimize_message()). > So the error paths leak the split transfers and the controller's > optimize state, and leave msg->optimized set. A caller that resends the > message then optimizes an already optimized message. > > This happens when spi_async() returns -EBUSY because another device > holds spi_bus_lock() on the controller (for example mmc_spi), when the > controller's message queue is stopped (-ESHUTDOWN), when the controller > has no ->transfer() callback (-ENOTSUPP), and when __spi_sync() gets an > error from a controller's legacy ->transfer() callback. > > Call spi_maybe_unoptimize_message() on these two error paths. It only > acts on messages the core optimized itself, so messages optimized by > the peripheral driver and controllers that defer optimization (such as > spi-mux) are not affected. A queued message is never unoptimized here. > > Tested with KUnit (spi_core_optimize: spi_core_queue_error_test, > added later in this series) on ARCH=um; builds with W=1 on x86_64. > Not tested on hardware. > > Assisted-by: Claude, Gemini > Fixes: 7b1d87af14d9 ("spi: add spi_optimize_message() APIs") > Fixes: c86a918b1bdb ("spi: don't unoptimize message in spi_async()") > Cc: stable@vger.kernel.org > Signed-off-by: Joseph CT Chan > --- > drivers/spi/spi.c | 7 +++++++ > 1 file changed, 7 insertions(+) > > diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c > index 40cda0037ea5..0d36460e2c87 100644 > --- a/drivers/spi/spi.c > +++ b/drivers/spi/spi.c > @@ -4906,6 +4906,10 @@ int spi_async(struct spi_device *spi, struct spi_message *message) > > spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); > > + /* The message was not queued, so it will not be finalized. */ > + if (ret) > + spi_maybe_unoptimize_message(message); > + > return ret; > } > EXPORT_SYMBOL_GPL(spi_async); > @@ -5009,6 +5013,9 @@ static int __spi_sync(struct spi_device *spi, struct spi_message *message) > if (status == 0) { > wait_for_completion(&done); > status = message->status; > + } else { > + /* The message was not queued, so it will not be finalized. */ > + spi_maybe_unoptimize_message(message); > } > message->complete = NULL; > message->context = NULL; Reviewed-by: David Lechner