Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3122014pxk; Mon, 5 Oct 2020 01:22:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz/iab1Jj79zN28RXmeun42oH3203vUjxjRnVqPsn3k4ildS4rOzH225AM2TeDOgZyB5sxz X-Received: by 2002:a05:6402:b64:: with SMTP id cb4mr15794867edb.25.1601886137511; Mon, 05 Oct 2020 01:22:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601886137; cv=none; d=google.com; s=arc-20160816; b=qEkJXO5rDkbGfkgS10nhemBADSD29O0ydL5p34J5qsp1WtXTbrlmBJUY07DI3exiKP kfuZ0ZvrtXA0MaCEz08paVNiajRF3Wc9ecjA46ZANcVeDdMYhN8+6zoLArai0HBclhyr tTIQ3mBMBl8kIm0SGqci7ZJIYyMM3up2caenq3CV8P0n0t0kTPGaIon2lxbkIaF7M30X mhJJDrNYj8DHIKdXC3SD2/qDEwXJJBUOz/r1JSTUhFU4yPFbLzEX9Awvwoy0bSRTS8Pe cu6kQPVMUMf9xpN3byFGnK3OHXzYYD7wFZn4ndoJvUkP8xSXSP9gdV5vyrb6JqFEefWu Gxmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:to :from:date:dkim-signature; bh=jWqdW2UetOqVxr7Ag8ufT+K5pphhAHBlrgXjXZFWUJo=; b=oIhQZH5flSO8Qo2KsxIQ30bAcIRE+IFJ+lZKPGCko+jfsScqbMjKEhDQcugtZyA8Wq UJLpROQhEWkHGgmAvYM2sXclBCUgvKXzhKvqzUAKMLkZpUxg3GCGHsb7zNPkeHk2qcUk DNhTbRL27Gn4d2PRNWVpR3/hEZCKKaBgh8J2AUQqFzWV9gx9ptHcyeuQsTt+l5/2Jxze U6UJ1wcA6Nu0C0gweVL5Tb1lS8wiBRwfnG3s8agCQYuEPUenAZssDKu23GNN0vwWzwUT knRYtIafmJkSqvxdltccIa3gKz/5aOaKr1jnr3nqvRmRDmWqblCdulT/c45CYftn/kGd qA5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=b2mxDKap; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y24si2106398edu.325.2020.10.05.01.21.55; Mon, 05 Oct 2020 01:22:17 -0700 (PDT) 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; dkim=pass header.i=@kernel.org header.s=default header.b=b2mxDKap; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726131AbgJEIUW (ORCPT + 99 others); Mon, 5 Oct 2020 04:20:22 -0400 Received: from mail.kernel.org ([198.145.29.99]:50720 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725885AbgJEIUW (ORCPT ); Mon, 5 Oct 2020 04:20:22 -0400 Received: from pali.im (pali.im [31.31.79.79]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E83EA2075A; Mon, 5 Oct 2020 08:20:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601886021; bh=k+cn2MVadKtAEfTX4w1xKEndDAizAPhubuZsEg16NKA=; h=Date:From:To:Subject:References:In-Reply-To:From; b=b2mxDKapGiFurhQjsp4HEfPs36HSRoFnBpwIkksZFf7oGoqHFfZwDmAeyDz66PMy5 vtXa8G3wT/xClHEI77HGa5MbaoN9TdiCX63VoeDnTWmqHFuK2W+C7j2BIZdJKPLFo5 ki1pYznY6gijxc82EWP3dG1CZgPqk84tAeUBZHsE= Received: by pali.im (Postfix) id 62647A07; Mon, 5 Oct 2020 10:20:18 +0200 (CEST) Date: Mon, 5 Oct 2020 10:20:18 +0200 From: Pali =?utf-8?B?Um9ow6Fy?= To: Bjorn Helgaas , linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, Lorenzo Pieralisi , Gregory Clement , Andrew Lunn Subject: Re: PCI: Race condition in pci_create_sysfs_dev_files Message-ID: <20201005082018.rowqpfifyyrilxma@pali> References: <20200716110423.xtfyb3n6tn5ixedh@pali> <20200814080824.dg57kmbimyf3ushe@pali> <20200909112850.hbtgkvwqy2rlixst@pali> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20200909112850.hbtgkvwqy2rlixst@pali> User-Agent: NeoMutt/20180716 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org PING? On Wednesday 09 September 2020 13:28:50 Pali Rohár wrote: > Hello! I'm adding more people to loop. > > Can somebody look at these race conditions and my patch? > > On Friday 14 August 2020 10:08:24 Pali Rohár wrote: > > Hello! I would like to remind this issue which I reported month ago. > > > > On Thursday 16 July 2020 13:04:23 Pali Rohár wrote: > > > Hello Bjorn! > > > > > > I see following error message in dmesg which looks like a race condition: > > > > > > sysfs: cannot create duplicate filename '/devices/platform/soc/d0070000.pcie/pci0000:00/0000:00:00.0/config' > > > > > > I looked at it deeper and found out that in PCI subsystem code is race > > > condition between pci_bus_add_device() and pci_sysfs_init() calls. Both > > > of these functions calls pci_create_sysfs_dev_files() and calling this > > > function more times for same pci device throws above error message. > > > > > > There can be two different race conditions: > > > > > > 1. pci_bus_add_device() called pcibios_bus_add_device() or > > > pci_fixup_device() but have not called pci_create_sysfs_dev_files() yet. > > > Meanwhile pci_sysfs_init() is running and pci_create_sysfs_dev_files() > > > was called for newly registered device. In this case function > > > pci_create_sysfs_dev_files() is called two times, ones from > > > pci_bus_add_device() and once from pci_sysfs_init(). > > > > > > 2. pci_sysfs_init() is called. It first sets sysfs_initialized to 1 > > > which unblock calling pci_create_sysfs_dev_files(). Then another bus > > > registers new PCI device and calls pci_bus_add_device() which calls > > > pci_create_sysfs_dev_files() and registers sysfs files. Function > > > pci_sysfs_init() continues execution and calls function > > > pci_create_sysfs_dev_files() also for this newly registered device. So > > > pci_create_sysfs_dev_files() is again called two times. > > > > > > > > > I workaround both race conditions I created following hack patch. After > > > applying it I'm not getting that 'sysfs: cannot create duplicate filename' > > > error message anymore. > > > > > > Can you look at it how to fix both race conditions in proper way? > > > > Is this workaround diff enough? Or are you going to prepare something better? > > > > Please let me know if I should send this diff as regular patch. > > > > > > > > diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c > > > index 8e40b3e6da77..691be2258c4e 100644 > > > --- a/drivers/pci/bus.c > > > +++ b/drivers/pci/bus.c > > > @@ -316,7 +316,7 @@ void pci_bus_add_device(struct pci_dev *dev) > > > */ > > > pcibios_bus_add_device(dev); > > > pci_fixup_device(pci_fixup_final, dev); > > > - pci_create_sysfs_dev_files(dev); > > > + pci_create_sysfs_dev_files(dev, false); > > > pci_proc_attach_device(dev); > > > pci_bridge_d3_update(dev); > > > > > > diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c > > > index 6d78df981d41..b0c4852a51dd 100644 > > > --- a/drivers/pci/pci-sysfs.c > > > +++ b/drivers/pci/pci-sysfs.c > > > @@ -1328,13 +1328,13 @@ static int pci_create_capabilities_sysfs(struct pci_dev *dev) > > > return retval; > > > } > > > > > > -int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) > > > +int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev, bool sysfs_initializing) > > > { > > > int retval; > > > int rom_size; > > > struct bin_attribute *attr; > > > > > > - if (!sysfs_initialized) > > > + if (!sysfs_initializing && !sysfs_initialized) > > > return -EACCES; > > > > > > if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) > > > @@ -1437,18 +1437,21 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev) > > > static int __init pci_sysfs_init(void) > > > { > > > struct pci_dev *pdev = NULL; > > > - int retval; > > > + int retval = 0; > > > > > > - sysfs_initialized = 1; > > > for_each_pci_dev(pdev) { > > > - retval = pci_create_sysfs_dev_files(pdev); > > > + if (!pci_dev_is_added(pdev)) > > > + continue; > > > + retval = pci_create_sysfs_dev_files(pdev, true); > > > if (retval) { > > > pci_dev_put(pdev); > > > - return retval; > > > + goto out; > > > } > > > } > > > > > > - return 0; > > > +out: > > > + sysfs_initialized = 1; > > > + return retval; > > > } > > > late_initcall(pci_sysfs_init); > > > > > > diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h > > > index 6d3f75867106..304294c7171e 100644 > > > --- a/drivers/pci/pci.h > > > +++ b/drivers/pci/pci.h > > > @@ -19,7 +19,7 @@ bool pcie_cap_has_rtctl(const struct pci_dev *dev); > > > > > > /* Functions internal to the PCI core code */ > > > > > > -int pci_create_sysfs_dev_files(struct pci_dev *pdev); > > > +int pci_create_sysfs_dev_files(struct pci_dev *pdev, bool sysfs_initializing); > > > void pci_remove_sysfs_dev_files(struct pci_dev *pdev); > > > #if !defined(CONFIG_DMI) && !defined(CONFIG_ACPI) > > > static inline void pci_create_firmware_label_files(struct pci_dev *pdev) > > >