diff options
| author | Alan Stern <stern@rowland.harvard.edu> | 2026-09-20 17:43:36 -0400 |
|---|---|---|
| committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2026-10-01 07:15:16 +0200 |
| commit | dfb7052396e3591c0ff8212e7d1eb86f403412e5 (patch) | |
| tree | 661c1cf1e71f5da2d78db4d9de9e87990f392be4 /drivers | |
| parent | 6f41c4ff394f502d9ed7b15ce862bc494f553a6f (diff) | |
USB: gadgetfs: Wait for work routines before unloading
The gadgetfs driver uses work routines to handle asynchronous I/O,
but it doesn't make certain that the work routines have completed
before it allows itself to be unloaded from memory (if it is built as
a module). Testing shows that under the right conditions this can
actually happen.
Fix this problem by creating a dedicated workqueue for the driver's
work routines. Automatic flushing when the workqueue is destroyed
will ensure that no work routines are still in operation when the
driver is unloaded.
Tested-by: Minseo Kim <neck3922@gmail.com>
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Link: https://patch.msgid.link/2f657ef5-6ea0-4820-9579-324b9b812417@rowland.harvard.edu
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers')
| -rw-r--r-- | drivers/usb/gadget/legacy/inode.c | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c index a3d86327a987..d5cec67bab17 100644 --- a/drivers/usb/gadget/legacy/inode.c +++ b/drivers/usb/gadget/legacy/inode.c @@ -237,6 +237,7 @@ static const char *CHIP; static DEFINE_MUTEX(sb_mutex); /* Serialize superblock operations */ static DEFINE_SPINLOCK(aio_lock); /* Protect aio cancellation info */ +static struct workqueue_struct *gadgetfs_wq; /*----------------------------------------------------------------------*/ @@ -514,9 +515,7 @@ static int ep_aio_cancel(struct kiocb *iocb) spin_unlock_irqrestore(&aio_lock, flags); return 0; /* ep_aio() will call us again if needed */ } - priv->cancel_state = AIO_UNLINKING; - spin_unlock_irqrestore(&aio_lock, flags); /* * We are called with the aio core holding iocb's context lock. @@ -527,7 +526,9 @@ static int ep_aio_cancel(struct kiocb *iocb) * For this reason, do the dequeue operation in a work routine. */ INIT_WORK(&priv->unlink_work, ep_unlink_worker); - schedule_work(&priv->unlink_work); + queue_work(gadgetfs_wq, &priv->unlink_work); + + spin_unlock_irqrestore(&aio_lock, flags); return 0; } @@ -604,7 +605,7 @@ static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req) if (new_req_state == AIO_COMPLETED) { INIT_WORK(&priv->copy_work, ep_user_copy_worker); - schedule_work(&priv->copy_work); + queue_work(gadgetfs_wq, &priv->copy_work); } if (cancel_state != AIO_UNLINKING) { usb_ep_free_request(ep, req); @@ -2255,10 +2256,17 @@ static int __init gadgetfs_init (void) { int status; + gadgetfs_wq = alloc_workqueue("gadgetfs", WQ_PERCPU, 0); + if (!gadgetfs_wq) + return -ENOMEM; + status = register_filesystem (&gadgetfs_type); - if (status == 0) - pr_info ("%s: %s, version " DRIVER_VERSION "\n", - shortname, driver_desc); + if (status) { + destroy_workqueue(gadgetfs_wq); + return status; + } + + pr_info("%s: %s, version " DRIVER_VERSION "\n", shortname, driver_desc); return status; } module_init (gadgetfs_init); @@ -2267,6 +2275,7 @@ static void __exit gadgetfs_cleanup (void) { pr_debug ("unregister %s\n", shortname); unregister_filesystem (&gadgetfs_type); + destroy_workqueue(gadgetfs_wq); } module_exit (gadgetfs_cleanup); |
