Skip to content

Commit 6a27b65

Browse files
Ming Leiaxboe
authored andcommitted
block: virtio-blk: support multi virt queues per virtio-blk device
Firstly this patch supports more than one virtual queues for virtio-blk device. Secondly this patch maps the virtual queue to blk-mq's hardware queue. With this approach, both scalability and performance can be improved. Signed-off-by: Ming Lei <ming.lei@canonical.com> Acked-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent cb55321 commit 6a27b65

1 file changed

Lines changed: 84 additions & 20 deletions

File tree

‎drivers/block/virtio_blk.c‎

Lines changed: 84 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,22 @@
1515
#include <linux/numa.h>
1616

1717
#define PART_BITS 4
18+
#define VQ_NAME_LEN 16
1819

1920
static int major;
2021
static DEFINE_IDA(vd_index_ida);
2122

2223
static struct workqueue_struct *virtblk_wq;
2324

25+
struct virtio_blk_vq {
26+
struct virtqueue *vq;
27+
spinlock_t lock;
28+
char name[VQ_NAME_LEN];
29+
} ____cacheline_aligned_in_smp;
30+
2431
struct virtio_blk
2532
{
2633
struct virtio_device *vdev;
27-
struct virtqueue *vq;
28-
spinlock_t vq_lock;
2934

3035
/* The disk structure for the kernel. */
3136
struct gendisk *disk;
@@ -47,6 +52,10 @@ struct virtio_blk
4752

4853
/* Ida index - used to track minor number allocations. */
4954
int index;
55+
56+
/* num of vqs */
57+
int num_vqs;
58+
struct virtio_blk_vq *vqs;
5059
};
5160

5261
struct virtblk_req
@@ -133,14 +142,15 @@ static void virtblk_done(struct virtqueue *vq)
133142
{
134143
struct virtio_blk *vblk = vq->vdev->priv;
135144
bool req_done = false;
145+
int qid = vq->index;
136146
struct virtblk_req *vbr;
137147
unsigned long flags;
138148
unsigned int len;
139149

140-
spin_lock_irqsave(&vblk->vq_lock, flags);
150+
spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
141151
do {
142152
virtqueue_disable_cb(vq);
143-
while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
153+
while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
144154
blk_mq_complete_request(vbr->req);
145155
req_done = true;
146156
}
@@ -151,7 +161,7 @@ static void virtblk_done(struct virtqueue *vq)
151161
/* In case queue is stopped waiting for more buffers. */
152162
if (req_done)
153163
blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
154-
spin_unlock_irqrestore(&vblk->vq_lock, flags);
164+
spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
155165
}
156166

157167
static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req)
@@ -160,6 +170,7 @@ static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req)
160170
struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
161171
unsigned long flags;
162172
unsigned int num;
173+
int qid = hctx->queue_num;
163174
const bool last = (req->cmd_flags & REQ_END) != 0;
164175
int err;
165176
bool notify = false;
@@ -202,25 +213,25 @@ static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req)
202213
vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
203214
}
204215

205-
spin_lock_irqsave(&vblk->vq_lock, flags);
206-
err = __virtblk_add_req(vblk->vq, vbr, vbr->sg, num);
216+
spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
217+
err = __virtblk_add_req(vblk->vqs[qid].vq, vbr, vbr->sg, num);
207218
if (err) {
208-
virtqueue_kick(vblk->vq);
219+
virtqueue_kick(vblk->vqs[qid].vq);
209220
blk_mq_stop_hw_queue(hctx);
210-
spin_unlock_irqrestore(&vblk->vq_lock, flags);
221+
spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
211222
/* Out of mem doesn't actually happen, since we fall back
212223
* to direct descriptors */
213224
if (err == -ENOMEM || err == -ENOSPC)
214225
return BLK_MQ_RQ_QUEUE_BUSY;
215226
return BLK_MQ_RQ_QUEUE_ERROR;
216227
}
217228

218-
if (last && virtqueue_kick_prepare(vblk->vq))
229+
if (last && virtqueue_kick_prepare(vblk->vqs[qid].vq))
219230
notify = true;
220-
spin_unlock_irqrestore(&vblk->vq_lock, flags);
231+
spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
221232

222233
if (notify)
223-
virtqueue_notify(vblk->vq);
234+
virtqueue_notify(vblk->vqs[qid].vq);
224235
return BLK_MQ_RQ_QUEUE_OK;
225236
}
226237

