Skip to contentSkip to conversation

vduse: Reconnection support in vduse

PATCH In review Cindy Lu 2 participants 14 messages Oct 11, 2023 – Nov 7, 2023 virtio core

lore
Share & tools
lore

Cindy Lu

author
#0 lore raw

cover vduse: Reconnection support in vduse

Here is the reconnect support in vduse,

The kernel will allocate pages for reconnection userspace needs to use ioctl VDUSE_GET_RECONNECT_INFO to get the mmap related infomation and then map these pages to userspace.
The kernel and userspace will use these pages to sync the reconnect information

Tested in vduse + dpdk test-pmd

Cindy Lu (4):
vduse: Add function to get/free the pages for reconnection
vduse: Add file operation for mmap
vduse: Add new ioctl VDUSE_GET_RECONNECT_INFO
vduse: update the vq_info in ioctl

 drivers/vdpa/vdpa_user/vduse_dev.c | 175 +++++++++++++++++++++++++++++
 include/uapi/linux/vduse.h         |  43 +++++++
 2 files changed, 218 insertions(+)
Signature · 1 line
2.34.3
author
#1 lore raw

PATCH 1/4 vduse: Add function to get/free the pages for reconnection

Add the function vduse_alloc_reconnnect_info_mem
and vduse_alloc_reconnnect_info_mem
In this 2 function, vduse will get/free (vq_num + 1)*page Page 0 will be used to save the reconnection information, The Userspace App will maintain this. Page 1 ~ vq_num + 1 will save the reconnection information for vqs.

---
 drivers/vdpa/vdpa_user/vduse_dev.c | 78 ++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
drivers/vdpa/vdpa_user/vduse_dev.c +78 -0
index 26b7e29cb900..565126a9ab01 100644
@@ -30,6 +30,10 @@
 #include <uapi/linux/virtio_blk.h>
 #include <linux/mod_devicetable.h>
 
+#ifdef CONFIG_X86
+#include <asm/set_memory.h>
+#endif
+
 #include "iova_domain.h"
 
 #define DRV_AUTHOR   "Yongji Xie <[email protected]>"
@@ -41,6 +45,19 @@
 #define VDUSE_IOVA_SIZE (128 * 1024 * 1024)
 #define VDUSE_MSG_DEFAULT_TIMEOUT 30
 
+/* struct vdpa_reconnect_info saved the alloc pages info
+ * these pages will mmaped to userspace for reconnection
+ */
+struct vdpa_reconnect_info {
+	/* Offset (within vm_file) in PAGE_SIZE
+	 */
+	u32 index;
+	/* virtual address for this page*/
+	unsigned long vaddr;
+	/* allocated memory size, */
+	phys_addr_t size;
+};
+
 struct vduse_virtqueue {
 	u16 index;
 	u16 num_max;
@@ -57,6 +74,7 @@ struct vduse_virtqueue {
 	struct vdpa_callback cb;
 	struct work_struct inject;
 	struct work_struct kick;
+	struct vdpa_reconnect_info reconnect_info;
 };
 
 struct vduse_dev;
@@ -106,6 +124,7 @@ struct vduse_dev {
 	u32 vq_align;
 	struct vduse_umem *umem;
 	struct mutex mem_lock;
+	struct vdpa_reconnect_info reconnect_status;
 };
 
 struct vduse_dev_msg {
@@ -1030,6 +1049,61 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev,
 	return ret;
 }
 
+static int vduse_alloc_reconnnect_info_mem(struct vduse_dev *dev)
+{
+	struct vdpa_reconnect_info *info;
+	struct vduse_virtqueue *vq;
+	void *addr;
+
+	/*page 0 is use to save status,dpdk will use this to save the information
+	 *needed in reconnection,kernel don't need to maintain this
+	 */
+	info = &dev->reconnect_status;
+	addr = (void *)get_zeroed_page(GFP_KERNEL);
+	if (!addr)
+		return -1;
+
+	info->vaddr = (unsigned long)(addr);
+	info->size = PAGE_SIZE;
+	/* index is vm Offset in PAGE_SIZE */
+	info->index = 0;
+
+	/*page 1~ vq_num + 1 save the reconnect info for vqs*/
+	for (int i = 0; i < dev->vq_num + 1; i++) {
+		vq = &dev->vqs[i];
+		info = &vq->reconnect_info;
+		addr = (void *)get_zeroed_page(GFP_KERNEL);
+		if (!addr)
+			return -1;
+
+		info->vaddr = (unsigned long)(addr);
+		info->size = PAGE_SIZE;
+		info->index = i + 1;
+	}
+
+	return 0;
+}
+
+static int vduse_free_reconnnect_info_mem(struct vduse_dev *dev)
+{
+	struct vdpa_reconnect_info *info;
+	struct vduse_virtqueue *vq;
+
+	info = &dev->reconnect_status;
+	free_page(info->vaddr);
+	info->size = 0;
+	info->vaddr = 0;
+	for (int i = 0; i < dev->vq_num + 1; i++) {
+		vq = &dev->vqs[i];
+		info = &vq->reconnect_info;
+		free_page(info->vaddr);
+		info->size = 0;
+		info->vaddr = 0;
+	}
+
+	return 0;
+}
+
 static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
 			    unsigned long arg)
 {
@@ -1390,6 +1464,8 @@ static int vduse_destroy_dev(char *name)
 		mutex_unlock(&dev->lock);
 		return -EBUSY;
 	}
