Skip to content

Commit af76e55

Browse files
Christoph Hellwigaxboe
authored andcommitted
blk-mq: initialize struct request fields individually
This allows us to avoid a non-atomic memset over ->atomic_flags as well as killing lots of duplicate initializations. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent 9fccfed commit af76e55

2 files changed

Lines changed: 49 additions & 5 deletions

File tree

‎block/blk-mq.c‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
8282
tag = blk_mq_get_tag(hctx->tags, gfp, reserved);
8383
if (tag != BLK_MQ_TAG_FAIL) {
8484
rq = hctx->tags->rqs[tag];
85-
blk_rq_init(hctx->queue, rq);
8685
rq->tag = tag;
87-
8886
return rq;
8987
}
9088

@@ -187,10 +185,54 @@ static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
187185
if (blk_queue_io_stat(q))
188186
rw_flags |= REQ_IO_STAT;
189187

188+
INIT_LIST_HEAD(&rq->queuelist);
189+
/* csd/requeue_work/fifo_time is initialized before use */
190+
rq->q = q;
190191
rq->mq_ctx = ctx;
191192
rq->cmd_flags = rw_flags;
193+
rq->cmd_type = 0;
194+
/* do not touch atomic flags, it needs atomic ops against the timer */
195+
rq->cpu = -1;
196+
rq->__data_len = 0;
197+
rq->__sector = (sector_t) -1;
198+
rq->bio = NULL;
199+
rq->biotail = NULL;
200+
INIT_HLIST_NODE(&rq->hash);
201+
RB_CLEAR_NODE(&rq->rb_node);
202+
memset(&rq->flush, 0, max(sizeof(rq->flush), sizeof(rq->elv)));
203+
rq->rq_disk = NULL;
204+
rq->part = NULL;
192205
rq->start_time = jiffies;
206+
#ifdef CONFIG_BLK_CGROUP
207+
rq->rl = NULL;
193208
set_start_time_ns(rq);
209+
rq->io_start_time_ns = 0;
210+
#endif
211+
rq->nr_phys_segments = 0;
212+
#if defined(CONFIG_BLK_DEV_INTEGRITY)
213+
rq->nr_integrity_segments = 0;
214+
#endif
215+
rq->ioprio = 0;
216+
rq->special = NULL;
217+
/* tag was already set */
218+
rq->errors = 0;
219+
memset(rq->__cmd, 0, sizeof(rq->__cmd));
220+
rq->cmd = rq->__cmd;
221+
rq->cmd_len = BLK_MAX_CDB;
222+
223+
rq->extra_len = 0;
224+
rq->sense_len = 0;
225+
rq->resid_len = 0;
226+
rq->sense = NULL;
227+
228+
rq->deadline = 0;
229+
INIT_LIST_HEAD(&rq->timeout_list);
230+
rq->timeout = 0;
231+
rq->retries = 0;
232+
rq->end_io = NULL;
233+
rq->end_io_data = NULL;
234+
rq->next_rq = NULL;
235+
194236
ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
195237
}
196238

@@ -258,6 +300,7 @@ static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
258300
const int tag = rq->tag;
259301
struct request_queue *q = rq->q;
260302

303+
clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
261304
blk_mq_put_tag(hctx->tags, tag);
262305
blk_mq_queue_exit(q);
263306
}

‎include/linux/blkdev.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ enum rq_cmd_type_bits {
9090
#define BLK_MAX_CDB 16
9191

9292
/*
93-
* try to put the fields that are referenced together in the same cacheline.
94-
* if you modify this structure, be sure to check block/blk-core.c:blk_rq_init()
95-
* as well!
93+
* Try to put the fields that are referenced together in the same cacheline.
94+
*
95+
* If you modify this structure, make sure to update blk_rq_init() and
96+
* especially blk_mq_rq_ctx_init() to take care of the added fields.
9697
*/
9798
struct request {
9899
struct list_head queuelist;

0 commit comments

Comments
 (0)