Re: [PATCH v2 1/5] driver core: add a faux bus for use when a simple device/bus is needed

Zijun Hu
2025-02-06T15:35:10Z

On 2/4/2025 7:09 PM, Greg Kroah-Hartman wrote:
> +#define MAX_NAME_SIZE 256 /* Max size of a faux_device name */
> +
> +/*
> + * Internal wrapper structure so we can hold the memory
> + * for the driver and the name string of the faux device.
> + */
> +struct faux_object {
> + struct faux_device faux_dev;
> + const struct faux_driver_ops *faux_ops;
> + char name[];

Remove name since it is not used actually ?

> +};+ */
> +void faux_device_destroy(struct faux_device *faux_dev)
> +{
> + struct device *dev = &faux_dev->dev;
> +
> + if (IS_ERR_OR_NULL(faux_dev))
> + return;
> +

struct device *dev;

//faux_device_create() does not return ERR_PTR().
if (!faux_dev)
return;

// avoid NULL pointer dereference in case of above error
dev = &faux_dev->dev;

> + device_del(dev);
> +
> + /* The final put_device() will clean up the driver we created for this device. */
> + put_device(dev);

use device_unregister() instead of above 2 statements?

> +}
> +EXPORT_SYMBOL_GPL(faux_device_destroy);
> +
> +int __init faux_bus_init(void)
> +{
> + int ret;
> +
> + ret = device_register(&faux_bus_root);
> + if (ret) {
> + put_device(&faux_bus_root);

put_device() for static device may trigger below warning:

drivers/base/core.c:device_release():
WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is
broken and must be fixed. See Documentation/core-api/kobject.rst.\n",
dev_name(dev));
> + return ret;
> + }
> +
> + ret = bus_register(&faux_bus_type);
> + if (ret)
> + goto error_bus;
> +

> + ret = driver_register(&faux_driver);
> + if (ret)
> + goto error_driver;
> +
> + return ret;
> +
> +error_driver:
> + bus_unregister(&faux_bus_type);
> +
> +error_bus:
> + device_unregister(&faux_bus_root);
> + return ret;
> +}