+	vduse_free_reconnnect_info_mem(dev);
+
 	dev->connected = true;
 	mutex_unlock(&dev->lock);
 
@@ -1542,6 +1618,8 @@ static int vduse_create_dev(struct vduse_dev_config *config,
 		ret = PTR_ERR(dev->dev);
 		goto err_dev;
 	}
+
+	vduse_alloc_reconnnect_info_mem(dev);
 	__module_get(THIS_MODULE);
 
 	return 0;
Signature · 1 line
2.34.3
#2 lore raw

Re: [PATCH v1 1/4] vduse: Add function to get/free the pages for reconnection

Quoted 7 lines · Cindy Lu

Add the function vduse_alloc_reconnnect_info_mem
and vduse_alloc_reconnnect_info_mem
In this 2 function, vduse will get/free (vq_num + 1)*page Page 0 will be used to save the reconnection information, The Userspace App will maintain this. Page 1 ~ vq_num + 1 will save the reconnection information for vqs.

I'd align this with the spec. E.g spec starts virtqueue index from 0.

Quoted 11 lines
---
 drivers/vdpa/vdpa_user/vduse_dev.c | 78 ++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
drivers/vdpa/vdpa_user/vduse_dev.c +0 -0
index 26b7e29cb900..565126a9ab01 100644

drivers/vdpa/vdpa_user/vduse_dev.c line 35

Quoted 6 lines
drivers/vdpa/vdpa_user/vduse_dev.c +3 -0
@@ -30,6 +30,10 @@
 #include <uapi/linux/virtio_blk.h>
 #include <linux/mod_devicetable.h>
 
+#ifdef CONFIG_X86
+#include <asm/set_memory.h>
+#endif

Why do we need special care for x86?

Quoted 9 lines
drivers/vdpa/vdpa_user/vduse_dev.c +2 -0
+
 #include "iova_domain.h"
 
 #define DRV_AUTHOR   "Yongji Xie <[email protected]>"
@@ -41,6 +45,19 @@
 #define VDUSE_IOVA_SIZE (128 * 1024 * 1024)
 #define VDUSE_MSG_DEFAULT_TIMEOUT 30
 
