Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp2986676rwb; Mon, 5 Sep 2022 04:56:02 -0700 (PDT) X-Google-Smtp-Source: AA6agR6TjdXLx1BEOf82L2j+6eFVXflKpZx0KUvmRTFCLwd61H+qkCITdzx+BVc4O02eKnXnXVYg X-Received: by 2002:a17:902:db06:b0:16e:e5fc:56d8 with SMTP id m6-20020a170902db0600b0016ee5fc56d8mr49314639plx.100.1662378961950; Mon, 05 Sep 2022 04:56:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662378961; cv=none; d=google.com; s=arc-20160816; b=Aao+vYFto5971+Wfh8F68+TrZVp/cN784E9dFcUXB/CrTd7PKM0EmWZ0fsLmsB23r9 hugqCSRhInMW5RIrsjjSdGl+bTeNKdV7Uu3nlMA75YTS2nQqEmUl859q8hIb8MGR1+P7 DwQg4kmYmE4reLuOZCRNib8Sb05Jj7il52BH0o9u0M2OGL9DyayAKSyjgdM5umd+S0G0 Ypx5Ghm0BiP9HlDquXLOgAenHn4+sjb5ZeK7PbfMEO4hmTqCh0mwGFNI/ZUUwcbPDwe5 b5CevUmLTe7VttcmynA6HZAB8RbQpzgR+KZYJAG88EWwI4qYXqlKUHTQo0LYENe635Ya E4Xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:subject:cc:to:from:message-id :date:dkim-signature:dkim-signature; bh=pdjvV5vVZbGw/kdAksGm6cLcJpOHeP3800cvgy6tsTs=; b=VC8qrbBigzBG0sOqiD8u/i94DFPRZzwms/rFjq9NFu6z3F72h1KkB631XIpqBEzOv5 zTaEnETa3Ec0B6kHAXULM6avoatyJsMldItGl+iQuSsJLhcawwWhyQg61EMwtLft9pvZ L++60x28mvwSyMhXeRqx9YiHoR7x/7xt9/UrmnbkLS+CL0gPL6y5bSo5qeQ3E75jrOWu j9bPZwKv10PIpCG4exe8gXTq5mEnrZc2uW14W2viPqg6Af+4zj89PfAm1/hmCxrOhclH peCw12BbxCMYYp2YjHT1237rMFPjKJaIfSBHn41fjIGN3mAZesGyZSCqmRcMwHszYOCV kvSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=vNFl6wQY; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l10-20020a056a00140a00b0053852c8cd6csi10466955pfu.185.2022.09.05.04.55.50; Mon, 05 Sep 2022 04:56:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=vNFl6wQY; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238233AbiIELoF (ORCPT + 99 others); Mon, 5 Sep 2022 07:44:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238216AbiIELoD (ORCPT ); Mon, 5 Sep 2022 07:44:03 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32D545788A; Mon, 5 Sep 2022 04:44:02 -0700 (PDT) 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 DF89220E8E; Mon, 5 Sep 2022 11:44:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1662378240; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pdjvV5vVZbGw/kdAksGm6cLcJpOHeP3800cvgy6tsTs=; b=vNFl6wQYvAOm14d29IqL3inQp0PzKgBlQBqlxrT1KiF8tZ96lJNK8i+zKVRbmizHBYQu/r bSyfRMicACpUc4EscfMjhlFOEK+RTTR3ulj/Ff3wgtUJobzEVrHFPu4xks6MjzsQ8MxOwa cl4UEFiFvtG4VBo1lcMvKAnUbM2iviA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1662378240; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pdjvV5vVZbGw/kdAksGm6cLcJpOHeP3800cvgy6tsTs=; b=OsJPV8DRncMYyeMDdS7YXbXEn8BfgB8fWqgmBEEB8Ftv0DTawKsjg3qEiSikQOJGcZDzrE kzXtQ5Oq3rv8oDAw== 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 BDA6F139C7; Mon, 5 Sep 2022 11:44:00 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id vYybLQDhFWOxawAAMHmgww (envelope-from ); Mon, 05 Sep 2022 11:44:00 +0000 Date: Mon, 05 Sep 2022 13:44:00 +0200 Message-ID: <87sfl6jbb3.wl-tiwai@suse.de> From: Takashi Iwai To: "Jason A. Donenfeld" Cc: alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, =?ISO-8859-4?Q?Nikl=E0vs_Ko=B6es=F1ikovs?= <89q1r14hd@relay.firefox.com>, Wim Taymans Subject: Re: [PATCH] ALSA: usb-audio: Don't refcount multiple accesses on the single clock In-Reply-To: <20220905101403.1435037-1-Jason@zx2c4.com> References: <20220905101403.1435037-1-Jason@zx2c4.com> 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=ISO-8859-4 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 05 Sep 2022 12:14:03 +0200, Jason A. Donenfeld wrote: > > This reverts commit 03a8b0df757f1beb21ba1626e23ca7412e48b525. > This reverts commit c11117b634f4f832c4420d3cf41c44227f140ce1. > > Pipewire and PulseAudio start devices with 44.1khz before changing them > to 48khz (or something different). By locking the rate, daemons are > unable to enumerate possible rates, and so they never change them to a > more optimal rate. This revert patch should allow 48khz audio again. Well, in that case, the revert is no right solution, IMO. If the patch caused a problem, it means that the application tries to change the rate while it's being still running by another. If it worked, it worked just casually without noticing the bad behavior. That said, changing the rate for a tied stream with a different rate while another is actually running is anyway no-go. Maybe it should have better PCM hw constraint for covering this. We need to check more details. When you load snd-usb-audio with dyndbg=+p option, does it show the new error message "Mismatched sample rate xxx"? thanks, Takashi > > Fixes: c11117b634f4 ("ALSA: usb-audio: Refcount multiple accesses on the single clock") > Cc: stable@vger.kernel.org > Cc: Takashi Iwai > Cc: Nikl?vs Ko?es?ikovs <89q1r14hd@relay.firefox.com> > Cc: Wim Taymans > Link: https://lore.kernel.org/all/YxXIWv8dYmg1tnXP@zx2c4.com/ > Link: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/2620 > Signed-off-by: Jason A. Donenfeld > --- > sound/usb/card.c | 1 - > sound/usb/card.h | 3 +- > sound/usb/endpoint.c | 90 +++++--------------------------------------- > sound/usb/usbaudio.h | 1 - > 4 files changed, 10 insertions(+), 85 deletions(-) > > diff --git a/sound/usb/card.c b/sound/usb/card.c > index d356743de2ff..12bca2e85505 100644 > --- a/sound/usb/card.c > +++ b/sound/usb/card.c > @@ -643,7 +643,6 @@ static int snd_usb_audio_create(struct usb_interface *intf, > INIT_LIST_HEAD(&chip->pcm_list); > INIT_LIST_HEAD(&chip->ep_list); > INIT_LIST_HEAD(&chip->iface_ref_list); > - INIT_LIST_HEAD(&chip->clock_ref_list); > INIT_LIST_HEAD(&chip->midi_list); > INIT_LIST_HEAD(&chip->mixer_list); > > diff --git a/sound/usb/card.h b/sound/usb/card.h > index ca75f2206170..87f042d06ce0 100644 > --- a/sound/usb/card.h > +++ b/sound/usb/card.h > @@ -44,7 +44,6 @@ struct audioformat { > > struct snd_usb_substream; > struct snd_usb_iface_ref; > -struct snd_usb_clock_ref; > struct snd_usb_endpoint; > struct snd_usb_power_domain; > > @@ -63,7 +62,6 @@ struct snd_urb_ctx { > struct snd_usb_endpoint { > struct snd_usb_audio *chip; > struct snd_usb_iface_ref *iface_ref; > - struct snd_usb_clock_ref *clock_ref; > > int opened; /* open refcount; protect with chip->mutex */ > atomic_t running; /* running status */ > @@ -140,6 +138,7 @@ struct snd_usb_endpoint { > unsigned int cur_period_frames; > unsigned int cur_period_bytes; > unsigned int cur_buffer_periods; > + unsigned char cur_clock; > > spinlock_t lock; > struct list_head list; > diff --git a/sound/usb/endpoint.c b/sound/usb/endpoint.c > index 0d7b73bf7945..0f5e9224a799 100644 > --- a/sound/usb/endpoint.c > +++ b/sound/usb/endpoint.c > @@ -35,14 +35,6 @@ struct snd_usb_iface_ref { > struct list_head list; > }; > > -/* clock refcounting */ > -struct snd_usb_clock_ref { > - unsigned char clock; > - atomic_t locked; > - int rate; > - struct list_head list; > -}; > - > /* > * snd_usb_endpoint is a model that abstracts everything related to an > * USB endpoint and its streaming. > @@ -599,25 +591,6 @@ iface_ref_find(struct snd_usb_audio *chip, int iface) > return ip; > } > > -/* Similarly, a refcount object for clock */ > -static struct snd_usb_clock_ref * > -clock_ref_find(struct snd_usb_audio *chip, int clock) > -{ > - struct snd_usb_clock_ref *ref; > - > - list_for_each_entry(ref, &chip->clock_ref_list, list) > - if (ref->clock == clock) > - return ref; > - > - ref = kzalloc(sizeof(*ref), GFP_KERNEL); > - if (!ref) > - return NULL; > - ref->clock = clock; > - atomic_set(&ref->locked, 0); > - list_add_tail(&ref->list, &chip->clock_ref_list); > - return ref; > -} > - > /* > * Get the existing endpoint object corresponding EP > * Returns NULL if not present. > @@ -795,14 +768,6 @@ snd_usb_endpoint_open(struct snd_usb_audio *chip, > goto unlock; > } > > - if (fp->protocol != UAC_VERSION_1) { > - ep->clock_ref = clock_ref_find(chip, fp->clock); > - if (!ep->clock_ref) { > - ep = NULL; > - goto unlock; > - } > - } > - > ep->cur_audiofmt = fp; > ep->cur_channels = fp->channels; > ep->cur_rate = params_rate(params); > @@ -812,6 +777,7 @@ snd_usb_endpoint_open(struct snd_usb_audio *chip, > ep->cur_period_frames = params_period_size(params); > ep->cur_period_bytes = ep->cur_period_frames * ep->cur_frame_bytes; > ep->cur_buffer_periods = params_periods(params); > + ep->cur_clock = fp->clock; > > if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC) > endpoint_set_syncinterval(chip, ep); > @@ -928,8 +894,8 @@ void snd_usb_endpoint_close(struct snd_usb_audio *chip, > ep->altsetting = 0; > ep->cur_audiofmt = NULL; > ep->cur_rate = 0; > + ep->cur_clock = 0; > ep->iface_ref = NULL; > - ep->clock_ref = NULL; > usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num); > } > mutex_unlock(&chip->mutex); > @@ -941,8 +907,6 @@ void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep) > ep->need_setup = true; > if (ep->iface_ref) > ep->iface_ref->need_setup = true; > - if (ep->clock_ref) > - ep->clock_ref->rate = 0; > } > > /* > @@ -1350,33 +1314,6 @@ static int snd_usb_endpoint_set_params(struct snd_usb_audio *chip, > return 0; > } > > -static int init_sample_rate(struct snd_usb_audio *chip, > - struct snd_usb_endpoint *ep) > -{ > - struct snd_usb_clock_ref *clock = ep->clock_ref; > - int err; > - > - if (clock) { > - if (atomic_read(&clock->locked)) > - return 0; > - if (clock->rate == ep->cur_rate) > - return 0; > - if (clock->rate && clock->rate != ep->cur_rate) { > - usb_audio_dbg(chip, "Mismatched sample rate %d vs %d for EP 0x%x\n", > - clock->rate, ep->cur_rate, ep->ep_num); > - return -EINVAL; > - } > - } > - > - err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, ep->cur_rate); > - if (err < 0) > - return err; > - > - if (clock) > - clock->rate = ep->cur_rate; > - return 0; > -} > - > /* > * snd_usb_endpoint_configure: Configure the endpoint > * > @@ -1406,7 +1343,8 @@ int snd_usb_endpoint_configure(struct snd_usb_audio *chip, > * to update at each EP configuration > */ > if (ep->cur_audiofmt->protocol == UAC_VERSION_1) { > - err = init_sample_rate(chip, ep); > + err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, > + ep->cur_rate); > if (err < 0) > goto unlock; > } > @@ -1436,7 +1374,7 @@ int snd_usb_endpoint_configure(struct snd_usb_audio *chip, > if (err < 0) > goto unlock; > > - err = init_sample_rate(chip, ep); > + err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, ep->cur_rate); > if (err < 0) > goto unlock; > > @@ -1469,15 +1407,15 @@ int snd_usb_endpoint_configure(struct snd_usb_audio *chip, > /* get the current rate set to the given clock by any endpoint */ > int snd_usb_endpoint_get_clock_rate(struct snd_usb_audio *chip, int clock) > { > - struct snd_usb_clock_ref *ref; > + struct snd_usb_endpoint *ep; > int rate = 0; > > if (!clock) > return 0; > mutex_lock(&chip->mutex); > - list_for_each_entry(ref, &chip->clock_ref_list, list) { > - if (ref->clock == clock) { > - rate = ref->rate; > + list_for_each_entry(ep, &chip->ep_list, list) { > + if (ep->cur_clock == clock && ep->cur_rate) { > + rate = ep->cur_rate; > break; > } > } > @@ -1518,9 +1456,6 @@ int snd_usb_endpoint_start(struct snd_usb_endpoint *ep) > if (atomic_inc_return(&ep->running) != 1) > return 0; > > - if (ep->clock_ref) > - atomic_inc(&ep->clock_ref->locked); > - > ep->active_mask = 0; > ep->unlink_mask = 0; > ep->phase = 0; > @@ -1630,9 +1565,6 @@ void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, bool keep_pending) > if (ep->sync_source) > WRITE_ONCE(ep->sync_source->sync_sink, NULL); > stop_urbs(ep, false, keep_pending); > - if (ep->clock_ref) > - if (!atomic_dec_return(&ep->clock_ref->locked)) > - ep->clock_ref->rate = 0; > } > } > > @@ -1659,16 +1591,12 @@ void snd_usb_endpoint_free_all(struct snd_usb_audio *chip) > { > struct snd_usb_endpoint *ep, *en; > struct snd_usb_iface_ref *ip, *in; > - struct snd_usb_clock_ref *cp, *cn; > > list_for_each_entry_safe(ep, en, &chip->ep_list, list) > kfree(ep); > > list_for_each_entry_safe(ip, in, &chip->iface_ref_list, list) > kfree(ip); > - > - list_for_each_entry_safe(cp, cn, &chip->clock_ref_list, list) > - kfree(cp); > } > > /* > diff --git a/sound/usb/usbaudio.h b/sound/usb/usbaudio.h > index ffbb4b0d09a0..044cd7ab27cb 100644 > --- a/sound/usb/usbaudio.h > +++ b/sound/usb/usbaudio.h > @@ -45,7 +45,6 @@ struct snd_usb_audio { > struct list_head pcm_list; /* list of pcm streams */ > struct list_head ep_list; /* list of audio-related endpoints */ > struct list_head iface_ref_list; /* list of interface refcounts */ > - struct list_head clock_ref_list; /* list of clock refcounts */ > int pcm_devs; > > struct list_head midi_list; /* list of midi interfaces */ > -- > 2.37.3 >