2024-03-08 12:16:42

by Ricardo B. Marliere

[permalink] [raw]
Subject: [PATCH v2] bcachefs: chardev: make bch_chardev_class constant

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 bch_chardev_class structure to be declared at build
time placing it into read-only memory, instead of having to be dynamically
allocated at boot time. Also, correctly clean up after failing paths in
bch2_chardev_init().

Cc: Hongbo Li <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Suggested-by: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Ricardo B. Marliere <[email protected]>
---
Changes in v2:
- Used "free the last thing" pattern in bch2_chardev_init().
- Link to v1: https://lore.kernel.org/r/[email protected]
---
fs/bcachefs/chardev.c | 35 ++++++++++++++++++++++-------------
1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/fs/bcachefs/chardev.c b/fs/bcachefs/chardev.c
index 226b39c17667..dc09f547dae6 100644
--- a/fs/bcachefs/chardev.c
+++ b/fs/bcachefs/chardev.c
@@ -940,7 +940,9 @@ static const struct file_operations bch_chardev_fops = {
};

static int bch_chardev_major;
-static struct class *bch_chardev_class;
+static const struct class bch_chardev_class = {
+ .name = "bcachefs",
+};
static struct device *bch_chardev;

void bch2_fs_chardev_exit(struct bch_fs *c)
@@ -957,7 +959,7 @@ int bch2_fs_chardev_init(struct bch_fs *c)
if (c->minor < 0)
return c->minor;

