Bug 215515

Summary: sysfs: cannot create duplicate filename '.../0000:e0'
Product: Drivers Reporter: Bjorn Helgaas (bjorn)
Component: PCIAssignee: drivers_pci (drivers_pci)
Status: NEW ---    
Severity: normal CC: decui, khalasa, korneliuszo
Priority: P1    
Hardware: All   
OS: Linux   
Kernel Version: 5.14 Subsystem:
Regression: No Bisected commit-id:
Attachments: Linux v5.16 kernel log
Debug patch used to generate the kernel log
Linux v5.16 .config file
serial console log of a VM crash on Azure.

Description Bjorn Helgaas 2022-01-21 16:54:05 UTC
Krzysztof Hałasa <khalasa@piap.pl> reports these errors with v5.14 on i.MX6-based Gateworks Ventana SBC:

  sysfs: cannot create duplicate filename '/devices/platform/soc/1ffc000.pcie/pci0000:00/0000:00:00.0/0000:e0'
  Unable to handle kernel paging request at virtual address 6f736572 (ASCII = "reso")

Initial report at https://lore.kernel.org/r/m3eebg9puj.fsf@t19.piap.pl
Comment 1 Krzysztof Halasa 2022-02-15 07:15:46 UTC
Created attachment 300462 [details]
Linux v5.16 kernel log

Attaching the kernel log from Gateworks Ventana SBC, v5.16 with only a small debug patch applied.
Comment 2 Krzysztof Halasa 2022-02-15 07:18:09 UTC
Created attachment 300463 [details]
Debug patch used to generate the kernel log
Comment 3 Krzysztof Halasa 2022-02-15 07:20:17 UTC
Created attachment 300464 [details]
Linux v5.16 .config file
Comment 4 Korneliusz Osmenda 2022-08-23 18:34:38 UTC
On 5.17.1 problem still exists.
Comment 5 Korneliusz Osmenda 2022-08-29 17:37:19 UTC
I've probably fixed this in:
https://github.com/korneliuszo/linux/commit/167139e40c43befecfa7cacd6b6c3fcf6f5a2a48

If you consider this patch worthy, on which mailing list should I add it?
Comment 6 Bjorn Helgaas 2022-08-29 18:13:54 UTC
Thanks for your work!  Please send the patch to the following addresses:

$ ./scripts/get_maintainer.pl drivers/pci/pci-sysfs.c
Bjorn Helgaas <bhelgaas@google.com> (supporter:PCI SUBSYSTEM)
linux-pci@vger.kernel.org (open list:PCI SUBSYSTEM)
linux-kernel@vger.kernel.org (open list)

Also see the hints here:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst
Comment 7 Dexuan Cui 2023-03-31 07:13:49 UTC
I think I'm still seeing the bug with 6.2.0-rc8. The panic log is attached: I got the log with 6.2.0-rc8 + my recent patchset https://lwn.net/ml/linux-kernel/20230328045122.25850-1-decui%40microsoft.com/ 
I don't think my patchset causes the panic.
Comment 8 Dexuan Cui 2023-03-31 07:14:50 UTC
Created attachment 304067 [details]
serial console log of a VM crash on Azure.
Comment 9 Dexuan Cui 2023-03-31 07:26:25 UTC
"Linux v5.16 kernel log" shows:
[    0.534071] sysfs: cannot create duplicate filename '/devices/platform/soc/1ffc000.pcie/pci0000:00/0000:00:00.0/resource0'

My log (serial console log of a VM crash on Azure) shows:
 [   21.364685] sysfs: cannot create duplicate filename '/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0004:00/VMBUS:00/29c2dd7f-fe78-4c21-9c4a-994ca4ffe153/pcife78:00/fe78:00:02.0/resource2'
[   21.370907] sysfs: cannot create duplicate filename '/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0004:00/VMBUS:00/4ad09f4b-92b8-49cf-928e-c5884b47a58c/pci92b8:00/92b8:00:02.0/resource2'
and the call-traces are pretty similar.

In my case, after sysfs_add_bin_file_mode_ns() returns -EEXIST, pci_create_resource_files() -> pci_create_attr() returns -EEXIST, and pci_create_resource_files() -> pci_remove_resource_files() removes the sysfile that's already created and calls kfree(res_attr), i.e. kfree(pdev->res_attr[i]) .

pci_bus_add_device() doesn't check the return value of pci_create_sysfs_dev_files(), so this -EEXIST error is ignored. Later, when the PCI device is removed by the host,  hv_eject_device_work() -> ... -> pci_remove_resource_files() -> sysfs_remove_bin_file(..., pdev->res_attr[i]) tries to remove the nonexistent sysfile, and triggers a NULL dereference in kernfs_remove_by_name_ns() -> … > strlen().
Comment 10 Dexuan Cui 2023-03-31 07:28:12 UTC
(In reply to Korneliusz Osmenda from comment #5)
> I've probably fixed this in:
> https://github.com/korneliuszo/linux/commit/
> 167139e40c43befecfa7cacd6b6c3fcf6f5a2a48
> 
> If you consider this patch worthy, on which mailing list should I add it?

Looks like the patch is still not in the upstream kernel. Would you please send it?