Skip to content

Commit 4942e82

Browse files
jasowangmstsirkin
authored andcommitted
vhost: introduce helpers to get the size of metadata area
To avoid code duplication since it will be used by kernel VA prefetching. Signed-off-by: Jason Wang <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent 9b5e830 commit 4942e82

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

drivers/vhost/vhost.c

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,32 @@ bool vhost_exceeds_weight(struct vhost_virtqueue *vq,
428428
}
429429
EXPORT_SYMBOL_GPL(vhost_exceeds_weight);
430430

431+
static size_t vhost_get_avail_size(struct vhost_virtqueue *vq,
432+
unsigned int num)
433+
{
434+
size_t event __maybe_unused =
435+
vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
436+
437+
return sizeof(*vq->avail) +
438+
sizeof(*vq->avail->ring) * num + event;
439+
}
440+
441+
static size_t vhost_get_used_size(struct vhost_virtqueue *vq,
442+
unsigned int num)
443+
{
444+
size_t event __maybe_unused =
445+
vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
446+
447+
return sizeof(*vq->used) +
448+
sizeof(*vq->used->ring) * num + event;
449+
}
450+
451+
static size_t vhost_get_desc_size(struct vhost_virtqueue *vq,
452+
unsigned int num)
453+
{
454+
return sizeof(*vq->desc) * num;
455+
}
456+
431457
void vhost_dev_init(struct vhost_dev *dev,
432458
struct vhost_virtqueue **vqs, int nvqs,
433459
int iov_limit, int weight, int byte_weight)
@@ -1275,13 +1301,9 @@ static bool vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
12751301
struct vring_used __user *used)
12761302

12771303
{
1278-
size_t s __maybe_unused = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1279-
1280-
return access_ok(desc, num * sizeof *desc) &&
1281-
access_ok(avail,
1282-
sizeof *avail + num * sizeof *avail->ring + s) &&
1283-
access_ok(used,
1284-
sizeof *used + num * sizeof *used->ring + s);
1304+
return access_ok(desc, vhost_get_desc_size(vq, num)) &&
1305+
access_ok(avail, vhost_get_avail_size(vq, num)) &&
1306+
access_ok(used, vhost_get_used_size(vq, num));
12851307
}
12861308

12871309
static void vhost_vq_meta_update(struct vhost_virtqueue *vq,
@@ -1333,22 +1355,18 @@ static bool iotlb_access_ok(struct vhost_virtqueue *vq,
13331355

13341356
int vq_meta_prefetch(struct vhost_virtqueue *vq)
13351357
{
1336-
size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
13371358
unsigned int num = vq->num;
13381359

13391360
if (!vq->iotlb)
13401361
return 1;
13411362

13421363
return iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->desc,
1343-
num * sizeof(*vq->desc), VHOST_ADDR_DESC) &&
1364+
vhost_get_desc_size(vq, num), VHOST_ADDR_DESC) &&
13441365
iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->avail,
1345-
sizeof *vq->avail +
1346-
num * sizeof(*vq->avail->ring) + s,
1366+
vhost_get_avail_size(vq, num),
13471367
VHOST_ADDR_AVAIL) &&
13481368
iotlb_access_ok(vq, VHOST_ACCESS_WO, (u64)(uintptr_t)vq->used,
1349-
sizeof *vq->used +
1350-
num * sizeof(*vq->used->ring) + s,
1351-
VHOST_ADDR_USED);
1369+
vhost_get_used_size(vq, num), VHOST_ADDR_USED);
13521370
}
13531371
EXPORT_SYMBOL_GPL(vq_meta_prefetch);
13541372

@@ -1365,13 +1383,10 @@ EXPORT_SYMBOL_GPL(vhost_log_access_ok);
13651383
static bool vq_log_access_ok(struct vhost_virtqueue *vq,
13661384
void __user *log_base)
13671385
{
1368-
size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1369-
13701386
return vq_memory_access_ok(log_base, vq->umem,
13711387
vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
13721388
(!vq->log_used || log_access_ok(log_base, vq->log_addr,
1373-
sizeof *vq->used +
1374-
vq->num * sizeof *vq->used->ring + s));
1389+
vhost_get_used_size(vq, vq->num)));
13751390
}
13761391

13771392
/* Can we start vq? */

0 commit comments

Comments
 (0)