Skip to contentSkip to conversation

AS spin lock bugs

PATCH Jens Axboe 2 participants 8 messages Nov 13, 2003 Block layer

lore
Share & tools
lore

Jens Axboe

author
#0 lore raw

Hi,

Was looking at io tracking for cfq, and I think I found some spin lock bugs in current as (current BK). as_update_iohist() runs from add_request which is typically in process context. It could be run with interrupts disabled though, either driver private stuff or using the generic block layer tagging.

Anyways, as_update_iohist() grabs aic->lock without disabling interrupts, while as_completed_request() typically runs at interrupt time and grabs the same lock. Deadlock.

To be safe, both need to use the flags saving lock variants.

===== drivers/block/as-iosched.c 1.28 vs edited =====

drivers/block/as-iosched.c +6 -5
@@ -783,14 +783,14 @@
 {
 	struct as_rq *arq = RQ_DATA(rq);
 	int data_dir = arq->is_sync;
-	unsigned long thinktime;
+	unsigned long thinktime, flags;
 	sector_t seek_dist;
 
 	if (aic == NULL)
 		return;
 
 	if (data_dir == REQ_SYNC) {
-		spin_lock(&aic->lock);
+		spin_lock_irqsave(&aic->lock, flags);
 
 		if (test_bit(AS_TASK_IORUNNING, &aic->state)
 				&& !atomic_read(&aic->nr_queued)
@@ -844,7 +844,7 @@
 		aic->seek_total = (aic->seek_total>>1)
 					+ (aic->seek_total>>2);
 
-		spin_unlock(&aic->lock);
+		spin_unlock_irqrestore(&aic->lock, flags);
 	}
 }
 
@@ -909,6 +909,7 @@
 	struct as_data *ad = q->elevator.elevator_data;
 	struct as_rq *arq = RQ_DATA(rq);
 	struct as_io_context *aic;
+	unsigned long flags;
 
 	WARN_ON(!list_empty(&rq->queuelist));
 
@@ -959,12 +960,12 @@
 	if (!aic)
 		return;
 
-	spin_lock(&aic->lock);
+	spin_lock_irqsave(&aic->lock, flags);
 	if (arq->is_sync == REQ_SYNC) {
 		set_bit(AS_TASK_IORUNNING, &aic->state);
 		aic->last_end_request = jiffies;
 	}
-	spin_unlock(&aic->lock);
+	spin_unlock_irqrestore(&aic->lock, flags);
 
 	put_io_context(arq->io_context);
 }
Signature · 1 line
Jens Axboe
author
#1 lore raw
Quoted 5 lines · Jens Axboe
drivers/block/as-iosched.c +0 -1
@@ -959,12 +960,12 @@
 	if (!aic)
 		return;
 
-	spin_lock(&aic->lock);

drivers/block/as-iosched.c line 971

Quoted 10 lines
drivers/block/as-iosched.c +2 -1
+	spin_lock_irqsave(&aic->lock, flags);
 	if (arq->is_sync == REQ_SYNC) {
 		set_bit(AS_TASK_IORUNNING, &aic->state);
 		aic->last_end_request = jiffies;
 	}
-	spin_unlock(&aic->lock);
+	spin_unlock_irqrestore(&aic->lock, flags);
 
 	put_io_context(arq->io_context);
 }

BTW, this looks bogus. Why do you need any locking there?

Signature · 1 line
Jens Axboe
#6 lore raw
In reply to Jens Axboe
Quoted 8 lines · Jens Axboe

Hi,

Was looking at io tracking for cfq, and I think I found some spin lock bugs in current as (current BK). as_update_iohist() runs from add_request which is typically in process context. It could be run with interrupts disabled though, either driver private stuff or using the generic block layer tagging.

Quoted 6 lines

Anyways, as_update_iohist() grabs aic->lock without disabling interrupts, while as_completed_request() typically runs at interrupt time and grabs the same lock. Deadlock.

To be safe, both need to use the flags saving lock variants.

Hi Jens,
I was hoping everything ran under the queue lock which should always have interrupts off on the local CPU. The lock in question is to prevent a as_completed_request on one queue from racing with as_update_iohist on another. Each would be on a different CPU.

Maybe I'm wrong, did you actually see misbehaviour?

author
#7 lore raw
Quoted 16 lines · Nick Piggin
Quoted 12 lines · Jens Axboe

Hi,

Was looking at io tracking for cfq, and I think I found some spin lock bugs in current as (current BK). as_update_iohist() runs from add_request which is typically in process context. It could be run with interrupts disabled though, either driver private stuff or using the generic block layer tagging.

Anyways, as_update_iohist() grabs aic->lock without disabling interrupts, while as_completed_request() typically runs at interrupt time and grabs the same lock. Deadlock.

Quoted 8 lines
Quoted 2 lines

To be safe, both need to use the flags saving lock variants.

Hi Jens,
I was hoping everything ran under the queue lock which should always have interrupts off on the local CPU. The lock in question is to prevent a as_completed_request on one queue from racing with as_update_iohist on another. Each would be on a different CPU.

Ah yes you are right. The queue lock will be held in both places.

Quoted 1 line

Maybe I'm wrong, did you actually see misbehaviour?

Nope, just looking over the code. What about the second lock, why is that needed? I don't see that protecting anything.

Signature · 1 line
Jens Axboe
#2 lore raw
In reply to Jens Axboe
Quoted 15 lines · Jens Axboe
Quoted 13 lines · Jens Axboe
drivers/block/as-iosched.c +2 -2
@@ -959,12 +960,12 @@
 	if (!aic)
 		return;
 
