Skip to content

Commit 0e9911f

Browse files
asfacamstsirkin
authored andcommitted
virtio-blk: support mq_ops->queue_rqs()
This patch supports mq_ops->queue_rqs() hook. It has an advantage of batch submission to virtio-blk driver. It also helps polling I/O because polling uses batched completion of block layer. Batch submission in queue_rqs() can boost polling performance. In queue_rqs(), it iterates plug->mq_list, collects requests that belong to same HW queue until it encounters a request from other HW queue or sees the end of the list. Then, virtio-blk adds requests into virtqueue and kicks virtqueue to submit requests. If there is an error, it inserts error request to requeue_list and passes it to ordinary block layer path. For verification, I did fio test. (io_uring, randread, direct=1, bs=4K, iodepth=64 numjobs=N) I set 4 vcpu and 2 virtio-blk queues for VM and run fio test 5 times. It shows about 2% improvement. | numjobs=2 | numjobs=4 ----------------------------------------------------------- fio without queue_rqs() | 291K IOPS | 238K IOPS ----------------------------------------------------------- fio with queue_rqs() | 295K IOPS | 243K IOPS For polling I/O performance, I also did fio test as below. (io_uring, hipri, randread, direct=1, bs=512, iodepth=64 numjobs=4) I set 4 vcpu and 2 poll queues for VM. It shows about 2% improvement in polling I/O. | IOPS | avg latency ----------------------------------------------------------- fio poll without queue_rqs() | 424K | 613.05 usec ----------------------------------------------------------- fio poll with queue_rqs() | 435K | 601.01 usec Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Suwan Kim <suwan.kim027@gmail.com> Message-Id: <20220406153207.163134-3-suwan.kim027@gmail.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Reviewed-by: Chaitanya Kulkarni <kch@nvidia.com>
1 parent 4e04005 commit 0e9911f

File tree

1 file changed

+98
-16
lines changed

1 file changed

+98
-16
lines changed

drivers/block/virtio_blk.c

Lines changed: 98 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,19 @@ static inline blk_status_t virtblk_result(struct virtblk_req *vbr)
101101
}
102102
}
103103

104-
static int virtblk_add_req(struct virtqueue *vq, struct virtblk_req *vbr,
105-
struct scatterlist *data_sg, bool have_data)
104+
static int virtblk_add_req(struct virtqueue *vq, struct virtblk_req *vbr)
106105
{
107106
struct scatterlist hdr, status, *sgs[3];
108107
unsigned int num_out = 0, num_in = 0;
109108

110109
sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
111110
sgs[num_out++] = &hdr;
112111

113-
if (have_data) {
112+
if (vbr->sg_table.nents) {
114113
if (vbr->out_hdr.type & cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_OUT))
115-
sgs[num_out++] = data_sg;
114+
sgs[num_out++] = vbr->sg_table.sgl;
116115
else
117-
sgs[num_out + num_in++] = data_sg;
116+
sgs[num_out + num_in++] = vbr->sg_table.sgl;
118117
}
119118

120119
sg_init_one(&status, &vbr->status, sizeof(vbr->status));
@@ -304,33 +303,46 @@ static void virtio_commit_rqs(struct blk_mq_hw_ctx *hctx)
304303
virtqueue_notify(vq->vq);
305304
}
306305

