Commit b70af9be authored by Alex Smith's avatar Alex Smith Committed by Brian Norris

mtd: nand: increase ready wait timeout and report timeouts

If nand_wait_ready() times out, this is silently ignored, and its
caller will then proceed to read from/write to the chip before it is
ready. This can potentially result in corruption with no indication as
to why.

While a 20ms timeout seems like it should be plenty enough, certain
behaviour can cause it to timeout much earlier than expected. The
situation which prompted this change was that CPU 0, which is
responsible for updating jiffies, was holding interrupts disabled
for a fairly long time while writing to the console during a printk,
causing several jiffies updates to be delayed. If CPU 1 happens to
enter the timeout loop in nand_wait_ready() just before CPU 0 re-
enables interrupts and updates jiffies, CPU 1 will immediately time
out when the delayed jiffies updates are made. The result of this is
that nand_wait_ready() actually waits less time than the NAND chip
would normally take to be ready, and then read_page() proceeds to
read out bad data from the chip.

The situation described above may seem unlikely, but in fact it can be
reproduced almost every boot on the MIPS Creator Ci20.

Therefore, this patch increases the timeout to 400ms. This should be
enough to cover cases where jiffies updates get delayed. In nand_wait()
the timeout was previously chosen based on whether erasing or
programming. This is changed to be 400ms unconditionally as well to
avoid similar problems there. nand_wait() is also slightly refactored
to be consistent with nand_wait{,_status}_ready(). These changes should
have no effect during normal operation.

Debugging this was made more difficult by the misleading comment above
nand_wait_ready() stating "The timeout is caught later" - no timeout was
ever reported, leading me away from the real source of the problem.
Therefore, a pr_warn() is added when a timeout does occur so that it is
easier to pinpoint similar problems in future.
Signed-off-by: default avatarAlex Smith <alex.smith@imgtec.com>
Signed-off-by: default avatarHarvey Hunt <harvey.hunt@imgtec.com>
Reviewed-by: default avatarNiklas Cassel <niklas.cassel@axis.com>
Cc: Alex Smith <alex@alex-smith.me.uk>
Cc: Zubair Lutfullah Kakakhel <Zubair.Kakakhel@imgtec.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Niklas Cassel <niklas.cassel@axis.com>
Signed-off-by: default avatarBrian Norris <computersforpeace@gmail.com>
parent 23829607
...@@ -543,23 +543,32 @@ static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo) ...@@ -543,23 +543,32 @@ static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
} }
} }
/* Wait for the ready pin, after a command. The timeout is caught later. */ /**
* nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
* @mtd: MTD device structure
*
* Wait for the ready pin after a command, and warn if a timeout occurs.
*/
void nand_wait_ready(struct mtd_info *mtd) void nand_wait_ready(struct mtd_info *mtd)
{ {
struct nand_chip *chip = mtd->priv; struct nand_chip *chip = mtd->priv;
unsigned long timeo = jiffies + msecs_to_jiffies(20); unsigned long timeo = 400;
/* 400ms timeout */
if (in_interrupt() || oops_in_progress) if (in_interrupt() || oops_in_progress)
return panic_nand_wait_ready(mtd, 400); return panic_nand_wait_ready(mtd, timeo);
led_trigger_event(nand_led_trigger, LED_FULL); led_trigger_event(nand_led_trigger, LED_FULL);
/* Wait until command is processed or timeout occurs */ /* Wait until command is processed or timeout occurs */
timeo = jiffies + msecs_to_jiffies(timeo);
do { do {
if (chip->dev_ready(mtd)) if (chip->dev_ready(mtd))
break; goto out;
touch_softlockup_watchdog(); cond_resched();
} while (time_before(jiffies, timeo)); } while (time_before(jiffies, timeo));
pr_warn_ratelimited(
"timeout while waiting for chip to become ready\n");
out:
led_trigger_event(nand_led_trigger, LED_OFF); led_trigger_event(nand_led_trigger, LED_OFF);
} }
EXPORT_SYMBOL_GPL(nand_wait_ready); EXPORT_SYMBOL_GPL(nand_wait_ready);
...@@ -885,15 +894,13 @@ static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, ...@@ -885,15 +894,13 @@ static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
* @mtd: MTD device structure * @mtd: MTD device structure
* @chip: NAND chip structure * @chip: NAND chip structure
* *
* Wait for command done. This applies to erase and program only. Erase can * Wait for command done. This applies to erase and program only.
* take up to 400ms and program up to 20ms according to general NAND and
* SmartMedia specs.
*/ */
static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
{ {
int status, state = chip->state; int status;
unsigned long timeo = (state == FL_ERASING ? 400 : 20); unsigned long timeo = 400;
led_trigger_event(nand_led_trigger, LED_FULL); led_trigger_event(nand_led_trigger, LED_FULL);
...@@ -909,7 +916,7 @@ static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) ...@@ -909,7 +916,7 @@ static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
panic_nand_wait(mtd, chip, timeo); panic_nand_wait(mtd, chip, timeo);
else { else {
timeo = jiffies + msecs_to_jiffies(timeo); timeo = jiffies + msecs_to_jiffies(timeo);
while (time_before(jiffies, timeo)) { do {
if (chip->dev_ready) { if (chip->dev_ready) {
if (chip->dev_ready(mtd)) if (chip->dev_ready(mtd))
break; break;
...@@ -918,7 +925,7 @@ static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) ...@@ -918,7 +925,7 @@ static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
break; break;
} }
cond_resched(); cond_resched();
} } while (time_before(jiffies, timeo));
} }
led_trigger_event(nand_led_trigger, LED_OFF); led_trigger_event(nand_led_trigger, LED_OFF);
......
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