Skip to content

Commit

Permalink
driver core: faux: only create the device if probe() succeeds
Browse files Browse the repository at this point in the history
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.

Suggested-by: Kurt Borja <[email protected]>
Cc: Rafael J. Wysocki <[email protected]>
Reviewed-by: Kurt Borja <[email protected]>
Reviewed-by: Danilo Krummrich <[email protected]>
Link: https://lore.kernel.org/r/2025022545-unroasted-common-fa0e@gregkh
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
gregkh committed Feb 28, 2025
1 parent 95cb0cb commit 21b0dc5
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion drivers/base/faux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 21b0dc5

Please sign in to comment.