Skip to contentSkip to conversation

net/vhost: fix vhost invalid state

PATCH Superseded Junjie Chen 4 participants 17 messages Apr 10, 2018 – Apr 12, 2018 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/vhost: fix vhost invalid state

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by *dev_attached* but by queues pointers or other separated variable(s).

Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

---
 drivers/net/vhost/rte_eth_vhost.c | 64 +++++++++++++++++++++++----------------
 1 file changed, 38 insertions(+), 26 deletions(-)
drivers/net/vhost/rte_eth_vhost.c +38 -26
index 11b6076..6a2ff76 100644
@@ -118,6 +118,7 @@ struct pmd_internal {
 	char *iface_name;
 	uint16_t max_queues;
 	uint16_t vid;
+	uint16_t queue_ready;
 	rte_atomic32_t started;
 };
 
@@ -528,10 +529,13 @@ update_queuing_status(struct rte_eth_dev *dev)
 	unsigned int i;
 	int allow_queuing = 1;
 
-	if (rte_atomic32_read(&internal->dev_attached) == 0)
+	if (!dev->data->rx_queues || !dev->data->tx_queues) {
+		RTE_LOG(ERR, PMD, "RX/TX queues not setup yet\n");
 		return;
+	}
 
-	if (rte_atomic32_read(&internal->started) == 0)
+	if (rte_atomic32_read(&internal->started) == 0 ||
+	    rte_atomic32_read(&internal->dev_attached) == 0)
 		allow_queuing = 0;
 
 	/* Wait until rx/tx_pkt_burst stops accessing vhost device */
@@ -576,6 +580,8 @@ queue_setup(struct rte_eth_dev *eth_dev, struct pmd_internal *internal)
 		vq->internal = internal;
 		vq->port = eth_dev->data->port_id;
 	}
+
+	internal->queue_ready = 1;
 }
 
 static int
@@ -607,13 +613,10 @@ new_device(int vid)
 #endif
 
 	internal->vid = vid;
-	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues)
 		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	} else {
-		RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
-		rte_atomic32_set(&internal->dev_attached, 0);
-	}
+	else
+		RTE_LOG(INFO, PMD, "RX/TX queues not setup yet\n");
 
 	for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
 		rte_vhost_enable_guest_notification(vid, i, 0);
@@ -622,6 +625,7 @@ new_device(int vid)
 
 	eth_dev->data->dev_link.link_status = ETH_LINK_UP;
 
+	rte_atomic32_set(&internal->dev_attached, 1);
 	update_queuing_status(eth_dev);
 
 	RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid);
@@ -657,17 +661,19 @@ destroy_device(int vid)
 
 	eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
 
-	for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
-		vq = eth_dev->data->rx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
-	}
-	for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
-		vq = eth_dev->data->tx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
+	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+		for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
+			vq = eth_dev->data->rx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
+		for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
+			vq = eth_dev->data->tx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
 	}
 
 	state = vring_states[eth_dev->data->port_id];
@@ -792,11 +798,14 @@ eth_dev_start(struct rte_eth_dev *eth_dev)
 {
 	struct pmd_internal *internal = eth_dev->data->dev_private;
 
-	if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
-		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
+	if (!eth_dev->data->rx_queues || !eth_dev->data->tx_queues) {
+		RTE_LOG(ERR, PMD, "RX/TX queues not exist yet\n");
+		return -1;
 	}
 
+	if (!internal->queue_ready)
+		queue_setup(eth_dev, internal);
+
 	rte_atomic32_set(&internal->started, 1);
 	update_queuing_status(eth_dev);
 
@@ -836,10 +845,13 @@ eth_dev_close(struct rte_eth_dev *dev)
 	pthread_mutex_unlock(&internal_list_lock);
 	rte_free(list);
 
-	for (i = 0; i < dev->data->nb_rx_queues; i++)
-		rte_free(dev->data->rx_queues[i]);
-	for (i = 0; i < dev->data->nb_tx_queues; i++)
-		rte_free(dev->data->tx_queues[i]);
+	if (dev->data->rx_queues)
+		for (i = 0; i < dev->data->nb_rx_queues; i++)
+			rte_free(dev->data->rx_queues[i]);
+
+	if (dev->data->tx_queues)
+		for (i = 0; i < dev->data->nb_tx_queues; i++)
+			rte_free(dev->data->tx_queues[i]);
 
 	rte_free(dev->data->mac_addrs);
 	free(internal->dev_name);
Signature · 1 line
2.0.1
#1 lore raw

Hi Junjie,

I think the code is still buggy. As vhost thread and master thread are separately invoking new_device() and dev_start().

Quoted 19 lines · Junjie Chen

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by *dev_attached* but by queues pointers or other separated variable(s).

Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

---
  drivers/net/vhost/rte_eth_vhost.c | 64 +++++++++++++++++++++++----------------
  1 file changed, 38 insertions(+), 26 deletions(-)
drivers/net/vhost/rte_eth_vhost.c +0 -0
index 11b6076..6a2ff76 100644

drivers/net/vhost/rte_eth_vhost.c struct pmd_internal { · line 121

Quoted 4 lines
drivers/net/vhost/rte_eth_vhost.c +1 -0
@@ -118,6 +118,7 @@ struct pmd_internal {
  	char *iface_name;
  	uint16_t max_queues;
  	uint16_t vid;
+	uint16_t queue_ready;

If we can reuse below *started* field, instead of introducing a new variable?

Quoted 31 lines
drivers/net/vhost/rte_eth_vhost.c +7 -2
  	rte_atomic32_t started;
  };
 
@@ -528,10 +529,13 @@ update_queuing_status(struct rte_eth_dev *dev)
  	unsigned int i;
  	int allow_queuing = 1;
 
-	if (rte_atomic32_read(&internal->dev_attached) == 0)
+	if (!dev->data->rx_queues || !dev->data->tx_queues) {
+		RTE_LOG(ERR, PMD, "RX/TX queues not setup yet\n");
  		return;
+	}
 
-	if (rte_atomic32_read(&internal->started) == 0)
+	if (rte_atomic32_read(&internal->started) == 0 ||
+	    rte_atomic32_read(&internal->dev_attached) == 0)
  		allow_queuing = 0;
 
  	/* Wait until rx/tx_pkt_burst stops accessing vhost device */
@@ -576,6 +580,8 @@ queue_setup(struct rte_eth_dev *eth_dev, struct pmd_internal *internal)
  		vq->internal = internal;
  		vq->port = eth_dev->data->port_id;
  	}
+
+	internal->queue_ready = 1;
  }
 
  static int
@@ -607,13 +613,10 @@ new_device(int vid)
  #endif
 

drivers/net/vhost/rte_eth_vhost.c new_device(int vid) · line 618

Quoted 10 lines
drivers/net/vhost/rte_eth_vhost.c +2 -6
  	internal->vid = vid;
-	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues)
  		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	} else {
-		RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
-		rte_atomic32_set(&internal->dev_attached, 0);
-	}
+	else

vhost thread (t1): goes here and before setting dev_attached ...

Quoted 44 lines
drivers/net/vhost/rte_eth_vhost.c +15 -11
+		RTE_LOG(INFO, PMD, "RX/TX queues not setup yet\n");
 
  	for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
  		rte_vhost_enable_guest_notification(vid, i, 0);
@@ -622,6 +625,7 @@ new_device(int vid)
 
  	eth_dev->data->dev_link.link_status = ETH_LINK_UP;
 
+	rte_atomic32_set(&internal->dev_attached, 1);
  	update_queuing_status(eth_dev);
 
  	RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid);
@@ -657,17 +661,19 @@ destroy_device(int vid)
 
  	eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
 
-	for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
-		vq = eth_dev->data->rx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
-	}
-	for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
-		vq = eth_dev->data->tx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
+	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+		for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
+			vq = eth_dev->data->rx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
+		for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
+			vq = eth_dev->data->tx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
  	}
 
  	state = vring_states[eth_dev->data->port_id];

drivers/net/vhost/rte_eth_vhost.c eth_dev_start(struct rte_eth_dev *eth_dev) · line 804

