2024-01-05 13:45:36

by Christophe JAILLET

[permalink] [raw]
Subject: [PATCH] thermal: core: Save a few bytes of memory when registering a cooling device

Some *thermal_cooling_device_register() calls pass a string literal as the
'type' parameter.

So kstrdup_const() can be used instead of kfree() to avoid a memory
allocation in such cases.

Signed-off-by: Christophe JAILLET <[email protected]>
---
drivers/thermal/thermal_core.c | 6 +++---
include/linux/thermal.h | 2 +-
2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index fa88d8707241..d21225ddbf10 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -846,7 +846,7 @@ static void thermal_release(struct device *dev)
sizeof("cooling_device") - 1)) {
cdev = to_cooling_device(dev);
thermal_cooling_device_destroy_sysfs(cdev);
- kfree(cdev->type);
+ kfree_const(cdev->type);
ida_free(&thermal_cdev_ida, cdev->id);
kfree(cdev);
}
@@ -918,7 +918,7 @@ __thermal_cooling_device_register(struct device_node *np,
cdev->id = ret;
id = ret;

- cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
+ cdev->type = kstrdup_const(type ? type : "", GFP_KERNEL);
if (!cdev->type) {
ret = -ENOMEM;
goto out_ida_remove;
@@ -969,7 +969,7 @@ __thermal_cooling_device_register(struct device_node *np,
out_cooling_dev:
thermal_cooling_device_destroy_sysfs(cdev);
out_cdev_type:
- kfree(cdev->type);
+ kfree_const(cdev->type);
out_ida_remove:
ida_free(&thermal_cdev_ida, id);
out_kfree_cdev:
diff --git a/include/linux/thermal.h b/include/linux/thermal.h
index bf84595a4e86..052c72c0fa17 100644
--- a/include/linux/thermal.h
+++ b/include/linux/thermal.h
@@ -102,7 +102,7 @@ struct thermal_cooling_device_ops {

struct thermal_cooling_device {
int id;
- char *type;
+ const char *type;
unsigned long max_state;
struct device device;
struct device_node *np;
--
2.34.1



2024-01-05 13:50:56

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [PATCH] thermal: core: Save a few bytes of memory when registering a cooling device

On Fri, Jan 5, 2024 at 2:45 PM Christophe JAILLET
<[email protected]> wrote:
>
> Some *thermal_cooling_device_register() calls pass a string literal as the
> 'type' parameter.
>
> So kstrdup_const() can be used instead of kfree() to avoid a memory

I guess you mean kstrdup()?

> allocation in such cases.
>
> Signed-off-by: Christophe JAILLET <[email protected]>
> ---
> drivers/thermal/thermal_core.c | 6 +++---
> include/linux/thermal.h | 2 +-
> 2 files changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
> index fa88d8707241..d21225ddbf10 100644
> --- a/drivers/thermal/thermal_core.c
> +++ b/drivers/thermal/thermal_core.c
> @@ -846,7 +846,7 @@ static void thermal_release(struct device *dev)
> sizeof("cooling_device") - 1)) {
> cdev = to_cooling_device(dev);
> thermal_cooling_device_destroy_sysfs(cdev);
> - kfree(cdev->type);
> + kfree_const(cdev->type);
> ida_free(&thermal_cdev_ida, cdev->id);
> kfree(cdev);
> }
> @@ -918,7 +918,7 @@ __thermal_cooling_device_register(struct device_node *np,
> cdev->id = ret;
> id = ret;
>
> - cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
> + cdev->type = kstrdup_const(type ? type : "", GFP_KERNEL);
> if (!cdev->type) {
> ret = -ENOMEM;
> goto out_ida_remove;
> @@ -969,7 +969,7 @@ __thermal_cooling_device_register(struct device_node *np,
> out_cooling_dev:
> thermal_cooling_device_destroy_sysfs(cdev);
> out_cdev_type:
> - kfree(cdev->type);
> + kfree_const(cdev->type);
> out_ida_remove:
> ida_free(&thermal_cdev_ida, id);
> out_kfree_cdev:
> diff --git a/include/linux/thermal.h b/include/linux/thermal.h
> index bf84595a4e86..052c72c0fa17 100644
> --- a/include/linux/thermal.h
> +++ b/include/linux/thermal.h
> @@ -102,7 +102,7 @@ struct thermal_cooling_device_ops {
>
> struct thermal_cooling_device {
> int id;
> - char *type;
> + const char *type;
> unsigned long max_state;
> struct device device;
> struct device_node *np;
> --
> 2.34.1
>

2024-01-05 14:56:22

by Christophe JAILLET

[permalink] [raw]
Subject: Re: [PATCH] thermal: core: Save a few bytes of memory when registering a cooling device

Le 05/01/2024 à 14:50, Rafael J. Wysocki a écrit :
> On Fri, Jan 5, 2024 at 2:45 PM Christophe JAILLET
> <[email protected]> wrote:
>>
>> Some *thermal_cooling_device_register() calls pass a string literal as the
>> 'type' parameter.
>>
>> So kstrdup_const() can be used instead of kfree() to avoid a memory
>
> I guess you mean kstrdup()?

Sure.
Stupid cut'n'paste typo. Thx for catching it.

Should I resend, or can it be fixed if/when the patch is applied?

CJ

>
>> allocation in such cases.
>>
>> Signed-off-by: Christophe JAILLET <[email protected]>
>> ---
>> drivers/thermal/thermal_core.c | 6 +++---
>> include/linux/thermal.h | 2 +-
>> 2 files changed, 4 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
>> index fa88d8707241..d21225ddbf10 100644
>> --- a/drivers/thermal/thermal_core.c
>> +++ b/drivers/thermal/thermal_core.c
>> @@ -846,7 +846,7 @@ static void thermal_release(struct device *dev)
>> sizeof("cooling_device") - 1)) {
>> cdev = to_cooling_device(dev);
>> thermal_cooling_device_destroy_sysfs(cdev);
>> - kfree(cdev->type);
>> + kfree_const(cdev->type);
>> ida_free(&thermal_cdev_ida, cdev->id);
>> kfree(cdev);
>> }
>> @@ -918,7 +918,7 @@ __thermal_cooling_device_register(struct device_node *np,
>> cdev->id = ret;
>> id = ret;
>>
>> - cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
>> + cdev->type = kstrdup_const(type ? type : "", GFP_KERNEL);
>> if (!cdev->type) {
>> ret = -ENOMEM;
>> goto out_ida_remove;
>> @@ -969,7 +969,7 @@ __thermal_cooling_device_register(struct device_node *np,
>> out_cooling_dev:
>> thermal_cooling_device_destroy_sysfs(cdev);
>> out_cdev_type:
>> - kfree(cdev->type);
>> + kfree_const(cdev->type);
>> out_ida_remove:
>> ida_free(&thermal_cdev_ida, id);
>> out_kfree_cdev:
>> diff --git a/include/linux/thermal.h b/include/linux/thermal.h
>> index bf84595a4e86..052c72c0fa17 100644
>> --- a/include/linux/thermal.h
>> +++ b/include/linux/thermal.h
>> @@ -102,7 +102,7 @@ struct thermal_cooling_device_ops {
>>
>> struct thermal_cooling_device {
>> int id;
>> - char *type;
>> + const char *type;
>> unsigned long max_state;
>> struct device device;
>> struct device_node *np;
>> --
>> 2.34.1
>>
>
>


2024-01-05 15:48:27

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [PATCH] thermal: core: Save a few bytes of memory when registering a cooling device

On Fri, Jan 5, 2024 at 3:55 PM Christophe JAILLET
<[email protected]> wrote:
>
> Le 05/01/2024 à 14:50, Rafael J. Wysocki a écrit :
> > On Fri, Jan 5, 2024 at 2:45 PM Christophe JAILLET
> > <[email protected]> wrote:
> >>
> >> Some *thermal_cooling_device_register() calls pass a string literal as the
> >> 'type' parameter.
> >>
> >> So kstrdup_const() can be used instead of kfree() to avoid a memory
> >
> > I guess you mean kstrdup()?
>
> Sure.
> Stupid cut'n'paste typo. Thx for catching it.
>
> Should I resend, or can it be fixed if/when the patch is applied?

I can fix it up.

2024-01-09 13:50:51

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [PATCH] thermal: core: Save a few bytes of memory when registering a cooling device

On Fri, Jan 5, 2024 at 2:45 PM Christophe JAILLET
<[email protected]> wrote:
>
> Some *thermal_cooling_device_register() calls pass a string literal as the
> 'type' parameter.
>
> So kstrdup_const() can be used instead of kfree() to avoid a memory
> allocation in such cases.
>
> Signed-off-by: Christophe JAILLET <[email protected]>
> ---
> drivers/thermal/thermal_core.c | 6 +++---
> include/linux/thermal.h | 2 +-
> 2 files changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
> index fa88d8707241..d21225ddbf10 100644
> --- a/drivers/thermal/thermal_core.c
> +++ b/drivers/thermal/thermal_core.c
> @@ -846,7 +846,7 @@ static void thermal_release(struct device *dev)
> sizeof("cooling_device") - 1)) {
> cdev = to_cooling_device(dev);
> thermal_cooling_device_destroy_sysfs(cdev);
> - kfree(cdev->type);
> + kfree_const(cdev->type);
> ida_free(&thermal_cdev_ida, cdev->id);
> kfree(cdev);
> }
> @@ -918,7 +918,7 @@ __thermal_cooling_device_register(struct device_node *np,
> cdev->id = ret;
> id = ret;
>
> - cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
> + cdev->type = kstrdup_const(type ? type : "", GFP_KERNEL);
> if (!cdev->type) {
> ret = -ENOMEM;
> goto out_ida_remove;
> @@ -969,7 +969,7 @@ __thermal_cooling_device_register(struct device_node *np,
> out_cooling_dev:
> thermal_cooling_device_destroy_sysfs(cdev);
> out_cdev_type:
> - kfree(cdev->type);
> + kfree_const(cdev->type);
> out_ida_remove:
> ida_free(&thermal_cdev_ida, id);
> out_kfree_cdev:
> diff --git a/include/linux/thermal.h b/include/linux/thermal.h
> index bf84595a4e86..052c72c0fa17 100644
> --- a/include/linux/thermal.h
> +++ b/include/linux/thermal.h
> @@ -102,7 +102,7 @@ struct thermal_cooling_device_ops {
>
> struct thermal_cooling_device {
> int id;
> - char *type;
> + const char *type;
> unsigned long max_state;
> struct device device;
> struct device_node *np;
> --

Applied as 6.8-rc1 material under a different subject and with some
changelog edits.

Thanks!