Skip to content

Commit 247643f

Browse files
eugpermarmstsirkin
authored andcommitted
vhost: Create accessors for virtqueues private_data
Signed-off-by: Eugenio Pérez <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent 21818ed commit 247643f

File tree

5 files changed

+61
-32
lines changed

5 files changed

+61
-32
lines changed

drivers/vhost/net.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ static void vhost_net_disable_vq(struct vhost_net *n,
424424
struct vhost_net_virtqueue *nvq =
425425
container_of(vq, struct vhost_net_virtqueue, vq);
426426
struct vhost_poll *poll = n->poll + (nvq - n->vqs);
427-
if (!vq->private_data)
427+
if (!vhost_vq_get_backend(vq))
428428
return;
429429
vhost_poll_stop(poll);
430430
}
@@ -437,7 +437,7 @@ static int vhost_net_enable_vq(struct vhost_net *n,
437437
struct vhost_poll *poll = n->poll + (nvq - n->vqs);
438438
struct socket *sock;
439439

440-
sock = vq->private_data;
440+
sock = vhost_vq_get_backend(vq);
441441
if (!sock)
442442
return 0;
443443

@@ -524,7 +524,7 @@ static void vhost_net_busy_poll(struct vhost_net *net,
524524
return;
525525

526526
vhost_disable_notify(&net->dev, vq);
527-
sock = rvq->private_data;
527+
sock = vhost_vq_get_backend(rvq);
528528

529529
busyloop_timeout = poll_rx ? rvq->busyloop_timeout:
530530
tvq->busyloop_timeout;
@@ -570,8 +570,10 @@ static int vhost_net_tx_get_vq_desc(struct vhost_net *net,
570570

571571
if (r == tvq->num && tvq->busyloop_timeout) {
572572
/* Flush batched packets first */
573-
if (!vhost_sock_zcopy(tvq->private_data))
574-
vhost_tx_batch(net, tnvq, tvq->private_data, msghdr);
573+
if (!vhost_sock_zcopy(vhost_vq_get_backend(tvq)))
574+
vhost_tx_batch(net, tnvq,
575+
vhost_vq_get_backend(tvq),
576+
msghdr);
575577

576578
vhost_net_busy_poll(net, rvq, tvq, busyloop_intr, false);
577579

@@ -685,7 +687,7 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,
685687
struct vhost_virtqueue *vq = &nvq->vq;
686688
struct vhost_net *net = container_of(vq->dev, struct vhost_net,
687689
dev);
688-
struct socket *sock = vq->private_data;
690+
struct socket *sock = vhost_vq_get_backend(vq);
689691
struct page_frag *alloc_frag = &net->page_frag;
690692
struct virtio_net_hdr *gso;
691693
struct xdp_buff *xdp = &nvq->xdp[nvq->batched_xdp];
@@ -952,7 +954,7 @@ static void handle_tx(struct vhost_net *net)
952954
struct socket *sock;
953955

954956
mutex_lock_nested(&vq->mutex, VHOST_NET_VQ_TX);
955-
sock = vq->private_data;
957+
sock = vhost_vq_get_backend(vq);
956958
if (!sock)
957959
goto out;
958960

@@ -1121,7 +1123,7 @@ static void handle_rx(struct vhost_net *net)
11211123
int recv_pkts = 0;
11221124

11231125
mutex_lock_nested(&vq->mutex, VHOST_NET_VQ_RX);
1124-
sock = vq->private_data;
1126+
sock = vhost_vq_get_backend(vq);
11251127
if (!sock)
11261128
goto out;
11271129

@@ -1345,9 +1347,9 @@ static struct socket *vhost_net_stop_vq(struct vhost_net *n,
13451347
container_of(vq, struct vhost_net_virtqueue, vq);
13461348

13471349
mutex_lock(&vq->mutex);
1348-
sock = vq->private_data;
1350+
sock = vhost_vq_get_backend(vq);
13491351
vhost_net_disable_vq(n, vq);
1350-
vq->private_data = NULL;
1352+
vhost_vq_set_backend(vq, NULL);
13511353
vhost_net_buf_unproduce(nvq);
13521354
nvq->rx_ring = NULL;
13531355
mutex_unlock(&vq->mutex);
@@ -1521,7 +1523,7 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
15211523
}
15221524

15231525
/* start polling new socket */
1524-
oldsock = vq->private_data;
1526+
oldsock = vhost_vq_get_backend(vq);
15251527
if (sock != oldsock) {
15261528
ubufs = vhost_net_ubuf_alloc(vq,
15271529
sock && vhost_sock_zcopy(sock));
@@ -1531,7 +1533,7 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
15311533
}
15321534

15331535
vhost_net_disable_vq(n, vq);
1534-
vq->private_data = sock;
1536+
vhost_vq_set_backend(vq, sock);
15351537
vhost_net_buf_unproduce(nvq);
15361538
r = vhost_vq_init_access(vq);
15371539
if (r)
@@ -1568,7 +1570,7 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
15681570
return 0;
15691571

15701572
err_used:
1571-
vq->private_data = oldsock;
1573+
vhost_vq_set_backend(vq, oldsock);
15721574
vhost_net_enable_vq(n, vq);
15731575
if (ubufs)
15741576
vhost_net_ubuf_put_wait_and_free(ubufs);

drivers/vhost/scsi.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
452452
unsigned out, in;
453453
int head, ret;
454454

455-
if (!vq->private_data) {
455+
if (!vhost_vq_get_backend(vq)) {
456456
vs->vs_events_missed = true;
457457
return;
458458
}
@@ -892,7 +892,7 @@ vhost_scsi_get_req(struct vhost_virtqueue *vq, struct vhost_scsi_ctx *vc,
892892
} else {
893893
struct vhost_scsi_tpg **vs_tpg, *tpg;
894894

895-
vs_tpg = vq->private_data; /* validated at handler entry */
895+
vs_tpg = vhost_vq_get_backend(vq); /* validated at handler entry */
896896

897897
tpg = READ_ONCE(vs_tpg[*vc->target]);
898898
if (unlikely(!tpg)) {
@@ -929,7 +929,7 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
929929
* We can handle the vq only after the endpoint is setup by calling the
930930
* VHOST_SCSI_SET_ENDPOINT ioctl.
931931
*/
932-
vs_tpg = vq->private_data;
932+
vs_tpg = vhost_vq_get_backend(vq);
933933
if (!vs_tpg)
934934
goto out;
935935

@@ -1184,7 +1184,7 @@ vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
11841184
* We can handle the vq only after the endpoint is setup by calling the
11851185
* VHOST_SCSI_SET_ENDPOINT ioctl.
11861186
*/
1187-
if (!vq->private_data)
1187+
if (!vhost_vq_get_backend(vq))
11881188
goto out;
11891189

11901190
memset(&vc, 0, sizeof(vc));
@@ -1322,7 +1322,7 @@ static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
13221322
struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
13231323

13241324
mutex_lock(&vq->mutex);
1325-
if (!vq->private_data)
1325+
if (!vhost_vq_get_backend(vq))
13261326
goto out;
13271327

13281328
if (vs->vs_events_missed)
@@ -1460,7 +1460,7 @@ vhost_scsi_set_endpoint(struct vhost_scsi *vs,
14601460
for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
14611461
vq = &vs->vqs[i].vq;
14621462
mutex_lock(&vq->mutex);
1463-
vq->private_data = vs_tpg;
1463+
vhost_vq_set_backend(vq, vs_tpg);
14641464
vhost_vq_init_access(vq);
14651465
mutex_unlock(&vq->mutex);
14661466
}
@@ -1547,7 +1547,7 @@ vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
15471547
for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
15481548
vq = &vs->vqs[i].vq;
15491549
mutex_lock(&vq->mutex);
1550-
vq->private_data = NULL;
1550+
vhost_vq_set_backend(vq, NULL);
15511551
mutex_unlock(&vq->mutex);
15521552
}
15531553
}

drivers/vhost/test.c

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

5151
mutex_lock(&vq->mutex);
52-
private = vq->private_data;
52+
private = vhost_vq_get_backend(vq);
5353
if (!private) {
5454
mutex_unlock(&vq->mutex);
5555
return;
@@ -133,8 +133,8 @@ static void *vhost_test_stop_vq(struct vhost_test *n,
133133
void *private;
134134

135135
mutex_lock(&vq->mutex);
136-
private = vq->private_data;
137-
vq->private_data = NULL;
136+
private = vhost_vq_get_backend(vq);
137+
vhost_vq_set_backend(vq, NULL);
138138
mutex_unlock(&vq->mutex);
139139
return private;
140140
}
@@ -198,8 +198,8 @@ static long vhost_test_run(struct vhost_test *n, int test)
198198
priv = test ? n : NULL;
199199

200200
/* start polling new socket */
201-
oldpriv = vq->private_data;
202-
vq->private_data = priv;
201+
oldpriv = vhost_vq_get_backend(vq);
202+
vhost_vq_set_backend(vq, priv);
203203

204204
r = vhost_vq_init_access(&n->vqs[index]);
205205

drivers/vhost/vhost.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,33 @@ enum {
231231
(1ULL << VIRTIO_F_VERSION_1)
232232
};
233233

234+
/**
235+
* vhost_vq_set_backend - Set backend.
236+
*
237+
* @vq Virtqueue.
238+
* @private_data The private data.
239+
*
240+
* Context: Need to call with vq->mutex acquired.
241+
*/
242+
static inline void vhost_vq_set_backend(struct vhost_virtqueue *vq,
243+
void *private_data)
244+
{
245+
vq->private_data = private_data;
246+
}
247+
248+
/**
249+
* vhost_vq_get_backend - Get backend.
250+
*
251+
* @vq Virtqueue.
252+
*
253+
* Context: Need to call with vq->mutex acquired.
254+
* Return: Private data previously set with vhost_vq_set_backend.
255+
*/
256+
static inline void *vhost_vq_get_backend(struct vhost_virtqueue *vq)
257+
{
258+
return vq->private_data;
259+
}
260+
234261
static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
235262
{
236263
return vq->acked_features & (1ULL << bit);

drivers/vhost/vsock.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
9191

9292
mutex_lock(&vq->mutex);
9393

94-
if (!vq->private_data)
94+
if (!vhost_vq_get_backend(vq))
9595
goto out;
9696

9797
/* Avoid further vmexits, we're already processing the virtqueue */
@@ -440,7 +440,7 @@ static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
440440

441441
mutex_lock(&vq->mutex);
442442

443-
if (!vq->private_data)
443+
if (!vhost_vq_get_backend(vq))
444444
goto out;
445445

446446
vhost_disable_notify(&vsock->dev, vq);
@@ -533,8 +533,8 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
533533
goto err_vq;
534534
}
535535

536-
if (!vq->private_data) {
537-
vq->private_data = vsock;
536+
if (!vhost_vq_get_backend(vq)) {
537+
vhost_vq_set_backend(vq, vsock);
538538
ret = vhost_vq_init_access(vq);
539539
if (ret)
540540
goto err_vq;
@@ -547,14 +547,14 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
547547
return 0;
548548

549549
err_vq:
550-
vq->private_data = NULL;
550+
vhost_vq_set_backend(vq, NULL);
551551
mutex_unlock(&vq->mutex);
552552

553553
for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
554554
vq = &vsock->vqs[i];
555555

556556
mutex_lock(&vq->mutex);
557-
vq->private_data = NULL;
557+
vhost_vq_set_backend(vq, NULL);
558558
mutex_unlock(&vq->mutex);
559559
}
560560
err:
@@ -577,7 +577,7 @@ static int vhost_vsock_stop(struct vhost_vsock *vsock)
577577
struct vhost_virtqueue *vq = &vsock->vqs[i];
578578

579579
mutex_lock(&vq->mutex);
580-
vq->private_data = NULL;
580+
vhost_vq_set_backend(vq, NULL);
581581
mutex_unlock(&vq->mutex);
582582
}
583583

0 commit comments

Comments
 (0)