mtd: fix race in cfi_cmdset_0001 driver

Linux-MTD Mailing List linux-mtd at lists.infradead.org
Fri Mar 11 10:59:01 EST 2011


Gitweb:     http://git.infradead.org/?p=mtd-2.6.git;a=commit;h=ecf3fde07c8dcb92a1bf3fbdfe70905d85cd00e1
Commit:     ecf3fde07c8dcb92a1bf3fbdfe70905d85cd00e1
Parent:     ceabebb2bd2672f709e4454e16bc6042732e2dfe
Author:     Joakim Tjernlund <Joakim.Tjernlund at transmode.se>
AuthorDate: Mon Feb 7 17:07:11 2011 +0100
Committer:  David Woodhouse <David.Woodhouse at intel.com>
CommitDate: Fri Mar 11 12:09:07 2011 +0000

    mtd: fix race in cfi_cmdset_0001 driver
    
    As inval_cache_and_wait_for_operation() drop and reclaim the lock
    to invalidate the cache, some other thread may suspend the operation
    before reaching the for(;;) loop. Therefore the loop must start with
    checking the chip->state before reading status from the chip.
    
    Signed-off-by: Joakim Tjernlund <Joakim.Tjernlund at transmode.se>
    Acked-by: Michael Cashwell <mboards at prograde.net>
    Acked-by: Stefan Bigler <stefan.bigler at keymile.com>
    Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy at nokia.com>
    Signed-off-by: David Woodhouse <David.Woodhouse at intel.com>
    Cc: stable at kernel.org
---
 drivers/mtd/chips/cfi_cmdset_0001.c |   43 ++++++++++++++++++-----------------
 1 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/drivers/mtd/chips/cfi_cmdset_0001.c b/drivers/mtd/chips/cfi_cmdset_0001.c
index a8c3e1c..4aaa88f 100644
--- a/drivers/mtd/chips/cfi_cmdset_0001.c
+++ b/drivers/mtd/chips/cfi_cmdset_0001.c
@@ -1230,10 +1230,32 @@ static int inval_cache_and_wait_for_operation(
 	sleep_time = chip_op_time / 2;
 
 	for (;;) {
+		if (chip->state != chip_state) {
+			/* Someone's suspended the operation: sleep */
+			DECLARE_WAITQUEUE(wait, current);
+			set_current_state(TASK_UNINTERRUPTIBLE);
+			add_wait_queue(&chip->wq, &wait);
+			mutex_unlock(&chip->mutex);
+			schedule();
+			remove_wait_queue(&chip->wq, &wait);
+			mutex_lock(&chip->mutex);
+			continue;
+		}
+
 		status = map_read(map, cmd_adr);
 		if (map_word_andequal(map, status, status_OK, status_OK))
 			break;
 
+		if (chip->erase_suspended && chip_state == FL_ERASING)  {
+			/* Erase suspend occured while sleep: reset timeout */
+			timeo = reset_timeo;
+			chip->erase_suspended = 0;
+		}
+		if (chip->write_suspended && chip_state == FL_WRITING)  {
+			/* Write suspend occured while sleep: reset timeout */
+			timeo = reset_timeo;
+			chip->write_suspended = 0;
+		}
 		if (!timeo) {
 			map_write(map, CMD(0x70), cmd_adr);
 			chip->state = FL_STATUS;
@@ -1257,27 +1279,6 @@ static int inval_cache_and_wait_for_operation(
 			timeo--;
 		}
 		mutex_lock(&chip->mutex);
-
-		while (chip->state != chip_state) {
-			/* Someone's suspended the operation: sleep */
-			DECLARE_WAITQUEUE(wait, current);
-			set_current_state(TASK_UNINTERRUPTIBLE);
-			add_wait_queue(&chip->wq, &wait);
-			mutex_unlock(&chip->mutex);
-			schedule();
-			remove_wait_queue(&chip->wq, &wait);
-			mutex_lock(&chip->mutex);
-		}
-		if (chip->erase_suspended && chip_state == FL_ERASING)  {
-			/* Erase suspend occured while sleep: reset timeout */
-			timeo = reset_timeo;
-			chip->erase_suspended = 0;
-		}
-		if (chip->write_suspended && chip_state == FL_WRITING)  {
-			/* Write suspend occured while sleep: reset timeout */
-			timeo = reset_timeo;
-			chip->write_suspended = 0;
-		}
 	}
 
 	/* Done and happy. */



More information about the linux-mtd-cvs mailing list