some bugfix for acpi power meter
v2 cover some bugfix for acpi power meter
I found some bugfix when I use this driver. And remove a redundant variable by the way.
Huisong Li (3):
hwmon: (acpi_power_meter) Fix uninitialized variables
hwmon: (acpi_power_meter) Fix update the power trip points on failure
hwmon: (acpi_power_meter) Remove redundant 'sensors_valid' variable
drivers/hwmon/acpi_power_meter.c | 55 ++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 17 deletions(-)
Signature · 1 line
2.22.0
PATCH v2 1/3 hwmon: (acpi_power_meter) Fix uninitialized variables
The 'power1_alarm' attribute uses the 'power' and 'cap' in the acpi_power_meter_resource structure. Currently, these two fields are just updated when user query 'power' and 'cap' attribute. If user directly query the 'power1_alarm' attribute without queryng above two attributes, driver will use uninitialized variables to judge.
So this patch adds the setting of alarm state and update 'cap' in the notification callback and update 'power' and 'cap' if needed to show the real alarm state.
--- drivers/hwmon/acpi_power_meter.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-)
drivers/hwmon/acpi_power_meter.c +23 -4
@@ -84,6 +84,7 @@ struct acpi_power_meter_resource {u64 power;u64 cap;u64 avg_interval; +bool power_alarm;int sensors_valid;unsigned long sensors_last_updated;struct sensor_device_attribute sensors[NUM_SENSORS ];
@@ -396,6 +397,9 @@ static ssize_t show_val(struct device *dev,struct acpi_device *acpi_dev =to_acpi_device (dev);struct acpi_power_meter_resource *resource = acpi_dev->driver_data;u64 val =0 ; +int ret; + +guard (mutex)(&resource->lock);switch (attr->index) {case 0 :
@@ -423,10 +427,17 @@ static ssize_t show_val(struct device *dev, val =0 ;break ;case 6 : -if (resource->power > resource->cap) - val =1 ; -else - val =0 ; + ret =update_meter (resource); +if (ret) +return ret; +/* need to update cap if not to support the notification. */ +if (!(resource->caps.flags &POWER_METER_CAN_NOTIFY )) { + ret =update_cap (resource); +if (ret) +return ret; + } + val = resource->power_alarm || resource->power > resource->cap; + resource->power_alarm = resource->power > resource->cap ? true : false;break ;case 7 :case 8 :
@@ -847,12 +858,20 @@ static void acpi_power_meter_notify(struct acpi_device *device, u32 event)sysfs_notify (&device->dev.kobj,NULL ,POWER_AVERAGE_NAME );break ;case METER_NOTIFY_CAP : +mutex_lock (&resource->lock); + res =update_cap (resource); +if (res) +dev_err (&device->dev,"update cap failed when capping value is changed.\n" ); +mutex_unlock (&resource->lock);sysfs_notify (&device->dev.kobj,NULL ,POWER_CAP_NAME );break ;case METER_NOTIFY_INTERVAL :sysfs_notify (&device->dev.kobj,NULL ,POWER_AVG_INTERVAL_NAME );break ;case METER_NOTIFY_CAPPING : +mutex_lock (&resource->lock); + resource->power_alarm = true; +mutex_unlock (&resource->lock);sysfs_notify (&device->dev.kobj,NULL ,POWER_ALARM_NAME );dev_info (&device->dev,"Capping in progress.\n" );break ;
Signature · 1 line
2.22.0
Guenter Roeck
maintainerRe: [PATCH v2 1/3] hwmon: (acpi_power_meter) Fix uninitialized variables
Quoted 46 lines · Huisong Li
The '
power1_alarm' attribute uses the 'power' and 'cap' in theacpi_power_meter_resourcestructure. Currently, these two fields are just updated when user query 'power' and 'cap' attribute. If user directly query the 'power1_alarm' attribute without queryng above two attributes, driver will use uninitialized variables to judge.So this patch adds the setting of alarm state and update 'cap' in the notification callback and update 'power' and 'cap' if needed to show the real alarm state.
Signed-off-by Huisong Li <[email protected]>--- drivers/hwmon/acpi_power_meter.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-)drivers/hwmon/acpi_power_meter.c +5
-4@@ -84,6 +84,7 @@ struct acpi_power_meter_resource {u64 power;u64 cap;u64 avg_interval; +bool power_alarm;int sensors_valid;unsigned long sensors_last_updated;struct sensor_device_attribute sensors[NUM_SENSORS ];@@ -396,6 +397,9 @@ static ssize_t show_val(struct device *dev,struct acpi_device *acpi_dev =to_acpi_device (dev);struct acpi_power_meter_resource *resource = acpi_dev->driver_data;u64 val =0 ; +int ret; + +guard (mutex)(&resource->lock);switch (attr->index) {case 0 :@@ -423,10 +427,17 @@ static ssize_t show_val(struct device *dev, val =0 ;break ;case 6 : -if (resource->power > resource->cap) - val =1 ; -else - val =0 ; + ret =update_meter (resource);
drivers/hwmon/acpi_power_meter.c static ssize_t show_val(struct device *dev, · line 440
Quoted 10 lines
drivers/hwmon/acpi_power_meter.c +10
-0+if (ret) +return ret; +/* need to update cap if not to support the notification. */ +if (!(resource->caps.flags &POWER_METER_CAN_NOTIFY )) { + ret =update_cap (resource); +if (ret) +return ret; + } + val = resource->power_alarm || resource->power > resource->cap; + resource->power_alarm = resource->power > resource->cap ? true : false;
The result of the comparison is already a boolean. ? : is unnecessary.
drivers/hwmon/acpi_power_meter.c static void acpi_power_meter_notify(struct acpi_device *device, u32 event) · line 864
Quoted 11 lines
drivers/hwmon/acpi_power_meter.c +4
-0break ;case 7 :case 8 :@@ -847,12 +858,20 @@ static void acpi_power_meter_notify(struct acpi_device *device, u32 event)sysfs_notify (&device->dev.kobj,NULL ,POWER_AVERAGE_NAME );break ;case METER_NOTIFY_CAP : +mutex_lock (&resource->lock); + res =update_cap (resource); +if (res) +dev_err (&device->dev,"update cap failed when capping value is changed.\n" );
Please make this dev_err_once() to avoid logging noise if the problem is persistent.
Quoted 13 lines
drivers/hwmon/acpi_power_meter.c +4
-0+mutex_unlock (&resource->lock);sysfs_notify (&device->dev.kobj,NULL ,POWER_CAP_NAME );break ;case METER_NOTIFY_INTERVAL :sysfs_notify (&device->dev.kobj,NULL ,POWER_AVG_INTERVAL_NAME );break ;case METER_NOTIFY_CAPPING : +mutex_lock (&resource->lock); + resource->power_alarm = true; +mutex_unlock (&resource->lock);sysfs_notify (&device->dev.kobj,NULL ,POWER_ALARM_NAME );dev_info (&device->dev,"Capping in progress.\n" );break ;
Re: [PATCH v2 1/3] hwmon: (acpi_power_meter) Fix uninitialized variables
Quoted 51 lines · Guenter Roeck
Quoted 50 lines · Huisong Li
The '
power1_alarm' attribute uses the 'power' and 'cap' in theacpi_power_meter_resourcestructure. Currently, these two fields are just updated when user query 'power' and 'cap' attribute. If user directly query the 'power1_alarm' attribute without queryng above two attributes, driver will use uninitialized variables to judge.So this patch adds the setting of alarm state and update 'cap' in the notification callback and update 'power' and 'cap' if needed to show the real alarm state.
Signed-off-by Huisong Li <[email protected]>---
drivers/hwmon/acpi_power_meter.c| 27 +++++++++++++++++++++++----1 file changed, 23 insertions(+), 4 deletions(-)drivers/hwmon/acpi_power_meter.c +4
-0@@ -84,6 +84,7 @@ struct acpi_power_meter_resource { u64 power; u64 cap; u64 avg_interval; + bool power_alarm; int sensors_valid; unsigned long sensors_last_updated; struct sensor_device_attribute sensors[NUM_SENSORS];@@ -396,6 +397,9 @@ static ssize_t show_val(struct device *dev, struct acpi_device *acpi_dev = to_acpi_device(dev); struct acpi_power_meter_resource *resource = acpi_dev->driver_data; u64 val = 0; + int ret; + + guard(mutex)(&resource->lock); switch (attr->index) {case 0:
drivers/hwmon/acpi_power_meter.c +0
-0@@ -423,10 +427,17 @@ static ssize_t show_val(struct device *dev, val = 0; break;case 6:
- if (resource->power>resource->cap)
- val = 1;
- else
- val = 0;
+ ret =update_meter(resource);
+ if (ret)
+ return ret;
+ /* need to update cap if not to support the notification. */ + if (!(resource->caps.flags &POWER_METER_CAN_NOTIFY)) { + ret =update_cap(resource);
Quoted 8 lines
Quoted 6 lines
+ if (ret)
+ return ret;
+ }
+ val =resource->power_alarm||resource->power>resource->cap; +resource->power_alarm=resource->power>resource->cap? true : false;The result of the comparison is already a boolean. ? : is unnecessary.
Right, will fix in v3.
Quoted 17 lines
Quoted 8 lines
break;
case 7:
case 8:drivers/hwmon/acpi_power_meter.c +0
-0@@ -847,12 +858,20 @@ static void acpi_power_meter_notify(struct acpi_device *device, u32 event) sysfs_notify(&device->dev.kobj, NULL, POWER_AVERAGE_NAME); break;Quoted 6 lines
case
METER_NOTIFY_CAP:
+mutex_lock(&resource->lock);
+ res =update_cap(resource);
+ if (res)
+dev_err(&device->dev, "update cap failed when capping value is changed.\n");Please make this
dev_err_once()to avoid logging noise if the problem is persistent.
will fix in v3.
Thanks.
Quoted 16 lines
Quoted 14 lines
+
mutex_unlock(&resource->lock);sysfs_notify(&device->dev.kobj, NULL, POWER_CAP_NAME); break;
caseMETER_NOTIFY_INTERVAL:
sysfs_notify(&device->dev.kobj, NULL, POWER_AVG_INTERVAL_NAME); break;
caseMETER_NOTIFY_CAPPING:
+mutex_lock(&resource->lock);
+resource->power_alarm= true;
+mutex_unlock(&resource->lock);sysfs_notify(&device->dev.kobj, NULL, POWER_ALARM_NAME);dev_info(&device->dev, "Capping in progress.\n"); break;.
PATCH v2 2/3 hwmon: (acpi_power_meter) Fix update the power trip points on failure
In reply to Huisong LiThe power trip points maintained in local should not be updated when '_PTP' method fails to evaluate.
--- drivers/hwmon/acpi_power_meter.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/hwmon/acpi_power_meter.c +7 -5
@@ -293,8 +293,8 @@ static ssize_t set_trip(struct device *dev, struct device_attribute *devattr,struct sensor_device_attribute *attr =to_sensor_dev_attr (devattr);struct acpi_device *acpi_dev =to_acpi_device (dev);struct acpi_power_meter_resource *resource = acpi_dev->driver_data; +unsigned long temp, trip_bk;int res; -unsigned long temp; res =kstrtoul (buf,10 , &temp);if (res)
@@ -302,13 +302,15 @@ static ssize_t set_trip(struct device *dev, struct device_attribute *devattr, temp =DIV_ROUND_CLOSEST (temp,1000 ); -mutex_lock (&resource->lock); +guard (mutex)(&resource->lock); + + trip_bk = resource->trip[attr->index -7 ]; resource->trip[attr->index -7 ] = temp; res =set_acpi_trip (resource); -mutex_unlock (&resource->lock); - -if (res) +if (res) { + resource->trip[attr->index -7 ] = trip_bk;return res; + }return count; }
Signature · 1 line
2.22.0
PATCH v2 3/3 hwmon: (acpi_power_meter) Remove redundant 'sensors_valid' variable
In reply to Huisong LiThe 'sensors_valid' in acpi_power_meter_resource structure is always '1' after querying power once. The default value of this variable is zero which just ensure user can query power successfully without any time requirement at first time. We can get power and fill the 'sensors_last_updated' field at probing phase to make sure that a valid value is returned to user at first query within the sampling interval. Then this redundant variable can be safely removed.
--- drivers/hwmon/acpi_power_meter.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/hwmon/acpi_power_meter.c +9 -9
@@ -85,7 +85,6 @@ struct acpi_power_meter_resource {u64 cap;u64 avg_interval;bool power_alarm; -int sensors_valid;unsigned long sensors_last_updated;struct sensor_device_attribute sensors[NUM_SENSORS ];int num_sensors;
@@ -316,15 +315,14 @@ static ssize_t set_trip(struct device *dev, struct device_attribute *devattr, }/* Power meter */ -static int update_meter (struct acpi_power_meter_resource *resource) +static int update_meter (struct acpi_power_meter_resource *resource,bool check) {unsigned long long data;acpi_status status;unsigned long local_jiffies = jiffies; -if (time_before (local_jiffies, resource->sensors_last_updated + -msecs_to_jiffies (resource->caps.sampling_time)) && - resource->sensors_valid) +if (check &&time_before (local_jiffies, resource->sensors_last_updated + +msecs_to_jiffies (resource->caps.sampling_time)))return 0 ; status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PMM",
@@ -336,7 +334,6 @@ static int update_meter(struct acpi_power_meter_resource *resource) } resource->power = data; - resource->sensors_valid =1 ; resource->sensors_last_updated = jiffies;return 0 ; }
@@ -349,7 +346,7 @@ static ssize_t show_power(struct device *dev,struct acpi_power_meter_resource *resource = acpi_dev->driver_data;mutex_lock (&resource->lock); -update_meter (resource); +update_meter (resource, true);mutex_unlock (&resource->lock);if (resource->power ==UNKNOWN_POWER )
@@ -429,7 +426,7 @@ static ssize_t show_val(struct device *dev, val =0 ;break ;case 6 : - ret =update_meter (resource); + ret =update_meter (resource, true);if (ret)return ret;/* need to update cap if not to support the notification. */
@@ -699,6 +696,10 @@ static int setup_attrs(struct acpi_power_meter_resource *resource)return res;if (resource->caps.flags &POWER_METER_CAN_MEASURE ) { + res =update_meter (resource, false); +if (res) + goto error; + res =register_attrs (resource, meter_attrs);if (res) goto error;
@@ -898,7 +899,6 @@ static int acpi_power_meter_add(struct acpi_device *device)if (!resource)return -ENOMEM ; - resource->sensors_valid =0 ; resource->acpi_dev = device;mutex_init (&resource->lock);strcpy (acpi_device_name (device),ACPI_POWER_METER_DEVICE_NAME );
Signature · 1 line
2.22.0
Guenter Roeck
maintainerRe: [PATCH v2 3/3] hwmon: (acpi_power_meter) Remove redundant 'sensors_valid' variable
Quoted 8 lines · Huisong Li
The '
sensors_valid' inacpi_power_meter_resourcestructure is always '1' after querying power once. The default value of this variable is zero which just ensure user can query power successfully without any time requirement at first time. We can get power and fill the 'sensors_last_updated' field at probing phase to make sure that a valid value is returned to user at first query within the sampling interval. Then this redundant variable can be safely removed.
The "benefit" of this change is the saved variable. The cost associated with it is that update_meter() is now _always_ called from setup_attrs(), during probe and when handling configuration change notifications. It seems to me that this is much more costly than keeping the variable since it is unlikely that the show functions are actually called within the sampling time.
I fail to see why removing a variable would be more beneficial than the overhead of unnecessarily calling update_meter() during probe (which also increases probe time) and while handling configuration change notifications.
This would need a much better rationale to be acceptable.
Guenter
Quoted 81 lines
Signed-off-by Huisong Li <[email protected]>--- drivers/hwmon/acpi_power_meter.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)drivers/hwmon/acpi_power_meter.c +9
-9@@ -85,7 +85,6 @@ struct acpi_power_meter_resource {u64 cap;u64 avg_interval;bool power_alarm; -int sensors_valid;unsigned long sensors_last_updated;struct sensor_device_attribute sensors[NUM_SENSORS ];int num_sensors;@@ -316,15 +315,14 @@ static ssize_t set_trip(struct device *dev, struct device_attribute *devattr, }/* Power meter */ -static int update_meter (struct acpi_power_meter_resource *resource) +static int update_meter (struct acpi_power_meter_resource *resource,bool check) {unsigned long long data;acpi_status status;unsigned long local_jiffies = jiffies; -if (time_before (local_jiffies, resource->sensors_last_updated + -msecs_to_jiffies (resource->caps.sampling_time)) && - resource->sensors_valid) +if (check &&time_before (local_jiffies, resource->sensors_last_updated + +msecs_to_jiffies (resource->caps.sampling_time)))return 0 ; status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PMM",@@ -336,7 +334,6 @@ static int update_meter(struct acpi_power_meter_resource *resource) } resource->power = data; - resource->sensors_valid =1 ; resource->sensors_last_updated = jiffies;return 0 ; }@@ -349,7 +346,7 @@ static ssize_t show_power(struct device *dev,struct acpi_power_meter_resource *resource = acpi_dev->driver_data;mutex_lock (&resource->lock); -update_meter (resource); +update_meter (resource, true);mutex_unlock (&resource->lock);if (resource->power ==UNKNOWN_POWER )@@ -429,7 +426,7 @@ static ssize_t show_val(struct device *dev, val =0 ;break ;case 6 : - ret =update_meter (resource); + ret =update_meter (resource, true);if (ret)return ret;/* need to update cap if not to support the notification. */ @@ -699,6 +696,10 @@ static int setup_attrs(struct acpi_power_meter_resource *resource)return res;if (resource->caps.flags &POWER_METER_CAN_MEASURE ) { + res =update_meter (resource, false); +if (res) + goto error; + res =register_attrs (resource, meter_attrs);if (res) goto error;@@ -898,7 +899,6 @@ static int acpi_power_meter_add(struct acpi_device *device)if (!resource)return -ENOMEM ; - resource->sensors_valid =0 ; resource->acpi_dev = device;mutex_init (&resource->lock);strcpy (acpi_device_name (device),ACPI_POWER_METER_DEVICE_NAME );
Re: [PATCH v2 3/3] hwmon: (acpi_power_meter) Remove redundant 'sensors_valid' variable
Quoted 16 lines · Guenter Roeck
Quoted 8 lines · Huisong Li
The '
sensors_valid' inacpi_power_meter_resourcestructure is always '1' after querying power once. The default value of this variable is zero which just ensure user can query power successfully without any time requirement at first time. We can get power and fill the 'sensors_last_updated' field at probing phase to make sure that a valid value is returned to user at first query within the sampling interval. Then this redundant variable can be safely removed.The "benefit" of this change is the saved variable. The cost associated with it is that
update_meter()is now _always_ called fromsetup_attrs(), during probe and when handling configuration change notifications. It seems to me that this is much more costly than keeping the variable since it is unlikely that the show functions are actually called within the sampling time.
Quoted 3 lines
I fail to see why removing a variable would be more beneficial than the overhead of unnecessarily calling
update_meter()during probe (which also increases probe time) and while handling configuration change notifications.
That's what it sounds like.
Ok, let's drop this patch.
Quoted 86 lines
This would need a much better rationale to be acceptable.
Guenter
Quoted 78 lines
Signed-off-by Huisong Li <[email protected]>---
drivers/hwmon/acpi_power_meter.c| 18 +++++++++---------1 file changed, 9 insertions(+), 9 deletions(-)drivers/hwmon/acpi_power_meter.c +1
-2@@ -85,7 +85,6 @@ struct acpi_power_meter_resource { u64 cap; u64 avg_interval; bool power_alarm; - int sensors_valid; unsigned long sensors_last_updated; struct sensor_device_attribute sensors[NUM_SENSORS]; int num_sensors;@@ -316,15 +315,14 @@ static ssize_t set_trip(struct device *dev,struct device_attribute *devattr, } /* Power meter */ -static int update_meter(struct acpi_power_meter_resource *resource) +static int update_meter(struct acpi_power_meter_resource *resource, bool check) { unsigned long long data; acpi_status status; unsigned long local_jiffies = jiffies;- if (time_before(local_jiffies,
resource->sensors_last_updated+ -msecs_to_jiffies(resource->caps.sampling_time)) && -resource->sensors_valid)
+ if (check && time_before(local_jiffies,resource->sensors_last_updated+
+msecs_to_jiffies(resource->caps.sampling_time))) return 0;
status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PMM",drivers/hwmon/acpi_power_meter.c +1
-2@@ -336,7 +334,6 @@ static int update_meter(struct acpi_power_meter_resource *resource) } resource->power = data; - resource->sensors_valid = 1; resource->sensors_last_updated = jiffies; return 0; }@@ -349,7 +346,7 @@ static ssize_t show_power(struct device *dev, struct acpi_power_meter_resource *resource = acpi_dev->driver_data; mutex_lock(&resource->lock); - update_meter(resource); + update_meter(resource, true); mutex_unlock(&resource->lock); if (resource->power == UNKNOWN_POWER)@@ -429,7 +426,7 @@ static ssize_t show_val(struct device *dev, val = 0; break;case 6:
- ret =update_meter(resource);
+ ret =update_meter(resource, true);
if (ret)
return ret;
/* need to update cap if not to support the notification. */drivers/hwmon/acpi_power_meter.c +4
-0@@ -699,6 +696,10 @@ static int setup_attrs(struct acpi_power_meter_resource *resource) return res; if (resource->caps.flags & POWER_METER_CAN_MEASURE) { + res = update_meter(resource, false); + if (res) + goto error; + res = register_attrs(resource, meter_attrs); if (res) goto error;@@ -898,7 +899,6 @@ static int acpi_power_meter_add(struct acpi_device *device) if (!resource) return -ENOMEM; - resource->sensors_valid = 0; resource->acpi_dev = device; mutex_init(&resource->lock);Quoted 1 line
strcpy(acpi_device_name(device), ACPI_POWER_METER_DEVICE_NAME);.