All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: f_tcm: do not dequeue UAS stream requests in BOT mode
@ 2026-11-20 11:39 Yehyeong Lee
  2026-11-20 11:47 ` sashiko-bot
  2026-11-20 13:42 ` Greg KH
  0 siblings, 2 replies; 4+ messages in thread
From: Yehyeong Lee @ 2026-11-20 11:39 UTC (permalink / raw)
  To: gregkh, nkapron, linux-usb
  Cc: Thinh.Nguyen, bigeasy, target-devel, linux-kernel,
	lgs201920130244

usbg_aborted_task() starts by reading stream->req_out->status.  The
stream requests are allocated by uasp_alloc_stream_res() for the UAS
alternate setting only, so in BOT mode they are NULL.

The target core calls .aborted_task() from target_handle_abort() for an
aborted command whose CMD_T_TAS is clear.  A BOT command gets there when
a LUN RESET arrives on another fabric that exports the same backstore
and the backstore has emulate_tas set to 0: core_tmr_drain_state_list()
aborts every command on the device, no TASK ABORTED status is owed, and
the core calls .aborted_task(), where the read faults.  With the default
emulate_tas of 1 the core sends TASK ABORTED status instead.

Return early unless the function is in UAS mode.  A BOT transfer uses
fu->bot_req_in and fu->bot_req_out, not the stream requests.

Reproduced on dummy_hcd in BOT mode with a fileio backstore that is also
exported through tcm_loop and has emulate_tas set to 0: a WRITE(10) has
its data stage held while SG_SCSI_RESET_DEVICE on the loopback disk
issues the LUN RESET.  Without this patch 3 of 3 boots oops as below;
with it none do, and the write does not reach the backing store.

  BUG: kernel NULL pointer dereference, address: 000000000000005c
  #PF: supervisor read access in kernel mode
  #PF: error_code(0x0000) - not-present page
  PGD 0 P4D 0
  Oops: Oops: 0000 [#1] SMP NOPTI
  CPU: 1 UID: 0 PID: 30 Comm: kworker/1:1 Not tainted 7.4.0-rc3-FT9NNM-00395-g8dac6df92a2e #1 PREEMPT(lazy)
  Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
  Workqueue: target_completion target_abort_work
  RIP: 0010:usbg_aborted_task+0x2d/0xc0
  Code: fa 55 53 48 8b 87 e0 01 00 00 48 89 fb 48 8b 50 38 48 8b 52 30 48 8b 2a 8b 57 24 48 8d 14 d2 48 8d 14 d0 48 8b b2 58 03 00 00 <83> 7e 5c 8d 74 2b 48 8b b2 50 03 00 00 83 7e 5c 8d 74 58 48 8b b2
  RSP: 0018:ffffc90000107e40 EFLAGS: 00010246
  RAX: ffff88800540d000 RBX: ffff888006c6c870 RCX: 0000000000000001
  RDX: ffff88800540d6c0 RSI: 0000000000000000 RDI: ffff888006c6c870
  RBP: ffff8880053febc8 R08: 8080808080808080 R09: ffff88800491c218
  R10: ffff888005031cc0 R11: fefefefefefefeff R12: ffff88807db295c0
  R13: ffff888006c6c9a8 R14: ffff888005031805 R15: 0000000000000000
  FS:  0000000000000000(0000) GS:ffff8880f9e92000(0000) knlGS:0000000000000000
  CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
  CR2: 000000000000005c CR3: 0000000003030002 CR4: 0000000000770ef0
  PKRU: 55555554
  Call Trace:
   <TASK>
   target_handle_abort+0x6b/0x130
   process_one_work+0x19e/0x3f0
   worker_thread+0x180/0x2f0
   ? __pfx_worker_thread+0x10/0x10
   kthread+0xee/0x120
   ? __pfx_kthread+0x10/0x10
   ret_from_fork+0x186/0x230
   ? __pfx_kthread+0x10/0x10
   ret_from_fork_asm+0x1a/0x30
   </TASK>
  Modules linked in:
  CR2: 000000000000005c
  ---[ end trace 0000000000000000 ]---
  note: kworker/1:1[30] exited with irqs disabled

Fixes: 76003eb6d09278 ("usb: gadget: f_tcm: Handle abort command")
Cc: stable@vger.kernel.org
Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
---
 drivers/usb/gadget/function/f_tcm.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
index 9e6d4f39900a27..57e2ec81f29a8a 100644
--- a/drivers/usb/gadget/function/f_tcm.c
+++ b/drivers/usb/gadget/function/f_tcm.c
@@ -1614,6 +1614,10 @@ static void usbg_aborted_task(struct se_cmd *se_cmd)
 	struct uas_stream *stream = &fu->stream[se_cmd->map_tag];
 	int ret = 0;
 
+	/* the stream requests exist only in UAS mode */
+	if (!(fu->flags & USBG_IS_UAS))
+		return;
+
 	if (stream->req_out->status == -EINPROGRESS)
 		ret = usb_ep_dequeue(fu->ep_out, stream->req_out);
 	else if (stream->req_in->status == -EINPROGRESS)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] usb: gadget: f_tcm: do not dequeue UAS stream requests in BOT mode
  2026-11-20 11:39 [PATCH] usb: gadget: f_tcm: do not dequeue UAS stream requests in BOT mode Yehyeong Lee
