Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2693783ybl; Sun, 12 Jan 2020 00:21:46 -0800 (PST) X-Google-Smtp-Source: APXvYqweB5MR+B2WyaLfkbXZwdH3vWmvdy8ntkqI/WLyEFVuV5VObMh4yrWRWRa4t8Z7qwvejiUa X-Received: by 2002:a05:6808:10d:: with SMTP id b13mr8936628oie.69.1578817306645; Sun, 12 Jan 2020 00:21:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578817306; cv=none; d=google.com; s=arc-20160816; b=oC6eaHHpLX0AENGYGTMoDdwp8vLKxL7ANsqTlmxF/q7bJdJ2vaZtcxplnPIAhytGmG 9psybh3SyM//FAtecOUSywaj88JyFlW6mGajHs9hEGaG6Ryy4fZN+SYo77mejFMqZxHm dvgdv38DbjEcRYZpSXrR0DnQrJIkqM/m1RLX7AJaYAAK1b2Nb+F5dQb8eqIxPRoHFWbw XlrazgvV2fRtxO4HhIQ02tQZRkzzl8NY47rDNjNzm3J/OTwoPxWeaeXw/jZJPR8kux/P H9TcasEK0koMLyJqfH6UkqdlDT4Y9vTShJCiyHdGztPfugOFuvMziFGsphjn/5IWR+t3 yrBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:subject:cc:to:from:message-id:date; bh=/1rwzeAlnZtLBD4ZbkI4Oby14Le8rmZlt/w9yTyHmqA=; b=cYj3WifKc1rislAd6rHMVmDO7dt1TU+hdVH4JyyZRlKpVLoY069tK7bXpWI0IQhrJz A4KjGGOSkPidf0HNV3dZ8L8cyibdiUlTHY/poEfJAEduFiUD+0FgrH36VJbo4dXth9Tp 0PycaRRMsORQ8JZf5A23pvpT2tgRRV0UCpYotEAnMpIVDHHnFHrIv1uAIUAbukRtNYgw DXkGuNxHSq+rI4W5sMLlRWB3LuMgzvJQVfSAWlV4KchK7e/rAsNcGK7paWtcZ5bh4XsT NTqmrW5Rw8lZo8VRKmQ39s6n8VsQemCiGwHGrDqGZ+Xt1EB5hvmn8asLcak61H7dX+ge w07Q== 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 v25si2424468ote.90.2020.01.12.00.21.35; Sun, 12 Jan 2020 00:21:46 -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 S1732394AbgALIUU (ORCPT + 99 others); Sun, 12 Jan 2020 03:20:20 -0500 Received: from mx2.suse.de ([195.135.220.15]:35840 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732371AbgALIUU (ORCPT ); Sun, 12 Jan 2020 03:20:20 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 1B2C5B03D; Sun, 12 Jan 2020 08:20:18 +0000 (UTC) Date: Sun, 12 Jan 2020 09:20:17 +0100 Message-ID: From: Takashi Iwai To: Jia-Ju Bai Cc: perex@perex.cz, tiwai@suse.com, rfontana@redhat.com, tglx@linutronix.de, allison@lohutok.net, alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] ALSA: cmipci: Fix possible a data race in snd_cmipci_interrupt() In-Reply-To: <20200111163027.27135-1-baijiaju1990@gmail.com> References: <20200111163027.27135-1-baijiaju1990@gmail.com> 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.3 (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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 11 Jan 2020 17:30:27 +0100, Jia-Ju Bai wrote: > > The functions snd_cmipci_interrupt() and snd_cmipci_capture_trigger() > may be concurrently executed. > > The function snd_cmipci_capture_trigger() calls > snd_cmipci_pcm_trigger(). In snd_cmipci_pcm_trigger(), the variable > rec->running is written with holding a spinlock cm->reg_lock. But in > snd_cmipci_interrupt(), the identical variable cm->channel[0].running > or cm->channel[1].running is read without holding this spinlock. Thus, > a possible data race may occur. > > To fix this data race, in snd_cmipci_interrupt(), the variables > cm->channel[0].running and cm->channel[1].running are read with holding > the spinlock cm->reg_lock. > > This data race is found by the runtime testing of our tool DILP-2. > > Signed-off-by: Jia-Ju Bai Thanks for the patch. That's indeed a kind of race, but this change won't fix anything in practice, though. The inconsistent running flag between those places, there are two cases: - running became 0 to 1; this cannot happen, as the irq isn't issued before the stream gets started - running became 1 to 0; this means that the stream gets stopped between two points, and it's not better to call snd_pcm_period_elapsed() for an already stopped stream. Takashi > --- > sound/pci/cmipci.c | 8 +++++--- > 1 file changed, 5 insertions(+), 3 deletions(-) > > diff --git a/sound/pci/cmipci.c b/sound/pci/cmipci.c > index dd9d62e2b633..f791152ec48f 100644 > --- a/sound/pci/cmipci.c > +++ b/sound/pci/cmipci.c > @@ -1430,7 +1430,7 @@ static int snd_cmipci_capture_spdif_hw_free(struct snd_pcm_substream *subs) > static irqreturn_t snd_cmipci_interrupt(int irq, void *dev_id) > { > struct cmipci *cm = dev_id; > - unsigned int status, mask = 0; > + unsigned int run_flag0, run_flag1, status, mask = 0; > > /* fastpath out, to ease interrupt sharing */ > status = snd_cmipci_read(cm, CM_REG_INT_STATUS); > @@ -1445,15 +1445,17 @@ static irqreturn_t snd_cmipci_interrupt(int irq, void *dev_id) > mask |= CM_CH1_INT_EN; > snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, mask); > snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, mask); > + run_flag0 = cm->channel[0].running; > + run_flag1 = cm->channel[1].running; > spin_unlock(&cm->reg_lock); > > if (cm->rmidi && (status & CM_UARTINT)) > snd_mpu401_uart_interrupt(irq, cm->rmidi->private_data); > > if (cm->pcm) { > - if ((status & CM_CHINT0) && cm->channel[0].running) > + if ((status & CM_CHINT0) && run_flag0) > snd_pcm_period_elapsed(cm->channel[0].substream); > - if ((status & CM_CHINT1) && cm->channel[1].running) > + if ((status & CM_CHINT1) && run_flag1) > snd_pcm_period_elapsed(cm->channel[1].substream); > } > return IRQ_HANDLED; > -- > 2.17.1 >