opnsense-src/sys
Christos Margiolis 99b8be414c sound: Terminate stream properly when closing vchans
When a channel is closed, dsp_close() either calls vchan_destroy() on vchans,
or chn_abort()/chn_flush() on primary channels. However, the problem with this
is that, when closing a vchan, we end up not terminating the stream properly.

The call sequence we are interested in is the following:

	vchan_destroy(vchan) -> chn_kill(vchan) -> chn_trigger(vchan) ->
	vchan_trigger(vchan) -> chn_notify(parent)

Even though chn_notify() contains codepaths which call chn_abort(parent),
apparently we do not execute any of those codepaths in this case, so the
DMA remains unterminated, hence why we keep seeing the primary
channel(s) being interrupted even once the application has exited:

root@freebsd:~ # sndctl interrupts
dsp0.play.0.interrupts=1139
dsp0.record.0.interrupts=0
root@freebsd:~ # sndctl interrupts
dsp0.play.0.interrupts=1277
dsp0.record.0.interrupts=0
root@freebsd:~ # sndctl interrupts
dsp0.play.0.interrupts=1394
dsp0.record.0.interrupts=0

The only applications that do not have this issue are those (e.g., mpv) that
manually call ioctls which end up calling chn_abort(), like SNDCTL_DSP_HALT, to
abort the channel(s) during shutdown. For all other applications that do not
manually abort the channel(s), we can confirm that chn_abort()/chn_flush(), or
even chn_trigger(PCMTRIG_ABORT) on the parent, doesn't happen during shutdown.

root@freebsd:~ # dtrace -n 'fbt::chn_abort:entry,fbt::chn_flush:entry { printf("%s", args[0]->name); stack(); }'
dtrace: description 'fbt::chn_abort:entry,fbt::chn_flush:entry ' matched 2 probes
dtrace: buffer size lowered to 1m
^C

[...]

root@freebsd:~ # dtrace -n 'fbt::chn_trigger:entry /args[1] == -1/ { printf("%s", args[0]->name); stack(); }'
dtrace: description 'fbt::chn_trigger:entry ' matched 1 probe
dtrace: buffer size lowered to 1m
CPU     ID                    FUNCTION:NAME
  0  68037                chn_trigger:entry dsp0.virtual_play.0
	      sound.ko`chn_kill+0x134
	      sound.ko`vchan_destroy+0x94
	      sound.ko`dsp_close+0x39b
	      kernel`devfs_destroy_cdevpriv+0xab
	      kernel`devfs_close_f+0x63
	      kernel`_fdrop+0x1a
	      kernel`closef+0x1e3
	      kernel`closefp_impl+0x76
	      kernel`amd64_syscall+0x151
	      kernel`0xffffffff8103841b1

To fix this, modify dsp_close() to execute the primary channel case on both
primary and virtual channels. While what we really care about are the
chn_abort()/chn_flush() calls, it shouldn't hurt to call the rest of the
functions on the vchans as well, to avoid complicating things; they get deleted
right below, anyway.

With the patch applied:

