Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit f4d1960

Browse files
AlanSterngregkh
authored andcommitted
USB: core: Fix deadlock in port "disable" sysfs attribute
The show and store callback routines for the "disable" sysfs attribute file in port.c acquire the device lock for the port's parent hub device. This can cause problems if another process has locked the hub to remove it or change its configuration: Removing the hub or changing its configuration requires the hub interface to be removed, which requires the port device to be removed, and device_del() waits until all outstanding sysfs attribute callbacks for the ports have returned. The lock can't be released until then. But the disable_show() or disable_store() routine can't return until after it has acquired the lock. The resulting deadlock can be avoided by calling sysfs_break_active_protection(). This will cause the sysfs core not to wait for the attribute's callback routine to return, allowing the removal to proceed. The disadvantage is that after making this call, there is no guarantee that the hub structure won't be deallocated at any moment. To prevent this, we have to acquire a reference to it first by calling hub_get(). Signed-off-by: Alan Stern <stern@rowland.harvard.edu> Cc: stable <stable@kernel.org> Link: https://lore.kernel.org/r/f7a8c135-a495-4ce6-bd49-405a45e7ea9a@rowland.harvard.edu Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ee113b8 commit f4d1960

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

drivers/usb/core/port.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,22 @@ static ssize_t disable_show(struct device *dev,
5656
u16 portstatus, unused;
5757
bool disabled;
5858
int rc;
59+
struct kernfs_node *kn;
5960

61+
hub_get(hub);
6062
rc = usb_autopm_get_interface(intf);
6163
if (rc < 0)
62-
return rc;
64+
goto out_hub_get;
6365

66+
/*
67+
* Prevent deadlock if another process is concurrently
68+
* trying to unregister hdev.
69+
*/
70+
kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
71+
if (!kn) {
72+
rc = -ENODEV;
73+
goto out_autopm;
74+
}
6475
usb_lock_device(hdev);
6576
if (hub->disconnected) {
6677
rc = -ENODEV;
@@ -70,9 +81,13 @@ static ssize_t disable_show(struct device *dev,
7081
usb_hub_port_status(hub, port1, &portstatus, &unused);
7182
disabled = !usb_port_is_power_on(hub, portstatus);
7283

73-
out_hdev_lock:
84+
out_hdev_lock:
7485
usb_unlock_device(hdev);
86+
sysfs_unbreak_active_protection(kn);
87+
out_autopm:
7588
usb_autopm_put_interface(intf);
89+
out_hub_get:
90+
hub_put(hub);
7691

7792
if (rc)
7893
return rc;
@@ -90,15 +105,26 @@ static ssize_t disable_store(struct device *dev, struct device_attribute *attr,
90105
int port1 = port_dev->portnum;
91106
bool disabled;
92107
int rc;
108+
struct kernfs_node *kn;
93109

94110
rc = kstrtobool(buf, &disabled);
95111
if (rc)
96112
return rc;
97113

114+
hub_get(hub);
98115
rc = usb_autopm_get_interface(intf);
99116
if (rc < 0)
100-
return rc;
117+
goto out_hub_get;
101118

119+
/*
120+
* Prevent deadlock if another process is concurrently
121+
* trying to unregister hdev.
122+
*/
123+
kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
124+
if (!kn) {
125+
rc = -ENODEV;
126+
goto out_autopm;
127+
}
102128
usb_lock_device(hdev);
103129
if (hub->disconnected) {
104130
rc = -ENODEV;
@@ -119,9 +145,13 @@ static ssize_t disable_store(struct device *dev, struct device_attribute *attr,
119145
if (!rc)
120146
rc = count;
121147

122-
out_hdev_lock:
148+
out_hdev_lock:
123149
usb_unlock_device(hdev);
150+
sysfs_unbreak_active_protection(kn);
151+
out_autopm:
124152
usb_autopm_put_interface(intf);
153+
out_hub_get:
154+
hub_put(hub);
125155

126156
return rc;
127157
}

0 commit comments

Comments
 (0)