From: sashiko-bot@kernel.org
To: pratyush@kernel.org, mwalle@kernel.org,
"Itai Handler" <itai.handler@gmail.com>
Cc: takahiro.kuwano@infineon.com, linux-kernel@vger.kernel.org,
vigneshr@ti.com, miquel.raynal@bootlin.com,
linux-mtd@lists.infradead.org, richard@nod.at
Subject: Re: [PATCH v2 3/3] mtd: spi-nor: take the flash lock in spi_nor_remove()
Date: Mon, 14 Sep 2026 08:26:20 +0000 [thread overview]
Message-ID: <20260914082620.E0FC91F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260914081149.1916589-4-itai.handler@gmail.com>
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
New issues:
- [Critical] Using a killable wait in the device removal path allows signals to abort critical hardware teardown, potentially bricking the device on the next boot.
Pre-existing issues:
- [Critical] devm-allocated driver data (struct spi_nor and its DMA bouncebuf) is freed while userspace can still access the MTD device.
- [High] Releasing the device lock before unregistering the MTD device exposes restored hardware to concurrent I/O and block-layer flushes, causing data corruption.
- [High] If `wait_event_killable()` is interrupted by a signal, `spi_nor_prep_and_lock()` returns an error without unwinding `spi_nor_prep()`, permanently leaking the SPI controller's prepared state.
--
commit f3b4a47f56b538ca231ed893cf829db3979e2e80
Author: Itai Handler <itai.handler@gmail.com>
mtd: spi-nor: take the flash lock in spi_nor_remove()
spi_nor_remove() restores the addressing mode with the same unlocked
call to spi_nor_restore() that spi_nor_shutdown() used before the
previous patch, and it is exposed the same way. This patch moves the
restore inside spi_nor_prep_and_lock() to ensure the lock is held.
> diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c
> index 647bf8dce7193..8d0302565445b 100644
> --- a/drivers/mtd/spi-nor/core.c
> +++ b/drivers/mtd/spi-nor/core.c
> @@ -3852,8 +3852,14 @@ static int spi_nor_probe(struct spi_mem *spimem)
> static int spi_nor_remove(struct spi_mem *spimem)
> {
> struct spi_nor *nor = spi_mem_get_drvdata(spimem);
> + int ret;
>
> - spi_nor_restore(nor);
> + /* As in spi_nor_shutdown(), do not restore under an operation. */
> + ret = spi_nor_prep_and_lock(nor);
[Severity: High]
This is a pre-existing issue, but does this function leak the SPI
controller's prepared state if interrupted by a signal?
Looking at drivers/mtd/spi-nor/core.c:spi_nor_prep_and_lock(), it
successfully calls spi_nor_prep(), but then waits via wait_event_killable():
ret = wait_event_killable(nor->rww.wait,
spi_nor_rww_start_exclusive(nor));
return ret;
If this wait is interrupted by a fatal signal, it returns -ERESTARTSYS
directly without calling spi_nor_unprep().
Can this permanently leak SPI controller resources like clocks and PM
runtime references?
[Severity: Critical]
Does using a killable wait here allow signals to abort the essential hardware
teardown?
If the process triggering the unbind receives a fatal signal, the lock
acquisition aborts and returns an error. This causes spi_nor_remove() to
skip the essential spi_nor_restore() hardware reset step, leaving the
physical flash in a non-default active state (e.g. 4-byte addressing mode).
Can this cause the system to become unbootable on the next boot if the
bootloader fails to read the flash in the unexpected 4-byte addressing mode?
> + if (!ret) {
> + spi_nor_restore(nor);
> + spi_nor_unlock_and_unprep(nor);
> + }
[Severity: High]
This is a pre-existing issue, but does dropping the lock here re-open the
race window before the MTD device is unregistered?
Concurrent I/O operations can acquire the lock after
spi_nor_unlock_and_unprep() but before or during mtd_device_unregister()
(which can trigger block layer flushes via mtdblock_release()).
These operations will execute using the driver's cached software state
(4-byte addressing), but the physical chip has already been restored to
3-byte addressing.
Can this result in silent data corruption?
>
> /* Clean up MTD stuff. */
> return mtd_device_unregister(&nor->mtd);
> }
[Severity: Critical]
This is a pre-existing issue, but is it possible for devm-allocated driver
data to be freed while userspace can still access the MTD device?
In drivers/mtd/spi-nor/core.c:spi_nor_probe(), struct spi_nor is allocated
using devm_kzalloc().
During spi_nor_remove(), mtd_device_unregister() drops a reference to the
MTD device, but does not block if userspace still holds an open file
descriptor:
return mtd_device_unregister(&nor->mtd);
Since spi_nor_remove() returns immediately, the driver core will execute
devres cleanup, freeing struct spi_nor.
Could subsequent MTD operations from the open file descriptor dereference
the freed memory?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260914081149.1916589-1-itai.handler@gmail.com?part=3
prev parent reply other threads:[~2026-09-14 8:26 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-14 8:11 [PATCH v2 0/3] mtd: spi-nor: fix the unlocked restore on shutdown and remove Itai Handler
2026-09-14 8:11 ` [PATCH v2 1/3] mtd: spi-nor: fix the lock left held by spi_nor_rww_start_exclusive() Itai Handler
2026-09-14 8:22 ` sashiko-bot
2026-09-14 8:11 ` [PATCH v2 2/3] mtd: spi-nor: take the flash lock in spi_nor_shutdown() Itai Handler
2026-09-14 8:24 ` sashiko-bot
2026-09-14 8:11 ` [PATCH v2 3/3] mtd: spi-nor: take the flash lock in spi_nor_remove() Itai Handler
2026-09-14 8:26 ` sashiko-bot [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=20260914082620.E0FC91F000FF@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=itai.handler@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=miquel.raynal@bootlin.com \
--cc=mwalle@kernel.org \
--cc=pratyush@kernel.org \
--cc=richard@nod.at \
--cc=sashiko-reviews@lists.linux.dev \
--cc=takahiro.kuwano@infineon.com \
--cc=vigneshr@ti.com \
/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
all inboxes | Powered by JetHome®