Skip to content

Commit 25abc06

Browse files
jasowangmstsirkin
authored andcommitted
vhost-vdpa: support IOTLB batching hints
This patches extend the vhost IOTLB API to accept batch updating hints form userspace. When userspace wants update the device IOTLB in a batch, it may do: 1) Write vhost_iotlb_msg with VHOST_IOTLB_BATCH_BEGIN flag 2) Perform a batch of IOTLB updating via VHOST_IOTLB_UPDATE/INVALIDATE 3) Write vhost_iotlb_msg with VHOST_IOTLB_BATCH_END flag Vhost-vdpa may decide to batch the IOMMU/IOTLB updating in step 3 when vDPA device support set_map() ops. This is useful for the vDPA device that want to know all the mappings to tweak their own DMA translation logic. For vDPA device that doesn't require set_map(), no behavior changes. This capability is advertised via VHOST_BACKEND_F_IOTLB_BATCH capability. Signed-off-by: Jason Wang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent 653055b commit 25abc06

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

drivers/vhost/vdpa.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
#include "vhost.h"
2828

2929
enum {
30-
VHOST_VDPA_BACKEND_FEATURES = (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2)
30+
VHOST_VDPA_BACKEND_FEATURES =
31+
(1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) |
32+
(1ULL << VHOST_BACKEND_F_IOTLB_BATCH),
3133
};
3234

3335
/* Currently, only network backend w/o multiqueue is supported. */
@@ -48,6 +50,7 @@ struct vhost_vdpa {
4850
int virtio_id;
4951
int minor;
5052
struct eventfd_ctx *config_ctx;
53+
int in_batch;
5154
};
5255

5356
static DEFINE_IDA(vhost_vdpa_ida);
@@ -124,6 +127,7 @@ static void vhost_vdpa_reset(struct vhost_vdpa *v)
124127
struct vdpa_device *vdpa = v->vdpa;
125128

126129
vdpa_reset(vdpa);
130+
v->in_batch = 0;
127131
}
128132

129133
static long vhost_vdpa_get_device_id(struct vhost_vdpa *v, u8 __user *argp)
@@ -546,13 +550,15 @@ static int vhost_vdpa_map(struct vhost_vdpa *v,
546550
if (r)
547551
return r;
548552

549-
if (ops->dma_map)
553+
if (ops->dma_map) {
550554
r = ops->dma_map(vdpa, iova, size, pa, perm);
551-
else if (ops->set_map)
552-
r = ops->set_map(vdpa, dev->iotlb);
553-
else
555+
} else if (ops->set_map) {
556+
if (!v->in_batch)
557+
r = ops->set_map(vdpa, dev->iotlb);
558+
} else {
554559
r = iommu_map(v->domain, iova, pa, size,
555560
perm_to_iommu_flags(perm));
561+
}
556562

557563
return r;
558564
}
@@ -565,12 +571,14 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v, u64 iova, u64 size)
565571

566572
vhost_vdpa_iotlb_unmap(v, iova, iova + size - 1);
567573

568-
if (ops->dma_map)
574+
if (ops->dma_map) {
569575
ops->dma_unmap(vdpa, iova, size);
570-
else if (ops->set_map)
571-
ops->set_map(vdpa, dev->iotlb);
572-
else
576+
} else if (ops->set_map) {
577+
if (!v->in_batch)
578+
ops->set_map(vdpa, dev->iotlb);
579+
} else {
573580
iommu_unmap(v->domain, iova, size);
581+
}
574582
}
575583

576584
static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
@@ -663,6 +671,8 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
663671
struct vhost_iotlb_msg *msg)
664672
{
665673
struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
674+
struct vdpa_device *vdpa = v->vdpa;
675+
const struct vdpa_config_ops *ops = vdpa->config;
666676
int r = 0;
667677

668678
r = vhost_dev_check_owner(dev);
@@ -676,6 +686,14 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
676686
case VHOST_IOTLB_INVALIDATE:
677687
vhost_vdpa_unmap(v, msg->iova, msg->size);
678688
break;
689+
case VHOST_IOTLB_BATCH_BEGIN:
690+
v->in_batch = true;
691+
break;
692+
case VHOST_IOTLB_BATCH_END:
693+
if (v->in_batch && ops->set_map)
694+
ops->set_map(vdpa, dev->iotlb);
695+
v->in_batch = false;
696+
break;
679697
default:
680698
r = -EINVAL;
681699
break;

include/uapi/linux/vhost.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191

9292
/* Use message type V2 */
9393
#define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1
94+
/* IOTLB can accept batching hints */
95+
#define VHOST_BACKEND_F_IOTLB_BATCH 0x2
9496

9597
#define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64)
9698
#define VHOST_GET_BACKEND_FEATURES _IOR(VHOST_VIRTIO, 0x26, __u64)

include/uapi/linux/vhost_types.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ struct vhost_iotlb_msg {
6060
#define VHOST_IOTLB_UPDATE 2
6161
#define VHOST_IOTLB_INVALIDATE 3
6262
#define VHOST_IOTLB_ACCESS_FAIL 4
63+
/*
64+
* VHOST_IOTLB_BATCH_BEGIN and VHOST_IOTLB_BATCH_END allow modifying
65+
* multiple mappings in one go: beginning with
66+
* VHOST_IOTLB_BATCH_BEGIN, followed by any number of
67+
* VHOST_IOTLB_UPDATE messages, and ending with VHOST_IOTLB_BATCH_END.
68+
* When one of these two values is used as the message type, the rest
69+
* of the fields in the message are ignored. There's no guarantee that
70+
* these changes take place automatically in the device.
71+
*/
72+
#define VHOST_IOTLB_BATCH_BEGIN 5
73+
#define VHOST_IOTLB_BATCH_END 6
6374
__u8 type;
6475
};
6576

0 commit comments

Comments
 (0)