Skip to contentSkip to conversation

bus/pci: update device devargs on each rescan

PATCH In review Darek Stojaczyk 5 participants 14 messages Nov 5, 2018 – Nov 12, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 bus/pci: update device devargs on each rescan

Bus rescan is done e.g. during the device hotplug, where devargs are re-allocated. By not updating the rte_device->devargs pointer we potentially make it a dangling one, as previous devargs could have been (or will be soon) freed.

Fixes 55e411b301c3 ("bus/pci: fix resource mapping override")
---
 drivers/bus/pci/linux/pci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/bus/pci/linux/pci.c +1 -1
index f87533c5c..f3172960e 100644
@@ -349,10 +349,10 @@ pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
 			if (ret < 0) {
 				rte_pci_insert_device(dev2, dev);
 			} else { /* already registered */
+				pci_name_set(dev2);
 				if (!rte_dev_is_probed(&dev2->device)) {
 					dev2->kdrv = dev->kdrv;
 					dev2->max_vfs = dev->max_vfs;
-					pci_name_set(dev2);
 					memmove(dev2->mem_resource,
 						dev->mem_resource,
 						sizeof(dev->mem_resource));
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/3 devargs: delay freeing previous devargs when overriding them

In eal hotplug path, the previous devargs may be still referenced by device structs at the time the rte_devargs_insert() is called. Those references are updated almost immediately afterwards, but in cases something goes wrong and they cannot be updated, we might want to still keep the old devargs around.

This patch modifies rte_devargs_insert() so that it returns a pointer to previous devargs that are being overridden. In case something in the EAL hotplug path goes wrong, we can now remove the newly inserted devargs and re-insert the old ones.

Note: Functional changes will come in a subsequent patch. This
one only extends the API.

---
 drivers/bus/vdev/vdev.c                     | 12 +++++++++---
 lib/librte_eal/common/eal_common_dev.c      | 11 +++++++----
 lib/librte_eal/common/eal_common_devargs.c  | 21 ++++++++++++++-------
 lib/librte_eal/common/include/rte_devargs.h |  8 ++------
 4 files changed, 32 insertions(+), 20 deletions(-)
drivers/bus/vdev/vdev.c +9 -3
index 9c66bdc78..bbdae2314 100644
@@ -9,6 +9,7 @@
 #include <stdint.h>
 #include <stdbool.h>
 #include <sys/queue.h>
+#include <assert.h>
 
 #include <rte_eal.h>
 #include <rte_dev.h>
@@ -207,7 +208,7 @@ insert_vdev(const char *name, const char *args,
 		bool init)
 {
 	struct rte_vdev_device *dev;
-	struct rte_devargs *devargs;
+	struct rte_devargs *devargs, *prev_devargs;
 	int ret;
 
 	if (name == NULL)
@@ -239,8 +240,13 @@ insert_vdev(const char *name, const char *args,
 	}
 
 	TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
-	if (init)
-		rte_devargs_insert(devargs);
+	if (init) {
+		rte_devargs_insert(devargs, &prev_devargs);
+		 /* any previous devargs should have been caught by the above
+		  * find_vdev(name) check
+		  */
+		assert(prev_devargs == NULL);
+	}
 
 	if (p_dev)
 		*p_dev = dev;
lib/librte_eal/common/eal_common_dev.c +7 -4
index 62e9ed477..4cb424df1 100644
@@ -131,7 +131,7 @@ int
 local_dev_probe(const char *devargs, struct rte_device **new_dev)
 {
 	struct rte_device *dev;
-	struct rte_devargs *da;
+	struct rte_devargs *da, *prev_da;
 	int ret;
 
 	*new_dev = NULL;
@@ -150,9 +150,12 @@ local_dev_probe(const char *devargs, struct rte_device **new_dev)
 		goto err_devarg;
 	}
 
-	ret = rte_devargs_insert(da);
-	if (ret)
-		goto err_devarg;
+	rte_devargs_insert(da, &prev_da);
+
+	if (prev_da != NULL) {
+		free(prev_da->args);
+		free(prev_da);
+	}
 
 	ret = da->bus->scan();
 	if (ret)
lib/librte_eal/common/eal_common_devargs.c +14 -7
index b7b9cb69e..c46365b69 100644
@@ -262,16 +262,23 @@ rte_devargs_parsef(struct rte_devargs *da, const char *format, ...)
 	return ret;
 }
 
-int __rte_experimental
-rte_devargs_insert(struct rte_devargs *da)
+void __rte_experimental
+rte_devargs_insert(struct rte_devargs *da, struct rte_devargs **prev_da)
 {
-	int ret;
+	struct rte_devargs *d;
+	void *tmp;
+
+	*prev_da = NULL;
+	TAILQ_FOREACH_SAFE(d, &devargs_list, next, tmp) {
+		if (strcmp(d->bus->name, da->bus->name) == 0 &&
+		    strcmp(d->name, da->name) == 0) {
+			TAILQ_REMOVE(&devargs_list, d, next);
+			*prev_da = d;
+			break;
+		}
+	}
 
-	ret = rte_devargs_remove(da);
-	if (ret < 0)
-		return ret;
 	TAILQ_INSERT_TAIL(&devargs_list, da, next);
-	return 0;
 }
 
 /* store a whitelist parameter for later parsing */
lib/librte_eal/common/include/rte_devargs.h +2 -6
index b1f121f83..753cf7f54 100644
@@ -146,14 +146,10 @@ __attribute__((format(printf, 2, 0)));
  *
  * @param da
  *  The devargs structure to insert.
- *
- * @return
- *   - 0 on success
- *   - Negative on error.
  */
 __rte_experimental
-int
-rte_devargs_insert(struct rte_devargs *da);
+void
+rte_devargs_insert(struct rte_devargs *da, struct rte_devargs **prev_da);
 
 /**
  * Add a device to the user device list
Signature · 1 line
2.17.1
#6 lore raw

PATCH 3/3 eal: handle bus rescan failures during hotplug

In reply to Darek Stojaczyk

Fixes for devargs dangling pointers and various segfaults caused by failed hotplug requests.

If rescan failed, we have one the following scenarios to cover:
a) the device was never scanned at all; the devargs that were just allocated are not referenced anywhere and can be removed straight away. Previous devargs (if any) need to be re-inserted as they are still in use.
b) the device was scanned before, but the rescan failed before it reached our device, meaning that its devargs were not overridden and we can remove our newly allocated ones. Previous devargs need to be re-inserted.
c) we managed to rescan our device and only failed on something after that, which means we have to free the memory of previous devargs and stick with the new ones, despite the fact that the hotplug still needs to be failed.

Fixes 7e8b26650146 ("eal: fix hotplug add / remove")
---
 lib/librte_eal/common/eal_common_dev.c | 30 ++++++++++++++++++--------
 1 file changed, 21 insertions(+), 9 deletions(-)
lib/librte_eal/common/eal_common_dev.c +21 -9
index 4cb424df1..b090295b9 100644
@@ -7,6 +7,7 @@
 #include <string.h>
 #include <inttypes.h>
 #include <sys/queue.h>
+#include <assert.h>
 
 #include <rte_compat.h>
 #include <rte_bus.h>
@@ -141,32 +142,33 @@ local_dev_probe(const char *devargs, struct rte_device **new_dev)
 
 	ret = rte_devargs_parse(da, devargs);
 	if (ret)
-		goto err_devarg;
+		goto err_remove_devargs;
 
 	if (da->bus->plug == NULL) {
 		RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
 			da->bus->name);
 		ret = -ENOTSUP;
-		goto err_devarg;
+		goto err_remove_devargs;
 	}
 
 	rte_devargs_insert(da, &prev_da);
 
+	ret = da->bus->scan();
+	if (ret)
+		goto err_rollback_devargs;
+
 	if (prev_da != NULL) {
 		free(prev_da->args);
 		free(prev_da);
+		prev_da = NULL;
 	}
 
-	ret = da->bus->scan();
-	if (ret)
-		goto err_devarg;
-
 	dev = da->bus->find_device(NULL, cmp_dev_name, da->name);
 	if (dev == NULL) {
 		RTE_LOG(ERR, EAL, "Cannot find device (%s)\n",
 			da->name);
 		ret = -ENODEV;
-		goto err_devarg;
+		goto err_remove_devargs;
 	}
 
 	ret = dev->bus->plug(dev);
@@ -175,13 +177,23 @@ local_dev_probe(const char *devargs, struct rte_device **new_dev)
 			return ret; /* no rollback */
 		RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
 			dev->name);
-		goto err_devarg;
+		goto err_remove_devargs;
 	}
 
 	*new_dev = dev;
 	return 0;
 
