From patchwork Fri Nov 22 17:52:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gabbasov, Andrew" X-Patchwork-Id: 11258209 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D0F5C913 for ; Fri, 22 Nov 2019 17:54:11 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 622CE2071B for ; Fri, 22 Nov 2019 17:54:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alsa-project.org header.i=@alsa-project.org header.b="Ckg9ydz8" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 622CE2071B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=mentor.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-devel-bounces@alsa-project.org 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 5DF06181C; Fri, 22 Nov 2019 18:53:19 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 5DF06181C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1574445249; bh=z5ymzH3Bxjp1709CohWAe5OrJLQEEdMA/RvFA3JmTTk=; h=From:To:Date:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From; b=Ckg9ydz8bSqkiVDQMrDrAuRe4eMDE/9K85qVV3jtFKch/nOZgSeZDhwQZs3EftmkZ WwGkVwVcsyK10HQCfB3/4NpTvaJJ+oleVvsPtbVa09m5gXJf14b57lbDFi6QZLk/0t 1KdcN38ZyvxJAcrVBjvcJRA1yGZFc7aQpE4kEsRE= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id C0064F80140; Fri, 22 Nov 2019 18:53:18 +0100 (CET) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa1.perex.cz (Postfix, from userid 50401) id 9BA58F800EF; Fri, 22 Nov 2019 18:53:17 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on alsa1.perex.cz X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from esa2.mentor.iphmx.com (esa2.mentor.iphmx.com [68.232.141.98]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 71156F800EF for ; Fri, 22 Nov 2019 18:53:13 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 71156F800EF IronPort-SDR: 4EZfq6p9ngHgFGcN2Aoysp3vMnB21Uy35m/HOmMF7lWXxYqYEVh56wlSwQJ6pZ+XwmM0fpb/Oy SQ3JtbItDLFjT6r3SliE4cI7axB1iTeg8bfIqT8EMoXWNVI5WOtK5q/y8Gj/URLzj2Upb1qmJ+ AKp9GyBxpi4Y7c1rDNbD0zC7iuarqLpLJDJyREE1qaU0NFSlUpXx3nYbdUL+DL6tKv1wS/u0Z0 RXNXCZ5KYj9f7kF+a+MePviDUzWLfiJNnmMeno3GC8tU6dOgMTuJDReZGW5TC9JbvZX+7T+7Qm dLw= X-IronPort-AV: E=Sophos;i="5.69,230,1571731200"; d="scan'208";a="43373733" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa2.mentor.iphmx.com with ESMTP; 22 Nov 2019 09:53:11 -0800 IronPort-SDR: wF5LDk+dyX65opAPimVh69XX/p/Naarbwluir5E8WXg8Mh0HUe+SXC8pOKFVrRGqAOu3hN6cch XJnpWvbXeMUYwWTrqBe8NoyKJhslb9l1EE/GHcqm3Kokds2pGICYJMBLZTDZy1j/D7jwwA9Lqp ThFcxkRlL6LVOITHjJTADJcYLSL6xaAcmbN2fDh+hlVjtklIR58xN5LkhAfXMkPyuXbW0jU4wS ZUAw361MxV7ZEZi7x9txEyBAA9noHSNzxH9ZeprSXCbWypQSPhZTYOuX4ZDuZ9eBJimjmfCTXf /Go= From: Andrew Gabbasov To: , , Jaroslav Kysela , Takashi Iwai , Timo Wischer , Andrew Gabbasov Date: Fri, 22 Nov 2019 11:52:17 -0600 Message-ID: <20191122175218.17187-1-andrew_gabbasov@mentor.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: SVR-IES-MBX-09.mgc.mentorg.com (139.181.222.9) To svr-ies-mbx-02.mgc.mentorg.com (139.181.222.2) Subject: [alsa-devel] [PATCH 1/2] ALSA: aloop: Remove redundant locking in timer open function 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" loopback_parse_timer_id() uses snd_card_ref(), that can lock on mutex, also snd_timer_instance_new() uses non-atomic allocation, that can sleep. So, both functions can not be called from loopback_snd_timer_open() with cable->lock spinlock locked. Moreover, most part of loopback_snd_timer_open() function body works when the opposite stream of the same cable does not yet exist, and the current stream is not yet completely open and can't be running, so existing locking of loopback->cable_lock mutex is enough to protect from conflicts with simultaneous opening or closing. Locking of cable->lock spinlock is not needed in this case. Fixes: 26c53379f98d ("ALSA: aloop: Support selection of snd_timer instead of jiffies") Signed-off-by: Andrew Gabbasov --- sound/drivers/aloop.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/sound/drivers/aloop.c b/sound/drivers/aloop.c index 1408403f727a..6408932f5f72 100644 --- a/sound/drivers/aloop.c +++ b/sound/drivers/aloop.c @@ -1107,20 +1107,18 @@ static int loopback_snd_timer_open(struct loopback_pcm *dpcm) struct snd_timer_instance *timeri; struct loopback_cable *cable = dpcm->cable; - spin_lock_irq(&cable->lock); - /* check if timer was already opened. It is only opened once * per playback and capture subdevice (aka cable). */ if (cable->snd_timer.instance) - goto unlock; + goto exit; err = loopback_parse_timer_id(dpcm->loopback->timer_source, &tid); if (err < 0) { pcm_err(dpcm->substream->pcm, "Parsing timer source \'%s\' failed with %d", dpcm->loopback->timer_source, err); - goto unlock; + goto exit; } cable->snd_timer.stream = dpcm->substream->stream; @@ -1129,7 +1127,7 @@ static int loopback_snd_timer_open(struct loopback_pcm *dpcm) timeri = snd_timer_instance_new(dpcm->loopback->card->id); if (!timeri) { err = -ENOMEM; - goto unlock; + goto exit; } /* The callback has to be called from another tasklet. If * SNDRV_TIMER_IFLG_FAST is specified it will be called from the @@ -1148,10 +1146,9 @@ static int loopback_snd_timer_open(struct loopback_pcm *dpcm) tasklet_init(&cable->snd_timer.event_tasklet, loopback_snd_timer_tasklet, (unsigned long)timeri); - /* snd_timer_close() and snd_timer_open() should not be called with - * locked spinlock because both functions can block on a mutex. The - * mutex loopback->cable_lock is kept locked. Therefore snd_timer_open() - * cannot be called a second time by the other device of the same cable. + /* The mutex loopback->cable_lock is kept locked. + * Therefore snd_timer_open() cannot be called a second time + * by the other device of the same cable. * Therefore the following issue cannot happen: * [proc1] Call loopback_timer_open() -> * Unlock cable->lock for snd_timer_close/open() call @@ -1160,9 +1157,7 @@ static int loopback_snd_timer_open(struct loopback_pcm *dpcm) * [proc1] Call snd_timer_open() and overwrite running timer * instance */ - spin_unlock_irq(&cable->lock); err = snd_timer_open(timeri, &cable->snd_timer.id, current->pid); - spin_lock_irq(&cable->lock); if (err < 0) { pcm_err(dpcm->substream->pcm, "snd_timer_open (%d,%d,%d) failed with %d", @@ -1171,14 +1166,12 @@ static int loopback_snd_timer_open(struct loopback_pcm *dpcm) cable->snd_timer.id.subdevice, err); snd_timer_instance_free(timeri); - goto unlock; + goto exit; } cable->snd_timer.instance = timeri; -unlock: - spin_unlock_irq(&cable->lock); - +exit: return err; } From patchwork Fri Nov 22 17:52:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gabbasov, Andrew" X-Patchwork-Id: 11258213 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 12A311593 for ; Fri, 22 Nov 2019 17:55:06 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 9C5752071B for ; Fri, 22 Nov 2019 17:55:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alsa-project.org header.i=@alsa-project.org header.b="msQmpJP0" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9C5752071B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=mentor.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-devel-bounces@alsa-project.org 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 641581836; Fri, 22 Nov 2019 18:54:13 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 641581836 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1574445303; bh=eRf/OpqkS1LayJ6tE4oOfSWU/0ggEEAIed27TlxaPBg=; h=From:To:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=msQmpJP0Ck6Hc0nfDBnUUhxn45Xd/L1fm4OMFz76al1CTD9kmjfBwcpJKoVWvnP83 K/OVvkaP2xY+0O0DD8XA9yYzZUrg8DMDTXkDSRU7AoGkpLXlqOD9dyTWbSQyQx/XXU sRlqqk12fFvqV+x869POpsl8nlOO3udH6kRQfw0I= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id E129EF80157; Fri, 22 Nov 2019 18:53:22 +0100 (CET) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa1.perex.cz (Postfix, from userid 50401) id 69B81F80159; Fri, 22 Nov 2019 18:53:19 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on alsa1.perex.cz X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=SPF_HELO_NONE,SPF_PASS, SURBL_BLOCKED,URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from esa2.mentor.iphmx.com (esa2.mentor.iphmx.com [68.232.141.98]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 61E08F8013F for ; Fri, 22 Nov 2019 18:53:16 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 61E08F8013F IronPort-SDR: XTC+PDTauJzQLksOhMEFskb9GRYfuuglO/upcsJqtF8HIUaxREnP1bSXWqXg3FLnNKbzyHH/2t eRAYYXwsQx4uvGHocLsdID9f2qzYZhM/nBMb8/rhAH8f4+HjMuqsd7bwHlRVv4x0zoAuzvg5jH YUyQwUFFAxrMvlycFN553ORmGS+hGOaIDEnKZv7D4PKCdNY55CpB5jGlO4q+AoFGKWMbFBDpSF 23QZcpZ0mnGM25Rxq7kX5Ki7oB8LtKF06l7pzVzBDffCDc4NHthx4OrolTdKCP9EsN3Hu8/cI9 g6k= X-IronPort-AV: E=Sophos;i="5.69,230,1571731200"; d="scan'208";a="43373736" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa2.mentor.iphmx.com with ESMTP; 22 Nov 2019 09:53:15 -0800 IronPort-SDR: 2gCvD/dQEOuAHYexa/Y5xnz/L+2hxKOQnlRoNmnKdX8n+7SX1wRjZJ9js11Z1yXmCbCUJJ1RO7 h5rdzTNVMa/xLwc0d7woUuf4VqFyQHjfNugZF4wuQXkYhtucY5g+FWXorbGYYHDzSHtO4G47vj nycLJlCNZt1jGZ0GG8OqmLzg2T3xBIW950ppdf4xb4hfit5QrHrlchnmgsmjddr8WAYSt+kYhH FTl9vhrSXQHQHPH1pT0kYZSlVP7DHdtaTqSml6MFZwLienqXlrUhqD3QyEjOTaFSbOks6Pbfil 8tw= From: Andrew Gabbasov To: , , Jaroslav Kysela , Takashi Iwai , Timo Wischer , Andrew Gabbasov Date: Fri, 22 Nov 2019 11:52:18 -0600 Message-ID: <20191122175218.17187-2-andrew_gabbasov@mentor.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191122175218.17187-1-andrew_gabbasov@mentor.com> References: <20191122175218.17187-1-andrew_gabbasov@mentor.com> MIME-Version: 1.0 X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: SVR-IES-MBX-09.mgc.mentorg.com (139.181.222.9) To svr-ies-mbx-02.mgc.mentorg.com (139.181.222.2) Subject: [alsa-devel] [PATCH 2/2] ALSA: aloop: Avoid unexpected timer event callback tasklets 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" loopback_snd_timer_close_cable() function waits until all scheduled tasklets are completed, but the timer is closed after that and can generate more event callbacks, scheduling new tasklets, that will not be synchronized with cable closing. Move tasklet_kill() call to be executed after snd_timer_close() call to avoid such case. Fixes: 26c53379f98d ("ALSA: aloop: Support selection of snd_timer instead of jiffies") Signed-off-by: Andrew Gabbasov --- sound/drivers/aloop.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sound/drivers/aloop.c b/sound/drivers/aloop.c index 6408932f5f72..0ebfbe70db00 100644 --- a/sound/drivers/aloop.c +++ b/sound/drivers/aloop.c @@ -302,15 +302,16 @@ static int loopback_snd_timer_close_cable(struct loopback_pcm *dpcm) if (!cable->snd_timer.instance) return 0; - /* wait till drain tasklet has finished if requested */ - tasklet_kill(&cable->snd_timer.event_tasklet); - /* will only be called from free_cable() when other stream was * already closed. Other stream cannot be reopened as long as * loopback->cable_lock is locked. Therefore no need to lock * cable->lock; */ snd_timer_close(cable->snd_timer.instance); + + /* wait till drain tasklet has finished if requested */ + tasklet_kill(&cable->snd_timer.event_tasklet); + snd_timer_instance_free(cable->snd_timer.instance); memset(&cable->snd_timer, 0, sizeof(cable->snd_timer));