Skip to contentSkip to conversation

driver core: clear deferred probe reason on probe retry

PATCH Superseded Ahmad Fatoum 4 participants 11 messages Mar 19, 2021 – Mar 23, 2021 Driver core

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 driver core: clear deferred probe reason on probe retry

When retrying a deferred probe, any old defer reason string should be discarded. Otherwise, if the probe is deferred again at a different spot, but without setting a message, the now incorrect probe reason will remain.

This was observed with the i.MX I2C driver, which ultimately failed to probe due to lack of the GPIO driver. The probe defer for GPIO doesn't record a message, but a previous probe defer to clock_get did. This had the effect that /sys/kernel/debug/devices_deferred listed a misleading probe deferral reason.

Fixes d090b70ede02 ("driver core: add deferring probe reason to devices_deferred property")

---
v1 -> v2:
- reworded commit message (Andy)
- collected Andy's Reviewed-by

---
 drivers/base/dd.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/base/dd.c +3 -0
index 9179825ff646..e2cf3b29123e 100644
@@ -97,6 +97,9 @@ static void deferred_probe_work_func(struct work_struct *work)
 
 		get_device(dev);
 
+		kfree(dev->p->deferred_probe_reason);
+		dev->p->deferred_probe_reason = NULL;
+
 		/*
 		 * Drop the mutex while probing each device; the probe path may
 		 * manipulate the deferred list
Signature · 1 line
2.29.2
#1 lore raw

PATCH v2 2/2 driver core: add helper for deferred probe reason setting

We now have three places within the same file doing the same operation of freeing this pointer and setting it anew. A helper make this arguably easier to read, so add one.

---
v1 -> v2:
- no change

---
 drivers/base/dd.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/base/dd.c +11 -6
index e2cf3b29123e..4201baa1cc13 100644
@@ -69,6 +69,12 @@ static char async_probe_drv_names[ASYNC_DRV_NAMES_MAX_LEN];
  */
 static bool defer_all_probes;
 
+static void __device_set_deferred_probe_reason(const struct device *dev, char *reason)
+{
+	kfree(dev->p->deferred_probe_reason);
+	dev->p->deferred_probe_reason = reason;
+}
+
 /*
  * deferred_probe_work_func() - Retry probing devices in the active list.
  */
@@ -97,8 +103,7 @@ static void deferred_probe_work_func(struct work_struct *work)
 
 		get_device(dev);
 
-		kfree(dev->p->deferred_probe_reason);
-		dev->p->deferred_probe_reason = NULL;
+		__device_set_deferred_probe_reason(dev, NULL);
 
 		/*
 		 * Drop the mutex while probing each device; the probe path may
@@ -140,8 +145,7 @@ void driver_deferred_probe_del(struct device *dev)
 	if (!list_empty(&dev->p->deferred_probe)) {
 		dev_dbg(dev, "Removed from deferred list\n");
 		list_del_init(&dev->p->deferred_probe);
-		kfree(dev->p->deferred_probe_reason);
-		dev->p->deferred_probe_reason = NULL;
+		__device_set_deferred_probe_reason(dev, NULL);
 	}
 	mutex_unlock(&deferred_probe_mutex);
 }
@@ -220,11 +224,12 @@ void device_unblock_probing(void)
 void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf)
 {
 	const char *drv = dev_driver_string(dev);
+	char *reason;
 
 	mutex_lock(&deferred_probe_mutex);
 
-	kfree(dev->p->deferred_probe_reason);
-	dev->p->deferred_probe_reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);
+	reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);
+	__device_set_deferred_probe_reason(dev, reason);
 
 	mutex_unlock(&deferred_probe_mutex);
 }
Signature · 1 line
2.29.2
Reviewed-by on v2 2/2
#2 lore raw

Re: [PATCH v2 2/2] driver core: add helper for deferred probe reason setting

Quoted 3 lines · Ahmad Fatoum

We now have three places within the same file doing the same operation of freeing this pointer and setting it anew. A helper make this

makes

Quoted 1 line

arguably easier to read, so add one.

FWIW,

Now I'm wondering why deferred_probe_reason is not defined with const.

Can you check and maybe squeeze a patch in the middle (before these two of this series) to move to const?

Quoted 63 lines

---
v1 -> v2:
- no change

---
 drivers/base/dd.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/base/dd.c +11 -7
index e2cf3b29123e..4201baa1cc13 100644
@@ -69,6 +69,12 @@ static char async_probe_drv_names[ASYNC_DRV_NAMES_MAX_LEN];
  */
 static bool defer_all_probes;
 
