Bug 272286 - When unplugging USB sound card, usbconfig stops working
Summary: When unplugging USB sound card, usbconfig stops working
Status: Closed DUPLICATE of bug 194727
Alias: None
Product: Base System
Classification: Unclassified
Component: usb (show other bugs)
Version: CURRENT
Hardware: amd64 Any
: --- Affects Many People
Assignee: Christos Margiolis
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-06-29 22:58 UTC by Jonas Lopes
Modified: 2024-04-15 00:18 UTC (History)
1 user (show)

See Also:
linimon: mfc-stable14?
linimon: mfc-stable13?


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonas Lopes 2023-06-29 22:58:10 UTC
Dears,

I use a HyperX USB Sound Card. When I unplug the sound card the FreeBSD USB subsystem does not work properly. When I plug the sound card back in, it doesn't work again. And the usbconfig command stops working too.

# tail -f /var/log/messages
Jun 29 19:43:24 mithrandir kernel: pcm3: unregister: channel pcm3:virtual:dsp3.vp2 busy (pid 84596)
Jun 29 19:43:24 mithrandir kernel: pcm3: Waiting for sound application to exit!
Jun 29 19:43:24 mithrandir kernel: pcm3: unregister: channel pcm3:virtual:dsp3.vp2 busy (pid 84596)
Jun 29 19:43:24 mithrandir kernel: pcm3: Waiting for sound application to exit!
Jun 29 19:43:24 mithrandir kernel: pcm3: unregister: channel pcm3:virtual:dsp3.vp2 busy (pid 84596)
Jun 29 19:43:24 mithrandir kernel: pcm3: Waiting for sound application to exit!
Jun 29 19:43:25 mithrandir kernel: pcm3: unregister: channel pcm3:virtual:dsp3.vp2 busy (pid 84596)
Jun 29 19:43:25 mithrandir kernel: pcm3: Waiting for sound application to exit!
Jun 29 19:43:28 mithrandir kernel: pcm3: unregister: channel pcm3:virtual:dsp3.vp2 busy (pid 84596)
Jun 29 19:43:28 mithrandir kernel: pcm3: Waiting for sound application to exit!

# usbconfig list
^C
Comment 1 Mina Galić freebsd_triage 2023-06-30 00:31:31 UTC
Hi Jonas,
thanks for the report.

you can use `procstat -a -k` to drill down where exactly it is hanging.
but an educated guess from IRC would be that PID 84596 is holding the PCM device so the USB stack can't free it and it is holding a lock while it waits.
Comment 2 Jonas Lopes 2023-06-30 02:05:53 UTC
(In reply to Mina Galić from comment #1)

Hi Mina,

On the nose!

It was SNDIO arresting the process.

After I stopped the service:

# usbconfig list
ugen0.1: <Intel XHCI root HUB> at usbus0, cfg=0 md=HOST spd=SUPER (5.0Gbps) pwr=SAVE (0mA)
ugen0.3: <Dell Computer Corp Dell Universal Receiver> at usbus0, cfg=0 md=HOST spd=FULL (12Mbps) pwr=ON (100mA)
ugen0.4: <Chicony Electronics Co.,Ltd. Integrated Camera> at usbus0, cfg=0 md=HOST spd=HIGH (480Mbps) pwr=ON (500mA)
ugen0.5: <vendor 0x8087 product 0x0a2a> at usbus0, cfg=0 md=HOST spd=FULL (12Mbps) pwr=ON (100mA)

After I plugged the sound card back in:

# usbconfig list
ugen0.1: <Intel XHCI root HUB> at usbus0, cfg=0 md=HOST spd=SUPER (5.0Gbps) pwr=SAVE (0mA)
ugen0.3: <Dell Computer Corp Dell Universal Receiver> at usbus0, cfg=0 md=HOST spd=FULL (12Mbps) pwr=ON (100mA)
ugen0.4: <Chicony Electronics Co.,Ltd. Integrated Camera> at usbus0, cfg=0 md=HOST spd=HIGH (480Mbps) pwr=ON (500mA)
ugen0.5: <vendor 0x8087 product 0x0a2a> at usbus0, cfg=0 md=HOST spd=FULL (12Mbps) pwr=ON (100mA)
*ugen0.2: <Kingston HyperX Amp> at usbus0, cfg=0 md=HOST spd=FULL (12Mbps) pwr=ON (100mA)*


The `procstat -a -k` command ran to completion without any problems. It did not show any hanging.

I think it's interesting to have a callback in the USB processes to signal when something has been unplugged and to detach the reference. Such as, "hey sndio, virtual_oss, pulse, etc, that sound card was unplugged. Take it off your list".

Mina, thank you very much!!!

Best,
Comment 3 commit-hook freebsd_committer freebsd_triage 2024-03-31 14:17:06 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=9eff58c6d52b66eb8abe7f724dabcd804a566df4

commit 9eff58c6d52b66eb8abe7f724dabcd804a566df4
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2024-03-31 14:13:43 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2024-03-31 14:13:43 +0000

    sound: Implement asynchronous device detach

    Hot-unplugging a sound device, such as a USB sound card, whilst being
    consumed by an application, results in an infinite loop until either the
    application closes the device's file descriptor, or the channel
    automatically times out after hw.snd.timeout seconds. In the case of a
    detach however, the timeout approach is still not ideal, since we want
    all resources to be released immediatelly, without waiting for N seconds
    until we can use the bus again.

    The timeout mechanism works by calling chn_sleep() in chn_read() and
    chn_write() (see pcm/channel.c) in order to send the thread to sleep,
    using cv_timedwait_sig(). Since chn_sleep() sets the CHN_F_SLEEPING flag
    while waiting for cv_timedwait_sig() to return, we can test this flag in
    pcm_unregister() (called during detach) and wakeup the sleeping
    thread(s) to immediately kill the channel(s) being consumed.

    Sponsored by:   The FreeBSD Foundation
    MFC after:      2 months
    PR:             194727, 278055, 202275, 220949, 272286
    Reviewed by:    dev_submerge.ch, markj
    Differential Revision:  https://reviews.freebsd.org/D43545

 share/man/man4/snd_uaudio.4 | 11 +----------
 sys/dev/sound/pcm/dsp.c     |  2 +-
 sys/dev/sound/pcm/mixer.c   | 11 -----------
 sys/dev/sound/pcm/sound.c   | 24 ++++++++++--------------
 sys/dev/sound/usb/uaudio.c  | 13 +++----------
 5 files changed, 15 insertions(+), 46 deletions(-)
Comment 4 Mark Linimon freebsd_committer freebsd_triage 2024-04-05 09:51:10 UTC
^Triage: assign to committer.
Comment 5 Christos Margiolis freebsd_committer freebsd_triage 2024-04-14 21:05:16 UTC
Fixed as of 44e128fe9d92c1a544b801cb56e907a66ef34691
Comment 6 Christos Margiolis freebsd_committer freebsd_triage 2024-04-15 00:18:32 UTC

*** This bug has been marked as a duplicate of bug 194727 ***