Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] mtd: rawnand: unlocked hardware access after nand_disconnect()
in write_bbt()
- [Medium] mtd: rawnand: nand_suspend() returning -ENODEV aborts system
suspend
--
commit 0d69828030d21f2e8c3f391544ee5a56406e7996
Author: James Hilliard <james.hilliard1@gmail.com>
mtd: rawnand: separate hardware disconnection from cleanup
This commit adds nand_disconnect() to mark a NAND chip as disconnected
under the chip and controller locks and makes nand_get_device(),
nand_suspend(), nand_resume(), and panic_nand_write() reject hardware
access once disconnected.
> diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c
> index a5b278ab9384f..8f7d2571e8d9a 100644
> --- a/drivers/mtd/nand/raw/nand_base.c
> +++ b/drivers/mtd/nand/raw/nand_base.c
[ ... ]
> @@ -581,7 +607,9 @@ static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs)
> nand_erase_nand(chip, &einfo, 0);
>
> /* Write bad block marker to OOB */
> - nand_get_device(chip);
> + ret = nand_get_device(chip);
> + if (ret)
> + return ret;
>
> ret = nand_markbad_bbm(chip, ofs);
> nand_release_device(chip);
[Severity: High]
Can a concurrent nand_disconnect() cause write_bbt() to issue unlocked
hardware operations on the disconnected controller when updating an
on-flash BBT?
nand_block_markbad_lowlevel() releases the chip and controller locks
before calling nand_markbad_bbt() -> nand_update_bbt() -> write_bbt().
If nand_disconnect() runs before write_bbt() calls nand_erase_nand() or
scan_write_bbt(), nand_get_device() returns -ENODEV, which write_bbt()
treats as a bad BBT eraseblock:
drivers/mtd/nand/raw/nand_bbt.c:write_bbt() {
...
res = nand_erase_nand(this, &einfo, 1);
if (res < 0) {
pr_warn("nand_bbt: error while erasing BBT block %d\n",
res);
mark_bbt_block_bad(this, td, chip, block);
continue;
}
...
}
mark_bbt_block_bad() then marks each reserved BBT block worn in memory
and calls nand_markbad_bbm() -> nand_default_block_markbad() ->
nand_do_write_oob() without calling nand_get_device(), accessing
controller resources that have already been freed after
nand_disconnect() returned.
[ ... ]
> @@ -4688,10 +4728,14 @@ static int nand_suspend(struct mtd_info *mtd)
> int ret = 0;
>
> mutex_lock(&chip->lock);
> + if (chip->disconnected) {
> + mutex_unlock(&chip->lock);
> + return -ENODEV;
> + }
> if (chip->ops.suspend)
> ret = chip->ops.suspend(chip);
> if (!ret)
> - chip->suspended = 1;
> + WRITE_ONCE(chip->suspended, true);
> mutex_unlock(&chip->lock);
>
> return ret;
[Severity: Medium]
Does returning -ENODEV from nand_suspend() abort system suspend when an
open MTD handle outlives controller unbind?
While an MTD handle remains open after del_mtd_device() and
nand_disconnect(), mtd->dev stays registered on dpm_list until the last
reference is put in mtd_device_release(). During system suspend,
mtd_cls_suspend() calls mtd_suspend() -> nand_suspend(), and returning
-ENODEV fails dpm_suspend() instead of treating the disconnected chip as
a no-op like nand_resume() and nand_sync() do.
@@ -336,21 +336,47 @@ static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
*
* Lock the device and its controller for exclusive access
*/
-static void nand_get_device(struct nand_chip *chip)
+static int nand_get_device(struct nand_chip *chip)
{
/* Wait until the device is resumed. */
while (1) {
mutex_lock(&chip->lock);
+ if (chip->disconnected) {
+ mutex_unlock(&chip->lock);
+ return -ENODEV;
+ }
if (!chip->suspended) {
mutex_lock(&chip->controller->lock);
- return;
+ return 0;
}
mutex_unlock(&chip->lock);
- wait_event(chip->resume_wq, !chip->suspended);
+ wait_event(chip->resume_wq, !READ_ONCE(chip->suspended) ||
+ READ_ONCE(chip->disconnected));
}
}
+/**
+ * nand_disconnect - drain hardware access and reject further operations
+ * @chip: NAND chip object
+ *
+ * Unregister the MTD device first so removal notifiers can finish their I/O.
+ * Call before releasing controller resources. Operations waiting for resume
+ * are woken and fail with -ENODEV.
+ * The chip, controller lock and NAND metadata must remain alive
+ * until existing MTD handles are released; nand_cleanup() is a separate step.
+ */
+void nand_disconnect(struct nand_chip *chip)
+{
+ mutex_lock(&chip->lock);
+ mutex_lock(&chip->controller->lock);
+ WRITE_ONCE(chip->disconnected, true);
+ mutex_unlock(&chip->controller->lock);
+ mutex_unlock(&chip->lock);
+ wake_up_all(&chip->resume_wq);
+}
+EXPORT_SYMBOL_GPL(nand_disconnect);
+
/**
* nand_check_wp - [GENERIC] check if the chip is write protected
* @chip: NAND chip object
@@ -581,7 +607,9 @@ static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs)
nand_erase_nand(chip, &einfo, 0);
/* Write bad block marker to OOB */
- nand_get_device(chip);
+ ret = nand_get_device(chip);
+ if (ret)
+ return ret;
ret = nand_markbad_bbm(chip, ofs);
nand_release_device(chip);
@@ -3879,7 +3907,9 @@ static int nand_read_oob(struct mtd_info *mtd, loff_t from,
ops->mode != MTD_OPS_RAW)
return -ENOTSUPP;
- nand_get_device(chip);
+ ret = nand_get_device(chip);
+ if (ret)
+ return ret;
old_stats = mtd->ecc_stats;
@@ -4449,6 +4479,9 @@ static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
struct mtd_oob_ops ops;
int ret;
+ if (READ_ONCE(chip->disconnected))
+ return -ENODEV;
+
nand_select_target(chip, chipnr);
/* Wait for the device to get ready */
@@ -4479,7 +4512,9 @@ static int nand_write_oob(struct mtd_info *mtd, loff_t to,
ops->retlen = 0;
- nand_get_device(chip);
+ ret = nand_get_device(chip);
+ if (ret)
+ return ret;
switch (ops->mode) {
case MTD_OPS_PLACE_OOB:
@@ -4539,7 +4574,9 @@ int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
return -EIO;
/* Grab the lock and see if the device is available */
- nand_get_device(chip);
+ ret = nand_get_device(chip);
+ if (ret)
+ return ret;
/* Shift to get first page */
page = (int)(instr->addr >> chip->page_shift);
@@ -4627,7 +4664,8 @@ static void nand_sync(struct mtd_info *mtd)
pr_debug("%s: called\n", __func__);
/* Grab the lock and see if the device is available */
- nand_get_device(chip);
+ if (nand_get_device(chip))
+ return;
/* Release it and go back */
nand_release_device(chip);
}
@@ -4644,7 +4682,9 @@ static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
int ret;
/* Select the NAND device */
- nand_get_device(chip);
+ ret = nand_get_device(chip);
+ if (ret)
+ return ret;
nand_select_target(chip, chipnr);
@@ -4688,10 +4728,14 @@ static int nand_suspend(struct mtd_info *mtd)
int ret = 0;
mutex_lock(&chip->lock);
+ if (chip->disconnected) {
+ mutex_unlock(&chip->lock);
+ return -ENODEV;
+ }
if (chip->ops.suspend)
ret = chip->ops.suspend(chip);
if (!ret)
- chip->suspended = 1;
+ WRITE_ONCE(chip->suspended, true);
mutex_unlock(&chip->lock);
return ret;
@@ -4706,10 +4750,12 @@ static void nand_resume(struct mtd_info *mtd)
struct nand_chip *chip = mtd_to_nand(mtd);
scoped_guard(mutex, &chip->lock) {
+ if (chip->disconnected)
+ return;
if (chip->suspended) {
if (chip->ops.resume)
chip->ops.resume(chip);
- chip->suspended = 0;
+ WRITE_ONCE(chip->suspended, false);
} else {
pr_err("%s called for a chip which is not in suspended state\n",
__func__);
@@ -4743,7 +4789,9 @@ static int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
if (!chip->ops.lock_area)
return -ENOTSUPP;
- nand_get_device(chip);
+ ret = nand_get_device(chip);
+ if (ret)
+ return ret;
ret = chip->ops.lock_area(chip, ofs, len);
nand_release_device(chip);
@@ -4764,7 +4812,9 @@ static int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
if (!chip->ops.unlock_area)
return -ENOTSUPP;
- nand_get_device(chip);
+ ret = nand_get_device(chip);
+ if (ret)
+ return ret;
ret = chip->ops.unlock_area(chip, ofs, len);
nand_release_device(chip);
@@ -1264,9 +1264,10 @@ struct nand_secure_region {
* @pagecache.page: Page number currently in the cache. -1 means no page is
* currently cached
* @buf_align: Minimum buffer alignment required by a platform
- * @lock: Lock protecting the suspended field. Also used to serialize accesses
- * to the NAND device
+ * @lock: Lock protecting the suspended and disconnected fields. Also serializes
+ * accesses to the NAND device
* @suspended: Set to 1 when the device is suspended, 0 when it's not
+ * @disconnected: Controller resources are unavailable; reject hardware access
* @resume_wq: wait queue to sleep if rawnand is in suspended state.
* @cur_cs: Currently selected target. -1 means no target selected, otherwise we
* should always have cur_cs >= 0 && cur_cs < nanddev_ntargets().
@@ -1326,7 +1327,8 @@ struct nand_chip {
/* Internals */
struct mutex lock;
- unsigned int suspended : 1;
+ bool suspended;
+ bool disconnected;
wait_queue_head_t resume_wq;
int cur_cs;
int read_retries;
@@ -1595,6 +1597,7 @@ void nand_wait_ready(struct nand_chip *chip);
* sucessful nand_scan().
*/
void nand_cleanup(struct nand_chip *chip);
+void nand_disconnect(struct nand_chip *chip);
/*
* External helper for controller drivers that have to implement the WAITRDY