-err_devarg:
+err_rollback_devargs:
+	dev = da->bus->find_device(NULL, cmp_dev_name, da->name);
+	if (prev_da != NULL && (dev == NULL || dev->devargs != da)) {
+		/* either the device wasn't scanned, or we didn't
+		 * manage yet to update its devargs.
+		 */
+		rte_devargs_insert(prev_da, &da);
+		assert(da != NULL);
+	}
+
+err_remove_devargs:
 	if (rte_devargs_remove(da) != 0) {
 		free(da->args);
 		free(da);
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH 2/3] devargs: delay freeing previous devargs when overriding them

In reply to Darek Stojaczyk

Hi,

05/11/2018 08:04, Darek Stojaczyk:

lib/librte_eal/common/eal_common_devargs.c rte_devargs_parsef(struct rte_devargs *da, const char *format, ...) · line 266

Quoted 4 lines
lib/librte_eal/common/eal_common_devargs.c +2 -2
-int __rte_experimental
-rte_devargs_insert(struct rte_devargs *da)
+void __rte_experimental
+rte_devargs_insert(struct rte_devargs *da, struct rte_devargs **prev_da)

You should update the API section of the release notes.

Quoted 8 lines

{

lib/librte_eal/common/eal_common_devargs.c +6 -1
-       int ret;
+       struct rte_devargs *d;
+       void *tmp;
+
+       *prev_da = NULL;
+       TAILQ_FOREACH_SAFE(d, &devargs_list, next, tmp) {
+               if (strcmp(d->bus->name, da->bus->name) == 0 &&

lib/librte_eal/common/eal_common_devargs.c rte_devargs_parsef(struct rte_devargs *da, const char *format, ...) · line 272

Quoted 10 lines
lib/librte_eal/common/eal_common_devargs.c +6 -3
+                   strcmp(d->name, da->name) == 0) {
+                       TAILQ_REMOVE(&devargs_list, d, next);
+                       *prev_da = d;
+                       break;
+               }
+       }
 
-       ret = rte_devargs_remove(da);
-       if (ret < 0)
-               return ret;

Why not updating rte_devargs_remove instead of duplicating its code?

#3 lore raw

Re: [PATCH 2/3] devargs: delay freeing previous devargs when overriding them

Hi Thomas,

Quoted 10 lines

-----Original Message-----
From: Thomas Monjalon [mailto:[email protected]]
Sent: Monday, November 5, 2018 8:31 AM
To: Stojaczyk, Dariusz <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH 2/3] devargs: delay freeing previous devargs when overriding them

