From: Johan Hovold <johan@kernel.org>
To: Mark Brown <broonie@kernel.org>
Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org, Saravana Kannan <saravanak@kernel.org>
Subject: Re: [PATCH] spi: fix resource leaks on device setup failure
Date: Tue, 24 Mar 2026 15:13:18 +0100 [thread overview]
Message-ID: <acKb_jjoGBimP4CK@hovoldconsulting.com> (raw)
In-Reply-To: <542cbea1-ed9f-4ba7-a906-7ec1e58c241c@sirena.org.uk>
[-- Attachment #1: Type: text/plain, Size: 1753 bytes --]
On Tue, Mar 24, 2026 at 01:23:56PM +0000, Mark Brown wrote:
> On Tue, Mar 24, 2026 at 11:30:42AM +0100, Johan Hovold wrote:
>
> > Make sure to call controller cleanup() on late device setup failures to
> > avoid leaking resources allocated by setup().
>
> > --- a/drivers/spi/spi.c
> > +++ b/drivers/spi/spi.c
> > @@ -4091,7 +4091,7 @@ int spi_setup(struct spi_device *spi)
>
> This is specifically setup(). This is called repeatedly at runtime,
> it's not part of the device registration flow like the issues that were
> addressed by the commit you tagged as a fix. Can you be more specific
> about the leak you're trying to fix here?
It's when spi_add_device() device fails in spi_setup() that cleanup()
may no longer be called.
spi_add_device()
__spi_add_device()
spi_setup()
controller->setup()
return -<errno>;
And there are drivers that allocate memory in the controller->setup()
callback which is released in ->cleanup() (e.g. atmel_spi_setup() and
atmel_spi_cleanup()).
From a quick look at some of the drivers it did not seem limited to
memory allocations.
But I see now that not all of them can handle cleanup() being called
more than once, which could indeed happen if a later call to spi_setup()
fails after the device has been added.
We could set a flag to track whether setup() has ever been called
successfully, or make sure that cleanup() can be called more than once
for all drivers.
> > +err_cleanup:
> > + if (spi->controller->cleanup)
> > + spi->controller->cleanup(spi);
>
> spi_cleanup()
I open-coded as I'm matching the call to ->setup() inside spi_setup()
with ->cleanup() (i.e. not spi_setup() with spi_cleanup()).
Johan
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 265 bytes --]
prev parent reply other threads:[~2026-03-24 14:13 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-24 10:30 [PATCH] spi: fix resource leaks on device setup failure Johan Hovold
2026-03-24 13:23 ` Mark Brown
2026-03-24 14:13 ` Johan Hovold [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=acKb_jjoGBimP4CK@hovoldconsulting.com \
--to=johan@kernel.org \
--cc=broonie@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-spi@vger.kernel.org \
--cc=saravanak@kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox