diff --git a/block/blk-mq-virtio.c b/block/blk-mq-virtio.c index 68d0945c0b08a2..29daf4be0f7a71 100644 --- a/block/blk-mq-virtio.c +++ b/block/blk-mq-virtio.c @@ -1,13 +1,3 @@ -// SPDX-License-Identifier: GPL-2.0 -/* - * Copyright (c) 2016 Christoph Hellwig. - */ -#include -#include -#include -#include -#include "blk-mq.h" - /** * blk_mq_virtio_map_queues - provide a default queue mapping for virtio device * @qmap: CPU to hardware queue map. @@ -16,20 +6,26 @@ * * This function assumes the virtio device @vdev has at least as many available * interrupt vectors as @set has queues. It will then query the vector - * corresponding to each queue for it's affinity mask and built queue mapping - * that maps a queue to the CPUs that have irq affinity for the corresponding - * vector. + * corresponding to each queue for its affinity mask and build a queue mapping + * that maps a queue to the CPUs that have IRQ affinity for the corresponding + * vector. If the number of available vectors is less than the number of queues, + * it dynamically allocates vectors and assigns queues accordingly. */ void blk_mq_virtio_map_queues(struct blk_mq_queue_map *qmap, struct virtio_device *vdev, int first_vec) { const struct cpumask *mask; unsigned int queue, cpu; + unsigned int nr_vecs_needed = qmap->nr_queues; if (!vdev->config->get_vq_affinity) goto fallback; - for (queue = 0; queue < qmap->nr_queues; queue++) { + // Ensure the virtio device has at least nr_vecs_needed interrupt vectors + if (vdev->config->num_vectors < nr_vecs_needed) + nr_vecs_needed = vdev->config->num_vectors; + + for (queue = 0; queue < nr_vecs_needed; queue++) { mask = vdev->config->get_vq_affinity(vdev, first_vec + queue); if (!mask) goto fallback;