From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751683AbdHaPgk (ORCPT ); Thu, 31 Aug 2017 11:36:40 -0400 Received: from mx2.suse.de ([195.135.220.15]:58712 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751386AbdHaPgj (ORCPT ); Thu, 31 Aug 2017 11:36:39 -0400 Date: Thu, 31 Aug 2017 17:36:37 +0200 Message-ID: From: Takashi Iwai To: "Anna-Maria Gleixner" Cc: "LKML" , , , "Peter Zijlstra" , "John Stultz" , "Thomas Gleixner" , "Christoph Hellwig" , "Jaroslav Kysela" , "Ingo Molnar" , "Takashi Sakamoto" Subject: Re: [PATCH 23/25] ALSA/dummy: Replace tasklet with softirq hrtimer In-Reply-To: <20170831105827.401092373@linutronix.de> References: <20170831105725.809317030@linutronix.de> <20170831105827.401092373@linutronix.de> 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.2 (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 List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 31 Aug 2017 14:23:45 +0200, Anna-Maria Gleixner wrote: > > From: Thomas Gleixner > > The tasklet is used to defer the execution of snd_pcm_period_elapsed() to > the softirq context. Using the CLOCK_MONOTONIC_SOFT base invokes the timer > callback in softirq context as well which renders the tasklet useless. > > Signed-off-by: Thomas Gleixner > Signed-off-by: Anna-Maria Gleixner > Cc: Jaroslav Kysela > Cc: Takashi Iwai > Cc: Takashi Sakamoto > Cc: alsa-devel@alsa-project.org I gave it at try, but it caused a kernel hang, unfortunately. The reason is that snd_pcm_period_elapased() may stop the stream (e.g. when reaching at the end). With this patchset, it'll lead to the call of hrtimer_cancel() from the hrtimer callback itself, thus it stalls. Below is the additional fix over your patch for working around it. I believe it should cover most corner cases, and seems working fine through quick tests, so far. thanks, Takashi --- diff --git a/sound/drivers/dummy.c b/sound/drivers/dummy.c index 273d60c42125..b5dd64e3dab1 100644 --- a/sound/drivers/dummy.c +++ b/sound/drivers/dummy.c @@ -375,6 +375,7 @@ struct dummy_hrtimer_pcm { ktime_t base_time; ktime_t period_time; atomic_t running; + atomic_t callback_running; struct hrtimer timer; struct snd_pcm_substream *substream; }; @@ -387,8 +388,15 @@ static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer) if (!atomic_read(&dpcm->running)) return HRTIMER_NORESTART; + atomic_inc(&dpcm->callback_running); snd_pcm_period_elapsed(dpcm->substream); + atomic_dec(&dpcm->callback_running); + /* may be flipped during snd_pcm_period_elapsed() */ + if (!atomic_read(&dpcm->running)) + return HRTIMER_NORESTART; + hrtimer_forward_now(timer, dpcm->period_time); + atomic_dec(&dpcm->callback_running); return HRTIMER_RESTART; } @@ -407,7 +415,9 @@ static int dummy_hrtimer_stop(struct snd_pcm_substream *substream) struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; atomic_set(&dpcm->running, 0); - hrtimer_cancel(&dpcm->timer); + /* issue hrtimer_cancel() only when called outside the callback */ + if (!atomic_read(&dpcm->callback_running)) + hrtimer_cancel(&dpcm->timer); return 0; } @@ -462,6 +472,7 @@ static int dummy_hrtimer_create(struct snd_pcm_substream *substream) dpcm->timer.function = dummy_hrtimer_callback; dpcm->substream = substream; atomic_set(&dpcm->running, 0); + atomic_set(&dpcm->callback_running, 0); return 0; } From mboxrd@z Thu Jan 1 00:00:00 1970 From: Takashi Iwai Subject: Re: [PATCH 23/25] ALSA/dummy: Replace tasklet with softirq hrtimer Date: Thu, 31 Aug 2017 17:36:37 +0200 Message-ID: References: <20170831105725.809317030@linutronix.de> <20170831105827.401092373@linutronix.de> Mime-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Return-path: In-Reply-To: <20170831105827.401092373@linutronix.de> Sender: linux-kernel-owner@vger.kernel.org To: Anna-Maria Gleixner Cc: LKML , alsa-devel@alsa-project.org, keescook@chromium.org, Peter Zijlstra , John Stultz , Thomas Gleixner , Christoph Hellwig , Jaroslav Kysela , Ingo Molnar , Takashi Sakamoto List-Id: alsa-devel@alsa-project.org On Thu, 31 Aug 2017 14:23:45 +0200, Anna-Maria Gleixner wrote: > > From: Thomas Gleixner > > The tasklet is used to defer the execution of snd_pcm_period_elapsed() to > the softirq context. Using the CLOCK_MONOTONIC_SOFT base invokes the timer > callback in softirq context as well which renders the tasklet useless. > > Signed-off-by: Thomas Gleixner > Signed-off-by: Anna-Maria Gleixner > Cc: Jaroslav Kysela > Cc: Takashi Iwai > Cc: Takashi Sakamoto > Cc: alsa-devel@alsa-project.org I gave it at try, but it caused a kernel hang, unfortunately. The reason is that snd_pcm_period_elapased() may stop the stream (e.g. when reaching at the end). With this patchset, it'll lead to the call of hrtimer_cancel() from the hrtimer callback itself, thus it stalls. Below is the additional fix over your patch for working around it. I believe it should cover most corner cases, and seems working fine through quick tests, so far. thanks, Takashi --- diff --git a/sound/drivers/dummy.c b/sound/drivers/dummy.c index 273d60c42125..b5dd64e3dab1 100644 --- a/sound/drivers/dummy.c +++ b/sound/drivers/dummy.c @@ -375,6 +375,7 @@ struct dummy_hrtimer_pcm { ktime_t base_time; ktime_t period_time; atomic_t running; + atomic_t callback_running; struct hrtimer timer; struct snd_pcm_substream *substream; }; @@ -387,8 +388,15 @@ static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer) if (!atomic_read(&dpcm->running)) return HRTIMER_NORESTART; + atomic_inc(&dpcm->callback_running); snd_pcm_period_elapsed(dpcm->substream); + atomic_dec(&dpcm->callback_running); + /* may be flipped during snd_pcm_period_elapsed() */ + if (!atomic_read(&dpcm->running)) + return HRTIMER_NORESTART; + hrtimer_forward_now(timer, dpcm->period_time); + atomic_dec(&dpcm->callback_running); return HRTIMER_RESTART; } @@ -407,7 +415,9 @@ static int dummy_hrtimer_stop(struct snd_pcm_substream *substream) struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; atomic_set(&dpcm->running, 0); - hrtimer_cancel(&dpcm->timer); + /* issue hrtimer_cancel() only when called outside the callback */ + if (!atomic_read(&dpcm->callback_running)) + hrtimer_cancel(&dpcm->timer); return 0; } @@ -462,6 +472,7 @@ static int dummy_hrtimer_create(struct snd_pcm_substream *substream) dpcm->timer.function = dummy_hrtimer_callback; dpcm->substream = substream; atomic_set(&dpcm->running, 0); + atomic_set(&dpcm->callback_running, 0); return 0; }