Skip to contentSkip to conversation

bug in 2.4 bh_kmap_irq() breaks IDE under preempt patch

PATCH In review Joe Korty 4 participants 8 messages Mar 12, 2003 – Mar 13, 2003

lore
Share & tools
lore

Joe Korty

author
#0 lore raw

PATCH 1/1 bug in 2.4 bh_kmap_irq() breaks IDE under preempt patch

Hi Alan, Everyone,
The bh_map_irq/bh_unmap_irq functions are broken in 2.4.21-pre5. However no symptoms occur unless the preemption patch is applied.

The bug is that bh_map_irq *conditionally* calls kmap_atomic (which disables preemption as one of its functions), while bh_unmap_irq *unconditionally* calls kunmap_atomic (which enables it). This imbalance results in a occasional off-by-one preempt_count, which in turn causes IDE PIO mode interrupt code (specifically, read_intr) to erronously invoke preempt_schedule while at interrupt level.

The below patch compiles and boots ide=nodma on my preempt 2.4 kernel on the one motherboard that had the problem. Before this patch, the kernel would not even boot for that motherboard. I also applied and test booted a pure 2.4.21-pre5 kernel with this patch.

The patch implements my preference for simplicity, so you may want to take some other approach if maximal performance is what you want.

Joe

linux/highmem.h renamed from linux/highmem.h.orig +3 -15
@@ -33,22 +33,10 @@
 {
 	unsigned long addr;
 
-	__save_flags(*flags);
-
-	/*
-	 * could be low
-	 */
-	if (!PageHighMem(bh->b_page))
-		return bh->b_data;
-
-	/*
-	 * it's a highmem page
-	 */
-	__cli();
+	local_irq_save(*flags);
 	addr = (unsigned long) kmap_atomic(bh->b_page, KM_BH_IRQ);
 
-	if (addr & ~PAGE_MASK)
-		BUG();
+	BUG_ON (addr & ~PAGE_MASK);
 
 	return (char *) addr + bh_offset(bh);
 }
@@ -58,7 +46,7 @@
 	unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
 
 	kunmap_atomic((void *) ptr, KM_BH_IRQ);
-	__restore_flags(*flags);
+	local_irq_restore(*flags);
 }
 
 #else /* CONFIG_HIGHMEM */
#1 lore raw
Quoted 3 lines · Joe Korty

The bug is that bh_map_irq *conditionally* calls kmap_atomic (which disables preemption as one of its functions), while bh_unmap_irq *unconditionally* calls kunmap_atomic (which enables it). This

Thats a pre-empt bug ont a bh_map_irq bug. I'm glad you've found it however. It explains a few things and will be useful for people wanting pre-empt 2.4 .

#2 lore raw
In reply to Joe Korty
Quoted 11 lines · Joe Korty

Hi Alan, Everyone,
The bh_map_irq/bh_unmap_irq functions are broken in 2.4.21-pre5. However no symptoms occur unless the preemption patch is applied.

The bug is that bh_map_irq *conditionally* calls kmap_atomic (which disables preemption as one of its functions), while bh_unmap_irq *unconditionally* calls kunmap_atomic (which enables it). This imbalance results in a occasional off-by-one preempt_count, which in turn causes IDE PIO mode interrupt code (specifically, read_intr) to erronously invoke preempt_schedule while at interrupt level.

Quoted 7 lines

The below patch compiles and boots ide=nodma on my preempt 2.4 kernel on the one motherboard that had the problem. Before this patch, the kernel would not even boot for that motherboard. I also applied and test booted a pure 2.4.21-pre5 kernel with this patch.

The patch implements my preference for simplicity, so you may want to take some other approach if maximal performance is what you want.

I fixed this in 2.5 ages ago, just didn't get it in 2.4 block-highmem... There's a tiny bit missing from your patch:

Quoted 9 lines
linux/highmem.h renamed from linux/highmem.h.orig +0 -3
@@ -33,22 +33,10 @@
 {
 	unsigned long addr;
 
-	__save_flags(*flags);
-
-	/*

linux/highmem.h line 36

Quoted 10 lines
linux/highmem.h renamed from linux/highmem.h.orig +1 -9
-	 * could be low
-	 */
-	if (!PageHighMem(bh->b_page))
-		return bh->b_data;
-
-	/*
-	 * it's a highmem page
-	 */
-	__cli();
+	local_irq_save(*flags);
	local_irq_disable();
Quoted 9 lines
linux/highmem.h renamed from linux/highmem.h.orig +1 -2
 	addr = (unsigned long) kmap_atomic(bh->b_page, KM_BH_IRQ);
 
-	if (addr & ~PAGE_MASK)
-		BUG();
+	BUG_ON (addr & ~PAGE_MASK);
 
 	return (char *) addr + bh_offset(bh);
 }