- c->chardev = device_create(bch_chardev_class, NULL,
+ c->chardev = device_create(&bch_chardev_class, NULL,
MKDEV(bch_chardev_major, c->minor), c,
"bcachefs%u-ctl", c->minor);
if (IS_ERR(c->chardev))
@@ -968,32 +970,39 @@ int bch2_fs_chardev_init(struct bch_fs *c)

void bch2_chardev_exit(void)
{
- if (!IS_ERR_OR_NULL(bch_chardev_class))
- device_destroy(bch_chardev_class,
- MKDEV(bch_chardev_major, U8_MAX));
- if (!IS_ERR_OR_NULL(bch_chardev_class))
- class_destroy(bch_chardev_class);
+ device_destroy(&bch_chardev_class, MKDEV(bch_chardev_major, U8_MAX));
+ class_unregister(&bch_chardev_class);
if (bch_chardev_major > 0)
unregister_chrdev(bch_chardev_major, "bcachefs");
}

int __init bch2_chardev_init(void)
{
+ int ret;
+
bch_chardev_major = register_chrdev(0, "bcachefs-ctl", &bch_chardev_fops);
if (bch_chardev_major < 0)
return bch_chardev_major;

- bch_chardev_class = class_create("bcachefs");
- if (IS_ERR(bch_chardev_class))
- return PTR_ERR(bch_chardev_class);
+ ret = class_register(&bch_chardev_class);
+ if (ret)
+ goto major_out;

- bch_chardev = device_create(bch_chardev_class, NULL,
+ bch_chardev = device_create(&bch_chardev_class, NULL,
MKDEV(bch_chardev_major, U8_MAX),
NULL, "bcachefs-ctl");
- if (IS_ERR(bch_chardev))
- return PTR_ERR(bch_chardev);
+ if (IS_ERR(bch_chardev)) {
+ ret = PTR_ERR(bch_chardev);
+ goto class_out;
+ }

return 0;
+
+class_out:
+ class_unregister(&bch_chardev_class);
+major_out:
+ unregister_chrdev(bch_chardev_major, "bcachefs-ctl");
+ return ret;
}

#endif /* NO_BCACHEFS_CHARDEV */

---
base-commit: 90d35da658da8cff0d4ecbb5113f5fac9d00eb72
change-id: 20240305-bcachefs-27a4bb8b9f4f

Best regards,
--
Ricardo B. Marliere <[email protected]>



2024-03-11 01:42:18

by Hongbo Li

[permalink] [raw]
Subject: Re: [PATCH v2] bcachefs: chardev: make bch_chardev_class constant

It's fine for me.

On 2024/3/8 20:12, 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 bch_chardev_class structure to be declared at build
> time placing it into read-only memory, instead of having to be dynamically
> allocated at boot time. Also, correctly clean up after failing paths in
> bch2_chardev_init().
>
> Cc: Hongbo Li <[email protected]>
> Cc: Greg Kroah-Hartman <[email protected]>
> Suggested-by: Greg Kroah-Hartman <[email protected]>
> Signed-off-by: Ricardo B. Marliere <[email protected]>
> ---
> Changes in v2:
> - Used "free the last thing" pattern in bch2_chardev_init().
> - Link to v1: https://lore.kernel.org/r/[email protected]
> ---
> fs/bcachefs/chardev.c | 35 ++++++++++++++++++++++-------------
> 1 file changed, 22 insertions(+), 13 deletions(-)
>
> diff --git a/fs/bcachefs/chardev.c b/fs/bcachefs/chardev.c
> index 226b39c17667..dc09f547dae6 100644
> --- a/fs/bcachefs/chardev.c
> +++ b/fs/bcachefs/chardev.c
> @@ -940,7 +940,9 @@ static const struct file_operations bch_chardev_fops = {
> };
>
> static int bch_chardev_major;
> -static struct class *bch_chardev_class;
> +static const struct class bch_chardev_class = {
> + .name = "bcachefs",
> +};
> static struct device *bch_chardev;
>
> void bch2_fs_chardev_exit(struct bch_fs *c)
> @@ -957,7 +959,7 @@ int bch2_fs_chardev_init(struct bch_fs *c)
> if (c->minor < 0)
> return c->minor;
>
> - c->chardev = device_create(bch_chardev_class, NULL,
> + c->chardev = device_create(&bch_chardev_class, NULL,
> MKDEV(bch_chardev_major, c->minor), c,
> "bcachefs%u-ctl", c->minor);
> if (IS_ERR(c->chardev))
> @@ -968,32 +970,39 @@ int bch2_fs_chardev_init(struct bch_fs *c)
>
> void bch2_chardev_exit(void)
> {
> - if (!IS_ERR_OR_NULL(bch_chardev_class))
> - device_destroy(bch_chardev_class,
> - MKDEV(bch_chardev_major, U8_MAX));
> - if (!IS_ERR_OR_NULL(bch_chardev_class))
> - class_destroy(bch_chardev_class);
> + device_destroy(&bch_chardev_class, MKDEV(bch_chardev_major, U8_MAX));
> + class_unregister(&bch_chardev_class);
> if (bch_chardev_major > 0)
> unregister_chrdev(bch_chardev_major, "bcachefs");
> }
>
> int __init bch2_chardev_init(void)
> {
> + int ret;
> +
> bch_chardev_major = register_chrdev(0, "bcachefs-ctl", &bch_chardev_fops);
> if (bch_chardev_major < 0)
> return bch_chardev_major;
>
> - bch_chardev_class = class_create("bcachefs");
> - if (IS_ERR(bch_chardev_class))
> - return PTR_ERR(bch_chardev_class);
> + ret = class_register(&bch_chardev_class);
> + if (ret)
> + goto major_out;
>
> - bch_chardev = device_create(bch_chardev_class, NULL,
> + bch_chardev = device_create(&bch_chardev_class, NULL,
> MKDEV(bch_chardev_major, U8_MAX),
> NULL, "bcachefs-ctl");
> - if (IS_ERR(bch_chardev))
> - return PTR_ERR(bch_chardev);
> + if (IS_ERR(bch_chardev)) {
> + ret = PTR_ERR(bch_chardev);
> + goto class_out;
> + }
>
> return 0;
> +
> +class_out:
> + class_unregister(&bch_chardev_class);
> +major_out:
> + unregister_chrdev(bch_chardev_major, "bcachefs-ctl");
> + return ret;
> }
>
> #endif /* NO_BCACHEFS_CHARDEV */
>
> ---
> base-commit: 90d35da658da8cff0d4ecbb5113f5fac9d00eb72
> change-id: 20240305-bcachefs-27a4bb8b9f4f
>
> Best regards,

2024-04-12 02:31:28

by Hongbo Li

[permalink] [raw]
Subject: Re: [PATCH v2] bcachefs: chardev: make bch_chardev_class constant

On 2024/3/11 9:41, Hongbo Li wrote:
> It's fine for me.
>
> On 2024/3/8 20:12, 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 bch_chardev_class structure to be declared at build
>> time placing it into read-only memory, instead of having to be
>> dynamically
>> allocated at boot time. Also, correctly clean up after failing paths in
>> bch2_chardev_init().
>>
>> Cc: Hongbo Li <[email protected]>
>> Cc: Greg Kroah-Hartman <[email protected]>
>> Suggested-by: Greg Kroah-Hartman <[email protected]>
>> Signed-off-by: Ricardo B. Marliere <[email protected]>
>> ---
>> Changes in v2:
>> - Used "free the last thing" pattern in bch2_chardev_init().
>> - Link to v1:
>> https://lore.kernel.org/r/[email protected]
>> ---
>>   fs/bcachefs/chardev.c | 35 ++++++++++++++++++++++-------------
>>   1 file changed, 22 insertions(+), 13 deletions(-)
>>
>> diff --git a/fs/bcachefs/chardev.c b/fs/bcachefs/chardev.c
>> index 226b39c17667..dc09f547dae6 100644
>> --- a/fs/bcachefs/chardev.c
>> +++ b/fs/bcachefs/chardev.c
>> @@ -940,7 +940,9 @@ static const struct file_operations
>> bch_chardev_fops = {
>>   };
>>   static int bch_chardev_major;
>> -static struct class *bch_chardev_class;
>> +static const struct class bch_chardev_class = {
>> +    .name = "bcachefs",
>> +};
>>   static struct device *bch_chardev;
>>   void bch2_fs_chardev_exit(struct bch_fs *c)
>> @@ -957,7 +959,7 @@ int bch2_fs_chardev_init(struct bch_fs *c)
>>       if (c->minor < 0)
>>           return c->minor;
>> -    c->chardev = device_create(bch_chardev_class, NULL,
>> +    c->chardev = device_create(&bch_chardev_class, NULL,
>>                      MKDEV(bch_chardev_major, c->minor), c,
>>                      "bcachefs%u-ctl", c->minor);
>>       if (IS_ERR(c->chardev))
>> @@ -968,32 +970,39 @@ int bch2_fs_chardev_init(struct bch_fs *c)
>>   void bch2_chardev_exit(void)
>>   {
>> -    if (!IS_ERR_OR_NULL(bch_chardev_class))
>> -        device_destroy(bch_chardev_class,
>> -                   MKDEV(bch_chardev_major, U8_MAX));
>> -    if (!IS_ERR_OR_NULL(bch_chardev_class))
>> -        class_destroy(bch_chardev_class);
>> +    device_destroy(&bch_chardev_class, MKDEV(bch_chardev_major,
>> U8_MAX));
>> +    class_unregister(&bch_chardev_class);
>>       if (bch_chardev_major > 0)
>>           unregister_chrdev(bch_chardev_major, "bcachefs");
>>   }
>>   int __init bch2_chardev_init(void)
>>   {
>> +    int ret;
>> +
>>       bch_chardev_major = register_chrdev(0, "bcachefs-ctl",
>> &bch_chardev_fops);
>>       if (bch_chardev_major < 0)
>>           return bch_chardev_major;
>> -    bch_chardev_class = class_create("bcachefs");
>> -    if (IS_ERR(bch_chardev_class))
>> -        return PTR_ERR(bch_chardev_class);
>> +    ret = class_register(&bch_chardev_class);
>> +    if (ret)
>> +        goto major_out;
>> -    bch_chardev = device_create(bch_chardev_class, NULL,
>> +    bch_chardev = device_create(&bch_chardev_class, NULL,
>>                       MKDEV(bch_chardev_major, U8_MAX),
>>                       NULL, "bcachefs-ctl");
>> -    if (IS_ERR(bch_chardev))
>> -        return PTR_ERR(bch_chardev);
>> +    if (IS_ERR(bch_chardev)) {
>> +        ret = PTR_ERR(bch_chardev);
>> +        goto class_out;
>> +    }
>>       return 0;
>> +
>> +class_out:
>> +    class_unregister(&bch_chardev_class);
>> +major_out:
>> +    unregister_chrdev(bch_chardev_major, "bcachefs-ctl");
>> +    return ret;
>>   }
>>   #endif /* NO_BCACHEFS_CHARDEV */
>>
>> ---
>> base-commit: 90d35da658da8cff0d4ecbb5113f5fac9d00eb72
>> change-id: 20240305-bcachefs-27a4bb8b9f4f
>>
>> Best regards,
>

This is a useful patch, and also works on latest code. Maybe we almost
forgot it.

Reviewed-by: Hongbo Li <[email protected]>

2024-04-12 02:44:56

by Kent Overstreet

[permalink] [raw]
Subject: Re: [PATCH v2] bcachefs: chardev: make bch_chardev_class constant

On Fri, Mar 08, 2024 at 09:12:47AM -0300, 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 bch_chardev_class structure to be declared at build
> time placing it into read-only memory, instead of having to be dynamically
> allocated at boot time. Also, correctly clean up after failing paths in
> bch2_chardev_init().
>
> Cc: Hongbo Li <[email protected]>
> Cc: Greg Kroah-Hartman <[email protected]>
> Suggested-by: Greg Kroah-Hartman <[email protected]>
> Signed-off-by: Ricardo B. Marliere <[email protected]>

Thanks, applied
> ---
> Changes in v2:
> - Used "free the last thing" pattern in bch2_chardev_init().
> - Link to v1: https://lore.kernel.org/r/[email protected]
> ---
> fs/bcachefs/chardev.c | 35 ++++++++++++++++++++++-------------
> 1 file changed, 22 insertions(+), 13 deletions(-)
>
> diff --git a/fs/bcachefs/chardev.c b/fs/bcachefs/chardev.c
> index 226b39c17667..dc09f547dae6 100644
> --- a/fs/bcachefs/chardev.c
> +++ b/fs/bcachefs/chardev.c
> @@ -940,7 +940,9 @@ static const struct file_operations bch_chardev_fops = {
> };
>
> static int bch_chardev_major;
> -static struct class *bch_chardev_class;
> +static const struct class bch_chardev_class = {
> + .name = "bcachefs",
> +};
> static struct device *bch_chardev;
>
> void bch2_fs_chardev_exit(struct bch_fs *c)
> @@ -957,7 +959,7 @@ int bch2_fs_chardev_init(struct bch_fs *c)
> if (c->minor < 0)
> return c->minor;
>
> - c->chardev = device_create(bch_chardev_class, NULL,
> + c->chardev = device_create(&bch_chardev_class, NULL,
> MKDEV(bch_chardev_major, c->minor), c,
> "bcachefs%u-ctl", c->minor);
> if (IS_ERR(c->chardev))
> @@ -968,32 +970,39 @@ int bch2_fs_chardev_init(struct bch_fs *c)
>
> void bch2_chardev_exit(void)
> {
> - if (!IS_ERR_OR_NULL(bch_chardev_class))
> - device_destroy(bch_chardev_class,
> - MKDEV(bch_chardev_major, U8_MAX));
> - if (!IS_ERR_OR_NULL(bch_chardev_class))
> - class_destroy(bch_chardev_class);
> + device_destroy(&bch_chardev_class, MKDEV(bch_chardev_major, U8_MAX));
> + class_unregister(&bch_chardev_class);
> if (bch_chardev_major > 0)
> unregister_chrdev(bch_chardev_major, "bcachefs");
> }
>
> int __init bch2_chardev_init(void)
> {
> + int ret;
> +
> bch_chardev_major = register_chrdev(0, "bcachefs-ctl", &bch_chardev_fops);
> if (bch_chardev_major < 0)
> return bch_chardev_major;
>
> - bch_chardev_class = class_create("bcachefs");
> - if (IS_ERR(bch_chardev_class))
> - return PTR_ERR(bch_chardev_class);
> + ret = class_register(&bch_chardev_class);
> + if (ret)
> + goto major_out;
>
> - bch_chardev = device_create(bch_chardev_class, NULL,
> + bch_chardev = device_create(&bch_chardev_class, NULL,
> MKDEV(bch_chardev_major, U8_MAX),
> NULL, "bcachefs-ctl");
> - if (IS_ERR(bch_chardev))
> - return PTR_ERR(bch_chardev);
> + if (IS_ERR(bch_chardev)) {
> + ret = PTR_ERR(bch_chardev);
> + goto class_out;
> + }
>
> return 0;
> +
> +class_out:
> + class_unregister(&bch_chardev_class);
> +major_out:
> + unregister_chrdev(bch_chardev_major, "bcachefs-ctl");
> + return ret;
> }
>
> #endif /* NO_BCACHEFS_CHARDEV */
>
> ---
> base-commit: 90d35da658da8cff0d4ecbb5113f5fac9d00eb72
> change-id: 20240305-bcachefs-27a4bb8b9f4f
>
> Best regards,
> --
> Ricardo B. Marliere <[email protected]>
>