+static void __device_set_deferred_probe_reason(const struct device *dev, char *reason)
+{
+       kfree(dev->p->deferred_probe_reason);
+       dev->p->deferred_probe_reason = reason;
+}
+
 /*
  * deferred_probe_work_func() - Retry probing devices in the active list.
  */
@@ -97,8 +103,7 @@ static void deferred_probe_work_func(struct work_struct *work)
 
                get_device(dev);
 
-               kfree(dev->p->deferred_probe_reason);
-               dev->p->deferred_probe_reason = NULL;
+               __device_set_deferred_probe_reason(dev, NULL);
 
                /*
                 * Drop the mutex while probing each device; the probe path may
@@ -140,8 +145,7 @@ void driver_deferred_probe_del(struct device *dev)
        if (!list_empty(&dev->p->deferred_probe)) {
                dev_dbg(dev, "Removed from deferred list\n");
                list_del_init(&dev->p->deferred_probe);
-               kfree(dev->p->deferred_probe_reason);
-               dev->p->deferred_probe_reason = NULL;
+               __device_set_deferred_probe_reason(dev, NULL);
        }
        mutex_unlock(&deferred_probe_mutex);
 }
@@ -220,11 +224,12 @@ void device_unblock_probing(void)
 void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf)
 {
        const char *drv = dev_driver_string(dev);
+       char *reason;
 
        mutex_lock(&deferred_probe_mutex);
 
-       kfree(dev->p->deferred_probe_reason);
-       dev->p->deferred_probe_reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);
+       reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);
+       __device_set_deferred_probe_reason(dev, reason);
 
        mutex_unlock(&deferred_probe_mutex);
 }
--

2.29.2

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH v2 2/2] driver core: add helper for deferred probe reason setting

Quoted 11 lines · Andy Shevchenko
Quoted 3 lines · Ahmad Fatoum

We now have three places within the same file doing the same operation of freeing this pointer and setting it anew. A helper make this

makes

Quoted 1 line

arguably easier to read, so add one.

FWIW,

Thanks will add for v3.

Quoted 4 lines

Now I'm wondering why deferred_probe_reason is not defined with const.

Can you check and maybe squeeze a patch in the middle (before these two of this series) to move to const?

The deferred_probe_reason is only used in this file and it either holds NULL or a pointer to a dynamically allocated string. I don't see a reason why the member should be const.

Cheers,
Ahmad

Quoted 65 lines
Quoted 63 lines

---
v1 -> v2:
- no change

---
 drivers/base/dd.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/base/dd.c +11 -7
index e2cf3b29123e..4201baa1cc13 100644
@@ -69,6 +69,12 @@ static char async_probe_drv_names[ASYNC_DRV_NAMES_MAX_LEN];
  */
 static bool defer_all_probes;
 
+static void __device_set_deferred_probe_reason(const struct device *dev, char *reason)
+{
+       kfree(dev->p->deferred_probe_reason);
+       dev->p->deferred_probe_reason = reason;
+}
+
 /*
  * deferred_probe_work_func() - Retry probing devices in the active list.
  */
@@ -97,8 +103,7 @@ static void deferred_probe_work_func(struct work_struct *work)
 
                get_device(dev);
 
