aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuenter Roeck <linux@roeck-us.net>2016-10-16 13:52:04 -0400
committerGuenter Roeck <linux@roeck-us.net>2016-12-10 00:54:24 -0500
commit3a412d5e4a1c831723d0aaf305f1cf9a78ad9c90 (patch)
treeae1f9db72cf6eb4fd05de98c9ecc3f042da16001
parent848ba0a2f20dc121a3ef5272a24641d2bd963d8b (diff)
hwmon: (core) Simplify sysfs attribute name allocation
Allocating the sysfs attribute name only if needed and only with the required minimum length looks optimal, but does not take the additional overhead for both devm_ data structures and the allocation header itself into account. This also results in unnecessary memory fragmentation. Move the sysfs name string into struct hwmon_device_attribute and give it a sufficient length to reduce this overhead. Signed-off-by: Guenter Roeck <linux@roeck-us.net>
-rw-r--r--drivers/hwmon/hwmon.c17
1 files changed, 9 insertions, 8 deletions
diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 58c328f4508d..3932f9276c07 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -38,12 +38,15 @@ struct hwmon_device {
38 38
39#define to_hwmon_device(d) container_of(d, struct hwmon_device, dev) 39#define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
40 40
41#define MAX_SYSFS_ATTR_NAME_LENGTH 32
42
41struct hwmon_device_attribute { 43struct hwmon_device_attribute {
42 struct device_attribute dev_attr; 44 struct device_attribute dev_attr;
43 const struct hwmon_ops *ops; 45 const struct hwmon_ops *ops;
44 enum hwmon_sensor_types type; 46 enum hwmon_sensor_types type;
45 u32 attr; 47 u32 attr;
46 int index; 48 int index;
49 char name[MAX_SYSFS_ATTR_NAME_LENGTH];
47}; 50};
48 51
49#define to_hwmon_attr(d) \ 52#define to_hwmon_attr(d) \
@@ -261,20 +264,18 @@ static struct attribute *hwmon_genattr(struct device *dev,
261 if ((mode & S_IWUGO) && !ops->write) 264 if ((mode & S_IWUGO) && !ops->write)
262 return ERR_PTR(-EINVAL); 265 return ERR_PTR(-EINVAL);
263 266
267 hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
268 if (!hattr)
269 return ERR_PTR(-ENOMEM);
270
264 if (type == hwmon_chip) { 271 if (type == hwmon_chip) {
265 name = (char *)template; 272 name = (char *)template;
266 } else { 273 } else {
267 name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL); 274 scnprintf(hattr->name, sizeof(hattr->name), template,
268 if (!name)
269 return ERR_PTR(-ENOMEM);
270 scnprintf(name, strlen(template) + 16, template,
271 index + hwmon_attr_base(type)); 275 index + hwmon_attr_base(type));
276 name = hattr->name;
272 } 277 }
273 278
274 hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
275 if (!hattr)
276 return ERR_PTR(-ENOMEM);
277
278 hattr->type = type; 279 hattr->type = type;
279 hattr->attr = attr; 280 hattr->attr = attr;
280 hattr->index = index; 281 hattr->index = index;