Skip to contentSkip to conversation

Consultation on extending hwmon channel attributes number to 64

PATCH lihuisong (C) 2 participants 3 messages Apr 27, 2025 – Apr 28, 2025 Hardware monitoring

lore
Share & tools
lore
#0 lore raw

Hi Guenter,

Currently, the implementation of hwmon core limits the maximum number of hwmon channel attributes is 32. The maximum number of used attributes in hwmon.h is 31(namely, hwmon_power_attributes) .
Actually, we can extend this upper limit to 64 without any impact on driver. And the main modification is as follows. It's also mentioned in series[1].
I want to know what do you think about this? I will drop this thing If it's not necessary to you.

/Huisong

[1]https://patchwork.kernel.org/project/linux-arm-kernel/cover/[email protected]/

-->

drivers/hwmon/hwmon.c +0 -0
index 1688c210888a..6f3e92b67e74 100644
@@ -785,19 +785,21 @@ int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,

 }
 EXPORT_SYMBOL_GPL(hwmon_notify_event);

-static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) +static int hwmon_num_channel_attrs(const struct hwmon_chip_info *chip, +                                  const struct hwmon_channel_info *info) {
        int i, n;

        for (i = n = 0; info->config[i]; i++)
-               n += hweight32(info->config[i]);
+               n += chip->attribute_bit64 ? hweight64(info->config64[i]) :
+                               hweight32(info->config[i]);

        return n;
 }

 static int hwmon_genattrs(const void *drvdata,
                          struct attribute **attrs,
-                         const struct hwmon_ops *ops, +                         const struct hwmon_chip_info *chip, const struct hwmon_channel_info *info) {
        const char * const *templates;
@@ -811,7 +813,8 @@ static int hwmon_genattrs(const void *drvdata, template_size = __templates_size[info->type];

        for (i = 0; info->config[i]; i++) {
-               u32 attr_mask = info->config[i];
+               u64 attr_mask = chip->attribute_bit64 ?
+                                info->config64[i] : info->config[i]; u32 attr;

                while (attr_mask) {
@@ -822,7 +825,7 @@ static int hwmon_genattrs(const void *drvdata,
                        if (attr >= template_size || !templates[attr])
                                continue;       /* attribute is invisible */
                        a = hwmon_genattr(drvdata, info->type, attr, i,
-                                         templates[attr], ops);
+                                         templates[attr], chip->ops);
                        if (IS_ERR(a)) {
                                if (PTR_ERR(a) != -ENOENT)
                                        return PTR_ERR(a);
@@ -841,7 +844,7 @@ __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip) struct attribute **attrs;

        for (i = 0; chip->info[i]; i++)
-               nattrs += hwmon_num_channel_attrs(chip->info[i]); +               nattrs += hwmon_num_channel_attrs(chip, chip->info[i]);

        if (nattrs == 0)
                return ERR_PTR(-EINVAL);
@@ -851,7 +854,7 @@ __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip) return ERR_PTR(-ENOMEM);

        for (i = 0; chip->info[i]; i++) {
-               ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops, +               ret = hwmon_genattrs(drvdata, &attrs[aindex], chip, chip->info[i]);
                if (ret < 0) {
                        hwmon_free_attrs(attrs);

include/linux/hwmon.h +0 -0
index 3a63dff62d03..8634bc4bdd1c 100644

 /**
  * struct hwmon_ops - hwmon device operations
@@ -433,7 +433,10 @@ struct hwmon_ops {
  */
 struct hwmon_channel_info {
        enum hwmon_sensor_types type;
-       const u32 *config;
+       union {
+               const u32 *config;
+               const u64 *config64;
+       }
 };

 #define HWMON_CHANNEL_INFO(stype, ...)         \
@@ -444,12 +447,21 @@ struct hwmon_channel_info {
                }                               \
        })

+#define HWMON_CHANNEL_INFO64(stype, ...)               \
+       (&(const struct hwmon_channel_info) {   \
+               .type = hwmon_##stype,          \
+               .config64 = (const u64 []) {    \
+                       __VA_ARGS__, 0          \
+               }                               \
+       })
+
 /**
  * struct hwmon_chip_info - Chip configuration
  * @ops:       Pointer to hwmon operations.
  * @info:      Null-terminated list of channel information. */
 struct hwmon_chip_info {
+       bool attribute_bit64; // use config64 pointer if it is true. const struct hwmon_ops *ops;
        const struct hwmon_channel_info * const *info;
 };
-->

maintainer
#1 lore raw
Quoted 8 lines · lihuisong (C)

Hi Guenter,

Currently, the implementation of hwmon core limits the maximum number of hwmon channel attributes is 32. The maximum number of used attributes in hwmon.h is 31(namely, hwmon_power_attributes) .
Actually, we can extend this upper limit to 64 without any impact on driver. And the main modification is as follows. It's also mentioned in series[1].
I want to know what do you think about this? I will drop this thing If it's not necessary to you.

I dislike complicate solutions if a simpler one is available.

Your suggested change still increases the size of struct hwmon_channel_info for every single driver. It is also not complete, because any definitions exceeding 32 bit would have to use BIT_ULL(), not BIT(). If we need the increased size, we can either add a second group of power attributes or something like

include/linux/hwmon.h +2 -2
index 3a63dff62d03..837c11fb1e3b 100644
@@ -433,13 +433,13 @@ struct hwmon_ops {
  */
 struct hwmon_channel_info {
        enum hwmon_sensor_types type;
-       const u32 *config;
+       const u64 *config;
 };
 
 #define HWMON_CHANNEL_INFO(stype, ...)         \
        (&(const struct hwmon_channel_info) {   \
                .type = hwmon_##stype,          \
-               .config = (const u32 []) {      \
+               .config = (const u64 []) {      \
                        __VA_ARGS__, 0          \
                }                               \
        })

[ plus BIT() -> BIT_ULL() in include/drivers/hwmon.h ]

and handle the fallout for affected drivers which assign .config manually.

I still hesitate doing that though because of the data size increase. My strong preference is to add a second group of power attributes if and when needed.

Guenter

#2 lore raw
Quoted 11 lines · Guenter Roeck
Quoted 8 lines · lihuisong (C)

Hi Guenter,

Currently, the implementation of hwmon core limits the maximum number of hwmon channel attributes is 32. The maximum number of used attributes in hwmon.h is 31(namely, hwmon_power_attributes) .
Actually, we can extend this upper limit to 64 without any impact on driver. And the main modification is as follows. It's also mentioned in series[1].
I want to know what do you think about this? I will drop this thing If it's not necessary to you.

I dislike complicate solutions if a simpler one is available.

Ack

Quoted 3 lines

Your suggested change still increases the size of struct hwmon_channel_info for every single driver. It is also not complete, because any definitions

It does't increase the size of struct hwmon_channel_info because the 'config' is pointer type.

Quoted 1 line

exceeding 32 bit would have to use BIT_ULL(), not BIT(). If we need the

Yes, we need to use BIT_ULL(), not BIT(), which will increase the array size of struct hwmon_channel_info. But the size of variables in driver should not be an obstacle for this.

Quoted 24 lines

increased size, we can either add a second group of power attributes or something like

include/linux/hwmon.h +2 -2
index 3a63dff62d03..837c11fb1e3b 100644
@@ -433,13 +433,13 @@ struct hwmon_ops {
   */
  struct hwmon_channel_info {
         enum hwmon_sensor_types type;
-       const u32 *config;
+       const u64 *config;
  };
 
  #define HWMON_CHANNEL_INFO(stype, ...)         \
         (&(const struct hwmon_channel_info) {   \
                 .type = hwmon_##stype,          \
-               .config = (const u32 []) {      \
+               .config = (const u64 []) {      \
                         __VA_ARGS__, 0          \
                 }                               \
         })
Quoted 7 lines

[ plus BIT() -> BIT_ULL() in include/drivers/hwmon.h ]

and handle the fallout for affected drivers which assign .config manually.

I still hesitate doing that though because of the data size increase. My strong preference is to add a second group of power attributes if and when needed.

I understand you.
Perhaps we can compare the advantages and disadvantage of the two schemes:
*Scheme A*: add new u64* config for struct hwmon_channel_info
Advantage:
No need to define multiple attributes for single hwmon type, which is friendly to use for driver because of no need to use two similar name hwmon types. Disadvantage:
Increase the array size of struct hwmon_channel_info for the existed driver.

For this scheme, we can suggest all later drivers use the u64 type config.

*Scheme B: *add a second group for needed hwmon type.
Advantage:
1> Don't increase the data size for every single driver.
2> No need to modify the code in hwmon core.
3> All are u32 data type, which is simple.
Disadvantage:
Driver may see two similar name hwmon types when in use, which is not very friendly.

From above comparing, I think they're all ok.

/Huisong

Quoted 2 lines