Skip to contentSkip to conversation

driver core: faux: only create the device if probe() succeeds

PATCH In review Greg Kroah-Hartman 3 participants 4 messages Feb 25, 2025 – Feb 26, 2025

Subsystems (2)
lore
Share & tools
lore

Greg Kroah-Hartman

author maintainer
#0 lore raw

PATCH 1/1 driver core: faux: only create the device if probe() succeeds

It's really hard to know if a faux device properly passes the callback to probe() without having to poke around in the faux_device structure and then clean up. Instead of having to have every user of the api do this logic, just do it in the faux device core itself.

This makes the use of a custom probe() callback for a faux device much simpler overall.

Cc "Rafael J. Wysocki" <[email protected]>
Cc Danilo Krummrich <[email protected]>
---
 drivers/base/faux.c |   15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/base/faux.c +14 -1
index 531e9d789ee0..407c1d1aad50 100644
@@ -102,7 +102,9 @@ static void faux_device_release(struct device *dev)
  *
  * Note, when this function is called, the functions specified in struct
  * faux_ops can be called before the function returns, so be prepared for
- * everything to be properly initialized before that point in time.
+ * everything to be properly initialized before that point in time.  If the
+ * probe callback (if one is present) does NOT succeed, the creation of the
+ * device will fail and NULL will be returned.
  *
  * Return:
  * * NULL if an error happened with creating the device
@@ -147,6 +149,17 @@ struct faux_device *faux_device_create_with_groups(const char *name,
 		return NULL;
 	}
 
+	/*
+	 * Verify that we did bind the driver to the device (i.e. probe worked),
+	 * if not, let's fail the creation as trying to guess if probe was
+	 * successful is almost impossible to determine by the caller.
+	 */
+	if (!dev->driver) {
+		dev_err(dev, "probe did not succeed, tearing down the device\n");
+		faux_device_destroy(faux_dev);
+		faux_dev = NULL;
+	}
+
 	return faux_dev;
 }
 EXPORT_SYMBOL_GPL(faux_device_create_with_groups);
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 32 lines · Greg Kroah-Hartman

It's really hard to know if a faux device properly passes the callback to probe() without having to poke around in the faux_device structure and then clean up. Instead of having to have every user of the api do this logic, just do it in the faux device core itself.

This makes the use of a custom probe() callback for a faux device much simpler overall.

Cc "Rafael J. Wysocki" <[email protected]>
Cc Danilo Krummrich <[email protected]>
---
 drivers/base/faux.c |   15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/base/faux.c +3 -1
index 531e9d789ee0..407c1d1aad50 100644
@@ -102,7 +102,9 @@ static void faux_device_release(struct device *dev)
  *
  * Note, when this function is called, the functions specified in struct
  * faux_ops can be called before the function returns, so be prepared for
- * everything to be properly initialized before that point in time.
+ * everything to be properly initialized before that point in time.  If the
+ * probe callback (if one is present) does NOT succeed, the creation of the
+ * device will fail and NULL will be returned.
  *
  * Return:
  * * NULL if an error happened with creating the device