Quoted 10 lines
drivers/net/vhost/rte_eth_vhost.c +3 -3
@@ -792,11 +798,14 @@ eth_dev_start(struct rte_eth_dev *eth_dev)
  {
  	struct pmd_internal *internal = eth_dev->data->dev_private;
 
-	if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
-		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
+	if (!eth_dev->data->rx_queues || !eth_dev->data->tx_queues) {
+		RTE_LOG(ERR, PMD, "RX/TX queues not exist yet\n");
+		return -1;
  	}

I think in DPDK, we can make the assumption that dev_start() comes later than queue setup. So we don't need this check.

drivers/net/vhost/rte_eth_vhost.c eth_dev_start(struct rte_eth_dev *eth_dev) · line 807

Quoted 3 lines
drivers/net/vhost/rte_eth_vhost.c +2 -0
+	if (!internal->queue_ready)
+		queue_setup(eth_dev, internal);

Master thread (t2): setting invalid values.

Quoted 22 lines
drivers/net/vhost/rte_eth_vhost.c +8 -4
+
  	rte_atomic32_set(&internal->started, 1);
  	update_queuing_status(eth_dev);
 
@@ -836,10 +845,13 @@ eth_dev_close(struct rte_eth_dev *dev)
  	pthread_mutex_unlock(&internal_list_lock);
  	rte_free(list);
 
-	for (i = 0; i < dev->data->nb_rx_queues; i++)
-		rte_free(dev->data->rx_queues[i]);
-	for (i = 0; i < dev->data->nb_tx_queues; i++)
-		rte_free(dev->data->tx_queues[i]);
+	if (dev->data->rx_queues)
+		for (i = 0; i < dev->data->nb_rx_queues; i++)
+			rte_free(dev->data->rx_queues[i]);
+
+	if (dev->data->tx_queues)
+		for (i = 0; i < dev->data->nb_tx_queues; i++)
+			rte_free(dev->data->tx_queues[i]);
 
  	rte_free(dev->data->mac_addrs);
  	free(internal->dev_name);
#2 lore raw
In reply to Junjie Chen
Quoted 10 lines · Junjie Chen

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by *dev_attached* but by queues pointers or other separated variable(s).

Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

So this fixes the problem I saw with allow_queueing always being zero and the error message "VHOST_CONFIG: (0) device not found".

However with this patch on top of virtio-next/master no packets are being forwarded to the guest and back anymore.

When I use virtio-next/master and revert 30a701a53737 both works fine.

regards,
Jens

Quoted 136 lines

---
drivers/net/vhost/rte_eth_vhost.c | 64 +++++++++++++++++++++++----------------

1 file changed, 38 insertions(+), 26 deletions(-)
drivers/net/vhost/rte_eth_vhost.c +8 -2
index 11b6076..6a2ff76 100644
@@ -118,6 +118,7 @@ struct pmd_internal {
 	char *iface_name;
 	uint16_t max_queues;
 	uint16_t vid;
+	uint16_t queue_ready;
 	rte_atomic32_t started;
 };
 
@@ -528,10 +529,13 @@ update_queuing_status(struct rte_eth_dev *dev)
 	unsigned int i;
 	int allow_queuing = 1;
 
-	if (rte_atomic32_read(&internal->dev_attached) == 0)
+	if (!dev->data->rx_queues || !dev->data->tx_queues) {
+		RTE_LOG(ERR, PMD, "RX/TX queues not setup yet\n");
 		return;
+	}
 
-	if (rte_atomic32_read(&internal->started) == 0)
+	if (rte_atomic32_read(&internal->started) == 0 ||
+	    rte_atomic32_read(&internal->dev_attached) == 0)
 		allow_queuing = 0;
 
 	/* Wait until rx/tx_pkt_burst stops accessing vhost device */
@@ -576,6 +580,8 @@ queue_setup(struct rte_eth_dev *eth_dev, struct pmd_internal *internal)
 		vq->internal = internal;
 		vq->port = eth_dev->data->port_id;
 	}
+
+	internal->queue_ready = 1;
 }
 

static int

drivers/net/vhost/rte_eth_vhost.c +30 -24
@@ -607,13 +613,10 @@ new_device(int vid)
 #endif
 
 	internal->vid = vid;
-	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues)
 		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	} else {
-		RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
-		rte_atomic32_set(&internal->dev_attached, 0);
-	}
+	else
+		RTE_LOG(INFO, PMD, "RX/TX queues not setup yet\n");
 
 	for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
 		rte_vhost_enable_guest_notification(vid, i, 0);
@@ -622,6 +625,7 @@ new_device(int vid)
 
 	eth_dev->data->dev_link.link_status = ETH_LINK_UP;
 
+	rte_atomic32_set(&internal->dev_attached, 1);
 	update_queuing_status(eth_dev);
 
 	RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid);
@@ -657,17 +661,19 @@ destroy_device(int vid)
 
 	eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
 
-	for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
-		vq = eth_dev->data->rx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
-	}
-	for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
-		vq = eth_dev->data->tx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
+	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+		for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
+			vq = eth_dev->data->rx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
+		for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
+			vq = eth_dev->data->tx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
 	}
 
 	state = vring_states[eth_dev->data->port_id];
@@ -792,11 +798,14 @@ eth_dev_start(struct rte_eth_dev *eth_dev)
 {
 	struct pmd_internal *internal = eth_dev->data->dev_private;
 
-	if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
-		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
+	if (!eth_dev->data->rx_queues || !eth_dev->data->tx_queues) {
+		RTE_LOG(ERR, PMD, "RX/TX queues not exist yet\n");
+		return -1;
 	}
 
+	if (!internal->queue_ready)
+		queue_setup(eth_dev, internal);
+
 	rte_atomic32_set(&internal->started, 1);
 	update_queuing_status(eth_dev);
 
@@ -836,10 +845,13 @@ eth_dev_close(struct rte_eth_dev *dev)
 	pthread_mutex_unlock(&internal_list_lock);
 	rte_free(list);
 
-	for (i = 0; i < dev->data->nb_rx_queues; i++)
-		rte_free(dev->data->rx_queues[i]);
-	for (i = 0; i < dev->data->nb_tx_queues; i++)
-		rte_free(dev->data->tx_queues[i]);
+	if (dev->data->rx_queues)
+		for (i = 0; i < dev->data->nb_rx_queues; i++)
+			rte_free(dev->data->rx_queues[i]);
+
+	if (dev->data->tx_queues)
+		for (i = 0; i < dev->data->nb_tx_queues; i++)
+			rte_free(dev->data->tx_queues[i]);
 
 	rte_free(dev->data->mac_addrs);
 	free(internal->dev_name);
Signature · 1 line
2.0.1
#3 lore raw

Thanks to point this out, I forgot to run 'git add' before generating patch.

Need following change also:

drivers/net/vhost/rte_eth_vhost.c +1 -2
@@ -655,9 +655,8 @@ destroy_device(int vid)
        eth_dev = list->eth_dev;
        internal = eth_dev->data->dev_private;
 
-       rte_atomic32_set(&internal->started, 0);
-       update_queuing_status(eth_dev);
        rte_atomic32_set(&internal->dev_attached, 0);
+       update_queuing_status(eth_dev);
 
        eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
 

Will send out V2 to include this.

Cheers
JJ

Quoted 170 lines

-----Original Message-----
From: Jens Freimann [mailto:[email protected]]
Sent: Tuesday, April 10, 2018 7:13 PM
To: Chen, Junjie J <[email protected]>
Cc: Tan, Jianfeng <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [dpdk-dev] [PATCH] net/vhost: fix vhost invalid state

Quoted 11 lines · Junjie Chen

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by
*dev_attached* but by queues pointers or other separated variable(s).

Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

So this fixes the problem I saw with allow_queueing always being zero and the error message "VHOST_CONFIG: (0) device not found".

However with this patch on top of virtio-next/master no packets are being forwarded to the guest and back anymore.

When I use virtio-next/master and revert 30a701a53737 both works fine.

regards,
Jens

Quoted 25 lines

---
drivers/net/vhost/rte_eth_vhost.c | 64
+++++++++++++++++++++++----------------

1 file changed, 38 insertions(+), 26 deletions(-)
drivers/net/vhost/rte_eth_vhost.c +3 -1
index 11b6076..6a2ff76 100644
@@ -118,6 +118,7 @@ struct pmd_internal {
 	char *iface_name;
 	uint16_t max_queues;
 	uint16_t vid;
+	uint16_t queue_ready;
 	rte_atomic32_t started;
 };
 
@@ -528,10 +529,13 @@ update_queuing_status(struct rte_eth_dev *dev)
 	unsigned int i;
 	int allow_queuing = 1;
 
-	if (rte_atomic32_read(&internal->dev_attached) == 0)
+	if (!dev->data->rx_queues || !dev->data->tx_queues) {
+		RTE_LOG(ERR, PMD, "RX/TX queues not setup yet\n");

drivers/net/vhost/rte_eth_vhost.c update_queuing_status(struct rte_eth_dev *dev)

Quoted 10 lines
drivers/net/vhost/rte_eth_vhost.c +3 -2
 		return;
+	}
 
-	if (rte_atomic32_read(&internal->started) == 0)
+	if (rte_atomic32_read(&internal->started) == 0 ||
+	    rte_atomic32_read(&internal->dev_attached) == 0)
 		allow_queuing = 0;
 
 	/* Wait until rx/tx_pkt_burst stops accessing vhost device */ @@
-576,6 +580,8 @@ queue_setup(struct rte_eth_dev *eth_dev, struct

pmd_internal *internal)

Quoted 14 lines
		vq->internal = internal;
		vq->port = eth_dev->data->port_id;
	}
drivers/net/vhost/rte_eth_vhost.c +2 -0
+
+	internal->queue_ready = 1;

}

static int

drivers/net/vhost/rte_eth_vhost.c +1 -1
@@ -607,13 +613,10 @@ new_device(int vid)  #endif
 
 	internal->vid = vid;
-	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues)
 		queue_setup(eth_dev, internal);

drivers/net/vhost/rte_eth_vhost.c new_device(int vid) #endif · line 621

Quoted 10 lines
drivers/net/vhost/rte_eth_vhost.c +2 -5
-		rte_atomic32_set(&internal->dev_attached, 1);
-	} else {
-		RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
-		rte_atomic32_set(&internal->dev_attached, 0);
-	}
+	else
+		RTE_LOG(INFO, PMD, "RX/TX queues not setup yet\n");
 
 	for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
 		rte_vhost_enable_guest_notification(vid, i, 0); @@ -622,6 +625,7

@@

Quoted 77 lines
drivers/net/vhost/rte_eth_vhost.c +0 -0
 new_device(int vid)
	eth_dev->data->dev_link.link_status = ETH_LINK_UP;
drivers/net/vhost/rte_eth_vhost.c +1 -0
+	rte_atomic32_set(&internal->dev_attached, 1);
	update_queuing_status(eth_dev);

	RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid); @@ -657,17

+661,19 @@ destroy_device(int vid)

	eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
drivers/net/vhost/rte_eth_vhost.c +13 -11
-	for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
-		vq = eth_dev->data->rx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
-	}
-	for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
-		vq = eth_dev->data->tx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
+	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+		for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
+			vq = eth_dev->data->rx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
+		for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
+			vq = eth_dev->data->tx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
	}

	state = vring_states[eth_dev->data->port_id];