+/* struct vdpa_reconnect_info saved the alloc pages info

drivers/vdpa/vdpa_user/vduse_dev.c line 58

Quoted 10 lines
drivers/vdpa/vdpa_user/vduse_dev.c +10 -0
+ * these pages will mmaped to userspace for reconnection
+ */
+struct vdpa_reconnect_info {
+       /* Offset (within vm_file) in PAGE_SIZE
+        */
+       u32 index;
+       /* virtual address for this page*/
+       unsigned long vaddr;
+       /* allocated memory size, */
+       phys_addr_t size;

Anything wrong with using "unsigned long" here.

Btw, what is "allocated memory size" since I think we can use up to PAGE_SIZE here?

Quoted 24 lines
drivers/vdpa/vdpa_user/vduse_dev.c +4 -0
+};
+
 struct vduse_virtqueue {
        u16 index;
        u16 num_max;
@@ -57,6 +74,7 @@ struct vduse_virtqueue {
        struct vdpa_callback cb;
        struct work_struct inject;
        struct work_struct kick;
+       struct vdpa_reconnect_info reconnect_info;
 };
 
 struct vduse_dev;
@@ -106,6 +124,7 @@ struct vduse_dev {
        u32 vq_align;
        struct vduse_umem *umem;
        struct mutex mem_lock;
+       struct vdpa_reconnect_info reconnect_status;
 };
 
 struct vduse_dev_msg {
@@ -1030,6 +1049,61 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev,
        return ret;
 }

drivers/vdpa/vdpa_user/vduse_dev.c static int vduse_dev_reg_umem(struct vduse_dev *dev, · line 1060

Quoted 10 lines
drivers/vdpa/vdpa_user/vduse_dev.c +9 -0
 
+static int vduse_alloc_reconnnect_info_mem(struct vduse_dev *dev)
+{
+       struct vdpa_reconnect_info *info;
+       struct vduse_virtqueue *vq;
+       void *addr;
+
+       /*page 0 is use to save status,dpdk will use this to save the information
+        *needed in reconnection,kernel don't need to maintain this
+        */

Let's avoid mentioning any specific userspace like DPDK in the kernel source. A good uAPI should not be designed for a specific userspace.

drivers/vdpa/vdpa_user/vduse_dev.c static int vduse_dev_reg_umem(struct vduse_dev *dev, · line 1064

Quoted 4 lines
drivers/vdpa/vdpa_user/vduse_dev.c +4 -0
+       info = &dev->reconnect_status;
+       addr = (void *)get_zeroed_page(GFP_KERNEL);
+       if (!addr)
+               return -1;

Let's don't use magic number here but something like -ENOMEM.

drivers/vdpa/vdpa_user/vduse_dev.c static int vduse_dev_reg_umem(struct vduse_dev *dev, · line 1071

Quoted 7 lines
drivers/vdpa/vdpa_user/vduse_dev.c +7 -0
+
+       info->vaddr = (unsigned long)(addr);
+       info->size = PAGE_SIZE;
+       /* index is vm Offset in PAGE_SIZE */
+       info->index = 0;
+
+       /*page 1~ vq_num + 1 save the reconnect info for vqs*/

There's no need to explain what the code does.

drivers/vdpa/vdpa_user/vduse_dev.c static int vduse_dev_reg_umem(struct vduse_dev *dev, · line 1081

Quoted 10 lines
drivers/vdpa/vdpa_user/vduse_dev.c +10 -0
+       for (int i = 0; i < dev->vq_num + 1; i++) {
+               vq = &dev->vqs[i];
+               info = &vq->reconnect_info;
+               addr = (void *)get_zeroed_page(GFP_KERNEL);
+               if (!addr)
+                       return -1;
+
+               info->vaddr = (unsigned long)(addr);
+               info->size = PAGE_SIZE;
+               info->index = i + 1;

Btw, I don't see an obvious difference in the page for virtqueue and device. Could we unify the logic there? So did the free.

Quoted 38 lines
drivers/vdpa/vdpa_user/vduse_dev.c +27 -0
+       }
+
+       return 0;
+}
+
+static int vduse_free_reconnnect_info_mem(struct vduse_dev *dev)
+{
+       struct vdpa_reconnect_info *info;
+       struct vduse_virtqueue *vq;
+
+       info = &dev->reconnect_status;
+       free_page(info->vaddr);
+       info->size = 0;
+       info->vaddr = 0;
+       for (int i = 0; i < dev->vq_num + 1; i++) {
+               vq = &dev->vqs[i];
+               info = &vq->reconnect_info;
+               free_page(info->vaddr);
+               info->size = 0;
+               info->vaddr = 0;
+       }
+
+       return 0;
+}
+
 static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
                            unsigned long arg)
 {
@@ -1390,6 +1464,8 @@ static int vduse_destroy_dev(char *name)
                mutex_unlock(&dev->lock);
                return -EBUSY;
        }
+       vduse_free_reconnnect_info_mem(dev);
+
        dev->connected = true;
        mutex_unlock(&dev->lock);
 

drivers/vdpa/vdpa_user/vduse_dev.c static int vduse_create_dev(struct vduse_dev_config *config, · line 1622

Quoted 5 lines
drivers/vdpa/vdpa_user/vduse_dev.c +2 -0
@@ -1542,6 +1618,8 @@ static int vduse_create_dev(struct vduse_dev_config *config,
                ret = PTR_ERR(dev->dev);
                goto err_dev;
        }
+
+       vduse_alloc_reconnnect_info_mem(dev);

The errors were ignored here.

Thanks

Quoted 6 lines
drivers/vdpa/vdpa_user/vduse_dev.c +0 -1
        __module_get(THIS_MODULE);
 
        return 0;
--

2.34.3

author
#3 lore raw

Re: [PATCH v1 1/4] vduse: Add function to get/free the pages for reconnection

Quoted 54 lines · Jason Wang
Quoted 7 lines · Cindy Lu

Add the function vduse_alloc_reconnnect_info_mem
and vduse_alloc_reconnnect_info_mem
In this 2 function, vduse will get/free (vq_num + 1)*page Page 0 will be used to save the reconnection information, The Userspace App will maintain this. Page 1 ~ vq_num + 1 will save the reconnection information for vqs.

I'd align this with the spec. E.g spec starts virtqueue index from 0.

Quoted 11 lines
---
 drivers/vdpa/vdpa_user/vduse_dev.c | 78 ++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
drivers/vdpa/vdpa_user/vduse_dev.c +0 -0
index 26b7e29cb900..565126a9ab01 100644

drivers/vdpa/vdpa_user/vduse_dev.c line 35

Quoted 6 lines
drivers/vdpa/vdpa_user/vduse_dev.c +3 -0
@@ -30,6 +30,10 @@
 #include <uapi/linux/virtio_blk.h>
 #include <linux/mod_devicetable.h>
 
+#ifdef CONFIG_X86
+#include <asm/set_memory.h>
+#endif

Why do we need special care for x86?

Quoted 9 lines
drivers/vdpa/vdpa_user/vduse_dev.c +2 -0
+
 #include "iova_domain.h"
 
 #define DRV_AUTHOR   "Yongji Xie <[email protected]>"
@@ -41,6 +45,19 @@
 #define VDUSE_IOVA_SIZE (128 * 1024 * 1024)
 #define VDUSE_MSG_DEFAULT_TIMEOUT 30
 
+/* struct vdpa_reconnect_info saved the alloc pages info

drivers/vdpa/vdpa_user/vduse_dev.c line 58

Quoted 10 lines
drivers/vdpa/vdpa_user/vduse_dev.c +10 -0
+ * these pages will mmaped to userspace for reconnection
+ */
+struct vdpa_reconnect_info {
+       /* Offset (within vm_file) in PAGE_SIZE
+        */
+       u32 index;
+       /* virtual address for this page*/
+       unsigned long vaddr;
+       /* allocated memory size, */
+       phys_addr_t size;

Anything wrong with using "unsigned long" here.

Quoted 3 lines

Btw, what is "allocated memory size" since I think we can use up to PAGE_SIZE here?

sure, will remove this here
Thanks
Cindy

Quoted 25 lines
Quoted 25 lines
drivers/vdpa/vdpa_user/vduse_dev.c +4 -0
+};
+
 struct vduse_virtqueue {
        u16 index;
        u16 num_max;
@@ -57,6 +74,7 @@ struct vduse_virtqueue {
        struct vdpa_callback cb;
        struct work_struct inject;
        struct work_struct kick;
+       struct vdpa_reconnect_info reconnect_info;
 };
 
 struct vduse_dev;
@@ -106,6 +124,7 @@ struct vduse_dev {
        u32 vq_align;
        struct vduse_umem *umem;
        struct mutex mem_lock;
+       struct vdpa_reconnect_info reconnect_status;
 };
 
 struct vduse_dev_msg {
@@ -1030,6 +1049,61 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev,
        return ret;
 }
 
Quoted 13 lines

drivers/vdpa/vdpa_user/vduse_dev.c static int vduse_dev_reg_umem(struct vduse_dev *dev, · line 1060

Quoted 9 lines
drivers/vdpa/vdpa_user/vduse_dev.c +9 -0
+static int vduse_alloc_reconnnect_info_mem(struct vduse_dev *dev)
+{
+       struct vdpa_reconnect_info *info;
+       struct vduse_virtqueue *vq;
+       void *addr;
+
+       /*page 0 is use to save status,dpdk will use this to save the information
+        *needed in reconnection,kernel don't need to maintain this
+        */

Let's avoid mentioning any specific userspace like DPDK in the kernel source. A good uAPI should not be designed for a specific userspace.

will change this

Quoted 7 lines

drivers/vdpa/vdpa_user/vduse_dev.c static int vduse_dev_reg_umem(struct vduse_dev *dev, · line 1064

Quoted 4 lines
drivers/vdpa/vdpa_user/vduse_dev.c +4 -0
+       info = &dev->reconnect_status;
+       addr = (void *)get_zeroed_page(GFP_KERNEL);
+       if (!addr)
+               return -1;

Let's don't use magic number here but something like -ENOMEM.

Quoted 10 lines

drivers/vdpa/vdpa_user/vduse_dev.c static int vduse_dev_reg_umem(struct vduse_dev *dev, · line 1071

Quoted 7 lines
drivers/vdpa/vdpa_user/vduse_dev.c +7 -0
+
+       info->vaddr = (unsigned long)(addr);
+       info->size = PAGE_SIZE;
+       /* index is vm Offset in PAGE_SIZE */
+       info->index = 0;
+
+       /*page 1~ vq_num + 1 save the reconnect info for vqs*/

There's no need to explain what the code does.

will change this

Quoted 60 lines

drivers/vdpa/vdpa_user/vduse_dev.c static int vduse_dev_reg_umem(struct vduse_dev *dev, · line 1081

Quoted 10 lines
drivers/vdpa/vdpa_user/vduse_dev.c +10 -0
+       for (int i = 0; i < dev->vq_num + 1; i++) {
+               vq = &dev->vqs[i];
+               info = &vq->reconnect_info;
+               addr = (void *)get_zeroed_page(GFP_KERNEL);
+               if (!addr)
+                       return -1;
+
+               info->vaddr = (unsigned long)(addr);
+               info->size = PAGE_SIZE;
+               info->index = i + 1;

Btw, I don't see an obvious difference in the page for virtqueue and device. Could we unify the logic there? So did the free.

Quoted 38 lines
drivers/vdpa/vdpa_user/vduse_dev.c +27 -0
+       }
+
+       return 0;
+}
+
+static int vduse_free_reconnnect_info_mem(struct vduse_dev *dev)
+{
+       struct vdpa_reconnect_info *info;
+       struct vduse_virtqueue *vq;
+
+       info = &dev->reconnect_status;
+       free_page(info->vaddr);
+       info->size = 0;
+       info->vaddr = 0;
+       for (int i = 0; i < dev->vq_num + 1; i++) {
+               vq = &dev->vqs[i];
+               info = &vq->reconnect_info;
+               free_page(info->vaddr);
+               info->size = 0;
+               info->vaddr = 0;
+       }
+
+       return 0;
+}
+
 static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
                            unsigned long arg)
 {
@@ -1390,6 +1464,8 @@ static int vduse_destroy_dev(char *name)
                mutex_unlock(&dev->lock);
                return -EBUSY;
        }
+       vduse_free_reconnnect_info_mem(dev);
+
        dev->connected = true;
        mutex_unlock(&dev->lock);
 

drivers/vdpa/vdpa_user/vduse_dev.c static int vduse_create_dev(struct vduse_dev_config *config, · line 1622

Quoted 5 lines
drivers/vdpa/vdpa_user/vduse_dev.c +2 -0
@@ -1542,6 +1618,8 @@ static int vduse_create_dev(struct vduse_dev_config *config,
                ret = PTR_ERR(dev->dev);
                goto err_dev;
        }
+
+       vduse_alloc_reconnnect_info_mem(dev);

The errors were ignored here.

Quoted 2 lines

Thanks

sure will add the check here

Thanks
cindy

Quoted 7 lines
Quoted 6 lines
drivers/vdpa/vdpa_user/vduse_dev.c +0 -1
        __module_get(THIS_MODULE);
 
        return 0;
--

2.34.3

author
#4 lore raw

PATCH 2/4 vduse: Add file operation for mmap

In reply to Cindy Lu

Add the operation for mmap, The user space APP will use this function to map the pages to userspace

---
 drivers/vdpa/vdpa_user/vduse_dev.c | 66 ++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)
drivers/vdpa/vdpa_user/vduse_dev.c +66 -0
index 565126a9ab01..05e72d752fb6 100644
@@ -1380,6 +1380,70 @@ static struct vduse_dev *vduse_dev_get_from_minor(int minor)
 	return dev;
 }
 
+static vm_fault_t vduse_vm_fault(struct vm_fault *vmf)
+{
+	struct vduse_dev *dev = vmf->vma->vm_file->private_data;
+	struct vm_area_struct *vma = vmf->vma;
+	u16 index = vma->vm_pgoff;
+	struct vduse_virtqueue *vq;
+	struct vdpa_reconnect_info *info;
+
+	/* index 0  page reserved for vduse status*/
+	if (index == 0) {
+		info = &dev->reconnect_status;
+	} else {
+		/* index 1+vq_number page reserved for vduse vqs*/
+		vq = &dev->vqs[index - 1];
+		info = &vq->reconnect_info;
+	}
+	if (remap_pfn_range(vma, vmf->address & PAGE_MASK,
+			    PFN_DOWN(virt_to_phys((void *)info->vaddr)),
+			    PAGE_SIZE, vma->vm_page_prot))
+		return VM_FAULT_SIGBUS;
+	return VM_FAULT_NOPAGE;
+}
+
+static const struct vm_operations_struct vduse_vm_ops = {
+	.fault = vduse_vm_fault,
+};
+
+static int vduse_dev_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct vduse_dev *dev = file->private_data;
+	struct vdpa_reconnect_info *info;
+	unsigned long index = vma->vm_pgoff;
+	struct vduse_virtqueue *vq;
+
+	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
+		return -EINVAL;
+	if ((vma->vm_flags & VM_SHARED) == 0)
+		return -EINVAL;
+
+	if (index > dev->vq_num + 1)
+		return -EINVAL;
+
+	/* index 0  page reserved for vduse status*/
+	if (index == 0) {
+		info = &dev->reconnect_status;
+	} else {
+		/* index 1+vq_number page reserved for vduse vqs*/
+		vq = &dev->vqs[index - 1];
+		info = &vq->reconnect_info;
+	}
+	if (info->vaddr == 0)
+		return -ENOTSUPP;
+
+	if (virt_to_phys((void *)info->vaddr) & (PAGE_SIZE - 1))
+		return -EINVAL;
+	if (vma->vm_end - vma->vm_start != info->size)
+		return -EOPNOTSUPP;
+
+	vm_flags_set(vma, VM_IO | VM_DONTDUMP);
+	vma->vm_ops = &vduse_vm_ops;
+
+	return 0;
+}
+
 static int vduse_dev_open(struct inode *inode, struct file *file)
 {
 	int ret;
@@ -1412,6 +1476,8 @@ static const struct file_operations vduse_dev_fops = {
 	.unlocked_ioctl	= vduse_dev_ioctl,
 	.compat_ioctl	= compat_ptr_ioctl,
 	.llseek		= noop_llseek,
+	.mmap		= vduse_dev_mmap,
+
 };
 
 static struct vduse_dev *vduse_dev_create(void)
Signature · 1 line
2.34.3
#5 lore raw

Re: [PATCH v1 2/4] vduse: Add file operation for mmap

Quoted 59 lines · Cindy Lu

Add the operation for mmap, The user space APP will use this function to map the pages to userspace

---
 drivers/vdpa/vdpa_user/vduse_dev.c | 66 ++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)
drivers/vdpa/vdpa_user/vduse_dev.c +42 -0
index 565126a9ab01..05e72d752fb6 100644
@@ -1380,6 +1380,70 @@ static struct vduse_dev *vduse_dev_get_from_minor(int minor)
        return dev;
 }
 
+static vm_fault_t vduse_vm_fault(struct vm_fault *vmf)
+{
+       struct vduse_dev *dev = vmf->vma->vm_file->private_data;
+       struct vm_area_struct *vma = vmf->vma;
+       u16 index = vma->vm_pgoff;
+       struct vduse_virtqueue *vq;
+       struct vdpa_reconnect_info *info;
+
+       /* index 0  page reserved for vduse status*/
+       if (index == 0) {
+               info = &dev->reconnect_status;
+       } else {
+               /* index 1+vq_number page reserved for vduse vqs*/
+               vq = &dev->vqs[index - 1];
+               info = &vq->reconnect_info;
+       }
+       if (remap_pfn_range(vma, vmf->address & PAGE_MASK,
+                           PFN_DOWN(virt_to_phys((void *)info->vaddr)),
+                           PAGE_SIZE, vma->vm_page_prot))
+               return VM_FAULT_SIGBUS;
+       return VM_FAULT_NOPAGE;
+}
+
+static const struct vm_operations_struct vduse_vm_ops = {
+       .fault = vduse_vm_fault,
+};
+
+static int vduse_dev_mmap(struct file *file, struct vm_area_struct *vma)
+{
+       struct vduse_dev *dev = file->private_data;
+       struct vdpa_reconnect_info *info;
+       unsigned long index = vma->vm_pgoff;
+       struct vduse_virtqueue *vq;
+
+       if (vma->vm_end - vma->vm_start != PAGE_SIZE)
+               return -EINVAL;
+       if ((vma->vm_flags & VM_SHARED) == 0)
+               return -EINVAL;
+
+       if (index > dev->vq_num + 1)
+               return -EINVAL;
+

drivers/vdpa/vdpa_user/vduse_dev.c static struct vduse_dev *vduse_dev_get_from_minor(int minor) · line 1434

Quoted 10 lines
drivers/vdpa/vdpa_user/vduse_dev.c +10 -0
+       /* index 0  page reserved for vduse status*/
+       if (index == 0) {
+               info = &dev->reconnect_status;
+       } else {
+               /* index 1+vq_number page reserved for vduse vqs*/
+               vq = &dev->vqs[index - 1];
+               info = &vq->reconnect_info;
+       }
+       if (info->vaddr == 0)
+               return -ENOTSUPP;

Under which condition could we meet this?

drivers/vdpa/vdpa_user/vduse_dev.c static struct vduse_dev *vduse_dev_get_from_minor(int minor) · line 1437

Quoted 3 lines
drivers/vdpa/vdpa_user/vduse_dev.c +3 -0
+
+       if (virt_to_phys((void *)info->vaddr) & (PAGE_SIZE - 1))
+               return -EINVAL;

And this?

drivers/vdpa/vdpa_user/vduse_dev.c static struct vduse_dev *vduse_dev_get_from_minor(int minor) · line 1441

Quoted 4 lines
drivers/vdpa/vdpa_user/vduse_dev.c +4 -0
+       if (vma->vm_end - vma->vm_start != info->size)
+               return -EOPNOTSUPP;
+
+       vm_flags_set(vma, VM_IO | VM_DONTDUMP);

I still don't understand the flags here. Or at least it requires some comment to explain why they are needed here.

Please refer to the kernel sources which explains their use cases.

Thanks

Quoted 20 lines
drivers/vdpa/vdpa_user/vduse_dev.c +7 -1
+       vma->vm_ops = &vduse_vm_ops;
+
+       return 0;
+}
+
 static int vduse_dev_open(struct inode *inode, struct file *file)
 {
        int ret;
@@ -1412,6 +1476,8 @@ static const struct file_operations vduse_dev_fops = {
        .unlocked_ioctl = vduse_dev_ioctl,
        .compat_ioctl   = compat_ptr_ioctl,
        .llseek         = noop_llseek,
+       .mmap           = vduse_dev_mmap,
+
 };
 
 static struct vduse_dev *vduse_dev_create(void)
--

2.34.3

author
#6 lore raw

PATCH 3/4 vduse: Add new ioctl VDUSE_GET_RECONNECT_INFO

In reply to Cindy Lu

In VDUSE_GET_RECONNECT_INFO, the Userspace App can get the map size and The number of mapping memory pages from the kernel. The userspace App can use this information to map the pages.

Add struct vhost_reconnect_data/vhost_reconnect_vring for sync the information in reconnection

---
 drivers/vdpa/vdpa_user/vduse_dev.c | 15 +++++++++++
 include/uapi/linux/vduse.h         | 43 ++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)
drivers/vdpa/vdpa_user/vduse_dev.c +15 -0
index 05e72d752fb6..0f15e7ac716b 100644
@@ -1347,6 +1347,21 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
 		ret = 0;
 		break;
 	}
+	case VDUSE_GET_RECONNECT_INFO: {
+		struct vduse_reconnect_mmap_info info;
+
+		ret = -EFAULT;
+		if (copy_from_user(&info, argp, sizeof(info)))
+			break;
+
+		info.size = PAGE_SIZE;
+		info.max_index = dev->vq_num + 1;
+
+		if (copy_to_user(argp, &info, sizeof(info)))
+			break;
+		ret = 0;
+		break;
+	}
 	default:
 		ret = -ENOIOCTLCMD;
 		break;
include/uapi/linux/vduse.h +43 -0

47 lines not on this page · raw message

Signature · 1 line
2.34.3
#7 lore raw

Re: [PATCH v1 3/4] vduse: Add new ioctl VDUSE_GET_RECONNECT_INFO

Quoted 2 lines · Cindy Lu

In VDUSE_GET_RECONNECT_INFO, the Userspace App can get the map size

I'm not sure why this is needed, the structure that mmaped to userspace should belong to uAPI then userspace can do versions there?

Quoted 1 line

and The number of mapping memory pages from the kernel.

Userspace knows how many vq at most, why is this still needed?

Quoted 43 lines

The userspace
App can use this information to map the pages.

Add struct vhost_reconnect_data/vhost_reconnect_vring for sync the information in reconnection

---
 drivers/vdpa/vdpa_user/vduse_dev.c | 15 +++++++++++
 include/uapi/linux/vduse.h         | 43 ++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)
drivers/vdpa/vdpa_user/vduse_dev.c +15 -0
index 05e72d752fb6..0f15e7ac716b 100644
@@ -1347,6 +1347,21 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
                ret = 0;
                break;
        }
+       case VDUSE_GET_RECONNECT_INFO: {
+               struct vduse_reconnect_mmap_info info;
+
+               ret = -EFAULT;
+               if (copy_from_user(&info, argp, sizeof(info)))
+                       break;
+
+               info.size = PAGE_SIZE;
+               info.max_index = dev->vq_num + 1;
+
+               if (copy_to_user(argp, &info, sizeof(info)))
+                       break;
+               ret = 0;
+               break;
+       }
        default:
                ret = -ENOIOCTLCMD;
                break;
include/uapi/linux/vduse.h +0 -0
index 11bd48c72c6c..5ccac535fba6 100644

include/uapi/linux/vduse.h struct vduse_dev_response { · line 356

Quoted 7 lines
include/uapi/linux/vduse.h +4 -0
@@ -350,4 +350,47 @@ struct vduse_dev_response {
        };
 };
 
+/**
+ * struct vhost_reconnect_data - saved the reconnect info for device
+ * @reconnect_time: reconnect time for this device. userspace APP needs to do ++
+ *                  while reconnecting

This commnet needs tweaking as I don't think "++" is good English. And you need to explain why we need this.

include/uapi/linux/vduse.h struct vduse_dev_response { · line 357

Quoted 1 line
include/uapi/linux/vduse.h +1 -0
+ * @version; version for userspace APP

It's the version of uAPI not the userspace APP. And in order to be able for a correct bootstrap, this needs to be the first field instead of the second.

include/uapi/linux/vduse.h struct vduse_dev_response { · line 359

Quoted 2 lines
include/uapi/linux/vduse.h +2 -0
+ * @features; Device features negotiated in the last reconnect.
+ * @status; Device status in last reconnect

I wonder if we need just more than this, for example the number of active queues, or this is what does @nr_vrings mean?

Quoted 10 lines
include/uapi/linux/vduse.h +10 -0
+ * @nr_vrings; number of vqs
+ */
+
+struct vhost_reconnect_data {
+       __u32 reconnect_time;
+       __u32 version;
+       __u64 features;
+       __u8 status;
+       __u32 nr_vrings;
+};

include/uapi/linux/vduse.h struct vduse_dev_response { · line 379

Quoted 10 lines
include/uapi/linux/vduse.h +10 -0
+
+/**
+ * struct vhost_reconnect_vring -saved the reconnect info for vqs
+ * @last_avail_idx: device available index
+ * @avail_wrap_counter: Driver ring wrap counter
+ */
+struct vhost_reconnect_vring {
+       __u16 last_avail_idx;
+       __u16 avail_wrap_counter;
+};

Do we need the last_used_idx and last_used_wrap_counter? If not, please document why.

include/uapi/linux/vduse.h struct vduse_dev_response { · line 385

Quoted 6 lines
include/uapi/linux/vduse.h +6 -0
+
+/**
+ * struct vduse_reconnect_mmap_info
+ * @size: mapping memory size, here we use page_size
+ * @max_index: the number of pages allocated in kernel,just
+ * use for check

Did you mean "sanity check"?

Thanks

Quoted 14 lines
include/uapi/linux/vduse.h +10 -1
+ */
+
+struct vduse_reconnect_mmap_info {
+       __u32 size;
+       __u32 max_index;
+};
+
+#define VDUSE_GET_RECONNECT_INFO \
+       _IOWR(VDUSE_BASE, 0x1b, struct vduse_reconnect_mmap_info)
+
 #endif /* _UAPI_VDUSE_H_ */
--

2.34.3

#8 lore raw

Re: [PATCH v1 3/4] vduse: Add new ioctl VDUSE_GET_RECONNECT_INFO

Quoted 7 lines · Jason Wang
Quoted 2 lines · Cindy Lu

In VDUSE_GET_RECONNECT_INFO, the Userspace App can get the map size

I'm not sure why this is needed, the structure that mmaped to userspace should belong to uAPI then userspace can do versions there?

Ok, so I think this makes sense, it can help to avoid duplicating versions in each mmaped pages.

Thanks

author
#9 lore raw

PATCH 4/4 vduse: update the vq_info in ioctl

In reply to Cindy Lu

In VDUSE_VQ_GET_INFO, the driver will sync the last_avail_idx with reconnect info, After mapping the reconnect pages to userspace The userspace App will update the reconnect_time in struct vhost_reconnect_vring, If this is not 0 then it means this vq is reconnected and will update the last_avail_idx

---
 drivers/vdpa/vdpa_user/vduse_dev.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/vdpa/vdpa_user/vduse_dev.c +16 -0
index 0f15e7ac716b..42e7a90ab74c 100644
@@ -1213,6 +1213,9 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
 		struct vduse_vq_info vq_info;
 		struct vduse_virtqueue *vq;
 		u32 index;
+		struct vdpa_reconnect_info *area;
+		struct vhost_reconnect_vring *vq_reconnect;
+		struct vhost_reconnect_data *dev_reconnect;
 
 		ret = -EFAULT;
 		if (copy_from_user(&vq_info, argp, sizeof(vq_info)))
@@ -1244,6 +1247,19 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
 
 		vq_info.ready = vq->ready;
 
+		area = &dev->reconnect_status;
+		dev_reconnect = (struct vhost_reconnect_data *)area->vaddr;
+
+		area = &vq->reconnect_info;
+		vq_reconnect = (struct vhost_reconnect_vring *)area->vaddr;
+		/*check if the vq is reconnect, if yes then update the last_avail_idx*/
+		if ((vq_reconnect->last_avail_idx !=
+		     vq_info.split.avail_index) &&
+		    (dev_reconnect->reconnect_time != 0)) {
+			vq_info.split.avail_index =
+				vq_reconnect->last_avail_idx;
+		}
+
 		ret = -EFAULT;
 		if (copy_to_user(argp, &vq_info, sizeof(vq_info)))
 			break;
Signature · 1 line
2.34.3
#10 lore raw

Re: [PATCH v1 4/4] vduse: update the vq_info in ioctl

Quoted 32 lines · Cindy Lu

In VDUSE_VQ_GET_INFO, the driver will sync the last_avail_idx with reconnect info, After mapping the reconnect pages to userspace The userspace App will update the reconnect_time in struct vhost_reconnect_vring, If this is not 0 then it means this vq is reconnected and will update the last_avail_idx

---
 drivers/vdpa/vdpa_user/vduse_dev.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/vdpa/vdpa_user/vduse_dev.c +5 -0
index 0f15e7ac716b..42e7a90ab74c 100644
@@ -1213,6 +1213,9 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
                struct vduse_vq_info vq_info;
                struct vduse_virtqueue *vq;
                u32 index;
+               struct vdpa_reconnect_info *area;
+               struct vhost_reconnect_vring *vq_reconnect;
+               struct vhost_reconnect_data *dev_reconnect;
 
                ret = -EFAULT;
                if (copy_from_user(&vq_info, argp, sizeof(vq_info)))
@@ -1244,6 +1247,19 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
 
                vq_info.ready = vq->ready;
 
+               area = &dev->reconnect_status;
+               dev_reconnect = (struct vhost_reconnect_data *)area->vaddr;

drivers/vdpa/vdpa_user/vduse_dev.c static long vduse_dev_ioctl(struct file *file, unsigned int cmd, · line 1261

Quoted 10 lines
drivers/vdpa/vdpa_user/vduse_dev.c +10 -0
+
+               area = &vq->reconnect_info;
+               vq_reconnect = (struct vhost_reconnect_vring *)area->vaddr;
+               /*check if the vq is reconnect, if yes then update the last_avail_idx*/
+               if ((vq_reconnect->last_avail_idx !=
+                    vq_info.split.avail_index) &&
+                   (dev_reconnect->reconnect_time != 0)) {
+                       vq_info.split.avail_index =
+                               vq_reconnect->last_avail_idx;
+               }

This may work only for split, how about packed? And I wonder when such synchronization is not done at set_vq_state() but get_vq_info()?

Thanks

Quoted 7 lines
drivers/vdpa/vdpa_user/vduse_dev.c +1 -1
+
                ret = -EFAULT;
                if (copy_to_user(argp, &vq_info, sizeof(vq_info)))
                        break;
--

2.34.3

#11 lore raw
In reply to Cindy Lu
Quoted 11 lines · Cindy Lu

Here is the reconnect support in vduse,

The kernel will allocate pages for reconnection userspace needs to use ioctl VDUSE_GET_RECONNECT_INFO to get the mmap related infomation and then map these pages to userspace.
The kernel and userspace will use these pages to sync the reconnect information

Tested in vduse + dpdk test-pmd

I would like to see the DPDK part as a reference. Would you mind giving me a pointer?

Thanks

Quoted 14 lines

Cindy Lu (4):
vduse: Add function to get/free the pages for reconnection
vduse: Add file operation for mmap
vduse: Add new ioctl VDUSE_GET_RECONNECT_INFO
vduse: update the vq_info in ioctl

 drivers/vdpa/vdpa_user/vduse_dev.c | 175 +++++++++++++++++++++++++++++
 include/uapi/linux/vduse.h         |  43 +++++++
 2 files changed, 218 insertions(+)

--
2.34.3

author
#12 lore raw
Quoted 12 lines · Jason Wang
Quoted 10 lines · Cindy Lu

Here is the reconnect support in vduse,

The kernel will allocate pages for reconnection userspace needs to use ioctl VDUSE_GET_RECONNECT_INFO to get the mmap related infomation and then map these pages to userspace.
The kernel and userspace will use these pages to sync the reconnect information

Quoted 7 lines
Quoted 1 line

Tested in vduse + dpdk test-pmd

I would like to see the DPDK part as a reference. Would you mind giving me a pointer?

Thanks

sure, Will send the dpdk patch soon
thanks
cindy

Quoted 15 lines
Quoted 14 lines

Cindy Lu (4):
vduse: Add function to get/free the pages for reconnection
vduse: Add file operation for mmap
vduse: Add new ioctl VDUSE_GET_RECONNECT_INFO
vduse: update the vq_info in ioctl

 drivers/vdpa/vdpa_user/vduse_dev.c | 175 +++++++++++++++++++++++++++++
 include/uapi/linux/vduse.h         |  43 +++++++
 2 files changed, 218 insertions(+)

--
2.34.3

#13 lore raw
Quoted 16 lines · Cindy Lu
Quoted 14 lines · Jason Wang
Quoted 11 lines · Cindy Lu

Here is the reconnect support in vduse,

The kernel will allocate pages for reconnection userspace needs to use ioctl VDUSE_GET_RECONNECT_INFO to get the mmap related infomation and then map these pages to userspace.
The kernel and userspace will use these pages to sync the reconnect information

Tested in vduse + dpdk test-pmd

Quoted 8 lines
Quoted 5 lines

I would like to see the DPDK part as a reference. Would you mind giving me a pointer?

Thanks

sure, Will send the dpdk patch soon
thanks
cindy

Btw, I would expect to add the protocol into the VDUSE doc to describe the exact step for doing reconnection.

Thanks

Quoted 16 lines
Quoted 15 lines
Quoted 14 lines

Cindy Lu (4):
vduse: Add function to get/free the pages for reconnection
vduse: Add file operation for mmap
vduse: Add new ioctl VDUSE_GET_RECONNECT_INFO
vduse: update the vq_info in ioctl

 drivers/vdpa/vdpa_user/vduse_dev.c | 175 +++++++++++++++++++++++++++++
 include/uapi/linux/vduse.h         |  43 +++++++
 2 files changed, 218 insertions(+)

--
2.34.3