Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp1227658ybd; Wed, 26 Jun 2019 13:37:23 -0700 (PDT) X-Google-Smtp-Source: APXvYqzSwiJ+hb0yE0uzJXUBj0d0yR45Qs4wLcqQ0sYWipXuPeHBcJWqB5WUq9G8RQpuFLzXepxE X-Received: by 2002:a17:902:549:: with SMTP id 67mr50990plf.86.1561581443203; Wed, 26 Jun 2019 13:37:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561581443; cv=none; d=google.com; s=arc-20160816; b=I1KniPxM96vXxgoPz6jCKsrgARr3PVx3yBCT/mjGjmrtcieU4BDC1sEzXcMjxuVu3Y IkIY09dhjQ1wNAP7frgHYrVoHfWbPD1wf3JHZsuROb7sBvV3CQaaFvm6tUtX7m3IrKbz clb74yjsN9BD9K97G6tzyzboQxe9x50Hm5yR/JCnzTS10XHpi/1IEP0qGCOx7hJQ8I5K BhsnotZDJk9IsXHcus0FWILI1pPvtep5GQePl96X8WEs7kCVu9CvXNik4FEaZ+gti1UH SnrF307kozGvC2wgOYytsQvsURJARWGELKwbAQtYBUfBn+jb7aQKFFUoeS3h1M96V2nL SwQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=LTRf588K19VR3uE4xJO4Ojwc8tEPausRnAboL2Ui+lU=; b=qWLTXYSriDIrkFRht/KSbQ4S4gMqqonLETFSTYZDTRtRGE1w678fjNZSWnPx3beSkp Hg8VNmRvaixFJiGLqH9VwQYPvJsxWh2Eh4ZuMft/3lHN/2rSctrIUIcWqmjPK0BL0qCq o9MuJLDMBTYXL0n2nT2g2Bym75FrJva0DBE2CxyHWDmtRnZe7Ku0m3CYF8ZKNlTbEAIz 5+YvWgNUOPvoUJTxEoGNoQC6l8yhDuXdHfulmmikPHx5WH2DV8vKXvpdf9yornFbKmTH PG0Ivse2w5VT2MbM2XviaME9xe8XElf13p5oxIlJ686RU4chgDiqpuJxYuBe7rmiIMUS LE2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kyg5STNu; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e12si40461pgs.34.2019.06.26.13.37.06; Wed, 26 Jun 2019 13:37:23 -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; dkim=pass header.i=@chromium.org header.s=google header.b=kyg5STNu; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726467AbfFZUfL (ORCPT + 99 others); Wed, 26 Jun 2019 16:35:11 -0400 Received: from mail-lf1-f65.google.com ([209.85.167.65]:33545 "EHLO mail-lf1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726369AbfFZUfJ (ORCPT ); Wed, 26 Jun 2019 16:35:09 -0400 Received: by mail-lf1-f65.google.com with SMTP id y17so43397lfe.0 for ; Wed, 26 Jun 2019 13:35:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=LTRf588K19VR3uE4xJO4Ojwc8tEPausRnAboL2Ui+lU=; b=kyg5STNuNn7xfncn6wo2Yv/G5ZEt6xObU+gLxf5bai1e0LwHH3HnqqpmBIDfCmVxlS qz+z5g81bjwQWPsYzAdU9I4U77GW33GYOGebiIVAVbGpBrcTkwORPHT2q7vcucoNZwg8 ePNS9c2+vF0OyA0BDj2kyQSCfD65z6LrU8Zzg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=LTRf588K19VR3uE4xJO4Ojwc8tEPausRnAboL2Ui+lU=; b=NMBp8ZC9F+uN2Vtiuw6DG8g5vzjzmKah7984ZHYfqZApL7CwCAfGmDvi66NQNOrGb+ E+CVYpyYVscbwnA4bY8fULE5xdUPL27IEnlSWMi6yi8GBrjU1mZscmMxBNwOdup5UsvS 3wuwlXS7x7d8il15Ht69Is0Q4aNM/GYVnSyk/5Q9t44zwOFAEB9YLZrZOojaqX/BCNKg 41TFcKLyBvAlOC7kAOAHf4A3N8g+h3+ZUYhFIT7/ZZr5qWasXrztXWbjt9szmf3yn1Sx gRVOGGfr5P/LQd7iC6zXLNj3jALVW59C9fBsq/Z2b51JU8DgG8DMttTuNif2WNKBEpqI Sdjw== X-Gm-Message-State: APjAAAViIVsmQRdUQjjbNZJI9jg2rfwzip4RI/jgX5nQ5dGus2J2aIzq FRdjCwoJeXvf/xhcYDSRbDQyVFsoIRI= X-Received: by 2002:ac2:4904:: with SMTP id n4mr38847lfi.53.1561581306421; Wed, 26 Jun 2019 13:35:06 -0700 (PDT) Received: from mail-lj1-f172.google.com (mail-lj1-f172.google.com. [209.85.208.172]) by smtp.gmail.com with ESMTPSA id 80sm12449lfz.56.2019.06.26.13.35.05 for (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Wed, 26 Jun 2019 13:35:05 -0700 (PDT) Received: by mail-lj1-f172.google.com with SMTP id 131so3587852ljf.4 for ; Wed, 26 Jun 2019 13:35:05 -0700 (PDT) X-Received: by 2002:a2e:9c41:: with SMTP id t1mr142105ljj.6.1561581304868; Wed, 26 Jun 2019 13:35:04 -0700 (PDT) MIME-Version: 1.0 References: <20190625215418.17548-1-evgreen@chromium.org> In-Reply-To: From: Evan Green Date: Wed, 26 Jun 2019 13:34:28 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] ALSA: hda: Use correct start/count for sysfs init To: Takashi Iwai Cc: alsa-devel@alsa-project.org, Thomas Gleixner , "Amadeusz S*awi*ski" , Greg Kroah-Hartman , Jaroslav Kysela , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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. 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)