root@freebsd:~ # dtrace -n 'fbt::chn_trigger:entry /args[1] == -1/ { printf("%s", args[0]->name); stack(); }'
dtrace: description 'fbt::chn_trigger:entry ' matched 1 probe
dtrace: buffer size lowered to 1m
CPU     ID                    FUNCTION:NAME
  1  68037                chn_trigger:entry dsp0.virtual_play.0
              sound.ko`chn_flush+0x2a
              sound.ko`dsp_close+0x330
              kernel`devfs_destroy_cdevpriv+0xab
              kernel`devfs_close_f+0x63
              kernel`_fdrop+0x1a
              kernel`closef+0x1e3
              kernel`closefp_impl+0x76
              kernel`amd64_syscall+0x151
              kernel`0xffffffff8103841b

  0  68037                chn_trigger:entry dsp0.play.0
              sound.ko`chn_notify+0x4ce
              sound.ko`vchan_trigger+0x105
              sound.ko`chn_trigger+0xb4
              sound.ko`chn_flush+0x2a
              sound.ko`dsp_close+0x330
              kernel`devfs_destroy_cdevpriv+0xab
              kernel`devfs_close_f+0x63
              kernel`_fdrop+0x1a
              kernel`closef+0x1e3
              kernel`closefp_impl+0x76
              kernel`amd64_syscall+0x151
              kernel`0xffffffff8103841b

Above we can see a chn_trigger(PCMTRIG_ABORT) on the parent (dsp0.play.0),
which is coming from the chn_abort() (inlined) in chn_notify():

root@freebsd:~ # dtrace -n 'kinst::chn_abort:entry { stack(); }'
dtrace: description 'kinst::chn_abort:entry ' matched 5 probes
dtrace: buffer size lowered to 1m
CPU     ID                    FUNCTION:NAME
  1  72580                  chn_notify:1192
              sound.ko`0xffffffff8296cab4
              sound.ko`vchan_trigger+0x105
              sound.ko`chn_trigger+0xb4
              sound.ko`chn_flush+0x2a
              sound.ko`dsp_close+0x330
              kernel`devfs_destroy_cdevpriv+0xab
              kernel`devfs_close_f+0x63
              kernel`_fdrop+0x1a
              kernel`closef+0x1e3
              kernel`closefp_impl+0x76
              kernel`amd64_syscall+0x151
              kernel`0xffffffff8103841b

We can also confirm the primary channel(s) are not interrupted anymore:

root@freebsd:/mnt/src # sndctl interrupts
dsp0.play.0.interrupts=0
dsp0.record.0.interrupts=0

In collaboration with:	adrian
Tested by:		adrian, christos, thj
Sponsored by:		The FreeBSD Foundation
MFC after:		2 days
Reviewed by:		thj, adrian, emaste
Differential Revision:	https://reviews.freebsd.org/D50488

(cherry picked from commit f6430bc61df78be070209d52b4452ae9cf4cd015)
(cherry picked from commit 0c6aa445ec0c85e7c9653d20562907742569de6f)

Approved by:		re (cperciva)
2025-05-30 00:51:30 +02:00
..
amd64 mpi3mr: Broadcom's MPT-Fusion version 4 is amd64 and aarch64 only 2025-04-30 11:06:54 -06:00
arm mmc: Use bus_topo_lock and taskqueue_bus while adding/removing child devices 2025-04-29 10:40:47 -04:00
arm64 mpi3mr: Broadcom's MPT-Fusion version 4 is amd64 and aarch64 only 2025-04-30 11:06:54 -06:00
bsm New setcred() system call and associated MAC hooks 2025-04-03 21:31:03 +02:00
cam cam: Add 3e/3 as a fatal code 2025-01-24 15:34:19 -07:00
cddl dtrace/arm64: Fix dtrace_gethrtime() 2025-04-02 15:14:37 +00:00
compat LinuxKPI: 802.11: mark struct ieee80211_ht_cap __packed 2025-05-27 22:44:20 +00:00
conf 14.3: Update to BETA4 2025-05-22 17:04:19 -07:00
contrib sysctl(9): Ease exporting struct sizes; Discourage doing that 2025-05-13 14:41:33 +02:00
crypto openssl: update ASM and version info for 3.0.16 import 2025-03-25 14:07:59 -07:00
ddb
dev sound: Terminate stream properly when closing vchans 2025-05-30 00:51:30 +02:00
dts
fs sysctl(9): Ease exporting struct sizes; Discourage doing that 2025-05-13 14:41:33 +02:00
gdb
geom sysctl(9): Ease exporting struct sizes; Discourage doing that 2025-05-13 14:41:33 +02:00
gnu
i386 i386: Avoid registering overlapping vm_phys_seg entries 2025-04-15 02:25:24 +00:00
isa isa: Use a bus_child_deleted method to free ivars for children 2024-11-30 21:46:06 -05:00
kern vfs: vntblinit(): Raise default 'kern.maxvnodes' higher than 'kern.maxfiles' 2025-05-21 12:07:52 +02:00
kgssapi
libkern libkern: strdup.c, strndup.c: Prefer memcpy() over bcopy() 2025-03-03 22:56:34 +08:00
modules mpi3mr: Remove -g 2025-05-12 16:45:35 -06:00
net bpf: Make bpf.h self-contained 2025-05-07 12:05:08 +00:00
net80211 net80211: fix a race between ieee80211_sta_join and scan entries 2025-05-08 21:22:59 +00:00
netgraph netgraph: MFC: prevent panic with INVARIANTS-enabled kernel 2025-04-13 05:02:16 +07:00
netinet netinet: Make in_systm.h self-contained 2025-05-07 12:04:59 +00:00
netinet6 netinet: Fix getcred sysctl handlers to do nothing if no input is given 2025-04-06 13:54:03 +00:00
netipsec pfkey2: use correct value for a key length 2025-03-13 13:21:30 +03:00
netlink netlink/route: validate family attribute 2025-03-31 10:31:21 -07:00
netpfil pf: explicitly NULL state key pointers 2025-04-21 22:25:46 +02:00
netsmb netsmb: Stop checking for failures from malloc(M_WAITOK) 2024-09-30 12:44:16 +08:00
nfs
nfsclient
nfsserver
nlm
ofed
opencrypto
powerpc powerpc: Always use BE tag in nexus_map_resource 2025-05-15 09:06:51 -04:00
riscv riscv: Fix handling of interrupts during kernel page faults 2025-04-29 00:44:01 +00:00
rpc nfs, rpc: Ensure kernel credentials have at least one group 2024-11-15 11:47:43 +01:00
security MAC/do: Fix a too stringent debug assertion for a target of 'uid=*' 2025-05-27 23:01:54 +02:00
sys sysctl(9): Ease exporting struct sizes; Discourage doing that 2025-05-13 14:41:33 +02:00
teken
tests tests: Stop checking for failures from malloc(M_WAITOK) 2024-09-30 12:44:18 +08:00
tools vnode: Make the vop_vector reference a pointer to const 2024-12-03 01:02:10 +00:00
ufs Get consistent updates for UFS superblocks. 2025-03-28 17:41:04 -07:00
vm vm_pageout: Disallow invalid values for act_scan_laundry_weight 2025-05-03 20:22:08 +00:00
x86 x86/amdiommu: add amdiommu_is_running() 2025-04-09 03:53:17 +03:00
xdr rpc: Fix the definition of xdr_void() 2024-11-05 01:05:26 +00:00
xen
Makefile
README.md

FreeBSD Kernel Source:

This directory contains the source files and build glue that make up the FreeBSD kernel and its modules, including both original and contributed software.

Kernel configuration files are located in the conf/ subdirectory of each architecture. GENERIC is the configuration used in release builds. NOTES contains documentation of all possible entries. LINT is a compile-only configuration used to maximize build coverage and detect regressions.

Documentation:

Source code documentation is maintained in a set of man pages, under section 9. These pages are located in share/man/man9, from the top-level of the src tree. Consult intro(9) for an overview of existing pages.

Some additional high-level documentation of the kernel is maintained in the Architecture Handbook.

Source Roadmap:

Directory Description
amd64 AMD64 (64-bit x86) architecture support
arm 32-bit ARM architecture support
arm64 64-bit ARM (AArch64) architecture support
cam Common Access Method storage subsystem - cam(4) and ctl(4)
cddl CDDL-licensed optional sources such as DTrace
conf kernel build glue
compat Linux compatibility layer, FreeBSD 32-bit compatibility
contrib 3rd-party imported software such as OpenZFS
crypto crypto drivers
ddb interactive kernel debugger - ddb(4)
fs most filesystems, excluding UFS, NFS, and ZFS
dev device drivers and other arch independent code
gdb kernel remote GDB stub - gdb(4)
geom GEOM framework - geom(4)
i386 i386 (32-bit x86) architecture support
kern main part of the kernel
libkern libc-like and other support functions for kernel use
modules kernel module infrastructure
net core networking code
net80211 wireless networking (IEEE 802.11) - net80211(4)
netgraph graph-based networking subsystem - netgraph(4)
netinet IPv4 protocol implementation - inet(4)
netinet6 IPv6 protocol implementation - inet6(4)
netipsec IPsec protocol implementation - ipsec(4)
netpfil packet filters - ipfw(4), pf(4), and ipfilter(4)
opencrypto OpenCrypto framework - crypto(7)
powerpc PowerPC/POWER (32 and 64-bit) architecture support
riscv 64-bit RISC-V architecture support
security security facilities - audit(4) and mac(4)
sys kernel headers
tests kernel unit tests
ufs Unix File System - ffs(7)
vm virtual memory system
x86 code shared by AMD64 and i386 architectures