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

From: Jason Wang
Date: Thu Dec 07 2023 - 03:46:31 EST


On Tue, Dec 5, 2023 at 4:35 PM Cindy Lu <lulu@xxxxxxxxxx> wrote:
>
> Add the function vduse_alloc_reconnnect_info_mem
> and vduse_alloc_reconnnect_info_mem
> These functions allow vduse to allocate and free memory for reconnection
> information. The amount of memory allocated is (vq_num + 1) pages.
>
> Page 0 is reserved for saving the dev reconnection information, which
> will be maintained by the Userspace App. Pages 1 to vq_num + 1 will be
> used to save the reconnection information for vqs.
>
> Signed-off-by: Cindy Lu <lulu@xxxxxxxxxx>
> ---
> drivers/vdpa/vdpa_user/vduse_dev.c | 64 ++++++++++++++++++++++++++++++
> 1 file changed, 64 insertions(+)
>
> diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vduse_dev.c
> index dd074a7b4bc7..52ccde636406 100644
> --- a/drivers/vdpa/vdpa_user/vduse_dev.c
> +++ b/drivers/vdpa/vdpa_user/vduse_dev.c
> @@ -41,6 +41,7 @@
> #define VDUSE_IOVA_SIZE (128 * 1024 * 1024)
> #define VDUSE_MSG_DEFAULT_TIMEOUT 30
>
> +

Unnecessary changes.

> struct vduse_virtqueue {
> u16 index;
> u16 num_max;
> @@ -57,6 +58,7 @@ struct vduse_virtqueue {
> struct vdpa_callback cb;
> struct work_struct inject;
> struct work_struct kick;
> + unsigned long vdpa_reconnect_vaddr;
> };
>
> struct vduse_dev;
> @@ -106,6 +108,7 @@ struct vduse_dev {
> u32 vq_align;
> struct vduse_umem *umem;
> struct mutex mem_lock;
> + unsigned long vdpa_reconnect_vaddr;
> };
>
> struct vduse_dev_msg {
> @@ -1030,6 +1033,57 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev,
> return ret;
> }
>
> +static int vduse_alloc_reconnnect_info_mem(struct vduse_dev *dev)
> +{
> + unsigned long vaddr = 0;
> + struct vduse_virtqueue *vq;
> +
> + for (int i = 0; i < dev->vq_num + 1; i++) {
> + if (i == 0) {
> + vaddr = __get_free_pages(
> + GFP_KERNEL | __GFP_ZERO,
> + get_order(VDUSE_RECONNCT_MMAP_SIZE));

order 0 should be sufficient here or anything I missed?

> + if (vaddr == 0)
> + return -ENOMEM;
> + dev->vdpa_reconnect_vaddr = vaddr;
> + }
> +
> + /*page 1~ vq_num + 1 save the reconnect info for vq*/
> + vq = &dev->vqs[i];
> + vaddr = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
> + get_order(VDUSE_RECONNCT_MMAP_SIZE));
> + if (vaddr == 0)
> + return -ENOMEM;
> +
> + vq->vdpa_reconnect_vaddr = vaddr;
> + }
> +
> + return 0;
> +}
> +
> +static int vduse_free_reconnnect_info_mem(struct vduse_dev *dev)
> +{
> + struct vduse_virtqueue *vq;
> +
> + for (int i = 0; i < dev->vq_num + 1; i++) {
> + if (i == 0) {
> + if (dev->vdpa_reconnect_vaddr)
> + free_pages(dev->vdpa_reconnect_vaddr,
> + get_order(VDUSE_RECONNCT_MMAP_SIZE));
> + dev->vdpa_reconnect_vaddr = 0;

Let's use NULL here.

> + }
> +
> + vq = &dev->vqs[i];
> +
> + if (vq->vdpa_reconnect_vaddr)
> + free_pages(vq->vdpa_reconnect_vaddr,
> + get_order(VDUSE_RECONNCT_MMAP_SIZE));
> + vq->vdpa_reconnect_vaddr = 0;
> + }
> +
> + return 0;
> +}
> +
> static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
> unsigned long arg)
> {
> @@ -1411,6 +1465,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);
>
> @@ -1563,9 +1619,17 @@ static int vduse_create_dev(struct vduse_dev_config *config,
> ret = PTR_ERR(dev->dev);
> goto err_dev;
> }
> +
> + ret = vduse_alloc_reconnnect_info_mem(dev);
> + if (ret < 0)
> + goto err_mem;
> +
> __module_get(THIS_MODULE);
>
> return 0;
> +
> +err_mem:
> + vduse_free_reconnnect_info_mem(dev);
> err_dev:
> idr_remove(&vduse_idr, dev->minor);
> err_idr:
> --
> 2.34.3
>

Thanks