Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp1259891ybd; Wed, 26 Jun 2019 14:16:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqziPhOWubI0OKDcoVNU9wLYj5iVefuHeyq+fHeIa4QiqEDtX/3auwXLQM20U0uqslGX6tJj X-Received: by 2002:a17:902:b102:: with SMTP id q2mr234470plr.72.1561583798292; Wed, 26 Jun 2019 14:16:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561583798; cv=none; d=google.com; s=arc-20160816; b=rPRxNSv+I+xm52KJKR7hPIyTXNU4hkZY3QxVDKc7CkwK7Ld6KLBnaAHIWFxkndttzm Aps9lUrr3i8DENsekfaqysn+lEuIHlpgpn74TiRU2ccaZk2G41KrgXRBXVhj7ZbxgK5l toRACAg8l5qXBeTmj/Zo53dYvJYdEW++TFubF2XJ24bGfIU81wmTokNtSdn13CjADUmJ OLsg4hQNJe5HEt7V2X2h68fkcHRbg/4tC1YVMXKw6fQdT+1LNx0qUhDvOVWrdMFmigqu Ab0lNZ4n+95Y7pfQMyo7gLg/ZG/6dKrZ811cgZEZMXvgDPTpj2xkT3JIjtKKkTJMN5xa jr0w== 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=d0bMZNhqSAEz61VoopRdynTysloNQSvCRZToOmuqLO4=; b=ScR1+frcrDdw5IBiJm+ufDLqiRr1sb9c2COo/mIRq8mzxZ7dsRDa3ALYLRuKSlcaqY 987+b3jWDSlexbwudKmQQIJEwt0zj6uVlEQwSxPyAPDNFbKIEUfuw4E670DaE/xN+FiF WFbbzCo78/aazUIOguGmlGm/tDaIT3iZmZQ5YXTUwecJarZ11VXBpHHkbVEoN7dcX5Xr kadMo9cJ8ZoTCqMmqtFaVFBeasx51gG1kfa919aGfm24O23iAJVimwheHX0VWqB8Qp02 ngrfZghQBUOyMg/IGOgWwvu10xj+h2tkWcjzdaljnPEQ3RxGRrK15exE01sn+/Tbg1ws 3Wqw== 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 f11si119090pgl.402.2019.06.26.14.16.21; Wed, 26 Jun 2019 14:16:38 -0700 (PDT) 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 S1726359AbfFZVQH (ORCPT + 99 others); Wed, 26 Jun 2019 17:16:07 -0400 Received: from mx2.suse.de ([195.135.220.15]:37032 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726223AbfFZVQG (ORCPT ); Wed, 26 Jun 2019 17:16:06 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 79AC9AF1B; Wed, 26 Jun 2019 21:16:05 +0000 (UTC) Date: Wed, 26 Jun 2019 23:16:05 +0200 Message-ID: From: Takashi Iwai To: Evan Green Cc: alsa-devel@alsa-project.org, Thomas Gleixner , "Amadeusz S*awi*ski" , Greg Kroah-Hartman , Jaroslav Kysela , LKML Subject: Re: [PATCH] ALSA: hda: Use correct start/count for sysfs init In-Reply-To: References: <20190625215418.17548-1-evgreen@chromium.org> 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 Wed, 26 Jun 2019 22:34:28 +0200, Evan Green wrote: > > On Wed, Jun 26, 2019 at 1:27 AM Takashi Iwai wrote: > > > > On Tue, 25 Jun 2019 23:54:18 +0200, > > Evan Green wrote: > > > > > > The normal flow through the widget sysfs codepath is that > > > snd_hdac_refresh_widgets() is called once without the sysfs bool set > > > to set up codec->num_nodes and friends, then another time with the > > > bool set to actually allocate all the sysfs widgets. However, during > > > the first time allocation, hda_widget_sysfs_reinit() ignores the new > > > num_nodes passed in via parameter and just calls hda_widget_sysfs_init(), > > > using whatever was in codec->num_nodes before the update. This is not > > > correct in cases where num_nodes changes. Here's an example: > > > > > > Sometime earlier: > > > snd_hdac_refresh_widgets(hdac, false) > > > sets codec->num_nodes to 2, widgets is still not allocated > > > > > > Now: > > > snd_hdac_refresh_widgets(hdac, true) > > > hda_widget_sysfs_reinit(num_nodes=7) > > > hda_widget_sysfs_init() > > > widget_tree_create() > > > alloc(codec->num_nodes) // this is still 2 > > > codec->num_nodes = 7 > > > > > > Pass num_nodes and start_nid down into widget_tree_create() so that > > > the right number of nodes are allocated in all cases. > > > > > > Signed-off-by: Evan Green > > > > Thanks for the patch. That's indeed a problem, but I guess a simpler > > approach is just to return if sysfs didn't exist. If the sysfs > > entries aren't present at the second call with sysfs=true, it implies > > that the codec object will be exposed anyway later, and the sysfs will > > be created there. So, something like below would work instead? > > Hi Takashi, > Thanks for taking a look. I'm not sure you'd want to do that because > then you end up returning from sysfs_reinit without having allocated > any of the sysfs widgets. You'd be relying on the implicit behavior > that another call to init is coming later (despite having updated > num_nodes and start node), which is difficult to follow and easy to > break. In my opinion the slight bit of extra diffs is well worth the > clarity of having widget_tree_create always allocate the correct > start/count. Well, skipping is the right behavior, actually. The whole need of the refresh function is just to refresh the widget list, and the current behavior to create a sysfs is rather superfluous. This action has never been used, so better to get removed for avoiding misuse. > Actually, in looking at the widget lock patch, I don't think it's > sufficient either. It adds a lock around sysfs_reinit, but the setting > of codec->num_nodes and codec->start_nid is unprotected by the lock. > So you could have the two threads politely serialize through > sysfs_reinit, but then get reordered before setting codec->num_nodes, > landing you with an array whose length doesn't match num_nodes. The usage of snd_hdac_refresh_widgets() is supposed to be done only at the codec probe phase, hence there is no lock done in the core code; IOW, any concurrent access has to be protected in the caller side in general. Have you actually seen such concurrent accesses? If yes, that's a problem in the usage. thanks, Takashi > > Let me craft up an additional patch to fix the locking. > -Evan > > > > > > > thanks, > > > > Takashi > > > > --- a/sound/hda/hdac_sysfs.c > > +++ b/sound/hda/hdac_sysfs.c > > @@ -428,7 +428,7 @@ int hda_widget_sysfs_reinit(struct hdac_device *codec, > > int i; > > > > if (!codec->widgets) > > - return hda_widget_sysfs_init(codec); > > + return 0; > > > > tree = kmemdup(codec->widgets, sizeof(*tree), GFP_KERNEL); > > if (!tree) >