Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH v3] usb: gadget: midi2: serialize the IN endpoint transmit path
@ 2026-12-03 13:58 Jaegyun Lee
  2026-12-03 17:30 ` Takashi Iwai
  0 siblings, 1 reply; 2+ messages in thread
From: Jaegyun Lee @ 2026-12-03 13:58 UTC (permalink / raw)
  To: Takashi Iwai, Greg Kroah-Hartman, Neill Kapron
  Cc: Jaegyun Lee, linux-usb, linux-sound, linux-kernel, stable

process_midi1_transmit() and process_ump_transmit() run unlocked from
both the ALSA output trigger and the IN request completion handler,
and f_midi2_stop_eps() frees IN requests on SET_INTERFACE without
serializing against them.

In MIDI 1.0 mode, two concurrent callers of process_midi1_transmit()
race on port->pending in process_midi1_pending_buf(), underflowing
port->pending into a huge memmove(). When SET_INTERFACE calls
f_midi2_stop_eps() while an IN transmit is running, the request being
filled is freed under the transmitter.

Reproduced on dummy_hcd in altset 0 by writing to the UMP device.

OOB write because of the underflow:

[   48.568314][ T9607] ------------[ cut here ]------------
[   48.568330][ T9607] memmove: detected field-spanning write (size 4294967294) of single field "port->buf" at drivers/usb/gadget/function/f_midi2.c:957 (size 32)
[   48.568336][    C3] BUG: unable to handle page fault for address: ffffed102908202d
[   48.568344][ T9607] WARNING: drivers/usb/gadget/function/f_midi2.c:957 at process_midi1_transmit+0x1394/0x1930, CPU#1: midi2_m1_race/9607
[   48.568383][ T9607] Modules linked in:
[   48.568395][ T9607] CPU: 1 UID: 0 PID: 9607 Comm: midi2_m1_race Not tainted 7.4.0-rc3-g666539eecdf4 #1 PREEMPT(full) 
[   48.568411][ T9607] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
[   48.568420][ T9607] RIP: 0010:process_midi1_transmit+0x13a3/0x1930
[   48.568441][ T9607] Code: fc 07 0f 86 18 fe ff ff 31 ed e9 eb f2 ff ff e8 f3 10 e7 f9 48 8d 3d 5c c0 7c 0a b9 20 00 00 00 48 c7 c2 80 ea e4 8c 48 89 ee <67> 48 0f b9 3a e9 ed f8 ff ff e8 ce 10 e7 f9 48 8b 04 24 48 8d 78
[   48.568464][ T9607] RSP: 0018:ffffc9001279f9c0 EFLAGS: 00010293
[   48.568478][ T9607] RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000020
[   48.568487][ T9607] RDX: ffffffff8ce4ea80 RSI: 00000000fffffffe RDI: ffffffff92592ac0
[   48.568496][ T9607] RBP: 00000000fffffffe R08: ffffffff87dc635e R09: 00000000fffffffe
[   48.568504][ T9607] R10: ffffffff924c579f R11: 0000000000000000 R12: ffff88804841016c
[   48.568513][ T9607] R13: ffff888048410000 R14: dffffc0000000000 R15: ffff88804841016e
[   48.568524][ T9607] FS:  00007fc67a40f640(0000) GS:ffff8880e9493000(0000) knlGS:0000000000000000
[   48.568536][ T9607] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   48.568545][ T9607] CR2: 00007fc67bc17000 CR3: 000000004b3ca000 CR4: 0000000000350ef0
[   48.568553][ T9607] Call Trace:
[   48.568558][ T9607]  <TASK>
[   48.568568][ T9607]  ? __pfx_process_midi1_transmit+0x10/0x10
[   48.568587][ T9607]  ? do_raw_spin_lock+0x12b/0x270
[   48.568603][ T9607]  ? find_held_lock+0x2b/0x80
[   48.568619][ T9607]  f_midi2_ump_trigger+0x102/0x120
[   48.568638][ T9607]  snd_rawmidi_kernel_write1+0x62c/0x8f0
[   48.568657][ T9607]  snd_rawmidi_write+0x26f/0xc80
[   48.568683][ T9607]  ? __pfx_snd_rawmidi_write+0x10/0x10
[   48.568701][ T9607]  ? common_file_perm+0x278/0x4f0
[   48.568725][ T9607]  ? bpf_lsm_file_permission+0x9/0x10
[   48.568739][ T9607]  ? security_file_permission+0x75/0x230
[   48.568754][ T9607]  ? rw_verify_area+0xcf/0x6e0
[   48.568773][ T9607]  vfs_write+0x2b1/0x1140
[   48.568785][ T9607]  ? __pfx_snd_rawmidi_write+0x10/0x10
[   48.568800][ T9607]  ? __pfx_vfs_write+0x10/0x10
[   48.568811][ T9607]  ? __fget_files+0x1f1/0x3b0
[   48.568826][ T9607]  ? __fget_files+0x1fb/0x3b0
[   48.568841][ T9607]  ksys_write+0x1ef/0x240
[   48.568863][ T9607]  ? __pfx_ksys_write+0x10/0x10
[   48.568881][ T9607]  do_syscall_64+0x132/0x7c0
[   48.568908][ T9607]  entry_SYSCALL_64_after_hwframe+0x77/0x7f
[   48.568923][ T9607] RIP: 0033:0x45694f
[   48.568933][ T9607] Code: 89 54 24 18 48 89 74 24 10 89 7c 24 08 e8 79 8f 03 00 48 8b 54 24 18 48 8b 74 24 10 41 89 c0 8b 7c 24 08 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 31 44 89 c7 48 89 44 24 08 e8 bc 8f 03 00 48
[   48.568945][ T9607] RSP: 002b:00007fc67a40f110 EFLAGS: 00000293 ORIG_RAX: 0000000000000001
[   48.568958][ T9607] RAX: ffffffffffffffda RBX: 00000000004fe400 RCX: 000000000045694f
[   48.568966][ T9607] RDX: 0000000000001000 RSI: 00000000004fe400 RDI: 0000000000000003
[   48.568975][ T9607] RBP: 0000000000000000 R08: 0000000000000000 R09: 00007ffde13a563f
[   48.568983][ T9607] R10: 0000000000000008 R11: 0000000000000293 R12: 00007fc67a40f640
[   48.568991][ T9607] R13: 0000000000000010 R14: 000000000041ee60 R15: 00007fc679c0f000
[   48.569003][ T9607]  </TASK>

UAF while the host toggles altset 0 and 1 rapidly:

[   37.649231][ T9613] ==================================================================
[   37.649247][ T9613] BUG: KASAN: slab-use-after-free in process_midi1_transmit+0x165f/0x1930
[   37.649288][ T9613] Read of size 8 at addr ffff888048083218 by task midi2_m1_race/9613
[   37.649304][ T9613] 
[   37.649314][ T9613] CPU: 1 UID: 0 PID: 9613 Comm: midi2_m1_race Not tainted 7.4.0-rc3-g666539eecdf4 #1 PREEMPT(full) 
[   37.649339][ T9613] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
[   37.649360][ T9613] Call Trace:
[   37.649367][ T9613]  <TASK>
[   37.649374][ T9613]  dump_stack_lvl+0x116/0x1b0
[   37.649398][ T9613]  print_report+0xf1/0x5c0
[   37.649418][ T9613]  ? __virt_addr_valid+0x238/0x430
[   37.649446][ T9613]  ? process_midi1_transmit+0x165f/0x1930
[   37.649475][ T9613]  kasan_report+0xca/0x100
[   37.649494][ T9613]  ? process_midi1_transmit+0x165f/0x1930
[   37.649525][ T9613]  process_midi1_transmit+0x165f/0x1930
[   37.649558][ T9613]  ? __pfx_process_midi1_transmit+0x10/0x10
[   37.649587][ T9613]  ? do_raw_spin_lock+0x12b/0x270
[   37.649610][ T9613]  ? find_held_lock+0x2b/0x80
[   37.649635][ T9613]  f_midi2_ump_trigger+0x102/0x120
[   37.649664][ T9613]  snd_rawmidi_kernel_write1+0x62c/0x8f0
[   37.649692][ T9613]  snd_rawmidi_write+0x26f/0xc80
[   37.649716][ T9613]  ? __pfx_snd_rawmidi_write+0x10/0x10
[   37.649737][ T9613]  ? common_file_perm+0x278/0x4f0
[   37.649767][ T9613]  ? bpf_lsm_file_permission+0x9/0x10
[   37.649786][ T9613]  ? security_file_permission+0x75/0x230
[   37.649806][ T9613]  ? rw_verify_area+0xcf/0x6e0
[   37.649836][ T9613]  vfs_write+0x2b1/0x1140
[   37.649853][ T9613]  ? __pfx_snd_rawmidi_write+0x10/0x10
[   37.649876][ T9613]  ? __pfx_vfs_write+0x10/0x10
[   37.649894][ T9613]  ? __fget_files+0x1f1/0x3b0
[   37.649917][ T9613]  ? __fget_files+0x1fb/0x3b0
[   37.649940][ T9613]  ksys_write+0x1ef/0x240
[   37.649958][ T9613]  ? __pfx_ksys_write+0x10/0x10
[   37.649980][ T9613]  do_syscall_64+0x132/0x7c0
[   37.650009][ T9613]  entry_SYSCALL_64_after_hwframe+0x77/0x7f
[   37.650030][ T9613] RIP: 0033:0x45698f
[   37.650045][ T9613] Code: 89 54 24 18 48 89 74 24 10 89 7c 24 08 e8 79 8f 03 00 48 8b 54 24 18 48 8b 74 24 10 41 89 c0 8b 7c 24 08 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 31 44 89 c7 48 89 44 24 08 e8 bc 8f 03 00 48
[   37.650065][ T9613] RSP: 002b:00007f185b0500f0 EFLAGS: 00000293 ORIG_RAX: 0000000000000001
[   37.650084][ T9613] RAX: ffffffffffffffda RBX: 00000000004fe400 RCX: 000000000045698f
[   37.650098][ T9613] RDX: 0000000000001000 RSI: 00000000004fe400 RDI: 0000000000000003
[   37.650110][ T9613] RBP: 00007f185b05012c R08: 0000000000000000 R09: 00007fff022429ef
[   37.650123][ T9613] R10: 0000000000000008 R11: 0000000000000293 R12: 00007f185b050640
[   37.650135][ T9613] R13: 0000000000000010 R14: 000000000041eea0 R15: 00007f185a850000
[   37.650154][ T9613]  </TASK>
[   37.650161][ T9613] 
[   37.650164][ T9613] Allocated by task 9578:
[   37.650173][ T9613]  kasan_save_stack+0x24/0x50
[   37.650200][ T9613]  kasan_save_track+0x14/0x30
[   37.650226][ T9613]  __kasan_kmalloc+0xaa/0xb0
[   37.650251][ T9613]  __kmalloc_cache_noprof+0x2da/0x6e0
[   37.650272][ T9613]  dummy_alloc_request+0xa9/0x1b0
[   37.650289][ T9613]  usb_ep_alloc_request+0x6e/0x2e0
[   37.650308][ T9613]  alloc_ep_req+0x28/0x1e0
[   37.650333][ T9613]  f_midi2_alloc_ep_reqs+0x19b/0x2c0
[   37.650361][ T9613]  f_midi2_start_eps+0x23a/0x610
[   37.650382][ T9613]  f_midi2_set_alt+0x2fc/0x610
[   37.650403][ T9613]  composite_setup+0xb1b/0x9130
[   37.650419][ T9613]  configfs_composite_setup+0x10f/0x150
[   37.650445][ T9613]  dummy_timer+0x12ce/0x35c0
[   37.650463][ T9613]  __hrtimer_run_queues+0x17f/0xa00
[   37.650491][ T9613]  hrtimer_run_softirq+0x1c9/0x360
[   37.650517][ T9613]  handle_softirqs+0x20e/0x9e0
[   37.650541][ T9613]  __irq_exit_rcu+0x184/0x240
[   37.650563][ T9613]  irq_exit_rcu+0x9/0x30
[   37.650584][ T9613]  sysvec_apic_timer_interrupt+0xa8/0xc0
[   37.650611][ T9613]  asm_sysvec_apic_timer_interrupt+0x1a/0x20
[   37.650630][ T9613] 
[   37.650634][ T9613] Freed by task 0:
[   37.650642][ T9613]  kasan_save_stack+0x24/0x50
[   37.650667][ T9613]  kasan_save_track+0x14/0x30
[   37.650693][ T9613]  kasan_save_free_info+0x3b/0x60
[   37.650715][ T9613]  __kasan_slab_free+0x61/0x80
[   37.650730][ T9613]  kfree+0x2ca/0x6e0
[   37.650747][ T9613]  dummy_free_request+0x56/0x90
[   37.650763][ T9613]  f_midi2_free_ep_reqs+0x151/0x270
[   37.650782][ T9613]  f_midi2_set_alt+0x44d/0x610
[   37.650803][ T9613]  composite_setup+0xb1b/0x9130
[   37.650818][ T9613]  configfs_composite_setup+0x10f/0x150
[   37.650840][ T9613]  dummy_timer+0x12ce/0x35c0
[   37.650858][ T9613]  __hrtimer_run_queues+0x17f/0xa00
[   37.650884][ T9613]  hrtimer_run_softirq+0x1c9/0x360
[   37.650910][ T9613]  handle_softirqs+0x20e/0x9e0
[   37.650932][ T9613]  __irq_exit_rcu+0x184/0x240
[   37.650954][ T9613]  irq_exit_rcu+0x9/0x30
[   37.650976][ T9613]  sysvec_apic_timer_interrupt+0xa8/0xc0
[   37.651002][ T9613]  asm_sysvec_apic_timer_interrupt+0x1a/0x20
[   37.651020][ T9613] 
[   37.651024][ T9613] The buggy address belongs to the object at ffff888048083200
[   37.651024][ T9613]  which belongs to the cache kmalloc-128 of size 128
[   37.651039][ T9613] The buggy address is located 24 bytes inside of
[   37.651039][ T9613]  freed 128-byte region [ffff888048083200, ffff888048083280)
[   37.651058][ T9613] 
[   37.651062][ T9613] The buggy address belongs to the physical page:
[   37.651071][ T9613] page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x48083
[   37.651088][ T9613] flags: 0x4fff00000000000(node=1|zone=1|lastcpupid=0x7ff)
[   37.651104][ T9613] page_type: f5(slab)
[   37.651120][ T9613] raw: 04fff00000000000 ffff88801d042a00 dead000000000122 0000000000000000
[   37.651137][ T9613] raw: 0000000000000000 0000000000100010 00000000f5000000 0000000000000000
[   37.651148][ T9613] page dumped because: kasan: bad access detected
[   37.651157][ T9613] page_owner tracks the page as allocated
[   37.651163][ T9613] page last allocated via order 0, migratetype Unmovable, gfp_mask 0xd2820(GFP_ATOMIC|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC), pid 40, tgid 40 (kworker/1:1), ts 37471649956
[   37.651195][ T9613]  post_alloc_hook+0xff/0x130
[   37.651214][ T9613]  get_page_from_freelist+0xdf0/0x2a10
[   37.651234][ T9613]  __alloc_frozen_pages_noprof+0x28b/0x2dd0
[   37.651257][ T9613]  new_slab+0xb9/0x6b0
[   37.651278][ T9613]  refill_objects+0x10e/0x3d0
[   37.651299][ T9613]  __pcs_replace_empty_main+0x35a/0x680
[   37.651319][ T9613]  __kmalloc_cache_noprof+0x592/0x6e0
[   37.651339][ T9613]  dummy_urb_enqueue+0xa3/0x9a0
[   37.651366][ T9613]  usb_hcd_submit_urb+0x266/0x1fe0
[   37.651391][ T9613]  usb_submit_urb+0x8a6/0x18d0
[   37.651417][ T9613]  do_submit_urbs_locked.part.0+0x197/0x370
[   37.651440][ T9613]  submit_io_urbs.part.0+0xac/0x130
[   37.651462][ T9613]  snd_usb_midi_v2_trigger+0x15f/0x1b0
[   37.651484][ T9613]  snd_rawmidi_kernel_write1+0x62c/0x8f0
[   37.651503][ T9613]  ump_req_msg+0x158/0x570
[   37.651523][ T9613]  snd_ump_parse_endpoint+0x104/0xc90
[   37.651546][ T9613] page last free pid 24 tgid 24 ts 37070428769 stack trace:
[   37.651559][ T9613]  __free_frozen_pages+0x75e/0x1190
[   37.651575][ T9613]  rcu_core+0x5f0/0x1260
[   37.651597][ T9613]  handle_softirqs+0x20e/0x9e0
[   37.651619][ T9613]  run_ksoftirqd+0x3a/0x60
[   37.651640][ T9613]  smpboot_thread_fn+0x2bf/0xb40
[   37.651658][ T9613]  kthread+0x390/0x4a0
[   37.651677][ T9613]  ret_from_fork+0xb0c/0xdb0
[   37.651702][ T9613]  ret_from_fork_asm+0x1a/0x30
[   37.651725][ T9613] 
[   37.651729][ T9613] Memory state around the buggy address:
[   37.651737][ T9613]  ffff888048083100: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[   37.651751][ T9613]  ffff888048083180: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
[   37.651764][ T9613] >ffff888048083200: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[   37.651774][ T9613]                             ^
[   37.651783][ T9613]  ffff888048083280: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
[   37.651796][ T9613]  ffff888048083300: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[   37.651806][ T9613] ==================================================================

Serialize the transmit path with IN request allocation and teardown
using a per-endpoint lock, and defer it to a work item when it is
kicked from the IN request completion handler.

Fixes: d6468be779af ("usb: gadget: midi2: MIDI 1.0 interface (altset 0) support")
Fixes: 8b645922b223 ("usb: gadget: Add support for USB MIDI 2.0 function driver")
Cc: stable@vger.kernel.org
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Jaegyun Lee <dreams200260@gmail.com>
---
v3: disable tx_work in unbind instead of cancelling it, keep the
    original request drop order in f_midi2_stop_eps(), transmit directly
    from the ALSA trigger
v2: https://lore.kernel.org/r/20261203080717.2643096-1-dreams200260@gmail.com
v1: https://lore.kernel.org/r/20261203075318.2517506-1-dreams200260@gmail.com

 drivers/usb/gadget/function/f_midi2.c |   69 +++++++++++++++++++++++++++++++--
 1 file changed, 65 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/gadget/function/f_midi2.c b/drivers/usb/gadget/function/f_midi2.c
index c02d6f1..fe4442a 100644
--- a/drivers/usb/gadget/function/f_midi2.c
+++ b/drivers/usb/gadget/function/f_midi2.c
@@ -42,6 +42,8 @@ struct f_midi2_usb_ep {
 	unsigned long free_reqs;	/* bitmap for unused requests */
 	unsigned int num_reqs;		/* number of allocated requests */
 	struct f_midi2_req_ctx *reqs;	/* request context array */
+	spinlock_t tx_lock;
+	struct work_struct tx_work;
 };
 
 /* Resources for UMP Function Block (and USB Group Terminal Block) */
@@ -784,7 +786,7 @@ static void f_midi2_ep_in_complete(struct usb_ep *usb_ep,
 		return;
 	}
 
-	process_ump_transmit(ep);
+	queue_work(system_highpri_wq, &ep->ep_in.tx_work);
 }
 
 /*
@@ -1052,6 +1054,26 @@ static void process_midi1_transmit(struct f_midi2 *midi2)
 	}
 }
 
+static void f_midi2_tx_run(struct f_midi2_usb_ep *usb_ep)
+{
+	unsigned long flags;
+
+	if (!usb_ep->usb_ep)
+		return;
+
+	spin_lock_irqsave(&usb_ep->tx_lock, flags);
+	if (usb_ep->ep)
+		process_ump_transmit(usb_ep->ep);
+	else
+		process_midi1_transmit(usb_ep->card);
+	spin_unlock_irqrestore(&usb_ep->tx_lock, flags);
+}
+
+static void f_midi2_tx_work(struct work_struct *work)
+{
+	f_midi2_tx_run(container_of(work, struct f_midi2_usb_ep, tx_work));
+}
+
 /* complete handler for MIDI1 EP-in requests */
 static void f_midi2_midi1_ep_in_complete(struct usb_ep *usb_ep,
 					 struct usb_request *req)
@@ -1068,7 +1090,7 @@ static void f_midi2_midi1_ep_in_complete(struct usb_ep *usb_ep,
 		return;
 	}
 
-	process_midi1_transmit(midi2);
+	queue_work(system_highpri_wq, &midi2->midi1_ep_in.tx_work);
 }
 
 /* complete handler for MIDI1 EP-out requests */
@@ -1227,6 +1249,9 @@ static int f_midi2_init_ep(struct f_midi2 *midi2, struct f_midi2_ep *ep,
 /* Free EP */
 static void f_midi2_free_ep(struct f_midi2_usb_ep *usb_ep)
 {
+	unsigned long flags;
+
+	spin_lock_irqsave(&usb_ep->tx_lock, flags);
 	f_midi2_drop_reqs(usb_ep);
 
 	f_midi2_free_ep_reqs(usb_ep);
@@ -1235,6 +1260,7 @@ static void f_midi2_free_ep(struct f_midi2_usb_ep *usb_ep)
 	usb_ep->num_reqs = 0;
 	usb_ep->free_reqs = 0;
 	usb_ep->reqs = NULL;
+	spin_unlock_irqrestore(&usb_ep->tx_lock, flags);
 }
 
 /* Queue requests for EP-out at start */
@@ -1264,9 +1290,13 @@ static void f_midi2_queue_out_reqs(struct f_midi2_usb_ep *usb_ep)
 static void f_midi2_stop_eps(struct f_midi2_usb_ep *ep_in,
 			     struct f_midi2_usb_ep *ep_out)
 {
+	unsigned long flags;
+
+	spin_lock_irqsave(&ep_in->tx_lock, flags);
 	f_midi2_drop_reqs(ep_in);
 	f_midi2_drop_reqs(ep_out);
 	f_midi2_free_ep_reqs(ep_in);
+	spin_unlock_irqrestore(&ep_in->tx_lock, flags);
 	f_midi2_free_ep_reqs(ep_out);
 }
 
@@ -1275,6 +1305,7 @@ static int f_midi2_start_eps(struct f_midi2_usb_ep *ep_in,
 			     struct f_midi2_usb_ep *ep_out,
 			     struct usb_function *fn)
 {
+	unsigned long flags;
 	int err;
 
 	err = f_midi2_start_ep(ep_in, fn);
@@ -1284,7 +1315,9 @@ static int f_midi2_start_eps(struct f_midi2_usb_ep *ep_in,
 	if (err)
 		return err;
 
+	spin_lock_irqsave(&ep_in->tx_lock, flags);
 	err = f_midi2_alloc_ep_reqs(ep_in);
+	spin_unlock_irqrestore(&ep_in->tx_lock, flags);
 	if (err)
 		return err;
 	err = f_midi2_alloc_ep_reqs(ep_out);
@@ -1481,10 +1514,10 @@ static void f_midi2_ump_trigger(struct snd_ump_endpoint *ump, int dir, int up)
 	if (up && dir == SNDRV_RAWMIDI_STREAM_OUTPUT) {
 		switch (midi2->operation_mode) {
 		case MIDI_OP_MODE_MIDI1:
-			process_midi1_transmit(midi2);
+			f_midi2_tx_run(&midi2->midi1_ep_in);
 			break;
 		case MIDI_OP_MODE_MIDI2:
-			process_ump_transmit(ep);
+			f_midi2_tx_run(&ep->ep_in);
 			break;
 		}
 	}
@@ -1949,6 +1982,24 @@ static int f_midi2_init_midi2_ep_out(struct f_midi2 *midi2, int index)
 			       f_midi2_ep_out_complete);
 }
 
+static void f_midi2_enable_tx_work(struct f_midi2 *midi2)
+{
+	int i;
+
+	enable_work(&midi2->midi1_ep_in.tx_work);
+	for (i = 0; i < midi2->num_eps; i++)
+		enable_work(&midi2->midi2_eps[i].ep_in.tx_work);
+}
+
+static void f_midi2_disable_tx_work(struct f_midi2 *midi2)
+{
+	int i;
+
+	disable_work_sync(&midi2->midi1_ep_in.tx_work);
+	for (i = 0; i < midi2->num_eps; i++)
+		disable_work_sync(&midi2->midi2_eps[i].ep_in.tx_work);
+}
+
 /* gadget function bind callback */
 static int f_midi2_bind(struct usb_configuration *c, struct usb_function *f)
 {
@@ -2065,6 +2116,7 @@ static int f_midi2_bind(struct usb_configuration *c, struct usb_function *f)
 	f_midi2_free_usb_configs(&config);
 
 	mutex_unlock(&f_midi2_desc_mutex);
+	f_midi2_enable_tx_work(midi2);
 	return 0;
 
 fail:
@@ -2084,6 +2136,7 @@ static void f_midi2_unbind(struct usb_configuration *c, struct usb_function *f)
 	struct f_midi2 *midi2 = func_to_midi2(f);
 	int i;
 
+	f_midi2_disable_tx_work(midi2);
 	f_midi2_free_card(midi2);
 
 	f_midi2_free_ep(&midi2->midi1_ep_in);
@@ -2854,6 +2907,10 @@ static struct usb_function *f_midi2_alloc(struct usb_function_instance *fi)
 	++opts->refcnt;
 
 	spin_lock_init(&midi2->queue_lock);
+	spin_lock_init(&midi2->midi1_ep_in.tx_lock);
+	spin_lock_init(&midi2->midi1_ep_out.tx_lock);
+	INIT_WORK(&midi2->midi1_ep_in.tx_work, f_midi2_tx_work);
+	disable_work(&midi2->midi1_ep_in.tx_work);
 
 	midi2->func.name = "midi2_func";
 	midi2->func.bind = f_midi2_bind;
@@ -2873,6 +2930,10 @@ static struct usb_function *f_midi2_alloc(struct usb_function_instance *fi)
 
 	for (i = 0; i < num_eps; i++) {
 		ep = &midi2->midi2_eps[i];
+		spin_lock_init(&ep->ep_in.tx_lock);
+		spin_lock_init(&ep->ep_out.tx_lock);
+		INIT_WORK(&ep->ep_in.tx_work, f_midi2_tx_work);
+		disable_work(&ep->ep_in.tx_work);
 		ep->info = opts->eps[i]->info;
 		ep->info.ep_name = NULL;
 		ep->info.product_id = NULL;

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

end of thread, other threads:[~2026-12-03 17:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 13:58 [PATCH v3] usb: gadget: midi2: serialize the IN endpoint transmit path Jaegyun Lee
2026-12-03 17:30 ` Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox