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 X-Spam-Level: X-Spam-Status: No, score=-0.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0FD2C432C0 for ; Wed, 20 Nov 2019 14:33:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CF1A120898 for ; Wed, 20 Nov 2019 14:33:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731629AbfKTOdz (ORCPT ); Wed, 20 Nov 2019 09:33:55 -0500 Received: from mx2.suse.de ([195.135.220.15]:46144 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729591AbfKTOdz (ORCPT ); Wed, 20 Nov 2019 09:33:55 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 015E7ACD9; Wed, 20 Nov 2019 14:33:51 +0000 (UTC) Date: Wed, 20 Nov 2019 15:33:50 +0100 Message-ID: From: Takashi Iwai To: Andrew Gabbasov Cc: , , "Jaroslav Kysela" , Takashi Iwai , Timo Wischer Subject: Re: [PATCH v4 6/7] ALSA: aloop: Support selection of snd_timer instead of jiffies In-Reply-To: <20191120115856.4125-7-andrew_gabbasov@mentor.com> References: <20191120115856.4125-1-andrew_gabbasov@mentor.com> <20191120115856.4125-2-andrew_gabbasov@mentor.com> <20191120115856.4125-3-andrew_gabbasov@mentor.com> <20191120115856.4125-4-andrew_gabbasov@mentor.com> <20191120115856.4125-5-andrew_gabbasov@mentor.com> <20191120115856.4125-6-andrew_gabbasov@mentor.com> <20191120115856.4125-7-andrew_gabbasov@mentor.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 Emacs/25.3 (x86_64-suse-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 20 Nov 2019 12:58:55 +0100, Andrew Gabbasov wrote: > +/* call in loopback->cable_lock */ > +static int loopback_snd_timer_open(struct loopback_pcm *dpcm) > +{ > + int err = 0; > + struct snd_timer_id tid = { > + .dev_class = SNDRV_TIMER_CLASS_PCM, > + .dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION, > + }; > + 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; > + > + 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; > + } > + > + cable->snd_timer.stream = dpcm->substream->stream; > + cable->snd_timer.id = tid; > + > + timeri = snd_timer_instance_new(dpcm->loopback->card->id); > + if (!timeri) { > + err = -ENOMEM; > + goto unlock; > + } > + /* The callback has to be called from another tasklet. If > + * SNDRV_TIMER_IFLG_FAST is specified it will be called from the > + * snd_pcm_period_elapsed() call of the selected sound card. > + * snd_pcm_period_elapsed() helds snd_pcm_stream_lock_irqsave(). > + * Due to our callback loopback_snd_timer_function() also calls > + * snd_pcm_period_elapsed() which calls snd_pcm_stream_lock_irqsave(). > + * This would end up in a dead lock. > + */ > + timeri->flags |= SNDRV_TIMER_IFLG_AUTO; > + timeri->callback = loopback_snd_timer_function; > + timeri->callback_data = (void *)cable; > + timeri->ccallback = loopback_snd_timer_event; > + > + /* 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. > + * Therefore the following issue cannot happen: > + * [proc1] Call loopback_timer_open() -> > + * Unlock cable->lock for snd_timer_close/open() call > + * [proc2] Call loopback_timer_open() -> snd_timer_open(), > + * snd_timer_start() > + * [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); > + if (err < 0) { > + pcm_err(dpcm->substream->pcm, > + "snd_timer_open (%d,%d,%d) failed with %d", > + cable->snd_timer.id.card, > + cable->snd_timer.id.device, > + cable->snd_timer.id.subdevice, > + err); > + snd_timer_instance_free(timeri); > + return err; > + } > + spin_lock_irq(&cable->lock); > + > + cable->snd_timer.instance = timeri; > + > + /* initialise a tasklet used for draining */ > + tasklet_init(&cable->snd_timer.event_tasklet, > + loopback_snd_timer_tasklet, (unsigned long)timeri); This has to be set before snd_timer_open(). The callback might be called immediately after snd_timer_open(). thanks, Takashi