From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 576DE5B1E8; Tue, 6 Feb 2024 13:07:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707224834; cv=none; b=pVIuIAkGfiUjw96aFyOiabi1/tyE4kzQTJAJcijhbXkS5zEuJnX5ikk0zeZSa78KUauL347hcBC/0//5xdiH5HlN7tL6WzhEAAA9pNdmisYvK4o8IS6C6Rn52Kb7gG2j8oVQIlVsoBSrMqzrBGPXoKkm9uiUjDHhZc0elZXWov4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707224834; c=relaxed/simple; bh=Rk5h5UIopbKH5U0G82QzovYfd7Xtz6kEqds0jEkVH8s=; h=Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type; b=Q0JT/6uqPSf6ybNbk1zYODJKjHWP6ZsNGTp3IBL9GF/sCHmHdVwkqmlZlPPUyBHGBvRT+rd885YBzIlFVfMtWyv4aKKAOJhzl9nPL+okIaVgqUgXMatutu0UY6g6J+czwNB6xFvcB54rHa+NXsukJfES+wCU4iPZGERK3Cqzsqk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=Rq77jRC/; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=E7cC4vu+; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=VNSmwHXL; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=OUot+J0z; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="Rq77jRC/"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="E7cC4vu+"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="VNSmwHXL"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="OUot+J0z" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 81A841F385; Tue, 6 Feb 2024 13:07:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707224830; 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=uIELh7o1rf7Q9H6OH9+qJK0Dq6Bc0SJb8oKPETTnW6M=; b=Rq77jRC/vRJKsbIrwrA0fKPplFv9GALPkxsoAUlBzXGT+J6cwSSUX27enNncFkNvRTODR5 P0hHQOaWvYF6pCrOdatV+0+R5rCGUbiNgqzToVsN1+MsL5W1g1V5FwoOVWrRSi1GdwzivJ 8tHc25qsy88t4VlYFsJUbPOSIwhxq4E= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707224830; 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=uIELh7o1rf7Q9H6OH9+qJK0Dq6Bc0SJb8oKPETTnW6M=; b=E7cC4vu+ixHkjarke+cEHhy63uJ/aTzXRw7uYPIuSIX9oRAA6LcXcGke6d1+E+LnlhFN7E +aAwR75LKjpk29Cg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707224828; 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=uIELh7o1rf7Q9H6OH9+qJK0Dq6Bc0SJb8oKPETTnW6M=; b=VNSmwHXLZV1vQnXC8Qzm6jeAOmw2ZzmR36+X5vhFEhN4HO7eM8dSunrFsCfHZelrynPoKF JNRbB7P9PjmgSN2jgIML8T3ig7WaSjVa98NcIsZyBaJTUDJ+GooknWJEol+LBPU5JuBuWl zLByi4qudwxUzVE/kZEFkiXw12vZ5Aw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707224828; 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=uIELh7o1rf7Q9H6OH9+qJK0Dq6Bc0SJb8oKPETTnW6M=; b=OUot+J0z9hiWb651NB0borJfEJwa57lzNq0aAwl9ePJy2TdFKGJE1t25x2f41888iOTUBi AY0coTgyhbrSAADw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 3209F132DD; Tue, 6 Feb 2024 13:07:08 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap1.dmz-prg2.suse.org with ESMTPSA id A7yVC/wuwmW2NgAAD6G6ig (envelope-from ); Tue, 06 Feb 2024 13:07:08 +0000 Date: Tue, 06 Feb 2024 14:07:03 +0100 Message-ID: <87y1bxvj0o.wl-tiwai@suse.de> From: Takashi Iwai To: Wesley Cheng Cc: , , , , , , , , , , , , , , , , , , , , , Subject: Re: [PATCH v13 35/53] ALSA: usb-audio: Prevent starting of audio stream if in use In-Reply-To: <20240203023645.31105-36-quic_wcheng@quicinc.com> References: <20240203023645.31105-1-quic_wcheng@quicinc.com> <20240203023645.31105-36-quic_wcheng@quicinc.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) Emacs/27.2 Mule/6.0 Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: X-Spam-Score: -1.80 X-Spamd-Result: default: False [-1.80 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; BAYES_HAM(-3.00)[100.00%]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TAGGED_RCPT(0.00)[dt]; MIME_GOOD(-0.10)[text/plain]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_RATELIMIT(0.00)[to_ip_from(RLjs3ec4aura4kmsd6wxjjm4hg)]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; RCPT_COUNT_TWELVE(0.00)[23]; MID_CONTAINS_FROM(1.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[quicinc.com:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; FREEMAIL_CC(0.00)[linaro.org,intel.com,perex.cz,kernel.org,lwn.net,gmail.com,linuxfoundation.org,synopsys.com,quicinc.com,suse.com,vger.kernel.org,alsa-project.org]; RCVD_TLS_ALL(0.00)[]; SUSPICIOUS_RECIPS(1.50)[] X-Spam-Flag: NO On Sat, 03 Feb 2024 03:36:27 +0100, Wesley Cheng wrote: > > With USB audio offloading, an audio session is started from the ASoC > platform sound card and PCM devices. Likewise, the USB SND path is still > readily available for use, in case the non-offload path is desired. In > order to prevent the two entities from attempting to use the USB bus, > introduce a flag that determines when either paths are in use. > > If a PCM device is already in use, the check will return an error to > userspace notifying that the stream is currently busy. This ensures that > only one path is using the USB substream. > > Signed-off-by: Wesley Cheng Hm, I'm not sure whether it's safe to hold chip->mutex there for the long code path. It even kicks off the auto-resume, which may call various functions at resuming, and some of them may re-hold chip->mutex. If it's only about the open flag, protect only the flag access with the mutex, not covering the all open function. At least the re-entry can be avoided by that. thanks, Takashi > --- > sound/usb/card.h | 1 + > sound/usb/pcm.c | 19 +++++++++++++++++-- > 2 files changed, 18 insertions(+), 2 deletions(-) > > diff --git a/sound/usb/card.h b/sound/usb/card.h > index ed4a664e24e5..6d59995440c3 100644 > --- a/sound/usb/card.h > +++ b/sound/usb/card.h > @@ -165,6 +165,7 @@ struct snd_usb_substream { > unsigned int pkt_offset_adj; /* Bytes to drop from beginning of packets (for non-compliant devices) */ > unsigned int stream_offset_adj; /* Bytes to drop from beginning of stream (for non-compliant devices) */ > > + unsigned int opened:1; /* pcm device opened */ > unsigned int running: 1; /* running status */ > unsigned int period_elapsed_pending; /* delay period handling */ > > diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c > index 3adb09ce1702..c2cb52cd5d23 100644 > --- a/sound/usb/pcm.c > +++ b/sound/usb/pcm.c > @@ -1241,8 +1241,15 @@ static int snd_usb_pcm_open(struct snd_pcm_substream *substream) > struct snd_usb_stream *as = snd_pcm_substream_chip(substream); > struct snd_pcm_runtime *runtime = substream->runtime; > struct snd_usb_substream *subs = &as->substream[direction]; > + struct snd_usb_audio *chip = subs->stream->chip; > int ret; > > + mutex_lock(&chip->mutex); > + if (subs->opened) { > + mutex_unlock(&chip->mutex); > + return -EBUSY; > + } > + > runtime->hw = snd_usb_hardware; > /* need an explicit sync to catch applptr update in low-latency mode */ > if (direction == SNDRV_PCM_STREAM_PLAYBACK && > @@ -1259,13 +1266,17 @@ static int snd_usb_pcm_open(struct snd_pcm_substream *substream) > > ret = setup_hw_info(runtime, subs); > if (ret < 0) > - return ret; > + goto out; > ret = snd_usb_autoresume(subs->stream->chip); > if (ret < 0) > - return ret; > + goto out; > ret = snd_media_stream_init(subs, as->pcm, direction); > if (ret < 0) > snd_usb_autosuspend(subs->stream->chip); > + subs->opened = 1; > +out: > + mutex_unlock(&chip->mutex); > + > return ret; > } > > @@ -1274,6 +1285,7 @@ static int snd_usb_pcm_close(struct snd_pcm_substream *substream) > int direction = substream->stream; > struct snd_usb_stream *as = snd_pcm_substream_chip(substream); > struct snd_usb_substream *subs = &as->substream[direction]; > + struct snd_usb_audio *chip = subs->stream->chip; > int ret; > > snd_media_stop_pipeline(subs); > @@ -1287,6 +1299,9 @@ static int snd_usb_pcm_close(struct snd_pcm_substream *substream) > > subs->pcm_substream = NULL; > snd_usb_autosuspend(subs->stream->chip); > + mutex_lock(&chip->mutex); > + subs->opened = 0; > + mutex_unlock(&chip->mutex); > > return 0; > }