drivers/net/vhost/rte_eth_vhost.c +13 -8
@@ -792,11 +798,14 @@ eth_dev_start(struct rte_eth_dev *eth_dev)  {
 	struct pmd_internal *internal = eth_dev->data->dev_private;
 
-	if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
-		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
+	if (!eth_dev->data->rx_queues || !eth_dev->data->tx_queues) {
+		RTE_LOG(ERR, PMD, "RX/TX queues not exist yet\n");
+		return -1;
 	}
 
+	if (!internal->queue_ready)
+		queue_setup(eth_dev, internal);
+
 	rte_atomic32_set(&internal->started, 1);
 	update_queuing_status(eth_dev);
 
@@ -836,10 +845,13 @@ eth_dev_close(struct rte_eth_dev *dev)
 	pthread_mutex_unlock(&internal_list_lock);
 	rte_free(list);
 
-	for (i = 0; i < dev->data->nb_rx_queues; i++)
-		rte_free(dev->data->rx_queues[i]);
-	for (i = 0; i < dev->data->nb_tx_queues; i++)
-		rte_free(dev->data->tx_queues[i]);
+	if (dev->data->rx_queues)
+		for (i = 0; i < dev->data->nb_rx_queues; i++)
+			rte_free(dev->data->rx_queues[i]);
+
+	if (dev->data->tx_queues)
+		for (i = 0; i < dev->data->nb_tx_queues; i++)
+			rte_free(dev->data->tx_queues[i]);
 
 	rte_free(dev->data->mac_addrs);
 	free(internal->dev_name);
--

2.0.1

#4 lore raw

PATCH v2 1/1 net/vhost: fix vhost invalid state

In reply to Junjie Chen

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by *dev_attached* but by queues pointers or other separated variable(s).

Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

---
Changes in v2:
- use started to determine vhost queues readiness
- revert setting started to zero in destroy_device

 drivers/net/vhost/rte_eth_vhost.c | 61 ++++++++++++++++++++-------------------
 1 file changed, 31 insertions(+), 30 deletions(-)
drivers/net/vhost/rte_eth_vhost.c +31 -30
index 11b6076..ff462b3 100644
@@ -528,10 +528,13 @@ update_queuing_status(struct rte_eth_dev *dev)
 	unsigned int i;
 	int allow_queuing = 1;
 
-	if (rte_atomic32_read(&internal->dev_attached) == 0)
+	if (!dev->data->rx_queues || !dev->data->tx_queues) {
+		RTE_LOG(ERR, PMD, "RX/TX queues not exist yet\n");
 		return;
+	}
 
-	if (rte_atomic32_read(&internal->started) == 0)
+	if (rte_atomic32_read(&internal->started) == 0 ||
+	    rte_atomic32_read(&internal->dev_attached) == 0)
 		allow_queuing = 0;
 
 	/* Wait until rx/tx_pkt_burst stops accessing vhost device */
@@ -607,13 +610,10 @@ new_device(int vid)
 #endif
 
 	internal->vid = vid;
-	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+	if (rte_atomic32_read(&internal->started) == 1)
 		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	} else {
-		RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
-		rte_atomic32_set(&internal->dev_attached, 0);
-	}
+	else
+		RTE_LOG(INFO, PMD, "RX/TX queues not exist yet\n");
 
 	for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
 		rte_vhost_enable_guest_notification(vid, i, 0);
@@ -622,6 +622,7 @@ new_device(int vid)
 
 	eth_dev->data->dev_link.link_status = ETH_LINK_UP;
 
+	rte_atomic32_set(&internal->dev_attached, 1);
 	update_queuing_status(eth_dev);
 
 	RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid);
@@ -651,23 +652,24 @@ destroy_device(int vid)
 	eth_dev = list->eth_dev;
 	internal = eth_dev->data->dev_private;
 
-	rte_atomic32_set(&internal->started, 0);
-	update_queuing_status(eth_dev);
 	rte_atomic32_set(&internal->dev_attached, 0);
+	update_queuing_status(eth_dev);
 
 	eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
 
-	for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
-		vq = eth_dev->data->rx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
-	}
-	for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
-		vq = eth_dev->data->tx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
+	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+		for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
+			vq = eth_dev->data->rx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
+		for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
+			vq = eth_dev->data->tx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
 	}
 
 	state = vring_states[eth_dev->data->port_id];
@@ -792,11 +794,7 @@ eth_dev_start(struct rte_eth_dev *eth_dev)
 {
 	struct pmd_internal *internal = eth_dev->data->dev_private;
 
-	if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
-		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	}
-
+	queue_setup(eth_dev, internal);
 	rte_atomic32_set(&internal->started, 1);
 	update_queuing_status(eth_dev);
 
@@ -836,10 +834,13 @@ eth_dev_close(struct rte_eth_dev *dev)
 	pthread_mutex_unlock(&internal_list_lock);
 	rte_free(list);
 
-	for (i = 0; i < dev->data->nb_rx_queues; i++)
-		rte_free(dev->data->rx_queues[i]);
-	for (i = 0; i < dev->data->nb_tx_queues; i++)
-		rte_free(dev->data->tx_queues[i]);
+	if (dev->data->rx_queues)
+		for (i = 0; i < dev->data->nb_rx_queues; i++)
+			rte_free(dev->data->rx_queues[i]);
+
+	if (dev->data->tx_queues)
+		for (i = 0; i < dev->data->nb_tx_queues; i++)
+			rte_free(dev->data->tx_queues[i]);
 
 	rte_free(dev->data->mac_addrs);
 	free(internal->dev_name);
Signature · 1 line
2.0.1
#5 lore raw
Quoted 5 lines · Junjie Chen

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by *dev_attached* but by queues pointers or other separated variable(s).

I see two issues from your description:

- We shall fix the wrong use of dev_attached by commit 30a701a53737.
- The allow_queuing is not set correctly. But I doubt if it's a real issue, as we do update the queue status in dev_start(), dev_stop(), and new_device(). Can you explain more?

Quoted 17 lines
Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

---
Changes in v2:
- use started to determine vhost queues readiness
- revert setting started to zero in destroy_device

  drivers/net/vhost/rte_eth_vhost.c | 61 ++++++++++++++++++++-------------------
  1 file changed, 31 insertions(+), 30 deletions(-)
drivers/net/vhost/rte_eth_vhost.c +0 -0
index 11b6076..ff462b3 100644

drivers/net/vhost/rte_eth_vhost.c update_queuing_status(struct rte_eth_dev *dev) · line 532

Quoted 6 lines
drivers/net/vhost/rte_eth_vhost.c +2 -1
@@ -528,10 +528,13 @@ update_queuing_status(struct rte_eth_dev *dev)
  	unsigned int i;
  	int allow_queuing = 1;
 
-	if (rte_atomic32_read(&internal->dev_attached) == 0)
+	if (!dev->data->rx_queues || !dev->data->tx_queues) {
+		RTE_LOG(ERR, PMD, "RX/TX queues not exist yet\n");

Even it's not introduced in this patch, but I think we shall not report error log here.

Quoted 103 lines
drivers/net/vhost/rte_eth_vhost.c +29 -29
  		return;
+	}
 
-	if (rte_atomic32_read(&internal->started) == 0)
+	if (rte_atomic32_read(&internal->started) == 0 ||
+	    rte_atomic32_read(&internal->dev_attached) == 0)
  		allow_queuing = 0;
 
  	/* Wait until rx/tx_pkt_burst stops accessing vhost device */
@@ -607,13 +610,10 @@ new_device(int vid)
  #endif
 
  	internal->vid = vid;
-	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+	if (rte_atomic32_read(&internal->started) == 1)
  		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	} else {
-		RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
-		rte_atomic32_set(&internal->dev_attached, 0);
-	}
+	else
+		RTE_LOG(INFO, PMD, "RX/TX queues not exist yet\n");
 
  	for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
  		rte_vhost_enable_guest_notification(vid, i, 0);
@@ -622,6 +622,7 @@ new_device(int vid)
 
  	eth_dev->data->dev_link.link_status = ETH_LINK_UP;
 
+	rte_atomic32_set(&internal->dev_attached, 1);
  	update_queuing_status(eth_dev);
 
  	RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid);
@@ -651,23 +652,24 @@ destroy_device(int vid)
  	eth_dev = list->eth_dev;
  	internal = eth_dev->data->dev_private;
 
-	rte_atomic32_set(&internal->started, 0);
-	update_queuing_status(eth_dev);
  	rte_atomic32_set(&internal->dev_attached, 0);
+	update_queuing_status(eth_dev);
 
  	eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
 
-	for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
-		vq = eth_dev->data->rx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
-	}
-	for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
-		vq = eth_dev->data->tx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
+	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+		for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
+			vq = eth_dev->data->rx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
+		for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
+			vq = eth_dev->data->tx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
  	}
 
  	state = vring_states[eth_dev->data->port_id];
@@ -792,11 +794,7 @@ eth_dev_start(struct rte_eth_dev *eth_dev)
  {
  	struct pmd_internal *internal = eth_dev->data->dev_private;
 
-	if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
-		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	}
-
+	queue_setup(eth_dev, internal);
  	rte_atomic32_set(&internal->started, 1);
  	update_queuing_status(eth_dev);
 
@@ -836,10 +834,13 @@ eth_dev_close(struct rte_eth_dev *dev)
  	pthread_mutex_unlock(&internal_list_lock);
  	rte_free(list);
 
-	for (i = 0; i < dev->data->nb_rx_queues; i++)
-		rte_free(dev->data->rx_queues[i]);
-	for (i = 0; i < dev->data->nb_tx_queues; i++)
-		rte_free(dev->data->tx_queues[i]);
+	if (dev->data->rx_queues)
+		for (i = 0; i < dev->data->nb_rx_queues; i++)
+			rte_free(dev->data->rx_queues[i]);
+
+	if (dev->data->tx_queues)
+		for (i = 0; i < dev->data->nb_tx_queues; i++)
+			rte_free(dev->data->tx_queues[i]);
 
  	rte_free(dev->data->mac_addrs);
  	free(internal->dev_name);
#6 lore raw
Quoted 10 lines · Junjie Chen
Quoted 6 lines

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by
*dev_attached* but by queues pointers or other separated variable(s).

Quoted 6 lines

I see two issues from your description:

- We shall fix the wrong use of dev_attached by commit 30a701a53737.
- The allow_queuing is not set correctly. But I doubt if it's a real issue, as we do update the queue status in dev_start(), dev_stop(), and new_device(). Can you explain more?

The started is set to 0 in destroy_device in 30a701a53737 commit, so allow_queuing is always set to 0 once update_queuing_status get called. We have to use "port start" to reset to 1.

Quoted 23 lines
Quoted 11 lines
Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

---
Changes in v2:
- use started to determine vhost queues readiness
- revert setting started to zero in destroy_device

  drivers/net/vhost/rte_eth_vhost.c | 61

++++++++++++++++++++-------------------

Review

Quoted 3 lines
  1 file changed, 31 insertions(+), 30 deletions(-)
(quoted diff) +0 -0

b/drivers/net/vhost/rte_eth_vhost.c

Quoted 7 lines

index 11b6076..ff462b3 100644

drivers/net/vhost/rte_eth_vhost.c +0 -0
@@ -528,10 +528,13 @@ update_queuing_status(struct rte_eth_dev *dev)
  	unsigned int i;
  	int allow_queuing = 1;
 
Quoted 6 lines

drivers/net/vhost/rte_eth_vhost.c update_queuing_status(struct rte_eth_dev *dev) · line 532

Quoted 3 lines
drivers/net/vhost/rte_eth_vhost.c +2 -1
-	if (rte_atomic32_read(&internal->dev_attached) == 0)
+	if (!dev->data->rx_queues || !dev->data->tx_queues) {
+		RTE_LOG(ERR, PMD, "RX/TX queues not exist yet\n");

Even it's not introduced in this patch, but I think we shall not report error log here.

if you attach one port with "port attach" (no vhost queue setup yet), and then execute "port stop", the queue status is not exist yet, right?

Quoted 104 lines
Quoted 104 lines
drivers/net/vhost/rte_eth_vhost.c +29 -29
  		return;
+	}
 
-	if (rte_atomic32_read(&internal->started) == 0)
+	if (rte_atomic32_read(&internal->started) == 0 ||
+	    rte_atomic32_read(&internal->dev_attached) == 0)
  		allow_queuing = 0;
 
  	/* Wait until rx/tx_pkt_burst stops accessing vhost device */
@@ -607,13 +610,10 @@ new_device(int vid)
  #endif
 
  	internal->vid = vid;
-	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+	if (rte_atomic32_read(&internal->started) == 1)
  		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	} else {
-		RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
-		rte_atomic32_set(&internal->dev_attached, 0);
-	}
+	else
+		RTE_LOG(INFO, PMD, "RX/TX queues not exist yet\n");
 
  	for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
  		rte_vhost_enable_guest_notification(vid, i, 0);
@@ -622,6 +622,7 @@ new_device(int vid)
 
  	eth_dev->data->dev_link.link_status = ETH_LINK_UP;
 
+	rte_atomic32_set(&internal->dev_attached, 1);
  	update_queuing_status(eth_dev);
 
  	RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid);
@@ -651,23 +652,24 @@ destroy_device(int vid)
  	eth_dev = list->eth_dev;
  	internal = eth_dev->data->dev_private;
 
-	rte_atomic32_set(&internal->started, 0);
-	update_queuing_status(eth_dev);
  	rte_atomic32_set(&internal->dev_attached, 0);
+	update_queuing_status(eth_dev);
 
  	eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
 
-	for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
-		vq = eth_dev->data->rx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
-	}
-	for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
-		vq = eth_dev->data->tx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
+	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+		for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
+			vq = eth_dev->data->rx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
+		for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
+			vq = eth_dev->data->tx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
  	}
 
  	state = vring_states[eth_dev->data->port_id];
@@ -792,11 +794,7 @@ eth_dev_start(struct rte_eth_dev *eth_dev)
  {
  	struct pmd_internal *internal = eth_dev->data->dev_private;
 
-	if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
-		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	}
-
+	queue_setup(eth_dev, internal);
  	rte_atomic32_set(&internal->started, 1);
  	update_queuing_status(eth_dev);
 
@@ -836,10 +834,13 @@ eth_dev_close(struct rte_eth_dev *dev)
  	pthread_mutex_unlock(&internal_list_lock);
  	rte_free(list);
 
-	for (i = 0; i < dev->data->nb_rx_queues; i++)
-		rte_free(dev->data->rx_queues[i]);
-	for (i = 0; i < dev->data->nb_tx_queues; i++)
-		rte_free(dev->data->tx_queues[i]);
+	if (dev->data->rx_queues)
+		for (i = 0; i < dev->data->nb_rx_queues; i++)
+			rte_free(dev->data->rx_queues[i]);
+
+	if (dev->data->tx_queues)
+		for (i = 0; i < dev->data->nb_tx_queues; i++)
+			rte_free(dev->data->tx_queues[i]);
 
  	rte_free(dev->data->mac_addrs);
  	free(internal->dev_name);
#7 lore raw
Quoted 8 lines · Junjie J
Quoted 8 lines · Junjie Chen
Quoted 6 lines

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by
*dev_attached* but by queues pointers or other separated variable(s).

Quoted 8 lines
Quoted 6 lines

I see two issues from your description:

- We shall fix the wrong use of dev_attached by commit 30a701a53737.
- The allow_queuing is not set correctly. But I doubt if it's a real issue, as we do update the queue status in dev_start(), dev_stop(), and new_device(). Can you explain more?

The started is set to 0 in destroy_device in 30a701a53737 commit, so allow_queuing is always set to 0 once update_queuing_status get called. We have to use "port start" to reset to 1.

OK, that means it is also due to the wrong use of dev_attached?

Quoted 21 lines
Quoted 21 lines
Quoted 9 lines
Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

---
Changes in v2:
- use started to determine vhost queues readiness
- revert setting started to zero in destroy_device

   drivers/net/vhost/rte_eth_vhost.c | 61

++++++++++++++++++++-------------------

Review

Quoted 3 lines
   1 file changed, 31 insertions(+), 30 deletions(-)
(quoted diff) +0 -0

b/drivers/net/vhost/rte_eth_vhost.c

Quoted 7 lines

index 11b6076..ff462b3 100644

drivers/net/vhost/rte_eth_vhost.c +0 -0
@@ -528,10 +528,13 @@ update_queuing_status(struct rte_eth_dev *dev)
   	unsigned int i;
   	int allow_queuing = 1;
 
Quoted 7 lines
Quoted 5 lines

drivers/net/vhost/rte_eth_vhost.c update_queuing_status(struct rte_eth_dev *dev) · line 532

Quoted 3 lines
drivers/net/vhost/rte_eth_vhost.c +2 -1
-	if (rte_atomic32_read(&internal->dev_attached) == 0)
+	if (!dev->data->rx_queues || !dev->data->tx_queues) {
+		RTE_LOG(ERR, PMD, "RX/TX queues not exist yet\n");

Even it's not introduced in this patch, but I think we shall not report error log here.

if you attach one port with "port attach" (no vhost queue setup yet), and then execute "port stop", the queue status is not exist yet, right?

My point is that we allow a dev is not started but attached, so it's not an error, and we shall not print error log.

Quoted 103 lines
Quoted 103 lines
Quoted 103 lines
drivers/net/vhost/rte_eth_vhost.c +29 -29
   		return;
+	}
 
-	if (rte_atomic32_read(&internal->started) == 0)
+	if (rte_atomic32_read(&internal->started) == 0 ||
+	    rte_atomic32_read(&internal->dev_attached) == 0)
   		allow_queuing = 0;
 
   	/* Wait until rx/tx_pkt_burst stops accessing vhost device */
@@ -607,13 +610,10 @@ new_device(int vid)
   #endif
 
   	internal->vid = vid;
-	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+	if (rte_atomic32_read(&internal->started) == 1)
   		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	} else {
-		RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
-		rte_atomic32_set(&internal->dev_attached, 0);
-	}
+	else
+		RTE_LOG(INFO, PMD, "RX/TX queues not exist yet\n");
 
   	for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
   		rte_vhost_enable_guest_notification(vid, i, 0);
@@ -622,6 +622,7 @@ new_device(int vid)
 
   	eth_dev->data->dev_link.link_status = ETH_LINK_UP;
 
+	rte_atomic32_set(&internal->dev_attached, 1);
   	update_queuing_status(eth_dev);
 
   	RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid);
@@ -651,23 +652,24 @@ destroy_device(int vid)
   	eth_dev = list->eth_dev;
   	internal = eth_dev->data->dev_private;
 
-	rte_atomic32_set(&internal->started, 0);
-	update_queuing_status(eth_dev);
   	rte_atomic32_set(&internal->dev_attached, 0);
+	update_queuing_status(eth_dev);
 
   	eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
 
-	for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
-		vq = eth_dev->data->rx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
-	}
-	for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
-		vq = eth_dev->data->tx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
+	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+		for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
+			vq = eth_dev->data->rx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
+		for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
+			vq = eth_dev->data->tx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
   	}
 
   	state = vring_states[eth_dev->data->port_id];