@ 2026-11-20 11:47 ` sashiko-bot
  2026-11-20 13:42 ` Greg KH
  1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-20 11:47 UTC (permalink / raw)
  To: Yehyeong Lee, linux-usb

> usbg_aborted_task() starts by reading stream->req_out->status.  The
> stream requests are allocated by uasp_alloc_stream_res() for the UAS
> alternate setting only, so in BOT mode they are NULL.
> 
> The target core calls .aborted_task() from target_handle_abort() for an
> aborted command whose CMD_T_TAS is clear.  A BOT command gets there when
> a LUN RESET arrives on another fabric that exports the same backstore
> and the backstore has emulate_tas set to 0: core_tmr_drain_state_list()
> aborts every command on the device, no TASK ABORTED status is owed, and
> the core calls .aborted_task(), where the read faults.  With the default
> emulate_tas of 1 the core sends TASK ABORTED status instead.
> 
> Return early unless the function is in UAS mode.  A BOT transfer uses
> fu->bot_req_in and fu->bot_req_out, not the stream requests.
> 
> [ ... ]
>   note: kworker/1:1[30] exited with irqs disabled
> 
> Fixes: 76003eb6d09278 ("usb: gadget: f_tcm: Handle abort command")
> Cc: stable@vger.kernel.org
> Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120113956.3071640-1-yhlee@isslab.korea.ac.kr?part=1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] usb: gadget: f_tcm: do not dequeue UAS stream requests in BOT mode
  2026-11-20 11:39 [PATCH] usb: gadget: f_tcm: do not dequeue UAS stream requests in BOT mode Yehyeong Lee
  2026-11-20 11:47 ` sashiko-bot
@ 2026-11-20 13:42 ` Greg KH
  2026-11-20 16:08   ` Yehyeong Lee
  1 sibling, 1 reply; 4+ messages in thread
From: Greg KH @ 2026-11-20 13:42 UTC (permalink / raw)
  To: Yehyeong Lee
  Cc: nkapron, linux-usb, Thinh.Nguyen, bigeasy, target-devel,
	linux-kernel, lgs201920130244

On Fri, Nov 20, 2026 at 08:39:56PM +0900, Yehyeong Lee wrote:
> usbg_aborted_task() starts by reading stream->req_out->status.  The
> stream requests are allocated by uasp_alloc_stream_res() for the UAS
> alternate setting only, so in BOT mode they are NULL.
> 
> The target core calls .aborted_task() from target_handle_abort() for an
> aborted command whose CMD_T_TAS is clear.  A BOT command gets there when
> a LUN RESET arrives on another fabric that exports the same backstore
> and the backstore has emulate_tas set to 0: core_tmr_drain_state_list()
> aborts every command on the device, no TASK ABORTED status is owed, and
> the core calls .aborted_task(), where the read faults.  With the default
> emulate_tas of 1 the core sends TASK ABORTED status instead.
> 
> Return early unless the function is in UAS mode.  A BOT transfer uses
> fu->bot_req_in and fu->bot_req_out, not the stream requests.
> 
> Reproduced on dummy_hcd in BOT mode with a fileio backstore that is also
> exported through tcm_loop and has emulate_tas set to 0: a WRITE(10) has
> its data stage held while SG_SCSI_RESET_DEVICE on the loopback disk
> issues the LUN RESET.  Without this patch 3 of 3 boots oops as below;
> with it none do, and the write does not reach the backing store.
> 
>   BUG: kernel NULL pointer dereference, address: 000000000000005c
>   #PF: supervisor read access in kernel mode
>   #PF: error_code(0x0000) - not-present page
>   PGD 0 P4D 0
>   Oops: Oops: 0000 [#1] SMP NOPTI
>   CPU: 1 UID: 0 PID: 30 Comm: kworker/1:1 Not tainted 7.4.0-rc3-FT9NNM-00395-g8dac6df92a2e #1 PREEMPT(lazy)
>   Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
>   Workqueue: target_completion target_abort_work
>   RIP: 0010:usbg_aborted_task+0x2d/0xc0
>   Code: fa 55 53 48 8b 87 e0 01 00 00 48 89 fb 48 8b 50 38 48 8b 52 30 48 8b 2a 8b 57 24 48 8d 14 d2 48 8d 14 d0 48 8b b2 58 03 00 00 <83> 7e 5c 8d 74 2b 48 8b b2 50 03 00 00 83 7e 5c 8d 74 58 48 8b b2
>   RSP: 0018:ffffc90000107e40 EFLAGS: 00010246
>   RAX: ffff88800540d000 RBX: ffff888006c6c870 RCX: 0000000000000001
>   RDX: ffff88800540d6c0 RSI: 0000000000000000 RDI: ffff888006c6c870
>   RBP: ffff8880053febc8 R08: 8080808080808080 R09: ffff88800491c218
>   R10: ffff888005031cc0 R11: fefefefefefefeff R12: ffff88807db295c0
>   R13: ffff888006c6c9a8 R14: ffff888005031805 R15: 0000000000000000
>   FS:  0000000000000000(0000) GS:ffff8880f9e92000(0000) knlGS:0000000000000000
>   CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
>   CR2: 000000000000005c CR3: 0000000003030002 CR4: 0000000000770ef0
>   PKRU: 55555554
>   Call Trace:
>    <TASK>
>    target_handle_abort+0x6b/0x130
>    process_one_work+0x19e/0x3f0
>    worker_thread+0x180/0x2f0
>    ? __pfx_worker_thread+0x10/0x10
>    kthread+0xee/0x120
>    ? __pfx_kthread+0x10/0x10
>    ret_from_fork+0x186/0x230
>    ? __pfx_kthread+0x10/0x10
>    ret_from_fork_asm+0x1a/0x30
>    </TASK>
>   Modules linked in:
>   CR2: 000000000000005c
>   ---[ end trace 0000000000000000 ]---
>   note: kworker/1:1[30] exited with irqs disabled
> 
> Fixes: 76003eb6d09278 ("usb: gadget: f_tcm: Handle abort command")
> Cc: stable@vger.kernel.org
> Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
> ---
>  drivers/usb/gadget/function/f_tcm.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
> index 9e6d4f39900a27..57e2ec81f29a8a 100644
> --- a/drivers/usb/gadget/function/f_tcm.c
> +++ b/drivers/usb/gadget/function/f_tcm.c
> @@ -1614,6 +1614,10 @@ static void usbg_aborted_task(struct se_cmd *se_cmd)
>  	struct uas_stream *stream = &fu->stream[se_cmd->map_tag];
>  	int ret = 0;
>  
> +	/* the stream requests exist only in UAS mode */
> +	if (!(fu->flags & USBG_IS_UAS))
> +		return;

What prevents the flags from changing right after you read them?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] usb: gadget: f_tcm: do not dequeue UAS stream requests in BOT mode
  2026-11-20 13:42 ` Greg KH