-               kfree(dev->p->deferred_probe_reason);
-               dev->p->deferred_probe_reason = NULL;
+               __device_set_deferred_probe_reason(dev, NULL);
 
                /*
                 * Drop the mutex while probing each device; the probe path may
@@ -140,8 +145,7 @@ void driver_deferred_probe_del(struct device *dev)
        if (!list_empty(&dev->p->deferred_probe)) {
                dev_dbg(dev, "Removed from deferred list\n");
                list_del_init(&dev->p->deferred_probe);
-               kfree(dev->p->deferred_probe_reason);
-               dev->p->deferred_probe_reason = NULL;
+               __device_set_deferred_probe_reason(dev, NULL);
        }
        mutex_unlock(&deferred_probe_mutex);
 }
@@ -220,11 +224,12 @@ void device_unblock_probing(void)
 void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf)
 {
        const char *drv = dev_driver_string(dev);
+       char *reason;
 
        mutex_lock(&deferred_probe_mutex);
 
-       kfree(dev->p->deferred_probe_reason);
-       dev->p->deferred_probe_reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);
+       reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);
+       __device_set_deferred_probe_reason(dev, reason);
 
        mutex_unlock(&deferred_probe_mutex);
 }
--

2.29.2

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#4 lore raw

Re: [PATCH v2 2/2] driver core: add helper for deferred probe reason setting

Quoted 15 lines · Ahmad Fatoum
Quoted 11 lines · Andy Shevchenko
Quoted 3 lines · Ahmad Fatoum

We now have three places within the same file doing the same operation of freeing this pointer and setting it anew. A helper make this

makes

Quoted 1 line

arguably easier to read, so add one.

FWIW,

Thanks will add for v3.

Quoted 8 lines
Quoted 4 lines

Now I'm wondering why deferred_probe_reason is not defined with const.

Can you check and maybe squeeze a patch in the middle (before these two of this series) to move to const?

The deferred_probe_reason is only used in this file and it either holds NULL or a pointer to a dynamically allocated string. I don't see a reason why the member should be const.

But we want to be reliant on the contents of the string, right? I would put this why it shouldn't be const.

As far as I understand the strictness here is for good.

--
With Best Regards,
Andy Shevchenko

#5 lore raw

Re: [PATCH v2 2/2] driver core: add helper for deferred probe reason setting

Hello Andy,

Quoted 21 lines · Andy Shevchenko
Quoted 20 lines · Ahmad Fatoum
Quoted 11 lines · Andy Shevchenko
Quoted 3 lines · Ahmad Fatoum

We now have three places within the same file doing the same operation of freeing this pointer and setting it anew. A helper make this

makes

Quoted 1 line

arguably easier to read, so add one.

FWIW,

Thanks will add for v3.

Quoted 4 lines

Now I'm wondering why deferred_probe_reason is not defined with const.

Can you check and maybe squeeze a patch in the middle (before these two of this series) to move to const?

Quoted 8 lines
Quoted 3 lines

The deferred_probe_reason is only used in this file and it either holds NULL or a pointer to a dynamically allocated string. I don't see a reason why the member should be const.

But we want to be reliant on the contents of the string, right? I would put this why it shouldn't be const.

As far as I understand the strictness here is for good.

I don't understand. Mind sending a patch that I can squash?

Cheers,
Ahmad

Quoted 5 lines

--
With Best Regards,
Andy Shevchenko

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#6 lore raw

Re: [PATCH v2 2/2] driver core: add helper for deferred probe reason setting

In reply to Ahmad Fatoum
Quoted 6 lines · Ahmad Fatoum

We now have three places within the same file doing the same operation of freeing this pointer and setting it anew. A helper make this arguably easier to read, so add one.

One thing: Suggested-by?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

Re: [PATCH v2 2/2] driver core: add helper for deferred probe reason setting

Hi,

Quoted 9 lines · Andy Shevchenko
Quoted 6 lines · Ahmad Fatoum

We now have three places within the same file doing the same operation of freeing this pointer and setting it anew. A helper make this arguably easier to read, so add one.

One thing: Suggested-by?

I suggested it to myself while drafting the first patch.

Cheers,
Ahmad

Quoted 1 line
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#8 lore raw

Re: [PATCH v2 2/2] driver core: add helper for deferred probe reason setting

In reply to Ahmad Fatoum
Quoted 50 lines · Ahmad Fatoum

We now have three places within the same file doing the same operation of freeing this pointer and setting it anew. A helper make this arguably easier to read, so add one.

---
v1 -> v2:
- no change

---
 drivers/base/dd.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/base/dd.c +8 -4
index e2cf3b29123e..4201baa1cc13 100644
@@ -69,6 +69,12 @@ static char async_probe_drv_names[ASYNC_DRV_NAMES_MAX_LEN];
  */
 static bool defer_all_probes;
 
+static void __device_set_deferred_probe_reason(const struct device *dev, char *reason)
+{
+	kfree(dev->p->deferred_probe_reason);
+	dev->p->deferred_probe_reason = reason;
+}
+
 /*
  * deferred_probe_work_func() - Retry probing devices in the active list.
  */
@@ -97,8 +103,7 @@ static void deferred_probe_work_func(struct work_struct *work)
 
 		get_device(dev);
 
