Skip to content

Fix guest sound playback XRUN issue#138

Open
Cuda-Chen wants to merge 1 commit into
sysprog21:masterfrom
Cuda-Chen:fix-vsnd-playback-xrun
Open

Fix guest sound playback XRUN issue#138
Cuda-Chen wants to merge 1 commit into
sysprog21:masterfrom
Cuda-Chen:fix-vsnd-playback-xrun

Conversation

@Cuda-Chen

@Cuda-Chen Cuda-Chen commented May 31, 2026

Copy link
Copy Markdown
Collaborator

Fix guest OS sound playback XRUN (underrun, specifically) issue.

As using a concurrent queue with conditional variables holding PCM
frames, apply the famous self pipe trick so that the emulator can
safely handle asynchronous signals when virtio-snd is enabled.

Test Procedures

  1. Set these parameters to =y in the Linux Kernel config:
CONFIG_SND_PCM_XRUN_DEBUG=y
CONFIG_SND_VERBOSE_PROCFS=y
CONFIG_SND_DEBUG=y
  1. Remove the existing config using rm linux/.config.
  2. Compile the Linux Kernel with ./scripts/build-image.sh --linux.
  3. Run make check to run emulated guest OS.
  4. After login into guest OS, run echo 3 > /proc/asound/card0/pcmp0/xrun_debug.
  5. Run aplay /usr/share/sounds/alsa/Front_Center.wav three times in guest OS.
  6. In guest OS, run dmesg.

Expected Results

  • The dmesg should not produce any XRUN messages, for example:
# dmesg
...
[   65.908925] ALSA: PCM: [Q] Lost interrupts?: (stream=1, delta=4000, new_hw_ptr=4000, old_hw_ptr=0)           
[   65.913926] CPU: 0 UID: 0 PID: 10 Comm: kworker/0:1 Not tainted 6.12.61 #1                                   
[   65.914926] Hardware name: semu (DT)                                                                         
[   65.914926] Workqueue: events virtsnd_pcm_period_elapsed                                                     
[   65.915927] Call Trace:                                                                                      
[   65.915927] [<c0004aa4>] dump_backtrace+0x2c/0x3c                                                            
[   65.916927] [<c0419a74>] show_stack+0x40/0x54                                                                
[   65.918927] [<c041f3d0>] dump_stack_lvl+0x74/0xa8                                                            
[   65.919928] [<c041f420>] dump_stack+0x1c/0x2c                                                                
[   65.920928] [<c030693c>] snd_pcm_update_hw_ptr0+0x46c/0x540                                                  
[   65.921928] [<c0306a7c>] snd_pcm_period_elapsed_under_stream_lock+0x6c/0x9c                                  
[   65.923929] [<c0306ae0>] snd_pcm_period_elapsed+0x34/0x54                                                    
[   65.924929] [<c030bc28>] virtsnd_pcm_period_elapsed+0x18/0x28                                                
[   65.926929] [<c0027760>] process_scheduled_works+0x1dc/0x2a4
[   65.927930] [<c0027d74>] worker_thread+0x170/0x208
[   65.928930] [<c002ff4c>] kthread+0xe0/0xe4
[   65.929930] [<c04276c8>] ret_from_exception_end+0x14/0x20

Summary by cubic

Fixes guest audio playback XRUNs and the missing last period by waking the event loop on each used-ring update, honoring the guest’s period size, and draining buffers before stop. Playback now ends cleanly without underruns or dropping the final period.

  • Bug Fixes
    • Enable the emulator wake pipe for VIRTIOSND; on each used-ring update raise IRQ with release semantics and write one wake byte so poll(-1) unblocks promptly.
    • Update used-ring len and index inside the processing loop to ensure the final period is reported.
    • Derive period frames from guest period_bytes; clear the release flag on START and wait for PortAudio to drain pending buffers before STOP.

Written for commit 2279fe3. Summary will update on new commits.

Review in cubic

@Cuda-Chen Cuda-Chen force-pushed the fix-vsnd-playback-xrun branch 5 times, most recently from ed76457 to 07f3dca Compare June 2, 2026 12:21
@Cuda-Chen Cuda-Chen marked this pull request as ready for review June 2, 2026 12:21
@Cuda-Chen Cuda-Chen requested a review from jserv June 2, 2026 12:21

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 3 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread virtio-snd.c
Fix guest OS sound playback XRUN (underrun, specifically) issue.

As using a concurrent queue with conditional variables holding PCM
frames, apply the famous self pipe trick so that the emulator can
safely handle asynchronous signals when virtio-snd is enabled.
@Cuda-Chen Cuda-Chen force-pushed the fix-vsnd-playback-xrun branch from 07f3dca to 2279fe3 Compare June 2, 2026 13:03
@Cuda-Chen Cuda-Chen changed the title [WIP] Fix guest sound playback XRUN issue Fix guest sound playback XRUN issue Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant