virtio-vdpa: Support interrupt affinity spreading mechanism
authorXie Yongji <xieyongji@bytedance.com>
Thu, 23 Mar 2023 05:30:35 +0000 (13:30 +0800)
committerMichael S. Tsirkin <mst@redhat.com>
Fri, 21 Apr 2023 07:02:31 +0000 (03:02 -0400)
To support interrupt affinity spreading mechanism,
this makes use of group_cpus_evenly() to create
an irq callback affinity mask for each virtqueue
of vdpa device. Then we will unify set_vq_affinity
callback to pass the affinity to the vdpa device driver.

Signed-off-by: Xie Yongji <xieyongji@bytedance.com>
Message-Id: <20230323053043.35-4-xieyongji@bytedance.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Jason Wang <jasowang@redhat.com>
drivers/virtio/virtio_vdpa.c

index f72696b..f3826f4 100644 (file)
@@ -13,6 +13,7 @@
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/uuid.h>
+#include <linux/group_cpus.h>
 #include <linux/virtio.h>
 #include <linux/vdpa.h>
 #include <linux/virtio_config.h>
@@ -272,6 +273,66 @@ static void virtio_vdpa_del_vqs(struct virtio_device *vdev)
                virtio_vdpa_del_vq(vq);
 }
 
+static void default_calc_sets(struct irq_affinity *affd, unsigned int affvecs)
+{
+       affd->nr_sets = 1;
+       affd->set_size[0] = affvecs;
+}
+
+static struct cpumask *
+create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)
+{
+       unsigned int affvecs = 0, curvec, usedvecs, i;
+       struct cpumask *masks = NULL;
+
+       if (nvecs > affd->pre_vectors + affd->post_vectors)
+               affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
+
+       if (!affd->calc_sets)
+               affd->calc_sets = default_calc_sets;
+
+       affd->calc_sets(affd, affvecs);
+
+       if (!affvecs)
+               return NULL;
+
+       masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
+       if (!masks)
+               return NULL;
+
+       /* Fill out vectors at the beginning that don't need affinity */
+       for (curvec = 0; curvec < affd->pre_vectors; curvec++)
+               cpumask_setall(&masks[curvec]);
+
+       for (i = 0, usedvecs = 0; i < affd->nr_sets; i++) {
+               unsigned int this_vecs = affd->set_size[i];
+               int j;
+               struct cpumask *result = group_cpus_evenly(this_vecs);
+
+               if (!result) {
+                       kfree(masks);
+                       return NULL;
+               }
+
+               for (j = 0; j < this_vecs; j++)
+                       cpumask_copy(&masks[curvec + j], &result[j]);
+               kfree(result);
+
+               curvec += this_vecs;
+               usedvecs += this_vecs;
+       }
+
+       /* Fill out vectors at the end that don't need affinity */
+       if (usedvecs >= affvecs)
+               curvec = affd->pre_vectors + affvecs;
+       else
+               curvec = affd->pre_vectors + usedvecs;
+       for (; curvec < nvecs; curvec++)
+               cpumask_setall(&masks[curvec]);
+
+       return masks;
+}
+
 static int virtio_vdpa_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
                                struct virtqueue *vqs[],
                                vq_callback_t *callbacks[],
@@ -282,9 +343,15 @@ static int virtio_vdpa_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
        struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev);
        struct vdpa_device *vdpa = vd_get_vdpa(vdev);
        const struct vdpa_config_ops *ops = vdpa->config;
+       struct irq_affinity default_affd = { 0 };
+       struct cpumask *masks;
        struct vdpa_callback cb;
        int i, err, queue_idx = 0;
 
+       masks = create_affinity_masks(nvqs, desc ? desc : &default_affd);
+       if (!masks)
+               return -ENOMEM;
+
        for (i = 0; i < nvqs; ++i) {
                if (!names[i]) {
                        vqs[i] = NULL;
@@ -298,6 +365,7 @@ static int virtio_vdpa_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
                        err = PTR_ERR(vqs[i]);
                        goto err_setup_vq;
                }
+               ops->set_vq_affinity(vdpa, i, &masks[i]);
        }
 
        cb.callback = virtio_vdpa_config_cb;