-		kfree(dev->p->deferred_probe_reason);
-		dev->p->deferred_probe_reason = NULL;
+		__device_set_deferred_probe_reason(dev, NULL);
 
 		/*
 		 * Drop the mutex while probing each device; the probe path may
@@ -140,8 +145,7 @@ void driver_deferred_probe_del(struct device *dev)
 	if (!list_empty(&dev->p->deferred_probe)) {
 		dev_dbg(dev, "Removed from deferred list\n");
 		list_del_init(&dev->p->deferred_probe);
-		kfree(dev->p->deferred_probe_reason);
-		dev->p->deferred_probe_reason = NULL;
+		__device_set_deferred_probe_reason(dev, NULL);
 	}
 	mutex_unlock(&deferred_probe_mutex);
 }

drivers/base/dd.c void device_unblock_probing(void) · line 231

Quoted 10 lines
drivers/base/dd.c +2 -2
@@ -220,11 +224,12 @@ void device_unblock_probing(void)
 void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf)
 {
 	const char *drv = dev_driver_string(dev);
+	char *reason;
 
 	mutex_lock(&deferred_probe_mutex);
 
-	kfree(dev->p->deferred_probe_reason);
-	dev->p->deferred_probe_reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);
+	reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);

No error checking?

:(

#9 lore raw

Re: [PATCH v2 2/2] driver core: add helper for deferred probe reason setting

Hello,

Quoted 56 lines · Greg Kroah-Hartman
Quoted 55 lines · Ahmad Fatoum

We now have three places within the same file doing the same operation of freeing this pointer and setting it anew. A helper make this arguably easier to read, so add one.

---
v1 -> v2:
- no change

---
 drivers/base/dd.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/base/dd.c +9 -4
index e2cf3b29123e..4201baa1cc13 100644
@@ -69,6 +69,12 @@ static char async_probe_drv_names[ASYNC_DRV_NAMES_MAX_LEN];
  */
 static bool defer_all_probes;
 
+static void __device_set_deferred_probe_reason(const struct device *dev, char *reason)
+{
+	kfree(dev->p->deferred_probe_reason);
+	dev->p->deferred_probe_reason = reason;
+}
+
 /*
  * deferred_probe_work_func() - Retry probing devices in the active list.
  */
@@ -97,8 +103,7 @@ static void deferred_probe_work_func(struct work_struct *work)
 
 		get_device(dev);
 
-		kfree(dev->p->deferred_probe_reason);
-		dev->p->deferred_probe_reason = NULL;
+		__device_set_deferred_probe_reason(dev, NULL);
 
 		/*
 		 * Drop the mutex while probing each device; the probe path may
@@ -140,8 +145,7 @@ void driver_deferred_probe_del(struct device *dev)
 	if (!list_empty(&dev->p->deferred_probe)) {
 		dev_dbg(dev, "Removed from deferred list\n");
 		list_del_init(&dev->p->deferred_probe);
-		kfree(dev->p->deferred_probe_reason);
-		dev->p->deferred_probe_reason = NULL;
+		__device_set_deferred_probe_reason(dev, NULL);
 	}
 	mutex_unlock(&deferred_probe_mutex);
 }
@@ -220,11 +224,12 @@ void device_unblock_probing(void)
 void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf)
 {
 	const char *drv = dev_driver_string(dev);
+	char *reason;
 
Quoted 7 lines

drivers/base/dd.c void device_unblock_probing(void) · line 231

Quoted 5 lines
drivers/base/dd.c +1 -2
 	mutex_lock(&deferred_probe_mutex);
 
-	kfree(dev->p->deferred_probe_reason);
-	dev->p->deferred_probe_reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);
+	reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);

No error checking?

There wasn't any before and I think silent failure to set the defer reason is completely adequate behavior.

Cheers,
Ahmad

Quoted 4 lines

:(

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
Reviewed-by on v2 1/2
#10 lore raw
In reply to Ahmad Fatoum

Hi Ahmad,

W dniu 19.03.2021 o 12:04, Ahmad Fatoum pisze:

Quoted 10 lines

When retrying a deferred probe, any old defer reason string should be discarded. Otherwise, if the probe is deferred again at a different spot, but without setting a message, the now incorrect probe reason will remain.

This was observed with the i.MX I2C driver, which ultimately failed to probe due to lack of the GPIO driver. The probe defer for GPIO doesn't record a message, but a previous probe defer to clock_get did. This had the effect that /sys/kernel/debug/devices_deferred listed a misleading probe deferral reason.

Quoted 4 lines
Fixes d090b70ede02 ("driver core: add deferring probe reason to devices_deferred property")

Regards

Andrzej

Quoted 22 lines

---
v1 -> v2:
- reworded commit message (Andy)
- collected Andy's Reviewed-by

---
  drivers/base/dd.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/base/dd.c +3 -0
index 9179825ff646..e2cf3b29123e 100644
@@ -97,6 +97,9 @@ static void deferred_probe_work_func(struct work_struct *work)
 
  		get_device(dev);
 
+		kfree(dev->p->deferred_probe_reason);
+		dev->p->deferred_probe_reason = NULL;
+
  		/*
  		 * Drop the mutex while probing each device; the probe path may
  		 * manipulate the deferred list