diff options
| author | Alan Stern <stern@rowland.harvard.edu> | 2026-09-20 17:39:46 -0400 |
|---|---|---|
| committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2026-10-01 07:15:16 +0200 |
| commit | 4ea75eeaf4e55ef0447037f552abbaba4af06144 (patch) | |
| tree | 2210b0229e2198e2ef24825e51ebc7077128e9d5 | |
| parent | 20b73dc5c5d2f5b0800bff62cd4586d3472b4dc6 (diff) | |
USB: gadgetfs: Fix races and locking in asynchronous I/O
It has been known for a long time that the asynchronous I/O handling
in the gadgetfs driver is subject to multiple races and locking
issues. Even the very first version of AIO support -- see:
https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git/commit/drivers/usb/gadget/inode.c?id=75787d943ab3770270c6c3f6e154ab8e59f693ef
-- had weird locking in ep_aio_cancel(), a tacit acknowledgment that
it wasn't being done correctly. (I vaguely recall some discussion
about this on the linux-usb-devel mailing list, eventually concluding
that the problem was too hard to solve then. See for example these
threads:
https://marc.info/?l=linux-usb-devel&m=118530826204776&w=2
https://marc.info/?l=linux-usb-devel&m=119129049420190&w=2
Note: usbfs2 was an attempt to update the usbfs API. It never went
anywhere.)
Regardless, it's well past time to fix all these issues. Minseo Kim
has done very thorough testing, and all the races he was able to find
are fixed by this patch. So are the lockdep violations.
The key is keeping explicit track of the various stages of
asynchronous URB submission/completion as well as of AIO cancellation,
because bad interactions between those two are responsible for the
races. Doing this required adding a new spinlock to serialize access
to the new state variables, as well as making local copies of values
belonging to a private data structure that could be deallocated at
unexpected times.
Fixing the lockdep problems required moving the usb_unlink_urb()
operation to a separate worker thread (see the comment added to
ep_aio_cancel()). Aside from that, the overall structure of the code
was not changed very much, although there is quite a lot of churn in
several of the AIO-handling subroutines.
Reported-by: Minseo Kim <neck3922@gmail.com>
Closes: https://lore.kernel.org/CAFmvuTWc880R=mw7=kixfx3Yb8u9thLHPA7D=rm6a8p9dYBBDA@mail.gmail.com/
Tested-by: Minseo Kim <neck3922@gmail.com>
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Link: https://patch.msgid.link/c06cb937-6872-401a-8c94-1944f2838007@rowland.harvard.edu
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
| -rw-r--r-- | drivers/usb/gadget/legacy/inode.c | 184 |
1 files changed, 151 insertions, 33 deletions
diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c index 67c6ffaf4f72..257ba1f596cf 100644 --- a/drivers/usb/gadget/legacy/inode.c +++ b/drivers/usb/gadget/legacy/inode.c @@ -236,6 +236,8 @@ static void put_ep (struct ep_data *data) static const char *CHIP; static DEFINE_MUTEX(sb_mutex); /* Serialize superblock operations */ +static DEFINE_SPINLOCK(aio_lock); /* Protect aio cancellation info */ + /*----------------------------------------------------------------------*/ /* NOTE: don't use dev_printk calls before binding to the gadget @@ -433,40 +435,104 @@ static long ep_ioctl(struct file *fd, unsigned code, unsigned long value) /* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */ +enum aio_req_state { + AIO_SUBMITTING, + AIO_RUNNING, + AIO_COMPLETED, + AIO_GIVEN_BACK, +}; + +enum aio_cancel_state { + AIO_NOT_CANCELLED, + AIO_UNLINKING, + AIO_UNLINK_DONE, +}; + struct kiocb_priv { struct usb_request *req; struct ep_data *epdata; + struct usb_ep *ep; struct kiocb *iocb; struct mm_struct *mm; - struct work_struct work; + struct work_struct copy_work; + struct work_struct unlink_work; void *buf; struct iov_iter to; const void *to_free; unsigned actual; + enum aio_req_state req_state; + enum aio_cancel_state cancel_state; }; -static int ep_aio_cancel(struct kiocb *iocb) +static void ep_unlink_worker(struct work_struct *work) { - struct kiocb_priv *priv = iocb->private; + struct kiocb_priv *priv; + struct usb_request *req; struct ep_data *epdata; - int value; + struct usb_ep *ep; + enum aio_req_state req_state; - local_irq_disable(); + priv = container_of(work, struct kiocb_priv, unlink_work); + req = priv->req; epdata = priv->epdata; - // spin_lock(&epdata->dev->lock); - if (likely(epdata && epdata->ep && priv->req)) - value = usb_ep_dequeue (epdata->ep, priv->req); - else - value = -EINVAL; - // spin_unlock(&epdata->dev->lock); - local_irq_enable(); + ep = priv->ep; - return value; + usb_ep_dequeue(ep, req); + + spin_lock_irq(&aio_lock); + req_state = priv->req_state; + priv->cancel_state = AIO_UNLINK_DONE; + spin_unlock_irq(&aio_lock); + + /* + * req and epdata are freed after unlinking and completion are both done. + * priv is freed after unlinking and giveback are both done. + */ + if (req_state >= AIO_COMPLETED) { + /* priv may have been freed already, depending on req_state */ + usb_ep_free_request(ep, req); + put_ep(epdata); + if (req_state == AIO_GIVEN_BACK) + kfree(priv); + } +} + +static int ep_aio_cancel(struct kiocb *iocb) +{ + struct kiocb_priv *priv; + unsigned long flags; + + spin_lock_irqsave(&aio_lock, flags); + priv = iocb->private; + if (!priv || priv->cancel_state != AIO_NOT_CANCELLED) { + spin_unlock_irqrestore(&aio_lock, flags); + return -EINVAL; /* Already completed or cancelled */ + } + if (priv->req_state == AIO_SUBMITTING) { + priv->cancel_state = AIO_UNLINK_DONE; + 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. + * usb_ep_dequeue() is allowed to run synchronously, calling the + * completion handler ep_aio_complete() before it returns. + * But ep_aio_complete() may call iocb->io_complete(), which + * tries to acquire the context lock, leading to deadlock. + * For this reason, do the dequeue operation in a work routine. + */ + INIT_WORK(&priv->unlink_work, ep_unlink_worker); + schedule_work(&priv->unlink_work); + return 0; } static void ep_user_copy_worker(struct work_struct *work) { - struct kiocb_priv *priv = container_of(work, struct kiocb_priv, work); + struct kiocb_priv *priv = container_of(work, struct kiocb_priv, copy_work); struct mm_struct *mm = priv->mm; struct kiocb *iocb = priv->iocb; size_t ret; @@ -488,19 +554,25 @@ static void ep_user_copy_worker(struct work_struct *work) kfree(priv->buf); kfree(priv->to_free); - kfree(priv); + + spin_lock_irq(&aio_lock); + priv->req_state = AIO_GIVEN_BACK; + if (priv->cancel_state != AIO_UNLINKING) + kfree(priv); + spin_unlock_irq(&aio_lock); } static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req) { struct kiocb *iocb = req->context; struct kiocb_priv *priv = iocb->private; - struct ep_data *epdata = priv->epdata; + enum aio_req_state new_req_state; + enum aio_cancel_state cancel_state; - /* lock against disconnect (and ideally, cancel) */ - spin_lock(&epdata->dev->lock); - priv->req = NULL; - priv->epdata = NULL; + /* Prevent future cancellation */ + spin_lock(&aio_lock); + iocb->private = NULL; + spin_unlock(&aio_lock); /* if this was a write or a read returning no data then we * don't need to copy anything to userspace, so we can @@ -510,25 +582,35 @@ static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req) mmdrop(priv->mm); kfree(req->buf); kfree(priv->to_free); - kfree(priv); - iocb->private = NULL; iocb->ki_complete(iocb, req->actual ? req->actual : (long)req->status); + new_req_state = AIO_GIVEN_BACK; } else { /* ep_copy_to_user() won't report both; we hide some faults */ if (unlikely(0 != req->status)) - DBG(epdata->dev, "%s fault %d len %d\n", + DBG(priv->epdata->dev, "%s fault %d len %d\n", ep->name, req->status, req->actual); priv->buf = req->buf; priv->actual = req->actual; - INIT_WORK(&priv->work, ep_user_copy_worker); - schedule_work(&priv->work); + new_req_state = AIO_COMPLETED; } - usb_ep_free_request(ep, req); - spin_unlock(&epdata->dev->lock); - put_ep(epdata); + spin_lock(&aio_lock); + priv->req_state = new_req_state; + cancel_state = priv->cancel_state; + spin_unlock(&aio_lock); + + if (new_req_state == AIO_COMPLETED) { + INIT_WORK(&priv->copy_work, ep_user_copy_worker); + schedule_work(&priv->copy_work); + } + if (cancel_state != AIO_UNLINKING) { + usb_ep_free_request(ep, req); + put_ep(priv->epdata); + if (new_req_state == AIO_GIVEN_BACK) + kfree(priv); + } } static ssize_t ep_aio(struct kiocb *iocb, @@ -539,11 +621,12 @@ static ssize_t ep_aio(struct kiocb *iocb, { struct usb_request *req; ssize_t value; + struct usb_ep *ep; + bool need_unlink = false; iocb->private = priv; priv->iocb = iocb; - kiocb_set_cancel_fn(iocb, ep_aio_cancel); get_ep(epdata); priv->epdata = epdata; priv->actual = 0; @@ -555,10 +638,12 @@ static ssize_t ep_aio(struct kiocb *iocb, */ spin_lock_irq(&epdata->dev->lock); value = -ENODEV; - if (unlikely(epdata->ep == NULL)) + ep = epdata->ep; + if (unlikely(ep == NULL)) goto fail; + priv->ep = ep; - req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC); + req = usb_ep_alloc_request(ep, GFP_ATOMIC); value = -ENOMEM; if (unlikely(!req)) goto fail; @@ -568,12 +653,45 @@ static ssize_t ep_aio(struct kiocb *iocb, req->length = len; req->complete = ep_aio_complete; req->context = iocb; - value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC); + + priv->req_state = AIO_SUBMITTING; + priv->cancel_state = AIO_NOT_CANCELLED; + + /* Not allowed to manipulate the aio context while holding dev->lock */ + ++epdata->dev->udc_usage; + spin_unlock_irq(&epdata->dev->lock); + + kiocb_set_cancel_fn(iocb, ep_aio_cancel); + value = usb_ep_queue(ep, req, GFP_KERNEL); + + spin_lock_irq(&epdata->dev->lock); + --epdata->dev->udc_usage; + if (unlikely(0 != value)) { - usb_ep_free_request(epdata->ep, req); + spin_lock(&aio_lock); + iocb->private = NULL; + spin_unlock(&aio_lock); + + usb_ep_free_request(ep, req); goto fail; } spin_unlock_irq(&epdata->dev->lock); + + spin_lock_irq(&aio_lock); + if (iocb->private != NULL) { + priv->req_state = AIO_RUNNING; + + /* Cancelled before or just after submission? */ + if (priv->cancel_state == AIO_UNLINK_DONE) { + priv->cancel_state = AIO_NOT_CANCELLED; + need_unlink = true; + } + } /* Otherwise already completed */ + spin_unlock_irq(&aio_lock); + + if (need_unlink) /* Redo cancel that was too early */ + ep_aio_cancel(iocb); + return -EIOCBQUEUED; fail: |
