Skip to content

Commit 8ea8cf8

Browse files
mstsirkinrustyrussell
authored andcommitted
vhost: support event index
Support the new event index feature. When acked, utilize it to reduce the # of interrupts sent to the guest. Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent a5c262c commit 8ea8cf8

4 files changed

Lines changed: 127 additions & 50 deletions

File tree

‎drivers/vhost/net.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static void handle_tx(struct vhost_net *net)
144144
}
145145

146146
mutex_lock(&vq->mutex);
147-
vhost_disable_notify(vq);
147+
vhost_disable_notify(&net->dev, vq);
148148

149149
if (wmem < sock->sk->sk_sndbuf / 2)
150150
tx_poll_stop(net);
@@ -166,8 +166,8 @@ static void handle_tx(struct vhost_net *net)
166166
set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
167167
break;
168168
}
169-
if (unlikely(vhost_enable_notify(vq))) {
170-
vhost_disable_notify(vq);
169+
if (unlikely(vhost_enable_notify(&net->dev, vq))) {
170+
vhost_disable_notify(&net->dev, vq);
171171
continue;
172172
}
173173
break;
@@ -315,7 +315,7 @@ static void handle_rx(struct vhost_net *net)
315315
return;
316316

317317
mutex_lock(&vq->mutex);
318-
vhost_disable_notify(vq);
318+
vhost_disable_notify(&net->dev, vq);
319319
vhost_hlen = vq->vhost_hlen;
320320
sock_hlen = vq->sock_hlen;
321321

@@ -334,10 +334,10 @@ static void handle_rx(struct vhost_net *net)
334334
break;
335335
/* OK, now we need to know about added descriptors. */
336336
if (!headcount) {
337-
if (unlikely(vhost_enable_notify(vq))) {
337+
if (unlikely(vhost_enable_notify(&net->dev, vq))) {
338338
/* They have slipped one in as we were
339339
* doing that: check again. */
340-
vhost_disable_notify(vq);
340+
vhost_disable_notify(&net->dev, vq);
341341
continue;
342342
}
343343
/* Nothing new? Wait for eventfd to tell us

‎drivers/vhost/test.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static void handle_vq(struct vhost_test *n)
4949
return;
5050

5151
mutex_lock(&vq->mutex);
52-
vhost_disable_notify(vq);
52+
vhost_disable_notify(&n->dev, vq);
5353

5454
for (;;) {
5555
head = vhost_get_vq_desc(&n->dev, vq, vq->iov,
@@ -61,8 +61,8 @@ static void handle_vq(struct vhost_test *n)
6161
break;
6262
/* Nothing new? Wait for eventfd to tell us they refilled. */
6363
if (head == vq->num) {
64-
if (unlikely(vhost_enable_notify(vq))) {
65-
vhost_disable_notify(vq);
64+
if (unlikely(vhost_enable_notify(&n->dev, vq))) {
65+
vhost_disable_notify(&n->dev, vq);
6666
continue;
6767
}
6868
break;

‎drivers/vhost/vhost.c‎

Lines changed: 104 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ enum {
3737
VHOST_MEMORY_F_LOG = 0x1,
3838
};
3939

40+
#define vhost_used_event(vq) ((u16 __user *)&vq->avail->ring[vq->num])
41+
#define vhost_avail_event(vq) ((u16 __user *)&vq->used->ring[vq->num])
42+
4043
static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
4144
poll_table *pt)
4245
{
@@ -161,6 +164,8 @@ static void vhost_vq_reset(struct vhost_dev *dev,
161164
vq->last_avail_idx = 0;
162165
vq->avail_idx = 0;
163166
vq->last_used_idx = 0;
167+
vq->signalled_used = 0;
168+
vq->signalled_used_valid = false;
164169
vq->used_flags = 0;
165170
vq->log_used = false;
166171
vq->log_addr = -1ull;
@@ -489,16 +494,17 @@ static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
489494
return 1;
490495
}
491496

492-
static int vq_access_ok(unsigned int num,
497+
static int vq_access_ok(struct vhost_dev *d, unsigned int num,
493498
struct vring_desc __user *desc,
494499
struct vring_avail __user *avail,
495500
struct vring_used __user *used)
496501
{
502+
size_t s = vhost_has_feature(d, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
497503
return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
498504
access_ok(VERIFY_READ, avail,
499-
sizeof *avail + num * sizeof *avail->ring) &&
505+
sizeof *avail + num * sizeof *avail->ring + s) &&
500506
access_ok(VERIFY_WRITE, used,
501-
sizeof *used + num * sizeof *used->ring);
507+
sizeof *used + num * sizeof *used->ring + s);
502508
}
503509

504510
/* Can we log writes? */
@@ -514,25 +520,27 @@ int vhost_log_access_ok(struct vhost_dev *dev)
514520

515521
/* Verify access for write logging. */
516522
/* Caller should have vq mutex and device mutex */
517-
static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base)
523+
static int vq_log_access_ok(struct vhost_dev *d, struct vhost_virtqueue *vq,
524+
void __user *log_base)
518525
{
519526
struct vhost_memory *mp;
527+
size_t s = vhost_has_feature(d, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
520528

521529
mp = rcu_dereference_protected(vq->dev->memory,
522530
lockdep_is_held(&vq->mutex));
523531
return vq_memory_access_ok(log_base, mp,
524532
vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
525533
(!vq->log_used || log_access_ok(log_base, vq->log_addr,
526534
sizeof *vq->used +
527-
vq->num * sizeof *vq->used->ring));
535+
vq->num * sizeof *vq->used->ring + s));
528536
}
529537

530538
/* Can we start vq? */
531539
/* Caller should have vq mutex and device mutex */
532540
int vhost_vq_access_ok(struct vhost_virtqueue *vq)
533541
{
534-
return vq_access_ok(vq->num, vq->desc, vq->avail, vq->used) &&
535-
vq_log_access_ok(vq, vq->log_base);
542+
return vq_access_ok(vq->dev, vq->num, vq->desc, vq->avail, vq->used) &&
543+
vq_log_access_ok(vq->dev, vq, vq->log_base);
536544
}
537545

538546
static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
@@ -577,6 +585,7 @@ static int init_used(struct vhost_virtqueue *vq,
577585

578586
if (r)
579587
return r;
588+
vq->signalled_used_valid = false;
580589
return get_user(vq->last_used_idx, &used->idx);
581590
}
582591

@@ -674,7 +683,7 @@ static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
674683
* If it is not, we don't as size might not have been setup.
675684
* We will verify when backend is configured. */
676685
if (vq->private_data) {
677-
if (!vq_access_ok(vq->num,
686+
if (!vq_access_ok(d, vq->num,
678687
(void __user *)(unsigned long)a.desc_user_addr,
679688
(void __user *)(unsigned long)a.avail_user_addr,
680689
(void __user *)(unsigned long)a.used_user_addr)) {
@@ -818,7 +827,7 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
818827
vq = d->vqs + i;
819828
mutex_lock(&vq->mutex);
820829
/* If ring is inactive, will check when it's enabled. */
821-
if (vq->private_data && !vq_log_access_ok(vq, base))
830+
if (vq->private_data && !vq_log_access_ok(d, vq, base))
822831
r = -EFAULT;
823832
else
824833
vq->log_base = base;
@@ -1219,6 +1228,10 @@ int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
12191228

12201229
/* On success, increment avail index. */
12211230
vq->last_avail_idx++;
1231+
1232+
/* Assume notifications from guest are disabled at this point,
1233+
* if they aren't we would need to update avail_event index. */
1234+
BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
12221235
return head;
12231236
}
12241237

@@ -1267,6 +1280,12 @@ int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
12671280
eventfd_signal(vq->log_ctx, 1);
12681281
}
12691282
vq->last_used_idx++;
1283+
/* If the driver never bothers to signal in a very long while,
1284+
* used index might wrap around. If that happens, invalidate
1285+
* signalled_used index we stored. TODO: make sure driver
1286+
* signals at least once in 2^16 and remove this. */
1287+
if (unlikely(vq->last_used_idx == vq->signalled_used))
1288+
vq->signalled_used_valid = false;
12701289
return 0;
12711290
}
12721291

@@ -1275,6 +1294,7 @@ static int __vhost_add_used_n(struct vhost_virtqueue *vq,
12751294
unsigned count)
12761295
{
12771296
struct vring_used_elem __user *used;
1297+
u16 old, new;
12781298
int start;
12791299

12801300
start = vq->last_used_idx % vq->num;
@@ -1292,7 +1312,14 @@ static int __vhost_add_used_n(struct vhost_virtqueue *vq,
12921312
((void __user *)used - (void __user *)vq->used),
12931313
count * sizeof *used);
12941314
}
1295-
vq->last_used_idx += count;
1315+
old = vq->last_used_idx;
1316+
new = (vq->last_used_idx += count);
1317+
/* If the driver never bothers to signal in a very long while,
1318+
* used index might wrap around. If that happens, invalidate
1319+
* signalled_used index we stored. TODO: make sure driver
1320+
* signals at least once in 2^16 and remove this. */
1321+
if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
1322+
vq->signalled_used_valid = false;
12961323
return 0;
12971324
}
12981325

@@ -1331,29 +1358,47 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
13311358
return r;
13321359
}
13331360

1334-
/* This actually signals the guest, using eventfd. */
1335-
void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1361+
static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
13361362
{
1337-
__u16 flags;
1338-
1363+
__u16 old, new, event;
1364+
bool v;
13391365
/* Flush out used index updates. This is paired
13401366
* with the barrier that the Guest executes when enabling
13411367
* interrupts. */
13421368
smp_mb();
13431369

1344-
if (__get_user(flags, &vq->avail->flags)) {
1345-
vq_err(vq, "Failed to get flags");
1346-
return;
1370+
if (vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
1371+
unlikely(vq->avail_idx == vq->last_avail_idx))
1372+
return true;
1373+
1374+
if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
1375+
__u16 flags;
1376+
if (__get_user(flags, &vq->avail->flags)) {
1377+
vq_err(vq, "Failed to get flags");
1378+
return true;
1379+
}
1380+
return !(flags & VRING_AVAIL_F_NO_INTERRUPT);
13471381
}
1382+
old = vq->signalled_used;
1383+
v = vq->signalled_used_valid;
1384+
new = vq->signalled_used = vq->last_used_idx;
1385+
vq->signalled_used_valid = true;
13481386

1349-
/* If they don't want an interrupt, don't signal, unless empty. */
1350-
if ((flags & VRING_AVAIL_F_NO_INTERRUPT) &&
1351-
(vq->avail_idx != vq->last_avail_idx ||
1352-
!vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY)))
1353-
return;
1387+
if (unlikely(!v))
1388+
return true;
13541389

1390+
if (get_user(event, vhost_used_event(vq))) {
1391+
vq_err(vq, "Failed to get used event idx");
1392+
return true;
1393+
}
1394+
return vring_need_event(event, new, old);
1395+
}
1396+
1397+
/* This actually signals the guest, using eventfd. */
1398+
void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1399+
{
13551400
/* Signal the Guest tell them we used something up. */
1356-
if (vq->call_ctx)
1401+
if (vq->call_ctx && vhost_notify(dev, vq))
13571402
eventfd_signal(vq->call_ctx, 1);
13581403
}
13591404

@@ -1376,19 +1421,42 @@ void vhost_add_used_and_signal_n(struct vhost_dev *dev,
13761421
}
13771422

13781423
/* OK, now we need to know about added descriptors. */
1379-
bool vhost_enable_notify(struct vhost_virtqueue *vq)
1424+
bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
13801425
{
13811426
u16 avail_idx;
13821427
int r;
13831428

13841429
if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
13851430
return false;
13861431
vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
1387-
r = put_user(vq->used_flags, &vq->used->flags);
1388-
if (r) {
1389-
vq_err(vq, "Failed to enable notification at %p: %d\n",
1390-
&vq->used->flags, r);
1391-
return false;
1432+
if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
1433+
r = put_user(vq->used_flags, &vq->used->flags);
1434+
if (r) {
1435+
vq_err(vq, "Failed to enable notification at %p: %d\n",
1436+
&vq->used->flags, r);
1437+
return false;
1438+
}
1439+
} else {
1440+
r = put_user(vq->avail_idx, vhost_avail_event(vq));
1441+
if (r) {
1442+
vq_err(vq, "Failed to update avail event index at %p: %d\n",
1443+
vhost_avail_event(vq), r);
1444+
return false;
1445+
}
1446+
}
1447+
if (unlikely(vq->log_used)) {
1448+
void __user *used;
1449+
/* Make sure data is seen before log. */
1450+
smp_wmb();
1451+
used = vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX) ?
1452+
&vq->used->flags : vhost_avail_event(vq);
1453+
/* Log used flags or event index entry write. Both are 16 bit
1454+
* fields. */
1455+
log_write(vq->log_base, vq->log_addr +
1456+
(used - (void __user *)vq->used),
1457+
sizeof(u16));
1458+
if (vq->log_ctx)
1459+
eventfd_signal(vq->log_ctx, 1);
13921460
}
13931461
/* They could have slipped one in as we were doing that: make
13941462
* sure it's written, then check again. */
@@ -1404,15 +1472,17 @@ bool vhost_enable_notify(struct vhost_virtqueue *vq)
14041472
}
14051473

14061474
/* We don't need to be notified again. */
1407-
void vhost_disable_notify(struct vhost_virtqueue *vq)
1475+
void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
14081476
{
14091477
int r;
14101478

14111479
if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
14121480
return;
14131481
vq->used_flags |= VRING_USED_F_NO_NOTIFY;
1414-
r = put_user(vq->used_flags, &vq->used->flags);
1415-
if (r)
1416-
vq_err(vq, "Failed to enable notification at %p: %d\n",
1417-
&vq->used->flags, r);
1482+
if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
1483+
r = put_user(vq->used_flags, &vq->used->flags);
1484+
if (r)
1485+
vq_err(vq, "Failed to enable notification at %p: %d\n",
1486+
&vq->used->flags, r);
1487+
}
14181488
}

