Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1250449pxb; Fri, 26 Feb 2021 06:26:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJwR0fNf38mXW/PGzo8UnMaD/zaFWlrQJPP8KXfKvZgcrdLRwyyGdV/R222QKWzzpW3qxyme X-Received: by 2002:a17:907:ea3:: with SMTP id ho35mr3589030ejc.396.1614349610173; Fri, 26 Feb 2021 06:26:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614349610; cv=none; d=google.com; s=arc-20160816; b=nfWav8jHJD/3BebALYY17GTsqLrXZUIAc0d9GTzc0spYJdAPqHaNWIgdjr5LkJCtUT S7oMJwBbVJTlM+/B/GZ0x+aegYCmZrX2hgSTexa/2q65lngRs3XyuY+TdrjxYRK91Rua i2MgnqNX8WP0zPQ2gpRuP8/HwruC5QemTLDf8vgHsihPSJtL4t0fjYkPVUQCPSHxdABt myqt/9eMw5YN2eBbbMmOUQMoClxaQL4n4FRyizlK/OWH8oM5RutNkO6iNaOWVaQXPZhe 1Sy02XJ6c6QkW7MDv/VdBcEyiAKNHAVQM7CVidlFNN3/4vOQR4mwhQj4WmXTAvf/+8qT kmMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date; bh=bgev4exKou5rEavzCL0qMTYvQcy8E0rJpsDiv3kloIc=; b=blyimYM3Q9UgXu1Jryl62sJl3SzV+qqtKHgUu7lLKqwRVIEXSeoEru/YBfomhOR22L v/CBocg3ZebGQfj0+v6bqW0cHda6hL7ZuKzzlcA63wN0adDXO6u/NPC7vVQNOfo/W1QX vubgrxnBFpZO2hgF9bM9AEnvwN3yFMHC0DsNGfKL5SoKX45vX93ma6hpZrs4K9vdO7F5 sYXyTS9OtZVdb4ls7XsyNEP/akUX+eNQNRVHkJ4JhFs+S/1LpNn4zUBWQbZqV6ZdgMd+ RDP7N1QcGXk7v5N9gazL+IMJD12NwJ4AnJenDAQ6lDrdsL5JjfgSJuEtNQnB27FdP4G2 XTZg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u23si5509438edx.26.2021.02.26.06.26.27; Fri, 26 Feb 2021 06:26:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230140AbhBZOYo (ORCPT + 99 others); Fri, 26 Feb 2021 09:24:44 -0500 Received: from mx2.suse.de ([195.135.220.15]:56052 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230105AbhBZOYU (ORCPT ); Fri, 26 Feb 2021 09:24:20 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id DEAC9ADFB; Fri, 26 Feb 2021 14:23:37 +0000 (UTC) Date: Fri, 26 Feb 2021 15:23:37 +0100 Message-ID: From: Takashi Iwai To: Anton Yakovlev Cc: "Michael S. Tsirkin" , , , , Jaroslav Kysela , Takashi Iwai , Subject: Re: [PATCH v5 6/9] ALSA: virtio: PCM substream operators In-Reply-To: <0a9f6dea-ed75-16eb-9fc2-84148fa820be@opensynergy.com> References: <20210222153444.348390-1-anton.yakovlev@opensynergy.com> <20210222153444.348390-7-anton.yakovlev@opensynergy.com> <20210225135951-mutt-send-email-mst@kernel.org> <0a9f6dea-ed75-16eb-9fc2-84148fa820be@opensynergy.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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 25 Feb 2021 23:19:31 +0100, Anton Yakovlev wrote: > > On 25.02.2021 21:30, Takashi Iwai wrote:> On Thu, 25 Feb 2021 20:02:50 > +0100, > > Michael S. Tsirkin wrote: > >> > >> On Thu, Feb 25, 2021 at 01:51:16PM +0100, Takashi Iwai wrote: > >>> On Thu, 25 Feb 2021 13:14:37 +0100, > >>> Anton Yakovlev wrote: > > > [snip] > > > >> Takashi given I was in my tree for a while and I planned to merge > >> it this merge window. > > > > Hmm, that's too quick, I'm afraid. I see still a few rough edges in > > the code. e.g. the reset work should be canceled at the driver > > removal, but it's missing right now. And that'll become tricky > > because the reset work itself unbinds the device, hence it'll get > > stuck if calling cancel_work_sync() at remove callback. > > Yes, you made a good point here! In this case, we need some external > mutex for synchronization. This is just a rough idea, but maybe > something like this might work: > > struct reset_work { > struct mutex mutex; > struct work_struct work; > struct virtio_snd *snd; > bool resetting; > }; > > static struct reset_work reset_works[SNDRV_CARDS]; > > init() > // init mutexes and workers > > > virtsnd_probe() > snd_card_new(snd->card) > reset_works[snd->card->number].snd = snd; > > > virtsnd_remove() > mutex_lock(reset_works[snd->card->number].mutex) > reset_works[snd->card->number].snd = NULL; > resetting = reset_works[snd->card->number].resetting; > mutex_unlock(reset_works[snd->card->number].mutex) > > if (!resetting) > // cancel worker reset_works[snd->card->number].work > // remove device > > > virtsnd_reset_fn(work) > mutex_lock(work->mutex) > if (!work->snd) > // do nothing and take an exit path > work->resetting = true; > mutex_unlock(work->mutex) > > device_reprobe() > > work->resetting = false; > > > interrupt_handler() > schedule_work(reset_works[snd->card->number].work); > > > What do you think? I think it's still somehow racy. Suppose that the reset_work is already running right before entering virtsnd_remove(): it sets reset_works[].resetting flag, virtsnd_remove() skips canceling, and both reset work and virtsnd_remove() perform at the very same time. (I don't know whether this may happen, but I assume it's possible.) In that case, maybe a better check is to check current_work(), and perform cancel_work_sync() unless it's &reset_works[].work itself. Then the recursive cancel call can be avoided. After that point, the reset must be completed, and we can (again) process the rest release procedure. (But also snd object itself might have been changed again, so it needs to be re-evaluated.) One remaining concern is that the card number of the sound instance may change after reprobe. That is, we may want to another persistent object instead of accessing via an array index of sound card number. So, we might need reset_works[] associated with virtio_snd object instead. In anyway, this is damn complex. I sincerely hope that we can avoid this kind of things. Wouldn't it be better to shift the reset stuff up to the virtio core layer? Or drop the feature in the first version. Shooting itself (and revival) is a dangerous magic spell, after all. thanks, Takashi