virtiofs: enable multiple request queues

Message ID 20221211103857.25805-1-zhangjiachen.jaycee@bytedance.com
State New
Headers
Series virtiofs: enable multiple request queues |

Commit Message

Jiachen Zhang Dec. 11, 2022, 10:38 a.m. UTC
  Support virtio-fs multiple virtqueues and distribute requests across the
multiqueue complex automatically based on the IRQ affinity.

This commit is based on Connor's patch in the virtio-fs mailing-list,
and additionally intergates cpu-to-vq map into struct virtio_fs so that
this virtio-fs multi-queue feature can fit into multiple virtio-fs mounts.

Link: https://www.mail-archive.com/virtio-fs@redhat.com/msg03320.html
Suggested-by: Stefan Hajnoczi <stefanha@redhat.com>
Cc: Connor Kuehl <ckuehl@redhat.com>
Signed-off-by: Jiachen Zhang <zhangjiachen.jaycee@bytedance.com>
---
 fs/fuse/virtio_fs.c | 37 +++++++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 8 deletions(-)
  

Comments

Jiachen Zhang Dec. 11, 2022, 11:01 a.m. UTC | #1
On Sun, Dec 11, 2022 at 6:40 PM Jiachen Zhang
<zhangjiachen.jaycee@bytedance.com> wrote:
>
> Support virtio-fs multiple virtqueues and distribute requests across the
> multiqueue complex automatically based on the IRQ affinity.
>
> This commit is based on Connor's patch in the virtio-fs mailing-list,
> and additionally intergates cpu-to-vq map into struct virtio_fs so that
> this virtio-fs multi-queue feature can fit into multiple virtio-fs mounts.
>
> Link: https://www.mail-archive.com/virtio-fs@redhat.com/msg03320.html
> Suggested-by: Stefan Hajnoczi <stefanha@redhat.com>
> Cc: Connor Kuehl <ckuehl@redhat.com>
> Signed-off-by: Jiachen Zhang <zhangjiachen.jaycee@bytedance.com>
> ---

Hi all,

The corresponding QEMU virtiofsd changes can be found in the
qemu-devel mailing list. The mail link is
https://lore.kernel.org/qemu-devel/20221211104743.27333-1-zhangjiachen.jaycee@bytedance.com/
.

To enable this multi-queue feature with QEMU emulated virtio-fs
devices, you should specify both the qemu-system-x86_64 vhost-user-fs
parameter and the virtiofsd parameter.

For example, to setup 16 virtio-fs request queues, you should apply
the kernel patch in this mail, the QEMU vhost-user-fs device should be
like  '-device vhost-user-fs-pci,chardev=char0,tag=myfs,num-request-queues=16',
and for the virtiofsd you should specify  '-o num_request_queues=16'.

Thanks,
Jiachen

