Skip to content

Commit 615901b

Browse files
rafaeljwgroeck
authored andcommitted
hwmon: (acpi_power_meter) Fix deadlocks related to acpi_power_meter_notify()
The acpi_power_meter driver's .notify() callback function, acpi_power_meter_notify(), calls hwmon_device_unregister() under a lock that is also acquired by callbacks in sysfs attributes of the device being unregistered which is prone to deadlocks between sysfs access and device removal. Address this by moving the hwmon device removal in acpi_power_meter_notify() outside the lock in question, but notice that doing it alone is not sufficient because two concurrent METER_NOTIFY_CONFIG notifications may be attempting to remove the same device at the same time. To prevent that from happening, add a new lock serializing the execution of the switch () statement in acpi_power_meter_notify(). For simplicity, it is a static mutex which should not be a problem from the performance perspective. The new lock also allows the hwmon_device_register_with_info() in acpi_power_meter_notify() to be called outside the inner lock because it prevents the other notifications handled by that function from manipulating the "resource" object while the hwmon device based on it is being registered. The sending of ACPI netlink messages from acpi_power_meter_notify() is serialized by the new lock too which generally helps to ensure that the order of handling firmware notifications is the same as the order of sending netlink messages related to them. In addition, notice that hwmon_device_register_with_info() may fail in which case resource->hwmon_dev will become an error pointer, so add checks to avoid attempting to unregister the hwmon device pointer to by it in that case to acpi_power_meter_notify() and acpi_power_meter_remove(). Fixes: 16746ce ("hwmon: (acpi_power_meter) Replace the deprecated hwmon_device_register") Closes: https://lore.kernel.org/linux-hwmon/CAK8fFZ58fidGUCHi5WFX0uoTPzveUUDzT=k=AAm4yWo3bAuCFg@mail.gmail.com/ Reported-by: Jaroslav Pulchart <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]> Signed-off-by: Guenter Roeck <[email protected]>
1 parent 830e0be commit 615901b

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

drivers/hwmon/acpi_power_meter.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
static int cap_in_hardware;
4848
static bool force_cap_on;
4949

50+
static DEFINE_MUTEX(acpi_notify_lock);
51+
5052
static int can_cap_in_hardware(void)
5153
{
5254
return force_cap_on || cap_in_hardware;
@@ -823,18 +825,26 @@ static void acpi_power_meter_notify(struct acpi_device *device, u32 event)
823825

824826
resource = acpi_driver_data(device);
825827

828+
guard(mutex)(&acpi_notify_lock);
829+
826830
switch (event) {
827831
case METER_NOTIFY_CONFIG:
832+
if (!IS_ERR(resource->hwmon_dev))
833+
hwmon_device_unregister(resource->hwmon_dev);
834+
828835
mutex_lock(&resource->lock);
836+
829837
free_capabilities(resource);
830838
remove_domain_devices(resource);
831-
hwmon_device_unregister(resource->hwmon_dev);
832839
res = read_capabilities(resource);
833840
if (res)
834841
dev_err_once(&device->dev, "read capabilities failed.\n");
835842
res = read_domain_devices(resource);
836843
if (res && res != -ENODEV)
837844
dev_err_once(&device->dev, "read domain devices failed.\n");
845+
846+
mutex_unlock(&resource->lock);
847+
838848
resource->hwmon_dev =
839849
hwmon_device_register_with_info(&device->dev,
840850
ACPI_POWER_METER_NAME,
@@ -843,7 +853,7 @@ static void acpi_power_meter_notify(struct acpi_device *device, u32 event)
843853
power_extra_groups);
844854
if (IS_ERR(resource->hwmon_dev))
845855
dev_err_once(&device->dev, "register hwmon device failed.\n");
846-
mutex_unlock(&resource->lock);
856+
847857
break;
848858
case METER_NOTIFY_TRIP:
849859
sysfs_notify(&device->dev.kobj, NULL, POWER_AVERAGE_NAME);
@@ -953,7 +963,8 @@ static void acpi_power_meter_remove(struct acpi_device *device)
953963
return;
954964

955965
resource = acpi_driver_data(device);
956-
hwmon_device_unregister(resource->hwmon_dev);
966+
if (!IS_ERR(resource->hwmon_dev))
967+
hwmon_device_unregister(resource->hwmon_dev);
957968

958969
remove_domain_devices(resource);
959970
free_capabilities(resource);

0 commit comments

Comments
 (0)