linux/highmem.h line 50

Quoted 6 lines
linux/highmem.h renamed from linux/highmem.h.orig +1 -1
@@ -58,7 +46,7 @@
 	unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
 
 	kunmap_atomic((void *) ptr, KM_BH_IRQ);
-	__restore_flags(*flags);
+	local_irq_restore(*flags);
 }

other than that it's fine. See 2.5 for reference.

Signature · 1 line
Jens Axboe
#3 lore raw

On Thursday 13 March 2003 10:26, Jens Axboe wrote:

Hi Jens,

Quoted 14 lines

There's a tiny bit missing from your patch:

linux/highmem.h line 36

Quoted 4 lines
linux/highmem.h renamed from linux/highmem.h.orig +1 -3
-	 * it's a highmem page
-	 */
-	__cli();
+	local_irq_save(*flags);
	local_irq_disable();
Quoted 6 lines
linux/highmem.h renamed from linux/highmem.h.orig +1 -2
 	addr = (unsigned long) kmap_atomic(bh->b_page, KM_BH_IRQ);
 
-	if (addr & ~PAGE_MASK)
-		BUG();
+	BUG_ON (addr & ~PAGE_MASK);
Quoted 8 lines

linux/highmem.h line 49

Quoted 8 lines
linux/highmem.h renamed from linux/highmem.h.orig +1 -1
 	return (char *) addr + bh_offset(bh);
 }
@@ -58,7 +46,7 @@
 	unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
 
 	kunmap_atomic((void *) ptr, KM_BH_IRQ);
-	__restore_flags(*flags);
+	local_irq_restore(*flags);
	local_irq_enable();

^ isn't this missing too with your suggested one-liner?

ciao, Marc

#4 lore raw
Quoted 23 lines · Marc-Christian Petersen

On Thursday 13 March 2003 10:26, Jens Axboe wrote:

Hi Jens,

Quoted 19 lines

There's a tiny bit missing from your patch:

linux/highmem.h line 36

Quoted 4 lines
linux/highmem.h renamed from linux/highmem.h.orig +1 -3
-	 * it's a highmem page
-	 */
-	__cli();
+	local_irq_save(*flags);
	local_irq_disable();
Quoted 11 lines
linux/highmem.h renamed from linux/highmem.h.orig +1 -2
 	addr = (unsigned long) kmap_atomic(bh->b_page, KM_BH_IRQ);
 
-	if (addr & ~PAGE_MASK)
-		BUG();
+	BUG_ON (addr & ~PAGE_MASK);
 
 	return (char *) addr + bh_offset(bh);
 }
@@ -58,7 +46,7 @@
 	unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
 
Quoted 6 lines
Quoted 3 lines

linux/highmem.h line 49

Quoted 3 lines
linux/highmem.h renamed from linux/highmem.h.orig +1 -1
 	kunmap_atomic((void *) ptr, KM_BH_IRQ);
-	__restore_flags(*flags);
+	local_irq_restore(*flags);
	local_irq_enable();

^ isn't this missing too with your suggested one-liner?

no, the local_irq_restore() brings back the irq flags from before we did the irq disable. if interrupts were disabled before bh_kmap_irq() was called, we must not enable them. basically, maintain the same flags.

Signature · 1 line
Jens Axboe
#5 lore raw

On Thursday 13 March 2003 11:28, Jens Axboe wrote:

Hi Jens,

Quoted 5 lines
Quoted 2 lines
	local_irq_enable();

^ isn't this missing too with your suggested one-liner?

no, the local_irq_restore() brings back the irq flags from before we did the irq disable. if interrupts were disabled before bh_kmap_irq() was called, we must not enable them. basically, maintain the same flags.

hmm, then I am blind 8-) ... Thanks.

ciao, Marc

author
#6 lore raw
In reply to Jens Axboe
Quoted 7 lines

I fixed this in 2.5 ages ago, just didn't get it in 2.4 block-highmem... There's a tiny bit missing from your patch:

linux/highmem.h line 36

Quoted 1 line
linux/highmem.h renamed from linux/highmem.h.orig +1 -0
+	local_irq_save(*flags);
	local_irq_disable();

other than that it's fine. See 2.5 for reference.

local_irq_save() does a local_irq_disable() as part of its function. Joe

#7 lore raw
Quoted 9 lines · Joe Korty
Quoted 7 lines

I fixed this in 2.5 ages ago, just didn't get it in 2.4 block-highmem... There's a tiny bit missing from your patch:

linux/highmem.h line 36

Quoted 1 line
linux/highmem.h renamed from linux/highmem.h.orig +1 -0
+	local_irq_save(*flags);
	local_irq_disable();

other than that it's fine. See 2.5 for reference.

local_irq_save() does a local_irq_disable() as part of its function.

indeed, you are right.

Signature · 1 line
Jens Axboe