mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Cindy Lu <lulu@redhat.com>
To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com,
	xieyongji@bytedance.com, linux-kernel@vger.kernel.org,
	maxime.coquelin@redhat.com
Subject: [PATCH v3 4/7] vduse: Add function to get/free the pages for reconnection
Date: Tue,  5 Dec 2023 16:34:41 +0800	[thread overview]
Message-ID: <20231205083444.3029239-5-lulu@redhat.com> (raw)
In-Reply-To: <20231205083444.3029239-1-lulu@redhat.com>

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@redhat.com>
---
 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
 
+
 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));
+			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;
+		}
+
+		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


  parent reply	other threads:[~2023-12-05  8:36 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-05  8:34 [PATCH v3 0/7] vduse: Add support " Cindy Lu
2023-12-05  8:34 ` [PATCH v3 1/7] vdpa: Move struct vdpa_vq_state to uAPI Cindy Lu
2023-12-05  8:34 ` [PATCH v3 2/7] vduse: Add new uAPI for vduse reconnection Cindy Lu
2023-12-07  8:46   ` Jason Wang
2023-12-05  8:34 ` [PATCH v3 3/7] vduse: Add new ioctl VDUSE_DEV_GET_CONFIG Cindy Lu
2023-12-05  8:34 ` Cindy Lu [this message]
2023-12-07  8:46   ` [PATCH v3 4/7] vduse: Add function to get/free the pages for reconnection Jason Wang
2023-12-05  8:34 ` [PATCH v3 5/7] vduse: Add file operation for mmap Cindy Lu
2023-12-07  8:46   ` Jason Wang
2023-12-05  8:34 ` [PATCH v3 6/7] vduse: Update the vq_info in ioctl VDUSE_VQ_GET_INFO Cindy Lu
2023-12-07  8:46   ` Jason Wang
2023-12-05  8:34 ` [PATCH v3 7/7] Documentation: Add reconnect process for VDUSE Cindy Lu
2023-12-07  8:46   ` Jason Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231205083444.3029239-5-lulu@redhat.com \
    --to=lulu@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maxime.coquelin@redhat.com \
    --cc=mst@redhat.com \
    --cc=xieyongji@bytedance.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®