Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp3021159rdb; Tue, 6 Feb 2024 05:07:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IFnZljiB6ccC8VBMS0lTgNF9Iyf89z1Ljd2NnXcEIbbyQTPuonyfiRnMAMtngMvWBXTc2v4 X-Received: by 2002:a17:90b:3ec9:b0:296:c695:4962 with SMTP id rm9-20020a17090b3ec900b00296c6954962mr1647542pjb.41.1707224847551; Tue, 06 Feb 2024 05:07:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707224847; cv=pass; d=google.com; s=arc-20160816; b=U8chnIqd5YEDFunTtdh0G9f+5iYbWa0BGs5KX75g4ClVDPmkOYkw3o3SzYMpCaEEct 7f1zFYiyzOgAY33BHWlyktqE8bIkdmUHmYmez4pV4Ob6yF++BQnzBPCTPPXuq7y3i7Bq PjsHMj1Pv1iNW5nxeNC6cH8gtpTgA6akvgSYSVssEuGjeHbByDbUca/nzwsOYBH7EswN b0PCV70CKxFXjjZJwEtXlg49n/pPFk63wyTjR5HoCidNsT0178lx5/80igFHqKSGKrEC sqRIZ3xtRhngE50qzoHOKoZcsxM5n42acklamaSFNLYRQMSbtV7YKADqU88hq4ed/dKc iEgg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:references:in-reply-to:subject:cc:to:from:message-id :date:dkim-signature:dkim-signature:dkim-signature:dkim-signature; bh=uIELh7o1rf7Q9H6OH9+qJK0Dq6Bc0SJb8oKPETTnW6M=; fh=6brbHFyNQjfadoN/jRu8XWr2rUMz0/XdLKVEk8rMymA=; b=KNa62uatTvJ5Bu0y4A9mcuDaFRcVHDwT5qstNWNt9EgtlzFEpzG42kPr2YdYkf0/Es h7BP4IYS0ambgg3SBjuildewWq0ekoqegdrbzvNfugbkAxQeouUamjjltH/hemVHQ0gT Q1NqHIlpnhc8YTi9HiO2ucUCTZx78WKsGaj21CeZn2uI2SBtPE3V/VE1yWwrVLIth8HH JHzu2Oo4gKd/eRHKhUIvQoK0rINLxMTIO3uHyXVm2zldANNB7+wicKB6I9Wb2nRyXSYV ZC8nnyv+QETGie3zOJohxSuviVHIXeDOpxP+S1FwtXrqRfgZGE//N6vBtPpqY9qOq7ek Lhgw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b="Rq77jRC/"; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=VNSmwHXL; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; arc=pass (i=1 spf=pass spfdomain=suse.de dkim=pass dkdomain=suse.de dkim=pass dkdomain=suse.de dmarc=pass fromdomain=suse.de); spf=pass (google.com: domain of linux-kernel+bounces-54991-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54991-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de X-Forwarded-Encrypted: i=1; AJvYcCVtD7dh+Epfj+A803Knzwd0VJNsCgALW8DunkksI3f5MIXtk0CuFAeYBtnPR+xINwAaFOPVs441C6xAcyR83Y8cuqgFY8cO8z1tTjmHVw== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id lb4-20020a17090b4a4400b00296c2af4c0bsi1138287pjb.23.2024.02.06.05.07.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:07:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54991-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b="Rq77jRC/"; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=VNSmwHXL; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; arc=pass (i=1 spf=pass spfdomain=suse.de dkim=pass dkdomain=suse.de dkim=pass dkdomain=suse.de dmarc=pass fromdomain=suse.de); spf=pass (google.com: domain of linux-kernel+bounces-54991-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54991-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 1BCF1286038 for ; Tue, 6 Feb 2024 13:07:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 38785130AFE; Tue, 6 Feb 2024 13:07:15 +0000 (UTC) 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 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 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-kernel@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; > }