Skip to content

Commit 792a4f2

Browse files
jasowangmstsirkin
authored andcommitted
vhost: allow per device message handler
This patch allow device to register its own message handler during vhost_dev_init(). vDPA device will use it to implement its own DMA mapping logic. 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 20c384f commit 792a4f2

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

drivers/vhost/net.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,8 @@ static int vhost_net_open(struct inode *inode, struct file *f)
13241324
}
13251325
vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX,
13261326
UIO_MAXIOV + VHOST_NET_BATCH,
1327-
VHOST_NET_PKT_WEIGHT, VHOST_NET_WEIGHT);
1327+
VHOST_NET_PKT_WEIGHT, VHOST_NET_WEIGHT,
1328+
NULL);
13281329

13291330
vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, EPOLLOUT, dev);
13301331
vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, EPOLLIN, dev);

drivers/vhost/scsi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)
16281628
vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
16291629
}
16301630
vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ, UIO_MAXIOV,
1631-
VHOST_SCSI_WEIGHT, 0);
1631+
VHOST_SCSI_WEIGHT, 0, NULL);
16321632

16331633
vhost_scsi_init_inflight(vs, NULL);
16341634

drivers/vhost/vhost.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,9 @@ static size_t vhost_get_desc_size(struct vhost_virtqueue *vq,
457457

458458
void vhost_dev_init(struct vhost_dev *dev,
459459
struct vhost_virtqueue **vqs, int nvqs,
460-
int iov_limit, int weight, int byte_weight)
460+
int iov_limit, int weight, int byte_weight,
461+
int (*msg_handler)(struct vhost_dev *dev,
462+
struct vhost_iotlb_msg *msg))
461463
{
462464
struct vhost_virtqueue *vq;
463465
int i;
@@ -473,6 +475,7 @@ void vhost_dev_init(struct vhost_dev *dev,
473475
dev->iov_limit = iov_limit;
474476
dev->weight = weight;
475477
dev->byte_weight = byte_weight;
478+
dev->msg_handler = msg_handler;
476479
init_llist_head(&dev->work_list);
477480
init_waitqueue_head(&dev->wait);
478481
INIT_LIST_HEAD(&dev->read_list);
@@ -1178,7 +1181,12 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
11781181
ret = -EINVAL;
11791182
goto done;
11801183
}
1181-
if (vhost_process_iotlb_msg(dev, &msg)) {
1184+
1185+
if (dev->msg_handler)
1186+
ret = dev->msg_handler(dev, &msg);
1187+
else
1188+
ret = vhost_process_iotlb_msg(dev, &msg);
1189+
if (ret) {
11821190
ret = -EFAULT;
11831191
goto done;
11841192
}

drivers/vhost/vhost.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,15 @@ struct vhost_dev {
174174
int weight;
175175
int byte_weight;
176176
u64 kcov_handle;
177+
int (*msg_handler)(struct vhost_dev *dev,
178+
struct vhost_iotlb_msg *msg);
177179
};
178180

179181
bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
180182
void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
181-
int nvqs, int iov_limit, int weight, int byte_weight);
183+
int nvqs, int iov_limit, int weight, int byte_weight,
184+
int (*msg_handler)(struct vhost_dev *dev,
185+
struct vhost_iotlb_msg *msg));
182186
long vhost_dev_set_owner(struct vhost_dev *dev);
183187
bool vhost_dev_has_owner(struct vhost_dev *dev);
184188
long vhost_dev_check_owner(struct vhost_dev *);

drivers/vhost/vsock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
621621

622622
vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs),
623623
UIO_MAXIOV, VHOST_VSOCK_PKT_WEIGHT,
624-
VHOST_VSOCK_WEIGHT);
624+
VHOST_VSOCK_WEIGHT, NULL);
625625

626626
file->private_data = vsock;
627627
spin_lock_init(&vsock->send_pkt_list_lock);

0 commit comments

Comments
 (0)