Commit 8cba3234 authored by Sean Nyekjaer's avatar Sean Nyekjaer Committed by Miquel Raynal

mtd: rawnand: protect access to rawnand devices while in suspend

Prevent rawnand access while in a suspended state.

Commit 013e6292 ("mtd: rawnand: Simplify the locking") allows the
rawnand layer to return errors rather than waiting in a blocking wait.

Tested on a iMX6ULL.

Fixes: 013e6292 ("mtd: rawnand: Simplify the locking")
Signed-off-by: default avatarSean Nyekjaer <sean@geanix.com>
Reviewed-by: default avatarBoris Brezillon <boris.brezillon@collabora.com>
Cc: stable@vger.kernel.org
Signed-off-by: default avatarMiquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20220208085213.1838273-1-sean@geanix.com
parent d430e4ac
...@@ -338,16 +338,19 @@ static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs) ...@@ -338,16 +338,19 @@ static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
* *
* Return: -EBUSY if the chip has been suspended, 0 otherwise * Return: -EBUSY if the chip has been suspended, 0 otherwise
*/ */
static int nand_get_device(struct nand_chip *chip) static void nand_get_device(struct nand_chip *chip)
{ {
/* Wait until the device is resumed. */
while (1) {
mutex_lock(&chip->lock); mutex_lock(&chip->lock);
if (chip->suspended) { if (!chip->suspended) {
mutex_unlock(&chip->lock);
return -EBUSY;
}
mutex_lock(&chip->controller->lock); mutex_lock(&chip->controller->lock);
return;
}
mutex_unlock(&chip->lock);
return 0; wait_event(chip->resume_wq, !chip->suspended);
}
} }
/** /**
...@@ -576,9 +579,7 @@ static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs) ...@@ -576,9 +579,7 @@ static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs)
nand_erase_nand(chip, &einfo, 0); nand_erase_nand(chip, &einfo, 0);
/* Write bad block marker to OOB */ /* Write bad block marker to OOB */
ret = nand_get_device(chip); nand_get_device(chip);
if (ret)
return ret;
ret = nand_markbad_bbm(chip, ofs); ret = nand_markbad_bbm(chip, ofs);
nand_release_device(chip); nand_release_device(chip);
...@@ -3826,9 +3827,7 @@ static int nand_read_oob(struct mtd_info *mtd, loff_t from, ...@@ -3826,9 +3827,7 @@ static int nand_read_oob(struct mtd_info *mtd, loff_t from,
ops->mode != MTD_OPS_RAW) ops->mode != MTD_OPS_RAW)
return -ENOTSUPP; return -ENOTSUPP;
ret = nand_get_device(chip); nand_get_device(chip);
if (ret)
return ret;
if (!ops->datbuf) if (!ops->datbuf)
ret = nand_do_read_oob(chip, from, ops); ret = nand_do_read_oob(chip, from, ops);
...@@ -4415,13 +4414,11 @@ static int nand_write_oob(struct mtd_info *mtd, loff_t to, ...@@ -4415,13 +4414,11 @@ static int nand_write_oob(struct mtd_info *mtd, loff_t to,
struct mtd_oob_ops *ops) struct mtd_oob_ops *ops)
{ {
struct nand_chip *chip = mtd_to_nand(mtd); struct nand_chip *chip = mtd_to_nand(mtd);
int ret; int ret = 0;
ops->retlen = 0; ops->retlen = 0;
ret = nand_get_device(chip); nand_get_device(chip);
if (ret)
return ret;
switch (ops->mode) { switch (ops->mode) {
case MTD_OPS_PLACE_OOB: case MTD_OPS_PLACE_OOB:
...@@ -4481,9 +4478,7 @@ int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr, ...@@ -4481,9 +4478,7 @@ int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
return -EIO; return -EIO;
/* Grab the lock and see if the device is available */ /* Grab the lock and see if the device is available */
ret = nand_get_device(chip); nand_get_device(chip);
if (ret)
return ret;
/* Shift to get first page */ /* Shift to get first page */
page = (int)(instr->addr >> chip->page_shift); page = (int)(instr->addr >> chip->page_shift);
...@@ -4570,7 +4565,7 @@ static void nand_sync(struct mtd_info *mtd) ...@@ -4570,7 +4565,7 @@ static void nand_sync(struct mtd_info *mtd)
pr_debug("%s: called\n", __func__); pr_debug("%s: called\n", __func__);
/* Grab the lock and see if the device is available */ /* Grab the lock and see if the device is available */
WARN_ON(nand_get_device(chip)); nand_get_device(chip);
/* Release it and go back */ /* Release it and go back */
nand_release_device(chip); nand_release_device(chip);
} }
...@@ -4587,9 +4582,7 @@ static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) ...@@ -4587,9 +4582,7 @@ static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
int ret; int ret;
/* Select the NAND device */ /* Select the NAND device */
ret = nand_get_device(chip); nand_get_device(chip);
if (ret)
return ret;
nand_select_target(chip, chipnr); nand_select_target(chip, chipnr);
...@@ -4660,6 +4653,8 @@ static void nand_resume(struct mtd_info *mtd) ...@@ -4660,6 +4653,8 @@ static void nand_resume(struct mtd_info *mtd)
__func__); __func__);
} }
mutex_unlock(&chip->lock); mutex_unlock(&chip->lock);
wake_up_all(&chip->resume_wq);
} }
/** /**
...@@ -5438,6 +5433,7 @@ static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips, ...@@ -5438,6 +5433,7 @@ static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
chip->cur_cs = -1; chip->cur_cs = -1;
mutex_init(&chip->lock); mutex_init(&chip->lock);
init_waitqueue_head(&chip->resume_wq);
/* Enforce the right timings for reset/detection */ /* Enforce the right timings for reset/detection */
chip->current_interface_config = nand_get_reset_interface_config(); chip->current_interface_config = nand_get_reset_interface_config();
......
...@@ -1240,6 +1240,7 @@ struct nand_secure_region { ...@@ -1240,6 +1240,7 @@ struct nand_secure_region {
* @lock: Lock protecting the suspended field. Also used to serialize accesses * @lock: Lock protecting the suspended field. Also used to serialize accesses
* to the NAND device * to the NAND device
* @suspended: Set to 1 when the device is suspended, 0 when it's not * @suspended: Set to 1 when the device is suspended, 0 when it's not
* @resume_wq: wait queue to sleep if rawnand is in suspended state.
* @cur_cs: Currently selected target. -1 means no target selected, otherwise we * @cur_cs: Currently selected target. -1 means no target selected, otherwise we
* should always have cur_cs >= 0 && cur_cs < nanddev_ntargets(). * should always have cur_cs >= 0 && cur_cs < nanddev_ntargets().
* NAND Controller drivers should not modify this value, but they're * NAND Controller drivers should not modify this value, but they're
...@@ -1294,6 +1295,7 @@ struct nand_chip { ...@@ -1294,6 +1295,7 @@ struct nand_chip {
/* Internals */ /* Internals */
struct mutex lock; struct mutex lock;
unsigned int suspended : 1; unsigned int suspended : 1;
wait_queue_head_t resume_wq;
int cur_cs; int cur_cs;
int read_retries; int read_retries;
struct nand_secure_region *secure_regions; struct nand_secure_region *secure_regions;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment