mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steve Sistare <steven.sistare@oracle.com>
To: virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org
Cc: "Michael S. Tsirkin" <mst@redhat.com>,
	Jason Wang <jasowang@redhat.com>,
	Si-Wei Liu <si-wei.liu@oracle.com>,
	Eugenio Perez Martin <eperezma@redhat.com>,
	Xuan Zhuo <xuanzhuo@linux.alibaba.com>,
	Dragos Tatulea <dtatulea@nvidia.com>, Eli Cohen <elic@nvidia.com>,
	Xie Yongji <xieyongji@bytedance.com>,
	Steve Sistare <steven.sistare@oracle.com>
Subject: [RFC V1 03/13] vhost-vdpa: VHOST_NEW_OWNER
Date: Wed, 10 Jan 2024 12:40:05 -0800	[thread overview]
Message-ID: <1704919215-91319-4-git-send-email-steven.sistare@oracle.com> (raw)
In-Reply-To: <1704919215-91319-1-git-send-email-steven.sistare@oracle.com>

Add an ioctl to transfer file descriptor ownership and pinned memory
accounting from one process to another.

Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
---
 drivers/vhost/vdpa.c       | 37 +++++++++++++++++++++++++++++++++++++
 drivers/vhost/vhost.c      | 15 +++++++++++++++
 drivers/vhost/vhost.h      |  1 +
 include/uapi/linux/vhost.h | 10 ++++++++++
 4 files changed, 63 insertions(+)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 2269988d6d33..eb3a95e703b0 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -613,6 +613,40 @@ static long vhost_vdpa_resume(struct vhost_vdpa *v)
 	return ops->resume(vdpa);
 }
 
+static long vhost_vdpa_new_owner(struct vhost_vdpa *v)
+{
+	int r;
+	struct vhost_dev *vdev = &v->vdev;
+	struct mm_struct *mm_old = vdev->mm;
+	struct mm_struct *mm_new = current->mm;
+	long pinned_vm = v->pinned_vm;
+	unsigned long lock_limit = PFN_DOWN(rlimit(RLIMIT_MEMLOCK));
+
+	if (!mm_old)
+		return -EINVAL;
+
+	if (!v->vdpa->use_va &&
+	    pinned_vm + atomic64_read(&mm_new->pinned_vm) > lock_limit)
+		return -ENOMEM;
+
+	r = vhost_vdpa_bind_mm(v, mm_new);
+	if (r)
+		return r;
+
+	r = vhost_dev_new_owner(vdev);
+	if (r) {
+		vhost_vdpa_bind_mm(v, mm_old);
+		return r;
+	}
+
+	if (!v->vdpa->use_va) {
+		atomic64_sub(pinned_vm, &mm_old->pinned_vm);
+		atomic64_add(pinned_vm, &mm_new->pinned_vm);
+	}
+
+	return r;
+}
+
 static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,
 				   void __user *argp)
 {
@@ -843,6 +877,9 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
 	case VHOST_VDPA_RESUME:
 		r = vhost_vdpa_resume(v);
 		break;
+	case VHOST_NEW_OWNER:
+		r = vhost_vdpa_new_owner(v);
+		break;
 	default:
 		r = vhost_dev_ioctl(&v->vdev, cmd, argp);
 		if (r == -ENOIOCTLCMD)
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index e0c181ad17e3..0ce7ee9834f4 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -907,6 +907,21 @@ long vhost_dev_set_owner(struct vhost_dev *dev)
 }
 EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
 
