From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80276C33CA8 for ; Mon, 13 Jan 2020 08:40:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5B2D820678 for ; Mon, 13 Jan 2020 08:40:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728978AbgAMIko (ORCPT ); Mon, 13 Jan 2020 03:40:44 -0500 Received: from mx2.suse.de ([195.135.220.15]:42054 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728946AbgAMIkl (ORCPT ); Mon, 13 Jan 2020 03:40:41 -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 0F65AAEBA; Mon, 13 Jan 2020 08:40:39 +0000 (UTC) Date: Mon, 13 Jan 2020 09:40:38 +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: 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=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 On Mon, 13 Jan 2020 09:20:37 +0100, Jia-Ju Bai wrote: > > > > On 2020/1/12 16:20, Takashi Iwai wrote: > > 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. > > Thanks for the reply :) > > I am not sure to understand your words. > > Do you mean that this code should be also protected by the spinlock? >     if (cm->pcm) { >         if ((status & CM_CHINT0) && cm->channel[0].running) >             snd_pcm_period_elapsed(cm->channel[0].substream); >         if ((status & CM_CHINT1) && cm->channel[1].running) >             snd_pcm_period_elapsed(cm->channel[1].substream); >     } No, it can't be protected as it would lead to ABBA deadlock. That said, it's rather safe to leave the code as is. thanks, Takashi