-	spin_lock(&aic->lock);
+	spin_lock_irqsave(&aic->lock, flags);
 	if (arq->is_sync == REQ_SYNC) {
 		set_bit(AS_TASK_IORUNNING, &aic->state);
 		aic->last_end_request = jiffies;
 	}
-	spin_unlock(&aic->lock);
+	spin_unlock_irqrestore(&aic->lock, flags);
 
Quoted 6 lines

drivers/block/as-iosched.c line 971

Quoted 3 lines
drivers/block/as-iosched.c +0 -0
 	put_io_context(arq->io_context);
 }

BTW, this looks bogus. Why do you need any locking there?

To prevent a request completion on another queue on another CPU from racing with request insertion: last_end_request is undefined if the flag is not set. I guess you could flip the statements and put a smp_mb between them. Probably not worth the trouble though.

author
#3 lore raw
Quoted 26 lines · Nick Piggin
Quoted 15 lines · Jens Axboe
Quoted 13 lines · Jens Axboe
drivers/block/as-iosched.c +2 -2
@@ -959,12 +960,12 @@
 	if (!aic)
 		return;
 
-	spin_lock(&aic->lock);
+	spin_lock_irqsave(&aic->lock, flags);
 	if (arq->is_sync == REQ_SYNC) {
 		set_bit(AS_TASK_IORUNNING, &aic->state);
 		aic->last_end_request = jiffies;
 	}
-	spin_unlock(&aic->lock);
+	spin_unlock_irqrestore(&aic->lock, flags);
 
Quoted 6 lines

drivers/block/as-iosched.c line 971

Quoted 3 lines
drivers/block/as-iosched.c +0 -0
 	put_io_context(arq->io_context);
 }

BTW, this looks bogus. Why do you need any locking there?

Quoted 4 lines

To prevent a request completion on another queue on another CPU from racing with request insertion: last_end_request is undefined if the flag is not set. I guess you could flip the statements and put a smp_mb between them. Probably not worth the trouble though.

No better to make it explicit, probably doesn't matter much in real-life. Thanks for the clarifications.

Signature · 1 line
Jens Axboe
author
#4 lore raw
Quoted 27 lines · Jens Axboe
Quoted 26 lines · Nick Piggin
Quoted 21 lines · Jens Axboe
Quoted 6 lines · Jens Axboe
drivers/block/as-iosched.c +0 -1
@@ -959,12 +960,12 @@
 	if (!aic)
 		return;
 
-	spin_lock(&aic->lock);

drivers/block/as-iosched.c line 971

Quoted 10 lines
drivers/block/as-iosched.c +2 -1
+	spin_lock_irqsave(&aic->lock, flags);
 	if (arq->is_sync == REQ_SYNC) {
 		set_bit(AS_TASK_IORUNNING, &aic->state);
 		aic->last_end_request = jiffies;
 	}
-	spin_unlock(&aic->lock);
+	spin_unlock_irqrestore(&aic->lock, flags);
 
 	put_io_context(arq->io_context);
 }

BTW, this looks bogus. Why do you need any locking there?

Quoted 7 lines
Quoted 4 lines

To prevent a request completion on another queue on another CPU from racing with request insertion: last_end_request is undefined if the flag is not set. I guess you could flip the statements and put a smp_mb between them. Probably not worth the trouble though.

No better to make it explicit, probably doesn't matter much in real-life. Thanks for the clarifications.

Ah, it would be clearer as:

	if (arq->is_sync == REQ_SYNC) {
		spin_lock(&aic->lock);
		set_bit(AS_TASK_IORUNNING, &aic->state);
		aic->last_end_request = jiffies;
		spin_unlock(&aic->lock);
	}

Then it doesn't need comments :)

Signature · 1 line
Jens Axboe
#5 lore raw
Quoted 42 lines · Jens Axboe
Quoted 29 lines · Jens Axboe
Quoted 27 lines · Nick Piggin
Quoted 23 lines · Jens Axboe
Quoted 7 lines · Jens Axboe
drivers/block/as-iosched.c +0 -1
@@ -959,12 +960,12 @@
 	if (!aic)
 		return;
 
-	spin_lock(&aic->lock);

drivers/block/as-iosched.c line 971

Quoted 10 lines
drivers/block/as-iosched.c +2 -1
+	spin_lock_irqsave(&aic->lock, flags);
 	if (arq->is_sync == REQ_SYNC) {
 		set_bit(AS_TASK_IORUNNING, &aic->state);
 		aic->last_end_request = jiffies;
 	}
-	spin_unlock(&aic->lock);
+	spin_unlock_irqrestore(&aic->lock, flags);
 
 	put_io_context(arq->io_context);
 }

BTW, this looks bogus. Why do you need any locking there?

Quoted 8 lines
Quoted 5 lines

To prevent a request completion on another queue on another CPU from racing with request insertion: last_end_request is undefined if the flag is not set. I guess you could flip the statements and put a smp_mb between them. Probably not worth the trouble though.

No better to make it explicit, probably doesn't matter much in real-life. Thanks for the clarifications.

Ah, it would be clearer as:

Quoted 9 lines
	if (arq->is_sync == REQ_SYNC) {
		spin_lock(&aic->lock);
		set_bit(AS_TASK_IORUNNING, &aic->state);
		aic->last_end_request = jiffies;
		spin_unlock(&aic->lock);
	}

Then it doesn't need comments :)

Yeah thats was a bit silly of me I see why you got confused. I have actually fixed this up in mm3. So it should get through to Linus sometime after 2.6.0.