Re: [PATCH 1/2] media: dvbdev: make dvb_class constant

From: Hans Verkuil
Date: Mon Apr 08 2024 - 05:47:58 EST


On 05/03/2024 14:26, Ricardo B. Marliere wrote:
> Since commit 43a7206b0963 ("driver core: class: make class_register() take
> a const *"), the driver core allows for struct class to be in read-only
> memory, so move the dvb_class structure to be declared at build time
> placing it into read-only memory, instead of having to be dynamically
> allocated at boot time.
>
> Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
> Suggested-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
> Signed-off-by: Ricardo B. Marliere <ricardo@xxxxxxxxxxxx>
> ---
> drivers/media/dvb-core/dvbdev.c | 23 +++++++++++++----------
> 1 file changed, 13 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/media/dvb-core/dvbdev.c b/drivers/media/dvb-core/dvbdev.c
> index 733d0bc4b4cc..dcbf68b00240 100644
> --- a/drivers/media/dvb-core/dvbdev.c
> +++ b/drivers/media/dvb-core/dvbdev.c
> @@ -78,7 +78,13 @@ static const u8 minor_type[] = {
> #define MAX_DVB_MINORS (DVB_MAX_ADAPTERS * 64)
> #endif
>
> -static struct class *dvb_class;
> +static int dvb_uevent(const struct device *dev, struct kobj_uevent_env *env);
> +static char *dvb_devnode(const struct device *dev, umode_t *mode);

Forward references are typically something you want to avoid.

Looking at the code, I think it makes sense to just move those two functions
to just before this dvb_class.

> +static const struct class dvb_class = {
> + .name = "dvb",
> + .dev_uevent = dvb_uevent,
> + .devnode = dvb_devnode,
> +};
>
> static struct dvb_device *dvb_minors[MAX_DVB_MINORS];
> static DECLARE_RWSEM(minor_rwsem);

Also move the dvb_class (+ the two functions) to after this line. I think that's
a more suitable place for this.

> @@ -561,7 +567,7 @@ int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
> return ret;
> }
>
> - clsdev = device_create(dvb_class, adap->device,
> + clsdev = device_create(&dvb_class, adap->device,
> MKDEV(DVB_MAJOR, minor),
> dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
> if (IS_ERR(clsdev)) {
> @@ -600,7 +606,7 @@ void dvb_remove_device(struct dvb_device *dvbdev)
>
> dvb_media_device_free(dvbdev);
>
> - device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
> + device_destroy(&dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
>
> list_del(&dvbdev->list_head);
> }
> @@ -1096,13 +1102,10 @@ static int __init init_dvbdev(void)
> goto error;
> }
>
> - dvb_class = class_create("dvb");
> - if (IS_ERR(dvb_class)) {
> - retval = PTR_ERR(dvb_class);
> + retval = class_register(&dvb_class);
> + if (retval != 0)

This can just be 'if (retval)'.

> goto error;
> - }
> - dvb_class->dev_uevent = dvb_uevent;
> - dvb_class->devnode = dvb_devnode;
> +
> return 0;
>
> error:
> @@ -1115,7 +1118,7 @@ static void __exit exit_dvbdev(void)
> {
> struct dvbdevfops_node *node, *next;
>
> - class_destroy(dvb_class);
> + class_unregister(&dvb_class);
> cdev_del(&dvb_device_cdev);
> unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
>
>

Regards,

Hans