‎drivers/vhost/vhost.h‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ struct vhost_virtqueue {
8484
/* Used flags */
8585
u16 used_flags;
8686

87+
/* Last used index value we have signalled on */
88+
u16 signalled_used;
89+
90+
/* Last used index value we have signalled on */
91+
bool signalled_used_valid;
92+
8793
/* Log writes to used structure. */
8894
bool log_used;
8995
u64 log_addr;
@@ -149,8 +155,8 @@ void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
149155
void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
150156
struct vring_used_elem *heads, unsigned count);
151157
void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
152-
void vhost_disable_notify(struct vhost_virtqueue *);
153-
bool vhost_enable_notify(struct vhost_virtqueue *);
158+
void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
159+
bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
154160

155161
int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
156162
unsigned int log_num, u64 len);
@@ -162,11 +168,12 @@ int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
162168
} while (0)
163169

164170
enum {
165-
VHOST_FEATURES = (1 << VIRTIO_F_NOTIFY_ON_EMPTY) |
166-
(1 << VIRTIO_RING_F_INDIRECT_DESC) |
167-
(1 << VHOST_F_LOG_ALL) |
168-
(1 << VHOST_NET_F_VIRTIO_NET_HDR) |
169-
(1 << VIRTIO_NET_F_MRG_RXBUF),
171+
VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
172+
(1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
173+
(1ULL << VIRTIO_RING_F_EVENT_IDX) |
174+
(1ULL << VHOST_F_LOG_ALL) |
175+
(1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
176+
(1ULL << VIRTIO_NET_F_MRG_RXBUF),
170177
};
171178

172179
static inline int vhost_has_feature(struct vhost_dev *dev, int bit)

0 commit comments

Comments
 (0)