From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8A4ECC433FE for ; Tue, 22 Nov 2022 14:23:47 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 3C5961634; Tue, 22 Nov 2022 15:22:55 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 3C5961634 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1669127025; bh=3GHMzo9Kwxp+tAmGKx+hg0Fh30kDQ/Ay1GPO2mDOlhY=; h=Date:From:To:Subject:In-Reply-To:References:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=H9gv28WGcMbJf4VGxwu6agY1fhpsP9scGPEFIqrAzjjDLQMe8nEl832w5dbCdcDTr DTGds6f/mfuA8WFgJcAAPAdKY8DBA8RRObkXbhYeLStPkqI1xYq1wpczYIYJFEvWa0 wK+wj/nHRvI54faNM23WmMugxSYns02nt73cOvio= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id D943CF8026A; Tue, 22 Nov 2022 15:22:54 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id B9796F80272; Tue, 22 Nov 2022 15:22:53 +0100 (CET) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 3D78EF80154 for ; Tue, 22 Nov 2022 15:22:50 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 3D78EF80154 Authentication-Results: alsa1.perex.cz; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="X0k1Hxj5"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="pVc8f6uI" Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id A06911F8AB; Tue, 22 Nov 2022 14:22:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1669126970; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=OqwC4qCf+MDVySDLbLGnlO+Dj7CVgo11u4ylP6+E3no=; b=X0k1Hxj5lopSud0Qmb9jiHnJismAF7WxFqVHNOWHDjsxUKvUDn18U8AZmQpevitDDzXROY stxTJZ8TWVWRtqnaV7IkAsykh7a8AhoQM2tuEcB6opcVTXVShYZ+CjixsBZIwy/N7JQrM2 7W5LxdyycRzSJL82rUMRs4FVaHQpQa0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1669126970; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=OqwC4qCf+MDVySDLbLGnlO+Dj7CVgo11u4ylP6+E3no=; b=pVc8f6uIp0BRxfEY2s48eEtZdjgMykn4Rh1LM0d/lx787k3VzDVoSnVoAZPrYCnlwF1EWc Ez3mcvhFrYw+WICg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 87AD013AA1; Tue, 22 Nov 2022 14:22:50 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id ZxpwIDrbfGN6bwAAMHmgww (envelope-from ); Tue, 22 Nov 2022 14:22:50 +0000 Date: Tue, 22 Nov 2022 15:22:50 +0100 Message-ID: <87bkoz6pc5.wl-tiwai@suse.de> From: Takashi Iwai To: Carl Hetherington Subject: Re: Query about xrun on usb/pcm In-Reply-To: <87edtv6pi6.wl-tiwai@suse.de> References: <87y1s3v4ba.wl-tiwai@suse.de> <87edtv6pi6.wl-tiwai@suse.de> User-Agent: Wanderlust/2.15.9 (Almost Unreal) Emacs/27.2 Mule/6.0 MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII Cc: alsa-devel@alsa-project.org X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" On Tue, 22 Nov 2022 15:19:13 +0100, Takashi Iwai wrote: > > On Tue, 22 Nov 2022 12:16:47 +0100, > Carl Hetherington wrote: > > > > Hi Takashi, > > > > Thank you for getting back to me! > > > > On Tue, 22 Nov 2022, Takashi Iwai wrote: > > > > [snip] > > > > > > Now, snd_usb_endpoint_start() is called on 0x2 and that is fine. Next, > > > > snd_usb_endpoint_start() is called on 0x82 and that fails because its > > > > state is still STOPPING. > > > > > > > > At this point things seem broken. > > > > > > > > Does anyone have a hint about where in this sequence things are going > > > > wrong, and maybe even why? > > > > > > The problem is that it's treating XRUNs on the both streams > > > individually. It's correct to recover only the PCM stream when an > > > XRUN is reported to the PCM stream. However, for an XRUN on the > > > capture stream that serves as a sync source, it should stop and > > > recover not only the capture PCM stream but also the playback stream > > > as a sync sink as well. > > > > > > Below is a possible test fix (totally untested!). > > > This may give XRUNs twice eventually, which is a bit confusing, but it > > > aligns with the actual hardware behavior, at least. > > > > [snip fix] > > > > Makes sense, thank you! Sadly, the fix doesn't seem to work because (I > > think) the xruns I'm seeing come via a different path (not though > > notify_xrun()). Mine arrive via this trace: > > > > __snd_pcm_xrun > > snd_pcm_update_state > > snd_pcm_update_hw_ptr > > usb_hcd_giveback_urb > > snd_pcm_period_elapsed_under_stream_lock > > snd_pcm_period_elapsed > > retire_capture_urb > > snd_complete_urb > > > > I'll see if can apply a similar fix to this case, though to my naive > > eyes it looks a little trickier as the xrun is found in the snd_pcm > > code rather than the USB code. Any suggestions most welcome! > > OK, then it's a bit different problem, and not so trivial to fix in > the kernel side alone, I'm afraid. Basically it's a race between > start and stop of two streams. The key point is that, for stopping a > (USB) stream, a sync-stop operation is needed, and this can't be > performed at the PCM trigger itself (which is an atomic operation). > So, the kernel trigger may at most return an error there. > > I assume that it's from snd_usb_endpoint_start() and it returning > -EPIPE error. If so, we may change the PCM core code to set the PCM > state again XRUN in such an error case, so that application may repeat > the standard recovery process. Something like below. Also, it might be slightly better if we swap the starting order of two streams: sync at first, then data. A race can still happen, though. Takashi -- 8< -- --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -241,19 +241,19 @@ static int start_endpoints(struct snd_usb_substream *subs) if (!subs->data_endpoint) return -EINVAL; - if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) { - err = snd_usb_endpoint_start(subs->data_endpoint); + if (subs->sync_endpoint && + !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) { + err = snd_usb_endpoint_start(subs->sync_endpoint); if (err < 0) { - clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags); + clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags); goto error; } } - if (subs->sync_endpoint && - !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) { - err = snd_usb_endpoint_start(subs->sync_endpoint); + if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) { + err = snd_usb_endpoint_start(subs->data_endpoint); if (err < 0) { - clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags); + clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags); goto error; } }