Received: by 10.223.185.116 with SMTP id b49csp6397418wrg; Wed, 28 Feb 2018 08:44:36 -0800 (PST) X-Google-Smtp-Source: AH8x227MMsPe+oGOUQkmfzNBNVAlCcw4Hgm4oQwFbnfx0YJeASXbgyHNVgk0/+F5PT9wRqYAxUbQ X-Received: by 2002:a17:902:684a:: with SMTP id f10-v6mr19045356pln.129.1519836276480; Wed, 28 Feb 2018 08:44:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519836276; cv=none; d=google.com; s=arc-20160816; b=FVTkGgVfKak0ur9+jjWixSVDXYaddCP3QuHX/z2PlPBTaXpmPgWpPeZe0/hJJjv0mV 2R3d/Wzq++0n49hbRfoWKAtsC2TZRPFCu74P1uPNGWm58dZbF7HvUVbe8M/QQuCRKV/Z HFqMppZrTyghHg111gnknMY2dpdRgKO3+6NQPW1AcrmXpELWoWQ7sGu5Y7hkTZa+tpEU WZeTeewTTASvLpD230NGY7ljQqW7VdcdYzBiVgovXWwLdyeCqjz1zvgLP72gnDEY9eRp uMri0kT3omk4GRCK+gx4U8hblPvUJCw3E5iiE82EC3TqljrldMp1Vcza9HIRlUkVOQKX R1Fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=3HIowupGOGTgMdjaOsreQU90i/f3wXVQCxxXRwVtFcw=; b=ZeiX1G+ptt+C98O54hWaxeTeW+q3HHnjAh/iMUqU5uGhJcTtg1C7jMDllOSdZWIh2s hBh1eERLO8jXlgEe4x2SyVbQGMCggH7PfxgxpXn0yZ9qLHHvktpNZlUICVMWV2AJp2iI 1WBC3udt3gFnkTfL+h1c2GyPNAZRHUUrlhwKfubnu84/dUoj2gPgre3Rs43X7nMYz56I mM28jGXtuN8HdZC6hAPqAhxGT47SEh0WSQ4ek6h2lwcjMn9pTbiw2weBeOkGFs/s2W8V oQ0js7Mq2EMrPTyJs/mouQyZvBDxEd//zRxa2wMmJROHsQA3zpI8QMoHbFBM6yHqEWux TRGA== 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 33-v6si1484191pls.710.2018.02.28.08.44.21; Wed, 28 Feb 2018 08:44:36 -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 S1753281AbeB1QmZ (ORCPT + 99 others); Wed, 28 Feb 2018 11:42:25 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:34762 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932854AbeB1QDW (ORCPT ); Wed, 28 Feb 2018 11:03:22 -0500 Received: from [2a02:8011:400e:2:6f00:88c8:c921:d332] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1er3Yk-0006Xb-8p; Wed, 28 Feb 2018 15:22:23 +0000 Received: from ben by deadeye with local (Exim 4.90_1) (envelope-from ) id 1er3Yi-0000DT-VF; Wed, 28 Feb 2018 15:22:20 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Takashi Iwai" Date: Wed, 28 Feb 2018 15:20:18 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 182/254] ALSA: pcm: Allow aborting mutex lock at OSS read/write loops In-Reply-To: X-SA-Exim-Connect-IP: 2a02:8011:400e:2:6f00:88c8:c921:d332 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.55-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Takashi Iwai commit 900498a34a3ac9c611e9b425094c8106bdd7dc1c upstream. PCM OSS read/write loops keep taking the mutex lock for the whole read/write, and this might take very long when the exceptionally high amount of data is given. Also, since it invokes with mutex_lock(), the concurrent read/write becomes unbreakable. This patch tries to address these issues by replacing mutex_lock() with mutex_lock_interruptible(), and also splits / re-takes the lock at each read/write period chunk, so that it can switch the context more finely if requested. Signed-off-by: Takashi Iwai Signed-off-by: Ben Hutchings --- sound/core/oss/pcm_oss.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) --- a/sound/core/oss/pcm_oss.c +++ b/sound/core/oss/pcm_oss.c @@ -1370,8 +1370,11 @@ static ssize_t snd_pcm_oss_write1(struct if ((tmp = snd_pcm_oss_make_ready(substream)) < 0) return tmp; - mutex_lock(&runtime->oss.params_lock); while (bytes > 0) { + if (mutex_lock_interruptible(&runtime->oss.params_lock)) { + tmp = -ERESTARTSYS; + break; + } if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) { tmp = bytes; if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes) @@ -1415,18 +1418,18 @@ static ssize_t snd_pcm_oss_write1(struct xfer += tmp; if ((substream->f_flags & O_NONBLOCK) != 0 && tmp != runtime->oss.period_bytes) - break; + tmp = -EAGAIN; } + err: + mutex_unlock(&runtime->oss.params_lock); + if (tmp < 0) + break; if (signal_pending(current)) { tmp = -ERESTARTSYS; - goto err; + break; } + tmp = 0; } - mutex_unlock(&runtime->oss.params_lock); - return xfer; - - err: - mutex_unlock(&runtime->oss.params_lock); return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp; } @@ -1474,8 +1477,11 @@ static ssize_t snd_pcm_oss_read1(struct if ((tmp = snd_pcm_oss_make_ready(substream)) < 0) return tmp; - mutex_lock(&runtime->oss.params_lock); while (bytes > 0) { + if (mutex_lock_interruptible(&runtime->oss.params_lock)) { + tmp = -ERESTARTSYS; + break; + } if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) { if (runtime->oss.buffer_used == 0) { tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1); @@ -1506,16 +1512,16 @@ static ssize_t snd_pcm_oss_read1(struct bytes -= tmp; xfer += tmp; } + err: + mutex_unlock(&runtime->oss.params_lock); + if (tmp < 0) + break; if (signal_pending(current)) { tmp = -ERESTARTSYS; - goto err; + break; } + tmp = 0; } - mutex_unlock(&runtime->oss.params_lock); - return xfer; - - err: - mutex_unlock(&runtime->oss.params_lock); return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp; }