eal: fix floating device argument pointer
PATCH 1/1 eal: fix floating device argument pointer
After we insert a devargs into devargs_list, following bus->scan may destroy it due to another rte_devargs_insert. Its better not to use a devargs pointer after it has been inserted.
--- lib/librte_eal/common/eal_common_dev.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_common_dev.c +9 -3
@@ -185,6 +185,7 @@ local_dev_probe(const char *devargs, struct rte_device **new_dev) {struct rte_device *dev;struct rte_devargs *da; +struct rte_devargs da_cp;int ret; *new_dev =NULL ;
@@ -203,18 +204,23 @@ local_dev_probe(const char *devargs, struct rte_device **new_dev) goto err_devarg; } +/** +* its better not to use da after rte_devargs_insert, +* so make a copy here. +*/ + da_cp = *da; ret =rte_devargs_insert (da);if (ret) goto err_devarg; - ret = da->bus->scan (); + ret = da_cp.bus->scan ();if (ret) goto err_devarg; - dev = da->bus->find_device (NULL , cmp_dev_name, da->name); + dev = da_cp.bus->find_device (NULL , cmp_dev_name, da_cp.name);if (dev ==NULL ) {RTE_LOG (ERR ,EAL ,"Cannot find device (%s)\n" , - da->name); + da_cp.name); ret = -ENODEV ; goto err_devarg; }
Signature · 1 line
2.13.6
22/10/2018 07:49, Qi Zhang:
Quoted 3 lines
After we insert a devargs into
devargs_list, followingbus->scanmay destroy it due to anotherrte_devargs_insert. Its better not to use a devargs pointer after it has been inserted.
I think the problem is in:
rte_devargs_insert(struct rte_devargs *da)
{
int ret;
ret = rte_devargs_remove(da);
if (ret < 0)
return ret;
TAILQ_INSERT_TAIL(&devargs_list, da, next);
return 0;
}We insert a structure which is freed!
See http://git.dpdk.org/dpdk/commit/?id=55744d83d525
Gaetan, what can be the fix?
Hi,
Quoted 5 lines · Thomas Monjalon
22/10/2018 07:49, Qi Zhang:
Quoted 3 lines
After we insert a devargs into
devargs_list, followingbus->scanmay destroy it due to anotherrte_devargs_insert. Its better not to use a devargs pointer after it has been inserted.
A bus scan calls rte_devargs_insert? Mapping devargs to device is the responsibility of the bus scan, if it calls potentially destructive functions, it must rebuild the map.
Quoted 13 lines
I think the problem is in:
rte_devargs_insert(struct rte_devargs *da){ int ret; ret = rte_devargs_remove(da); if (ret < 0) return ret; TAILQ_INSERT_TAIL(&devargs_list, da, next); return 0; }
Quoted 1 line
We insert a structure which is freed!
Not usually, I hope!
Quoted 4 lines
See http://git.dpdk.org/dpdk/commit/?id=55744d83d525
Gaetan, what can be the fix?
1. rte_devargs_insert is misdefined.
It is designed as a function that can never fail. The function should return void instead.
2. rte_devargs_remove(da), will not remove da itself.
It will remove whichever rte_devargs matches da within the internal list. If da does not match any in the list, it does nothing. As da is a newly-callocated structure, it is actually safe to continue using it after having called rte_devargs_remove(), because it cannot possibly have been inserted in the meantime (so would not have been freed, even if another devargs matched it).
The actual issue is that the matching rte_devargs within the list would be referenced by a device after a successful scan, meaning that this reference is not safe if someone attemps to insert the same device after the bus->scan(). If my understanding is correct, the above fix is not necessary, but probing should be guarded against re-entrancy.
3. To fix this bug, one should check that the device one attempts to hotplug does not already exists as a probed rte_device. An existing rte_devargs is not sufficient, because a blacklisted device would have an rte_devargs without having a probed rte_device, and the current behavior is to supersede the current blacklist and forcibly insert the new device, as if it was newly whitelisted. This check can only happen at rte_dev level.
4. Your confusion about rte_devargs_remove is understandable, the API is muddy. The reason for these quirks is because I wanted a user to be able to remove any devargs, even without having a direct reference to it: you only had to define the bus and the device id (name), and it would find it and remove it. It might be preferrable to force the user to find the rte_device, and from it, use the actual rte_devargs reference to remove it, but then, it would be impossible to remove devargs for non-existing devices (spoiler: that's the blacklisted ones).
5. It bears repeating: blacklisted mode is horrible and should be removed. It is all-around abominable, forces unsightly designs to exist and be used, makes managers ask questions about "why do you add this quirky -w 00:00.0 parameter to your command line and what is your timeline for not needing it?", makes at least one team integrating OVS ask themselves "why not --no-pci? but then why can't I hotplug PCI ports?", and I would not be surprised if it killed puppies as a hobby.
So far, I was able to collect "but it simplifies testing bot configuration" as a plus, which I do not agree with.
And anyone trying to package DPDK on their platform, expecting users not to know or care about it, would be better off developping a proper autoconf tool, instead of baking it in the entrails of the EAL, which are ugly enough as it is. /rant
Regards,
Signature · 2 lines
Gaëtan Rivet 6WIND
24/10/2018 00:39, Gaëtan Rivet:
Quoted 39 lines
Hi,
Quoted 5 lines · Thomas Monjalon
22/10/2018 07:49, Qi Zhang:
Quoted 3 lines
After we insert a devargs into
devargs_list, followingbus->scanmay destroy it due to anotherrte_devargs_insert. Its better not to use a devargs pointer after it has been inserted.A bus scan calls
rte_devargs_insert? Mapping devargs to device is the responsibility of the bus scan, if it calls potentially destructive functions, it must rebuild the map.Quoted 13 lines
I think the problem is in:
rte_devargs_insert(struct rte_devargs *da){ int ret; ret = rte_devargs_remove(da); if (ret < 0) return ret; TAILQ_INSERT_TAIL(&devargs_list, da, next); return 0; }Quoted 1 line
We insert a structure which is freed!
Not usually, I hope!
Quoted 4 lines
See http://git.dpdk.org/dpdk/commit/?id=55744d83d525
Gaetan, what can be the fix?
1.
rte_devargs_insertis misdefined.
It is designed as a function that can never fail. The function should return void instead.
Quoted 7 lines
2.
rte_devargs_remove(da), will not remove da itself.
It will remove whicheverrte_devargsmatches da within the internal list. If da does not match any in the list, it does nothing. As da is a newly-callocated structure, it is actually safe to continue using it after having calledrte_devargs_remove(), because it cannot possibly have been inserted in the meantime (so would not have been freed, even if another devargs matched it).
If the devargs pointer passed in parameter is the same as the one in the list, it will be freed.
Quoted 6 lines
The actual issue is that the matching
rte_devargswithin the list would be referenced by a device after a successful scan, meaning that this reference is not safe if someone attemps to insert the same device after thebus->scan(). If my understanding is correct, the above fix is not necessary, but probing should be guarded against re-entrancy.
We may want to probe again with different parameters.
Quoted 26 lines
3. To fix this bug, one should check that the device one attempts to hotplug does not already exists as a probed
rte_device. An existingrte_devargsis not sufficient, because a blacklisted device would have anrte_devargswithout having a probedrte_device, and the current behavior is to supersede the current blacklist and forcibly insert the new device, as if it was newly whitelisted. This check can only happen atrte_devlevel.4. Your confusion about
rte_devargs_removeis understandable, the API is muddy. The reason for these quirks is because I wanted a user to be able to remove any devargs, even without having a direct reference to it: you only had to define the bus and the device id (name), and it would find it and remove it. It might be preferrable to force the user to find therte_device, and from it, use the actualrte_devargsreference to remove it, but then, it would be impossible to remove devargs for non-existing devices (spoiler: that's the blacklisted ones).5. It bears repeating: blacklisted mode is horrible and should be removed. It is all-around abominable, forces unsightly designs to exist and be used, makes managers ask questions about "why do you add this quirky
-w 00:00.0parameter to your command line and what is your timeline for not needing it?", makes at least one team integrating OVS ask themselves "why not --no-pci? but then why can't I hotplug PCI ports?", and I would not be surprised if it killed puppies as a hobby.
Quoted 7 lines
So far, I was able to collect "but it simplifies testing bot configuration" as a plus, which I do not agree with.
And anyone trying to package DPDK on their platform, expecting users not to know or care about it, would be better off developping a proper autoconf tool, instead of baking it in the entrails of the EAL, which are ugly enough as it is. /rant
Nice rant :)
Quoted 48 lines · Thomas Monjalon
24/10/2018 00:39, Gaëtan Rivet:
Quoted 39 lines
Hi,
Quoted 5 lines · Thomas Monjalon
22/10/2018 07:49, Qi Zhang:
Quoted 3 lines
After we insert a devargs into
devargs_list, followingbus->scanmay destroy it due to anotherrte_devargs_insert. Its better not to use a devargs pointer after it has been inserted.A bus scan calls
rte_devargs_insert? Mapping devargs to device is the responsibility of the bus scan, if it calls potentially destructive functions, it must rebuild the map.Quoted 13 lines
I think the problem is in:
rte_devargs_insert(struct rte_devargs *da){ int ret; ret = rte_devargs_remove(da); if (ret < 0) return ret; TAILQ_INSERT_TAIL(&devargs_list, da, next); return 0; }Quoted 1 line
We insert a structure which is freed!
Not usually, I hope!
Quoted 4 lines
See http://git.dpdk.org/dpdk/commit/?id=55744d83d525
Gaetan, what can be the fix?
1.
rte_devargs_insertis misdefined.
It is designed as a function that can never fail. The function should return void instead.Quoted 7 lines
2.
rte_devargs_remove(da), will not remove da itself.
It will remove whicheverrte_devargsmatches da within the internal list. If da does not match any in the list, it does nothing. As da is a newly-callocated structure, it is actually safe to continue using it after having calledrte_devargs_remove(), because it cannot possibly have been inserted in the meantime (so would not have been freed, even if another devargs matched it).
Quoted 3 lines
If the devargs pointer passed in parameter is the same as the one in the list, it will be freed.
This would only happen if one did:
rte_devargs_insert(dev->devargs);
Quoted 9 lines
Quoted 6 lines
The actual issue is that the matching
rte_devargswithin the list would be referenced by a device after a successful scan, meaning that this reference is not safe if someone attemps to insert the same device after thebus->scan(). If my understanding is correct, the above fix is not necessary, but probing should be guarded against re-entrancy.We may want to probe again with different parameters.
Sure, but in this case the fix is to check whether the device is already probed, and if so remove it before probing it again with the new devargs.
Quoted 2 lines
Nice rant :)
:)
Signature · 2 lines
Gaëtan Rivet 6WIND
Quoted 23 lines
-----Original Message-----
From: Gaëtan Rivet [mailto:[email protected]]
Sent: Wednesday, October 24, 2018 10:34 AM
To: Thomas Monjalon <[email protected]>
Cc: [email protected]; Zhang, Qi Z <[email protected]>
Subject: Re: [dpdk-dev] [PATCH] eal: fix floating device argument pointerQuoted 13 lines · Thomas Monjalon
24/10/2018 00:39, Gaëtan Rivet:
Quoted 12 lines
Hi,
Quoted 5 lines · Thomas Monjalon
22/10/2018 07:49, Qi Zhang:
Quoted 3 lines
After we insert a devargs into
devargs_list, followingbus->scanmay destroy it due to anotherrte_devargs_insert. Its better not to use a devargs pointer after it has been inserted.A bus scan calls
rte_devargs_insert? Mapping devargs to device is the responsibility of the bus scan, if it calls potentially destructive functions, it must rebuild the map.
This does happens when try to attach a vdev on secondary, and I think this is the real place need to fix. I will drop this patch and submit a new fix to prevent unnecessary rte_devargs_insert during the vdev bus scan.
Thanks
Qi
Quoted 64 lines
Quoted 34 lines
Quoted 26 lines
Quoted 13 lines
I think the problem is in:
rte_devargs_insert(struct rte_devargs *da) { int ret; ret = rte_devargs_remove(da); if (ret < 0) return ret; TAILQ_INSERT_TAIL(&devargs_list, da, next); return 0; }Quoted 1 line
We insert a structure which is freed!
Not usually, I hope!
Quoted 4 lines
See http://git.dpdk.org/dpdk/commit/?id=55744d83d525
Gaetan, what can be the fix?
1.
rte_devargs_insertis misdefined.
It is designed as a function that can never fail. The function should return void instead.Quoted 7 lines
2.
rte_devargs_remove(da), will not remove da itself.
It will remove whicheverrte_devargsmatches da within the internal list. If da does not match any in the list, it does nothing. As da is a newly-callocated structure, it is actually safe to continue using it after having calledrte_devargs_remove(), because it cannot possibly have been inserted in the meantime (so would not have been freed, even if another devargs matched it).Quoted 3 lines
If the devargs pointer passed in parameter is the same as the one in the list, it will be freed.
This would only happen if one did:
rte_devargs_insert(dev->devargs);Quoted 4 lines
Quoted 4 lines
The actual issue is that the matching
rte_devargswithin the list would be referenced by a device after a successful scan, meaning that this reference is not safe if someone attemps to insert the same device after thebus->scan(). If my understanding is correct, theabove
Quoted 5 lines
Quoted 2 lines
fix is not necessary, but probing should be guarded against re-entrancy.
We may want to probe again with different parameters.
Sure, but in this case the fix is to check whether the device is already probed, and if so remove it before probing it again with the new devargs.
Quoted 2 lines
Nice rant :)
:)
--
Gaëtan Rivet
6WIND
Quoted 20 lines · Qi Z
Quoted 20 lines
-----Original Message-----
From: Gaëtan Rivet [mailto:[email protected]]
Sent: Wednesday, October 24, 2018 10:34 AM
To: Thomas Monjalon <[email protected]>
Cc: [email protected]; Zhang, Qi Z <[email protected]>
Subject: Re: [dpdk-dev] [PATCH] eal: fix floating device argument pointerQuoted 10 lines · Thomas Monjalon
24/10/2018 00:39, Gaëtan Rivet:
Quoted 9 lines
Hi,
Quoted 5 lines · Thomas Monjalon
22/10/2018 07:49, Qi Zhang:
Quoted 3 lines
After we insert a devargs into
devargs_list, followingbus->scanmay destroy it due to anotherrte_devargs_insert. Its better not to use a devargs pointer after it has been inserted.
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
A bus scan calls
rte_devargs_insert? Mapping devargs to device is the responsibility of the bus scan, if it calls potentially destructive functions, it must rebuild the map.This does happens when try to attach a vdev on secondary, and I think this is the real place need to fix. I will drop this patch and submit a new fix to prevent unnecessary
rte_devargs_insertduring the vdev bus scan.
The vdev_init function should call dev_probe instead of reimplementing it. But looking at the big picture, maybe the real bug is secondary process.
Quoted 67 lines
Thanks
QiQuoted 64 lines
Quoted 34 lines
Quoted 26 lines
Quoted 13 lines
I think the problem is in:
rte_devargs_insert(struct rte_devargs *da) { int ret; ret = rte_devargs_remove(da); if (ret < 0) return ret; TAILQ_INSERT_TAIL(&devargs_list, da, next); return 0; }Quoted 1 line
We insert a structure which is freed!
Not usually, I hope!
Quoted 4 lines
See http://git.dpdk.org/dpdk/commit/?id=55744d83d525
Gaetan, what can be the fix?
1.
rte_devargs_insertis misdefined.
It is designed as a function that can never fail. The function should return void instead.Quoted 7 lines
2.
rte_devargs_remove(da), will not remove da itself.
It will remove whicheverrte_devargsmatches da within the internal list. If da does not match any in the list, it does nothing. As da is a newly-callocated structure, it is actually safe to continue using it after having calledrte_devargs_remove(), because it cannot possibly have been inserted in the meantime (so would not have been freed, even if another devargs matched it).Quoted 3 lines
If the devargs pointer passed in parameter is the same as the one in the list, it will be freed.
This would only happen if one did:
rte_devargs_insert(dev->devargs);Quoted 4 lines
Quoted 4 lines
The actual issue is that the matching
rte_devargswithin the list would be referenced by a device after a successful scan, meaning that this reference is not safe if someone attemps to insert the same device after thebus->scan(). If my understanding is correct, theabove
Quoted 5 lines
Quoted 2 lines
fix is not necessary, but probing should be guarded against re-entrancy.
We may want to probe again with different parameters.
Sure, but in this case the fix is to check whether the device is already probed, and if so remove it before probing it again with the new devargs.
Quoted 2 lines
Nice rant :)
:)
--
Gaëtan Rivet
6WIND
Signature · 2 lines
Gaëtan Rivet 6WIND