basic device IOTLB support
Subsystems (5)
v3 cover basic device IOTLB support
This patch tries to implement an device IOTLB for vhost. This could be used with for co-operation with userspace IOMMU implementation (qemu) for a secure DMA environment (DMAR) in guest.
The idea is simple. When vhost meets an IOTLB miss, it will request the assistance of userspace to do the translation, this is done through:
- when there's a IOTLB miss, it will notify userspace through vhost_net fd and then userspace read the fault address, size and access from vhost fd.
- userspace write the translation result back to vhost fd, vhost can then update its IOTLB.
The codes were optimized for fixed mapping users e.g dpdk in guest. It will be slow if dynamic mappings were used in guest. We could do optimizations on top.
The codes were designed to be architecture independent. It should be easily ported to any architecture.
Stress tested with l2fwd/vfio in guest with 4K/2M/1G page size. On 1G hugepage case, 100% TLB hit rate were noticed.
Changes from V2:
- introduce memory accessors for vhost
- switch from ioctls to oridinary file read/write for iotlb miss and updating
- do not assume virtqueue were virtually mapped contiguously, all virtqueue access were done throug IOTLB
- verify memory access during IOTLB update and fail early
- introduce a module parameter for the size of IOTLB
Changes from V1:
- support any size/range of updating and invalidation through introducing the interval tree.
- convert from per device iotlb request to per virtqueue iotlb request, this solves the possible deadlock in V1.
- read/write permission check support.
Please review.
Jason Wang (3):
vhost: introduce vhost memory accessors
vhost: convert pre sorted vhost memory array to interval tree
vhost: device IOTLB API
drivers/vhost/net.c | 63 +++- drivers/vhost/vhost.c | 760 ++++++++++++++++++++++++++++++++++++++------- drivers/vhost/vhost.h | 60 +++- include/uapi/linux/vhost.h | 28 ++ 4 files changed, 790 insertions(+), 121 deletions(-)
Signature · 1 line
2.7.4
RFC PATCH v3 1/3 vhost: introduce vhost memory accessors
This patch introduces vhost memory accessors which were just wrappers for userspace address access helpers. This is a requirement for vhost device iotlb implementation which will add iotlb translations in those accessors.
--- drivers/vhost/net.c | 1 + drivers/vhost/vhost.c | 50 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 36 insertions(+), 15 deletions(-)
drivers/vhost/net.c +1 -0
@@ -985,6 +985,7 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)vhost_net_disable_vq (n, vq); vq->private_data = sock; +/* FIXME: iotlb prefetch here? */ r =vhost_vq_init_access (vq);if (r) goto err_used;
drivers/vhost/vhost.c +35 -15
134 lines not on this page · raw message
Signature · 1 line
2.7.4
RFC PATCH v3 1/3 vhost: introduce vhost memory accessors
In reply to Jason WangThis patch introduces vhost memory accessors which were just wrappers for userspace address access helpers. This is a requirement for vhost device iotlb implementation which will add iotlb translations in those accessors.
--- drivers/vhost/net.c | 1 + drivers/vhost/vhost.c | 50 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 36 insertions(+), 15 deletions(-)
drivers/vhost/net.c +1 -0
@@ -985,6 +985,7 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)vhost_net_disable_vq (n, vq); vq->private_data = sock; +/* FIXME: iotlb prefetch here? */ r =vhost_vq_init_access (vq);if (r) goto err_used;
drivers/vhost/vhost.c +35 -15
134 lines not on this page · raw message
Signature · 1 line
2.7.4
RFC PATCH v3 2/3 vhost: convert pre sorted vhost memory array to interval tree
In reply to Jason WangCurrent pre-sorted memory region array has some limitations for future device IOTLB conversion:
1) need extra work for adding and removing a single region, and it's expected to be slow because of sorting or memory re-allocation.
2) need extra work of removing a large range which may intersect several regions with different size.
3) need trick for a replacement policy like LRU
To overcome the above shortcomings, this patch convert it to interval tree which can easily address the above issue with almost no extra work.
The patch could be used for:
- Extend the current API and only let the userspace to send diffs of memory table.
- Simplify Device IOTLB implementation.
--- drivers/vhost/net.c | 8 +-- drivers/vhost/vhost.c | 182 ++++++++++++++++++++++++++++---------------------- drivers/vhost/vhost.h | 27 ++++++-- 3 files changed, 128 insertions(+), 89 deletions(-)
drivers/vhost/net.c +4 -4
@@ -1037,20 +1037,20 @@ static long vhost_net_reset_owner(struct vhost_net *n)struct socket *tx_sock =NULL ;struct socket *rx_sock =NULL ;long err; -struct vhost_memory *memory; +struct vhost_umem *umem;mutex_lock (&n->dev.mutex); err =vhost_dev_check_owner (&n->dev);if (err) goto done; - memory =vhost_dev_reset_owner_prepare (); -if (!memory) { + umem =vhost_dev_reset_owner_prepare (); +if (!umem) { err = -ENOMEM ; goto done; }vhost_net_stop (n, &tx_sock, &rx_sock);vhost_net_flush (n); -vhost_dev_reset_owner (&n->dev, memory); +vhost_dev_reset_owner (&n->dev, umem);vhost_net_vq_reset (n); done:mutex_unlock (&n->dev.mutex);
drivers/vhost/vhost.c +101 -81
333 lines not on this page · raw message
drivers/vhost/vhost.h +23 -4
@@ -53,6 +53,25 @@ struct vhost_log {u64 len; }; +#define START (node) ((node)->start) +#define LAST (node) ((node)->last) + +struct vhost_umem_node { +struct rb_node rb; +struct list_head link; +__u64 start; +__u64 last; +__u64 size; +__u64 userspace_addr; +__u64 flags_padding; +__u64 __subtree_last; +}; + +struct vhost_umem { +struct rb_root umem_tree; +struct list_head umem_list; +}; +/* The virtqueue structure describes a queue attached to a device. */ struct vhost_virtqueue {struct vhost_dev *dev;
@@ -101,7 +120,7 @@ struct vhost_virtqueue {struct iovec *indirect;struct vring_used_elem *heads;/* Protected by virtqueue mutex. */ -struct vhost_memory *memory; +struct vhost_umem *umem;void *private_data;u64 acked_features;/* Log write descriptors */
@@ -119,7 +138,6 @@ struct vhost_virtqueue { };struct vhost_dev { -struct vhost_memory *memory;struct mm_struct *mm;struct mutex mutex;struct vhost_virtqueue **vqs;
@@ -129,14 +147,15 @@ struct vhost_dev {spinlock_t work_lock;struct list_head work_list;struct task_struct *worker; +struct vhost_umem *umem; };void vhost_dev_init (struct vhost_dev *,struct vhost_virtqueue **vqs,int nvqs);long vhost_dev_set_owner (struct vhost_dev *dev);bool vhost_dev_has_owner (struct vhost_dev *dev);long vhost_dev_check_owner (struct vhost_dev *); -struct vhost_memory *vhost_dev_reset_owner_prepare (void ); -void vhost_dev_reset_owner (struct vhost_dev *,struct vhost_memory *); +struct vhost_umem *vhost_dev_reset_owner_prepare (void ); +void vhost_dev_reset_owner (struct vhost_dev *,struct vhost_umem *);void vhost_dev_cleanup (struct vhost_dev *,bool locked);void vhost_dev_stop (struct vhost_dev *);long vhost_dev_ioctl (struct vhost_dev *,unsigned int ioctl,void __user *argp);
Signature · 1 line
2.7.4
RFC PATCH v3 3/3 vhost: device IOTLB API
In reply to Jason WangThis patch tries to implement an device IOTLB for vhost. This could be used with for co-operation with userspace(qemu) implementation of DMA remapping.
The idea is simple, cache the translation in a software device IOTLB (interval tree) implementation in vhost and make vhost_net file descriptor could be read or wrote by a process. When vhost meets an IOTLB miss, the fault address, size and access could be read from the file. After userspace finishes the translation, it write the translated address to the vhost_net file to update the device IOTLB.
When device IOTLB (VHOST_F_DEVICE_IOTLB) is enabled all vq address set by ioctl were treated as iova instead of virtual address and the accessing could only be done through IOTLB instead of direct userspace memory access. Before each rounds or vq processing, all vq metadata were prefetched in device IOTLB to make sure no translation fault happens during vq processing.
In most cases, virtqueue were mapped contiguous even in virtual address. So the IOTLB translation for virtqueue itself maybe a little bit slower. We can add fast path on top of this patch.
--- drivers/vhost/net.c | 54 ++++- drivers/vhost/vhost.c | 586 +++++++++++++++++++++++++++++++++++++++++---- drivers/vhost/vhost.h | 35 ++- include/uapi/linux/vhost.h | 28 +++ 4 files changed, 656 insertions(+), 47 deletions(-)
drivers/vhost/net.c +50 -4
@@ -61,7 +61,8 @@ MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;"enum {VHOST_NET_FEATURES =VHOST_FEATURES | (1ULL <<VHOST_NET_F_VIRTIO_NET_HDR ) | - (1ULL <<VIRTIO_NET_F_MRG_RXBUF ) + (1ULL <<VIRTIO_NET_F_MRG_RXBUF ) | + (1ULL <<VHOST_F_DEVICE_IOTLB ) };enum {
@@ -308,7 +309,8 @@ static int vhost_net_tx_get_vq_desc(struct vhost_net *net, {unsigned long uninitialized_var (endtime );int r =vhost_get_vq_desc (vq, vq->iov,ARRAY_SIZE (vq->iov), - out_num, in_num,NULL ,NULL ); + out_num, in_num,NULL ,NULL , +VHOST_ACCESS_RO );if (r == vq->num && vq->busyloop_timeout) {preempt_disable ();
@@ -318,7 +320,8 @@ static int vhost_net_tx_get_vq_desc(struct vhost_net *net,cpu_relax_lowlatency ();preempt_enable (); r =vhost_get_vq_desc (vq, vq->iov,ARRAY_SIZE (vq->iov), - out_num, in_num,NULL ,NULL ); + out_num, in_num,NULL ,NULL , +VHOST_ACCESS_RO ); }return r;
@@ -351,6 +354,9 @@ static void handle_tx(struct vhost_net *net)if (!sock) goto out; +if (!vq_iotlb_prefetch (vq)) + goto out; +vhost_disable_notify (&net->dev, vq); hdr_size = nvq->vhost_hlen;
@@ -538,7 +544,7 @@ static int get_rx_bufs(struct vhost_virtqueue *vq, } r =vhost_get_vq_desc (vq, vq->iov + seg,ARRAY_SIZE (vq->iov) - seg, &out, - &in, log, log_num); + &in, log, log_num,VHOST_ACCESS_WO );if (unlikely (r <0 )) goto err;
@@ -612,6 +618,10 @@ static void handle_rx(struct vhost_net *net) sock = vq->private_data;if (!sock) goto out; + +if (!vq_iotlb_prefetch (vq)) + goto out; +vhost_disable_notify (&net->dev, vq); vhost_hlen = nvq->vhost_hlen;
@@ -1085,6 +1095,11 @@ static int vhost_net_set_features(struct vhost_net *n, u64 features)mutex_unlock (&n->dev.mutex);return -EFAULT ; } +if ((features & (1ULL <<VHOST_F_DEVICE_IOTLB ))) { +if (vhost_init_device_iotlb (&n->dev, true)) +return -EFAULT ; + } +for (i =0 ; i <VHOST_NET_VQ_MAX ; ++i) {mutex_lock (&n->vqs[i].vq.mutex); n->vqs[i].vq.acked_features = features;
@@ -1167,9 +1182,40 @@ static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl, }#endif +static ssize_t vhost_net_chr_read_iter (struct kiocb *iocb,struct iov_iter *to) +{ +struct file *file = iocb->ki_filp; +struct vhost_net *n = file->private_data; +struct vhost_dev *dev = &n->dev; +int noblock = file->f_flags &O_NONBLOCK ; + +return vhost_chr_read_iter (dev, to, noblock); +} + +static ssize_t vhost_net_chr_write_iter (struct kiocb *iocb, +struct iov_iter *from) +{ +struct file *file = iocb->ki_filp; +struct vhost_net *n = file->private_data; +struct vhost_dev *dev = &n->dev; + +return vhost_chr_write_iter (dev, from); +} + +static unsigned int vhost_net_chr_poll (struct file *file,poll_table *wait) +{ +struct vhost_net *n = file->private_data; +struct vhost_dev *dev = &n->dev; + +return vhost_chr_poll (file, dev, wait); +} +static const struct file_operations vhost_net_fops = { .owner =THIS_MODULE , .release = vhost_net_release, + .read_iter = vhost_net_chr_read_iter, + .write_iter = vhost_net_chr_write_iter, + .poll = vhost_net_chr_poll, .unlocked_ioctl = vhost_net_ioctl, #ifdef CONFIG_COMPAT .compat_ioctl = vhost_net_compat_ioctl,
drivers/vhost/vhost.c +545 -41
778 lines not on this page · raw message
drivers/vhost/vhost.h +33 -2
@@ -63,13 +63,15 @@ struct vhost_umem_node {__u64 last;__u64 size;__u64 userspace_addr; -__u64 flags_padding; +__u32 perm; +__u32 flags_padding;__u64 __subtree_last; };struct vhost_umem {struct rb_root umem_tree;struct list_head umem_list; +int numem; };/* The virtqueue structure describes a queue attached to a device. */
@@ -117,10 +119,12 @@ struct vhost_virtqueue {u64 log_addr;struct iovec iov[UIO_MAXIOV ]; +struct iovec iotlb_iov[64 ];struct iovec *indirect;struct vring_used_elem *heads;/* Protected by virtqueue mutex. */ struct vhost_umem *umem; +struct vhost_umem *iotlb;void *private_data;u64 acked_features;/* Log write descriptors */
@@ -137,6 +141,12 @@ struct vhost_virtqueue {u32 busyloop_timeout; }; +struct vhost_msg_node { +struct vhost_msg msg; +struct vhost_virtqueue *vq; +struct list_head node; +}; +struct vhost_dev {struct mm_struct *mm;struct mutex mutex;
@@ -148,6 +158,11 @@ struct vhost_dev {struct list_head work_list;struct task_struct *worker;struct vhost_umem *umem; +struct vhost_umem *iotlb; +spinlock_t iotlb_lock; +struct list_head read_list; +struct list_head pending_list; +wait_queue_head_t wait; };void vhost_dev_init (struct vhost_dev *,struct vhost_virtqueue **vqs,int nvqs);
@@ -166,7 +181,8 @@ int vhost_log_access_ok(struct vhost_dev *);int vhost_get_vq_desc (struct vhost_virtqueue *,struct iovec iov[],unsigned int iov_count,unsigned int *out_num,unsigned int *in_num, -struct vhost_log *log,unsigned int *log_num); +struct vhost_log *log,unsigned int *log_num, +int access);void vhost_discard_vq_desc (struct vhost_virtqueue *,int n);int vhost_vq_init_access (struct vhost_virtqueue *);
@@ -184,6 +200,21 @@ bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);int vhost_log_write (struct vhost_virtqueue *vq,struct vhost_log *log,unsigned int log_num,u64 len); +int vq_iotlb_prefetch (struct vhost_virtqueue *vq); + +struct vhost_msg_node *vhost_new_msg (struct vhost_virtqueue *vq,int type); +void vhost_enqueue_msg (struct vhost_dev *dev, +struct list_head *head, +struct vhost_msg_node *node); +struct vhost_msg_node *vhost_dequeue_msg (struct vhost_dev *dev, +struct list_head *head); +unsigned int vhost_chr_poll (struct file *file,struct vhost_dev *dev, +poll_table *wait); +ssize_t vhost_chr_read_iter (struct vhost_dev *dev,struct iov_iter *to, +int noblock); +ssize_t vhost_chr_write_iter (struct vhost_dev *dev, +struct iov_iter *from); +int vhost_init_device_iotlb (struct vhost_dev *d,bool enabled);#define vq_err (vq, fmt, ...) do { \ pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
include/uapi/linux/vhost.h +28 -0
@@ -47,6 +47,32 @@ struct vhost_vring_addr {__u64 log_guest_addr; }; +/* no alignment requirement */ +struct vhost_iotlb_msg { +__u64 iova; +__u64 size; +__u64 uaddr; +#define VHOST_ACCESS_RO 0x1 +#define VHOST_ACCESS_WO 0x2 +#define VHOST_ACCESS_RW 0x3 +__u8 perm; +#define VHOST_IOTLB_MISS 1 +#define VHOST_IOTLB_UPDATE 2 +#define VHOST_IOTLB_INVALIDATE 3 +#define VHOST_IOTLB_ACCESS_FAIL 4 +__u8 type; +}; + +#define VHOST_IOTLB_MSG 0x1 + +struct vhost_msg { +int type; +union { +struct vhost_iotlb_msg iotlb; +__u8 padding[64 ]; + }; +}; +struct vhost_memory_region {__u64 guest_phys_addr;__u64 memory_size;/* bytes */
@@ -146,6 +172,8 @@ struct vhost_memory {#define VHOST_F_LOG_ALL 26/* vhost-net should add virtio_net_hdr for RX, and strip for TX packets. */ #define VHOST_NET_F_VIRTIO_NET_HDR 27 +/* Vhost have device IOTLB */ +#define VHOST_F_DEVICE_IOTLB 63/* VHOST_SCSI specific definitions */
Signature · 1 line
2.7.4
RFC PATCH v3 3/3 vhost: device IOTLB API
In reply to Jason WangThis patch tries to implement an device IOTLB for vhost. This could be used with for co-operation with userspace(qemu) implementation of DMA remapping.
The idea is simple, cache the translation in a software device IOTLB (interval tree) implementation in vhost and make vhost_net file descriptor could be read or wrote by a process. When vhost meets an IOTLB miss, the fault address, size and access could be read from the file. After userspace finishes the translation, it write the translated address to the vhost_net file to update the device IOTLB.
When device IOTLB (VHOST_F_DEVICE_IOTLB) is enabled all vq address set by ioctl were treated as iova instead of virtual address and the accessing could only be done through IOTLB instead of direct userspace memory access. Before each rounds or vq processing, all vq metadata were prefetched in device IOTLB to make sure no translation fault happens during vq processing.
In most cases, virtqueue were mapped contiguous even in virtual address. So the IOTLB translation for virtqueue itself maybe a little bit slower. We can add fast path on top of this patch.
--- drivers/vhost/net.c | 54 ++++- drivers/vhost/vhost.c | 586 +++++++++++++++++++++++++++++++++++++++++---- drivers/vhost/vhost.h | 35 ++- include/uapi/linux/vhost.h | 28 +++ 4 files changed, 656 insertions(+), 47 deletions(-)
drivers/vhost/net.c +50 -4
@@ -61,7 +61,8 @@ MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;"enum {VHOST_NET_FEATURES =VHOST_FEATURES | (1ULL <<VHOST_NET_F_VIRTIO_NET_HDR ) | - (1ULL <<VIRTIO_NET_F_MRG_RXBUF ) + (1ULL <<VIRTIO_NET_F_MRG_RXBUF ) | + (1ULL <<VHOST_F_DEVICE_IOTLB ) };enum {
@@ -308,7 +309,8 @@ static int vhost_net_tx_get_vq_desc(struct vhost_net *net, {unsigned long uninitialized_var (endtime );int r =vhost_get_vq_desc (vq, vq->iov,ARRAY_SIZE (vq->iov), - out_num, in_num,NULL ,NULL ); + out_num, in_num,NULL ,NULL , +VHOST_ACCESS_RO );if (r == vq->num && vq->busyloop_timeout) {preempt_disable ();
@@ -318,7 +320,8 @@ static int vhost_net_tx_get_vq_desc(struct vhost_net *net,cpu_relax_lowlatency ();preempt_enable (); r =vhost_get_vq_desc (vq, vq->iov,ARRAY_SIZE (vq->iov), - out_num, in_num,NULL ,NULL ); + out_num, in_num,NULL ,NULL , +VHOST_ACCESS_RO ); }return r;
@@ -351,6 +354,9 @@ static void handle_tx(struct vhost_net *net)if (!sock) goto out; +if (!vq_iotlb_prefetch (vq)) + goto out; +vhost_disable_notify (&net->dev, vq); hdr_size = nvq->vhost_hlen;
@@ -538,7 +544,7 @@ static int get_rx_bufs(struct vhost_virtqueue *vq, } r =vhost_get_vq_desc (vq, vq->iov + seg,ARRAY_SIZE (vq->iov) - seg, &out, - &in, log, log_num); + &in, log, log_num,VHOST_ACCESS_WO );if (unlikely (r <0 )) goto err;
@@ -612,6 +618,10 @@ static void handle_rx(struct vhost_net *net) sock = vq->private_data;if (!sock) goto out; + +if (!vq_iotlb_prefetch (vq)) + goto out; +vhost_disable_notify (&net->dev, vq); vhost_hlen = nvq->vhost_hlen;
@@ -1085,6 +1095,11 @@ static int vhost_net_set_features(struct vhost_net *n, u64 features)mutex_unlock (&n->dev.mutex);return -EFAULT ; } +if ((features & (1ULL <<VHOST_F_DEVICE_IOTLB ))) { +if (vhost_init_device_iotlb (&n->dev, true)) +return -EFAULT ; + } +for (i =0 ; i <VHOST_NET_VQ_MAX ; ++i) {mutex_lock (&n->vqs[i].vq.mutex); n->vqs[i].vq.acked_features = features;
@@ -1167,9 +1182,40 @@ static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl, }#endif +static ssize_t vhost_net_chr_read_iter (struct kiocb *iocb,struct iov_iter *to) +{ +struct file *file = iocb->ki_filp; +struct vhost_net *n = file->private_data; +struct vhost_dev *dev = &n->dev; +int noblock = file->f_flags &O_NONBLOCK ; + +return vhost_chr_read_iter (dev, to, noblock); +} + +static ssize_t vhost_net_chr_write_iter (struct kiocb *iocb, +struct iov_iter *from) +{ +struct file *file = iocb->ki_filp; +struct vhost_net *n = file->private_data; +struct vhost_dev *dev = &n->dev; + +return vhost_chr_write_iter (dev, from); +} + +static unsigned int vhost_net_chr_poll (struct file *file,poll_table *wait) +{ +struct vhost_net *n = file->private_data; +struct vhost_dev *dev = &n->dev; + +return vhost_chr_poll (file, dev, wait); +} +static const struct file_operations vhost_net_fops = { .owner =THIS_MODULE , .release = vhost_net_release, + .read_iter = vhost_net_chr_read_iter, + .write_iter = vhost_net_chr_write_iter, + .poll = vhost_net_chr_poll, .unlocked_ioctl = vhost_net_ioctl, #ifdef CONFIG_COMPAT .compat_ioctl = vhost_net_compat_ioctl,
drivers/vhost/vhost.c +545 -41
778 lines not on this page · raw message
drivers/vhost/vhost.h +33 -2
@@ -63,13 +63,15 @@ struct vhost_umem_node {__u64 last;__u64 size;__u64 userspace_addr; -__u64 flags_padding; +__u32 perm; +__u32 flags_padding;__u64 __subtree_last; };struct vhost_umem {struct rb_root umem_tree;struct list_head umem_list; +int numem; };/* The virtqueue structure describes a queue attached to a device. */
@@ -117,10 +119,12 @@ struct vhost_virtqueue {u64 log_addr;struct iovec iov[UIO_MAXIOV ]; +struct iovec iotlb_iov[64 ];struct iovec *indirect;struct vring_used_elem *heads;/* Protected by virtqueue mutex. */ struct vhost_umem *umem; +struct vhost_umem *iotlb;void *private_data;u64 acked_features;/* Log write descriptors */
@@ -137,6 +141,12 @@ struct vhost_virtqueue {u32 busyloop_timeout; }; +struct vhost_msg_node { +struct vhost_msg msg; +struct vhost_virtqueue *vq; +struct list_head node; +}; +struct vhost_dev {struct mm_struct *mm;struct mutex mutex;
@@ -148,6 +158,11 @@ struct vhost_dev {struct list_head work_list;struct task_struct *worker;struct vhost_umem *umem; +struct vhost_umem *iotlb; +spinlock_t iotlb_lock; +struct list_head read_list; +struct list_head pending_list; +wait_queue_head_t wait; };void vhost_dev_init (struct vhost_dev *,struct vhost_virtqueue **vqs,int nvqs);
@@ -166,7 +181,8 @@ int vhost_log_access_ok(struct vhost_dev *);int vhost_get_vq_desc (struct vhost_virtqueue *,struct iovec iov[],unsigned int iov_count,unsigned int *out_num,unsigned int *in_num, -struct vhost_log *log,unsigned int *log_num); +struct vhost_log *log,unsigned int *log_num, +int access);void vhost_discard_vq_desc (struct vhost_virtqueue *,int n);int vhost_vq_init_access (struct vhost_virtqueue *);
@@ -184,6 +200,21 @@ bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);int vhost_log_write (struct vhost_virtqueue *vq,struct vhost_log *log,unsigned int log_num,u64 len); +int vq_iotlb_prefetch (struct vhost_virtqueue *vq); + +struct vhost_msg_node *vhost_new_msg (struct vhost_virtqueue *vq,int type); +void vhost_enqueue_msg (struct vhost_dev *dev, +struct list_head *head, +struct vhost_msg_node *node); +struct vhost_msg_node *vhost_dequeue_msg (struct vhost_dev *dev, +struct list_head *head); +unsigned int vhost_chr_poll (struct file *file,struct vhost_dev *dev, +poll_table *wait); +ssize_t vhost_chr_read_iter (struct vhost_dev *dev,struct iov_iter *to, +int noblock); +ssize_t vhost_chr_write_iter (struct vhost_dev *dev, +struct iov_iter *from); +int vhost_init_device_iotlb (struct vhost_dev *d,bool enabled);#define vq_err (vq, fmt, ...) do { \ pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
include/uapi/linux/vhost.h +28 -0
@@ -47,6 +47,32 @@ struct vhost_vring_addr {__u64 log_guest_addr; }; +/* no alignment requirement */ +struct vhost_iotlb_msg { +__u64 iova; +__u64 size; +__u64 uaddr; +#define VHOST_ACCESS_RO 0x1 +#define VHOST_ACCESS_WO 0x2 +#define VHOST_ACCESS_RW 0x3 +__u8 perm; +#define VHOST_IOTLB_MISS 1 +#define VHOST_IOTLB_UPDATE 2 +#define VHOST_IOTLB_INVALIDATE 3 +#define VHOST_IOTLB_ACCESS_FAIL 4 +__u8 type; +}; + +#define VHOST_IOTLB_MSG 0x1 + +struct vhost_msg { +int type; +union { +struct vhost_iotlb_msg iotlb; +__u8 padding[64 ]; + }; +}; +struct vhost_memory_region {__u64 guest_phys_addr;__u64 memory_size;/* bytes */
@@ -146,6 +172,8 @@ struct vhost_memory {#define VHOST_F_LOG_ALL 26/* vhost-net should add virtio_net_hdr for RX, and strip for TX packets. */ #define VHOST_NET_F_VIRTIO_NET_HDR 27 +/* Vhost have device IOTLB */ +#define VHOST_F_DEVICE_IOTLB 63/* VHOST_SCSI specific definitions */
Signature · 1 line
2.7.4
Quoted 33 lines · Jason Wang
This patch tries to implement an device IOTLB for vhost. This could be used with for co-operation with userspace IOMMU implementation (qemu) for a secure DMA environment (DMAR) in guest.
The idea is simple. When vhost meets an IOTLB miss, it will request the assistance of userspace to do the translation, this is done through:
- when there's a IOTLB miss, it will notify userspace through
vhost_netfd and then userspace read the fault address, size and access from vhost fd.
- userspace write the translation result back to vhost fd, vhost can then update its IOTLB.The codes were optimized for fixed mapping users e.g dpdk in guest. It will be slow if dynamic mappings were used in guest. We could do optimizations on top.
The codes were designed to be architecture independent. It should be easily ported to any architecture.
Stress tested with l2fwd/vfio in guest with 4K/2M/1G page size. On 1G hugepage case, 100% TLB hit rate were noticed.
Changes from V2:
- introduce memory accessors for vhost
- switch from ioctls to oridinary file read/write for iotlb miss and updating
- do not assume virtqueue were virtually mapped contiguously, all virtqueue access were done throug IOTLB
- verify memory access during IOTLB update and fail early
- introduce a module parameter for the size of IOTLB
Quoted 8 lines
Changes from V1:
- support any size/range of updating and invalidation through introducing the interval tree.
- convert from per device iotlb request to per virtqueue iotlb request, this solves the possible deadlock in V1.
- read/write permission check support.Please review.
Have a benchmark on this. Test was done with l2fwd in guest.
For 2MB page, no difference in 64B performance and I notice a 4%-5% drop for 1500B performance compare to UIO in guest. We can add some shortcut to bypass the IOTLB for virtqueue accessing, but I think it's better to be done on top.
Quoted 12 lines
Jason Wang (3):
vhost: introduce vhost memory accessors
vhost: convert pre sorted vhost memory array to interval tree
vhost: device IOTLB APIdrivers/vhost/net.c | 63 +++- drivers/vhost/vhost.c | 760 ++++++++++++++++++++++++++++++++++++++------- drivers/vhost/vhost.h | 60 +++- include/uapi/linux/vhost.h | 28 ++ 4 files changed, 790 insertions(+), 121 deletions(-)
Quoted 33 lines · Jason Wang
This patch tries to implement an device IOTLB for vhost. This could be used with for co-operation with userspace IOMMU implementation (qemu) for a secure DMA environment (DMAR) in guest.
The idea is simple. When vhost meets an IOTLB miss, it will request the assistance of userspace to do the translation, this is done through:
- when there's a IOTLB miss, it will notify userspace through
vhost_netfd and then userspace read the fault address, size and access from vhost fd.
- userspace write the translation result back to vhost fd, vhost can then update its IOTLB.The codes were optimized for fixed mapping users e.g dpdk in guest. It will be slow if dynamic mappings were used in guest. We could do optimizations on top.
The codes were designed to be architecture independent. It should be easily ported to any architecture.
Stress tested with l2fwd/vfio in guest with 4K/2M/1G page size. On 1G hugepage case, 100% TLB hit rate were noticed.
Changes from V2:
- introduce memory accessors for vhost
- switch from ioctls to oridinary file read/write for iotlb miss and updating
- do not assume virtqueue were virtually mapped contiguously, all virtqueue access were done throug IOTLB
- verify memory access during IOTLB update and fail early
- introduce a module parameter for the size of IOTLB
Quoted 8 lines
Changes from V1:
- support any size/range of updating and invalidation through introducing the interval tree.
- convert from per device iotlb request to per virtqueue iotlb request, this solves the possible deadlock in V1.
- read/write permission check support.Please review.
Have a benchmark on this. Test was done with l2fwd in guest.
For 2MB page, no difference in 64B performance and I notice a 4%-5% drop for 1500B performance compare to UIO in guest. We can add some shortcut to bypass the IOTLB for virtqueue accessing, but I think it's better to be done on top.
Quoted 6 lines
Jason Wang (3):
vhost: introduce vhost memory accessors
vhost: convert pre sorted vhost memory array to interval tree
vhost: device IOTLB API
Quoted 6 lines
drivers/vhost/net.c | 63 +++- drivers/vhost/vhost.c | 760 ++++++++++++++++++++++++++++++++++++++------- drivers/vhost/vhost.h | 60 +++- include/uapi/linux/vhost.h | 28 ++ 4 files changed, 790 insertions(+), 121 deletions(-)
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Michael S. Tsirkin
maintainerQuoted 33 lines · Jason Wang
This patch tries to implement an device IOTLB for vhost. This could be used with for co-operation with userspace IOMMU implementation (qemu) for a secure DMA environment (DMAR) in guest.
The idea is simple. When vhost meets an IOTLB miss, it will request the assistance of userspace to do the translation, this is done through:
- when there's a IOTLB miss, it will notify userspace through
vhost_netfd and then userspace read the fault address, size and access from vhost fd.
- userspace write the translation result back to vhost fd, vhost can then update its IOTLB.The codes were optimized for fixed mapping users e.g dpdk in guest. It will be slow if dynamic mappings were used in guest. We could do optimizations on top.
The codes were designed to be architecture independent. It should be easily ported to any architecture.
Stress tested with l2fwd/vfio in guest with 4K/2M/1G page size. On 1G hugepage case, 100% TLB hit rate were noticed.
Changes from V2:
- introduce memory accessors for vhost
- switch from ioctls to oridinary file read/write for iotlb miss and updating
- do not assume virtqueue were virtually mapped contiguously, all virtqueue access were done throug IOTLB
- verify memory access during IOTLB update and fail early
- introduce a module parameter for the size of IOTLB
Quoted 8 lines
Changes from V1:
- support any size/range of updating and invalidation through introducing the interval tree.
- convert from per device iotlb request to per virtqueue iotlb request, this solves the possible deadlock in V1.
- read/write permission check support.Please review.
Nice, this looks good to me. Can you post a non-rfc please?
Quoted 13 lines
Jason Wang (3):
vhost: introduce vhost memory accessors
vhost: convert pre sorted vhost memory array to interval tree
vhost: device IOTLB APIdrivers/vhost/net.c | 63 +++- drivers/vhost/vhost.c | 760 ++++++++++++++++++++++++++++++++++++++------- drivers/vhost/vhost.h | 60 +++- include/uapi/linux/vhost.h | 28 ++ 4 files changed, 790 insertions(+), 121 deletions(-)Signature · 1 line
2.7.4
Quoted 41 lines · Michael S. Tsirkin
Quoted 40 lines · Jason Wang
This patch tries to implement an device IOTLB for vhost. This could be used with for co-operation with userspace IOMMU implementation (qemu) for a secure DMA environment (DMAR) in guest.
The idea is simple. When vhost meets an IOTLB miss, it will request the assistance of userspace to do the translation, this is done through:
- when there's a IOTLB miss, it will notify userspace through
vhost_netfd and then userspace read the fault address, size and access from vhost fd.
- userspace write the translation result back to vhost fd, vhost can then update its IOTLB.The codes were optimized for fixed mapping users e.g dpdk in guest. It will be slow if dynamic mappings were used in guest. We could do optimizations on top.
The codes were designed to be architecture independent. It should be easily ported to any architecture.
Stress tested with l2fwd/vfio in guest with 4K/2M/1G page size. On 1G hugepage case, 100% TLB hit rate were noticed.
Changes from V2:
- introduce memory accessors for vhost
- switch from ioctls to oridinary file read/write for iotlb miss and updating
- do not assume virtqueue were virtually mapped contiguously, all virtqueue access were done throug IOTLB
- verify memory access during IOTLB update and fail early
- introduce a module parameter for the size of IOTLBChanges from V1:
- support any size/range of updating and invalidation through introducing the interval tree.
- convert from per device iotlb request to per virtqueue iotlb request, this solves the possible deadlock in V1.
- read/write permission check support.
Quoted 4 lines
Quoted 1 line
Please review.
Nice, this looks good to me. Can you post a non-rfc please?
Posted, thanks.
Quoted 11 lines
Quoted 11 lines
Jason Wang (3):
vhost: introduce vhost memory accessors
vhost: convert pre sorted vhost memory array to interval tree
vhost: device IOTLB APIdrivers/vhost/net.c | 63 +++- drivers/vhost/vhost.c | 760 ++++++++++++++++++++++++++++++++++++++------- drivers/vhost/vhost.h | 60 +++- include/uapi/linux/vhost.h | 28 ++ 4 files changed, 790 insertions(+), 121 deletions(-)
Quoted 6 lines
Quoted 2 lines
Signature · 1 line
2.7.4--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization