ethdev: clear struct on releasing port
PATCH 1/1 ethdev: clear struct on releasing port
The function rte_eth_dev_release_port() is called by drivers when closing or removing a device.
Its main usage should be via rte_eth_dev_close() by up-to-date drivers which are compliant with RTE_ETH_DEV_CLOSE_REMOVE flag.
When a port is released, the data (rte_eth_dev_data) are cleared, but the pointers in rte_eth_dev were not cleared.
It may cause issues with code paths trying to use dangling pointers (e.g. the .device pointer which may reference a removed rte_device). Everything is now cleared to 0 when releasing.
The state is explicitly set to RTE_ETH_DEV_UNUSED which is 0 anyway.
Using this patch may reveal bugs in some code paths:
- device pointer must be saved before closing a port if needed
- drivers must close ports on device remove for consistent cleanup
--- lib/librte_ethdev/rte_ethdev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/librte_ethdev/rte_ethdev.c +2 -2
@@ -553,8 +553,6 @@ rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)rte_spinlock_lock (&rte_eth_dev_shared_data->ownership_lock); - eth_dev->state =RTE_ETH_DEV_UNUSED ; -if (rte_eal_process_type () ==RTE_PROC_PRIMARY ) {rte_free (eth_dev->data->rx_queues);rte_free (eth_dev->data->tx_queues);
@@ -563,6 +561,8 @@ rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)rte_free (eth_dev->data->dev_private);memset (eth_dev->data,0 ,sizeof (struct rte_eth_dev_data )); } +memset (eth_dev,0 ,sizeof (struct rte_eth_dev )); + eth_dev->state =RTE_ETH_DEV_UNUSED ;rte_spinlock_unlock (&rte_eth_dev_shared_data->ownership_lock);
Signature · 1 line
2.25.0
Quoted 12 lines · Thomas Monjalon
The function
rte_eth_dev_release_port()is called by drivers when closing or removing a device.
Its main usage should be viarte_eth_dev_close()by up-to-date drivers which are compliant withRTE_ETH_DEV_CLOSE_REMOVEflag.When a port is released, the data (
rte_eth_dev_data) are cleared, but the pointers inrte_eth_devwere not cleared.
It may cause issues with code paths trying to use dangling pointers (e.g. the .device pointer which may reference a removedrte_device). Everything is now cleared to 0 when releasing.
The state is explicitly set toRTE_ETH_DEV_UNUSEDwhich is 0 anyway.
Quoted 2 lines
Using this patch may reveal bugs in some code paths:
- device pointer must be saved before closing a port if needed
Is this saving should be done in application code or will be done by ethdev?
Quoted 30 lines
- drivers must close ports on device remove for consistent cleanup
Signed-off-by Thomas Monjalon <[email protected]>--- lib/librte_ethdev/rte_ethdev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)lib/librte_ethdev/rte_ethdev.c +2
-2@@ -553,8 +553,6 @@ rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)rte_spinlock_lock (&rte_eth_dev_shared_data->ownership_lock); - eth_dev->state =RTE_ETH_DEV_UNUSED ; -if (rte_eal_process_type () ==RTE_PROC_PRIMARY ) {rte_free (eth_dev->data->rx_queues);rte_free (eth_dev->data->tx_queues);@@ -563,6 +561,8 @@ rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)rte_free (eth_dev->data->dev_private);memset (eth_dev->data,0 ,sizeof (struct rte_eth_dev_data )); } +memset (eth_dev,0 ,sizeof (struct rte_eth_dev )); + eth_dev->state =RTE_ETH_DEV_UNUSED ;rte_spinlock_unlock (&rte_eth_dev_shared_data->ownership_lock);
13/02/2020 16:16, Ferruh Yigit:
Quoted 13 lines · Thomas Monjalon
Quoted 12 lines
The function
rte_eth_dev_release_port()is called by drivers when closing or removing a device.
Its main usage should be viarte_eth_dev_close()by up-to-date drivers which are compliant withRTE_ETH_DEV_CLOSE_REMOVEflag.When a port is released, the data (
rte_eth_dev_data) are cleared, but the pointers inrte_eth_devwere not cleared.
It may cause issues with code paths trying to use dangling pointers (e.g. the .device pointer which may reference a removedrte_device). Everything is now cleared to 0 when releasing.
The state is explicitly set toRTE_ETH_DEV_UNUSEDwhich is 0 anyway.
Quoted 4 lines
Quoted 2 lines
Using this patch may reveal bugs in some code paths:
- device pointer must be saved before closing a port if neededIs this saving should be done in application code or will be done by ethdev?
by the application
From ethdev point of view, when a port is closed, there is nothing more to do next.
Quoted 14 lines · Thomas Monjalon
13/02/2020 16:16, Ferruh Yigit:
Quoted 13 lines · Thomas Monjalon
Quoted 12 lines
The function
rte_eth_dev_release_port()is called by drivers when closing or removing a device.
Its main usage should be viarte_eth_dev_close()by up-to-date drivers which are compliant withRTE_ETH_DEV_CLOSE_REMOVEflag.When a port is released, the data (
rte_eth_dev_data) are cleared, but the pointers inrte_eth_devwere not cleared.
It may cause issues with code paths trying to use dangling pointers (e.g. the .device pointer which may reference a removedrte_device). Everything is now cleared to 0 when releasing.
The state is explicitly set toRTE_ETH_DEV_UNUSEDwhich is 0 anyway.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Using this patch may reveal bugs in some code paths:
- device pointer must be saved before closing a port if neededIs this saving should be done in application code or will be done by ethdev?
by the application
From ethdev point of view, when a port is closed, there is nothing more to do next.
That sound reasonable from ethdev perspective, but it will push dealing with rte_device level details to the application.
Like this change will be forcing each application to store the rte_device pointer before close(), in case device can be detached later.
13/02/2020 17:05, Ferruh Yigit:
Quoted 24 lines · Thomas Monjalon
Quoted 14 lines
13/02/2020 16:16, Ferruh Yigit:
Quoted 13 lines · Thomas Monjalon
Quoted 12 lines
The function
rte_eth_dev_release_port()is called by drivers when closing or removing a device.
Its main usage should be viarte_eth_dev_close()by up-to-date drivers which are compliant withRTE_ETH_DEV_CLOSE_REMOVEflag.When a port is released, the data (
rte_eth_dev_data) are cleared, but the pointers inrte_eth_devwere not cleared.
It may cause issues with code paths trying to use dangling pointers (e.g. the .device pointer which may reference a removedrte_device). Everything is now cleared to 0 when releasing.
The state is explicitly set toRTE_ETH_DEV_UNUSEDwhich is 0 anyway.Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Using this patch may reveal bugs in some code paths:
- device pointer must be saved before closing a port if neededIs this saving should be done in application code or will be done by ethdev?
by the application
From ethdev point of view, when a port is closed, there is nothing more to do next.
Quoted 5 lines
That sound reasonable from ethdev perspective, but it will push dealing with
rte_devicelevel details to the application.Like this change will be forcing each application to store the
rte_devicepointer beforeclose(), in case device can be detached later.
Yes
I think PMDs could release rte_device when closing the last port of the device. I am not sure what is the benefit of explicit detach.
That's something we should discuss, agree and write in the API doc.