Skip to contentSkip to conversation

usb: gadget: f_fs: cancellation fix

PATCH Superseded Ingo Rohloff 2 participants 7 messages Nov 10 – Nov 18 USB

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover usb: gadget: f_fs: cancellation fix

This 2 part patch series does some mini refactoring and fixes a previous commits.

---
Changes in v2:
- First patch "Fix mmgrab()/mmdrop() balance." now posted separately.
- Fix comment style and whitespace as requested by
Neill Kapron <[email protected]>
- Make sure io_data->kiocb->ki_complete() == aio_complete_rw() can't be called before kiocb_set_cancel_fn() has been called. That should it make safe to call kiocb_set_cancel_fn() after usb_ep_queue().

Link to v1: https://lore.kernel.org/r/[email protected]

---
Ingo Rohloff (2):
usb: gadget: f_fs: simplify error handling using goto
usb: gadget: f_fs: register cancellation handler only after request
queueing

 drivers/usb/gadget/function/f_fs.c | 140 ++++++++++++++++-------------
 1 file changed, 76 insertions(+), 64 deletions(-)

--
2.55.0

#1 lore raw

PATCH v2 1/2 usb: gadget: f_fs: simplify error handling using goto

Refactor conditional checks to use direct error returns/gotos instead of a long if/else chain. This makes failure paths more obvious.

At the end of the function AIO request handling is done.

---
 drivers/usb/gadget/function/f_fs.c | 82 +++++++++++++++++-------------
 1 file changed, 48 insertions(+), 34 deletions(-)
drivers/usb/gadget/function/f_fs.c +48 -34
index 704cfae49f11..e347916923df 100644
@@ -1104,11 +1104,17 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
 	if (epfile->ep != ep) {
 		/* In the meantime, endpoint got disabled or changed. */
 		ret = -ESHUTDOWN;
-	} else if (halt) {
+		goto error_lock;
+	}
+
+	if (halt) {
 		ret = usb_ep_set_halt(ep->ep);
 		if (!ret)
 			ret = -EBADMSG;
-	} else if (data_len == -EINVAL) {
+		goto error_lock;
+	}
+
+	if (data_len == -EINVAL) {
 		/*
 		 * Sanity Check: even though data_len can't be used
 		 * uninitialized at the time I write this comment, some
@@ -1122,7 +1128,10 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
 		 */
 		WARN(1, "%s: data_len == -EINVAL\n", __func__);
 		ret = -EINVAL;
-	} else if (!io_data->aio) {
+		goto error_lock;
+	}
+
+	if (!io_data->aio) {
 		bool interrupted = false;
 
 		req = ep->req;
@@ -1141,7 +1150,7 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
 		io_data->buf = data;
 
 		init_completion(&io_data->done);
-		req->context  = io_data;
+		req->context = io_data;
 		req->complete = ffs_epfile_io_complete;
 
 		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
@@ -1176,44 +1185,49 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
 		else
 			ret = io_data->status;
 		goto error_mutex;
-	} else if (!(req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC))) {
+	}
+
+	/* It's an AIO request */
+	req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC);
+	if (!req) {
 		ret = -ENOMEM;
-	} else {
-		if (io_data->use_sg) {
-			req->buf = NULL;
-			req->sg	= io_data->sgt.sgl;
-			req->num_sgs = io_data->sgt.nents;
-		} else {
-			req->buf = data;
-			req->num_sgs = 0;
-		}
+		goto error_lock;
+	}
 
-		req->zero = !io_data->read ? epfile->zlp_enabled : 0;
-		req->length = data_len;
+	if (io_data->use_sg) {
+		req->buf = NULL;
+		req->sg = io_data->sgt.sgl;
+		req->num_sgs = io_data->sgt.nents;
+	} else {
+		req->buf = data;
+		req->num_sgs = 0;
+	}
 
-		io_data->buf = data;
-		io_data->ep = ep->ep;
-		io_data->req = req;
-		io_data->ffs = epfile->ffs;
+	req->zero = !io_data->read ? epfile->zlp_enabled : 0;
+	req->length = data_len;
 
-		req->context  = io_data;
-		req->complete = ffs_epfile_async_io_complete;
+	io_data->buf = data;
+	io_data->ep = ep->ep;
+	io_data->req = req;
+	io_data->ffs = epfile->ffs;
 