@@ -377,12 +388,64 @@ static void virtblk_config_changed(struct virtio_device *vdev)
377388
static int init_vq(struct virtio_blk *vblk)
378389
{
379390
int err = 0;
391+
int i;
392+
vq_callback_t **callbacks;
393+
const char **names;
394+
struct virtqueue **vqs;
395+
unsigned short num_vqs;
396+
struct virtio_device *vdev = vblk->vdev;
397+
398+
err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
399+
struct virtio_blk_config, num_queues,
400+
&num_vqs);
401+
if (err)
402+
num_vqs = 1;
403+
404+
vblk->vqs = kmalloc(sizeof(*vblk->vqs) * num_vqs, GFP_KERNEL);
405+
if (!vblk->vqs) {
406+
err = -ENOMEM;
407+
goto out;
408+
}
409+
410+
names = kmalloc(sizeof(*names) * num_vqs, GFP_KERNEL);
411+
if (!names)
412+
goto err_names;
413+
414+
callbacks = kmalloc(sizeof(*callbacks) * num_vqs, GFP_KERNEL);
415+
if (!callbacks)
416+
goto err_callbacks;
417+
418+
vqs = kmalloc(sizeof(*vqs) * num_vqs, GFP_KERNEL);
419+
if (!vqs)
420+
goto err_vqs;
380421

381-
/* We expect one virtqueue, for output. */
382-
vblk->vq = virtio_find_single_vq(vblk->vdev, virtblk_done, "requests");
383-
if (IS_ERR(vblk->vq))
384-
err = PTR_ERR(vblk->vq);
422+
for (i = 0; i < num_vqs; i++) {
423+
callbacks[i] = virtblk_done;
424+
snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%d", i);
425+
names[i] = vblk->vqs[i].name;
426+
}
427+
428+
/* Discover virtqueues and write information to configuration. */
429+
err = vdev->config->find_vqs(vdev, num_vqs, vqs, callbacks, names);
430+
if (err)
431+
goto err_find_vqs;
385432

433+
for (i = 0; i < num_vqs; i++) {
434+
spin_lock_init(&vblk->vqs[i].lock);
435+
vblk->vqs[i].vq = vqs[i];
436+
}
437+
vblk->num_vqs = num_vqs;
438+
439+
err_find_vqs:
440+
kfree(vqs);
441+
err_vqs:
442+
kfree(callbacks);
443+
err_callbacks:
444+
kfree(names);
445+
err_names:
446+
if (err)
447+
kfree(vblk->vqs);
448+
out:
386449
return err;
387450
}
388451

@@ -551,7 +614,6 @@ static int virtblk_probe(struct virtio_device *vdev)
551614
err = init_vq(vblk);
552615
if (err)
553616
goto out_free_vblk;
554-
spin_lock_init(&vblk->vq_lock);
555617

556618
/* FIXME: How many partitions? How long is a piece of string? */
557619
vblk->disk = alloc_disk(1 << PART_BITS);
@@ -562,22 +624,22 @@ static int virtblk_probe(struct virtio_device *vdev)
562624

563625
/* Default queue sizing is to fill the ring. */
564626
if (!virtblk_queue_depth) {
565-
virtblk_queue_depth = vblk->vq->num_free;
627+
virtblk_queue_depth = vblk->vqs[0].vq->num_free;
566628
/* ... but without indirect descs, we use 2 descs per req */
567629
if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
568630
virtblk_queue_depth /= 2;
569631
}
570632

571633
memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
572634
vblk->tag_set.ops = &virtio_mq_ops;
573-
vblk->tag_set.nr_hw_queues = 1;
574635
vblk->tag_set.queue_depth = virtblk_queue_depth;
575636
vblk->tag_set.numa_node = NUMA_NO_NODE;
576637
vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
577638
vblk->tag_set.cmd_size =
578639
sizeof(struct virtblk_req) +
579640
sizeof(struct scatterlist) * sg_elems;
580641
vblk->tag_set.driver_data = vblk;
642+
vblk->tag_set.nr_hw_queues = vblk->num_vqs;
581643

582644
err = blk_mq_alloc_tag_set(&vblk->tag_set);
583645
if (err)
@@ -727,6 +789,7 @@ static void virtblk_remove(struct virtio_device *vdev)
727789
refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
728790
put_disk(vblk->disk);
729791
vdev->config->del_vqs(vdev);
792+
kfree(vblk->vqs);
730793
kfree(vblk);
731794

732795
/* Only free device id if we don't have any users */
@@ -777,7 +840,8 @@ static const struct virtio_device_id id_table[] = {
777840
static unsigned int features[] = {
778841
VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
779842
VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
780-
VIRTIO_BLK_F_WCE, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE
843+
VIRTIO_BLK_F_WCE, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
844+
VIRTIO_BLK_F_MQ,
781845
};
782846

783847
static struct virtio_driver virtio_blk = {

0 commit comments

Comments
 (0)