Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5324050imu; Sun, 25 Nov 2018 21:38:53 -0800 (PST) X-Google-Smtp-Source: AFSGD/WMtWVZHAf/bjNLCkq4L4X6fHIrzY9yI1fuY7w1FZmx+dAjKqFvwVcdyUg3+4egbWhOrhV3 X-Received: by 2002:a62:8949:: with SMTP id v70mr11611132pfd.85.1543210733814; Sun, 25 Nov 2018 21:38:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543210733; cv=none; d=google.com; s=arc-20160816; b=vdnBLLQ/kKEF9I4QNagr78HNxHnjJMFl6SnqiFYQVTuIJ2eAl6z+nywmuQik+C2afN NNGjMMxe9WnyuEBXbenysjuy3PclXQkhkEHdAwIjKxBbHHCEckz+cqLXIA6BRM+zMYHi JXE7MMrvWjZY30zh5YHIqhHdJdbwzcgwdbZSjHdDym8gNW4X6HccwRn9IMH1LbHGyqPG iuVGcx2uEdpPsCX6s0S4o+VNXeDI0F3p4/1libFMXnFEKMs/LkDlGMr9DDFyjzAdGgSk tvQE0qjP7BfH/3piAAjHFuOhpnNwnJ0alXys5QB3MhNsGZDTdDAAg1H/fDrilY+RuIvy 4EJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=pB+fkA5hvyTfMYQLzP2Ro+Mj4dtqIrCRG/GjuC5dZ5g=; b=qnnmnja/v3suTUs3hQps6O5VthDEzIQqKOCIcpQNvyUL7CtFL7Fi6/wWUjRpwd5EJu ZQlaZz1UUclTTzX7OkXg5lK653ksR1K/WLZ+kkMx/pI44Zx/NynOdWKMmfm82RIezgEv AQuDydkvJoA0yhlvqGlWvu799l6cLsBDAATIcESH4HoienFHko0eajPT3z4XtnhQXxle TytWXoGwuEqT0olEO2gmdCj3d+D+sdz4pDXO1Wmg34ZI/EgBEMnnG7TyAZElIjF8CyaF qFkntVzE0Z32Rk881s3Io64LDLOrjcADKkmLpv0kfX48CiezAUcdpqd/L+c34wTeCeNY i66g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r25si22787737pfk.28.2018.11.25.21.38.39; Sun, 25 Nov 2018 21:38:53 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726225AbeKZQ3s (ORCPT + 99 others); Mon, 26 Nov 2018 11:29:48 -0500 Received: from lgeamrelo12.lge.com ([156.147.23.52]:56777 "EHLO lgeamrelo11.lge.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726144AbeKZQ3s (ORCPT ); Mon, 26 Nov 2018 11:29:48 -0500 Received: from unknown (HELO lgeamrelo02.lge.com) (156.147.1.126) by 156.147.23.52 with ESMTP; 26 Nov 2018 14:36:48 +0900 X-Original-SENDERIP: 156.147.1.126 X-Original-MAILFROM: chanho.min@lge.com Received: from unknown (HELO kernel.lge.com) (165.186.175.97) by 156.147.1.126 with ESMTP; 26 Nov 2018 14:36:48 +0900 X-Original-SENDERIP: 165.186.175.97 X-Original-MAILFROM: chanho.min@lge.com From: Chanho Min To: Jaroslav Kysela , Takashi Iwai , Vinod Koul , Daniel Mentz Cc: linux-kernel@vger.kernel.org, alsa-devel@alsa-project.org, Seungho Park , Jongsung Kim , Wonmin Jung , Jaehyun Kim , Hyonwoo Park , Chanho Min Subject: [PATCH] ALSA: pcm: Fix starvation on down_write_nonblock() Date: Mon, 26 Nov 2018 14:36:37 +0900 Message-Id: <1543210597-6717-1-git-send-email-chanho.min@lge.com> X-Mailer: git-send-email 2.1.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit 67ec1072b053 ("ALSA: pcm: Fix rwsem deadlock for non-atomic PCM stream") fixes deadlock for non-atomic PCM stream. But, This patch causes antother stuck. If writer is RT thread and reader is a normal thread, the reader thread will be difficult to get scheduled. It may not give chance to release readlocks and writer gets stuck for a long time if they are pinned to single cpu. The deadlock described in the previous commit is because the linux rwsem queues like a FIFO. So, we might need non-FIFO writelock, not non-block one. My suggestion is that the writer gives reader a chance to be scheduled by using the minimum msleep() instaed of spinning without blocking by writer. Also, The *_nonblock may be changed to *_nonfifo appropriately to this concept. In terms of performance, when trylock is failed, this minimum periodic msleep will have the same performance as the tick-based schedule()/wake_up_q(). Suggested-by: Wonmin Jung Signed-off-by: Chanho Min --- sound/core/pcm_native.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c index 66c90f4..bdca0e1 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -36,6 +36,7 @@ #include #include #include +#include #include "pcm_local.h" @@ -91,12 +92,12 @@ static DECLARE_RWSEM(snd_pcm_link_rwsem); * and this may lead to a deadlock when the code path takes read sem * twice (e.g. one in snd_pcm_action_nonatomic() and another in * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to - * spin until it gets the lock. + * sleep until all the readers are completed without blocking by writer. */ -static inline void down_write_nonblock(struct rw_semaphore *lock) +static inline void down_write_nonfifo(struct rw_semaphore *lock) { while (!down_write_trylock(lock)) - cond_resched(); + msleep(1); } #define PCM_LOCK_DEFAULT 0 @@ -1967,7 +1968,7 @@ static int snd_pcm_link(struct snd_pcm_substream *substream, int fd) res = -ENOMEM; goto _nolock; } - down_write_nonblock(&snd_pcm_link_rwsem); + down_write_nonfifo(&snd_pcm_link_rwsem); write_lock_irq(&snd_pcm_link_rwlock); if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN || substream->runtime->status->state != substream1->runtime->status->state || @@ -2014,7 +2015,7 @@ static int snd_pcm_unlink(struct snd_pcm_substream *substream) struct snd_pcm_substream *s; int res = 0; - down_write_nonblock(&snd_pcm_link_rwsem); + down_write_nonfifo(&snd_pcm_link_rwsem); write_lock_irq(&snd_pcm_link_rwlock); if (!snd_pcm_stream_linked(substream)) { res = -EALREADY; -- 2.1.4