-		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
-		if (ret) {
-			io_data->req = NULL;
-			usb_ep_free_request(ep->ep, req);
-			goto error_lock;
-		}
+	req->context  = io_data;
+	req->complete = ffs_epfile_async_io_complete;
 
-		ret = -EIOCBQUEUED;
-		/*
-		 * Do not kfree the buffer in this function.  It will be freed
-		 * by ffs_user_copy_worker.
-		 */
-		data = NULL;
+	ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
+	if (ret) {
+		io_data->req = NULL;
+		usb_ep_free_request(ep->ep, req);
+		goto error_lock;
 	}
 
+	ret = -EIOCBQUEUED;
+	/*
+	 * Do not kfree the buffer in this function.  It will be freed
+	 * by ffs_user_copy_worker.
+	 */
+	data = NULL;
+
 error_lock:
 	spin_unlock_irq(&epfile->ffs->eps_lock);
 error_mutex:
Signature · 1 line
2.55.0
#2 lore raw

PATCH v2 2/2 usb: gadget: f_fs: register cancellation handler only after request queueing

In reply to Ingo Rohloff

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.

---
 drivers/usb/gadget/function/f_fs.c | 70 +++++++++++++++---------------
 1 file changed, 34 insertions(+), 36 deletions(-)
drivers/usb/gadget/function/f_fs.c +34 -36
index e347916923df..d40059eebd49 100644
@@ -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);
 
 	if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd)
 		eventfd_signal(io_data->ffs->ffs_eventfd);
@@ -1014,8 +1017,22 @@ static struct ffs_ep *ffs_epfile_wait_ep(struct ffs_epfile *epfile, struct file
 	return ep;
 }
 
-static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
+static int ffs_aio_cancel(struct kiocb *kiocb)
+{
+	struct ffs_io_data *io_data = kiocb->private;
+	int value;
+
+	if (!io_data || !io_data->ep || !io_data->req)
+		return -EINVAL;
+
+	value = usb_ep_dequeue(io_data->ep, io_data->req);
+	return value;
+}
+
+static ssize_t ffs_epfile_io(struct ffs_io_data *io_data)
 {
+	struct kiocb *kiocb = io_data->kiocb;
+	struct file *file = kiocb->ki_filp;
 	struct ffs_epfile *epfile = file->private_data;
 	struct usb_request *req;
 	struct ffs_ep *ep;
@@ -1210,24 +1227,30 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
 	io_data->ep = ep->ep;
 	io_data->req = req;
 	io_data->ffs = epfile->ffs;
+	spin_lock_init(&io_data->aio_completion_lock);
 
 	req->context  = io_data;
 	req->complete = ffs_epfile_async_io_complete;
 
+	/*
+	 * prevent ffs_user_copy_worker from calling
+	 *    io_data->kiocb->ki_complete
+	 * before kiocb_set_cancel_fn was called.
+	 */
+	spin_lock(&io_data->aio_completion_lock);
 	ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
 	if (ret) {
 		io_data->req = NULL;
 		usb_ep_free_request(ep->ep, req);
-		goto error_lock;
+		goto aio_completion_lock;
 	}
+	kiocb->private = io_data;
+	kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
 
 	ret = -EIOCBQUEUED;
-	/*
-	 * Do not kfree the buffer in this function.  It will be freed
-	 * by ffs_user_copy_worker.
-	 */
-	data = NULL;
 
+aio_completion_lock:
+	spin_unlock(&io_data->aio_completion_lock);
 error_lock:
 	spin_unlock_irq(&epfile->ffs->eps_lock);
 error_mutex:
@@ -1266,19 +1289,6 @@ ffs_epfile_open(struct inode *inode, struct file *file)
 	return stream_open(inode, file);
 }
 
-static int ffs_aio_cancel(struct kiocb *kiocb)
-{
-	struct ffs_io_data *io_data = kiocb->private;
-	int value;
-
-	if (io_data && io_data->ep && io_data->req)
-		value = usb_ep_dequeue(io_data->ep, io_data->req);
-	else
-		value = -EINVAL;
-
-	return value;
-}
-
 static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)
 {
 	struct ffs_io_data io_data, *p = &io_data;
@@ -1299,21 +1309,15 @@ static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)
 	p->data = *from;
 	p->mm = current->mm;
 
