From: Ingo Rohloff <ingo.rohloff@lauterbach.com>
To: Neill Kapron <nkapron@google.com>
Cc: gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk,
me@samcday.com, michael.bommarito@gmail.com,
linux-usb@vger.kernel.org
Subject: Re: [PATCH v2 2/2] usb: gadget: f_fs: register cancellation handler only after request queueing
Date: Tue, 17 Nov 2026 12:21:53 +0100 [thread overview]
Message-ID: <20261117122153.51dd826a@ingpc2.intern.lauterbach.com> (raw)
In-Reply-To: <179487196868.2938203.4555043762269001898.b4-review@b4>
Hello Neill,
> > @@ -887,7 +888,9 @@ static void ffs_user_copy_worker(struct work_struct *work)
> > mmdrop(io_data->mm);
> > }
> >
> > + spin_lock(&io_data->aio_completion_lock);
> > io_data->kiocb->ki_complete(io_data->kiocb, ret);
> > + spin_unlock(&io_data->aio_completion_lock);
>
> ffs_epfile_io() grabs the lock while holding ffs->eps_lock in interrupt
> context. Therefore, I think this could lead to lock inversion if we
> don't call with with spin_lock_irq() and a spin_unlock_irq(). Could you
> send a v3 with _irq() or the save/restore equivalents here?
I can change that to the "_irq()" variants (this should be harmless),
but I would like to understand why.
"ffs_epfile_io()" itself does not run in interrupt context.
That's also why it uses "spin_lock_irq(&epfile->ffs->eps_lock);",
unconditionally blocking IRQs.
The code part in "ffs_epfile_io()", which calls
"spin_lock(&io_data->aio_completion_lock);" runs with local interrupts disabled,
which also means it cannot be interrupted, which in turn means it will reach
"spin_unlock(&io_data->aio_completion_lock);" un-interrupted.
"ffs_user_copy_worker()" is scheduled via a regular work queue.
As far as I understand this means it's also not running in interrupt context.
So the potential danger here is, that in the code part:
spin_lock(&io_data->aio_completion_lock);
io_data->kiocb->ki_complete(io_data->kiocb, ret);
spin_unlock(&io_data->aio_completion_lock);
An interrupt happens after the "spin_lock" has been executed (so the code
above is interrupted) and the code for the interrupt also wants to grab
"&io_data->aio_completion_lock".
But there is no interrupt code which uses "&io_data->aio_completion_lock".
As I said: ffs_epfile_io() is not running in interrupt context.
There is also a second reason why I can't see how there might be a problem:
"ffs_user_copy_worker()" can only run, once the request to the USB Controller
has been scheduled via
ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
in the "ffs_epfile_io()" function. But right before this happens
"ffs_epfile_io()" grabs "&io_data->aio_completion_lock".
Meaning when "ffs_user_copy_worker()" reaches
"spin_lock(&io_data->aio_completion_lock)", there are only two possibilities:
1) "&io_data->aio_completion_lock" is still taken by "ffs_epfile_io()"
=> That's the whole reason why this lock exists:
Preventing "io_data->kiocb->ki_complete(io_data->kiocb, ret);" to run
before "ffs_epfile_io()" has called
"kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);"
2) "&io_data->aio_completion_lock" has already been released.
In this case the lock will not be taken a second time by "ffs_epfile_io()",
so there is no problem.
with best regards
Ingo
On Mon, 16 Nov 2026 23:32:48 +0000
Neill Kapron <nkapron@google.com> wrote:
> > Call kiocb_set_cancel_fn() only after usb_ep_queue() completes
> > successfully.
> >
> > This also undoes commit e78dcb1f7ec2 ("usb: gadget: f_fs: Fix
> > Use-After-Free in AIO error path"): The described error is avoided by
> > making sure the cancellation handler is only registered, if there is
> > something to cancel.
> >
> > Registering the cancellation handler adds the kiocb to the context's
> > active request list (active_reqs in aio.c). If usb_ep_queue() fails, the
> > request was never actually submitted to the hardware, which means the
> > request is not active. The aio framework already deals with calling the
> > completion function ki_complete() automatically if you do not return
> > -EIOCBQUEUED.
> >
> > Signed-off-by: Ingo Rohloff <ingo.rohloff@lauterbach.com>
>
> Thanks Ingo, this is cleaner than the -EIOCBQUEUED hack in my original
> approach. Can you add Fixes: and Cc: stable tags on a v3 addressing the
> feedback below?
>
> > ---
> > drivers/usb/gadget/function/f_fs.c | 70 +++++++++++++++---------------
> > 1 file changed, 34 insertions(+), 36 deletions(-)
> >
> > diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
> > index d3fd1dfcd454..135c2979f737 100644
> > --- a/drivers/usb/gadget/function/f_fs.c
> > +++ b/drivers/usb/gadget/function/f_fs.c
> > @@ -264,6 +264,7 @@ struct ffs_io_data {
> >
> > int status;
> > struct completion done;
> > + spinlock_t aio_completion_lock;
> > };
> >
> > struct ffs_desc_helper {
> > @@ -887,7 +888,9 @@ static void ffs_user_copy_worker(struct work_struct *work)
> > mmdrop(io_data->mm);
> > }
> >
> > + spin_lock(&io_data->aio_completion_lock);
> > io_data->kiocb->ki_complete(io_data->kiocb, ret);
> > + spin_unlock(&io_data->aio_completion_lock);
>
> ffs_epfile_io() grabs the lock while holding ffs->eps_lock in interrupt
> context. Therefore, I think this could lead to lock inversion if we
> don't call with with spin_lock_irq() and a spin_unlock_irq(). Could you
> send a v3 with _irq() or the save/restore equivalents here?
>
--
-------------------------------------------------------------------------
Dipl.-Inform.
Ingo ROHLOFF
Senior Staff Engineer
phone +49 8102 9876-142 - ingo.rohloff@lauterbach.com
Lauterbach Engineering GmbH & Co. KG
Altlaufstrasse 40, 85635 Hoehenkirchen-Siegertsbrunn, GERMANY
www.lauterbach.com
Registered Office: Hoehenkirchen-Siegertsbrunn, Germany,
Local Court: Munich, HRA 87406, VAT ID: DE246770537,
Managing Directors: Stephan Lauterbach, Dr. Thomas Ullmann
-------------------------------------------------------------------------
next prev parent reply other threads:[~2026-11-17 11:22 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-10 17:12 [PATCH v2 0/2] usb: gadget: f_fs: cancellation fix Ingo Rohloff
2026-11-10 17:12 ` [PATCH v2 1/2] usb: gadget: f_fs: simplify error handling using goto Ingo Rohloff
2026-11-10 17:12 ` [PATCH v2 2/2] usb: gadget: f_fs: register cancellation handler only after request queueing Ingo Rohloff
2026-11-16 23:32 ` Neill Kapron
2026-11-17 11:21 ` Ingo Rohloff [this message]
2026-11-17 23:14 ` Neill Kapron
2026-11-18 13:00 ` Ingo Rohloff
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261117122153.51dd826a@ingpc2.intern.lauterbach.com \
--to=ingo.rohloff@lauterbach.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-usb@vger.kernel.org \
--cc=me@samcday.com \
--cc=michael.bommarito@gmail.com \
--cc=nkapron@google.com \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox