Skip to content

Commit 91233ad

Browse files
Gautam Dawarmstsirkin
authored andcommitted
vhost: support ASID in IOTLB API
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 <[email protected]> Signed-off-by: Gautam Dawar <[email protected]> Message-Id: <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent 1cb1089 commit 91233ad

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

drivers/vhost/vdpa.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
870870
msg->perm);
871871
}
872872

873-
static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
873+
static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
874874
struct vhost_iotlb_msg *msg)
875875
{
876876
struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
@@ -879,6 +879,9 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
879879
struct vhost_iotlb *iotlb = v->iotlb;
880880
int r = 0;
881881

882+
if (asid != 0)
883+
return -EINVAL;
884+
882885
mutex_lock(&dev->mutex);
883886

884887
r = vhost_dev_check_owner(dev);

drivers/vhost/vhost.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ void vhost_dev_init(struct vhost_dev *dev,
468468
struct vhost_virtqueue **vqs, int nvqs,
469469
int iov_limit, int weight, int byte_weight,
470470
bool use_worker,
471-
int (*msg_handler)(struct vhost_dev *dev,
471+
int (*msg_handler)(struct vhost_dev *dev, u32 asid,
472472
struct vhost_iotlb_msg *msg))
473473
{
474474
struct vhost_virtqueue *vq;
@@ -1090,11 +1090,14 @@ static bool umem_access_ok(u64 uaddr, u64 size, int access)
10901090
return true;
10911091
}
10921092

1093-
static int vhost_process_iotlb_msg(struct vhost_dev *dev,
1093+
static int vhost_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
10941094
struct vhost_iotlb_msg *msg)
10951095
{
10961096
int ret = 0;
10971097

1098+
if (asid != 0)
1099+
return -EINVAL;
1100+
10981101
mutex_lock(&dev->mutex);
10991102
vhost_dev_lock_vqs(dev);
11001103
switch (msg->type) {
@@ -1141,6 +1144,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
11411144
struct vhost_iotlb_msg msg;
11421145
size_t offset;
11431146
int type, ret;
1147+
u32 asid = 0;
11441148

11451149
ret = copy_from_iter(&type, sizeof(type), from);
11461150
if (ret != sizeof(type)) {
@@ -1156,7 +1160,16 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
11561160
offset = offsetof(struct vhost_msg, iotlb) - sizeof(int);
11571161
break;
11581162
case VHOST_IOTLB_MSG_V2:
1159-
offset = sizeof(__u32);
1163+
if (vhost_backend_has_feature(dev->vqs[0],
1164+
VHOST_BACKEND_F_IOTLB_ASID)) {
1165+
ret = copy_from_iter(&asid, sizeof(asid), from);
1166+
if (ret != sizeof(asid)) {
1167+
ret = -EINVAL;
1168+
goto done;
1169+
}
1170+
offset = sizeof(__u16);
1171+
} else
1172+
offset = sizeof(__u32);
11601173
break;
11611174
default:
11621175
ret = -EINVAL;
@@ -1178,9 +1191,9 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
11781191
}
11791192

11801193
if (dev->msg_handler)
1181-
ret = dev->msg_handler(dev, &msg);
1194+
ret = dev->msg_handler(dev, asid, &msg);
11821195
else
1183-
ret = vhost_process_iotlb_msg(dev, &msg);
1196+
ret = vhost_process_iotlb_msg(dev, asid, &msg);
11841197
if (ret) {
11851198
ret = -EFAULT;
11861199
goto done;

drivers/vhost/vhost.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ struct vhost_dev {
161161
int byte_weight;
162162
u64 kcov_handle;
163163
bool use_worker;
164-
int (*msg_handler)(struct vhost_dev *dev,
164+
int (*msg_handler)(struct vhost_dev *dev, u32 asid,
165165
struct vhost_iotlb_msg *msg);
166166
};
167167

168168
bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
169169
void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
170170
int nvqs, int iov_limit, int weight, int byte_weight,
171171
bool use_worker,
172-
int (*msg_handler)(struct vhost_dev *dev,
172+
int (*msg_handler)(struct vhost_dev *dev, u32 asid,
173173
struct vhost_iotlb_msg *msg));
174174
long vhost_dev_set_owner(struct vhost_dev *dev);
175175
bool vhost_dev_has_owner(struct vhost_dev *dev);

include/uapi/linux/vhost_types.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct vhost_msg {
8787

8888
struct vhost_msg_v2 {
8989
__u32 type;
90-
__u32 reserved;
90+
__u32 asid;
9191
union {
9292
struct vhost_iotlb_msg iotlb;
9393
__u8 padding[64];
@@ -157,5 +157,9 @@ struct vhost_vdpa_iova_range {
157157
#define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1
158158
/* IOTLB can accept batching hints */
159159
#define VHOST_BACKEND_F_IOTLB_BATCH 0x2
160+
/* IOTLB can accept address space identifier through V2 type of IOTLB
161+
* message
162+
*/
163+
#define VHOST_BACKEND_F_IOTLB_ASID 0x3
160164

161165
#endif

0 commit comments

Comments
 (0)