All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 00/21] Control VQ support in vDPA
@ 2020-12-16  6:47 ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:47 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

Hi All:

This series tries to add the support for control virtqueue in vDPA.

Control virtqueue is used by networking device for accepting various
commands from the driver. It's a must to support multiqueue and other
configurations.

When used by vhost-vDPA bus driver for VM, the control virtqueue
should be shadowed via userspace VMM (Qemu) instead of being assigned
directly to Guest. This is because Qemu needs to know the device state
in order to start and stop device correctly (e.g for Live Migration).

This requies to isolate the memory mapping for control virtqueue
presented by vhost-vDPA to prevent guest from accesing it directly.

To achieve this, vDPA introduce two new abstractions:

- address space: identified through address space id (ASID) and a set
                 of memory mapping in maintained
- virtqueue group: the minimal set of virtqueues that must share an
                 address space

Device needs to advertise the following attributes to vDPA:

- the number of address spaces supported in the device
- the number of virtqueue groups supported in the device
- the mappings from a specific virtqueue to its virtqueue groups

The mappings from virtqueue to virtqueue groups is fixed and defined
by vDPA device driver. E.g:

- For the device that has hardware ASID support, it can simply
  advertise a per virtqueue virtqueue group.
- For the device that does not have hardware ASID support, it can
  simply advertise a single virtqueue group that contains all
  virtqueues. Or if it wants a software emulated control virtqueue, it
  can advertise two virtqueue groups, one is for cvq, another is for
  the rest virtqueues.

vDPA also allow to change the association between virtqueue group and
address space. So in the case of control virtqueue, userspace
VMM(Qemu) may use a dedicated address space for the control virtqueue
group to isolate the memory mapping.

The vhost/vhost-vDPA is also extend for the userspace to:

- query the number of virtqueue groups and address spaces supported by
  the device
- query the virtqueue group for a specific virtqueue
- assocaite a virtqueue group with an address space
- send ASID based IOTLB commands

This will help userspace VMM(Qemu) to detect whether the control vq
could be supported and isolate memory mappings of control virtqueue
from the others.

To demonstrate the usage, vDPA simulator is extended to support
setting MAC address via a emulated control virtqueue.

Please review.

Changes since RFC:

- tweak vhost uAPI documentation
- switch to use device specific IOTLB really in patch 4
- tweak the commit log
- fix that ASID in vhost is claimed to be 32 actually but 16bit
  actually
- fix use after free when using ASID with IOTLB batching requests
- switch to use Stefano's patch for having separated iov
- remove unused "used_as" variable
- fix the iotlb/asid checking in vhost_vdpa_unmap()

Thanks

Jason Wang (20):
  vhost: move the backend feature bits to vhost_types.h
  virtio-vdpa: don't set callback if virtio doesn't need it
  vhost-vdpa: passing iotlb to IOMMU mapping helpers
  vhost-vdpa: switch to use vhost-vdpa specific IOTLB
  vdpa: add the missing comment for nvqs in struct vdpa_device
  vdpa: introduce virtqueue groups
  vdpa: multiple address spaces support
  vdpa: introduce config operations for associating ASID to a virtqueue
    group
  vhost_iotlb: split out IOTLB initialization
  vhost: support ASID in IOTLB API
  vhost-vdpa: introduce asid based IOTLB
  vhost-vdpa: introduce uAPI to get the number of virtqueue groups
  vhost-vdpa: introduce uAPI to get the number of address spaces
  vhost-vdpa: uAPI to get virtqueue group id
  vhost-vdpa: introduce uAPI to set group ASID
  vhost-vdpa: support ASID based IOTLB API
  vdpa_sim: advertise VIRTIO_NET_F_MTU
  vdpa_sim: factor out buffer completion logic
  vdpa_sim: filter destination mac address
  vdpasim: control virtqueue support

Stefano Garzarella (1):
  vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov

 drivers/vdpa/ifcvf/ifcvf_main.c   |   9 +-
 drivers/vdpa/mlx5/net/mlx5_vnet.c |  11 +-
 drivers/vdpa/vdpa.c               |   8 +-
 drivers/vdpa/vdpa_sim/vdpa_sim.c  | 292 ++++++++++++++++++++++++------
 drivers/vhost/iotlb.c             |  23 ++-
 drivers/vhost/vdpa.c              | 246 ++++++++++++++++++++-----
 drivers/vhost/vhost.c             |  23 ++-
 drivers/vhost/vhost.h             |   4 +-
 drivers/virtio/virtio_vdpa.c      |   2 +-
 include/linux/vdpa.h              |  42 ++++-
 include/linux/vhost_iotlb.h       |   2 +
 include/uapi/linux/vhost.h        |  25 ++-
 include/uapi/linux/vhost_types.h  |  10 +-
 13 files changed, 561 insertions(+), 136 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 136+ messages in thread

* [PATCH 00/21] Control VQ support in vDPA
@ 2020-12-16  6:47 ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:47 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

Hi All:

This series tries to add the support for control virtqueue in vDPA.

Control virtqueue is used by networking device for accepting various
commands from the driver. It's a must to support multiqueue and other
configurations.

When used by vhost-vDPA bus driver for VM, the control virtqueue
should be shadowed via userspace VMM (Qemu) instead of being assigned
directly to Guest. This is because Qemu needs to know the device state
in order to start and stop device correctly (e.g for Live Migration).

This requies to isolate the memory mapping for control virtqueue
presented by vhost-vDPA to prevent guest from accesing it directly.

To achieve this, vDPA introduce two new abstractions:

- address space: identified through address space id (ASID) and a set
                 of memory mapping in maintained
- virtqueue group: the minimal set of virtqueues that must share an
                 address space

Device needs to advertise the following attributes to vDPA:

- the number of address spaces supported in the device
- the number of virtqueue groups supported in the device
- the mappings from a specific virtqueue to its virtqueue groups

The mappings from virtqueue to virtqueue groups is fixed and defined
by vDPA device driver. E.g:

- For the device that has hardware ASID support, it can simply
  advertise a per virtqueue virtqueue group.
- For the device that does not have hardware ASID support, it can
  simply advertise a single virtqueue group that contains all
  virtqueues. Or if it wants a software emulated control virtqueue, it
  can advertise two virtqueue groups, one is for cvq, another is for
  the rest virtqueues.

vDPA also allow to change the association between virtqueue group and
address space. So in the case of control virtqueue, userspace
VMM(Qemu) may use a dedicated address space for the control virtqueue
group to isolate the memory mapping.

The vhost/vhost-vDPA is also extend for the userspace to:

- query the number of virtqueue groups and address spaces supported by
  the device
- query the virtqueue group for a specific virtqueue
- assocaite a virtqueue group with an address space
- send ASID based IOTLB commands

This will help userspace VMM(Qemu) to detect whether the control vq
could be supported and isolate memory mappings of control virtqueue
from the others.

To demonstrate the usage, vDPA simulator is extended to support
setting MAC address via a emulated control virtqueue.

Please review.

Changes since RFC:

- tweak vhost uAPI documentation
- switch to use device specific IOTLB really in patch 4
- tweak the commit log
- fix that ASID in vhost is claimed to be 32 actually but 16bit
  actually
- fix use after free when using ASID with IOTLB batching requests
- switch to use Stefano's patch for having separated iov
- remove unused "used_as" variable
- fix the iotlb/asid checking in vhost_vdpa_unmap()

Thanks

Jason Wang (20):
  vhost: move the backend feature bits to vhost_types.h
  virtio-vdpa: don't set callback if virtio doesn't need it
  vhost-vdpa: passing iotlb to IOMMU mapping helpers
  vhost-vdpa: switch to use vhost-vdpa specific IOTLB
  vdpa: add the missing comment for nvqs in struct vdpa_device
  vdpa: introduce virtqueue groups
  vdpa: multiple address spaces support
  vdpa: introduce config operations for associating ASID to a virtqueue
    group
  vhost_iotlb: split out IOTLB initialization
  vhost: support ASID in IOTLB API
  vhost-vdpa: introduce asid based IOTLB
  vhost-vdpa: introduce uAPI to get the number of virtqueue groups
  vhost-vdpa: introduce uAPI to get the number of address spaces
  vhost-vdpa: uAPI to get virtqueue group id
  vhost-vdpa: introduce uAPI to set group ASID
  vhost-vdpa: support ASID based IOTLB API
  vdpa_sim: advertise VIRTIO_NET_F_MTU
  vdpa_sim: factor out buffer completion logic
  vdpa_sim: filter destination mac address
  vdpasim: control virtqueue support

Stefano Garzarella (1):
  vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov

 drivers/vdpa/ifcvf/ifcvf_main.c   |   9 +-
 drivers/vdpa/mlx5/net/mlx5_vnet.c |  11 +-
 drivers/vdpa/vdpa.c               |   8 +-
 drivers/vdpa/vdpa_sim/vdpa_sim.c  | 292 ++++++++++++++++++++++++------
 drivers/vhost/iotlb.c             |  23 ++-
 drivers/vhost/vdpa.c              | 246 ++++++++++++++++++++-----
 drivers/vhost/vhost.c             |  23 ++-
 drivers/vhost/vhost.h             |   4 +-
 drivers/virtio/virtio_vdpa.c      |   2 +-
 include/linux/vdpa.h              |  42 ++++-
 include/linux/vhost_iotlb.h       |   2 +
 include/uapi/linux/vhost.h        |  25 ++-
 include/uapi/linux/vhost_types.h  |  10 +-
 13 files changed, 561 insertions(+), 136 deletions(-)

-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* [PATCH 01/21] vhost: move the backend feature bits to vhost_types.h
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:47   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:47 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

We should store feature bits in vhost_types.h as what has been done
for e.g VHOST_F_LOG_ALL.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 include/uapi/linux/vhost.h       | 5 -----
 include/uapi/linux/vhost_types.h | 5 +++++
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index c998860d7bbc..59c6c0fbaba1 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -89,11 +89,6 @@
 
 /* Set or get vhost backend capability */
 
-/* Use message type V2 */
-#define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1
-/* IOTLB can accept batching hints */
-#define VHOST_BACKEND_F_IOTLB_BATCH  0x2
-
 #define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64)
 #define VHOST_GET_BACKEND_FEATURES _IOR(VHOST_VIRTIO, 0x26, __u64)
 
diff --git a/include/uapi/linux/vhost_types.h b/include/uapi/linux/vhost_types.h
index f7f6a3a28977..76ee7016c501 100644
--- a/include/uapi/linux/vhost_types.h
+++ b/include/uapi/linux/vhost_types.h
@@ -153,4 +153,9 @@ struct vhost_vdpa_iova_range {
 /* vhost-net should add virtio_net_hdr for RX, and strip for TX packets. */
 #define VHOST_NET_F_VIRTIO_NET_HDR 27
 
+/* Use message type V2 */
+#define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1
+/* IOTLB can accept batching hints */
+#define VHOST_BACKEND_F_IOTLB_BATCH  0x2
+
 #endif
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 01/21] vhost: move the backend feature bits to vhost_types.h
@ 2020-12-16  6:47   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:47 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

We should store feature bits in vhost_types.h as what has been done
for e.g VHOST_F_LOG_ALL.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 include/uapi/linux/vhost.h       | 5 -----
 include/uapi/linux/vhost_types.h | 5 +++++
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index c998860d7bbc..59c6c0fbaba1 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -89,11 +89,6 @@
 
 /* Set or get vhost backend capability */
 
-/* Use message type V2 */
-#define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1
-/* IOTLB can accept batching hints */
-#define VHOST_BACKEND_F_IOTLB_BATCH  0x2
-
 #define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64)
 #define VHOST_GET_BACKEND_FEATURES _IOR(VHOST_VIRTIO, 0x26, __u64)
 
diff --git a/include/uapi/linux/vhost_types.h b/include/uapi/linux/vhost_types.h
index f7f6a3a28977..76ee7016c501 100644
--- a/include/uapi/linux/vhost_types.h
+++ b/include/uapi/linux/vhost_types.h
@@ -153,4 +153,9 @@ struct vhost_vdpa_iova_range {
 /* vhost-net should add virtio_net_hdr for RX, and strip for TX packets. */
 #define VHOST_NET_F_VIRTIO_NET_HDR 27
 
+/* Use message type V2 */
+#define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1
+/* IOTLB can accept batching hints */
+#define VHOST_BACKEND_F_IOTLB_BATCH  0x2
+
 #endif
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 02/21] virtio-vdpa: don't set callback if virtio doesn't need it
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:47   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:47 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

There's no need for setting callbacks for the driver that doesn't care
about that.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/virtio/virtio_vdpa.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/virtio/virtio_vdpa.c b/drivers/virtio/virtio_vdpa.c
index 4a9ddb44b2a7..af6ee677f319 100644
--- a/drivers/virtio/virtio_vdpa.c
+++ b/drivers/virtio/virtio_vdpa.c
@@ -175,7 +175,7 @@ virtio_vdpa_setup_vq(struct virtio_device *vdev, unsigned int index,
 	}
 
 	/* Setup virtqueue callback */
-	cb.callback = virtio_vdpa_virtqueue_cb;
+	cb.callback = callback ? virtio_vdpa_virtqueue_cb : NULL;
 	cb.private = info;
 	ops->set_vq_cb(vdpa, index, &cb);
 	ops->set_vq_num(vdpa, index, virtqueue_get_vring_size(vq));
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 02/21] virtio-vdpa: don't set callback if virtio doesn't need it
@ 2020-12-16  6:47   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:47 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

There's no need for setting callbacks for the driver that doesn't care
about that.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/virtio/virtio_vdpa.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/virtio/virtio_vdpa.c b/drivers/virtio/virtio_vdpa.c
index 4a9ddb44b2a7..af6ee677f319 100644
--- a/drivers/virtio/virtio_vdpa.c
+++ b/drivers/virtio/virtio_vdpa.c
@@ -175,7 +175,7 @@ virtio_vdpa_setup_vq(struct virtio_device *vdev, unsigned int index,
 	}
 
 	/* Setup virtqueue callback */
-	cb.callback = virtio_vdpa_virtqueue_cb;
+	cb.callback = callback ? virtio_vdpa_virtqueue_cb : NULL;
 	cb.private = info;
 	ops->set_vq_cb(vdpa, index, &cb);
 	ops->set_vq_num(vdpa, index, virtqueue_get_vring_size(vq));
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 03/21] vhost-vdpa: passing iotlb to IOMMU mapping helpers
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

To prepare for the ASID support for vhost-vdpa, try to pass IOTLB
object to dma helpers. No functional changes, it's just a preparation
for support multiple IOTLBs.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c | 41 +++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 29ed4173f04e..07f92d48c173 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -501,10 +501,11 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
 	return r;
 }
 
-static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v, u64 start, u64 last)
+static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
+				   struct vhost_iotlb *iotlb,
+				   u64 start, u64 last)
 {
 	struct vhost_dev *dev = &v->vdev;
-	struct vhost_iotlb *iotlb = dev->iotlb;
 	struct vhost_iotlb_map *map;
 	struct page *page;
 	unsigned long pfn, pinned;
@@ -526,8 +527,9 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v, u64 start, u64 last)
 static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v)
 {
 	struct vhost_dev *dev = &v->vdev;
+	struct vhost_iotlb *iotlb = dev->iotlb;
 
-	vhost_vdpa_iotlb_unmap(v, 0ULL, 0ULL - 1);
+	vhost_vdpa_iotlb_unmap(v, iotlb, 0ULL, 0ULL - 1);
 	kfree(dev->iotlb);
 	dev->iotlb = NULL;
 }
@@ -554,7 +556,7 @@ static int perm_to_iommu_flags(u32 perm)
 	return flags | IOMMU_CACHE;
 }
 
-static int vhost_vdpa_map(struct vhost_vdpa *v,
+static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
 			  u64 iova, u64 size, u64 pa, u32 perm)
 {
 	struct vhost_dev *dev = &v->vdev;
@@ -562,7 +564,7 @@ static int vhost_vdpa_map(struct vhost_vdpa *v,
 	const struct vdpa_config_ops *ops = vdpa->config;
 	int r = 0;
 
-	r = vhost_iotlb_add_range(dev->iotlb, iova, iova + size - 1,
+	r = vhost_iotlb_add_range(iotlb, iova, iova + size - 1,
 				  pa, perm);
 	if (r)
 		return r;
@@ -571,43 +573,44 @@ static int vhost_vdpa_map(struct vhost_vdpa *v,
 		r = ops->dma_map(vdpa, iova, size, pa, perm);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			r = ops->set_map(vdpa, dev->iotlb);
+			r = ops->set_map(vdpa, iotlb);
 	} else {
 		r = iommu_map(v->domain, iova, pa, size,
 			      perm_to_iommu_flags(perm));
 	}
 
 	if (r)
-		vhost_iotlb_del_range(dev->iotlb, iova, iova + size - 1);
+		vhost_iotlb_del_range(iotlb, iova, iova + size - 1);
 	else
 		atomic64_add(size >> PAGE_SHIFT, &dev->mm->pinned_vm);
 
 	return r;
 }
 
-static void vhost_vdpa_unmap(struct vhost_vdpa *v, u64 iova, u64 size)
+static void vhost_vdpa_unmap(struct vhost_vdpa *v,
+			     struct vhost_iotlb *iotlb,
+			     u64 iova, u64 size)
 {
-	struct vhost_dev *dev = &v->vdev;
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
 
-	vhost_vdpa_iotlb_unmap(v, iova, iova + size - 1);
+	vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
 
 	if (ops->dma_map) {
 		ops->dma_unmap(vdpa, iova, size);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			ops->set_map(vdpa, dev->iotlb);
+			ops->set_map(vdpa, iotlb);
 	} else {
 		iommu_unmap(v->domain, iova, size);
 	}
 }
 
 static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
+					   struct vhost_iotlb *iotlb,
 					   struct vhost_iotlb_msg *msg)
 {
 	struct vhost_dev *dev = &v->vdev;
-	struct vhost_iotlb *iotlb = dev->iotlb;
 	struct page **page_list;
 	unsigned long list_size = PAGE_SIZE / sizeof(struct page *);
 	unsigned int gup_flags = FOLL_LONGTERM;
@@ -676,7 +679,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
 			if (last_pfn && (this_pfn != last_pfn + 1)) {
 				/* Pin a contiguous chunk of memory */
 				csize = (last_pfn - map_pfn + 1) << PAGE_SHIFT;
-				ret = vhost_vdpa_map(v, iova, csize,
+				ret = vhost_vdpa_map(v, iotlb, iova, csize,
 						     map_pfn << PAGE_SHIFT,
 						     msg->perm);
 				if (ret) {
@@ -706,7 +709,8 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
 	}
 
 	/* Pin the rest chunk */
-	ret = vhost_vdpa_map(v, iova, (last_pfn - map_pfn + 1) << PAGE_SHIFT,
+	ret = vhost_vdpa_map(v, iotlb, iova,
+			     (last_pfn - map_pfn + 1) << PAGE_SHIFT,
 			     map_pfn << PAGE_SHIFT, msg->perm);
 out:
 	if (ret) {
@@ -726,7 +730,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
 			for (pfn = map_pfn; pfn <= last_pfn; pfn++)
 				unpin_user_page(pfn_to_page(pfn));
 		}
-		vhost_vdpa_unmap(v, msg->iova, msg->size);
+		vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
 	}
 unlock:
 	mmap_read_unlock(dev->mm);
@@ -741,6 +745,7 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
 	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
+	struct vhost_iotlb *iotlb = dev->iotlb;
 	int r = 0;
 
 	r = vhost_dev_check_owner(dev);
@@ -749,17 +754,17 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
 
 	switch (msg->type) {
 	case VHOST_IOTLB_UPDATE:
-		r = vhost_vdpa_process_iotlb_update(v, msg);
+		r = vhost_vdpa_process_iotlb_update(v, iotlb, msg);
 		break;
 	case VHOST_IOTLB_INVALIDATE:
-		vhost_vdpa_unmap(v, msg->iova, msg->size);
+		vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
 		break;
 	case VHOST_IOTLB_BATCH_BEGIN:
 		v->in_batch = true;
 		break;
 	case VHOST_IOTLB_BATCH_END:
 		if (v->in_batch && ops->set_map)
-			ops->set_map(vdpa, dev->iotlb);
+			ops->set_map(vdpa, iotlb);
 		v->in_batch = false;
 		break;
 	default:
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 03/21] vhost-vdpa: passing iotlb to IOMMU mapping helpers
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

To prepare for the ASID support for vhost-vdpa, try to pass IOTLB
object to dma helpers. No functional changes, it's just a preparation
for support multiple IOTLBs.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c | 41 +++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 29ed4173f04e..07f92d48c173 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -501,10 +501,11 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
 	return r;
 }
 
-static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v, u64 start, u64 last)
+static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
+				   struct vhost_iotlb *iotlb,
+				   u64 start, u64 last)
 {
 	struct vhost_dev *dev = &v->vdev;
-	struct vhost_iotlb *iotlb = dev->iotlb;
 	struct vhost_iotlb_map *map;
 	struct page *page;
 	unsigned long pfn, pinned;
@@ -526,8 +527,9 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v, u64 start, u64 last)
 static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v)
 {
 	struct vhost_dev *dev = &v->vdev;
+	struct vhost_iotlb *iotlb = dev->iotlb;
 
-	vhost_vdpa_iotlb_unmap(v, 0ULL, 0ULL - 1);
+	vhost_vdpa_iotlb_unmap(v, iotlb, 0ULL, 0ULL - 1);
 	kfree(dev->iotlb);
 	dev->iotlb = NULL;
 }
@@ -554,7 +556,7 @@ static int perm_to_iommu_flags(u32 perm)
 	return flags | IOMMU_CACHE;
 }
 
-static int vhost_vdpa_map(struct vhost_vdpa *v,
+static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
 			  u64 iova, u64 size, u64 pa, u32 perm)
 {
 	struct vhost_dev *dev = &v->vdev;
@@ -562,7 +564,7 @@ static int vhost_vdpa_map(struct vhost_vdpa *v,
 	const struct vdpa_config_ops *ops = vdpa->config;
 	int r = 0;
 
-	r = vhost_iotlb_add_range(dev->iotlb, iova, iova + size - 1,
+	r = vhost_iotlb_add_range(iotlb, iova, iova + size - 1,
 				  pa, perm);
 	if (r)
 		return r;
@@ -571,43 +573,44 @@ static int vhost_vdpa_map(struct vhost_vdpa *v,
 		r = ops->dma_map(vdpa, iova, size, pa, perm);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			r = ops->set_map(vdpa, dev->iotlb);
+			r = ops->set_map(vdpa, iotlb);
 	} else {
 		r = iommu_map(v->domain, iova, pa, size,
 			      perm_to_iommu_flags(perm));
 	}
 
 	if (r)
-		vhost_iotlb_del_range(dev->iotlb, iova, iova + size - 1);
+		vhost_iotlb_del_range(iotlb, iova, iova + size - 1);
 	else
 		atomic64_add(size >> PAGE_SHIFT, &dev->mm->pinned_vm);
 
 	return r;
 }
 
-static void vhost_vdpa_unmap(struct vhost_vdpa *v, u64 iova, u64 size)
+static void vhost_vdpa_unmap(struct vhost_vdpa *v,
+			     struct vhost_iotlb *iotlb,
+			     u64 iova, u64 size)
 {
-	struct vhost_dev *dev = &v->vdev;
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
 
-	vhost_vdpa_iotlb_unmap(v, iova, iova + size - 1);
+	vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
 
 	if (ops->dma_map) {
 		ops->dma_unmap(vdpa, iova, size);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			ops->set_map(vdpa, dev->iotlb);
+			ops->set_map(vdpa, iotlb);
 	} else {
 		iommu_unmap(v->domain, iova, size);
 	}
 }
 
 static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
+					   struct vhost_iotlb *iotlb,
 					   struct vhost_iotlb_msg *msg)
 {
 	struct vhost_dev *dev = &v->vdev;
-	struct vhost_iotlb *iotlb = dev->iotlb;
 	struct page **page_list;
 	unsigned long list_size = PAGE_SIZE / sizeof(struct page *);
 	unsigned int gup_flags = FOLL_LONGTERM;
@@ -676,7 +679,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
 			if (last_pfn && (this_pfn != last_pfn + 1)) {
 				/* Pin a contiguous chunk of memory */
 				csize = (last_pfn - map_pfn + 1) << PAGE_SHIFT;
-				ret = vhost_vdpa_map(v, iova, csize,
+				ret = vhost_vdpa_map(v, iotlb, iova, csize,
 						     map_pfn << PAGE_SHIFT,
 						     msg->perm);
 				if (ret) {
@@ -706,7 +709,8 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
 	}
 
 	/* Pin the rest chunk */
-	ret = vhost_vdpa_map(v, iova, (last_pfn - map_pfn + 1) << PAGE_SHIFT,
+	ret = vhost_vdpa_map(v, iotlb, iova,
+			     (last_pfn - map_pfn + 1) << PAGE_SHIFT,
 			     map_pfn << PAGE_SHIFT, msg->perm);
 out:
 	if (ret) {
@@ -726,7 +730,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
 			for (pfn = map_pfn; pfn <= last_pfn; pfn++)
 				unpin_user_page(pfn_to_page(pfn));
 		}
-		vhost_vdpa_unmap(v, msg->iova, msg->size);
+		vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
 	}
 unlock:
 	mmap_read_unlock(dev->mm);
@@ -741,6 +745,7 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
 	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
+	struct vhost_iotlb *iotlb = dev->iotlb;
 	int r = 0;
 
 	r = vhost_dev_check_owner(dev);
@@ -749,17 +754,17 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
 
 	switch (msg->type) {
 	case VHOST_IOTLB_UPDATE:
-		r = vhost_vdpa_process_iotlb_update(v, msg);
+		r = vhost_vdpa_process_iotlb_update(v, iotlb, msg);
 		break;
 	case VHOST_IOTLB_INVALIDATE:
-		vhost_vdpa_unmap(v, msg->iova, msg->size);
+		vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
 		break;
 	case VHOST_IOTLB_BATCH_BEGIN:
 		v->in_batch = true;
 		break;
 	case VHOST_IOTLB_BATCH_END:
 		if (v->in_batch && ops->set_map)
-			ops->set_map(vdpa, dev->iotlb);
+			ops->set_map(vdpa, iotlb);
 		v->in_batch = false;
 		break;
 	default:
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 04/21] vhost-vdpa: switch to use vhost-vdpa specific IOTLB
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

To ease the implementation of per group ASID support for vDPA
device. This patch switches to use a vhost-vdpa specific IOTLB to
avoid the unnecessary refactoring of the vhost core.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 07f92d48c173..9bcc03d4e68b 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -39,6 +39,7 @@ struct vhost_vdpa {
 	struct vhost_virtqueue *vqs;
 	struct completion completion;
 	struct vdpa_device *vdpa;
+	struct vhost_iotlb *iotlb;
 	struct device dev;
 	struct cdev cdev;
 	atomic_t opened;
@@ -526,12 +527,11 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
 
 static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v)
 {
-	struct vhost_dev *dev = &v->vdev;
-	struct vhost_iotlb *iotlb = dev->iotlb;
+	struct vhost_iotlb *iotlb = v->iotlb;
 
 	vhost_vdpa_iotlb_unmap(v, iotlb, 0ULL, 0ULL - 1);
-	kfree(dev->iotlb);
-	dev->iotlb = NULL;
+	kfree(v->iotlb);
+	v->iotlb = NULL;
 }
 
 static int perm_to_iommu_flags(u32 perm)
@@ -745,7 +745,7 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
 	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
-	struct vhost_iotlb *iotlb = dev->iotlb;
+	struct vhost_iotlb *iotlb = v->iotlb;
 	int r = 0;
 
 	r = vhost_dev_check_owner(dev);
@@ -883,15 +883,15 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 	vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
 		       vhost_vdpa_process_iotlb_msg);
 
-	dev->iotlb = vhost_iotlb_alloc(0, 0);
-	if (!dev->iotlb) {
+	v->iotlb = vhost_iotlb_alloc(0, 0);
+	if (!v->iotlb) {
 		r = -ENOMEM;
 		goto err_init_iotlb;
 	}
 
 	r = vhost_vdpa_alloc_domain(v);
 	if (r)
-		goto err_init_iotlb;
+		goto err_alloc_domain;
 
 	vhost_vdpa_set_iova_range(v);
 
@@ -899,6 +899,8 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 
 	return 0;
 
+err_alloc_domain:
+	vhost_vdpa_iotlb_free(v);
 err_init_iotlb:
 	vhost_dev_cleanup(&v->vdev);
 	kfree(vqs);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 04/21] vhost-vdpa: switch to use vhost-vdpa specific IOTLB
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

To ease the implementation of per group ASID support for vDPA
device. This patch switches to use a vhost-vdpa specific IOTLB to
avoid the unnecessary refactoring of the vhost core.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 07f92d48c173..9bcc03d4e68b 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -39,6 +39,7 @@ struct vhost_vdpa {
 	struct vhost_virtqueue *vqs;
 	struct completion completion;
 	struct vdpa_device *vdpa;
+	struct vhost_iotlb *iotlb;
 	struct device dev;
 	struct cdev cdev;
 	atomic_t opened;
@@ -526,12 +527,11 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
 
 static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v)
 {
-	struct vhost_dev *dev = &v->vdev;
-	struct vhost_iotlb *iotlb = dev->iotlb;
+	struct vhost_iotlb *iotlb = v->iotlb;
 
 	vhost_vdpa_iotlb_unmap(v, iotlb, 0ULL, 0ULL - 1);
-	kfree(dev->iotlb);
-	dev->iotlb = NULL;
+	kfree(v->iotlb);
+	v->iotlb = NULL;
 }
 
 static int perm_to_iommu_flags(u32 perm)
@@ -745,7 +745,7 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
 	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
-	struct vhost_iotlb *iotlb = dev->iotlb;
+	struct vhost_iotlb *iotlb = v->iotlb;
 	int r = 0;
 
 	r = vhost_dev_check_owner(dev);
@@ -883,15 +883,15 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 	vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
 		       vhost_vdpa_process_iotlb_msg);
 
-	dev->iotlb = vhost_iotlb_alloc(0, 0);
-	if (!dev->iotlb) {
+	v->iotlb = vhost_iotlb_alloc(0, 0);
+	if (!v->iotlb) {
 		r = -ENOMEM;
 		goto err_init_iotlb;
 	}
 
 	r = vhost_vdpa_alloc_domain(v);
 	if (r)
-		goto err_init_iotlb;
+		goto err_alloc_domain;
 
 	vhost_vdpa_set_iova_range(v);
 
@@ -899,6 +899,8 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 
 	return 0;
 
+err_alloc_domain:
+	vhost_vdpa_iotlb_free(v);
 err_init_iotlb:
 	vhost_dev_cleanup(&v->vdev);
 	kfree(vqs);
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 05/21] vdpa: add the missing comment for nvqs in struct vdpa_device
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 include/linux/vdpa.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
index 30bc7a7223bb..8ab8dcde705d 100644
--- a/include/linux/vdpa.h
+++ b/include/linux/vdpa.h
@@ -42,6 +42,7 @@ struct vdpa_vq_state {
  * @config: the configuration ops for this device.
  * @index: device index
  * @features_valid: were features initialized? for legacy guests
+ * @nvqs: the number of virtqueues
  */
 struct vdpa_device {
 	struct device dev;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 05/21] vdpa: add the missing comment for nvqs in struct vdpa_device
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 include/linux/vdpa.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
index 30bc7a7223bb..8ab8dcde705d 100644
--- a/include/linux/vdpa.h
+++ b/include/linux/vdpa.h
@@ -42,6 +42,7 @@ struct vdpa_vq_state {
  * @config: the configuration ops for this device.
  * @index: device index
  * @features_valid: were features initialized? for legacy guests
+ * @nvqs: the number of virtqueues
  */
 struct vdpa_device {
 	struct device dev;
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 06/21] vdpa: introduce virtqueue groups
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

This patch introduces virtqueue groups to vDPA device. The virtqueue
group is the minimal set of virtqueues that must share an address
space. And the adddress space identifier could only be attached to
a specific virtqueue group.

A new mandated bus operation is introduced to get the virtqueue group
ID for a specific virtqueue.

All the vDPA device drivers were converted to simply support a single
virtqueue group.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vdpa/ifcvf/ifcvf_main.c   |  9 ++++++++-
 drivers/vdpa/mlx5/net/mlx5_vnet.c |  8 +++++++-
 drivers/vdpa/vdpa.c               |  4 +++-
 drivers/vdpa/vdpa_sim/vdpa_sim.c  | 11 ++++++++++-
 include/linux/vdpa.h              | 12 +++++++++---
 5 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_main.c
index 8b4028556cb6..c629f4fcc738 100644
--- a/drivers/vdpa/ifcvf/ifcvf_main.c
+++ b/drivers/vdpa/ifcvf/ifcvf_main.c
@@ -332,6 +332,11 @@ static u32 ifcvf_vdpa_get_vq_align(struct vdpa_device *vdpa_dev)
 	return IFCVF_QUEUE_ALIGNMENT;
 }
 
+static u32 ifcvf_vdpa_get_vq_group(struct vdpa_device *vdpa, u16 idx)
+{
+	return 0;
+}
+
 static void ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev,
 				  unsigned int offset,
 				  void *buf, unsigned int len)
@@ -392,6 +397,7 @@ static const struct vdpa_config_ops ifc_vdpa_ops = {
 	.get_device_id	= ifcvf_vdpa_get_device_id,
 	.get_vendor_id	= ifcvf_vdpa_get_vendor_id,
 	.get_vq_align	= ifcvf_vdpa_get_vq_align,
+	.get_vq_group	= ifcvf_vdpa_get_vq_group,
 	.get_config	= ifcvf_vdpa_get_config,
 	.set_config	= ifcvf_vdpa_set_config,
 	.set_config_cb  = ifcvf_vdpa_set_config_cb,
@@ -439,7 +445,8 @@ static int ifcvf_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
 	adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa,
 				    dev, &ifc_vdpa_ops,
-				    IFCVF_MAX_QUEUE_PAIRS * 2);
+				    IFCVF_MAX_QUEUE_PAIRS * 2, 1);
+
 	if (adapter == NULL) {
 		IFCVF_ERR(pdev, "Failed to allocate vDPA structure");
 		return -ENOMEM;
diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c
index 1fa6fcac8299..719b52fcc547 100644
--- a/drivers/vdpa/mlx5/net/mlx5_vnet.c
+++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c
@@ -1436,6 +1436,11 @@ static u32 mlx5_vdpa_get_vq_align(struct vdpa_device *vdev)
 	return PAGE_SIZE;
 }
 
+static u32 mlx5_vdpa_get_vq_group(struct vdpa_device *vdpa, u16 idx)
+{
+	return 0;
+}
+
 enum { MLX5_VIRTIO_NET_F_GUEST_CSUM = 1 << 9,
 	MLX5_VIRTIO_NET_F_CSUM = 1 << 10,
 	MLX5_VIRTIO_NET_F_HOST_TSO6 = 1 << 11,
@@ -1854,6 +1859,7 @@ static const struct vdpa_config_ops mlx5_vdpa_ops = {
 	.get_vq_notification = mlx5_get_vq_notification,
 	.get_vq_irq = mlx5_get_vq_irq,
 	.get_vq_align = mlx5_vdpa_get_vq_align,
+	.get_vq_group = mlx5_vdpa_get_vq_group,
 	.get_features = mlx5_vdpa_get_features,
 	.set_features = mlx5_vdpa_set_features,
 	.set_config_cb = mlx5_vdpa_set_config_cb,
@@ -1941,7 +1947,7 @@ void *mlx5_vdpa_add_dev(struct mlx5_core_dev *mdev)
 	max_vqs = min_t(u32, max_vqs, MLX5_MAX_SUPPORTED_VQS);
 
 	ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
-				 2 * mlx5_vdpa_max_qps(max_vqs));
+				 2 * mlx5_vdpa_max_qps(max_vqs), 1);
 	if (IS_ERR(ndev))
 		return ndev;
 
diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c
index a69ffc991e13..46399746ec7c 100644
--- a/drivers/vdpa/vdpa.c
+++ b/drivers/vdpa/vdpa.c
@@ -62,6 +62,7 @@ static void vdpa_release_dev(struct device *d)
  * @parent: the parent device
  * @config: the bus operations that is supported by this device
  * @nvqs: number of virtqueues supported by this device
+ * @ngroups: number of groups supported by this device
  * @size: size of the parent structure that contains private data
  *
  * Driver should use vdpa_alloc_device() wrapper macro instead of
@@ -72,7 +73,7 @@ static void vdpa_release_dev(struct device *d)
  */
 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 					const struct vdpa_config_ops *config,
-					int nvqs,
+					int nvqs, unsigned int ngroups,
 					size_t size)
 {
 	struct vdpa_device *vdev;
@@ -100,6 +101,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 	vdev->config = config;
 	vdev->features_valid = false;
 	vdev->nvqs = nvqs;
+	vdev->ngroups = ngroups;
 
 	err = dev_set_name(&vdev->dev, "vdpa%u", vdev->index);
 	if (err)
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index 6a90fdb9cbfc..5d554b3cd152 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -80,6 +80,7 @@ struct vdpasim {
 	u32 status;
 	u32 generation;
 	u64 features;
+	u32 groups;
 	/* spinlock to synchronize iommu table */
 	spinlock_t iommu_lock;
 };
@@ -357,7 +358,8 @@ static struct vdpasim *vdpasim_create(void)
 	else
 		ops = &vdpasim_net_config_ops;
 
-	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, VDPASIM_VQ_NUM);
+	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
+				    VDPASIM_VQ_NUM, 1);
 	if (!vdpasim)
 		goto err_alloc;
 
@@ -496,6 +498,11 @@ static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
 	return VDPASIM_QUEUE_ALIGN;
 }
 
+static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
+{
+	return 0;
+}
+
 static u64 vdpasim_get_features(struct vdpa_device *vdpa)
 {
 	return vdpasim_features;
@@ -671,6 +678,7 @@ static const struct vdpa_config_ops vdpasim_net_config_ops = {
 	.set_vq_state           = vdpasim_set_vq_state,
 	.get_vq_state           = vdpasim_get_vq_state,
 	.get_vq_align           = vdpasim_get_vq_align,
+	.get_vq_group           = vdpasim_get_vq_group,
 	.get_features           = vdpasim_get_features,
 	.set_features           = vdpasim_set_features,
 	.set_config_cb          = vdpasim_set_config_cb,
@@ -698,6 +706,7 @@ static const struct vdpa_config_ops vdpasim_net_batch_config_ops = {
 	.set_vq_state           = vdpasim_set_vq_state,
 	.get_vq_state           = vdpasim_get_vq_state,
 	.get_vq_align           = vdpasim_get_vq_align,
+	.get_vq_group           = vdpasim_get_vq_group,
 	.get_features           = vdpasim_get_features,
 	.set_features           = vdpasim_set_features,
 	.set_config_cb          = vdpasim_set_config_cb,
diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
index 8ab8dcde705d..bfc6790b263e 100644
--- a/include/linux/vdpa.h
+++ b/include/linux/vdpa.h
@@ -51,6 +51,7 @@ struct vdpa_device {
 	unsigned int index;
 	bool features_valid;
 	int nvqs;
+	unsigned int ngroups;
 };
 
 /**
@@ -119,6 +120,10 @@ struct vdpa_iova_range {
  *				for the device
  *				@vdev: vdpa device
  *				Returns virtqueue algin requirement
+ * @get_vq_group:		Get the group id for a specific virtqueue
+ *				@vdev: vdpa device
+ *				@idx: virtqueue index
+ *				Returns u32: group id for this virtqueue
  * @get_features:		Get virtio features supported by the device
  *				@vdev: vdpa device
  *				Returns the virtio features support by the
@@ -217,6 +222,7 @@ struct vdpa_config_ops {
 
 	/* Device ops */
 	u32 (*get_vq_align)(struct vdpa_device *vdev);
+	u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx);
 	u64 (*get_features)(struct vdpa_device *vdev);
 	int (*set_features)(struct vdpa_device *vdev, u64 features);
 	void (*set_config_cb)(struct vdpa_device *vdev,
@@ -245,12 +251,12 @@ struct vdpa_config_ops {
 
 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 					const struct vdpa_config_ops *config,
-					int nvqs,
+					int nvqs, unsigned int ngroups,
 					size_t size);
 
-#define vdpa_alloc_device(dev_struct, member, parent, config, nvqs)   \
+#define vdpa_alloc_device(dev_struct, member, parent, config, nvqs, ngroups) \
 			  container_of(__vdpa_alloc_device( \
-				       parent, config, nvqs, \
+				       parent, config, nvqs, ngroups, \
 				       sizeof(dev_struct) + \
 				       BUILD_BUG_ON_ZERO(offsetof( \
 				       dev_struct, member))), \
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 06/21] vdpa: introduce virtqueue groups
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

This patch introduces virtqueue groups to vDPA device. The virtqueue
group is the minimal set of virtqueues that must share an address
space. And the adddress space identifier could only be attached to
a specific virtqueue group.

A new mandated bus operation is introduced to get the virtqueue group
ID for a specific virtqueue.

All the vDPA device drivers were converted to simply support a single
virtqueue group.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vdpa/ifcvf/ifcvf_main.c   |  9 ++++++++-
 drivers/vdpa/mlx5/net/mlx5_vnet.c |  8 +++++++-
 drivers/vdpa/vdpa.c               |  4 +++-
 drivers/vdpa/vdpa_sim/vdpa_sim.c  | 11 ++++++++++-
 include/linux/vdpa.h              | 12 +++++++++---
 5 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_main.c
index 8b4028556cb6..c629f4fcc738 100644
--- a/drivers/vdpa/ifcvf/ifcvf_main.c
+++ b/drivers/vdpa/ifcvf/ifcvf_main.c
@@ -332,6 +332,11 @@ static u32 ifcvf_vdpa_get_vq_align(struct vdpa_device *vdpa_dev)
 	return IFCVF_QUEUE_ALIGNMENT;
 }
 
+static u32 ifcvf_vdpa_get_vq_group(struct vdpa_device *vdpa, u16 idx)
+{
+	return 0;
+}
+
 static void ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev,
 				  unsigned int offset,
 				  void *buf, unsigned int len)
@@ -392,6 +397,7 @@ static const struct vdpa_config_ops ifc_vdpa_ops = {
 	.get_device_id	= ifcvf_vdpa_get_device_id,
 	.get_vendor_id	= ifcvf_vdpa_get_vendor_id,
 	.get_vq_align	= ifcvf_vdpa_get_vq_align,
+	.get_vq_group	= ifcvf_vdpa_get_vq_group,
 	.get_config	= ifcvf_vdpa_get_config,
 	.set_config	= ifcvf_vdpa_set_config,
 	.set_config_cb  = ifcvf_vdpa_set_config_cb,
@@ -439,7 +445,8 @@ static int ifcvf_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
 	adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa,
 				    dev, &ifc_vdpa_ops,
-				    IFCVF_MAX_QUEUE_PAIRS * 2);
+				    IFCVF_MAX_QUEUE_PAIRS * 2, 1);
+
 	if (adapter == NULL) {
 		IFCVF_ERR(pdev, "Failed to allocate vDPA structure");
 		return -ENOMEM;
diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c
index 1fa6fcac8299..719b52fcc547 100644
--- a/drivers/vdpa/mlx5/net/mlx5_vnet.c
+++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c
@@ -1436,6 +1436,11 @@ static u32 mlx5_vdpa_get_vq_align(struct vdpa_device *vdev)
 	return PAGE_SIZE;
 }
 
+static u32 mlx5_vdpa_get_vq_group(struct vdpa_device *vdpa, u16 idx)
+{
+	return 0;
+}
+
 enum { MLX5_VIRTIO_NET_F_GUEST_CSUM = 1 << 9,
 	MLX5_VIRTIO_NET_F_CSUM = 1 << 10,
 	MLX5_VIRTIO_NET_F_HOST_TSO6 = 1 << 11,
@@ -1854,6 +1859,7 @@ static const struct vdpa_config_ops mlx5_vdpa_ops = {
 	.get_vq_notification = mlx5_get_vq_notification,
 	.get_vq_irq = mlx5_get_vq_irq,
 	.get_vq_align = mlx5_vdpa_get_vq_align,
+	.get_vq_group = mlx5_vdpa_get_vq_group,
 	.get_features = mlx5_vdpa_get_features,
 	.set_features = mlx5_vdpa_set_features,
 	.set_config_cb = mlx5_vdpa_set_config_cb,
@@ -1941,7 +1947,7 @@ void *mlx5_vdpa_add_dev(struct mlx5_core_dev *mdev)
 	max_vqs = min_t(u32, max_vqs, MLX5_MAX_SUPPORTED_VQS);
 
 	ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
-				 2 * mlx5_vdpa_max_qps(max_vqs));
+				 2 * mlx5_vdpa_max_qps(max_vqs), 1);
 	if (IS_ERR(ndev))
 		return ndev;
 
diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c
index a69ffc991e13..46399746ec7c 100644
--- a/drivers/vdpa/vdpa.c
+++ b/drivers/vdpa/vdpa.c
@@ -62,6 +62,7 @@ static void vdpa_release_dev(struct device *d)
  * @parent: the parent device
  * @config: the bus operations that is supported by this device
  * @nvqs: number of virtqueues supported by this device
+ * @ngroups: number of groups supported by this device
  * @size: size of the parent structure that contains private data
  *
  * Driver should use vdpa_alloc_device() wrapper macro instead of
@@ -72,7 +73,7 @@ static void vdpa_release_dev(struct device *d)
  */
 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 					const struct vdpa_config_ops *config,
-					int nvqs,
+					int nvqs, unsigned int ngroups,
 					size_t size)
 {
 	struct vdpa_device *vdev;
@@ -100,6 +101,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 	vdev->config = config;
 	vdev->features_valid = false;
 	vdev->nvqs = nvqs;
+	vdev->ngroups = ngroups;
 
 	err = dev_set_name(&vdev->dev, "vdpa%u", vdev->index);
 	if (err)
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index 6a90fdb9cbfc..5d554b3cd152 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -80,6 +80,7 @@ struct vdpasim {
 	u32 status;
 	u32 generation;
 	u64 features;
+	u32 groups;
 	/* spinlock to synchronize iommu table */
 	spinlock_t iommu_lock;
 };
@@ -357,7 +358,8 @@ static struct vdpasim *vdpasim_create(void)
 	else
 		ops = &vdpasim_net_config_ops;
 
-	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, VDPASIM_VQ_NUM);
+	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
+				    VDPASIM_VQ_NUM, 1);
 	if (!vdpasim)
 		goto err_alloc;
 
@@ -496,6 +498,11 @@ static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
 	return VDPASIM_QUEUE_ALIGN;
 }
 
+static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
+{
+	return 0;
+}
+
 static u64 vdpasim_get_features(struct vdpa_device *vdpa)
 {
 	return vdpasim_features;
@@ -671,6 +678,7 @@ static const struct vdpa_config_ops vdpasim_net_config_ops = {
 	.set_vq_state           = vdpasim_set_vq_state,
 	.get_vq_state           = vdpasim_get_vq_state,
 	.get_vq_align           = vdpasim_get_vq_align,
+	.get_vq_group           = vdpasim_get_vq_group,
 	.get_features           = vdpasim_get_features,
 	.set_features           = vdpasim_set_features,
 	.set_config_cb          = vdpasim_set_config_cb,
@@ -698,6 +706,7 @@ static const struct vdpa_config_ops vdpasim_net_batch_config_ops = {
 	.set_vq_state           = vdpasim_set_vq_state,
 	.get_vq_state           = vdpasim_get_vq_state,
 	.get_vq_align           = vdpasim_get_vq_align,
+	.get_vq_group           = vdpasim_get_vq_group,
 	.get_features           = vdpasim_get_features,
 	.set_features           = vdpasim_set_features,
 	.set_config_cb          = vdpasim_set_config_cb,
diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
index 8ab8dcde705d..bfc6790b263e 100644
--- a/include/linux/vdpa.h
+++ b/include/linux/vdpa.h
@@ -51,6 +51,7 @@ struct vdpa_device {
 	unsigned int index;
 	bool features_valid;
 	int nvqs;
+	unsigned int ngroups;
 };
 
 /**
@@ -119,6 +120,10 @@ struct vdpa_iova_range {
  *				for the device
  *				@vdev: vdpa device
  *				Returns virtqueue algin requirement
+ * @get_vq_group:		Get the group id for a specific virtqueue
+ *				@vdev: vdpa device
+ *				@idx: virtqueue index
+ *				Returns u32: group id for this virtqueue
  * @get_features:		Get virtio features supported by the device
  *				@vdev: vdpa device
  *				Returns the virtio features support by the
@@ -217,6 +222,7 @@ struct vdpa_config_ops {
 
 	/* Device ops */
 	u32 (*get_vq_align)(struct vdpa_device *vdev);
+	u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx);
 	u64 (*get_features)(struct vdpa_device *vdev);
 	int (*set_features)(struct vdpa_device *vdev, u64 features);
 	void (*set_config_cb)(struct vdpa_device *vdev,
@@ -245,12 +251,12 @@ struct vdpa_config_ops {
 
 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 					const struct vdpa_config_ops *config,
-					int nvqs,
+					int nvqs, unsigned int ngroups,
 					size_t size);
 
-#define vdpa_alloc_device(dev_struct, member, parent, config, nvqs)   \
+#define vdpa_alloc_device(dev_struct, member, parent, config, nvqs, ngroups) \
 			  container_of(__vdpa_alloc_device( \
-				       parent, config, nvqs, \
+				       parent, config, nvqs, ngroups, \
 				       sizeof(dev_struct) + \
 				       BUILD_BUG_ON_ZERO(offsetof( \
 				       dev_struct, member))), \
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 07/21] vdpa: multiple address spaces support
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

This patches introduces the multiple address spaces support for vDPA
device. This idea is to identify a specific address space via an
dedicated identifier - ASID.

During vDPA device allocation, vDPA device driver needs to report the
number of address spaces supported by the device then the DMA mapping
ops of the vDPA device needs to be extended to support ASID.

This helps to isolate the environments for the virtqueue that will not
be assigned directly. E.g in the case of virtio-net, the control
virtqueue will not be assigned directly to guest.

As a start, simply claim 1 virtqueue groups and 1 address spaces for
all vDPA devices. And vhost-vDPA will simply reject the device with
more than 1 virtqueue groups or address spaces.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vdpa/ifcvf/ifcvf_main.c   |  2 +-
 drivers/vdpa/mlx5/net/mlx5_vnet.c |  5 +++--
 drivers/vdpa/vdpa.c               |  4 +++-
 drivers/vdpa/vdpa_sim/vdpa_sim.c  | 10 ++++++----
 drivers/vhost/vdpa.c              | 14 +++++++++-----
 include/linux/vdpa.h              | 23 ++++++++++++++++-------
 6 files changed, 38 insertions(+), 20 deletions(-)

diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_main.c
index c629f4fcc738..8a43f562b169 100644
--- a/drivers/vdpa/ifcvf/ifcvf_main.c
+++ b/drivers/vdpa/ifcvf/ifcvf_main.c
@@ -445,7 +445,7 @@ static int ifcvf_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
 	adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa,
 				    dev, &ifc_vdpa_ops,
-				    IFCVF_MAX_QUEUE_PAIRS * 2, 1);
+				    IFCVF_MAX_QUEUE_PAIRS * 2, 1, 1);
 
 	if (adapter == NULL) {
 		IFCVF_ERR(pdev, "Failed to allocate vDPA structure");
diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c
index 719b52fcc547..7aaf0a4ee80d 100644
--- a/drivers/vdpa/mlx5/net/mlx5_vnet.c
+++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c
@@ -1804,7 +1804,8 @@ static u32 mlx5_vdpa_get_generation(struct vdpa_device *vdev)
 	return mvdev->generation;
 }
 
-static int mlx5_vdpa_set_map(struct vdpa_device *vdev, struct vhost_iotlb *iotlb)
+static int mlx5_vdpa_set_map(struct vdpa_device *vdev, unsigned int asid,
+			     struct vhost_iotlb *iotlb)
 {
 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
@@ -1947,7 +1948,7 @@ void *mlx5_vdpa_add_dev(struct mlx5_core_dev *mdev)
 	max_vqs = min_t(u32, max_vqs, MLX5_MAX_SUPPORTED_VQS);
 
 	ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
-				 2 * mlx5_vdpa_max_qps(max_vqs), 1);
+				 2 * mlx5_vdpa_max_qps(max_vqs), 1, 1);
 	if (IS_ERR(ndev))
 		return ndev;
 
diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c
index 46399746ec7c..05195fa7865d 100644
--- a/drivers/vdpa/vdpa.c
+++ b/drivers/vdpa/vdpa.c
@@ -63,6 +63,7 @@ static void vdpa_release_dev(struct device *d)
  * @config: the bus operations that is supported by this device
  * @nvqs: number of virtqueues supported by this device
  * @ngroups: number of groups supported by this device
+ * @nas: number of address spaces supported by this device
  * @size: size of the parent structure that contains private data
  *
  * Driver should use vdpa_alloc_device() wrapper macro instead of
@@ -74,7 +75,7 @@ static void vdpa_release_dev(struct device *d)
 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 					const struct vdpa_config_ops *config,
 					int nvqs, unsigned int ngroups,
-					size_t size)
+					unsigned int nas, size_t size)
 {
 	struct vdpa_device *vdev;
 	int err = -EINVAL;
@@ -102,6 +103,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 	vdev->features_valid = false;
 	vdev->nvqs = nvqs;
 	vdev->ngroups = ngroups;
+	vdev->nas = nas;
 
 	err = dev_set_name(&vdev->dev, "vdpa%u", vdev->index);
 	if (err)
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index 5d554b3cd152..140de45ffff2 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -359,7 +359,7 @@ static struct vdpasim *vdpasim_create(void)
 		ops = &vdpasim_net_config_ops;
 
 	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
-				    VDPASIM_VQ_NUM, 1);
+				    VDPASIM_VQ_NUM, 1, 1);
 	if (!vdpasim)
 		goto err_alloc;
 
@@ -606,7 +606,7 @@ static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
 	return range;
 }
 
-static int vdpasim_set_map(struct vdpa_device *vdpa,
+static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
 			   struct vhost_iotlb *iotlb)
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
@@ -633,7 +633,8 @@ static int vdpasim_set_map(struct vdpa_device *vdpa,
 	return ret;
 }
 
-static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
+static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
+			   u64 iova, u64 size,
 			   u64 pa, u32 perm)
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
@@ -647,7 +648,8 @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
 	return ret;
 }
 
-static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
+static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
+			     u64 iova, u64 size)
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 
diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 9bcc03d4e68b..03a9b3311c6c 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -570,10 +570,10 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
 		return r;
 
 	if (ops->dma_map) {
-		r = ops->dma_map(vdpa, iova, size, pa, perm);
+		r = ops->dma_map(vdpa, 0, iova, size, pa, perm);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			r = ops->set_map(vdpa, iotlb);
+			r = ops->set_map(vdpa, 0, iotlb);
 	} else {
 		r = iommu_map(v->domain, iova, pa, size,
 			      perm_to_iommu_flags(perm));
@@ -597,10 +597,10 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
 	vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
 
 	if (ops->dma_map) {
-		ops->dma_unmap(vdpa, iova, size);
+		ops->dma_unmap(vdpa, 0, iova, size);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			ops->set_map(vdpa, iotlb);
+			ops->set_map(vdpa, 0, iotlb);
 	} else {
 		iommu_unmap(v->domain, iova, size);
 	}
@@ -764,7 +764,7 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
 		break;
 	case VHOST_IOTLB_BATCH_END:
 		if (v->in_batch && ops->set_map)
-			ops->set_map(vdpa, iotlb);
+			ops->set_map(vdpa, 0, iotlb);
 		v->in_batch = false;
 		break;
 	default:
@@ -1032,6 +1032,10 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
 	int minor;
 	int r;
 
+	/* Only support 1 address space and 1 groups */
+	if (vdpa->ngroups != 1 || vdpa->nas != 1)
+		return -ENOTSUPP;
+
 	/* Currently, we only accept the network devices. */
 	if (ops->get_device_id(vdpa) != VIRTIO_ID_NET)
 		return -ENOTSUPP;
diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
index bfc6790b263e..0a9a754f8180 100644
--- a/include/linux/vdpa.h
+++ b/include/linux/vdpa.h
@@ -43,6 +43,8 @@ struct vdpa_vq_state {
  * @index: device index
  * @features_valid: were features initialized? for legacy guests
  * @nvqs: the number of virtqueues
+ * @ngroups: the number of virtqueue groups
+ * @nas: the number of address spaces
  */
 struct vdpa_device {
 	struct device dev;
@@ -52,6 +54,7 @@ struct vdpa_device {
 	bool features_valid;
 	int nvqs;
 	unsigned int ngroups;
+	unsigned int nas;
 };
 
 /**
@@ -175,6 +178,7 @@ struct vdpa_iova_range {
  *				Needed for device that using device
  *				specific DMA translation (on-chip IOMMU)
  *				@vdev: vdpa device
+ *				@asid: address space identifier
  *				@iotlb: vhost memory mapping to be
  *				used by the vDPA
  *				Returns integer: success (0) or error (< 0)
@@ -183,6 +187,7 @@ struct vdpa_iova_range {
  *				specific DMA translation (on-chip IOMMU)
  *				and preferring incremental map.
  *				@vdev: vdpa device
+ *				@asid: address space identifier
  *				@iova: iova to be mapped
  *				@size: size of the area
  *				@pa: physical address for the map
@@ -194,6 +199,7 @@ struct vdpa_iova_range {
  *				specific DMA translation (on-chip IOMMU)
  *				and preferring incremental unmap.
  *				@vdev: vdpa device
+ *				@asid: address space identifier
  *				@iova: iova to be unmapped
  *				@size: size of the area
  *				Returns integer: success (0) or error (< 0)
@@ -240,10 +246,12 @@ struct vdpa_config_ops {
 	struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
 
 	/* DMA ops */
-	int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
-	int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
-		       u64 pa, u32 perm);
-	int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
+	int (*set_map)(struct vdpa_device *vdev, unsigned int asid,
+		       struct vhost_iotlb *iotlb);
+	int (*dma_map)(struct vdpa_device *vdev, unsigned int asid,
+		       u64 iova, u64 size, u64 pa, u32 perm);
+	int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
+			 u64 iova, u64 size);
 
 	/* Free device resources */
 	void (*free)(struct vdpa_device *vdev);
@@ -252,11 +260,12 @@ struct vdpa_config_ops {
 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 					const struct vdpa_config_ops *config,
 					int nvqs, unsigned int ngroups,
-					size_t size);
+					unsigned int nas, size_t size);
 
-#define vdpa_alloc_device(dev_struct, member, parent, config, nvqs, ngroups) \
+#define vdpa_alloc_device(dev_struct, member, parent, config, nvqs, \
+			  ngroups, nas)				    \
 			  container_of(__vdpa_alloc_device( \
-				       parent, config, nvqs, ngroups, \
+				       parent, config, nvqs, ngroups, nas,  \
 				       sizeof(dev_struct) + \
 				       BUILD_BUG_ON_ZERO(offsetof( \
 				       dev_struct, member))), \
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 07/21] vdpa: multiple address spaces support
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

This patches introduces the multiple address spaces support for vDPA
device. This idea is to identify a specific address space via an
dedicated identifier - ASID.

During vDPA device allocation, vDPA device driver needs to report the
number of address spaces supported by the device then the DMA mapping
ops of the vDPA device needs to be extended to support ASID.

This helps to isolate the environments for the virtqueue that will not
be assigned directly. E.g in the case of virtio-net, the control
virtqueue will not be assigned directly to guest.

As a start, simply claim 1 virtqueue groups and 1 address spaces for
all vDPA devices. And vhost-vDPA will simply reject the device with
more than 1 virtqueue groups or address spaces.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vdpa/ifcvf/ifcvf_main.c   |  2 +-
 drivers/vdpa/mlx5/net/mlx5_vnet.c |  5 +++--
 drivers/vdpa/vdpa.c               |  4 +++-
 drivers/vdpa/vdpa_sim/vdpa_sim.c  | 10 ++++++----
 drivers/vhost/vdpa.c              | 14 +++++++++-----
 include/linux/vdpa.h              | 23 ++++++++++++++++-------
 6 files changed, 38 insertions(+), 20 deletions(-)

diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_main.c
index c629f4fcc738..8a43f562b169 100644
--- a/drivers/vdpa/ifcvf/ifcvf_main.c
+++ b/drivers/vdpa/ifcvf/ifcvf_main.c
@@ -445,7 +445,7 @@ static int ifcvf_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
 	adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa,
 				    dev, &ifc_vdpa_ops,
-				    IFCVF_MAX_QUEUE_PAIRS * 2, 1);
+				    IFCVF_MAX_QUEUE_PAIRS * 2, 1, 1);
 
 	if (adapter == NULL) {
 		IFCVF_ERR(pdev, "Failed to allocate vDPA structure");
diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c
index 719b52fcc547..7aaf0a4ee80d 100644
--- a/drivers/vdpa/mlx5/net/mlx5_vnet.c
+++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c
@@ -1804,7 +1804,8 @@ static u32 mlx5_vdpa_get_generation(struct vdpa_device *vdev)
 	return mvdev->generation;
 }
 
-static int mlx5_vdpa_set_map(struct vdpa_device *vdev, struct vhost_iotlb *iotlb)
+static int mlx5_vdpa_set_map(struct vdpa_device *vdev, unsigned int asid,
+			     struct vhost_iotlb *iotlb)
 {
 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
@@ -1947,7 +1948,7 @@ void *mlx5_vdpa_add_dev(struct mlx5_core_dev *mdev)
 	max_vqs = min_t(u32, max_vqs, MLX5_MAX_SUPPORTED_VQS);
 
 	ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
-				 2 * mlx5_vdpa_max_qps(max_vqs), 1);
+				 2 * mlx5_vdpa_max_qps(max_vqs), 1, 1);
 	if (IS_ERR(ndev))
 		return ndev;
 
diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c
index 46399746ec7c..05195fa7865d 100644
--- a/drivers/vdpa/vdpa.c
+++ b/drivers/vdpa/vdpa.c
@@ -63,6 +63,7 @@ static void vdpa_release_dev(struct device *d)
  * @config: the bus operations that is supported by this device
  * @nvqs: number of virtqueues supported by this device
  * @ngroups: number of groups supported by this device
+ * @nas: number of address spaces supported by this device
  * @size: size of the parent structure that contains private data
  *
  * Driver should use vdpa_alloc_device() wrapper macro instead of
@@ -74,7 +75,7 @@ static void vdpa_release_dev(struct device *d)
 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 					const struct vdpa_config_ops *config,
 					int nvqs, unsigned int ngroups,
-					size_t size)
+					unsigned int nas, size_t size)
 {
 	struct vdpa_device *vdev;
 	int err = -EINVAL;
@@ -102,6 +103,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 	vdev->features_valid = false;
 	vdev->nvqs = nvqs;
 	vdev->ngroups = ngroups;
+	vdev->nas = nas;
 
 	err = dev_set_name(&vdev->dev, "vdpa%u", vdev->index);
 	if (err)
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index 5d554b3cd152..140de45ffff2 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -359,7 +359,7 @@ static struct vdpasim *vdpasim_create(void)
 		ops = &vdpasim_net_config_ops;
 
 	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
-				    VDPASIM_VQ_NUM, 1);
+				    VDPASIM_VQ_NUM, 1, 1);
 	if (!vdpasim)
 		goto err_alloc;
 
@@ -606,7 +606,7 @@ static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
 	return range;
 }
 
-static int vdpasim_set_map(struct vdpa_device *vdpa,
+static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
 			   struct vhost_iotlb *iotlb)
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
@@ -633,7 +633,8 @@ static int vdpasim_set_map(struct vdpa_device *vdpa,
 	return ret;
 }
 
-static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
+static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
+			   u64 iova, u64 size,
 			   u64 pa, u32 perm)
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
@@ -647,7 +648,8 @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
 	return ret;
 }
 
-static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
+static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
+			     u64 iova, u64 size)
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 
diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 9bcc03d4e68b..03a9b3311c6c 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -570,10 +570,10 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
 		return r;
 
 	if (ops->dma_map) {
-		r = ops->dma_map(vdpa, iova, size, pa, perm);
+		r = ops->dma_map(vdpa, 0, iova, size, pa, perm);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			r = ops->set_map(vdpa, iotlb);
+			r = ops->set_map(vdpa, 0, iotlb);
 	} else {
 		r = iommu_map(v->domain, iova, pa, size,
 			      perm_to_iommu_flags(perm));
@@ -597,10 +597,10 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
 	vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
 
 	if (ops->dma_map) {
-		ops->dma_unmap(vdpa, iova, size);
+		ops->dma_unmap(vdpa, 0, iova, size);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			ops->set_map(vdpa, iotlb);
+			ops->set_map(vdpa, 0, iotlb);
 	} else {
 		iommu_unmap(v->domain, iova, size);
 	}
@@ -764,7 +764,7 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
 		break;
 	case VHOST_IOTLB_BATCH_END:
 		if (v->in_batch && ops->set_map)
-			ops->set_map(vdpa, iotlb);
+			ops->set_map(vdpa, 0, iotlb);
 		v->in_batch = false;
 		break;
 	default:
@@ -1032,6 +1032,10 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
 	int minor;
 	int r;
 
+	/* Only support 1 address space and 1 groups */
+	if (vdpa->ngroups != 1 || vdpa->nas != 1)
+		return -ENOTSUPP;
+
 	/* Currently, we only accept the network devices. */
 	if (ops->get_device_id(vdpa) != VIRTIO_ID_NET)
 		return -ENOTSUPP;
diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
index bfc6790b263e..0a9a754f8180 100644
--- a/include/linux/vdpa.h
+++ b/include/linux/vdpa.h
@@ -43,6 +43,8 @@ struct vdpa_vq_state {
  * @index: device index
  * @features_valid: were features initialized? for legacy guests
  * @nvqs: the number of virtqueues
+ * @ngroups: the number of virtqueue groups
+ * @nas: the number of address spaces
  */
 struct vdpa_device {
 	struct device dev;
@@ -52,6 +54,7 @@ struct vdpa_device {
 	bool features_valid;
 	int nvqs;
 	unsigned int ngroups;
+	unsigned int nas;
 };
 
 /**
@@ -175,6 +178,7 @@ struct vdpa_iova_range {
  *				Needed for device that using device
  *				specific DMA translation (on-chip IOMMU)
  *				@vdev: vdpa device
+ *				@asid: address space identifier
  *				@iotlb: vhost memory mapping to be
  *				used by the vDPA
  *				Returns integer: success (0) or error (< 0)
@@ -183,6 +187,7 @@ struct vdpa_iova_range {
  *				specific DMA translation (on-chip IOMMU)
  *				and preferring incremental map.
  *				@vdev: vdpa device
+ *				@asid: address space identifier
  *				@iova: iova to be mapped
  *				@size: size of the area
  *				@pa: physical address for the map
@@ -194,6 +199,7 @@ struct vdpa_iova_range {
  *				specific DMA translation (on-chip IOMMU)
  *				and preferring incremental unmap.
  *				@vdev: vdpa device
+ *				@asid: address space identifier
  *				@iova: iova to be unmapped
  *				@size: size of the area
  *				Returns integer: success (0) or error (< 0)
@@ -240,10 +246,12 @@ struct vdpa_config_ops {
 	struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
 
 	/* DMA ops */
-	int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
-	int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
-		       u64 pa, u32 perm);
-	int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
+	int (*set_map)(struct vdpa_device *vdev, unsigned int asid,
+		       struct vhost_iotlb *iotlb);
+	int (*dma_map)(struct vdpa_device *vdev, unsigned int asid,
+		       u64 iova, u64 size, u64 pa, u32 perm);
+	int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
+			 u64 iova, u64 size);
 
 	/* Free device resources */
 	void (*free)(struct vdpa_device *vdev);
@@ -252,11 +260,12 @@ struct vdpa_config_ops {
 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 					const struct vdpa_config_ops *config,
 					int nvqs, unsigned int ngroups,
-					size_t size);
+					unsigned int nas, size_t size);
 
-#define vdpa_alloc_device(dev_struct, member, parent, config, nvqs, ngroups) \
+#define vdpa_alloc_device(dev_struct, member, parent, config, nvqs, \
+			  ngroups, nas)				    \
 			  container_of(__vdpa_alloc_device( \
-				       parent, config, nvqs, ngroups, \
+				       parent, config, nvqs, ngroups, nas,  \
 				       sizeof(dev_struct) + \
 				       BUILD_BUG_ON_ZERO(offsetof( \
 				       dev_struct, member))), \
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 08/21] vdpa: introduce config operations for associating ASID to a virtqueue group
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

This patch introduces a new bus operation to allow the vDPA bus driver
to associate an ASID to a virtqueue group.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 include/linux/vdpa.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
index 0a9a754f8180..2a8671f27b0b 100644
--- a/include/linux/vdpa.h
+++ b/include/linux/vdpa.h
@@ -174,6 +174,12 @@ struct vdpa_iova_range {
  *				@vdev: vdpa device
  *				Returns the iova range supported by
  *				the device.
+ * @set_group_asid:		Set address space identifier for a
+ *				virtqueue group
+ *				@vdev: vdpa device
+ *				@group: virtqueue group
+ *				@asid: address space id for this group
+ *				Returns integer: success (0) or error (< 0)
  * @set_map:			Set device memory mapping (optional)
  *				Needed for device that using device
  *				specific DMA translation (on-chip IOMMU)
@@ -252,6 +258,10 @@ struct vdpa_config_ops {
 		       u64 iova, u64 size, u64 pa, u32 perm);
 	int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
 			 u64 iova, u64 size);
+	int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group,
+			      unsigned int asid);
+
+
 
 	/* Free device resources */
 	void (*free)(struct vdpa_device *vdev);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 08/21] vdpa: introduce config operations for associating ASID to a virtqueue group
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

This patch introduces a new bus operation to allow the vDPA bus driver
to associate an ASID to a virtqueue group.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 include/linux/vdpa.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
index 0a9a754f8180..2a8671f27b0b 100644
--- a/include/linux/vdpa.h
+++ b/include/linux/vdpa.h
@@ -174,6 +174,12 @@ struct vdpa_iova_range {
  *				@vdev: vdpa device
  *				Returns the iova range supported by
  *				the device.
+ * @set_group_asid:		Set address space identifier for a
+ *				virtqueue group
+ *				@vdev: vdpa device
+ *				@group: virtqueue group
+ *				@asid: address space id for this group
+ *				Returns integer: success (0) or error (< 0)
  * @set_map:			Set device memory mapping (optional)
  *				Needed for device that using device
  *				specific DMA translation (on-chip IOMMU)
@@ -252,6 +258,10 @@ struct vdpa_config_ops {
 		       u64 iova, u64 size, u64 pa, u32 perm);
 	int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
 			 u64 iova, u64 size);
+	int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group,
+			      unsigned int asid);
+
+
 
 	/* Free device resources */
 	void (*free)(struct vdpa_device *vdev);
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 09/21] vhost_iotlb: split out IOTLB initialization
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

This patch splits out IOTLB initialization to make sure it could be
reused by external modules.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/iotlb.c       | 23 ++++++++++++++++++-----
 include/linux/vhost_iotlb.h |  2 ++
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/drivers/vhost/iotlb.c b/drivers/vhost/iotlb.c
index 0fd3f87e913c..e842d76c179e 100644
--- a/drivers/vhost/iotlb.c
+++ b/drivers/vhost/iotlb.c
@@ -98,6 +98,23 @@ void vhost_iotlb_del_range(struct vhost_iotlb *iotlb, u64 start, u64 last)
 }
 EXPORT_SYMBOL_GPL(vhost_iotlb_del_range);
 
+/**
+ * vhost_iotlb_init - initialize a vhost IOTLB
+ * @iotlb: the IOTLB that needs to be initialized
+ * @limit: maximum number of IOTLB entries
+ * @flags: VHOST_IOTLB_FLAG_XXX
+ */
+void vhost_iotlb_init(struct vhost_iotlb *iotlb, unsigned int limit,
+		      unsigned int flags)
+{
+	iotlb->root = RB_ROOT_CACHED;
+	iotlb->limit = limit;
+	iotlb->nmaps = 0;
+	iotlb->flags = flags;
+	INIT_LIST_HEAD(&iotlb->list);
+}
+EXPORT_SYMBOL_GPL(vhost_iotlb_init);
+
 /**
  * vhost_iotlb_alloc - add a new vhost IOTLB
  * @limit: maximum number of IOTLB entries
@@ -112,11 +129,7 @@ struct vhost_iotlb *vhost_iotlb_alloc(unsigned int limit, unsigned int flags)
 	if (!iotlb)
 		return NULL;
 
-	iotlb->root = RB_ROOT_CACHED;
-	iotlb->limit = limit;
-	iotlb->nmaps = 0;
-	iotlb->flags = flags;
-	INIT_LIST_HEAD(&iotlb->list);
+	vhost_iotlb_init(iotlb, limit, flags);
 
 	return iotlb;
 }
diff --git a/include/linux/vhost_iotlb.h b/include/linux/vhost_iotlb.h
index 6b09b786a762..c0df193ec3e1 100644
--- a/include/linux/vhost_iotlb.h
+++ b/include/linux/vhost_iotlb.h
@@ -33,6 +33,8 @@ int vhost_iotlb_add_range(struct vhost_iotlb *iotlb, u64 start, u64 last,
 			  u64 addr, unsigned int perm);
 void vhost_iotlb_del_range(struct vhost_iotlb *iotlb, u64 start, u64 last);
 
+void vhost_iotlb_init(struct vhost_iotlb *iotlb, unsigned int limit,
+		      unsigned int flags);
 struct vhost_iotlb *vhost_iotlb_alloc(unsigned int limit, unsigned int flags);
 void vhost_iotlb_free(struct vhost_iotlb *iotlb);
 void vhost_iotlb_reset(struct vhost_iotlb *iotlb);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 09/21] vhost_iotlb: split out IOTLB initialization
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

This patch splits out IOTLB initialization to make sure it could be
reused by external modules.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/iotlb.c       | 23 ++++++++++++++++++-----
 include/linux/vhost_iotlb.h |  2 ++
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/drivers/vhost/iotlb.c b/drivers/vhost/iotlb.c
index 0fd3f87e913c..e842d76c179e 100644
--- a/drivers/vhost/iotlb.c
+++ b/drivers/vhost/iotlb.c
@@ -98,6 +98,23 @@ void vhost_iotlb_del_range(struct vhost_iotlb *iotlb, u64 start, u64 last)
 }
 EXPORT_SYMBOL_GPL(vhost_iotlb_del_range);
 
+/**
+ * vhost_iotlb_init - initialize a vhost IOTLB
+ * @iotlb: the IOTLB that needs to be initialized
+ * @limit: maximum number of IOTLB entries
+ * @flags: VHOST_IOTLB_FLAG_XXX
+ */
+void vhost_iotlb_init(struct vhost_iotlb *iotlb, unsigned int limit,
+		      unsigned int flags)
+{
+	iotlb->root = RB_ROOT_CACHED;
+	iotlb->limit = limit;
+	iotlb->nmaps = 0;
+	iotlb->flags = flags;
+	INIT_LIST_HEAD(&iotlb->list);
+}
+EXPORT_SYMBOL_GPL(vhost_iotlb_init);
+
 /**
  * vhost_iotlb_alloc - add a new vhost IOTLB
  * @limit: maximum number of IOTLB entries
@@ -112,11 +129,7 @@ struct vhost_iotlb *vhost_iotlb_alloc(unsigned int limit, unsigned int flags)
 	if (!iotlb)
 		return NULL;
 
-	iotlb->root = RB_ROOT_CACHED;
-	iotlb->limit = limit;
-	iotlb->nmaps = 0;
-	iotlb->flags = flags;
-	INIT_LIST_HEAD(&iotlb->list);
+	vhost_iotlb_init(iotlb, limit, flags);
 
 	return iotlb;
 }
diff --git a/include/linux/vhost_iotlb.h b/include/linux/vhost_iotlb.h
index 6b09b786a762..c0df193ec3e1 100644
--- a/include/linux/vhost_iotlb.h
+++ b/include/linux/vhost_iotlb.h
@@ -33,6 +33,8 @@ int vhost_iotlb_add_range(struct vhost_iotlb *iotlb, u64 start, u64 last,
 			  u64 addr, unsigned int perm);
 void vhost_iotlb_del_range(struct vhost_iotlb *iotlb, u64 start, u64 last);
 
+void vhost_iotlb_init(struct vhost_iotlb *iotlb, unsigned int limit,
+		      unsigned int flags);
 struct vhost_iotlb *vhost_iotlb_alloc(unsigned int limit, unsigned int flags);
 void vhost_iotlb_free(struct vhost_iotlb *iotlb);
 void vhost_iotlb_reset(struct vhost_iotlb *iotlb);
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 10/21] vhost: support ASID in IOTLB API
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

This patches allows userspace to send ASID based IOTLB message to
vhost. This idea is to use the reserved u32 field in the existing V2
IOTLB message. Vhost device should advertise this capability via
VHOST_BACKEND_F_IOTLB_ASID backend feature.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c             |  5 ++++-
 drivers/vhost/vhost.c            | 23 ++++++++++++++++++-----
 drivers/vhost/vhost.h            |  4 ++--
 include/uapi/linux/vhost_types.h |  5 ++++-
 4 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 03a9b3311c6c..feb6a58df22d 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -739,7 +739,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
 	return ret;
 }
 
-static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
+static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
 					struct vhost_iotlb_msg *msg)
 {
 	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
@@ -748,6 +748,9 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
 	struct vhost_iotlb *iotlb = v->iotlb;
 	int r = 0;
 
+	if (asid != 0)
+		return -EINVAL;
+
 	r = vhost_dev_check_owner(dev);
 	if (r)
 		return r;
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index a262e12c6dc2..7477b724c29b 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -468,7 +468,7 @@ void vhost_dev_init(struct vhost_dev *dev,
 		    struct vhost_virtqueue **vqs, int nvqs,
 		    int iov_limit, int weight, int byte_weight,
 		    bool use_worker,
-		    int (*msg_handler)(struct vhost_dev *dev,
+		    int (*msg_handler)(struct vhost_dev *dev, u32 asid,
 				       struct vhost_iotlb_msg *msg))
 {
 	struct vhost_virtqueue *vq;
@@ -1084,11 +1084,14 @@ static bool umem_access_ok(u64 uaddr, u64 size, int access)
 	return true;
 }
 
-static int vhost_process_iotlb_msg(struct vhost_dev *dev,
+static int vhost_process_iotlb_msg(struct vhost_dev *dev, u16 asid,
 				   struct vhost_iotlb_msg *msg)
 {
 	int ret = 0;
 
+	if (asid != 0)
+		return -EINVAL;
+
 	mutex_lock(&dev->mutex);
 	vhost_dev_lock_vqs(dev);
 	switch (msg->type) {
@@ -1135,6 +1138,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 	struct vhost_iotlb_msg msg;
 	size_t offset;
 	int type, ret;
+	u16 asid = 0;
 
 	ret = copy_from_iter(&type, sizeof(type), from);
 	if (ret != sizeof(type)) {
@@ -1150,7 +1154,16 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 		offset = offsetof(struct vhost_msg, iotlb) - sizeof(int);
 		break;
 	case VHOST_IOTLB_MSG_V2:
-		offset = sizeof(__u32);
+		if (vhost_backend_has_feature(dev->vqs[0],
+					      VHOST_BACKEND_F_IOTLB_ASID)) {
+			ret = copy_from_iter(&asid, sizeof(asid), from);
+			if (ret != sizeof(asid)) {
+				ret = -EINVAL;
+				goto done;
+			}
+			offset = sizeof(__u16);
+		} else
+			offset = sizeof(__u32);
 		break;
 	default:
 		ret = -EINVAL;
@@ -1165,9 +1178,9 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 	}
 
 	if (dev->msg_handler)
-		ret = dev->msg_handler(dev, &msg);
+		ret = dev->msg_handler(dev, asid, &msg);
 	else
-		ret = vhost_process_iotlb_msg(dev, &msg);
+		ret = vhost_process_iotlb_msg(dev, asid, &msg);
 	if (ret) {
 		ret = -EFAULT;
 		goto done;
diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
index b063324c7669..19753a90875c 100644
--- a/drivers/vhost/vhost.h
+++ b/drivers/vhost/vhost.h
@@ -162,7 +162,7 @@ struct vhost_dev {
 	int byte_weight;
 	u64 kcov_handle;
 	bool use_worker;
-	int (*msg_handler)(struct vhost_dev *dev,
+	int (*msg_handler)(struct vhost_dev *dev, u32 asid,
 			   struct vhost_iotlb_msg *msg);
 };
 
@@ -170,7 +170,7 @@ bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
 void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
 		    int nvqs, int iov_limit, int weight, int byte_weight,
 		    bool use_worker,
-		    int (*msg_handler)(struct vhost_dev *dev,
+		    int (*msg_handler)(struct vhost_dev *dev, u32 asid,
 				       struct vhost_iotlb_msg *msg));
 long vhost_dev_set_owner(struct vhost_dev *dev);
 bool vhost_dev_has_owner(struct vhost_dev *dev);
diff --git a/include/uapi/linux/vhost_types.h b/include/uapi/linux/vhost_types.h
index 76ee7016c501..222fc66ce2ac 100644
--- a/include/uapi/linux/vhost_types.h
+++ b/include/uapi/linux/vhost_types.h
@@ -87,7 +87,7 @@ struct vhost_msg {
 
 struct vhost_msg_v2 {
 	__u32 type;
-	__u32 reserved;
+	__u32 asid;
 	union {
 		struct vhost_iotlb_msg iotlb;
 		__u8 padding[64];
@@ -157,5 +157,8 @@ struct vhost_vdpa_iova_range {
 #define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1
 /* IOTLB can accept batching hints */
 #define VHOST_BACKEND_F_IOTLB_BATCH  0x2
+/* IOTLB can accept address space identifier through V2 type of IOTLB
+   message */
+#define VHOST_BACKEND_F_IOTLB_ASID  0x3
 
 #endif
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 10/21] vhost: support ASID in IOTLB API
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

This patches allows userspace to send ASID based IOTLB message to
vhost. This idea is to use the reserved u32 field in the existing V2
IOTLB message. Vhost device should advertise this capability via
VHOST_BACKEND_F_IOTLB_ASID backend feature.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c             |  5 ++++-
 drivers/vhost/vhost.c            | 23 ++++++++++++++++++-----
 drivers/vhost/vhost.h            |  4 ++--
 include/uapi/linux/vhost_types.h |  5 ++++-
 4 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 03a9b3311c6c..feb6a58df22d 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -739,7 +739,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
 	return ret;
 }
 
-static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
+static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
 					struct vhost_iotlb_msg *msg)
 {
 	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
@@ -748,6 +748,9 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
 	struct vhost_iotlb *iotlb = v->iotlb;
 	int r = 0;
 
+	if (asid != 0)
+		return -EINVAL;
+
 	r = vhost_dev_check_owner(dev);
 	if (r)
 		return r;
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index a262e12c6dc2..7477b724c29b 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -468,7 +468,7 @@ void vhost_dev_init(struct vhost_dev *dev,
 		    struct vhost_virtqueue **vqs, int nvqs,
 		    int iov_limit, int weight, int byte_weight,
 		    bool use_worker,
-		    int (*msg_handler)(struct vhost_dev *dev,
+		    int (*msg_handler)(struct vhost_dev *dev, u32 asid,
 				       struct vhost_iotlb_msg *msg))
 {
 	struct vhost_virtqueue *vq;
@@ -1084,11 +1084,14 @@ static bool umem_access_ok(u64 uaddr, u64 size, int access)
 	return true;
 }
 
-static int vhost_process_iotlb_msg(struct vhost_dev *dev,
+static int vhost_process_iotlb_msg(struct vhost_dev *dev, u16 asid,
 				   struct vhost_iotlb_msg *msg)
 {
 	int ret = 0;
 
+	if (asid != 0)
+		return -EINVAL;
+
 	mutex_lock(&dev->mutex);
 	vhost_dev_lock_vqs(dev);
 	switch (msg->type) {
@@ -1135,6 +1138,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 	struct vhost_iotlb_msg msg;
 	size_t offset;
 	int type, ret;
+	u16 asid = 0;
 
 	ret = copy_from_iter(&type, sizeof(type), from);
 	if (ret != sizeof(type)) {
@@ -1150,7 +1154,16 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 		offset = offsetof(struct vhost_msg, iotlb) - sizeof(int);
 		break;
 	case VHOST_IOTLB_MSG_V2:
-		offset = sizeof(__u32);
+		if (vhost_backend_has_feature(dev->vqs[0],
+					      VHOST_BACKEND_F_IOTLB_ASID)) {
+			ret = copy_from_iter(&asid, sizeof(asid), from);
+			if (ret != sizeof(asid)) {
+				ret = -EINVAL;
+				goto done;
+			}
+			offset = sizeof(__u16);
+		} else
+			offset = sizeof(__u32);
 		break;
 	default:
 		ret = -EINVAL;
@@ -1165,9 +1178,9 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 	}
 
 	if (dev->msg_handler)
-		ret = dev->msg_handler(dev, &msg);
+		ret = dev->msg_handler(dev, asid, &msg);
 	else
-		ret = vhost_process_iotlb_msg(dev, &msg);
+		ret = vhost_process_iotlb_msg(dev, asid, &msg);
 	if (ret) {
 		ret = -EFAULT;
 		goto done;
diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
index b063324c7669..19753a90875c 100644
--- a/drivers/vhost/vhost.h
+++ b/drivers/vhost/vhost.h
@@ -162,7 +162,7 @@ struct vhost_dev {
 	int byte_weight;
 	u64 kcov_handle;
 	bool use_worker;
-	int (*msg_handler)(struct vhost_dev *dev,
+	int (*msg_handler)(struct vhost_dev *dev, u32 asid,
 			   struct vhost_iotlb_msg *msg);
 };
 
@@ -170,7 +170,7 @@ bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
 void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
 		    int nvqs, int iov_limit, int weight, int byte_weight,
 		    bool use_worker,
-		    int (*msg_handler)(struct vhost_dev *dev,
+		    int (*msg_handler)(struct vhost_dev *dev, u32 asid,
 				       struct vhost_iotlb_msg *msg));
 long vhost_dev_set_owner(struct vhost_dev *dev);
 bool vhost_dev_has_owner(struct vhost_dev *dev);
diff --git a/include/uapi/linux/vhost_types.h b/include/uapi/linux/vhost_types.h
index 76ee7016c501..222fc66ce2ac 100644
--- a/include/uapi/linux/vhost_types.h
+++ b/include/uapi/linux/vhost_types.h
@@ -87,7 +87,7 @@ struct vhost_msg {
 
 struct vhost_msg_v2 {
 	__u32 type;
-	__u32 reserved;
+	__u32 asid;
 	union {
 		struct vhost_iotlb_msg iotlb;
 		__u8 padding[64];
@@ -157,5 +157,8 @@ struct vhost_vdpa_iova_range {
 #define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1
 /* IOTLB can accept batching hints */
 #define VHOST_BACKEND_F_IOTLB_BATCH  0x2
+/* IOTLB can accept address space identifier through V2 type of IOTLB
+   message */
+#define VHOST_BACKEND_F_IOTLB_ASID  0x3
 
 #endif
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 11/21] vhost-vdpa: introduce asid based IOTLB
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

This patch converts the vhost-vDPA device to support multiple IOTLBs
tagged via ASID via hlist. This will be used for supporting multiple
address spaces in the following patches.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c | 106 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 80 insertions(+), 26 deletions(-)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index feb6a58df22d..060d5b5b7e64 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -33,13 +33,21 @@ enum {
 
 #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
 
+#define VHOST_VDPA_IOTLB_BUCKETS 16
+
+struct vhost_vdpa_as {
+	struct hlist_node hash_link;
+	struct vhost_iotlb iotlb;
+	u32 id;
+};
+
 struct vhost_vdpa {
 	struct vhost_dev vdev;
 	struct iommu_domain *domain;
 	struct vhost_virtqueue *vqs;
 	struct completion completion;
 	struct vdpa_device *vdpa;
-	struct vhost_iotlb *iotlb;
+	struct hlist_head as[VHOST_VDPA_IOTLB_BUCKETS];
 	struct device dev;
 	struct cdev cdev;
 	atomic_t opened;
@@ -49,12 +57,64 @@ struct vhost_vdpa {
 	struct eventfd_ctx *config_ctx;
 	int in_batch;
 	struct vdpa_iova_range range;
+	int used_as;
 };
 
 static DEFINE_IDA(vhost_vdpa_ida);
 
 static dev_t vhost_vdpa_major;
 
+static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
+{
+	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
+	struct vhost_vdpa_as *as;
+
+	hlist_for_each_entry(as, head, hash_link)
+		if (as->id == asid)
+			return as;
+
+	return NULL;
+}
+
+static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
+{
+	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
+	struct vhost_vdpa_as *as;
+
+	if (asid_to_as(v, asid))
+		return NULL;
+
+	as = kmalloc(sizeof(*as), GFP_KERNEL);
+	if (!as)
+		return NULL;
+
+	vhost_iotlb_init(&as->iotlb, 0, 0);
+	as->id = asid;
+	hlist_add_head(&as->hash_link, head);
+	++v->used_as;
+
+	return as;
+}
+
+static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
+{
+	struct vhost_vdpa_as *as = asid_to_as(v, asid);
+
+	/* Remove default address space is not allowed */
+	if (asid == 0)
+		return -EINVAL;
+
+	if (!as)
+		return -EINVAL;
+
+	hlist_del(&as->hash_link);
+	vhost_iotlb_reset(&as->iotlb);
+	kfree(as);
+	--v->used_as;
+
+	return 0;
+}
+
 static void handle_vq_kick(struct vhost_work *work)
 {
 	struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
@@ -525,15 +585,6 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
 	}
 }
 
-static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v)
-{
-	struct vhost_iotlb *iotlb = v->iotlb;
-
-	vhost_vdpa_iotlb_unmap(v, iotlb, 0ULL, 0ULL - 1);
-	kfree(v->iotlb);
-	v->iotlb = NULL;
-}
-
 static int perm_to_iommu_flags(u32 perm)
 {
 	int flags = 0;
@@ -745,7 +796,8 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
 	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
-	struct vhost_iotlb *iotlb = v->iotlb;
+	struct vhost_vdpa_as *as = asid_to_as(v, 0);
+	struct vhost_iotlb *iotlb = &as->iotlb;
 	int r = 0;
 
 	if (asid != 0)
@@ -856,6 +908,13 @@ static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v)
 	}
 }
 
+static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
+{
+	vhost_dev_cleanup(&v->vdev);
+	kfree(v->vdev.vqs);
+	vhost_vdpa_remove_as(v, 0);
+}
+
 static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 {
 	struct vhost_vdpa *v;
@@ -886,15 +945,12 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 	vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
 		       vhost_vdpa_process_iotlb_msg);
 
-	v->iotlb = vhost_iotlb_alloc(0, 0);
-	if (!v->iotlb) {
-		r = -ENOMEM;
-		goto err_init_iotlb;
-	}
+	if (!vhost_vdpa_alloc_as(v, 0))
+		goto err_alloc_as;
 
 	r = vhost_vdpa_alloc_domain(v);
 	if (r)
-		goto err_alloc_domain;
+		goto err_alloc_as;
 
 	vhost_vdpa_set_iova_range(v);
 
@@ -902,11 +958,8 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 
 	return 0;
 
-err_alloc_domain:
-	vhost_vdpa_iotlb_free(v);
-err_init_iotlb:
-	vhost_dev_cleanup(&v->vdev);
-	kfree(vqs);
+err_alloc_as:
+	vhost_vdpa_cleanup(v);
 err:
 	atomic_dec(&v->opened);
 	return r;
@@ -933,12 +986,10 @@ static int vhost_vdpa_release(struct inode *inode, struct file *filep)
 	filep->private_data = NULL;
 	vhost_vdpa_reset(v);
 	vhost_dev_stop(&v->vdev);
-	vhost_vdpa_iotlb_free(v);
 	vhost_vdpa_free_domain(v);
 	vhost_vdpa_config_put(v);
 	vhost_vdpa_clean_irq(v);
-	vhost_dev_cleanup(&v->vdev);
-	kfree(v->vdev.vqs);
+	vhost_vdpa_cleanup(v);
 	mutex_unlock(&d->mutex);
 
 	atomic_dec(&v->opened);
@@ -1033,7 +1084,7 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
 	const struct vdpa_config_ops *ops = vdpa->config;
 	struct vhost_vdpa *v;
 	int minor;
-	int r;
+	int i, r;
 
 	/* Only support 1 address space and 1 groups */
 	if (vdpa->ngroups != 1 || vdpa->nas != 1)
@@ -1085,6 +1136,9 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
 	init_completion(&v->completion);
 	vdpa_set_drvdata(vdpa, v);
 
+	for (i = 0; i < VHOST_VDPA_IOTLB_BUCKETS; i++)
+		INIT_HLIST_HEAD(&v->as[i]);
+
 	return 0;
 
 err:
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 11/21] vhost-vdpa: introduce asid based IOTLB
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

This patch converts the vhost-vDPA device to support multiple IOTLBs
tagged via ASID via hlist. This will be used for supporting multiple
address spaces in the following patches.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c | 106 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 80 insertions(+), 26 deletions(-)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index feb6a58df22d..060d5b5b7e64 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -33,13 +33,21 @@ enum {
 
 #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
 
+#define VHOST_VDPA_IOTLB_BUCKETS 16
+
+struct vhost_vdpa_as {
+	struct hlist_node hash_link;
+	struct vhost_iotlb iotlb;
+	u32 id;
+};
+
 struct vhost_vdpa {
 	struct vhost_dev vdev;
 	struct iommu_domain *domain;
 	struct vhost_virtqueue *vqs;
 	struct completion completion;
 	struct vdpa_device *vdpa;
-	struct vhost_iotlb *iotlb;
+	struct hlist_head as[VHOST_VDPA_IOTLB_BUCKETS];
 	struct device dev;
 	struct cdev cdev;
 	atomic_t opened;
@@ -49,12 +57,64 @@ struct vhost_vdpa {
 	struct eventfd_ctx *config_ctx;
 	int in_batch;
 	struct vdpa_iova_range range;
+	int used_as;
 };
 
 static DEFINE_IDA(vhost_vdpa_ida);
 
 static dev_t vhost_vdpa_major;
 
+static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
+{
+	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
+	struct vhost_vdpa_as *as;
+
+	hlist_for_each_entry(as, head, hash_link)
+		if (as->id == asid)
+			return as;
+
+	return NULL;
+}
+
+static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
+{
+	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
+	struct vhost_vdpa_as *as;
+
+	if (asid_to_as(v, asid))
+		return NULL;
+
+	as = kmalloc(sizeof(*as), GFP_KERNEL);
+	if (!as)
+		return NULL;
+
+	vhost_iotlb_init(&as->iotlb, 0, 0);
+	as->id = asid;
+	hlist_add_head(&as->hash_link, head);
+	++v->used_as;
+
+	return as;
+}
+
+static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
+{
+	struct vhost_vdpa_as *as = asid_to_as(v, asid);
+
+	/* Remove default address space is not allowed */
+	if (asid == 0)
+		return -EINVAL;
+
+	if (!as)
+		return -EINVAL;
+
+	hlist_del(&as->hash_link);
+	vhost_iotlb_reset(&as->iotlb);
+	kfree(as);
+	--v->used_as;
+
+	return 0;
+}
+
 static void handle_vq_kick(struct vhost_work *work)
 {
 	struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
@@ -525,15 +585,6 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
 	}
 }
 
-static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v)
-{
-	struct vhost_iotlb *iotlb = v->iotlb;
-
-	vhost_vdpa_iotlb_unmap(v, iotlb, 0ULL, 0ULL - 1);
-	kfree(v->iotlb);
-	v->iotlb = NULL;
-}
-
 static int perm_to_iommu_flags(u32 perm)
 {
 	int flags = 0;
@@ -745,7 +796,8 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
 	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
-	struct vhost_iotlb *iotlb = v->iotlb;
+	struct vhost_vdpa_as *as = asid_to_as(v, 0);
+	struct vhost_iotlb *iotlb = &as->iotlb;
 	int r = 0;
 
 	if (asid != 0)
@@ -856,6 +908,13 @@ static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v)
 	}
 }
 
+static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
+{
+	vhost_dev_cleanup(&v->vdev);
+	kfree(v->vdev.vqs);
+	vhost_vdpa_remove_as(v, 0);
+}
+
 static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 {
 	struct vhost_vdpa *v;
@@ -886,15 +945,12 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 	vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
 		       vhost_vdpa_process_iotlb_msg);
 
-	v->iotlb = vhost_iotlb_alloc(0, 0);
-	if (!v->iotlb) {
-		r = -ENOMEM;
-		goto err_init_iotlb;
-	}
+	if (!vhost_vdpa_alloc_as(v, 0))
+		goto err_alloc_as;
 
 	r = vhost_vdpa_alloc_domain(v);
 	if (r)
-		goto err_alloc_domain;
+		goto err_alloc_as;
 
 	vhost_vdpa_set_iova_range(v);
 
@@ -902,11 +958,8 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 
 	return 0;
 
-err_alloc_domain:
-	vhost_vdpa_iotlb_free(v);
-err_init_iotlb:
-	vhost_dev_cleanup(&v->vdev);
-	kfree(vqs);
+err_alloc_as:
+	vhost_vdpa_cleanup(v);
 err:
 	atomic_dec(&v->opened);
 	return r;
@@ -933,12 +986,10 @@ static int vhost_vdpa_release(struct inode *inode, struct file *filep)
 	filep->private_data = NULL;
 	vhost_vdpa_reset(v);
 	vhost_dev_stop(&v->vdev);
-	vhost_vdpa_iotlb_free(v);
 	vhost_vdpa_free_domain(v);
 	vhost_vdpa_config_put(v);
 	vhost_vdpa_clean_irq(v);
-	vhost_dev_cleanup(&v->vdev);
-	kfree(v->vdev.vqs);
+	vhost_vdpa_cleanup(v);
 	mutex_unlock(&d->mutex);
 
 	atomic_dec(&v->opened);
@@ -1033,7 +1084,7 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
 	const struct vdpa_config_ops *ops = vdpa->config;
 	struct vhost_vdpa *v;
 	int minor;
-	int r;
+	int i, r;
 
 	/* Only support 1 address space and 1 groups */
 	if (vdpa->ngroups != 1 || vdpa->nas != 1)
@@ -1085,6 +1136,9 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
 	init_completion(&v->completion);
 	vdpa_set_drvdata(vdpa, v);
 
+	for (i = 0; i < VHOST_VDPA_IOTLB_BUCKETS; i++)
+		INIT_HLIST_HEAD(&v->as[i]);
+
 	return 0;
 
 err:
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 12/21] vhost-vdpa: introduce uAPI to get the number of virtqueue groups
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

Follows the vDPA support for multiple address spaces, this patch
introduce uAPI for the userspace to know the number of virtqueue
groups supported by the vDPA device.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c       | 4 ++++
 include/uapi/linux/vhost.h | 3 +++
 2 files changed, 7 insertions(+)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 060d5b5b7e64..1ba5901b28e7 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -536,6 +536,10 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
 	case VHOST_VDPA_GET_VRING_NUM:
 		r = vhost_vdpa_get_vring_num(v, argp);
 		break;
+	case VHOST_VDPA_GET_GROUP_NUM:
+		r = copy_to_user(argp, &v->vdpa->ngroups,
+				 sizeof(v->vdpa->ngroups));
+		break;
 	case VHOST_SET_LOG_BASE:
 	case VHOST_SET_LOG_FD:
 		r = -ENOIOCTLCMD;
diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index 59c6c0fbaba1..8a4e6e426bbf 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -145,4 +145,7 @@
 /* Get the valid iova range */
 #define VHOST_VDPA_GET_IOVA_RANGE	_IOR(VHOST_VIRTIO, 0x78, \
 					     struct vhost_vdpa_iova_range)
+/* Get the number of virtqueue groups. */
+#define VHOST_VDPA_GET_GROUP_NUM	_IOR(VHOST_VIRTIO, 0x79, unsigned int)
+
 #endif
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 12/21] vhost-vdpa: introduce uAPI to get the number of virtqueue groups
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

Follows the vDPA support for multiple address spaces, this patch
introduce uAPI for the userspace to know the number of virtqueue
groups supported by the vDPA device.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c       | 4 ++++
 include/uapi/linux/vhost.h | 3 +++
 2 files changed, 7 insertions(+)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 060d5b5b7e64..1ba5901b28e7 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -536,6 +536,10 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
 	case VHOST_VDPA_GET_VRING_NUM:
 		r = vhost_vdpa_get_vring_num(v, argp);
 		break;
+	case VHOST_VDPA_GET_GROUP_NUM:
+		r = copy_to_user(argp, &v->vdpa->ngroups,
+				 sizeof(v->vdpa->ngroups));
+		break;
 	case VHOST_SET_LOG_BASE:
 	case VHOST_SET_LOG_FD:
 		r = -ENOIOCTLCMD;
diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index 59c6c0fbaba1..8a4e6e426bbf 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -145,4 +145,7 @@
 /* Get the valid iova range */
 #define VHOST_VDPA_GET_IOVA_RANGE	_IOR(VHOST_VIRTIO, 0x78, \
 					     struct vhost_vdpa_iova_range)
+/* Get the number of virtqueue groups. */
+#define VHOST_VDPA_GET_GROUP_NUM	_IOR(VHOST_VIRTIO, 0x79, unsigned int)
+
 #endif
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 13/21] vhost-vdpa: introduce uAPI to get the number of address spaces
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

This patch introduces the uAPI for getting the number of address
spaces supported by this vDPA device.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c       | 3 +++
 include/uapi/linux/vhost.h | 2 ++
 2 files changed, 5 insertions(+)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 1ba5901b28e7..bff8aa214f78 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -540,6 +540,9 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
 		r = copy_to_user(argp, &v->vdpa->ngroups,
 				 sizeof(v->vdpa->ngroups));
 		break;
+	case VHOST_VDPA_GET_AS_NUM:
+		r = copy_to_user(argp, &v->vdpa->nas, sizeof(v->vdpa->nas));
+		break;
 	case VHOST_SET_LOG_BASE:
 	case VHOST_SET_LOG_FD:
 		r = -ENOIOCTLCMD;
diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index 8a4e6e426bbf..8762911a3cb8 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -148,4 +148,6 @@
 /* Get the number of virtqueue groups. */
 #define VHOST_VDPA_GET_GROUP_NUM	_IOR(VHOST_VIRTIO, 0x79, unsigned int)
 
+/* Get the number of address spaces. */
+#define VHOST_VDPA_GET_AS_NUM		_IOR(VHOST_VIRTIO, 0x7A, unsigned int)
 #endif
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 13/21] vhost-vdpa: introduce uAPI to get the number of address spaces
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

This patch introduces the uAPI for getting the number of address
spaces supported by this vDPA device.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c       | 3 +++
 include/uapi/linux/vhost.h | 2 ++
 2 files changed, 5 insertions(+)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 1ba5901b28e7..bff8aa214f78 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -540,6 +540,9 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
 		r = copy_to_user(argp, &v->vdpa->ngroups,
 				 sizeof(v->vdpa->ngroups));
 		break;
+	case VHOST_VDPA_GET_AS_NUM:
+		r = copy_to_user(argp, &v->vdpa->nas, sizeof(v->vdpa->nas));
+		break;
 	case VHOST_SET_LOG_BASE:
 	case VHOST_SET_LOG_FD:
 		r = -ENOIOCTLCMD;
diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index 8a4e6e426bbf..8762911a3cb8 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -148,4 +148,6 @@
 /* Get the number of virtqueue groups. */
 #define VHOST_VDPA_GET_GROUP_NUM	_IOR(VHOST_VIRTIO, 0x79, unsigned int)
 
+/* Get the number of address spaces. */
+#define VHOST_VDPA_GET_AS_NUM		_IOR(VHOST_VIRTIO, 0x7A, unsigned int)
 #endif
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 14/21] vhost-vdpa: uAPI to get virtqueue group id
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

Follows the support for virtqueue group in vDPA. This patches
introduces uAPI to get the virtqueue group ID for a specific virtqueue
in vhost-vdpa.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c       | 8 ++++++++
 include/uapi/linux/vhost.h | 8 ++++++++
 2 files changed, 16 insertions(+)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index bff8aa214f78..e7023abda12c 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -442,6 +442,14 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,
 			return -EFAULT;
 		ops->set_vq_ready(vdpa, idx, s.num);
 		return 0;
+	case VHOST_VDPA_GET_VRING_GROUP:
+		s.index = idx;
+		s.num = ops->get_vq_group(vdpa, idx);
+		if (s.num >= vdpa->ngroups)
+			return -EIO;
+		else if (copy_to_user(argp, &s, sizeof s))
+			return -EFAULT;
+		return 0;
 	case VHOST_GET_VRING_BASE:
 		r = ops->get_vq_state(v->vdpa, idx, &vq_state);
 		if (r)
diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index 8762911a3cb8..99de06476fdc 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -150,4 +150,12 @@
 
 /* Get the number of address spaces. */
 #define VHOST_VDPA_GET_AS_NUM		_IOR(VHOST_VIRTIO, 0x7A, unsigned int)
+
+/* Get the group for a virtqueue: read index, write group in num,
+ * The virtqueue index is stored in the index field of
+ * vhost_vring_state. The group for this specific virtqueue is
+ * returned via num field of vhost_vring_state.
+ */
+#define VHOST_VDPA_GET_VRING_GROUP	_IOWR(VHOST_VIRTIO, 0x7B,	\
+					      struct vhost_vring_state)
 #endif
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 14/21] vhost-vdpa: uAPI to get virtqueue group id
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

Follows the support for virtqueue group in vDPA. This patches
introduces uAPI to get the virtqueue group ID for a specific virtqueue
in vhost-vdpa.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c       | 8 ++++++++
 include/uapi/linux/vhost.h | 8 ++++++++
 2 files changed, 16 insertions(+)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index bff8aa214f78..e7023abda12c 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -442,6 +442,14 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,
 			return -EFAULT;
 		ops->set_vq_ready(vdpa, idx, s.num);
 		return 0;
+	case VHOST_VDPA_GET_VRING_GROUP:
+		s.index = idx;
+		s.num = ops->get_vq_group(vdpa, idx);
+		if (s.num >= vdpa->ngroups)
+			return -EIO;
+		else if (copy_to_user(argp, &s, sizeof s))
+			return -EFAULT;
+		return 0;
 	case VHOST_GET_VRING_BASE:
 		r = ops->get_vq_state(v->vdpa, idx, &vq_state);
 		if (r)
diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index 8762911a3cb8..99de06476fdc 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -150,4 +150,12 @@
 
 /* Get the number of address spaces. */
 #define VHOST_VDPA_GET_AS_NUM		_IOR(VHOST_VIRTIO, 0x7A, unsigned int)
+
+/* Get the group for a virtqueue: read index, write group in num,
+ * The virtqueue index is stored in the index field of
+ * vhost_vring_state. The group for this specific virtqueue is
+ * returned via num field of vhost_vring_state.
+ */
+#define VHOST_VDPA_GET_VRING_GROUP	_IOWR(VHOST_VIRTIO, 0x7B,	\
+					      struct vhost_vring_state)
 #endif
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 15/21] vhost-vdpa: introduce uAPI to set group ASID
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

Follows the vDPA support for associating ASID to a specific virtqueue
group. This patch adds a uAPI to support setting them from userspace.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c       | 8 ++++++++
 include/uapi/linux/vhost.h | 7 +++++++
 2 files changed, 15 insertions(+)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index e7023abda12c..cd7c9a401a61 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -450,6 +450,14 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,
 		else if (copy_to_user(argp, &s, sizeof s))
 			return -EFAULT;
 		return 0;
+	case VHOST_VDPA_SET_GROUP_ASID:
+		if (copy_from_user(&s, argp, sizeof(s)))
+			return -EFAULT;
+		if (s.num >= vdpa->nas)
+			return -EINVAL;
+		if (!ops->set_group_asid)
+			return -ENOTSUPP;
+		return ops->set_group_asid(vdpa, idx, s.num);
 	case VHOST_GET_VRING_BASE:
 		r = ops->get_vq_state(v->vdpa, idx, &vq_state);
 		if (r)
diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index 99de06476fdc..5e083490f1aa 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -158,4 +158,11 @@
  */
 #define VHOST_VDPA_GET_VRING_GROUP	_IOWR(VHOST_VIRTIO, 0x7B,	\
 					      struct vhost_vring_state)
+/* Set the ASID for a virtqueue group. The group index is stored in
+ * the index field of vhost_vring_state, the ASID associated with this
+ * group is stored at num field of vhost_vring_state.
+ */
+#define VHOST_VDPA_SET_GROUP_ASID	_IOW(VHOST_VIRTIO, 0x7C, \
+					     struct vhost_vring_state)
+
 #endif
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 15/21] vhost-vdpa: introduce uAPI to set group ASID
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

Follows the vDPA support for associating ASID to a specific virtqueue
group. This patch adds a uAPI to support setting them from userspace.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c       | 8 ++++++++
 include/uapi/linux/vhost.h | 7 +++++++
 2 files changed, 15 insertions(+)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index e7023abda12c..cd7c9a401a61 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -450,6 +450,14 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,
 		else if (copy_to_user(argp, &s, sizeof s))
 			return -EFAULT;
 		return 0;
+	case VHOST_VDPA_SET_GROUP_ASID:
+		if (copy_from_user(&s, argp, sizeof(s)))
+			return -EFAULT;
+		if (s.num >= vdpa->nas)
+			return -EINVAL;
+		if (!ops->set_group_asid)
+			return -ENOTSUPP;
+		return ops->set_group_asid(vdpa, idx, s.num);
 	case VHOST_GET_VRING_BASE:
 		r = ops->get_vq_state(v->vdpa, idx, &vq_state);
 		if (r)
diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index 99de06476fdc..5e083490f1aa 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -158,4 +158,11 @@
  */
 #define VHOST_VDPA_GET_VRING_GROUP	_IOWR(VHOST_VIRTIO, 0x7B,	\
 					      struct vhost_vring_state)
+/* Set the ASID for a virtqueue group. The group index is stored in
+ * the index field of vhost_vring_state, the ASID associated with this
+ * group is stored at num field of vhost_vring_state.
+ */
+#define VHOST_VDPA_SET_GROUP_ASID	_IOW(VHOST_VIRTIO, 0x7C, \
+					     struct vhost_vring_state)
+
 #endif
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 16/21] vhost-vdpa: support ASID based IOTLB API
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

This patch extends the vhost-vdpa to support ASID based IOTLB API. The
vhost-vdpa device will allocated multple IOTLBs for vDPA device that
supports multiple address spaces. The IOTLBs and vDPA device memory
mappings is determined and maintained through ASID.

Note that we still don't support vDPA device with more than one
address spaces that depends on platform IOMMU. This work will be done
by moving the IOMMU logic from vhost-vDPA to vDPA device driver.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c  | 127 ++++++++++++++++++++++++++++++++----------
 drivers/vhost/vhost.c |   4 +-
 2 files changed, 99 insertions(+), 32 deletions(-)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index cd7c9a401a61..c4fda48d4273 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -28,7 +28,8 @@
 enum {
 	VHOST_VDPA_BACKEND_FEATURES =
 	(1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) |
-	(1ULL << VHOST_BACKEND_F_IOTLB_BATCH),
+	(1ULL << VHOST_BACKEND_F_IOTLB_BATCH) |
+	(1ULL << VHOST_BACKEND_F_IOTLB_ASID),
 };
 
 #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
@@ -57,13 +58,20 @@ struct vhost_vdpa {
 	struct eventfd_ctx *config_ctx;
 	int in_batch;
 	struct vdpa_iova_range range;
-	int used_as;
+	u32 batch_asid;
 };
 
 static DEFINE_IDA(vhost_vdpa_ida);
 
 static dev_t vhost_vdpa_major;
 
+static inline u32 iotlb_to_asid(struct vhost_iotlb *iotlb)
+{
+	struct vhost_vdpa_as *as = container_of(iotlb, struct
+						vhost_vdpa_as, iotlb);
+	return as->id;
+}
+
 static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
 {
 	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
@@ -76,6 +84,16 @@ static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
 	return NULL;
 }
 
+static struct vhost_iotlb *asid_to_iotlb(struct vhost_vdpa *v, u32 asid)
+{
+	struct vhost_vdpa_as *as = asid_to_as(v, asid);
+
+	if (!as)
+		return NULL;
+
+	return &as->iotlb;
+}
+
 static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
 {
 	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
@@ -84,6 +102,9 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
 	if (asid_to_as(v, asid))
 		return NULL;
 
+	if (asid >= v->vdpa->nas)
+		return NULL;
+
 	as = kmalloc(sizeof(*as), GFP_KERNEL);
 	if (!as)
 		return NULL;
@@ -91,18 +112,24 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
 	vhost_iotlb_init(&as->iotlb, 0, 0);
 	as->id = asid;
 	hlist_add_head(&as->hash_link, head);
-	++v->used_as;
 
 	return as;
 }
 
-static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
+static struct vhost_vdpa_as *vhost_vdpa_find_alloc_as(struct vhost_vdpa *v,
+						      u32 asid)
 {
 	struct vhost_vdpa_as *as = asid_to_as(v, asid);
 
-	/* Remove default address space is not allowed */
-	if (asid == 0)
-		return -EINVAL;
+	if (as)
+		return as;
+
+	return vhost_vdpa_alloc_as(v, asid);
+}
+
+static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
+{
+	struct vhost_vdpa_as *as = asid_to_as(v, asid);
 
 	if (!as)
 		return -EINVAL;
@@ -110,7 +137,6 @@ static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
 	hlist_del(&as->hash_link);
 	vhost_iotlb_reset(&as->iotlb);
 	kfree(as);
-	--v->used_as;
 
 	return 0;
 }
@@ -636,6 +662,7 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
 	struct vhost_dev *dev = &v->vdev;
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
+	u32 asid = iotlb_to_asid(iotlb);
 	int r = 0;
 
 	r = vhost_iotlb_add_range(iotlb, iova, iova + size - 1,
@@ -644,10 +671,10 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
 		return r;
 
 	if (ops->dma_map) {
-		r = ops->dma_map(vdpa, 0, iova, size, pa, perm);
+		r = ops->dma_map(vdpa, asid, iova, size, pa, perm);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			r = ops->set_map(vdpa, 0, iotlb);
+			r = ops->set_map(vdpa, asid, iotlb);
 	} else {
 		r = iommu_map(v->domain, iova, pa, size,
 			      perm_to_iommu_flags(perm));
@@ -661,23 +688,35 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
 	return r;
 }
 
-static void vhost_vdpa_unmap(struct vhost_vdpa *v,
-			     struct vhost_iotlb *iotlb,
-			     u64 iova, u64 size)
+static int vhost_vdpa_unmap(struct vhost_vdpa *v,
+			    struct vhost_iotlb *iotlb,
+			    u64 iova, u64 size)
 {
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
+	u32 asid = iotlb_to_asid(iotlb);
+
+	if (!iotlb)
+		return -EINVAL;
 
 	vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
 
 	if (ops->dma_map) {
-		ops->dma_unmap(vdpa, 0, iova, size);
+		ops->dma_unmap(vdpa, asid, iova, size);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			ops->set_map(vdpa, 0, iotlb);
+			ops->set_map(vdpa, asid, iotlb);
 	} else {
 		iommu_unmap(v->domain, iova, size);
 	}
+
+	/* If we are in the middle of batch processing, delay the free
+	 * of AS until BATCH_END.
+	 */
+	if (!v->in_batch && !iotlb->nmaps)
+		vhost_vdpa_remove_as(v, asid);
+
+	return 0;
 }
 
 static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
@@ -819,31 +858,52 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
 	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
-	struct vhost_vdpa_as *as = asid_to_as(v, 0);
-	struct vhost_iotlb *iotlb = &as->iotlb;
+	struct vhost_iotlb *iotlb = NULL;
+	struct vhost_vdpa_as *as = NULL;
 	int r = 0;
 
-	if (asid != 0)
-		return -EINVAL;
-
 	r = vhost_dev_check_owner(dev);
 	if (r)
 		return r;
 
+	if (msg->type == VHOST_IOTLB_UPDATE ||
+	    msg->type == VHOST_IOTLB_BATCH_BEGIN) {
+		as = vhost_vdpa_find_alloc_as(v, asid);
+		if (!as) {
+			printk("can't find and alloc asid %d\n", asid);
+			return -EINVAL;
+		}
+		iotlb = &as->iotlb;
+	} else
+		iotlb = asid_to_iotlb(v, asid);
+
+	if ((v->in_batch && v->batch_asid != asid) || !iotlb) {
+		if (v->in_batch && v->batch_asid != asid) {
+			printk("batch id %d asid %d\n",
+				v->batch_asid, asid);
+		}
+		if (!iotlb)
+			printk("no iotlb for asid %d\n", asid);
+		return -EINVAL;
+	}
+
 	switch (msg->type) {
 	case VHOST_IOTLB_UPDATE:
 		r = vhost_vdpa_process_iotlb_update(v, iotlb, msg);
 		break;
 	case VHOST_IOTLB_INVALIDATE:
-		vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
+		r = vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
 		break;
 	case VHOST_IOTLB_BATCH_BEGIN:
+		v->batch_asid = asid;
 		v->in_batch = true;
 		break;
 	case VHOST_IOTLB_BATCH_END:
 		if (v->in_batch && ops->set_map)
-			ops->set_map(vdpa, 0, iotlb);
+			ops->set_map(vdpa, asid, iotlb);
 		v->in_batch = false;
+		if (!iotlb->nmaps)
+			vhost_vdpa_remove_as(v, asid);
 		break;
 	default:
 		r = -EINVAL;
@@ -933,9 +993,17 @@ static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v)
 
 static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
 {
+	struct vhost_vdpa_as *as;
+	u32 asid;
+
 	vhost_dev_cleanup(&v->vdev);
 	kfree(v->vdev.vqs);
-	vhost_vdpa_remove_as(v, 0);
+
+	for (asid = 0; asid < v->vdpa->nas; asid++) {
+		as = asid_to_as(v, asid);
+		if (as)
+			vhost_vdpa_remove_as(v, asid);
+	}
 }
 
 static int vhost_vdpa_open(struct inode *inode, struct file *filep)
@@ -968,12 +1036,9 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 	vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
 		       vhost_vdpa_process_iotlb_msg);
 
-	if (!vhost_vdpa_alloc_as(v, 0))
-		goto err_alloc_as;
-
 	r = vhost_vdpa_alloc_domain(v);
 	if (r)
-		goto err_alloc_as;
+		goto err_alloc_domain;
 
 	vhost_vdpa_set_iova_range(v);
 
@@ -981,7 +1046,7 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 
 	return 0;
 
-err_alloc_as:
+err_alloc_domain:
 	vhost_vdpa_cleanup(v);
 err:
 	atomic_dec(&v->opened);
@@ -1109,8 +1174,10 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
 	int minor;
 	int i, r;
 
-	/* Only support 1 address space and 1 groups */
-	if (vdpa->ngroups != 1 || vdpa->nas != 1)
+	/* We can't support platform IOMMU device with more than 1
+	   group or as */
+	if (!ops->set_map && !ops->dma_map &&
+	    (vdpa->ngroups > 1 || vdpa->nas > 1))
 		return -ENOTSUPP;
 
 	/* Currently, we only accept the network devices. */
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 7477b724c29b..d59a9b171756 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -1138,7 +1138,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 	struct vhost_iotlb_msg msg;
 	size_t offset;
 	int type, ret;
-	u16 asid = 0;
+	u32 asid = 0;
 
 	ret = copy_from_iter(&type, sizeof(type), from);
 	if (ret != sizeof(type)) {
@@ -1161,7 +1161,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 				ret = -EINVAL;
 				goto done;
 			}
-			offset = sizeof(__u16);
+			offset = 0;
 		} else
 			offset = sizeof(__u32);
 		break;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 16/21] vhost-vdpa: support ASID based IOTLB API
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

This patch extends the vhost-vdpa to support ASID based IOTLB API. The
vhost-vdpa device will allocated multple IOTLBs for vDPA device that
supports multiple address spaces. The IOTLBs and vDPA device memory
mappings is determined and maintained through ASID.

Note that we still don't support vDPA device with more than one
address spaces that depends on platform IOMMU. This work will be done
by moving the IOMMU logic from vhost-vDPA to vDPA device driver.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vhost/vdpa.c  | 127 ++++++++++++++++++++++++++++++++----------
 drivers/vhost/vhost.c |   4 +-
 2 files changed, 99 insertions(+), 32 deletions(-)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index cd7c9a401a61..c4fda48d4273 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -28,7 +28,8 @@
 enum {
 	VHOST_VDPA_BACKEND_FEATURES =
 	(1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) |
-	(1ULL << VHOST_BACKEND_F_IOTLB_BATCH),
+	(1ULL << VHOST_BACKEND_F_IOTLB_BATCH) |
+	(1ULL << VHOST_BACKEND_F_IOTLB_ASID),
 };
 
 #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
@@ -57,13 +58,20 @@ struct vhost_vdpa {
 	struct eventfd_ctx *config_ctx;
 	int in_batch;
 	struct vdpa_iova_range range;
-	int used_as;
+	u32 batch_asid;
 };
 
 static DEFINE_IDA(vhost_vdpa_ida);
 
 static dev_t vhost_vdpa_major;
 
+static inline u32 iotlb_to_asid(struct vhost_iotlb *iotlb)
+{
+	struct vhost_vdpa_as *as = container_of(iotlb, struct
+						vhost_vdpa_as, iotlb);
+	return as->id;
+}
+
 static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
 {
 	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
@@ -76,6 +84,16 @@ static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
 	return NULL;
 }
 
+static struct vhost_iotlb *asid_to_iotlb(struct vhost_vdpa *v, u32 asid)
+{
+	struct vhost_vdpa_as *as = asid_to_as(v, asid);
+
+	if (!as)
+		return NULL;
+
+	return &as->iotlb;
+}
+
 static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
 {
 	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
@@ -84,6 +102,9 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
 	if (asid_to_as(v, asid))
 		return NULL;
 
+	if (asid >= v->vdpa->nas)
+		return NULL;
+
 	as = kmalloc(sizeof(*as), GFP_KERNEL);
 	if (!as)
 		return NULL;
@@ -91,18 +112,24 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
 	vhost_iotlb_init(&as->iotlb, 0, 0);
 	as->id = asid;
 	hlist_add_head(&as->hash_link, head);
-	++v->used_as;
 
 	return as;
 }
 
-static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
+static struct vhost_vdpa_as *vhost_vdpa_find_alloc_as(struct vhost_vdpa *v,
+						      u32 asid)
 {
 	struct vhost_vdpa_as *as = asid_to_as(v, asid);
 
-	/* Remove default address space is not allowed */
-	if (asid == 0)
-		return -EINVAL;
+	if (as)
+		return as;
+
+	return vhost_vdpa_alloc_as(v, asid);
+}
+
+static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
+{
+	struct vhost_vdpa_as *as = asid_to_as(v, asid);
 
 	if (!as)
 		return -EINVAL;
@@ -110,7 +137,6 @@ static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
 	hlist_del(&as->hash_link);
 	vhost_iotlb_reset(&as->iotlb);
 	kfree(as);
-	--v->used_as;
 
 	return 0;
 }
@@ -636,6 +662,7 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
 	struct vhost_dev *dev = &v->vdev;
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
+	u32 asid = iotlb_to_asid(iotlb);
 	int r = 0;
 
 	r = vhost_iotlb_add_range(iotlb, iova, iova + size - 1,
@@ -644,10 +671,10 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
 		return r;
 
 	if (ops->dma_map) {
-		r = ops->dma_map(vdpa, 0, iova, size, pa, perm);
+		r = ops->dma_map(vdpa, asid, iova, size, pa, perm);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			r = ops->set_map(vdpa, 0, iotlb);
+			r = ops->set_map(vdpa, asid, iotlb);
 	} else {
 		r = iommu_map(v->domain, iova, pa, size,
 			      perm_to_iommu_flags(perm));
@@ -661,23 +688,35 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
 	return r;
 }
 
-static void vhost_vdpa_unmap(struct vhost_vdpa *v,
-			     struct vhost_iotlb *iotlb,
-			     u64 iova, u64 size)
+static int vhost_vdpa_unmap(struct vhost_vdpa *v,
+			    struct vhost_iotlb *iotlb,
+			    u64 iova, u64 size)
 {
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
+	u32 asid = iotlb_to_asid(iotlb);
+
+	if (!iotlb)
+		return -EINVAL;
 
 	vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
 
 	if (ops->dma_map) {
-		ops->dma_unmap(vdpa, 0, iova, size);
+		ops->dma_unmap(vdpa, asid, iova, size);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			ops->set_map(vdpa, 0, iotlb);
+			ops->set_map(vdpa, asid, iotlb);
 	} else {
 		iommu_unmap(v->domain, iova, size);
 	}
+
+	/* If we are in the middle of batch processing, delay the free
+	 * of AS until BATCH_END.
+	 */
+	if (!v->in_batch && !iotlb->nmaps)
+		vhost_vdpa_remove_as(v, asid);
+
+	return 0;
 }
 
 static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
@@ -819,31 +858,52 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
 	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
-	struct vhost_vdpa_as *as = asid_to_as(v, 0);
-	struct vhost_iotlb *iotlb = &as->iotlb;
+	struct vhost_iotlb *iotlb = NULL;
+	struct vhost_vdpa_as *as = NULL;
 	int r = 0;
 
-	if (asid != 0)
-		return -EINVAL;
-
 	r = vhost_dev_check_owner(dev);
 	if (r)
 		return r;
 
+	if (msg->type == VHOST_IOTLB_UPDATE ||
+	    msg->type == VHOST_IOTLB_BATCH_BEGIN) {
+		as = vhost_vdpa_find_alloc_as(v, asid);
+		if (!as) {
+			printk("can't find and alloc asid %d\n", asid);
+			return -EINVAL;
+		}
+		iotlb = &as->iotlb;
+	} else
+		iotlb = asid_to_iotlb(v, asid);
+
+	if ((v->in_batch && v->batch_asid != asid) || !iotlb) {
+		if (v->in_batch && v->batch_asid != asid) {
+			printk("batch id %d asid %d\n",
+				v->batch_asid, asid);
+		}
+		if (!iotlb)
+			printk("no iotlb for asid %d\n", asid);
+		return -EINVAL;
+	}
+
 	switch (msg->type) {
 	case VHOST_IOTLB_UPDATE:
 		r = vhost_vdpa_process_iotlb_update(v, iotlb, msg);
 		break;
 	case VHOST_IOTLB_INVALIDATE:
-		vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
+		r = vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
 		break;
 	case VHOST_IOTLB_BATCH_BEGIN:
+		v->batch_asid = asid;
 		v->in_batch = true;
 		break;
 	case VHOST_IOTLB_BATCH_END:
 		if (v->in_batch && ops->set_map)
-			ops->set_map(vdpa, 0, iotlb);
+			ops->set_map(vdpa, asid, iotlb);
 		v->in_batch = false;
+		if (!iotlb->nmaps)
+			vhost_vdpa_remove_as(v, asid);
 		break;
 	default:
 		r = -EINVAL;
@@ -933,9 +993,17 @@ static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v)
 
 static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
 {
+	struct vhost_vdpa_as *as;
+	u32 asid;
+
 	vhost_dev_cleanup(&v->vdev);
 	kfree(v->vdev.vqs);
-	vhost_vdpa_remove_as(v, 0);
+
+	for (asid = 0; asid < v->vdpa->nas; asid++) {
+		as = asid_to_as(v, asid);
+		if (as)
+			vhost_vdpa_remove_as(v, asid);
+	}
 }
 
 static int vhost_vdpa_open(struct inode *inode, struct file *filep)
@@ -968,12 +1036,9 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 	vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
 		       vhost_vdpa_process_iotlb_msg);
 
-	if (!vhost_vdpa_alloc_as(v, 0))
-		goto err_alloc_as;
-
 	r = vhost_vdpa_alloc_domain(v);
 	if (r)
-		goto err_alloc_as;
+		goto err_alloc_domain;
 
 	vhost_vdpa_set_iova_range(v);
 
@@ -981,7 +1046,7 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 
 	return 0;
 
-err_alloc_as:
+err_alloc_domain:
 	vhost_vdpa_cleanup(v);
 err:
 	atomic_dec(&v->opened);
@@ -1109,8 +1174,10 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
 	int minor;
 	int i, r;
 
-	/* Only support 1 address space and 1 groups */
-	if (vdpa->ngroups != 1 || vdpa->nas != 1)
+	/* We can't support platform IOMMU device with more than 1
+	   group or as */
+	if (!ops->set_map && !ops->dma_map &&
+	    (vdpa->ngroups > 1 || vdpa->nas > 1))
 		return -ENOTSUPP;
 
 	/* Currently, we only accept the network devices. */
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 7477b724c29b..d59a9b171756 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -1138,7 +1138,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 	struct vhost_iotlb_msg msg;
 	size_t offset;
 	int type, ret;
-	u16 asid = 0;
+	u32 asid = 0;
 
 	ret = copy_from_iter(&type, sizeof(type), from);
 	if (ret != sizeof(type)) {
@@ -1161,7 +1161,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 				ret = -EINVAL;
 				goto done;
 			}
-			offset = sizeof(__u16);
+			offset = 0;
 		} else
 			offset = sizeof(__u32);
 		break;
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 17/21] vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

From: Stefano Garzarella <sgarzare@redhat.com>

vringh_getdesc_iotlb() manages 2 iovs for writable and readable
descriptors. This is very useful for the block device, where for
each request we have both types of descriptor.

Let's split the vdpasim_virtqueue's iov field in out_iov and
in_iov to use them with vringh_getdesc_iotlb().

We are using VIRTIO terminology for "out" (readable by the device)
and "in" (writable by the device) descriptors.

Acked-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
---
 drivers/vdpa/vdpa_sim/vdpa_sim.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index 140de45ffff2..fe4888dfb70f 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -44,7 +44,8 @@ MODULE_PARM_DESC(macaddr, "Ethernet MAC address");
 
 struct vdpasim_virtqueue {
 	struct vringh vring;
-	struct vringh_kiov iov;
+	struct vringh_kiov in_iov;
+	struct vringh_kiov out_iov;
 	unsigned short head;
 	bool ready;
 	u64 desc_addr;
@@ -178,12 +179,12 @@ static void vdpasim_work(struct work_struct *work)
 
 	while (true) {
 		total_write = 0;
-		err = vringh_getdesc_iotlb(&txq->vring, &txq->iov, NULL,
+		err = vringh_getdesc_iotlb(&txq->vring, &txq->out_iov, NULL,
 					   &txq->head, GFP_ATOMIC);
 		if (err <= 0)
 			break;
 
-		err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->iov,
+		err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->in_iov,
 					   &rxq->head, GFP_ATOMIC);
 		if (err <= 0) {
 			vringh_complete_iotlb(&txq->vring, txq->head, 0);
@@ -191,13 +192,13 @@ static void vdpasim_work(struct work_struct *work)
 		}
 
 		while (true) {
-			read = vringh_iov_pull_iotlb(&txq->vring, &txq->iov,
+			read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
 						     vdpasim->buffer,
 						     PAGE_SIZE);
 			if (read <= 0)
 				break;
 
-			write = vringh_iov_push_iotlb(&rxq->vring, &rxq->iov,
+			write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
 						      vdpasim->buffer, read);
 			if (write <= 0)
 				break;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 17/21] vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

From: Stefano Garzarella <sgarzare@redhat.com>

vringh_getdesc_iotlb() manages 2 iovs for writable and readable
descriptors. This is very useful for the block device, where for
each request we have both types of descriptor.

Let's split the vdpasim_virtqueue's iov field in out_iov and
in_iov to use them with vringh_getdesc_iotlb().

We are using VIRTIO terminology for "out" (readable by the device)
and "in" (writable by the device) descriptors.

Acked-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
---
 drivers/vdpa/vdpa_sim/vdpa_sim.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index 140de45ffff2..fe4888dfb70f 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -44,7 +44,8 @@ MODULE_PARM_DESC(macaddr, "Ethernet MAC address");
 
 struct vdpasim_virtqueue {
 	struct vringh vring;
-	struct vringh_kiov iov;
+	struct vringh_kiov in_iov;
+	struct vringh_kiov out_iov;
 	unsigned short head;
 	bool ready;
 	u64 desc_addr;
@@ -178,12 +179,12 @@ static void vdpasim_work(struct work_struct *work)
 
 	while (true) {
 		total_write = 0;
-		err = vringh_getdesc_iotlb(&txq->vring, &txq->iov, NULL,
+		err = vringh_getdesc_iotlb(&txq->vring, &txq->out_iov, NULL,
 					   &txq->head, GFP_ATOMIC);
 		if (err <= 0)
 			break;
 
-		err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->iov,
+		err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->in_iov,
 					   &rxq->head, GFP_ATOMIC);
 		if (err <= 0) {
 			vringh_complete_iotlb(&txq->vring, txq->head, 0);
@@ -191,13 +192,13 @@ static void vdpasim_work(struct work_struct *work)
 		}
 
 		while (true) {
-			read = vringh_iov_pull_iotlb(&txq->vring, &txq->iov,
+			read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
 						     vdpasim->buffer,
 						     PAGE_SIZE);
 			if (read <= 0)
 				break;
 
-			write = vringh_iov_push_iotlb(&rxq->vring, &rxq->iov,
+			write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
 						      vdpasim->buffer, read);
 			if (write <= 0)
 				break;
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 18/21] vdpa_sim: advertise VIRTIO_NET_F_MTU
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

We've already reported maximum mtu via config space, so let's
advertise the feature.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vdpa/vdpa_sim/vdpa_sim.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index fe4888dfb70f..8d051cf25f0a 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -66,7 +66,8 @@ struct vdpasim_virtqueue {
 static u64 vdpasim_features = (1ULL << VIRTIO_F_ANY_LAYOUT) |
 			      (1ULL << VIRTIO_F_VERSION_1)  |
 			      (1ULL << VIRTIO_F_ACCESS_PLATFORM) |
-			      (1ULL << VIRTIO_NET_F_MAC);
+			      (1ULL << VIRTIO_NET_F_MAC) |
+			      (1ULL << VIRTIO_NET_F_MTU);
 
 /* State of each vdpasim device */
 struct vdpasim {
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 18/21] vdpa_sim: advertise VIRTIO_NET_F_MTU
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

We've already reported maximum mtu via config space, so let's
advertise the feature.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vdpa/vdpa_sim/vdpa_sim.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index fe4888dfb70f..8d051cf25f0a 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -66,7 +66,8 @@ struct vdpasim_virtqueue {
 static u64 vdpasim_features = (1ULL << VIRTIO_F_ANY_LAYOUT) |
 			      (1ULL << VIRTIO_F_VERSION_1)  |
 			      (1ULL << VIRTIO_F_ACCESS_PLATFORM) |
-			      (1ULL << VIRTIO_NET_F_MAC);
+			      (1ULL << VIRTIO_NET_F_MAC) |
+			      (1ULL << VIRTIO_NET_F_MTU);
 
 /* State of each vdpasim device */
 struct vdpasim {
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 19/21] vdpa_sim: factor out buffer completion logic
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vdpa/vdpa_sim/vdpa_sim.c | 33 +++++++++++++++++---------------
 1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index 8d051cf25f0a..e901177c6dfe 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -159,6 +159,22 @@ static void vdpasim_reset(struct vdpasim *vdpasim)
 	++vdpasim->generation;
 }
 
+static void vdpasim_complete(struct vdpasim_virtqueue *vq, size_t len)
+{
+	/* Make sure data is wrote before advancing index */
+	smp_wmb();
+
+	vringh_complete_iotlb(&vq->vring, vq->head, len);
+
+	/* Make sure used is visible before rasing the interrupt. */
+	smp_wmb();
+
+	local_bh_disable();
+	if (vq->cb)
+		vq->cb(vq->private);
+	local_bh_enable();
+}
+
 static void vdpasim_work(struct work_struct *work)
 {
 	struct vdpasim *vdpasim = container_of(work, struct
@@ -207,21 +223,8 @@ static void vdpasim_work(struct work_struct *work)
 			total_write += write;
 		}
 
-		/* Make sure data is wrote before advancing index */
-		smp_wmb();
-
-		vringh_complete_iotlb(&txq->vring, txq->head, 0);
-		vringh_complete_iotlb(&rxq->vring, rxq->head, total_write);
-
-		/* Make sure used is visible before rasing the interrupt. */
-		smp_wmb();
-
-		local_bh_disable();
-		if (txq->cb)
-			txq->cb(txq->private);
-		if (rxq->cb)
-			rxq->cb(rxq->private);
-		local_bh_enable();
+		vdpasim_complete(txq, 0);
+		vdpasim_complete(rxq, total_write);
 
 		if (++pkts > 4) {
 			schedule_work(&vdpasim->work);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 19/21] vdpa_sim: factor out buffer completion logic
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vdpa/vdpa_sim/vdpa_sim.c | 33 +++++++++++++++++---------------
 1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index 8d051cf25f0a..e901177c6dfe 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -159,6 +159,22 @@ static void vdpasim_reset(struct vdpasim *vdpasim)
 	++vdpasim->generation;
 }
 
+static void vdpasim_complete(struct vdpasim_virtqueue *vq, size_t len)
+{
+	/* Make sure data is wrote before advancing index */
+	smp_wmb();
+
+	vringh_complete_iotlb(&vq->vring, vq->head, len);
+
+	/* Make sure used is visible before rasing the interrupt. */
+	smp_wmb();
+
+	local_bh_disable();
+	if (vq->cb)
+		vq->cb(vq->private);
+	local_bh_enable();
+}
+
 static void vdpasim_work(struct work_struct *work)
 {
 	struct vdpasim *vdpasim = container_of(work, struct
@@ -207,21 +223,8 @@ static void vdpasim_work(struct work_struct *work)
 			total_write += write;
 		}
 
-		/* Make sure data is wrote before advancing index */
-		smp_wmb();
-
-		vringh_complete_iotlb(&txq->vring, txq->head, 0);
-		vringh_complete_iotlb(&rxq->vring, rxq->head, total_write);
-
-		/* Make sure used is visible before rasing the interrupt. */
-		smp_wmb();
-
-		local_bh_disable();
-		if (txq->cb)
-			txq->cb(txq->private);
-		if (rxq->cb)
-			rxq->cb(rxq->private);
-		local_bh_enable();
+		vdpasim_complete(txq, 0);
+		vdpasim_complete(rxq, total_write);
 
 		if (++pkts > 4) {
 			schedule_work(&vdpasim->work);
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 20/21] vdpa_sim: filter destination mac address
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

This patch implements a simple unicast filter for vDPA simulator.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vdpa/vdpa_sim/vdpa_sim.c | 49 ++++++++++++++++++++------------
 1 file changed, 31 insertions(+), 18 deletions(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index e901177c6dfe..fe90a783bde4 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -175,6 +175,22 @@ static void vdpasim_complete(struct vdpasim_virtqueue *vq, size_t len)
 	local_bh_enable();
 }
 
+static bool receive_filter(struct vdpasim *vdpasim, size_t len)
+{
+	bool modern = vdpasim->features & (1ULL << VIRTIO_F_VERSION_1);
+	size_t hdr_len = modern ? sizeof(struct virtio_net_hdr_v1) :
+				  sizeof(struct virtio_net_hdr);
+
+	if (len < ETH_ALEN + hdr_len)
+		return false;
+
+	if (!strncmp(vdpasim->buffer + hdr_len,
+		     vdpasim->config.mac, ETH_ALEN))
+		return true;
+
+	return false;
+}
+
 static void vdpasim_work(struct work_struct *work)
 {
 	struct vdpasim *vdpasim = container_of(work, struct
@@ -182,7 +198,6 @@ static void vdpasim_work(struct work_struct *work)
 	struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
 	struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
 	ssize_t read, write;
-	size_t total_write;
 	int pkts = 0;
 	int err;
 
@@ -195,36 +210,34 @@ static void vdpasim_work(struct work_struct *work)
 		goto out;
 
 	while (true) {
-		total_write = 0;
 		err = vringh_getdesc_iotlb(&txq->vring, &txq->out_iov, NULL,
 					   &txq->head, GFP_ATOMIC);
 		if (err <= 0)
 			break;
 
+		read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
+					     vdpasim->buffer,
+					     PAGE_SIZE);
+
+		if (!receive_filter(vdpasim, read)) {
+			vdpasim_complete(txq, 0);
+			continue;
+		}
+
 		err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->in_iov,
 					   &rxq->head, GFP_ATOMIC);
 		if (err <= 0) {
-			vringh_complete_iotlb(&txq->vring, txq->head, 0);
+			vdpasim_complete(txq, 0);
 			break;
 		}
 
-		while (true) {
-			read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
-						     vdpasim->buffer,
-						     PAGE_SIZE);
-			if (read <= 0)
-				break;
-
-			write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
-						      vdpasim->buffer, read);
-			if (write <= 0)
-				break;
-
-			total_write += write;
-		}
+		write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
+					      vdpasim->buffer, read);
+		if (write <= 0)
+			break;
 
 		vdpasim_complete(txq, 0);
-		vdpasim_complete(rxq, total_write);
+		vdpasim_complete(rxq, write);
 
 		if (++pkts > 4) {
 			schedule_work(&vdpasim->work);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 20/21] vdpa_sim: filter destination mac address
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

This patch implements a simple unicast filter for vDPA simulator.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vdpa/vdpa_sim/vdpa_sim.c | 49 ++++++++++++++++++++------------
 1 file changed, 31 insertions(+), 18 deletions(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index e901177c6dfe..fe90a783bde4 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -175,6 +175,22 @@ static void vdpasim_complete(struct vdpasim_virtqueue *vq, size_t len)
 	local_bh_enable();
 }
 
+static bool receive_filter(struct vdpasim *vdpasim, size_t len)
+{
+	bool modern = vdpasim->features & (1ULL << VIRTIO_F_VERSION_1);
+	size_t hdr_len = modern ? sizeof(struct virtio_net_hdr_v1) :
+				  sizeof(struct virtio_net_hdr);
+
+	if (len < ETH_ALEN + hdr_len)
+		return false;
+
+	if (!strncmp(vdpasim->buffer + hdr_len,
+		     vdpasim->config.mac, ETH_ALEN))
+		return true;
+
+	return false;
+}
+
 static void vdpasim_work(struct work_struct *work)
 {
 	struct vdpasim *vdpasim = container_of(work, struct
@@ -182,7 +198,6 @@ static void vdpasim_work(struct work_struct *work)
 	struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
 	struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
 	ssize_t read, write;
-	size_t total_write;
 	int pkts = 0;
 	int err;
 
@@ -195,36 +210,34 @@ static void vdpasim_work(struct work_struct *work)
 		goto out;
 
 	while (true) {
-		total_write = 0;
 		err = vringh_getdesc_iotlb(&txq->vring, &txq->out_iov, NULL,
 					   &txq->head, GFP_ATOMIC);
 		if (err <= 0)
 			break;
 
+		read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
+					     vdpasim->buffer,
+					     PAGE_SIZE);
+
+		if (!receive_filter(vdpasim, read)) {
+			vdpasim_complete(txq, 0);
+			continue;
+		}
+
 		err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->in_iov,
 					   &rxq->head, GFP_ATOMIC);
 		if (err <= 0) {
-			vringh_complete_iotlb(&txq->vring, txq->head, 0);
+			vdpasim_complete(txq, 0);
 			break;
 		}
 
-		while (true) {
-			read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
-						     vdpasim->buffer,
-						     PAGE_SIZE);
-			if (read <= 0)
-				break;
-
-			write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
-						      vdpasim->buffer, read);
-			if (write <= 0)
-				break;
-
-			total_write += write;
-		}
+		write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
+					      vdpasim->buffer, read);
+		if (write <= 0)
+			break;
 
 		vdpasim_complete(txq, 0);
-		vdpasim_complete(rxq, total_write);
+		vdpasim_complete(rxq, write);
 
 		if (++pkts > 4) {
 			schedule_work(&vdpasim->work);
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 21/21] vdpasim: control virtqueue support
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  6:48   ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

This patch introduces the control virtqueue support for vDPA
simulator. This is a requirement for supporting advanced features like
multiqueue.

A requirement for control virtqueue is to isolate its memory access
from the rx/tx virtqueues. This is because when using vDPA device
for VM, the control virqueue is not directly assigned to VM. Userspace
(Qemu) will present a shadow control virtqueue to control for
recording the device states.

The isolation is done via the virtqueue groups and ASID support in
vDPA through vhost-vdpa. The simulator is extended to have:

1) three virtqueues: RXVQ, TXVQ and CVQ (control virtqueue)
2) two virtqueue groups: group 0 contains RXVQ and TXVQ; group 1
   contains CVQ
3) two address spaces and the simulator simply implements the address
   spaces by mapping it 1:1 to IOTLB.

For the VM use cases, userspace(Qemu) may set AS 0 to group 0 and AS 1
to group 1. So we have:

1) The IOTLB for virtqueue group 0 contains the mappings of guest, so
   RX and TX can be assigned to guest directly.
2) The IOTLB for virtqueue group 1 contains the mappings of CVQ which
   is the buffers that allocated and managed by VMM only. So CVQ of
   vhost-vdpa is visible to VMM only. And Guest can not access the CVQ
   of vhost-vdpa.

For the other use cases, since AS 0 is associated to all virtqueue
groups by default. All virtqueues share the same mapping by default.

To demonstrate the function, VIRITO_NET_F_CTRL_MACADDR is
implemented in the simulator for the driver to set mac address.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vdpa/vdpa_sim/vdpa_sim.c | 189 +++++++++++++++++++++++++++----
 1 file changed, 166 insertions(+), 23 deletions(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index fe90a783bde4..0fd06ac491cd 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -60,14 +60,18 @@ struct vdpasim_virtqueue {
 #define VDPASIM_QUEUE_MAX 256
 #define VDPASIM_DEVICE_ID 0x1
 #define VDPASIM_VENDOR_ID 0
-#define VDPASIM_VQ_NUM 0x2
+#define VDPASIM_VQ_NUM 0x3
+#define VDPASIM_AS_NUM 0x2
+#define VDPASIM_GROUP_NUM 0x2
 #define VDPASIM_NAME "vdpasim-netdev"
 
 static u64 vdpasim_features = (1ULL << VIRTIO_F_ANY_LAYOUT) |
 			      (1ULL << VIRTIO_F_VERSION_1)  |
 			      (1ULL << VIRTIO_F_ACCESS_PLATFORM) |
+			      (1ULL << VIRTIO_NET_F_MTU) |
 			      (1ULL << VIRTIO_NET_F_MAC) |
-			      (1ULL << VIRTIO_NET_F_MTU);
+			      (1ULL << VIRTIO_NET_F_CTRL_VQ) |
+			      (1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR);
 
 /* State of each vdpasim device */
 struct vdpasim {
@@ -147,11 +151,17 @@ static void vdpasim_reset(struct vdpasim *vdpasim)
 {
 	int i;
 
-	for (i = 0; i < VDPASIM_VQ_NUM; i++)
+	spin_lock(&vdpasim->iommu_lock);
+
+	for (i = 0; i < VDPASIM_VQ_NUM; i++) {
 		vdpasim_vq_reset(&vdpasim->vqs[i]);
+		vringh_set_iotlb(&vdpasim->vqs[i].vring,
+				 &vdpasim->iommu[0]);
+	}
 
-	spin_lock(&vdpasim->iommu_lock);
-	vhost_iotlb_reset(vdpasim->iommu);
+	for (i = 0; i < VDPASIM_AS_NUM; i++) {
+		vhost_iotlb_reset(&vdpasim->iommu[i]);
+	}
 	spin_unlock(&vdpasim->iommu_lock);
 
 	vdpasim->features = 0;
@@ -191,6 +201,81 @@ static bool receive_filter(struct vdpasim *vdpasim, size_t len)
 	return false;
 }
 
+virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
+					    u8 cmd)
+{
+	struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
+	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
+	size_t read;
+
+	switch (cmd) {
+	case VIRTIO_NET_CTRL_MAC_ADDR_SET:
+		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov,
+					     (void *)vdpasim->config.mac,
+					     ETH_ALEN);
+		if (read == ETH_ALEN)
+			status = VIRTIO_NET_OK;
+		break;
+	default:
+		break;
+	}
+
+	return status;
+}
+
+static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
+{
+	struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
+	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
+	struct virtio_net_ctrl_hdr ctrl;
+	size_t read, write;
+	int err;
+
+	if (!(vdpasim->features & (1ULL << VIRTIO_NET_F_CTRL_VQ)))
+		return;
+
+	if (!cvq->ready)
+		return;
+
+	while (true) {
+		err = vringh_getdesc_iotlb(&cvq->vring, &cvq->in_iov,
+					   &cvq->out_iov,
+					   &cvq->head, GFP_ATOMIC);
+		if (err <= 0)
+			break;
+
+		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov, &ctrl,
+					     sizeof(ctrl));
+		if (read != sizeof(ctrl))
+			break;
+
+		switch (ctrl.class) {
+		case VIRTIO_NET_CTRL_MAC:
+			status = vdpasim_handle_ctrl_mac(vdpasim, ctrl.cmd);
+			break;
+		default:
+			break;
+		}
+
+		/* Make sure data is wrote before advancing index */
+		smp_wmb();
+
+		write = vringh_iov_push_iotlb(&cvq->vring, &cvq->out_iov,
+					      &status, sizeof (status));
+		vringh_complete_iotlb(&cvq->vring, cvq->head, write);
+		vringh_kiov_cleanup(&cvq->in_iov);
+		vringh_kiov_cleanup(&cvq->out_iov);
+
+		/* Make sure used is visible before rasing the interrupt. */
+		smp_wmb();
+
+		local_bh_disable();
+		if (cvq->cb)
+			cvq->cb(cvq->private);
+		local_bh_enable();
+	}
+}
+
 static void vdpasim_work(struct work_struct *work)
 {
 	struct vdpasim *vdpasim = container_of(work, struct
@@ -276,7 +361,7 @@ static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
 				   unsigned long attrs)
 {
 	struct vdpasim *vdpasim = dev_to_sim(dev);
-	struct vhost_iotlb *iommu = vdpasim->iommu;
+	struct vhost_iotlb *iommu = &vdpasim->iommu[0];
 	u64 pa = (page_to_pfn(page) << PAGE_SHIFT) + offset;
 	int ret, perm = dir_to_perm(dir);
 
@@ -301,7 +386,7 @@ static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
 			       unsigned long attrs)
 {
 	struct vdpasim *vdpasim = dev_to_sim(dev);
-	struct vhost_iotlb *iommu = vdpasim->iommu;
+	struct vhost_iotlb *iommu = &vdpasim->iommu[0];
 
 	spin_lock(&vdpasim->iommu_lock);
 	vhost_iotlb_del_range(iommu, (u64)dma_addr,
@@ -314,7 +399,7 @@ static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
 				    unsigned long attrs)
 {
 	struct vdpasim *vdpasim = dev_to_sim(dev);
-	struct vhost_iotlb *iommu = vdpasim->iommu;
+	struct vhost_iotlb *iommu = &vdpasim->iommu[0];
 	void *addr = kmalloc(size, flag);
 	int ret;
 
@@ -344,7 +429,7 @@ static void vdpasim_free_coherent(struct device *dev, size_t size,
 				  unsigned long attrs)
 {
 	struct vdpasim *vdpasim = dev_to_sim(dev);
-	struct vhost_iotlb *iommu = vdpasim->iommu;
+	struct vhost_iotlb *iommu = &vdpasim->iommu[0];
 
 	spin_lock(&vdpasim->iommu_lock);
 	vhost_iotlb_del_range(iommu, (u64)dma_addr,
@@ -370,14 +455,17 @@ static struct vdpasim *vdpasim_create(void)
 	struct vdpasim *vdpasim;
 	struct device *dev;
 	int ret = -ENOMEM;
+	int i;
 
 	if (batch_mapping)
 		ops = &vdpasim_net_batch_config_ops;
 	else
 		ops = &vdpasim_net_config_ops;
 
+	/* 3 virtqueues, 2 address spaces, 2 virtqueue groups */
 	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
-				    VDPASIM_VQ_NUM, 1, 1);
+				    VDPASIM_VQ_NUM, VDPASIM_AS_NUM,
+				    VDPASIM_GROUP_NUM);
 	if (!vdpasim)
 		goto err_alloc;
 
@@ -391,10 +479,14 @@ static struct vdpasim *vdpasim_create(void)
 		goto err_iommu;
 	set_dma_ops(dev, &vdpasim_dma_ops);
 
-	vdpasim->iommu = vhost_iotlb_alloc(2048, 0);
+	vdpasim->iommu = kmalloc_array(VDPASIM_AS_NUM,
+				       sizeof(*vdpasim->iommu), GFP_KERNEL);
 	if (!vdpasim->iommu)
 		goto err_iommu;
 
+	for (i = 0; i < VDPASIM_AS_NUM; i++)
+		vhost_iotlb_init(&vdpasim->iommu[i], 0, 0);
+
 	vdpasim->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!vdpasim->buffer)
 		goto err_iommu;
@@ -409,8 +501,9 @@ static struct vdpasim *vdpasim_create(void)
 		eth_random_addr(vdpasim->config.mac);
 	}
 
-	vringh_set_iotlb(&vdpasim->vqs[0].vring, vdpasim->iommu);
-	vringh_set_iotlb(&vdpasim->vqs[1].vring, vdpasim->iommu);
+	/* Make sure that default ASID is zero */
+	for (i = 0; i < VDPASIM_VQ_NUM; i++)
+		vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0]);
 
 	vdpasim->vdpa.dma_dev = dev;
 	ret = vdpa_register_device(&vdpasim->vdpa);
@@ -452,7 +545,14 @@ static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx)
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
 
-	if (vq->ready)
+	if (idx == 2) {
+		/* Kernel virtio driver will do busy waiting for the
+		 * result, so we can't handle cvq in the workqueue.
+		 */
+		spin_lock(&vdpasim->lock);
+		vdpasim_handle_cvq(vdpasim);
+		spin_unlock(&vdpasim->lock);
+	} else if (vq->ready)
 		schedule_work(&vdpasim->work);
 }
 
@@ -518,7 +618,11 @@ static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
 
 static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
 {
-	return 0;
+	/* RX and TX belongs to group 0, CVQ belongs to group 1 */
+	if (idx == 2)
+		return 1;
+	else
+		return 0;
 }
 
 static u64 vdpasim_get_features(struct vdpa_device *vdpa)
@@ -624,20 +728,52 @@ static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
 	return range;
 }
 
+int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
+			   unsigned int asid)
+{
+	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
+	struct vhost_iotlb *iommu;
+	int i;
+
+	if (group > VDPASIM_GROUP_NUM)
+		return -EINVAL;
+
+	if (asid > VDPASIM_AS_NUM)
+		return -EINVAL;
+
+	iommu = &vdpasim->iommu[asid];
+
+	spin_lock(&vdpasim->lock);
+
+	for (i = 0; i < VDPASIM_VQ_NUM; i++)
+		if (vdpasim_get_vq_group(vdpa, i) == group)
+			vringh_set_iotlb(&vdpasim->vqs[i].vring, iommu);
+
+	spin_unlock(&vdpasim->lock);
+
+	return 0;
+}
+
 static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
 			   struct vhost_iotlb *iotlb)
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 	struct vhost_iotlb_map *map;
+	struct vhost_iotlb *iommu;
 	u64 start = 0ULL, last = 0ULL - 1;
 	int ret;
 
+	if (asid >= VDPASIM_AS_NUM)
+		return -EINVAL;
+
 	spin_lock(&vdpasim->iommu_lock);
-	vhost_iotlb_reset(vdpasim->iommu);
+
+	iommu = &vdpasim->iommu[asid];
+	vhost_iotlb_reset(iommu);
 
 	for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
 	     map = vhost_iotlb_itree_next(map, start, last)) {
-		ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
+		ret = vhost_iotlb_add_range(iommu, map->start,
 					    map->last, map->addr, map->perm);
 		if (ret)
 			goto err;
@@ -646,7 +782,7 @@ static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
 	return 0;
 
 err:
-	vhost_iotlb_reset(vdpasim->iommu);
+	vhost_iotlb_reset(iommu);
 	spin_unlock(&vdpasim->iommu_lock);
 	return ret;
 }
@@ -658,9 +794,12 @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 	int ret;
 
+	if (asid >= VDPASIM_AS_NUM)
+		return -EINVAL;
+
 	spin_lock(&vdpasim->iommu_lock);
-	ret = vhost_iotlb_add_range(vdpasim->iommu, iova, iova + size - 1, pa,
-				    perm);
+	ret = vhost_iotlb_add_range(&vdpasim->iommu[asid], iova,
+				    iova + size - 1, pa, perm);
 	spin_unlock(&vdpasim->iommu_lock);
 
 	return ret;
@@ -671,8 +810,11 @@ static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 
+	if (asid >= VDPASIM_AS_NUM)
+		return -EINVAL;
+
 	spin_lock(&vdpasim->iommu_lock);
-	vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
+	vhost_iotlb_del_range(&vdpasim->iommu[asid], iova, iova + size - 1);
 	spin_unlock(&vdpasim->iommu_lock);
 
 	return 0;
@@ -684,8 +826,7 @@ static void vdpasim_free(struct vdpa_device *vdpa)
 
 	cancel_work_sync(&vdpasim->work);
 	kfree(vdpasim->buffer);
-	if (vdpasim->iommu)
-		vhost_iotlb_free(vdpasim->iommu);
+	vhost_iotlb_free(vdpasim->iommu);
 }
 
 static const struct vdpa_config_ops vdpasim_net_config_ops = {
@@ -711,6 +852,7 @@ static const struct vdpa_config_ops vdpasim_net_config_ops = {
 	.set_config             = vdpasim_set_config,
 	.get_generation         = vdpasim_get_generation,
 	.get_iova_range         = vdpasim_get_iova_range,
+	.set_group_asid         = vdpasim_set_group_asid,
 	.dma_map                = vdpasim_dma_map,
 	.dma_unmap              = vdpasim_dma_unmap,
 	.free                   = vdpasim_free,
@@ -739,6 +881,7 @@ static const struct vdpa_config_ops vdpasim_net_batch_config_ops = {
 	.set_config             = vdpasim_set_config,
 	.get_generation         = vdpasim_get_generation,
 	.get_iova_range         = vdpasim_get_iova_range,
+	.set_group_asid         = vdpasim_set_group_asid,
 	.set_map                = vdpasim_set_map,
 	.free                   = vdpasim_free,
 };
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [PATCH 21/21] vdpasim: control virtqueue support
@ 2020-12-16  6:48   ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-16  6:48 UTC (permalink / raw)
  To: mst, jasowang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

This patch introduces the control virtqueue support for vDPA
simulator. This is a requirement for supporting advanced features like
multiqueue.

A requirement for control virtqueue is to isolate its memory access
from the rx/tx virtqueues. This is because when using vDPA device
for VM, the control virqueue is not directly assigned to VM. Userspace
(Qemu) will present a shadow control virtqueue to control for
recording the device states.

The isolation is done via the virtqueue groups and ASID support in
vDPA through vhost-vdpa. The simulator is extended to have:

1) three virtqueues: RXVQ, TXVQ and CVQ (control virtqueue)
2) two virtqueue groups: group 0 contains RXVQ and TXVQ; group 1
   contains CVQ
3) two address spaces and the simulator simply implements the address
   spaces by mapping it 1:1 to IOTLB.

For the VM use cases, userspace(Qemu) may set AS 0 to group 0 and AS 1
to group 1. So we have:

1) The IOTLB for virtqueue group 0 contains the mappings of guest, so
   RX and TX can be assigned to guest directly.
2) The IOTLB for virtqueue group 1 contains the mappings of CVQ which
   is the buffers that allocated and managed by VMM only. So CVQ of
   vhost-vdpa is visible to VMM only. And Guest can not access the CVQ
   of vhost-vdpa.

For the other use cases, since AS 0 is associated to all virtqueue
groups by default. All virtqueues share the same mapping by default.

To demonstrate the function, VIRITO_NET_F_CTRL_MACADDR is
implemented in the simulator for the driver to set mac address.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/vdpa/vdpa_sim/vdpa_sim.c | 189 +++++++++++++++++++++++++++----
 1 file changed, 166 insertions(+), 23 deletions(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index fe90a783bde4..0fd06ac491cd 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -60,14 +60,18 @@ struct vdpasim_virtqueue {
 #define VDPASIM_QUEUE_MAX 256
 #define VDPASIM_DEVICE_ID 0x1
 #define VDPASIM_VENDOR_ID 0
-#define VDPASIM_VQ_NUM 0x2
+#define VDPASIM_VQ_NUM 0x3
+#define VDPASIM_AS_NUM 0x2
+#define VDPASIM_GROUP_NUM 0x2
 #define VDPASIM_NAME "vdpasim-netdev"
 
 static u64 vdpasim_features = (1ULL << VIRTIO_F_ANY_LAYOUT) |
 			      (1ULL << VIRTIO_F_VERSION_1)  |
 			      (1ULL << VIRTIO_F_ACCESS_PLATFORM) |
+			      (1ULL << VIRTIO_NET_F_MTU) |
 			      (1ULL << VIRTIO_NET_F_MAC) |
-			      (1ULL << VIRTIO_NET_F_MTU);
+			      (1ULL << VIRTIO_NET_F_CTRL_VQ) |
+			      (1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR);
 
 /* State of each vdpasim device */
 struct vdpasim {
@@ -147,11 +151,17 @@ static void vdpasim_reset(struct vdpasim *vdpasim)
 {
 	int i;
 
-	for (i = 0; i < VDPASIM_VQ_NUM; i++)
+	spin_lock(&vdpasim->iommu_lock);
+
+	for (i = 0; i < VDPASIM_VQ_NUM; i++) {
 		vdpasim_vq_reset(&vdpasim->vqs[i]);
+		vringh_set_iotlb(&vdpasim->vqs[i].vring,
+				 &vdpasim->iommu[0]);
+	}
 
-	spin_lock(&vdpasim->iommu_lock);
-	vhost_iotlb_reset(vdpasim->iommu);
+	for (i = 0; i < VDPASIM_AS_NUM; i++) {
+		vhost_iotlb_reset(&vdpasim->iommu[i]);
+	}
 	spin_unlock(&vdpasim->iommu_lock);
 
 	vdpasim->features = 0;
@@ -191,6 +201,81 @@ static bool receive_filter(struct vdpasim *vdpasim, size_t len)
 	return false;
 }
 
+virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
+					    u8 cmd)
+{
+	struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
+	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
+	size_t read;
+
+	switch (cmd) {
+	case VIRTIO_NET_CTRL_MAC_ADDR_SET:
+		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov,
+					     (void *)vdpasim->config.mac,
+					     ETH_ALEN);
+		if (read == ETH_ALEN)
+			status = VIRTIO_NET_OK;
+		break;
+	default:
+		break;
+	}
+
+	return status;
+}
+
+static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
+{
+	struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
+	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
+	struct virtio_net_ctrl_hdr ctrl;
+	size_t read, write;
+	int err;
+
+	if (!(vdpasim->features & (1ULL << VIRTIO_NET_F_CTRL_VQ)))
+		return;
+
+	if (!cvq->ready)
+		return;
+
+	while (true) {
+		err = vringh_getdesc_iotlb(&cvq->vring, &cvq->in_iov,
+					   &cvq->out_iov,
+					   &cvq->head, GFP_ATOMIC);
+		if (err <= 0)
+			break;
+
+		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov, &ctrl,
+					     sizeof(ctrl));
+		if (read != sizeof(ctrl))
+			break;
+
+		switch (ctrl.class) {
+		case VIRTIO_NET_CTRL_MAC:
+			status = vdpasim_handle_ctrl_mac(vdpasim, ctrl.cmd);
+			break;
+		default:
+			break;
+		}
+
+		/* Make sure data is wrote before advancing index */
+		smp_wmb();
+
+		write = vringh_iov_push_iotlb(&cvq->vring, &cvq->out_iov,
+					      &status, sizeof (status));
+		vringh_complete_iotlb(&cvq->vring, cvq->head, write);
+		vringh_kiov_cleanup(&cvq->in_iov);
+		vringh_kiov_cleanup(&cvq->out_iov);
+
+		/* Make sure used is visible before rasing the interrupt. */
+		smp_wmb();
+
+		local_bh_disable();
+		if (cvq->cb)
+			cvq->cb(cvq->private);
+		local_bh_enable();
+	}
+}
+
 static void vdpasim_work(struct work_struct *work)
 {
 	struct vdpasim *vdpasim = container_of(work, struct
@@ -276,7 +361,7 @@ static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
 				   unsigned long attrs)
 {
 	struct vdpasim *vdpasim = dev_to_sim(dev);
-	struct vhost_iotlb *iommu = vdpasim->iommu;
+	struct vhost_iotlb *iommu = &vdpasim->iommu[0];
 	u64 pa = (page_to_pfn(page) << PAGE_SHIFT) + offset;
 	int ret, perm = dir_to_perm(dir);
 
@@ -301,7 +386,7 @@ static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
 			       unsigned long attrs)
 {
 	struct vdpasim *vdpasim = dev_to_sim(dev);
-	struct vhost_iotlb *iommu = vdpasim->iommu;
+	struct vhost_iotlb *iommu = &vdpasim->iommu[0];
 
 	spin_lock(&vdpasim->iommu_lock);
 	vhost_iotlb_del_range(iommu, (u64)dma_addr,
@@ -314,7 +399,7 @@ static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
 				    unsigned long attrs)
 {
 	struct vdpasim *vdpasim = dev_to_sim(dev);
-	struct vhost_iotlb *iommu = vdpasim->iommu;
+	struct vhost_iotlb *iommu = &vdpasim->iommu[0];
 	void *addr = kmalloc(size, flag);
 	int ret;
 
@@ -344,7 +429,7 @@ static void vdpasim_free_coherent(struct device *dev, size_t size,
 				  unsigned long attrs)
 {
 	struct vdpasim *vdpasim = dev_to_sim(dev);
-	struct vhost_iotlb *iommu = vdpasim->iommu;
+	struct vhost_iotlb *iommu = &vdpasim->iommu[0];
 
 	spin_lock(&vdpasim->iommu_lock);
 	vhost_iotlb_del_range(iommu, (u64)dma_addr,
@@ -370,14 +455,17 @@ static struct vdpasim *vdpasim_create(void)
 	struct vdpasim *vdpasim;
 	struct device *dev;
 	int ret = -ENOMEM;
+	int i;
 
 	if (batch_mapping)
 		ops = &vdpasim_net_batch_config_ops;
 	else
 		ops = &vdpasim_net_config_ops;
 
+	/* 3 virtqueues, 2 address spaces, 2 virtqueue groups */
 	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
-				    VDPASIM_VQ_NUM, 1, 1);
+				    VDPASIM_VQ_NUM, VDPASIM_AS_NUM,
+				    VDPASIM_GROUP_NUM);
 	if (!vdpasim)
 		goto err_alloc;
 
@@ -391,10 +479,14 @@ static struct vdpasim *vdpasim_create(void)
 		goto err_iommu;
 	set_dma_ops(dev, &vdpasim_dma_ops);
 
-	vdpasim->iommu = vhost_iotlb_alloc(2048, 0);
+	vdpasim->iommu = kmalloc_array(VDPASIM_AS_NUM,
+				       sizeof(*vdpasim->iommu), GFP_KERNEL);
 	if (!vdpasim->iommu)
 		goto err_iommu;
 
+	for (i = 0; i < VDPASIM_AS_NUM; i++)
+		vhost_iotlb_init(&vdpasim->iommu[i], 0, 0);
+
 	vdpasim->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!vdpasim->buffer)
 		goto err_iommu;
@@ -409,8 +501,9 @@ static struct vdpasim *vdpasim_create(void)
 		eth_random_addr(vdpasim->config.mac);
 	}
 
-	vringh_set_iotlb(&vdpasim->vqs[0].vring, vdpasim->iommu);
-	vringh_set_iotlb(&vdpasim->vqs[1].vring, vdpasim->iommu);
+	/* Make sure that default ASID is zero */
+	for (i = 0; i < VDPASIM_VQ_NUM; i++)
+		vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0]);
 
 	vdpasim->vdpa.dma_dev = dev;
 	ret = vdpa_register_device(&vdpasim->vdpa);
@@ -452,7 +545,14 @@ static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx)
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
 
-	if (vq->ready)
+	if (idx == 2) {
+		/* Kernel virtio driver will do busy waiting for the
+		 * result, so we can't handle cvq in the workqueue.
+		 */
+		spin_lock(&vdpasim->lock);
+		vdpasim_handle_cvq(vdpasim);
+		spin_unlock(&vdpasim->lock);
+	} else if (vq->ready)
 		schedule_work(&vdpasim->work);
 }
 
@@ -518,7 +618,11 @@ static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
 
 static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
 {
-	return 0;
+	/* RX and TX belongs to group 0, CVQ belongs to group 1 */
+	if (idx == 2)
+		return 1;
+	else
+		return 0;
 }
 
 static u64 vdpasim_get_features(struct vdpa_device *vdpa)
@@ -624,20 +728,52 @@ static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
 	return range;
 }
 
+int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
+			   unsigned int asid)
+{
+	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
+	struct vhost_iotlb *iommu;
+	int i;
+
+	if (group > VDPASIM_GROUP_NUM)
+		return -EINVAL;
+
+	if (asid > VDPASIM_AS_NUM)
+		return -EINVAL;
+
+	iommu = &vdpasim->iommu[asid];
+
+	spin_lock(&vdpasim->lock);
+
+	for (i = 0; i < VDPASIM_VQ_NUM; i++)
+		if (vdpasim_get_vq_group(vdpa, i) == group)
+			vringh_set_iotlb(&vdpasim->vqs[i].vring, iommu);
+
+	spin_unlock(&vdpasim->lock);
+
+	return 0;
+}
+
 static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
 			   struct vhost_iotlb *iotlb)
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 	struct vhost_iotlb_map *map;
+	struct vhost_iotlb *iommu;
 	u64 start = 0ULL, last = 0ULL - 1;
 	int ret;
 
+	if (asid >= VDPASIM_AS_NUM)
+		return -EINVAL;
+
 	spin_lock(&vdpasim->iommu_lock);
-	vhost_iotlb_reset(vdpasim->iommu);
+
+	iommu = &vdpasim->iommu[asid];
+	vhost_iotlb_reset(iommu);
 
 	for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
 	     map = vhost_iotlb_itree_next(map, start, last)) {
-		ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
+		ret = vhost_iotlb_add_range(iommu, map->start,
 					    map->last, map->addr, map->perm);
 		if (ret)
 			goto err;
@@ -646,7 +782,7 @@ static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
 	return 0;
 
 err:
-	vhost_iotlb_reset(vdpasim->iommu);
+	vhost_iotlb_reset(iommu);
 	spin_unlock(&vdpasim->iommu_lock);
 	return ret;
 }
@@ -658,9 +794,12 @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 	int ret;
 
+	if (asid >= VDPASIM_AS_NUM)
+		return -EINVAL;
+
 	spin_lock(&vdpasim->iommu_lock);
-	ret = vhost_iotlb_add_range(vdpasim->iommu, iova, iova + size - 1, pa,
-				    perm);
+	ret = vhost_iotlb_add_range(&vdpasim->iommu[asid], iova,
+				    iova + size - 1, pa, perm);
 	spin_unlock(&vdpasim->iommu_lock);
 
 	return ret;
@@ -671,8 +810,11 @@ static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 
+	if (asid >= VDPASIM_AS_NUM)
+		return -EINVAL;
+
 	spin_lock(&vdpasim->iommu_lock);
-	vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
+	vhost_iotlb_del_range(&vdpasim->iommu[asid], iova, iova + size - 1);
 	spin_unlock(&vdpasim->iommu_lock);
 
 	return 0;
@@ -684,8 +826,7 @@ static void vdpasim_free(struct vdpa_device *vdpa)
 
 	cancel_work_sync(&vdpasim->work);
 	kfree(vdpasim->buffer);
-	if (vdpasim->iommu)
-		vhost_iotlb_free(vdpasim->iommu);
+	vhost_iotlb_free(vdpasim->iommu);
 }
 
 static const struct vdpa_config_ops vdpasim_net_config_ops = {
@@ -711,6 +852,7 @@ static const struct vdpa_config_ops vdpasim_net_config_ops = {
 	.set_config             = vdpasim_set_config,
 	.get_generation         = vdpasim_get_generation,
 	.get_iova_range         = vdpasim_get_iova_range,
+	.set_group_asid         = vdpasim_set_group_asid,
 	.dma_map                = vdpasim_dma_map,
 	.dma_unmap              = vdpasim_dma_unmap,
 	.free                   = vdpasim_free,
@@ -739,6 +881,7 @@ static const struct vdpa_config_ops vdpasim_net_batch_config_ops = {
 	.set_config             = vdpasim_set_config,
 	.get_generation         = vdpasim_get_generation,
 	.get_iova_range         = vdpasim_get_iova_range,
+	.set_group_asid         = vdpasim_set_group_asid,
 	.set_map                = vdpasim_set_map,
 	.free                   = vdpasim_free,
 };
-- 
2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply related	[flat|nested] 136+ messages in thread

* Re: [PATCH 00/21] Control VQ support in vDPA
  2020-12-16  6:47 ` Jason Wang
@ 2020-12-16  9:47   ` Michael S. Tsirkin
  -1 siblings, 0 replies; 136+ messages in thread
From: Michael S. Tsirkin @ 2020-12-16  9:47 UTC (permalink / raw)
  To: Jason Wang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

On Wed, Dec 16, 2020 at 02:47:57PM +0800, Jason Wang wrote:
> Hi All:
> 
> This series tries to add the support for control virtqueue in vDPA.
> 
> Control virtqueue is used by networking device for accepting various
> commands from the driver. It's a must to support multiqueue and other
> configurations.
> 
> When used by vhost-vDPA bus driver for VM, the control virtqueue
> should be shadowed via userspace VMM (Qemu) instead of being assigned
> directly to Guest. This is because Qemu needs to know the device state
> in order to start and stop device correctly (e.g for Live Migration).
> 
> This requies to isolate the memory mapping for control virtqueue
> presented by vhost-vDPA to prevent guest from accesing it directly.
> To achieve this, vDPA introduce two new abstractions:
> 
> - address space: identified through address space id (ASID) and a set
>                  of memory mapping in maintained
> - virtqueue group: the minimal set of virtqueues that must share an
>                  address space

How will this support the pretty common case where control vq
is programmed by the kernel through the PF, and others by the VFs?


I actually thought the way to support it is by exposing
something like an "inject buffers" API which sends data to a given VQ.
Maybe an ioctl, and maybe down the road uio ring can support batching
these ....


> 
> Device needs to advertise the following attributes to vDPA:
> 
> - the number of address spaces supported in the device
> - the number of virtqueue groups supported in the device
> - the mappings from a specific virtqueue to its virtqueue groups
> 
> The mappings from virtqueue to virtqueue groups is fixed and defined
> by vDPA device driver. E.g:
> 
> - For the device that has hardware ASID support, it can simply
>   advertise a per virtqueue virtqueue group.
> - For the device that does not have hardware ASID support, it can
>   simply advertise a single virtqueue group that contains all
>   virtqueues. Or if it wants a software emulated control virtqueue, it
>   can advertise two virtqueue groups, one is for cvq, another is for
>   the rest virtqueues.
> 
> vDPA also allow to change the association between virtqueue group and
> address space. So in the case of control virtqueue, userspace
> VMM(Qemu) may use a dedicated address space for the control virtqueue
> group to isolate the memory mapping.
> 
> The vhost/vhost-vDPA is also extend for the userspace to:
> 
> - query the number of virtqueue groups and address spaces supported by
>   the device
> - query the virtqueue group for a specific virtqueue
> - assocaite a virtqueue group with an address space
> - send ASID based IOTLB commands
> 
> This will help userspace VMM(Qemu) to detect whether the control vq
> could be supported and isolate memory mappings of control virtqueue
> from the others.
> 
> To demonstrate the usage, vDPA simulator is extended to support
> setting MAC address via a emulated control virtqueue.
> 
> Please review.
> 
> Changes since RFC:
> 
> - tweak vhost uAPI documentation
> - switch to use device specific IOTLB really in patch 4
> - tweak the commit log
> - fix that ASID in vhost is claimed to be 32 actually but 16bit
>   actually
> - fix use after free when using ASID with IOTLB batching requests
> - switch to use Stefano's patch for having separated iov
> - remove unused "used_as" variable
> - fix the iotlb/asid checking in vhost_vdpa_unmap()
> 
> Thanks
> 
> Jason Wang (20):
>   vhost: move the backend feature bits to vhost_types.h
>   virtio-vdpa: don't set callback if virtio doesn't need it
>   vhost-vdpa: passing iotlb to IOMMU mapping helpers
>   vhost-vdpa: switch to use vhost-vdpa specific IOTLB
>   vdpa: add the missing comment for nvqs in struct vdpa_device
>   vdpa: introduce virtqueue groups
>   vdpa: multiple address spaces support
>   vdpa: introduce config operations for associating ASID to a virtqueue
>     group
>   vhost_iotlb: split out IOTLB initialization
>   vhost: support ASID in IOTLB API
>   vhost-vdpa: introduce asid based IOTLB
>   vhost-vdpa: introduce uAPI to get the number of virtqueue groups
>   vhost-vdpa: introduce uAPI to get the number of address spaces
>   vhost-vdpa: uAPI to get virtqueue group id
>   vhost-vdpa: introduce uAPI to set group ASID
>   vhost-vdpa: support ASID based IOTLB API
>   vdpa_sim: advertise VIRTIO_NET_F_MTU
>   vdpa_sim: factor out buffer completion logic
>   vdpa_sim: filter destination mac address
>   vdpasim: control virtqueue support
> 
> Stefano Garzarella (1):
>   vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov
> 
>  drivers/vdpa/ifcvf/ifcvf_main.c   |   9 +-
>  drivers/vdpa/mlx5/net/mlx5_vnet.c |  11 +-
>  drivers/vdpa/vdpa.c               |   8 +-
>  drivers/vdpa/vdpa_sim/vdpa_sim.c  | 292 ++++++++++++++++++++++++------
>  drivers/vhost/iotlb.c             |  23 ++-
>  drivers/vhost/vdpa.c              | 246 ++++++++++++++++++++-----
>  drivers/vhost/vhost.c             |  23 ++-
>  drivers/vhost/vhost.h             |   4 +-
>  drivers/virtio/virtio_vdpa.c      |   2 +-
>  include/linux/vdpa.h              |  42 ++++-
>  include/linux/vhost_iotlb.h       |   2 +
>  include/uapi/linux/vhost.h        |  25 ++-
>  include/uapi/linux/vhost_types.h  |  10 +-
>  13 files changed, 561 insertions(+), 136 deletions(-)
> 
> -- 
> 2.25.1


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 00/21] Control VQ support in vDPA
@ 2020-12-16  9:47   ` Michael S. Tsirkin
  0 siblings, 0 replies; 136+ messages in thread
From: Michael S. Tsirkin @ 2020-12-16  9:47 UTC (permalink / raw)
  To: Jason Wang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

On Wed, Dec 16, 2020 at 02:47:57PM +0800, Jason Wang wrote:
> Hi All:
> 
> This series tries to add the support for control virtqueue in vDPA.
> 
> Control virtqueue is used by networking device for accepting various
> commands from the driver. It's a must to support multiqueue and other
> configurations.
> 
> When used by vhost-vDPA bus driver for VM, the control virtqueue
> should be shadowed via userspace VMM (Qemu) instead of being assigned
> directly to Guest. This is because Qemu needs to know the device state
> in order to start and stop device correctly (e.g for Live Migration).
> 
> This requies to isolate the memory mapping for control virtqueue
> presented by vhost-vDPA to prevent guest from accesing it directly.
> To achieve this, vDPA introduce two new abstractions:
> 
> - address space: identified through address space id (ASID) and a set
>                  of memory mapping in maintained
> - virtqueue group: the minimal set of virtqueues that must share an
>                  address space

How will this support the pretty common case where control vq
is programmed by the kernel through the PF, and others by the VFs?


I actually thought the way to support it is by exposing
something like an "inject buffers" API which sends data to a given VQ.
Maybe an ioctl, and maybe down the road uio ring can support batching
these ....


> 
> Device needs to advertise the following attributes to vDPA:
> 
> - the number of address spaces supported in the device
> - the number of virtqueue groups supported in the device
> - the mappings from a specific virtqueue to its virtqueue groups
> 
> The mappings from virtqueue to virtqueue groups is fixed and defined
> by vDPA device driver. E.g:
> 
> - For the device that has hardware ASID support, it can simply
>   advertise a per virtqueue virtqueue group.
> - For the device that does not have hardware ASID support, it can
>   simply advertise a single virtqueue group that contains all
>   virtqueues. Or if it wants a software emulated control virtqueue, it
>   can advertise two virtqueue groups, one is for cvq, another is for
>   the rest virtqueues.
> 
> vDPA also allow to change the association between virtqueue group and
> address space. So in the case of control virtqueue, userspace
> VMM(Qemu) may use a dedicated address space for the control virtqueue
> group to isolate the memory mapping.
> 
> The vhost/vhost-vDPA is also extend for the userspace to:
> 
> - query the number of virtqueue groups and address spaces supported by
>   the device
> - query the virtqueue group for a specific virtqueue
> - assocaite a virtqueue group with an address space
> - send ASID based IOTLB commands
> 
> This will help userspace VMM(Qemu) to detect whether the control vq
> could be supported and isolate memory mappings of control virtqueue
> from the others.
> 
> To demonstrate the usage, vDPA simulator is extended to support
> setting MAC address via a emulated control virtqueue.
> 
> Please review.
> 
> Changes since RFC:
> 
> - tweak vhost uAPI documentation
> - switch to use device specific IOTLB really in patch 4
> - tweak the commit log
> - fix that ASID in vhost is claimed to be 32 actually but 16bit
>   actually
> - fix use after free when using ASID with IOTLB batching requests
> - switch to use Stefano's patch for having separated iov
> - remove unused "used_as" variable
> - fix the iotlb/asid checking in vhost_vdpa_unmap()
> 
> Thanks
> 
> Jason Wang (20):
>   vhost: move the backend feature bits to vhost_types.h
>   virtio-vdpa: don't set callback if virtio doesn't need it
>   vhost-vdpa: passing iotlb to IOMMU mapping helpers
>   vhost-vdpa: switch to use vhost-vdpa specific IOTLB
>   vdpa: add the missing comment for nvqs in struct vdpa_device
>   vdpa: introduce virtqueue groups
>   vdpa: multiple address spaces support
>   vdpa: introduce config operations for associating ASID to a virtqueue
>     group
>   vhost_iotlb: split out IOTLB initialization
>   vhost: support ASID in IOTLB API
>   vhost-vdpa: introduce asid based IOTLB
>   vhost-vdpa: introduce uAPI to get the number of virtqueue groups
>   vhost-vdpa: introduce uAPI to get the number of address spaces
>   vhost-vdpa: uAPI to get virtqueue group id
>   vhost-vdpa: introduce uAPI to set group ASID
>   vhost-vdpa: support ASID based IOTLB API
>   vdpa_sim: advertise VIRTIO_NET_F_MTU
>   vdpa_sim: factor out buffer completion logic
>   vdpa_sim: filter destination mac address
>   vdpasim: control virtqueue support
> 
> Stefano Garzarella (1):
>   vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov
> 
>  drivers/vdpa/ifcvf/ifcvf_main.c   |   9 +-
>  drivers/vdpa/mlx5/net/mlx5_vnet.c |  11 +-
>  drivers/vdpa/vdpa.c               |   8 +-
>  drivers/vdpa/vdpa_sim/vdpa_sim.c  | 292 ++++++++++++++++++++++++------
>  drivers/vhost/iotlb.c             |  23 ++-
>  drivers/vhost/vdpa.c              | 246 ++++++++++++++++++++-----
>  drivers/vhost/vhost.c             |  23 ++-
>  drivers/vhost/vhost.h             |   4 +-
>  drivers/virtio/virtio_vdpa.c      |   2 +-
>  include/linux/vdpa.h              |  42 ++++-
>  include/linux/vhost_iotlb.h       |   2 +
>  include/uapi/linux/vhost.h        |  25 ++-
>  include/uapi/linux/vhost_types.h  |  10 +-
>  13 files changed, 561 insertions(+), 136 deletions(-)
> 
> -- 
> 2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 00/21] Control VQ support in vDPA
  2020-12-16  9:47   ` Michael S. Tsirkin
@ 2020-12-17  3:30     ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-17  3:30 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare


On 2020/12/16 下午5:47, Michael S. Tsirkin wrote:
> On Wed, Dec 16, 2020 at 02:47:57PM +0800, Jason Wang wrote:
>> Hi All:
>>
>> This series tries to add the support for control virtqueue in vDPA.
>>
>> Control virtqueue is used by networking device for accepting various
>> commands from the driver. It's a must to support multiqueue and other
>> configurations.
>>
>> When used by vhost-vDPA bus driver for VM, the control virtqueue
>> should be shadowed via userspace VMM (Qemu) instead of being assigned
>> directly to Guest. This is because Qemu needs to know the device state
>> in order to start and stop device correctly (e.g for Live Migration).
>>
>> This requies to isolate the memory mapping for control virtqueue
>> presented by vhost-vDPA to prevent guest from accesing it directly.
>> To achieve this, vDPA introduce two new abstractions:
>>
>> - address space: identified through address space id (ASID) and a set
>>                   of memory mapping in maintained
>> - virtqueue group: the minimal set of virtqueues that must share an
>>                   address space
> How will this support the pretty common case where control vq
> is programmed by the kernel through the PF, and others by the VFs?


In this case, the VF parent need to provide a software control vq and 
decode the command then send them to VF.


>
>
> I actually thought the way to support it is by exposing
> something like an "inject buffers" API which sends data to a given VQ.
> Maybe an ioctl, and maybe down the road uio ring can support batching
> these ....


So the virtuqueue allows the request to be processed asynchronously (e.g 
driver may choose to use interrupt for control vq). This means we need 
to support that in uAPI level. And if we manage to do that, it's just 
another type of virtqueue.

For virtio-vDPA, this also means the extensions for queue processing 
which is a functional duplication. Using what proposed in this series, 
we don't need any changes for kernel virtio drivers.

What's more important, this series could be used for future features 
that requires DMA isolation between virtqueues:

- report dirty pages via virtqueue
- sub function level device slicing

...

Thanks


>
>
>> Device needs to advertise the following attributes to vDPA:
>>
>> - the number of address spaces supported in the device
>> - the number of virtqueue groups supported in the device
>> - the mappings from a specific virtqueue to its virtqueue groups
>>
>> The mappings from virtqueue to virtqueue groups is fixed and defined
>> by vDPA device driver. E.g:
>>
>> - For the device that has hardware ASID support, it can simply
>>    advertise a per virtqueue virtqueue group.
>> - For the device that does not have hardware ASID support, it can
>>    simply advertise a single virtqueue group that contains all
>>    virtqueues. Or if it wants a software emulated control virtqueue, it
>>    can advertise two virtqueue groups, one is for cvq, another is for
>>    the rest virtqueues.
>>
>> vDPA also allow to change the association between virtqueue group and
>> address space. So in the case of control virtqueue, userspace
>> VMM(Qemu) may use a dedicated address space for the control virtqueue
>> group to isolate the memory mapping.
>>
>> The vhost/vhost-vDPA is also extend for the userspace to:
>>
>> - query the number of virtqueue groups and address spaces supported by
>>    the device
>> - query the virtqueue group for a specific virtqueue
>> - assocaite a virtqueue group with an address space
>> - send ASID based IOTLB commands
>>
>> This will help userspace VMM(Qemu) to detect whether the control vq
>> could be supported and isolate memory mappings of control virtqueue
>> from the others.
>>
>> To demonstrate the usage, vDPA simulator is extended to support
>> setting MAC address via a emulated control virtqueue.
>>
>> Please review.
>>
>> Changes since RFC:
>>
>> - tweak vhost uAPI documentation
>> - switch to use device specific IOTLB really in patch 4
>> - tweak the commit log
>> - fix that ASID in vhost is claimed to be 32 actually but 16bit
>>    actually
>> - fix use after free when using ASID with IOTLB batching requests
>> - switch to use Stefano's patch for having separated iov
>> - remove unused "used_as" variable
>> - fix the iotlb/asid checking in vhost_vdpa_unmap()
>>
>> Thanks
>>
>> Jason Wang (20):
>>    vhost: move the backend feature bits to vhost_types.h
>>    virtio-vdpa: don't set callback if virtio doesn't need it
>>    vhost-vdpa: passing iotlb to IOMMU mapping helpers
>>    vhost-vdpa: switch to use vhost-vdpa specific IOTLB
>>    vdpa: add the missing comment for nvqs in struct vdpa_device
>>    vdpa: introduce virtqueue groups
>>    vdpa: multiple address spaces support
>>    vdpa: introduce config operations for associating ASID to a virtqueue
>>      group
>>    vhost_iotlb: split out IOTLB initialization
>>    vhost: support ASID in IOTLB API
>>    vhost-vdpa: introduce asid based IOTLB
>>    vhost-vdpa: introduce uAPI to get the number of virtqueue groups
>>    vhost-vdpa: introduce uAPI to get the number of address spaces
>>    vhost-vdpa: uAPI to get virtqueue group id
>>    vhost-vdpa: introduce uAPI to set group ASID
>>    vhost-vdpa: support ASID based IOTLB API
>>    vdpa_sim: advertise VIRTIO_NET_F_MTU
>>    vdpa_sim: factor out buffer completion logic
>>    vdpa_sim: filter destination mac address
>>    vdpasim: control virtqueue support
>>
>> Stefano Garzarella (1):
>>    vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov
>>
>>   drivers/vdpa/ifcvf/ifcvf_main.c   |   9 +-
>>   drivers/vdpa/mlx5/net/mlx5_vnet.c |  11 +-
>>   drivers/vdpa/vdpa.c               |   8 +-
>>   drivers/vdpa/vdpa_sim/vdpa_sim.c  | 292 ++++++++++++++++++++++++------
>>   drivers/vhost/iotlb.c             |  23 ++-
>>   drivers/vhost/vdpa.c              | 246 ++++++++++++++++++++-----
>>   drivers/vhost/vhost.c             |  23 ++-
>>   drivers/vhost/vhost.h             |   4 +-
>>   drivers/virtio/virtio_vdpa.c      |   2 +-
>>   include/linux/vdpa.h              |  42 ++++-
>>   include/linux/vhost_iotlb.h       |   2 +
>>   include/uapi/linux/vhost.h        |  25 ++-
>>   include/uapi/linux/vhost_types.h  |  10 +-
>>   13 files changed, 561 insertions(+), 136 deletions(-)
>>
>> -- 
>> 2.25.1


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 00/21] Control VQ support in vDPA
@ 2020-12-17  3:30     ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-17  3:30 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller


On 2020/12/16 下午5:47, Michael S. Tsirkin wrote:
> On Wed, Dec 16, 2020 at 02:47:57PM +0800, Jason Wang wrote:
>> Hi All:
>>
>> This series tries to add the support for control virtqueue in vDPA.
>>
>> Control virtqueue is used by networking device for accepting various
>> commands from the driver. It's a must to support multiqueue and other
>> configurations.
>>
>> When used by vhost-vDPA bus driver for VM, the control virtqueue
>> should be shadowed via userspace VMM (Qemu) instead of being assigned
>> directly to Guest. This is because Qemu needs to know the device state
>> in order to start and stop device correctly (e.g for Live Migration).
>>
>> This requies to isolate the memory mapping for control virtqueue
>> presented by vhost-vDPA to prevent guest from accesing it directly.
>> To achieve this, vDPA introduce two new abstractions:
>>
>> - address space: identified through address space id (ASID) and a set
>>                   of memory mapping in maintained
>> - virtqueue group: the minimal set of virtqueues that must share an
>>                   address space
> How will this support the pretty common case where control vq
> is programmed by the kernel through the PF, and others by the VFs?


In this case, the VF parent need to provide a software control vq and 
decode the command then send them to VF.


>
>
> I actually thought the way to support it is by exposing
> something like an "inject buffers" API which sends data to a given VQ.
> Maybe an ioctl, and maybe down the road uio ring can support batching
> these ....


So the virtuqueue allows the request to be processed asynchronously (e.g 
driver may choose to use interrupt for control vq). This means we need 
to support that in uAPI level. And if we manage to do that, it's just 
another type of virtqueue.

For virtio-vDPA, this also means the extensions for queue processing 
which is a functional duplication. Using what proposed in this series, 
we don't need any changes for kernel virtio drivers.

What's more important, this series could be used for future features 
that requires DMA isolation between virtqueues:

- report dirty pages via virtqueue
- sub function level device slicing

...

Thanks


>
>
>> Device needs to advertise the following attributes to vDPA:
>>
>> - the number of address spaces supported in the device
>> - the number of virtqueue groups supported in the device
>> - the mappings from a specific virtqueue to its virtqueue groups
>>
>> The mappings from virtqueue to virtqueue groups is fixed and defined
>> by vDPA device driver. E.g:
>>
>> - For the device that has hardware ASID support, it can simply
>>    advertise a per virtqueue virtqueue group.
>> - For the device that does not have hardware ASID support, it can
>>    simply advertise a single virtqueue group that contains all
>>    virtqueues. Or if it wants a software emulated control virtqueue, it
>>    can advertise two virtqueue groups, one is for cvq, another is for
>>    the rest virtqueues.
>>
>> vDPA also allow to change the association between virtqueue group and
>> address space. So in the case of control virtqueue, userspace
>> VMM(Qemu) may use a dedicated address space for the control virtqueue
>> group to isolate the memory mapping.
>>
>> The vhost/vhost-vDPA is also extend for the userspace to:
>>
>> - query the number of virtqueue groups and address spaces supported by
>>    the device
>> - query the virtqueue group for a specific virtqueue
>> - assocaite a virtqueue group with an address space
>> - send ASID based IOTLB commands
>>
>> This will help userspace VMM(Qemu) to detect whether the control vq
>> could be supported and isolate memory mappings of control virtqueue
>> from the others.
>>
>> To demonstrate the usage, vDPA simulator is extended to support
>> setting MAC address via a emulated control virtqueue.
>>
>> Please review.
>>
>> Changes since RFC:
>>
>> - tweak vhost uAPI documentation
>> - switch to use device specific IOTLB really in patch 4
>> - tweak the commit log
>> - fix that ASID in vhost is claimed to be 32 actually but 16bit
>>    actually
>> - fix use after free when using ASID with IOTLB batching requests
>> - switch to use Stefano's patch for having separated iov
>> - remove unused "used_as" variable
>> - fix the iotlb/asid checking in vhost_vdpa_unmap()
>>
>> Thanks
>>
>> Jason Wang (20):
>>    vhost: move the backend feature bits to vhost_types.h
>>    virtio-vdpa: don't set callback if virtio doesn't need it
>>    vhost-vdpa: passing iotlb to IOMMU mapping helpers
>>    vhost-vdpa: switch to use vhost-vdpa specific IOTLB
>>    vdpa: add the missing comment for nvqs in struct vdpa_device
>>    vdpa: introduce virtqueue groups
>>    vdpa: multiple address spaces support
>>    vdpa: introduce config operations for associating ASID to a virtqueue
>>      group
>>    vhost_iotlb: split out IOTLB initialization
>>    vhost: support ASID in IOTLB API
>>    vhost-vdpa: introduce asid based IOTLB
>>    vhost-vdpa: introduce uAPI to get the number of virtqueue groups
>>    vhost-vdpa: introduce uAPI to get the number of address spaces
>>    vhost-vdpa: uAPI to get virtqueue group id
>>    vhost-vdpa: introduce uAPI to set group ASID
>>    vhost-vdpa: support ASID based IOTLB API
>>    vdpa_sim: advertise VIRTIO_NET_F_MTU
>>    vdpa_sim: factor out buffer completion logic
>>    vdpa_sim: filter destination mac address
>>    vdpasim: control virtqueue support
>>
>> Stefano Garzarella (1):
>>    vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov
>>
>>   drivers/vdpa/ifcvf/ifcvf_main.c   |   9 +-
>>   drivers/vdpa/mlx5/net/mlx5_vnet.c |  11 +-
>>   drivers/vdpa/vdpa.c               |   8 +-
>>   drivers/vdpa/vdpa_sim/vdpa_sim.c  | 292 ++++++++++++++++++++++++------
>>   drivers/vhost/iotlb.c             |  23 ++-
>>   drivers/vhost/vdpa.c              | 246 ++++++++++++++++++++-----
>>   drivers/vhost/vhost.c             |  23 ++-
>>   drivers/vhost/vhost.h             |   4 +-
>>   drivers/virtio/virtio_vdpa.c      |   2 +-
>>   include/linux/vdpa.h              |  42 ++++-
>>   include/linux/vhost_iotlb.h       |   2 +
>>   include/uapi/linux/vhost.h        |  25 ++-
>>   include/uapi/linux/vhost_types.h  |  10 +-
>>   13 files changed, 561 insertions(+), 136 deletions(-)
>>
>> -- 
>> 2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 00/21] Control VQ support in vDPA
  2020-12-16  6:47 ` Jason Wang
                   ` (22 preceding siblings ...)
  (?)
@ 2020-12-17  7:26 ` Eli Cohen
  -1 siblings, 0 replies; 136+ messages in thread
From: Eli Cohen @ 2020-12-17  7:26 UTC (permalink / raw)
  To: Jason Wang
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare

On Wed, Dec 16, 2020 at 02:47:57PM +0800, Jason Wang wrote:

Hi Jason,
I saw the patchset and will start reviewing it starting Dec 27. I am out
of office next week.

> Hi All:
> 
> This series tries to add the support for control virtqueue in vDPA.
> 
> Control virtqueue is used by networking device for accepting various
> commands from the driver. It's a must to support multiqueue and other
> configurations.
> 
> When used by vhost-vDPA bus driver for VM, the control virtqueue
> should be shadowed via userspace VMM (Qemu) instead of being assigned
> directly to Guest. This is because Qemu needs to know the device state
> in order to start and stop device correctly (e.g for Live Migration).
> 
> This requies to isolate the memory mapping for control virtqueue
> presented by vhost-vDPA to prevent guest from accesing it directly.
> 
> To achieve this, vDPA introduce two new abstractions:
> 
> - address space: identified through address space id (ASID) and a set
>                  of memory mapping in maintained
> - virtqueue group: the minimal set of virtqueues that must share an
>                  address space
> 
> Device needs to advertise the following attributes to vDPA:
> 
> - the number of address spaces supported in the device
> - the number of virtqueue groups supported in the device
> - the mappings from a specific virtqueue to its virtqueue groups
> 
> The mappings from virtqueue to virtqueue groups is fixed and defined
> by vDPA device driver. E.g:
> 
> - For the device that has hardware ASID support, it can simply
>   advertise a per virtqueue virtqueue group.
> - For the device that does not have hardware ASID support, it can
>   simply advertise a single virtqueue group that contains all
>   virtqueues. Or if it wants a software emulated control virtqueue, it
>   can advertise two virtqueue groups, one is for cvq, another is for
>   the rest virtqueues.
> 
> vDPA also allow to change the association between virtqueue group and
> address space. So in the case of control virtqueue, userspace
> VMM(Qemu) may use a dedicated address space for the control virtqueue
> group to isolate the memory mapping.
> 
> The vhost/vhost-vDPA is also extend for the userspace to:
> 
> - query the number of virtqueue groups and address spaces supported by
>   the device
> - query the virtqueue group for a specific virtqueue
> - assocaite a virtqueue group with an address space
> - send ASID based IOTLB commands
> 
> This will help userspace VMM(Qemu) to detect whether the control vq
> could be supported and isolate memory mappings of control virtqueue
> from the others.
> 
> To demonstrate the usage, vDPA simulator is extended to support
> setting MAC address via a emulated control virtqueue.
> 
> Please review.
> 
> Changes since RFC:
> 
> - tweak vhost uAPI documentation
> - switch to use device specific IOTLB really in patch 4
> - tweak the commit log
> - fix that ASID in vhost is claimed to be 32 actually but 16bit
>   actually
> - fix use after free when using ASID with IOTLB batching requests
> - switch to use Stefano's patch for having separated iov
> - remove unused "used_as" variable
> - fix the iotlb/asid checking in vhost_vdpa_unmap()
> 
> Thanks
> 
> Jason Wang (20):
>   vhost: move the backend feature bits to vhost_types.h
>   virtio-vdpa: don't set callback if virtio doesn't need it
>   vhost-vdpa: passing iotlb to IOMMU mapping helpers
>   vhost-vdpa: switch to use vhost-vdpa specific IOTLB
>   vdpa: add the missing comment for nvqs in struct vdpa_device
>   vdpa: introduce virtqueue groups
>   vdpa: multiple address spaces support
>   vdpa: introduce config operations for associating ASID to a virtqueue
>     group
>   vhost_iotlb: split out IOTLB initialization
>   vhost: support ASID in IOTLB API
>   vhost-vdpa: introduce asid based IOTLB
>   vhost-vdpa: introduce uAPI to get the number of virtqueue groups
>   vhost-vdpa: introduce uAPI to get the number of address spaces
>   vhost-vdpa: uAPI to get virtqueue group id
>   vhost-vdpa: introduce uAPI to set group ASID
>   vhost-vdpa: support ASID based IOTLB API
>   vdpa_sim: advertise VIRTIO_NET_F_MTU
>   vdpa_sim: factor out buffer completion logic
>   vdpa_sim: filter destination mac address
>   vdpasim: control virtqueue support
> 
> Stefano Garzarella (1):
>   vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov
> 
>  drivers/vdpa/ifcvf/ifcvf_main.c   |   9 +-
>  drivers/vdpa/mlx5/net/mlx5_vnet.c |  11 +-
>  drivers/vdpa/vdpa.c               |   8 +-
>  drivers/vdpa/vdpa_sim/vdpa_sim.c  | 292 ++++++++++++++++++++++++------
>  drivers/vhost/iotlb.c             |  23 ++-
>  drivers/vhost/vdpa.c              | 246 ++++++++++++++++++++-----
>  drivers/vhost/vhost.c             |  23 ++-
>  drivers/vhost/vhost.h             |   4 +-
>  drivers/virtio/virtio_vdpa.c      |   2 +-
>  include/linux/vdpa.h              |  42 ++++-
>  include/linux/vhost_iotlb.h       |   2 +
>  include/uapi/linux/vhost.h        |  25 ++-
>  include/uapi/linux/vhost_types.h  |  10 +-
>  13 files changed, 561 insertions(+), 136 deletions(-)
> 
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 00/21] Control VQ support in vDPA
  2020-12-17  3:30     ` Jason Wang
@ 2020-12-17  7:58       ` Michael S. Tsirkin
  -1 siblings, 0 replies; 136+ messages in thread
From: Michael S. Tsirkin @ 2020-12-17  7:58 UTC (permalink / raw)
  To: Jason Wang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

On Thu, Dec 17, 2020 at 11:30:18AM +0800, Jason Wang wrote:
> 
> On 2020/12/16 下午5:47, Michael S. Tsirkin wrote:
> > On Wed, Dec 16, 2020 at 02:47:57PM +0800, Jason Wang wrote:
> > > Hi All:
> > > 
> > > This series tries to add the support for control virtqueue in vDPA.
> > > 
> > > Control virtqueue is used by networking device for accepting various
> > > commands from the driver. It's a must to support multiqueue and other
> > > configurations.
> > > 
> > > When used by vhost-vDPA bus driver for VM, the control virtqueue
> > > should be shadowed via userspace VMM (Qemu) instead of being assigned
> > > directly to Guest. This is because Qemu needs to know the device state
> > > in order to start and stop device correctly (e.g for Live Migration).
> > > 
> > > This requies to isolate the memory mapping for control virtqueue
> > > presented by vhost-vDPA to prevent guest from accesing it directly.
> > > To achieve this, vDPA introduce two new abstractions:
> > > 
> > > - address space: identified through address space id (ASID) and a set
> > >                   of memory mapping in maintained
> > > - virtqueue group: the minimal set of virtqueues that must share an
> > >                   address space
> > How will this support the pretty common case where control vq
> > is programmed by the kernel through the PF, and others by the VFs?
> 
> 
> In this case, the VF parent need to provide a software control vq and decode
> the command then send them to VF.


But how does that tie to the address space infrastructure?



> 
> > 
> > 
> > I actually thought the way to support it is by exposing
> > something like an "inject buffers" API which sends data to a given VQ.
> > Maybe an ioctl, and maybe down the road uio ring can support batching
> > these ....
> 
> 
> So the virtuqueue allows the request to be processed asynchronously (e.g
> driver may choose to use interrupt for control vq). This means we need to
> support that in uAPI level.

I don't think we need to make it async, just a regular ioctl will do.
In fact no guest uses the asynchronous property.


> And if we manage to do that, it's just another
> type of virtqueue.
> 
> For virtio-vDPA, this also means the extensions for queue processing which
> is a functional duplication.

I don't see why, just send it to the actual control vq :)

> Using what proposed in this series, we don't
> need any changes for kernel virtio drivers.
> 
> What's more important, this series could be used for future features that
> requires DMA isolation between virtqueues:
> 
> - report dirty pages via virtqueue
> - sub function level device slicing


I agree these are nice to have, but I am not sure basic control vq must
be tied to that.

> ...
> 
> Thanks
> 
> 
> > 
> > 
> > > Device needs to advertise the following attributes to vDPA:
> > > 
> > > - the number of address spaces supported in the device
> > > - the number of virtqueue groups supported in the device
> > > - the mappings from a specific virtqueue to its virtqueue groups
> > > 
> > > The mappings from virtqueue to virtqueue groups is fixed and defined
> > > by vDPA device driver. E.g:
> > > 
> > > - For the device that has hardware ASID support, it can simply
> > >    advertise a per virtqueue virtqueue group.
> > > - For the device that does not have hardware ASID support, it can
> > >    simply advertise a single virtqueue group that contains all
> > >    virtqueues. Or if it wants a software emulated control virtqueue, it
> > >    can advertise two virtqueue groups, one is for cvq, another is for
> > >    the rest virtqueues.
> > > 
> > > vDPA also allow to change the association between virtqueue group and
> > > address space. So in the case of control virtqueue, userspace
> > > VMM(Qemu) may use a dedicated address space for the control virtqueue
> > > group to isolate the memory mapping.
> > > 
> > > The vhost/vhost-vDPA is also extend for the userspace to:
> > > 
> > > - query the number of virtqueue groups and address spaces supported by
> > >    the device
> > > - query the virtqueue group for a specific virtqueue
> > > - assocaite a virtqueue group with an address space
> > > - send ASID based IOTLB commands
> > > 
> > > This will help userspace VMM(Qemu) to detect whether the control vq
> > > could be supported and isolate memory mappings of control virtqueue
> > > from the others.
> > > 
> > > To demonstrate the usage, vDPA simulator is extended to support
> > > setting MAC address via a emulated control virtqueue.
> > > 
> > > Please review.
> > > 
> > > Changes since RFC:
> > > 
> > > - tweak vhost uAPI documentation
> > > - switch to use device specific IOTLB really in patch 4
> > > - tweak the commit log
> > > - fix that ASID in vhost is claimed to be 32 actually but 16bit
> > >    actually
> > > - fix use after free when using ASID with IOTLB batching requests
> > > - switch to use Stefano's patch for having separated iov
> > > - remove unused "used_as" variable
> > > - fix the iotlb/asid checking in vhost_vdpa_unmap()
> > > 
> > > Thanks
> > > 
> > > Jason Wang (20):
> > >    vhost: move the backend feature bits to vhost_types.h
> > >    virtio-vdpa: don't set callback if virtio doesn't need it
> > >    vhost-vdpa: passing iotlb to IOMMU mapping helpers
> > >    vhost-vdpa: switch to use vhost-vdpa specific IOTLB
> > >    vdpa: add the missing comment for nvqs in struct vdpa_device
> > >    vdpa: introduce virtqueue groups
> > >    vdpa: multiple address spaces support
> > >    vdpa: introduce config operations for associating ASID to a virtqueue
> > >      group
> > >    vhost_iotlb: split out IOTLB initialization
> > >    vhost: support ASID in IOTLB API
> > >    vhost-vdpa: introduce asid based IOTLB
> > >    vhost-vdpa: introduce uAPI to get the number of virtqueue groups
> > >    vhost-vdpa: introduce uAPI to get the number of address spaces
> > >    vhost-vdpa: uAPI to get virtqueue group id
> > >    vhost-vdpa: introduce uAPI to set group ASID
> > >    vhost-vdpa: support ASID based IOTLB API
> > >    vdpa_sim: advertise VIRTIO_NET_F_MTU
> > >    vdpa_sim: factor out buffer completion logic
> > >    vdpa_sim: filter destination mac address
> > >    vdpasim: control virtqueue support
> > > 
> > > Stefano Garzarella (1):
> > >    vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov
> > > 
> > >   drivers/vdpa/ifcvf/ifcvf_main.c   |   9 +-
> > >   drivers/vdpa/mlx5/net/mlx5_vnet.c |  11 +-
> > >   drivers/vdpa/vdpa.c               |   8 +-
> > >   drivers/vdpa/vdpa_sim/vdpa_sim.c  | 292 ++++++++++++++++++++++++------
> > >   drivers/vhost/iotlb.c             |  23 ++-
> > >   drivers/vhost/vdpa.c              | 246 ++++++++++++++++++++-----
> > >   drivers/vhost/vhost.c             |  23 ++-
> > >   drivers/vhost/vhost.h             |   4 +-
> > >   drivers/virtio/virtio_vdpa.c      |   2 +-
> > >   include/linux/vdpa.h              |  42 ++++-
> > >   include/linux/vhost_iotlb.h       |   2 +
> > >   include/uapi/linux/vhost.h        |  25 ++-
> > >   include/uapi/linux/vhost_types.h  |  10 +-
> > >   13 files changed, 561 insertions(+), 136 deletions(-)
> > > 
> > > -- 
> > > 2.25.1


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 00/21] Control VQ support in vDPA
@ 2020-12-17  7:58       ` Michael S. Tsirkin
  0 siblings, 0 replies; 136+ messages in thread
From: Michael S. Tsirkin @ 2020-12-17  7:58 UTC (permalink / raw)
  To: Jason Wang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

On Thu, Dec 17, 2020 at 11:30:18AM +0800, Jason Wang wrote:
> 
> On 2020/12/16 下午5:47, Michael S. Tsirkin wrote:
> > On Wed, Dec 16, 2020 at 02:47:57PM +0800, Jason Wang wrote:
> > > Hi All:
> > > 
> > > This series tries to add the support for control virtqueue in vDPA.
> > > 
> > > Control virtqueue is used by networking device for accepting various
> > > commands from the driver. It's a must to support multiqueue and other
> > > configurations.
> > > 
> > > When used by vhost-vDPA bus driver for VM, the control virtqueue
> > > should be shadowed via userspace VMM (Qemu) instead of being assigned
> > > directly to Guest. This is because Qemu needs to know the device state
> > > in order to start and stop device correctly (e.g for Live Migration).
> > > 
> > > This requies to isolate the memory mapping for control virtqueue
> > > presented by vhost-vDPA to prevent guest from accesing it directly.
> > > To achieve this, vDPA introduce two new abstractions:
> > > 
> > > - address space: identified through address space id (ASID) and a set
> > >                   of memory mapping in maintained
> > > - virtqueue group: the minimal set of virtqueues that must share an
> > >                   address space
> > How will this support the pretty common case where control vq
> > is programmed by the kernel through the PF, and others by the VFs?
> 
> 
> In this case, the VF parent need to provide a software control vq and decode
> the command then send them to VF.


But how does that tie to the address space infrastructure?



> 
> > 
> > 
> > I actually thought the way to support it is by exposing
> > something like an "inject buffers" API which sends data to a given VQ.
> > Maybe an ioctl, and maybe down the road uio ring can support batching
> > these ....
> 
> 
> So the virtuqueue allows the request to be processed asynchronously (e.g
> driver may choose to use interrupt for control vq). This means we need to
> support that in uAPI level.

I don't think we need to make it async, just a regular ioctl will do.
In fact no guest uses the asynchronous property.


> And if we manage to do that, it's just another
> type of virtqueue.
> 
> For virtio-vDPA, this also means the extensions for queue processing which
> is a functional duplication.

I don't see why, just send it to the actual control vq :)

> Using what proposed in this series, we don't
> need any changes for kernel virtio drivers.
> 
> What's more important, this series could be used for future features that
> requires DMA isolation between virtqueues:
> 
> - report dirty pages via virtqueue
> - sub function level device slicing


I agree these are nice to have, but I am not sure basic control vq must
be tied to that.

> ...
> 
> Thanks
> 
> 
> > 
> > 
> > > Device needs to advertise the following attributes to vDPA:
> > > 
> > > - the number of address spaces supported in the device
> > > - the number of virtqueue groups supported in the device
> > > - the mappings from a specific virtqueue to its virtqueue groups
> > > 
> > > The mappings from virtqueue to virtqueue groups is fixed and defined
> > > by vDPA device driver. E.g:
> > > 
> > > - For the device that has hardware ASID support, it can simply
> > >    advertise a per virtqueue virtqueue group.
> > > - For the device that does not have hardware ASID support, it can
> > >    simply advertise a single virtqueue group that contains all
> > >    virtqueues. Or if it wants a software emulated control virtqueue, it
> > >    can advertise two virtqueue groups, one is for cvq, another is for
> > >    the rest virtqueues.
> > > 
> > > vDPA also allow to change the association between virtqueue group and
> > > address space. So in the case of control virtqueue, userspace
> > > VMM(Qemu) may use a dedicated address space for the control virtqueue
> > > group to isolate the memory mapping.
> > > 
> > > The vhost/vhost-vDPA is also extend for the userspace to:
> > > 
> > > - query the number of virtqueue groups and address spaces supported by
> > >    the device
> > > - query the virtqueue group for a specific virtqueue
> > > - assocaite a virtqueue group with an address space
> > > - send ASID based IOTLB commands
> > > 
> > > This will help userspace VMM(Qemu) to detect whether the control vq
> > > could be supported and isolate memory mappings of control virtqueue
> > > from the others.
> > > 
> > > To demonstrate the usage, vDPA simulator is extended to support
> > > setting MAC address via a emulated control virtqueue.
> > > 
> > > Please review.
> > > 
> > > Changes since RFC:
> > > 
> > > - tweak vhost uAPI documentation
> > > - switch to use device specific IOTLB really in patch 4
> > > - tweak the commit log
> > > - fix that ASID in vhost is claimed to be 32 actually but 16bit
> > >    actually
> > > - fix use after free when using ASID with IOTLB batching requests
> > > - switch to use Stefano's patch for having separated iov
> > > - remove unused "used_as" variable
> > > - fix the iotlb/asid checking in vhost_vdpa_unmap()
> > > 
> > > Thanks
> > > 
> > > Jason Wang (20):
> > >    vhost: move the backend feature bits to vhost_types.h
> > >    virtio-vdpa: don't set callback if virtio doesn't need it
> > >    vhost-vdpa: passing iotlb to IOMMU mapping helpers
> > >    vhost-vdpa: switch to use vhost-vdpa specific IOTLB
> > >    vdpa: add the missing comment for nvqs in struct vdpa_device
> > >    vdpa: introduce virtqueue groups
> > >    vdpa: multiple address spaces support
> > >    vdpa: introduce config operations for associating ASID to a virtqueue
> > >      group
> > >    vhost_iotlb: split out IOTLB initialization
> > >    vhost: support ASID in IOTLB API
> > >    vhost-vdpa: introduce asid based IOTLB
> > >    vhost-vdpa: introduce uAPI to get the number of virtqueue groups
> > >    vhost-vdpa: introduce uAPI to get the number of address spaces
> > >    vhost-vdpa: uAPI to get virtqueue group id
> > >    vhost-vdpa: introduce uAPI to set group ASID
> > >    vhost-vdpa: support ASID based IOTLB API
> > >    vdpa_sim: advertise VIRTIO_NET_F_MTU
> > >    vdpa_sim: factor out buffer completion logic
> > >    vdpa_sim: filter destination mac address
> > >    vdpasim: control virtqueue support
> > > 
> > > Stefano Garzarella (1):
> > >    vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov
> > > 
> > >   drivers/vdpa/ifcvf/ifcvf_main.c   |   9 +-
> > >   drivers/vdpa/mlx5/net/mlx5_vnet.c |  11 +-
> > >   drivers/vdpa/vdpa.c               |   8 +-
> > >   drivers/vdpa/vdpa_sim/vdpa_sim.c  | 292 ++++++++++++++++++++++++------
> > >   drivers/vhost/iotlb.c             |  23 ++-
> > >   drivers/vhost/vdpa.c              | 246 ++++++++++++++++++++-----
> > >   drivers/vhost/vhost.c             |  23 ++-
> > >   drivers/vhost/vhost.h             |   4 +-
> > >   drivers/virtio/virtio_vdpa.c      |   2 +-
> > >   include/linux/vdpa.h              |  42 ++++-
> > >   include/linux/vhost_iotlb.h       |   2 +
> > >   include/uapi/linux/vhost.h        |  25 ++-
> > >   include/uapi/linux/vhost_types.h  |  10 +-
> > >   13 files changed, 561 insertions(+), 136 deletions(-)
> > > 
> > > -- 
> > > 2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 00/21] Control VQ support in vDPA
  2020-12-17  7:58       ` Michael S. Tsirkin
@ 2020-12-17  9:02         ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-17  9:02 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare


On 2020/12/17 下午3:58, Michael S. Tsirkin wrote:
> On Thu, Dec 17, 2020 at 11:30:18AM +0800, Jason Wang wrote:
>> On 2020/12/16 下午5:47, Michael S. Tsirkin wrote:
>>> On Wed, Dec 16, 2020 at 02:47:57PM +0800, Jason Wang wrote:
>>>> Hi All:
>>>>
>>>> This series tries to add the support for control virtqueue in vDPA.
>>>>
>>>> Control virtqueue is used by networking device for accepting various
>>>> commands from the driver. It's a must to support multiqueue and other
>>>> configurations.
>>>>
>>>> When used by vhost-vDPA bus driver for VM, the control virtqueue
>>>> should be shadowed via userspace VMM (Qemu) instead of being assigned
>>>> directly to Guest. This is because Qemu needs to know the device state
>>>> in order to start and stop device correctly (e.g for Live Migration).
>>>>
>>>> This requies to isolate the memory mapping for control virtqueue
>>>> presented by vhost-vDPA to prevent guest from accesing it directly.
>>>> To achieve this, vDPA introduce two new abstractions:
>>>>
>>>> - address space: identified through address space id (ASID) and a set
>>>>                    of memory mapping in maintained
>>>> - virtqueue group: the minimal set of virtqueues that must share an
>>>>                    address space
>>> How will this support the pretty common case where control vq
>>> is programmed by the kernel through the PF, and others by the VFs?
>>
>> In this case, the VF parent need to provide a software control vq and decode
>> the command then send them to VF.
>
> But how does that tie to the address space infrastructure?


In this case, address space is not a must. But the idea is to make 
control vq works for all types of hardware:

1) control virtqueue is implemented via VF/PF communication
2) control virtqueue is implemented by VF but not through DMA
3) control virtqueue is implemented by VF DMA, it could be either a 
hardware control virtqueue or other type of DMA

The address space is a must for 3) to work and can work for both 1) and 2).


>
>
>
>>>
>>> I actually thought the way to support it is by exposing
>>> something like an "inject buffers" API which sends data to a given VQ.
>>> Maybe an ioctl, and maybe down the road uio ring can support batching
>>> these ....
>>
>> So the virtuqueue allows the request to be processed asynchronously (e.g
>> driver may choose to use interrupt for control vq). This means we need to
>> support that in uAPI level.
> I don't think we need to make it async, just a regular ioctl will do.
> In fact no guest uses the asynchronous property.


It was not forbidden by the spec then we need to support that. E.g we 
can not assume driver doesn't assign interrupt for cvq.


>
>
>> And if we manage to do that, it's just another
>> type of virtqueue.
>>
>> For virtio-vDPA, this also means the extensions for queue processing which
>> is a functional duplication.
> I don't see why, just send it to the actual control vq :)


But in the case you've pointed out, there's no hardware control vq in fact.


>
>> Using what proposed in this series, we don't
>> need any changes for kernel virtio drivers.
>>
>> What's more important, this series could be used for future features that
>> requires DMA isolation between virtqueues:
>>
>> - report dirty pages via virtqueue
>> - sub function level device slicing
>
> I agree these are nice to have, but I am not sure basic control vq must
> be tied to that.


If the control virtqueue is implemented via DMA through VF, it looks 
like a must.

Thanks


>
>> ...
>>
>> Thanks
>>
>>
>>>
>>>> Device needs to advertise the following attributes to vDPA:
>>>>
>>>> - the number of address spaces supported in the device
>>>> - the number of virtqueue groups supported in the device
>>>> - the mappings from a specific virtqueue to its virtqueue groups
>>>>
>>>> The mappings from virtqueue to virtqueue groups is fixed and defined
>>>> by vDPA device driver. E.g:
>>>>
>>>> - For the device that has hardware ASID support, it can simply
>>>>     advertise a per virtqueue virtqueue group.
>>>> - For the device that does not have hardware ASID support, it can
>>>>     simply advertise a single virtqueue group that contains all
>>>>     virtqueues. Or if it wants a software emulated control virtqueue, it
>>>>     can advertise two virtqueue groups, one is for cvq, another is for
>>>>     the rest virtqueues.
>>>>
>>>> vDPA also allow to change the association between virtqueue group and
>>>> address space. So in the case of control virtqueue, userspace
>>>> VMM(Qemu) may use a dedicated address space for the control virtqueue
>>>> group to isolate the memory mapping.
>>>>
>>>> The vhost/vhost-vDPA is also extend for the userspace to:
>>>>
>>>> - query the number of virtqueue groups and address spaces supported by
>>>>     the device
>>>> - query the virtqueue group for a specific virtqueue
>>>> - assocaite a virtqueue group with an address space
>>>> - send ASID based IOTLB commands
>>>>
>>>> This will help userspace VMM(Qemu) to detect whether the control vq
>>>> could be supported and isolate memory mappings of control virtqueue
>>>> from the others.
>>>>
>>>> To demonstrate the usage, vDPA simulator is extended to support
>>>> setting MAC address via a emulated control virtqueue.
>>>>
>>>> Please review.
>>>>
>>>> Changes since RFC:
>>>>
>>>> - tweak vhost uAPI documentation
>>>> - switch to use device specific IOTLB really in patch 4
>>>> - tweak the commit log
>>>> - fix that ASID in vhost is claimed to be 32 actually but 16bit
>>>>     actually
>>>> - fix use after free when using ASID with IOTLB batching requests
>>>> - switch to use Stefano's patch for having separated iov
>>>> - remove unused "used_as" variable
>>>> - fix the iotlb/asid checking in vhost_vdpa_unmap()
>>>>
>>>> Thanks
>>>>
>>>> Jason Wang (20):
>>>>     vhost: move the backend feature bits to vhost_types.h
>>>>     virtio-vdpa: don't set callback if virtio doesn't need it
>>>>     vhost-vdpa: passing iotlb to IOMMU mapping helpers
>>>>     vhost-vdpa: switch to use vhost-vdpa specific IOTLB
>>>>     vdpa: add the missing comment for nvqs in struct vdpa_device
>>>>     vdpa: introduce virtqueue groups
>>>>     vdpa: multiple address spaces support
>>>>     vdpa: introduce config operations for associating ASID to a virtqueue
>>>>       group
>>>>     vhost_iotlb: split out IOTLB initialization
>>>>     vhost: support ASID in IOTLB API
>>>>     vhost-vdpa: introduce asid based IOTLB
>>>>     vhost-vdpa: introduce uAPI to get the number of virtqueue groups
>>>>     vhost-vdpa: introduce uAPI to get the number of address spaces
>>>>     vhost-vdpa: uAPI to get virtqueue group id
>>>>     vhost-vdpa: introduce uAPI to set group ASID
>>>>     vhost-vdpa: support ASID based IOTLB API
>>>>     vdpa_sim: advertise VIRTIO_NET_F_MTU
>>>>     vdpa_sim: factor out buffer completion logic
>>>>     vdpa_sim: filter destination mac address
>>>>     vdpasim: control virtqueue support
>>>>
>>>> Stefano Garzarella (1):
>>>>     vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov
>>>>
>>>>    drivers/vdpa/ifcvf/ifcvf_main.c   |   9 +-
>>>>    drivers/vdpa/mlx5/net/mlx5_vnet.c |  11 +-
>>>>    drivers/vdpa/vdpa.c               |   8 +-
>>>>    drivers/vdpa/vdpa_sim/vdpa_sim.c  | 292 ++++++++++++++++++++++++------
>>>>    drivers/vhost/iotlb.c             |  23 ++-
>>>>    drivers/vhost/vdpa.c              | 246 ++++++++++++++++++++-----
>>>>    drivers/vhost/vhost.c             |  23 ++-
>>>>    drivers/vhost/vhost.h             |   4 +-
>>>>    drivers/virtio/virtio_vdpa.c      |   2 +-
>>>>    include/linux/vdpa.h              |  42 ++++-
>>>>    include/linux/vhost_iotlb.h       |   2 +
>>>>    include/uapi/linux/vhost.h        |  25 ++-
>>>>    include/uapi/linux/vhost_types.h  |  10 +-
>>>>    13 files changed, 561 insertions(+), 136 deletions(-)
>>>>
>>>> -- 
>>>> 2.25.1


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 00/21] Control VQ support in vDPA
@ 2020-12-17  9:02         ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-17  9:02 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller


On 2020/12/17 下午3:58, Michael S. Tsirkin wrote:
> On Thu, Dec 17, 2020 at 11:30:18AM +0800, Jason Wang wrote:
>> On 2020/12/16 下午5:47, Michael S. Tsirkin wrote:
>>> On Wed, Dec 16, 2020 at 02:47:57PM +0800, Jason Wang wrote:
>>>> Hi All:
>>>>
>>>> This series tries to add the support for control virtqueue in vDPA.
>>>>
>>>> Control virtqueue is used by networking device for accepting various
>>>> commands from the driver. It's a must to support multiqueue and other
>>>> configurations.
>>>>
>>>> When used by vhost-vDPA bus driver for VM, the control virtqueue
>>>> should be shadowed via userspace VMM (Qemu) instead of being assigned
>>>> directly to Guest. This is because Qemu needs to know the device state
>>>> in order to start and stop device correctly (e.g for Live Migration).
>>>>
>>>> This requies to isolate the memory mapping for control virtqueue
>>>> presented by vhost-vDPA to prevent guest from accesing it directly.
>>>> To achieve this, vDPA introduce two new abstractions:
>>>>
>>>> - address space: identified through address space id (ASID) and a set
>>>>                    of memory mapping in maintained
>>>> - virtqueue group: the minimal set of virtqueues that must share an
>>>>                    address space
>>> How will this support the pretty common case where control vq
>>> is programmed by the kernel through the PF, and others by the VFs?
>>
>> In this case, the VF parent need to provide a software control vq and decode
>> the command then send them to VF.
>
> But how does that tie to the address space infrastructure?


In this case, address space is not a must. But the idea is to make 
control vq works for all types of hardware:

1) control virtqueue is implemented via VF/PF communication
2) control virtqueue is implemented by VF but not through DMA
3) control virtqueue is implemented by VF DMA, it could be either a 
hardware control virtqueue or other type of DMA

The address space is a must for 3) to work and can work for both 1) and 2).


>
>
>
>>>
>>> I actually thought the way to support it is by exposing
>>> something like an "inject buffers" API which sends data to a given VQ.
>>> Maybe an ioctl, and maybe down the road uio ring can support batching
>>> these ....
>>
>> So the virtuqueue allows the request to be processed asynchronously (e.g
>> driver may choose to use interrupt for control vq). This means we need to
>> support that in uAPI level.
> I don't think we need to make it async, just a regular ioctl will do.
> In fact no guest uses the asynchronous property.


It was not forbidden by the spec then we need to support that. E.g we 
can not assume driver doesn't assign interrupt for cvq.


>
>
>> And if we manage to do that, it's just another
>> type of virtqueue.
>>
>> For virtio-vDPA, this also means the extensions for queue processing which
>> is a functional duplication.
> I don't see why, just send it to the actual control vq :)


But in the case you've pointed out, there's no hardware control vq in fact.


>
>> Using what proposed in this series, we don't
>> need any changes for kernel virtio drivers.
>>
>> What's more important, this series could be used for future features that
>> requires DMA isolation between virtqueues:
>>
>> - report dirty pages via virtqueue
>> - sub function level device slicing
>
> I agree these are nice to have, but I am not sure basic control vq must
> be tied to that.


If the control virtqueue is implemented via DMA through VF, it looks 
like a must.

Thanks


>
>> ...
>>
>> Thanks
>>
>>
>>>
>>>> Device needs to advertise the following attributes to vDPA:
>>>>
>>>> - the number of address spaces supported in the device
>>>> - the number of virtqueue groups supported in the device
>>>> - the mappings from a specific virtqueue to its virtqueue groups
>>>>
>>>> The mappings from virtqueue to virtqueue groups is fixed and defined
>>>> by vDPA device driver. E.g:
>>>>
>>>> - For the device that has hardware ASID support, it can simply
>>>>     advertise a per virtqueue virtqueue group.
>>>> - For the device that does not have hardware ASID support, it can
>>>>     simply advertise a single virtqueue group that contains all
>>>>     virtqueues. Or if it wants a software emulated control virtqueue, it
>>>>     can advertise two virtqueue groups, one is for cvq, another is for
>>>>     the rest virtqueues.
>>>>
>>>> vDPA also allow to change the association between virtqueue group and
>>>> address space. So in the case of control virtqueue, userspace
>>>> VMM(Qemu) may use a dedicated address space for the control virtqueue
>>>> group to isolate the memory mapping.
>>>>
>>>> The vhost/vhost-vDPA is also extend for the userspace to:
>>>>
>>>> - query the number of virtqueue groups and address spaces supported by
>>>>     the device
>>>> - query the virtqueue group for a specific virtqueue
>>>> - assocaite a virtqueue group with an address space
>>>> - send ASID based IOTLB commands
>>>>
>>>> This will help userspace VMM(Qemu) to detect whether the control vq
>>>> could be supported and isolate memory mappings of control virtqueue
>>>> from the others.
>>>>
>>>> To demonstrate the usage, vDPA simulator is extended to support
>>>> setting MAC address via a emulated control virtqueue.
>>>>
>>>> Please review.
>>>>
>>>> Changes since RFC:
>>>>
>>>> - tweak vhost uAPI documentation
>>>> - switch to use device specific IOTLB really in patch 4
>>>> - tweak the commit log
>>>> - fix that ASID in vhost is claimed to be 32 actually but 16bit
>>>>     actually
>>>> - fix use after free when using ASID with IOTLB batching requests
>>>> - switch to use Stefano's patch for having separated iov
>>>> - remove unused "used_as" variable
>>>> - fix the iotlb/asid checking in vhost_vdpa_unmap()
>>>>
>>>> Thanks
>>>>
>>>> Jason Wang (20):
>>>>     vhost: move the backend feature bits to vhost_types.h
>>>>     virtio-vdpa: don't set callback if virtio doesn't need it
>>>>     vhost-vdpa: passing iotlb to IOMMU mapping helpers
>>>>     vhost-vdpa: switch to use vhost-vdpa specific IOTLB
>>>>     vdpa: add the missing comment for nvqs in struct vdpa_device
>>>>     vdpa: introduce virtqueue groups
>>>>     vdpa: multiple address spaces support
>>>>     vdpa: introduce config operations for associating ASID to a virtqueue
>>>>       group
>>>>     vhost_iotlb: split out IOTLB initialization
>>>>     vhost: support ASID in IOTLB API
>>>>     vhost-vdpa: introduce asid based IOTLB
>>>>     vhost-vdpa: introduce uAPI to get the number of virtqueue groups
>>>>     vhost-vdpa: introduce uAPI to get the number of address spaces
>>>>     vhost-vdpa: uAPI to get virtqueue group id
>>>>     vhost-vdpa: introduce uAPI to set group ASID
>>>>     vhost-vdpa: support ASID based IOTLB API
>>>>     vdpa_sim: advertise VIRTIO_NET_F_MTU
>>>>     vdpa_sim: factor out buffer completion logic
>>>>     vdpa_sim: filter destination mac address
>>>>     vdpasim: control virtqueue support
>>>>
>>>> Stefano Garzarella (1):
>>>>     vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov
>>>>
>>>>    drivers/vdpa/ifcvf/ifcvf_main.c   |   9 +-
>>>>    drivers/vdpa/mlx5/net/mlx5_vnet.c |  11 +-
>>>>    drivers/vdpa/vdpa.c               |   8 +-
>>>>    drivers/vdpa/vdpa_sim/vdpa_sim.c  | 292 ++++++++++++++++++++++++------
>>>>    drivers/vhost/iotlb.c             |  23 ++-
>>>>    drivers/vhost/vdpa.c              | 246 ++++++++++++++++++++-----
>>>>    drivers/vhost/vhost.c             |  23 ++-
>>>>    drivers/vhost/vhost.h             |   4 +-
>>>>    drivers/virtio/virtio_vdpa.c      |   2 +-
>>>>    include/linux/vdpa.h              |  42 ++++-
>>>>    include/linux/vhost_iotlb.h       |   2 +
>>>>    include/uapi/linux/vhost.h        |  25 ++-
>>>>    include/uapi/linux/vhost_types.h  |  10 +-
>>>>    13 files changed, 561 insertions(+), 136 deletions(-)
>>>>
>>>> -- 
>>>> 2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 21/21] vdpasim: control virtqueue support
  2020-12-16  6:48   ` Jason Wang
  (?)
@ 2020-12-17 20:19     ` kernel test robot
  -1 siblings, 0 replies; 136+ messages in thread
From: kernel test robot @ 2020-12-17 20:19 UTC (permalink / raw)
  To: Jason Wang, mst
  Cc: kbuild-all, clang-built-linux, eperezma, kvm, virtualization,
	netdev, linux-kernel, lulu, eli, lingshan.zhu

[-- Attachment #1: Type: text/plain, Size: 3595 bytes --]

Hi Jason,

I love your patch! Perhaps something to improve:

[auto build test WARNING on vhost/linux-next]
[also build test WARNING on linus/master v5.10]
[cannot apply to next-20201217]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Jason-Wang/Control-VQ-support-in-vDPA/20201216-150025
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next
config: x86_64-randconfig-a014-20201217 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project cee1e7d14f4628d6174b33640d502bff3b54ae45)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install x86_64 cross compiling tool for clang build
        # apt-get install binutils-x86-64-linux-gnu
        # https://github.com/0day-ci/linux/commit/157796e1c075942914d2096899f4a9b7cf824373
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Jason-Wang/Control-VQ-support-in-vDPA/20201216-150025
        git checkout 157796e1c075942914d2096899f4a9b7cf824373
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/vdpa/vdpa_sim/vdpa_sim.c:204:21: warning: no previous prototype for function 'vdpasim_handle_ctrl_mac' [-Wmissing-prototypes]
   virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
                       ^
   drivers/vdpa/vdpa_sim/vdpa_sim.c:204:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
   ^
   static 
>> drivers/vdpa/vdpa_sim/vdpa_sim.c:731:5: warning: no previous prototype for function 'vdpasim_set_group_asid' [-Wmissing-prototypes]
   int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
       ^
   drivers/vdpa/vdpa_sim/vdpa_sim.c:731:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
   ^
   static 
   drivers/vdpa/vdpa_sim/vdpa_sim.c:101:19: warning: unused function 'vdpasim16_to_cpu' [-Wunused-function]
   static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
                     ^
   3 warnings generated.


vim +/vdpasim_handle_ctrl_mac +204 drivers/vdpa/vdpa_sim/vdpa_sim.c

   203	
 > 204	virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
   205						    u8 cmd)
   206	{
   207		struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
   208		virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
   209		size_t read;
   210	
   211		switch (cmd) {
   212		case VIRTIO_NET_CTRL_MAC_ADDR_SET:
   213			read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov,
   214						     (void *)vdpasim->config.mac,
   215						     ETH_ALEN);
   216			if (read == ETH_ALEN)
   217				status = VIRTIO_NET_OK;
   218			break;
   219		default:
   220			break;
   221		}
   222	
   223		return status;
   224	}
   225	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 32325 bytes --]

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 21/21] vdpasim: control virtqueue support
@ 2020-12-17 20:19     ` kernel test robot
  0 siblings, 0 replies; 136+ messages in thread
From: kernel test robot @ 2020-12-17 20:19 UTC (permalink / raw)
  To: Jason Wang, mst
  Cc: kbuild-all, lulu, kvm, eperezma, netdev, linux-kernel,
	virtualization, clang-built-linux, eli, lingshan.zhu

[-- Attachment #1: Type: text/plain, Size: 3595 bytes --]

Hi Jason,

I love your patch! Perhaps something to improve:

[auto build test WARNING on vhost/linux-next]
[also build test WARNING on linus/master v5.10]
[cannot apply to next-20201217]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Jason-Wang/Control-VQ-support-in-vDPA/20201216-150025
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next
config: x86_64-randconfig-a014-20201217 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project cee1e7d14f4628d6174b33640d502bff3b54ae45)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install x86_64 cross compiling tool for clang build
        # apt-get install binutils-x86-64-linux-gnu
        # https://github.com/0day-ci/linux/commit/157796e1c075942914d2096899f4a9b7cf824373
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Jason-Wang/Control-VQ-support-in-vDPA/20201216-150025
        git checkout 157796e1c075942914d2096899f4a9b7cf824373
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/vdpa/vdpa_sim/vdpa_sim.c:204:21: warning: no previous prototype for function 'vdpasim_handle_ctrl_mac' [-Wmissing-prototypes]
   virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
                       ^
   drivers/vdpa/vdpa_sim/vdpa_sim.c:204:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
   ^
   static 
>> drivers/vdpa/vdpa_sim/vdpa_sim.c:731:5: warning: no previous prototype for function 'vdpasim_set_group_asid' [-Wmissing-prototypes]
   int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
       ^
   drivers/vdpa/vdpa_sim/vdpa_sim.c:731:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
   ^
   static 
   drivers/vdpa/vdpa_sim/vdpa_sim.c:101:19: warning: unused function 'vdpasim16_to_cpu' [-Wunused-function]
   static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
                     ^
   3 warnings generated.


vim +/vdpasim_handle_ctrl_mac +204 drivers/vdpa/vdpa_sim/vdpa_sim.c

   203	
 > 204	virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
   205						    u8 cmd)
   206	{
   207		struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
   208		virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
   209		size_t read;
   210	
   211		switch (cmd) {
   212		case VIRTIO_NET_CTRL_MAC_ADDR_SET:
   213			read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov,
   214						     (void *)vdpasim->config.mac,
   215						     ETH_ALEN);
   216			if (read == ETH_ALEN)
   217				status = VIRTIO_NET_OK;
   218			break;
   219		default:
   220			break;
   221		}
   222	
   223		return status;
   224	}
   225	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 32325 bytes --]

[-- Attachment #3: Type: text/plain, Size: 183 bytes --]

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 21/21] vdpasim: control virtqueue support
@ 2020-12-17 20:19     ` kernel test robot
  0 siblings, 0 replies; 136+ messages in thread
From: kernel test robot @ 2020-12-17 20:19 UTC (permalink / raw)
  To: kbuild-all

[-- Attachment #1: Type: text/plain, Size: 3678 bytes --]

Hi Jason,

I love your patch! Perhaps something to improve:

[auto build test WARNING on vhost/linux-next]
[also build test WARNING on linus/master v5.10]
[cannot apply to next-20201217]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Jason-Wang/Control-VQ-support-in-vDPA/20201216-150025
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next
config: x86_64-randconfig-a014-20201217 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project cee1e7d14f4628d6174b33640d502bff3b54ae45)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install x86_64 cross compiling tool for clang build
        # apt-get install binutils-x86-64-linux-gnu
        # https://github.com/0day-ci/linux/commit/157796e1c075942914d2096899f4a9b7cf824373
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Jason-Wang/Control-VQ-support-in-vDPA/20201216-150025
        git checkout 157796e1c075942914d2096899f4a9b7cf824373
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/vdpa/vdpa_sim/vdpa_sim.c:204:21: warning: no previous prototype for function 'vdpasim_handle_ctrl_mac' [-Wmissing-prototypes]
   virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
                       ^
   drivers/vdpa/vdpa_sim/vdpa_sim.c:204:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
   ^
   static 
>> drivers/vdpa/vdpa_sim/vdpa_sim.c:731:5: warning: no previous prototype for function 'vdpasim_set_group_asid' [-Wmissing-prototypes]
   int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
       ^
   drivers/vdpa/vdpa_sim/vdpa_sim.c:731:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
   ^
   static 
   drivers/vdpa/vdpa_sim/vdpa_sim.c:101:19: warning: unused function 'vdpasim16_to_cpu' [-Wunused-function]
   static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
                     ^
   3 warnings generated.


vim +/vdpasim_handle_ctrl_mac +204 drivers/vdpa/vdpa_sim/vdpa_sim.c

   203	
 > 204	virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
   205						    u8 cmd)
   206	{
   207		struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
   208		virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
   209		size_t read;
   210	
   211		switch (cmd) {
   212		case VIRTIO_NET_CTRL_MAC_ADDR_SET:
   213			read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov,
   214						     (void *)vdpasim->config.mac,
   215						     ETH_ALEN);
   216			if (read == ETH_ALEN)
   217				status = VIRTIO_NET_OK;
   218			break;
   219		default:
   220			break;
   221		}
   222	
   223		return status;
   224	}
   225	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

[-- Attachment #2: config.gz --]
[-- Type: application/gzip, Size: 32325 bytes --]

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 00/21] Control VQ support in vDPA
  2020-12-17  9:02         ` Jason Wang
@ 2020-12-17 22:28           ` Michael S. Tsirkin
  -1 siblings, 0 replies; 136+ messages in thread
From: Michael S. Tsirkin @ 2020-12-17 22:28 UTC (permalink / raw)
  To: Jason Wang
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare

On Thu, Dec 17, 2020 at 05:02:49PM +0800, Jason Wang wrote:
> 
> On 2020/12/17 下午3:58, Michael S. Tsirkin wrote:
> > On Thu, Dec 17, 2020 at 11:30:18AM +0800, Jason Wang wrote:
> > > On 2020/12/16 下午5:47, Michael S. Tsirkin wrote:
> > > > On Wed, Dec 16, 2020 at 02:47:57PM +0800, Jason Wang wrote:
> > > > > Hi All:
> > > > > 
> > > > > This series tries to add the support for control virtqueue in vDPA.
> > > > > 
> > > > > Control virtqueue is used by networking device for accepting various
> > > > > commands from the driver. It's a must to support multiqueue and other
> > > > > configurations.
> > > > > 
> > > > > When used by vhost-vDPA bus driver for VM, the control virtqueue
> > > > > should be shadowed via userspace VMM (Qemu) instead of being assigned
> > > > > directly to Guest. This is because Qemu needs to know the device state
> > > > > in order to start and stop device correctly (e.g for Live Migration).
> > > > > 
> > > > > This requies to isolate the memory mapping for control virtqueue
> > > > > presented by vhost-vDPA to prevent guest from accesing it directly.
> > > > > To achieve this, vDPA introduce two new abstractions:
> > > > > 
> > > > > - address space: identified through address space id (ASID) and a set
> > > > >                    of memory mapping in maintained
> > > > > - virtqueue group: the minimal set of virtqueues that must share an
> > > > >                    address space
> > > > How will this support the pretty common case where control vq
> > > > is programmed by the kernel through the PF, and others by the VFs?
> > > 
> > > In this case, the VF parent need to provide a software control vq and decode
> > > the command then send them to VF.
> > 
> > But how does that tie to the address space infrastructure?
> 
> 
> In this case, address space is not a must.

That's ok, problem is I don't see how address space is going
to work in this case at all.

There's no address space there that userspace/guest can control.


> But the idea is to make control
> vq works for all types of hardware:
> 
> 1) control virtqueue is implemented via VF/PF communication
> 2) control virtqueue is implemented by VF but not through DMA
> 3) control virtqueue is implemented by VF DMA, it could be either a hardware
> control virtqueue or other type of DMA
> 
> The address space is a must for 3) to work and can work for both 1) and 2).
> 
> 
> > 
> > 
> > 
> > > > 
> > > > I actually thought the way to support it is by exposing
> > > > something like an "inject buffers" API which sends data to a given VQ.
> > > > Maybe an ioctl, and maybe down the road uio ring can support batching
> > > > these ....
> > > 
> > > So the virtuqueue allows the request to be processed asynchronously (e.g
> > > driver may choose to use interrupt for control vq). This means we need to
> > > support that in uAPI level.
> > I don't think we need to make it async, just a regular ioctl will do.
> > In fact no guest uses the asynchronous property.
> 
> 
> It was not forbidden by the spec then we need to support that. E.g we can
> not assume driver doesn't assign interrupt for cvq.
> 
> 
> > 
> > 
> > > And if we manage to do that, it's just another
> > > type of virtqueue.
> > > 
> > > For virtio-vDPA, this also means the extensions for queue processing which
> > > is a functional duplication.
> > I don't see why, just send it to the actual control vq :)
> 
> 
> But in the case you've pointed out, there's no hardware control vq in fact.
> 
> 
> > 
> > > Using what proposed in this series, we don't
> > > need any changes for kernel virtio drivers.
> > > 
> > > What's more important, this series could be used for future features that
> > > requires DMA isolation between virtqueues:
> > > 
> > > - report dirty pages via virtqueue
> > > - sub function level device slicing
> > 
> > I agree these are nice to have, but I am not sure basic control vq must
> > be tied to that.
> 
> 
> If the control virtqueue is implemented via DMA through VF, it looks like a
> must.
> 
> Thanks
> 
> 
> > 
> > > ...
> > > 
> > > Thanks
> > > 
> > > 
> > > > 
> > > > > Device needs to advertise the following attributes to vDPA:
> > > > > 
> > > > > - the number of address spaces supported in the device
> > > > > - the number of virtqueue groups supported in the device
> > > > > - the mappings from a specific virtqueue to its virtqueue groups
> > > > > 
> > > > > The mappings from virtqueue to virtqueue groups is fixed and defined
> > > > > by vDPA device driver. E.g:
> > > > > 
> > > > > - For the device that has hardware ASID support, it can simply
> > > > >     advertise a per virtqueue virtqueue group.
> > > > > - For the device that does not have hardware ASID support, it can
> > > > >     simply advertise a single virtqueue group that contains all
> > > > >     virtqueues. Or if it wants a software emulated control virtqueue, it
> > > > >     can advertise two virtqueue groups, one is for cvq, another is for
> > > > >     the rest virtqueues.
> > > > > 
> > > > > vDPA also allow to change the association between virtqueue group and
> > > > > address space. So in the case of control virtqueue, userspace
> > > > > VMM(Qemu) may use a dedicated address space for the control virtqueue
> > > > > group to isolate the memory mapping.
> > > > > 
> > > > > The vhost/vhost-vDPA is also extend for the userspace to:
> > > > > 
> > > > > - query the number of virtqueue groups and address spaces supported by
> > > > >     the device
> > > > > - query the virtqueue group for a specific virtqueue
> > > > > - assocaite a virtqueue group with an address space
> > > > > - send ASID based IOTLB commands
> > > > > 
> > > > > This will help userspace VMM(Qemu) to detect whether the control vq
> > > > > could be supported and isolate memory mappings of control virtqueue
> > > > > from the others.
> > > > > 
> > > > > To demonstrate the usage, vDPA simulator is extended to support
> > > > > setting MAC address via a emulated control virtqueue.
> > > > > 
> > > > > Please review.
> > > > > 
> > > > > Changes since RFC:
> > > > > 
> > > > > - tweak vhost uAPI documentation
> > > > > - switch to use device specific IOTLB really in patch 4
> > > > > - tweak the commit log
> > > > > - fix that ASID in vhost is claimed to be 32 actually but 16bit
> > > > >     actually
> > > > > - fix use after free when using ASID with IOTLB batching requests
> > > > > - switch to use Stefano's patch for having separated iov
> > > > > - remove unused "used_as" variable
> > > > > - fix the iotlb/asid checking in vhost_vdpa_unmap()
> > > > > 
> > > > > Thanks
> > > > > 
> > > > > Jason Wang (20):
> > > > >     vhost: move the backend feature bits to vhost_types.h
> > > > >     virtio-vdpa: don't set callback if virtio doesn't need it
> > > > >     vhost-vdpa: passing iotlb to IOMMU mapping helpers
> > > > >     vhost-vdpa: switch to use vhost-vdpa specific IOTLB
> > > > >     vdpa: add the missing comment for nvqs in struct vdpa_device
> > > > >     vdpa: introduce virtqueue groups
> > > > >     vdpa: multiple address spaces support
> > > > >     vdpa: introduce config operations for associating ASID to a virtqueue
> > > > >       group
> > > > >     vhost_iotlb: split out IOTLB initialization
> > > > >     vhost: support ASID in IOTLB API
> > > > >     vhost-vdpa: introduce asid based IOTLB
> > > > >     vhost-vdpa: introduce uAPI to get the number of virtqueue groups
> > > > >     vhost-vdpa: introduce uAPI to get the number of address spaces
> > > > >     vhost-vdpa: uAPI to get virtqueue group id
> > > > >     vhost-vdpa: introduce uAPI to set group ASID
> > > > >     vhost-vdpa: support ASID based IOTLB API
> > > > >     vdpa_sim: advertise VIRTIO_NET_F_MTU
> > > > >     vdpa_sim: factor out buffer completion logic
> > > > >     vdpa_sim: filter destination mac address
> > > > >     vdpasim: control virtqueue support
> > > > > 
> > > > > Stefano Garzarella (1):
> > > > >     vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov
> > > > > 
> > > > >    drivers/vdpa/ifcvf/ifcvf_main.c   |   9 +-
> > > > >    drivers/vdpa/mlx5/net/mlx5_vnet.c |  11 +-
> > > > >    drivers/vdpa/vdpa.c               |   8 +-
> > > > >    drivers/vdpa/vdpa_sim/vdpa_sim.c  | 292 ++++++++++++++++++++++++------
> > > > >    drivers/vhost/iotlb.c             |  23 ++-
> > > > >    drivers/vhost/vdpa.c              | 246 ++++++++++++++++++++-----
> > > > >    drivers/vhost/vhost.c             |  23 ++-
> > > > >    drivers/vhost/vhost.h             |   4 +-
> > > > >    drivers/virtio/virtio_vdpa.c      |   2 +-
> > > > >    include/linux/vdpa.h              |  42 ++++-
> > > > >    include/linux/vhost_iotlb.h       |   2 +
> > > > >    include/uapi/linux/vhost.h        |  25 ++-
> > > > >    include/uapi/linux/vhost_types.h  |  10 +-
> > > > >    13 files changed, 561 insertions(+), 136 deletions(-)
> > > > > 
> > > > > -- 
> > > > > 2.25.1


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 00/21] Control VQ support in vDPA
@ 2020-12-17 22:28           ` Michael S. Tsirkin
  0 siblings, 0 replies; 136+ messages in thread
From: Michael S. Tsirkin @ 2020-12-17 22:28 UTC (permalink / raw)
  To: Jason Wang
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

On Thu, Dec 17, 2020 at 05:02:49PM +0800, Jason Wang wrote:
> 
> On 2020/12/17 下午3:58, Michael S. Tsirkin wrote:
> > On Thu, Dec 17, 2020 at 11:30:18AM +0800, Jason Wang wrote:
> > > On 2020/12/16 下午5:47, Michael S. Tsirkin wrote:
> > > > On Wed, Dec 16, 2020 at 02:47:57PM +0800, Jason Wang wrote:
> > > > > Hi All:
> > > > > 
> > > > > This series tries to add the support for control virtqueue in vDPA.
> > > > > 
> > > > > Control virtqueue is used by networking device for accepting various
> > > > > commands from the driver. It's a must to support multiqueue and other
> > > > > configurations.
> > > > > 
> > > > > When used by vhost-vDPA bus driver for VM, the control virtqueue
> > > > > should be shadowed via userspace VMM (Qemu) instead of being assigned
> > > > > directly to Guest. This is because Qemu needs to know the device state
> > > > > in order to start and stop device correctly (e.g for Live Migration).
> > > > > 
> > > > > This requies to isolate the memory mapping for control virtqueue
> > > > > presented by vhost-vDPA to prevent guest from accesing it directly.
> > > > > To achieve this, vDPA introduce two new abstractions:
> > > > > 
> > > > > - address space: identified through address space id (ASID) and a set
> > > > >                    of memory mapping in maintained
> > > > > - virtqueue group: the minimal set of virtqueues that must share an
> > > > >                    address space
> > > > How will this support the pretty common case where control vq
> > > > is programmed by the kernel through the PF, and others by the VFs?
> > > 
> > > In this case, the VF parent need to provide a software control vq and decode
> > > the command then send them to VF.
> > 
> > But how does that tie to the address space infrastructure?
> 
> 
> In this case, address space is not a must.

That's ok, problem is I don't see how address space is going
to work in this case at all.

There's no address space there that userspace/guest can control.


> But the idea is to make control
> vq works for all types of hardware:
> 
> 1) control virtqueue is implemented via VF/PF communication
> 2) control virtqueue is implemented by VF but not through DMA
> 3) control virtqueue is implemented by VF DMA, it could be either a hardware
> control virtqueue or other type of DMA
> 
> The address space is a must for 3) to work and can work for both 1) and 2).
> 
> 
> > 
> > 
> > 
> > > > 
> > > > I actually thought the way to support it is by exposing
> > > > something like an "inject buffers" API which sends data to a given VQ.
> > > > Maybe an ioctl, and maybe down the road uio ring can support batching
> > > > these ....
> > > 
> > > So the virtuqueue allows the request to be processed asynchronously (e.g
> > > driver may choose to use interrupt for control vq). This means we need to
> > > support that in uAPI level.
> > I don't think we need to make it async, just a regular ioctl will do.
> > In fact no guest uses the asynchronous property.
> 
> 
> It was not forbidden by the spec then we need to support that. E.g we can
> not assume driver doesn't assign interrupt for cvq.
> 
> 
> > 
> > 
> > > And if we manage to do that, it's just another
> > > type of virtqueue.
> > > 
> > > For virtio-vDPA, this also means the extensions for queue processing which
> > > is a functional duplication.
> > I don't see why, just send it to the actual control vq :)
> 
> 
> But in the case you've pointed out, there's no hardware control vq in fact.
> 
> 
> > 
> > > Using what proposed in this series, we don't
> > > need any changes for kernel virtio drivers.
> > > 
> > > What's more important, this series could be used for future features that
> > > requires DMA isolation between virtqueues:
> > > 
> > > - report dirty pages via virtqueue
> > > - sub function level device slicing
> > 
> > I agree these are nice to have, but I am not sure basic control vq must
> > be tied to that.
> 
> 
> If the control virtqueue is implemented via DMA through VF, it looks like a
> must.
> 
> Thanks
> 
> 
> > 
> > > ...
> > > 
> > > Thanks
> > > 
> > > 
> > > > 
> > > > > Device needs to advertise the following attributes to vDPA:
> > > > > 
> > > > > - the number of address spaces supported in the device
> > > > > - the number of virtqueue groups supported in the device
> > > > > - the mappings from a specific virtqueue to its virtqueue groups
> > > > > 
> > > > > The mappings from virtqueue to virtqueue groups is fixed and defined
> > > > > by vDPA device driver. E.g:
> > > > > 
> > > > > - For the device that has hardware ASID support, it can simply
> > > > >     advertise a per virtqueue virtqueue group.
> > > > > - For the device that does not have hardware ASID support, it can
> > > > >     simply advertise a single virtqueue group that contains all
> > > > >     virtqueues. Or if it wants a software emulated control virtqueue, it
> > > > >     can advertise two virtqueue groups, one is for cvq, another is for
> > > > >     the rest virtqueues.
> > > > > 
> > > > > vDPA also allow to change the association between virtqueue group and
> > > > > address space. So in the case of control virtqueue, userspace
> > > > > VMM(Qemu) may use a dedicated address space for the control virtqueue
> > > > > group to isolate the memory mapping.
> > > > > 
> > > > > The vhost/vhost-vDPA is also extend for the userspace to:
> > > > > 
> > > > > - query the number of virtqueue groups and address spaces supported by
> > > > >     the device
> > > > > - query the virtqueue group for a specific virtqueue
> > > > > - assocaite a virtqueue group with an address space
> > > > > - send ASID based IOTLB commands
> > > > > 
> > > > > This will help userspace VMM(Qemu) to detect whether the control vq
> > > > > could be supported and isolate memory mappings of control virtqueue
> > > > > from the others.
> > > > > 
> > > > > To demonstrate the usage, vDPA simulator is extended to support
> > > > > setting MAC address via a emulated control virtqueue.
> > > > > 
> > > > > Please review.
> > > > > 
> > > > > Changes since RFC:
> > > > > 
> > > > > - tweak vhost uAPI documentation
> > > > > - switch to use device specific IOTLB really in patch 4
> > > > > - tweak the commit log
> > > > > - fix that ASID in vhost is claimed to be 32 actually but 16bit
> > > > >     actually
> > > > > - fix use after free when using ASID with IOTLB batching requests
> > > > > - switch to use Stefano's patch for having separated iov
> > > > > - remove unused "used_as" variable
> > > > > - fix the iotlb/asid checking in vhost_vdpa_unmap()
> > > > > 
> > > > > Thanks
> > > > > 
> > > > > Jason Wang (20):
> > > > >     vhost: move the backend feature bits to vhost_types.h
> > > > >     virtio-vdpa: don't set callback if virtio doesn't need it
> > > > >     vhost-vdpa: passing iotlb to IOMMU mapping helpers
> > > > >     vhost-vdpa: switch to use vhost-vdpa specific IOTLB
> > > > >     vdpa: add the missing comment for nvqs in struct vdpa_device
> > > > >     vdpa: introduce virtqueue groups
> > > > >     vdpa: multiple address spaces support
> > > > >     vdpa: introduce config operations for associating ASID to a virtqueue
> > > > >       group
> > > > >     vhost_iotlb: split out IOTLB initialization
> > > > >     vhost: support ASID in IOTLB API
> > > > >     vhost-vdpa: introduce asid based IOTLB
> > > > >     vhost-vdpa: introduce uAPI to get the number of virtqueue groups
> > > > >     vhost-vdpa: introduce uAPI to get the number of address spaces
> > > > >     vhost-vdpa: uAPI to get virtqueue group id
> > > > >     vhost-vdpa: introduce uAPI to set group ASID
> > > > >     vhost-vdpa: support ASID based IOTLB API
> > > > >     vdpa_sim: advertise VIRTIO_NET_F_MTU
> > > > >     vdpa_sim: factor out buffer completion logic
> > > > >     vdpa_sim: filter destination mac address
> > > > >     vdpasim: control virtqueue support
> > > > > 
> > > > > Stefano Garzarella (1):
> > > > >     vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov
> > > > > 
> > > > >    drivers/vdpa/ifcvf/ifcvf_main.c   |   9 +-
> > > > >    drivers/vdpa/mlx5/net/mlx5_vnet.c |  11 +-
> > > > >    drivers/vdpa/vdpa.c               |   8 +-
> > > > >    drivers/vdpa/vdpa_sim/vdpa_sim.c  | 292 ++++++++++++++++++++++++------
> > > > >    drivers/vhost/iotlb.c             |  23 ++-
> > > > >    drivers/vhost/vdpa.c              | 246 ++++++++++++++++++++-----
> > > > >    drivers/vhost/vhost.c             |  23 ++-
> > > > >    drivers/vhost/vhost.h             |   4 +-
> > > > >    drivers/virtio/virtio_vdpa.c      |   2 +-
> > > > >    include/linux/vdpa.h              |  42 ++++-
> > > > >    include/linux/vhost_iotlb.h       |   2 +
> > > > >    include/uapi/linux/vhost.h        |  25 ++-
> > > > >    include/uapi/linux/vhost_types.h  |  10 +-
> > > > >    13 files changed, 561 insertions(+), 136 deletions(-)
> > > > > 
> > > > > -- 
> > > > > 2.25.1

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 00/21] Control VQ support in vDPA
  2020-12-17 22:28           ` Michael S. Tsirkin
@ 2020-12-18  2:56             ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-18  2:56 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: eperezma, kvm, virtualization, netdev, linux-kernel, lulu, eli,
	lingshan.zhu, rob.miller, stefanha, sgarzare


On 2020/12/18 上午6:28, Michael S. Tsirkin wrote:
> On Thu, Dec 17, 2020 at 05:02:49PM +0800, Jason Wang wrote:
>> On 2020/12/17 下午3:58, Michael S. Tsirkin wrote:
>>> On Thu, Dec 17, 2020 at 11:30:18AM +0800, Jason Wang wrote:
>>>> On 2020/12/16 下午5:47, Michael S. Tsirkin wrote:
>>>>> On Wed, Dec 16, 2020 at 02:47:57PM +0800, Jason Wang wrote:
>>>>>> Hi All:
>>>>>>
>>>>>> This series tries to add the support for control virtqueue in vDPA.
>>>>>>
>>>>>> Control virtqueue is used by networking device for accepting various
>>>>>> commands from the driver. It's a must to support multiqueue and other
>>>>>> configurations.
>>>>>>
>>>>>> When used by vhost-vDPA bus driver for VM, the control virtqueue
>>>>>> should be shadowed via userspace VMM (Qemu) instead of being assigned
>>>>>> directly to Guest. This is because Qemu needs to know the device state
>>>>>> in order to start and stop device correctly (e.g for Live Migration).
>>>>>>
>>>>>> This requies to isolate the memory mapping for control virtqueue
>>>>>> presented by vhost-vDPA to prevent guest from accesing it directly.
>>>>>> To achieve this, vDPA introduce two new abstractions:
>>>>>>
>>>>>> - address space: identified through address space id (ASID) and a set
>>>>>>                     of memory mapping in maintained
>>>>>> - virtqueue group: the minimal set of virtqueues that must share an
>>>>>>                     address space
>>>>> How will this support the pretty common case where control vq
>>>>> is programmed by the kernel through the PF, and others by the VFs?
>>>> In this case, the VF parent need to provide a software control vq and decode
>>>> the command then send them to VF.
>>> But how does that tie to the address space infrastructure?
>> In this case, address space is not a must.
> That's ok, problem is I don't see how address space is going
> to work in this case at all.
>
> There's no address space there that userspace/guest can control.
>

The virtqueue group is mandated by parent but the association between 
virtqueue group and address space is under the control of userspace (Qemu).

A simple but common case is that:

1) Device advertise two virtqueue groups: group 0 contains RX and TX, 
group 1 contains CVQ.
2) Device advertise two address spaces

Then, for vhost-vDPA using by VM:

1) associate group 0 with as 0, group 1 with as 1 (via vhost-vDPA 
VHOST_VDPA_SET_GROUP_ASID)
2) Publish guest memory mapping via IOTLB asid 0
3) Publish control virtqueue mapping via IOTLB asid 1

Then the DMA is totally isolated in this case.

For vhost-vDPA using by DPDK or virtio-vDPA

1) associate group 0 and group 1 with as 0

since we don't need DMA isolation in this case.

In order to let it be controlled by Guest, we need extend virtio spec to 
support those concepts.

Thanks



^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 00/21] Control VQ support in vDPA
@ 2020-12-18  2:56             ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-18  2:56 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller


On 2020/12/18 上午6:28, Michael S. Tsirkin wrote:
> On Thu, Dec 17, 2020 at 05:02:49PM +0800, Jason Wang wrote:
>> On 2020/12/17 下午3:58, Michael S. Tsirkin wrote:
>>> On Thu, Dec 17, 2020 at 11:30:18AM +0800, Jason Wang wrote:
>>>> On 2020/12/16 下午5:47, Michael S. Tsirkin wrote:
>>>>> On Wed, Dec 16, 2020 at 02:47:57PM +0800, Jason Wang wrote:
>>>>>> Hi All:
>>>>>>
>>>>>> This series tries to add the support for control virtqueue in vDPA.
>>>>>>
>>>>>> Control virtqueue is used by networking device for accepting various
>>>>>> commands from the driver. It's a must to support multiqueue and other
>>>>>> configurations.
>>>>>>
>>>>>> When used by vhost-vDPA bus driver for VM, the control virtqueue
>>>>>> should be shadowed via userspace VMM (Qemu) instead of being assigned
>>>>>> directly to Guest. This is because Qemu needs to know the device state
>>>>>> in order to start and stop device correctly (e.g for Live Migration).
>>>>>>
>>>>>> This requies to isolate the memory mapping for control virtqueue
>>>>>> presented by vhost-vDPA to prevent guest from accesing it directly.
>>>>>> To achieve this, vDPA introduce two new abstractions:
>>>>>>
>>>>>> - address space: identified through address space id (ASID) and a set
>>>>>>                     of memory mapping in maintained
>>>>>> - virtqueue group: the minimal set of virtqueues that must share an
>>>>>>                     address space
>>>>> How will this support the pretty common case where control vq
>>>>> is programmed by the kernel through the PF, and others by the VFs?
>>>> In this case, the VF parent need to provide a software control vq and decode
>>>> the command then send them to VF.
>>> But how does that tie to the address space infrastructure?
>> In this case, address space is not a must.
> That's ok, problem is I don't see how address space is going
> to work in this case at all.
>
> There's no address space there that userspace/guest can control.
>

The virtqueue group is mandated by parent but the association between 
virtqueue group and address space is under the control of userspace (Qemu).

A simple but common case is that:

1) Device advertise two virtqueue groups: group 0 contains RX and TX, 
group 1 contains CVQ.
2) Device advertise two address spaces

Then, for vhost-vDPA using by VM:

1) associate group 0 with as 0, group 1 with as 1 (via vhost-vDPA 
VHOST_VDPA_SET_GROUP_ASID)
2) Publish guest memory mapping via IOTLB asid 0
3) Publish control virtqueue mapping via IOTLB asid 1

Then the DMA is totally isolated in this case.

For vhost-vDPA using by DPDK or virtio-vDPA

1) associate group 0 and group 1 with as 0

since we don't need DMA isolation in this case.

In order to let it be controlled by Guest, we need extend virtio spec to 
support those concepts.

Thanks


_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 07/21] vdpa: multiple address spaces support
  2020-12-16  6:48   ` Jason Wang
  (?)
@ 2020-12-29  7:28   ` Eli Cohen
  2020-12-30  4:00       ` Jason Wang
  2020-12-30  4:04       ` Jason Wang
  -1 siblings, 2 replies; 136+ messages in thread
From: Eli Cohen @ 2020-12-29  7:28 UTC (permalink / raw)
  To: Jason Wang
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare

On Wed, Dec 16, 2020 at 02:48:04PM +0800, Jason Wang wrote:
> This patches introduces the multiple address spaces support for vDPA
> device. This idea is to identify a specific address space via an
> dedicated identifier - ASID.
> 
> During vDPA device allocation, vDPA device driver needs to report the
> number of address spaces supported by the device then the DMA mapping
> ops of the vDPA device needs to be extended to support ASID.
> 
> This helps to isolate the environments for the virtqueue that will not
> be assigned directly. E.g in the case of virtio-net, the control
> virtqueue will not be assigned directly to guest.
> 
> As a start, simply claim 1 virtqueue groups and 1 address spaces for
> all vDPA devices. And vhost-vDPA will simply reject the device with
> more than 1 virtqueue groups or address spaces.
> 
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
>  drivers/vdpa/ifcvf/ifcvf_main.c   |  2 +-
>  drivers/vdpa/mlx5/net/mlx5_vnet.c |  5 +++--
>  drivers/vdpa/vdpa.c               |  4 +++-
>  drivers/vdpa/vdpa_sim/vdpa_sim.c  | 10 ++++++----
>  drivers/vhost/vdpa.c              | 14 +++++++++-----
>  include/linux/vdpa.h              | 23 ++++++++++++++++-------
>  6 files changed, 38 insertions(+), 20 deletions(-)
> 
> diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_main.c
> index c629f4fcc738..8a43f562b169 100644
> --- a/drivers/vdpa/ifcvf/ifcvf_main.c
> +++ b/drivers/vdpa/ifcvf/ifcvf_main.c
> @@ -445,7 +445,7 @@ static int ifcvf_probe(struct pci_dev *pdev, const struct pci_device_id *id)
>  
>  	adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa,
>  				    dev, &ifc_vdpa_ops,
> -				    IFCVF_MAX_QUEUE_PAIRS * 2, 1);
> +				    IFCVF_MAX_QUEUE_PAIRS * 2, 1, 1);
>  
>  	if (adapter == NULL) {
>  		IFCVF_ERR(pdev, "Failed to allocate vDPA structure");
> diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> index 719b52fcc547..7aaf0a4ee80d 100644
> --- a/drivers/vdpa/mlx5/net/mlx5_vnet.c
> +++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> @@ -1804,7 +1804,8 @@ static u32 mlx5_vdpa_get_generation(struct vdpa_device *vdev)
>  	return mvdev->generation;
>  }
>  
> -static int mlx5_vdpa_set_map(struct vdpa_device *vdev, struct vhost_iotlb *iotlb)
> +static int mlx5_vdpa_set_map(struct vdpa_device *vdev, unsigned int asid,
> +			     struct vhost_iotlb *iotlb)
>  {
>  	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
>  	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
> @@ -1947,7 +1948,7 @@ void *mlx5_vdpa_add_dev(struct mlx5_core_dev *mdev)
>  	max_vqs = min_t(u32, max_vqs, MLX5_MAX_SUPPORTED_VQS);
>  
>  	ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
> -				 2 * mlx5_vdpa_max_qps(max_vqs), 1);
> +				 2 * mlx5_vdpa_max_qps(max_vqs), 1, 1);
>  	if (IS_ERR(ndev))
>  		return ndev;
>  
> diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c
> index 46399746ec7c..05195fa7865d 100644
> --- a/drivers/vdpa/vdpa.c
> +++ b/drivers/vdpa/vdpa.c
> @@ -63,6 +63,7 @@ static void vdpa_release_dev(struct device *d)
>   * @config: the bus operations that is supported by this device
>   * @nvqs: number of virtqueues supported by this device
>   * @ngroups: number of groups supported by this device
> + * @nas: number of address spaces supported by this device
>   * @size: size of the parent structure that contains private data
>   *
>   * Driver should use vdpa_alloc_device() wrapper macro instead of
> @@ -74,7 +75,7 @@ static void vdpa_release_dev(struct device *d)
>  struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>  					const struct vdpa_config_ops *config,
>  					int nvqs, unsigned int ngroups,
> -					size_t size)
> +					unsigned int nas, size_t size)
>  {
>  	struct vdpa_device *vdev;
>  	int err = -EINVAL;
> @@ -102,6 +103,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>  	vdev->features_valid = false;
>  	vdev->nvqs = nvqs;
>  	vdev->ngroups = ngroups;
> +	vdev->nas = nas;
>  
>  	err = dev_set_name(&vdev->dev, "vdpa%u", vdev->index);
>  	if (err)
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> index 5d554b3cd152..140de45ffff2 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> @@ -359,7 +359,7 @@ static struct vdpasim *vdpasim_create(void)
>  		ops = &vdpasim_net_config_ops;
>  
>  	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
> -				    VDPASIM_VQ_NUM, 1);
> +				    VDPASIM_VQ_NUM, 1, 1);
>  	if (!vdpasim)
>  		goto err_alloc;
>  
> @@ -606,7 +606,7 @@ static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
>  	return range;
>  }
>  
> -static int vdpasim_set_map(struct vdpa_device *vdpa,
> +static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
>  			   struct vhost_iotlb *iotlb)
>  {
>  	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
> @@ -633,7 +633,8 @@ static int vdpasim_set_map(struct vdpa_device *vdpa,
>  	return ret;
>  }
>  
> -static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
> +static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
> +			   u64 iova, u64 size,
>  			   u64 pa, u32 perm)
>  {
>  	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
> @@ -647,7 +648,8 @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
>  	return ret;
>  }
>  
> -static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
> +static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
> +			     u64 iova, u64 size)
>  {
>  	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>  
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> index 9bcc03d4e68b..03a9b3311c6c 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -570,10 +570,10 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
>  		return r;
>  
>  	if (ops->dma_map) {
> -		r = ops->dma_map(vdpa, iova, size, pa, perm);
> +		r = ops->dma_map(vdpa, 0, iova, size, pa, perm);
>  	} else if (ops->set_map) {
>  		if (!v->in_batch)
> -			r = ops->set_map(vdpa, iotlb);
> +			r = ops->set_map(vdpa, 0, iotlb);
>  	} else {
>  		r = iommu_map(v->domain, iova, pa, size,
>  			      perm_to_iommu_flags(perm));
> @@ -597,10 +597,10 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
>  	vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
>  
>  	if (ops->dma_map) {
> -		ops->dma_unmap(vdpa, iova, size);
> +		ops->dma_unmap(vdpa, 0, iova, size);
>  	} else if (ops->set_map) {
>  		if (!v->in_batch)
> -			ops->set_map(vdpa, iotlb);
> +			ops->set_map(vdpa, 0, iotlb);
>  	} else {
>  		iommu_unmap(v->domain, iova, size);
>  	}
> @@ -764,7 +764,7 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
>  		break;
>  	case VHOST_IOTLB_BATCH_END:
>  		if (v->in_batch && ops->set_map)
> -			ops->set_map(vdpa, iotlb);
> +			ops->set_map(vdpa, 0, iotlb);
>  		v->in_batch = false;
>  		break;
>  	default:
> @@ -1032,6 +1032,10 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>  	int minor;
>  	int r;
>  
> +	/* Only support 1 address space and 1 groups */
> +	if (vdpa->ngroups != 1 || vdpa->nas != 1)
> +		return -ENOTSUPP;
> +
>  	/* Currently, we only accept the network devices. */
>  	if (ops->get_device_id(vdpa) != VIRTIO_ID_NET)
>  		return -ENOTSUPP;
> diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
> index bfc6790b263e..0a9a754f8180 100644
> --- a/include/linux/vdpa.h
> +++ b/include/linux/vdpa.h
> @@ -43,6 +43,8 @@ struct vdpa_vq_state {
>   * @index: device index
>   * @features_valid: were features initialized? for legacy guests
>   * @nvqs: the number of virtqueues
> + * @ngroups: the number of virtqueue groups
> + * @nas: the number of address spaces

I am not sure these can be categorised as part of the state of the VQ.
It's more of a property so maybe we can have a callback to get the
properties of the VQ?

Moreover, I think they should be handled in the hardware drivers to
return 1 for both ngroups and nas.

>   */
>  struct vdpa_device {
>  	struct device dev;
> @@ -52,6 +54,7 @@ struct vdpa_device {
>  	bool features_valid;
>  	int nvqs;
>  	unsigned int ngroups;
> +	unsigned int nas;
>  };
>  
>  /**
> @@ -175,6 +178,7 @@ struct vdpa_iova_range {
>   *				Needed for device that using device
>   *				specific DMA translation (on-chip IOMMU)
>   *				@vdev: vdpa device
> + *				@asid: address space identifier
>   *				@iotlb: vhost memory mapping to be
>   *				used by the vDPA
>   *				Returns integer: success (0) or error (< 0)
> @@ -183,6 +187,7 @@ struct vdpa_iova_range {
>   *				specific DMA translation (on-chip IOMMU)
>   *				and preferring incremental map.
>   *				@vdev: vdpa device
> + *				@asid: address space identifier
>   *				@iova: iova to be mapped
>   *				@size: size of the area
>   *				@pa: physical address for the map
> @@ -194,6 +199,7 @@ struct vdpa_iova_range {
>   *				specific DMA translation (on-chip IOMMU)
>   *				and preferring incremental unmap.
>   *				@vdev: vdpa device
> + *				@asid: address space identifier
>   *				@iova: iova to be unmapped
>   *				@size: size of the area
>   *				Returns integer: success (0) or error (< 0)
> @@ -240,10 +246,12 @@ struct vdpa_config_ops {
>  	struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
>  
>  	/* DMA ops */
> -	int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
> -	int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
> -		       u64 pa, u32 perm);
> -	int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
> +	int (*set_map)(struct vdpa_device *vdev, unsigned int asid,
> +		       struct vhost_iotlb *iotlb);
> +	int (*dma_map)(struct vdpa_device *vdev, unsigned int asid,
> +		       u64 iova, u64 size, u64 pa, u32 perm);
> +	int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
> +			 u64 iova, u64 size);
>  
>  	/* Free device resources */
>  	void (*free)(struct vdpa_device *vdev);
> @@ -252,11 +260,12 @@ struct vdpa_config_ops {
>  struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>  					const struct vdpa_config_ops *config,
>  					int nvqs, unsigned int ngroups,
> -					size_t size);
> +					unsigned int nas, size_t size);
>  
> -#define vdpa_alloc_device(dev_struct, member, parent, config, nvqs, ngroups) \
> +#define vdpa_alloc_device(dev_struct, member, parent, config, nvqs, \
> +			  ngroups, nas)				    \
>  			  container_of(__vdpa_alloc_device( \
> -				       parent, config, nvqs, ngroups, \
> +				       parent, config, nvqs, ngroups, nas,  \
>  				       sizeof(dev_struct) + \
>  				       BUILD_BUG_ON_ZERO(offsetof( \
>  				       dev_struct, member))), \
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 10/21] vhost: support ASID in IOTLB API
  2020-12-16  6:48   ` Jason Wang
  (?)
@ 2020-12-29 10:20   ` Eli Cohen
  2020-12-30  4:27       ` Jason Wang
  -1 siblings, 1 reply; 136+ messages in thread
From: Eli Cohen @ 2020-12-29 10:20 UTC (permalink / raw)
  To: Jason Wang
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare

On Wed, Dec 16, 2020 at 02:48:07PM +0800, Jason Wang wrote:
> This patches allows userspace to send ASID based IOTLB message to
> vhost. This idea is to use the reserved u32 field in the existing V2
> IOTLB message. Vhost device should advertise this capability via
> VHOST_BACKEND_F_IOTLB_ASID backend feature.
> 
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
>  drivers/vhost/vdpa.c             |  5 ++++-
>  drivers/vhost/vhost.c            | 23 ++++++++++++++++++-----
>  drivers/vhost/vhost.h            |  4 ++--
>  include/uapi/linux/vhost_types.h |  5 ++++-
>  4 files changed, 28 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> index 03a9b3311c6c..feb6a58df22d 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -739,7 +739,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
>  	return ret;
>  }
>  
> -static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
> +static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
>  					struct vhost_iotlb_msg *msg)
>  {
>  	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
> @@ -748,6 +748,9 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
>  	struct vhost_iotlb *iotlb = v->iotlb;
>  	int r = 0;
>  
> +	if (asid != 0)
> +		return -EINVAL;
> +
>  	r = vhost_dev_check_owner(dev);
>  	if (r)
>  		return r;
> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> index a262e12c6dc2..7477b724c29b 100644
> --- a/drivers/vhost/vhost.c
> +++ b/drivers/vhost/vhost.c
> @@ -468,7 +468,7 @@ void vhost_dev_init(struct vhost_dev *dev,
>  		    struct vhost_virtqueue **vqs, int nvqs,
>  		    int iov_limit, int weight, int byte_weight,
>  		    bool use_worker,
> -		    int (*msg_handler)(struct vhost_dev *dev,
> +		    int (*msg_handler)(struct vhost_dev *dev, u32 asid,
>  				       struct vhost_iotlb_msg *msg))
>  {
>  	struct vhost_virtqueue *vq;
> @@ -1084,11 +1084,14 @@ static bool umem_access_ok(u64 uaddr, u64 size, int access)
>  	return true;
>  }
>  
> -static int vhost_process_iotlb_msg(struct vhost_dev *dev,
> +static int vhost_process_iotlb_msg(struct vhost_dev *dev, u16 asid,
>  				   struct vhost_iotlb_msg *msg)
>  {
>  	int ret = 0;
>  
> +	if (asid != 0)
> +		return -EINVAL;
> +
>  	mutex_lock(&dev->mutex);
>  	vhost_dev_lock_vqs(dev);
>  	switch (msg->type) {
> @@ -1135,6 +1138,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
>  	struct vhost_iotlb_msg msg;
>  	size_t offset;
>  	int type, ret;
> +	u16 asid = 0;

You assume asid occupies just 16 bits. So maybe you should reserve the
other 16 bits for future extension:

struct vhost_msg_v2 {
        __u32 type;
-       __u32 reserved;
+       __u16 asid;
+       __u16 reserved;
        union {

Moreover, maybe this should be reflected in previous patches that use
the asid:

-static int mlx5_vdpa_set_map(struct vdpa_device *vdev, struct vhost_iotlb *iotlb)
+static int mlx5_vdpa_set_map(struct vdpa_device *vdev, u16 asid,
+                            struct vhost_iotlb *iotlb)

-static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
+static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u16 asid,
                                        struct vhost_iotlb_msg *msg)

etc.

>  
>  	ret = copy_from_iter(&type, sizeof(type), from);
>  	if (ret != sizeof(type)) {
> @@ -1150,7 +1154,16 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
>  		offset = offsetof(struct vhost_msg, iotlb) - sizeof(int);
>  		break;
>  	case VHOST_IOTLB_MSG_V2:
> -		offset = sizeof(__u32);
> +		if (vhost_backend_has_feature(dev->vqs[0],
> +					      VHOST_BACKEND_F_IOTLB_ASID)) {
> +			ret = copy_from_iter(&asid, sizeof(asid), from);
> +			if (ret != sizeof(asid)) {
> +				ret = -EINVAL;
> +				goto done;
> +			}
> +			offset = sizeof(__u16);
> +		} else
> +			offset = sizeof(__u32);
>  		break;
>  	default:
>  		ret = -EINVAL;
> @@ -1165,9 +1178,9 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
>  	}
>  
>  	if (dev->msg_handler)
> -		ret = dev->msg_handler(dev, &msg);
> +		ret = dev->msg_handler(dev, asid, &msg);
>  	else
> -		ret = vhost_process_iotlb_msg(dev, &msg);
> +		ret = vhost_process_iotlb_msg(dev, asid, &msg);
>  	if (ret) {
>  		ret = -EFAULT;
>  		goto done;
> diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
> index b063324c7669..19753a90875c 100644
> --- a/drivers/vhost/vhost.h
> +++ b/drivers/vhost/vhost.h
> @@ -162,7 +162,7 @@ struct vhost_dev {
>  	int byte_weight;
>  	u64 kcov_handle;
>  	bool use_worker;
> -	int (*msg_handler)(struct vhost_dev *dev,
> +	int (*msg_handler)(struct vhost_dev *dev, u32 asid,
>  			   struct vhost_iotlb_msg *msg);
>  };
>  
> @@ -170,7 +170,7 @@ bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
>  void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
>  		    int nvqs, int iov_limit, int weight, int byte_weight,
>  		    bool use_worker,
> -		    int (*msg_handler)(struct vhost_dev *dev,
> +		    int (*msg_handler)(struct vhost_dev *dev, u32 asid,
>  				       struct vhost_iotlb_msg *msg));
>  long vhost_dev_set_owner(struct vhost_dev *dev);
>  bool vhost_dev_has_owner(struct vhost_dev *dev);
> diff --git a/include/uapi/linux/vhost_types.h b/include/uapi/linux/vhost_types.h
> index 76ee7016c501..222fc66ce2ac 100644
> --- a/include/uapi/linux/vhost_types.h
> +++ b/include/uapi/linux/vhost_types.h
> @@ -87,7 +87,7 @@ struct vhost_msg {
>  
>  struct vhost_msg_v2 {
>  	__u32 type;
> -	__u32 reserved;
> +	__u32 asid;
>  	union {
>  		struct vhost_iotlb_msg iotlb;
>  		__u8 padding[64];
> @@ -157,5 +157,8 @@ struct vhost_vdpa_iova_range {
>  #define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1
>  /* IOTLB can accept batching hints */
>  #define VHOST_BACKEND_F_IOTLB_BATCH  0x2
> +/* IOTLB can accept address space identifier through V2 type of IOTLB
> +   message */
> +#define VHOST_BACKEND_F_IOTLB_ASID  0x3
>  
>  #endif
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 11/21] vhost-vdpa: introduce asid based IOTLB
  2020-12-16  6:48   ` Jason Wang
  (?)
@ 2020-12-29 11:41   ` Eli Cohen
  2020-12-30  6:23       ` Jason Wang
  -1 siblings, 1 reply; 136+ messages in thread
From: Eli Cohen @ 2020-12-29 11:41 UTC (permalink / raw)
  To: Jason Wang
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare

On Wed, Dec 16, 2020 at 02:48:08PM +0800, Jason Wang wrote:
> This patch converts the vhost-vDPA device to support multiple IOTLBs
> tagged via ASID via hlist. This will be used for supporting multiple
> address spaces in the following patches.
> 
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
>  drivers/vhost/vdpa.c | 106 ++++++++++++++++++++++++++++++++-----------
>  1 file changed, 80 insertions(+), 26 deletions(-)
> 
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> index feb6a58df22d..060d5b5b7e64 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -33,13 +33,21 @@ enum {
>  
>  #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
>  
> +#define VHOST_VDPA_IOTLB_BUCKETS 16
> +
> +struct vhost_vdpa_as {
> +	struct hlist_node hash_link;
> +	struct vhost_iotlb iotlb;
> +	u32 id;
> +};
> +
>  struct vhost_vdpa {
>  	struct vhost_dev vdev;
>  	struct iommu_domain *domain;
>  	struct vhost_virtqueue *vqs;
>  	struct completion completion;
>  	struct vdpa_device *vdpa;
> -	struct vhost_iotlb *iotlb;
> +	struct hlist_head as[VHOST_VDPA_IOTLB_BUCKETS];
>  	struct device dev;
>  	struct cdev cdev;
>  	atomic_t opened;
> @@ -49,12 +57,64 @@ struct vhost_vdpa {
>  	struct eventfd_ctx *config_ctx;
>  	int in_batch;
>  	struct vdpa_iova_range range;
> +	int used_as;

This is not really used. Not in this patch and later removed.

>  };
>  
>  static DEFINE_IDA(vhost_vdpa_ida);
>  
>  static dev_t vhost_vdpa_major;
>  
> +static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
> +{
> +	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
> +	struct vhost_vdpa_as *as;
> +
> +	hlist_for_each_entry(as, head, hash_link)
> +		if (as->id == asid)
> +			return as;
> +
> +	return NULL;
> +}
> +
> +static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
> +{
> +	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
> +	struct vhost_vdpa_as *as;
> +
> +	if (asid_to_as(v, asid))
> +		return NULL;
> +
> +	as = kmalloc(sizeof(*as), GFP_KERNEL);
> +	if (!as)
> +		return NULL;
> +
> +	vhost_iotlb_init(&as->iotlb, 0, 0);
> +	as->id = asid;
> +	hlist_add_head(&as->hash_link, head);
> +	++v->used_as;
> +
> +	return as;
> +}
> +
> +static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
> +{
> +	struct vhost_vdpa_as *as = asid_to_as(v, asid);
> +
> +	/* Remove default address space is not allowed */
> +	if (asid == 0)
> +		return -EINVAL;
> +
> +	if (!as)
> +		return -EINVAL;
> +
> +	hlist_del(&as->hash_link);
> +	vhost_iotlb_reset(&as->iotlb);
> +	kfree(as);
> +	--v->used_as;
> +
> +	return 0;
> +}
> +
>  static void handle_vq_kick(struct vhost_work *work)
>  {
>  	struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
> @@ -525,15 +585,6 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
>  	}
>  }
>  
> -static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v)
> -{
> -	struct vhost_iotlb *iotlb = v->iotlb;
> -
> -	vhost_vdpa_iotlb_unmap(v, iotlb, 0ULL, 0ULL - 1);
> -	kfree(v->iotlb);
> -	v->iotlb = NULL;
> -}
> -
>  static int perm_to_iommu_flags(u32 perm)
>  {
>  	int flags = 0;
> @@ -745,7 +796,8 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
>  	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
>  	struct vdpa_device *vdpa = v->vdpa;
>  	const struct vdpa_config_ops *ops = vdpa->config;
> -	struct vhost_iotlb *iotlb = v->iotlb;
> +	struct vhost_vdpa_as *as = asid_to_as(v, 0);
> +	struct vhost_iotlb *iotlb = &as->iotlb;
>  	int r = 0;
>  
>  	if (asid != 0)
> @@ -856,6 +908,13 @@ static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v)
>  	}
>  }
>  
> +static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
> +{
> +	vhost_dev_cleanup(&v->vdev);
> +	kfree(v->vdev.vqs);
> +	vhost_vdpa_remove_as(v, 0);
> +}
> +
>  static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>  {
>  	struct vhost_vdpa *v;
> @@ -886,15 +945,12 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>  	vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
>  		       vhost_vdpa_process_iotlb_msg);
>  
> -	v->iotlb = vhost_iotlb_alloc(0, 0);
> -	if (!v->iotlb) {
> -		r = -ENOMEM;
> -		goto err_init_iotlb;
> -	}
> +	if (!vhost_vdpa_alloc_as(v, 0))
> +		goto err_alloc_as;
>  
>  	r = vhost_vdpa_alloc_domain(v);
>  	if (r)
> -		goto err_alloc_domain;
> +		goto err_alloc_as;
>  
>  	vhost_vdpa_set_iova_range(v);
>  
> @@ -902,11 +958,8 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>  
>  	return 0;
>  
> -err_alloc_domain:
> -	vhost_vdpa_iotlb_free(v);
> -err_init_iotlb:
> -	vhost_dev_cleanup(&v->vdev);
> -	kfree(vqs);
> +err_alloc_as:
> +	vhost_vdpa_cleanup(v);
>  err:
>  	atomic_dec(&v->opened);
>  	return r;
> @@ -933,12 +986,10 @@ static int vhost_vdpa_release(struct inode *inode, struct file *filep)
>  	filep->private_data = NULL;
>  	vhost_vdpa_reset(v);
>  	vhost_dev_stop(&v->vdev);
> -	vhost_vdpa_iotlb_free(v);
>  	vhost_vdpa_free_domain(v);
>  	vhost_vdpa_config_put(v);
>  	vhost_vdpa_clean_irq(v);
> -	vhost_dev_cleanup(&v->vdev);
> -	kfree(v->vdev.vqs);
> +	vhost_vdpa_cleanup(v);
>  	mutex_unlock(&d->mutex);
>  
>  	atomic_dec(&v->opened);
> @@ -1033,7 +1084,7 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>  	const struct vdpa_config_ops *ops = vdpa->config;
>  	struct vhost_vdpa *v;
>  	int minor;
> -	int r;
> +	int i, r;
>  
>  	/* Only support 1 address space and 1 groups */
>  	if (vdpa->ngroups != 1 || vdpa->nas != 1)
> @@ -1085,6 +1136,9 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>  	init_completion(&v->completion);
>  	vdpa_set_drvdata(vdpa, v);
>  
> +	for (i = 0; i < VHOST_VDPA_IOTLB_BUCKETS; i++)
> +		INIT_HLIST_HEAD(&v->as[i]);
> +
>  	return 0;
>  
>  err:
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 11/21] vhost-vdpa: introduce asid based IOTLB
  2020-12-16  6:48   ` Jason Wang
  (?)
  (?)
@ 2020-12-29 11:53   ` Eli Cohen
  2020-12-30  6:34       ` Jason Wang
  -1 siblings, 1 reply; 136+ messages in thread
From: Eli Cohen @ 2020-12-29 11:53 UTC (permalink / raw)
  To: Jason Wang
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare

On Wed, Dec 16, 2020 at 02:48:08PM +0800, Jason Wang wrote:
> This patch converts the vhost-vDPA device to support multiple IOTLBs
> tagged via ASID via hlist. This will be used for supporting multiple
> address spaces in the following patches.
> 
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
>  drivers/vhost/vdpa.c | 106 ++++++++++++++++++++++++++++++++-----------
>  1 file changed, 80 insertions(+), 26 deletions(-)
> 
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> index feb6a58df22d..060d5b5b7e64 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -33,13 +33,21 @@ enum {
>  
>  #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
>  
> +#define VHOST_VDPA_IOTLB_BUCKETS 16
> +
> +struct vhost_vdpa_as {
> +	struct hlist_node hash_link;
> +	struct vhost_iotlb iotlb;
> +	u32 id;
> +};
> +
>  struct vhost_vdpa {
>  	struct vhost_dev vdev;
>  	struct iommu_domain *domain;
>  	struct vhost_virtqueue *vqs;
>  	struct completion completion;
>  	struct vdpa_device *vdpa;
> -	struct vhost_iotlb *iotlb;
> +	struct hlist_head as[VHOST_VDPA_IOTLB_BUCKETS];
>  	struct device dev;
>  	struct cdev cdev;
>  	atomic_t opened;
> @@ -49,12 +57,64 @@ struct vhost_vdpa {
>  	struct eventfd_ctx *config_ctx;
>  	int in_batch;
>  	struct vdpa_iova_range range;
> +	int used_as;
>  };
>  
>  static DEFINE_IDA(vhost_vdpa_ida);
>  
>  static dev_t vhost_vdpa_major;
>  
> +static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
> +{
> +	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
> +	struct vhost_vdpa_as *as;
> +
> +	hlist_for_each_entry(as, head, hash_link)
> +		if (as->id == asid)
> +			return as;
> +
> +	return NULL;
> +}
> +
> +static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
> +{
> +	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
> +	struct vhost_vdpa_as *as;
> +
> +	if (asid_to_as(v, asid))
> +		return NULL;
> +
> +	as = kmalloc(sizeof(*as), GFP_KERNEL);

kzalloc()? See comment below.

> +	if (!as)
> +		return NULL;
> +
> +	vhost_iotlb_init(&as->iotlb, 0, 0);
> +	as->id = asid;
> +	hlist_add_head(&as->hash_link, head);
> +	++v->used_as;

Although you eventually ended up removing used_as, this is a bug since
you're incrementing a random value. Maybe it's better to be on the safe
side and use kzalloc() for as above.

> +
> +	return as;
> +}
> +
> +static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
> +{
> +	struct vhost_vdpa_as *as = asid_to_as(v, asid);
> +
> +	/* Remove default address space is not allowed */
> +	if (asid == 0)
> +		return -EINVAL;
> +
> +	if (!as)
> +		return -EINVAL;
> +
> +	hlist_del(&as->hash_link);
> +	vhost_iotlb_reset(&as->iotlb);
> +	kfree(as);
> +	--v->used_as;
> +
> +	return 0;
> +}
> +
>  static void handle_vq_kick(struct vhost_work *work)
>  {
>  	struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
> @@ -525,15 +585,6 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
>  	}
>  }
>  
> -static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v)
> -{
> -	struct vhost_iotlb *iotlb = v->iotlb;
> -
> -	vhost_vdpa_iotlb_unmap(v, iotlb, 0ULL, 0ULL - 1);
> -	kfree(v->iotlb);
> -	v->iotlb = NULL;
> -}
> -
>  static int perm_to_iommu_flags(u32 perm)
>  {
>  	int flags = 0;
> @@ -745,7 +796,8 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
>  	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
>  	struct vdpa_device *vdpa = v->vdpa;
>  	const struct vdpa_config_ops *ops = vdpa->config;
> -	struct vhost_iotlb *iotlb = v->iotlb;
> +	struct vhost_vdpa_as *as = asid_to_as(v, 0);
> +	struct vhost_iotlb *iotlb = &as->iotlb;
>  	int r = 0;
>  
>  	if (asid != 0)
> @@ -856,6 +908,13 @@ static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v)
>  	}
>  }
>  
> +static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
> +{
> +	vhost_dev_cleanup(&v->vdev);
> +	kfree(v->vdev.vqs);
> +	vhost_vdpa_remove_as(v, 0);
> +}
> +
>  static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>  {
>  	struct vhost_vdpa *v;
> @@ -886,15 +945,12 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>  	vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
>  		       vhost_vdpa_process_iotlb_msg);
>  
> -	v->iotlb = vhost_iotlb_alloc(0, 0);
> -	if (!v->iotlb) {
> -		r = -ENOMEM;
> -		goto err_init_iotlb;
> -	}
> +	if (!vhost_vdpa_alloc_as(v, 0))
> +		goto err_alloc_as;
>  
>  	r = vhost_vdpa_alloc_domain(v);
>  	if (r)
> -		goto err_alloc_domain;
> +		goto err_alloc_as;
>  
>  	vhost_vdpa_set_iova_range(v);
>  
> @@ -902,11 +958,8 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>  
>  	return 0;
>  
> -err_alloc_domain:
> -	vhost_vdpa_iotlb_free(v);
> -err_init_iotlb:
> -	vhost_dev_cleanup(&v->vdev);
> -	kfree(vqs);
> +err_alloc_as:
> +	vhost_vdpa_cleanup(v);
>  err:
>  	atomic_dec(&v->opened);
>  	return r;
> @@ -933,12 +986,10 @@ static int vhost_vdpa_release(struct inode *inode, struct file *filep)
>  	filep->private_data = NULL;
>  	vhost_vdpa_reset(v);
>  	vhost_dev_stop(&v->vdev);
> -	vhost_vdpa_iotlb_free(v);
>  	vhost_vdpa_free_domain(v);
>  	vhost_vdpa_config_put(v);
>  	vhost_vdpa_clean_irq(v);
> -	vhost_dev_cleanup(&v->vdev);
> -	kfree(v->vdev.vqs);
> +	vhost_vdpa_cleanup(v);
>  	mutex_unlock(&d->mutex);
>  
>  	atomic_dec(&v->opened);
> @@ -1033,7 +1084,7 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>  	const struct vdpa_config_ops *ops = vdpa->config;
>  	struct vhost_vdpa *v;
>  	int minor;
> -	int r;
> +	int i, r;
>  
>  	/* Only support 1 address space and 1 groups */
>  	if (vdpa->ngroups != 1 || vdpa->nas != 1)
> @@ -1085,6 +1136,9 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>  	init_completion(&v->completion);
>  	vdpa_set_drvdata(vdpa, v);
>  
> +	for (i = 0; i < VHOST_VDPA_IOTLB_BUCKETS; i++)
> +		INIT_HLIST_HEAD(&v->as[i]);
> +
>  	return 0;
>  
>  err:
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 11/21] vhost-vdpa: introduce asid based IOTLB
  2020-12-16  6:48   ` Jason Wang
                     ` (2 preceding siblings ...)
  (?)
@ 2020-12-29 12:05   ` Eli Cohen
  2020-12-30  6:33       ` Jason Wang
  -1 siblings, 1 reply; 136+ messages in thread
From: Eli Cohen @ 2020-12-29 12:05 UTC (permalink / raw)
  To: Jason Wang
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare

On Wed, Dec 16, 2020 at 02:48:08PM +0800, Jason Wang wrote:
> This patch converts the vhost-vDPA device to support multiple IOTLBs
> tagged via ASID via hlist. This will be used for supporting multiple
> address spaces in the following patches.
> 
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
>  drivers/vhost/vdpa.c | 106 ++++++++++++++++++++++++++++++++-----------
>  1 file changed, 80 insertions(+), 26 deletions(-)
> 
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> index feb6a58df22d..060d5b5b7e64 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -33,13 +33,21 @@ enum {
>  
>  #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
>  
> +#define VHOST_VDPA_IOTLB_BUCKETS 16
> +
> +struct vhost_vdpa_as {
> +	struct hlist_node hash_link;
> +	struct vhost_iotlb iotlb;
> +	u32 id;
> +};
> +
>  struct vhost_vdpa {
>  	struct vhost_dev vdev;
>  	struct iommu_domain *domain;
>  	struct vhost_virtqueue *vqs;
>  	struct completion completion;
>  	struct vdpa_device *vdpa;
> -	struct vhost_iotlb *iotlb;
> +	struct hlist_head as[VHOST_VDPA_IOTLB_BUCKETS];
>  	struct device dev;
>  	struct cdev cdev;
>  	atomic_t opened;
> @@ -49,12 +57,64 @@ struct vhost_vdpa {
>  	struct eventfd_ctx *config_ctx;
>  	int in_batch;
>  	struct vdpa_iova_range range;
> +	int used_as;
>  };
>  
>  static DEFINE_IDA(vhost_vdpa_ida);
>  
>  static dev_t vhost_vdpa_major;
>  
> +static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
> +{
> +	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
> +	struct vhost_vdpa_as *as;
> +
> +	hlist_for_each_entry(as, head, hash_link)
> +		if (as->id == asid)
> +			return as;
> +
> +	return NULL;
> +}
> +
> +static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
> +{
> +	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
> +	struct vhost_vdpa_as *as;
> +
> +	if (asid_to_as(v, asid))
> +		return NULL;
> +
> +	as = kmalloc(sizeof(*as), GFP_KERNEL);
> +	if (!as)
> +		return NULL;
> +
> +	vhost_iotlb_init(&as->iotlb, 0, 0);
> +	as->id = asid;
> +	hlist_add_head(&as->hash_link, head);
> +	++v->used_as;
> +
> +	return as;
> +}
> +
> +static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)

The return value is never interpreted. I think it should either be made
void or return values checked.

> +{
> +	struct vhost_vdpa_as *as = asid_to_as(v, asid);
> +
> +	/* Remove default address space is not allowed */
> +	if (asid == 0)
> +		return -EINVAL;

Can you explain why? I think you have a memory leak due to this as no
one will ever free as with id 0.

> +
> +	if (!as)
> +		return -EINVAL;
> +
> +	hlist_del(&as->hash_link);
> +	vhost_iotlb_reset(&as->iotlb);
> +	kfree(as);
> +	--v->used_as;
> +
> +	return 0;
> +}
> +
>  static void handle_vq_kick(struct vhost_work *work)
>  {
>  	struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
> @@ -525,15 +585,6 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
>  	}
>  }
>  
> -static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v)
> -{
> -	struct vhost_iotlb *iotlb = v->iotlb;
> -
> -	vhost_vdpa_iotlb_unmap(v, iotlb, 0ULL, 0ULL - 1);
> -	kfree(v->iotlb);
> -	v->iotlb = NULL;
> -}
> -
>  static int perm_to_iommu_flags(u32 perm)
>  {
>  	int flags = 0;
> @@ -745,7 +796,8 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
>  	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
>  	struct vdpa_device *vdpa = v->vdpa;
>  	const struct vdpa_config_ops *ops = vdpa->config;
> -	struct vhost_iotlb *iotlb = v->iotlb;
> +	struct vhost_vdpa_as *as = asid_to_as(v, 0);
> +	struct vhost_iotlb *iotlb = &as->iotlb;
>  	int r = 0;
>  
>  	if (asid != 0)
> @@ -856,6 +908,13 @@ static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v)
>  	}
>  }
>  
> +static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
> +{
> +	vhost_dev_cleanup(&v->vdev);
> +	kfree(v->vdev.vqs);
> +	vhost_vdpa_remove_as(v, 0);
> +}
> +
>  static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>  {
>  	struct vhost_vdpa *v;
> @@ -886,15 +945,12 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>  	vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
>  		       vhost_vdpa_process_iotlb_msg);
>  
> -	v->iotlb = vhost_iotlb_alloc(0, 0);
> -	if (!v->iotlb) {
> -		r = -ENOMEM;
> -		goto err_init_iotlb;
> -	}
> +	if (!vhost_vdpa_alloc_as(v, 0))
> +		goto err_alloc_as;
>  
>  	r = vhost_vdpa_alloc_domain(v);
>  	if (r)
> -		goto err_alloc_domain;
> +		goto err_alloc_as;
>  
>  	vhost_vdpa_set_iova_range(v);
>  
> @@ -902,11 +958,8 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>  
>  	return 0;
>  
> -err_alloc_domain:
> -	vhost_vdpa_iotlb_free(v);
> -err_init_iotlb:
> -	vhost_dev_cleanup(&v->vdev);
> -	kfree(vqs);
> +err_alloc_as:
> +	vhost_vdpa_cleanup(v);
>  err:
>  	atomic_dec(&v->opened);
>  	return r;
> @@ -933,12 +986,10 @@ static int vhost_vdpa_release(struct inode *inode, struct file *filep)
>  	filep->private_data = NULL;
>  	vhost_vdpa_reset(v);
>  	vhost_dev_stop(&v->vdev);
> -	vhost_vdpa_iotlb_free(v);
>  	vhost_vdpa_free_domain(v);
>  	vhost_vdpa_config_put(v);
>  	vhost_vdpa_clean_irq(v);
> -	vhost_dev_cleanup(&v->vdev);
> -	kfree(v->vdev.vqs);
> +	vhost_vdpa_cleanup(v);
>  	mutex_unlock(&d->mutex);
>  
>  	atomic_dec(&v->opened);
> @@ -1033,7 +1084,7 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>  	const struct vdpa_config_ops *ops = vdpa->config;
>  	struct vhost_vdpa *v;
>  	int minor;
> -	int r;
> +	int i, r;
>  
>  	/* Only support 1 address space and 1 groups */
>  	if (vdpa->ngroups != 1 || vdpa->nas != 1)
> @@ -1085,6 +1136,9 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>  	init_completion(&v->completion);
>  	vdpa_set_drvdata(vdpa, v);
>  
> +	for (i = 0; i < VHOST_VDPA_IOTLB_BUCKETS; i++)
> +		INIT_HLIST_HEAD(&v->as[i]);
> +
>  	return 0;
>  
>  err:
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 12/21] vhost-vdpa: introduce uAPI to get the number of virtqueue groups
  2020-12-16  6:48   ` Jason Wang
  (?)
@ 2020-12-29 12:24   ` Eli Cohen
  2020-12-30  6:49       ` Jason Wang
  -1 siblings, 1 reply; 136+ messages in thread
From: Eli Cohen @ 2020-12-29 12:24 UTC (permalink / raw)
  To: Jason Wang
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare

On Wed, Dec 16, 2020 at 02:48:09PM +0800, Jason Wang wrote:
> Follows the vDPA support for multiple address spaces, this patch
> introduce uAPI for the userspace to know the number of virtqueue
> groups supported by the vDPA device.

Can you explain what exactly you mean be userspace? Is it just qemu or
is it destined to the virtio_net driver run by the qemu process?
Also can you say for what purpose?

> 
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
>  drivers/vhost/vdpa.c       | 4 ++++
>  include/uapi/linux/vhost.h | 3 +++
>  2 files changed, 7 insertions(+)
> 
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> index 060d5b5b7e64..1ba5901b28e7 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -536,6 +536,10 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
>  	case VHOST_VDPA_GET_VRING_NUM:
>  		r = vhost_vdpa_get_vring_num(v, argp);
>  		break;
> +	case VHOST_VDPA_GET_GROUP_NUM:
> +		r = copy_to_user(argp, &v->vdpa->ngroups,
> +				 sizeof(v->vdpa->ngroups));
> +		break;
>  	case VHOST_SET_LOG_BASE:
>  	case VHOST_SET_LOG_FD:
>  		r = -ENOIOCTLCMD;
> diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
> index 59c6c0fbaba1..8a4e6e426bbf 100644
> --- a/include/uapi/linux/vhost.h
> +++ b/include/uapi/linux/vhost.h
> @@ -145,4 +145,7 @@
>  /* Get the valid iova range */
>  #define VHOST_VDPA_GET_IOVA_RANGE	_IOR(VHOST_VIRTIO, 0x78, \
>  					     struct vhost_vdpa_iova_range)
> +/* Get the number of virtqueue groups. */
> +#define VHOST_VDPA_GET_GROUP_NUM	_IOR(VHOST_VIRTIO, 0x79, unsigned int)
> +
>  #endif
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 07/21] vdpa: multiple address spaces support
  2020-12-29  7:28   ` Eli Cohen
@ 2020-12-30  4:00       ` Jason Wang
  2020-12-30  4:04       ` Jason Wang
  1 sibling, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-30  4:00 UTC (permalink / raw)
  To: Eli Cohen
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare


On 2020/12/29 下午3:28, Eli Cohen wrote:
>> @@ -43,6 +43,8 @@ struct vdpa_vq_state {
>>    * @index: device index
>>    * @features_valid: were features initialized? for legacy guests
>>    * @nvqs: the number of virtqueues
>> + * @ngroups: the number of virtqueue groups
>> + * @nas: the number of address spaces
> I am not sure these can be categorised as part of the state of the VQ.
> It's more of a property so maybe we can have a callback to get the
> properties of the VQ?
>
> Moreover, I think they should be handled in the hardware drivers to
> return 1 for both ngroups and nas.


We can, but those are static attributes that is not expected to be 
changed by the driver.

Introduce callbacks for those static stuffs does not give us any advantage.

For group id and notification area, since we don't have a abstract of 
vdpa_virtqueue. The only choice is to introduce callbacks for them.

Maybe it's time to introduce vdpa_virtqueue.

Thanks


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 07/21] vdpa: multiple address spaces support
@ 2020-12-30  4:00       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-30  4:00 UTC (permalink / raw)
  To: Eli Cohen
  Cc: lulu, kvm, mst, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller


On 2020/12/29 下午3:28, Eli Cohen wrote:
>> @@ -43,6 +43,8 @@ struct vdpa_vq_state {
>>    * @index: device index
>>    * @features_valid: were features initialized? for legacy guests
>>    * @nvqs: the number of virtqueues
>> + * @ngroups: the number of virtqueue groups
>> + * @nas: the number of address spaces
> I am not sure these can be categorised as part of the state of the VQ.
> It's more of a property so maybe we can have a callback to get the
> properties of the VQ?
>
> Moreover, I think they should be handled in the hardware drivers to
> return 1 for both ngroups and nas.


We can, but those are static attributes that is not expected to be 
changed by the driver.

Introduce callbacks for those static stuffs does not give us any advantage.

For group id and notification area, since we don't have a abstract of 
vdpa_virtqueue. The only choice is to introduce callbacks for them.

Maybe it's time to introduce vdpa_virtqueue.

Thanks

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 07/21] vdpa: multiple address spaces support
  2020-12-29  7:28   ` Eli Cohen
@ 2020-12-30  4:04       ` Jason Wang
  2020-12-30  4:04       ` Jason Wang
  1 sibling, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-30  4:04 UTC (permalink / raw)
  To: Eli Cohen
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare


On 2020/12/29 下午3:28, Eli Cohen wrote:
>> @@ -43,6 +43,8 @@ struct vdpa_vq_state {
>>    * @index: device index
>>    * @features_valid: were features initialized? for legacy guests
>>    * @nvqs: the number of virtqueues
>> + * @ngroups: the number of virtqueue groups
>> + * @nas: the number of address spaces
> I am not sure these can be categorised as part of the state of the VQ.
> It's more of a property so maybe we can have a callback to get the
> properties of the VQ?


Or maybe there's a misunderstanding of the patch.

Those two attributes belongs to vdpa_device instead of vdpa_vq_state 
actually.

Thanks


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 07/21] vdpa: multiple address spaces support
@ 2020-12-30  4:04       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-30  4:04 UTC (permalink / raw)
  To: Eli Cohen
  Cc: lulu, kvm, mst, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller


On 2020/12/29 下午3:28, Eli Cohen wrote:
>> @@ -43,6 +43,8 @@ struct vdpa_vq_state {
>>    * @index: device index
>>    * @features_valid: were features initialized? for legacy guests
>>    * @nvqs: the number of virtqueues
>> + * @ngroups: the number of virtqueue groups
>> + * @nas: the number of address spaces
> I am not sure these can be categorised as part of the state of the VQ.
> It's more of a property so maybe we can have a callback to get the
> properties of the VQ?


Or maybe there's a misunderstanding of the patch.

Those two attributes belongs to vdpa_device instead of vdpa_vq_state 
actually.

Thanks

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 10/21] vhost: support ASID in IOTLB API
  2020-12-29 10:20   ` Eli Cohen
@ 2020-12-30  4:27       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-30  4:27 UTC (permalink / raw)
  To: Eli Cohen
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare


On 2020/12/29 下午6:20, Eli Cohen wrote:
>> -static int vhost_process_iotlb_msg(struct vhost_dev *dev,
>> +static int vhost_process_iotlb_msg(struct vhost_dev *dev, u16 asid,
>>   				   struct vhost_iotlb_msg *msg)
>>   {
>>   	int ret = 0;
>>   
>> +	if (asid != 0)
>> +		return -EINVAL;
>> +
>>   	mutex_lock(&dev->mutex);
>>   	vhost_dev_lock_vqs(dev);
>>   	switch (msg->type) {
>> @@ -1135,6 +1138,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
>>   	struct vhost_iotlb_msg msg;
>>   	size_t offset;
>>   	int type, ret;
>> +	u16 asid = 0;
> You assume asid occupies just 16 bits. So maybe you should reserve the
> other 16 bits for future extension:
>
> struct vhost_msg_v2 {
>          __u32 type;
> -       __u32 reserved;
> +       __u16 asid;
> +       __u16 reserved;
>          union {
>
> Moreover, maybe this should be reflected in previous patches that use
> the asid:
>
> -static int mlx5_vdpa_set_map(struct vdpa_device *vdev, struct vhost_iotlb *iotlb)
> +static int mlx5_vdpa_set_map(struct vdpa_device *vdev, u16 asid,
> +                            struct vhost_iotlb *iotlb)
>
> -static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
> +static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u16 asid,
>                                          struct vhost_iotlb_msg *msg)
>
> etc.


Good catch.

This is a bug of the code actually. Since I want to stick to 32bit to be 
large enough for e.g PASID.

Will fix.

Thanks


>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 10/21] vhost: support ASID in IOTLB API
@ 2020-12-30  4:27       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-30  4:27 UTC (permalink / raw)
  To: Eli Cohen
  Cc: lulu, kvm, mst, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller


On 2020/12/29 下午6:20, Eli Cohen wrote:
>> -static int vhost_process_iotlb_msg(struct vhost_dev *dev,
>> +static int vhost_process_iotlb_msg(struct vhost_dev *dev, u16 asid,
>>   				   struct vhost_iotlb_msg *msg)
>>   {
>>   	int ret = 0;
>>   
>> +	if (asid != 0)
>> +		return -EINVAL;
>> +
>>   	mutex_lock(&dev->mutex);
>>   	vhost_dev_lock_vqs(dev);
>>   	switch (msg->type) {
>> @@ -1135,6 +1138,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
>>   	struct vhost_iotlb_msg msg;
>>   	size_t offset;
>>   	int type, ret;
>> +	u16 asid = 0;
> You assume asid occupies just 16 bits. So maybe you should reserve the
> other 16 bits for future extension:
>
> struct vhost_msg_v2 {
>          __u32 type;
> -       __u32 reserved;
> +       __u16 asid;
> +       __u16 reserved;
>          union {
>
> Moreover, maybe this should be reflected in previous patches that use
> the asid:
>
> -static int mlx5_vdpa_set_map(struct vdpa_device *vdev, struct vhost_iotlb *iotlb)
> +static int mlx5_vdpa_set_map(struct vdpa_device *vdev, u16 asid,
> +                            struct vhost_iotlb *iotlb)
>
> -static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
> +static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u16 asid,
>                                          struct vhost_iotlb_msg *msg)
>
> etc.


Good catch.

This is a bug of the code actually. Since I want to stick to 32bit to be 
large enough for e.g PASID.

Will fix.

Thanks


>

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 11/21] vhost-vdpa: introduce asid based IOTLB
  2020-12-29 11:41   ` Eli Cohen
@ 2020-12-30  6:23       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-30  6:23 UTC (permalink / raw)
  To: Eli Cohen
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare


On 2020/12/29 下午7:41, Eli Cohen wrote:
> On Wed, Dec 16, 2020 at 02:48:08PM +0800, Jason Wang wrote:
>> This patch converts the vhost-vDPA device to support multiple IOTLBs
>> tagged via ASID via hlist. This will be used for supporting multiple
>> address spaces in the following patches.
>>
>> Signed-off-by: Jason Wang <jasowang@redhat.com>
>> ---
>>   drivers/vhost/vdpa.c | 106 ++++++++++++++++++++++++++++++++-----------
>>   1 file changed, 80 insertions(+), 26 deletions(-)
>>
>> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
>> index feb6a58df22d..060d5b5b7e64 100644
>> --- a/drivers/vhost/vdpa.c
>> +++ b/drivers/vhost/vdpa.c
>> @@ -33,13 +33,21 @@ enum {
>>   
>>   #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
>>   
>> +#define VHOST_VDPA_IOTLB_BUCKETS 16
>> +
>> +struct vhost_vdpa_as {
>> +	struct hlist_node hash_link;
>> +	struct vhost_iotlb iotlb;
>> +	u32 id;
>> +};
>> +
>>   struct vhost_vdpa {
>>   	struct vhost_dev vdev;
>>   	struct iommu_domain *domain;
>>   	struct vhost_virtqueue *vqs;
>>   	struct completion completion;
>>   	struct vdpa_device *vdpa;
>> -	struct vhost_iotlb *iotlb;
>> +	struct hlist_head as[VHOST_VDPA_IOTLB_BUCKETS];
>>   	struct device dev;
>>   	struct cdev cdev;
>>   	atomic_t opened;
>> @@ -49,12 +57,64 @@ struct vhost_vdpa {
>>   	struct eventfd_ctx *config_ctx;
>>   	int in_batch;
>>   	struct vdpa_iova_range range;
>> +	int used_as;
> This is not really used. Not in this patch and later removed.


Right, will remove this.

Thanks



^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 11/21] vhost-vdpa: introduce asid based IOTLB
@ 2020-12-30  6:23       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-30  6:23 UTC (permalink / raw)
  To: Eli Cohen
  Cc: lulu, kvm, mst, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller


On 2020/12/29 下午7:41, Eli Cohen wrote:
> On Wed, Dec 16, 2020 at 02:48:08PM +0800, Jason Wang wrote:
>> This patch converts the vhost-vDPA device to support multiple IOTLBs
>> tagged via ASID via hlist. This will be used for supporting multiple
>> address spaces in the following patches.
>>
>> Signed-off-by: Jason Wang <jasowang@redhat.com>
>> ---
>>   drivers/vhost/vdpa.c | 106 ++++++++++++++++++++++++++++++++-----------
>>   1 file changed, 80 insertions(+), 26 deletions(-)
>>
>> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
>> index feb6a58df22d..060d5b5b7e64 100644
>> --- a/drivers/vhost/vdpa.c
>> +++ b/drivers/vhost/vdpa.c
>> @@ -33,13 +33,21 @@ enum {
>>   
>>   #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
>>   
>> +#define VHOST_VDPA_IOTLB_BUCKETS 16
>> +
>> +struct vhost_vdpa_as {
>> +	struct hlist_node hash_link;
>> +	struct vhost_iotlb iotlb;
>> +	u32 id;
>> +};
>> +
>>   struct vhost_vdpa {
>>   	struct vhost_dev vdev;
>>   	struct iommu_domain *domain;
>>   	struct vhost_virtqueue *vqs;
>>   	struct completion completion;
>>   	struct vdpa_device *vdpa;
>> -	struct vhost_iotlb *iotlb;
>> +	struct hlist_head as[VHOST_VDPA_IOTLB_BUCKETS];
>>   	struct device dev;
>>   	struct cdev cdev;
>>   	atomic_t opened;
>> @@ -49,12 +57,64 @@ struct vhost_vdpa {
>>   	struct eventfd_ctx *config_ctx;
>>   	int in_batch;
>>   	struct vdpa_iova_range range;
>> +	int used_as;
> This is not really used. Not in this patch and later removed.


Right, will remove this.

Thanks


_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 11/21] vhost-vdpa: introduce asid based IOTLB
  2020-12-29 12:05   ` Eli Cohen
@ 2020-12-30  6:33       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-30  6:33 UTC (permalink / raw)
  To: Eli Cohen
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare


On 2020/12/29 下午8:05, Eli Cohen wrote:
>> +
>> +static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
> The return value is never interpreted. I think it should either be made
> void or return values checked.


Right, will make it void.


>
>> +{
>> +	struct vhost_vdpa_as *as = asid_to_as(v, asid);
>> +
>> +	/* Remove default address space is not allowed */
>> +	if (asid == 0)
>> +		return -EINVAL;
> Can you explain why? I think you have a memory leak due to this as no
> one will ever free as with id 0.
>

Looks like a bug. Will remove this.

Thanks



^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 11/21] vhost-vdpa: introduce asid based IOTLB
@ 2020-12-30  6:33       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-30  6:33 UTC (permalink / raw)
  To: Eli Cohen
  Cc: lulu, kvm, mst, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller


On 2020/12/29 下午8:05, Eli Cohen wrote:
>> +
>> +static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
> The return value is never interpreted. I think it should either be made
> void or return values checked.


Right, will make it void.


>
>> +{
>> +	struct vhost_vdpa_as *as = asid_to_as(v, asid);
>> +
>> +	/* Remove default address space is not allowed */
>> +	if (asid == 0)
>> +		return -EINVAL;
> Can you explain why? I think you have a memory leak due to this as no
> one will ever free as with id 0.
>

Looks like a bug. Will remove this.

Thanks


_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 11/21] vhost-vdpa: introduce asid based IOTLB
  2020-12-29 11:53   ` Eli Cohen
@ 2020-12-30  6:34       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-30  6:34 UTC (permalink / raw)
  To: Eli Cohen
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare


On 2020/12/29 下午7:53, Eli Cohen wrote:
>> +
>> +static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
>> +{
>> +	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
>> +	struct vhost_vdpa_as *as;
>> +
>> +	if (asid_to_as(v, asid))
>> +		return NULL;
>> +
>> +	as = kmalloc(sizeof(*as), GFP_KERNEL);
> kzalloc()? See comment below.
>
>> +	if (!as)
>> +		return NULL;
>> +
>> +	vhost_iotlb_init(&as->iotlb, 0, 0);
>> +	as->id = asid;
>> +	hlist_add_head(&as->hash_link, head);
>> +	++v->used_as;
> Although you eventually ended up removing used_as, this is a bug since
> you're incrementing a random value. Maybe it's better to be on the safe
> side and use kzalloc() for as above.


Yes and used_as needs to be removed.

Thanks



>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 11/21] vhost-vdpa: introduce asid based IOTLB
@ 2020-12-30  6:34       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-30  6:34 UTC (permalink / raw)
  To: Eli Cohen
  Cc: lulu, kvm, mst, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller


On 2020/12/29 下午7:53, Eli Cohen wrote:
>> +
>> +static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
>> +{
>> +	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
>> +	struct vhost_vdpa_as *as;
>> +
>> +	if (asid_to_as(v, asid))
>> +		return NULL;
>> +
>> +	as = kmalloc(sizeof(*as), GFP_KERNEL);
> kzalloc()? See comment below.
>
>> +	if (!as)
>> +		return NULL;
>> +
>> +	vhost_iotlb_init(&as->iotlb, 0, 0);
>> +	as->id = asid;
>> +	hlist_add_head(&as->hash_link, head);
>> +	++v->used_as;
> Although you eventually ended up removing used_as, this is a bug since
> you're incrementing a random value. Maybe it's better to be on the safe
> side and use kzalloc() for as above.


Yes and used_as needs to be removed.

Thanks



>

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 12/21] vhost-vdpa: introduce uAPI to get the number of virtqueue groups
  2020-12-29 12:24   ` Eli Cohen
@ 2020-12-30  6:49       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-30  6:49 UTC (permalink / raw)
  To: Eli Cohen
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare


On 2020/12/29 下午8:24, Eli Cohen wrote:
> On Wed, Dec 16, 2020 at 02:48:09PM +0800, Jason Wang wrote:
>> Follows the vDPA support for multiple address spaces, this patch
>> introduce uAPI for the userspace to know the number of virtqueue
>> groups supported by the vDPA device.
> Can you explain what exactly you mean be userspace?


It's the userspace that uses the uAPI introduced in this patch.


> Is it just qemu or
> is it destined to the virtio_net driver run by the qemu process?


It could be Qemu, DPDK or other userspace program.

The guest virtio-net driver will not use this but talks to the virtio 
device emulated by Qemu.


> Also can you say for what purpose?


This can be used for facilitate the checking of whether the control vq 
could be supported.

E.g if the device support less than 2 groups, qemu won't advertise 
control vq.

Yes, #groups could be inferred from GET_VRING_GROUP. But it's not 
straightforward as this.

Thanks


>
>> Signed-off-by: Jason Wang <jasowang@redhat.com>
>> ---
>>   drivers/vhost/vdpa.c       | 4 ++++
>>   include/uapi/linux/vhost.h | 3 +++
>>   2 files changed, 7 insertions(+)
>>
>> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
>> index 060d5b5b7e64..1ba5901b28e7 100644
>> --- a/drivers/vhost/vdpa.c
>> +++ b/drivers/vhost/vdpa.c
>> @@ -536,6 +536,10 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
>>   	case VHOST_VDPA_GET_VRING_NUM:
>>   		r = vhost_vdpa_get_vring_num(v, argp);
>>   		break;
>> +	case VHOST_VDPA_GET_GROUP_NUM:
>> +		r = copy_to_user(argp, &v->vdpa->ngroups,
>> +				 sizeof(v->vdpa->ngroups));
>> +		break;
>>   	case VHOST_SET_LOG_BASE:
>>   	case VHOST_SET_LOG_FD:
>>   		r = -ENOIOCTLCMD;
>> diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
>> index 59c6c0fbaba1..8a4e6e426bbf 100644
>> --- a/include/uapi/linux/vhost.h
>> +++ b/include/uapi/linux/vhost.h
>> @@ -145,4 +145,7 @@
>>   /* Get the valid iova range */
>>   #define VHOST_VDPA_GET_IOVA_RANGE	_IOR(VHOST_VIRTIO, 0x78, \
>>   					     struct vhost_vdpa_iova_range)
>> +/* Get the number of virtqueue groups. */
>> +#define VHOST_VDPA_GET_GROUP_NUM	_IOR(VHOST_VIRTIO, 0x79, unsigned int)
>> +
>>   #endif
>> -- 
>> 2.25.1
>>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 12/21] vhost-vdpa: introduce uAPI to get the number of virtqueue groups
@ 2020-12-30  6:49       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-30  6:49 UTC (permalink / raw)
  To: Eli Cohen
  Cc: lulu, kvm, mst, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller


On 2020/12/29 下午8:24, Eli Cohen wrote:
> On Wed, Dec 16, 2020 at 02:48:09PM +0800, Jason Wang wrote:
>> Follows the vDPA support for multiple address spaces, this patch
>> introduce uAPI for the userspace to know the number of virtqueue
>> groups supported by the vDPA device.
> Can you explain what exactly you mean be userspace?


It's the userspace that uses the uAPI introduced in this patch.


> Is it just qemu or
> is it destined to the virtio_net driver run by the qemu process?


It could be Qemu, DPDK or other userspace program.

The guest virtio-net driver will not use this but talks to the virtio 
device emulated by Qemu.


> Also can you say for what purpose?


This can be used for facilitate the checking of whether the control vq 
could be supported.

E.g if the device support less than 2 groups, qemu won't advertise 
control vq.

Yes, #groups could be inferred from GET_VRING_GROUP. But it's not 
straightforward as this.

Thanks


>
>> Signed-off-by: Jason Wang <jasowang@redhat.com>
>> ---
>>   drivers/vhost/vdpa.c       | 4 ++++
>>   include/uapi/linux/vhost.h | 3 +++
>>   2 files changed, 7 insertions(+)
>>
>> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
>> index 060d5b5b7e64..1ba5901b28e7 100644
>> --- a/drivers/vhost/vdpa.c
>> +++ b/drivers/vhost/vdpa.c
>> @@ -536,6 +536,10 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
>>   	case VHOST_VDPA_GET_VRING_NUM:
>>   		r = vhost_vdpa_get_vring_num(v, argp);
>>   		break;
>> +	case VHOST_VDPA_GET_GROUP_NUM:
>> +		r = copy_to_user(argp, &v->vdpa->ngroups,
>> +				 sizeof(v->vdpa->ngroups));
>> +		break;
>>   	case VHOST_SET_LOG_BASE:
>>   	case VHOST_SET_LOG_FD:
>>   		r = -ENOIOCTLCMD;
>> diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
>> index 59c6c0fbaba1..8a4e6e426bbf 100644
>> --- a/include/uapi/linux/vhost.h
>> +++ b/include/uapi/linux/vhost.h
>> @@ -145,4 +145,7 @@
>>   /* Get the valid iova range */
>>   #define VHOST_VDPA_GET_IOVA_RANGE	_IOR(VHOST_VIRTIO, 0x78, \
>>   					     struct vhost_vdpa_iova_range)
>> +/* Get the number of virtqueue groups. */
>> +#define VHOST_VDPA_GET_GROUP_NUM	_IOR(VHOST_VIRTIO, 0x79, unsigned int)
>> +
>>   #endif
>> -- 
>> 2.25.1
>>

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 07/21] vdpa: multiple address spaces support
  2020-12-30  4:04       ` Jason Wang
  (?)
@ 2020-12-30  9:44       ` Eli Cohen
  -1 siblings, 0 replies; 136+ messages in thread
From: Eli Cohen @ 2020-12-30  9:44 UTC (permalink / raw)
  To: Jason Wang
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare

On Wed, Dec 30, 2020 at 12:04:30PM +0800, Jason Wang wrote:
> 
> On 2020/12/29 下午3:28, Eli Cohen wrote:
> > > @@ -43,6 +43,8 @@ struct vdpa_vq_state {
> > >    * @index: device index
> > >    * @features_valid: were features initialized? for legacy guests
> > >    * @nvqs: the number of virtqueues
> > > + * @ngroups: the number of virtqueue groups
> > > + * @nas: the number of address spaces
> > I am not sure these can be categorised as part of the state of the VQ.
> > It's more of a property so maybe we can have a callback to get the
> > properties of the VQ?
> 
> 
> Or maybe there's a misunderstanding of the patch.
> 

Yes, I misinterpreted the hunk. No issue here.

> Those two attributes belongs to vdpa_device instead of vdpa_vq_state
> actually.
> 
> Thanks
> 

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 12/21] vhost-vdpa: introduce uAPI to get the number of virtqueue groups
  2020-12-16  6:48   ` Jason Wang
  (?)
  (?)
@ 2020-12-30 10:05   ` Eli Cohen
  2020-12-31  2:36       ` Jason Wang
  -1 siblings, 1 reply; 136+ messages in thread
From: Eli Cohen @ 2020-12-30 10:05 UTC (permalink / raw)
  To: Jason Wang
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare

On Wed, Dec 16, 2020 at 02:48:09PM +0800, Jason Wang wrote:
> Follows the vDPA support for multiple address spaces, this patch
> introduce uAPI for the userspace to know the number of virtqueue
> groups supported by the vDPA device.
> 
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
>  drivers/vhost/vdpa.c       | 4 ++++
>  include/uapi/linux/vhost.h | 3 +++
>  2 files changed, 7 insertions(+)
> 
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> index 060d5b5b7e64..1ba5901b28e7 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -536,6 +536,10 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
>  	case VHOST_VDPA_GET_VRING_NUM:
>  		r = vhost_vdpa_get_vring_num(v, argp);
>  		break;
> +	case VHOST_VDPA_GET_GROUP_NUM:
> +		r = copy_to_user(argp, &v->vdpa->ngroups,
> +				 sizeof(v->vdpa->ngroups));
> +		break;

Is this and other ioctls already supported in qemu?

>  	case VHOST_SET_LOG_BASE:
>  	case VHOST_SET_LOG_FD:
>  		r = -ENOIOCTLCMD;
> diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
> index 59c6c0fbaba1..8a4e6e426bbf 100644
> --- a/include/uapi/linux/vhost.h
> +++ b/include/uapi/linux/vhost.h
> @@ -145,4 +145,7 @@
>  /* Get the valid iova range */
>  #define VHOST_VDPA_GET_IOVA_RANGE	_IOR(VHOST_VIRTIO, 0x78, \
>  					     struct vhost_vdpa_iova_range)
> +/* Get the number of virtqueue groups. */
> +#define VHOST_VDPA_GET_GROUP_NUM	_IOR(VHOST_VIRTIO, 0x79, unsigned int)
> +
>  #endif
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 12/21] vhost-vdpa: introduce uAPI to get the number of virtqueue groups
  2020-12-30 10:05   ` Eli Cohen
@ 2020-12-31  2:36       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-31  2:36 UTC (permalink / raw)
  To: Eli Cohen
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare


On 2020/12/30 下午6:05, Eli Cohen wrote:
> On Wed, Dec 16, 2020 at 02:48:09PM +0800, Jason Wang wrote:
>> Follows the vDPA support for multiple address spaces, this patch
>> introduce uAPI for the userspace to know the number of virtqueue
>> groups supported by the vDPA device.
>>
>> Signed-off-by: Jason Wang<jasowang@redhat.com>
>> ---
>>   drivers/vhost/vdpa.c       | 4 ++++
>>   include/uapi/linux/vhost.h | 3 +++
>>   2 files changed, 7 insertions(+)
>>
>> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
>> index 060d5b5b7e64..1ba5901b28e7 100644
>> --- a/drivers/vhost/vdpa.c
>> +++ b/drivers/vhost/vdpa.c
>> @@ -536,6 +536,10 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
>>   	case VHOST_VDPA_GET_VRING_NUM:
>>   		r = vhost_vdpa_get_vring_num(v, argp);
>>   		break;
>> +	case VHOST_VDPA_GET_GROUP_NUM:
>> +		r = copy_to_user(argp, &v->vdpa->ngroups,
>> +				 sizeof(v->vdpa->ngroups));
>> +		break;
> Is this and other ioctls already supported in qemu?


Not yet, the prototype is under development.

I test the series with a small and dedicated userspace program.

Thanks


>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 12/21] vhost-vdpa: introduce uAPI to get the number of virtqueue groups
@ 2020-12-31  2:36       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2020-12-31  2:36 UTC (permalink / raw)
  To: Eli Cohen
  Cc: lulu, kvm, mst, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller


On 2020/12/30 下午6:05, Eli Cohen wrote:
> On Wed, Dec 16, 2020 at 02:48:09PM +0800, Jason Wang wrote:
>> Follows the vDPA support for multiple address spaces, this patch
>> introduce uAPI for the userspace to know the number of virtqueue
>> groups supported by the vDPA device.
>>
>> Signed-off-by: Jason Wang<jasowang@redhat.com>
>> ---
>>   drivers/vhost/vdpa.c       | 4 ++++
>>   include/uapi/linux/vhost.h | 3 +++
>>   2 files changed, 7 insertions(+)
>>
>> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
>> index 060d5b5b7e64..1ba5901b28e7 100644
>> --- a/drivers/vhost/vdpa.c
>> +++ b/drivers/vhost/vdpa.c
>> @@ -536,6 +536,10 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
>>   	case VHOST_VDPA_GET_VRING_NUM:
>>   		r = vhost_vdpa_get_vring_num(v, argp);
>>   		break;
>> +	case VHOST_VDPA_GET_GROUP_NUM:
>> +		r = copy_to_user(argp, &v->vdpa->ngroups,
>> +				 sizeof(v->vdpa->ngroups));
>> +		break;
> Is this and other ioctls already supported in qemu?


Not yet, the prototype is under development.

I test the series with a small and dedicated userspace program.

Thanks


>

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 06/21] vdpa: introduce virtqueue groups
  2020-12-16  6:48   ` Jason Wang
@ 2021-01-04 10:04     ` Stefan Hajnoczi
  -1 siblings, 0 replies; 136+ messages in thread
From: Stefan Hajnoczi @ 2021-01-04 10:04 UTC (permalink / raw)
  To: Jason Wang
  Cc: mst, kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller

[-- Attachment #1: Type: text/plain, Size: 984 bytes --]

On Wed, Dec 16, 2020 at 02:48:03PM +0800, Jason Wang wrote:
> This patch introduces virtqueue groups to vDPA device. The virtqueue
> group is the minimal set of virtqueues that must share an address
> space. And the adddress space identifier could only be attached to
> a specific virtqueue group.
> 
> A new mandated bus operation is introduced to get the virtqueue group
> ID for a specific virtqueue.
> 
> All the vDPA device drivers were converted to simply support a single
> virtqueue group.
> 
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
>  drivers/vdpa/ifcvf/ifcvf_main.c   |  9 ++++++++-
>  drivers/vdpa/mlx5/net/mlx5_vnet.c |  8 +++++++-
>  drivers/vdpa/vdpa.c               |  4 +++-
>  drivers/vdpa/vdpa_sim/vdpa_sim.c  | 11 ++++++++++-
>  include/linux/vdpa.h              | 12 +++++++++---
>  5 files changed, 37 insertions(+), 7 deletions(-)

Maybe consider calling it iotlb_group or iommu_group so the purpose of
the group is clear?

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 06/21] vdpa: introduce virtqueue groups
@ 2021-01-04 10:04     ` Stefan Hajnoczi
  0 siblings, 0 replies; 136+ messages in thread
From: Stefan Hajnoczi @ 2021-01-04 10:04 UTC (permalink / raw)
  To: Jason Wang
  Cc: kvm, lulu, mst, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller


[-- Attachment #1.1: Type: text/plain, Size: 984 bytes --]

On Wed, Dec 16, 2020 at 02:48:03PM +0800, Jason Wang wrote:
> This patch introduces virtqueue groups to vDPA device. The virtqueue
> group is the minimal set of virtqueues that must share an address
> space. And the adddress space identifier could only be attached to
> a specific virtqueue group.
> 
> A new mandated bus operation is introduced to get the virtqueue group
> ID for a specific virtqueue.
> 
> All the vDPA device drivers were converted to simply support a single
> virtqueue group.
> 
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
>  drivers/vdpa/ifcvf/ifcvf_main.c   |  9 ++++++++-
>  drivers/vdpa/mlx5/net/mlx5_vnet.c |  8 +++++++-
>  drivers/vdpa/vdpa.c               |  4 +++-
>  drivers/vdpa/vdpa_sim/vdpa_sim.c  | 11 ++++++++++-
>  include/linux/vdpa.h              | 12 +++++++++---
>  5 files changed, 37 insertions(+), 7 deletions(-)

Maybe consider calling it iotlb_group or iommu_group so the purpose of
the group is clear?

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 183 bytes --]

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 06/21] vdpa: introduce virtqueue groups
  2021-01-04 10:04     ` Stefan Hajnoczi
@ 2021-01-05  4:13       ` Jason Wang
  -1 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2021-01-05  4:13 UTC (permalink / raw)
  To: Stefan Hajnoczi
  Cc: mst, kvm, lulu, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller


On 2021/1/4 下午6:04, Stefan Hajnoczi wrote:
> On Wed, Dec 16, 2020 at 02:48:03PM +0800, Jason Wang wrote:
>> This patch introduces virtqueue groups to vDPA device. The virtqueue
>> group is the minimal set of virtqueues that must share an address
>> space. And the adddress space identifier could only be attached to
>> a specific virtqueue group.
>>
>> A new mandated bus operation is introduced to get the virtqueue group
>> ID for a specific virtqueue.
>>
>> All the vDPA device drivers were converted to simply support a single
>> virtqueue group.
>>
>> Signed-off-by: Jason Wang <jasowang@redhat.com>
>> ---
>>   drivers/vdpa/ifcvf/ifcvf_main.c   |  9 ++++++++-
>>   drivers/vdpa/mlx5/net/mlx5_vnet.c |  8 +++++++-
>>   drivers/vdpa/vdpa.c               |  4 +++-
>>   drivers/vdpa/vdpa_sim/vdpa_sim.c  | 11 ++++++++++-
>>   include/linux/vdpa.h              | 12 +++++++++---
>>   5 files changed, 37 insertions(+), 7 deletions(-)
> Maybe consider calling it iotlb_group or iommu_group so the purpose of
> the group is clear?


I'm not sure. The reason that I choose virtqueue group is because:

1) Virtqueue is the only entity that tries to issues DMA
2) For IOMMU group, it may cause confusion to the existing IOMMU group 
who group devices
3) IOTLB is the concept in vhost, we don't have such definition in the 
virtio spec

Thanks



^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 06/21] vdpa: introduce virtqueue groups
@ 2021-01-05  4:13       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2021-01-05  4:13 UTC (permalink / raw)
  To: Stefan Hajnoczi
  Cc: kvm, lulu, mst, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller


On 2021/1/4 下午6:04, Stefan Hajnoczi wrote:
> On Wed, Dec 16, 2020 at 02:48:03PM +0800, Jason Wang wrote:
>> This patch introduces virtqueue groups to vDPA device. The virtqueue
>> group is the minimal set of virtqueues that must share an address
>> space. And the adddress space identifier could only be attached to
>> a specific virtqueue group.
>>
>> A new mandated bus operation is introduced to get the virtqueue group
>> ID for a specific virtqueue.
>>
>> All the vDPA device drivers were converted to simply support a single
>> virtqueue group.
>>
>> Signed-off-by: Jason Wang <jasowang@redhat.com>
>> ---
>>   drivers/vdpa/ifcvf/ifcvf_main.c   |  9 ++++++++-
>>   drivers/vdpa/mlx5/net/mlx5_vnet.c |  8 +++++++-
>>   drivers/vdpa/vdpa.c               |  4 +++-
>>   drivers/vdpa/vdpa_sim/vdpa_sim.c  | 11 ++++++++++-
>>   include/linux/vdpa.h              | 12 +++++++++---
>>   5 files changed, 37 insertions(+), 7 deletions(-)
> Maybe consider calling it iotlb_group or iommu_group so the purpose of
> the group is clear?


I'm not sure. The reason that I choose virtqueue group is because:

1) Virtqueue is the only entity that tries to issues DMA
2) For IOMMU group, it may cause confusion to the existing IOMMU group 
who group devices
3) IOTLB is the concept in vhost, we don't have such definition in the 
virtio spec

Thanks


_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 21/21] vdpasim: control virtqueue support
  2020-12-16  6:48   ` Jason Wang
  (?)
  (?)
@ 2021-01-11 12:26   ` Eli Cohen
  2021-01-12  3:11       ` Jason Wang
  -1 siblings, 1 reply; 136+ messages in thread
From: Eli Cohen @ 2021-01-11 12:26 UTC (permalink / raw)
  To: Jason Wang
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare

On Wed, Dec 16, 2020 at 02:48:18PM +0800, Jason Wang wrote:
> This patch introduces the control virtqueue support for vDPA
> simulator. This is a requirement for supporting advanced features like
> multiqueue.
> 
> A requirement for control virtqueue is to isolate its memory access
> from the rx/tx virtqueues. This is because when using vDPA device
> for VM, the control virqueue is not directly assigned to VM. Userspace
> (Qemu) will present a shadow control virtqueue to control for
> recording the device states.
> 
> The isolation is done via the virtqueue groups and ASID support in
> vDPA through vhost-vdpa. The simulator is extended to have:
> 
> 1) three virtqueues: RXVQ, TXVQ and CVQ (control virtqueue)
> 2) two virtqueue groups: group 0 contains RXVQ and TXVQ; group 1
>    contains CVQ
> 3) two address spaces and the simulator simply implements the address
>    spaces by mapping it 1:1 to IOTLB.
> 
> For the VM use cases, userspace(Qemu) may set AS 0 to group 0 and AS 1
> to group 1. So we have:
> 
> 1) The IOTLB for virtqueue group 0 contains the mappings of guest, so
>    RX and TX can be assigned to guest directly.
> 2) The IOTLB for virtqueue group 1 contains the mappings of CVQ which
>    is the buffers that allocated and managed by VMM only. So CVQ of
>    vhost-vdpa is visible to VMM only. And Guest can not access the CVQ
>    of vhost-vdpa.
> 
> For the other use cases, since AS 0 is associated to all virtqueue
> groups by default. All virtqueues share the same mapping by default.
> 
> To demonstrate the function, VIRITO_NET_F_CTRL_MACADDR is
> implemented in the simulator for the driver to set mac address.
> 

Hi Jason,

is there any version of qemu/libvirt available that I can see the
control virtqueue working in action?

> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
>  drivers/vdpa/vdpa_sim/vdpa_sim.c | 189 +++++++++++++++++++++++++++----
>  1 file changed, 166 insertions(+), 23 deletions(-)
> 
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> index fe90a783bde4..0fd06ac491cd 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> @@ -60,14 +60,18 @@ struct vdpasim_virtqueue {
>  #define VDPASIM_QUEUE_MAX 256
>  #define VDPASIM_DEVICE_ID 0x1
>  #define VDPASIM_VENDOR_ID 0
> -#define VDPASIM_VQ_NUM 0x2
> +#define VDPASIM_VQ_NUM 0x3
> +#define VDPASIM_AS_NUM 0x2
> +#define VDPASIM_GROUP_NUM 0x2
>  #define VDPASIM_NAME "vdpasim-netdev"
>  
>  static u64 vdpasim_features = (1ULL << VIRTIO_F_ANY_LAYOUT) |
>  			      (1ULL << VIRTIO_F_VERSION_1)  |
>  			      (1ULL << VIRTIO_F_ACCESS_PLATFORM) |
> +			      (1ULL << VIRTIO_NET_F_MTU) |
>  			      (1ULL << VIRTIO_NET_F_MAC) |
> -			      (1ULL << VIRTIO_NET_F_MTU);
> +			      (1ULL << VIRTIO_NET_F_CTRL_VQ) |
> +			      (1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR);
>  
>  /* State of each vdpasim device */
>  struct vdpasim {
> @@ -147,11 +151,17 @@ static void vdpasim_reset(struct vdpasim *vdpasim)
>  {
>  	int i;
>  
> -	for (i = 0; i < VDPASIM_VQ_NUM; i++)
> +	spin_lock(&vdpasim->iommu_lock);
> +
> +	for (i = 0; i < VDPASIM_VQ_NUM; i++) {
>  		vdpasim_vq_reset(&vdpasim->vqs[i]);
> +		vringh_set_iotlb(&vdpasim->vqs[i].vring,
> +				 &vdpasim->iommu[0]);
> +	}
>  
> -	spin_lock(&vdpasim->iommu_lock);
> -	vhost_iotlb_reset(vdpasim->iommu);
> +	for (i = 0; i < VDPASIM_AS_NUM; i++) {
> +		vhost_iotlb_reset(&vdpasim->iommu[i]);
> +	}
>  	spin_unlock(&vdpasim->iommu_lock);
>  
>  	vdpasim->features = 0;
> @@ -191,6 +201,81 @@ static bool receive_filter(struct vdpasim *vdpasim, size_t len)
>  	return false;
>  }
>  
> +virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
> +					    u8 cmd)
> +{
> +	struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
> +	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
> +	size_t read;
> +
> +	switch (cmd) {
> +	case VIRTIO_NET_CTRL_MAC_ADDR_SET:
> +		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov,
> +					     (void *)vdpasim->config.mac,
> +					     ETH_ALEN);
> +		if (read == ETH_ALEN)
> +			status = VIRTIO_NET_OK;
> +		break;
> +	default:
> +		break;
> +	}
> +
> +	return status;
> +}
> +
> +static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
> +{
> +	struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
> +	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
> +	struct virtio_net_ctrl_hdr ctrl;
> +	size_t read, write;
> +	int err;
> +
> +	if (!(vdpasim->features & (1ULL << VIRTIO_NET_F_CTRL_VQ)))
> +		return;
> +
> +	if (!cvq->ready)
> +		return;
> +
> +	while (true) {
> +		err = vringh_getdesc_iotlb(&cvq->vring, &cvq->in_iov,
> +					   &cvq->out_iov,
> +					   &cvq->head, GFP_ATOMIC);
> +		if (err <= 0)
> +			break;
> +
> +		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov, &ctrl,
> +					     sizeof(ctrl));
> +		if (read != sizeof(ctrl))
> +			break;
> +
> +		switch (ctrl.class) {
> +		case VIRTIO_NET_CTRL_MAC:
> +			status = vdpasim_handle_ctrl_mac(vdpasim, ctrl.cmd);
> +			break;
> +		default:
> +			break;
> +		}
> +
> +		/* Make sure data is wrote before advancing index */
> +		smp_wmb();
> +
> +		write = vringh_iov_push_iotlb(&cvq->vring, &cvq->out_iov,
> +					      &status, sizeof (status));
> +		vringh_complete_iotlb(&cvq->vring, cvq->head, write);
> +		vringh_kiov_cleanup(&cvq->in_iov);
> +		vringh_kiov_cleanup(&cvq->out_iov);
> +
> +		/* Make sure used is visible before rasing the interrupt. */
> +		smp_wmb();
> +
> +		local_bh_disable();
> +		if (cvq->cb)
> +			cvq->cb(cvq->private);
> +		local_bh_enable();
> +	}
> +}
> +
>  static void vdpasim_work(struct work_struct *work)
>  {
>  	struct vdpasim *vdpasim = container_of(work, struct
> @@ -276,7 +361,7 @@ static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
>  				   unsigned long attrs)
>  {
>  	struct vdpasim *vdpasim = dev_to_sim(dev);
> -	struct vhost_iotlb *iommu = vdpasim->iommu;
> +	struct vhost_iotlb *iommu = &vdpasim->iommu[0];
>  	u64 pa = (page_to_pfn(page) << PAGE_SHIFT) + offset;
>  	int ret, perm = dir_to_perm(dir);
>  
> @@ -301,7 +386,7 @@ static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
>  			       unsigned long attrs)
>  {
>  	struct vdpasim *vdpasim = dev_to_sim(dev);
> -	struct vhost_iotlb *iommu = vdpasim->iommu;
> +	struct vhost_iotlb *iommu = &vdpasim->iommu[0];
>  
>  	spin_lock(&vdpasim->iommu_lock);
>  	vhost_iotlb_del_range(iommu, (u64)dma_addr,
> @@ -314,7 +399,7 @@ static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
>  				    unsigned long attrs)
>  {
>  	struct vdpasim *vdpasim = dev_to_sim(dev);
> -	struct vhost_iotlb *iommu = vdpasim->iommu;
> +	struct vhost_iotlb *iommu = &vdpasim->iommu[0];
>  	void *addr = kmalloc(size, flag);
>  	int ret;
>  
> @@ -344,7 +429,7 @@ static void vdpasim_free_coherent(struct device *dev, size_t size,
>  				  unsigned long attrs)
>  {
>  	struct vdpasim *vdpasim = dev_to_sim(dev);
> -	struct vhost_iotlb *iommu = vdpasim->iommu;
> +	struct vhost_iotlb *iommu = &vdpasim->iommu[0];
>  
>  	spin_lock(&vdpasim->iommu_lock);
>  	vhost_iotlb_del_range(iommu, (u64)dma_addr,
> @@ -370,14 +455,17 @@ static struct vdpasim *vdpasim_create(void)
>  	struct vdpasim *vdpasim;
>  	struct device *dev;
>  	int ret = -ENOMEM;
> +	int i;
>  
>  	if (batch_mapping)
>  		ops = &vdpasim_net_batch_config_ops;
>  	else
>  		ops = &vdpasim_net_config_ops;
>  
> +	/* 3 virtqueues, 2 address spaces, 2 virtqueue groups */
>  	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
> -				    VDPASIM_VQ_NUM, 1, 1);
> +				    VDPASIM_VQ_NUM, VDPASIM_AS_NUM,
> +				    VDPASIM_GROUP_NUM);
>  	if (!vdpasim)
>  		goto err_alloc;
>  
> @@ -391,10 +479,14 @@ static struct vdpasim *vdpasim_create(void)
>  		goto err_iommu;
>  	set_dma_ops(dev, &vdpasim_dma_ops);
>  
> -	vdpasim->iommu = vhost_iotlb_alloc(2048, 0);
> +	vdpasim->iommu = kmalloc_array(VDPASIM_AS_NUM,
> +				       sizeof(*vdpasim->iommu), GFP_KERNEL);
>  	if (!vdpasim->iommu)
>  		goto err_iommu;
>  
> +	for (i = 0; i < VDPASIM_AS_NUM; i++)
> +		vhost_iotlb_init(&vdpasim->iommu[i], 0, 0);
> +
>  	vdpasim->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
>  	if (!vdpasim->buffer)
>  		goto err_iommu;
> @@ -409,8 +501,9 @@ static struct vdpasim *vdpasim_create(void)
>  		eth_random_addr(vdpasim->config.mac);
>  	}
>  
> -	vringh_set_iotlb(&vdpasim->vqs[0].vring, vdpasim->iommu);
> -	vringh_set_iotlb(&vdpasim->vqs[1].vring, vdpasim->iommu);
> +	/* Make sure that default ASID is zero */
> +	for (i = 0; i < VDPASIM_VQ_NUM; i++)
> +		vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0]);
>  
>  	vdpasim->vdpa.dma_dev = dev;
>  	ret = vdpa_register_device(&vdpasim->vdpa);
> @@ -452,7 +545,14 @@ static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx)
>  	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>  	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
>  
> -	if (vq->ready)
> +	if (idx == 2) {
> +		/* Kernel virtio driver will do busy waiting for the
> +		 * result, so we can't handle cvq in the workqueue.
> +		 */
> +		spin_lock(&vdpasim->lock);
> +		vdpasim_handle_cvq(vdpasim);
> +		spin_unlock(&vdpasim->lock);
> +	} else if (vq->ready)
>  		schedule_work(&vdpasim->work);
>  }
>  
> @@ -518,7 +618,11 @@ static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
>  
>  static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
>  {
> -	return 0;
> +	/* RX and TX belongs to group 0, CVQ belongs to group 1 */
> +	if (idx == 2)
> +		return 1;
> +	else
> +		return 0;
>  }
>  
>  static u64 vdpasim_get_features(struct vdpa_device *vdpa)
> @@ -624,20 +728,52 @@ static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
>  	return range;
>  }
>  
> +int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
> +			   unsigned int asid)
> +{
> +	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
> +	struct vhost_iotlb *iommu;
> +	int i;
> +
> +	if (group > VDPASIM_GROUP_NUM)
> +		return -EINVAL;
> +
> +	if (asid > VDPASIM_AS_NUM)
> +		return -EINVAL;
> +
> +	iommu = &vdpasim->iommu[asid];
> +
> +	spin_lock(&vdpasim->lock);
> +
> +	for (i = 0; i < VDPASIM_VQ_NUM; i++)
> +		if (vdpasim_get_vq_group(vdpa, i) == group)
> +			vringh_set_iotlb(&vdpasim->vqs[i].vring, iommu);
> +
> +	spin_unlock(&vdpasim->lock);
> +
> +	return 0;
> +}
> +
>  static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
>  			   struct vhost_iotlb *iotlb)
>  {
>  	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>  	struct vhost_iotlb_map *map;
> +	struct vhost_iotlb *iommu;
>  	u64 start = 0ULL, last = 0ULL - 1;
>  	int ret;
>  
> +	if (asid >= VDPASIM_AS_NUM)
> +		return -EINVAL;
> +
>  	spin_lock(&vdpasim->iommu_lock);
> -	vhost_iotlb_reset(vdpasim->iommu);
> +
> +	iommu = &vdpasim->iommu[asid];
> +	vhost_iotlb_reset(iommu);
>  
>  	for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
>  	     map = vhost_iotlb_itree_next(map, start, last)) {
> -		ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
> +		ret = vhost_iotlb_add_range(iommu, map->start,
>  					    map->last, map->addr, map->perm);
>  		if (ret)
>  			goto err;
> @@ -646,7 +782,7 @@ static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
>  	return 0;
>  
>  err:
> -	vhost_iotlb_reset(vdpasim->iommu);
> +	vhost_iotlb_reset(iommu);
>  	spin_unlock(&vdpasim->iommu_lock);
>  	return ret;
>  }
> @@ -658,9 +794,12 @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
>  	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>  	int ret;
>  
> +	if (asid >= VDPASIM_AS_NUM)
> +		return -EINVAL;
> +
>  	spin_lock(&vdpasim->iommu_lock);
> -	ret = vhost_iotlb_add_range(vdpasim->iommu, iova, iova + size - 1, pa,
> -				    perm);
> +	ret = vhost_iotlb_add_range(&vdpasim->iommu[asid], iova,
> +				    iova + size - 1, pa, perm);
>  	spin_unlock(&vdpasim->iommu_lock);
>  
>  	return ret;
> @@ -671,8 +810,11 @@ static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
>  {
>  	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>  
> +	if (asid >= VDPASIM_AS_NUM)
> +		return -EINVAL;
> +
>  	spin_lock(&vdpasim->iommu_lock);
> -	vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
> +	vhost_iotlb_del_range(&vdpasim->iommu[asid], iova, iova + size - 1);
>  	spin_unlock(&vdpasim->iommu_lock);
>  
>  	return 0;
> @@ -684,8 +826,7 @@ static void vdpasim_free(struct vdpa_device *vdpa)
>  
>  	cancel_work_sync(&vdpasim->work);
>  	kfree(vdpasim->buffer);
> -	if (vdpasim->iommu)
> -		vhost_iotlb_free(vdpasim->iommu);
> +	vhost_iotlb_free(vdpasim->iommu);
>  }
>  
>  static const struct vdpa_config_ops vdpasim_net_config_ops = {
> @@ -711,6 +852,7 @@ static const struct vdpa_config_ops vdpasim_net_config_ops = {
>  	.set_config             = vdpasim_set_config,
>  	.get_generation         = vdpasim_get_generation,
>  	.get_iova_range         = vdpasim_get_iova_range,
> +	.set_group_asid         = vdpasim_set_group_asid,
>  	.dma_map                = vdpasim_dma_map,
>  	.dma_unmap              = vdpasim_dma_unmap,
>  	.free                   = vdpasim_free,
> @@ -739,6 +881,7 @@ static const struct vdpa_config_ops vdpasim_net_batch_config_ops = {
>  	.set_config             = vdpasim_set_config,
>  	.get_generation         = vdpasim_get_generation,
>  	.get_iova_range         = vdpasim_get_iova_range,
> +	.set_group_asid         = vdpasim_set_group_asid,
>  	.set_map                = vdpasim_set_map,
>  	.free                   = vdpasim_free,
>  };
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 21/21] vdpasim: control virtqueue support
  2021-01-11 12:26   ` Eli Cohen
@ 2021-01-12  3:11       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2021-01-12  3:11 UTC (permalink / raw)
  To: Eli Cohen
  Cc: mst, eperezma, kvm, virtualization, netdev, linux-kernel, lulu,
	eli, lingshan.zhu, rob.miller, stefanha, sgarzare


On 2021/1/11 下午8:26, Eli Cohen wrote:
> On Wed, Dec 16, 2020 at 02:48:18PM +0800, Jason Wang wrote:
>> This patch introduces the control virtqueue support for vDPA
>> simulator. This is a requirement for supporting advanced features like
>> multiqueue.
>>
>> A requirement for control virtqueue is to isolate its memory access
>> from the rx/tx virtqueues. This is because when using vDPA device
>> for VM, the control virqueue is not directly assigned to VM. Userspace
>> (Qemu) will present a shadow control virtqueue to control for
>> recording the device states.
>>
>> The isolation is done via the virtqueue groups and ASID support in
>> vDPA through vhost-vdpa. The simulator is extended to have:
>>
>> 1) three virtqueues: RXVQ, TXVQ and CVQ (control virtqueue)
>> 2) two virtqueue groups: group 0 contains RXVQ and TXVQ; group 1
>>     contains CVQ
>> 3) two address spaces and the simulator simply implements the address
>>     spaces by mapping it 1:1 to IOTLB.
>>
>> For the VM use cases, userspace(Qemu) may set AS 0 to group 0 and AS 1
>> to group 1. So we have:
>>
>> 1) The IOTLB for virtqueue group 0 contains the mappings of guest, so
>>     RX and TX can be assigned to guest directly.
>> 2) The IOTLB for virtqueue group 1 contains the mappings of CVQ which
>>     is the buffers that allocated and managed by VMM only. So CVQ of
>>     vhost-vdpa is visible to VMM only. And Guest can not access the CVQ
>>     of vhost-vdpa.
>>
>> For the other use cases, since AS 0 is associated to all virtqueue
>> groups by default. All virtqueues share the same mapping by default.
>>
>> To demonstrate the function, VIRITO_NET_F_CTRL_MACADDR is
>> implemented in the simulator for the driver to set mac address.
>>
> Hi Jason,
>
> is there any version of qemu/libvirt available that I can see the
> control virtqueue working in action?


Not yet, the qemu part depends on the shadow virtqueue work of Eugenio. 
But it will work as:

1) qemu will use a separated address space for the control virtqueue 
(shadow) exposed through vhost-vDPA
2) the commands sent through control virtqueue by guest driver will 
intercept by qemu
3) Qemu will send those commands to the shadow control virtqueue

Eugenio, any ETA for the new version of shadow virtqueue support in Qemu?

Thanks


>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 21/21] vdpasim: control virtqueue support
@ 2021-01-12  3:11       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2021-01-12  3:11 UTC (permalink / raw)
  To: Eli Cohen
  Cc: lulu, kvm, mst, netdev, linux-kernel, virtualization, eperezma,
	stefanha, eli, lingshan.zhu, rob.miller


On 2021/1/11 下午8:26, Eli Cohen wrote:
> On Wed, Dec 16, 2020 at 02:48:18PM +0800, Jason Wang wrote:
>> This patch introduces the control virtqueue support for vDPA
>> simulator. This is a requirement for supporting advanced features like
>> multiqueue.
>>
>> A requirement for control virtqueue is to isolate its memory access
>> from the rx/tx virtqueues. This is because when using vDPA device
>> for VM, the control virqueue is not directly assigned to VM. Userspace
>> (Qemu) will present a shadow control virtqueue to control for
>> recording the device states.
>>
>> The isolation is done via the virtqueue groups and ASID support in
>> vDPA through vhost-vdpa. The simulator is extended to have:
>>
>> 1) three virtqueues: RXVQ, TXVQ and CVQ (control virtqueue)
>> 2) two virtqueue groups: group 0 contains RXVQ and TXVQ; group 1
>>     contains CVQ
>> 3) two address spaces and the simulator simply implements the address
>>     spaces by mapping it 1:1 to IOTLB.
>>
>> For the VM use cases, userspace(Qemu) may set AS 0 to group 0 and AS 1
>> to group 1. So we have:
>>
>> 1) The IOTLB for virtqueue group 0 contains the mappings of guest, so
>>     RX and TX can be assigned to guest directly.
>> 2) The IOTLB for virtqueue group 1 contains the mappings of CVQ which
>>     is the buffers that allocated and managed by VMM only. So CVQ of
>>     vhost-vdpa is visible to VMM only. And Guest can not access the CVQ
>>     of vhost-vdpa.
>>
>> For the other use cases, since AS 0 is associated to all virtqueue
>> groups by default. All virtqueues share the same mapping by default.
>>
>> To demonstrate the function, VIRITO_NET_F_CTRL_MACADDR is
>> implemented in the simulator for the driver to set mac address.
>>
> Hi Jason,
>
> is there any version of qemu/libvirt available that I can see the
> control virtqueue working in action?


Not yet, the qemu part depends on the shadow virtqueue work of Eugenio. 
But it will work as:

1) qemu will use a separated address space for the control virtqueue 
(shadow) exposed through vhost-vDPA
2) the commands sent through control virtqueue by guest driver will 
intercept by qemu
3) Qemu will send those commands to the shadow control virtqueue

Eugenio, any ETA for the new version of shadow virtqueue support in Qemu?

Thanks


>

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 21/21] vdpasim: control virtqueue support
  2021-01-12  3:11       ` Jason Wang
  (?)
@ 2021-01-22 19:43       ` Eugenio Perez Martin
  2021-01-25  3:16           ` Jason Wang
  -1 siblings, 1 reply; 136+ messages in thread
From: Eugenio Perez Martin @ 2021-01-22 19:43 UTC (permalink / raw)
  To: Jason Wang
  Cc: Eli Cohen, Michael Tsirkin, kvm list, virtualization, netdev,
	linux-kernel, Cindy Lu, Eli Cohen, lingshan.zhu, Rob Miller,
	Stefan Hajnoczi, Stefano Garzarella

On Tue, Jan 12, 2021 at 4:12 AM Jason Wang <jasowang@redhat.com> wrote:
>
>
> On 2021/1/11 下午8:26, Eli Cohen wrote:
> > On Wed, Dec 16, 2020 at 02:48:18PM +0800, Jason Wang wrote:
> >> This patch introduces the control virtqueue support for vDPA
> >> simulator. This is a requirement for supporting advanced features like
> >> multiqueue.
> >>
> >> A requirement for control virtqueue is to isolate its memory access
> >> from the rx/tx virtqueues. This is because when using vDPA device
> >> for VM, the control virqueue is not directly assigned to VM. Userspace
> >> (Qemu) will present a shadow control virtqueue to control for
> >> recording the device states.
> >>
> >> The isolation is done via the virtqueue groups and ASID support in
> >> vDPA through vhost-vdpa. The simulator is extended to have:
> >>
> >> 1) three virtqueues: RXVQ, TXVQ and CVQ (control virtqueue)
> >> 2) two virtqueue groups: group 0 contains RXVQ and TXVQ; group 1
> >>     contains CVQ
> >> 3) two address spaces and the simulator simply implements the address
> >>     spaces by mapping it 1:1 to IOTLB.
> >>
> >> For the VM use cases, userspace(Qemu) may set AS 0 to group 0 and AS 1
> >> to group 1. So we have:
> >>
> >> 1) The IOTLB for virtqueue group 0 contains the mappings of guest, so
> >>     RX and TX can be assigned to guest directly.
> >> 2) The IOTLB for virtqueue group 1 contains the mappings of CVQ which
> >>     is the buffers that allocated and managed by VMM only. So CVQ of
> >>     vhost-vdpa is visible to VMM only. And Guest can not access the CVQ
> >>     of vhost-vdpa.
> >>
> >> For the other use cases, since AS 0 is associated to all virtqueue
> >> groups by default. All virtqueues share the same mapping by default.
> >>
> >> To demonstrate the function, VIRITO_NET_F_CTRL_MACADDR is
> >> implemented in the simulator for the driver to set mac address.
> >>
> > Hi Jason,
> >
> > is there any version of qemu/libvirt available that I can see the
> > control virtqueue working in action?
>
>
> Not yet, the qemu part depends on the shadow virtqueue work of Eugenio.
> But it will work as:
>
> 1) qemu will use a separated address space for the control virtqueue
> (shadow) exposed through vhost-vDPA
> 2) the commands sent through control virtqueue by guest driver will
> intercept by qemu
> 3) Qemu will send those commands to the shadow control virtqueue
>
> Eugenio, any ETA for the new version of shadow virtqueue support in Qemu?
>

Hi Jason. Sorry for the late response.

For the notification part I have addressed all the issues of the RFC
[1], except the potential race conditions Stefan pointed, and tested
with vdpa devices. You can find at
https://github.com/eugpermar/qemu/tree/vdpa_sw_live_migration.d/notifications.rfc
. Since the shadow path is activated only through QMP and does not
interfere with regular operation, I could post to the qemu list if you
prefer. The series will be smaller if merged in steps.

Adding the buffer forwarding on top should not take long.

[1] https://lkml.org/lkml/2020/9/23/1243

Thanks!


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 21/21] vdpasim: control virtqueue support
  2021-01-22 19:43       ` Eugenio Perez Martin
@ 2021-01-25  3:16           ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2021-01-25  3:16 UTC (permalink / raw)
  To: Eugenio Perez Martin
  Cc: Eli Cohen, Michael Tsirkin, kvm list, virtualization, netdev,
	linux-kernel, Cindy Lu, Eli Cohen, lingshan.zhu, Rob Miller,
	Stefan Hajnoczi, Stefano Garzarella


On 2021/1/23 上午3:43, Eugenio Perez Martin wrote:
> On Tue, Jan 12, 2021 at 4:12 AM Jason Wang <jasowang@redhat.com> wrote:
>>
>> On 2021/1/11 下午8:26, Eli Cohen wrote:
>>> On Wed, Dec 16, 2020 at 02:48:18PM +0800, Jason Wang wrote:
>>>> This patch introduces the control virtqueue support for vDPA
>>>> simulator. This is a requirement for supporting advanced features like
>>>> multiqueue.
>>>>
>>>> A requirement for control virtqueue is to isolate its memory access
>>>> from the rx/tx virtqueues. This is because when using vDPA device
>>>> for VM, the control virqueue is not directly assigned to VM. Userspace
>>>> (Qemu) will present a shadow control virtqueue to control for
>>>> recording the device states.
>>>>
>>>> The isolation is done via the virtqueue groups and ASID support in
>>>> vDPA through vhost-vdpa. The simulator is extended to have:
>>>>
>>>> 1) three virtqueues: RXVQ, TXVQ and CVQ (control virtqueue)
>>>> 2) two virtqueue groups: group 0 contains RXVQ and TXVQ; group 1
>>>>      contains CVQ
>>>> 3) two address spaces and the simulator simply implements the address
>>>>      spaces by mapping it 1:1 to IOTLB.
>>>>
>>>> For the VM use cases, userspace(Qemu) may set AS 0 to group 0 and AS 1
>>>> to group 1. So we have:
>>>>
>>>> 1) The IOTLB for virtqueue group 0 contains the mappings of guest, so
>>>>      RX and TX can be assigned to guest directly.
>>>> 2) The IOTLB for virtqueue group 1 contains the mappings of CVQ which
>>>>      is the buffers that allocated and managed by VMM only. So CVQ of
>>>>      vhost-vdpa is visible to VMM only. And Guest can not access the CVQ
>>>>      of vhost-vdpa.
>>>>
>>>> For the other use cases, since AS 0 is associated to all virtqueue
>>>> groups by default. All virtqueues share the same mapping by default.
>>>>
>>>> To demonstrate the function, VIRITO_NET_F_CTRL_MACADDR is
>>>> implemented in the simulator for the driver to set mac address.
>>>>
>>> Hi Jason,
>>>
>>> is there any version of qemu/libvirt available that I can see the
>>> control virtqueue working in action?
>>
>> Not yet, the qemu part depends on the shadow virtqueue work of Eugenio.
>> But it will work as:
>>
>> 1) qemu will use a separated address space for the control virtqueue
>> (shadow) exposed through vhost-vDPA
>> 2) the commands sent through control virtqueue by guest driver will
>> intercept by qemu
>> 3) Qemu will send those commands to the shadow control virtqueue
>>
>> Eugenio, any ETA for the new version of shadow virtqueue support in Qemu?
>>
> Hi Jason. Sorry for the late response.
>
> For the notification part I have addressed all the issues of the RFC
> [1], except the potential race conditions Stefan pointed, and tested
> with vdpa devices. You can find at
> https://github.com/eugpermar/qemu/tree/vdpa_sw_live_migration.d/notifications.rfc
> . Since the shadow path is activated only through QMP and does not
> interfere with regular operation, I could post to the qemu list if you
> prefer. The series will be smaller if merged in steps.


Sure. Please post them.


>
> Adding the buffer forwarding on top should not take long.
>
> [1] https://lkml.org/lkml/2020/9/23/1243
>
> Thanks!


Thanks



^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [PATCH 21/21] vdpasim: control virtqueue support
@ 2021-01-25  3:16           ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2021-01-25  3:16 UTC (permalink / raw)
  To: Eugenio Perez Martin
  Cc: Cindy Lu, kvm list, Michael Tsirkin, netdev, linux-kernel,
	virtualization, Stefan Hajnoczi, Eli Cohen, Eli Cohen,
	lingshan.zhu, Rob Miller


On 2021/1/23 上午3:43, Eugenio Perez Martin wrote:
> On Tue, Jan 12, 2021 at 4:12 AM Jason Wang <jasowang@redhat.com> wrote:
>>
>> On 2021/1/11 下午8:26, Eli Cohen wrote:
>>> On Wed, Dec 16, 2020 at 02:48:18PM +0800, Jason Wang wrote:
>>>> This patch introduces the control virtqueue support for vDPA
>>>> simulator. This is a requirement for supporting advanced features like
>>>> multiqueue.
>>>>
>>>> A requirement for control virtqueue is to isolate its memory access
>>>> from the rx/tx virtqueues. This is because when using vDPA device
>>>> for VM, the control virqueue is not directly assigned to VM. Userspace
>>>> (Qemu) will present a shadow control virtqueue to control for
>>>> recording the device states.
>>>>
>>>> The isolation is done via the virtqueue groups and ASID support in
>>>> vDPA through vhost-vdpa. The simulator is extended to have:
>>>>
>>>> 1) three virtqueues: RXVQ, TXVQ and CVQ (control virtqueue)
>>>> 2) two virtqueue groups: group 0 contains RXVQ and TXVQ; group 1
>>>>      contains CVQ
>>>> 3) two address spaces and the simulator simply implements the address
>>>>      spaces by mapping it 1:1 to IOTLB.
>>>>
>>>> For the VM use cases, userspace(Qemu) may set AS 0 to group 0 and AS 1
>>>> to group 1. So we have:
>>>>
>>>> 1) The IOTLB for virtqueue group 0 contains the mappings of guest, so
>>>>      RX and TX can be assigned to guest directly.
>>>> 2) The IOTLB for virtqueue group 1 contains the mappings of CVQ which
>>>>      is the buffers that allocated and managed by VMM only. So CVQ of
>>>>      vhost-vdpa is visible to VMM only. And Guest can not access the CVQ
>>>>      of vhost-vdpa.
>>>>
>>>> For the other use cases, since AS 0 is associated to all virtqueue
>>>> groups by default. All virtqueues share the same mapping by default.
>>>>
>>>> To demonstrate the function, VIRITO_NET_F_CTRL_MACADDR is
>>>> implemented in the simulator for the driver to set mac address.
>>>>
>>> Hi Jason,
>>>
>>> is there any version of qemu/libvirt available that I can see the
>>> control virtqueue working in action?
>>
>> Not yet, the qemu part depends on the shadow virtqueue work of Eugenio.
>> But it will work as:
>>
>> 1) qemu will use a separated address space for the control virtqueue
>> (shadow) exposed through vhost-vDPA
>> 2) the commands sent through control virtqueue by guest driver will
>> intercept by qemu
>> 3) Qemu will send those commands to the shadow control virtqueue
>>
>> Eugenio, any ETA for the new version of shadow virtqueue support in Qemu?
>>
> Hi Jason. Sorry for the late response.
>
> For the notification part I have addressed all the issues of the RFC
> [1], except the potential race conditions Stefan pointed, and tested
> with vdpa devices. You can find at
> https://github.com/eugpermar/qemu/tree/vdpa_sw_live_migration.d/notifications.rfc
> . Since the shadow path is activated only through QMP and does not
> interfere with regular operation, I could post to the qemu list if you
> prefer. The series will be smaller if merged in steps.


Sure. Please post them.


>
> Adding the buffer forwarding on top should not take long.
>
> [1] https://lkml.org/lkml/2020/9/23/1243
>
> Thanks!


Thanks


_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 00/19] Control VQ support in vDPA
  2020-12-16  6:47 ` Jason Wang
                   ` (23 preceding siblings ...)
  (?)
@ 2022-02-24 21:22 ` Gautam Dawar
  2022-02-24 21:22   ` [RFC PATCH v2 01/19] vhost: move the backend feature bits to vhost_types.h Gautam Dawar
                     ` (19 more replies)
  -1 siblings, 20 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Michael S. Tsirkin,
	Jason Wang, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

Hi All:

This series tries to add the support for control virtqueue in vDPA.

Control virtqueue is used by networking device for accepting various
commands from the driver. It's a must to support multiqueue and other
configurations.

When used by vhost-vDPA bus driver for VM, the control virtqueue
should be shadowed via userspace VMM (Qemu) instead of being assigned
directly to Guest. This is because Qemu needs to know the device state
in order to start and stop device correctly (e.g for Live Migration).

This requies to isolate the memory mapping for control virtqueue
presented by vhost-vDPA to prevent guest from accessing it directly.

To achieve this, vDPA introduce two new abstractions:

- address space: identified through address space id (ASID) and a set
                 of memory mapping in maintained
- virtqueue group: the minimal set of virtqueues that must share an
                 address space

Device needs to advertise the following attributes to vDPA:

- the number of address spaces supported in the device
- the number of virtqueue groups supported in the device
- the mappings from a specific virtqueue to its virtqueue groups

The mappings from virtqueue to virtqueue groups is fixed and defined
by vDPA device driver. E.g:

- For the device that has hardware ASID support, it can simply
  advertise a per virtqueue virtqueue group.
- For the device that does not have hardware ASID support, it can
  simply advertise a single virtqueue group that contains all
  virtqueues. Or if it wants a software emulated control virtqueue, it
  can advertise two virtqueue groups, one is for cvq, another is for
  the rest virtqueues.

vDPA also allow to change the association between virtqueue group and
address space. So in the case of control virtqueue, userspace
VMM(Qemu) may use a dedicated address space for the control virtqueue
group to isolate the memory mapping.

The vhost/vhost-vDPA is also extend for the userspace to:

- query the number of virtqueue groups and address spaces supported by
  the device
- query the virtqueue group for a specific virtqueue
- assocaite a virtqueue group with an address space
- send ASID based IOTLB commands

This will help userspace VMM(Qemu) to detect whether the control vq
could be supported and isolate memory mappings of control virtqueue
from the others.

To demonstrate the usage, vDPA simulator is extended to support
setting MAC address via a emulated control virtqueue.

Please review.

Changes since v1:

- Rebased the v1 patch series on vhost branch of MST vhost git repo
  git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git/log/?h=vhost
- Updates to accommodate vdpa_sim changes from monolithic module in
  kernel used v1 patch series to current modularized class (net, block)
  based approach.
- Added new attributes (ngroups and nas) to "vdpasim_dev_attr" and
  propagated them from vdpa_sim_net to vdpa_sim
- Widened the data-type for "asid" member of vhost_msg_v2 to __u32
  to accommodate PASID
- Fixed the buildbot warnings
- Resolved all checkpatch.pl errors and warnings
- Tested both control and datapath with Xilinx Smartnic SN1000 series
  device using QEMU implementing the Shadow virtqueue and support for
  VQ groups and ASID available at:
  github.com/eugpermar/qemu/releases/tag/vdpa_sw_live_migration.d%2F
  asid_groups-v1.d%2F00

Changes since RFC:

- tweak vhost uAPI documentation
- switch to use device specific IOTLB really in patch 4
- tweak the commit log
- fix that ASID in vhost is claimed to be 32 actually but 16bit
  actually
- fix use after free when using ASID with IOTLB batching requests
- switch to use Stefano's patch for having separated iov
- remove unused "used_as" variable
- fix the iotlb/asid checking in vhost_vdpa_unmap()

Thanks

Gautam Dawar (19):
  vhost: move the backend feature bits to vhost_types.h
  virtio-vdpa: don't set callback if virtio doesn't need it
  vhost-vdpa: passing iotlb to IOMMU mapping helpers
  vhost-vdpa: switch to use vhost-vdpa specific IOTLB
  vdpa: introduce virtqueue groups
  vdpa: multiple address spaces support
  vdpa: introduce config operations for associating ASID to a virtqueue
    group
  vhost_iotlb: split out IOTLB initialization
  vhost: support ASID in IOTLB API
  vhost-vdpa: introduce asid based IOTLB
  vhost-vdpa: introduce uAPI to get the number of virtqueue groups
  vhost-vdpa: introduce uAPI to get the number of address spaces
  vhost-vdpa: uAPI to get virtqueue group id
  vhost-vdpa: introduce uAPI to set group ASID
  vhost-vdpa: support ASID based IOTLB API
  vdpa_sim: advertise VIRTIO_NET_F_MTU
  vdpa_sim: factor out buffer completion logic
  vdpa_sim: filter destination mac address
  vdpasim: control virtqueue support

 drivers/vdpa/ifcvf/ifcvf_main.c      |   8 +-
 drivers/vdpa/mlx5/net/mlx5_vnet.c    |  11 +-
 drivers/vdpa/vdpa.c                  |   5 +
 drivers/vdpa/vdpa_sim/vdpa_sim.c     | 100 ++++++++--
 drivers/vdpa/vdpa_sim/vdpa_sim.h     |   3 +
 drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 169 +++++++++++++----
 drivers/vhost/iotlb.c                |  23 ++-
 drivers/vhost/vdpa.c                 | 272 +++++++++++++++++++++------
 drivers/vhost/vhost.c                |  23 ++-
 drivers/vhost/vhost.h                |   4 +-
 drivers/virtio/virtio_vdpa.c         |   2 +-
 include/linux/vdpa.h                 |  46 ++++-
 include/linux/vhost_iotlb.h          |   2 +
 include/uapi/linux/vhost.h           |  25 ++-
 include/uapi/linux/vhost_types.h     |  11 +-
 15 files changed, 566 insertions(+), 138 deletions(-)

-- 
2.25.0


^ permalink raw reply	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 01/19] vhost: move the backend feature bits to vhost_types.h
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-02-24 21:22   ` [RFC PATCH v2 02/19] virtio-vdpa: don't set callback if virtio doesn't need it Gautam Dawar
                     ` (18 subsequent siblings)
  19 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

We should store feature bits in vhost_types.h as what has been done
for e.g VHOST_F_LOG_ALL.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 include/uapi/linux/vhost.h       | 5 -----
 include/uapi/linux/vhost_types.h | 5 +++++
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index c998860d7bbc..59c6c0fbaba1 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -89,11 +89,6 @@
 
 /* Set or get vhost backend capability */
 
-/* Use message type V2 */
-#define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1
-/* IOTLB can accept batching hints */
-#define VHOST_BACKEND_F_IOTLB_BATCH  0x2
-
 #define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64)
 #define VHOST_GET_BACKEND_FEATURES _IOR(VHOST_VIRTIO, 0x26, __u64)
 
diff --git a/include/uapi/linux/vhost_types.h b/include/uapi/linux/vhost_types.h
index f7f6a3a28977..76ee7016c501 100644
--- a/include/uapi/linux/vhost_types.h
+++ b/include/uapi/linux/vhost_types.h
@@ -153,4 +153,9 @@ struct vhost_vdpa_iova_range {
 /* vhost-net should add virtio_net_hdr for RX, and strip for TX packets. */
 #define VHOST_NET_F_VIRTIO_NET_HDR 27
 
+/* Use message type V2 */
+#define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1
+/* IOTLB can accept batching hints */
+#define VHOST_BACKEND_F_IOTLB_BATCH  0x2
+
 #endif
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 02/19] virtio-vdpa: don't set callback if virtio doesn't need it
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
  2022-02-24 21:22   ` [RFC PATCH v2 01/19] vhost: move the backend feature bits to vhost_types.h Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-02-24 21:22   ` [RFC PATCH v2 03/19] vhost-vdpa: passing iotlb to IOMMU mapping helpers Gautam Dawar
                     ` (17 subsequent siblings)
  19 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

There's no need for setting callbacks for the driver that doesn't care
about that.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/virtio/virtio_vdpa.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/virtio/virtio_vdpa.c b/drivers/virtio/virtio_vdpa.c
index 7767a7f0119b..bc74ba5fa717 100644
--- a/drivers/virtio/virtio_vdpa.c
+++ b/drivers/virtio/virtio_vdpa.c
@@ -184,7 +184,7 @@ virtio_vdpa_setup_vq(struct virtio_device *vdev, unsigned int index,
 	}
 
 	/* Setup virtqueue callback */
-	cb.callback = virtio_vdpa_virtqueue_cb;
+	cb.callback = callback ? virtio_vdpa_virtqueue_cb : NULL;
 	cb.private = info;
 	ops->set_vq_cb(vdpa, index, &cb);
 	ops->set_vq_num(vdpa, index, virtqueue_get_vring_size(vq));
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 03/19] vhost-vdpa: passing iotlb to IOMMU mapping helpers
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
  2022-02-24 21:22   ` [RFC PATCH v2 01/19] vhost: move the backend feature bits to vhost_types.h Gautam Dawar
  2022-02-24 21:22   ` [RFC PATCH v2 02/19] virtio-vdpa: don't set callback if virtio doesn't need it Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-02-24 21:22   ` [RFC PATCH v2 04/19] vhost-vdpa: switch to use vhost-vdpa specific IOTLB Gautam Dawar
                     ` (16 subsequent siblings)
  19 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

To prepare for the ASID support for vhost-vdpa, try to pass IOTLB
object to dma helpers. No functional changes, it's just a preparation
for support multiple IOTLBs.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vhost/vdpa.c | 67 ++++++++++++++++++++++++--------------------
 1 file changed, 37 insertions(+), 30 deletions(-)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 851539807bc9..146911082514 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -503,10 +503,11 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
 	return r;
 }
 
-static void vhost_vdpa_pa_unmap(struct vhost_vdpa *v, u64 start, u64 last)
+static void vhost_vdpa_pa_unmap(struct vhost_vdpa *v,
+				struct vhost_iotlb *iotlb,
+				u64 start, u64 last)
 {
 	struct vhost_dev *dev = &v->vdev;
-	struct vhost_iotlb *iotlb = dev->iotlb;
 	struct vhost_iotlb_map *map;
 	struct page *page;
 	unsigned long pfn, pinned;
@@ -525,10 +526,10 @@ static void vhost_vdpa_pa_unmap(struct vhost_vdpa *v, u64 start, u64 last)
 	}
 }
 
-static void vhost_vdpa_va_unmap(struct vhost_vdpa *v, u64 start, u64 last)
+static void vhost_vdpa_va_unmap(struct vhost_vdpa *v,
+				struct vhost_iotlb *iotlb,
+				u64 start, u64 last)
 {
-	struct vhost_dev *dev = &v->vdev;
-	struct vhost_iotlb *iotlb = dev->iotlb;
 	struct vhost_iotlb_map *map;
 	struct vdpa_map_file *map_file;
 
@@ -540,21 +541,24 @@ static void vhost_vdpa_va_unmap(struct vhost_vdpa *v, u64 start, u64 last)
 	}
 }
 
-static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v, u64 start, u64 last)
+static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
+				   struct vhost_iotlb *iotlb,
+				   u64 start, u64 last)
 {
 	struct vdpa_device *vdpa = v->vdpa;
 
 	if (vdpa->use_va)
-		return vhost_vdpa_va_unmap(v, start, last);
+		return vhost_vdpa_va_unmap(v, iotlb, start, last);
 
-	return vhost_vdpa_pa_unmap(v, start, last);
+	return vhost_vdpa_pa_unmap(v, iotlb, start, last);
 }
 
 static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v)
 {
 	struct vhost_dev *dev = &v->vdev;
+	struct vhost_iotlb *iotlb = dev->iotlb;
 
-	vhost_vdpa_iotlb_unmap(v, 0ULL, 0ULL - 1);
+	vhost_vdpa_iotlb_unmap(v, iotlb, 0ULL, 0ULL - 1);
 	kfree(dev->iotlb);
 	dev->iotlb = NULL;
 }
@@ -581,15 +585,15 @@ static int perm_to_iommu_flags(u32 perm)
 	return flags | IOMMU_CACHE;
 }
 
-static int vhost_vdpa_map(struct vhost_vdpa *v, u64 iova,
-			  u64 size, u64 pa, u32 perm, void *opaque)
+static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
+			  u64 iova, u64 size, u64 pa, u32 perm, void *opaque)
 {
 	struct vhost_dev *dev = &v->vdev;
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
 	int r = 0;
 
-	r = vhost_iotlb_add_range_ctx(dev->iotlb, iova, iova + size - 1,
+	r = vhost_iotlb_add_range_ctx(iotlb, iova, iova + size - 1,
 				      pa, perm, opaque);
 	if (r)
 		return r;
@@ -598,13 +602,13 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, u64 iova,
 		r = ops->dma_map(vdpa, iova, size, pa, perm, opaque);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			r = ops->set_map(vdpa, dev->iotlb);
+			r = ops->set_map(vdpa, iotlb);
 	} else {
 		r = iommu_map(v->domain, iova, pa, size,
 			      perm_to_iommu_flags(perm));
 	}
 	if (r) {
-		vhost_iotlb_del_range(dev->iotlb, iova, iova + size - 1);
+		vhost_iotlb_del_range(iotlb, iova, iova + size - 1);
 		return r;
 	}
 
@@ -614,25 +618,27 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, u64 iova,
 	return 0;
 }
 
-static void vhost_vdpa_unmap(struct vhost_vdpa *v, u64 iova, u64 size)
+static void vhost_vdpa_unmap(struct vhost_vdpa *v,
+			     struct vhost_iotlb *iotlb,
+			     u64 iova, u64 size)
 {
-	struct vhost_dev *dev = &v->vdev;
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
 
-	vhost_vdpa_iotlb_unmap(v, iova, iova + size - 1);
+	vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
 
 	if (ops->dma_map) {
 		ops->dma_unmap(vdpa, iova, size);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			ops->set_map(vdpa, dev->iotlb);
+			ops->set_map(vdpa, iotlb);
 	} else {
 		iommu_unmap(v->domain, iova, size);
 	}
 }
 
 static int vhost_vdpa_va_map(struct vhost_vdpa *v,
+			     struct vhost_iotlb *iotlb,
 			     u64 iova, u64 size, u64 uaddr, u32 perm)
 {
 	struct vhost_dev *dev = &v->vdev;
@@ -662,7 +668,7 @@ static int vhost_vdpa_va_map(struct vhost_vdpa *v,
 		offset = (vma->vm_pgoff << PAGE_SHIFT) + uaddr - vma->vm_start;
 		map_file->offset = offset;
 		map_file->file = get_file(vma->vm_file);
-		ret = vhost_vdpa_map(v, map_iova, map_size, uaddr,
+		ret = vhost_vdpa_map(v, iotlb, map_iova, map_size, uaddr,
 				     perm, map_file);
 		if (ret) {
 			fput(map_file->file);
@@ -675,7 +681,7 @@ static int vhost_vdpa_va_map(struct vhost_vdpa *v,
 		map_iova += map_size;
 	}
 	if (ret)
-		vhost_vdpa_unmap(v, iova, map_iova - iova);
+		vhost_vdpa_unmap(v, iotlb, iova, map_iova - iova);
 
 	mmap_read_unlock(dev->mm);
 
@@ -683,6 +689,7 @@ static int vhost_vdpa_va_map(struct vhost_vdpa *v,
 }
 
 static int vhost_vdpa_pa_map(struct vhost_vdpa *v,
+			     struct vhost_iotlb *iotlb,
 			     u64 iova, u64 size, u64 uaddr, u32 perm)
 {
 	struct vhost_dev *dev = &v->vdev;
@@ -746,7 +753,7 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v,
 			if (last_pfn && (this_pfn != last_pfn + 1)) {
 				/* Pin a contiguous chunk of memory */
 				csize = PFN_PHYS(last_pfn - map_pfn + 1);
-				ret = vhost_vdpa_map(v, iova, csize,
+				ret = vhost_vdpa_map(v, iotlb, iova, csize,
 						     PFN_PHYS(map_pfn),
 						     perm, NULL);
 				if (ret) {
@@ -776,7 +783,7 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v,
 	}
 
 	/* Pin the rest chunk */
-	ret = vhost_vdpa_map(v, iova, PFN_PHYS(last_pfn - map_pfn + 1),
+	ret = vhost_vdpa_map(v, iotlb, iova, PFN_PHYS(last_pfn - map_pfn + 1),
 			     PFN_PHYS(map_pfn), perm, NULL);
 out:
 	if (ret) {
@@ -796,7 +803,7 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v,
 			for (pfn = map_pfn; pfn <= last_pfn; pfn++)
 				unpin_user_page(pfn_to_page(pfn));
 		}
-		vhost_vdpa_unmap(v, start, size);
+		vhost_vdpa_unmap(v, iotlb, start, size);
 	}
 unlock:
 	mmap_read_unlock(dev->mm);
@@ -807,11 +814,10 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v,
 }
 
 static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
+					   struct vhost_iotlb *iotlb,
 					   struct vhost_iotlb_msg *msg)
 {
-	struct vhost_dev *dev = &v->vdev;
 	struct vdpa_device *vdpa = v->vdpa;
-	struct vhost_iotlb *iotlb = dev->iotlb;
 
 	if (msg->iova < v->range.first || !msg->size ||
 	    msg->iova > U64_MAX - msg->size + 1 ||
@@ -823,10 +829,10 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
 		return -EEXIST;
 
 	if (vdpa->use_va)
-		return vhost_vdpa_va_map(v, msg->iova, msg->size,
+		return vhost_vdpa_va_map(v, iotlb, msg->iova, msg->size,
 					 msg->uaddr, msg->perm);
 
-	return vhost_vdpa_pa_map(v, msg->iova, msg->size, msg->uaddr,
+	return vhost_vdpa_pa_map(v, iotlb, msg->iova, msg->size, msg->uaddr,
 				 msg->perm);
 }
 
@@ -836,6 +842,7 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
 	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
+	struct vhost_iotlb *iotlb = dev->iotlb;
 	int r = 0;
 
 	mutex_lock(&dev->mutex);
@@ -846,17 +853,17 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
 
 	switch (msg->type) {
 	case VHOST_IOTLB_UPDATE:
-		r = vhost_vdpa_process_iotlb_update(v, msg);
+		r = vhost_vdpa_process_iotlb_update(v, iotlb, msg);
 		break;
 	case VHOST_IOTLB_INVALIDATE:
-		vhost_vdpa_unmap(v, msg->iova, msg->size);
+		vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
 		break;
 	case VHOST_IOTLB_BATCH_BEGIN:
 		v->in_batch = true;
 		break;
 	case VHOST_IOTLB_BATCH_END:
 		if (v->in_batch && ops->set_map)
-			ops->set_map(vdpa, dev->iotlb);
+			ops->set_map(vdpa, iotlb);
 		v->in_batch = false;
 		break;
 	default:
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 04/19] vhost-vdpa: switch to use vhost-vdpa specific IOTLB
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (2 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 03/19] vhost-vdpa: passing iotlb to IOMMU mapping helpers Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-02-24 21:22   ` [RFC PATCH v2 05/19] vdpa: introduce virtqueue groups Gautam Dawar
                     ` (15 subsequent siblings)
  19 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

To ease the implementation of per group ASID support for vDPA
device. This patch switches to use a vhost-vdpa specific IOTLB to
avoid the unnecessary refactoring of the vhost core.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vhost/vdpa.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 146911082514..655ff7029401 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -39,6 +39,7 @@ struct vhost_vdpa {
 	struct vhost_virtqueue *vqs;
 	struct completion completion;
 	struct vdpa_device *vdpa;
+	struct vhost_iotlb *iotlb;
 	struct device dev;
 	struct cdev cdev;
 	atomic_t opened;
@@ -555,12 +556,11 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
 
 static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v)
 {
-	struct vhost_dev *dev = &v->vdev;
-	struct vhost_iotlb *iotlb = dev->iotlb;
+	struct vhost_iotlb *iotlb = v->iotlb;
 
 	vhost_vdpa_iotlb_unmap(v, iotlb, 0ULL, 0ULL - 1);
-	kfree(dev->iotlb);
-	dev->iotlb = NULL;
+	kfree(v->iotlb);
+	v->iotlb = NULL;
 }
 
 static int perm_to_iommu_flags(u32 perm)
@@ -842,7 +842,7 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
 	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
-	struct vhost_iotlb *iotlb = dev->iotlb;
+	struct vhost_iotlb *iotlb = v->iotlb;
 	int r = 0;
 
 	mutex_lock(&dev->mutex);
@@ -982,15 +982,15 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 	vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
 		       vhost_vdpa_process_iotlb_msg);
 
-	dev->iotlb = vhost_iotlb_alloc(0, 0);
-	if (!dev->iotlb) {
+	v->iotlb = vhost_iotlb_alloc(0, 0);
+	if (!v->iotlb) {
 		r = -ENOMEM;
 		goto err_init_iotlb;
 	}
 
 	r = vhost_vdpa_alloc_domain(v);
 	if (r)
-		goto err_init_iotlb;
+		goto err_alloc_domain;
 
 	vhost_vdpa_set_iova_range(v);
 
@@ -998,6 +998,8 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 
 	return 0;
 
+err_alloc_domain:
+	vhost_vdpa_iotlb_free(v);
 err_init_iotlb:
 	vhost_dev_cleanup(&v->vdev);
 	kfree(vqs);
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 05/19] vdpa: introduce virtqueue groups
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (3 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 04/19] vhost-vdpa: switch to use vhost-vdpa specific IOTLB Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-02-28  8:07       ` Jason Wang
  2022-02-24 21:22   ` [RFC PATCH v2 06/19] vdpa: multiple address spaces support Gautam Dawar
                     ` (14 subsequent siblings)
  19 siblings, 1 reply; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

This patch introduces virtqueue groups to vDPA device. The virtqueue
group is the minimal set of virtqueues that must share an address
space. And the address space identifier could only be attached to
a specific virtqueue group.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vdpa/ifcvf/ifcvf_main.c   |  8 +++++++-
 drivers/vdpa/mlx5/net/mlx5_vnet.c |  8 +++++++-
 drivers/vdpa/vdpa.c               |  3 +++
 drivers/vdpa/vdpa_sim/vdpa_sim.c  |  9 ++++++++-
 drivers/vdpa/vdpa_sim/vdpa_sim.h  |  1 +
 include/linux/vdpa.h              | 16 ++++++++++++----
 6 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_main.c
index d1a6b5ab543c..c815a2e62440 100644
--- a/drivers/vdpa/ifcvf/ifcvf_main.c
+++ b/drivers/vdpa/ifcvf/ifcvf_main.c
@@ -378,6 +378,11 @@ static size_t ifcvf_vdpa_get_config_size(struct vdpa_device *vdpa_dev)
 	return  vf->config_size;
 }
 
+static u32 ifcvf_vdpa_get_vq_group(struct vdpa_device *vdpa, u16 idx)
+{
+	return 0;
+}
+
 static void ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev,
 				  unsigned int offset,
 				  void *buf, unsigned int len)
@@ -453,6 +458,7 @@ static const struct vdpa_config_ops ifc_vdpa_ops = {
 	.get_device_id	= ifcvf_vdpa_get_device_id,
 	.get_vendor_id	= ifcvf_vdpa_get_vendor_id,
 	.get_vq_align	= ifcvf_vdpa_get_vq_align,
+	.get_vq_group	= ifcvf_vdpa_get_vq_group,
 	.get_config_size	= ifcvf_vdpa_get_config_size,
 	.get_config	= ifcvf_vdpa_get_config,
 	.set_config	= ifcvf_vdpa_set_config,
@@ -507,7 +513,7 @@ static int ifcvf_vdpa_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
 	pdev = ifcvf_mgmt_dev->pdev;
 	dev = &pdev->dev;
 	adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa,
-				    dev, &ifc_vdpa_ops, name, false);
+				    dev, &ifc_vdpa_ops, 1, name, false);
 	if (IS_ERR(adapter)) {
 		IFCVF_ERR(pdev, "Failed to allocate vDPA structure");
 		return PTR_ERR(adapter);
diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c
index b53603d94082..fcfc28460b72 100644
--- a/drivers/vdpa/mlx5/net/mlx5_vnet.c
+++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c
@@ -1847,6 +1847,11 @@ static u32 mlx5_vdpa_get_vq_align(struct vdpa_device *vdev)
 	return PAGE_SIZE;
 }
 
+static u32 mlx5_vdpa_get_vq_group(struct vdpa_device *vdpa, u16 idx)
+{
+	return 0;
+}
+
 enum { MLX5_VIRTIO_NET_F_GUEST_CSUM = 1 << 9,
 	MLX5_VIRTIO_NET_F_CSUM = 1 << 10,
 	MLX5_VIRTIO_NET_F_HOST_TSO6 = 1 << 11,
@@ -2363,6 +2368,7 @@ static const struct vdpa_config_ops mlx5_vdpa_ops = {
 	.get_vq_notification = mlx5_get_vq_notification,
 	.get_vq_irq = mlx5_get_vq_irq,
 	.get_vq_align = mlx5_vdpa_get_vq_align,
+	.get_vq_group = mlx5_vdpa_get_vq_group,
 	.get_device_features = mlx5_vdpa_get_device_features,
 	.set_driver_features = mlx5_vdpa_set_driver_features,
 	.get_driver_features = mlx5_vdpa_get_driver_features,
@@ -2575,7 +2581,7 @@ static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
 	}
 
 	ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
-				 name, false);
+				 1, name, false);
 	if (IS_ERR(ndev))
 		return PTR_ERR(ndev);
 
diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c
index 9846c9de4bfa..a07bf0130559 100644
--- a/drivers/vdpa/vdpa.c
+++ b/drivers/vdpa/vdpa.c
@@ -159,6 +159,7 @@ static void vdpa_release_dev(struct device *d)
  * initialized but before registered.
  * @parent: the parent device
  * @config: the bus operations that is supported by this device
+ * @ngroups: number of groups supported by this device
  * @size: size of the parent structure that contains private data
  * @name: name of the vdpa device; optional.
  * @use_va: indicate whether virtual address must be used by this device
@@ -171,6 +172,7 @@ static void vdpa_release_dev(struct device *d)
  */
 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 					const struct vdpa_config_ops *config,
+					unsigned int ngroups,
 					size_t size, const char *name,
 					bool use_va)
 {
@@ -203,6 +205,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 	vdev->config = config;
 	vdev->features_valid = false;
 	vdev->use_va = use_va;
+	vdev->ngroups = ngroups;
 
 	if (name)
 		err = dev_set_name(&vdev->dev, "%s", name);
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index ddbe142af09a..c98cb1f869fa 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -250,7 +250,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
 	else
 		ops = &vdpasim_config_ops;
 
-	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
+	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, 1,
 				    dev_attr->name, false);
 	if (IS_ERR(vdpasim)) {
 		ret = PTR_ERR(vdpasim);
@@ -399,6 +399,11 @@ static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
 	return VDPASIM_QUEUE_ALIGN;
 }
 
+static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
+{
+	return 0;
+}
+
 static u64 vdpasim_get_device_features(struct vdpa_device *vdpa)
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
@@ -620,6 +625,7 @@ static const struct vdpa_config_ops vdpasim_config_ops = {
 	.set_vq_state           = vdpasim_set_vq_state,
 	.get_vq_state           = vdpasim_get_vq_state,
 	.get_vq_align           = vdpasim_get_vq_align,
+	.get_vq_group           = vdpasim_get_vq_group,
 	.get_device_features    = vdpasim_get_device_features,
 	.set_driver_features    = vdpasim_set_driver_features,
 	.get_driver_features    = vdpasim_get_driver_features,
@@ -650,6 +656,7 @@ static const struct vdpa_config_ops vdpasim_batch_config_ops = {
 	.set_vq_state           = vdpasim_set_vq_state,
 	.get_vq_state           = vdpasim_get_vq_state,
 	.get_vq_align           = vdpasim_get_vq_align,
+	.get_vq_group           = vdpasim_get_vq_group,
 	.get_device_features    = vdpasim_get_device_features,
 	.set_driver_features    = vdpasim_set_driver_features,
 	.get_driver_features    = vdpasim_get_driver_features,
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h
index cd58e888bcf3..0be7c1e7ef80 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.h
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h
@@ -63,6 +63,7 @@ struct vdpasim {
 	u32 status;
 	u32 generation;
 	u64 features;
+	u32 groups;
 	/* spinlock to synchronize iommu table */
 	spinlock_t iommu_lock;
 };
diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
index 2de442ececae..026b7ad72ed7 100644
--- a/include/linux/vdpa.h
+++ b/include/linux/vdpa.h
@@ -85,6 +85,7 @@ struct vdpa_device {
 	bool use_va;
 	int nvqs;
 	struct vdpa_mgmt_dev *mdev;
+	unsigned int ngroups;
 };
 
 /**
@@ -172,6 +173,10 @@ struct vdpa_map_file {
  *				for the device
  *				@vdev: vdpa device
  *				Returns virtqueue algin requirement
+ * @get_vq_group:		Get the group id for a specific virtqueue
+ *				@vdev: vdpa device
+ *				@idx: virtqueue index
+ *				Returns u32: group id for this virtqueue
  * @get_device_features:	Get virtio features supported by the device
  *				@vdev: vdpa device
  *				Returns the virtio features support by the
@@ -282,6 +287,7 @@ struct vdpa_config_ops {
 
 	/* Device ops */
 	u32 (*get_vq_align)(struct vdpa_device *vdev);
+	u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx);
 	u64 (*get_device_features)(struct vdpa_device *vdev);
 	int (*set_driver_features)(struct vdpa_device *vdev, u64 features);
 	u64 (*get_driver_features)(struct vdpa_device *vdev);
@@ -314,6 +320,7 @@ struct vdpa_config_ops {
 
 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 					const struct vdpa_config_ops *config,
+					unsigned int ngroups,
 					size_t size, const char *name,
 					bool use_va);
 
@@ -324,17 +331,18 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
  * @member: the name of struct vdpa_device within the @dev_struct
  * @parent: the parent device
  * @config: the bus operations that is supported by this device
+ * @ngroups: the number of virtqueue groups supported by this device
  * @name: name of the vdpa device
  * @use_va: indicate whether virtual address must be used by this device
  *
  * Return allocated data structure or ERR_PTR upon error
  */
-#define vdpa_alloc_device(dev_struct, member, parent, config, name, use_va)   \
-			  container_of(__vdpa_alloc_device( \
-				       parent, config, \
+#define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, name, use_va)   \
+			  container_of((__vdpa_alloc_device( \
+				       parent, config, ngroups, \
 				       sizeof(dev_struct) + \
 				       BUILD_BUG_ON_ZERO(offsetof( \
-				       dev_struct, member)), name, use_va), \
+				       dev_struct, member)), name, use_va)), \
 				       dev_struct, member)
 
 int vdpa_register_device(struct vdpa_device *vdev, int nvqs);
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 06/19] vdpa: multiple address spaces support
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (4 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 05/19] vdpa: introduce virtqueue groups Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-03-03 19:39     ` Eugenio Perez Martin
  2022-02-24 21:22   ` [RFC PATCH v2 07/19] vdpa: introduce config operations for associating ASID to a virtqueue group Gautam Dawar
                     ` (13 subsequent siblings)
  19 siblings, 1 reply; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

This patches introduces the multiple address spaces support for vDPA
device. This idea is to identify a specific address space via an
dedicated identifier - ASID.

During vDPA device allocation, vDPA device driver needs to report the
number of address spaces supported by the device then the DMA mapping
ops of the vDPA device needs to be extended to support ASID.

This helps to isolate the environments for the virtqueue that will not
be assigned directly. E.g in the case of virtio-net, the control
virtqueue will not be assigned directly to guest.

As a start, simply claim 1 virtqueue groups and 1 address spaces for
all vDPA devices. And vhost-vDPA will simply reject the device with
more than 1 virtqueue groups or address spaces.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vdpa/ifcvf/ifcvf_main.c   |  2 +-
 drivers/vdpa/mlx5/net/mlx5_vnet.c |  5 +++--
 drivers/vdpa/vdpa.c               |  4 +++-
 drivers/vdpa/vdpa_sim/vdpa_sim.c  | 10 ++++++----
 drivers/vhost/vdpa.c              | 14 +++++++++-----
 include/linux/vdpa.h              | 28 +++++++++++++++++++---------
 6 files changed, 41 insertions(+), 22 deletions(-)

diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_main.c
index c815a2e62440..a4815c5612f9 100644
--- a/drivers/vdpa/ifcvf/ifcvf_main.c
+++ b/drivers/vdpa/ifcvf/ifcvf_main.c
@@ -513,7 +513,7 @@ static int ifcvf_vdpa_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
 	pdev = ifcvf_mgmt_dev->pdev;
 	dev = &pdev->dev;
 	adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa,
-				    dev, &ifc_vdpa_ops, 1, name, false);
+				    dev, &ifc_vdpa_ops, 1, 1, name, false);
 	if (IS_ERR(adapter)) {
 		IFCVF_ERR(pdev, "Failed to allocate vDPA structure");
 		return PTR_ERR(adapter);
diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c
index fcfc28460b72..a76417892ef3 100644
--- a/drivers/vdpa/mlx5/net/mlx5_vnet.c
+++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c
@@ -2282,7 +2282,8 @@ static u32 mlx5_vdpa_get_generation(struct vdpa_device *vdev)
 	return mvdev->generation;
 }
 
-static int mlx5_vdpa_set_map(struct vdpa_device *vdev, struct vhost_iotlb *iotlb)
+static int mlx5_vdpa_set_map(struct vdpa_device *vdev, unsigned int asid,
+			     struct vhost_iotlb *iotlb)
 {
 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
 	bool change_map;
@@ -2581,7 +2582,7 @@ static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
 	}
 
 	ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
-				 1, name, false);
+				 1, 1, name, false);
 	if (IS_ERR(ndev))
 		return PTR_ERR(ndev);
 
diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c
index a07bf0130559..1793dc12b208 100644
--- a/drivers/vdpa/vdpa.c
+++ b/drivers/vdpa/vdpa.c
@@ -160,6 +160,7 @@ static void vdpa_release_dev(struct device *d)
  * @parent: the parent device
  * @config: the bus operations that is supported by this device
  * @ngroups: number of groups supported by this device
+ * @nas: number of address spaces supported by this device
  * @size: size of the parent structure that contains private data
  * @name: name of the vdpa device; optional.
  * @use_va: indicate whether virtual address must be used by this device
@@ -172,7 +173,7 @@ static void vdpa_release_dev(struct device *d)
  */
 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 					const struct vdpa_config_ops *config,
-					unsigned int ngroups,
+					unsigned int ngroups, unsigned int nas,
 					size_t size, const char *name,
 					bool use_va)
 {
@@ -206,6 +207,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 	vdev->features_valid = false;
 	vdev->use_va = use_va;
 	vdev->ngroups = ngroups;
+	vdev->nas = nas;
 
 	if (name)
 		err = dev_set_name(&vdev->dev, "%s", name);
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index c98cb1f869fa..659e2e2e4b0c 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -251,7 +251,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
 		ops = &vdpasim_config_ops;
 
 	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, 1,
-				    dev_attr->name, false);
+				    1, dev_attr->name, false);
 	if (IS_ERR(vdpasim)) {
 		ret = PTR_ERR(vdpasim);
 		goto err_alloc;
@@ -539,7 +539,7 @@ static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
 	return range;
 }
 
-static int vdpasim_set_map(struct vdpa_device *vdpa,
+static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
 			   struct vhost_iotlb *iotlb)
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
@@ -566,7 +566,8 @@ static int vdpasim_set_map(struct vdpa_device *vdpa,
 	return ret;
 }
 
-static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
+static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
+			   u64 iova, u64 size,
 			   u64 pa, u32 perm, void *opaque)
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
@@ -580,7 +581,8 @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
 	return ret;
 }
 
-static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
+static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
+			     u64 iova, u64 size)
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 
diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 655ff7029401..6bf755f84d26 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -599,10 +599,10 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
 		return r;
 
 	if (ops->dma_map) {
-		r = ops->dma_map(vdpa, iova, size, pa, perm, opaque);
+		r = ops->dma_map(vdpa, 0, iova, size, pa, perm, opaque);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			r = ops->set_map(vdpa, iotlb);
+			r = ops->set_map(vdpa, 0, iotlb);
 	} else {
 		r = iommu_map(v->domain, iova, pa, size,
 			      perm_to_iommu_flags(perm));
@@ -628,10 +628,10 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
 	vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
 
 	if (ops->dma_map) {
-		ops->dma_unmap(vdpa, iova, size);
+		ops->dma_unmap(vdpa, 0, iova, size);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			ops->set_map(vdpa, iotlb);
+			ops->set_map(vdpa, 0, iotlb);
 	} else {
 		iommu_unmap(v->domain, iova, size);
 	}
@@ -863,7 +863,7 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
 		break;
 	case VHOST_IOTLB_BATCH_END:
 		if (v->in_batch && ops->set_map)
-			ops->set_map(vdpa, iotlb);
+			ops->set_map(vdpa, 0, iotlb);
 		v->in_batch = false;
 		break;
 	default:
@@ -1128,6 +1128,10 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
 	int minor;
 	int r;
 
+	/* Only support 1 address space and 1 groups */
+	if (vdpa->ngroups != 1 || vdpa->nas != 1)
+		return -EOPNOTSUPP;
+
 	v = kzalloc(sizeof(*v), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!v)
 		return -ENOMEM;
diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
index 026b7ad72ed7..de22ca1a8ef3 100644
--- a/include/linux/vdpa.h
+++ b/include/linux/vdpa.h
@@ -69,6 +69,8 @@ struct vdpa_mgmt_dev;
  * @cf_mutex: Protects get and set access to configuration layout.
  * @index: device index
  * @features_valid: were features initialized? for legacy guests
+ * @ngroups: the number of virtqueue groups
+ * @nas: the number of address spaces
  * @use_va: indicate whether virtual address must be used by this device
  * @nvqs: maximum number of supported virtqueues
  * @mdev: management device pointer; caller must setup when registering device as part
@@ -86,6 +88,7 @@ struct vdpa_device {
 	int nvqs;
 	struct vdpa_mgmt_dev *mdev;
 	unsigned int ngroups;
+	unsigned int nas;
 };
 
 /**
@@ -240,6 +243,7 @@ struct vdpa_map_file {
  *				Needed for device that using device
  *				specific DMA translation (on-chip IOMMU)
  *				@vdev: vdpa device
+ *				@asid: address space identifier
  *				@iotlb: vhost memory mapping to be
  *				used by the vDPA
  *				Returns integer: success (0) or error (< 0)
@@ -248,6 +252,7 @@ struct vdpa_map_file {
  *				specific DMA translation (on-chip IOMMU)
  *				and preferring incremental map.
  *				@vdev: vdpa device
+ *				@asid: address space identifier
  *				@iova: iova to be mapped
  *				@size: size of the area
  *				@pa: physical address for the map
@@ -259,6 +264,7 @@ struct vdpa_map_file {
  *				specific DMA translation (on-chip IOMMU)
  *				and preferring incremental unmap.
  *				@vdev: vdpa device
+ *				@asid: address space identifier
  *				@iova: iova to be unmapped
  *				@size: size of the area
  *				Returns integer: success (0) or error (< 0)
@@ -309,10 +315,12 @@ struct vdpa_config_ops {
 	struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
 
 	/* DMA ops */
-	int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
-	int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
-		       u64 pa, u32 perm, void *opaque);
-	int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
+	int (*set_map)(struct vdpa_device *vdev, unsigned int asid,
+		       struct vhost_iotlb *iotlb);
+	int (*dma_map)(struct vdpa_device *vdev, unsigned int asid,
+		       u64 iova, u64 size, u64 pa, u32 perm, void *opaque);
+	int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
+			 u64 iova, u64 size);
 
 	/* Free device resources */
 	void (*free)(struct vdpa_device *vdev);
@@ -320,7 +328,7 @@ struct vdpa_config_ops {
 
 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
 					const struct vdpa_config_ops *config,
-					unsigned int ngroups,
+					unsigned int ngroups, unsigned int nas,
 					size_t size, const char *name,
 					bool use_va);
 
@@ -332,17 +340,19 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
  * @parent: the parent device
  * @config: the bus operations that is supported by this device
  * @ngroups: the number of virtqueue groups supported by this device
+ * @nas: the number of address spaces
  * @name: name of the vdpa device
  * @use_va: indicate whether virtual address must be used by this device
  *
  * Return allocated data structure or ERR_PTR upon error
  */
-#define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, name, use_va)   \
+#define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, nas, \
+			  name, use_va) \
 			  container_of((__vdpa_alloc_device( \
-				       parent, config, ngroups, \
-				       sizeof(dev_struct) + \
+				       parent, config, ngroups, nas, \
+				       (sizeof(dev_struct) + \
 				       BUILD_BUG_ON_ZERO(offsetof( \
-				       dev_struct, member)), name, use_va)), \
+				       dev_struct, member))), name, use_va)), \
 				       dev_struct, member)
 
 int vdpa_register_device(struct vdpa_device *vdev, int nvqs);
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 07/19] vdpa: introduce config operations for associating ASID to a virtqueue group
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (5 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 06/19] vdpa: multiple address spaces support Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-03-04  9:54     ` Eugenio Perez Martin
  2022-02-24 21:22   ` [RFC PATCH v2 08/19] vhost_iotlb: split out IOTLB initialization Gautam Dawar
                     ` (12 subsequent siblings)
  19 siblings, 1 reply; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

This patch introduces a new bus operation to allow the vDPA bus driver
to associate an ASID to a virtqueue group.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 include/linux/vdpa.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
index de22ca1a8ef3..7386860c3995 100644
--- a/include/linux/vdpa.h
+++ b/include/linux/vdpa.h
@@ -239,6 +239,12 @@ struct vdpa_map_file {
  *				@vdev: vdpa device
  *				Returns the iova range supported by
  *				the device.
+ * @set_group_asid:		Set address space identifier for a
+ *				virtqueue group
+ *				@vdev: vdpa device
+ *				@group: virtqueue group
+ *				@asid: address space id for this group
+ *				Returns integer: success (0) or error (< 0)
  * @set_map:			Set device memory mapping (optional)
  *				Needed for device that using device
  *				specific DMA translation (on-chip IOMMU)
@@ -321,6 +327,10 @@ struct vdpa_config_ops {
 		       u64 iova, u64 size, u64 pa, u32 perm, void *opaque);
 	int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
 			 u64 iova, u64 size);
+	int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group,
+			      unsigned int asid);
+
+
 
 	/* Free device resources */
 	void (*free)(struct vdpa_device *vdev);
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 08/19] vhost_iotlb: split out IOTLB initialization
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (6 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 07/19] vdpa: introduce config operations for associating ASID to a virtqueue group Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-02-24 21:22   ` [RFC PATCH v2 09/19] vhost: support ASID in IOTLB API Gautam Dawar
                     ` (11 subsequent siblings)
  19 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

This patch splits out IOTLB initialization to make sure it could be
reused by external modules.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vhost/iotlb.c       | 23 ++++++++++++++++++-----
 include/linux/vhost_iotlb.h |  2 ++
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/drivers/vhost/iotlb.c b/drivers/vhost/iotlb.c
index 670d56c879e5..6239d0ad3853 100644
--- a/drivers/vhost/iotlb.c
+++ b/drivers/vhost/iotlb.c
@@ -110,6 +110,23 @@ void vhost_iotlb_del_range(struct vhost_iotlb *iotlb, u64 start, u64 last)
 }
 EXPORT_SYMBOL_GPL(vhost_iotlb_del_range);
 
+/**
+ * vhost_iotlb_init - initialize a vhost IOTLB
+ * @iotlb: the IOTLB that needs to be initialized
+ * @limit: maximum number of IOTLB entries
+ * @flags: VHOST_IOTLB_FLAG_XXX
+ */
+void vhost_iotlb_init(struct vhost_iotlb *iotlb, unsigned int limit,
+		      unsigned int flags)
+{
+	iotlb->root = RB_ROOT_CACHED;
+	iotlb->limit = limit;
+	iotlb->nmaps = 0;
+	iotlb->flags = flags;
+	INIT_LIST_HEAD(&iotlb->list);
+}
+EXPORT_SYMBOL_GPL(vhost_iotlb_init);
+
 /**
  * vhost_iotlb_alloc - add a new vhost IOTLB
  * @limit: maximum number of IOTLB entries
@@ -124,11 +141,7 @@ struct vhost_iotlb *vhost_iotlb_alloc(unsigned int limit, unsigned int flags)
 	if (!iotlb)
 		return NULL;
 
-	iotlb->root = RB_ROOT_CACHED;
-	iotlb->limit = limit;
-	iotlb->nmaps = 0;
-	iotlb->flags = flags;
-	INIT_LIST_HEAD(&iotlb->list);
+	vhost_iotlb_init(iotlb, limit, flags);
 
 	return iotlb;
 }
diff --git a/include/linux/vhost_iotlb.h b/include/linux/vhost_iotlb.h
index 2d0e2f52f938..e79a40838998 100644
--- a/include/linux/vhost_iotlb.h
+++ b/include/linux/vhost_iotlb.h
@@ -36,6 +36,8 @@ int vhost_iotlb_add_range(struct vhost_iotlb *iotlb, u64 start, u64 last,
 			  u64 addr, unsigned int perm);
 void vhost_iotlb_del_range(struct vhost_iotlb *iotlb, u64 start, u64 last);
 
+void vhost_iotlb_init(struct vhost_iotlb *iotlb, unsigned int limit,
+		      unsigned int flags);
 struct vhost_iotlb *vhost_iotlb_alloc(unsigned int limit, unsigned int flags);
 void vhost_iotlb_free(struct vhost_iotlb *iotlb);
 void vhost_iotlb_reset(struct vhost_iotlb *iotlb);
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 09/19] vhost: support ASID in IOTLB API
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (7 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 08/19] vhost_iotlb: split out IOTLB initialization Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-03-04 10:25     ` Eugenio Perez Martin
  2022-02-24 21:22   ` [RFC PATCH v2 10/19] vhost-vdpa: introduce asid based IOTLB Gautam Dawar
                     ` (10 subsequent siblings)
  19 siblings, 1 reply; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

This patches allows userspace to send ASID based IOTLB message to
vhost. This idea is to use the reserved u32 field in the existing V2
IOTLB message. Vhost device should advertise this capability via
VHOST_BACKEND_F_IOTLB_ASID backend feature.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vhost/vdpa.c             |  5 ++++-
 drivers/vhost/vhost.c            | 23 ++++++++++++++++++-----
 drivers/vhost/vhost.h            |  4 ++--
 include/uapi/linux/vhost_types.h |  6 +++++-
 4 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 6bf755f84d26..d0aacc0cc79a 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -836,7 +836,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
 				 msg->perm);
 }
 
-static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
+static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
 					struct vhost_iotlb_msg *msg)
 {
 	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
@@ -847,6 +847,9 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
 
 	mutex_lock(&dev->mutex);
 
+	if (asid != 0)
+		return -EINVAL;
+
 	r = vhost_dev_check_owner(dev);
 	if (r)
 		goto unlock;
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 59edb5a1ffe2..1f514d98f0de 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -468,7 +468,7 @@ void vhost_dev_init(struct vhost_dev *dev,
 		    struct vhost_virtqueue **vqs, int nvqs,
 		    int iov_limit, int weight, int byte_weight,
 		    bool use_worker,
-		    int (*msg_handler)(struct vhost_dev *dev,
+		    int (*msg_handler)(struct vhost_dev *dev, u32 asid,
 				       struct vhost_iotlb_msg *msg))
 {
 	struct vhost_virtqueue *vq;
@@ -1090,11 +1090,14 @@ static bool umem_access_ok(u64 uaddr, u64 size, int access)
 	return true;
 }
 
-static int vhost_process_iotlb_msg(struct vhost_dev *dev,
+static int vhost_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
 				   struct vhost_iotlb_msg *msg)
 {
 	int ret = 0;
 
+	if (asid != 0)
+		return -EINVAL;
+
 	mutex_lock(&dev->mutex);
 	vhost_dev_lock_vqs(dev);
 	switch (msg->type) {
@@ -1141,6 +1144,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 	struct vhost_iotlb_msg msg;
 	size_t offset;
 	int type, ret;
+	u32 asid = 0;
 
 	ret = copy_from_iter(&type, sizeof(type), from);
 	if (ret != sizeof(type)) {
@@ -1156,7 +1160,16 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 		offset = offsetof(struct vhost_msg, iotlb) - sizeof(int);
 		break;
 	case VHOST_IOTLB_MSG_V2:
-		offset = sizeof(__u32);
+		if (vhost_backend_has_feature(dev->vqs[0],
+					      VHOST_BACKEND_F_IOTLB_ASID)) {
+			ret = copy_from_iter(&asid, sizeof(asid), from);
+			if (ret != sizeof(asid)) {
+				ret = -EINVAL;
+				goto done;
+			}
+			offset = sizeof(__u16);
+		} else
+			offset = sizeof(__u32);
 		break;
 	default:
 		ret = -EINVAL;
@@ -1171,9 +1184,9 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 	}
 
 	if (dev->msg_handler)
-		ret = dev->msg_handler(dev, &msg);
+		ret = dev->msg_handler(dev, asid, &msg);
 	else
-		ret = vhost_process_iotlb_msg(dev, &msg);
+		ret = vhost_process_iotlb_msg(dev, asid, &msg);
 	if (ret) {
 		ret = -EFAULT;
 		goto done;
diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
index 638bb640d6b4..9f238d6c7b58 100644
--- a/drivers/vhost/vhost.h
+++ b/drivers/vhost/vhost.h
@@ -161,7 +161,7 @@ struct vhost_dev {
 	int byte_weight;
 	u64 kcov_handle;
 	bool use_worker;
-	int (*msg_handler)(struct vhost_dev *dev,
+	int (*msg_handler)(struct vhost_dev *dev, u32 asid,
 			   struct vhost_iotlb_msg *msg);
 };
 
@@ -169,7 +169,7 @@ bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
 void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
 		    int nvqs, int iov_limit, int weight, int byte_weight,
 		    bool use_worker,
-		    int (*msg_handler)(struct vhost_dev *dev,
+		    int (*msg_handler)(struct vhost_dev *dev, u32 asid,
 				       struct vhost_iotlb_msg *msg));
 long vhost_dev_set_owner(struct vhost_dev *dev);
 bool vhost_dev_has_owner(struct vhost_dev *dev);
diff --git a/include/uapi/linux/vhost_types.h b/include/uapi/linux/vhost_types.h
index 76ee7016c501..634cee485abb 100644
--- a/include/uapi/linux/vhost_types.h
+++ b/include/uapi/linux/vhost_types.h
@@ -87,7 +87,7 @@ struct vhost_msg {
 
 struct vhost_msg_v2 {
 	__u32 type;
-	__u32 reserved;
+	__u32 asid;
 	union {
 		struct vhost_iotlb_msg iotlb;
 		__u8 padding[64];
@@ -157,5 +157,9 @@ struct vhost_vdpa_iova_range {
 #define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1
 /* IOTLB can accept batching hints */
 #define VHOST_BACKEND_F_IOTLB_BATCH  0x2
+/* IOTLB can accept address space identifier through V2 type of IOTLB
+ * message
+ */
+#define VHOST_BACKEND_F_IOTLB_ASID  0x3
 
 #endif
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 10/19] vhost-vdpa: introduce asid based IOTLB
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (8 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 09/19] vhost: support ASID in IOTLB API Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-03-04 17:56     ` Eugenio Perez Martin
  2022-02-24 21:22   ` [RFC PATCH v2 11/19] vhost-vdpa: introduce uAPI to get the number of virtqueue groups Gautam Dawar
                     ` (9 subsequent siblings)
  19 siblings, 1 reply; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

This patch converts the vhost-vDPA device to support multiple IOTLBs
tagged via ASID via hlist. This will be used for supporting multiple
address spaces in the following patches.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vhost/vdpa.c | 104 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 79 insertions(+), 25 deletions(-)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index d0aacc0cc79a..4e8b7c4809cd 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -33,13 +33,21 @@ enum {
 
 #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
 
+#define VHOST_VDPA_IOTLB_BUCKETS 16
+
+struct vhost_vdpa_as {
+	struct hlist_node hash_link;
+	struct vhost_iotlb iotlb;
+	u32 id;
+};
+
 struct vhost_vdpa {
 	struct vhost_dev vdev;
 	struct iommu_domain *domain;
 	struct vhost_virtqueue *vqs;
 	struct completion completion;
 	struct vdpa_device *vdpa;
-	struct vhost_iotlb *iotlb;
+	struct hlist_head as[VHOST_VDPA_IOTLB_BUCKETS];
 	struct device dev;
 	struct cdev cdev;
 	atomic_t opened;
@@ -49,12 +57,64 @@ struct vhost_vdpa {
 	struct eventfd_ctx *config_ctx;
 	int in_batch;
 	struct vdpa_iova_range range;
+	int used_as;
 };
 
 static DEFINE_IDA(vhost_vdpa_ida);
 
 static dev_t vhost_vdpa_major;
 
+static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
+{
+	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
+	struct vhost_vdpa_as *as;
+
+	hlist_for_each_entry(as, head, hash_link)
+		if (as->id == asid)
+			return as;
+
+	return NULL;
+}
+
+static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
+{
+	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
+	struct vhost_vdpa_as *as;
+
+	if (asid_to_as(v, asid))
+		return NULL;
+
+	as = kmalloc(sizeof(*as), GFP_KERNEL);
+	if (!as)
+		return NULL;
+
+	vhost_iotlb_init(&as->iotlb, 0, 0);
+	as->id = asid;
+	hlist_add_head(&as->hash_link, head);
+	++v->used_as;
+
+	return as;
+}
+
+static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
+{
+	struct vhost_vdpa_as *as = asid_to_as(v, asid);
+
+	/* Remove default address space is not allowed */
+	if (asid == 0)
+		return -EINVAL;
+
+	if (!as)
+		return -EINVAL;
+
+	hlist_del(&as->hash_link);
+	vhost_iotlb_reset(&as->iotlb);
+	kfree(as);
+	--v->used_as;
+
+	return 0;
+}
+
 static void handle_vq_kick(struct vhost_work *work)
 {
 	struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
@@ -554,15 +614,6 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
 	return vhost_vdpa_pa_unmap(v, iotlb, start, last);
 }
 
-static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v)
-{
-	struct vhost_iotlb *iotlb = v->iotlb;
-
-	vhost_vdpa_iotlb_unmap(v, iotlb, 0ULL, 0ULL - 1);
-	kfree(v->iotlb);
-	v->iotlb = NULL;
-}
-
 static int perm_to_iommu_flags(u32 perm)
 {
 	int flags = 0;
@@ -842,7 +893,8 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
 	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
-	struct vhost_iotlb *iotlb = v->iotlb;
+	struct vhost_vdpa_as *as = asid_to_as(v, 0);
+	struct vhost_iotlb *iotlb = &as->iotlb;
 	int r = 0;
 
 	mutex_lock(&dev->mutex);
@@ -953,6 +1005,13 @@ static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v)
 	}
 }
 
+static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
+{
+	vhost_dev_cleanup(&v->vdev);
+	kfree(v->vdev.vqs);
+	vhost_vdpa_remove_as(v, 0);
+}
+
 static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 {
 	struct vhost_vdpa *v;
@@ -985,15 +1044,12 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 	vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
 		       vhost_vdpa_process_iotlb_msg);
 
-	v->iotlb = vhost_iotlb_alloc(0, 0);
-	if (!v->iotlb) {
-		r = -ENOMEM;
-		goto err_init_iotlb;
-	}
+	if (!vhost_vdpa_alloc_as(v, 0))
+		goto err_alloc_as;
 
 	r = vhost_vdpa_alloc_domain(v);
 	if (r)
-		goto err_alloc_domain;
+		goto err_alloc_as;
 
 	vhost_vdpa_set_iova_range(v);
 
@@ -1001,11 +1057,8 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 
 	return 0;
 
-err_alloc_domain:
-	vhost_vdpa_iotlb_free(v);
-err_init_iotlb:
-	vhost_dev_cleanup(&v->vdev);
-	kfree(vqs);
+err_alloc_as:
+	vhost_vdpa_cleanup(v);
 err:
 	atomic_dec(&v->opened);
 	return r;
@@ -1029,11 +1082,9 @@ static int vhost_vdpa_release(struct inode *inode, struct file *filep)
 	vhost_vdpa_clean_irq(v);
 	vhost_vdpa_reset(v);
 	vhost_dev_stop(&v->vdev);
-	vhost_vdpa_iotlb_free(v);
 	vhost_vdpa_free_domain(v);
 	vhost_vdpa_config_put(v);
 	vhost_dev_cleanup(&v->vdev);
-	kfree(v->vdev.vqs);
 	mutex_unlock(&d->mutex);
 
 	atomic_dec(&v->opened);
@@ -1129,7 +1180,7 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
 	const struct vdpa_config_ops *ops = vdpa->config;
 	struct vhost_vdpa *v;
 	int minor;
-	int r;
+	int i, r;
 
 	/* Only support 1 address space and 1 groups */
 	if (vdpa->ngroups != 1 || vdpa->nas != 1)
@@ -1177,6 +1228,9 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
 	init_completion(&v->completion);
 	vdpa_set_drvdata(vdpa, v);
 
+	for (i = 0; i < VHOST_VDPA_IOTLB_BUCKETS; i++)
+		INIT_HLIST_HEAD(&v->as[i]);
+
 	return 0;
 
 err:
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 11/19] vhost-vdpa: introduce uAPI to get the number of virtqueue groups
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (9 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 10/19] vhost-vdpa: introduce asid based IOTLB Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-02-24 21:22   ` [RFC PATCH v2 12/19] vhost-vdpa: introduce uAPI to get the number of address spaces Gautam Dawar
                     ` (8 subsequent siblings)
  19 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

Follows the vDPA support for multiple address spaces, this patch
introduce uAPI for the userspace to know the number of virtqueue
groups supported by the vDPA device.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vhost/vdpa.c       | 4 ++++
 include/uapi/linux/vhost.h | 3 +++
 2 files changed, 7 insertions(+)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 4e8b7c4809cd..7a8a99cef8a4 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -538,6 +538,10 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
 	case VHOST_VDPA_GET_VRING_NUM:
 		r = vhost_vdpa_get_vring_num(v, argp);
 		break;
+	case VHOST_VDPA_GET_GROUP_NUM:
+		r = copy_to_user(argp, &v->vdpa->ngroups,
+				 sizeof(v->vdpa->ngroups));
+		break;
 	case VHOST_SET_LOG_BASE:
 	case VHOST_SET_LOG_FD:
 		r = -ENOIOCTLCMD;
diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index 59c6c0fbaba1..8a4e6e426bbf 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -145,4 +145,7 @@
 /* Get the valid iova range */
 #define VHOST_VDPA_GET_IOVA_RANGE	_IOR(VHOST_VIRTIO, 0x78, \
 					     struct vhost_vdpa_iova_range)
+/* Get the number of virtqueue groups. */
+#define VHOST_VDPA_GET_GROUP_NUM	_IOR(VHOST_VIRTIO, 0x79, unsigned int)
+
 #endif
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 12/19] vhost-vdpa: introduce uAPI to get the number of address spaces
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (10 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 11/19] vhost-vdpa: introduce uAPI to get the number of virtqueue groups Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-02-24 21:22   ` [RFC PATCH v2 13/19] vhost-vdpa: uAPI to get virtqueue group id Gautam Dawar
                     ` (7 subsequent siblings)
  19 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

This patch introduces the uAPI for getting the number of address
spaces supported by this vDPA device.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vhost/vdpa.c       | 3 +++
 include/uapi/linux/vhost.h | 2 ++
 2 files changed, 5 insertions(+)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 7a8a99cef8a4..733b305c5029 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -542,6 +542,9 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
 		r = copy_to_user(argp, &v->vdpa->ngroups,
 				 sizeof(v->vdpa->ngroups));
 		break;
+	case VHOST_VDPA_GET_AS_NUM:
+		r = copy_to_user(argp, &v->vdpa->nas, sizeof(v->vdpa->nas));
+		break;
 	case VHOST_SET_LOG_BASE:
 	case VHOST_SET_LOG_FD:
 		r = -ENOIOCTLCMD;
diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index 8a4e6e426bbf..8762911a3cb8 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -148,4 +148,6 @@
 /* Get the number of virtqueue groups. */
 #define VHOST_VDPA_GET_GROUP_NUM	_IOR(VHOST_VIRTIO, 0x79, unsigned int)
 
+/* Get the number of address spaces. */
+#define VHOST_VDPA_GET_AS_NUM		_IOR(VHOST_VIRTIO, 0x7A, unsigned int)
 #endif
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 13/19] vhost-vdpa: uAPI to get virtqueue group id
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (11 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 12/19] vhost-vdpa: introduce uAPI to get the number of address spaces Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-02-24 21:22   ` [RFC PATCH v2 14/19] vhost-vdpa: introduce uAPI to set group ASID Gautam Dawar
                     ` (6 subsequent siblings)
  19 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

Follows the support for virtqueue group in vDPA. This patches
introduces uAPI to get the virtqueue group ID for a specific virtqueue
in vhost-vdpa.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vhost/vdpa.c       | 8 ++++++++
 include/uapi/linux/vhost.h | 8 ++++++++
 2 files changed, 16 insertions(+)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 733b305c5029..cfe57f0871a3 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -444,6 +444,14 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,
 			return -EFAULT;
 		ops->set_vq_ready(vdpa, idx, s.num);
 		return 0;
+	case VHOST_VDPA_GET_VRING_GROUP:
+		s.index = idx;
+		s.num = ops->get_vq_group(vdpa, idx);
+		if (s.num >= vdpa->ngroups)
+			return -EIO;
+		else if (copy_to_user(argp, &s, sizeof(s)))
+			return -EFAULT;
+		return 0;
 	case VHOST_GET_VRING_BASE:
 		r = ops->get_vq_state(v->vdpa, idx, &vq_state);
 		if (r)
diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index 8762911a3cb8..99de06476fdc 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -150,4 +150,12 @@
 
 /* Get the number of address spaces. */
 #define VHOST_VDPA_GET_AS_NUM		_IOR(VHOST_VIRTIO, 0x7A, unsigned int)
+
+/* Get the group for a virtqueue: read index, write group in num,
+ * The virtqueue index is stored in the index field of
+ * vhost_vring_state. The group for this specific virtqueue is
+ * returned via num field of vhost_vring_state.
+ */
+#define VHOST_VDPA_GET_VRING_GROUP	_IOWR(VHOST_VIRTIO, 0x7B,	\
+					      struct vhost_vring_state)
 #endif
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 14/19] vhost-vdpa: introduce uAPI to set group ASID
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (12 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 13/19] vhost-vdpa: uAPI to get virtqueue group id Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-02-24 21:22   ` [RFC PATCH v2 15/19] vhost-vdpa: support ASID based IOTLB API Gautam Dawar
                     ` (5 subsequent siblings)
  19 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

Follows the vDPA support for associating ASID to a specific virtqueue
group. This patch adds a uAPI to support setting them from userspace.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vhost/vdpa.c       | 8 ++++++++
 include/uapi/linux/vhost.h | 7 +++++++
 2 files changed, 15 insertions(+)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index cfe57f0871a3..47e6cf9d0881 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -452,6 +452,14 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,
 		else if (copy_to_user(argp, &s, sizeof(s)))
 			return -EFAULT;
 		return 0;
+	case VHOST_VDPA_SET_GROUP_ASID:
+		if (copy_from_user(&s, argp, sizeof(s)))
+			return -EFAULT;
+		if (s.num >= vdpa->nas)
+			return -EINVAL;
+		if (!ops->set_group_asid)
+			return -EOPNOTSUPP;
+		return ops->set_group_asid(vdpa, idx, s.num);
 	case VHOST_GET_VRING_BASE:
 		r = ops->get_vq_state(v->vdpa, idx, &vq_state);
 		if (r)
diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index 99de06476fdc..5e083490f1aa 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h
@@ -158,4 +158,11 @@
  */
 #define VHOST_VDPA_GET_VRING_GROUP	_IOWR(VHOST_VIRTIO, 0x7B,	\
 					      struct vhost_vring_state)
+/* Set the ASID for a virtqueue group. The group index is stored in
+ * the index field of vhost_vring_state, the ASID associated with this
+ * group is stored at num field of vhost_vring_state.
+ */
+#define VHOST_VDPA_SET_GROUP_ASID	_IOW(VHOST_VIRTIO, 0x7C, \
+					     struct vhost_vring_state)
+
 #endif
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 15/19] vhost-vdpa: support ASID based IOTLB API
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (13 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 14/19] vhost-vdpa: introduce uAPI to set group ASID Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-03-04 18:04     ` Eugenio Perez Martin
  2022-02-24 21:22   ` [RFC PATCH v2 16/19] vdpa_sim: advertise VIRTIO_NET_F_MTU Gautam Dawar
                     ` (4 subsequent siblings)
  19 siblings, 1 reply; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

This patch extends the vhost-vdpa to support ASID based IOTLB API. The
vhost-vdpa device will allocated multiple IOTLBs for vDPA device that
supports multiple address spaces. The IOTLBs and vDPA device memory
mappings is determined and maintained through ASID.

Note that we still don't support vDPA device with more than one
address spaces that depends on platform IOMMU. This work will be done
by moving the IOMMU logic from vhost-vDPA to vDPA device driver.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vhost/vdpa.c  | 129 ++++++++++++++++++++++++++++++++----------
 drivers/vhost/vhost.c |   2 +-
 2 files changed, 100 insertions(+), 31 deletions(-)

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index 47e6cf9d0881..4bcf824e3b12 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -28,7 +28,8 @@
 enum {
 	VHOST_VDPA_BACKEND_FEATURES =
 	(1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) |
-	(1ULL << VHOST_BACKEND_F_IOTLB_BATCH),
+	(1ULL << VHOST_BACKEND_F_IOTLB_BATCH) |
+	(1ULL << VHOST_BACKEND_F_IOTLB_ASID),
 };
 
 #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
@@ -57,13 +58,20 @@ struct vhost_vdpa {
 	struct eventfd_ctx *config_ctx;
 	int in_batch;
 	struct vdpa_iova_range range;
-	int used_as;
+	u32 batch_asid;
 };
 
 static DEFINE_IDA(vhost_vdpa_ida);
 
 static dev_t vhost_vdpa_major;
 
+static inline u32 iotlb_to_asid(struct vhost_iotlb *iotlb)
+{
+	struct vhost_vdpa_as *as = container_of(iotlb, struct
+						vhost_vdpa_as, iotlb);
+	return as->id;
+}
+
 static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
 {
 	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
@@ -76,6 +84,16 @@ static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
 	return NULL;
 }
 
+static struct vhost_iotlb *asid_to_iotlb(struct vhost_vdpa *v, u32 asid)
+{
+	struct vhost_vdpa_as *as = asid_to_as(v, asid);
+
+	if (!as)
+		return NULL;
+
+	return &as->iotlb;
+}
+
 static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
 {
 	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
@@ -84,6 +102,9 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
 	if (asid_to_as(v, asid))
 		return NULL;
 
+	if (asid >= v->vdpa->nas)
+		return NULL;
+
 	as = kmalloc(sizeof(*as), GFP_KERNEL);
 	if (!as)
 		return NULL;
@@ -91,18 +112,24 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
 	vhost_iotlb_init(&as->iotlb, 0, 0);
 	as->id = asid;
 	hlist_add_head(&as->hash_link, head);
-	++v->used_as;
 
 	return as;
 }
 
-static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
+static struct vhost_vdpa_as *vhost_vdpa_find_alloc_as(struct vhost_vdpa *v,
+						      u32 asid)
 {
 	struct vhost_vdpa_as *as = asid_to_as(v, asid);
 
-	/* Remove default address space is not allowed */
-	if (asid == 0)
-		return -EINVAL;
+	if (as)
+		return as;
+
+	return vhost_vdpa_alloc_as(v, asid);
+}
+
+static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
+{
+	struct vhost_vdpa_as *as = asid_to_as(v, asid);
 
 	if (!as)
 		return -EINVAL;
@@ -110,7 +137,6 @@ static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
 	hlist_del(&as->hash_link);
 	vhost_iotlb_reset(&as->iotlb);
 	kfree(as);
-	--v->used_as;
 
 	return 0;
 }
@@ -665,6 +691,7 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
 	struct vhost_dev *dev = &v->vdev;
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
+	u32 asid = iotlb_to_asid(iotlb);
 	int r = 0;
 
 	r = vhost_iotlb_add_range_ctx(iotlb, iova, iova + size - 1,
@@ -673,10 +700,10 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
 		return r;
 
 	if (ops->dma_map) {
-		r = ops->dma_map(vdpa, 0, iova, size, pa, perm, opaque);
+		r = ops->dma_map(vdpa, asid, iova, size, pa, perm, opaque);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			r = ops->set_map(vdpa, 0, iotlb);
+			r = ops->set_map(vdpa, asid, iotlb);
 	} else {
 		r = iommu_map(v->domain, iova, pa, size,
 			      perm_to_iommu_flags(perm));
@@ -692,23 +719,35 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
 	return 0;
 }
 
-static void vhost_vdpa_unmap(struct vhost_vdpa *v,
-			     struct vhost_iotlb *iotlb,
-			     u64 iova, u64 size)
+static int vhost_vdpa_unmap(struct vhost_vdpa *v,
+			    struct vhost_iotlb *iotlb,
+			    u64 iova, u64 size)
 {
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
+	u32 asid = iotlb_to_asid(iotlb);
+
+	if (!iotlb)
+		return -EINVAL;
 
 	vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
 
 	if (ops->dma_map) {
-		ops->dma_unmap(vdpa, 0, iova, size);
+		ops->dma_unmap(vdpa, asid, iova, size);
 	} else if (ops->set_map) {
 		if (!v->in_batch)
-			ops->set_map(vdpa, 0, iotlb);
+			ops->set_map(vdpa, asid, iotlb);
 	} else {
 		iommu_unmap(v->domain, iova, size);
 	}
+
+	/* If we are in the middle of batch processing, delay the free
+	 * of AS until BATCH_END.
+	 */
+	if (!v->in_batch && !iotlb->nmaps)
+		vhost_vdpa_remove_as(v, asid);
+
+	return 0;
 }
 
 static int vhost_vdpa_va_map(struct vhost_vdpa *v,
@@ -916,33 +955,55 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
 	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
-	struct vhost_vdpa_as *as = asid_to_as(v, 0);
-	struct vhost_iotlb *iotlb = &as->iotlb;
+	struct vhost_iotlb *iotlb = NULL;
+	struct vhost_vdpa_as *as = NULL;
 	int r = 0;
 
 	mutex_lock(&dev->mutex);
 
-	if (asid != 0)
-		return -EINVAL;
-
 	r = vhost_dev_check_owner(dev);
 	if (r)
 		goto unlock;
 
+	if (msg->type == VHOST_IOTLB_UPDATE ||
+	    msg->type == VHOST_IOTLB_BATCH_BEGIN) {
+		as = vhost_vdpa_find_alloc_as(v, asid);
+		if (!as) {
+			dev_err(&v->dev, "can't find and alloc asid %d\n",
+				asid);
+			return -EINVAL;
+		}
+		iotlb = &as->iotlb;
+	} else
+		iotlb = asid_to_iotlb(v, asid);
+
+	if ((v->in_batch && v->batch_asid != asid) || !iotlb) {
+		if (v->in_batch && v->batch_asid != asid) {
+			dev_info(&v->dev, "batch id %d asid %d\n",
+				 v->batch_asid, asid);
+		}
+		if (!iotlb)
+			dev_err(&v->dev, "no iotlb for asid %d\n", asid);
+		return -EINVAL;
+	}
+
 	switch (msg->type) {
 	case VHOST_IOTLB_UPDATE:
 		r = vhost_vdpa_process_iotlb_update(v, iotlb, msg);
 		break;
 	case VHOST_IOTLB_INVALIDATE:
-		vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
+		r = vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
 		break;
 	case VHOST_IOTLB_BATCH_BEGIN:
+		v->batch_asid = asid;
 		v->in_batch = true;
 		break;
 	case VHOST_IOTLB_BATCH_END:
 		if (v->in_batch && ops->set_map)
-			ops->set_map(vdpa, 0, iotlb);
+			ops->set_map(vdpa, asid, iotlb);
 		v->in_batch = false;
+		if (!iotlb->nmaps)
+			vhost_vdpa_remove_as(v, asid);
 		break;
 	default:
 		r = -EINVAL;
@@ -1030,9 +1091,17 @@ static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v)
 
 static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
 {
+	struct vhost_vdpa_as *as;
+	u32 asid;
+
 	vhost_dev_cleanup(&v->vdev);
 	kfree(v->vdev.vqs);
-	vhost_vdpa_remove_as(v, 0);
+
+	for (asid = 0; asid < v->vdpa->nas; asid++) {
+		as = asid_to_as(v, asid);
+		if (as)
+			vhost_vdpa_remove_as(v, asid);
+	}
 }
 
 static int vhost_vdpa_open(struct inode *inode, struct file *filep)
@@ -1067,12 +1136,9 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 	vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
 		       vhost_vdpa_process_iotlb_msg);
 
-	if (!vhost_vdpa_alloc_as(v, 0))
-		goto err_alloc_as;
-
 	r = vhost_vdpa_alloc_domain(v);
 	if (r)
-		goto err_alloc_as;
+		goto err_alloc_domain;
 
 	vhost_vdpa_set_iova_range(v);
 
@@ -1080,7 +1146,7 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
 
 	return 0;
 
-err_alloc_as:
+err_alloc_domain:
 	vhost_vdpa_cleanup(v);
 err:
 	atomic_dec(&v->opened);
@@ -1205,8 +1271,11 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
 	int minor;
 	int i, r;
 
-	/* Only support 1 address space and 1 groups */
-	if (vdpa->ngroups != 1 || vdpa->nas != 1)
+	/* We can't support platform IOMMU device with more than 1
+	 * group or as
+	 */
+	if (!ops->set_map && !ops->dma_map &&
+	    (vdpa->ngroups > 1 || vdpa->nas > 1))
 		return -EOPNOTSUPP;
 
 	v = kzalloc(sizeof(*v), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 1f514d98f0de..92eeb684c84d 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -1167,7 +1167,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 				ret = -EINVAL;
 				goto done;
 			}
-			offset = sizeof(__u16);
+			offset = 0;
 		} else
 			offset = sizeof(__u32);
 		break;
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 16/19] vdpa_sim: advertise VIRTIO_NET_F_MTU
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (14 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 15/19] vhost-vdpa: support ASID based IOTLB API Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-03-10 17:55     ` Eugenio Perez Martin
  2022-02-24 21:22   ` [RFC PATCH v2 17/19] vdpa_sim: factor out buffer completion logic Gautam Dawar
                     ` (3 subsequent siblings)
  19 siblings, 1 reply; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

We've already reported maximum mtu via config space, so let's
advertise the feature.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
index d5324f6fd8c7..ff22cc56f40b 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
@@ -26,7 +26,8 @@
 #define DRV_LICENSE  "GPL v2"
 
 #define VDPASIM_NET_FEATURES	(VDPASIM_FEATURES | \
-				 (1ULL << VIRTIO_NET_F_MAC))
+				 (1ULL << VIRTIO_NET_F_MAC) | \
+				 (1ULL << VIRTIO_NET_F_MTU));
 
 #define VDPASIM_NET_VQ_NUM	2
 
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 17/19] vdpa_sim: factor out buffer completion logic
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (15 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 16/19] vdpa_sim: advertise VIRTIO_NET_F_MTU Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-02-24 21:22   ` [RFC PATCH v2 18/19] vdpa_sim: filter destination mac address Gautam Dawar
                     ` (2 subsequent siblings)
  19 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

Wrap up common buffer completion logic in to vdpasim_net_complete

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 33 +++++++++++++++-------------
 1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
index ff22cc56f40b..05d552cb7f94 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
@@ -31,6 +31,22 @@
 
 #define VDPASIM_NET_VQ_NUM	2
 
+static void vdpasim_net_complete(struct vdpasim_virtqueue *vq, size_t len)
+{
+	/* Make sure data is wrote before advancing index */
+	smp_wmb();
+
+	vringh_complete_iotlb(&vq->vring, vq->head, len);
+
+	/* Make sure used is visible before rasing the interrupt. */
+	smp_wmb();
+
+	local_bh_disable();
+	if (vringh_need_notify_iotlb(&vq->vring) > 0)
+		vringh_notify(&vq->vring);
+	local_bh_enable();
+}
+
 static void vdpasim_net_work(struct work_struct *work)
 {
 	struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
@@ -78,21 +94,8 @@ static void vdpasim_net_work(struct work_struct *work)
 			total_write += write;
 		}
 
-		/* Make sure data is wrote before advancing index */
-		smp_wmb();
-
-		vringh_complete_iotlb(&txq->vring, txq->head, 0);
-		vringh_complete_iotlb(&rxq->vring, rxq->head, total_write);
-
-		/* Make sure used is visible before rasing the interrupt. */
-		smp_wmb();
-
-		local_bh_disable();
-		if (vringh_need_notify_iotlb(&txq->vring) > 0)
-			vringh_notify(&txq->vring);
-		if (vringh_need_notify_iotlb(&rxq->vring) > 0)
-			vringh_notify(&rxq->vring);
-		local_bh_enable();
+		vdpasim_net_complete(txq, 0);
+		vdpasim_net_complete(rxq, total_write);
 
 		if (++pkts > 4) {
 			schedule_work(&vdpasim->work);
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 18/19] vdpa_sim: filter destination mac address
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (16 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 17/19] vdpa_sim: factor out buffer completion logic Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-03-10 18:22     ` Eugenio Perez Martin
  2022-02-24 21:22   ` [RFC PATCH v2 19/19] vdpasim: control virtqueue support Gautam Dawar
  2022-02-28  8:17     ` Jason Wang
  19 siblings, 1 reply; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

This patch implements a simple unicast filter for vDPA simulator.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 49 ++++++++++++++++++----------
 1 file changed, 31 insertions(+), 18 deletions(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
index 05d552cb7f94..ed5ade4ae570 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
@@ -47,13 +47,28 @@ static void vdpasim_net_complete(struct vdpasim_virtqueue *vq, size_t len)
 	local_bh_enable();
 }
 
+static bool receive_filter(struct vdpasim *vdpasim, size_t len)
+{
+	bool modern = vdpasim->features & (1ULL << VIRTIO_F_VERSION_1);
+	size_t hdr_len = modern ? sizeof(struct virtio_net_hdr_v1) :
+				  sizeof(struct virtio_net_hdr);
+
+	if (len < ETH_ALEN + hdr_len)
+		return false;
+
+	if (!strncmp(vdpasim->buffer + hdr_len,
+		     vdpasim->config.mac, ETH_ALEN))
+		return true;
+
+	return false;
+}
+
 static void vdpasim_net_work(struct work_struct *work)
 {
 	struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
 	struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
 	struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
 	ssize_t read, write;
-	size_t total_write;
 	int pkts = 0;
 	int err;
 
@@ -66,36 +81,34 @@ static void vdpasim_net_work(struct work_struct *work)
 		goto out;
 
 	while (true) {
-		total_write = 0;
 		err = vringh_getdesc_iotlb(&txq->vring, &txq->out_iov, NULL,
 					   &txq->head, GFP_ATOMIC);
 		if (err <= 0)
 			break;
 
+		read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
+					     vdpasim->buffer,
+					     PAGE_SIZE);
+
+		if (!receive_filter(vdpasim, read)) {
+			vdpasim_complete(txq, 0);
+			continue;
+		}
+
 		err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->in_iov,
 					   &rxq->head, GFP_ATOMIC);
 		if (err <= 0) {
-			vringh_complete_iotlb(&txq->vring, txq->head, 0);
+			vdpasim_net_complete(txq, 0);
 			break;
 		}
 
-		while (true) {
-			read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
-						     vdpasim->buffer,
-						     PAGE_SIZE);
-			if (read <= 0)
-				break;
-
-			write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
-						      vdpasim->buffer, read);
-			if (write <= 0)
-				break;
-
-			total_write += write;
-		}
+		write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
+					      vdpasim->buffer, read);
+		if (write <= 0)
+			break;
 
 		vdpasim_net_complete(txq, 0);
-		vdpasim_net_complete(rxq, total_write);
+		vdpasim_net_complete(rxq, write);
 
 		if (++pkts > 4) {
 			schedule_work(&vdpasim->work);
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* [RFC PATCH v2 19/19] vdpasim: control virtqueue support
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
                     ` (17 preceding siblings ...)
  2022-02-24 21:22   ` [RFC PATCH v2 18/19] vdpa_sim: filter destination mac address Gautam Dawar
@ 2022-02-24 21:22   ` Gautam Dawar
  2022-03-10 18:20     ` Eugenio Perez Martin
  2022-03-18  7:35     ` Eugenio Perez Martin
  2022-02-28  8:17     ` Jason Wang
  19 siblings, 2 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-02-24 21:22 UTC (permalink / raw)
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm, netdev

This patch introduces the control virtqueue support for vDPA
simulator. This is a requirement for supporting advanced features like
multiqueue.

A requirement for control virtqueue is to isolate its memory access
from the rx/tx virtqueues. This is because when using vDPA device
for VM, the control virqueue is not directly assigned to VM. Userspace
(Qemu) will present a shadow control virtqueue to control for
recording the device states.

The isolation is done via the virtqueue groups and ASID support in
vDPA through vhost-vdpa. The simulator is extended to have:

1) three virtqueues: RXVQ, TXVQ and CVQ (control virtqueue)
2) two virtqueue groups: group 0 contains RXVQ and TXVQ; group 1
   contains CVQ
3) two address spaces and the simulator simply implements the address
   spaces by mapping it 1:1 to IOTLB.

For the VM use cases, userspace(Qemu) may set AS 0 to group 0 and AS 1
to group 1. So we have:

1) The IOTLB for virtqueue group 0 contains the mappings of guest, so
   RX and TX can be assigned to guest directly.
2) The IOTLB for virtqueue group 1 contains the mappings of CVQ which
   is the buffers that allocated and managed by VMM only. So CVQ of
   vhost-vdpa is visible to VMM only. And Guest can not access the CVQ
   of vhost-vdpa.

For the other use cases, since AS 0 is associated to all virtqueue
groups by default. All virtqueues share the same mapping by default.

To demonstrate the function, VIRITO_NET_F_CTRL_MACADDR is
implemented in the simulator for the driver to set mac address.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
---
 drivers/vdpa/vdpa_sim/vdpa_sim.c     | 91 ++++++++++++++++++++++------
 drivers/vdpa/vdpa_sim/vdpa_sim.h     |  2 +
 drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 88 ++++++++++++++++++++++++++-
 3 files changed, 161 insertions(+), 20 deletions(-)

diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index 659e2e2e4b0c..59611f18a3a8 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -96,11 +96,17 @@ static void vdpasim_do_reset(struct vdpasim *vdpasim)
 {
 	int i;
 
-	for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
+	spin_lock(&vdpasim->iommu_lock);
+
+	for (i = 0; i < vdpasim->dev_attr.nvqs; i++) {
 		vdpasim_vq_reset(vdpasim, &vdpasim->vqs[i]);
+		vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
+				 &vdpasim->iommu_lock);
+	}
+
+	for (i = 0; i < vdpasim->dev_attr.nas; i++)
+		vhost_iotlb_reset(&vdpasim->iommu[i]);
 
-	spin_lock(&vdpasim->iommu_lock);
-	vhost_iotlb_reset(vdpasim->iommu);
 	spin_unlock(&vdpasim->iommu_lock);
 
 	vdpasim->features = 0;
@@ -145,7 +151,7 @@ static dma_addr_t vdpasim_map_range(struct vdpasim *vdpasim, phys_addr_t paddr,
 	dma_addr = iova_dma_addr(&vdpasim->iova, iova);
 
 	spin_lock(&vdpasim->iommu_lock);
-	ret = vhost_iotlb_add_range(vdpasim->iommu, (u64)dma_addr,
+	ret = vhost_iotlb_add_range(&vdpasim->iommu[0], (u64)dma_addr,
 				    (u64)dma_addr + size - 1, (u64)paddr, perm);
 	spin_unlock(&vdpasim->iommu_lock);
 
@@ -161,7 +167,7 @@ static void vdpasim_unmap_range(struct vdpasim *vdpasim, dma_addr_t dma_addr,
 				size_t size)
 {
 	spin_lock(&vdpasim->iommu_lock);
-	vhost_iotlb_del_range(vdpasim->iommu, (u64)dma_addr,
+	vhost_iotlb_del_range(&vdpasim->iommu[0], (u64)dma_addr,
 			      (u64)dma_addr + size - 1);
 	spin_unlock(&vdpasim->iommu_lock);
 
@@ -250,8 +256,9 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
 	else
 		ops = &vdpasim_config_ops;
 
-	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, 1,
-				    1, dev_attr->name, false);
+	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
+				    dev_attr->ngroups, dev_attr->nas,
+				    dev_attr->name, false);
 	if (IS_ERR(vdpasim)) {
 		ret = PTR_ERR(vdpasim);
 		goto err_alloc;
@@ -278,16 +285,20 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
 	if (!vdpasim->vqs)
 		goto err_iommu;
 
-	vdpasim->iommu = vhost_iotlb_alloc(max_iotlb_entries, 0);
+	vdpasim->iommu = kmalloc_array(vdpasim->dev_attr.nas,
+				       sizeof(*vdpasim->iommu), GFP_KERNEL);
 	if (!vdpasim->iommu)
 		goto err_iommu;
 
+	for (i = 0; i < vdpasim->dev_attr.nas; i++)
+		vhost_iotlb_init(&vdpasim->iommu[i], 0, 0);
+
 	vdpasim->buffer = kvmalloc(dev_attr->buffer_size, GFP_KERNEL);
 	if (!vdpasim->buffer)
 		goto err_iommu;
 
 	for (i = 0; i < dev_attr->nvqs; i++)
-		vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu,
+		vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
 				 &vdpasim->iommu_lock);
 
 	ret = iova_cache_get();
@@ -401,7 +412,11 @@ static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
 
 static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
 {
-	return 0;
+	/* RX and TX belongs to group 0, CVQ belongs to group 1 */
+	if (idx == 2)
+		return 1;
+	else
+		return 0;
 }
 
 static u64 vdpasim_get_device_features(struct vdpa_device *vdpa)
@@ -539,20 +554,53 @@ static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
 	return range;
 }
 
+static int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
+				  unsigned int asid)
+{
+	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
+	struct vhost_iotlb *iommu;
+	int i;
+
+	if (group > vdpasim->dev_attr.ngroups)
+		return -EINVAL;
+
+	if (asid > vdpasim->dev_attr.nas)
+		return -EINVAL;
+
+	iommu = &vdpasim->iommu[asid];
+
+	spin_lock(&vdpasim->lock);
+
+	for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
+		if (vdpasim_get_vq_group(vdpa, i) == group)
+			vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
+					 &vdpasim->iommu_lock);
+
+	spin_unlock(&vdpasim->lock);
+
+	return 0;
+}
+
 static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
 			   struct vhost_iotlb *iotlb)
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 	struct vhost_iotlb_map *map;
+	struct vhost_iotlb *iommu;
 	u64 start = 0ULL, last = 0ULL - 1;
 	int ret;
 
+	if (asid >= vdpasim->dev_attr.nas)
+		return -EINVAL;
+
 	spin_lock(&vdpasim->iommu_lock);
-	vhost_iotlb_reset(vdpasim->iommu);
+
+	iommu = &vdpasim->iommu[asid];
+	vhost_iotlb_reset(iommu);
 
 	for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
 	     map = vhost_iotlb_itree_next(map, start, last)) {
-		ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
+		ret = vhost_iotlb_add_range(iommu, map->start,
 					    map->last, map->addr, map->perm);
 		if (ret)
 			goto err;
@@ -561,7 +609,7 @@ static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
 	return 0;
 
 err:
-	vhost_iotlb_reset(vdpasim->iommu);
+	vhost_iotlb_reset(iommu);
 	spin_unlock(&vdpasim->iommu_lock);
 	return ret;
 }
@@ -573,9 +621,12 @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 	int ret;
 
+	if (asid >= vdpasim->dev_attr.nas)
+		return -EINVAL;
+
 	spin_lock(&vdpasim->iommu_lock);
-	ret = vhost_iotlb_add_range_ctx(vdpasim->iommu, iova, iova + size - 1,
-					pa, perm, opaque);
+	ret = vhost_iotlb_add_range_ctx(&vdpasim->iommu[asid], iova,
+					iova + size - 1, pa, perm, opaque);
 	spin_unlock(&vdpasim->iommu_lock);
 
 	return ret;
@@ -586,8 +637,11 @@ static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
 {
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 
+	if (asid >= vdpasim->dev_attr.nas)
+		return -EINVAL;
+
 	spin_lock(&vdpasim->iommu_lock);
-	vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
+	vhost_iotlb_del_range(&vdpasim->iommu[asid], iova, iova + size - 1);
 	spin_unlock(&vdpasim->iommu_lock);
 
 	return 0;
@@ -611,8 +665,7 @@ static void vdpasim_free(struct vdpa_device *vdpa)
 	}
 
 	kvfree(vdpasim->buffer);
-	if (vdpasim->iommu)
-		vhost_iotlb_free(vdpasim->iommu);
+	vhost_iotlb_free(vdpasim->iommu);
 	kfree(vdpasim->vqs);
 	kfree(vdpasim->config);
 }
@@ -643,6 +696,7 @@ static const struct vdpa_config_ops vdpasim_config_ops = {
 	.set_config             = vdpasim_set_config,
 	.get_generation         = vdpasim_get_generation,
 	.get_iova_range         = vdpasim_get_iova_range,
+	.set_group_asid         = vdpasim_set_group_asid,
 	.dma_map                = vdpasim_dma_map,
 	.dma_unmap              = vdpasim_dma_unmap,
 	.free                   = vdpasim_free,
@@ -674,6 +728,7 @@ static const struct vdpa_config_ops vdpasim_batch_config_ops = {
 	.set_config             = vdpasim_set_config,
 	.get_generation         = vdpasim_get_generation,
 	.get_iova_range         = vdpasim_get_iova_range,
+	.set_group_asid         = vdpasim_set_group_asid,
 	.set_map                = vdpasim_set_map,
 	.free                   = vdpasim_free,
 };
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h
index 0be7c1e7ef80..622782e92239 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.h
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h
@@ -41,6 +41,8 @@ struct vdpasim_dev_attr {
 	size_t buffer_size;
 	int nvqs;
 	u32 id;
+	u32 ngroups;
+	u32 nas;
 
 	work_func_t work_fn;
 	void (*get_config)(struct vdpasim *vdpasim, void *config);
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
index ed5ade4ae570..513970c05af2 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
@@ -26,10 +26,15 @@
 #define DRV_LICENSE  "GPL v2"
 
 #define VDPASIM_NET_FEATURES	(VDPASIM_FEATURES | \
+				 (1ULL << VIRTIO_NET_F_MTU) | \
 				 (1ULL << VIRTIO_NET_F_MAC) | \
-				 (1ULL << VIRTIO_NET_F_MTU));
+				 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
+				 (1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR));
 
-#define VDPASIM_NET_VQ_NUM	2
+/* 3 virtqueues, 2 address spaces, 2 virtqueue groups */
+#define VDPASIM_NET_VQ_NUM	3
+#define VDPASIM_NET_AS_NUM	2
+#define VDPASIM_NET_GROUP_NUM	2
 
 static void vdpasim_net_complete(struct vdpasim_virtqueue *vq, size_t len)
 {
@@ -63,6 +68,81 @@ static bool receive_filter(struct vdpasim *vdpasim, size_t len)
 	return false;
 }
 
+static virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
+						   u8 cmd)
+{
+	struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
+	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
+	size_t read;
+
+	switch (cmd) {
+	case VIRTIO_NET_CTRL_MAC_ADDR_SET:
+		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov,
+					     (void *)vdpasim->config.mac,
+					     ETH_ALEN);
+		if (read == ETH_ALEN)
+			status = VIRTIO_NET_OK;
+		break;
+	default:
+		break;
+	}
+
+	return status;
+}
+
+static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
+{
+	struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
+	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
+	struct virtio_net_ctrl_hdr ctrl;
+	size_t read, write;
+	int err;
+
+	if (!(vdpasim->features & (1ULL << VIRTIO_NET_F_CTRL_VQ)))
+		return;
+
+	if (!cvq->ready)
+		return;
+
+	while (true) {
+		err = vringh_getdesc_iotlb(&cvq->vring, &cvq->in_iov,
+					   &cvq->out_iov,
+					   &cvq->head, GFP_ATOMIC);
+		if (err <= 0)
+			break;
+
+		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov, &ctrl,
+					     sizeof(ctrl));
+		if (read != sizeof(ctrl))
+			break;
+
+		switch (ctrl.class) {
+		case VIRTIO_NET_CTRL_MAC:
+			status = vdpasim_handle_ctrl_mac(vdpasim, ctrl.cmd);
+			break;
+		default:
+			break;
+		}
+
+		/* Make sure data is wrote before advancing index */
+		smp_wmb();
+
+		write = vringh_iov_push_iotlb(&cvq->vring, &cvq->out_iov,
+					      &status, sizeof(status));
+		vringh_complete_iotlb(&cvq->vring, cvq->head, write);
+		vringh_kiov_cleanup(&cvq->in_iov);
+		vringh_kiov_cleanup(&cvq->out_iov);
+
+		/* Make sure used is visible before rasing the interrupt. */
+		smp_wmb();
+
+		local_bh_disable();
+		if (cvq->cb)
+			cvq->cb(cvq->private);
+		local_bh_enable();
+	}
+}
+
 static void vdpasim_net_work(struct work_struct *work)
 {
 	struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
@@ -77,6 +157,8 @@ static void vdpasim_net_work(struct work_struct *work)
 	if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
 		goto out;
 
+	vdpasim_handle_cvq(vdpasim);
+
 	if (!txq->ready || !rxq->ready)
 		goto out;
 
@@ -162,6 +244,8 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
 	dev_attr.id = VIRTIO_ID_NET;
 	dev_attr.supported_features = VDPASIM_NET_FEATURES;
 	dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
+	dev_attr.ngroups = VDPASIM_NET_GROUP_NUM;
+	dev_attr.nas = VDPASIM_NET_AS_NUM;
 	dev_attr.config_size = sizeof(struct virtio_net_config);
 	dev_attr.get_config = vdpasim_net_get_config;
 	dev_attr.work_fn = vdpasim_net_work;
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 136+ messages in thread

* Re: [RFC PATCH v2 05/19] vdpa: introduce virtqueue groups
  2022-02-24 21:22   ` [RFC PATCH v2 05/19] vdpa: introduce virtqueue groups Gautam Dawar
@ 2022-02-28  8:07       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2022-02-28  8:07 UTC (permalink / raw)
  To: Gautam Dawar
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Michael S. Tsirkin,
	Zhu Lingshan, Stefano Garzarella, Xie Yongji, Eli Cohen,
	Si-Wei Liu, Parav Pandit, Longpeng, virtualization, linux-kernel,
	kvm, netdev


在 2022/2/25 上午5:22, Gautam Dawar 写道:
> This patch introduces virtqueue groups to vDPA device. The virtqueue
> group is the minimal set of virtqueues that must share an address
> space. And the address space identifier could only be attached to
> a specific virtqueue group.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>   drivers/vdpa/ifcvf/ifcvf_main.c   |  8 +++++++-
>   drivers/vdpa/mlx5/net/mlx5_vnet.c |  8 +++++++-
>   drivers/vdpa/vdpa.c               |  3 +++
>   drivers/vdpa/vdpa_sim/vdpa_sim.c  |  9 ++++++++-
>   drivers/vdpa/vdpa_sim/vdpa_sim.h  |  1 +
>   include/linux/vdpa.h              | 16 ++++++++++++----
>   6 files changed, 38 insertions(+), 7 deletions(-)


We had three more parents now, so we should convert them as well:

vp_vdpa, VDUSE and eni_vdpa.

Thanks


>
> diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_main.c
> index d1a6b5ab543c..c815a2e62440 100644
> --- a/drivers/vdpa/ifcvf/ifcvf_main.c
> +++ b/drivers/vdpa/ifcvf/ifcvf_main.c
> @@ -378,6 +378,11 @@ static size_t ifcvf_vdpa_get_config_size(struct vdpa_device *vdpa_dev)
>   	return  vf->config_size;
>   }
>   
> +static u32 ifcvf_vdpa_get_vq_group(struct vdpa_device *vdpa, u16 idx)
> +{
> +	return 0;
> +}
> +
>   static void ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev,
>   				  unsigned int offset,
>   				  void *buf, unsigned int len)
> @@ -453,6 +458,7 @@ static const struct vdpa_config_ops ifc_vdpa_ops = {
>   	.get_device_id	= ifcvf_vdpa_get_device_id,
>   	.get_vendor_id	= ifcvf_vdpa_get_vendor_id,
>   	.get_vq_align	= ifcvf_vdpa_get_vq_align,
> +	.get_vq_group	= ifcvf_vdpa_get_vq_group,
>   	.get_config_size	= ifcvf_vdpa_get_config_size,
>   	.get_config	= ifcvf_vdpa_get_config,
>   	.set_config	= ifcvf_vdpa_set_config,
> @@ -507,7 +513,7 @@ static int ifcvf_vdpa_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
>   	pdev = ifcvf_mgmt_dev->pdev;
>   	dev = &pdev->dev;
>   	adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa,
> -				    dev, &ifc_vdpa_ops, name, false);
> +				    dev, &ifc_vdpa_ops, 1, name, false);
>   	if (IS_ERR(adapter)) {
>   		IFCVF_ERR(pdev, "Failed to allocate vDPA structure");
>   		return PTR_ERR(adapter);
> diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> index b53603d94082..fcfc28460b72 100644
> --- a/drivers/vdpa/mlx5/net/mlx5_vnet.c
> +++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> @@ -1847,6 +1847,11 @@ static u32 mlx5_vdpa_get_vq_align(struct vdpa_device *vdev)
>   	return PAGE_SIZE;
>   }
>   
> +static u32 mlx5_vdpa_get_vq_group(struct vdpa_device *vdpa, u16 idx)
> +{
> +	return 0;
> +}
> +
>   enum { MLX5_VIRTIO_NET_F_GUEST_CSUM = 1 << 9,
>   	MLX5_VIRTIO_NET_F_CSUM = 1 << 10,
>   	MLX5_VIRTIO_NET_F_HOST_TSO6 = 1 << 11,
> @@ -2363,6 +2368,7 @@ static const struct vdpa_config_ops mlx5_vdpa_ops = {
>   	.get_vq_notification = mlx5_get_vq_notification,
>   	.get_vq_irq = mlx5_get_vq_irq,
>   	.get_vq_align = mlx5_vdpa_get_vq_align,
> +	.get_vq_group = mlx5_vdpa_get_vq_group,
>   	.get_device_features = mlx5_vdpa_get_device_features,
>   	.set_driver_features = mlx5_vdpa_set_driver_features,
>   	.get_driver_features = mlx5_vdpa_get_driver_features,
> @@ -2575,7 +2581,7 @@ static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
>   	}
>   
>   	ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
> -				 name, false);
> +				 1, name, false);
>   	if (IS_ERR(ndev))
>   		return PTR_ERR(ndev);
>   
> diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c
> index 9846c9de4bfa..a07bf0130559 100644
> --- a/drivers/vdpa/vdpa.c
> +++ b/drivers/vdpa/vdpa.c
> @@ -159,6 +159,7 @@ static void vdpa_release_dev(struct device *d)
>    * initialized but before registered.
>    * @parent: the parent device
>    * @config: the bus operations that is supported by this device
> + * @ngroups: number of groups supported by this device
>    * @size: size of the parent structure that contains private data
>    * @name: name of the vdpa device; optional.
>    * @use_va: indicate whether virtual address must be used by this device
> @@ -171,6 +172,7 @@ static void vdpa_release_dev(struct device *d)
>    */
>   struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>   					const struct vdpa_config_ops *config,
> +					unsigned int ngroups,
>   					size_t size, const char *name,
>   					bool use_va)
>   {
> @@ -203,6 +205,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>   	vdev->config = config;
>   	vdev->features_valid = false;
>   	vdev->use_va = use_va;
> +	vdev->ngroups = ngroups;
>   
>   	if (name)
>   		err = dev_set_name(&vdev->dev, "%s", name);
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> index ddbe142af09a..c98cb1f869fa 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> @@ -250,7 +250,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
>   	else
>   		ops = &vdpasim_config_ops;
>   
> -	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
> +	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, 1,
>   				    dev_attr->name, false);
>   	if (IS_ERR(vdpasim)) {
>   		ret = PTR_ERR(vdpasim);
> @@ -399,6 +399,11 @@ static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
>   	return VDPASIM_QUEUE_ALIGN;
>   }
>   
> +static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
> +{
> +	return 0;
> +}
> +
>   static u64 vdpasim_get_device_features(struct vdpa_device *vdpa)
>   {
>   	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
> @@ -620,6 +625,7 @@ static const struct vdpa_config_ops vdpasim_config_ops = {
>   	.set_vq_state           = vdpasim_set_vq_state,
>   	.get_vq_state           = vdpasim_get_vq_state,
>   	.get_vq_align           = vdpasim_get_vq_align,
> +	.get_vq_group           = vdpasim_get_vq_group,
>   	.get_device_features    = vdpasim_get_device_features,
>   	.set_driver_features    = vdpasim_set_driver_features,
>   	.get_driver_features    = vdpasim_get_driver_features,
> @@ -650,6 +656,7 @@ static const struct vdpa_config_ops vdpasim_batch_config_ops = {
>   	.set_vq_state           = vdpasim_set_vq_state,
>   	.get_vq_state           = vdpasim_get_vq_state,
>   	.get_vq_align           = vdpasim_get_vq_align,
> +	.get_vq_group           = vdpasim_get_vq_group,
>   	.get_device_features    = vdpasim_get_device_features,
>   	.set_driver_features    = vdpasim_set_driver_features,
>   	.get_driver_features    = vdpasim_get_driver_features,
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> index cd58e888bcf3..0be7c1e7ef80 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> @@ -63,6 +63,7 @@ struct vdpasim {
>   	u32 status;
>   	u32 generation;
>   	u64 features;
> +	u32 groups;
>   	/* spinlock to synchronize iommu table */
>   	spinlock_t iommu_lock;
>   };
> diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
> index 2de442ececae..026b7ad72ed7 100644
> --- a/include/linux/vdpa.h
> +++ b/include/linux/vdpa.h
> @@ -85,6 +85,7 @@ struct vdpa_device {
>   	bool use_va;
>   	int nvqs;
>   	struct vdpa_mgmt_dev *mdev;
> +	unsigned int ngroups;
>   };
>   
>   /**
> @@ -172,6 +173,10 @@ struct vdpa_map_file {
>    *				for the device
>    *				@vdev: vdpa device
>    *				Returns virtqueue algin requirement
> + * @get_vq_group:		Get the group id for a specific virtqueue
> + *				@vdev: vdpa device
> + *				@idx: virtqueue index
> + *				Returns u32: group id for this virtqueue
>    * @get_device_features:	Get virtio features supported by the device
>    *				@vdev: vdpa device
>    *				Returns the virtio features support by the
> @@ -282,6 +287,7 @@ struct vdpa_config_ops {
>   
>   	/* Device ops */
>   	u32 (*get_vq_align)(struct vdpa_device *vdev);
> +	u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx);
>   	u64 (*get_device_features)(struct vdpa_device *vdev);
>   	int (*set_driver_features)(struct vdpa_device *vdev, u64 features);
>   	u64 (*get_driver_features)(struct vdpa_device *vdev);
> @@ -314,6 +320,7 @@ struct vdpa_config_ops {
>   
>   struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>   					const struct vdpa_config_ops *config,
> +					unsigned int ngroups,
>   					size_t size, const char *name,
>   					bool use_va);
>   
> @@ -324,17 +331,18 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>    * @member: the name of struct vdpa_device within the @dev_struct
>    * @parent: the parent device
>    * @config: the bus operations that is supported by this device
> + * @ngroups: the number of virtqueue groups supported by this device
>    * @name: name of the vdpa device
>    * @use_va: indicate whether virtual address must be used by this device
>    *
>    * Return allocated data structure or ERR_PTR upon error
>    */
> -#define vdpa_alloc_device(dev_struct, member, parent, config, name, use_va)   \
> -			  container_of(__vdpa_alloc_device( \
> -				       parent, config, \
> +#define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, name, use_va)   \
> +			  container_of((__vdpa_alloc_device( \
> +				       parent, config, ngroups, \
>   				       sizeof(dev_struct) + \
>   				       BUILD_BUG_ON_ZERO(offsetof( \
> -				       dev_struct, member)), name, use_va), \
> +				       dev_struct, member)), name, use_va)), \
>   				       dev_struct, member)
>   
>   int vdpa_register_device(struct vdpa_device *vdev, int nvqs);


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [RFC PATCH v2 05/19] vdpa: introduce virtqueue groups
@ 2022-02-28  8:07       ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2022-02-28  8:07 UTC (permalink / raw)
  To: Gautam Dawar
  Cc: kvm, Michael S. Tsirkin, netdev, Xie Yongji, linux-kernel,
	gdawar, virtualization, martinh, eperezma, hanand, tanujk,
	Si-Wei Liu, Zhu Lingshan, Longpeng, Eli Cohen


在 2022/2/25 上午5:22, Gautam Dawar 写道:
> This patch introduces virtqueue groups to vDPA device. The virtqueue
> group is the minimal set of virtqueues that must share an address
> space. And the address space identifier could only be attached to
> a specific virtqueue group.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>   drivers/vdpa/ifcvf/ifcvf_main.c   |  8 +++++++-
>   drivers/vdpa/mlx5/net/mlx5_vnet.c |  8 +++++++-
>   drivers/vdpa/vdpa.c               |  3 +++
>   drivers/vdpa/vdpa_sim/vdpa_sim.c  |  9 ++++++++-
>   drivers/vdpa/vdpa_sim/vdpa_sim.h  |  1 +
>   include/linux/vdpa.h              | 16 ++++++++++++----
>   6 files changed, 38 insertions(+), 7 deletions(-)


We had three more parents now, so we should convert them as well:

vp_vdpa, VDUSE and eni_vdpa.

Thanks


>
> diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_main.c
> index d1a6b5ab543c..c815a2e62440 100644
> --- a/drivers/vdpa/ifcvf/ifcvf_main.c
> +++ b/drivers/vdpa/ifcvf/ifcvf_main.c
> @@ -378,6 +378,11 @@ static size_t ifcvf_vdpa_get_config_size(struct vdpa_device *vdpa_dev)
>   	return  vf->config_size;
>   }
>   
> +static u32 ifcvf_vdpa_get_vq_group(struct vdpa_device *vdpa, u16 idx)
> +{
> +	return 0;
> +}
> +
>   static void ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev,
>   				  unsigned int offset,
>   				  void *buf, unsigned int len)
> @@ -453,6 +458,7 @@ static const struct vdpa_config_ops ifc_vdpa_ops = {
>   	.get_device_id	= ifcvf_vdpa_get_device_id,
>   	.get_vendor_id	= ifcvf_vdpa_get_vendor_id,
>   	.get_vq_align	= ifcvf_vdpa_get_vq_align,
> +	.get_vq_group	= ifcvf_vdpa_get_vq_group,
>   	.get_config_size	= ifcvf_vdpa_get_config_size,
>   	.get_config	= ifcvf_vdpa_get_config,
>   	.set_config	= ifcvf_vdpa_set_config,
> @@ -507,7 +513,7 @@ static int ifcvf_vdpa_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
>   	pdev = ifcvf_mgmt_dev->pdev;
>   	dev = &pdev->dev;
>   	adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa,
> -				    dev, &ifc_vdpa_ops, name, false);
> +				    dev, &ifc_vdpa_ops, 1, name, false);
>   	if (IS_ERR(adapter)) {
>   		IFCVF_ERR(pdev, "Failed to allocate vDPA structure");
>   		return PTR_ERR(adapter);
> diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> index b53603d94082..fcfc28460b72 100644
> --- a/drivers/vdpa/mlx5/net/mlx5_vnet.c
> +++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> @@ -1847,6 +1847,11 @@ static u32 mlx5_vdpa_get_vq_align(struct vdpa_device *vdev)
>   	return PAGE_SIZE;
>   }
>   
> +static u32 mlx5_vdpa_get_vq_group(struct vdpa_device *vdpa, u16 idx)
> +{
> +	return 0;
> +}
> +
>   enum { MLX5_VIRTIO_NET_F_GUEST_CSUM = 1 << 9,
>   	MLX5_VIRTIO_NET_F_CSUM = 1 << 10,
>   	MLX5_VIRTIO_NET_F_HOST_TSO6 = 1 << 11,
> @@ -2363,6 +2368,7 @@ static const struct vdpa_config_ops mlx5_vdpa_ops = {
>   	.get_vq_notification = mlx5_get_vq_notification,
>   	.get_vq_irq = mlx5_get_vq_irq,
>   	.get_vq_align = mlx5_vdpa_get_vq_align,
> +	.get_vq_group = mlx5_vdpa_get_vq_group,
>   	.get_device_features = mlx5_vdpa_get_device_features,
>   	.set_driver_features = mlx5_vdpa_set_driver_features,
>   	.get_driver_features = mlx5_vdpa_get_driver_features,
> @@ -2575,7 +2581,7 @@ static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
>   	}
>   
>   	ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
> -				 name, false);
> +				 1, name, false);
>   	if (IS_ERR(ndev))
>   		return PTR_ERR(ndev);
>   
> diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c
> index 9846c9de4bfa..a07bf0130559 100644
> --- a/drivers/vdpa/vdpa.c
> +++ b/drivers/vdpa/vdpa.c
> @@ -159,6 +159,7 @@ static void vdpa_release_dev(struct device *d)
>    * initialized but before registered.
>    * @parent: the parent device
>    * @config: the bus operations that is supported by this device
> + * @ngroups: number of groups supported by this device
>    * @size: size of the parent structure that contains private data
>    * @name: name of the vdpa device; optional.
>    * @use_va: indicate whether virtual address must be used by this device
> @@ -171,6 +172,7 @@ static void vdpa_release_dev(struct device *d)
>    */
>   struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>   					const struct vdpa_config_ops *config,
> +					unsigned int ngroups,
>   					size_t size, const char *name,
>   					bool use_va)
>   {
> @@ -203,6 +205,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>   	vdev->config = config;
>   	vdev->features_valid = false;
>   	vdev->use_va = use_va;
> +	vdev->ngroups = ngroups;
>   
>   	if (name)
>   		err = dev_set_name(&vdev->dev, "%s", name);
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> index ddbe142af09a..c98cb1f869fa 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> @@ -250,7 +250,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
>   	else
>   		ops = &vdpasim_config_ops;
>   
> -	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
> +	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, 1,
>   				    dev_attr->name, false);
>   	if (IS_ERR(vdpasim)) {
>   		ret = PTR_ERR(vdpasim);
> @@ -399,6 +399,11 @@ static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
>   	return VDPASIM_QUEUE_ALIGN;
>   }
>   
> +static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
> +{
> +	return 0;
> +}
> +
>   static u64 vdpasim_get_device_features(struct vdpa_device *vdpa)
>   {
>   	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
> @@ -620,6 +625,7 @@ static const struct vdpa_config_ops vdpasim_config_ops = {
>   	.set_vq_state           = vdpasim_set_vq_state,
>   	.get_vq_state           = vdpasim_get_vq_state,
>   	.get_vq_align           = vdpasim_get_vq_align,
> +	.get_vq_group           = vdpasim_get_vq_group,
>   	.get_device_features    = vdpasim_get_device_features,
>   	.set_driver_features    = vdpasim_set_driver_features,
>   	.get_driver_features    = vdpasim_get_driver_features,
> @@ -650,6 +656,7 @@ static const struct vdpa_config_ops vdpasim_batch_config_ops = {
>   	.set_vq_state           = vdpasim_set_vq_state,
>   	.get_vq_state           = vdpasim_get_vq_state,
>   	.get_vq_align           = vdpasim_get_vq_align,
> +	.get_vq_group           = vdpasim_get_vq_group,
>   	.get_device_features    = vdpasim_get_device_features,
>   	.set_driver_features    = vdpasim_set_driver_features,
>   	.get_driver_features    = vdpasim_get_driver_features,
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> index cd58e888bcf3..0be7c1e7ef80 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> @@ -63,6 +63,7 @@ struct vdpasim {
>   	u32 status;
>   	u32 generation;
>   	u64 features;
> +	u32 groups;
>   	/* spinlock to synchronize iommu table */
>   	spinlock_t iommu_lock;
>   };
> diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
> index 2de442ececae..026b7ad72ed7 100644
> --- a/include/linux/vdpa.h
> +++ b/include/linux/vdpa.h
> @@ -85,6 +85,7 @@ struct vdpa_device {
>   	bool use_va;
>   	int nvqs;
>   	struct vdpa_mgmt_dev *mdev;
> +	unsigned int ngroups;
>   };
>   
>   /**
> @@ -172,6 +173,10 @@ struct vdpa_map_file {
>    *				for the device
>    *				@vdev: vdpa device
>    *				Returns virtqueue algin requirement
> + * @get_vq_group:		Get the group id for a specific virtqueue
> + *				@vdev: vdpa device
> + *				@idx: virtqueue index
> + *				Returns u32: group id for this virtqueue
>    * @get_device_features:	Get virtio features supported by the device
>    *				@vdev: vdpa device
>    *				Returns the virtio features support by the
> @@ -282,6 +287,7 @@ struct vdpa_config_ops {
>   
>   	/* Device ops */
>   	u32 (*get_vq_align)(struct vdpa_device *vdev);
> +	u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx);
>   	u64 (*get_device_features)(struct vdpa_device *vdev);
>   	int (*set_driver_features)(struct vdpa_device *vdev, u64 features);
>   	u64 (*get_driver_features)(struct vdpa_device *vdev);
> @@ -314,6 +320,7 @@ struct vdpa_config_ops {
>   
>   struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>   					const struct vdpa_config_ops *config,
> +					unsigned int ngroups,
>   					size_t size, const char *name,
>   					bool use_va);
>   
> @@ -324,17 +331,18 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>    * @member: the name of struct vdpa_device within the @dev_struct
>    * @parent: the parent device
>    * @config: the bus operations that is supported by this device
> + * @ngroups: the number of virtqueue groups supported by this device
>    * @name: name of the vdpa device
>    * @use_va: indicate whether virtual address must be used by this device
>    *
>    * Return allocated data structure or ERR_PTR upon error
>    */
> -#define vdpa_alloc_device(dev_struct, member, parent, config, name, use_va)   \
> -			  container_of(__vdpa_alloc_device( \
> -				       parent, config, \
> +#define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, name, use_va)   \
> +			  container_of((__vdpa_alloc_device( \
> +				       parent, config, ngroups, \
>   				       sizeof(dev_struct) + \
>   				       BUILD_BUG_ON_ZERO(offsetof( \
> -				       dev_struct, member)), name, use_va), \
> +				       dev_struct, member)), name, use_va)), \
>   				       dev_struct, member)
>   
>   int vdpa_register_device(struct vdpa_device *vdev, int nvqs);

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [RFC PATCH v2 00/19] Control VQ support in vDPA
  2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
@ 2022-02-28  8:17     ` Jason Wang
  2022-02-24 21:22   ` [RFC PATCH v2 02/19] virtio-vdpa: don't set callback if virtio doesn't need it Gautam Dawar
                       ` (18 subsequent siblings)
  19 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2022-02-28  8:17 UTC (permalink / raw)
  To: Gautam Dawar
  Cc: gdawar, martinh, hanand, tanujk, eperezma, Michael S. Tsirkin,
	Zhu Lingshan, Stefano Garzarella, Xie Yongji, Eli Cohen,
	Si-Wei Liu, Parav Pandit, Longpeng, virtualization, linux-kernel,
	kvm, netdev


在 2022/2/25 上午5:22, Gautam Dawar 写道:
> Hi All:
>
> This series tries to add the support for control virtqueue in vDPA.
>
> Control virtqueue is used by networking device for accepting various
> commands from the driver. It's a must to support multiqueue and other
> configurations.
>
> When used by vhost-vDPA bus driver for VM, the control virtqueue
> should be shadowed via userspace VMM (Qemu) instead of being assigned
> directly to Guest. This is because Qemu needs to know the device state
> in order to start and stop device correctly (e.g for Live Migration).
>
> This requies to isolate the memory mapping for control virtqueue
> presented by vhost-vDPA to prevent guest from accessing it directly.
>
> To achieve this, vDPA introduce two new abstractions:
>
> - address space: identified through address space id (ASID) and a set
>                   of memory mapping in maintained
> - virtqueue group: the minimal set of virtqueues that must share an
>                   address space
>
> Device needs to advertise the following attributes to vDPA:
>
> - the number of address spaces supported in the device
> - the number of virtqueue groups supported in the device
> - the mappings from a specific virtqueue to its virtqueue groups
>
> The mappings from virtqueue to virtqueue groups is fixed and defined
> by vDPA device driver. E.g:
>
> - For the device that has hardware ASID support, it can simply
>    advertise a per virtqueue virtqueue group.
> - For the device that does not have hardware ASID support, it can
>    simply advertise a single virtqueue group that contains all
>    virtqueues. Or if it wants a software emulated control virtqueue, it
>    can advertise two virtqueue groups, one is for cvq, another is for
>    the rest virtqueues.
>
> vDPA also allow to change the association between virtqueue group and
> address space. So in the case of control virtqueue, userspace
> VMM(Qemu) may use a dedicated address space for the control virtqueue
> group to isolate the memory mapping.
>
> The vhost/vhost-vDPA is also extend for the userspace to:
>
> - query the number of virtqueue groups and address spaces supported by
>    the device
> - query the virtqueue group for a specific virtqueue
> - assocaite a virtqueue group with an address space
> - send ASID based IOTLB commands
>
> This will help userspace VMM(Qemu) to detect whether the control vq
> could be supported and isolate memory mappings of control virtqueue
> from the others.
>
> To demonstrate the usage, vDPA simulator is extended to support
> setting MAC address via a emulated control virtqueue.
>
> Please review.
>
> Changes since v1:
>
> - Rebased the v1 patch series on vhost branch of MST vhost git repo
>    git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git/log/?h=vhost
> - Updates to accommodate vdpa_sim changes from monolithic module in
>    kernel used v1 patch series to current modularized class (net, block)
>    based approach.
> - Added new attributes (ngroups and nas) to "vdpasim_dev_attr" and
>    propagated them from vdpa_sim_net to vdpa_sim
> - Widened the data-type for "asid" member of vhost_msg_v2 to __u32
>    to accommodate PASID


This is great. Then the semantic matches exactly the PASID proposal here[1].


> - Fixed the buildbot warnings
> - Resolved all checkpatch.pl errors and warnings
> - Tested both control and datapath with Xilinx Smartnic SN1000 series
>    device using QEMU implementing the Shadow virtqueue and support for
>    VQ groups and ASID available at:
>    github.com/eugpermar/qemu/releases/tag/vdpa_sw_live_migration.d%2F
>    asid_groups-v1.d%2F00


On top, we may extend the netlink protocol to report the mapping between 
virtqueue to its groups.


Thanks

[1] 
https://www.mail-archive.com/virtio-dev@lists.oasis-open.org/msg08077.html


>
> Changes since RFC:
>
> - tweak vhost uAPI documentation
> - switch to use device specific IOTLB really in patch 4
> - tweak the commit log
> - fix that ASID in vhost is claimed to be 32 actually but 16bit
>    actually
> - fix use after free when using ASID with IOTLB batching requests
> - switch to use Stefano's patch for having separated iov
> - remove unused "used_as" variable
> - fix the iotlb/asid checking in vhost_vdpa_unmap()
>
> Thanks
>
> Gautam Dawar (19):
>    vhost: move the backend feature bits to vhost_types.h
>    virtio-vdpa: don't set callback if virtio doesn't need it
>    vhost-vdpa: passing iotlb to IOMMU mapping helpers
>    vhost-vdpa: switch to use vhost-vdpa specific IOTLB
>    vdpa: introduce virtqueue groups
>    vdpa: multiple address spaces support
>    vdpa: introduce config operations for associating ASID to a virtqueue
>      group
>    vhost_iotlb: split out IOTLB initialization
>    vhost: support ASID in IOTLB API
>    vhost-vdpa: introduce asid based IOTLB
>    vhost-vdpa: introduce uAPI to get the number of virtqueue groups
>    vhost-vdpa: introduce uAPI to get the number of address spaces
>    vhost-vdpa: uAPI to get virtqueue group id
>    vhost-vdpa: introduce uAPI to set group ASID
>    vhost-vdpa: support ASID based IOTLB API
>    vdpa_sim: advertise VIRTIO_NET_F_MTU
>    vdpa_sim: factor out buffer completion logic
>    vdpa_sim: filter destination mac address
>    vdpasim: control virtqueue support
>
>   drivers/vdpa/ifcvf/ifcvf_main.c      |   8 +-
>   drivers/vdpa/mlx5/net/mlx5_vnet.c    |  11 +-
>   drivers/vdpa/vdpa.c                  |   5 +
>   drivers/vdpa/vdpa_sim/vdpa_sim.c     | 100 ++++++++--
>   drivers/vdpa/vdpa_sim/vdpa_sim.h     |   3 +
>   drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 169 +++++++++++++----
>   drivers/vhost/iotlb.c                |  23 ++-
>   drivers/vhost/vdpa.c                 | 272 +++++++++++++++++++++------
>   drivers/vhost/vhost.c                |  23 ++-
>   drivers/vhost/vhost.h                |   4 +-
>   drivers/virtio/virtio_vdpa.c         |   2 +-
>   include/linux/vdpa.h                 |  46 ++++-
>   include/linux/vhost_iotlb.h          |   2 +
>   include/uapi/linux/vhost.h           |  25 ++-
>   include/uapi/linux/vhost_types.h     |  11 +-
>   15 files changed, 566 insertions(+), 138 deletions(-)
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [RFC PATCH v2 00/19] Control VQ support in vDPA
@ 2022-02-28  8:17     ` Jason Wang
  0 siblings, 0 replies; 136+ messages in thread
From: Jason Wang @ 2022-02-28  8:17 UTC (permalink / raw)
  To: Gautam Dawar
  Cc: kvm, Michael S. Tsirkin, netdev, Xie Yongji, linux-kernel,
	gdawar, virtualization, martinh, eperezma, hanand, tanujk,
	Si-Wei Liu, Zhu Lingshan, Longpeng, Eli Cohen


在 2022/2/25 上午5:22, Gautam Dawar 写道:
> Hi All:
>
> This series tries to add the support for control virtqueue in vDPA.
>
> Control virtqueue is used by networking device for accepting various
> commands from the driver. It's a must to support multiqueue and other
> configurations.
>
> When used by vhost-vDPA bus driver for VM, the control virtqueue
> should be shadowed via userspace VMM (Qemu) instead of being assigned
> directly to Guest. This is because Qemu needs to know the device state
> in order to start and stop device correctly (e.g for Live Migration).
>
> This requies to isolate the memory mapping for control virtqueue
> presented by vhost-vDPA to prevent guest from accessing it directly.
>
> To achieve this, vDPA introduce two new abstractions:
>
> - address space: identified through address space id (ASID) and a set
>                   of memory mapping in maintained
> - virtqueue group: the minimal set of virtqueues that must share an
>                   address space
>
> Device needs to advertise the following attributes to vDPA:
>
> - the number of address spaces supported in the device
> - the number of virtqueue groups supported in the device
> - the mappings from a specific virtqueue to its virtqueue groups
>
> The mappings from virtqueue to virtqueue groups is fixed and defined
> by vDPA device driver. E.g:
>
> - For the device that has hardware ASID support, it can simply
>    advertise a per virtqueue virtqueue group.
> - For the device that does not have hardware ASID support, it can
>    simply advertise a single virtqueue group that contains all
>    virtqueues. Or if it wants a software emulated control virtqueue, it
>    can advertise two virtqueue groups, one is for cvq, another is for
>    the rest virtqueues.
>
> vDPA also allow to change the association between virtqueue group and
> address space. So in the case of control virtqueue, userspace
> VMM(Qemu) may use a dedicated address space for the control virtqueue
> group to isolate the memory mapping.
>
> The vhost/vhost-vDPA is also extend for the userspace to:
>
> - query the number of virtqueue groups and address spaces supported by
>    the device
> - query the virtqueue group for a specific virtqueue
> - assocaite a virtqueue group with an address space
> - send ASID based IOTLB commands
>
> This will help userspace VMM(Qemu) to detect whether the control vq
> could be supported and isolate memory mappings of control virtqueue
> from the others.
>
> To demonstrate the usage, vDPA simulator is extended to support
> setting MAC address via a emulated control virtqueue.
>
> Please review.
>
> Changes since v1:
>
> - Rebased the v1 patch series on vhost branch of MST vhost git repo
>    git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git/log/?h=vhost
> - Updates to accommodate vdpa_sim changes from monolithic module in
>    kernel used v1 patch series to current modularized class (net, block)
>    based approach.
> - Added new attributes (ngroups and nas) to "vdpasim_dev_attr" and
>    propagated them from vdpa_sim_net to vdpa_sim
> - Widened the data-type for "asid" member of vhost_msg_v2 to __u32
>    to accommodate PASID


This is great. Then the semantic matches exactly the PASID proposal here[1].


> - Fixed the buildbot warnings
> - Resolved all checkpatch.pl errors and warnings
> - Tested both control and datapath with Xilinx Smartnic SN1000 series
>    device using QEMU implementing the Shadow virtqueue and support for
>    VQ groups and ASID available at:
>    github.com/eugpermar/qemu/releases/tag/vdpa_sw_live_migration.d%2F
>    asid_groups-v1.d%2F00


On top, we may extend the netlink protocol to report the mapping between 
virtqueue to its groups.


Thanks

[1] 
https://www.mail-archive.com/virtio-dev@lists.oasis-open.org/msg08077.html


>
> Changes since RFC:
>
> - tweak vhost uAPI documentation
> - switch to use device specific IOTLB really in patch 4
> - tweak the commit log
> - fix that ASID in vhost is claimed to be 32 actually but 16bit
>    actually
> - fix use after free when using ASID with IOTLB batching requests
> - switch to use Stefano's patch for having separated iov
> - remove unused "used_as" variable
> - fix the iotlb/asid checking in vhost_vdpa_unmap()
>
> Thanks
>
> Gautam Dawar (19):
>    vhost: move the backend feature bits to vhost_types.h
>    virtio-vdpa: don't set callback if virtio doesn't need it
>    vhost-vdpa: passing iotlb to IOMMU mapping helpers
>    vhost-vdpa: switch to use vhost-vdpa specific IOTLB
>    vdpa: introduce virtqueue groups
>    vdpa: multiple address spaces support
>    vdpa: introduce config operations for associating ASID to a virtqueue
>      group
>    vhost_iotlb: split out IOTLB initialization
>    vhost: support ASID in IOTLB API
>    vhost-vdpa: introduce asid based IOTLB
>    vhost-vdpa: introduce uAPI to get the number of virtqueue groups
>    vhost-vdpa: introduce uAPI to get the number of address spaces
>    vhost-vdpa: uAPI to get virtqueue group id
>    vhost-vdpa: introduce uAPI to set group ASID
>    vhost-vdpa: support ASID based IOTLB API
>    vdpa_sim: advertise VIRTIO_NET_F_MTU
>    vdpa_sim: factor out buffer completion logic
>    vdpa_sim: filter destination mac address
>    vdpasim: control virtqueue support
>
>   drivers/vdpa/ifcvf/ifcvf_main.c      |   8 +-
>   drivers/vdpa/mlx5/net/mlx5_vnet.c    |  11 +-
>   drivers/vdpa/vdpa.c                  |   5 +
>   drivers/vdpa/vdpa_sim/vdpa_sim.c     | 100 ++++++++--
>   drivers/vdpa/vdpa_sim/vdpa_sim.h     |   3 +
>   drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 169 +++++++++++++----
>   drivers/vhost/iotlb.c                |  23 ++-
>   drivers/vhost/vdpa.c                 | 272 +++++++++++++++++++++------
>   drivers/vhost/vhost.c                |  23 ++-
>   drivers/vhost/vhost.h                |   4 +-
>   drivers/virtio/virtio_vdpa.c         |   2 +-
>   include/linux/vdpa.h                 |  46 ++++-
>   include/linux/vhost_iotlb.h          |   2 +
>   include/uapi/linux/vhost.h           |  25 ++-
>   include/uapi/linux/vhost_types.h     |  11 +-
>   15 files changed, 566 insertions(+), 138 deletions(-)
>

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

^ permalink raw reply	[flat|nested] 136+ messages in thread

* RE: [RFC PATCH v2 00/19] Control VQ support in vDPA
  2022-02-28  8:17     ` Jason Wang
  (?)
@ 2022-02-28 10:56     ` Gautam Dawar
  -1 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-02-28 10:56 UTC (permalink / raw)
  To: Jason Wang
  Cc: Martin Petrus Hubertus Habets, Harpreet Singh Anand,
	Tanuj Murlidhar Kamde, eperezma, Michael S. Tsirkin,
	Zhu Lingshan, Stefano Garzarella, Xie Yongji, Eli Cohen,
	Si-Wei Liu, Parav Pandit, Longpeng, virtualization, linux-kernel,
	kvm, netdev

在 2022/2/25 上午5:22, Gautam Dawar 写道:
> Hi All:
>
> This series tries to add the support for control virtqueue in vDPA.
>
> Control virtqueue is used by networking device for accepting various 
> commands from the driver. It's a must to support multiqueue and other 
> configurations.
>
> When used by vhost-vDPA bus driver for VM, the control virtqueue 
> should be shadowed via userspace VMM (Qemu) instead of being assigned 
> directly to Guest. This is because Qemu needs to know the device state 
> in order to start and stop device correctly (e.g for Live Migration).
>
> This requies to isolate the memory mapping for control virtqueue 
> presented by vhost-vDPA to prevent guest from accessing it directly.
>
> To achieve this, vDPA introduce two new abstractions:
>
> - address space: identified through address space id (ASID) and a set
>                   of memory mapping in maintained
> - virtqueue group: the minimal set of virtqueues that must share an
>                   address space
>
> Device needs to advertise the following attributes to vDPA:
>
> - the number of address spaces supported in the device
> - the number of virtqueue groups supported in the device
> - the mappings from a specific virtqueue to its virtqueue groups
>
> The mappings from virtqueue to virtqueue groups is fixed and defined 
> by vDPA device driver. E.g:
>
> - For the device that has hardware ASID support, it can simply
>    advertise a per virtqueue virtqueue group.
> - For the device that does not have hardware ASID support, it can
>    simply advertise a single virtqueue group that contains all
>    virtqueues. Or if it wants a software emulated control virtqueue, it
>    can advertise two virtqueue groups, one is for cvq, another is for
>    the rest virtqueues.
>
> vDPA also allow to change the association between virtqueue group and 
> address space. So in the case of control virtqueue, userspace
> VMM(Qemu) may use a dedicated address space for the control virtqueue 
> group to isolate the memory mapping.
>
> The vhost/vhost-vDPA is also extend for the userspace to:
>
> - query the number of virtqueue groups and address spaces supported by
>    the device
> - query the virtqueue group for a specific virtqueue
> - assocaite a virtqueue group with an address space
> - send ASID based IOTLB commands
>
> This will help userspace VMM(Qemu) to detect whether the control vq 
> could be supported and isolate memory mappings of control virtqueue 
> from the others.
>
> To demonstrate the usage, vDPA simulator is extended to support 
> setting MAC address via a emulated control virtqueue.
>
> Please review.
>
> Changes since v1:
>
> - Rebased the v1 patch series on vhost branch of MST vhost git repo
>    git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git/log/?h=vhost
> - Updates to accommodate vdpa_sim changes from monolithic module in
>    kernel used v1 patch series to current modularized class (net, block)
>    based approach.
> - Added new attributes (ngroups and nas) to "vdpasim_dev_attr" and
>    propagated them from vdpa_sim_net to vdpa_sim
> - Widened the data-type for "asid" member of vhost_msg_v2 to __u32
>    to accommodate PASID


This is great. Then the semantic matches exactly the PASID proposal here[1].


> - Fixed the buildbot warnings
> - Resolved all checkpatch.pl errors and warnings
> - Tested both control and datapath with Xilinx Smartnic SN1000 series
>    device using QEMU implementing the Shadow virtqueue and support for
>    VQ groups and ASID available at:
>    github.com/eugpermar/qemu/releases/tag/vdpa_sw_live_migration.d%2F
>    asid_groups-v1.d%2F00


On top, we may extend the netlink protocol to report the mapping between virtqueue to its groups.
[GD>>] Yes, I've already discussed this with Eugenio. For testing purpose, I added the mapping in Xilinx netdriver "sfc".

Thanks

[1] 
https://www.mail-archive.com/virtio-dev@lists.oasis-open.org/msg08077.html


>
> Changes since RFC:
>
> - tweak vhost uAPI documentation
> - switch to use device specific IOTLB really in patch 4
> - tweak the commit log
> - fix that ASID in vhost is claimed to be 32 actually but 16bit
>    actually
> - fix use after free when using ASID with IOTLB batching requests
> - switch to use Stefano's patch for having separated iov
> - remove unused "used_as" variable
> - fix the iotlb/asid checking in vhost_vdpa_unmap()
>
> Thanks
>
> Gautam Dawar (19):
>    vhost: move the backend feature bits to vhost_types.h
>    virtio-vdpa: don't set callback if virtio doesn't need it
>    vhost-vdpa: passing iotlb to IOMMU mapping helpers
>    vhost-vdpa: switch to use vhost-vdpa specific IOTLB
>    vdpa: introduce virtqueue groups
>    vdpa: multiple address spaces support
>    vdpa: introduce config operations for associating ASID to a virtqueue
>      group
>    vhost_iotlb: split out IOTLB initialization
>    vhost: support ASID in IOTLB API
>    vhost-vdpa: introduce asid based IOTLB
>    vhost-vdpa: introduce uAPI to get the number of virtqueue groups
>    vhost-vdpa: introduce uAPI to get the number of address spaces
>    vhost-vdpa: uAPI to get virtqueue group id
>    vhost-vdpa: introduce uAPI to set group ASID
>    vhost-vdpa: support ASID based IOTLB API
>    vdpa_sim: advertise VIRTIO_NET_F_MTU
>    vdpa_sim: factor out buffer completion logic
>    vdpa_sim: filter destination mac address
>    vdpasim: control virtqueue support
>
>   drivers/vdpa/ifcvf/ifcvf_main.c      |   8 +-
>   drivers/vdpa/mlx5/net/mlx5_vnet.c    |  11 +-
>   drivers/vdpa/vdpa.c                  |   5 +
>   drivers/vdpa/vdpa_sim/vdpa_sim.c     | 100 ++++++++--
>   drivers/vdpa/vdpa_sim/vdpa_sim.h     |   3 +
>   drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 169 +++++++++++++----
>   drivers/vhost/iotlb.c                |  23 ++-
>   drivers/vhost/vdpa.c                 | 272 +++++++++++++++++++++------
>   drivers/vhost/vhost.c                |  23 ++-
>   drivers/vhost/vhost.h                |   4 +-
>   drivers/virtio/virtio_vdpa.c         |   2 +-
>   include/linux/vdpa.h                 |  46 ++++-
>   include/linux/vhost_iotlb.h          |   2 +
>   include/uapi/linux/vhost.h           |  25 ++-
>   include/uapi/linux/vhost_types.h     |  11 +-
>   15 files changed, 566 insertions(+), 138 deletions(-)
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* RE: [RFC PATCH v2 05/19] vdpa: introduce virtqueue groups
  2022-02-28  8:07       ` Jason Wang
  (?)
@ 2022-02-28 10:57       ` Gautam Dawar
  -1 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-02-28 10:57 UTC (permalink / raw)
  To: Jason Wang
  Cc: Martin Petrus Hubertus Habets, Harpreet Singh Anand,
	Tanuj Murlidhar Kamde, eperezma, Michael S. Tsirkin,
	Zhu Lingshan, Stefano Garzarella, Xie Yongji, Eli Cohen,
	Si-Wei Liu, Parav Pandit, Longpeng, virtualization, linux-kernel,
	kvm, netdev

在 2022/2/25 上午5:22, Gautam Dawar 写道:
> This patch introduces virtqueue groups to vDPA device. The virtqueue 
> group is the minimal set of virtqueues that must share an address 
> space. And the address space identifier could only be attached to a 
> specific virtqueue group.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>   drivers/vdpa/ifcvf/ifcvf_main.c   |  8 +++++++-
>   drivers/vdpa/mlx5/net/mlx5_vnet.c |  8 +++++++-
>   drivers/vdpa/vdpa.c               |  3 +++
>   drivers/vdpa/vdpa_sim/vdpa_sim.c  |  9 ++++++++-
>   drivers/vdpa/vdpa_sim/vdpa_sim.h  |  1 +
>   include/linux/vdpa.h              | 16 ++++++++++++----
>   6 files changed, 38 insertions(+), 7 deletions(-)


We had three more parents now, so we should convert them as well:

vp_vdpa, VDUSE and eni_vdpa.
[GD>>] I agree. However, the intention of publishing this as a RFC series is to gather feedback on the approach in the cover letter and at the same time to test its integration with both a simulator (vdpa_sim) and an actual (Xilinx SmartNIC) HW device with a development build of QEMU supporting Shadow virtqueue by Eugnio.
Once this is approved, we can move to the actual patch series covering all other vdpa parent drivers.

Thanks


>
> diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c 
> b/drivers/vdpa/ifcvf/ifcvf_main.c index d1a6b5ab543c..c815a2e62440 
> 100644
> --- a/drivers/vdpa/ifcvf/ifcvf_m
> +++ b/drivers/vdpa/ifcvf/ifcvf_main.c
> @@ -378,6 +378,11 @@ static size_t ifcvf_vdpa_get_config_size(struct vdpa_device *vdpa_dev)
>   	return  vf->config_size;
>   }
>   
> +static u32 ifcvf_vdpa_get_vq_group(struct vdpa_device *vdpa, u16 idx) 
> +{
> +	return 0;
> +}
> +
>   static void ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev,
>   				  unsigned int offset,
>   				  void *buf, unsigned int len)
> @@ -453,6 +458,7 @@ static const struct vdpa_config_ops ifc_vdpa_ops = {
>   	.get_device_id	= ifcvf_vdpa_get_device_id,
>   	.get_vendor_id	= ifcvf_vdpa_get_vendor_id,
>   	.get_vq_align	= ifcvf_vdpa_get_vq_align,
> +	.get_vq_group	= ifcvf_vdpa_get_vq_group,
>   	.get_config_size	= ifcvf_vdpa_get_config_size,
>   	.get_config	= ifcvf_vdpa_get_config,
>   	.set_config	= ifcvf_vdpa_set_config,
> @@ -507,7 +513,7 @@ static int ifcvf_vdpa_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
>   	pdev = ifcvf_mgmt_dev->pdev;
>   	dev = &pdev->dev;
>   	adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa,
> -				    dev, &ifc_vdpa_ops, name, false);
> +				    dev, &ifc_vdpa_ops, 1, name, false);
>   	if (IS_ERR(adapter)) {
>   		IFCVF_ERR(pdev, "Failed to allocate vDPA structure");
>   		return PTR_ERR(adapter);
> diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c 
> b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> index b53603d94082..fcfc28460b72 100644
> --- a/drivers/vdpa/mlx5/net/mlx5_vnet.c
> +++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> @@ -1847,6 +1847,11 @@ static u32 mlx5_vdpa_get_vq_align(struct vdpa_device *vdev)
>   	return PAGE_SIZE;
>   }
>   
> +static u32 mlx5_vdpa_get_vq_group(struct vdpa_device *vdpa, u16 idx) 
> +{
> +	return 0;
> +}
> +
>   enum { MLX5_VIRTIO_NET_F_GUEST_CSUM = 1 << 9,
>   	MLX5_VIRTIO_NET_F_CSUM = 1 << 10,
>   	MLX5_VIRTIO_NET_F_HOST_TSO6 = 1 << 11, @@ -2363,6 +2368,7 @@ static 
> const struct vdpa_config_ops mlx5_vdpa_ops = {
>   	.get_vq_notification = mlx5_get_vq_notification,
>   	.get_vq_irq = mlx5_get_vq_irq,
>   	.get_vq_align = mlx5_vdpa_get_vq_align,
> +	.get_vq_group = mlx5_vdpa_get_vq_group,
>   	.get_device_features = mlx5_vdpa_get_device_features,
>   	.set_driver_features = mlx5_vdpa_set_driver_features,
>   	.get_driver_features = mlx5_vdpa_get_driver_features, @@ -2575,7 
> +2581,7 @@ static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
>   	}
>   
>   	ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
> -				 name, false);
> +				 1, name, false);
>   	if (IS_ERR(ndev))
>   		return PTR_ERR(ndev);
>   
> diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c index 
> 9846c9de4bfa..a07bf0130559 100644
> --- a/drivers/vdpa/vdpa.c
> +++ b/drivers/vdpa/vdpa.c
> @@ -159,6 +159,7 @@ static void vdpa_release_dev(struct device *d)
>    * initialized but before registered.
>    * @parent: the parent device
>    * @config: the bus operations that is supported by this device
> + * @ngroups: number of groups supported by this device
>    * @size: size of the parent structure that contains private data
>    * @name: name of the vdpa device; optional.
>    * @use_va: indicate whether virtual address must be used by this 
> device @@ -171,6 +172,7 @@ static void vdpa_release_dev(struct device *d)
>    */
>   struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>   					const struct vdpa_config_ops *config,
> +					unsigned int ngroups,
>   					size_t size, const char *name,
>   					bool use_va)
>   {
> @@ -203,6 +205,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>   	vdev->config = config;
>   	vdev->features_valid = false;
>   	vdev->use_va = use_va;
> +	vdev->ngroups = ngroups;
>   
>   	if (name)
>   		err = dev_set_name(&vdev->dev, "%s", name); diff --git 
> a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> index ddbe142af09a..c98cb1f869fa 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> @@ -250,7 +250,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
>   	else
>   		ops = &vdpasim_config_ops;
>   
> -	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
> +	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, 1,
>   				    dev_attr->name, false);
>   	if (IS_ERR(vdpasim)) {
>   		ret = PTR_ERR(vdpasim);
> @@ -399,6 +399,11 @@ static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
>   	return VDPASIM_QUEUE_ALIGN;
>   }
>   
> +static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx) {
> +	return 0;
> +}
> +
>   static u64 vdpasim_get_device_features(struct vdpa_device *vdpa)
>   {
>   	struct vdpasim *vdpasim = vdpa_to_sim(vdpa); @@ -620,6 +625,7 @@ 
> static const struct vdpa_config_ops vdpasim_config_ops = {
>   	.set_vq_state           = vdpasim_set_vq_state,
>   	.get_vq_state           = vdpasim_get_vq_state,
>   	.get_vq_align           = vdpasim_get_vq_align,
> +	.get_vq_group           = vdpasim_get_vq_group,
>   	.get_device_features    = vdpasim_get_device_features,
>   	.set_driver_features    = vdpasim_set_driver_features,
>   	.get_driver_features    = vdpasim_get_driver_features,
> @@ -650,6 +656,7 @@ static const struct vdpa_config_ops vdpasim_batch_config_ops = {
>   	.set_vq_state           = vdpasim_set_vq_state,
>   	.get_vq_state           = vdpasim_get_vq_state,
>   	.get_vq_align           = vdpasim_get_vq_align,
> +	.get_vq_group           = vdpasim_get_vq_group,
>   	.get_device_features    = vdpasim_get_device_features,
>   	.set_driver_features    = vdpasim_set_driver_features,
>   	.get_driver_features    = vdpasim_get_driver_features,
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h 
> b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> index cd58e888bcf3..0be7c1e7ef80 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> @@ -63,6 +63,7 @@ struct vdpasim {
>   	u32 status;
>   	u32 generation;
>   	u64 features;
> +	u32 groups;
>   	/* spinlock to synchronize iommu table */
>   	spinlock_t iommu_lock;
>   };
> diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h index 
> 2de442ececae..026b7ad72ed7 100644
> --- a/include/linux/vdpa.h
> +++ b/include/linux/vdpa.h
> @@ -85,6 +85,7 @@ struct vdpa_device {
>   	bool use_va;
>   	int nvqs;
>   	struct vdpa_mgmt_dev *mdev;
> +	unsigned int ngroups;
>   };
>   
>   /**
> @@ -172,6 +173,10 @@ struct vdpa_map_file {
>    *				for the device
>    *				@vdev: vdpa device
>    *				Returns virtqueue algin requirement
> + * @get_vq_group:		Get the group id for a specific virtqueue
> + *				@vdev: vdpa device
> + *				@idx: virtqueue index
> + *				Returns u32: group id for this virtqueue
>    * @get_device_features:	Get virtio features supported by the device
>    *				@vdev: vdpa device
>    *				Returns the virtio features support by the
> @@ -282,6 +287,7 @@ struct vdpa_config_ops {
>   
>   	/* Device ops */
>   	u32 (*get_vq_align)(struct vdpa_device *vdev);
> +	u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx);
>   	u64 (*get_device_features)(struct vdpa_device *vdev);
>   	int (*set_driver_features)(struct vdpa_device *vdev, u64 features);
>   	u64 (*get_driver_features)(struct vdpa_device *vdev); @@ -314,6 
> +320,7 @@ struct vdpa_config_ops {
>   
>   struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>   					const struct vdpa_config_ops *config,
> +					unsigned int ngroups,
>   					size_t size, const char *name,
>   					bool use_va);
>   
> @@ -324,17 +331,18 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>    * @member: the name of struct vdpa_device within the @dev_struct
>    * @parent: the parent device
>    * @config: the bus operations that is supported by this device
> + * @ngroups: the number of virtqueue groups supported by this device
>    * @name: name of the vdpa device
>    * @use_va: indicate whether virtual address must be used by this device
>    *
>    * Return allocated data structure or ERR_PTR upon error
>    */
> -#define vdpa_alloc_device(dev_struct, member, parent, config, name, use_va)   \
> -			  container_of(__vdpa_alloc_device( \
> -				       parent, config, \
> +#define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, name, use_va)   \
> +			  container_of((__vdpa_alloc_device( \
> +				       parent, config, ngroups, \
>   				       sizeof(dev_struct) + \
>   				       BUILD_BUG_ON_ZERO(offsetof( \
> -				       dev_struct, member)), name, use_va), \
> +				       dev_struct, member)), name, use_va)), \
>   				       dev_struct, member)
>   
>   int vdpa_register_device(struct vdpa_device *vdev, int nvqs);


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [RFC PATCH v2 06/19] vdpa: multiple address spaces support
  2022-02-24 21:22   ` [RFC PATCH v2 06/19] vdpa: multiple address spaces support Gautam Dawar
@ 2022-03-03 19:39     ` Eugenio Perez Martin
  2022-03-04  6:30       ` Gautam Dawar
  0 siblings, 1 reply; 136+ messages in thread
From: Eugenio Perez Martin @ 2022-03-03 19:39 UTC (permalink / raw)
  To: Gautam Dawar
  Cc: Gautam Dawar, Martin Petrus Hubertus Habets,
	Harpreet Singh Anand, tanujk, Jason Wang, Michael S. Tsirkin,
	Zhu Lingshan, Stefano Garzarella, Xie Yongji, Eli Cohen,
	Si-Wei Liu, Parav Pandit, Longpeng, virtualization, linux-kernel,
	kvm list, netdev

On Thu, Feb 24, 2022 at 10:25 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
>
> This patches introduces the multiple address spaces support for vDPA
> device. This idea is to identify a specific address space via an
> dedicated identifier - ASID.
>
> During vDPA device allocation, vDPA device driver needs to report the
> number of address spaces supported by the device then the DMA mapping
> ops of the vDPA device needs to be extended to support ASID.
>
> This helps to isolate the environments for the virtqueue that will not
> be assigned directly. E.g in the case of virtio-net, the control
> virtqueue will not be assigned directly to guest.
>
> As a start, simply claim 1 virtqueue groups and 1 address spaces for
> all vDPA devices. And vhost-vDPA will simply reject the device with
> more than 1 virtqueue groups or address spaces.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>  drivers/vdpa/ifcvf/ifcvf_main.c   |  2 +-
>  drivers/vdpa/mlx5/net/mlx5_vnet.c |  5 +++--
>  drivers/vdpa/vdpa.c               |  4 +++-
>  drivers/vdpa/vdpa_sim/vdpa_sim.c  | 10 ++++++----
>  drivers/vhost/vdpa.c              | 14 +++++++++-----
>  include/linux/vdpa.h              | 28 +++++++++++++++++++---------
>  6 files changed, 41 insertions(+), 22 deletions(-)
>
> diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_main.c
> index c815a2e62440..a4815c5612f9 100644
> --- a/drivers/vdpa/ifcvf/ifcvf_main.c
> +++ b/drivers/vdpa/ifcvf/ifcvf_main.c
> @@ -513,7 +513,7 @@ static int ifcvf_vdpa_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
>         pdev = ifcvf_mgmt_dev->pdev;
>         dev = &pdev->dev;
>         adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa,
> -                                   dev, &ifc_vdpa_ops, 1, name, false);
> +                                   dev, &ifc_vdpa_ops, 1, 1, name, false);
>         if (IS_ERR(adapter)) {
>                 IFCVF_ERR(pdev, "Failed to allocate vDPA structure");
>                 return PTR_ERR(adapter);
> diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> index fcfc28460b72..a76417892ef3 100644
> --- a/drivers/vdpa/mlx5/net/mlx5_vnet.c
> +++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> @@ -2282,7 +2282,8 @@ static u32 mlx5_vdpa_get_generation(struct vdpa_device *vdev)
>         return mvdev->generation;
>  }
>
> -static int mlx5_vdpa_set_map(struct vdpa_device *vdev, struct vhost_iotlb *iotlb)
> +static int mlx5_vdpa_set_map(struct vdpa_device *vdev, unsigned int asid,
> +                            struct vhost_iotlb *iotlb)
>  {
>         struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
>         bool change_map;
> @@ -2581,7 +2582,7 @@ static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
>         }
>
>         ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
> -                                1, name, false);
> +                                1, 1, name, false);
>         if (IS_ERR(ndev))
>                 return PTR_ERR(ndev);
>
> diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c
> index a07bf0130559..1793dc12b208 100644
> --- a/drivers/vdpa/vdpa.c
> +++ b/drivers/vdpa/vdpa.c
> @@ -160,6 +160,7 @@ static void vdpa_release_dev(struct device *d)
>   * @parent: the parent device
>   * @config: the bus operations that is supported by this device
>   * @ngroups: number of groups supported by this device
> + * @nas: number of address spaces supported by this device
>   * @size: size of the parent structure that contains private data
>   * @name: name of the vdpa device; optional.
>   * @use_va: indicate whether virtual address must be used by this device
> @@ -172,7 +173,7 @@ static void vdpa_release_dev(struct device *d)
>   */
>  struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>                                         const struct vdpa_config_ops *config,
> -                                       unsigned int ngroups,
> +                                       unsigned int ngroups, unsigned int nas,
>                                         size_t size, const char *name,
>                                         bool use_va)
>  {
> @@ -206,6 +207,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>         vdev->features_valid = false;
>         vdev->use_va = use_va;
>         vdev->ngroups = ngroups;
> +       vdev->nas = nas;
>
>         if (name)
>                 err = dev_set_name(&vdev->dev, "%s", name);
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> index c98cb1f869fa..659e2e2e4b0c 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> @@ -251,7 +251,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
>                 ops = &vdpasim_config_ops;
>
>         vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, 1,
> -                                   dev_attr->name, false);
> +                                   1, dev_attr->name, false);
>         if (IS_ERR(vdpasim)) {
>                 ret = PTR_ERR(vdpasim);
>                 goto err_alloc;
> @@ -539,7 +539,7 @@ static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
>         return range;
>  }
>
> -static int vdpasim_set_map(struct vdpa_device *vdpa,
> +static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
>                            struct vhost_iotlb *iotlb)
>  {
>         struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
> @@ -566,7 +566,8 @@ static int vdpasim_set_map(struct vdpa_device *vdpa,
>         return ret;
>  }
>
> -static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
> +static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
> +                          u64 iova, u64 size,
>                            u64 pa, u32 perm, void *opaque)
>  {
>         struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
> @@ -580,7 +581,8 @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
>         return ret;
>  }
>
> -static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
> +static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
> +                            u64 iova, u64 size)
>  {
>         struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> index 655ff7029401..6bf755f84d26 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -599,10 +599,10 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
>                 return r;
>
>         if (ops->dma_map) {
> -               r = ops->dma_map(vdpa, iova, size, pa, perm, opaque);
> +               r = ops->dma_map(vdpa, 0, iova, size, pa, perm, opaque);
>         } else if (ops->set_map) {
>                 if (!v->in_batch)
> -                       r = ops->set_map(vdpa, iotlb);
> +                       r = ops->set_map(vdpa, 0, iotlb);
>         } else {
>                 r = iommu_map(v->domain, iova, pa, size,
>                               perm_to_iommu_flags(perm));
> @@ -628,10 +628,10 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
>         vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
>
>         if (ops->dma_map) {
> -               ops->dma_unmap(vdpa, iova, size);
> +               ops->dma_unmap(vdpa, 0, iova, size);
>         } else if (ops->set_map) {
>                 if (!v->in_batch)
> -                       ops->set_map(vdpa, iotlb);
> +                       ops->set_map(vdpa, 0, iotlb);
>         } else {
>                 iommu_unmap(v->domain, iova, size);
>         }
> @@ -863,7 +863,7 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
>                 break;
>         case VHOST_IOTLB_BATCH_END:
>                 if (v->in_batch && ops->set_map)
> -                       ops->set_map(vdpa, iotlb);
> +                       ops->set_map(vdpa, 0, iotlb);
>                 v->in_batch = false;
>                 break;
>         default:
> @@ -1128,6 +1128,10 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>         int minor;
>         int r;
>
> +       /* Only support 1 address space and 1 groups */
> +       if (vdpa->ngroups != 1 || vdpa->nas != 1)
> +               return -EOPNOTSUPP;
> +
>         v = kzalloc(sizeof(*v), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
>         if (!v)
>                 return -ENOMEM;
> diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
> index 026b7ad72ed7..de22ca1a8ef3 100644
> --- a/include/linux/vdpa.h
> +++ b/include/linux/vdpa.h
> @@ -69,6 +69,8 @@ struct vdpa_mgmt_dev;
>   * @cf_mutex: Protects get and set access to configuration layout.
>   * @index: device index
>   * @features_valid: were features initialized? for legacy guests
> + * @ngroups: the number of virtqueue groups
> + * @nas: the number of address spaces
>   * @use_va: indicate whether virtual address must be used by this device
>   * @nvqs: maximum number of supported virtqueues
>   * @mdev: management device pointer; caller must setup when registering device as part
> @@ -86,6 +88,7 @@ struct vdpa_device {
>         int nvqs;
>         struct vdpa_mgmt_dev *mdev;
>         unsigned int ngroups;
> +       unsigned int nas;
>  };
>
>  /**
> @@ -240,6 +243,7 @@ struct vdpa_map_file {
>   *                             Needed for device that using device
>   *                             specific DMA translation (on-chip IOMMU)
>   *                             @vdev: vdpa device
> + *                             @asid: address space identifier
>   *                             @iotlb: vhost memory mapping to be
>   *                             used by the vDPA
>   *                             Returns integer: success (0) or error (< 0)
> @@ -248,6 +252,7 @@ struct vdpa_map_file {
>   *                             specific DMA translation (on-chip IOMMU)
>   *                             and preferring incremental map.
>   *                             @vdev: vdpa device
> + *                             @asid: address space identifier
>   *                             @iova: iova to be mapped
>   *                             @size: size of the area
>   *                             @pa: physical address for the map
> @@ -259,6 +264,7 @@ struct vdpa_map_file {
>   *                             specific DMA translation (on-chip IOMMU)
>   *                             and preferring incremental unmap.
>   *                             @vdev: vdpa device
> + *                             @asid: address space identifier
>   *                             @iova: iova to be unmapped
>   *                             @size: size of the area
>   *                             Returns integer: success (0) or error (< 0)
> @@ -309,10 +315,12 @@ struct vdpa_config_ops {
>         struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
>
>         /* DMA ops */
> -       int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
> -       int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
> -                      u64 pa, u32 perm, void *opaque);
> -       int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
> +       int (*set_map)(struct vdpa_device *vdev, unsigned int asid,
> +                      struct vhost_iotlb *iotlb);
> +       int (*dma_map)(struct vdpa_device *vdev, unsigned int asid,
> +                      u64 iova, u64 size, u64 pa, u32 perm, void *opaque);
> +       int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
> +                        u64 iova, u64 size);
>
>         /* Free device resources */
>         void (*free)(struct vdpa_device *vdev);
> @@ -320,7 +328,7 @@ struct vdpa_config_ops {
>
>  struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>                                         const struct vdpa_config_ops *config,
> -                                       unsigned int ngroups,
> +                                       unsigned int ngroups, unsigned int nas,
>                                         size_t size, const char *name,
>                                         bool use_va);
>
> @@ -332,17 +340,19 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>   * @parent: the parent device
>   * @config: the bus operations that is supported by this device
>   * @ngroups: the number of virtqueue groups supported by this device
> + * @nas: the number of address spaces
>   * @name: name of the vdpa device
>   * @use_va: indicate whether virtual address must be used by this device
>   *
>   * Return allocated data structure or ERR_PTR upon error
>   */
> -#define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, name, use_va)   \
> +#define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, nas, \
> +                         name, use_va) \
>                           container_of((__vdpa_alloc_device( \
> -                                      parent, config, ngroups, \
> -                                      sizeof(dev_struct) + \
> +                                      parent, config, ngroups, nas, \
> +                                      (sizeof(dev_struct) + \

Maybe too nitpick or I'm missing something, but do we need to add the
parentheses around (sizeof(dev_struct) + BUILD_BUG_ON_ZERO(...)) ?

>                                        BUILD_BUG_ON_ZERO(offsetof( \
> -                                      dev_struct, member)), name, use_va)), \
> +                                      dev_struct, member))), name, use_va)), \
>                                        dev_struct, member)
>
>  int vdpa_register_device(struct vdpa_device *vdev, int nvqs);
> --
> 2.25.0
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* RE: [RFC PATCH v2 06/19] vdpa: multiple address spaces support
  2022-03-03 19:39     ` Eugenio Perez Martin
@ 2022-03-04  6:30       ` Gautam Dawar
  2022-03-04 17:45         ` Eugenio Perez Martin
  0 siblings, 1 reply; 136+ messages in thread
From: Gautam Dawar @ 2022-03-04  6:30 UTC (permalink / raw)
  To: Eugenio Perez Martin
  Cc: Martin Petrus Hubertus Habets, Harpreet Singh Anand,
	Tanuj Murlidhar Kamde, Jason Wang, Michael S. Tsirkin,
	Zhu Lingshan, Stefano Garzarella, Xie Yongji, Eli Cohen,
	Si-Wei Liu, Parav Pandit, Longpeng, virtualization, linux-kernel,
	kvm list, netdev

-----Original Message-----
From: Eugenio Perez Martin <eperezma@redhat.com> 
Sent: Friday, March 4, 2022 1:10 AM
To: Gautam Dawar <gdawar@xilinx.com>
Cc: Gautam Dawar <gdawar@xilinx.com>; Martin Petrus Hubertus Habets <martinh@xilinx.com>; Harpreet Singh Anand <hanand@xilinx.com>; Tanuj Murlidhar Kamde <tanujk@xilinx.com>; Jason Wang <jasowang@redhat.com>; Michael S. Tsirkin <mst@redhat.com>; Zhu Lingshan <lingshan.zhu@intel.com>; Stefano Garzarella <sgarzare@redhat.com>; Xie Yongji <xieyongji@bytedance.com>; Eli Cohen <elic@nvidia.com>; Si-Wei Liu <si-wei.liu@oracle.com>; Parav Pandit <parav@nvidia.com>; Longpeng <longpeng2@huawei.com>; virtualization <virtualization@lists.linux-foundation.org>; linux-kernel@vger.kernel.org; kvm list <kvm@vger.kernel.org>; netdev@vger.kernel.org
Subject: Re: [RFC PATCH v2 06/19] vdpa: multiple address spaces support

On Thu, Feb 24, 2022 at 10:25 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
>
> This patches introduces the multiple address spaces support for vDPA 
> device. This idea is to identify a specific address space via an 
> dedicated identifier - ASID.
>
> During vDPA device allocation, vDPA device driver needs to report the 
> number of address spaces supported by the device then the DMA mapping 
> ops of the vDPA device needs to be extended to support ASID.
>
> This helps to isolate the environments for the virtqueue that will not 
> be assigned directly. E.g in the case of virtio-net, the control 
> virtqueue will not be assigned directly to guest.
>
> As a start, simply claim 1 virtqueue groups and 1 address spaces for 
> all vDPA devices. And vhost-vDPA will simply reject the device with 
> more than 1 virtqueue groups or address spaces.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>  drivers/vdpa/ifcvf/ifcvf_main.c   |  2 +-
>  drivers/vdpa/mlx5/net/mlx5_vnet.c |  5 +++--
>  drivers/vdpa/vdpa.c               |  4 +++-
>  drivers/vdpa/vdpa_sim/vdpa_sim.c  | 10 ++++++----
>  drivers/vhost/vdpa.c              | 14 +++++++++-----
>  include/linux/vdpa.h              | 28 +++++++++++++++++++---------
>  6 files changed, 41 insertions(+), 22 deletions(-)
>
> diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c 
> b/drivers/vdpa/ifcvf/ifcvf_main.c index c815a2e62440..a4815c5612f9 
> 100644
> --- a/drivers/vdpa/ifcvf/ifcvf_main.c
> +++ b/drivers/vdpa/ifcvf/ifcvf_main.c
> @@ -513,7 +513,7 @@ static int ifcvf_vdpa_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
>         pdev = ifcvf_mgmt_dev->pdev;
>         dev = &pdev->dev;
>         adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa,
> -                                   dev, &ifc_vdpa_ops, 1, name, false);
> +                                   dev, &ifc_vdpa_ops, 1, 1, name, 
> + false);
>         if (IS_ERR(adapter)) {
>                 IFCVF_ERR(pdev, "Failed to allocate vDPA structure");
>                 return PTR_ERR(adapter); diff --git 
> a/drivers/vdpa/mlx5/net/mlx5_vnet.c 
> b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> index fcfc28460b72..a76417892ef3 100644
> --- a/drivers/vdpa/mlx5/net/mlx5_vnet.c
> +++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> @@ -2282,7 +2282,8 @@ static u32 mlx5_vdpa_get_generation(struct vdpa_device *vdev)
>         return mvdev->generation;
>  }
>
> -static int mlx5_vdpa_set_map(struct vdpa_device *vdev, struct 
> vhost_iotlb *iotlb)
> +static int mlx5_vdpa_set_map(struct vdpa_device *vdev, unsigned int asid,
> +                            struct vhost_iotlb *iotlb)
>  {
>         struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
>         bool change_map;
> @@ -2581,7 +2582,7 @@ static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
>         }
>
>         ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
> -                                1, name, false);
> +                                1, 1, name, false);
>         if (IS_ERR(ndev))
>                 return PTR_ERR(ndev);
>
> diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c index 
> a07bf0130559..1793dc12b208 100644
> --- a/drivers/vdpa/vdpa.c
> +++ b/drivers/vdpa/vdpa.c
> @@ -160,6 +160,7 @@ static void vdpa_release_dev(struct device *d)
>   * @parent: the parent device
>   * @config: the bus operations that is supported by this device
>   * @ngroups: number of groups supported by this device
> + * @nas: number of address spaces supported by this device
>   * @size: size of the parent structure that contains private data
>   * @name: name of the vdpa device; optional.
>   * @use_va: indicate whether virtual address must be used by this 
> device @@ -172,7 +173,7 @@ static void vdpa_release_dev(struct device *d)
>   */
>  struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>                                         const struct vdpa_config_ops *config,
> -                                       unsigned int ngroups,
> +                                       unsigned int ngroups, unsigned 
> + int nas,
>                                         size_t size, const char *name,
>                                         bool use_va)  { @@ -206,6 
> +207,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>         vdev->features_valid = false;
>         vdev->use_va = use_va;
>         vdev->ngroups = ngroups;
> +       vdev->nas = nas;
>
>         if (name)
>                 err = dev_set_name(&vdev->dev, "%s", name); diff --git 
> a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> index c98cb1f869fa..659e2e2e4b0c 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> @@ -251,7 +251,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
>                 ops = &vdpasim_config_ops;
>
>         vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, 1,
> -                                   dev_attr->name, false);
> +                                   1, dev_attr->name, false);
>         if (IS_ERR(vdpasim)) {
>                 ret = PTR_ERR(vdpasim);
>                 goto err_alloc;
> @@ -539,7 +539,7 @@ static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
>         return range;
>  }
>
> -static int vdpasim_set_map(struct vdpa_device *vdpa,
> +static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int 
> +asid,
>                            struct vhost_iotlb *iotlb)  {
>         struct vdpasim *vdpasim = vdpa_to_sim(vdpa); @@ -566,7 +566,8 
> @@ static int vdpasim_set_map(struct vdpa_device *vdpa,
>         return ret;
>  }
>
> -static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 
> size,
> +static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
> +                          u64 iova, u64 size,
>                            u64 pa, u32 perm, void *opaque)  {
>         struct vdpasim *vdpasim = vdpa_to_sim(vdpa); @@ -580,7 +581,8 
> @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
>         return ret;
>  }
>
> -static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 
> size)
> +static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
> +                            u64 iova, u64 size)
>  {
>         struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index 
> 655ff7029401..6bf755f84d26 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -599,10 +599,10 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
>                 return r;
>
>         if (ops->dma_map) {
> -               r = ops->dma_map(vdpa, iova, size, pa, perm, opaque);
> +               r = ops->dma_map(vdpa, 0, iova, size, pa, perm, 
> + opaque);
>         } else if (ops->set_map) {
>                 if (!v->in_batch)
> -                       r = ops->set_map(vdpa, iotlb);
> +                       r = ops->set_map(vdpa, 0, iotlb);
>         } else {
>                 r = iommu_map(v->domain, iova, pa, size,
>                               perm_to_iommu_flags(perm)); @@ -628,10 
> +628,10 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
>         vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
>
>         if (ops->dma_map) {
> -               ops->dma_unmap(vdpa, iova, size);
> +               ops->dma_unmap(vdpa, 0, iova, size);
>         } else if (ops->set_map) {
>                 if (!v->in_batch)
> -                       ops->set_map(vdpa, iotlb);
> +                       ops->set_map(vdpa, 0, iotlb);
>         } else {
>                 iommu_unmap(v->domain, iova, size);
>         }
> @@ -863,7 +863,7 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
>                 break;
>         case VHOST_IOTLB_BATCH_END:
>                 if (v->in_batch && ops->set_map)
> -                       ops->set_map(vdpa, iotlb);
> +                       ops->set_map(vdpa, 0, iotlb);
>                 v->in_batch = false;
>                 break;
>         default:
> @@ -1128,6 +1128,10 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>         int minor;
>         int r;
>
> +       /* Only support 1 address space and 1 groups */
> +       if (vdpa->ngroups != 1 || vdpa->nas != 1)
> +               return -EOPNOTSUPP;
> +
>         v = kzalloc(sizeof(*v), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
>         if (!v)
>                 return -ENOMEM;
> diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h index 
> 026b7ad72ed7..de22ca1a8ef3 100644
> --- a/include/linux/vdpa.h
> +++ b/include/linux/vdpa.h
> @@ -69,6 +69,8 @@ struct vdpa_mgmt_dev;
>   * @cf_mutex: Protects get and set access to configuration layout.
>   * @index: device index
>   * @features_valid: were features initialized? for legacy guests
> + * @ngroups: the number of virtqueue groups
> + * @nas: the number of address spaces
>   * @use_va: indicate whether virtual address must be used by this device
>   * @nvqs: maximum number of supported virtqueues
>   * @mdev: management device pointer; caller must setup when 
> registering device as part @@ -86,6 +88,7 @@ struct vdpa_device {
>         int nvqs;
>         struct vdpa_mgmt_dev *mdev;
>         unsigned int ngroups;
> +       unsigned int nas;
>  };
>
>  /**
> @@ -240,6 +243,7 @@ struct vdpa_map_file {
>   *                             Needed for device that using device
>   *                             specific DMA translation (on-chip IOMMU)
>   *                             @vdev: vdpa device
> + *                             @asid: address space identifier
>   *                             @iotlb: vhost memory mapping to be
>   *                             used by the vDPA
>   *                             Returns integer: success (0) or error (< 0)
> @@ -248,6 +252,7 @@ struct vdpa_map_file {
>   *                             specific DMA translation (on-chip IOMMU)
>   *                             and preferring incremental map.
>   *                             @vdev: vdpa device
> + *                             @asid: address space identifier
>   *                             @iova: iova to be mapped
>   *                             @size: size of the area
>   *                             @pa: physical address for the map
> @@ -259,6 +264,7 @@ struct vdpa_map_file {
>   *                             specific DMA translation (on-chip IOMMU)
>   *                             and preferring incremental unmap.
>   *                             @vdev: vdpa device
> + *                             @asid: address space identifier
>   *                             @iova: iova to be unmapped
>   *                             @size: size of the area
>   *                             Returns integer: success (0) or error (< 0)
> @@ -309,10 +315,12 @@ struct vdpa_config_ops {
>         struct vdpa_iova_range (*get_iova_range)(struct vdpa_device 
> *vdev);
>
>         /* DMA ops */
> -       int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
> -       int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
> -                      u64 pa, u32 perm, void *opaque);
> -       int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
> +       int (*set_map)(struct vdpa_device *vdev, unsigned int asid,
> +                      struct vhost_iotlb *iotlb);
> +       int (*dma_map)(struct vdpa_device *vdev, unsigned int asid,
> +                      u64 iova, u64 size, u64 pa, u32 perm, void *opaque);
> +       int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
> +                        u64 iova, u64 size);
>
>         /* Free device resources */
>         void (*free)(struct vdpa_device *vdev); @@ -320,7 +328,7 @@ 
> struct vdpa_config_ops {
>
>  struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>                                         const struct vdpa_config_ops *config,
> -                                       unsigned int ngroups,
> +                                       unsigned int ngroups, unsigned 
> + int nas,
>                                         size_t size, const char *name,
>                                         bool use_va);
>
> @@ -332,17 +340,19 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
>   * @parent: the parent device
>   * @config: the bus operations that is supported by this device
>   * @ngroups: the number of virtqueue groups supported by this device
> + * @nas: the number of address spaces
>   * @name: name of the vdpa device
>   * @use_va: indicate whether virtual address must be used by this device
>   *
>   * Return allocated data structure or ERR_PTR upon error
>   */
> -#define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, name, use_va)   \
> +#define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, nas, \
> +                         name, use_va) \
>                           container_of((__vdpa_alloc_device( \
> -                                      parent, config, ngroups, \
> -                                      sizeof(dev_struct) + \
> +                                      parent, config, ngroups, nas, \
> +                                      (sizeof(dev_struct) + \

Maybe too nitpick or I'm missing something, but do we need to add the parentheses around (sizeof(dev_struct) + BUILD_BUG_ON_ZERO(...)) ?
[GD>>] Yes, that's required as without it checkpatch reports "ERROR: Macros with complex values should be enclosed in parentheses"

>                                        BUILD_BUG_ON_ZERO(offsetof( \
> -                                      dev_struct, member)), name, use_va)), \
> +                                      dev_struct, member))), name, 
> + use_va)), \
>                                        dev_struct, member)
>
>  int vdpa_register_device(struct vdpa_device *vdev, int nvqs);
> --
> 2.25.0
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [RFC PATCH v2 07/19] vdpa: introduce config operations for associating ASID to a virtqueue group
  2022-02-24 21:22   ` [RFC PATCH v2 07/19] vdpa: introduce config operations for associating ASID to a virtqueue group Gautam Dawar
@ 2022-03-04  9:54     ` Eugenio Perez Martin
  2022-03-04 17:48       ` Gautam Dawar
  0 siblings, 1 reply; 136+ messages in thread
From: Eugenio Perez Martin @ 2022-03-04  9:54 UTC (permalink / raw)
  To: Gautam Dawar
  Cc: Gautam Dawar, Martin Petrus Hubertus Habets,
	Harpreet Singh Anand, tanujk, Jason Wang, Michael S. Tsirkin,
	Zhu Lingshan, Stefano Garzarella, Xie Yongji, Eli Cohen,
	Si-Wei Liu, Parav Pandit, Longpeng, virtualization, linux-kernel,
	kvm list, netdev

On Thu, Feb 24, 2022 at 10:25 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
>
> This patch introduces a new bus operation to allow the vDPA bus driver
> to associate an ASID to a virtqueue group.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>  include/linux/vdpa.h | 10 ++++++++++
>  1 file changed, 10 insertions(+)
>
> diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
> index de22ca1a8ef3..7386860c3995 100644
> --- a/include/linux/vdpa.h
> +++ b/include/linux/vdpa.h
> @@ -239,6 +239,12 @@ struct vdpa_map_file {
>   *                             @vdev: vdpa device
>   *                             Returns the iova range supported by
>   *                             the device.
> + * @set_group_asid:            Set address space identifier for a
> + *                             virtqueue group
> + *                             @vdev: vdpa device
> + *                             @group: virtqueue group
> + *                             @asid: address space id for this group
> + *                             Returns integer: success (0) or error (< 0)
>   * @set_map:                   Set device memory mapping (optional)
>   *                             Needed for device that using device
>   *                             specific DMA translation (on-chip IOMMU)
> @@ -321,6 +327,10 @@ struct vdpa_config_ops {
>                        u64 iova, u64 size, u64 pa, u32 perm, void *opaque);
>         int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
>                          u64 iova, u64 size);
> +       int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group,
> +                             unsigned int asid);
> +
> +

Nit again, and Jason's patch already contained these, but I think
these two blank lines are introduced unintentionally.

>
>         /* Free device resources */
>         void (*free)(struct vdpa_device *vdev);
> --
> 2.25.0
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [RFC PATCH v2 09/19] vhost: support ASID in IOTLB API
  2022-02-24 21:22   ` [RFC PATCH v2 09/19] vhost: support ASID in IOTLB API Gautam Dawar
@ 2022-03-04 10:25     ` Eugenio Perez Martin
  2022-03-04 17:52       ` Gautam Dawar
  0 siblings, 1 reply; 136+ messages in thread
From: Eugenio Perez Martin @ 2022-03-04 10:25 UTC (permalink / raw)
  To: Gautam Dawar
  Cc: Gautam Dawar, Martin Petrus Hubertus Habets,
	Harpreet Singh Anand, tanujk, Jason Wang, Michael S. Tsirkin,
	Zhu Lingshan, Stefano Garzarella, Xie Yongji, Eli Cohen,
	Si-Wei Liu, Parav Pandit, Longpeng, virtualization, linux-kernel,
	kvm list, netdev

On Thu, Feb 24, 2022 at 10:26 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
>
> This patches allows userspace to send ASID based IOTLB message to
> vhost. This idea is to use the reserved u32 field in the existing V2
> IOTLB message. Vhost device should advertise this capability via
> VHOST_BACKEND_F_IOTLB_ASID backend feature.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>  drivers/vhost/vdpa.c             |  5 ++++-
>  drivers/vhost/vhost.c            | 23 ++++++++++++++++++-----
>  drivers/vhost/vhost.h            |  4 ++--
>  include/uapi/linux/vhost_types.h |  6 +++++-
>  4 files changed, 29 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> index 6bf755f84d26..d0aacc0cc79a 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -836,7 +836,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
>                                  msg->perm);
>  }
>
> -static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
> +static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
>                                         struct vhost_iotlb_msg *msg)
>  {
>         struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
> @@ -847,6 +847,9 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
>
>         mutex_lock(&dev->mutex);
>
> +       if (asid != 0)
> +               return -EINVAL;
> +

Should we check for asid != 0 before acquiring the mutex? Otherwise
the code never releases it.

>         r = vhost_dev_check_owner(dev);
>         if (r)
>                 goto unlock;
> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> index 59edb5a1ffe2..1f514d98f0de 100644
> --- a/drivers/vhost/vhost.c
> +++ b/drivers/vhost/vhost.c
> @@ -468,7 +468,7 @@ void vhost_dev_init(struct vhost_dev *dev,
>                     struct vhost_virtqueue **vqs, int nvqs,
>                     int iov_limit, int weight, int byte_weight,
>                     bool use_worker,
> -                   int (*msg_handler)(struct vhost_dev *dev,
> +                   int (*msg_handler)(struct vhost_dev *dev, u32 asid,
>                                        struct vhost_iotlb_msg *msg))
>  {
>         struct vhost_virtqueue *vq;
> @@ -1090,11 +1090,14 @@ static bool umem_access_ok(u64 uaddr, u64 size, int access)
>         return true;
>  }
>
> -static int vhost_process_iotlb_msg(struct vhost_dev *dev,
> +static int vhost_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
>                                    struct vhost_iotlb_msg *msg)
>  {
>         int ret = 0;
>
> +       if (asid != 0)
> +               return -EINVAL;
> +
>         mutex_lock(&dev->mutex);
>         vhost_dev_lock_vqs(dev);
>         switch (msg->type) {
> @@ -1141,6 +1144,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
>         struct vhost_iotlb_msg msg;
>         size_t offset;
>         int type, ret;
> +       u32 asid = 0;
>
>         ret = copy_from_iter(&type, sizeof(type), from);
>         if (ret != sizeof(type)) {
> @@ -1156,7 +1160,16 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
>                 offset = offsetof(struct vhost_msg, iotlb) - sizeof(int);
>                 break;
>         case VHOST_IOTLB_MSG_V2:
> -               offset = sizeof(__u32);
> +               if (vhost_backend_has_feature(dev->vqs[0],
> +                                             VHOST_BACKEND_F_IOTLB_ASID)) {
> +                       ret = copy_from_iter(&asid, sizeof(asid), from);
> +                       if (ret != sizeof(asid)) {
> +                               ret = -EINVAL;
> +                               goto done;
> +                       }
> +                       offset = sizeof(__u16);
> +               } else
> +                       offset = sizeof(__u32);
>                 break;
>         default:
>                 ret = -EINVAL;
> @@ -1171,9 +1184,9 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
>         }
>
>         if (dev->msg_handler)
> -               ret = dev->msg_handler(dev, &msg);
> +               ret = dev->msg_handler(dev, asid, &msg);
>         else
> -               ret = vhost_process_iotlb_msg(dev, &msg);
> +               ret = vhost_process_iotlb_msg(dev, asid, &msg);
>         if (ret) {
>                 ret = -EFAULT;
>                 goto done;
> diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
> index 638bb640d6b4..9f238d6c7b58 100644
> --- a/drivers/vhost/vhost.h
> +++ b/drivers/vhost/vhost.h
> @@ -161,7 +161,7 @@ struct vhost_dev {
>         int byte_weight;
>         u64 kcov_handle;
>         bool use_worker;
> -       int (*msg_handler)(struct vhost_dev *dev,
> +       int (*msg_handler)(struct vhost_dev *dev, u32 asid,
>                            struct vhost_iotlb_msg *msg);
>  };
>
> @@ -169,7 +169,7 @@ bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
>  void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
>                     int nvqs, int iov_limit, int weight, int byte_weight,
>                     bool use_worker,
> -                   int (*msg_handler)(struct vhost_dev *dev,
> +                   int (*msg_handler)(struct vhost_dev *dev, u32 asid,
>                                        struct vhost_iotlb_msg *msg));
>  long vhost_dev_set_owner(struct vhost_dev *dev);
>  bool vhost_dev_has_owner(struct vhost_dev *dev);
> diff --git a/include/uapi/linux/vhost_types.h b/include/uapi/linux/vhost_types.h
> index 76ee7016c501..634cee485abb 100644
> --- a/include/uapi/linux/vhost_types.h
> +++ b/include/uapi/linux/vhost_types.h
> @@ -87,7 +87,7 @@ struct vhost_msg {
>
>  struct vhost_msg_v2 {
>         __u32 type;
> -       __u32 reserved;
> +       __u32 asid;
>         union {
>                 struct vhost_iotlb_msg iotlb;
>                 __u8 padding[64];
> @@ -157,5 +157,9 @@ struct vhost_vdpa_iova_range {
>  #define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1
>  /* IOTLB can accept batching hints */
>  #define VHOST_BACKEND_F_IOTLB_BATCH  0x2
> +/* IOTLB can accept address space identifier through V2 type of IOTLB
> + * message
> + */
> +#define VHOST_BACKEND_F_IOTLB_ASID  0x3
>
>  #endif
> --
> 2.25.0
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [RFC PATCH v2 06/19] vdpa: multiple address spaces support
  2022-03-04  6:30       ` Gautam Dawar
@ 2022-03-04 17:45         ` Eugenio Perez Martin
  0 siblings, 0 replies; 136+ messages in thread
From: Eugenio Perez Martin @ 2022-03-04 17:45 UTC (permalink / raw)
  To: Gautam Dawar
  Cc: Martin Petrus Hubertus Habets, Harpreet Singh Anand,
	Tanuj Murlidhar Kamde, Jason Wang, Michael S. Tsirkin,
	Zhu Lingshan, Stefano Garzarella, Xie Yongji, Eli Cohen,
	Si-Wei Liu, Parav Pandit, Longpeng, virtualization, linux-kernel,
	kvm list, netdev

On Fri, Mar 4, 2022 at 7:30 AM Gautam Dawar <gdawar@xilinx.com> wrote:
>
> -----Original Message-----
> From: Eugenio Perez Martin <eperezma@redhat.com>
> Sent: Friday, March 4, 2022 1:10 AM
> To: Gautam Dawar <gdawar@xilinx.com>
> Cc: Gautam Dawar <gdawar@xilinx.com>; Martin Petrus Hubertus Habets <martinh@xilinx.com>; Harpreet Singh Anand <hanand@xilinx.com>; Tanuj Murlidhar Kamde <tanujk@xilinx.com>; Jason Wang <jasowang@redhat.com>; Michael S. Tsirkin <mst@redhat.com>; Zhu Lingshan <lingshan.zhu@intel.com>; Stefano Garzarella <sgarzare@redhat.com>; Xie Yongji <xieyongji@bytedance.com>; Eli Cohen <elic@nvidia.com>; Si-Wei Liu <si-wei.liu@oracle.com>; Parav Pandit <parav@nvidia.com>; Longpeng <longpeng2@huawei.com>; virtualization <virtualization@lists.linux-foundation.org>; linux-kernel@vger.kernel.org; kvm list <kvm@vger.kernel.org>; netdev@vger.kernel.org
> Subject: Re: [RFC PATCH v2 06/19] vdpa: multiple address spaces support
>
> On Thu, Feb 24, 2022 at 10:25 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
> >
> > This patches introduces the multiple address spaces support for vDPA
> > device. This idea is to identify a specific address space via an
> > dedicated identifier - ASID.
> >
> > During vDPA device allocation, vDPA device driver needs to report the
> > number of address spaces supported by the device then the DMA mapping
> > ops of the vDPA device needs to be extended to support ASID.
> >
> > This helps to isolate the environments for the virtqueue that will not
> > be assigned directly. E.g in the case of virtio-net, the control
> > virtqueue will not be assigned directly to guest.
> >
> > As a start, simply claim 1 virtqueue groups and 1 address spaces for
> > all vDPA devices. And vhost-vDPA will simply reject the device with
> > more than 1 virtqueue groups or address spaces.
> >
> > Signed-off-by: Jason Wang <jasowang@redhat.com>
> > Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> > ---
> >  drivers/vdpa/ifcvf/ifcvf_main.c   |  2 +-
> >  drivers/vdpa/mlx5/net/mlx5_vnet.c |  5 +++--
> >  drivers/vdpa/vdpa.c               |  4 +++-
> >  drivers/vdpa/vdpa_sim/vdpa_sim.c  | 10 ++++++----
> >  drivers/vhost/vdpa.c              | 14 +++++++++-----
> >  include/linux/vdpa.h              | 28 +++++++++++++++++++---------
> >  6 files changed, 41 insertions(+), 22 deletions(-)
> >
> > diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c
> > b/drivers/vdpa/ifcvf/ifcvf_main.c index c815a2e62440..a4815c5612f9
> > 100644
> > --- a/drivers/vdpa/ifcvf/ifcvf_main.c
> > +++ b/drivers/vdpa/ifcvf/ifcvf_main.c
> > @@ -513,7 +513,7 @@ static int ifcvf_vdpa_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
> >         pdev = ifcvf_mgmt_dev->pdev;
> >         dev = &pdev->dev;
> >         adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa,
> > -                                   dev, &ifc_vdpa_ops, 1, name, false);
> > +                                   dev, &ifc_vdpa_ops, 1, 1, name,
> > + false);
> >         if (IS_ERR(adapter)) {
> >                 IFCVF_ERR(pdev, "Failed to allocate vDPA structure");
> >                 return PTR_ERR(adapter); diff --git
> > a/drivers/vdpa/mlx5/net/mlx5_vnet.c
> > b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> > index fcfc28460b72..a76417892ef3 100644
> > --- a/drivers/vdpa/mlx5/net/mlx5_vnet.c
> > +++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c
> > @@ -2282,7 +2282,8 @@ static u32 mlx5_vdpa_get_generation(struct vdpa_device *vdev)
> >         return mvdev->generation;
> >  }
> >
> > -static int mlx5_vdpa_set_map(struct vdpa_device *vdev, struct
> > vhost_iotlb *iotlb)
> > +static int mlx5_vdpa_set_map(struct vdpa_device *vdev, unsigned int asid,
> > +                            struct vhost_iotlb *iotlb)
> >  {
> >         struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
> >         bool change_map;
> > @@ -2581,7 +2582,7 @@ static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
> >         }
> >
> >         ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
> > -                                1, name, false);
> > +                                1, 1, name, false);
> >         if (IS_ERR(ndev))
> >                 return PTR_ERR(ndev);
> >
> > diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c index
> > a07bf0130559..1793dc12b208 100644
> > --- a/drivers/vdpa/vdpa.c
> > +++ b/drivers/vdpa/vdpa.c
> > @@ -160,6 +160,7 @@ static void vdpa_release_dev(struct device *d)
> >   * @parent: the parent device
> >   * @config: the bus operations that is supported by this device
> >   * @ngroups: number of groups supported by this device
> > + * @nas: number of address spaces supported by this device
> >   * @size: size of the parent structure that contains private data
> >   * @name: name of the vdpa device; optional.
> >   * @use_va: indicate whether virtual address must be used by this
> > device @@ -172,7 +173,7 @@ static void vdpa_release_dev(struct device *d)
> >   */
> >  struct vdpa_device *__vdpa_alloc_device(struct device *parent,
> >                                         const struct vdpa_config_ops *config,
> > -                                       unsigned int ngroups,
> > +                                       unsigned int ngroups, unsigned
> > + int nas,
> >                                         size_t size, const char *name,
> >                                         bool use_va)  { @@ -206,6
> > +207,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
> >         vdev->features_valid = false;
> >         vdev->use_va = use_va;
> >         vdev->ngroups = ngroups;
> > +       vdev->nas = nas;
> >
> >         if (name)
> >                 err = dev_set_name(&vdev->dev, "%s", name); diff --git
> > a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> > index c98cb1f869fa..659e2e2e4b0c 100644
> > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
> > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> > @@ -251,7 +251,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
> >                 ops = &vdpasim_config_ops;
> >
> >         vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, 1,
> > -                                   dev_attr->name, false);
> > +                                   1, dev_attr->name, false);
> >         if (IS_ERR(vdpasim)) {
> >                 ret = PTR_ERR(vdpasim);
> >                 goto err_alloc;
> > @@ -539,7 +539,7 @@ static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
> >         return range;
> >  }
> >
> > -static int vdpasim_set_map(struct vdpa_device *vdpa,
> > +static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int
> > +asid,
> >                            struct vhost_iotlb *iotlb)  {
> >         struct vdpasim *vdpasim = vdpa_to_sim(vdpa); @@ -566,7 +566,8
> > @@ static int vdpasim_set_map(struct vdpa_device *vdpa,
> >         return ret;
> >  }
> >
> > -static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64
> > size,
> > +static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
> > +                          u64 iova, u64 size,
> >                            u64 pa, u32 perm, void *opaque)  {
> >         struct vdpasim *vdpasim = vdpa_to_sim(vdpa); @@ -580,7 +581,8
> > @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
> >         return ret;
> >  }
> >
> > -static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64
> > size)
> > +static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
> > +                            u64 iova, u64 size)
> >  {
> >         struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
> >
> > diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index
> > 655ff7029401..6bf755f84d26 100644
> > --- a/drivers/vhost/vdpa.c
> > +++ b/drivers/vhost/vdpa.c
> > @@ -599,10 +599,10 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
> >                 return r;
> >
> >         if (ops->dma_map) {
> > -               r = ops->dma_map(vdpa, iova, size, pa, perm, opaque);
> > +               r = ops->dma_map(vdpa, 0, iova, size, pa, perm,
> > + opaque);
> >         } else if (ops->set_map) {
> >                 if (!v->in_batch)
> > -                       r = ops->set_map(vdpa, iotlb);
> > +                       r = ops->set_map(vdpa, 0, iotlb);
> >         } else {
> >                 r = iommu_map(v->domain, iova, pa, size,
> >                               perm_to_iommu_flags(perm)); @@ -628,10
> > +628,10 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
> >         vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
> >
> >         if (ops->dma_map) {
> > -               ops->dma_unmap(vdpa, iova, size);
> > +               ops->dma_unmap(vdpa, 0, iova, size);
> >         } else if (ops->set_map) {
> >                 if (!v->in_batch)
> > -                       ops->set_map(vdpa, iotlb);
> > +                       ops->set_map(vdpa, 0, iotlb);
> >         } else {
> >                 iommu_unmap(v->domain, iova, size);
> >         }
> > @@ -863,7 +863,7 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
> >                 break;
> >         case VHOST_IOTLB_BATCH_END:
> >                 if (v->in_batch && ops->set_map)
> > -                       ops->set_map(vdpa, iotlb);
> > +                       ops->set_map(vdpa, 0, iotlb);
> >                 v->in_batch = false;
> >                 break;
> >         default:
> > @@ -1128,6 +1128,10 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
> >         int minor;
> >         int r;
> >
> > +       /* Only support 1 address space and 1 groups */
> > +       if (vdpa->ngroups != 1 || vdpa->nas != 1)
> > +               return -EOPNOTSUPP;
> > +
> >         v = kzalloc(sizeof(*v), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
> >         if (!v)
> >                 return -ENOMEM;
> > diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h index
> > 026b7ad72ed7..de22ca1a8ef3 100644
> > --- a/include/linux/vdpa.h
> > +++ b/include/linux/vdpa.h
> > @@ -69,6 +69,8 @@ struct vdpa_mgmt_dev;
> >   * @cf_mutex: Protects get and set access to configuration layout.
> >   * @index: device index
> >   * @features_valid: were features initialized? for legacy guests
> > + * @ngroups: the number of virtqueue groups
> > + * @nas: the number of address spaces
> >   * @use_va: indicate whether virtual address must be used by this device
> >   * @nvqs: maximum number of supported virtqueues
> >   * @mdev: management device pointer; caller must setup when
> > registering device as part @@ -86,6 +88,7 @@ struct vdpa_device {
> >         int nvqs;
> >         struct vdpa_mgmt_dev *mdev;
> >         unsigned int ngroups;
> > +       unsigned int nas;
> >  };
> >
> >  /**
> > @@ -240,6 +243,7 @@ struct vdpa_map_file {
> >   *                             Needed for device that using device
> >   *                             specific DMA translation (on-chip IOMMU)
> >   *                             @vdev: vdpa device
> > + *                             @asid: address space identifier
> >   *                             @iotlb: vhost memory mapping to be
> >   *                             used by the vDPA
> >   *                             Returns integer: success (0) or error (< 0)
> > @@ -248,6 +252,7 @@ struct vdpa_map_file {
> >   *                             specific DMA translation (on-chip IOMMU)
> >   *                             and preferring incremental map.
> >   *                             @vdev: vdpa device
> > + *                             @asid: address space identifier
> >   *                             @iova: iova to be mapped
> >   *                             @size: size of the area
> >   *                             @pa: physical address for the map
> > @@ -259,6 +264,7 @@ struct vdpa_map_file {
> >   *                             specific DMA translation (on-chip IOMMU)
> >   *                             and preferring incremental unmap.
> >   *                             @vdev: vdpa device
> > + *                             @asid: address space identifier
> >   *                             @iova: iova to be unmapped
> >   *                             @size: size of the area
> >   *                             Returns integer: success (0) or error (< 0)
> > @@ -309,10 +315,12 @@ struct vdpa_config_ops {
> >         struct vdpa_iova_range (*get_iova_range)(struct vdpa_device
> > *vdev);
> >
> >         /* DMA ops */
> > -       int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
> > -       int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
> > -                      u64 pa, u32 perm, void *opaque);
> > -       int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
> > +       int (*set_map)(struct vdpa_device *vdev, unsigned int asid,
> > +                      struct vhost_iotlb *iotlb);
> > +       int (*dma_map)(struct vdpa_device *vdev, unsigned int asid,
> > +                      u64 iova, u64 size, u64 pa, u32 perm, void *opaque);
> > +       int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
> > +                        u64 iova, u64 size);
> >
> >         /* Free device resources */
> >         void (*free)(struct vdpa_device *vdev); @@ -320,7 +328,7 @@
> > struct vdpa_config_ops {
> >
> >  struct vdpa_device *__vdpa_alloc_device(struct device *parent,
> >                                         const struct vdpa_config_ops *config,
> > -                                       unsigned int ngroups,
> > +                                       unsigned int ngroups, unsigned
> > + int nas,
> >                                         size_t size, const char *name,
> >                                         bool use_va);
> >
> > @@ -332,17 +340,19 @@ struct vdpa_device *__vdpa_alloc_device(struct device *parent,
> >   * @parent: the parent device
> >   * @config: the bus operations that is supported by this device
> >   * @ngroups: the number of virtqueue groups supported by this device
> > + * @nas: the number of address spaces
> >   * @name: name of the vdpa device
> >   * @use_va: indicate whether virtual address must be used by this device
> >   *
> >   * Return allocated data structure or ERR_PTR upon error
> >   */
> > -#define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, name, use_va)   \
> > +#define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, nas, \
> > +                         name, use_va) \
> >                           container_of((__vdpa_alloc_device( \
> > -                                      parent, config, ngroups, \
> > -                                      sizeof(dev_struct) + \
> > +                                      parent, config, ngroups, nas, \
> > +                                      (sizeof(dev_struct) + \
>
> Maybe too nitpick or I'm missing something, but do we need to add the parentheses around (sizeof(dev_struct) + BUILD_BUG_ON_ZERO(...)) ?
> [GD>>] Yes, that's required as without it checkpatch reports "ERROR: Macros with complex values should be enclosed in parentheses"

Interestingly, I cannot reproduce locally. But it's not something that
matters a lot in my opinion.

Thanks!

>
> >                                        BUILD_BUG_ON_ZERO(offsetof( \
> > -                                      dev_struct, member)), name, use_va)), \
> > +                                      dev_struct, member))), name,
> > + use_va)), \
> >                                        dev_struct, member)
> >
> >  int vdpa_register_device(struct vdpa_device *vdev, int nvqs);
> > --
> > 2.25.0
> >
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* RE: [RFC PATCH v2 07/19] vdpa: introduce config operations for associating ASID to a virtqueue group
  2022-03-04  9:54     ` Eugenio Perez Martin
@ 2022-03-04 17:48       ` Gautam Dawar
  0 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-03-04 17:48 UTC (permalink / raw)
  To: Eugenio Perez Martin
  Cc: Martin Petrus Hubertus Habets, Harpreet Singh Anand,
	Tanuj Murlidhar Kamde, Jason Wang, Michael S. Tsirkin,
	Zhu Lingshan, Stefano Garzarella, Xie Yongji, Eli Cohen,
	Si-Wei Liu, Parav Pandit, Longpeng, virtualization, linux-kernel,
	kvm list, netdev

-----Original Message-----
From: Eugenio Perez Martin <eperezma@redhat.com> 
Sent: Friday, March 4, 2022 3:25 PM
To: Gautam Dawar <gdawar@xilinx.com>
Cc: Gautam Dawar <gdawar@xilinx.com>; Martin Petrus Hubertus Habets <martinh@xilinx.com>; Harpreet Singh Anand <hanand@xilinx.com>; Tanuj Murlidhar Kamde <tanujk@xilinx.com>; Jason Wang <jasowang@redhat.com>; Michael S. Tsirkin <mst@redhat.com>; Zhu Lingshan <lingshan.zhu@intel.com>; Stefano Garzarella <sgarzare@redhat.com>; Xie Yongji <xieyongji@bytedance.com>; Eli Cohen <elic@nvidia.com>; Si-Wei Liu <si-wei.liu@oracle.com>; Parav Pandit <parav@nvidia.com>; Longpeng <longpeng2@huawei.com>; virtualization <virtualization@lists.linux-foundation.org>; linux-kernel@vger.kernel.org; kvm list <kvm@vger.kernel.org>; netdev@vger.kernel.org
Subject: Re: [RFC PATCH v2 07/19] vdpa: introduce config operations for associating ASID to a virtqueue group

On Thu, Feb 24, 2022 at 10:25 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
>
> This patch introduces a new bus operation to allow the vDPA bus driver 
> to associate an ASID to a virtqueue group.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>  include/linux/vdpa.h | 10 ++++++++++
>  1 file changed, 10 insertions(+)
>
> diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h index 
> de22ca1a8ef3..7386860c3995 100644
> --- a/include/linux/vdpa.h
> +++ b/include/linux/vdpa.h
> @@ -239,6 +239,12 @@ struct vdpa_map_file {
>   *                             @vdev: vdpa device
>   *                             Returns the iova range supported by
>   *                             the device.
> + * @set_group_asid:            Set address space identifier for a
> + *                             virtqueue group
> + *                             @vdev: vdpa device
> + *                             @group: virtqueue group
> + *                             @asid: address space id for this group
> + *                             Returns integer: success (0) or error (< 0)
>   * @set_map:                   Set device memory mapping (optional)
>   *                             Needed for device that using device
>   *                             specific DMA translation (on-chip IOMMU)
> @@ -321,6 +327,10 @@ struct vdpa_config_ops {
>                        u64 iova, u64 size, u64 pa, u32 perm, void *opaque);
>         int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
>                          u64 iova, u64 size);
> +       int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group,
> +                             unsigned int asid);
> +
> +

Nit again, and Jason's patch already contained these, but I think these two blank lines are introduced unintentionally.
[GD>>] Will fix this in the next revision.

>
>         /* Free device resources */
>         void (*free)(struct vdpa_device *vdev);
> --
> 2.25.0
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* RE: [RFC PATCH v2 09/19] vhost: support ASID in IOTLB API
  2022-03-04 10:25     ` Eugenio Perez Martin
@ 2022-03-04 17:52       ` Gautam Dawar
  0 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-03-04 17:52 UTC (permalink / raw)
  To: Eugenio Perez Martin
  Cc: Martin Petrus Hubertus Habets, Harpreet Singh Anand,
	Tanuj Murlidhar Kamde, Jason Wang, Michael S. Tsirkin,
	Zhu Lingshan, Stefano Garzarella, Xie Yongji, Eli Cohen,
	Si-Wei Liu, Parav Pandit, Longpeng, virtualization, linux-kernel,
	kvm list, netdev

-----Original Message-----
From: Eugenio Perez Martin <eperezma@redhat.com> 
Sent: Friday, March 4, 2022 3:55 PM
To: Gautam Dawar <gdawar@xilinx.com>
Cc: Gautam Dawar <gdawar@xilinx.com>; Martin Petrus Hubertus Habets <martinh@xilinx.com>; Harpreet Singh Anand <hanand@xilinx.com>; Tanuj Murlidhar Kamde <tanujk@xilinx.com>; Jason Wang <jasowang@redhat.com>; Michael S. Tsirkin <mst@redhat.com>; Zhu Lingshan <lingshan.zhu@intel.com>; Stefano Garzarella <sgarzare@redhat.com>; Xie Yongji <xieyongji@bytedance.com>; Eli Cohen <elic@nvidia.com>; Si-Wei Liu <si-wei.liu@oracle.com>; Parav Pandit <parav@nvidia.com>; Longpeng <longpeng2@huawei.com>; virtualization <virtualization@lists.linux-foundation.org>; linux-kernel@vger.kernel.org; kvm list <kvm@vger.kernel.org>; netdev@vger.kernel.org
Subject: Re: [RFC PATCH v2 09/19] vhost: support ASID in IOTLB API

On Thu, Feb 24, 2022 at 10:26 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
>
> This patches allows userspace to send ASID based IOTLB message to 
> vhost. This idea is to use the reserved u32 field in the existing V2 
> IOTLB message. Vhost device should advertise this capability via 
> VHOST_BACKEND_F_IOTLB_ASID backend feature.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>  drivers/vhost/vdpa.c             |  5 ++++-
>  drivers/vhost/vhost.c            | 23 ++++++++++++++++++-----
>  drivers/vhost/vhost.h            |  4 ++--
>  include/uapi/linux/vhost_types.h |  6 +++++-
>  4 files changed, 29 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index 
> 6bf755f84d26..d0aacc0cc79a 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -836,7 +836,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
>                                  msg->perm);  }
>
> -static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
> +static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 
> +asid,
>                                         struct vhost_iotlb_msg *msg)  
> {
>         struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, 
> vdev); @@ -847,6 +847,9 @@ static int 
> vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
>
>         mutex_lock(&dev->mutex);
>
> +       if (asid != 0)
> +               return -EINVAL;
> +

Should we check for asid != 0 before acquiring the mutex? Otherwise the code never releases it.
[GD>>] Yes, you are right. Will fix this in next revision.

>         r = vhost_dev_check_owner(dev);
>         if (r)
>                 goto unlock;
> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 
> 59edb5a1ffe2..1f514d98f0de 100644
> --- a/drivers/vhost/vhost.c
> +++ b/drivers/vhost/vhost.c
> @@ -468,7 +468,7 @@ void vhost_dev_init(struct vhost_dev *dev,
>                     struct vhost_virtqueue **vqs, int nvqs,
>                     int iov_limit, int weight, int byte_weight,
>                     bool use_worker,
> -                   int (*msg_handler)(struct vhost_dev *dev,
> +                   int (*msg_handler)(struct vhost_dev *dev, u32 
> + asid,
>                                        struct vhost_iotlb_msg *msg))  
> {
>         struct vhost_virtqueue *vq;
> @@ -1090,11 +1090,14 @@ static bool umem_access_ok(u64 uaddr, u64 size, int access)
>         return true;
>  }
>
> -static int vhost_process_iotlb_msg(struct vhost_dev *dev,
> +static int vhost_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
>                                    struct vhost_iotlb_msg *msg)  {
>         int ret = 0;
>
> +       if (asid != 0)
> +               return -EINVAL;
> +
>         mutex_lock(&dev->mutex);
>         vhost_dev_lock_vqs(dev);
>         switch (msg->type) {
> @@ -1141,6 +1144,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
>         struct vhost_iotlb_msg msg;
>         size_t offset;
>         int type, ret;
> +       u32 asid = 0;
>
>         ret = copy_from_iter(&type, sizeof(type), from);
>         if (ret != sizeof(type)) {
> @@ -1156,7 +1160,16 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
>                 offset = offsetof(struct vhost_msg, iotlb) - sizeof(int);
>                 break;
>         case VHOST_IOTLB_MSG_V2:
> -               offset = sizeof(__u32);
> +               if (vhost_backend_has_feature(dev->vqs[0],
> +                                             VHOST_BACKEND_F_IOTLB_ASID)) {
> +                       ret = copy_from_iter(&asid, sizeof(asid), from);
> +                       if (ret != sizeof(asid)) {
> +                               ret = -EINVAL;
> +                               goto done;
> +                       }
> +                       offset = sizeof(__u16);
> +               } else
> +                       offset = sizeof(__u32);
>                 break;
>         default:
>                 ret = -EINVAL;
> @@ -1171,9 +1184,9 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
>         }
>
>         if (dev->msg_handler)
> -               ret = dev->msg_handler(dev, &msg);
> +               ret = dev->msg_handler(dev, asid, &msg);
>         else
> -               ret = vhost_process_iotlb_msg(dev, &msg);
> +               ret = vhost_process_iotlb_msg(dev, asid, &msg);
>         if (ret) {
>                 ret = -EFAULT;
>                 goto done;
> diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index 
> 638bb640d6b4..9f238d6c7b58 100644
> --- a/drivers/vhost/vhost.h
> +++ b/drivers/vhost/vhost.h
> @@ -161,7 +161,7 @@ struct vhost_dev {
>         int byte_weight;
>         u64 kcov_handle;
>         bool use_worker;
> -       int (*msg_handler)(struct vhost_dev *dev,
> +       int (*msg_handler)(struct vhost_dev *dev, u32 asid,
>                            struct vhost_iotlb_msg *msg);  };
>
> @@ -169,7 +169,7 @@ bool vhost_exceeds_weight(struct vhost_virtqueue 
> *vq, int pkts, int total_len);  void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
>                     int nvqs, int iov_limit, int weight, int byte_weight,
>                     bool use_worker,
> -                   int (*msg_handler)(struct vhost_dev *dev,
> +                   int (*msg_handler)(struct vhost_dev *dev, u32 
> + asid,
>                                        struct vhost_iotlb_msg *msg));  
> long vhost_dev_set_owner(struct vhost_dev *dev);  bool 
> vhost_dev_has_owner(struct vhost_dev *dev); diff --git 
> a/include/uapi/linux/vhost_types.h b/include/uapi/linux/vhost_types.h
> index 76ee7016c501..634cee485abb 100644
> --- a/include/uapi/linux/vhost_types.h
> +++ b/include/uapi/linux/vhost_types.h
> @@ -87,7 +87,7 @@ struct vhost_msg {
>
>  struct vhost_msg_v2 {
>         __u32 type;
> -       __u32 reserved;
> +       __u32 asid;
>         union {
>                 struct vhost_iotlb_msg iotlb;
>                 __u8 padding[64];
> @@ -157,5 +157,9 @@ struct vhost_vdpa_iova_range {  #define 
> VHOST_BACKEND_F_IOTLB_MSG_V2 0x1
>  /* IOTLB can accept batching hints */  #define 
> VHOST_BACKEND_F_IOTLB_BATCH  0x2
> +/* IOTLB can accept address space identifier through V2 type of IOTLB
> + * message
> + */
> +#define VHOST_BACKEND_F_IOTLB_ASID  0x3
>
>  #endif
> --
> 2.25.0
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [RFC PATCH v2 10/19] vhost-vdpa: introduce asid based IOTLB
  2022-02-24 21:22   ` [RFC PATCH v2 10/19] vhost-vdpa: introduce asid based IOTLB Gautam Dawar
@ 2022-03-04 17:56     ` Eugenio Perez Martin
  2022-03-07 10:07       ` Gautam Dawar
  0 siblings, 1 reply; 136+ messages in thread
From: Eugenio Perez Martin @ 2022-03-04 17:56 UTC (permalink / raw)
  To: Gautam Dawar
  Cc: Gautam Dawar, Martin Petrus Hubertus Habets,
	Harpreet Singh Anand, Tanuj Murlidhar Kamde, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm list, netdev

On Thu, Feb 24, 2022 at 10:26 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
>
> This patch converts the vhost-vDPA device to support multiple IOTLBs
> tagged via ASID via hlist. This will be used for supporting multiple
> address spaces in the following patches.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>  drivers/vhost/vdpa.c | 104 ++++++++++++++++++++++++++++++++-----------
>  1 file changed, 79 insertions(+), 25 deletions(-)
>
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> index d0aacc0cc79a..4e8b7c4809cd 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -33,13 +33,21 @@ enum {
>
>  #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
>
> +#define VHOST_VDPA_IOTLB_BUCKETS 16
> +
> +struct vhost_vdpa_as {
> +       struct hlist_node hash_link;
> +       struct vhost_iotlb iotlb;
> +       u32 id;
> +};
> +
>  struct vhost_vdpa {
>         struct vhost_dev vdev;
>         struct iommu_domain *domain;
>         struct vhost_virtqueue *vqs;
>         struct completion completion;
>         struct vdpa_device *vdpa;
> -       struct vhost_iotlb *iotlb;
> +       struct hlist_head as[VHOST_VDPA_IOTLB_BUCKETS];
>         struct device dev;
>         struct cdev cdev;
>         atomic_t opened;
> @@ -49,12 +57,64 @@ struct vhost_vdpa {
>         struct eventfd_ctx *config_ctx;
>         int in_batch;
>         struct vdpa_iova_range range;
> +       int used_as;

This member is only modified to count the number of AS, but is never
read again. In patch 15/19 is deleted. Could we avoid introducing it
in all the series?

This is also in the previous series, but it was declared useless if I
recall correctly.

>  };
>
>  static DEFINE_IDA(vhost_vdpa_ida);
>
>  static dev_t vhost_vdpa_major;
>
> +static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
> +{
> +       struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
> +       struct vhost_vdpa_as *as;
> +
> +       hlist_for_each_entry(as, head, hash_link)
> +               if (as->id == asid)
> +                       return as;
> +
> +       return NULL;
> +}
> +
> +static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
> +{
> +       struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
> +       struct vhost_vdpa_as *as;
> +
> +       if (asid_to_as(v, asid))
> +               return NULL;
> +
> +       as = kmalloc(sizeof(*as), GFP_KERNEL);
> +       if (!as)
> +               return NULL;
> +
> +       vhost_iotlb_init(&as->iotlb, 0, 0);
> +       as->id = asid;
> +       hlist_add_head(&as->hash_link, head);
> +       ++v->used_as;
> +
> +       return as;
> +}
> +
> +static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
> +{
> +       struct vhost_vdpa_as *as = asid_to_as(v, asid);
> +
> +       /* Remove default address space is not allowed */
> +       if (asid == 0)
> +               return -EINVAL;

We must remove address space id 0 so we don't leak it at
vhost_vdpa_cleanup. I think the check could go away with no need of
moving it.

> +
> +       if (!as)
> +               return -EINVAL;
> +
> +       hlist_del(&as->hash_link);
> +       vhost_iotlb_reset(&as->iotlb);
> +       kfree(as);
> +       --v->used_as;
> +
> +       return 0;
> +}
> +
>  static void handle_vq_kick(struct vhost_work *work)
>  {
>         struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
> @@ -554,15 +614,6 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
>         return vhost_vdpa_pa_unmap(v, iotlb, start, last);
>  }
>
> -static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v)
> -{
> -       struct vhost_iotlb *iotlb = v->iotlb;
> -
> -       vhost_vdpa_iotlb_unmap(v, iotlb, 0ULL, 0ULL - 1);
> -       kfree(v->iotlb);
> -       v->iotlb = NULL;
> -}
> -
>  static int perm_to_iommu_flags(u32 perm)
>  {
>         int flags = 0;
> @@ -842,7 +893,8 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
>         struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
>         struct vdpa_device *vdpa = v->vdpa;
>         const struct vdpa_config_ops *ops = vdpa->config;
> -       struct vhost_iotlb *iotlb = v->iotlb;
> +       struct vhost_vdpa_as *as = asid_to_as(v, 0);
> +       struct vhost_iotlb *iotlb = &as->iotlb;
>         int r = 0;
>
>         mutex_lock(&dev->mutex);
> @@ -953,6 +1005,13 @@ static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v)
>         }
>  }
>
> +static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
> +{
> +       vhost_dev_cleanup(&v->vdev);
> +       kfree(v->vdev.vqs);
> +       vhost_vdpa_remove_as(v, 0);
> +}
> +
>  static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>  {
>         struct vhost_vdpa *v;
> @@ -985,15 +1044,12 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>         vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
>                        vhost_vdpa_process_iotlb_msg);
>
> -       v->iotlb = vhost_iotlb_alloc(0, 0);
> -       if (!v->iotlb) {
> -               r = -ENOMEM;
> -               goto err_init_iotlb;
> -       }
> +       if (!vhost_vdpa_alloc_as(v, 0))
> +               goto err_alloc_as;
>
>         r = vhost_vdpa_alloc_domain(v);
>         if (r)
> -               goto err_alloc_domain;
> +               goto err_alloc_as;
>
>         vhost_vdpa_set_iova_range(v);
>
> @@ -1001,11 +1057,8 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>
>         return 0;
>
> -err_alloc_domain:
> -       vhost_vdpa_iotlb_free(v);
> -err_init_iotlb:
> -       vhost_dev_cleanup(&v->vdev);
> -       kfree(vqs);
> +err_alloc_as:
> +       vhost_vdpa_cleanup(v);
>  err:
>         atomic_dec(&v->opened);
>         return r;
> @@ -1029,11 +1082,9 @@ static int vhost_vdpa_release(struct inode *inode, struct file *filep)
>         vhost_vdpa_clean_irq(v);
>         vhost_vdpa_reset(v);
>         vhost_dev_stop(&v->vdev);
> -       vhost_vdpa_iotlb_free(v);
>         vhost_vdpa_free_domain(v);
>         vhost_vdpa_config_put(v);
>         vhost_dev_cleanup(&v->vdev);
> -       kfree(v->vdev.vqs);
>         mutex_unlock(&d->mutex);
>
>         atomic_dec(&v->opened);
> @@ -1129,7 +1180,7 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>         const struct vdpa_config_ops *ops = vdpa->config;
>         struct vhost_vdpa *v;
>         int minor;
> -       int r;
> +       int i, r;
>
>         /* Only support 1 address space and 1 groups */
>         if (vdpa->ngroups != 1 || vdpa->nas != 1)
> @@ -1177,6 +1228,9 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>         init_completion(&v->completion);
>         vdpa_set_drvdata(vdpa, v);
>
> +       for (i = 0; i < VHOST_VDPA_IOTLB_BUCKETS; i++)
> +               INIT_HLIST_HEAD(&v->as[i]);
> +
>         return 0;
>
>  err:
> --
> 2.25.0
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [RFC PATCH v2 15/19] vhost-vdpa: support ASID based IOTLB API
  2022-02-24 21:22   ` [RFC PATCH v2 15/19] vhost-vdpa: support ASID based IOTLB API Gautam Dawar
@ 2022-03-04 18:04     ` Eugenio Perez Martin
  2022-03-07 10:23       ` Gautam Dawar
  0 siblings, 1 reply; 136+ messages in thread
From: Eugenio Perez Martin @ 2022-03-04 18:04 UTC (permalink / raw)
  To: Gautam Dawar
  Cc: Gautam Dawar, Martin Petrus Hubertus Habets,
	Harpreet Singh Anand, Tanuj Murlidhar Kamde, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm list, netdev

On Thu, Feb 24, 2022 at 10:28 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
>
> This patch extends the vhost-vdpa to support ASID based IOTLB API. The
> vhost-vdpa device will allocated multiple IOTLBs for vDPA device that
> supports multiple address spaces. The IOTLBs and vDPA device memory
> mappings is determined and maintained through ASID.
>
> Note that we still don't support vDPA device with more than one
> address spaces that depends on platform IOMMU. This work will be done
> by moving the IOMMU logic from vhost-vDPA to vDPA device driver.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>  drivers/vhost/vdpa.c  | 129 ++++++++++++++++++++++++++++++++----------
>  drivers/vhost/vhost.c |   2 +-
>  2 files changed, 100 insertions(+), 31 deletions(-)
>
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> index 47e6cf9d0881..4bcf824e3b12 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -28,7 +28,8 @@
>  enum {
>         VHOST_VDPA_BACKEND_FEATURES =
>         (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) |
> -       (1ULL << VHOST_BACKEND_F_IOTLB_BATCH),
> +       (1ULL << VHOST_BACKEND_F_IOTLB_BATCH) |
> +       (1ULL << VHOST_BACKEND_F_IOTLB_ASID),
>  };
>
>  #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
> @@ -57,13 +58,20 @@ struct vhost_vdpa {
>         struct eventfd_ctx *config_ctx;
>         int in_batch;
>         struct vdpa_iova_range range;
> -       int used_as;
> +       u32 batch_asid;
>  };
>
>  static DEFINE_IDA(vhost_vdpa_ida);
>
>  static dev_t vhost_vdpa_major;
>
> +static inline u32 iotlb_to_asid(struct vhost_iotlb *iotlb)
> +{
> +       struct vhost_vdpa_as *as = container_of(iotlb, struct
> +                                               vhost_vdpa_as, iotlb);
> +       return as->id;
> +}
> +
>  static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
>  {
>         struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
> @@ -76,6 +84,16 @@ static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
>         return NULL;
>  }
>
> +static struct vhost_iotlb *asid_to_iotlb(struct vhost_vdpa *v, u32 asid)
> +{
> +       struct vhost_vdpa_as *as = asid_to_as(v, asid);
> +
> +       if (!as)
> +               return NULL;
> +
> +       return &as->iotlb;
> +}
> +
>  static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
>  {
>         struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
> @@ -84,6 +102,9 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
>         if (asid_to_as(v, asid))
>                 return NULL;
>
> +       if (asid >= v->vdpa->nas)
> +               return NULL;
> +
>         as = kmalloc(sizeof(*as), GFP_KERNEL);
>         if (!as)
>                 return NULL;
> @@ -91,18 +112,24 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
>         vhost_iotlb_init(&as->iotlb, 0, 0);
>         as->id = asid;
>         hlist_add_head(&as->hash_link, head);
> -       ++v->used_as;
>
>         return as;
>  }
>
> -static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
> +static struct vhost_vdpa_as *vhost_vdpa_find_alloc_as(struct vhost_vdpa *v,
> +                                                     u32 asid)
>  {
>         struct vhost_vdpa_as *as = asid_to_as(v, asid);
>
> -       /* Remove default address space is not allowed */
> -       if (asid == 0)
> -               return -EINVAL;
> +       if (as)
> +               return as;
> +
> +       return vhost_vdpa_alloc_as(v, asid);
> +}
> +
> +static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
> +{
> +       struct vhost_vdpa_as *as = asid_to_as(v, asid);
>
>         if (!as)
>                 return -EINVAL;
> @@ -110,7 +137,6 @@ static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
>         hlist_del(&as->hash_link);
>         vhost_iotlb_reset(&as->iotlb);
>         kfree(as);
> -       --v->used_as;
>
>         return 0;
>  }
> @@ -665,6 +691,7 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
>         struct vhost_dev *dev = &v->vdev;
>         struct vdpa_device *vdpa = v->vdpa;
>         const struct vdpa_config_ops *ops = vdpa->config;
> +       u32 asid = iotlb_to_asid(iotlb);
>         int r = 0;
>
>         r = vhost_iotlb_add_range_ctx(iotlb, iova, iova + size - 1,
> @@ -673,10 +700,10 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
>                 return r;
>
>         if (ops->dma_map) {
> -               r = ops->dma_map(vdpa, 0, iova, size, pa, perm, opaque);
> +               r = ops->dma_map(vdpa, asid, iova, size, pa, perm, opaque);
>         } else if (ops->set_map) {
>                 if (!v->in_batch)
> -                       r = ops->set_map(vdpa, 0, iotlb);
> +                       r = ops->set_map(vdpa, asid, iotlb);
>         } else {
>                 r = iommu_map(v->domain, iova, pa, size,
>                               perm_to_iommu_flags(perm));
> @@ -692,23 +719,35 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
>         return 0;
>  }
>
> -static void vhost_vdpa_unmap(struct vhost_vdpa *v,
> -                            struct vhost_iotlb *iotlb,
> -                            u64 iova, u64 size)
> +static int vhost_vdpa_unmap(struct vhost_vdpa *v,
> +                           struct vhost_iotlb *iotlb,
> +                           u64 iova, u64 size)
>  {
>         struct vdpa_device *vdpa = v->vdpa;
>         const struct vdpa_config_ops *ops = vdpa->config;
> +       u32 asid = iotlb_to_asid(iotlb);
> +
> +       if (!iotlb)
> +               return -EINVAL;

I think there is no need of checking for this. Similar functions
assume the caller will pass non-null arguments, and
vhost_vdpa_process_iotlb_msg does it.

With that into account, I think there is little point in making this
function return something different than void.

Apart from that, iotlb is already used before checking for NULL.

>
>         vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
>
>         if (ops->dma_map) {
> -               ops->dma_unmap(vdpa, 0, iova, size);
> +               ops->dma_unmap(vdpa, asid, iova, size);
>         } else if (ops->set_map) {
>                 if (!v->in_batch)
> -                       ops->set_map(vdpa, 0, iotlb);
> +                       ops->set_map(vdpa, asid, iotlb);
>         } else {
>                 iommu_unmap(v->domain, iova, size);
>         }
> +
> +       /* If we are in the middle of batch processing, delay the free
> +        * of AS until BATCH_END.
> +        */
> +       if (!v->in_batch && !iotlb->nmaps)
> +               vhost_vdpa_remove_as(v, asid);
> +
> +       return 0;
>  }
>
>  static int vhost_vdpa_va_map(struct vhost_vdpa *v,
> @@ -916,33 +955,55 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
>         struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
>         struct vdpa_device *vdpa = v->vdpa;
>         const struct vdpa_config_ops *ops = vdpa->config;
> -       struct vhost_vdpa_as *as = asid_to_as(v, 0);
> -       struct vhost_iotlb *iotlb = &as->iotlb;
> +       struct vhost_iotlb *iotlb = NULL;
> +       struct vhost_vdpa_as *as = NULL;
>         int r = 0;
>
>         mutex_lock(&dev->mutex);
>
> -       if (asid != 0)
> -               return -EINVAL;
> -
>         r = vhost_dev_check_owner(dev);
>         if (r)
>                 goto unlock;
>
> +       if (msg->type == VHOST_IOTLB_UPDATE ||
> +           msg->type == VHOST_IOTLB_BATCH_BEGIN) {
> +               as = vhost_vdpa_find_alloc_as(v, asid);
> +               if (!as) {
> +                       dev_err(&v->dev, "can't find and alloc asid %d\n",
> +                               asid);
> +                       return -EINVAL;
> +               }
> +               iotlb = &as->iotlb;
> +       } else
> +               iotlb = asid_to_iotlb(v, asid);
> +
> +       if ((v->in_batch && v->batch_asid != asid) || !iotlb) {
> +               if (v->in_batch && v->batch_asid != asid) {
> +                       dev_info(&v->dev, "batch id %d asid %d\n",
> +                                v->batch_asid, asid);
> +               }
> +               if (!iotlb)
> +                       dev_err(&v->dev, "no iotlb for asid %d\n", asid);
> +               return -EINVAL;
> +       }
> +
>         switch (msg->type) {
>         case VHOST_IOTLB_UPDATE:
>                 r = vhost_vdpa_process_iotlb_update(v, iotlb, msg);
>                 break;
>         case VHOST_IOTLB_INVALIDATE:
> -               vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
> +               r = vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
>                 break;
>         case VHOST_IOTLB_BATCH_BEGIN:
> +               v->batch_asid = asid;
>                 v->in_batch = true;
>                 break;
>         case VHOST_IOTLB_BATCH_END:
>                 if (v->in_batch && ops->set_map)
> -                       ops->set_map(vdpa, 0, iotlb);
> +                       ops->set_map(vdpa, asid, iotlb);
>                 v->in_batch = false;
> +               if (!iotlb->nmaps)
> +                       vhost_vdpa_remove_as(v, asid);
>                 break;
>         default:
>                 r = -EINVAL;
> @@ -1030,9 +1091,17 @@ static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v)
>
>  static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
>  {
> +       struct vhost_vdpa_as *as;
> +       u32 asid;
> +
>         vhost_dev_cleanup(&v->vdev);
>         kfree(v->vdev.vqs);
> -       vhost_vdpa_remove_as(v, 0);
> +
> +       for (asid = 0; asid < v->vdpa->nas; asid++) {
> +               as = asid_to_as(v, asid);
> +               if (as)
> +                       vhost_vdpa_remove_as(v, asid);
> +       }
>  }
>
>  static int vhost_vdpa_open(struct inode *inode, struct file *filep)
> @@ -1067,12 +1136,9 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>         vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
>                        vhost_vdpa_process_iotlb_msg);
>
> -       if (!vhost_vdpa_alloc_as(v, 0))
> -               goto err_alloc_as;
> -
>         r = vhost_vdpa_alloc_domain(v);
>         if (r)
> -               goto err_alloc_as;
> +               goto err_alloc_domain;
>
>         vhost_vdpa_set_iova_range(v);
>
> @@ -1080,7 +1146,7 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>
>         return 0;
>
> -err_alloc_as:
> +err_alloc_domain:
>         vhost_vdpa_cleanup(v);
>  err:
>         atomic_dec(&v->opened);
> @@ -1205,8 +1271,11 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>         int minor;
>         int i, r;
>
> -       /* Only support 1 address space and 1 groups */
> -       if (vdpa->ngroups != 1 || vdpa->nas != 1)
> +       /* We can't support platform IOMMU device with more than 1
> +        * group or as
> +        */
> +       if (!ops->set_map && !ops->dma_map &&
> +           (vdpa->ngroups > 1 || vdpa->nas > 1))
>                 return -EOPNOTSUPP;
>
>         v = kzalloc(sizeof(*v), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> index 1f514d98f0de..92eeb684c84d 100644
> --- a/drivers/vhost/vhost.c
> +++ b/drivers/vhost/vhost.c
> @@ -1167,7 +1167,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
>                                 ret = -EINVAL;
>                                 goto done;
>                         }
> -                       offset = sizeof(__u16);
> +                       offset = 0;
>                 } else
>                         offset = sizeof(__u32);
>                 break;
> --
> 2.25.0
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* RE: [RFC PATCH v2 10/19] vhost-vdpa: introduce asid based IOTLB
  2022-03-04 17:56     ` Eugenio Perez Martin
@ 2022-03-07 10:07       ` Gautam Dawar
  0 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-03-07 10:07 UTC (permalink / raw)
  To: Eugenio Perez Martin
  Cc: Martin Petrus Hubertus Habets, Harpreet Singh Anand,
	Tanuj Murlidhar Kamde, Jason Wang, Michael S. Tsirkin,
	Zhu Lingshan, Stefano Garzarella, Xie Yongji, Eli Cohen,
	Si-Wei Liu, Parav Pandit, Longpeng, virtualization, linux-kernel,
	kvm list, netdev

-----Original Message-----
From: Eugenio Perez Martin <eperezma@redhat.com> 
Sent: Friday, March 4, 2022 11:26 PM
To: Gautam Dawar <gdawar@xilinx.com>
Cc: Gautam Dawar <gdawar@xilinx.com>; Martin Petrus Hubertus Habets <martinh@xilinx.com>; Harpreet Singh Anand <hanand@xilinx.com>; Tanuj Murlidhar Kamde <tanujk@xilinx.com>; Jason Wang <jasowang@redhat.com>; Michael S. Tsirkin <mst@redhat.com>; Zhu Lingshan <lingshan.zhu@intel.com>; Stefano Garzarella <sgarzare@redhat.com>; Xie Yongji <xieyongji@bytedance.com>; Eli Cohen <elic@nvidia.com>; Si-Wei Liu <si-wei.liu@oracle.com>; Parav Pandit <parav@nvidia.com>; Longpeng <longpeng2@huawei.com>; virtualization <virtualization@lists.linux-foundation.org>; linux-kernel@vger.kernel.org; kvm list <kvm@vger.kernel.org>; netdev@vger.kernel.org
Subject: Re: [RFC PATCH v2 10/19] vhost-vdpa: introduce asid based IOTLB

On Thu, Feb 24, 2022 at 10:26 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
>
> This patch converts the vhost-vDPA device to support multiple IOTLBs 
> tagged via ASID via hlist. This will be used for supporting multiple 
> address spaces in the following patches.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>  drivers/vhost/vdpa.c | 104 
> ++++++++++++++++++++++++++++++++-----------
>  1 file changed, 79 insertions(+), 25 deletions(-)
>
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index 
> d0aacc0cc79a..4e8b7c4809cd 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -33,13 +33,21 @@ enum {
>
>  #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
>
> +#define VHOST_VDPA_IOTLB_BUCKETS 16
> +
> +struct vhost_vdpa_as {
> +       struct hlist_node hash_link;
> +       struct vhost_iotlb iotlb;
> +       u32 id;
> +};
> +
>  struct vhost_vdpa {
>         struct vhost_dev vdev;
>         struct iommu_domain *domain;
>         struct vhost_virtqueue *vqs;
>         struct completion completion;
>         struct vdpa_device *vdpa;
> -       struct vhost_iotlb *iotlb;
> +       struct hlist_head as[VHOST_VDPA_IOTLB_BUCKETS];
>         struct device dev;
>         struct cdev cdev;
>         atomic_t opened;
> @@ -49,12 +57,64 @@ struct vhost_vdpa {
>         struct eventfd_ctx *config_ctx;
>         int in_batch;
>         struct vdpa_iova_range range;
> +       int used_as;

This member is only modified to count the number of AS, but is never read again. In patch 15/19 is deleted. Could we avoid introducing it in all the series?

This is also in the previous series, but it was declared useless if I recall correctly.
[GD>>] Yes, it makes sense to remove used_as structure member. Will do.

>  };
>
>  static DEFINE_IDA(vhost_vdpa_ida);
>
>  static dev_t vhost_vdpa_major;
>
> +static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 
> +asid) {
> +       struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
> +       struct vhost_vdpa_as *as;
> +
> +       hlist_for_each_entry(as, head, hash_link)
> +               if (as->id == asid)
> +                       return as;
> +
> +       return NULL;
> +}
> +
> +static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa 
> +*v, u32 asid) {
> +       struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
> +       struct vhost_vdpa_as *as;
> +
> +       if (asid_to_as(v, asid))
> +               return NULL;
> +
> +       as = kmalloc(sizeof(*as), GFP_KERNEL);
> +       if (!as)
> +               return NULL;
> +
> +       vhost_iotlb_init(&as->iotlb, 0, 0);
> +       as->id = asid;
> +       hlist_add_head(&as->hash_link, head);
> +       ++v->used_as;
> +
> +       return as;
> +}
> +
> +static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid) {
> +       struct vhost_vdpa_as *as = asid_to_as(v, asid);
> +
> +       /* Remove default address space is not allowed */
> +       if (asid == 0)
> +               return -EINVAL;

We must remove address space id 0 so we don't leak it at vhost_vdpa_cleanup. I think the check could go away with no need of moving it.
[GD>>] Good catch. Will remove this check to allow freeing "as" for asid 0 in vhost_vdpa_cleanup()

> +
> +       if (!as)
> +               return -EINVAL;
> +
> +       hlist_del(&as->hash_link);
> +       vhost_iotlb_reset(&as->iotlb);
> +       kfree(as);
> +       --v->used_as;
> +
> +       return 0;
> +}
> +
>  static void handle_vq_kick(struct vhost_work *work)  {
>         struct vhost_virtqueue *vq = container_of(work, struct 
> vhost_virtqueue, @@ -554,15 +614,6 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
>         return vhost_vdpa_pa_unmap(v, iotlb, start, last);  }
>
> -static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v) -{
> -       struct vhost_iotlb *iotlb = v->iotlb;
> -
> -       vhost_vdpa_iotlb_unmap(v, iotlb, 0ULL, 0ULL - 1);
> -       kfree(v->iotlb);
> -       v->iotlb = NULL;
> -}
> -
>  static int perm_to_iommu_flags(u32 perm)  {
>         int flags = 0;
> @@ -842,7 +893,8 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
>         struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
>         struct vdpa_device *vdpa = v->vdpa;
>         const struct vdpa_config_ops *ops = vdpa->config;
> -       struct vhost_iotlb *iotlb = v->iotlb;
> +       struct vhost_vdpa_as *as = asid_to_as(v, 0);
> +       struct vhost_iotlb *iotlb = &as->iotlb;
>         int r = 0;
>
>         mutex_lock(&dev->mutex);
> @@ -953,6 +1005,13 @@ static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v)
>         }
>  }
>
> +static void vhost_vdpa_cleanup(struct vhost_vdpa *v) {
> +       vhost_dev_cleanup(&v->vdev);
> +       kfree(v->vdev.vqs);
> +       vhost_vdpa_remove_as(v, 0);
> +}
> +
>  static int vhost_vdpa_open(struct inode *inode, struct file *filep)  
> {
>         struct vhost_vdpa *v;
> @@ -985,15 +1044,12 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>         vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
>                        vhost_vdpa_process_iotlb_msg);
>
> -       v->iotlb = vhost_iotlb_alloc(0, 0);
> -       if (!v->iotlb) {
> -               r = -ENOMEM;
> -               goto err_init_iotlb;
> -       }
> +       if (!vhost_vdpa_alloc_as(v, 0))
> +               goto err_alloc_as;
>
>         r = vhost_vdpa_alloc_domain(v);
>         if (r)
> -               goto err_alloc_domain;
> +               goto err_alloc_as;
>
>         vhost_vdpa_set_iova_range(v);
>
> @@ -1001,11 +1057,8 @@ static int vhost_vdpa_open(struct inode *inode, 
> struct file *filep)
>
>         return 0;
>
> -err_alloc_domain:
> -       vhost_vdpa_iotlb_free(v);
> -err_init_iotlb:
> -       vhost_dev_cleanup(&v->vdev);
> -       kfree(vqs);
> +err_alloc_as:
> +       vhost_vdpa_cleanup(v);
>  err:
>         atomic_dec(&v->opened);
>         return r;
> @@ -1029,11 +1082,9 @@ static int vhost_vdpa_release(struct inode *inode, struct file *filep)
>         vhost_vdpa_clean_irq(v);
>         vhost_vdpa_reset(v);
>         vhost_dev_stop(&v->vdev);
> -       vhost_vdpa_iotlb_free(v);
>         vhost_vdpa_free_domain(v);
>         vhost_vdpa_config_put(v);
>         vhost_dev_cleanup(&v->vdev);
> -       kfree(v->vdev.vqs);
>         mutex_unlock(&d->mutex);
>
>         atomic_dec(&v->opened);
> @@ -1129,7 +1180,7 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>         const struct vdpa_config_ops *ops = vdpa->config;
>         struct vhost_vdpa *v;
>         int minor;
> -       int r;
> +       int i, r;
>
>         /* Only support 1 address space and 1 groups */
>         if (vdpa->ngroups != 1 || vdpa->nas != 1) @@ -1177,6 +1228,9 
> @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>         init_completion(&v->completion);
>         vdpa_set_drvdata(vdpa, v);
>
> +       for (i = 0; i < VHOST_VDPA_IOTLB_BUCKETS; i++)
> +               INIT_HLIST_HEAD(&v->as[i]);
> +
>         return 0;
>
>  err:
> --
> 2.25.0
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* RE: [RFC PATCH v2 15/19] vhost-vdpa: support ASID based IOTLB API
  2022-03-04 18:04     ` Eugenio Perez Martin
@ 2022-03-07 10:23       ` Gautam Dawar
  0 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-03-07 10:23 UTC (permalink / raw)
  To: Eugenio Perez Martin
  Cc: Martin Petrus Hubertus Habets, Harpreet Singh Anand,
	Tanuj Murlidhar Kamde, Jason Wang, Michael S. Tsirkin,
	Zhu Lingshan, Stefano Garzarella, Xie Yongji, Eli Cohen,
	Si-Wei Liu, Parav Pandit, Longpeng, virtualization, linux-kernel,
	kvm list, netdev

-----Original Message-----
From: Eugenio Perez Martin <eperezma@redhat.com> 
Sent: Friday, March 4, 2022 11:35 PM
To: Gautam Dawar <gdawar@xilinx.com>
Cc: Gautam Dawar <gdawar@xilinx.com>; Martin Petrus Hubertus Habets <martinh@xilinx.com>; Harpreet Singh Anand <hanand@xilinx.com>; Tanuj Murlidhar Kamde <tanujk@xilinx.com>; Jason Wang <jasowang@redhat.com>; Michael S. Tsirkin <mst@redhat.com>; Zhu Lingshan <lingshan.zhu@intel.com>; Stefano Garzarella <sgarzare@redhat.com>; Xie Yongji <xieyongji@bytedance.com>; Eli Cohen <elic@nvidia.com>; Si-Wei Liu <si-wei.liu@oracle.com>; Parav Pandit <parav@nvidia.com>; Longpeng <longpeng2@huawei.com>; virtualization <virtualization@lists.linux-foundation.org>; linux-kernel@vger.kernel.org; kvm list <kvm@vger.kernel.org>; netdev@vger.kernel.org
Subject: Re: [RFC PATCH v2 15/19] vhost-vdpa: support ASID based IOTLB API

On Thu, Feb 24, 2022 at 10:28 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
>
> This patch extends the vhost-vdpa to support ASID based IOTLB API. The 
> vhost-vdpa device will allocated multiple IOTLBs for vDPA device that 
> supports multiple address spaces. The IOTLBs and vDPA device memory 
> mappings is determined and maintained through ASID.
>
> Note that we still don't support vDPA device with more than one 
> address spaces that depends on platform IOMMU. This work will be done 
> by moving the IOMMU logic from vhost-vDPA to vDPA device driver.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>  drivers/vhost/vdpa.c  | 129 ++++++++++++++++++++++++++++++++----------
>  drivers/vhost/vhost.c |   2 +-
>  2 files changed, 100 insertions(+), 31 deletions(-)
>
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index 
> 47e6cf9d0881..4bcf824e3b12 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -28,7 +28,8 @@
>  enum {
>         VHOST_VDPA_BACKEND_FEATURES =
>         (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) |
> -       (1ULL << VHOST_BACKEND_F_IOTLB_BATCH),
> +       (1ULL << VHOST_BACKEND_F_IOTLB_BATCH) |
> +       (1ULL << VHOST_BACKEND_F_IOTLB_ASID),
>  };
>
>  #define VHOST_VDPA_DEV_MAX (1U << MINORBITS) @@ -57,13 +58,20 @@ 
> struct vhost_vdpa {
>         struct eventfd_ctx *config_ctx;
>         int in_batch;
>         struct vdpa_iova_range range;
> -       int used_as;
> +       u32 batch_asid;
>  };
>
>  static DEFINE_IDA(vhost_vdpa_ida);
>
>  static dev_t vhost_vdpa_major;
>
> +static inline u32 iotlb_to_asid(struct vhost_iotlb *iotlb) {
> +       struct vhost_vdpa_as *as = container_of(iotlb, struct
> +                                               vhost_vdpa_as, iotlb);
> +       return as->id;
> +}
> +
>  static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 
> asid)  {
>         struct hlist_head *head = &v->as[asid % 
> VHOST_VDPA_IOTLB_BUCKETS]; @@ -76,6 +84,16 @@ static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
>         return NULL;
>  }
>
> +static struct vhost_iotlb *asid_to_iotlb(struct vhost_vdpa *v, u32 
> +asid) {
> +       struct vhost_vdpa_as *as = asid_to_as(v, asid);
> +
> +       if (!as)
> +               return NULL;
> +
> +       return &as->iotlb;
> +}
> +
>  static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa 
> *v, u32 asid)  {
>         struct hlist_head *head = &v->as[asid % 
> VHOST_VDPA_IOTLB_BUCKETS]; @@ -84,6 +102,9 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
>         if (asid_to_as(v, asid))
>                 return NULL;
>
> +       if (asid >= v->vdpa->nas)
> +               return NULL;
> +
>         as = kmalloc(sizeof(*as), GFP_KERNEL);
>         if (!as)
>                 return NULL;
> @@ -91,18 +112,24 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
>         vhost_iotlb_init(&as->iotlb, 0, 0);
>         as->id = asid;
>         hlist_add_head(&as->hash_link, head);
> -       ++v->used_as;
>
>         return as;
>  }
>
> -static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
> +static struct vhost_vdpa_as *vhost_vdpa_find_alloc_as(struct vhost_vdpa *v,
> +                                                     u32 asid)
>  {
>         struct vhost_vdpa_as *as = asid_to_as(v, asid);
>
> -       /* Remove default address space is not allowed */
> -       if (asid == 0)
> -               return -EINVAL;
> +       if (as)
> +               return as;
> +
> +       return vhost_vdpa_alloc_as(v, asid); }
> +
> +static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid) {
> +       struct vhost_vdpa_as *as = asid_to_as(v, asid);
>
>         if (!as)
>                 return -EINVAL;
> @@ -110,7 +137,6 @@ static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
>         hlist_del(&as->hash_link);
>         vhost_iotlb_reset(&as->iotlb);
>         kfree(as);
> -       --v->used_as;
>
>         return 0;
>  }
> @@ -665,6 +691,7 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
>         struct vhost_dev *dev = &v->vdev;
>         struct vdpa_device *vdpa = v->vdpa;
>         const struct vdpa_config_ops *ops = vdpa->config;
> +       u32 asid = iotlb_to_asid(iotlb);
>         int r = 0;
>
>         r = vhost_iotlb_add_range_ctx(iotlb, iova, iova + size - 1, @@ 
> -673,10 +700,10 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
>                 return r;
>
>         if (ops->dma_map) {
> -               r = ops->dma_map(vdpa, 0, iova, size, pa, perm, opaque);
> +               r = ops->dma_map(vdpa, asid, iova, size, pa, perm, 
> + opaque);
>         } else if (ops->set_map) {
>                 if (!v->in_batch)
> -                       r = ops->set_map(vdpa, 0, iotlb);
> +                       r = ops->set_map(vdpa, asid, iotlb);
>         } else {
>                 r = iommu_map(v->domain, iova, pa, size,
>                               perm_to_iommu_flags(perm)); @@ -692,23 
> +719,35 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
>         return 0;
>  }
>
> -static void vhost_vdpa_unmap(struct vhost_vdpa *v,
> -                            struct vhost_iotlb *iotlb,
> -                            u64 iova, u64 size)
> +static int vhost_vdpa_unmap(struct vhost_vdpa *v,
> +                           struct vhost_iotlb *iotlb,
> +                           u64 iova, u64 size)
>  {
>         struct vdpa_device *vdpa = v->vdpa;
>         const struct vdpa_config_ops *ops = vdpa->config;
> +       u32 asid = iotlb_to_asid(iotlb);
> +
> +       if (!iotlb)
> +               return -EINVAL;

I think there is no need of checking for this. Similar functions assume the caller will pass non-null arguments, and vhost_vdpa_process_iotlb_msg does it.

With that into account, I think there is little point in making this function return something different than void.

Apart from that, iotlb is already used before checking for NULL.
[GD>>] Agreed. Will remove the unnecessary check and change the return type to void.

>
>         vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
>
>         if (ops->dma_map) {
> -               ops->dma_unmap(vdpa, 0, iova, size);
> +               ops->dma_unmap(vdpa, asid, iova, size);
>         } else if (ops->set_map) {
>                 if (!v->in_batch)
> -                       ops->set_map(vdpa, 0, iotlb);
> +                       ops->set_map(vdpa, asid, iotlb);
>         } else {
>                 iommu_unmap(v->domain, iova, size);
>         }
> +
> +       /* If we are in the middle of batch processing, delay the free
> +        * of AS until BATCH_END.
> +        */
> +       if (!v->in_batch && !iotlb->nmaps)
> +               vhost_vdpa_remove_as(v, asid);
> +
> +       return 0;
>  }
>
>  static int vhost_vdpa_va_map(struct vhost_vdpa *v, @@ -916,33 +955,55 
> @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
>         struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
>         struct vdpa_device *vdpa = v->vdpa;
>         const struct vdpa_config_ops *ops = vdpa->config;
> -       struct vhost_vdpa_as *as = asid_to_as(v, 0);
> -       struct vhost_iotlb *iotlb = &as->iotlb;
> +       struct vhost_iotlb *iotlb = NULL;
> +       struct vhost_vdpa_as *as = NULL;
>         int r = 0;
>
>         mutex_lock(&dev->mutex);
>
> -       if (asid != 0)
> -               return -EINVAL;
> -
>         r = vhost_dev_check_owner(dev);
>         if (r)
>                 goto unlock;
>
> +       if (msg->type == VHOST_IOTLB_UPDATE ||
> +           msg->type == VHOST_IOTLB_BATCH_BEGIN) {
> +               as = vhost_vdpa_find_alloc_as(v, asid);
> +               if (!as) {
> +                       dev_err(&v->dev, "can't find and alloc asid %d\n",
> +                               asid);
> +                       return -EINVAL;
> +               }
> +               iotlb = &as->iotlb;
> +       } else
> +               iotlb = asid_to_iotlb(v, asid);
> +
> +       if ((v->in_batch && v->batch_asid != asid) || !iotlb) {
> +               if (v->in_batch && v->batch_asid != asid) {
> +                       dev_info(&v->dev, "batch id %d asid %d\n",
> +                                v->batch_asid, asid);
> +               }
> +               if (!iotlb)
> +                       dev_err(&v->dev, "no iotlb for asid %d\n", asid);
> +               return -EINVAL;
> +       }
> +
>         switch (msg->type) {
>         case VHOST_IOTLB_UPDATE:
>                 r = vhost_vdpa_process_iotlb_update(v, iotlb, msg);
>                 break;
>         case VHOST_IOTLB_INVALIDATE:
> -               vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
> +               r = vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
>                 break;
>         case VHOST_IOTLB_BATCH_BEGIN:
> +               v->batch_asid = asid;
>                 v->in_batch = true;
>                 break;
>         case VHOST_IOTLB_BATCH_END:
>                 if (v->in_batch && ops->set_map)
> -                       ops->set_map(vdpa, 0, iotlb);
> +                       ops->set_map(vdpa, asid, iotlb);
>                 v->in_batch = false;
> +               if (!iotlb->nmaps)
> +                       vhost_vdpa_remove_as(v, asid);
>                 break;
>         default:
>                 r = -EINVAL;
> @@ -1030,9 +1091,17 @@ static void vhost_vdpa_set_iova_range(struct 
> vhost_vdpa *v)
>
>  static void vhost_vdpa_cleanup(struct vhost_vdpa *v)  {
> +       struct vhost_vdpa_as *as;
> +       u32 asid;
> +
>         vhost_dev_cleanup(&v->vdev);
>         kfree(v->vdev.vqs);
> -       vhost_vdpa_remove_as(v, 0);
> +
> +       for (asid = 0; asid < v->vdpa->nas; asid++) {
> +               as = asid_to_as(v, asid);
> +               if (as)
> +                       vhost_vdpa_remove_as(v, asid);
> +       }
>  }
>
>  static int vhost_vdpa_open(struct inode *inode, struct file *filep) 
> @@ -1067,12 +1136,9 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>         vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
>                        vhost_vdpa_process_iotlb_msg);
>
> -       if (!vhost_vdpa_alloc_as(v, 0))
> -               goto err_alloc_as;
> -
>         r = vhost_vdpa_alloc_domain(v);
>         if (r)
> -               goto err_alloc_as;
> +               goto err_alloc_domain;
>
>         vhost_vdpa_set_iova_range(v);
>
> @@ -1080,7 +1146,7 @@ static int vhost_vdpa_open(struct inode *inode, 
> struct file *filep)
>
>         return 0;
>
> -err_alloc_as:
> +err_alloc_domain:
>         vhost_vdpa_cleanup(v);
>  err:
>         atomic_dec(&v->opened);
> @@ -1205,8 +1271,11 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>         int minor;
>         int i, r;
>
> -       /* Only support 1 address space and 1 groups */
> -       if (vdpa->ngroups != 1 || vdpa->nas != 1)
> +       /* We can't support platform IOMMU device with more than 1
> +        * group or as
> +        */
> +       if (!ops->set_map && !ops->dma_map &&
> +           (vdpa->ngroups > 1 || vdpa->nas > 1))
>                 return -EOPNOTSUPP;
>
>         v = kzalloc(sizeof(*v), GFP_KERNEL | __GFP_RETRY_MAYFAIL); 
> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 
> 1f514d98f0de..92eeb684c84d 100644
> --- a/drivers/vhost/vhost.c
> +++ b/drivers/vhost/vhost.c
> @@ -1167,7 +1167,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
>                                 ret = -EINVAL;
>                                 goto done;
>                         }
> -                       offset = sizeof(__u16);
> +                       offset = 0;
>                 } else
>                         offset = sizeof(__u32);
>                 break;
> --
> 2.25.0
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [RFC PATCH v2 16/19] vdpa_sim: advertise VIRTIO_NET_F_MTU
  2022-02-24 21:22   ` [RFC PATCH v2 16/19] vdpa_sim: advertise VIRTIO_NET_F_MTU Gautam Dawar
@ 2022-03-10 17:55     ` Eugenio Perez Martin
  0 siblings, 0 replies; 136+ messages in thread
From: Eugenio Perez Martin @ 2022-03-10 17:55 UTC (permalink / raw)
  To: Gautam Dawar
  Cc: Gautam Dawar, Martin Petrus Hubertus Habets,
	Harpreet Singh Anand, Tanuj Murlidhar Kamde, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm list, netdev

On Thu, Feb 24, 2022 at 10:28 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
>
> We've already reported maximum mtu via config space, so let's
> advertise the feature.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>  drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> index d5324f6fd8c7..ff22cc56f40b 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> @@ -26,7 +26,8 @@
>  #define DRV_LICENSE  "GPL v2"
>
>  #define VDPASIM_NET_FEATURES   (VDPASIM_FEATURES | \
> -                                (1ULL << VIRTIO_NET_F_MAC))
> +                                (1ULL << VIRTIO_NET_F_MAC) | \
> +                                (1ULL << VIRTIO_NET_F_MTU));

Extra semicolon at the end of macro.

Thanks.

>
>  #define VDPASIM_NET_VQ_NUM     2
>
> --
> 2.25.0
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [RFC PATCH v2 19/19] vdpasim: control virtqueue support
  2022-02-24 21:22   ` [RFC PATCH v2 19/19] vdpasim: control virtqueue support Gautam Dawar
@ 2022-03-10 18:20     ` Eugenio Perez Martin
  2022-03-18  7:35     ` Eugenio Perez Martin
  1 sibling, 0 replies; 136+ messages in thread
From: Eugenio Perez Martin @ 2022-03-10 18:20 UTC (permalink / raw)
  To: Gautam Dawar
  Cc: Gautam Dawar, Martin Petrus Hubertus Habets,
	Harpreet Singh Anand, Tanuj Murlidhar Kamde, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm list, netdev

On Thu, Feb 24, 2022 at 10:29 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
>
> This patch introduces the control virtqueue support for vDPA
> simulator. This is a requirement for supporting advanced features like
> multiqueue.
>
> A requirement for control virtqueue is to isolate its memory access
> from the rx/tx virtqueues. This is because when using vDPA device
> for VM, the control virqueue is not directly assigned to VM. Userspace
> (Qemu) will present a shadow control virtqueue to control for
> recording the device states.
>
> The isolation is done via the virtqueue groups and ASID support in
> vDPA through vhost-vdpa. The simulator is extended to have:
>
> 1) three virtqueues: RXVQ, TXVQ and CVQ (control virtqueue)
> 2) two virtqueue groups: group 0 contains RXVQ and TXVQ; group 1
>    contains CVQ
> 3) two address spaces and the simulator simply implements the address
>    spaces by mapping it 1:1 to IOTLB.
>
> For the VM use cases, userspace(Qemu) may set AS 0 to group 0 and AS 1
> to group 1. So we have:
>
> 1) The IOTLB for virtqueue group 0 contains the mappings of guest, so
>    RX and TX can be assigned to guest directly.
> 2) The IOTLB for virtqueue group 1 contains the mappings of CVQ which
>    is the buffers that allocated and managed by VMM only. So CVQ of
>    vhost-vdpa is visible to VMM only. And Guest can not access the CVQ
>    of vhost-vdpa.
>
> For the other use cases, since AS 0 is associated to all virtqueue
> groups by default. All virtqueues share the same mapping by default.
>
> To demonstrate the function, VIRITO_NET_F_CTRL_MACADDR is
> implemented in the simulator for the driver to set mac address.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>  drivers/vdpa/vdpa_sim/vdpa_sim.c     | 91 ++++++++++++++++++++++------
>  drivers/vdpa/vdpa_sim/vdpa_sim.h     |  2 +
>  drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 88 ++++++++++++++++++++++++++-
>  3 files changed, 161 insertions(+), 20 deletions(-)
>
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> index 659e2e2e4b0c..59611f18a3a8 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> @@ -96,11 +96,17 @@ static void vdpasim_do_reset(struct vdpasim *vdpasim)
>  {
>         int i;
>
> -       for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
> +       spin_lock(&vdpasim->iommu_lock);
> +
> +       for (i = 0; i < vdpasim->dev_attr.nvqs; i++) {
>                 vdpasim_vq_reset(vdpasim, &vdpasim->vqs[i]);
> +               vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
> +                                &vdpasim->iommu_lock);
> +       }
> +
> +       for (i = 0; i < vdpasim->dev_attr.nas; i++)
> +               vhost_iotlb_reset(&vdpasim->iommu[i]);
>
> -       spin_lock(&vdpasim->iommu_lock);
> -       vhost_iotlb_reset(vdpasim->iommu);
>         spin_unlock(&vdpasim->iommu_lock);
>
>         vdpasim->features = 0;
> @@ -145,7 +151,7 @@ static dma_addr_t vdpasim_map_range(struct vdpasim *vdpasim, phys_addr_t paddr,
>         dma_addr = iova_dma_addr(&vdpasim->iova, iova);
>
>         spin_lock(&vdpasim->iommu_lock);
> -       ret = vhost_iotlb_add_range(vdpasim->iommu, (u64)dma_addr,
> +       ret = vhost_iotlb_add_range(&vdpasim->iommu[0], (u64)dma_addr,
>                                     (u64)dma_addr + size - 1, (u64)paddr, perm);
>         spin_unlock(&vdpasim->iommu_lock);
>
> @@ -161,7 +167,7 @@ static void vdpasim_unmap_range(struct vdpasim *vdpasim, dma_addr_t dma_addr,
>                                 size_t size)
>  {
>         spin_lock(&vdpasim->iommu_lock);
> -       vhost_iotlb_del_range(vdpasim->iommu, (u64)dma_addr,
> +       vhost_iotlb_del_range(&vdpasim->iommu[0], (u64)dma_addr,
>                               (u64)dma_addr + size - 1);
>         spin_unlock(&vdpasim->iommu_lock);
>
> @@ -250,8 +256,9 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
>         else
>                 ops = &vdpasim_config_ops;
>
> -       vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, 1,
> -                                   1, dev_attr->name, false);
> +       vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
> +                                   dev_attr->ngroups, dev_attr->nas,
> +                                   dev_attr->name, false);
>         if (IS_ERR(vdpasim)) {
>                 ret = PTR_ERR(vdpasim);
>                 goto err_alloc;
> @@ -278,16 +285,20 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
>         if (!vdpasim->vqs)
>                 goto err_iommu;
>
> -       vdpasim->iommu = vhost_iotlb_alloc(max_iotlb_entries, 0);
> +       vdpasim->iommu = kmalloc_array(vdpasim->dev_attr.nas,
> +                                      sizeof(*vdpasim->iommu), GFP_KERNEL);
>         if (!vdpasim->iommu)
>                 goto err_iommu;
>
> +       for (i = 0; i < vdpasim->dev_attr.nas; i++)
> +               vhost_iotlb_init(&vdpasim->iommu[i], 0, 0);
> +
>         vdpasim->buffer = kvmalloc(dev_attr->buffer_size, GFP_KERNEL);
>         if (!vdpasim->buffer)
>                 goto err_iommu;
>
>         for (i = 0; i < dev_attr->nvqs; i++)
> -               vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu,
> +               vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
>                                  &vdpasim->iommu_lock);
>
>         ret = iova_cache_get();
> @@ -401,7 +412,11 @@ static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
>
>  static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
>  {
> -       return 0;
> +       /* RX and TX belongs to group 0, CVQ belongs to group 1 */
> +       if (idx == 2)
> +               return 1;
> +       else
> +               return 0;
>  }
>
>  static u64 vdpasim_get_device_features(struct vdpa_device *vdpa)
> @@ -539,20 +554,53 @@ static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
>         return range;
>  }
>
> +static int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
> +                                 unsigned int asid)
> +{
> +       struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
> +       struct vhost_iotlb *iommu;
> +       int i;
> +
> +       if (group > vdpasim->dev_attr.ngroups)
> +               return -EINVAL;
> +
> +       if (asid > vdpasim->dev_attr.nas)
> +               return -EINVAL;
> +
> +       iommu = &vdpasim->iommu[asid];
> +
> +       spin_lock(&vdpasim->lock);
> +
> +       for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
> +               if (vdpasim_get_vq_group(vdpa, i) == group)
> +                       vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
> +                                        &vdpasim->iommu_lock);
> +
> +       spin_unlock(&vdpasim->lock);
> +
> +       return 0;
> +}
> +
>  static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
>                            struct vhost_iotlb *iotlb)
>  {
>         struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>         struct vhost_iotlb_map *map;
> +       struct vhost_iotlb *iommu;
>         u64 start = 0ULL, last = 0ULL - 1;
>         int ret;
>
> +       if (asid >= vdpasim->dev_attr.nas)
> +               return -EINVAL;
> +
>         spin_lock(&vdpasim->iommu_lock);
> -       vhost_iotlb_reset(vdpasim->iommu);
> +
> +       iommu = &vdpasim->iommu[asid];
> +       vhost_iotlb_reset(iommu);
>
>         for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
>              map = vhost_iotlb_itree_next(map, start, last)) {
> -               ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
> +               ret = vhost_iotlb_add_range(iommu, map->start,
>                                             map->last, map->addr, map->perm);
>                 if (ret)
>                         goto err;
> @@ -561,7 +609,7 @@ static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
>         return 0;
>
>  err:
> -       vhost_iotlb_reset(vdpasim->iommu);
> +       vhost_iotlb_reset(iommu);
>         spin_unlock(&vdpasim->iommu_lock);
>         return ret;
>  }
> @@ -573,9 +621,12 @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
>         struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>         int ret;
>
> +       if (asid >= vdpasim->dev_attr.nas)
> +               return -EINVAL;
> +
>         spin_lock(&vdpasim->iommu_lock);
> -       ret = vhost_iotlb_add_range_ctx(vdpasim->iommu, iova, iova + size - 1,
> -                                       pa, perm, opaque);
> +       ret = vhost_iotlb_add_range_ctx(&vdpasim->iommu[asid], iova,
> +                                       iova + size - 1, pa, perm, opaque);
>         spin_unlock(&vdpasim->iommu_lock);
>
>         return ret;
> @@ -586,8 +637,11 @@ static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
>  {
>         struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>
> +       if (asid >= vdpasim->dev_attr.nas)
> +               return -EINVAL;
> +
>         spin_lock(&vdpasim->iommu_lock);
> -       vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
> +       vhost_iotlb_del_range(&vdpasim->iommu[asid], iova, iova + size - 1);
>         spin_unlock(&vdpasim->iommu_lock);
>
>         return 0;
> @@ -611,8 +665,7 @@ static void vdpasim_free(struct vdpa_device *vdpa)
>         }
>
>         kvfree(vdpasim->buffer);
> -       if (vdpasim->iommu)
> -               vhost_iotlb_free(vdpasim->iommu);
> +       vhost_iotlb_free(vdpasim->iommu);
>         kfree(vdpasim->vqs);
>         kfree(vdpasim->config);
>  }
> @@ -643,6 +696,7 @@ static const struct vdpa_config_ops vdpasim_config_ops = {
>         .set_config             = vdpasim_set_config,
>         .get_generation         = vdpasim_get_generation,
>         .get_iova_range         = vdpasim_get_iova_range,
> +       .set_group_asid         = vdpasim_set_group_asid,
>         .dma_map                = vdpasim_dma_map,
>         .dma_unmap              = vdpasim_dma_unmap,
>         .free                   = vdpasim_free,
> @@ -674,6 +728,7 @@ static const struct vdpa_config_ops vdpasim_batch_config_ops = {
>         .set_config             = vdpasim_set_config,
>         .get_generation         = vdpasim_get_generation,
>         .get_iova_range         = vdpasim_get_iova_range,
> +       .set_group_asid         = vdpasim_set_group_asid,
>         .set_map                = vdpasim_set_map,
>         .free                   = vdpasim_free,
>  };
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> index 0be7c1e7ef80..622782e92239 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> @@ -41,6 +41,8 @@ struct vdpasim_dev_attr {
>         size_t buffer_size;
>         int nvqs;
>         u32 id;
> +       u32 ngroups;
> +       u32 nas;
>
>         work_func_t work_fn;
>         void (*get_config)(struct vdpasim *vdpasim, void *config);
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> index ed5ade4ae570..513970c05af2 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> @@ -26,10 +26,15 @@
>  #define DRV_LICENSE  "GPL v2"
>
>  #define VDPASIM_NET_FEATURES   (VDPASIM_FEATURES | \
> +                                (1ULL << VIRTIO_NET_F_MTU) | \

Why the reorder here?

>                                  (1ULL << VIRTIO_NET_F_MAC) | \
> -                                (1ULL << VIRTIO_NET_F_MTU));
> +                                (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
> +                                (1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR));
>
> -#define VDPASIM_NET_VQ_NUM     2
> +/* 3 virtqueues, 2 address spaces, 2 virtqueue groups */
> +#define VDPASIM_NET_VQ_NUM     3
> +#define VDPASIM_NET_AS_NUM     2
> +#define VDPASIM_NET_GROUP_NUM  2
>
>  static void vdpasim_net_complete(struct vdpasim_virtqueue *vq, size_t len)
>  {
> @@ -63,6 +68,81 @@ static bool receive_filter(struct vdpasim *vdpasim, size_t len)
>         return false;
>  }
>
> +static virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
> +                                                  u8 cmd)
> +{
> +       struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
> +       virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
> +       size_t read;
> +
> +       switch (cmd) {
> +       case VIRTIO_NET_CTRL_MAC_ADDR_SET:
> +               read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov,
> +                                            (void *)vdpasim->config.mac,

This is not valid anymore as config is of type void *. Need to convert
to virtio_net_config. This also occurs in receive_filter.

You can copy how vdpasim_net_setup_config does it, for example.

Thanks!

> +                                            ETH_ALEN);
> +               if (read == ETH_ALEN)
> +                       status = VIRTIO_NET_OK;
> +               break;
> +       default:
> +               break;
> +       }
> +
> +       return status;
> +}
> +
> +static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
> +{
> +       struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
> +       virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
> +       struct virtio_net_ctrl_hdr ctrl;
> +       size_t read, write;
> +       int err;
> +
> +       if (!(vdpasim->features & (1ULL << VIRTIO_NET_F_CTRL_VQ)))
> +               return;
> +
> +       if (!cvq->ready)
> +               return;
> +
> +       while (true) {
> +               err = vringh_getdesc_iotlb(&cvq->vring, &cvq->in_iov,
> +                                          &cvq->out_iov,
> +                                          &cvq->head, GFP_ATOMIC);
> +               if (err <= 0)
> +                       break;
> +
> +               read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov, &ctrl,
> +                                            sizeof(ctrl));
> +               if (read != sizeof(ctrl))
> +                       break;
> +
> +               switch (ctrl.class) {
> +               case VIRTIO_NET_CTRL_MAC:
> +                       status = vdpasim_handle_ctrl_mac(vdpasim, ctrl.cmd);
> +                       break;
> +               default:
> +                       break;
> +               }
> +
> +               /* Make sure data is wrote before advancing index */
> +               smp_wmb();
> +
> +               write = vringh_iov_push_iotlb(&cvq->vring, &cvq->out_iov,
> +                                             &status, sizeof(status));
> +               vringh_complete_iotlb(&cvq->vring, cvq->head, write);
> +               vringh_kiov_cleanup(&cvq->in_iov);
> +               vringh_kiov_cleanup(&cvq->out_iov);
> +
> +               /* Make sure used is visible before rasing the interrupt. */
> +               smp_wmb();
> +
> +               local_bh_disable();
> +               if (cvq->cb)
> +                       cvq->cb(cvq->private);
> +               local_bh_enable();
> +       }
> +}
> +
>  static void vdpasim_net_work(struct work_struct *work)
>  {
>         struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
> @@ -77,6 +157,8 @@ static void vdpasim_net_work(struct work_struct *work)
>         if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
>                 goto out;
>
> +       vdpasim_handle_cvq(vdpasim);
> +
>         if (!txq->ready || !rxq->ready)
>                 goto out;
>
> @@ -162,6 +244,8 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
>         dev_attr.id = VIRTIO_ID_NET;
>         dev_attr.supported_features = VDPASIM_NET_FEATURES;
>         dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
> +       dev_attr.ngroups = VDPASIM_NET_GROUP_NUM;
> +       dev_attr.nas = VDPASIM_NET_AS_NUM;
>         dev_attr.config_size = sizeof(struct virtio_net_config);
>         dev_attr.get_config = vdpasim_net_get_config;
>         dev_attr.work_fn = vdpasim_net_work;
> --
> 2.25.0
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [RFC PATCH v2 18/19] vdpa_sim: filter destination mac address
  2022-02-24 21:22   ` [RFC PATCH v2 18/19] vdpa_sim: filter destination mac address Gautam Dawar
@ 2022-03-10 18:22     ` Eugenio Perez Martin
  0 siblings, 0 replies; 136+ messages in thread
From: Eugenio Perez Martin @ 2022-03-10 18:22 UTC (permalink / raw)
  To: Gautam Dawar
  Cc: Gautam Dawar, Martin Petrus Hubertus Habets,
	Harpreet Singh Anand, Tanuj Murlidhar Kamde, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm list, netdev

On Thu, Feb 24, 2022 at 10:29 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
>
> This patch implements a simple unicast filter for vDPA simulator.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>  drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 49 ++++++++++++++++++----------
>  1 file changed, 31 insertions(+), 18 deletions(-)
>
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> index 05d552cb7f94..ed5ade4ae570 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> @@ -47,13 +47,28 @@ static void vdpasim_net_complete(struct vdpasim_virtqueue *vq, size_t len)
>         local_bh_enable();
>  }
>
> +static bool receive_filter(struct vdpasim *vdpasim, size_t len)
> +{
> +       bool modern = vdpasim->features & (1ULL << VIRTIO_F_VERSION_1);
> +       size_t hdr_len = modern ? sizeof(struct virtio_net_hdr_v1) :
> +                                 sizeof(struct virtio_net_hdr);
> +
> +       if (len < ETH_ALEN + hdr_len)
> +               return false;
> +
> +       if (!strncmp(vdpasim->buffer + hdr_len,
> +                    vdpasim->config.mac, ETH_ALEN))
> +               return true;
> +
> +       return false;
> +}
> +
>  static void vdpasim_net_work(struct work_struct *work)
>  {
>         struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
>         struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
>         struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
>         ssize_t read, write;
> -       size_t total_write;
>         int pkts = 0;
>         int err;
>
> @@ -66,36 +81,34 @@ static void vdpasim_net_work(struct work_struct *work)
>                 goto out;
>
>         while (true) {
> -               total_write = 0;
>                 err = vringh_getdesc_iotlb(&txq->vring, &txq->out_iov, NULL,
>                                            &txq->head, GFP_ATOMIC);
>                 if (err <= 0)
>                         break;
>
> +               read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
> +                                            vdpasim->buffer,
> +                                            PAGE_SIZE);
> +
> +               if (!receive_filter(vdpasim, read)) {
> +                       vdpasim_complete(txq, 0);

vdpasim_net_complete?

Thanks!

> +                       continue;
> +               }
> +
>                 err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->in_iov,
>                                            &rxq->head, GFP_ATOMIC);
>                 if (err <= 0) {
> -                       vringh_complete_iotlb(&txq->vring, txq->head, 0);
> +                       vdpasim_net_complete(txq, 0);
>                         break;
>                 }
>
> -               while (true) {
> -                       read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
> -                                                    vdpasim->buffer,
> -                                                    PAGE_SIZE);
> -                       if (read <= 0)
> -                               break;
> -
> -                       write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
> -                                                     vdpasim->buffer, read);
> -                       if (write <= 0)
> -                               break;
> -
> -                       total_write += write;
> -               }
> +               write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
> +                                             vdpasim->buffer, read);
> +               if (write <= 0)
> +                       break;
>
>                 vdpasim_net_complete(txq, 0);
> -               vdpasim_net_complete(rxq, total_write);
> +               vdpasim_net_complete(rxq, write);
>
>                 if (++pkts > 4) {
>                         schedule_work(&vdpasim->work);
> --
> 2.25.0
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* Re: [RFC PATCH v2 19/19] vdpasim: control virtqueue support
  2022-02-24 21:22   ` [RFC PATCH v2 19/19] vdpasim: control virtqueue support Gautam Dawar
  2022-03-10 18:20     ` Eugenio Perez Martin
@ 2022-03-18  7:35     ` Eugenio Perez Martin
  2022-03-22  8:46       ` Gautam Dawar
  1 sibling, 1 reply; 136+ messages in thread
From: Eugenio Perez Martin @ 2022-03-18  7:35 UTC (permalink / raw)
  To: Gautam Dawar
  Cc: Gautam Dawar, Martin Petrus Hubertus Habets,
	Harpreet Singh Anand, Tanuj Murlidhar Kamde, Jason Wang,
	Michael S. Tsirkin, Zhu Lingshan, Stefano Garzarella, Xie Yongji,
	Eli Cohen, Si-Wei Liu, Parav Pandit, Longpeng, virtualization,
	linux-kernel, kvm list, netdev

On Thu, Feb 24, 2022 at 10:29 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
>
> This patch introduces the control virtqueue support for vDPA
> simulator. This is a requirement for supporting advanced features like
> multiqueue.
>
> A requirement for control virtqueue is to isolate its memory access
> from the rx/tx virtqueues. This is because when using vDPA device
> for VM, the control virqueue is not directly assigned to VM. Userspace
> (Qemu) will present a shadow control virtqueue to control for
> recording the device states.
>
> The isolation is done via the virtqueue groups and ASID support in
> vDPA through vhost-vdpa. The simulator is extended to have:
>
> 1) three virtqueues: RXVQ, TXVQ and CVQ (control virtqueue)
> 2) two virtqueue groups: group 0 contains RXVQ and TXVQ; group 1
>    contains CVQ
> 3) two address spaces and the simulator simply implements the address
>    spaces by mapping it 1:1 to IOTLB.
>
> For the VM use cases, userspace(Qemu) may set AS 0 to group 0 and AS 1
> to group 1. So we have:
>
> 1) The IOTLB for virtqueue group 0 contains the mappings of guest, so
>    RX and TX can be assigned to guest directly.
> 2) The IOTLB for virtqueue group 1 contains the mappings of CVQ which
>    is the buffers that allocated and managed by VMM only. So CVQ of
>    vhost-vdpa is visible to VMM only. And Guest can not access the CVQ
>    of vhost-vdpa.
>
> For the other use cases, since AS 0 is associated to all virtqueue
> groups by default. All virtqueues share the same mapping by default.
>
> To demonstrate the function, VIRITO_NET_F_CTRL_MACADDR is
> implemented in the simulator for the driver to set mac address.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>  drivers/vdpa/vdpa_sim/vdpa_sim.c     | 91 ++++++++++++++++++++++------
>  drivers/vdpa/vdpa_sim/vdpa_sim.h     |  2 +
>  drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 88 ++++++++++++++++++++++++++-
>  3 files changed, 161 insertions(+), 20 deletions(-)
>
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> index 659e2e2e4b0c..59611f18a3a8 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> @@ -96,11 +96,17 @@ static void vdpasim_do_reset(struct vdpasim *vdpasim)
>  {
>         int i;
>
> -       for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
> +       spin_lock(&vdpasim->iommu_lock);
> +
> +       for (i = 0; i < vdpasim->dev_attr.nvqs; i++) {
>                 vdpasim_vq_reset(vdpasim, &vdpasim->vqs[i]);
> +               vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
> +                                &vdpasim->iommu_lock);
> +       }
> +
> +       for (i = 0; i < vdpasim->dev_attr.nas; i++)
> +               vhost_iotlb_reset(&vdpasim->iommu[i]);
>
> -       spin_lock(&vdpasim->iommu_lock);
> -       vhost_iotlb_reset(vdpasim->iommu);
>         spin_unlock(&vdpasim->iommu_lock);
>
>         vdpasim->features = 0;
> @@ -145,7 +151,7 @@ static dma_addr_t vdpasim_map_range(struct vdpasim *vdpasim, phys_addr_t paddr,
>         dma_addr = iova_dma_addr(&vdpasim->iova, iova);
>
>         spin_lock(&vdpasim->iommu_lock);
> -       ret = vhost_iotlb_add_range(vdpasim->iommu, (u64)dma_addr,
> +       ret = vhost_iotlb_add_range(&vdpasim->iommu[0], (u64)dma_addr,
>                                     (u64)dma_addr + size - 1, (u64)paddr, perm);
>         spin_unlock(&vdpasim->iommu_lock);
>
> @@ -161,7 +167,7 @@ static void vdpasim_unmap_range(struct vdpasim *vdpasim, dma_addr_t dma_addr,
>                                 size_t size)
>  {
>         spin_lock(&vdpasim->iommu_lock);
> -       vhost_iotlb_del_range(vdpasim->iommu, (u64)dma_addr,
> +       vhost_iotlb_del_range(&vdpasim->iommu[0], (u64)dma_addr,
>                               (u64)dma_addr + size - 1);
>         spin_unlock(&vdpasim->iommu_lock);
>
> @@ -250,8 +256,9 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
>         else
>                 ops = &vdpasim_config_ops;
>
> -       vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, 1,
> -                                   1, dev_attr->name, false);
> +       vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
> +                                   dev_attr->ngroups, dev_attr->nas,
> +                                   dev_attr->name, false);
>         if (IS_ERR(vdpasim)) {
>                 ret = PTR_ERR(vdpasim);
>                 goto err_alloc;
> @@ -278,16 +285,20 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
>         if (!vdpasim->vqs)
>                 goto err_iommu;
>
> -       vdpasim->iommu = vhost_iotlb_alloc(max_iotlb_entries, 0);
> +       vdpasim->iommu = kmalloc_array(vdpasim->dev_attr.nas,
> +                                      sizeof(*vdpasim->iommu), GFP_KERNEL);
>         if (!vdpasim->iommu)
>                 goto err_iommu;
>
> +       for (i = 0; i < vdpasim->dev_attr.nas; i++)
> +               vhost_iotlb_init(&vdpasim->iommu[i], 0, 0);
> +
>         vdpasim->buffer = kvmalloc(dev_attr->buffer_size, GFP_KERNEL);
>         if (!vdpasim->buffer)
>                 goto err_iommu;
>
>         for (i = 0; i < dev_attr->nvqs; i++)
> -               vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu,
> +               vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
>                                  &vdpasim->iommu_lock);
>
>         ret = iova_cache_get();
> @@ -401,7 +412,11 @@ static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
>
>  static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
>  {
> -       return 0;
> +       /* RX and TX belongs to group 0, CVQ belongs to group 1 */
> +       if (idx == 2)
> +               return 1;
> +       else
> +               return 0;
>  }
>
>  static u64 vdpasim_get_device_features(struct vdpa_device *vdpa)
> @@ -539,20 +554,53 @@ static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
>         return range;
>  }
>
> +static int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
> +                                 unsigned int asid)
> +{
> +       struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
> +       struct vhost_iotlb *iommu;
> +       int i;
> +
> +       if (group > vdpasim->dev_attr.ngroups)
> +               return -EINVAL;
> +
> +       if (asid > vdpasim->dev_attr.nas)
> +               return -EINVAL;
> +
> +       iommu = &vdpasim->iommu[asid];
> +
> +       spin_lock(&vdpasim->lock);
> +
> +       for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
> +               if (vdpasim_get_vq_group(vdpa, i) == group)
> +                       vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],

The second argument to "vringh_set_iotlb" call must be simply "iommu".
If not, we're effectively setting asid 0 to all virtqueue groups that
match "group", making it impossible to change it.

Thanks!

> +                                        &vdpasim->iommu_lock);
> +
> +       spin_unlock(&vdpasim->lock);
> +
> +       return 0;
> +}
> +
>  static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
>                            struct vhost_iotlb *iotlb)
>  {
>         struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>         struct vhost_iotlb_map *map;
> +       struct vhost_iotlb *iommu;
>         u64 start = 0ULL, last = 0ULL - 1;
>         int ret;
>
> +       if (asid >= vdpasim->dev_attr.nas)
> +               return -EINVAL;
> +
>         spin_lock(&vdpasim->iommu_lock);
> -       vhost_iotlb_reset(vdpasim->iommu);
> +
> +       iommu = &vdpasim->iommu[asid];
> +       vhost_iotlb_reset(iommu);
>
>         for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
>              map = vhost_iotlb_itree_next(map, start, last)) {
> -               ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
> +               ret = vhost_iotlb_add_range(iommu, map->start,
>                                             map->last, map->addr, map->perm);
>                 if (ret)
>                         goto err;
> @@ -561,7 +609,7 @@ static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
>         return 0;
>
>  err:
> -       vhost_iotlb_reset(vdpasim->iommu);
> +       vhost_iotlb_reset(iommu);
>         spin_unlock(&vdpasim->iommu_lock);
>         return ret;
>  }
> @@ -573,9 +621,12 @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
>         struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>         int ret;
>
> +       if (asid >= vdpasim->dev_attr.nas)
> +               return -EINVAL;
> +
>         spin_lock(&vdpasim->iommu_lock);
> -       ret = vhost_iotlb_add_range_ctx(vdpasim->iommu, iova, iova + size - 1,
> -                                       pa, perm, opaque);
> +       ret = vhost_iotlb_add_range_ctx(&vdpasim->iommu[asid], iova,
> +                                       iova + size - 1, pa, perm, opaque);
>         spin_unlock(&vdpasim->iommu_lock);
>
>         return ret;
> @@ -586,8 +637,11 @@ static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
>  {
>         struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>
> +       if (asid >= vdpasim->dev_attr.nas)
> +               return -EINVAL;
> +
>         spin_lock(&vdpasim->iommu_lock);
> -       vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
> +       vhost_iotlb_del_range(&vdpasim->iommu[asid], iova, iova + size - 1);
>         spin_unlock(&vdpasim->iommu_lock);
>
>         return 0;
> @@ -611,8 +665,7 @@ static void vdpasim_free(struct vdpa_device *vdpa)
>         }
>
>         kvfree(vdpasim->buffer);
> -       if (vdpasim->iommu)
> -               vhost_iotlb_free(vdpasim->iommu);
> +       vhost_iotlb_free(vdpasim->iommu);
>         kfree(vdpasim->vqs);
>         kfree(vdpasim->config);
>  }
> @@ -643,6 +696,7 @@ static const struct vdpa_config_ops vdpasim_config_ops = {
>         .set_config             = vdpasim_set_config,
>         .get_generation         = vdpasim_get_generation,
>         .get_iova_range         = vdpasim_get_iova_range,
> +       .set_group_asid         = vdpasim_set_group_asid,
>         .dma_map                = vdpasim_dma_map,
>         .dma_unmap              = vdpasim_dma_unmap,
>         .free                   = vdpasim_free,
> @@ -674,6 +728,7 @@ static const struct vdpa_config_ops vdpasim_batch_config_ops = {
>         .set_config             = vdpasim_set_config,
>         .get_generation         = vdpasim_get_generation,
>         .get_iova_range         = vdpasim_get_iova_range,
> +       .set_group_asid         = vdpasim_set_group_asid,
>         .set_map                = vdpasim_set_map,
>         .free                   = vdpasim_free,
>  };
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> index 0be7c1e7ef80..622782e92239 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> @@ -41,6 +41,8 @@ struct vdpasim_dev_attr {
>         size_t buffer_size;
>         int nvqs;
>         u32 id;
> +       u32 ngroups;
> +       u32 nas;
>
>         work_func_t work_fn;
>         void (*get_config)(struct vdpasim *vdpasim, void *config);
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> index ed5ade4ae570..513970c05af2 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> @@ -26,10 +26,15 @@
>  #define DRV_LICENSE  "GPL v2"
>
>  #define VDPASIM_NET_FEATURES   (VDPASIM_FEATURES | \
> +                                (1ULL << VIRTIO_NET_F_MTU) | \
>                                  (1ULL << VIRTIO_NET_F_MAC) | \
> -                                (1ULL << VIRTIO_NET_F_MTU));
> +                                (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
> +                                (1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR));
>
> -#define VDPASIM_NET_VQ_NUM     2
> +/* 3 virtqueues, 2 address spaces, 2 virtqueue groups */
> +#define VDPASIM_NET_VQ_NUM     3
> +#define VDPASIM_NET_AS_NUM     2
> +#define VDPASIM_NET_GROUP_NUM  2
>
>  static void vdpasim_net_complete(struct vdpasim_virtqueue *vq, size_t len)
>  {
> @@ -63,6 +68,81 @@ static bool receive_filter(struct vdpasim *vdpasim, size_t len)
>         return false;
>  }
>
> +static virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
> +                                                  u8 cmd)
> +{
> +       struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
> +       virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
> +       size_t read;
> +
> +       switch (cmd) {
> +       case VIRTIO_NET_CTRL_MAC_ADDR_SET:
> +               read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov,
> +                                            (void *)vdpasim->config.mac,
> +                                            ETH_ALEN);
> +               if (read == ETH_ALEN)
> +                       status = VIRTIO_NET_OK;
> +               break;
> +       default:
> +               break;
> +       }
> +
> +       return status;
> +}
> +
> +static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
> +{
> +       struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
> +       virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
> +       struct virtio_net_ctrl_hdr ctrl;
> +       size_t read, write;
> +       int err;
> +
> +       if (!(vdpasim->features & (1ULL << VIRTIO_NET_F_CTRL_VQ)))
> +               return;
> +
> +       if (!cvq->ready)
> +               return;
> +
> +       while (true) {
> +               err = vringh_getdesc_iotlb(&cvq->vring, &cvq->in_iov,
> +                                          &cvq->out_iov,
> +                                          &cvq->head, GFP_ATOMIC);
> +               if (err <= 0)
> +                       break;
> +
> +               read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov, &ctrl,
> +                                            sizeof(ctrl));
> +               if (read != sizeof(ctrl))
> +                       break;
> +
> +               switch (ctrl.class) {
> +               case VIRTIO_NET_CTRL_MAC:
> +                       status = vdpasim_handle_ctrl_mac(vdpasim, ctrl.cmd);
> +                       break;
> +               default:
> +                       break;
> +               }
> +
> +               /* Make sure data is wrote before advancing index */
> +               smp_wmb();
> +
> +               write = vringh_iov_push_iotlb(&cvq->vring, &cvq->out_iov,
> +                                             &status, sizeof(status));
> +               vringh_complete_iotlb(&cvq->vring, cvq->head, write);
> +               vringh_kiov_cleanup(&cvq->in_iov);
> +               vringh_kiov_cleanup(&cvq->out_iov);
> +
> +               /* Make sure used is visible before rasing the interrupt. */
> +               smp_wmb();
> +
> +               local_bh_disable();
> +               if (cvq->cb)
> +                       cvq->cb(cvq->private);
> +               local_bh_enable();
> +       }
> +}
> +
>  static void vdpasim_net_work(struct work_struct *work)
>  {
>         struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
> @@ -77,6 +157,8 @@ static void vdpasim_net_work(struct work_struct *work)
>         if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
>                 goto out;
>
> +       vdpasim_handle_cvq(vdpasim);
> +
>         if (!txq->ready || !rxq->ready)
>                 goto out;
>
> @@ -162,6 +244,8 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
>         dev_attr.id = VIRTIO_ID_NET;
>         dev_attr.supported_features = VDPASIM_NET_FEATURES;
>         dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
> +       dev_attr.ngroups = VDPASIM_NET_GROUP_NUM;
> +       dev_attr.nas = VDPASIM_NET_AS_NUM;
>         dev_attr.config_size = sizeof(struct virtio_net_config);
>         dev_attr.get_config = vdpasim_net_get_config;
>         dev_attr.work_fn = vdpasim_net_work;
> --
> 2.25.0
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

* RE: [RFC PATCH v2 19/19] vdpasim: control virtqueue support
  2022-03-18  7:35     ` Eugenio Perez Martin
@ 2022-03-22  8:46       ` Gautam Dawar
  0 siblings, 0 replies; 136+ messages in thread
From: Gautam Dawar @ 2022-03-22  8:46 UTC (permalink / raw)
  To: Eugenio Perez Martin
  Cc: Martin Petrus Hubertus Habets, Harpreet Singh Anand,
	Tanuj Murlidhar Kamde, Jason Wang, Michael S. Tsirkin,
	Zhu Lingshan, Stefano Garzarella, Xie Yongji, Eli Cohen,
	Si-Wei Liu, Parav Pandit, Longpeng, virtualization, linux-kernel,
	kvm list, netdev

-----Original Message-----
From: Eugenio Perez Martin <eperezma@redhat.com> 
Sent: Friday, March 18, 2022 1:05 PM
To: Gautam Dawar <gdawar@xilinx.com>
Cc: Gautam Dawar <gdawar@xilinx.com>; Martin Petrus Hubertus Habets <martinh@xilinx.com>; Harpreet Singh Anand <hanand@xilinx.com>; Tanuj Murlidhar Kamde <tanujk@xilinx.com>; Jason Wang <jasowang@redhat.com>; Michael S. Tsirkin <mst@redhat.com>; Zhu Lingshan <lingshan.zhu@intel.com>; Stefano Garzarella <sgarzare@redhat.com>; Xie Yongji <xieyongji@bytedance.com>; Eli Cohen <elic@nvidia.com>; Si-Wei Liu <si-wei.liu@oracle.com>; Parav Pandit <parav@nvidia.com>; Longpeng <longpeng2@huawei.com>; virtualization <virtualization@lists.linux-foundation.org>; linux-kernel@vger.kernel.org; kvm list <kvm@vger.kernel.org>; netdev@vger.kernel.org
Subject: Re: [RFC PATCH v2 19/19] vdpasim: control virtqueue support

On Thu, Feb 24, 2022 at 10:29 PM Gautam Dawar <gautam.dawar@xilinx.com> wrote:
>
> This patch introduces the control virtqueue support for vDPA 
> simulator. This is a requirement for supporting advanced features like 
> multiqueue.
>
> A requirement for control virtqueue is to isolate its memory access 
> from the rx/tx virtqueues. This is because when using vDPA device for 
> VM, the control virqueue is not directly assigned to VM. Userspace
> (Qemu) will present a shadow control virtqueue to control for 
> recording the device states.
>
> The isolation is done via the virtqueue groups and ASID support in 
> vDPA through vhost-vdpa. The simulator is extended to have:
>
> 1) three virtqueues: RXVQ, TXVQ and CVQ (control virtqueue)
> 2) two virtqueue groups: group 0 contains RXVQ and TXVQ; group 1
>    contains CVQ
> 3) two address spaces and the simulator simply implements the address
>    spaces by mapping it 1:1 to IOTLB.
>
> For the VM use cases, userspace(Qemu) may set AS 0 to group 0 and AS 1 
> to group 1. So we have:
>
> 1) The IOTLB for virtqueue group 0 contains the mappings of guest, so
>    RX and TX can be assigned to guest directly.
> 2) The IOTLB for virtqueue group 1 contains the mappings of CVQ which
>    is the buffers that allocated and managed by VMM only. So CVQ of
>    vhost-vdpa is visible to VMM only. And Guest can not access the CVQ
>    of vhost-vdpa.
>
> For the other use cases, since AS 0 is associated to all virtqueue 
> groups by default. All virtqueues share the same mapping by default.
>
> To demonstrate the function, VIRITO_NET_F_CTRL_MACADDR is implemented 
> in the simulator for the driver to set mac address.
>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Gautam Dawar <gdawar@xilinx.com>
> ---
>  drivers/vdpa/vdpa_sim/vdpa_sim.c     | 91 ++++++++++++++++++++++------
>  drivers/vdpa/vdpa_sim/vdpa_sim.h     |  2 +
>  drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 88 ++++++++++++++++++++++++++-
>  3 files changed, 161 insertions(+), 20 deletions(-)
>
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c 
> b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> index 659e2e2e4b0c..59611f18a3a8 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> @@ -96,11 +96,17 @@ static void vdpasim_do_reset(struct vdpasim 
> *vdpasim)  {
>         int i;
>
> -       for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
> +       spin_lock(&vdpasim->iommu_lock);
> +
> +       for (i = 0; i < vdpasim->dev_attr.nvqs; i++) {
>                 vdpasim_vq_reset(vdpasim, &vdpasim->vqs[i]);
> +               vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
> +                                &vdpasim->iommu_lock);
> +       }
> +
> +       for (i = 0; i < vdpasim->dev_attr.nas; i++)
> +               vhost_iotlb_reset(&vdpasim->iommu[i]);
>
> -       spin_lock(&vdpasim->iommu_lock);
> -       vhost_iotlb_reset(vdpasim->iommu);
>         spin_unlock(&vdpasim->iommu_lock);
>
>         vdpasim->features = 0;
> @@ -145,7 +151,7 @@ static dma_addr_t vdpasim_map_range(struct vdpasim *vdpasim, phys_addr_t paddr,
>         dma_addr = iova_dma_addr(&vdpasim->iova, iova);
>
>         spin_lock(&vdpasim->iommu_lock);
> -       ret = vhost_iotlb_add_range(vdpasim->iommu, (u64)dma_addr,
> +       ret = vhost_iotlb_add_range(&vdpasim->iommu[0], (u64)dma_addr,
>                                     (u64)dma_addr + size - 1, (u64)paddr, perm);
>         spin_unlock(&vdpasim->iommu_lock);
>
> @@ -161,7 +167,7 @@ static void vdpasim_unmap_range(struct vdpasim *vdpasim, dma_addr_t dma_addr,
>                                 size_t size)  {
>         spin_lock(&vdpasim->iommu_lock);
> -       vhost_iotlb_del_range(vdpasim->iommu, (u64)dma_addr,
> +       vhost_iotlb_del_range(&vdpasim->iommu[0], (u64)dma_addr,
>                               (u64)dma_addr + size - 1);
>         spin_unlock(&vdpasim->iommu_lock);
>
> @@ -250,8 +256,9 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
>         else
>                 ops = &vdpasim_config_ops;
>
> -       vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, 1,
> -                                   1, dev_attr->name, false);
> +       vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
> +                                   dev_attr->ngroups, dev_attr->nas,
> +                                   dev_attr->name, false);
>         if (IS_ERR(vdpasim)) {
>                 ret = PTR_ERR(vdpasim);
>                 goto err_alloc;
> @@ -278,16 +285,20 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
>         if (!vdpasim->vqs)
>                 goto err_iommu;
>
> -       vdpasim->iommu = vhost_iotlb_alloc(max_iotlb_entries, 0);
> +       vdpasim->iommu = kmalloc_array(vdpasim->dev_attr.nas,
> +                                      sizeof(*vdpasim->iommu), 
> + GFP_KERNEL);
>         if (!vdpasim->iommu)
>                 goto err_iommu;
>
> +       for (i = 0; i < vdpasim->dev_attr.nas; i++)
> +               vhost_iotlb_init(&vdpasim->iommu[i], 0, 0);
> +
>         vdpasim->buffer = kvmalloc(dev_attr->buffer_size, GFP_KERNEL);
>         if (!vdpasim->buffer)
>                 goto err_iommu;
>
>         for (i = 0; i < dev_attr->nvqs; i++)
> -               vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu,
> +               vringh_set_iotlb(&vdpasim->vqs[i].vring, 
> + &vdpasim->iommu[0],
>                                  &vdpasim->iommu_lock);
>
>         ret = iova_cache_get();
> @@ -401,7 +412,11 @@ static u32 vdpasim_get_vq_align(struct 
> vdpa_device *vdpa)
>
>  static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)  {
> -       return 0;
> +       /* RX and TX belongs to group 0, CVQ belongs to group 1 */
> +       if (idx == 2)
> +               return 1;
> +       else
> +               return 0;
>  }
>
>  static u64 vdpasim_get_device_features(struct vdpa_device *vdpa) @@ 
> -539,20 +554,53 @@ static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
>         return range;
>  }
>
> +static int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
> +                                 unsigned int asid) {
> +       struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
> +       struct vhost_iotlb *iommu;
> +       int i;
> +
> +       if (group > vdpasim->dev_attr.ngroups)
> +               return -EINVAL;
> +
> +       if (asid > vdpasim->dev_attr.nas)
> +               return -EINVAL;
> +
> +       iommu = &vdpasim->iommu[asid];
> +
> +       spin_lock(&vdpasim->lock);
> +
> +       for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
> +               if (vdpasim_get_vq_group(vdpa, i) == group)
> +                       vringh_set_iotlb(&vdpasim->vqs[i].vring, 
> + &vdpasim->iommu[0],

The second argument to "vringh_set_iotlb" call must be simply "iommu".
If not, we're effectively setting asid 0 to all virtqueue groups that match "group", making it impossible to change it.
[GD>>] I agree. Will fix this.

Thanks!

> +                                        &vdpasim->iommu_lock);
> +
> +       spin_unlock(&vdpasim->lock);
> +
> +       return 0;
> +}
> +
>  static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
>                            struct vhost_iotlb *iotlb)  {
>         struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>         struct vhost_iotlb_map *map;
> +       struct vhost_iotlb *iommu;
>         u64 start = 0ULL, last = 0ULL - 1;
>         int ret;
>
> +       if (asid >= vdpasim->dev_attr.nas)
> +               return -EINVAL;
> +
>         spin_lock(&vdpasim->iommu_lock);
> -       vhost_iotlb_reset(vdpasim->iommu);
> +
> +       iommu = &vdpasim->iommu[asid];
> +       vhost_iotlb_reset(iommu);
>
>         for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
>              map = vhost_iotlb_itree_next(map, start, last)) {
> -               ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
> +               ret = vhost_iotlb_add_range(iommu, map->start,
>                                             map->last, map->addr, map->perm);
>                 if (ret)
>                         goto err;
> @@ -561,7 +609,7 @@ static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
>         return 0;
>
>  err:
> -       vhost_iotlb_reset(vdpasim->iommu);
> +       vhost_iotlb_reset(iommu);
>         spin_unlock(&vdpasim->iommu_lock);
>         return ret;
>  }
> @@ -573,9 +621,12 @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
>         struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>         int ret;
>
> +       if (asid >= vdpasim->dev_attr.nas)
> +               return -EINVAL;
> +
>         spin_lock(&vdpasim->iommu_lock);
> -       ret = vhost_iotlb_add_range_ctx(vdpasim->iommu, iova, iova + size - 1,
> -                                       pa, perm, opaque);
> +       ret = vhost_iotlb_add_range_ctx(&vdpasim->iommu[asid], iova,
> +                                       iova + size - 1, pa, perm, 
> + opaque);
>         spin_unlock(&vdpasim->iommu_lock);
>
>         return ret;
> @@ -586,8 +637,11 @@ static int vdpasim_dma_unmap(struct vdpa_device 
> *vdpa, unsigned int asid,  {
>         struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
>
> +       if (asid >= vdpasim->dev_attr.nas)
> +               return -EINVAL;
> +
>         spin_lock(&vdpasim->iommu_lock);
> -       vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
> +       vhost_iotlb_del_range(&vdpasim->iommu[asid], iova, iova + size 
> + - 1);
>         spin_unlock(&vdpasim->iommu_lock);
>
>         return 0;
> @@ -611,8 +665,7 @@ static void vdpasim_free(struct vdpa_device *vdpa)
>         }
>
>         kvfree(vdpasim->buffer);
> -       if (vdpasim->iommu)
> -               vhost_iotlb_free(vdpasim->iommu);
> +       vhost_iotlb_free(vdpasim->iommu);
>         kfree(vdpasim->vqs);
>         kfree(vdpasim->config);
>  }
> @@ -643,6 +696,7 @@ static const struct vdpa_config_ops vdpasim_config_ops = {
>         .set_config             = vdpasim_set_config,
>         .get_generation         = vdpasim_get_generation,
>         .get_iova_range         = vdpasim_get_iova_range,
> +       .set_group_asid         = vdpasim_set_group_asid,
>         .dma_map                = vdpasim_dma_map,
>         .dma_unmap              = vdpasim_dma_unmap,
>         .free                   = vdpasim_free,
> @@ -674,6 +728,7 @@ static const struct vdpa_config_ops vdpasim_batch_config_ops = {
>         .set_config             = vdpasim_set_config,
>         .get_generation         = vdpasim_get_generation,
>         .get_iova_range         = vdpasim_get_iova_range,
> +       .set_group_asid         = vdpasim_set_group_asid,
>         .set_map                = vdpasim_set_map,
>         .free                   = vdpasim_free,
>  };
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h 
> b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> index 0be7c1e7ef80..622782e92239 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> @@ -41,6 +41,8 @@ struct vdpasim_dev_attr {
>         size_t buffer_size;
>         int nvqs;
>         u32 id;
> +       u32 ngroups;
> +       u32 nas;
>
>         work_func_t work_fn;
>         void (*get_config)(struct vdpasim *vdpasim, void *config); 
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c 
> b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> index ed5ade4ae570..513970c05af2 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> @@ -26,10 +26,15 @@
>  #define DRV_LICENSE  "GPL v2"
>
>  #define VDPASIM_NET_FEATURES   (VDPASIM_FEATURES | \
> +                                (1ULL << VIRTIO_NET_F_MTU) | \
>                                  (1ULL << VIRTIO_NET_F_MAC) | \
> -                                (1ULL << VIRTIO_NET_F_MTU));
> +                                (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
> +                                (1ULL << 
> + VIRTIO_NET_F_CTRL_MAC_ADDR));
>
> -#define VDPASIM_NET_VQ_NUM     2
> +/* 3 virtqueues, 2 address spaces, 2 virtqueue groups */
> +#define VDPASIM_NET_VQ_NUM     3
> +#define VDPASIM_NET_AS_NUM     2
> +#define VDPASIM_NET_GROUP_NUM  2
>
>  static void vdpasim_net_complete(struct vdpasim_virtqueue *vq, size_t 
> len)  { @@ -63,6 +68,81 @@ static bool receive_filter(struct vdpasim 
> *vdpasim, size_t len)
>         return false;
>  }
>
> +static virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
> +                                                  u8 cmd) {
> +       struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
> +       virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
> +       size_t read;
> +
> +       switch (cmd) {
> +       case VIRTIO_NET_CTRL_MAC_ADDR_SET:
> +               read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov,
> +                                            (void *)vdpasim->config.mac,
> +                                            ETH_ALEN);
> +               if (read == ETH_ALEN)
> +                       status = VIRTIO_NET_OK;
> +               break;
> +       default:
> +               break;
> +       }
> +
> +       return status;
> +}
> +
> +static void vdpasim_handle_cvq(struct vdpasim *vdpasim) {
> +       struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
> +       virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
> +       struct virtio_net_ctrl_hdr ctrl;
> +       size_t read, write;
> +       int err;
> +
> +       if (!(vdpasim->features & (1ULL << VIRTIO_NET_F_CTRL_VQ)))
> +               return;
> +
> +       if (!cvq->ready)
> +               return;
> +
> +       while (true) {
> +               err = vringh_getdesc_iotlb(&cvq->vring, &cvq->in_iov,
> +                                          &cvq->out_iov,
> +                                          &cvq->head, GFP_ATOMIC);
> +               if (err <= 0)
> +                       break;
> +
> +               read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov, &ctrl,
> +                                            sizeof(ctrl));
> +               if (read != sizeof(ctrl))
> +                       break;
> +
> +               switch (ctrl.class) {
> +               case VIRTIO_NET_CTRL_MAC:
> +                       status = vdpasim_handle_ctrl_mac(vdpasim, ctrl.cmd);
> +                       break;
> +               default:
> +                       break;
> +               }
> +
> +               /* Make sure data is wrote before advancing index */
> +               smp_wmb();
> +
> +               write = vringh_iov_push_iotlb(&cvq->vring, &cvq->out_iov,
> +                                             &status, sizeof(status));
> +               vringh_complete_iotlb(&cvq->vring, cvq->head, write);
> +               vringh_kiov_cleanup(&cvq->in_iov);
> +               vringh_kiov_cleanup(&cvq->out_iov);
> +
> +               /* Make sure used is visible before rasing the interrupt. */
> +               smp_wmb();
> +
> +               local_bh_disable();
> +               if (cvq->cb)
> +                       cvq->cb(cvq->private);
> +               local_bh_enable();
> +       }
> +}
> +
>  static void vdpasim_net_work(struct work_struct *work)  {
>         struct vdpasim *vdpasim = container_of(work, struct vdpasim, 
> work); @@ -77,6 +157,8 @@ static void vdpasim_net_work(struct work_struct *work)
>         if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
>                 goto out;
>
> +       vdpasim_handle_cvq(vdpasim);
> +
>         if (!txq->ready || !rxq->ready)
>                 goto out;
>
> @@ -162,6 +244,8 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
>         dev_attr.id = VIRTIO_ID_NET;
>         dev_attr.supported_features = VDPASIM_NET_FEATURES;
>         dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
> +       dev_attr.ngroups = VDPASIM_NET_GROUP_NUM;
> +       dev_attr.nas = VDPASIM_NET_AS_NUM;
>         dev_attr.config_size = sizeof(struct virtio_net_config);
>         dev_attr.get_config = vdpasim_net_get_config;
>         dev_attr.work_fn = vdpasim_net_work;
> --
> 2.25.0
>


^ permalink raw reply	[flat|nested] 136+ messages in thread

end of thread, other threads:[~2022-03-22  8:47 UTC | newest]

Thread overview: 136+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-16  6:47 [PATCH 00/21] Control VQ support in vDPA Jason Wang
2020-12-16  6:47 ` Jason Wang
2020-12-16  6:47 ` [PATCH 01/21] vhost: move the backend feature bits to vhost_types.h Jason Wang
2020-12-16  6:47   ` Jason Wang
2020-12-16  6:47 ` [PATCH 02/21] virtio-vdpa: don't set callback if virtio doesn't need it Jason Wang
2020-12-16  6:47   ` Jason Wang
2020-12-16  6:48 ` [PATCH 03/21] vhost-vdpa: passing iotlb to IOMMU mapping helpers Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-16  6:48 ` [PATCH 04/21] vhost-vdpa: switch to use vhost-vdpa specific IOTLB Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-16  6:48 ` [PATCH 05/21] vdpa: add the missing comment for nvqs in struct vdpa_device Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-16  6:48 ` [PATCH 06/21] vdpa: introduce virtqueue groups Jason Wang
2020-12-16  6:48   ` Jason Wang
2021-01-04 10:04   ` Stefan Hajnoczi
2021-01-04 10:04     ` Stefan Hajnoczi
2021-01-05  4:13     ` Jason Wang
2021-01-05  4:13       ` Jason Wang
2020-12-16  6:48 ` [PATCH 07/21] vdpa: multiple address spaces support Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-29  7:28   ` Eli Cohen
2020-12-30  4:00     ` Jason Wang
2020-12-30  4:00       ` Jason Wang
2020-12-30  4:04     ` Jason Wang
2020-12-30  4:04       ` Jason Wang
2020-12-30  9:44       ` Eli Cohen
2020-12-16  6:48 ` [PATCH 08/21] vdpa: introduce config operations for associating ASID to a virtqueue group Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-16  6:48 ` [PATCH 09/21] vhost_iotlb: split out IOTLB initialization Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-16  6:48 ` [PATCH 10/21] vhost: support ASID in IOTLB API Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-29 10:20   ` Eli Cohen
2020-12-30  4:27     ` Jason Wang
2020-12-30  4:27       ` Jason Wang
2020-12-16  6:48 ` [PATCH 11/21] vhost-vdpa: introduce asid based IOTLB Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-29 11:41   ` Eli Cohen
2020-12-30  6:23     ` Jason Wang
2020-12-30  6:23       ` Jason Wang
2020-12-29 11:53   ` Eli Cohen
2020-12-30  6:34     ` Jason Wang
2020-12-30  6:34       ` Jason Wang
2020-12-29 12:05   ` Eli Cohen
2020-12-30  6:33     ` Jason Wang
2020-12-30  6:33       ` Jason Wang
2020-12-16  6:48 ` [PATCH 12/21] vhost-vdpa: introduce uAPI to get the number of virtqueue groups Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-29 12:24   ` Eli Cohen
2020-12-30  6:49     ` Jason Wang
2020-12-30  6:49       ` Jason Wang
2020-12-30 10:05   ` Eli Cohen
2020-12-31  2:36     ` Jason Wang
2020-12-31  2:36       ` Jason Wang
2020-12-16  6:48 ` [PATCH 13/21] vhost-vdpa: introduce uAPI to get the number of address spaces Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-16  6:48 ` [PATCH 14/21] vhost-vdpa: uAPI to get virtqueue group id Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-16  6:48 ` [PATCH 15/21] vhost-vdpa: introduce uAPI to set group ASID Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-16  6:48 ` [PATCH 16/21] vhost-vdpa: support ASID based IOTLB API Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-16  6:48 ` [PATCH 17/21] vdpa_sim: split vdpasim_virtqueue's iov field in out_iov and in_iov Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-16  6:48 ` [PATCH 18/21] vdpa_sim: advertise VIRTIO_NET_F_MTU Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-16  6:48 ` [PATCH 19/21] vdpa_sim: factor out buffer completion logic Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-16  6:48 ` [PATCH 20/21] vdpa_sim: filter destination mac address Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-16  6:48 ` [PATCH 21/21] vdpasim: control virtqueue support Jason Wang
2020-12-16  6:48   ` Jason Wang
2020-12-17 20:19   ` kernel test robot
2020-12-17 20:19     ` kernel test robot
2020-12-17 20:19     ` kernel test robot
2021-01-11 12:26   ` Eli Cohen
2021-01-12  3:11     ` Jason Wang
2021-01-12  3:11       ` Jason Wang
2021-01-22 19:43       ` Eugenio Perez Martin
2021-01-25  3:16         ` Jason Wang
2021-01-25  3:16           ` Jason Wang
2020-12-16  9:47 ` [PATCH 00/21] Control VQ support in vDPA Michael S. Tsirkin
2020-12-16  9:47   ` Michael S. Tsirkin
2020-12-17  3:30   ` Jason Wang
2020-12-17  3:30     ` Jason Wang
2020-12-17  7:58     ` Michael S. Tsirkin
2020-12-17  7:58       ` Michael S. Tsirkin
2020-12-17  9:02       ` Jason Wang
2020-12-17  9:02         ` Jason Wang
2020-12-17 22:28         ` Michael S. Tsirkin
2020-12-17 22:28           ` Michael S. Tsirkin
2020-12-18  2:56           ` Jason Wang
2020-12-18  2:56             ` Jason Wang
2020-12-17  7:26 ` Eli Cohen
2022-02-24 21:22 ` [RFC PATCH v2 00/19] " Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 01/19] vhost: move the backend feature bits to vhost_types.h Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 02/19] virtio-vdpa: don't set callback if virtio doesn't need it Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 03/19] vhost-vdpa: passing iotlb to IOMMU mapping helpers Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 04/19] vhost-vdpa: switch to use vhost-vdpa specific IOTLB Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 05/19] vdpa: introduce virtqueue groups Gautam Dawar
2022-02-28  8:07     ` Jason Wang
2022-02-28  8:07       ` Jason Wang
2022-02-28 10:57       ` Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 06/19] vdpa: multiple address spaces support Gautam Dawar
2022-03-03 19:39     ` Eugenio Perez Martin
2022-03-04  6:30       ` Gautam Dawar
2022-03-04 17:45         ` Eugenio Perez Martin
2022-02-24 21:22   ` [RFC PATCH v2 07/19] vdpa: introduce config operations for associating ASID to a virtqueue group Gautam Dawar
2022-03-04  9:54     ` Eugenio Perez Martin
2022-03-04 17:48       ` Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 08/19] vhost_iotlb: split out IOTLB initialization Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 09/19] vhost: support ASID in IOTLB API Gautam Dawar
2022-03-04 10:25     ` Eugenio Perez Martin
2022-03-04 17:52       ` Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 10/19] vhost-vdpa: introduce asid based IOTLB Gautam Dawar
2022-03-04 17:56     ` Eugenio Perez Martin
2022-03-07 10:07       ` Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 11/19] vhost-vdpa: introduce uAPI to get the number of virtqueue groups Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 12/19] vhost-vdpa: introduce uAPI to get the number of address spaces Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 13/19] vhost-vdpa: uAPI to get virtqueue group id Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 14/19] vhost-vdpa: introduce uAPI to set group ASID Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 15/19] vhost-vdpa: support ASID based IOTLB API Gautam Dawar
2022-03-04 18:04     ` Eugenio Perez Martin
2022-03-07 10:23       ` Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 16/19] vdpa_sim: advertise VIRTIO_NET_F_MTU Gautam Dawar
2022-03-10 17:55     ` Eugenio Perez Martin
2022-02-24 21:22   ` [RFC PATCH v2 17/19] vdpa_sim: factor out buffer completion logic Gautam Dawar
2022-02-24 21:22   ` [RFC PATCH v2 18/19] vdpa_sim: filter destination mac address Gautam Dawar
2022-03-10 18:22     ` Eugenio Perez Martin
2022-02-24 21:22   ` [RFC PATCH v2 19/19] vdpasim: control virtqueue support Gautam Dawar
2022-03-10 18:20     ` Eugenio Perez Martin
2022-03-18  7:35     ` Eugenio Perez Martin
2022-03-22  8:46       ` Gautam Dawar
2022-02-28  8:17   ` [RFC PATCH v2 00/19] Control VQ support in vDPA Jason Wang
2022-02-28  8:17     ` Jason Wang
2022-02-28 10:56     ` Gautam Dawar

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.