@@ -792,11 +794,7 @@ eth_dev_start(struct rte_eth_dev *eth_dev)
   {
   	struct pmd_internal *internal = eth_dev->data->dev_private;
 
-	if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
-		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	}
-
+	queue_setup(eth_dev, internal);
   	rte_atomic32_set(&internal->started, 1);
   	update_queuing_status(eth_dev);
 
@@ -836,10 +834,13 @@ eth_dev_close(struct rte_eth_dev *dev)
   	pthread_mutex_unlock(&internal_list_lock);
   	rte_free(list);
 
-	for (i = 0; i < dev->data->nb_rx_queues; i++)
-		rte_free(dev->data->rx_queues[i]);
-	for (i = 0; i < dev->data->nb_tx_queues; i++)
-		rte_free(dev->data->tx_queues[i]);
+	if (dev->data->rx_queues)
+		for (i = 0; i < dev->data->nb_rx_queues; i++)
+			rte_free(dev->data->rx_queues[i]);
+
+	if (dev->data->tx_queues)
+		for (i = 0; i < dev->data->nb_tx_queues; i++)
+			rte_free(dev->data->tx_queues[i]);
 
   	rte_free(dev->data->mac_addrs);
   	free(internal->dev_name);
Tested-by on v2 1/1
#9 lore raw
In reply to Junjie Chen
Quoted 6 lines · Junjie Chen

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by *dev_attached* but by queues pointers or other separated variable(s).

Quoted 8 lines
Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

---
Changes in v2:
- use started to determine vhost queues readiness
- revert setting started to zero in destroy_device

This one works for me. Thanks!

regards,
Jens

#8 lore raw
In reply to Tan, Jianfeng

Hi Jianfeng.

Quoted 16 lines · Junjie J
Quoted 15 lines
Quoted 15 lines · Junjie Chen
Quoted 7 lines

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set
*started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by
*dev_attached* but by queues pointers or other separated variable(s).

I see two issues from your description:

- We shall fix the wrong use of dev_attached by commit 30a701a53737.
- The allow_queuing is not set correctly. But I doubt if it's a real issue, as we do update the queue status in dev_start(), dev_stop(), and new_device(). Can you explain more?

Quoted 5 lines
Quoted 2 lines

The started is set to 0 in destroy_device in 30a701a53737 commit, so allow_queuing is always set to 0 once update_queuing_status get called. We

have to use "port start" to reset to 1.

OK, that means it is also due to the wrong use of dev_attached?

Yes, a mixed wrong. That is why I tried to use new variable to track queue. One variable for one state.

Quoted 28 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

---
Changes in v2:
- use started to determine vhost queues readiness
- revert setting started to zero in destroy_device

   drivers/net/vhost/rte_eth_vhost.c | 61

++++++++++++++++++++-------------------

Quoted 8 lines
Quoted 8 lines

Review

Quoted 3 lines
   1 file changed, 31 insertions(+), 30 deletions(-)
(quoted diff) +0 -0

b/drivers/net/vhost/rte_eth_vhost.c

drivers/net/vhost/rte_eth_vhost.c update_queuing_status(struct rte_eth_dev

Quoted 4 lines

index 11b6076..ff462b3 100644

drivers/net/vhost/rte_eth_vhost.c +0 -0
@@ -528,10 +528,13 @@ update_queuing_status(struct rte_eth_dev

*dev)

Quoted 8 lines
Quoted 8 lines

drivers/net/vhost/rte_eth_vhost.c update_queuing_status(struct rte_eth_dev *dev) · line 532

Quoted 6 lines
drivers/net/vhost/rte_eth_vhost.c +2 -1
   	unsigned int i;
   	int allow_queuing = 1;
 
-	if (rte_atomic32_read(&internal->dev_attached) == 0)
+	if (!dev->data->rx_queues || !dev->data->tx_queues) {
+		RTE_LOG(ERR, PMD, "RX/TX queues not exist yet\n");

Even it's not introduced in this patch, but I think we shall not report error log here.

Quoted 5 lines
Quoted 2 lines

if you attach one port with "port attach" (no vhost queue setup yet), and then execute "port stop", the queue status is not exist yet, right?

My point is that we allow a dev is not started but attached, so it's not an error, and we shall not print error log.

OK, will remove it in V3.

Quoted 105 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
drivers/net/vhost/rte_eth_vhost.c +7 -8
   		return;
+	}
 
-	if (rte_atomic32_read(&internal->started) == 0)
+	if (rte_atomic32_read(&internal->started) == 0 ||
+	    rte_atomic32_read(&internal->dev_attached) == 0)
   		allow_queuing = 0;
 
   	/* Wait until rx/tx_pkt_burst stops accessing vhost device */ @@
-607,13 +610,10 @@ new_device(int vid)
   #endif
 
   	internal->vid = vid;
-	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+	if (rte_atomic32_read(&internal->started) == 1)
   		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	} else {
-		RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
-		rte_atomic32_set(&internal->dev_attached, 0);
-	}
+	else
+		RTE_LOG(INFO, PMD, "RX/TX queues not exist yet\n");
 
   	for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
   		rte_vhost_enable_guest_notification(vid, i, 0); @@ -622,6
+622,7 @@ new_device(int vid)
Quoted 6 lines
Quoted 6 lines

drivers/net/vhost/rte_eth_vhost.c new_device(int vid)

Quoted 6 lines
   	eth_dev->data->dev_link.link_status = ETH_LINK_UP;
drivers/net/vhost/rte_eth_vhost.c +1 -0
+	rte_atomic32_set(&internal->dev_attached, 1);
   	update_queuing_status(eth_dev);
 
   	RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid); @@

-651,23

Quoted 69 lines
Quoted 69 lines
Quoted 69 lines

+652,24 @@ destroy_device(int vid)

   	eth_dev = list->eth_dev;
   	internal = eth_dev->data->dev_private;
drivers/net/vhost/rte_eth_vhost.c +22 -22
-	rte_atomic32_set(&internal->started, 0);
-	update_queuing_status(eth_dev);
   	rte_atomic32_set(&internal->dev_attached, 0);
+	update_queuing_status(eth_dev);
 
   	eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
 
-	for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
-		vq = eth_dev->data->rx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
-	}
-	for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
-		vq = eth_dev->data->tx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
+	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+		for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
+			vq = eth_dev->data->rx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
+		for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
+			vq = eth_dev->data->tx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
   	}
 
   	state = vring_states[eth_dev->data->port_id];
@@ -792,11 +794,7 @@ eth_dev_start(struct rte_eth_dev *eth_dev)
   {
   	struct pmd_internal *internal = eth_dev->data->dev_private;
 
-	if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
-		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	}
-
+	queue_setup(eth_dev, internal);
   	rte_atomic32_set(&internal->started, 1);
   	update_queuing_status(eth_dev);
 
@@ -836,10 +834,13 @@ eth_dev_close(struct rte_eth_dev *dev)
   	pthread_mutex_unlock(&internal_list_lock);
   	rte_free(list);
 
-	for (i = 0; i < dev->data->nb_rx_queues; i++)
-		rte_free(dev->data->rx_queues[i]);
-	for (i = 0; i < dev->data->nb_tx_queues; i++)
-		rte_free(dev->data->tx_queues[i]);
+	if (dev->data->rx_queues)
+		for (i = 0; i < dev->data->nb_rx_queues; i++)
+			rte_free(dev->data->rx_queues[i]);
+
+	if (dev->data->tx_queues)
+		for (i = 0; i < dev->data->nb_tx_queues; i++)
+			rte_free(dev->data->tx_queues[i]);
 
   	rte_free(dev->data->mac_addrs);
   	free(internal->dev_name);
#10 lore raw

PATCH v3 1/1 net/vhost: fix vhost invalid state

In reply to Junjie Chen

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by *dev_attached* but by queues pointers or other separated variable(s).

Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

---
Changes in v3:
- remove useless log in queue status showing Changes in v2:
- use started to determine vhost queues readiness
- revert setting started to zero in destroy_device

 drivers/net/vhost/rte_eth_vhost.c | 59 +++++++++++++++++++--------------------
 1 file changed, 29 insertions(+), 30 deletions(-)
drivers/net/vhost/rte_eth_vhost.c +29 -30
index 11b6076..e392d71 100644
@@ -528,10 +528,11 @@ update_queuing_status(struct rte_eth_dev *dev)
 	unsigned int i;
 	int allow_queuing = 1;
 
-	if (rte_atomic32_read(&internal->dev_attached) == 0)
+	if (!dev->data->rx_queues || !dev->data->tx_queues)
 		return;
 
-	if (rte_atomic32_read(&internal->started) == 0)
+	if (rte_atomic32_read(&internal->started) == 0 ||
+	    rte_atomic32_read(&internal->dev_attached) == 0)
 		allow_queuing = 0;
 
 	/* Wait until rx/tx_pkt_burst stops accessing vhost device */
@@ -607,13 +608,10 @@ new_device(int vid)
 #endif
 
 	internal->vid = vid;
-	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+	if (rte_atomic32_read(&internal->started) == 1)
 		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	} else {
-		RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
-		rte_atomic32_set(&internal->dev_attached, 0);
-	}
+	else
+		RTE_LOG(INFO, PMD, "RX/TX queues not exist yet\n");
 
 	for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
 		rte_vhost_enable_guest_notification(vid, i, 0);
@@ -622,6 +620,7 @@ new_device(int vid)
 
 	eth_dev->data->dev_link.link_status = ETH_LINK_UP;
 
+	rte_atomic32_set(&internal->dev_attached, 1);
 	update_queuing_status(eth_dev);
 
 	RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid);
@@ -651,23 +650,24 @@ destroy_device(int vid)
 	eth_dev = list->eth_dev;
 	internal = eth_dev->data->dev_private;
 
