Skip to content

Commit b29fc61

Browse files
chejianjwenlingz
authored andcommitted
dm: virtio-net: apply new mevent API to avoid race issue
Teardown callback is provided when mevent_add is called and it is used to free the virtio-net resources. Tracked-On: #1877 Signed-off-by: Jian Jun Chen <jian.jun.chen@intel.com> Acked-by: Yin Fengwei <fengwei.yin@intel.com>
1 parent 4f36244 commit b29fc61

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

devicemodel/hw/pci/virtio/virtio_net.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ static int virtio_net_cfgwrite(void *vdev, int offset, int size,
180180
uint32_t value);
181181
static void virtio_net_neg_features(void *vdev, uint64_t negotiated_features);
182182
static void virtio_net_set_status(void *vdev, uint64_t status);
183+
static void virtio_net_teardown(void *param);
183184
static struct vhost_net *vhost_net_init(struct virtio_base *base, int vhostfd,
184185
int tapfd, int vq_idx);
185186
static int vhost_net_deinit(struct vhost_net *vhost_net);
@@ -707,7 +708,8 @@ virtio_net_tap_setup(struct virtio_net *net, char *devname)
707708

708709
if (vhost_fd < 0) {
709710
net->mevp = mevent_add(net->tapfd, EVF_READ,
710-
virtio_net_rx_callback, net, NULL, NULL);
711+
virtio_net_rx_callback, net,
712+
virtio_net_teardown, net);
711713
if (net->mevp == NULL) {
712714
WPRINTF(("Could not register event\n"));
713715
close(net->tapfd);
@@ -934,10 +936,8 @@ virtio_net_set_status(void *vdev, uint64_t status)
934936

935937
if (!net->vhost_net->vhost_started &&
936938
(status & VIRTIO_CR_STATUS_DRIVER_OK)) {
937-
if (net->mevp) {
938-
mevent_delete(net->mevp);
939-
net->mevp = NULL;
940-
}
939+
if (net->mevp)
940+
mevent_disable(net->mevp);
941941

942942
rc = vhost_net_start(net->vhost_net);
943943
if (rc < 0) {
@@ -952,6 +952,24 @@ virtio_net_set_status(void *vdev, uint64_t status)
952952
}
953953
}
954954

955+
static void
956+
virtio_net_teardown(void *param)
957+
{
958+
struct virtio_net *net;
959+
960+
net = (struct virtio_net *)param;
961+
if (!net)
962+
return;
963+
964+
if (net->tapfd >= 0) {
965+
close(net->tapfd);
966+
net->tapfd = -1;
967+
} else
968+
fprintf(stderr, "net->tapfd is -1!\n");
969+
970+
free(net);
971+
}
972+
955973
static void
956974
virtio_net_deinit(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
957975
{
@@ -969,16 +987,10 @@ virtio_net_deinit(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
969987
net->vhost_net = NULL;
970988
}
971989

972-
if (net->tapfd >= 0) {
973-
close(net->tapfd);
974-
net->tapfd = -1;
975-
} else
976-
fprintf(stderr, "net->tapfd is -1!\n");
977-
978990
if (net->mevp != NULL)
979991
mevent_delete(net->mevp);
980-
981-
free(net);
992+
else
993+
virtio_net_teardown(net);
982994

983995
DPRINTF(("%s: done\n", __func__));
984996
} else

0 commit comments

Comments
 (0)