+/* Caller should have device mutex */
+long vhost_dev_new_owner(struct vhost_dev *dev)
+{
+	if (dev->mm == current->mm)
+		return -EBUSY;
+
+	if (!vhost_dev_has_owner(dev))
+		return -EINVAL;
+
+	vhost_detach_mm(dev);
+	vhost_attach_mm(dev);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(vhost_dev_new_owner);
+
 static struct vhost_iotlb *iotlb_alloc(void)
 {
 	return vhost_iotlb_alloc(max_iotlb_entries,
diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
index f60d5f7bef94..cd0dab21d99e 100644
--- a/drivers/vhost/vhost.h
+++ b/drivers/vhost/vhost.h
@@ -185,6 +185,7 @@ void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
 		    int (*msg_handler)(struct vhost_dev *dev, u32 asid,
 				       struct vhost_iotlb_msg *msg));
 long vhost_dev_set_owner(struct vhost_dev *dev);
+long vhost_dev_new_owner(struct vhost_dev *dev);
 bool vhost_dev_has_owner(struct vhost_dev *dev);
 long vhost_dev_check_owner(struct vhost_dev *);
 struct vhost_iotlb *vhost_dev_reset_owner_prepare(void);
diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index 649560c685f1..5e3cdce4c0cf 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -123,6 +123,16 @@
 #define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64)
 #define VHOST_GET_BACKEND_FEATURES _IOR(VHOST_VIRTIO, 0x26, __u64)
 
+/* Set current process as the new owner of this file descriptor.  The fd must
+ * already be owned, via a prior call to VHOST_SET_OWNER.  The pinned memory
+ * count is transferred from the previous to the new owner.
+ * Errors:
+ *   EINVAL: not owned
+ *   EBUSY:  caller is already the owner
+ *   ENOMEM: RLIMIT_MEMLOCK exceeded
+ */
+#define VHOST_NEW_OWNER _IO(VHOST_VIRTIO, 0x27)
+
 /* VHOST_NET specific defines */
 
 /* Attach virtio net ring to a raw socket, or tap device.
-- 
2.39.3


  parent reply	other threads:[~2024-01-10 20:40 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-10 20:40 [RFC V1 00/13] vdpa live update Steve Sistare
2024-01-10 20:40 ` [RFC V1 01/13] vhost-vdpa: count pinned memory Steve Sistare
2024-01-10 22:24   ` Michael S. Tsirkin
2024-01-17 20:34     ` Steven Sistare
2024-01-10 20:40 ` [RFC V1 02/13] vhost-vdpa: pass mm to bind Steve Sistare
2024-01-10 20:40 ` Steve Sistare [this message]
2024-01-10 20:40 ` [RFC V1 04/13] vhost-vdpa: VHOST_BACKEND_F_NEW_OWNER Steve Sistare
2024-01-10 20:40 ` [RFC V1 05/13] vhost-vdpa: VHOST_IOTLB_REMAP Steve Sistare
2024-01-11  3:08   ` Jason Wang
2024-01-17 20:31     ` Steven Sistare
2024-01-22  4:05       ` Jason Wang
2024-01-16 18:14   ` Eugenio Perez Martin
2024-02-09 15:49     ` Steven Sistare
2024-01-10 20:40 ` [RFC V1 06/13] vhost-vdpa: VHOST_BACKEND_F_IOTLB_REMAP Steve Sistare
2024-01-10 20:40 ` [RFC V1 07/13] vhost-vdpa: flush workers on suspend Steve Sistare
2024-01-11  3:09   ` Jason Wang
2024-01-11 16:17     ` Mike Christie
2024-01-12  2:28       ` Jason Wang
2024-01-17 20:30         ` Steven Sistare
2024-01-10 20:40 ` [RFC V1 08/13] vduse: " Steve Sistare
2024-01-11  3:09   ` Jason Wang
2024-01-17 20:31     ` Steven Sistare
2024-01-10 20:40 ` [RFC V1 09/13] vdpa_sim: reset must not run Steve Sistare
2024-01-16 18:33   ` Eugenio Perez Martin
2024-01-10 20:40 ` [RFC V1 10/13] vdpa_sim: flush workers on suspend Steve Sistare
2024-01-16 18:57   ` Eugenio Perez Martin
2024-01-17 20:31     ` Steven Sistare
2024-01-10 20:40 ` [RFC V1 11/13] vdpa/mlx5: new owner capability Steve Sistare
2024-01-10 20:40 ` [RFC V1 12/13] vdpa_sim: " Steve Sistare
2024-01-10 20:40 ` [RFC V1 13/13] vduse: " Steve Sistare
2024-01-11  2:55 ` [RFC V1 00/13] vdpa live update Jason Wang
2024-01-17 20:31   ` Steven Sistare
2024-01-22  4:12     ` 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=1704919215-91319-4-git-send-email-steven.sistare@oracle.com \
    --to=steven.sistare@oracle.com \
    --cc=dtatulea@nvidia.com \
    --cc=elic@nvidia.com \
    --cc=eperezma@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=si-wei.liu@oracle.com \
    --cc=virtualization@lists.linux-foundation.org \
    --cc=xieyongji@bytedance.com \
    --cc=xuanzhuo@linux.alibaba.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®