-	rte_atomic32_set(&internal->started, 0);
-	update_queuing_status(eth_dev);
 	rte_atomic32_set(&internal->dev_attached, 0);
+	update_queuing_status(eth_dev);
 
 	eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
 
-	for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
-		vq = eth_dev->data->rx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
-	}
-	for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
-		vq = eth_dev->data->tx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
+	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+		for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
+			vq = eth_dev->data->rx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
+		for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
+			vq = eth_dev->data->tx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
 	}
 
 	state = vring_states[eth_dev->data->port_id];
@@ -792,11 +792,7 @@ eth_dev_start(struct rte_eth_dev *eth_dev)
 {
 	struct pmd_internal *internal = eth_dev->data->dev_private;
 
-	if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
-		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	}
-
+	queue_setup(eth_dev, internal);
 	rte_atomic32_set(&internal->started, 1);
 	update_queuing_status(eth_dev);
 
@@ -836,10 +832,13 @@ eth_dev_close(struct rte_eth_dev *dev)
 	pthread_mutex_unlock(&internal_list_lock);
 	rte_free(list);
 
-	for (i = 0; i < dev->data->nb_rx_queues; i++)
-		rte_free(dev->data->rx_queues[i]);
-	for (i = 0; i < dev->data->nb_tx_queues; i++)
-		rte_free(dev->data->tx_queues[i]);
+	if (dev->data->rx_queues)
+		for (i = 0; i < dev->data->nb_rx_queues; i++)
+			rte_free(dev->data->rx_queues[i]);
+
+	if (dev->data->tx_queues)
+		for (i = 0; i < dev->data->nb_tx_queues; i++)
+			rte_free(dev->data->tx_queues[i]);
 
 	rte_free(dev->data->mac_addrs);
 	free(internal->dev_name);
Signature · 1 line
2.0.1
Reviewed-by on v3 1/1
#11 lore raw
Quoted 11 lines · Junjie Chen

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by *dev_attached* but by queues pointers or other separated variable(s).

Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

Overall, looks great to me except a nit below.

Quoted 104 lines

---
Changes in v3:
- remove useless log in queue status showing Changes in v2:
- use started to determine vhost queues readiness
- revert setting started to zero in destroy_device

  drivers/net/vhost/rte_eth_vhost.c | 59 +++++++++++++++++++--------------------
  1 file changed, 29 insertions(+), 30 deletions(-)
drivers/net/vhost/rte_eth_vhost.c +22 -26
index 11b6076..e392d71 100644
@@ -528,10 +528,11 @@ update_queuing_status(struct rte_eth_dev *dev)
  	unsigned int i;
  	int allow_queuing = 1;
 
-	if (rte_atomic32_read(&internal->dev_attached) == 0)
+	if (!dev->data->rx_queues || !dev->data->tx_queues)
  		return;
 
-	if (rte_atomic32_read(&internal->started) == 0)
+	if (rte_atomic32_read(&internal->started) == 0 ||
+	    rte_atomic32_read(&internal->dev_attached) == 0)
  		allow_queuing = 0;
 
  	/* Wait until rx/tx_pkt_burst stops accessing vhost device */
@@ -607,13 +608,10 @@ new_device(int vid)
  #endif
 
  	internal->vid = vid;
-	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+	if (rte_atomic32_read(&internal->started) == 1)
  		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	} else {
-		RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
-		rte_atomic32_set(&internal->dev_attached, 0);
-	}
+	else
+		RTE_LOG(INFO, PMD, "RX/TX queues not exist yet\n");
 
  	for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
  		rte_vhost_enable_guest_notification(vid, i, 0);
@@ -622,6 +620,7 @@ new_device(int vid)
 
  	eth_dev->data->dev_link.link_status = ETH_LINK_UP;
 
+	rte_atomic32_set(&internal->dev_attached, 1);
  	update_queuing_status(eth_dev);
 
  	RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid);
@@ -651,23 +650,24 @@ destroy_device(int vid)
  	eth_dev = list->eth_dev;
  	internal = eth_dev->data->dev_private;
 
-	rte_atomic32_set(&internal->started, 0);
-	update_queuing_status(eth_dev);
  	rte_atomic32_set(&internal->dev_attached, 0);
+	update_queuing_status(eth_dev);
 
  	eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
 
-	for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
-		vq = eth_dev->data->rx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
-	}
-	for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
-		vq = eth_dev->data->tx_queues[i];
-		if (vq == NULL)
-			continue;
-		vq->vid = -1;
+	if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+		for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
+			vq = eth_dev->data->rx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
+		for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
+			vq = eth_dev->data->tx_queues[i];
+			if (!vq)
+				continue;
+			vq->vid = -1;
+		}
  	}
 
  	state = vring_states[eth_dev->data->port_id];
@@ -792,11 +792,7 @@ eth_dev_start(struct rte_eth_dev *eth_dev)
  {
  	struct pmd_internal *internal = eth_dev->data->dev_private;
 
-	if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
-		queue_setup(eth_dev, internal);
-		rte_atomic32_set(&internal->dev_attached, 1);
-	}
-
+	queue_setup(eth_dev, internal);
  	rte_atomic32_set(&internal->started, 1);
  	update_queuing_status(eth_dev);
 

drivers/net/vhost/rte_eth_vhost.c eth_dev_close(struct rte_eth_dev *dev) · line 835

Quoted 8 lines
drivers/net/vhost/rte_eth_vhost.c +1 -4
@@ -836,10 +832,13 @@ eth_dev_close(struct rte_eth_dev *dev)
  	pthread_mutex_unlock(&internal_list_lock);
  	rte_free(list);
 
-	for (i = 0; i < dev->data->nb_rx_queues; i++)
-		rte_free(dev->data->rx_queues[i]);
-	for (i = 0; i < dev->data->nb_tx_queues; i++)
-		rte_free(dev->data->tx_queues[i]);
+	if (dev->data->rx_queues)

This is implied that rx_queues is already allocated. So I don't think we need this.

Quoted 9 lines
drivers/net/vhost/rte_eth_vhost.c +6 -0
+		for (i = 0; i < dev->data->nb_rx_queues; i++)
+			rte_free(dev->data->rx_queues[i]);
+
+	if (dev->data->tx_queues)
+		for (i = 0; i < dev->data->nb_tx_queues; i++)
+			rte_free(dev->data->tx_queues[i]);
 
  	rte_free(dev->data->mac_addrs);
  	free(internal->dev_name);
#12 lore raw
Quoted 12 lines · Jianfeng
Quoted 9 lines · Junjie Chen

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by *dev_attached* but by queues pointers or other separated variable(s).

Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

Quoted 6 lines
Quoted 2 lines

Overall, looks great to me except a nit below.

Thanks Jianfeng, I can handle the small change while applying.

Can you confirm that it is implied that the queue are already allocated, else we wouldn't find the internal resource and quit earlier (in case of eth_dev_close called twice for example)?

Thanks,
Maxime

Quoted 111 lines
Quoted 92 lines

---
Changes in v3:
- remove useless log in queue status showing Changes in v2:
- use started to determine vhost queues readiness
- revert setting started to zero in destroy_device drivers/net/vhost/rte_eth_vhost.c | 59
+++++++++++++++++++--------------------

  1 file changed, 29 insertions(+), 30 deletions(-)
drivers/net/vhost/rte_eth_vhost.c +22 -26
index 11b6076..e392d71 100644
@@ -528,10 +528,11 @@ update_queuing_status(struct rte_eth_dev *dev)
       unsigned int i;
       int allow_queuing = 1;
-    if (rte_atomic32_read(&internal->dev_attached) == 0)
+    if (!dev->data->rx_queues || !dev->data->tx_queues)
           return;
-    if (rte_atomic32_read(&internal->started) == 0)
+    if (rte_atomic32_read(&internal->started) == 0 ||
+        rte_atomic32_read(&internal->dev_attached) == 0)
           allow_queuing = 0;
       /* Wait until rx/tx_pkt_burst stops accessing vhost device */
@@ -607,13 +608,10 @@ new_device(int vid)
   #endif
       internal->vid = vid;
-    if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+    if (rte_atomic32_read(&internal->started) == 1)
           queue_setup(eth_dev, internal);
-        rte_atomic32_set(&internal->dev_attached, 1);
-    } else {
-        RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
-        rte_atomic32_set(&internal->dev_attached, 0);
-    }
+    else
+        RTE_LOG(INFO, PMD, "RX/TX queues not exist yet\n");
       for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
           rte_vhost_enable_guest_notification(vid, i, 0);
@@ -622,6 +620,7 @@ new_device(int vid)
       eth_dev->data->dev_link.link_status = ETH_LINK_UP;
+    rte_atomic32_set(&internal->dev_attached, 1);
       update_queuing_status(eth_dev);
       RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid);
@@ -651,23 +650,24 @@ destroy_device(int vid)
       eth_dev = list->eth_dev;
       internal = eth_dev->data->dev_private;
-    rte_atomic32_set(&internal->started, 0);
-    update_queuing_status(eth_dev);
       rte_atomic32_set(&internal->dev_attached, 0);
+    update_queuing_status(eth_dev);
       eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
-    for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
-        vq = eth_dev->data->rx_queues[i];
-        if (vq == NULL)
-            continue;
-        vq->vid = -1;
-    }
-    for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
-        vq = eth_dev->data->tx_queues[i];
-        if (vq == NULL)
-            continue;
-        vq->vid = -1;
+    if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+        for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
+            vq = eth_dev->data->rx_queues[i];
+            if (!vq)
+                continue;
+            vq->vid = -1;
+        }
+        for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
+            vq = eth_dev->data->tx_queues[i];
+            if (!vq)
+                continue;
+            vq->vid = -1;
+        }
       }
       state = vring_states[eth_dev->data->port_id];
