Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp803694imu; Tue, 11 Dec 2018 07:50:51 -0800 (PST) X-Google-Smtp-Source: AFSGD/XL0mYwxMUZ38B2RUQzyGjrLvtMa788wYBse/ykNzfUTskYg/JhkwdbGWLzhMR68viTY+yh X-Received: by 2002:a62:5444:: with SMTP id i65mr17398707pfb.193.1544543451789; Tue, 11 Dec 2018 07:50:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544543451; cv=none; d=google.com; s=arc-20160816; b=D2vVMululsPHrcyFk+AJuNMK0BQ8FUuHCfwgCfTzNPdG+JPx9Pywry5yKrk8SUzqs4 hpw8gyySCBJe+6hIVAgovq51B9QVJQNWKsg16sckYi0dBlRYtmJTZnvHfh3nTTQg3G/u FPcNCkzRyNb3Aw9hwD10b1jjOosnaf5owUhkb9lIVaB/sUOu3GAL2g0CYJChdwDtsOCe CrGfmIXZQKhy3RLJgrGzrmxaJlPczPNZfG8ZqSUdfKDpVGiGQypiu7WJzVM4FmOXMsIR DhXmdrTgkklNeDoixZYFUdLKzFv/Io5BcbBdIfYLXCwN7bdHl4yK5AkUu5gI2N9BS51q JD+w== 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=lzDxlcWIocM+gT2sbaikU9rLTXJcvF7S2XtxhH7sjm8=; b=KTrDqwxU7YqTha3hsHI9BFh3Oc6g7JAVOAAz5tYzky1qIp89Bys5R0LpKnux0bQcUL mCeyQI/CwvXDynXX0zGmThDg7EIA6Zp71LTF0BZuBTK/LBSMmbGHjo5u4ws1xz6c9aPC D6ZBjHWAGhyr9OxXsd+yuw/y1gggZiIfB6V9NeIdAGCtWClI4GHpAk+BG1otnZSsJ5po UJBrvcCAi9P0K6RpgJ+69g7L23OHimfkvzMfV4XeOyQSgrlmVhPol/wDft8kjGRTX58p 7DE94uzmaBS2zxCxVailReMilsUJBbpR2r0ZWkoiZRoftDE5H5/AoNg7n9nhK9vnH9Po v5WQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=YpUZlKo5; 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 f22si12212890pgm.81.2018.12.11.07.50.37; Tue, 11 Dec 2018 07:50:51 -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=YpUZlKo5; 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 S1727176AbeLKPrs (ORCPT + 99 others); Tue, 11 Dec 2018 10:47:48 -0500 Received: from mail.kernel.org ([198.145.29.99]:36204 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728152AbeLKPrp (ORCPT ); Tue, 11 Dec 2018 10:47:45 -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 A5F322145D; Tue, 11 Dec 2018 15:47:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544543264; bh=wCZKrx4H0QRBi2A+ox/vSfx+q10GvK0cBg14mkYv/yQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YpUZlKo5/OhUJclfdaL0z0Mcgc5EMC2FlWlX6GSGSeqKRkKNu/Cv87OuCn9jgbSl4 LViyxD/hKx/84UvXQrLbdJskEzsiIEFdi17sddwilzgB2SEqAMEb/cW7zlEFi2tZ7s X0CCZx5SfeYBSl7P3+6yGOVejOjuc0bJqdzDCE2c= 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.4 75/91] ALSA: pcm: Fix starvation on down_write_nonblock() Date: Tue, 11 Dec 2018 16:41:34 +0100 Message-Id: <20181211151612.555362106@linuxfoundation.org> X-Mailer: git-send-email 2.20.0 In-Reply-To: <20181211151606.026852373@linuxfoundation.org> References: <20181211151606.026852373@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.4-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 @@ -35,6 +35,7 @@ #include #include #include +#include /* * Compatibility @@ -78,12 +79,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); } /** @@ -1825,7 +1826,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 || @@ -1872,7 +1873,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;