ACPI: platform-profile: Fix possible deadlock in platform_profile_remove()
Subsystems (2)
cover ACPI: platform-profile: Fix possible deadlock in platform_profile_remove()
Hi Rafael,
Sorry for throwing another patch into the mix for the acpi-platform branch. I always build my local kernels with lockdep enabled and while testing an unrelated acpi_thinkpad patch I did "rmmod acpi_thinkpad" and lockdep pointed out this potential deadlock.
The fix is simple enough, although it does rely on the assumption that drivers will only call platform_profile_remove() after they have first successfully called platform_profile_register().
See the actual patch for more details.
Regards,
Hans
Hans de Goede (1):
ACPI: platform-profile: Fix possible deadlock in platform_profile_remove()
drivers/acpi/platform_profile.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
PATCH 1/1 ACPI: platform-profile: Fix possible deadlock in platform_profile_remove()
After a rmmod thinkpad_acpi, lockdep pointed out this possible deadlock:
Our _show and _store sysfs attr functions get called with the kn->active lock held for the sysfs attr and then take the profile_lock. sysfs_remove_group() also takes the kn->active lock for the sysfs attr, so if we call it with the profile_lock held, then we get an ABBA deadlock.
platform_profile_remove() must only be called by drivers which have first *successfully* called platform_profile_register(). Anything else is a driver bug. So the check for cur_profile being set before calling sysfs_remove_group() is not necessary and it can be dropped.
It is safe to call sysfs_remove_group() without holding the profile_lock since the attr-group group cannot be re-added until after we clear cur_profile.
Change platform_profile_remove() to only hold the profile_lock while clearing the cur_profile, fixing the deadlock.
--- drivers/acpi/platform_profile.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/acpi/platform_profile.c +2 -6
@@ -164,13 +164,9 @@ EXPORT_SYMBOL_GPL(platform_profile_register);int platform_profile_remove (void ) { -mutex_lock (&profile_lock); -if (!cur_profile) { -mutex_unlock (&profile_lock); -return -ENODEV ; - } -sysfs_remove_group (acpi_kobj, &platform_profile_group); + +mutex_lock (&profile_lock); cur_profile =NULL ;mutex_unlock (&profile_lock);return 0 ;
Signature · 1 line
2.29.2
Quoted 49 lines
-----Original Message-----
From: Hans de Goede <[email protected]>
Sent: Monday, January 25, 2021 2:09 PMAfter a rmmod
thinkpad_acpi, lockdep pointed out this possible deadlock:Our
_showand_storesysfs attr functions get called with thekn->activelock held for the sysfs attr and then take theprofile_lock.sysfs_remove_group()also takes thekn->activelock for the sysfs attr, so if we call it with theprofile_lockheld, then we get an ABBA deadlock.
platform_profile_remove()must only be called by drivers which have first *successfully* calledplatform_profile_register(). Anything else is a driver bug. So the check forcur_profilebeing set before callingsysfs_remove_group()is not necessary and it can be dropped.It is safe to call
sysfs_remove_group()without holding theprofile_locksince the attr-group group cannot be re-added until after we clearcur_profile.Change
platform_profile_remove()to only hold theprofile_lockwhile clearing thecur_profile, fixing the deadlock.Signed-off-by Hans de Goede <[email protected]>--- drivers/acpi/platform_profile.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)drivers/acpi/platform_profile.c +2
-7@@ -164,13 +164,9 @@ EXPORT_SYMBOL_GPL(platform_profile_register);int platform_profile_remove (void ) { -mutex_lock (&profile_lock); -if (!cur_profile) { -mutex_unlock (&profile_lock); -return -ENODEV ; - } -sysfs_remove_group (acpi_kobj, &platform_profile_group); + +mutex_lock (&profile_lock); cur_profile =NULL ;mutex_unlock (&profile_lock);return 0 ; --
Quoted 1 line
2.29.2
Thanks Hans - good catch! Looks good to me.
Mark
Rafael J. Wysocki
maintainerQuoted 13 lines · Hans de Goede
After a rmmod
thinkpad_acpi, lockdep pointed out this possible deadlock:Our
_showand_storesysfs attr functions get called with thekn->activelock held for the sysfs attr and then take theprofile_lock.sysfs_remove_group()also takes thekn->activelock for the sysfs attr, so if we call it with theprofile_lockheld, then we get an ABBA deadlock.
platform_profile_remove()must only be called by drivers which have first *successfully* calledplatform_profile_register(). Anything else is a driver bug. So the check forcur_profilebeing set before callingsysfs_remove_group()is not necessary and it can be dropped.
Quoted 8 lines
It is safe to call
sysfs_remove_group()without holding theprofile_locksince the attr-group group cannot be re-added until after we clearcur_profile.Change
platform_profile_remove()to only hold theprofile_lockwhile clearing thecur_profile, fixing the deadlock.Signed-off-by Hans de Goede <[email protected]>
Applied on top of the previous platform-profile material, thanks!
Quoted 27 lines
--- drivers/acpi/platform_profile.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)drivers/acpi/platform_profile.c +2
-7@@ -164,13 +164,9 @@ EXPORT_SYMBOL_GPL(platform_profile_register);int platform_profile_remove (void ) { -mutex_lock (&profile_lock); -if (!cur_profile) { -mutex_unlock (&profile_lock); -return -ENODEV ; - } -sysfs_remove_group (acpi_kobj, &platform_profile_group); + +mutex_lock (&profile_lock); cur_profile =NULL ;mutex_unlock (&profile_lock);return 0 ; --2.29.2