@@ -792,11 +792,7 @@ eth_dev_start(struct rte_eth_dev *eth_dev)
   {
       struct pmd_internal *internal = eth_dev->data->dev_private;
-    if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
-        queue_setup(eth_dev, internal);
-        rte_atomic32_set(&internal->dev_attached, 1);
-    }
-
+    queue_setup(eth_dev, internal);
       rte_atomic32_set(&internal->started, 1);
       update_queuing_status(eth_dev);

drivers/net/vhost/rte_eth_vhost.c eth_dev_close(struct rte_eth_dev *dev) · line 834

Quoted 7 lines
drivers/net/vhost/rte_eth_vhost.c +1 -4
@@ -836,10 +832,13 @@ eth_dev_close(struct rte_eth_dev *dev)
       pthread_mutex_unlock(&internal_list_lock);
       rte_free(list);
-    for (i = 0; i < dev->data->nb_rx_queues; i++)
-        rte_free(dev->data->rx_queues[i]);
-    for (i = 0; i < dev->data->nb_tx_queues; i++)
-        rte_free(dev->data->tx_queues[i]);
+    if (dev->data->rx_queues)

This is implied that rx_queues is already allocated. So I don't think we need this.

Quoted 8 lines
drivers/net/vhost/rte_eth_vhost.c +6 -0
+        for (i = 0; i < dev->data->nb_rx_queues; i++)
+            rte_free(dev->data->rx_queues[i]);
+
+    if (dev->data->tx_queues)
+        for (i = 0; i < dev->data->nb_tx_queues; i++)
+            rte_free(dev->data->tx_queues[i]);
       rte_free(dev->data->mac_addrs);
       free(internal->dev_name);
#13 lore raw
Quoted 22 lines · Jianfeng
Quoted 18 lines · Junjie Chen
Quoted 7 lines

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by
*dev_attached* but by queues pointers or other separated variable(s).

Quoted 5 lines
Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

Overall, looks great to me except a nit below.

Quoted 7 lines
Quoted 1 line

Thanks Jianfeng, I can handle the small change while applying.

Can you confirm that it is implied that the queue are already allocated, else we wouldn't find the internal resource and quit earlier (in case of eth_dev_close called twice for example)?

That is required, otherwise it generate segfault if we close device before queue setup. For example we execute following steps in testpmd:
1. port attach
2. ctrl+D

Quoted 117 lines

Thanks,
Maxime

Quoted 10 lines
Quoted 10 lines

---
Changes in v3:
- remove useless log in queue status showing Changes in v2:
- use started to determine vhost queues readiness
- revert setting started to zero in destroy_device drivers/net/vhost/rte_eth_vhost.c | 59
+++++++++++++++++++--------------------

  1 file changed, 29 insertions(+), 30 deletions(-)
Quoted 6 lines

drivers/net/vhost/rte_eth_vhost.c update_queuing_status(struct rte_eth_dev

Quoted 6 lines
drivers/net/vhost/rte_eth_vhost.c +0 -0
index 11b6076..e392d71 100644
@@ -528,10 +528,11 @@ update_queuing_status(struct rte_eth_dev

*dev)

Quoted 96 lines
Quoted 75 lines
drivers/net/vhost/rte_eth_vhost.c +6 -8
       unsigned int i;
       int allow_queuing = 1;
-    if (rte_atomic32_read(&internal->dev_attached) == 0)
+    if (!dev->data->rx_queues || !dev->data->tx_queues)
           return;
-    if (rte_atomic32_read(&internal->started) == 0)
+    if (rte_atomic32_read(&internal->started) == 0 ||
+        rte_atomic32_read(&internal->dev_attached) == 0)
           allow_queuing = 0;
       /* Wait until rx/tx_pkt_burst stops accessing vhost device */
@@ -607,13 +608,10 @@ new_device(int vid)
   #endif
       internal->vid = vid;
-    if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+    if (rte_atomic32_read(&internal->started) == 1)
           queue_setup(eth_dev, internal);
-        rte_atomic32_set(&internal->dev_attached, 1);
-    } else {
-        RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
-        rte_atomic32_set(&internal->dev_attached, 0);
-    }
+    else
+        RTE_LOG(INFO, PMD, "RX/TX queues not exist yet\n");
       for (i = 0; i < rte_vhost_get_vring_num(vid); i++)

          rte_vhost_enable_guest_notification(vid, i, 0); @@ -622,6 +620,7 @@ new_device(int vid) eth_dev->data->dev_link.link_status = ETH_LINK_UP; +    rte_atomic32_set(&internal->dev_attached, 1); update_queuing_status(eth_dev);
      RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid); @@ -651,23 +650,24 @@ destroy_device(int vid)
      eth_dev = list->eth_dev;
      internal = eth_dev->data->dev_private;
-    rte_atomic32_set(&internal->started, 0);
-    update_queuing_status(eth_dev); rte_atomic32_set(&internal->dev_attached, 0);
+    update_queuing_status(eth_dev); eth_dev->data->dev_link.link_status = ETH_LINK_DOWN; -    for (i = 0; i < eth_dev->data->nb_rx_queues; i++) { -        vq = eth_dev->data->rx_queues[i];
-        if (vq == NULL)
-            continue;
-        vq->vid = -1;
-    }
-    for (i = 0; i < eth_dev->data->nb_tx_queues; i++) { -        vq = eth_dev->data->tx_queues[i];
-        if (vq == NULL)
-            continue;
-        vq->vid = -1;
+    if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) { +        for (i = 0; i < eth_dev->data->nb_rx_queues; i++) { +            vq = eth_dev->data->rx_queues[i];
+            if (!vq)
+                continue;
+            vq->vid = -1;
+        }
+        for (i = 0; i < eth_dev->data->nb_tx_queues; i++) { +            vq = eth_dev->data->tx_queues[i];
+            if (!vq)
+                continue;
+            vq->vid = -1;
+        }
      }
      state = vring_states[eth_dev->data->port_id];

drivers/net/vhost/rte_eth_vhost.c +1 -5
@@ -792,11 +792,7 @@ eth_dev_start(struct rte_eth_dev *eth_dev)
   {
       struct pmd_internal *internal = eth_dev->data->dev_private;
-    if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
-        queue_setup(eth_dev, internal);
-        rte_atomic32_set(&internal->dev_attached, 1);
-    }
-
+    queue_setup(eth_dev, internal);
       rte_atomic32_set(&internal->started, 1);

      update_queuing_status(eth_dev); @@ -836,10 +832,13 @@

Quoted 8 lines

eth_dev_close(struct rte_eth_dev *dev) pthread_mutex_unlock(&internal_list_lock); rte_free(list);
-    for (i = 0; i < dev->data->nb_rx_queues; i++) -        rte_free(dev->data->rx_queues[i]); -    for (i = 0; i < dev->data->nb_tx_queues; i++) -        rte_free(dev->data->tx_queues[i]); +    if (dev->data->rx_queues)

This is implied that rx_queues is already allocated. So I don't think we need this.

Quoted 8 lines

+        for (i = 0; i < dev->data->nb_rx_queues; i++) +            rte_free(dev->data->rx_queues[i]); +
+    if (dev->data->tx_queues)
+        for (i = 0; i < dev->data->nb_tx_queues; i++) +            rte_free(dev->data->tx_queues[i]); rte_free(dev->data->mac_addrs); free(internal->dev_name);

Reviewed-by
#14 lore raw
Quoted 26 lines · Junjie J
Quoted 26 lines · Jianfeng
Quoted 13 lines · Junjie Chen
Quoted 10 lines

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by
*dev_attached* but by queues pointers or other separated variable(s).

Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

Quoted 6 lines
Quoted 2 lines

Overall, looks great to me except a nit below.

Thanks Jianfeng, I can handle the small change while applying.

Quoted 8 lines
Quoted 3 lines

Can you confirm that it is implied that the queue are already allocated, else we wouldn't find the internal resource and quit earlier (in case of eth_dev_close called twice for example)?

That is required, otherwise it generate segfault if we close device before queue setup. For example we execute following steps in testpmd:
1. port attach
2. ctrl+D

Thanks for confirming Junjie, I will apply it as is then.

Thanks,
Maxime

Quoted 117 lines
Quoted 117 lines

Thanks,
Maxime

Quoted 10 lines
Quoted 10 lines

---
Changes in v3:
- remove useless log in queue status showing Changes in v2:
- use started to determine vhost queues readiness
- revert setting started to zero in destroy_device

   drivers/net/vhost/rte_eth_vhost.c | 59
+++++++++++++++++++--------------------
   1 file changed, 29 insertions(+), 30 deletions(-)
Quoted 6 lines

drivers/net/vhost/rte_eth_vhost.c update_queuing_status(struct rte_eth_dev

Quoted 6 lines
drivers/net/vhost/rte_eth_vhost.c +0 -0
index 11b6076..e392d71 100644
@@ -528,10 +528,11 @@ update_queuing_status(struct rte_eth_dev

*dev)

Quoted 96 lines
Quoted 77 lines
drivers/net/vhost/rte_eth_vhost.c +23 -27
       unsigned int i;
       int allow_queuing = 1;
-    if (rte_atomic32_read(&internal->dev_attached) == 0)
+    if (!dev->data->rx_queues || !dev->data->tx_queues)
           return;
-    if (rte_atomic32_read(&internal->started) == 0)
+    if (rte_atomic32_read(&internal->started) == 0 ||
+        rte_atomic32_read(&internal->dev_attached) == 0)
           allow_queuing = 0;
       /* Wait until rx/tx_pkt_burst stops accessing vhost device */
@@ -607,13 +608,10 @@ new_device(int vid)
   #endif
       internal->vid = vid;
-    if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+    if (rte_atomic32_read(&internal->started) == 1)
           queue_setup(eth_dev, internal);
-        rte_atomic32_set(&internal->dev_attached, 1);
-    } else {
-        RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
-        rte_atomic32_set(&internal->dev_attached, 0);
-    }
+    else
+        RTE_LOG(INFO, PMD, "RX/TX queues not exist yet\n");
       for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
           rte_vhost_enable_guest_notification(vid, i, 0); @@ -622,6
