Skip to contentSkip to conversation

PATCH (for comment): ide-cd possible race in PIO mode

PATCH Alan Cox 2 participants 6 messages Nov 17, 2004 – Nov 26, 2004 libata

lore
Share & tools
lore

Alan Cox

author
#0 lore raw

Working on tracing down Fedora bug #115458
(https://bugzilla.redhat.com/bugzilla/process_bug.cgi) I found what appears to be a race between the IDE CD driver and the hardware status. It doesn't appear to explain the bug at all but it does look like a bug of itself

When we issue an ide command the status bits don't become valid for 400nS. In the DMA case ide_execute_command handles this but in the PIO case we don't do the needed locking, use OUTBSYNC to avoid posting or delay. This means that in some situations we can execute the command handler in PIO mode before the command status bits are valid and the handler may read and act wrongly.

ide/ide-cd.c renamed from ide/ide-cd.c~ +4 -1
@@ -897,7 +897,10 @@
 		return ide_started;
 	} else {
 		/* packet command */
-		HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
+		spin_lock_irqsave(&ide_lock, flags);
+		HWIF(drive)->OUTBSYNC(WIN_PACKETCMD, IDE_COMMAND_REG);
+		ndelay(400);
+		spin_unlock_irqsave(&ide_lock, flags);
 		return (*handler) (drive);
 	}
 }
#1 lore raw
Quoted 17 lines · Alan Cox

Working on tracing down Fedora bug #115458
(https://bugzilla.redhat.com/bugzilla/process_bug.cgi) I found what appears to be a race between the IDE CD driver and the hardware status. It doesn't appear to explain the bug at all but it does look like a bug of itself

When we issue an ide command the status bits don't become valid for 400nS. In the DMA case ide_execute_command handles this but in the PIO case we don't do the needed locking, use OUTBSYNC to avoid posting or delay. This means that in some situations we can execute the command handler in PIO mode before the command status bits are valid and the handler may read and act wrongly.

ide/ide-cd.c renamed from ide/ide-cd.c~ +0 -0
@@ -897,7 +897,10 @@
 		return ide_started;

ide/ide-cd.c line 906

Quoted 10 lines
ide/ide-cd.c renamed from ide/ide-cd.c~ +4 -1
 	} else {
 		/* packet command */
-		HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
+		spin_lock_irqsave(&ide_lock, flags);
+		HWIF(drive)->OUTBSYNC(WIN_PACKETCMD, IDE_COMMAND_REG);
+		ndelay(400);
+		spin_unlock_irqsave(&ide_lock, flags);
 		return (*handler) (drive);
 	}
 }

What good does the lock do?

Signature · 1 line
Jens Axboe
author
#2 lore raw
Quoted 10 lines · Jens Axboe

ide/ide-cd.c line 906

Quoted 8 lines
ide/ide-cd.c renamed from ide/ide-cd.c~ +4 -1
-		HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
+		spin_lock_irqsave(&ide_lock, flags);
+		HWIF(drive)->OUTBSYNC(WIN_PACKETCMD, IDE_COMMAND_REG);
+		ndelay(400);
+		spin_unlock_irqsave(&ide_lock, flags);
 		return (*handler) (drive);
 	}
 }

What good does the lock do?

The same as in ide_execute_command - make sure we don't take an IDE interrupt that tries to read the state during the delay. That is the old 2.4 "may drives shared IRQ random fails" fix and why the lock is taken in ide_execute_command.

#3 lore raw
Quoted 12 lines · Alan Cox
Quoted 10 lines · Jens Axboe

ide/ide-cd.c line 906

Quoted 8 lines
ide/ide-cd.c renamed from ide/ide-cd.c~ +4 -1
-		HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
+		spin_lock_irqsave(&ide_lock, flags);
+		HWIF(drive)->OUTBSYNC(WIN_PACKETCMD, IDE_COMMAND_REG);
+		ndelay(400);
+		spin_unlock_irqsave(&ide_lock, flags);
 		return (*handler) (drive);
 	}
 }

What good does the lock do?

Quoted 4 lines

The same as in ide_execute_command - make sure we don't take an IDE interrupt that tries to read the state during the delay. That is the old 2.4 "may drives shared IRQ random fails" fix and why the lock is taken in ide_execute_command.

Ah I see, makes sense. I didn't think about the shared irq case.

Signature · 1 line
Jens Axboe
#4 lore raw
In reply to Jens Axboe
Quoted 14 lines · Jens Axboe
Quoted 13 lines · Alan Cox

Working on tracing down Fedora bug #115458
(https://bugzilla.redhat.com/bugzilla/process_bug.cgi) I found what appears to be a race between the IDE CD driver and the hardware status. It doesn't appear to explain the bug at all but it does look like a bug of itself

When we issue an ide command the status bits don't become valid for 400nS. In the DMA case ide_execute_command handles this but in the PIO case we don't do the needed locking, use OUTBSYNC to avoid posting or delay. This means that in some situations we can execute the command handler in PIO mode before the command status bits are valid and the handler may read and act wrongly.

Quoted 13 lines

ide/ide-cd.c line 905

Quoted 13 lines
ide/ide-cd.c renamed from ide/ide-cd.c~ +4 -1
@@ -897,7 +897,10 @@
 		return ide_started;
 	} else {
 		/* packet command */
-		HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
+		spin_lock_irqsave(&ide_lock, flags);
+		HWIF(drive)->OUTBSYNC(WIN_PACKETCMD, IDE_COMMAND_REG);
+		ndelay(400);
+		spin_unlock_irqsave(&ide_lock, flags);
 		return (*handler) (drive);
 	}

btw alan, have you attempted to compile this? It averages 2 errors out of 4 lines :)

Signature · 1 line
Jens Axboe
author
#5 lore raw
Quoted 6 lines · Jens Axboe
Quoted 3 lines

ide/ide-cd.c line 905

Quoted 3 lines
ide/ide-cd.c renamed from ide/ide-cd.c~ +1 -0
+		spin_unlock_irqsave(&ide_lock, flags);
 		return (*handler) (drive);
 	}

btw alan, have you attempted to compile this? It averages 2 errors out of 4 lines :)

Guess why it said "for comment". The one that does compile is in current -ac
but the fixes are kind of obvious 8)