Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp820904imu; Tue, 11 Dec 2018 08:05:43 -0800 (PST) X-Google-Smtp-Source: AFSGD/XerAcbnq8uZhNfQ7RzSOqzjVMkic4SKDbmptK5pS93VOlYLCw2cBjDj3ORdSptcspooANd X-Received: by 2002:a63:451a:: with SMTP id s26mr15203805pga.150.1544544343322; Tue, 11 Dec 2018 08:05:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544544343; cv=none; d=google.com; s=arc-20160816; b=X6+IwiidCZ+mVqkW7RUoc/ZDu1RAkfQBSSx1w1CgEPRecoOHzEwlR0MK7eaqICG9ig v7V45fYb9QXQh9jI/rFBKsgvX9y+ZL2ZFwZT3SEFlUJ6pGAFyG45OYz07Um2VXaMnzJX OI4a1y0VGU8e2lCM4WYqSZ4/PchgHhXHRhJiMYp920BFrQrPGOao7c4px+QpCza/kFst K4cx+4sSz+J+esH1H4OAhRwuK+kKPGDpvw91Ky8ddTlbNRKFwGqQ6Vj7dmSMNdFxpCaf l+F15b9KYLsABhZ1PfSgMLrSDfRuGtkS2yXeuni/cLNO9GC/T7zuF8JpzUTX0w1oZfMc WGPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=WrowqaU4WDJhmWS0AHddob9XTbHsg9PsOfTErIHMYYc=; b=wdMZLS8Phs/8/28+FzwwUYWYXLg2SuQ8s5PtTzNrxVaHcrvr2Vdfc91+U6Npg4XJu5 llFFmKGjdM1VOCvm/3xOaoPXAKX50l0UudZ77DOXWGAZ99uWopSTGJB925DK/WYEPlOZ mI0JoDU+vwpq0MCf/1q8rphCFc4fakvEl5tYJcDzUaU+uQa1kLMBc+N5Z0PFBZdWB/qi +kNtWqAHiP+14yUx5lZRNZ9uZegVgd4lIhAdtx2V0OMl1eoLjvWpcvcEWj6HY6XKHt7q NRsHvb6LJC5iu8L1gK8ngkieUztZ5E1JWjxR1p36NIPXjXEOhcqR6VwDmHSqKDqxqo90 /fFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ZPPTAlqf; 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 k6si12886516pla.350.2018.12.11.08.05.25; Tue, 11 Dec 2018 08:05:43 -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; dkim=pass header.i=@kernel.org header.s=default header.b=ZPPTAlqf; 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 S1730580AbeLKP43 (ORCPT + 99 others); Tue, 11 Dec 2018 10:56:29 -0500 Received: from mail.kernel.org ([198.145.29.99]:45224 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730561AbeLKP40 (ORCPT ); Tue, 11 Dec 2018 10:56:26 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A1E992086D; Tue, 11 Dec 2018 15:56:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544543786; bh=KPNbV4y3ql7m9SLJSEpHzwzMTmc4euLZAqvQayaX5SY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZPPTAlqfEpA/O0Em+fNTG5QcGWiVbTNTpHH6he4pgveQS1q2F0zaXcI/T1jVc+nrr SHWLdZLLISZ0ujoji0jR6K5Ctyw5PZYRuu8VxEf7BZXqcBa2RDKbobxvC1cAfbgs02 vJTf+DiDc1mPvXA6pDeUekzFpu+hwpi6vxIIqXAo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Wonmin Jung , Chanho Min , Takashi Iwai Subject: [PATCH 4.19 063/118] ALSA: pcm: Fix starvation on down_write_nonblock() Date: Tue, 11 Dec 2018 16:41:22 +0100 Message-Id: <20181211151646.780620595@linuxfoundation.org> X-Mailer: git-send-email 2.20.0 In-Reply-To: <20181211151644.216668863@linuxfoundation.org> References: <20181211151644.216668863@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Chanho Min commit b888a5f713e4d17faaaff24316585a4eb07f35b7 upstream. 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(). [ Although this has a fairly high performance penalty, the relevant code path became already rare due to the previous commit ("ALSA: pcm: Call snd_pcm_unlink() conditionally at closing"). That is, now this unconditional msleep appears only when using linked streams, and this must be a rare case. So we accept this as a quick workaround until finding a more suitable one -- tiwai ] Fixes: 67ec1072b053 ("ALSA: pcm: Fix rwsem deadlock for non-atomic PCM stream") Suggested-by: Wonmin Jung Signed-off-by: Chanho Min Cc: Signed-off-by: Takashi Iwai Signed-off-by: Greg Kroah-Hartman --- sound/core/pcm_native.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) --- 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_s 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 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;