Skip to content

Commit 0738854

Browse files
Ming Leiaxboe
authored andcommitted
blk-merge: fix blk_recount_segments
QUEUE_FLAG_NO_SG_MERGE is set at default for blk-mq devices, so bio->bi_phys_segment computed may be bigger than queue_max_segments(q) for blk-mq devices, then drivers will fail to handle the case, for example, BUG_ON() in virtio_queue_rq() can be triggerd for virtio-blk: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1359146 This patch fixes the issue by ignoring the QUEUE_FLAG_NO_SG_MERGE flag if the computed bio->bi_phys_segment is bigger than queue_max_segments(q), and the regression is caused by commit 05f1dd5(block: add queue flag for disabling SG merging). Reported-by: Kick In <pierre-andre.morey@canonical.com> Tested-by: Chris J Arges <chris.j.arges@canonical.com> Signed-off-by: Ming Lei <ming.lei@canonical.com> Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent 7505cea commit 0738854

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

‎block/blk-merge.c‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
#include "blk.h"
1111

1212
static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
13-
struct bio *bio)
13+
struct bio *bio,
14+
bool no_sg_merge)
1415
{
1516
struct bio_vec bv, bvprv = { NULL };
16-
int cluster, high, highprv = 1, no_sg_merge;
17+
int cluster, high, highprv = 1;
1718
unsigned int seg_size, nr_phys_segs;
1819
struct bio *fbio, *bbio;
1920
struct bvec_iter iter;
@@ -35,7 +36,6 @@ static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
3536
cluster = blk_queue_cluster(q);
3637
seg_size = 0;
3738
nr_phys_segs = 0;
38-
no_sg_merge = test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags);
3939
high = 0;
4040
for_each_bio(bio) {
4141
bio_for_each_segment(bv, bio, iter) {
@@ -88,18 +88,23 @@ static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
8888

8989
void blk_recalc_rq_segments(struct request *rq)
9090
{
91-
rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio);
91+
bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE,
92+
&rq->q->queue_flags);
93+
94+
rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio,
95+
no_sg_merge);
9296
}
9397

9498
void blk_recount_segments(struct request_queue *q, struct bio *bio)
9599
{
96-
if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags))
100+
if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) &&
101+
bio->bi_vcnt < queue_max_segments(q))
97102
bio->bi_phys_segments = bio->bi_vcnt;
98103
else {
99104
struct bio *nxt = bio->bi_next;
100105

101106
bio->bi_next = NULL;
102-
bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio);
107+
bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false);
103108
bio->bi_next = nxt;
104109
}
105110

0 commit comments

Comments
 (0)