drivers/base/faux.c struct faux_device *faux_device_create_with_groups(const char *name, · line 157

Quoted 9 lines
drivers/base/faux.c +6 -0
@@ -147,6 +149,17 @@ struct faux_device *faux_device_create_with_groups(const char *name,
 		return NULL;
 	}
 
+	/*
+	 * Verify that we did bind the driver to the device (i.e. probe worked),
+	 * if not, let's fail the creation as trying to guess if probe was
+	 * successful is almost impossible to determine by the caller.
+	 */
+	if (!dev->driver) {

Seems like really_probe() cleans things up properly through device_unbind_cleanup(), such that dev->driver is guaranteed to be NULL on failure.

drivers/base/faux.c struct faux_device *faux_device_create_with_groups(const char *name, · line 160

Quoted 3 lines
drivers/base/faux.c +3 -0
+		dev_err(dev, "probe did not succeed, tearing down the device\n");
+		faux_device_destroy(faux_dev);
+		faux_dev = NULL;

NIT: Maybe return NULL directly (like above) in case a subsequent change adds more code to the end of this function and does not consider that we fall through.

Either way,

Quoted 5 lines
drivers/base/faux.c +2 -0
+	}
+
 	return faux_dev;
 }
 EXPORT_SYMBOL_GPL(faux_device_create_with_groups);
author maintainer
#2 lore raw
Quoted 36 lines · Danilo Krummrich
Quoted 35 lines · Greg Kroah-Hartman

It's really hard to know if a faux device properly passes the callback to probe() without having to poke around in the faux_device structure and then clean up. Instead of having to have every user of the api do this logic, just do it in the faux device core itself.

This makes the use of a custom probe() callback for a faux device much simpler overall.

Cc "Rafael J. Wysocki" <[email protected]>
Cc Danilo Krummrich <[email protected]>
---
 drivers/base/faux.c |   15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/base/faux.c +3 -1
index 531e9d789ee0..407c1d1aad50 100644
@@ -102,7 +102,9 @@ static void faux_device_release(struct device *dev)
  *
  * Note, when this function is called, the functions specified in struct
  * faux_ops can be called before the function returns, so be prepared for
- * everything to be properly initialized before that point in time.
+ * everything to be properly initialized before that point in time.  If the
+ * probe callback (if one is present) does NOT succeed, the creation of the
+ * device will fail and NULL will be returned.
  *
  * Return:
  * * NULL if an error happened with creating the device
@@ -147,6 +149,17 @@ struct faux_device *faux_device_create_with_groups(const char *name,
 		return NULL;
 	}
 
Quoted 10 lines

drivers/base/faux.c struct faux_device *faux_device_create_with_groups(const char *name, · line 157

Quoted 6 lines
drivers/base/faux.c +6 -0
+	/*
+	 * Verify that we did bind the driver to the device (i.e. probe worked),
+	 * if not, let's fail the creation as trying to guess if probe was
+	 * successful is almost impossible to determine by the caller.
+	 */
+	if (!dev->driver) {

Seems like really_probe() cleans things up properly through device_unbind_cleanup(), such that dev->driver is guaranteed to be NULL on failure.

Yes.

Quoted 11 lines

drivers/base/faux.c struct faux_device *faux_device_create_with_groups(const char *name, · line 160

Quoted 3 lines
drivers/base/faux.c +3 -0
+		dev_err(dev, "probe did not succeed, tearing down the device\n");
+		faux_device_destroy(faux_dev);
+		faux_dev = NULL;

NIT: Maybe return NULL directly (like above) in case a subsequent change adds more code to the end of this function and does not consider that we fall through.

Either way,

Thanks for the review!

greg k-h

Reviewed-by on v1 1/1
#3 lore raw
In reply to Greg Kroah-Hartman
Quoted 5 lines · Greg Kroah-Hartman

It's really hard to know if a faux device properly passes the callback to probe() without having to poke around in the faux_device structure and then clean up. Instead of having to have every user of the api do this logic, just do it in the faux device core itself.

Quoted 7 lines

This makes the use of a custom probe() callback for a faux device much simpler overall.

Cc "Rafael J. Wysocki" <[email protected]>
Cc Danilo Krummrich <[email protected]>

Thanks for considering this!

Quoted 37 lines
---
 drivers/base/faux.c |   15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/base/faux.c +14 -1
index 531e9d789ee0..407c1d1aad50 100644
@@ -102,7 +102,9 @@ static void faux_device_release(struct device *dev)
  *
  * Note, when this function is called, the functions specified in struct
  * faux_ops can be called before the function returns, so be prepared for
- * everything to be properly initialized before that point in time.
+ * everything to be properly initialized before that point in time.  If the
+ * probe callback (if one is present) does NOT succeed, the creation of the
+ * device will fail and NULL will be returned.
  *
  * Return:
  * * NULL if an error happened with creating the device
@@ -147,6 +149,17 @@ struct faux_device *faux_device_create_with_groups(const char *name,
 		return NULL;
 	}
 
+	/*
+	 * Verify that we did bind the driver to the device (i.e. probe worked),
+	 * if not, let's fail the creation as trying to guess if probe was
+	 * successful is almost impossible to determine by the caller.
+	 */
+	if (!dev->driver) {
+		dev_err(dev, "probe did not succeed, tearing down the device\n");
+		faux_device_destroy(faux_dev);
+		faux_dev = NULL;
+	}
+
 	return faux_dev;
 }
 EXPORT_SYMBOL_GPL(faux_device_create_with_groups);
Signature · 1 line
 ~ Kurt