306+
static blk_status_t virtblk_prep_rq(struct blk_mq_hw_ctx *hctx,
307+
struct virtio_blk *vblk,
308+
struct request *req,
309+
struct virtblk_req *vbr)
310+
{
311+
blk_status_t status;
312+
313+
status = virtblk_setup_cmd(vblk->vdev, req, vbr);
314+
if (unlikely(status))
315+
return status;
316+
317+
blk_mq_start_request(req);
318+
319+
vbr->sg_table.nents = virtblk_map_data(hctx, req, vbr);
320+
if (unlikely(vbr->sg_table.nents < 0)) {
321+
virtblk_cleanup_cmd(req);
322+
return BLK_STS_RESOURCE;
323+
}
324+
325+
return BLK_STS_OK;
326+
}
327+
307328
static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
308329
const struct blk_mq_queue_data *bd)
309330
{
310331
struct virtio_blk *vblk = hctx->queue->queuedata;
311332
struct request *req = bd->rq;
312333
struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
313334
unsigned long flags;
314-
int num;
315335
int qid = hctx->queue_num;
316336
bool notify = false;
317337
blk_status_t status;
318338
int err;
319339

320-
status = virtblk_setup_cmd(vblk->vdev, req, vbr);
340+
status = virtblk_prep_rq(hctx, vblk, req, vbr);
321341
if (unlikely(status))
322342
return status;
323343

324-
blk_mq_start_request(req);
325-
326-
num = virtblk_map_data(hctx, req, vbr);
327-
if (unlikely(num < 0)) {
328-
virtblk_cleanup_cmd(req);
329-
return BLK_STS_RESOURCE;
330-
}
331-
332344
spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
333-
err = virtblk_add_req(vblk->vqs[qid].vq, vbr, vbr->sg_table.sgl, num);
345+
err = virtblk_add_req(vblk->vqs[qid].vq, vbr);
334346
if (err) {
335347
virtqueue_kick(vblk->vqs[qid].vq);
336348
/* Don't stop the queue if -ENOMEM: we may have failed to
@@ -360,6 +372,75 @@ static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
360372
return BLK_STS_OK;
361373
}
362374

375+
static bool virtblk_prep_rq_batch(struct request *req)
376+
{
377+
struct virtio_blk *vblk = req->mq_hctx->queue->queuedata;
378+
struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
379+
380+
req->mq_hctx->tags->rqs[req->tag] = req;
381+
382+
return virtblk_prep_rq(req->mq_hctx, vblk, req, vbr) == BLK_STS_OK;
383+
}
384+
385+
static bool virtblk_add_req_batch(struct virtio_blk_vq *vq,
386+
struct request **rqlist,
387+
struct request **requeue_list)
388+
{
389+
unsigned long flags;
390+
int err;
391+
bool kick;
392+
393+
spin_lock_irqsave(&vq->lock, flags);
394+
395+
while (!rq_list_empty(*rqlist)) {
396+
struct request *req = rq_list_pop(rqlist);
397+
struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
398+
399+
err = virtblk_add_req(vq->vq, vbr);
400+
if (err) {
401+
virtblk_unmap_data(req, vbr);
402+
virtblk_cleanup_cmd(req);
403+
rq_list_add(requeue_list, req);
404+
}
405+
}
406+
407+
kick = virtqueue_kick_prepare(vq->vq);
408+
spin_unlock_irqrestore(&vq->lock, flags);
409+
410+
return kick;
411+
}
412+
413+
static void virtio_queue_rqs(struct request **rqlist)
414+
{
415+
struct request *req, *next, *prev = NULL;
416+
struct request *requeue_list = NULL;
417+
418+
rq_list_for_each_safe(rqlist, req, next) {
419+
struct virtio_blk_vq *vq = req->mq_hctx->driver_data;
420+
bool kick;
421+
422+
if (!virtblk_prep_rq_batch(req)) {
423+
rq_list_move(rqlist, &requeue_list, req, prev);
424+
req = prev;
425+
if (!req)
426+
continue;
427+
}
428+
429+
if (!next || req->mq_hctx != next->mq_hctx) {
430+
req->rq_next = NULL;
431+
kick = virtblk_add_req_batch(vq, rqlist, &requeue_list);
432+
if (kick)
433+
virtqueue_notify(vq->vq);
434+
435+
*rqlist = next;
436+
prev = NULL;
437+
} else
438+
prev = req;
439+
}
440+
441+
*rqlist = requeue_list;
442+
}
443+
363444
/* return id (s/n) string for *disk to *id_str
364445
*/
365446
static int virtblk_get_id(struct gendisk *disk, char *id_str)
@@ -794,6 +875,7 @@ static int virtblk_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
794875

795876
static const struct blk_mq_ops virtio_mq_ops = {
796877
.queue_rq = virtio_queue_rq,
878+
.queue_rqs = virtio_queue_rqs,
797879
.commit_rqs = virtio_commit_rqs,
798880
.init_hctx = virtblk_init_hctx,
799881
.complete = virtblk_request_done,

0 commit comments

Comments
 (0)