Skip to content

Commit bfe2bc5

Browse files
jasowangmstsirkin
authored andcommitted
vhost: introduce vhost memory accessors
This patch introduces vhost memory accessors which were just wrappers for userspace address access helpers. This is a requirement for vhost device iotlb implementation which will add iotlb translations in those accessors. Signed-off-by: Jason Wang <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent 304ba62 commit bfe2bc5

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

drivers/vhost/vhost.c

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,22 @@ static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
638638
return 1;
639639
}
640640

641+
#define vhost_put_user(vq, x, ptr) __put_user(x, ptr)
642+
643+
static int vhost_copy_to_user(struct vhost_virtqueue *vq, void *to,
644+
const void *from, unsigned size)
645+
{
646+
return __copy_to_user(to, from, size);
647+
}
648+
649+
#define vhost_get_user(vq, x, ptr) __get_user(x, ptr)
650+
651+
static int vhost_copy_from_user(struct vhost_virtqueue *vq, void *to,
652+
void *from, unsigned size)
653+
{
654+
return __copy_from_user(to, from, size);
655+
}
656+
641657
static int vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
642658
struct vring_desc __user *desc,
643659
struct vring_avail __user *avail,
@@ -1143,7 +1159,8 @@ EXPORT_SYMBOL_GPL(vhost_log_write);
11431159
static int vhost_update_used_flags(struct vhost_virtqueue *vq)
11441160
{
11451161
void __user *used;
1146-
if (__put_user(cpu_to_vhost16(vq, vq->used_flags), &vq->used->flags) < 0)
1162+
if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->used_flags),
1163+
&vq->used->flags) < 0)
11471164
return -EFAULT;
11481165
if (unlikely(vq->log_used)) {
11491166
/* Make sure the flag is seen before log. */
@@ -1161,7 +1178,8 @@ static int vhost_update_used_flags(struct vhost_virtqueue *vq)
11611178

11621179
static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
11631180
{
1164-
if (__put_user(cpu_to_vhost16(vq, vq->avail_idx), vhost_avail_event(vq)))
1181+
if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->avail_idx),
1182+
vhost_avail_event(vq)))
11651183
return -EFAULT;
11661184
if (unlikely(vq->log_used)) {
11671185
void __user *used;
@@ -1199,7 +1217,7 @@ int vhost_vq_init_access(struct vhost_virtqueue *vq)
11991217
r = -EFAULT;
12001218
goto err;
12011219
}
1202-
r = __get_user(last_used_idx, &vq->used->idx);
1220+
r = vhost_get_user(vq, last_used_idx, &vq->used->idx);
12031221
if (r)
12041222
goto err;
12051223
vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
@@ -1379,7 +1397,7 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq,
13791397

13801398
/* Check it isn't doing very strange things with descriptor numbers. */
13811399
last_avail_idx = vq->last_avail_idx;
1382-
if (unlikely(__get_user(avail_idx, &vq->avail->idx))) {
1400+
if (unlikely(vhost_get_user(vq, avail_idx, &vq->avail->idx))) {
13831401
vq_err(vq, "Failed to access avail idx at %p\n",
13841402
&vq->avail->idx);
13851403
return -EFAULT;
@@ -1401,8 +1419,8 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq,
14011419

14021420
/* Grab the next descriptor number they're advertising, and increment
14031421
* the index we've seen. */
1404-
if (unlikely(__get_user(ring_head,
1405-
&vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
1422+
if (unlikely(vhost_get_user(vq, ring_head,
1423+
&vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
14061424
vq_err(vq, "Failed to read head: idx %d address %p\n",
14071425
last_avail_idx,
14081426
&vq->avail->ring[last_avail_idx % vq->num]);
@@ -1437,7 +1455,8 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq,
14371455
i, vq->num, head);
14381456
return -EINVAL;
14391457
}
1440-
ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
1458+
ret = vhost_copy_from_user(vq, &desc, vq->desc + i,
1459+
sizeof desc);
14411460
if (unlikely(ret)) {
14421461
vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
14431462
i, vq->desc + i);
@@ -1525,15 +1544,15 @@ static int __vhost_add_used_n(struct vhost_virtqueue *vq,
15251544
start = vq->last_used_idx & (vq->num - 1);
15261545
used = vq->used->ring + start;
15271546
if (count == 1) {
1528-
if (__put_user(heads[0].id, &used->id)) {
1547+
if (vhost_put_user(vq, heads[0].id, &used->id)) {
15291548
vq_err(vq, "Failed to write used id");
15301549
return -EFAULT;
15311550
}
1532-
if (__put_user(heads[0].len, &used->len)) {
1551+
if (vhost_put_user(vq, heads[0].len, &used->len)) {
15331552
vq_err(vq, "Failed to write used len");
15341553
return -EFAULT;
15351554
}
1536-
} else if (__copy_to_user(used, heads, count * sizeof *used)) {
1555+
} else if (vhost_copy_to_user(vq, used, heads, count * sizeof *used)) {
15371556
vq_err(vq, "Failed to write used");
15381557
return -EFAULT;
15391558
}
@@ -1577,7 +1596,8 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
15771596

15781597
/* Make sure buffer is written before we update index. */
15791598
smp_wmb();
1580-
if (__put_user(cpu_to_vhost16(vq, vq->last_used_idx), &vq->used->idx)) {
1599+
if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->last_used_idx),
1600+
&vq->used->idx)) {
15811601
vq_err(vq, "Failed to increment used idx");
15821602
return -EFAULT;
15831603
}
@@ -1609,7 +1629,7 @@ static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
16091629

16101630
if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
16111631
__virtio16 flags;
1612-
if (__get_user(flags, &vq->avail->flags)) {
1632+
if (vhost_get_user(vq, flags, &vq->avail->flags)) {
16131633
vq_err(vq, "Failed to get flags");
16141634
return true;
16151635
}
@@ -1623,7 +1643,7 @@ static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
16231643
if (unlikely(!v))
16241644
return true;
16251645

1626-
if (__get_user(event, vhost_used_event(vq))) {
1646+
if (vhost_get_user(vq, event, vhost_used_event(vq))) {
16271647
vq_err(vq, "Failed to get used event idx");
16281648
return true;
16291649
}
@@ -1665,7 +1685,7 @@ bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
16651685
__virtio16 avail_idx;
16661686
int r;
16671687

1668-
r = __get_user(avail_idx, &vq->avail->idx);
1688+
r = vhost_get_user(vq, avail_idx, &vq->avail->idx);
16691689
if (r)
16701690
return false;
16711691

@@ -1700,7 +1720,7 @@ bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
17001720
/* They could have slipped one in as we were doing that: make
17011721
* sure it's written, then check again. */
17021722
smp_mb();
1703-
r = __get_user(avail_idx, &vq->avail->idx);
1723+
r = vhost_get_user(vq, avail_idx, &vq->avail->idx);
17041724
if (r) {
17051725
vq_err(vq, "Failed to check avail idx at %p: %d\n",
17061726
&vq->avail->idx, r);

0 commit comments

Comments
 (0)