Re: [PATCH 2/7] drivers/hwmon: add local variable for newly allocated attribute_group**

From: Greg KH
Date: Mon Oct 09 2023 - 13:28:01 EST


On Mon, Oct 09, 2023 at 06:57:35PM +0200, Max Kellermann wrote:
> This allows the compiler to keep the pointer in a register and

Maybe, maybe not, there's no guarantee for register usage.

And it doesn't matter, this is a very slow path, no registers are
required :)

> prepares for making the struct field "const".

What struct field?


>
> Signed-off-by: Max Kellermann <max.kellermann@xxxxxxxxx>
> ---
> drivers/hwmon/hwmon.c | 11 ++++++-----
> 1 file changed, 6 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
> index c7dd3f5b2bd5..e50ab229b27d 100644
> --- a/drivers/hwmon/hwmon.c
> +++ b/drivers/hwmon/hwmon.c
> @@ -783,6 +783,7 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
> hdev = &hwdev->dev;
>
> if (chip) {
> + const struct attribute_group **new_groups;
> struct attribute **attrs;
> int ngroups = 2; /* terminating NULL plus &hwdev->groups */
>
> @@ -790,8 +791,8 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
> for (i = 0; groups[i]; i++)
> ngroups++;
>
> - hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
> - if (!hwdev->groups) {
> + hwdev->groups = new_groups = kcalloc(ngroups, sizeof(*new_groups), GFP_KERNEL);

So you have a const pointer AND a non-const pointer pointing to the same
thing?


> + if (!new_groups) {
> err = -ENOMEM;
> goto free_hwmon;
> }
> @@ -804,14 +805,14 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
>
> hwdev->group.attrs = attrs;
> ngroups = 0;
> - hwdev->groups[ngroups++] = &hwdev->group;
> + new_groups[ngroups++] = &hwdev->group;

This shoul be identical, you assign both above the same way, so why
change this?

>
> if (groups) {
> for (i = 0; groups[i]; i++)
> - hwdev->groups[ngroups++] = groups[i];
> + new_groups[ngroups++] = groups[i];

Same here.

thanks,

greg k-h