@ 2026-11-20 16:08   ` Yehyeong Lee
  0 siblings, 0 replies; 4+ messages in thread
From: Yehyeong Lee @ 2026-11-20 16:08 UTC (permalink / raw)
  To: gregkh, nkapron, linux-usb
  Cc: Thinh.Nguyen, bigeasy, target-devel, linux-kernel,
	lgs201920130244

On Fri, Nov 20, 2026 at 02:42:56PM +0100, Greg KH wrote:
> On Fri, Nov 20, 2026 at 08:39:56PM +0900, Yehyeong Lee wrote:
> > @@ -1614,6 +1614,10 @@ static void usbg_aborted_task(struct se_cmd *se_cmd)
> >  	struct uas_stream *stream = &fu->stream[se_cmd->map_tag];
> >  	int ret = 0;
> >
> > +	/* the stream requests exist only in UAS mode */
> > +	if (!(fu->flags & USBG_IS_UAS))
> > +		return;
> > +
> >  	if (stream->req_out->status == -EINPROGRESS)
> >  		ret = usb_ep_dequeue(fu->ep_out, stream->req_out);
>
> What prevents the flags from changing right after you read them?

Nothing does.  The same teardown that clears fu->flags also frees the
stream requests, from the set_alt work or from .disable(), without
waiting for the commands the target core still holds.  So the test is
wrong, not just racy: the host can take the UAS alternate setting away
between the test and the dequeue.

v2 drops the test and puts the requests under a lock, so .aborted_task()
either dequeues a live request or finds none:
  https://lore.kernel.org/all/20261120160616.3219043-1-yhlee@isslab.korea.ac.kr/

Best regards,
Yehyeong Lee

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-20 16:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 11:39 [PATCH] usb: gadget: f_tcm: do not dequeue UAS stream requests in BOT mode Yehyeong Lee
2026-11-20 11:47 ` sashiko-bot
2026-11-20 13:42 ` Greg KH
2026-11-20 16:08   ` Yehyeong Lee

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.