>  fs/fuse/virtio_fs.c | 37 +++++++++++++++++++++++++++++--------
>  1 file changed, 29 insertions(+), 8 deletions(-)
>
> diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c
> index 4d8d4f16c727..410968dede0c 100644
> --- a/fs/fuse/virtio_fs.c
> +++ b/fs/fuse/virtio_fs.c
> @@ -32,8 +32,9 @@ static DEFINE_MUTEX(virtio_fs_mutex);
>  static LIST_HEAD(virtio_fs_instances);
>
>  enum {
> -       VQ_HIPRIO,
> -       VQ_REQUEST
> +       VQ_HIPRIO = 0,
> +       /* TODO add VQ_NOTIFICATION according to the virtio 1.2 spec. */
> +       VQ_REQUEST = 1,
>  };
>
>  #define VQ_NAME_LEN    24
> @@ -59,6 +60,7 @@ struct virtio_fs {
>         struct list_head list;    /* on virtio_fs_instances */
>         char *tag;
>         struct virtio_fs_vq *vqs;
> +       struct virtio_fs_vq * __percpu *vq_proxy;
>         unsigned int nvqs;               /* number of virtqueues */
>         unsigned int num_request_queues; /* number of request queues */
>         struct dax_device *dax_dev;
> @@ -686,6 +688,7 @@ static int virtio_fs_setup_vqs(struct virtio_device *vdev,
>         struct virtqueue **vqs;
>         vq_callback_t **callbacks;
>         const char **names;
> +       struct irq_affinity desc = { .pre_vectors = 1, .nr_sets = 1, };
>         unsigned int i;
>         int ret = 0;
>
> @@ -694,11 +697,16 @@ static int virtio_fs_setup_vqs(struct virtio_device *vdev,
>         if (fs->num_request_queues == 0)
>                 return -EINVAL;
>
> +       fs->num_request_queues = min_t(unsigned int, nr_cpu_ids,
> +                                      fs->num_request_queues);
> +
>         fs->nvqs = VQ_REQUEST + fs->num_request_queues;
>         fs->vqs = kcalloc(fs->nvqs, sizeof(fs->vqs[VQ_HIPRIO]), GFP_KERNEL);
>         if (!fs->vqs)
>                 return -ENOMEM;
>
> +       pr_debug("virtio-fs: number of vqs: %d\n", fs->nvqs);
> +
>         vqs = kmalloc_array(fs->nvqs, sizeof(vqs[VQ_HIPRIO]), GFP_KERNEL);
>         callbacks = kmalloc_array(fs->nvqs, sizeof(callbacks[VQ_HIPRIO]),
>                                         GFP_KERNEL);
> @@ -723,12 +731,26 @@ static int virtio_fs_setup_vqs(struct virtio_device *vdev,
>                 names[i] = fs->vqs[i].name;
>         }
>
> -       ret = virtio_find_vqs(vdev, fs->nvqs, vqs, callbacks, names, NULL);
> +       ret = virtio_find_vqs(vdev, fs->nvqs, vqs, callbacks, names, &desc);
>         if (ret < 0)
>                 goto out;
>
> -       for (i = 0; i < fs->nvqs; i++)
> +       fs->vq_proxy = alloc_percpu(struct virtio_fs_vq *);
> +       for (i = 0; i < fs->nvqs; i++) {
> +               const struct cpumask *mask;
> +               unsigned int cpu;
> +
>                 fs->vqs[i].vq = vqs[i];
> +               if (i == VQ_HIPRIO)
> +                       continue;
> +
> +               mask = vdev->config->get_vq_affinity(vdev, i);
> +               for_each_cpu(cpu, mask) {
> +                       struct virtio_fs_vq **cpu_vq = per_cpu_ptr(fs->vq_proxy, cpu);
> +                       *cpu_vq = &fs->vqs[i];
> +                       pr_debug("virtio-fs: map cpu %d to vq%d\n", cpu, i);
> +               }
> +       }
>
>         virtio_fs_start_all_queues(fs);
>  out:
> @@ -875,8 +897,6 @@ static int virtio_fs_probe(struct virtio_device *vdev)
>         if (ret < 0)
>                 goto out;
>
> -       /* TODO vq affinity */
> -
>         ret = virtio_fs_setup_dax(vdev, fs);
>         if (ret < 0)
>                 goto out_vqs;
> @@ -926,6 +946,7 @@ static void virtio_fs_remove(struct virtio_device *vdev)
>         virtio_fs_stop_all_queues(fs);
>         virtio_fs_drain_all_queues_locked(fs);
>         virtio_reset_device(vdev);
> +       free_percpu(fs->vq_proxy);
>         virtio_fs_cleanup_vqs(vdev);
>
>         vdev->priv = NULL;
> @@ -1223,7 +1244,6 @@ static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
>  static void virtio_fs_wake_pending_and_unlock(struct fuse_iqueue *fiq)
>  __releases(fiq->lock)
>  {
> -       unsigned int queue_id = VQ_REQUEST; /* TODO multiqueue */
>         struct virtio_fs *fs;
>         struct fuse_req *req;
>         struct virtio_fs_vq *fsvq;
> @@ -1243,7 +1263,8 @@ __releases(fiq->lock)
>                  req->in.h.nodeid, req->in.h.len,
>                  fuse_len_args(req->args->out_numargs, req->args->out_args));
>
> -       fsvq = &fs->vqs[queue_id];
> +       fsvq = this_cpu_read(*fs->vq_proxy);
> +
>         ret = virtio_fs_enqueue_req(fsvq, req, false);
>         if (ret < 0) {
>                 if (ret == -ENOMEM || ret == -ENOSPC) {
> --
> 2.20.1
>
  
kernel test robot Dec. 11, 2022, 7:09 p.m. UTC | #2
Hi Jiachen,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on mszeredi-fuse/for-next]
[also build test WARNING on linus/master v6.1-rc8 next-20221208]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Jiachen-Zhang/virtiofs-enable-multiple-request-queues/20221211-184228
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/fuse.git for-next
patch link:    https://lore.kernel.org/r/20221211103857.25805-1-zhangjiachen.jaycee%40bytedance.com
patch subject: [PATCH] virtiofs: enable multiple request queues
config: loongarch-randconfig-s041-20221211
compiler: loongarch64-linux-gcc (GCC) 12.1.0
reproduce:
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # apt-get install sparse
        # sparse version: v0.6.4-39-gce1a6720-dirty
        # https://github.com/intel-lab-lkp/linux/commit/691f021ab737166116f9fc6800904ebfed076f05
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Jiachen-Zhang/virtiofs-enable-multiple-request-queues/20221211-184228
        git checkout 691f021ab737166116f9fc6800904ebfed076f05
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=loongarch SHELL=/bin/bash fs/fuse/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

sparse warnings: (new ones prefixed by >>)
>> fs/fuse/virtio_fs.c:1266:16: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected void *ptr @@     got struct virtio_fs_vq *[noderef] __percpu *vq_proxy @@
   fs/fuse/virtio_fs.c:1266:16: sparse:     expected void *ptr
   fs/fuse/virtio_fs.c:1266:16: sparse:     got struct virtio_fs_vq *[noderef] __percpu *vq_proxy
>> fs/fuse/virtio_fs.c:1266:16: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected void *ptr @@     got struct virtio_fs_vq *[noderef] __percpu *vq_proxy @@
   fs/fuse/virtio_fs.c:1266:16: sparse:     expected void *ptr
   fs/fuse/virtio_fs.c:1266:16: sparse:     got struct virtio_fs_vq *[noderef] __percpu *vq_proxy
>> fs/fuse/virtio_fs.c:1266:16: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected void *ptr @@     got struct virtio_fs_vq *[noderef] __percpu *vq_proxy @@
   fs/fuse/virtio_fs.c:1266:16: sparse:     expected void *ptr
   fs/fuse/virtio_fs.c:1266:16: sparse:     got struct virtio_fs_vq *[noderef] __percpu *vq_proxy
>> fs/fuse/virtio_fs.c:1266:16: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected void *ptr @@     got struct virtio_fs_vq *[noderef] __percpu *vq_proxy @@
   fs/fuse/virtio_fs.c:1266:16: sparse:     expected void *ptr
   fs/fuse/virtio_fs.c:1266:16: sparse:     got struct virtio_fs_vq *[noderef] __percpu *vq_proxy

vim +1266 fs/fuse/virtio_fs.c

  1243	
  1244	static void virtio_fs_wake_pending_and_unlock(struct fuse_iqueue *fiq)
  1245	__releases(fiq->lock)
  1246	{
  1247		struct virtio_fs *fs;
  1248		struct fuse_req *req;
  1249		struct virtio_fs_vq *fsvq;
  1250		int ret;
  1251	
  1252		WARN_ON(list_empty(&fiq->pending));
  1253		req = list_last_entry(&fiq->pending, struct fuse_req, list);
  1254		clear_bit(FR_PENDING, &req->flags);
  1255		list_del_init(&req->list);
  1256		WARN_ON(!list_empty(&fiq->pending));
  1257		spin_unlock(&fiq->lock);
  1258	
  1259		fs = fiq->priv;
  1260	
  1261		pr_debug("%s: opcode %u unique %#llx nodeid %#llx in.len %u out.len %u\n",
  1262			  __func__, req->in.h.opcode, req->in.h.unique,
  1263			 req->in.h.nodeid, req->in.h.len,
  1264			 fuse_len_args(req->args->out_numargs, req->args->out_args));
  1265	
> 1266		fsvq = this_cpu_read(*fs->vq_proxy);
  1267	
  1268		ret = virtio_fs_enqueue_req(fsvq, req, false);
  1269		if (ret < 0) {
  1270			if (ret == -ENOMEM || ret == -ENOSPC) {
  1271				/*
  1272				 * Virtqueue full. Retry submission from worker
  1273				 * context as we might be holding fc->bg_lock.
  1274				 */
  1275				spin_lock(&fsvq->lock);
  1276				list_add_tail(&req->list, &fsvq->queued_reqs);
  1277				inc_in_flight_req(fsvq);
  1278				schedule_delayed_work(&fsvq->dispatch_work,
  1279							msecs_to_jiffies(1));
  1280				spin_unlock(&fsvq->lock);
  1281				return;
  1282			}
  1283			req->out.h.error = ret;
  1284			pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", ret);
  1285	
  1286			/* Can't end request in submission context. Use a worker */
  1287			spin_lock(&fsvq->lock);
  1288			list_add_tail(&req->list, &fsvq->end_reqs);
  1289			schedule_delayed_work(&fsvq->dispatch_work, 0);
  1290			spin_unlock(&fsvq->lock);
  1291			return;
  1292		}
  1293	}
  1294
  

Patch

diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c
index 4d8d4f16c727..410968dede0c 100644
--- a/fs/fuse/virtio_fs.c
+++ b/fs/fuse/virtio_fs.c
@@ -32,8 +32,9 @@  static DEFINE_MUTEX(virtio_fs_mutex);
 static LIST_HEAD(virtio_fs_instances);
 
 enum {
-	VQ_HIPRIO,
-	VQ_REQUEST
+	VQ_HIPRIO = 0,
+	/* TODO add VQ_NOTIFICATION according to the virtio 1.2 spec. */
+	VQ_REQUEST = 1,
 };
 
 #define VQ_NAME_LEN	24
@@ -59,6 +60,7 @@  struct virtio_fs {
 	struct list_head list;    /* on virtio_fs_instances */
 	char *tag;
 	struct virtio_fs_vq *vqs;
+	struct virtio_fs_vq * __percpu *vq_proxy;
 	unsigned int nvqs;               /* number of virtqueues */
 	unsigned int num_request_queues; /* number of request queues */
 	struct dax_device *dax_dev;
@@ -686,6 +688,7 @@  static int virtio_fs_setup_vqs(struct virtio_device *vdev,
 	struct virtqueue **vqs;
 	vq_callback_t **callbacks;
 	const char **names;
+	struct irq_affinity desc = { .pre_vectors = 1, .nr_sets = 1, };
 	unsigned int i;
 	int ret = 0;
 
@@ -694,11 +697,16 @@  static int virtio_fs_setup_vqs(struct virtio_device *vdev,
 	if (fs->num_request_queues == 0)
 		return -EINVAL;
 
+	fs->num_request_queues = min_t(unsigned int, nr_cpu_ids,
+				       fs->num_request_queues);
+
 	fs->nvqs = VQ_REQUEST + fs->num_request_queues;
 	fs->vqs = kcalloc(fs->nvqs, sizeof(fs->vqs[VQ_HIPRIO]), GFP_KERNEL);
 	if (!fs->vqs)
 		return -ENOMEM;
 
+	pr_debug("virtio-fs: number of vqs: %d\n", fs->nvqs);
+
 	vqs = kmalloc_array(fs->nvqs, sizeof(vqs[VQ_HIPRIO]), GFP_KERNEL);
 	callbacks = kmalloc_array(fs->nvqs, sizeof(callbacks[VQ_HIPRIO]),
 					GFP_KERNEL);
@@ -723,12 +731,26 @@  static int virtio_fs_setup_vqs(struct virtio_device *vdev,
 		names[i] = fs->vqs[i].name;
 	}
 
-	ret = virtio_find_vqs(vdev, fs->nvqs, vqs, callbacks, names, NULL);
+	ret = virtio_find_vqs(vdev, fs->nvqs, vqs, callbacks, names, &desc);
 	if (ret < 0)
 		goto out;
 
-	for (i = 0; i < fs->nvqs; i++)
+	fs->vq_proxy = alloc_percpu(struct virtio_fs_vq *);
+	for (i = 0; i < fs->nvqs; i++) {
+		const struct cpumask *mask;
+		unsigned int cpu;
+
 		fs->vqs[i].vq = vqs[i];
+		if (i == VQ_HIPRIO)
+			continue;
+
+		mask = vdev->config->get_vq_affinity(vdev, i);
+		for_each_cpu(cpu, mask) {
+			struct virtio_fs_vq **cpu_vq = per_cpu_ptr(fs->vq_proxy, cpu);
+			*cpu_vq = &fs->vqs[i];
+			pr_debug("virtio-fs: map cpu %d to vq%d\n", cpu, i);
+		}
+	}
 
 	virtio_fs_start_all_queues(fs);
 out:
@@ -875,8 +897,6 @@  static int virtio_fs_probe(struct virtio_device *vdev)
 	if (ret < 0)
 		goto out;
 
-	/* TODO vq affinity */
-
 	ret = virtio_fs_setup_dax(vdev, fs);
 	if (ret < 0)
 		goto out_vqs;
@@ -926,6 +946,7 @@  static void virtio_fs_remove(struct virtio_device *vdev)
 	virtio_fs_stop_all_queues(fs);
 	virtio_fs_drain_all_queues_locked(fs);
 	virtio_reset_device(vdev);
+	free_percpu(fs->vq_proxy);
 	virtio_fs_cleanup_vqs(vdev);
 
 	vdev->priv = NULL;
@@ -1223,7 +1244,6 @@  static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
 static void virtio_fs_wake_pending_and_unlock(struct fuse_iqueue *fiq)
 __releases(fiq->lock)
 {
-	unsigned int queue_id = VQ_REQUEST; /* TODO multiqueue */
 	struct virtio_fs *fs;
 	struct fuse_req *req;
 	struct virtio_fs_vq *fsvq;
@@ -1243,7 +1263,8 @@  __releases(fiq->lock)
 		 req->in.h.nodeid, req->in.h.len,
 		 fuse_len_args(req->args->out_numargs, req->args->out_args));
 
-	fsvq = &fs->vqs[queue_id];
+	fsvq = this_cpu_read(*fs->vq_proxy);
+
 	ret = virtio_fs_enqueue_req(fsvq, req, false);
 	if (ret < 0) {
 		if (ret == -ENOMEM || ret == -ENOSPC) {