Hi,

Quoted 8 lines

05/11/2018 08:04, Darek Stojaczyk:

lib/librte_eal/common/eal_common_devargs.c rte_devargs_parsef(struct rte_devargs *da, const char *format, ...)

Quoted 4 lines
lib/librte_eal/common/eal_common_devargs.c +2 -2
-int __rte_experimental
-rte_devargs_insert(struct rte_devargs *da)
+void __rte_experimental
+rte_devargs_insert(struct rte_devargs *da, struct rte_devargs

**prev_da)

You should update the API section of the release notes.

Even for experimental API? OK, I didn't know it's needed.

Quoted 16 lines
Quoted 16 lines

{

lib/librte_eal/common/eal_common_devargs.c +12 -1
-       int ret;
+       struct rte_devargs *d;
+       void *tmp;
+
+       *prev_da = NULL;
+       TAILQ_FOREACH_SAFE(d, &devargs_list, next, tmp) {
+               if (strcmp(d->bus->name, da->bus->name) == 0 &&
+                   strcmp(d->name, da->name) == 0) {
+                       TAILQ_REMOVE(&devargs_list, d, next);
+                       *prev_da = d;
+                       break;
+               }
+       }
Quoted 6 lines

lib/librte_eal/common/eal_common_devargs.c rte_devargs_parsef(struct rte_devargs *da, const char *format, ...) · line 272

Quoted 3 lines
lib/librte_eal/common/eal_common_devargs.c +0 -3
-       ret = rte_devargs_remove(da);
-       if (ret < 0)
-               return ret;

Why not updating rte_devargs_remove instead of duplicating its code?

We still want to preserve the functionality of rte_devargs_remove.
rte_devargs_remove does TAILQ_REMOVE + free; rte_devargs_insert does just TAILQ_REMOVE. (I think I also forgot to update rte_devargs_insert documentation, I'll do that in V2)

Since you've mentioned it:
Eventually I'd see rte_devargs_remove to accept the exact same devargs parameter that was passed to rte_devargs_insert. Then rte_devargs_remove wouldn't do any sort of lookup. Maybe additional rte_devargs_find(const char *name) could be added for existing cases where the original devargs struct is not available. However, I'm not familiar enough with this code to perform the refactor and am just trying to fix the stuff. Still, how does it sound?

D.

#4 lore raw

Re: [PATCH 2/3] devargs: delay freeing previous devargs when overriding them

05/11/2018 09:25, Stojaczyk, Dariusz:

Quoted 10 lines

Hi Thomas,

From: Thomas Monjalon [mailto:[email protected]]

Quoted 7 lines

05/11/2018 08:04, Darek Stojaczyk:

lib/librte_eal/common/eal_common_devargs.c rte_devargs_parsef(struct rte_devargs *da, const char *format, ...)

Quoted 4 lines
lib/librte_eal/common/eal_common_devargs.c +2 -2
-int __rte_experimental
-rte_devargs_insert(struct rte_devargs *da)
+void __rte_experimental
+rte_devargs_insert(struct rte_devargs *da, struct rte_devargs

**prev_da)

Quoted 3 lines
Quoted 1 line

You should update the API section of the release notes.

Even for experimental API? OK, I didn't know it's needed.

Yes, even for experimental API, the API changes must documented.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

{

lib/librte_eal/common/eal_common_devargs.c +12 -1
-       int ret;
+       struct rte_devargs *d;
+       void *tmp;
+
+       *prev_da = NULL;
+       TAILQ_FOREACH_SAFE(d, &devargs_list, next, tmp) {
+               if (strcmp(d->bus->name, da->bus->name) == 0 &&
+                   strcmp(d->name, da->name) == 0) {
+                       TAILQ_REMOVE(&devargs_list, d, next);
+                       *prev_da = d;
+                       break;
+               }
+       }
Quoted 8 lines
Quoted 5 lines

lib/librte_eal/common/eal_common_devargs.c rte_devargs_parsef(struct rte_devargs *da, const char *format, ...) · line 272

Quoted 3 lines
lib/librte_eal/common/eal_common_devargs.c +0 -3
-       ret = rte_devargs_remove(da);
-       if (ret < 0)
-               return ret;

Why not updating rte_devargs_remove instead of duplicating its code?

We still want to preserve the functionality of rte_devargs_remove.
rte_devargs_remove does TAILQ_REMOVE + free; rte_devargs_insert does just TAILQ_REMOVE. (I think I also forgot to update rte_devargs_insert documentation, I'll do that in V2)

Yes, because of the rollback, OK.
Please mention in devargs_insert doc that the old devargs can be used for rollback.

Quoted 2 lines

Since you've mentioned it:
Eventually I'd see rte_devargs_remove to accept the exact same devargs parameter that was passed to rte_devargs_insert. Then rte_devargs_remove wouldn't do any sort of lookup. Maybe additional rte_devargs_find(const char *name) could be added for existing cases where the original devargs struct is not available. However, I'm not familiar enough with this code to perform the refactor and am just trying to fix the stuff. Still, how does it sound?

I think we can keep it as is.

We can re-think the whole thing in the next release.
I think we should not play with devargs list as we do.
There should be only lists for scanned devices of each bus and that's all.

#7 lore raw
In reply to Darek Stojaczyk

Hi Darek,

Quoted 19 lines · Darek Stojaczyk

Bus rescan is done e.g. during the device hotplug, where devargs are re-allocated. By not updating the rte_device->devargs pointer we potentially make it a dangling one, as previous devargs could have been (or will be soon) freed.

Fixes 55e411b301c3 ("bus/pci: fix resource mapping override")
---
 drivers/bus/pci/linux/pci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/bus/pci/linux/pci.c +0 -0
index f87533c5c..f3172960e 100644

drivers/bus/pci/linux/pci.c pci_scan_one(const char *dirname, const struct rte_pci_addr *addr) · line 352

Quoted 4 lines
drivers/bus/pci/linux/pci.c +1 -0
@@ -349,10 +349,10 @@ pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
 			if (ret < 0) {
 				rte_pci_insert_device(dev2, dev);
 			} else { /* already registered */
+				pci_name_set(dev2);

This is rather unfortunate to call pci_name_set()
to trigger the mapping devargs <-> devices.

pci_devargs_lookup could be made non-static instead,
if that's sufficient. Given that the PCI id matches because the device is a duplicate (already registered), then the name itself probably does not need to be updated.

Quoted 10 lines
drivers/bus/pci/linux/pci.c +0 -1
 				if (!rte_dev_is_probed(&dev2->device)) {
 					dev2->kdrv = dev->kdrv;
 					dev2->max_vfs = dev->max_vfs;
-					pci_name_set(dev2);
 					memmove(dev2->mem_resource,
 						dev->mem_resource,
 						sizeof(dev->mem_resource));
Signature · 1 line
2.17.1
Signature · 2 lines
Gaëtan Rivet
6WIND
#8 lore raw

Hi Gaetan,

Quoted 36 lines

-----Original Message-----
From: Gaëtan Rivet [mailto:[email protected]]
Subject: Re: [dpdk-dev] [PATCH 1/3] bus/pci: update device devargs on each rescan

Hi Darek,

drivers/bus/pci/linux/pci.c pci_scan_one(const char *dirname, const struct

Quoted 19 lines · Darek Stojaczyk

Bus rescan is done e.g. during the device hotplug, where devargs are re-allocated. By not updating the rte_device->devargs pointer we potentially make it a dangling one, as previous devargs could have been (or will be soon) freed.

Fixes 55e411b301c3 ("bus/pci: fix resource mapping override")
---
 drivers/bus/pci/linux/pci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/bus/pci/linux/pci.c +0 -0
index f87533c5c..f3172960e 100644
@@ -349,10 +349,10 @@ pci_scan_one(const char *dirname, const struct

rte_pci_addr *addr)

drivers/bus/pci/linux/pci.c pci_scan_one(const char *dirname, const struct rte_pci_addr *addr) · line 352

Quoted 4 lines
drivers/bus/pci/linux/pci.c +1 -0
 			if (ret < 0) {
 				rte_pci_insert_device(dev2, dev);
 			} else { /* already registered */
+				pci_name_set(dev2);

This is rather unfortunate to call pci_name_set()
to trigger the mapping devargs <-> devices.

Quoted 2 lines

pci_devargs_lookup could be made non-static instead, if that's sufficient.

This is unfortunately trigerred by the generic eal device hotplug path which looks as follows:

da = calloc(sizeof rte_devargs);
[...]
rte_devargs_insert(da); # frees the previous devargs that rte_device still references da->bus->scan(); # this should update the dangling devargs/name pointer in rte_device

Instead of making pci_devargs_lookup public, we would have to introduce a new bus callback for updating devargs of only a single device and that would be a really, realy ugly overkill. Thomas already mentioned in the subsequent patch within this series that we might want to re-think the devargs design in the next release.

Quoted 3 lines

Given that the PCI id matches because the device
is a duplicate (already registered), then the name itself probably does not need to be updated.

The issue here was that old devargs/name could have been freed. The new name is the same as the old one, it's just in a different buffer.

I know calling pci_name_set() on each device during scan is not perfect, but IMO it's enforced by the current rte_devargs design. Besides, this used to be the original behavior before 55e411b301c3 ("bus/pci: fix resource mapping override").

Thanks,
D.

Quoted 6 lines
Quoted 2 lines

<snip>

--
Gaëtan Rivet
6WIND

#5 lore raw

Re: [PATCH 2/3] devargs: delay freeing previous devargs when overriding them

In reply to Thomas Monjalon
Quoted 36 lines · Thomas Monjalon

05/11/2018 09:25, Stojaczyk, Dariusz:

Quoted 10 lines

Hi Thomas,

From: Thomas Monjalon [mailto:[email protected]]

Quoted 7 lines

05/11/2018 08:04, Darek Stojaczyk:

lib/librte_eal/common/eal_common_devargs.c rte_devargs_parsef(struct rte_devargs *da, const char *format, ...)

Quoted 4 lines
lib/librte_eal/common/eal_common_devargs.c +2 -2
-int __rte_experimental
-rte_devargs_insert(struct rte_devargs *da)
+void __rte_experimental
+rte_devargs_insert(struct rte_devargs *da, struct rte_devargs

**prev_da)

Quoted 3 lines
Quoted 1 line

You should update the API section of the release notes.

Even for experimental API? OK, I didn't know it's needed.

Yes, even for experimental API, the API changes must documented.

Quoted 19 lines
Quoted 19 lines
Quoted 18 lines

{

lib/librte_eal/common/eal_common_devargs.c +12 -4
-       int ret;
+       struct rte_devargs *d;
+       void *tmp;
+
+       *prev_da = NULL;
+       TAILQ_FOREACH_SAFE(d, &devargs_list, next, tmp) {
+               if (strcmp(d->bus->name, da->bus->name) == 0 &&
+                   strcmp(d->name, da->name) == 0) {
+                       TAILQ_REMOVE(&devargs_list, d, next);
+                       *prev_da = d;
+                       break;
+               }
+       }
 
-       ret = rte_devargs_remove(da);
-       if (ret < 0)
-               return ret;
Quoted 9 lines
Quoted 4 lines
Quoted 1 line

Why not updating rte_devargs_remove instead of duplicating its code?

We still want to preserve the functionality of rte_devargs_remove.
rte_devargs_remove does TAILQ_REMOVE + free; rte_devargs_insert does just TAILQ_REMOVE. (I think I also forgot to update rte_devargs_insert documentation, I'll do that in V2)

Yes, because of the rollback, OK.
Please mention in devargs_insert doc that the old devargs can be used for rollback.

This is not ok. You are leaking those devargs that are not freed. Once a devargs is inserted, the current API considers them to be managed by EAL - i.e. no one else should manipulate them (get them out of the list or freeing them). They are meant afterward to be linked by rte_device, so the only moment they can be removed is right before a bus scan (which will rebuild the devargs -> device mapping).

Returning the prev_devargs from devargs_insert() is a bad construct. Insert only does insertion, not search+insert.

Either, devargs_insert() finds a previous occurence, and returns an error EEXIST before aborting, or it forces the insertion. But it should not do something else to avoid a function call.

Quoted 11 lines
Quoted 2 lines

Since you've mentioned it:
Eventually I'd see rte_devargs_remove to accept the exact same devargs parameter that was passed to rte_devargs_insert. Then rte_devargs_remove wouldn't do any sort of lookup. Maybe additional rte_devargs_find(const char *name) could be added for existing cases where the original devargs struct is not available. However, I'm not familiar enough with this code to perform the refactor and am just trying to fix the stuff. Still, how does it sound?

I think we can keep it as is.

We can re-think the whole thing in the next release.
I think we should not play with devargs list as we do.
There should be only lists for scanned devices of each bus and that's all.

devargs_{insert,remove} currently identifies a devargs from its (bus, name) pair. This pair is unique in the list. Accepting the exact same devargs parameter is not possible, because sometimes you won't have this devargs handle, you can only identify it by the pair above.

I think devargs_find() would give the wrong idea (returning a devargs handle that is still in the devargs list: a user should not think it can be manipulated this way).

Instead I'd propose:

devargs_insert() returns -EEXIST if the devargs identifying pair is found
in the list.
devargs_extract() will remove this devargs from the list and return it
if found. devargs_insert() afterward would work. Once extracted, the devargs is the responsibility of the caller (so should be freed if not needed anymore).
devargs_remove() stays the same, finding the devargs and freeing it, or
returning >0 if the devargs was not within the list.

Remaining issue is that extract() would invalidate a bunch of rte_device pointers, which is not acceptable. Either a back-reference is made from devargs to device, so that rte_devices are updated accordingly, or we must ensure each device bus scan is called right after to make sure the mapping is fixed.

Signature · 2 lines
Gaëtan Rivet
6WIND
#9 lore raw
In reply to Stojaczyk, Dariusz
Quoted 41 lines · Dariusz

Hi Gaetan,

Quoted 36 lines

-----Original Message-----
From: Gaëtan Rivet [mailto:[email protected]]
Subject: Re: [dpdk-dev] [PATCH 1/3] bus/pci: update device devargs on each rescan

Hi Darek,

drivers/bus/pci/linux/pci.c pci_scan_one(const char *dirname, const struct

Quoted 19 lines · Darek Stojaczyk

Bus rescan is done e.g. during the device hotplug, where devargs are re-allocated. By not updating the rte_device->devargs pointer we potentially make it a dangling one, as previous devargs could have been (or will be soon) freed.

Fixes 55e411b301c3 ("bus/pci: fix resource mapping override")
---
 drivers/bus/pci/linux/pci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/bus/pci/linux/pci.c +0 -0
index f87533c5c..f3172960e 100644
@@ -349,10 +349,10 @@ pci_scan_one(const char *dirname, const struct

rte_pci_addr *addr)

drivers/bus/pci/linux/pci.c pci_scan_one(const char *dirname, const struct rte_pci_addr *addr) · line 352

Quoted 4 lines
drivers/bus/pci/linux/pci.c +1 -0
 			if (ret < 0) {
 				rte_pci_insert_device(dev2, dev);
 			} else { /* already registered */
+				pci_name_set(dev2);

This is rather unfortunate to call pci_name_set()
to trigger the mapping devargs <-> devices.

Quoted 2 lines

pci_devargs_lookup could be made non-static instead, if that's sufficient.

Quoted 9 lines

This is unfortunately trigerred by the generic eal device hotplug path which looks as follows:

da = calloc(sizeof rte_devargs);
[...]
rte_devargs_insert(da); # frees the previous devargs that rte_device still references da->bus->scan(); # this should update the dangling devargs/name pointer in rte_device

Instead of making pci_devargs_lookup public, we would have to introduce a new bus callback for updating devargs of only a single device and that would be a really, realy ugly overkill. Thomas already mentioned in the subsequent patch within this series that we might want to re-think the devargs design in the next release.

No, of course, I'm not saying to make pci_devargs_lookup public, only private to the PCI bus, and within the bus, non-static. This way, instead of calling pci_name_set(), you call instead only pci_devargs_lookup().

Quoted 18 lines
Quoted 3 lines

Given that the PCI id matches because the device
is a duplicate (already registered), then the name itself probably does not need to be updated.

The issue here was that old devargs/name could have been freed. The new name is the same as the old one, it's just in a different buffer.

I know calling pci_name_set() on each device during scan is not perfect, but IMO it's enforced by the current rte_devargs design. Besides, this used to be the original behavior before 55e411b301c3 ("bus/pci: fix resource mapping override").

Thanks,
D.

Quoted 6 lines
Quoted 2 lines

<snip>

--
Gaëtan Rivet
6WIND

Signature · 2 lines
Gaëtan Rivet
6WIND
#10 lore raw

PATCH v2 1/1 bus/pci: update device devargs on each rescan

In reply to Darek Stojaczyk

From: Darek Stojaczyk <[email protected]>

Bus rescan is done e.g. during the device hotplug, where devargs are re-allocated. By not updating the rte_device->devargs pointer we potentially make it a dangling one, as previous devargs could have been (or will be soon) freed.

Fixes 55e411b301c3 ("bus/pci: fix resource mapping override")

---
Changes since v1:
* introduce additional pci_devargs_lookup() check to make the code slightly more readable (Gaetan Rivet)

 drivers/bus/pci/linux/pci.c  |  5 +++++
 drivers/bus/pci/pci_common.c |  3 ++-
 drivers/bus/pci/private.h    | 11 +++++++++++
 3 files changed, 18 insertions(+), 1 deletion(-)
drivers/bus/pci/linux/pci.c +5 -0
index c99d523f0..a32d4848e 100644
@@ -379,6 +379,11 @@ pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
 						RTE_LOG(ERR, EAL, "Unexpected device scan at %s!\n",
 							filename);
 				}
+
+				if (pci_devargs_lookup(dev2) !=
+						dev2->device.devargs)
+					pci_name_set(dev2);
+
 				free(dev);
 			}
 			return 0;
drivers/bus/pci/pci_common.c +2 -1
index 6276e5d69..fcaf76ed2 100644
@@ -42,7 +42,8 @@ const char *rte_pci_get_sysfs_path(void)
 	return path;
 }
 
-static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
+struct rte_devargs *
+pci_devargs_lookup(struct rte_pci_device *dev)
 {
 	struct rte_devargs *devargs;
 	struct rte_pci_addr addr;
drivers/bus/pci/private.h +11 -0
index 13c3324bb..8b2e904f0 100644
@@ -43,6 +43,17 @@ void
 pci_name_set(struct rte_pci_device *dev);
 
 /**
+ * Find matching up-to-date devargs for a given device.
+ *
+ * @param pci_dev
+ *   PCI device
+ * @return
+ *   A devargs pointer or NULL
+ */
+struct rte_devargs *
+pci_devargs_lookup(struct rte_pci_device *dev);
+
+/**
  * Add a PCI device to the PCI Bus (append to PCI Device list). This function
  * also updates the bus references of the PCI Device (and the generic device
  * object embedded within.
Signature · 1 line
2.11.0
#11 lore raw
Quoted 10 lines

-----Original Message-----
From: Dariusz Stojaczyk [mailto:[email protected]]
Sent: Monday, November 5, 2018 10:40 PM
To: [email protected]; [email protected]
Cc: Stojaczyk, Dariusz <[email protected]>; Zhang, Qi Z <[email protected]>
Subject: [PATCH v2] bus/pci: update device devargs on each rescan

Quoted 6 lines

From: Darek Stojaczyk <[email protected]>

Bus rescan is done e.g. during the device hotplug, where devargs are re-allocated. By not updating the rte_device->devargs pointer we potentially make it a dangling one, as previous devargs could have been (or will be soon) freed.

Yes, this is the similar issue we met on vdev.

The key problem is we have rte_devargs_insert will destroy a devargs which is still referenced by a rte_device
I'm thinking , why not just keep always keep a snapshot of devargs in rte_device to make things simple?
We could introduce a API like rte_dev_assign_devargs(dev, devargs) which handled the clone and destroy stuff and can be used for all buses. If that idea is acceptable, I would prefer the issue in this patch could be fixed in pci_name_set by clone a new copy as workaround.

Quoted 71 lines
Fixes 55e411b301c3 ("bus/pci: fix resource mapping override")

---
Changes since v1:
* introduce additional pci_devargs_lookup() check to make the code slightly more readable (Gaetan Rivet)

drivers/bus/pci/linux/pci.c | 5 +++++ drivers/bus/pci/pci_common.c |
3 ++-

 drivers/bus/pci/private.h    | 11 +++++++++++
 3 files changed, 18 insertions(+), 1 deletion(-)
drivers/bus/pci/linux/pci.c index renamed from drivers/bus/pci/linux/pci.c +0 -0

c99d523f0..a32d4848e 100644

drivers/bus/pci/linux/pci.c +5 -0
@@ -379,6 +379,11 @@ pci_scan_one(const char *dirname, const struct
 rte_pci_addr *addr)
 						RTE_LOG(ERR, EAL, "Unexpected device scan
 at %s!\n",
 							filename);
 				}
+
+				if (pci_devargs_lookup(dev2) !=
+						dev2->device.devargs)
+					pci_name_set(dev2);
+
 				free(dev);
drivers/bus/pci/linux/pci.c +0 -0
 			}
 			return 0;
drivers/bus/pci/pci_common.c +2 -1
index 6276e5d69..fcaf76ed2 100644
@@ -42,7 +42,8 @@ const char *rte_pci_get_sysfs_path(void)
 	return path;
 }
 
-static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
+struct rte_devargs *
+pci_devargs_lookup(struct rte_pci_device *dev)
 {
 	struct rte_devargs *devargs;
 	struct rte_pci_addr addr;
drivers/bus/pci/private.h index renamed from drivers/bus/pci/private.h +0 -0

13c3324bb..8b2e904f0 100644

drivers/bus/pci/private.h +11 -1
@@ -43,6 +43,17 @@ void
 pci_name_set(struct rte_pci_device *dev);
 
 /**
+ * Find matching up-to-date devargs for a given device.
+ *
+ * @param pci_dev
+ *   PCI device
+ * @return
+ *   A devargs pointer or NULL
+ */
+struct rte_devargs *
+pci_devargs_lookup(struct rte_pci_device *dev);
+
+/**
  * Add a PCI device to the PCI Bus (append to PCI Device list). This function
  * also updates the bus references of the PCI Device (and the generic device
  * object embedded within.
--

2.11.0

#12 lore raw
Quoted 17 lines · Qi Z
Quoted 10 lines

-----Original Message-----
From: Dariusz Stojaczyk [mailto:[email protected]]
Sent: Monday, November 5, 2018 10:40 PM
To: [email protected]; [email protected]
Cc: Stojaczyk, Dariusz <[email protected]>; Zhang, Qi Z <[email protected]>
Subject: [PATCH v2] bus/pci: update device devargs on each rescan

Quoted 6 lines

From: Darek Stojaczyk <[email protected]>

Bus rescan is done e.g. during the device hotplug, where devargs are re-allocated. By not updating the rte_device->devargs pointer we potentially make it a dangling one, as previous devargs could have been (or will be soon) freed.

Quoted 7 lines

Yes, this is the similar issue we met on vdev.

The key problem is we have rte_devargs_insert will destroy a devargs which is still referenced by a rte_device
I'm thinking , why not just keep always keep a snapshot of devargs in rte_device to make things simple?
We could introduce a API like rte_dev_assign_devargs(dev, devargs) which handled the clone and destroy stuff and can be used for all buses. If that idea is acceptable, I would prefer the issue in this patch could be fixed in pci_name_set by clone a new copy as workaround.

I agree that it would be useful to have

    rte_devargs_map(devargs, device);

That will link safely a devargs to a device, but cloning is overkill.

I disagree that dangling pointers and loose references is fixed by introducing more cloning and copies of stuff here and there.

We must tighten the device -> devargs coupling, not loosen it.

Signature · 2 lines
Gaëtan Rivet
6WIND
#13 lore raw

07/11/2018 00:40, Gaëtan Rivet:

Quoted 19 lines · Qi Z
Quoted 8 lines

From: Dariusz Stojaczyk [mailto:[email protected]]

Quoted 6 lines

From: Darek Stojaczyk <[email protected]>

Bus rescan is done e.g. during the device hotplug, where devargs are re-allocated. By not updating the rte_device->devargs pointer we potentially make it a dangling one, as previous devargs could have been (or will be soon) freed.

Quoted 7 lines

Yes, this is the similar issue we met on vdev.

The key problem is we have rte_devargs_insert will destroy a devargs which is still referenced by a rte_device
I'm thinking , why not just keep always keep a snapshot of devargs in rte_device to make things simple?
We could introduce a API like rte_dev_assign_devargs(dev, devargs) which handled the clone and destroy stuff and can be used for all buses. If that idea is acceptable, I would prefer the issue in this patch could be fixed in pci_name_set by clone a new copy as workaround.

I agree that it would be useful to have

Quoted 8 lines
    rte_devargs_map(devargs, device);

That will link safely a devargs to a device, but cloning is overkill.

I disagree that dangling pointers and loose references is fixed by introducing more cloning and copies of stuff here and there.

We must tighten the device -> devargs coupling, not loosen it.

This issue is fixed with a different approach:

	http://git.dpdk.org/dpdk/commit/?id=c7ad7754
	devargs: do not replace already inserted device