[PATCH] PCI: Fix pci devices double register WARN in the kernel starting process

Shuan He posted 1 patch 7 months, 1 week ago
drivers/pci/proc.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
[PATCH] PCI: Fix pci devices double register WARN in the kernel starting process
Posted by Shuan He 7 months, 1 week ago
To avoid the double register of pcie device (otherwise a WARNING will
be triggered), two more steps are added in the pci_proc_init function.
1. The pci_rescan_remove_lock is held to prevent the concurrent issue
with pci_bus_add_devices.
2. The PCI_DEV_ADDED bit is added to dev->priv_flags after the device is
successfully registered to procfs.

WARN Trace:
[    0.518993] proc_dir_entry '000c:00/00.0' already registered
[    0.519187] WARNING: CPU: 2 PID: 179 at fs/proc/generic.c:375 proc_register+0xf6/0x180
[    0.519214] [<ffffffff804055a6>] proc_register+0xf6/0x180
[    0.519217] [<ffffffff80405a9e>] proc_create_data+0x3e/0x60
[    0.519220] [<ffffffff80616e44>] pci_proc_attach_device+0x74/0x130
[    0.509991] [<ffffffff805f1af2>] pci_bus_add_device+0x42/0x100
[    0.509997] [<ffffffff805f1c76>] pci_bus_add_devices+0xc6/0x110
[    0.519230] [<ffffffff8066763c>] acpi_pci_root_add+0x54c/0x810
[    0.519233] [<ffffffff8065d206>] acpi_bus_attach+0x196/0x2f0
[    0.519234] [<ffffffff8065d390>] acpi_scan_clear_dep_fn+0x30/0x70
[    0.519236] [<ffffffff800468fa>] process_one_work+0x19a/0x390
[    0.519239] [<ffffffff80047a6e>] worker_thread+0x2be/0x420
[    0.519241] [<ffffffff80050dc4>] kthread+0xc4/0xf0
[    0.519243] [<ffffffff80ad6ad2>] ret_from_fork+0xe/0x1c

Signed-off-by: Shuan He <heshuan@bytedance.com>
---
 drivers/pci/proc.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/pci/proc.c b/drivers/pci/proc.c
index 9348a0fb8084..6506316c8a31 100644
--- a/drivers/pci/proc.c
+++ b/drivers/pci/proc.c
@@ -468,9 +468,14 @@ static int __init pci_proc_init(void)
 	proc_create_seq("devices", 0, proc_bus_pci_dir,
 		    &proc_bus_pci_devices_op);
 	proc_initialized = 1;
-	for_each_pci_dev(dev)
-		pci_proc_attach_device(dev);
-
+	pci_lock_rescan_remove();
+	for_each_pci_dev(dev) {
+		if (pci_dev_is_added(dev))
+			continue;
+		if (!pci_proc_attach_device(dev))
+			pci_dev_assign_added(dev, true);
+	}
+	pci_unlock_rescan_remove();
 	return 0;
 }
 device_initcall(pci_proc_init);
-- 
2.39.5 (Apple Git-154)
Re: [PATCH] PCI: Fix pci devices double register WARN in the kernel starting process
Posted by kernel test robot 7 months, 1 week ago
Hi Shuan,

kernel test robot noticed the following build errors:

[auto build test ERROR on pci/next]
[also build test ERROR on pci/for-linus linus/master v6.16-rc4 next-20250703]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Shuan-He/PCI-Fix-pci-devices-double-register-WARN-in-the-kernel-starting-process/20250703-035442
base:   https://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git next
patch link:    https://lore.kernel.org/r/20250702155112.40124-2-heshuan%40bytedance.com
patch subject: [PATCH] PCI: Fix pci devices double register WARN in the kernel starting process
config: alpha-allnoconfig (https://download.01.org/0day-ci/archive/20250703/202507032253.sXSgz4lH-lkp@intel.com/config)
compiler: alpha-linux-gcc (GCC) 15.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250703/202507032253.sXSgz4lH-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202507032253.sXSgz4lH-lkp@intel.com/

All errors (new ones prefixed by >>):

   drivers/pci/proc.c: In function 'pci_proc_init':
>> drivers/pci/proc.c:476:25: error: too many arguments to function 'pci_dev_assign_added'; expected 1, have 2
     476 |                         pci_dev_assign_added(dev, true);
         |                         ^~~~~~~~~~~~~~~~~~~~      ~~~~
   In file included from drivers/pci/proc.c:18:
   drivers/pci/pci.h:577:20: note: declared here
     577 | static inline void pci_dev_assign_added(struct pci_dev *dev)
         |                    ^~~~~~~~~~~~~~~~~~~~


vim +/pci_dev_assign_added +476 drivers/pci/proc.c

   463	
   464	static int __init pci_proc_init(void)
   465	{
   466		struct pci_dev *dev = NULL;
   467		proc_bus_pci_dir = proc_mkdir("bus/pci", NULL);
   468		proc_create_seq("devices", 0, proc_bus_pci_dir,
   469			    &proc_bus_pci_devices_op);
   470		proc_initialized = 1;
   471		pci_lock_rescan_remove();
   472		for_each_pci_dev(dev) {
   473			if (pci_dev_is_added(dev))
   474				continue;
   475			if (!pci_proc_attach_device(dev))
 > 476				pci_dev_assign_added(dev, true);

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki