Skip to contentSkip to conversation

BUG: ll_merge_requests_fn() updates req->nr_phys_segments wrongly

PATCH In review Nikanth Karthikesan 3 participants 3 messages Sep 23, 2008 – Sep 25, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 BUG: ll_merge_requests_fn() updates req->nr_phys_segments wrongly

From: Nikanth Karthikesan <[email protected]>

ll_merge_requests_fn() decreases the req->nr_phys_segments if blk_phys_contig_segment() returns true, but it is perfectly possible that blk_hw_contig_segment() is false. A new hw_segment implies a new phys_segment. So decrementing nr_phys_segments wrongly here triggers the BUG_ON() in scsi_init_sgtable(), as blk_rq_map_sg() would map 1 phys_segment more than req->nr_phys_segment. This is easily reproducible. Other callers of blk_rq_map_sg() should also be affected by this bug.

---

 block/blk-merge.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)
block/blk-merge.c +0 -0
index 5efc9e7..6e6d04b 100644
@@ -392,11 +392,6 @@ static int ll_merge_requests_fn(struct request_queue *q, 

struct request *req,
return 0;

total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
- if (blk_phys_contig_segment(q, req->biotail, next->bio))
- total_phys_segments--;
-
- if (total_phys_segments > q->max_phys_segments)
- return 0;

total_hw_segments = req->nr_hw_segments + next->nr_hw_segments; if (blk_hw_contig_segment(q, req->biotail, next->bio)) {
@@ -410,9 +405,12 @@ static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
if (next->nr_hw_segments == 1)
next->biotail->bi_hw_back_size = len;
total_hw_segments--;
+ if (blk_phys_contig_segment(q, req->biotail, next->bio))
+ total_phys_segments--;
}

- if (total_hw_segments > q->max_hw_segments)
+ if (total_phys_segments > q->max_phys_segments ||
+ total_hw_segments > q->max_hw_segments)
return 0;

/* Merge is OK... */

#1 lore raw
Quoted 6 lines · On Tue, 23 Sep 2008 23:58:02 +0530 Nikanth Karthikesan <[email protected]> wrote:

From: Nikanth Karthikesan <[email protected]>

ll_merge_requests_fn() decreases the req->nr_phys_segments if blk_phys_contig_segment() returns true, but it is perfectly possible that blk_hw_contig_segment() is false.

Yeah, in fact, blk_hw_contig_segment() is always false on the majority of architectures (on only PARISC and Alpha, it could be true).

Your patch doesn't look correct. Virtually, the patch always disables physical merging.

Quoted 5 lines

A new hw_segment implies a new phys_segment.
So decrementing nr_phys_segments wrongly here triggers the BUG_ON() in scsi_init_sgtable(), as blk_rq_map_sg() would map 1 phys_segment more than req->nr_phys_segment. This is easily reproducible. Other callers of blk_rq_map_sg() should also be affected by this bug.

I have no idea how BUG_ON() in scsi_init_sgtable() is triggered.

Can you give more information, HBA, IOMMU (if you use), and the values of req->nr_phys_segment, req->nr_hw_segment, count, etc in in scsi_init_sgtable() when you hit the bug?

BTW, blk_hw_contig_segment() will be removed for 2.6.28 (virtual merging account in the block layer will be removed).

#2 lore raw
In reply to Nikanth Karthikesan
Quoted 22 lines · Nikanth Karthikesan

From: Nikanth Karthikesan <[email protected]>

ll_merge_requests_fn() decreases the req->nr_phys_segments if blk_phys_contig_segment() returns true, but it is perfectly possible that blk_hw_contig_segment() is false. A new hw_segment implies a new phys_segment. So decrementing nr_phys_segments wrongly here triggers the BUG_ON() in scsi_init_sgtable(), as blk_rq_map_sg() would map 1 phys_segment more than req->nr_phys_segment. This is easily reproducible. Other callers of blk_rq_map_sg() should also be affected by this bug.

---

 block/blk-merge.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)
block/blk-merge.c +0 -0
index 5efc9e7..6e6d04b 100644

block/blk-merge.c static int ll_merge_requests_fn(struct request_queue *q, · line 397

Quoted 6 lines
block/blk-merge.c +0 -2
@@ -392,11 +392,6 @@ static int ll_merge_requests_fn(struct request_queue *q, 
 struct request *req,
 		return 0;
 
 	total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
-	if (blk_phys_contig_segment(q, req->biotail, next->bio))
-		total_phys_segments--;

This is totally broken, I gather you are thinking the bug is fixed since the counts always match up. Well that's mainly because you know don't do ANY merging.

block/blk-merge.c static int ll_merge_requests_fn(struct request_queue *q, · line 399

Quoted 2 lines
block/blk-merge.c +0 -2
-	if (total_phys_segments > q->max_phys_segments)
-		return 0;

Hmm?

I think you should try and describe the problem you are seeing instead, then we can perhaps find the real issue.

Signature · 1 line
Jens Axboe