Skip to content

Commit 05f1dd5

Browse files
committed
block: add queue flag for disabling SG merging
If devices are not SG starved, we waste a lot of time potentially collapsing SG segments. Enough that 1.5% of the CPU time goes to this, at only 400K IOPS. Add a queue flag, QUEUE_FLAG_NO_SG_MERGE, which just returns the number of vectors in a bio instead of looping over all segments and checking for collapsible ones. Add a BLK_MQ_F_SG_MERGE flag so that drivers can opt-in on the sg merging, if they so desire. Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent 4d92a9b commit 05f1dd5

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

‎block/blk-merge.c‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
1313
struct bio *bio)
1414
{
1515
struct bio_vec bv, bvprv = { NULL };
16-
int cluster, high, highprv = 1;
16+
int cluster, high, highprv = 1, no_sg_merge;
1717
unsigned int seg_size, nr_phys_segs;
1818
struct bio *fbio, *bbio;
1919
struct bvec_iter iter;
@@ -35,12 +35,21 @@ static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
3535
cluster = blk_queue_cluster(q);
3636
seg_size = 0;
3737
nr_phys_segs = 0;
38+
no_sg_merge = test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags);
39+
high = 0;
3840
for_each_bio(bio) {
3941
bio_for_each_segment(bv, bio, iter) {
42+
/*
43+
* If SG merging is disabled, each bio vector is
44+
* a segment
45+
*/
46+
if (no_sg_merge)
47+
goto new_segment;
48+
4049
/*
4150
* the trick here is making sure that a high page is
42-
* never considered part of another segment, since that
43-
* might change with the bounce page.
51+
* never considered part of another segment, since
52+
* that might change with the bounce page.
4453
*/
4554
high = page_to_pfn(bv.bv_page) > queue_bounce_pfn(q);
4655
if (!high && !highprv && cluster) {
@@ -84,11 +93,16 @@ void blk_recalc_rq_segments(struct request *rq)
8493

8594
void blk_recount_segments(struct request_queue *q, struct bio *bio)
8695
{
87-
struct bio *nxt = bio->bi_next;
96+
if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags))
97+
bio->bi_phys_segments = bio->bi_vcnt;
98+
else {
99+
struct bio *nxt = bio->bi_next;
100+
101+
bio->bi_next = NULL;
102+
bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio);
103+
bio->bi_next = nxt;
104+
}
88105

89-
bio->bi_next = NULL;
90-
bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio);
91-
bio->bi_next = nxt;
92106
bio->bi_flags |= (1 << BIO_SEG_VALID);
93107
}
94108
EXPORT_SYMBOL(blk_recount_segments);

‎block/blk-mq.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,9 @@ struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
18291829
q->mq_ops = set->ops;
18301830
q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
18311831

1832+
if (!(set->flags & BLK_MQ_F_SG_MERGE))
1833+
q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
1834+
18321835
q->sg_reserved_size = INT_MAX;
18331836

18341837
INIT_WORK(&q->requeue_work, blk_mq_requeue_work);

‎include/linux/blk-mq.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ enum {
129129
BLK_MQ_F_SHOULD_MERGE = 1 << 0,
130130
BLK_MQ_F_SHOULD_SORT = 1 << 1,
131131
BLK_MQ_F_TAG_SHARED = 1 << 2,
132+
BLK_MQ_F_SG_MERGE = 1 << 3,
132133

133134
BLK_MQ_S_STOPPED = 0,
134135
BLK_MQ_S_TAG_ACTIVE = 1,

‎include/linux/blkdev.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ struct request_queue {
510510
#define QUEUE_FLAG_SAME_FORCE 18 /* force complete on same CPU */
511511
#define QUEUE_FLAG_DEAD 19 /* queue tear-down finished */
512512
#define QUEUE_FLAG_INIT_DONE 20 /* queue is initialized */
513+
#define QUEUE_FLAG_NO_SG_MERGE 21 /* don't attempt to merge SG segments*/
513514

514515
#define QUEUE_FLAG_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
515516
(1 << QUEUE_FLAG_STACKABLE) | \

0 commit comments

Comments
 (0)