+620,7 @@ new_device(int vid)
       eth_dev->data->dev_link.link_status = ETH_LINK_UP;
+    rte_atomic32_set(&internal->dev_attached, 1);
       update_queuing_status(eth_dev);
       RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid); @@
-651,23 +650,24 @@ destroy_device(int vid)
       eth_dev = list->eth_dev;
       internal = eth_dev->data->dev_private;
-    rte_atomic32_set(&internal->started, 0);
-    update_queuing_status(eth_dev);
       rte_atomic32_set(&internal->dev_attached, 0);
+    update_queuing_status(eth_dev);
       eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
-    for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
-        vq = eth_dev->data->rx_queues[i];
-        if (vq == NULL)
-            continue;
-        vq->vid = -1;
-    }
-    for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
-        vq = eth_dev->data->tx_queues[i];
-        if (vq == NULL)
-            continue;
-        vq->vid = -1;
+    if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+        for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
+            vq = eth_dev->data->rx_queues[i];
+            if (!vq)
+                continue;
+            vq->vid = -1;
+        }
+        for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
+            vq = eth_dev->data->tx_queues[i];
+            if (!vq)
+                continue;
+            vq->vid = -1;
+        }
       }
       state = vring_states[eth_dev->data->port_id];
@@ -792,11 +792,7 @@ eth_dev_start(struct rte_eth_dev *eth_dev)
   {
       struct pmd_internal *internal = eth_dev->data->dev_private;
-    if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
-        queue_setup(eth_dev, internal);
-        rte_atomic32_set(&internal->dev_attached, 1);
-    }
-
+    queue_setup(eth_dev, internal);
       rte_atomic32_set(&internal->started, 1);
       update_queuing_status(eth_dev); @@ -836,10 +832,13 @@
 eth_dev_close(struct rte_eth_dev *dev)
       pthread_mutex_unlock(&internal_list_lock);
Quoted 6 lines

      rte_free(list);
-    for (i = 0; i < dev->data->nb_rx_queues; i++) -        rte_free(dev->data->rx_queues[i]); -    for (i = 0; i < dev->data->nb_tx_queues; i++) -        rte_free(dev->data->tx_queues[i]); +    if (dev->data->rx_queues)

This is implied that rx_queues is already allocated. So I don't think we need this.

Quoted 8 lines

+        for (i = 0; i < dev->data->nb_rx_queues; i++) +            rte_free(dev->data->rx_queues[i]); +
+    if (dev->data->tx_queues)
+        for (i = 0; i < dev->data->nb_tx_queues; i++) +            rte_free(dev->data->tx_queues[i]);
      rte_free(dev->data->mac_addrs); free(internal->dev_name);

#16 lore raw
In reply to Maxime Coquelin
Quoted 20 lines · Maxime Coquelin
Quoted 17 lines · Jianfeng
Quoted 11 lines · Junjie Chen

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by *dev_attached* but by queues pointers or other separated variable(s).

Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

Overall, looks great to me except a nit below.

Quoted 7 lines
Quoted 1 line

Thanks Jianfeng, I can handle the small change while applying.

Can you confirm that it is implied that the queue are already allocated, else we wouldn't find the internal resource and quit earlier (in case of eth_dev_close called twice for example)?

I was referring to i40e_dev_free_queues() and ixgbe_dev_free_queues(). But a 2nd thought, no harm to keep the check.

Quoted 115 lines

Thanks,
Maxime

Quoted 111 lines
Quoted 92 lines

---
Changes in v3:
- remove useless log in queue status showing Changes in v2:
- use started to determine vhost queues readiness
- revert setting started to zero in destroy_device

  drivers/net/vhost/rte_eth_vhost.c | 59 
+++++++++++++++++++--------------------
  1 file changed, 29 insertions(+), 30 deletions(-)
drivers/net/vhost/rte_eth_vhost.c +22 -26
index 11b6076..e392d71 100644
@@ -528,10 +528,11 @@ update_queuing_status(struct rte_eth_dev *dev)
      unsigned int i;
      int allow_queuing = 1;
-    if (rte_atomic32_read(&internal->dev_attached) == 0)
+    if (!dev->data->rx_queues || !dev->data->tx_queues)
          return;
-    if (rte_atomic32_read(&internal->started) == 0)
+    if (rte_atomic32_read(&internal->started) == 0 ||
+        rte_atomic32_read(&internal->dev_attached) == 0)
          allow_queuing = 0;
      /* Wait until rx/tx_pkt_burst stops accessing vhost device */
@@ -607,13 +608,10 @@ new_device(int vid)
  #endif
      internal->vid = vid;
-    if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+    if (rte_atomic32_read(&internal->started) == 1)
          queue_setup(eth_dev, internal);
-        rte_atomic32_set(&internal->dev_attached, 1);
-    } else {
-        RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
-        rte_atomic32_set(&internal->dev_attached, 0);
-    }
+    else
+        RTE_LOG(INFO, PMD, "RX/TX queues not exist yet\n");
      for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
          rte_vhost_enable_guest_notification(vid, i, 0);
@@ -622,6 +620,7 @@ new_device(int vid)
      eth_dev->data->dev_link.link_status = ETH_LINK_UP;
+    rte_atomic32_set(&internal->dev_attached, 1);
      update_queuing_status(eth_dev);
      RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid);
@@ -651,23 +650,24 @@ destroy_device(int vid)
      eth_dev = list->eth_dev;
      internal = eth_dev->data->dev_private;
-    rte_atomic32_set(&internal->started, 0);
-    update_queuing_status(eth_dev);
      rte_atomic32_set(&internal->dev_attached, 0);
+    update_queuing_status(eth_dev);
      eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
-    for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
-        vq = eth_dev->data->rx_queues[i];
-        if (vq == NULL)
-            continue;
-        vq->vid = -1;
-    }
-    for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
-        vq = eth_dev->data->tx_queues[i];
-        if (vq == NULL)
-            continue;
-        vq->vid = -1;
+    if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
+        for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
+            vq = eth_dev->data->rx_queues[i];
+            if (!vq)
+                continue;
+            vq->vid = -1;
+        }
+        for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
+            vq = eth_dev->data->tx_queues[i];
+            if (!vq)
+                continue;
+            vq->vid = -1;
+        }
      }
      state = vring_states[eth_dev->data->port_id];
@@ -792,11 +792,7 @@ eth_dev_start(struct rte_eth_dev *eth_dev)
  {
      struct pmd_internal *internal = eth_dev->data->dev_private;
-    if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
-        queue_setup(eth_dev, internal);
-        rte_atomic32_set(&internal->dev_attached, 1);
-    }
-
+    queue_setup(eth_dev, internal);
      rte_atomic32_set(&internal->started, 1);
      update_queuing_status(eth_dev);

drivers/net/vhost/rte_eth_vhost.c eth_dev_close(struct rte_eth_dev *dev) · line 834

Quoted 7 lines
drivers/net/vhost/rte_eth_vhost.c +1 -4
@@ -836,10 +832,13 @@ eth_dev_close(struct rte_eth_dev *dev)
      pthread_mutex_unlock(&internal_list_lock);
      rte_free(list);
-    for (i = 0; i < dev->data->nb_rx_queues; i++)
-        rte_free(dev->data->rx_queues[i]);
-    for (i = 0; i < dev->data->nb_tx_queues; i++)
-        rte_free(dev->data->tx_queues[i]);
+    if (dev->data->rx_queues)

This is implied that rx_queues is already allocated. So I don't think we need this.

Quoted 8 lines
drivers/net/vhost/rte_eth_vhost.c +6 -0
+        for (i = 0; i < dev->data->nb_rx_queues; i++)
+            rte_free(dev->data->rx_queues[i]);
+
+    if (dev->data->tx_queues)
+        for (i = 0; i < dev->data->nb_tx_queues; i++)
+            rte_free(dev->data->tx_queues[i]);
      rte_free(dev->data->mac_addrs);
      free(internal->dev_name);
#15 lore raw
In reply to Maxime Coquelin
Quoted 33 lines · Maxime Coquelin
Quoted 30 lines · Junjie J
Quoted 29 lines · Jianfeng
Quoted 13 lines · Junjie Chen
Quoted 10 lines

dev_start sets *dev_attached* after setup queues, this sets device to invalid state since no frontend is attached. Also destroy_device set *started* to zero which makes *allow_queuing* always zero until dev_start get called again. Actually, we should not determine queues existence by
*dev_attached* but by queues pointers or other separated variable(s).

Fixes 30a701a53737 ("net/vhost: fix crash when creating vdev

dynamically")

Quoted 6 lines
Quoted 2 lines

Overall, looks great to me except a nit below.

Thanks Jianfeng, I can handle the small change while applying.

Can you confirm that it is implied that the queue are already allocated, else we wouldn't find the internal resource and quit earlier (in case of eth_dev_close called twice for example)?

Quoted 9 lines
Quoted 4 lines

That is required, otherwise it generate segfault if we close device before queue setup. For example we execute following steps in testpmd:
1. port attach
2. ctrl+D

Thanks for confirming Junjie, I will apply it as is then.

Applied to dpdk-next-virtio/master

Thanks,
Maxime