aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorAlan Stern <stern@rowland.harvard.edu>2017-09-21 13:22:00 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2017-09-22 12:29:00 -0400
commit1fbbb78f25d1291274f320462bf6908906f538db (patch)
tree6eae549acb0e08cfc876dcf6d4705d8077e211a9 /drivers
parent520b72fc64debf8a86c3853b8e486aa5982188f0 (diff)
USB: g_mass_storage: Fix deadlock when driver is unbound
As a holdover from the old g_file_storage gadget, the g_mass_storage legacy gadget driver attempts to unregister itself when its main operating thread terminates (if it hasn't been unregistered already). This is not strictly necessary; it was never more than an attempt to have the gadget fail cleanly if something went wrong and the main thread was killed. However, now that the UDC core manages gadget drivers independently of UDC drivers, this scheme doesn't work any more. A simple test: modprobe dummy-hcd modprobe g-mass-storage file=... rmmod dummy-hcd ends up in a deadlock with the following backtrace: sysrq: SysRq : Show Blocked State task PC stack pid father file-storage D 0 1130 2 0x00000000 Call Trace: __schedule+0x53e/0x58c schedule+0x6e/0x77 schedule_preempt_disabled+0xd/0xf __mutex_lock.isra.1+0x129/0x224 ? _raw_spin_unlock_irqrestore+0x12/0x14 __mutex_lock_slowpath+0x12/0x14 mutex_lock+0x28/0x2b usb_gadget_unregister_driver+0x29/0x9b [udc_core] usb_composite_unregister+0x10/0x12 [libcomposite] msg_cleanup+0x1d/0x20 [g_mass_storage] msg_thread_exits+0xd/0xdd7 [g_mass_storage] fsg_main_thread+0x1395/0x13d6 [usb_f_mass_storage] ? __schedule+0x573/0x58c kthread+0xd9/0xdb ? do_set_interface+0x25c/0x25c [usb_f_mass_storage] ? init_completion+0x1e/0x1e ret_from_fork+0x19/0x24 rmmod D 0 1155 683 0x00000000 Call Trace: __schedule+0x53e/0x58c schedule+0x6e/0x77 schedule_timeout+0x26/0xbc ? __schedule+0x573/0x58c do_wait_for_common+0xb3/0x128 ? usleep_range+0x81/0x81 ? wake_up_q+0x3f/0x3f wait_for_common+0x2e/0x45 wait_for_completion+0x17/0x19 fsg_common_put+0x34/0x81 [usb_f_mass_storage] fsg_free_inst+0x13/0x1e [usb_f_mass_storage] usb_put_function_instance+0x1a/0x25 [libcomposite] msg_unbind+0x2a/0x42 [g_mass_storage] __composite_unbind+0x4a/0x6f [libcomposite] composite_unbind+0x12/0x14 [libcomposite] usb_gadget_remove_driver+0x4f/0x77 [udc_core] usb_del_gadget_udc+0x52/0xcc [udc_core] dummy_udc_remove+0x27/0x2c [dummy_hcd] platform_drv_remove+0x1d/0x31 device_release_driver_internal+0xe9/0x16d device_release_driver+0x11/0x13 bus_remove_device+0xd2/0xe2 device_del+0x19f/0x221 ? selinux_capable+0x22/0x27 platform_device_del+0x21/0x63 platform_device_unregister+0x10/0x1a cleanup+0x20/0x817 [dummy_hcd] SyS_delete_module+0x10c/0x197 ? ____fput+0xd/0xf ? task_work_run+0x55/0x62 ? prepare_exit_to_usermode+0x65/0x75 do_fast_syscall_32+0x86/0xc3 entry_SYSENTER_32+0x4e/0x7c What happens is that removing the dummy-hcd driver causes the UDC core to unbind the gadget driver, which it does while holding the udc_lock mutex. The unbind routine in g_mass_storage tells the main thread to exit and waits for it to terminate. But as mentioned above, when the main thread exits it tries to unregister the mass-storage function driver. Via the composite framework this ends up calling usb_gadget_unregister_driver(), which tries to acquire the udc_lock mutex. The result is deadlock. The simplest way to fix the problem is not to be so clever: The main thread doesn't have to unregister the function driver. The side effects won't be so terrible; if the gadget is still attached to a USB host when the main thread is killed, it will appear to the host as though the gadget's firmware has crashed -- a reasonably accurate interpretation, and an all-too-common occurrence for USB mass-storage devices. In fact, the code to unregister the driver when the main thread exits is specific to g-mass-storage; it is not used when f-mass-storage is included as a function in a larger composite device. Therefore the entire mechanism responsible for this (the fsg_operations structure with its ->thread_exits method, the fsg_common_set_ops() routine, and the msg_thread_exits() callback routine) can all be eliminated. Even the msg_registered bitflag can be removed, because now the driver is unregistered in only one place rather than in two places. Signed-off-by: Alan Stern <stern@rowland.harvard.edu> CC: <stable@vger.kernel.org> Acked-by: Felipe Balbi <felipe.balbi@linux.intel.com> Acked-by: Michal Nazarewicz <mina86@mina86.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/usb/gadget/function/f_mass_storage.c27
-rw-r--r--drivers/usb/gadget/function/f_mass_storage.h14
-rw-r--r--drivers/usb/gadget/legacy/mass_storage.c26
3 files changed, 10 insertions, 57 deletions
diff --git a/drivers/usb/gadget/function/f_mass_storage.c b/drivers/usb/gadget/function/f_mass_storage.c
index d6bd0244b008..5153e29870c3 100644
--- a/drivers/usb/gadget/function/f_mass_storage.c
+++ b/drivers/usb/gadget/function/f_mass_storage.c
@@ -307,8 +307,6 @@ struct fsg_common {
307 struct completion thread_notifier; 307 struct completion thread_notifier;
308 struct task_struct *thread_task; 308 struct task_struct *thread_task;
309 309
310 /* Callback functions. */
311 const struct fsg_operations *ops;
312 /* Gadget's private data. */ 310 /* Gadget's private data. */
313 void *private_data; 311 void *private_data;
314 312
@@ -2438,6 +2436,7 @@ static void handle_exception(struct fsg_common *common)
2438static int fsg_main_thread(void *common_) 2436static int fsg_main_thread(void *common_)
2439{ 2437{
2440 struct fsg_common *common = common_; 2438 struct fsg_common *common = common_;
2439 int i;
2441 2440
2442 /* 2441 /*
2443 * Allow the thread to be killed by a signal, but set the signal mask 2442 * Allow the thread to be killed by a signal, but set the signal mask
@@ -2476,21 +2475,16 @@ static int fsg_main_thread(void *common_)
2476 common->thread_task = NULL; 2475 common->thread_task = NULL;
2477 spin_unlock_irq(&common->lock); 2476 spin_unlock_irq(&common->lock);
2478 2477
2479 if (!common->ops || !common->ops->thread_exits 2478 /* Eject media from all LUNs */
2480 || common->ops->thread_exits(common) < 0) {
2481 int i;
2482 2479
2483 down_write(&common->filesem); 2480 down_write(&common->filesem);
2484 for (i = 0; i < ARRAY_SIZE(common->luns); i++) { 2481 for (i = 0; i < ARRAY_SIZE(common->luns); i++) {
2485 struct fsg_lun *curlun = common->luns[i]; 2482 struct fsg_lun *curlun = common->luns[i];
2486 if (!curlun || !fsg_lun_is_open(curlun))
2487 continue;
2488 2483
2484 if (curlun && fsg_lun_is_open(curlun))
2489 fsg_lun_close(curlun); 2485 fsg_lun_close(curlun);
2490 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
2491 }
2492 up_write(&common->filesem);
2493 } 2486 }
2487 up_write(&common->filesem);
2494 2488
2495 /* Let fsg_unbind() know the thread has exited */ 2489 /* Let fsg_unbind() know the thread has exited */
2496 complete_and_exit(&common->thread_notifier, 0); 2490 complete_and_exit(&common->thread_notifier, 0);
@@ -2681,13 +2675,6 @@ void fsg_common_remove_luns(struct fsg_common *common)
2681} 2675}
2682EXPORT_SYMBOL_GPL(fsg_common_remove_luns); 2676EXPORT_SYMBOL_GPL(fsg_common_remove_luns);
2683 2677
2684void fsg_common_set_ops(struct fsg_common *common,
2685 const struct fsg_operations *ops)
2686{
2687 common->ops = ops;
2688}
2689EXPORT_SYMBOL_GPL(fsg_common_set_ops);
2690
2691void fsg_common_free_buffers(struct fsg_common *common) 2678void fsg_common_free_buffers(struct fsg_common *common)
2692{ 2679{
2693 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers); 2680 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers);
diff --git a/drivers/usb/gadget/function/f_mass_storage.h b/drivers/usb/gadget/function/f_mass_storage.h
index d3902313b8ac..dc05ca0c4359 100644
--- a/drivers/usb/gadget/function/f_mass_storage.h
+++ b/drivers/usb/gadget/function/f_mass_storage.h
@@ -60,17 +60,6 @@ struct fsg_module_parameters {
60struct fsg_common; 60struct fsg_common;
61 61
62/* FSF callback functions */ 62/* FSF callback functions */
63struct fsg_operations {
64 /*
65 * Callback function to call when thread exits. If no
66 * callback is set or it returns value lower then zero MSF
67 * will force eject all LUNs it operates on (including those
68 * marked as non-removable or with prevent_medium_removal flag
69 * set).
70 */
71 int (*thread_exits)(struct fsg_common *common);
72};
73
74struct fsg_lun_opts { 63struct fsg_lun_opts {
75 struct config_group group; 64 struct config_group group;
76 struct fsg_lun *lun; 65 struct fsg_lun *lun;
@@ -142,9 +131,6 @@ void fsg_common_remove_lun(struct fsg_lun *lun);
142 131
143void fsg_common_remove_luns(struct fsg_common *common); 132void fsg_common_remove_luns(struct fsg_common *common);
144 133
145void fsg_common_set_ops(struct fsg_common *common,
146 const struct fsg_operations *ops);
147
148int fsg_common_create_lun(struct fsg_common *common, struct fsg_lun_config *cfg, 134int fsg_common_create_lun(struct fsg_common *common, struct fsg_lun_config *cfg,
149 unsigned int id, const char *name, 135 unsigned int id, const char *name,
150 const char **name_pfx); 136 const char **name_pfx);
diff --git a/drivers/usb/gadget/legacy/mass_storage.c b/drivers/usb/gadget/legacy/mass_storage.c
index e99ab57ee3e5..fcba59782f26 100644
--- a/drivers/usb/gadget/legacy/mass_storage.c
+++ b/drivers/usb/gadget/legacy/mass_storage.c
@@ -107,15 +107,6 @@ static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
107 107
108FSG_MODULE_PARAMETERS(/* no prefix */, mod_data); 108FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
109 109
110static unsigned long msg_registered;
111static void msg_cleanup(void);
112
113static int msg_thread_exits(struct fsg_common *common)
114{
115 msg_cleanup();
116 return 0;
117}
118
119static int msg_do_config(struct usb_configuration *c) 110static int msg_do_config(struct usb_configuration *c)
120{ 111{
121 struct fsg_opts *opts; 112 struct fsg_opts *opts;
@@ -154,9 +145,6 @@ static struct usb_configuration msg_config_driver = {
154 145
155static int msg_bind(struct usb_composite_dev *cdev) 146static int msg_bind(struct usb_composite_dev *cdev)
156{ 147{
157 static const struct fsg_operations ops = {
158 .thread_exits = msg_thread_exits,
159 };
160 struct fsg_opts *opts; 148 struct fsg_opts *opts;
161 struct fsg_config config; 149 struct fsg_config config;
162 int status; 150 int status;
@@ -173,8 +161,6 @@ static int msg_bind(struct usb_composite_dev *cdev)
173 if (status) 161 if (status)
174 goto fail; 162 goto fail;
175 163
176 fsg_common_set_ops(opts->common, &ops);
177
178 status = fsg_common_set_cdev(opts->common, cdev, config.can_stall); 164 status = fsg_common_set_cdev(opts->common, cdev, config.can_stall);
179 if (status) 165 if (status)
180 goto fail_set_cdev; 166 goto fail_set_cdev;
@@ -256,18 +242,12 @@ MODULE_LICENSE("GPL");
256 242
257static int __init msg_init(void) 243static int __init msg_init(void)
258{ 244{
259 int ret; 245 return usb_composite_probe(&msg_driver);
260
261 ret = usb_composite_probe(&msg_driver);
262 set_bit(0, &msg_registered);
263
264 return ret;
265} 246}
266module_init(msg_init); 247module_init(msg_init);
267 248
268static void msg_cleanup(void) 249static void __exit msg_cleanup(void)
269{ 250{
270 if (test_and_clear_bit(0, &msg_registered)) 251 usb_composite_unregister(&msg_driver);
271 usb_composite_unregister(&msg_driver);
272} 252}
273module_exit(msg_cleanup); 253module_exit(msg_cleanup);