From nobody Fri Jun 12 18:33:13 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 9B3623D903C; Fri, 10 Apr 2026 15:49:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775836162; cv=none; b=Ukg/bLxKijszepc3KwM5Ym3v1vSTxce3j9m8zAl0AcA3nUkD7gD0VVp6wk/Ltypt4i7e/1rtFgAiTb+7q0wkZA8e5baFl2hyoBDLslWcQSDU4s6KZzLVUGFhw3KS17XvDY6KjtHy4sEEM1jEv+jLtbKWQ9CKcXXbx6ls3sh2I4E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775836162; c=relaxed/simple; bh=rVtKk+vhYWkR9k1Oiu7/We4EJMuGL7w+xUU08ZTg9yE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZsAl2jawGTgpCshpgtapduv4e6r0T4p6swqWgBsHermUcAq2cG9tE/gEWg7TkSRG0wUQ5ZE7l9MMswT2djgRF70WsNi+GFOJG+TUWL4fUZX67qOSM1qY0rG4P44Qf+5j4U2Skt7tZa6USHMbBzXTjLrlRqW4IPCMmZK0HSaUQ8w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=es0GasYk; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="es0GasYk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5F7DAC2BCB0; Fri, 10 Apr 2026 15:49:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775836162; bh=rVtKk+vhYWkR9k1Oiu7/We4EJMuGL7w+xUU08ZTg9yE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=es0GasYkRKi4Pw+QektFE5ffTn7USDJCiY1sr6y/9KwjodVBieP8i4o3sD0rPDeVC OddhCKW6UDEQAJTm364q0IOadegn0BeoluiPwQaznPeBpLN3g3VagmzqqfLOLkXP1P kf700RW/7o04z3pvuOK7vGlnsx9h11bRUh2W1X0Mig+WxWmJUuNNmYR3jx6NTkZS9C v7syC/EcnJxz+nJKEZhbWr0pvYgw6BeHjvoRlGC3RxG0zWTpCCVKnkxykloG3NZQar kVAPtnO3vqy7IpQXR2fZEcGaGxEMuiCYaO4CNXnVld4uCrTQN45VGaqDzdJaMx7n5n 3STuDGmySrPkg== Received: from johan by xi.lan with local (Exim 4.98.2) (envelope-from ) id 1wBE6i-00000000Xd4-08ua; Fri, 10 Apr 2026 17:49:20 +0200 From: Johan Hovold To: Mark Brown Cc: Saravana Kannan , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, Johan Hovold , stable@vger.kernel.org Subject: [PATCH 1/2] spi: fix resource leaks on device setup failure Date: Fri, 10 Apr 2026 17:49:06 +0200 Message-ID: <20260410154907.129248-2-johan@kernel.org> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260410154907.129248-1-johan@kernel.org> References: <20260410154907.129248-1-johan@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Make sure to call controller cleanup() if spi_setup() fails while registering a device to avoid leaking any resources allocated by setup(). Fixes: c7299fea6769 ("spi: Fix spi device unregister flow") Cc: stable@vger.kernel.org # 5.13 Cc: Saravana Kannan Signed-off-by: Johan Hovold --- drivers/spi/spi.c | 61 ++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 272b45d6b5aa..c14f29457c29 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -43,6 +43,8 @@ EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop); =20 #include "internals.h" =20 +static int __spi_setup(struct spi_device *spi, bool initial_setup); + static DEFINE_IDR(spi_controller_idr); =20 static void spidev_release(struct device *dev) @@ -726,7 +728,7 @@ static int __spi_add_device(struct spi_device *spi) * normally rely on the device being setup. Devices * using SPI_CS_HIGH can't coexist well otherwise... */ - status =3D spi_setup(spi); + status =3D __spi_setup(spi, true); if (status < 0) { dev_err(dev, "can't setup %s, status %d\n", dev_name(&spi->dev), status); @@ -3992,27 +3994,7 @@ static int spi_set_cs_timing(struct spi_device *spi) return status; } =20 -/** - * spi_setup - setup SPI mode and clock rate - * @spi: the device whose settings are being modified - * Context: can sleep, and no requests are queued to the device - * - * SPI protocol drivers may need to update the transfer mode if the - * device doesn't work with its default. They may likewise need - * to update clock rates or word sizes from initial values. This function - * changes those settings, and must be called from a context that can slee= p. - * Except for SPI_CS_HIGH, which takes effect immediately, the changes take - * effect the next time the device is selected and data is transferred to - * or from it. When this function returns, the SPI device is deselected. - * - * Note that this call will fail if the protocol driver specifies an option - * that the underlying controller or its driver does not support. For - * example, not all hardware supports wire transfers using nine bit words, - * LSB-first wire encoding, or active-high chipselects. - * - * Return: zero on success, else a negative error code. - */ -int spi_setup(struct spi_device *spi) +static int __spi_setup(struct spi_device *spi, bool initial_setup) { unsigned bad_bits, ugly_bits; int status; @@ -4097,7 +4079,7 @@ int spi_setup(struct spi_device *spi) status =3D spi_set_cs_timing(spi); if (status) { mutex_unlock(&spi->controller->io_mutex); - return status; + goto err_cleanup; } =20 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { @@ -4106,7 +4088,7 @@ int spi_setup(struct spi_device *spi) mutex_unlock(&spi->controller->io_mutex); dev_err(&spi->controller->dev, "Failed to power device: %d\n", status); - return status; + goto err_cleanup; } =20 /* @@ -4142,6 +4124,37 @@ int spi_setup(struct spi_device *spi) status); =20 return status; + +err_cleanup: + if (initial_setup) + spi_cleanup(spi); + + return status; +} + +/** + * spi_setup - setup SPI mode and clock rate + * @spi: the device whose settings are being modified + * Context: can sleep, and no requests are queued to the device + * + * SPI protocol drivers may need to update the transfer mode if the + * device doesn't work with its default. They may likewise need + * to update clock rates or word sizes from initial values. This function + * changes those settings, and must be called from a context that can slee= p. + * Except for SPI_CS_HIGH, which takes effect immediately, the changes take + * effect the next time the device is selected and data is transferred to + * or from it. When this function returns, the SPI device is deselected. + * + * Note that this call will fail if the protocol driver specifies an option + * that the underlying controller or its driver does not support. For + * example, not all hardware supports wire transfers using nine bit words, + * LSB-first wire encoding, or active-high chipselects. + * + * Return: zero on success, else a negative error code. + */ +int spi_setup(struct spi_device *spi) +{ + return __spi_setup(spi, false); } EXPORT_SYMBOL_GPL(spi_setup); =20 --=20 2.52.0 From nobody Fri Jun 12 18:33:13 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 9B2AA3D903B; Fri, 10 Apr 2026 15:49:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775836162; cv=none; b=RlkI99Sc/CQYApDvdXthQKu4nNE/RSrMUAU+ZC+OJmoupQlFXG/8TTZnWGAV7qOxtoKmBPzFBnVumfRZrAO65pKSZ5iOCMRg7E4BBz4zTHgCFwMAI682P+iI3g9YzTD077YXXv2fs9vuZJg2FkOrHigzvYqT8A3BhJ5222zCnyE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775836162; c=relaxed/simple; bh=hhcWLZ7i+S1Fd0VS4l3oTnHgeKqBUhsVX+nrjCmWJ1g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dYCfAhrXU/RE9VJuQbFkSzsRGQsYv+hdsTOu7BUcF1LAGFIGfyCeSJzwrlmL/7+zxvOiuRknDOuGcrpyhhrhlNaJKSqjtScx0UCp9EbL3O7SNsZak72Zjwyc7Db+lnzcjUtiNPnKEsBBFt5FluOX3LAhXAhajXGS1IB9TI7ul/Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=e5zJEm3d; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="e5zJEm3d" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5D39AC2BCAF; Fri, 10 Apr 2026 15:49:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775836162; bh=hhcWLZ7i+S1Fd0VS4l3oTnHgeKqBUhsVX+nrjCmWJ1g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=e5zJEm3dgEywQYrkdVLCow24rrXvAyvAOGOxOzUIiIn/pb9mv+6ZcSbSjZGkTepkU dKEigPIB4S1yKSUY9fIbahRbkck9Dnt3Bg0pEp3zsNXdKg+/PaDpZK31kPhBrpPaSC c292B/BFMB7KnDLmWDVOPCajRnUd+09D6GbC84o4pG6GZhnO+W4WEHbokzgfUMvxVg yyXZJm8K2/9ZEBgHtweKJxCrYv0Q7n8JhTDXj6JFwOVXfHLI947uIRbwoGSVJmav4x fM8j3PHEyk6TtblgVKRMIt3vm3pd7ZxNEA/lE2ky+LPSAgV8SfgSCX6iQTuUQwh2l9 D8vQ+NBCb9gZw== Received: from johan by xi.lan with local (Exim 4.98.2) (envelope-from ) id 1wBE6i-00000000Xd6-0BNX; Fri, 10 Apr 2026 17:49:20 +0200 From: Johan Hovold To: Mark Brown Cc: Saravana Kannan , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, Johan Hovold Subject: [PATCH 2/2] spi: fix controller cleanup() documentation Date: Fri, 10 Apr 2026 17:49:07 +0200 Message-ID: <20260410154907.129248-3-johan@kernel.org> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260410154907.129248-1-johan@kernel.org> References: <20260410154907.129248-1-johan@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The controller cleanup() callback is no longer called when releasing a device, but rather when deregistering it (and on registration failures). Fixes: c7299fea6769 ("spi: Fix spi device unregister flow") Cc: Saravana Kannan Signed-off-by: Johan Hovold --- include/linux/spi/spi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index 0dc671c07d3a..0de636484f3d 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -700,7 +700,7 @@ struct spi_controller { int (*transfer)(struct spi_device *spi, struct spi_message *mesg); =20 - /* Called on release() to free memory provided by spi_controller */ + /* Called on deregistration to free memory provided by spi_controller */ void (*cleanup)(struct spi_device *spi); =20 /* --=20 2.52.0