-	kiocb->private = p;
-
-	if (p->aio) {
+	if (p->aio)
 		mmgrab(p->mm);
-		kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
-	}
 
-	res = ffs_epfile_io(kiocb->ki_filp, p);
+	res = ffs_epfile_io(p);
 	if (res == -EIOCBQUEUED)
 		return res;
 	if (p->aio) {
-		kiocb->ki_complete(kiocb, res);
 		mmdrop(p->mm);
 		kfree(p);
-		return -EIOCBQUEUED;
 	} else {
 		*from = p->data;
 	}
@@ -1349,23 +1353,17 @@ static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
 	}
 	p->mm = current->mm;
 
-	kiocb->private = p;
-
-	if (p->aio) {
+	if (p->aio)
 		mmgrab(p->mm);
-		kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
-	}
 
-	res = ffs_epfile_io(kiocb->ki_filp, p);
+	res = ffs_epfile_io(p);
 	if (res == -EIOCBQUEUED)
 		return res;
 
 	if (p->aio) {
-		kiocb->ki_complete(kiocb, res);
 		mmdrop(p->mm);
 		kfree(p->to_free);
 		kfree(p);
-		return -EIOCBQUEUED;
 	} else {
 		*to = p->data;
 	}
Signature · 1 line
2.55.0
#3 lore raw

Re: [PATCH v2 2/2] usb: gadget: f_fs: register cancellation handler only after request queueing

Quoted 8 lines

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.

Quoted 8 lines

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.

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?

Quoted 17 lines
---
 drivers/usb/gadget/function/f_fs.c | 70 +++++++++++++++---------------
 1 file changed, 34 insertions(+), 36 deletions(-)
drivers/usb/gadget/function/f_fs.c +1 -0
index d3fd1dfcd454..135c2979f737 100644
@@ -264,6 +264,7 @@ struct ffs_io_data {
 
 	int status;
 	struct completion done;
+	spinlock_t aio_completion_lock;
 };
 
 struct ffs_desc_helper {

drivers/usb/gadget/function/f_fs.c static void ffs_user_copy_worker(struct work_struct *work) · line 893

Quoted 6 lines
drivers/usb/gadget/function/f_fs.c +2 -0
@@ -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?

Signature · 1 line
Neill Kapron <[email protected]>
#4 lore raw

Re: [PATCH v2 2/2] usb: gadget: f_fs: register cancellation handler only after request queueing

Hello Neill,

Quoted 4 lines
Quoted 4 lines
drivers/usb/gadget/function/f_fs.c +0 -0
@@ -887,7 +888,9 @@ static void ffs_user_copy_worker(struct work_struct *work)
 		mmdrop(io_data->mm);
 	}
 
Quoted 8 lines

drivers/usb/gadget/function/f_fs.c static void ffs_user_copy_worker(struct work_struct *work) · line 893

Quoted 3 lines
drivers/usb/gadget/function/f_fs.c +2 -0
+	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

Quoted 50 lines · On Mon, 16 Nov 2026 23:32:48 +0000 Neill Kapron <[email protected]> wrote:
Quoted 8 lines

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.

Quoted 8 lines

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.

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?

Quoted 17 lines
---
 drivers/usb/gadget/function/f_fs.c | 70 +++++++++++++++---------------
 1 file changed, 34 insertions(+), 36 deletions(-)
drivers/usb/gadget/function/f_fs.c +1 -0
index d3fd1dfcd454..135c2979f737 100644
@@ -264,6 +264,7 @@ struct ffs_io_data {
 
 	int status;
 	struct completion done;
+	spinlock_t aio_completion_lock;
 };
 
 struct ffs_desc_helper {

drivers/usb/gadget/function/f_fs.c static void ffs_user_copy_worker(struct work_struct *work) · line 893

Quoted 6 lines
drivers/usb/gadget/function/f_fs.c +2 -0
@@ -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?

Signature · 15 lines
-------------------------------------------------------------------------
Dipl.-Inform.
Ingo ROHLOFF
Senior Staff Engineer
phone +49 8102 9876-142 - [email protected]

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

-------------------------------------------------------------------------
#5 lore raw

Re: [PATCH v2 2/2] usb: gadget: f_fs: register cancellation handler only after request queueing

Quoted 9 lines · Ingo Rohloff

Hello Neill,

Quoted 6 lines

Review static void ffs_user_copy_worker(struct work_struct *work)

Quoted 1 line
(quoted diff) +0 -0
@@ -887,7 +888,9 @@ static void ffs_user_copy_worker(struct work_struct *work)

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?

Quoted 6 lines

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.

Hi Ingo,

Sorry for the confusion- I misinterpreted the lockdep splat. You are correct that ffs_epfile_io() does not run in interrupt context, but with lockdep enabled, we get the following trace on a VM with dummy_hcd and a simple ffs test application):

========================================================
WARNING: possible irq lock inversion dependency detected
7.4.0-rc3-g9b73231bde82 #2 Not tainted
--------------------------------------------------------
kworker/u64:0/13 just changed the state of lock:
ffff8881014ebb40 (&io_data->aio_completion_lock){+.+.}-{3:3}, at: ffs_user_copy_worker+0x4b/0x170 but this lock was taken by another, SOFTIRQ-safe lock in the past:
(&ffs->eps_lock){..-.}-{3:3}

and interrupts could create inverse lock ordering between them.

other info that might help us debug this:
Possible interrupt unsafe locking scenario:

         CPU0                    CPU1
         ----                    ----
    lock(&io_data->aio_completion_lock);
                                 local_irq_disable();
                                 lock(&ffs->eps_lock);
                                 lock(&io_data->aio_completion_lock);
    <Interrupt>
      lock(&ffs->eps_lock);
   *** DEADLOCK ***
  [...]
  the shortest dependencies between 2nd lock and 1st lock:
   -> (&ffs->eps_lock){..-.}-{3:3} {
      IN-SOFTIRQ-W at:
                        lock_acquire+0xcf/0x2e0
                        _raw_spin_lock_irqsave+0x35/0x50
                        ffs_func_set_alt+0xaf/0x2b0
                        composite_setup+0xc37/0x2000
                        configfs_composite_setup+0x73/0xa0
                        dummy_timer+0xa5c/0xe10
                        __hrtimer_run_queues+0x146/0x310
                        hrtimer_run_softirq+0x89/0xf0
  [...]
    }
    ... acquired at:
     lock_acquire+0xcf/0x2e0
     _raw_spin_lock+0x2c/0x40
     ffs_epfile_io+0x652/0x8c0
     ffs_epfile_read_iter+0x11a/0x1c0
     aio_read+0x12d/0x210
  -> (&io_data->aio_completion_lock){+.+.}-{3:3} {
     HARDIRQ-ON-W at:
                      lock_acquire+0xcf/0x2e0
                      _raw_spin_lock+0x2c/0x40
                      ffs_user_copy_worker+0x4b/0x170
                      process_one_work+0x278/0x6e0
     SOFTIRQ-ON-W at:
                      lock_acquire+0xcf/0x2e0
                      _raw_spin_lock+0x2c/0x40
                      ffs_user_copy_worker+0x4b/0x170
                      process_one_work+0x278/0x6e0
  [...]
   }

Switching ffs_user_copy_worker() to spin_lock_irq() / spin_unlock_irq() resolves the lockdep warning in testing.

Thanks,
Neill

Signature · 1 line
Neill Kapron <[email protected]>
#6 lore raw

Re: [PATCH v2 2/2] usb: gadget: f_fs: register cancellation handler only after request queueing

Hello Neill,

Thanks for the explanation!

Quoted 3 lines

with lockdep enabled, we get the following trace on a VM with dummy_hcd and a simple ffs test application:

========================================================

Quoted 12 lines

WARNING: possible irq lock inversion dependency detected
7.4.0-rc3-g9b73231bde82 #2 Not tainted
--------------------------------------------------------
kworker/u64:0/13 just changed the state of lock:
ffff8881014ebb40 (&io_data->aio_completion_lock){+.+.}-{3:3}, at: ffs_user_copy_worker+0x4b/0x170 but this lock was taken by another, SOFTIRQ-safe lock in the past:

   (&ffs->eps_lock){..-.}-{3:3}

and interrupts could create inverse lock ordering between them.

other info that might help us debug this:

Quoted 10 lines
   Possible interrupt unsafe locking scenario:

         CPU0                    CPU1
         ----                    ----
    lock(&io_data->aio_completion_lock);
                                 local_irq_disable();
                                 lock(&ffs->eps_lock);
                                 lock(&io_data->aio_completion_lock);
    <Interrupt>
      lock(&ffs->eps_lock);

Ah now I understand :-)

I still don't think this can happen, because the "lock(&io_data->aio_completion_lock);" on CPU0 can't happen before the "lock(&io_data->aio_completion_lock);" on CPU1, because the request first has to be scheduled via "usb_ep_queue()" on CPU1 as far as I can tell.

But this argument sounds rather fragile...

Quoted 2 lines

Switching ffs_user_copy_worker() to spin_lock_irq() / spin_unlock_irq() resolves the lockdep warning in testing.

If using the "_irq" variant satisfies lockdep, then it's certainly better to use the "_irq" variants; after all the whole point of lockdep is to find these kind of issues.

I will post another V3 patch variant, which will use the "_irq" variants.

with best regards
Ingo

Quoted 90 lines · On Tue, 17 Nov 2026 23:14:21 +0000 Neill Kapron <[email protected]> wrote:
Quoted 9 lines · Ingo Rohloff

Hello Neill,

Quoted 6 lines

Review static void ffs_user_copy_worker(struct work_struct *work)

Quoted 1 line
(quoted diff) +0 -0
@@ -887,7 +888,9 @@ static void ffs_user_copy_worker(struct work_struct *work)  

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?

Quoted 6 lines

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.

Hi Ingo,

Sorry for the confusion- I misinterpreted the lockdep splat. You are correct that ffs_epfile_io() does not run in interrupt context, but with lockdep enabled, we get the following trace on a VM with dummy_hcd and a simple ffs test application):

========================================================
WARNING: possible irq lock inversion dependency detected
7.4.0-rc3-g9b73231bde82 #2 Not tainted
--------------------------------------------------------
kworker/u64:0/13 just changed the state of lock:
ffff8881014ebb40 (&io_data->aio_completion_lock){+.+.}-{3:3}, at: ffs_user_copy_worker+0x4b/0x170 but this lock was taken by another, SOFTIRQ-safe lock in the past:

   (&ffs->eps_lock){..-.}-{3:3}

and interrupts could create inverse lock ordering between them.

other info that might help us debug this:

   Possible interrupt unsafe locking scenario:

         CPU0                    CPU1
         ----                    ----
    lock(&io_data->aio_completion_lock);
                                 local_irq_disable();
                                 lock(&ffs->eps_lock);
                                 lock(&io_data->aio_completion_lock);
    <Interrupt>
      lock(&ffs->eps_lock);

   *** DEADLOCK ***
  [...]
  the shortest dependencies between 2nd lock and 1st lock:
   -> (&ffs->eps_lock){..-.}-{3:3} {  
      IN-SOFTIRQ-W at:
                        lock_acquire+0xcf/0x2e0
                        _raw_spin_lock_irqsave+0x35/0x50
                        ffs_func_set_alt+0xaf/0x2b0
                        composite_setup+0xc37/0x2000
                        configfs_composite_setup+0x73/0xa0
                        dummy_timer+0xa5c/0xe10
                        __hrtimer_run_queues+0x146/0x310
                        hrtimer_run_softirq+0x89/0xf0
  [...]
    }
    ... acquired at:
     lock_acquire+0xcf/0x2e0
     _raw_spin_lock+0x2c/0x40
     ffs_epfile_io+0x652/0x8c0
     ffs_epfile_read_iter+0x11a/0x1c0
     aio_read+0x12d/0x210
  -> (&io_data->aio_completion_lock){+.+.}-{3:3} {  
     HARDIRQ-ON-W at:
                      lock_acquire+0xcf/0x2e0
                      _raw_spin_lock+0x2c/0x40
                      ffs_user_copy_worker+0x4b/0x170
                      process_one_work+0x278/0x6e0
     SOFTIRQ-ON-W at:
                      lock_acquire+0xcf/0x2e0
                      _raw_spin_lock+0x2c/0x40
                      ffs_user_copy_worker+0x4b/0x170
                      process_one_work+0x278/0x6e0
  [...]
   }

Switching ffs_user_copy_worker() to spin_lock_irq() / spin_unlock_irq() resolves the lockdep warning in testing.

Thanks,
Neill

Signature · 15 lines
-------------------------------------------------------------------------
Dipl.-Inform.
Ingo ROHLOFF
Senior Staff Engineer
phone +49 8102 9876-142 - [email protected]

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

-------------------------------------------------------------------------